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.

3076 lines
102KB

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