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.

1931 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.getFileNameWithoutExtension().toRawUTF8());
  751. CarlaString extension(file.getFileExtension().replace(".","").toLowerCase().toRawUTF8());
  752. const uint curPluginId(pData->nextPluginId < pData->curPluginCount ? pData->nextPluginId : pData->curPluginCount);
  753. // -------------------------------------------------------------------
  754. if (extension == "carxp" || extension == "carxs")
  755. return loadProject(filename);
  756. // -------------------------------------------------------------------
  757. if (extension == "gig")
  758. return addPlugin(PLUGIN_GIG, filename, baseName, baseName, 0, nullptr);
  759. if (extension == "sf2")
  760. return addPlugin(PLUGIN_SF2, filename, baseName, baseName, 0, nullptr);
  761. if (extension == "sfz")
  762. return addPlugin(PLUGIN_SFZ, filename, baseName, baseName, 0, nullptr);
  763. // -------------------------------------------------------------------
  764. if (extension == "aif" || extension == "aiff" || extension == "bwf" || extension == "flac" || extension == "ogg" || extension == "wav")
  765. {
  766. if (addPlugin(PLUGIN_INTERNAL, nullptr, baseName, "audiofile", 0, nullptr))
  767. {
  768. if (CarlaPlugin* const plugin = getPlugin(curPluginId))
  769. plugin->setCustomData(CUSTOM_DATA_TYPE_STRING, "file", filename, true);
  770. return true;
  771. }
  772. return false;
  773. }
  774. // -------------------------------------------------------------------
  775. if (extension == "mid" || extension == "midi")
  776. {
  777. if (addPlugin(PLUGIN_INTERNAL, nullptr, baseName, "midifile", 0, nullptr))
  778. {
  779. if (CarlaPlugin* const plugin = getPlugin(curPluginId))
  780. plugin->setCustomData(CUSTOM_DATA_TYPE_STRING, "file", filename, true);
  781. return true;
  782. }
  783. return false;
  784. }
  785. // -------------------------------------------------------------------
  786. // ZynAddSubFX
  787. if (extension == "xmz" || extension == "xiz")
  788. {
  789. #ifdef HAVE_ZYN_DEPS
  790. if (addPlugin(PLUGIN_INTERNAL, nullptr, baseName, "zynaddsubfx", 0, nullptr))
  791. {
  792. if (CarlaPlugin* const plugin = getPlugin(curPluginId))
  793. plugin->setCustomData(CUSTOM_DATA_TYPE_STRING, (extension == "xmz") ? "CarlaAlternateFile1" : "CarlaAlternateFile2", filename, true);
  794. return true;
  795. }
  796. return false;
  797. #else
  798. setLastError("This Carla build does not have ZynAddSubFX support");
  799. return false;
  800. #endif
  801. }
  802. // -------------------------------------------------------------------
  803. setLastError("Unknown file extension");
  804. return false;
  805. }
  806. bool CarlaEngine::loadProject(const char* const filename)
  807. {
  808. CARLA_SAFE_ASSERT_RETURN_ERR(pData->isIdling == 0, "An operation is still being processed, please wait for it to finish");
  809. CARLA_SAFE_ASSERT_RETURN_ERR(filename != nullptr && filename[0] != '\0', "Invalid filename");
  810. carla_debug("CarlaEngine::loadProject(\"%s\")", filename);
  811. const String jfilename = String(CharPointer_UTF8(filename));
  812. File file(jfilename);
  813. CARLA_SAFE_ASSERT_RETURN_ERR(file.existsAsFile(), "Requested file does not exist or is not a readable file");
  814. XmlDocument xml(file);
  815. return loadProjectInternal(xml);
  816. }
  817. bool CarlaEngine::saveProject(const char* const filename)
  818. {
  819. CARLA_SAFE_ASSERT_RETURN_ERR(filename != nullptr && filename[0] != '\0', "Invalid filename");
  820. carla_debug("CarlaEngine::saveProject(\"%s\")", filename);
  821. MemoryOutputStream out;
  822. saveProjectInternal(out);
  823. const String jfilename = String(CharPointer_UTF8(filename));
  824. File file(jfilename);
  825. if (file.replaceWithData(out.getData(), out.getDataSize()))
  826. return true;
  827. setLastError("Failed to write file");
  828. return false;
  829. }
  830. // -----------------------------------------------------------------------
  831. // Information (base)
  832. uint CarlaEngine::getHints() const noexcept
  833. {
  834. return pData->hints;
  835. }
  836. uint32_t CarlaEngine::getBufferSize() const noexcept
  837. {
  838. return pData->bufferSize;
  839. }
  840. double CarlaEngine::getSampleRate() const noexcept
  841. {
  842. return pData->sampleRate;
  843. }
  844. const char* CarlaEngine::getName() const noexcept
  845. {
  846. return pData->name;
  847. }
  848. EngineProcessMode CarlaEngine::getProccessMode() const noexcept
  849. {
  850. return pData->options.processMode;
  851. }
  852. const EngineOptions& CarlaEngine::getOptions() const noexcept
  853. {
  854. return pData->options;
  855. }
  856. const EngineTimeInfo& CarlaEngine::getTimeInfo() const noexcept
  857. {
  858. return pData->timeInfo;
  859. }
  860. // -----------------------------------------------------------------------
  861. // Information (peaks)
  862. float CarlaEngine::getInputPeak(const uint pluginId, const bool isLeft) const noexcept
  863. {
  864. CARLA_SAFE_ASSERT_RETURN(pluginId < pData->curPluginCount, 0.0f);
  865. return pData->plugins[pluginId].insPeak[isLeft ? 0 : 1];
  866. }
  867. float CarlaEngine::getOutputPeak(const uint pluginId, const bool isLeft) const noexcept
  868. {
  869. CARLA_SAFE_ASSERT_RETURN(pluginId < pData->curPluginCount, 0.0f);
  870. return pData->plugins[pluginId].outsPeak[isLeft ? 0 : 1];
  871. }
  872. // -----------------------------------------------------------------------
  873. // Callback
  874. void CarlaEngine::callback(const EngineCallbackOpcode action, const uint pluginId, const int value1, const int value2, const float value3, const char* const valueStr) noexcept
  875. {
  876. #ifdef DEBUG
  877. if (action != ENGINE_CALLBACK_IDLE)
  878. carla_debug("CarlaEngine::callback(%i:%s, %i, %i, %i, %f, \"%s\")", action, EngineCallbackOpcode2Str(action), pluginId, value1, value2, value3, valueStr);
  879. #endif
  880. #ifdef BUILD_BRIDGE
  881. if (pData->isIdling)
  882. #else
  883. if (pData->isIdling && action != ENGINE_CALLBACK_PATCHBAY_CLIENT_DATA_CHANGED)
  884. #endif
  885. {
  886. carla_stdout("callback while idling (%i:%s, %i, %i, %i, %f, \"%s\")", action, EngineCallbackOpcode2Str(action), pluginId, value1, value2, value3, valueStr);
  887. }
  888. if (pData->callback != nullptr)
  889. {
  890. if (action == ENGINE_CALLBACK_IDLE)
  891. ++pData->isIdling;
  892. try {
  893. pData->callback(pData->callbackPtr, action, pluginId, value1, value2, value3, valueStr);
  894. } CARLA_SAFE_EXCEPTION("callback");
  895. if (action == ENGINE_CALLBACK_IDLE)
  896. --pData->isIdling;
  897. }
  898. }
  899. void CarlaEngine::setCallback(const EngineCallbackFunc func, void* const ptr) noexcept
  900. {
  901. carla_debug("CarlaEngine::setCallback(%p, %p)", func, ptr);
  902. pData->callback = func;
  903. pData->callbackPtr = ptr;
  904. }
  905. // -----------------------------------------------------------------------
  906. // File Callback
  907. const char* CarlaEngine::runFileCallback(const FileCallbackOpcode action, const bool isDir, const char* const title, const char* const filter) noexcept
  908. {
  909. CARLA_SAFE_ASSERT_RETURN(title != nullptr && title[0] != '\0', nullptr);
  910. CARLA_SAFE_ASSERT_RETURN(filter != nullptr, nullptr);
  911. carla_debug("CarlaEngine::runFileCallback(%i:%s, %s, \"%s\", \"%s\")", action, FileCallbackOpcode2Str(action), bool2str(isDir), title, filter);
  912. const char* ret = nullptr;
  913. if (pData->fileCallback != nullptr)
  914. {
  915. try {
  916. ret = pData->fileCallback(pData->fileCallbackPtr, action, isDir, title, filter);
  917. } CARLA_SAFE_EXCEPTION("runFileCallback");
  918. }
  919. return ret;
  920. }
  921. void CarlaEngine::setFileCallback(const FileCallbackFunc func, void* const ptr) noexcept
  922. {
  923. carla_debug("CarlaEngine::setFileCallback(%p, %p)", func, ptr);
  924. pData->fileCallback = func;
  925. pData->fileCallbackPtr = ptr;
  926. }
  927. // -----------------------------------------------------------------------
  928. // Transport
  929. void CarlaEngine::transportPlay() noexcept
  930. {
  931. pData->time.playing = true;
  932. }
  933. void CarlaEngine::transportPause() noexcept
  934. {
  935. pData->time.playing = false;
  936. }
  937. void CarlaEngine::transportRelocate(const uint64_t frame) noexcept
  938. {
  939. pData->time.frame = frame;
  940. }
  941. // -----------------------------------------------------------------------
  942. // Error handling
  943. const char* CarlaEngine::getLastError() const noexcept
  944. {
  945. return pData->lastError;
  946. }
  947. void CarlaEngine::setLastError(const char* const error) const noexcept
  948. {
  949. pData->lastError = error;
  950. }
  951. void CarlaEngine::setAboutToClose() noexcept
  952. {
  953. carla_debug("CarlaEngine::setAboutToClose()");
  954. pData->aboutToClose = true;
  955. }
  956. // -----------------------------------------------------------------------
  957. // Global options
  958. void CarlaEngine::setOption(const EngineOption option, const int value, const char* const valueStr) noexcept
  959. {
  960. carla_debug("CarlaEngine::setOption(%i:%s, %i, \"%s\")", option, EngineOption2Str(option), value, valueStr);
  961. if (isRunning() && (option == ENGINE_OPTION_PROCESS_MODE || option == ENGINE_OPTION_AUDIO_NUM_PERIODS || option == ENGINE_OPTION_AUDIO_DEVICE))
  962. return carla_stderr("CarlaEngine::setOption(%i:%s, %i, \"%s\") - Cannot set this option while engine is running!", option, EngineOption2Str(option), value, valueStr);
  963. // do not un-force stereo for rack mode
  964. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK && option == ENGINE_OPTION_FORCE_STEREO && value != 0)
  965. return;
  966. switch (option)
  967. {
  968. case ENGINE_OPTION_DEBUG:
  969. break;
  970. case ENGINE_OPTION_PROCESS_MODE:
  971. CARLA_SAFE_ASSERT_RETURN(value >= ENGINE_PROCESS_MODE_SINGLE_CLIENT && value <= ENGINE_PROCESS_MODE_BRIDGE,);
  972. pData->options.processMode = static_cast<EngineProcessMode>(value);
  973. break;
  974. case ENGINE_OPTION_TRANSPORT_MODE:
  975. CARLA_SAFE_ASSERT_RETURN(value >= ENGINE_TRANSPORT_MODE_INTERNAL && value <= ENGINE_TRANSPORT_MODE_BRIDGE,);
  976. pData->options.transportMode = static_cast<EngineTransportMode>(value);
  977. break;
  978. case ENGINE_OPTION_FORCE_STEREO:
  979. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  980. pData->options.forceStereo = (value != 0);
  981. break;
  982. case ENGINE_OPTION_PREFER_PLUGIN_BRIDGES:
  983. #ifdef BUILD_BRIDGE
  984. CARLA_SAFE_ASSERT_RETURN(value == 0,);
  985. #else
  986. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  987. #endif
  988. pData->options.preferPluginBridges = (value != 0);
  989. break;
  990. case ENGINE_OPTION_PREFER_UI_BRIDGES:
  991. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  992. pData->options.preferUiBridges = (value != 0);
  993. break;
  994. case ENGINE_OPTION_UIS_ALWAYS_ON_TOP:
  995. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  996. pData->options.uisAlwaysOnTop = (value != 0);
  997. break;
  998. case ENGINE_OPTION_MAX_PARAMETERS:
  999. CARLA_SAFE_ASSERT_RETURN(value >= 0,);
  1000. pData->options.maxParameters = static_cast<uint>(value);
  1001. break;
  1002. case ENGINE_OPTION_UI_BRIDGES_TIMEOUT:
  1003. CARLA_SAFE_ASSERT_RETURN(value >= 0,);
  1004. pData->options.uiBridgesTimeout = static_cast<uint>(value);
  1005. break;
  1006. case ENGINE_OPTION_AUDIO_NUM_PERIODS:
  1007. CARLA_SAFE_ASSERT_RETURN(value >= 2 && value <= 3,);
  1008. pData->options.audioNumPeriods = static_cast<uint>(value);
  1009. break;
  1010. case ENGINE_OPTION_AUDIO_BUFFER_SIZE:
  1011. CARLA_SAFE_ASSERT_RETURN(value >= 8,);
  1012. pData->options.audioBufferSize = static_cast<uint>(value);
  1013. break;
  1014. case ENGINE_OPTION_AUDIO_SAMPLE_RATE:
  1015. CARLA_SAFE_ASSERT_RETURN(value >= 22050,);
  1016. pData->options.audioSampleRate = static_cast<uint>(value);
  1017. break;
  1018. case ENGINE_OPTION_AUDIO_DEVICE:
  1019. CARLA_SAFE_ASSERT_RETURN(valueStr != nullptr,);
  1020. if (pData->options.audioDevice != nullptr)
  1021. delete[] pData->options.audioDevice;
  1022. pData->options.audioDevice = carla_strdup_safe(valueStr);
  1023. break;
  1024. case ENGINE_OPTION_PLUGIN_PATH:
  1025. CARLA_SAFE_ASSERT_RETURN(value > PLUGIN_NONE,);
  1026. CARLA_SAFE_ASSERT_RETURN(value <= PLUGIN_SFZ,);
  1027. switch (value)
  1028. {
  1029. case PLUGIN_LADSPA:
  1030. if (pData->options.pathLADSPA != nullptr)
  1031. delete[] pData->options.pathLADSPA;
  1032. if (valueStr != nullptr)
  1033. pData->options.pathLADSPA = carla_strdup_safe(valueStr);
  1034. else
  1035. pData->options.pathLADSPA = nullptr;
  1036. break;
  1037. case PLUGIN_DSSI:
  1038. if (pData->options.pathDSSI != nullptr)
  1039. delete[] pData->options.pathDSSI;
  1040. if (valueStr != nullptr)
  1041. pData->options.pathDSSI = carla_strdup_safe(valueStr);
  1042. else
  1043. pData->options.pathDSSI = nullptr;
  1044. break;
  1045. case PLUGIN_LV2:
  1046. if (pData->options.pathLV2 != nullptr)
  1047. delete[] pData->options.pathLV2;
  1048. if (valueStr != nullptr)
  1049. pData->options.pathLV2 = carla_strdup_safe(valueStr);
  1050. else
  1051. pData->options.pathLV2 = nullptr;
  1052. break;
  1053. case PLUGIN_VST2:
  1054. if (pData->options.pathVST2 != nullptr)
  1055. delete[] pData->options.pathVST2;
  1056. if (valueStr != nullptr)
  1057. pData->options.pathVST2 = carla_strdup_safe(valueStr);
  1058. else
  1059. pData->options.pathVST2 = nullptr;
  1060. break;
  1061. case PLUGIN_VST3:
  1062. if (pData->options.pathVST3 != nullptr)
  1063. delete[] pData->options.pathVST3;
  1064. if (valueStr != nullptr)
  1065. pData->options.pathVST3 = carla_strdup_safe(valueStr);
  1066. else
  1067. pData->options.pathVST3 = nullptr;
  1068. break;
  1069. case PLUGIN_GIG:
  1070. if (pData->options.pathGIG != nullptr)
  1071. delete[] pData->options.pathGIG;
  1072. if (valueStr != nullptr)
  1073. pData->options.pathGIG = carla_strdup_safe(valueStr);
  1074. else
  1075. pData->options.pathGIG = nullptr;
  1076. break;
  1077. case PLUGIN_SF2:
  1078. if (pData->options.pathSF2 != nullptr)
  1079. delete[] pData->options.pathSF2;
  1080. if (valueStr != nullptr)
  1081. pData->options.pathSF2 = carla_strdup_safe(valueStr);
  1082. else
  1083. pData->options.pathSF2 = nullptr;
  1084. break;
  1085. case PLUGIN_SFZ:
  1086. if (pData->options.pathSFZ != nullptr)
  1087. delete[] pData->options.pathSFZ;
  1088. if (valueStr != nullptr)
  1089. pData->options.pathSFZ = carla_strdup_safe(valueStr);
  1090. else
  1091. pData->options.pathSFZ = nullptr;
  1092. break;
  1093. default:
  1094. return carla_stderr("CarlaEngine::setOption(%i:%s, %i, \"%s\") - Invalid plugin type", option, EngineOption2Str(option), value, valueStr);
  1095. break;
  1096. }
  1097. break;
  1098. case ENGINE_OPTION_PATH_BINARIES:
  1099. CARLA_SAFE_ASSERT_RETURN(valueStr != nullptr && valueStr[0] != '\0',);
  1100. if (pData->options.binaryDir != nullptr)
  1101. delete[] pData->options.binaryDir;
  1102. pData->options.binaryDir = carla_strdup_safe(valueStr);
  1103. break;
  1104. case ENGINE_OPTION_PATH_RESOURCES:
  1105. CARLA_SAFE_ASSERT_RETURN(valueStr != nullptr && valueStr[0] != '\0',);
  1106. if (pData->options.resourceDir != nullptr)
  1107. delete[] pData->options.resourceDir;
  1108. pData->options.resourceDir = carla_strdup_safe(valueStr);
  1109. break;
  1110. case ENGINE_OPTION_PREVENT_BAD_BEHAVIOUR:
  1111. CARLA_SAFE_ASSERT_RETURN(pData->options.binaryDir != nullptr && pData->options.binaryDir[0] != '\0',);
  1112. #ifdef CARLA_OS_LINUX
  1113. if (value != 0)
  1114. {
  1115. CarlaString interposerPath(CarlaString(pData->options.binaryDir) + CARLA_OS_SEP_STR "libcarla_interposer.so");
  1116. ::setenv("LD_PRELOAD", interposerPath.buffer(), 1);
  1117. }
  1118. else
  1119. {
  1120. ::unsetenv("LD_PRELOAD");
  1121. }
  1122. #endif
  1123. break;
  1124. case ENGINE_OPTION_FRONTEND_WIN_ID:
  1125. CARLA_SAFE_ASSERT_RETURN(valueStr != nullptr && valueStr[0] != '\0',);
  1126. const long long winId(std::strtoll(valueStr, nullptr, 16));
  1127. CARLA_SAFE_ASSERT_RETURN(winId >= 0,);
  1128. pData->options.frontendWinId = static_cast<uintptr_t>(winId);
  1129. break;
  1130. }
  1131. }
  1132. #ifdef HAVE_LIBLO
  1133. // -----------------------------------------------------------------------
  1134. // OSC Stuff
  1135. # ifndef BUILD_BRIDGE
  1136. bool CarlaEngine::isOscControlRegistered() const noexcept
  1137. {
  1138. return pData->osc.isControlRegistered();
  1139. }
  1140. # endif
  1141. void CarlaEngine::idleOsc() const noexcept
  1142. {
  1143. pData->osc.idle();
  1144. }
  1145. const char* CarlaEngine::getOscServerPathTCP() const noexcept
  1146. {
  1147. return pData->osc.getServerPathTCP();
  1148. }
  1149. const char* CarlaEngine::getOscServerPathUDP() const noexcept
  1150. {
  1151. return pData->osc.getServerPathUDP();
  1152. }
  1153. #endif
  1154. // -----------------------------------------------------------------------
  1155. // Helper functions
  1156. EngineEvent* CarlaEngine::getInternalEventBuffer(const bool isInput) const noexcept
  1157. {
  1158. return isInput ? pData->events.in : pData->events.out;
  1159. }
  1160. void CarlaEngine::lockEnvironment() const noexcept
  1161. {
  1162. pData->envMutex.lock();
  1163. }
  1164. void CarlaEngine::unlockEnvironment() const noexcept
  1165. {
  1166. pData->envMutex.unlock();
  1167. }
  1168. // -----------------------------------------------------------------------
  1169. // Internal stuff
  1170. void CarlaEngine::bufferSizeChanged(const uint32_t newBufferSize)
  1171. {
  1172. carla_debug("CarlaEngine::bufferSizeChanged(%i)", newBufferSize);
  1173. #ifndef BUILD_BRIDGE
  1174. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK ||
  1175. pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  1176. {
  1177. pData->graph.setBufferSize(newBufferSize);
  1178. }
  1179. #endif
  1180. for (uint i=0; i < pData->curPluginCount; ++i)
  1181. {
  1182. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1183. if (plugin != nullptr && plugin->isEnabled())
  1184. plugin->bufferSizeChanged(newBufferSize);
  1185. }
  1186. callback(ENGINE_CALLBACK_BUFFER_SIZE_CHANGED, 0, static_cast<int>(newBufferSize), 0, 0.0f, nullptr);
  1187. }
  1188. void CarlaEngine::sampleRateChanged(const double newSampleRate)
  1189. {
  1190. carla_debug("CarlaEngine::sampleRateChanged(%g)", newSampleRate);
  1191. #ifndef BUILD_BRIDGE
  1192. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK ||
  1193. pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  1194. {
  1195. pData->graph.setSampleRate(newSampleRate);
  1196. }
  1197. #endif
  1198. for (uint i=0; i < pData->curPluginCount; ++i)
  1199. {
  1200. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1201. if (plugin != nullptr && plugin->isEnabled())
  1202. plugin->sampleRateChanged(newSampleRate);
  1203. }
  1204. callback(ENGINE_CALLBACK_SAMPLE_RATE_CHANGED, 0, 0, 0, static_cast<float>(newSampleRate), nullptr);
  1205. }
  1206. void CarlaEngine::offlineModeChanged(const bool isOfflineNow)
  1207. {
  1208. carla_debug("CarlaEngine::offlineModeChanged(%s)", bool2str(isOfflineNow));
  1209. #ifndef BUILD_BRIDGE
  1210. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK ||
  1211. pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  1212. {
  1213. pData->graph.setOffline(isOfflineNow);
  1214. }
  1215. #endif
  1216. for (uint i=0; i < pData->curPluginCount; ++i)
  1217. {
  1218. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1219. if (plugin != nullptr && plugin->isEnabled())
  1220. plugin->offlineModeChanged(isOfflineNow);
  1221. }
  1222. }
  1223. void CarlaEngine::runPendingRtEvents() noexcept
  1224. {
  1225. pData->doNextPluginAction(true);
  1226. if (pData->time.playing)
  1227. pData->time.frame += pData->bufferSize;
  1228. if (pData->options.transportMode == ENGINE_TRANSPORT_MODE_INTERNAL)
  1229. {
  1230. pData->timeInfo.playing = pData->time.playing;
  1231. pData->timeInfo.frame = pData->time.frame;
  1232. }
  1233. }
  1234. void CarlaEngine::setPluginPeaks(const uint pluginId, float const inPeaks[2], float const outPeaks[2]) noexcept
  1235. {
  1236. EnginePluginData& pluginData(pData->plugins[pluginId]);
  1237. pluginData.insPeak[0] = inPeaks[0];
  1238. pluginData.insPeak[1] = inPeaks[1];
  1239. pluginData.outsPeak[0] = outPeaks[0];
  1240. pluginData.outsPeak[1] = outPeaks[1];
  1241. }
  1242. void CarlaEngine::saveProjectInternal(juce::MemoryOutputStream& outStream) const
  1243. {
  1244. // send initial prepareForSave first, giving time for bridges to act
  1245. for (uint i=0; i < pData->curPluginCount; ++i)
  1246. {
  1247. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1248. if (plugin != nullptr && plugin->isEnabled())
  1249. {
  1250. #ifndef BUILD_BRIDGE
  1251. // deactivate bridge client-side ping check, since some plugins block during save
  1252. if (plugin->getHints() & PLUGIN_IS_BRIDGE)
  1253. plugin->setCustomData(CUSTOM_DATA_TYPE_STRING, "__CarlaPingOnOff__", "false", false);
  1254. #endif
  1255. plugin->prepareForSave();
  1256. }
  1257. }
  1258. outStream << "<?xml version='1.0' encoding='UTF-8'?>\n";
  1259. outStream << "<!DOCTYPE CARLA-PROJECT>\n";
  1260. outStream << "<CARLA-PROJECT VERSION='2.0'>\n";
  1261. const bool isPlugin(std::strcmp(getCurrentDriverName(), "Plugin") == 0);
  1262. const EngineOptions& options(pData->options);
  1263. MemoryOutputStream outSettings(1024);
  1264. // save appropriate engine settings
  1265. outSettings << " <EngineSettings>\n";
  1266. //processMode
  1267. //transportMode
  1268. outSettings << " <ForceStereo>" << bool2str(options.forceStereo) << "</ForceStereo>\n";
  1269. outSettings << " <PreferPluginBridges>" << bool2str(options.preferPluginBridges) << "</PreferPluginBridges>\n";
  1270. outSettings << " <PreferUiBridges>" << bool2str(options.preferUiBridges) << "</PreferUiBridges>\n";
  1271. outSettings << " <UIsAlwaysOnTop>" << bool2str(options.uisAlwaysOnTop) << "</UIsAlwaysOnTop>\n";
  1272. outSettings << " <MaxParameters>" << String(options.maxParameters) << "</MaxParameters>\n";
  1273. outSettings << " <UIBridgesTimeout>" << String(options.uiBridgesTimeout) << "</UIBridgesTimeout>\n";
  1274. if (isPlugin)
  1275. {
  1276. outSettings << " <LADSPA_PATH>" << xmlSafeString(options.pathLADSPA, true) << "</LADSPA_PATH>\n";
  1277. outSettings << " <DSSI_PATH>" << xmlSafeString(options.pathDSSI, true) << "</DSSI_PATH>\n";
  1278. outSettings << " <LV2_PATH>" << xmlSafeString(options.pathLV2, true) << "</LV2_PATH>\n";
  1279. outSettings << " <VST2_PATH>" << xmlSafeString(options.pathVST2, true) << "</VST2_PATH>\n";
  1280. outSettings << " <VST3_PATH>" << xmlSafeString(options.pathVST3, true) << "</VST3_PATH>\n";
  1281. outSettings << " <GIG_PATH>" << xmlSafeString(options.pathGIG, true) << "</GIG_PATH>\n";
  1282. outSettings << " <SF2_PATH>" << xmlSafeString(options.pathSF2, true) << "</SF2_PATH>\n";
  1283. outSettings << " <SFZ_PATH>" << xmlSafeString(options.pathSFZ, true) << "</SFZ_PATH>\n";
  1284. }
  1285. outSettings << " </EngineSettings>\n";
  1286. outStream << outSettings;
  1287. char strBuf[STR_MAX+1];
  1288. for (uint i=0; i < pData->curPluginCount; ++i)
  1289. {
  1290. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1291. if (plugin != nullptr && plugin->isEnabled())
  1292. {
  1293. MemoryOutputStream outPlugin(4096);
  1294. outPlugin << "\n";
  1295. strBuf[0] = '\0';
  1296. plugin->getRealName(strBuf);
  1297. if (strBuf[0] != '\0')
  1298. outPlugin << " <!-- " << xmlSafeString(strBuf, true) << " -->\n";
  1299. outPlugin << " <Plugin>\n";
  1300. outPlugin << plugin->getStateSave(false).toString();
  1301. outPlugin << " </Plugin>\n";
  1302. outStream << outPlugin;
  1303. }
  1304. }
  1305. #ifndef BUILD_BRIDGE
  1306. // tell bridges we're done saving
  1307. for (uint i=0; i < pData->curPluginCount; ++i)
  1308. {
  1309. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1310. if (plugin != nullptr && plugin->isEnabled() && (plugin->getHints() & PLUGIN_IS_BRIDGE) != 0)
  1311. plugin->setCustomData(CUSTOM_DATA_TYPE_STRING, "__CarlaPingOnOff__", "true", false);
  1312. }
  1313. bool saveConnections = true;
  1314. // if we're running inside some session-manager, let them handle the connections
  1315. if (pData->options.processMode != ENGINE_PROCESS_MODE_PATCHBAY)
  1316. {
  1317. /**/ if (std::getenv("CARLA_DONT_MANAGE_CONNECTIONS") != nullptr)
  1318. saveConnections = false;
  1319. else if (std::getenv("LADISH_APP_NAME") != nullptr)
  1320. saveConnections = false;
  1321. else if (std::getenv("NSM_URL") != nullptr)
  1322. saveConnections = false;
  1323. else if (std::strcmp(getCurrentDriverName(), "Plugin") == 0)
  1324. saveConnections = false;
  1325. }
  1326. if (saveConnections)
  1327. {
  1328. if (const char* const* const patchbayConns = getPatchbayConnections())
  1329. {
  1330. MemoryOutputStream outPatchbay(2048);
  1331. outPatchbay << "\n <Patchbay>\n";
  1332. for (int i=0; patchbayConns[i] != nullptr && patchbayConns[i+1] != nullptr; ++i, ++i )
  1333. {
  1334. const char* const connSource(patchbayConns[i]);
  1335. const char* const connTarget(patchbayConns[i+1]);
  1336. CARLA_SAFE_ASSERT_CONTINUE(connSource != nullptr && connSource[0] != '\0');
  1337. CARLA_SAFE_ASSERT_CONTINUE(connTarget != nullptr && connTarget[0] != '\0');
  1338. outPatchbay << " <Connection>\n";
  1339. outPatchbay << " <Source>" << connSource << "</Source>\n";
  1340. outPatchbay << " <Target>" << connTarget << "</Target>\n";
  1341. outPatchbay << " </Connection>\n";
  1342. }
  1343. outPatchbay << " </Patchbay>\n";
  1344. outStream << outPatchbay;
  1345. }
  1346. }
  1347. #endif
  1348. outStream << "</CARLA-PROJECT>\n";
  1349. }
  1350. bool CarlaEngine::loadProjectInternal(juce::XmlDocument& xmlDoc)
  1351. {
  1352. ScopedPointer<XmlElement> xmlElement(xmlDoc.getDocumentElement(true));
  1353. CARLA_SAFE_ASSERT_RETURN_ERR(xmlElement != nullptr, "Failed to parse project file");
  1354. const String& xmlType(xmlElement->getTagName());
  1355. const bool isPreset(xmlType.equalsIgnoreCase("carla-preset"));
  1356. if (! (xmlType.equalsIgnoreCase("carla-project") || isPreset))
  1357. {
  1358. setLastError("Not a valid Carla project or preset file");
  1359. return false;
  1360. }
  1361. // completely load file
  1362. xmlElement = xmlDoc.getDocumentElement(false);
  1363. CARLA_SAFE_ASSERT_RETURN_ERR(xmlElement != nullptr, "Failed to completely parse project file");
  1364. const bool isPlugin(std::strcmp(getCurrentDriverName(), "Plugin") == 0);
  1365. // engine settings
  1366. for (XmlElement* elem = xmlElement->getFirstChildElement(); elem != nullptr; elem = elem->getNextElement())
  1367. {
  1368. const String& tagName(elem->getTagName());
  1369. if (! tagName.equalsIgnoreCase("enginesettings"))
  1370. continue;
  1371. for (XmlElement* settElem = elem->getFirstChildElement(); settElem != nullptr; settElem = settElem->getNextElement())
  1372. {
  1373. const String& tag(settElem->getTagName());
  1374. const String text(settElem->getAllSubText().trim());
  1375. /** some settings might be incorrect or require extra work,
  1376. so we call setOption rather than modifying them direly */
  1377. int option = -1;
  1378. int value = 0;
  1379. const char* valueStr = nullptr;
  1380. /**/ if (tag.equalsIgnoreCase("forcestereo"))
  1381. {
  1382. option = ENGINE_OPTION_FORCE_STEREO;
  1383. value = text.equalsIgnoreCase("true") ? 1 : 0;
  1384. }
  1385. else if (tag.equalsIgnoreCase("preferpluginbridges"))
  1386. {
  1387. option = ENGINE_OPTION_PREFER_PLUGIN_BRIDGES;
  1388. value = text.equalsIgnoreCase("true") ? 1 : 0;
  1389. }
  1390. else if (tag.equalsIgnoreCase("preferuibridges"))
  1391. {
  1392. option = ENGINE_OPTION_PREFER_UI_BRIDGES;
  1393. value = text.equalsIgnoreCase("true") ? 1 : 0;
  1394. }
  1395. else if (tag.equalsIgnoreCase("uisalwaysontop"))
  1396. {
  1397. option = ENGINE_OPTION_UIS_ALWAYS_ON_TOP;
  1398. value = text.equalsIgnoreCase("true") ? 1 : 0;
  1399. }
  1400. else if (tag.equalsIgnoreCase("maxparameters"))
  1401. {
  1402. option = ENGINE_OPTION_MAX_PARAMETERS;
  1403. value = text.getIntValue();
  1404. }
  1405. else if (tag.equalsIgnoreCase("uibridgestimeout"))
  1406. {
  1407. option = ENGINE_OPTION_UI_BRIDGES_TIMEOUT;
  1408. value = text.getIntValue();
  1409. }
  1410. else if (isPlugin)
  1411. {
  1412. /**/ if (tag.equalsIgnoreCase("LADSPA_PATH"))
  1413. {
  1414. option = ENGINE_OPTION_PLUGIN_PATH;
  1415. value = PLUGIN_LADSPA;
  1416. valueStr = text.toRawUTF8();
  1417. }
  1418. else if (tag.equalsIgnoreCase("DSSI_PATH"))
  1419. {
  1420. option = ENGINE_OPTION_PLUGIN_PATH;
  1421. value = PLUGIN_DSSI;
  1422. valueStr = text.toRawUTF8();
  1423. }
  1424. else if (tag.equalsIgnoreCase("LV2_PATH"))
  1425. {
  1426. option = ENGINE_OPTION_PLUGIN_PATH;
  1427. value = PLUGIN_LV2;
  1428. valueStr = text.toRawUTF8();
  1429. }
  1430. else if (tag.equalsIgnoreCase("VST2_PATH"))
  1431. {
  1432. option = ENGINE_OPTION_PLUGIN_PATH;
  1433. value = PLUGIN_VST2;
  1434. valueStr = text.toRawUTF8();
  1435. }
  1436. else if (tag.equalsIgnoreCase("VST3_PATH"))
  1437. {
  1438. option = ENGINE_OPTION_PLUGIN_PATH;
  1439. value = PLUGIN_VST3;
  1440. valueStr = text.toRawUTF8();
  1441. }
  1442. else if (tag.equalsIgnoreCase("GIG_PATH"))
  1443. {
  1444. option = ENGINE_OPTION_PLUGIN_PATH;
  1445. value = PLUGIN_GIG;
  1446. valueStr = text.toRawUTF8();
  1447. }
  1448. else if (tag.equalsIgnoreCase("SF2_PATH"))
  1449. {
  1450. option = ENGINE_OPTION_PLUGIN_PATH;
  1451. value = PLUGIN_SF2;
  1452. valueStr = text.toRawUTF8();
  1453. }
  1454. else if (tag.equalsIgnoreCase("SFZ_PATH"))
  1455. {
  1456. option = ENGINE_OPTION_PLUGIN_PATH;
  1457. value = PLUGIN_SFZ;
  1458. valueStr = text.toRawUTF8();
  1459. }
  1460. }
  1461. CARLA_SAFE_ASSERT_CONTINUE(option != -1);
  1462. setOption(static_cast<EngineOption>(option), value, valueStr);
  1463. }
  1464. break;
  1465. }
  1466. // handle plugins first
  1467. for (XmlElement* elem = xmlElement->getFirstChildElement(); elem != nullptr; elem = elem->getNextElement())
  1468. {
  1469. const String& tagName(elem->getTagName());
  1470. if (isPreset || tagName.equalsIgnoreCase("plugin"))
  1471. {
  1472. CarlaStateSave stateSave;
  1473. stateSave.fillFromXmlElement(isPreset ? xmlElement.get() : elem);
  1474. callback(ENGINE_CALLBACK_IDLE, 0, 0, 0, 0.0f, nullptr);
  1475. CARLA_SAFE_ASSERT_CONTINUE(stateSave.type != nullptr);
  1476. const void* extraStuff = nullptr;
  1477. // check if using GIG or SF2 16outs
  1478. static const char kUse16OutsSuffix[] = " (16 outs)";
  1479. const BinaryType btype(getBinaryTypeFromFile(stateSave.binary));
  1480. const PluginType ptype(getPluginTypeFromString(stateSave.type));
  1481. if (CarlaString(stateSave.label).endsWith(kUse16OutsSuffix))
  1482. {
  1483. if (ptype == PLUGIN_GIG || ptype == PLUGIN_SF2)
  1484. extraStuff = "true";
  1485. }
  1486. // TODO - proper find&load plugins
  1487. if (addPlugin(btype, ptype, stateSave.binary, stateSave.name, stateSave.label, stateSave.uniqueId, extraStuff, stateSave.options))
  1488. {
  1489. if (CarlaPlugin* const plugin = getPlugin(pData->curPluginCount-1))
  1490. {
  1491. #ifndef BUILD_BRIDGE
  1492. // deactivate bridge client-side ping check, since some plugins block during load
  1493. if ((plugin->getHints() & PLUGIN_IS_BRIDGE) != 0 && ! isPreset)
  1494. plugin->setCustomData(CUSTOM_DATA_TYPE_STRING, "__CarlaPingOnOff__", "false", false);
  1495. #endif
  1496. plugin->loadStateSave(stateSave);
  1497. }
  1498. }
  1499. else
  1500. carla_stderr2("Failed to load a plugin, error was:\n%s", getLastError());
  1501. }
  1502. if (isPreset)
  1503. return true;
  1504. }
  1505. #ifndef BUILD_BRIDGE
  1506. // tell bridges we're done loading
  1507. for (uint i=0; i < pData->curPluginCount; ++i)
  1508. {
  1509. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1510. if (plugin != nullptr && plugin->isEnabled() && (plugin->getHints() & PLUGIN_IS_BRIDGE) != 0)
  1511. plugin->setCustomData(CUSTOM_DATA_TYPE_STRING, "__CarlaPingOnOff__", "true", false);
  1512. }
  1513. callback(ENGINE_CALLBACK_IDLE, 0, 0, 0, 0.0f, nullptr);
  1514. // if we're running inside some session-manager, let them handle the connections
  1515. if (pData->options.processMode != ENGINE_PROCESS_MODE_PATCHBAY)
  1516. {
  1517. /**/ if (std::getenv("CARLA_DONT_MANAGE_CONNECTIONS") != nullptr)
  1518. return true;
  1519. else if (std::getenv("LADISH_APP_NAME") != nullptr)
  1520. return true;
  1521. else if (std::getenv("NSM_URL") != nullptr)
  1522. return true;
  1523. else if (std::strcmp(getCurrentDriverName(), "Plugin") == 0)
  1524. return true;
  1525. }
  1526. // now handle connections
  1527. for (XmlElement* elem = xmlElement->getFirstChildElement(); elem != nullptr; elem = elem->getNextElement())
  1528. {
  1529. const String& tagName(elem->getTagName());
  1530. if (! tagName.equalsIgnoreCase("patchbay"))
  1531. continue;
  1532. CarlaString sourcePort, targetPort;
  1533. for (XmlElement* patchElem = elem->getFirstChildElement(); patchElem != nullptr; patchElem = patchElem->getNextElement())
  1534. {
  1535. const String& patchTag(patchElem->getTagName());
  1536. sourcePort.clear();
  1537. targetPort.clear();
  1538. if (! patchTag.equalsIgnoreCase("connection"))
  1539. continue;
  1540. for (XmlElement* connElem = patchElem->getFirstChildElement(); connElem != nullptr; connElem = connElem->getNextElement())
  1541. {
  1542. const String& tag(connElem->getTagName());
  1543. const String text(connElem->getAllSubText().trim());
  1544. /**/ if (tag.equalsIgnoreCase("source"))
  1545. sourcePort = text.toRawUTF8();
  1546. else if (tag.equalsIgnoreCase("target"))
  1547. targetPort = text.toRawUTF8();
  1548. }
  1549. if (sourcePort.isNotEmpty() && targetPort.isNotEmpty())
  1550. restorePatchbayConnection(sourcePort, targetPort);
  1551. }
  1552. break;
  1553. }
  1554. #endif
  1555. return true;
  1556. }
  1557. // -----------------------------------------------------------------------
  1558. CARLA_BACKEND_END_NAMESPACE