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.

3005 lines
123KB

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