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.

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