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.

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