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 206KB

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