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.

3106 lines
127KB

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