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.

2375 lines
82KB

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