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.

2175 lines
77KB

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