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.

2139 lines
70KB

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