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.

CarlaLv2Utils.hpp 136KB

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