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.

2387 lines
82KB

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