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.

2122 lines
73KB

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