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.

2276 lines
77KB

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