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.

3144 lines
129KB

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