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.

2422 lines
76KB

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