Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2375 lines
82KB

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