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.

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