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.

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