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.

1929 lines
64KB

  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. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  438. plugin->registerToOscClient();
  439. #endif
  440. EnginePluginData& pluginData(pData->plugins[id]);
  441. pluginData.plugin = plugin;
  442. pluginData.insPeak[0] = 0.0f;
  443. pluginData.insPeak[1] = 0.0f;
  444. pluginData.outsPeak[0] = 0.0f;
  445. pluginData.outsPeak[1] = 0.0f;
  446. #ifndef BUILD_BRIDGE
  447. if (oldPlugin != nullptr)
  448. {
  449. // the engine thread might be reading from the old plugin
  450. pData->thread.stopThread(500);
  451. pData->thread.startThread();
  452. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  453. pData->graph.replacePlugin(oldPlugin, plugin);
  454. const bool wasActive = oldPlugin->getInternalParameterValue(PARAMETER_ACTIVE) >= 0.5f;
  455. const float oldDryWet = oldPlugin->getInternalParameterValue(PARAMETER_DRYWET);
  456. const float oldVolume = oldPlugin->getInternalParameterValue(PARAMETER_VOLUME);
  457. delete oldPlugin;
  458. if (plugin->getHints() & PLUGIN_CAN_DRYWET)
  459. plugin->setDryWet(oldDryWet, true, true);
  460. if (plugin->getHints() & PLUGIN_CAN_VOLUME)
  461. plugin->setVolume(oldVolume, true, true);
  462. if (wasActive)
  463. plugin->setActive(true, true, true);
  464. callback(ENGINE_CALLBACK_RELOAD_ALL, id, 0, 0, 0.0f, nullptr);
  465. }
  466. else
  467. #endif
  468. {
  469. ++pData->curPluginCount;
  470. callback(ENGINE_CALLBACK_PLUGIN_ADDED, id, 0, 0, 0.0f, plugin->getName());
  471. #ifndef BUILD_BRIDGE
  472. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  473. pData->graph.addPlugin(plugin);
  474. #endif
  475. }
  476. return true;
  477. }
  478. 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)
  479. {
  480. return addPlugin(BINARY_NATIVE, ptype, filename, name, label, uniqueId, extra, 0x0);
  481. }
  482. bool CarlaEngine::removePlugin(const uint id)
  483. {
  484. CARLA_SAFE_ASSERT_RETURN_ERR(pData->isIdling == 0, "An operation is still being processed, please wait for it to finish");
  485. CARLA_SAFE_ASSERT_RETURN_ERR(pData->plugins != nullptr, "Invalid engine internal data");
  486. CARLA_SAFE_ASSERT_RETURN_ERR(pData->curPluginCount != 0, "Invalid engine internal data");
  487. CARLA_SAFE_ASSERT_RETURN_ERR(pData->nextAction.opcode == kEnginePostActionNull, "Invalid engine internal data");
  488. CARLA_SAFE_ASSERT_RETURN_ERR(id < pData->curPluginCount, "Invalid plugin Id");
  489. carla_debug("CarlaEngine::removePlugin(%i)", id);
  490. CarlaPlugin* const plugin(pData->plugins[id].plugin);
  491. CARLA_SAFE_ASSERT_RETURN_ERR(plugin != nullptr, "Could not find plugin to remove");
  492. CARLA_SAFE_ASSERT_RETURN_ERR(plugin->getId() == id, "Invalid engine internal data");
  493. pData->thread.stopThread(500);
  494. #ifndef BUILD_BRIDGE
  495. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  496. pData->graph.removePlugin(plugin);
  497. const bool lockWait(isRunning() /*&& pData->options.processMode != ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS*/);
  498. const ScopedActionLock sal(pData, kEnginePostActionRemovePlugin, id, 0, lockWait);
  499. /*
  500. for (uint i=id; i < pData->curPluginCount; ++i)
  501. {
  502. CarlaPlugin* const plugin2(pData->plugins[i].plugin);
  503. CARLA_SAFE_ASSERT_BREAK(plugin2 != nullptr);
  504. plugin2->updateOscURL();
  505. }
  506. */
  507. # ifdef HAVE_LIBLO
  508. if (isOscControlRegistered())
  509. oscSend_control_remove_plugin(id);
  510. # endif
  511. #else
  512. pData->curPluginCount = 0;
  513. carla_zeroStruct(pData->plugins, 1);
  514. #endif
  515. delete plugin;
  516. if (isRunning() && ! pData->aboutToClose)
  517. pData->thread.startThread();
  518. callback(ENGINE_CALLBACK_PLUGIN_REMOVED, id, 0, 0, 0.0f, nullptr);
  519. return true;
  520. }
  521. bool CarlaEngine::removeAllPlugins()
  522. {
  523. CARLA_SAFE_ASSERT_RETURN_ERR(pData->isIdling == 0, "An operation is still being processed, please wait for it to finish");
  524. CARLA_SAFE_ASSERT_RETURN_ERR(pData->plugins != nullptr, "Invalid engine internal data");
  525. CARLA_SAFE_ASSERT_RETURN_ERR(pData->nextPluginId == pData->maxPluginNumber, "Invalid engine internal data");
  526. CARLA_SAFE_ASSERT_RETURN_ERR(pData->nextAction.opcode == kEnginePostActionNull, "Invalid engine internal data");
  527. carla_debug("CarlaEngine::removeAllPlugins()");
  528. if (pData->curPluginCount == 0)
  529. return true;
  530. pData->thread.stopThread(500);
  531. #ifndef BUILD_BRIDGE
  532. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  533. pData->graph.removeAllPlugins(this);
  534. #endif
  535. const uint32_t curPluginCount(pData->curPluginCount);
  536. const bool lockWait(isRunning());
  537. const ScopedActionLock sal(pData, kEnginePostActionZeroCount, 0, 0, lockWait);
  538. callback(ENGINE_CALLBACK_IDLE, 0, 0, 0, 0.0f, nullptr);
  539. for (uint i=0; i < curPluginCount; ++i)
  540. {
  541. EnginePluginData& pluginData(pData->plugins[i]);
  542. if (pluginData.plugin != nullptr)
  543. {
  544. delete pluginData.plugin;
  545. pluginData.plugin = nullptr;
  546. }
  547. pluginData.insPeak[0] = 0.0f;
  548. pluginData.insPeak[1] = 0.0f;
  549. pluginData.outsPeak[0] = 0.0f;
  550. pluginData.outsPeak[1] = 0.0f;
  551. callback(ENGINE_CALLBACK_IDLE, 0, 0, 0, 0.0f, nullptr);
  552. }
  553. if (isRunning() && ! pData->aboutToClose)
  554. pData->thread.startThread();
  555. return true;
  556. }
  557. #ifndef BUILD_BRIDGE
  558. const char* CarlaEngine::renamePlugin(const uint id, const char* const newName)
  559. {
  560. CARLA_SAFE_ASSERT_RETURN_ERRN(pData->isIdling == 0, "An operation is still being processed, please wait for it to finish");
  561. CARLA_SAFE_ASSERT_RETURN_ERRN(pData->plugins != nullptr, "Invalid engine internal data");
  562. CARLA_SAFE_ASSERT_RETURN_ERRN(pData->curPluginCount != 0, "Invalid engine internal data");
  563. CARLA_SAFE_ASSERT_RETURN_ERRN(pData->nextAction.opcode == kEnginePostActionNull, "Invalid engine internal data");
  564. CARLA_SAFE_ASSERT_RETURN_ERRN(id < pData->curPluginCount, "Invalid plugin Id");
  565. CARLA_SAFE_ASSERT_RETURN_ERRN(newName != nullptr && newName[0] != '\0', "Invalid plugin name");
  566. carla_debug("CarlaEngine::renamePlugin(%i, \"%s\")", id, newName);
  567. CarlaPlugin* const plugin(pData->plugins[id].plugin);
  568. CARLA_SAFE_ASSERT_RETURN_ERRN(plugin != nullptr, "Could not find plugin to rename");
  569. CARLA_SAFE_ASSERT_RETURN_ERRN(plugin->getId() == id, "Invalid engine internal data");
  570. if (const char* const name = getUniquePluginName(newName))
  571. {
  572. plugin->setName(name);
  573. return name;
  574. }
  575. setLastError("Unable to get new unique plugin name");
  576. return nullptr;
  577. }
  578. bool CarlaEngine::clonePlugin(const uint id)
  579. {
  580. CARLA_SAFE_ASSERT_RETURN_ERR(pData->isIdling == 0, "An operation is still being processed, please wait for it to finish");
  581. CARLA_SAFE_ASSERT_RETURN_ERR(pData->plugins != nullptr, "Invalid engine internal data");
  582. CARLA_SAFE_ASSERT_RETURN_ERR(pData->curPluginCount != 0, "Invalid engine internal data");
  583. CARLA_SAFE_ASSERT_RETURN_ERR(pData->nextAction.opcode == kEnginePostActionNull, "Invalid engine internal data");
  584. CARLA_SAFE_ASSERT_RETURN_ERR(id < pData->curPluginCount, "Invalid plugin Id");
  585. carla_debug("CarlaEngine::clonePlugin(%i)", id);
  586. CarlaPlugin* const plugin(pData->plugins[id].plugin);
  587. CARLA_SAFE_ASSERT_RETURN_ERR(plugin != nullptr, "Could not find plugin to clone");
  588. CARLA_SAFE_ASSERT_RETURN_ERR(plugin->getId() == id, "Invalid engine internal data");
  589. char label[STR_MAX+1];
  590. carla_zeroChar(label, STR_MAX+1);
  591. plugin->getLabel(label);
  592. const uint pluginCountBefore(pData->curPluginCount);
  593. if (! addPlugin(plugin->getBinaryType(), plugin->getType(),
  594. plugin->getFilename(), plugin->getName(), label, plugin->getUniqueId(),
  595. plugin->getExtraStuff(), plugin->getOptionsEnabled()))
  596. return false;
  597. CARLA_SAFE_ASSERT_RETURN_ERR(pluginCountBefore+1 == pData->curPluginCount, "No new plugin found");
  598. if (CarlaPlugin* const newPlugin = pData->plugins[pluginCountBefore].plugin)
  599. newPlugin->loadStateSave(plugin->getStateSave());
  600. return true;
  601. }
  602. bool CarlaEngine::replacePlugin(const uint id) noexcept
  603. {
  604. CARLA_SAFE_ASSERT_RETURN_ERR(pData->isIdling == 0, "An operation is still being processed, please wait for it to finish");
  605. CARLA_SAFE_ASSERT_RETURN_ERR(pData->plugins != nullptr, "Invalid engine internal data");
  606. CARLA_SAFE_ASSERT_RETURN_ERR(pData->curPluginCount != 0, "Invalid engine internal data");
  607. CARLA_SAFE_ASSERT_RETURN_ERR(pData->nextAction.opcode == kEnginePostActionNull, "Invalid engine internal data");
  608. carla_debug("CarlaEngine::replacePlugin(%i)", id);
  609. // might use this to reset
  610. if (id == pData->maxPluginNumber)
  611. {
  612. pData->nextPluginId = pData->maxPluginNumber;
  613. return true;
  614. }
  615. CARLA_SAFE_ASSERT_RETURN_ERR(id < pData->curPluginCount, "Invalid plugin Id");
  616. CarlaPlugin* const plugin(pData->plugins[id].plugin);
  617. CARLA_SAFE_ASSERT_RETURN_ERR(plugin != nullptr, "Could not find plugin to replace");
  618. CARLA_SAFE_ASSERT_RETURN_ERR(plugin->getId() == id, "Invalid engine internal data");
  619. pData->nextPluginId = id;
  620. return true;
  621. }
  622. bool CarlaEngine::switchPlugins(const uint idA, const uint idB) noexcept
  623. {
  624. CARLA_SAFE_ASSERT_RETURN_ERR(pData->isIdling == 0, "An operation is still being processed, please wait for it to finish");
  625. CARLA_SAFE_ASSERT_RETURN_ERR(pData->plugins != nullptr, "Invalid engine internal data");
  626. CARLA_SAFE_ASSERT_RETURN_ERR(pData->curPluginCount >= 2, "Invalid engine internal data");
  627. CARLA_SAFE_ASSERT_RETURN_ERR(pData->nextAction.opcode == kEnginePostActionNull, "Invalid engine internal data");
  628. CARLA_SAFE_ASSERT_RETURN_ERR(idA != idB, "Invalid operation, cannot switch plugin with itself");
  629. CARLA_SAFE_ASSERT_RETURN_ERR(idA < pData->curPluginCount, "Invalid plugin Id");
  630. CARLA_SAFE_ASSERT_RETURN_ERR(idB < pData->curPluginCount, "Invalid plugin Id");
  631. carla_debug("CarlaEngine::switchPlugins(%i)", idA, idB);
  632. {
  633. CarlaPlugin* const pluginA(pData->plugins[idA].plugin);
  634. CarlaPlugin* const pluginB(pData->plugins[idB].plugin);
  635. CARLA_SAFE_ASSERT_RETURN_ERR(pluginA != nullptr, "Could not find plugin to switch");
  636. CARLA_SAFE_ASSERT_RETURN_ERR(pluginA != nullptr, "Could not find plugin to switch");
  637. CARLA_SAFE_ASSERT_RETURN_ERR(pluginA->getId() == idA, "Invalid engine internal data");
  638. CARLA_SAFE_ASSERT_RETURN_ERR(pluginB->getId() == idB, "Invalid engine internal data");
  639. pData->thread.stopThread(500);
  640. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  641. pData->graph.replacePlugin(pluginA, pluginB);
  642. }
  643. const bool lockWait(isRunning() /*&& pData->options.processMode != ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS*/);
  644. const ScopedActionLock sal(pData, kEnginePostActionSwitchPlugins, idA, idB, lockWait);
  645. /*
  646. CarlaPlugin* const pluginA(pData->plugins[idA].plugin);
  647. CarlaPlugin* const pluginB(pData->plugins[idB].plugin);
  648. if (pluginA != nullptr && pluginB != nullptr)
  649. {
  650. pluginA->updateOscURL();
  651. pluginB->updateOscURL();
  652. }
  653. */
  654. // TODO
  655. //if (isOscControlRegistered())
  656. // oscSend_control_switch_plugins(idA, idB);
  657. if (isRunning() && ! pData->aboutToClose)
  658. pData->thread.startThread();
  659. return true;
  660. }
  661. #endif
  662. CarlaPlugin* CarlaEngine::getPlugin(const uint id) const noexcept
  663. {
  664. CARLA_SAFE_ASSERT_RETURN_ERRN(pData->plugins != nullptr, "Invalid engine internal data");
  665. CARLA_SAFE_ASSERT_RETURN_ERRN(pData->curPluginCount != 0, "Invalid engine internal data");
  666. CARLA_SAFE_ASSERT_RETURN_ERRN(pData->nextAction.opcode == kEnginePostActionNull, "Invalid engine internal data");
  667. CARLA_SAFE_ASSERT_RETURN_ERRN(id < pData->curPluginCount, "Invalid plugin Id");
  668. return pData->plugins[id].plugin;
  669. }
  670. CarlaPlugin* CarlaEngine::getPluginUnchecked(const uint id) const noexcept
  671. {
  672. return pData->plugins[id].plugin;
  673. }
  674. const char* CarlaEngine::getUniquePluginName(const char* const name) const
  675. {
  676. CARLA_SAFE_ASSERT_RETURN(pData->nextAction.opcode == kEnginePostActionNull, nullptr);
  677. CARLA_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0', nullptr);
  678. carla_debug("CarlaEngine::getUniquePluginName(\"%s\")", name);
  679. CarlaString sname;
  680. sname = name;
  681. if (sname.isEmpty())
  682. {
  683. sname = "(No name)";
  684. return sname.dup();
  685. }
  686. const std::size_t maxNameSize(carla_minWithBase<uint>(getMaxClientNameSize(), 0xff, 6U) - 6); // 6 = strlen(" (10)") + 1
  687. if (maxNameSize == 0 || ! isRunning())
  688. return sname.dup();
  689. sname.truncate(maxNameSize);
  690. sname.replace(':', '.'); // ':' is used in JACK1 to split client/port names
  691. for (uint i=0; i < pData->curPluginCount; ++i)
  692. {
  693. CARLA_SAFE_ASSERT_BREAK(pData->plugins[i].plugin != nullptr);
  694. // Check if unique name doesn't exist
  695. if (const char* const pluginName = pData->plugins[i].plugin->getName())
  696. {
  697. if (sname != pluginName)
  698. continue;
  699. }
  700. // Check if string has already been modified
  701. {
  702. const std::size_t len(sname.length());
  703. // 1 digit, ex: " (2)"
  704. if (sname[len-4] == ' ' && sname[len-3] == '(' && sname.isDigit(len-2) && sname[len-1] == ')')
  705. {
  706. const int number = sname[len-2] - '0';
  707. if (number == 9)
  708. {
  709. // next number is 10, 2 digits
  710. sname.truncate(len-4);
  711. sname += " (10)";
  712. //sname.replace(" (9)", " (10)");
  713. }
  714. else
  715. sname[len-2] = char('0' + number + 1);
  716. continue;
  717. }
  718. // 2 digits, ex: " (11)"
  719. if (sname[len-5] == ' ' && sname[len-4] == '(' && sname.isDigit(len-3) && sname.isDigit(len-2) && sname[len-1] == ')')
  720. {
  721. char n2 = sname[len-2];
  722. char n3 = sname[len-3];
  723. if (n2 == '9')
  724. {
  725. n2 = '0';
  726. n3 = static_cast<char>(n3 + 1);
  727. }
  728. else
  729. n2 = static_cast<char>(n2 + 1);
  730. sname[len-2] = n2;
  731. sname[len-3] = n3;
  732. continue;
  733. }
  734. }
  735. // Modify string if not
  736. sname += " (2)";
  737. }
  738. return sname.dup();
  739. }
  740. // -----------------------------------------------------------------------
  741. // Project management
  742. bool CarlaEngine::loadFile(const char* const filename)
  743. {
  744. CARLA_SAFE_ASSERT_RETURN_ERR(pData->isIdling == 0, "An operation is still being processed, please wait for it to finish");
  745. CARLA_SAFE_ASSERT_RETURN_ERR(filename != nullptr && filename[0] != '\0', "Invalid filename");
  746. carla_debug("CarlaEngine::loadFile(\"%s\")", filename);
  747. const String jfilename = String(CharPointer_UTF8(filename));
  748. File file(jfilename);
  749. CARLA_SAFE_ASSERT_RETURN_ERR(file.existsAsFile(), "Requested file does not exist or is not a readable file");
  750. CarlaString baseName(file.getFileName().toRawUTF8());
  751. CarlaString extension(file.getFileExtension().replace(".","").toLowerCase().toRawUTF8());
  752. // -------------------------------------------------------------------
  753. if (extension == "carxp" || extension == "carxs")
  754. return loadProject(filename);
  755. // -------------------------------------------------------------------
  756. if (extension == "gig")
  757. return addPlugin(PLUGIN_GIG, filename, baseName, baseName, 0, nullptr);
  758. if (extension == "sf2")
  759. return addPlugin(PLUGIN_SF2, filename, baseName, baseName, 0, nullptr);
  760. if (extension == "sfz")
  761. return addPlugin(PLUGIN_SFZ, filename, baseName, baseName, 0, nullptr);
  762. // -------------------------------------------------------------------
  763. if (extension == "aif" || extension == "aiff" || extension == "bwf" || extension == "flac" || extension == "ogg" || extension == "wav")
  764. {
  765. if (addPlugin(PLUGIN_INTERNAL, nullptr, baseName, "audiofile", 0, nullptr))
  766. {
  767. if (CarlaPlugin* const plugin = getPlugin(pData->curPluginCount-1))
  768. plugin->setCustomData(CUSTOM_DATA_TYPE_STRING, "file", filename, true);
  769. return true;
  770. }
  771. return false;
  772. }
  773. // -------------------------------------------------------------------
  774. if (extension == "mid" || extension == "midi")
  775. {
  776. if (addPlugin(PLUGIN_INTERNAL, nullptr, baseName, "midifile", 0, nullptr))
  777. {
  778. if (CarlaPlugin* const plugin = getPlugin(pData->curPluginCount-1))
  779. plugin->setCustomData(CUSTOM_DATA_TYPE_STRING, "file", filename, true);
  780. return true;
  781. }
  782. return false;
  783. }
  784. // -------------------------------------------------------------------
  785. // ZynAddSubFX
  786. if (extension == "xmz" || extension == "xiz")
  787. {
  788. #ifdef HAVE_ZYN_DEPS
  789. if (addPlugin(PLUGIN_INTERNAL, nullptr, baseName, "zynaddsubfx", 0, nullptr))
  790. {
  791. if (CarlaPlugin* const plugin = getPlugin(pData->curPluginCount-1))
  792. plugin->setCustomData(CUSTOM_DATA_TYPE_STRING, (extension == "xmz") ? "CarlaAlternateFile1" : "CarlaAlternateFile2", filename, true);
  793. return true;
  794. }
  795. return false;
  796. #else
  797. setLastError("This Carla build does not have ZynAddSubFX support");
  798. return false;
  799. #endif
  800. }
  801. // -------------------------------------------------------------------
  802. setLastError("Unknown file extension");
  803. return false;
  804. }
  805. bool CarlaEngine::loadProject(const char* const filename)
  806. {
  807. CARLA_SAFE_ASSERT_RETURN_ERR(pData->isIdling == 0, "An operation is still being processed, please wait for it to finish");
  808. CARLA_SAFE_ASSERT_RETURN_ERR(filename != nullptr && filename[0] != '\0', "Invalid filename");
  809. carla_debug("CarlaEngine::loadProject(\"%s\")", filename);
  810. const String jfilename = String(CharPointer_UTF8(filename));
  811. File file(jfilename);
  812. CARLA_SAFE_ASSERT_RETURN_ERR(file.existsAsFile(), "Requested file does not exist or is not a readable file");
  813. XmlDocument xml(file);
  814. return loadProjectInternal(xml);
  815. }
  816. bool CarlaEngine::saveProject(const char* const filename)
  817. {
  818. CARLA_SAFE_ASSERT_RETURN_ERR(filename != nullptr && filename[0] != '\0', "Invalid filename");
  819. carla_debug("CarlaEngine::saveProject(\"%s\")", filename);
  820. MemoryOutputStream out;
  821. saveProjectInternal(out);
  822. const String jfilename = String(CharPointer_UTF8(filename));
  823. File file(jfilename);
  824. if (file.replaceWithData(out.getData(), out.getDataSize()))
  825. return true;
  826. setLastError("Failed to write file");
  827. return false;
  828. }
  829. // -----------------------------------------------------------------------
  830. // Information (base)
  831. uint CarlaEngine::getHints() const noexcept
  832. {
  833. return pData->hints;
  834. }
  835. uint32_t CarlaEngine::getBufferSize() const noexcept
  836. {
  837. return pData->bufferSize;
  838. }
  839. double CarlaEngine::getSampleRate() const noexcept
  840. {
  841. return pData->sampleRate;
  842. }
  843. const char* CarlaEngine::getName() const noexcept
  844. {
  845. return pData->name;
  846. }
  847. EngineProcessMode CarlaEngine::getProccessMode() const noexcept
  848. {
  849. return pData->options.processMode;
  850. }
  851. const EngineOptions& CarlaEngine::getOptions() const noexcept
  852. {
  853. return pData->options;
  854. }
  855. const EngineTimeInfo& CarlaEngine::getTimeInfo() const noexcept
  856. {
  857. return pData->timeInfo;
  858. }
  859. // -----------------------------------------------------------------------
  860. // Information (peaks)
  861. float CarlaEngine::getInputPeak(const uint pluginId, const bool isLeft) const noexcept
  862. {
  863. CARLA_SAFE_ASSERT_RETURN(pluginId < pData->curPluginCount, 0.0f);
  864. return pData->plugins[pluginId].insPeak[isLeft ? 0 : 1];
  865. }
  866. float CarlaEngine::getOutputPeak(const uint pluginId, const bool isLeft) const noexcept
  867. {
  868. CARLA_SAFE_ASSERT_RETURN(pluginId < pData->curPluginCount, 0.0f);
  869. return pData->plugins[pluginId].outsPeak[isLeft ? 0 : 1];
  870. }
  871. // -----------------------------------------------------------------------
  872. // Callback
  873. void CarlaEngine::callback(const EngineCallbackOpcode action, const uint pluginId, const int value1, const int value2, const float value3, const char* const valueStr) noexcept
  874. {
  875. #ifdef DEBUG
  876. if (action != ENGINE_CALLBACK_IDLE)
  877. carla_debug("CarlaEngine::callback(%i:%s, %i, %i, %i, %f, \"%s\")", action, EngineCallbackOpcode2Str(action), pluginId, value1, value2, value3, valueStr);
  878. #endif
  879. #ifdef BUILD_BRIDGE
  880. if (pData->isIdling)
  881. #else
  882. if (pData->isIdling && action != ENGINE_CALLBACK_PATCHBAY_CLIENT_DATA_CHANGED)
  883. #endif
  884. {
  885. carla_stdout("callback while idling (%i:%s, %i, %i, %i, %f, \"%s\")", action, EngineCallbackOpcode2Str(action), pluginId, value1, value2, value3, valueStr);
  886. }
  887. if (pData->callback != nullptr)
  888. {
  889. if (action == ENGINE_CALLBACK_IDLE)
  890. ++pData->isIdling;
  891. try {
  892. pData->callback(pData->callbackPtr, action, pluginId, value1, value2, value3, valueStr);
  893. } CARLA_SAFE_EXCEPTION("callback");
  894. if (action == ENGINE_CALLBACK_IDLE)
  895. --pData->isIdling;
  896. }
  897. }
  898. void CarlaEngine::setCallback(const EngineCallbackFunc func, void* const ptr) noexcept
  899. {
  900. carla_debug("CarlaEngine::setCallback(%p, %p)", func, ptr);
  901. pData->callback = func;
  902. pData->callbackPtr = ptr;
  903. }
  904. // -----------------------------------------------------------------------
  905. // File Callback
  906. const char* CarlaEngine::runFileCallback(const FileCallbackOpcode action, const bool isDir, const char* const title, const char* const filter) noexcept
  907. {
  908. CARLA_SAFE_ASSERT_RETURN(title != nullptr && title[0] != '\0', nullptr);
  909. CARLA_SAFE_ASSERT_RETURN(filter != nullptr, nullptr);
  910. carla_debug("CarlaEngine::runFileCallback(%i:%s, %s, \"%s\", \"%s\")", action, FileCallbackOpcode2Str(action), bool2str(isDir), title, filter);
  911. const char* ret = nullptr;
  912. if (pData->fileCallback != nullptr)
  913. {
  914. try {
  915. ret = pData->fileCallback(pData->fileCallbackPtr, action, isDir, title, filter);
  916. } CARLA_SAFE_EXCEPTION("runFileCallback");
  917. }
  918. return ret;
  919. }
  920. void CarlaEngine::setFileCallback(const FileCallbackFunc func, void* const ptr) noexcept
  921. {
  922. carla_debug("CarlaEngine::setFileCallback(%p, %p)", func, ptr);
  923. pData->fileCallback = func;
  924. pData->fileCallbackPtr = ptr;
  925. }
  926. // -----------------------------------------------------------------------
  927. // Transport
  928. void CarlaEngine::transportPlay() noexcept
  929. {
  930. pData->time.playing = true;
  931. }
  932. void CarlaEngine::transportPause() noexcept
  933. {
  934. pData->time.playing = false;
  935. }
  936. void CarlaEngine::transportRelocate(const uint64_t frame) noexcept
  937. {
  938. pData->time.frame = frame;
  939. }
  940. // -----------------------------------------------------------------------
  941. // Error handling
  942. const char* CarlaEngine::getLastError() const noexcept
  943. {
  944. return pData->lastError;
  945. }
  946. void CarlaEngine::setLastError(const char* const error) const noexcept
  947. {
  948. pData->lastError = error;
  949. }
  950. void CarlaEngine::setAboutToClose() noexcept
  951. {
  952. carla_debug("CarlaEngine::setAboutToClose()");
  953. pData->aboutToClose = true;
  954. }
  955. // -----------------------------------------------------------------------
  956. // Global options
  957. void CarlaEngine::setOption(const EngineOption option, const int value, const char* const valueStr) noexcept
  958. {
  959. carla_debug("CarlaEngine::setOption(%i:%s, %i, \"%s\")", option, EngineOption2Str(option), value, valueStr);
  960. if (isRunning() && (option == ENGINE_OPTION_PROCESS_MODE || option == ENGINE_OPTION_AUDIO_NUM_PERIODS || option == ENGINE_OPTION_AUDIO_DEVICE))
  961. return carla_stderr("CarlaEngine::setOption(%i:%s, %i, \"%s\") - Cannot set this option while engine is running!", option, EngineOption2Str(option), value, valueStr);
  962. // do not un-force stereo for rack mode
  963. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK && option == ENGINE_OPTION_FORCE_STEREO && value != 0)
  964. return;
  965. switch (option)
  966. {
  967. case ENGINE_OPTION_DEBUG:
  968. break;
  969. case ENGINE_OPTION_PROCESS_MODE:
  970. CARLA_SAFE_ASSERT_RETURN(value >= ENGINE_PROCESS_MODE_SINGLE_CLIENT && value <= ENGINE_PROCESS_MODE_BRIDGE,);
  971. pData->options.processMode = static_cast<EngineProcessMode>(value);
  972. break;
  973. case ENGINE_OPTION_TRANSPORT_MODE:
  974. CARLA_SAFE_ASSERT_RETURN(value >= ENGINE_TRANSPORT_MODE_INTERNAL && value <= ENGINE_TRANSPORT_MODE_BRIDGE,);
  975. pData->options.transportMode = static_cast<EngineTransportMode>(value);
  976. break;
  977. case ENGINE_OPTION_FORCE_STEREO:
  978. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  979. pData->options.forceStereo = (value != 0);
  980. break;
  981. case ENGINE_OPTION_PREFER_PLUGIN_BRIDGES:
  982. #ifdef BUILD_BRIDGE
  983. CARLA_SAFE_ASSERT_RETURN(value == 0,);
  984. #else
  985. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  986. #endif
  987. pData->options.preferPluginBridges = (value != 0);
  988. break;
  989. case ENGINE_OPTION_PREFER_UI_BRIDGES:
  990. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  991. pData->options.preferUiBridges = (value != 0);
  992. break;
  993. case ENGINE_OPTION_UIS_ALWAYS_ON_TOP:
  994. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  995. pData->options.uisAlwaysOnTop = (value != 0);
  996. break;
  997. case ENGINE_OPTION_MAX_PARAMETERS:
  998. CARLA_SAFE_ASSERT_RETURN(value >= 0,);
  999. pData->options.maxParameters = static_cast<uint>(value);
  1000. break;
  1001. case ENGINE_OPTION_UI_BRIDGES_TIMEOUT:
  1002. CARLA_SAFE_ASSERT_RETURN(value >= 0,);
  1003. pData->options.uiBridgesTimeout = static_cast<uint>(value);
  1004. break;
  1005. case ENGINE_OPTION_AUDIO_NUM_PERIODS:
  1006. CARLA_SAFE_ASSERT_RETURN(value >= 2 && value <= 3,);
  1007. pData->options.audioNumPeriods = static_cast<uint>(value);
  1008. break;
  1009. case ENGINE_OPTION_AUDIO_BUFFER_SIZE:
  1010. CARLA_SAFE_ASSERT_RETURN(value >= 8,);
  1011. pData->options.audioBufferSize = static_cast<uint>(value);
  1012. break;
  1013. case ENGINE_OPTION_AUDIO_SAMPLE_RATE:
  1014. CARLA_SAFE_ASSERT_RETURN(value >= 22050,);
  1015. pData->options.audioSampleRate = static_cast<uint>(value);
  1016. break;
  1017. case ENGINE_OPTION_AUDIO_DEVICE:
  1018. CARLA_SAFE_ASSERT_RETURN(valueStr != nullptr,);
  1019. if (pData->options.audioDevice != nullptr)
  1020. delete[] pData->options.audioDevice;
  1021. pData->options.audioDevice = carla_strdup_safe(valueStr);
  1022. break;
  1023. case ENGINE_OPTION_PLUGIN_PATH:
  1024. CARLA_SAFE_ASSERT_RETURN(value > PLUGIN_NONE,);
  1025. CARLA_SAFE_ASSERT_RETURN(value <= PLUGIN_SFZ,);
  1026. switch (value)
  1027. {
  1028. case PLUGIN_LADSPA:
  1029. if (pData->options.pathLADSPA != nullptr)
  1030. delete[] pData->options.pathLADSPA;
  1031. if (valueStr != nullptr)
  1032. pData->options.pathLADSPA = carla_strdup_safe(valueStr);
  1033. else
  1034. pData->options.pathLADSPA = nullptr;
  1035. break;
  1036. case PLUGIN_DSSI:
  1037. if (pData->options.pathDSSI != nullptr)
  1038. delete[] pData->options.pathDSSI;
  1039. if (valueStr != nullptr)
  1040. pData->options.pathDSSI = carla_strdup_safe(valueStr);
  1041. else
  1042. pData->options.pathDSSI = nullptr;
  1043. break;
  1044. case PLUGIN_LV2:
  1045. if (pData->options.pathLV2 != nullptr)
  1046. delete[] pData->options.pathLV2;
  1047. if (valueStr != nullptr)
  1048. pData->options.pathLV2 = carla_strdup_safe(valueStr);
  1049. else
  1050. pData->options.pathLV2 = nullptr;
  1051. break;
  1052. case PLUGIN_VST2:
  1053. if (pData->options.pathVST2 != nullptr)
  1054. delete[] pData->options.pathVST2;
  1055. if (valueStr != nullptr)
  1056. pData->options.pathVST2 = carla_strdup_safe(valueStr);
  1057. else
  1058. pData->options.pathVST2 = nullptr;
  1059. break;
  1060. case PLUGIN_VST3:
  1061. if (pData->options.pathVST3 != nullptr)
  1062. delete[] pData->options.pathVST3;
  1063. if (valueStr != nullptr)
  1064. pData->options.pathVST3 = carla_strdup_safe(valueStr);
  1065. else
  1066. pData->options.pathVST3 = nullptr;
  1067. break;
  1068. case PLUGIN_GIG:
  1069. if (pData->options.pathGIG != nullptr)
  1070. delete[] pData->options.pathGIG;
  1071. if (valueStr != nullptr)
  1072. pData->options.pathGIG = carla_strdup_safe(valueStr);
  1073. else
  1074. pData->options.pathGIG = nullptr;
  1075. break;
  1076. case PLUGIN_SF2:
  1077. if (pData->options.pathSF2 != nullptr)
  1078. delete[] pData->options.pathSF2;
  1079. if (valueStr != nullptr)
  1080. pData->options.pathSF2 = carla_strdup_safe(valueStr);
  1081. else
  1082. pData->options.pathSF2 = nullptr;
  1083. break;
  1084. case PLUGIN_SFZ:
  1085. if (pData->options.pathSFZ != nullptr)
  1086. delete[] pData->options.pathSFZ;
  1087. if (valueStr != nullptr)
  1088. pData->options.pathSFZ = carla_strdup_safe(valueStr);
  1089. else
  1090. pData->options.pathSFZ = nullptr;
  1091. break;
  1092. default:
  1093. return carla_stderr("CarlaEngine::setOption(%i:%s, %i, \"%s\") - Invalid plugin type", option, EngineOption2Str(option), value, valueStr);
  1094. break;
  1095. }
  1096. break;
  1097. case ENGINE_OPTION_PATH_BINARIES:
  1098. CARLA_SAFE_ASSERT_RETURN(valueStr != nullptr && valueStr[0] != '\0',);
  1099. if (pData->options.binaryDir != nullptr)
  1100. delete[] pData->options.binaryDir;
  1101. pData->options.binaryDir = carla_strdup_safe(valueStr);
  1102. break;
  1103. case ENGINE_OPTION_PATH_RESOURCES:
  1104. CARLA_SAFE_ASSERT_RETURN(valueStr != nullptr && valueStr[0] != '\0',);
  1105. if (pData->options.resourceDir != nullptr)
  1106. delete[] pData->options.resourceDir;
  1107. pData->options.resourceDir = carla_strdup_safe(valueStr);
  1108. break;
  1109. case ENGINE_OPTION_PREVENT_BAD_BEHAVIOUR:
  1110. CARLA_SAFE_ASSERT_RETURN(pData->options.binaryDir != nullptr && pData->options.binaryDir[0] != '\0',);
  1111. #ifdef CARLA_OS_LINUX
  1112. if (value != 0)
  1113. {
  1114. CarlaString interposerPath(CarlaString(pData->options.binaryDir) + CARLA_OS_SEP_STR "libcarla_interposer.so");
  1115. ::setenv("LD_PRELOAD", interposerPath.buffer(), 1);
  1116. }
  1117. else
  1118. {
  1119. ::unsetenv("LD_PRELOAD");
  1120. }
  1121. #endif
  1122. break;
  1123. case ENGINE_OPTION_FRONTEND_WIN_ID:
  1124. CARLA_SAFE_ASSERT_RETURN(valueStr != nullptr && valueStr[0] != '\0',);
  1125. const long long winId(std::strtoll(valueStr, nullptr, 16));
  1126. CARLA_SAFE_ASSERT_RETURN(winId >= 0,);
  1127. pData->options.frontendWinId = static_cast<uintptr_t>(winId);
  1128. break;
  1129. }
  1130. }
  1131. #ifdef HAVE_LIBLO
  1132. // -----------------------------------------------------------------------
  1133. // OSC Stuff
  1134. # ifndef BUILD_BRIDGE
  1135. bool CarlaEngine::isOscControlRegistered() const noexcept
  1136. {
  1137. return pData->osc.isControlRegistered();
  1138. }
  1139. # endif
  1140. void CarlaEngine::idleOsc() const noexcept
  1141. {
  1142. pData->osc.idle();
  1143. }
  1144. const char* CarlaEngine::getOscServerPathTCP() const noexcept
  1145. {
  1146. return pData->osc.getServerPathTCP();
  1147. }
  1148. const char* CarlaEngine::getOscServerPathUDP() const noexcept
  1149. {
  1150. return pData->osc.getServerPathUDP();
  1151. }
  1152. #endif
  1153. // -----------------------------------------------------------------------
  1154. // Helper functions
  1155. EngineEvent* CarlaEngine::getInternalEventBuffer(const bool isInput) const noexcept
  1156. {
  1157. return isInput ? pData->events.in : pData->events.out;
  1158. }
  1159. void CarlaEngine::lockEnvironment() const noexcept
  1160. {
  1161. pData->envMutex.lock();
  1162. }
  1163. void CarlaEngine::unlockEnvironment() const noexcept
  1164. {
  1165. pData->envMutex.unlock();
  1166. }
  1167. // -----------------------------------------------------------------------
  1168. // Internal stuff
  1169. void CarlaEngine::bufferSizeChanged(const uint32_t newBufferSize)
  1170. {
  1171. carla_debug("CarlaEngine::bufferSizeChanged(%i)", newBufferSize);
  1172. #ifndef BUILD_BRIDGE
  1173. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK ||
  1174. pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  1175. {
  1176. pData->graph.setBufferSize(newBufferSize);
  1177. }
  1178. #endif
  1179. for (uint i=0; i < pData->curPluginCount; ++i)
  1180. {
  1181. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1182. if (plugin != nullptr && plugin->isEnabled())
  1183. plugin->bufferSizeChanged(newBufferSize);
  1184. }
  1185. callback(ENGINE_CALLBACK_BUFFER_SIZE_CHANGED, 0, static_cast<int>(newBufferSize), 0, 0.0f, nullptr);
  1186. }
  1187. void CarlaEngine::sampleRateChanged(const double newSampleRate)
  1188. {
  1189. carla_debug("CarlaEngine::sampleRateChanged(%g)", newSampleRate);
  1190. #ifndef BUILD_BRIDGE
  1191. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK ||
  1192. pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  1193. {
  1194. pData->graph.setSampleRate(newSampleRate);
  1195. }
  1196. #endif
  1197. for (uint i=0; i < pData->curPluginCount; ++i)
  1198. {
  1199. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1200. if (plugin != nullptr && plugin->isEnabled())
  1201. plugin->sampleRateChanged(newSampleRate);
  1202. }
  1203. callback(ENGINE_CALLBACK_SAMPLE_RATE_CHANGED, 0, 0, 0, static_cast<float>(newSampleRate), nullptr);
  1204. }
  1205. void CarlaEngine::offlineModeChanged(const bool isOfflineNow)
  1206. {
  1207. carla_debug("CarlaEngine::offlineModeChanged(%s)", bool2str(isOfflineNow));
  1208. #ifndef BUILD_BRIDGE
  1209. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK ||
  1210. pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  1211. {
  1212. pData->graph.setOffline(isOfflineNow);
  1213. }
  1214. #endif
  1215. for (uint i=0; i < pData->curPluginCount; ++i)
  1216. {
  1217. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1218. if (plugin != nullptr && plugin->isEnabled())
  1219. plugin->offlineModeChanged(isOfflineNow);
  1220. }
  1221. }
  1222. void CarlaEngine::runPendingRtEvents() noexcept
  1223. {
  1224. pData->doNextPluginAction(true);
  1225. if (pData->time.playing)
  1226. pData->time.frame += pData->bufferSize;
  1227. if (pData->options.transportMode == ENGINE_TRANSPORT_MODE_INTERNAL)
  1228. {
  1229. pData->timeInfo.playing = pData->time.playing;
  1230. pData->timeInfo.frame = pData->time.frame;
  1231. }
  1232. }
  1233. void CarlaEngine::setPluginPeaks(const uint pluginId, float const inPeaks[2], float const outPeaks[2]) noexcept
  1234. {
  1235. EnginePluginData& pluginData(pData->plugins[pluginId]);
  1236. pluginData.insPeak[0] = inPeaks[0];
  1237. pluginData.insPeak[1] = inPeaks[1];
  1238. pluginData.outsPeak[0] = outPeaks[0];
  1239. pluginData.outsPeak[1] = outPeaks[1];
  1240. }
  1241. void CarlaEngine::saveProjectInternal(juce::MemoryOutputStream& outStream) const
  1242. {
  1243. // send initial prepareForSave first, giving time for bridges to act
  1244. for (uint i=0; i < pData->curPluginCount; ++i)
  1245. {
  1246. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1247. if (plugin != nullptr && plugin->isEnabled())
  1248. {
  1249. #ifndef BUILD_BRIDGE
  1250. // deactivate bridge client-side ping check, since some plugins block during save
  1251. if (plugin->getHints() & PLUGIN_IS_BRIDGE)
  1252. plugin->setCustomData(CUSTOM_DATA_TYPE_STRING, "__CarlaPingOnOff__", "false", false);
  1253. #endif
  1254. plugin->prepareForSave();
  1255. }
  1256. }
  1257. outStream << "<?xml version='1.0' encoding='UTF-8'?>\n";
  1258. outStream << "<!DOCTYPE CARLA-PROJECT>\n";
  1259. outStream << "<CARLA-PROJECT VERSION='2.0'>\n";
  1260. const bool isPlugin(std::strcmp(getCurrentDriverName(), "Plugin") == 0);
  1261. const EngineOptions& options(pData->options);
  1262. MemoryOutputStream outSettings(1024);
  1263. // save appropriate engine settings
  1264. outSettings << " <EngineSettings>\n";
  1265. //processMode
  1266. //transportMode
  1267. outSettings << " <ForceStereo>" << bool2str(options.forceStereo) << "</ForceStereo>\n";
  1268. outSettings << " <PreferPluginBridges>" << bool2str(options.preferPluginBridges) << "</PreferPluginBridges>\n";
  1269. outSettings << " <PreferUiBridges>" << bool2str(options.preferUiBridges) << "</PreferUiBridges>\n";
  1270. outSettings << " <UIsAlwaysOnTop>" << bool2str(options.uisAlwaysOnTop) << "</UIsAlwaysOnTop>\n";
  1271. outSettings << " <MaxParameters>" << String(options.maxParameters) << "</MaxParameters>\n";
  1272. outSettings << " <UIBridgesTimeout>" << String(options.uiBridgesTimeout) << "</UIBridgesTimeout>\n";
  1273. if (isPlugin)
  1274. {
  1275. outSettings << " <LADSPA_PATH>" << xmlSafeString(options.pathLADSPA, true) << "</LADSPA_PATH>\n";
  1276. outSettings << " <DSSI_PATH>" << xmlSafeString(options.pathDSSI, true) << "</DSSI_PATH>\n";
  1277. outSettings << " <LV2_PATH>" << xmlSafeString(options.pathLV2, true) << "</LV2_PATH>\n";
  1278. outSettings << " <VST2_PATH>" << xmlSafeString(options.pathVST2, true) << "</VST2_PATH>\n";
  1279. outSettings << " <VST3_PATH>" << xmlSafeString(options.pathVST3, true) << "</VST3_PATH>\n";
  1280. outSettings << " <GIG_PATH>" << xmlSafeString(options.pathGIG, true) << "</GIG_PATH>\n";
  1281. outSettings << " <SF2_PATH>" << xmlSafeString(options.pathSF2, true) << "</SF2_PATH>\n";
  1282. outSettings << " <SFZ_PATH>" << xmlSafeString(options.pathSFZ, true) << "</SFZ_PATH>\n";
  1283. }
  1284. outSettings << " </EngineSettings>\n";
  1285. outStream << outSettings;
  1286. char strBuf[STR_MAX+1];
  1287. for (uint i=0; i < pData->curPluginCount; ++i)
  1288. {
  1289. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1290. if (plugin != nullptr && plugin->isEnabled())
  1291. {
  1292. MemoryOutputStream outPlugin(4096);
  1293. outPlugin << "\n";
  1294. strBuf[0] = '\0';
  1295. plugin->getRealName(strBuf);
  1296. if (strBuf[0] != '\0')
  1297. outPlugin << " <!-- " << xmlSafeString(strBuf, true) << " -->\n";
  1298. outPlugin << " <Plugin>\n";
  1299. outPlugin << plugin->getStateSave(false).toString();
  1300. outPlugin << " </Plugin>\n";
  1301. outStream << outPlugin;
  1302. }
  1303. }
  1304. #ifndef BUILD_BRIDGE
  1305. // tell bridges we're done saving
  1306. for (uint i=0; i < pData->curPluginCount; ++i)
  1307. {
  1308. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1309. if (plugin != nullptr && plugin->isEnabled() && (plugin->getHints() & PLUGIN_IS_BRIDGE) != 0)
  1310. plugin->setCustomData(CUSTOM_DATA_TYPE_STRING, "__CarlaPingOnOff__", "true", false);
  1311. }
  1312. bool saveConnections = true;
  1313. // if we're running inside some session-manager, let them handle the connections
  1314. if (pData->options.processMode != ENGINE_PROCESS_MODE_PATCHBAY)
  1315. {
  1316. /**/ if (std::getenv("CARLA_DONT_MANAGE_CONNECTIONS") != nullptr)
  1317. saveConnections = false;
  1318. else if (std::getenv("LADISH_APP_NAME") != nullptr)
  1319. saveConnections = false;
  1320. else if (std::getenv("NSM_URL") != nullptr)
  1321. saveConnections = false;
  1322. else if (std::strcmp(getCurrentDriverName(), "Plugin") == 0)
  1323. saveConnections = false;
  1324. }
  1325. if (saveConnections)
  1326. {
  1327. if (const char* const* const patchbayConns = getPatchbayConnections())
  1328. {
  1329. MemoryOutputStream outPatchbay(2048);
  1330. outPatchbay << "\n <Patchbay>\n";
  1331. for (int i=0; patchbayConns[i] != nullptr && patchbayConns[i+1] != nullptr; ++i, ++i )
  1332. {
  1333. const char* const connSource(patchbayConns[i]);
  1334. const char* const connTarget(patchbayConns[i+1]);
  1335. CARLA_SAFE_ASSERT_CONTINUE(connSource != nullptr && connSource[0] != '\0');
  1336. CARLA_SAFE_ASSERT_CONTINUE(connTarget != nullptr && connTarget[0] != '\0');
  1337. outPatchbay << " <Connection>\n";
  1338. outPatchbay << " <Source>" << connSource << "</Source>\n";
  1339. outPatchbay << " <Target>" << connTarget << "</Target>\n";
  1340. outPatchbay << " </Connection>\n";
  1341. }
  1342. outPatchbay << " </Patchbay>\n";
  1343. outStream << outPatchbay;
  1344. }
  1345. }
  1346. #endif
  1347. outStream << "</CARLA-PROJECT>\n";
  1348. }
  1349. bool CarlaEngine::loadProjectInternal(juce::XmlDocument& xmlDoc)
  1350. {
  1351. ScopedPointer<XmlElement> xmlElement(xmlDoc.getDocumentElement(true));
  1352. CARLA_SAFE_ASSERT_RETURN_ERR(xmlElement != nullptr, "Failed to parse project file");
  1353. const String& xmlType(xmlElement->getTagName());
  1354. const bool isPreset(xmlType.equalsIgnoreCase("carla-preset"));
  1355. if (! (xmlType.equalsIgnoreCase("carla-project") || isPreset))
  1356. {
  1357. setLastError("Not a valid Carla project or preset file");
  1358. return false;
  1359. }
  1360. // completely load file
  1361. xmlElement = xmlDoc.getDocumentElement(false);
  1362. CARLA_SAFE_ASSERT_RETURN_ERR(xmlElement != nullptr, "Failed to completely parse project file");
  1363. const bool isPlugin(std::strcmp(getCurrentDriverName(), "Plugin") == 0);
  1364. // engine settings
  1365. for (XmlElement* elem = xmlElement->getFirstChildElement(); elem != nullptr; elem = elem->getNextElement())
  1366. {
  1367. const String& tagName(elem->getTagName());
  1368. if (! tagName.equalsIgnoreCase("enginesettings"))
  1369. continue;
  1370. for (XmlElement* settElem = elem->getFirstChildElement(); settElem != nullptr; settElem = settElem->getNextElement())
  1371. {
  1372. const String& tag(settElem->getTagName());
  1373. const String text(settElem->getAllSubText().trim());
  1374. /** some settings might be incorrect or require extra work,
  1375. so we call setOption rather than modifying them direly */
  1376. int option = -1;
  1377. int value = 0;
  1378. const char* valueStr = nullptr;
  1379. /**/ if (tag.equalsIgnoreCase("forcestereo"))
  1380. {
  1381. option = ENGINE_OPTION_FORCE_STEREO;
  1382. value = text.equalsIgnoreCase("true") ? 1 : 0;
  1383. }
  1384. else if (tag.equalsIgnoreCase("preferpluginbridges"))
  1385. {
  1386. option = ENGINE_OPTION_PREFER_PLUGIN_BRIDGES;
  1387. value = text.equalsIgnoreCase("true") ? 1 : 0;
  1388. }
  1389. else if (tag.equalsIgnoreCase("preferuibridges"))
  1390. {
  1391. option = ENGINE_OPTION_PREFER_UI_BRIDGES;
  1392. value = text.equalsIgnoreCase("true") ? 1 : 0;
  1393. }
  1394. else if (tag.equalsIgnoreCase("uisalwaysontop"))
  1395. {
  1396. option = ENGINE_OPTION_UIS_ALWAYS_ON_TOP;
  1397. value = text.equalsIgnoreCase("true") ? 1 : 0;
  1398. }
  1399. else if (tag.equalsIgnoreCase("maxparameters"))
  1400. {
  1401. option = ENGINE_OPTION_MAX_PARAMETERS;
  1402. value = text.getIntValue();
  1403. }
  1404. else if (tag.equalsIgnoreCase("uibridgestimeout"))
  1405. {
  1406. option = ENGINE_OPTION_UI_BRIDGES_TIMEOUT;
  1407. value = text.getIntValue();
  1408. }
  1409. else if (isPlugin)
  1410. {
  1411. /**/ if (tag.equalsIgnoreCase("LADSPA_PATH"))
  1412. {
  1413. option = ENGINE_OPTION_PLUGIN_PATH;
  1414. value = PLUGIN_LADSPA;
  1415. valueStr = text.toRawUTF8();
  1416. }
  1417. else if (tag.equalsIgnoreCase("DSSI_PATH"))
  1418. {
  1419. option = ENGINE_OPTION_PLUGIN_PATH;
  1420. value = PLUGIN_DSSI;
  1421. valueStr = text.toRawUTF8();
  1422. }
  1423. else if (tag.equalsIgnoreCase("LV2_PATH"))
  1424. {
  1425. option = ENGINE_OPTION_PLUGIN_PATH;
  1426. value = PLUGIN_LV2;
  1427. valueStr = text.toRawUTF8();
  1428. }
  1429. else if (tag.equalsIgnoreCase("VST2_PATH"))
  1430. {
  1431. option = ENGINE_OPTION_PLUGIN_PATH;
  1432. value = PLUGIN_VST2;
  1433. valueStr = text.toRawUTF8();
  1434. }
  1435. else if (tag.equalsIgnoreCase("VST3_PATH"))
  1436. {
  1437. option = ENGINE_OPTION_PLUGIN_PATH;
  1438. value = PLUGIN_VST3;
  1439. valueStr = text.toRawUTF8();
  1440. }
  1441. else if (tag.equalsIgnoreCase("GIG_PATH"))
  1442. {
  1443. option = ENGINE_OPTION_PLUGIN_PATH;
  1444. value = PLUGIN_GIG;
  1445. valueStr = text.toRawUTF8();
  1446. }
  1447. else if (tag.equalsIgnoreCase("SF2_PATH"))
  1448. {
  1449. option = ENGINE_OPTION_PLUGIN_PATH;
  1450. value = PLUGIN_SF2;
  1451. valueStr = text.toRawUTF8();
  1452. }
  1453. else if (tag.equalsIgnoreCase("SFZ_PATH"))
  1454. {
  1455. option = ENGINE_OPTION_PLUGIN_PATH;
  1456. value = PLUGIN_SFZ;
  1457. valueStr = text.toRawUTF8();
  1458. }
  1459. }
  1460. CARLA_SAFE_ASSERT_CONTINUE(option != -1);
  1461. setOption(static_cast<EngineOption>(option), value, valueStr);
  1462. }
  1463. break;
  1464. }
  1465. // handle plugins first
  1466. for (XmlElement* elem = xmlElement->getFirstChildElement(); elem != nullptr; elem = elem->getNextElement())
  1467. {
  1468. const String& tagName(elem->getTagName());
  1469. if (isPreset || tagName.equalsIgnoreCase("plugin"))
  1470. {
  1471. CarlaStateSave stateSave;
  1472. stateSave.fillFromXmlElement(isPreset ? xmlElement.get() : elem);
  1473. callback(ENGINE_CALLBACK_IDLE, 0, 0, 0, 0.0f, nullptr);
  1474. CARLA_SAFE_ASSERT_CONTINUE(stateSave.type != nullptr);
  1475. const void* extraStuff = nullptr;
  1476. // check if using GIG or SF2 16outs
  1477. static const char kUse16OutsSuffix[] = " (16 outs)";
  1478. const BinaryType btype(getBinaryTypeFromFile(stateSave.binary));
  1479. const PluginType ptype(getPluginTypeFromString(stateSave.type));
  1480. if (CarlaString(stateSave.label).endsWith(kUse16OutsSuffix))
  1481. {
  1482. if (ptype == PLUGIN_GIG || ptype == PLUGIN_SF2)
  1483. extraStuff = "true";
  1484. }
  1485. // TODO - proper find&load plugins
  1486. if (addPlugin(btype, ptype, stateSave.binary, stateSave.name, stateSave.label, stateSave.uniqueId, extraStuff, stateSave.options))
  1487. {
  1488. if (CarlaPlugin* const plugin = getPlugin(pData->curPluginCount-1))
  1489. {
  1490. #ifndef BUILD_BRIDGE
  1491. // deactivate bridge client-side ping check, since some plugins block during load
  1492. if ((plugin->getHints() & PLUGIN_IS_BRIDGE) != 0 && ! isPreset)
  1493. plugin->setCustomData(CUSTOM_DATA_TYPE_STRING, "__CarlaPingOnOff__", "false", false);
  1494. #endif
  1495. plugin->loadStateSave(stateSave);
  1496. }
  1497. }
  1498. else
  1499. carla_stderr2("Failed to load a plugin, error was:\n%s", getLastError());
  1500. }
  1501. if (isPreset)
  1502. return true;
  1503. }
  1504. #ifndef BUILD_BRIDGE
  1505. // tell bridges we're done loading
  1506. for (uint i=0; i < pData->curPluginCount; ++i)
  1507. {
  1508. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1509. if (plugin != nullptr && plugin->isEnabled() && (plugin->getHints() & PLUGIN_IS_BRIDGE) != 0)
  1510. plugin->setCustomData(CUSTOM_DATA_TYPE_STRING, "__CarlaPingOnOff__", "true", false);
  1511. }
  1512. callback(ENGINE_CALLBACK_IDLE, 0, 0, 0, 0.0f, nullptr);
  1513. // if we're running inside some session-manager, let them handle the connections
  1514. if (pData->options.processMode != ENGINE_PROCESS_MODE_PATCHBAY)
  1515. {
  1516. /**/ if (std::getenv("CARLA_DONT_MANAGE_CONNECTIONS") != nullptr)
  1517. return true;
  1518. else if (std::getenv("LADISH_APP_NAME") != nullptr)
  1519. return true;
  1520. else if (std::getenv("NSM_URL") != nullptr)
  1521. return true;
  1522. else if (std::strcmp(getCurrentDriverName(), "Plugin") == 0)
  1523. return true;
  1524. }
  1525. // now handle connections
  1526. for (XmlElement* elem = xmlElement->getFirstChildElement(); elem != nullptr; elem = elem->getNextElement())
  1527. {
  1528. const String& tagName(elem->getTagName());
  1529. if (! tagName.equalsIgnoreCase("patchbay"))
  1530. continue;
  1531. CarlaString sourcePort, targetPort;
  1532. for (XmlElement* patchElem = elem->getFirstChildElement(); patchElem != nullptr; patchElem = patchElem->getNextElement())
  1533. {
  1534. const String& patchTag(patchElem->getTagName());
  1535. sourcePort.clear();
  1536. targetPort.clear();
  1537. if (! patchTag.equalsIgnoreCase("connection"))
  1538. continue;
  1539. for (XmlElement* connElem = patchElem->getFirstChildElement(); connElem != nullptr; connElem = connElem->getNextElement())
  1540. {
  1541. const String& tag(connElem->getTagName());
  1542. const String text(connElem->getAllSubText().trim());
  1543. /**/ if (tag.equalsIgnoreCase("source"))
  1544. sourcePort = text.toRawUTF8();
  1545. else if (tag.equalsIgnoreCase("target"))
  1546. targetPort = text.toRawUTF8();
  1547. }
  1548. if (sourcePort.isNotEmpty() && targetPort.isNotEmpty())
  1549. restorePatchbayConnection(sourcePort, targetPort);
  1550. }
  1551. break;
  1552. }
  1553. #endif
  1554. return true;
  1555. }
  1556. // -----------------------------------------------------------------------
  1557. CARLA_BACKEND_END_NAMESPACE