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.

1908 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. /*
  488. for (uint i=id; i < pData->curPluginCount; ++i)
  489. {
  490. CarlaPlugin* const plugin2(pData->plugins[i].plugin);
  491. CARLA_SAFE_ASSERT_BREAK(plugin2 != nullptr);
  492. plugin2->updateOscURL();
  493. }
  494. */
  495. if (isOscControlRegistered())
  496. oscSend_control_remove_plugin(id);
  497. #else
  498. pData->curPluginCount = 0;
  499. carla_zeroStruct(pData->plugins, 1);
  500. #endif
  501. delete plugin;
  502. if (isRunning() && ! pData->aboutToClose)
  503. pData->thread.startThread();
  504. callback(ENGINE_CALLBACK_PLUGIN_REMOVED, id, 0, 0, 0.0f, nullptr);
  505. return true;
  506. }
  507. bool CarlaEngine::removeAllPlugins()
  508. {
  509. CARLA_SAFE_ASSERT_RETURN_ERR(pData->isIdling == 0, "An operation is still being processed, please wait for it to finish");
  510. CARLA_SAFE_ASSERT_RETURN_ERR(pData->plugins != nullptr, "Invalid engine internal data");
  511. CARLA_SAFE_ASSERT_RETURN_ERR(pData->nextPluginId == pData->maxPluginNumber, "Invalid engine internal data");
  512. CARLA_SAFE_ASSERT_RETURN_ERR(pData->nextAction.opcode == kEnginePostActionNull, "Invalid engine internal data");
  513. carla_debug("CarlaEngine::removeAllPlugins()");
  514. if (pData->curPluginCount == 0)
  515. return true;
  516. pData->thread.stopThread(500);
  517. #ifndef BUILD_BRIDGE
  518. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  519. pData->graph.removeAllPlugins(this);
  520. #endif
  521. const uint32_t curPluginCount(pData->curPluginCount);
  522. const bool lockWait(isRunning());
  523. const ScopedActionLock sal(pData, kEnginePostActionZeroCount, 0, 0, lockWait);
  524. callback(ENGINE_CALLBACK_IDLE, 0, 0, 0, 0.0f, nullptr);
  525. for (uint i=0; i < curPluginCount; ++i)
  526. {
  527. EnginePluginData& pluginData(pData->plugins[i]);
  528. if (pluginData.plugin != nullptr)
  529. {
  530. delete pluginData.plugin;
  531. pluginData.plugin = nullptr;
  532. }
  533. pluginData.insPeak[0] = 0.0f;
  534. pluginData.insPeak[1] = 0.0f;
  535. pluginData.outsPeak[0] = 0.0f;
  536. pluginData.outsPeak[1] = 0.0f;
  537. callback(ENGINE_CALLBACK_IDLE, 0, 0, 0, 0.0f, nullptr);
  538. }
  539. if (isRunning() && ! pData->aboutToClose)
  540. pData->thread.startThread();
  541. return true;
  542. }
  543. #ifndef BUILD_BRIDGE
  544. const char* CarlaEngine::renamePlugin(const uint id, const char* const newName)
  545. {
  546. CARLA_SAFE_ASSERT_RETURN_ERRN(pData->isIdling == 0, "An operation is still being processed, please wait for it to finish");
  547. CARLA_SAFE_ASSERT_RETURN_ERRN(pData->plugins != nullptr, "Invalid engine internal data");
  548. CARLA_SAFE_ASSERT_RETURN_ERRN(pData->curPluginCount != 0, "Invalid engine internal data");
  549. CARLA_SAFE_ASSERT_RETURN_ERRN(pData->nextAction.opcode == kEnginePostActionNull, "Invalid engine internal data");
  550. CARLA_SAFE_ASSERT_RETURN_ERRN(id < pData->curPluginCount, "Invalid plugin Id");
  551. CARLA_SAFE_ASSERT_RETURN_ERRN(newName != nullptr && newName[0] != '\0', "Invalid plugin name");
  552. carla_debug("CarlaEngine::renamePlugin(%i, \"%s\")", id, newName);
  553. CarlaPlugin* const plugin(pData->plugins[id].plugin);
  554. CARLA_SAFE_ASSERT_RETURN_ERRN(plugin != nullptr, "Could not find plugin to rename");
  555. CARLA_SAFE_ASSERT_RETURN_ERRN(plugin->getId() == id, "Invalid engine internal data");
  556. if (const char* const name = getUniquePluginName(newName))
  557. {
  558. plugin->setName(name);
  559. return name;
  560. }
  561. setLastError("Unable to get new unique plugin name");
  562. return nullptr;
  563. }
  564. bool CarlaEngine::clonePlugin(const uint id)
  565. {
  566. CARLA_SAFE_ASSERT_RETURN_ERR(pData->isIdling == 0, "An operation is still being processed, please wait for it to finish");
  567. CARLA_SAFE_ASSERT_RETURN_ERR(pData->plugins != nullptr, "Invalid engine internal data");
  568. CARLA_SAFE_ASSERT_RETURN_ERR(pData->curPluginCount != 0, "Invalid engine internal data");
  569. CARLA_SAFE_ASSERT_RETURN_ERR(pData->nextAction.opcode == kEnginePostActionNull, "Invalid engine internal data");
  570. CARLA_SAFE_ASSERT_RETURN_ERR(id < pData->curPluginCount, "Invalid plugin Id");
  571. carla_debug("CarlaEngine::clonePlugin(%i)", id);
  572. CarlaPlugin* const plugin(pData->plugins[id].plugin);
  573. CARLA_SAFE_ASSERT_RETURN_ERR(plugin != nullptr, "Could not find plugin to clone");
  574. CARLA_SAFE_ASSERT_RETURN_ERR(plugin->getId() == id, "Invalid engine internal data");
  575. char label[STR_MAX+1];
  576. carla_zeroChar(label, STR_MAX+1);
  577. plugin->getLabel(label);
  578. const uint pluginCountBefore(pData->curPluginCount);
  579. if (! addPlugin(plugin->getBinaryType(), plugin->getType(), plugin->getFilename(), plugin->getName(), label, plugin->getUniqueId(), plugin->getExtraStuff()))
  580. return false;
  581. CARLA_SAFE_ASSERT_RETURN_ERR(pluginCountBefore+1 == pData->curPluginCount, "No new plugin found");
  582. if (CarlaPlugin* const newPlugin = pData->plugins[pluginCountBefore].plugin)
  583. newPlugin->loadStateSave(plugin->getStateSave());
  584. return true;
  585. }
  586. bool CarlaEngine::replacePlugin(const uint id) noexcept
  587. {
  588. CARLA_SAFE_ASSERT_RETURN_ERR(pData->isIdling == 0, "An operation is still being processed, please wait for it to finish");
  589. CARLA_SAFE_ASSERT_RETURN_ERR(pData->plugins != nullptr, "Invalid engine internal data");
  590. CARLA_SAFE_ASSERT_RETURN_ERR(pData->curPluginCount != 0, "Invalid engine internal data");
  591. CARLA_SAFE_ASSERT_RETURN_ERR(pData->nextAction.opcode == kEnginePostActionNull, "Invalid engine internal data");
  592. carla_debug("CarlaEngine::replacePlugin(%i)", id);
  593. // might use this to reset
  594. if (id == pData->maxPluginNumber)
  595. {
  596. pData->nextPluginId = pData->maxPluginNumber;
  597. return true;
  598. }
  599. CARLA_SAFE_ASSERT_RETURN_ERR(id < pData->curPluginCount, "Invalid plugin Id");
  600. CarlaPlugin* const plugin(pData->plugins[id].plugin);
  601. CARLA_SAFE_ASSERT_RETURN_ERR(plugin != nullptr, "Could not find plugin to replace");
  602. CARLA_SAFE_ASSERT_RETURN_ERR(plugin->getId() == id, "Invalid engine internal data");
  603. pData->nextPluginId = id;
  604. return true;
  605. }
  606. bool CarlaEngine::switchPlugins(const uint idA, const uint idB) noexcept
  607. {
  608. CARLA_SAFE_ASSERT_RETURN_ERR(pData->isIdling == 0, "An operation is still being processed, please wait for it to finish");
  609. CARLA_SAFE_ASSERT_RETURN_ERR(pData->plugins != nullptr, "Invalid engine internal data");
  610. CARLA_SAFE_ASSERT_RETURN_ERR(pData->curPluginCount >= 2, "Invalid engine internal data");
  611. CARLA_SAFE_ASSERT_RETURN_ERR(pData->nextAction.opcode == kEnginePostActionNull, "Invalid engine internal data");
  612. CARLA_SAFE_ASSERT_RETURN_ERR(idA != idB, "Invalid operation, cannot switch plugin with itself");
  613. CARLA_SAFE_ASSERT_RETURN_ERR(idA < pData->curPluginCount, "Invalid plugin Id");
  614. CARLA_SAFE_ASSERT_RETURN_ERR(idB < pData->curPluginCount, "Invalid plugin Id");
  615. carla_debug("CarlaEngine::switchPlugins(%i)", idA, idB);
  616. {
  617. CarlaPlugin* const pluginA(pData->plugins[idA].plugin);
  618. CarlaPlugin* const pluginB(pData->plugins[idB].plugin);
  619. CARLA_SAFE_ASSERT_RETURN_ERR(pluginA != nullptr, "Could not find plugin to switch");
  620. CARLA_SAFE_ASSERT_RETURN_ERR(pluginA != nullptr, "Could not find plugin to switch");
  621. CARLA_SAFE_ASSERT_RETURN_ERR(pluginA->getId() == idA, "Invalid engine internal data");
  622. CARLA_SAFE_ASSERT_RETURN_ERR(pluginB->getId() == idB, "Invalid engine internal data");
  623. pData->thread.stopThread(500);
  624. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  625. pData->graph.replacePlugin(pluginA, pluginB);
  626. }
  627. const bool lockWait(isRunning() /*&& pData->options.processMode != ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS*/);
  628. const ScopedActionLock sal(pData, kEnginePostActionSwitchPlugins, idA, idB, lockWait);
  629. /*
  630. CarlaPlugin* const pluginA(pData->plugins[idA].plugin);
  631. CarlaPlugin* const pluginB(pData->plugins[idB].plugin);
  632. if (pluginA != nullptr && pluginB != nullptr)
  633. {
  634. pluginA->updateOscURL();
  635. pluginB->updateOscURL();
  636. }
  637. */
  638. // TODO
  639. //if (isOscControlRegistered())
  640. // oscSend_control_switch_plugins(idA, idB);
  641. if (isRunning() && ! pData->aboutToClose)
  642. pData->thread.startThread();
  643. return true;
  644. }
  645. #endif
  646. CarlaPlugin* CarlaEngine::getPlugin(const uint id) const noexcept
  647. {
  648. CARLA_SAFE_ASSERT_RETURN_ERRN(pData->plugins != nullptr, "Invalid engine internal data");
  649. CARLA_SAFE_ASSERT_RETURN_ERRN(pData->curPluginCount != 0, "Invalid engine internal data");
  650. CARLA_SAFE_ASSERT_RETURN_ERRN(pData->nextAction.opcode == kEnginePostActionNull, "Invalid engine internal data");
  651. CARLA_SAFE_ASSERT_RETURN_ERRN(id < pData->curPluginCount, "Invalid plugin Id");
  652. return pData->plugins[id].plugin;
  653. }
  654. CarlaPlugin* CarlaEngine::getPluginUnchecked(const uint id) const noexcept
  655. {
  656. return pData->plugins[id].plugin;
  657. }
  658. const char* CarlaEngine::getUniquePluginName(const char* const name) const
  659. {
  660. CARLA_SAFE_ASSERT_RETURN(pData->nextAction.opcode == kEnginePostActionNull, nullptr);
  661. CARLA_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0', nullptr);
  662. carla_debug("CarlaEngine::getUniquePluginName(\"%s\")", name);
  663. CarlaString sname;
  664. sname = name;
  665. if (sname.isEmpty())
  666. {
  667. sname = "(No name)";
  668. return sname.dup();
  669. }
  670. const size_t maxNameSize(carla_min<uint>(getMaxClientNameSize(), 0xff, 6) - 6); // 6 = strlen(" (10)") + 1
  671. if (maxNameSize == 0 || ! isRunning())
  672. return sname.dup();
  673. sname.truncate(maxNameSize);
  674. sname.replace(':', '.'); // ':' is used in JACK1 to split client/port names
  675. for (uint i=0; i < pData->curPluginCount; ++i)
  676. {
  677. CARLA_SAFE_ASSERT_BREAK(pData->plugins[i].plugin != nullptr);
  678. // Check if unique name doesn't exist
  679. if (const char* const pluginName = pData->plugins[i].plugin->getName())
  680. {
  681. if (sname != pluginName)
  682. continue;
  683. }
  684. // Check if string has already been modified
  685. {
  686. const size_t len(sname.length());
  687. // 1 digit, ex: " (2)"
  688. if (sname[len-4] == ' ' && sname[len-3] == '(' && sname.isDigit(len-2) && sname[len-1] == ')')
  689. {
  690. int number = sname[len-2] - '0';
  691. if (number == 9)
  692. {
  693. // next number is 10, 2 digits
  694. sname.truncate(len-4);
  695. sname += " (10)";
  696. //sname.replace(" (9)", " (10)");
  697. }
  698. else
  699. sname[len-2] = char('0' + number + 1);
  700. continue;
  701. }
  702. // 2 digits, ex: " (11)"
  703. if (sname[len-5] == ' ' && sname[len-4] == '(' && sname.isDigit(len-3) && sname.isDigit(len-2) && sname[len-1] == ')')
  704. {
  705. char n2 = sname[len-2];
  706. char n3 = sname[len-3];
  707. if (n2 == '9')
  708. {
  709. n2 = '0';
  710. n3 = static_cast<char>(n3 + 1);
  711. }
  712. else
  713. n2 = static_cast<char>(n2 + 1);
  714. sname[len-2] = n2;
  715. sname[len-3] = n3;
  716. continue;
  717. }
  718. }
  719. // Modify string if not
  720. sname += " (2)";
  721. }
  722. return sname.dup();
  723. }
  724. // -----------------------------------------------------------------------
  725. // Project management
  726. bool CarlaEngine::loadFile(const char* const filename)
  727. {
  728. CARLA_SAFE_ASSERT_RETURN_ERR(pData->isIdling == 0, "An operation is still being processed, please wait for it to finish");
  729. CARLA_SAFE_ASSERT_RETURN_ERR(filename != nullptr && filename[0] != '\0', "Invalid filename");
  730. carla_debug("CarlaEngine::loadFile(\"%s\")", filename);
  731. File file(filename);
  732. CARLA_SAFE_ASSERT_RETURN_ERR(file.existsAsFile(), "Requested file does not exist or is not a readable file");
  733. CarlaString baseName(file.getFileName().toRawUTF8());
  734. CarlaString extension(file.getFileExtension().replace(".","").toLowerCase().toRawUTF8());
  735. // -------------------------------------------------------------------
  736. if (extension == "carxp" || extension == "carxs")
  737. return loadProject(filename);
  738. // -------------------------------------------------------------------
  739. if (extension == "gig")
  740. return addPlugin(PLUGIN_GIG, filename, baseName, baseName, 0, nullptr);
  741. if (extension == "sf2")
  742. return addPlugin(PLUGIN_SF2, filename, baseName, baseName, 0, nullptr);
  743. if (extension == "sfz")
  744. return addPlugin(PLUGIN_SFZ, filename, baseName, baseName, 0, nullptr);
  745. // -------------------------------------------------------------------
  746. if (extension == "aif" || extension == "aiff" || extension == "bwf" || extension == "flac" || extension == "ogg" || extension == "wav")
  747. {
  748. if (addPlugin(PLUGIN_INTERNAL, nullptr, baseName, "audiofile", 0, nullptr))
  749. {
  750. if (CarlaPlugin* const plugin = getPlugin(pData->curPluginCount-1))
  751. plugin->setCustomData(CUSTOM_DATA_TYPE_STRING, "file", filename, true);
  752. return true;
  753. }
  754. return false;
  755. }
  756. // -------------------------------------------------------------------
  757. if (extension == "mid" || extension == "midi")
  758. {
  759. if (addPlugin(PLUGIN_INTERNAL, nullptr, baseName, "midifile", 0, nullptr))
  760. {
  761. if (CarlaPlugin* const plugin = getPlugin(pData->curPluginCount-1))
  762. plugin->setCustomData(CUSTOM_DATA_TYPE_STRING, "file", filename, true);
  763. return true;
  764. }
  765. return false;
  766. }
  767. // -------------------------------------------------------------------
  768. // ZynAddSubFX
  769. if (extension == "xmz" || extension == "xiz")
  770. {
  771. #ifdef WANT_ZYNADDSUBFX
  772. if (addPlugin(PLUGIN_INTERNAL, nullptr, baseName, "zynaddsubfx", 0, nullptr))
  773. {
  774. if (CarlaPlugin* const plugin = getPlugin(pData->curPluginCount-1))
  775. plugin->setCustomData(CUSTOM_DATA_TYPE_STRING, (extension == "xmz") ? "CarlaAlternateFile1" : "CarlaAlternateFile2", filename, true);
  776. return true;
  777. }
  778. return false;
  779. #else
  780. setLastError("This Carla build does not have ZynAddSubFX support");
  781. return false;
  782. #endif
  783. }
  784. // -------------------------------------------------------------------
  785. setLastError("Unknown file extension");
  786. return false;
  787. }
  788. bool CarlaEngine::loadProject(const char* const filename)
  789. {
  790. CARLA_SAFE_ASSERT_RETURN_ERR(pData->isIdling == 0, "An operation is still being processed, please wait for it to finish");
  791. CARLA_SAFE_ASSERT_RETURN_ERR(filename != nullptr && filename[0] != '\0', "Invalid filename");
  792. carla_debug("CarlaEngine::loadProject(\"%s\")", filename);
  793. File file(filename);
  794. CARLA_SAFE_ASSERT_RETURN_ERR(file.existsAsFile(), "Requested file does not exist or is not a readable file");
  795. XmlDocument xml(file);
  796. return loadProjectInternal(xml);
  797. }
  798. bool CarlaEngine::saveProject(const char* const filename)
  799. {
  800. CARLA_SAFE_ASSERT_RETURN_ERR(filename != nullptr && filename[0] != '\0', "Invalid filename");
  801. carla_debug("CarlaEngine::saveProject(\"%s\")", filename);
  802. MemoryOutputStream out;
  803. saveProjectInternal(out);
  804. File file(filename);
  805. if (file.replaceWithData(out.getData(), out.getDataSize()))
  806. return true;
  807. setLastError("Failed to write file");
  808. return false;
  809. }
  810. // -----------------------------------------------------------------------
  811. // Information (base)
  812. uint CarlaEngine::getHints() const noexcept
  813. {
  814. return pData->hints;
  815. }
  816. uint32_t CarlaEngine::getBufferSize() const noexcept
  817. {
  818. return pData->bufferSize;
  819. }
  820. double CarlaEngine::getSampleRate() const noexcept
  821. {
  822. return pData->sampleRate;
  823. }
  824. const char* CarlaEngine::getName() const noexcept
  825. {
  826. return pData->name;
  827. }
  828. EngineProcessMode CarlaEngine::getProccessMode() const noexcept
  829. {
  830. return pData->options.processMode;
  831. }
  832. const EngineOptions& CarlaEngine::getOptions() const noexcept
  833. {
  834. return pData->options;
  835. }
  836. const EngineTimeInfo& CarlaEngine::getTimeInfo() const noexcept
  837. {
  838. return pData->timeInfo;
  839. }
  840. // -----------------------------------------------------------------------
  841. // Information (peaks)
  842. float CarlaEngine::getInputPeak(const uint pluginId, const bool isLeft) const noexcept
  843. {
  844. CARLA_SAFE_ASSERT_RETURN(pluginId < pData->curPluginCount, 0.0f);
  845. return pData->plugins[pluginId].insPeak[isLeft ? 0 : 1];
  846. }
  847. float CarlaEngine::getOutputPeak(const uint pluginId, const bool isLeft) const noexcept
  848. {
  849. CARLA_SAFE_ASSERT_RETURN(pluginId < pData->curPluginCount, 0.0f);
  850. return pData->plugins[pluginId].outsPeak[isLeft ? 0 : 1];
  851. }
  852. // -----------------------------------------------------------------------
  853. // Callback
  854. void CarlaEngine::callback(const EngineCallbackOpcode action, const uint pluginId, const int value1, const int value2, const float value3, const char* const valueStr) noexcept
  855. {
  856. #ifdef DEBUG
  857. if (action != ENGINE_CALLBACK_IDLE)
  858. carla_debug("CarlaEngine::callback(%i:%s, %i, %i, %i, %f, \"%s\")", action, EngineCallbackOpcode2Str(action), pluginId, value1, value2, value3, valueStr);
  859. #endif
  860. #ifdef BUILD_BRIDGE
  861. if (pData->isIdling)
  862. #else
  863. if (pData->isIdling && action != ENGINE_CALLBACK_PATCHBAY_CLIENT_DATA_CHANGED)
  864. #endif
  865. {
  866. carla_stdout("callback while idling (%i:%s, %i, %i, %i, %f, \"%s\")", action, EngineCallbackOpcode2Str(action), pluginId, value1, value2, value3, valueStr);
  867. }
  868. if (action == ENGINE_CALLBACK_IDLE)
  869. {
  870. ++pData->isIdling;
  871. }
  872. #ifdef BUILD_BRIDGE
  873. else if (pData->oscData != nullptr)
  874. {
  875. switch (action)
  876. {
  877. case ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED:
  878. CARLA_SAFE_ASSERT_BREAK(value1 >= 0);
  879. oscSend_bridge_parameter_value(static_cast<uint>(value1), value3);
  880. break;
  881. case ENGINE_CALLBACK_PARAMETER_DEFAULT_CHANGED:
  882. CARLA_SAFE_ASSERT_BREAK(value1 >= 0);
  883. oscSend_bridge_default_value(static_cast<uint>(value1), value3);
  884. break;
  885. case ENGINE_CALLBACK_PROGRAM_CHANGED:
  886. CARLA_SAFE_ASSERT_BREAK(value1 >= -1);
  887. oscSend_bridge_current_program(value1);
  888. break;
  889. case ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED:
  890. CARLA_SAFE_ASSERT_BREAK(value1 >= -1);
  891. oscSend_bridge_current_midi_program(value1);
  892. break;
  893. case ENGINE_CALLBACK_UI_STATE_CHANGED:
  894. if (value1 != 1)
  895. oscSend_bridge_configure("CarlaBridgeHideGUI", "");
  896. break;
  897. default:
  898. break;
  899. }
  900. }
  901. #endif
  902. if (pData->callback != nullptr)
  903. {
  904. try {
  905. pData->callback(pData->callbackPtr, action, pluginId, value1, value2, value3, valueStr);
  906. } CARLA_SAFE_EXCEPTION("callback");
  907. }
  908. if (action == ENGINE_CALLBACK_IDLE)
  909. --pData->isIdling;
  910. }
  911. void CarlaEngine::setCallback(const EngineCallbackFunc func, void* const ptr) noexcept
  912. {
  913. carla_debug("CarlaEngine::setCallback(%p, %p)", func, ptr);
  914. pData->callback = func;
  915. pData->callbackPtr = ptr;
  916. }
  917. // -----------------------------------------------------------------------
  918. // File Callback
  919. const char* CarlaEngine::runFileCallback(const FileCallbackOpcode action, const bool isDir, const char* const title, const char* const filter) noexcept
  920. {
  921. CARLA_SAFE_ASSERT_RETURN(title != nullptr && title[0] != '\0', nullptr);
  922. CARLA_SAFE_ASSERT_RETURN(filter != nullptr, nullptr);
  923. carla_debug("CarlaEngine::runFileCallback(%i:%s, %s, \"%s\", \"%s\")", action, FileCallbackOpcode2Str(action), bool2str(isDir), title, filter);
  924. const char* ret = nullptr;
  925. if (pData->fileCallback != nullptr)
  926. {
  927. try {
  928. ret = pData->fileCallback(pData->fileCallbackPtr, action, isDir, title, filter);
  929. } CARLA_SAFE_EXCEPTION("runFileCallback");
  930. }
  931. return ret;
  932. }
  933. void CarlaEngine::setFileCallback(const FileCallbackFunc func, void* const ptr) noexcept
  934. {
  935. carla_debug("CarlaEngine::setFileCallback(%p, %p)", func, ptr);
  936. pData->fileCallback = func;
  937. pData->fileCallbackPtr = ptr;
  938. }
  939. // -----------------------------------------------------------------------
  940. // Transport
  941. void CarlaEngine::transportPlay() noexcept
  942. {
  943. pData->time.playing = true;
  944. }
  945. void CarlaEngine::transportPause() noexcept
  946. {
  947. pData->time.playing = false;
  948. }
  949. void CarlaEngine::transportRelocate(const uint64_t frame) noexcept
  950. {
  951. pData->time.frame = frame;
  952. }
  953. // -----------------------------------------------------------------------
  954. // Error handling
  955. const char* CarlaEngine::getLastError() const noexcept
  956. {
  957. return pData->lastError;
  958. }
  959. void CarlaEngine::setLastError(const char* const error) const noexcept
  960. {
  961. pData->lastError = error;
  962. }
  963. void CarlaEngine::setAboutToClose() noexcept
  964. {
  965. carla_debug("CarlaEngine::setAboutToClose()");
  966. pData->aboutToClose = true;
  967. }
  968. // -----------------------------------------------------------------------
  969. // Global options
  970. void CarlaEngine::setOption(const EngineOption option, const int value, const char* const valueStr) noexcept
  971. {
  972. carla_debug("CarlaEngine::setOption(%i:%s, %i, \"%s\")", option, EngineOption2Str(option), value, valueStr);
  973. if (isRunning() && (option == ENGINE_OPTION_PROCESS_MODE || option == ENGINE_OPTION_AUDIO_NUM_PERIODS || option == ENGINE_OPTION_AUDIO_DEVICE))
  974. return carla_stderr("CarlaEngine::setOption(%i:%s, %i, \"%s\") - Cannot set this option while engine is running!", option, EngineOption2Str(option), value, valueStr);
  975. if (option == ENGINE_OPTION_FORCE_STEREO && pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  976. {
  977. // do not un-force stereo for rack mode
  978. CARLA_SAFE_ASSERT_RETURN(value == 1,);
  979. }
  980. switch (option)
  981. {
  982. case ENGINE_OPTION_DEBUG:
  983. case ENGINE_OPTION_NSM_INIT:
  984. break;
  985. case ENGINE_OPTION_PROCESS_MODE:
  986. CARLA_SAFE_ASSERT_RETURN(value >= ENGINE_PROCESS_MODE_SINGLE_CLIENT && value <= ENGINE_PROCESS_MODE_BRIDGE,);
  987. pData->options.processMode = static_cast<EngineProcessMode>(value);
  988. break;
  989. case ENGINE_OPTION_TRANSPORT_MODE:
  990. CARLA_SAFE_ASSERT_RETURN(value >= ENGINE_TRANSPORT_MODE_INTERNAL && value <= ENGINE_TRANSPORT_MODE_BRIDGE,);
  991. pData->options.transportMode = static_cast<EngineTransportMode>(value);
  992. break;
  993. case ENGINE_OPTION_FORCE_STEREO:
  994. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  995. pData->options.forceStereo = (value != 0);
  996. break;
  997. case ENGINE_OPTION_PREFER_PLUGIN_BRIDGES:
  998. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  999. pData->options.preferPluginBridges = (value != 0);
  1000. break;
  1001. case ENGINE_OPTION_PREFER_UI_BRIDGES:
  1002. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  1003. pData->options.preferUiBridges = (value != 0);
  1004. break;
  1005. case ENGINE_OPTION_UIS_ALWAYS_ON_TOP:
  1006. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  1007. pData->options.uisAlwaysOnTop = (value != 0);
  1008. break;
  1009. case ENGINE_OPTION_MAX_PARAMETERS:
  1010. CARLA_SAFE_ASSERT_RETURN(value >= 0,);
  1011. pData->options.maxParameters = static_cast<uint>(value);
  1012. break;
  1013. case ENGINE_OPTION_UI_BRIDGES_TIMEOUT:
  1014. CARLA_SAFE_ASSERT_RETURN(value >= 0,);
  1015. pData->options.uiBridgesTimeout = static_cast<uint>(value);
  1016. break;
  1017. case ENGINE_OPTION_AUDIO_NUM_PERIODS:
  1018. CARLA_SAFE_ASSERT_RETURN(value >= 2 && value <= 3,);
  1019. pData->options.audioNumPeriods = static_cast<uint>(value);
  1020. break;
  1021. case ENGINE_OPTION_AUDIO_BUFFER_SIZE:
  1022. CARLA_SAFE_ASSERT_RETURN(value >= 8,);
  1023. pData->options.audioBufferSize = static_cast<uint>(value);
  1024. break;
  1025. case ENGINE_OPTION_AUDIO_SAMPLE_RATE:
  1026. CARLA_SAFE_ASSERT_RETURN(value >= 22050,);
  1027. pData->options.audioSampleRate = static_cast<uint>(value);
  1028. break;
  1029. case ENGINE_OPTION_AUDIO_DEVICE:
  1030. CARLA_SAFE_ASSERT_RETURN(valueStr != nullptr,);
  1031. if (pData->options.audioDevice != nullptr)
  1032. delete[] pData->options.audioDevice;
  1033. pData->options.audioDevice = carla_strdup_safe(valueStr);
  1034. break;
  1035. case ENGINE_OPTION_PLUGIN_PATH:
  1036. CARLA_SAFE_ASSERT_RETURN(value > PLUGIN_NONE,);
  1037. CARLA_SAFE_ASSERT_RETURN(value <= PLUGIN_SFZ,);
  1038. switch (value)
  1039. {
  1040. case PLUGIN_LADSPA:
  1041. if (pData->options.pathLADSPA != nullptr)
  1042. delete[] pData->options.pathLADSPA;
  1043. if (valueStr != nullptr)
  1044. pData->options.pathLADSPA = carla_strdup_safe(valueStr);
  1045. else
  1046. pData->options.pathLADSPA = nullptr;
  1047. break;
  1048. case PLUGIN_DSSI:
  1049. if (pData->options.pathDSSI != nullptr)
  1050. delete[] pData->options.pathDSSI;
  1051. if (valueStr != nullptr)
  1052. pData->options.pathDSSI = carla_strdup_safe(valueStr);
  1053. else
  1054. pData->options.pathDSSI = nullptr;
  1055. break;
  1056. case PLUGIN_LV2:
  1057. if (pData->options.pathLV2 != nullptr)
  1058. delete[] pData->options.pathLV2;
  1059. if (valueStr != nullptr)
  1060. pData->options.pathLV2 = carla_strdup_safe(valueStr);
  1061. else
  1062. pData->options.pathLV2 = nullptr;
  1063. break;
  1064. case PLUGIN_VST2:
  1065. if (pData->options.pathVST2 != nullptr)
  1066. delete[] pData->options.pathVST2;
  1067. if (valueStr != nullptr)
  1068. pData->options.pathVST2 = carla_strdup_safe(valueStr);
  1069. else
  1070. pData->options.pathVST2 = nullptr;
  1071. break;
  1072. case PLUGIN_VST3:
  1073. if (pData->options.pathVST3 != nullptr)
  1074. delete[] pData->options.pathVST3;
  1075. if (valueStr != nullptr)
  1076. pData->options.pathVST3 = carla_strdup_safe(valueStr);
  1077. else
  1078. pData->options.pathVST3 = nullptr;
  1079. break;
  1080. case PLUGIN_AU:
  1081. if (pData->options.pathAU != nullptr)
  1082. delete[] pData->options.pathAU;
  1083. if (valueStr != nullptr)
  1084. pData->options.pathAU = carla_strdup_safe(valueStr);
  1085. else
  1086. pData->options.pathAU = nullptr;
  1087. break;
  1088. case PLUGIN_GIG:
  1089. if (pData->options.pathGIG != nullptr)
  1090. delete[] pData->options.pathGIG;
  1091. if (valueStr != nullptr)
  1092. pData->options.pathGIG = carla_strdup_safe(valueStr);
  1093. else
  1094. pData->options.pathGIG = nullptr;
  1095. break;
  1096. case PLUGIN_SF2:
  1097. if (pData->options.pathSF2 != nullptr)
  1098. delete[] pData->options.pathSF2;
  1099. if (valueStr != nullptr)
  1100. pData->options.pathSF2 = carla_strdup_safe(valueStr);
  1101. else
  1102. pData->options.pathSF2 = nullptr;
  1103. break;
  1104. case PLUGIN_SFZ:
  1105. if (pData->options.pathSFZ != nullptr)
  1106. delete[] pData->options.pathSFZ;
  1107. if (valueStr != nullptr)
  1108. pData->options.pathSFZ = carla_strdup_safe(valueStr);
  1109. else
  1110. pData->options.pathSFZ = nullptr;
  1111. break;
  1112. }
  1113. break;
  1114. case ENGINE_OPTION_PATH_BINARIES:
  1115. CARLA_SAFE_ASSERT_RETURN(valueStr != nullptr && valueStr[0] != '\0',);
  1116. if (pData->options.binaryDir != nullptr)
  1117. delete[] pData->options.binaryDir;
  1118. pData->options.binaryDir = carla_strdup_safe(valueStr);
  1119. break;
  1120. case ENGINE_OPTION_PATH_RESOURCES:
  1121. CARLA_SAFE_ASSERT_RETURN(valueStr != nullptr && valueStr[0] != '\0',);
  1122. if (pData->options.resourceDir != nullptr)
  1123. delete[] pData->options.resourceDir;
  1124. pData->options.resourceDir = carla_strdup_safe(valueStr);
  1125. break;
  1126. case ENGINE_OPTION_PREVENT_BAD_BEHAVIOUR:
  1127. CARLA_SAFE_ASSERT_RETURN(pData->options.binaryDir != nullptr && pData->options.binaryDir[0] != '\0',);
  1128. #ifdef CARLA_OS_LINUX
  1129. if (value != 0)
  1130. {
  1131. CarlaString interposerPath(CarlaString(pData->options.binaryDir) + CARLA_OS_SEP_STR "libcarla_interposer.so");
  1132. ::setenv("LD_PRELOAD", interposerPath.buffer(), 1);
  1133. }
  1134. else
  1135. {
  1136. ::unsetenv("LD_PRELOAD");
  1137. }
  1138. #endif
  1139. break;
  1140. case ENGINE_OPTION_FRONTEND_WIN_ID:
  1141. CARLA_SAFE_ASSERT_RETURN(valueStr != nullptr && valueStr[0] != '\0',);
  1142. const long long winId(std::strtoll(valueStr, nullptr, 16));
  1143. CARLA_SAFE_ASSERT_RETURN(winId >= 0,);
  1144. pData->options.frontendWinId = static_cast<uintptr_t>(winId);
  1145. break;
  1146. }
  1147. }
  1148. // -----------------------------------------------------------------------
  1149. // OSC Stuff
  1150. #ifdef BUILD_BRIDGE
  1151. bool CarlaEngine::isOscBridgeRegistered() const noexcept
  1152. {
  1153. return (pData->oscData != nullptr);
  1154. }
  1155. #else
  1156. bool CarlaEngine::isOscControlRegistered() const noexcept
  1157. {
  1158. return pData->osc.isControlRegistered();
  1159. }
  1160. #endif
  1161. void CarlaEngine::idleOsc() const noexcept
  1162. {
  1163. pData->osc.idle();
  1164. }
  1165. const char* CarlaEngine::getOscServerPathTCP() const noexcept
  1166. {
  1167. return pData->osc.getServerPathTCP();
  1168. }
  1169. const char* CarlaEngine::getOscServerPathUDP() const noexcept
  1170. {
  1171. return pData->osc.getServerPathUDP();
  1172. }
  1173. #ifdef BUILD_BRIDGE
  1174. void CarlaEngine::setOscBridgeData(CarlaOscData* const oscData) const noexcept
  1175. {
  1176. pData->oscData = oscData;
  1177. }
  1178. #endif
  1179. // -----------------------------------------------------------------------
  1180. // Helper functions
  1181. EngineEvent* CarlaEngine::getInternalEventBuffer(const bool isInput) const noexcept
  1182. {
  1183. return isInput ? pData->events.in : pData->events.out;
  1184. }
  1185. void CarlaEngine::registerEnginePlugin(const uint id, CarlaPlugin* const plugin) noexcept
  1186. {
  1187. CARLA_SAFE_ASSERT_RETURN(id == pData->curPluginCount,);
  1188. carla_debug("CarlaEngine::registerEnginePlugin(%i, %p)", id, plugin);
  1189. pData->plugins[id].plugin = plugin;
  1190. }
  1191. // -----------------------------------------------------------------------
  1192. // Internal stuff
  1193. void CarlaEngine::bufferSizeChanged(const uint32_t newBufferSize)
  1194. {
  1195. carla_debug("CarlaEngine::bufferSizeChanged(%i)", newBufferSize);
  1196. #ifndef BUILD_BRIDGE
  1197. pData->graph.setBufferSize(newBufferSize);
  1198. #endif
  1199. for (uint i=0; i < pData->curPluginCount; ++i)
  1200. {
  1201. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1202. if (plugin != nullptr && plugin->isEnabled())
  1203. plugin->bufferSizeChanged(newBufferSize);
  1204. }
  1205. callback(ENGINE_CALLBACK_BUFFER_SIZE_CHANGED, 0, static_cast<int>(newBufferSize), 0, 0.0f, nullptr);
  1206. }
  1207. void CarlaEngine::sampleRateChanged(const double newSampleRate)
  1208. {
  1209. carla_debug("CarlaEngine::sampleRateChanged(%g)", newSampleRate);
  1210. #ifndef BUILD_BRIDGE
  1211. pData->graph.setSampleRate(newSampleRate);
  1212. #endif
  1213. for (uint i=0; i < pData->curPluginCount; ++i)
  1214. {
  1215. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1216. if (plugin != nullptr && plugin->isEnabled())
  1217. plugin->sampleRateChanged(newSampleRate);
  1218. }
  1219. callback(ENGINE_CALLBACK_SAMPLE_RATE_CHANGED, 0, 0, 0, static_cast<float>(newSampleRate), nullptr);
  1220. }
  1221. void CarlaEngine::offlineModeChanged(const bool isOfflineNow)
  1222. {
  1223. carla_debug("CarlaEngine::offlineModeChanged(%s)", bool2str(isOfflineNow));
  1224. #ifndef BUILD_BRIDGE
  1225. pData->graph.setOffline(isOfflineNow);
  1226. #endif
  1227. for (uint i=0; i < pData->curPluginCount; ++i)
  1228. {
  1229. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1230. if (plugin != nullptr && plugin->isEnabled())
  1231. plugin->offlineModeChanged(isOfflineNow);
  1232. }
  1233. }
  1234. void CarlaEngine::runPendingRtEvents() noexcept
  1235. {
  1236. pData->doNextPluginAction(true);
  1237. if (pData->time.playing)
  1238. pData->time.frame += pData->bufferSize;
  1239. if (pData->options.transportMode == ENGINE_TRANSPORT_MODE_INTERNAL)
  1240. {
  1241. pData->timeInfo.playing = pData->time.playing;
  1242. pData->timeInfo.frame = pData->time.frame;
  1243. }
  1244. }
  1245. void CarlaEngine::setPluginPeaks(const uint pluginId, float const inPeaks[2], float const outPeaks[2]) noexcept
  1246. {
  1247. EnginePluginData& pluginData(pData->plugins[pluginId]);
  1248. pluginData.insPeak[0] = inPeaks[0];
  1249. pluginData.insPeak[1] = inPeaks[1];
  1250. pluginData.outsPeak[0] = outPeaks[0];
  1251. pluginData.outsPeak[1] = outPeaks[1];
  1252. }
  1253. void CarlaEngine::saveProjectInternal(juce::MemoryOutputStream& outStream) const
  1254. {
  1255. outStream << "<?xml version='1.0' encoding='UTF-8'?>\n";
  1256. outStream << "<!DOCTYPE CARLA-PROJECT>\n";
  1257. outStream << "<CARLA-PROJECT VERSION='2.0'>\n";
  1258. const bool isPlugin(std::strcmp(getCurrentDriverName(), "Plugin") == 0);
  1259. const EngineOptions& options(pData->options);
  1260. MemoryOutputStream outSettings(1024);
  1261. // save appropriate engine settings
  1262. outSettings << " <EngineSettings>\n";
  1263. //processMode
  1264. //transportMode
  1265. outSettings << " <ForceStereo>" << bool2str(options.forceStereo) << "</ForceStereo>\n";
  1266. outSettings << " <PreferPluginBridges>" << bool2str(options.preferPluginBridges) << "</PreferPluginBridges>\n";
  1267. outSettings << " <PreferUiBridges>" << bool2str(options.preferUiBridges) << "</PreferUiBridges>\n";
  1268. outSettings << " <UIsAlwaysOnTop>" << bool2str(options.uisAlwaysOnTop) << "</UIsAlwaysOnTop>\n";
  1269. outSettings << " <MaxParameters>" << String(options.maxParameters) << "</MaxParameters>\n";
  1270. outSettings << " <UIBridgesTimeout>" << String(options.uiBridgesTimeout) << "</UIBridgesTimeout>\n";
  1271. if (isPlugin)
  1272. {
  1273. outSettings << " <LADSPA_PATH>" << xmlSafeString(options.pathLADSPA, true) << "</LADSPA_PATH>\n";
  1274. outSettings << " <DSSI_PATH>" << xmlSafeString(options.pathDSSI, true) << "</DSSI_PATH>\n";
  1275. outSettings << " <LV2_PATH>" << xmlSafeString(options.pathLV2, true) << "</LV2_PATH>\n";
  1276. outSettings << " <VST2_PATH>" << xmlSafeString(options.pathVST2, true) << "</VST2_PATH>\n";
  1277. outSettings << " <VST3_PATH>" << xmlSafeString(options.pathVST3, true) << "</VST3_PATH>\n";
  1278. outSettings << " <AU_PATH>" << xmlSafeString(options.pathAU, true) << "</AU_PATH>\n";
  1279. outSettings << " <GIG_PATH>" << xmlSafeString(options.pathGIG, true) << "</GIG_PATH>\n";
  1280. outSettings << " <SF2_PATH>" << xmlSafeString(options.pathSF2, true) << "</SF2_PATH>\n";
  1281. outSettings << " <SFZ_PATH>" << xmlSafeString(options.pathSFZ, true) << "</SFZ_PATH>\n";
  1282. }
  1283. outSettings << " </EngineSettings>\n";
  1284. outStream << outSettings;
  1285. char strBuf[STR_MAX+1];
  1286. for (uint i=0; i < pData->curPluginCount; ++i)
  1287. {
  1288. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1289. if (plugin != nullptr && plugin->isEnabled())
  1290. {
  1291. MemoryOutputStream outPlugin(4096);
  1292. outPlugin << "\n";
  1293. strBuf[0] = '\0';
  1294. plugin->getRealName(strBuf);
  1295. if (strBuf[0] != '\0')
  1296. outPlugin << " <!-- " << xmlSafeString(strBuf, true) << " -->\n";
  1297. outPlugin << " <Plugin>\n";
  1298. outPlugin << plugin->getStateSave().toString();
  1299. outPlugin << " </Plugin>\n";
  1300. outStream << outPlugin;
  1301. }
  1302. }
  1303. #ifndef BUILD_BRIDGE
  1304. bool saveConnections = true;
  1305. // if we're running inside some session-manager, let them handle the connections
  1306. if (pData->options.processMode != ENGINE_PROCESS_MODE_PATCHBAY)
  1307. {
  1308. /**/ if (std::getenv("CARLA_DONT_MANAGE_CONNECTIONS") != nullptr)
  1309. saveConnections = false;
  1310. else if (std::getenv("LADISH_APP_NAME") != nullptr)
  1311. saveConnections = false;
  1312. else if (std::getenv("NSM_URL") != nullptr)
  1313. saveConnections = false;
  1314. else if (std::strcmp(getCurrentDriverName(), "Plugin") == 0)
  1315. saveConnections = false;
  1316. }
  1317. if (saveConnections)
  1318. {
  1319. if (const char* const* const patchbayConns = getPatchbayConnections())
  1320. {
  1321. MemoryOutputStream outPatchbay(2048);
  1322. outPatchbay << "\n <Patchbay>\n";
  1323. for (int i=0; patchbayConns[i] != nullptr && patchbayConns[i+1] != nullptr; ++i, ++i )
  1324. {
  1325. const char* const connSource(patchbayConns[i]);
  1326. const char* const connTarget(patchbayConns[i+1]);
  1327. CARLA_SAFE_ASSERT_CONTINUE(connSource != nullptr && connSource[0] != '\0');
  1328. CARLA_SAFE_ASSERT_CONTINUE(connTarget != nullptr && connTarget[0] != '\0');
  1329. outPatchbay << " <Connection>\n";
  1330. outPatchbay << " <Source>" << connSource << "</Source>\n";
  1331. outPatchbay << " <Target>" << connTarget << "</Target>\n";
  1332. outPatchbay << " </Connection>\n";
  1333. }
  1334. outPatchbay << " </Patchbay>\n";
  1335. outStream << outPatchbay;
  1336. }
  1337. }
  1338. #endif
  1339. outStream << "</CARLA-PROJECT>\n";
  1340. }
  1341. bool CarlaEngine::loadProjectInternal(juce::XmlDocument& xmlDoc)
  1342. {
  1343. ScopedPointer<XmlElement> xmlElement(xmlDoc.getDocumentElement(true));
  1344. CARLA_SAFE_ASSERT_RETURN_ERR(xmlElement != nullptr, "Failed to parse project file");
  1345. const String& xmlType(xmlElement->getTagName());
  1346. const bool isPreset(xmlType.equalsIgnoreCase("carla-preset"));
  1347. if (! (xmlType.equalsIgnoreCase("carla-project") || isPreset))
  1348. {
  1349. setLastError("Not a valid Carla project or preset file");
  1350. return false;
  1351. }
  1352. // completely load file
  1353. xmlElement = xmlDoc.getDocumentElement(false);
  1354. CARLA_SAFE_ASSERT_RETURN_ERR(xmlElement != nullptr, "Failed to completely parse project file");
  1355. const bool isPlugin(std::strcmp(getCurrentDriverName(), "Plugin") == 0);
  1356. // engine settings
  1357. for (XmlElement* elem = xmlElement->getFirstChildElement(); elem != nullptr; elem = elem->getNextElement())
  1358. {
  1359. const String& tagName(elem->getTagName());
  1360. if (! tagName.equalsIgnoreCase("enginesettings"))
  1361. continue;
  1362. for (XmlElement* settElem = elem->getFirstChildElement(); settElem != nullptr; settElem = settElem->getNextElement())
  1363. {
  1364. const String& tag(settElem->getTagName());
  1365. const String text(settElem->getAllSubText().trim());
  1366. /** some settings might be incorrect or require extra work,
  1367. so we call setOption rather than modifying them direly */
  1368. int option = -1;
  1369. int value = 0;
  1370. const char* valueStr = nullptr;
  1371. /**/ if (tag.equalsIgnoreCase("forcestereo"))
  1372. {
  1373. option = ENGINE_OPTION_FORCE_STEREO;
  1374. value = text.equalsIgnoreCase("true") ? 1 : 0;
  1375. }
  1376. else if (tag.equalsIgnoreCase("preferpluginbridges"))
  1377. {
  1378. option = ENGINE_OPTION_PREFER_PLUGIN_BRIDGES;
  1379. value = text.equalsIgnoreCase("true") ? 1 : 0;
  1380. }
  1381. else if (tag.equalsIgnoreCase("preferuibridges"))
  1382. {
  1383. option = ENGINE_OPTION_PREFER_UI_BRIDGES;
  1384. value = text.equalsIgnoreCase("true") ? 1 : 0;
  1385. }
  1386. else if (tag.equalsIgnoreCase("uisalwaysontop"))
  1387. {
  1388. option = ENGINE_OPTION_UIS_ALWAYS_ON_TOP;
  1389. value = text.equalsIgnoreCase("true") ? 1 : 0;
  1390. }
  1391. else if (tag.equalsIgnoreCase("maxparameters"))
  1392. {
  1393. option = ENGINE_OPTION_MAX_PARAMETERS;
  1394. value = text.getIntValue();
  1395. }
  1396. else if (tag.equalsIgnoreCase("uibridgestimeout"))
  1397. {
  1398. option = ENGINE_OPTION_UI_BRIDGES_TIMEOUT;
  1399. value = text.getIntValue();
  1400. }
  1401. else if (isPlugin)
  1402. {
  1403. /**/ if (tag.equalsIgnoreCase("LADSPA_PATH"))
  1404. {
  1405. option = ENGINE_OPTION_PLUGIN_PATH;
  1406. value = PLUGIN_LADSPA;
  1407. valueStr = text.toRawUTF8();
  1408. }
  1409. else if (tag.equalsIgnoreCase("DSSI_PATH"))
  1410. {
  1411. option = ENGINE_OPTION_PLUGIN_PATH;
  1412. value = PLUGIN_DSSI;
  1413. valueStr = text.toRawUTF8();
  1414. }
  1415. else if (tag.equalsIgnoreCase("LV2_PATH"))
  1416. {
  1417. option = ENGINE_OPTION_PLUGIN_PATH;
  1418. value = PLUGIN_LV2;
  1419. valueStr = text.toRawUTF8();
  1420. }
  1421. else if (tag.equalsIgnoreCase("VST2_PATH"))
  1422. {
  1423. option = ENGINE_OPTION_PLUGIN_PATH;
  1424. value = PLUGIN_VST2;
  1425. valueStr = text.toRawUTF8();
  1426. }
  1427. else if (tag.equalsIgnoreCase("VST3_PATH"))
  1428. {
  1429. option = ENGINE_OPTION_PLUGIN_PATH;
  1430. value = PLUGIN_VST3;
  1431. valueStr = text.toRawUTF8();
  1432. }
  1433. else if (tag.equalsIgnoreCase("AU_PATH"))
  1434. {
  1435. option = ENGINE_OPTION_PLUGIN_PATH;
  1436. value = PLUGIN_AU;
  1437. valueStr = text.toRawUTF8();
  1438. }
  1439. else if (tag.equalsIgnoreCase("GIG_PATH"))
  1440. {
  1441. option = ENGINE_OPTION_PLUGIN_PATH;
  1442. value = PLUGIN_GIG;
  1443. valueStr = text.toRawUTF8();
  1444. }
  1445. else if (tag.equalsIgnoreCase("SF2_PATH"))
  1446. {
  1447. option = ENGINE_OPTION_PLUGIN_PATH;
  1448. value = PLUGIN_SF2;
  1449. valueStr = text.toRawUTF8();
  1450. }
  1451. else if (tag.equalsIgnoreCase("SFZ_PATH"))
  1452. {
  1453. option = ENGINE_OPTION_PLUGIN_PATH;
  1454. value = PLUGIN_SFZ;
  1455. valueStr = text.toRawUTF8();
  1456. }
  1457. }
  1458. CARLA_SAFE_ASSERT_CONTINUE(option != -1);
  1459. setOption(static_cast<EngineOption>(option), value, valueStr);
  1460. }
  1461. break;
  1462. }
  1463. // handle plugins first
  1464. for (XmlElement* elem = xmlElement->getFirstChildElement(); elem != nullptr; elem = elem->getNextElement())
  1465. {
  1466. const String& tagName(elem->getTagName());
  1467. if (isPreset || tagName.equalsIgnoreCase("plugin"))
  1468. {
  1469. CarlaStateSave stateSave;
  1470. stateSave.fillFromXmlElement(isPreset ? xmlElement.get() : elem);
  1471. callback(ENGINE_CALLBACK_IDLE, 0, 0, 0, 0.0f, nullptr);
  1472. CARLA_SAFE_ASSERT_CONTINUE(stateSave.type != nullptr);
  1473. const void* extraStuff = nullptr;
  1474. // check if using GIG or SF2 16outs
  1475. static const char kUse16OutsSuffix[] = " (16 outs)";
  1476. const BinaryType btype(getBinaryTypeFromFile(stateSave.binary));
  1477. const PluginType ptype(getPluginTypeFromString(stateSave.type));
  1478. if (CarlaString(stateSave.label).endsWith(kUse16OutsSuffix))
  1479. {
  1480. if (ptype == PLUGIN_GIG || ptype == PLUGIN_SF2)
  1481. extraStuff = "true";
  1482. }
  1483. // TODO - proper find&load plugins
  1484. if (addPlugin(btype, ptype, stateSave.binary, stateSave.name, stateSave.label, stateSave.uniqueId, extraStuff))
  1485. {
  1486. if (CarlaPlugin* const plugin = getPlugin(pData->curPluginCount-1))
  1487. plugin->loadStateSave(stateSave);
  1488. }
  1489. else
  1490. carla_stderr2("Failed to load a plugin, error was:\n%s", getLastError());
  1491. }
  1492. if (isPreset)
  1493. return true;
  1494. }
  1495. #ifndef BUILD_BRIDGE
  1496. callback(ENGINE_CALLBACK_IDLE, 0, 0, 0, 0.0f, nullptr);
  1497. // if we're running inside some session-manager, let them handle the connections
  1498. if (pData->options.processMode != ENGINE_PROCESS_MODE_PATCHBAY)
  1499. {
  1500. /**/ if (std::getenv("CARLA_DONT_MANAGE_CONNECTIONS") != nullptr)
  1501. return true;
  1502. else if (std::getenv("LADISH_APP_NAME") != nullptr)
  1503. return true;
  1504. else if (std::getenv("NSM_URL") != nullptr)
  1505. return true;
  1506. else if (std::strcmp(getCurrentDriverName(), "Plugin") == 0)
  1507. return true;
  1508. }
  1509. // now handle connections
  1510. for (XmlElement* elem = xmlElement->getFirstChildElement(); elem != nullptr; elem = elem->getNextElement())
  1511. {
  1512. const String& tagName(elem->getTagName());
  1513. if (! tagName.equalsIgnoreCase("patchbay"))
  1514. continue;
  1515. CarlaString sourcePort, targetPort;
  1516. for (XmlElement* patchElem = elem->getFirstChildElement(); patchElem != nullptr; patchElem = patchElem->getNextElement())
  1517. {
  1518. const String& patchTag(patchElem->getTagName());
  1519. sourcePort.clear();
  1520. targetPort.clear();
  1521. if (! patchTag.equalsIgnoreCase("connection"))
  1522. continue;
  1523. for (XmlElement* connElem = patchElem->getFirstChildElement(); connElem != nullptr; connElem = connElem->getNextElement())
  1524. {
  1525. const String& tag(connElem->getTagName());
  1526. const String text(connElem->getAllSubText().trim());
  1527. /**/ if (tag.equalsIgnoreCase("source"))
  1528. sourcePort = text.toRawUTF8();
  1529. else if (tag.equalsIgnoreCase("target"))
  1530. targetPort = text.toRawUTF8();
  1531. }
  1532. if (sourcePort.isNotEmpty() && targetPort.isNotEmpty())
  1533. restorePatchbayConnection(sourcePort, targetPort);
  1534. }
  1535. break;
  1536. }
  1537. #endif
  1538. return true;
  1539. }
  1540. // -----------------------------------------------------------------------
  1541. CARLA_BACKEND_END_NAMESPACE