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.

3025 lines
124KB

  1. /*
  2. * Carla LV2 utils
  3. * Copyright (C) 2011-2019 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #ifndef CARLA_LV2_UTILS_HPP_INCLUDED
  18. #define CARLA_LV2_UTILS_HPP_INCLUDED
  19. #include "CarlaMathUtils.hpp"
  20. #ifndef nullptr
  21. # undef NULL
  22. # define NULL nullptr
  23. #endif
  24. // disable -Wdocumentation for LV2 headers
  25. #if defined(__clang__) && (__clang_major__ * 100 + __clang_minor__) > 300
  26. # pragma clang diagnostic push
  27. # pragma clang diagnostic ignored "-Wdocumentation"
  28. #endif
  29. #include "lv2/lv2.h"
  30. #include "lv2/atom.h"
  31. #include "lv2/atom-forge.h"
  32. #include "lv2/atom-helpers.h"
  33. #include "lv2/atom-util.h"
  34. #include "lv2/buf-size.h"
  35. #include "lv2/data-access.h"
  36. // dynmanifest
  37. #include "lv2/event.h"
  38. #include "lv2/event-helpers.h"
  39. #include "lv2/inline-display.h"
  40. #include "lv2/instance-access.h"
  41. #include "lv2/log.h"
  42. // logger
  43. #include "lv2/midi.h"
  44. // morph
  45. #include "lv2/options.h"
  46. #include "lv2/parameters.h"
  47. #include "lv2/patch.h"
  48. #include "lv2/port-groups.h"
  49. #include "lv2/port-props.h"
  50. #include "lv2/presets.h"
  51. #include "lv2/resize-port.h"
  52. #include "lv2/state.h"
  53. #include "lv2/time.h"
  54. #include "lv2/ui.h"
  55. #include "lv2/units.h"
  56. #include "lv2/uri-map.h"
  57. #include "lv2/urid.h"
  58. #include "lv2/worker.h"
  59. #include "lv2/lv2-miditype.h"
  60. #include "lv2/lv2-midifunctions.h"
  61. #include "lv2/lv2_external_ui.h"
  62. #include "lv2/lv2_kxstudio_properties.h"
  63. #include "lv2/lv2_programs.h"
  64. #include "lv2/lv2_rtmempool.h"
  65. #include "lilv/lilvmm.hpp"
  66. #include "sratom/sratom.h"
  67. #include "lilv/config/lilv_config.h"
  68. // enable -Wdocumentation again
  69. #if defined(__clang__) && (__clang_major__ * 100 + __clang_minor__) > 300
  70. # pragma clang diagnostic pop
  71. #endif
  72. #include "lv2_rdf.hpp"
  73. #ifdef USE_QT
  74. # include <QtCore/QStringList>
  75. #else
  76. # include "water/text/StringArray.h"
  77. #endif
  78. // used for scalepoint sorting
  79. #include <map>
  80. typedef std::map<double,const LilvScalePoint*> LilvScalePointMap;
  81. // --------------------------------------------------------------------------------------------------------------------
  82. // Define namespaces and missing prefixes
  83. #define NS_dct "http://purl.org/dc/terms/"
  84. #define NS_doap "http://usefulinc.com/ns/doap#"
  85. #define NS_rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  86. #define NS_rdfs "http://www.w3.org/2000/01/rdf-schema#"
  87. #define NS_llmm "http://ll-plugins.nongnu.org/lv2/ext/midimap#"
  88. #define NS_devp "http://lv2plug.in/ns/dev/extportinfo#"
  89. #define NS_mod "http://moddevices.com/ns/modgui#"
  90. #define LV2_MIDI_Map__CC "http://ll-plugins.nongnu.org/lv2/namespace#CC"
  91. #define LV2_MIDI_Map__NRPN "http://ll-plugins.nongnu.org/lv2/namespace#NRPN"
  92. #define LV2_MIDI_LL__MidiPort "http://ll-plugins.nongnu.org/lv2/ext/MidiPort"
  93. #define LV2_UI__makeResident LV2_UI_PREFIX "makeResident"
  94. #define LV2_UI__makeSONameResident LV2_UI_PREFIX "makeSONameResident"
  95. // TODO: update LV2 headers once again
  96. #define LV2_CORE__Parameter LV2_CORE_PREFIX "Parameter" ///< http://lv2plug.in/ns/lv2core#Parameter
  97. #define LV2_CORE__enabled LV2_CORE_PREFIX "enabled" ///< http://lv2plug.in/ns/lv2core#enabled
  98. // --------------------------------------------------------------------------------------------------------------------
  99. // Custom Atom types
  100. struct LV2_Atom_MidiEvent {
  101. LV2_Atom atom; /**< Atom header. */
  102. uint8_t data[4]; /**< MIDI data (body). */
  103. };
  104. static inline
  105. uint32_t lv2_atom_total_size(const LV2_Atom_MidiEvent& midiEv)
  106. {
  107. return static_cast<uint32_t>(sizeof(LV2_Atom)) + midiEv.atom.size;
  108. }
  109. // ---------------------------------------------------------------------------------------------------------------------
  110. // -Weffc++ compat ext widget
  111. extern "C" {
  112. typedef struct _LV2_External_UI_Widget_Compat {
  113. void (*run )(struct _LV2_External_UI_Widget_Compat*);
  114. void (*show)(struct _LV2_External_UI_Widget_Compat*);
  115. void (*hide)(struct _LV2_External_UI_Widget_Compat*);
  116. _LV2_External_UI_Widget_Compat() noexcept
  117. : run(nullptr), show(nullptr), hide(nullptr) {}
  118. } LV2_External_UI_Widget_Compat;
  119. }
  120. // --------------------------------------------------------------------------------------------------------------------
  121. // Our LV2 World class
  122. class Lv2WorldClass : public Lilv::World
  123. {
  124. public:
  125. // Base Types
  126. Lilv::Node port;
  127. Lilv::Node symbol;
  128. Lilv::Node designation;
  129. Lilv::Node freeWheeling;
  130. Lilv::Node reportsLatency;
  131. // Plugin Types
  132. Lilv::Node class_allpass;
  133. Lilv::Node class_amplifier;
  134. Lilv::Node class_analyzer;
  135. Lilv::Node class_bandpass;
  136. Lilv::Node class_chorus;
  137. Lilv::Node class_comb;
  138. Lilv::Node class_compressor;
  139. Lilv::Node class_constant;
  140. Lilv::Node class_converter;
  141. Lilv::Node class_delay;
  142. Lilv::Node class_distortion;
  143. Lilv::Node class_dynamics;
  144. Lilv::Node class_eq;
  145. Lilv::Node class_envelope;
  146. Lilv::Node class_expander;
  147. Lilv::Node class_filter;
  148. Lilv::Node class_flanger;
  149. Lilv::Node class_function;
  150. Lilv::Node class_gate;
  151. Lilv::Node class_generator;
  152. Lilv::Node class_highpass;
  153. Lilv::Node class_instrument;
  154. Lilv::Node class_limiter;
  155. Lilv::Node class_lowpass;
  156. Lilv::Node class_mixer;
  157. Lilv::Node class_modulator;
  158. Lilv::Node class_multiEQ;
  159. Lilv::Node class_oscillator;
  160. Lilv::Node class_paraEQ;
  161. Lilv::Node class_phaser;
  162. Lilv::Node class_pitch;
  163. Lilv::Node class_reverb;
  164. Lilv::Node class_simulator;
  165. Lilv::Node class_spatial;
  166. Lilv::Node class_spectral;
  167. Lilv::Node class_utility;
  168. Lilv::Node class_waveshaper;
  169. // Port Types
  170. Lilv::Node port_input;
  171. Lilv::Node port_output;
  172. Lilv::Node port_control;
  173. Lilv::Node port_audio;
  174. Lilv::Node port_cv;
  175. Lilv::Node port_atom;
  176. Lilv::Node port_event;
  177. Lilv::Node port_midi;
  178. // Port Properties
  179. Lilv::Node pprop_optional;
  180. Lilv::Node pprop_enumeration;
  181. Lilv::Node pprop_integer;
  182. Lilv::Node pprop_sampleRate;
  183. Lilv::Node pprop_toggled;
  184. Lilv::Node pprop_artifacts;
  185. Lilv::Node pprop_continuousCV;
  186. Lilv::Node pprop_discreteCV;
  187. Lilv::Node pprop_expensive;
  188. Lilv::Node pprop_strictBounds;
  189. Lilv::Node pprop_logarithmic;
  190. Lilv::Node pprop_notAutomatic;
  191. Lilv::Node pprop_notOnGUI;
  192. Lilv::Node pprop_trigger;
  193. Lilv::Node pprop_nonAutomable;
  194. // Unit Hints
  195. Lilv::Node unit_name;
  196. Lilv::Node unit_render;
  197. Lilv::Node unit_symbol;
  198. Lilv::Node unit_unit;
  199. // UI Types
  200. Lilv::Node ui;
  201. Lilv::Node ui_gtk2;
  202. Lilv::Node ui_gtk3;
  203. Lilv::Node ui_qt4;
  204. Lilv::Node ui_qt5;
  205. Lilv::Node ui_cocoa;
  206. Lilv::Node ui_windows;
  207. Lilv::Node ui_x11;
  208. Lilv::Node ui_external;
  209. Lilv::Node ui_externalOld;
  210. // Misc
  211. Lilv::Node atom_bufferType;
  212. Lilv::Node atom_sequence;
  213. Lilv::Node atom_supports;
  214. Lilv::Node patch_writable;
  215. Lilv::Node parameter;
  216. Lilv::Node preset_preset;
  217. Lilv::Node state_state;
  218. Lilv::Node ui_portIndex;
  219. Lilv::Node ui_portNotif;
  220. Lilv::Node ui_protocol;
  221. Lilv::Node value_default;
  222. Lilv::Node value_minimum;
  223. Lilv::Node value_maximum;
  224. Lilv::Node rz_asLargeAs;
  225. Lilv::Node rz_minSize;
  226. // Port Data Types
  227. Lilv::Node midi_event;
  228. Lilv::Node patch_message;
  229. Lilv::Node time_position;
  230. // MIDI CC
  231. Lilv::Node mm_defaultControl;
  232. Lilv::Node mm_controlType;
  233. Lilv::Node mm_controlNumber;
  234. // Other
  235. Lilv::Node dct_replaces;
  236. Lilv::Node doap_license;
  237. Lilv::Node rdf_type;
  238. Lilv::Node rdfs_comment;
  239. Lilv::Node rdfs_label;
  240. Lilv::Node rdfs_range;
  241. bool needsInit;
  242. const LilvPlugins* allPlugins;
  243. const LilvPlugin** cachedPlugins;
  244. uint pluginCount;
  245. // ----------------------------------------------------------------------------------------------------------------
  246. Lv2WorldClass()
  247. : Lilv::World(),
  248. port (new_uri(LV2_CORE__port)),
  249. symbol (new_uri(LV2_CORE__symbol)),
  250. designation (new_uri(LV2_CORE__designation)),
  251. freeWheeling (new_uri(LV2_CORE__freeWheeling)),
  252. reportsLatency (new_uri(LV2_CORE__reportsLatency)),
  253. class_allpass (new_uri(LV2_CORE__AllpassPlugin)),
  254. class_amplifier (new_uri(LV2_CORE__AmplifierPlugin)),
  255. class_analyzer (new_uri(LV2_CORE__AnalyserPlugin)),
  256. class_bandpass (new_uri(LV2_CORE__BandpassPlugin)),
  257. class_chorus (new_uri(LV2_CORE__ChorusPlugin)),
  258. class_comb (new_uri(LV2_CORE__CombPlugin)),
  259. class_compressor (new_uri(LV2_CORE__CompressorPlugin)),
  260. class_constant (new_uri(LV2_CORE__ConstantPlugin)),
  261. class_converter (new_uri(LV2_CORE__ConverterPlugin)),
  262. class_delay (new_uri(LV2_CORE__DelayPlugin)),
  263. class_distortion (new_uri(LV2_CORE__DistortionPlugin)),
  264. class_dynamics (new_uri(LV2_CORE__DynamicsPlugin)),
  265. class_eq (new_uri(LV2_CORE__EQPlugin)),
  266. class_envelope (new_uri(LV2_CORE__EnvelopePlugin)),
  267. class_expander (new_uri(LV2_CORE__ExpanderPlugin)),
  268. class_filter (new_uri(LV2_CORE__FilterPlugin)),
  269. class_flanger (new_uri(LV2_CORE__FlangerPlugin)),
  270. class_function (new_uri(LV2_CORE__FunctionPlugin)),
  271. class_gate (new_uri(LV2_CORE__GatePlugin)),
  272. class_generator (new_uri(LV2_CORE__GeneratorPlugin)),
  273. class_highpass (new_uri(LV2_CORE__HighpassPlugin)),
  274. class_instrument (new_uri(LV2_CORE__InstrumentPlugin)),
  275. class_limiter (new_uri(LV2_CORE__LimiterPlugin)),
  276. class_lowpass (new_uri(LV2_CORE__LowpassPlugin)),
  277. class_mixer (new_uri(LV2_CORE__MixerPlugin)),
  278. class_modulator (new_uri(LV2_CORE__ModulatorPlugin)),
  279. class_multiEQ (new_uri(LV2_CORE__MultiEQPlugin)),
  280. class_oscillator (new_uri(LV2_CORE__OscillatorPlugin)),
  281. class_paraEQ (new_uri(LV2_CORE__ParaEQPlugin)),
  282. class_phaser (new_uri(LV2_CORE__PhaserPlugin)),
  283. class_pitch (new_uri(LV2_CORE__PitchPlugin)),
  284. class_reverb (new_uri(LV2_CORE__ReverbPlugin)),
  285. class_simulator (new_uri(LV2_CORE__SimulatorPlugin)),
  286. class_spatial (new_uri(LV2_CORE__SpatialPlugin)),
  287. class_spectral (new_uri(LV2_CORE__SpectralPlugin)),
  288. class_utility (new_uri(LV2_CORE__UtilityPlugin)),
  289. class_waveshaper (new_uri(LV2_CORE__WaveshaperPlugin)),
  290. port_input (new_uri(LV2_CORE__InputPort)),
  291. port_output (new_uri(LV2_CORE__OutputPort)),
  292. port_control (new_uri(LV2_CORE__ControlPort)),
  293. port_audio (new_uri(LV2_CORE__AudioPort)),
  294. port_cv (new_uri(LV2_CORE__CVPort)),
  295. port_atom (new_uri(LV2_ATOM__AtomPort)),
  296. port_event (new_uri(LV2_EVENT__EventPort)),
  297. port_midi (new_uri(LV2_MIDI_LL__MidiPort)),
  298. pprop_optional (new_uri(LV2_CORE__connectionOptional)),
  299. pprop_enumeration (new_uri(LV2_CORE__enumeration)),
  300. pprop_integer (new_uri(LV2_CORE__integer)),
  301. pprop_sampleRate (new_uri(LV2_CORE__sampleRate)),
  302. pprop_toggled (new_uri(LV2_CORE__toggled)),
  303. pprop_artifacts (new_uri(LV2_PORT_PROPS__causesArtifacts)),
  304. pprop_continuousCV (new_uri(LV2_PORT_PROPS__continuousCV)),
  305. pprop_discreteCV (new_uri(LV2_PORT_PROPS__discreteCV)),
  306. pprop_expensive (new_uri(LV2_PORT_PROPS__expensive)),
  307. pprop_strictBounds (new_uri(LV2_PORT_PROPS__hasStrictBounds)),
  308. pprop_logarithmic (new_uri(LV2_PORT_PROPS__logarithmic)),
  309. pprop_notAutomatic (new_uri(LV2_PORT_PROPS__notAutomatic)),
  310. pprop_notOnGUI (new_uri(LV2_PORT_PROPS__notOnGUI)),
  311. pprop_trigger (new_uri(LV2_PORT_PROPS__trigger)),
  312. pprop_nonAutomable (new_uri(LV2_KXSTUDIO_PROPERTIES__NonAutomable)),
  313. unit_name (new_uri(LV2_UNITS__name)),
  314. unit_render (new_uri(LV2_UNITS__render)),
  315. unit_symbol (new_uri(LV2_UNITS__symbol)),
  316. unit_unit (new_uri(LV2_UNITS__unit)),
  317. ui (new_uri(LV2_UI__UI)),
  318. ui_gtk2 (new_uri(LV2_UI__GtkUI)),
  319. ui_gtk3 (new_uri(LV2_UI__Gtk3UI)),
  320. ui_qt4 (new_uri(LV2_UI__Qt4UI)),
  321. ui_qt5 (new_uri(LV2_UI__Qt5UI)),
  322. ui_cocoa (new_uri(LV2_UI__CocoaUI)),
  323. ui_windows (new_uri(LV2_UI__WindowsUI)),
  324. ui_x11 (new_uri(LV2_UI__X11UI)),
  325. ui_external (new_uri(LV2_EXTERNAL_UI__Widget)),
  326. ui_externalOld (new_uri(LV2_EXTERNAL_UI_DEPRECATED_URI)),
  327. atom_bufferType (new_uri(LV2_ATOM__bufferType)),
  328. atom_sequence (new_uri(LV2_ATOM__Sequence)),
  329. atom_supports (new_uri(LV2_ATOM__supports)),
  330. patch_writable (new_uri(LV2_PATCH__writable)),
  331. parameter (new_uri(LV2_CORE__Parameter)),
  332. preset_preset (new_uri(LV2_PRESETS__Preset)),
  333. state_state (new_uri(LV2_STATE__state)),
  334. ui_portIndex (new_uri(LV2_UI__portIndex)),
  335. ui_portNotif (new_uri(LV2_UI__portNotification)),
  336. ui_protocol (new_uri(LV2_UI__protocol)),
  337. value_default (new_uri(LV2_CORE__default)),
  338. value_minimum (new_uri(LV2_CORE__minimum)),
  339. value_maximum (new_uri(LV2_CORE__maximum)),
  340. rz_asLargeAs (new_uri(LV2_RESIZE_PORT__asLargeAs)),
  341. rz_minSize (new_uri(LV2_RESIZE_PORT__minimumSize)),
  342. midi_event (new_uri(LV2_MIDI__MidiEvent)),
  343. patch_message (new_uri(LV2_PATCH__Message)),
  344. time_position (new_uri(LV2_TIME__Position)),
  345. mm_defaultControl (new_uri(NS_llmm "defaultMidiController")),
  346. mm_controlType (new_uri(NS_llmm "controllerType")),
  347. mm_controlNumber (new_uri(NS_llmm "controllerNumber")),
  348. dct_replaces (new_uri(NS_dct "replaces")),
  349. doap_license (new_uri(NS_doap "license")),
  350. rdf_type (new_uri(NS_rdf "type")),
  351. rdfs_comment (new_uri(NS_rdfs "comment")),
  352. rdfs_label (new_uri(NS_rdfs "label")),
  353. rdfs_range (new_uri(NS_rdfs "range")),
  354. needsInit(true),
  355. allPlugins(nullptr),
  356. cachedPlugins(nullptr),
  357. pluginCount(0) {}
  358. ~Lv2WorldClass() override
  359. {
  360. pluginCount = 0;
  361. allPlugins = nullptr;
  362. if (cachedPlugins != nullptr)
  363. {
  364. delete[] cachedPlugins;
  365. cachedPlugins = nullptr;
  366. }
  367. }
  368. // FIXME - remove this
  369. static Lv2WorldClass& getInstance()
  370. {
  371. static Lv2WorldClass lv2World;
  372. return lv2World;
  373. }
  374. void initIfNeeded(const char* LV2_PATH)
  375. {
  376. if (LV2_PATH == nullptr || LV2_PATH[0] == '\0')
  377. {
  378. static const char* const DEFAULT_LV2_PATH = LILV_DEFAULT_LV2_PATH;
  379. LV2_PATH = DEFAULT_LV2_PATH;
  380. }
  381. if (! needsInit)
  382. return;
  383. needsInit = false;
  384. Lilv::World::load_all(LV2_PATH);
  385. allPlugins = lilv_world_get_all_plugins(this->me);
  386. CARLA_SAFE_ASSERT_RETURN(allPlugins != nullptr,);
  387. if ((pluginCount = lilv_plugins_size(allPlugins)))
  388. {
  389. cachedPlugins = new const LilvPlugin*[pluginCount+1];
  390. carla_zeroPointers(cachedPlugins, pluginCount+1);
  391. int i = 0;
  392. for (LilvIter* it = lilv_plugins_begin(allPlugins); ! lilv_plugins_is_end(allPlugins, it); it = lilv_plugins_next(allPlugins, it))
  393. cachedPlugins[i++] = lilv_plugins_get(allPlugins, it);
  394. }
  395. }
  396. void load_bundle(const char* const bundle)
  397. {
  398. CARLA_SAFE_ASSERT_RETURN(bundle != nullptr && bundle[0] != '\0',);
  399. CARLA_SAFE_ASSERT_RETURN(needsInit,);
  400. needsInit = false;
  401. Lilv::World::load_bundle(Lilv::Node(new_uri(bundle)));
  402. allPlugins = lilv_world_get_all_plugins(this->me);
  403. CARLA_SAFE_ASSERT_RETURN(allPlugins != nullptr,);
  404. if ((pluginCount = lilv_plugins_size(allPlugins)))
  405. {
  406. cachedPlugins = new const LilvPlugin*[pluginCount+1];
  407. carla_zeroPointers(cachedPlugins, pluginCount+1);
  408. int i = 0;
  409. for (LilvIter* it = lilv_plugins_begin(allPlugins); ! lilv_plugins_is_end(allPlugins, it); it = lilv_plugins_next(allPlugins, it))
  410. cachedPlugins[i++] = lilv_plugins_get(allPlugins, it);
  411. }
  412. }
  413. uint getPluginCount() const
  414. {
  415. CARLA_SAFE_ASSERT_RETURN(! needsInit, 0);
  416. return pluginCount;
  417. }
  418. const LilvPlugin* getPluginFromIndex(const uint index) const
  419. {
  420. CARLA_SAFE_ASSERT_RETURN(! needsInit, nullptr);
  421. CARLA_SAFE_ASSERT_RETURN(cachedPlugins != nullptr, nullptr);
  422. CARLA_SAFE_ASSERT_RETURN(index < pluginCount, nullptr);
  423. return cachedPlugins[index];
  424. }
  425. const LilvPlugin* getPluginFromURI(const LV2_URI uri) const
  426. {
  427. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0', nullptr);
  428. CARLA_SAFE_ASSERT_RETURN(! needsInit, nullptr);
  429. CARLA_SAFE_ASSERT_RETURN(allPlugins != nullptr, nullptr);
  430. LilvNode* const uriNode(lilv_new_uri(this->me, uri));
  431. CARLA_SAFE_ASSERT_RETURN(uriNode != nullptr, nullptr);
  432. const LilvPlugin* const cPlugin(lilv_plugins_get_by_uri(allPlugins, uriNode));
  433. lilv_node_free(uriNode);
  434. return cPlugin;
  435. }
  436. LilvState* getStateFromURI(const LV2_URI uri, const LV2_URID_Map* const uridMap) const
  437. {
  438. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0', nullptr);
  439. CARLA_SAFE_ASSERT_RETURN(uridMap != nullptr, nullptr);
  440. CARLA_SAFE_ASSERT_RETURN(! needsInit, nullptr);
  441. LilvNode* const uriNode(lilv_new_uri(this->me, uri));
  442. CARLA_SAFE_ASSERT_RETURN(uriNode != nullptr, nullptr);
  443. CARLA_SAFE_ASSERT(lilv_world_load_resource(this->me, uriNode) >= 0);
  444. LilvState* const cState(lilv_state_new_from_world(this->me, uridMap, uriNode));
  445. lilv_node_free(uriNode);
  446. return cState;
  447. }
  448. CARLA_PREVENT_VIRTUAL_HEAP_ALLOCATION
  449. CARLA_DECLARE_NON_COPY_STRUCT(Lv2WorldClass)
  450. };
  451. // --------------------------------------------------------------------------------------------------------------------
  452. // Our LV2 Plugin base class
  453. #if defined(__clang__)
  454. # pragma clang diagnostic push
  455. # pragma clang diagnostic ignored "-Weffc++"
  456. # pragma clang diagnostic ignored "-Wnon-virtual-dtor"
  457. #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  458. # pragma GCC diagnostic push
  459. # pragma GCC diagnostic ignored "-Weffc++"
  460. # pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
  461. #endif
  462. template<class TimeInfoStruct>
  463. class Lv2PluginBaseClass : public LV2_External_UI_Widget_Compat
  464. {
  465. #if defined(__clang__)
  466. # pragma clang diagnostic pop
  467. #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  468. # pragma GCC diagnostic pop
  469. #endif
  470. public:
  471. Lv2PluginBaseClass(const double sampleRate, const LV2_Feature* const* const features)
  472. : fIsActive(false),
  473. fIsOffline(false),
  474. fUsingNominal(false),
  475. fBufferSize(0),
  476. fSampleRate(sampleRate),
  477. fUridMap(nullptr),
  478. fUridUnmap(nullptr),
  479. fWorker(nullptr),
  480. fTimeInfo(),
  481. fLastPositionData(),
  482. fPorts(),
  483. fURIs(),
  484. fUI()
  485. {
  486. run = extui_run;
  487. show = extui_show;
  488. hide = extui_hide;
  489. if (fSampleRate < 1.0)
  490. {
  491. carla_stderr("Host doesn't provide a valid sample rate");
  492. return;
  493. }
  494. const LV2_Options_Option* options = nullptr;
  495. const LV2_URID_Map* uridMap = nullptr;
  496. const LV2_URID_Unmap* uridUnmap = nullptr;
  497. const LV2_Worker_Schedule* worker = nullptr;
  498. for (int i=0; features[i] != nullptr; ++i)
  499. {
  500. /**/ if (std::strcmp(features[i]->URI, LV2_OPTIONS__options) == 0)
  501. options = (const LV2_Options_Option*)features[i]->data;
  502. else if (std::strcmp(features[i]->URI, LV2_URID__map) == 0)
  503. uridMap = (const LV2_URID_Map*)features[i]->data;
  504. else if (std::strcmp(features[i]->URI, LV2_URID__unmap) == 0)
  505. uridUnmap = (const LV2_URID_Unmap*)features[i]->data;
  506. else if (std::strcmp(features[i]->URI, LV2_WORKER__schedule) == 0)
  507. worker = (const LV2_Worker_Schedule*)features[i]->data;
  508. }
  509. if (options == nullptr || uridMap == nullptr)
  510. {
  511. carla_stderr("Host doesn't provide option and urid-map features");
  512. return;
  513. }
  514. for (int i=0; options[i].key != 0; ++i)
  515. {
  516. if (uridUnmap != nullptr) {
  517. carla_debug("Host option %i:\"%s\"", i, uridUnmap->unmap(uridUnmap->handle, options[i].key));
  518. }
  519. if (options[i].key == uridMap->map(uridMap->handle, LV2_BUF_SIZE__nominalBlockLength))
  520. {
  521. if (options[i].type == uridMap->map(uridMap->handle, LV2_ATOM__Int))
  522. {
  523. const int32_t value(*(const int32_t*)options[i].value);
  524. CARLA_SAFE_ASSERT_CONTINUE(value > 0);
  525. fBufferSize = static_cast<uint32_t>(value);
  526. fUsingNominal = true;
  527. }
  528. else
  529. {
  530. carla_stderr("Host provides nominalBlockLength but has wrong value type");
  531. }
  532. break;
  533. }
  534. if (options[i].key == uridMap->map(uridMap->handle, LV2_BUF_SIZE__maxBlockLength))
  535. {
  536. if (options[i].type == uridMap->map(uridMap->handle, LV2_ATOM__Int))
  537. {
  538. const int32_t value(*(const int32_t*)options[i].value);
  539. CARLA_SAFE_ASSERT_CONTINUE(value > 0);
  540. fBufferSize = static_cast<uint32_t>(value);
  541. }
  542. else
  543. {
  544. carla_stderr("Host provides maxBlockLength but has wrong value type");
  545. }
  546. // no break, continue in case host supports nominalBlockLength
  547. }
  548. }
  549. if (fBufferSize == 0)
  550. {
  551. carla_stderr("Host doesn't provide buffer-size feature");
  552. //return;
  553. // as testing, continue for now
  554. fBufferSize = 1024;
  555. }
  556. fUridMap = uridMap;
  557. fURIs.map(uridMap);
  558. fUridUnmap = uridUnmap;
  559. fWorker = worker;
  560. clearTimeData();
  561. }
  562. virtual ~Lv2PluginBaseClass() {}
  563. bool loadedInProperHost() const noexcept
  564. {
  565. return fUridMap != nullptr && fBufferSize != 0;
  566. }
  567. // ----------------------------------------------------------------------------------------------------------------
  568. void lv2_connect_port(const uint32_t port, void* const dataLocation) noexcept
  569. {
  570. fPorts.connectPort(port, dataLocation);
  571. }
  572. bool lv2_pre_run(const uint32_t frames)
  573. {
  574. CARLA_SAFE_ASSERT_RETURN(fIsActive, false);
  575. fIsOffline = (fPorts.freewheel != nullptr && *fPorts.freewheel >= 0.5f);
  576. // cache midi events and time information first
  577. if (fPorts.usesTime)
  578. {
  579. LV2_ATOM_SEQUENCE_FOREACH(fPorts.eventsIn[0], event)
  580. {
  581. if (event == nullptr)
  582. continue;
  583. if (event->body.type != fURIs.atomBlank && event->body.type != fURIs.atomObject)
  584. continue;
  585. const LV2_Atom_Object* const obj((const LV2_Atom_Object*)&event->body);
  586. if (obj->body.otype != fURIs.timePos)
  587. continue;
  588. LV2_Atom* bar = nullptr;
  589. LV2_Atom* barBeat = nullptr;
  590. LV2_Atom* beatUnit = nullptr;
  591. LV2_Atom* beatsPerBar = nullptr;
  592. LV2_Atom* beatsPerMinute = nullptr;
  593. LV2_Atom* frame = nullptr;
  594. LV2_Atom* speed = nullptr;
  595. LV2_Atom* ticksPerBeat = nullptr;
  596. lv2_atom_object_get(obj,
  597. fURIs.timeBar, &bar,
  598. fURIs.timeBarBeat, &barBeat,
  599. fURIs.timeBeatUnit, &beatUnit,
  600. fURIs.timeBeatsPerBar, &beatsPerBar,
  601. fURIs.timeBeatsPerMinute, &beatsPerMinute,
  602. fURIs.timeFrame, &frame,
  603. fURIs.timeSpeed, &speed,
  604. fURIs.timeTicksPerBeat, &ticksPerBeat,
  605. 0);
  606. // need to handle this first as other values depend on it
  607. if (ticksPerBeat != nullptr)
  608. {
  609. double ticksPerBeatValue = -1.0;
  610. /**/ if (ticksPerBeat->type == fURIs.atomDouble)
  611. ticksPerBeatValue = ((LV2_Atom_Double*)ticksPerBeat)->body;
  612. else if (ticksPerBeat->type == fURIs.atomFloat)
  613. ticksPerBeatValue = ((LV2_Atom_Float*)ticksPerBeat)->body;
  614. else if (ticksPerBeat->type == fURIs.atomInt)
  615. ticksPerBeatValue = static_cast<double>(((LV2_Atom_Int*)ticksPerBeat)->body);
  616. else if (ticksPerBeat->type == fURIs.atomLong)
  617. ticksPerBeatValue = static_cast<double>(((LV2_Atom_Long*)ticksPerBeat)->body);
  618. else
  619. carla_stderr("Unknown lv2 ticksPerBeat value type");
  620. if (ticksPerBeatValue > 0.0)
  621. fTimeInfo.bbt.ticksPerBeat = fLastPositionData.ticksPerBeat = ticksPerBeatValue;
  622. else
  623. carla_stderr("Invalid lv2 ticksPerBeat value");
  624. }
  625. // same
  626. if (speed != nullptr)
  627. {
  628. /**/ if (speed->type == fURIs.atomDouble)
  629. fLastPositionData.speed = ((LV2_Atom_Double*)speed)->body;
  630. else if (speed->type == fURIs.atomFloat)
  631. fLastPositionData.speed = ((LV2_Atom_Float*)speed)->body;
  632. else if (speed->type == fURIs.atomInt)
  633. fLastPositionData.speed = static_cast<double>(((LV2_Atom_Int*)speed)->body);
  634. else if (speed->type == fURIs.atomLong)
  635. fLastPositionData.speed = static_cast<double>(((LV2_Atom_Long*)speed)->body);
  636. else
  637. carla_stderr("Unknown lv2 speed value type");
  638. fTimeInfo.playing = carla_isNotZero(fLastPositionData.speed);
  639. if (fTimeInfo.playing && fLastPositionData.beatsPerMinute > 0.0)
  640. {
  641. fTimeInfo.bbt.beatsPerMinute = fLastPositionData.beatsPerMinute*
  642. std::abs(fLastPositionData.speed);
  643. }
  644. }
  645. if (bar != nullptr)
  646. {
  647. int64_t barValue = -1;
  648. /**/ if (bar->type == fURIs.atomDouble)
  649. barValue = static_cast<int64_t>(((LV2_Atom_Double*)bar)->body);
  650. else if (bar->type == fURIs.atomFloat)
  651. barValue = static_cast<int64_t>(((LV2_Atom_Float*)bar)->body);
  652. else if (bar->type == fURIs.atomInt)
  653. barValue = ((LV2_Atom_Int*)bar)->body;
  654. else if (bar->type == fURIs.atomLong)
  655. barValue = ((LV2_Atom_Long*)bar)->body;
  656. else
  657. carla_stderr("Unknown lv2 bar value type");
  658. if (barValue >= 0 && barValue < INT32_MAX)
  659. {
  660. fLastPositionData.bar = static_cast<int32_t>(barValue);
  661. fLastPositionData.bar_f = static_cast<float>(barValue);
  662. fTimeInfo.bbt.bar = fLastPositionData.bar + 1;
  663. }
  664. else
  665. {
  666. carla_stderr("Invalid lv2 bar value");
  667. }
  668. }
  669. if (barBeat != nullptr)
  670. {
  671. double barBeatValue = -1.0;
  672. /**/ if (barBeat->type == fURIs.atomDouble)
  673. barBeatValue = ((LV2_Atom_Double*)barBeat)->body;
  674. else if (barBeat->type == fURIs.atomFloat)
  675. barBeatValue = ((LV2_Atom_Float*)barBeat)->body;
  676. else if (barBeat->type == fURIs.atomInt)
  677. barBeatValue = static_cast<float>(((LV2_Atom_Int*)barBeat)->body);
  678. else if (barBeat->type == fURIs.atomLong)
  679. barBeatValue = static_cast<float>(((LV2_Atom_Long*)barBeat)->body);
  680. else
  681. carla_stderr("Unknown lv2 barBeat value type");
  682. if (barBeatValue >= 0.0)
  683. {
  684. fLastPositionData.barBeat = static_cast<float>(barBeatValue);
  685. const double rest = std::fmod(barBeatValue, 1.0);
  686. fTimeInfo.bbt.beat = static_cast<int32_t>(barBeatValue-rest+1.0);
  687. fTimeInfo.bbt.tick = static_cast<int32_t>(rest*fTimeInfo.bbt.ticksPerBeat+0.5);
  688. }
  689. else
  690. {
  691. carla_stderr("Invalid lv2 barBeat value");
  692. }
  693. }
  694. if (beatUnit != nullptr)
  695. {
  696. int64_t beatUnitValue = -1;
  697. /**/ if (beatUnit->type == fURIs.atomDouble)
  698. beatUnitValue = static_cast<int64_t>(((LV2_Atom_Double*)beatUnit)->body);
  699. else if (beatUnit->type == fURIs.atomFloat)
  700. beatUnitValue = static_cast<int64_t>(((LV2_Atom_Float*)beatUnit)->body);
  701. else if (beatUnit->type == fURIs.atomInt)
  702. beatUnitValue = ((LV2_Atom_Int*)beatUnit)->body;
  703. else if (beatUnit->type == fURIs.atomLong)
  704. beatUnitValue = ((LV2_Atom_Long*)beatUnit)->body;
  705. else
  706. carla_stderr("Unknown lv2 beatUnit value type");
  707. if (beatUnitValue > 0 && beatUnitValue < UINT32_MAX)
  708. {
  709. fLastPositionData.beatUnit = static_cast<uint32_t>(beatUnitValue);
  710. fTimeInfo.bbt.beatType = static_cast<float>(beatUnitValue);
  711. }
  712. else
  713. {
  714. carla_stderr("Invalid lv2 beatUnit value");
  715. }
  716. }
  717. if (beatsPerBar != nullptr)
  718. {
  719. float beatsPerBarValue = -1.0f;
  720. /**/ if (beatsPerBar->type == fURIs.atomDouble)
  721. beatsPerBarValue = static_cast<float>(((LV2_Atom_Double*)beatsPerBar)->body);
  722. else if (beatsPerBar->type == fURIs.atomFloat)
  723. beatsPerBarValue = ((LV2_Atom_Float*)beatsPerBar)->body;
  724. else if (beatsPerBar->type == fURIs.atomInt)
  725. beatsPerBarValue = static_cast<float>(((LV2_Atom_Int*)beatsPerBar)->body);
  726. else if (beatsPerBar->type == fURIs.atomLong)
  727. beatsPerBarValue = static_cast<float>(((LV2_Atom_Long*)beatsPerBar)->body);
  728. else
  729. carla_stderr("Unknown lv2 beatsPerBar value type");
  730. if (beatsPerBarValue > 0.0f)
  731. fTimeInfo.bbt.beatsPerBar = fLastPositionData.beatsPerBar = beatsPerBarValue;
  732. else
  733. carla_stderr("Invalid lv2 beatsPerBar value");
  734. }
  735. if (beatsPerMinute != nullptr)
  736. {
  737. double beatsPerMinuteValue = -1.0;
  738. /**/ if (beatsPerMinute->type == fURIs.atomDouble)
  739. beatsPerMinuteValue = ((LV2_Atom_Double*)beatsPerMinute)->body;
  740. else if (beatsPerMinute->type == fURIs.atomFloat)
  741. beatsPerMinuteValue = ((LV2_Atom_Float*)beatsPerMinute)->body;
  742. else if (beatsPerMinute->type == fURIs.atomInt)
  743. beatsPerMinuteValue = static_cast<double>(((LV2_Atom_Int*)beatsPerMinute)->body);
  744. else if (beatsPerMinute->type == fURIs.atomLong)
  745. beatsPerMinuteValue = static_cast<double>(((LV2_Atom_Long*)beatsPerMinute)->body);
  746. else
  747. carla_stderr("Unknown lv2 beatsPerMinute value type");
  748. if (beatsPerMinuteValue >= 12.0 && beatsPerMinuteValue <= 999.0)
  749. {
  750. fTimeInfo.bbt.beatsPerMinute = fLastPositionData.beatsPerMinute = beatsPerMinuteValue;
  751. if (carla_isNotZero(fLastPositionData.speed))
  752. fTimeInfo.bbt.beatsPerMinute *= std::abs(fLastPositionData.speed);
  753. }
  754. else
  755. {
  756. carla_stderr("Invalid lv2 beatsPerMinute value");
  757. }
  758. }
  759. if (frame != nullptr)
  760. {
  761. int64_t frameValue = -1;
  762. /**/ if (frame->type == fURIs.atomDouble)
  763. frameValue = static_cast<int64_t>(((LV2_Atom_Double*)frame)->body);
  764. else if (frame->type == fURIs.atomFloat)
  765. frameValue = static_cast<int64_t>(((LV2_Atom_Float*)frame)->body);
  766. else if (frame->type == fURIs.atomInt)
  767. frameValue = ((LV2_Atom_Int*)frame)->body;
  768. else if (frame->type == fURIs.atomLong)
  769. frameValue = ((LV2_Atom_Long*)frame)->body;
  770. else
  771. carla_stderr("Unknown lv2 frame value type");
  772. if (frameValue >= 0)
  773. fTimeInfo.frame = fLastPositionData.frame = static_cast<uint64_t>(frameValue);
  774. else
  775. carla_stderr("Invalid lv2 frame value");
  776. }
  777. fTimeInfo.bbt.barStartTick = static_cast<double>(fTimeInfo.bbt.ticksPerBeat) *
  778. static_cast<double>(fTimeInfo.bbt.beatsPerBar) *
  779. (fTimeInfo.bbt.bar-1);
  780. fTimeInfo.bbt.valid = (fLastPositionData.beatsPerMinute > 0.0 &&
  781. fLastPositionData.beatUnit > 0 &&
  782. fLastPositionData.beatsPerBar > 0.0f);
  783. }
  784. }
  785. // Check for updated parameters
  786. float curValue;
  787. for (uint32_t i=0; i < fPorts.numParams; ++i)
  788. {
  789. if (fPorts.paramsOut[i])
  790. continue;
  791. CARLA_SAFE_ASSERT_CONTINUE(fPorts.paramsPtr[i] != nullptr)
  792. curValue = *fPorts.paramsPtr[i];
  793. if (carla_isEqual(fPorts.paramsLast[i], curValue))
  794. continue;
  795. fPorts.paramsLast[i] = curValue;
  796. handleParameterValueChanged(i, curValue);
  797. }
  798. if (frames == 0)
  799. return false;
  800. // init midi out data
  801. if (fPorts.numMidiOuts > 0 || fPorts.hasUI)
  802. {
  803. for (uint32_t i=0; i<fPorts.numMidiOuts; ++i)
  804. {
  805. LV2_Atom_Sequence* const seq(fPorts.eventsOut[i]);
  806. CARLA_SAFE_ASSERT_CONTINUE(seq != nullptr);
  807. fPorts.eventsOutData[i].capacity = seq->atom.size;
  808. fPorts.eventsOutData[i].offset = 0;
  809. seq->atom.size = sizeof(LV2_Atom_Sequence_Body);
  810. seq->atom.type = fURIs.atomSequence;
  811. seq->body.unit = 0;
  812. seq->body.pad = 0;
  813. }
  814. }
  815. return true;
  816. }
  817. void lv2_post_run(const uint32_t frames)
  818. {
  819. // update timePos for next callback
  820. if (carla_isZero(fLastPositionData.speed))
  821. return;
  822. if (fLastPositionData.speed > 0.0)
  823. {
  824. // playing forwards
  825. fLastPositionData.frame += frames;
  826. }
  827. else
  828. {
  829. // playing backwards
  830. if (frames >= fLastPositionData.frame)
  831. fLastPositionData.frame = 0;
  832. else
  833. fLastPositionData.frame -= frames;
  834. }
  835. fTimeInfo.frame = fLastPositionData.frame;
  836. if (fTimeInfo.bbt.valid)
  837. {
  838. const double beatsPerMinute = fLastPositionData.beatsPerMinute * fLastPositionData.speed;
  839. const double framesPerBeat = 60.0 * fSampleRate / beatsPerMinute;
  840. const double addedBarBeats = double(frames) / framesPerBeat;
  841. if (fLastPositionData.barBeat >= 0.0f)
  842. {
  843. fLastPositionData.barBeat = std::fmod(fLastPositionData.barBeat+static_cast<float>(addedBarBeats),
  844. fLastPositionData.beatsPerBar);
  845. const double rest = std::fmod(fLastPositionData.barBeat, 1.0f);
  846. fTimeInfo.bbt.beat = static_cast<int32_t>(static_cast<double>(fLastPositionData.barBeat)-rest+1.0);
  847. fTimeInfo.bbt.tick = static_cast<int32_t>(rest*fTimeInfo.bbt.ticksPerBeat+0.5);
  848. if (fLastPositionData.bar_f >= 0.0f)
  849. {
  850. fLastPositionData.bar_f += std::floor((fLastPositionData.barBeat+static_cast<float>(addedBarBeats))/
  851. fLastPositionData.beatsPerBar);
  852. if (fLastPositionData.bar_f <= 0.0f)
  853. {
  854. fLastPositionData.bar = 0;
  855. fLastPositionData.bar_f = 0.0f;
  856. }
  857. else
  858. {
  859. fLastPositionData.bar = static_cast<int32_t>(fLastPositionData.bar_f+0.5f);
  860. }
  861. fTimeInfo.bbt.bar = fLastPositionData.bar + 1;
  862. fTimeInfo.bbt.barStartTick = static_cast<double>(fTimeInfo.bbt.ticksPerBeat) *
  863. static_cast<double>(fTimeInfo.bbt.beatsPerBar) *
  864. (fTimeInfo.bbt.bar-1);
  865. }
  866. }
  867. }
  868. }
  869. // ----------------------------------------------------------------------------------------------------------------
  870. uint32_t lv2_get_options(LV2_Options_Option* const /*options*/) const
  871. {
  872. // currently unused
  873. return LV2_OPTIONS_SUCCESS;
  874. }
  875. uint32_t lv2_set_options(const LV2_Options_Option* const options)
  876. {
  877. for (int i=0; options[i].key != 0; ++i)
  878. {
  879. if (options[i].key == fUridMap->map(fUridMap->handle, LV2_BUF_SIZE__nominalBlockLength))
  880. {
  881. if (options[i].type == fURIs.atomInt)
  882. {
  883. const int32_t value(*(const int32_t*)options[i].value);
  884. CARLA_SAFE_ASSERT_CONTINUE(value > 0);
  885. const uint32_t newBufferSize = static_cast<uint32_t>(value);
  886. if (fBufferSize != newBufferSize)
  887. {
  888. fBufferSize = newBufferSize;
  889. handleBufferSizeChanged(newBufferSize);
  890. }
  891. }
  892. else
  893. {
  894. carla_stderr("Host changed nominalBlockLength but with wrong value type");
  895. }
  896. }
  897. else if (options[i].key == fUridMap->map(fUridMap->handle, LV2_BUF_SIZE__maxBlockLength) && ! fUsingNominal)
  898. {
  899. if (options[i].type == fURIs.atomInt)
  900. {
  901. const int32_t value(*(const int32_t*)options[i].value);
  902. CARLA_SAFE_ASSERT_CONTINUE(value > 0);
  903. const uint32_t newBufferSize = static_cast<uint32_t>(value);
  904. if (fBufferSize != newBufferSize)
  905. {
  906. fBufferSize = newBufferSize;
  907. handleBufferSizeChanged(newBufferSize);
  908. }
  909. }
  910. else
  911. {
  912. carla_stderr("Host changed maxBlockLength but with wrong value type");
  913. }
  914. }
  915. else if (options[i].key == fUridMap->map(fUridMap->handle, LV2_PARAMETERS__sampleRate))
  916. {
  917. if (options[i].type == fURIs.atomFloat)
  918. {
  919. const double value(*(const float*)options[i].value);
  920. CARLA_SAFE_ASSERT_CONTINUE(value > 0.0);
  921. if (carla_isNotEqual(fSampleRate, value))
  922. {
  923. fSampleRate = value;
  924. handleSampleRateChanged(value);
  925. }
  926. }
  927. else
  928. {
  929. carla_stderr("Host changed sampleRate but with wrong value type");
  930. }
  931. }
  932. }
  933. return LV2_OPTIONS_SUCCESS;
  934. }
  935. // ----------------------------------------------------------------------------------------------------------------
  936. int lv2ui_idle() const
  937. {
  938. if (! fUI.isVisible)
  939. return 1;
  940. handleUiRun();
  941. return 0;
  942. }
  943. int lv2ui_show()
  944. {
  945. handleUiShow();
  946. return 0;
  947. }
  948. int lv2ui_hide()
  949. {
  950. handleUiHide();
  951. return 0;
  952. }
  953. void lv2ui_cleanup()
  954. {
  955. if (fUI.isVisible)
  956. handleUiHide();
  957. fUI.host = nullptr;
  958. fUI.touch = nullptr;
  959. fUI.writeFunction = nullptr;
  960. fUI.controller = nullptr;
  961. }
  962. // ----------------------------------------------------------------------------------------------------------------
  963. protected:
  964. virtual void handleUiRun() const = 0;
  965. virtual void handleUiShow() = 0;
  966. virtual void handleUiHide() = 0;
  967. virtual void handleParameterValueChanged(const uint32_t index, const float value) = 0;
  968. virtual void handleBufferSizeChanged(const uint32_t bufferSize) = 0;
  969. virtual void handleSampleRateChanged(const double sampleRate) = 0;
  970. void resetTimeInfo() noexcept
  971. {
  972. clearTimeData();
  973. // hosts may not send all values, resulting on some invalid data
  974. fTimeInfo.bbt.bar = 1;
  975. fTimeInfo.bbt.beat = 1;
  976. fTimeInfo.bbt.beatsPerBar = 4;
  977. fTimeInfo.bbt.beatType = 4;
  978. fTimeInfo.bbt.ticksPerBeat = fLastPositionData.ticksPerBeat = 960.0;
  979. fTimeInfo.bbt.beatsPerMinute = fLastPositionData.beatsPerMinute = 120.0;
  980. }
  981. // LV2 host data
  982. bool fIsActive : 1;
  983. bool fIsOffline : 1;
  984. bool fUsingNominal : 1;
  985. uint32_t fBufferSize;
  986. double fSampleRate;
  987. // LV2 host features
  988. const LV2_URID_Map* fUridMap;
  989. const LV2_URID_Unmap* fUridUnmap;
  990. const LV2_Worker_Schedule* fWorker;
  991. // Time info stuff
  992. TimeInfoStruct fTimeInfo;
  993. struct Lv2PositionData {
  994. int32_t bar;
  995. float bar_f;
  996. float barBeat;
  997. uint32_t beatUnit;
  998. float beatsPerBar;
  999. double beatsPerMinute;
  1000. uint64_t frame;
  1001. double speed;
  1002. double ticksPerBeat;
  1003. Lv2PositionData()
  1004. : bar(-1),
  1005. bar_f(-1.0f),
  1006. barBeat(-1.0f),
  1007. beatUnit(0),
  1008. beatsPerBar(0.0f),
  1009. beatsPerMinute(-1.0),
  1010. frame(0),
  1011. speed(0.0),
  1012. ticksPerBeat(-1.0) {}
  1013. void clear()
  1014. {
  1015. bar = -1;
  1016. bar_f = -1.0f;
  1017. barBeat = -1.0f;
  1018. beatUnit = 0;
  1019. beatsPerBar = 0.0f;
  1020. beatsPerMinute = -1.0;
  1021. frame = 0;
  1022. speed = 0.0;
  1023. ticksPerBeat = -1.0;
  1024. }
  1025. } fLastPositionData;
  1026. // Port stuff
  1027. struct Ports {
  1028. // need to save current state
  1029. struct EventsOutData {
  1030. uint32_t capacity;
  1031. uint32_t offset;
  1032. EventsOutData()
  1033. : capacity(0),
  1034. offset(0) {}
  1035. };
  1036. // store port info
  1037. uint32_t indexOffset;
  1038. uint32_t numAudioIns;
  1039. uint32_t numAudioOuts;
  1040. uint32_t numCVIns;
  1041. uint32_t numCVOuts;
  1042. uint32_t numMidiIns;
  1043. uint32_t numMidiOuts;
  1044. uint32_t numParams;
  1045. bool hasUI;
  1046. bool usesTime;
  1047. // port buffers
  1048. const LV2_Atom_Sequence** eventsIn;
  1049. /* */ LV2_Atom_Sequence** eventsOut;
  1050. /* */ EventsOutData* eventsOutData;
  1051. const float** audioCVIns;
  1052. /* */ float** audioCVOuts;
  1053. /* */ float* freewheel;
  1054. // cached parameter values
  1055. float* paramsLast;
  1056. float** paramsPtr;
  1057. bool* paramsOut;
  1058. Ports()
  1059. : indexOffset(0),
  1060. numAudioIns(0),
  1061. numAudioOuts(0),
  1062. numCVIns(0),
  1063. numCVOuts(0),
  1064. numMidiIns(0),
  1065. numMidiOuts(0),
  1066. numParams(0),
  1067. hasUI(false),
  1068. usesTime(false),
  1069. eventsIn(nullptr),
  1070. eventsOut(nullptr),
  1071. eventsOutData(nullptr),
  1072. audioCVIns(nullptr),
  1073. audioCVOuts(nullptr),
  1074. freewheel(nullptr),
  1075. paramsLast(nullptr),
  1076. paramsPtr(nullptr),
  1077. paramsOut(nullptr) {}
  1078. ~Ports()
  1079. {
  1080. if (eventsIn != nullptr)
  1081. {
  1082. delete[] eventsIn;
  1083. eventsIn = nullptr;
  1084. }
  1085. if (eventsOut != nullptr)
  1086. {
  1087. delete[] eventsOut;
  1088. eventsOut = nullptr;
  1089. }
  1090. if (eventsOutData != nullptr)
  1091. {
  1092. delete[] eventsOutData;
  1093. eventsOutData = nullptr;
  1094. }
  1095. if (audioCVIns != nullptr)
  1096. {
  1097. delete[] audioCVIns;
  1098. audioCVIns = nullptr;
  1099. }
  1100. if (audioCVOuts != nullptr)
  1101. {
  1102. delete[] audioCVOuts;
  1103. audioCVOuts = nullptr;
  1104. }
  1105. if (paramsLast != nullptr)
  1106. {
  1107. delete[] paramsLast;
  1108. paramsLast = nullptr;
  1109. }
  1110. if (paramsPtr != nullptr)
  1111. {
  1112. delete[] paramsPtr;
  1113. paramsPtr = nullptr;
  1114. }
  1115. if (paramsOut != nullptr)
  1116. {
  1117. delete[] paramsOut;
  1118. paramsOut = nullptr;
  1119. }
  1120. }
  1121. // NOTE: assumes num* has been filled by parent class
  1122. void init()
  1123. {
  1124. if (numMidiIns > 0)
  1125. {
  1126. eventsIn = new const LV2_Atom_Sequence*[numMidiIns];
  1127. for (uint32_t i=0; i < numMidiIns; ++i)
  1128. eventsIn[i] = nullptr;
  1129. }
  1130. else if (usesTime || hasUI)
  1131. {
  1132. eventsIn = new const LV2_Atom_Sequence*[1];
  1133. eventsIn[0] = nullptr;
  1134. }
  1135. if (numMidiOuts > 0)
  1136. {
  1137. eventsOut = new LV2_Atom_Sequence*[numMidiOuts];
  1138. eventsOutData = new EventsOutData[numMidiOuts];
  1139. for (uint32_t i=0; i < numMidiOuts; ++i)
  1140. eventsOut[i] = nullptr;
  1141. }
  1142. else if (hasUI)
  1143. {
  1144. eventsOut = new LV2_Atom_Sequence*[1];
  1145. eventsOut[0] = nullptr;
  1146. }
  1147. if (const uint32_t numAudioCVIns = numAudioIns+numCVIns)
  1148. {
  1149. audioCVIns = new const float*[numAudioCVIns];
  1150. carla_zeroPointers(audioCVIns, numAudioCVIns);
  1151. }
  1152. if (const uint32_t numAudioCVOuts = numAudioOuts+numCVOuts)
  1153. {
  1154. audioCVOuts = new float*[numAudioCVOuts];
  1155. carla_zeroPointers(audioCVOuts, numAudioCVOuts);
  1156. }
  1157. if (numParams > 0)
  1158. {
  1159. paramsLast = new float[numParams];
  1160. paramsPtr = new float*[numParams];
  1161. paramsOut = new bool[numParams];
  1162. carla_zeroFloats(paramsLast, numParams);
  1163. carla_zeroPointers(paramsPtr, numParams);
  1164. carla_zeroStructs(paramsOut, numParams);
  1165. // NOTE: need to be filled in by the parent class
  1166. }
  1167. indexOffset = numAudioIns + numAudioOuts + numCVIns + numCVOuts;
  1168. // 1 event port for time or ui if no midi input is used
  1169. indexOffset += numMidiIns > 0 ? numMidiIns : ((usesTime || hasUI) ? 1 : 0);
  1170. // 1 event port for ui if no midi output is used
  1171. indexOffset += numMidiOuts > 0 ? numMidiOuts : (hasUI ? 1 : 0);
  1172. // 1 extra for freewheel port
  1173. indexOffset += 1;
  1174. }
  1175. void connectPort(const uint32_t port, void* const dataLocation)
  1176. {
  1177. uint32_t index = 0;
  1178. if (numMidiIns > 0 || usesTime || hasUI)
  1179. {
  1180. if (port == index++)
  1181. {
  1182. eventsIn[0] = (LV2_Atom_Sequence*)dataLocation;
  1183. return;
  1184. }
  1185. }
  1186. for (uint32_t i=1; i < numMidiIns; ++i)
  1187. {
  1188. if (port == index++)
  1189. {
  1190. eventsIn[i] = (LV2_Atom_Sequence*)dataLocation;
  1191. return;
  1192. }
  1193. }
  1194. if (numMidiOuts > 0 || hasUI)
  1195. {
  1196. if (port == index++)
  1197. {
  1198. eventsOut[0] = (LV2_Atom_Sequence*)dataLocation;
  1199. return;
  1200. }
  1201. }
  1202. for (uint32_t i=1; i < numMidiOuts; ++i)
  1203. {
  1204. if (port == index++)
  1205. {
  1206. eventsOut[i] = (LV2_Atom_Sequence*)dataLocation;
  1207. return;
  1208. }
  1209. }
  1210. if (port == index++)
  1211. {
  1212. freewheel = (float*)dataLocation;
  1213. return;
  1214. }
  1215. for (uint32_t i=0; i < numAudioIns; ++i)
  1216. {
  1217. if (port == index++)
  1218. {
  1219. audioCVIns[i] = (const float*)dataLocation;
  1220. return;
  1221. }
  1222. }
  1223. for (uint32_t i=0; i < numAudioOuts; ++i)
  1224. {
  1225. if (port == index++)
  1226. {
  1227. audioCVOuts[i] = (float*)dataLocation;
  1228. return;
  1229. }
  1230. }
  1231. for (uint32_t i=0; i < numCVIns; ++i)
  1232. {
  1233. if (port == index++)
  1234. {
  1235. audioCVIns[numAudioIns+i] = (const float*)dataLocation;
  1236. return;
  1237. }
  1238. }
  1239. for (uint32_t i=0; i < numCVOuts; ++i)
  1240. {
  1241. if (port == index++)
  1242. {
  1243. audioCVOuts[numAudioOuts+i] = (float*)dataLocation;
  1244. return;
  1245. }
  1246. }
  1247. for (uint32_t i=0; i < numParams; ++i)
  1248. {
  1249. if (port == index++)
  1250. {
  1251. paramsPtr[i] = (float*)dataLocation;
  1252. return;
  1253. }
  1254. }
  1255. }
  1256. CARLA_DECLARE_NON_COPY_STRUCT(Ports);
  1257. } fPorts;
  1258. // Rest of host<->plugin support
  1259. struct URIDs {
  1260. LV2_URID atomBlank;
  1261. LV2_URID atomObject;
  1262. LV2_URID atomDouble;
  1263. LV2_URID atomFloat;
  1264. LV2_URID atomInt;
  1265. LV2_URID atomLong;
  1266. LV2_URID atomPath;
  1267. LV2_URID atomSequence;
  1268. LV2_URID atomString;
  1269. LV2_URID atomURID;
  1270. LV2_URID carlaFile;
  1271. LV2_URID midiEvent;
  1272. LV2_URID patchProperty;
  1273. LV2_URID patchSet;
  1274. LV2_URID patchValue;
  1275. LV2_URID timePos;
  1276. LV2_URID timeBar;
  1277. LV2_URID timeBarBeat;
  1278. LV2_URID timeBeatsPerBar;
  1279. LV2_URID timeBeatsPerMinute;
  1280. LV2_URID timeBeatUnit;
  1281. LV2_URID timeFrame;
  1282. LV2_URID timeSpeed;
  1283. LV2_URID timeTicksPerBeat;
  1284. LV2_URID uiEvents;
  1285. URIDs()
  1286. : atomBlank(0),
  1287. atomObject(0),
  1288. atomDouble(0),
  1289. atomFloat(0),
  1290. atomInt(0),
  1291. atomLong(0),
  1292. atomPath(0),
  1293. atomSequence(0),
  1294. atomString(0),
  1295. atomURID(0),
  1296. carlaFile(0),
  1297. midiEvent(0),
  1298. patchProperty(0),
  1299. patchSet(0),
  1300. patchValue(0),
  1301. timePos(0),
  1302. timeBar(0),
  1303. timeBarBeat(0),
  1304. timeBeatsPerBar(0),
  1305. timeBeatsPerMinute(0),
  1306. timeBeatUnit(0),
  1307. timeFrame(0),
  1308. timeSpeed(0),
  1309. timeTicksPerBeat(0),
  1310. uiEvents(0) {}
  1311. void map(const LV2_URID_Map* const uridMap)
  1312. {
  1313. atomBlank = uridMap->map(uridMap->handle, LV2_ATOM__Blank);
  1314. atomObject = uridMap->map(uridMap->handle, LV2_ATOM__Object);
  1315. atomDouble = uridMap->map(uridMap->handle, LV2_ATOM__Double);
  1316. atomFloat = uridMap->map(uridMap->handle, LV2_ATOM__Float);
  1317. atomInt = uridMap->map(uridMap->handle, LV2_ATOM__Int);
  1318. atomLong = uridMap->map(uridMap->handle, LV2_ATOM__Long);
  1319. atomPath = uridMap->map(uridMap->handle, LV2_ATOM__Path);
  1320. atomSequence = uridMap->map(uridMap->handle, LV2_ATOM__Sequence);
  1321. atomString = uridMap->map(uridMap->handle, LV2_ATOM__String);
  1322. atomURID = uridMap->map(uridMap->handle, LV2_ATOM__URID);
  1323. carlaFile = uridMap->map(uridMap->handle, "http://kxstudio.sf.net/carla/file");
  1324. midiEvent = uridMap->map(uridMap->handle, LV2_MIDI__MidiEvent);
  1325. patchProperty = uridMap->map(uridMap->handle, LV2_PATCH__property);
  1326. patchSet = uridMap->map(uridMap->handle, LV2_PATCH__Set);
  1327. patchValue = uridMap->map(uridMap->handle, LV2_PATCH__value);
  1328. timePos = uridMap->map(uridMap->handle, LV2_TIME__Position);
  1329. timeBar = uridMap->map(uridMap->handle, LV2_TIME__bar);
  1330. timeBarBeat = uridMap->map(uridMap->handle, LV2_TIME__barBeat);
  1331. timeBeatUnit = uridMap->map(uridMap->handle, LV2_TIME__beatUnit);
  1332. timeFrame = uridMap->map(uridMap->handle, LV2_TIME__frame);
  1333. timeSpeed = uridMap->map(uridMap->handle, LV2_TIME__speed);
  1334. timeBeatsPerBar = uridMap->map(uridMap->handle, LV2_TIME__beatsPerBar);
  1335. timeBeatsPerMinute = uridMap->map(uridMap->handle, LV2_TIME__beatsPerMinute);
  1336. timeTicksPerBeat = uridMap->map(uridMap->handle, LV2_KXSTUDIO_PROPERTIES__TimePositionTicksPerBeat);
  1337. uiEvents = uridMap->map(uridMap->handle, "urn:carla:transmitEv");
  1338. }
  1339. } fURIs;
  1340. struct UI {
  1341. const LV2_External_UI_Host* host;
  1342. const LV2UI_Touch* touch;
  1343. LV2UI_Write_Function writeFunction;
  1344. LV2UI_Controller controller;
  1345. bool isVisible;
  1346. UI()
  1347. : host(nullptr),
  1348. touch(nullptr),
  1349. writeFunction(nullptr),
  1350. controller(nullptr),
  1351. isVisible(false) {}
  1352. } fUI;
  1353. private:
  1354. // ----------------------------------------------------------------------------------------------------------------
  1355. #define handlePtr ((Lv2PluginBaseClass*)handle)
  1356. static void extui_run(LV2_External_UI_Widget_Compat* handle)
  1357. {
  1358. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  1359. handlePtr->handleUiRun();
  1360. }
  1361. static void extui_show(LV2_External_UI_Widget_Compat* handle)
  1362. {
  1363. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  1364. carla_debug("extui_show(%p)", handle);
  1365. handlePtr->handleUiShow();
  1366. }
  1367. static void extui_hide(LV2_External_UI_Widget_Compat* handle)
  1368. {
  1369. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  1370. carla_debug("extui_hide(%p)", handle);
  1371. handlePtr->handleUiHide();
  1372. }
  1373. #undef handlePtr
  1374. // ----------------------------------------------------------------------------------------------------------------
  1375. void clearTimeData() noexcept;
  1376. // ----------------------------------------------------------------------------------------------------------------
  1377. CARLA_DECLARE_NON_COPY_STRUCT(Lv2PluginBaseClass)
  1378. };
  1379. // --------------------------------------------------------------------------------------------------------------------
  1380. // Create new RDF object (using lilv)
  1381. static inline
  1382. const LV2_RDF_Descriptor* lv2_rdf_new(const LV2_URI uri, const bool loadPresets)
  1383. {
  1384. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0', nullptr);
  1385. Lv2WorldClass& lv2World(Lv2WorldClass::getInstance());
  1386. const LilvPlugin* const cPlugin(lv2World.getPluginFromURI(uri));
  1387. CARLA_SAFE_ASSERT_RETURN(cPlugin != nullptr, nullptr);
  1388. Lilv::Plugin lilvPlugin(cPlugin);
  1389. LV2_RDF_Descriptor* const rdfDescriptor(new LV2_RDF_Descriptor());
  1390. // ----------------------------------------------------------------------------------------------------------------
  1391. // Set Plugin Type
  1392. {
  1393. Lilv::Nodes typeNodes(lilvPlugin.get_value(lv2World.rdf_type));
  1394. if (typeNodes.size() > 0)
  1395. {
  1396. if (typeNodes.contains(lv2World.class_allpass))
  1397. rdfDescriptor->Type[0] |= LV2_PLUGIN_ALLPASS;
  1398. if (typeNodes.contains(lv2World.class_amplifier))
  1399. rdfDescriptor->Type[0] |= LV2_PLUGIN_AMPLIFIER;
  1400. if (typeNodes.contains(lv2World.class_analyzer))
  1401. rdfDescriptor->Type[1] |= LV2_PLUGIN_ANALYSER;
  1402. if (typeNodes.contains(lv2World.class_bandpass))
  1403. rdfDescriptor->Type[0] |= LV2_PLUGIN_BANDPASS;
  1404. if (typeNodes.contains(lv2World.class_chorus))
  1405. rdfDescriptor->Type[1] |= LV2_PLUGIN_CHORUS;
  1406. if (typeNodes.contains(lv2World.class_comb))
  1407. rdfDescriptor->Type[1] |= LV2_PLUGIN_COMB;
  1408. if (typeNodes.contains(lv2World.class_compressor))
  1409. rdfDescriptor->Type[0] |= LV2_PLUGIN_COMPRESSOR;
  1410. if (typeNodes.contains(lv2World.class_constant))
  1411. rdfDescriptor->Type[1] |= LV2_PLUGIN_CONSTANT;
  1412. if (typeNodes.contains(lv2World.class_converter))
  1413. rdfDescriptor->Type[1] |= LV2_PLUGIN_CONVERTER;
  1414. if (typeNodes.contains(lv2World.class_delay))
  1415. rdfDescriptor->Type[0] |= LV2_PLUGIN_DELAY;
  1416. if (typeNodes.contains(lv2World.class_distortion))
  1417. rdfDescriptor->Type[0] |= LV2_PLUGIN_DISTORTION;
  1418. if (typeNodes.contains(lv2World.class_dynamics))
  1419. rdfDescriptor->Type[0] |= LV2_PLUGIN_DYNAMICS;
  1420. if (typeNodes.contains(lv2World.class_eq))
  1421. rdfDescriptor->Type[0] |= LV2_PLUGIN_EQ;
  1422. if (typeNodes.contains(lv2World.class_envelope))
  1423. rdfDescriptor->Type[0] |= LV2_PLUGIN_ENVELOPE;
  1424. if (typeNodes.contains(lv2World.class_expander))
  1425. rdfDescriptor->Type[0] |= LV2_PLUGIN_EXPANDER;
  1426. if (typeNodes.contains(lv2World.class_filter))
  1427. rdfDescriptor->Type[0] |= LV2_PLUGIN_FILTER;
  1428. if (typeNodes.contains(lv2World.class_flanger))
  1429. rdfDescriptor->Type[1] |= LV2_PLUGIN_FLANGER;
  1430. if (typeNodes.contains(lv2World.class_function))
  1431. rdfDescriptor->Type[1] |= LV2_PLUGIN_FUNCTION;
  1432. if (typeNodes.contains(lv2World.class_gate))
  1433. rdfDescriptor->Type[0] |= LV2_PLUGIN_GATE;
  1434. if (typeNodes.contains(lv2World.class_generator))
  1435. rdfDescriptor->Type[1] |= LV2_PLUGIN_GENERATOR;
  1436. if (typeNodes.contains(lv2World.class_highpass))
  1437. rdfDescriptor->Type[0] |= LV2_PLUGIN_HIGHPASS;
  1438. if (typeNodes.contains(lv2World.class_instrument))
  1439. rdfDescriptor->Type[1] |= LV2_PLUGIN_INSTRUMENT;
  1440. if (typeNodes.contains(lv2World.class_limiter))
  1441. rdfDescriptor->Type[0] |= LV2_PLUGIN_LIMITER;
  1442. if (typeNodes.contains(lv2World.class_lowpass))
  1443. rdfDescriptor->Type[0] |= LV2_PLUGIN_LOWPASS;
  1444. if (typeNodes.contains(lv2World.class_mixer))
  1445. rdfDescriptor->Type[1] |= LV2_PLUGIN_MIXER;
  1446. if (typeNodes.contains(lv2World.class_modulator))
  1447. rdfDescriptor->Type[1] |= LV2_PLUGIN_MODULATOR;
  1448. if (typeNodes.contains(lv2World.class_multiEQ))
  1449. rdfDescriptor->Type[0] |= LV2_PLUGIN_MULTI_EQ;
  1450. if (typeNodes.contains(lv2World.class_oscillator))
  1451. rdfDescriptor->Type[1] |= LV2_PLUGIN_OSCILLATOR;
  1452. if (typeNodes.contains(lv2World.class_paraEQ))
  1453. rdfDescriptor->Type[0] |= LV2_PLUGIN_PARA_EQ;
  1454. if (typeNodes.contains(lv2World.class_phaser))
  1455. rdfDescriptor->Type[1] |= LV2_PLUGIN_PHASER;
  1456. if (typeNodes.contains(lv2World.class_pitch))
  1457. rdfDescriptor->Type[1] |= LV2_PLUGIN_PITCH;
  1458. if (typeNodes.contains(lv2World.class_reverb))
  1459. rdfDescriptor->Type[0] |= LV2_PLUGIN_REVERB;
  1460. if (typeNodes.contains(lv2World.class_simulator))
  1461. rdfDescriptor->Type[0] |= LV2_PLUGIN_SIMULATOR;
  1462. if (typeNodes.contains(lv2World.class_spatial))
  1463. rdfDescriptor->Type[1] |= LV2_PLUGIN_SPATIAL;
  1464. if (typeNodes.contains(lv2World.class_spectral))
  1465. rdfDescriptor->Type[1] |= LV2_PLUGIN_SPECTRAL;
  1466. if (typeNodes.contains(lv2World.class_utility))
  1467. rdfDescriptor->Type[1] |= LV2_PLUGIN_UTILITY;
  1468. if (typeNodes.contains(lv2World.class_waveshaper))
  1469. rdfDescriptor->Type[0] |= LV2_PLUGIN_WAVESHAPER;
  1470. }
  1471. lilv_nodes_free(const_cast<LilvNodes*>(typeNodes.me));
  1472. }
  1473. // ----------------------------------------------------------------------------------------------------------------
  1474. // Set Plugin Information
  1475. {
  1476. rdfDescriptor->URI = carla_strdup(uri);
  1477. if (LilvNode* const nameNode = lilv_plugin_get_name(lilvPlugin.me))
  1478. {
  1479. if (const char* const name = lilv_node_as_string(nameNode))
  1480. rdfDescriptor->Name = carla_strdup(name);
  1481. lilv_node_free(nameNode);
  1482. }
  1483. if (const char* const author = lilvPlugin.get_author_name().as_string())
  1484. rdfDescriptor->Author = carla_strdup(author);
  1485. if (const char* const binary = lilvPlugin.get_library_uri().as_string())
  1486. rdfDescriptor->Binary = carla_strdup_free(lilv_file_uri_parse(binary, nullptr));
  1487. if (const char* const bundle = lilvPlugin.get_bundle_uri().as_string())
  1488. rdfDescriptor->Bundle = carla_strdup_free(lilv_file_uri_parse(bundle, nullptr));
  1489. Lilv::Nodes licenseNodes(lilvPlugin.get_value(lv2World.doap_license));
  1490. if (licenseNodes.size() > 0)
  1491. {
  1492. if (const char* const license = licenseNodes.get_first().as_string())
  1493. rdfDescriptor->License = carla_strdup(license);
  1494. }
  1495. lilv_nodes_free(const_cast<LilvNodes*>(licenseNodes.me));
  1496. }
  1497. // ----------------------------------------------------------------------------------------------------------------
  1498. // Set Plugin UniqueID
  1499. {
  1500. Lilv::Nodes replaceNodes(lilvPlugin.get_value(lv2World.dct_replaces));
  1501. if (replaceNodes.size() > 0)
  1502. {
  1503. Lilv::Node replaceNode(replaceNodes.get_first());
  1504. if (replaceNode.is_uri())
  1505. {
  1506. #ifdef USE_QT
  1507. const QString replaceURI(replaceNode.as_uri());
  1508. if (replaceURI.startsWith("urn:"))
  1509. {
  1510. const QString replaceId(replaceURI.split(":").last());
  1511. bool ok;
  1512. const ulong uniqueId(replaceId.toULong(&ok));
  1513. if (ok && uniqueId != 0)
  1514. rdfDescriptor->UniqueID = uniqueId;
  1515. }
  1516. #else
  1517. const water::String replaceURI(replaceNode.as_uri());
  1518. if (replaceURI.startsWith("urn:"))
  1519. {
  1520. const int uniqueId(replaceURI.getTrailingIntValue());
  1521. if (uniqueId > 0)
  1522. rdfDescriptor->UniqueID = static_cast<ulong>(uniqueId);
  1523. }
  1524. #endif
  1525. }
  1526. }
  1527. lilv_nodes_free(const_cast<LilvNodes*>(replaceNodes.me));
  1528. }
  1529. // ----------------------------------------------------------------------------------------------------------------
  1530. // Set Plugin Ports
  1531. if (const uint numPorts = lilvPlugin.get_num_ports())
  1532. {
  1533. rdfDescriptor->PortCount = numPorts;
  1534. rdfDescriptor->Ports = new LV2_RDF_Port[numPorts];
  1535. for (uint i = 0; i < numPorts; ++i)
  1536. {
  1537. Lilv::Port lilvPort(lilvPlugin.get_port_by_index(i));
  1538. LV2_RDF_Port* const rdfPort(&rdfDescriptor->Ports[i]);
  1539. // --------------------------------------------------------------------------------------------------------
  1540. // Set Port Information
  1541. {
  1542. if (LilvNode* const nameNode = lilv_port_get_name(lilvPlugin.me, lilvPort.me))
  1543. {
  1544. if (const char* const name = lilv_node_as_string(nameNode))
  1545. rdfPort->Name = carla_strdup(name);
  1546. lilv_node_free(nameNode);
  1547. }
  1548. if (const char* const symbol = lilv_node_as_string(lilvPort.get_symbol()))
  1549. rdfPort->Symbol = carla_strdup(symbol);
  1550. }
  1551. // --------------------------------------------------------------------------------------------------------
  1552. // Set Port Mode and Type
  1553. {
  1554. // Input or Output
  1555. /**/ if (lilvPort.is_a(lv2World.port_input))
  1556. rdfPort->Types |= LV2_PORT_INPUT;
  1557. else if (lilvPort.is_a(lv2World.port_output))
  1558. rdfPort->Types |= LV2_PORT_OUTPUT;
  1559. else
  1560. carla_stderr("lv2_rdf_new(\"%s\") - port '%s' is not input or output", uri, rdfPort->Name);
  1561. // Data Type
  1562. /**/ if (lilvPort.is_a(lv2World.port_control))
  1563. {
  1564. rdfPort->Types |= LV2_PORT_CONTROL;
  1565. }
  1566. else if (lilvPort.is_a(lv2World.port_audio))
  1567. {
  1568. rdfPort->Types |= LV2_PORT_AUDIO;
  1569. }
  1570. else if (lilvPort.is_a(lv2World.port_cv))
  1571. {
  1572. rdfPort->Types |= LV2_PORT_CV;
  1573. }
  1574. else if (lilvPort.is_a(lv2World.port_atom))
  1575. {
  1576. rdfPort->Types |= LV2_PORT_ATOM;
  1577. Lilv::Nodes bufferTypeNodes(lilvPort.get_value(lv2World.atom_bufferType));
  1578. for (LilvIter* it = lilv_nodes_begin(bufferTypeNodes.me); ! lilv_nodes_is_end(bufferTypeNodes.me, it); it = lilv_nodes_next(bufferTypeNodes.me, it))
  1579. {
  1580. const Lilv::Node node(lilv_nodes_get(bufferTypeNodes.me, it));
  1581. CARLA_SAFE_ASSERT_CONTINUE(node.is_uri());
  1582. if (node.equals(lv2World.atom_sequence))
  1583. rdfPort->Types |= LV2_PORT_ATOM_SEQUENCE;
  1584. else
  1585. carla_stderr("lv2_rdf_new(\"%s\") - port '%s' uses an unknown atom buffer type '%s'", uri, rdfPort->Name, node.as_uri());
  1586. }
  1587. Lilv::Nodes supportNodes(lilvPort.get_value(lv2World.atom_supports));
  1588. for (LilvIter* it = lilv_nodes_begin(supportNodes.me); ! lilv_nodes_is_end(supportNodes.me, it); it = lilv_nodes_next(supportNodes.me, it))
  1589. {
  1590. const Lilv::Node node(lilv_nodes_get(supportNodes.me, it));
  1591. CARLA_SAFE_ASSERT_CONTINUE(node.is_uri());
  1592. /**/ if (node.equals(lv2World.midi_event))
  1593. rdfPort->Types |= LV2_PORT_DATA_MIDI_EVENT;
  1594. else if (node.equals(lv2World.patch_message))
  1595. rdfPort->Types |= LV2_PORT_DATA_PATCH_MESSAGE;
  1596. else if (node.equals(lv2World.time_position))
  1597. rdfPort->Types |= LV2_PORT_DATA_TIME_POSITION;
  1598. #if 0
  1599. // something new we don't support yet?
  1600. else if (std::strstr(node.as_uri(), "lv2plug.in/") != nullptr)
  1601. carla_stderr("lv2_rdf_new(\"%s\") - port '%s' is of atom type but has unsupported data '%s'", uri, rdfPort->Name, node.as_uri());
  1602. #endif
  1603. }
  1604. lilv_nodes_free(const_cast<LilvNodes*>(bufferTypeNodes.me));
  1605. lilv_nodes_free(const_cast<LilvNodes*>(supportNodes.me));
  1606. }
  1607. else if (lilvPort.is_a(lv2World.port_event))
  1608. {
  1609. rdfPort->Types |= LV2_PORT_EVENT;
  1610. bool supported = false;
  1611. if (lilvPort.supports_event(lv2World.midi_event))
  1612. {
  1613. rdfPort->Types |= LV2_PORT_DATA_MIDI_EVENT;
  1614. supported = true;
  1615. }
  1616. if (lilvPort.supports_event(lv2World.patch_message))
  1617. {
  1618. rdfPort->Types |= LV2_PORT_DATA_PATCH_MESSAGE;
  1619. supported = true;
  1620. }
  1621. if (lilvPort.supports_event(lv2World.time_position))
  1622. {
  1623. rdfPort->Types |= LV2_PORT_DATA_TIME_POSITION;
  1624. supported = true;
  1625. }
  1626. if (! supported)
  1627. carla_stderr("lv2_rdf_new(\"%s\") - port '%s' is of event type but has unsupported data", uri, rdfPort->Name);
  1628. }
  1629. else if (lilvPort.is_a(lv2World.port_midi))
  1630. {
  1631. rdfPort->Types |= LV2_PORT_MIDI_LL;
  1632. rdfPort->Types |= LV2_PORT_DATA_MIDI_EVENT;
  1633. }
  1634. else
  1635. carla_stderr("lv2_rdf_new(\"%s\") - port '%s' is of unknown data type", uri, rdfPort->Name);
  1636. }
  1637. // --------------------------------------------------------------------------------------------------------
  1638. // Set Port Properties
  1639. {
  1640. if (lilvPort.has_property(lv2World.pprop_optional))
  1641. rdfPort->Properties |= LV2_PORT_OPTIONAL;
  1642. if (lilvPort.has_property(lv2World.pprop_enumeration))
  1643. rdfPort->Properties |= LV2_PORT_ENUMERATION;
  1644. if (lilvPort.has_property(lv2World.pprop_integer))
  1645. rdfPort->Properties |= LV2_PORT_INTEGER;
  1646. if (lilvPort.has_property(lv2World.pprop_sampleRate))
  1647. rdfPort->Properties |= LV2_PORT_SAMPLE_RATE;
  1648. if (lilvPort.has_property(lv2World.pprop_toggled))
  1649. rdfPort->Properties |= LV2_PORT_TOGGLED;
  1650. if (lilvPort.has_property(lv2World.pprop_artifacts))
  1651. rdfPort->Properties |= LV2_PORT_CAUSES_ARTIFACTS;
  1652. if (lilvPort.has_property(lv2World.pprop_continuousCV))
  1653. rdfPort->Properties |= LV2_PORT_CONTINUOUS_CV;
  1654. if (lilvPort.has_property(lv2World.pprop_discreteCV))
  1655. rdfPort->Properties |= LV2_PORT_DISCRETE_CV;
  1656. if (lilvPort.has_property(lv2World.pprop_expensive))
  1657. rdfPort->Properties |= LV2_PORT_EXPENSIVE;
  1658. if (lilvPort.has_property(lv2World.pprop_strictBounds))
  1659. rdfPort->Properties |= LV2_PORT_STRICT_BOUNDS;
  1660. if (lilvPort.has_property(lv2World.pprop_logarithmic))
  1661. rdfPort->Properties |= LV2_PORT_LOGARITHMIC;
  1662. if (lilvPort.has_property(lv2World.pprop_notAutomatic))
  1663. rdfPort->Properties |= LV2_PORT_NOT_AUTOMATIC;
  1664. if (lilvPort.has_property(lv2World.pprop_notOnGUI))
  1665. rdfPort->Properties |= LV2_PORT_NOT_ON_GUI;
  1666. if (lilvPort.has_property(lv2World.pprop_trigger))
  1667. rdfPort->Properties |= LV2_PORT_TRIGGER;
  1668. if (lilvPort.has_property(lv2World.pprop_nonAutomable))
  1669. rdfPort->Properties |= LV2_PORT_NON_AUTOMABLE;
  1670. if (lilvPort.has_property(lv2World.reportsLatency))
  1671. rdfPort->Designation = LV2_PORT_DESIGNATION_LATENCY;
  1672. // no port properties set, check if using old/invalid ones
  1673. if (rdfPort->Properties == 0x0)
  1674. {
  1675. const Lilv::Node oldPropArtifacts(lv2World.new_uri(NS_devp "causesArtifacts"));
  1676. const Lilv::Node oldPropContinuousCV(lv2World.new_uri(NS_devp "continuousCV"));
  1677. const Lilv::Node oldPropDiscreteCV(lv2World.new_uri(NS_devp "discreteCV"));
  1678. const Lilv::Node oldPropExpensive(lv2World.new_uri(NS_devp "expensive"));
  1679. const Lilv::Node oldPropStrictBounds(lv2World.new_uri(NS_devp "hasStrictBounds"));
  1680. const Lilv::Node oldPropLogarithmic(lv2World.new_uri(NS_devp "logarithmic"));
  1681. const Lilv::Node oldPropNotAutomatic(lv2World.new_uri(NS_devp "notAutomatic"));
  1682. const Lilv::Node oldPropNotOnGUI(lv2World.new_uri(NS_devp "notOnGUI"));
  1683. const Lilv::Node oldPropTrigger(lv2World.new_uri(NS_devp "trigger"));
  1684. if (lilvPort.has_property(oldPropArtifacts))
  1685. {
  1686. rdfPort->Properties |= LV2_PORT_CAUSES_ARTIFACTS;
  1687. carla_stderr("lv2_rdf_new(\"%s\") - port '%s' uses old/invalid LV2 property for 'causesArtifacts'", uri, rdfPort->Name);
  1688. }
  1689. if (lilvPort.has_property(oldPropContinuousCV))
  1690. {
  1691. rdfPort->Properties |= LV2_PORT_CONTINUOUS_CV;
  1692. carla_stderr("lv2_rdf_new(\"%s\") - port '%s' uses old/invalid LV2 property for 'continuousCV'", uri, rdfPort->Name);
  1693. }
  1694. if (lilvPort.has_property(oldPropDiscreteCV))
  1695. {
  1696. rdfPort->Properties |= LV2_PORT_DISCRETE_CV;
  1697. carla_stderr("lv2_rdf_new(\"%s\") - port '%s' uses old/invalid LV2 property for 'discreteCV'", uri, rdfPort->Name);
  1698. }
  1699. if (lilvPort.has_property(oldPropExpensive))
  1700. {
  1701. rdfPort->Properties |= LV2_PORT_EXPENSIVE;
  1702. carla_stderr("lv2_rdf_new(\"%s\") - port '%s' uses old/invalid LV2 property for 'expensive'", uri, rdfPort->Name);
  1703. }
  1704. if (lilvPort.has_property(oldPropStrictBounds))
  1705. {
  1706. rdfPort->Properties |= LV2_PORT_STRICT_BOUNDS;
  1707. carla_stderr("lv2_rdf_new(\"%s\") - port '%s' uses old/invalid LV2 property for 'hasStrictBounds'", uri, rdfPort->Name);
  1708. }
  1709. if (lilvPort.has_property(oldPropLogarithmic))
  1710. {
  1711. rdfPort->Properties |= LV2_PORT_LOGARITHMIC;
  1712. carla_stderr("lv2_rdf_new(\"%s\") - port '%s' uses old/invalid LV2 property for 'logarithmic'", uri, rdfPort->Name);
  1713. }
  1714. if (lilvPort.has_property(oldPropNotAutomatic))
  1715. {
  1716. rdfPort->Properties |= LV2_PORT_NOT_AUTOMATIC;
  1717. carla_stderr("lv2_rdf_new(\"%s\") - port '%s' uses old/invalid LV2 property for 'notAutomatic'", uri, rdfPort->Name);
  1718. }
  1719. if (lilvPort.has_property(oldPropNotOnGUI))
  1720. {
  1721. rdfPort->Properties |= LV2_PORT_NOT_ON_GUI;
  1722. carla_stderr("lv2_rdf_new(\"%s\") - port '%s' uses old/invalid LV2 property for 'notOnGUI'", uri, rdfPort->Name);
  1723. }
  1724. if (lilvPort.has_property(oldPropTrigger))
  1725. {
  1726. rdfPort->Properties |= LV2_PORT_TRIGGER;
  1727. carla_stderr("lv2_rdf_new(\"%s\") - port '%s' uses old/invalid LV2 property for 'trigger'", uri, rdfPort->Name);
  1728. }
  1729. }
  1730. }
  1731. // --------------------------------------------------------------------------------------------------------
  1732. // Set Port Designation
  1733. {
  1734. if (LilvNode* const designationNode = lilv_port_get(lilvPort.parent, lilvPort.me, lv2World.designation.me))
  1735. {
  1736. if (const char* const designation = lilv_node_as_string(designationNode))
  1737. {
  1738. /**/ if (std::strcmp(designation, LV2_CORE__control) == 0)
  1739. rdfPort->Designation = LV2_PORT_DESIGNATION_CONTROL;
  1740. else if (std::strcmp(designation, LV2_CORE__enabled) == 0)
  1741. rdfPort->Designation = LV2_PORT_DESIGNATION_ENABLED;
  1742. else if (std::strcmp(designation, LV2_CORE__freeWheeling) == 0)
  1743. rdfPort->Designation = LV2_PORT_DESIGNATION_FREEWHEELING;
  1744. else if (std::strcmp(designation, LV2_CORE__latency) == 0)
  1745. rdfPort->Designation = LV2_PORT_DESIGNATION_LATENCY;
  1746. else if (std::strcmp(designation, LV2_PARAMETERS__sampleRate) == 0)
  1747. rdfPort->Designation = LV2_PORT_DESIGNATION_SAMPLE_RATE;
  1748. else if (std::strcmp(designation, LV2_TIME__bar) == 0)
  1749. rdfPort->Designation = LV2_PORT_DESIGNATION_TIME_BAR;
  1750. else if (std::strcmp(designation, LV2_TIME__barBeat) == 0)
  1751. rdfPort->Designation = LV2_PORT_DESIGNATION_TIME_BAR_BEAT;
  1752. else if (std::strcmp(designation, LV2_TIME__beat) == 0)
  1753. rdfPort->Designation = LV2_PORT_DESIGNATION_TIME_BEAT;
  1754. else if (std::strcmp(designation, LV2_TIME__beatUnit) == 0)
  1755. rdfPort->Designation = LV2_PORT_DESIGNATION_TIME_BEAT_UNIT;
  1756. else if (std::strcmp(designation, LV2_TIME__beatsPerBar) == 0)
  1757. rdfPort->Designation = LV2_PORT_DESIGNATION_TIME_BEATS_PER_BAR;
  1758. else if (std::strcmp(designation, LV2_TIME__beatsPerMinute) == 0)
  1759. rdfPort->Designation = LV2_PORT_DESIGNATION_TIME_BEATS_PER_MINUTE;
  1760. else if (std::strcmp(designation, LV2_TIME__frame) == 0)
  1761. rdfPort->Designation = LV2_PORT_DESIGNATION_TIME_FRAME;
  1762. else if (std::strcmp(designation, LV2_TIME__framesPerSecond) == 0)
  1763. rdfPort->Designation = LV2_PORT_DESIGNATION_TIME_FRAMES_PER_SECOND;
  1764. else if (std::strcmp(designation, LV2_TIME__speed) == 0)
  1765. rdfPort->Designation = LV2_PORT_DESIGNATION_TIME_SPEED;
  1766. else if (std::strcmp(designation, LV2_KXSTUDIO_PROPERTIES__TimePositionTicksPerBeat) == 0)
  1767. rdfPort->Designation = LV2_PORT_DESIGNATION_TIME_TICKS_PER_BEAT;
  1768. else if (std::strncmp(designation, LV2_PARAMETERS_PREFIX, std::strlen(LV2_PARAMETERS_PREFIX)) == 0)
  1769. pass();
  1770. else if (std::strncmp(designation, LV2_PORT_GROUPS_PREFIX, std::strlen(LV2_PORT_GROUPS_PREFIX)) == 0)
  1771. pass();
  1772. else
  1773. carla_stderr("lv2_rdf_new(\"%s\") - got unknown port designation '%s'", uri, designation);
  1774. }
  1775. lilv_node_free(designationNode);
  1776. }
  1777. }
  1778. // --------------------------------------------------------------------------------------------------------
  1779. // Set Port MIDI Map
  1780. {
  1781. if (LilvNode* const midiMapNode = lilv_port_get(lilvPort.parent, lilvPort.me, lv2World.mm_defaultControl.me))
  1782. {
  1783. if (lilv_node_is_blank(midiMapNode))
  1784. {
  1785. Lilv::Nodes midiMapTypeNodes(lv2World.find_nodes(midiMapNode, lv2World.mm_controlType, nullptr));
  1786. Lilv::Nodes midiMapNumberNodes(lv2World.find_nodes(midiMapNode, lv2World.mm_controlNumber, nullptr));
  1787. if (midiMapTypeNodes.size() == 1 && midiMapNumberNodes.size() == 1)
  1788. {
  1789. if (const char* const midiMapType = midiMapTypeNodes.get_first().as_string())
  1790. {
  1791. /**/ if (std::strcmp(midiMapType, LV2_MIDI_Map__CC) == 0)
  1792. rdfPort->MidiMap.Type = LV2_PORT_MIDI_MAP_CC;
  1793. else if (std::strcmp(midiMapType, LV2_MIDI_Map__NRPN) == 0)
  1794. rdfPort->MidiMap.Type = LV2_PORT_MIDI_MAP_NRPN;
  1795. else
  1796. carla_stderr("lv2_rdf_new(\"%s\") - got unknown port Midi-Map type '%s'", uri, midiMapType);
  1797. rdfPort->MidiMap.Number = static_cast<uint32_t>(midiMapNumberNodes.get_first().as_int());
  1798. }
  1799. }
  1800. lilv_nodes_free(const_cast<LilvNodes*>(midiMapTypeNodes.me));
  1801. lilv_nodes_free(const_cast<LilvNodes*>(midiMapNumberNodes.me));
  1802. }
  1803. lilv_node_free(midiMapNode);
  1804. }
  1805. // TODO - also check using new official MIDI API too
  1806. }
  1807. // --------------------------------------------------------------------------------------------------------
  1808. // Set Port Points
  1809. {
  1810. if (LilvNode* const defNode = lilv_port_get(lilvPort.parent, lilvPort.me, lv2World.value_default.me))
  1811. {
  1812. rdfPort->Points.Hints |= LV2_PORT_POINT_DEFAULT;
  1813. rdfPort->Points.Default = lilv_node_as_float(defNode);
  1814. lilv_node_free(defNode);
  1815. }
  1816. if (LilvNode* const minNode = lilv_port_get(lilvPort.parent, lilvPort.me, lv2World.value_minimum.me))
  1817. {
  1818. rdfPort->Points.Hints |= LV2_PORT_POINT_MINIMUM;
  1819. rdfPort->Points.Minimum = lilv_node_as_float(minNode);
  1820. lilv_node_free(minNode);
  1821. }
  1822. if (LilvNode* const maxNode = lilv_port_get(lilvPort.parent, lilvPort.me, lv2World.value_maximum.me))
  1823. {
  1824. rdfPort->Points.Hints |= LV2_PORT_POINT_MAXIMUM;
  1825. rdfPort->Points.Maximum = lilv_node_as_float(maxNode);
  1826. lilv_node_free(maxNode);
  1827. }
  1828. }
  1829. // --------------------------------------------------------------------------------------------------------
  1830. // Set Port Unit
  1831. {
  1832. if (LilvNode* const unitUnitNode = lilv_port_get(lilvPort.parent, lilvPort.me, lv2World.unit_unit.me))
  1833. {
  1834. if (lilv_node_is_uri(unitUnitNode))
  1835. {
  1836. if (const char* const unitUnit = lilv_node_as_uri(unitUnitNode))
  1837. {
  1838. rdfPort->Unit.Hints |= LV2_PORT_UNIT_UNIT;
  1839. /**/ if (std::strcmp(unitUnit, LV2_UNITS__bar) == 0)
  1840. rdfPort->Unit.Unit = LV2_PORT_UNIT_BAR;
  1841. else if (std::strcmp(unitUnit, LV2_UNITS__beat) == 0)
  1842. rdfPort->Unit.Unit = LV2_PORT_UNIT_BEAT;
  1843. else if (std::strcmp(unitUnit, LV2_UNITS__bpm) == 0)
  1844. rdfPort->Unit.Unit = LV2_PORT_UNIT_BPM;
  1845. else if (std::strcmp(unitUnit, LV2_UNITS__cent) == 0)
  1846. rdfPort->Unit.Unit = LV2_PORT_UNIT_CENT;
  1847. else if (std::strcmp(unitUnit, LV2_UNITS__cm) == 0)
  1848. rdfPort->Unit.Unit = LV2_PORT_UNIT_CM;
  1849. else if (std::strcmp(unitUnit, LV2_UNITS__coef) == 0)
  1850. rdfPort->Unit.Unit = LV2_PORT_UNIT_COEF;
  1851. else if (std::strcmp(unitUnit, LV2_UNITS__db) == 0)
  1852. rdfPort->Unit.Unit = LV2_PORT_UNIT_DB;
  1853. else if (std::strcmp(unitUnit, LV2_UNITS__degree) == 0)
  1854. rdfPort->Unit.Unit = LV2_PORT_UNIT_DEGREE;
  1855. else if (std::strcmp(unitUnit, LV2_UNITS__frame) == 0)
  1856. rdfPort->Unit.Unit = LV2_PORT_UNIT_FRAME;
  1857. else if (std::strcmp(unitUnit, LV2_UNITS__hz) == 0)
  1858. rdfPort->Unit.Unit = LV2_PORT_UNIT_HZ;
  1859. else if (std::strcmp(unitUnit, LV2_UNITS__inch) == 0)
  1860. rdfPort->Unit.Unit = LV2_PORT_UNIT_INCH;
  1861. else if (std::strcmp(unitUnit, LV2_UNITS__khz) == 0)
  1862. rdfPort->Unit.Unit = LV2_PORT_UNIT_KHZ;
  1863. else if (std::strcmp(unitUnit, LV2_UNITS__km) == 0)
  1864. rdfPort->Unit.Unit = LV2_PORT_UNIT_KM;
  1865. else if (std::strcmp(unitUnit, LV2_UNITS__m) == 0)
  1866. rdfPort->Unit.Unit = LV2_PORT_UNIT_M;
  1867. else if (std::strcmp(unitUnit, LV2_UNITS__mhz) == 0)
  1868. rdfPort->Unit.Unit = LV2_PORT_UNIT_MHZ;
  1869. else if (std::strcmp(unitUnit, LV2_UNITS__midiNote) == 0)
  1870. rdfPort->Unit.Unit = LV2_PORT_UNIT_MIDINOTE;
  1871. else if (std::strcmp(unitUnit, LV2_UNITS__mile) == 0)
  1872. rdfPort->Unit.Unit = LV2_PORT_UNIT_MILE;
  1873. else if (std::strcmp(unitUnit, LV2_UNITS__min) == 0)
  1874. rdfPort->Unit.Unit = LV2_PORT_UNIT_MIN;
  1875. else if (std::strcmp(unitUnit, LV2_UNITS__mm) == 0)
  1876. rdfPort->Unit.Unit = LV2_PORT_UNIT_MM;
  1877. else if (std::strcmp(unitUnit, LV2_UNITS__ms) == 0)
  1878. rdfPort->Unit.Unit = LV2_PORT_UNIT_MS;
  1879. else if (std::strcmp(unitUnit, LV2_UNITS__oct) == 0)
  1880. rdfPort->Unit.Unit = LV2_PORT_UNIT_OCT;
  1881. else if (std::strcmp(unitUnit, LV2_UNITS__pc) == 0)
  1882. rdfPort->Unit.Unit = LV2_PORT_UNIT_PC;
  1883. else if (std::strcmp(unitUnit, LV2_UNITS__s) == 0)
  1884. rdfPort->Unit.Unit = LV2_PORT_UNIT_S;
  1885. else if (std::strcmp(unitUnit, LV2_UNITS__semitone12TET) == 0)
  1886. rdfPort->Unit.Unit = LV2_PORT_UNIT_SEMITONE;
  1887. else
  1888. carla_stderr("lv2_rdf_new(\"%s\") - got unknown unit '%s'", uri, unitUnit);
  1889. }
  1890. }
  1891. if (LilvNode* const unitNameNode = lilv_world_get(lv2World.me, unitUnitNode, lv2World.unit_name.me, nullptr))
  1892. {
  1893. if (const char* const unitName = lilv_node_as_string(unitNameNode))
  1894. {
  1895. rdfPort->Unit.Hints |= LV2_PORT_UNIT_NAME;
  1896. rdfPort->Unit.Name = carla_strdup(unitName);
  1897. }
  1898. lilv_node_free(unitNameNode);
  1899. }
  1900. if (LilvNode* const unitRenderNode = lilv_world_get(lv2World.me, unitUnitNode, lv2World.unit_render.me, nullptr))
  1901. {
  1902. if (const char* const unitRender = lilv_node_as_string(unitRenderNode))
  1903. {
  1904. rdfPort->Unit.Hints |= LV2_PORT_UNIT_RENDER;
  1905. rdfPort->Unit.Render = carla_strdup(unitRender);
  1906. }
  1907. lilv_node_free(unitRenderNode);
  1908. }
  1909. if (LilvNode* const unitSymbolNode = lilv_world_get(lv2World.me, unitUnitNode, lv2World.unit_symbol.me, nullptr))
  1910. {
  1911. if (const char* const unitSymbol = lilv_node_as_string(unitSymbolNode))
  1912. {
  1913. rdfPort->Unit.Hints |= LV2_PORT_UNIT_SYMBOL;
  1914. rdfPort->Unit.Symbol = carla_strdup(unitSymbol);
  1915. }
  1916. lilv_node_free(unitSymbolNode);
  1917. }
  1918. lilv_node_free(unitUnitNode);
  1919. }
  1920. }
  1921. // --------------------------------------------------------------------------------------------------------
  1922. // Set Port Minimum Size
  1923. {
  1924. if (LilvNode* const minimumSizeNode = lilv_port_get(lilvPort.parent, lilvPort.me, lv2World.rz_minSize.me))
  1925. {
  1926. const int minimumSize(lilv_node_as_int(minimumSizeNode));
  1927. if (minimumSize > 0)
  1928. rdfPort->MinimumSize = static_cast<uint32_t>(minimumSize);
  1929. else
  1930. carla_safe_assert_int("minimumSize > 0", __FILE__, __LINE__, minimumSize);
  1931. lilv_node_free(minimumSizeNode);
  1932. }
  1933. }
  1934. // --------------------------------------------------------------------------------------------------------
  1935. // Set Port Scale Points
  1936. {
  1937. Lilv::ScalePoints lilvScalePoints(lilvPort.get_scale_points());
  1938. if (const uint numScalePoints = lilvScalePoints.size())
  1939. {
  1940. rdfPort->ScalePoints = new LV2_RDF_PortScalePoint[numScalePoints];
  1941. // get all scalepoints and sort them by value
  1942. LilvScalePointMap sortedpoints;
  1943. LILV_FOREACH(scale_points, it, lilvScalePoints)
  1944. {
  1945. Lilv::ScalePoint lilvScalePoint(lilvScalePoints.get(it));
  1946. CARLA_SAFE_ASSERT_CONTINUE(lilvScalePoint.get_label() != nullptr);
  1947. if (const LilvNode* const valuenode = lilvScalePoint.get_value())
  1948. {
  1949. const double valueid = lilv_node_as_float(valuenode);
  1950. sortedpoints[valueid] = lilvScalePoint.me;
  1951. }
  1952. }
  1953. // now safe to store, sorted by using std::map
  1954. uint numUsed = 0;
  1955. for (LilvScalePointMap::iterator it=sortedpoints.begin(), end=sortedpoints.end(); it != end; ++it)
  1956. {
  1957. CARLA_SAFE_ASSERT_BREAK(numUsed < numScalePoints);
  1958. LV2_RDF_PortScalePoint* const rdfScalePoint(&rdfPort->ScalePoints[numUsed++]);
  1959. const LilvScalePoint* const scalepoint = it->second;
  1960. const LilvNode* const xlabel = lilv_scale_point_get_label(scalepoint);
  1961. const LilvNode* const xvalue = lilv_scale_point_get_value(scalepoint);
  1962. rdfScalePoint->Label = carla_strdup(lilv_node_as_string(xlabel));
  1963. rdfScalePoint->Value = lilv_node_as_float(xvalue);
  1964. }
  1965. rdfPort->ScalePointCount = numUsed;
  1966. }
  1967. lilv_nodes_free(const_cast<LilvNodes*>(lilvScalePoints.me));
  1968. }
  1969. }
  1970. }
  1971. // ----------------------------------------------------------------------------------------------------------------
  1972. // Set Plugin Parameters
  1973. {
  1974. Lilv::Nodes patchWritableNodes(lilvPlugin.get_value(lv2World.patch_writable));
  1975. if (const uint numParameters = patchWritableNodes.size())
  1976. {
  1977. rdfDescriptor->Parameters = new LV2_RDF_Parameter[numParameters];
  1978. uint numUsed = 0;
  1979. LILV_FOREACH(nodes, it, patchWritableNodes)
  1980. {
  1981. CARLA_SAFE_ASSERT_BREAK(numUsed < numParameters);
  1982. Lilv::Node patchWritableNode(patchWritableNodes.get(it));
  1983. LV2_RDF_Parameter& rdfParam(rdfDescriptor->Parameters[numUsed++]);
  1984. CARLA_SAFE_ASSERT_CONTINUE(patchWritableNode.is_uri());
  1985. rdfParam.URI = carla_strdup(patchWritableNode.as_uri());
  1986. // ----------------------------------------------------------------------------------------------------
  1987. // Set Basics
  1988. if (LilvNode* const rangeNode = lilv_world_get(lv2World.me, patchWritableNode,
  1989. lv2World.rdfs_range.me, nullptr))
  1990. {
  1991. const char* const rangeURI = lilv_node_as_string(rangeNode);
  1992. /**/ if (std::strcmp(rangeURI, LV2_ATOM__Bool) == 0)
  1993. rdfParam.Type = LV2_PARAMETER_BOOL;
  1994. else if (std::strcmp(rangeURI, LV2_ATOM__Int) == 0)
  1995. rdfParam.Type = LV2_PARAMETER_INT;
  1996. else if (std::strcmp(rangeURI, LV2_ATOM__Long) == 0)
  1997. rdfParam.Type = LV2_PARAMETER_LONG;
  1998. else if (std::strcmp(rangeURI, LV2_ATOM__Float) == 0)
  1999. rdfParam.Type = LV2_PARAMETER_FLOAT;
  2000. else if (std::strcmp(rangeURI, LV2_ATOM__Double) == 0)
  2001. rdfParam.Type = LV2_PARAMETER_DOUBLE;
  2002. else if (std::strcmp(rangeURI, LV2_ATOM__Path) == 0)
  2003. rdfParam.Type = LV2_PARAMETER_PATH;
  2004. else if (std::strcmp(rangeURI, LV2_ATOM__String) == 0)
  2005. rdfParam.Type = LV2_PARAMETER_STRING;
  2006. else
  2007. carla_stderr("lv2_rdf_new(\"%s\") - got unknown parameter type '%s'", uri, rangeURI);
  2008. lilv_node_free(rangeNode);
  2009. }
  2010. if (LilvNode* const labelNode = lilv_world_get(lv2World.me, patchWritableNode,
  2011. lv2World.rdfs_label.me, nullptr))
  2012. {
  2013. rdfParam.Label = carla_strdup(lilv_node_as_string(labelNode));
  2014. lilv_node_free(labelNode);
  2015. }
  2016. if (LilvNode* const commentNode = lilv_world_get(lv2World.me, patchWritableNode,
  2017. lv2World.rdfs_comment.me, nullptr))
  2018. {
  2019. rdfParam.Comment = carla_strdup(lilv_node_as_string(commentNode));
  2020. lilv_node_free(commentNode);
  2021. }
  2022. // ----------------------------------------------------------------------------------------------------
  2023. // Set Port Points
  2024. if (LilvNode* const defNode = lilv_world_get(lv2World.me, patchWritableNode,
  2025. lv2World.value_default.me, nullptr))
  2026. {
  2027. rdfParam.Points.Hints |= LV2_PORT_POINT_DEFAULT;
  2028. rdfParam.Points.Default = lilv_node_as_float(defNode);
  2029. lilv_node_free(defNode);
  2030. }
  2031. if (LilvNode* const minNode = lilv_world_get(lv2World.me, patchWritableNode,
  2032. lv2World.value_minimum.me, nullptr))
  2033. {
  2034. rdfParam.Points.Hints |= LV2_PORT_POINT_MINIMUM;
  2035. rdfParam.Points.Minimum = lilv_node_as_float(minNode);
  2036. lilv_node_free(minNode);
  2037. }
  2038. if (LilvNode* const maxNode = lilv_world_get(lv2World.me, patchWritableNode,
  2039. lv2World.value_maximum.me, nullptr))
  2040. {
  2041. rdfParam.Points.Hints |= LV2_PORT_POINT_MAXIMUM;
  2042. rdfParam.Points.Maximum = lilv_node_as_float(maxNode);
  2043. lilv_node_free(maxNode);
  2044. }
  2045. // ----------------------------------------------------------------------------------------------------
  2046. // Set Port Unit
  2047. if (LilvNode* const unitUnitNode = lilv_world_get(lv2World.me, patchWritableNode,
  2048. lv2World.unit_unit.me, nullptr))
  2049. {
  2050. if (lilv_node_is_uri(unitUnitNode))
  2051. {
  2052. if (const char* const unitUnit = lilv_node_as_uri(unitUnitNode))
  2053. {
  2054. rdfParam.Unit.Hints |= LV2_PORT_UNIT_UNIT;
  2055. /**/ if (std::strcmp(unitUnit, LV2_UNITS__bar) == 0)
  2056. rdfParam.Unit.Unit = LV2_PORT_UNIT_BAR;
  2057. else if (std::strcmp(unitUnit, LV2_UNITS__beat) == 0)
  2058. rdfParam.Unit.Unit = LV2_PORT_UNIT_BEAT;
  2059. else if (std::strcmp(unitUnit, LV2_UNITS__bpm) == 0)
  2060. rdfParam.Unit.Unit = LV2_PORT_UNIT_BPM;
  2061. else if (std::strcmp(unitUnit, LV2_UNITS__cent) == 0)
  2062. rdfParam.Unit.Unit = LV2_PORT_UNIT_CENT;
  2063. else if (std::strcmp(unitUnit, LV2_UNITS__cm) == 0)
  2064. rdfParam.Unit.Unit = LV2_PORT_UNIT_CM;
  2065. else if (std::strcmp(unitUnit, LV2_UNITS__coef) == 0)
  2066. rdfParam.Unit.Unit = LV2_PORT_UNIT_COEF;
  2067. else if (std::strcmp(unitUnit, LV2_UNITS__db) == 0)
  2068. rdfParam.Unit.Unit = LV2_PORT_UNIT_DB;
  2069. else if (std::strcmp(unitUnit, LV2_UNITS__degree) == 0)
  2070. rdfParam.Unit.Unit = LV2_PORT_UNIT_DEGREE;
  2071. else if (std::strcmp(unitUnit, LV2_UNITS__frame) == 0)
  2072. rdfParam.Unit.Unit = LV2_PORT_UNIT_FRAME;
  2073. else if (std::strcmp(unitUnit, LV2_UNITS__hz) == 0)
  2074. rdfParam.Unit.Unit = LV2_PORT_UNIT_HZ;
  2075. else if (std::strcmp(unitUnit, LV2_UNITS__inch) == 0)
  2076. rdfParam.Unit.Unit = LV2_PORT_UNIT_INCH;
  2077. else if (std::strcmp(unitUnit, LV2_UNITS__khz) == 0)
  2078. rdfParam.Unit.Unit = LV2_PORT_UNIT_KHZ;
  2079. else if (std::strcmp(unitUnit, LV2_UNITS__km) == 0)
  2080. rdfParam.Unit.Unit = LV2_PORT_UNIT_KM;
  2081. else if (std::strcmp(unitUnit, LV2_UNITS__m) == 0)
  2082. rdfParam.Unit.Unit = LV2_PORT_UNIT_M;
  2083. else if (std::strcmp(unitUnit, LV2_UNITS__mhz) == 0)
  2084. rdfParam.Unit.Unit = LV2_PORT_UNIT_MHZ;
  2085. else if (std::strcmp(unitUnit, LV2_UNITS__midiNote) == 0)
  2086. rdfParam.Unit.Unit = LV2_PORT_UNIT_MIDINOTE;
  2087. else if (std::strcmp(unitUnit, LV2_UNITS__mile) == 0)
  2088. rdfParam.Unit.Unit = LV2_PORT_UNIT_MILE;
  2089. else if (std::strcmp(unitUnit, LV2_UNITS__min) == 0)
  2090. rdfParam.Unit.Unit = LV2_PORT_UNIT_MIN;
  2091. else if (std::strcmp(unitUnit, LV2_UNITS__mm) == 0)
  2092. rdfParam.Unit.Unit = LV2_PORT_UNIT_MM;
  2093. else if (std::strcmp(unitUnit, LV2_UNITS__ms) == 0)
  2094. rdfParam.Unit.Unit = LV2_PORT_UNIT_MS;
  2095. else if (std::strcmp(unitUnit, LV2_UNITS__oct) == 0)
  2096. rdfParam.Unit.Unit = LV2_PORT_UNIT_OCT;
  2097. else if (std::strcmp(unitUnit, LV2_UNITS__pc) == 0)
  2098. rdfParam.Unit.Unit = LV2_PORT_UNIT_PC;
  2099. else if (std::strcmp(unitUnit, LV2_UNITS__s) == 0)
  2100. rdfParam.Unit.Unit = LV2_PORT_UNIT_S;
  2101. else if (std::strcmp(unitUnit, LV2_UNITS__semitone12TET) == 0)
  2102. rdfParam.Unit.Unit = LV2_PORT_UNIT_SEMITONE;
  2103. else
  2104. carla_stderr("lv2_rdf_new(\"%s\") - got unknown unit '%s'", uri, unitUnit);
  2105. }
  2106. }
  2107. if (LilvNode* const unitNameNode = lilv_world_get(lv2World.me, unitUnitNode,
  2108. lv2World.unit_name.me, nullptr))
  2109. {
  2110. if (const char* const unitName = lilv_node_as_string(unitNameNode))
  2111. {
  2112. rdfParam.Unit.Hints |= LV2_PORT_UNIT_NAME;
  2113. rdfParam.Unit.Name = carla_strdup(unitName);
  2114. }
  2115. lilv_node_free(unitNameNode);
  2116. }
  2117. if (LilvNode* const unitRenderNode = lilv_world_get(lv2World.me, unitUnitNode,
  2118. lv2World.unit_render.me, nullptr))
  2119. {
  2120. if (const char* const unitRender = lilv_node_as_string(unitRenderNode))
  2121. {
  2122. rdfParam.Unit.Hints |= LV2_PORT_UNIT_RENDER;
  2123. rdfParam.Unit.Render = carla_strdup(unitRender);
  2124. }
  2125. lilv_node_free(unitRenderNode);
  2126. }
  2127. if (LilvNode* const unitSymbolNode = lilv_world_get(lv2World.me, unitUnitNode,
  2128. lv2World.unit_symbol.me, nullptr))
  2129. {
  2130. if (const char* const unitSymbol = lilv_node_as_string(unitSymbolNode))
  2131. {
  2132. rdfParam.Unit.Hints |= LV2_PORT_UNIT_SYMBOL;
  2133. rdfParam.Unit.Symbol = carla_strdup(unitSymbol);
  2134. }
  2135. lilv_node_free(unitSymbolNode);
  2136. }
  2137. lilv_node_free(unitUnitNode);
  2138. }
  2139. }
  2140. rdfDescriptor->ParameterCount = numUsed;
  2141. }
  2142. lilv_nodes_free(const_cast<LilvNodes*>(patchWritableNodes.me));
  2143. }
  2144. // ----------------------------------------------------------------------------------------------------------------
  2145. // Set Plugin Presets
  2146. if (loadPresets)
  2147. {
  2148. Lilv::Nodes presetNodes(lilvPlugin.get_related(lv2World.preset_preset));
  2149. if (presetNodes.size() > 0)
  2150. {
  2151. // create a list of preset URIs (for sorting and unique-ness)
  2152. #ifdef USE_QT
  2153. QStringList presetListURIs;
  2154. LILV_FOREACH(nodes, it, presetNodes)
  2155. {
  2156. Lilv::Node presetNode(presetNodes.get(it));
  2157. QString presetURI(presetNode.as_uri());
  2158. if (! (presetURI.trimmed().isEmpty() || presetListURIs.contains(presetURI)))
  2159. presetListURIs.append(presetURI);
  2160. }
  2161. presetListURIs.sort();
  2162. rdfDescriptor->PresetCount = static_cast<uint32_t>(presetListURIs.count());
  2163. #else
  2164. water::StringArray presetListURIs;
  2165. LILV_FOREACH(nodes, it, presetNodes)
  2166. {
  2167. Lilv::Node presetNode(presetNodes.get(it));
  2168. water::String presetURI(presetNode.as_uri());
  2169. if (presetURI.trim().isNotEmpty())
  2170. presetListURIs.addIfNotAlreadyThere(presetURI);
  2171. }
  2172. presetListURIs.sortNatural();
  2173. rdfDescriptor->PresetCount = static_cast<uint32_t>(presetListURIs.size());
  2174. #endif
  2175. // create presets with unique URIs
  2176. rdfDescriptor->Presets = new LV2_RDF_Preset[rdfDescriptor->PresetCount];
  2177. // set preset data
  2178. LILV_FOREACH(nodes, it, presetNodes)
  2179. {
  2180. Lilv::Node presetNode(presetNodes.get(it));
  2181. const char* const presetURI(presetNode.as_uri());
  2182. CARLA_SAFE_ASSERT_CONTINUE(presetURI != nullptr && presetURI[0] != '\0');
  2183. // try to find label without loading the preset resource first
  2184. Lilv::Nodes presetLabelNodes(lv2World.find_nodes(presetNode, lv2World.rdfs_label, nullptr));
  2185. // failed, try loading resource
  2186. if (presetLabelNodes.size() == 0)
  2187. {
  2188. // if loading resource fails, skip this preset
  2189. if (lv2World.load_resource(presetNode) == -1)
  2190. continue;
  2191. // ok, let's try again
  2192. presetLabelNodes = lv2World.find_nodes(presetNode, lv2World.rdfs_label, nullptr);
  2193. }
  2194. if (presetLabelNodes.size() > 0)
  2195. {
  2196. #ifdef USE_QT
  2197. const int index(presetListURIs.indexOf(QString(presetURI)));
  2198. #else
  2199. const int index(presetListURIs.indexOf(water::String(presetURI)));
  2200. #endif
  2201. CARLA_SAFE_ASSERT_CONTINUE(index >= 0 && index < static_cast<int>(rdfDescriptor->PresetCount));
  2202. LV2_RDF_Preset* const rdfPreset(&rdfDescriptor->Presets[index]);
  2203. // ---------------------------------------------------
  2204. // Set Preset Information
  2205. rdfPreset->URI = carla_strdup(presetURI);
  2206. if (const char* const label = presetLabelNodes.get_first().as_string())
  2207. rdfPreset->Label = carla_strdup(label);
  2208. lilv_nodes_free(const_cast<LilvNodes*>(presetLabelNodes.me));
  2209. }
  2210. }
  2211. }
  2212. lilv_nodes_free(const_cast<LilvNodes*>(presetNodes.me));
  2213. }
  2214. // ----------------------------------------------------------------------------------------------------------------
  2215. // Set Plugin Features
  2216. {
  2217. Lilv::Nodes lilvFeatureNodes(lilvPlugin.get_supported_features());
  2218. if (const uint numFeatures = lilvFeatureNodes.size())
  2219. {
  2220. Lilv::Nodes lilvFeatureNodesR(lilvPlugin.get_required_features());
  2221. rdfDescriptor->Features = new LV2_RDF_Feature[numFeatures];
  2222. uint numUsed = 0;
  2223. LILV_FOREACH(nodes, it, lilvFeatureNodes)
  2224. {
  2225. CARLA_SAFE_ASSERT_BREAK(numUsed < numFeatures);
  2226. Lilv::Node lilvFeatureNode(lilvFeatureNodes.get(it));
  2227. LV2_RDF_Feature* const rdfFeature(&rdfDescriptor->Features[numUsed++]);
  2228. rdfFeature->Required = lilvFeatureNodesR.contains(lilvFeatureNode);
  2229. if (const char* const featureURI = lilvFeatureNode.as_uri())
  2230. rdfFeature->URI = carla_strdup(featureURI);
  2231. else
  2232. rdfFeature->URI = nullptr;
  2233. }
  2234. rdfDescriptor->FeatureCount = numUsed;
  2235. lilv_nodes_free(const_cast<LilvNodes*>(lilvFeatureNodesR.me));
  2236. }
  2237. lilv_nodes_free(const_cast<LilvNodes*>(lilvFeatureNodes.me));
  2238. }
  2239. // ----------------------------------------------------------------------------------------------------------------
  2240. // Set Plugin Extensions
  2241. {
  2242. Lilv::Nodes lilvExtensionDataNodes(lilvPlugin.get_extension_data());
  2243. if (const uint numExtensions = lilvExtensionDataNodes.size())
  2244. {
  2245. rdfDescriptor->Extensions = new LV2_URI[numExtensions];
  2246. uint numUsed = 0;
  2247. LILV_FOREACH(nodes, it, lilvExtensionDataNodes)
  2248. {
  2249. CARLA_SAFE_ASSERT_BREAK(numUsed < numExtensions);
  2250. Lilv::Node lilvExtensionDataNode(lilvExtensionDataNodes.get(it));
  2251. LV2_URI* const rdfExtension(&rdfDescriptor->Extensions[numUsed++]);
  2252. if (lilvExtensionDataNode.is_uri())
  2253. {
  2254. if (const char* const extURI = lilvExtensionDataNode.as_uri())
  2255. {
  2256. *rdfExtension = carla_strdup(extURI);
  2257. continue;
  2258. }
  2259. }
  2260. *rdfExtension = nullptr;
  2261. }
  2262. for (uint32_t x=numUsed; x < rdfDescriptor->ExtensionCount; ++x)
  2263. rdfDescriptor->Extensions[x] = nullptr;
  2264. rdfDescriptor->ExtensionCount = numUsed;
  2265. }
  2266. lilv_nodes_free(const_cast<LilvNodes*>(lilvExtensionDataNodes.me));
  2267. }
  2268. // ----------------------------------------------------------------------------------------------------------------
  2269. // Set Plugin UIs
  2270. {
  2271. const bool hasMODGui(lilvPlugin.get_modgui_resources_directory().as_uri() != nullptr);
  2272. Lilv::UIs lilvUIs(lilvPlugin.get_uis());
  2273. const uint numUIs = lilvUIs.size() + (hasMODGui ? 1 : 0);
  2274. if (numUIs > 0)
  2275. {
  2276. rdfDescriptor->UIs = new LV2_RDF_UI[numUIs];
  2277. uint numUsed = 0;
  2278. LILV_FOREACH(uis, it, lilvUIs)
  2279. {
  2280. CARLA_SAFE_ASSERT_BREAK(numUsed < numUIs);
  2281. Lilv::UI lilvUI(lilvUIs.get(it));
  2282. LV2_RDF_UI* const rdfUI(&rdfDescriptor->UIs[numUsed++]);
  2283. lv2World.load_resource(lilvUI.get_uri());
  2284. // ----------------------------------------------------------------------------------------------------
  2285. // Set UI Type
  2286. /**/ if (lilvUI.is_a(lv2World.ui_gtk2))
  2287. rdfUI->Type = LV2_UI_GTK2;
  2288. else if (lilvUI.is_a(lv2World.ui_gtk3))
  2289. rdfUI->Type = LV2_UI_GTK3;
  2290. else if (lilvUI.is_a(lv2World.ui_qt4))
  2291. rdfUI->Type = LV2_UI_QT4;
  2292. else if (lilvUI.is_a(lv2World.ui_qt5))
  2293. rdfUI->Type = LV2_UI_QT5;
  2294. else if (lilvUI.is_a(lv2World.ui_cocoa))
  2295. rdfUI->Type = LV2_UI_COCOA;
  2296. else if (lilvUI.is_a(lv2World.ui_windows))
  2297. rdfUI->Type = LV2_UI_WINDOWS;
  2298. else if (lilvUI.is_a(lv2World.ui_x11))
  2299. rdfUI->Type = LV2_UI_X11;
  2300. else if (lilvUI.is_a(lv2World.ui_external))
  2301. rdfUI->Type = LV2_UI_EXTERNAL;
  2302. else if (lilvUI.is_a(lv2World.ui_externalOld))
  2303. rdfUI->Type = LV2_UI_OLD_EXTERNAL;
  2304. else if (lilvUI.is_a(lv2World.ui))
  2305. rdfUI->Type = LV2_UI_NONE;
  2306. else
  2307. carla_stderr("lv2_rdf_new(\"%s\") - UI '%s' is of unknown type", uri, lilvUI.get_uri().as_uri());
  2308. // ----------------------------------------------------------------------------------------------------
  2309. // Set UI Information
  2310. {
  2311. if (const char* const uiURI = lilvUI.get_uri().as_uri())
  2312. rdfUI->URI = carla_strdup(uiURI);
  2313. if (const char* const uiBinary = lilvUI.get_binary_uri().as_string())
  2314. rdfUI->Binary = carla_strdup_free(lilv_file_uri_parse(uiBinary, nullptr));
  2315. if (const char* const uiBundle = lilvUI.get_bundle_uri().as_string())
  2316. rdfUI->Bundle = carla_strdup_free(lilv_file_uri_parse(uiBundle, nullptr));
  2317. }
  2318. // ----------------------------------------------------------------------------------------------------
  2319. // Set UI Features
  2320. {
  2321. Lilv::Nodes lilvFeatureNodes(lilvUI.get_supported_features());
  2322. if (const uint numFeatures = lilvFeatureNodes.size())
  2323. {
  2324. Lilv::Nodes lilvFeatureNodesR(lilvUI.get_required_features());
  2325. rdfUI->Features = new LV2_RDF_Feature[numFeatures];
  2326. uint numUsed2 = 0;
  2327. LILV_FOREACH(nodes, it2, lilvFeatureNodes)
  2328. {
  2329. CARLA_SAFE_ASSERT_UINT2_BREAK(numUsed2 < numFeatures, numUsed2, numFeatures);
  2330. Lilv::Node lilvFeatureNode(lilvFeatureNodes.get(it2));
  2331. LV2_RDF_Feature* const rdfFeature(&rdfUI->Features[numUsed2++]);
  2332. rdfFeature->Required = lilvFeatureNodesR.contains(lilvFeatureNode);
  2333. if (const char* const featureURI = lilvFeatureNode.as_uri())
  2334. rdfFeature->URI = carla_strdup(featureURI);
  2335. else
  2336. rdfFeature->URI = nullptr;
  2337. }
  2338. rdfUI->FeatureCount = numUsed2;
  2339. lilv_nodes_free(const_cast<LilvNodes*>(lilvFeatureNodesR.me));
  2340. }
  2341. lilv_nodes_free(const_cast<LilvNodes*>(lilvFeatureNodes.me));
  2342. }
  2343. // ----------------------------------------------------------------------------------------------------
  2344. // Set UI Extensions
  2345. {
  2346. Lilv::Nodes lilvExtensionDataNodes(lilvUI.get_extension_data());
  2347. if (const uint numExtensions = lilvExtensionDataNodes.size())
  2348. {
  2349. rdfUI->Extensions = new LV2_URI[numExtensions];
  2350. uint numUsed2 = 0;
  2351. LILV_FOREACH(nodes, it2, lilvExtensionDataNodes)
  2352. {
  2353. CARLA_SAFE_ASSERT_UINT2_BREAK(numUsed2 < numExtensions, numUsed2, numExtensions);
  2354. Lilv::Node lilvExtensionDataNode(lilvExtensionDataNodes.get(it2));
  2355. LV2_URI* const rdfExtension(&rdfUI->Extensions[numUsed2++]);
  2356. if (lilvExtensionDataNode.is_uri())
  2357. {
  2358. if (const char* const extURI = lilvExtensionDataNode.as_uri())
  2359. {
  2360. *rdfExtension = carla_strdup(extURI);
  2361. continue;
  2362. }
  2363. }
  2364. *rdfExtension = nullptr;
  2365. }
  2366. for (uint x2=numUsed2; x2 < rdfUI->ExtensionCount; ++x2)
  2367. rdfUI->Extensions[x2] = nullptr;
  2368. rdfUI->ExtensionCount = numUsed2;
  2369. }
  2370. lilv_nodes_free(const_cast<LilvNodes*>(lilvExtensionDataNodes.me));
  2371. }
  2372. // ----------------------------------------------------------------------------------------------------
  2373. // Set UI Port Notifications
  2374. {
  2375. Lilv::Nodes portNotifNodes(lv2World.find_nodes(lilvUI.get_uri(), lv2World.ui_portNotif.me, nullptr));
  2376. if (const uint portNotifCount = portNotifNodes.size())
  2377. {
  2378. rdfUI->PortNotificationCount = portNotifCount;
  2379. rdfUI->PortNotifications = new LV2_RDF_UI_PortNotification[portNotifCount];
  2380. uint numUsed2 = 0;
  2381. LILV_FOREACH(nodes, it2, portNotifNodes)
  2382. {
  2383. CARLA_SAFE_ASSERT_UINT2_BREAK(numUsed2 < portNotifCount, numUsed2, portNotifCount);
  2384. Lilv::Node portNotifNode(portNotifNodes.get(it2));
  2385. LV2_RDF_UI_PortNotification* const rdfPortNotif(&rdfUI->PortNotifications[numUsed2++]);
  2386. LilvNode* const protocolNode = lilv_world_get(lv2World.me, portNotifNode,
  2387. lv2World.ui_protocol.me, nullptr);
  2388. if (protocolNode != nullptr)
  2389. {
  2390. CARLA_SAFE_ASSERT_CONTINUE(lilv_node_is_uri(protocolNode));
  2391. const char* const protocol = lilv_node_as_uri(protocolNode);
  2392. CARLA_SAFE_ASSERT_CONTINUE(protocol != nullptr && protocol[0] != '\0');
  2393. /**/ if (std::strcmp(protocol, LV2_UI__floatProtocol) == 0)
  2394. rdfPortNotif->Protocol = LV2_UI_PORT_PROTOCOL_FLOAT;
  2395. else if (std::strcmp(protocol, LV2_UI__peakProtocol) == 0)
  2396. rdfPortNotif->Protocol = LV2_UI_PORT_PROTOCOL_PEAK;
  2397. }
  2398. else
  2399. {
  2400. rdfPortNotif->Protocol = LV2_UI_PORT_PROTOCOL_FLOAT;
  2401. }
  2402. /**/ if (LilvNode* const symbolNode = lilv_world_get(lv2World.me, portNotifNode,
  2403. lv2World.symbol.me, nullptr))
  2404. {
  2405. CARLA_SAFE_ASSERT_CONTINUE(lilv_node_is_string(symbolNode));
  2406. const char* const symbol = lilv_node_as_string(symbolNode);
  2407. CARLA_SAFE_ASSERT_CONTINUE(symbol != nullptr && symbol[0] != '\0');
  2408. rdfPortNotif->Symbol = carla_strdup(symbol);
  2409. lilv_node_free(symbolNode);
  2410. }
  2411. else if (LilvNode* const indexNode = lilv_world_get(lv2World.me, portNotifNode,
  2412. lv2World.ui_portIndex.me, nullptr))
  2413. {
  2414. CARLA_SAFE_ASSERT_CONTINUE(lilv_node_is_int(indexNode));
  2415. const int index = lilv_node_as_int(indexNode);
  2416. CARLA_SAFE_ASSERT_CONTINUE(index >= 0);
  2417. rdfPortNotif->Index = static_cast<uint32_t>(index);
  2418. lilv_node_free(indexNode);
  2419. }
  2420. lilv_node_free(protocolNode);
  2421. }
  2422. }
  2423. lilv_nodes_free(const_cast<LilvNodes*>(portNotifNodes.me));
  2424. }
  2425. }
  2426. for (; hasMODGui;)
  2427. {
  2428. CARLA_SAFE_ASSERT_BREAK(numUsed == numUIs-1);
  2429. LV2_RDF_UI* const rdfUI(&rdfDescriptor->UIs[numUsed++]);
  2430. // -------------------------------------------------------
  2431. // Set UI Type
  2432. rdfUI->Type = LV2_UI_MOD;
  2433. // -------------------------------------------------------
  2434. // Set UI Information
  2435. if (const char* const resDir = lilvPlugin.get_modgui_resources_directory().as_uri())
  2436. rdfUI->URI = carla_strdup_free(lilv_file_uri_parse(resDir, nullptr));
  2437. if (rdfDescriptor->Bundle != nullptr)
  2438. rdfUI->Bundle = carla_strdup(rdfDescriptor->Bundle);
  2439. break;
  2440. }
  2441. rdfDescriptor->UICount = numUsed;
  2442. }
  2443. lilv_nodes_free(const_cast<LilvNodes*>(lilvUIs.me));
  2444. }
  2445. return rdfDescriptor;
  2446. }
  2447. // --------------------------------------------------------------------------------------------------------------------
  2448. // Check if we support a plugin port
  2449. static inline
  2450. bool is_lv2_port_supported(const LV2_Property types) noexcept
  2451. {
  2452. if (LV2_IS_PORT_CONTROL(types))
  2453. return true;
  2454. if (LV2_IS_PORT_AUDIO(types))
  2455. return true;
  2456. if (LV2_IS_PORT_CV(types))
  2457. return true;
  2458. if (LV2_IS_PORT_ATOM_SEQUENCE(types))
  2459. return true;
  2460. if (LV2_IS_PORT_EVENT(types))
  2461. return true;
  2462. if (LV2_IS_PORT_MIDI_LL(types))
  2463. return true;
  2464. return false;
  2465. }
  2466. // --------------------------------------------------------------------------------------------------------------------
  2467. // Check if we support a plugin feature
  2468. static inline
  2469. bool is_lv2_feature_supported(const LV2_URI uri) noexcept
  2470. {
  2471. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0', false);
  2472. if (std::strcmp(uri, LV2_BUF_SIZE__boundedBlockLength) == 0)
  2473. return true;
  2474. if (std::strcmp(uri, LV2_BUF_SIZE__fixedBlockLength) == 0)
  2475. return true;
  2476. if (std::strcmp(uri, LV2_BUF_SIZE__powerOf2BlockLength) == 0)
  2477. return true;
  2478. if (std::strcmp(uri, LV2_CORE__hardRTCapable) == 0)
  2479. return true;
  2480. if (std::strcmp(uri, LV2_CORE__inPlaceBroken) == 0)
  2481. return true;
  2482. if (std::strcmp(uri, LV2_CORE__isLive) == 0)
  2483. return true;
  2484. if (std::strcmp(uri, LV2_EVENT_URI) == 0)
  2485. return true;
  2486. if (std::strcmp(uri, LV2_INLINEDISPLAY__queue_draw) == 0)
  2487. return true;
  2488. if (std::strcmp(uri, LV2_LOG__log) == 0)
  2489. return true;
  2490. if (std::strcmp(uri, LV2_OPTIONS__options) == 0)
  2491. return true;
  2492. if (std::strcmp(uri, LV2_PROGRAMS__Host) == 0)
  2493. return true;
  2494. if (std::strcmp(uri, LV2_RESIZE_PORT__resize) == 0)
  2495. return true;
  2496. if (std::strcmp(uri, LV2_RTSAFE_MEMORY_POOL__Pool) == 0)
  2497. return true;
  2498. if (std::strcmp(uri, LV2_RTSAFE_MEMORY_POOL_DEPRECATED_URI) == 0)
  2499. return true;
  2500. if (std::strcmp(uri, LV2_STATE__loadDefaultState) == 0)
  2501. return true;
  2502. if (std::strcmp(uri, LV2_STATE__makePath) == 0)
  2503. return true;
  2504. if (std::strcmp(uri, LV2_STATE__mapPath) == 0)
  2505. return true;
  2506. if (std::strcmp(uri, LV2_PORT_PROPS__supportsStrictBounds) == 0)
  2507. return true;
  2508. if (std::strcmp(uri, LV2_URI_MAP_URI) == 0)
  2509. return true;
  2510. if (std::strcmp(uri, LV2_URID__map) == 0)
  2511. return true;
  2512. if (std::strcmp(uri, LV2_URID__unmap) == 0)
  2513. return true;
  2514. if (std::strcmp(uri, LV2_WORKER__schedule) == 0)
  2515. return true;
  2516. return false;
  2517. }
  2518. // --------------------------------------------------------------------------------------------------------------------
  2519. // Check if we support a plugin or UI feature
  2520. static inline
  2521. bool is_lv2_ui_feature_supported(const LV2_URI uri) noexcept
  2522. {
  2523. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0', false);
  2524. if (is_lv2_feature_supported(uri))
  2525. return true;
  2526. #ifndef BUILD_BRIDGE_UI
  2527. if (std::strcmp(uri, LV2_DATA_ACCESS_URI) == 0)
  2528. return true;
  2529. if (std::strcmp(uri, LV2_INSTANCE_ACCESS_URI) == 0)
  2530. return true;
  2531. #endif
  2532. if (std::strcmp(uri, LV2_UI__fixedSize) == 0)
  2533. return true;
  2534. if (std::strcmp(uri, LV2_UI__idleInterface) == 0)
  2535. return true;
  2536. if (std::strcmp(uri, LV2_UI__makeResident) == 0)
  2537. return true;
  2538. if (std::strcmp(uri, LV2_UI__makeSONameResident) == 0)
  2539. return true;
  2540. if (std::strcmp(uri, LV2_UI__noUserResize) == 0)
  2541. return true;
  2542. if (std::strcmp(uri, LV2_UI__parent) == 0)
  2543. return true;
  2544. if (std::strcmp(uri, LV2_UI__portMap) == 0)
  2545. return true;
  2546. if (std::strcmp(uri, LV2_UI__portSubscribe) == 0)
  2547. return true;
  2548. if (std::strcmp(uri, LV2_UI__resize) == 0)
  2549. return true;
  2550. if (std::strcmp(uri, LV2_UI__touch) == 0)
  2551. return true;
  2552. if (std::strcmp(uri, LV2_EXTERNAL_UI__Widget) == 0)
  2553. return true;
  2554. if (std::strcmp(uri, LV2_EXTERNAL_UI_DEPRECATED_URI) == 0)
  2555. return true;
  2556. return false;
  2557. }
  2558. // --------------------------------------------------------------------------------------------------------------------
  2559. #endif // CARLA_LV2_UTILS_HPP_INCLUDED