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.

2604 lines
104KB

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