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.

2082 lines
85KB

  1. /*
  2. * Carla LV2 utils
  3. * Copyright (C) 2011-2017 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. #ifndef nullptr
  21. # undef NULL
  22. # define NULL nullptr
  23. #endif
  24. // disable -Wdocumentation for LV2 headers
  25. #if defined(__clang__) && (__clang_major__ * 100 + __clang_minor__) > 300
  26. # pragma clang diagnostic push
  27. # pragma clang diagnostic ignored "-Wdocumentation"
  28. #endif
  29. #include "lv2/lv2.h"
  30. #include "lv2/atom.h"
  31. #include "lv2/atom-forge.h"
  32. #include "lv2/atom-helpers.h"
  33. #include "lv2/atom-util.h"
  34. #include "lv2/buf-size.h"
  35. #include "lv2/data-access.h"
  36. // dynmanifest
  37. #include "lv2/event.h"
  38. #include "lv2/event-helpers.h"
  39. #include "lv2/inline-display.h"
  40. #include "lv2/instance-access.h"
  41. #include "lv2/log.h"
  42. // logger
  43. #include "lv2/midi.h"
  44. // morph
  45. #include "lv2/options.h"
  46. #include "lv2/parameters.h"
  47. #include "lv2/patch.h"
  48. #include "lv2/port-groups.h"
  49. #include "lv2/port-props.h"
  50. #include "lv2/presets.h"
  51. #include "lv2/resize-port.h"
  52. #include "lv2/state.h"
  53. #include "lv2/time.h"
  54. #include "lv2/ui.h"
  55. #include "lv2/units.h"
  56. #include "lv2/uri-map.h"
  57. #include "lv2/urid.h"
  58. #include "lv2/worker.h"
  59. #include "lv2/lv2-miditype.h"
  60. #include "lv2/lv2-midifunctions.h"
  61. #include "lv2/lv2_external_ui.h"
  62. #include "lv2/lv2_kxstudio_properties.h"
  63. #include "lv2/lv2_programs.h"
  64. #include "lv2/lv2_rtmempool.h"
  65. #include "lilv/lilvmm.hpp"
  66. #include "sratom/sratom.h"
  67. // enable -Wdocumentation again
  68. #if defined(__clang__) && (__clang_major__ * 100 + __clang_minor__) > 300
  69. # pragma clang diagnostic pop
  70. #endif
  71. #include "lv2_rdf.hpp"
  72. #ifdef USE_QT
  73. # include <QtCore/QStringList>
  74. #else
  75. # include "water/text/StringArray.h"
  76. #endif
  77. // used for scalepoint sorting
  78. #include <map>
  79. typedef std::map<double,const LilvScalePoint*> LilvScalePointMap;
  80. // --------------------------------------------------------------------------------------------------------------------
  81. // Define namespaces and missing prefixes
  82. #define NS_dct "http://purl.org/dc/terms/"
  83. #define NS_doap "http://usefulinc.com/ns/doap#"
  84. #define NS_rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  85. #define NS_rdfs "http://www.w3.org/2000/01/rdf-schema#"
  86. #define NS_llmm "http://ll-plugins.nongnu.org/lv2/ext/midimap#"
  87. #define LV2_MIDI_Map__CC "http://ll-plugins.nongnu.org/lv2/namespace#CC"
  88. #define LV2_MIDI_Map__NRPN "http://ll-plugins.nongnu.org/lv2/namespace#NRPN"
  89. #define LV2_MIDI_LL__MidiPort "http://ll-plugins.nongnu.org/lv2/ext/MidiPort"
  90. #define LV2_UI__makeResident LV2_UI_PREFIX "makeResident"
  91. #define LV2_UI__makeSONameResident LV2_UI_PREFIX "makeSONameResident"
  92. // TODO: update LV2 headers once again
  93. #define LV2_CORE__enabled LV2_CORE_PREFIX "enabled" ///< http://lv2plug.in/ns/lv2core#enabled
  94. // --------------------------------------------------------------------------------------------------------------------
  95. // Custom Atom types
  96. struct LV2_Atom_MidiEvent {
  97. LV2_Atom atom; /**< Atom header. */
  98. uint8_t data[4]; /**< MIDI data (body). */
  99. };
  100. static inline
  101. uint32_t lv2_atom_total_size(const LV2_Atom_MidiEvent& midiEv)
  102. {
  103. return static_cast<uint32_t>(sizeof(LV2_Atom)) + midiEv.atom.size;
  104. }
  105. // ---------------------------------------------------------------------------------------------------------------------
  106. // -Weffc++ compat ext widget
  107. extern "C" {
  108. typedef struct _LV2_External_UI_Widget_Compat {
  109. void (*run )(struct _LV2_External_UI_Widget_Compat*);
  110. void (*show)(struct _LV2_External_UI_Widget_Compat*);
  111. void (*hide)(struct _LV2_External_UI_Widget_Compat*);
  112. _LV2_External_UI_Widget_Compat() noexcept
  113. : run(nullptr), show(nullptr), hide(nullptr) {}
  114. } LV2_External_UI_Widget_Compat;
  115. }
  116. // --------------------------------------------------------------------------------------------------------------------
  117. // Our LV2 World class
  118. class Lv2WorldClass : public Lilv::World
  119. {
  120. public:
  121. // Base Types
  122. Lilv::Node port;
  123. Lilv::Node symbol;
  124. Lilv::Node designation;
  125. Lilv::Node freeWheeling;
  126. Lilv::Node reportsLatency;
  127. // Plugin Types
  128. Lilv::Node class_allpass;
  129. Lilv::Node class_amplifier;
  130. Lilv::Node class_analyzer;
  131. Lilv::Node class_bandpass;
  132. Lilv::Node class_chorus;
  133. Lilv::Node class_comb;
  134. Lilv::Node class_compressor;
  135. Lilv::Node class_constant;
  136. Lilv::Node class_converter;
  137. Lilv::Node class_delay;
  138. Lilv::Node class_distortion;
  139. Lilv::Node class_dynamics;
  140. Lilv::Node class_eq;
  141. Lilv::Node class_envelope;
  142. Lilv::Node class_expander;
  143. Lilv::Node class_filter;
  144. Lilv::Node class_flanger;
  145. Lilv::Node class_function;
  146. Lilv::Node class_gate;
  147. Lilv::Node class_generator;
  148. Lilv::Node class_highpass;
  149. Lilv::Node class_instrument;
  150. Lilv::Node class_limiter;
  151. Lilv::Node class_lowpass;
  152. Lilv::Node class_mixer;
  153. Lilv::Node class_modulator;
  154. Lilv::Node class_multiEQ;
  155. Lilv::Node class_oscillator;
  156. Lilv::Node class_paraEQ;
  157. Lilv::Node class_phaser;
  158. Lilv::Node class_pitch;
  159. Lilv::Node class_reverb;
  160. Lilv::Node class_simulator;
  161. Lilv::Node class_spatial;
  162. Lilv::Node class_spectral;
  163. Lilv::Node class_utility;
  164. Lilv::Node class_waveshaper;
  165. // Port Types
  166. Lilv::Node port_input;
  167. Lilv::Node port_output;
  168. Lilv::Node port_control;
  169. Lilv::Node port_audio;
  170. Lilv::Node port_cv;
  171. Lilv::Node port_atom;
  172. Lilv::Node port_event;
  173. Lilv::Node port_midi;
  174. // Port Properties
  175. Lilv::Node pprop_optional;
  176. Lilv::Node pprop_enumeration;
  177. Lilv::Node pprop_integer;
  178. Lilv::Node pprop_sampleRate;
  179. Lilv::Node pprop_toggled;
  180. Lilv::Node pprop_artifacts;
  181. Lilv::Node pprop_continuousCV;
  182. Lilv::Node pprop_discreteCV;
  183. Lilv::Node pprop_expensive;
  184. Lilv::Node pprop_strictBounds;
  185. Lilv::Node pprop_logarithmic;
  186. Lilv::Node pprop_notAutomatic;
  187. Lilv::Node pprop_notOnGUI;
  188. Lilv::Node pprop_trigger;
  189. Lilv::Node pprop_nonAutomable;
  190. // Unit Hints
  191. Lilv::Node unit_name;
  192. Lilv::Node unit_render;
  193. Lilv::Node unit_symbol;
  194. Lilv::Node unit_unit;
  195. // UI Types
  196. Lilv::Node ui_gtk2;
  197. Lilv::Node ui_gtk3;
  198. Lilv::Node ui_qt4;
  199. Lilv::Node ui_qt5;
  200. Lilv::Node ui_cocoa;
  201. Lilv::Node ui_windows;
  202. Lilv::Node ui_x11;
  203. Lilv::Node ui_external;
  204. Lilv::Node ui_externalOld;
  205. Lilv::Node ui_externalOld2;
  206. // Misc
  207. Lilv::Node atom_bufferType;
  208. Lilv::Node atom_sequence;
  209. Lilv::Node atom_supports;
  210. Lilv::Node preset_preset;
  211. Lilv::Node state_state;
  212. Lilv::Node value_default;
  213. Lilv::Node value_minimum;
  214. Lilv::Node value_maximum;
  215. Lilv::Node rz_asLargeAs;
  216. Lilv::Node rz_minSize;
  217. // Port Data Types
  218. Lilv::Node midi_event;
  219. Lilv::Node patch_message;
  220. Lilv::Node time_position;
  221. // MIDI CC
  222. Lilv::Node mm_defaultControl;
  223. Lilv::Node mm_controlType;
  224. Lilv::Node mm_controlNumber;
  225. // Other
  226. Lilv::Node dct_replaces;
  227. Lilv::Node doap_license;
  228. Lilv::Node rdf_type;
  229. Lilv::Node rdfs_label;
  230. bool needsInit;
  231. const LilvPlugins* allPlugins;
  232. const LilvPlugin** cachedPlugins;
  233. uint pluginCount;
  234. // ----------------------------------------------------------------------------------------------------------------
  235. Lv2WorldClass()
  236. : Lilv::World(),
  237. port (new_uri(LV2_CORE__port)),
  238. symbol (new_uri(LV2_CORE__symbol)),
  239. designation (new_uri(LV2_CORE__designation)),
  240. freeWheeling (new_uri(LV2_CORE__freeWheeling)),
  241. reportsLatency (new_uri(LV2_CORE__reportsLatency)),
  242. class_allpass (new_uri(LV2_CORE__AllpassPlugin)),
  243. class_amplifier (new_uri(LV2_CORE__AmplifierPlugin)),
  244. class_analyzer (new_uri(LV2_CORE__AnalyserPlugin)),
  245. class_bandpass (new_uri(LV2_CORE__BandpassPlugin)),
  246. class_chorus (new_uri(LV2_CORE__ChorusPlugin)),
  247. class_comb (new_uri(LV2_CORE__CombPlugin)),
  248. class_compressor (new_uri(LV2_CORE__CompressorPlugin)),
  249. class_constant (new_uri(LV2_CORE__ConstantPlugin)),
  250. class_converter (new_uri(LV2_CORE__ConverterPlugin)),
  251. class_delay (new_uri(LV2_CORE__DelayPlugin)),
  252. class_distortion (new_uri(LV2_CORE__DistortionPlugin)),
  253. class_dynamics (new_uri(LV2_CORE__DynamicsPlugin)),
  254. class_eq (new_uri(LV2_CORE__EQPlugin)),
  255. class_envelope (new_uri(LV2_CORE__EnvelopePlugin)),
  256. class_expander (new_uri(LV2_CORE__ExpanderPlugin)),
  257. class_filter (new_uri(LV2_CORE__FilterPlugin)),
  258. class_flanger (new_uri(LV2_CORE__FlangerPlugin)),
  259. class_function (new_uri(LV2_CORE__FunctionPlugin)),
  260. class_gate (new_uri(LV2_CORE__GatePlugin)),
  261. class_generator (new_uri(LV2_CORE__GeneratorPlugin)),
  262. class_highpass (new_uri(LV2_CORE__HighpassPlugin)),
  263. class_instrument (new_uri(LV2_CORE__InstrumentPlugin)),
  264. class_limiter (new_uri(LV2_CORE__LimiterPlugin)),
  265. class_lowpass (new_uri(LV2_CORE__LowpassPlugin)),
  266. class_mixer (new_uri(LV2_CORE__MixerPlugin)),
  267. class_modulator (new_uri(LV2_CORE__ModulatorPlugin)),
  268. class_multiEQ (new_uri(LV2_CORE__MultiEQPlugin)),
  269. class_oscillator (new_uri(LV2_CORE__OscillatorPlugin)),
  270. class_paraEQ (new_uri(LV2_CORE__ParaEQPlugin)),
  271. class_phaser (new_uri(LV2_CORE__PhaserPlugin)),
  272. class_pitch (new_uri(LV2_CORE__PitchPlugin)),
  273. class_reverb (new_uri(LV2_CORE__ReverbPlugin)),
  274. class_simulator (new_uri(LV2_CORE__SimulatorPlugin)),
  275. class_spatial (new_uri(LV2_CORE__SpatialPlugin)),
  276. class_spectral (new_uri(LV2_CORE__SpectralPlugin)),
  277. class_utility (new_uri(LV2_CORE__UtilityPlugin)),
  278. class_waveshaper (new_uri(LV2_CORE__WaveshaperPlugin)),
  279. port_input (new_uri(LV2_CORE__InputPort)),
  280. port_output (new_uri(LV2_CORE__OutputPort)),
  281. port_control (new_uri(LV2_CORE__ControlPort)),
  282. port_audio (new_uri(LV2_CORE__AudioPort)),
  283. port_cv (new_uri(LV2_CORE__CVPort)),
  284. port_atom (new_uri(LV2_ATOM__AtomPort)),
  285. port_event (new_uri(LV2_EVENT__EventPort)),
  286. port_midi (new_uri(LV2_MIDI_LL__MidiPort)),
  287. pprop_optional (new_uri(LV2_CORE__connectionOptional)),
  288. pprop_enumeration (new_uri(LV2_CORE__enumeration)),
  289. pprop_integer (new_uri(LV2_CORE__integer)),
  290. pprop_sampleRate (new_uri(LV2_CORE__sampleRate)),
  291. pprop_toggled (new_uri(LV2_CORE__toggled)),
  292. pprop_artifacts (new_uri(LV2_PORT_PROPS__causesArtifacts)),
  293. pprop_continuousCV (new_uri(LV2_PORT_PROPS__continuousCV)),
  294. pprop_discreteCV (new_uri(LV2_PORT_PROPS__discreteCV)),
  295. pprop_expensive (new_uri(LV2_PORT_PROPS__expensive)),
  296. pprop_strictBounds (new_uri(LV2_PORT_PROPS__hasStrictBounds)),
  297. pprop_logarithmic (new_uri(LV2_PORT_PROPS__logarithmic)),
  298. pprop_notAutomatic (new_uri(LV2_PORT_PROPS__notAutomatic)),
  299. pprop_notOnGUI (new_uri(LV2_PORT_PROPS__notOnGUI)),
  300. pprop_trigger (new_uri(LV2_PORT_PROPS__trigger)),
  301. pprop_nonAutomable (new_uri(LV2_KXSTUDIO_PROPERTIES__NonAutomable)),
  302. unit_name (new_uri(LV2_UNITS__name)),
  303. unit_render (new_uri(LV2_UNITS__render)),
  304. unit_symbol (new_uri(LV2_UNITS__symbol)),
  305. unit_unit (new_uri(LV2_UNITS__unit)),
  306. ui_gtk2 (new_uri(LV2_UI__GtkUI)),
  307. ui_gtk3 (new_uri(LV2_UI__Gtk3UI)),
  308. ui_qt4 (new_uri(LV2_UI__Qt4UI)),
  309. ui_qt5 (new_uri(LV2_UI__Qt5UI)),
  310. ui_cocoa (new_uri(LV2_UI__CocoaUI)),
  311. ui_windows (new_uri(LV2_UI__WindowsUI)),
  312. ui_x11 (new_uri(LV2_UI__X11UI)),
  313. ui_external (new_uri(LV2_EXTERNAL_UI__Widget)),
  314. ui_externalOld (new_uri(LV2_EXTERNAL_UI_DEPRECATED_URI)),
  315. ui_externalOld2 (new_uri("http://nedko.arnaudov.name/lv2/external_ui/")),
  316. atom_bufferType (new_uri(LV2_ATOM__bufferType)),
  317. atom_sequence (new_uri(LV2_ATOM__Sequence)),
  318. atom_supports (new_uri(LV2_ATOM__supports)),
  319. preset_preset (new_uri(LV2_PRESETS__Preset)),
  320. state_state (new_uri(LV2_STATE__state)),
  321. value_default (new_uri(LV2_CORE__default)),
  322. value_minimum (new_uri(LV2_CORE__minimum)),
  323. value_maximum (new_uri(LV2_CORE__maximum)),
  324. rz_asLargeAs (new_uri(LV2_RESIZE_PORT__asLargeAs)),
  325. rz_minSize (new_uri(LV2_RESIZE_PORT__minimumSize)),
  326. midi_event (new_uri(LV2_MIDI__MidiEvent)),
  327. patch_message (new_uri(LV2_PATCH__Message)),
  328. time_position (new_uri(LV2_TIME__Position)),
  329. mm_defaultControl (new_uri(NS_llmm "defaultMidiController")),
  330. mm_controlType (new_uri(NS_llmm "controllerType")),
  331. mm_controlNumber (new_uri(NS_llmm "controllerNumber")),
  332. dct_replaces (new_uri(NS_dct "replaces")),
  333. doap_license (new_uri(NS_doap "license")),
  334. rdf_type (new_uri(NS_rdf "type")),
  335. rdfs_label (new_uri(NS_rdfs "label")),
  336. needsInit(true),
  337. allPlugins(nullptr),
  338. cachedPlugins(nullptr),
  339. pluginCount(0) {}
  340. ~Lv2WorldClass() override
  341. {
  342. pluginCount = 0;
  343. allPlugins = nullptr;
  344. if (cachedPlugins != nullptr)
  345. {
  346. delete[] cachedPlugins;
  347. cachedPlugins = nullptr;
  348. }
  349. }
  350. // FIXME - remove this
  351. static Lv2WorldClass& getInstance()
  352. {
  353. static Lv2WorldClass lv2World;
  354. return lv2World;
  355. }
  356. void initIfNeeded(const char* const LV2_PATH)
  357. {
  358. CARLA_SAFE_ASSERT_RETURN(LV2_PATH != nullptr,);
  359. if (! needsInit)
  360. return;
  361. needsInit = false;
  362. Lilv::World::load_all(LV2_PATH);
  363. allPlugins = lilv_world_get_all_plugins(this->me);
  364. CARLA_SAFE_ASSERT_RETURN(allPlugins != nullptr,);
  365. if ((pluginCount = lilv_plugins_size(allPlugins)))
  366. {
  367. cachedPlugins = new const LilvPlugin*[pluginCount+1];
  368. carla_zeroPointers(cachedPlugins, pluginCount+1);
  369. int i = 0;
  370. for (LilvIter* it = lilv_plugins_begin(allPlugins); ! lilv_plugins_is_end(allPlugins, it); it = lilv_plugins_next(allPlugins, it))
  371. cachedPlugins[i++] = lilv_plugins_get(allPlugins, it);
  372. }
  373. }
  374. void load_bundle(const char* const bundle)
  375. {
  376. CARLA_SAFE_ASSERT_RETURN(bundle != nullptr && bundle[0] != '\0',);
  377. CARLA_SAFE_ASSERT_RETURN(needsInit,);
  378. needsInit = false;
  379. Lilv::World::load_bundle(Lilv::Node(new_uri(bundle)));
  380. allPlugins = lilv_world_get_all_plugins(this->me);
  381. CARLA_SAFE_ASSERT_RETURN(allPlugins != nullptr,);
  382. if ((pluginCount = lilv_plugins_size(allPlugins)))
  383. {
  384. cachedPlugins = new const LilvPlugin*[pluginCount+1];
  385. carla_zeroPointers(cachedPlugins, pluginCount+1);
  386. int i = 0;
  387. for (LilvIter* it = lilv_plugins_begin(allPlugins); ! lilv_plugins_is_end(allPlugins, it); it = lilv_plugins_next(allPlugins, it))
  388. cachedPlugins[i++] = lilv_plugins_get(allPlugins, it);
  389. }
  390. }
  391. uint getPluginCount() const
  392. {
  393. CARLA_SAFE_ASSERT_RETURN(! needsInit, 0);
  394. return pluginCount;
  395. }
  396. const LilvPlugin* getPluginFromIndex(const uint index) const
  397. {
  398. CARLA_SAFE_ASSERT_RETURN(! needsInit, nullptr);
  399. CARLA_SAFE_ASSERT_RETURN(cachedPlugins != nullptr, nullptr);
  400. CARLA_SAFE_ASSERT_RETURN(index < pluginCount, nullptr);
  401. return cachedPlugins[index];
  402. }
  403. const LilvPlugin* getPluginFromURI(const LV2_URI uri) const
  404. {
  405. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0', nullptr);
  406. CARLA_SAFE_ASSERT_RETURN(! needsInit, nullptr);
  407. CARLA_SAFE_ASSERT_RETURN(allPlugins != nullptr, nullptr);
  408. LilvNode* const uriNode(lilv_new_uri(this->me, uri));
  409. CARLA_SAFE_ASSERT_RETURN(uriNode != nullptr, nullptr);
  410. const LilvPlugin* const cPlugin(lilv_plugins_get_by_uri(allPlugins, uriNode));
  411. lilv_node_free(uriNode);
  412. return cPlugin;
  413. }
  414. LilvState* getStateFromURI(const LV2_URI uri, const LV2_URID_Map* const uridMap) const
  415. {
  416. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0', nullptr);
  417. CARLA_SAFE_ASSERT_RETURN(uridMap != nullptr, nullptr);
  418. CARLA_SAFE_ASSERT_RETURN(! needsInit, nullptr);
  419. LilvNode* const uriNode(lilv_new_uri(this->me, uri));
  420. CARLA_SAFE_ASSERT_RETURN(uriNode != nullptr, nullptr);
  421. CARLA_SAFE_ASSERT(lilv_world_load_resource(this->me, uriNode) >= 0);
  422. LilvState* const cState(lilv_state_new_from_world(this->me, uridMap, uriNode));
  423. lilv_node_free(uriNode);
  424. return cState;
  425. }
  426. CARLA_PREVENT_VIRTUAL_HEAP_ALLOCATION
  427. CARLA_DECLARE_NON_COPY_STRUCT(Lv2WorldClass)
  428. };
  429. // --------------------------------------------------------------------------------------------------------------------
  430. // Our LV2 Plugin base class
  431. #if defined(__clang__)
  432. # pragma clang diagnostic push
  433. # pragma clang diagnostic ignored "-Weffc++"
  434. #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  435. # pragma GCC diagnostic push
  436. # pragma GCC diagnostic ignored "-Weffc++"
  437. #endif
  438. template<class TimeInfoStruct>
  439. class Lv2PluginBaseClass : public LV2_External_UI_Widget_Compat
  440. {
  441. #if defined(__clang__)
  442. # pragma clang diagnostic pop
  443. #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  444. # pragma GCC diagnostic pop
  445. #endif
  446. public:
  447. Lv2PluginBaseClass(const double sampleRate, const LV2_Feature* const* const features)
  448. : fIsActive(false),
  449. fIsOffline(false),
  450. fUsingNominal(false),
  451. fBufferSize(0),
  452. fSampleRate(sampleRate),
  453. fUridMap(nullptr),
  454. fTimeInfo(),
  455. fLastPositionData(),
  456. fURIs(),
  457. fUI()
  458. {
  459. run = extui_run;
  460. show = extui_show;
  461. hide = extui_hide;
  462. if (fSampleRate < 1.0)
  463. {
  464. carla_stderr("Host doesn't provide a valid sample rate");
  465. return;
  466. }
  467. const LV2_Options_Option* options = nullptr;
  468. const LV2_URID_Map* uridMap = nullptr;
  469. const LV2_URID_Unmap* uridUnmap = nullptr;
  470. for (int i=0; features[i] != nullptr; ++i)
  471. {
  472. if (std::strcmp(features[i]->URI, LV2_OPTIONS__options) == 0)
  473. options = (const LV2_Options_Option*)features[i]->data;
  474. else if (std::strcmp(features[i]->URI, LV2_URID__map) == 0)
  475. uridMap = (const LV2_URID_Map*)features[i]->data;
  476. else if (std::strcmp(features[i]->URI, LV2_URID__unmap) == 0)
  477. uridUnmap = (const LV2_URID_Unmap*)features[i]->data;
  478. }
  479. if (options == nullptr || uridMap == nullptr)
  480. {
  481. carla_stderr("Host doesn't provide option or urid-map features");
  482. return;
  483. }
  484. for (int i=0; options[i].key != 0; ++i)
  485. {
  486. if (uridUnmap != nullptr) {
  487. carla_debug("Host option %i:\"%s\"", i, uridUnmap->unmap(uridUnmap->handle, options[i].key));
  488. }
  489. if (options[i].key == uridMap->map(uridMap->handle, LV2_BUF_SIZE__nominalBlockLength))
  490. {
  491. if (options[i].type == uridMap->map(uridMap->handle, LV2_ATOM__Int))
  492. {
  493. const int value(*(const int*)options[i].value);
  494. CARLA_SAFE_ASSERT_CONTINUE(value > 0);
  495. fBufferSize = static_cast<uint32_t>(value);
  496. fUsingNominal = true;
  497. }
  498. else
  499. {
  500. carla_stderr("Host provides nominalBlockLength but has wrong value type");
  501. }
  502. break;
  503. }
  504. if (options[i].key == uridMap->map(uridMap->handle, LV2_BUF_SIZE__maxBlockLength))
  505. {
  506. if (options[i].type == uridMap->map(uridMap->handle, LV2_ATOM__Int))
  507. {
  508. const int value(*(const int*)options[i].value);
  509. CARLA_SAFE_ASSERT_CONTINUE(value > 0);
  510. fBufferSize = static_cast<uint32_t>(value);
  511. }
  512. else
  513. {
  514. carla_stderr("Host provides maxBlockLength but has wrong value type");
  515. }
  516. // no break, continue in case host supports nominalBlockLength
  517. }
  518. }
  519. if (fBufferSize == 0)
  520. {
  521. carla_stderr("Host doesn't provide buffer-size feature");
  522. //return;
  523. // as testing, continue for now
  524. fBufferSize = 1024;
  525. }
  526. fUridMap = uridMap;
  527. fURIs.map(uridMap);
  528. carla_zeroStruct(fTimeInfo);
  529. carla_zeroStruct(fLastPositionData);
  530. }
  531. virtual ~Lv2PluginBaseClass()
  532. {
  533. }
  534. bool loadedInProperHost() const noexcept
  535. {
  536. return fUridMap != nullptr && fBufferSize != 0;
  537. }
  538. // ----------------------------------------------------------------------------------------------------------------
  539. bool lv2_pre_run(const uint32_t /*frames*/)
  540. {
  541. return true;
  542. }
  543. void lv2_post_run(const uint32_t frames)
  544. {
  545. // update timePos for next callback
  546. if (carla_isZero(fLastPositionData.speed))
  547. return;
  548. if (fLastPositionData.speed > 0.0)
  549. {
  550. // playing forwards
  551. fLastPositionData.frame += frames;
  552. }
  553. else
  554. {
  555. // playing backwards
  556. if (frames >= fLastPositionData.frame)
  557. fLastPositionData.frame = 0;
  558. else
  559. fLastPositionData.frame -= frames;
  560. }
  561. fTimeInfo.frame = fLastPositionData.frame;
  562. if (fTimeInfo.bbt.valid)
  563. {
  564. const double beatsPerMinute = fLastPositionData.beatsPerMinute * fLastPositionData.speed;
  565. const double framesPerBeat = 60.0 * fSampleRate / beatsPerMinute;
  566. const double addedBarBeats = double(frames) / framesPerBeat;
  567. if (fLastPositionData.barBeat >= 0.0f)
  568. {
  569. fLastPositionData.barBeat = std::fmod(fLastPositionData.barBeat+static_cast<float>(addedBarBeats),
  570. fLastPositionData.beatsPerBar);
  571. const double rest = std::fmod(fLastPositionData.barBeat, 1.0f);
  572. fTimeInfo.bbt.beat = static_cast<int32_t>(fLastPositionData.barBeat-rest+1.0);
  573. fTimeInfo.bbt.tick = static_cast<int32_t>(rest*fTimeInfo.bbt.ticksPerBeat+0.5);
  574. if (fLastPositionData.bar_f >= 0.0f)
  575. {
  576. fLastPositionData.bar_f += std::floor((fLastPositionData.barBeat+static_cast<float>(addedBarBeats))/
  577. fLastPositionData.beatsPerBar);
  578. if (fLastPositionData.bar_f <= 0.0f)
  579. {
  580. fLastPositionData.bar = 0;
  581. fLastPositionData.bar_f = 0.0f;
  582. }
  583. else
  584. {
  585. fLastPositionData.bar = static_cast<int32_t>(fLastPositionData.bar_f+0.5f);
  586. }
  587. fTimeInfo.bbt.bar = fLastPositionData.bar + 1;
  588. fTimeInfo.bbt.barStartTick = fTimeInfo.bbt.ticksPerBeat *
  589. fTimeInfo.bbt.beatsPerBar *
  590. (fTimeInfo.bbt.bar-1);
  591. }
  592. }
  593. }
  594. }
  595. // ----------------------------------------------------------------------------------------------------------------
  596. uint32_t lv2_get_options(LV2_Options_Option* const /*options*/) const
  597. {
  598. // currently unused
  599. return LV2_OPTIONS_SUCCESS;
  600. }
  601. uint32_t lv2_set_options(const LV2_Options_Option* const options)
  602. {
  603. for (int i=0; options[i].key != 0; ++i)
  604. {
  605. if (options[i].key == fUridMap->map(fUridMap->handle, LV2_BUF_SIZE__nominalBlockLength))
  606. {
  607. if (options[i].type == fURIs.atomInt)
  608. {
  609. const int value(*(const int*)options[i].value);
  610. CARLA_SAFE_ASSERT_CONTINUE(value > 0);
  611. const uint32_t newBufferSize = static_cast<uint32_t>(value);
  612. if (fBufferSize != newBufferSize)
  613. {
  614. fBufferSize = newBufferSize;
  615. handleBufferSizeChanged(newBufferSize);
  616. }
  617. }
  618. else
  619. {
  620. carla_stderr("Host changed nominalBlockLength but with wrong value type");
  621. }
  622. }
  623. else if (options[i].key == fUridMap->map(fUridMap->handle, LV2_BUF_SIZE__maxBlockLength) && ! fUsingNominal)
  624. {
  625. if (options[i].type == fURIs.atomInt)
  626. {
  627. const int value(*(const int*)options[i].value);
  628. CARLA_SAFE_ASSERT_CONTINUE(value > 0);
  629. const uint32_t newBufferSize = static_cast<uint32_t>(value);
  630. if (fBufferSize != newBufferSize)
  631. {
  632. fBufferSize = newBufferSize;
  633. handleBufferSizeChanged(newBufferSize);
  634. }
  635. }
  636. else
  637. {
  638. carla_stderr("Host changed maxBlockLength but with wrong value type");
  639. }
  640. }
  641. else if (options[i].key == fUridMap->map(fUridMap->handle, LV2_PARAMETERS__sampleRate))
  642. {
  643. if (options[i].type == fURIs.atomFloat)
  644. {
  645. const double value(*(const float*)options[i].value);
  646. CARLA_SAFE_ASSERT_CONTINUE(value > 0.0);
  647. if (carla_isNotEqual(fSampleRate, value))
  648. {
  649. fSampleRate = value;
  650. handleSampleRateChanged(value);
  651. }
  652. }
  653. else
  654. {
  655. carla_stderr("Host changed sampleRate but with wrong value type");
  656. }
  657. }
  658. }
  659. return LV2_OPTIONS_SUCCESS;
  660. }
  661. // ----------------------------------------------------------------------------------------------------------------
  662. int lv2ui_idle() const
  663. {
  664. if (! fUI.isVisible)
  665. return 1;
  666. handleUiRun();
  667. return 0;
  668. }
  669. int lv2ui_show()
  670. {
  671. handleUiShow();
  672. return 0;
  673. }
  674. int lv2ui_hide()
  675. {
  676. handleUiHide();
  677. return 0;
  678. }
  679. void lv2ui_cleanup()
  680. {
  681. if (fUI.isVisible)
  682. handleUiHide();
  683. fUI.host = nullptr;
  684. fUI.writeFunction = nullptr;
  685. fUI.controller = nullptr;
  686. }
  687. // ----------------------------------------------------------------------------------------------------------------
  688. protected:
  689. virtual void handleUiRun() const = 0;
  690. virtual void handleUiShow() = 0;
  691. virtual void handleUiHide() = 0;
  692. virtual void handleBufferSizeChanged(const uint32_t bufferSize) = 0;
  693. virtual void handleSampleRateChanged(const double sampleRate) = 0;
  694. // LV2 host data
  695. bool fIsActive : 1;
  696. bool fIsOffline : 1;
  697. bool fUsingNominal : 1;
  698. uint32_t fBufferSize;
  699. double fSampleRate;
  700. // LV2 host features
  701. const LV2_URID_Map* fUridMap;
  702. // Time info stuff
  703. TimeInfoStruct fTimeInfo;
  704. struct Lv2PositionData {
  705. int32_t bar;
  706. float bar_f;
  707. float barBeat;
  708. uint32_t beatUnit;
  709. float beatsPerBar;
  710. double beatsPerMinute;
  711. uint64_t frame;
  712. double speed;
  713. double ticksPerBeat;
  714. Lv2PositionData()
  715. : bar(-1),
  716. bar_f(-1.0f),
  717. barBeat(-1.0f),
  718. beatUnit(0),
  719. beatsPerBar(0.0f),
  720. beatsPerMinute(-1.0),
  721. frame(0),
  722. speed(0.0),
  723. ticksPerBeat(-1.0) {}
  724. } fLastPositionData;
  725. void resetTimeInfo()
  726. {
  727. carla_zeroStruct(fLastPositionData);
  728. carla_zeroStruct(fTimeInfo);
  729. // hosts may not send all values, resulting on some invalid data
  730. fTimeInfo.bbt.bar = 1;
  731. fTimeInfo.bbt.beat = 1;
  732. fTimeInfo.bbt.beatsPerBar = 4;
  733. fTimeInfo.bbt.beatType = 4;
  734. fTimeInfo.bbt.ticksPerBeat = fLastPositionData.ticksPerBeat = 960.0;
  735. fTimeInfo.bbt.beatsPerMinute = fLastPositionData.beatsPerMinute = 120.0;
  736. }
  737. // Rest of host<->plugin support
  738. struct URIDs {
  739. LV2_URID atomBlank;
  740. LV2_URID atomObject;
  741. LV2_URID atomDouble;
  742. LV2_URID atomFloat;
  743. LV2_URID atomInt;
  744. LV2_URID atomLong;
  745. LV2_URID atomSequence;
  746. LV2_URID atomString;
  747. LV2_URID midiEvent;
  748. LV2_URID timePos;
  749. LV2_URID timeBar;
  750. LV2_URID timeBarBeat;
  751. LV2_URID timeBeatsPerBar;
  752. LV2_URID timeBeatsPerMinute;
  753. LV2_URID timeBeatUnit;
  754. LV2_URID timeFrame;
  755. LV2_URID timeSpeed;
  756. LV2_URID timeTicksPerBeat;
  757. URIDs()
  758. : atomBlank(0),
  759. atomObject(0),
  760. atomDouble(0),
  761. atomFloat(0),
  762. atomInt(0),
  763. atomLong(0),
  764. atomSequence(0),
  765. atomString(0),
  766. midiEvent(0),
  767. timePos(0),
  768. timeBar(0),
  769. timeBarBeat(0),
  770. timeBeatsPerBar(0),
  771. timeBeatsPerMinute(0),
  772. timeBeatUnit(0),
  773. timeFrame(0),
  774. timeSpeed(0),
  775. timeTicksPerBeat(0) {}
  776. void map(const LV2_URID_Map* const uridMap)
  777. {
  778. atomBlank = uridMap->map(uridMap->handle, LV2_ATOM__Blank);
  779. atomObject = uridMap->map(uridMap->handle, LV2_ATOM__Object);
  780. atomDouble = uridMap->map(uridMap->handle, LV2_ATOM__Double);
  781. atomFloat = uridMap->map(uridMap->handle, LV2_ATOM__Float);
  782. atomInt = uridMap->map(uridMap->handle, LV2_ATOM__Int);
  783. atomLong = uridMap->map(uridMap->handle, LV2_ATOM__Long);
  784. atomSequence = uridMap->map(uridMap->handle, LV2_ATOM__Sequence);
  785. atomString = uridMap->map(uridMap->handle, LV2_ATOM__String);
  786. midiEvent = uridMap->map(uridMap->handle, LV2_MIDI__MidiEvent);
  787. timePos = uridMap->map(uridMap->handle, LV2_TIME__Position);
  788. timeBar = uridMap->map(uridMap->handle, LV2_TIME__bar);
  789. timeBarBeat = uridMap->map(uridMap->handle, LV2_TIME__barBeat);
  790. timeBeatUnit = uridMap->map(uridMap->handle, LV2_TIME__beatUnit);
  791. timeFrame = uridMap->map(uridMap->handle, LV2_TIME__frame);
  792. timeSpeed = uridMap->map(uridMap->handle, LV2_TIME__speed);
  793. timeBeatsPerBar = uridMap->map(uridMap->handle, LV2_TIME__beatsPerBar);
  794. timeBeatsPerMinute = uridMap->map(uridMap->handle, LV2_TIME__beatsPerMinute);
  795. timeTicksPerBeat = uridMap->map(uridMap->handle, LV2_KXSTUDIO_PROPERTIES__TimePositionTicksPerBeat);
  796. }
  797. } fURIs;
  798. struct UI {
  799. const LV2_External_UI_Host* host;
  800. LV2UI_Write_Function writeFunction;
  801. LV2UI_Controller controller;
  802. uint32_t portOffset;
  803. bool isEmbed;
  804. bool isVisible;
  805. UI()
  806. : host(nullptr),
  807. writeFunction(nullptr),
  808. controller(nullptr),
  809. portOffset(0),
  810. isEmbed(false),
  811. isVisible(false) {}
  812. } fUI;
  813. private:
  814. // ----------------------------------------------------------------------------------------------------------------
  815. #define handlePtr ((Lv2PluginBaseClass*)handle)
  816. static void extui_run(LV2_External_UI_Widget_Compat* handle)
  817. {
  818. handlePtr->handleUiRun();
  819. }
  820. static void extui_show(LV2_External_UI_Widget_Compat* handle)
  821. {
  822. handlePtr->handleUiShow();
  823. }
  824. static void extui_hide(LV2_External_UI_Widget_Compat* handle)
  825. {
  826. handlePtr->handleUiHide();
  827. }
  828. #undef handlePtr
  829. // ----------------------------------------------------------------------------------------------------------------
  830. CARLA_DECLARE_NON_COPY_STRUCT(Lv2PluginBaseClass)
  831. };
  832. // --------------------------------------------------------------------------------------------------------------------
  833. // Create new RDF object (using lilv)
  834. static inline
  835. const LV2_RDF_Descriptor* lv2_rdf_new(const LV2_URI uri, const bool loadPresets)
  836. {
  837. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0', nullptr);
  838. Lv2WorldClass& lv2World(Lv2WorldClass::getInstance());
  839. const LilvPlugin* const cPlugin(lv2World.getPluginFromURI(uri));
  840. CARLA_SAFE_ASSERT_RETURN(cPlugin != nullptr, nullptr);
  841. Lilv::Plugin lilvPlugin(cPlugin);
  842. LV2_RDF_Descriptor* const rdfDescriptor(new LV2_RDF_Descriptor());
  843. // ----------------------------------------------------------------------------------------------------------------
  844. // Set Plugin Type
  845. {
  846. Lilv::Nodes typeNodes(lilvPlugin.get_value(lv2World.rdf_type));
  847. if (typeNodes.size() > 0)
  848. {
  849. if (typeNodes.contains(lv2World.class_allpass))
  850. rdfDescriptor->Type[0] |= LV2_PLUGIN_ALLPASS;
  851. if (typeNodes.contains(lv2World.class_amplifier))
  852. rdfDescriptor->Type[0] |= LV2_PLUGIN_AMPLIFIER;
  853. if (typeNodes.contains(lv2World.class_analyzer))
  854. rdfDescriptor->Type[1] |= LV2_PLUGIN_ANALYSER;
  855. if (typeNodes.contains(lv2World.class_bandpass))
  856. rdfDescriptor->Type[0] |= LV2_PLUGIN_BANDPASS;
  857. if (typeNodes.contains(lv2World.class_chorus))
  858. rdfDescriptor->Type[1] |= LV2_PLUGIN_CHORUS;
  859. if (typeNodes.contains(lv2World.class_comb))
  860. rdfDescriptor->Type[1] |= LV2_PLUGIN_COMB;
  861. if (typeNodes.contains(lv2World.class_compressor))
  862. rdfDescriptor->Type[0] |= LV2_PLUGIN_COMPRESSOR;
  863. if (typeNodes.contains(lv2World.class_constant))
  864. rdfDescriptor->Type[1] |= LV2_PLUGIN_CONSTANT;
  865. if (typeNodes.contains(lv2World.class_converter))
  866. rdfDescriptor->Type[1] |= LV2_PLUGIN_CONVERTER;
  867. if (typeNodes.contains(lv2World.class_delay))
  868. rdfDescriptor->Type[0] |= LV2_PLUGIN_DELAY;
  869. if (typeNodes.contains(lv2World.class_distortion))
  870. rdfDescriptor->Type[0] |= LV2_PLUGIN_DISTORTION;
  871. if (typeNodes.contains(lv2World.class_dynamics))
  872. rdfDescriptor->Type[0] |= LV2_PLUGIN_DYNAMICS;
  873. if (typeNodes.contains(lv2World.class_eq))
  874. rdfDescriptor->Type[0] |= LV2_PLUGIN_EQ;
  875. if (typeNodes.contains(lv2World.class_envelope))
  876. rdfDescriptor->Type[0] |= LV2_PLUGIN_ENVELOPE;
  877. if (typeNodes.contains(lv2World.class_expander))
  878. rdfDescriptor->Type[0] |= LV2_PLUGIN_EXPANDER;
  879. if (typeNodes.contains(lv2World.class_filter))
  880. rdfDescriptor->Type[0] |= LV2_PLUGIN_FILTER;
  881. if (typeNodes.contains(lv2World.class_flanger))
  882. rdfDescriptor->Type[1] |= LV2_PLUGIN_FLANGER;
  883. if (typeNodes.contains(lv2World.class_function))
  884. rdfDescriptor->Type[1] |= LV2_PLUGIN_FUNCTION;
  885. if (typeNodes.contains(lv2World.class_gate))
  886. rdfDescriptor->Type[0] |= LV2_PLUGIN_GATE;
  887. if (typeNodes.contains(lv2World.class_generator))
  888. rdfDescriptor->Type[1] |= LV2_PLUGIN_GENERATOR;
  889. if (typeNodes.contains(lv2World.class_highpass))
  890. rdfDescriptor->Type[0] |= LV2_PLUGIN_HIGHPASS;
  891. if (typeNodes.contains(lv2World.class_instrument))
  892. rdfDescriptor->Type[1] |= LV2_PLUGIN_INSTRUMENT;
  893. if (typeNodes.contains(lv2World.class_limiter))
  894. rdfDescriptor->Type[0] |= LV2_PLUGIN_LIMITER;
  895. if (typeNodes.contains(lv2World.class_lowpass))
  896. rdfDescriptor->Type[0] |= LV2_PLUGIN_LOWPASS;
  897. if (typeNodes.contains(lv2World.class_mixer))
  898. rdfDescriptor->Type[1] |= LV2_PLUGIN_MIXER;
  899. if (typeNodes.contains(lv2World.class_modulator))
  900. rdfDescriptor->Type[1] |= LV2_PLUGIN_MODULATOR;
  901. if (typeNodes.contains(lv2World.class_multiEQ))
  902. rdfDescriptor->Type[0] |= LV2_PLUGIN_MULTI_EQ;
  903. if (typeNodes.contains(lv2World.class_oscillator))
  904. rdfDescriptor->Type[1] |= LV2_PLUGIN_OSCILLATOR;
  905. if (typeNodes.contains(lv2World.class_paraEQ))
  906. rdfDescriptor->Type[0] |= LV2_PLUGIN_PARA_EQ;
  907. if (typeNodes.contains(lv2World.class_phaser))
  908. rdfDescriptor->Type[1] |= LV2_PLUGIN_PHASER;
  909. if (typeNodes.contains(lv2World.class_pitch))
  910. rdfDescriptor->Type[1] |= LV2_PLUGIN_PITCH;
  911. if (typeNodes.contains(lv2World.class_reverb))
  912. rdfDescriptor->Type[0] |= LV2_PLUGIN_REVERB;
  913. if (typeNodes.contains(lv2World.class_simulator))
  914. rdfDescriptor->Type[0] |= LV2_PLUGIN_SIMULATOR;
  915. if (typeNodes.contains(lv2World.class_spatial))
  916. rdfDescriptor->Type[1] |= LV2_PLUGIN_SPATIAL;
  917. if (typeNodes.contains(lv2World.class_spectral))
  918. rdfDescriptor->Type[1] |= LV2_PLUGIN_SPECTRAL;
  919. if (typeNodes.contains(lv2World.class_utility))
  920. rdfDescriptor->Type[1] |= LV2_PLUGIN_UTILITY;
  921. if (typeNodes.contains(lv2World.class_waveshaper))
  922. rdfDescriptor->Type[0] |= LV2_PLUGIN_WAVESHAPER;
  923. }
  924. lilv_nodes_free(const_cast<LilvNodes*>(typeNodes.me));
  925. }
  926. // ----------------------------------------------------------------------------------------------------------------
  927. // Set Plugin Information
  928. {
  929. rdfDescriptor->URI = carla_strdup(uri);
  930. if (LilvNode* const nameNode = lilv_plugin_get_name(lilvPlugin.me))
  931. {
  932. if (const char* const name = lilv_node_as_string(nameNode))
  933. rdfDescriptor->Name = carla_strdup(name);
  934. lilv_node_free(nameNode);
  935. }
  936. if (const char* const author = lilvPlugin.get_author_name().as_string())
  937. rdfDescriptor->Author = carla_strdup(author);
  938. if (const char* const binary = lilvPlugin.get_library_uri().as_string())
  939. rdfDescriptor->Binary = carla_strdup_free(lilv_file_uri_parse(binary, nullptr));
  940. if (const char* const bundle = lilvPlugin.get_bundle_uri().as_string())
  941. rdfDescriptor->Bundle = carla_strdup_free(lilv_file_uri_parse(bundle, nullptr));
  942. Lilv::Nodes licenseNodes(lilvPlugin.get_value(lv2World.doap_license));
  943. if (licenseNodes.size() > 0)
  944. {
  945. if (const char* const license = licenseNodes.get_first().as_string())
  946. rdfDescriptor->License = carla_strdup(license);
  947. }
  948. lilv_nodes_free(const_cast<LilvNodes*>(licenseNodes.me));
  949. }
  950. // ----------------------------------------------------------------------------------------------------------------
  951. // Set Plugin UniqueID
  952. {
  953. Lilv::Nodes replaceNodes(lilvPlugin.get_value(lv2World.dct_replaces));
  954. if (replaceNodes.size() > 0)
  955. {
  956. Lilv::Node replaceNode(replaceNodes.get_first());
  957. if (replaceNode.is_uri())
  958. {
  959. #ifdef USE_QT
  960. const QString replaceURI(replaceNode.as_uri());
  961. if (replaceURI.startsWith("urn:"))
  962. {
  963. const QString replaceId(replaceURI.split(":").last());
  964. bool ok;
  965. const ulong uniqueId(replaceId.toULong(&ok));
  966. if (ok && uniqueId != 0)
  967. rdfDescriptor->UniqueID = uniqueId;
  968. }
  969. #else
  970. const water::String replaceURI(replaceNode.as_uri());
  971. if (replaceURI.startsWith("urn:"))
  972. {
  973. const int uniqueId(replaceURI.getTrailingIntValue());
  974. if (uniqueId > 0)
  975. rdfDescriptor->UniqueID = static_cast<ulong>(uniqueId);
  976. }
  977. #endif
  978. }
  979. }
  980. lilv_nodes_free(const_cast<LilvNodes*>(replaceNodes.me));
  981. }
  982. // ----------------------------------------------------------------------------------------------------------------
  983. // Set Plugin Ports
  984. if (lilvPlugin.get_num_ports() > 0)
  985. {
  986. rdfDescriptor->PortCount = lilvPlugin.get_num_ports();
  987. rdfDescriptor->Ports = new LV2_RDF_Port[rdfDescriptor->PortCount];
  988. for (uint32_t i = 0; i < rdfDescriptor->PortCount; ++i)
  989. {
  990. Lilv::Port lilvPort(lilvPlugin.get_port_by_index(i));
  991. LV2_RDF_Port* const rdfPort(&rdfDescriptor->Ports[i]);
  992. // --------------------------------------------------------------------------------------------------------
  993. // Set Port Information
  994. {
  995. if (LilvNode* const nameNode = lilv_port_get_name(lilvPlugin.me, lilvPort.me))
  996. {
  997. if (const char* const name = lilv_node_as_string(nameNode))
  998. rdfPort->Name = carla_strdup(name);
  999. lilv_node_free(nameNode);
  1000. }
  1001. if (const char* const symbol = lilv_node_as_string(lilvPort.get_symbol()))
  1002. rdfPort->Symbol = carla_strdup(symbol);
  1003. }
  1004. // --------------------------------------------------------------------------------------------------------
  1005. // Set Port Mode and Type
  1006. {
  1007. // Input or Output
  1008. /**/ if (lilvPort.is_a(lv2World.port_input))
  1009. rdfPort->Types |= LV2_PORT_INPUT;
  1010. else if (lilvPort.is_a(lv2World.port_output))
  1011. rdfPort->Types |= LV2_PORT_OUTPUT;
  1012. else
  1013. carla_stderr("lv2_rdf_new(\"%s\") - port '%s' is not input or output", uri, rdfPort->Name);
  1014. // Data Type
  1015. /**/ if (lilvPort.is_a(lv2World.port_control))
  1016. {
  1017. rdfPort->Types |= LV2_PORT_CONTROL;
  1018. }
  1019. else if (lilvPort.is_a(lv2World.port_audio))
  1020. {
  1021. rdfPort->Types |= LV2_PORT_AUDIO;
  1022. }
  1023. else if (lilvPort.is_a(lv2World.port_cv))
  1024. {
  1025. rdfPort->Types |= LV2_PORT_CV;
  1026. }
  1027. else if (lilvPort.is_a(lv2World.port_atom))
  1028. {
  1029. rdfPort->Types |= LV2_PORT_ATOM;
  1030. Lilv::Nodes bufferTypeNodes(lilvPort.get_value(lv2World.atom_bufferType));
  1031. for (LilvIter* it = lilv_nodes_begin(bufferTypeNodes.me); ! lilv_nodes_is_end(bufferTypeNodes.me, it); it = lilv_nodes_next(bufferTypeNodes.me, it))
  1032. {
  1033. const Lilv::Node node(lilv_nodes_get(bufferTypeNodes.me, it));
  1034. CARLA_SAFE_ASSERT_CONTINUE(node.is_uri());
  1035. if (node.equals(lv2World.atom_sequence))
  1036. rdfPort->Types |= LV2_PORT_ATOM_SEQUENCE;
  1037. else
  1038. carla_stderr("lv2_rdf_new(\"%s\") - port '%s' uses an unknown atom buffer type '%s'", uri, rdfPort->Name, node.as_uri());
  1039. }
  1040. Lilv::Nodes supportNodes(lilvPort.get_value(lv2World.atom_supports));
  1041. for (LilvIter* it = lilv_nodes_begin(supportNodes.me); ! lilv_nodes_is_end(supportNodes.me, it); it = lilv_nodes_next(supportNodes.me, it))
  1042. {
  1043. const Lilv::Node node(lilv_nodes_get(supportNodes.me, it));
  1044. CARLA_SAFE_ASSERT_CONTINUE(node.is_uri());
  1045. /**/ if (node.equals(lv2World.midi_event))
  1046. rdfPort->Types |= LV2_PORT_DATA_MIDI_EVENT;
  1047. else if (node.equals(lv2World.patch_message))
  1048. rdfPort->Types |= LV2_PORT_DATA_PATCH_MESSAGE;
  1049. else if (node.equals(lv2World.time_position))
  1050. rdfPort->Types |= LV2_PORT_DATA_TIME_POSITION;
  1051. #if 0
  1052. // something new we don't support yet?
  1053. else if (std::strstr(node.as_uri(), "lv2plug.in/") != nullptr)
  1054. carla_stderr("lv2_rdf_new(\"%s\") - port '%s' is of atom type but has unsupported data '%s'", uri, rdfPort->Name, node.as_uri());
  1055. #endif
  1056. }
  1057. lilv_nodes_free(const_cast<LilvNodes*>(bufferTypeNodes.me));
  1058. lilv_nodes_free(const_cast<LilvNodes*>(supportNodes.me));
  1059. }
  1060. else if (lilvPort.is_a(lv2World.port_event))
  1061. {
  1062. rdfPort->Types |= LV2_PORT_EVENT;
  1063. bool supported = false;
  1064. if (lilvPort.supports_event(lv2World.midi_event))
  1065. {
  1066. rdfPort->Types |= LV2_PORT_DATA_MIDI_EVENT;
  1067. supported = true;
  1068. }
  1069. if (lilvPort.supports_event(lv2World.patch_message))
  1070. {
  1071. rdfPort->Types |= LV2_PORT_DATA_PATCH_MESSAGE;
  1072. supported = true;
  1073. }
  1074. if (lilvPort.supports_event(lv2World.time_position))
  1075. {
  1076. rdfPort->Types |= LV2_PORT_DATA_TIME_POSITION;
  1077. supported = true;
  1078. }
  1079. if (! supported)
  1080. carla_stderr("lv2_rdf_new(\"%s\") - port '%s' is of event type but has unsupported data", uri, rdfPort->Name);
  1081. }
  1082. else if (lilvPort.is_a(lv2World.port_midi))
  1083. {
  1084. rdfPort->Types |= LV2_PORT_MIDI_LL;
  1085. rdfPort->Types |= LV2_PORT_DATA_MIDI_EVENT;
  1086. }
  1087. else
  1088. carla_stderr("lv2_rdf_new(\"%s\") - port '%s' is of unkown data type", uri, rdfPort->Name);
  1089. }
  1090. // --------------------------------------------------------------------------------------------------------
  1091. // Set Port Properties
  1092. {
  1093. if (lilvPort.has_property(lv2World.pprop_optional))
  1094. rdfPort->Properties |= LV2_PORT_OPTIONAL;
  1095. if (lilvPort.has_property(lv2World.pprop_enumeration))
  1096. rdfPort->Properties |= LV2_PORT_ENUMERATION;
  1097. if (lilvPort.has_property(lv2World.pprop_integer))
  1098. rdfPort->Properties |= LV2_PORT_INTEGER;
  1099. if (lilvPort.has_property(lv2World.pprop_sampleRate))
  1100. rdfPort->Properties |= LV2_PORT_SAMPLE_RATE;
  1101. if (lilvPort.has_property(lv2World.pprop_toggled))
  1102. rdfPort->Properties |= LV2_PORT_TOGGLED;
  1103. if (lilvPort.has_property(lv2World.pprop_artifacts))
  1104. rdfPort->Properties |= LV2_PORT_CAUSES_ARTIFACTS;
  1105. if (lilvPort.has_property(lv2World.pprop_continuousCV))
  1106. rdfPort->Properties |= LV2_PORT_CONTINUOUS_CV;
  1107. if (lilvPort.has_property(lv2World.pprop_discreteCV))
  1108. rdfPort->Properties |= LV2_PORT_DISCRETE_CV;
  1109. if (lilvPort.has_property(lv2World.pprop_expensive))
  1110. rdfPort->Properties |= LV2_PORT_EXPENSIVE;
  1111. if (lilvPort.has_property(lv2World.pprop_strictBounds))
  1112. rdfPort->Properties |= LV2_PORT_STRICT_BOUNDS;
  1113. if (lilvPort.has_property(lv2World.pprop_logarithmic))
  1114. rdfPort->Properties |= LV2_PORT_LOGARITHMIC;
  1115. if (lilvPort.has_property(lv2World.pprop_notAutomatic))
  1116. rdfPort->Properties |= LV2_PORT_NOT_AUTOMATIC;
  1117. if (lilvPort.has_property(lv2World.pprop_notOnGUI))
  1118. rdfPort->Properties |= LV2_PORT_NOT_ON_GUI;
  1119. if (lilvPort.has_property(lv2World.pprop_trigger))
  1120. rdfPort->Properties |= LV2_PORT_TRIGGER;
  1121. if (lilvPort.has_property(lv2World.pprop_nonAutomable))
  1122. rdfPort->Properties |= LV2_PORT_NON_AUTOMABLE;
  1123. if (lilvPort.has_property(lv2World.reportsLatency))
  1124. rdfPort->Designation = LV2_PORT_DESIGNATION_LATENCY;
  1125. // no port properties set, check if using old/invalid ones
  1126. if (rdfPort->Properties == 0x0)
  1127. {
  1128. const Lilv::Node oldPropArtifacts(lv2World.new_uri("http://lv2plug.in/ns/dev/extportinfo#causesArtifacts"));
  1129. const Lilv::Node oldPropContinuousCV(lv2World.new_uri("http://lv2plug.in/ns/dev/extportinfo#continuousCV"));
  1130. const Lilv::Node oldPropDiscreteCV(lv2World.new_uri("http://lv2plug.in/ns/dev/extportinfo#discreteCV"));
  1131. const Lilv::Node oldPropExpensive(lv2World.new_uri("http://lv2plug.in/ns/dev/extportinfo#expensive"));
  1132. const Lilv::Node oldPropStrictBounds(lv2World.new_uri("http://lv2plug.in/ns/dev/extportinfo#hasStrictBounds"));
  1133. const Lilv::Node oldPropLogarithmic(lv2World.new_uri("http://lv2plug.in/ns/dev/extportinfo#logarithmic"));
  1134. const Lilv::Node oldPropNotAutomatic(lv2World.new_uri("http://lv2plug.in/ns/dev/extportinfo#notAutomatic"));
  1135. const Lilv::Node oldPropNotOnGUI(lv2World.new_uri("http://lv2plug.in/ns/dev/extportinfo#notOnGUI"));
  1136. const Lilv::Node oldPropTrigger(lv2World.new_uri("http://lv2plug.in/ns/dev/extportinfo#trigger"));
  1137. if (lilvPort.has_property(oldPropArtifacts))
  1138. {
  1139. rdfPort->Properties |= LV2_PORT_CAUSES_ARTIFACTS;
  1140. carla_stderr("lv2_rdf_new(\"%s\") - port '%s' uses old/invalid LV2 property for 'causesArtifacts'", uri, rdfPort->Name);
  1141. }
  1142. if (lilvPort.has_property(oldPropContinuousCV))
  1143. {
  1144. rdfPort->Properties |= LV2_PORT_CONTINUOUS_CV;
  1145. carla_stderr("lv2_rdf_new(\"%s\") - port '%s' uses old/invalid LV2 property for 'continuousCV'", uri, rdfPort->Name);
  1146. }
  1147. if (lilvPort.has_property(oldPropDiscreteCV))
  1148. {
  1149. rdfPort->Properties |= LV2_PORT_DISCRETE_CV;
  1150. carla_stderr("lv2_rdf_new(\"%s\") - port '%s' uses old/invalid LV2 property for 'discreteCV'", uri, rdfPort->Name);
  1151. }
  1152. if (lilvPort.has_property(oldPropExpensive))
  1153. {
  1154. rdfPort->Properties |= LV2_PORT_EXPENSIVE;
  1155. carla_stderr("lv2_rdf_new(\"%s\") - port '%s' uses old/invalid LV2 property for 'expensive'", uri, rdfPort->Name);
  1156. }
  1157. if (lilvPort.has_property(oldPropStrictBounds))
  1158. {
  1159. rdfPort->Properties |= LV2_PORT_STRICT_BOUNDS;
  1160. carla_stderr("lv2_rdf_new(\"%s\") - port '%s' uses old/invalid LV2 property for 'hasStrictBounds'", uri, rdfPort->Name);
  1161. }
  1162. if (lilvPort.has_property(oldPropLogarithmic))
  1163. {
  1164. rdfPort->Properties |= LV2_PORT_LOGARITHMIC;
  1165. carla_stderr("lv2_rdf_new(\"%s\") - port '%s' uses old/invalid LV2 property for 'logarithmic'", uri, rdfPort->Name);
  1166. }
  1167. if (lilvPort.has_property(oldPropNotAutomatic))
  1168. {
  1169. rdfPort->Properties |= LV2_PORT_NOT_AUTOMATIC;
  1170. carla_stderr("lv2_rdf_new(\"%s\") - port '%s' uses old/invalid LV2 property for 'notAutomatic'", uri, rdfPort->Name);
  1171. }
  1172. if (lilvPort.has_property(oldPropNotOnGUI))
  1173. {
  1174. rdfPort->Properties |= LV2_PORT_NOT_ON_GUI;
  1175. carla_stderr("lv2_rdf_new(\"%s\") - port '%s' uses old/invalid LV2 property for 'notOnGUI'", uri, rdfPort->Name);
  1176. }
  1177. if (lilvPort.has_property(oldPropTrigger))
  1178. {
  1179. rdfPort->Properties |= LV2_PORT_TRIGGER;
  1180. carla_stderr("lv2_rdf_new(\"%s\") - port '%s' uses old/invalid LV2 property for 'trigger'", uri, rdfPort->Name);
  1181. }
  1182. }
  1183. }
  1184. // --------------------------------------------------------------------------------------------------------
  1185. // Set Port Designation
  1186. {
  1187. if (LilvNode* const designationNode = lilv_port_get(lilvPort.parent, lilvPort.me, lv2World.designation.me))
  1188. {
  1189. if (const char* const designation = lilv_node_as_string(designationNode))
  1190. {
  1191. /**/ if (std::strcmp(designation, LV2_CORE__control) == 0)
  1192. rdfPort->Designation = LV2_PORT_DESIGNATION_CONTROL;
  1193. else if (std::strcmp(designation, LV2_CORE__enabled) == 0)
  1194. rdfPort->Designation = LV2_PORT_DESIGNATION_ENABLED;
  1195. else if (std::strcmp(designation, LV2_CORE__freeWheeling) == 0)
  1196. rdfPort->Designation = LV2_PORT_DESIGNATION_FREEWHEELING;
  1197. else if (std::strcmp(designation, LV2_CORE__latency) == 0)
  1198. rdfPort->Designation = LV2_PORT_DESIGNATION_LATENCY;
  1199. else if (std::strcmp(designation, LV2_PARAMETERS__sampleRate) == 0)
  1200. rdfPort->Designation = LV2_PORT_DESIGNATION_SAMPLE_RATE;
  1201. else if (std::strcmp(designation, LV2_TIME__bar) == 0)
  1202. rdfPort->Designation = LV2_PORT_DESIGNATION_TIME_BAR;
  1203. else if (std::strcmp(designation, LV2_TIME__barBeat) == 0)
  1204. rdfPort->Designation = LV2_PORT_DESIGNATION_TIME_BAR_BEAT;
  1205. else if (std::strcmp(designation, LV2_TIME__beat) == 0)
  1206. rdfPort->Designation = LV2_PORT_DESIGNATION_TIME_BEAT;
  1207. else if (std::strcmp(designation, LV2_TIME__beatUnit) == 0)
  1208. rdfPort->Designation = LV2_PORT_DESIGNATION_TIME_BEAT_UNIT;
  1209. else if (std::strcmp(designation, LV2_TIME__beatsPerBar) == 0)
  1210. rdfPort->Designation = LV2_PORT_DESIGNATION_TIME_BEATS_PER_BAR;
  1211. else if (std::strcmp(designation, LV2_TIME__beatsPerMinute) == 0)
  1212. rdfPort->Designation = LV2_PORT_DESIGNATION_TIME_BEATS_PER_MINUTE;
  1213. else if (std::strcmp(designation, LV2_TIME__frame) == 0)
  1214. rdfPort->Designation = LV2_PORT_DESIGNATION_TIME_FRAME;
  1215. else if (std::strcmp(designation, LV2_TIME__framesPerSecond) == 0)
  1216. rdfPort->Designation = LV2_PORT_DESIGNATION_TIME_FRAMES_PER_SECOND;
  1217. else if (std::strcmp(designation, LV2_TIME__speed) == 0)
  1218. rdfPort->Designation = LV2_PORT_DESIGNATION_TIME_SPEED;
  1219. else if (std::strcmp(designation, LV2_KXSTUDIO_PROPERTIES__TimePositionTicksPerBeat) == 0)
  1220. rdfPort->Designation = LV2_PORT_DESIGNATION_TIME_TICKS_PER_BEAT;
  1221. else if (std::strncmp(designation, LV2_PARAMETERS_PREFIX, std::strlen(LV2_PARAMETERS_PREFIX)) == 0)
  1222. pass();
  1223. else if (std::strncmp(designation, LV2_PORT_GROUPS_PREFIX, std::strlen(LV2_PORT_GROUPS_PREFIX)) == 0)
  1224. pass();
  1225. else
  1226. carla_stderr("lv2_rdf_new(\"%s\") - got unknown port designation '%s'", uri, designation);
  1227. }
  1228. lilv_node_free(designationNode);
  1229. }
  1230. }
  1231. // --------------------------------------------------------------------------------------------------------
  1232. // Set Port MIDI Map
  1233. {
  1234. if (LilvNode* const midiMapNode = lilv_port_get(lilvPort.parent, lilvPort.me, lv2World.mm_defaultControl.me))
  1235. {
  1236. if (lilv_node_is_blank(midiMapNode))
  1237. {
  1238. Lilv::Nodes midiMapTypeNodes(lv2World.find_nodes(midiMapNode, lv2World.mm_controlType, nullptr));
  1239. Lilv::Nodes midiMapNumberNodes(lv2World.find_nodes(midiMapNode, lv2World.mm_controlNumber, nullptr));
  1240. if (midiMapTypeNodes.size() == 1 && midiMapNumberNodes.size() == 1)
  1241. {
  1242. if (const char* const midiMapType = midiMapTypeNodes.get_first().as_string())
  1243. {
  1244. /**/ if (std::strcmp(midiMapType, LV2_MIDI_Map__CC) == 0)
  1245. rdfPort->MidiMap.Type = LV2_PORT_MIDI_MAP_CC;
  1246. else if (std::strcmp(midiMapType, LV2_MIDI_Map__NRPN) == 0)
  1247. rdfPort->MidiMap.Type = LV2_PORT_MIDI_MAP_NRPN;
  1248. else
  1249. carla_stderr("lv2_rdf_new(\"%s\") - got unknown port Midi-Map type '%s'", uri, midiMapType);
  1250. rdfPort->MidiMap.Number = static_cast<uint32_t>(midiMapNumberNodes.get_first().as_int());
  1251. }
  1252. }
  1253. lilv_nodes_free(const_cast<LilvNodes*>(midiMapTypeNodes.me));
  1254. lilv_nodes_free(const_cast<LilvNodes*>(midiMapNumberNodes.me));
  1255. }
  1256. lilv_node_free(midiMapNode);
  1257. }
  1258. // TODO - also check using new official MIDI API too
  1259. }
  1260. // --------------------------------------------------------------------------------------------------------
  1261. // Set Port Points
  1262. {
  1263. if (LilvNode* const defNode = lilv_port_get(lilvPort.parent, lilvPort.me, lv2World.value_default.me))
  1264. {
  1265. rdfPort->Points.Hints |= LV2_PORT_POINT_DEFAULT;
  1266. rdfPort->Points.Default = lilv_node_as_float(defNode);
  1267. lilv_node_free(defNode);
  1268. }
  1269. if (LilvNode* const minNode = lilv_port_get(lilvPort.parent, lilvPort.me, lv2World.value_minimum.me))
  1270. {
  1271. rdfPort->Points.Hints |= LV2_PORT_POINT_MINIMUM;
  1272. rdfPort->Points.Minimum = lilv_node_as_float(minNode);
  1273. lilv_node_free(minNode);
  1274. }
  1275. if (LilvNode* const maxNode = lilv_port_get(lilvPort.parent, lilvPort.me, lv2World.value_maximum.me))
  1276. {
  1277. rdfPort->Points.Hints |= LV2_PORT_POINT_MAXIMUM;
  1278. rdfPort->Points.Maximum = lilv_node_as_float(maxNode);
  1279. lilv_node_free(maxNode);
  1280. }
  1281. }
  1282. // --------------------------------------------------------------------------------------------------------
  1283. // Set Port Unit
  1284. {
  1285. if (LilvNode* const unitUnitNode = lilv_port_get(lilvPort.parent, lilvPort.me, lv2World.unit_unit.me))
  1286. {
  1287. if (lilv_node_is_uri(unitUnitNode))
  1288. {
  1289. if (const char* const unitUnit = lilv_node_as_uri(unitUnitNode))
  1290. {
  1291. rdfPort->Unit.Hints |= LV2_PORT_UNIT_UNIT;
  1292. /**/ if (std::strcmp(unitUnit, LV2_UNITS__bar) == 0)
  1293. rdfPort->Unit.Unit = LV2_PORT_UNIT_BAR;
  1294. else if (std::strcmp(unitUnit, LV2_UNITS__beat) == 0)
  1295. rdfPort->Unit.Unit = LV2_PORT_UNIT_BEAT;
  1296. else if (std::strcmp(unitUnit, LV2_UNITS__bpm) == 0)
  1297. rdfPort->Unit.Unit = LV2_PORT_UNIT_BPM;
  1298. else if (std::strcmp(unitUnit, LV2_UNITS__cent) == 0)
  1299. rdfPort->Unit.Unit = LV2_PORT_UNIT_CENT;
  1300. else if (std::strcmp(unitUnit, LV2_UNITS__cm) == 0)
  1301. rdfPort->Unit.Unit = LV2_PORT_UNIT_CM;
  1302. else if (std::strcmp(unitUnit, LV2_UNITS__coef) == 0)
  1303. rdfPort->Unit.Unit = LV2_PORT_UNIT_COEF;
  1304. else if (std::strcmp(unitUnit, LV2_UNITS__db) == 0)
  1305. rdfPort->Unit.Unit = LV2_PORT_UNIT_DB;
  1306. else if (std::strcmp(unitUnit, LV2_UNITS__degree) == 0)
  1307. rdfPort->Unit.Unit = LV2_PORT_UNIT_DEGREE;
  1308. else if (std::strcmp(unitUnit, LV2_UNITS__frame) == 0)
  1309. rdfPort->Unit.Unit = LV2_PORT_UNIT_FRAME;
  1310. else if (std::strcmp(unitUnit, LV2_UNITS__hz) == 0)
  1311. rdfPort->Unit.Unit = LV2_PORT_UNIT_HZ;
  1312. else if (std::strcmp(unitUnit, LV2_UNITS__inch) == 0)
  1313. rdfPort->Unit.Unit = LV2_PORT_UNIT_INCH;
  1314. else if (std::strcmp(unitUnit, LV2_UNITS__khz) == 0)
  1315. rdfPort->Unit.Unit = LV2_PORT_UNIT_KHZ;
  1316. else if (std::strcmp(unitUnit, LV2_UNITS__km) == 0)
  1317. rdfPort->Unit.Unit = LV2_PORT_UNIT_KM;
  1318. else if (std::strcmp(unitUnit, LV2_UNITS__m) == 0)
  1319. rdfPort->Unit.Unit = LV2_PORT_UNIT_M;
  1320. else if (std::strcmp(unitUnit, LV2_UNITS__mhz) == 0)
  1321. rdfPort->Unit.Unit = LV2_PORT_UNIT_MHZ;
  1322. else if (std::strcmp(unitUnit, LV2_UNITS__midiNote) == 0)
  1323. rdfPort->Unit.Unit = LV2_PORT_UNIT_MIDINOTE;
  1324. else if (std::strcmp(unitUnit, LV2_UNITS__mile) == 0)
  1325. rdfPort->Unit.Unit = LV2_PORT_UNIT_MILE;
  1326. else if (std::strcmp(unitUnit, LV2_UNITS__min) == 0)
  1327. rdfPort->Unit.Unit = LV2_PORT_UNIT_MIN;
  1328. else if (std::strcmp(unitUnit, LV2_UNITS__mm) == 0)
  1329. rdfPort->Unit.Unit = LV2_PORT_UNIT_MM;
  1330. else if (std::strcmp(unitUnit, LV2_UNITS__ms) == 0)
  1331. rdfPort->Unit.Unit = LV2_PORT_UNIT_MS;
  1332. else if (std::strcmp(unitUnit, LV2_UNITS__oct) == 0)
  1333. rdfPort->Unit.Unit = LV2_PORT_UNIT_OCT;
  1334. else if (std::strcmp(unitUnit, LV2_UNITS__pc) == 0)
  1335. rdfPort->Unit.Unit = LV2_PORT_UNIT_PC;
  1336. else if (std::strcmp(unitUnit, LV2_UNITS__s) == 0)
  1337. rdfPort->Unit.Unit = LV2_PORT_UNIT_S;
  1338. else if (std::strcmp(unitUnit, LV2_UNITS__semitone12TET) == 0)
  1339. rdfPort->Unit.Unit = LV2_PORT_UNIT_SEMITONE;
  1340. else
  1341. carla_stderr("lv2_rdf_new(\"%s\") - got unknown unit unit '%s'", uri, unitUnit);
  1342. }
  1343. }
  1344. if (LilvNode* const unitNameNode = lilv_world_get(lv2World.me, unitUnitNode, lv2World.unit_name.me, nullptr))
  1345. {
  1346. if (const char* const unitName = lilv_node_as_string(unitNameNode))
  1347. {
  1348. rdfPort->Unit.Hints |= LV2_PORT_UNIT_NAME;
  1349. rdfPort->Unit.Name = carla_strdup(unitName);
  1350. }
  1351. lilv_node_free(unitNameNode);
  1352. }
  1353. if (LilvNode* const unitRenderNode = lilv_world_get(lv2World.me, unitUnitNode, lv2World.unit_render.me, nullptr))
  1354. {
  1355. if (const char* const unitRender = lilv_node_as_string(unitRenderNode))
  1356. {
  1357. rdfPort->Unit.Hints |= LV2_PORT_UNIT_RENDER;
  1358. rdfPort->Unit.Render = carla_strdup(unitRender);
  1359. }
  1360. lilv_node_free(unitRenderNode);
  1361. }
  1362. if (LilvNode* const unitSymbolNode = lilv_world_get(lv2World.me, unitUnitNode, lv2World.unit_symbol.me, nullptr))
  1363. {
  1364. if (const char* const unitSymbol = lilv_node_as_string(unitSymbolNode))
  1365. {
  1366. rdfPort->Unit.Hints |= LV2_PORT_UNIT_SYMBOL;
  1367. rdfPort->Unit.Symbol = carla_strdup(unitSymbol);
  1368. }
  1369. lilv_node_free(unitSymbolNode);
  1370. }
  1371. lilv_node_free(unitUnitNode);
  1372. }
  1373. }
  1374. // --------------------------------------------------------------------------------------------------------
  1375. // Set Port Minimum Size
  1376. {
  1377. if (LilvNode* const minimumSizeNode = lilv_port_get(lilvPort.parent, lilvPort.me, lv2World.rz_minSize.me))
  1378. {
  1379. const int minimumSize(lilv_node_as_int(minimumSizeNode));
  1380. if (minimumSize > 0)
  1381. rdfPort->MinimumSize = static_cast<uint32_t>(minimumSize);
  1382. else
  1383. carla_safe_assert_int("minimumSize > 0", __FILE__, __LINE__, minimumSize);
  1384. lilv_node_free(minimumSizeNode);
  1385. }
  1386. }
  1387. // --------------------------------------------------------------------------------------------------------
  1388. // Set Port Scale Points
  1389. {
  1390. Lilv::ScalePoints lilvScalePoints(lilvPort.get_scale_points());
  1391. if (lilvScalePoints.size() > 0)
  1392. {
  1393. rdfPort->ScalePointCount = lilvScalePoints.size();
  1394. rdfPort->ScalePoints = new LV2_RDF_PortScalePoint[rdfPort->ScalePointCount];
  1395. // get all scalepoints and sort them by value
  1396. LilvScalePointMap sortedpoints;
  1397. LILV_FOREACH(scale_points, it, lilvScalePoints)
  1398. {
  1399. Lilv::ScalePoint lilvScalePoint(lilvScalePoints.get(it));
  1400. CARLA_SAFE_ASSERT_CONTINUE(lilvScalePoint.get_label() != nullptr);
  1401. if (const LilvNode* const valuenode = lilvScalePoint.get_value())
  1402. {
  1403. const double valueid = lilv_node_as_float(valuenode);
  1404. sortedpoints[valueid] = lilvScalePoint.me;
  1405. }
  1406. }
  1407. // now safe to store, sorted by using std::map
  1408. uint32_t h = 0;
  1409. for (LilvScalePointMap::iterator it=sortedpoints.begin(), end=sortedpoints.end(); it != end; ++it)
  1410. {
  1411. CARLA_SAFE_ASSERT_BREAK(h < rdfPort->ScalePointCount);
  1412. LV2_RDF_PortScalePoint* const rdfScalePoint(&rdfPort->ScalePoints[h++]);
  1413. const LilvScalePoint* const scalepoint = it->second;
  1414. const LilvNode* const xlabel = lilv_scale_point_get_label(scalepoint);
  1415. const LilvNode* const xvalue = lilv_scale_point_get_value(scalepoint);
  1416. rdfScalePoint->Label = carla_strdup(lilv_node_as_string(xlabel));
  1417. rdfScalePoint->Value = lilv_node_as_float(xvalue);
  1418. }
  1419. }
  1420. lilv_nodes_free(const_cast<LilvNodes*>(lilvScalePoints.me));
  1421. }
  1422. }
  1423. }
  1424. // ----------------------------------------------------------------------------------------------------------------
  1425. // Set Plugin Presets
  1426. if (loadPresets)
  1427. {
  1428. Lilv::Nodes presetNodes(lilvPlugin.get_related(lv2World.preset_preset));
  1429. if (presetNodes.size() > 0)
  1430. {
  1431. // create a list of preset URIs (for sorting and unique-ness)
  1432. #ifdef USE_QT
  1433. QStringList presetListURIs;
  1434. LILV_FOREACH(nodes, it, presetNodes)
  1435. {
  1436. Lilv::Node presetNode(presetNodes.get(it));
  1437. QString presetURI(presetNode.as_uri());
  1438. if (! (presetURI.trimmed().isEmpty() || presetListURIs.contains(presetURI)))
  1439. presetListURIs.append(presetURI);
  1440. }
  1441. presetListURIs.sort();
  1442. rdfDescriptor->PresetCount = static_cast<uint32_t>(presetListURIs.count());
  1443. #else
  1444. water::StringArray presetListURIs;
  1445. LILV_FOREACH(nodes, it, presetNodes)
  1446. {
  1447. Lilv::Node presetNode(presetNodes.get(it));
  1448. water::String presetURI(presetNode.as_uri());
  1449. if (presetURI.trim().isNotEmpty())
  1450. presetListURIs.addIfNotAlreadyThere(presetURI);
  1451. }
  1452. presetListURIs.sortNatural();
  1453. rdfDescriptor->PresetCount = static_cast<uint32_t>(presetListURIs.size());
  1454. #endif
  1455. // create presets with unique URIs
  1456. rdfDescriptor->Presets = new LV2_RDF_Preset[rdfDescriptor->PresetCount];
  1457. // set preset data
  1458. LILV_FOREACH(nodes, it, presetNodes)
  1459. {
  1460. Lilv::Node presetNode(presetNodes.get(it));
  1461. const char* const presetURI(presetNode.as_uri());
  1462. CARLA_SAFE_ASSERT_CONTINUE(presetURI != nullptr && presetURI[0] != '\0');
  1463. // try to find label without loading the preset resource first
  1464. Lilv::Nodes presetLabelNodes(lv2World.find_nodes(presetNode, lv2World.rdfs_label, nullptr));
  1465. // failed, try loading resource
  1466. if (presetLabelNodes.size() == 0)
  1467. {
  1468. // if loading resource fails, skip this preset
  1469. if (lv2World.load_resource(presetNode) == -1)
  1470. continue;
  1471. // ok, let's try again
  1472. presetLabelNodes = lv2World.find_nodes(presetNode, lv2World.rdfs_label, nullptr);
  1473. }
  1474. if (presetLabelNodes.size() > 0)
  1475. {
  1476. #ifdef USE_QT
  1477. const int index(presetListURIs.indexOf(QString(presetURI)));
  1478. #else
  1479. const int index(presetListURIs.indexOf(water::String(presetURI)));
  1480. #endif
  1481. CARLA_SAFE_ASSERT_CONTINUE(index >= 0 && index < static_cast<int>(rdfDescriptor->PresetCount));
  1482. LV2_RDF_Preset* const rdfPreset(&rdfDescriptor->Presets[index]);
  1483. // ---------------------------------------------------
  1484. // Set Preset Information
  1485. rdfPreset->URI = carla_strdup(presetURI);
  1486. if (const char* const label = presetLabelNodes.get_first().as_string())
  1487. rdfPreset->Label = carla_strdup(label);
  1488. lilv_nodes_free(const_cast<LilvNodes*>(presetLabelNodes.me));
  1489. }
  1490. }
  1491. }
  1492. lilv_nodes_free(const_cast<LilvNodes*>(presetNodes.me));
  1493. }
  1494. // ----------------------------------------------------------------------------------------------------------------
  1495. // Set Plugin Features
  1496. {
  1497. Lilv::Nodes lilvFeatureNodes(lilvPlugin.get_supported_features());
  1498. if (lilvFeatureNodes.size() > 0)
  1499. {
  1500. Lilv::Nodes lilvFeatureNodesR(lilvPlugin.get_required_features());
  1501. rdfDescriptor->FeatureCount = lilvFeatureNodes.size();
  1502. rdfDescriptor->Features = new LV2_RDF_Feature[rdfDescriptor->FeatureCount];
  1503. uint32_t h = 0;
  1504. LILV_FOREACH(nodes, it, lilvFeatureNodes)
  1505. {
  1506. CARLA_SAFE_ASSERT_BREAK(h < rdfDescriptor->FeatureCount);
  1507. Lilv::Node lilvFeatureNode(lilvFeatureNodes.get(it));
  1508. LV2_RDF_Feature* const rdfFeature(&rdfDescriptor->Features[h++]);
  1509. rdfFeature->Required = lilvFeatureNodesR.contains(lilvFeatureNode);
  1510. if (const char* const featureURI = lilvFeatureNode.as_uri())
  1511. rdfFeature->URI = carla_strdup(featureURI);
  1512. else
  1513. rdfFeature->URI = nullptr;
  1514. }
  1515. lilv_nodes_free(const_cast<LilvNodes*>(lilvFeatureNodesR.me));
  1516. }
  1517. lilv_nodes_free(const_cast<LilvNodes*>(lilvFeatureNodes.me));
  1518. }
  1519. // ----------------------------------------------------------------------------------------------------------------
  1520. // Set Plugin Extensions
  1521. {
  1522. Lilv::Nodes lilvExtensionDataNodes(lilvPlugin.get_extension_data());
  1523. if (lilvExtensionDataNodes.size() > 0)
  1524. {
  1525. rdfDescriptor->ExtensionCount = lilvExtensionDataNodes.size();
  1526. rdfDescriptor->Extensions = new LV2_URI[rdfDescriptor->ExtensionCount];
  1527. uint32_t h = 0;
  1528. LILV_FOREACH(nodes, it, lilvExtensionDataNodes)
  1529. {
  1530. CARLA_SAFE_ASSERT_BREAK(h < rdfDescriptor->ExtensionCount);
  1531. Lilv::Node lilvExtensionDataNode(lilvExtensionDataNodes.get(it));
  1532. LV2_URI* const rdfExtension(&rdfDescriptor->Extensions[h++]);
  1533. if (lilvExtensionDataNode.is_uri())
  1534. {
  1535. if (const char* const extURI = lilvExtensionDataNode.as_uri())
  1536. {
  1537. *rdfExtension = carla_strdup(extURI);
  1538. continue;
  1539. }
  1540. }
  1541. *rdfExtension = nullptr;
  1542. }
  1543. for (uint32_t x=h; x < rdfDescriptor->ExtensionCount; ++x)
  1544. rdfDescriptor->Extensions[x] = nullptr;
  1545. }
  1546. lilv_nodes_free(const_cast<LilvNodes*>(lilvExtensionDataNodes.me));
  1547. }
  1548. // ----------------------------------------------------------------------------------------------------------------
  1549. // Set Plugin UIs
  1550. {
  1551. Lilv::UIs lilvUIs(lilvPlugin.get_uis());
  1552. if (lilvUIs.size() > 0)
  1553. {
  1554. rdfDescriptor->UICount = lilvUIs.size();
  1555. rdfDescriptor->UIs = new LV2_RDF_UI[rdfDescriptor->UICount];
  1556. uint32_t h = 0;
  1557. LILV_FOREACH(uis, it, lilvUIs)
  1558. {
  1559. CARLA_SAFE_ASSERT_BREAK(h < rdfDescriptor->UICount);
  1560. Lilv::UI lilvUI(lilvUIs.get(it));
  1561. LV2_RDF_UI* const rdfUI(&rdfDescriptor->UIs[h++]);
  1562. // ----------------------------------------------------------------------------------------------------
  1563. // Set UI Type
  1564. /**/ if (lilvUI.is_a(lv2World.ui_gtk2))
  1565. rdfUI->Type = LV2_UI_GTK2;
  1566. else if (lilvUI.is_a(lv2World.ui_gtk3))
  1567. rdfUI->Type = LV2_UI_GTK3;
  1568. else if (lilvUI.is_a(lv2World.ui_qt4))
  1569. rdfUI->Type = LV2_UI_QT4;
  1570. else if (lilvUI.is_a(lv2World.ui_qt5))
  1571. rdfUI->Type = LV2_UI_QT5;
  1572. else if (lilvUI.is_a(lv2World.ui_cocoa))
  1573. rdfUI->Type = LV2_UI_COCOA;
  1574. else if (lilvUI.is_a(lv2World.ui_windows))
  1575. rdfUI->Type = LV2_UI_WINDOWS;
  1576. else if (lilvUI.is_a(lv2World.ui_x11))
  1577. rdfUI->Type = LV2_UI_X11;
  1578. else if (lilvUI.is_a(lv2World.ui_external))
  1579. rdfUI->Type = LV2_UI_EXTERNAL;
  1580. else if (lilvUI.is_a(lv2World.ui_externalOld))
  1581. rdfUI->Type = LV2_UI_OLD_EXTERNAL;
  1582. else if (lilvUI.is_a(lv2World.ui_externalOld2))
  1583. pass();
  1584. else
  1585. carla_stderr("lv2_rdf_new(\"%s\") - UI '%s' is of unknown type", uri, lilvUI.get_uri().as_uri());
  1586. // ----------------------------------------------------------------------------------------------------
  1587. // Set UI Information
  1588. {
  1589. if (const char* const uiURI = lilvUI.get_uri().as_uri())
  1590. rdfUI->URI = carla_strdup(uiURI);
  1591. if (const char* const uiBinary = lilvUI.get_binary_uri().as_string())
  1592. rdfUI->Binary = carla_strdup_free(lilv_file_uri_parse(uiBinary, nullptr));
  1593. if (const char* const uiBundle = lilvUI.get_bundle_uri().as_string())
  1594. rdfUI->Bundle = carla_strdup_free(lilv_file_uri_parse(uiBundle, nullptr));
  1595. }
  1596. // ----------------------------------------------------------------------------------------------------
  1597. // Set UI Features
  1598. {
  1599. Lilv::Nodes lilvFeatureNodes(lilvUI.get_supported_features());
  1600. if (lilvFeatureNodes.size() > 0)
  1601. {
  1602. Lilv::Nodes lilvFeatureNodesR(lilvUI.get_required_features());
  1603. rdfUI->FeatureCount = lilvFeatureNodes.size();
  1604. rdfUI->Features = new LV2_RDF_Feature[rdfUI->FeatureCount];
  1605. uint32_t h2 = 0;
  1606. LILV_FOREACH(nodes, it2, lilvFeatureNodes)
  1607. {
  1608. CARLA_SAFE_ASSERT_BREAK(h2 < rdfUI->FeatureCount);
  1609. Lilv::Node lilvFeatureNode(lilvFeatureNodes.get(it2));
  1610. LV2_RDF_Feature* const rdfFeature(&rdfUI->Features[h2++]);
  1611. rdfFeature->Required = lilvFeatureNodesR.contains(lilvFeatureNode);
  1612. if (const char* const featureURI = lilvFeatureNode.as_uri())
  1613. rdfFeature->URI = carla_strdup(featureURI);
  1614. else
  1615. rdfFeature->URI = nullptr;
  1616. }
  1617. lilv_nodes_free(const_cast<LilvNodes*>(lilvFeatureNodesR.me));
  1618. }
  1619. lilv_nodes_free(const_cast<LilvNodes*>(lilvFeatureNodes.me));
  1620. }
  1621. // ----------------------------------------------------------------------------------------------------
  1622. // Set UI Extensions
  1623. {
  1624. Lilv::Nodes lilvExtensionDataNodes(lilvUI.get_extension_data());
  1625. if (lilvExtensionDataNodes.size() > 0)
  1626. {
  1627. rdfUI->ExtensionCount = lilvExtensionDataNodes.size();
  1628. rdfUI->Extensions = new LV2_URI[rdfUI->ExtensionCount];
  1629. uint32_t h2 = 0;
  1630. LILV_FOREACH(nodes, it2, lilvExtensionDataNodes)
  1631. {
  1632. CARLA_SAFE_ASSERT_BREAK(h2 < rdfUI->ExtensionCount);
  1633. Lilv::Node lilvExtensionDataNode(lilvExtensionDataNodes.get(it2));
  1634. LV2_URI* const rdfExtension(&rdfUI->Extensions[h2++]);
  1635. if (lilvExtensionDataNode.is_uri())
  1636. {
  1637. if (const char* const extURI = lilvExtensionDataNode.as_uri())
  1638. {
  1639. *rdfExtension = carla_strdup(extURI);
  1640. continue;
  1641. }
  1642. }
  1643. *rdfExtension = nullptr;
  1644. }
  1645. for (uint32_t x2=h2; x2 < rdfUI->ExtensionCount; ++x2)
  1646. rdfUI->Extensions[x2] = nullptr;
  1647. }
  1648. lilv_nodes_free(const_cast<LilvNodes*>(lilvExtensionDataNodes.me));
  1649. }
  1650. }
  1651. }
  1652. lilv_nodes_free(const_cast<LilvNodes*>(lilvUIs.me));
  1653. }
  1654. return rdfDescriptor;
  1655. }
  1656. // --------------------------------------------------------------------------------------------------------------------
  1657. // Check if we support a plugin port
  1658. static inline
  1659. bool is_lv2_port_supported(const LV2_Property types) noexcept
  1660. {
  1661. if (LV2_IS_PORT_CONTROL(types))
  1662. return true;
  1663. if (LV2_IS_PORT_AUDIO(types))
  1664. return true;
  1665. if (LV2_IS_PORT_CV(types))
  1666. return true;
  1667. if (LV2_IS_PORT_ATOM_SEQUENCE(types))
  1668. return true;
  1669. if (LV2_IS_PORT_EVENT(types))
  1670. return true;
  1671. if (LV2_IS_PORT_MIDI_LL(types))
  1672. return true;
  1673. return false;
  1674. }
  1675. // --------------------------------------------------------------------------------------------------------------------
  1676. // Check if we support a plugin feature
  1677. static inline
  1678. bool is_lv2_feature_supported(const LV2_URI uri) noexcept
  1679. {
  1680. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0', false);
  1681. if (std::strcmp(uri, LV2_BUF_SIZE__boundedBlockLength) == 0)
  1682. return true;
  1683. if (std::strcmp(uri, LV2_BUF_SIZE__fixedBlockLength) == 0)
  1684. return true;
  1685. if (std::strcmp(uri, LV2_BUF_SIZE__powerOf2BlockLength) == 0)
  1686. return true;
  1687. if (std::strcmp(uri, LV2_CORE__hardRTCapable) == 0)
  1688. return true;
  1689. if (std::strcmp(uri, LV2_CORE__inPlaceBroken) == 0)
  1690. return true;
  1691. if (std::strcmp(uri, LV2_CORE__isLive) == 0)
  1692. return true;
  1693. if (std::strcmp(uri, LV2_EVENT_URI) == 0)
  1694. return true;
  1695. if (std::strcmp(uri, LV2_LOG__log) == 0)
  1696. return true;
  1697. if (std::strcmp(uri, LV2_OPTIONS__options) == 0)
  1698. return true;
  1699. if (std::strcmp(uri, LV2_PROGRAMS__Host) == 0)
  1700. return true;
  1701. if (std::strcmp(uri, LV2_RESIZE_PORT__resize) == 0)
  1702. return true;
  1703. if (std::strcmp(uri, LV2_RTSAFE_MEMORY_POOL__Pool) == 0)
  1704. return true;
  1705. if (std::strcmp(uri, LV2_RTSAFE_MEMORY_POOL_DEPRECATED_URI) == 0)
  1706. return true;
  1707. if (std::strcmp(uri, LV2_STATE__loadDefaultState) == 0)
  1708. return true;
  1709. if (std::strcmp(uri, LV2_STATE__makePath) == 0)
  1710. return true;
  1711. if (std::strcmp(uri, LV2_STATE__mapPath) == 0)
  1712. return true;
  1713. if (std::strcmp(uri, LV2_PORT_PROPS__supportsStrictBounds) == 0)
  1714. return true;
  1715. if (std::strcmp(uri, LV2_URI_MAP_URI) == 0)
  1716. return true;
  1717. if (std::strcmp(uri, LV2_URID__map) == 0)
  1718. return true;
  1719. if (std::strcmp(uri, LV2_URID__unmap) == 0)
  1720. return true;
  1721. if (std::strcmp(uri, LV2_WORKER__schedule) == 0)
  1722. return true;
  1723. return false;
  1724. }
  1725. // --------------------------------------------------------------------------------------------------------------------
  1726. // Check if we support a plugin or UI feature
  1727. static inline
  1728. bool is_lv2_ui_feature_supported(const LV2_URI uri) noexcept
  1729. {
  1730. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0', false);
  1731. if (is_lv2_feature_supported(uri))
  1732. return true;
  1733. #ifndef BUILD_BRIDGE_UI
  1734. if (std::strcmp(uri, LV2_DATA_ACCESS_URI) == 0)
  1735. return true;
  1736. if (std::strcmp(uri, LV2_INSTANCE_ACCESS_URI) == 0)
  1737. return true;
  1738. #endif
  1739. if (std::strcmp(uri, LV2_UI__fixedSize) == 0)
  1740. return true;
  1741. if (std::strcmp(uri, LV2_UI__idleInterface) == 0)
  1742. return true;
  1743. if (std::strcmp(uri, LV2_UI__makeResident) == 0)
  1744. return true;
  1745. if (std::strcmp(uri, LV2_UI__makeSONameResident) == 0)
  1746. return true;
  1747. if (std::strcmp(uri, LV2_UI__noUserResize) == 0)
  1748. return true;
  1749. if (std::strcmp(uri, LV2_UI__parent) == 0)
  1750. return true;
  1751. if (std::strcmp(uri, LV2_UI__portMap) == 0)
  1752. return true;
  1753. if (std::strcmp(uri, LV2_UI__portSubscribe) == 0)
  1754. return true;
  1755. if (std::strcmp(uri, LV2_UI__resize) == 0)
  1756. return true;
  1757. if (std::strcmp(uri, LV2_UI__touch) == 0)
  1758. return true;
  1759. if (std::strcmp(uri, LV2_EXTERNAL_UI__Widget) == 0)
  1760. return true;
  1761. if (std::strcmp(uri, LV2_EXTERNAL_UI_DEPRECATED_URI) == 0)
  1762. return true;
  1763. return false;
  1764. }
  1765. // --------------------------------------------------------------------------------------------------------------------
  1766. #endif // CARLA_LV2_UTILS_HPP_INCLUDED