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.

1871 lines
61KB

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