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.

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