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.

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