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.

2381 lines
75KB

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