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.

2468 lines
82KB

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