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.

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