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.

2984 lines
122KB

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