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.

3071 lines
126KB

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