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.

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