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.

2409 lines
86KB

  1. /*
  2. * Carla Standalone
  3. * Copyright (C) 2011-2022 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 "CarlaHostImpl.hpp"
  20. #include "CarlaMIDI.h"
  21. #include "CarlaEngineInit.hpp"
  22. #include "CarlaPlugin.hpp"
  23. #include "CarlaBackendUtils.hpp"
  24. #include "CarlaBase64Utils.hpp"
  25. #include "ThreadSafeFFTW.hpp"
  26. #include "water/files/File.h"
  27. #ifdef USING_JUCE
  28. # include "carla_juce/carla_juce.h"
  29. #endif
  30. #define CARLA_SAFE_ASSERT_WITH_LAST_ERROR_RETURN(cond, msg, ret) \
  31. if (! (cond)) { \
  32. carla_stderr2("%s: " msg, __FUNCTION__); \
  33. if (handle->isStandalone) \
  34. ((CarlaHostStandalone*)handle)->lastError = msg; \
  35. return ret; \
  36. }
  37. // -------------------------------------------------------------------------------------------------------------------
  38. // Always return a valid string ptr for standalone functions
  39. static const char* const gNullCharPtr = "";
  40. static void checkStringPtr(const char*& charPtr) noexcept
  41. {
  42. if (charPtr == nullptr)
  43. charPtr = gNullCharPtr;
  44. }
  45. // -------------------------------------------------------------------------------------------------------------------
  46. // Constructors
  47. _CarlaPluginInfo::_CarlaPluginInfo() noexcept
  48. : type(CB::PLUGIN_NONE),
  49. category(CB::PLUGIN_CATEGORY_NONE),
  50. hints(0x0),
  51. optionsAvailable(0x0),
  52. optionsEnabled(0x0),
  53. filename(gNullCharPtr),
  54. name(gNullCharPtr),
  55. label(gNullCharPtr),
  56. maker(gNullCharPtr),
  57. copyright(gNullCharPtr),
  58. iconName(gNullCharPtr),
  59. uniqueId(0) {}
  60. _CarlaPluginInfo::~_CarlaPluginInfo() noexcept
  61. {
  62. if (label != gNullCharPtr)
  63. delete[] label;
  64. if (maker != gNullCharPtr)
  65. delete[] maker;
  66. if (copyright != gNullCharPtr)
  67. delete[] copyright;
  68. }
  69. _CarlaParameterInfo::_CarlaParameterInfo() noexcept
  70. : name(gNullCharPtr),
  71. symbol(gNullCharPtr),
  72. unit(gNullCharPtr),
  73. comment(gNullCharPtr),
  74. groupName(gNullCharPtr),
  75. scalePointCount(0) {}
  76. _CarlaParameterInfo::~_CarlaParameterInfo() noexcept
  77. {
  78. if (name != gNullCharPtr)
  79. delete[] name;
  80. if (symbol != gNullCharPtr)
  81. delete[] symbol;
  82. if (unit != gNullCharPtr)
  83. delete[] unit;
  84. if (comment != gNullCharPtr)
  85. delete[] comment;
  86. if (groupName != gNullCharPtr)
  87. delete[] groupName;
  88. }
  89. _CarlaScalePointInfo::_CarlaScalePointInfo() noexcept
  90. : value(0.0f),
  91. label(gNullCharPtr) {}
  92. _CarlaScalePointInfo::~_CarlaScalePointInfo() noexcept
  93. {
  94. if (label != gNullCharPtr)
  95. delete[] label;
  96. }
  97. _CarlaTransportInfo::_CarlaTransportInfo() noexcept
  98. : playing(false),
  99. frame(0),
  100. bar(0),
  101. beat(0),
  102. tick(0),
  103. bpm(0.0) {}
  104. void _CarlaTransportInfo::clear() noexcept
  105. {
  106. playing = false;
  107. frame = 0;
  108. bar = 0;
  109. beat = 0;
  110. tick = 0;
  111. bpm = 0.0;
  112. }
  113. // --------------------------------------------------------------------------------------------------------------------
  114. using CARLA_BACKEND_NAMESPACE::CarlaPluginPtr;
  115. // --------------------------------------------------------------------------------------------------------------------
  116. uint carla_get_engine_driver_count()
  117. {
  118. carla_debug("carla_get_engine_driver_count()");
  119. return CarlaEngine::getDriverCount();
  120. }
  121. const char* carla_get_engine_driver_name(uint index)
  122. {
  123. carla_debug("carla_get_engine_driver_name(%i)", index);
  124. return CarlaEngine::getDriverName(index);
  125. }
  126. const char* const* carla_get_engine_driver_device_names(uint index)
  127. {
  128. carla_debug("carla_get_engine_driver_device_names(%i)", index);
  129. return CarlaEngine::getDriverDeviceNames(index);
  130. }
  131. const EngineDriverDeviceInfo* carla_get_engine_driver_device_info(uint index, const char* name)
  132. {
  133. CARLA_SAFE_ASSERT_RETURN(name != nullptr, nullptr);
  134. static EngineDriverDeviceInfo retDevInfo;
  135. static const uint32_t nullBufferSizes[] = { 0 };
  136. static const double nullSampleRates[] = { 0.0 };
  137. carla_debug("carla_get_engine_driver_device_info(%i, \"%s\")", index, name);
  138. if (const EngineDriverDeviceInfo* const devInfo = CarlaEngine::getDriverDeviceInfo(index, name))
  139. {
  140. retDevInfo.hints = devInfo->hints;
  141. retDevInfo.bufferSizes = (devInfo->bufferSizes != nullptr) ? devInfo->bufferSizes : nullBufferSizes;
  142. retDevInfo.sampleRates = (devInfo->sampleRates != nullptr) ? devInfo->sampleRates : nullSampleRates;
  143. }
  144. else
  145. {
  146. retDevInfo.hints = 0x0;
  147. retDevInfo.bufferSizes = nullBufferSizes;
  148. retDevInfo.sampleRates = nullSampleRates;
  149. }
  150. return &retDevInfo;
  151. }
  152. bool carla_show_engine_driver_device_control_panel(uint index, const char* name)
  153. {
  154. return CarlaEngine::showDriverDeviceControlPanel(index, name);
  155. }
  156. // --------------------------------------------------------------------------------------------------------------------
  157. CarlaHostHandle carla_standalone_host_init(void)
  158. {
  159. #ifdef CARLA_OS_UNIX
  160. static const ThreadSafeFFTW sThreadSafeFFTW;
  161. #endif
  162. static CarlaHostStandalone gStandalone;
  163. return &gStandalone;
  164. }
  165. CarlaEngine* carla_get_engine_from_handle(CarlaHostHandle handle)
  166. {
  167. carla_debug("carla_get_engine(%p)", handle);
  168. return handle->engine;
  169. }
  170. // --------------------------------------------------------------------------------------------------------------------
  171. static void carla_engine_init_common(const CarlaHostStandalone& standalone, CarlaEngine* const engine)
  172. {
  173. engine->setCallback(standalone.engineCallback, standalone.engineCallbackPtr);
  174. engine->setFileCallback(standalone.fileCallback, standalone.fileCallbackPtr);
  175. using water::File;
  176. const File waterBinaryDir(File::getSpecialLocation(File::currentExecutableFile).getParentDirectory());
  177. #ifdef BUILD_BRIDGE
  178. /*
  179. if (const char* const forceStereo = std::getenv("ENGINE_OPTION_FORCE_STEREO"))
  180. engine->setOption(CB::ENGINE_OPTION_FORCE_STEREO, (std::strcmp(forceStereo, "true") == 0) ? 1 : 0, nullptr);
  181. if (const char* const preferPluginBridges = std::getenv("ENGINE_OPTION_PREFER_PLUGIN_BRIDGES"))
  182. engine->setOption(CB::ENGINE_OPTION_PREFER_PLUGIN_BRIDGES, (std::strcmp(preferPluginBridges, "true") == 0) ? 1 : 0, nullptr);
  183. if (const char* const preferUiBridges = std::getenv("ENGINE_OPTION_PREFER_UI_BRIDGES"))
  184. engine->setOption(CB::ENGINE_OPTION_PREFER_UI_BRIDGES, (std::strcmp(preferUiBridges, "true") == 0) ? 1 : 0, nullptr);
  185. */
  186. if (const char* const uisAlwaysOnTop = std::getenv("ENGINE_OPTION_UIS_ALWAYS_ON_TOP"))
  187. engine->setOption(CB::ENGINE_OPTION_UIS_ALWAYS_ON_TOP, (std::strcmp(uisAlwaysOnTop, "true") == 0) ? 1 : 0, nullptr);
  188. if (const char* const maxParameters = std::getenv("ENGINE_OPTION_MAX_PARAMETERS"))
  189. engine->setOption(CB::ENGINE_OPTION_MAX_PARAMETERS, std::atoi(maxParameters), nullptr);
  190. if (const char* const resetXruns = std::getenv("ENGINE_OPTION_RESET_XRUNS"))
  191. engine->setOption(CB::ENGINE_OPTION_RESET_XRUNS, (std::strcmp(resetXruns, "true") == 0) ? 1 : 0, nullptr);
  192. if (const char* const uiBridgesTimeout = std::getenv("ENGINE_OPTION_UI_BRIDGES_TIMEOUT"))
  193. engine->setOption(CB::ENGINE_OPTION_UI_BRIDGES_TIMEOUT, std::atoi(uiBridgesTimeout), nullptr);
  194. if (const char* const pathAudio = std::getenv("ENGINE_OPTION_FILE_PATH_AUDIO"))
  195. engine->setOption(CB::ENGINE_OPTION_FILE_PATH, CB::FILE_AUDIO, pathAudio);
  196. if (const char* const pathMIDI = std::getenv("ENGINE_OPTION_FILE_PATH_MIDI"))
  197. engine->setOption(CB::ENGINE_OPTION_FILE_PATH, CB::FILE_MIDI, pathMIDI);
  198. if (const char* const pathLADSPA = std::getenv("ENGINE_OPTION_PLUGIN_PATH_LADSPA"))
  199. engine->setOption(CB::ENGINE_OPTION_PLUGIN_PATH, CB::PLUGIN_LADSPA, pathLADSPA);
  200. if (const char* const pathDSSI = std::getenv("ENGINE_OPTION_PLUGIN_PATH_DSSI"))
  201. engine->setOption(CB::ENGINE_OPTION_PLUGIN_PATH, CB::PLUGIN_DSSI, pathDSSI);
  202. if (const char* const pathLV2 = std::getenv("ENGINE_OPTION_PLUGIN_PATH_LV2"))
  203. engine->setOption(CB::ENGINE_OPTION_PLUGIN_PATH, CB::PLUGIN_LV2, pathLV2);
  204. if (const char* const pathVST2 = std::getenv("ENGINE_OPTION_PLUGIN_PATH_VST2"))
  205. engine->setOption(CB::ENGINE_OPTION_PLUGIN_PATH, CB::PLUGIN_VST2, pathVST2);
  206. if (const char* const pathVST3 = std::getenv("ENGINE_OPTION_PLUGIN_PATH_VST3"))
  207. engine->setOption(CB::ENGINE_OPTION_PLUGIN_PATH, CB::PLUGIN_VST3, pathVST3);
  208. if (const char* const pathSF2 = std::getenv("ENGINE_OPTION_PLUGIN_PATH_SF2"))
  209. engine->setOption(CB::ENGINE_OPTION_PLUGIN_PATH, CB::PLUGIN_SF2, pathSF2);
  210. if (const char* const pathSFZ = std::getenv("ENGINE_OPTION_PLUGIN_PATH_SFZ"))
  211. engine->setOption(CB::ENGINE_OPTION_PLUGIN_PATH, CB::PLUGIN_SFZ, pathSFZ);
  212. if (const char* const pathJSFX = std::getenv("ENGINE_OPTION_PLUGIN_PATH_JSFX"))
  213. engine->setOption(CB::ENGINE_OPTION_PLUGIN_PATH, CB::PLUGIN_JSFX, pathJSFX);
  214. if (const char* const binaryDir = std::getenv("ENGINE_OPTION_PATH_BINARIES"))
  215. engine->setOption(CB::ENGINE_OPTION_PATH_BINARIES, 0, binaryDir);
  216. else
  217. engine->setOption(CB::ENGINE_OPTION_PATH_BINARIES, 0, waterBinaryDir.getFullPathName().toRawUTF8());
  218. if (const char* const resourceDir = std::getenv("ENGINE_OPTION_PATH_RESOURCES"))
  219. engine->setOption(CB::ENGINE_OPTION_PATH_RESOURCES, 0, resourceDir);
  220. else
  221. engine->setOption(CB::ENGINE_OPTION_PATH_RESOURCES, 0, waterBinaryDir.getChildFile("resources").getFullPathName().toRawUTF8());
  222. if (const char* const preventBadBehaviour = std::getenv("ENGINE_OPTION_PREVENT_BAD_BEHAVIOUR"))
  223. engine->setOption(CB::ENGINE_OPTION_PREVENT_BAD_BEHAVIOUR, (std::strcmp(preventBadBehaviour, "true") == 0) ? 1 : 0, nullptr);
  224. if (const char* const frontendWinId = std::getenv("ENGINE_OPTION_FRONTEND_WIN_ID"))
  225. engine->setOption(CB::ENGINE_OPTION_FRONTEND_WIN_ID, 0, frontendWinId);
  226. #else
  227. engine->setOption(CB::ENGINE_OPTION_FORCE_STEREO, standalone.engineOptions.forceStereo ? 1 : 0, nullptr);
  228. engine->setOption(CB::ENGINE_OPTION_PREFER_PLUGIN_BRIDGES, standalone.engineOptions.preferPluginBridges ? 1 : 0, nullptr);
  229. engine->setOption(CB::ENGINE_OPTION_PREFER_UI_BRIDGES, standalone.engineOptions.preferUiBridges ? 1 : 0, nullptr);
  230. engine->setOption(CB::ENGINE_OPTION_UIS_ALWAYS_ON_TOP, standalone.engineOptions.uisAlwaysOnTop ? 1 : 0, nullptr);
  231. engine->setOption(CB::ENGINE_OPTION_MAX_PARAMETERS, static_cast<int>(standalone.engineOptions.maxParameters), nullptr);
  232. engine->setOption(CB::ENGINE_OPTION_RESET_XRUNS, standalone.engineOptions.resetXruns ? 1 : 0, nullptr);
  233. engine->setOption(CB::ENGINE_OPTION_UI_BRIDGES_TIMEOUT, static_cast<int>(standalone.engineOptions.uiBridgesTimeout), nullptr);
  234. engine->setOption(CB::ENGINE_OPTION_AUDIO_BUFFER_SIZE, static_cast<int>(standalone.engineOptions.audioBufferSize), nullptr);
  235. engine->setOption(CB::ENGINE_OPTION_AUDIO_SAMPLE_RATE, static_cast<int>(standalone.engineOptions.audioSampleRate), nullptr);
  236. engine->setOption(CB::ENGINE_OPTION_AUDIO_TRIPLE_BUFFER, standalone.engineOptions.audioTripleBuffer ? 1 : 0, nullptr);
  237. if (standalone.engineOptions.audioDriver != nullptr)
  238. engine->setOption(CB::ENGINE_OPTION_AUDIO_DRIVER, 0, standalone.engineOptions.audioDriver);
  239. if (standalone.engineOptions.audioDevice != nullptr)
  240. engine->setOption(CB::ENGINE_OPTION_AUDIO_DEVICE, 0, standalone.engineOptions.audioDevice);
  241. engine->setOption(CB::ENGINE_OPTION_OSC_ENABLED, standalone.engineOptions.oscEnabled, nullptr);
  242. engine->setOption(CB::ENGINE_OPTION_OSC_PORT_TCP, standalone.engineOptions.oscPortTCP, nullptr);
  243. engine->setOption(CB::ENGINE_OPTION_OSC_PORT_UDP, standalone.engineOptions.oscPortUDP, nullptr);
  244. if (standalone.engineOptions.pathAudio != nullptr)
  245. engine->setOption(CB::ENGINE_OPTION_FILE_PATH, CB::FILE_AUDIO, standalone.engineOptions.pathAudio);
  246. if (standalone.engineOptions.pathMIDI != nullptr)
  247. engine->setOption(CB::ENGINE_OPTION_FILE_PATH, CB::FILE_MIDI, standalone.engineOptions.pathMIDI);
  248. if (standalone.engineOptions.pathLADSPA != nullptr)
  249. engine->setOption(CB::ENGINE_OPTION_PLUGIN_PATH, CB::PLUGIN_LADSPA, standalone.engineOptions.pathLADSPA);
  250. if (standalone.engineOptions.pathDSSI != nullptr)
  251. engine->setOption(CB::ENGINE_OPTION_PLUGIN_PATH, CB::PLUGIN_DSSI, standalone.engineOptions.pathDSSI);
  252. if (standalone.engineOptions.pathLV2 != nullptr)
  253. engine->setOption(CB::ENGINE_OPTION_PLUGIN_PATH, CB::PLUGIN_LV2, standalone.engineOptions.pathLV2);
  254. if (standalone.engineOptions.pathVST2 != nullptr)
  255. engine->setOption(CB::ENGINE_OPTION_PLUGIN_PATH, CB::PLUGIN_VST2, standalone.engineOptions.pathVST2);
  256. if (standalone.engineOptions.pathVST3 != nullptr)
  257. engine->setOption(CB::ENGINE_OPTION_PLUGIN_PATH, CB::PLUGIN_VST3, standalone.engineOptions.pathVST3);
  258. if (standalone.engineOptions.pathSF2 != nullptr)
  259. engine->setOption(CB::ENGINE_OPTION_PLUGIN_PATH, CB::PLUGIN_SF2, standalone.engineOptions.pathSF2);
  260. if (standalone.engineOptions.pathSFZ != nullptr)
  261. engine->setOption(CB::ENGINE_OPTION_PLUGIN_PATH, CB::PLUGIN_SFZ, standalone.engineOptions.pathSFZ);
  262. if (standalone.engineOptions.pathJSFX != nullptr)
  263. engine->setOption(CB::ENGINE_OPTION_PLUGIN_PATH, CB::PLUGIN_JSFX, standalone.engineOptions.pathJSFX);
  264. if (standalone.engineOptions.binaryDir != nullptr && standalone.engineOptions.binaryDir[0] != '\0')
  265. engine->setOption(CB::ENGINE_OPTION_PATH_BINARIES, 0, standalone.engineOptions.binaryDir);
  266. else
  267. engine->setOption(CB::ENGINE_OPTION_PATH_BINARIES, 0, waterBinaryDir.getFullPathName().toRawUTF8());
  268. if (standalone.engineOptions.resourceDir != nullptr && standalone.engineOptions.resourceDir[0] != '\0')
  269. engine->setOption(CB::ENGINE_OPTION_PATH_RESOURCES, 0, standalone.engineOptions.resourceDir);
  270. engine->setOption(CB::ENGINE_OPTION_PREVENT_BAD_BEHAVIOUR, standalone.engineOptions.preventBadBehaviour ? 1 : 0, nullptr);
  271. engine->setOption(CB::ENGINE_OPTION_FRONTEND_BACKGROUND_COLOR, static_cast<int>(standalone.engineOptions.bgColor), nullptr);
  272. engine->setOption(CB::ENGINE_OPTION_FRONTEND_FOREGROUND_COLOR, static_cast<int>(standalone.engineOptions.fgColor), nullptr);
  273. engine->setOption(CB::ENGINE_OPTION_FRONTEND_UI_SCALE, static_cast<int>(standalone.engineOptions.uiScale * 1000.0f), nullptr);
  274. if (standalone.engineOptions.frontendWinId != 0)
  275. {
  276. char strBuf[STR_MAX+1];
  277. strBuf[STR_MAX] = '\0';
  278. std::snprintf(strBuf, STR_MAX, P_UINTPTR, standalone.engineOptions.frontendWinId);
  279. engine->setOption(CB::ENGINE_OPTION_FRONTEND_WIN_ID, 0, strBuf);
  280. }
  281. else
  282. {
  283. engine->setOption(CB::ENGINE_OPTION_FRONTEND_WIN_ID, 0, "0");
  284. }
  285. # ifndef CARLA_OS_WIN
  286. if (standalone.engineOptions.wine.executable != nullptr && standalone.engineOptions.wine.executable[0] != '\0')
  287. engine->setOption(CB::ENGINE_OPTION_WINE_EXECUTABLE, 0, standalone.engineOptions.wine.executable);
  288. engine->setOption(CB::ENGINE_OPTION_WINE_AUTO_PREFIX, standalone.engineOptions.wine.autoPrefix ? 1 : 0, nullptr);
  289. if (standalone.engineOptions.wine.fallbackPrefix != nullptr && standalone.engineOptions.wine.fallbackPrefix[0] != '\0')
  290. engine->setOption(CB::ENGINE_OPTION_WINE_FALLBACK_PREFIX, 0, standalone.engineOptions.wine.fallbackPrefix);
  291. engine->setOption(CB::ENGINE_OPTION_WINE_RT_PRIO_ENABLED, standalone.engineOptions.wine.rtPrio ? 1 : 0, nullptr);
  292. engine->setOption(CB::ENGINE_OPTION_WINE_BASE_RT_PRIO, standalone.engineOptions.wine.baseRtPrio, nullptr);
  293. engine->setOption(CB::ENGINE_OPTION_WINE_SERVER_RT_PRIO, standalone.engineOptions.wine.serverRtPrio, nullptr);
  294. # endif
  295. engine->setOption(CB::ENGINE_OPTION_CLIENT_NAME_PREFIX, 0, standalone.engineOptions.clientNamePrefix);
  296. engine->setOption(CB::ENGINE_OPTION_PLUGINS_ARE_STANDALONE, standalone.engineOptions.pluginsAreStandalone, nullptr);
  297. #endif // BUILD_BRIDGE
  298. }
  299. bool carla_engine_init(CarlaHostHandle handle, const char* driverName, const char* clientName)
  300. {
  301. CARLA_SAFE_ASSERT_RETURN(driverName != nullptr && driverName[0] != '\0', false);
  302. CARLA_SAFE_ASSERT_RETURN(clientName != nullptr && clientName[0] != '\0', false);
  303. carla_debug("carla_engine_init(%p, \"%s\", \"%s\")", handle, driverName, clientName);
  304. CARLA_SAFE_ASSERT_WITH_LAST_ERROR_RETURN(handle->isStandalone, "Must be a standalone host handle", false);
  305. CARLA_SAFE_ASSERT_WITH_LAST_ERROR_RETURN(handle->engine == nullptr, "Engine is already initialized", false);
  306. #ifdef CARLA_OS_WIN
  307. carla_setenv("WINEASIO_CLIENT_NAME", clientName);
  308. #endif
  309. #ifdef USING_JUCE
  310. CarlaJUCE::initialiseJuce_GUI();
  311. #endif
  312. CarlaHostStandalone& shandle((CarlaHostStandalone&)*handle);
  313. CarlaEngine* const engine = CarlaEngine::newDriverByName(driverName);
  314. CARLA_SAFE_ASSERT_WITH_LAST_ERROR_RETURN(engine != nullptr, "The selected audio driver is not available", false);
  315. shandle.engine = engine;
  316. #ifdef BUILD_BRIDGE
  317. if (std::getenv("CARLA_BRIDGE_DUMMY") != nullptr)
  318. {
  319. // engine->setOption(CB::ENGINE_OPTION_PROCESS_MODE, CB::ENGINE_PROCESS_MODE_PATCHBAY, nullptr);
  320. engine->setOption(CB::ENGINE_OPTION_PROCESS_MODE, CB::ENGINE_PROCESS_MODE_CONTINUOUS_RACK, nullptr);
  321. engine->setOption(CB::ENGINE_OPTION_TRANSPORT_MODE, CB::ENGINE_TRANSPORT_MODE_INTERNAL, nullptr);
  322. engine->setOption(CB::ENGINE_OPTION_AUDIO_BUFFER_SIZE, 4096, nullptr);
  323. engine->setOption(CB::ENGINE_OPTION_AUDIO_SAMPLE_RATE, 48000, nullptr);
  324. }
  325. else
  326. {
  327. engine->setOption(CB::ENGINE_OPTION_PROCESS_MODE, CB::ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS, nullptr);
  328. engine->setOption(CB::ENGINE_OPTION_TRANSPORT_MODE, CB::ENGINE_TRANSPORT_MODE_JACK, nullptr);
  329. }
  330. engine->setOption(CB::ENGINE_OPTION_FORCE_STEREO, false, nullptr);
  331. engine->setOption(CB::ENGINE_OPTION_PREFER_PLUGIN_BRIDGES, false, nullptr);
  332. engine->setOption(CB::ENGINE_OPTION_PREFER_UI_BRIDGES, false, nullptr);
  333. #else
  334. engine->setOption(CB::ENGINE_OPTION_PROCESS_MODE, static_cast<int>(shandle.engineOptions.processMode), nullptr);
  335. engine->setOption(CB::ENGINE_OPTION_TRANSPORT_MODE, static_cast<int>(shandle.engineOptions.transportMode), shandle.engineOptions.transportExtra);
  336. #endif
  337. carla_engine_init_common(shandle, engine);
  338. if (engine->init(clientName))
  339. {
  340. #ifdef CARLA_CAN_USE_LOG_THREAD
  341. if (shandle.logThreadEnabled && std::getenv("CARLA_LOGS_DISABLED") == nullptr)
  342. shandle.logThread.init();
  343. #endif
  344. shandle.lastError = "No error";
  345. return true;
  346. }
  347. else
  348. {
  349. shandle.lastError = engine->getLastError();
  350. shandle.engine = nullptr;
  351. delete engine;
  352. #ifdef USING_JUCE
  353. CarlaJUCE::shutdownJuce_GUI();
  354. #endif
  355. return false;
  356. }
  357. }
  358. #ifdef BUILD_BRIDGE
  359. bool carla_engine_init_bridge(CarlaHostHandle handle,
  360. const char audioBaseName[6+1],
  361. const char rtClientBaseName[6+1],
  362. const char nonRtClientBaseName[6+1],
  363. const char nonRtServerBaseName[6+1],
  364. const char* const clientName)
  365. {
  366. CARLA_SAFE_ASSERT_RETURN(audioBaseName != nullptr && audioBaseName[0] != '\0', false);
  367. CARLA_SAFE_ASSERT_RETURN(rtClientBaseName != nullptr && rtClientBaseName[0] != '\0', false);
  368. CARLA_SAFE_ASSERT_RETURN(nonRtClientBaseName != nullptr && nonRtClientBaseName[0] != '\0', false);
  369. CARLA_SAFE_ASSERT_RETURN(nonRtServerBaseName != nullptr && nonRtServerBaseName[0] != '\0', false);
  370. CARLA_SAFE_ASSERT_RETURN(clientName != nullptr && clientName[0] != '\0', false);
  371. carla_debug("carla_engine_init_bridge(%p, \"%s\", \"%s\", \"%s\", \"%s\", \"%s\")",
  372. handle, audioBaseName, rtClientBaseName, nonRtClientBaseName, nonRtServerBaseName, clientName);
  373. CARLA_SAFE_ASSERT_WITH_LAST_ERROR_RETURN(handle->isStandalone, "Must be a standalone host handle", false);
  374. CARLA_SAFE_ASSERT_WITH_LAST_ERROR_RETURN(handle->engine == nullptr, "Engine is already initialized", false);
  375. CarlaScopedPointer<CarlaEngine> engine(CB::EngineInit::newBridge(audioBaseName,
  376. rtClientBaseName,
  377. nonRtClientBaseName,
  378. nonRtServerBaseName));
  379. CARLA_SAFE_ASSERT_WITH_LAST_ERROR_RETURN(engine != nullptr, "The selected audio driver is not available", false);
  380. engine->setOption(CB::ENGINE_OPTION_PROCESS_MODE, CB::ENGINE_PROCESS_MODE_BRIDGE, nullptr);
  381. engine->setOption(CB::ENGINE_OPTION_TRANSPORT_MODE, CB::ENGINE_TRANSPORT_MODE_BRIDGE, nullptr);
  382. CarlaHostStandalone& shandle((CarlaHostStandalone&)*handle);
  383. carla_engine_init_common(shandle, engine);
  384. if (engine->init(clientName))
  385. {
  386. shandle.lastError = "No error";
  387. shandle.engine = engine.release();
  388. return true;
  389. }
  390. else
  391. {
  392. shandle.lastError = engine->getLastError();
  393. return false;
  394. }
  395. }
  396. #endif
  397. bool carla_engine_close(CarlaHostHandle handle)
  398. {
  399. carla_debug("carla_engine_close(%p)", handle);
  400. CARLA_SAFE_ASSERT_WITH_LAST_ERROR_RETURN(handle->isStandalone, "Must be a standalone host handle", false);
  401. CARLA_SAFE_ASSERT_WITH_LAST_ERROR_RETURN(handle->engine != nullptr, "Engine is not initialized", false);
  402. CarlaHostStandalone& shandle((CarlaHostStandalone&)*handle);
  403. CarlaEngine* const engine = shandle.engine;
  404. engine->setAboutToClose();
  405. engine->removeAllPlugins();
  406. const bool closed = engine->close();
  407. if (! closed)
  408. shandle.lastError = engine->getLastError();
  409. #ifdef CARLA_CAN_USE_LOG_THREAD
  410. shandle.logThread.stop();
  411. #endif
  412. shandle.engine = nullptr;
  413. delete engine;
  414. #ifdef USING_JUCE
  415. CarlaJUCE::shutdownJuce_GUI();
  416. #endif
  417. return closed;
  418. }
  419. void carla_engine_idle(CarlaHostHandle handle)
  420. {
  421. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr && handle->isStandalone,);
  422. handle->engine->idle();
  423. #ifdef USING_JUCE
  424. if (handle->isStandalone)
  425. CarlaJUCE::idleJuce_GUI();
  426. #endif
  427. }
  428. bool carla_is_engine_running(CarlaHostHandle handle)
  429. {
  430. return (handle->engine != nullptr && handle->engine->isRunning());
  431. }
  432. const CarlaRuntimeEngineInfo* carla_get_runtime_engine_info(CarlaHostHandle handle)
  433. {
  434. static CarlaRuntimeEngineInfo retInfo;
  435. // reset
  436. retInfo.load = 0.0f;
  437. retInfo.xruns = 0;
  438. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr, &retInfo);
  439. retInfo.load = handle->engine->getDSPLoad();
  440. retInfo.xruns = handle->engine->getTotalXruns();
  441. return &retInfo;
  442. }
  443. #ifndef BUILD_BRIDGE
  444. const CarlaRuntimeEngineDriverDeviceInfo* carla_get_runtime_engine_driver_device_info(CarlaHostHandle handle)
  445. {
  446. static CarlaRuntimeEngineDriverDeviceInfo retInfo;
  447. // reset
  448. retInfo.name = gNullCharPtr;
  449. retInfo.hints = 0x0;
  450. retInfo.bufferSize = 0;
  451. retInfo.bufferSizes = nullptr;
  452. retInfo.sampleRate = 0.0;
  453. retInfo.sampleRates = nullptr;
  454. const char* audioDriver;
  455. const char* audioDevice;
  456. if (CarlaEngine* const engine = handle->engine)
  457. {
  458. audioDriver = engine->getCurrentDriverName();
  459. audioDevice = engine->getOptions().audioDevice;
  460. retInfo.bufferSize = engine->getBufferSize();
  461. retInfo.sampleRate = engine->getSampleRate();
  462. }
  463. else if (handle->isStandalone)
  464. {
  465. CarlaHostStandalone& shandle((CarlaHostStandalone&)*handle);
  466. audioDriver = shandle.engineOptions.audioDriver;
  467. audioDevice = shandle.engineOptions.audioDevice;
  468. retInfo.bufferSize = shandle.engineOptions.audioBufferSize;
  469. retInfo.sampleRate = shandle.engineOptions.audioSampleRate;
  470. }
  471. else
  472. {
  473. return &retInfo;
  474. }
  475. CARLA_SAFE_ASSERT_RETURN(audioDriver != nullptr, &retInfo);
  476. CARLA_SAFE_ASSERT_RETURN(audioDevice != nullptr, &retInfo);
  477. uint index = 0;
  478. uint count = CarlaEngine::getDriverCount();
  479. for (; index<count; ++index)
  480. {
  481. const char* const testDriverName = CarlaEngine::getDriverName(index);
  482. CARLA_SAFE_ASSERT_CONTINUE(testDriverName != nullptr);
  483. if (std::strcmp(testDriverName, audioDriver) == 0)
  484. break;
  485. }
  486. CARLA_SAFE_ASSERT_RETURN(index != count, &retInfo);
  487. const EngineDriverDeviceInfo* const devInfo = CarlaEngine::getDriverDeviceInfo(index, audioDevice);
  488. CARLA_SAFE_ASSERT_RETURN(devInfo != nullptr, &retInfo);
  489. retInfo.name = audioDevice;
  490. retInfo.hints = devInfo->hints;
  491. retInfo.bufferSizes = devInfo->bufferSizes;
  492. retInfo.sampleRates = devInfo->sampleRates;
  493. return &retInfo;
  494. }
  495. bool carla_set_engine_buffer_size_and_sample_rate(CarlaHostHandle handle, uint bufferSize, double sampleRate)
  496. {
  497. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr, false);
  498. carla_debug("carla_set_engine_buffer_size_and_sample_rate(%p, %u, %f)", handle, bufferSize, sampleRate);
  499. return handle->engine->setBufferSizeAndSampleRate(bufferSize, sampleRate);
  500. }
  501. bool carla_show_engine_device_control_panel(CarlaHostHandle handle)
  502. {
  503. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr, false);
  504. carla_debug("carla_show_engine_device_control_panel(%p)", handle);
  505. return handle->engine->showDeviceControlPanel();
  506. }
  507. #endif // BUILD_BRIDGE
  508. void carla_clear_engine_xruns(CarlaHostHandle handle)
  509. {
  510. if (handle->engine != nullptr)
  511. handle->engine->clearXruns();
  512. }
  513. void carla_cancel_engine_action(CarlaHostHandle handle)
  514. {
  515. if (handle->engine != nullptr)
  516. handle->engine->setActionCanceled(true);
  517. }
  518. bool carla_set_engine_about_to_close(CarlaHostHandle handle)
  519. {
  520. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr, true);
  521. carla_debug("carla_set_engine_about_to_close(%p)", handle);
  522. return handle->engine->setAboutToClose();
  523. }
  524. void carla_set_engine_callback(CarlaHostHandle handle, EngineCallbackFunc func, void* ptr)
  525. {
  526. carla_debug("carla_set_engine_callback(%p, %p, %p)", handle, func, ptr);
  527. if (handle->isStandalone)
  528. {
  529. CarlaHostStandalone& shandle((CarlaHostStandalone&)*handle);
  530. shandle.engineCallback = func;
  531. shandle.engineCallbackPtr = ptr;
  532. #ifdef CARLA_CAN_USE_LOG_THREAD
  533. shandle.logThread.setCallback(func, ptr);
  534. #endif
  535. }
  536. if (handle->engine != nullptr)
  537. handle->engine->setCallback(func, ptr);
  538. }
  539. void carla_set_engine_option(CarlaHostHandle handle, EngineOption option, int value, const char* valueStr)
  540. {
  541. carla_debug("carla_set_engine_option(%p, %i:%s, %i, \"%s\")",
  542. handle, option, CB::EngineOption2Str(option), value, valueStr);
  543. if (handle->isStandalone)
  544. {
  545. CarlaHostStandalone& shandle((CarlaHostStandalone&)*handle);
  546. switch (option)
  547. {
  548. case CB::ENGINE_OPTION_DEBUG:
  549. break;
  550. case CB::ENGINE_OPTION_PROCESS_MODE:
  551. CARLA_SAFE_ASSERT_RETURN(value >= CB::ENGINE_PROCESS_MODE_SINGLE_CLIENT && value < CB::ENGINE_PROCESS_MODE_BRIDGE,);
  552. shandle.engineOptions.processMode = static_cast<CB::EngineProcessMode>(value);
  553. break;
  554. case CB::ENGINE_OPTION_TRANSPORT_MODE:
  555. CARLA_SAFE_ASSERT_RETURN(value >= CB::ENGINE_TRANSPORT_MODE_DISABLED && value <= CB::ENGINE_TRANSPORT_MODE_BRIDGE,);
  556. // jack transport cannot be disabled in multi-client
  557. if (shandle.engineOptions.processMode == CB::ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS
  558. && value != CB::ENGINE_TRANSPORT_MODE_JACK)
  559. {
  560. shandle.engineOptions.transportMode = CB::ENGINE_TRANSPORT_MODE_JACK;
  561. if (shandle.engineCallback != nullptr)
  562. shandle.engineCallback(shandle.engineCallbackPtr,
  563. CB::ENGINE_CALLBACK_TRANSPORT_MODE_CHANGED,
  564. 0,
  565. CB::ENGINE_TRANSPORT_MODE_JACK,
  566. 0, 0, 0.0f,
  567. shandle.engineOptions.transportExtra);
  568. }
  569. else
  570. {
  571. shandle.engineOptions.transportMode = static_cast<CB::EngineTransportMode>(value);
  572. }
  573. delete[] shandle.engineOptions.transportExtra;
  574. if (value != CB::ENGINE_TRANSPORT_MODE_DISABLED && valueStr != nullptr)
  575. shandle.engineOptions.transportExtra = carla_strdup_safe(valueStr);
  576. else
  577. shandle.engineOptions.transportExtra = nullptr;
  578. break;
  579. case CB::ENGINE_OPTION_FORCE_STEREO:
  580. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  581. shandle.engineOptions.forceStereo = (value != 0);
  582. break;
  583. case CB::ENGINE_OPTION_PREFER_PLUGIN_BRIDGES:
  584. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  585. shandle.engineOptions.preferPluginBridges = (value != 0);
  586. break;
  587. case CB::ENGINE_OPTION_PREFER_UI_BRIDGES:
  588. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  589. shandle.engineOptions.preferUiBridges = (value != 0);
  590. break;
  591. case CB::ENGINE_OPTION_UIS_ALWAYS_ON_TOP:
  592. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  593. shandle.engineOptions.uisAlwaysOnTop = (value != 0);
  594. break;
  595. case CB::ENGINE_OPTION_MAX_PARAMETERS:
  596. CARLA_SAFE_ASSERT_RETURN(value >= 0,);
  597. shandle.engineOptions.maxParameters = static_cast<uint>(value);
  598. break;
  599. case CB::ENGINE_OPTION_RESET_XRUNS:
  600. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  601. shandle.engineOptions.resetXruns = (value != 0);
  602. break;
  603. case CB::ENGINE_OPTION_UI_BRIDGES_TIMEOUT:
  604. CARLA_SAFE_ASSERT_RETURN(value >= 0,);
  605. shandle.engineOptions.uiBridgesTimeout = static_cast<uint>(value);
  606. break;
  607. case CB::ENGINE_OPTION_AUDIO_BUFFER_SIZE:
  608. CARLA_SAFE_ASSERT_RETURN(value >= 8,);
  609. shandle.engineOptions.audioBufferSize = static_cast<uint>(value);
  610. break;
  611. case CB::ENGINE_OPTION_AUDIO_SAMPLE_RATE:
  612. CARLA_SAFE_ASSERT_RETURN(value >= 22050,);
  613. shandle.engineOptions.audioSampleRate = static_cast<uint>(value);
  614. break;
  615. case CB::ENGINE_OPTION_AUDIO_TRIPLE_BUFFER:
  616. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  617. shandle.engineOptions.audioTripleBuffer = (value != 0);
  618. break;
  619. case CB::ENGINE_OPTION_AUDIO_DRIVER:
  620. CARLA_SAFE_ASSERT_RETURN(valueStr != nullptr,);
  621. if (shandle.engineOptions.audioDriver != nullptr)
  622. delete[] shandle.engineOptions.audioDriver;
  623. shandle.engineOptions.audioDriver = carla_strdup_safe(valueStr);
  624. break;
  625. case CB::ENGINE_OPTION_AUDIO_DEVICE:
  626. CARLA_SAFE_ASSERT_RETURN(valueStr != nullptr,);
  627. if (shandle.engineOptions.audioDevice != nullptr)
  628. delete[] shandle.engineOptions.audioDevice;
  629. shandle.engineOptions.audioDevice = carla_strdup_safe(valueStr);
  630. break;
  631. #ifndef BUILD_BRIDGE
  632. case CB::ENGINE_OPTION_OSC_ENABLED:
  633. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  634. shandle.engineOptions.oscEnabled = (value != 0);
  635. break;
  636. case CB::ENGINE_OPTION_OSC_PORT_TCP:
  637. CARLA_SAFE_ASSERT_RETURN(value <= 0 || value >= 1024,);
  638. shandle.engineOptions.oscPortTCP = value;
  639. break;
  640. case CB::ENGINE_OPTION_OSC_PORT_UDP:
  641. CARLA_SAFE_ASSERT_RETURN(value <= 0 || value >= 1024,);
  642. shandle.engineOptions.oscPortUDP = value;
  643. break;
  644. #endif
  645. case CB::ENGINE_OPTION_FILE_PATH:
  646. CARLA_SAFE_ASSERT_RETURN(value > CB::FILE_NONE,);
  647. CARLA_SAFE_ASSERT_RETURN(value <= CB::FILE_MIDI,);
  648. CARLA_SAFE_ASSERT_RETURN(valueStr != nullptr,);
  649. switch (value)
  650. {
  651. case CB::FILE_AUDIO:
  652. if (shandle.engineOptions.pathAudio != nullptr)
  653. delete[] shandle.engineOptions.pathAudio;
  654. shandle.engineOptions.pathAudio = carla_strdup_safe(valueStr);
  655. break;
  656. case CB::FILE_MIDI:
  657. if (shandle.engineOptions.pathMIDI != nullptr)
  658. delete[] shandle.engineOptions.pathMIDI;
  659. shandle.engineOptions.pathMIDI = carla_strdup_safe(valueStr);
  660. break;
  661. }
  662. break;
  663. case CB::ENGINE_OPTION_PLUGIN_PATH:
  664. CARLA_SAFE_ASSERT_RETURN(value > CB::PLUGIN_NONE,);
  665. CARLA_SAFE_ASSERT_RETURN(value <= CB::PLUGIN_JSFX,);
  666. CARLA_SAFE_ASSERT_RETURN(valueStr != nullptr,);
  667. switch (value)
  668. {
  669. case CB::PLUGIN_LADSPA:
  670. if (shandle.engineOptions.pathLADSPA != nullptr)
  671. delete[] shandle.engineOptions.pathLADSPA;
  672. shandle.engineOptions.pathLADSPA = carla_strdup_safe(valueStr);
  673. break;
  674. case CB::PLUGIN_DSSI:
  675. if (shandle.engineOptions.pathDSSI != nullptr)
  676. delete[] shandle.engineOptions.pathDSSI;
  677. shandle.engineOptions.pathDSSI = carla_strdup_safe(valueStr);
  678. break;
  679. case CB::PLUGIN_LV2:
  680. if (shandle.engineOptions.pathLV2 != nullptr)
  681. delete[] shandle.engineOptions.pathLV2;
  682. shandle.engineOptions.pathLV2 = carla_strdup_safe(valueStr);
  683. break;
  684. case CB::PLUGIN_VST2:
  685. if (shandle.engineOptions.pathVST2 != nullptr)
  686. delete[] shandle.engineOptions.pathVST2;
  687. shandle.engineOptions.pathVST2 = carla_strdup_safe(valueStr);
  688. break;
  689. case CB::PLUGIN_VST3:
  690. if (shandle.engineOptions.pathVST3 != nullptr)
  691. delete[] shandle.engineOptions.pathVST3;
  692. shandle.engineOptions.pathVST3 = carla_strdup_safe(valueStr);
  693. break;
  694. case CB::PLUGIN_SF2:
  695. if (shandle.engineOptions.pathSF2 != nullptr)
  696. delete[] shandle.engineOptions.pathSF2;
  697. shandle.engineOptions.pathSF2 = carla_strdup_safe(valueStr);
  698. break;
  699. case CB::PLUGIN_SFZ:
  700. if (shandle.engineOptions.pathSFZ != nullptr)
  701. delete[] shandle.engineOptions.pathSFZ;
  702. shandle.engineOptions.pathSFZ = carla_strdup_safe(valueStr);
  703. break;
  704. case CB::PLUGIN_JSFX:
  705. if (shandle.engineOptions.pathJSFX != nullptr)
  706. delete[] shandle.engineOptions.pathJSFX;
  707. shandle.engineOptions.pathJSFX = carla_strdup_safe(valueStr);
  708. break;
  709. }
  710. break;
  711. case CB::ENGINE_OPTION_PATH_BINARIES:
  712. CARLA_SAFE_ASSERT_RETURN(valueStr != nullptr && valueStr[0] != '\0',);
  713. if (shandle.engineOptions.binaryDir != nullptr)
  714. delete[] shandle.engineOptions.binaryDir;
  715. shandle.engineOptions.binaryDir = carla_strdup_safe(valueStr);
  716. break;
  717. case CB::ENGINE_OPTION_PATH_RESOURCES:
  718. CARLA_SAFE_ASSERT_RETURN(valueStr != nullptr && valueStr[0] != '\0',);
  719. if (shandle.engineOptions.resourceDir != nullptr)
  720. delete[] shandle.engineOptions.resourceDir;
  721. shandle.engineOptions.resourceDir = carla_strdup_safe(valueStr);
  722. break;
  723. case CB::ENGINE_OPTION_PREVENT_BAD_BEHAVIOUR:
  724. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  725. shandle.engineOptions.preventBadBehaviour = (value != 0);
  726. break;
  727. case CB::ENGINE_OPTION_FRONTEND_BACKGROUND_COLOR:
  728. shandle.engineOptions.bgColor = static_cast<uint>(value);
  729. break;
  730. case CB::ENGINE_OPTION_FRONTEND_FOREGROUND_COLOR:
  731. shandle.engineOptions.fgColor = static_cast<uint>(value);
  732. break;
  733. case CB::ENGINE_OPTION_FRONTEND_UI_SCALE:
  734. CARLA_SAFE_ASSERT_RETURN(value > 0,);
  735. shandle.engineOptions.uiScale = static_cast<float>(value) / 1000;
  736. break;
  737. case CB::ENGINE_OPTION_FRONTEND_WIN_ID: {
  738. CARLA_SAFE_ASSERT_RETURN(valueStr != nullptr && valueStr[0] != '\0',);
  739. const long long winId(std::strtoll(valueStr, nullptr, 16));
  740. CARLA_SAFE_ASSERT_RETURN(winId >= 0,);
  741. shandle.engineOptions.frontendWinId = static_cast<uintptr_t>(winId);
  742. } break;
  743. #if !defined(BUILD_BRIDGE_ALTERNATIVE_ARCH) && !defined(CARLA_OS_WIN)
  744. case CB::ENGINE_OPTION_WINE_EXECUTABLE:
  745. CARLA_SAFE_ASSERT_RETURN(valueStr != nullptr && valueStr[0] != '\0',);
  746. if (shandle.engineOptions.wine.executable != nullptr)
  747. delete[] shandle.engineOptions.wine.executable;
  748. shandle.engineOptions.wine.executable = carla_strdup_safe(valueStr);
  749. break;
  750. case CB::ENGINE_OPTION_WINE_AUTO_PREFIX:
  751. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  752. shandle.engineOptions.wine.autoPrefix = (value != 0);
  753. break;
  754. case CB::ENGINE_OPTION_WINE_FALLBACK_PREFIX:
  755. CARLA_SAFE_ASSERT_RETURN(valueStr != nullptr && valueStr[0] != '\0',);
  756. if (shandle.engineOptions.wine.fallbackPrefix != nullptr)
  757. delete[] shandle.engineOptions.wine.fallbackPrefix;
  758. shandle.engineOptions.wine.fallbackPrefix = carla_strdup_safe(valueStr);
  759. break;
  760. case CB::ENGINE_OPTION_WINE_RT_PRIO_ENABLED:
  761. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  762. shandle.engineOptions.wine.rtPrio = (value != 0);
  763. break;
  764. case CB::ENGINE_OPTION_WINE_BASE_RT_PRIO:
  765. CARLA_SAFE_ASSERT_RETURN(value >= 1 && value <= 89,);
  766. shandle.engineOptions.wine.baseRtPrio = value;
  767. break;
  768. case CB::ENGINE_OPTION_WINE_SERVER_RT_PRIO:
  769. CARLA_SAFE_ASSERT_RETURN(value >= 1 && value <= 99,);
  770. shandle.engineOptions.wine.serverRtPrio = value;
  771. break;
  772. #endif
  773. #ifndef BUILD_BRIDGE
  774. case CB::ENGINE_OPTION_DEBUG_CONSOLE_OUTPUT:
  775. #ifdef CARLA_CAN_USE_LOG_THREAD
  776. shandle.logThreadEnabled = (value != 0);
  777. #endif
  778. break;
  779. #endif
  780. case CB::ENGINE_OPTION_CLIENT_NAME_PREFIX:
  781. if (shandle.engineOptions.clientNamePrefix != nullptr)
  782. delete[] shandle.engineOptions.clientNamePrefix;
  783. shandle.engineOptions.clientNamePrefix = valueStr != nullptr && valueStr[0] != '\0'
  784. ? carla_strdup_safe(valueStr)
  785. : nullptr;
  786. break;
  787. case CB::ENGINE_OPTION_PLUGINS_ARE_STANDALONE:
  788. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  789. shandle.engineOptions.pluginsAreStandalone = (value != 0);
  790. break;
  791. }
  792. }
  793. if (handle->engine != nullptr)
  794. handle->engine->setOption(option, value, valueStr);
  795. }
  796. void carla_set_file_callback(CarlaHostHandle handle, FileCallbackFunc func, void* ptr)
  797. {
  798. carla_debug("carla_set_file_callback(%p, %p, %p)", handle, func, ptr);
  799. if (handle->isStandalone)
  800. {
  801. CarlaHostStandalone& shandle((CarlaHostStandalone&)*handle);
  802. shandle.fileCallback = func;
  803. shandle.fileCallbackPtr = ptr;
  804. }
  805. if (handle->engine != nullptr)
  806. handle->engine->setFileCallback(func, ptr);
  807. }
  808. // --------------------------------------------------------------------------------------------------------------------
  809. bool carla_load_file(CarlaHostHandle handle, const char* filename)
  810. {
  811. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', false);
  812. CARLA_SAFE_ASSERT_WITH_LAST_ERROR_RETURN(handle->engine != nullptr, "Engine is not initialized", false);
  813. carla_debug("carla_load_file(%p, \"%s\")", handle, filename);
  814. return handle->engine->loadFile(filename);
  815. }
  816. bool carla_load_project(CarlaHostHandle handle, const char* filename)
  817. {
  818. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', false);
  819. CARLA_SAFE_ASSERT_WITH_LAST_ERROR_RETURN(handle->engine != nullptr, "Engine is not initialized", false);
  820. carla_debug("carla_load_project(%p, \"%s\")", handle, filename);
  821. return handle->engine->loadProject(filename, true);
  822. }
  823. bool carla_save_project(CarlaHostHandle handle, const char* filename)
  824. {
  825. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', false);
  826. CARLA_SAFE_ASSERT_WITH_LAST_ERROR_RETURN(handle->engine != nullptr, "Engine is not initialized", false);
  827. carla_debug("carla_save_project(%p, \"%s\")", handle, filename);
  828. return handle->engine->saveProject(filename, true);
  829. }
  830. #ifndef BUILD_BRIDGE
  831. const char* carla_get_current_project_folder(CarlaHostHandle handle)
  832. {
  833. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr, gNullCharPtr);
  834. carla_debug("carla_get_current_project_folder(%p)", handle);
  835. if (const char* const ret = handle->engine->getCurrentProjectFolder())
  836. return ret;
  837. return gNullCharPtr;
  838. }
  839. const char* carla_get_current_project_filename(CarlaHostHandle handle)
  840. {
  841. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr && handle->isStandalone, gNullCharPtr);
  842. carla_debug("carla_get_current_project_filename(%p)", handle);
  843. if (const char* const ret = handle->engine->getCurrentProjectFilename())
  844. return ret;
  845. return gNullCharPtr;
  846. }
  847. void carla_clear_project_filename(CarlaHostHandle handle)
  848. {
  849. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr,);
  850. carla_debug("carla_clear_project_filename(%p)", handle);
  851. handle->engine->clearCurrentProjectFilename();
  852. }
  853. // --------------------------------------------------------------------------------------------------------------------
  854. bool carla_patchbay_connect(CarlaHostHandle handle, bool external, uint groupIdA, uint portIdA, uint groupIdB, uint portIdB)
  855. {
  856. CARLA_SAFE_ASSERT_WITH_LAST_ERROR_RETURN(handle->engine != nullptr, "Engine is not initialized", false);
  857. carla_debug("carla_patchbay_connect(%p, %s, %u, %u, %u, %u)",
  858. handle, bool2str(external), groupIdA, portIdA, groupIdB, portIdB);
  859. return handle->engine->patchbayConnect(external, groupIdA, portIdA, groupIdB, portIdB);
  860. }
  861. bool carla_patchbay_disconnect(CarlaHostHandle handle, bool external, uint connectionId)
  862. {
  863. CARLA_SAFE_ASSERT_WITH_LAST_ERROR_RETURN(handle->engine != nullptr, "Engine is not initialized", false);
  864. carla_debug("carla_patchbay_disconnect(%p, %s, %i)", handle, bool2str(external), connectionId);
  865. return handle->engine->patchbayDisconnect(external, connectionId);
  866. }
  867. bool carla_patchbay_set_group_pos(CarlaHostHandle handle, bool external, uint groupId, int x1, int y1, int x2, int y2)
  868. {
  869. CARLA_SAFE_ASSERT_WITH_LAST_ERROR_RETURN(handle->engine != nullptr && handle->engine->isRunning(),
  870. "Engine is not running", false);
  871. carla_debug("carla_patchbay_set_group_pos(%p, %s, %u, %i, %i, %i, %i)",
  872. handle, bool2str(external), groupId, x1, y1, x2, y2);
  873. if (handle->engine->isAboutToClose())
  874. return true;
  875. return handle->engine->patchbaySetGroupPos(false, true, external, groupId, x1, y1, x2, y2);
  876. }
  877. bool carla_patchbay_refresh(CarlaHostHandle handle, bool external)
  878. {
  879. CARLA_SAFE_ASSERT_WITH_LAST_ERROR_RETURN(handle->engine != nullptr, "Engine is not initialized", false);
  880. carla_debug("carla_patchbay_refresh(%p, %s)", handle, bool2str(external));
  881. return handle->engine->patchbayRefresh(true, false, external);
  882. }
  883. // --------------------------------------------------------------------------------------------------------------------
  884. void carla_transport_play(CarlaHostHandle handle)
  885. {
  886. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr && handle->engine->isRunning(),);
  887. carla_debug("carla_transport_play(%p)", handle);
  888. handle->engine->transportPlay();
  889. }
  890. void carla_transport_pause(CarlaHostHandle handle)
  891. {
  892. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr && handle->engine->isRunning(),);
  893. carla_debug("carla_transport_pause(%p)", handle);
  894. handle->engine->transportPause();
  895. }
  896. void carla_transport_bpm(CarlaHostHandle handle, double bpm)
  897. {
  898. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr && handle->engine->isRunning(),);
  899. carla_debug("carla_transport_bpm(%p, %f)", handle, bpm);
  900. handle->engine->transportBPM(bpm);
  901. }
  902. void carla_transport_relocate(CarlaHostHandle handle, uint64_t frame)
  903. {
  904. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr && handle->engine->isRunning(),);
  905. carla_debug("carla_transport_relocate(%p, %i)", handle, frame);
  906. handle->engine->transportRelocate(frame);
  907. }
  908. uint64_t carla_get_current_transport_frame(CarlaHostHandle handle)
  909. {
  910. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr && handle->engine->isRunning(), 0);
  911. return handle->engine->getTimeInfo().frame;
  912. }
  913. const CarlaTransportInfo* carla_get_transport_info(CarlaHostHandle handle)
  914. {
  915. static CarlaTransportInfo retTransInfo;
  916. retTransInfo.clear();
  917. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr && handle->engine->isRunning(), &retTransInfo);
  918. const CB::EngineTimeInfo& timeInfo(handle->engine->getTimeInfo());
  919. retTransInfo.playing = timeInfo.playing;
  920. retTransInfo.frame = timeInfo.frame;
  921. if (timeInfo.bbt.valid)
  922. {
  923. retTransInfo.bar = timeInfo.bbt.bar;
  924. retTransInfo.beat = timeInfo.bbt.beat;
  925. retTransInfo.tick = static_cast<int32_t>(timeInfo.bbt.tick + 0.5);
  926. retTransInfo.bpm = timeInfo.bbt.beatsPerMinute;
  927. }
  928. return &retTransInfo;
  929. }
  930. #endif
  931. // --------------------------------------------------------------------------------------------------------------------
  932. uint32_t carla_get_current_plugin_count(CarlaHostHandle handle)
  933. {
  934. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr, 0);
  935. carla_debug("carla_get_current_plugin_count(%p)", handle);
  936. return handle->engine->getCurrentPluginCount();
  937. }
  938. uint32_t carla_get_max_plugin_number(CarlaHostHandle handle)
  939. {
  940. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr, 0);
  941. carla_debug("carla_get_max_plugin_number(%p)", handle);
  942. return handle->engine->getMaxPluginNumber();
  943. }
  944. // --------------------------------------------------------------------------------------------------------------------
  945. bool carla_add_plugin(CarlaHostHandle handle,
  946. BinaryType btype, PluginType ptype,
  947. const char* filename, const char* name, const char* label, int64_t uniqueId,
  948. const void* extraPtr, uint options)
  949. {
  950. CARLA_SAFE_ASSERT_WITH_LAST_ERROR_RETURN(handle->engine != nullptr, "Engine is not initialized", false);
  951. carla_debug("carla_add_plugin(%p, %i:%s, %i:%s, \"%s\", \"%s\", \"%s\", " P_INT64 ", %p, %u)",
  952. handle,
  953. btype, CB::BinaryType2Str(btype),
  954. ptype, CB::PluginType2Str(ptype),
  955. filename, name, label, uniqueId, extraPtr, options);
  956. return handle->engine->addPlugin(btype, ptype, filename, name, label, uniqueId, extraPtr, options);
  957. }
  958. bool carla_remove_plugin(CarlaHostHandle handle, uint pluginId)
  959. {
  960. CARLA_SAFE_ASSERT_WITH_LAST_ERROR_RETURN(handle->engine != nullptr, "Engine is not initialized", false);
  961. carla_debug("carla_remove_plugin(%p, %i)", handle, pluginId);
  962. return handle->engine->removePlugin(pluginId);
  963. }
  964. bool carla_remove_all_plugins(CarlaHostHandle handle)
  965. {
  966. CARLA_SAFE_ASSERT_WITH_LAST_ERROR_RETURN(handle->engine != nullptr, "Engine is not initialized", false);
  967. carla_debug("carla_remove_all_plugins(%p)", handle);
  968. return handle->engine->removeAllPlugins();
  969. }
  970. #ifndef BUILD_BRIDGE
  971. bool carla_rename_plugin(CarlaHostHandle handle, uint pluginId, const char* newName)
  972. {
  973. CARLA_SAFE_ASSERT_RETURN(newName != nullptr && newName[0] != '\0', false);
  974. CARLA_SAFE_ASSERT_WITH_LAST_ERROR_RETURN(handle->engine != nullptr, "Engine is not initialized", false);
  975. carla_debug("carla_rename_plugin(%p, %i, \"%s\")", handle, pluginId, newName);
  976. return handle->engine->renamePlugin(pluginId, newName);
  977. }
  978. bool carla_clone_plugin(CarlaHostHandle handle, uint pluginId)
  979. {
  980. CARLA_SAFE_ASSERT_WITH_LAST_ERROR_RETURN(handle->engine != nullptr, "Engine is not initialized", false);
  981. carla_debug("carla_clone_plugin(%p, %i)", handle, pluginId);
  982. return handle->engine->clonePlugin(pluginId);
  983. }
  984. bool carla_replace_plugin(CarlaHostHandle handle, uint pluginId)
  985. {
  986. CARLA_SAFE_ASSERT_WITH_LAST_ERROR_RETURN(handle->engine != nullptr, "Engine is not initialized", false);
  987. carla_debug("carla_replace_plugin(%p, %i)", handle, pluginId);
  988. return handle->engine->replacePlugin(pluginId);
  989. }
  990. bool carla_switch_plugins(CarlaHostHandle handle, uint pluginIdA, uint pluginIdB)
  991. {
  992. CARLA_SAFE_ASSERT_RETURN(pluginIdA != pluginIdB, false);
  993. CARLA_SAFE_ASSERT_WITH_LAST_ERROR_RETURN(handle->engine != nullptr, "Engine is not initialized", false);
  994. carla_debug("carla_switch_plugins(%p, %i, %i)", handle, pluginIdA, pluginIdB);
  995. return handle->engine->switchPlugins(pluginIdA, pluginIdB);
  996. }
  997. #endif
  998. // --------------------------------------------------------------------------------------------------------------------
  999. bool carla_load_plugin_state(CarlaHostHandle handle, uint pluginId, const char* filename)
  1000. {
  1001. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', false);
  1002. CARLA_SAFE_ASSERT_WITH_LAST_ERROR_RETURN(handle->engine != nullptr
  1003. && handle->engine->isRunning(), "Engine is not running", false);
  1004. if (const CarlaPluginPtr plugin = handle->engine->getPlugin(pluginId))
  1005. return plugin->loadStateFromFile(filename);
  1006. return false;
  1007. }
  1008. bool carla_save_plugin_state(CarlaHostHandle handle, uint pluginId, const char* filename)
  1009. {
  1010. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', false);
  1011. CARLA_SAFE_ASSERT_WITH_LAST_ERROR_RETURN(handle->engine != nullptr, "Engine is not initialized", false);
  1012. if (const CarlaPluginPtr plugin = handle->engine->getPlugin(pluginId))
  1013. return plugin->saveStateToFile(filename);
  1014. return false;
  1015. }
  1016. bool carla_export_plugin_lv2(CarlaHostHandle handle, uint pluginId, const char* lv2path)
  1017. {
  1018. CARLA_SAFE_ASSERT_RETURN(lv2path != nullptr && lv2path[0] != '\0', false);
  1019. CARLA_SAFE_ASSERT_WITH_LAST_ERROR_RETURN(handle->engine != nullptr, "Engine is not initialized", false);
  1020. if (const CarlaPluginPtr plugin = handle->engine->getPlugin(pluginId))
  1021. return plugin->exportAsLV2(lv2path);
  1022. return false;
  1023. }
  1024. // --------------------------------------------------------------------------------------------------------------------
  1025. const CarlaPluginInfo* carla_get_plugin_info(CarlaHostHandle handle, uint pluginId)
  1026. {
  1027. static CarlaPluginInfo retInfo;
  1028. // reset
  1029. retInfo.type = CB::PLUGIN_NONE;
  1030. retInfo.category = CB::PLUGIN_CATEGORY_NONE;
  1031. retInfo.hints = 0x0;
  1032. retInfo.optionsAvailable = 0x0;
  1033. retInfo.optionsEnabled = 0x0;
  1034. retInfo.filename = gNullCharPtr;
  1035. retInfo.name = gNullCharPtr;
  1036. retInfo.iconName = gNullCharPtr;
  1037. retInfo.uniqueId = 0;
  1038. // cleanup
  1039. if (retInfo.label != gNullCharPtr)
  1040. {
  1041. delete[] retInfo.label;
  1042. retInfo.label = gNullCharPtr;
  1043. }
  1044. if (retInfo.maker != gNullCharPtr)
  1045. {
  1046. delete[] retInfo.maker;
  1047. retInfo.maker = gNullCharPtr;
  1048. }
  1049. if (retInfo.copyright != gNullCharPtr)
  1050. {
  1051. delete[] retInfo.copyright;
  1052. retInfo.copyright = gNullCharPtr;
  1053. }
  1054. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr, &retInfo);
  1055. if (const CarlaPluginPtr plugin = handle->engine->getPlugin(pluginId))
  1056. {
  1057. char strBuf[STR_MAX+1];
  1058. carla_zeroChars(strBuf, STR_MAX+1);
  1059. retInfo.type = plugin->getType();
  1060. retInfo.category = plugin->getCategory();
  1061. retInfo.hints = plugin->getHints();
  1062. retInfo.filename = plugin->getFilename();
  1063. retInfo.name = plugin->getName();
  1064. retInfo.iconName = plugin->getIconName();
  1065. retInfo.uniqueId = plugin->getUniqueId();
  1066. retInfo.optionsAvailable = plugin->getOptionsAvailable();
  1067. retInfo.optionsEnabled = plugin->getOptionsEnabled();
  1068. if (plugin->getLabel(strBuf))
  1069. retInfo.label = carla_strdup_safe(strBuf);
  1070. if (plugin->getMaker(strBuf))
  1071. retInfo.maker = carla_strdup_safe(strBuf);
  1072. if (plugin->getCopyright(strBuf))
  1073. retInfo.copyright = carla_strdup_safe(strBuf);
  1074. checkStringPtr(retInfo.filename);
  1075. checkStringPtr(retInfo.name);
  1076. checkStringPtr(retInfo.iconName);
  1077. checkStringPtr(retInfo.label);
  1078. checkStringPtr(retInfo.maker);
  1079. checkStringPtr(retInfo.copyright);
  1080. }
  1081. return &retInfo;
  1082. }
  1083. const CarlaPortCountInfo* carla_get_audio_port_count_info(CarlaHostHandle handle, uint pluginId)
  1084. {
  1085. static CarlaPortCountInfo retInfo;
  1086. carla_zeroStruct(retInfo);
  1087. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr, &retInfo);
  1088. if (const CarlaPluginPtr plugin = handle->engine->getPlugin(pluginId))
  1089. {
  1090. retInfo.ins = plugin->getAudioInCount();
  1091. retInfo.outs = plugin->getAudioOutCount();
  1092. }
  1093. return &retInfo;
  1094. }
  1095. const CarlaPortCountInfo* carla_get_midi_port_count_info(CarlaHostHandle handle, uint pluginId)
  1096. {
  1097. static CarlaPortCountInfo retInfo;
  1098. carla_zeroStruct(retInfo);
  1099. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr, &retInfo);
  1100. if (const CarlaPluginPtr plugin = handle->engine->getPlugin(pluginId))
  1101. {
  1102. retInfo.ins = plugin->getMidiInCount();
  1103. retInfo.outs = plugin->getMidiOutCount();
  1104. }
  1105. return &retInfo;
  1106. }
  1107. const CarlaPortCountInfo* carla_get_parameter_count_info(CarlaHostHandle handle, uint pluginId)
  1108. {
  1109. static CarlaPortCountInfo retInfo;
  1110. carla_zeroStruct(retInfo);
  1111. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr, &retInfo);
  1112. if (const CarlaPluginPtr plugin = handle->engine->getPlugin(pluginId))
  1113. plugin->getParameterCountInfo(retInfo.ins, retInfo.outs);
  1114. return &retInfo;
  1115. }
  1116. const CarlaParameterInfo* carla_get_parameter_info(CarlaHostHandle handle, uint pluginId, uint32_t parameterId)
  1117. {
  1118. static CarlaParameterInfo retInfo;
  1119. // reset
  1120. retInfo.scalePointCount = 0;
  1121. // cleanup
  1122. if (retInfo.name != gNullCharPtr)
  1123. {
  1124. delete[] retInfo.name;
  1125. retInfo.name = gNullCharPtr;
  1126. }
  1127. if (retInfo.symbol != gNullCharPtr)
  1128. {
  1129. delete[] retInfo.symbol;
  1130. retInfo.symbol = gNullCharPtr;
  1131. }
  1132. if (retInfo.unit != gNullCharPtr)
  1133. {
  1134. delete[] retInfo.unit;
  1135. retInfo.unit = gNullCharPtr;
  1136. }
  1137. if (retInfo.comment != gNullCharPtr)
  1138. {
  1139. delete[] retInfo.comment;
  1140. retInfo.comment = gNullCharPtr;
  1141. }
  1142. if (retInfo.groupName != gNullCharPtr)
  1143. {
  1144. delete[] retInfo.groupName;
  1145. retInfo.groupName = gNullCharPtr;
  1146. }
  1147. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr, &retInfo);
  1148. if (const CarlaPluginPtr plugin = handle->engine->getPlugin(pluginId))
  1149. {
  1150. char strBuf[STR_MAX+1];
  1151. carla_zeroChars(strBuf, STR_MAX+1);
  1152. retInfo.scalePointCount = plugin->getParameterScalePointCount(parameterId);
  1153. if (plugin->getParameterName(parameterId, strBuf))
  1154. {
  1155. retInfo.name = carla_strdup_safe(strBuf);
  1156. carla_zeroChars(strBuf, STR_MAX+1);
  1157. }
  1158. if (plugin->getParameterSymbol(parameterId, strBuf))
  1159. {
  1160. retInfo.symbol = carla_strdup_safe(strBuf);
  1161. carla_zeroChars(strBuf, STR_MAX+1);
  1162. }
  1163. if (plugin->getParameterUnit(parameterId, strBuf))
  1164. {
  1165. retInfo.unit = carla_strdup_safe(strBuf);
  1166. carla_zeroChars(strBuf, STR_MAX+1);
  1167. }
  1168. if (plugin->getParameterComment(parameterId, strBuf))
  1169. {
  1170. retInfo.comment = carla_strdup_safe(strBuf);
  1171. carla_zeroChars(strBuf, STR_MAX+1);
  1172. }
  1173. if (plugin->getParameterGroupName(parameterId, strBuf))
  1174. {
  1175. retInfo.groupName = carla_strdup_safe(strBuf);
  1176. carla_zeroChars(strBuf, STR_MAX+1);
  1177. }
  1178. checkStringPtr(retInfo.name);
  1179. checkStringPtr(retInfo.symbol);
  1180. checkStringPtr(retInfo.unit);
  1181. checkStringPtr(retInfo.comment);
  1182. checkStringPtr(retInfo.groupName);
  1183. }
  1184. return &retInfo;
  1185. }
  1186. const CarlaScalePointInfo* carla_get_parameter_scalepoint_info(CarlaHostHandle handle,
  1187. uint pluginId,
  1188. uint32_t parameterId,
  1189. uint32_t scalePointId)
  1190. {
  1191. CARLA_ASSERT(handle->engine != nullptr);
  1192. static CarlaScalePointInfo retInfo;
  1193. // reset
  1194. retInfo.value = 0.0f;
  1195. // cleanup
  1196. if (retInfo.label != gNullCharPtr)
  1197. {
  1198. delete[] retInfo.label;
  1199. retInfo.label = gNullCharPtr;
  1200. }
  1201. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr, &retInfo);
  1202. if (const CarlaPluginPtr plugin = handle->engine->getPlugin(pluginId))
  1203. {
  1204. char strBuf[STR_MAX+1];
  1205. retInfo.value = plugin->getParameterScalePointValue(parameterId, scalePointId);
  1206. carla_zeroChars(strBuf, STR_MAX+1);
  1207. if (plugin->getParameterScalePointLabel(parameterId, scalePointId, strBuf))
  1208. retInfo.label = carla_strdup_safe(strBuf);
  1209. checkStringPtr(retInfo.label);
  1210. }
  1211. return &retInfo;
  1212. }
  1213. // --------------------------------------------------------------------------------------------------------------------
  1214. uint carla_get_audio_port_hints(CarlaHostHandle handle, uint pluginId, bool isOutput, uint32_t portIndex)
  1215. {
  1216. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr, 0x0);
  1217. if (const CarlaPluginPtr plugin = handle->engine->getPlugin(pluginId))
  1218. {
  1219. CARLA_SAFE_ASSERT_RETURN(portIndex < (isOutput ? plugin->getAudioOutCount() : plugin->getAudioInCount()), 0x0);
  1220. return plugin->getAudioPortHints(isOutput, portIndex);
  1221. }
  1222. return 0x0;
  1223. }
  1224. // --------------------------------------------------------------------------------------------------------------------
  1225. const ParameterData* carla_get_parameter_data(CarlaHostHandle handle, uint pluginId, uint32_t parameterId)
  1226. {
  1227. static ParameterData retParamData;
  1228. // reset
  1229. retParamData.type = CB::PARAMETER_UNKNOWN;
  1230. retParamData.hints = 0x0;
  1231. retParamData.index = CB::PARAMETER_NULL;
  1232. retParamData.rindex = -1;
  1233. retParamData.midiChannel = 0;
  1234. retParamData.mappedControlIndex = CB::CONTROL_INDEX_NONE;
  1235. retParamData.mappedMinimum = 0.0f;
  1236. retParamData.mappedMaximum = 0.0f;
  1237. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr, &retParamData);
  1238. if (const CarlaPluginPtr plugin = handle->engine->getPlugin(pluginId))
  1239. {
  1240. CARLA_SAFE_ASSERT_RETURN(parameterId < plugin->getParameterCount(), &retParamData);
  1241. const ParameterData& pluginParamData(plugin->getParameterData(parameterId));
  1242. retParamData.type = pluginParamData.type;
  1243. retParamData.hints = pluginParamData.hints;
  1244. retParamData.index = pluginParamData.index;
  1245. retParamData.rindex = pluginParamData.rindex;
  1246. retParamData.midiChannel = pluginParamData.midiChannel;
  1247. retParamData.mappedControlIndex = pluginParamData.mappedControlIndex;
  1248. retParamData.mappedMinimum = pluginParamData.mappedMinimum;
  1249. retParamData.mappedMaximum = pluginParamData.mappedMaximum;
  1250. }
  1251. return &retParamData;
  1252. }
  1253. const ParameterRanges* carla_get_parameter_ranges(CarlaHostHandle handle, uint pluginId, uint32_t parameterId)
  1254. {
  1255. static ParameterRanges retParamRanges;
  1256. // reset
  1257. retParamRanges.def = 0.0f;
  1258. retParamRanges.min = 0.0f;
  1259. retParamRanges.max = 1.0f;
  1260. retParamRanges.step = 0.01f;
  1261. retParamRanges.stepSmall = 0.0001f;
  1262. retParamRanges.stepLarge = 0.1f;
  1263. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr, &retParamRanges);
  1264. if (const CarlaPluginPtr plugin = handle->engine->getPlugin(pluginId))
  1265. {
  1266. CARLA_SAFE_ASSERT_RETURN(parameterId < plugin->getParameterCount(), &retParamRanges);
  1267. const ParameterRanges& pluginParamRanges(plugin->getParameterRanges(parameterId));
  1268. retParamRanges.def = pluginParamRanges.def;
  1269. retParamRanges.min = pluginParamRanges.min;
  1270. retParamRanges.max = pluginParamRanges.max;
  1271. retParamRanges.step = pluginParamRanges.step;
  1272. retParamRanges.stepSmall = pluginParamRanges.stepSmall;
  1273. retParamRanges.stepLarge = pluginParamRanges.stepLarge;
  1274. }
  1275. return &retParamRanges;
  1276. }
  1277. const MidiProgramData* carla_get_midi_program_data(CarlaHostHandle handle, uint pluginId, uint32_t midiProgramId)
  1278. {
  1279. static MidiProgramData retMidiProgData = { 0, 0, gNullCharPtr };
  1280. // reset
  1281. retMidiProgData.bank = 0;
  1282. retMidiProgData.program = 0;
  1283. if (retMidiProgData.name != gNullCharPtr)
  1284. {
  1285. delete[] retMidiProgData.name;
  1286. retMidiProgData.name = gNullCharPtr;
  1287. }
  1288. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr, &retMidiProgData);
  1289. if (const CarlaPluginPtr plugin = handle->engine->getPlugin(pluginId))
  1290. {
  1291. CARLA_SAFE_ASSERT_RETURN(midiProgramId < plugin->getMidiProgramCount(), &retMidiProgData);
  1292. const MidiProgramData& pluginMidiProgData(plugin->getMidiProgramData(midiProgramId));
  1293. retMidiProgData.bank = pluginMidiProgData.bank;
  1294. retMidiProgData.program = pluginMidiProgData.program;
  1295. if (pluginMidiProgData.name != nullptr)
  1296. {
  1297. retMidiProgData.name = carla_strdup_safe(pluginMidiProgData.name);
  1298. checkStringPtr(retMidiProgData.name);
  1299. }
  1300. else
  1301. {
  1302. retMidiProgData.name = gNullCharPtr;
  1303. }
  1304. }
  1305. return &retMidiProgData;
  1306. }
  1307. const CustomData* carla_get_custom_data(CarlaHostHandle handle, uint pluginId, uint32_t customDataId)
  1308. {
  1309. static CustomData retCustomData = { gNullCharPtr, gNullCharPtr, gNullCharPtr };
  1310. // reset
  1311. if (retCustomData.type != gNullCharPtr)
  1312. {
  1313. delete[] retCustomData.type;
  1314. retCustomData.type = gNullCharPtr;
  1315. }
  1316. if (retCustomData.key != gNullCharPtr)
  1317. {
  1318. delete[] retCustomData.key;
  1319. retCustomData.key = gNullCharPtr;
  1320. }
  1321. if (retCustomData.value != gNullCharPtr)
  1322. {
  1323. delete[] retCustomData.value;
  1324. retCustomData.value = gNullCharPtr;
  1325. }
  1326. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr, &retCustomData);
  1327. if (const CarlaPluginPtr plugin = handle->engine->getPlugin(pluginId))
  1328. {
  1329. CARLA_SAFE_ASSERT_RETURN(customDataId < plugin->getCustomDataCount(), &retCustomData)
  1330. const CustomData& pluginCustomData(plugin->getCustomData(customDataId));
  1331. retCustomData.type = carla_strdup_safe(pluginCustomData.type);
  1332. retCustomData.key = carla_strdup_safe(pluginCustomData.key);
  1333. retCustomData.value = carla_strdup_safe(pluginCustomData.value);
  1334. checkStringPtr(retCustomData.type);
  1335. checkStringPtr(retCustomData.key);
  1336. checkStringPtr(retCustomData.value);
  1337. }
  1338. return &retCustomData;
  1339. }
  1340. const char* carla_get_custom_data_value(CarlaHostHandle handle, uint pluginId, const char* type, const char* key)
  1341. {
  1342. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0', gNullCharPtr);
  1343. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0', gNullCharPtr);
  1344. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr, gNullCharPtr);
  1345. if (const CarlaPluginPtr plugin = handle->engine->getPlugin(pluginId))
  1346. {
  1347. const uint32_t count = plugin->getCustomDataCount();
  1348. if (count == 0)
  1349. return gNullCharPtr;
  1350. static CarlaString customDataValue;
  1351. for (uint32_t i=0; i<count; ++i)
  1352. {
  1353. const CustomData& pluginCustomData(plugin->getCustomData(i));
  1354. if (std::strcmp(pluginCustomData.type, type) != 0)
  1355. continue;
  1356. if (std::strcmp(pluginCustomData.key, key) != 0)
  1357. continue;
  1358. customDataValue = pluginCustomData.value;
  1359. return customDataValue.buffer();
  1360. }
  1361. }
  1362. return gNullCharPtr;
  1363. }
  1364. const char* carla_get_chunk_data(CarlaHostHandle handle, uint pluginId)
  1365. {
  1366. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr, gNullCharPtr);
  1367. if (const CarlaPluginPtr plugin = handle->engine->getPlugin(pluginId))
  1368. {
  1369. CARLA_SAFE_ASSERT_RETURN(plugin->getOptionsEnabled() & CB::PLUGIN_OPTION_USE_CHUNKS, gNullCharPtr);
  1370. void* data = nullptr;
  1371. const std::size_t dataSize(plugin->getChunkData(&data));
  1372. CARLA_SAFE_ASSERT_RETURN(data != nullptr && dataSize > 0, gNullCharPtr);
  1373. static CarlaString chunkData;
  1374. chunkData = CarlaString::asBase64(data, static_cast<std::size_t>(dataSize));
  1375. return chunkData.buffer();
  1376. }
  1377. return gNullCharPtr;
  1378. }
  1379. // --------------------------------------------------------------------------------------------------------------------
  1380. uint32_t carla_get_parameter_count(CarlaHostHandle handle, uint pluginId)
  1381. {
  1382. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr, 0);
  1383. if (const CarlaPluginPtr plugin = handle->engine->getPlugin(pluginId))
  1384. return plugin->getParameterCount();
  1385. return 0;
  1386. }
  1387. uint32_t carla_get_program_count(CarlaHostHandle handle, uint pluginId)
  1388. {
  1389. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr, 0);
  1390. if (const CarlaPluginPtr plugin = handle->engine->getPlugin(pluginId))
  1391. return plugin->getProgramCount();
  1392. return 0;
  1393. }
  1394. uint32_t carla_get_midi_program_count(CarlaHostHandle handle, uint pluginId)
  1395. {
  1396. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr, 0);
  1397. if (const CarlaPluginPtr plugin = handle->engine->getPlugin(pluginId))
  1398. return plugin->getMidiProgramCount();
  1399. return 0;
  1400. }
  1401. uint32_t carla_get_custom_data_count(CarlaHostHandle handle, uint pluginId)
  1402. {
  1403. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr, 0);
  1404. if (const CarlaPluginPtr plugin = handle->engine->getPlugin(pluginId))
  1405. return plugin->getCustomDataCount();
  1406. return 0;
  1407. }
  1408. // --------------------------------------------------------------------------------------------------------------------
  1409. const char* carla_get_parameter_text(CarlaHostHandle handle, uint pluginId, uint32_t parameterId)
  1410. {
  1411. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr, gNullCharPtr);
  1412. if (const CarlaPluginPtr plugin = handle->engine->getPlugin(pluginId))
  1413. {
  1414. CARLA_SAFE_ASSERT_RETURN(parameterId < plugin->getParameterCount(), gNullCharPtr);
  1415. static char textBuf[STR_MAX+1];
  1416. carla_zeroChars(textBuf, STR_MAX+1);
  1417. if (! plugin->getParameterText(parameterId, textBuf))
  1418. textBuf[0] = '\0';
  1419. return textBuf;
  1420. }
  1421. return gNullCharPtr;
  1422. }
  1423. const char* carla_get_program_name(CarlaHostHandle handle, uint pluginId, uint32_t programId)
  1424. {
  1425. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr, nullptr);
  1426. if (const CarlaPluginPtr plugin = handle->engine->getPlugin(pluginId))
  1427. {
  1428. CARLA_SAFE_ASSERT_RETURN(programId < plugin->getProgramCount(), gNullCharPtr);
  1429. static char programName[STR_MAX+1];
  1430. carla_zeroChars(programName, STR_MAX+1);
  1431. if (! plugin->getProgramName(programId, programName))
  1432. programName[0] = '\0';
  1433. return programName;
  1434. }
  1435. return gNullCharPtr;
  1436. }
  1437. const char* carla_get_midi_program_name(CarlaHostHandle handle, uint pluginId, uint32_t midiProgramId)
  1438. {
  1439. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr, gNullCharPtr);
  1440. if (const CarlaPluginPtr plugin = handle->engine->getPlugin(pluginId))
  1441. {
  1442. CARLA_SAFE_ASSERT_RETURN(midiProgramId < plugin->getMidiProgramCount(), gNullCharPtr);
  1443. static char midiProgramName[STR_MAX+1];
  1444. carla_zeroChars(midiProgramName, STR_MAX+1);
  1445. if (! plugin->getMidiProgramName(midiProgramId, midiProgramName))
  1446. midiProgramName[0] = '\0';
  1447. return midiProgramName;
  1448. }
  1449. return gNullCharPtr;
  1450. }
  1451. const char* carla_get_real_plugin_name(CarlaHostHandle handle, uint pluginId)
  1452. {
  1453. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr, gNullCharPtr);
  1454. if (const CarlaPluginPtr plugin = handle->engine->getPlugin(pluginId))
  1455. {
  1456. static char realPluginName[STR_MAX+1];
  1457. carla_zeroChars(realPluginName, STR_MAX+1);
  1458. if (! plugin->getRealName(realPluginName))
  1459. realPluginName[0] = '\0';
  1460. return realPluginName;
  1461. }
  1462. return gNullCharPtr;
  1463. }
  1464. // --------------------------------------------------------------------------------------------------------------------
  1465. int32_t carla_get_current_program_index(CarlaHostHandle handle, uint pluginId)
  1466. {
  1467. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr, -1);
  1468. if (const CarlaPluginPtr plugin = handle->engine->getPlugin(pluginId))
  1469. return plugin->getCurrentProgram();
  1470. return -1;
  1471. }
  1472. int32_t carla_get_current_midi_program_index(CarlaHostHandle handle, uint pluginId)
  1473. {
  1474. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr, -1);
  1475. if (const CarlaPluginPtr plugin = handle->engine->getPlugin(pluginId))
  1476. return plugin->getCurrentMidiProgram();
  1477. return -1;
  1478. }
  1479. // --------------------------------------------------------------------------------------------------------------------
  1480. float carla_get_default_parameter_value(CarlaHostHandle handle, uint pluginId, uint32_t parameterId)
  1481. {
  1482. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr, 0.0f);
  1483. if (const CarlaPluginPtr plugin = handle->engine->getPlugin(pluginId))
  1484. {
  1485. CARLA_SAFE_ASSERT_RETURN(parameterId < plugin->getParameterCount(), 0.0f);
  1486. return plugin->getParameterRanges(parameterId).def;
  1487. }
  1488. return 0.0f;
  1489. }
  1490. float carla_get_current_parameter_value(CarlaHostHandle handle, uint pluginId, uint32_t parameterId)
  1491. {
  1492. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr, 0.0f);
  1493. if (const CarlaPluginPtr plugin = handle->engine->getPlugin(pluginId))
  1494. {
  1495. CARLA_SAFE_ASSERT_RETURN(parameterId < plugin->getParameterCount(), 0.0f);
  1496. return plugin->getParameterValue(parameterId);
  1497. }
  1498. return 0.0f;
  1499. }
  1500. float carla_get_internal_parameter_value(CarlaHostHandle handle, uint pluginId, int32_t parameterId)
  1501. {
  1502. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1503. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr, (parameterId == CB::PARAMETER_CTRL_CHANNEL) ? -1.0f : 0.0f);
  1504. #else
  1505. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr, 0.0f);
  1506. #endif
  1507. CARLA_SAFE_ASSERT_RETURN(parameterId != CB::PARAMETER_NULL && parameterId > CB::PARAMETER_MAX, 0.0f);
  1508. if (const CarlaPluginPtr plugin = handle->engine->getPlugin(pluginId))
  1509. return plugin->getInternalParameterValue(parameterId);
  1510. return 0.0f;
  1511. }
  1512. // --------------------------------------------------------------------------------------------------------------------
  1513. uint32_t carla_get_plugin_latency(CarlaHostHandle handle, uint pluginId)
  1514. {
  1515. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr, 0);
  1516. if (const CarlaPluginPtr plugin = handle->engine->getPlugin(pluginId))
  1517. return plugin->getLatencyInFrames();
  1518. return 0;
  1519. }
  1520. // --------------------------------------------------------------------------------------------------------------------
  1521. const float* carla_get_peak_values(CarlaHostHandle handle, uint pluginId)
  1522. {
  1523. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr, nullptr);
  1524. return handle->engine->getPeaks(pluginId);
  1525. }
  1526. float carla_get_input_peak_value(CarlaHostHandle handle, uint pluginId, bool isLeft)
  1527. {
  1528. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr, 0.0f);
  1529. return handle->engine->getInputPeak(pluginId, isLeft);
  1530. }
  1531. float carla_get_output_peak_value(CarlaHostHandle handle, uint pluginId, bool isLeft)
  1532. {
  1533. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr, 0.0f);
  1534. return handle->engine->getOutputPeak(pluginId, isLeft);
  1535. }
  1536. // --------------------------------------------------------------------------------------------------------------------
  1537. CARLA_BACKEND_START_NAMESPACE
  1538. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1539. // defined in CarlaPluginInternal.cpp
  1540. const void* carla_render_inline_display_internal(const CarlaPluginPtr& plugin, uint32_t width, uint32_t height);
  1541. #endif
  1542. // defined in CarlaPluginLV2.cpp
  1543. const void* carla_render_inline_display_lv2(const CarlaPluginPtr& plugin, uint32_t width, uint32_t height);
  1544. CARLA_BACKEND_END_NAMESPACE
  1545. const CarlaInlineDisplayImageSurface* carla_render_inline_display(CarlaHostHandle handle,
  1546. uint pluginId,
  1547. uint32_t width, uint32_t height)
  1548. {
  1549. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr && handle->engine->isRunning(), nullptr);
  1550. if (handle->engine->isAboutToClose())
  1551. return nullptr;
  1552. if (const CarlaPluginPtr plugin = handle->engine->getPlugin(pluginId))
  1553. {
  1554. switch (plugin->getType())
  1555. {
  1556. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1557. case CB::PLUGIN_INTERNAL:
  1558. return (const CarlaInlineDisplayImageSurface*)CB::carla_render_inline_display_internal(plugin, width, height);
  1559. #endif
  1560. case CB::PLUGIN_LV2:
  1561. return (const CarlaInlineDisplayImageSurface*)CB::carla_render_inline_display_lv2(plugin, width, height);
  1562. default:
  1563. return nullptr;
  1564. }
  1565. }
  1566. return nullptr;
  1567. }
  1568. // --------------------------------------------------------------------------------------------------------------------
  1569. void carla_set_active(CarlaHostHandle handle, uint pluginId, bool onOff)
  1570. {
  1571. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr,);
  1572. if (const CarlaPluginPtr plugin = handle->engine->getPlugin(pluginId))
  1573. plugin->setActive(onOff, true, false);
  1574. }
  1575. #ifndef BUILD_BRIDGE
  1576. void carla_set_drywet(CarlaHostHandle handle, uint pluginId, float value)
  1577. {
  1578. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr,);
  1579. if (const CarlaPluginPtr plugin = handle->engine->getPlugin(pluginId))
  1580. plugin->setDryWet(value, true, false);
  1581. }
  1582. void carla_set_volume(CarlaHostHandle handle, uint pluginId, float value)
  1583. {
  1584. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr,);
  1585. if (const CarlaPluginPtr plugin = handle->engine->getPlugin(pluginId))
  1586. plugin->setVolume(value, true, false);
  1587. }
  1588. void carla_set_balance_left(CarlaHostHandle handle, uint pluginId, float value)
  1589. {
  1590. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr,);
  1591. if (const CarlaPluginPtr plugin = handle->engine->getPlugin(pluginId))
  1592. plugin->setBalanceLeft(value, true, false);
  1593. }
  1594. void carla_set_balance_right(CarlaHostHandle handle, uint pluginId, float value)
  1595. {
  1596. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr,);
  1597. if (const CarlaPluginPtr plugin = handle->engine->getPlugin(pluginId))
  1598. plugin->setBalanceRight(value, true, false);
  1599. }
  1600. void carla_set_panning(CarlaHostHandle handle, uint pluginId, float value)
  1601. {
  1602. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr,);
  1603. if (const CarlaPluginPtr plugin = handle->engine->getPlugin(pluginId))
  1604. plugin->setPanning(value, true, false);
  1605. }
  1606. void carla_set_ctrl_channel(CarlaHostHandle handle, uint pluginId, int8_t channel)
  1607. {
  1608. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr,);
  1609. CARLA_SAFE_ASSERT_RETURN(channel >= -1 && channel < MAX_MIDI_CHANNELS,);
  1610. if (const CarlaPluginPtr plugin = handle->engine->getPlugin(pluginId))
  1611. plugin->setCtrlChannel(channel, true, false);
  1612. }
  1613. #endif
  1614. void carla_set_option(CarlaHostHandle handle, uint pluginId, uint option, bool yesNo)
  1615. {
  1616. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr,);
  1617. if (const CarlaPluginPtr plugin = handle->engine->getPlugin(pluginId))
  1618. plugin->setOption(option, yesNo, false);
  1619. }
  1620. // --------------------------------------------------------------------------------------------------------------------
  1621. void carla_set_parameter_value(CarlaHostHandle handle, uint pluginId, uint32_t parameterId, float value)
  1622. {
  1623. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr,);
  1624. if (const CarlaPluginPtr plugin = handle->engine->getPlugin(pluginId))
  1625. {
  1626. CARLA_SAFE_ASSERT_RETURN(parameterId < plugin->getParameterCount(),);
  1627. plugin->setParameterValue(parameterId, value, true, true, false);
  1628. }
  1629. }
  1630. #ifndef BUILD_BRIDGE
  1631. void carla_set_parameter_midi_channel(CarlaHostHandle handle, uint pluginId, uint32_t parameterId, uint8_t channel)
  1632. {
  1633. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr,);
  1634. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1635. if (const CarlaPluginPtr plugin = handle->engine->getPlugin(pluginId))
  1636. {
  1637. CARLA_SAFE_ASSERT_RETURN(parameterId < plugin->getParameterCount(),);
  1638. plugin->setParameterMidiChannel(parameterId, channel, true, false);
  1639. }
  1640. }
  1641. void carla_set_parameter_mapped_control_index(CarlaHostHandle handle, uint pluginId, uint32_t parameterId, int16_t index)
  1642. {
  1643. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr,);
  1644. CARLA_SAFE_ASSERT_RETURN(index >= CB::CONTROL_INDEX_NONE && index <= CB::CONTROL_INDEX_MAX_ALLOWED,);
  1645. if (const CarlaPluginPtr plugin = handle->engine->getPlugin(pluginId))
  1646. {
  1647. CARLA_SAFE_ASSERT_RETURN(parameterId < plugin->getParameterCount(),);
  1648. plugin->setParameterMappedControlIndex(parameterId, index, true, false, true);
  1649. }
  1650. }
  1651. void carla_set_parameter_mapped_range(CarlaHostHandle handle, uint pluginId, uint32_t parameterId, float minimum, float maximum)
  1652. {
  1653. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr,);
  1654. if (const CarlaPluginPtr plugin = handle->engine->getPlugin(pluginId))
  1655. {
  1656. CARLA_SAFE_ASSERT_RETURN(parameterId < plugin->getParameterCount(),);
  1657. plugin->setParameterMappedRange(parameterId, minimum, maximum, true, false);
  1658. }
  1659. }
  1660. void carla_set_parameter_touch(CarlaHostHandle handle, uint pluginId, uint32_t parameterId, bool touch)
  1661. {
  1662. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr,);
  1663. carla_debug("carla_set_parameter_touch(%p, %i, %i, %s)", handle, pluginId, parameterId, bool2str(touch));
  1664. return handle->engine->touchPluginParameter(pluginId, parameterId, touch);
  1665. }
  1666. #endif
  1667. // --------------------------------------------------------------------------------------------------------------------
  1668. void carla_set_program(CarlaHostHandle handle, uint pluginId, uint32_t programId)
  1669. {
  1670. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr,);
  1671. if (const CarlaPluginPtr plugin = handle->engine->getPlugin(pluginId))
  1672. {
  1673. CARLA_SAFE_ASSERT_RETURN(programId < plugin->getProgramCount(),);
  1674. plugin->setProgram(static_cast<int32_t>(programId), true, true, false);
  1675. }
  1676. }
  1677. void carla_set_midi_program(CarlaHostHandle handle, uint pluginId, uint32_t midiProgramId)
  1678. {
  1679. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr,);
  1680. if (const CarlaPluginPtr plugin = handle->engine->getPlugin(pluginId))
  1681. {
  1682. CARLA_SAFE_ASSERT_RETURN(midiProgramId < plugin->getMidiProgramCount(),);
  1683. plugin->setMidiProgram(static_cast<int32_t>(midiProgramId), true, true, false);
  1684. }
  1685. }
  1686. // --------------------------------------------------------------------------------------------------------------------
  1687. void carla_set_custom_data(CarlaHostHandle handle, uint pluginId, const char* type, const char* key, const char* value)
  1688. {
  1689. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr,);
  1690. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  1691. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  1692. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  1693. if (const CarlaPluginPtr plugin = handle->engine->getPlugin(pluginId))
  1694. plugin->setCustomData(type, key, value, true);
  1695. }
  1696. void carla_set_chunk_data(CarlaHostHandle handle, uint pluginId, const char* chunkData)
  1697. {
  1698. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr,);
  1699. CARLA_SAFE_ASSERT_RETURN(chunkData != nullptr && chunkData[0] != '\0',);
  1700. if (const CarlaPluginPtr plugin = handle->engine->getPlugin(pluginId))
  1701. {
  1702. CARLA_SAFE_ASSERT_RETURN(plugin->getOptionsEnabled() & CB::PLUGIN_OPTION_USE_CHUNKS,);
  1703. std::vector<uint8_t> chunk(carla_getChunkFromBase64String(chunkData));
  1704. #ifdef CARLA_PROPER_CPP11_SUPPORT
  1705. plugin->setChunkData(chunk.data(), chunk.size());
  1706. #else
  1707. plugin->setChunkData(&chunk.front(), chunk.size());
  1708. #endif
  1709. }
  1710. }
  1711. // --------------------------------------------------------------------------------------------------------------------
  1712. void carla_prepare_for_save(CarlaHostHandle handle, uint pluginId)
  1713. {
  1714. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr,);
  1715. if (const CarlaPluginPtr plugin = handle->engine->getPlugin(pluginId))
  1716. plugin->prepareForSave(false);
  1717. }
  1718. void carla_reset_parameters(CarlaHostHandle handle, uint pluginId)
  1719. {
  1720. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr,);
  1721. if (const CarlaPluginPtr plugin = handle->engine->getPlugin(pluginId))
  1722. plugin->resetParameters();
  1723. }
  1724. void carla_randomize_parameters(CarlaHostHandle handle, uint pluginId)
  1725. {
  1726. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr,);
  1727. if (const CarlaPluginPtr plugin = handle->engine->getPlugin(pluginId))
  1728. plugin->randomizeParameters();
  1729. }
  1730. #ifndef BUILD_BRIDGE
  1731. void carla_send_midi_note(CarlaHostHandle handle, uint pluginId, uint8_t channel, uint8_t note, uint8_t velocity)
  1732. {
  1733. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr && handle->engine->isRunning(),);
  1734. if (const CarlaPluginPtr plugin = handle->engine->getPlugin(pluginId))
  1735. plugin->sendMidiSingleNote(channel, note, velocity, true, true, false);
  1736. }
  1737. #endif
  1738. void carla_set_custom_ui_title(CarlaHostHandle handle, uint pluginId, const char* title)
  1739. {
  1740. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr,);
  1741. CARLA_SAFE_ASSERT_RETURN(title != nullptr,);
  1742. if (const CarlaPluginPtr plugin = handle->engine->getPlugin(pluginId))
  1743. plugin->setCustomUITitle(title);
  1744. }
  1745. void carla_show_custom_ui(CarlaHostHandle handle, uint pluginId, bool yesNo)
  1746. {
  1747. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr,);
  1748. if (const CarlaPluginPtr plugin = handle->engine->getPlugin(pluginId))
  1749. plugin->showCustomUI(yesNo);
  1750. }
  1751. void* carla_embed_custom_ui(CarlaHostHandle handle, uint pluginId, void* ptr)
  1752. {
  1753. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr, nullptr);
  1754. if (const CarlaPluginPtr plugin = handle->engine->getPlugin(pluginId))
  1755. return plugin->embedCustomUI(ptr);
  1756. return nullptr;
  1757. }
  1758. // --------------------------------------------------------------------------------------------------------------------
  1759. uint32_t carla_get_buffer_size(CarlaHostHandle handle)
  1760. {
  1761. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr, 0);
  1762. carla_debug("carla_get_buffer_size(%p)", handle);
  1763. return handle->engine->getBufferSize();
  1764. }
  1765. double carla_get_sample_rate(CarlaHostHandle handle)
  1766. {
  1767. CARLA_SAFE_ASSERT_RETURN(handle->engine != nullptr, 0.0);
  1768. carla_debug("carla_get_sample_rate(%p)", handle);
  1769. return handle->engine->getSampleRate();
  1770. }
  1771. // --------------------------------------------------------------------------------------------------------------------
  1772. const char* carla_get_last_error(CarlaHostHandle handle)
  1773. {
  1774. carla_debug("carla_get_last_error(%p)", handle);
  1775. if (handle->engine != nullptr)
  1776. return handle->engine->getLastError();
  1777. return handle->isStandalone
  1778. ? ((CarlaHostStandalone*)handle)->lastError.buffer()
  1779. : gNullCharPtr;
  1780. }
  1781. const char* carla_get_host_osc_url_tcp(CarlaHostHandle handle)
  1782. {
  1783. carla_debug("carla_get_host_osc_url_tcp(%p)", handle);
  1784. #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  1785. if (handle->engine == nullptr)
  1786. {
  1787. carla_stderr2("carla_get_host_osc_url_tcp() failed, engine is not running");
  1788. if (handle->isStandalone)
  1789. ((CarlaHostStandalone*)handle)->lastError = "Engine is not running";
  1790. return gNullCharPtr;
  1791. }
  1792. const char* const path = handle->engine->getOscServerPathTCP();
  1793. if (path != nullptr && path[0] != '\0')
  1794. return path;
  1795. static const char* const notAvailable = "(OSC TCP port not available)";
  1796. return notAvailable;
  1797. #else
  1798. return "(OSC support not available in this build)";
  1799. // unused
  1800. (void)handle;
  1801. #endif
  1802. }
  1803. const char* carla_get_host_osc_url_udp(CarlaHostHandle handle)
  1804. {
  1805. carla_debug("carla_get_host_osc_url_udp(%p)", handle);
  1806. #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  1807. if (handle->engine == nullptr)
  1808. {
  1809. carla_stderr2("carla_get_host_osc_url_udp() failed, engine is not running");
  1810. if (handle->isStandalone)
  1811. ((CarlaHostStandalone*)handle)->lastError = "Engine is not running";
  1812. return gNullCharPtr;
  1813. }
  1814. const char* const path = handle->engine->getOscServerPathUDP();
  1815. if (path != nullptr && path[0] != '\0')
  1816. return path;
  1817. static const char* const notAvailable = "(OSC UDP port not available)";
  1818. return notAvailable;
  1819. #else
  1820. return "(OSC support not available in this build)";
  1821. // unused
  1822. (void)handle;
  1823. #endif
  1824. }
  1825. // --------------------------------------------------------------------------------------------------------------------
  1826. #ifndef CARLA_PLUGIN_BUILD
  1827. # define CARLA_PLUGIN_UI_CLASS_PREFIX Standalone
  1828. # include "CarlaPluginUI.cpp"
  1829. # undef CARLA_PLUGIN_UI_CLASS_PREFIX
  1830. # include "CarlaDssiUtils.cpp"
  1831. # include "CarlaMacUtils.cpp"
  1832. # include "CarlaPatchbayUtils.cpp"
  1833. # include "CarlaPipeUtils.cpp"
  1834. # include "CarlaProcessUtils.cpp"
  1835. # include "CarlaStateUtils.cpp"
  1836. # include "utils/Information.cpp"
  1837. # include "utils/Windows.cpp"
  1838. #endif /* CARLA_PLUGIN_BUILD */
  1839. // --------------------------------------------------------------------------------------------------------------------