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.

851 lines
31KB

  1. /*
  2. * DISTRHO Ildaeil Plugin
  3. * Copyright (C) 2021-2023 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 LICENSE file.
  16. */
  17. #include "IldaeilBasePlugin.hpp"
  18. #include "DistrhoPluginUtils.hpp"
  19. #include "CarlaEngine.hpp"
  20. #include "water/files/File.h"
  21. #include "water/streams/MemoryOutputStream.h"
  22. #include "water/xml/XmlDocument.h"
  23. START_NAMESPACE_DISTRHO
  24. using namespace CARLA_BACKEND_NAMESPACE;
  25. // --------------------------------------------------------------------------------------------------------------------
  26. static uint32_t host_get_buffer_size(NativeHostHandle);
  27. static double host_get_sample_rate(NativeHostHandle);
  28. static bool host_is_offline(NativeHostHandle);
  29. static const NativeTimeInfo* host_get_time_info(NativeHostHandle handle);
  30. static bool host_write_midi_event(NativeHostHandle handle, const NativeMidiEvent* event);
  31. static void host_ui_parameter_changed(NativeHostHandle handle, uint32_t index, float value);
  32. static void host_ui_midi_program_changed(NativeHostHandle handle, uint8_t channel, uint32_t bank, uint32_t program);
  33. static void host_ui_custom_data_changed(NativeHostHandle handle, const char* key, const char* value);
  34. static void host_ui_closed(NativeHostHandle handle);
  35. static const char* host_ui_open_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter);
  36. static const char* host_ui_save_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter);
  37. static intptr_t host_dispatcher(NativeHostHandle h, NativeHostDispatcherOpcode op, int32_t, intptr_t, void*, float);
  38. // --------------------------------------------------------------------------------------------------------------------
  39. Mutex IldaeilBasePlugin::sPluginInfoLoadMutex;
  40. // --------------------------------------------------------------------------------------------------------------------
  41. #ifndef CARLA_OS_WIN
  42. static water::String getHomePath()
  43. {
  44. static water::String path(water::File::getSpecialLocation(water::File::userHomeDirectory).getFullPathName());
  45. return path;
  46. }
  47. #endif
  48. static const char* getPathForLADSPA()
  49. {
  50. static water::String path;
  51. if (path.isEmpty())
  52. {
  53. #if defined(CARLA_OS_HAIKU)
  54. path = getHomePath() + "/.ladspa:/system/add-ons/media/ladspaplugins:/system/lib/ladspa";
  55. #elif defined(CARLA_OS_MAC)
  56. path = getHomePath() + "/Library/Audio/Plug-Ins/LADSPA:/Library/Audio/Plug-Ins/LADSPA";
  57. #elif defined(CARLA_OS_WASM)
  58. path = "/ladspa";
  59. #elif defined(CARLA_OS_WIN)
  60. path = water::File::getSpecialLocation(water::File::winAppData).getFullPathName() + "\\LADSPA;";
  61. path += water::File::getSpecialLocation(water::File::winProgramFiles).getFullPathName() + "\\LADSPA";
  62. #else
  63. path = getHomePath() + "/.ladspa:/usr/lib/ladspa:/usr/local/lib/ladspa";
  64. #endif
  65. }
  66. return path.toRawUTF8();
  67. }
  68. static const char* getPathForDSSI()
  69. {
  70. static water::String path;
  71. if (path.isEmpty())
  72. {
  73. #if defined(CARLA_OS_HAIKU)
  74. path = getHomePath() + "/.dssi:/system/add-ons/media/dssiplugins:/system/lib/dssi";
  75. #elif defined(CARLA_OS_MAC)
  76. path = getHomePath() + "/Library/Audio/Plug-Ins/DSSI:/Library/Audio/Plug-Ins/DSSI";
  77. #elif defined(CARLA_OS_WASM)
  78. path = "/dssi";
  79. #elif defined(CARLA_OS_WIN)
  80. path = water::File::getSpecialLocation(water::File::winAppData).getFullPathName() + "\\DSSI;";
  81. path += water::File::getSpecialLocation(water::File::winProgramFiles).getFullPathName() + "\\DSSI";
  82. #else
  83. path = getHomePath() + "/.dssi:/usr/lib/dssi:/usr/local/lib/dssi";
  84. #endif
  85. }
  86. return path.toRawUTF8();
  87. }
  88. static const char* getPathForLV2()
  89. {
  90. static water::String path;
  91. if (path.isEmpty())
  92. {
  93. #if defined(CARLA_OS_HAIKU)
  94. path = getHomePath() + "/.lv2:/system/add-ons/media/lv2plugins";
  95. #elif defined(CARLA_OS_MAC)
  96. path = getHomePath() + "/Library/Audio/Plug-Ins/LV2:/Library/Audio/Plug-Ins/LV2";
  97. #elif defined(CARLA_OS_WASM)
  98. path = "/lv2";
  99. #elif defined(CARLA_OS_WIN)
  100. path = water::File::getSpecialLocation(water::File::winAppData).getFullPathName() + "\\LV2;";
  101. path += water::File::getSpecialLocation(water::File::winCommonProgramFiles).getFullPathName() + "\\LV2";
  102. #else
  103. path = getHomePath() + "/.lv2:/usr/lib/lv2:/usr/local/lib/lv2";
  104. #endif
  105. }
  106. return path.toRawUTF8();
  107. }
  108. static const char* getPathForVST2()
  109. {
  110. static water::String path;
  111. if (path.isEmpty())
  112. {
  113. #if defined(CARLA_OS_HAIKU)
  114. path = getHomePath() + "/.vst:/system/add-ons/media/vstplugins";
  115. #elif defined(CARLA_OS_MAC)
  116. path = getHomePath() + "/Library/Audio/Plug-Ins/VST:/Library/Audio/Plug-Ins/VST";
  117. #elif defined(CARLA_OS_WASM)
  118. path = "/vst";
  119. #elif defined(CARLA_OS_WIN)
  120. path = water::File::getSpecialLocation(water::File::winProgramFiles).getFullPathName() + "\\VstPlugins;";
  121. path += water::File::getSpecialLocation(water::File::winProgramFiles).getFullPathName() + "\\Steinberg\\VstPlugins;";
  122. path += water::File::getSpecialLocation(water::File::winCommonProgramFiles).getFullPathName() + "\\VST2";
  123. #else
  124. path = getHomePath() + "/.vst:/usr/lib/vst:/usr/local/lib/vst";
  125. water::String winePrefix;
  126. if (const char* const envWINEPREFIX = std::getenv("WINEPREFIX"))
  127. winePrefix = envWINEPREFIX;
  128. if (winePrefix.isEmpty())
  129. winePrefix = getHomePath() + "/.wine";
  130. if (water::File(winePrefix).exists())
  131. {
  132. path += ":" + winePrefix + "/drive_c/Program Files/Common Files/VST2";
  133. path += ":" + winePrefix + "/drive_c/Program Files/VstPlugins";
  134. path += ":" + winePrefix + "/drive_c/Program Files/Steinberg/VstPlugins";
  135. #ifdef CARLA_OS_64BIT
  136. path += ":" + winePrefix + "/drive_c/Program Files (x86)/Common Files/VST2";
  137. path += ":" + winePrefix + "/drive_c/Program Files (x86)/VstPlugins";
  138. path += ":" + winePrefix + "/drive_c/Program Files (x86)/Steinberg/VstPlugins";
  139. #endif
  140. }
  141. #endif
  142. }
  143. return path.toRawUTF8();
  144. }
  145. static const char* getPathForVST3()
  146. {
  147. static water::String path;
  148. if (path.isEmpty())
  149. {
  150. #if defined(CARLA_OS_HAIKU)
  151. path = getHomePath() + "/.vst3:/system/add-ons/media/dssiplugins";
  152. #elif defined(CARLA_OS_MAC)
  153. path = getHomePath() + "/Library/Audio/Plug-Ins/VST3:/Library/Audio/Plug-Ins/VST3";
  154. #elif defined(CARLA_OS_WASM)
  155. path = "/vst3";
  156. #elif defined(CARLA_OS_WIN)
  157. path = water::File::getSpecialLocation(water::File::winAppData).getFullPathName() + "\\VST3;";
  158. path += water::File::getSpecialLocation(water::File::winCommonProgramFiles).getFullPathName() + "\\VST3";
  159. #else
  160. path = getHomePath() + "/.vst3:/usr/lib/vst3:/usr/local/lib/vst3";
  161. water::String winePrefix;
  162. if (const char* const envWINEPREFIX = std::getenv("WINEPREFIX"))
  163. winePrefix = envWINEPREFIX;
  164. if (winePrefix.isEmpty())
  165. winePrefix = getHomePath() + "/.wine";
  166. if (water::File(winePrefix).exists())
  167. {
  168. path += ":" + winePrefix + "/drive_c/Program Files/Common Files/VST3";
  169. #ifdef CARLA_OS_64BIT
  170. path += ":" + winePrefix + "/drive_c/Program Files (x86)/Common Files/VST3";
  171. #endif
  172. }
  173. #endif
  174. }
  175. return path.toRawUTF8();
  176. }
  177. static const char* getPathForCLAP()
  178. {
  179. static water::String path;
  180. if (path.isEmpty())
  181. {
  182. #if defined(CARLA_OS_HAIKU)
  183. path = getHomePath() + "/.clap:/system/add-ons/media/clapplugins";
  184. #elif defined(CARLA_OS_MAC)
  185. path = getHomePath() + "/Library/Audio/Plug-Ins/CLAP:/Library/Audio/Plug-Ins/CLAP";
  186. #elif defined(CARLA_OS_WASM)
  187. path = "/clap";
  188. #elif defined(CARLA_OS_WIN)
  189. path = water::File::getSpecialLocation(water::File::winAppData).getFullPathName() + "\\CLAP;";
  190. path += water::File::getSpecialLocation(water::File::winCommonProgramFiles).getFullPathName() + "\\CLAP";
  191. #else
  192. path = getHomePath() + "/.clap:/usr/lib/clap:/usr/local/lib/clap";
  193. water::String winePrefix;
  194. if (const char* const envWINEPREFIX = std::getenv("WINEPREFIX"))
  195. winePrefix = envWINEPREFIX;
  196. if (winePrefix.isEmpty())
  197. winePrefix = getHomePath() + "/.wine";
  198. if (water::File(winePrefix).exists())
  199. {
  200. path += ":" + winePrefix + "/drive_c/Program Files/Common Files/CLAP";
  201. #ifdef CARLA_OS_64BIT
  202. path += ":" + winePrefix + "/drive_c/Program Files (x86)/Common Files/CLAP";
  203. #endif
  204. }
  205. #endif
  206. }
  207. return path.toRawUTF8();
  208. }
  209. static const char* getPathForJSFX()
  210. {
  211. static water::String path;
  212. if (path.isEmpty())
  213. {
  214. #if defined(CARLA_OS_MAC)
  215. path = getHomePath()
  216. + "/Library/Application Support/REAPER/Effects";
  217. if (! water::File(path).isDirectory())
  218. path = "/Applications/REAPER.app/Contents/InstallFiles/Effects";
  219. #elif defined(CARLA_OS_WASM)
  220. path = "/jsfx";
  221. #elif defined(CARLA_OS_WIN)
  222. path = water::File::getSpecialLocation(water::File::winAppData).getFullPathName() + "\\REAPER\\Effects";
  223. if (! water::File(path).isDirectory())
  224. path = water::File::getSpecialLocation(water::File::winProgramFiles).getFullPathName()
  225. + "\\REAPER\\InstallData\\Effects";
  226. #else
  227. if (const char* const configHome = std::getenv("XDG_CONFIG_HOME"))
  228. path = configHome;
  229. else
  230. path = getHomePath() + "/.config";
  231. path += "/REAPER/Effects";
  232. #endif
  233. }
  234. return path.toRawUTF8();
  235. }
  236. const char* IldaeilBasePlugin::getPluginPath(const PluginType ptype)
  237. {
  238. switch (ptype)
  239. {
  240. case PLUGIN_LADSPA:
  241. if (const char* const path = std::getenv("LADSPA_PATH"))
  242. return path;
  243. return getPathForLADSPA();
  244. case PLUGIN_DSSI:
  245. if (const char* const path = std::getenv("DSSI_PATH"))
  246. return path;
  247. return getPathForDSSI();
  248. case PLUGIN_LV2:
  249. if (const char* const path = std::getenv("LV2_PATH"))
  250. return path;
  251. return getPathForLV2();
  252. case PLUGIN_VST2:
  253. if (const char* const path = std::getenv("VST_PATH"))
  254. return path;
  255. return getPathForVST2();
  256. case PLUGIN_VST3:
  257. if (const char* const path = std::getenv("VST3_PATH"))
  258. return path;
  259. return getPathForVST3();
  260. case PLUGIN_CLAP:
  261. if (const char* const path = std::getenv("CLAP_PATH"))
  262. return path;
  263. return getPathForCLAP();
  264. case PLUGIN_JSFX:
  265. return getPathForJSFX();
  266. default:
  267. return nullptr;
  268. }
  269. }
  270. // --------------------------------------------------------------------------------------------------------------------
  271. const char* ildaeilConfigDir()
  272. {
  273. static water::String configDir;
  274. if (configDir.isEmpty())
  275. {
  276. #if defined(CARLA_OS_WASM)
  277. configDir = "/userfiles";
  278. #elif defined(CARLA_OS_MAC)
  279. configDir = getHomePath() + "/Documents/Ildaeil";
  280. #elif defined(CARLA_OS_WIN)
  281. configDir = water::File::getSpecialLocation(water::File::winMyDocuments).getFullPathName() + "\\Ildaeil";
  282. #else
  283. if (const char* const xdgEnv = getenv("XDG_CONFIG_HOME"))
  284. configDir = xdgEnv;
  285. else
  286. configDir = getHomePath() + "/.config";
  287. configDir += "/Ildaeil";
  288. #endif
  289. }
  290. return configDir.toRawUTF8();
  291. }
  292. // --------------------------------------------------------------------------------------------------------------------
  293. class IldaeilPlugin : public IldaeilBasePlugin
  294. {
  295. #if DISTRHO_PLUGIN_NUM_INPUTS == 0 || DISTRHO_PLUGIN_NUM_OUTPUTS == 0
  296. float* fDummyBuffer;
  297. float* fDummyBuffers[2];
  298. #endif
  299. #if DISTRHO_PLUGIN_WANT_MIDI_INPUT
  300. static constexpr const uint kMaxMidiEventCount = 512;
  301. NativeMidiEvent* fMidiEvents;
  302. #endif
  303. mutable NativeTimeInfo fCarlaTimeInfo;
  304. mutable water::MemoryOutputStream fLastProjectState;
  305. uint32_t fLastLatencyValue;
  306. public:
  307. IldaeilPlugin()
  308. : IldaeilBasePlugin(),
  309. #if DISTRHO_PLUGIN_NUM_INPUTS == 0 || DISTRHO_PLUGIN_NUM_OUTPUTS == 0
  310. fDummyBuffer(nullptr),
  311. #endif
  312. #if DISTRHO_PLUGIN_WANT_MIDI_INPUT
  313. fMidiEvents(nullptr),
  314. #endif
  315. fLastLatencyValue(0)
  316. {
  317. fCarlaPluginDescriptor = carla_get_native_rack_plugin();
  318. DISTRHO_SAFE_ASSERT_RETURN(fCarlaPluginDescriptor != nullptr,);
  319. memset(&fCarlaTimeInfo, 0, sizeof(fCarlaTimeInfo));
  320. fCarlaHostDescriptor.handle = this;
  321. fCarlaHostDescriptor.resourceDir = carla_get_library_folder();
  322. fCarlaHostDescriptor.uiName = "Ildaeil";
  323. fCarlaHostDescriptor.uiParentId = 0;
  324. fCarlaHostDescriptor.get_buffer_size = host_get_buffer_size;
  325. fCarlaHostDescriptor.get_sample_rate = host_get_sample_rate;
  326. fCarlaHostDescriptor.is_offline = host_is_offline;
  327. fCarlaHostDescriptor.get_time_info = host_get_time_info;
  328. fCarlaHostDescriptor.write_midi_event = host_write_midi_event;
  329. fCarlaHostDescriptor.ui_parameter_changed = host_ui_parameter_changed;
  330. fCarlaHostDescriptor.ui_midi_program_changed = host_ui_midi_program_changed;
  331. fCarlaHostDescriptor.ui_custom_data_changed = host_ui_custom_data_changed;
  332. fCarlaHostDescriptor.ui_closed = host_ui_closed;
  333. fCarlaHostDescriptor.ui_open_file = host_ui_open_file;
  334. fCarlaHostDescriptor.ui_save_file = host_ui_save_file;
  335. fCarlaHostDescriptor.dispatcher = host_dispatcher;
  336. fCarlaPluginHandle = fCarlaPluginDescriptor->instantiate(&fCarlaHostDescriptor);
  337. DISTRHO_SAFE_ASSERT_RETURN(fCarlaPluginHandle != nullptr,);
  338. fCarlaHostHandle = carla_create_native_plugin_host_handle(fCarlaPluginDescriptor, fCarlaPluginHandle);
  339. DISTRHO_SAFE_ASSERT_RETURN(fCarlaHostHandle != nullptr,);
  340. const char* const bundlePath = getBundlePath();
  341. #ifdef CARLA_OS_WIN
  342. #define EXT ".exe"
  343. #else
  344. #define EXT ""
  345. #endif
  346. if (bundlePath != nullptr
  347. && water::File(bundlePath + water::String(DISTRHO_OS_SEP_STR "carla-bridge-native" EXT)).existsAsFile())
  348. {
  349. fBinaryPath = bundlePath;
  350. carla_set_engine_option(fCarlaHostHandle, ENGINE_OPTION_PATH_BINARIES, 0, bundlePath);
  351. carla_set_engine_option(fCarlaHostHandle, ENGINE_OPTION_PATH_RESOURCES, 0, getResourcePath(bundlePath));
  352. }
  353. #ifdef CARLA_OS_MAC
  354. else if (bundlePath != nullptr
  355. && water::File(bundlePath + water::String("/Contents/MacOS/carla-bridge-native" EXT)).existsAsFile())
  356. {
  357. fBinaryPath = bundlePath;
  358. fBinaryPath += "/Contents/MacOS";
  359. carla_set_engine_option(fCarlaHostHandle, ENGINE_OPTION_PATH_BINARIES, 0, fBinaryPath);
  360. carla_set_engine_option(fCarlaHostHandle, ENGINE_OPTION_PATH_RESOURCES, 0, getResourcePath(bundlePath));
  361. }
  362. #endif
  363. else
  364. {
  365. #ifdef CARLA_OS_MAC
  366. fBinaryPath = "/Applications/Carla.app/Contents/MacOS";
  367. carla_set_engine_option(fCarlaHostHandle, ENGINE_OPTION_PATH_BINARIES, 0, "/Applications/Carla.app/Contents/MacOS");
  368. carla_set_engine_option(fCarlaHostHandle, ENGINE_OPTION_PATH_RESOURCES, 0, "/Applications/Carla.app/Contents/MacOS/resources");
  369. #else
  370. fBinaryPath = "/usr/lib/carla";
  371. carla_set_engine_option(fCarlaHostHandle, ENGINE_OPTION_PATH_BINARIES, 0, "/usr/lib/carla");
  372. carla_set_engine_option(fCarlaHostHandle, ENGINE_OPTION_PATH_RESOURCES, 0, "/usr/share/carla/resources");
  373. #endif
  374. }
  375. if (fBinaryPath.isNotEmpty())
  376. carla_stdout("Using binary path for discovery tools: %s", fBinaryPath.buffer());
  377. #undef EXT
  378. carla_set_engine_option(fCarlaHostHandle, ENGINE_OPTION_PLUGIN_PATH, PLUGIN_LADSPA, getPluginPath(PLUGIN_LADSPA));
  379. carla_set_engine_option(fCarlaHostHandle, ENGINE_OPTION_PLUGIN_PATH, PLUGIN_DSSI, getPluginPath(PLUGIN_DSSI));
  380. carla_set_engine_option(fCarlaHostHandle, ENGINE_OPTION_PLUGIN_PATH, PLUGIN_LV2, getPluginPath(PLUGIN_LV2));
  381. carla_set_engine_option(fCarlaHostHandle, ENGINE_OPTION_PLUGIN_PATH, PLUGIN_VST2, getPluginPath(PLUGIN_VST2));
  382. carla_set_engine_option(fCarlaHostHandle, ENGINE_OPTION_PLUGIN_PATH, PLUGIN_VST3, getPluginPath(PLUGIN_VST3));
  383. carla_set_engine_option(fCarlaHostHandle, ENGINE_OPTION_PLUGIN_PATH, PLUGIN_CLAP, getPluginPath(PLUGIN_CLAP));
  384. carla_set_engine_option(fCarlaHostHandle, ENGINE_OPTION_PLUGIN_PATH, PLUGIN_JSFX, getPluginPath(PLUGIN_JSFX));
  385. fCarlaPluginDescriptor->dispatcher(fCarlaPluginHandle, NATIVE_PLUGIN_OPCODE_HOST_USES_EMBED,
  386. 0, 0, nullptr, 0.0f);
  387. #if DISTRHO_PLUGIN_WANT_MIDI_INPUT
  388. fMidiEvents = new NativeMidiEvent[kMaxMidiEventCount];
  389. #endif
  390. #if DISTRHO_PLUGIN_NUM_INPUTS == 0 || DISTRHO_PLUGIN_NUM_OUTPUTS == 0
  391. // create dummy buffers
  392. bufferSizeChanged(getBufferSize());
  393. #endif
  394. }
  395. ~IldaeilPlugin() override
  396. {
  397. if (fCarlaHostHandle != nullptr)
  398. {
  399. carla_host_handle_free(fCarlaHostHandle);
  400. #if DISTRHO_PLUGIN_NUM_INPUTS == 0 || DISTRHO_PLUGIN_NUM_OUTPUTS == 0
  401. delete[] fDummyBuffer;
  402. #endif
  403. #if DISTRHO_PLUGIN_WANT_MIDI_INPUT
  404. delete[] fMidiEvents;
  405. #endif
  406. }
  407. if (fCarlaPluginHandle != nullptr)
  408. fCarlaPluginDescriptor->cleanup(fCarlaPluginHandle);
  409. }
  410. const NativeTimeInfo* hostGetTimeInfo() const noexcept
  411. {
  412. const TimePosition& timePos(getTimePosition());
  413. fCarlaTimeInfo.playing = timePos.playing;
  414. fCarlaTimeInfo.frame = timePos.frame;
  415. fCarlaTimeInfo.bbt.valid = timePos.bbt.valid;
  416. fCarlaTimeInfo.bbt.bar = timePos.bbt.bar;
  417. fCarlaTimeInfo.bbt.beat = timePos.bbt.beat;
  418. fCarlaTimeInfo.bbt.tick = timePos.bbt.tick;
  419. fCarlaTimeInfo.bbt.barStartTick = timePos.bbt.barStartTick;
  420. fCarlaTimeInfo.bbt.beatsPerBar = timePos.bbt.beatsPerBar;
  421. fCarlaTimeInfo.bbt.beatType = timePos.bbt.beatType;
  422. fCarlaTimeInfo.bbt.ticksPerBeat = timePos.bbt.ticksPerBeat;
  423. fCarlaTimeInfo.bbt.beatsPerMinute = timePos.bbt.beatsPerMinute;
  424. return &fCarlaTimeInfo;
  425. }
  426. #if DISTRHO_PLUGIN_WANT_MIDI_OUTPUT
  427. bool hostWriteMidiEvent(const NativeMidiEvent* const event)
  428. {
  429. MidiEvent midiEvent;
  430. midiEvent.frame = event->time;
  431. midiEvent.size = event->size;
  432. midiEvent.dataExt = nullptr;
  433. uint32_t i = 0;
  434. for (; i < event->size; ++i)
  435. midiEvent.data[i] = event->data[i];
  436. for (; i < MidiEvent::kDataSize; ++i)
  437. midiEvent.data[i] = 0;
  438. return writeMidiEvent(midiEvent);
  439. }
  440. #endif
  441. intptr_t hostDispatcher(const NativeHostDispatcherOpcode opcode,
  442. const int32_t index, const intptr_t value, void* const ptr, const float opt)
  443. {
  444. switch (opcode)
  445. {
  446. // cannnot be supported
  447. case NATIVE_HOST_OPCODE_HOST_IDLE:
  448. break;
  449. // other stuff
  450. case NATIVE_HOST_OPCODE_NULL:
  451. case NATIVE_HOST_OPCODE_UPDATE_PARAMETER:
  452. case NATIVE_HOST_OPCODE_UPDATE_MIDI_PROGRAM:
  453. case NATIVE_HOST_OPCODE_RELOAD_PARAMETERS:
  454. case NATIVE_HOST_OPCODE_RELOAD_MIDI_PROGRAMS:
  455. case NATIVE_HOST_OPCODE_RELOAD_ALL:
  456. case NATIVE_HOST_OPCODE_UI_UNAVAILABLE:
  457. case NATIVE_HOST_OPCODE_INTERNAL_PLUGIN:
  458. case NATIVE_HOST_OPCODE_QUEUE_INLINE_DISPLAY:
  459. case NATIVE_HOST_OPCODE_UI_TOUCH_PARAMETER:
  460. case NATIVE_HOST_OPCODE_REQUEST_IDLE:
  461. case NATIVE_HOST_OPCODE_GET_FILE_PATH:
  462. case NATIVE_HOST_OPCODE_UI_RESIZE:
  463. case NATIVE_HOST_OPCODE_PREVIEW_BUFFER_DATA:
  464. // TESTING
  465. d_stdout("dispatcher %i, %i, %li, %p, %f", opcode, index, value, ptr, opt);
  466. break;
  467. }
  468. return 0;
  469. }
  470. protected:
  471. /* --------------------------------------------------------------------------------------------------------
  472. * Information */
  473. const char* getLabel() const override
  474. {
  475. #if ILDAEIL_STANDALONE
  476. return "Ildaeil";
  477. #elif DISTRHO_PLUGIN_IS_SYNTH
  478. return "IldaeilSynth";
  479. #elif DISTRHO_PLUGIN_WANT_MIDI_OUTPUT
  480. return "IldaeilMIDI";
  481. #else
  482. return "IldaeilFX";
  483. #endif
  484. }
  485. const char* getDescription() const override
  486. {
  487. return "Ildaeil is a mini-plugin host working as a plugin, allowing one-to-one plugin format reusage.";
  488. }
  489. const char* getMaker() const override
  490. {
  491. return "DISTRHO";
  492. }
  493. const char* getHomePage() const override
  494. {
  495. return "https://github.com/DISTRHO/Ildaeil";
  496. }
  497. const char* getLicense() const override
  498. {
  499. return "GPLv2+";
  500. }
  501. uint32_t getVersion() const override
  502. {
  503. return d_version(1, 3, 0);
  504. }
  505. int64_t getUniqueId() const override
  506. {
  507. #if ILDAEIL_STANDALONE
  508. return d_cconst('d', 'I', 'l', 'd');
  509. #elif DISTRHO_PLUGIN_IS_SYNTH
  510. return d_cconst('d', 'I', 'l', 'S');
  511. #elif DISTRHO_PLUGIN_WANT_MIDI_OUTPUT
  512. return d_cconst('d', 'I', 'l', 'M');
  513. #else
  514. return d_cconst('d', 'I', 'l', 'F');
  515. #endif
  516. }
  517. /* --------------------------------------------------------------------------------------------------------
  518. * Init */
  519. void initAudioPort(bool input, uint32_t index, AudioPort& port) override
  520. {
  521. port.groupId = kPortGroupStereo;
  522. Plugin::initAudioPort(input, index, port);
  523. }
  524. void initState(const uint32_t index, State& state) override
  525. {
  526. DISTRHO_SAFE_ASSERT_RETURN(index == 0,);
  527. state.hints = kStateIsOnlyForDSP;
  528. state.key = "project";
  529. state.defaultValue = ""
  530. "<?xml version='1.0' encoding='UTF-8'?>\n"
  531. "<!DOCTYPE CARLA-PROJECT>\n"
  532. "<CARLA-PROJECT VERSION='" CARLA_VERSION_STRMIN "'>\n"
  533. "</CARLA-PROJECT>\n";
  534. }
  535. /* --------------------------------------------------------------------------------------------------------
  536. * Internal data */
  537. String getState(const char* const key) const override
  538. {
  539. if (std::strcmp(key, "project") == 0)
  540. {
  541. CarlaEngine* const engine = carla_get_engine_from_handle(fCarlaHostHandle);
  542. fLastProjectState.reset();
  543. engine->saveProjectInternal(fLastProjectState);
  544. return String(static_cast<char*>(fLastProjectState.getDataAndRelease()), false);
  545. }
  546. return String();
  547. }
  548. void setState(const char* const key, const char* const value) override
  549. {
  550. if (std::strcmp(key, "project") == 0)
  551. {
  552. CarlaEngine* const engine = carla_get_engine_from_handle(fCarlaHostHandle);
  553. water::XmlDocument xml(value);
  554. {
  555. const MutexLocker cml(sPluginInfoLoadMutex);
  556. engine->loadProjectInternal(xml, true);
  557. }
  558. if (fUI != nullptr)
  559. ildaeilProjectLoadedFromDSP(fUI);
  560. }
  561. }
  562. /* --------------------------------------------------------------------------------------------------------
  563. * Process */
  564. void checkLatencyChanged()
  565. {
  566. if (fCarlaHostHandle == nullptr)
  567. return;
  568. uint32_t latency = 0;
  569. for (uint32_t i=0; i < carla_get_current_plugin_count(fCarlaHostHandle); ++i)
  570. latency += carla_get_plugin_latency(fCarlaHostHandle, i);
  571. if (fLastLatencyValue != latency)
  572. {
  573. fLastLatencyValue = latency;
  574. setLatency(latency);
  575. }
  576. }
  577. void activate() override
  578. {
  579. if (fCarlaPluginHandle != nullptr)
  580. fCarlaPluginDescriptor->activate(fCarlaPluginHandle);
  581. checkLatencyChanged();
  582. }
  583. void deactivate() override
  584. {
  585. checkLatencyChanged();
  586. if (fCarlaPluginHandle != nullptr)
  587. fCarlaPluginDescriptor->deactivate(fCarlaPluginHandle);
  588. }
  589. #if DISTRHO_PLUGIN_WANT_MIDI_INPUT
  590. void run(const float** inputs, float** outputs, uint32_t frames,
  591. const MidiEvent* dpfMidiEvents, uint32_t dpfMidiEventCount) override
  592. #else
  593. void run(const float** inputs, float** outputs, uint32_t frames) override
  594. #endif
  595. {
  596. if (fCarlaPluginHandle != nullptr)
  597. {
  598. #if DISTRHO_PLUGIN_WANT_MIDI_INPUT
  599. uint32_t midiEventCount = 0;
  600. for (uint32_t i=0; i < dpfMidiEventCount; ++i)
  601. {
  602. const MidiEvent& dpfMidiEvent(dpfMidiEvents[i]);
  603. if (dpfMidiEvent.size > 4)
  604. continue;
  605. NativeMidiEvent& midiEvent(fMidiEvents[midiEventCount]);
  606. midiEvent.time = dpfMidiEvent.frame;
  607. midiEvent.port = 0;
  608. midiEvent.size = dpfMidiEvent.size;
  609. std::memcpy(midiEvent.data, dpfMidiEvent.data, midiEvent.size);
  610. if (++midiEventCount == kMaxMidiEventCount)
  611. break;
  612. }
  613. #else
  614. static constexpr const NativeMidiEvent* fMidiEvents = nullptr;
  615. static constexpr const uint32_t midiEventCount = 0;
  616. #endif
  617. #if DISTRHO_PLUGIN_NUM_INPUTS == 0
  618. inputs = (const float**)fDummyBuffers;
  619. #endif
  620. #if DISTRHO_PLUGIN_NUM_OUTPUTS == 0
  621. outputs = fDummyBuffers;
  622. #endif
  623. fCarlaPluginDescriptor->process(fCarlaPluginHandle, (float**)inputs, outputs, frames,
  624. fMidiEvents, midiEventCount);
  625. checkLatencyChanged();
  626. }
  627. else
  628. {
  629. std::memset(outputs[0], 0, sizeof(float)*frames);
  630. std::memset(outputs[1], 0, sizeof(float)*frames);
  631. }
  632. }
  633. void bufferSizeChanged(const uint32_t newBufferSize) override
  634. {
  635. #if DISTRHO_PLUGIN_NUM_INPUTS == 0 || DISTRHO_PLUGIN_NUM_OUTPUTS == 0
  636. delete[] fDummyBuffer;
  637. fDummyBuffer = new float[newBufferSize];
  638. fDummyBuffers[0] = fDummyBuffer;
  639. fDummyBuffers[1] = fDummyBuffer;
  640. std::memset(fDummyBuffer, 0, sizeof(float)*newBufferSize);
  641. #endif
  642. if (fCarlaPluginHandle != nullptr)
  643. fCarlaPluginDescriptor->dispatcher(fCarlaPluginHandle, NATIVE_PLUGIN_OPCODE_BUFFER_SIZE_CHANGED,
  644. 0, newBufferSize, nullptr, 0.0f);
  645. }
  646. void sampleRateChanged(const double newSampleRate) override
  647. {
  648. if (fCarlaPluginHandle != nullptr)
  649. fCarlaPluginDescriptor->dispatcher(fCarlaPluginHandle, NATIVE_PLUGIN_OPCODE_SAMPLE_RATE_CHANGED,
  650. 0, 0, nullptr, newSampleRate);
  651. }
  652. // -------------------------------------------------------------------------------------------------------
  653. /**
  654. Set our plugin class as non-copyable and add a leak detector just in case.
  655. */
  656. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(IldaeilPlugin)
  657. };
  658. // -----------------------------------------------------------------------------------------------------------
  659. static uint32_t host_get_buffer_size(const NativeHostHandle handle)
  660. {
  661. return static_cast<IldaeilPlugin*>(handle)->getBufferSize();
  662. }
  663. static double host_get_sample_rate(const NativeHostHandle handle)
  664. {
  665. return static_cast<IldaeilPlugin*>(handle)->getSampleRate();
  666. }
  667. static bool host_is_offline(NativeHostHandle)
  668. {
  669. return false;
  670. }
  671. static const NativeTimeInfo* host_get_time_info(const NativeHostHandle handle)
  672. {
  673. return static_cast<IldaeilPlugin*>(handle)->hostGetTimeInfo();
  674. }
  675. static bool host_write_midi_event(const NativeHostHandle handle, const NativeMidiEvent* const event)
  676. {
  677. #if DISTRHO_PLUGIN_WANT_MIDI_OUTPUT
  678. return static_cast<IldaeilPlugin*>(handle)->hostWriteMidiEvent(event);
  679. #else
  680. return handle != nullptr && event != nullptr && false;
  681. #endif
  682. }
  683. static void host_ui_parameter_changed(const NativeHostHandle handle, const uint32_t index, const float value)
  684. {
  685. ildaeilParameterChangeForUI(static_cast<IldaeilPlugin*>(handle)->fUI, index, value);
  686. }
  687. static void host_ui_midi_program_changed(NativeHostHandle handle, uint8_t channel, uint32_t bank, uint32_t program)
  688. {
  689. d_stdout("%s %p %u %u %u", __FUNCTION__, handle, channel, bank, program);
  690. }
  691. static void host_ui_custom_data_changed(NativeHostHandle handle, const char* key, const char* value)
  692. {
  693. d_stdout("%s %p %s %s", __FUNCTION__, handle, key, value);
  694. }
  695. static void host_ui_closed(NativeHostHandle handle)
  696. {
  697. ildaeilCloseUI(static_cast<IldaeilPlugin*>(handle));
  698. }
  699. static const char* host_ui_open_file(const NativeHostHandle handle, const bool isDir, const char* const title, const char* const filter)
  700. {
  701. return ildaeilOpenFileForUI(static_cast<IldaeilPlugin*>(handle)->fUI, isDir, title, filter);
  702. }
  703. static const char* host_ui_save_file(NativeHostHandle, bool, const char*, const char*)
  704. {
  705. return nullptr;
  706. }
  707. static intptr_t host_dispatcher(const NativeHostHandle handle, const NativeHostDispatcherOpcode opcode,
  708. const int32_t index, const intptr_t value, void* const ptr, const float opt)
  709. {
  710. return static_cast<IldaeilPlugin*>(handle)->hostDispatcher(opcode, index, value, ptr, opt);
  711. }
  712. /* --------------------------------------------------------------------------------------------------------------------
  713. * Plugin entry point, called by DPF to create a new plugin instance. */
  714. Plugin* createPlugin()
  715. {
  716. return new IldaeilPlugin();
  717. }
  718. // --------------------------------------------------------------------------------------------------------------------
  719. END_NAMESPACE_DISTRHO