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.

2869 lines
94KB

  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_FILE_PATH:
  1407. CARLA_SAFE_ASSERT_RETURN(value > FILE_NONE,);
  1408. CARLA_SAFE_ASSERT_RETURN(value <= FILE_MIDI,);
  1409. switch (value)
  1410. {
  1411. case FILE_AUDIO:
  1412. if (pData->options.pathAudio != nullptr)
  1413. delete[] pData->options.pathAudio;
  1414. if (valueStr != nullptr)
  1415. pData->options.pathAudio = carla_strdup_safe(valueStr);
  1416. else
  1417. pData->options.pathAudio = nullptr;
  1418. break;
  1419. case FILE_MIDI:
  1420. if (pData->options.pathMIDI != nullptr)
  1421. delete[] pData->options.pathMIDI;
  1422. if (valueStr != nullptr)
  1423. pData->options.pathMIDI = carla_strdup_safe(valueStr);
  1424. else
  1425. pData->options.pathMIDI = nullptr;
  1426. break;
  1427. default:
  1428. return carla_stderr("CarlaEngine::setOption(%i:%s, %i, \"%s\") - Invalid file type",
  1429. option, EngineOption2Str(option), value, valueStr);
  1430. break;
  1431. }
  1432. break;
  1433. case ENGINE_OPTION_PLUGIN_PATH:
  1434. CARLA_SAFE_ASSERT_RETURN(value > PLUGIN_NONE,);
  1435. CARLA_SAFE_ASSERT_RETURN(value <= PLUGIN_SFZ,);
  1436. switch (value)
  1437. {
  1438. case PLUGIN_LADSPA:
  1439. if (pData->options.pathLADSPA != nullptr)
  1440. delete[] pData->options.pathLADSPA;
  1441. if (valueStr != nullptr)
  1442. pData->options.pathLADSPA = carla_strdup_safe(valueStr);
  1443. else
  1444. pData->options.pathLADSPA = nullptr;
  1445. break;
  1446. case PLUGIN_DSSI:
  1447. if (pData->options.pathDSSI != nullptr)
  1448. delete[] pData->options.pathDSSI;
  1449. if (valueStr != nullptr)
  1450. pData->options.pathDSSI = carla_strdup_safe(valueStr);
  1451. else
  1452. pData->options.pathDSSI = nullptr;
  1453. break;
  1454. case PLUGIN_LV2:
  1455. if (pData->options.pathLV2 != nullptr)
  1456. delete[] pData->options.pathLV2;
  1457. if (valueStr != nullptr)
  1458. pData->options.pathLV2 = carla_strdup_safe(valueStr);
  1459. else
  1460. pData->options.pathLV2 = nullptr;
  1461. break;
  1462. case PLUGIN_VST2:
  1463. if (pData->options.pathVST2 != nullptr)
  1464. delete[] pData->options.pathVST2;
  1465. if (valueStr != nullptr)
  1466. pData->options.pathVST2 = carla_strdup_safe(valueStr);
  1467. else
  1468. pData->options.pathVST2 = nullptr;
  1469. break;
  1470. case PLUGIN_VST3:
  1471. if (pData->options.pathVST3 != nullptr)
  1472. delete[] pData->options.pathVST3;
  1473. if (valueStr != nullptr)
  1474. pData->options.pathVST3 = carla_strdup_safe(valueStr);
  1475. else
  1476. pData->options.pathVST3 = nullptr;
  1477. break;
  1478. case PLUGIN_SF2:
  1479. if (pData->options.pathSF2 != nullptr)
  1480. delete[] pData->options.pathSF2;
  1481. if (valueStr != nullptr)
  1482. pData->options.pathSF2 = carla_strdup_safe(valueStr);
  1483. else
  1484. pData->options.pathSF2 = nullptr;
  1485. break;
  1486. case PLUGIN_SFZ:
  1487. if (pData->options.pathSFZ != nullptr)
  1488. delete[] pData->options.pathSFZ;
  1489. if (valueStr != nullptr)
  1490. pData->options.pathSFZ = carla_strdup_safe(valueStr);
  1491. else
  1492. pData->options.pathSFZ = nullptr;
  1493. break;
  1494. default:
  1495. return carla_stderr("CarlaEngine::setOption(%i:%s, %i, \"%s\") - Invalid plugin type",
  1496. option, EngineOption2Str(option), value, valueStr);
  1497. break;
  1498. }
  1499. break;
  1500. case ENGINE_OPTION_PATH_BINARIES:
  1501. CARLA_SAFE_ASSERT_RETURN(valueStr != nullptr && valueStr[0] != '\0',);
  1502. if (pData->options.binaryDir != nullptr)
  1503. delete[] pData->options.binaryDir;
  1504. pData->options.binaryDir = carla_strdup_safe(valueStr);
  1505. break;
  1506. case ENGINE_OPTION_PATH_RESOURCES:
  1507. CARLA_SAFE_ASSERT_RETURN(valueStr != nullptr && valueStr[0] != '\0',);
  1508. if (pData->options.resourceDir != nullptr)
  1509. delete[] pData->options.resourceDir;
  1510. pData->options.resourceDir = carla_strdup_safe(valueStr);
  1511. break;
  1512. case ENGINE_OPTION_PREVENT_BAD_BEHAVIOUR: {
  1513. CARLA_SAFE_ASSERT_RETURN(pData->options.binaryDir != nullptr && pData->options.binaryDir[0] != '\0',);
  1514. #ifdef CARLA_OS_LINUX
  1515. const ScopedEngineEnvironmentLocker _seel(this);
  1516. if (value != 0)
  1517. {
  1518. CarlaString interposerPath(CarlaString(pData->options.binaryDir) + "/libcarla_interposer-safe.so");
  1519. ::setenv("LD_PRELOAD", interposerPath.buffer(), 1);
  1520. }
  1521. else
  1522. {
  1523. ::unsetenv("LD_PRELOAD");
  1524. }
  1525. #endif
  1526. } break;
  1527. case ENGINE_OPTION_FRONTEND_UI_SCALE:
  1528. CARLA_SAFE_ASSERT_RETURN(value > 0,);
  1529. pData->options.uiScale = static_cast<float>(value) / 1000;
  1530. break;
  1531. case ENGINE_OPTION_FRONTEND_WIN_ID: {
  1532. CARLA_SAFE_ASSERT_RETURN(valueStr != nullptr && valueStr[0] != '\0',);
  1533. const long long winId(std::strtoll(valueStr, nullptr, 16));
  1534. CARLA_SAFE_ASSERT_RETURN(winId >= 0,);
  1535. pData->options.frontendWinId = static_cast<uintptr_t>(winId);
  1536. } break;
  1537. #if !defined(BUILD_BRIDGE_ALTERNATIVE_ARCH) && !defined(CARLA_OS_WIN)
  1538. case ENGINE_OPTION_WINE_EXECUTABLE:
  1539. CARLA_SAFE_ASSERT_RETURN(valueStr != nullptr && valueStr[0] != '\0',);
  1540. if (pData->options.wine.executable != nullptr)
  1541. delete[] pData->options.wine.executable;
  1542. pData->options.wine.executable = carla_strdup_safe(valueStr);
  1543. break;
  1544. case ENGINE_OPTION_WINE_AUTO_PREFIX:
  1545. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  1546. pData->options.wine.autoPrefix = (value != 0);
  1547. break;
  1548. case ENGINE_OPTION_WINE_FALLBACK_PREFIX:
  1549. CARLA_SAFE_ASSERT_RETURN(valueStr != nullptr && valueStr[0] != '\0',);
  1550. if (pData->options.wine.fallbackPrefix != nullptr)
  1551. delete[] pData->options.wine.fallbackPrefix;
  1552. pData->options.wine.fallbackPrefix = carla_strdup_safe(valueStr);
  1553. break;
  1554. case ENGINE_OPTION_WINE_RT_PRIO_ENABLED:
  1555. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  1556. pData->options.wine.rtPrio = (value != 0);
  1557. break;
  1558. case ENGINE_OPTION_WINE_BASE_RT_PRIO:
  1559. CARLA_SAFE_ASSERT_RETURN(value >= 1 && value <= 89,);
  1560. pData->options.wine.baseRtPrio = value;
  1561. break;
  1562. case ENGINE_OPTION_WINE_SERVER_RT_PRIO:
  1563. CARLA_SAFE_ASSERT_RETURN(value >= 1 && value <= 99,);
  1564. pData->options.wine.serverRtPrio = value;
  1565. break;
  1566. #endif
  1567. case ENGINE_OPTION_DEBUG_CONSOLE_OUTPUT:
  1568. break;
  1569. }
  1570. }
  1571. #ifndef BUILD_BRIDGE
  1572. // -----------------------------------------------------------------------
  1573. // OSC Stuff
  1574. bool CarlaEngine::isOscControlRegistered() const noexcept
  1575. {
  1576. # ifdef HAVE_LIBLO
  1577. return pData->osc.isControlRegisteredForTCP();
  1578. # else
  1579. return false;
  1580. # endif
  1581. }
  1582. const char* CarlaEngine::getOscServerPathTCP() const noexcept
  1583. {
  1584. # ifdef HAVE_LIBLO
  1585. return pData->osc.getServerPathTCP();
  1586. # else
  1587. return nullptr;
  1588. # endif
  1589. }
  1590. const char* CarlaEngine::getOscServerPathUDP() const noexcept
  1591. {
  1592. # ifdef HAVE_LIBLO
  1593. return pData->osc.getServerPathUDP();
  1594. # else
  1595. return nullptr;
  1596. # endif
  1597. }
  1598. #endif
  1599. // -----------------------------------------------------------------------
  1600. // Helper functions
  1601. EngineEvent* CarlaEngine::getInternalEventBuffer(const bool isInput) const noexcept
  1602. {
  1603. return isInput ? pData->events.in : pData->events.out;
  1604. }
  1605. // -----------------------------------------------------------------------
  1606. // Internal stuff
  1607. void CarlaEngine::bufferSizeChanged(const uint32_t newBufferSize)
  1608. {
  1609. carla_debug("CarlaEngine::bufferSizeChanged(%i)", newBufferSize);
  1610. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1611. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK ||
  1612. pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  1613. {
  1614. pData->graph.setBufferSize(newBufferSize);
  1615. }
  1616. #endif
  1617. pData->time.updateAudioValues(newBufferSize, pData->sampleRate);
  1618. for (uint i=0; i < pData->curPluginCount; ++i)
  1619. {
  1620. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1621. if (plugin != nullptr && plugin->isEnabled())
  1622. {
  1623. plugin->tryLock(true);
  1624. plugin->bufferSizeChanged(newBufferSize);
  1625. plugin->unlock();
  1626. }
  1627. }
  1628. callback(true, true, ENGINE_CALLBACK_BUFFER_SIZE_CHANGED, 0, static_cast<int>(newBufferSize), 0, 0, 0.0f, nullptr);
  1629. }
  1630. void CarlaEngine::sampleRateChanged(const double newSampleRate)
  1631. {
  1632. carla_debug("CarlaEngine::sampleRateChanged(%g)", newSampleRate);
  1633. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1634. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK ||
  1635. pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  1636. {
  1637. pData->graph.setSampleRate(newSampleRate);
  1638. }
  1639. #endif
  1640. pData->time.updateAudioValues(pData->bufferSize, newSampleRate);
  1641. for (uint i=0; i < pData->curPluginCount; ++i)
  1642. {
  1643. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1644. if (plugin != nullptr && plugin->isEnabled())
  1645. {
  1646. plugin->tryLock(true);
  1647. plugin->sampleRateChanged(newSampleRate);
  1648. plugin->unlock();
  1649. }
  1650. }
  1651. callback(true, true, ENGINE_CALLBACK_SAMPLE_RATE_CHANGED, 0, 0, 0, 0, static_cast<float>(newSampleRate), nullptr);
  1652. }
  1653. void CarlaEngine::offlineModeChanged(const bool isOfflineNow)
  1654. {
  1655. carla_debug("CarlaEngine::offlineModeChanged(%s)", bool2str(isOfflineNow));
  1656. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1657. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK ||
  1658. pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  1659. {
  1660. pData->graph.setOffline(isOfflineNow);
  1661. }
  1662. #endif
  1663. for (uint i=0; i < pData->curPluginCount; ++i)
  1664. {
  1665. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1666. if (plugin != nullptr && plugin->isEnabled())
  1667. plugin->offlineModeChanged(isOfflineNow);
  1668. }
  1669. }
  1670. void CarlaEngine::setPluginPeaksRT(const uint pluginId, float const inPeaks[2], float const outPeaks[2]) noexcept
  1671. {
  1672. EnginePluginData& pluginData(pData->plugins[pluginId]);
  1673. pluginData.peaks[0] = inPeaks[0];
  1674. pluginData.peaks[1] = inPeaks[1];
  1675. pluginData.peaks[2] = outPeaks[0];
  1676. pluginData.peaks[3] = outPeaks[1];
  1677. }
  1678. void CarlaEngine::saveProjectInternal(water::MemoryOutputStream& outStream) const
  1679. {
  1680. // send initial prepareForSave first, giving time for bridges to act
  1681. for (uint i=0; i < pData->curPluginCount; ++i)
  1682. {
  1683. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1684. if (plugin != nullptr && plugin->isEnabled())
  1685. {
  1686. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1687. // deactivate bridge client-side ping check, since some plugins block during save
  1688. if (plugin->getHints() & PLUGIN_IS_BRIDGE)
  1689. plugin->setCustomData(CUSTOM_DATA_TYPE_STRING, "__CarlaPingOnOff__", "false", false);
  1690. #endif
  1691. plugin->prepareForSave();
  1692. }
  1693. }
  1694. outStream << "<?xml version='1.0' encoding='UTF-8'?>\n";
  1695. outStream << "<!DOCTYPE CARLA-PROJECT>\n";
  1696. outStream << "<CARLA-PROJECT VERSION='2.0'>\n";
  1697. const bool isPlugin(getType() == kEngineTypePlugin);
  1698. const EngineOptions& options(pData->options);
  1699. {
  1700. MemoryOutputStream outSettings(1024);
  1701. outSettings << " <EngineSettings>\n";
  1702. outSettings << " <ForceStereo>" << bool2str(options.forceStereo) << "</ForceStereo>\n";
  1703. outSettings << " <PreferPluginBridges>" << bool2str(options.preferPluginBridges) << "</PreferPluginBridges>\n";
  1704. outSettings << " <PreferUiBridges>" << bool2str(options.preferUiBridges) << "</PreferUiBridges>\n";
  1705. outSettings << " <UIsAlwaysOnTop>" << bool2str(options.uisAlwaysOnTop) << "</UIsAlwaysOnTop>\n";
  1706. outSettings << " <MaxParameters>" << String(options.maxParameters) << "</MaxParameters>\n";
  1707. outSettings << " <UIBridgesTimeout>" << String(options.uiBridgesTimeout) << "</UIBridgesTimeout>\n";
  1708. if (isPlugin)
  1709. {
  1710. outSettings << " <LADSPA_PATH>" << xmlSafeString(options.pathLADSPA, true) << "</LADSPA_PATH>\n";
  1711. outSettings << " <DSSI_PATH>" << xmlSafeString(options.pathDSSI, true) << "</DSSI_PATH>\n";
  1712. outSettings << " <LV2_PATH>" << xmlSafeString(options.pathLV2, true) << "</LV2_PATH>\n";
  1713. outSettings << " <VST2_PATH>" << xmlSafeString(options.pathVST2, true) << "</VST2_PATH>\n";
  1714. outSettings << " <VST3_PATH>" << xmlSafeString(options.pathVST3, true) << "</VST3_PATH>\n";
  1715. outSettings << " <SF2_PATH>" << xmlSafeString(options.pathSF2, true) << "</SF2_PATH>\n";
  1716. outSettings << " <SFZ_PATH>" << xmlSafeString(options.pathSFZ, true) << "</SFZ_PATH>\n";
  1717. }
  1718. outSettings << " </EngineSettings>\n";
  1719. outStream << outSettings;
  1720. }
  1721. if (pData->timeInfo.bbt.valid && ! isPlugin)
  1722. {
  1723. MemoryOutputStream outTransport(128);
  1724. outTransport << "\n <Transport>\n";
  1725. // outTransport << " <BeatsPerBar>" << pData->timeInfo.bbt.beatsPerBar << "</BeatsPerBar>\n";
  1726. outTransport << " <BeatsPerMinute>" << pData->timeInfo.bbt.beatsPerMinute << "</BeatsPerMinute>\n";
  1727. outTransport << " </Transport>\n";
  1728. outStream << outTransport;
  1729. }
  1730. char strBuf[STR_MAX+1];
  1731. carla_zeroChars(strBuf, STR_MAX+1);
  1732. for (uint i=0; i < pData->curPluginCount; ++i)
  1733. {
  1734. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1735. if (plugin != nullptr && plugin->isEnabled())
  1736. {
  1737. MemoryOutputStream outPlugin(4096), streamPlugin;
  1738. plugin->getStateSave(false).dumpToMemoryStream(streamPlugin);
  1739. outPlugin << "\n";
  1740. if (plugin->getRealName(strBuf))
  1741. outPlugin << " <!-- " << xmlSafeString(strBuf, true) << " -->\n";
  1742. outPlugin << " <Plugin>\n";
  1743. outPlugin << streamPlugin;
  1744. outPlugin << " </Plugin>\n";
  1745. outStream << outPlugin;
  1746. }
  1747. }
  1748. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1749. // tell bridges we're done saving
  1750. for (uint i=0; i < pData->curPluginCount; ++i)
  1751. {
  1752. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1753. if (plugin != nullptr && plugin->isEnabled() && (plugin->getHints() & PLUGIN_IS_BRIDGE) != 0)
  1754. plugin->setCustomData(CUSTOM_DATA_TYPE_STRING, "__CarlaPingOnOff__", "true", false);
  1755. }
  1756. // save internal connections
  1757. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  1758. {
  1759. if (const char* const* const patchbayConns = getPatchbayConnections(false))
  1760. {
  1761. MemoryOutputStream outPatchbay(2048);
  1762. outPatchbay << "\n <Patchbay>\n";
  1763. for (int i=0; patchbayConns[i] != nullptr && patchbayConns[i+1] != nullptr; ++i, ++i )
  1764. {
  1765. const char* const connSource(patchbayConns[i]);
  1766. const char* const connTarget(patchbayConns[i+1]);
  1767. CARLA_SAFE_ASSERT_CONTINUE(connSource != nullptr && connSource[0] != '\0');
  1768. CARLA_SAFE_ASSERT_CONTINUE(connTarget != nullptr && connTarget[0] != '\0');
  1769. outPatchbay << " <Connection>\n";
  1770. outPatchbay << " <Source>" << xmlSafeString(connSource, true) << "</Source>\n";
  1771. outPatchbay << " <Target>" << xmlSafeString(connTarget, true) << "</Target>\n";
  1772. outPatchbay << " </Connection>\n";
  1773. }
  1774. outPatchbay << " </Patchbay>\n";
  1775. outStream << outPatchbay;
  1776. }
  1777. }
  1778. // if we're running inside some session-manager (and using JACK), let them handle the connections
  1779. bool saveExternalConnections;
  1780. /**/ if (isPlugin)
  1781. saveExternalConnections = false;
  1782. else if (std::strcmp(getCurrentDriverName(), "JACK") != 0)
  1783. saveExternalConnections = true;
  1784. else if (std::getenv("CARLA_DONT_MANAGE_CONNECTIONS") != nullptr)
  1785. saveExternalConnections = false;
  1786. else if (std::getenv("LADISH_APP_NAME") != nullptr)
  1787. saveExternalConnections = false;
  1788. else if (std::getenv("NSM_URL") != nullptr)
  1789. saveExternalConnections = false;
  1790. else
  1791. saveExternalConnections = true;
  1792. if (saveExternalConnections)
  1793. {
  1794. if (const char* const* const patchbayConns = getPatchbayConnections(true))
  1795. {
  1796. MemoryOutputStream outPatchbay(2048);
  1797. outPatchbay << "\n <ExternalPatchbay>\n";
  1798. for (int i=0; patchbayConns[i] != nullptr && patchbayConns[i+1] != nullptr; ++i, ++i )
  1799. {
  1800. const char* const connSource(patchbayConns[i]);
  1801. const char* const connTarget(patchbayConns[i+1]);
  1802. CARLA_SAFE_ASSERT_CONTINUE(connSource != nullptr && connSource[0] != '\0');
  1803. CARLA_SAFE_ASSERT_CONTINUE(connTarget != nullptr && connTarget[0] != '\0');
  1804. outPatchbay << " <Connection>\n";
  1805. outPatchbay << " <Source>" << xmlSafeString(connSource, true) << "</Source>\n";
  1806. outPatchbay << " <Target>" << xmlSafeString(connTarget, true) << "</Target>\n";
  1807. outPatchbay << " </Connection>\n";
  1808. }
  1809. outPatchbay << " </ExternalPatchbay>\n";
  1810. outStream << outPatchbay;
  1811. }
  1812. }
  1813. #endif
  1814. outStream << "</CARLA-PROJECT>\n";
  1815. }
  1816. static String findBinaryInCustomPath(const char* const searchPath, const char* const binary)
  1817. {
  1818. const StringArray searchPaths(StringArray::fromTokens(searchPath, CARLA_OS_SPLIT_STR, ""));
  1819. // try direct filename first
  1820. String jbinary(binary);
  1821. // adjust for current platform
  1822. #ifdef CARLA_OS_WIN
  1823. if (jbinary[0] == '/')
  1824. jbinary = "C:" + jbinary.replaceCharacter('/', '\\');
  1825. #else
  1826. if (jbinary[1] == ':' && (jbinary[2] == '\\' || jbinary[2] == '/'))
  1827. jbinary = jbinary.substring(2).replaceCharacter('\\', '/');
  1828. #endif
  1829. String filename = File(jbinary).getFileName();
  1830. int searchFlags = File::findFiles|File::ignoreHiddenFiles;
  1831. #ifdef CARLA_OS_MAC
  1832. if (filename.endsWithIgnoreCase(".vst") || filename.endsWithIgnoreCase(".vst3"))
  1833. searchFlags |= File::findDirectories;
  1834. #endif
  1835. Array<File> results;
  1836. for (const String *it=searchPaths.begin(), *end=searchPaths.end(); it != end; ++it)
  1837. {
  1838. const File path(*it);
  1839. results.clear();
  1840. path.findChildFiles(results, searchFlags, true, filename);
  1841. if (results.size() > 0)
  1842. return results.getFirst().getFullPathName();
  1843. }
  1844. // try changing extension
  1845. #if defined(CARLA_OS_MAC)
  1846. if (filename.endsWithIgnoreCase(".dll") || filename.endsWithIgnoreCase(".so"))
  1847. filename = File(jbinary).getFileNameWithoutExtension() + ".dylib";
  1848. #elif defined(CARLA_OS_WIN)
  1849. if (filename.endsWithIgnoreCase(".dylib") || filename.endsWithIgnoreCase(".so"))
  1850. filename = File(jbinary).getFileNameWithoutExtension() + ".dll";
  1851. #else
  1852. if (filename.endsWithIgnoreCase(".dll") || filename.endsWithIgnoreCase(".dylib"))
  1853. filename = File(jbinary).getFileNameWithoutExtension() + ".so";
  1854. #endif
  1855. else
  1856. return String();
  1857. for (const String *it=searchPaths.begin(), *end=searchPaths.end(); it != end; ++it)
  1858. {
  1859. const File path(*it);
  1860. results.clear();
  1861. path.findChildFiles(results, searchFlags, true, filename);
  1862. if (results.size() > 0)
  1863. return results.getFirst().getFullPathName();
  1864. }
  1865. return String();
  1866. }
  1867. bool CarlaEngine::loadProjectInternal(water::XmlDocument& xmlDoc)
  1868. {
  1869. ScopedPointer<XmlElement> xmlElement(xmlDoc.getDocumentElement(true));
  1870. CARLA_SAFE_ASSERT_RETURN_ERR(xmlElement != nullptr, "Failed to parse project file");
  1871. const String& xmlType(xmlElement->getTagName());
  1872. const bool isPreset(xmlType.equalsIgnoreCase("carla-preset"));
  1873. if (! (xmlType.equalsIgnoreCase("carla-project") || isPreset))
  1874. {
  1875. callback(true, true, ENGINE_CALLBACK_PROJECT_LOAD_FINISHED, 0, 0, 0, 0, 0.0f, nullptr);
  1876. setLastError("Not a valid Carla project or preset file");
  1877. return false;
  1878. }
  1879. pData->actionCanceled = false;
  1880. callback(true, true, ENGINE_CALLBACK_CANCELABLE_ACTION, 0, 1, 0, 0, 0.0f, "Loading project");
  1881. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1882. const ScopedValueSetter<bool> _svs2(pData->loadingProject, true, false);
  1883. #endif
  1884. // completely load file
  1885. xmlElement = xmlDoc.getDocumentElement(false);
  1886. CARLA_SAFE_ASSERT_RETURN_ERR(xmlElement != nullptr, "Failed to completely parse project file");
  1887. callback(true, false, ENGINE_CALLBACK_IDLE, 0, 0, 0, 0, 0.0f, nullptr);
  1888. if (pData->aboutToClose)
  1889. return true;
  1890. if (pData->actionCanceled)
  1891. {
  1892. setLastError("Project load canceled");
  1893. return false;
  1894. }
  1895. const bool isPlugin(getType() == kEngineTypePlugin);
  1896. // load engine settings first of all
  1897. if (XmlElement* const elem = isPreset ? nullptr : xmlElement->getChildByName("EngineSettings"))
  1898. {
  1899. for (XmlElement* settElem = elem->getFirstChildElement(); settElem != nullptr; settElem = settElem->getNextElement())
  1900. {
  1901. const String& tag(settElem->getTagName());
  1902. const String text(settElem->getAllSubText().trim());
  1903. /** some settings might be incorrect or require extra work,
  1904. so we call setOption rather than modifying them direly */
  1905. int option = -1;
  1906. int value = 0;
  1907. const char* valueStr = nullptr;
  1908. /**/ if (tag == "ForceStereo")
  1909. {
  1910. option = ENGINE_OPTION_FORCE_STEREO;
  1911. value = text == "true" ? 1 : 0;
  1912. }
  1913. else if (tag == "PreferPluginBridges")
  1914. {
  1915. option = ENGINE_OPTION_PREFER_PLUGIN_BRIDGES;
  1916. value = text == "true" ? 1 : 0;
  1917. }
  1918. else if (tag == "PreferUiBridges")
  1919. {
  1920. option = ENGINE_OPTION_PREFER_UI_BRIDGES;
  1921. value = text == "true" ? 1 : 0;
  1922. }
  1923. else if (tag == "UIsAlwaysOnTop")
  1924. {
  1925. option = ENGINE_OPTION_UIS_ALWAYS_ON_TOP;
  1926. value = text == "true" ? 1 : 0;
  1927. }
  1928. else if (tag == "MaxParameters")
  1929. {
  1930. option = ENGINE_OPTION_MAX_PARAMETERS;
  1931. value = text.getIntValue();
  1932. }
  1933. else if (tag == "UIBridgesTimeout")
  1934. {
  1935. option = ENGINE_OPTION_UI_BRIDGES_TIMEOUT;
  1936. value = text.getIntValue();
  1937. }
  1938. else if (isPlugin)
  1939. {
  1940. /**/ if (tag == "LADSPA_PATH")
  1941. {
  1942. option = ENGINE_OPTION_PLUGIN_PATH;
  1943. value = PLUGIN_LADSPA;
  1944. valueStr = text.toRawUTF8();
  1945. }
  1946. else if (tag == "DSSI_PATH")
  1947. {
  1948. option = ENGINE_OPTION_PLUGIN_PATH;
  1949. value = PLUGIN_DSSI;
  1950. valueStr = text.toRawUTF8();
  1951. }
  1952. else if (tag == "LV2_PATH")
  1953. {
  1954. option = ENGINE_OPTION_PLUGIN_PATH;
  1955. value = PLUGIN_LV2;
  1956. valueStr = text.toRawUTF8();
  1957. }
  1958. else if (tag == "VST2_PATH")
  1959. {
  1960. option = ENGINE_OPTION_PLUGIN_PATH;
  1961. value = PLUGIN_VST2;
  1962. valueStr = text.toRawUTF8();
  1963. }
  1964. else if (tag.equalsIgnoreCase("VST3_PATH"))
  1965. {
  1966. option = ENGINE_OPTION_PLUGIN_PATH;
  1967. value = PLUGIN_VST3;
  1968. valueStr = text.toRawUTF8();
  1969. }
  1970. else if (tag == "SF2_PATH")
  1971. {
  1972. option = ENGINE_OPTION_PLUGIN_PATH;
  1973. value = PLUGIN_SF2;
  1974. valueStr = text.toRawUTF8();
  1975. }
  1976. else if (tag == "SFZ_PATH")
  1977. {
  1978. option = ENGINE_OPTION_PLUGIN_PATH;
  1979. value = PLUGIN_SFZ;
  1980. valueStr = text.toRawUTF8();
  1981. }
  1982. }
  1983. if (option == -1)
  1984. {
  1985. // check old stuff, unhandled now
  1986. if (tag == "GIG_PATH")
  1987. continue;
  1988. // ignored tags
  1989. if (tag == "LADSPA_PATH" || tag == "DSSI_PATH" || tag == "LV2_PATH" || tag == "VST2_PATH")
  1990. continue;
  1991. if (tag == "VST3_PATH" || tag == "AU_PATH")
  1992. continue;
  1993. if (tag == "SF2_PATH" || tag == "SFZ_PATH")
  1994. continue;
  1995. // hmm something is wrong..
  1996. carla_stderr2("CarlaEngine::loadProjectInternal() - Unhandled option '%s'", tag.toRawUTF8());
  1997. continue;
  1998. }
  1999. setOption(static_cast<EngineOption>(option), value, valueStr);
  2000. }
  2001. callback(true, true, ENGINE_CALLBACK_IDLE, 0, 0, 0, 0, 0.0f, nullptr);
  2002. if (pData->aboutToClose)
  2003. return true;
  2004. if (pData->actionCanceled)
  2005. {
  2006. setLastError("Project load canceled");
  2007. return false;
  2008. }
  2009. }
  2010. // now setup transport
  2011. if (XmlElement* const elem = (isPreset || isPlugin) ? nullptr : xmlElement->getChildByName("Transport"))
  2012. {
  2013. if (XmlElement* const bpmElem = elem->getChildByName("BeatsPerMinute"))
  2014. {
  2015. const String bpmText(bpmElem->getAllSubText().trim());
  2016. const double bpm = bpmText.getDoubleValue();
  2017. // some sane limits
  2018. if (bpm >= 20.0 && bpm < 400.0)
  2019. pData->time.setBPM(bpm);
  2020. callback(true, true, ENGINE_CALLBACK_IDLE, 0, 0, 0, 0, 0.0f, nullptr);
  2021. if (pData->aboutToClose)
  2022. return true;
  2023. if (pData->actionCanceled)
  2024. {
  2025. setLastError("Project load canceled");
  2026. return false;
  2027. }
  2028. }
  2029. }
  2030. // and we handle plugins
  2031. for (XmlElement* elem = xmlElement->getFirstChildElement(); elem != nullptr; elem = elem->getNextElement())
  2032. {
  2033. const String& tagName(elem->getTagName());
  2034. if (isPreset || tagName == "Plugin")
  2035. {
  2036. CarlaStateSave stateSave;
  2037. stateSave.fillFromXmlElement(isPreset ? xmlElement.get() : elem);
  2038. callback(true, true, ENGINE_CALLBACK_IDLE, 0, 0, 0, 0, 0.0f, nullptr);
  2039. if (pData->aboutToClose)
  2040. return true;
  2041. if (pData->actionCanceled)
  2042. {
  2043. setLastError("Project load canceled");
  2044. return false;
  2045. }
  2046. CARLA_SAFE_ASSERT_CONTINUE(stateSave.type != nullptr);
  2047. #ifndef BUILD_BRIDGE
  2048. // compatibility code to load projects with GIG files
  2049. // FIXME Remove on 2.1 release
  2050. if (std::strcmp(stateSave.type, "GIG") == 0)
  2051. {
  2052. if (addPlugin(PLUGIN_LV2, "", stateSave.name, "http://linuxsampler.org/plugins/linuxsampler", 0, nullptr))
  2053. {
  2054. const uint pluginId = pData->curPluginCount;
  2055. if (CarlaPlugin* const plugin = pData->plugins[pluginId].plugin)
  2056. {
  2057. callback(true, true, ENGINE_CALLBACK_IDLE, 0, 0, 0, 0, 0.0f, nullptr);
  2058. if (pData->aboutToClose)
  2059. return true;
  2060. if (pData->actionCanceled)
  2061. {
  2062. setLastError("Project load canceled");
  2063. return false;
  2064. }
  2065. String lsState;
  2066. lsState << "0.35\n";
  2067. lsState << "18 0 Chromatic\n";
  2068. lsState << "18 1 Drum Kits\n";
  2069. lsState << "20 0\n";
  2070. lsState << "0 1 " << stateSave.binary << "\n";
  2071. lsState << "0 0 0 0 1 0 GIG\n";
  2072. plugin->setCustomData(LV2_ATOM__String, "http://linuxsampler.org/schema#state-string", lsState.toRawUTF8(), true);
  2073. plugin->restoreLV2State();
  2074. plugin->setDryWet(stateSave.dryWet, true, true);
  2075. plugin->setVolume(stateSave.volume, true, true);
  2076. plugin->setBalanceLeft(stateSave.balanceLeft, true, true);
  2077. plugin->setBalanceRight(stateSave.balanceRight, true, true);
  2078. plugin->setPanning(stateSave.panning, true, true);
  2079. plugin->setCtrlChannel(stateSave.ctrlChannel, true, true);
  2080. plugin->setActive(stateSave.active, true, true);
  2081. plugin->setEnabled(true);
  2082. ++pData->curPluginCount;
  2083. callback(true, true, ENGINE_CALLBACK_PLUGIN_ADDED, pluginId, 0, 0, 0, 0.0f, plugin->getName());
  2084. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  2085. pData->graph.addPlugin(plugin);
  2086. }
  2087. else
  2088. {
  2089. carla_stderr2("Failed to get new plugin, state will not be restored correctly\n");
  2090. }
  2091. }
  2092. else
  2093. {
  2094. carla_stderr2("Failed to load a linuxsampler LV2 plugin, GIG file won't be loaded");
  2095. }
  2096. continue;
  2097. }
  2098. #endif
  2099. const void* extraStuff = nullptr;
  2100. static const char kTrue[] = "true";
  2101. const PluginType ptype(getPluginTypeFromString(stateSave.type));
  2102. switch (ptype)
  2103. {
  2104. case PLUGIN_SF2:
  2105. if (CarlaString(stateSave.label).endsWith(" (16 outs)"))
  2106. extraStuff = kTrue;
  2107. // fall through
  2108. case PLUGIN_LADSPA:
  2109. case PLUGIN_DSSI:
  2110. case PLUGIN_VST2:
  2111. case PLUGIN_VST3:
  2112. case PLUGIN_SFZ:
  2113. if (stateSave.binary != nullptr && stateSave.binary[0] != '\0' &&
  2114. ! (File::isAbsolutePath(stateSave.binary) && File(stateSave.binary).exists()))
  2115. {
  2116. const char* searchPath;
  2117. switch (ptype)
  2118. {
  2119. case PLUGIN_LADSPA: searchPath = pData->options.pathLADSPA; break;
  2120. case PLUGIN_DSSI: searchPath = pData->options.pathDSSI; break;
  2121. case PLUGIN_VST2: searchPath = pData->options.pathVST2; break;
  2122. case PLUGIN_VST3: searchPath = pData->options.pathVST3; break;
  2123. case PLUGIN_SF2: searchPath = pData->options.pathSF2; break;
  2124. case PLUGIN_SFZ: searchPath = pData->options.pathSFZ; break;
  2125. default: searchPath = nullptr; break;
  2126. }
  2127. if (searchPath != nullptr && searchPath[0] != '\0')
  2128. {
  2129. carla_stderr("Plugin binary '%s' doesn't exist on this filesystem, let's look for it...",
  2130. stateSave.binary);
  2131. String result = findBinaryInCustomPath(searchPath, stateSave.binary);
  2132. if (result.isEmpty())
  2133. {
  2134. switch (ptype)
  2135. {
  2136. case PLUGIN_LADSPA: searchPath = std::getenv("LADSPA_PATH"); break;
  2137. case PLUGIN_DSSI: searchPath = std::getenv("DSSI_PATH"); break;
  2138. case PLUGIN_VST2: searchPath = std::getenv("VST_PATH"); break;
  2139. case PLUGIN_VST3: searchPath = std::getenv("VST3_PATH"); break;
  2140. case PLUGIN_SF2: searchPath = std::getenv("SF2_PATH"); break;
  2141. case PLUGIN_SFZ: searchPath = std::getenv("SFZ_PATH"); break;
  2142. default: searchPath = nullptr; break;
  2143. }
  2144. if (searchPath != nullptr && searchPath[0] != '\0')
  2145. result = findBinaryInCustomPath(searchPath, stateSave.binary);
  2146. }
  2147. if (result.isNotEmpty())
  2148. {
  2149. delete[] stateSave.binary;
  2150. stateSave.binary = carla_strdup(result.toRawUTF8());
  2151. carla_stderr("Found it! :)");
  2152. }
  2153. else
  2154. {
  2155. carla_stderr("Damn, we failed... :(");
  2156. }
  2157. }
  2158. }
  2159. break;
  2160. default:
  2161. break;
  2162. }
  2163. BinaryType btype;
  2164. switch (ptype)
  2165. {
  2166. case PLUGIN_LADSPA:
  2167. case PLUGIN_DSSI:
  2168. case PLUGIN_LV2:
  2169. case PLUGIN_VST2:
  2170. btype = getBinaryTypeFromFile(stateSave.binary);
  2171. break;
  2172. default:
  2173. btype = BINARY_NATIVE;
  2174. break;
  2175. }
  2176. if (addPlugin(btype, ptype, stateSave.binary,
  2177. stateSave.name, stateSave.label, stateSave.uniqueId, extraStuff, stateSave.options))
  2178. {
  2179. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  2180. const uint pluginId = pData->curPluginCount;
  2181. #else
  2182. const uint pluginId = 0;
  2183. #endif
  2184. if (CarlaPlugin* const plugin = pData->plugins[pluginId].plugin)
  2185. {
  2186. callback(true, true, ENGINE_CALLBACK_IDLE, 0, 0, 0, 0, 0.0f, nullptr);
  2187. if (pData->aboutToClose)
  2188. return true;
  2189. if (pData->actionCanceled)
  2190. {
  2191. setLastError("Project load canceled");
  2192. return false;
  2193. }
  2194. // deactivate bridge client-side ping check, since some plugins block during load
  2195. if ((plugin->getHints() & PLUGIN_IS_BRIDGE) != 0 && ! isPreset)
  2196. plugin->setCustomData(CUSTOM_DATA_TYPE_STRING, "__CarlaPingOnOff__", "false", false);
  2197. plugin->loadStateSave(stateSave);
  2198. /* NOTE: The following code is the same as the end of addPlugin().
  2199. * When project is loading we do not enable the plugin right away,
  2200. * as we want to load state first.
  2201. */
  2202. plugin->setEnabled(true);
  2203. ++pData->curPluginCount;
  2204. callback(true, true, ENGINE_CALLBACK_PLUGIN_ADDED, pluginId, 0, 0, 0, 0.0f, plugin->getName());
  2205. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  2206. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  2207. pData->graph.addPlugin(plugin);
  2208. #endif
  2209. }
  2210. else
  2211. {
  2212. carla_stderr2("Failed to get new plugin, state will not be restored correctly\n");
  2213. }
  2214. }
  2215. else
  2216. {
  2217. carla_stderr2("Failed to load a plugin '%s', error was:\n%s", stateSave.name, getLastError());
  2218. }
  2219. }
  2220. if (isPreset)
  2221. {
  2222. callback(true, true, ENGINE_CALLBACK_PROJECT_LOAD_FINISHED, 0, 0, 0, 0, 0.0f, nullptr);
  2223. callback(true, true, ENGINE_CALLBACK_CANCELABLE_ACTION, 0, 0, 0, 0, 0.0f, "Loading project");
  2224. return true;
  2225. }
  2226. }
  2227. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  2228. // tell bridges we're done loading
  2229. for (uint i=0; i < pData->curPluginCount; ++i)
  2230. {
  2231. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  2232. if (plugin != nullptr && plugin->isEnabled() && (plugin->getHints() & PLUGIN_IS_BRIDGE) != 0)
  2233. plugin->setCustomData(CUSTOM_DATA_TYPE_STRING, "__CarlaPingOnOff__", "true", false);
  2234. }
  2235. callback(true, true, ENGINE_CALLBACK_IDLE, 0, 0, 0, 0, 0.0f, nullptr);
  2236. if (pData->aboutToClose)
  2237. return true;
  2238. if (pData->actionCanceled)
  2239. {
  2240. setLastError("Project load canceled");
  2241. return false;
  2242. }
  2243. bool hasInternalConnections = false;
  2244. // and now we handle connections (internal)
  2245. if (XmlElement* const elem = xmlElement->getChildByName("Patchbay"))
  2246. {
  2247. hasInternalConnections = true;
  2248. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  2249. {
  2250. CarlaString sourcePort, targetPort;
  2251. for (XmlElement* patchElem = elem->getFirstChildElement(); patchElem != nullptr; patchElem = patchElem->getNextElement())
  2252. {
  2253. const String& patchTag(patchElem->getTagName());
  2254. if (patchTag != "Connection")
  2255. continue;
  2256. sourcePort.clear();
  2257. targetPort.clear();
  2258. for (XmlElement* connElem = patchElem->getFirstChildElement(); connElem != nullptr; connElem = connElem->getNextElement())
  2259. {
  2260. const String& tag(connElem->getTagName());
  2261. const String text(connElem->getAllSubText().trim());
  2262. /**/ if (tag == "Source")
  2263. sourcePort = xmlSafeString(text, false).toRawUTF8();
  2264. else if (tag == "Target")
  2265. targetPort = xmlSafeString(text, false).toRawUTF8();
  2266. }
  2267. if (sourcePort.isNotEmpty() && targetPort.isNotEmpty())
  2268. restorePatchbayConnection(false, sourcePort, targetPort);
  2269. }
  2270. callback(true, true, ENGINE_CALLBACK_IDLE, 0, 0, 0, 0, 0.0f, nullptr);
  2271. if (pData->aboutToClose)
  2272. return true;
  2273. if (pData->actionCanceled)
  2274. {
  2275. setLastError("Project load canceled");
  2276. return false;
  2277. }
  2278. }
  2279. }
  2280. // if we're running inside some session-manager (and using JACK), let them handle the external connections
  2281. bool loadExternalConnections;
  2282. /**/ if (std::strcmp(getCurrentDriverName(), "JACK") != 0)
  2283. loadExternalConnections = true;
  2284. else if (std::getenv("CARLA_DONT_MANAGE_CONNECTIONS") != nullptr)
  2285. loadExternalConnections = false;
  2286. else if (std::getenv("LADISH_APP_NAME") != nullptr)
  2287. loadExternalConnections = false;
  2288. else if (std::getenv("NSM_URL") != nullptr)
  2289. loadExternalConnections = false;
  2290. else
  2291. loadExternalConnections = true;
  2292. // plus external connections too
  2293. if (loadExternalConnections)
  2294. {
  2295. const bool loadingAsExternal = pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY &&
  2296. hasInternalConnections;
  2297. for (XmlElement* elem = xmlElement->getFirstChildElement(); elem != nullptr; elem = elem->getNextElement())
  2298. {
  2299. const String& tagName(elem->getTagName());
  2300. // check if we want to load patchbay-mode connections into an external (multi-client) graph
  2301. if (tagName == "Patchbay")
  2302. {
  2303. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  2304. continue;
  2305. }
  2306. // or load external patchbay connections
  2307. else if (tagName != "ExternalPatchbay")
  2308. {
  2309. continue;
  2310. }
  2311. CarlaString sourcePort, targetPort;
  2312. for (XmlElement* patchElem = elem->getFirstChildElement(); patchElem != nullptr; patchElem = patchElem->getNextElement())
  2313. {
  2314. const String& patchTag(patchElem->getTagName());
  2315. if (patchTag != "Connection")
  2316. continue;
  2317. sourcePort.clear();
  2318. targetPort.clear();
  2319. for (XmlElement* connElem = patchElem->getFirstChildElement(); connElem != nullptr; connElem = connElem->getNextElement())
  2320. {
  2321. const String& tag(connElem->getTagName());
  2322. const String text(connElem->getAllSubText().trim());
  2323. /**/ if (tag == "Source")
  2324. sourcePort = xmlSafeString(text, false).toRawUTF8();
  2325. else if (tag == "Target")
  2326. targetPort = xmlSafeString(text, false).toRawUTF8();
  2327. }
  2328. if (sourcePort.isNotEmpty() && targetPort.isNotEmpty())
  2329. restorePatchbayConnection(loadingAsExternal, sourcePort, targetPort);
  2330. }
  2331. break;
  2332. }
  2333. }
  2334. #endif
  2335. callback(true, true, ENGINE_CALLBACK_PROJECT_LOAD_FINISHED, 0, 0, 0, 0, 0.0f, nullptr);
  2336. callback(true, true, ENGINE_CALLBACK_CANCELABLE_ACTION, 0, 0, 0, 0, 0.0f, "Loading project");
  2337. return true;
  2338. }
  2339. // -----------------------------------------------------------------------
  2340. CARLA_BACKEND_END_NAMESPACE