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.

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