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.

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