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.

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