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.

2037 lines
68KB

  1. /*
  2. * Carla Plugin Host
  3. * Copyright (C) 2011-2014 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. * - proper find&load plugins
  22. * - something about the peaks?
  23. */
  24. #include "CarlaEngineInternal.hpp"
  25. #include "CarlaPlugin.hpp"
  26. #include "CarlaBackendUtils.hpp"
  27. #include "CarlaBinaryUtils.hpp"
  28. #include "CarlaEngineUtils.hpp"
  29. #include "CarlaMathUtils.hpp"
  30. #include "CarlaPipeUtils.hpp"
  31. #include "CarlaStateUtils.hpp"
  32. #include "CarlaMIDI.h"
  33. #include "jackbridge/JackBridge.hpp"
  34. #include "juce_core.h"
  35. using juce::CharPointer_UTF8;
  36. using juce::File;
  37. using juce::MemoryOutputStream;
  38. using juce::ScopedPointer;
  39. using juce::String;
  40. using juce::XmlDocument;
  41. using juce::XmlElement;
  42. CARLA_BACKEND_START_NAMESPACE
  43. // -----------------------------------------------------------------------
  44. // Carla Engine
  45. CarlaEngine::CarlaEngine()
  46. : pData(new ProtectedData(this))
  47. {
  48. carla_debug("CarlaEngine::CarlaEngine()");
  49. }
  50. CarlaEngine::~CarlaEngine()
  51. {
  52. carla_debug("CarlaEngine::~CarlaEngine()");
  53. delete pData;
  54. }
  55. // -----------------------------------------------------------------------
  56. // Static calls
  57. uint CarlaEngine::getDriverCount()
  58. {
  59. carla_debug("CarlaEngine::getDriverCount()");
  60. uint count = 0;
  61. if (jackbridge_is_ok())
  62. count += 1;
  63. #ifndef BUILD_BRIDGE
  64. # if defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN)
  65. count += getJuceApiCount();
  66. # else
  67. count += getRtAudioApiCount();
  68. # endif
  69. #endif
  70. return count;
  71. }
  72. const char* CarlaEngine::getDriverName(const uint index2)
  73. {
  74. carla_debug("CarlaEngine::getDriverName(%i)", index2);
  75. uint index(index2);
  76. if (jackbridge_is_ok() && index-- == 0)
  77. return "JACK";
  78. #ifndef BUILD_BRIDGE
  79. # if defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN)
  80. if (const uint count = getJuceApiCount())
  81. {
  82. if (index < count)
  83. return getJuceApiName(index);
  84. index -= count;
  85. }
  86. # else
  87. if (const uint count = getRtAudioApiCount())
  88. {
  89. if (index < count)
  90. return getRtAudioApiName(index);
  91. index -= count;
  92. }
  93. # endif
  94. #endif
  95. carla_stderr("CarlaEngine::getDriverName(%i) - invalid index", index2);
  96. return nullptr;
  97. }
  98. const char* const* CarlaEngine::getDriverDeviceNames(const uint index2)
  99. {
  100. carla_debug("CarlaEngine::getDriverDeviceNames(%i)", index2);
  101. uint index(index2);
  102. if (jackbridge_is_ok() && index-- == 0)
  103. {
  104. static const char* ret[3] = { "Auto-Connect OFF", "Auto-Connect ON", nullptr };
  105. return ret;
  106. }
  107. #ifndef BUILD_BRIDGE
  108. # if defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN)
  109. if (const uint count = getJuceApiCount())
  110. {
  111. if (index < count)
  112. return getJuceApiDeviceNames(index);
  113. index -= count;
  114. }
  115. # else
  116. if (const uint count = getRtAudioApiCount())
  117. {
  118. if (index < count)
  119. return getRtAudioApiDeviceNames(index);
  120. index -= count;
  121. }
  122. # endif
  123. #endif
  124. carla_stderr("CarlaEngine::getDriverDeviceNames(%i) - invalid index", index2);
  125. return nullptr;
  126. }
  127. const EngineDriverDeviceInfo* CarlaEngine::getDriverDeviceInfo(const uint index2, const char* const deviceName)
  128. {
  129. carla_debug("CarlaEngine::getDriverDeviceInfo(%i, \"%s\")", index2, deviceName);
  130. uint index(index2);
  131. if (jackbridge_is_ok() && index-- == 0)
  132. {
  133. static uint32_t bufSizes[11] = { 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 0 };
  134. static EngineDriverDeviceInfo devInfo;
  135. devInfo.hints = ENGINE_DRIVER_DEVICE_VARIABLE_BUFFER_SIZE;
  136. devInfo.bufferSizes = bufSizes;
  137. devInfo.sampleRates = nullptr;
  138. return &devInfo;
  139. }
  140. #ifndef BUILD_BRIDGE
  141. # if defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN)
  142. if (const uint count = getJuceApiCount())
  143. {
  144. if (index < count)
  145. return getJuceDeviceInfo(index, deviceName);
  146. index -= count;
  147. }
  148. # else
  149. if (const uint count = getRtAudioApiCount())
  150. {
  151. if (index < count)
  152. return getRtAudioDeviceInfo(index, deviceName);
  153. index -= count;
  154. }
  155. # endif
  156. #endif
  157. carla_stderr("CarlaEngine::getDriverDeviceNames(%i, \"%s\") - invalid index", index2, deviceName);
  158. return nullptr;
  159. }
  160. CarlaEngine* CarlaEngine::newDriverByName(const char* const driverName)
  161. {
  162. CARLA_SAFE_ASSERT_RETURN(driverName != nullptr && driverName[0] != '\0', nullptr);
  163. carla_debug("CarlaEngine::newDriverByName(\"%s\")", driverName);
  164. if (std::strcmp(driverName, "JACK") == 0)
  165. return newJack();
  166. #ifndef BUILD_BRIDGE
  167. # if defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN)
  168. // -------------------------------------------------------------------
  169. // macos
  170. if (std::strcmp(driverName, "CoreAudio") == 0)
  171. return newJuce(AUDIO_API_CORE);
  172. // -------------------------------------------------------------------
  173. // windows
  174. if (std::strcmp(driverName, "ASIO") == 0)
  175. return newJuce(AUDIO_API_ASIO);
  176. if (std::strcmp(driverName, "DirectSound") == 0)
  177. return newJuce(AUDIO_API_DS);
  178. #else
  179. // -------------------------------------------------------------------
  180. // common
  181. if (std::strncmp(driverName, "JACK ", 5) == 0)
  182. return newRtAudio(AUDIO_API_JACK);
  183. // -------------------------------------------------------------------
  184. // linux
  185. if (std::strcmp(driverName, "ALSA") == 0)
  186. return newRtAudio(AUDIO_API_ALSA);
  187. if (std::strcmp(driverName, "OSS") == 0)
  188. return newRtAudio(AUDIO_API_OSS);
  189. if (std::strcmp(driverName, "PulseAudio") == 0)
  190. return newRtAudio(AUDIO_API_PULSE);
  191. # endif
  192. #endif
  193. carla_stderr("CarlaEngine::newDriverByName(\"%s\") - invalid driver name", driverName);
  194. return nullptr;
  195. }
  196. // -----------------------------------------------------------------------
  197. // Constant values
  198. uint CarlaEngine::getMaxClientNameSize() const noexcept
  199. {
  200. return STR_MAX/2;
  201. }
  202. uint CarlaEngine::getMaxPortNameSize() const noexcept
  203. {
  204. return STR_MAX;
  205. }
  206. uint CarlaEngine::getCurrentPluginCount() const noexcept
  207. {
  208. return pData->curPluginCount;
  209. }
  210. uint CarlaEngine::getMaxPluginNumber() const noexcept
  211. {
  212. return pData->maxPluginNumber;
  213. }
  214. // -----------------------------------------------------------------------
  215. // Virtual, per-engine type calls
  216. bool CarlaEngine::close()
  217. {
  218. carla_debug("CarlaEngine::close()");
  219. if (pData->curPluginCount != 0)
  220. {
  221. pData->aboutToClose = true;
  222. removeAllPlugins();
  223. }
  224. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  225. if (pData->osc.isControlRegistered())
  226. oscSend_control_exit();
  227. #endif
  228. pData->close();
  229. callback(ENGINE_CALLBACK_ENGINE_STOPPED, 0, 0, 0, 0.0f, nullptr);
  230. return true;
  231. }
  232. void CarlaEngine::idle() noexcept
  233. {
  234. CARLA_SAFE_ASSERT_RETURN(pData->nextAction.opcode == kEnginePostActionNull,); // FIXME REMOVE
  235. CARLA_SAFE_ASSERT_RETURN(pData->nextPluginId == pData->maxPluginNumber,);
  236. for (uint i=0; i < pData->curPluginCount; ++i)
  237. {
  238. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  239. if (plugin != nullptr && plugin->isEnabled())
  240. {
  241. const uint hints(plugin->getHints());
  242. if ((hints & PLUGIN_HAS_CUSTOM_UI) != 0 && (hints & PLUGIN_NEEDS_UI_MAIN_THREAD) != 0)
  243. {
  244. try {
  245. plugin->uiIdle();
  246. } CARLA_SAFE_EXCEPTION_CONTINUE("Plugin uiIdle");
  247. }
  248. }
  249. }
  250. #ifdef HAVE_LIBLO
  251. pData->osc.idle();
  252. #endif
  253. }
  254. CarlaEngineClient* CarlaEngine::addClient(CarlaPlugin* const)
  255. {
  256. return new CarlaEngineClient(*this);
  257. }
  258. // -----------------------------------------------------------------------
  259. // Plugin management
  260. bool CarlaEngine::addPlugin(const BinaryType btype, const PluginType ptype,
  261. const char* const filename, const char* const name, const char* const label, const int64_t uniqueId,
  262. const void* const extra, const uint options)
  263. {
  264. CARLA_SAFE_ASSERT_RETURN_ERR(pData->isIdling == 0, "An operation is still being processed, please wait for it to finish");
  265. CARLA_SAFE_ASSERT_RETURN_ERR(pData->plugins != nullptr, "Invalid engine internal data");
  266. CARLA_SAFE_ASSERT_RETURN_ERR(pData->nextPluginId <= pData->maxPluginNumber, "Invalid engine internal data");
  267. CARLA_SAFE_ASSERT_RETURN_ERR(pData->nextAction.opcode == kEnginePostActionNull, "Invalid engine internal data");
  268. CARLA_SAFE_ASSERT_RETURN_ERR(btype != BINARY_NONE, "Invalid plugin binary mode");
  269. CARLA_SAFE_ASSERT_RETURN_ERR(ptype != PLUGIN_NONE, "Invalid plugin type");
  270. CARLA_SAFE_ASSERT_RETURN_ERR((filename != nullptr && filename[0] != '\0') || (label != nullptr && label[0] != '\0'), "Invalid plugin filename and label");
  271. 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);
  272. uint id;
  273. #ifndef BUILD_BRIDGE
  274. CarlaPlugin* oldPlugin = nullptr;
  275. if (pData->nextPluginId < pData->curPluginCount)
  276. {
  277. id = pData->nextPluginId;
  278. pData->nextPluginId = pData->maxPluginNumber;
  279. oldPlugin = pData->plugins[id].plugin;
  280. CARLA_SAFE_ASSERT_RETURN_ERR(oldPlugin != nullptr, "Invalid replace plugin Id");
  281. }
  282. else
  283. #endif
  284. {
  285. id = pData->curPluginCount;
  286. if (id == pData->maxPluginNumber)
  287. {
  288. setLastError("Maximum number of plugins reached");
  289. return false;
  290. }
  291. CARLA_SAFE_ASSERT_RETURN_ERR(pData->plugins[id].plugin == nullptr, "Invalid engine internal data");
  292. }
  293. CarlaPlugin::Initializer initializer = {
  294. this,
  295. id,
  296. filename,
  297. name,
  298. label,
  299. uniqueId,
  300. options
  301. };
  302. CarlaPlugin* plugin = nullptr;
  303. #ifndef BRIDGE_PLUGIN
  304. CarlaString bridgeBinary(pData->options.binaryDir);
  305. if (bridgeBinary.isNotEmpty())
  306. {
  307. if (btype == BINARY_NATIVE)
  308. {
  309. #ifdef CARLA_OS_WIN
  310. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-native.exe";
  311. #else
  312. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-native";
  313. #endif
  314. }
  315. else
  316. {
  317. switch (btype)
  318. {
  319. case BINARY_POSIX32:
  320. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-posix32";
  321. break;
  322. case BINARY_POSIX64:
  323. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-posix64";
  324. break;
  325. case BINARY_WIN32:
  326. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-win32.exe";
  327. break;
  328. case BINARY_WIN64:
  329. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-win64.exe";
  330. break;
  331. default:
  332. bridgeBinary.clear();
  333. break;
  334. }
  335. }
  336. if (! File(bridgeBinary.buffer()).existsAsFile())
  337. bridgeBinary.clear();
  338. }
  339. if (ptype != PLUGIN_INTERNAL && (btype != BINARY_NATIVE || (pData->options.preferPluginBridges && bridgeBinary.isNotEmpty())))
  340. {
  341. if (bridgeBinary.isNotEmpty())
  342. {
  343. plugin = CarlaPlugin::newBridge(initializer, btype, ptype, bridgeBinary);
  344. }
  345. # ifdef CARLA_OS_LINUX
  346. // fallback to dssi-vst if possible
  347. else if (btype == BINARY_WIN32 && File("/usr/lib/dssi/dssi-vst.so").existsAsFile())
  348. {
  349. const String jfilename = String(CharPointer_UTF8(filename));
  350. File file(jfilename);
  351. CarlaString label2(file.getFileName().toRawUTF8());
  352. label2.replace(' ', '*');
  353. CarlaPlugin::Initializer init2 = {
  354. this,
  355. id,
  356. "/usr/lib/dssi/dssi-vst.so",
  357. name,
  358. label2,
  359. uniqueId,
  360. options
  361. };
  362. ScopedEnvVar sev("VST_PATH", file.getParentDirectory().getFullPathName().toRawUTF8());
  363. plugin = CarlaPlugin::newDSSI(init2);
  364. }
  365. # endif
  366. else
  367. {
  368. setLastError("This Carla build cannot handle this binary");
  369. return false;
  370. }
  371. }
  372. else
  373. #endif // ! BUILD_BRIDGE
  374. {
  375. bool use16Outs;
  376. setLastError("Invalid or unsupported plugin type");
  377. switch (ptype)
  378. {
  379. case PLUGIN_NONE:
  380. break;
  381. case PLUGIN_INTERNAL:
  382. /*if (std::strcmp(label, "FluidSynth") == 0)
  383. {
  384. use16Outs = (extra != nullptr && std::strcmp((const char*)extra, "true") == 0);
  385. plugin = CarlaPlugin::newFluidSynth(initializer, use16Outs);
  386. }
  387. else if (std::strcmp(label, "LinuxSampler (GIG)") == 0)
  388. {
  389. use16Outs = (extra != nullptr && std::strcmp((const char*)extra, "true") == 0);
  390. plugin = CarlaPlugin::newLinuxSampler(initializer, "GIG", use16Outs);
  391. }
  392. else if (std::strcmp(label, "LinuxSampler (SF2)") == 0)
  393. {
  394. use16Outs = (extra != nullptr && std::strcmp((const char*)extra, "true") == 0);
  395. plugin = CarlaPlugin::newLinuxSampler(initializer, "SF2", use16Outs);
  396. }
  397. else if (std::strcmp(label, "LinuxSampler (SFZ)") == 0)
  398. {
  399. use16Outs = (extra != nullptr && std::strcmp((const char*)extra, "true") == 0);
  400. plugin = CarlaPlugin::newLinuxSampler(initializer, "SFZ", use16Outs);
  401. }*/
  402. plugin = CarlaPlugin::newNative(initializer);
  403. break;
  404. case PLUGIN_LADSPA:
  405. plugin = CarlaPlugin::newLADSPA(initializer, (const LADSPA_RDF_Descriptor*)extra);
  406. break;
  407. case PLUGIN_DSSI:
  408. plugin = CarlaPlugin::newDSSI(initializer);
  409. break;
  410. case PLUGIN_LV2:
  411. plugin = CarlaPlugin::newLV2(initializer);
  412. break;
  413. case PLUGIN_VST2:
  414. plugin = CarlaPlugin::newVST2(initializer);
  415. break;
  416. case PLUGIN_VST3:
  417. plugin = CarlaPlugin::newVST3(initializer);
  418. break;
  419. case PLUGIN_AU:
  420. plugin = CarlaPlugin::newAU(initializer);
  421. break;
  422. case PLUGIN_GIG:
  423. use16Outs = (extra != nullptr && std::strcmp((const char*)extra, "true") == 0);
  424. plugin = CarlaPlugin::newFileGIG(initializer, use16Outs);
  425. break;
  426. case PLUGIN_SF2:
  427. use16Outs = (extra != nullptr && std::strcmp((const char*)extra, "true") == 0);
  428. plugin = CarlaPlugin::newFileSF2(initializer, use16Outs);
  429. break;
  430. case PLUGIN_SFZ:
  431. plugin = CarlaPlugin::newFileSFZ(initializer);
  432. break;
  433. }
  434. }
  435. if (plugin == nullptr)
  436. return false;
  437. plugin->reload();
  438. bool canRun = true;
  439. /**/ if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  440. {
  441. /**/ if (! plugin->canRunInRack())
  442. {
  443. setLastError("Carla's rack mode can only work with Mono or Stereo plugins, sorry!");
  444. canRun = false;
  445. }
  446. else if (plugin->getCVInCount() > 0 || plugin->getCVInCount() > 0)
  447. {
  448. setLastError("Carla's rack mode cannot work with plugins that have CV ports, sorry!");
  449. canRun = false;
  450. }
  451. }
  452. else if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  453. {
  454. /**/ if (plugin->getMidiInCount() > 1 || plugin->getMidiOutCount() > 1)
  455. {
  456. setLastError("Carla's patchbay mode cannot work with plugins that have multiple MIDI ports, sorry!");
  457. canRun = false;
  458. }
  459. else if (plugin->getCVInCount() > 0 || plugin->getCVInCount() > 0)
  460. {
  461. setLastError("CV ports in patchbay mode is still TODO");
  462. canRun = false;
  463. }
  464. }
  465. if (! canRun)
  466. {
  467. delete plugin;
  468. return false;
  469. }
  470. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  471. plugin->registerToOscClient();
  472. #endif
  473. EnginePluginData& pluginData(pData->plugins[id]);
  474. pluginData.plugin = plugin;
  475. pluginData.insPeak[0] = 0.0f;
  476. pluginData.insPeak[1] = 0.0f;
  477. pluginData.outsPeak[0] = 0.0f;
  478. pluginData.outsPeak[1] = 0.0f;
  479. #ifndef BUILD_BRIDGE
  480. if (oldPlugin != nullptr)
  481. {
  482. // the engine thread might be reading from the old plugin
  483. pData->thread.stopThread(500);
  484. pData->thread.startThread();
  485. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  486. pData->graph.replacePlugin(oldPlugin, plugin);
  487. const bool wasActive = oldPlugin->getInternalParameterValue(PARAMETER_ACTIVE) >= 0.5f;
  488. const float oldDryWet = oldPlugin->getInternalParameterValue(PARAMETER_DRYWET);
  489. const float oldVolume = oldPlugin->getInternalParameterValue(PARAMETER_VOLUME);
  490. delete oldPlugin;
  491. if (plugin->getHints() & PLUGIN_CAN_DRYWET)
  492. plugin->setDryWet(oldDryWet, true, true);
  493. if (plugin->getHints() & PLUGIN_CAN_VOLUME)
  494. plugin->setVolume(oldVolume, true, true);
  495. if (wasActive)
  496. plugin->setActive(true, true, true);
  497. callback(ENGINE_CALLBACK_RELOAD_ALL, id, 0, 0, 0.0f, nullptr);
  498. }
  499. else
  500. #endif
  501. {
  502. ++pData->curPluginCount;
  503. callback(ENGINE_CALLBACK_PLUGIN_ADDED, id, 0, 0, 0.0f, plugin->getName());
  504. #ifndef BUILD_BRIDGE
  505. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  506. pData->graph.addPlugin(plugin);
  507. #endif
  508. }
  509. return true;
  510. }
  511. 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)
  512. {
  513. return addPlugin(BINARY_NATIVE, ptype, filename, name, label, uniqueId, extra, 0x0);
  514. }
  515. bool CarlaEngine::removePlugin(const uint id)
  516. {
  517. CARLA_SAFE_ASSERT_RETURN_ERR(pData->isIdling == 0, "An operation is still being processed, please wait for it to finish");
  518. CARLA_SAFE_ASSERT_RETURN_ERR(pData->plugins != nullptr, "Invalid engine internal data");
  519. CARLA_SAFE_ASSERT_RETURN_ERR(pData->curPluginCount != 0, "Invalid engine internal data");
  520. CARLA_SAFE_ASSERT_RETURN_ERR(pData->nextAction.opcode == kEnginePostActionNull, "Invalid engine internal data");
  521. CARLA_SAFE_ASSERT_RETURN_ERR(id < pData->curPluginCount, "Invalid plugin Id");
  522. carla_debug("CarlaEngine::removePlugin(%i)", id);
  523. CarlaPlugin* const plugin(pData->plugins[id].plugin);
  524. CARLA_SAFE_ASSERT_RETURN_ERR(plugin != nullptr, "Could not find plugin to remove");
  525. CARLA_SAFE_ASSERT_RETURN_ERR(plugin->getId() == id, "Invalid engine internal data");
  526. pData->thread.stopThread(500);
  527. #ifndef BUILD_BRIDGE
  528. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  529. pData->graph.removePlugin(plugin);
  530. const bool lockWait(isRunning() /*&& pData->options.processMode != ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS*/);
  531. const ScopedActionLock sal(this, kEnginePostActionRemovePlugin, id, 0, lockWait);
  532. /*
  533. for (uint i=id; i < pData->curPluginCount; ++i)
  534. {
  535. CarlaPlugin* const plugin2(pData->plugins[i].plugin);
  536. CARLA_SAFE_ASSERT_BREAK(plugin2 != nullptr);
  537. plugin2->updateOscURL();
  538. }
  539. */
  540. # ifdef HAVE_LIBLO
  541. if (isOscControlRegistered())
  542. oscSend_control_remove_plugin(id);
  543. # endif
  544. #else
  545. pData->curPluginCount = 0;
  546. carla_zeroStruct(pData->plugins, 1);
  547. #endif
  548. delete plugin;
  549. if (isRunning() && ! pData->aboutToClose)
  550. pData->thread.startThread();
  551. callback(ENGINE_CALLBACK_PLUGIN_REMOVED, id, 0, 0, 0.0f, nullptr);
  552. return true;
  553. }
  554. bool CarlaEngine::removeAllPlugins()
  555. {
  556. CARLA_SAFE_ASSERT_RETURN_ERR(pData->isIdling == 0, "An operation is still being processed, please wait for it to finish");
  557. CARLA_SAFE_ASSERT_RETURN_ERR(pData->plugins != nullptr, "Invalid engine internal data");
  558. CARLA_SAFE_ASSERT_RETURN_ERR(pData->nextPluginId == pData->maxPluginNumber, "Invalid engine internal data");
  559. CARLA_SAFE_ASSERT_RETURN_ERR(pData->nextAction.opcode == kEnginePostActionNull, "Invalid engine internal data");
  560. carla_debug("CarlaEngine::removeAllPlugins()");
  561. if (pData->curPluginCount == 0)
  562. return true;
  563. pData->thread.stopThread(500);
  564. #ifndef BUILD_BRIDGE
  565. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  566. pData->graph.removeAllPlugins();
  567. #endif
  568. const uint32_t curPluginCount(pData->curPluginCount);
  569. const bool lockWait(isRunning());
  570. const ScopedActionLock sal(this, kEnginePostActionZeroCount, 0, 0, lockWait);
  571. callback(ENGINE_CALLBACK_IDLE, 0, 0, 0, 0.0f, nullptr);
  572. for (uint i=0; i < curPluginCount; ++i)
  573. {
  574. EnginePluginData& pluginData(pData->plugins[i]);
  575. if (pluginData.plugin != nullptr)
  576. {
  577. delete pluginData.plugin;
  578. pluginData.plugin = nullptr;
  579. }
  580. pluginData.insPeak[0] = 0.0f;
  581. pluginData.insPeak[1] = 0.0f;
  582. pluginData.outsPeak[0] = 0.0f;
  583. pluginData.outsPeak[1] = 0.0f;
  584. callback(ENGINE_CALLBACK_IDLE, 0, 0, 0, 0.0f, nullptr);
  585. }
  586. if (isRunning() && ! pData->aboutToClose)
  587. pData->thread.startThread();
  588. return true;
  589. }
  590. #ifndef BUILD_BRIDGE
  591. const char* CarlaEngine::renamePlugin(const uint id, const char* const newName)
  592. {
  593. CARLA_SAFE_ASSERT_RETURN_ERRN(pData->isIdling == 0, "An operation is still being processed, please wait for it to finish");
  594. CARLA_SAFE_ASSERT_RETURN_ERRN(pData->plugins != nullptr, "Invalid engine internal data");
  595. CARLA_SAFE_ASSERT_RETURN_ERRN(pData->curPluginCount != 0, "Invalid engine internal data");
  596. CARLA_SAFE_ASSERT_RETURN_ERRN(pData->nextAction.opcode == kEnginePostActionNull, "Invalid engine internal data");
  597. CARLA_SAFE_ASSERT_RETURN_ERRN(id < pData->curPluginCount, "Invalid plugin Id");
  598. CARLA_SAFE_ASSERT_RETURN_ERRN(newName != nullptr && newName[0] != '\0', "Invalid plugin name");
  599. carla_debug("CarlaEngine::renamePlugin(%i, \"%s\")", id, newName);
  600. CarlaPlugin* const plugin(pData->plugins[id].plugin);
  601. CARLA_SAFE_ASSERT_RETURN_ERRN(plugin != nullptr, "Could not find plugin to rename");
  602. CARLA_SAFE_ASSERT_RETURN_ERRN(plugin->getId() == id, "Invalid engine internal data");
  603. if (const char* const name = getUniquePluginName(newName))
  604. {
  605. plugin->setName(name);
  606. return name;
  607. }
  608. setLastError("Unable to get new unique plugin name");
  609. return nullptr;
  610. }
  611. bool CarlaEngine::clonePlugin(const uint id)
  612. {
  613. CARLA_SAFE_ASSERT_RETURN_ERR(pData->isIdling == 0, "An operation is still being processed, please wait for it to finish");
  614. CARLA_SAFE_ASSERT_RETURN_ERR(pData->plugins != nullptr, "Invalid engine internal data");
  615. CARLA_SAFE_ASSERT_RETURN_ERR(pData->curPluginCount != 0, "Invalid engine internal data");
  616. CARLA_SAFE_ASSERT_RETURN_ERR(pData->nextAction.opcode == kEnginePostActionNull, "Invalid engine internal data");
  617. CARLA_SAFE_ASSERT_RETURN_ERR(id < pData->curPluginCount, "Invalid plugin Id");
  618. carla_debug("CarlaEngine::clonePlugin(%i)", id);
  619. CarlaPlugin* const plugin(pData->plugins[id].plugin);
  620. CARLA_SAFE_ASSERT_RETURN_ERR(plugin != nullptr, "Could not find plugin to clone");
  621. CARLA_SAFE_ASSERT_RETURN_ERR(plugin->getId() == id, "Invalid engine internal data");
  622. char label[STR_MAX+1];
  623. carla_zeroChar(label, STR_MAX+1);
  624. plugin->getLabel(label);
  625. const uint pluginCountBefore(pData->curPluginCount);
  626. if (! addPlugin(plugin->getBinaryType(), plugin->getType(),
  627. plugin->getFilename(), plugin->getName(), label, plugin->getUniqueId(),
  628. plugin->getExtraStuff(), plugin->getOptionsEnabled()))
  629. return false;
  630. CARLA_SAFE_ASSERT_RETURN_ERR(pluginCountBefore+1 == pData->curPluginCount, "No new plugin found");
  631. if (CarlaPlugin* const newPlugin = pData->plugins[pluginCountBefore].plugin)
  632. newPlugin->loadStateSave(plugin->getStateSave());
  633. return true;
  634. }
  635. bool CarlaEngine::replacePlugin(const uint id) noexcept
  636. {
  637. CARLA_SAFE_ASSERT_RETURN_ERR(pData->isIdling == 0, "An operation is still being processed, please wait for it to finish");
  638. CARLA_SAFE_ASSERT_RETURN_ERR(pData->plugins != nullptr, "Invalid engine internal data");
  639. CARLA_SAFE_ASSERT_RETURN_ERR(pData->curPluginCount != 0, "Invalid engine internal data");
  640. CARLA_SAFE_ASSERT_RETURN_ERR(pData->nextAction.opcode == kEnginePostActionNull, "Invalid engine internal data");
  641. carla_debug("CarlaEngine::replacePlugin(%i)", id);
  642. // might use this to reset
  643. if (id == pData->maxPluginNumber)
  644. {
  645. pData->nextPluginId = pData->maxPluginNumber;
  646. return true;
  647. }
  648. CARLA_SAFE_ASSERT_RETURN_ERR(id < pData->curPluginCount, "Invalid plugin Id");
  649. CarlaPlugin* const plugin(pData->plugins[id].plugin);
  650. CARLA_SAFE_ASSERT_RETURN_ERR(plugin != nullptr, "Could not find plugin to replace");
  651. CARLA_SAFE_ASSERT_RETURN_ERR(plugin->getId() == id, "Invalid engine internal data");
  652. pData->nextPluginId = id;
  653. return true;
  654. }
  655. bool CarlaEngine::switchPlugins(const uint idA, const uint idB) noexcept
  656. {
  657. CARLA_SAFE_ASSERT_RETURN_ERR(pData->isIdling == 0, "An operation is still being processed, please wait for it to finish");
  658. CARLA_SAFE_ASSERT_RETURN_ERR(pData->plugins != nullptr, "Invalid engine internal data");
  659. CARLA_SAFE_ASSERT_RETURN_ERR(pData->curPluginCount >= 2, "Invalid engine internal data");
  660. CARLA_SAFE_ASSERT_RETURN_ERR(pData->nextAction.opcode == kEnginePostActionNull, "Invalid engine internal data");
  661. CARLA_SAFE_ASSERT_RETURN_ERR(idA != idB, "Invalid operation, cannot switch plugin with itself");
  662. CARLA_SAFE_ASSERT_RETURN_ERR(idA < pData->curPluginCount, "Invalid plugin Id");
  663. CARLA_SAFE_ASSERT_RETURN_ERR(idB < pData->curPluginCount, "Invalid plugin Id");
  664. carla_debug("CarlaEngine::switchPlugins(%i)", idA, idB);
  665. {
  666. CarlaPlugin* const pluginA(pData->plugins[idA].plugin);
  667. CarlaPlugin* const pluginB(pData->plugins[idB].plugin);
  668. CARLA_SAFE_ASSERT_RETURN_ERR(pluginA != nullptr, "Could not find plugin to switch");
  669. CARLA_SAFE_ASSERT_RETURN_ERR(pluginA != nullptr, "Could not find plugin to switch");
  670. CARLA_SAFE_ASSERT_RETURN_ERR(pluginA->getId() == idA, "Invalid engine internal data");
  671. CARLA_SAFE_ASSERT_RETURN_ERR(pluginB->getId() == idB, "Invalid engine internal data");
  672. pData->thread.stopThread(500);
  673. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  674. pData->graph.replacePlugin(pluginA, pluginB);
  675. }
  676. const bool lockWait(isRunning() /*&& pData->options.processMode != ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS*/);
  677. const ScopedActionLock sal(this, kEnginePostActionSwitchPlugins, idA, idB, lockWait);
  678. /*
  679. CarlaPlugin* const pluginA(pData->plugins[idA].plugin);
  680. CarlaPlugin* const pluginB(pData->plugins[idB].plugin);
  681. if (pluginA != nullptr && pluginB != nullptr)
  682. {
  683. pluginA->updateOscURL();
  684. pluginB->updateOscURL();
  685. }
  686. */
  687. // TODO
  688. //if (isOscControlRegistered())
  689. // oscSend_control_switch_plugins(idA, idB);
  690. if (isRunning() && ! pData->aboutToClose)
  691. pData->thread.startThread();
  692. return true;
  693. }
  694. #endif
  695. CarlaPlugin* CarlaEngine::getPlugin(const uint id) const noexcept
  696. {
  697. CARLA_SAFE_ASSERT_RETURN_ERRN(pData->plugins != nullptr, "Invalid engine internal data");
  698. CARLA_SAFE_ASSERT_RETURN_ERRN(pData->curPluginCount != 0, "Invalid engine internal data");
  699. CARLA_SAFE_ASSERT_RETURN_ERRN(pData->nextAction.opcode == kEnginePostActionNull, "Invalid engine internal data");
  700. CARLA_SAFE_ASSERT_RETURN_ERRN(id < pData->curPluginCount, "Invalid plugin Id");
  701. return pData->plugins[id].plugin;
  702. }
  703. CarlaPlugin* CarlaEngine::getPluginUnchecked(const uint id) const noexcept
  704. {
  705. return pData->plugins[id].plugin;
  706. }
  707. const char* CarlaEngine::getUniquePluginName(const char* const name) const
  708. {
  709. CARLA_SAFE_ASSERT_RETURN(pData->nextAction.opcode == kEnginePostActionNull, nullptr);
  710. CARLA_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0', nullptr);
  711. carla_debug("CarlaEngine::getUniquePluginName(\"%s\")", name);
  712. CarlaString sname;
  713. sname = name;
  714. if (sname.isEmpty())
  715. {
  716. sname = "(No name)";
  717. return sname.dup();
  718. }
  719. const std::size_t maxNameSize(carla_minWithBase<uint>(getMaxClientNameSize(), 0xff, 6U) - 6); // 6 = strlen(" (10)") + 1
  720. if (maxNameSize == 0 || ! isRunning())
  721. return sname.dup();
  722. sname.truncate(maxNameSize);
  723. sname.replace(':', '.'); // ':' is used in JACK1 to split client/port names
  724. for (uint i=0; i < pData->curPluginCount; ++i)
  725. {
  726. CARLA_SAFE_ASSERT_BREAK(pData->plugins[i].plugin != nullptr);
  727. // Check if unique name doesn't exist
  728. if (const char* const pluginName = pData->plugins[i].plugin->getName())
  729. {
  730. if (sname != pluginName)
  731. continue;
  732. }
  733. // Check if string has already been modified
  734. {
  735. const std::size_t len(sname.length());
  736. // 1 digit, ex: " (2)"
  737. if (sname[len-4] == ' ' && sname[len-3] == '(' && sname.isDigit(len-2) && sname[len-1] == ')')
  738. {
  739. const int number = sname[len-2] - '0';
  740. if (number == 9)
  741. {
  742. // next number is 10, 2 digits
  743. sname.truncate(len-4);
  744. sname += " (10)";
  745. //sname.replace(" (9)", " (10)");
  746. }
  747. else
  748. sname[len-2] = char('0' + number + 1);
  749. continue;
  750. }
  751. // 2 digits, ex: " (11)"
  752. if (sname[len-5] == ' ' && sname[len-4] == '(' && sname.isDigit(len-3) && sname.isDigit(len-2) && sname[len-1] == ')')
  753. {
  754. char n2 = sname[len-2];
  755. char n3 = sname[len-3];
  756. if (n2 == '9')
  757. {
  758. n2 = '0';
  759. n3 = static_cast<char>(n3 + 1);
  760. }
  761. else
  762. n2 = static_cast<char>(n2 + 1);
  763. sname[len-2] = n2;
  764. sname[len-3] = n3;
  765. continue;
  766. }
  767. }
  768. // Modify string if not
  769. sname += " (2)";
  770. }
  771. return sname.dup();
  772. }
  773. // -----------------------------------------------------------------------
  774. // Project management
  775. bool CarlaEngine::loadFile(const char* const filename)
  776. {
  777. CARLA_SAFE_ASSERT_RETURN_ERR(pData->isIdling == 0, "An operation is still being processed, please wait for it to finish");
  778. CARLA_SAFE_ASSERT_RETURN_ERR(filename != nullptr && filename[0] != '\0', "Invalid filename");
  779. carla_debug("CarlaEngine::loadFile(\"%s\")", filename);
  780. const String jfilename = String(CharPointer_UTF8(filename));
  781. File file(jfilename);
  782. CARLA_SAFE_ASSERT_RETURN_ERR(file.existsAsFile(), "Requested file does not exist or is not a readable file");
  783. CarlaString baseName(file.getFileNameWithoutExtension().toRawUTF8());
  784. CarlaString extension(file.getFileExtension().replace(".","").toLowerCase().toRawUTF8());
  785. const uint curPluginId(pData->nextPluginId < pData->curPluginCount ? pData->nextPluginId : pData->curPluginCount);
  786. // -------------------------------------------------------------------
  787. if (extension == "carxp" || extension == "carxs")
  788. return loadProject(filename);
  789. // -------------------------------------------------------------------
  790. if (extension == "gig")
  791. return addPlugin(PLUGIN_GIG, filename, baseName, baseName, 0, nullptr);
  792. if (extension == "sf2")
  793. return addPlugin(PLUGIN_SF2, filename, baseName, baseName, 0, nullptr);
  794. if (extension == "sfz")
  795. return addPlugin(PLUGIN_SFZ, filename, baseName, baseName, 0, nullptr);
  796. // -------------------------------------------------------------------
  797. if (extension == "aif" || extension == "aiff" || extension == "bwf" || extension == "flac" || extension == "ogg" || extension == "wav")
  798. {
  799. if (addPlugin(PLUGIN_INTERNAL, nullptr, baseName, "audiofile", 0, nullptr))
  800. {
  801. if (CarlaPlugin* const plugin = getPlugin(curPluginId))
  802. plugin->setCustomData(CUSTOM_DATA_TYPE_STRING, "file", filename, true);
  803. return true;
  804. }
  805. return false;
  806. }
  807. // -------------------------------------------------------------------
  808. if (extension == "mid" || extension == "midi")
  809. {
  810. if (addPlugin(PLUGIN_INTERNAL, nullptr, baseName, "midifile", 0, nullptr))
  811. {
  812. if (CarlaPlugin* const plugin = getPlugin(curPluginId))
  813. plugin->setCustomData(CUSTOM_DATA_TYPE_STRING, "file", filename, true);
  814. return true;
  815. }
  816. return false;
  817. }
  818. // -------------------------------------------------------------------
  819. // ZynAddSubFX
  820. if (extension == "xmz" || extension == "xiz")
  821. {
  822. #ifdef HAVE_ZYN_DEPS
  823. CarlaString nicerName("Zyn - ");
  824. const std::size_t sep(baseName.find('-')+1);
  825. if (sep < baseName.length())
  826. nicerName += baseName.buffer()+sep;
  827. else
  828. nicerName += baseName;
  829. //nicerName
  830. if (addPlugin(PLUGIN_INTERNAL, nullptr, nicerName, "zynaddsubfx", 0, nullptr))
  831. {
  832. callback(ENGINE_CALLBACK_UI_STATE_CHANGED, curPluginId, 0, 0, 0.0f, nullptr);
  833. if (CarlaPlugin* const plugin = getPlugin(curPluginId))
  834. plugin->setCustomData(CUSTOM_DATA_TYPE_STRING, (extension == "xmz") ? "CarlaAlternateFile1" : "CarlaAlternateFile2", filename, true);
  835. return true;
  836. }
  837. return false;
  838. #else
  839. setLastError("This Carla build does not have ZynAddSubFX support");
  840. return false;
  841. #endif
  842. }
  843. // -------------------------------------------------------------------
  844. setLastError("Unknown file extension");
  845. return false;
  846. }
  847. bool CarlaEngine::loadProject(const char* const filename)
  848. {
  849. CARLA_SAFE_ASSERT_RETURN_ERR(pData->isIdling == 0, "An operation is still being processed, please wait for it to finish");
  850. CARLA_SAFE_ASSERT_RETURN_ERR(filename != nullptr && filename[0] != '\0', "Invalid filename");
  851. carla_debug("CarlaEngine::loadProject(\"%s\")", filename);
  852. const String jfilename = String(CharPointer_UTF8(filename));
  853. File file(jfilename);
  854. CARLA_SAFE_ASSERT_RETURN_ERR(file.existsAsFile(), "Requested file does not exist or is not a readable file");
  855. XmlDocument xml(file);
  856. return loadProjectInternal(xml);
  857. }
  858. bool CarlaEngine::saveProject(const char* const filename)
  859. {
  860. CARLA_SAFE_ASSERT_RETURN_ERR(filename != nullptr && filename[0] != '\0', "Invalid filename");
  861. carla_debug("CarlaEngine::saveProject(\"%s\")", filename);
  862. MemoryOutputStream out;
  863. saveProjectInternal(out);
  864. const String jfilename = String(CharPointer_UTF8(filename));
  865. File file(jfilename);
  866. if (file.replaceWithData(out.getData(), out.getDataSize()))
  867. return true;
  868. setLastError("Failed to write file");
  869. return false;
  870. }
  871. // -----------------------------------------------------------------------
  872. // Information (base)
  873. uint CarlaEngine::getHints() const noexcept
  874. {
  875. return pData->hints;
  876. }
  877. uint32_t CarlaEngine::getBufferSize() const noexcept
  878. {
  879. return pData->bufferSize;
  880. }
  881. double CarlaEngine::getSampleRate() const noexcept
  882. {
  883. return pData->sampleRate;
  884. }
  885. const char* CarlaEngine::getName() const noexcept
  886. {
  887. return pData->name;
  888. }
  889. EngineProcessMode CarlaEngine::getProccessMode() const noexcept
  890. {
  891. return pData->options.processMode;
  892. }
  893. const EngineOptions& CarlaEngine::getOptions() const noexcept
  894. {
  895. return pData->options;
  896. }
  897. const EngineTimeInfo& CarlaEngine::getTimeInfo() const noexcept
  898. {
  899. return pData->timeInfo;
  900. }
  901. // -----------------------------------------------------------------------
  902. // Information (peaks)
  903. float CarlaEngine::getInputPeak(const uint pluginId, const bool isLeft) const noexcept
  904. {
  905. CARLA_SAFE_ASSERT_RETURN(pluginId < pData->curPluginCount, 0.0f);
  906. return pData->plugins[pluginId].insPeak[isLeft ? 0 : 1];
  907. }
  908. float CarlaEngine::getOutputPeak(const uint pluginId, const bool isLeft) const noexcept
  909. {
  910. CARLA_SAFE_ASSERT_RETURN(pluginId < pData->curPluginCount, 0.0f);
  911. return pData->plugins[pluginId].outsPeak[isLeft ? 0 : 1];
  912. }
  913. // -----------------------------------------------------------------------
  914. // Callback
  915. void CarlaEngine::callback(const EngineCallbackOpcode action, const uint pluginId, const int value1, const int value2, const float value3, const char* const valueStr) noexcept
  916. {
  917. #ifdef DEBUG
  918. if (action != ENGINE_CALLBACK_IDLE)
  919. carla_debug("CarlaEngine::callback(%i:%s, %i, %i, %i, %f, \"%s\")", action, EngineCallbackOpcode2Str(action), pluginId, value1, value2, value3, valueStr);
  920. #endif
  921. #ifdef BUILD_BRIDGE
  922. if (pData->isIdling)
  923. #else
  924. if (pData->isIdling && action != ENGINE_CALLBACK_PATCHBAY_CLIENT_DATA_CHANGED)
  925. #endif
  926. {
  927. carla_stdout("callback while idling (%i:%s, %i, %i, %i, %f, \"%s\")", action, EngineCallbackOpcode2Str(action), pluginId, value1, value2, value3, valueStr);
  928. }
  929. if (pData->callback != nullptr)
  930. {
  931. if (action == ENGINE_CALLBACK_IDLE)
  932. ++pData->isIdling;
  933. try {
  934. pData->callback(pData->callbackPtr, action, pluginId, value1, value2, value3, valueStr);
  935. } CARLA_SAFE_EXCEPTION("callback");
  936. if (action == ENGINE_CALLBACK_IDLE)
  937. --pData->isIdling;
  938. }
  939. }
  940. void CarlaEngine::setCallback(const EngineCallbackFunc func, void* const ptr) noexcept
  941. {
  942. carla_debug("CarlaEngine::setCallback(%p, %p)", func, ptr);
  943. pData->callback = func;
  944. pData->callbackPtr = ptr;
  945. }
  946. // -----------------------------------------------------------------------
  947. // File Callback
  948. const char* CarlaEngine::runFileCallback(const FileCallbackOpcode action, const bool isDir, const char* const title, const char* const filter) noexcept
  949. {
  950. CARLA_SAFE_ASSERT_RETURN(title != nullptr && title[0] != '\0', nullptr);
  951. CARLA_SAFE_ASSERT_RETURN(filter != nullptr, nullptr);
  952. carla_debug("CarlaEngine::runFileCallback(%i:%s, %s, \"%s\", \"%s\")", action, FileCallbackOpcode2Str(action), bool2str(isDir), title, filter);
  953. const char* ret = nullptr;
  954. if (pData->fileCallback != nullptr)
  955. {
  956. try {
  957. ret = pData->fileCallback(pData->fileCallbackPtr, action, isDir, title, filter);
  958. } CARLA_SAFE_EXCEPTION("runFileCallback");
  959. }
  960. return ret;
  961. }
  962. void CarlaEngine::setFileCallback(const FileCallbackFunc func, void* const ptr) noexcept
  963. {
  964. carla_debug("CarlaEngine::setFileCallback(%p, %p)", func, ptr);
  965. pData->fileCallback = func;
  966. pData->fileCallbackPtr = ptr;
  967. }
  968. // -----------------------------------------------------------------------
  969. // Transport
  970. void CarlaEngine::transportPlay() noexcept
  971. {
  972. pData->time.playing = true;
  973. }
  974. void CarlaEngine::transportPause() noexcept
  975. {
  976. pData->time.playing = false;
  977. }
  978. void CarlaEngine::transportRelocate(const uint64_t frame) noexcept
  979. {
  980. pData->time.frame = frame;
  981. }
  982. // -----------------------------------------------------------------------
  983. // Error handling
  984. const char* CarlaEngine::getLastError() const noexcept
  985. {
  986. return pData->lastError;
  987. }
  988. void CarlaEngine::setLastError(const char* const error) const noexcept
  989. {
  990. pData->lastError = error;
  991. }
  992. void CarlaEngine::setAboutToClose() noexcept
  993. {
  994. carla_debug("CarlaEngine::setAboutToClose()");
  995. pData->aboutToClose = true;
  996. }
  997. // -----------------------------------------------------------------------
  998. // Global options
  999. void CarlaEngine::setOption(const EngineOption option, const int value, const char* const valueStr) noexcept
  1000. {
  1001. carla_debug("CarlaEngine::setOption(%i:%s, %i, \"%s\")", option, EngineOption2Str(option), value, valueStr);
  1002. if (isRunning() && (option == ENGINE_OPTION_PROCESS_MODE || option == ENGINE_OPTION_AUDIO_NUM_PERIODS || option == ENGINE_OPTION_AUDIO_DEVICE))
  1003. return carla_stderr("CarlaEngine::setOption(%i:%s, %i, \"%s\") - Cannot set this option while engine is running!", option, EngineOption2Str(option), value, valueStr);
  1004. // do not un-force stereo for rack mode
  1005. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK && option == ENGINE_OPTION_FORCE_STEREO && value != 0)
  1006. return;
  1007. switch (option)
  1008. {
  1009. case ENGINE_OPTION_DEBUG:
  1010. break;
  1011. case ENGINE_OPTION_PROCESS_MODE:
  1012. CARLA_SAFE_ASSERT_RETURN(value >= ENGINE_PROCESS_MODE_SINGLE_CLIENT && value <= ENGINE_PROCESS_MODE_BRIDGE,);
  1013. pData->options.processMode = static_cast<EngineProcessMode>(value);
  1014. break;
  1015. case ENGINE_OPTION_TRANSPORT_MODE:
  1016. CARLA_SAFE_ASSERT_RETURN(value >= ENGINE_TRANSPORT_MODE_INTERNAL && value <= ENGINE_TRANSPORT_MODE_BRIDGE,);
  1017. pData->options.transportMode = static_cast<EngineTransportMode>(value);
  1018. break;
  1019. case ENGINE_OPTION_FORCE_STEREO:
  1020. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  1021. pData->options.forceStereo = (value != 0);
  1022. break;
  1023. case ENGINE_OPTION_PREFER_PLUGIN_BRIDGES:
  1024. #ifdef BUILD_BRIDGE
  1025. CARLA_SAFE_ASSERT_RETURN(value == 0,);
  1026. #else
  1027. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  1028. #endif
  1029. pData->options.preferPluginBridges = (value != 0);
  1030. break;
  1031. case ENGINE_OPTION_PREFER_UI_BRIDGES:
  1032. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  1033. pData->options.preferUiBridges = (value != 0);
  1034. break;
  1035. case ENGINE_OPTION_UIS_ALWAYS_ON_TOP:
  1036. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  1037. pData->options.uisAlwaysOnTop = (value != 0);
  1038. break;
  1039. case ENGINE_OPTION_MAX_PARAMETERS:
  1040. CARLA_SAFE_ASSERT_RETURN(value >= 0,);
  1041. pData->options.maxParameters = static_cast<uint>(value);
  1042. break;
  1043. case ENGINE_OPTION_UI_BRIDGES_TIMEOUT:
  1044. CARLA_SAFE_ASSERT_RETURN(value >= 0,);
  1045. pData->options.uiBridgesTimeout = static_cast<uint>(value);
  1046. break;
  1047. case ENGINE_OPTION_AUDIO_NUM_PERIODS:
  1048. CARLA_SAFE_ASSERT_RETURN(value >= 2 && value <= 3,);
  1049. pData->options.audioNumPeriods = static_cast<uint>(value);
  1050. break;
  1051. case ENGINE_OPTION_AUDIO_BUFFER_SIZE:
  1052. CARLA_SAFE_ASSERT_RETURN(value >= 8,);
  1053. pData->options.audioBufferSize = static_cast<uint>(value);
  1054. break;
  1055. case ENGINE_OPTION_AUDIO_SAMPLE_RATE:
  1056. CARLA_SAFE_ASSERT_RETURN(value >= 22050,);
  1057. pData->options.audioSampleRate = static_cast<uint>(value);
  1058. break;
  1059. case ENGINE_OPTION_AUDIO_DEVICE:
  1060. CARLA_SAFE_ASSERT_RETURN(valueStr != nullptr,);
  1061. if (pData->options.audioDevice != nullptr)
  1062. delete[] pData->options.audioDevice;
  1063. pData->options.audioDevice = carla_strdup_safe(valueStr);
  1064. break;
  1065. case ENGINE_OPTION_PLUGIN_PATH:
  1066. CARLA_SAFE_ASSERT_RETURN(value > PLUGIN_NONE,);
  1067. CARLA_SAFE_ASSERT_RETURN(value <= PLUGIN_SFZ,);
  1068. switch (value)
  1069. {
  1070. case PLUGIN_LADSPA:
  1071. if (pData->options.pathLADSPA != nullptr)
  1072. delete[] pData->options.pathLADSPA;
  1073. if (valueStr != nullptr)
  1074. pData->options.pathLADSPA = carla_strdup_safe(valueStr);
  1075. else
  1076. pData->options.pathLADSPA = nullptr;
  1077. break;
  1078. case PLUGIN_DSSI:
  1079. if (pData->options.pathDSSI != nullptr)
  1080. delete[] pData->options.pathDSSI;
  1081. if (valueStr != nullptr)
  1082. pData->options.pathDSSI = carla_strdup_safe(valueStr);
  1083. else
  1084. pData->options.pathDSSI = nullptr;
  1085. break;
  1086. case PLUGIN_LV2:
  1087. if (pData->options.pathLV2 != nullptr)
  1088. delete[] pData->options.pathLV2;
  1089. if (valueStr != nullptr)
  1090. pData->options.pathLV2 = carla_strdup_safe(valueStr);
  1091. else
  1092. pData->options.pathLV2 = nullptr;
  1093. break;
  1094. case PLUGIN_VST2:
  1095. if (pData->options.pathVST2 != nullptr)
  1096. delete[] pData->options.pathVST2;
  1097. if (valueStr != nullptr)
  1098. pData->options.pathVST2 = carla_strdup_safe(valueStr);
  1099. else
  1100. pData->options.pathVST2 = nullptr;
  1101. break;
  1102. case PLUGIN_VST3:
  1103. if (pData->options.pathVST3 != nullptr)
  1104. delete[] pData->options.pathVST3;
  1105. if (valueStr != nullptr)
  1106. pData->options.pathVST3 = carla_strdup_safe(valueStr);
  1107. else
  1108. pData->options.pathVST3 = nullptr;
  1109. break;
  1110. case PLUGIN_GIG:
  1111. if (pData->options.pathGIG != nullptr)
  1112. delete[] pData->options.pathGIG;
  1113. if (valueStr != nullptr)
  1114. pData->options.pathGIG = carla_strdup_safe(valueStr);
  1115. else
  1116. pData->options.pathGIG = nullptr;
  1117. break;
  1118. case PLUGIN_SF2:
  1119. if (pData->options.pathSF2 != nullptr)
  1120. delete[] pData->options.pathSF2;
  1121. if (valueStr != nullptr)
  1122. pData->options.pathSF2 = carla_strdup_safe(valueStr);
  1123. else
  1124. pData->options.pathSF2 = nullptr;
  1125. break;
  1126. case PLUGIN_SFZ:
  1127. if (pData->options.pathSFZ != nullptr)
  1128. delete[] pData->options.pathSFZ;
  1129. if (valueStr != nullptr)
  1130. pData->options.pathSFZ = carla_strdup_safe(valueStr);
  1131. else
  1132. pData->options.pathSFZ = nullptr;
  1133. break;
  1134. default:
  1135. return carla_stderr("CarlaEngine::setOption(%i:%s, %i, \"%s\") - Invalid plugin type", option, EngineOption2Str(option), value, valueStr);
  1136. break;
  1137. }
  1138. break;
  1139. case ENGINE_OPTION_PATH_BINARIES:
  1140. CARLA_SAFE_ASSERT_RETURN(valueStr != nullptr && valueStr[0] != '\0',);
  1141. if (pData->options.binaryDir != nullptr)
  1142. delete[] pData->options.binaryDir;
  1143. pData->options.binaryDir = carla_strdup_safe(valueStr);
  1144. break;
  1145. case ENGINE_OPTION_PATH_RESOURCES:
  1146. CARLA_SAFE_ASSERT_RETURN(valueStr != nullptr && valueStr[0] != '\0',);
  1147. if (pData->options.resourceDir != nullptr)
  1148. delete[] pData->options.resourceDir;
  1149. pData->options.resourceDir = carla_strdup_safe(valueStr);
  1150. break;
  1151. case ENGINE_OPTION_PREVENT_BAD_BEHAVIOUR:
  1152. CARLA_SAFE_ASSERT_RETURN(pData->options.binaryDir != nullptr && pData->options.binaryDir[0] != '\0',);
  1153. #ifdef CARLA_OS_LINUX
  1154. if (value != 0)
  1155. {
  1156. CarlaString interposerPath(CarlaString(pData->options.binaryDir) + CARLA_OS_SEP_STR "libcarla_interposer.so");
  1157. ::setenv("LD_PRELOAD", interposerPath.buffer(), 1);
  1158. }
  1159. else
  1160. {
  1161. ::unsetenv("LD_PRELOAD");
  1162. }
  1163. #endif
  1164. break;
  1165. case ENGINE_OPTION_FRONTEND_WIN_ID:
  1166. CARLA_SAFE_ASSERT_RETURN(valueStr != nullptr && valueStr[0] != '\0',);
  1167. const long long winId(std::strtoll(valueStr, nullptr, 16));
  1168. CARLA_SAFE_ASSERT_RETURN(winId >= 0,);
  1169. pData->options.frontendWinId = static_cast<uintptr_t>(winId);
  1170. break;
  1171. }
  1172. }
  1173. #ifdef HAVE_LIBLO
  1174. // -----------------------------------------------------------------------
  1175. // OSC Stuff
  1176. # ifndef BUILD_BRIDGE
  1177. bool CarlaEngine::isOscControlRegistered() const noexcept
  1178. {
  1179. return pData->osc.isControlRegistered();
  1180. }
  1181. # endif
  1182. void CarlaEngine::idleOsc() const noexcept
  1183. {
  1184. pData->osc.idle();
  1185. }
  1186. const char* CarlaEngine::getOscServerPathTCP() const noexcept
  1187. {
  1188. return pData->osc.getServerPathTCP();
  1189. }
  1190. const char* CarlaEngine::getOscServerPathUDP() const noexcept
  1191. {
  1192. return pData->osc.getServerPathUDP();
  1193. }
  1194. #endif
  1195. // -----------------------------------------------------------------------
  1196. // Helper functions
  1197. EngineEvent* CarlaEngine::getInternalEventBuffer(const bool isInput) const noexcept
  1198. {
  1199. return isInput ? pData->events.in : pData->events.out;
  1200. }
  1201. // -----------------------------------------------------------------------
  1202. // Internal stuff
  1203. void CarlaEngine::bufferSizeChanged(const uint32_t newBufferSize)
  1204. {
  1205. carla_debug("CarlaEngine::bufferSizeChanged(%i)", newBufferSize);
  1206. #ifndef BUILD_BRIDGE
  1207. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK ||
  1208. pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  1209. {
  1210. pData->graph.setBufferSize(newBufferSize);
  1211. }
  1212. #endif
  1213. for (uint i=0; i < pData->curPluginCount; ++i)
  1214. {
  1215. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1216. if (plugin != nullptr && plugin->isEnabled())
  1217. plugin->bufferSizeChanged(newBufferSize);
  1218. }
  1219. callback(ENGINE_CALLBACK_BUFFER_SIZE_CHANGED, 0, static_cast<int>(newBufferSize), 0, 0.0f, nullptr);
  1220. }
  1221. void CarlaEngine::sampleRateChanged(const double newSampleRate)
  1222. {
  1223. carla_debug("CarlaEngine::sampleRateChanged(%g)", newSampleRate);
  1224. #ifndef BUILD_BRIDGE
  1225. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK ||
  1226. pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  1227. {
  1228. pData->graph.setSampleRate(newSampleRate);
  1229. }
  1230. #endif
  1231. for (uint i=0; i < pData->curPluginCount; ++i)
  1232. {
  1233. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1234. if (plugin != nullptr && plugin->isEnabled())
  1235. plugin->sampleRateChanged(newSampleRate);
  1236. }
  1237. callback(ENGINE_CALLBACK_SAMPLE_RATE_CHANGED, 0, 0, 0, static_cast<float>(newSampleRate), nullptr);
  1238. }
  1239. void CarlaEngine::offlineModeChanged(const bool isOfflineNow)
  1240. {
  1241. carla_debug("CarlaEngine::offlineModeChanged(%s)", bool2str(isOfflineNow));
  1242. #ifndef BUILD_BRIDGE
  1243. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK ||
  1244. pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  1245. {
  1246. pData->graph.setOffline(isOfflineNow);
  1247. }
  1248. #endif
  1249. for (uint i=0; i < pData->curPluginCount; ++i)
  1250. {
  1251. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1252. if (plugin != nullptr && plugin->isEnabled())
  1253. plugin->offlineModeChanged(isOfflineNow);
  1254. }
  1255. }
  1256. void CarlaEngine::setPluginPeaks(const uint pluginId, float const inPeaks[2], float const outPeaks[2]) noexcept
  1257. {
  1258. EnginePluginData& pluginData(pData->plugins[pluginId]);
  1259. pluginData.insPeak[0] = inPeaks[0];
  1260. pluginData.insPeak[1] = inPeaks[1];
  1261. pluginData.outsPeak[0] = outPeaks[0];
  1262. pluginData.outsPeak[1] = outPeaks[1];
  1263. }
  1264. void CarlaEngine::saveProjectInternal(juce::MemoryOutputStream& outStream) const
  1265. {
  1266. // send initial prepareForSave first, giving time for bridges to act
  1267. for (uint i=0; i < pData->curPluginCount; ++i)
  1268. {
  1269. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1270. if (plugin != nullptr && plugin->isEnabled())
  1271. {
  1272. #ifndef BUILD_BRIDGE
  1273. // deactivate bridge client-side ping check, since some plugins block during save
  1274. if (plugin->getHints() & PLUGIN_IS_BRIDGE)
  1275. plugin->setCustomData(CUSTOM_DATA_TYPE_STRING, "__CarlaPingOnOff__", "false", false);
  1276. #endif
  1277. plugin->prepareForSave();
  1278. }
  1279. }
  1280. outStream << "<?xml version='1.0' encoding='UTF-8'?>\n";
  1281. outStream << "<!DOCTYPE CARLA-PROJECT>\n";
  1282. outStream << "<CARLA-PROJECT VERSION='2.0'>\n";
  1283. const bool isPlugin(std::strcmp(getCurrentDriverName(), "Plugin") == 0);
  1284. const EngineOptions& options(pData->options);
  1285. MemoryOutputStream outSettings(1024);
  1286. // save appropriate engine settings
  1287. outSettings << " <EngineSettings>\n";
  1288. //processMode
  1289. //transportMode
  1290. outSettings << " <ForceStereo>" << bool2str(options.forceStereo) << "</ForceStereo>\n";
  1291. outSettings << " <PreferPluginBridges>" << bool2str(options.preferPluginBridges) << "</PreferPluginBridges>\n";
  1292. outSettings << " <PreferUiBridges>" << bool2str(options.preferUiBridges) << "</PreferUiBridges>\n";
  1293. outSettings << " <UIsAlwaysOnTop>" << bool2str(options.uisAlwaysOnTop) << "</UIsAlwaysOnTop>\n";
  1294. outSettings << " <MaxParameters>" << String(options.maxParameters) << "</MaxParameters>\n";
  1295. outSettings << " <UIBridgesTimeout>" << String(options.uiBridgesTimeout) << "</UIBridgesTimeout>\n";
  1296. if (isPlugin)
  1297. {
  1298. outSettings << " <LADSPA_PATH>" << xmlSafeString(options.pathLADSPA, true) << "</LADSPA_PATH>\n";
  1299. outSettings << " <DSSI_PATH>" << xmlSafeString(options.pathDSSI, true) << "</DSSI_PATH>\n";
  1300. outSettings << " <LV2_PATH>" << xmlSafeString(options.pathLV2, true) << "</LV2_PATH>\n";
  1301. outSettings << " <VST2_PATH>" << xmlSafeString(options.pathVST2, true) << "</VST2_PATH>\n";
  1302. outSettings << " <VST3_PATH>" << xmlSafeString(options.pathVST3, true) << "</VST3_PATH>\n";
  1303. outSettings << " <GIG_PATH>" << xmlSafeString(options.pathGIG, true) << "</GIG_PATH>\n";
  1304. outSettings << " <SF2_PATH>" << xmlSafeString(options.pathSF2, true) << "</SF2_PATH>\n";
  1305. outSettings << " <SFZ_PATH>" << xmlSafeString(options.pathSFZ, true) << "</SFZ_PATH>\n";
  1306. }
  1307. outSettings << " </EngineSettings>\n";
  1308. outStream << outSettings;
  1309. char strBuf[STR_MAX+1];
  1310. for (uint i=0; i < pData->curPluginCount; ++i)
  1311. {
  1312. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1313. if (plugin != nullptr && plugin->isEnabled())
  1314. {
  1315. MemoryOutputStream outPlugin(4096);
  1316. outPlugin << "\n";
  1317. strBuf[0] = '\0';
  1318. plugin->getRealName(strBuf);
  1319. if (strBuf[0] != '\0')
  1320. outPlugin << " <!-- " << xmlSafeString(strBuf, true) << " -->\n";
  1321. outPlugin << " <Plugin>\n";
  1322. outPlugin << plugin->getStateSave(false).toString();
  1323. outPlugin << " </Plugin>\n";
  1324. outStream << outPlugin;
  1325. }
  1326. }
  1327. #ifndef BUILD_BRIDGE
  1328. // tell bridges we're done saving
  1329. for (uint i=0; i < pData->curPluginCount; ++i)
  1330. {
  1331. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1332. if (plugin != nullptr && plugin->isEnabled() && (plugin->getHints() & PLUGIN_IS_BRIDGE) != 0)
  1333. plugin->setCustomData(CUSTOM_DATA_TYPE_STRING, "__CarlaPingOnOff__", "true", false);
  1334. }
  1335. // save internal connections
  1336. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  1337. {
  1338. if (const char* const* const patchbayConns = getPatchbayConnections(false))
  1339. {
  1340. MemoryOutputStream outPatchbay(2048);
  1341. outPatchbay << "\n <Patchbay>\n";
  1342. for (int i=0; patchbayConns[i] != nullptr && patchbayConns[i+1] != nullptr; ++i, ++i )
  1343. {
  1344. const char* const connSource(patchbayConns[i]);
  1345. const char* const connTarget(patchbayConns[i+1]);
  1346. CARLA_SAFE_ASSERT_CONTINUE(connSource != nullptr && connSource[0] != '\0');
  1347. CARLA_SAFE_ASSERT_CONTINUE(connTarget != nullptr && connTarget[0] != '\0');
  1348. outPatchbay << " <Connection>\n";
  1349. outPatchbay << " <Source>" << connSource << "</Source>\n";
  1350. outPatchbay << " <Target>" << connTarget << "</Target>\n";
  1351. outPatchbay << " </Connection>\n";
  1352. }
  1353. outPatchbay << " </Patchbay>\n";
  1354. outStream << outPatchbay;
  1355. }
  1356. }
  1357. // if we're running inside some session-manager (and using JACK), let them handle the connections
  1358. bool saveExternalConnections;
  1359. /**/ if (std::strcmp(getCurrentDriverName(), "Plugin") == 0)
  1360. saveExternalConnections = false;
  1361. else if (std::strcmp(getCurrentDriverName(), "JACK") != 0)
  1362. saveExternalConnections = true;
  1363. else if (std::getenv("CARLA_DONT_MANAGE_CONNECTIONS") != nullptr)
  1364. saveExternalConnections = false;
  1365. else if (std::getenv("LADISH_APP_NAME") != nullptr)
  1366. saveExternalConnections = false;
  1367. else if (std::getenv("NSM_URL") != nullptr)
  1368. saveExternalConnections = false;
  1369. else
  1370. saveExternalConnections = true;
  1371. if (saveExternalConnections)
  1372. {
  1373. if (const char* const* const patchbayConns = getPatchbayConnections(true))
  1374. {
  1375. MemoryOutputStream outPatchbay(2048);
  1376. outPatchbay << "\n <ExternalPatchbay>\n";
  1377. for (int i=0; patchbayConns[i] != nullptr && patchbayConns[i+1] != nullptr; ++i, ++i )
  1378. {
  1379. const char* const connSource(patchbayConns[i]);
  1380. const char* const connTarget(patchbayConns[i+1]);
  1381. CARLA_SAFE_ASSERT_CONTINUE(connSource != nullptr && connSource[0] != '\0');
  1382. CARLA_SAFE_ASSERT_CONTINUE(connTarget != nullptr && connTarget[0] != '\0');
  1383. outPatchbay << " <Connection>\n";
  1384. outPatchbay << " <Source>" << connSource << "</Source>\n";
  1385. outPatchbay << " <Target>" << connTarget << "</Target>\n";
  1386. outPatchbay << " </Connection>\n";
  1387. }
  1388. outPatchbay << " </ExternalPatchbay>\n";
  1389. outStream << outPatchbay;
  1390. }
  1391. }
  1392. #endif
  1393. outStream << "</CARLA-PROJECT>\n";
  1394. }
  1395. bool CarlaEngine::loadProjectInternal(juce::XmlDocument& xmlDoc)
  1396. {
  1397. ScopedPointer<XmlElement> xmlElement(xmlDoc.getDocumentElement(true));
  1398. CARLA_SAFE_ASSERT_RETURN_ERR(xmlElement != nullptr, "Failed to parse project file");
  1399. const String& xmlType(xmlElement->getTagName());
  1400. const bool isPreset(xmlType.equalsIgnoreCase("carla-preset"));
  1401. if (! (xmlType.equalsIgnoreCase("carla-project") || isPreset))
  1402. {
  1403. setLastError("Not a valid Carla project or preset file");
  1404. return false;
  1405. }
  1406. // completely load file
  1407. xmlElement = xmlDoc.getDocumentElement(false);
  1408. CARLA_SAFE_ASSERT_RETURN_ERR(xmlElement != nullptr, "Failed to completely parse project file");
  1409. const bool isPlugin(std::strcmp(getCurrentDriverName(), "Plugin") == 0);
  1410. // engine settings
  1411. for (XmlElement* elem = xmlElement->getFirstChildElement(); elem != nullptr; elem = elem->getNextElement())
  1412. {
  1413. const String& tagName(elem->getTagName());
  1414. if (! tagName.equalsIgnoreCase("enginesettings"))
  1415. continue;
  1416. for (XmlElement* settElem = elem->getFirstChildElement(); settElem != nullptr; settElem = settElem->getNextElement())
  1417. {
  1418. const String& tag(settElem->getTagName());
  1419. const String text(settElem->getAllSubText().trim());
  1420. /** some settings might be incorrect or require extra work,
  1421. so we call setOption rather than modifying them direly */
  1422. int option = -1;
  1423. int value = 0;
  1424. const char* valueStr = nullptr;
  1425. /**/ if (tag.equalsIgnoreCase("forcestereo"))
  1426. {
  1427. option = ENGINE_OPTION_FORCE_STEREO;
  1428. value = text.equalsIgnoreCase("true") ? 1 : 0;
  1429. }
  1430. else if (tag.equalsIgnoreCase("preferpluginbridges"))
  1431. {
  1432. option = ENGINE_OPTION_PREFER_PLUGIN_BRIDGES;
  1433. value = text.equalsIgnoreCase("true") ? 1 : 0;
  1434. }
  1435. else if (tag.equalsIgnoreCase("preferuibridges"))
  1436. {
  1437. option = ENGINE_OPTION_PREFER_UI_BRIDGES;
  1438. value = text.equalsIgnoreCase("true") ? 1 : 0;
  1439. }
  1440. else if (tag.equalsIgnoreCase("uisalwaysontop"))
  1441. {
  1442. option = ENGINE_OPTION_UIS_ALWAYS_ON_TOP;
  1443. value = text.equalsIgnoreCase("true") ? 1 : 0;
  1444. }
  1445. else if (tag.equalsIgnoreCase("maxparameters"))
  1446. {
  1447. option = ENGINE_OPTION_MAX_PARAMETERS;
  1448. value = text.getIntValue();
  1449. }
  1450. else if (tag.equalsIgnoreCase("uibridgestimeout"))
  1451. {
  1452. option = ENGINE_OPTION_UI_BRIDGES_TIMEOUT;
  1453. value = text.getIntValue();
  1454. }
  1455. else if (isPlugin)
  1456. {
  1457. /**/ if (tag.equalsIgnoreCase("LADSPA_PATH"))
  1458. {
  1459. option = ENGINE_OPTION_PLUGIN_PATH;
  1460. value = PLUGIN_LADSPA;
  1461. valueStr = text.toRawUTF8();
  1462. }
  1463. else if (tag.equalsIgnoreCase("DSSI_PATH"))
  1464. {
  1465. option = ENGINE_OPTION_PLUGIN_PATH;
  1466. value = PLUGIN_DSSI;
  1467. valueStr = text.toRawUTF8();
  1468. }
  1469. else if (tag.equalsIgnoreCase("LV2_PATH"))
  1470. {
  1471. option = ENGINE_OPTION_PLUGIN_PATH;
  1472. value = PLUGIN_LV2;
  1473. valueStr = text.toRawUTF8();
  1474. }
  1475. else if (tag.equalsIgnoreCase("VST2_PATH"))
  1476. {
  1477. option = ENGINE_OPTION_PLUGIN_PATH;
  1478. value = PLUGIN_VST2;
  1479. valueStr = text.toRawUTF8();
  1480. }
  1481. else if (tag.equalsIgnoreCase("VST3_PATH"))
  1482. {
  1483. option = ENGINE_OPTION_PLUGIN_PATH;
  1484. value = PLUGIN_VST3;
  1485. valueStr = text.toRawUTF8();
  1486. }
  1487. else if (tag.equalsIgnoreCase("GIG_PATH"))
  1488. {
  1489. option = ENGINE_OPTION_PLUGIN_PATH;
  1490. value = PLUGIN_GIG;
  1491. valueStr = text.toRawUTF8();
  1492. }
  1493. else if (tag.equalsIgnoreCase("SF2_PATH"))
  1494. {
  1495. option = ENGINE_OPTION_PLUGIN_PATH;
  1496. value = PLUGIN_SF2;
  1497. valueStr = text.toRawUTF8();
  1498. }
  1499. else if (tag.equalsIgnoreCase("SFZ_PATH"))
  1500. {
  1501. option = ENGINE_OPTION_PLUGIN_PATH;
  1502. value = PLUGIN_SFZ;
  1503. valueStr = text.toRawUTF8();
  1504. }
  1505. }
  1506. CARLA_SAFE_ASSERT_CONTINUE(option != -1);
  1507. setOption(static_cast<EngineOption>(option), value, valueStr);
  1508. }
  1509. break;
  1510. }
  1511. // handle plugins first
  1512. for (XmlElement* elem = xmlElement->getFirstChildElement(); elem != nullptr; elem = elem->getNextElement())
  1513. {
  1514. const String& tagName(elem->getTagName());
  1515. if (isPreset || tagName.equalsIgnoreCase("plugin"))
  1516. {
  1517. CarlaStateSave stateSave;
  1518. stateSave.fillFromXmlElement(isPreset ? xmlElement.get() : elem);
  1519. callback(ENGINE_CALLBACK_IDLE, 0, 0, 0, 0.0f, nullptr);
  1520. CARLA_SAFE_ASSERT_CONTINUE(stateSave.type != nullptr);
  1521. const void* extraStuff = nullptr;
  1522. // check if using GIG or SF2 16outs
  1523. static const char kUse16OutsSuffix[] = " (16 outs)";
  1524. const BinaryType btype(getBinaryTypeFromFile(stateSave.binary));
  1525. const PluginType ptype(getPluginTypeFromString(stateSave.type));
  1526. if (CarlaString(stateSave.label).endsWith(kUse16OutsSuffix))
  1527. {
  1528. if (ptype == PLUGIN_GIG || ptype == PLUGIN_SF2)
  1529. extraStuff = "true";
  1530. }
  1531. // TODO - proper find&load plugins
  1532. if (addPlugin(btype, ptype, stateSave.binary, stateSave.name, stateSave.label, stateSave.uniqueId, extraStuff, stateSave.options))
  1533. {
  1534. if (CarlaPlugin* const plugin = getPlugin(pData->curPluginCount-1))
  1535. {
  1536. #ifndef BUILD_BRIDGE
  1537. // deactivate bridge client-side ping check, since some plugins block during load
  1538. if ((plugin->getHints() & PLUGIN_IS_BRIDGE) != 0 && ! isPreset)
  1539. plugin->setCustomData(CUSTOM_DATA_TYPE_STRING, "__CarlaPingOnOff__", "false", false);
  1540. #endif
  1541. plugin->loadStateSave(stateSave);
  1542. }
  1543. }
  1544. else
  1545. carla_stderr2("Failed to load a plugin, error was:\n%s", getLastError());
  1546. }
  1547. if (isPreset)
  1548. return true;
  1549. }
  1550. #ifndef BUILD_BRIDGE
  1551. // tell bridges we're done loading
  1552. for (uint i=0; i < pData->curPluginCount; ++i)
  1553. {
  1554. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1555. if (plugin != nullptr && plugin->isEnabled() && (plugin->getHints() & PLUGIN_IS_BRIDGE) != 0)
  1556. plugin->setCustomData(CUSTOM_DATA_TYPE_STRING, "__CarlaPingOnOff__", "true", false);
  1557. }
  1558. callback(ENGINE_CALLBACK_IDLE, 0, 0, 0, 0.0f, nullptr);
  1559. // handle connections (internal)
  1560. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  1561. {
  1562. const bool isUsingExternal(pData->graph.isUsingExternal());
  1563. for (XmlElement* elem = xmlElement->getFirstChildElement(); elem != nullptr; elem = elem->getNextElement())
  1564. {
  1565. const String& tagName(elem->getTagName());
  1566. if (! tagName.equalsIgnoreCase("patchbay"))
  1567. continue;
  1568. CarlaString sourcePort, targetPort;
  1569. for (XmlElement* patchElem = elem->getFirstChildElement(); patchElem != nullptr; patchElem = patchElem->getNextElement())
  1570. {
  1571. const String& patchTag(patchElem->getTagName());
  1572. sourcePort.clear();
  1573. targetPort.clear();
  1574. if (! patchTag.equalsIgnoreCase("connection"))
  1575. continue;
  1576. for (XmlElement* connElem = patchElem->getFirstChildElement(); connElem != nullptr; connElem = connElem->getNextElement())
  1577. {
  1578. const String& tag(connElem->getTagName());
  1579. const String text(connElem->getAllSubText().trim());
  1580. /**/ if (tag.equalsIgnoreCase("source"))
  1581. sourcePort = text.toRawUTF8();
  1582. else if (tag.equalsIgnoreCase("target"))
  1583. targetPort = text.toRawUTF8();
  1584. }
  1585. if (sourcePort.isNotEmpty() && targetPort.isNotEmpty())
  1586. restorePatchbayConnection(false, sourcePort, targetPort, !isUsingExternal);
  1587. }
  1588. break;
  1589. }
  1590. }
  1591. callback(ENGINE_CALLBACK_IDLE, 0, 0, 0, 0.0f, nullptr);
  1592. // if we're running inside some session-manager (and using JACK), let them handle the external connections
  1593. bool loadExternalConnections;
  1594. /**/ if (std::strcmp(getCurrentDriverName(), "Plugin") == 0)
  1595. loadExternalConnections = false;
  1596. else if (std::strcmp(getCurrentDriverName(), "JACK") != 0)
  1597. loadExternalConnections = true;
  1598. else if (std::getenv("CARLA_DONT_MANAGE_CONNECTIONS") != nullptr)
  1599. loadExternalConnections = false;
  1600. else if (std::getenv("LADISH_APP_NAME") != nullptr)
  1601. loadExternalConnections = false;
  1602. else if (std::getenv("NSM_URL") != nullptr)
  1603. loadExternalConnections = false;
  1604. else
  1605. loadExternalConnections = true;
  1606. // handle connections (external)
  1607. if (loadExternalConnections)
  1608. {
  1609. const bool isUsingExternal(pData->graph.isUsingExternal());
  1610. for (XmlElement* elem = xmlElement->getFirstChildElement(); elem != nullptr; elem = elem->getNextElement())
  1611. {
  1612. const String& tagName(elem->getTagName());
  1613. if (! tagName.equalsIgnoreCase("externalpatchbay"))
  1614. continue;
  1615. CarlaString sourcePort, targetPort;
  1616. for (XmlElement* patchElem = elem->getFirstChildElement(); patchElem != nullptr; patchElem = patchElem->getNextElement())
  1617. {
  1618. const String& patchTag(patchElem->getTagName());
  1619. sourcePort.clear();
  1620. targetPort.clear();
  1621. if (! patchTag.equalsIgnoreCase("connection"))
  1622. continue;
  1623. for (XmlElement* connElem = patchElem->getFirstChildElement(); connElem != nullptr; connElem = connElem->getNextElement())
  1624. {
  1625. const String& tag(connElem->getTagName());
  1626. const String text(connElem->getAllSubText().trim());
  1627. /**/ if (tag.equalsIgnoreCase("source"))
  1628. sourcePort = text.toRawUTF8();
  1629. else if (tag.equalsIgnoreCase("target"))
  1630. targetPort = text.toRawUTF8();
  1631. }
  1632. if (sourcePort.isNotEmpty() && targetPort.isNotEmpty())
  1633. restorePatchbayConnection(true, sourcePort, targetPort, isUsingExternal);
  1634. }
  1635. break;
  1636. }
  1637. }
  1638. #endif
  1639. return true;
  1640. }
  1641. // -----------------------------------------------------------------------
  1642. CARLA_BACKEND_END_NAMESPACE