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.

1881 lines
62KB

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