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.

2200 lines
70KB

  1. /*
  2. * Carla Standalone
  3. * Copyright (C) 2011-2013 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 GPL.txt file
  16. */
  17. #include "CarlaStandalone.hpp"
  18. #include "CarlaBackendUtils.hpp"
  19. #include "CarlaOscUtils.hpp"
  20. #include "CarlaEngine.hpp"
  21. #include "CarlaPlugin.hpp"
  22. #include "CarlaMIDI.h"
  23. #include "CarlaNative.h"
  24. #include "CarlaStyle.hpp"
  25. #include <QtCore/QSettings>
  26. #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
  27. # include <QtWidgets/QApplication>
  28. #else
  29. # include <QtGui/QApplication>
  30. #endif
  31. using CarlaBackend::CarlaEngine;
  32. using CarlaBackend::CarlaPlugin;
  33. using CarlaBackend::CallbackFunc;
  34. using CarlaBackend::EngineOptions;
  35. using CarlaBackend::EngineTimeInfo;
  36. // -------------------------------------------------------------------------------------------------------------------
  37. // Single, standalone engine
  38. struct CarlaBackendStandalone {
  39. CallbackFunc callback;
  40. void* callbackPtr;
  41. CarlaEngine* engine;
  42. CarlaString lastError;
  43. CarlaString procName;
  44. #ifndef BUILD_BRIDGE
  45. EngineOptions options;
  46. #endif
  47. QApplication* app;
  48. bool needsInit;
  49. CarlaBackendStandalone()
  50. : callback(nullptr),
  51. callbackPtr(nullptr),
  52. engine(nullptr),
  53. app(qApp),
  54. needsInit(app == nullptr)
  55. {
  56. if (app != nullptr)
  57. {
  58. QSettings settings;
  59. if (settings.value("Main/UseProTheme", true).toBool())
  60. {
  61. CarlaStyle* const style(new CarlaStyle());
  62. app->setStyle(style);
  63. style->ready(app);
  64. QString color(settings.value("Main/ProThemeColor", "Black").toString());
  65. if (color == "Blue")
  66. style->setColorScheme(CarlaStyle::COLOR_BLUE);
  67. else if (color == "System")
  68. pass(); //style->setColorScheme(CarlaStyle::COLOR_SYSTEM);
  69. else
  70. style->setColorScheme(CarlaStyle::COLOR_BLACK);
  71. }
  72. }
  73. }
  74. ~CarlaBackendStandalone()
  75. {
  76. CARLA_ASSERT(engine == nullptr);
  77. }
  78. void init()
  79. {
  80. if (! needsInit)
  81. return;
  82. if (app != nullptr)
  83. return;
  84. static int argc = 0;
  85. static char** argv = nullptr;
  86. app = new QApplication(argc, argv, true);
  87. }
  88. void close()
  89. {
  90. if (! needsInit)
  91. return;
  92. if (app == nullptr)
  93. return;
  94. app->quit();
  95. app->processEvents();
  96. delete app;
  97. app = nullptr;
  98. }
  99. CARLA_DECLARE_NON_COPY_STRUCT_WITH_LEAK_DETECTOR(CarlaBackendStandalone)
  100. } standalone;
  101. // -------------------------------------------------------------------------------------------------------------------
  102. // API
  103. const char* carla_get_extended_license_text()
  104. {
  105. carla_debug("carla_get_extended_license_text()");
  106. static CarlaString retText;
  107. if (retText.isEmpty())
  108. {
  109. CarlaString text1, text2;
  110. text1 += "<p>This current Carla build is using the following features and 3rd-party code:</p>";
  111. text1 += "<ul>";
  112. // Plugin formats
  113. #ifdef WANT_LADSPA
  114. text1 += "<li>LADSPA plugin support, http://www.ladspa.org/</li>";
  115. #endif
  116. #ifdef WANT_DSSI
  117. text1 += "<li>DSSI plugin support, http://dssi.sourceforge.net/</li>";
  118. #endif
  119. #ifdef WANT_LV2
  120. text1 += "<li>LV2 plugin support, http://lv2plug.in/</li>";
  121. #endif
  122. #ifdef WANT_VST
  123. # ifdef VESTIGE_HEADER
  124. text1 += "<li>VST plugin support, using VeSTige header by Javier Serrano Polo</li>";
  125. # else
  126. text1 += "<li>VST plugin support, using official VST SDK 2.4 (trademark of Steinberg Media Technologies GmbH)</li>";
  127. # endif
  128. #endif
  129. // Sample kit libraries
  130. #ifdef WANT_FLUIDSYNTH
  131. text1 += "<li>FluidSynth library for SF2 support, http://www.fluidsynth.org/</li>";
  132. #endif
  133. #ifdef WANT_LINUXSAMPLER
  134. text1 += "<li>LinuxSampler library for GIG and SFZ support*, http://www.linuxsampler.org/</li>";
  135. #endif
  136. // Internal plugins
  137. #ifdef WANT_OPENGL
  138. text1 += "<li>DISTRHO Mini-Series plugin code, based on LOSER-dev suite by Michael Gruhn</li>";
  139. #endif
  140. text1 += "<li>NekoFilter plugin code, based on lv2fil by Nedko Arnaudov and Fons Adriaensen</li>";
  141. text1 += "<li>SunVox library file support, http://www.warmplace.ru/soft/sunvox/</li>";
  142. #ifdef WANT_AUDIOFILE
  143. text1 += "<li>AudioDecoder library for Audio file support, by Robin Gareus</li>";
  144. #endif
  145. #ifdef WANT_MIDIFILE
  146. text1 += "<li>LibSMF library for MIDI file support, http://libsmf.sourceforge.net/</li>";
  147. #endif
  148. #ifdef WANT_ZYNADDSUBFX
  149. text1 += "<li>ZynAddSubFX plugin code, http://zynaddsubfx.sf.net/</li>";
  150. # ifdef WANT_ZYNADDSUBFX_UI
  151. text1 += "<li>ZynAddSubFX UI using NTK, http://non.tuxfamily.org/wiki/NTK</li>";
  152. # endif
  153. #endif
  154. // misc libs
  155. text1 += "<li>liblo library for OSC support, http://liblo.sourceforge.net/</li>";
  156. #ifdef WANT_LV2
  157. text1 += "<li>serd, sord, sratom and lilv libraries for LV2 discovery, http://drobilla.net/software/lilv/</li>";
  158. #endif
  159. #ifdef WANT_RTAUDIO
  160. text1 += "<li>RtAudio+RtMidi libraries for extra Audio and MIDI support, http://www.music.mcgill.ca/~gary/rtaudio/</li>";
  161. #endif
  162. text1 += "</ul>";
  163. // code snippets
  164. text2 += "<p>Additionally, Carla uses code snippets from the following projects:</p>";
  165. text2 += "<ul>";
  166. text2 += "<li>Pointer and data leak utils from JUCE, http://www.rawmaterialsoftware.com/juce.php</li>";
  167. text2 += "<li>Shared memory utils from dssi-vst, http://www.breakfastquay.com/dssi-vst/</li>";
  168. text2 += "<li>Real-time memory pool, by Nedko Arnaudov</li>";
  169. text2 += "</ul>";
  170. // LinuxSampler GPL exception
  171. #ifdef WANT_LINUXSAMPLER
  172. text2 += "<p>(*) Using LinuxSampler code in commercial hardware or software products is not allowed without prior written authorization by the authors.</p>";
  173. #endif
  174. retText += text1;
  175. retText += text2;
  176. }
  177. return retText;
  178. }
  179. const char* carla_get_supported_file_types()
  180. {
  181. carla_debug("carla_get_supported_file_types()");
  182. static CarlaString retText;
  183. if (retText.isEmpty())
  184. {
  185. // Base types
  186. retText += "*.carxp;*.carxs";
  187. // Sample kits
  188. #ifdef WANT_FLUIDSYNTH
  189. retText += ";*.sf2";
  190. #endif
  191. #ifdef WANT_LINUXSAMPLER
  192. retText += ";*.gig;*.sfz";
  193. #endif
  194. // Files provided by internal plugins
  195. #ifdef WANT_AUDIOFILE
  196. retText += "*.3g2;*.3gp;*.aac;*.ac3;*.aiff;*.amr;*.ape;*.flac;*.mp2;*.mp3;*.mpc;*.oga;*.ogg;*.w64;*.wav;*.wma;";
  197. #endif
  198. #ifdef WANT_MIDIFILE
  199. retText += ";*.mid;*.midi";
  200. #endif
  201. // Plugin presets
  202. #ifdef WANT_ZYNADDSUBFX
  203. retText += ";*.xmz";
  204. #endif
  205. }
  206. return retText;
  207. }
  208. // -------------------------------------------------------------------------------------------------------------------
  209. unsigned int carla_get_engine_driver_count()
  210. {
  211. carla_debug("carla_get_engine_driver_count()");
  212. return CarlaEngine::getDriverCount();
  213. }
  214. const char* carla_get_engine_driver_name(unsigned int index)
  215. {
  216. carla_debug("carla_get_engine_driver_name(%i)", index);
  217. return CarlaEngine::getDriverName(index);
  218. }
  219. const void* carla_get_engine_driver_options(unsigned int index)
  220. {
  221. carla_debug("carla_get_engine_driver_options(%i)", index);
  222. return nullptr;
  223. // unused
  224. (void)index;
  225. }
  226. // -------------------------------------------------------------------------------------------------------------------
  227. unsigned int carla_get_internal_plugin_count()
  228. {
  229. carla_debug("carla_get_internal_plugin_count()");
  230. #ifdef WANT_NATIVE
  231. return static_cast<unsigned int>(CarlaPlugin::getNativePluginCount());
  232. #endif
  233. return 0;
  234. }
  235. const CarlaNativePluginInfo* carla_get_internal_plugin_info(unsigned int internalPluginId)
  236. {
  237. carla_debug("carla_get_internal_plugin_info(%i)", internalPluginId);
  238. static CarlaNativePluginInfo info;
  239. #ifdef WANT_NATIVE
  240. const PluginDescriptor* const nativePlugin(CarlaPlugin::getNativePluginDescriptor(internalPluginId));
  241. // as internal plugin, this must never fail
  242. CARLA_ASSERT(nativePlugin != nullptr);
  243. if (nativePlugin == nullptr)
  244. return nullptr;
  245. info.category = static_cast<CarlaPluginCategory>(nativePlugin->category);
  246. info.hints = 0x0;
  247. if (nativePlugin->hints & PLUGIN_IS_RTSAFE)
  248. info.hints |= CarlaBackend::PLUGIN_IS_RTSAFE;
  249. if (nativePlugin->hints & PLUGIN_IS_SYNTH)
  250. info.hints |= CarlaBackend::PLUGIN_IS_SYNTH;
  251. if (nativePlugin->hints & PLUGIN_HAS_GUI)
  252. info.hints |= CarlaBackend::PLUGIN_HAS_GUI;
  253. if (nativePlugin->hints & PLUGIN_USES_SINGLE_THREAD)
  254. info.hints |= CarlaBackend::PLUGIN_HAS_SINGLE_THREAD;
  255. info.audioIns = nativePlugin->audioIns;
  256. info.audioOuts = nativePlugin->audioOuts;
  257. info.midiIns = nativePlugin->midiIns;
  258. info.midiOuts = nativePlugin->midiOuts;
  259. info.parameterIns = nativePlugin->parameterIns;
  260. info.parameterOuts = nativePlugin->parameterOuts;
  261. info.name = nativePlugin->name;
  262. info.label = nativePlugin->label;
  263. info.maker = nativePlugin->maker;
  264. info.copyright = nativePlugin->copyright;
  265. #endif
  266. return &info;
  267. #ifndef WANT_NATIVE
  268. // unused
  269. (void)internalPluginId;
  270. #endif
  271. }
  272. // -------------------------------------------------------------------------------------------------------------------
  273. bool carla_engine_init(const char* driverName, const char* clientName)
  274. {
  275. carla_debug("carla_engine_init(\"%s\", \"%s\")", driverName, clientName);
  276. CARLA_ASSERT(standalone.engine == nullptr);
  277. CARLA_ASSERT(driverName != nullptr);
  278. CARLA_ASSERT(clientName != nullptr);
  279. if (standalone.engine != nullptr)
  280. {
  281. standalone.lastError = "Engine is already running";
  282. return false;
  283. }
  284. standalone.engine = CarlaEngine::newDriverByName(driverName);
  285. if (standalone.engine == nullptr)
  286. {
  287. standalone.lastError = "The seleted audio driver is not available!";
  288. return false;
  289. }
  290. #ifndef Q_OS_WIN
  291. // TODO: make this an option, put somewhere else
  292. if (getenv("WINE_RT") == nullptr)
  293. {
  294. carla_setenv("WINE_RT", "15");
  295. carla_setenv("WINE_SVR_RT", "10");
  296. }
  297. #endif
  298. if (standalone.callback != nullptr)
  299. standalone.engine->setCallback(standalone.callback, nullptr);
  300. #ifndef BUILD_BRIDGE
  301. standalone.engine->setOption(CarlaBackend::OPTION_PROCESS_MODE, static_cast<int>(standalone.options.processMode), nullptr);
  302. # if 0 // disabled for now
  303. standalone.engine->setOption(CarlaBackend::OPTION_TRANSPORT_MODE, static_cast<int>(standalone.options.transportMode), nullptr);
  304. # endif
  305. standalone.engine->setOption(CarlaBackend::OPTION_FORCE_STEREO, standalone.options.forceStereo ? 1 : 0, nullptr);
  306. standalone.engine->setOption(CarlaBackend::OPTION_PREFER_PLUGIN_BRIDGES, standalone.options.preferPluginBridges ? 1 : 0, nullptr);
  307. standalone.engine->setOption(CarlaBackend::OPTION_PREFER_UI_BRIDGES, standalone.options.preferUiBridges ? 1 : 0, nullptr);
  308. # ifdef WANT_DSSI
  309. standalone.engine->setOption(CarlaBackend::OPTION_USE_DSSI_VST_CHUNKS, standalone.options.useDssiVstChunks ? 1 : 0, nullptr);
  310. # endif
  311. standalone.engine->setOption(CarlaBackend::OPTION_MAX_PARAMETERS, static_cast<int>(standalone.options.maxParameters), nullptr);
  312. standalone.engine->setOption(CarlaBackend::OPTION_PREFERRED_BUFFER_SIZE, static_cast<int>(standalone.options.preferredBufferSize), nullptr);
  313. standalone.engine->setOption(CarlaBackend::OPTION_PREFERRED_SAMPLE_RATE, static_cast<int>(standalone.options.preferredSampleRate), nullptr);
  314. standalone.engine->setOption(CarlaBackend::OPTION_OSC_UI_TIMEOUT, static_cast<int>(standalone.options.oscUiTimeout), nullptr);
  315. standalone.engine->setOption(CarlaBackend::OPTION_PATH_BRIDGE_NATIVE, 0, (const char*)standalone.options.bridge_native);
  316. standalone.engine->setOption(CarlaBackend::OPTION_PATH_BRIDGE_POSIX32, 0, (const char*)standalone.options.bridge_posix32);
  317. standalone.engine->setOption(CarlaBackend::OPTION_PATH_BRIDGE_POSIX64, 0, (const char*)standalone.options.bridge_posix64);
  318. standalone.engine->setOption(CarlaBackend::OPTION_PATH_BRIDGE_WIN32, 0, (const char*)standalone.options.bridge_win32);
  319. standalone.engine->setOption(CarlaBackend::OPTION_PATH_BRIDGE_WIN64, 0, (const char*)standalone.options.bridge_win64);
  320. # ifdef WANT_LV2
  321. standalone.engine->setOption(CarlaBackend::OPTION_PATH_BRIDGE_LV2_GTK2, 0, (const char*)standalone.options.bridge_lv2Gtk2);
  322. standalone.engine->setOption(CarlaBackend::OPTION_PATH_BRIDGE_LV2_GTK3, 0, (const char*)standalone.options.bridge_lv2Gtk3);
  323. standalone.engine->setOption(CarlaBackend::OPTION_PATH_BRIDGE_LV2_QT4, 0, (const char*)standalone.options.bridge_lv2Qt4);
  324. standalone.engine->setOption(CarlaBackend::OPTION_PATH_BRIDGE_LV2_QT5, 0, (const char*)standalone.options.bridge_lv2Qt5);
  325. standalone.engine->setOption(CarlaBackend::OPTION_PATH_BRIDGE_LV2_COCOA, 0, (const char*)standalone.options.bridge_lv2Cocoa);
  326. standalone.engine->setOption(CarlaBackend::OPTION_PATH_BRIDGE_LV2_WINDOWS, 0, (const char*)standalone.options.bridge_lv2Win);
  327. standalone.engine->setOption(CarlaBackend::OPTION_PATH_BRIDGE_LV2_X11, 0, (const char*)standalone.options.bridge_lv2X11);
  328. # endif
  329. # ifdef WANT_VST
  330. standalone.engine->setOption(CarlaBackend::OPTION_PATH_BRIDGE_VST_COCOA, 0, (const char*)standalone.options.bridge_vstCocoa);
  331. standalone.engine->setOption(CarlaBackend::OPTION_PATH_BRIDGE_VST_HWND, 0, (const char*)standalone.options.bridge_vstHWND);
  332. standalone.engine->setOption(CarlaBackend::OPTION_PATH_BRIDGE_VST_X11, 0, (const char*)standalone.options.bridge_vstX11);
  333. # endif
  334. if (standalone.procName.isNotEmpty())
  335. standalone.engine->setOption(CarlaBackend::OPTION_PROCESS_NAME, 0, (const char*)standalone.procName);
  336. #endif
  337. if (standalone.engine->init(clientName))
  338. {
  339. standalone.lastError = "no error";
  340. standalone.init();
  341. return true;
  342. }
  343. else
  344. {
  345. standalone.lastError = standalone.engine->getLastError();
  346. delete standalone.engine;
  347. standalone.engine = nullptr;
  348. return false;
  349. }
  350. }
  351. bool carla_engine_close()
  352. {
  353. carla_debug("carla_engine_close()");
  354. CARLA_ASSERT(standalone.engine != nullptr);
  355. if (standalone.engine == nullptr)
  356. {
  357. standalone.lastError = "Engine is not started";
  358. return false;
  359. }
  360. standalone.engine->setAboutToClose();
  361. standalone.engine->removeAllPlugins();
  362. const bool closed(standalone.engine->close());
  363. if (! closed)
  364. standalone.lastError = standalone.engine->getLastError();
  365. delete standalone.engine;
  366. standalone.engine = nullptr;
  367. standalone.close();
  368. return closed;
  369. }
  370. void carla_engine_idle()
  371. {
  372. CARLA_ASSERT(standalone.engine != nullptr);
  373. if (standalone.needsInit && standalone.app != nullptr)
  374. standalone.app->processEvents();
  375. if (standalone.engine != nullptr)
  376. standalone.engine->idle();
  377. }
  378. bool carla_is_engine_running()
  379. {
  380. carla_debug("carla_is_engine_running()");
  381. return (standalone.engine != nullptr && standalone.engine->isRunning());
  382. }
  383. void carla_set_engine_about_to_close()
  384. {
  385. carla_debug("carla_set_engine_about_to_close()");
  386. CARLA_ASSERT(standalone.engine != nullptr);
  387. if (standalone.engine != nullptr)
  388. standalone.engine->setAboutToClose();
  389. }
  390. void carla_set_engine_callback(CarlaCallbackFunc func, void* ptr)
  391. {
  392. carla_debug("carla_set_engine_callback(%p)", func);
  393. standalone.callback = func;
  394. standalone.callbackPtr = ptr;
  395. if (standalone.engine != nullptr)
  396. standalone.engine->setCallback(func, ptr);
  397. }
  398. void carla_set_engine_option(CarlaOptionsType option, int value, const char* valueStr)
  399. {
  400. carla_debug("carla_set_engine_option(%s, %i, \"%s\")", CarlaBackend::OptionsType2Str(option), value, valueStr);
  401. switch (option)
  402. {
  403. case CarlaBackend::OPTION_PROCESS_NAME:
  404. standalone.procName = valueStr;
  405. break;
  406. case CarlaBackend::OPTION_PROCESS_MODE:
  407. if (value < CarlaBackend::PROCESS_MODE_SINGLE_CLIENT || value > CarlaBackend::PROCESS_MODE_PATCHBAY)
  408. return carla_stderr2("carla_set_engine_option(OPTION_PROCESS_MODE, %i, \"%s\") - invalid value", value, valueStr);
  409. standalone.options.processMode = static_cast<CarlaBackend::ProcessMode>(value);
  410. break;
  411. case CarlaBackend::OPTION_TRANSPORT_MODE:
  412. #if 0 // disabled for now
  413. if (value < CarlaBackend::TRANSPORT_MODE_INTERNAL || value > CarlaBackend::TRANSPORT_MODE_JACK)
  414. return carla_stderr2("carla_set_engine_option(OPTION_TRANSPORT_MODE, %i, \"%s\") - invalid value", value, valueStr);
  415. standalone.options.transportMode = static_cast<CarlaBackend::TransportMode>(value);
  416. #endif
  417. break;
  418. case CarlaBackend::OPTION_FORCE_STEREO:
  419. standalone.options.forceStereo = (value != 0);
  420. break;
  421. case CarlaBackend::OPTION_PREFER_PLUGIN_BRIDGES:
  422. standalone.options.preferPluginBridges = (value != 0);
  423. break;
  424. case CarlaBackend::OPTION_PREFER_UI_BRIDGES:
  425. standalone.options.preferUiBridges = (value != 0);
  426. break;
  427. #ifdef WANT_DSSI
  428. case CarlaBackend::OPTION_USE_DSSI_VST_CHUNKS:
  429. standalone.options.useDssiVstChunks = (value != 0);
  430. break;
  431. #endif
  432. case CarlaBackend::OPTION_MAX_PARAMETERS:
  433. if (value <= 0)
  434. return carla_stderr2("carla_set_engine_option(OPTION_MAX_PARAMETERS, %i, \"%s\") - invalid value", value, valueStr);
  435. standalone.options.maxParameters = static_cast<unsigned int>(value);
  436. break;
  437. case CarlaBackend::OPTION_OSC_UI_TIMEOUT:
  438. if (value <= 0)
  439. return carla_stderr2("carla_set_engine_option(OPTION_OSC_UI_TIMEOUT, %i, \"%s\") - invalid value", value, valueStr);
  440. standalone.options.oscUiTimeout = static_cast<unsigned int>(value);
  441. break;
  442. case CarlaBackend::OPTION_PREFERRED_BUFFER_SIZE:
  443. if (value <= 0)
  444. return carla_stderr2("carla_set_engine_option(OPTION_PREFERRED_BUFFER_SIZE, %i, \"%s\") - invalid value", value, valueStr);
  445. standalone.options.preferredBufferSize = static_cast<unsigned int>(value);
  446. break;
  447. case CarlaBackend::OPTION_PREFERRED_SAMPLE_RATE:
  448. if (value <= 0)
  449. return carla_stderr2("carla_set_engine_option(OPTION_PREFERRED_SAMPLE_RATE, %i, \"%s\") - invalid value", value, valueStr);
  450. standalone.options.preferredSampleRate = static_cast<unsigned int>(value);
  451. break;
  452. #ifndef BUILD_BRIDGE
  453. case CarlaBackend::OPTION_PATH_BRIDGE_NATIVE:
  454. standalone.options.bridge_native = valueStr;
  455. break;
  456. case CarlaBackend::OPTION_PATH_BRIDGE_POSIX32:
  457. standalone.options.bridge_posix32 = valueStr;
  458. break;
  459. case CarlaBackend::OPTION_PATH_BRIDGE_POSIX64:
  460. standalone.options.bridge_posix64 = valueStr;
  461. break;
  462. case CarlaBackend::OPTION_PATH_BRIDGE_WIN32:
  463. standalone.options.bridge_win32 = valueStr;
  464. break;
  465. case CarlaBackend::OPTION_PATH_BRIDGE_WIN64:
  466. standalone.options.bridge_win64 = valueStr;
  467. break;
  468. #endif
  469. #ifdef WANT_LV2
  470. case CarlaBackend::OPTION_PATH_BRIDGE_LV2_GTK2:
  471. standalone.options.bridge_lv2Gtk2 = valueStr;
  472. break;
  473. case CarlaBackend::OPTION_PATH_BRIDGE_LV2_GTK3:
  474. standalone.options.bridge_lv2Gtk3 = valueStr;
  475. break;
  476. case CarlaBackend::OPTION_PATH_BRIDGE_LV2_QT4:
  477. standalone.options.bridge_lv2Qt4 = valueStr;
  478. break;
  479. case CarlaBackend::OPTION_PATH_BRIDGE_LV2_QT5:
  480. standalone.options.bridge_lv2Qt5 = valueStr;
  481. break;
  482. case CarlaBackend::OPTION_PATH_BRIDGE_LV2_COCOA:
  483. standalone.options.bridge_lv2Cocoa = valueStr;
  484. break;
  485. case CarlaBackend::OPTION_PATH_BRIDGE_LV2_WINDOWS:
  486. standalone.options.bridge_lv2Win = valueStr;
  487. break;
  488. case CarlaBackend::OPTION_PATH_BRIDGE_LV2_X11:
  489. standalone.options.bridge_lv2X11 = valueStr;
  490. break;
  491. #endif
  492. #ifdef WANT_VST
  493. case CarlaBackend::OPTION_PATH_BRIDGE_VST_COCOA:
  494. standalone.options.bridge_vstCocoa = valueStr;
  495. break;
  496. case CarlaBackend::OPTION_PATH_BRIDGE_VST_HWND:
  497. standalone.options.bridge_vstHWND = valueStr;
  498. break;
  499. case CarlaBackend::OPTION_PATH_BRIDGE_VST_X11:
  500. standalone.options.bridge_vstX11 = valueStr;
  501. break;
  502. #endif
  503. }
  504. if (standalone.engine != nullptr)
  505. standalone.engine->setOption(option, value, valueStr);
  506. }
  507. // -------------------------------------------------------------------------------------------------------------------
  508. bool carla_load_filename(const char* filename)
  509. {
  510. carla_debug("carla_load_filename(\"%s\")", filename);
  511. CARLA_ASSERT(standalone.engine != nullptr);
  512. CARLA_ASSERT(filename != nullptr);
  513. if (standalone.engine != nullptr && standalone.engine->isRunning())
  514. return standalone.engine->loadFilename(filename);
  515. standalone.lastError = "Engine is not started";
  516. return false;
  517. }
  518. bool carla_load_project(const char* filename)
  519. {
  520. carla_debug("carla_load_project(\"%s\")", filename);
  521. CARLA_ASSERT(standalone.engine != nullptr);
  522. CARLA_ASSERT(filename != nullptr);
  523. if (standalone.engine != nullptr && standalone.engine->isRunning())
  524. return standalone.engine->loadProject(filename);
  525. standalone.lastError = "Engine is not started";
  526. return false;
  527. }
  528. bool carla_save_project(const char* filename)
  529. {
  530. carla_debug("carla_save_project(\"%s\")", filename);
  531. CARLA_ASSERT(standalone.engine != nullptr);
  532. CARLA_ASSERT(filename != nullptr);
  533. if (standalone.engine != nullptr) // allow to save even if engine stopped
  534. return standalone.engine->saveProject(filename);
  535. standalone.lastError = "Engine is not started";
  536. return false;
  537. }
  538. // -------------------------------------------------------------------------------------------------------------------
  539. bool carla_patchbay_connect(int portA, int portB)
  540. {
  541. carla_debug("carla_patchbay_connect(%i, %i)", portA, portB);
  542. CARLA_ASSERT(standalone.engine != nullptr);
  543. if (standalone.engine != nullptr && standalone.engine->isRunning())
  544. return standalone.engine->patchbayConnect(portA, portB);
  545. standalone.lastError = "Engine is not started";
  546. return false;
  547. }
  548. bool carla_patchbay_disconnect(int connectionId)
  549. {
  550. carla_debug("carla_patchbay_disconnect(%i)", connectionId);
  551. CARLA_ASSERT(standalone.engine != nullptr);
  552. if (standalone.engine != nullptr && standalone.engine->isRunning())
  553. return standalone.engine->patchbayDisconnect(connectionId);
  554. standalone.lastError = "Engine is not started";
  555. return false;
  556. }
  557. void carla_patchbay_refresh()
  558. {
  559. carla_debug("carla_patchbay_refresh()");
  560. CARLA_ASSERT(standalone.engine != nullptr);
  561. if (standalone.engine != nullptr && standalone.engine->isRunning())
  562. standalone.engine->patchbayRefresh();
  563. }
  564. // -------------------------------------------------------------------------------------------------------------------
  565. void carla_transport_play()
  566. {
  567. carla_debug("carla_transport_play()");
  568. CARLA_ASSERT(standalone.engine != nullptr);
  569. if (standalone.engine != nullptr && standalone.engine->isRunning())
  570. standalone.engine->transportPlay();
  571. }
  572. void carla_transport_pause()
  573. {
  574. carla_debug("carla_transport_pause()");
  575. CARLA_ASSERT(standalone.engine != nullptr);
  576. if (standalone.engine != nullptr && standalone.engine->isRunning())
  577. standalone.engine->transportPause();
  578. }
  579. void carla_transport_relocate(uint32_t frames)
  580. {
  581. carla_debug("carla_transport_relocate(%i)", frames);
  582. CARLA_ASSERT(standalone.engine != nullptr);
  583. if (standalone.engine != nullptr && standalone.engine->isRunning())
  584. standalone.engine->transportRelocate(frames);
  585. }
  586. uint32_t carla_get_current_transport_frame()
  587. {
  588. CARLA_ASSERT(standalone.engine != nullptr);
  589. if (standalone.engine != nullptr)
  590. {
  591. const EngineTimeInfo& timeInfo(standalone.engine->getTimeInfo());
  592. return timeInfo.frame;
  593. }
  594. return 0;
  595. }
  596. const CarlaTransportInfo* carla_get_transport_info()
  597. {
  598. CARLA_ASSERT(standalone.engine != nullptr);
  599. static CarlaTransportInfo info;
  600. if (standalone.engine != nullptr)
  601. {
  602. const EngineTimeInfo& timeInfo(standalone.engine->getTimeInfo());
  603. info.playing = timeInfo.playing;
  604. info.frame = timeInfo.frame;
  605. if (timeInfo.valid & timeInfo.ValidBBT)
  606. {
  607. info.bar = timeInfo.bbt.bar;
  608. info.beat = timeInfo.bbt.beat;
  609. info.tick = timeInfo.bbt.tick;
  610. info.bpm = timeInfo.bbt.beatsPerMinute;
  611. }
  612. else
  613. {
  614. info.bar = 0;
  615. info.beat = 0;
  616. info.tick = 0;
  617. info.bpm = 0.0;
  618. }
  619. }
  620. else
  621. {
  622. info.playing = false;
  623. info.frame = 0;
  624. info.bar = 0;
  625. info.beat = 0;
  626. info.tick = 0;
  627. info.bpm = 0.0;
  628. }
  629. return &info;
  630. }
  631. // -------------------------------------------------------------------------------------------------------------------
  632. bool carla_add_plugin(CarlaBinaryType btype, CarlaPluginType ptype, const char* filename, const char* const name, const char* label, const void* extraStuff)
  633. {
  634. carla_debug("carla_add_plugin(%s, %s, \"%s\", \"%s\", \"%s\", %p)", CarlaBackend::BinaryType2Str(btype), CarlaBackend::PluginType2Str(ptype), filename, name, label, extraStuff);
  635. CARLA_ASSERT(standalone.engine != nullptr);
  636. if (standalone.engine != nullptr && standalone.engine->isRunning())
  637. return standalone.engine->addPlugin(btype, ptype, filename, name, label, extraStuff);
  638. standalone.lastError = "Engine is not started";
  639. return false;
  640. }
  641. bool carla_remove_plugin(unsigned int pluginId)
  642. {
  643. carla_debug("carla_remove_plugin(%i)", pluginId);
  644. CARLA_ASSERT(standalone.engine != nullptr);
  645. if (standalone.engine != nullptr && standalone.engine->isRunning())
  646. return standalone.engine->removePlugin(pluginId);
  647. standalone.lastError = "Engine is not started";
  648. return false;
  649. }
  650. void carla_remove_all_plugins()
  651. {
  652. carla_debug("carla_remove_all_plugins()");
  653. CARLA_ASSERT(standalone.engine != nullptr);
  654. if (standalone.engine != nullptr && standalone.engine->isRunning())
  655. standalone.engine->removeAllPlugins();
  656. }
  657. const char* carla_rename_plugin(unsigned int pluginId, const char* newName)
  658. {
  659. carla_debug("carla_rename_plugin(%i, \"%s\")", pluginId, newName);
  660. CARLA_ASSERT(standalone.engine != nullptr);
  661. if (standalone.engine != nullptr && standalone.engine->isRunning())
  662. return standalone.engine->renamePlugin(pluginId, newName);
  663. standalone.lastError = "Engine is not started";
  664. return nullptr;
  665. }
  666. bool carla_clone_plugin(unsigned int pluginId)
  667. {
  668. carla_debug("carla_clone_plugin(%i)", pluginId);
  669. CARLA_ASSERT(standalone.engine != nullptr);
  670. if (standalone.engine != nullptr && standalone.engine->isRunning())
  671. return standalone.engine->clonePlugin(pluginId);
  672. standalone.lastError = "Engine is not started";
  673. return false;
  674. }
  675. bool carla_replace_plugin(unsigned int pluginId)
  676. {
  677. carla_debug("carla_replace_plugin(%i)", pluginId);
  678. CARLA_ASSERT(standalone.engine != nullptr);
  679. if (standalone.engine != nullptr && standalone.engine->isRunning())
  680. return standalone.engine->replacePlugin(pluginId);
  681. standalone.lastError = "Engine is not started";
  682. return false;
  683. }
  684. bool carla_switch_plugins(unsigned int pluginIdA, unsigned int pluginIdB)
  685. {
  686. carla_debug("carla_switch_plugins(%i, %i)", pluginIdA, pluginIdB);
  687. CARLA_ASSERT(standalone.engine != nullptr);
  688. if (standalone.engine != nullptr && standalone.engine->isRunning())
  689. return standalone.engine->switchPlugins(pluginIdA, pluginIdB);
  690. standalone.lastError = "Engine is not started";
  691. return false;
  692. }
  693. // -------------------------------------------------------------------------------------------------------------------
  694. bool carla_load_plugin_state(unsigned int pluginId, const char* filename)
  695. {
  696. carla_debug("carla_load_plugin_state(%i, \"%s\")", pluginId, filename);
  697. CARLA_ASSERT(standalone.engine != nullptr);
  698. if (standalone.engine == nullptr || ! standalone.engine->isRunning())
  699. {
  700. standalone.lastError = "Engine is not started";
  701. return false;
  702. }
  703. if (CarlaPlugin* const plugin = standalone.engine->getPlugin(pluginId))
  704. return plugin->loadStateFromFile(filename);
  705. carla_stderr2("carla_load_plugin_state(%i, \"%s\") - could not find plugin", pluginId, filename);
  706. return false;
  707. }
  708. bool carla_save_plugin_state(unsigned int pluginId, const char* filename)
  709. {
  710. carla_debug("carla_save_plugin_state(%i, \"%s\")", pluginId, filename);
  711. CARLA_ASSERT(standalone.engine != nullptr);
  712. if (standalone.engine == nullptr)
  713. {
  714. standalone.lastError = "Engine is not started";
  715. return false;
  716. }
  717. if (CarlaPlugin* const plugin = standalone.engine->getPlugin(pluginId))
  718. return plugin->saveStateToFile(filename);
  719. carla_stderr2("carla_save_plugin_state(%i, \"%s\") - could not find plugin", pluginId, filename);
  720. return false;
  721. }
  722. // -------------------------------------------------------------------------------------------------------------------
  723. const CarlaPluginInfo* carla_get_plugin_info(unsigned int pluginId)
  724. {
  725. carla_debug("carla_get_plugin_info(%i)", pluginId);
  726. CARLA_ASSERT(standalone.engine != nullptr);
  727. static CarlaPluginInfo info;
  728. // reset
  729. info.type = CarlaBackend::PLUGIN_NONE;
  730. info.category = CarlaBackend::PLUGIN_CATEGORY_NONE;
  731. info.hints = 0x0;
  732. info.hints = 0x0;
  733. info.binary = nullptr;
  734. info.name = nullptr;
  735. info.uniqueId = 0;
  736. info.latency = 0;
  737. info.optionsAvailable = 0x0;
  738. info.optionsEnabled = 0x0;
  739. // cleanup
  740. if (info.label != nullptr)
  741. {
  742. delete[] info.label;
  743. info.label = nullptr;
  744. }
  745. if (info.maker != nullptr)
  746. {
  747. delete[] info.maker;
  748. info.maker = nullptr;
  749. }
  750. if (info.copyright != nullptr)
  751. {
  752. delete[] info.copyright;
  753. info.copyright = nullptr;
  754. }
  755. if (standalone.engine == nullptr)
  756. return &info;
  757. if (CarlaPlugin* const plugin = standalone.engine->getPlugin(pluginId))
  758. {
  759. char strBufLabel[STR_MAX+1] = { '\0' };
  760. char strBufMaker[STR_MAX+1] = { '\0' };
  761. char strBufCopyright[STR_MAX+1] = { '\0' };
  762. info.type = plugin->type();
  763. info.category = plugin->category();
  764. info.hints = plugin->hints();
  765. info.binary = plugin->filename();
  766. info.name = plugin->name();
  767. info.uniqueId = plugin->uniqueId();
  768. info.latency = plugin->latency();
  769. info.optionsAvailable = plugin->availableOptions();
  770. info.optionsEnabled = plugin->options();
  771. plugin->getLabel(strBufLabel);
  772. info.label = carla_strdup(strBufLabel);
  773. plugin->getMaker(strBufMaker);
  774. info.maker = carla_strdup(strBufMaker);
  775. plugin->getCopyright(strBufCopyright);
  776. info.copyright = carla_strdup(strBufCopyright);
  777. return &info;
  778. }
  779. carla_stderr2("carla_get_plugin_info(%i) - could not find plugin", pluginId);
  780. return &info;
  781. }
  782. const CarlaPortCountInfo* carla_get_audio_port_count_info(unsigned int pluginId)
  783. {
  784. carla_debug("carla_get_audio_port_count_info(%i)", pluginId);
  785. CARLA_ASSERT(standalone.engine != nullptr);
  786. static CarlaPortCountInfo info;
  787. // reset
  788. info.ins = 0;
  789. info.outs = 0;
  790. info.total = 0;
  791. if (standalone.engine == nullptr)
  792. return &info;
  793. if (CarlaPlugin* const plugin = standalone.engine->getPlugin(pluginId))
  794. {
  795. info.ins = plugin->audioInCount();
  796. info.outs = plugin->audioOutCount();
  797. info.total = info.ins + info.outs;
  798. return &info;
  799. }
  800. carla_stderr2("carla_get_audio_port_count_info(%i) - could not find plugin", pluginId);
  801. return &info;
  802. }
  803. const CarlaPortCountInfo* carla_get_midi_port_count_info(unsigned int pluginId)
  804. {
  805. carla_debug("carla_get_midi_port_count_info(%i)", pluginId);
  806. CARLA_ASSERT(standalone.engine != nullptr);
  807. static CarlaPortCountInfo info;
  808. // reset
  809. info.ins = 0;
  810. info.outs = 0;
  811. info.total = 0;
  812. if (standalone.engine == nullptr)
  813. return &info;
  814. if (CarlaPlugin* const plugin = standalone.engine->getPlugin(pluginId))
  815. {
  816. info.ins = plugin->midiInCount();
  817. info.outs = plugin->midiOutCount();
  818. info.total = info.ins + info.outs;
  819. return &info;
  820. }
  821. carla_stderr2("carla_get_midi_port_count_info(%i) - could not find plugin", pluginId);
  822. return &info;
  823. }
  824. const CarlaPortCountInfo* carla_get_parameter_count_info(unsigned int pluginId)
  825. {
  826. carla_debug("carla_get_parameter_count_info(%i)", pluginId);
  827. CARLA_ASSERT(standalone.engine != nullptr);
  828. static CarlaPortCountInfo info;
  829. // reset
  830. info.ins = 0;
  831. info.outs = 0;
  832. info.total = 0;
  833. if (standalone.engine == nullptr)
  834. return &info;
  835. if (CarlaPlugin* const plugin = standalone.engine->getPlugin(pluginId))
  836. {
  837. plugin->getParameterCountInfo(&info.ins, &info.outs, &info.total);
  838. return &info;
  839. }
  840. carla_stderr2("carla_get_parameter_count_info(%i) - could not find plugin", pluginId);
  841. return &info;
  842. }
  843. const CarlaParameterInfo* carla_get_parameter_info(unsigned int pluginId, uint32_t parameterId)
  844. {
  845. carla_debug("carla_get_parameter_info(%i, %i)", pluginId, parameterId);
  846. CARLA_ASSERT(standalone.engine != nullptr);
  847. static CarlaParameterInfo info;
  848. // reset
  849. info.scalePointCount = 0;
  850. // cleanup
  851. if (info.name != nullptr)
  852. {
  853. delete[] info.name;
  854. info.name = nullptr;
  855. }
  856. if (info.symbol != nullptr)
  857. {
  858. delete[] info.symbol;
  859. info.symbol = nullptr;
  860. }
  861. if (info.unit != nullptr)
  862. {
  863. delete[] info.unit;
  864. info.unit = nullptr;
  865. }
  866. if (standalone.engine == nullptr)
  867. return &info;
  868. if (CarlaPlugin* const plugin = standalone.engine->getPlugin(pluginId))
  869. {
  870. if (parameterId < plugin->parameterCount())
  871. {
  872. char strBufName[STR_MAX+1] = { '\0' };
  873. char strBufSymbol[STR_MAX+1] = { '\0' };
  874. char strBufUnit[STR_MAX+1] = { '\0' };
  875. info.scalePointCount = plugin->parameterScalePointCount(parameterId);
  876. plugin->getParameterName(parameterId, strBufName);
  877. info.name = carla_strdup(strBufName);
  878. plugin->getParameterSymbol(parameterId, strBufSymbol);
  879. info.symbol = carla_strdup(strBufSymbol);
  880. plugin->getParameterUnit(parameterId, strBufUnit);
  881. info.unit = carla_strdup(strBufUnit);
  882. }
  883. else
  884. carla_stderr2("carla_get_parameter_info(%i, %i) - parameterId out of bounds", pluginId, parameterId);
  885. return &info;
  886. }
  887. carla_stderr2("carla_get_parameter_info(%i, %i) - could not find plugin", pluginId, parameterId);
  888. return &info;
  889. }
  890. const CarlaScalePointInfo* carla_get_parameter_scalepoint_info(unsigned int pluginId, uint32_t parameterId, uint32_t scalePointId)
  891. {
  892. carla_debug("carla_get_parameter_scalepoint_info(%i, %i, %i)", pluginId, parameterId, scalePointId);
  893. CARLA_ASSERT(standalone.engine != nullptr);
  894. static CarlaScalePointInfo info;
  895. // reset
  896. info.value = 0.0f;
  897. // cleanup
  898. if (info.label != nullptr)
  899. {
  900. delete[] info.label;
  901. info.label = nullptr;
  902. }
  903. if (standalone.engine == nullptr)
  904. return &info;
  905. if (CarlaPlugin* const plugin = standalone.engine->getPlugin(pluginId))
  906. {
  907. if (parameterId < plugin->parameterCount())
  908. {
  909. if (scalePointId < plugin->parameterScalePointCount(parameterId))
  910. {
  911. char strBufLabel[STR_MAX+1] = { '\0' };
  912. info.value = plugin->getParameterScalePointValue(parameterId, scalePointId);
  913. plugin->getParameterScalePointLabel(parameterId, scalePointId, strBufLabel);
  914. info.label = carla_strdup(strBufLabel);
  915. }
  916. else
  917. carla_stderr2("carla_get_parameter_scalepoint_info(%i, %i, %i) - scalePointId out of bounds", pluginId, parameterId, scalePointId);
  918. }
  919. else
  920. carla_stderr2("carla_get_parameter_scalepoint_info(%i, %i, %i) - parameterId out of bounds", pluginId, parameterId, scalePointId);
  921. return &info;
  922. }
  923. carla_stderr2("carla_get_parameter_scalepoint_info(%i, %i, %i) - could not find plugin", pluginId, parameterId, scalePointId);
  924. return &info;
  925. }
  926. // -------------------------------------------------------------------------------------------------------------------
  927. const CarlaParameterData* carla_get_parameter_data(unsigned int pluginId, uint32_t parameterId)
  928. {
  929. carla_debug("carla_get_parameter_data(%i, %i)", pluginId, parameterId);
  930. CARLA_ASSERT(standalone.engine != nullptr);
  931. static CarlaParameterData data;
  932. if (standalone.engine == nullptr)
  933. return &data;
  934. if (CarlaPlugin* const plugin = standalone.engine->getPlugin(pluginId))
  935. {
  936. if (parameterId < plugin->parameterCount())
  937. return &plugin->parameterData(parameterId);
  938. carla_stderr2("carla_get_parameter_data(%i, %i) - parameterId out of bounds", pluginId, parameterId);
  939. return &data;
  940. }
  941. carla_stderr2("carla_get_parameter_data(%i, %i) - could not find plugin", pluginId, parameterId);
  942. return &data;
  943. }
  944. const CarlaParameterRanges* carla_get_parameter_ranges(unsigned int pluginId, uint32_t parameterId)
  945. {
  946. carla_debug("carla_get_parameter_ranges(%i, %i)", pluginId, parameterId);
  947. CARLA_ASSERT(standalone.engine != nullptr);
  948. static CarlaParameterRanges ranges;
  949. if (standalone.engine == nullptr)
  950. return &ranges;
  951. if (CarlaPlugin* const plugin = standalone.engine->getPlugin(pluginId))
  952. {
  953. if (parameterId < plugin->parameterCount())
  954. return &plugin->parameterRanges(parameterId);
  955. carla_stderr2("carla_get_parameter_ranges(%i, %i) - parameterId out of bounds", pluginId, parameterId);
  956. return &ranges;
  957. }
  958. carla_stderr2("carla_get_parameter_ranges(%i, %i) - could not find plugin", pluginId, parameterId);
  959. return &ranges;
  960. }
  961. const CarlaMidiProgramData* carla_get_midi_program_data(unsigned int pluginId, uint32_t midiProgramId)
  962. {
  963. carla_debug("carla_get_midi_program_data(%i, %i)", pluginId, midiProgramId);
  964. CARLA_ASSERT(standalone.engine != nullptr);
  965. static CarlaMidiProgramData data;
  966. if (standalone.engine == nullptr)
  967. return &data;
  968. if (CarlaPlugin* const plugin = standalone.engine->getPlugin(pluginId))
  969. {
  970. if (midiProgramId < plugin->midiProgramCount())
  971. return &plugin->midiProgramData(midiProgramId);
  972. carla_stderr2("carla_get_midi_program_data(%i, %i) - midiProgramId out of bounds", pluginId, midiProgramId);
  973. return &data;
  974. }
  975. carla_stderr2("carla_get_midi_program_data(%i, %i) - could not find plugin", pluginId, midiProgramId);
  976. return &data;
  977. }
  978. const CarlaCustomData* carla_get_custom_data(unsigned int pluginId, uint32_t customDataId)
  979. {
  980. carla_debug("carla_get_custom_data(%i, %i)", pluginId, customDataId);
  981. CARLA_ASSERT(standalone.engine != nullptr);
  982. static CarlaCustomData data;
  983. if (standalone.engine == nullptr)
  984. return &data;
  985. if (CarlaPlugin* const plugin = standalone.engine->getPlugin(pluginId))
  986. {
  987. if (customDataId < plugin->customDataCount())
  988. return &plugin->customData(customDataId);
  989. carla_stderr2("carla_get_custom_data(%i, %i) - customDataId out of bounds", pluginId, customDataId);
  990. return &data;
  991. }
  992. carla_stderr2("carla_get_custom_data(%i, %i) - could not find plugin", pluginId, customDataId);
  993. return &data;
  994. }
  995. const char* carla_get_chunk_data(unsigned int pluginId)
  996. {
  997. carla_debug("carla_get_chunk_data(%i)", pluginId);
  998. CARLA_ASSERT(standalone.engine != nullptr);
  999. if (standalone.engine == nullptr)
  1000. return nullptr;
  1001. static CarlaString chunkData;
  1002. // cleanup
  1003. chunkData.clear();
  1004. if (CarlaPlugin* const plugin = standalone.engine->getPlugin(pluginId))
  1005. {
  1006. if (plugin->options() & CarlaBackend::PLUGIN_OPTION_USE_CHUNKS)
  1007. {
  1008. void* data = nullptr;
  1009. const int32_t dataSize = plugin->chunkData(&data);
  1010. if (data != nullptr && dataSize > 0)
  1011. {
  1012. QByteArray chunk(QByteArray((char*)data, dataSize).toBase64());
  1013. chunkData = chunk.constData();
  1014. return (const char*)chunkData;
  1015. }
  1016. else
  1017. carla_stderr2("carla_get_chunk_data(%i) - got invalid chunk data", pluginId);
  1018. }
  1019. else
  1020. carla_stderr2("carla_get_chunk_data(%i) - plugin does not support chunks", pluginId);
  1021. return nullptr;
  1022. }
  1023. carla_stderr2("carla_get_chunk_data(%i) - could not find plugin", pluginId);
  1024. return nullptr;
  1025. }
  1026. // -------------------------------------------------------------------------------------------------------------------
  1027. uint32_t carla_get_parameter_count(unsigned int pluginId)
  1028. {
  1029. carla_debug("carla_get_parameter_count(%i)", pluginId);
  1030. CARLA_ASSERT(standalone.engine != nullptr);
  1031. if (standalone.engine == nullptr)
  1032. return 0;
  1033. if (CarlaPlugin* const plugin = standalone.engine->getPlugin(pluginId))
  1034. return plugin->parameterCount();
  1035. carla_stderr2("carla_get_parameter_count(%i) - could not find plugin", pluginId);
  1036. return 0;
  1037. }
  1038. uint32_t carla_get_program_count(unsigned int pluginId)
  1039. {
  1040. carla_debug("carla_get_program_count(%i)", pluginId);
  1041. CARLA_ASSERT(standalone.engine != nullptr);
  1042. if (standalone.engine == nullptr)
  1043. return 0;
  1044. if (CarlaPlugin* const plugin = standalone.engine->getPlugin(pluginId))
  1045. return plugin->programCount();
  1046. carla_stderr2("carla_get_program_count(%i) - could not find plugin", pluginId);
  1047. return 0;
  1048. }
  1049. uint32_t carla_get_midi_program_count(unsigned int pluginId)
  1050. {
  1051. carla_debug("carla_get_midi_program_count(%i)", pluginId);
  1052. CARLA_ASSERT(standalone.engine != nullptr);
  1053. if (standalone.engine == nullptr)
  1054. return 0;
  1055. if (CarlaPlugin* const plugin = standalone.engine->getPlugin(pluginId))
  1056. return plugin->midiProgramCount();
  1057. carla_stderr2("carla_get_midi_program_count(%i) - could not find plugin", pluginId);
  1058. return 0;
  1059. }
  1060. uint32_t carla_get_custom_data_count(unsigned int pluginId)
  1061. {
  1062. carla_debug("carla_get_custom_data_count(%i)", pluginId);
  1063. CARLA_ASSERT(standalone.engine != nullptr);
  1064. if (standalone.engine == nullptr)
  1065. return 0;
  1066. if (CarlaPlugin* const plugin = standalone.engine->getPlugin(pluginId))
  1067. return plugin->customDataCount();
  1068. carla_stderr2("carla_get_custom_data_count(%i) - could not find plugin", pluginId);
  1069. return 0;
  1070. }
  1071. // -------------------------------------------------------------------------------------------------------------------
  1072. const char* carla_get_parameter_text(unsigned int pluginId, uint32_t parameterId)
  1073. {
  1074. carla_debug("carla_get_parameter_text(%i, %i)", pluginId, parameterId);
  1075. CARLA_ASSERT(standalone.engine != nullptr);
  1076. if (standalone.engine == nullptr)
  1077. return nullptr;
  1078. static char textBuf[STR_MAX+1];
  1079. carla_fill<char>(textBuf, STR_MAX+1, '\0');
  1080. if (CarlaPlugin* const plugin = standalone.engine->getPlugin(pluginId))
  1081. {
  1082. if (parameterId < plugin->parameterCount())
  1083. {
  1084. plugin->getParameterText(parameterId, textBuf);
  1085. return textBuf;
  1086. }
  1087. carla_stderr2("carla_get_parameter_text(%i, %i) - parameterId out of bounds", pluginId, parameterId);
  1088. return nullptr;
  1089. }
  1090. carla_stderr2("carla_get_parameter_text(%i, %i) - could not find plugin", pluginId, parameterId);
  1091. return nullptr;
  1092. }
  1093. const char* carla_get_program_name(unsigned int pluginId, uint32_t programId)
  1094. {
  1095. carla_debug("carla_get_program_name(%i, %i)", pluginId, programId);
  1096. CARLA_ASSERT(standalone.engine != nullptr);
  1097. if (standalone.engine == nullptr)
  1098. return nullptr;
  1099. static char programName[STR_MAX+1];
  1100. carla_fill<char>(programName, STR_MAX+1, '\0');
  1101. if (CarlaPlugin* const plugin = standalone.engine->getPlugin(pluginId))
  1102. {
  1103. if (programId < plugin->programCount())
  1104. {
  1105. plugin->getProgramName(programId, programName);
  1106. return programName;
  1107. }
  1108. carla_stderr2("carla_get_program_name(%i, %i) - programId out of bounds", pluginId, programId);
  1109. return nullptr;
  1110. }
  1111. carla_stderr2("carla_get_program_name(%i, %i) - could not find plugin", pluginId, programId);
  1112. return nullptr;
  1113. }
  1114. const char* carla_get_midi_program_name(unsigned int pluginId, uint32_t midiProgramId)
  1115. {
  1116. carla_debug("carla_get_midi_program_name(%i, %i)", pluginId, midiProgramId);
  1117. CARLA_ASSERT(standalone.engine != nullptr);
  1118. if (standalone.engine == nullptr)
  1119. return nullptr;
  1120. static char midiProgramName[STR_MAX+1];
  1121. carla_fill<char>(midiProgramName, STR_MAX+1, '\0');
  1122. if (CarlaPlugin* const plugin = standalone.engine->getPlugin(pluginId))
  1123. {
  1124. if (midiProgramId < plugin->midiProgramCount())
  1125. {
  1126. plugin->getMidiProgramName(midiProgramId, midiProgramName);
  1127. return midiProgramName;
  1128. }
  1129. carla_stderr2("carla_get_midi_program_name(%i, %i) - midiProgramId out of bounds", pluginId, midiProgramId);
  1130. return nullptr;
  1131. }
  1132. carla_stderr2("carla_get_midi_program_name(%i, %i) - could not find plugin", pluginId, midiProgramId);
  1133. return nullptr;
  1134. }
  1135. const char* carla_get_real_plugin_name(unsigned int pluginId)
  1136. {
  1137. carla_debug("carla_get_real_plugin_name(%i)", pluginId);
  1138. CARLA_ASSERT(standalone.engine != nullptr);
  1139. if (standalone.engine == nullptr)
  1140. return nullptr;
  1141. static char realPluginName[STR_MAX+1];
  1142. carla_fill<char>(realPluginName, STR_MAX+1, '\0');
  1143. if (CarlaPlugin* const plugin = standalone.engine->getPlugin(pluginId))
  1144. {
  1145. plugin->getRealName(realPluginName);
  1146. return realPluginName;
  1147. }
  1148. carla_stderr2("carla_get_real_plugin_name(%i) - could not find plugin", pluginId);
  1149. return nullptr;
  1150. }
  1151. // -------------------------------------------------------------------------------------------------------------------
  1152. int32_t carla_get_current_program_index(unsigned int pluginId)
  1153. {
  1154. carla_debug("carla_get_current_program_index(%i)", pluginId);
  1155. CARLA_ASSERT(standalone.engine != nullptr);
  1156. if (standalone.engine == nullptr)
  1157. return -1;
  1158. if (CarlaPlugin* const plugin = standalone.engine->getPlugin(pluginId))
  1159. return plugin->currentProgram();
  1160. carla_stderr2("carla_get_current_program_index(%i) - could not find plugin", pluginId);
  1161. return -1;
  1162. }
  1163. int32_t carla_get_current_midi_program_index(unsigned int pluginId)
  1164. {
  1165. carla_debug("carla_get_current_midi_program_index(%i)", pluginId);
  1166. CARLA_ASSERT(standalone.engine != nullptr);
  1167. if (standalone.engine == nullptr)
  1168. return -1;
  1169. if (CarlaPlugin* const plugin = standalone.engine->getPlugin(pluginId))
  1170. return plugin->currentMidiProgram();
  1171. carla_stderr2("carla_get_current_midi_program_index(%i) - could not find plugin", pluginId);
  1172. return -1;
  1173. }
  1174. // -------------------------------------------------------------------------------------------------------------------
  1175. float carla_get_default_parameter_value(unsigned int pluginId, uint32_t parameterId)
  1176. {
  1177. carla_debug("carla_get_default_parameter_value(%i, %i)", pluginId, parameterId);
  1178. CARLA_ASSERT(standalone.engine != nullptr);
  1179. if (standalone.engine == nullptr)
  1180. return 0.0f;
  1181. if (CarlaPlugin* const plugin = standalone.engine->getPlugin(pluginId))
  1182. {
  1183. if (parameterId < plugin->parameterCount())
  1184. return plugin->parameterRanges(parameterId).def;
  1185. carla_stderr2("carla_get_default_parameter_value(%i, %i) - parameterId out of bounds", pluginId, parameterId);
  1186. return 0.0f;
  1187. }
  1188. carla_stderr2("carla_get_default_parameter_value(%i, %i) - could not find plugin", pluginId, parameterId);
  1189. return 0.0f;
  1190. }
  1191. float carla_get_current_parameter_value(unsigned int pluginId, uint32_t parameterId)
  1192. {
  1193. carla_debug("carla_get_current_parameter_value(%i, %i)", pluginId, parameterId);
  1194. CARLA_ASSERT(standalone.engine != nullptr);
  1195. if (standalone.engine == nullptr)
  1196. return 0.0f;
  1197. if (CarlaPlugin* const plugin = standalone.engine->getPlugin(pluginId))
  1198. {
  1199. if (parameterId < plugin->parameterCount())
  1200. return plugin->getParameterValue(parameterId);
  1201. carla_stderr2("carla_get_current_parameter_value(%i, %i) - parameterId out of bounds", pluginId, parameterId);
  1202. return 0.0f;
  1203. }
  1204. carla_stderr2("carla_get_current_parameter_value(%i, %i) - could not find plugin", pluginId, parameterId);
  1205. return 0.0f;
  1206. }
  1207. // -------------------------------------------------------------------------------------------------------------------
  1208. float carla_get_input_peak_value(unsigned int pluginId, unsigned short portId)
  1209. {
  1210. CARLA_ASSERT(standalone.engine != nullptr);
  1211. CARLA_ASSERT(portId == 1 || portId == 2);
  1212. if (standalone.engine == nullptr)
  1213. return 0.0f;
  1214. return standalone.engine->getInputPeak(pluginId, portId);
  1215. }
  1216. float carla_get_output_peak_value(unsigned int pluginId, unsigned short portId)
  1217. {
  1218. CARLA_ASSERT(standalone.engine != nullptr);
  1219. CARLA_ASSERT(portId == 1 || portId == 2);
  1220. if (standalone.engine == nullptr)
  1221. return 0.0f;
  1222. return standalone.engine->getOutputPeak(pluginId, portId);
  1223. }
  1224. // -------------------------------------------------------------------------------------------------------------------
  1225. void carla_set_option(unsigned int pluginId, unsigned int option, bool yesNo)
  1226. {
  1227. carla_debug("carla_set_option(%i, %i, %s)", pluginId, option, bool2str(yesNo));
  1228. CARLA_ASSERT(standalone.engine != nullptr);
  1229. if (standalone.engine == nullptr)
  1230. return;
  1231. if (CarlaPlugin* const plugin = standalone.engine->getPlugin(pluginId))
  1232. return plugin->setOption(option, yesNo);
  1233. carla_stderr2("carla_set_option(%i, %i, %s) - could not find plugin", pluginId, option, bool2str(yesNo));
  1234. }
  1235. void carla_set_active(unsigned int pluginId, bool onOff)
  1236. {
  1237. carla_debug("carla_set_active(%i, %s)", pluginId, bool2str(onOff));
  1238. CARLA_ASSERT(standalone.engine != nullptr);
  1239. if (standalone.engine == nullptr)
  1240. return;
  1241. if (CarlaPlugin* const plugin = standalone.engine->getPlugin(pluginId))
  1242. return plugin->setActive(onOff, true, false);
  1243. carla_stderr2("carla_set_active(%i, %s) - could not find plugin", pluginId, bool2str(onOff));
  1244. }
  1245. void carla_set_drywet(unsigned int pluginId, float value)
  1246. {
  1247. carla_debug("carla_set_drywet(%i, %f)", pluginId, value);
  1248. CARLA_ASSERT(standalone.engine != nullptr);
  1249. if (standalone.engine == nullptr)
  1250. return;
  1251. if (CarlaPlugin* const plugin = standalone.engine->getPlugin(pluginId))
  1252. return plugin->setDryWet(value, true, false);
  1253. carla_stderr2("carla_set_drywet(%i, %f) - could not find plugin", pluginId, value);
  1254. }
  1255. void carla_set_volume(unsigned int pluginId, float value)
  1256. {
  1257. carla_debug("carla_set_volume(%i, %f)", pluginId, value);
  1258. CARLA_ASSERT(standalone.engine != nullptr);
  1259. if (standalone.engine == nullptr)
  1260. return;
  1261. if (CarlaPlugin* const plugin = standalone.engine->getPlugin(pluginId))
  1262. return plugin->setVolume(value, true, false);
  1263. carla_stderr2("carla_set_volume(%i, %f) - could not find plugin", pluginId, value);
  1264. }
  1265. void carla_set_balance_left(unsigned int pluginId, float value)
  1266. {
  1267. carla_debug("carla_set_balance_left(%i, %f)", pluginId, value);
  1268. CARLA_ASSERT(standalone.engine != nullptr);
  1269. if (standalone.engine == nullptr)
  1270. return;
  1271. if (CarlaPlugin* const plugin = standalone.engine->getPlugin(pluginId))
  1272. return plugin->setBalanceLeft(value, true, false);
  1273. carla_stderr2("carla_set_balance_left(%i, %f) - could not find plugin", pluginId, value);
  1274. }
  1275. void carla_set_balance_right(unsigned int pluginId, float value)
  1276. {
  1277. carla_debug("carla_set_balance_right(%i, %f)", pluginId, value);
  1278. CARLA_ASSERT(standalone.engine != nullptr);
  1279. if (standalone.engine == nullptr)
  1280. return;
  1281. if (CarlaPlugin* const plugin = standalone.engine->getPlugin(pluginId))
  1282. return plugin->setBalanceRight(value, true, false);
  1283. carla_stderr2("carla_set_balance_right(%i, %f) - could not find plugin", pluginId, value);
  1284. }
  1285. void carla_set_panning(unsigned int pluginId, float value)
  1286. {
  1287. carla_debug("carla_set_panning(%i, %f)", pluginId, value);
  1288. CARLA_ASSERT(standalone.engine != nullptr);
  1289. if (standalone.engine == nullptr)
  1290. return;
  1291. if (CarlaPlugin* const plugin = standalone.engine->getPlugin(pluginId))
  1292. return plugin->setPanning(value, true, false);
  1293. carla_stderr2("carla_set_panning(%i, %f) - could not find plugin", pluginId, value);
  1294. }
  1295. void carla_set_ctrl_channel(unsigned int pluginId, int8_t channel)
  1296. {
  1297. carla_debug("carla_set_ctrl_channel(%i, %i)", pluginId, channel);
  1298. CARLA_ASSERT(standalone.engine != nullptr);
  1299. if (standalone.engine == nullptr)
  1300. return;
  1301. if (CarlaPlugin* const plugin = standalone.engine->getPlugin(pluginId))
  1302. return plugin->setCtrlChannel(channel, true, false);
  1303. carla_stderr2("carla_set_ctrl_channel(%i, %i) - could not find plugin", pluginId, channel);
  1304. }
  1305. // -------------------------------------------------------------------------------------------------------------------
  1306. void carla_set_parameter_value(unsigned int pluginId, uint32_t parameterId, float value)
  1307. {
  1308. carla_debug("carla_set_parameter_value(%i, %i, %f)", pluginId, parameterId, value);
  1309. CARLA_ASSERT(standalone.engine != nullptr);
  1310. if (standalone.engine == nullptr)
  1311. return;
  1312. if (CarlaPlugin* const plugin = standalone.engine->getPlugin(pluginId))
  1313. {
  1314. if (parameterId < plugin->parameterCount())
  1315. return plugin->setParameterValue(parameterId, value, true, true, false);
  1316. carla_stderr2("carla_set_parameter_value(%i, %i, %f) - parameterId out of bounds", pluginId, parameterId, value);
  1317. return;
  1318. }
  1319. carla_stderr2("carla_set_parameter_value(%i, %i, %f) - could not find plugin", pluginId, parameterId, value);
  1320. }
  1321. void carla_set_parameter_midi_channel(unsigned int pluginId, uint32_t parameterId, uint8_t channel)
  1322. {
  1323. carla_debug("carla_set_parameter_midi_channel(%i, %i, %i)", pluginId, parameterId, channel);
  1324. CARLA_ASSERT(standalone.engine != nullptr);
  1325. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  1326. if (channel >= MAX_MIDI_CHANNELS)
  1327. {
  1328. carla_stderr2("carla_set_parameter_midi_channel(%i, %i, %i) - invalid channel number", pluginId, parameterId, channel);
  1329. return;
  1330. }
  1331. if (standalone.engine == nullptr)
  1332. return;
  1333. if (CarlaPlugin* const plugin = standalone.engine->getPlugin(pluginId))
  1334. {
  1335. if (parameterId < plugin->parameterCount())
  1336. return plugin->setParameterMidiChannel(parameterId, channel, true, false);
  1337. carla_stderr2("carla_set_parameter_midi_channel(%i, %i, %i) - parameterId out of bounds", pluginId, parameterId, channel);
  1338. return;
  1339. }
  1340. carla_stderr2("carla_set_parameter_midi_channel(%i, %i, %i) - could not find plugin", pluginId, parameterId, channel);
  1341. }
  1342. void carla_set_parameter_midi_cc(unsigned int pluginId, uint32_t parameterId, int16_t cc)
  1343. {
  1344. carla_debug("carla_set_parameter_midi_cc(%i, %i, %i)", pluginId, parameterId, cc);
  1345. CARLA_ASSERT(standalone.engine != nullptr);
  1346. CARLA_ASSERT(cc >= -1 && cc <= 0x5F);
  1347. if (cc < -1)
  1348. {
  1349. cc = -1;
  1350. }
  1351. else if (cc > 0x5F) // 95
  1352. {
  1353. carla_stderr2("carla_set_parameter_midi_cc(%i, %i, %i) - invalid cc number", pluginId, parameterId, cc);
  1354. return;
  1355. }
  1356. if (standalone.engine == nullptr)
  1357. return;
  1358. if (CarlaPlugin* const plugin = standalone.engine->getPlugin(pluginId))
  1359. {
  1360. if (parameterId < plugin->parameterCount())
  1361. return plugin->setParameterMidiCC(parameterId, cc, true, false);
  1362. carla_stderr2("carla_set_parameter_midi_cc(%i, %i, %i) - parameterId out of bounds", pluginId, parameterId, cc);
  1363. return;
  1364. }
  1365. carla_stderr2("carla_set_parameter_midi_cc(%i, %i, %i) - could not find plugin", pluginId, parameterId, cc);
  1366. }
  1367. // -------------------------------------------------------------------------------------------------------------------
  1368. void carla_set_program(unsigned int pluginId, uint32_t programId)
  1369. {
  1370. carla_debug("carla_set_program(%i, %i)", pluginId, programId);
  1371. CARLA_ASSERT(standalone.engine != nullptr);
  1372. if (standalone.engine == nullptr)
  1373. return;
  1374. if (CarlaPlugin* const plugin = standalone.engine->getPlugin(pluginId))
  1375. {
  1376. if (programId < plugin->programCount())
  1377. return plugin->setProgram(static_cast<int32_t>(programId), true, true, false);
  1378. carla_stderr2("carla_set_program(%i, %i) - programId out of bounds", pluginId, programId);
  1379. return;
  1380. }
  1381. carla_stderr2("carla_set_program(%i, %i) - could not find plugin", pluginId, programId);
  1382. }
  1383. void carla_set_midi_program(unsigned int pluginId, uint32_t midiProgramId)
  1384. {
  1385. carla_debug("carla_set_midi_program(%i, %i)", pluginId, midiProgramId);
  1386. CARLA_ASSERT(standalone.engine != nullptr);
  1387. if (standalone.engine == nullptr)
  1388. return;
  1389. if (CarlaPlugin* const plugin = standalone.engine->getPlugin(pluginId))
  1390. {
  1391. if (midiProgramId < plugin->midiProgramCount())
  1392. return plugin->setMidiProgram(static_cast<int32_t>(midiProgramId), true, true, false);
  1393. carla_stderr2("carla_set_midi_program(%i, %i) - midiProgramId out of bounds", pluginId, midiProgramId);
  1394. return;
  1395. }
  1396. carla_stderr2("carla_set_midi_program(%i, %i) - could not find plugin", pluginId, midiProgramId);
  1397. }
  1398. // -------------------------------------------------------------------------------------------------------------------
  1399. void carla_set_custom_data(unsigned int pluginId, const char* type, const char* key, const char* value)
  1400. {
  1401. carla_debug("carla_set_custom_data(%i, \"%s\", \"%s\", \"%s\")", pluginId, type, key, value);
  1402. CARLA_ASSERT(standalone.engine != nullptr);
  1403. if (standalone.engine == nullptr)
  1404. return;
  1405. if (CarlaPlugin* const plugin = standalone.engine->getPlugin(pluginId))
  1406. return plugin->setCustomData(type, key, value, true);
  1407. carla_stderr2("carla_set_custom_data(%i, \"%s\", \"%s\", \"%s\") - could not find plugin", pluginId, type, key, value);
  1408. }
  1409. void carla_set_chunk_data(unsigned int pluginId, const char* chunkData)
  1410. {
  1411. carla_debug("carla_set_chunk_data(%i, \"%s\")", pluginId, chunkData);
  1412. CARLA_ASSERT(standalone.engine != nullptr);
  1413. if (standalone.engine == nullptr)
  1414. return;
  1415. if (CarlaPlugin* const plugin = standalone.engine->getPlugin(pluginId))
  1416. {
  1417. if (plugin->options() & CarlaBackend::PLUGIN_OPTION_USE_CHUNKS)
  1418. return plugin->setChunkData(chunkData);
  1419. carla_stderr2("carla_set_chunk_data(%i, \"%s\") - plugin does not support chunks", pluginId, chunkData);
  1420. return;
  1421. }
  1422. carla_stderr2("carla_set_chunk_data(%i, \"%s\") - could not find plugin", pluginId, chunkData);
  1423. }
  1424. // -------------------------------------------------------------------------------------------------------------------
  1425. void carla_prepare_for_save(unsigned int pluginId)
  1426. {
  1427. carla_debug("carla_prepare_for_save(%i)", pluginId);
  1428. CARLA_ASSERT(standalone.engine != nullptr);
  1429. if (standalone.engine == nullptr)
  1430. return;
  1431. if (CarlaPlugin* const plugin = standalone.engine->getPlugin(pluginId))
  1432. return plugin->prepareForSave();
  1433. carla_stderr2("carla_prepare_for_save(%i) - could not find plugin", pluginId);
  1434. }
  1435. void carla_send_midi_note(unsigned int pluginId, uint8_t channel, uint8_t note, uint8_t velocity)
  1436. {
  1437. carla_debug("carla_send_midi_note(%i, %i, %i, %i)", pluginId, channel, note, velocity);
  1438. CARLA_ASSERT(standalone.engine != nullptr);
  1439. if (standalone.engine == nullptr || ! standalone.engine->isRunning())
  1440. return;
  1441. if (CarlaPlugin* const plugin = standalone.engine->getPlugin(pluginId))
  1442. return plugin->sendMidiSingleNote(channel, note, velocity, true, true, false);
  1443. carla_stderr2("carla_send_midi_note(%i, %i, %i, %i) - could not find plugin", pluginId, channel, note, velocity);
  1444. }
  1445. void carla_show_gui(unsigned int pluginId, bool yesno)
  1446. {
  1447. carla_debug("carla_show_gui(%i, %s)", pluginId, bool2str(yesno));
  1448. CARLA_ASSERT(standalone.engine != nullptr);
  1449. if (standalone.engine == nullptr)
  1450. return;
  1451. if (CarlaPlugin* const plugin = standalone.engine->getPlugin(pluginId))
  1452. return plugin->showGui(yesno);
  1453. carla_stderr2("carla_show_gui(%i, %s) - could not find plugin", pluginId, bool2str(yesno));
  1454. }
  1455. // -------------------------------------------------------------------------------------------------------------------
  1456. uint32_t carla_get_buffer_size()
  1457. {
  1458. carla_debug("carla_get_buffer_size()");
  1459. CARLA_ASSERT(standalone.engine != nullptr);
  1460. if (standalone.engine == nullptr)
  1461. return 0;
  1462. return standalone.engine->getBufferSize();
  1463. }
  1464. double carla_get_sample_rate()
  1465. {
  1466. carla_debug("carla_get_sample_rate()");
  1467. CARLA_ASSERT(standalone.engine != nullptr);
  1468. if (standalone.engine == nullptr)
  1469. return 0.0;
  1470. return standalone.engine->getSampleRate();
  1471. }
  1472. // -------------------------------------------------------------------------------------------------------------------
  1473. const char* carla_get_last_error()
  1474. {
  1475. carla_debug("carla_get_last_error()");
  1476. if (standalone.engine != nullptr)
  1477. return standalone.engine->getLastError();
  1478. return standalone.lastError;
  1479. }
  1480. const char* carla_get_host_osc_url()
  1481. {
  1482. carla_debug("carla_get_host_osc_url()");
  1483. CARLA_ASSERT(standalone.engine != nullptr);
  1484. if (standalone.engine == nullptr)
  1485. {
  1486. standalone.lastError = "Engine is not started";
  1487. return nullptr;
  1488. }
  1489. return standalone.engine->getOscServerPathTCP();
  1490. }
  1491. // -------------------------------------------------------------------------------------------------------------------
  1492. #define NSM_API_VERSION_MAJOR 1
  1493. #define NSM_API_VERSION_MINOR 2
  1494. class CarlaNSM
  1495. {
  1496. public:
  1497. CarlaNSM()
  1498. : fServerThread(nullptr),
  1499. fReplyAddr(nullptr),
  1500. fIsOpened(false),
  1501. fIsSaved(false)
  1502. {
  1503. }
  1504. ~CarlaNSM()
  1505. {
  1506. if (fReplyAddr != nullptr)
  1507. lo_address_free(fReplyAddr);
  1508. if (fServerThread != nullptr)
  1509. {
  1510. lo_server_thread_stop(fServerThread);
  1511. lo_server_thread_del_method(fServerThread, "/reply", "ssss");
  1512. lo_server_thread_del_method(fServerThread, "/nsm/client/open", "sss");
  1513. lo_server_thread_del_method(fServerThread, "/nsm/client/save", "");
  1514. lo_server_thread_free(fServerThread);
  1515. }
  1516. }
  1517. void announce(const char* const url, const int pid)
  1518. {
  1519. lo_address const addr = lo_address_new_from_url(url);
  1520. if (addr == nullptr)
  1521. return;
  1522. const int proto = lo_address_get_protocol(addr);
  1523. if (fServerThread == nullptr)
  1524. {
  1525. // create new OSC thread
  1526. fServerThread = lo_server_thread_new_with_proto(nullptr, proto, error_handler);
  1527. // register message handlers and start OSC thread
  1528. lo_server_thread_add_method(fServerThread, "/reply", "ssss", _reply_handler, this);
  1529. lo_server_thread_add_method(fServerThread, "/nsm/client/open", "sss", _open_handler, this);
  1530. lo_server_thread_add_method(fServerThread, "/nsm/client/save", "", _save_handler, this);
  1531. lo_server_thread_start(fServerThread);
  1532. }
  1533. lo_send_from(addr, lo_server_thread_get_server(fServerThread), LO_TT_IMMEDIATE, "/nsm/server/announce", "sssiii",
  1534. "Carla", ":switch:", "carla", NSM_API_VERSION_MAJOR, NSM_API_VERSION_MINOR, pid);
  1535. lo_address_free(addr);
  1536. }
  1537. void replyOpen()
  1538. {
  1539. fIsOpened = true;
  1540. }
  1541. void replySave()
  1542. {
  1543. fIsSaved = true;
  1544. }
  1545. protected:
  1546. int handleReply(const char* const path, const char* const types, lo_arg** const argv, const int argc, const lo_message msg)
  1547. {
  1548. carla_debug("CarlaNSM::handleReply(%s, %i, %p, %s, %p)", path, argc, argv, types, msg);
  1549. if (fReplyAddr != nullptr)
  1550. lo_address_free(fReplyAddr);
  1551. fIsOpened = false;
  1552. fIsSaved = false;
  1553. char* const url = lo_address_get_url(lo_message_get_source(msg));
  1554. fReplyAddr = lo_address_new_from_url(url);
  1555. std::free(url);
  1556. const char* const method = &argv[0]->s;
  1557. const char* const smName = &argv[2]->s;
  1558. if (std::strcmp(method, "/nsm/server/announce") == 0 && standalone.callback != nullptr)
  1559. standalone.callback(standalone.callbackPtr, CarlaBackend::CALLBACK_NSM_ANNOUNCE, 0, 0, 0, 0.0f, smName);
  1560. return 0;
  1561. #ifndef DEBUG
  1562. // unused
  1563. (void)path;
  1564. (void)types;
  1565. (void)argc;
  1566. #endif
  1567. }
  1568. int handleOpen(const char* const path, const char* const types, lo_arg** const argv, const int argc, const lo_message msg)
  1569. {
  1570. carla_debug("CarlaNSM::handleOpen(\"%s\", \"%s\", %p, %i, %p)", path, types, argv, argc, msg);
  1571. if (standalone.callback == nullptr)
  1572. return 1;
  1573. if (fServerThread == nullptr)
  1574. return 1;
  1575. if (fReplyAddr == nullptr)
  1576. return 1;
  1577. const char* const projectPath = &argv[0]->s;
  1578. const char* const clientId = &argv[2]->s;
  1579. char data[std::strlen(projectPath)+std::strlen(clientId)+2];
  1580. std::strcpy(data, projectPath);
  1581. std::strcat(data, ":");
  1582. std::strcat(data, clientId);
  1583. standalone.callback(nullptr, CarlaBackend::CALLBACK_NSM_OPEN, 0, 0, 0, 0.0f, data);
  1584. for (int i=0; i < 30 && ! fIsOpened; i++)
  1585. carla_msleep(100);
  1586. if (fIsOpened)
  1587. lo_send_from(fReplyAddr, lo_server_thread_get_server(fServerThread), LO_TT_IMMEDIATE, "/reply", "ss", "/nsm/client/open", "OK");
  1588. return 0;
  1589. #ifndef DEBUG
  1590. // unused
  1591. (void)path;
  1592. (void)types;
  1593. (void)argc;
  1594. (void)msg;
  1595. #endif
  1596. }
  1597. int handleSave(const char* const path, const char* const types, lo_arg** const argv, const int argc, const lo_message msg)
  1598. {
  1599. carla_debug("CarlaNSM::handleSave(\"%s\", \"%s\", %p, %i, %p)", path, types, argv, argc, msg);
  1600. if (standalone.callback == nullptr)
  1601. return 1;
  1602. if (fServerThread == nullptr)
  1603. return 1;
  1604. if (fReplyAddr == nullptr)
  1605. return 1;
  1606. standalone.callback(nullptr, CarlaBackend::CALLBACK_NSM_SAVE, 0, 0, 0, 0.0f, nullptr);
  1607. for (int i=0; i < 30 && ! fIsSaved; i++)
  1608. carla_msleep(100);
  1609. if (fIsSaved)
  1610. lo_send_from(fReplyAddr, lo_server_thread_get_server(fServerThread), LO_TT_IMMEDIATE, "/reply", "ss", "/nsm/client/save", "OK");
  1611. return 0;
  1612. #ifndef DEBUG
  1613. // unused
  1614. (void)path;
  1615. (void)types;
  1616. (void)argv;
  1617. (void)argc;
  1618. (void)msg;
  1619. #endif
  1620. }
  1621. private:
  1622. lo_server_thread fServerThread;
  1623. lo_address fReplyAddr;
  1624. bool fIsOpened;
  1625. bool fIsSaved;
  1626. #define handlePtr ((CarlaNSM*)data)
  1627. static int _reply_handler(const char* path, const char* types, lo_arg** argv, int argc, lo_message msg, void* data)
  1628. {
  1629. return handlePtr->handleReply(path, types, argv, argc, msg);
  1630. }
  1631. static int _open_handler(const char* path, const char* types, lo_arg** argv, int argc, lo_message msg, void* data)
  1632. {
  1633. return handlePtr->handleOpen(path, types, argv, argc, msg);
  1634. }
  1635. static int _save_handler(const char* path, const char* types, lo_arg** argv, int argc, lo_message msg, void* data)
  1636. {
  1637. return handlePtr->handleSave(path, types, argv, argc, msg);
  1638. }
  1639. #undef handlePtr
  1640. static void error_handler(int num, const char* msg, const char* path)
  1641. {
  1642. carla_stderr2("CarlaNSM::error_handler(%i, \"%s\", \"%s\")", num, msg, path);
  1643. }
  1644. };
  1645. static CarlaNSM gCarlaNSM;
  1646. void carla_nsm_announce(const char* url, int pid)
  1647. {
  1648. gCarlaNSM.announce(url, pid);
  1649. }
  1650. void carla_nsm_reply_open()
  1651. {
  1652. gCarlaNSM.replyOpen();
  1653. }
  1654. void carla_nsm_reply_save()
  1655. {
  1656. gCarlaNSM.replySave();
  1657. }
  1658. // -------------------------------------------------------------------------------------------------------------------
  1659. #ifdef BUILD_BRIDGE
  1660. CarlaEngine* carla_get_standalone_engine()
  1661. {
  1662. return standalone.engine;
  1663. }
  1664. bool carla_engine_init_bridge(const char* audioBaseName, const char* controlBaseName, const char* clientName)
  1665. {
  1666. carla_debug("carla_engine_init_bridge(\"%s\", \"%s\", \"%s\")", audioBaseName, controlBaseName, clientName);
  1667. CARLA_ASSERT(standalone.engine == nullptr);
  1668. CARLA_ASSERT(audioBaseName != nullptr);
  1669. CARLA_ASSERT(controlBaseName != nullptr);
  1670. CARLA_ASSERT(clientName != nullptr);
  1671. standalone.engine = CarlaEngine::newBridge(audioBaseName, controlBaseName);
  1672. if (standalone.engine == nullptr)
  1673. {
  1674. standalone.lastError = "The seleted audio driver is not available!";
  1675. return false;
  1676. }
  1677. if (standalone.callback != nullptr)
  1678. standalone.engine->setCallback(standalone.callback, nullptr);
  1679. standalone.engine->setOption(CarlaBackend::OPTION_PROCESS_MODE, CarlaBackend::PROCESS_MODE_BRIDGE, nullptr);
  1680. standalone.engine->setOption(CarlaBackend::OPTION_TRANSPORT_MODE, CarlaBackend::TRANSPORT_MODE_BRIDGE, nullptr);
  1681. standalone.engine->setOption(CarlaBackend::OPTION_PREFER_PLUGIN_BRIDGES, false, nullptr);
  1682. standalone.engine->setOption(CarlaBackend::OPTION_PREFER_UI_BRIDGES, false, nullptr);
  1683. // TODO - read from environment
  1684. #ifndef BUILD_BRIDGE
  1685. standalone.engine->setOption(CarlaBackend::OPTION_FORCE_STEREO, standalone.options.forceStereo ? 1 : 0, nullptr);
  1686. # ifdef WANT_DSSI
  1687. standalone.engine->setOption(CarlaBackend::OPTION_USE_DSSI_VST_CHUNKS, standalone.options.useDssiVstChunks ? 1 : 0, nullptr);
  1688. # endif
  1689. standalone.engine->setOption(CarlaBackend::OPTION_MAX_PARAMETERS, static_cast<int>(standalone.options.maxParameters), nullptr);
  1690. #endif
  1691. const bool initiated = standalone.engine->init(clientName);
  1692. if (initiated)
  1693. {
  1694. standalone.lastError = "no error";
  1695. standalone.init();
  1696. }
  1697. else
  1698. {
  1699. standalone.lastError = standalone.engine->getLastError();
  1700. delete standalone.engine;
  1701. standalone.engine = nullptr;
  1702. }
  1703. return initiated;
  1704. }
  1705. #endif