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.

3187 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 atomBool;
  1284. LV2_URID atomObject;
  1285. LV2_URID atomDouble;
  1286. LV2_URID atomFloat;
  1287. LV2_URID atomInt;
  1288. LV2_URID atomLong;
  1289. LV2_URID atomPath;
  1290. LV2_URID atomSequence;
  1291. LV2_URID atomString;
  1292. LV2_URID atomURID;
  1293. LV2_URID carlaFile;
  1294. LV2_URID carlaFileAudio;
  1295. LV2_URID carlaFileMIDI;
  1296. LV2_URID carlaPreview;
  1297. LV2_URID midiEvent;
  1298. LV2_URID patchProperty;
  1299. LV2_URID patchGet;
  1300. LV2_URID patchSet;
  1301. LV2_URID patchValue;
  1302. LV2_URID timePos;
  1303. LV2_URID timeBar;
  1304. LV2_URID timeBarBeat;
  1305. LV2_URID timeBeatsPerBar;
  1306. LV2_URID timeBeatsPerMinute;
  1307. LV2_URID timeBeatUnit;
  1308. LV2_URID timeFrame;
  1309. LV2_URID timeSpeed;
  1310. LV2_URID timeTicksPerBeat;
  1311. LV2_URID carlaRequestIdle;
  1312. LV2_URID carlaUiEvents;
  1313. URIDs()
  1314. : atomBlank(0),
  1315. atomBool(0),
  1316. atomObject(0),
  1317. atomDouble(0),
  1318. atomFloat(0),
  1319. atomInt(0),
  1320. atomLong(0),
  1321. atomPath(0),
  1322. atomSequence(0),
  1323. atomString(0),
  1324. atomURID(0),
  1325. carlaFile(0),
  1326. carlaFileAudio(0),
  1327. carlaFileMIDI(0),
  1328. carlaPreview(0),
  1329. midiEvent(0),
  1330. patchProperty(0),
  1331. patchGet(0),
  1332. patchSet(0),
  1333. patchValue(0),
  1334. timePos(0),
  1335. timeBar(0),
  1336. timeBarBeat(0),
  1337. timeBeatsPerBar(0),
  1338. timeBeatsPerMinute(0),
  1339. timeBeatUnit(0),
  1340. timeFrame(0),
  1341. timeSpeed(0),
  1342. timeTicksPerBeat(0),
  1343. carlaRequestIdle(0),
  1344. carlaUiEvents(0) {}
  1345. void map(const LV2_URID_Map* const uridMap)
  1346. {
  1347. atomBlank = uridMap->map(uridMap->handle, LV2_ATOM__Blank);
  1348. atomBool = uridMap->map(uridMap->handle, LV2_ATOM__Bool);
  1349. atomObject = uridMap->map(uridMap->handle, LV2_ATOM__Object);
  1350. atomDouble = uridMap->map(uridMap->handle, LV2_ATOM__Double);
  1351. atomFloat = uridMap->map(uridMap->handle, LV2_ATOM__Float);
  1352. atomInt = uridMap->map(uridMap->handle, LV2_ATOM__Int);
  1353. atomLong = uridMap->map(uridMap->handle, LV2_ATOM__Long);
  1354. atomPath = uridMap->map(uridMap->handle, LV2_ATOM__Path);
  1355. atomSequence = uridMap->map(uridMap->handle, LV2_ATOM__Sequence);
  1356. atomString = uridMap->map(uridMap->handle, LV2_ATOM__String);
  1357. atomURID = uridMap->map(uridMap->handle, LV2_ATOM__URID);
  1358. carlaFile = uridMap->map(uridMap->handle, "http://kxstudio.sf.net/carla/file");
  1359. carlaFileAudio = uridMap->map(uridMap->handle, "http://kxstudio.sf.net/carla/file/audio");
  1360. carlaFileMIDI = uridMap->map(uridMap->handle, "http://kxstudio.sf.net/carla/file/midi");
  1361. carlaPreview = uridMap->map(uridMap->handle, "http://kxstudio.sf.net/carla/preview");
  1362. midiEvent = uridMap->map(uridMap->handle, LV2_MIDI__MidiEvent);
  1363. patchProperty = uridMap->map(uridMap->handle, LV2_PATCH__property);
  1364. patchGet = uridMap->map(uridMap->handle, LV2_PATCH__Get);
  1365. patchSet = uridMap->map(uridMap->handle, LV2_PATCH__Set);
  1366. patchValue = uridMap->map(uridMap->handle, LV2_PATCH__value);
  1367. timePos = uridMap->map(uridMap->handle, LV2_TIME__Position);
  1368. timeBar = uridMap->map(uridMap->handle, LV2_TIME__bar);
  1369. timeBarBeat = uridMap->map(uridMap->handle, LV2_TIME__barBeat);
  1370. timeBeatUnit = uridMap->map(uridMap->handle, LV2_TIME__beatUnit);
  1371. timeFrame = uridMap->map(uridMap->handle, LV2_TIME__frame);
  1372. timeSpeed = uridMap->map(uridMap->handle, LV2_TIME__speed);
  1373. timeBeatsPerBar = uridMap->map(uridMap->handle, LV2_TIME__beatsPerBar);
  1374. timeBeatsPerMinute = uridMap->map(uridMap->handle, LV2_TIME__beatsPerMinute);
  1375. timeTicksPerBeat = uridMap->map(uridMap->handle, LV2_KXSTUDIO_PROPERTIES__TimePositionTicksPerBeat);
  1376. carlaRequestIdle = uridMap->map(uridMap->handle, "urn:carla:idle");
  1377. carlaUiEvents = uridMap->map(uridMap->handle, "urn:carla:uiEvents");
  1378. }
  1379. } fURIs;
  1380. struct UI {
  1381. const LV2_External_UI_Host* host;
  1382. const LV2UI_Touch* touch;
  1383. LV2UI_Write_Function writeFunction;
  1384. LV2UI_Controller controller;
  1385. bool isVisible;
  1386. UI()
  1387. : host(nullptr),
  1388. touch(nullptr),
  1389. writeFunction(nullptr),
  1390. controller(nullptr),
  1391. isVisible(false) {}
  1392. } fUI;
  1393. private:
  1394. // ----------------------------------------------------------------------------------------------------------------
  1395. #define handlePtr ((Lv2PluginBaseClass*)handle)
  1396. static void extui_run(LV2_External_UI_Widget_Compat* handle)
  1397. {
  1398. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  1399. handlePtr->handleUiRun();
  1400. }
  1401. static void extui_show(LV2_External_UI_Widget_Compat* handle)
  1402. {
  1403. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  1404. carla_debug("extui_show(%p)", handle);
  1405. handlePtr->handleUiShow();
  1406. }
  1407. static void extui_hide(LV2_External_UI_Widget_Compat* handle)
  1408. {
  1409. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  1410. carla_debug("extui_hide(%p)", handle);
  1411. handlePtr->handleUiHide();
  1412. }
  1413. #undef handlePtr
  1414. // ----------------------------------------------------------------------------------------------------------------
  1415. void clearTimeData() noexcept;
  1416. // ----------------------------------------------------------------------------------------------------------------
  1417. CARLA_DECLARE_NON_COPY_STRUCT(Lv2PluginBaseClass)
  1418. };
  1419. // --------------------------------------------------------------------------------------------------------------------
  1420. // Create new RDF object (using lilv)
  1421. static inline
  1422. const LV2_RDF_Descriptor* lv2_rdf_new(const LV2_URI uri, const bool loadPresets)
  1423. {
  1424. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0', nullptr);
  1425. Lv2WorldClass& lv2World(Lv2WorldClass::getInstance());
  1426. const LilvPlugin* const cPlugin(lv2World.getPluginFromURI(uri));
  1427. CARLA_SAFE_ASSERT_RETURN(cPlugin != nullptr, nullptr);
  1428. Lilv::Plugin lilvPlugin(cPlugin);
  1429. LV2_RDF_Descriptor* const rdfDescriptor(new LV2_RDF_Descriptor());
  1430. CarlaStringList portGroupURIs(false); // does not allocate own elements
  1431. LinkedList<LilvNode*> portGroupNodes;
  1432. // ----------------------------------------------------------------------------------------------------------------
  1433. // Set Plugin Type
  1434. {
  1435. Lilv::Nodes typeNodes(lilvPlugin.get_value(lv2World.rdf_type));
  1436. if (typeNodes.size() > 0)
  1437. {
  1438. if (typeNodes.contains(lv2World.class_allpass))
  1439. rdfDescriptor->Type[0] |= LV2_PLUGIN_ALLPASS;
  1440. if (typeNodes.contains(lv2World.class_amplifier))
  1441. rdfDescriptor->Type[0] |= LV2_PLUGIN_AMPLIFIER;
  1442. if (typeNodes.contains(lv2World.class_analyzer))
  1443. rdfDescriptor->Type[1] |= LV2_PLUGIN_ANALYSER;
  1444. if (typeNodes.contains(lv2World.class_bandpass))
  1445. rdfDescriptor->Type[0] |= LV2_PLUGIN_BANDPASS;
  1446. if (typeNodes.contains(lv2World.class_chorus))
  1447. rdfDescriptor->Type[1] |= LV2_PLUGIN_CHORUS;
  1448. if (typeNodes.contains(lv2World.class_comb))
  1449. rdfDescriptor->Type[1] |= LV2_PLUGIN_COMB;
  1450. if (typeNodes.contains(lv2World.class_compressor))
  1451. rdfDescriptor->Type[0] |= LV2_PLUGIN_COMPRESSOR;
  1452. if (typeNodes.contains(lv2World.class_constant))
  1453. rdfDescriptor->Type[1] |= LV2_PLUGIN_CONSTANT;
  1454. if (typeNodes.contains(lv2World.class_converter))
  1455. rdfDescriptor->Type[1] |= LV2_PLUGIN_CONVERTER;
  1456. if (typeNodes.contains(lv2World.class_delay))
  1457. rdfDescriptor->Type[0] |= LV2_PLUGIN_DELAY;
  1458. if (typeNodes.contains(lv2World.class_distortion))
  1459. rdfDescriptor->Type[0] |= LV2_PLUGIN_DISTORTION;
  1460. if (typeNodes.contains(lv2World.class_dynamics))
  1461. rdfDescriptor->Type[0] |= LV2_PLUGIN_DYNAMICS;
  1462. if (typeNodes.contains(lv2World.class_eq))
  1463. rdfDescriptor->Type[0] |= LV2_PLUGIN_EQ;
  1464. if (typeNodes.contains(lv2World.class_envelope))
  1465. rdfDescriptor->Type[0] |= LV2_PLUGIN_ENVELOPE;
  1466. if (typeNodes.contains(lv2World.class_expander))
  1467. rdfDescriptor->Type[0] |= LV2_PLUGIN_EXPANDER;
  1468. if (typeNodes.contains(lv2World.class_filter))
  1469. rdfDescriptor->Type[0] |= LV2_PLUGIN_FILTER;
  1470. if (typeNodes.contains(lv2World.class_flanger))
  1471. rdfDescriptor->Type[1] |= LV2_PLUGIN_FLANGER;
  1472. if (typeNodes.contains(lv2World.class_function))
  1473. rdfDescriptor->Type[1] |= LV2_PLUGIN_FUNCTION;
  1474. if (typeNodes.contains(lv2World.class_gate))
  1475. rdfDescriptor->Type[0] |= LV2_PLUGIN_GATE;
  1476. if (typeNodes.contains(lv2World.class_generator))
  1477. rdfDescriptor->Type[1] |= LV2_PLUGIN_GENERATOR;
  1478. if (typeNodes.contains(lv2World.class_highpass))
  1479. rdfDescriptor->Type[0] |= LV2_PLUGIN_HIGHPASS;
  1480. if (typeNodes.contains(lv2World.class_instrument))
  1481. rdfDescriptor->Type[1] |= LV2_PLUGIN_INSTRUMENT;
  1482. if (typeNodes.contains(lv2World.class_limiter))
  1483. rdfDescriptor->Type[0] |= LV2_PLUGIN_LIMITER;
  1484. if (typeNodes.contains(lv2World.class_lowpass))
  1485. rdfDescriptor->Type[0] |= LV2_PLUGIN_LOWPASS;
  1486. if (typeNodes.contains(lv2World.class_mixer))
  1487. rdfDescriptor->Type[1] |= LV2_PLUGIN_MIXER;
  1488. if (typeNodes.contains(lv2World.class_modulator))
  1489. rdfDescriptor->Type[1] |= LV2_PLUGIN_MODULATOR;
  1490. if (typeNodes.contains(lv2World.class_multiEQ))
  1491. rdfDescriptor->Type[0] |= LV2_PLUGIN_MULTI_EQ;
  1492. if (typeNodes.contains(lv2World.class_oscillator))
  1493. rdfDescriptor->Type[1] |= LV2_PLUGIN_OSCILLATOR;
  1494. if (typeNodes.contains(lv2World.class_paraEQ))
  1495. rdfDescriptor->Type[0] |= LV2_PLUGIN_PARA_EQ;
  1496. if (typeNodes.contains(lv2World.class_phaser))
  1497. rdfDescriptor->Type[1] |= LV2_PLUGIN_PHASER;
  1498. if (typeNodes.contains(lv2World.class_pitch))
  1499. rdfDescriptor->Type[1] |= LV2_PLUGIN_PITCH;
  1500. if (typeNodes.contains(lv2World.class_reverb))
  1501. rdfDescriptor->Type[0] |= LV2_PLUGIN_REVERB;
  1502. if (typeNodes.contains(lv2World.class_simulator))
  1503. rdfDescriptor->Type[0] |= LV2_PLUGIN_SIMULATOR;
  1504. if (typeNodes.contains(lv2World.class_spatial))
  1505. rdfDescriptor->Type[1] |= LV2_PLUGIN_SPATIAL;
  1506. if (typeNodes.contains(lv2World.class_spectral))
  1507. rdfDescriptor->Type[1] |= LV2_PLUGIN_SPECTRAL;
  1508. if (typeNodes.contains(lv2World.class_utility))
  1509. rdfDescriptor->Type[1] |= LV2_PLUGIN_UTILITY;
  1510. if (typeNodes.contains(lv2World.class_waveshaper))
  1511. rdfDescriptor->Type[0] |= LV2_PLUGIN_WAVESHAPER;
  1512. }
  1513. lilv_nodes_free(const_cast<LilvNodes*>(typeNodes.me));
  1514. }
  1515. // ----------------------------------------------------------------------------------------------------------------
  1516. // Set Plugin Information
  1517. {
  1518. rdfDescriptor->URI = carla_strdup(uri);
  1519. if (LilvNode* const nameNode = lilv_plugin_get_name(lilvPlugin.me))
  1520. {
  1521. if (const char* const name = lilv_node_as_string(nameNode))
  1522. rdfDescriptor->Name = carla_strdup(name);
  1523. lilv_node_free(nameNode);
  1524. }
  1525. if (const char* const author = lilvPlugin.get_author_name().as_string())
  1526. rdfDescriptor->Author = carla_strdup(author);
  1527. if (const char* const binary = lilvPlugin.get_library_uri().as_string())
  1528. rdfDescriptor->Binary = carla_strdup_free(lilv_file_uri_parse(binary, nullptr));
  1529. if (const char* const bundle = lilvPlugin.get_bundle_uri().as_string())
  1530. rdfDescriptor->Bundle = carla_strdup_free(lilv_file_uri_parse(bundle, nullptr));
  1531. Lilv::Nodes licenseNodes(lilvPlugin.get_value(lv2World.doap_license));
  1532. if (licenseNodes.size() > 0)
  1533. {
  1534. if (const char* const license = licenseNodes.get_first().as_string())
  1535. rdfDescriptor->License = carla_strdup(license);
  1536. }
  1537. lilv_nodes_free(const_cast<LilvNodes*>(licenseNodes.me));
  1538. }
  1539. // ----------------------------------------------------------------------------------------------------------------
  1540. // Set Plugin UniqueID
  1541. {
  1542. Lilv::Nodes replaceNodes(lilvPlugin.get_value(lv2World.dct_replaces));
  1543. if (replaceNodes.size() > 0)
  1544. {
  1545. Lilv::Node replaceNode(replaceNodes.get_first());
  1546. if (replaceNode.is_uri())
  1547. {
  1548. #ifdef USE_QT
  1549. const QString replaceURI(replaceNode.as_uri());
  1550. if (replaceURI.startsWith("urn:"))
  1551. {
  1552. const QString replaceId(replaceURI.split(":").last());
  1553. bool ok;
  1554. const ulong uniqueId(replaceId.toULong(&ok));
  1555. if (ok && uniqueId != 0)
  1556. rdfDescriptor->UniqueID = uniqueId;
  1557. }
  1558. #else
  1559. const water::String replaceURI(replaceNode.as_uri());
  1560. if (replaceURI.startsWith("urn:"))
  1561. {
  1562. const int uniqueId(replaceURI.getTrailingIntValue());
  1563. if (uniqueId > 0)
  1564. rdfDescriptor->UniqueID = static_cast<ulong>(uniqueId);
  1565. }
  1566. #endif
  1567. }
  1568. }
  1569. lilv_nodes_free(const_cast<LilvNodes*>(replaceNodes.me));
  1570. }
  1571. // ----------------------------------------------------------------------------------------------------------------
  1572. // Set Plugin Ports
  1573. if (const uint numPorts = lilvPlugin.get_num_ports())
  1574. {
  1575. rdfDescriptor->PortCount = numPorts;
  1576. rdfDescriptor->Ports = new LV2_RDF_Port[numPorts];
  1577. for (uint i = 0; i < numPorts; ++i)
  1578. {
  1579. Lilv::Port lilvPort(lilvPlugin.get_port_by_index(i));
  1580. LV2_RDF_Port* const rdfPort(&rdfDescriptor->Ports[i]);
  1581. // --------------------------------------------------------------------------------------------------------
  1582. // Set Port Information
  1583. {
  1584. if (LilvNode* const nameNode = lilv_port_get_name(lilvPlugin.me, lilvPort.me))
  1585. {
  1586. if (const char* const name = lilv_node_as_string(nameNode))
  1587. rdfPort->Name = carla_strdup(name);
  1588. lilv_node_free(nameNode);
  1589. }
  1590. if (const char* const symbol = lilv_node_as_string(lilvPort.get_symbol()))
  1591. rdfPort->Symbol = carla_strdup(symbol);
  1592. if (LilvNode* const commentNode = lilvPort.get(lv2World.rdfs_comment.me))
  1593. {
  1594. rdfPort->Comment = carla_strdup(lilv_node_as_string(commentNode));
  1595. lilv_node_free(commentNode);
  1596. }
  1597. if (LilvNode* const groupNode = lilvPort.get(lv2World.pg_group.me))
  1598. {
  1599. rdfPort->GroupURI = carla_strdup(lilv_node_as_uri(groupNode));
  1600. if (portGroupURIs.appendUnique(rdfPort->GroupURI))
  1601. portGroupNodes.append(groupNode);
  1602. else
  1603. lilv_node_free(groupNode);
  1604. }
  1605. }
  1606. // --------------------------------------------------------------------------------------------------------
  1607. // Set Port Mode and Type
  1608. {
  1609. // Input or Output
  1610. /**/ if (lilvPort.is_a(lv2World.port_input))
  1611. rdfPort->Types |= LV2_PORT_INPUT;
  1612. else if (lilvPort.is_a(lv2World.port_output))
  1613. rdfPort->Types |= LV2_PORT_OUTPUT;
  1614. else
  1615. carla_stderr("lv2_rdf_new(\"%s\") - port '%s' is not input or output", uri, rdfPort->Name);
  1616. // Data Type
  1617. /**/ if (lilvPort.is_a(lv2World.port_control))
  1618. {
  1619. rdfPort->Types |= LV2_PORT_CONTROL;
  1620. }
  1621. else if (lilvPort.is_a(lv2World.port_audio))
  1622. {
  1623. rdfPort->Types |= LV2_PORT_AUDIO;
  1624. }
  1625. else if (lilvPort.is_a(lv2World.port_cv))
  1626. {
  1627. rdfPort->Types |= LV2_PORT_CV;
  1628. }
  1629. else if (lilvPort.is_a(lv2World.port_atom))
  1630. {
  1631. rdfPort->Types |= LV2_PORT_ATOM;
  1632. Lilv::Nodes bufferTypeNodes(lilvPort.get_value(lv2World.atom_bufferType));
  1633. for (LilvIter* it = lilv_nodes_begin(bufferTypeNodes.me); ! lilv_nodes_is_end(bufferTypeNodes.me, it); it = lilv_nodes_next(bufferTypeNodes.me, it))
  1634. {
  1635. const Lilv::Node node(lilv_nodes_get(bufferTypeNodes.me, it));
  1636. CARLA_SAFE_ASSERT_CONTINUE(node.is_uri());
  1637. if (node.equals(lv2World.atom_sequence))
  1638. rdfPort->Types |= LV2_PORT_ATOM_SEQUENCE;
  1639. else
  1640. carla_stderr("lv2_rdf_new(\"%s\") - port '%s' uses an unknown atom buffer type '%s'", uri, rdfPort->Name, node.as_uri());
  1641. }
  1642. Lilv::Nodes supportNodes(lilvPort.get_value(lv2World.atom_supports));
  1643. for (LilvIter* it = lilv_nodes_begin(supportNodes.me); ! lilv_nodes_is_end(supportNodes.me, it); it = lilv_nodes_next(supportNodes.me, it))
  1644. {
  1645. const Lilv::Node node(lilv_nodes_get(supportNodes.me, it));
  1646. CARLA_SAFE_ASSERT_CONTINUE(node.is_uri());
  1647. /**/ if (node.equals(lv2World.midi_event))
  1648. rdfPort->Types |= LV2_PORT_DATA_MIDI_EVENT;
  1649. else if (node.equals(lv2World.patch_message))
  1650. rdfPort->Types |= LV2_PORT_DATA_PATCH_MESSAGE;
  1651. else if (node.equals(lv2World.time_position))
  1652. rdfPort->Types |= LV2_PORT_DATA_TIME_POSITION;
  1653. #if 0
  1654. // something new we don't support yet?
  1655. else if (std::strstr(node.as_uri(), "lv2plug.in/") != nullptr)
  1656. carla_stderr("lv2_rdf_new(\"%s\") - port '%s' is of atom type but has unsupported data '%s'", uri, rdfPort->Name, node.as_uri());
  1657. #endif
  1658. }
  1659. lilv_nodes_free(const_cast<LilvNodes*>(bufferTypeNodes.me));
  1660. lilv_nodes_free(const_cast<LilvNodes*>(supportNodes.me));
  1661. }
  1662. else if (lilvPort.is_a(lv2World.port_event))
  1663. {
  1664. rdfPort->Types |= LV2_PORT_EVENT;
  1665. bool supported = false;
  1666. if (lilvPort.supports_event(lv2World.midi_event))
  1667. {
  1668. rdfPort->Types |= LV2_PORT_DATA_MIDI_EVENT;
  1669. supported = true;
  1670. }
  1671. if (lilvPort.supports_event(lv2World.patch_message))
  1672. {
  1673. rdfPort->Types |= LV2_PORT_DATA_PATCH_MESSAGE;
  1674. supported = true;
  1675. }
  1676. if (lilvPort.supports_event(lv2World.time_position))
  1677. {
  1678. rdfPort->Types |= LV2_PORT_DATA_TIME_POSITION;
  1679. supported = true;
  1680. }
  1681. if (! supported)
  1682. carla_stderr("lv2_rdf_new(\"%s\") - port '%s' is of event type but has unsupported data", uri, rdfPort->Name);
  1683. }
  1684. else if (lilvPort.is_a(lv2World.port_midi))
  1685. {
  1686. rdfPort->Types |= LV2_PORT_MIDI_LL;
  1687. rdfPort->Types |= LV2_PORT_DATA_MIDI_EVENT;
  1688. }
  1689. else
  1690. carla_stderr("lv2_rdf_new(\"%s\") - port '%s' is of unknown data type", uri, rdfPort->Name);
  1691. }
  1692. // --------------------------------------------------------------------------------------------------------
  1693. // Set Port Properties
  1694. {
  1695. if (lilvPort.has_property(lv2World.pprop_optional))
  1696. rdfPort->Properties |= LV2_PORT_OPTIONAL;
  1697. if (lilvPort.has_property(lv2World.pprop_enumeration))
  1698. rdfPort->Properties |= LV2_PORT_ENUMERATION;
  1699. if (lilvPort.has_property(lv2World.pprop_integer))
  1700. rdfPort->Properties |= LV2_PORT_INTEGER;
  1701. if (lilvPort.has_property(lv2World.pprop_sampleRate))
  1702. rdfPort->Properties |= LV2_PORT_SAMPLE_RATE;
  1703. if (lilvPort.has_property(lv2World.pprop_toggled))
  1704. rdfPort->Properties |= LV2_PORT_TOGGLED;
  1705. if (lilvPort.has_property(lv2World.pprop_artifacts))
  1706. rdfPort->Properties |= LV2_PORT_CAUSES_ARTIFACTS;
  1707. if (lilvPort.has_property(lv2World.pprop_continuousCV))
  1708. rdfPort->Properties |= LV2_PORT_CONTINUOUS_CV;
  1709. if (lilvPort.has_property(lv2World.pprop_discreteCV))
  1710. rdfPort->Properties |= LV2_PORT_DISCRETE_CV;
  1711. if (lilvPort.has_property(lv2World.pprop_expensive))
  1712. rdfPort->Properties |= LV2_PORT_EXPENSIVE;
  1713. if (lilvPort.has_property(lv2World.pprop_strictBounds))
  1714. rdfPort->Properties |= LV2_PORT_STRICT_BOUNDS;
  1715. if (lilvPort.has_property(lv2World.pprop_logarithmic))
  1716. rdfPort->Properties |= LV2_PORT_LOGARITHMIC;
  1717. if (lilvPort.has_property(lv2World.pprop_notAutomatic))
  1718. rdfPort->Properties |= LV2_PORT_NOT_AUTOMATIC;
  1719. if (lilvPort.has_property(lv2World.pprop_notOnGUI))
  1720. rdfPort->Properties |= LV2_PORT_NOT_ON_GUI;
  1721. if (lilvPort.has_property(lv2World.pprop_trigger))
  1722. rdfPort->Properties |= LV2_PORT_TRIGGER;
  1723. if (lilvPort.has_property(lv2World.pprop_nonAutomable))
  1724. rdfPort->Properties |= LV2_PORT_NON_AUTOMABLE;
  1725. if (lilvPort.has_property(lv2World.reportsLatency))
  1726. rdfPort->Designation = LV2_PORT_DESIGNATION_LATENCY;
  1727. // no port properties set, check if using old/invalid ones
  1728. if (rdfPort->Properties == 0x0)
  1729. {
  1730. const Lilv::Node oldPropArtifacts(lv2World.new_uri(NS_devp "causesArtifacts"));
  1731. const Lilv::Node oldPropContinuousCV(lv2World.new_uri(NS_devp "continuousCV"));
  1732. const Lilv::Node oldPropDiscreteCV(lv2World.new_uri(NS_devp "discreteCV"));
  1733. const Lilv::Node oldPropExpensive(lv2World.new_uri(NS_devp "expensive"));
  1734. const Lilv::Node oldPropStrictBounds(lv2World.new_uri(NS_devp "hasStrictBounds"));
  1735. const Lilv::Node oldPropLogarithmic(lv2World.new_uri(NS_devp "logarithmic"));
  1736. const Lilv::Node oldPropNotAutomatic(lv2World.new_uri(NS_devp "notAutomatic"));
  1737. const Lilv::Node oldPropNotOnGUI(lv2World.new_uri(NS_devp "notOnGUI"));
  1738. const Lilv::Node oldPropTrigger(lv2World.new_uri(NS_devp "trigger"));
  1739. if (lilvPort.has_property(oldPropArtifacts))
  1740. {
  1741. rdfPort->Properties |= LV2_PORT_CAUSES_ARTIFACTS;
  1742. carla_stderr("lv2_rdf_new(\"%s\") - port '%s' uses old/invalid LV2 property for 'causesArtifacts'", uri, rdfPort->Name);
  1743. }
  1744. if (lilvPort.has_property(oldPropContinuousCV))
  1745. {
  1746. rdfPort->Properties |= LV2_PORT_CONTINUOUS_CV;
  1747. carla_stderr("lv2_rdf_new(\"%s\") - port '%s' uses old/invalid LV2 property for 'continuousCV'", uri, rdfPort->Name);
  1748. }
  1749. if (lilvPort.has_property(oldPropDiscreteCV))
  1750. {
  1751. rdfPort->Properties |= LV2_PORT_DISCRETE_CV;
  1752. carla_stderr("lv2_rdf_new(\"%s\") - port '%s' uses old/invalid LV2 property for 'discreteCV'", uri, rdfPort->Name);
  1753. }
  1754. if (lilvPort.has_property(oldPropExpensive))
  1755. {
  1756. rdfPort->Properties |= LV2_PORT_EXPENSIVE;
  1757. carla_stderr("lv2_rdf_new(\"%s\") - port '%s' uses old/invalid LV2 property for 'expensive'", uri, rdfPort->Name);
  1758. }
  1759. if (lilvPort.has_property(oldPropStrictBounds))
  1760. {
  1761. rdfPort->Properties |= LV2_PORT_STRICT_BOUNDS;
  1762. carla_stderr("lv2_rdf_new(\"%s\") - port '%s' uses old/invalid LV2 property for 'hasStrictBounds'", uri, rdfPort->Name);
  1763. }
  1764. if (lilvPort.has_property(oldPropLogarithmic))
  1765. {
  1766. rdfPort->Properties |= LV2_PORT_LOGARITHMIC;
  1767. carla_stderr("lv2_rdf_new(\"%s\") - port '%s' uses old/invalid LV2 property for 'logarithmic'", uri, rdfPort->Name);
  1768. }
  1769. if (lilvPort.has_property(oldPropNotAutomatic))
  1770. {
  1771. rdfPort->Properties |= LV2_PORT_NOT_AUTOMATIC;
  1772. carla_stderr("lv2_rdf_new(\"%s\") - port '%s' uses old/invalid LV2 property for 'notAutomatic'", uri, rdfPort->Name);
  1773. }
  1774. if (lilvPort.has_property(oldPropNotOnGUI))
  1775. {
  1776. rdfPort->Properties |= LV2_PORT_NOT_ON_GUI;
  1777. carla_stderr("lv2_rdf_new(\"%s\") - port '%s' uses old/invalid LV2 property for 'notOnGUI'", uri, rdfPort->Name);
  1778. }
  1779. if (lilvPort.has_property(oldPropTrigger))
  1780. {
  1781. rdfPort->Properties |= LV2_PORT_TRIGGER;
  1782. carla_stderr("lv2_rdf_new(\"%s\") - port '%s' uses old/invalid LV2 property for 'trigger'", uri, rdfPort->Name);
  1783. }
  1784. }
  1785. }
  1786. // --------------------------------------------------------------------------------------------------------
  1787. // Set Port Designation
  1788. {
  1789. if (LilvNode* const designationNode = lilv_port_get(lilvPort.parent, lilvPort.me, lv2World.designation.me))
  1790. {
  1791. if (const char* const designation = lilv_node_as_string(designationNode))
  1792. {
  1793. /**/ if (std::strcmp(designation, LV2_CORE__control) == 0)
  1794. rdfPort->Designation = LV2_PORT_DESIGNATION_CONTROL;
  1795. else if (std::strcmp(designation, LV2_CORE__enabled) == 0)
  1796. rdfPort->Designation = LV2_PORT_DESIGNATION_ENABLED;
  1797. else if (std::strcmp(designation, LV2_CORE__freeWheeling) == 0)
  1798. rdfPort->Designation = LV2_PORT_DESIGNATION_FREEWHEELING;
  1799. else if (std::strcmp(designation, LV2_CORE__latency) == 0)
  1800. rdfPort->Designation = LV2_PORT_DESIGNATION_LATENCY;
  1801. else if (std::strcmp(designation, LV2_PARAMETERS__sampleRate) == 0)
  1802. rdfPort->Designation = LV2_PORT_DESIGNATION_SAMPLE_RATE;
  1803. else if (std::strcmp(designation, LV2_TIME__bar) == 0)
  1804. rdfPort->Designation = LV2_PORT_DESIGNATION_TIME_BAR;
  1805. else if (std::strcmp(designation, LV2_TIME__barBeat) == 0)
  1806. rdfPort->Designation = LV2_PORT_DESIGNATION_TIME_BAR_BEAT;
  1807. else if (std::strcmp(designation, LV2_TIME__beat) == 0)
  1808. rdfPort->Designation = LV2_PORT_DESIGNATION_TIME_BEAT;
  1809. else if (std::strcmp(designation, LV2_TIME__beatUnit) == 0)
  1810. rdfPort->Designation = LV2_PORT_DESIGNATION_TIME_BEAT_UNIT;
  1811. else if (std::strcmp(designation, LV2_TIME__beatsPerBar) == 0)
  1812. rdfPort->Designation = LV2_PORT_DESIGNATION_TIME_BEATS_PER_BAR;
  1813. else if (std::strcmp(designation, LV2_TIME__beatsPerMinute) == 0)
  1814. rdfPort->Designation = LV2_PORT_DESIGNATION_TIME_BEATS_PER_MINUTE;
  1815. else if (std::strcmp(designation, LV2_TIME__frame) == 0)
  1816. rdfPort->Designation = LV2_PORT_DESIGNATION_TIME_FRAME;
  1817. else if (std::strcmp(designation, LV2_TIME__framesPerSecond) == 0)
  1818. rdfPort->Designation = LV2_PORT_DESIGNATION_TIME_FRAMES_PER_SECOND;
  1819. else if (std::strcmp(designation, LV2_TIME__speed) == 0)
  1820. rdfPort->Designation = LV2_PORT_DESIGNATION_TIME_SPEED;
  1821. else if (std::strcmp(designation, LV2_KXSTUDIO_PROPERTIES__TimePositionTicksPerBeat) == 0)
  1822. rdfPort->Designation = LV2_PORT_DESIGNATION_TIME_TICKS_PER_BEAT;
  1823. else if (std::strncmp(designation, LV2_PARAMETERS_PREFIX, std::strlen(LV2_PARAMETERS_PREFIX)) == 0)
  1824. pass();
  1825. else if (std::strncmp(designation, LV2_PORT_GROUPS_PREFIX, std::strlen(LV2_PORT_GROUPS_PREFIX)) == 0)
  1826. pass();
  1827. else
  1828. carla_stderr("lv2_rdf_new(\"%s\") - got unknown port designation '%s'", uri, designation);
  1829. }
  1830. lilv_node_free(designationNode);
  1831. }
  1832. }
  1833. // --------------------------------------------------------------------------------------------------------
  1834. // Set Port MIDI Map
  1835. {
  1836. if (LilvNode* const midiMapNode = lilv_port_get(lilvPort.parent, lilvPort.me, lv2World.mm_defaultControl.me))
  1837. {
  1838. if (lilv_node_is_blank(midiMapNode))
  1839. {
  1840. Lilv::Nodes midiMapTypeNodes(lv2World.find_nodes(midiMapNode, lv2World.mm_controlType, nullptr));
  1841. Lilv::Nodes midiMapNumberNodes(lv2World.find_nodes(midiMapNode, lv2World.mm_controlNumber, nullptr));
  1842. if (midiMapTypeNodes.size() == 1 && midiMapNumberNodes.size() == 1)
  1843. {
  1844. if (const char* const midiMapType = midiMapTypeNodes.get_first().as_string())
  1845. {
  1846. /**/ if (std::strcmp(midiMapType, LV2_MIDI_Map__CC) == 0)
  1847. rdfPort->MidiMap.Type = LV2_PORT_MIDI_MAP_CC;
  1848. else if (std::strcmp(midiMapType, LV2_MIDI_Map__NRPN) == 0)
  1849. rdfPort->MidiMap.Type = LV2_PORT_MIDI_MAP_NRPN;
  1850. else
  1851. carla_stderr("lv2_rdf_new(\"%s\") - got unknown port Midi-Map type '%s'", uri, midiMapType);
  1852. rdfPort->MidiMap.Number = static_cast<uint32_t>(midiMapNumberNodes.get_first().as_int());
  1853. }
  1854. }
  1855. lilv_nodes_free(const_cast<LilvNodes*>(midiMapTypeNodes.me));
  1856. lilv_nodes_free(const_cast<LilvNodes*>(midiMapNumberNodes.me));
  1857. }
  1858. lilv_node_free(midiMapNode);
  1859. }
  1860. // TODO - also check using new official MIDI API too
  1861. }
  1862. // --------------------------------------------------------------------------------------------------------
  1863. // Set Port Points
  1864. {
  1865. if (LilvNode* const defNode = lilv_port_get(lilvPort.parent, lilvPort.me, lv2World.value_default.me))
  1866. {
  1867. rdfPort->Points.Hints |= LV2_PORT_POINT_DEFAULT;
  1868. rdfPort->Points.Default = lilv_node_as_float(defNode);
  1869. lilv_node_free(defNode);
  1870. }
  1871. if (LilvNode* const minNode = lilv_port_get(lilvPort.parent, lilvPort.me, lv2World.value_minimum.me))
  1872. {
  1873. rdfPort->Points.Hints |= LV2_PORT_POINT_MINIMUM;
  1874. rdfPort->Points.Minimum = lilv_node_as_float(minNode);
  1875. lilv_node_free(minNode);
  1876. }
  1877. if (LilvNode* const maxNode = lilv_port_get(lilvPort.parent, lilvPort.me, lv2World.value_maximum.me))
  1878. {
  1879. rdfPort->Points.Hints |= LV2_PORT_POINT_MAXIMUM;
  1880. rdfPort->Points.Maximum = lilv_node_as_float(maxNode);
  1881. lilv_node_free(maxNode);
  1882. }
  1883. }
  1884. // --------------------------------------------------------------------------------------------------------
  1885. // Set Port Unit
  1886. {
  1887. if (LilvNode* const unitUnitNode = lilv_port_get(lilvPort.parent, lilvPort.me, lv2World.unit_unit.me))
  1888. {
  1889. if (lilv_node_is_uri(unitUnitNode))
  1890. {
  1891. if (const char* const unitUnit = lilv_node_as_uri(unitUnitNode))
  1892. {
  1893. rdfPort->Unit.Hints |= LV2_PORT_UNIT_UNIT;
  1894. /**/ if (std::strcmp(unitUnit, LV2_UNITS__bar) == 0)
  1895. rdfPort->Unit.Unit = LV2_PORT_UNIT_BAR;
  1896. else if (std::strcmp(unitUnit, LV2_UNITS__beat) == 0)
  1897. rdfPort->Unit.Unit = LV2_PORT_UNIT_BEAT;
  1898. else if (std::strcmp(unitUnit, LV2_UNITS__bpm) == 0)
  1899. rdfPort->Unit.Unit = LV2_PORT_UNIT_BPM;
  1900. else if (std::strcmp(unitUnit, LV2_UNITS__cent) == 0)
  1901. rdfPort->Unit.Unit = LV2_PORT_UNIT_CENT;
  1902. else if (std::strcmp(unitUnit, LV2_UNITS__cm) == 0)
  1903. rdfPort->Unit.Unit = LV2_PORT_UNIT_CM;
  1904. else if (std::strcmp(unitUnit, LV2_UNITS__coef) == 0)
  1905. rdfPort->Unit.Unit = LV2_PORT_UNIT_COEF;
  1906. else if (std::strcmp(unitUnit, LV2_UNITS__db) == 0)
  1907. rdfPort->Unit.Unit = LV2_PORT_UNIT_DB;
  1908. else if (std::strcmp(unitUnit, LV2_UNITS__degree) == 0)
  1909. rdfPort->Unit.Unit = LV2_PORT_UNIT_DEGREE;
  1910. else if (std::strcmp(unitUnit, LV2_UNITS__frame) == 0)
  1911. rdfPort->Unit.Unit = LV2_PORT_UNIT_FRAME;
  1912. else if (std::strcmp(unitUnit, LV2_UNITS__hz) == 0)
  1913. rdfPort->Unit.Unit = LV2_PORT_UNIT_HZ;
  1914. else if (std::strcmp(unitUnit, LV2_UNITS__inch) == 0)
  1915. rdfPort->Unit.Unit = LV2_PORT_UNIT_INCH;
  1916. else if (std::strcmp(unitUnit, LV2_UNITS__khz) == 0)
  1917. rdfPort->Unit.Unit = LV2_PORT_UNIT_KHZ;
  1918. else if (std::strcmp(unitUnit, LV2_UNITS__km) == 0)
  1919. rdfPort->Unit.Unit = LV2_PORT_UNIT_KM;
  1920. else if (std::strcmp(unitUnit, LV2_UNITS__m) == 0)
  1921. rdfPort->Unit.Unit = LV2_PORT_UNIT_M;
  1922. else if (std::strcmp(unitUnit, LV2_UNITS__mhz) == 0)
  1923. rdfPort->Unit.Unit = LV2_PORT_UNIT_MHZ;
  1924. else if (std::strcmp(unitUnit, LV2_UNITS__midiNote) == 0)
  1925. rdfPort->Unit.Unit = LV2_PORT_UNIT_MIDINOTE;
  1926. else if (std::strcmp(unitUnit, LV2_UNITS__mile) == 0)
  1927. rdfPort->Unit.Unit = LV2_PORT_UNIT_MILE;
  1928. else if (std::strcmp(unitUnit, LV2_UNITS__min) == 0)
  1929. rdfPort->Unit.Unit = LV2_PORT_UNIT_MIN;
  1930. else if (std::strcmp(unitUnit, LV2_UNITS__mm) == 0)
  1931. rdfPort->Unit.Unit = LV2_PORT_UNIT_MM;
  1932. else if (std::strcmp(unitUnit, LV2_UNITS__ms) == 0)
  1933. rdfPort->Unit.Unit = LV2_PORT_UNIT_MS;
  1934. else if (std::strcmp(unitUnit, LV2_UNITS__oct) == 0)
  1935. rdfPort->Unit.Unit = LV2_PORT_UNIT_OCT;
  1936. else if (std::strcmp(unitUnit, LV2_UNITS__pc) == 0)
  1937. rdfPort->Unit.Unit = LV2_PORT_UNIT_PC;
  1938. else if (std::strcmp(unitUnit, LV2_UNITS__s) == 0)
  1939. rdfPort->Unit.Unit = LV2_PORT_UNIT_S;
  1940. else if (std::strcmp(unitUnit, LV2_UNITS__semitone12TET) == 0)
  1941. rdfPort->Unit.Unit = LV2_PORT_UNIT_SEMITONE;
  1942. else
  1943. carla_stderr("lv2_rdf_new(\"%s\") - got unknown unit '%s'", uri, unitUnit);
  1944. }
  1945. }
  1946. if (LilvNode* const unitNameNode = lilv_world_get(lv2World.me, unitUnitNode, lv2World.unit_name.me, nullptr))
  1947. {
  1948. if (const char* const unitName = lilv_node_as_string(unitNameNode))
  1949. {
  1950. rdfPort->Unit.Hints |= LV2_PORT_UNIT_NAME;
  1951. rdfPort->Unit.Name = carla_strdup(unitName);
  1952. }
  1953. lilv_node_free(unitNameNode);
  1954. }
  1955. if (LilvNode* const unitRenderNode = lilv_world_get(lv2World.me, unitUnitNode, lv2World.unit_render.me, nullptr))
  1956. {
  1957. if (const char* const unitRender = lilv_node_as_string(unitRenderNode))
  1958. {
  1959. rdfPort->Unit.Hints |= LV2_PORT_UNIT_RENDER;
  1960. rdfPort->Unit.Render = carla_strdup(unitRender);
  1961. }
  1962. lilv_node_free(unitRenderNode);
  1963. }
  1964. if (LilvNode* const unitSymbolNode = lilv_world_get(lv2World.me, unitUnitNode, lv2World.unit_symbol.me, nullptr))
  1965. {
  1966. if (const char* const unitSymbol = lilv_node_as_string(unitSymbolNode))
  1967. {
  1968. rdfPort->Unit.Hints |= LV2_PORT_UNIT_SYMBOL;
  1969. rdfPort->Unit.Symbol = carla_strdup(unitSymbol);
  1970. }
  1971. lilv_node_free(unitSymbolNode);
  1972. }
  1973. lilv_node_free(unitUnitNode);
  1974. }
  1975. }
  1976. // --------------------------------------------------------------------------------------------------------
  1977. // Set Port Minimum Size
  1978. {
  1979. if (LilvNode* const minimumSizeNode = lilv_port_get(lilvPort.parent, lilvPort.me, lv2World.rz_minSize.me))
  1980. {
  1981. const int minimumSize(lilv_node_as_int(minimumSizeNode));
  1982. if (minimumSize > 0)
  1983. rdfPort->MinimumSize = static_cast<uint32_t>(minimumSize);
  1984. else
  1985. carla_safe_assert_int("minimumSize > 0", __FILE__, __LINE__, minimumSize);
  1986. lilv_node_free(minimumSizeNode);
  1987. }
  1988. }
  1989. // --------------------------------------------------------------------------------------------------------
  1990. // Set Port Scale Points
  1991. {
  1992. Lilv::ScalePoints lilvScalePoints(lilvPort.get_scale_points());
  1993. if (const uint numScalePoints = lilvScalePoints.size())
  1994. {
  1995. rdfPort->ScalePoints = new LV2_RDF_PortScalePoint[numScalePoints];
  1996. // get all scalepoints and sort them by value
  1997. LilvScalePointMap sortedpoints;
  1998. LILV_FOREACH(scale_points, it, lilvScalePoints)
  1999. {
  2000. Lilv::ScalePoint lilvScalePoint(lilvScalePoints.get(it));
  2001. CARLA_SAFE_ASSERT_CONTINUE(lilvScalePoint.get_label() != nullptr);
  2002. if (const LilvNode* const valuenode = lilvScalePoint.get_value())
  2003. {
  2004. const double valueid = lilv_node_as_float(valuenode);
  2005. sortedpoints[valueid] = lilvScalePoint.me;
  2006. }
  2007. }
  2008. // now safe to store, sorted by using std::map
  2009. uint numUsed = 0;
  2010. for (LilvScalePointMap::iterator it=sortedpoints.begin(), end=sortedpoints.end(); it != end; ++it)
  2011. {
  2012. CARLA_SAFE_ASSERT_BREAK(numUsed < numScalePoints);
  2013. LV2_RDF_PortScalePoint* const rdfScalePoint(&rdfPort->ScalePoints[numUsed++]);
  2014. const LilvScalePoint* const scalepoint = it->second;
  2015. const LilvNode* const xlabel = lilv_scale_point_get_label(scalepoint);
  2016. const LilvNode* const xvalue = lilv_scale_point_get_value(scalepoint);
  2017. rdfScalePoint->Label = carla_strdup(lilv_node_as_string(xlabel));
  2018. rdfScalePoint->Value = lilv_node_as_float(xvalue);
  2019. }
  2020. rdfPort->ScalePointCount = numUsed;
  2021. }
  2022. lilv_nodes_free(const_cast<LilvNodes*>(lilvScalePoints.me));
  2023. }
  2024. }
  2025. }
  2026. // ----------------------------------------------------------------------------------------------------------------
  2027. // Set Plugin Parameters
  2028. {
  2029. std::map<std::string, LV2_RDF_Parameter> parameters;
  2030. Lilv::Nodes patchReadableNodes(lilvPlugin.get_value(lv2World.patch_readable));
  2031. Lilv::Nodes patchWritableNodes(lilvPlugin.get_value(lv2World.patch_writable));
  2032. if (const uint numParameters = patchWritableNodes.size())
  2033. {
  2034. uint numUsed = 0;
  2035. LILV_FOREACH(nodes, it, patchWritableNodes)
  2036. {
  2037. CARLA_SAFE_ASSERT_BREAK(numUsed < numParameters);
  2038. Lilv::Node patchWritableNode(patchWritableNodes.get(it));
  2039. if (LilvNode* const typeNode = lilv_world_get(lv2World.me, patchWritableNode,
  2040. lv2World.rdf_type.me, nullptr))
  2041. {
  2042. const char* const type = lilv_node_as_uri(typeNode);
  2043. if (std::strcmp(type, LV2_CORE__Parameter) != 0)
  2044. {
  2045. lilv_node_free(typeNode);
  2046. continue;
  2047. }
  2048. lilv_node_free(typeNode);
  2049. }
  2050. else
  2051. {
  2052. continue;
  2053. }
  2054. CARLA_SAFE_ASSERT_CONTINUE(patchWritableNode.is_uri());
  2055. ++numUsed;
  2056. LV2_RDF_Parameter rdfParam;
  2057. rdfParam.URI = carla_strdup(patchWritableNode.as_uri());
  2058. rdfParam.Flags = LV2_PARAMETER_FLAG_INPUT;
  2059. if (patchReadableNodes.contains(patchWritableNode))
  2060. rdfParam.Flags |= LV2_PARAMETER_FLAG_OUTPUT;
  2061. // ----------------------------------------------------------------------------------------------------
  2062. // Set Basics
  2063. if (LilvNode* const rangeNode = lilv_world_get(lv2World.me, patchWritableNode,
  2064. lv2World.rdfs_range.me, nullptr))
  2065. {
  2066. const char* const rangeURI = lilv_node_as_string(rangeNode);
  2067. /**/ if (std::strcmp(rangeURI, LV2_ATOM__Bool) == 0)
  2068. rdfParam.Type = LV2_PARAMETER_TYPE_BOOL;
  2069. else if (std::strcmp(rangeURI, LV2_ATOM__Int) == 0)
  2070. rdfParam.Type = LV2_PARAMETER_TYPE_INT;
  2071. else if (std::strcmp(rangeURI, LV2_ATOM__Long) == 0)
  2072. rdfParam.Type = LV2_PARAMETER_TYPE_LONG;
  2073. else if (std::strcmp(rangeURI, LV2_ATOM__Float) == 0)
  2074. rdfParam.Type = LV2_PARAMETER_TYPE_FLOAT;
  2075. else if (std::strcmp(rangeURI, LV2_ATOM__Double) == 0)
  2076. rdfParam.Type = LV2_PARAMETER_TYPE_DOUBLE;
  2077. else if (std::strcmp(rangeURI, LV2_ATOM__Path) == 0)
  2078. rdfParam.Type = LV2_PARAMETER_TYPE_PATH;
  2079. else if (std::strcmp(rangeURI, LV2_ATOM__String) == 0)
  2080. rdfParam.Type = LV2_PARAMETER_TYPE_STRING;
  2081. else
  2082. carla_stderr("lv2_rdf_new(\"%s\") - got unknown parameter type '%s'", uri, rangeURI);
  2083. lilv_node_free(rangeNode);
  2084. }
  2085. if (LilvNode* const labelNode = lilv_world_get(lv2World.me, patchWritableNode,
  2086. lv2World.rdfs_label.me, nullptr))
  2087. {
  2088. rdfParam.Label = carla_strdup(lilv_node_as_string(labelNode));
  2089. lilv_node_free(labelNode);
  2090. }
  2091. if (LilvNode* const commentNode = lilv_world_get(lv2World.me, patchWritableNode,
  2092. lv2World.rdfs_comment.me, nullptr))
  2093. {
  2094. rdfParam.Comment = carla_strdup_safe(lilv_node_as_string(commentNode));
  2095. lilv_node_free(commentNode);
  2096. }
  2097. if (LilvNode* const groupNode = lilv_world_get(lv2World.me, patchWritableNode,
  2098. lv2World.pg_group.me, nullptr))
  2099. {
  2100. rdfParam.GroupURI = carla_strdup_safe(lilv_node_as_uri(groupNode));
  2101. if (portGroupURIs.appendUnique(rdfParam.GroupURI))
  2102. portGroupNodes.append(groupNode);
  2103. else
  2104. lilv_node_free(groupNode);
  2105. }
  2106. // ----------------------------------------------------------------------------------------------------
  2107. // Set Port Points
  2108. if (LilvNode* const defNode = lilv_world_get(lv2World.me, patchWritableNode,
  2109. lv2World.value_default.me, nullptr))
  2110. {
  2111. rdfParam.Points.Hints |= LV2_PORT_POINT_DEFAULT;
  2112. rdfParam.Points.Default = lilv_node_as_float(defNode);
  2113. lilv_node_free(defNode);
  2114. }
  2115. if (LilvNode* const minNode = lilv_world_get(lv2World.me, patchWritableNode,
  2116. lv2World.value_minimum.me, nullptr))
  2117. {
  2118. rdfParam.Points.Hints |= LV2_PORT_POINT_MINIMUM;
  2119. rdfParam.Points.Minimum = lilv_node_as_float(minNode);
  2120. lilv_node_free(minNode);
  2121. }
  2122. if (LilvNode* const maxNode = lilv_world_get(lv2World.me, patchWritableNode,
  2123. lv2World.value_maximum.me, nullptr))
  2124. {
  2125. rdfParam.Points.Hints |= LV2_PORT_POINT_MAXIMUM;
  2126. rdfParam.Points.Maximum = lilv_node_as_float(maxNode);
  2127. lilv_node_free(maxNode);
  2128. }
  2129. // ----------------------------------------------------------------------------------------------------
  2130. // Set Port Unit
  2131. if (LilvNode* const unitUnitNode = lilv_world_get(lv2World.me, patchWritableNode,
  2132. lv2World.unit_unit.me, nullptr))
  2133. {
  2134. if (lilv_node_is_uri(unitUnitNode))
  2135. {
  2136. if (const char* const unitUnit = lilv_node_as_uri(unitUnitNode))
  2137. {
  2138. rdfParam.Unit.Hints |= LV2_PORT_UNIT_UNIT;
  2139. /**/ if (std::strcmp(unitUnit, LV2_UNITS__bar) == 0)
  2140. rdfParam.Unit.Unit = LV2_PORT_UNIT_BAR;
  2141. else if (std::strcmp(unitUnit, LV2_UNITS__beat) == 0)
  2142. rdfParam.Unit.Unit = LV2_PORT_UNIT_BEAT;
  2143. else if (std::strcmp(unitUnit, LV2_UNITS__bpm) == 0)
  2144. rdfParam.Unit.Unit = LV2_PORT_UNIT_BPM;
  2145. else if (std::strcmp(unitUnit, LV2_UNITS__cent) == 0)
  2146. rdfParam.Unit.Unit = LV2_PORT_UNIT_CENT;
  2147. else if (std::strcmp(unitUnit, LV2_UNITS__cm) == 0)
  2148. rdfParam.Unit.Unit = LV2_PORT_UNIT_CM;
  2149. else if (std::strcmp(unitUnit, LV2_UNITS__coef) == 0)
  2150. rdfParam.Unit.Unit = LV2_PORT_UNIT_COEF;
  2151. else if (std::strcmp(unitUnit, LV2_UNITS__db) == 0)
  2152. rdfParam.Unit.Unit = LV2_PORT_UNIT_DB;
  2153. else if (std::strcmp(unitUnit, LV2_UNITS__degree) == 0)
  2154. rdfParam.Unit.Unit = LV2_PORT_UNIT_DEGREE;
  2155. else if (std::strcmp(unitUnit, LV2_UNITS__frame) == 0)
  2156. rdfParam.Unit.Unit = LV2_PORT_UNIT_FRAME;
  2157. else if (std::strcmp(unitUnit, LV2_UNITS__hz) == 0)
  2158. rdfParam.Unit.Unit = LV2_PORT_UNIT_HZ;
  2159. else if (std::strcmp(unitUnit, LV2_UNITS__inch) == 0)
  2160. rdfParam.Unit.Unit = LV2_PORT_UNIT_INCH;
  2161. else if (std::strcmp(unitUnit, LV2_UNITS__khz) == 0)
  2162. rdfParam.Unit.Unit = LV2_PORT_UNIT_KHZ;
  2163. else if (std::strcmp(unitUnit, LV2_UNITS__km) == 0)
  2164. rdfParam.Unit.Unit = LV2_PORT_UNIT_KM;
  2165. else if (std::strcmp(unitUnit, LV2_UNITS__m) == 0)
  2166. rdfParam.Unit.Unit = LV2_PORT_UNIT_M;
  2167. else if (std::strcmp(unitUnit, LV2_UNITS__mhz) == 0)
  2168. rdfParam.Unit.Unit = LV2_PORT_UNIT_MHZ;
  2169. else if (std::strcmp(unitUnit, LV2_UNITS__midiNote) == 0)
  2170. rdfParam.Unit.Unit = LV2_PORT_UNIT_MIDINOTE;
  2171. else if (std::strcmp(unitUnit, LV2_UNITS__mile) == 0)
  2172. rdfParam.Unit.Unit = LV2_PORT_UNIT_MILE;
  2173. else if (std::strcmp(unitUnit, LV2_UNITS__min) == 0)
  2174. rdfParam.Unit.Unit = LV2_PORT_UNIT_MIN;
  2175. else if (std::strcmp(unitUnit, LV2_UNITS__mm) == 0)
  2176. rdfParam.Unit.Unit = LV2_PORT_UNIT_MM;
  2177. else if (std::strcmp(unitUnit, LV2_UNITS__ms) == 0)
  2178. rdfParam.Unit.Unit = LV2_PORT_UNIT_MS;
  2179. else if (std::strcmp(unitUnit, LV2_UNITS__oct) == 0)
  2180. rdfParam.Unit.Unit = LV2_PORT_UNIT_OCT;
  2181. else if (std::strcmp(unitUnit, LV2_UNITS__pc) == 0)
  2182. rdfParam.Unit.Unit = LV2_PORT_UNIT_PC;
  2183. else if (std::strcmp(unitUnit, LV2_UNITS__s) == 0)
  2184. rdfParam.Unit.Unit = LV2_PORT_UNIT_S;
  2185. else if (std::strcmp(unitUnit, LV2_UNITS__semitone12TET) == 0)
  2186. rdfParam.Unit.Unit = LV2_PORT_UNIT_SEMITONE;
  2187. else
  2188. carla_stderr("lv2_rdf_new(\"%s\") - got unknown unit '%s'", uri, unitUnit);
  2189. }
  2190. }
  2191. if (LilvNode* const unitNameNode = lilv_world_get(lv2World.me, unitUnitNode,
  2192. lv2World.unit_name.me, nullptr))
  2193. {
  2194. if (const char* const unitName = lilv_node_as_string(unitNameNode))
  2195. {
  2196. rdfParam.Unit.Hints |= LV2_PORT_UNIT_NAME;
  2197. rdfParam.Unit.Name = carla_strdup(unitName);
  2198. }
  2199. lilv_node_free(unitNameNode);
  2200. }
  2201. if (LilvNode* const unitRenderNode = lilv_world_get(lv2World.me, unitUnitNode,
  2202. lv2World.unit_render.me, nullptr))
  2203. {
  2204. if (const char* const unitRender = lilv_node_as_string(unitRenderNode))
  2205. {
  2206. rdfParam.Unit.Hints |= LV2_PORT_UNIT_RENDER;
  2207. rdfParam.Unit.Render = carla_strdup(unitRender);
  2208. }
  2209. lilv_node_free(unitRenderNode);
  2210. }
  2211. if (LilvNode* const unitSymbolNode = lilv_world_get(lv2World.me, unitUnitNode,
  2212. lv2World.unit_symbol.me, nullptr))
  2213. {
  2214. if (const char* const unitSymbol = lilv_node_as_string(unitSymbolNode))
  2215. {
  2216. rdfParam.Unit.Hints |= LV2_PORT_UNIT_SYMBOL;
  2217. rdfParam.Unit.Symbol = carla_strdup(unitSymbol);
  2218. }
  2219. lilv_node_free(unitSymbolNode);
  2220. }
  2221. lilv_node_free(unitUnitNode);
  2222. }
  2223. parameters[rdfParam.URI].copyAndReplace(rdfParam);
  2224. }
  2225. CARLA_SAFE_ASSERT_UINT2(parameters.size() == numUsed, parameters.size(), numUsed);
  2226. rdfDescriptor->Parameters = new LV2_RDF_Parameter[numUsed];
  2227. rdfDescriptor->ParameterCount = numUsed;
  2228. numUsed = 0;
  2229. for (std::map<std::string, LV2_RDF_Parameter>::iterator it = parameters.begin(), end = parameters.end();
  2230. it != end; ++it)
  2231. {
  2232. rdfDescriptor->Parameters[numUsed++].copyAndReplace(it->second);
  2233. }
  2234. }
  2235. lilv_nodes_free(const_cast<LilvNodes*>(patchReadableNodes.me));
  2236. lilv_nodes_free(const_cast<LilvNodes*>(patchWritableNodes.me));
  2237. }
  2238. // ----------------------------------------------------------------------------------------------------------------
  2239. // Set Plugin Port Groups
  2240. if (const size_t portGroupCount = portGroupURIs.count())
  2241. {
  2242. rdfDescriptor->PortGroups = new LV2_RDF_PortGroup[portGroupCount];
  2243. uint32_t count = 0;
  2244. CarlaStringList::Itenerator itu = portGroupURIs.begin2();
  2245. LinkedList<LilvNode*>::Itenerator itn = portGroupNodes.begin2();
  2246. for (; itu.valid() && itn.valid(); itu.next(), itn.next())
  2247. {
  2248. const char* const portGroupURI = itu.getValue(nullptr);
  2249. CARLA_SAFE_ASSERT_CONTINUE(portGroupURI != nullptr);
  2250. LilvNode* const portGroupNode = itn.getValue(nullptr);
  2251. CARLA_SAFE_ASSERT_CONTINUE(portGroupNode != nullptr);
  2252. LV2_RDF_PortGroup& portGroup(rdfDescriptor->PortGroups[count]);
  2253. portGroup.URI = portGroupURI;
  2254. if (LilvNode* const portGroupNameNode = lilv_world_get(lv2World.me, portGroupNode,
  2255. lv2World.lv2_name.me, nullptr))
  2256. {
  2257. portGroup.Name = carla_strdup_safe(lilv_node_as_string(portGroupNameNode));
  2258. lilv_node_free(portGroupNameNode);
  2259. }
  2260. // some plugins use rdfs:label, spec was not clear which one to use
  2261. else if (LilvNode* const portGroupLabelNode = lilv_world_get(lv2World.me, portGroupNode,
  2262. lv2World.rdfs_label.me, nullptr))
  2263. {
  2264. portGroup.Name = carla_strdup_safe(lilv_node_as_string(portGroupLabelNode));
  2265. lilv_node_free(portGroupLabelNode);
  2266. }
  2267. if (LilvNode* const portGroupSymbolNode = lilv_world_get(lv2World.me, portGroupNode,
  2268. lv2World.lv2_symbol.me, nullptr))
  2269. {
  2270. portGroup.Symbol = carla_strdup_safe(lilv_node_as_string(portGroupSymbolNode));
  2271. lilv_node_free(portGroupSymbolNode);
  2272. }
  2273. ++count;
  2274. lilv_node_free(portGroupNode);
  2275. }
  2276. rdfDescriptor->PortGroupCount = count;
  2277. portGroupNodes.clear();
  2278. }
  2279. // ----------------------------------------------------------------------------------------------------------------
  2280. // Set Plugin Presets
  2281. if (loadPresets)
  2282. {
  2283. Lilv::Nodes presetNodes(lilvPlugin.get_related(lv2World.preset_preset));
  2284. if (presetNodes.size() > 0)
  2285. {
  2286. // create a list of preset URIs (for sorting and unique-ness)
  2287. #ifdef USE_QT
  2288. QStringList presetListURIs;
  2289. LILV_FOREACH(nodes, it, presetNodes)
  2290. {
  2291. Lilv::Node presetNode(presetNodes.get(it));
  2292. QString presetURI(presetNode.as_uri());
  2293. if (! (presetURI.trimmed().isEmpty() || presetListURIs.contains(presetURI)))
  2294. presetListURIs.append(presetURI);
  2295. }
  2296. presetListURIs.sort();
  2297. rdfDescriptor->PresetCount = static_cast<uint32_t>(presetListURIs.count());
  2298. #else
  2299. water::StringArray presetListURIs;
  2300. LILV_FOREACH(nodes, it, presetNodes)
  2301. {
  2302. Lilv::Node presetNode(presetNodes.get(it));
  2303. water::String presetURI(presetNode.as_uri());
  2304. if (presetURI.trim().isNotEmpty())
  2305. presetListURIs.addIfNotAlreadyThere(presetURI);
  2306. }
  2307. presetListURIs.sortNatural();
  2308. rdfDescriptor->PresetCount = static_cast<uint32_t>(presetListURIs.size());
  2309. #endif
  2310. // create presets with unique URIs
  2311. rdfDescriptor->Presets = new LV2_RDF_Preset[rdfDescriptor->PresetCount];
  2312. // set preset data
  2313. LILV_FOREACH(nodes, it, presetNodes)
  2314. {
  2315. Lilv::Node presetNode(presetNodes.get(it));
  2316. const char* const presetURI(presetNode.as_uri());
  2317. CARLA_SAFE_ASSERT_CONTINUE(presetURI != nullptr && presetURI[0] != '\0');
  2318. // try to find label without loading the preset resource first
  2319. Lilv::Nodes presetLabelNodes(lv2World.find_nodes(presetNode, lv2World.rdfs_label, nullptr));
  2320. // failed, try loading resource
  2321. if (presetLabelNodes.size() == 0)
  2322. {
  2323. // if loading resource fails, skip this preset
  2324. if (lv2World.load_resource(presetNode) == -1)
  2325. continue;
  2326. // ok, let's try again
  2327. presetLabelNodes = lv2World.find_nodes(presetNode, lv2World.rdfs_label, nullptr);
  2328. }
  2329. if (presetLabelNodes.size() > 0)
  2330. {
  2331. #ifdef USE_QT
  2332. const int index(presetListURIs.indexOf(QString(presetURI)));
  2333. #else
  2334. const int index(presetListURIs.indexOf(water::String(presetURI)));
  2335. #endif
  2336. CARLA_SAFE_ASSERT_CONTINUE(index >= 0 && index < static_cast<int>(rdfDescriptor->PresetCount));
  2337. LV2_RDF_Preset* const rdfPreset(&rdfDescriptor->Presets[index]);
  2338. // ---------------------------------------------------
  2339. // Set Preset Information
  2340. rdfPreset->URI = carla_strdup(presetURI);
  2341. if (const char* const label = presetLabelNodes.get_first().as_string())
  2342. rdfPreset->Label = carla_strdup(label);
  2343. lilv_nodes_free(const_cast<LilvNodes*>(presetLabelNodes.me));
  2344. }
  2345. }
  2346. }
  2347. lilv_nodes_free(const_cast<LilvNodes*>(presetNodes.me));
  2348. }
  2349. // ----------------------------------------------------------------------------------------------------------------
  2350. // Set Plugin Features
  2351. {
  2352. Lilv::Nodes lilvFeatureNodes(lilvPlugin.get_supported_features());
  2353. if (const uint numFeatures = lilvFeatureNodes.size())
  2354. {
  2355. Lilv::Nodes lilvFeatureNodesR(lilvPlugin.get_required_features());
  2356. rdfDescriptor->Features = new LV2_RDF_Feature[numFeatures];
  2357. uint numUsed = 0;
  2358. LILV_FOREACH(nodes, it, lilvFeatureNodes)
  2359. {
  2360. CARLA_SAFE_ASSERT_BREAK(numUsed < numFeatures);
  2361. Lilv::Node lilvFeatureNode(lilvFeatureNodes.get(it));
  2362. LV2_RDF_Feature* const rdfFeature(&rdfDescriptor->Features[numUsed++]);
  2363. rdfFeature->Required = lilvFeatureNodesR.contains(lilvFeatureNode);
  2364. if (const char* const featureURI = lilvFeatureNode.as_uri())
  2365. rdfFeature->URI = carla_strdup(featureURI);
  2366. else
  2367. rdfFeature->URI = nullptr;
  2368. }
  2369. rdfDescriptor->FeatureCount = numUsed;
  2370. lilv_nodes_free(const_cast<LilvNodes*>(lilvFeatureNodesR.me));
  2371. }
  2372. lilv_nodes_free(const_cast<LilvNodes*>(lilvFeatureNodes.me));
  2373. }
  2374. // ----------------------------------------------------------------------------------------------------------------
  2375. // Set Plugin Extensions
  2376. {
  2377. Lilv::Nodes lilvExtensionDataNodes(lilvPlugin.get_extension_data());
  2378. if (const uint numExtensions = lilvExtensionDataNodes.size())
  2379. {
  2380. rdfDescriptor->Extensions = new LV2_URI[numExtensions];
  2381. uint numUsed = 0;
  2382. LILV_FOREACH(nodes, it, lilvExtensionDataNodes)
  2383. {
  2384. CARLA_SAFE_ASSERT_BREAK(numUsed < numExtensions);
  2385. Lilv::Node lilvExtensionDataNode(lilvExtensionDataNodes.get(it));
  2386. LV2_URI* const rdfExtension(&rdfDescriptor->Extensions[numUsed++]);
  2387. if (lilvExtensionDataNode.is_uri())
  2388. {
  2389. if (const char* const extURI = lilvExtensionDataNode.as_uri())
  2390. {
  2391. *rdfExtension = carla_strdup(extURI);
  2392. continue;
  2393. }
  2394. }
  2395. *rdfExtension = nullptr;
  2396. }
  2397. for (uint32_t x=numUsed; x < rdfDescriptor->ExtensionCount; ++x)
  2398. rdfDescriptor->Extensions[x] = nullptr;
  2399. rdfDescriptor->ExtensionCount = numUsed;
  2400. }
  2401. lilv_nodes_free(const_cast<LilvNodes*>(lilvExtensionDataNodes.me));
  2402. }
  2403. // ----------------------------------------------------------------------------------------------------------------
  2404. // Set Plugin UIs
  2405. {
  2406. #ifdef CARLA_OS_LINUX
  2407. const bool hasMODGui = lilvPlugin.get_modgui_resources_directory().as_uri() != nullptr;
  2408. #else
  2409. const bool hasMODGui = false;
  2410. #endif
  2411. Lilv::UIs lilvUIs(lilvPlugin.get_uis());
  2412. const uint numUIs = lilvUIs.size() + (hasMODGui ? 1 : 0);
  2413. if (numUIs > 0)
  2414. {
  2415. rdfDescriptor->UIs = new LV2_RDF_UI[numUIs];
  2416. uint numUsed = 0;
  2417. LILV_FOREACH(uis, it, lilvUIs)
  2418. {
  2419. CARLA_SAFE_ASSERT_BREAK(numUsed < numUIs);
  2420. Lilv::UI lilvUI(lilvUIs.get(it));
  2421. LV2_RDF_UI* const rdfUI(&rdfDescriptor->UIs[numUsed++]);
  2422. lv2World.load_resource(lilvUI.get_uri());
  2423. // ----------------------------------------------------------------------------------------------------
  2424. // Set UI Type
  2425. /**/ if (lilvUI.is_a(lv2World.ui_gtk2))
  2426. rdfUI->Type = LV2_UI_GTK2;
  2427. else if (lilvUI.is_a(lv2World.ui_gtk3))
  2428. rdfUI->Type = LV2_UI_GTK3;
  2429. else if (lilvUI.is_a(lv2World.ui_qt4))
  2430. rdfUI->Type = LV2_UI_QT4;
  2431. else if (lilvUI.is_a(lv2World.ui_qt5))
  2432. rdfUI->Type = LV2_UI_QT5;
  2433. else if (lilvUI.is_a(lv2World.ui_cocoa))
  2434. rdfUI->Type = LV2_UI_COCOA;
  2435. else if (lilvUI.is_a(lv2World.ui_windows))
  2436. rdfUI->Type = LV2_UI_WINDOWS;
  2437. else if (lilvUI.is_a(lv2World.ui_x11))
  2438. rdfUI->Type = LV2_UI_X11;
  2439. else if (lilvUI.is_a(lv2World.ui_external))
  2440. rdfUI->Type = LV2_UI_EXTERNAL;
  2441. else if (lilvUI.is_a(lv2World.ui_externalOld))
  2442. rdfUI->Type = LV2_UI_OLD_EXTERNAL;
  2443. else if (lilvUI.is_a(lv2World.ui))
  2444. rdfUI->Type = LV2_UI_NONE;
  2445. else
  2446. carla_stderr("lv2_rdf_new(\"%s\") - UI '%s' is of unknown type", uri, lilvUI.get_uri().as_uri());
  2447. // ----------------------------------------------------------------------------------------------------
  2448. // Set UI Information
  2449. {
  2450. if (const char* const uiURI = lilvUI.get_uri().as_uri())
  2451. rdfUI->URI = carla_strdup(uiURI);
  2452. if (const char* const uiBinary = lilvUI.get_binary_uri().as_string())
  2453. rdfUI->Binary = carla_strdup_free(lilv_file_uri_parse(uiBinary, nullptr));
  2454. if (const char* const uiBundle = lilvUI.get_bundle_uri().as_string())
  2455. rdfUI->Bundle = carla_strdup_free(lilv_file_uri_parse(uiBundle, nullptr));
  2456. }
  2457. // ----------------------------------------------------------------------------------------------------
  2458. // Set UI Features
  2459. {
  2460. Lilv::Nodes lilvFeatureNodes(lilvUI.get_supported_features());
  2461. if (const uint numFeatures = lilvFeatureNodes.size())
  2462. {
  2463. Lilv::Nodes lilvFeatureNodesR(lilvUI.get_required_features());
  2464. rdfUI->Features = new LV2_RDF_Feature[numFeatures];
  2465. uint numUsed2 = 0;
  2466. LILV_FOREACH(nodes, it2, lilvFeatureNodes)
  2467. {
  2468. CARLA_SAFE_ASSERT_UINT2_BREAK(numUsed2 < numFeatures, numUsed2, numFeatures);
  2469. Lilv::Node lilvFeatureNode(lilvFeatureNodes.get(it2));
  2470. LV2_RDF_Feature* const rdfFeature(&rdfUI->Features[numUsed2++]);
  2471. rdfFeature->Required = lilvFeatureNodesR.contains(lilvFeatureNode);
  2472. if (const char* const featureURI = lilvFeatureNode.as_uri())
  2473. rdfFeature->URI = carla_strdup(featureURI);
  2474. else
  2475. rdfFeature->URI = nullptr;
  2476. }
  2477. rdfUI->FeatureCount = numUsed2;
  2478. lilv_nodes_free(const_cast<LilvNodes*>(lilvFeatureNodesR.me));
  2479. }
  2480. lilv_nodes_free(const_cast<LilvNodes*>(lilvFeatureNodes.me));
  2481. }
  2482. // ----------------------------------------------------------------------------------------------------
  2483. // Set UI Extensions
  2484. {
  2485. Lilv::Nodes lilvExtensionDataNodes(lilvUI.get_extension_data());
  2486. if (const uint numExtensions = lilvExtensionDataNodes.size())
  2487. {
  2488. rdfUI->Extensions = new LV2_URI[numExtensions];
  2489. uint numUsed2 = 0;
  2490. LILV_FOREACH(nodes, it2, lilvExtensionDataNodes)
  2491. {
  2492. CARLA_SAFE_ASSERT_UINT2_BREAK(numUsed2 < numExtensions, numUsed2, numExtensions);
  2493. Lilv::Node lilvExtensionDataNode(lilvExtensionDataNodes.get(it2));
  2494. LV2_URI* const rdfExtension(&rdfUI->Extensions[numUsed2++]);
  2495. if (lilvExtensionDataNode.is_uri())
  2496. {
  2497. if (const char* const extURI = lilvExtensionDataNode.as_uri())
  2498. {
  2499. *rdfExtension = carla_strdup(extURI);
  2500. continue;
  2501. }
  2502. }
  2503. *rdfExtension = nullptr;
  2504. }
  2505. for (uint x2=numUsed2; x2 < rdfUI->ExtensionCount; ++x2)
  2506. rdfUI->Extensions[x2] = nullptr;
  2507. rdfUI->ExtensionCount = numUsed2;
  2508. }
  2509. lilv_nodes_free(const_cast<LilvNodes*>(lilvExtensionDataNodes.me));
  2510. }
  2511. // ----------------------------------------------------------------------------------------------------
  2512. // Set UI Port Notifications
  2513. {
  2514. Lilv::Nodes portNotifNodes(lv2World.find_nodes(lilvUI.get_uri(), lv2World.ui_portNotif.me, nullptr));
  2515. if (const uint portNotifCount = portNotifNodes.size())
  2516. {
  2517. rdfUI->PortNotificationCount = portNotifCount;
  2518. rdfUI->PortNotifications = new LV2_RDF_UI_PortNotification[portNotifCount];
  2519. uint numUsed2 = 0;
  2520. LILV_FOREACH(nodes, it2, portNotifNodes)
  2521. {
  2522. CARLA_SAFE_ASSERT_UINT2_BREAK(numUsed2 < portNotifCount, numUsed2, portNotifCount);
  2523. Lilv::Node portNotifNode(portNotifNodes.get(it2));
  2524. LV2_RDF_UI_PortNotification* const rdfPortNotif(&rdfUI->PortNotifications[numUsed2++]);
  2525. LilvNode* const protocolNode = lilv_world_get(lv2World.me, portNotifNode,
  2526. lv2World.ui_protocol.me, nullptr);
  2527. if (protocolNode != nullptr)
  2528. {
  2529. CARLA_SAFE_ASSERT_CONTINUE(lilv_node_is_uri(protocolNode));
  2530. const char* const protocol = lilv_node_as_uri(protocolNode);
  2531. CARLA_SAFE_ASSERT_CONTINUE(protocol != nullptr && protocol[0] != '\0');
  2532. /**/ if (std::strcmp(protocol, LV2_UI__floatProtocol) == 0)
  2533. rdfPortNotif->Protocol = LV2_UI_PORT_PROTOCOL_FLOAT;
  2534. else if (std::strcmp(protocol, LV2_UI__peakProtocol) == 0)
  2535. rdfPortNotif->Protocol = LV2_UI_PORT_PROTOCOL_PEAK;
  2536. }
  2537. else
  2538. {
  2539. rdfPortNotif->Protocol = LV2_UI_PORT_PROTOCOL_FLOAT;
  2540. }
  2541. /**/ if (LilvNode* const symbolNode = lilv_world_get(lv2World.me, portNotifNode,
  2542. lv2World.symbol.me, nullptr))
  2543. {
  2544. CARLA_SAFE_ASSERT_CONTINUE(lilv_node_is_string(symbolNode));
  2545. const char* const symbol = lilv_node_as_string(symbolNode);
  2546. CARLA_SAFE_ASSERT_CONTINUE(symbol != nullptr && symbol[0] != '\0');
  2547. rdfPortNotif->Symbol = carla_strdup(symbol);
  2548. lilv_node_free(symbolNode);
  2549. }
  2550. else if (LilvNode* const indexNode = lilv_world_get(lv2World.me, portNotifNode,
  2551. lv2World.ui_portIndex.me, nullptr))
  2552. {
  2553. CARLA_SAFE_ASSERT_CONTINUE(lilv_node_is_int(indexNode));
  2554. const int index = lilv_node_as_int(indexNode);
  2555. CARLA_SAFE_ASSERT_CONTINUE(index >= 0);
  2556. rdfPortNotif->Index = static_cast<uint32_t>(index);
  2557. lilv_node_free(indexNode);
  2558. }
  2559. lilv_node_free(protocolNode);
  2560. }
  2561. }
  2562. lilv_nodes_free(const_cast<LilvNodes*>(portNotifNodes.me));
  2563. }
  2564. }
  2565. for (; hasMODGui;)
  2566. {
  2567. CARLA_SAFE_ASSERT_BREAK(numUsed == numUIs-1);
  2568. LV2_RDF_UI* const rdfUI(&rdfDescriptor->UIs[numUsed++]);
  2569. // -------------------------------------------------------
  2570. // Set UI Type
  2571. rdfUI->Type = LV2_UI_MOD;
  2572. // -------------------------------------------------------
  2573. // Set UI Information
  2574. if (const char* const resDir = lilvPlugin.get_modgui_resources_directory().as_uri())
  2575. rdfUI->URI = carla_strdup_free(lilv_file_uri_parse(resDir, nullptr));
  2576. if (rdfDescriptor->Bundle != nullptr)
  2577. rdfUI->Bundle = carla_strdup(rdfDescriptor->Bundle);
  2578. break;
  2579. }
  2580. rdfDescriptor->UICount = numUsed;
  2581. }
  2582. lilv_nodes_free(const_cast<LilvNodes*>(lilvUIs.me));
  2583. }
  2584. return rdfDescriptor;
  2585. }
  2586. // --------------------------------------------------------------------------------------------------------------------
  2587. // Check if we support a plugin port
  2588. static inline
  2589. bool is_lv2_port_supported(const LV2_Property types) noexcept
  2590. {
  2591. if (LV2_IS_PORT_CONTROL(types))
  2592. return true;
  2593. if (LV2_IS_PORT_AUDIO(types))
  2594. return true;
  2595. if (LV2_IS_PORT_CV(types))
  2596. return true;
  2597. if (LV2_IS_PORT_ATOM_SEQUENCE(types))
  2598. return true;
  2599. if (LV2_IS_PORT_EVENT(types))
  2600. return true;
  2601. if (LV2_IS_PORT_MIDI_LL(types))
  2602. return true;
  2603. return false;
  2604. }
  2605. // --------------------------------------------------------------------------------------------------------------------
  2606. // Check if we support a plugin feature
  2607. static inline
  2608. bool is_lv2_feature_supported(const LV2_URI uri) noexcept
  2609. {
  2610. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0', false);
  2611. if (std::strcmp(uri, LV2_BUF_SIZE__boundedBlockLength) == 0)
  2612. return true;
  2613. if (std::strcmp(uri, LV2_BUF_SIZE__fixedBlockLength) == 0)
  2614. return true;
  2615. if (std::strcmp(uri, LV2_BUF_SIZE__powerOf2BlockLength) == 0)
  2616. return true;
  2617. if (std::strcmp(uri, LV2_CORE__hardRTCapable) == 0)
  2618. return true;
  2619. if (std::strcmp(uri, LV2_CORE__inPlaceBroken) == 0)
  2620. return true;
  2621. if (std::strcmp(uri, LV2_CORE__isLive) == 0)
  2622. return true;
  2623. if (std::strcmp(uri, LV2_EVENT_URI) == 0)
  2624. return true;
  2625. if (std::strcmp(uri, LV2_INLINEDISPLAY__queue_draw) == 0)
  2626. return true;
  2627. if (std::strcmp(uri, LV2_LOG__log) == 0)
  2628. return true;
  2629. if (std::strcmp(uri, LV2_OPTIONS__options) == 0)
  2630. return true;
  2631. if (std::strcmp(uri, LV2_PROGRAMS__Host) == 0)
  2632. return true;
  2633. if (std::strcmp(uri, LV2_RESIZE_PORT__resize) == 0)
  2634. return true;
  2635. if (std::strcmp(uri, LV2_RTSAFE_MEMORY_POOL__Pool) == 0)
  2636. return true;
  2637. if (std::strcmp(uri, LV2_RTSAFE_MEMORY_POOL_DEPRECATED_URI) == 0)
  2638. return true;
  2639. if (std::strcmp(uri, LV2_STATE__freePath) == 0)
  2640. return true;
  2641. if (std::strcmp(uri, LV2_STATE__loadDefaultState) == 0)
  2642. return true;
  2643. if (std::strcmp(uri, LV2_STATE__makePath) == 0)
  2644. return true;
  2645. if (std::strcmp(uri, LV2_STATE__mapPath) == 0)
  2646. return true;
  2647. if (std::strcmp(uri, LV2_STATE__threadSafeRestore) == 0)
  2648. return true;
  2649. if (std::strcmp(uri, LV2_PORT_PROPS__supportsStrictBounds) == 0)
  2650. return true;
  2651. if (std::strcmp(uri, LV2_URI_MAP_URI) == 0)
  2652. return true;
  2653. if (std::strcmp(uri, LV2_URID__map) == 0)
  2654. return true;
  2655. if (std::strcmp(uri, LV2_URID__unmap) == 0)
  2656. return true;
  2657. if (std::strcmp(uri, LV2_WORKER__schedule) == 0)
  2658. return true;
  2659. return false;
  2660. }
  2661. // --------------------------------------------------------------------------------------------------------------------
  2662. // Check if we support a plugin or UI feature
  2663. static inline
  2664. bool is_lv2_ui_feature_supported(const LV2_URI uri) noexcept
  2665. {
  2666. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0', false);
  2667. if (is_lv2_feature_supported(uri))
  2668. return true;
  2669. #ifndef BUILD_BRIDGE_UI
  2670. if (std::strcmp(uri, LV2_DATA_ACCESS_URI) == 0)
  2671. return true;
  2672. if (std::strcmp(uri, LV2_INSTANCE_ACCESS_URI) == 0)
  2673. return true;
  2674. #endif
  2675. if (std::strcmp(uri, LV2_UI__fixedSize) == 0)
  2676. return true;
  2677. if (std::strcmp(uri, LV2_UI__idleInterface) == 0)
  2678. return true;
  2679. if (std::strcmp(uri, LV2_UI__makeResident) == 0)
  2680. return true;
  2681. if (std::strcmp(uri, LV2_UI__makeSONameResident) == 0)
  2682. return true;
  2683. if (std::strcmp(uri, LV2_UI__noUserResize) == 0)
  2684. return true;
  2685. if (std::strcmp(uri, LV2_UI__parent) == 0)
  2686. return true;
  2687. if (std::strcmp(uri, LV2_UI__portMap) == 0)
  2688. return true;
  2689. if (std::strcmp(uri, LV2_UI__portSubscribe) == 0)
  2690. return true;
  2691. if (std::strcmp(uri, LV2_UI__requestValue) == 0)
  2692. return true;
  2693. if (std::strcmp(uri, LV2_UI__resize) == 0)
  2694. return true;
  2695. if (std::strcmp(uri, LV2_UI__touch) == 0)
  2696. return true;
  2697. if (std::strcmp(uri, LV2_EXTERNAL_UI__Widget) == 0)
  2698. return true;
  2699. if (std::strcmp(uri, LV2_EXTERNAL_UI_DEPRECATED_URI) == 0)
  2700. return true;
  2701. return false;
  2702. }
  2703. // --------------------------------------------------------------------------------------------------------------------
  2704. #endif // CARLA_LV2_UTILS_HPP_INCLUDED