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.

2665 lines
107KB

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