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.

2840 lines
93KB

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