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.

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