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.

2249 lines
73KB

  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 doc/GPL.txt file.
  16. */
  17. // TODO:
  18. // Check carla_stderr2("Engine is not running"); <= prepend func name and args
  19. #include "CarlaHost.h"
  20. #include "CarlaMIDI.h"
  21. #include "CarlaNative.h"
  22. #include "CarlaEngine.hpp"
  23. #include "CarlaPlugin.hpp"
  24. #include "CarlaBackendUtils.hpp"
  25. #include "CarlaThread.hpp"
  26. #include <QtCore/QByteArray>
  27. #ifdef BUILD_BRIDGE
  28. # undef HAVE_JUCE
  29. #endif
  30. #ifdef HAVE_JUCE
  31. # include "juce_gui_basics.h"
  32. using juce::initialiseJuce_GUI;
  33. using juce::shutdownJuce_GUI;
  34. using juce::MessageManager;
  35. #endif
  36. namespace CB = CarlaBackend;
  37. using CB::EngineOptions;
  38. #if defined(HAVE_JUCE) && defined(CARLA_OS_LINUX)
  39. using juce::Thread;
  40. // -----------------------------------------------------------------------
  41. // Juce Message Thread
  42. class JuceMessageThread : public Thread
  43. {
  44. public:
  45. JuceMessageThread()
  46. : Thread("JuceMessageThread"),
  47. fInitialised(false)
  48. {
  49. }
  50. ~JuceMessageThread() override
  51. {
  52. stop();
  53. }
  54. void start()
  55. {
  56. CARLA_SAFE_ASSERT_RETURN(! fInitialised,);
  57. fInitialised = false;
  58. startThread();
  59. while (! fInitialised)
  60. carla_msleep(1);
  61. }
  62. void stop()
  63. {
  64. if (! fInitialised)
  65. return;
  66. stopThread(-1);
  67. }
  68. protected:
  69. void run() override
  70. {
  71. fInitialised = true;
  72. if (MessageManager* const msgMgr = MessageManager::getInstance())
  73. {
  74. msgMgr->setCurrentThreadAsMessageThread();
  75. for (; (! threadShouldExit()) && msgMgr->runDispatchLoopUntil(250);) {}
  76. }
  77. fInitialised = false;
  78. }
  79. private:
  80. volatile bool fInitialised;
  81. };
  82. #endif
  83. // -------------------------------------------------------------------------------------------------------------------
  84. // Single, standalone engine
  85. struct CarlaBackendStandalone {
  86. CarlaEngine* engine;
  87. EngineCallbackFunc engineCallback;
  88. void* engineCallbackPtr;
  89. EngineOptions engineOptions;
  90. FileCallbackFunc fileCallback;
  91. void* fileCallbackPtr;
  92. CarlaString lastError;
  93. #if defined(HAVE_JUCE) && defined(CARLA_OS_LINUX)
  94. JuceMessageThread juceMsgThread;
  95. #endif
  96. CarlaBackendStandalone()
  97. : engine(nullptr),
  98. engineCallback(nullptr),
  99. engineCallbackPtr(nullptr),
  100. fileCallback(nullptr),
  101. fileCallbackPtr(nullptr)
  102. {
  103. #ifdef BUILD_BRIDGE
  104. engineOptions.processMode = CB::ENGINE_PROCESS_MODE_BRIDGE;
  105. engineOptions.transportMode = CB::ENGINE_TRANSPORT_MODE_BRIDGE;
  106. engineOptions.forceStereo = false;
  107. engineOptions.preferPluginBridges = false;
  108. engineOptions.preferUiBridges = false;
  109. #endif
  110. }
  111. ~CarlaBackendStandalone()
  112. {
  113. CARLA_ASSERT(engine == nullptr);
  114. #ifdef HAVE_JUCE
  115. CARLA_ASSERT(MessageManager::getInstanceWithoutCreating() == nullptr);
  116. #endif
  117. }
  118. #ifdef HAVE_JUCE
  119. void init()
  120. {
  121. JUCE_AUTORELEASEPOOL
  122. {
  123. initialiseJuce_GUI();
  124. # ifdef CARLA_OS_LINUX
  125. juceMsgThread.start();
  126. # else
  127. if (MessageManager* const msgMgr = MessageManager::getInstance())
  128. msgMgr->setCurrentThreadAsMessageThread();
  129. # endif
  130. }
  131. }
  132. void close()
  133. {
  134. JUCE_AUTORELEASEPOOL
  135. {
  136. # ifdef CARLA_OS_LINUX
  137. juceMsgThread.stop();
  138. # else
  139. MessageManager::deleteInstance();
  140. # endif
  141. shutdownJuce_GUI();
  142. }
  143. }
  144. # ifndef CARLA_OS_LINUX
  145. void idle()
  146. {
  147. JUCE_AUTORELEASEPOOL
  148. {
  149. if (MessageManager* const msgMgr = MessageManager::getInstanceWithoutCreating())
  150. msgMgr->runDispatchLoopUntil(5);
  151. }
  152. }
  153. # endif
  154. #endif
  155. CARLA_DECLARE_NON_COPY_STRUCT(CarlaBackendStandalone)
  156. };
  157. static CarlaBackendStandalone gStandalone;
  158. // -------------------------------------------------------------------------------------------------------------------
  159. // API
  160. const char* carla_get_complete_license_text()
  161. {
  162. carla_debug("carla_get_complete_license_text()");
  163. static CarlaString retText;
  164. if (retText.isEmpty())
  165. {
  166. CarlaString text1, text2, text3, text4, text5;
  167. text1 += "<p>This current Carla build is using the following features and 3rd-party code:</p>";
  168. text1 += "<ul>";
  169. // Plugin formats
  170. #ifdef WANT_LADSPA
  171. text2 += "<li>LADSPA plugin support, http://www.ladspa.org/</li>";
  172. #endif
  173. #ifdef WANT_DSSI
  174. text2 += "<li>DSSI plugin support, http://dssi.sourceforge.net/</li>";
  175. #endif
  176. #ifdef WANT_LV2
  177. text2 += "<li>LV2 plugin support, http://lv2plug.in/</li>";
  178. #endif
  179. #ifdef WANT_VST
  180. # ifdef VESTIGE_HEADER
  181. text2 += "<li>VST plugin support, using VeSTige header by Javier Serrano Polo</li>";
  182. # else
  183. text2 += "<li>VST plugin support, using official VST SDK 2.4 (trademark of Steinberg Media Technologies GmbH)</li>";
  184. # endif
  185. #endif
  186. #ifdef WANT_AU
  187. text2 += "<li>AU plugin support</li>"; // FIXME
  188. #endif
  189. // Files
  190. #ifdef WANT_CSOUND
  191. text2 += "<li>CSound library for csd support</li>"; // FIXME
  192. #endif
  193. // Sample kit libraries
  194. #ifdef WANT_FLUIDSYNTH
  195. text2 += "<li>FluidSynth library for SF2 support, http://www.fluidsynth.org/</li>";
  196. #endif
  197. #ifdef WANT_LINUXSAMPLER
  198. text2 += "<li>LinuxSampler library for GIG and SFZ support*, http://www.linuxsampler.org/</li>";
  199. #endif
  200. #ifdef WANT_NATIVE
  201. // Internal plugins
  202. # ifdef HAVE_OPENGL
  203. text3 += "<li>DISTRHO Mini-Series plugin code, based on LOSER-dev suite by Michael Gruhn</li>";
  204. # endif
  205. text3 += "<li>NekoFilter plugin code, based on lv2fil by Nedko Arnaudov and Fons Adriaensen</li>";
  206. //text1 += "<li>SunVox library file support, http://www.warmplace.ru/soft/sunvox/</li>"; // unfinished
  207. # ifdef WANT_AUDIOFILE
  208. text3 += "<li>AudioDecoder library for Audio file support, by Robin Gareus</li>";
  209. # endif
  210. # ifdef WANT_MIDIFILE
  211. text3 += "<li>LibSMF library for MIDI file support, http://libsmf.sourceforge.net/</li>";
  212. # endif
  213. # ifdef WANT_ZYNADDSUBFX
  214. text3 += "<li>ZynAddSubFX plugin code, http://zynaddsubfx.sf.net/</li>";
  215. # ifdef WANT_ZYNADDSUBFX_UI
  216. text3 += "<li>ZynAddSubFX UI using NTK, http://non.tuxfamily.org/wiki/NTK</li>";
  217. # endif
  218. # endif
  219. #endif
  220. // misc libs
  221. text4 += "<li>liblo library for OSC support, http://liblo.sourceforge.net/</li>";
  222. #ifdef WANT_LV2
  223. text4 += "<li>serd, sord, sratom and lilv libraries for LV2 discovery, http://drobilla.net/software/lilv/</li>";
  224. #endif
  225. text4 += "<li>RtAudio+RtMidi libraries for extra Audio and MIDI support, http://www.music.mcgill.ca/~gary/rtaudio/</li>";
  226. // end
  227. text4 += "</ul>";
  228. // code snippets
  229. text5 += "<p>Additionally, Carla uses code snippets from the following projects:</p>";
  230. text5 += "<ul>";
  231. text5 += "<li>Pointer and data leak utils from JUCE, http://www.rawmaterialsoftware.com/juce.php</li>";
  232. text5 += "<li>Shared memory utils from dssi-vst, http://www.breakfastquay.com/dssi-vst/</li>";
  233. text5 += "<li>Real-time memory pool, by Nedko Arnaudov</li>";
  234. text5 += "</ul>";
  235. #ifdef WANT_LINUXSAMPLER
  236. // LinuxSampler GPL exception
  237. text5 += "<p>(*) Using LinuxSampler code in commercial hardware or software products is not allowed without prior written authorization by the authors.</p>";
  238. #endif
  239. retText = text1 + text2 + text3 + text4 + text5;
  240. }
  241. return retText;
  242. }
  243. const char* carla_get_supported_file_extensions()
  244. {
  245. carla_debug("carla_get_supported_file_extensions()");
  246. static CarlaString retText;
  247. if (retText.isEmpty())
  248. {
  249. // Base types
  250. retText += "*.carxp;*.carxs";
  251. // CSound
  252. #ifdef WANT_CSOUND
  253. retText += ";*.csd";
  254. #endif
  255. // Sample kits
  256. #ifdef WANT_FLUIDSYNTH
  257. retText += ";*.sf2";
  258. #endif
  259. #ifdef WANT_LINUXSAMPLER
  260. retText += ";*.gig;*.sfz";
  261. #endif
  262. // Files provided by internal plugins
  263. #ifdef WANT_AUDIOFILE
  264. retText += ";*.aiff;*.flac;*.oga;*.ogg;*.w64;*.wav";
  265. # ifdef HAVE_FFMPEG
  266. retText += ";*.3g2;*.3gp;*.aac;*.ac3;*.amr;*.ape;*.mp2;*.mp3;*.mpc;*.wma";
  267. # endif
  268. #endif
  269. #ifdef WANT_MIDIFILE
  270. retText += ";*.mid;*.midi";
  271. #endif
  272. // Plugin presets
  273. #ifdef WANT_ZYNADDSUBFX
  274. retText += ";*.xmz;*.xiz";
  275. #endif
  276. }
  277. return retText;
  278. }
  279. // -------------------------------------------------------------------------------------------------------------------
  280. unsigned int carla_get_engine_driver_count()
  281. {
  282. carla_debug("carla_get_engine_driver_count()");
  283. return CarlaEngine::getDriverCount();
  284. }
  285. const char* carla_get_engine_driver_name(unsigned int index)
  286. {
  287. carla_debug("carla_get_engine_driver_name(%i)", index);
  288. return CarlaEngine::getDriverName(index);
  289. }
  290. const char* const* carla_get_engine_driver_device_names(unsigned int index)
  291. {
  292. carla_debug("carla_get_engine_driver_device_names(%i)", index);
  293. return CarlaEngine::getDriverDeviceNames(index);
  294. }
  295. const EngineDriverDeviceInfo* carla_get_engine_driver_device_info(unsigned int index, const char* name)
  296. {
  297. CARLA_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0', nullptr);
  298. carla_debug("carla_get_engine_driver_device_info(%i, \"%s\")", index, name);
  299. return CarlaEngine::getDriverDeviceInfo(index, name);
  300. }
  301. // -------------------------------------------------------------------------------------------------------------------
  302. unsigned int carla_get_internal_plugin_count()
  303. {
  304. carla_debug("carla_get_internal_plugin_count()");
  305. #ifdef WANT_NATIVE
  306. return static_cast<unsigned int>(CarlaPlugin::getNativePluginCount());
  307. #else
  308. return 0;
  309. #endif
  310. }
  311. const CarlaNativePluginInfo* carla_get_internal_plugin_info(unsigned int index)
  312. {
  313. carla_debug("carla_get_internal_plugin_info(%i)", index);
  314. #ifdef WANT_NATIVE
  315. static CarlaNativePluginInfo info;
  316. const NativePluginDescriptor* const nativePlugin(CarlaPlugin::getNativePluginDescriptor(index));
  317. // as internal plugin, this must never fail
  318. CARLA_SAFE_ASSERT_RETURN(nativePlugin != nullptr, nullptr);
  319. info.category = static_cast<CB::PluginCategory>(nativePlugin->category);
  320. info.hints = 0x0;
  321. if (nativePlugin->hints & ::PLUGIN_IS_RTSAFE)
  322. info.hints |= CB::PLUGIN_IS_RTSAFE;
  323. if (nativePlugin->hints & ::PLUGIN_IS_SYNTH)
  324. info.hints |= CB::PLUGIN_IS_SYNTH;
  325. if (nativePlugin->hints & ::PLUGIN_HAS_UI)
  326. info.hints |= CB::PLUGIN_HAS_CUSTOM_UI;
  327. if (nativePlugin->hints & ::PLUGIN_NEEDS_FIXED_BUFFERS)
  328. info.hints |= CB::PLUGIN_NEEDS_FIXED_BUFFERS;
  329. if (nativePlugin->hints & ::PLUGIN_NEEDS_SINGLE_THREAD)
  330. info.hints |= CB::PLUGIN_NEEDS_SINGLE_THREAD;
  331. info.audioIns = nativePlugin->audioIns;
  332. info.audioOuts = nativePlugin->audioOuts;
  333. info.midiIns = nativePlugin->midiIns;
  334. info.midiOuts = nativePlugin->midiOuts;
  335. info.parameterIns = nativePlugin->paramIns;
  336. info.parameterOuts = nativePlugin->paramOuts;
  337. info.name = nativePlugin->name;
  338. info.label = nativePlugin->label;
  339. info.maker = nativePlugin->maker;
  340. info.copyright = nativePlugin->copyright;
  341. return &info;
  342. #else
  343. return nullptr;
  344. // unused
  345. (void)index;
  346. #endif
  347. }
  348. // -------------------------------------------------------------------------------------------------------------------
  349. const CarlaEngine* carla_get_engine()
  350. {
  351. carla_debug("carla_get_engine()");
  352. return gStandalone.engine;
  353. }
  354. // -------------------------------------------------------------------------------------------------------------------
  355. bool carla_engine_init(const char* driverName, const char* clientName)
  356. {
  357. CARLA_SAFE_ASSERT_RETURN(driverName != nullptr && driverName[0] != '\0', false);
  358. CARLA_SAFE_ASSERT_RETURN(clientName != nullptr && clientName[0] != '\0', false);
  359. carla_debug("carla_engine_init(\"%s\", \"%s\")", driverName, clientName);
  360. if (gStandalone.engine != nullptr)
  361. {
  362. carla_stderr2("Engine is already running");
  363. gStandalone.lastError = "Engine is already running";
  364. return false;
  365. }
  366. #ifdef CARLA_OS_WIN
  367. carla_setenv("WINEASIO_CLIENT_NAME", clientName);
  368. #endif
  369. // TODO: make this an option, put somewhere else
  370. if (std::getenv("WINE_RT") == nullptr)
  371. {
  372. carla_setenv("WINE_RT", "15");
  373. carla_setenv("WINE_SVR_RT", "10");
  374. }
  375. gStandalone.engine = CarlaEngine::newDriverByName(driverName);
  376. if (gStandalone.engine == nullptr)
  377. {
  378. carla_stderr2("The seleted audio driver is not available");
  379. gStandalone.lastError = "The seleted audio driver is not available";
  380. return false;
  381. }
  382. gStandalone.engine->setCallback(gStandalone.engineCallback, gStandalone.engineCallbackPtr);
  383. gStandalone.engine->setFileCallback(gStandalone.fileCallback, gStandalone.fileCallbackPtr);
  384. #ifdef BUILD_BRIDGE
  385. gStandalone.engine->setOption(CB::ENGINE_OPTION_PROCESS_MODE, CB::ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS, nullptr);
  386. gStandalone.engine->setOption(CB::ENGINE_OPTION_TRANSPORT_MODE, CB::ENGINE_TRANSPORT_MODE_JACK, nullptr);
  387. #else
  388. gStandalone.engine->setOption(CB::ENGINE_OPTION_PROCESS_MODE, static_cast<int>(gStandalone.engineOptions.processMode), nullptr);
  389. gStandalone.engine->setOption(CB::ENGINE_OPTION_TRANSPORT_MODE, static_cast<int>(gStandalone.engineOptions.transportMode), nullptr);
  390. #endif
  391. gStandalone.engine->setOption(CB::ENGINE_OPTION_FORCE_STEREO, gStandalone.engineOptions.forceStereo ? 1 : 0, nullptr);
  392. gStandalone.engine->setOption(CB::ENGINE_OPTION_PREFER_PLUGIN_BRIDGES, gStandalone.engineOptions.preferPluginBridges ? 1 : 0, nullptr);
  393. gStandalone.engine->setOption(CB::ENGINE_OPTION_PREFER_UI_BRIDGES, gStandalone.engineOptions.preferUiBridges ? 1 : 0, nullptr);
  394. gStandalone.engine->setOption(CB::ENGINE_OPTION_UIS_ALWAYS_ON_TOP, gStandalone.engineOptions.uisAlwaysOnTop ? 1 : 0, nullptr);
  395. gStandalone.engine->setOption(CB::ENGINE_OPTION_MAX_PARAMETERS, static_cast<int>(gStandalone.engineOptions.maxParameters), nullptr);
  396. gStandalone.engine->setOption(CB::ENGINE_OPTION_UI_BRIDGES_TIMEOUT, static_cast<int>(gStandalone.engineOptions.uiBridgesTimeout), nullptr);
  397. gStandalone.engine->setOption(CB::ENGINE_OPTION_AUDIO_NUM_PERIODS, static_cast<int>(gStandalone.engineOptions.audioNumPeriods), nullptr);
  398. gStandalone.engine->setOption(CB::ENGINE_OPTION_AUDIO_BUFFER_SIZE, static_cast<int>(gStandalone.engineOptions.audioBufferSize), nullptr);
  399. gStandalone.engine->setOption(CB::ENGINE_OPTION_AUDIO_SAMPLE_RATE, static_cast<int>(gStandalone.engineOptions.audioSampleRate), nullptr);
  400. if (gStandalone.engineOptions.audioDevice != nullptr)
  401. gStandalone.engine->setOption(CB::ENGINE_OPTION_AUDIO_DEVICE, 0, gStandalone.engineOptions.audioDevice);
  402. if (gStandalone.engineOptions.binaryDir != nullptr && gStandalone.engineOptions.binaryDir[0] != '\0')
  403. gStandalone.engine->setOption(CB::ENGINE_OPTION_PATH_BINARIES, 0, gStandalone.engineOptions.binaryDir);
  404. if (gStandalone.engineOptions.resourceDir != nullptr && gStandalone.engineOptions.resourceDir[0] != '\0')
  405. gStandalone.engine->setOption(CB::ENGINE_OPTION_PATH_RESOURCES, 0, gStandalone.engineOptions.resourceDir);
  406. if (gStandalone.engineOptions.frontendWinId != 0)
  407. {
  408. char strBuf[STR_MAX+1];
  409. std::sprintf(strBuf, P_INTPTR, gStandalone.engineOptions.frontendWinId);
  410. gStandalone.engine->setOption(CB::ENGINE_OPTION_FRONTEND_WIN_ID, 0, strBuf);
  411. }
  412. else
  413. gStandalone.engine->setOption(CB::ENGINE_OPTION_FRONTEND_WIN_ID, 0, "0");
  414. if (gStandalone.engine->init(clientName))
  415. {
  416. gStandalone.lastError = "No error";
  417. #ifdef HAVE_JUCE
  418. gStandalone.init();
  419. #endif
  420. return true;
  421. }
  422. else
  423. {
  424. gStandalone.lastError = gStandalone.engine->getLastError();
  425. delete gStandalone.engine;
  426. gStandalone.engine = nullptr;
  427. return false;
  428. }
  429. }
  430. #ifdef BUILD_BRIDGE
  431. bool carla_engine_init_bridge(const char audioBaseName[6+1], const char controlBaseName[6+1], const char* clientName)
  432. {
  433. CARLA_SAFE_ASSERT_RETURN(audioBaseName != nullptr && audioBaseName[0] != '\0', false);
  434. CARLA_SAFE_ASSERT_RETURN(controlBaseName != nullptr && controlBaseName[0] != '\0', false);
  435. CARLA_SAFE_ASSERT_RETURN(clientName != nullptr && clientName[0] != '\0', false);
  436. carla_debug("carla_engine_init_bridge(\"%s\", \"%s\", \"%s\")", audioBaseName, controlBaseName, clientName);
  437. if (gStandalone.engine != nullptr)
  438. {
  439. carla_stderr2("Engine is already running");
  440. gStandalone.lastError = "Engine is already running";
  441. return false;
  442. }
  443. gStandalone.engine = CarlaEngine::newBridge(audioBaseName, controlBaseName);
  444. if (gStandalone.engine == nullptr)
  445. {
  446. carla_stderr2("The seleted audio driver is not available!");
  447. gStandalone.lastError = "The seleted audio driver is not available!";
  448. return false;
  449. }
  450. gStandalone.engine->setCallback(gStandalone.engineCallback, gStandalone.engineCallbackPtr);
  451. gStandalone.engine->setFileCallback(gStandalone.fileCallback, gStandalone.fileCallbackPtr);
  452. // forced options for bridge mode
  453. gStandalone.engine->setOption(CB::ENGINE_OPTION_PROCESS_MODE, CB::ENGINE_PROCESS_MODE_BRIDGE, nullptr);
  454. gStandalone.engine->setOption(CB::ENGINE_OPTION_TRANSPORT_MODE, CB::ENGINE_TRANSPORT_MODE_BRIDGE, nullptr);
  455. gStandalone.engine->setOption(CB::ENGINE_OPTION_FORCE_STEREO, false, nullptr);
  456. gStandalone.engine->setOption(CB::ENGINE_OPTION_PREFER_PLUGIN_BRIDGES, false, nullptr);
  457. gStandalone.engine->setOption(CB::ENGINE_OPTION_PREFER_UI_BRIDGES, false, nullptr);
  458. // TODO: get these from environment
  459. gStandalone.engine->setOption(CB::ENGINE_OPTION_UIS_ALWAYS_ON_TOP, gStandalone.engineOptions.uisAlwaysOnTop ? 1 : 0, nullptr);
  460. gStandalone.engine->setOption(CB::ENGINE_OPTION_MAX_PARAMETERS, static_cast<int>(gStandalone.engineOptions.maxParameters), nullptr);
  461. gStandalone.engine->setOption(CB::ENGINE_OPTION_UI_BRIDGES_TIMEOUT, static_cast<int>(gStandalone.engineOptions.uiBridgesTimeout), nullptr);
  462. gStandalone.engine->setOption(CB::ENGINE_OPTION_PATH_BINARIES, 0, (const char*)gStandalone.engineOptions.binaryDir);
  463. gStandalone.engine->setOption(CB::ENGINE_OPTION_PATH_RESOURCES, 0, (const char*)gStandalone.engineOptions.resourceDir);
  464. // frontend winId here
  465. if (gStandalone.engine->init(clientName))
  466. {
  467. gStandalone.lastError = "No error";
  468. #ifdef HAVE_JUCE
  469. gStandalone.init();
  470. #endif
  471. return true;
  472. }
  473. else
  474. {
  475. gStandalone.lastError = gStandalone.engine->getLastError();
  476. delete gStandalone.engine;
  477. gStandalone.engine = nullptr;
  478. return false;
  479. }
  480. }
  481. #endif
  482. bool carla_engine_close()
  483. {
  484. carla_debug("carla_engine_close()");
  485. if (gStandalone.engine == nullptr)
  486. {
  487. carla_stderr2("Engine is not running");
  488. gStandalone.lastError = "Engine is not running";
  489. return false;
  490. }
  491. gStandalone.engine->setAboutToClose();
  492. gStandalone.engine->removeAllPlugins();
  493. const bool closed(gStandalone.engine->close());
  494. if (! closed)
  495. gStandalone.lastError = gStandalone.engine->getLastError();
  496. #ifdef HAVE_JUCE
  497. gStandalone.close();
  498. #endif
  499. delete gStandalone.engine;
  500. gStandalone.engine = nullptr;
  501. return closed;
  502. }
  503. void carla_engine_idle()
  504. {
  505. CARLA_SAFE_ASSERT_RETURN(gStandalone.engine != nullptr,);
  506. #if defined(HAVE_JUCE) && ! defined(CARLA_OS_LINUX)
  507. gStandalone.idle();
  508. #endif
  509. gStandalone.engine->idle();
  510. }
  511. bool carla_is_engine_running()
  512. {
  513. return (gStandalone.engine != nullptr && gStandalone.engine->isRunning());
  514. }
  515. void carla_set_engine_about_to_close()
  516. {
  517. CARLA_SAFE_ASSERT_RETURN(gStandalone.engine != nullptr,);
  518. carla_debug("carla_set_engine_about_to_close()");
  519. gStandalone.engine->setAboutToClose();
  520. }
  521. void carla_set_engine_callback(EngineCallbackFunc func, void* ptr)
  522. {
  523. carla_debug("carla_set_engine_callback(%p, %p)", func, ptr);
  524. gStandalone.engineCallback = func;
  525. gStandalone.engineCallbackPtr = ptr;
  526. if (gStandalone.engine != nullptr)
  527. gStandalone.engine->setCallback(func, ptr);
  528. //#ifdef WANT_LOGS
  529. // gLogThread.setCallback(func, ptr);
  530. //#endif
  531. }
  532. #ifndef BUILD_BRIDGE
  533. void carla_set_engine_option(EngineOption option, int value, const char* valueStr)
  534. {
  535. carla_debug("carla_set_engine_option(%i:%s, %i, \"%s\")", option, CB::EngineOption2Str(option), value, valueStr);
  536. switch (option)
  537. {
  538. case CB::ENGINE_OPTION_DEBUG:
  539. break;
  540. case CB::ENGINE_OPTION_PROCESS_MODE:
  541. CARLA_SAFE_ASSERT_RETURN(value >= CB::ENGINE_PROCESS_MODE_SINGLE_CLIENT && value < CB::ENGINE_PROCESS_MODE_BRIDGE,);
  542. gStandalone.engineOptions.processMode = static_cast<CB::EngineProcessMode>(value);
  543. break;
  544. case CB::ENGINE_OPTION_TRANSPORT_MODE:
  545. CARLA_SAFE_ASSERT_RETURN(value >= CB::ENGINE_TRANSPORT_MODE_INTERNAL && value < CB::ENGINE_TRANSPORT_MODE_BRIDGE,);
  546. gStandalone.engineOptions.transportMode = static_cast<CB::EngineTransportMode>(value);
  547. break;
  548. case CB::ENGINE_OPTION_FORCE_STEREO:
  549. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  550. gStandalone.engineOptions.forceStereo = (value != 0);
  551. break;
  552. case CB::ENGINE_OPTION_PREFER_PLUGIN_BRIDGES:
  553. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  554. gStandalone.engineOptions.preferPluginBridges = (value != 0);
  555. break;
  556. case CB::ENGINE_OPTION_PREFER_UI_BRIDGES:
  557. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  558. gStandalone.engineOptions.preferUiBridges = (value != 0);
  559. break;
  560. case CB::ENGINE_OPTION_UIS_ALWAYS_ON_TOP:
  561. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  562. gStandalone.engineOptions.uisAlwaysOnTop = (value != 0);
  563. break;
  564. case CB::ENGINE_OPTION_MAX_PARAMETERS:
  565. CARLA_SAFE_ASSERT_RETURN(value >= 0,);
  566. gStandalone.engineOptions.maxParameters = static_cast<unsigned int>(value);
  567. break;
  568. case CB::ENGINE_OPTION_UI_BRIDGES_TIMEOUT:
  569. CARLA_SAFE_ASSERT_RETURN(value >= 0,);
  570. gStandalone.engineOptions.uiBridgesTimeout = static_cast<unsigned int>(value);
  571. break;
  572. case CB::ENGINE_OPTION_AUDIO_NUM_PERIODS:
  573. CARLA_SAFE_ASSERT_RETURN(value >= 2 && value <= 3,);
  574. gStandalone.engineOptions.audioNumPeriods = static_cast<unsigned int>(value);
  575. break;
  576. case CB::ENGINE_OPTION_AUDIO_BUFFER_SIZE:
  577. CARLA_SAFE_ASSERT_RETURN(value >= 8,);
  578. gStandalone.engineOptions.audioBufferSize = static_cast<unsigned int>(value);
  579. break;
  580. case CB::ENGINE_OPTION_AUDIO_SAMPLE_RATE:
  581. CARLA_SAFE_ASSERT_RETURN(value >= 22050,);
  582. gStandalone.engineOptions.audioSampleRate = static_cast<unsigned int>(value);
  583. break;
  584. case CB::ENGINE_OPTION_AUDIO_DEVICE:
  585. CARLA_SAFE_ASSERT_RETURN(valueStr != nullptr && valueStr[0] != '\0',);
  586. if (gStandalone.engineOptions.audioDevice != nullptr)
  587. delete[] gStandalone.engineOptions.audioDevice;
  588. gStandalone.engineOptions.audioDevice = carla_strdup(valueStr);
  589. break;
  590. case CB::ENGINE_OPTION_PATH_BINARIES:
  591. CARLA_SAFE_ASSERT_RETURN(valueStr != nullptr && valueStr[0] != '\0',);
  592. if (gStandalone.engineOptions.binaryDir != nullptr)
  593. delete[] gStandalone.engineOptions.binaryDir;
  594. gStandalone.engineOptions.binaryDir = carla_strdup(valueStr);
  595. break;
  596. case CB::ENGINE_OPTION_PATH_RESOURCES:
  597. CARLA_SAFE_ASSERT_RETURN(valueStr != nullptr && valueStr[0] != '\0',);
  598. if (gStandalone.engineOptions.resourceDir != nullptr)
  599. delete[] gStandalone.engineOptions.resourceDir;
  600. gStandalone.engineOptions.resourceDir = carla_strdup(valueStr);
  601. break;
  602. case CB::ENGINE_OPTION_FRONTEND_WIN_ID:
  603. CARLA_SAFE_ASSERT_RETURN(valueStr != nullptr && valueStr[0] != '\0',);
  604. const long winId(std::atol(valueStr));
  605. CARLA_SAFE_ASSERT_RETURN(winId >= 0,);
  606. gStandalone.engineOptions.frontendWinId = static_cast<uintptr_t>(winId);
  607. break;
  608. }
  609. if (gStandalone.engine != nullptr)
  610. gStandalone.engine->setOption(option, value, valueStr);
  611. }
  612. #endif
  613. void carla_set_file_callback(FileCallbackFunc func, void* ptr)
  614. {
  615. carla_debug("carla_set_file_callback(%p, %p)", func, ptr);
  616. gStandalone.fileCallback = func;
  617. gStandalone.fileCallbackPtr = ptr;
  618. if (gStandalone.engine != nullptr)
  619. gStandalone.engine->setFileCallback(func, ptr);
  620. }
  621. // -------------------------------------------------------------------------------------------------------------------
  622. bool carla_load_file(const char* filename)
  623. {
  624. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', false);
  625. carla_debug("carla_load_file(\"%s\")", filename);
  626. if (gStandalone.engine != nullptr)
  627. return gStandalone.engine->loadFile(filename);
  628. carla_stderr2("Engine is not running");
  629. gStandalone.lastError = "Engine is not running";
  630. return false;
  631. }
  632. bool carla_load_project(const char* filename)
  633. {
  634. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', false);
  635. carla_debug("carla_load_project(\"%s\")", filename);
  636. if (gStandalone.engine != nullptr)
  637. return gStandalone.engine->loadProject(filename);
  638. carla_stderr2("Engine is not running");
  639. gStandalone.lastError = "Engine is not running";
  640. return false;
  641. }
  642. bool carla_save_project(const char* filename)
  643. {
  644. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', false);
  645. carla_debug("carla_save_project(\"%s\")", filename);
  646. if (gStandalone.engine != nullptr)
  647. return gStandalone.engine->saveProject(filename);
  648. carla_stderr2("Engine was never initiated");
  649. gStandalone.lastError = "Engine was never initiated";
  650. return false;
  651. }
  652. #ifndef BUILD_BRIDGE
  653. // -------------------------------------------------------------------------------------------------------------------
  654. bool carla_patchbay_connect(int portIdA, int portIdB)
  655. {
  656. CARLA_SAFE_ASSERT_RETURN(portIdA != portIdB, false);
  657. carla_debug("carla_patchbay_connect(%i, %i)", portIdA, portIdB);
  658. if (gStandalone.engine != nullptr)
  659. return gStandalone.engine->patchbayConnect(portIdA, portIdB);
  660. carla_stderr2("Engine is not running");
  661. gStandalone.lastError = "Engine is not running";
  662. return false;
  663. }
  664. bool carla_patchbay_disconnect(uint connectionId)
  665. {
  666. carla_debug("carla_patchbay_disconnect(%i)", connectionId);
  667. if (gStandalone.engine != nullptr)
  668. return gStandalone.engine->patchbayDisconnect(connectionId);
  669. carla_stderr2("Engine is not running");
  670. gStandalone.lastError = "Engine is not running";
  671. return false;
  672. }
  673. bool carla_patchbay_refresh()
  674. {
  675. carla_debug("carla_patchbay_refresh()");
  676. if (gStandalone.engine != nullptr)
  677. return gStandalone.engine->patchbayRefresh();
  678. carla_stderr2("Engine is not running");
  679. gStandalone.lastError = "Engine is not running";
  680. return false;
  681. }
  682. #endif
  683. // -------------------------------------------------------------------------------------------------------------------
  684. void carla_transport_play()
  685. {
  686. CARLA_SAFE_ASSERT_RETURN(gStandalone.engine != nullptr && gStandalone.engine->isRunning(),);
  687. carla_debug("carla_transport_play()");
  688. gStandalone.engine->transportPlay();
  689. }
  690. void carla_transport_pause()
  691. {
  692. CARLA_SAFE_ASSERT_RETURN(gStandalone.engine != nullptr && gStandalone.engine->isRunning(),);
  693. carla_debug("carla_transport_pause()");
  694. gStandalone.engine->transportPause();
  695. }
  696. void carla_transport_relocate(uint64_t frame)
  697. {
  698. CARLA_SAFE_ASSERT_RETURN(gStandalone.engine != nullptr && gStandalone.engine->isRunning(),);
  699. carla_debug("carla_transport_relocate(%i)", frame);
  700. gStandalone.engine->transportRelocate(frame);
  701. }
  702. uint64_t carla_get_current_transport_frame()
  703. {
  704. CARLA_SAFE_ASSERT_RETURN(gStandalone.engine != nullptr && gStandalone.engine->isRunning(), 0);
  705. const CB::EngineTimeInfo& timeInfo(gStandalone.engine->getTimeInfo());
  706. return timeInfo.frame;
  707. }
  708. const CarlaTransportInfo* carla_get_transport_info()
  709. {
  710. static CarlaTransportInfo retInfo;
  711. // reset
  712. retInfo.playing = false;
  713. retInfo.frame = 0;
  714. retInfo.bar = 0;
  715. retInfo.beat = 0;
  716. retInfo.tick = 0;
  717. retInfo.bpm = 0.0;
  718. CARLA_SAFE_ASSERT_RETURN(gStandalone.engine != nullptr && gStandalone.engine->isRunning(), &retInfo);
  719. const CB::EngineTimeInfo& timeInfo(gStandalone.engine->getTimeInfo());
  720. retInfo.playing = timeInfo.playing;
  721. retInfo.frame = timeInfo.frame;
  722. if (timeInfo.valid & CB::EngineTimeInfo::kValidBBT)
  723. {
  724. retInfo.bar = timeInfo.bbt.bar;
  725. retInfo.beat = timeInfo.bbt.beat;
  726. retInfo.tick = timeInfo.bbt.tick;
  727. retInfo.bpm = timeInfo.bbt.beatsPerMinute;
  728. }
  729. return &retInfo;
  730. }
  731. // -------------------------------------------------------------------------------------------------------------------
  732. bool carla_add_plugin(BinaryType btype, PluginType ptype, const char* filename, const char* name, const char* label, const void* extraPtr)
  733. {
  734. CARLA_SAFE_ASSERT_RETURN(label != nullptr && label[0] != '\0', false);
  735. carla_debug("carla_add_plugin(%i:%s, %i:%s, \"%s\", \"%s\", \"%s\", %p)", btype, CB::BinaryType2Str(btype), ptype, CB::PluginType2Str(ptype), filename, name, label, extraPtr);
  736. if (gStandalone.engine != nullptr)
  737. return gStandalone.engine->addPlugin(btype, ptype, filename, name, label, extraPtr);
  738. carla_stderr2("Engine is not running");
  739. gStandalone.lastError = "Engine is not running";
  740. return false;
  741. }
  742. bool carla_remove_plugin(uint pluginId)
  743. {
  744. carla_debug("carla_remove_plugin(%i)", pluginId);
  745. if (gStandalone.engine != nullptr)
  746. return gStandalone.engine->removePlugin(pluginId);
  747. carla_stderr2("Engine is not running");
  748. gStandalone.lastError = "Engine is not running";
  749. return false;
  750. }
  751. bool carla_remove_all_plugins()
  752. {
  753. carla_debug("carla_remove_all_plugins()");
  754. if (gStandalone.engine != nullptr)
  755. return gStandalone.engine->removeAllPlugins();
  756. carla_stderr2("Engine is not running");
  757. gStandalone.lastError = "Engine is not running";
  758. return false;
  759. }
  760. const char* carla_rename_plugin(uint pluginId, const char* newName)
  761. {
  762. CARLA_SAFE_ASSERT_RETURN(newName != nullptr && newName[0] != '\0', nullptr);
  763. carla_debug("carla_rename_plugin(%i, \"%s\")", pluginId, newName);
  764. if (gStandalone.engine != nullptr)
  765. return gStandalone.engine->renamePlugin(pluginId, newName);
  766. carla_stderr2("Engine is not running");
  767. gStandalone.lastError = "Engine is not running";
  768. return nullptr;
  769. }
  770. bool carla_clone_plugin(uint pluginId)
  771. {
  772. carla_debug("carla_clone_plugin(%i)", pluginId);
  773. if (gStandalone.engine != nullptr)
  774. return gStandalone.engine->clonePlugin(pluginId);
  775. carla_stderr2("Engine is not running");
  776. gStandalone.lastError = "Engine is not running";
  777. return false;
  778. }
  779. bool carla_replace_plugin(uint pluginId)
  780. {
  781. carla_debug("carla_replace_plugin(%i)", pluginId);
  782. if (gStandalone.engine != nullptr)
  783. return gStandalone.engine->replacePlugin(pluginId);
  784. carla_stderr2("Engine is not running");
  785. gStandalone.lastError = "Engine is not running";
  786. return false;
  787. }
  788. bool carla_switch_plugins(uint pluginIdA, uint pluginIdB)
  789. {
  790. CARLA_SAFE_ASSERT_RETURN(pluginIdA != pluginIdB, false);
  791. carla_debug("carla_switch_plugins(%i, %i)", pluginIdA, pluginIdB);
  792. if (gStandalone.engine != nullptr)
  793. return gStandalone.engine->switchPlugins(pluginIdA, pluginIdB);
  794. carla_stderr2("Engine is not running");
  795. gStandalone.lastError = "Engine is not running";
  796. return false;
  797. }
  798. // -------------------------------------------------------------------------------------------------------------------
  799. bool carla_load_plugin_state(uint pluginId, const char* filename)
  800. {
  801. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', false);
  802. carla_debug("carla_load_plugin_state(%i, \"%s\")", pluginId, filename);
  803. if (gStandalone.engine == nullptr || ! gStandalone.engine->isRunning())
  804. {
  805. carla_stderr2("Engine is not running");
  806. gStandalone.lastError = "Engine is not running";
  807. return false;
  808. }
  809. if (CarlaPlugin* const plugin = gStandalone.engine->getPlugin(pluginId))
  810. return plugin->loadStateFromFile(filename);
  811. carla_stderr2("carla_load_plugin_state(%i, \"%s\") - could not find plugin", pluginId, filename);
  812. return false;
  813. }
  814. bool carla_save_plugin_state(uint pluginId, const char* filename)
  815. {
  816. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', false);
  817. carla_debug("carla_save_plugin_state(%i, \"%s\")", pluginId, filename);
  818. if (gStandalone.engine == nullptr)
  819. {
  820. carla_stderr2("Engine is not running");
  821. gStandalone.lastError = "Engine is not running";
  822. return false;
  823. }
  824. // allow to save even if engine isn't running
  825. if (CarlaPlugin* const plugin = gStandalone.engine->getPlugin(pluginId))
  826. return plugin->saveStateToFile(filename);
  827. carla_stderr2("carla_save_plugin_state(%i, \"%s\") - could not find plugin", pluginId, filename);
  828. return false;
  829. }
  830. // -------------------------------------------------------------------------------------------------------------------
  831. const CarlaPluginInfo* carla_get_plugin_info(uint pluginId)
  832. {
  833. carla_debug("carla_get_plugin_info(%i)", pluginId);
  834. static CarlaPluginInfo info;
  835. // reset
  836. info.type = CB::PLUGIN_NONE;
  837. info.category = CB::PLUGIN_CATEGORY_NONE;
  838. info.hints = 0x0;
  839. info.optionsAvailable = 0x0;
  840. info.optionsEnabled = 0x0;
  841. info.filename = nullptr;
  842. info.name = nullptr;
  843. info.iconName = nullptr;
  844. info.uniqueId = 0;
  845. // cleanup
  846. if (info.label != nullptr)
  847. {
  848. delete[] info.label;
  849. info.label = nullptr;
  850. }
  851. if (info.maker != nullptr)
  852. {
  853. delete[] info.maker;
  854. info.maker = nullptr;
  855. }
  856. if (info.copyright != nullptr)
  857. {
  858. delete[] info.copyright;
  859. info.copyright = nullptr;
  860. }
  861. CARLA_SAFE_ASSERT_RETURN(gStandalone.engine != nullptr, &info);
  862. if (CarlaPlugin* const plugin = gStandalone.engine->getPlugin(pluginId))
  863. {
  864. char strBufLabel[STR_MAX+1];
  865. char strBufMaker[STR_MAX+1];
  866. char strBufCopyright[STR_MAX+1];
  867. carla_zeroChar(strBufLabel, STR_MAX+1);
  868. carla_zeroChar(strBufMaker, STR_MAX+1);
  869. carla_zeroChar(strBufCopyright, STR_MAX+1);
  870. info.type = plugin->getType();
  871. info.category = plugin->getCategory();
  872. info.hints = plugin->getHints();
  873. info.filename = plugin->getFilename();
  874. info.name = plugin->getName();
  875. info.iconName = plugin->getIconName();
  876. info.uniqueId = plugin->getUniqueId();
  877. info.optionsAvailable = plugin->getOptionsAvailable();
  878. info.optionsEnabled = plugin->getOptionsEnabled();
  879. plugin->getLabel(strBufLabel);
  880. info.label = carla_strdup(strBufLabel);
  881. plugin->getMaker(strBufMaker);
  882. info.maker = carla_strdup(strBufMaker);
  883. plugin->getCopyright(strBufCopyright);
  884. info.copyright = carla_strdup(strBufCopyright);
  885. return &info;
  886. }
  887. carla_stderr2("carla_get_plugin_info(%i) - could not find plugin", pluginId);
  888. return &info;
  889. }
  890. const CarlaPortCountInfo* carla_get_audio_port_count_info(uint pluginId)
  891. {
  892. carla_debug("carla_get_audio_port_count_info(%i)", pluginId);
  893. static CarlaPortCountInfo info;
  894. // reset
  895. info.ins = 0;
  896. info.outs = 0;
  897. CARLA_SAFE_ASSERT_RETURN(gStandalone.engine != nullptr, &info);
  898. if (CarlaPlugin* const plugin = gStandalone.engine->getPlugin(pluginId))
  899. {
  900. info.ins = plugin->getAudioInCount();
  901. info.outs = plugin->getAudioOutCount();
  902. return &info;
  903. }
  904. carla_stderr2("carla_get_audio_port_count_info(%i) - could not find plugin", pluginId);
  905. return &info;
  906. }
  907. const CarlaPortCountInfo* carla_get_midi_port_count_info(uint pluginId)
  908. {
  909. carla_debug("carla_get_midi_port_count_info(%i)", pluginId);
  910. static CarlaPortCountInfo info;
  911. // reset
  912. info.ins = 0;
  913. info.outs = 0;
  914. CARLA_SAFE_ASSERT_RETURN(gStandalone.engine != nullptr, &info);
  915. if (CarlaPlugin* const plugin = gStandalone.engine->getPlugin(pluginId))
  916. {
  917. info.ins = plugin->getMidiInCount();
  918. info.outs = plugin->getMidiOutCount();
  919. return &info;
  920. }
  921. carla_stderr2("carla_get_midi_port_count_info(%i) - could not find plugin", pluginId);
  922. return &info;
  923. }
  924. const CarlaPortCountInfo* carla_get_parameter_count_info(uint pluginId)
  925. {
  926. carla_debug("carla_get_parameter_count_info(%i)", pluginId);
  927. static CarlaPortCountInfo info;
  928. // reset
  929. info.ins = 0;
  930. info.outs = 0;
  931. CARLA_SAFE_ASSERT_RETURN(gStandalone.engine != nullptr, &info);
  932. if (CarlaPlugin* const plugin = gStandalone.engine->getPlugin(pluginId))
  933. {
  934. plugin->getParameterCountInfo(info.ins, info.outs);
  935. return &info;
  936. }
  937. carla_stderr2("carla_get_parameter_count_info(%i) - could not find plugin", pluginId);
  938. return &info;
  939. }
  940. const CarlaParameterInfo* carla_get_parameter_info(uint pluginId, uint32_t parameterId)
  941. {
  942. carla_debug("carla_get_parameter_info(%i, %i)", pluginId, parameterId);
  943. static CarlaParameterInfo info;
  944. // reset
  945. info.scalePointCount = 0;
  946. // cleanup
  947. if (info.name != nullptr)
  948. {
  949. delete[] info.name;
  950. info.name = nullptr;
  951. }
  952. if (info.symbol != nullptr)
  953. {
  954. delete[] info.symbol;
  955. info.symbol = nullptr;
  956. }
  957. if (info.unit != nullptr)
  958. {
  959. delete[] info.unit;
  960. info.unit = nullptr;
  961. }
  962. CARLA_SAFE_ASSERT_RETURN(gStandalone.engine != nullptr, &info);
  963. if (CarlaPlugin* const plugin = gStandalone.engine->getPlugin(pluginId))
  964. {
  965. if (parameterId < plugin->getParameterCount())
  966. {
  967. char strBufName[STR_MAX+1];
  968. char strBufSymbol[STR_MAX+1];
  969. char strBufUnit[STR_MAX+1];
  970. carla_zeroChar(strBufName, STR_MAX+1);
  971. carla_zeroChar(strBufSymbol, STR_MAX+1);
  972. carla_zeroChar(strBufUnit, STR_MAX+1);
  973. info.scalePointCount = plugin->getParameterScalePointCount(parameterId);
  974. plugin->getParameterName(parameterId, strBufName);
  975. info.name = carla_strdup(strBufName);
  976. plugin->getParameterSymbol(parameterId, strBufSymbol);
  977. info.symbol = carla_strdup(strBufSymbol);
  978. plugin->getParameterUnit(parameterId, strBufUnit);
  979. info.unit = carla_strdup(strBufUnit);
  980. }
  981. else
  982. carla_stderr2("carla_get_parameter_info(%i, %i) - parameterId out of bounds", pluginId, parameterId);
  983. return &info;
  984. }
  985. carla_stderr2("carla_get_parameter_info(%i, %i) - could not find plugin", pluginId, parameterId);
  986. return &info;
  987. }
  988. const CarlaScalePointInfo* carla_get_parameter_scalepoint_info(uint pluginId, uint32_t parameterId, uint32_t scalePointId)
  989. {
  990. carla_debug("carla_get_parameter_scalepoint_info(%i, %i, %i)", pluginId, parameterId, scalePointId);
  991. CARLA_ASSERT(gStandalone.engine != nullptr);
  992. static CarlaScalePointInfo info;
  993. // reset
  994. info.value = 0.0f;
  995. // cleanup
  996. if (info.label != nullptr)
  997. {
  998. delete[] info.label;
  999. info.label = nullptr;
  1000. }
  1001. CARLA_SAFE_ASSERT_RETURN(gStandalone.engine != nullptr, &info);
  1002. if (CarlaPlugin* const plugin = gStandalone.engine->getPlugin(pluginId))
  1003. {
  1004. if (parameterId < plugin->getParameterCount())
  1005. {
  1006. if (scalePointId < plugin->getParameterScalePointCount(parameterId))
  1007. {
  1008. char strBufLabel[STR_MAX+1];
  1009. carla_zeroChar(strBufLabel, STR_MAX+1);
  1010. info.value = plugin->getParameterScalePointValue(parameterId, scalePointId);
  1011. plugin->getParameterScalePointLabel(parameterId, scalePointId, strBufLabel);
  1012. info.label = carla_strdup(strBufLabel);
  1013. }
  1014. else
  1015. carla_stderr2("carla_get_parameter_scalepoint_info(%i, %i, %i) - scalePointId out of bounds", pluginId, parameterId, scalePointId);
  1016. }
  1017. else
  1018. carla_stderr2("carla_get_parameter_scalepoint_info(%i, %i, %i) - parameterId out of bounds", pluginId, parameterId, scalePointId);
  1019. return &info;
  1020. }
  1021. carla_stderr2("carla_get_parameter_scalepoint_info(%i, %i, %i) - could not find plugin", pluginId, parameterId, scalePointId);
  1022. return &info;
  1023. }
  1024. // -------------------------------------------------------------------------------------------------------------------
  1025. const ParameterData* carla_get_parameter_data(uint pluginId, uint32_t parameterId)
  1026. {
  1027. carla_debug("carla_get_parameter_data(%i, %i)", pluginId, parameterId);
  1028. static const ParameterData fallbackParameterData = { CB::PARAMETER_UNKNOWN, 0x0, CB::PARAMETER_NULL, -1, -1, 0 };
  1029. CARLA_SAFE_ASSERT_RETURN(gStandalone.engine != nullptr, &fallbackParameterData);
  1030. if (CarlaPlugin* const plugin = gStandalone.engine->getPlugin(pluginId))
  1031. {
  1032. if (parameterId < plugin->getParameterCount())
  1033. return &plugin->getParameterData(parameterId);
  1034. carla_stderr2("carla_get_parameter_data(%i, %i) - parameterId out of bounds", pluginId, parameterId);
  1035. return &fallbackParameterData;
  1036. }
  1037. carla_stderr2("carla_get_parameter_data(%i, %i) - could not find plugin", pluginId, parameterId);
  1038. return &fallbackParameterData;
  1039. }
  1040. const ParameterRanges* carla_get_parameter_ranges(uint pluginId, uint32_t parameterId)
  1041. {
  1042. carla_debug("carla_get_parameter_ranges(%i, %i)", pluginId, parameterId);
  1043. static const ParameterRanges fallbackParamRanges = { 0.0f, 0.0f, 1.0f, 0.01f, 0.0001f, 0.1f };
  1044. CARLA_SAFE_ASSERT_RETURN(gStandalone.engine != nullptr, &fallbackParamRanges);
  1045. if (CarlaPlugin* const plugin = gStandalone.engine->getPlugin(pluginId))
  1046. {
  1047. if (parameterId < plugin->getParameterCount())
  1048. return &plugin->getParameterRanges(parameterId);
  1049. carla_stderr2("carla_get_parameter_ranges(%i, %i) - parameterId out of bounds", pluginId, parameterId);
  1050. return &fallbackParamRanges;
  1051. }
  1052. carla_stderr2("carla_get_parameter_ranges(%i, %i) - could not find plugin", pluginId, parameterId);
  1053. return &fallbackParamRanges;
  1054. }
  1055. const MidiProgramData* carla_get_midi_program_data(uint pluginId, uint32_t midiProgramId)
  1056. {
  1057. carla_debug("carla_get_midi_program_data(%i, %i)", pluginId, midiProgramId);
  1058. static const MidiProgramData fallbackMidiProgData = { 0, 0, nullptr };
  1059. CARLA_SAFE_ASSERT_RETURN(gStandalone.engine != nullptr, &fallbackMidiProgData);
  1060. if (CarlaPlugin* const plugin = gStandalone.engine->getPlugin(pluginId))
  1061. {
  1062. if (midiProgramId < plugin->getMidiProgramCount())
  1063. return &plugin->getMidiProgramData(midiProgramId);
  1064. carla_stderr2("carla_get_midi_program_data(%i, %i) - midiProgramId out of bounds", pluginId, midiProgramId);
  1065. return &fallbackMidiProgData;
  1066. }
  1067. carla_stderr2("carla_get_midi_program_data(%i, %i) - could not find plugin", pluginId, midiProgramId);
  1068. return &fallbackMidiProgData;
  1069. }
  1070. const CustomData* carla_get_custom_data(uint pluginId, uint32_t customDataId)
  1071. {
  1072. carla_debug("carla_get_custom_data(%i, %i)", pluginId, customDataId);
  1073. static const CustomData fallbackCustomData = { nullptr, nullptr, nullptr };
  1074. CARLA_SAFE_ASSERT_RETURN(gStandalone.engine != nullptr, &fallbackCustomData);
  1075. if (CarlaPlugin* const plugin = gStandalone.engine->getPlugin(pluginId))
  1076. {
  1077. if (customDataId < plugin->getCustomDataCount())
  1078. return &plugin->getCustomData(customDataId);
  1079. carla_stderr2("carla_get_custom_data(%i, %i) - customDataId out of bounds", pluginId, customDataId);
  1080. return &fallbackCustomData;
  1081. }
  1082. carla_stderr2("carla_get_custom_data(%i, %i) - could not find plugin", pluginId, customDataId);
  1083. return &fallbackCustomData;
  1084. }
  1085. const char* carla_get_chunk_data(uint pluginId)
  1086. {
  1087. carla_debug("carla_get_chunk_data(%i)", pluginId);
  1088. CARLA_SAFE_ASSERT_RETURN(gStandalone.engine != nullptr, nullptr);
  1089. static CarlaString chunkData;
  1090. // cleanup
  1091. chunkData.clear();
  1092. if (CarlaPlugin* const plugin = gStandalone.engine->getPlugin(pluginId))
  1093. {
  1094. if (plugin->getOptionsEnabled() & CB::PLUGIN_OPTION_USE_CHUNKS)
  1095. {
  1096. void* data = nullptr;
  1097. const int32_t dataSize(plugin->getChunkData(&data));
  1098. if (data != nullptr && dataSize > 0)
  1099. {
  1100. chunkData = QByteArray((char*)data, dataSize).toBase64().constData();
  1101. return (const char*)chunkData;
  1102. }
  1103. else
  1104. carla_stderr2("carla_get_chunk_data(%i) - got invalid chunk data", pluginId);
  1105. }
  1106. else
  1107. carla_stderr2("carla_get_chunk_data(%i) - plugin does not use chunks", pluginId);
  1108. return nullptr;
  1109. }
  1110. carla_stderr2("carla_get_chunk_data(%i) - could not find plugin", pluginId);
  1111. return nullptr;
  1112. }
  1113. // -------------------------------------------------------------------------------------------------------------------
  1114. uint32_t carla_get_parameter_count(uint pluginId)
  1115. {
  1116. CARLA_SAFE_ASSERT_RETURN(gStandalone.engine != nullptr, 0);
  1117. carla_debug("carla_get_parameter_count(%i)", pluginId);
  1118. if (CarlaPlugin* const plugin = gStandalone.engine->getPlugin(pluginId))
  1119. return plugin->getParameterCount();
  1120. carla_stderr2("carla_get_parameter_count(%i) - could not find plugin", pluginId);
  1121. return 0;
  1122. }
  1123. uint32_t carla_get_program_count(uint pluginId)
  1124. {
  1125. CARLA_SAFE_ASSERT_RETURN(gStandalone.engine != nullptr, 0);
  1126. carla_debug("carla_get_program_count(%i)", pluginId);
  1127. if (CarlaPlugin* const plugin = gStandalone.engine->getPlugin(pluginId))
  1128. return plugin->getProgramCount();
  1129. carla_stderr2("carla_get_program_count(%i) - could not find plugin", pluginId);
  1130. return 0;
  1131. }
  1132. uint32_t carla_get_midi_program_count(uint pluginId)
  1133. {
  1134. CARLA_SAFE_ASSERT_RETURN(gStandalone.engine != nullptr, 0);
  1135. carla_debug("carla_get_midi_program_count(%i)", pluginId);
  1136. if (CarlaPlugin* const plugin = gStandalone.engine->getPlugin(pluginId))
  1137. return plugin->getMidiProgramCount();
  1138. carla_stderr2("carla_get_midi_program_count(%i) - could not find plugin", pluginId);
  1139. return 0;
  1140. }
  1141. uint32_t carla_get_custom_data_count(uint pluginId)
  1142. {
  1143. CARLA_SAFE_ASSERT_RETURN(gStandalone.engine != nullptr, 0);
  1144. carla_debug("carla_get_custom_data_count(%i)", pluginId);
  1145. if (CarlaPlugin* const plugin = gStandalone.engine->getPlugin(pluginId))
  1146. return plugin->getCustomDataCount();
  1147. carla_stderr2("carla_get_custom_data_count(%i) - could not find plugin", pluginId);
  1148. return 0;
  1149. }
  1150. // -------------------------------------------------------------------------------------------------------------------
  1151. const char* carla_get_parameter_text(uint pluginId, uint32_t parameterId, float value)
  1152. {
  1153. CARLA_SAFE_ASSERT_RETURN(gStandalone.engine != nullptr, nullptr);
  1154. carla_debug("carla_get_parameter_text(%i, %i)", pluginId, parameterId);
  1155. static char textBuf[STR_MAX+1];
  1156. carla_zeroChar(textBuf, STR_MAX+1);
  1157. if (CarlaPlugin* const plugin = gStandalone.engine->getPlugin(pluginId))
  1158. {
  1159. if (parameterId < plugin->getParameterCount())
  1160. {
  1161. plugin->getParameterText(parameterId, value, textBuf);
  1162. return textBuf;
  1163. }
  1164. carla_stderr2("carla_get_parameter_text(%i, %i) - parameterId out of bounds", pluginId, parameterId);
  1165. return nullptr;
  1166. }
  1167. carla_stderr2("carla_get_parameter_text(%i, %i) - could not find plugin", pluginId, parameterId);
  1168. return nullptr;
  1169. }
  1170. const char* carla_get_program_name(uint pluginId, uint32_t programId)
  1171. {
  1172. CARLA_SAFE_ASSERT_RETURN(gStandalone.engine != nullptr, nullptr);
  1173. carla_debug("carla_get_program_name(%i, %i)", pluginId, programId);
  1174. static char programName[STR_MAX+1];
  1175. carla_zeroChar(programName, STR_MAX+1);
  1176. if (CarlaPlugin* const plugin = gStandalone.engine->getPlugin(pluginId))
  1177. {
  1178. if (programId < plugin->getProgramCount())
  1179. {
  1180. plugin->getProgramName(programId, programName);
  1181. return programName;
  1182. }
  1183. carla_stderr2("carla_get_program_name(%i, %i) - programId out of bounds", pluginId, programId);
  1184. return nullptr;
  1185. }
  1186. carla_stderr2("carla_get_program_name(%i, %i) - could not find plugin", pluginId, programId);
  1187. return nullptr;
  1188. }
  1189. const char* carla_get_midi_program_name(uint pluginId, uint32_t midiProgramId)
  1190. {
  1191. CARLA_SAFE_ASSERT_RETURN(gStandalone.engine != nullptr, nullptr);
  1192. carla_debug("carla_get_midi_program_name(%i, %i)", pluginId, midiProgramId);
  1193. static char midiProgramName[STR_MAX+1];
  1194. carla_zeroChar(midiProgramName, STR_MAX+1);
  1195. if (CarlaPlugin* const plugin = gStandalone.engine->getPlugin(pluginId))
  1196. {
  1197. if (midiProgramId < plugin->getMidiProgramCount())
  1198. {
  1199. plugin->getMidiProgramName(midiProgramId, midiProgramName);
  1200. return midiProgramName;
  1201. }
  1202. carla_stderr2("carla_get_midi_program_name(%i, %i) - midiProgramId out of bounds", pluginId, midiProgramId);
  1203. return nullptr;
  1204. }
  1205. carla_stderr2("carla_get_midi_program_name(%i, %i) - could not find plugin", pluginId, midiProgramId);
  1206. return nullptr;
  1207. }
  1208. const char* carla_get_real_plugin_name(uint pluginId)
  1209. {
  1210. CARLA_SAFE_ASSERT_RETURN(gStandalone.engine != nullptr, nullptr);
  1211. carla_debug("carla_get_real_plugin_name(%i)", pluginId);
  1212. static char realPluginName[STR_MAX+1];
  1213. carla_zeroChar(realPluginName, STR_MAX+1);
  1214. if (CarlaPlugin* const plugin = gStandalone.engine->getPlugin(pluginId))
  1215. {
  1216. plugin->getRealName(realPluginName);
  1217. return realPluginName;
  1218. }
  1219. carla_stderr2("carla_get_real_plugin_name(%i) - could not find plugin", pluginId);
  1220. return nullptr;
  1221. }
  1222. // -------------------------------------------------------------------------------------------------------------------
  1223. int32_t carla_get_current_program_index(uint pluginId)
  1224. {
  1225. CARLA_SAFE_ASSERT_RETURN(gStandalone.engine != nullptr, -1);
  1226. carla_debug("carla_get_current_program_index(%i)", pluginId);
  1227. if (CarlaPlugin* const plugin = gStandalone.engine->getPlugin(pluginId))
  1228. return plugin->getCurrentProgram();
  1229. carla_stderr2("carla_get_current_program_index(%i) - could not find plugin", pluginId);
  1230. return -1;
  1231. }
  1232. int32_t carla_get_current_midi_program_index(uint pluginId)
  1233. {
  1234. CARLA_SAFE_ASSERT_RETURN(gStandalone.engine != nullptr, -1);
  1235. carla_debug("carla_get_current_midi_program_index(%i)", pluginId);
  1236. if (CarlaPlugin* const plugin = gStandalone.engine->getPlugin(pluginId))
  1237. return plugin->getCurrentMidiProgram();
  1238. carla_stderr2("carla_get_current_midi_program_index(%i) - could not find plugin", pluginId);
  1239. return -1;
  1240. }
  1241. // -------------------------------------------------------------------------------------------------------------------
  1242. float carla_get_default_parameter_value(uint pluginId, uint32_t parameterId)
  1243. {
  1244. CARLA_SAFE_ASSERT_RETURN(gStandalone.engine != nullptr, 0.0f);
  1245. carla_debug("carla_get_default_parameter_value(%i, %i)", pluginId, parameterId);
  1246. if (CarlaPlugin* const plugin = gStandalone.engine->getPlugin(pluginId))
  1247. {
  1248. if (parameterId < plugin->getParameterCount())
  1249. return plugin->getParameterRanges(parameterId).def;
  1250. carla_stderr2("carla_get_default_parameter_value(%i, %i) - parameterId out of bounds", pluginId, parameterId);
  1251. return 0.0f;
  1252. }
  1253. carla_stderr2("carla_get_default_parameter_value(%i, %i) - could not find plugin", pluginId, parameterId);
  1254. return 0.0f;
  1255. }
  1256. float carla_get_current_parameter_value(uint pluginId, uint32_t parameterId)
  1257. {
  1258. CARLA_SAFE_ASSERT_RETURN(gStandalone.engine != nullptr, 0.0f);
  1259. carla_debug("carla_get_current_parameter_value(%i, %i)", pluginId, parameterId);
  1260. if (CarlaPlugin* const plugin = gStandalone.engine->getPlugin(pluginId))
  1261. {
  1262. if (parameterId < plugin->getParameterCount())
  1263. return plugin->getParameterValue(parameterId);
  1264. carla_stderr2("carla_get_current_parameter_value(%i, %i) - parameterId out of bounds", pluginId, parameterId);
  1265. return 0.0f;
  1266. }
  1267. carla_stderr2("carla_get_current_parameter_value(%i, %i) - could not find plugin", pluginId, parameterId);
  1268. return 0.0f;
  1269. }
  1270. // -------------------------------------------------------------------------------------------------------------------
  1271. float carla_get_input_peak_value(uint pluginId, bool isLeft)
  1272. {
  1273. CARLA_SAFE_ASSERT_RETURN(gStandalone.engine != nullptr, 0.0f);
  1274. return gStandalone.engine->getInputPeak(pluginId, isLeft);
  1275. }
  1276. float carla_get_output_peak_value(uint pluginId, bool isLeft)
  1277. {
  1278. CARLA_SAFE_ASSERT_RETURN(gStandalone.engine != nullptr, 0.0f);
  1279. return gStandalone.engine->getOutputPeak(pluginId, isLeft);
  1280. }
  1281. // -------------------------------------------------------------------------------------------------------------------
  1282. void carla_set_option(uint pluginId, uint option, bool yesNo)
  1283. {
  1284. CARLA_SAFE_ASSERT_RETURN(gStandalone.engine != nullptr,);
  1285. carla_debug("carla_set_option(%i, %i, %s)", pluginId, option, bool2str(yesNo));
  1286. if (CarlaPlugin* const plugin = gStandalone.engine->getPlugin(pluginId))
  1287. return plugin->setOption(option, yesNo);
  1288. carla_stderr2("carla_set_option(%i, %i, %s) - could not find plugin", pluginId, option, bool2str(yesNo));
  1289. }
  1290. void carla_set_active(uint pluginId, bool onOff)
  1291. {
  1292. CARLA_SAFE_ASSERT_RETURN(gStandalone.engine != nullptr,);
  1293. carla_debug("carla_set_active(%i, %s)", pluginId, bool2str(onOff));
  1294. if (CarlaPlugin* const plugin = gStandalone.engine->getPlugin(pluginId))
  1295. return plugin->setActive(onOff, true, false);
  1296. carla_stderr2("carla_set_active(%i, %s) - could not find plugin", pluginId, bool2str(onOff));
  1297. }
  1298. #ifndef BUILD_BRIDGE
  1299. void carla_set_drywet(uint pluginId, float value)
  1300. {
  1301. CARLA_SAFE_ASSERT_RETURN(gStandalone.engine != nullptr,);
  1302. carla_debug("carla_set_drywet(%i, %f)", pluginId, value);
  1303. if (CarlaPlugin* const plugin = gStandalone.engine->getPlugin(pluginId))
  1304. return plugin->setDryWet(value, true, false);
  1305. carla_stderr2("carla_set_drywet(%i, %f) - could not find plugin", pluginId, value);
  1306. }
  1307. void carla_set_volume(uint pluginId, float value)
  1308. {
  1309. CARLA_SAFE_ASSERT_RETURN(gStandalone.engine != nullptr,);
  1310. carla_debug("carla_set_volume(%i, %f)", pluginId, value);
  1311. if (CarlaPlugin* const plugin = gStandalone.engine->getPlugin(pluginId))
  1312. return plugin->setVolume(value, true, false);
  1313. carla_stderr2("carla_set_volume(%i, %f) - could not find plugin", pluginId, value);
  1314. }
  1315. void carla_set_balance_left(uint pluginId, float value)
  1316. {
  1317. CARLA_SAFE_ASSERT_RETURN(gStandalone.engine != nullptr,);
  1318. carla_debug("carla_set_balance_left(%i, %f)", pluginId, value);
  1319. if (CarlaPlugin* const plugin = gStandalone.engine->getPlugin(pluginId))
  1320. return plugin->setBalanceLeft(value, true, false);
  1321. carla_stderr2("carla_set_balance_left(%i, %f) - could not find plugin", pluginId, value);
  1322. }
  1323. void carla_set_balance_right(uint pluginId, float value)
  1324. {
  1325. CARLA_SAFE_ASSERT_RETURN(gStandalone.engine != nullptr,);
  1326. carla_debug("carla_set_balance_right(%i, %f)", pluginId, value);
  1327. if (CarlaPlugin* const plugin = gStandalone.engine->getPlugin(pluginId))
  1328. return plugin->setBalanceRight(value, true, false);
  1329. carla_stderr2("carla_set_balance_right(%i, %f) - could not find plugin", pluginId, value);
  1330. }
  1331. void carla_set_panning(uint pluginId, float value)
  1332. {
  1333. CARLA_SAFE_ASSERT_RETURN(gStandalone.engine != nullptr,);
  1334. carla_debug("carla_set_panning(%i, %f)", pluginId, value);
  1335. if (CarlaPlugin* const plugin = gStandalone.engine->getPlugin(pluginId))
  1336. return plugin->setPanning(value, true, false);
  1337. carla_stderr2("carla_set_panning(%i, %f) - could not find plugin", pluginId, value);
  1338. }
  1339. #endif
  1340. void carla_set_ctrl_channel(uint pluginId, int8_t channel)
  1341. {
  1342. CARLA_SAFE_ASSERT_RETURN(gStandalone.engine != nullptr,);
  1343. carla_debug("carla_set_ctrl_channel(%i, %i)", pluginId, channel);
  1344. if (CarlaPlugin* const plugin = gStandalone.engine->getPlugin(pluginId))
  1345. return plugin->setCtrlChannel(channel, true, false);
  1346. carla_stderr2("carla_set_ctrl_channel(%i, %i) - could not find plugin", pluginId, channel);
  1347. }
  1348. // -------------------------------------------------------------------------------------------------------------------
  1349. void carla_set_parameter_value(uint pluginId, uint32_t parameterId, float value)
  1350. {
  1351. CARLA_SAFE_ASSERT_RETURN(gStandalone.engine != nullptr,);
  1352. carla_debug("carla_set_parameter_value(%i, %i, %f)", pluginId, parameterId, value);
  1353. if (CarlaPlugin* const plugin = gStandalone.engine->getPlugin(pluginId))
  1354. {
  1355. if (parameterId < plugin->getParameterCount())
  1356. return plugin->setParameterValue(parameterId, value, true, true, false);
  1357. carla_stderr2("carla_set_parameter_value(%i, %i, %f) - parameterId out of bounds", pluginId, parameterId, value);
  1358. return;
  1359. }
  1360. carla_stderr2("carla_set_parameter_value(%i, %i, %f) - could not find plugin", pluginId, parameterId, value);
  1361. }
  1362. #ifndef BUILD_BRIDGE
  1363. void carla_set_parameter_midi_channel(uint pluginId, uint32_t parameterId, uint8_t channel)
  1364. {
  1365. CARLA_SAFE_ASSERT_RETURN(gStandalone.engine != nullptr,);
  1366. CARLA_SAFE_ASSERT_RETURN(channel >= MAX_MIDI_CHANNELS,);
  1367. carla_debug("carla_set_parameter_midi_channel(%i, %i, %i)", pluginId, parameterId, channel);
  1368. if (CarlaPlugin* const plugin = gStandalone.engine->getPlugin(pluginId))
  1369. {
  1370. if (parameterId < plugin->getParameterCount())
  1371. return plugin->setParameterMidiChannel(parameterId, channel, true, false);
  1372. carla_stderr2("carla_set_parameter_midi_channel(%i, %i, %i) - parameterId out of bounds", pluginId, parameterId, channel);
  1373. return;
  1374. }
  1375. carla_stderr2("carla_set_parameter_midi_channel(%i, %i, %i) - could not find plugin", pluginId, parameterId, channel);
  1376. }
  1377. void carla_set_parameter_midi_cc(uint pluginId, uint32_t parameterId, int16_t cc)
  1378. {
  1379. CARLA_SAFE_ASSERT_RETURN(gStandalone.engine != nullptr,);
  1380. CARLA_SAFE_ASSERT_RETURN(cc >= -1 && cc <= 0x5F,);
  1381. carla_debug("carla_set_parameter_midi_cc(%i, %i, %i)", pluginId, parameterId, cc);
  1382. if (CarlaPlugin* const plugin = gStandalone.engine->getPlugin(pluginId))
  1383. {
  1384. if (parameterId < plugin->getParameterCount())
  1385. return plugin->setParameterMidiCC(parameterId, cc, true, false);
  1386. carla_stderr2("carla_set_parameter_midi_cc(%i, %i, %i) - parameterId out of bounds", pluginId, parameterId, cc);
  1387. return;
  1388. }
  1389. carla_stderr2("carla_set_parameter_midi_cc(%i, %i, %i) - could not find plugin", pluginId, parameterId, cc);
  1390. }
  1391. #endif
  1392. // -------------------------------------------------------------------------------------------------------------------
  1393. void carla_set_program(uint pluginId, uint32_t programId)
  1394. {
  1395. CARLA_SAFE_ASSERT_RETURN(gStandalone.engine != nullptr,);
  1396. carla_debug("carla_set_program(%i, %i)", pluginId, programId);
  1397. if (CarlaPlugin* const plugin = gStandalone.engine->getPlugin(pluginId))
  1398. {
  1399. if (programId < plugin->getProgramCount())
  1400. return plugin->setProgram(static_cast<int32_t>(programId), true, true, false);
  1401. carla_stderr2("carla_set_program(%i, %i) - programId out of bounds", pluginId, programId);
  1402. return;
  1403. }
  1404. carla_stderr2("carla_set_program(%i, %i) - could not find plugin", pluginId, programId);
  1405. }
  1406. void carla_set_midi_program(uint pluginId, uint32_t midiProgramId)
  1407. {
  1408. CARLA_SAFE_ASSERT_RETURN(gStandalone.engine != nullptr,);
  1409. carla_debug("carla_set_midi_program(%i, %i)", pluginId, midiProgramId);
  1410. if (CarlaPlugin* const plugin = gStandalone.engine->getPlugin(pluginId))
  1411. {
  1412. if (midiProgramId < plugin->getMidiProgramCount())
  1413. return plugin->setMidiProgram(static_cast<int32_t>(midiProgramId), true, true, false);
  1414. carla_stderr2("carla_set_midi_program(%i, %i) - midiProgramId out of bounds", pluginId, midiProgramId);
  1415. return;
  1416. }
  1417. carla_stderr2("carla_set_midi_program(%i, %i) - could not find plugin", pluginId, midiProgramId);
  1418. }
  1419. // -------------------------------------------------------------------------------------------------------------------
  1420. void carla_set_custom_data(uint pluginId, const char* type, const char* key, const char* value)
  1421. {
  1422. CARLA_SAFE_ASSERT_RETURN(gStandalone.engine != nullptr,);
  1423. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  1424. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  1425. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  1426. carla_debug("carla_set_custom_data(%i, \"%s\", \"%s\", \"%s\")", pluginId, type, key, value);
  1427. if (CarlaPlugin* const plugin = gStandalone.engine->getPlugin(pluginId))
  1428. return plugin->setCustomData(type, key, value, true);
  1429. carla_stderr2("carla_set_custom_data(%i, \"%s\", \"%s\", \"%s\") - could not find plugin", pluginId, type, key, value);
  1430. }
  1431. void carla_set_chunk_data(uint pluginId, const char* chunkData)
  1432. {
  1433. CARLA_SAFE_ASSERT_RETURN(gStandalone.engine != nullptr,);
  1434. CARLA_SAFE_ASSERT_RETURN(chunkData != nullptr && chunkData[0] != '\0',);
  1435. carla_debug("carla_set_chunk_data(%i, \"%s\")", pluginId, chunkData);
  1436. if (CarlaPlugin* const plugin = gStandalone.engine->getPlugin(pluginId))
  1437. {
  1438. if (plugin->getOptionsEnabled() & CB::PLUGIN_OPTION_USE_CHUNKS)
  1439. return plugin->setChunkData(chunkData);
  1440. carla_stderr2("carla_set_chunk_data(%i, \"%s\") - plugin does not use chunks", pluginId, chunkData);
  1441. return;
  1442. }
  1443. carla_stderr2("carla_set_chunk_data(%i, \"%s\") - could not find plugin", pluginId, chunkData);
  1444. }
  1445. // -------------------------------------------------------------------------------------------------------------------
  1446. void carla_prepare_for_save(uint pluginId)
  1447. {
  1448. CARLA_SAFE_ASSERT_RETURN(gStandalone.engine != nullptr,);
  1449. carla_debug("carla_prepare_for_save(%i)", pluginId);
  1450. if (CarlaPlugin* const plugin = gStandalone.engine->getPlugin(pluginId))
  1451. return plugin->prepareForSave();
  1452. carla_stderr2("carla_prepare_for_save(%i) - could not find plugin", pluginId);
  1453. }
  1454. #ifndef BUILD_BRIDGE
  1455. void carla_send_midi_note(uint pluginId, uint8_t channel, uint8_t note, uint8_t velocity)
  1456. {
  1457. CARLA_SAFE_ASSERT_RETURN(gStandalone.engine != nullptr && gStandalone.engine->isRunning(),);
  1458. carla_debug("carla_send_midi_note(%i, %i, %i, %i)", pluginId, channel, note, velocity);
  1459. if (CarlaPlugin* const plugin = gStandalone.engine->getPlugin(pluginId))
  1460. return plugin->sendMidiSingleNote(channel, note, velocity, true, true, false);
  1461. carla_stderr2("carla_send_midi_note(%i, %i, %i, %i) - could not find plugin", pluginId, channel, note, velocity);
  1462. }
  1463. #endif
  1464. void carla_show_custom_ui(uint pluginId, bool yesNo)
  1465. {
  1466. CARLA_SAFE_ASSERT_RETURN(gStandalone.engine != nullptr,);
  1467. carla_debug("carla_show_custom_ui(%i, %s)", pluginId, bool2str(yesNo));
  1468. if (CarlaPlugin* const plugin = gStandalone.engine->getPlugin(pluginId))
  1469. return plugin->showCustomUI(yesNo);
  1470. carla_stderr2("carla_show_custom_ui(%i, %s) - could not find plugin", pluginId, bool2str(yesNo));
  1471. }
  1472. // -------------------------------------------------------------------------------------------------------------------
  1473. uint32_t carla_get_buffer_size()
  1474. {
  1475. CARLA_SAFE_ASSERT_RETURN(gStandalone.engine != nullptr, 0);
  1476. carla_debug("carla_get_buffer_size()");
  1477. return gStandalone.engine->getBufferSize();
  1478. }
  1479. double carla_get_sample_rate()
  1480. {
  1481. CARLA_SAFE_ASSERT_RETURN(gStandalone.engine != nullptr, 0.0);
  1482. carla_debug("carla_get_sample_rate()");
  1483. return gStandalone.engine->getSampleRate();
  1484. }
  1485. // -------------------------------------------------------------------------------------------------------------------
  1486. const char* carla_get_last_error()
  1487. {
  1488. carla_debug("carla_get_last_error()");
  1489. if (gStandalone.engine != nullptr)
  1490. return gStandalone.engine->getLastError();
  1491. return gStandalone.lastError;
  1492. }
  1493. const char* carla_get_host_osc_url_tcp()
  1494. {
  1495. carla_debug("carla_get_host_osc_url_tcp()");
  1496. if (gStandalone.engine == nullptr)
  1497. {
  1498. carla_stderr2("Engine is not running");
  1499. gStandalone.lastError = "Engine is not running";
  1500. return nullptr;
  1501. }
  1502. return gStandalone.engine->getOscServerPathTCP();
  1503. }
  1504. const char* carla_get_host_osc_url_udp()
  1505. {
  1506. carla_debug("carla_get_host_osc_url_udp()");
  1507. if (gStandalone.engine == nullptr)
  1508. {
  1509. carla_stderr2("Engine is not running");
  1510. gStandalone.lastError = "Engine is not running";
  1511. return nullptr;
  1512. }
  1513. return gStandalone.engine->getOscServerPathUDP();
  1514. }
  1515. // -------------------------------------------------------------------------------------------------------------------
  1516. #ifdef WANT_DSSI
  1517. # include "CarlaDssiUtils.cpp"
  1518. #endif
  1519. #include "CarlaStateUtils.cpp"
  1520. #if 0
  1521. int main(int argc, char* argv[])
  1522. {
  1523. return 0;
  1524. }
  1525. #endif
  1526. #if 0
  1527. //#include "CarlaOscUtils.hpp"
  1528. // -------------------------------------------------------------------------------------------------------------------
  1529. #define NSM_API_VERSION_MAJOR 1
  1530. #define NSM_API_VERSION_MINOR 2
  1531. class CarlaNSM
  1532. {
  1533. public:
  1534. CarlaNSM()
  1535. : fServerThread(nullptr),
  1536. fReplyAddr(nullptr),
  1537. fIsReady(false),
  1538. fIsOpened(false),
  1539. fIsSaved(false)
  1540. {
  1541. }
  1542. ~CarlaNSM()
  1543. {
  1544. if (fReplyAddr != nullptr)
  1545. lo_address_free(fReplyAddr);
  1546. if (fServerThread != nullptr)
  1547. {
  1548. lo_server_thread_stop(fServerThread);
  1549. lo_server_thread_del_method(fServerThread, "/reply", "ssss");
  1550. lo_server_thread_del_method(fServerThread, "/nsm/client/open", "sss");
  1551. lo_server_thread_del_method(fServerThread, "/nsm/client/save", "");
  1552. lo_server_thread_free(fServerThread);
  1553. }
  1554. }
  1555. void announce(const char* const url, const char* appName, const int pid)
  1556. {
  1557. lo_address const addr = lo_address_new_from_url(url);
  1558. if (addr == nullptr)
  1559. return;
  1560. const int proto = lo_address_get_protocol(addr);
  1561. if (fServerThread == nullptr)
  1562. {
  1563. // create new OSC thread
  1564. fServerThread = lo_server_thread_new_with_proto(nullptr, proto, error_handler);
  1565. // register message handlers and start OSC thread
  1566. lo_server_thread_add_method(fServerThread, "/reply", "ssss", _reply_handler, this);
  1567. lo_server_thread_add_method(fServerThread, "/nsm/client/open", "sss", _open_handler, this);
  1568. lo_server_thread_add_method(fServerThread, "/nsm/client/save", "", _save_handler, this);
  1569. lo_server_thread_start(fServerThread);
  1570. }
  1571. #ifndef BUILD_ANSI_TEST
  1572. lo_send_from(addr, lo_server_thread_get_server(fServerThread), LO_TT_IMMEDIATE, "/nsm/server/announce", "sssiii",
  1573. "Carla", ":switch:", appName, NSM_API_VERSION_MAJOR, NSM_API_VERSION_MINOR, pid);
  1574. #endif
  1575. lo_address_free(addr);
  1576. }
  1577. void ready()
  1578. {
  1579. fIsReady = true;
  1580. }
  1581. void replyOpen()
  1582. {
  1583. fIsOpened = true;
  1584. }
  1585. void replySave()
  1586. {
  1587. fIsSaved = true;
  1588. }
  1589. protected:
  1590. int handleReply(const char* const path, const char* const types, lo_arg** const argv, const int argc, const lo_message msg)
  1591. {
  1592. carla_debug("CarlaNSM::handleReply(%s, %i, %p, %s, %p)", path, argc, argv, types, msg);
  1593. if (fReplyAddr != nullptr)
  1594. lo_address_free(fReplyAddr);
  1595. fIsOpened = false;
  1596. fIsSaved = false;
  1597. char* const url = lo_address_get_url(lo_message_get_source(msg));
  1598. fReplyAddr = lo_address_new_from_url(url);
  1599. std::free(url);
  1600. const char* const method = &argv[0]->s;
  1601. const char* const smName = &argv[2]->s;
  1602. // wait max 6 secs for host to init
  1603. for (int i=0; i < 60 && ! fIsReady; ++i)
  1604. carla_msleep(100);
  1605. if (std::strcmp(method, "/nsm/server/announce") == 0 && gStandalone.callback != nullptr)
  1606. gStandalone.callback(gStandalone.callbackPtr, CB::ENGINE_CALLBACK_NSM_ANNOUNCE, 0, 0, 0, 0.0f, smName);
  1607. return 0;
  1608. #ifndef DEBUG
  1609. // unused
  1610. (void)path;
  1611. (void)types;
  1612. (void)argc;
  1613. #endif
  1614. }
  1615. int handleOpen(const char* const path, const char* const types, lo_arg** const argv, const int argc, const lo_message msg)
  1616. {
  1617. carla_debug("CarlaNSM::handleOpen(\"%s\", \"%s\", %p, %i, %p)", path, types, argv, argc, msg);
  1618. if (gStandalone.callback == nullptr)
  1619. return 1;
  1620. if (fServerThread == nullptr)
  1621. return 1;
  1622. if (fReplyAddr == nullptr)
  1623. return 1;
  1624. const char* const projectPath = &argv[0]->s;
  1625. const char* const clientId = &argv[2]->s;
  1626. char data[std::strlen(projectPath)+std::strlen(clientId)+2];
  1627. std::strcpy(data, projectPath);
  1628. std::strcat(data, ":");
  1629. std::strcat(data, clientId);
  1630. fIsOpened = false;
  1631. gStandalone.callback(nullptr, CB::ENGINE_CALLBACK_NSM_OPEN, 0, 0, 0, 0.0f, data);
  1632. // wait max 10 secs to open
  1633. for (int i=0; i < 100 && ! fIsOpened; ++i)
  1634. carla_msleep(100);
  1635. #ifndef BUILD_ANSI_TEST
  1636. if (fIsOpened)
  1637. lo_send_from(fReplyAddr, lo_server_thread_get_server(fServerThread), LO_TT_IMMEDIATE, "/reply", "ss", "/nsm/client/open", "OK");
  1638. #endif
  1639. return 0;
  1640. #ifndef DEBUG
  1641. // unused
  1642. (void)path;
  1643. (void)types;
  1644. (void)argc;
  1645. (void)msg;
  1646. #endif
  1647. }
  1648. int handleSave(const char* const path, const char* const types, lo_arg** const argv, const int argc, const lo_message msg)
  1649. {
  1650. carla_debug("CarlaNSM::handleSave(\"%s\", \"%s\", %p, %i, %p)", path, types, argv, argc, msg);
  1651. if (gStandalone.callback == nullptr)
  1652. return 1;
  1653. if (fServerThread == nullptr)
  1654. return 1;
  1655. if (fReplyAddr == nullptr)
  1656. return 1;
  1657. fIsSaved = false;
  1658. gStandalone.callback(nullptr, CB::ENGINE_CALLBACK_NSM_SAVE, 0, 0, 0, 0.0f, nullptr);
  1659. // wait max 10 secs to save
  1660. for (int i=0; i < 100 && ! fIsSaved; ++i)
  1661. carla_msleep(100);
  1662. #ifndef BUILD_ANSI_TEST
  1663. if (fIsSaved)
  1664. lo_send_from(fReplyAddr, lo_server_thread_get_server(fServerThread), LO_TT_IMMEDIATE, "/reply", "ss", "/nsm/client/save", "OK");
  1665. #endif
  1666. return 0;
  1667. #ifndef DEBUG
  1668. // unused
  1669. (void)path;
  1670. (void)types;
  1671. (void)argv;
  1672. (void)argc;
  1673. (void)msg;
  1674. #endif
  1675. }
  1676. private:
  1677. lo_server_thread fServerThread;
  1678. lo_address fReplyAddr;
  1679. bool fIsReady; // used to startup, only once
  1680. bool fIsOpened;
  1681. bool fIsSaved;
  1682. #define handlePtr ((CarlaNSM*)data)
  1683. static int _reply_handler(const char* path, const char* types, lo_arg** argv, int argc, lo_message msg, void* data)
  1684. {
  1685. return handlePtr->handleReply(path, types, argv, argc, msg);
  1686. }
  1687. static int _open_handler(const char* path, const char* types, lo_arg** argv, int argc, lo_message msg, void* data)
  1688. {
  1689. return handlePtr->handleOpen(path, types, argv, argc, msg);
  1690. }
  1691. static int _save_handler(const char* path, const char* types, lo_arg** argv, int argc, lo_message msg, void* data)
  1692. {
  1693. return handlePtr->handleSave(path, types, argv, argc, msg);
  1694. }
  1695. #undef handlePtr
  1696. static void error_handler(int num, const char* msg, const char* path)
  1697. {
  1698. carla_stderr2("CarlaNSM::error_handler(%i, \"%s\", \"%s\")", num, msg, path);
  1699. }
  1700. };
  1701. static CarlaNSM gCarlaNSM;
  1702. void carla_nsm_announce(const char* url, const char* appName, int pid)
  1703. {
  1704. CARLA_SAFE_ASSERT_RETURN(url != nullptr && url[0] != '\0',);
  1705. CARLA_SAFE_ASSERT_RETURN(appName != nullptr && appName[0] != '\0',);
  1706. CARLA_SAFE_ASSERT_RETURN(pid != 0,);
  1707. carla_debug("carla_nsm_announce(\"%s\", \"%s\", %i)", url, appName, pid);
  1708. gCarlaNSM.announce(url, appName, pid);
  1709. }
  1710. void carla_nsm_ready()
  1711. {
  1712. carla_debug("carla_nsm_ready()");
  1713. gCarlaNSM.ready();
  1714. }
  1715. void carla_nsm_reply_open()
  1716. {
  1717. carla_debug("carla_nsm_reply_open()");
  1718. gCarlaNSM.replyOpen();
  1719. }
  1720. void carla_nsm_reply_save()
  1721. {
  1722. carla_debug("carla_nsm_reply_save()");
  1723. gCarlaNSM.replySave();
  1724. }
  1725. #endif
  1726. // -------------------------------------------------------------------------------------------------------------------
  1727. //#include "CarlaLogThread.hpp"
  1728. //#if ! (defined(DEBUG) || defined(WANT_LOGS) || defined(BUILD_ANSI_TEST))
  1729. //# define WANT_LOGS
  1730. //#endif
  1731. //#ifdef WANT_LOGS
  1732. //static CarlaLogThread gLogThread;
  1733. //#endif
  1734. // -------------------------------------------------------------------------------------------------------------------