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.

2953 lines
121KB

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