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.

2296 lines
72KB

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