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.

CarlaPluginLV2.cpp 236KB

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