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.

2688 lines
108KB

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