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.

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