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.

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