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.

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