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.

2059 lines
72KB

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