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.

2048 lines
68KB

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