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.

1926 lines
64KB

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