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.

2315 lines
73KB

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