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.

3100 lines
103KB

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