Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2399 lines
80KB

  1. /*
  2. * Carla Plugin Host
  3. * Copyright (C) 2011-2018 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. /* TODO:
  18. * - complete processRack(): carefully add to input, sorted events
  19. * - implement processPatchbay()
  20. * - implement oscSend_control_switch_plugins()
  21. * - something about the peaks?
  22. */
  23. #include "CarlaEngineInternal.hpp"
  24. #include "CarlaPlugin.hpp"
  25. #include "CarlaBackendUtils.hpp"
  26. #include "CarlaBinaryUtils.hpp"
  27. #include "CarlaEngineUtils.hpp"
  28. #include "CarlaMathUtils.hpp"
  29. #include "CarlaPipeUtils.hpp"
  30. #include "CarlaStateUtils.hpp"
  31. #include "CarlaMIDI.h"
  32. #include "jackbridge/JackBridge.hpp"
  33. #include "water/files/File.h"
  34. #include "water/streams/MemoryOutputStream.h"
  35. #include "water/xml/XmlDocument.h"
  36. #include "water/xml/XmlElement.h"
  37. // FIXME Remove on 2.1 release
  38. #include "lv2/atom.h"
  39. using water::Array;
  40. using water::CharPointer_UTF8;
  41. using water::File;
  42. using water::MemoryOutputStream;
  43. using water::String;
  44. using water::StringArray;
  45. using water::XmlDocument;
  46. using water::XmlElement;
  47. CARLA_BACKEND_START_NAMESPACE
  48. // -----------------------------------------------------------------------
  49. // Carla Engine
  50. CarlaEngine::CarlaEngine()
  51. : pData(new ProtectedData(this))
  52. {
  53. carla_debug("CarlaEngine::CarlaEngine()");
  54. }
  55. CarlaEngine::~CarlaEngine()
  56. {
  57. carla_debug("CarlaEngine::~CarlaEngine()");
  58. delete pData;
  59. }
  60. // -----------------------------------------------------------------------
  61. // Static calls
  62. uint CarlaEngine::getDriverCount()
  63. {
  64. carla_debug("CarlaEngine::getDriverCount()");
  65. uint count = 0;
  66. if (jackbridge_is_ok())
  67. count += 1;
  68. #ifndef BUILD_BRIDGE
  69. count += getRtAudioApiCount();
  70. #endif
  71. return count;
  72. }
  73. const char* CarlaEngine::getDriverName(const uint index2)
  74. {
  75. carla_debug("CarlaEngine::getDriverName(%i)", index2);
  76. uint index(index2);
  77. if (jackbridge_is_ok() && index-- == 0)
  78. return "JACK";
  79. #ifndef BUILD_BRIDGE
  80. if (const uint count = getRtAudioApiCount())
  81. {
  82. if (index < count)
  83. return getRtAudioApiName(index);
  84. }
  85. #endif
  86. carla_stderr("CarlaEngine::getDriverName(%i) - invalid index", index2);
  87. return nullptr;
  88. }
  89. const char* const* CarlaEngine::getDriverDeviceNames(const uint index2)
  90. {
  91. carla_debug("CarlaEngine::getDriverDeviceNames(%i)", index2);
  92. uint index(index2);
  93. if (jackbridge_is_ok() && index-- == 0)
  94. {
  95. static const char* ret[3] = { "Auto-Connect OFF", "Auto-Connect ON", nullptr };
  96. return ret;
  97. }
  98. #ifndef BUILD_BRIDGE
  99. if (const uint count = getRtAudioApiCount())
  100. {
  101. if (index < count)
  102. return getRtAudioApiDeviceNames(index);
  103. }
  104. #endif
  105. carla_stderr("CarlaEngine::getDriverDeviceNames(%i) - invalid index", index2);
  106. return nullptr;
  107. }
  108. const EngineDriverDeviceInfo* CarlaEngine::getDriverDeviceInfo(const uint index2, const char* const deviceName)
  109. {
  110. carla_debug("CarlaEngine::getDriverDeviceInfo(%i, \"%s\")", index2, deviceName);
  111. uint index(index2);
  112. if (jackbridge_is_ok() && index-- == 0)
  113. {
  114. static uint32_t bufSizes[11] = { 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 0 };
  115. static EngineDriverDeviceInfo devInfo;
  116. devInfo.hints = ENGINE_DRIVER_DEVICE_VARIABLE_BUFFER_SIZE;
  117. devInfo.bufferSizes = bufSizes;
  118. devInfo.sampleRates = nullptr;
  119. return &devInfo;
  120. }
  121. #ifndef BUILD_BRIDGE
  122. if (const uint count = getRtAudioApiCount())
  123. {
  124. if (index < count)
  125. return getRtAudioDeviceInfo(index, deviceName);
  126. }
  127. #endif
  128. carla_stderr("CarlaEngine::getDriverDeviceNames(%i, \"%s\") - invalid index", index2, deviceName);
  129. return nullptr;
  130. }
  131. CarlaEngine* CarlaEngine::newDriverByName(const char* const driverName)
  132. {
  133. CARLA_SAFE_ASSERT_RETURN(driverName != nullptr && driverName[0] != '\0', nullptr);
  134. carla_debug("CarlaEngine::newDriverByName(\"%s\")", driverName);
  135. if (std::strcmp(driverName, "JACK") == 0)
  136. return newJack();
  137. #ifndef BUILD_BRIDGE
  138. // -------------------------------------------------------------------
  139. // common
  140. if (std::strcmp(driverName, "Dummy") == 0)
  141. return newRtAudio(AUDIO_API_NULL);
  142. if (std::strncmp(driverName, "JACK ", 5) == 0)
  143. return newRtAudio(AUDIO_API_JACK);
  144. if (std::strcmp(driverName, "OSS") == 0)
  145. return newRtAudio(AUDIO_API_OSS);
  146. // -------------------------------------------------------------------
  147. // linux
  148. if (std::strcmp(driverName, "ALSA") == 0)
  149. return newRtAudio(AUDIO_API_ALSA);
  150. if (std::strcmp(driverName, "PulseAudio") == 0)
  151. return newRtAudio(AUDIO_API_PULSEAUDIO);
  152. // -------------------------------------------------------------------
  153. // macos
  154. if (std::strcmp(driverName, "CoreAudio") == 0)
  155. return newRtAudio(AUDIO_API_COREAUDIO);
  156. // -------------------------------------------------------------------
  157. // windows
  158. if (std::strcmp(driverName, "ASIO") == 0)
  159. return newRtAudio(AUDIO_API_ASIO);
  160. if (std::strcmp(driverName, "DirectSound") == 0)
  161. return newRtAudio(AUDIO_API_DIRECTSOUND);
  162. if (std::strcmp(driverName, "WASAPI") == 0)
  163. return newRtAudio(AUDIO_API_WASAPI);
  164. #endif
  165. carla_stderr("CarlaEngine::newDriverByName(\"%s\") - invalid driver name", driverName);
  166. return nullptr;
  167. }
  168. // -----------------------------------------------------------------------
  169. // Constant values
  170. uint CarlaEngine::getMaxClientNameSize() const noexcept
  171. {
  172. return STR_MAX/2;
  173. }
  174. uint CarlaEngine::getMaxPortNameSize() const noexcept
  175. {
  176. return STR_MAX;
  177. }
  178. uint CarlaEngine::getCurrentPluginCount() const noexcept
  179. {
  180. return pData->curPluginCount;
  181. }
  182. uint CarlaEngine::getMaxPluginNumber() const noexcept
  183. {
  184. return pData->maxPluginNumber;
  185. }
  186. // -----------------------------------------------------------------------
  187. // Virtual, per-engine type calls
  188. bool CarlaEngine::close()
  189. {
  190. carla_debug("CarlaEngine::close()");
  191. if (pData->curPluginCount != 0)
  192. {
  193. pData->aboutToClose = true;
  194. removeAllPlugins();
  195. }
  196. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  197. if (pData->osc.isControlRegistered())
  198. oscSend_control_exit();
  199. #endif
  200. pData->close();
  201. callback(ENGINE_CALLBACK_ENGINE_STOPPED, 0, 0, 0, 0.0f, nullptr);
  202. return true;
  203. }
  204. bool CarlaEngine::usesConstantBufferSize() const noexcept
  205. {
  206. return true;
  207. }
  208. void CarlaEngine::idle() noexcept
  209. {
  210. CARLA_SAFE_ASSERT_RETURN(pData->nextAction.opcode == kEnginePostActionNull,);
  211. CARLA_SAFE_ASSERT_RETURN(pData->nextPluginId == pData->maxPluginNumber,);
  212. CARLA_SAFE_ASSERT_RETURN(getType() != kEngineTypePlugin,);
  213. for (uint i=0; i < pData->curPluginCount; ++i)
  214. {
  215. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  216. if (plugin != nullptr && plugin->isEnabled())
  217. {
  218. const uint hints(plugin->getHints());
  219. if ((hints & PLUGIN_HAS_CUSTOM_UI) != 0 && (hints & PLUGIN_NEEDS_UI_MAIN_THREAD) != 0)
  220. {
  221. try {
  222. plugin->uiIdle();
  223. } CARLA_SAFE_EXCEPTION_CONTINUE("Plugin uiIdle");
  224. }
  225. }
  226. }
  227. #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  228. pData->osc.idle();
  229. #endif
  230. }
  231. CarlaEngineClient* CarlaEngine::addClient(CarlaPlugin* const)
  232. {
  233. return new CarlaEngineClient(*this);
  234. }
  235. // -----------------------------------------------------------------------
  236. // Plugin management
  237. bool CarlaEngine::addPlugin(const BinaryType btype, const PluginType ptype,
  238. const char* const filename, const char* const name, const char* const label, const int64_t uniqueId,
  239. const void* const extra, const uint options)
  240. {
  241. CARLA_SAFE_ASSERT_RETURN_ERR(pData->isIdling == 0, "An operation is still being processed, please wait for it to finish");
  242. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  243. CARLA_SAFE_ASSERT_RETURN_ERR(pData->plugins != nullptr, "Invalid engine internal data");
  244. CARLA_SAFE_ASSERT_RETURN_ERR(pData->nextPluginId <= pData->maxPluginNumber, "Invalid engine internal data");
  245. #endif
  246. CARLA_SAFE_ASSERT_RETURN_ERR(pData->nextAction.opcode == kEnginePostActionNull, "Invalid engine internal data");
  247. CARLA_SAFE_ASSERT_RETURN_ERR(btype != BINARY_NONE, "Invalid plugin binary mode");
  248. CARLA_SAFE_ASSERT_RETURN_ERR(ptype != PLUGIN_NONE, "Invalid plugin type");
  249. CARLA_SAFE_ASSERT_RETURN_ERR((filename != nullptr && filename[0] != '\0') || (label != nullptr && label[0] != '\0'), "Invalid plugin filename and label");
  250. carla_debug("CarlaEngine::addPlugin(%i:%s, %i:%s, \"%s\", \"%s\", \"%s\", " P_INT64 ", %p, %u)", btype, BinaryType2Str(btype), ptype, PluginType2Str(ptype), filename, name, label, uniqueId, extra, options);
  251. uint id;
  252. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  253. CarlaPlugin* oldPlugin = nullptr;
  254. if (pData->nextPluginId < pData->curPluginCount)
  255. {
  256. id = pData->nextPluginId;
  257. pData->nextPluginId = pData->maxPluginNumber;
  258. oldPlugin = pData->plugins[id].plugin;
  259. CARLA_SAFE_ASSERT_RETURN_ERR(oldPlugin != nullptr, "Invalid replace plugin Id");
  260. }
  261. else
  262. #endif
  263. {
  264. id = pData->curPluginCount;
  265. if (id == pData->maxPluginNumber)
  266. {
  267. setLastError("Maximum number of plugins reached");
  268. return false;
  269. }
  270. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  271. CARLA_SAFE_ASSERT_RETURN_ERR(pData->plugins[id].plugin == nullptr, "Invalid engine internal data");
  272. #endif
  273. }
  274. CarlaPlugin::Initializer initializer = {
  275. this,
  276. id,
  277. filename,
  278. name,
  279. label,
  280. uniqueId,
  281. options
  282. };
  283. CarlaPlugin* plugin = nullptr;
  284. CarlaString bridgeBinary(pData->options.binaryDir);
  285. if (bridgeBinary.isNotEmpty())
  286. {
  287. #ifndef CARLA_OS_WIN
  288. if (btype == BINARY_NATIVE)
  289. {
  290. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-native";
  291. }
  292. else
  293. #endif
  294. {
  295. switch (btype)
  296. {
  297. case BINARY_POSIX32:
  298. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-posix32";
  299. break;
  300. case BINARY_POSIX64:
  301. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-posix64";
  302. break;
  303. case BINARY_WIN32:
  304. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-win32.exe";
  305. break;
  306. case BINARY_WIN64:
  307. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-win64.exe";
  308. break;
  309. default:
  310. bridgeBinary.clear();
  311. break;
  312. }
  313. }
  314. if (! File(bridgeBinary.buffer()).existsAsFile())
  315. bridgeBinary.clear();
  316. }
  317. // Prefer bridges for some specific plugins
  318. bool preferBridges = pData->options.preferPluginBridges;
  319. #ifndef BUILD_BRIDGE
  320. if (! preferBridges)
  321. {
  322. if (ptype == PLUGIN_LV2 && label != nullptr)
  323. {
  324. if (std::strncmp(label, "http://calf.sourceforge.net/plugins/", 36) == 0 ||
  325. std::strcmp(label, "http://factorial.hu/plugins/lv2/ir") == 0 ||
  326. std::strstr(label, "v1.sourceforge.net/lv2") != nullptr)
  327. {
  328. preferBridges = true;
  329. }
  330. }
  331. }
  332. #endif // ! BUILD_BRIDGE
  333. const bool canBeBridged = ptype != PLUGIN_INTERNAL
  334. && ptype != PLUGIN_SF2
  335. && ptype != PLUGIN_SFZ
  336. && ptype != PLUGIN_JACK;
  337. if (canBeBridged && (btype != BINARY_NATIVE || (preferBridges && bridgeBinary.isNotEmpty())))
  338. {
  339. if (bridgeBinary.isNotEmpty())
  340. {
  341. plugin = CarlaPlugin::newBridge(initializer, btype, ptype, bridgeBinary);
  342. }
  343. else
  344. {
  345. setLastError("This Carla build cannot handle this binary");
  346. return false;
  347. }
  348. }
  349. else
  350. {
  351. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  352. bool use16Outs;
  353. #endif
  354. setLastError("Invalid or unsupported plugin type");
  355. switch (ptype)
  356. {
  357. case PLUGIN_NONE:
  358. break;
  359. case PLUGIN_LADSPA:
  360. plugin = CarlaPlugin::newLADSPA(initializer, (const LADSPA_RDF_Descriptor*)extra);
  361. break;
  362. case PLUGIN_DSSI:
  363. plugin = CarlaPlugin::newDSSI(initializer);
  364. break;
  365. case PLUGIN_LV2:
  366. plugin = CarlaPlugin::newLV2(initializer);
  367. break;
  368. case PLUGIN_VST2:
  369. plugin = CarlaPlugin::newVST2(initializer);
  370. break;
  371. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  372. case PLUGIN_INTERNAL:
  373. plugin = CarlaPlugin::newNative(initializer);
  374. break;
  375. case PLUGIN_SF2:
  376. use16Outs = (extra != nullptr && std::strcmp((const char*)extra, "true") == 0);
  377. plugin = CarlaPlugin::newFluidSynth(initializer, use16Outs);
  378. break;
  379. case PLUGIN_SFZ:
  380. plugin = CarlaPlugin::newSFZero(initializer);
  381. break;
  382. case PLUGIN_JACK:
  383. plugin = CarlaPlugin::newJackApp(initializer);
  384. break;
  385. #else
  386. case PLUGIN_INTERNAL:
  387. case PLUGIN_SF2:
  388. case PLUGIN_SFZ:
  389. case PLUGIN_JACK:
  390. setLastError("Plugin bridges cannot handle this binary");
  391. break;
  392. #endif
  393. }
  394. }
  395. if (plugin == nullptr)
  396. return false;
  397. plugin->reload();
  398. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  399. bool canRun = true;
  400. /**/ if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  401. {
  402. if (plugin->getCVInCount() > 0 || plugin->getCVInCount() > 0)
  403. {
  404. setLastError("Carla's rack mode cannot work with plugins that have CV ports, sorry!");
  405. canRun = false;
  406. }
  407. }
  408. else if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  409. {
  410. /**/ if (plugin->getMidiInCount() > 1 || plugin->getMidiOutCount() > 1)
  411. {
  412. setLastError("Carla's patchbay mode cannot work with plugins that have multiple MIDI ports, sorry!");
  413. canRun = false;
  414. }
  415. else if (plugin->getCVInCount() > 0 || plugin->getCVInCount() > 0)
  416. {
  417. setLastError("CV ports in patchbay mode is still TODO");
  418. canRun = false;
  419. }
  420. }
  421. if (! canRun)
  422. {
  423. delete plugin;
  424. return false;
  425. }
  426. # if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  427. plugin->registerToOscClient();
  428. # endif
  429. #endif
  430. EnginePluginData& pluginData(pData->plugins[id]);
  431. pluginData.plugin = plugin;
  432. pluginData.insPeak[0] = 0.0f;
  433. pluginData.insPeak[1] = 0.0f;
  434. pluginData.outsPeak[0] = 0.0f;
  435. pluginData.outsPeak[1] = 0.0f;
  436. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  437. if (oldPlugin != nullptr)
  438. {
  439. CARLA_SAFE_ASSERT(! pData->loadingProject);
  440. const ScopedThreadStopper sts(this);
  441. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  442. pData->graph.replacePlugin(oldPlugin, plugin);
  443. const bool wasActive = oldPlugin->getInternalParameterValue(PARAMETER_ACTIVE) >= 0.5f;
  444. const float oldDryWet = oldPlugin->getInternalParameterValue(PARAMETER_DRYWET);
  445. const float oldVolume = oldPlugin->getInternalParameterValue(PARAMETER_VOLUME);
  446. delete oldPlugin;
  447. if (plugin->getHints() & PLUGIN_CAN_DRYWET)
  448. plugin->setDryWet(oldDryWet, true, true);
  449. if (plugin->getHints() & PLUGIN_CAN_VOLUME)
  450. plugin->setVolume(oldVolume, true, true);
  451. plugin->setActive(wasActive, true, true);
  452. plugin->setEnabled(true);
  453. callback(ENGINE_CALLBACK_RELOAD_ALL, id, 0, 0, 0.0f, nullptr);
  454. }
  455. else if (! pData->loadingProject)
  456. #endif
  457. {
  458. plugin->setActive(true, true, false);
  459. plugin->setEnabled(true);
  460. ++pData->curPluginCount;
  461. callback(ENGINE_CALLBACK_PLUGIN_ADDED, id, 0, 0, 0.0f, plugin->getName());
  462. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  463. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  464. pData->graph.addPlugin(plugin);
  465. #endif
  466. }
  467. return true;
  468. }
  469. bool CarlaEngine::addPlugin(const PluginType ptype, const char* const filename, const char* const name, const char* const label, const int64_t uniqueId, const void* const extra)
  470. {
  471. return addPlugin(BINARY_NATIVE, ptype, filename, name, label, uniqueId, extra, 0x0);
  472. }
  473. bool CarlaEngine::removePlugin(const uint id)
  474. {
  475. CARLA_SAFE_ASSERT_RETURN_ERR(pData->isIdling == 0, "An operation is still being processed, please wait for it to finish");
  476. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  477. CARLA_SAFE_ASSERT_RETURN_ERR(pData->plugins != nullptr, "Invalid engine internal data");
  478. CARLA_SAFE_ASSERT_RETURN_ERR(pData->curPluginCount != 0, "Invalid engine internal data");
  479. #endif
  480. CARLA_SAFE_ASSERT_RETURN_ERR(pData->nextAction.opcode == kEnginePostActionNull, "Invalid engine internal data");
  481. CARLA_SAFE_ASSERT_RETURN_ERR(id < pData->curPluginCount, "Invalid plugin Id");
  482. carla_debug("CarlaEngine::removePlugin(%i)", id);
  483. CarlaPlugin* const plugin(pData->plugins[id].plugin);
  484. CARLA_SAFE_ASSERT_RETURN_ERR(plugin != nullptr, "Could not find plugin to remove");
  485. CARLA_SAFE_ASSERT_RETURN_ERR(plugin->getId() == id, "Invalid engine internal data");
  486. const ScopedThreadStopper sts(this);
  487. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  488. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  489. pData->graph.removePlugin(plugin);
  490. const ScopedActionLock sal(this, kEnginePostActionRemovePlugin, id, 0);
  491. /*
  492. for (uint i=id; i < pData->curPluginCount; ++i)
  493. {
  494. CarlaPlugin* const plugin2(pData->plugins[i].plugin);
  495. CARLA_SAFE_ASSERT_BREAK(plugin2 != nullptr);
  496. plugin2->updateOscURL();
  497. }
  498. */
  499. # if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  500. if (isOscControlRegistered())
  501. oscSend_control_remove_plugin(id);
  502. # endif
  503. #else
  504. pData->curPluginCount = 0;
  505. carla_zeroStructs(pData->plugins, 1);
  506. #endif
  507. delete plugin;
  508. callback(ENGINE_CALLBACK_PLUGIN_REMOVED, id, 0, 0, 0.0f, nullptr);
  509. return true;
  510. }
  511. bool CarlaEngine::removeAllPlugins()
  512. {
  513. CARLA_SAFE_ASSERT_RETURN_ERR(pData->isIdling == 0, "An operation is still being processed, please wait for it to finish");
  514. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  515. CARLA_SAFE_ASSERT_RETURN_ERR(pData->plugins != nullptr, "Invalid engine internal data");
  516. CARLA_SAFE_ASSERT_RETURN_ERR(pData->nextPluginId == pData->maxPluginNumber, "Invalid engine internal data");
  517. #endif
  518. CARLA_SAFE_ASSERT_RETURN_ERR(pData->nextAction.opcode == kEnginePostActionNull, "Invalid engine internal data");
  519. carla_debug("CarlaEngine::removeAllPlugins()");
  520. if (pData->curPluginCount == 0)
  521. return true;
  522. const ScopedThreadStopper sts(this);
  523. const uint curPluginCount(pData->curPluginCount);
  524. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  525. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  526. pData->graph.removeAllPlugins();
  527. # if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  528. if (isOscControlRegistered())
  529. {
  530. for (uint i=0; i < curPluginCount; ++i)
  531. oscSend_control_remove_plugin(curPluginCount-i-1);
  532. }
  533. # endif
  534. #endif
  535. const ScopedActionLock sal(this, kEnginePostActionZeroCount, 0, 0);
  536. callback(ENGINE_CALLBACK_IDLE, 0, 0, 0, 0.0f, nullptr);
  537. for (uint i=0; i < curPluginCount; ++i)
  538. {
  539. EnginePluginData& pluginData(pData->plugins[i]);
  540. if (pluginData.plugin != nullptr)
  541. {
  542. delete pluginData.plugin;
  543. pluginData.plugin = nullptr;
  544. }
  545. pluginData.insPeak[0] = 0.0f;
  546. pluginData.insPeak[1] = 0.0f;
  547. pluginData.outsPeak[0] = 0.0f;
  548. pluginData.outsPeak[1] = 0.0f;
  549. callback(ENGINE_CALLBACK_IDLE, 0, 0, 0, 0.0f, nullptr);
  550. }
  551. return true;
  552. }
  553. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  554. const char* CarlaEngine::renamePlugin(const uint id, const char* const newName)
  555. {
  556. CARLA_SAFE_ASSERT_RETURN_ERRN(pData->isIdling == 0, "An operation is still being processed, please wait for it to finish");
  557. CARLA_SAFE_ASSERT_RETURN_ERRN(pData->plugins != nullptr, "Invalid engine internal data");
  558. CARLA_SAFE_ASSERT_RETURN_ERRN(pData->curPluginCount != 0, "Invalid engine internal data");
  559. CARLA_SAFE_ASSERT_RETURN_ERRN(pData->nextAction.opcode == kEnginePostActionNull, "Invalid engine internal data");
  560. CARLA_SAFE_ASSERT_RETURN_ERRN(id < pData->curPluginCount, "Invalid plugin Id");
  561. CARLA_SAFE_ASSERT_RETURN_ERRN(newName != nullptr && newName[0] != '\0', "Invalid plugin name");
  562. carla_debug("CarlaEngine::renamePlugin(%i, \"%s\")", id, newName);
  563. CarlaPlugin* const plugin(pData->plugins[id].plugin);
  564. CARLA_SAFE_ASSERT_RETURN_ERRN(plugin != nullptr, "Could not find plugin to rename");
  565. CARLA_SAFE_ASSERT_RETURN_ERRN(plugin->getId() == id, "Invalid engine internal data");
  566. const char* const uniqueName(getUniquePluginName(newName));
  567. CARLA_SAFE_ASSERT_RETURN_ERRN(uniqueName != nullptr, "Unable to get new unique plugin name");
  568. plugin->setName(uniqueName);
  569. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  570. pData->graph.renamePlugin(plugin, uniqueName);
  571. delete[] uniqueName;
  572. return plugin->getName();
  573. }
  574. bool CarlaEngine::clonePlugin(const uint id)
  575. {
  576. CARLA_SAFE_ASSERT_RETURN_ERR(pData->isIdling == 0, "An operation is still being processed, please wait for it to finish");
  577. CARLA_SAFE_ASSERT_RETURN_ERR(pData->plugins != nullptr, "Invalid engine internal data");
  578. CARLA_SAFE_ASSERT_RETURN_ERR(pData->curPluginCount != 0, "Invalid engine internal data");
  579. CARLA_SAFE_ASSERT_RETURN_ERR(pData->nextAction.opcode == kEnginePostActionNull, "Invalid engine internal data");
  580. CARLA_SAFE_ASSERT_RETURN_ERR(id < pData->curPluginCount, "Invalid plugin Id");
  581. carla_debug("CarlaEngine::clonePlugin(%i)", id);
  582. CarlaPlugin* const plugin(pData->plugins[id].plugin);
  583. CARLA_SAFE_ASSERT_RETURN_ERR(plugin != nullptr, "Could not find plugin to clone");
  584. CARLA_SAFE_ASSERT_RETURN_ERR(plugin->getId() == id, "Invalid engine internal data");
  585. char label[STR_MAX+1];
  586. carla_zeroChars(label, STR_MAX+1);
  587. plugin->getLabel(label);
  588. const uint pluginCountBefore(pData->curPluginCount);
  589. if (! addPlugin(plugin->getBinaryType(), plugin->getType(),
  590. plugin->getFilename(), plugin->getName(), label, plugin->getUniqueId(),
  591. plugin->getExtraStuff(), plugin->getOptionsEnabled()))
  592. return false;
  593. CARLA_SAFE_ASSERT_RETURN_ERR(pluginCountBefore+1 == pData->curPluginCount, "No new plugin found");
  594. if (CarlaPlugin* const newPlugin = pData->plugins[pluginCountBefore].plugin)
  595. newPlugin->loadStateSave(plugin->getStateSave());
  596. return true;
  597. }
  598. bool CarlaEngine::replacePlugin(const uint id) noexcept
  599. {
  600. CARLA_SAFE_ASSERT_RETURN_ERR(pData->isIdling == 0, "An operation is still being processed, please wait for it to finish");
  601. CARLA_SAFE_ASSERT_RETURN_ERR(pData->plugins != nullptr, "Invalid engine internal data");
  602. CARLA_SAFE_ASSERT_RETURN_ERR(pData->curPluginCount != 0, "Invalid engine internal data");
  603. CARLA_SAFE_ASSERT_RETURN_ERR(pData->nextAction.opcode == kEnginePostActionNull, "Invalid engine internal data");
  604. carla_debug("CarlaEngine::replacePlugin(%i)", id);
  605. // might use this to reset
  606. if (id == pData->maxPluginNumber)
  607. {
  608. pData->nextPluginId = pData->maxPluginNumber;
  609. return true;
  610. }
  611. CARLA_SAFE_ASSERT_RETURN_ERR(id < pData->curPluginCount, "Invalid plugin Id");
  612. CarlaPlugin* const plugin(pData->plugins[id].plugin);
  613. CARLA_SAFE_ASSERT_RETURN_ERR(plugin != nullptr, "Could not find plugin to replace");
  614. CARLA_SAFE_ASSERT_RETURN_ERR(plugin->getId() == id, "Invalid engine internal data");
  615. pData->nextPluginId = id;
  616. return true;
  617. }
  618. bool CarlaEngine::switchPlugins(const uint idA, const uint idB) noexcept
  619. {
  620. CARLA_SAFE_ASSERT_RETURN_ERR(pData->isIdling == 0, "An operation is still being processed, please wait for it to finish");
  621. CARLA_SAFE_ASSERT_RETURN_ERR(pData->plugins != nullptr, "Invalid engine internal data");
  622. CARLA_SAFE_ASSERT_RETURN_ERR(pData->curPluginCount >= 2, "Invalid engine internal data");
  623. CARLA_SAFE_ASSERT_RETURN_ERR(pData->nextAction.opcode == kEnginePostActionNull, "Invalid engine internal data");
  624. CARLA_SAFE_ASSERT_RETURN_ERR(idA != idB, "Invalid operation, cannot switch plugin with itself");
  625. CARLA_SAFE_ASSERT_RETURN_ERR(idA < pData->curPluginCount, "Invalid plugin Id");
  626. CARLA_SAFE_ASSERT_RETURN_ERR(idB < pData->curPluginCount, "Invalid plugin Id");
  627. carla_debug("CarlaEngine::switchPlugins(%i)", idA, idB);
  628. CarlaPlugin* const pluginA(pData->plugins[idA].plugin);
  629. CarlaPlugin* const pluginB(pData->plugins[idB].plugin);
  630. CARLA_SAFE_ASSERT_RETURN_ERR(pluginA != nullptr, "Could not find plugin to switch");
  631. CARLA_SAFE_ASSERT_RETURN_ERR(pluginA != nullptr, "Could not find plugin to switch");
  632. CARLA_SAFE_ASSERT_RETURN_ERR(pluginA->getId() == idA, "Invalid engine internal data");
  633. CARLA_SAFE_ASSERT_RETURN_ERR(pluginB->getId() == idB, "Invalid engine internal data");
  634. const ScopedThreadStopper sts(this);
  635. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  636. pData->graph.replacePlugin(pluginA, pluginB);
  637. const ScopedActionLock sal(this, kEnginePostActionSwitchPlugins, idA, idB);
  638. // TODO
  639. /*
  640. pluginA->updateOscURL();
  641. pluginB->updateOscURL();
  642. if (isOscControlRegistered())
  643. oscSend_control_switch_plugins(idA, idB);
  644. */
  645. return true;
  646. }
  647. #endif
  648. CarlaPlugin* CarlaEngine::getPlugin(const uint id) const noexcept
  649. {
  650. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  651. CARLA_SAFE_ASSERT_RETURN_ERRN(pData->plugins != nullptr, "Invalid engine internal data");
  652. CARLA_SAFE_ASSERT_RETURN_ERRN(pData->curPluginCount != 0, "Invalid engine internal data");
  653. #endif
  654. CARLA_SAFE_ASSERT_RETURN_ERRN(pData->nextAction.opcode == kEnginePostActionNull, "Invalid engine internal data");
  655. CARLA_SAFE_ASSERT_RETURN_ERRN(id < pData->curPluginCount, "Invalid plugin Id");
  656. return pData->plugins[id].plugin;
  657. }
  658. CarlaPlugin* CarlaEngine::getPluginUnchecked(const uint id) const noexcept
  659. {
  660. return pData->plugins[id].plugin;
  661. }
  662. const char* CarlaEngine::getUniquePluginName(const char* const name) const
  663. {
  664. CARLA_SAFE_ASSERT_RETURN(pData->nextAction.opcode == kEnginePostActionNull, nullptr);
  665. CARLA_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0', nullptr);
  666. carla_debug("CarlaEngine::getUniquePluginName(\"%s\")", name);
  667. CarlaString sname;
  668. sname = name;
  669. if (sname.isEmpty())
  670. {
  671. sname = "(No name)";
  672. return sname.dup();
  673. }
  674. const std::size_t maxNameSize(carla_minConstrained<uint>(getMaxClientNameSize(), 0xff, 6U) - 6); // 6 = strlen(" (10)") + 1
  675. if (maxNameSize == 0 || ! isRunning())
  676. return sname.dup();
  677. sname.truncate(maxNameSize);
  678. sname.replace(':', '.'); // ':' is used in JACK1 to split client/port names
  679. for (uint i=0; i < pData->curPluginCount; ++i)
  680. {
  681. CARLA_SAFE_ASSERT_BREAK(pData->plugins[i].plugin != nullptr);
  682. // Check if unique name doesn't exist
  683. if (const char* const pluginName = pData->plugins[i].plugin->getName())
  684. {
  685. if (sname != pluginName)
  686. continue;
  687. }
  688. // Check if string has already been modified
  689. {
  690. const std::size_t len(sname.length());
  691. // 1 digit, ex: " (2)"
  692. if (sname[len-4] == ' ' && sname[len-3] == '(' && sname.isDigit(len-2) && sname[len-1] == ')')
  693. {
  694. const int number = sname[len-2] - '0';
  695. if (number == 9)
  696. {
  697. // next number is 10, 2 digits
  698. sname.truncate(len-4);
  699. sname += " (10)";
  700. //sname.replace(" (9)", " (10)");
  701. }
  702. else
  703. sname[len-2] = char('0' + number + 1);
  704. continue;
  705. }
  706. // 2 digits, ex: " (11)"
  707. if (sname[len-5] == ' ' && sname[len-4] == '(' && sname.isDigit(len-3) && sname.isDigit(len-2) && sname[len-1] == ')')
  708. {
  709. char n2 = sname[len-2];
  710. char n3 = sname[len-3];
  711. if (n2 == '9')
  712. {
  713. n2 = '0';
  714. n3 = static_cast<char>(n3 + 1);
  715. }
  716. else
  717. n2 = static_cast<char>(n2 + 1);
  718. sname[len-2] = n2;
  719. sname[len-3] = n3;
  720. continue;
  721. }
  722. }
  723. // Modify string if not
  724. sname += " (2)";
  725. }
  726. return sname.dup();
  727. }
  728. // -----------------------------------------------------------------------
  729. // Project management
  730. bool CarlaEngine::loadFile(const char* const filename)
  731. {
  732. CARLA_SAFE_ASSERT_RETURN_ERR(pData->isIdling == 0, "An operation is still being processed, please wait for it to finish");
  733. CARLA_SAFE_ASSERT_RETURN_ERR(filename != nullptr && filename[0] != '\0', "Invalid filename");
  734. carla_debug("CarlaEngine::loadFile(\"%s\")", filename);
  735. const String jfilename = String(CharPointer_UTF8(filename));
  736. File file(jfilename);
  737. CARLA_SAFE_ASSERT_RETURN_ERR(file.exists(), "Requested file does not exist or is not a readable");
  738. CarlaString baseName(file.getFileNameWithoutExtension().toRawUTF8());
  739. CarlaString extension(file.getFileExtension().replace(".","").toLowerCase().toRawUTF8());
  740. const uint curPluginId(pData->nextPluginId < pData->curPluginCount ? pData->nextPluginId : pData->curPluginCount);
  741. // -------------------------------------------------------------------
  742. // NOTE: please keep in sync with carla_get_supported_file_extensions!!
  743. if (extension == "carxp" || extension == "carxs")
  744. return loadProject(filename);
  745. // -------------------------------------------------------------------
  746. if (extension == "sf2" || extension == "sf3")
  747. return addPlugin(PLUGIN_SF2, filename, baseName, baseName, 0, nullptr);
  748. if (extension == "sfz")
  749. return addPlugin(PLUGIN_SFZ, filename, baseName, baseName, 0, nullptr);
  750. // -------------------------------------------------------------------
  751. if (
  752. #ifdef HAVE_SNDFILE
  753. extension == "aif" ||
  754. extension == "aifc" ||
  755. extension == "aiff" ||
  756. extension == "au" ||
  757. extension == "bwf" ||
  758. extension == "flac" ||
  759. extension == "htk" ||
  760. extension == "iff" ||
  761. extension == "mat4" ||
  762. extension == "mat5" ||
  763. extension == "oga" ||
  764. extension == "ogg" ||
  765. extension == "paf" ||
  766. extension == "pvf" ||
  767. extension == "pvf5" ||
  768. extension == "sd2" ||
  769. extension == "sf" ||
  770. extension == "snd" ||
  771. extension == "svx" ||
  772. extension == "vcc" ||
  773. extension == "w64" ||
  774. extension == "wav" ||
  775. extension == "xi" ||
  776. #endif
  777. #ifdef HAVE_FFMPEG
  778. extension == "3g2" ||
  779. extension == "3gp" ||
  780. extension == "aac" ||
  781. extension == "ac3" ||
  782. extension == "amr" ||
  783. extension == "ape" ||
  784. extension == "mp2" ||
  785. extension == "mp3" ||
  786. extension == "mpc" ||
  787. extension == "wma" ||
  788. # ifdef HAVE_SNDFILE
  789. // FFmpeg without sndfile
  790. extension == "flac" ||
  791. extension == "oga" ||
  792. extension == "ogg" ||
  793. extension == "w64" ||
  794. extension == "wav" ||
  795. # endif
  796. #endif
  797. false
  798. )
  799. {
  800. if (addPlugin(PLUGIN_INTERNAL, nullptr, baseName, "audiofile", 0, nullptr))
  801. {
  802. if (CarlaPlugin* const plugin = getPlugin(curPluginId))
  803. plugin->setCustomData(CUSTOM_DATA_TYPE_STRING, "file", filename, true);
  804. return true;
  805. }
  806. return false;
  807. }
  808. // -------------------------------------------------------------------
  809. if (extension == "mid" || extension == "midi")
  810. {
  811. if (addPlugin(PLUGIN_INTERNAL, nullptr, baseName, "midifile", 0, nullptr))
  812. {
  813. if (CarlaPlugin* const plugin = getPlugin(curPluginId))
  814. plugin->setCustomData(CUSTOM_DATA_TYPE_STRING, "file", filename, true);
  815. return true;
  816. }
  817. return false;
  818. }
  819. // -------------------------------------------------------------------
  820. // ZynAddSubFX
  821. if (extension == "xmz" || extension == "xiz")
  822. {
  823. #ifdef HAVE_ZYN_DEPS
  824. CarlaString nicerName("Zyn - ");
  825. const std::size_t sep(baseName.find('-')+1);
  826. if (sep < baseName.length())
  827. nicerName += baseName.buffer()+sep;
  828. else
  829. nicerName += baseName;
  830. //nicerName
  831. if (addPlugin(PLUGIN_INTERNAL, nullptr, nicerName, "zynaddsubfx", 0, nullptr))
  832. {
  833. callback(ENGINE_CALLBACK_UI_STATE_CHANGED, curPluginId, 0, 0, 0.0f, nullptr);
  834. if (CarlaPlugin* const plugin = getPlugin(curPluginId))
  835. plugin->setCustomData(CUSTOM_DATA_TYPE_STRING, (extension == "xmz") ? "CarlaAlternateFile1" : "CarlaAlternateFile2", filename, true);
  836. return true;
  837. }
  838. return false;
  839. #else
  840. setLastError("This Carla build does not have ZynAddSubFX support");
  841. return false;
  842. #endif
  843. }
  844. // -------------------------------------------------------------------
  845. // Direct plugin binaries
  846. #ifdef CARLA_OS_MAC
  847. if (extension == "vst")
  848. return addPlugin(PLUGIN_VST2, filename, nullptr, nullptr, 0, nullptr);
  849. #else
  850. if (extension == "dll" || extension == "so")
  851. return addPlugin(getBinaryTypeFromFile(filename), PLUGIN_VST2, filename, nullptr, nullptr, 0, nullptr, 0x0);
  852. #endif
  853. // -------------------------------------------------------------------
  854. setLastError("Unknown file extension");
  855. return false;
  856. }
  857. bool CarlaEngine::loadProject(const char* const filename)
  858. {
  859. CARLA_SAFE_ASSERT_RETURN_ERR(pData->isIdling == 0, "An operation is still being processed, please wait for it to finish");
  860. CARLA_SAFE_ASSERT_RETURN_ERR(filename != nullptr && filename[0] != '\0', "Invalid filename");
  861. carla_debug("CarlaEngine::loadProject(\"%s\")", filename);
  862. const String jfilename = String(CharPointer_UTF8(filename));
  863. File file(jfilename);
  864. CARLA_SAFE_ASSERT_RETURN_ERR(file.existsAsFile(), "Requested file does not exist or is not a readable file");
  865. XmlDocument xml(file);
  866. return loadProjectInternal(xml);
  867. }
  868. bool CarlaEngine::saveProject(const char* const filename)
  869. {
  870. CARLA_SAFE_ASSERT_RETURN_ERR(filename != nullptr && filename[0] != '\0', "Invalid filename");
  871. carla_debug("CarlaEngine::saveProject(\"%s\")", filename);
  872. MemoryOutputStream out;
  873. saveProjectInternal(out);
  874. const String jfilename = String(CharPointer_UTF8(filename));
  875. File file(jfilename);
  876. if (file.replaceWithData(out.getData(), out.getDataSize()))
  877. return true;
  878. setLastError("Failed to write file");
  879. return false;
  880. }
  881. // -----------------------------------------------------------------------
  882. // Information (base)
  883. uint CarlaEngine::getHints() const noexcept
  884. {
  885. return pData->hints;
  886. }
  887. uint32_t CarlaEngine::getBufferSize() const noexcept
  888. {
  889. return pData->bufferSize;
  890. }
  891. double CarlaEngine::getSampleRate() const noexcept
  892. {
  893. return pData->sampleRate;
  894. }
  895. const char* CarlaEngine::getName() const noexcept
  896. {
  897. return pData->name;
  898. }
  899. EngineProcessMode CarlaEngine::getProccessMode() const noexcept
  900. {
  901. return pData->options.processMode;
  902. }
  903. const EngineOptions& CarlaEngine::getOptions() const noexcept
  904. {
  905. return pData->options;
  906. }
  907. const EngineTimeInfo& CarlaEngine::getTimeInfo() const noexcept
  908. {
  909. return pData->timeInfo;
  910. }
  911. // -----------------------------------------------------------------------
  912. // Information (peaks)
  913. float CarlaEngine::getInputPeak(const uint pluginId, const bool isLeft) const noexcept
  914. {
  915. if (pluginId == MAIN_CARLA_PLUGIN_ID)
  916. {
  917. // get peak from first plugin, if available
  918. if (pData->curPluginCount > 0)
  919. return pData->plugins[0].insPeak[isLeft ? 0 : 1];
  920. return 0.0f;
  921. }
  922. CARLA_SAFE_ASSERT_RETURN(pluginId < pData->curPluginCount, 0.0f);
  923. return pData->plugins[pluginId].insPeak[isLeft ? 0 : 1];
  924. }
  925. float CarlaEngine::getOutputPeak(const uint pluginId, const bool isLeft) const noexcept
  926. {
  927. if (pluginId == MAIN_CARLA_PLUGIN_ID)
  928. {
  929. // get peak from last plugin, if available
  930. if (pData->curPluginCount > 0)
  931. return pData->plugins[pData->curPluginCount-1].outsPeak[isLeft ? 0 : 1];
  932. return 0.0f;
  933. }
  934. CARLA_SAFE_ASSERT_RETURN(pluginId < pData->curPluginCount, 0.0f);
  935. return pData->plugins[pluginId].outsPeak[isLeft ? 0 : 1];
  936. }
  937. // -----------------------------------------------------------------------
  938. // Callback
  939. void CarlaEngine::callback(const EngineCallbackOpcode action, const uint pluginId, const int value1, const int value2, const float value3, const char* const valueStr) noexcept
  940. {
  941. #ifdef DEBUG
  942. if (action != ENGINE_CALLBACK_IDLE && action != ENGINE_CALLBACK_NOTE_ON && action != ENGINE_CALLBACK_NOTE_OFF)
  943. carla_debug("CarlaEngine::callback(%i:%s, %i, %i, %i, %f, \"%s\")", action, EngineCallbackOpcode2Str(action), pluginId, value1, value2, value3, valueStr);
  944. #endif
  945. #ifdef BUILD_BRIDGE_ALTERNATIVE_ARCH
  946. if (pData->isIdling)
  947. #else
  948. if (pData->isIdling && action != ENGINE_CALLBACK_PATCHBAY_CLIENT_DATA_CHANGED)
  949. #endif
  950. {
  951. carla_stdout("callback while idling (%i:%s, %i, %i, %i, %f, \"%s\")", action, EngineCallbackOpcode2Str(action), pluginId, value1, value2, value3, valueStr);
  952. }
  953. if (pData->callback != nullptr)
  954. {
  955. if (action == ENGINE_CALLBACK_IDLE)
  956. ++pData->isIdling;
  957. try {
  958. pData->callback(pData->callbackPtr, action, pluginId, value1, value2, value3, valueStr);
  959. #if defined(CARLA_OS_LINUX) && defined(__arm__)
  960. } catch (__cxxabiv1::__forced_unwind&) {
  961. carla_stderr2("Caught forced unwind exception in callback");
  962. throw;
  963. #endif
  964. } catch (...) {
  965. carla_safe_exception("callback", __FILE__, __LINE__);
  966. }
  967. if (action == ENGINE_CALLBACK_IDLE)
  968. --pData->isIdling;
  969. }
  970. }
  971. void CarlaEngine::setCallback(const EngineCallbackFunc func, void* const ptr) noexcept
  972. {
  973. carla_debug("CarlaEngine::setCallback(%p, %p)", func, ptr);
  974. pData->callback = func;
  975. pData->callbackPtr = ptr;
  976. }
  977. // -----------------------------------------------------------------------
  978. // File Callback
  979. const char* CarlaEngine::runFileCallback(const FileCallbackOpcode action, const bool isDir, const char* const title, const char* const filter) noexcept
  980. {
  981. CARLA_SAFE_ASSERT_RETURN(title != nullptr && title[0] != '\0', nullptr);
  982. CARLA_SAFE_ASSERT_RETURN(filter != nullptr, nullptr);
  983. carla_debug("CarlaEngine::runFileCallback(%i:%s, %s, \"%s\", \"%s\")", action, FileCallbackOpcode2Str(action), bool2str(isDir), title, filter);
  984. const char* ret = nullptr;
  985. if (pData->fileCallback != nullptr)
  986. {
  987. try {
  988. ret = pData->fileCallback(pData->fileCallbackPtr, action, isDir, title, filter);
  989. } CARLA_SAFE_EXCEPTION("runFileCallback");
  990. }
  991. return ret;
  992. }
  993. void CarlaEngine::setFileCallback(const FileCallbackFunc func, void* const ptr) noexcept
  994. {
  995. carla_debug("CarlaEngine::setFileCallback(%p, %p)", func, ptr);
  996. pData->fileCallback = func;
  997. pData->fileCallbackPtr = ptr;
  998. }
  999. // -----------------------------------------------------------------------
  1000. // Transport
  1001. void CarlaEngine::transportPlay() noexcept
  1002. {
  1003. pData->timeInfo.playing = true;
  1004. pData->time.setNeedsReset();
  1005. }
  1006. void CarlaEngine::transportPause() noexcept
  1007. {
  1008. if (pData->timeInfo.playing)
  1009. pData->time.pause();
  1010. else
  1011. pData->time.setNeedsReset();
  1012. }
  1013. void CarlaEngine::transportBPM(const double bpm) noexcept
  1014. {
  1015. try {
  1016. pData->time.setBPM(bpm);
  1017. } CARLA_SAFE_EXCEPTION("CarlaEngine::transportBPM");
  1018. }
  1019. void CarlaEngine::transportRelocate(const uint64_t frame) noexcept
  1020. {
  1021. pData->time.relocate(frame);
  1022. }
  1023. // -----------------------------------------------------------------------
  1024. // Error handling
  1025. const char* CarlaEngine::getLastError() const noexcept
  1026. {
  1027. return pData->lastError;
  1028. }
  1029. void CarlaEngine::setLastError(const char* const error) const noexcept
  1030. {
  1031. pData->lastError = error;
  1032. }
  1033. // -----------------------------------------------------------------------
  1034. // Misc
  1035. bool CarlaEngine::isAboutToClose() const noexcept
  1036. {
  1037. return pData->aboutToClose;
  1038. }
  1039. bool CarlaEngine::setAboutToClose() noexcept
  1040. {
  1041. carla_debug("CarlaEngine::setAboutToClose()");
  1042. pData->aboutToClose = true;
  1043. return (pData->isIdling == 0);
  1044. }
  1045. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1046. bool CarlaEngine::isLoadingProject() const noexcept
  1047. {
  1048. return pData->loadingProject;
  1049. }
  1050. #endif
  1051. // -----------------------------------------------------------------------
  1052. // Global options
  1053. void CarlaEngine::setOption(const EngineOption option, const int value, const char* const valueStr) noexcept
  1054. {
  1055. carla_debug("CarlaEngine::setOption(%i:%s, %i, \"%s\")", option, EngineOption2Str(option), value, valueStr);
  1056. if (isRunning())
  1057. {
  1058. switch (option)
  1059. {
  1060. case ENGINE_OPTION_PROCESS_MODE:
  1061. case ENGINE_OPTION_AUDIO_TRIPLE_BUFFER:
  1062. case ENGINE_OPTION_AUDIO_DEVICE:
  1063. return carla_stderr("CarlaEngine::setOption(%i:%s, %i, \"%s\") - Cannot set this option while engine is running!",
  1064. option, EngineOption2Str(option), value, valueStr);
  1065. default:
  1066. break;
  1067. }
  1068. }
  1069. // do not un-force stereo for rack mode
  1070. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK && option == ENGINE_OPTION_FORCE_STEREO && value != 0)
  1071. return;
  1072. switch (option)
  1073. {
  1074. case ENGINE_OPTION_DEBUG:
  1075. break;
  1076. case ENGINE_OPTION_PROCESS_MODE:
  1077. CARLA_SAFE_ASSERT_RETURN(value >= ENGINE_PROCESS_MODE_SINGLE_CLIENT && value <= ENGINE_PROCESS_MODE_BRIDGE,);
  1078. pData->options.processMode = static_cast<EngineProcessMode>(value);
  1079. break;
  1080. case ENGINE_OPTION_TRANSPORT_MODE:
  1081. CARLA_SAFE_ASSERT_RETURN(value >= ENGINE_TRANSPORT_MODE_DISABLED && value <= ENGINE_TRANSPORT_MODE_BRIDGE,);
  1082. CARLA_SAFE_ASSERT_RETURN(getType() == kEngineTypeJack || value != ENGINE_TRANSPORT_MODE_JACK,);
  1083. pData->options.transportMode = static_cast<EngineTransportMode>(value);
  1084. delete[] pData->options.transportExtra;
  1085. if (value >= ENGINE_TRANSPORT_MODE_DISABLED && valueStr != nullptr)
  1086. pData->options.transportExtra = carla_strdup_safe(valueStr);
  1087. else
  1088. pData->options.transportExtra = nullptr;
  1089. pData->time.setNeedsReset();
  1090. #if defined(HAVE_HYLIA) && !defined(BUILD_BRIDGE)
  1091. // enable link now if needed
  1092. {
  1093. const bool linkEnabled = pData->options.transportExtra != nullptr && std::strstr(pData->options.transportExtra, ":link:") != nullptr;
  1094. pData->time.enableLink(linkEnabled);
  1095. }
  1096. #endif
  1097. break;
  1098. case ENGINE_OPTION_FORCE_STEREO:
  1099. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  1100. pData->options.forceStereo = (value != 0);
  1101. break;
  1102. case ENGINE_OPTION_PREFER_PLUGIN_BRIDGES:
  1103. #ifdef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1104. CARLA_SAFE_ASSERT_RETURN(value == 0,);
  1105. #else
  1106. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  1107. #endif
  1108. pData->options.preferPluginBridges = (value != 0);
  1109. break;
  1110. case ENGINE_OPTION_PREFER_UI_BRIDGES:
  1111. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  1112. pData->options.preferUiBridges = (value != 0);
  1113. break;
  1114. case ENGINE_OPTION_UIS_ALWAYS_ON_TOP:
  1115. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  1116. pData->options.uisAlwaysOnTop = (value != 0);
  1117. break;
  1118. case ENGINE_OPTION_MAX_PARAMETERS:
  1119. CARLA_SAFE_ASSERT_RETURN(value >= 0,);
  1120. pData->options.maxParameters = static_cast<uint>(value);
  1121. break;
  1122. case ENGINE_OPTION_UI_BRIDGES_TIMEOUT:
  1123. CARLA_SAFE_ASSERT_RETURN(value >= 0,);
  1124. pData->options.uiBridgesTimeout = static_cast<uint>(value);
  1125. break;
  1126. case ENGINE_OPTION_AUDIO_BUFFER_SIZE:
  1127. CARLA_SAFE_ASSERT_RETURN(value >= 8,);
  1128. pData->options.audioBufferSize = static_cast<uint>(value);
  1129. break;
  1130. case ENGINE_OPTION_AUDIO_SAMPLE_RATE:
  1131. CARLA_SAFE_ASSERT_RETURN(value >= 22050,);
  1132. pData->options.audioSampleRate = static_cast<uint>(value);
  1133. break;
  1134. case ENGINE_OPTION_AUDIO_TRIPLE_BUFFER:
  1135. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  1136. pData->options.audioTripleBuffer = (value != 0);
  1137. break;
  1138. case ENGINE_OPTION_AUDIO_DEVICE:
  1139. CARLA_SAFE_ASSERT_RETURN(valueStr != nullptr,);
  1140. if (pData->options.audioDevice != nullptr)
  1141. delete[] pData->options.audioDevice;
  1142. pData->options.audioDevice = carla_strdup_safe(valueStr);
  1143. break;
  1144. case ENGINE_OPTION_PLUGIN_PATH:
  1145. CARLA_SAFE_ASSERT_RETURN(value > PLUGIN_NONE,);
  1146. CARLA_SAFE_ASSERT_RETURN(value <= PLUGIN_SFZ,);
  1147. switch (value)
  1148. {
  1149. case PLUGIN_LADSPA:
  1150. if (pData->options.pathLADSPA != nullptr)
  1151. delete[] pData->options.pathLADSPA;
  1152. if (valueStr != nullptr)
  1153. pData->options.pathLADSPA = carla_strdup_safe(valueStr);
  1154. else
  1155. pData->options.pathLADSPA = nullptr;
  1156. break;
  1157. case PLUGIN_DSSI:
  1158. if (pData->options.pathDSSI != nullptr)
  1159. delete[] pData->options.pathDSSI;
  1160. if (valueStr != nullptr)
  1161. pData->options.pathDSSI = carla_strdup_safe(valueStr);
  1162. else
  1163. pData->options.pathDSSI = nullptr;
  1164. break;
  1165. case PLUGIN_LV2:
  1166. if (pData->options.pathLV2 != nullptr)
  1167. delete[] pData->options.pathLV2;
  1168. if (valueStr != nullptr)
  1169. pData->options.pathLV2 = carla_strdup_safe(valueStr);
  1170. else
  1171. pData->options.pathLV2 = nullptr;
  1172. break;
  1173. case PLUGIN_VST2:
  1174. if (pData->options.pathVST2 != nullptr)
  1175. delete[] pData->options.pathVST2;
  1176. if (valueStr != nullptr)
  1177. pData->options.pathVST2 = carla_strdup_safe(valueStr);
  1178. else
  1179. pData->options.pathVST2 = nullptr;
  1180. break;
  1181. case PLUGIN_SF2:
  1182. if (pData->options.pathSF2 != nullptr)
  1183. delete[] pData->options.pathSF2;
  1184. if (valueStr != nullptr)
  1185. pData->options.pathSF2 = carla_strdup_safe(valueStr);
  1186. else
  1187. pData->options.pathSF2 = nullptr;
  1188. break;
  1189. case PLUGIN_SFZ:
  1190. if (pData->options.pathSFZ != nullptr)
  1191. delete[] pData->options.pathSFZ;
  1192. if (valueStr != nullptr)
  1193. pData->options.pathSFZ = carla_strdup_safe(valueStr);
  1194. else
  1195. pData->options.pathSFZ = nullptr;
  1196. break;
  1197. default:
  1198. return carla_stderr("CarlaEngine::setOption(%i:%s, %i, \"%s\") - Invalid plugin type", option, EngineOption2Str(option), value, valueStr);
  1199. break;
  1200. }
  1201. break;
  1202. case ENGINE_OPTION_PATH_BINARIES:
  1203. CARLA_SAFE_ASSERT_RETURN(valueStr != nullptr && valueStr[0] != '\0',);
  1204. if (pData->options.binaryDir != nullptr)
  1205. delete[] pData->options.binaryDir;
  1206. pData->options.binaryDir = carla_strdup_safe(valueStr);
  1207. break;
  1208. case ENGINE_OPTION_PATH_RESOURCES:
  1209. CARLA_SAFE_ASSERT_RETURN(valueStr != nullptr && valueStr[0] != '\0',);
  1210. if (pData->options.resourceDir != nullptr)
  1211. delete[] pData->options.resourceDir;
  1212. pData->options.resourceDir = carla_strdup_safe(valueStr);
  1213. break;
  1214. case ENGINE_OPTION_PREVENT_BAD_BEHAVIOUR: {
  1215. CARLA_SAFE_ASSERT_RETURN(pData->options.binaryDir != nullptr && pData->options.binaryDir[0] != '\0',);
  1216. #ifdef CARLA_OS_LINUX
  1217. const ScopedEngineEnvironmentLocker _seel(this);
  1218. if (value != 0)
  1219. {
  1220. CarlaString interposerPath(CarlaString(pData->options.binaryDir) + "/libcarla_interposer-safe.so");
  1221. ::setenv("LD_PRELOAD", interposerPath.buffer(), 1);
  1222. }
  1223. else
  1224. {
  1225. ::unsetenv("LD_PRELOAD");
  1226. }
  1227. #endif
  1228. } break;
  1229. case ENGINE_OPTION_FRONTEND_WIN_ID: {
  1230. CARLA_SAFE_ASSERT_RETURN(valueStr != nullptr && valueStr[0] != '\0',);
  1231. const long long winId(std::strtoll(valueStr, nullptr, 16));
  1232. CARLA_SAFE_ASSERT_RETURN(winId >= 0,);
  1233. pData->options.frontendWinId = static_cast<uintptr_t>(winId);
  1234. } break;
  1235. #if !defined(BUILD_BRIDGE_ALTERNATIVE_ARCH) && !defined(CARLA_OS_WIN)
  1236. case ENGINE_OPTION_WINE_EXECUTABLE:
  1237. CARLA_SAFE_ASSERT_RETURN(valueStr != nullptr && valueStr[0] != '\0',);
  1238. if (pData->options.wine.executable != nullptr)
  1239. delete[] pData->options.wine.executable;
  1240. pData->options.wine.executable = carla_strdup_safe(valueStr);
  1241. break;
  1242. case ENGINE_OPTION_WINE_AUTO_PREFIX:
  1243. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  1244. pData->options.wine.autoPrefix = (value != 0);
  1245. break;
  1246. case ENGINE_OPTION_WINE_FALLBACK_PREFIX:
  1247. CARLA_SAFE_ASSERT_RETURN(valueStr != nullptr && valueStr[0] != '\0',);
  1248. if (pData->options.wine.fallbackPrefix != nullptr)
  1249. delete[] pData->options.wine.fallbackPrefix;
  1250. pData->options.wine.fallbackPrefix = carla_strdup_safe(valueStr);
  1251. break;
  1252. case ENGINE_OPTION_WINE_RT_PRIO_ENABLED:
  1253. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  1254. pData->options.wine.rtPrio = (value != 0);
  1255. break;
  1256. case ENGINE_OPTION_WINE_BASE_RT_PRIO:
  1257. CARLA_SAFE_ASSERT_RETURN(value >= 1 && value <= 89,);
  1258. pData->options.wine.baseRtPrio = value;
  1259. break;
  1260. case ENGINE_OPTION_WINE_SERVER_RT_PRIO:
  1261. CARLA_SAFE_ASSERT_RETURN(value >= 1 && value <= 99,);
  1262. pData->options.wine.serverRtPrio = value;
  1263. break;
  1264. #endif
  1265. case ENGINE_OPTION_DEBUG_CONSOLE_OUTPUT:
  1266. break;
  1267. }
  1268. }
  1269. #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  1270. // -----------------------------------------------------------------------
  1271. // OSC Stuff
  1272. bool CarlaEngine::isOscControlRegistered() const noexcept
  1273. {
  1274. return pData->osc.isControlRegistered();
  1275. }
  1276. void CarlaEngine::idleOsc() const noexcept
  1277. {
  1278. pData->osc.idle();
  1279. }
  1280. const char* CarlaEngine::getOscServerPathTCP() const noexcept
  1281. {
  1282. return pData->osc.getServerPathTCP();
  1283. }
  1284. const char* CarlaEngine::getOscServerPathUDP() const noexcept
  1285. {
  1286. return pData->osc.getServerPathUDP();
  1287. }
  1288. #endif
  1289. // -----------------------------------------------------------------------
  1290. // Helper functions
  1291. EngineEvent* CarlaEngine::getInternalEventBuffer(const bool isInput) const noexcept
  1292. {
  1293. return isInput ? pData->events.in : pData->events.out;
  1294. }
  1295. // -----------------------------------------------------------------------
  1296. // Internal stuff
  1297. void CarlaEngine::bufferSizeChanged(const uint32_t newBufferSize)
  1298. {
  1299. carla_debug("CarlaEngine::bufferSizeChanged(%i)", newBufferSize);
  1300. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1301. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK ||
  1302. pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  1303. {
  1304. pData->graph.setBufferSize(newBufferSize);
  1305. }
  1306. #endif
  1307. pData->time.updateAudioValues(newBufferSize, pData->sampleRate);
  1308. for (uint i=0; i < pData->curPluginCount; ++i)
  1309. {
  1310. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1311. if (plugin != nullptr && plugin->isEnabled())
  1312. plugin->bufferSizeChanged(newBufferSize);
  1313. }
  1314. callback(ENGINE_CALLBACK_BUFFER_SIZE_CHANGED, 0, static_cast<int>(newBufferSize), 0, 0.0f, nullptr);
  1315. }
  1316. void CarlaEngine::sampleRateChanged(const double newSampleRate)
  1317. {
  1318. carla_debug("CarlaEngine::sampleRateChanged(%g)", newSampleRate);
  1319. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1320. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK ||
  1321. pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  1322. {
  1323. pData->graph.setSampleRate(newSampleRate);
  1324. }
  1325. #endif
  1326. pData->time.updateAudioValues(pData->bufferSize, newSampleRate);
  1327. for (uint i=0; i < pData->curPluginCount; ++i)
  1328. {
  1329. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1330. if (plugin != nullptr && plugin->isEnabled())
  1331. plugin->sampleRateChanged(newSampleRate);
  1332. }
  1333. callback(ENGINE_CALLBACK_SAMPLE_RATE_CHANGED, 0, 0, 0, static_cast<float>(newSampleRate), nullptr);
  1334. }
  1335. void CarlaEngine::offlineModeChanged(const bool isOfflineNow)
  1336. {
  1337. carla_debug("CarlaEngine::offlineModeChanged(%s)", bool2str(isOfflineNow));
  1338. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1339. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK ||
  1340. pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  1341. {
  1342. pData->graph.setOffline(isOfflineNow);
  1343. }
  1344. #endif
  1345. for (uint i=0; i < pData->curPluginCount; ++i)
  1346. {
  1347. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1348. if (plugin != nullptr && plugin->isEnabled())
  1349. plugin->offlineModeChanged(isOfflineNow);
  1350. }
  1351. }
  1352. void CarlaEngine::setPluginPeaks(const uint pluginId, float const inPeaks[2], float const outPeaks[2]) noexcept
  1353. {
  1354. EnginePluginData& pluginData(pData->plugins[pluginId]);
  1355. pluginData.insPeak[0] = inPeaks[0];
  1356. pluginData.insPeak[1] = inPeaks[1];
  1357. pluginData.outsPeak[0] = outPeaks[0];
  1358. pluginData.outsPeak[1] = outPeaks[1];
  1359. }
  1360. void CarlaEngine::saveProjectInternal(water::MemoryOutputStream& outStream) const
  1361. {
  1362. // send initial prepareForSave first, giving time for bridges to act
  1363. for (uint i=0; i < pData->curPluginCount; ++i)
  1364. {
  1365. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1366. if (plugin != nullptr && plugin->isEnabled())
  1367. {
  1368. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1369. // deactivate bridge client-side ping check, since some plugins block during save
  1370. if (plugin->getHints() & PLUGIN_IS_BRIDGE)
  1371. plugin->setCustomData(CUSTOM_DATA_TYPE_STRING, "__CarlaPingOnOff__", "false", false);
  1372. #endif
  1373. plugin->prepareForSave();
  1374. }
  1375. }
  1376. outStream << "<?xml version='1.0' encoding='UTF-8'?>\n";
  1377. outStream << "<!DOCTYPE CARLA-PROJECT>\n";
  1378. outStream << "<CARLA-PROJECT VERSION='2.0'>\n";
  1379. const bool isPlugin(getType() == kEngineTypePlugin);
  1380. const EngineOptions& options(pData->options);
  1381. {
  1382. MemoryOutputStream outSettings(1024);
  1383. outSettings << " <EngineSettings>\n";
  1384. outSettings << " <ForceStereo>" << bool2str(options.forceStereo) << "</ForceStereo>\n";
  1385. outSettings << " <PreferPluginBridges>" << bool2str(options.preferPluginBridges) << "</PreferPluginBridges>\n";
  1386. outSettings << " <PreferUiBridges>" << bool2str(options.preferUiBridges) << "</PreferUiBridges>\n";
  1387. outSettings << " <UIsAlwaysOnTop>" << bool2str(options.uisAlwaysOnTop) << "</UIsAlwaysOnTop>\n";
  1388. outSettings << " <MaxParameters>" << String(options.maxParameters) << "</MaxParameters>\n";
  1389. outSettings << " <UIBridgesTimeout>" << String(options.uiBridgesTimeout) << "</UIBridgesTimeout>\n";
  1390. if (isPlugin)
  1391. {
  1392. outSettings << " <LADSPA_PATH>" << xmlSafeString(options.pathLADSPA, true) << "</LADSPA_PATH>\n";
  1393. outSettings << " <DSSI_PATH>" << xmlSafeString(options.pathDSSI, true) << "</DSSI_PATH>\n";
  1394. outSettings << " <LV2_PATH>" << xmlSafeString(options.pathLV2, true) << "</LV2_PATH>\n";
  1395. outSettings << " <VST2_PATH>" << xmlSafeString(options.pathVST2, true) << "</VST2_PATH>\n";
  1396. outSettings << " <SF2_PATH>" << xmlSafeString(options.pathSF2, true) << "</SF2_PATH>\n";
  1397. outSettings << " <SFZ_PATH>" << xmlSafeString(options.pathSFZ, true) << "</SFZ_PATH>\n";
  1398. }
  1399. outSettings << " </EngineSettings>\n";
  1400. outStream << outSettings;
  1401. }
  1402. if (pData->timeInfo.bbt.valid && ! isPlugin)
  1403. {
  1404. MemoryOutputStream outTransport(128);
  1405. outTransport << "\n <Transport>\n";
  1406. // outTransport << " <BeatsPerBar>" << pData->timeInfo.bbt.beatsPerBar << "</BeatsPerBar>\n";
  1407. outTransport << " <BeatsPerMinute>" << pData->timeInfo.bbt.beatsPerMinute << "</BeatsPerMinute>\n";
  1408. outTransport << " </Transport>\n";
  1409. outStream << outTransport;
  1410. }
  1411. char strBuf[STR_MAX+1];
  1412. for (uint i=0; i < pData->curPluginCount; ++i)
  1413. {
  1414. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1415. if (plugin != nullptr && plugin->isEnabled())
  1416. {
  1417. MemoryOutputStream outPlugin(4096), streamPlugin;
  1418. plugin->getStateSave(false).dumpToMemoryStream(streamPlugin);
  1419. outPlugin << "\n";
  1420. strBuf[0] = '\0';
  1421. plugin->getRealName(strBuf);
  1422. if (strBuf[0] != '\0')
  1423. outPlugin << " <!-- " << xmlSafeString(strBuf, true) << " -->\n";
  1424. outPlugin << " <Plugin>\n";
  1425. outPlugin << streamPlugin;
  1426. outPlugin << " </Plugin>\n";
  1427. outStream << outPlugin;
  1428. }
  1429. }
  1430. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1431. // tell bridges we're done saving
  1432. for (uint i=0; i < pData->curPluginCount; ++i)
  1433. {
  1434. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1435. if (plugin != nullptr && plugin->isEnabled() && (plugin->getHints() & PLUGIN_IS_BRIDGE) != 0)
  1436. plugin->setCustomData(CUSTOM_DATA_TYPE_STRING, "__CarlaPingOnOff__", "true", false);
  1437. }
  1438. // save internal connections
  1439. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  1440. {
  1441. if (const char* const* const patchbayConns = getPatchbayConnections(false))
  1442. {
  1443. MemoryOutputStream outPatchbay(2048);
  1444. outPatchbay << "\n <Patchbay>\n";
  1445. for (int i=0; patchbayConns[i] != nullptr && patchbayConns[i+1] != nullptr; ++i, ++i )
  1446. {
  1447. const char* const connSource(patchbayConns[i]);
  1448. const char* const connTarget(patchbayConns[i+1]);
  1449. CARLA_SAFE_ASSERT_CONTINUE(connSource != nullptr && connSource[0] != '\0');
  1450. CARLA_SAFE_ASSERT_CONTINUE(connTarget != nullptr && connTarget[0] != '\0');
  1451. outPatchbay << " <Connection>\n";
  1452. outPatchbay << " <Source>" << xmlSafeString(connSource, true) << "</Source>\n";
  1453. outPatchbay << " <Target>" << xmlSafeString(connTarget, true) << "</Target>\n";
  1454. outPatchbay << " </Connection>\n";
  1455. }
  1456. outPatchbay << " </Patchbay>\n";
  1457. outStream << outPatchbay;
  1458. }
  1459. }
  1460. // if we're running inside some session-manager (and using JACK), let them handle the connections
  1461. bool saveExternalConnections;
  1462. /**/ if (isPlugin)
  1463. saveExternalConnections = false;
  1464. else if (std::strcmp(getCurrentDriverName(), "JACK") != 0)
  1465. saveExternalConnections = true;
  1466. else if (std::getenv("CARLA_DONT_MANAGE_CONNECTIONS") != nullptr)
  1467. saveExternalConnections = false;
  1468. else if (std::getenv("LADISH_APP_NAME") != nullptr)
  1469. saveExternalConnections = false;
  1470. else if (std::getenv("NSM_URL") != nullptr)
  1471. saveExternalConnections = false;
  1472. else
  1473. saveExternalConnections = true;
  1474. if (saveExternalConnections)
  1475. {
  1476. if (const char* const* const patchbayConns = getPatchbayConnections(true))
  1477. {
  1478. MemoryOutputStream outPatchbay(2048);
  1479. outPatchbay << "\n <ExternalPatchbay>\n";
  1480. for (int i=0; patchbayConns[i] != nullptr && patchbayConns[i+1] != nullptr; ++i, ++i )
  1481. {
  1482. const char* const connSource(patchbayConns[i]);
  1483. const char* const connTarget(patchbayConns[i+1]);
  1484. CARLA_SAFE_ASSERT_CONTINUE(connSource != nullptr && connSource[0] != '\0');
  1485. CARLA_SAFE_ASSERT_CONTINUE(connTarget != nullptr && connTarget[0] != '\0');
  1486. outPatchbay << " <Connection>\n";
  1487. outPatchbay << " <Source>" << xmlSafeString(connSource, true) << "</Source>\n";
  1488. outPatchbay << " <Target>" << xmlSafeString(connTarget, true) << "</Target>\n";
  1489. outPatchbay << " </Connection>\n";
  1490. }
  1491. outPatchbay << " </ExternalPatchbay>\n";
  1492. outStream << outPatchbay;
  1493. }
  1494. }
  1495. #endif
  1496. outStream << "</CARLA-PROJECT>\n";
  1497. }
  1498. static String findBinaryInCustomPath(const char* const searchPath, const char* const binary)
  1499. {
  1500. const StringArray searchPaths(StringArray::fromTokens(searchPath, CARLA_OS_SPLIT_STR, ""));
  1501. // try direct filename first
  1502. String jbinary(binary);
  1503. // adjust for current platform
  1504. #ifdef CARLA_OS_WIN
  1505. if (jbinary[0] == '/')
  1506. jbinary = "C:" + jbinary.replaceCharacter('/', '\\');
  1507. #else
  1508. if (jbinary[1] == ':' && (jbinary[2] == '\\' || jbinary[2] == '/'))
  1509. jbinary = jbinary.substring(2).replaceCharacter('\\', '/');
  1510. #endif
  1511. String filename = File(jbinary).getFileName();
  1512. int searchFlags = File::findFiles|File::ignoreHiddenFiles;
  1513. #ifdef CARLA_OS_MAC
  1514. if (filename.endsWithIgnoreCase(".vst"))
  1515. searchFlags |= File::findDirectories;
  1516. #endif
  1517. Array<File> results;
  1518. for (const String *it=searchPaths.begin(), *end=searchPaths.end(); it != end; ++it)
  1519. {
  1520. const File path(*it);
  1521. results.clear();
  1522. path.findChildFiles(results, searchFlags, true, filename);
  1523. if (results.size() > 0)
  1524. return results.getFirst().getFullPathName();
  1525. }
  1526. // try changing extension
  1527. #if defined(CARLA_OS_MAC)
  1528. if (filename.endsWithIgnoreCase(".dll") || filename.endsWithIgnoreCase(".so"))
  1529. filename = File(jbinary).getFileNameWithoutExtension() + ".dylib";
  1530. #elif defined(CARLA_OS_WIN)
  1531. if (filename.endsWithIgnoreCase(".dylib") || filename.endsWithIgnoreCase(".so"))
  1532. filename = File(jbinary).getFileNameWithoutExtension() + ".dll";
  1533. #else
  1534. if (filename.endsWithIgnoreCase(".dll") || filename.endsWithIgnoreCase(".dylib"))
  1535. filename = File(jbinary).getFileNameWithoutExtension() + ".so";
  1536. #endif
  1537. else
  1538. return String();
  1539. for (const String *it=searchPaths.begin(), *end=searchPaths.end(); it != end; ++it)
  1540. {
  1541. const File path(*it);
  1542. results.clear();
  1543. path.findChildFiles(results, searchFlags, true, filename);
  1544. if (results.size() > 0)
  1545. return results.getFirst().getFullPathName();
  1546. }
  1547. return String();
  1548. }
  1549. bool CarlaEngine::loadProjectInternal(water::XmlDocument& xmlDoc)
  1550. {
  1551. ScopedPointer<XmlElement> xmlElement(xmlDoc.getDocumentElement(true));
  1552. CARLA_SAFE_ASSERT_RETURN_ERR(xmlElement != nullptr, "Failed to parse project file");
  1553. const String& xmlType(xmlElement->getTagName());
  1554. const bool isPreset(xmlType.equalsIgnoreCase("carla-preset"));
  1555. if (! (xmlType.equalsIgnoreCase("carla-project") || isPreset))
  1556. {
  1557. callback(ENGINE_CALLBACK_PROJECT_LOAD_FINISHED, 0, 0, 0, 0.0f, nullptr);
  1558. setLastError("Not a valid Carla project or preset file");
  1559. return false;
  1560. }
  1561. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1562. const ScopedValueSetter<bool> _svs(pData->loadingProject, true, false);
  1563. #endif
  1564. // completely load file
  1565. xmlElement = xmlDoc.getDocumentElement(false);
  1566. CARLA_SAFE_ASSERT_RETURN_ERR(xmlElement != nullptr, "Failed to completely parse project file");
  1567. if (pData->aboutToClose)
  1568. return true;
  1569. const bool isPlugin(getType() == kEngineTypePlugin);
  1570. // load engine settings first of all
  1571. if (XmlElement* const elem = isPreset ? nullptr : xmlElement->getChildByName("EngineSettings"))
  1572. {
  1573. for (XmlElement* settElem = elem->getFirstChildElement(); settElem != nullptr; settElem = settElem->getNextElement())
  1574. {
  1575. const String& tag(settElem->getTagName());
  1576. const String text(settElem->getAllSubText().trim());
  1577. /** some settings might be incorrect or require extra work,
  1578. so we call setOption rather than modifying them direly */
  1579. int option = -1;
  1580. int value = 0;
  1581. const char* valueStr = nullptr;
  1582. /**/ if (tag == "ForceStereo")
  1583. {
  1584. option = ENGINE_OPTION_FORCE_STEREO;
  1585. value = text == "true" ? 1 : 0;
  1586. }
  1587. else if (tag == "PreferPluginBridges")
  1588. {
  1589. option = ENGINE_OPTION_PREFER_PLUGIN_BRIDGES;
  1590. value = text == "true" ? 1 : 0;
  1591. }
  1592. else if (tag == "PreferUiBridges")
  1593. {
  1594. option = ENGINE_OPTION_PREFER_UI_BRIDGES;
  1595. value = text == "true" ? 1 : 0;
  1596. }
  1597. else if (tag == "UIsAlwaysOnTop")
  1598. {
  1599. option = ENGINE_OPTION_UIS_ALWAYS_ON_TOP;
  1600. value = text == "true" ? 1 : 0;
  1601. }
  1602. else if (tag == "MaxParameters")
  1603. {
  1604. option = ENGINE_OPTION_MAX_PARAMETERS;
  1605. value = text.getIntValue();
  1606. }
  1607. else if (tag == "UIBridgesTimeout")
  1608. {
  1609. option = ENGINE_OPTION_UI_BRIDGES_TIMEOUT;
  1610. value = text.getIntValue();
  1611. }
  1612. else if (isPlugin)
  1613. {
  1614. /**/ if (tag == "LADSPA_PATH")
  1615. {
  1616. option = ENGINE_OPTION_PLUGIN_PATH;
  1617. value = PLUGIN_LADSPA;
  1618. valueStr = text.toRawUTF8();
  1619. }
  1620. else if (tag == "DSSI_PATH")
  1621. {
  1622. option = ENGINE_OPTION_PLUGIN_PATH;
  1623. value = PLUGIN_DSSI;
  1624. valueStr = text.toRawUTF8();
  1625. }
  1626. else if (tag == "LV2_PATH")
  1627. {
  1628. option = ENGINE_OPTION_PLUGIN_PATH;
  1629. value = PLUGIN_LV2;
  1630. valueStr = text.toRawUTF8();
  1631. }
  1632. else if (tag == "VST2_PATH")
  1633. {
  1634. option = ENGINE_OPTION_PLUGIN_PATH;
  1635. value = PLUGIN_VST2;
  1636. valueStr = text.toRawUTF8();
  1637. }
  1638. else if (tag == "SF2_PATH")
  1639. {
  1640. option = ENGINE_OPTION_PLUGIN_PATH;
  1641. value = PLUGIN_SF2;
  1642. valueStr = text.toRawUTF8();
  1643. }
  1644. else if (tag == "SFZ_PATH")
  1645. {
  1646. option = ENGINE_OPTION_PLUGIN_PATH;
  1647. value = PLUGIN_SFZ;
  1648. valueStr = text.toRawUTF8();
  1649. }
  1650. }
  1651. CARLA_SAFE_ASSERT_CONTINUE(option != -1);
  1652. setOption(static_cast<EngineOption>(option), value, valueStr);
  1653. }
  1654. }
  1655. if (pData->aboutToClose)
  1656. return true;
  1657. // now setup transport
  1658. if (XmlElement* const elem = isPreset || isPlugin ? nullptr : xmlElement->getChildByName("Transport"))
  1659. {
  1660. if (XmlElement* const bpmElem = elem->getChildByName("BeatsPerMinute"))
  1661. {
  1662. const String bpmText(bpmElem->getAllSubText().trim());
  1663. const double bpm = bpmText.getDoubleValue();
  1664. // some sane limits
  1665. if (bpm >= 20.0 && bpm < 400.0)
  1666. pData->time.setBPM(bpm);
  1667. }
  1668. }
  1669. if (pData->aboutToClose)
  1670. return true;
  1671. // and we handle plugins
  1672. for (XmlElement* elem = xmlElement->getFirstChildElement(); elem != nullptr; elem = elem->getNextElement())
  1673. {
  1674. const String& tagName(elem->getTagName());
  1675. if (isPreset || tagName == "Plugin")
  1676. {
  1677. CarlaStateSave stateSave;
  1678. stateSave.fillFromXmlElement(isPreset ? xmlElement.get() : elem);
  1679. callback(ENGINE_CALLBACK_IDLE, 0, 0, 0, 0.0f, nullptr);
  1680. if (pData->aboutToClose)
  1681. return true;
  1682. CARLA_SAFE_ASSERT_CONTINUE(stateSave.type != nullptr);
  1683. #ifndef BUILD_BRIDGE
  1684. // compatibility code to load projects with GIG files
  1685. // FIXME Remove on 2.1 release
  1686. if (std::strcmp(stateSave.type, "GIG") == 0)
  1687. {
  1688. if (addPlugin(PLUGIN_LV2, "", stateSave.name, "http://linuxsampler.org/plugins/linuxsampler", 0, nullptr))
  1689. {
  1690. const uint pluginId = pData->curPluginCount;
  1691. if (CarlaPlugin* const plugin = pData->plugins[pluginId].plugin)
  1692. {
  1693. callback(ENGINE_CALLBACK_IDLE, 0, 0, 0, 0.0f, nullptr);
  1694. if (pData->aboutToClose)
  1695. return true;
  1696. String lsState;
  1697. lsState << "0.35\n";
  1698. lsState << "18 0 Chromatic\n";
  1699. lsState << "18 1 Drum Kits\n";
  1700. lsState << "20 0\n";
  1701. lsState << "0 1 " << stateSave.binary << "\n";
  1702. lsState << "0 0 0 0 1 0 GIG\n";
  1703. plugin->setCustomData(LV2_ATOM__String, "http://linuxsampler.org/schema#state-string", lsState.toRawUTF8(), true);
  1704. plugin->restoreLV2State();
  1705. plugin->setDryWet(stateSave.dryWet, true, true);
  1706. plugin->setVolume(stateSave.volume, true, true);
  1707. plugin->setBalanceLeft(stateSave.balanceLeft, true, true);
  1708. plugin->setBalanceRight(stateSave.balanceRight, true, true);
  1709. plugin->setPanning(stateSave.panning, true, true);
  1710. plugin->setCtrlChannel(stateSave.ctrlChannel, true, true);
  1711. plugin->setActive(stateSave.active, true, true);
  1712. ++pData->curPluginCount;
  1713. plugin->setEnabled(true);
  1714. callback(ENGINE_CALLBACK_PLUGIN_ADDED, pluginId, 0, 0, 0.0f, plugin->getName());
  1715. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  1716. pData->graph.addPlugin(plugin);
  1717. }
  1718. else
  1719. {
  1720. carla_stderr2("Failed to get new plugin, state will not be restored correctly\n");
  1721. }
  1722. }
  1723. else
  1724. {
  1725. carla_stderr2("Failed to load a linuxsampler LV2 plugin, GIG file won't be loaded");
  1726. }
  1727. continue;
  1728. }
  1729. #endif
  1730. const void* extraStuff = nullptr;
  1731. static const char kTrue[] = "true";
  1732. const PluginType ptype(getPluginTypeFromString(stateSave.type));
  1733. switch (ptype)
  1734. {
  1735. case PLUGIN_SF2:
  1736. if (CarlaString(stateSave.label).endsWith(" (16 outs)"))
  1737. extraStuff = kTrue;
  1738. // fall through
  1739. case PLUGIN_LADSPA:
  1740. case PLUGIN_DSSI:
  1741. case PLUGIN_VST2:
  1742. case PLUGIN_SFZ:
  1743. if (stateSave.binary != nullptr && stateSave.binary[0] != '\0' &&
  1744. ! (File::isAbsolutePath(stateSave.binary) && File(stateSave.binary).exists()))
  1745. {
  1746. const char* searchPath;
  1747. switch (ptype)
  1748. {
  1749. case PLUGIN_LADSPA: searchPath = pData->options.pathLADSPA; break;
  1750. case PLUGIN_DSSI: searchPath = pData->options.pathDSSI; break;
  1751. case PLUGIN_VST2: searchPath = pData->options.pathVST2; break;
  1752. case PLUGIN_SF2: searchPath = pData->options.pathSF2; break;
  1753. case PLUGIN_SFZ: searchPath = pData->options.pathSFZ; break;
  1754. default: searchPath = nullptr; break;
  1755. }
  1756. if (searchPath != nullptr && searchPath[0] != '\0')
  1757. {
  1758. carla_stderr("Plugin binary '%s' doesn't exist on this filesystem, let's look for it...",
  1759. stateSave.binary);
  1760. String result = findBinaryInCustomPath(searchPath, stateSave.binary);
  1761. if (result.isEmpty())
  1762. {
  1763. switch (ptype)
  1764. {
  1765. case PLUGIN_LADSPA: searchPath = std::getenv("LADSPA_PATH"); break;
  1766. case PLUGIN_DSSI: searchPath = std::getenv("DSSI_PATH"); break;
  1767. case PLUGIN_VST2: searchPath = std::getenv("VST_PATH"); break;
  1768. case PLUGIN_SF2: searchPath = std::getenv("SF2_PATH"); break;
  1769. case PLUGIN_SFZ: searchPath = std::getenv("SFZ_PATH"); break;
  1770. default: searchPath = nullptr; break;
  1771. }
  1772. if (searchPath != nullptr && searchPath[0] != '\0')
  1773. result = findBinaryInCustomPath(searchPath, stateSave.binary);
  1774. }
  1775. if (result.isNotEmpty())
  1776. {
  1777. delete[] stateSave.binary;
  1778. stateSave.binary = carla_strdup(result.toRawUTF8());
  1779. carla_stderr("Found it! :)");
  1780. }
  1781. else
  1782. {
  1783. carla_stderr("Damn, we failed... :(");
  1784. }
  1785. }
  1786. }
  1787. break;
  1788. default:
  1789. break;
  1790. }
  1791. if (addPlugin(getBinaryTypeFromFile(stateSave.binary), ptype, stateSave.binary,
  1792. stateSave.name, stateSave.label, stateSave.uniqueId, extraStuff, stateSave.options))
  1793. {
  1794. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1795. const uint pluginId = pData->curPluginCount;
  1796. #else
  1797. const uint pluginId = 0;
  1798. #endif
  1799. if (CarlaPlugin* const plugin = pData->plugins[pluginId].plugin)
  1800. {
  1801. callback(ENGINE_CALLBACK_IDLE, 0, 0, 0, 0.0f, nullptr);
  1802. if (pData->aboutToClose)
  1803. return true;
  1804. // deactivate bridge client-side ping check, since some plugins block during load
  1805. if ((plugin->getHints() & PLUGIN_IS_BRIDGE) != 0 && ! isPreset)
  1806. plugin->setCustomData(CUSTOM_DATA_TYPE_STRING, "__CarlaPingOnOff__", "false", false);
  1807. plugin->loadStateSave(stateSave);
  1808. /* NOTE: The following code is the same as the end of addPlugin().
  1809. * When project is loading we do not enable the plugin right away,
  1810. * as we want to load state first.
  1811. */
  1812. #ifdef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1813. plugin->setActive(true, true, false);
  1814. #else
  1815. ++pData->curPluginCount;
  1816. #endif
  1817. plugin->setEnabled(true);
  1818. callback(ENGINE_CALLBACK_PLUGIN_ADDED, pluginId, 0, 0, 0.0f, plugin->getName());
  1819. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1820. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  1821. pData->graph.addPlugin(plugin);
  1822. #endif
  1823. }
  1824. else
  1825. {
  1826. carla_stderr2("Failed to get new plugin, state will not be restored correctly\n");
  1827. }
  1828. }
  1829. else
  1830. {
  1831. carla_stderr2("Failed to load a plugin, error was:\n%s", getLastError());
  1832. }
  1833. }
  1834. if (isPreset)
  1835. return true;
  1836. }
  1837. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1838. // tell bridges we're done loading
  1839. for (uint i=0; i < pData->curPluginCount; ++i)
  1840. {
  1841. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1842. if (plugin != nullptr && plugin->isEnabled() && (plugin->getHints() & PLUGIN_IS_BRIDGE) != 0)
  1843. plugin->setCustomData(CUSTOM_DATA_TYPE_STRING, "__CarlaPingOnOff__", "true", false);
  1844. }
  1845. callback(ENGINE_CALLBACK_IDLE, 0, 0, 0, 0.0f, nullptr);
  1846. if (pData->aboutToClose)
  1847. return true;
  1848. // and now we handle connections (internal)
  1849. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  1850. {
  1851. const bool isUsingExternal(pData->graph.isUsingExternal());
  1852. if (XmlElement* const elem = xmlElement->getChildByName("Patchbay"))
  1853. {
  1854. CarlaString sourcePort, targetPort;
  1855. for (XmlElement* patchElem = elem->getFirstChildElement(); patchElem != nullptr; patchElem = patchElem->getNextElement())
  1856. {
  1857. const String& patchTag(patchElem->getTagName());
  1858. if (patchTag != "Connection")
  1859. continue;
  1860. sourcePort.clear();
  1861. targetPort.clear();
  1862. for (XmlElement* connElem = patchElem->getFirstChildElement(); connElem != nullptr; connElem = connElem->getNextElement())
  1863. {
  1864. const String& tag(connElem->getTagName());
  1865. const String text(connElem->getAllSubText().trim());
  1866. /**/ if (tag == "Source")
  1867. sourcePort = xmlSafeString(text, false).toRawUTF8();
  1868. else if (tag == "Target")
  1869. targetPort = xmlSafeString(text, false).toRawUTF8();
  1870. }
  1871. if (sourcePort.isNotEmpty() && targetPort.isNotEmpty())
  1872. restorePatchbayConnection(false, sourcePort, targetPort, !isUsingExternal);
  1873. }
  1874. }
  1875. callback(ENGINE_CALLBACK_IDLE, 0, 0, 0, 0.0f, nullptr);
  1876. if (pData->aboutToClose)
  1877. return true;
  1878. }
  1879. // if we're running inside some session-manager (and using JACK), let them handle the external connections
  1880. bool loadExternalConnections;
  1881. /**/ if (isPlugin)
  1882. loadExternalConnections = false;
  1883. else if (std::strcmp(getCurrentDriverName(), "JACK") != 0)
  1884. loadExternalConnections = true;
  1885. else if (std::getenv("CARLA_DONT_MANAGE_CONNECTIONS") != nullptr)
  1886. loadExternalConnections = false;
  1887. else if (std::getenv("LADISH_APP_NAME") != nullptr)
  1888. loadExternalConnections = false;
  1889. else if (std::getenv("NSM_URL") != nullptr)
  1890. loadExternalConnections = false;
  1891. else
  1892. loadExternalConnections = true;
  1893. // plus external connections too
  1894. if (loadExternalConnections)
  1895. {
  1896. const bool isUsingExternal(pData->options.processMode != ENGINE_PROCESS_MODE_PATCHBAY ||
  1897. pData->graph.isUsingExternal());
  1898. for (XmlElement* elem = xmlElement->getFirstChildElement(); elem != nullptr; elem = elem->getNextElement())
  1899. {
  1900. const String& tagName(elem->getTagName());
  1901. // check if we want to load patchbay-mode connections into an external (multi-client) graph
  1902. if (tagName == "Patchbay")
  1903. {
  1904. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  1905. continue;
  1906. }
  1907. // or load external patchbay connections
  1908. else if (tagName != "ExternalPatchbay")
  1909. {
  1910. continue;
  1911. }
  1912. CarlaString sourcePort, targetPort;
  1913. for (XmlElement* patchElem = elem->getFirstChildElement(); patchElem != nullptr; patchElem = patchElem->getNextElement())
  1914. {
  1915. const String& patchTag(patchElem->getTagName());
  1916. if (patchTag != "Connection")
  1917. continue;
  1918. sourcePort.clear();
  1919. targetPort.clear();
  1920. for (XmlElement* connElem = patchElem->getFirstChildElement(); connElem != nullptr; connElem = connElem->getNextElement())
  1921. {
  1922. const String& tag(connElem->getTagName());
  1923. const String text(connElem->getAllSubText().trim());
  1924. /**/ if (tag == "Source")
  1925. sourcePort = xmlSafeString(text, false).toRawUTF8();
  1926. else if (tag == "Target")
  1927. targetPort = xmlSafeString(text, false).toRawUTF8();
  1928. }
  1929. if (sourcePort.isNotEmpty() && targetPort.isNotEmpty())
  1930. restorePatchbayConnection(true, sourcePort, targetPort, isUsingExternal);
  1931. }
  1932. break;
  1933. }
  1934. }
  1935. #endif
  1936. callback(ENGINE_CALLBACK_PROJECT_LOAD_FINISHED, 0, 0, 0, 0.0f, nullptr);
  1937. return true;
  1938. }
  1939. // -----------------------------------------------------------------------
  1940. CARLA_BACKEND_END_NAMESPACE