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.

Lv2Plugin.cpp 171KB

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
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
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
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
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852
  1. /*
  2. * Carla LV2 Plugin
  3. * Copyright (C) 2011-2013 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 GPL.txt file
  16. */
  17. #include "CarlaPluginInternal.hpp"
  18. #ifdef WANT_LV2
  19. #include "CarlaPluginGui.hpp"
  20. #include "CarlaEngineOsc.hpp"
  21. #include "CarlaLv2Utils.hpp"
  22. #include "Lv2AtomQueue.hpp"
  23. #include <QtCore/QDir>
  24. extern "C" {
  25. #include "rtmempool/rtmempool-lv2.h"
  26. }
  27. // -----------------------------------------------------
  28. // Our LV2 World class object
  29. Lv2WorldClass gLv2World;
  30. // -----------------------------------------------------
  31. CARLA_BACKEND_START_NAMESPACE
  32. #if 0
  33. }
  34. #endif
  35. /*!
  36. * @defgroup PluginHints Plugin Hints
  37. * @{
  38. */
  39. const unsigned int PLUGIN_HAS_EXTENSION_OPTIONS = 0x100; //!< LV2 Plugin has Options extension
  40. const unsigned int PLUGIN_HAS_EXTENSION_PROGRAMS = 0x200; //!< LV2 Plugin has Programs extension
  41. const unsigned int PLUGIN_HAS_EXTENSION_STATE = 0x400; //!< LV2 Plugin has State extension
  42. const unsigned int PLUGIN_HAS_EXTENSION_WORKER = 0x800; //!< LV2 Plugin has Worker extension
  43. /**@}*/
  44. /*!
  45. * @defgroup ParameterHints Parameter Hints
  46. * @{
  47. */
  48. const unsigned int PARAMETER_IS_STRICT_BOUNDS = 0x1000; //!< LV2 Parameter needs strict bounds
  49. const unsigned int PARAMETER_IS_TRIGGER = 0x2000; //!< LV2 Parameter is trigger (current value should be changed to its default after run())
  50. /**@}*/
  51. /*!
  52. * @defgroup Lv2EventTypes LV2 Event Data/Types
  53. *
  54. * Data and buffer types for LV2 EventData ports.
  55. * @{
  56. */
  57. const unsigned int CARLA_EVENT_DATA_ATOM = 0x01;
  58. const unsigned int CARLA_EVENT_DATA_EVENT = 0x02;
  59. const unsigned int CARLA_EVENT_DATA_MIDI_LL = 0x04;
  60. const unsigned int CARLA_EVENT_TYPE_MESSAGE = 0x10;
  61. const unsigned int CARLA_EVENT_TYPE_MIDI = 0x20;
  62. const unsigned int CARLA_EVENT_TYPE_TIME = 0x40;
  63. /**@}*/
  64. /*!
  65. * @defgroup Lv2UriMapIds LV2 URI Map Ids
  66. *
  67. * Static index list of the internal LV2 URI Map Ids.
  68. * @{
  69. */
  70. const uint32_t CARLA_URI_MAP_ID_NULL = 0;
  71. const uint32_t CARLA_URI_MAP_ID_ATOM_CHUNK = 1;
  72. const uint32_t CARLA_URI_MAP_ID_ATOM_DOUBLE = 2;
  73. const uint32_t CARLA_URI_MAP_ID_ATOM_INT = 3;
  74. const uint32_t CARLA_URI_MAP_ID_ATOM_PATH = 4;
  75. const uint32_t CARLA_URI_MAP_ID_ATOM_SEQUENCE = 5;
  76. const uint32_t CARLA_URI_MAP_ID_ATOM_STRING = 6;
  77. const uint32_t CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM = 7;
  78. const uint32_t CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT = 8;
  79. const uint32_t CARLA_URI_MAP_ID_BUF_MAX_LENGTH = 9;
  80. const uint32_t CARLA_URI_MAP_ID_BUF_MIN_LENGTH = 10;
  81. const uint32_t CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE = 11;
  82. const uint32_t CARLA_URI_MAP_ID_LOG_ERROR = 12;
  83. const uint32_t CARLA_URI_MAP_ID_LOG_NOTE = 13;
  84. const uint32_t CARLA_URI_MAP_ID_LOG_TRACE = 14;
  85. const uint32_t CARLA_URI_MAP_ID_LOG_WARNING = 15;
  86. const uint32_t CARLA_URI_MAP_ID_MIDI_EVENT = 16;
  87. const uint32_t CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE = 17;
  88. const uint32_t CARLA_URI_MAP_ID_TIME_POSITION = 18;
  89. const uint32_t CARLA_URI_MAP_ID_COUNT = 19;
  90. /**@}*/
  91. /*!
  92. * @defgroup Lv2FeatureIds LV2 Feature Ids
  93. *
  94. * Static index list of the internal LV2 Feature Ids.
  95. * @{
  96. */
  97. const uint32_t kFeatureIdBufSizeBounded = 0;
  98. const uint32_t kFeatureIdBufSizeFixed = 1;
  99. const uint32_t kFeatureIdBufSizePowerOf2 = 2;
  100. const uint32_t kFeatureIdEvent = 3;
  101. const uint32_t kFeatureIdLogs = 4;
  102. const uint32_t kFeatureIdOptions = 5;
  103. const uint32_t kFeatureIdPrograms = 6;
  104. const uint32_t kFeatureIdRtMemPool = 7;
  105. const uint32_t kFeatureIdStateMakePath = 8;
  106. const uint32_t kFeatureIdStateMapPath = 9;
  107. const uint32_t kFeatureIdStrictBounds = 10;
  108. const uint32_t kFeatureIdUriMap = 11;
  109. const uint32_t kFeatureIdUridMap = 12;
  110. const uint32_t kFeatureIdUridUnmap = 13;
  111. const uint32_t kFeatureIdWorker = 14;
  112. const uint32_t kFeatureIdUiDataAccess = 15;
  113. const uint32_t kFeatureIdUiInstanceAccess = 16;
  114. const uint32_t kFeatureIdUiParent = 17;
  115. const uint32_t kFeatureIdUiPortMap = 18;
  116. const uint32_t kFeatureIdUiResize = 19;
  117. const uint32_t kFeatureIdExternalUi = 20;
  118. const uint32_t kFeatureIdExternalUiOld = 21;
  119. const uint32_t kFeatureCount = 22;
  120. /**@}*/
  121. const unsigned int MAX_EVENT_BUFFER = 8192; // 0x2000
  122. enum Lv2PluginGuiType {
  123. PLUGIN_UI_NULL,
  124. PLUGIN_UI_OSC,
  125. PLUGIN_UI_QT,
  126. PLUGIN_UI_PARENT,
  127. PLUGIN_UI_EXTERNAL
  128. };
  129. struct Lv2EventData {
  130. uint32_t type;
  131. uint32_t rindex;
  132. CarlaEngineEventPort* port;
  133. union {
  134. LV2_Atom_Sequence* atom;
  135. LV2_Event_Buffer* event;
  136. LV2_MIDI* midi;
  137. };
  138. Lv2EventData()
  139. : type(0x0),
  140. rindex(0),
  141. port(nullptr) {}
  142. ~Lv2EventData()
  143. {
  144. if (port != nullptr)
  145. {
  146. delete port;
  147. port = nullptr;
  148. }
  149. if (type & CARLA_EVENT_DATA_ATOM)
  150. {
  151. CARLA_ASSERT(atom != nullptr);
  152. if (atom != nullptr)
  153. {
  154. std::free(atom);
  155. atom = nullptr;
  156. }
  157. }
  158. else if (type & CARLA_EVENT_DATA_EVENT)
  159. {
  160. CARLA_ASSERT(event != nullptr);
  161. if (event != nullptr)
  162. {
  163. std::free(event);
  164. event = nullptr;
  165. }
  166. }
  167. else if (type & CARLA_EVENT_DATA_MIDI_LL)
  168. {
  169. CARLA_ASSERT(midi != nullptr && midi->data != nullptr);
  170. if (midi != nullptr)
  171. {
  172. if (midi->data != nullptr)
  173. {
  174. delete[] midi->data;
  175. midi->data = nullptr;
  176. }
  177. delete midi;
  178. midi = nullptr;
  179. }
  180. }
  181. type = 0x0;
  182. }
  183. CARLA_DECLARE_NON_COPY_STRUCT_WITH_LEAK_DETECTOR(Lv2EventData)
  184. };
  185. struct Lv2PluginEventData {
  186. uint32_t count;
  187. Lv2EventData* data;
  188. Lv2EventData* ctrl; // default port
  189. uint32_t ctrlIndex;
  190. Lv2PluginEventData()
  191. : count(0),
  192. data(nullptr),
  193. ctrl(nullptr),
  194. ctrlIndex(0) {}
  195. ~Lv2PluginEventData()
  196. {
  197. CARLA_ASSERT_INT(count == 0, count);
  198. CARLA_ASSERT(data == nullptr);
  199. CARLA_ASSERT(ctrl == nullptr);
  200. CARLA_ASSERT(ctrlIndex == 0);
  201. }
  202. void createNew(const uint32_t newCount)
  203. {
  204. CARLA_ASSERT_INT(count == 0, count);
  205. CARLA_ASSERT(data == nullptr);
  206. CARLA_ASSERT(ctrl == nullptr);
  207. CARLA_ASSERT(ctrlIndex == 0);
  208. CARLA_ASSERT_INT(newCount > 0, newCount);
  209. if (data != nullptr || newCount == 0)
  210. return;
  211. data = new Lv2EventData[newCount];
  212. count = newCount;
  213. ctrl = nullptr;
  214. ctrlIndex = 0;
  215. }
  216. void clear()
  217. {
  218. if (data != nullptr)
  219. {
  220. for (uint32_t i=0; i < count; ++i)
  221. {
  222. if (data[i].port != nullptr && ctrl != nullptr && data[i].port == ctrl->port)
  223. data[i].port = nullptr;
  224. }
  225. delete[] data;
  226. data = nullptr;
  227. }
  228. count = 0;
  229. ctrl = nullptr;
  230. ctrlIndex = 0;
  231. }
  232. void initBuffers(CarlaEngine* const engine)
  233. {
  234. for (uint32_t i=0; i < count; ++i)
  235. {
  236. if (data[i].port != nullptr && (ctrl == nullptr || data[i].port != ctrl->port))
  237. data[i].port->initBuffer(engine);
  238. }
  239. }
  240. CARLA_DECLARE_NON_COPY_STRUCT_WITH_LEAK_DETECTOR(Lv2PluginEventData)
  241. };
  242. struct Lv2PluginOptions {
  243. int minBufferSize;
  244. int maxBufferSize;
  245. int sequenceSize;
  246. double sampleRate;
  247. LV2_Options_Option optMinBlockLenth;
  248. LV2_Options_Option optMaxBlockLenth;
  249. LV2_Options_Option optSequenceSize;
  250. LV2_Options_Option optSampleRate;
  251. LV2_Options_Option optNull;
  252. LV2_Options_Option* opts[5];
  253. Lv2PluginOptions()
  254. : minBufferSize(0),
  255. maxBufferSize(0),
  256. sequenceSize(MAX_EVENT_BUFFER),
  257. sampleRate(0.0)
  258. {
  259. optMinBlockLenth.context = LV2_OPTIONS_INSTANCE;
  260. optMinBlockLenth.subject = 0;
  261. optMinBlockLenth.key = CARLA_URI_MAP_ID_BUF_MIN_LENGTH;
  262. optMinBlockLenth.size = sizeof(int);
  263. optMinBlockLenth.type = CARLA_URI_MAP_ID_ATOM_INT;
  264. optMinBlockLenth.value = &minBufferSize;
  265. optMaxBlockLenth.context = LV2_OPTIONS_INSTANCE;
  266. optMaxBlockLenth.subject = 0;
  267. optMaxBlockLenth.key = CARLA_URI_MAP_ID_BUF_MAX_LENGTH;
  268. optMaxBlockLenth.size = sizeof(int);
  269. optMaxBlockLenth.type = CARLA_URI_MAP_ID_ATOM_INT;
  270. optMaxBlockLenth.value = &maxBufferSize;
  271. optSequenceSize.context = LV2_OPTIONS_INSTANCE;
  272. optSequenceSize.subject = 0;
  273. optSequenceSize.key = CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE;
  274. optSequenceSize.size = sizeof(int);
  275. optSequenceSize.type = CARLA_URI_MAP_ID_ATOM_INT;
  276. optSequenceSize.value = &sequenceSize;
  277. optSampleRate.context = LV2_OPTIONS_INSTANCE;
  278. optSampleRate.subject = 0;
  279. optSampleRate.key = CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE;
  280. optSampleRate.size = sizeof(double);
  281. optSampleRate.type = CARLA_URI_MAP_ID_ATOM_DOUBLE;
  282. optSampleRate.value = &sampleRate;
  283. optNull.context = LV2_OPTIONS_INSTANCE;
  284. optNull.subject = 0;
  285. optNull.key = CARLA_URI_MAP_ID_NULL;
  286. optNull.size = 0;
  287. optNull.type = CARLA_URI_MAP_ID_NULL;
  288. optNull.value = nullptr;
  289. opts[0] = &optMinBlockLenth;
  290. opts[1] = &optMaxBlockLenth;
  291. opts[2] = &optSequenceSize;
  292. opts[3] = &optSampleRate;
  293. opts[4] = &optNull;
  294. }
  295. CARLA_DECLARE_NON_COPY_STRUCT_WITH_LEAK_DETECTOR(Lv2PluginOptions)
  296. };
  297. LV2_Atom_Event* getLv2AtomEvent(LV2_Atom_Sequence* const atom, const uint32_t offset)
  298. {
  299. return (LV2_Atom_Event*)((char*)LV2_ATOM_CONTENTS(LV2_Atom_Sequence, atom) + offset);
  300. }
  301. // -----------------------------------------------------
  302. class Lv2Plugin : public CarlaPlugin,
  303. public CarlaPluginGui::Callback
  304. {
  305. public:
  306. Lv2Plugin(CarlaEngine* const engine, const unsigned int id)
  307. : CarlaPlugin(engine, id),
  308. fHandle(nullptr),
  309. fHandle2(nullptr),
  310. fFeatures{nullptr},
  311. fDescriptor(nullptr),
  312. fRdfDescriptor(nullptr),
  313. fAudioInBuffers(nullptr),
  314. fAudioOutBuffers(nullptr),
  315. fParamBuffers(nullptr)
  316. {
  317. carla_debug("Lv2Plugin::Lv2Plugin(%p, %i)", engine, id);
  318. kData->osc.thread.setMode(CarlaPluginThread::PLUGIN_THREAD_LV2_GUI);
  319. for (uint32_t i=0; i < CARLA_URI_MAP_ID_COUNT; ++i)
  320. fCustomURIDs.append(nullptr);
  321. }
  322. ~Lv2Plugin() override
  323. {
  324. carla_debug("Lv2Plugin::~Lv2Plugin()");
  325. // close UI
  326. if (fUi.type != PLUGIN_UI_NULL)
  327. {
  328. showGui(false);
  329. if (fUi.type == PLUGIN_UI_OSC)
  330. {
  331. // Wait a bit first, then force kill
  332. if (kData->osc.thread.isRunning() && ! kData->osc.thread.wait(kData->engine->getOptions().oscUiTimeout))
  333. {
  334. carla_stderr("LV2 OSC-GUI thread still running, forcing termination now");
  335. kData->osc.thread.terminate();
  336. }
  337. }
  338. else
  339. {
  340. if (fFeatures[kFeatureIdUiDataAccess] != nullptr && fFeatures[kFeatureIdUiDataAccess]->data != nullptr)
  341. delete (LV2_Extension_Data_Feature*)fFeatures[kFeatureIdUiDataAccess]->data;
  342. if (fFeatures[kFeatureIdUiPortMap] != nullptr && fFeatures[kFeatureIdUiPortMap]->data != nullptr)
  343. delete (LV2UI_Port_Map*)fFeatures[kFeatureIdUiPortMap]->data;
  344. if (fFeatures[kFeatureIdUiResize] != nullptr && fFeatures[kFeatureIdUiResize]->data != nullptr)
  345. delete (LV2UI_Resize*)fFeatures[kFeatureIdUiResize]->data;
  346. if (fFeatures[kFeatureIdExternalUi] != nullptr && fFeatures[kFeatureIdExternalUi]->data != nullptr)
  347. {
  348. const LV2_External_UI_Host* const uiHost((const LV2_External_UI_Host*)fFeatures[kFeatureIdExternalUi]->data);
  349. if (uiHost->plugin_human_id != nullptr)
  350. delete[] uiHost->plugin_human_id;
  351. delete uiHost;
  352. }
  353. fUi.descriptor = nullptr;
  354. kData->uiLibClose();
  355. }
  356. fUi.rdfDescriptor = nullptr;
  357. }
  358. kData->singleMutex.lock();
  359. kData->masterMutex.lock();
  360. if (kData->active)
  361. {
  362. deactivate();
  363. kData->active = false;
  364. }
  365. if (fDescriptor != nullptr)
  366. {
  367. if (fDescriptor->cleanup != nullptr)
  368. {
  369. if (fHandle != nullptr)
  370. fDescriptor->cleanup(fHandle);
  371. if (fHandle2 != nullptr)
  372. fDescriptor->cleanup(fHandle2);
  373. }
  374. fHandle = nullptr;
  375. fHandle2 = nullptr;
  376. fDescriptor = nullptr;
  377. }
  378. if (fRdfDescriptor != nullptr)
  379. {
  380. delete fRdfDescriptor;
  381. fRdfDescriptor = nullptr;
  382. }
  383. if (fFeatures[kFeatureIdEvent] != nullptr && fFeatures[kFeatureIdEvent]->data != nullptr)
  384. delete (LV2_Event_Feature*)fFeatures[kFeatureIdEvent]->data;
  385. if (fFeatures[kFeatureIdLogs] != nullptr && fFeatures[kFeatureIdLogs]->data != nullptr)
  386. delete (LV2_Log_Log*)fFeatures[kFeatureIdLogs]->data;
  387. if (fFeatures[kFeatureIdStateMakePath] != nullptr && fFeatures[kFeatureIdStateMakePath]->data != nullptr)
  388. delete (LV2_State_Make_Path*)fFeatures[kFeatureIdStateMakePath]->data;
  389. if (fFeatures[kFeatureIdStateMapPath] != nullptr && fFeatures[kFeatureIdStateMapPath]->data != nullptr)
  390. delete (LV2_State_Map_Path*)fFeatures[kFeatureIdStateMapPath]->data;
  391. if (fFeatures[kFeatureIdPrograms] != nullptr && fFeatures[kFeatureIdPrograms]->data != nullptr)
  392. delete (LV2_Programs_Host*)fFeatures[kFeatureIdPrograms]->data;
  393. if (fFeatures[kFeatureIdRtMemPool] != nullptr && fFeatures[kFeatureIdRtMemPool]->data != nullptr)
  394. delete (LV2_RtMemPool_Pool*)fFeatures[kFeatureIdRtMemPool]->data;
  395. if (fFeatures[kFeatureIdUriMap] != nullptr && fFeatures[kFeatureIdUriMap]->data != nullptr)
  396. delete (LV2_URI_Map_Feature*)fFeatures[kFeatureIdUriMap]->data;
  397. if (fFeatures[kFeatureIdUridMap] != nullptr && fFeatures[kFeatureIdUridMap]->data != nullptr)
  398. delete (LV2_URID_Map*)fFeatures[kFeatureIdUridMap]->data;
  399. if (fFeatures[kFeatureIdUridUnmap] != nullptr && fFeatures[kFeatureIdUridUnmap]->data != nullptr)
  400. delete (LV2_URID_Unmap*)fFeatures[kFeatureIdUridUnmap]->data;
  401. if (fFeatures[kFeatureIdWorker] != nullptr && fFeatures[kFeatureIdWorker]->data != nullptr)
  402. delete (LV2_Worker_Schedule*)fFeatures[kFeatureIdWorker]->data;
  403. for (uint32_t i=0; i < kFeatureCount; ++i)
  404. {
  405. if (fFeatures[i] != nullptr)
  406. delete fFeatures[i];
  407. }
  408. for (auto it = fCustomURIDs.begin(); it.valid(); it.next())
  409. {
  410. const char*& uri(*it);
  411. if (uri != nullptr)
  412. {
  413. delete[] uri;
  414. uri = nullptr;
  415. }
  416. }
  417. fCustomURIDs.clear();
  418. clearBuffers();
  419. }
  420. // -------------------------------------------------------------------
  421. // Information (base)
  422. PluginType type() const override
  423. {
  424. return PLUGIN_LV2;
  425. }
  426. PluginCategory category() override
  427. {
  428. CARLA_ASSERT(fRdfDescriptor != nullptr);
  429. const LV2_Property cat1(fRdfDescriptor->Type[0]);
  430. const LV2_Property cat2(fRdfDescriptor->Type[1]);
  431. if (LV2_IS_DELAY(cat1, cat2))
  432. return PLUGIN_CATEGORY_DELAY;
  433. if (LV2_IS_DISTORTION(cat1, cat2))
  434. return PLUGIN_CATEGORY_OTHER;
  435. if (LV2_IS_DYNAMICS(cat1, cat2))
  436. return PLUGIN_CATEGORY_DYNAMICS;
  437. if (LV2_IS_EQ(cat1, cat2))
  438. return PLUGIN_CATEGORY_EQ;
  439. if (LV2_IS_FILTER(cat1, cat2))
  440. return PLUGIN_CATEGORY_FILTER;
  441. if (LV2_IS_GENERATOR(cat1, cat2))
  442. return PLUGIN_CATEGORY_SYNTH;
  443. if (LV2_IS_MODULATOR(cat1, cat2))
  444. return PLUGIN_CATEGORY_MODULATOR;
  445. if (LV2_IS_REVERB(cat1, cat2))
  446. return PLUGIN_CATEGORY_DELAY;
  447. if (LV2_IS_SIMULATOR(cat1, cat2))
  448. return PLUGIN_CATEGORY_OTHER;
  449. if (LV2_IS_SPATIAL(cat1, cat2))
  450. return PLUGIN_CATEGORY_OTHER;
  451. if (LV2_IS_SPECTRAL(cat1, cat2))
  452. return PLUGIN_CATEGORY_UTILITY;
  453. if (LV2_IS_UTILITY(cat1, cat2))
  454. return PLUGIN_CATEGORY_UTILITY;
  455. return getPluginCategoryFromName(fName);
  456. }
  457. long uniqueId() const override
  458. {
  459. CARLA_ASSERT(fRdfDescriptor != nullptr);
  460. return fRdfDescriptor->UniqueID;
  461. }
  462. // -------------------------------------------------------------------
  463. // Information (count)
  464. uint32_t midiInCount() const override
  465. {
  466. CARLA_ASSERT(fRdfDescriptor != nullptr);
  467. uint32_t i, count = 0;
  468. for (i=0; i < fRdfDescriptor->PortCount; ++i)
  469. {
  470. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  471. if (LV2_IS_PORT_INPUT(portTypes) && LV2_PORT_SUPPORTS_MIDI_EVENT(portTypes))
  472. count += 1;
  473. }
  474. return count;
  475. }
  476. uint32_t midiOutCount() const override
  477. {
  478. CARLA_ASSERT(fRdfDescriptor != nullptr);
  479. uint32_t i, count = 0;
  480. for (i=0; i < fRdfDescriptor->PortCount; ++i)
  481. {
  482. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  483. if (LV2_IS_PORT_OUTPUT(portTypes) && LV2_PORT_SUPPORTS_MIDI_EVENT(portTypes))
  484. count += 1;
  485. }
  486. return count;
  487. }
  488. uint32_t parameterScalePointCount(const uint32_t parameterId) const override
  489. {
  490. CARLA_ASSERT(fRdfDescriptor != nullptr);
  491. CARLA_ASSERT(parameterId < kData->param.count);
  492. const int32_t rindex(kData->param.data[parameterId].rindex);
  493. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  494. {
  495. const LV2_RDF_Port& port(fRdfDescriptor->Ports[rindex]);
  496. return port.ScalePointCount;
  497. }
  498. return 0;
  499. }
  500. // -------------------------------------------------------------------
  501. // Information (current data)
  502. // nothing
  503. // -------------------------------------------------------------------
  504. // Information (per-plugin data)
  505. unsigned int availableOptions() override
  506. {
  507. unsigned int options = 0x0;
  508. options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  509. if (! needsFixedBuffer())
  510. options |= PLUGIN_OPTION_FIXED_BUFFER;
  511. if (kData->engine->getProccessMode() != PROCESS_MODE_CONTINUOUS_RACK)
  512. {
  513. if (fOptions & PLUGIN_OPTION_FORCE_STEREO)
  514. options |= PLUGIN_OPTION_FORCE_STEREO;
  515. else if (kData->audioIn.count <= 1 && kData->audioOut.count <= 1 && (kData->audioIn.count != 0 || kData->audioOut.count != 0))
  516. options |= PLUGIN_OPTION_FORCE_STEREO;
  517. }
  518. if (midiInCount() > 0)
  519. {
  520. options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  521. options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  522. options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  523. options |= PLUGIN_OPTION_SEND_PITCHBEND;
  524. options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  525. }
  526. return options;
  527. }
  528. float getParameterValue(const uint32_t parameterId) override
  529. {
  530. CARLA_ASSERT(fParamBuffers != nullptr);
  531. CARLA_ASSERT(parameterId < kData->param.count);
  532. return fParamBuffers[parameterId];
  533. }
  534. float getParameterScalePointValue(const uint32_t parameterId, const uint32_t scalePointId) override
  535. {
  536. CARLA_ASSERT(fRdfDescriptor != nullptr);
  537. CARLA_ASSERT(parameterId < kData->param.count);
  538. CARLA_ASSERT(scalePointId < parameterScalePointCount(parameterId));
  539. const int32_t rindex(kData->param.data[parameterId].rindex);
  540. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  541. {
  542. const LV2_RDF_Port& port(fRdfDescriptor->Ports[rindex]);
  543. if (scalePointId < port.ScalePointCount)
  544. {
  545. const LV2_RDF_PortScalePoint& portScalePoint(port.ScalePoints[scalePointId]);
  546. return portScalePoint.Value;
  547. }
  548. }
  549. return 0.0f;
  550. }
  551. void getLabel(char* const strBuf) override
  552. {
  553. CARLA_ASSERT(fRdfDescriptor != nullptr);
  554. CARLA_ASSERT(fRdfDescriptor->URI != nullptr);
  555. if (fRdfDescriptor->URI != nullptr)
  556. std::strncpy(strBuf, fRdfDescriptor->URI, STR_MAX);
  557. else
  558. CarlaPlugin::getLabel(strBuf);
  559. }
  560. void getMaker(char* const strBuf) override
  561. {
  562. CARLA_ASSERT(fRdfDescriptor != nullptr);
  563. if (fRdfDescriptor->Author != nullptr)
  564. std::strncpy(strBuf, fRdfDescriptor->Author, STR_MAX);
  565. else
  566. CarlaPlugin::getMaker(strBuf);
  567. }
  568. void getCopyright(char* const strBuf) override
  569. {
  570. CARLA_ASSERT(fRdfDescriptor != nullptr);
  571. if (fRdfDescriptor->License != nullptr)
  572. std::strncpy(strBuf, fRdfDescriptor->License, STR_MAX);
  573. else
  574. CarlaPlugin::getCopyright(strBuf);
  575. }
  576. void getRealName(char* const strBuf) override
  577. {
  578. CARLA_ASSERT(fRdfDescriptor != nullptr);
  579. if (fRdfDescriptor->Name != nullptr)
  580. std::strncpy(strBuf, fRdfDescriptor->Name, STR_MAX);
  581. else
  582. CarlaPlugin::getRealName(strBuf);
  583. }
  584. void getParameterName(const uint32_t parameterId, char* const strBuf) override
  585. {
  586. CARLA_ASSERT(fRdfDescriptor != nullptr);
  587. CARLA_ASSERT(parameterId < kData->param.count);
  588. const int32_t rindex(kData->param.data[parameterId].rindex);
  589. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  590. std::strncpy(strBuf, fRdfDescriptor->Ports[rindex].Name, STR_MAX);
  591. else
  592. CarlaPlugin::getParameterName(parameterId, strBuf);
  593. }
  594. void getParameterSymbol(const uint32_t parameterId, char* const strBuf) override
  595. {
  596. CARLA_ASSERT(fRdfDescriptor != nullptr);
  597. CARLA_ASSERT(parameterId < kData->param.count);
  598. const int32_t rindex(kData->param.data[parameterId].rindex);
  599. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  600. strncpy(strBuf, fRdfDescriptor->Ports[rindex].Symbol, STR_MAX);
  601. else
  602. CarlaPlugin::getParameterSymbol(parameterId, strBuf);
  603. }
  604. void getParameterUnit(const uint32_t parameterId, char* const strBuf) override
  605. {
  606. CARLA_ASSERT(fRdfDescriptor != nullptr);
  607. CARLA_ASSERT(parameterId < kData->param.count);
  608. const int32_t rindex(kData->param.data[parameterId].rindex);
  609. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  610. {
  611. const LV2_RDF_Port& port(fRdfDescriptor->Ports[rindex]);
  612. if (LV2_HAVE_PORT_UNIT_SYMBOL(port.Unit.Hints) && port.Unit.Symbol)
  613. std::strncpy(strBuf, port.Unit.Symbol, STR_MAX);
  614. else if (LV2_HAVE_PORT_UNIT_UNIT(port.Unit.Hints))
  615. {
  616. switch (port.Unit.Unit)
  617. {
  618. case LV2_PORT_UNIT_BAR:
  619. std::strncpy(strBuf, "bars", STR_MAX);
  620. return;
  621. case LV2_PORT_UNIT_BEAT:
  622. std::strncpy(strBuf, "beats", STR_MAX);
  623. return;
  624. case LV2_PORT_UNIT_BPM:
  625. std::strncpy(strBuf, "BPM", STR_MAX);
  626. return;
  627. case LV2_PORT_UNIT_CENT:
  628. std::strncpy(strBuf, "ct", STR_MAX);
  629. return;
  630. case LV2_PORT_UNIT_CM:
  631. std::strncpy(strBuf, "cm", STR_MAX);
  632. return;
  633. case LV2_PORT_UNIT_COEF:
  634. std::strncpy(strBuf, "(coef)", STR_MAX);
  635. return;
  636. case LV2_PORT_UNIT_DB:
  637. std::strncpy(strBuf, "dB", STR_MAX);
  638. return;
  639. case LV2_PORT_UNIT_DEGREE:
  640. std::strncpy(strBuf, "deg", STR_MAX);
  641. return;
  642. case LV2_PORT_UNIT_FRAME:
  643. std::strncpy(strBuf, "frames", STR_MAX);
  644. return;
  645. case LV2_PORT_UNIT_HZ:
  646. std::strncpy(strBuf, "Hz", STR_MAX);
  647. return;
  648. case LV2_PORT_UNIT_INCH:
  649. std::strncpy(strBuf, "in", STR_MAX);
  650. return;
  651. case LV2_PORT_UNIT_KHZ:
  652. std::strncpy(strBuf, "kHz", STR_MAX);
  653. return;
  654. case LV2_PORT_UNIT_KM:
  655. std::strncpy(strBuf, "km", STR_MAX);
  656. return;
  657. case LV2_PORT_UNIT_M:
  658. std::strncpy(strBuf, "m", STR_MAX);
  659. return;
  660. case LV2_PORT_UNIT_MHZ:
  661. std::strncpy(strBuf, "MHz", STR_MAX);
  662. return;
  663. case LV2_PORT_UNIT_MIDINOTE:
  664. std::strncpy(strBuf, "note", STR_MAX);
  665. return;
  666. case LV2_PORT_UNIT_MILE:
  667. std::strncpy(strBuf, "mi", STR_MAX);
  668. return;
  669. case LV2_PORT_UNIT_MIN:
  670. std::strncpy(strBuf, "min", STR_MAX);
  671. return;
  672. case LV2_PORT_UNIT_MM:
  673. std::strncpy(strBuf, "mm", STR_MAX);
  674. return;
  675. case LV2_PORT_UNIT_MS:
  676. std::strncpy(strBuf, "ms", STR_MAX);
  677. return;
  678. case LV2_PORT_UNIT_OCT:
  679. std::strncpy(strBuf, "oct", STR_MAX);
  680. return;
  681. case LV2_PORT_UNIT_PC:
  682. std::strncpy(strBuf, "%", STR_MAX);
  683. return;
  684. case LV2_PORT_UNIT_S:
  685. std::strncpy(strBuf, "s", STR_MAX);
  686. return;
  687. case LV2_PORT_UNIT_SEMITONE:
  688. std::strncpy(strBuf, "semi", STR_MAX);
  689. return;
  690. }
  691. }
  692. }
  693. CarlaPlugin::getParameterUnit(parameterId, strBuf);
  694. }
  695. void getParameterScalePointLabel(const uint32_t parameterId, const uint32_t scalePointId, char* const strBuf) override
  696. {
  697. CARLA_ASSERT(fRdfDescriptor != nullptr);
  698. CARLA_ASSERT(parameterId < kData->param.count);
  699. CARLA_ASSERT(scalePointId < parameterScalePointCount(parameterId));
  700. const int32_t rindex(kData->param.data[parameterId].rindex);
  701. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  702. {
  703. const LV2_RDF_Port& port(fRdfDescriptor->Ports[rindex]);
  704. if (scalePointId < port.ScalePointCount)
  705. {
  706. const LV2_RDF_PortScalePoint& portScalePoint(port.ScalePoints[scalePointId]);
  707. if (portScalePoint.Label != nullptr)
  708. {
  709. std::strncpy(strBuf, portScalePoint.Label, STR_MAX);
  710. return;
  711. }
  712. }
  713. }
  714. CarlaPlugin::getParameterScalePointLabel(parameterId, scalePointId, strBuf);
  715. }
  716. // -------------------------------------------------------------------
  717. // Set data (state)
  718. void prepareForSave() override
  719. {
  720. CARLA_ASSERT(fHandle != nullptr);
  721. if (fExt.state != nullptr && fExt.state->save != nullptr)
  722. {
  723. fExt.state->save(fHandle, carla_lv2_state_store, this, LV2_STATE_IS_POD, fFeatures);
  724. if (fHandle2 != nullptr)
  725. fExt.state->save(fHandle2, carla_lv2_state_store, this, LV2_STATE_IS_POD, fFeatures);
  726. }
  727. }
  728. // -------------------------------------------------------------------
  729. // Set data (internal stuff)
  730. // nothing
  731. // -------------------------------------------------------------------
  732. // Set data (plugin-specific stuff)
  733. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) override
  734. {
  735. CARLA_ASSERT(parameterId < kData->param.count);
  736. const float fixedValue(kData->param.fixValue(parameterId, value));
  737. fParamBuffers[parameterId] = fixedValue;
  738. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  739. }
  740. void setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui) override
  741. {
  742. CARLA_ASSERT(fDescriptor != nullptr);
  743. CARLA_ASSERT(fHandle != nullptr);
  744. CARLA_ASSERT(type != nullptr);
  745. CARLA_ASSERT(key != nullptr);
  746. CARLA_ASSERT(value != nullptr);
  747. carla_debug("Lv2Plugin::setCustomData(%s, %s, %s, %s)", type, key, value, bool2str(sendGui));
  748. if (type == nullptr)
  749. return carla_stderr2("Lv2Plugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is invalid", type, key, value, bool2str(sendGui));
  750. if (key == nullptr)
  751. return carla_stderr2("Lv2Plugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - key is null", type, key, value, bool2str(sendGui));
  752. if (value == nullptr)
  753. return carla_stderr2("Lv2Plugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - value is null", type, key, value, bool2str(sendGui));
  754. CarlaPlugin::setCustomData(type, key, value, sendGui);
  755. if (fExt.state != nullptr)
  756. {
  757. LV2_State_Status status;
  758. {
  759. const ScopedSingleProcessLocker spl(this, true);
  760. status = fExt.state->restore(fHandle, carla_lv2_state_retrieve, this, 0, fFeatures);
  761. if (fHandle2 != nullptr)
  762. fExt.state->restore(fHandle, carla_lv2_state_retrieve, this, 0, fFeatures);
  763. }
  764. switch (status)
  765. {
  766. case LV2_STATE_SUCCESS:
  767. carla_debug("Lv2Plugin::setCustomData(\"%s\", \"%s\", <value>, %s) - success", type, key, bool2str(sendGui));
  768. break;
  769. case LV2_STATE_ERR_UNKNOWN:
  770. carla_stderr("Lv2Plugin::setCustomData(\"%s\", \"%s\", <value>, %s) - unknown error", type, key, bool2str(sendGui));
  771. break;
  772. case LV2_STATE_ERR_BAD_TYPE:
  773. carla_stderr("Lv2Plugin::setCustomData(\"%s\", \"%s\", <value>, %s) - error, bad type", type, key, bool2str(sendGui));
  774. break;
  775. case LV2_STATE_ERR_BAD_FLAGS:
  776. carla_stderr("Lv2Plugin::setCustomData(\"%s\", \"%s\", <value>, %s) - error, bad flags", type, key, bool2str(sendGui));
  777. break;
  778. case LV2_STATE_ERR_NO_FEATURE:
  779. carla_stderr("Lv2Plugin::setCustomData(\"%s\", \"%s\", <value>, %s) - error, missing feature", type, key, bool2str(sendGui));
  780. break;
  781. case LV2_STATE_ERR_NO_PROPERTY:
  782. carla_stderr("Lv2Plugin::setCustomData(\"%s\", \"%s\", <value>, %s) - error, missing property", type, key, bool2str(sendGui));
  783. break;
  784. }
  785. }
  786. if (sendGui)
  787. {
  788. //CustomData cdata;
  789. //cdata.type = type;
  790. //cdata.key = key;
  791. //cdata.value = value;
  792. //uiTransferCustomData(&cdata);
  793. }
  794. }
  795. void setMidiProgram(int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) override
  796. {
  797. CARLA_ASSERT(fDescriptor != nullptr);
  798. CARLA_ASSERT(fHandle != nullptr);
  799. CARLA_ASSERT(index >= -1 && index < static_cast<int32_t>(kData->midiprog.count));
  800. if (index < -1)
  801. index = -1;
  802. else if (index > static_cast<int32_t>(kData->midiprog.count))
  803. return;
  804. if (index >= 0 && fExt.programs != nullptr && fExt.programs->select_program != nullptr)
  805. {
  806. const uint32_t bank = kData->midiprog.data[index].bank;
  807. const uint32_t program = kData->midiprog.data[index].program;
  808. const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  809. fExt.programs->select_program(fHandle, bank, program);
  810. if (fHandle2 != nullptr)
  811. fExt.programs->select_program(fHandle2, bank, program);
  812. }
  813. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback);
  814. }
  815. // -------------------------------------------------------------------
  816. // Set gui stuff
  817. void showGui(const bool yesNo) override
  818. {
  819. if (fUi.type == PLUGIN_UI_NULL)
  820. return;
  821. if (fUi.type == PLUGIN_UI_OSC)
  822. {
  823. if (yesNo)
  824. {
  825. kData->osc.thread.start();
  826. }
  827. else
  828. {
  829. if (kData->osc.data.target != nullptr)
  830. {
  831. osc_send_hide(&kData->osc.data);
  832. osc_send_quit(&kData->osc.data);
  833. kData->osc.data.free();
  834. }
  835. if (kData->osc.thread.isRunning() && ! kData->osc.thread.wait(kData->engine->getOptions().oscUiTimeout))
  836. kData->osc.thread.terminate();
  837. }
  838. return;
  839. }
  840. // take some precautions
  841. CARLA_ASSERT(fUi.descriptor != nullptr);
  842. CARLA_ASSERT(fUi.rdfDescriptor != nullptr);
  843. if (fUi.descriptor == nullptr)
  844. return;
  845. if (fUi.rdfDescriptor == nullptr)
  846. return;
  847. if (yesNo)
  848. {
  849. CARLA_ASSERT(fUi.descriptor->instantiate != nullptr);
  850. if (fUi.descriptor->instantiate == nullptr)
  851. return;
  852. }
  853. else
  854. {
  855. CARLA_ASSERT(fUi.descriptor->cleanup != nullptr);
  856. if (fUi.handle == nullptr)
  857. return;
  858. if (fUi.descriptor->cleanup == nullptr)
  859. return;
  860. }
  861. if (fUi.type == PLUGIN_UI_EXTERNAL)
  862. {
  863. if (yesNo)
  864. {
  865. fUi.widget = nullptr;
  866. if (fUi.handle == nullptr)
  867. fUi.handle = fUi.descriptor->instantiate(fUi.descriptor, fRdfDescriptor->URI, fUi.rdfDescriptor->Bundle,
  868. carla_lv2_ui_write_function, this, &fUi.widget, fFeatures);
  869. CARLA_ASSERT(fUi.handle != nullptr);
  870. CARLA_ASSERT(fUi.widget != nullptr);
  871. if (fUi.handle == nullptr || fUi.widget == nullptr)
  872. {
  873. fUi.handle = nullptr;
  874. fUi.widget = nullptr;
  875. kData->engine->callback(CALLBACK_ERROR, fId, 0, 0, 0.0f, "Plugin refused to open its own UI");
  876. kData->engine->callback(CALLBACK_SHOW_GUI, fId, 0, 0, 0.0f, nullptr);
  877. return;
  878. }
  879. updateUi();
  880. LV2_EXTERNAL_UI_SHOW((LV2_External_UI_Widget*)fUi.widget);
  881. }
  882. else
  883. {
  884. CARLA_ASSERT(fUi.widget != nullptr);
  885. if (fUi.widget != nullptr)
  886. LV2_EXTERNAL_UI_HIDE((LV2_External_UI_Widget*)fUi.widget);
  887. fUi.descriptor->cleanup(fUi.handle);
  888. fUi.handle = nullptr;
  889. fUi.widget = nullptr;
  890. }
  891. }
  892. else // means PLUGIN_UI_PARENT || PLUGIN_UI_QT
  893. {
  894. if (yesNo)
  895. {
  896. if (kData->gui == nullptr)
  897. {
  898. // TODO
  899. CarlaPluginGui::Options guiOptions;
  900. guiOptions.parented = (fUi.type == PLUGIN_UI_PARENT);
  901. guiOptions.resizable = isUiResizable();
  902. kData->gui = new CarlaPluginGui(kData->engine, this, guiOptions);
  903. }
  904. if (fUi.type == PLUGIN_UI_PARENT)
  905. {
  906. fFeatures[kFeatureIdUiParent]->data = kData->gui->getContainerWinId();
  907. fFeatures[kFeatureIdUiParent]->URI = LV2_UI__parent;
  908. }
  909. fUi.widget = nullptr;
  910. if (fUi.handle == nullptr)
  911. fUi.handle = fUi.descriptor->instantiate(fUi.descriptor, fRdfDescriptor->URI, fUi.rdfDescriptor->Bundle,
  912. carla_lv2_ui_write_function, this, &fUi.widget, fFeatures);
  913. CARLA_ASSERT(fUi.handle != nullptr);
  914. CARLA_ASSERT(fUi.widget != nullptr);
  915. if (fUi.handle == nullptr || fUi.widget == nullptr)
  916. {
  917. fUi.handle = nullptr;
  918. fUi.widget = nullptr;
  919. kData->gui->close();
  920. delete kData->gui;
  921. kData->gui = nullptr;
  922. kData->engine->callback(CALLBACK_ERROR, fId, 0, 0, 0.0f, "Plugin refused to open its own UI");
  923. kData->engine->callback(CALLBACK_SHOW_GUI, fId, -1, 0, 0.0f, nullptr);
  924. return;
  925. }
  926. if (fUi.type == PLUGIN_UI_QT)
  927. kData->gui->setWidget((QWidget*)fUi.widget);
  928. updateUi();
  929. kData->gui->setWindowTitle(QString("%1 (GUI)").arg((const char*)fName));
  930. kData->gui->show();
  931. }
  932. else
  933. {
  934. fUi.descriptor->cleanup(fUi.handle);
  935. fUi.handle = nullptr;
  936. fUi.widget = nullptr;
  937. if (kData->gui != nullptr)
  938. {
  939. kData->gui->close();
  940. delete kData->gui;
  941. kData->gui = nullptr;
  942. }
  943. }
  944. }
  945. }
  946. void idleGui() override
  947. {
  948. if (fUi.handle != nullptr && fUi.descriptor != nullptr)
  949. {
  950. if (fUi.type == PLUGIN_UI_EXTERNAL && fUi.widget != nullptr)
  951. LV2_EXTERNAL_UI_RUN((LV2_External_UI_Widget*)fUi.widget);
  952. if (fExt.uiidle != nullptr)
  953. {
  954. if (fExt.uiidle->idle(fUi.handle) != 0)
  955. {
  956. showGui(false);
  957. kData->engine->callback(CALLBACK_SHOW_GUI, fId, 0, 0, 0.0f, nullptr);
  958. }
  959. }
  960. }
  961. CarlaPlugin::idleGui();
  962. }
  963. // -------------------------------------------------------------------
  964. // Plugin state
  965. void reload() override
  966. {
  967. carla_debug("Lv2Plugin::reload() - start");
  968. CARLA_ASSERT(kData->engine != nullptr);
  969. CARLA_ASSERT(fHandle != nullptr);
  970. CARLA_ASSERT(fDescriptor != nullptr);
  971. CARLA_ASSERT(fRdfDescriptor != nullptr);
  972. if (kData->engine == nullptr)
  973. return;
  974. if (fHandle == nullptr)
  975. return;
  976. if (fDescriptor == nullptr)
  977. return;
  978. if (fRdfDescriptor == nullptr)
  979. return;
  980. const ProcessMode processMode(kData->engine->getProccessMode());
  981. // Safely disable plugin for reload
  982. const ScopedDisabler sd(this);
  983. if (kData->active)
  984. deactivate();
  985. clearBuffers();
  986. const float sampleRate = (float)kData->engine->getSampleRate();
  987. const uint32_t portCount = fRdfDescriptor->PortCount;
  988. uint32_t aIns, aOuts, cvIns, cvOuts, params, j;
  989. aIns = aOuts = cvIns = cvOuts = params = 0;
  990. NonRtList<uint32_t> evIns, evOuts;
  991. bool forcedStereoIn, forcedStereoOut;
  992. forcedStereoIn = forcedStereoOut = false;
  993. bool needsCtrlIn, needsCtrlOut;
  994. needsCtrlIn = needsCtrlOut = false;
  995. for (uint32_t i=0; i < portCount; ++i)
  996. {
  997. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  998. if (LV2_IS_PORT_AUDIO(portTypes))
  999. {
  1000. if (LV2_IS_PORT_INPUT(portTypes))
  1001. aIns += 1;
  1002. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1003. aOuts += 1;
  1004. }
  1005. else if (LV2_IS_PORT_CV(portTypes))
  1006. {
  1007. if (LV2_IS_PORT_INPUT(portTypes))
  1008. cvIns += 1;
  1009. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1010. cvOuts += 1;
  1011. }
  1012. else if (LV2_IS_PORT_ATOM_SEQUENCE(portTypes))
  1013. {
  1014. if (LV2_IS_PORT_INPUT(portTypes))
  1015. evIns.append(CARLA_EVENT_DATA_ATOM);
  1016. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1017. evOuts.append(CARLA_EVENT_DATA_ATOM);
  1018. }
  1019. else if (LV2_IS_PORT_EVENT(portTypes))
  1020. {
  1021. if (LV2_IS_PORT_INPUT(portTypes))
  1022. evIns.append(CARLA_EVENT_DATA_EVENT);
  1023. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1024. evOuts.append(CARLA_EVENT_DATA_EVENT);
  1025. }
  1026. else if (LV2_IS_PORT_MIDI_LL(portTypes))
  1027. {
  1028. if (LV2_IS_PORT_INPUT(portTypes))
  1029. evIns.append(CARLA_EVENT_DATA_MIDI_LL);
  1030. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1031. evOuts.append(CARLA_EVENT_DATA_MIDI_LL);
  1032. }
  1033. else if (LV2_IS_PORT_CONTROL(portTypes))
  1034. params += 1;
  1035. }
  1036. // check extensions
  1037. fExt.options = nullptr;
  1038. fExt.programs = nullptr;
  1039. fExt.state = nullptr;
  1040. fExt.worker = nullptr;
  1041. if (fDescriptor->extension_data != nullptr)
  1042. {
  1043. if (kData->extraHints & PLUGIN_HAS_EXTENSION_OPTIONS)
  1044. fExt.options = (const LV2_Options_Interface*)fDescriptor->extension_data(LV2_OPTIONS__interface);
  1045. if (kData->extraHints & PLUGIN_HAS_EXTENSION_PROGRAMS)
  1046. fExt.programs = (const LV2_Programs_Interface*)fDescriptor->extension_data(LV2_PROGRAMS__Interface);
  1047. if (kData->extraHints & PLUGIN_HAS_EXTENSION_STATE)
  1048. fExt.state = (const LV2_State_Interface*)fDescriptor->extension_data(LV2_STATE__interface);
  1049. if (kData->extraHints & PLUGIN_HAS_EXTENSION_WORKER)
  1050. fExt.worker = (const LV2_Worker_Interface*)fDescriptor->extension_data(LV2_WORKER__interface);
  1051. }
  1052. if ((fOptions & PLUGIN_OPTION_FORCE_STEREO) != 0 && (aIns == 1 || aOuts == 1) && fExt.state == nullptr && fExt.worker == nullptr)
  1053. {
  1054. if (fHandle2 == nullptr)
  1055. fHandle2 = fDescriptor->instantiate(fDescriptor, sampleRate, fRdfDescriptor->Bundle, fFeatures);
  1056. if (aIns == 1)
  1057. {
  1058. aIns = 2;
  1059. forcedStereoIn = true;
  1060. }
  1061. if (aOuts == 1)
  1062. {
  1063. aOuts = 2;
  1064. forcedStereoOut = true;
  1065. }
  1066. }
  1067. if (aIns > 0)
  1068. {
  1069. kData->audioIn.createNew(aIns);
  1070. fAudioInBuffers = new float*[aIns];
  1071. for (uint32_t i=0; i < aIns; ++i)
  1072. fAudioInBuffers[i] = nullptr;
  1073. }
  1074. if (aOuts > 0)
  1075. {
  1076. kData->audioOut.createNew(aOuts);
  1077. fAudioOutBuffers = new float*[aOuts];
  1078. needsCtrlIn = true;
  1079. for (uint32_t i=0; i < aOuts; ++i)
  1080. fAudioOutBuffers[i] = nullptr;
  1081. }
  1082. if (params > 0)
  1083. {
  1084. kData->param.createNew(params);
  1085. fParamBuffers = new float[params];
  1086. for (uint32_t i=0; i < params; ++i)
  1087. fParamBuffers[i] = 0.0f;
  1088. }
  1089. if (evIns.count() > 0)
  1090. {
  1091. const size_t count(evIns.count());
  1092. fEventsIn.createNew(count);
  1093. for (j=0; j < count; ++j)
  1094. {
  1095. const uint32_t& type(evIns.getAt(j));
  1096. if (type == CARLA_EVENT_DATA_ATOM)
  1097. {
  1098. fEventsIn.data[j].type = CARLA_EVENT_DATA_ATOM;
  1099. fEventsIn.data[j].atom = (LV2_Atom_Sequence*)std::malloc(sizeof(LV2_Atom_Sequence) + MAX_EVENT_BUFFER);
  1100. fEventsIn.data[j].atom->atom.size = 0;
  1101. fEventsIn.data[j].atom->atom.type = CARLA_URI_MAP_ID_ATOM_SEQUENCE;
  1102. fEventsIn.data[j].atom->body.unit = CARLA_URI_MAP_ID_NULL;
  1103. fEventsIn.data[j].atom->body.pad = 0;
  1104. }
  1105. else if (type == CARLA_EVENT_DATA_EVENT)
  1106. {
  1107. fEventsIn.data[j].type = CARLA_EVENT_DATA_EVENT;
  1108. fEventsIn.data[j].event = lv2_event_buffer_new(MAX_EVENT_BUFFER, LV2_EVENT_AUDIO_STAMP);
  1109. }
  1110. else if (type == CARLA_EVENT_DATA_MIDI_LL)
  1111. {
  1112. fEventsIn.data[j].type = CARLA_EVENT_DATA_MIDI_LL;
  1113. fEventsIn.data[j].midi = new LV2_MIDI;
  1114. fEventsIn.data[j].midi->capacity = MAX_EVENT_BUFFER;
  1115. fEventsIn.data[j].midi->data = new unsigned char[MAX_EVENT_BUFFER];
  1116. }
  1117. }
  1118. }
  1119. if (evOuts.count() > 0)
  1120. {
  1121. const size_t count(evOuts.count());
  1122. fEventsOut.createNew(count);
  1123. for (j=0; j < count; ++j)
  1124. {
  1125. const uint32_t& type(evOuts.getAt(j));
  1126. if (type == CARLA_EVENT_DATA_ATOM)
  1127. {
  1128. fEventsOut.data[j].type = CARLA_EVENT_DATA_ATOM;
  1129. fEventsOut.data[j].atom = (LV2_Atom_Sequence*)std::malloc(sizeof(LV2_Atom_Sequence) + MAX_EVENT_BUFFER);
  1130. fEventsOut.data[j].atom->atom.size = 0;
  1131. fEventsOut.data[j].atom->atom.type = CARLA_URI_MAP_ID_ATOM_SEQUENCE;
  1132. fEventsOut.data[j].atom->body.unit = CARLA_URI_MAP_ID_NULL;
  1133. fEventsOut.data[j].atom->body.pad = 0;
  1134. }
  1135. else if (type == CARLA_EVENT_DATA_EVENT)
  1136. {
  1137. fEventsOut.data[j].type = CARLA_EVENT_DATA_EVENT;
  1138. fEventsOut.data[j].event = lv2_event_buffer_new(MAX_EVENT_BUFFER, LV2_EVENT_AUDIO_STAMP);
  1139. }
  1140. else if (type == CARLA_EVENT_DATA_MIDI_LL)
  1141. {
  1142. fEventsOut.data[j].type = CARLA_EVENT_DATA_MIDI_LL;
  1143. fEventsOut.data[j].midi = new LV2_MIDI;
  1144. fEventsOut.data[j].midi->capacity = MAX_EVENT_BUFFER;
  1145. fEventsOut.data[j].midi->data = new unsigned char[MAX_EVENT_BUFFER];
  1146. }
  1147. }
  1148. }
  1149. const uint portNameSize(kData->engine->maxPortNameSize());
  1150. CarlaString portName;
  1151. for (uint32_t i=0, iAudioIn=0, iAudioOut=0, iEvIn=0, iEvOut=0, iCtrl=0; i < portCount; ++i)
  1152. {
  1153. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  1154. portName.clear();
  1155. if (LV2_IS_PORT_AUDIO(portTypes) || LV2_IS_PORT_ATOM_SEQUENCE(portTypes) || LV2_IS_PORT_CV(portTypes) || LV2_IS_PORT_EVENT(portTypes) || LV2_IS_PORT_MIDI_LL(portTypes))
  1156. {
  1157. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  1158. {
  1159. portName = fName;
  1160. portName += ":";
  1161. }
  1162. portName += fRdfDescriptor->Ports[i].Name;
  1163. portName.truncate(portNameSize);
  1164. }
  1165. if (LV2_IS_PORT_AUDIO(portTypes))
  1166. {
  1167. if (LV2_IS_PORT_INPUT(portTypes))
  1168. {
  1169. j = iAudioIn++;
  1170. kData->audioIn.ports[j].port = (CarlaEngineAudioPort*)kData->client->addPort(kEnginePortTypeAudio, portName, true);
  1171. kData->audioIn.ports[j].rindex = i;
  1172. if (forcedStereoIn)
  1173. {
  1174. portName += "_2";
  1175. kData->audioIn.ports[1].port = (CarlaEngineAudioPort*)kData->client->addPort(kEnginePortTypeAudio, portName, true);
  1176. kData->audioIn.ports[1].rindex = i;
  1177. }
  1178. }
  1179. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1180. {
  1181. j = iAudioOut++;
  1182. kData->audioOut.ports[j].port = (CarlaEngineAudioPort*)kData->client->addPort(kEnginePortTypeAudio, portName, false);
  1183. kData->audioOut.ports[j].rindex = i;
  1184. if (forcedStereoOut)
  1185. {
  1186. portName += "_2";
  1187. kData->audioOut.ports[1].port = (CarlaEngineAudioPort*)kData->client->addPort(kEnginePortTypeAudio, portName, false);
  1188. kData->audioOut.ports[1].rindex = i;
  1189. }
  1190. }
  1191. else
  1192. carla_stderr("WARNING - Got a broken Port (Audio, but not input or output)");
  1193. }
  1194. else if (LV2_IS_PORT_CV(portTypes))
  1195. {
  1196. if (LV2_IS_PORT_INPUT(portTypes))
  1197. {
  1198. carla_stderr("WARNING - CV Ports are not supported yet");
  1199. }
  1200. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1201. {
  1202. carla_stderr("WARNING - CV Ports are not supported yet");
  1203. }
  1204. else
  1205. carla_stderr("WARNING - Got a broken Port (CV, but not input or output)");
  1206. fDescriptor->connect_port(fHandle, i, nullptr);
  1207. if (fHandle2 != nullptr)
  1208. fDescriptor->connect_port(fHandle2, i, nullptr);
  1209. }
  1210. else if (LV2_IS_PORT_ATOM_SEQUENCE(portTypes))
  1211. {
  1212. if (LV2_IS_PORT_INPUT(portTypes))
  1213. {
  1214. j = iEvIn++;
  1215. fDescriptor->connect_port(fHandle, i, fEventsIn.data[j].atom);
  1216. if (fHandle2 != nullptr)
  1217. fDescriptor->connect_port(fHandle2, i, fEventsIn.data[j].atom);
  1218. fEventsIn.data[j].rindex = i;
  1219. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1220. {
  1221. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1222. if (evIns.count() == 1)
  1223. {
  1224. needsCtrlIn = true;
  1225. fEventsIn.ctrl = &fEventsIn.data[j];
  1226. fEventsIn.ctrlIndex = j;
  1227. }
  1228. else
  1229. {
  1230. fEventsIn.data[j].port = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, true);
  1231. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1232. {
  1233. fEventsIn.ctrl = &fEventsIn.data[j];
  1234. fEventsIn.ctrlIndex = j;
  1235. }
  1236. }
  1237. }
  1238. if (portTypes & LV2_PORT_DATA_PATCH_MESSAGE)
  1239. {
  1240. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1241. }
  1242. if (portTypes & LV2_PORT_DATA_TIME_POSITION)
  1243. {
  1244. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_TIME;
  1245. }
  1246. }
  1247. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1248. {
  1249. j = iEvOut++;
  1250. fDescriptor->connect_port(fHandle, i, fEventsOut.data[j].atom);
  1251. if (fHandle2 != nullptr)
  1252. fDescriptor->connect_port(fHandle2, i, fEventsOut.data[j].atom);
  1253. fEventsOut.data[j].rindex = i;
  1254. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1255. {
  1256. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1257. if (evOuts.count() == 1)
  1258. {
  1259. needsCtrlOut = true;
  1260. fEventsOut.ctrl = &fEventsOut.data[j];
  1261. fEventsOut.ctrlIndex = j;
  1262. }
  1263. else
  1264. {
  1265. fEventsOut.data[j].port = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, false);
  1266. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1267. {
  1268. fEventsOut.ctrl = &fEventsOut.data[j];
  1269. fEventsOut.ctrlIndex = j;
  1270. }
  1271. }
  1272. }
  1273. if (portTypes & LV2_PORT_DATA_PATCH_MESSAGE)
  1274. {
  1275. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1276. }
  1277. if (portTypes & LV2_PORT_DATA_TIME_POSITION)
  1278. {
  1279. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_TIME;
  1280. }
  1281. }
  1282. else
  1283. carla_stderr("WARNING - Got a broken Port (Atom Sequence, but not input or output)");
  1284. }
  1285. else if (LV2_IS_PORT_EVENT(portTypes))
  1286. {
  1287. if (LV2_IS_PORT_INPUT(portTypes))
  1288. {
  1289. j = iEvIn++;
  1290. fDescriptor->connect_port(fHandle, i, fEventsIn.data[j].event);
  1291. if (fHandle2 != nullptr)
  1292. fDescriptor->connect_port(fHandle2, i, fEventsIn.data[j].event);
  1293. fEventsIn.data[j].rindex = i;
  1294. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1295. {
  1296. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1297. if (evIns.count() == 1)
  1298. {
  1299. needsCtrlIn = true;
  1300. fEventsIn.ctrl = &fEventsIn.data[j];
  1301. fEventsIn.ctrlIndex = j;
  1302. }
  1303. else
  1304. {
  1305. fEventsIn.data[j].port = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, true);
  1306. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1307. {
  1308. fEventsIn.ctrl = &fEventsIn.data[j];
  1309. fEventsIn.ctrlIndex = j;
  1310. }
  1311. }
  1312. }
  1313. if (portTypes & LV2_PORT_DATA_PATCH_MESSAGE)
  1314. {
  1315. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1316. }
  1317. if (portTypes & LV2_PORT_DATA_TIME_POSITION)
  1318. {
  1319. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_TIME;
  1320. }
  1321. }
  1322. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1323. {
  1324. j = iEvOut++;
  1325. fDescriptor->connect_port(fHandle, i, fEventsOut.data[j].event);
  1326. if (fHandle2 != nullptr)
  1327. fDescriptor->connect_port(fHandle2, i, fEventsOut.data[j].event);
  1328. fEventsOut.data[j].rindex = i;
  1329. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1330. {
  1331. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1332. if (evOuts.count() == 1)
  1333. {
  1334. needsCtrlOut = true;
  1335. fEventsOut.ctrl = &fEventsOut.data[j];
  1336. fEventsOut.ctrlIndex = j;
  1337. }
  1338. else
  1339. {
  1340. fEventsOut.data[j].port = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, false);
  1341. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1342. {
  1343. fEventsOut.ctrl = &fEventsOut.data[j];
  1344. fEventsOut.ctrlIndex = j;
  1345. }
  1346. }
  1347. }
  1348. if (portTypes & LV2_PORT_DATA_PATCH_MESSAGE)
  1349. {
  1350. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1351. }
  1352. if (portTypes & LV2_PORT_DATA_TIME_POSITION)
  1353. {
  1354. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_TIME;
  1355. }
  1356. }
  1357. else
  1358. carla_stderr("WARNING - Got a broken Port (Event, but not input or output)");
  1359. }
  1360. else if (LV2_IS_PORT_MIDI_LL(portTypes))
  1361. {
  1362. if (LV2_IS_PORT_INPUT(portTypes))
  1363. {
  1364. j = iEvIn++;
  1365. fDescriptor->connect_port(fHandle, i, fEventsIn.data[j].midi);
  1366. if (fHandle2 != nullptr)
  1367. fDescriptor->connect_port(fHandle2, i, fEventsIn.data[j].midi);
  1368. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1369. fEventsIn.data[j].rindex = i;
  1370. if (evIns.count() == 1)
  1371. {
  1372. needsCtrlIn = true;
  1373. fEventsIn.ctrl = &fEventsIn.data[j];
  1374. fEventsIn.ctrlIndex = j;
  1375. }
  1376. else
  1377. {
  1378. fEventsIn.data[j].port = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, true);
  1379. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1380. {
  1381. fEventsIn.ctrl = &fEventsIn.data[j];
  1382. fEventsIn.ctrlIndex = j;
  1383. }
  1384. }
  1385. }
  1386. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1387. {
  1388. j = iEvOut++;
  1389. fDescriptor->connect_port(fHandle, i, fEventsOut.data[j].midi);
  1390. if (fHandle2 != nullptr)
  1391. fDescriptor->connect_port(fHandle2, i, fEventsOut.data[j].midi);
  1392. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1393. fEventsOut.data[j].rindex = i;
  1394. if (evOuts.count() == 1)
  1395. {
  1396. needsCtrlOut = true;
  1397. fEventsOut.ctrl = &fEventsOut.data[j];
  1398. fEventsOut.ctrlIndex = j;
  1399. }
  1400. else
  1401. {
  1402. fEventsOut.data[j].port = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, false);
  1403. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1404. {
  1405. fEventsOut.ctrl = &fEventsOut.data[j];
  1406. fEventsOut.ctrlIndex = j;
  1407. }
  1408. }
  1409. }
  1410. else
  1411. carla_stderr("WARNING - Got a broken Port (Midi, but not input or output)");
  1412. }
  1413. else if (LV2_IS_PORT_CONTROL(portTypes))
  1414. {
  1415. const LV2_Property portProps(fRdfDescriptor->Ports[i].Properties);
  1416. const LV2_Property portDesignation(fRdfDescriptor->Ports[i].Designation);
  1417. const LV2_RDF_PortPoints portPoints(fRdfDescriptor->Ports[i].Points);
  1418. j = iCtrl++;
  1419. kData->param.data[j].index = j;
  1420. kData->param.data[j].rindex = i;
  1421. kData->param.data[j].hints = 0x0;
  1422. kData->param.data[j].midiChannel = 0;
  1423. kData->param.data[j].midiCC = -1;
  1424. float min, max, def, step, stepSmall, stepLarge;
  1425. // min value
  1426. if (LV2_HAVE_MINIMUM_PORT_POINT(portPoints.Hints))
  1427. min = portPoints.Minimum;
  1428. else
  1429. min = 0.0f;
  1430. // max value
  1431. if (LV2_HAVE_MAXIMUM_PORT_POINT(portPoints.Hints))
  1432. max = portPoints.Maximum;
  1433. else
  1434. max = 1.0f;
  1435. if (min > max)
  1436. max = min;
  1437. else if (max < min)
  1438. min = max;
  1439. // stupid hack for ir.lv2 (broken plugin)
  1440. if (std::strcmp(fRdfDescriptor->URI, "http://factorial.hu/plugins/lv2/ir") == 0 && std::strncmp(fRdfDescriptor->Ports[i].Name, "FileHash", 8) == 0)
  1441. {
  1442. min = 0.0f;
  1443. max = (float)0xffffff;
  1444. }
  1445. if (max - min == 0.0f)
  1446. {
  1447. carla_stderr2("WARNING - Broken plugin parameter '%s': max - min == 0.0f", fRdfDescriptor->Ports[i].Name);
  1448. max = min + 0.1f;
  1449. }
  1450. // default value
  1451. if (LV2_HAVE_DEFAULT_PORT_POINT(portPoints.Hints))
  1452. {
  1453. def = portPoints.Default;
  1454. }
  1455. else
  1456. {
  1457. // no default value
  1458. if (min < 0.0f && max > 0.0f)
  1459. def = 0.0f;
  1460. else
  1461. def = min;
  1462. }
  1463. if (def < min)
  1464. def = min;
  1465. else if (def > max)
  1466. def = max;
  1467. if (LV2_IS_PORT_SAMPLE_RATE(portProps))
  1468. {
  1469. min *= sampleRate;
  1470. max *= sampleRate;
  1471. def *= sampleRate;
  1472. kData->param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  1473. }
  1474. if (LV2_IS_PORT_TOGGLED(portProps))
  1475. {
  1476. step = max - min;
  1477. stepSmall = step;
  1478. stepLarge = step;
  1479. kData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  1480. }
  1481. else if (LV2_IS_PORT_INTEGER(portProps))
  1482. {
  1483. step = 1.0f;
  1484. stepSmall = 1.0f;
  1485. stepLarge = 10.0f;
  1486. kData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  1487. }
  1488. else
  1489. {
  1490. float range = max - min;
  1491. step = range/100.0f;
  1492. stepSmall = range/1000.0f;
  1493. stepLarge = range/10.0f;
  1494. }
  1495. if (LV2_IS_PORT_INPUT(portTypes))
  1496. {
  1497. if (LV2_IS_PORT_DESIGNATION_LATENCY(portDesignation))
  1498. {
  1499. carla_stderr("Plugin has latency input port, this should not happen!");
  1500. }
  1501. else if (LV2_IS_PORT_DESIGNATION_SAMPLE_RATE(portDesignation))
  1502. {
  1503. def = sampleRate;
  1504. step = 1.0f;
  1505. stepSmall = 1.0f;
  1506. stepLarge = 1.0f;
  1507. kData->param.data[j].type = PARAMETER_SAMPLE_RATE;
  1508. kData->param.data[j].hints = 0x0;
  1509. }
  1510. else if (LV2_IS_PORT_DESIGNATION_FREEWHEELING(portDesignation))
  1511. {
  1512. kData->param.data[j].type = PARAMETER_LV2_FREEWHEEL;
  1513. }
  1514. else if (LV2_IS_PORT_DESIGNATION_TIME(portDesignation))
  1515. {
  1516. kData->param.data[j].type = PARAMETER_LV2_TIME;
  1517. }
  1518. else
  1519. {
  1520. kData->param.data[j].type = PARAMETER_INPUT;
  1521. kData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  1522. kData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  1523. needsCtrlIn = true;
  1524. }
  1525. // MIDI CC value
  1526. const LV2_RDF_PortMidiMap& portMidiMap(fRdfDescriptor->Ports[i].MidiMap);
  1527. if (LV2_IS_PORT_MIDI_MAP_CC(portMidiMap.Type))
  1528. {
  1529. if (! MIDI_IS_CONTROL_BANK_SELECT(portMidiMap.Number))
  1530. kData->param.data[j].midiCC = portMidiMap.Number;
  1531. }
  1532. }
  1533. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1534. {
  1535. if (LV2_IS_PORT_DESIGNATION_LATENCY(portDesignation))
  1536. {
  1537. min = 0.0f;
  1538. max = sampleRate;
  1539. def = 0.0f;
  1540. step = 1.0f;
  1541. stepSmall = 1.0f;
  1542. stepLarge = 1.0f;
  1543. kData->param.data[j].type = PARAMETER_LATENCY;
  1544. kData->param.data[j].hints = 0;
  1545. }
  1546. else if (LV2_IS_PORT_DESIGNATION_SAMPLE_RATE(portDesignation))
  1547. {
  1548. def = sampleRate;
  1549. step = 1.0f;
  1550. stepSmall = 1.0f;
  1551. stepLarge = 1.0f;
  1552. kData->param.data[j].type = PARAMETER_SAMPLE_RATE;
  1553. kData->param.data[j].hints = 0;
  1554. }
  1555. else if (LV2_IS_PORT_DESIGNATION_FREEWHEELING(portDesignation))
  1556. {
  1557. carla_stderr("Plugin has freewheeling output port, this should not happen!");
  1558. }
  1559. else if (LV2_IS_PORT_DESIGNATION_TIME(portDesignation))
  1560. {
  1561. kData->param.data[j].type = PARAMETER_LV2_TIME;
  1562. }
  1563. else
  1564. {
  1565. kData->param.data[j].type = PARAMETER_OUTPUT;
  1566. kData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  1567. kData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  1568. needsCtrlOut = true;
  1569. }
  1570. }
  1571. else
  1572. {
  1573. kData->param.data[j].type = PARAMETER_UNKNOWN;
  1574. carla_stderr2("WARNING - Got a broken Port (Control, but not input or output)");
  1575. }
  1576. // extra parameter hints
  1577. if (LV2_IS_PORT_ENUMERATION(portProps))
  1578. kData->param.data[j].hints |= PARAMETER_USES_SCALEPOINTS;
  1579. if (LV2_IS_PORT_LOGARITHMIC(portProps))
  1580. kData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  1581. if (LV2_IS_PORT_TRIGGER(portProps))
  1582. kData->param.data[j].hints |= PARAMETER_IS_TRIGGER;
  1583. if (LV2_IS_PORT_STRICT_BOUNDS(portProps))
  1584. kData->param.data[j].hints |= PARAMETER_IS_STRICT_BOUNDS;
  1585. // check if parameter is not enabled or automable
  1586. if (LV2_IS_PORT_NOT_ON_GUI(portProps))
  1587. kData->param.data[j].hints &= ~PARAMETER_IS_ENABLED;
  1588. if (LV2_IS_PORT_CAUSES_ARTIFACTS(portProps) || LV2_IS_PORT_EXPENSIVE(portProps) || LV2_IS_PORT_NOT_AUTOMATIC(portProps))
  1589. kData->param.data[j].hints &= ~PARAMETER_IS_AUTOMABLE;
  1590. kData->param.ranges[j].min = min;
  1591. kData->param.ranges[j].max = max;
  1592. kData->param.ranges[j].def = def;
  1593. kData->param.ranges[j].step = step;
  1594. kData->param.ranges[j].stepSmall = stepSmall;
  1595. kData->param.ranges[j].stepLarge = stepLarge;
  1596. // Start parameters in their default values
  1597. fParamBuffers[j] = def;
  1598. fDescriptor->connect_port(fHandle, i, &fParamBuffers[j]);
  1599. if (fHandle2 != nullptr)
  1600. fDescriptor->connect_port(fHandle2, i, &fParamBuffers[j]);
  1601. }
  1602. else
  1603. {
  1604. // Port Type not supported, but it's optional anyway
  1605. fDescriptor->connect_port(fHandle, i, nullptr);
  1606. if (fHandle2 != nullptr)
  1607. fDescriptor->connect_port(fHandle2, i, nullptr);
  1608. }
  1609. }
  1610. if (needsCtrlIn)
  1611. {
  1612. portName.clear();
  1613. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  1614. {
  1615. portName = fName;
  1616. portName += ":";
  1617. }
  1618. portName += "events-in";
  1619. portName.truncate(portNameSize);
  1620. kData->event.portIn = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, true);
  1621. }
  1622. if (needsCtrlOut)
  1623. {
  1624. portName.clear();
  1625. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  1626. {
  1627. portName = fName;
  1628. portName += ":";
  1629. }
  1630. portName += "events-out";
  1631. portName.truncate(portNameSize);
  1632. kData->event.portOut = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, false);
  1633. }
  1634. if (fEventsIn.ctrl != nullptr && fEventsIn.ctrl->port == nullptr)
  1635. fEventsIn.ctrl->port = kData->event.portIn;
  1636. if (fEventsOut.ctrl != nullptr && fEventsOut.ctrl->port == nullptr)
  1637. fEventsOut.ctrl->port = kData->event.portOut;
  1638. if (forcedStereoIn || forcedStereoOut)
  1639. fOptions |= PLUGIN_OPTION_FORCE_STEREO;
  1640. else
  1641. fOptions &= ~PLUGIN_OPTION_FORCE_STEREO;
  1642. // plugin hints
  1643. fHints = 0x0;
  1644. if (isRealtimeSafe())
  1645. fHints |= PLUGIN_IS_RTSAFE;
  1646. if (fUi.type != PLUGIN_UI_NULL)
  1647. {
  1648. fHints |= PLUGIN_HAS_GUI;
  1649. if (fUi.type == PLUGIN_UI_QT || fUi.type == PLUGIN_UI_PARENT)
  1650. fHints |= PLUGIN_HAS_SINGLE_THREAD;
  1651. }
  1652. if (LV2_IS_GENERATOR(fRdfDescriptor->Type[0], fRdfDescriptor->Type[1]))
  1653. fHints |= PLUGIN_IS_SYNTH;
  1654. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  1655. fHints |= PLUGIN_CAN_DRYWET;
  1656. if (aOuts > 0)
  1657. fHints |= PLUGIN_CAN_VOLUME;
  1658. if (aOuts >= 2 && aOuts % 2 == 0)
  1659. fHints |= PLUGIN_CAN_BALANCE;
  1660. // extra plugin hints
  1661. kData->extraHints &= ~PLUGIN_HINT_CAN_RUN_RACK;
  1662. if (fExt.state != nullptr || fExt.worker != nullptr)
  1663. {
  1664. if ((aIns == 0 || aIns == 2) && (aOuts == 0 || aOuts == 2) && evIns.count() <= 1 && evOuts.count() <= 1)
  1665. kData->extraHints |= PLUGIN_HINT_CAN_RUN_RACK;
  1666. }
  1667. else
  1668. {
  1669. if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0) && evIns.count() <= 1 && evOuts.count() <= 1)
  1670. kData->extraHints |= PLUGIN_HINT_CAN_RUN_RACK;
  1671. }
  1672. bufferSizeChanged(kData->engine->getBufferSize());
  1673. reloadPrograms(true);
  1674. if (kData->active)
  1675. activate();
  1676. evIns.clear();
  1677. evOuts.clear();
  1678. carla_debug("Lv2Plugin::reload() - end");
  1679. }
  1680. void reloadPrograms(const bool init) override
  1681. {
  1682. carla_debug("DssiPlugin::reloadPrograms(%s)", bool2str(init));
  1683. uint32_t i, oldCount = kData->midiprog.count;
  1684. const int32_t current = kData->midiprog.current;
  1685. // Delete old programs
  1686. kData->midiprog.clear();
  1687. // Query new programs
  1688. uint32_t count = 0;
  1689. if (fExt.programs != nullptr && fExt.programs->get_program != nullptr && fExt.programs->select_program != nullptr)
  1690. {
  1691. while (fExt.programs->get_program(fHandle, count))
  1692. count++;
  1693. }
  1694. if (count > 0)
  1695. {
  1696. kData->midiprog.createNew(count);
  1697. // Update data
  1698. for (i=0; i < count; ++i)
  1699. {
  1700. const LV2_Program_Descriptor* const pdesc(fExt.programs->get_program(fHandle, i));
  1701. CARLA_ASSERT(pdesc != nullptr);
  1702. CARLA_ASSERT(pdesc->name != nullptr);
  1703. kData->midiprog.data[i].bank = static_cast<uint32_t>(pdesc->bank);
  1704. kData->midiprog.data[i].program = static_cast<uint32_t>(pdesc->program);
  1705. kData->midiprog.data[i].name = carla_strdup(pdesc->name);
  1706. }
  1707. }
  1708. #ifndef BUILD_BRIDGE
  1709. // Update OSC Names
  1710. if (kData->engine->isOscControlRegistered())
  1711. {
  1712. kData->engine->osc_send_control_set_midi_program_count(fId, count);
  1713. for (i=0; i < count; ++i)
  1714. kData->engine->osc_send_control_set_midi_program_data(fId, i, kData->midiprog.data[i].bank, kData->midiprog.data[i].program, kData->midiprog.data[i].name);
  1715. }
  1716. #endif
  1717. if (init)
  1718. {
  1719. if (count > 0)
  1720. setMidiProgram(0, false, false, false);
  1721. }
  1722. else
  1723. {
  1724. // Check if current program is invalid
  1725. bool programChanged = false;
  1726. if (count == oldCount+1)
  1727. {
  1728. // one midi program added, probably created by user
  1729. kData->midiprog.current = oldCount;
  1730. programChanged = true;
  1731. }
  1732. else if (current < 0 && count > 0)
  1733. {
  1734. // programs exist now, but not before
  1735. kData->midiprog.current = 0;
  1736. programChanged = true;
  1737. }
  1738. else if (current >= 0 && count == 0)
  1739. {
  1740. // programs existed before, but not anymore
  1741. kData->midiprog.current = -1;
  1742. programChanged = true;
  1743. }
  1744. else if (current >= static_cast<int32_t>(count))
  1745. {
  1746. // current midi program > count
  1747. kData->midiprog.current = 0;
  1748. programChanged = true;
  1749. }
  1750. else
  1751. {
  1752. // no change
  1753. kData->midiprog.current = current;
  1754. }
  1755. if (programChanged)
  1756. setMidiProgram(kData->midiprog.current, true, true, true);
  1757. kData->engine->callback(CALLBACK_RELOAD_PROGRAMS, fId, 0, 0, 0.0f, nullptr);
  1758. }
  1759. }
  1760. // -------------------------------------------------------------------
  1761. // Plugin processing
  1762. void activate() override
  1763. {
  1764. CARLA_ASSERT(fDescriptor != nullptr);
  1765. CARLA_ASSERT(fHandle != nullptr);
  1766. if (fDescriptor->activate != nullptr)
  1767. {
  1768. fDescriptor->activate(fHandle);
  1769. if (fHandle2 != nullptr)
  1770. fDescriptor->activate(fHandle2);
  1771. }
  1772. }
  1773. void deactivate() override
  1774. {
  1775. CARLA_ASSERT(fDescriptor != nullptr);
  1776. CARLA_ASSERT(fHandle != nullptr);
  1777. if (fDescriptor->deactivate != nullptr)
  1778. {
  1779. fDescriptor->deactivate(fHandle);
  1780. if (fHandle2 != nullptr)
  1781. fDescriptor->deactivate(fHandle2);
  1782. }
  1783. }
  1784. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames) override
  1785. {
  1786. uint32_t i, k;
  1787. // --------------------------------------------------------------------------------------------------------
  1788. // Check if active
  1789. if (! kData->active)
  1790. {
  1791. // disable any output sound
  1792. for (i=0; i < kData->audioOut.count; ++i)
  1793. carla_zeroFloat(outBuffer[i], frames);
  1794. return;
  1795. }
  1796. // handle events from different APIs
  1797. uint32_t evInAtomOffsets[fEventsIn.count];
  1798. LV2_Event_Iterator evInEventIters[fEventsIn.count];
  1799. LV2_MIDIState evInMidiStates[fEventsIn.count];
  1800. for (i=0; i < fEventsIn.count; ++i)
  1801. {
  1802. if (fEventsIn.data[i].type & CARLA_EVENT_DATA_ATOM)
  1803. {
  1804. evInAtomOffsets[i] = 0;
  1805. fEventsIn.data[i].atom->atom.size = 0;
  1806. fEventsIn.data[i].atom->atom.type = CARLA_URI_MAP_ID_ATOM_SEQUENCE;
  1807. fEventsIn.data[i].atom->body.unit = CARLA_URI_MAP_ID_NULL;
  1808. fEventsIn.data[i].atom->body.pad = 0;
  1809. }
  1810. else if (fEventsIn.data[i].type & CARLA_EVENT_DATA_EVENT)
  1811. {
  1812. lv2_event_buffer_reset(fEventsIn.data[i].event, LV2_EVENT_AUDIO_STAMP, (uint8_t*)(fEventsIn.data[i].event + 1));
  1813. lv2_event_begin(&evInEventIters[i], fEventsIn.data[i].event);
  1814. }
  1815. else if (fEventsIn.data[i].type & CARLA_EVENT_DATA_MIDI_LL)
  1816. {
  1817. evInMidiStates[i].midi = fEventsIn.data[i].midi;
  1818. evInMidiStates[i].frame_count = frames;
  1819. evInMidiStates[i].position = 0;
  1820. evInMidiStates[i].midi->event_count = 0;
  1821. evInMidiStates[i].midi->size = 0;
  1822. }
  1823. }
  1824. for (i=0; i < fEventsOut.count; ++i)
  1825. {
  1826. if (fEventsOut.data[i].type & CARLA_EVENT_DATA_ATOM)
  1827. {
  1828. fEventsOut.data[i].atom->atom.size = 0;
  1829. fEventsOut.data[i].atom->atom.type = CARLA_URI_MAP_ID_ATOM_SEQUENCE;
  1830. fEventsOut.data[i].atom->body.unit = CARLA_URI_MAP_ID_NULL;
  1831. fEventsOut.data[i].atom->body.pad = 0;
  1832. }
  1833. else if (fEventsOut.data[i].type & CARLA_EVENT_DATA_EVENT)
  1834. {
  1835. lv2_event_buffer_reset(fEventsOut.data[i].event, LV2_EVENT_AUDIO_STAMP, (uint8_t*)(fEventsOut.data[i].event + 1));
  1836. }
  1837. else if (fEventsOut.data[i].type & CARLA_EVENT_DATA_MIDI_LL)
  1838. {
  1839. // not needed
  1840. }
  1841. }
  1842. CARLA_PROCESS_CONTINUE_CHECK;
  1843. // --------------------------------------------------------------------------------------------------------
  1844. // Check if needs reset
  1845. if (kData->needsReset)
  1846. {
  1847. // TODO!
  1848. if (fOptions & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1849. {
  1850. //for (unsigned char j=0, l=MAX_MIDI_CHANNELS; j < MAX_MIDI_CHANNELS; ++j)
  1851. {
  1852. }
  1853. }
  1854. else
  1855. {
  1856. }
  1857. if (kData->latency > 0)
  1858. {
  1859. //for (i=0; i < kData->audioIn.count; ++i)
  1860. // carla_zeroFloat(kData->latencyBuffers[i], kData->latency);
  1861. }
  1862. kData->needsReset = false;
  1863. }
  1864. CARLA_PROCESS_CONTINUE_CHECK;
  1865. // --------------------------------------------------------------------------------------------------------
  1866. // Special Parameters
  1867. {
  1868. int32_t rindex;
  1869. const EngineTimeInfo& timeInfo(kData->engine->getTimeInfo());
  1870. for (k=0; k < kData->param.count; ++k)
  1871. {
  1872. if (kData->param.data[k].type == PARAMETER_LATENCY)
  1873. {
  1874. // nothing
  1875. }
  1876. else if (kData->param.data[k].type == PARAMETER_LV2_FREEWHEEL)
  1877. {
  1878. setParameterValue(k, kData->engine->isOffline() ? 1.0f : 0.0f, false, false, false);
  1879. }
  1880. else if (kData->param.data[k].type == PARAMETER_LV2_TIME)
  1881. {
  1882. rindex = kData->param.data[k].rindex;
  1883. CARLA_ASSERT(rindex >= 0 && rindex < static_cast<int32_t>(fRdfDescriptor->PortCount));
  1884. switch (fRdfDescriptor->Ports[rindex].Designation)
  1885. {
  1886. // Non-BBT
  1887. case LV2_PORT_DESIGNATION_TIME_FRAME:
  1888. setParameterValue(k, timeInfo.frame, false, false, false);
  1889. break;
  1890. case LV2_PORT_DESIGNATION_TIME_FRAMES_PER_SECOND:
  1891. break;
  1892. //case LV2_PORT_DESIGNATION_TIME_POSITION:
  1893. // setParameterValue(k, timeInfo.usecs, false, false, false);
  1894. // break;
  1895. case LV2_PORT_DESIGNATION_TIME_SPEED:
  1896. setParameterValue(k, timeInfo.playing ? 1.0f : 0.0f, false, false, false);
  1897. break;
  1898. // BBT
  1899. case LV2_PORT_DESIGNATION_TIME_BAR:
  1900. if (timeInfo.valid & EngineTimeInfo::ValidBBT)
  1901. setParameterValue(k, timeInfo.bbt.bar - 1, false, false, false);
  1902. break;
  1903. case LV2_PORT_DESIGNATION_TIME_BAR_BEAT:
  1904. if (timeInfo.valid & EngineTimeInfo::ValidBBT)
  1905. setParameterValue(k, float(timeInfo.bbt.beat - 1) + (float(timeInfo.bbt.tick) / timeInfo.bbt.ticksPerBeat), false, false, false);
  1906. break;
  1907. case LV2_PORT_DESIGNATION_TIME_BEAT:
  1908. if (timeInfo.valid & EngineTimeInfo::ValidBBT)
  1909. setParameterValue(k, timeInfo.bbt.beat - 1, false, false, false);
  1910. break;
  1911. case LV2_PORT_DESIGNATION_TIME_BEAT_UNIT:
  1912. if (timeInfo.valid & EngineTimeInfo::ValidBBT)
  1913. setParameterValue(k, timeInfo.bbt.beatType, false, false, false);
  1914. break;
  1915. case LV2_PORT_DESIGNATION_TIME_BEATS_PER_BAR:
  1916. if (timeInfo.valid & EngineTimeInfo::ValidBBT)
  1917. setParameterValue(k, timeInfo.bbt.beatsPerBar, false, false, false);
  1918. break;
  1919. case LV2_PORT_DESIGNATION_TIME_BEATS_PER_MINUTE:
  1920. if (timeInfo.valid & EngineTimeInfo::ValidBBT)
  1921. setParameterValue(k, timeInfo.bbt.beatsPerMinute, false, false, false);
  1922. break;
  1923. }
  1924. }
  1925. }
  1926. }
  1927. // --------------------------------------------------------------------------------------------------------
  1928. // Event Input and Processing
  1929. if (fEventsIn.ctrl != nullptr && fEventsIn.ctrl->port != nullptr)
  1930. {
  1931. // ----------------------------------------------------------------------------------------------------
  1932. // Message Input
  1933. if (fAtomQueueIn.tryLock())
  1934. {
  1935. if (! fAtomQueueIn.isEmpty())
  1936. {
  1937. uint32_t portIndex;
  1938. const LV2_Atom* atom;
  1939. k = fEventsIn.ctrlIndex;
  1940. while (fAtomQueueIn.get(&portIndex, &atom))
  1941. {
  1942. const uint32_t evInPadSize(lv2_atom_pad_size(sizeof(LV2_Atom_Event) + atom->size));
  1943. if (evInAtomOffsets[k] + evInPadSize >= MAX_EVENT_BUFFER)
  1944. break;
  1945. //if (atom->type == CARLA_URI_MAP_ID_ATOM_WORKER)
  1946. //{
  1947. // const LV2_Atom_Worker* const atomWorker = (const LV2_Atom_Worker*)atom;
  1948. // fExt.worker->work_response(fHandle, atomWorker->body.size, atomWorker->body.data);
  1949. // continue;
  1950. //}
  1951. LV2_Atom_Event* const aev(getLv2AtomEvent(fEventsIn.ctrl->atom, evInAtomOffsets[k]));
  1952. aev->time.frames = 0;
  1953. aev->body.type = atom->type;
  1954. aev->body.size = atom->size;
  1955. std::memcpy(LV2_ATOM_BODY(&aev->body), LV2_ATOM_BODY(atom), atom->size);
  1956. evInAtomOffsets[k] += evInPadSize;
  1957. fEventsIn.ctrl->atom->atom.size = evInAtomOffsets[k];
  1958. }
  1959. }
  1960. fAtomQueueIn.unlock();
  1961. }
  1962. // ----------------------------------------------------------------------------------------------------
  1963. // MIDI Input (External)
  1964. if (kData->extNotes.mutex.tryLock())
  1965. {
  1966. k = fEventsIn.ctrlIndex;
  1967. while (! kData->extNotes.data.isEmpty())
  1968. {
  1969. const ExternalMidiNote& note(kData->extNotes.data.getFirst(true));
  1970. CARLA_ASSERT(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  1971. uint8_t midiEvent[3] = { 0 };
  1972. midiEvent[0] = (note.velo > 0) ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF;
  1973. midiEvent[0] += note.channel;
  1974. midiEvent[1] = note.note;
  1975. midiEvent[2] = note.velo;
  1976. if (fEventsIn.ctrl->type & CARLA_EVENT_TYPE_MIDI)
  1977. {
  1978. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  1979. {
  1980. const uint32_t evInPadSize(lv2_atom_pad_size(sizeof(LV2_Atom_Event) + 3));
  1981. if (evInAtomOffsets[k] + evInPadSize >= MAX_EVENT_BUFFER)
  1982. break;
  1983. LV2_Atom_Event* const aev = getLv2AtomEvent(fEventsIn.ctrl->atom, evInAtomOffsets[k]);
  1984. aev->time.frames = 0;
  1985. aev->body.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  1986. aev->body.size = 3;
  1987. std::memcpy(LV2_ATOM_BODY(&aev->body), midiEvent, 3);
  1988. evInAtomOffsets[k] += evInPadSize;
  1989. fEventsIn.ctrl->atom->atom.size = evInAtomOffsets[k];
  1990. }
  1991. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  1992. {
  1993. lv2_event_write(&evInEventIters[k], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiEvent);
  1994. }
  1995. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  1996. {
  1997. lv2midi_put_event(&evInMidiStates[k], 0, 3, midiEvent);
  1998. }
  1999. }
  2000. }
  2001. kData->extNotes.mutex.unlock();
  2002. } // End of MIDI Input (External)
  2003. // ----------------------------------------------------------------------------------------------------
  2004. // Event Input (System)
  2005. bool allNotesOffSent = false;
  2006. bool sampleAccurate = (fOptions & PLUGIN_OPTION_FIXED_BUFFER) == 0;
  2007. uint32_t time, nEvents = fEventsIn.ctrl->port->getEventCount();
  2008. uint32_t startTime = 0;
  2009. uint32_t timeOffset = 0;
  2010. uint32_t nextBankId = 0;
  2011. if (kData->midiprog.current >= 0 && kData->midiprog.count > 0)
  2012. nextBankId = kData->midiprog.data[kData->midiprog.current].bank;
  2013. for (i=0; i < nEvents; ++i)
  2014. {
  2015. const EngineEvent& event(fEventsIn.ctrl->port->getEvent(i));
  2016. time = event.time;
  2017. if (time >= frames)
  2018. continue;
  2019. CARLA_ASSERT_INT2(time >= timeOffset, time, timeOffset);
  2020. if (time > timeOffset && sampleAccurate)
  2021. {
  2022. if (processSingle(inBuffer, outBuffer, time - timeOffset, timeOffset))
  2023. {
  2024. startTime = 0;
  2025. timeOffset = time;
  2026. if (kData->midiprog.current >= 0 && kData->midiprog.count > 0)
  2027. nextBankId = kData->midiprog.data[kData->midiprog.current].bank;
  2028. else
  2029. nextBankId = 0;
  2030. // reset iters
  2031. k = fEventsIn.ctrlIndex;
  2032. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2033. {
  2034. evInAtomOffsets[k] = 0;
  2035. fEventsIn.ctrl->atom->atom.size = 0;
  2036. }
  2037. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2038. {
  2039. lv2_event_buffer_reset(fEventsIn.ctrl->event, LV2_EVENT_AUDIO_STAMP, (uint8_t*)(fEventsIn.ctrl->event + 1));
  2040. lv2_event_begin(&evInEventIters[k], fEventsIn.ctrl->event);
  2041. }
  2042. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2043. {
  2044. evInMidiStates[k].position = 0;
  2045. evInMidiStates[k].midi->event_count = 0;
  2046. evInMidiStates[k].midi->size = 0;
  2047. }
  2048. }
  2049. else
  2050. startTime += timeOffset;
  2051. }
  2052. // Control change
  2053. switch (event.type)
  2054. {
  2055. case kEngineEventTypeNull:
  2056. break;
  2057. case kEngineEventTypeControl:
  2058. {
  2059. const EngineControlEvent& ctrlEvent = event.ctrl;
  2060. switch (ctrlEvent.type)
  2061. {
  2062. case kEngineControlEventTypeNull:
  2063. break;
  2064. case kEngineControlEventTypeParameter:
  2065. {
  2066. // Control backend stuff
  2067. if (event.channel == kData->ctrlChannel)
  2068. {
  2069. float value;
  2070. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (fHints & PLUGIN_CAN_DRYWET) > 0)
  2071. {
  2072. value = ctrlEvent.value;
  2073. setDryWet(value, false, false);
  2074. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  2075. continue;
  2076. }
  2077. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (fHints & PLUGIN_CAN_VOLUME) > 0)
  2078. {
  2079. value = ctrlEvent.value*127.0f/100.0f;
  2080. setVolume(value, false, false);
  2081. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  2082. continue;
  2083. }
  2084. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (fHints & PLUGIN_CAN_BALANCE) > 0)
  2085. {
  2086. float left, right;
  2087. value = ctrlEvent.value/0.5f - 1.0f;
  2088. if (value < 0.0f)
  2089. {
  2090. left = -1.0f;
  2091. right = (value*2.0f)+1.0f;
  2092. }
  2093. else if (value > 0.0f)
  2094. {
  2095. left = (value*2.0f)-1.0f;
  2096. right = 1.0f;
  2097. }
  2098. else
  2099. {
  2100. left = -1.0f;
  2101. right = 1.0f;
  2102. }
  2103. setBalanceLeft(left, false, false);
  2104. setBalanceRight(right, false, false);
  2105. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  2106. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  2107. continue;
  2108. }
  2109. }
  2110. // Control plugin parameters
  2111. for (k=0; k < kData->param.count; ++k)
  2112. {
  2113. if (kData->param.data[k].midiChannel != event.channel)
  2114. continue;
  2115. if (kData->param.data[k].midiCC != ctrlEvent.param)
  2116. continue;
  2117. if (kData->param.data[k].type != PARAMETER_INPUT)
  2118. continue;
  2119. if ((kData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  2120. continue;
  2121. float value;
  2122. if (kData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  2123. {
  2124. value = (ctrlEvent.value < 0.5f) ? kData->param.ranges[k].min : kData->param.ranges[k].max;
  2125. }
  2126. else
  2127. {
  2128. value = kData->param.ranges[i].unnormalizeValue(ctrlEvent.value);
  2129. if (kData->param.data[k].hints & PARAMETER_IS_INTEGER)
  2130. value = std::rint(value);
  2131. }
  2132. setParameterValue(k, value, false, false, false);
  2133. postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  2134. }
  2135. break;
  2136. }
  2137. case kEngineControlEventTypeMidiBank:
  2138. if (event.channel == kData->ctrlChannel && (fOptions & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  2139. nextBankId = ctrlEvent.param;
  2140. break;
  2141. case kEngineControlEventTypeMidiProgram:
  2142. if (event.channel == kData->ctrlChannel && (fOptions & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  2143. {
  2144. const uint32_t nextProgramId = ctrlEvent.param;
  2145. for (k=0; k < kData->midiprog.count; ++k)
  2146. {
  2147. if (kData->midiprog.data[k].bank == nextBankId && kData->midiprog.data[k].program == nextProgramId)
  2148. {
  2149. setMidiProgram(k, false, false, false);
  2150. postponeRtEvent(kPluginPostRtEventMidiProgramChange, k, 0, 0.0f);
  2151. break;
  2152. }
  2153. }
  2154. }
  2155. break;
  2156. case kEngineControlEventTypeAllSoundOff:
  2157. if (event.channel == kData->ctrlChannel)
  2158. {
  2159. if (! allNotesOffSent)
  2160. {
  2161. sendMidiAllNotesOff();
  2162. allNotesOffSent = true;
  2163. }
  2164. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_ACTIVE, 0, 0.0f);
  2165. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_ACTIVE, 0, 1.0f);
  2166. }
  2167. if (fOptions & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  2168. {
  2169. // TODO
  2170. }
  2171. break;
  2172. case kEngineControlEventTypeAllNotesOff:
  2173. if (event.channel == kData->ctrlChannel)
  2174. {
  2175. if (! allNotesOffSent)
  2176. {
  2177. allNotesOffSent = true;
  2178. sendMidiAllNotesOff();
  2179. }
  2180. }
  2181. if (fOptions & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  2182. {
  2183. // TODO
  2184. }
  2185. break;
  2186. }
  2187. break;
  2188. }
  2189. case kEngineEventTypeMidi:
  2190. {
  2191. const EngineMidiEvent& midiEvent = event.midi;
  2192. uint8_t status = MIDI_GET_STATUS_FROM_DATA(midiEvent.data);
  2193. uint8_t channel = event.channel;
  2194. uint32_t mtime = sampleAccurate ? startTime : time;
  2195. if (MIDI_IS_STATUS_AFTERTOUCH(status) && (fOptions & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  2196. continue;
  2197. if (MIDI_IS_STATUS_CONTROL_CHANGE(status) && (fOptions & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  2198. continue;
  2199. if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status) && (fOptions & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  2200. continue;
  2201. if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status) && (fOptions & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  2202. continue;
  2203. // Fix bad note-off
  2204. if (status == MIDI_STATUS_NOTE_ON && midiEvent.data[2] == 0)
  2205. status -= 0x10;
  2206. k = fEventsIn.ctrlIndex;
  2207. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2208. {
  2209. const uint32_t evInPadSize(lv2_atom_pad_size(sizeof(LV2_Atom_Event) + midiEvent.size));
  2210. if (evInAtomOffsets[k] + evInPadSize >= MAX_EVENT_BUFFER)
  2211. continue;
  2212. LV2_Atom_Event* const aev(getLv2AtomEvent(fEventsIn.ctrl->atom, evInAtomOffsets[k]));
  2213. aev->time.frames = mtime;
  2214. aev->body.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  2215. aev->body.size = midiEvent.size;
  2216. std::memcpy(LV2_ATOM_BODY(&aev->body), midiEvent.data, midiEvent.size);
  2217. evInAtomOffsets[k] += evInPadSize;
  2218. fEventsIn.ctrl->atom->atom.size = evInAtomOffsets[k];
  2219. }
  2220. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2221. {
  2222. lv2_event_write(&evInEventIters[k], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, midiEvent.size, midiEvent.data);
  2223. }
  2224. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2225. {
  2226. lv2midi_put_event(&evInMidiStates[k], mtime, midiEvent.size, midiEvent.data);
  2227. }
  2228. if (status == MIDI_STATUS_NOTE_ON)
  2229. postponeRtEvent(kPluginPostRtEventNoteOn, channel, midiEvent.data[1], midiEvent.data[2]);
  2230. else if (status == MIDI_STATUS_NOTE_OFF)
  2231. postponeRtEvent(kPluginPostRtEventNoteOff, channel, midiEvent.data[1], 0.0f);
  2232. break;
  2233. }
  2234. }
  2235. }
  2236. kData->postRtEvents.trySplice();
  2237. if (frames > timeOffset)
  2238. processSingle(inBuffer, outBuffer, frames - timeOffset, timeOffset);
  2239. } // End of Event Input and Processing
  2240. // --------------------------------------------------------------------------------------------------------
  2241. // Plugin processing (no events)
  2242. else
  2243. {
  2244. processSingle(inBuffer, outBuffer, frames, 0);
  2245. } // End of Plugin processing (no events)
  2246. CARLA_PROCESS_CONTINUE_CHECK;
  2247. // --------------------------------------------------------------------------------------------------------
  2248. // Control Output
  2249. if (kData->event.portOut != nullptr)
  2250. {
  2251. uint8_t channel;
  2252. uint16_t param;
  2253. float value;
  2254. for (k=0; k < kData->param.count; ++k)
  2255. {
  2256. if (kData->param.data[k].type != PARAMETER_OUTPUT)
  2257. continue;
  2258. kData->param.ranges[k].fixValue(fParamBuffers[k]);
  2259. if (kData->param.data[k].midiCC > 0)
  2260. {
  2261. channel = kData->param.data[k].midiChannel;
  2262. param = static_cast<uint16_t>(kData->param.data[k].midiCC);
  2263. value = kData->param.ranges[k].normalizeValue(fParamBuffers[k]);
  2264. kData->event.portOut->writeControlEvent(0, channel, kEngineControlEventTypeParameter, param, value);
  2265. }
  2266. }
  2267. } // End of Control Output
  2268. CARLA_PROCESS_CONTINUE_CHECK;
  2269. #if 0
  2270. // --------------------------------------------------------------------------------------------------------
  2271. // Final work
  2272. if (fExt.worker != nullptr && fExt.worker->end_run != nullptr)
  2273. {
  2274. fExt.worker->end_run(fHandle);
  2275. if (fHandle2 != nullptr)
  2276. fExt.worker->end_run(fHandle2);
  2277. }
  2278. #endif
  2279. // --------------------------------------------------------------------------------------------------------
  2280. }
  2281. bool processSingle(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t timeOffset)
  2282. {
  2283. CARLA_ASSERT(frames > 0);
  2284. if (frames == 0)
  2285. return false;
  2286. if (kData->audioIn.count > 0)
  2287. {
  2288. CARLA_ASSERT(inBuffer != nullptr);
  2289. if (inBuffer == nullptr)
  2290. return false;
  2291. }
  2292. if (kData->audioOut.count > 0)
  2293. {
  2294. CARLA_ASSERT(outBuffer != nullptr);
  2295. if (outBuffer == nullptr)
  2296. return false;
  2297. }
  2298. uint32_t i, k;
  2299. // --------------------------------------------------------------------------------------------------------
  2300. // Try lock, silence otherwise
  2301. if (kData->engine->isOffline())
  2302. {
  2303. kData->singleMutex.lock();
  2304. }
  2305. else if (! kData->singleMutex.tryLock())
  2306. {
  2307. for (i=0; i < kData->audioOut.count; ++i)
  2308. {
  2309. for (k=0; k < frames; ++k)
  2310. outBuffer[i][k+timeOffset] = 0.0f;
  2311. }
  2312. return false;
  2313. }
  2314. // --------------------------------------------------------------------------------------------------------
  2315. // Reset audio buffers
  2316. for (i=0; i < kData->audioIn.count; ++i)
  2317. carla_copyFloat(fAudioInBuffers[i], inBuffer[i]+timeOffset, frames);
  2318. for (i=0; i < kData->audioOut.count; ++i)
  2319. carla_zeroFloat(fAudioOutBuffers[i], frames);
  2320. // --------------------------------------------------------------------------------------------------------
  2321. // Run plugin
  2322. fDescriptor->run(fHandle, frames);
  2323. if (fHandle2 != nullptr)
  2324. fDescriptor->run(fHandle2, frames);
  2325. // --------------------------------------------------------------------------------------------------------
  2326. // Post-processing (dry/wet, volume and balance)
  2327. {
  2328. const bool doDryWet = (fHints & PLUGIN_CAN_DRYWET) != 0 && kData->postProc.dryWet != 1.0f;
  2329. const bool doBalance = (fHints & PLUGIN_CAN_BALANCE) != 0 && (kData->postProc.balanceLeft != -1.0f || kData->postProc.balanceRight != 1.0f);
  2330. bool isPair;
  2331. float bufValue, oldBufLeft[doBalance ? frames : 1];
  2332. for (i=0; i < kData->audioOut.count; ++i)
  2333. {
  2334. // Dry/Wet
  2335. if (doDryWet)
  2336. {
  2337. for (k=0; k < frames; ++k)
  2338. {
  2339. // TODO
  2340. //if (k < kData->latency && kData->latency < frames)
  2341. // bufValue = (kData->audioIn.count == 1) ? kData->latencyBuffers[0][k] : kData->latencyBuffers[i][k];
  2342. //else
  2343. // bufValue = (kData->audioIn.count == 1) ? inBuffer[0][k-m_latency] : inBuffer[i][k-m_latency];
  2344. bufValue = fAudioInBuffers[(kData->audioIn.count == 1) ? 0 : i][k];
  2345. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * kData->postProc.dryWet) + (bufValue * (1.0f - kData->postProc.dryWet));
  2346. }
  2347. }
  2348. // Balance
  2349. if (doBalance)
  2350. {
  2351. isPair = (i % 2 == 0);
  2352. if (isPair)
  2353. {
  2354. CARLA_ASSERT(i+1 < kData->audioOut.count);
  2355. carla_copyFloat(oldBufLeft, fAudioOutBuffers[i], frames);
  2356. }
  2357. float balRangeL = (kData->postProc.balanceLeft + 1.0f)/2.0f;
  2358. float balRangeR = (kData->postProc.balanceRight + 1.0f)/2.0f;
  2359. for (k=0; k < frames; ++k)
  2360. {
  2361. if (isPair)
  2362. {
  2363. // left
  2364. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  2365. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  2366. }
  2367. else
  2368. {
  2369. // right
  2370. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  2371. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  2372. }
  2373. }
  2374. }
  2375. // Volume (and buffer copy)
  2376. {
  2377. for (k=0; k < frames; ++k)
  2378. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k] * kData->postProc.volume;
  2379. }
  2380. }
  2381. #if 0
  2382. // Latency, save values for next callback, TODO
  2383. if (kData->latency > 0 && kData->latency < frames)
  2384. {
  2385. for (i=0; i < kData->audioIn.count; ++i)
  2386. carla_copyFloat(kData->latencyBuffers[i], inBuffer[i] + (frames - kData->latency), kData->latency);
  2387. }
  2388. #endif
  2389. } // End of Post-processing
  2390. // --------------------------------------------------------------------------------------------------------
  2391. kData->singleMutex.unlock();
  2392. return true;
  2393. }
  2394. void bufferSizeChanged(const uint32_t newBufferSize) override
  2395. {
  2396. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  2397. carla_debug("Lv2Plugin::bufferSizeChanged(%i) - start", newBufferSize);
  2398. for (uint32_t i=0; i < kData->audioIn.count; ++i)
  2399. {
  2400. if (fAudioInBuffers[i] != nullptr)
  2401. delete[] fAudioInBuffers[i];
  2402. fAudioInBuffers[i] = new float[newBufferSize];
  2403. }
  2404. for (uint32_t i=0; i < kData->audioOut.count; ++i)
  2405. {
  2406. if (fAudioOutBuffers[i] != nullptr)
  2407. delete[] fAudioOutBuffers[i];
  2408. fAudioOutBuffers[i] = new float[newBufferSize];
  2409. }
  2410. if (fHandle2 == nullptr)
  2411. {
  2412. for (uint32_t i=0; i < kData->audioIn.count; ++i)
  2413. {
  2414. CARLA_ASSERT(fAudioInBuffers[i] != nullptr);
  2415. fDescriptor->connect_port(fHandle, kData->audioIn.ports[i].rindex, fAudioInBuffers[i]);
  2416. }
  2417. for (uint32_t i=0; i < kData->audioOut.count; ++i)
  2418. {
  2419. CARLA_ASSERT(fAudioOutBuffers[i] != nullptr);
  2420. fDescriptor->connect_port(fHandle, kData->audioOut.ports[i].rindex, fAudioOutBuffers[i]);
  2421. }
  2422. }
  2423. else
  2424. {
  2425. if (kData->audioIn.count > 0)
  2426. {
  2427. CARLA_ASSERT(kData->audioIn.count == 2);
  2428. CARLA_ASSERT(fAudioInBuffers[0] != nullptr);
  2429. CARLA_ASSERT(fAudioInBuffers[1] != nullptr);
  2430. fDescriptor->connect_port(fHandle, kData->audioIn.ports[0].rindex, fAudioInBuffers[0]);
  2431. fDescriptor->connect_port(fHandle2, kData->audioIn.ports[1].rindex, fAudioInBuffers[1]);
  2432. }
  2433. if (kData->audioOut.count > 0)
  2434. {
  2435. CARLA_ASSERT(kData->audioOut.count == 2);
  2436. CARLA_ASSERT(fAudioOutBuffers[0] != nullptr);
  2437. CARLA_ASSERT(fAudioOutBuffers[1] != nullptr);
  2438. fDescriptor->connect_port(fHandle, kData->audioOut.ports[0].rindex, fAudioOutBuffers[0]);
  2439. fDescriptor->connect_port(fHandle2, kData->audioOut.ports[1].rindex, fAudioOutBuffers[1]);
  2440. }
  2441. }
  2442. if (fLv2Options.maxBufferSize != static_cast<int>(newBufferSize) || (fLv2Options.minBufferSize > 1 && fLv2Options.minBufferSize != static_cast<int>(newBufferSize)))
  2443. {
  2444. fLv2Options.maxBufferSize = newBufferSize;
  2445. if (fLv2Options.minBufferSize > 1)
  2446. fLv2Options.minBufferSize = newBufferSize;
  2447. if (fExt.options != nullptr && fExt.options->set != nullptr)
  2448. {
  2449. fExt.options->set(fHandle, &fLv2Options.optMinBlockLenth);
  2450. fExt.options->set(fHandle, &fLv2Options.optMaxBlockLenth);
  2451. }
  2452. }
  2453. carla_debug("Lv2Plugin::bufferSizeChanged(%i) - end", newBufferSize);
  2454. }
  2455. void sampleRateChanged(const double newSampleRate) override
  2456. {
  2457. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  2458. carla_debug("Lv2Plugin::sampleRateChanged(%g) - start", newSampleRate);
  2459. if (fLv2Options.sampleRate != newSampleRate)
  2460. {
  2461. fLv2Options.sampleRate = newSampleRate;
  2462. if (fExt.options != nullptr && fExt.options->set != nullptr)
  2463. fExt.options->set(fHandle, &fLv2Options.optSampleRate);
  2464. }
  2465. carla_debug("Lv2Plugin::sampleRateChanged(%g) - end", newSampleRate);
  2466. }
  2467. // -------------------------------------------------------------------
  2468. // Plugin buffers
  2469. void initBuffers() override
  2470. {
  2471. fEventsIn.initBuffers(kData->engine);
  2472. fEventsOut.initBuffers(kData->engine);
  2473. CarlaPlugin::initBuffers();
  2474. }
  2475. void clearBuffers() override
  2476. {
  2477. carla_debug("Lv2Plugin::clearBuffers() - start");
  2478. if (fAudioInBuffers != nullptr)
  2479. {
  2480. for (uint32_t i=0; i < kData->audioIn.count; ++i)
  2481. {
  2482. if (fAudioInBuffers[i] != nullptr)
  2483. {
  2484. delete[] fAudioInBuffers[i];
  2485. fAudioInBuffers[i] = nullptr;
  2486. }
  2487. }
  2488. delete[] fAudioInBuffers;
  2489. fAudioInBuffers = nullptr;
  2490. }
  2491. if (fAudioOutBuffers != nullptr)
  2492. {
  2493. for (uint32_t i=0; i < kData->audioOut.count; ++i)
  2494. {
  2495. if (fAudioOutBuffers[i] != nullptr)
  2496. {
  2497. delete[] fAudioOutBuffers[i];
  2498. fAudioOutBuffers[i] = nullptr;
  2499. }
  2500. }
  2501. delete[] fAudioOutBuffers;
  2502. fAudioOutBuffers = nullptr;
  2503. }
  2504. if (fParamBuffers != nullptr)
  2505. {
  2506. delete[] fParamBuffers;
  2507. fParamBuffers = nullptr;
  2508. }
  2509. fEventsIn.clear();
  2510. fEventsOut.clear();
  2511. CarlaPlugin::clearBuffers();
  2512. carla_debug("Lv2Plugin::clearBuffers() - end");
  2513. }
  2514. // -------------------------------------------------------------------
  2515. // Post-poned UI Stuff
  2516. void uiParameterChange(const uint32_t index, const float value) override
  2517. {
  2518. CARLA_ASSERT(fDescriptor != nullptr);
  2519. CARLA_ASSERT(fHandle != nullptr);
  2520. CARLA_ASSERT(index < kData->param.count);
  2521. if (fDescriptor == nullptr || fHandle == nullptr)
  2522. return;
  2523. if (index >= kData->param.count)
  2524. return;
  2525. if (fUi.type == PLUGIN_UI_OSC)
  2526. {
  2527. if (kData->osc.data.target != nullptr)
  2528. osc_send_control(&kData->osc.data, kData->param.data[index].rindex, value);
  2529. }
  2530. else
  2531. {
  2532. if (fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->port_event != nullptr)
  2533. fUi.descriptor->port_event(fUi.handle, kData->param.data[index].rindex, sizeof(float), 0, &value);
  2534. }
  2535. }
  2536. void uiMidiProgramChange(const uint32_t index) override
  2537. {
  2538. CARLA_ASSERT(index < kData->midiprog.count);
  2539. if (index >= kData->midiprog.count)
  2540. return;
  2541. if (fUi.type == PLUGIN_UI_OSC)
  2542. {
  2543. if (kData->osc.data.target != nullptr)
  2544. osc_send_midi_program(&kData->osc.data, kData->midiprog.data[index].bank, kData->midiprog.data[index].program);
  2545. }
  2546. else
  2547. {
  2548. if (fExt.uiprograms != nullptr && fExt.uiprograms->select_program != nullptr)
  2549. fExt.uiprograms->select_program(fUi.handle, kData->midiprog.data[index].bank, kData->midiprog.data[index].program);
  2550. }
  2551. }
  2552. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) override
  2553. {
  2554. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  2555. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  2556. CARLA_ASSERT(velo > 0 && velo < MAX_MIDI_VALUE);
  2557. if (channel >= MAX_MIDI_CHANNELS)
  2558. return;
  2559. if (note >= MAX_MIDI_NOTE)
  2560. return;
  2561. if (velo >= MAX_MIDI_VALUE)
  2562. return;
  2563. if (fUi.type == PLUGIN_UI_OSC)
  2564. {
  2565. if (kData->osc.data.target != nullptr)
  2566. {
  2567. uint8_t midiData[4] = { 0 };
  2568. midiData[1] = MIDI_STATUS_NOTE_ON + channel;
  2569. midiData[2] = note;
  2570. midiData[3] = velo;
  2571. osc_send_midi(&kData->osc.data, midiData);
  2572. }
  2573. }
  2574. else
  2575. {
  2576. if (fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->port_event != nullptr && fEventsIn.ctrl != nullptr)
  2577. {
  2578. LV2_Atom_MidiEvent midiEv;
  2579. midiEv.event.time.frames = 0;
  2580. midiEv.event.body.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  2581. midiEv.event.body.size = 3;
  2582. midiEv.data[0] = MIDI_STATUS_NOTE_OFF + channel;
  2583. midiEv.data[1] = note;
  2584. midiEv.data[2] = velo;
  2585. fUi.descriptor->port_event(fUi.handle, fEventsIn.ctrl->rindex, 3, CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM, &midiEv);
  2586. }
  2587. }
  2588. }
  2589. void uiNoteOff(const uint8_t channel, const uint8_t note) override
  2590. {
  2591. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  2592. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  2593. if (channel >= MAX_MIDI_CHANNELS)
  2594. return;
  2595. if (note >= MAX_MIDI_NOTE)
  2596. return;
  2597. if (fUi.type == PLUGIN_UI_OSC)
  2598. {
  2599. if (kData->osc.data.target != nullptr)
  2600. {
  2601. uint8_t midiData[4] = { 0 };
  2602. midiData[1] = MIDI_STATUS_NOTE_OFF + channel;
  2603. midiData[2] = note;
  2604. osc_send_midi(&kData->osc.data, midiData);
  2605. }
  2606. }
  2607. else
  2608. {
  2609. if (fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->port_event != nullptr && fEventsIn.ctrl != nullptr)
  2610. {
  2611. LV2_Atom_MidiEvent midiEv;
  2612. midiEv.event.time.frames = 0;
  2613. midiEv.event.body.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  2614. midiEv.event.body.size = 3;
  2615. midiEv.data[0] = MIDI_STATUS_NOTE_OFF + channel;
  2616. midiEv.data[1] = note;
  2617. midiEv.data[2] = 0;
  2618. fUi.descriptor->port_event(fUi.handle, fEventsIn.ctrl->rindex, 3, CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM, &midiEv);
  2619. }
  2620. }
  2621. }
  2622. // -------------------------------------------------------------------
  2623. protected:
  2624. void guiClosedCallback() override
  2625. {
  2626. showGui(false);
  2627. kData->engine->callback(CALLBACK_SHOW_GUI, fId, 0, 0, 0.0f, nullptr);
  2628. }
  2629. // -------------------------------------------------------------------
  2630. uint32_t getCustomURID(const char* const uri)
  2631. {
  2632. CARLA_ASSERT(uri != nullptr);
  2633. carla_debug("Lv2Plugin::getCustomURID(\"%s\")", uri);
  2634. if (uri == nullptr)
  2635. return CARLA_URI_MAP_ID_NULL;
  2636. for (size_t i=0; i < fCustomURIDs.count(); ++i)
  2637. {
  2638. const char*& thisUri(fCustomURIDs.getAt(i));
  2639. if (thisUri != nullptr && std::strcmp(thisUri, uri) == 0)
  2640. return i;
  2641. }
  2642. fCustomURIDs.append(carla_strdup(uri));
  2643. return fCustomURIDs.count()-1;
  2644. }
  2645. const char* getCustomURIString(const LV2_URID urid)
  2646. {
  2647. CARLA_ASSERT(urid != CARLA_URI_MAP_ID_NULL);
  2648. CARLA_ASSERT_INT2(urid < fCustomURIDs.count(), urid, fCustomURIDs.count());
  2649. carla_debug("Lv2Plugin::getCustomURIString(%i)", urid);
  2650. if (urid == CARLA_URI_MAP_ID_NULL)
  2651. return nullptr;
  2652. if (urid < fCustomURIDs.count())
  2653. return fCustomURIDs.getAt(urid);
  2654. return nullptr;
  2655. }
  2656. // -------------------------------------------------------------------
  2657. void handleProgramChanged(const int32_t index)
  2658. {
  2659. CARLA_ASSERT_INT(index >= -1, index);
  2660. carla_debug("Lv2Plugin::handleProgramChanged(%i)", index);
  2661. if (index == -1)
  2662. {
  2663. const CarlaPlugin::ScopedDisabler m(this);
  2664. return reloadPrograms(false);
  2665. }
  2666. if (index >= 0 && index < static_cast<int32_t>(kData->midiprog.count) && fExt.programs != nullptr && fExt.programs->get_program != nullptr)
  2667. {
  2668. if (const LV2_Program_Descriptor* progDesc = fExt.programs->get_program(fHandle, index))
  2669. {
  2670. CARLA_ASSERT(progDesc->name != nullptr);
  2671. if (kData->midiprog.data[index].name != nullptr)
  2672. delete[] kData->midiprog.data[index].name;
  2673. kData->midiprog.data[index].name = carla_strdup(progDesc->name ? progDesc->name : "");
  2674. if (index == kData->midiprog.current)
  2675. kData->engine->callback(CALLBACK_UPDATE, fId, 0, 0, 0.0, nullptr);
  2676. else
  2677. kData->engine->callback(CALLBACK_RELOAD_PROGRAMS, fId, 0, 0, 0.0, nullptr);
  2678. }
  2679. }
  2680. }
  2681. // -------------------------------------------------------------------
  2682. LV2_State_Status handleStateStore(const uint32_t key, const void* const value, const size_t size, const uint32_t type, const uint32_t flags)
  2683. {
  2684. CARLA_ASSERT(key != CARLA_URI_MAP_ID_NULL);
  2685. CARLA_ASSERT(value != nullptr);
  2686. CARLA_ASSERT(size > 0);
  2687. carla_debug("Lv2Plugin::handleStateStore(%i, %p, " P_SIZE ", %i, %i)", key, value, size, type, flags);
  2688. // basic checks
  2689. if (key == CARLA_URI_MAP_ID_NULL)
  2690. {
  2691. carla_stderr2("Lv2Plugin::handleStateStore(%i, %p, " P_SIZE ", %i, %i) - invalid key", key, value, size, type, flags);
  2692. return LV2_STATE_ERR_NO_PROPERTY;
  2693. }
  2694. if (value == nullptr || size == 0)
  2695. {
  2696. carla_stderr2("Lv2Plugin::handleStateStore(%i, %p, " P_SIZE ", %i, %i) - invalid value", key, value, size, type, flags);
  2697. return LV2_STATE_ERR_NO_PROPERTY;
  2698. }
  2699. if ((flags & LV2_STATE_IS_POD) == 0)
  2700. {
  2701. carla_stderr2("Lv2Plugin::handleStateStore(%i, %p, " P_SIZE ", %i, %i) - invalid flags", key, value, size, type, flags);
  2702. return LV2_STATE_ERR_BAD_FLAGS;
  2703. }
  2704. const char* const stype(carla_lv2_urid_unmap(this, type));
  2705. if (stype == nullptr)
  2706. {
  2707. carla_stderr2("Lv2Plugin::handleStateStore(%i, %p, " P_SIZE ", %i, %i) - invalid type", key, value, size, type, flags);
  2708. return LV2_STATE_ERR_BAD_TYPE;
  2709. }
  2710. const char* const uriKey(carla_lv2_urid_unmap(this, key));
  2711. if (uriKey == nullptr)
  2712. {
  2713. carla_stderr2("Lv2Plugin::handleStateStore(%i, %p, " P_SIZE ", %i, %i) - invalid key URI", key, value, size, type, flags);
  2714. return LV2_STATE_ERR_NO_PROPERTY;
  2715. }
  2716. // Check if we already have this key
  2717. for (auto it = kData->custom.begin(); it.valid(); it.next())
  2718. {
  2719. CustomData& data(*it);
  2720. if (std::strcmp(data.key, uriKey) == 0)
  2721. {
  2722. if (data.value != nullptr)
  2723. delete[] data.value;
  2724. if (std::strcmp(stype, LV2_ATOM__String) == 0 || std::strcmp(stype, LV2_ATOM__Path) == 0)
  2725. data.value = carla_strdup((const char*)value);
  2726. else
  2727. data.value = carla_strdup(QByteArray((const char*)value, size).toBase64().constData());
  2728. return LV2_STATE_SUCCESS;
  2729. }
  2730. }
  2731. // Otherwise store it
  2732. CustomData newData;
  2733. newData.type = carla_strdup(stype);
  2734. newData.key = carla_strdup(uriKey);
  2735. if (std::strcmp(stype, LV2_ATOM__String) == 0 || std::strcmp(stype, LV2_ATOM__Path) == 0)
  2736. newData.value = carla_strdup((const char*)value);
  2737. else
  2738. newData.value = carla_strdup(QByteArray((const char*)value, size).toBase64().constData());
  2739. kData->custom.append(newData);
  2740. return LV2_STATE_SUCCESS;
  2741. }
  2742. const void* handleStateRetrieve(const uint32_t key, size_t* const size, uint32_t* const type, uint32_t* const flags)
  2743. {
  2744. CARLA_ASSERT(key != CARLA_URI_MAP_ID_NULL);
  2745. carla_debug("Lv2Plugin::handleStateRetrieve(%i, %p, %p, %p)", key, size, type, flags);
  2746. // basic checks
  2747. if (key == CARLA_URI_MAP_ID_NULL)
  2748. {
  2749. carla_stderr2("Lv2Plugin::handleStateRetrieve(%i, %p, %p, %p) - invalid key", key, size, type, flags);
  2750. return nullptr;
  2751. }
  2752. if (size == nullptr || type == nullptr || flags == nullptr)
  2753. {
  2754. carla_stderr2("Lv2Plugin::handleStateRetrieve(%i, %p, %p, %p) - invalid data", key, size, type, flags);
  2755. return nullptr;
  2756. }
  2757. const char* const uriKey(carla_lv2_urid_unmap(this, key));
  2758. if (uriKey == nullptr)
  2759. {
  2760. carla_stderr2("Lv2Plugin::handleStateRetrieve(%i, %p, %p, %p) - failed to find key", key, size, type, flags);
  2761. return nullptr;
  2762. }
  2763. const char* stype = nullptr;
  2764. const char* stringData = nullptr;
  2765. for (auto it = kData->custom.begin(); it.valid(); it.next())
  2766. {
  2767. CustomData& data(*it);
  2768. if (std::strcmp(data.key, uriKey) == 0)
  2769. {
  2770. stype = data.type;
  2771. stringData = data.value;
  2772. break;
  2773. }
  2774. }
  2775. if (stringData == nullptr)
  2776. {
  2777. carla_stderr2("Lv2Plugin::handleStateRetrieve(%i, %p, %p, %p) - invalid key '%s'", key, size, type, flags, uriKey);
  2778. return nullptr;
  2779. }
  2780. *type = carla_lv2_urid_map(this, stype);
  2781. *flags = LV2_STATE_IS_POD;
  2782. if (std::strcmp(stype, LV2_ATOM__String) == 0 || std::strcmp(stype, LV2_ATOM__Path) == 0)
  2783. {
  2784. *size = std::strlen(stringData);
  2785. return stringData;
  2786. }
  2787. else
  2788. {
  2789. static QByteArray chunk;
  2790. chunk = QByteArray::fromBase64(stringData);
  2791. *size = chunk.size();
  2792. return chunk.constData();
  2793. }
  2794. }
  2795. // -------------------------------------------------------------------
  2796. LV2_Worker_Status handleWorkerSchedule(const uint32_t size, const void* const data)
  2797. {
  2798. carla_stdout("Lv2Plugin::handleWorkerSchedule(%i, %p)", size, data);
  2799. if (fExt.worker == nullptr || fExt.worker->work == nullptr)
  2800. {
  2801. carla_stderr("Lv2Plugin::handleWorkerSchedule(%i, %p) - plugin has no worker", size, data);
  2802. return LV2_WORKER_ERR_UNKNOWN;
  2803. }
  2804. //if (kData->engine->isOffline())
  2805. fExt.worker->work(fHandle, carla_lv2_worker_respond, this, size, data);
  2806. //else
  2807. // postponeEvent(PluginPostEventCustom, size, 0, 0.0, data);
  2808. return LV2_WORKER_SUCCESS;
  2809. }
  2810. LV2_Worker_Status handleWorkerRespond(const uint32_t size, const void* const data)
  2811. {
  2812. carla_stdout("Lv2Plugin::handleWorkerRespond(%i, %p)", size, data);
  2813. #if 0
  2814. LV2_Atom_Worker workerAtom;
  2815. workerAtom.atom.type = CARLA_URI_MAP_ID_ATOM_WORKER;
  2816. workerAtom.atom.size = sizeof(LV2_Atom_Worker_Body);
  2817. workerAtom.body.size = size;
  2818. workerAtom.body.data = data;
  2819. atomQueueIn.put(0, (const LV2_Atom*)&workerAtom);
  2820. #endif
  2821. return LV2_WORKER_SUCCESS;
  2822. }
  2823. // -------------------------------------------------------------------
  2824. void handleExternalUiClosed()
  2825. {
  2826. CARLA_ASSERT(fUi.type == PLUGIN_UI_EXTERNAL);
  2827. if (fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->cleanup != nullptr)
  2828. fUi.descriptor->cleanup(fUi.handle);
  2829. fUi.handle = nullptr;
  2830. kData->engine->callback(CALLBACK_SHOW_GUI, fId, 0, 0, 0.0f, nullptr);
  2831. }
  2832. uint32_t handleUiPortMap(const char* const symbol)
  2833. {
  2834. CARLA_ASSERT(symbol != nullptr);
  2835. if (symbol == nullptr)
  2836. return LV2UI_INVALID_PORT_INDEX;
  2837. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  2838. {
  2839. if (std::strcmp(fRdfDescriptor->Ports[i].Symbol, symbol) == 0)
  2840. return i;
  2841. }
  2842. return LV2UI_INVALID_PORT_INDEX;
  2843. }
  2844. int handleUiResize(const int width, const int height)
  2845. {
  2846. CARLA_ASSERT(kData->gui != nullptr);
  2847. CARLA_ASSERT(width > 0);
  2848. CARLA_ASSERT(height > 0);
  2849. if (width <= 0 || height <= 0)
  2850. return 1;
  2851. if (kData->gui != nullptr)
  2852. kData->gui->setSize(width, height);
  2853. return 0;
  2854. }
  2855. void handleUiWrite(const uint32_t rindex, const uint32_t bufferSize, const uint32_t format, const void* const buffer)
  2856. {
  2857. if (format == 0)
  2858. {
  2859. CARLA_ASSERT(buffer != nullptr);
  2860. CARLA_ASSERT(bufferSize == sizeof(float));
  2861. if (buffer == nullptr || bufferSize != sizeof(float))
  2862. return;
  2863. float value = *(float*)buffer;
  2864. for (uint32_t i=0; i < kData->param.count; ++i)
  2865. {
  2866. if (kData->param.data[i].rindex == static_cast<int32_t>(rindex))
  2867. {
  2868. if (fParamBuffers[i] != value)
  2869. setParameterValue(i, value, false, true, true);
  2870. break;
  2871. }
  2872. }
  2873. }
  2874. else if (format == CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM)
  2875. {
  2876. CARLA_ASSERT(buffer != nullptr);
  2877. if (buffer == nullptr)
  2878. return;
  2879. const LV2_Atom* const atom = (const LV2_Atom*)buffer;
  2880. fAtomQueueIn.put(rindex, atom);
  2881. }
  2882. else if (format == CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT)
  2883. {
  2884. CARLA_ASSERT(buffer != nullptr);
  2885. if (buffer == nullptr)
  2886. return;
  2887. const LV2_Atom* const atom = (const LV2_Atom*)buffer;
  2888. fAtomQueueIn.put(rindex, atom);
  2889. }
  2890. }
  2891. // -------------------------------------------------------------------
  2892. bool isRealtimeSafe() const
  2893. {
  2894. CARLA_ASSERT(fRdfDescriptor != nullptr);
  2895. for (uint32_t i=0; i < fRdfDescriptor->FeatureCount; ++i)
  2896. {
  2897. if (std::strcmp(fRdfDescriptor->Features[i].URI, LV2_CORE__hardRTCapable) == 0)
  2898. return true;
  2899. }
  2900. return false;
  2901. }
  2902. bool needsFixedBuffer() const
  2903. {
  2904. CARLA_ASSERT(fRdfDescriptor != nullptr);
  2905. for (uint32_t i=0; i < fRdfDescriptor->FeatureCount; ++i)
  2906. {
  2907. if (std::strcmp(fRdfDescriptor->Features[i].URI, LV2_BUF_SIZE__fixedBlockLength) == 0)
  2908. return true;
  2909. }
  2910. return false;
  2911. }
  2912. // -------------------------------------------------------------------
  2913. const char* getUiBridgePath(const LV2_Property type) const
  2914. {
  2915. const EngineOptions& options(kData->engine->getOptions());
  2916. switch (type)
  2917. {
  2918. case LV2_UI_GTK2:
  2919. return options.bridge_lv2Gtk2;
  2920. case LV2_UI_GTK3:
  2921. return options.bridge_lv2Gtk3;
  2922. case LV2_UI_QT4:
  2923. return options.bridge_lv2Qt4;
  2924. case LV2_UI_QT5:
  2925. return options.bridge_lv2Qt5;
  2926. case LV2_UI_COCOA:
  2927. return options.bridge_lv2Cocoa;
  2928. case LV2_UI_WINDOWS:
  2929. return options.bridge_lv2Win;
  2930. case LV2_UI_X11:
  2931. return options.bridge_lv2X11;
  2932. default:
  2933. return nullptr;
  2934. }
  2935. }
  2936. bool isUiBridgeable(const uint32_t uiId) const
  2937. {
  2938. const LV2_RDF_UI& rdfUi(fRdfDescriptor->UIs[uiId]);
  2939. for (uint32_t i=0; i < rdfUi.FeatureCount; ++i)
  2940. {
  2941. if (std::strcmp(rdfUi.Features[i].URI, LV2_INSTANCE_ACCESS_URI) == 0)
  2942. return false;
  2943. if (std::strcmp(rdfUi.Features[i].URI, LV2_DATA_ACCESS_URI) == 0)
  2944. return false;
  2945. }
  2946. return true;
  2947. }
  2948. bool isUiResizable() const
  2949. {
  2950. for (uint32_t i=0; i < fUi.rdfDescriptor->FeatureCount; ++i)
  2951. {
  2952. if (std::strcmp(fUi.rdfDescriptor->Features[i].URI, LV2_UI__fixedSize) == 0)
  2953. return false;
  2954. if (std::strcmp(fUi.rdfDescriptor->Features[i].URI, LV2_UI__noUserResize) == 0)
  2955. return false;
  2956. }
  2957. return true;
  2958. }
  2959. void updateUi()
  2960. {
  2961. CARLA_ASSERT(fUi.handle != nullptr);
  2962. CARLA_ASSERT(fUi.descriptor != nullptr);
  2963. fExt.uiidle = nullptr;
  2964. fExt.uiprograms = nullptr;
  2965. if (fUi.descriptor->extension_data != nullptr)
  2966. {
  2967. fExt.uiidle = (const LV2UI_Idle_Interface*)fUi.descriptor->extension_data(LV2_UI__idleInterface);
  2968. fExt.uiprograms = (const LV2_Programs_UI_Interface*)fUi.descriptor->extension_data(LV2_PROGRAMS__UIInterface);
  2969. // check if invalid
  2970. if (fExt.uiidle != nullptr && fExt.uiidle->idle == nullptr)
  2971. fExt.uiidle = nullptr;
  2972. if (fExt.uiprograms != nullptr && fExt.uiprograms->select_program == nullptr)
  2973. fExt.uiprograms = nullptr;
  2974. // update midi program
  2975. if (fExt.uiprograms && kData->midiprog.count > 0 && kData->midiprog.current >= 0)
  2976. fExt.uiprograms->select_program(fUi.handle, kData->midiprog.data[kData->midiprog.current].bank,
  2977. kData->midiprog.data[kData->midiprog.current].program);
  2978. }
  2979. if (fUi.descriptor->port_event != nullptr)
  2980. {
  2981. // update control ports
  2982. float value;
  2983. for (uint32_t i=0; i < kData->param.count; ++i)
  2984. {
  2985. value = getParameterValue(i);
  2986. fUi.descriptor->port_event(fUi.handle, kData->param.data[i].rindex, sizeof(float), CARLA_URI_MAP_ID_NULL, &value);
  2987. }
  2988. }
  2989. }
  2990. // -------------------------------------------------------------------
  2991. public:
  2992. bool init(const char* const name, const char* const uri)
  2993. {
  2994. CARLA_ASSERT(kData->engine != nullptr);
  2995. CARLA_ASSERT(kData->client == nullptr);
  2996. CARLA_ASSERT(uri != nullptr);
  2997. // ---------------------------------------------------------------
  2998. // first checks
  2999. if (kData->engine == nullptr)
  3000. {
  3001. return false;
  3002. }
  3003. if (kData->client != nullptr)
  3004. {
  3005. kData->engine->setLastError("Plugin client is already registered");
  3006. return false;
  3007. }
  3008. if (uri == nullptr)
  3009. {
  3010. kData->engine->setLastError("null uri");
  3011. return false;
  3012. }
  3013. // ---------------------------------------------------------------
  3014. // get plugin from lv2_rdf (lilv)
  3015. gLv2World.init();
  3016. fRdfDescriptor = lv2_rdf_new(uri);
  3017. if (fRdfDescriptor == nullptr)
  3018. {
  3019. kData->engine->setLastError("Failed to find the requested plugin in the LV2 Bundle");
  3020. return false;
  3021. }
  3022. // ---------------------------------------------------------------
  3023. // open DLL
  3024. if (! kData->libOpen(fRdfDescriptor->Binary))
  3025. {
  3026. kData->engine->setLastError(kData->libError(fRdfDescriptor->Binary));
  3027. return false;
  3028. }
  3029. // ---------------------------------------------------------------
  3030. // initialize options
  3031. fLv2Options.minBufferSize = 1;
  3032. fLv2Options.maxBufferSize = kData->engine->getBufferSize();
  3033. fLv2Options.sampleRate = kData->engine->getSampleRate();
  3034. // ---------------------------------------------------------------
  3035. // initialize features (part 1)
  3036. LV2_Event_Feature* const eventFt = new LV2_Event_Feature;
  3037. eventFt->callback_data = this;
  3038. eventFt->lv2_event_ref = carla_lv2_event_ref;
  3039. eventFt->lv2_event_unref = carla_lv2_event_unref;
  3040. LV2_Log_Log* const logFt = new LV2_Log_Log;
  3041. logFt->handle = this;
  3042. logFt->printf = carla_lv2_log_printf;
  3043. logFt->vprintf = carla_lv2_log_vprintf;
  3044. LV2_State_Make_Path* const stateMakePathFt = new LV2_State_Make_Path;
  3045. stateMakePathFt->handle = this;
  3046. stateMakePathFt->path = carla_lv2_state_make_path;
  3047. LV2_State_Map_Path* const stateMapPathFt = new LV2_State_Map_Path;
  3048. stateMapPathFt->handle = this;
  3049. stateMapPathFt->abstract_path = carla_lv2_state_map_abstract_path;
  3050. stateMapPathFt->absolute_path = carla_lv2_state_map_absolute_path;
  3051. LV2_Programs_Host* const programsFt = new LV2_Programs_Host;
  3052. programsFt->handle = this;
  3053. programsFt->program_changed = carla_lv2_program_changed;
  3054. LV2_RtMemPool_Pool* const rtMemPoolFt = new LV2_RtMemPool_Pool;
  3055. lv2_rtmempool_init(rtMemPoolFt);
  3056. LV2_URI_Map_Feature* const uriMapFt = new LV2_URI_Map_Feature;
  3057. uriMapFt->callback_data = this;
  3058. uriMapFt->uri_to_id = carla_lv2_uri_to_id;
  3059. LV2_URID_Map* const uridMapFt = new LV2_URID_Map;
  3060. uridMapFt->handle = this;
  3061. uridMapFt->map = carla_lv2_urid_map;
  3062. LV2_URID_Unmap* const uridUnmapFt = new LV2_URID_Unmap;
  3063. uridUnmapFt->handle = this;
  3064. uridUnmapFt->unmap = carla_lv2_urid_unmap;
  3065. LV2_Worker_Schedule* const workerFt = new LV2_Worker_Schedule;
  3066. workerFt->handle = this;
  3067. workerFt->schedule_work = carla_lv2_worker_schedule;
  3068. // ---------------------------------------------------------------
  3069. // initialize features (part 2)
  3070. fFeatures[kFeatureIdBufSizeBounded] = new LV2_Feature;
  3071. fFeatures[kFeatureIdBufSizeBounded]->URI = LV2_BUF_SIZE__boundedBlockLength;
  3072. fFeatures[kFeatureIdBufSizeBounded]->data = nullptr;
  3073. fFeatures[kFeatureIdBufSizeFixed] = new LV2_Feature;
  3074. fFeatures[kFeatureIdBufSizeFixed]->URI = LV2_BUF_SIZE__fixedBlockLength;
  3075. fFeatures[kFeatureIdBufSizeFixed]->data = nullptr;
  3076. fFeatures[kFeatureIdBufSizePowerOf2] = new LV2_Feature;
  3077. fFeatures[kFeatureIdBufSizePowerOf2]->URI = LV2_BUF_SIZE__powerOf2BlockLength;
  3078. fFeatures[kFeatureIdBufSizePowerOf2]->data = nullptr;
  3079. fFeatures[kFeatureIdEvent] = new LV2_Feature;
  3080. fFeatures[kFeatureIdEvent]->URI = LV2_EVENT_URI;
  3081. fFeatures[kFeatureIdEvent]->data = eventFt;
  3082. fFeatures[kFeatureIdLogs] = new LV2_Feature;
  3083. fFeatures[kFeatureIdLogs]->URI = LV2_LOG__log;
  3084. fFeatures[kFeatureIdLogs]->data = logFt;
  3085. fFeatures[kFeatureIdOptions] = new LV2_Feature;
  3086. fFeatures[kFeatureIdOptions]->URI = LV2_OPTIONS__options;
  3087. fFeatures[kFeatureIdOptions]->data = fLv2Options.opts;
  3088. fFeatures[kFeatureIdPrograms] = new LV2_Feature;
  3089. fFeatures[kFeatureIdPrograms]->URI = LV2_PROGRAMS__Host;
  3090. fFeatures[kFeatureIdPrograms]->data = programsFt;
  3091. fFeatures[kFeatureIdRtMemPool] = new LV2_Feature;
  3092. fFeatures[kFeatureIdRtMemPool]->URI = LV2_RTSAFE_MEMORY_POOL__Pool;
  3093. fFeatures[kFeatureIdRtMemPool]->data = rtMemPoolFt;
  3094. fFeatures[kFeatureIdStateMakePath] = new LV2_Feature;
  3095. fFeatures[kFeatureIdStateMakePath]->URI = LV2_STATE__makePath;
  3096. fFeatures[kFeatureIdStateMakePath]->data = stateMakePathFt;
  3097. fFeatures[kFeatureIdStateMapPath] = new LV2_Feature;
  3098. fFeatures[kFeatureIdStateMapPath]->URI = LV2_STATE__mapPath;
  3099. fFeatures[kFeatureIdStateMapPath]->data = stateMapPathFt;
  3100. fFeatures[kFeatureIdStrictBounds] = new LV2_Feature;
  3101. fFeatures[kFeatureIdStrictBounds]->URI = LV2_PORT_PROPS__supportsStrictBounds;
  3102. fFeatures[kFeatureIdStrictBounds]->data = nullptr;
  3103. fFeatures[kFeatureIdUriMap] = new LV2_Feature;
  3104. fFeatures[kFeatureIdUriMap]->URI = LV2_URI_MAP_URI;
  3105. fFeatures[kFeatureIdUriMap]->data = uriMapFt;
  3106. fFeatures[kFeatureIdUridMap] = new LV2_Feature;
  3107. fFeatures[kFeatureIdUridMap]->URI = LV2_URID__map;
  3108. fFeatures[kFeatureIdUridMap]->data = uridMapFt;
  3109. fFeatures[kFeatureIdUridUnmap] = new LV2_Feature;
  3110. fFeatures[kFeatureIdUridUnmap]->URI = LV2_URID__unmap;
  3111. fFeatures[kFeatureIdUridUnmap]->data = uridUnmapFt;
  3112. fFeatures[kFeatureIdWorker] = new LV2_Feature;
  3113. fFeatures[kFeatureIdWorker]->URI = LV2_WORKER__schedule;
  3114. fFeatures[kFeatureIdWorker]->data = workerFt;
  3115. if (! needsFixedBuffer())
  3116. fFeatures[kFeatureIdBufSizeFixed]->URI = LV2_BUF_SIZE__boundedBlockLength;
  3117. // ---------------------------------------------------------------
  3118. // get DLL main entry
  3119. #if 0
  3120. const LV2_Lib_Descriptor_Function libDescFn = (LV2_Lib_Descriptor_Function)kData->libSymbol("lv2_lib_descriptor");
  3121. if (libDescFn != nullptr)
  3122. {
  3123. // -----------------------------------------------------------
  3124. // get lib descriptor
  3125. const LV2_Lib_Descriptor* libFn = nullptr; //descLibFn(fRdfDescriptor->Bundle, features);
  3126. if (libFn == nullptr || libFn->get_plugin == nullptr)
  3127. {
  3128. kData->engine->setLastError("Plugin failed to return library descriptor");
  3129. return false;
  3130. }
  3131. // -----------------------------------------------------------
  3132. // get descriptor that matches URI
  3133. uint32_t i = 0;
  3134. while ((fDescriptor = libFn->get_plugin(libFn->handle, i++)))
  3135. {
  3136. if (std::strcmp(fDescriptor->URI, uri) == 0)
  3137. break;
  3138. }
  3139. if (fDescriptor == nullptr)
  3140. libFn->cleanup(libFn->handle);
  3141. else
  3142. #endif
  3143. {
  3144. const LV2_Descriptor_Function descFn = (LV2_Descriptor_Function)kData->libSymbol("lv2_descriptor");
  3145. if (descFn == nullptr)
  3146. {
  3147. kData->engine->setLastError("Could not find the LV2 Descriptor in the plugin library");
  3148. return false;
  3149. }
  3150. // -----------------------------------------------------------
  3151. // get descriptor that matches URI
  3152. uint32_t i = 0;
  3153. while ((fDescriptor = descFn(i++)))
  3154. {
  3155. if (std::strcmp(fDescriptor->URI, uri) == 0)
  3156. break;
  3157. }
  3158. }
  3159. if (fDescriptor == nullptr)
  3160. {
  3161. kData->engine->setLastError("Could not find the requested plugin URI in the plugin library");
  3162. return false;
  3163. }
  3164. // ---------------------------------------------------------------
  3165. // check supported port-types and features
  3166. bool canContinue = true;
  3167. // Check supported ports
  3168. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  3169. {
  3170. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  3171. if (! (LV2_IS_PORT_AUDIO(portTypes) || LV2_IS_PORT_CONTROL(portTypes) || LV2_IS_PORT_ATOM_SEQUENCE(portTypes) || LV2_IS_PORT_EVENT(portTypes) || LV2_IS_PORT_MIDI_LL(portTypes)))
  3172. {
  3173. if (! LV2_IS_PORT_OPTIONAL(fRdfDescriptor->Ports[i].Properties))
  3174. {
  3175. kData->engine->setLastError("Plugin requires a port type that is not currently supported");
  3176. canContinue = false;
  3177. break;
  3178. }
  3179. }
  3180. }
  3181. // Check supported features
  3182. for (uint32_t i=0; i < fRdfDescriptor->FeatureCount && canContinue; ++i)
  3183. {
  3184. if (LV2_IS_FEATURE_REQUIRED(fRdfDescriptor->Features[i].Type) && ! is_lv2_feature_supported(fRdfDescriptor->Features[i].URI))
  3185. {
  3186. QString msg(QString("Plugin requires a feature that is not supported:\n%1").arg(fRdfDescriptor->Features[i].URI));
  3187. kData->engine->setLastError(msg.toUtf8().constData());
  3188. canContinue = false;
  3189. break;
  3190. }
  3191. }
  3192. // Check extensions
  3193. for (uint32_t i=0; i < fRdfDescriptor->ExtensionCount; ++i)
  3194. {
  3195. if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_OPTIONS__interface) == 0)
  3196. kData->extraHints |= PLUGIN_HAS_EXTENSION_OPTIONS;
  3197. else if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_PROGRAMS__Interface) == 0)
  3198. kData->extraHints |= PLUGIN_HAS_EXTENSION_PROGRAMS;
  3199. else if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_STATE__interface) == 0)
  3200. kData->extraHints |= PLUGIN_HAS_EXTENSION_STATE;
  3201. else if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_WORKER__interface) == 0)
  3202. kData->extraHints |= PLUGIN_HAS_EXTENSION_WORKER;
  3203. else
  3204. carla_stdout("Plugin has non-supported extension: '%s'", fRdfDescriptor->Extensions[i]);
  3205. }
  3206. if (! canContinue)
  3207. {
  3208. // error already set
  3209. return false;
  3210. }
  3211. // ---------------------------------------------------------------
  3212. // get info
  3213. if (name != nullptr)
  3214. fName = kData->engine->getUniquePluginName(name);
  3215. else
  3216. fName = kData->engine->getUniquePluginName(fRdfDescriptor->Name);
  3217. // ---------------------------------------------------------------
  3218. // register client
  3219. kData->client = kData->engine->addClient(this);
  3220. if (kData->client == nullptr || ! kData->client->isOk())
  3221. {
  3222. kData->engine->setLastError("Failed to register plugin client");
  3223. return false;
  3224. }
  3225. // ---------------------------------------------------------------
  3226. // initialize plugin
  3227. fHandle = fDescriptor->instantiate(fDescriptor, kData->engine->getSampleRate(), fRdfDescriptor->Bundle, fFeatures);
  3228. if (fHandle == nullptr)
  3229. {
  3230. kData->engine->setLastError("Plugin failed to initialize");
  3231. return false;
  3232. }
  3233. // ---------------------------------------------------------------
  3234. // load plugin settings
  3235. {
  3236. // set default options
  3237. fOptions = 0x0;
  3238. fOptions |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  3239. if (needsFixedBuffer())
  3240. fOptions |= PLUGIN_OPTION_FIXED_BUFFER;
  3241. if (kData->engine->getOptions().forceStereo)
  3242. fOptions |= PLUGIN_OPTION_FORCE_STEREO;
  3243. if (midiInCount() > 0)
  3244. {
  3245. fOptions |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  3246. fOptions |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  3247. fOptions |= PLUGIN_OPTION_SEND_PITCHBEND;
  3248. fOptions |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  3249. }
  3250. // load settings
  3251. kData->idStr = "LV2/";
  3252. kData->idStr += uri;
  3253. fOptions = kData->loadSettings(fOptions, availableOptions());
  3254. // ignore settings, we need this anyway
  3255. if (needsFixedBuffer())
  3256. fOptions |= PLUGIN_OPTION_FIXED_BUFFER;
  3257. }
  3258. // ---------------------------------------------------------------
  3259. // gui stuff
  3260. if (fRdfDescriptor->UICount == 0)
  3261. return true;
  3262. // -----------------------------------------------------------
  3263. // find more appropriate ui
  3264. int eQt4, eQt5, eCocoa, eWindows, eX11, eGtk2, eGtk3, iCocoa, iWindows, iX11, iQt4, iQt5, iExt, iFinal;
  3265. eQt4 = eQt5 = eCocoa = eWindows = eX11 = eGtk2 = eGtk3 = iQt4 = iQt5 = iCocoa = iWindows = iX11 = iExt = iFinal = -1;
  3266. #ifdef BUILD_BRIDGE
  3267. const bool preferUiBridges(kData->engine->getOptions().preferUiBridges);
  3268. #else
  3269. const bool preferUiBridges(kData->engine->getOptions().preferUiBridges && (fHints & PLUGIN_IS_BRIDGE) == 0);
  3270. #endif
  3271. for (uint32_t i=0; i < fRdfDescriptor->UICount; ++i)
  3272. {
  3273. CARLA_ASSERT(fRdfDescriptor->UIs[i].URI != nullptr);
  3274. if (fRdfDescriptor->UIs[i].URI == nullptr)
  3275. {
  3276. carla_stderr("Plugin has an UI without a valid URI");
  3277. continue;
  3278. }
  3279. switch (fRdfDescriptor->UIs[i].Type)
  3280. {
  3281. #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
  3282. case LV2_UI_QT4:
  3283. if (isUiBridgeable(i))
  3284. eQt4 = i;
  3285. break;
  3286. #else
  3287. case LV2_UI_QT4:
  3288. if (isUiBridgeable(i) && preferUiBridges)
  3289. eQt4 = i;
  3290. iQt4 = i;
  3291. break;
  3292. #endif
  3293. #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
  3294. case LV2_UI_QT5:
  3295. if (isUiBridgeable(i) && preferUiBridges)
  3296. eQt5 = i;
  3297. iQt5 = i;
  3298. break;
  3299. #else
  3300. case LV2_UI_QT5:
  3301. if (isUiBridgeable(i) && preferUiBridges)
  3302. eQt5 = i;
  3303. break;
  3304. #endif
  3305. #ifdef CARLA_OS_MAC
  3306. case LV2_UI_COCOA:
  3307. if (isUiBridgeable(i) && preferUiBridges)
  3308. eCocoa = i;
  3309. iCocoa = i;
  3310. break;
  3311. #endif
  3312. #ifdef CARLA_OS_WIN
  3313. case LV2_UI_WINDOWS:
  3314. if (isUiBridgeable(i) && preferUiBridges)
  3315. eWindows = i;
  3316. iWindows = i;
  3317. break;
  3318. #endif
  3319. case LV2_UI_X11:
  3320. if (isUiBridgeable(i) && preferUiBridges)
  3321. eX11 = i;
  3322. #ifdef Q_WS_X11
  3323. iX11 = i;
  3324. #endif
  3325. break;
  3326. case LV2_UI_GTK2:
  3327. if (isUiBridgeable(i))
  3328. eGtk2 = i;
  3329. break;
  3330. case LV2_UI_GTK3:
  3331. if (isUiBridgeable(i))
  3332. eGtk3 = i;
  3333. break;
  3334. case LV2_UI_EXTERNAL:
  3335. case LV2_UI_OLD_EXTERNAL:
  3336. iExt = i;
  3337. break;
  3338. default:
  3339. break;
  3340. }
  3341. }
  3342. if (eQt4 >= 0)
  3343. iFinal = eQt4;
  3344. else if (eQt5 >= 0)
  3345. iFinal = eQt5;
  3346. else if (eCocoa >= 0)
  3347. iFinal = eCocoa;
  3348. else if (eWindows >= 0)
  3349. iFinal = eWindows;
  3350. else if (eX11 >= 0)
  3351. iFinal = eX11;
  3352. else if (iQt4 >= 0)
  3353. iFinal = iQt4;
  3354. else if (iQt5 >= 0)
  3355. iFinal = iQt5;
  3356. else if (iCocoa >= 0)
  3357. iFinal = iCocoa;
  3358. else if (iWindows >= 0)
  3359. iFinal = iWindows;
  3360. else if (iX11 >= 0)
  3361. iFinal = iX11;
  3362. else if (iExt >= 0)
  3363. iFinal = iExt;
  3364. else if (eGtk2 >= 0)
  3365. iFinal = eGtk2;
  3366. else if (eGtk3 >= 0)
  3367. iFinal = eGtk3;
  3368. if (iFinal < 0)
  3369. {
  3370. carla_stderr("Failed to find an appropriate LV2 UI for this plugin");
  3371. return true;
  3372. }
  3373. fUi.rdfDescriptor = &fRdfDescriptor->UIs[iFinal];
  3374. // -----------------------------------------------------------
  3375. // check supported ui features
  3376. canContinue = true;
  3377. for (uint32_t i=0; i < fUi.rdfDescriptor->FeatureCount; ++i)
  3378. {
  3379. if (LV2_IS_FEATURE_REQUIRED(fUi.rdfDescriptor->Features[i].Type) && ! is_lv2_ui_feature_supported(fUi.rdfDescriptor->Features[i].URI))
  3380. {
  3381. carla_stderr2("Plugin UI requires a feature that is not supported:\n%s", fUi.rdfDescriptor->Features[i].URI);
  3382. canContinue = false;
  3383. break;
  3384. }
  3385. }
  3386. if (! canContinue)
  3387. {
  3388. fUi.rdfDescriptor = nullptr;
  3389. return true;
  3390. }
  3391. // -----------------------------------------------------------
  3392. // initialize ui according to type
  3393. const LV2_Property uiType(fUi.rdfDescriptor->Type);
  3394. if (iFinal == eQt4 || iFinal == eQt5 || iFinal == eCocoa || iFinal == eWindows || iFinal == eX11 || iFinal == eGtk2 || iFinal == eGtk3)
  3395. {
  3396. // -------------------------------------------------------
  3397. // initialize ui bridge
  3398. if (const char* const oscBinary = getUiBridgePath(uiType))
  3399. {
  3400. fUi.type = PLUGIN_UI_OSC;
  3401. kData->osc.thread.setOscData(oscBinary, fDescriptor->URI, fUi.rdfDescriptor->URI);
  3402. }
  3403. }
  3404. else
  3405. {
  3406. // -------------------------------------------------------
  3407. // open UI DLL
  3408. if (! kData->uiLibOpen(fUi.rdfDescriptor->Binary))
  3409. {
  3410. carla_stderr2("Could not load UI library, error was:\n%s", kData->libError(fUi.rdfDescriptor->Binary));
  3411. fUi.rdfDescriptor = nullptr;
  3412. return true;
  3413. }
  3414. // -------------------------------------------------------
  3415. // get UI DLL main entry
  3416. LV2UI_DescriptorFunction uiDescFn = (LV2UI_DescriptorFunction)kData->uiLibSymbol("lv2ui_descriptor");
  3417. if (uiDescFn == nullptr)
  3418. {
  3419. carla_stderr2("Could not find the LV2UI Descriptor in the UI library");
  3420. kData->uiLibClose();
  3421. fUi.rdfDescriptor = nullptr;
  3422. return true;
  3423. }
  3424. // -------------------------------------------------------
  3425. // get UI descriptor that matches UI URI
  3426. uint32_t i = 0;
  3427. while ((fUi.descriptor = uiDescFn(i++)))
  3428. {
  3429. if (std::strcmp(fUi.descriptor->URI, fUi.rdfDescriptor->URI) == 0)
  3430. break;
  3431. }
  3432. if (fUi.descriptor == nullptr)
  3433. {
  3434. carla_stderr2("Could not find the requested GUI in the plugin UI library");
  3435. kData->uiLibClose();
  3436. fUi.rdfDescriptor = nullptr;
  3437. return true;
  3438. }
  3439. // -------------------------------------------------------
  3440. // check if ui is usable
  3441. switch (uiType)
  3442. {
  3443. #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
  3444. case LV2_UI_QT5:
  3445. carla_debug("Will use LV2 Qt5 UI");
  3446. fUi.type = PLUGIN_UI_QT;
  3447. break;
  3448. #else
  3449. case LV2_UI_QT4:
  3450. carla_debug("Will use LV2 Qt4 UI");
  3451. fUi.type = PLUGIN_UI_QT;
  3452. break;
  3453. #endif
  3454. #ifdef CARLA_OS_MAC
  3455. case LV2_UI_COCOA:
  3456. carla_debug("Will use LV2 Cocoa UI");
  3457. fUi.type = PLUGIN_UI_PARENT;
  3458. break;
  3459. #endif
  3460. #ifdef CARLA_OS_WIN
  3461. case LV2_UI_WINDOWS:
  3462. carla_debug("Will use LV2 Windows UI");
  3463. fUi.type = PLUGIN_UI_PARENT;
  3464. break;
  3465. #endif
  3466. #ifdef Q_WS_X11
  3467. case LV2_UI_X11:
  3468. carla_debug("Will use LV2 X11 UI");
  3469. fUi.type = PLUGIN_UI_PARENT;
  3470. break;
  3471. #endif
  3472. case LV2_UI_GTK2:
  3473. carla_debug("Will use LV2 Gtk2 UI, NOT!");
  3474. break;
  3475. case LV2_UI_GTK3:
  3476. carla_debug("Will use LV2 Gtk3 UI, NOT!");
  3477. break;
  3478. case LV2_UI_EXTERNAL:
  3479. case LV2_UI_OLD_EXTERNAL:
  3480. carla_debug("Will use LV2 External UI");
  3481. fUi.type = PLUGIN_UI_EXTERNAL;
  3482. break;
  3483. }
  3484. if (fUi.type == PLUGIN_UI_NULL)
  3485. {
  3486. kData->uiLibClose();
  3487. fUi.descriptor = nullptr;
  3488. fUi.rdfDescriptor = nullptr;
  3489. return true;
  3490. }
  3491. // -------------------------------------------------------
  3492. // initialize ui features
  3493. QString guiTitle(QString("%1 (GUI)").arg((const char*)fName));
  3494. LV2_Extension_Data_Feature* const uiDataFt = new LV2_Extension_Data_Feature;
  3495. uiDataFt->data_access = fDescriptor->extension_data;
  3496. LV2UI_Port_Map* const uiPortMapFt = new LV2UI_Port_Map;
  3497. uiPortMapFt->handle = this;
  3498. uiPortMapFt->port_index = carla_lv2_ui_port_map;
  3499. LV2UI_Resize* const uiResizeFt = new LV2UI_Resize;
  3500. uiResizeFt->handle = this;
  3501. uiResizeFt->ui_resize = carla_lv2_ui_resize;
  3502. LV2_External_UI_Host* const uiExternalHostFt = new LV2_External_UI_Host;
  3503. uiExternalHostFt->ui_closed = carla_lv2_external_ui_closed;
  3504. uiExternalHostFt->plugin_human_id = carla_strdup(guiTitle.toUtf8().constData());
  3505. fFeatures[kFeatureIdUiDataAccess] = new LV2_Feature;
  3506. fFeatures[kFeatureIdUiDataAccess]->URI = LV2_DATA_ACCESS_URI;
  3507. fFeatures[kFeatureIdUiDataAccess]->data = uiDataFt;
  3508. fFeatures[kFeatureIdUiInstanceAccess] = new LV2_Feature;
  3509. fFeatures[kFeatureIdUiInstanceAccess]->URI = LV2_INSTANCE_ACCESS_URI;
  3510. fFeatures[kFeatureIdUiInstanceAccess]->data = fHandle;
  3511. fFeatures[kFeatureIdUiParent] = new LV2_Feature;
  3512. fFeatures[kFeatureIdUiParent]->URI = LV2_UI__parent;
  3513. fFeatures[kFeatureIdUiParent]->data = nullptr;
  3514. fFeatures[kFeatureIdUiPortMap] = new LV2_Feature;
  3515. fFeatures[kFeatureIdUiPortMap]->URI = LV2_UI__portMap;
  3516. fFeatures[kFeatureIdUiPortMap]->data = uiPortMapFt;
  3517. fFeatures[kFeatureIdUiResize] = new LV2_Feature;
  3518. fFeatures[kFeatureIdUiResize]->URI = LV2_UI__resize;
  3519. fFeatures[kFeatureIdUiResize]->data = uiResizeFt;
  3520. fFeatures[kFeatureIdExternalUi] = new LV2_Feature;
  3521. fFeatures[kFeatureIdExternalUi]->URI = LV2_EXTERNAL_UI__Host;
  3522. fFeatures[kFeatureIdExternalUi]->data = uiExternalHostFt;
  3523. fFeatures[kFeatureIdExternalUiOld] = new LV2_Feature;
  3524. fFeatures[kFeatureIdExternalUiOld]->URI = LV2_EXTERNAL_UI_DEPRECATED_URI;
  3525. fFeatures[kFeatureIdExternalUiOld]->data = uiExternalHostFt;
  3526. }
  3527. return true;
  3528. }
  3529. // -------------------------------------------------------------------
  3530. void handleTransferAtom(const int32_t portIndex, const char* const typeStr, LV2_Atom* const atom)
  3531. {
  3532. CARLA_ASSERT(portIndex >= 0);
  3533. CARLA_ASSERT(atom != nullptr);
  3534. CARLA_ASSERT(typeStr != nullptr);
  3535. carla_debug("Lv2Plugin::handleTransferAtom(%i, %s, %p)", portIndex, typeStr, atom);
  3536. atom->type = carla_lv2_urid_map(this, typeStr);
  3537. fAtomQueueIn.put(portIndex, atom);
  3538. }
  3539. void handleTransferEvent(const int32_t portIndex, const char* const typeStr, LV2_Atom* const atom)
  3540. {
  3541. CARLA_ASSERT(portIndex >= 0);
  3542. CARLA_ASSERT(atom != nullptr);
  3543. CARLA_ASSERT(typeStr != nullptr);
  3544. carla_debug("Lv2Plugin::handleTransferEvent(%i, %s, %p)", portIndex, typeStr, atom);
  3545. atom->type = carla_lv2_urid_map(this, typeStr);
  3546. fAtomQueueIn.put(portIndex, atom);
  3547. }
  3548. // -------------------------------------------------------------------
  3549. private:
  3550. LV2_Handle fHandle;
  3551. LV2_Handle fHandle2;
  3552. LV2_Feature* fFeatures[kFeatureCount+1];
  3553. const LV2_Descriptor* fDescriptor;
  3554. const LV2_RDF_Descriptor* fRdfDescriptor;
  3555. float** fAudioInBuffers;
  3556. float** fAudioOutBuffers;
  3557. float* fParamBuffers;
  3558. Lv2AtomQueue fAtomQueueIn;
  3559. Lv2AtomQueue fAtomQueueOut;
  3560. Lv2PluginEventData fEventsIn;
  3561. Lv2PluginEventData fEventsOut;
  3562. Lv2PluginOptions fLv2Options;
  3563. NonRtList<const char*> fCustomURIDs;
  3564. struct Extensions {
  3565. const LV2_Options_Interface* options;
  3566. const LV2_State_Interface* state;
  3567. const LV2_Worker_Interface* worker;
  3568. const LV2_Programs_Interface* programs;
  3569. const LV2UI_Idle_Interface* uiidle;
  3570. const LV2_Programs_UI_Interface* uiprograms;
  3571. Extensions()
  3572. : options(nullptr),
  3573. state(nullptr),
  3574. worker(nullptr),
  3575. programs(nullptr),
  3576. uiidle(nullptr),
  3577. uiprograms(nullptr) {}
  3578. } fExt;
  3579. struct UI {
  3580. Lv2PluginGuiType type;
  3581. LV2UI_Handle handle;
  3582. LV2UI_Widget widget;
  3583. const LV2UI_Descriptor* descriptor;
  3584. const LV2_RDF_UI* rdfDescriptor;
  3585. UI()
  3586. : type(PLUGIN_UI_NULL),
  3587. handle(nullptr),
  3588. widget(nullptr),
  3589. descriptor(nullptr),
  3590. rdfDescriptor(nullptr) {}
  3591. ~UI()
  3592. {
  3593. CARLA_ASSERT(handle == nullptr);
  3594. CARLA_ASSERT(widget == nullptr);
  3595. CARLA_ASSERT(descriptor == nullptr);
  3596. CARLA_ASSERT(rdfDescriptor == nullptr);
  3597. }
  3598. } fUi;
  3599. // -------------------------------------------------------------------
  3600. // Event Feature
  3601. static uint32_t carla_lv2_event_ref(LV2_Event_Callback_Data callback_data, LV2_Event* event)
  3602. {
  3603. carla_debug("carla_lv2_event_ref(%p, %p)", callback_data, event);
  3604. CARLA_ASSERT(callback_data != nullptr);
  3605. CARLA_ASSERT(event != nullptr);
  3606. return 0;
  3607. }
  3608. static uint32_t carla_lv2_event_unref(LV2_Event_Callback_Data callback_data, LV2_Event* event)
  3609. {
  3610. carla_debug("carla_lv2_event_unref(%p, %p)", callback_data, event);
  3611. CARLA_ASSERT(callback_data != nullptr);
  3612. CARLA_ASSERT(event != nullptr);
  3613. return 0;
  3614. }
  3615. // -------------------------------------------------------------------
  3616. // Logs Feature
  3617. static int carla_lv2_log_printf(LV2_Log_Handle handle, LV2_URID type, const char* fmt, ...)
  3618. {
  3619. CARLA_ASSERT(handle != nullptr);
  3620. CARLA_ASSERT(type > CARLA_URI_MAP_ID_NULL);
  3621. #ifndef DEBUG
  3622. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  3623. return 0;
  3624. #endif
  3625. va_list args;
  3626. va_start(args, fmt);
  3627. const int ret = carla_lv2_log_vprintf(handle, type, fmt, args);
  3628. va_end(args);
  3629. return ret;
  3630. }
  3631. static int carla_lv2_log_vprintf(LV2_Log_Handle handle, LV2_URID type, const char* fmt, va_list ap)
  3632. {
  3633. CARLA_ASSERT(handle != nullptr);
  3634. CARLA_ASSERT(type > CARLA_URI_MAP_ID_NULL);
  3635. #ifndef DEBUG
  3636. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  3637. return 0;
  3638. #endif
  3639. int ret = 0;
  3640. switch (type)
  3641. {
  3642. case CARLA_URI_MAP_ID_LOG_ERROR:
  3643. #ifndef CARLA_OS_WIN
  3644. std::fprintf(stderr, "\x1b[31m");
  3645. #endif
  3646. ret = std::vfprintf(stderr, fmt, ap);
  3647. #ifndef CARLA_OS_WIN
  3648. std::fprintf(stderr, "\x1b[0m");
  3649. #endif
  3650. break;
  3651. case CARLA_URI_MAP_ID_LOG_NOTE:
  3652. ret = std::vfprintf(stdout, fmt, ap);
  3653. break;
  3654. case CARLA_URI_MAP_ID_LOG_TRACE:
  3655. #ifdef DEBUG
  3656. # ifndef CARLA_OS_WIN
  3657. std::fprintf(stdout, "\x1b[30;1m");
  3658. # endif
  3659. ret = std::vfprintf(stdout, fmt, ap);
  3660. # ifndef CARLA_OS_WIN
  3661. std::fprintf(stdout, "\x1b[0m");
  3662. # endif
  3663. #endif
  3664. break;
  3665. case CARLA_URI_MAP_ID_LOG_WARNING:
  3666. ret = std::vfprintf(stderr, fmt, ap);
  3667. break;
  3668. default:
  3669. break;
  3670. }
  3671. return ret;
  3672. }
  3673. // -------------------------------------------------------------------
  3674. // Programs Feature
  3675. static void carla_lv2_program_changed(LV2_Programs_Handle handle, int32_t index)
  3676. {
  3677. carla_debug("carla_lv2_program_changed(%p, %i)", handle, index);
  3678. CARLA_ASSERT(handle != nullptr);
  3679. if (handle == nullptr)
  3680. return;
  3681. ((Lv2Plugin*)handle)->handleProgramChanged(index);
  3682. }
  3683. // -------------------------------------------------------------------
  3684. // State Feature
  3685. static char* carla_lv2_state_make_path(LV2_State_Make_Path_Handle handle, const char* path)
  3686. {
  3687. carla_debug("carla_lv2_state_make_path(%p, \"%s\")", handle, path);
  3688. CARLA_ASSERT(handle != nullptr);
  3689. CARLA_ASSERT(path != nullptr);
  3690. if (path == nullptr)
  3691. return nullptr;
  3692. QDir dir;
  3693. dir.mkpath(path);
  3694. return strdup(path);
  3695. }
  3696. static char* carla_lv2_state_map_abstract_path(LV2_State_Map_Path_Handle handle, const char* absolute_path)
  3697. {
  3698. carla_debug("carla_lv2_state_map_abstract_path(%p, \"%s\")", handle, absolute_path);
  3699. CARLA_ASSERT(handle != nullptr);
  3700. CARLA_ASSERT(absolute_path != nullptr);
  3701. if (absolute_path == nullptr)
  3702. return nullptr;
  3703. QDir dir(absolute_path);
  3704. return strdup(dir.canonicalPath().toUtf8().constData());
  3705. }
  3706. static char* carla_lv2_state_map_absolute_path(LV2_State_Map_Path_Handle handle, const char* abstract_path)
  3707. {
  3708. carla_debug("carla_lv2_state_map_absolute_path(%p, \"%s\")", handle, abstract_path);
  3709. CARLA_ASSERT(handle != nullptr);
  3710. CARLA_ASSERT(abstract_path != nullptr);
  3711. if (abstract_path == nullptr)
  3712. return nullptr;
  3713. QDir dir(abstract_path);
  3714. return strdup(dir.absolutePath().toUtf8().constData());
  3715. }
  3716. static LV2_State_Status carla_lv2_state_store(LV2_State_Handle handle, uint32_t key, const void* value, size_t size, uint32_t type, uint32_t flags)
  3717. {
  3718. carla_debug("carla_lv2_state_store(%p, %i, %p, " P_SIZE ", %i, %i)", handle, key, value, size, type, flags);
  3719. CARLA_ASSERT(handle != nullptr);
  3720. if (handle == nullptr)
  3721. return LV2_STATE_ERR_UNKNOWN;
  3722. return ((Lv2Plugin*)handle)->handleStateStore(key, value, size, type, flags);
  3723. }
  3724. static const void* carla_lv2_state_retrieve(LV2_State_Handle handle, uint32_t key, size_t* size, uint32_t* type, uint32_t* flags)
  3725. {
  3726. carla_debug("carla_lv2_state_retrieve(%p, %i, %p, %p, %p)", handle, key, size, type, flags);
  3727. CARLA_ASSERT(handle != nullptr);
  3728. if (handle == nullptr)
  3729. return nullptr;
  3730. return ((Lv2Plugin*)handle)->handleStateRetrieve(key, size, type, flags);
  3731. }
  3732. // -------------------------------------------------------------------
  3733. // URI-Map Feature
  3734. static uint32_t carla_lv2_uri_to_id(LV2_URI_Map_Callback_Data data, const char* map, const char* uri)
  3735. {
  3736. carla_debug("carla_lv2_uri_to_id(%p, \"%s\", \"%s\")", data, map, uri);
  3737. return carla_lv2_urid_map((LV2_URID_Map_Handle*)data, uri);
  3738. // unused
  3739. (void)map;
  3740. }
  3741. // -------------------------------------------------------------------
  3742. // URID Feature
  3743. static LV2_URID carla_lv2_urid_map(LV2_URID_Map_Handle handle, const char* uri)
  3744. {
  3745. CARLA_ASSERT(handle != nullptr);
  3746. CARLA_ASSERT(uri != nullptr);
  3747. carla_debug("carla_lv2_urid_map(%p, \"%s\")", handle, uri);
  3748. if (uri == nullptr)
  3749. return CARLA_URI_MAP_ID_NULL;
  3750. // Atom types
  3751. if (std::strcmp(uri, LV2_ATOM__Chunk) == 0)
  3752. return CARLA_URI_MAP_ID_ATOM_CHUNK;
  3753. if (std::strcmp(uri, LV2_ATOM__Double) == 0)
  3754. return CARLA_URI_MAP_ID_ATOM_DOUBLE;
  3755. if (std::strcmp(uri, LV2_ATOM__Int) == 0)
  3756. return CARLA_URI_MAP_ID_ATOM_INT;
  3757. if (std::strcmp(uri, LV2_ATOM__Path) == 0)
  3758. return CARLA_URI_MAP_ID_ATOM_PATH;
  3759. if (std::strcmp(uri, LV2_ATOM__Sequence) == 0)
  3760. return CARLA_URI_MAP_ID_ATOM_SEQUENCE;
  3761. if (std::strcmp(uri, LV2_ATOM__String) == 0)
  3762. return CARLA_URI_MAP_ID_ATOM_STRING;
  3763. if (std::strcmp(uri, LV2_ATOM__atomTransfer) == 0)
  3764. return CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM;
  3765. if (std::strcmp(uri, LV2_ATOM__eventTransfer) == 0)
  3766. return CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT;
  3767. // BufSize types
  3768. if (std::strcmp(uri, LV2_BUF_SIZE__maxBlockLength) == 0)
  3769. return CARLA_URI_MAP_ID_BUF_MAX_LENGTH;
  3770. if (std::strcmp(uri, LV2_BUF_SIZE__minBlockLength) == 0)
  3771. return CARLA_URI_MAP_ID_BUF_MIN_LENGTH;
  3772. if (std::strcmp(uri, LV2_BUF_SIZE__sequenceSize) == 0)
  3773. return CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE;
  3774. // Log types
  3775. if (std::strcmp(uri, LV2_LOG__Error) == 0)
  3776. return CARLA_URI_MAP_ID_LOG_ERROR;
  3777. if (std::strcmp(uri, LV2_LOG__Note) == 0)
  3778. return CARLA_URI_MAP_ID_LOG_NOTE;
  3779. if (std::strcmp(uri, LV2_LOG__Trace) == 0)
  3780. return CARLA_URI_MAP_ID_LOG_TRACE;
  3781. if (std::strcmp(uri, LV2_LOG__Warning) == 0)
  3782. return CARLA_URI_MAP_ID_LOG_WARNING;
  3783. // Others
  3784. if (std::strcmp(uri, LV2_MIDI__MidiEvent) == 0)
  3785. return CARLA_URI_MAP_ID_MIDI_EVENT;
  3786. if (std::strcmp(uri, LV2_PARAMETERS__sampleRate) == 0)
  3787. return CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE;
  3788. if (std::strcmp(uri, LV2_TIME__Position) == 0)
  3789. return CARLA_URI_MAP_ID_TIME_POSITION;
  3790. if (handle == nullptr)
  3791. return CARLA_URI_MAP_ID_NULL;
  3792. // Custom types
  3793. return ((Lv2Plugin*)handle)->getCustomURID(uri);
  3794. }
  3795. static const char* carla_lv2_urid_unmap(LV2_URID_Map_Handle handle, LV2_URID urid)
  3796. {
  3797. carla_debug("carla_lv2_urid_unmap(%p, %i)", handle, urid);
  3798. CARLA_ASSERT(handle != nullptr);
  3799. CARLA_ASSERT(urid > CARLA_URI_MAP_ID_NULL);
  3800. if (urid == CARLA_URI_MAP_ID_NULL)
  3801. return nullptr;
  3802. // Atom types
  3803. if (urid == CARLA_URI_MAP_ID_ATOM_CHUNK)
  3804. return LV2_ATOM__Chunk;
  3805. if (urid == CARLA_URI_MAP_ID_ATOM_DOUBLE)
  3806. return LV2_ATOM__Double;
  3807. if (urid == CARLA_URI_MAP_ID_ATOM_INT)
  3808. return LV2_ATOM__Int;
  3809. if (urid == CARLA_URI_MAP_ID_ATOM_PATH)
  3810. return LV2_ATOM__Path;
  3811. if (urid == CARLA_URI_MAP_ID_ATOM_SEQUENCE)
  3812. return LV2_ATOM__Sequence;
  3813. if (urid == CARLA_URI_MAP_ID_ATOM_STRING)
  3814. return LV2_ATOM__String;
  3815. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM)
  3816. return LV2_ATOM__atomTransfer;
  3817. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT)
  3818. return LV2_ATOM__eventTransfer;
  3819. // BufSize types
  3820. if (urid == CARLA_URI_MAP_ID_BUF_MAX_LENGTH)
  3821. return LV2_BUF_SIZE__maxBlockLength;
  3822. if (urid == CARLA_URI_MAP_ID_BUF_MIN_LENGTH)
  3823. return LV2_BUF_SIZE__minBlockLength;
  3824. if (urid == CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE)
  3825. return LV2_BUF_SIZE__sequenceSize;
  3826. // Log types
  3827. if (urid == CARLA_URI_MAP_ID_LOG_ERROR)
  3828. return LV2_LOG__Error;
  3829. if (urid == CARLA_URI_MAP_ID_LOG_NOTE)
  3830. return LV2_LOG__Note;
  3831. if (urid == CARLA_URI_MAP_ID_LOG_TRACE)
  3832. return LV2_LOG__Trace;
  3833. if (urid == CARLA_URI_MAP_ID_LOG_WARNING)
  3834. return LV2_LOG__Warning;
  3835. // Others
  3836. if (urid == CARLA_URI_MAP_ID_MIDI_EVENT)
  3837. return LV2_MIDI__MidiEvent;
  3838. if (urid == CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE)
  3839. return LV2_PARAMETERS__sampleRate;
  3840. if (urid == CARLA_URI_MAP_ID_TIME_POSITION)
  3841. return LV2_TIME__Position;
  3842. if (handle == nullptr)
  3843. return nullptr;
  3844. // Custom types
  3845. return ((Lv2Plugin*)handle)->getCustomURIString(urid);
  3846. }
  3847. // -------------------------------------------------------------------
  3848. // Worker Feature
  3849. static LV2_Worker_Status carla_lv2_worker_schedule(LV2_Worker_Schedule_Handle handle, uint32_t size, const void* data)
  3850. {
  3851. CARLA_ASSERT(handle != nullptr);
  3852. carla_debug("carla_lv2_worker_schedule(%p, %i, %p)", handle, size, data);
  3853. if (handle == nullptr)
  3854. return LV2_WORKER_ERR_UNKNOWN;
  3855. return ((Lv2Plugin*)handle)->handleWorkerSchedule(size, data);
  3856. }
  3857. static LV2_Worker_Status carla_lv2_worker_respond(LV2_Worker_Respond_Handle handle, uint32_t size, const void* data)
  3858. {
  3859. CARLA_ASSERT(handle != nullptr);
  3860. carla_debug("carla_lv2_worker_respond(%p, %i, %p)", handle, size, data);
  3861. if (handle == nullptr)
  3862. return LV2_WORKER_ERR_UNKNOWN;
  3863. return ((Lv2Plugin*)handle)->handleWorkerRespond(size, data);
  3864. }
  3865. // -------------------------------------------------------------------
  3866. // UI Port-Map Feature
  3867. static uint32_t carla_lv2_ui_port_map(LV2UI_Feature_Handle handle, const char* symbol)
  3868. {
  3869. carla_debug("carla_lv2_ui_port_map(%p, \"%s\")", handle, symbol);
  3870. CARLA_ASSERT(handle);
  3871. if (handle == nullptr)
  3872. return LV2UI_INVALID_PORT_INDEX;
  3873. return ((Lv2Plugin*)handle)->handleUiPortMap(symbol);
  3874. }
  3875. // -------------------------------------------------------------------
  3876. // UI Resize Feature
  3877. static int carla_lv2_ui_resize(LV2UI_Feature_Handle handle, int width, int height)
  3878. {
  3879. carla_debug("carla_lv2_ui_resize(%p, %i, %i)", handle, width, height);
  3880. CARLA_ASSERT(handle != nullptr);
  3881. if (handle == nullptr)
  3882. return 1;
  3883. return ((Lv2Plugin*)handle)->handleUiResize(width, height);
  3884. }
  3885. // -------------------------------------------------------------------
  3886. // External UI Feature
  3887. static void carla_lv2_external_ui_closed(LV2UI_Controller controller)
  3888. {
  3889. carla_debug("carla_lv2_external_ui_closed(%p)", controller);
  3890. CARLA_ASSERT(controller != nullptr);
  3891. if (controller == nullptr)
  3892. return;
  3893. ((Lv2Plugin*)controller)->handleExternalUiClosed();
  3894. }
  3895. // -------------------------------------------------------------------
  3896. // UI Extension
  3897. static void carla_lv2_ui_write_function(LV2UI_Controller controller, uint32_t port_index, uint32_t buffer_size, uint32_t format, const void* buffer)
  3898. {
  3899. carla_debug("carla_lv2_ui_write_function(%p, %i, %i, %i, %p)", controller, port_index, buffer_size, format, buffer);
  3900. CARLA_ASSERT(controller != nullptr);
  3901. if (controller == nullptr)
  3902. return;
  3903. ((Lv2Plugin*)controller)->handleUiWrite(port_index, buffer_size, format, buffer);
  3904. }
  3905. // -------------------------------------------------------------------
  3906. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Lv2Plugin)
  3907. };
  3908. // -------------------------------------------------------------------------------------------------------------------
  3909. int CarlaEngineOsc::handleMsgLv2AtomTransfer(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  3910. {
  3911. carla_debug("CarlaOsc::handleMsgLv2AtomTransfer()");
  3912. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(3, "iss");
  3913. const int32_t portIndex = argv[0]->i;
  3914. const char* const typeStr = (const char*)&argv[1]->s;
  3915. const char* const atomBuf = (const char*)&argv[2]->s;
  3916. QByteArray chunk;
  3917. chunk = QByteArray::fromBase64(atomBuf);
  3918. LV2_Atom* const atom = (LV2_Atom*)chunk.data();
  3919. ((Lv2Plugin*)plugin)->handleTransferAtom(portIndex, typeStr, atom);
  3920. return 0;
  3921. }
  3922. int CarlaEngineOsc::handleMsgLv2EventTransfer(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  3923. {
  3924. carla_debug("CarlaOsc::handleMsgLv2EventTransfer()");
  3925. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(3, "iss");
  3926. const int32_t portIndex = argv[0]->i;
  3927. const char* const typeStr = (const char*)&argv[1]->s;
  3928. const char* const atomBuf = (const char*)&argv[2]->s;
  3929. QByteArray chunk;
  3930. chunk = QByteArray::fromBase64(atomBuf);
  3931. LV2_Atom* const atom = (LV2_Atom*)chunk.data();
  3932. ((Lv2Plugin*)plugin)->handleTransferEvent(portIndex, typeStr, atom);
  3933. return 0;
  3934. }
  3935. CARLA_BACKEND_END_NAMESPACE
  3936. #else // WANT_VST
  3937. # warning Building without LV2 support
  3938. #endif
  3939. CARLA_BACKEND_START_NAMESPACE
  3940. CarlaPlugin* CarlaPlugin::newLV2(const Initializer& init)
  3941. {
  3942. carla_debug("CarlaPlugin::newLV2({%p, \"%s\", \"%s\"})", init.engine, init.name, init.label);
  3943. #ifdef WANT_LV2
  3944. Lv2Plugin* const plugin(new Lv2Plugin(init.engine, init.id));
  3945. if (! plugin->init(init.name, init.label))
  3946. {
  3947. delete plugin;
  3948. return nullptr;
  3949. }
  3950. plugin->reload();
  3951. if (init.engine->getProccessMode() == PROCESS_MODE_CONTINUOUS_RACK && ! CarlaPluginProtectedData::canRunInRack(plugin))
  3952. {
  3953. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo LV2 plugins, sorry!");
  3954. delete plugin;
  3955. return nullptr;
  3956. }
  3957. return plugin;
  3958. #else
  3959. init.engine->setLastError("LV2 support not available");
  3960. return nullptr;
  3961. #endif
  3962. }
  3963. CARLA_BACKEND_END_NAMESPACE