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.

3184 lines
131KB

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