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.

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