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

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