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.

2393 lines
81KB

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