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.

1904 lines
63KB

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