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.

2448 lines
83KB

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