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.

3075 lines
102KB

  1. /*
  2. * Carla Plugin Host
  3. * Copyright (C) 2011-2020 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 "CarlaEngineClient.hpp"
  24. #include "CarlaEngineInit.hpp"
  25. #include "CarlaEngineInternal.hpp"
  26. #include "CarlaPlugin.hpp"
  27. #include "CarlaBackendUtils.hpp"
  28. #include "CarlaBinaryUtils.hpp"
  29. #include "CarlaEngineUtils.hpp"
  30. #include "CarlaMathUtils.hpp"
  31. #include "CarlaPipeUtils.hpp"
  32. #include "CarlaProcessUtils.hpp"
  33. #include "CarlaScopeUtils.hpp"
  34. #include "CarlaStateUtils.hpp"
  35. #include "CarlaMIDI.h"
  36. #include "jackbridge/JackBridge.hpp"
  37. #include "water/files/File.h"
  38. #include "water/streams/MemoryOutputStream.h"
  39. #include "water/xml/XmlDocument.h"
  40. #include "water/xml/XmlElement.h"
  41. // FIXME Remove on 2.1 release
  42. #include "lv2/atom.h"
  43. using water::Array;
  44. using water::CharPointer_UTF8;
  45. using water::File;
  46. using water::MemoryOutputStream;
  47. using water::String;
  48. using water::StringArray;
  49. using water::XmlDocument;
  50. using water::XmlElement;
  51. CARLA_BACKEND_START_NAMESPACE
  52. // -----------------------------------------------------------------------
  53. // Carla Engine
  54. CarlaEngine::CarlaEngine()
  55. : pData(new ProtectedData(this))
  56. {
  57. carla_debug("CarlaEngine::CarlaEngine()");
  58. }
  59. CarlaEngine::~CarlaEngine()
  60. {
  61. carla_debug("CarlaEngine::~CarlaEngine()");
  62. delete pData;
  63. }
  64. // -----------------------------------------------------------------------
  65. // Static calls
  66. uint CarlaEngine::getDriverCount()
  67. {
  68. carla_debug("CarlaEngine::getDriverCount()");
  69. using namespace EngineInit;
  70. uint count = 0;
  71. if (jackbridge_is_ok())
  72. count += 1;
  73. #ifndef BUILD_BRIDGE
  74. # ifdef USING_JUCE_AUDIO_DEVICES
  75. count += getJuceApiCount();
  76. # else
  77. count += getRtAudioApiCount();
  78. # endif
  79. #endif
  80. return count;
  81. }
  82. const char* CarlaEngine::getDriverName(const uint index2)
  83. {
  84. carla_debug("CarlaEngine::getDriverName(%i)", index2);
  85. using namespace EngineInit;
  86. uint index = index2;
  87. if (jackbridge_is_ok() && index-- == 0)
  88. return "JACK";
  89. #ifndef BUILD_BRIDGE
  90. # ifdef USING_JUCE_AUDIO_DEVICES
  91. if (const uint count = getJuceApiCount())
  92. {
  93. if (index < count)
  94. return getJuceApiName(index);
  95. index -= count;
  96. }
  97. # else
  98. if (const uint count = getRtAudioApiCount())
  99. {
  100. if (index < count)
  101. return getRtAudioApiName(index);
  102. }
  103. # endif
  104. #endif
  105. carla_stderr("CarlaEngine::getDriverName(%i) - invalid index", index2);
  106. return nullptr;
  107. }
  108. const char* const* CarlaEngine::getDriverDeviceNames(const uint index2)
  109. {
  110. carla_debug("CarlaEngine::getDriverDeviceNames(%i)", index2);
  111. using namespace EngineInit;
  112. uint index = index2;
  113. if (jackbridge_is_ok() && index-- == 0)
  114. {
  115. static const char* ret[3] = { "Auto-Connect ON", "Auto-Connect OFF", nullptr };
  116. return ret;
  117. }
  118. #ifndef BUILD_BRIDGE
  119. # ifdef USING_JUCE_AUDIO_DEVICES
  120. if (const uint count = getJuceApiCount())
  121. {
  122. if (index < count)
  123. return getJuceApiDeviceNames(index);
  124. index -= count;
  125. }
  126. # else
  127. if (const uint count = getRtAudioApiCount())
  128. {
  129. if (index < count)
  130. return getRtAudioApiDeviceNames(index);
  131. }
  132. # endif
  133. #endif
  134. carla_stderr("CarlaEngine::getDriverDeviceNames(%i) - invalid index", index2);
  135. return nullptr;
  136. }
  137. const EngineDriverDeviceInfo* CarlaEngine::getDriverDeviceInfo(const uint index2, const char* const deviceName)
  138. {
  139. carla_debug("CarlaEngine::getDriverDeviceInfo(%i, \"%s\")", index2, deviceName);
  140. using namespace EngineInit;
  141. uint index = index2;
  142. if (jackbridge_is_ok() && index-- == 0)
  143. {
  144. static EngineDriverDeviceInfo devInfo;
  145. devInfo.hints = ENGINE_DRIVER_DEVICE_VARIABLE_BUFFER_SIZE;
  146. devInfo.bufferSizes = nullptr;
  147. devInfo.sampleRates = nullptr;
  148. return &devInfo;
  149. }
  150. #ifndef BUILD_BRIDGE
  151. # ifdef USING_JUCE_AUDIO_DEVICES
  152. if (const uint count = getJuceApiCount())
  153. {
  154. if (index < count)
  155. return getJuceDeviceInfo(index, deviceName);
  156. index -= count;
  157. }
  158. # else
  159. if (const uint count = getRtAudioApiCount())
  160. {
  161. if (index < count)
  162. return getRtAudioDeviceInfo(index, deviceName);
  163. }
  164. # endif
  165. #endif
  166. carla_stderr("CarlaEngine::getDriverDeviceNames(%i, \"%s\") - invalid index", index2, deviceName);
  167. return nullptr;
  168. }
  169. bool CarlaEngine::showDriverDeviceControlPanel(const uint index2, const char* const deviceName)
  170. {
  171. carla_debug("CarlaEngine::showDriverDeviceControlPanel(%i, \"%s\")", index2, deviceName);
  172. using namespace EngineInit;
  173. uint index = index2;
  174. if (jackbridge_is_ok() && index-- == 0)
  175. {
  176. return false;
  177. }
  178. #ifndef BUILD_BRIDGE
  179. # ifdef USING_JUCE_AUDIO_DEVICES
  180. if (const uint count = getJuceApiCount())
  181. {
  182. if (index < count)
  183. return showJuceDeviceControlPanel(index, deviceName);
  184. index -= count;
  185. }
  186. # else
  187. if (const uint count = getRtAudioApiCount())
  188. {
  189. if (index < count)
  190. return false;
  191. }
  192. # endif
  193. #endif
  194. carla_stderr("CarlaEngine::showDriverDeviceControlPanel(%i, \"%s\") - invalid index", index2, deviceName);
  195. return false;
  196. }
  197. CarlaEngine* CarlaEngine::newDriverByName(const char* const driverName)
  198. {
  199. CARLA_SAFE_ASSERT_RETURN(driverName != nullptr && driverName[0] != '\0', nullptr);
  200. carla_debug("CarlaEngine::newDriverByName(\"%s\")", driverName);
  201. using namespace EngineInit;
  202. if (std::strcmp(driverName, "JACK") == 0)
  203. return newJack();
  204. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  205. if (std::strcmp(driverName, "Dummy") == 0)
  206. return newDummy();
  207. #endif
  208. #ifndef BUILD_BRIDGE
  209. # ifdef USING_JUCE_AUDIO_DEVICES
  210. // -------------------------------------------------------------------
  211. // linux
  212. if (std::strcmp(driverName, "ALSA") == 0)
  213. return newJuce(AUDIO_API_ALSA);
  214. // -------------------------------------------------------------------
  215. // macos
  216. if (std::strcmp(driverName, "CoreAudio") == 0)
  217. return newJuce(AUDIO_API_COREAUDIO);
  218. // -------------------------------------------------------------------
  219. // windows
  220. if (std::strcmp(driverName, "ASIO") == 0)
  221. return newJuce(AUDIO_API_ASIO);
  222. if (std::strcmp(driverName, "DirectSound") == 0)
  223. return newJuce(AUDIO_API_DIRECTSOUND);
  224. if (std::strcmp(driverName, "WASAPI") == 0 || std::strcmp(driverName, "Windows Audio") == 0)
  225. return newJuce(AUDIO_API_WASAPI);
  226. # else
  227. // -------------------------------------------------------------------
  228. // common
  229. if (std::strncmp(driverName, "JACK ", 5) == 0)
  230. return newRtAudio(AUDIO_API_JACK);
  231. if (std::strcmp(driverName, "OSS") == 0)
  232. return newRtAudio(AUDIO_API_OSS);
  233. // -------------------------------------------------------------------
  234. // linux
  235. if (std::strcmp(driverName, "ALSA") == 0)
  236. return newRtAudio(AUDIO_API_ALSA);
  237. if (std::strcmp(driverName, "PulseAudio") == 0)
  238. return newRtAudio(AUDIO_API_PULSEAUDIO);
  239. // -------------------------------------------------------------------
  240. // macos
  241. if (std::strcmp(driverName, "CoreAudio") == 0)
  242. return newRtAudio(AUDIO_API_COREAUDIO);
  243. // -------------------------------------------------------------------
  244. // windows
  245. if (std::strcmp(driverName, "ASIO") == 0)
  246. return newRtAudio(AUDIO_API_ASIO);
  247. if (std::strcmp(driverName, "DirectSound") == 0)
  248. return newRtAudio(AUDIO_API_DIRECTSOUND);
  249. if (std::strcmp(driverName, "WASAPI") == 0)
  250. return newRtAudio(AUDIO_API_WASAPI);
  251. # endif
  252. #endif
  253. carla_stderr("CarlaEngine::newDriverByName(\"%s\") - invalid driver name", driverName);
  254. return nullptr;
  255. }
  256. // -----------------------------------------------------------------------
  257. // Constant values
  258. uint CarlaEngine::getMaxClientNameSize() const noexcept
  259. {
  260. return STR_MAX/2;
  261. }
  262. uint CarlaEngine::getMaxPortNameSize() const noexcept
  263. {
  264. return STR_MAX;
  265. }
  266. uint CarlaEngine::getCurrentPluginCount() const noexcept
  267. {
  268. return pData->curPluginCount;
  269. }
  270. uint CarlaEngine::getMaxPluginNumber() const noexcept
  271. {
  272. return pData->maxPluginNumber;
  273. }
  274. // -----------------------------------------------------------------------
  275. // Virtual, per-engine type calls
  276. bool CarlaEngine::close()
  277. {
  278. carla_debug("CarlaEngine::close()");
  279. if (pData->curPluginCount != 0)
  280. {
  281. pData->aboutToClose = true;
  282. removeAllPlugins();
  283. }
  284. pData->close();
  285. callback(true, true, ENGINE_CALLBACK_ENGINE_STOPPED, 0, 0, 0, 0, 0.0f, nullptr);
  286. return true;
  287. }
  288. bool CarlaEngine::usesConstantBufferSize() const noexcept
  289. {
  290. return true;
  291. }
  292. void CarlaEngine::idle() noexcept
  293. {
  294. CARLA_SAFE_ASSERT_RETURN(pData->nextAction.opcode == kEnginePostActionNull,);
  295. CARLA_SAFE_ASSERT_RETURN(pData->nextPluginId == pData->maxPluginNumber,);
  296. CARLA_SAFE_ASSERT_RETURN(getType() != kEngineTypePlugin,);
  297. for (uint i=0; i < pData->curPluginCount; ++i)
  298. {
  299. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  300. if (plugin != nullptr && plugin->isEnabled())
  301. {
  302. const uint hints(plugin->getHints());
  303. if ((hints & PLUGIN_HAS_CUSTOM_UI) != 0 && (hints & PLUGIN_NEEDS_UI_MAIN_THREAD) != 0)
  304. {
  305. try {
  306. plugin->uiIdle();
  307. } CARLA_SAFE_EXCEPTION_CONTINUE("Plugin uiIdle");
  308. }
  309. }
  310. }
  311. #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  312. pData->osc.idle();
  313. #endif
  314. }
  315. CarlaEngineClient* CarlaEngine::addClient(CarlaPlugin* const plugin)
  316. {
  317. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  318. return new CarlaEngineClientForStandalone(*this, pData->graph, plugin);
  319. #else
  320. return new CarlaEngineClientForBridge(*this);
  321. // unused
  322. (void)plugin;
  323. #endif
  324. }
  325. float CarlaEngine::getDSPLoad() const noexcept
  326. {
  327. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  328. return pData->dspLoad;
  329. #else
  330. return 0.0f;
  331. #endif
  332. }
  333. uint32_t CarlaEngine::getTotalXruns() const noexcept
  334. {
  335. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  336. return pData->xruns;
  337. #else
  338. return 0;
  339. #endif
  340. }
  341. void CarlaEngine::clearXruns() const noexcept
  342. {
  343. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  344. pData->xruns = 0;
  345. #endif
  346. }
  347. bool CarlaEngine::showDeviceControlPanel() const noexcept
  348. {
  349. return false;
  350. }
  351. bool CarlaEngine::setBufferSizeAndSampleRate(const uint, const double)
  352. {
  353. return false;
  354. }
  355. // -----------------------------------------------------------------------
  356. // Plugin management
  357. bool CarlaEngine::addPlugin(const BinaryType btype,
  358. const PluginType ptype,
  359. const char* const filename,
  360. const char* const name,
  361. const char* const label,
  362. const int64_t uniqueId,
  363. const void* const extra,
  364. const uint options)
  365. {
  366. CARLA_SAFE_ASSERT_RETURN_ERR(pData->isIdling == 0, "An operation is still being processed, please wait for it to finish");
  367. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  368. CARLA_SAFE_ASSERT_RETURN_ERR(pData->plugins != nullptr, "Invalid engine internal data");
  369. CARLA_SAFE_ASSERT_RETURN_ERR(pData->nextPluginId <= pData->maxPluginNumber, "Invalid engine internal data");
  370. #endif
  371. CARLA_SAFE_ASSERT_RETURN_ERR(pData->nextAction.opcode == kEnginePostActionNull, "Invalid engine internal data");
  372. CARLA_SAFE_ASSERT_RETURN_ERR(btype != BINARY_NONE, "Invalid plugin binary mode");
  373. CARLA_SAFE_ASSERT_RETURN_ERR(ptype != PLUGIN_NONE, "Invalid plugin type");
  374. CARLA_SAFE_ASSERT_RETURN_ERR((filename != nullptr && filename[0] != '\0') || (label != nullptr && label[0] != '\0'), "Invalid plugin filename and label");
  375. carla_debug("CarlaEngine::addPlugin(%i:%s, %i:%s, \"%s\", \"%s\", \"%s\", " P_INT64 ", %p, %u)",
  376. btype, BinaryType2Str(btype), ptype, PluginType2Str(ptype), filename, name, label, uniqueId, extra, options);
  377. #ifndef CARLA_OS_WIN
  378. if (ptype != PLUGIN_JACK && ptype != PLUGIN_LV2 && filename != nullptr && filename[0] != '\0') {
  379. CARLA_SAFE_ASSERT_RETURN_ERR(filename[0] == CARLA_OS_SEP || filename[0] == '.' || filename[0] == '~', "Invalid plugin filename");
  380. }
  381. #endif
  382. uint id;
  383. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  384. CarlaPlugin* oldPlugin = nullptr;
  385. if (pData->nextPluginId < pData->curPluginCount)
  386. {
  387. id = pData->nextPluginId;
  388. pData->nextPluginId = pData->maxPluginNumber;
  389. oldPlugin = pData->plugins[id].plugin;
  390. CARLA_SAFE_ASSERT_RETURN_ERR(oldPlugin != nullptr, "Invalid replace plugin Id");
  391. }
  392. else
  393. #endif
  394. {
  395. id = pData->curPluginCount;
  396. if (id == pData->maxPluginNumber)
  397. {
  398. setLastError("Maximum number of plugins reached");
  399. return false;
  400. }
  401. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  402. CARLA_SAFE_ASSERT_RETURN_ERR(pData->plugins[id].plugin == nullptr, "Invalid engine internal data");
  403. #endif
  404. }
  405. CarlaPlugin::Initializer initializer = {
  406. this,
  407. id,
  408. filename,
  409. name,
  410. label,
  411. uniqueId,
  412. options
  413. };
  414. CarlaPlugin* plugin = nullptr;
  415. CarlaString bridgeBinary(pData->options.binaryDir);
  416. if (bridgeBinary.isNotEmpty())
  417. {
  418. #ifndef CARLA_OS_WIN
  419. if (btype == BINARY_NATIVE)
  420. {
  421. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-native";
  422. }
  423. else
  424. #endif
  425. {
  426. switch (btype)
  427. {
  428. case BINARY_POSIX32:
  429. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-posix32";
  430. break;
  431. case BINARY_POSIX64:
  432. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-posix64";
  433. break;
  434. case BINARY_WIN32:
  435. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-win32.exe";
  436. break;
  437. case BINARY_WIN64:
  438. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-win64.exe";
  439. break;
  440. default:
  441. bridgeBinary.clear();
  442. break;
  443. }
  444. }
  445. if (! File(bridgeBinary.buffer()).existsAsFile())
  446. bridgeBinary.clear();
  447. }
  448. // Prefer bridges for some specific plugins
  449. const bool preferBridges = pData->options.preferPluginBridges;
  450. #if 0 // ndef BUILD_BRIDGE
  451. if (! preferBridges)
  452. {
  453. if (ptype == PLUGIN_LV2 && label != nullptr)
  454. {
  455. if (std::strncmp(label, "http://calf.sourceforge.net/plugins/", 36) == 0 ||
  456. std::strcmp(label, "http://factorial.hu/plugins/lv2/ir") == 0 ||
  457. std::strstr(label, "v1.sourceforge.net/lv2") != nullptr)
  458. {
  459. preferBridges = true;
  460. }
  461. }
  462. }
  463. #endif // ! BUILD_BRIDGE
  464. const bool canBeBridged = ptype != PLUGIN_INTERNAL
  465. && ptype != PLUGIN_SF2
  466. && ptype != PLUGIN_SFZ
  467. && ptype != PLUGIN_JACK;
  468. if (canBeBridged && (btype != BINARY_NATIVE || (preferBridges && bridgeBinary.isNotEmpty())))
  469. {
  470. if (bridgeBinary.isNotEmpty())
  471. {
  472. plugin = CarlaPlugin::newBridge(initializer, btype, ptype, bridgeBinary);
  473. }
  474. else
  475. {
  476. setLastError("This Carla build cannot handle this binary");
  477. return false;
  478. }
  479. }
  480. else
  481. {
  482. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  483. bool use16Outs;
  484. #endif
  485. setLastError("Invalid or unsupported plugin type");
  486. // Some stupid plugins mess up with global signals, err!!
  487. const CarlaSignalRestorer csr;
  488. switch (ptype)
  489. {
  490. case PLUGIN_NONE:
  491. break;
  492. case PLUGIN_LADSPA:
  493. plugin = CarlaPlugin::newLADSPA(initializer, (const LADSPA_RDF_Descriptor*)extra);
  494. break;
  495. case PLUGIN_DSSI:
  496. plugin = CarlaPlugin::newDSSI(initializer);
  497. break;
  498. case PLUGIN_LV2:
  499. plugin = CarlaPlugin::newLV2(initializer);
  500. break;
  501. case PLUGIN_VST2:
  502. plugin = CarlaPlugin::newVST2(initializer);
  503. break;
  504. case PLUGIN_VST3:
  505. plugin = CarlaPlugin::newVST3(initializer);
  506. break;
  507. case PLUGIN_AU:
  508. plugin = CarlaPlugin::newAU(initializer);
  509. break;
  510. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  511. case PLUGIN_INTERNAL:
  512. plugin = CarlaPlugin::newNative(initializer);
  513. break;
  514. case PLUGIN_DLS:
  515. case PLUGIN_GIG:
  516. case PLUGIN_SF2:
  517. use16Outs = (extra != nullptr && std::strcmp((const char*)extra, "true") == 0);
  518. plugin = CarlaPlugin::newFluidSynth(initializer, ptype, use16Outs);
  519. break;
  520. case PLUGIN_SFZ:
  521. plugin = CarlaPlugin::newSFZero(initializer);
  522. break;
  523. case PLUGIN_JACK:
  524. plugin = CarlaPlugin::newJackApp(initializer);
  525. break;
  526. #else
  527. case PLUGIN_INTERNAL:
  528. case PLUGIN_DLS:
  529. case PLUGIN_GIG:
  530. case PLUGIN_SF2:
  531. case PLUGIN_SFZ:
  532. case PLUGIN_JACK:
  533. setLastError("Plugin bridges cannot handle this binary");
  534. break;
  535. #endif
  536. }
  537. }
  538. if (plugin == nullptr)
  539. return false;
  540. plugin->reload();
  541. bool canRun = true;
  542. /**/ if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  543. {
  544. if (plugin->getCVInCount() > 0 || plugin->getCVInCount() > 0)
  545. {
  546. setLastError("Carla's rack mode cannot work with plugins that have CV ports, sorry!");
  547. canRun = false;
  548. }
  549. }
  550. else if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  551. {
  552. /**/ if (plugin->getMidiInCount() > 1 || plugin->getMidiOutCount() > 1)
  553. {
  554. setLastError("Carla's patchbay mode cannot work with plugins that have multiple MIDI ports, sorry!");
  555. canRun = false;
  556. }
  557. }
  558. if (! canRun)
  559. {
  560. delete plugin;
  561. return false;
  562. }
  563. EnginePluginData& pluginData(pData->plugins[id]);
  564. pluginData.plugin = plugin;
  565. carla_zeroFloats(pluginData.peaks, 4);
  566. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  567. if (oldPlugin != nullptr)
  568. {
  569. CARLA_SAFE_ASSERT(! pData->loadingProject);
  570. const ScopedThreadStopper sts(this);
  571. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  572. pData->graph.replacePlugin(oldPlugin, plugin);
  573. const bool wasActive = oldPlugin->getInternalParameterValue(PARAMETER_ACTIVE) >= 0.5f;
  574. const float oldDryWet = oldPlugin->getInternalParameterValue(PARAMETER_DRYWET);
  575. const float oldVolume = oldPlugin->getInternalParameterValue(PARAMETER_VOLUME);
  576. delete oldPlugin;
  577. if (plugin->getHints() & PLUGIN_CAN_DRYWET)
  578. plugin->setDryWet(oldDryWet, true, true);
  579. if (plugin->getHints() & PLUGIN_CAN_VOLUME)
  580. plugin->setVolume(oldVolume, true, true);
  581. plugin->setActive(wasActive, true, true);
  582. plugin->setEnabled(true);
  583. callback(true, true, ENGINE_CALLBACK_RELOAD_ALL, id, 0, 0, 0, 0.0f, nullptr);
  584. }
  585. else if (! pData->loadingProject)
  586. #endif
  587. {
  588. plugin->setEnabled(true);
  589. ++pData->curPluginCount;
  590. callback(true, true, ENGINE_CALLBACK_PLUGIN_ADDED, id, 0, 0, 0, 0.0f, plugin->getName());
  591. if (getType() != kEngineTypeBridge)
  592. plugin->setActive(true, true, true);
  593. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  594. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  595. pData->graph.addPlugin(plugin);
  596. #endif
  597. }
  598. return true;
  599. }
  600. bool CarlaEngine::addPlugin(const PluginType ptype,
  601. const char* const filename,
  602. const char* const name,
  603. const char* const label,
  604. const int64_t uniqueId,
  605. const void* const extra)
  606. {
  607. return addPlugin(BINARY_NATIVE, ptype, filename, name, label, uniqueId, extra, PLUGIN_OPTIONS_NULL);
  608. }
  609. bool CarlaEngine::removePlugin(const uint id)
  610. {
  611. CARLA_SAFE_ASSERT_RETURN_ERR(pData->isIdling == 0, "An operation is still being processed, please wait for it to finish");
  612. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  613. CARLA_SAFE_ASSERT_RETURN_ERR(pData->plugins != nullptr, "Invalid engine internal data");
  614. CARLA_SAFE_ASSERT_RETURN_ERR(pData->curPluginCount != 0, "Invalid engine internal data");
  615. #endif
  616. CARLA_SAFE_ASSERT_RETURN_ERR(pData->nextAction.opcode == kEnginePostActionNull, "Invalid engine internal data");
  617. CARLA_SAFE_ASSERT_RETURN_ERR(id < pData->curPluginCount, "Invalid plugin Id");
  618. carla_debug("CarlaEngine::removePlugin(%i)", id);
  619. CarlaPlugin* const plugin(pData->plugins[id].plugin);
  620. CARLA_SAFE_ASSERT_RETURN_ERR(plugin != nullptr, "Could not find plugin to remove");
  621. CARLA_SAFE_ASSERT_RETURN_ERR(plugin->getId() == id, "Invalid engine internal data");
  622. const ScopedThreadStopper sts(this);
  623. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  624. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  625. pData->graph.removePlugin(plugin);
  626. const ScopedActionLock sal(this, kEnginePostActionRemovePlugin, id, 0);
  627. /*
  628. for (uint i=id; i < pData->curPluginCount; ++i)
  629. {
  630. CarlaPlugin* const plugin2(pData->plugins[i].plugin);
  631. CARLA_SAFE_ASSERT_BREAK(plugin2 != nullptr);
  632. plugin2->updateOscURL();
  633. }
  634. */
  635. #else
  636. pData->curPluginCount = 0;
  637. carla_zeroStructs(pData->plugins, 1);
  638. #endif
  639. delete plugin;
  640. callback(true, true, ENGINE_CALLBACK_PLUGIN_REMOVED, id, 0, 0, 0, 0.0f, nullptr);
  641. return true;
  642. }
  643. bool CarlaEngine::removeAllPlugins()
  644. {
  645. CARLA_SAFE_ASSERT_RETURN_ERR(pData->isIdling == 0, "An operation is still being processed, please wait for it to finish");
  646. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  647. CARLA_SAFE_ASSERT_RETURN_ERR(pData->plugins != nullptr, "Invalid engine internal data");
  648. CARLA_SAFE_ASSERT_RETURN_ERR(pData->nextPluginId == pData->maxPluginNumber, "Invalid engine internal data");
  649. #endif
  650. CARLA_SAFE_ASSERT_RETURN_ERR(pData->nextAction.opcode == kEnginePostActionNull, "Invalid engine internal data");
  651. carla_debug("CarlaEngine::removeAllPlugins()");
  652. if (pData->curPluginCount == 0)
  653. return true;
  654. const ScopedThreadStopper sts(this);
  655. const uint curPluginCount(pData->curPluginCount);
  656. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  657. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  658. pData->graph.removeAllPlugins();
  659. #endif
  660. const ScopedActionLock sal(this, kEnginePostActionZeroCount, 0, 0);
  661. callback(true, false, ENGINE_CALLBACK_IDLE, 0, 0, 0, 0, 0.0f, nullptr);
  662. for (uint i=0; i < curPluginCount; ++i)
  663. {
  664. const uint id = curPluginCount - i - 1;
  665. EnginePluginData& pluginData(pData->plugins[id]);
  666. if (pluginData.plugin != nullptr)
  667. {
  668. delete pluginData.plugin;
  669. pluginData.plugin = nullptr;
  670. }
  671. carla_zeroFloats(pluginData.peaks, 4);
  672. callback(true, true, ENGINE_CALLBACK_PLUGIN_REMOVED, id, 0, 0, 0, 0.0f, nullptr);
  673. callback(true, false, ENGINE_CALLBACK_IDLE, 0, 0, 0, 0, 0.0f, nullptr);
  674. }
  675. return true;
  676. }
  677. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  678. bool CarlaEngine::renamePlugin(const uint id, const char* const newName)
  679. {
  680. CARLA_SAFE_ASSERT_RETURN_ERR(pData->isIdling == 0, "An operation is still being processed, please wait for it to finish");
  681. CARLA_SAFE_ASSERT_RETURN_ERR(pData->plugins != nullptr, "Invalid engine internal data");
  682. CARLA_SAFE_ASSERT_RETURN_ERR(pData->curPluginCount != 0, "Invalid engine internal data");
  683. CARLA_SAFE_ASSERT_RETURN_ERR(pData->nextAction.opcode == kEnginePostActionNull, "Invalid engine internal data");
  684. CARLA_SAFE_ASSERT_RETURN_ERR(id < pData->curPluginCount, "Invalid plugin Id");
  685. CARLA_SAFE_ASSERT_RETURN_ERR(newName != nullptr && newName[0] != '\0', "Invalid plugin name");
  686. carla_debug("CarlaEngine::renamePlugin(%i, \"%s\")", id, newName);
  687. CarlaPlugin* const plugin(pData->plugins[id].plugin);
  688. CARLA_SAFE_ASSERT_RETURN_ERR(plugin != nullptr, "Could not find plugin to rename");
  689. CARLA_SAFE_ASSERT_RETURN_ERR(plugin->getId() == id, "Invalid engine internal data");
  690. const char* const uniqueName(getUniquePluginName(newName));
  691. CARLA_SAFE_ASSERT_RETURN_ERR(uniqueName != nullptr, "Unable to get new unique plugin name");
  692. plugin->setName(uniqueName);
  693. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  694. pData->graph.renamePlugin(plugin, uniqueName);
  695. callback(true, true, ENGINE_CALLBACK_PLUGIN_RENAMED, id, 0, 0, 0, 0.0f, uniqueName);
  696. delete[] uniqueName;
  697. return true;
  698. }
  699. bool CarlaEngine::clonePlugin(const uint id)
  700. {
  701. CARLA_SAFE_ASSERT_RETURN_ERR(pData->isIdling == 0, "An operation is still being processed, please wait for it to finish");
  702. CARLA_SAFE_ASSERT_RETURN_ERR(pData->plugins != nullptr, "Invalid engine internal data");
  703. CARLA_SAFE_ASSERT_RETURN_ERR(pData->curPluginCount != 0, "Invalid engine internal data");
  704. CARLA_SAFE_ASSERT_RETURN_ERR(pData->nextAction.opcode == kEnginePostActionNull, "Invalid engine internal data");
  705. CARLA_SAFE_ASSERT_RETURN_ERR(id < pData->curPluginCount, "Invalid plugin Id");
  706. carla_debug("CarlaEngine::clonePlugin(%i)", id);
  707. CarlaPlugin* const plugin(pData->plugins[id].plugin);
  708. CARLA_SAFE_ASSERT_RETURN_ERR(plugin != nullptr, "Could not find plugin to clone");
  709. CARLA_SAFE_ASSERT_RETURN_ERR(plugin->getId() == id, "Invalid engine internal data");
  710. char label[STR_MAX+1];
  711. carla_zeroChars(label, STR_MAX+1);
  712. if (! plugin->getLabel(label))
  713. label[0] = '\0';
  714. const uint pluginCountBefore(pData->curPluginCount);
  715. if (! addPlugin(plugin->getBinaryType(), plugin->getType(),
  716. plugin->getFilename(), plugin->getName(), label, plugin->getUniqueId(),
  717. plugin->getExtraStuff(), plugin->getOptionsEnabled()))
  718. return false;
  719. CARLA_SAFE_ASSERT_RETURN_ERR(pluginCountBefore+1 == pData->curPluginCount, "No new plugin found");
  720. if (CarlaPlugin* const newPlugin = pData->plugins[pluginCountBefore].plugin)
  721. newPlugin->loadStateSave(plugin->getStateSave());
  722. return true;
  723. }
  724. bool CarlaEngine::replacePlugin(const uint id) noexcept
  725. {
  726. CARLA_SAFE_ASSERT_RETURN_ERR(pData->isIdling == 0, "An operation is still being processed, please wait for it to finish");
  727. CARLA_SAFE_ASSERT_RETURN_ERR(pData->plugins != nullptr, "Invalid engine internal data");
  728. CARLA_SAFE_ASSERT_RETURN_ERR(pData->curPluginCount != 0, "Invalid engine internal data");
  729. CARLA_SAFE_ASSERT_RETURN_ERR(pData->nextAction.opcode == kEnginePostActionNull, "Invalid engine internal data");
  730. carla_debug("CarlaEngine::replacePlugin(%i)", id);
  731. // might use this to reset
  732. if (id == pData->maxPluginNumber)
  733. {
  734. pData->nextPluginId = pData->maxPluginNumber;
  735. return true;
  736. }
  737. CARLA_SAFE_ASSERT_RETURN_ERR(id < pData->curPluginCount, "Invalid plugin Id");
  738. CarlaPlugin* const plugin(pData->plugins[id].plugin);
  739. CARLA_SAFE_ASSERT_RETURN_ERR(plugin != nullptr, "Could not find plugin to replace");
  740. CARLA_SAFE_ASSERT_RETURN_ERR(plugin->getId() == id, "Invalid engine internal data");
  741. pData->nextPluginId = id;
  742. return true;
  743. }
  744. bool CarlaEngine::switchPlugins(const uint idA, const uint idB) noexcept
  745. {
  746. CARLA_SAFE_ASSERT_RETURN_ERR(pData->isIdling == 0, "An operation is still being processed, please wait for it to finish");
  747. CARLA_SAFE_ASSERT_RETURN_ERR(pData->plugins != nullptr, "Invalid engine internal data");
  748. CARLA_SAFE_ASSERT_RETURN_ERR(pData->curPluginCount >= 2, "Invalid engine internal data");
  749. CARLA_SAFE_ASSERT_RETURN_ERR(pData->nextAction.opcode == kEnginePostActionNull, "Invalid engine internal data");
  750. CARLA_SAFE_ASSERT_RETURN_ERR(idA != idB, "Invalid operation, cannot switch plugin with itself");
  751. CARLA_SAFE_ASSERT_RETURN_ERR(idA < pData->curPluginCount, "Invalid plugin Id");
  752. CARLA_SAFE_ASSERT_RETURN_ERR(idB < pData->curPluginCount, "Invalid plugin Id");
  753. carla_debug("CarlaEngine::switchPlugins(%i)", idA, idB);
  754. CarlaPlugin* const pluginA(pData->plugins[idA].plugin);
  755. CarlaPlugin* const pluginB(pData->plugins[idB].plugin);
  756. CARLA_SAFE_ASSERT_RETURN_ERR(pluginA != nullptr, "Could not find plugin to switch");
  757. CARLA_SAFE_ASSERT_RETURN_ERR(pluginA != nullptr, "Could not find plugin to switch");
  758. CARLA_SAFE_ASSERT_RETURN_ERR(pluginA->getId() == idA, "Invalid engine internal data");
  759. CARLA_SAFE_ASSERT_RETURN_ERR(pluginB->getId() == idB, "Invalid engine internal data");
  760. const ScopedThreadStopper sts(this);
  761. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  762. pData->graph.replacePlugin(pluginA, pluginB);
  763. const ScopedActionLock sal(this, kEnginePostActionSwitchPlugins, idA, idB);
  764. // TODO
  765. /*
  766. pluginA->updateOscURL();
  767. pluginB->updateOscURL();
  768. if (isOscControlRegistered())
  769. oscSend_control_switch_plugins(idA, idB);
  770. */
  771. return true;
  772. }
  773. #endif
  774. void CarlaEngine::touchPluginParameter(const uint, const uint32_t, const bool) noexcept
  775. {
  776. }
  777. CarlaPlugin* CarlaEngine::getPlugin(const uint id) const noexcept
  778. {
  779. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  780. CARLA_SAFE_ASSERT_RETURN_ERRN(pData->plugins != nullptr, "Invalid engine internal data");
  781. CARLA_SAFE_ASSERT_RETURN_ERRN(pData->curPluginCount != 0, "Invalid engine internal data");
  782. #endif
  783. CARLA_SAFE_ASSERT_RETURN_ERRN(pData->nextAction.opcode == kEnginePostActionNull, "Invalid engine internal data");
  784. CARLA_SAFE_ASSERT_RETURN_ERRN(id < pData->curPluginCount, "Invalid plugin Id");
  785. return pData->plugins[id].plugin;
  786. }
  787. CarlaPlugin* CarlaEngine::getPluginUnchecked(const uint id) const noexcept
  788. {
  789. return pData->plugins[id].plugin;
  790. }
  791. const char* CarlaEngine::getUniquePluginName(const char* const name) const
  792. {
  793. CARLA_SAFE_ASSERT_RETURN(pData->nextAction.opcode == kEnginePostActionNull, nullptr);
  794. CARLA_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0', nullptr);
  795. carla_debug("CarlaEngine::getUniquePluginName(\"%s\")", name);
  796. CarlaString sname;
  797. sname = name;
  798. if (sname.isEmpty())
  799. {
  800. sname = "(No name)";
  801. return sname.dup();
  802. }
  803. const std::size_t maxNameSize(carla_minConstrained<uint>(getMaxClientNameSize(), 0xff, 6U) - 6); // 6 = strlen(" (10)") + 1
  804. if (maxNameSize == 0 || ! isRunning())
  805. return sname.dup();
  806. sname.truncate(maxNameSize);
  807. sname.replace(':', '.'); // ':' is used in JACK1 to split client/port names
  808. for (uint i=0; i < pData->curPluginCount; ++i)
  809. {
  810. CARLA_SAFE_ASSERT_BREAK(pData->plugins[i].plugin != nullptr);
  811. // Check if unique name doesn't exist
  812. if (const char* const pluginName = pData->plugins[i].plugin->getName())
  813. {
  814. if (sname != pluginName)
  815. continue;
  816. }
  817. // Check if string has already been modified
  818. {
  819. const std::size_t len(sname.length());
  820. // 1 digit, ex: " (2)"
  821. if (sname[len-4] == ' ' && sname[len-3] == '(' && sname.isDigit(len-2) && sname[len-1] == ')')
  822. {
  823. const int number = sname[len-2] - '0';
  824. if (number == 9)
  825. {
  826. // next number is 10, 2 digits
  827. sname.truncate(len-4);
  828. sname += " (10)";
  829. //sname.replace(" (9)", " (10)");
  830. }
  831. else
  832. sname[len-2] = char('0' + number + 1);
  833. continue;
  834. }
  835. // 2 digits, ex: " (11)"
  836. if (sname[len-5] == ' ' && sname[len-4] == '(' && sname.isDigit(len-3) && sname.isDigit(len-2) && sname[len-1] == ')')
  837. {
  838. char n2 = sname[len-2];
  839. char n3 = sname[len-3];
  840. if (n2 == '9')
  841. {
  842. n2 = '0';
  843. n3 = static_cast<char>(n3 + 1);
  844. }
  845. else
  846. n2 = static_cast<char>(n2 + 1);
  847. sname[len-2] = n2;
  848. sname[len-3] = n3;
  849. continue;
  850. }
  851. }
  852. // Modify string if not
  853. sname += " (2)";
  854. }
  855. return sname.dup();
  856. }
  857. // -----------------------------------------------------------------------
  858. // Project management
  859. bool CarlaEngine::loadFile(const char* const filename)
  860. {
  861. CARLA_SAFE_ASSERT_RETURN_ERR(pData->isIdling == 0, "An operation is still being processed, please wait for it to finish");
  862. CARLA_SAFE_ASSERT_RETURN_ERR(filename != nullptr && filename[0] != '\0', "Invalid filename");
  863. carla_debug("CarlaEngine::loadFile(\"%s\")", filename);
  864. const String jfilename = String(CharPointer_UTF8(filename));
  865. File file(jfilename);
  866. CARLA_SAFE_ASSERT_RETURN_ERR(file.exists(), "Requested file does not exist or is not a readable");
  867. CarlaString baseName(file.getFileNameWithoutExtension().toRawUTF8());
  868. CarlaString extension(file.getFileExtension().replace(".","").toLowerCase().toRawUTF8());
  869. const uint curPluginId(pData->nextPluginId < pData->curPluginCount ? pData->nextPluginId : pData->curPluginCount);
  870. // -------------------------------------------------------------------
  871. // NOTE: please keep in sync with carla_get_supported_file_extensions!!
  872. if (extension == "carxp" || extension == "carxs")
  873. return loadProject(filename, false);
  874. // -------------------------------------------------------------------
  875. if (extension == "dls")
  876. return addPlugin(PLUGIN_DLS, filename, baseName, baseName, 0, nullptr);
  877. if (extension == "gig")
  878. return addPlugin(PLUGIN_GIG, filename, baseName, baseName, 0, nullptr);
  879. if (extension == "sf2" || extension == "sf3")
  880. return addPlugin(PLUGIN_SF2, filename, baseName, baseName, 0, nullptr);
  881. if (extension == "sfz")
  882. return addPlugin(PLUGIN_SFZ, filename, baseName, baseName, 0, nullptr);
  883. // -------------------------------------------------------------------
  884. if (
  885. #ifdef HAVE_SNDFILE
  886. extension == "aif" ||
  887. extension == "aifc" ||
  888. extension == "aiff" ||
  889. extension == "au" ||
  890. extension == "bwf" ||
  891. extension == "flac" ||
  892. extension == "htk" ||
  893. extension == "iff" ||
  894. extension == "mat4" ||
  895. extension == "mat5" ||
  896. extension == "oga" ||
  897. extension == "ogg" ||
  898. extension == "paf" ||
  899. extension == "pvf" ||
  900. extension == "pvf5" ||
  901. extension == "sd2" ||
  902. extension == "sf" ||
  903. extension == "snd" ||
  904. extension == "svx" ||
  905. extension == "vcc" ||
  906. extension == "w64" ||
  907. extension == "wav" ||
  908. extension == "xi" ||
  909. #endif
  910. #ifdef HAVE_FFMPEG
  911. extension == "3g2" ||
  912. extension == "3gp" ||
  913. extension == "aac" ||
  914. extension == "ac3" ||
  915. extension == "amr" ||
  916. extension == "ape" ||
  917. extension == "mp2" ||
  918. extension == "mp3" ||
  919. extension == "mpc" ||
  920. extension == "wma" ||
  921. # ifndef HAVE_SNDFILE
  922. // FFmpeg without sndfile
  923. extension == "flac" ||
  924. extension == "oga" ||
  925. extension == "ogg" ||
  926. extension == "w64" ||
  927. extension == "wav" ||
  928. # endif
  929. #endif
  930. false
  931. )
  932. {
  933. if (addPlugin(PLUGIN_INTERNAL, nullptr, baseName, "audiofile", 0, nullptr))
  934. {
  935. if (CarlaPlugin* const plugin = getPlugin(curPluginId))
  936. plugin->setCustomData(CUSTOM_DATA_TYPE_STRING, "file", filename, true);
  937. return true;
  938. }
  939. return false;
  940. }
  941. // -------------------------------------------------------------------
  942. if (extension == "mid" || extension == "midi")
  943. {
  944. if (addPlugin(PLUGIN_INTERNAL, nullptr, baseName, "midifile", 0, nullptr))
  945. {
  946. if (CarlaPlugin* const plugin = getPlugin(curPluginId))
  947. plugin->setCustomData(CUSTOM_DATA_TYPE_STRING, "file", filename, true);
  948. return true;
  949. }
  950. return false;
  951. }
  952. // -------------------------------------------------------------------
  953. // ZynAddSubFX
  954. if (extension == "xmz" || extension == "xiz")
  955. {
  956. #ifdef HAVE_ZYN_DEPS
  957. CarlaString nicerName("Zyn - ");
  958. const std::size_t sep(baseName.find('-')+1);
  959. if (sep < baseName.length())
  960. nicerName += baseName.buffer()+sep;
  961. else
  962. nicerName += baseName;
  963. //nicerName
  964. if (addPlugin(PLUGIN_INTERNAL, nullptr, nicerName, "zynaddsubfx", 0, nullptr))
  965. {
  966. callback(true, true, ENGINE_CALLBACK_UI_STATE_CHANGED, curPluginId, 0, 0, 0, 0.0f, nullptr);
  967. if (CarlaPlugin* const plugin = getPlugin(curPluginId))
  968. plugin->setCustomData(CUSTOM_DATA_TYPE_STRING, (extension == "xmz") ? "CarlaAlternateFile1" : "CarlaAlternateFile2", filename, true);
  969. return true;
  970. }
  971. return false;
  972. #else
  973. setLastError("This Carla build does not have ZynAddSubFX support");
  974. return false;
  975. #endif
  976. }
  977. // -------------------------------------------------------------------
  978. // Direct plugin binaries
  979. #ifdef CARLA_OS_MAC
  980. if (extension == "vst")
  981. return addPlugin(PLUGIN_VST2, filename, nullptr, nullptr, 0, nullptr);
  982. #else
  983. if (extension == "dll" || extension == "so")
  984. return addPlugin(getBinaryTypeFromFile(filename), PLUGIN_VST2, filename, nullptr, nullptr, 0, nullptr);
  985. #endif
  986. #ifdef USING_JUCE
  987. if (extension == "vst3")
  988. return addPlugin(getBinaryTypeFromFile(filename), PLUGIN_VST3, filename, nullptr, nullptr, 0, nullptr);
  989. #endif
  990. // -------------------------------------------------------------------
  991. setLastError("Unknown file extension");
  992. return false;
  993. }
  994. bool CarlaEngine::loadProject(const char* const filename, const bool setAsCurrentProject)
  995. {
  996. CARLA_SAFE_ASSERT_RETURN_ERR(pData->isIdling == 0, "An operation is still being processed, please wait for it to finish");
  997. CARLA_SAFE_ASSERT_RETURN_ERR(filename != nullptr && filename[0] != '\0', "Invalid filename");
  998. carla_debug("CarlaEngine::loadProject(\"%s\")", filename);
  999. const String jfilename = String(CharPointer_UTF8(filename));
  1000. File file(jfilename);
  1001. CARLA_SAFE_ASSERT_RETURN_ERR(file.existsAsFile(), "Requested file does not exist or is not a readable file");
  1002. if (setAsCurrentProject)
  1003. {
  1004. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1005. pData->currentProjectFilename = filename;
  1006. #endif
  1007. }
  1008. XmlDocument xml(file);
  1009. return loadProjectInternal(xml);
  1010. }
  1011. bool CarlaEngine::saveProject(const char* const filename, const bool setAsCurrentProject)
  1012. {
  1013. CARLA_SAFE_ASSERT_RETURN_ERR(filename != nullptr && filename[0] != '\0', "Invalid filename");
  1014. carla_debug("CarlaEngine::saveProject(\"%s\")", filename);
  1015. MemoryOutputStream out;
  1016. saveProjectInternal(out);
  1017. const String jfilename = String(CharPointer_UTF8(filename));
  1018. File file(jfilename);
  1019. if (setAsCurrentProject)
  1020. {
  1021. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1022. pData->currentProjectFilename = filename;
  1023. #endif
  1024. }
  1025. if (file.replaceWithData(out.getData(), out.getDataSize()))
  1026. return true;
  1027. setLastError("Failed to write file");
  1028. return false;
  1029. }
  1030. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1031. const char* CarlaEngine::getCurrentProjectFilename() const noexcept
  1032. {
  1033. return pData->currentProjectFilename;
  1034. }
  1035. void CarlaEngine::clearCurrentProjectFilename() noexcept
  1036. {
  1037. pData->currentProjectFilename.clear();
  1038. }
  1039. #endif
  1040. // -----------------------------------------------------------------------
  1041. // Information (base)
  1042. uint32_t CarlaEngine::getBufferSize() const noexcept
  1043. {
  1044. return pData->bufferSize;
  1045. }
  1046. double CarlaEngine::getSampleRate() const noexcept
  1047. {
  1048. return pData->sampleRate;
  1049. }
  1050. const char* CarlaEngine::getName() const noexcept
  1051. {
  1052. return pData->name;
  1053. }
  1054. EngineProcessMode CarlaEngine::getProccessMode() const noexcept
  1055. {
  1056. return pData->options.processMode;
  1057. }
  1058. const EngineOptions& CarlaEngine::getOptions() const noexcept
  1059. {
  1060. return pData->options;
  1061. }
  1062. EngineTimeInfo CarlaEngine::getTimeInfo() const noexcept
  1063. {
  1064. return pData->timeInfo;
  1065. }
  1066. // -----------------------------------------------------------------------
  1067. // Information (peaks)
  1068. const float* CarlaEngine::getPeaks(const uint pluginId) const noexcept
  1069. {
  1070. static const float kFallback[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  1071. if (pluginId == MAIN_CARLA_PLUGIN_ID)
  1072. {
  1073. // get peak from first plugin, if available
  1074. if (const uint count = pData->curPluginCount)
  1075. {
  1076. pData->peaks[0] = pData->plugins[0].peaks[0];
  1077. pData->peaks[1] = pData->plugins[0].peaks[1];
  1078. pData->peaks[2] = pData->plugins[count-1].peaks[2];
  1079. pData->peaks[3] = pData->plugins[count-1].peaks[3];
  1080. }
  1081. else
  1082. {
  1083. carla_zeroFloats(pData->peaks, 4);
  1084. }
  1085. return pData->peaks;
  1086. }
  1087. CARLA_SAFE_ASSERT_RETURN(pluginId < pData->curPluginCount, kFallback);
  1088. return pData->plugins[pluginId].peaks;
  1089. }
  1090. float CarlaEngine::getInputPeak(const uint pluginId, const bool isLeft) const noexcept
  1091. {
  1092. if (pluginId == MAIN_CARLA_PLUGIN_ID)
  1093. {
  1094. // get peak from first plugin, if available
  1095. if (pData->curPluginCount > 0)
  1096. return pData->plugins[0].peaks[isLeft ? 0 : 1];
  1097. return 0.0f;
  1098. }
  1099. CARLA_SAFE_ASSERT_RETURN(pluginId < pData->curPluginCount, 0.0f);
  1100. return pData->plugins[pluginId].peaks[isLeft ? 0 : 1];
  1101. }
  1102. float CarlaEngine::getOutputPeak(const uint pluginId, const bool isLeft) const noexcept
  1103. {
  1104. if (pluginId == MAIN_CARLA_PLUGIN_ID)
  1105. {
  1106. // get peak from last plugin, if available
  1107. if (pData->curPluginCount > 0)
  1108. return pData->plugins[pData->curPluginCount-1].peaks[isLeft ? 2 : 3];
  1109. return 0.0f;
  1110. }
  1111. CARLA_SAFE_ASSERT_RETURN(pluginId < pData->curPluginCount, 0.0f);
  1112. return pData->plugins[pluginId].peaks[isLeft ? 2 : 3];
  1113. }
  1114. // -----------------------------------------------------------------------
  1115. // Callback
  1116. void CarlaEngine::callback(const bool sendHost, const bool sendOSC,
  1117. const EngineCallbackOpcode action, const uint pluginId,
  1118. const int value1, const int value2, const int value3,
  1119. const float valuef, const char* const valueStr) noexcept
  1120. {
  1121. #ifdef DEBUG
  1122. if (pData->isIdling)
  1123. carla_stdout("CarlaEngine::callback [while idling] (%i:%s, %i, %i, %i, %i, %f, \"%s\")",
  1124. action, EngineCallbackOpcode2Str(action), pluginId, value1, value2, value3,
  1125. static_cast<double>(valuef), valueStr);
  1126. else if (action != ENGINE_CALLBACK_IDLE && action != ENGINE_CALLBACK_NOTE_ON && action != ENGINE_CALLBACK_NOTE_OFF)
  1127. carla_debug("CarlaEngine::callback(%i:%s, %i, %i, %i, %i, %f, \"%s\")",
  1128. action, EngineCallbackOpcode2Str(action), pluginId, value1, value2, value3,
  1129. static_cast<double>(valuef), valueStr);
  1130. #endif
  1131. if (sendHost && pData->callback != nullptr)
  1132. {
  1133. if (action == ENGINE_CALLBACK_IDLE)
  1134. ++pData->isIdling;
  1135. try {
  1136. pData->callback(pData->callbackPtr, action, pluginId, value1, value2, value3, valuef, valueStr);
  1137. } CARLA_SAFE_EXCEPTION("callback")
  1138. if (action == ENGINE_CALLBACK_IDLE)
  1139. --pData->isIdling;
  1140. }
  1141. if (sendOSC)
  1142. {
  1143. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1144. if (pData->osc.isControlRegisteredForTCP())
  1145. {
  1146. switch (action)
  1147. {
  1148. case ENGINE_CALLBACK_RELOAD_INFO:
  1149. {
  1150. CarlaPlugin* const plugin = pData->plugins[pluginId].plugin;
  1151. CARLA_SAFE_ASSERT_BREAK(plugin != nullptr);
  1152. pData->osc.sendPluginInfo(plugin);
  1153. break;
  1154. }
  1155. case ENGINE_CALLBACK_RELOAD_PARAMETERS:
  1156. {
  1157. CarlaPlugin* const plugin = pData->plugins[pluginId].plugin;
  1158. CARLA_SAFE_ASSERT_BREAK(plugin != nullptr);
  1159. pData->osc.sendPluginPortCount(plugin);
  1160. if (const uint32_t count = plugin->getParameterCount())
  1161. {
  1162. for (uint32_t i=0; i<count; ++i)
  1163. pData->osc.sendPluginParameterInfo(plugin, i);
  1164. }
  1165. break;
  1166. }
  1167. case ENGINE_CALLBACK_RELOAD_PROGRAMS:
  1168. {
  1169. CarlaPlugin* const plugin = pData->plugins[pluginId].plugin;
  1170. CARLA_SAFE_ASSERT_BREAK(plugin != nullptr);
  1171. pData->osc.sendPluginProgramCount(plugin);
  1172. if (const uint32_t count = plugin->getProgramCount())
  1173. {
  1174. for (uint32_t i=0; i<count; ++i)
  1175. pData->osc.sendPluginProgram(plugin, i);
  1176. }
  1177. if (const uint32_t count = plugin->getMidiProgramCount())
  1178. {
  1179. for (uint32_t i=0; i<count; ++i)
  1180. pData->osc.sendPluginMidiProgram(plugin, i);
  1181. }
  1182. break;
  1183. }
  1184. case ENGINE_CALLBACK_PLUGIN_ADDED:
  1185. case ENGINE_CALLBACK_RELOAD_ALL:
  1186. {
  1187. CarlaPlugin* const plugin = pData->plugins[pluginId].plugin;
  1188. CARLA_SAFE_ASSERT_BREAK(plugin != nullptr);
  1189. pData->osc.sendPluginInfo(plugin);
  1190. pData->osc.sendPluginPortCount(plugin);
  1191. pData->osc.sendPluginDataCount(plugin);
  1192. if (const uint32_t count = plugin->getParameterCount())
  1193. {
  1194. for (uint32_t i=0; i<count; ++i)
  1195. pData->osc.sendPluginParameterInfo(plugin, i);
  1196. }
  1197. if (const uint32_t count = plugin->getProgramCount())
  1198. {
  1199. for (uint32_t i=0; i<count; ++i)
  1200. pData->osc.sendPluginProgram(plugin, i);
  1201. }
  1202. if (const uint32_t count = plugin->getMidiProgramCount())
  1203. {
  1204. for (uint32_t i=0; i<count; ++i)
  1205. pData->osc.sendPluginMidiProgram(plugin, i);
  1206. }
  1207. if (const uint32_t count = plugin->getCustomDataCount())
  1208. {
  1209. for (uint32_t i=0; i<count; ++i)
  1210. pData->osc.sendPluginCustomData(plugin, i);
  1211. }
  1212. pData->osc.sendPluginInternalParameterValues(plugin);
  1213. break;
  1214. }
  1215. case ENGINE_CALLBACK_IDLE:
  1216. return;
  1217. default:
  1218. break;
  1219. }
  1220. pData->osc.sendCallback(action, pluginId, value1, value2, value3, valuef, valueStr);
  1221. }
  1222. #endif
  1223. }
  1224. }
  1225. void CarlaEngine::setCallback(const EngineCallbackFunc func, void* const ptr) noexcept
  1226. {
  1227. carla_debug("CarlaEngine::setCallback(%p, %p)", func, ptr);
  1228. pData->callback = func;
  1229. pData->callbackPtr = ptr;
  1230. }
  1231. // -----------------------------------------------------------------------
  1232. // File Callback
  1233. const char* CarlaEngine::runFileCallback(const FileCallbackOpcode action, const bool isDir, const char* const title, const char* const filter) noexcept
  1234. {
  1235. CARLA_SAFE_ASSERT_RETURN(title != nullptr && title[0] != '\0', nullptr);
  1236. CARLA_SAFE_ASSERT_RETURN(filter != nullptr, nullptr);
  1237. carla_debug("CarlaEngine::runFileCallback(%i:%s, %s, \"%s\", \"%s\")", action, FileCallbackOpcode2Str(action), bool2str(isDir), title, filter);
  1238. const char* ret = nullptr;
  1239. if (pData->fileCallback != nullptr)
  1240. {
  1241. try {
  1242. ret = pData->fileCallback(pData->fileCallbackPtr, action, isDir, title, filter);
  1243. } CARLA_SAFE_EXCEPTION("runFileCallback");
  1244. }
  1245. return ret;
  1246. }
  1247. void CarlaEngine::setFileCallback(const FileCallbackFunc func, void* const ptr) noexcept
  1248. {
  1249. carla_debug("CarlaEngine::setFileCallback(%p, %p)", func, ptr);
  1250. pData->fileCallback = func;
  1251. pData->fileCallbackPtr = ptr;
  1252. }
  1253. // -----------------------------------------------------------------------
  1254. // Transport
  1255. void CarlaEngine::transportPlay() noexcept
  1256. {
  1257. pData->timeInfo.playing = true;
  1258. pData->time.setNeedsReset();
  1259. }
  1260. void CarlaEngine::transportPause() noexcept
  1261. {
  1262. if (pData->timeInfo.playing)
  1263. pData->time.pause();
  1264. else
  1265. pData->time.setNeedsReset();
  1266. }
  1267. void CarlaEngine::transportBPM(const double bpm) noexcept
  1268. {
  1269. CARLA_SAFE_ASSERT_RETURN(bpm >= 20.0,)
  1270. try {
  1271. pData->time.setBPM(bpm);
  1272. } CARLA_SAFE_EXCEPTION("CarlaEngine::transportBPM");
  1273. }
  1274. void CarlaEngine::transportRelocate(const uint64_t frame) noexcept
  1275. {
  1276. pData->time.relocate(frame);
  1277. }
  1278. // -----------------------------------------------------------------------
  1279. // Error handling
  1280. const char* CarlaEngine::getLastError() const noexcept
  1281. {
  1282. return pData->lastError;
  1283. }
  1284. void CarlaEngine::setLastError(const char* const error) const noexcept
  1285. {
  1286. pData->lastError = error;
  1287. }
  1288. // -----------------------------------------------------------------------
  1289. // Misc
  1290. bool CarlaEngine::isAboutToClose() const noexcept
  1291. {
  1292. return pData->aboutToClose;
  1293. }
  1294. bool CarlaEngine::setAboutToClose() noexcept
  1295. {
  1296. carla_debug("CarlaEngine::setAboutToClose()");
  1297. pData->aboutToClose = true;
  1298. return (pData->isIdling == 0);
  1299. }
  1300. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1301. bool CarlaEngine::isLoadingProject() const noexcept
  1302. {
  1303. return pData->loadingProject;
  1304. }
  1305. #endif
  1306. void CarlaEngine::setActionCanceled(const bool canceled) noexcept
  1307. {
  1308. pData->actionCanceled = canceled;
  1309. }
  1310. bool CarlaEngine::wasActionCanceled() const noexcept
  1311. {
  1312. return pData->actionCanceled;
  1313. }
  1314. // -----------------------------------------------------------------------
  1315. // Global options
  1316. void CarlaEngine::setOption(const EngineOption option, const int value, const char* const valueStr) noexcept
  1317. {
  1318. carla_debug("CarlaEngine::setOption(%i:%s, %i, \"%s\")", option, EngineOption2Str(option), value, valueStr);
  1319. if (isRunning())
  1320. {
  1321. switch (option)
  1322. {
  1323. case ENGINE_OPTION_PROCESS_MODE:
  1324. case ENGINE_OPTION_AUDIO_TRIPLE_BUFFER:
  1325. case ENGINE_OPTION_AUDIO_DRIVER:
  1326. case ENGINE_OPTION_AUDIO_DEVICE:
  1327. return carla_stderr("CarlaEngine::setOption(%i:%s, %i, \"%s\") - Cannot set this option while engine is running!",
  1328. option, EngineOption2Str(option), value, valueStr);
  1329. default:
  1330. break;
  1331. }
  1332. }
  1333. // do not un-force stereo for rack mode
  1334. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK && option == ENGINE_OPTION_FORCE_STEREO && value != 0)
  1335. return;
  1336. switch (option)
  1337. {
  1338. case ENGINE_OPTION_DEBUG:
  1339. break;
  1340. case ENGINE_OPTION_PROCESS_MODE:
  1341. CARLA_SAFE_ASSERT_RETURN(value >= ENGINE_PROCESS_MODE_SINGLE_CLIENT && value <= ENGINE_PROCESS_MODE_BRIDGE,);
  1342. pData->options.processMode = static_cast<EngineProcessMode>(value);
  1343. break;
  1344. case ENGINE_OPTION_TRANSPORT_MODE:
  1345. CARLA_SAFE_ASSERT_RETURN(value >= ENGINE_TRANSPORT_MODE_DISABLED && value <= ENGINE_TRANSPORT_MODE_BRIDGE,);
  1346. CARLA_SAFE_ASSERT_RETURN(getType() == kEngineTypeJack || value != ENGINE_TRANSPORT_MODE_JACK,);
  1347. pData->options.transportMode = static_cast<EngineTransportMode>(value);
  1348. delete[] pData->options.transportExtra;
  1349. if (value >= ENGINE_TRANSPORT_MODE_DISABLED && valueStr != nullptr)
  1350. pData->options.transportExtra = carla_strdup_safe(valueStr);
  1351. else
  1352. pData->options.transportExtra = nullptr;
  1353. pData->time.setNeedsReset();
  1354. #if defined(HAVE_HYLIA) && !defined(BUILD_BRIDGE)
  1355. // enable link now if needed
  1356. {
  1357. const bool linkEnabled = pData->options.transportExtra != nullptr && std::strstr(pData->options.transportExtra, ":link:") != nullptr;
  1358. pData->time.enableLink(linkEnabled);
  1359. }
  1360. #endif
  1361. break;
  1362. case ENGINE_OPTION_FORCE_STEREO:
  1363. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  1364. pData->options.forceStereo = (value != 0);
  1365. break;
  1366. case ENGINE_OPTION_PREFER_PLUGIN_BRIDGES:
  1367. #ifdef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1368. CARLA_SAFE_ASSERT_RETURN(value == 0,);
  1369. #else
  1370. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  1371. #endif
  1372. pData->options.preferPluginBridges = (value != 0);
  1373. break;
  1374. case ENGINE_OPTION_PREFER_UI_BRIDGES:
  1375. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  1376. pData->options.preferUiBridges = (value != 0);
  1377. break;
  1378. case ENGINE_OPTION_UIS_ALWAYS_ON_TOP:
  1379. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  1380. pData->options.uisAlwaysOnTop = (value != 0);
  1381. break;
  1382. case ENGINE_OPTION_MAX_PARAMETERS:
  1383. CARLA_SAFE_ASSERT_RETURN(value >= 0,);
  1384. pData->options.maxParameters = static_cast<uint>(value);
  1385. break;
  1386. case ENGINE_OPTION_RESET_XRUNS:
  1387. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  1388. pData->options.resetXruns = (value != 0);
  1389. break;
  1390. case ENGINE_OPTION_UI_BRIDGES_TIMEOUT:
  1391. CARLA_SAFE_ASSERT_RETURN(value >= 0,);
  1392. pData->options.uiBridgesTimeout = static_cast<uint>(value);
  1393. break;
  1394. case ENGINE_OPTION_AUDIO_BUFFER_SIZE:
  1395. CARLA_SAFE_ASSERT_RETURN(value >= 8,);
  1396. pData->options.audioBufferSize = static_cast<uint>(value);
  1397. break;
  1398. case ENGINE_OPTION_AUDIO_SAMPLE_RATE:
  1399. CARLA_SAFE_ASSERT_RETURN(value >= 22050,);
  1400. pData->options.audioSampleRate = static_cast<uint>(value);
  1401. break;
  1402. case ENGINE_OPTION_AUDIO_TRIPLE_BUFFER:
  1403. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  1404. pData->options.audioTripleBuffer = (value != 0);
  1405. break;
  1406. case ENGINE_OPTION_AUDIO_DRIVER:
  1407. CARLA_SAFE_ASSERT_RETURN(valueStr != nullptr,);
  1408. if (pData->options.audioDriver != nullptr)
  1409. delete[] pData->options.audioDriver;
  1410. pData->options.audioDriver = carla_strdup_safe(valueStr);
  1411. break;
  1412. case ENGINE_OPTION_AUDIO_DEVICE:
  1413. CARLA_SAFE_ASSERT_RETURN(valueStr != nullptr,);
  1414. if (pData->options.audioDevice != nullptr)
  1415. delete[] pData->options.audioDevice;
  1416. pData->options.audioDevice = carla_strdup_safe(valueStr);
  1417. break;
  1418. case ENGINE_OPTION_OSC_ENABLED:
  1419. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  1420. #ifndef BUILD_BRIDGE
  1421. pData->options.oscEnabled = (value != 0);
  1422. #endif
  1423. break;
  1424. case ENGINE_OPTION_OSC_PORT_TCP:
  1425. CARLA_SAFE_ASSERT_RETURN(value <= 0 || value >= 1024,);
  1426. #ifndef BUILD_BRIDGE
  1427. pData->options.oscPortTCP = value;
  1428. #endif
  1429. break;
  1430. case ENGINE_OPTION_OSC_PORT_UDP:
  1431. CARLA_SAFE_ASSERT_RETURN(value <= 0 || value >= 1024,);
  1432. #ifndef BUILD_BRIDGE
  1433. pData->options.oscPortUDP = value;
  1434. #endif
  1435. break;
  1436. case ENGINE_OPTION_FILE_PATH:
  1437. CARLA_SAFE_ASSERT_RETURN(value > FILE_NONE,);
  1438. CARLA_SAFE_ASSERT_RETURN(value <= FILE_MIDI,);
  1439. switch (value)
  1440. {
  1441. case FILE_AUDIO:
  1442. if (pData->options.pathAudio != nullptr)
  1443. delete[] pData->options.pathAudio;
  1444. if (valueStr != nullptr)
  1445. pData->options.pathAudio = carla_strdup_safe(valueStr);
  1446. else
  1447. pData->options.pathAudio = nullptr;
  1448. break;
  1449. case FILE_MIDI:
  1450. if (pData->options.pathMIDI != nullptr)
  1451. delete[] pData->options.pathMIDI;
  1452. if (valueStr != nullptr)
  1453. pData->options.pathMIDI = carla_strdup_safe(valueStr);
  1454. else
  1455. pData->options.pathMIDI = nullptr;
  1456. break;
  1457. default:
  1458. return carla_stderr("CarlaEngine::setOption(%i:%s, %i, \"%s\") - Invalid file type",
  1459. option, EngineOption2Str(option), value, valueStr);
  1460. break;
  1461. }
  1462. break;
  1463. case ENGINE_OPTION_PLUGIN_PATH:
  1464. CARLA_SAFE_ASSERT_RETURN(value > PLUGIN_NONE,);
  1465. CARLA_SAFE_ASSERT_RETURN(value <= PLUGIN_SFZ,);
  1466. switch (value)
  1467. {
  1468. case PLUGIN_LADSPA:
  1469. if (pData->options.pathLADSPA != nullptr)
  1470. delete[] pData->options.pathLADSPA;
  1471. if (valueStr != nullptr)
  1472. pData->options.pathLADSPA = carla_strdup_safe(valueStr);
  1473. else
  1474. pData->options.pathLADSPA = nullptr;
  1475. break;
  1476. case PLUGIN_DSSI:
  1477. if (pData->options.pathDSSI != nullptr)
  1478. delete[] pData->options.pathDSSI;
  1479. if (valueStr != nullptr)
  1480. pData->options.pathDSSI = carla_strdup_safe(valueStr);
  1481. else
  1482. pData->options.pathDSSI = nullptr;
  1483. break;
  1484. case PLUGIN_LV2:
  1485. if (pData->options.pathLV2 != nullptr)
  1486. delete[] pData->options.pathLV2;
  1487. if (valueStr != nullptr)
  1488. pData->options.pathLV2 = carla_strdup_safe(valueStr);
  1489. else
  1490. pData->options.pathLV2 = nullptr;
  1491. break;
  1492. case PLUGIN_VST2:
  1493. if (pData->options.pathVST2 != nullptr)
  1494. delete[] pData->options.pathVST2;
  1495. if (valueStr != nullptr)
  1496. pData->options.pathVST2 = carla_strdup_safe(valueStr);
  1497. else
  1498. pData->options.pathVST2 = nullptr;
  1499. break;
  1500. case PLUGIN_VST3:
  1501. if (pData->options.pathVST3 != nullptr)
  1502. delete[] pData->options.pathVST3;
  1503. if (valueStr != nullptr)
  1504. pData->options.pathVST3 = carla_strdup_safe(valueStr);
  1505. else
  1506. pData->options.pathVST3 = nullptr;
  1507. break;
  1508. case PLUGIN_SF2:
  1509. if (pData->options.pathSF2 != nullptr)
  1510. delete[] pData->options.pathSF2;
  1511. if (valueStr != nullptr)
  1512. pData->options.pathSF2 = carla_strdup_safe(valueStr);
  1513. else
  1514. pData->options.pathSF2 = nullptr;
  1515. break;
  1516. case PLUGIN_SFZ:
  1517. if (pData->options.pathSFZ != nullptr)
  1518. delete[] pData->options.pathSFZ;
  1519. if (valueStr != nullptr)
  1520. pData->options.pathSFZ = carla_strdup_safe(valueStr);
  1521. else
  1522. pData->options.pathSFZ = nullptr;
  1523. break;
  1524. default:
  1525. return carla_stderr("CarlaEngine::setOption(%i:%s, %i, \"%s\") - Invalid plugin type",
  1526. option, EngineOption2Str(option), value, valueStr);
  1527. break;
  1528. }
  1529. break;
  1530. case ENGINE_OPTION_PATH_BINARIES:
  1531. CARLA_SAFE_ASSERT_RETURN(valueStr != nullptr && valueStr[0] != '\0',);
  1532. if (pData->options.binaryDir != nullptr)
  1533. delete[] pData->options.binaryDir;
  1534. pData->options.binaryDir = carla_strdup_safe(valueStr);
  1535. break;
  1536. case ENGINE_OPTION_PATH_RESOURCES:
  1537. CARLA_SAFE_ASSERT_RETURN(valueStr != nullptr && valueStr[0] != '\0',);
  1538. if (pData->options.resourceDir != nullptr)
  1539. delete[] pData->options.resourceDir;
  1540. pData->options.resourceDir = carla_strdup_safe(valueStr);
  1541. break;
  1542. case ENGINE_OPTION_PREVENT_BAD_BEHAVIOUR: {
  1543. CARLA_SAFE_ASSERT_RETURN(pData->options.binaryDir != nullptr && pData->options.binaryDir[0] != '\0',);
  1544. #ifdef CARLA_OS_LINUX
  1545. const ScopedEngineEnvironmentLocker _seel(this);
  1546. if (value != 0)
  1547. {
  1548. CarlaString interposerPath(CarlaString(pData->options.binaryDir) + "/libcarla_interposer-safe.so");
  1549. ::setenv("LD_PRELOAD", interposerPath.buffer(), 1);
  1550. }
  1551. else
  1552. {
  1553. ::unsetenv("LD_PRELOAD");
  1554. }
  1555. #endif
  1556. } break;
  1557. case ENGINE_OPTION_FRONTEND_BACKGROUND_COLOR:
  1558. pData->options.bgColor = static_cast<uint>(value);
  1559. break;
  1560. case ENGINE_OPTION_FRONTEND_FOREGROUND_COLOR:
  1561. pData->options.fgColor = static_cast<uint>(value);
  1562. break;
  1563. case ENGINE_OPTION_FRONTEND_UI_SCALE:
  1564. CARLA_SAFE_ASSERT_RETURN(value > 0,);
  1565. pData->options.uiScale = static_cast<float>(value) / 1000;
  1566. break;
  1567. case ENGINE_OPTION_FRONTEND_WIN_ID: {
  1568. CARLA_SAFE_ASSERT_RETURN(valueStr != nullptr && valueStr[0] != '\0',);
  1569. const long long winId(std::strtoll(valueStr, nullptr, 16));
  1570. CARLA_SAFE_ASSERT_RETURN(winId >= 0,);
  1571. pData->options.frontendWinId = static_cast<uintptr_t>(winId);
  1572. } break;
  1573. #if !defined(BUILD_BRIDGE_ALTERNATIVE_ARCH) && !defined(CARLA_OS_WIN)
  1574. case ENGINE_OPTION_WINE_EXECUTABLE:
  1575. CARLA_SAFE_ASSERT_RETURN(valueStr != nullptr && valueStr[0] != '\0',);
  1576. if (pData->options.wine.executable != nullptr)
  1577. delete[] pData->options.wine.executable;
  1578. pData->options.wine.executable = carla_strdup_safe(valueStr);
  1579. break;
  1580. case ENGINE_OPTION_WINE_AUTO_PREFIX:
  1581. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  1582. pData->options.wine.autoPrefix = (value != 0);
  1583. break;
  1584. case ENGINE_OPTION_WINE_FALLBACK_PREFIX:
  1585. CARLA_SAFE_ASSERT_RETURN(valueStr != nullptr && valueStr[0] != '\0',);
  1586. if (pData->options.wine.fallbackPrefix != nullptr)
  1587. delete[] pData->options.wine.fallbackPrefix;
  1588. pData->options.wine.fallbackPrefix = carla_strdup_safe(valueStr);
  1589. break;
  1590. case ENGINE_OPTION_WINE_RT_PRIO_ENABLED:
  1591. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  1592. pData->options.wine.rtPrio = (value != 0);
  1593. break;
  1594. case ENGINE_OPTION_WINE_BASE_RT_PRIO:
  1595. CARLA_SAFE_ASSERT_RETURN(value >= 1 && value <= 89,);
  1596. pData->options.wine.baseRtPrio = value;
  1597. break;
  1598. case ENGINE_OPTION_WINE_SERVER_RT_PRIO:
  1599. CARLA_SAFE_ASSERT_RETURN(value >= 1 && value <= 99,);
  1600. pData->options.wine.serverRtPrio = value;
  1601. break;
  1602. #endif
  1603. case ENGINE_OPTION_DEBUG_CONSOLE_OUTPUT:
  1604. break;
  1605. }
  1606. }
  1607. #ifndef BUILD_BRIDGE
  1608. // -----------------------------------------------------------------------
  1609. // OSC Stuff
  1610. bool CarlaEngine::isOscControlRegistered() const noexcept
  1611. {
  1612. # ifdef HAVE_LIBLO
  1613. return pData->osc.isControlRegisteredForTCP();
  1614. # else
  1615. return false;
  1616. # endif
  1617. }
  1618. const char* CarlaEngine::getOscServerPathTCP() const noexcept
  1619. {
  1620. # ifdef HAVE_LIBLO
  1621. return pData->osc.getServerPathTCP();
  1622. # else
  1623. return nullptr;
  1624. # endif
  1625. }
  1626. const char* CarlaEngine::getOscServerPathUDP() const noexcept
  1627. {
  1628. # ifdef HAVE_LIBLO
  1629. return pData->osc.getServerPathUDP();
  1630. # else
  1631. return nullptr;
  1632. # endif
  1633. }
  1634. #endif
  1635. // -----------------------------------------------------------------------
  1636. // Internal stuff
  1637. void CarlaEngine::bufferSizeChanged(const uint32_t newBufferSize)
  1638. {
  1639. carla_debug("CarlaEngine::bufferSizeChanged(%i)", newBufferSize);
  1640. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1641. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK ||
  1642. pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  1643. {
  1644. pData->graph.setBufferSize(newBufferSize);
  1645. }
  1646. #endif
  1647. pData->time.updateAudioValues(newBufferSize, pData->sampleRate);
  1648. for (uint i=0; i < pData->curPluginCount; ++i)
  1649. {
  1650. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1651. if (plugin != nullptr && plugin->isEnabled())
  1652. {
  1653. plugin->tryLock(true);
  1654. plugin->bufferSizeChanged(newBufferSize);
  1655. plugin->unlock();
  1656. }
  1657. }
  1658. callback(true, true, ENGINE_CALLBACK_BUFFER_SIZE_CHANGED, 0, static_cast<int>(newBufferSize), 0, 0, 0.0f, nullptr);
  1659. }
  1660. void CarlaEngine::sampleRateChanged(const double newSampleRate)
  1661. {
  1662. carla_debug("CarlaEngine::sampleRateChanged(%g)", newSampleRate);
  1663. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1664. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK ||
  1665. pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  1666. {
  1667. pData->graph.setSampleRate(newSampleRate);
  1668. }
  1669. #endif
  1670. pData->time.updateAudioValues(pData->bufferSize, newSampleRate);
  1671. for (uint i=0; i < pData->curPluginCount; ++i)
  1672. {
  1673. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1674. if (plugin != nullptr && plugin->isEnabled())
  1675. {
  1676. plugin->tryLock(true);
  1677. plugin->sampleRateChanged(newSampleRate);
  1678. plugin->unlock();
  1679. }
  1680. }
  1681. callback(true, true, ENGINE_CALLBACK_SAMPLE_RATE_CHANGED, 0, 0, 0, 0, static_cast<float>(newSampleRate), nullptr);
  1682. }
  1683. void CarlaEngine::offlineModeChanged(const bool isOfflineNow)
  1684. {
  1685. carla_debug("CarlaEngine::offlineModeChanged(%s)", bool2str(isOfflineNow));
  1686. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1687. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK ||
  1688. pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  1689. {
  1690. pData->graph.setOffline(isOfflineNow);
  1691. }
  1692. #endif
  1693. for (uint i=0; i < pData->curPluginCount; ++i)
  1694. {
  1695. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1696. if (plugin != nullptr && plugin->isEnabled())
  1697. plugin->offlineModeChanged(isOfflineNow);
  1698. }
  1699. }
  1700. void CarlaEngine::setPluginPeaksRT(const uint pluginId, float const inPeaks[2], float const outPeaks[2]) noexcept
  1701. {
  1702. EnginePluginData& pluginData(pData->plugins[pluginId]);
  1703. pluginData.peaks[0] = inPeaks[0];
  1704. pluginData.peaks[1] = inPeaks[1];
  1705. pluginData.peaks[2] = outPeaks[0];
  1706. pluginData.peaks[3] = outPeaks[1];
  1707. }
  1708. void CarlaEngine::saveProjectInternal(water::MemoryOutputStream& outStream) const
  1709. {
  1710. // send initial prepareForSave first, giving time for bridges to act
  1711. for (uint i=0; i < pData->curPluginCount; ++i)
  1712. {
  1713. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1714. if (plugin != nullptr && plugin->isEnabled())
  1715. {
  1716. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1717. // deactivate bridge client-side ping check, since some plugins block during save
  1718. if (plugin->getHints() & PLUGIN_IS_BRIDGE)
  1719. plugin->setCustomData(CUSTOM_DATA_TYPE_STRING, "__CarlaPingOnOff__", "false", false);
  1720. #endif
  1721. plugin->prepareForSave();
  1722. }
  1723. }
  1724. outStream << "<?xml version='1.0' encoding='UTF-8'?>\n";
  1725. outStream << "<!DOCTYPE CARLA-PROJECT>\n";
  1726. outStream << "<CARLA-PROJECT VERSION='2.0'>\n";
  1727. const bool isPlugin(getType() == kEngineTypePlugin);
  1728. const EngineOptions& options(pData->options);
  1729. {
  1730. MemoryOutputStream outSettings(1024);
  1731. outSettings << " <EngineSettings>\n";
  1732. outSettings << " <ForceStereo>" << bool2str(options.forceStereo) << "</ForceStereo>\n";
  1733. outSettings << " <PreferPluginBridges>" << bool2str(options.preferPluginBridges) << "</PreferPluginBridges>\n";
  1734. outSettings << " <PreferUiBridges>" << bool2str(options.preferUiBridges) << "</PreferUiBridges>\n";
  1735. outSettings << " <UIsAlwaysOnTop>" << bool2str(options.uisAlwaysOnTop) << "</UIsAlwaysOnTop>\n";
  1736. outSettings << " <MaxParameters>" << String(options.maxParameters) << "</MaxParameters>\n";
  1737. outSettings << " <UIBridgesTimeout>" << String(options.uiBridgesTimeout) << "</UIBridgesTimeout>\n";
  1738. if (isPlugin)
  1739. {
  1740. outSettings << " <LADSPA_PATH>" << xmlSafeString(options.pathLADSPA, true) << "</LADSPA_PATH>\n";
  1741. outSettings << " <DSSI_PATH>" << xmlSafeString(options.pathDSSI, true) << "</DSSI_PATH>\n";
  1742. outSettings << " <LV2_PATH>" << xmlSafeString(options.pathLV2, true) << "</LV2_PATH>\n";
  1743. outSettings << " <VST2_PATH>" << xmlSafeString(options.pathVST2, true) << "</VST2_PATH>\n";
  1744. outSettings << " <VST3_PATH>" << xmlSafeString(options.pathVST3, true) << "</VST3_PATH>\n";
  1745. outSettings << " <SF2_PATH>" << xmlSafeString(options.pathSF2, true) << "</SF2_PATH>\n";
  1746. outSettings << " <SFZ_PATH>" << xmlSafeString(options.pathSFZ, true) << "</SFZ_PATH>\n";
  1747. }
  1748. outSettings << " </EngineSettings>\n";
  1749. outStream << outSettings;
  1750. }
  1751. if (pData->timeInfo.bbt.valid && ! isPlugin)
  1752. {
  1753. MemoryOutputStream outTransport(128);
  1754. outTransport << "\n <Transport>\n";
  1755. // outTransport << " <BeatsPerBar>" << pData->timeInfo.bbt.beatsPerBar << "</BeatsPerBar>\n";
  1756. outTransport << " <BeatsPerMinute>" << pData->timeInfo.bbt.beatsPerMinute << "</BeatsPerMinute>\n";
  1757. outTransport << " </Transport>\n";
  1758. outStream << outTransport;
  1759. }
  1760. char strBuf[STR_MAX+1];
  1761. carla_zeroChars(strBuf, STR_MAX+1);
  1762. for (uint i=0; i < pData->curPluginCount; ++i)
  1763. {
  1764. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1765. if (plugin != nullptr && plugin->isEnabled())
  1766. {
  1767. MemoryOutputStream outPlugin(4096), streamPlugin;
  1768. plugin->getStateSave(false).dumpToMemoryStream(streamPlugin);
  1769. outPlugin << "\n";
  1770. if (plugin->getRealName(strBuf))
  1771. outPlugin << " <!-- " << xmlSafeString(strBuf, true) << " -->\n";
  1772. outPlugin << " <Plugin>\n";
  1773. outPlugin << streamPlugin;
  1774. outPlugin << " </Plugin>\n";
  1775. outStream << outPlugin;
  1776. }
  1777. }
  1778. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1779. // tell bridges we're done saving
  1780. for (uint i=0; i < pData->curPluginCount; ++i)
  1781. {
  1782. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1783. if (plugin != nullptr && plugin->isEnabled() && (plugin->getHints() & PLUGIN_IS_BRIDGE) != 0)
  1784. plugin->setCustomData(CUSTOM_DATA_TYPE_STRING, "__CarlaPingOnOff__", "true", false);
  1785. }
  1786. // save internal connections
  1787. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  1788. {
  1789. uint posCount = 0;
  1790. const char* const* const patchbayConns = getPatchbayConnections(false);
  1791. const PatchbayPosition* const patchbayPos = getPatchbayPositions(false, posCount);
  1792. if (patchbayConns != nullptr || patchbayPos != nullptr)
  1793. {
  1794. MemoryOutputStream outPatchbay(2048);
  1795. outPatchbay << "\n <Patchbay>\n";
  1796. if (patchbayConns != nullptr)
  1797. {
  1798. for (int i=0; patchbayConns[i] != nullptr && patchbayConns[i+1] != nullptr; ++i, ++i)
  1799. {
  1800. const char* const connSource(patchbayConns[i]);
  1801. const char* const connTarget(patchbayConns[i+1]);
  1802. CARLA_SAFE_ASSERT_CONTINUE(connSource != nullptr && connSource[0] != '\0');
  1803. CARLA_SAFE_ASSERT_CONTINUE(connTarget != nullptr && connTarget[0] != '\0');
  1804. outPatchbay << " <Connection>\n";
  1805. outPatchbay << " <Source>" << xmlSafeString(connSource, true) << "</Source>\n";
  1806. outPatchbay << " <Target>" << xmlSafeString(connTarget, true) << "</Target>\n";
  1807. outPatchbay << " </Connection>\n";
  1808. }
  1809. }
  1810. if (patchbayPos != nullptr && posCount != 0)
  1811. {
  1812. outPatchbay << " <Positions>\n";
  1813. for (uint i=0; i<posCount; ++i)
  1814. {
  1815. const PatchbayPosition& ppos(patchbayPos[i]);
  1816. CARLA_SAFE_ASSERT_CONTINUE(ppos.name != nullptr && ppos.name[0] != '\0');
  1817. outPatchbay << " <Position x1=\"" << ppos.x1 << "\" y1=\"" << ppos.y1;
  1818. if (ppos.x2 != 0 || ppos.y2 != 0)
  1819. outPatchbay << "\" x2=\"" << ppos.x2 << "\" y2=\"" << ppos.y2;
  1820. if (ppos.pluginId >= 0)
  1821. outPatchbay << "\" pluginId=\"" << ppos.pluginId;
  1822. outPatchbay << "\">\n";
  1823. outPatchbay << " <Name>" << xmlSafeString(ppos.name, true) << "</Name>\n";
  1824. outPatchbay << " </Position>\n";
  1825. if (ppos.dealloc)
  1826. delete[] ppos.name;
  1827. }
  1828. outPatchbay << " </Positions>\n";
  1829. }
  1830. outPatchbay << " </Patchbay>\n";
  1831. outStream << outPatchbay;
  1832. delete[] patchbayPos;
  1833. }
  1834. }
  1835. // if we're running inside some session-manager (and using JACK), let them handle the connections
  1836. bool saveExternalConnections, saveExternalPositions = true;
  1837. /**/ if (isPlugin)
  1838. {
  1839. saveExternalConnections = false;
  1840. saveExternalPositions = false;
  1841. }
  1842. else if (std::strcmp(getCurrentDriverName(), "JACK") != 0)
  1843. {
  1844. saveExternalConnections = true;
  1845. }
  1846. else if (std::getenv("CARLA_DONT_MANAGE_CONNECTIONS") != nullptr)
  1847. {
  1848. saveExternalConnections = false;
  1849. }
  1850. else if (std::getenv("LADISH_APP_NAME") != nullptr)
  1851. {
  1852. saveExternalConnections = false;
  1853. }
  1854. else if (std::getenv("NSM_URL") != nullptr)
  1855. {
  1856. saveExternalConnections = false;
  1857. }
  1858. else
  1859. {
  1860. saveExternalConnections = true;
  1861. }
  1862. if (saveExternalConnections || saveExternalPositions)
  1863. {
  1864. uint posCount = 0;
  1865. const char* const* const patchbayConns = saveExternalConnections
  1866. ? getPatchbayConnections(true)
  1867. : nullptr;
  1868. const PatchbayPosition* const patchbayPos = saveExternalPositions
  1869. ? getPatchbayPositions(true, posCount)
  1870. : nullptr;
  1871. if (patchbayConns != nullptr || patchbayPos != nullptr)
  1872. {
  1873. MemoryOutputStream outPatchbay(2048);
  1874. outPatchbay << "\n <ExternalPatchbay>\n";
  1875. if (patchbayConns != nullptr)
  1876. {
  1877. for (int i=0; patchbayConns[i] != nullptr && patchbayConns[i+1] != nullptr; ++i, ++i )
  1878. {
  1879. const char* const connSource(patchbayConns[i]);
  1880. const char* const connTarget(patchbayConns[i+1]);
  1881. CARLA_SAFE_ASSERT_CONTINUE(connSource != nullptr && connSource[0] != '\0');
  1882. CARLA_SAFE_ASSERT_CONTINUE(connTarget != nullptr && connTarget[0] != '\0');
  1883. outPatchbay << " <Connection>\n";
  1884. outPatchbay << " <Source>" << xmlSafeString(connSource, true) << "</Source>\n";
  1885. outPatchbay << " <Target>" << xmlSafeString(connTarget, true) << "</Target>\n";
  1886. outPatchbay << " </Connection>\n";
  1887. }
  1888. }
  1889. if (patchbayPos != nullptr && posCount != 0)
  1890. {
  1891. outPatchbay << " <Positions>\n";
  1892. for (uint i=0; i<posCount; ++i)
  1893. {
  1894. const PatchbayPosition& ppos(patchbayPos[i]);
  1895. CARLA_SAFE_ASSERT_CONTINUE(ppos.name != nullptr && ppos.name[0] != '\0');
  1896. outPatchbay << " <Position x1=\"" << ppos.x1 << "\" y1=\"" << ppos.y1;
  1897. if (ppos.x2 != 0 || ppos.y2 != 0)
  1898. outPatchbay << "\" x2=\"" << ppos.x2 << "\" y2=\"" << ppos.y2;
  1899. if (ppos.pluginId >= 0)
  1900. outPatchbay << "\" pluginId=\"" << ppos.pluginId;
  1901. outPatchbay << "\">\n";
  1902. outPatchbay << " <Name>" << xmlSafeString(ppos.name, true) << "</Name>\n";
  1903. outPatchbay << " </Position>\n";
  1904. if (ppos.dealloc)
  1905. delete[] ppos.name;
  1906. }
  1907. outPatchbay << " </Positions>\n";
  1908. }
  1909. outPatchbay << " </ExternalPatchbay>\n";
  1910. outStream << outPatchbay;
  1911. }
  1912. }
  1913. #endif
  1914. outStream << "</CARLA-PROJECT>\n";
  1915. }
  1916. static String findBinaryInCustomPath(const char* const searchPath, const char* const binary)
  1917. {
  1918. const StringArray searchPaths(StringArray::fromTokens(searchPath, CARLA_OS_SPLIT_STR, ""));
  1919. // try direct filename first
  1920. String jbinary(binary);
  1921. // adjust for current platform
  1922. #ifdef CARLA_OS_WIN
  1923. if (jbinary[0] == '/')
  1924. jbinary = "C:" + jbinary.replaceCharacter('/', '\\');
  1925. #else
  1926. if (jbinary[1] == ':' && (jbinary[2] == '\\' || jbinary[2] == '/'))
  1927. jbinary = jbinary.substring(2).replaceCharacter('\\', '/');
  1928. #endif
  1929. String filename = File(jbinary).getFileName();
  1930. int searchFlags = File::findFiles|File::ignoreHiddenFiles;
  1931. #ifdef CARLA_OS_MAC
  1932. if (filename.endsWithIgnoreCase(".vst") || filename.endsWithIgnoreCase(".vst3"))
  1933. searchFlags |= File::findDirectories;
  1934. #endif
  1935. Array<File> results;
  1936. for (const String *it=searchPaths.begin(), *end=searchPaths.end(); it != end; ++it)
  1937. {
  1938. const File path(*it);
  1939. results.clear();
  1940. path.findChildFiles(results, searchFlags, true, filename);
  1941. if (results.size() > 0)
  1942. return results.getFirst().getFullPathName();
  1943. }
  1944. // try changing extension
  1945. #if defined(CARLA_OS_MAC)
  1946. if (filename.endsWithIgnoreCase(".dll") || filename.endsWithIgnoreCase(".so"))
  1947. filename = File(jbinary).getFileNameWithoutExtension() + ".dylib";
  1948. #elif defined(CARLA_OS_WIN)
  1949. if (filename.endsWithIgnoreCase(".dylib") || filename.endsWithIgnoreCase(".so"))
  1950. filename = File(jbinary).getFileNameWithoutExtension() + ".dll";
  1951. #else
  1952. if (filename.endsWithIgnoreCase(".dll") || filename.endsWithIgnoreCase(".dylib"))
  1953. filename = File(jbinary).getFileNameWithoutExtension() + ".so";
  1954. #endif
  1955. else
  1956. return String();
  1957. for (const String *it=searchPaths.begin(), *end=searchPaths.end(); it != end; ++it)
  1958. {
  1959. const File path(*it);
  1960. results.clear();
  1961. path.findChildFiles(results, searchFlags, true, filename);
  1962. if (results.size() > 0)
  1963. return results.getFirst().getFullPathName();
  1964. }
  1965. return String();
  1966. }
  1967. bool CarlaEngine::loadProjectInternal(water::XmlDocument& xmlDoc)
  1968. {
  1969. CarlaScopedPointer<XmlElement> xmlElement(xmlDoc.getDocumentElement(true));
  1970. CARLA_SAFE_ASSERT_RETURN_ERR(xmlElement != nullptr, "Failed to parse project file");
  1971. const String& xmlType(xmlElement->getTagName());
  1972. const bool isPreset(xmlType.equalsIgnoreCase("carla-preset"));
  1973. if (! (xmlType.equalsIgnoreCase("carla-project") || isPreset))
  1974. {
  1975. callback(true, true, ENGINE_CALLBACK_PROJECT_LOAD_FINISHED, 0, 0, 0, 0, 0.0f, nullptr);
  1976. setLastError("Not a valid Carla project or preset file");
  1977. return false;
  1978. }
  1979. pData->actionCanceled = false;
  1980. callback(true, true, ENGINE_CALLBACK_CANCELABLE_ACTION, 0, 1, 0, 0, 0.0f, "Loading project");
  1981. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1982. const CarlaScopedValueSetter<bool> csvs(pData->loadingProject, true, false);
  1983. #endif
  1984. // completely load file
  1985. xmlElement = xmlDoc.getDocumentElement(false);
  1986. CARLA_SAFE_ASSERT_RETURN_ERR(xmlElement != nullptr, "Failed to completely parse project file");
  1987. callback(true, false, ENGINE_CALLBACK_IDLE, 0, 0, 0, 0, 0.0f, nullptr);
  1988. if (pData->aboutToClose)
  1989. return true;
  1990. if (pData->actionCanceled)
  1991. {
  1992. setLastError("Project load canceled");
  1993. return false;
  1994. }
  1995. const bool isPlugin(getType() == kEngineTypePlugin);
  1996. // load engine settings first of all
  1997. if (XmlElement* const elem = isPreset ? nullptr : xmlElement->getChildByName("EngineSettings"))
  1998. {
  1999. for (XmlElement* settElem = elem->getFirstChildElement(); settElem != nullptr; settElem = settElem->getNextElement())
  2000. {
  2001. const String& tag(settElem->getTagName());
  2002. const String text(settElem->getAllSubText().trim());
  2003. /** some settings might be incorrect or require extra work,
  2004. so we call setOption rather than modifying them direly */
  2005. int option = -1;
  2006. int value = 0;
  2007. const char* valueStr = nullptr;
  2008. /**/ if (tag == "ForceStereo")
  2009. {
  2010. option = ENGINE_OPTION_FORCE_STEREO;
  2011. value = text == "true" ? 1 : 0;
  2012. }
  2013. else if (tag == "PreferPluginBridges")
  2014. {
  2015. option = ENGINE_OPTION_PREFER_PLUGIN_BRIDGES;
  2016. value = text == "true" ? 1 : 0;
  2017. }
  2018. else if (tag == "PreferUiBridges")
  2019. {
  2020. option = ENGINE_OPTION_PREFER_UI_BRIDGES;
  2021. value = text == "true" ? 1 : 0;
  2022. }
  2023. else if (tag == "UIsAlwaysOnTop")
  2024. {
  2025. option = ENGINE_OPTION_UIS_ALWAYS_ON_TOP;
  2026. value = text == "true" ? 1 : 0;
  2027. }
  2028. else if (tag == "MaxParameters")
  2029. {
  2030. option = ENGINE_OPTION_MAX_PARAMETERS;
  2031. value = text.getIntValue();
  2032. }
  2033. else if (tag == "UIBridgesTimeout")
  2034. {
  2035. option = ENGINE_OPTION_UI_BRIDGES_TIMEOUT;
  2036. value = text.getIntValue();
  2037. }
  2038. else if (isPlugin)
  2039. {
  2040. /**/ if (tag == "LADSPA_PATH")
  2041. {
  2042. option = ENGINE_OPTION_PLUGIN_PATH;
  2043. value = PLUGIN_LADSPA;
  2044. valueStr = text.toRawUTF8();
  2045. }
  2046. else if (tag == "DSSI_PATH")
  2047. {
  2048. option = ENGINE_OPTION_PLUGIN_PATH;
  2049. value = PLUGIN_DSSI;
  2050. valueStr = text.toRawUTF8();
  2051. }
  2052. else if (tag == "LV2_PATH")
  2053. {
  2054. option = ENGINE_OPTION_PLUGIN_PATH;
  2055. value = PLUGIN_LV2;
  2056. valueStr = text.toRawUTF8();
  2057. }
  2058. else if (tag == "VST2_PATH")
  2059. {
  2060. option = ENGINE_OPTION_PLUGIN_PATH;
  2061. value = PLUGIN_VST2;
  2062. valueStr = text.toRawUTF8();
  2063. }
  2064. else if (tag.equalsIgnoreCase("VST3_PATH"))
  2065. {
  2066. option = ENGINE_OPTION_PLUGIN_PATH;
  2067. value = PLUGIN_VST3;
  2068. valueStr = text.toRawUTF8();
  2069. }
  2070. else if (tag == "SF2_PATH")
  2071. {
  2072. option = ENGINE_OPTION_PLUGIN_PATH;
  2073. value = PLUGIN_SF2;
  2074. valueStr = text.toRawUTF8();
  2075. }
  2076. else if (tag == "SFZ_PATH")
  2077. {
  2078. option = ENGINE_OPTION_PLUGIN_PATH;
  2079. value = PLUGIN_SFZ;
  2080. valueStr = text.toRawUTF8();
  2081. }
  2082. }
  2083. if (option == -1)
  2084. {
  2085. // check old stuff, unhandled now
  2086. if (tag == "GIG_PATH")
  2087. continue;
  2088. // ignored tags
  2089. if (tag == "LADSPA_PATH" || tag == "DSSI_PATH" || tag == "LV2_PATH" || tag == "VST2_PATH")
  2090. continue;
  2091. if (tag == "VST3_PATH" || tag == "AU_PATH")
  2092. continue;
  2093. if (tag == "SF2_PATH" || tag == "SFZ_PATH")
  2094. continue;
  2095. // hmm something is wrong..
  2096. carla_stderr2("CarlaEngine::loadProjectInternal() - Unhandled option '%s'", tag.toRawUTF8());
  2097. continue;
  2098. }
  2099. setOption(static_cast<EngineOption>(option), value, valueStr);
  2100. }
  2101. callback(true, true, ENGINE_CALLBACK_IDLE, 0, 0, 0, 0, 0.0f, nullptr);
  2102. if (pData->aboutToClose)
  2103. return true;
  2104. if (pData->actionCanceled)
  2105. {
  2106. setLastError("Project load canceled");
  2107. return false;
  2108. }
  2109. }
  2110. // now setup transport
  2111. if (XmlElement* const elem = (isPreset || isPlugin) ? nullptr : xmlElement->getChildByName("Transport"))
  2112. {
  2113. if (XmlElement* const bpmElem = elem->getChildByName("BeatsPerMinute"))
  2114. {
  2115. const String bpmText(bpmElem->getAllSubText().trim());
  2116. const double bpm = bpmText.getDoubleValue();
  2117. // some sane limits
  2118. if (bpm >= 20.0 && bpm < 400.0)
  2119. pData->time.setBPM(bpm);
  2120. callback(true, true, ENGINE_CALLBACK_IDLE, 0, 0, 0, 0, 0.0f, nullptr);
  2121. if (pData->aboutToClose)
  2122. return true;
  2123. if (pData->actionCanceled)
  2124. {
  2125. setLastError("Project load canceled");
  2126. return false;
  2127. }
  2128. }
  2129. }
  2130. // and we handle plugins
  2131. for (XmlElement* elem = xmlElement->getFirstChildElement(); elem != nullptr; elem = elem->getNextElement())
  2132. {
  2133. const String& tagName(elem->getTagName());
  2134. if (isPreset || tagName == "Plugin")
  2135. {
  2136. CarlaStateSave stateSave;
  2137. stateSave.fillFromXmlElement(isPreset ? xmlElement.get() : elem);
  2138. callback(true, true, ENGINE_CALLBACK_IDLE, 0, 0, 0, 0, 0.0f, nullptr);
  2139. if (pData->aboutToClose)
  2140. return true;
  2141. if (pData->actionCanceled)
  2142. {
  2143. setLastError("Project load canceled");
  2144. return false;
  2145. }
  2146. CARLA_SAFE_ASSERT_CONTINUE(stateSave.type != nullptr);
  2147. #ifndef BUILD_BRIDGE
  2148. // compatibility code to load projects with GIG files
  2149. // FIXME Remove on 2.1 release
  2150. if (std::strcmp(stateSave.type, "GIG") == 0)
  2151. {
  2152. if (addPlugin(PLUGIN_LV2, "", stateSave.name, "http://linuxsampler.org/plugins/linuxsampler", 0, nullptr))
  2153. {
  2154. const uint pluginId = pData->curPluginCount;
  2155. if (CarlaPlugin* const plugin = pData->plugins[pluginId].plugin)
  2156. {
  2157. callback(true, true, ENGINE_CALLBACK_IDLE, 0, 0, 0, 0, 0.0f, nullptr);
  2158. if (pData->aboutToClose)
  2159. return true;
  2160. if (pData->actionCanceled)
  2161. {
  2162. setLastError("Project load canceled");
  2163. return false;
  2164. }
  2165. String lsState;
  2166. lsState << "0.35\n";
  2167. lsState << "18 0 Chromatic\n";
  2168. lsState << "18 1 Drum Kits\n";
  2169. lsState << "20 0\n";
  2170. lsState << "0 1 " << stateSave.binary << "\n";
  2171. lsState << "0 0 0 0 1 0 GIG\n";
  2172. plugin->setCustomData(LV2_ATOM__String, "http://linuxsampler.org/schema#state-string", lsState.toRawUTF8(), true);
  2173. plugin->restoreLV2State();
  2174. plugin->setDryWet(stateSave.dryWet, true, true);
  2175. plugin->setVolume(stateSave.volume, true, true);
  2176. plugin->setBalanceLeft(stateSave.balanceLeft, true, true);
  2177. plugin->setBalanceRight(stateSave.balanceRight, true, true);
  2178. plugin->setPanning(stateSave.panning, true, true);
  2179. plugin->setCtrlChannel(stateSave.ctrlChannel, true, true);
  2180. plugin->setActive(stateSave.active, true, true);
  2181. plugin->setEnabled(true);
  2182. ++pData->curPluginCount;
  2183. callback(true, true, ENGINE_CALLBACK_PLUGIN_ADDED, pluginId, 0, 0, 0, 0.0f, plugin->getName());
  2184. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  2185. pData->graph.addPlugin(plugin);
  2186. }
  2187. else
  2188. {
  2189. carla_stderr2("Failed to get new plugin, state will not be restored correctly\n");
  2190. }
  2191. }
  2192. else
  2193. {
  2194. carla_stderr2("Failed to load a linuxsampler LV2 plugin, GIG file won't be loaded");
  2195. }
  2196. continue;
  2197. }
  2198. #endif
  2199. const void* extraStuff = nullptr;
  2200. static const char kTrue[] = "true";
  2201. const PluginType ptype(getPluginTypeFromString(stateSave.type));
  2202. switch (ptype)
  2203. {
  2204. case PLUGIN_SF2:
  2205. if (CarlaString(stateSave.label).endsWith(" (16 outs)"))
  2206. extraStuff = kTrue;
  2207. // fall through
  2208. case PLUGIN_LADSPA:
  2209. case PLUGIN_DSSI:
  2210. case PLUGIN_VST2:
  2211. case PLUGIN_VST3:
  2212. case PLUGIN_SFZ:
  2213. if (stateSave.binary != nullptr && stateSave.binary[0] != '\0' &&
  2214. ! (File::isAbsolutePath(stateSave.binary) && File(stateSave.binary).exists()))
  2215. {
  2216. const char* searchPath;
  2217. switch (ptype)
  2218. {
  2219. case PLUGIN_LADSPA: searchPath = pData->options.pathLADSPA; break;
  2220. case PLUGIN_DSSI: searchPath = pData->options.pathDSSI; break;
  2221. case PLUGIN_VST2: searchPath = pData->options.pathVST2; break;
  2222. case PLUGIN_VST3: searchPath = pData->options.pathVST3; break;
  2223. case PLUGIN_SF2: searchPath = pData->options.pathSF2; break;
  2224. case PLUGIN_SFZ: searchPath = pData->options.pathSFZ; break;
  2225. default: searchPath = nullptr; break;
  2226. }
  2227. if (searchPath != nullptr && searchPath[0] != '\0')
  2228. {
  2229. carla_stderr("Plugin binary '%s' doesn't exist on this filesystem, let's look for it...",
  2230. stateSave.binary);
  2231. String result = findBinaryInCustomPath(searchPath, stateSave.binary);
  2232. if (result.isEmpty())
  2233. {
  2234. switch (ptype)
  2235. {
  2236. case PLUGIN_LADSPA: searchPath = std::getenv("LADSPA_PATH"); break;
  2237. case PLUGIN_DSSI: searchPath = std::getenv("DSSI_PATH"); break;
  2238. case PLUGIN_VST2: searchPath = std::getenv("VST_PATH"); break;
  2239. case PLUGIN_VST3: searchPath = std::getenv("VST3_PATH"); break;
  2240. case PLUGIN_SF2: searchPath = std::getenv("SF2_PATH"); break;
  2241. case PLUGIN_SFZ: searchPath = std::getenv("SFZ_PATH"); break;
  2242. default: searchPath = nullptr; break;
  2243. }
  2244. if (searchPath != nullptr && searchPath[0] != '\0')
  2245. result = findBinaryInCustomPath(searchPath, stateSave.binary);
  2246. }
  2247. if (result.isNotEmpty())
  2248. {
  2249. delete[] stateSave.binary;
  2250. stateSave.binary = carla_strdup(result.toRawUTF8());
  2251. carla_stderr("Found it! :)");
  2252. }
  2253. else
  2254. {
  2255. carla_stderr("Damn, we failed... :(");
  2256. }
  2257. }
  2258. }
  2259. break;
  2260. default:
  2261. break;
  2262. }
  2263. BinaryType btype;
  2264. switch (ptype)
  2265. {
  2266. case PLUGIN_LADSPA:
  2267. case PLUGIN_DSSI:
  2268. case PLUGIN_LV2:
  2269. case PLUGIN_VST2:
  2270. btype = getBinaryTypeFromFile(stateSave.binary);
  2271. break;
  2272. default:
  2273. btype = BINARY_NATIVE;
  2274. break;
  2275. }
  2276. if (addPlugin(btype, ptype, stateSave.binary,
  2277. stateSave.name, stateSave.label, stateSave.uniqueId, extraStuff, stateSave.options))
  2278. {
  2279. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  2280. const uint pluginId = pData->curPluginCount;
  2281. #else
  2282. const uint pluginId = 0;
  2283. #endif
  2284. if (CarlaPlugin* const plugin = pData->plugins[pluginId].plugin)
  2285. {
  2286. callback(true, true, ENGINE_CALLBACK_IDLE, 0, 0, 0, 0, 0.0f, nullptr);
  2287. if (pData->aboutToClose)
  2288. return true;
  2289. if (pData->actionCanceled)
  2290. {
  2291. setLastError("Project load canceled");
  2292. return false;
  2293. }
  2294. // deactivate bridge client-side ping check, since some plugins block during load
  2295. if ((plugin->getHints() & PLUGIN_IS_BRIDGE) != 0 && ! isPreset)
  2296. plugin->setCustomData(CUSTOM_DATA_TYPE_STRING, "__CarlaPingOnOff__", "false", false);
  2297. plugin->loadStateSave(stateSave);
  2298. /* NOTE: The following code is the same as the end of addPlugin().
  2299. * When project is loading we do not enable the plugin right away,
  2300. * as we want to load state first.
  2301. */
  2302. plugin->setEnabled(true);
  2303. ++pData->curPluginCount;
  2304. callback(true, true, ENGINE_CALLBACK_PLUGIN_ADDED, pluginId, 0, 0, 0, 0.0f, plugin->getName());
  2305. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  2306. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  2307. pData->graph.addPlugin(plugin);
  2308. #endif
  2309. }
  2310. else
  2311. {
  2312. carla_stderr2("Failed to get new plugin, state will not be restored correctly\n");
  2313. }
  2314. }
  2315. else
  2316. {
  2317. carla_stderr2("Failed to load a plugin '%s', error was:\n%s", stateSave.name, getLastError());
  2318. }
  2319. }
  2320. if (isPreset)
  2321. {
  2322. callback(true, true, ENGINE_CALLBACK_PROJECT_LOAD_FINISHED, 0, 0, 0, 0, 0.0f, nullptr);
  2323. callback(true, true, ENGINE_CALLBACK_CANCELABLE_ACTION, 0, 0, 0, 0, 0.0f, "Loading project");
  2324. return true;
  2325. }
  2326. }
  2327. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  2328. // tell bridges we're done loading
  2329. for (uint i=0; i < pData->curPluginCount; ++i)
  2330. {
  2331. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  2332. if (plugin != nullptr && plugin->isEnabled() && (plugin->getHints() & PLUGIN_IS_BRIDGE) != 0)
  2333. plugin->setCustomData(CUSTOM_DATA_TYPE_STRING, "__CarlaPingOnOff__", "true", false);
  2334. }
  2335. callback(true, true, ENGINE_CALLBACK_IDLE, 0, 0, 0, 0, 0.0f, nullptr);
  2336. if (pData->aboutToClose)
  2337. return true;
  2338. if (pData->actionCanceled)
  2339. {
  2340. setLastError("Project load canceled");
  2341. return false;
  2342. }
  2343. bool hasInternalConnections = false;
  2344. // and now we handle connections (internal)
  2345. if (XmlElement* const elem = xmlElement->getChildByName("Patchbay"))
  2346. {
  2347. hasInternalConnections = true;
  2348. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  2349. {
  2350. CarlaString sourcePort, targetPort;
  2351. for (XmlElement* patchElem = elem->getFirstChildElement(); patchElem != nullptr; patchElem = patchElem->getNextElement())
  2352. {
  2353. const String& patchTag(patchElem->getTagName());
  2354. if (patchTag != "Connection")
  2355. continue;
  2356. sourcePort.clear();
  2357. targetPort.clear();
  2358. for (XmlElement* connElem = patchElem->getFirstChildElement(); connElem != nullptr; connElem = connElem->getNextElement())
  2359. {
  2360. const String& tag(connElem->getTagName());
  2361. const String text(connElem->getAllSubText().trim());
  2362. /**/ if (tag == "Source")
  2363. sourcePort = xmlSafeString(text, false).toRawUTF8();
  2364. else if (tag == "Target")
  2365. targetPort = xmlSafeString(text, false).toRawUTF8();
  2366. }
  2367. if (sourcePort.isNotEmpty() && targetPort.isNotEmpty())
  2368. restorePatchbayConnection(false, sourcePort, targetPort);
  2369. }
  2370. callback(true, true, ENGINE_CALLBACK_IDLE, 0, 0, 0, 0, 0.0f, nullptr);
  2371. if (pData->aboutToClose)
  2372. return true;
  2373. if (pData->actionCanceled)
  2374. {
  2375. setLastError("Project load canceled");
  2376. return false;
  2377. }
  2378. }
  2379. }
  2380. // if we're running inside some session-manager (and using JACK), let them handle the external connections
  2381. bool loadExternalConnections;
  2382. /**/ if (std::strcmp(getCurrentDriverName(), "JACK") != 0)
  2383. loadExternalConnections = true;
  2384. else if (std::getenv("CARLA_DONT_MANAGE_CONNECTIONS") != nullptr)
  2385. loadExternalConnections = false;
  2386. else if (std::getenv("LADISH_APP_NAME") != nullptr)
  2387. loadExternalConnections = false;
  2388. else if (std::getenv("NSM_URL") != nullptr)
  2389. loadExternalConnections = false;
  2390. else
  2391. loadExternalConnections = true;
  2392. // plus external connections too
  2393. if (loadExternalConnections)
  2394. {
  2395. bool loadingAsExternal = hasInternalConnections &&
  2396. (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK ||
  2397. pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY);
  2398. for (XmlElement* elem = xmlElement->getFirstChildElement(); elem != nullptr; elem = elem->getNextElement())
  2399. {
  2400. const String& tagName(elem->getTagName());
  2401. // check if we want to load patchbay-mode connections into an external (multi-client) graph
  2402. if (tagName == "Patchbay")
  2403. {
  2404. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  2405. continue;
  2406. }
  2407. // or load external patchbay connections
  2408. else if (tagName == "ExternalPatchbay")
  2409. {
  2410. loadingAsExternal = true;
  2411. }
  2412. else
  2413. {
  2414. continue;
  2415. }
  2416. CarlaString sourcePort, targetPort;
  2417. for (XmlElement* patchElem = elem->getFirstChildElement(); patchElem != nullptr; patchElem = patchElem->getNextElement())
  2418. {
  2419. const String& patchTag(patchElem->getTagName());
  2420. if (patchTag != "Connection")
  2421. continue;
  2422. sourcePort.clear();
  2423. targetPort.clear();
  2424. for (XmlElement* connElem = patchElem->getFirstChildElement(); connElem != nullptr; connElem = connElem->getNextElement())
  2425. {
  2426. const String& tag(connElem->getTagName());
  2427. const String text(connElem->getAllSubText().trim());
  2428. /**/ if (tag == "Source")
  2429. sourcePort = xmlSafeString(text, false).toRawUTF8();
  2430. else if (tag == "Target")
  2431. targetPort = xmlSafeString(text, false).toRawUTF8();
  2432. }
  2433. if (sourcePort.isNotEmpty() && targetPort.isNotEmpty())
  2434. restorePatchbayConnection(loadingAsExternal, sourcePort, targetPort);
  2435. }
  2436. break;
  2437. }
  2438. }
  2439. // finally, we handle positions
  2440. if (XmlElement* const elemPatchbay = xmlElement->getChildByName("Patchbay"))
  2441. {
  2442. if (XmlElement* const elemPositions = elemPatchbay->getChildByName("Positions"))
  2443. {
  2444. String name;
  2445. PatchbayPosition ppos = { nullptr, -1, 0, 0, 0, 0, false };
  2446. for (XmlElement* patchElem = elemPositions->getFirstChildElement(); patchElem != nullptr; patchElem = patchElem->getNextElement())
  2447. {
  2448. const String& patchTag(patchElem->getTagName());
  2449. if (patchTag != "Position")
  2450. continue;
  2451. XmlElement* const patchName = patchElem->getChildByName("Name");
  2452. CARLA_SAFE_ASSERT_CONTINUE(patchName != nullptr);
  2453. const String nameText(patchName->getAllSubText().trim());
  2454. name = xmlSafeString(nameText, false);
  2455. ppos.name = name.toRawUTF8();
  2456. ppos.x1 = patchElem->getIntAttribute("x1");
  2457. ppos.y1 = patchElem->getIntAttribute("y1");
  2458. ppos.x2 = patchElem->getIntAttribute("x2");
  2459. ppos.y2 = patchElem->getIntAttribute("y2");
  2460. ppos.pluginId = patchElem->getIntAttribute("pluginId", -1);
  2461. if (name.isNotEmpty())
  2462. restorePatchbayGroupPosition(false, ppos);
  2463. }
  2464. callback(true, true, ENGINE_CALLBACK_IDLE, 0, 0, 0, 0, 0.0f, nullptr);
  2465. if (pData->aboutToClose)
  2466. return true;
  2467. if (pData->actionCanceled)
  2468. {
  2469. setLastError("Project load canceled");
  2470. return false;
  2471. }
  2472. }
  2473. }
  2474. if (XmlElement* const elemPatchbay = xmlElement->getChildByName("ExternalPatchbay"))
  2475. {
  2476. if (XmlElement* const elemPositions = elemPatchbay->getChildByName("Positions"))
  2477. {
  2478. String name;
  2479. PatchbayPosition ppos = { nullptr, -1, 0, 0, 0, 0, false };
  2480. for (XmlElement* patchElem = elemPositions->getFirstChildElement(); patchElem != nullptr; patchElem = patchElem->getNextElement())
  2481. {
  2482. const String& patchTag(patchElem->getTagName());
  2483. if (patchTag != "Position")
  2484. continue;
  2485. XmlElement* const patchName = patchElem->getChildByName("Name");
  2486. CARLA_SAFE_ASSERT_CONTINUE(patchName != nullptr);
  2487. const String nameText(patchName->getAllSubText().trim());
  2488. name = xmlSafeString(nameText, false);
  2489. ppos.name = name.toRawUTF8();
  2490. ppos.x1 = patchElem->getIntAttribute("x1");
  2491. ppos.y1 = patchElem->getIntAttribute("y1");
  2492. ppos.x2 = patchElem->getIntAttribute("x2");
  2493. ppos.y2 = patchElem->getIntAttribute("y2");
  2494. ppos.pluginId = patchElem->getIntAttribute("pluginId", -1);
  2495. if (name.isNotEmpty())
  2496. restorePatchbayGroupPosition(true, ppos);
  2497. }
  2498. }
  2499. }
  2500. #endif
  2501. if (pData->options.resetXruns)
  2502. clearXruns();
  2503. callback(true, true, ENGINE_CALLBACK_PROJECT_LOAD_FINISHED, 0, 0, 0, 0, 0.0f, nullptr);
  2504. callback(true, true, ENGINE_CALLBACK_CANCELABLE_ACTION, 0, 0, 0, 0, 0.0f, "Loading project");
  2505. return true;
  2506. }
  2507. // -----------------------------------------------------------------------
  2508. CARLA_BACKEND_END_NAMESPACE