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.

2356 lines
74KB

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