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.

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