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.

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