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.

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