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.

2457 lines
82KB

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