tests.cpp 465 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459
  1. // Core tests (no JUCE). Usage: lynx_tests [folder-with-flp-files]
  2. #include <atomic>
  3. #include <cctype>
  4. #include <cmath>
  5. #include <cstdio>
  6. #include <cstring>
  7. #include <filesystem>
  8. #include <map>
  9. #include <set>
  10. #include <sstream>
  11. #include <fstream>
  12. #include <functional>
  13. #include <iterator>
  14. #include <string>
  15. #include <tuple>
  16. #include <vector>
  17. #include "core/Dither.h"
  18. #include "core/DrumSynthLive.h"
  19. #include "core/Effects.h"
  20. #include "core/Effector.h"
  21. #include "core/FlpImport.h"
  22. #include "core/FlEffects.h"
  23. #include "core/FlPatcher.h"
  24. #include "core/FlSamplers.h"
  25. #include "core/FlSynths.h"
  26. #include "core/Inflate.h"
  27. #include "core/WavPack.h"
  28. #include "core/FlWrapper.h"
  29. #include "core/Gms.h"
  30. #include "core/PoiZone.h"
  31. #include "core/MiniSynth.h"
  32. #include "core/Toxic.h"
  33. #include "core/Harmless.h"
  34. #include "core/Harmor.h"
  35. #include "core/Morphine.h"
  36. #include "core/Ogun.h"
  37. #include "core/BassDrum.h"
  38. #include "core/FruitKick.h"
  39. #include "core/Dx10.h"
  40. #include "core/Kepler.h"
  41. #include "core/HarmlessFx.h"
  42. #include "core/MaximusFx.h"
  43. #include "core/Reeverb2.h"
  44. #include "core/Denormals.h"
  45. #include "core/FruityDynamics.h"
  46. #include "core/FruityShapers.h"
  47. #include "core/ChorusFx.h"
  48. #include "core/LiveEventQueue.h"
  49. #include "core/RackRenderer.h"
  50. #include "core/Scheduler.h"
  51. #include "core/SnapshotExchange.h"
  52. #include "core/SoftClip.h"
  53. #include "core/Slicer.h"
  54. #include "core/SytrusEngine.h"
  55. #include "core/Templates.h"
  56. #include "core/Vocodex.h"
  57. #include "core/TimeStretch.h"
  58. #include "core/WorkerPool.h"
  59. #include "core/Song.h"
  60. #include "core/NoteEdit.h"
  61. #include "core/Recorder.h"
  62. #include "core/KeyMap.h"
  63. #include "core/RackOps.h"
  64. #include "core/ValueMath.h"
  65. #include "core/Workspace.h"
  66. #include "core/StartupSound.h"
  67. #include "flp/Flp.h"
  68. #include "ui/I18nData.h"
  69. namespace
  70. {
  71. int failures = 0;
  72. int checks = 0;
  73. std::vector<std::pair<const char*, std::function<void()>>>& registry()
  74. {
  75. static std::vector<std::pair<const char*, std::function<void()>>> r;
  76. return r;
  77. }
  78. struct Register
  79. {
  80. Register (const char* name, std::function<void()> fn) { registry().emplace_back (name, std::move (fn)); }
  81. };
  82. #define TEST(name) \
  83. static void name(); \
  84. static Register reg_##name (#name, name); \
  85. static void name()
  86. #define CHECK(cond) \
  87. do \
  88. { \
  89. ++checks; \
  90. if (! (cond)) \
  91. { \
  92. ++failures; \
  93. std::printf (" FAILED %s:%d: %s\n", __FILE__, __LINE__, #cond); \
  94. } \
  95. } while (false)
  96. std::string realFlpDir;
  97. // --- helpers for building synthetic .flp files -----------------------------
  98. struct FlpBuilder
  99. {
  100. std::vector<std::uint8_t> bytes;
  101. FlpBuilder (std::uint16_t ppq = 96)
  102. {
  103. put ("FLhd");
  104. u32 (6);
  105. u16 (0);
  106. u16 (1);
  107. u16 (ppq);
  108. put ("FLdt");
  109. u32 (0); // size is not used by the reader
  110. }
  111. void put (const char* s) { bytes.insert (bytes.end(), s, s + std::strlen (s)); }
  112. void u8 (std::uint8_t v) { bytes.push_back (v); }
  113. void u16 (std::uint16_t v) { u8 ((std::uint8_t) v); u8 ((std::uint8_t) (v >> 8)); }
  114. void u32 (std::uint32_t v) { u16 ((std::uint16_t) v); u16 ((std::uint16_t) (v >> 16)); }
  115. void varint (std::size_t n)
  116. {
  117. do
  118. {
  119. auto b = (std::uint8_t) (n & 0x7F);
  120. n >>= 7;
  121. u8 (n ? (std::uint8_t) (b | 0x80) : b);
  122. } while (n);
  123. }
  124. void data (std::uint8_t id, const std::vector<std::uint8_t>& payload)
  125. {
  126. u8 (id);
  127. varint (payload.size());
  128. bytes.insert (bytes.end(), payload.begin(), payload.end());
  129. }
  130. void ascii (std::uint8_t id, const std::string& s)
  131. {
  132. std::vector<std::uint8_t> p (s.begin(), s.end());
  133. p.push_back (0);
  134. data (id, p);
  135. }
  136. void utf16 (std::uint8_t id, const std::u16string& s)
  137. {
  138. std::vector<std::uint8_t> p;
  139. for (auto c : s)
  140. {
  141. p.push_back ((std::uint8_t) c);
  142. p.push_back ((std::uint8_t) (c >> 8));
  143. }
  144. p.push_back (0);
  145. p.push_back (0);
  146. data (id, p);
  147. }
  148. };
  149. std::vector<std::uint8_t> noteRecord (std::uint32_t pos, std::uint16_t channel, std::uint32_t len, std::uint16_t key,
  150. std::uint8_t velocity)
  151. {
  152. std::vector<std::uint8_t> r (24, 0);
  153. std::memcpy (&r[0], &pos, 4);
  154. std::memcpy (&r[6], &channel, 2);
  155. std::memcpy (&r[8], &len, 4);
  156. std::memcpy (&r[12], &key, 2);
  157. r[16] = 120;
  158. r[20] = 64;
  159. r[21] = velocity;
  160. return r;
  161. }
  162. } // namespace
  163. // ---------------------------------------------------------------------------
  164. // FLP
  165. TEST (flp_rejects_garbage)
  166. {
  167. std::vector<std::uint8_t> junk (64, 0x41);
  168. bool threw = false;
  169. try { flp::EventReader r (junk); } catch (const flp::Error&) { threw = true; }
  170. CHECK (threw);
  171. }
  172. TEST (flp_reads_size_classes_varint_and_extended_ids)
  173. {
  174. FlpBuilder b;
  175. b.u8 (0x15); b.u8 (2); // u8
  176. b.u8 (0x40); b.u16 (513); // u16
  177. b.u8 (0x9C); b.u32 (128500); // u32
  178. b.data (0xD5, std::vector<std::uint8_t> (300, 7)); // 2-byte varint
  179. b.u8 (0xAC); b.u32 (0xC0000100); b.varint (3); b.u8 (1); b.u8 (2); b.u8 (3); // extended ID
  180. flp::EventReader r (b.bytes);
  181. flp::Event e;
  182. CHECK (r.header().ppq == 96);
  183. CHECK (r.next (e) && e.id == 0x15 && e.value == 2);
  184. CHECK (r.next (e) && e.id == 0x40 && e.value == 513);
  185. CHECK (r.next (e) && e.id == 0x9C && e.value == 128500);
  186. CHECK (r.next (e) && e.id == 0xD5 && e.data.size() == 300 && e.data[299] == 7);
  187. CHECK (r.next (e) && e.id == 0xC0000100 && e.isData() && e.data.size() == 3 && e.data[2] == 3);
  188. CHECK (! r.next (e));
  189. }
  190. TEST (flp_truncated_file_throws)
  191. {
  192. FlpBuilder b;
  193. b.u8 (0x9C); b.u8 (1); // u32 with only 1 byte
  194. flp::EventReader r (b.bytes);
  195. flp::Event e;
  196. bool threw = false;
  197. try { r.next (e); } catch (const flp::Error&) { threw = true; }
  198. CHECK (threw);
  199. }
  200. TEST (flp_version_numbers)
  201. {
  202. CHECK (flp::parseVersionNumber ("25.2.5.5319") == 25002005u);
  203. CHECK (flp::parseVersionNumber ("20.99.3000.3031") == 20102000u);
  204. CHECK (flp::parseVersionNumber ("11.0.3") == 11000003u);
  205. }
  206. TEST (flp_playlist_record_size_by_version)
  207. {
  208. CHECK (flp::playlistRecordSize (12009003, 64) == 32);
  209. CHECK (flp::playlistRecordSize (21000000, 120) == 60);
  210. CHECK (flp::playlistRecordSize (25002005, 160) == 80);
  211. CHECK (flp::playlistRecordSize (25002005, 60) == 60); // size doesn't match the version -> tries the others
  212. CHECK (flp::playlistRecordSize (25002005, 7) == 0);
  213. }
  214. TEST (flp_parses_project_model)
  215. {
  216. FlpBuilder b (96);
  217. b.ascii (0xC7, "25.2.5.5319");
  218. b.u8 (0x9C); b.u32 (174500);
  219. b.utf16 (0xC2, u"Título");
  220. b.u8 (0x41); b.u16 (3);
  221. b.u8 (0x96); b.u32 (0x00A14547); // BGR in the file
  222. auto notes = noteRecord (96, 1, 48, 64, 100);
  223. auto n2 = noteRecord (0, 0, 0, 60, 128);
  224. notes.insert (notes.end(), n2.begin(), n2.end());
  225. b.data (0xE0, notes);
  226. b.u8 (0x40); b.u16 (0);
  227. b.u8 (0x15); b.u8 (0);
  228. b.utf16 (0xCB, u"Kick");
  229. {
  230. std::vector<std::uint8_t> levels (24, 0);
  231. const std::int32_t lv[] = { 3200, 12800, -150 };
  232. std::memcpy (levels.data(), lv, sizeof (lv));
  233. b.data (0xDB, levels);
  234. }
  235. b.u8 (0x68); b.u16 (5);
  236. b.u8 (0x40); b.u16 (1);
  237. b.u8 (0x15); b.u8 (2);
  238. b.utf16 (0xC9, u"FLEX");
  239. b.utf16 (0xCB, u"Lead");
  240. b.u8 (0x63); b.u16 (0);
  241. std::vector<std::uint8_t> item (80, 0);
  242. std::uint32_t pos = 384, len = 768;
  243. std::uint16_t base = 20480, idx = 20480 + 3, rv = 499 - 2;
  244. std::memcpy (&item[0], &pos, 4);
  245. std::memcpy (&item[4], &base, 2);
  246. std::memcpy (&item[6], &idx, 2);
  247. std::memcpy (&item[8], &len, 4);
  248. std::memcpy (&item[12], &rv, 2);
  249. b.data (0xE9, item);
  250. b.utf16 (0xCC, u"Drums");
  251. b.data (0xEC, std::vector<std::uint8_t> (12, 0));
  252. b.data (0xEC, std::vector<std::uint8_t> (12, 0));
  253. const auto p = flp::parseProject (b.bytes);
  254. CHECK (p.version == "25.2.5.5319");
  255. CHECK (std::abs (p.tempo - 174.5) < 1e-9);
  256. CHECK (p.title == "Título");
  257. CHECK (p.channels.size() == 2);
  258. CHECK (p.channels[0].name == "Kick" && p.channels[0].mixerTrack == 5);
  259. CHECK (p.channels[0].hasLevels && p.channels[0].pan == 3200 && p.channels[0].volume == 12800 && p.channels[0].pitchCents == -150);
  260. CHECK (p.channels[1].type == flp::ChannelType::instrument && p.channels[1].pluginName == "FLEX");
  261. CHECK (p.patterns.size() == 1 && p.patterns[0].index == 3);
  262. CHECK (p.patterns[0].colour && *p.patterns[0].colour == 0x4745A1);
  263. CHECK (p.patterns[0].notes.size() == 2 && p.patterns[0].notes[0].position == 0); // sorted
  264. CHECK (p.patterns[0].notes[1].key == 64 && p.patterns[0].notes[1].length == 48);
  265. CHECK (p.arrangements.size() == 1 && p.arrangements[0].items.size() == 1);
  266. const auto& it = p.arrangements[0].items[0];
  267. CHECK (it.kind == flp::PlaylistItem::Kind::pattern && it.index == 3 && it.track == 2 && it.position == 384);
  268. CHECK (p.mixer.size() == 2 && p.mixer[0].name == "Drums" && p.mixer[1].name.empty());
  269. CHECK (p.warnings.empty());
  270. }
  271. TEST (flp_fuzz_corrupted_files_do_not_crash)
  272. {
  273. if (realFlpDir.empty())
  274. return;
  275. // some real .flp files, corrupted in various ways: the parser may reject them (flp::Error), but
  276. // must never hang, read past the buffer or produce a Song with invalid indices
  277. std::vector<std::vector<std::uint8_t>> sources;
  278. for (auto& entry : std::filesystem::recursive_directory_iterator (realFlpDir))
  279. if (entry.path().extension() == ".flp" && sources.size() < 6 && std::filesystem::file_size (entry.path()) < 3'000'000)
  280. {
  281. std::ifstream in (entry.path(), std::ios::binary);
  282. sources.emplace_back ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  283. }
  284. std::uint32_t seed = 2024;
  285. auto rnd = [&] (std::uint32_t n) { seed = seed * 1664525u + 1013904223u; return n == 0 ? 0u : (seed >> 8) % n; };
  286. int parsed = 0, rejected = 0;
  287. for (int round = 0; round < 240 && ! sources.empty(); ++round)
  288. {
  289. auto bytes = sources[(std::size_t) rnd ((std::uint32_t) sources.size())];
  290. switch (round % 4)
  291. {
  292. case 0: bytes.resize (rnd ((std::uint32_t) bytes.size())); break; // truncated
  293. case 1: for (int k = 0; k < 20; ++k) bytes[(std::size_t) rnd ((std::uint32_t) bytes.size())] ^= (std::uint8_t) (1 + rnd (255)); break;
  294. case 2: // absurd event size
  295. for (int k = 0; k < 5; ++k)
  296. bytes[(std::size_t) rnd ((std::uint32_t) bytes.size())] = 0xFF;
  297. break;
  298. default: // shuffled block
  299. {
  300. const auto a = rnd ((std::uint32_t) bytes.size()), b = rnd ((std::uint32_t) bytes.size());
  301. std::reverse (bytes.begin() + std::min (a, b), bytes.begin() + std::max (a, b));
  302. break;
  303. }
  304. }
  305. try
  306. {
  307. const auto project = flp::parseProject (bytes);
  308. const auto song = lynx::importFlp (project);
  309. ++parsed;
  310. for (const auto& p : song.patterns)
  311. for (const auto& n : p.notes)
  312. CHECK (n.channel >= 0 && n.channel < (int) song.channels.size());
  313. for (const auto& c : song.playlist)
  314. CHECK (c.kind != lynx::ClipKind::pattern || (c.pattern >= 0 && c.pattern < (int) song.patterns.size()));
  315. }
  316. catch (const flp::Error&)
  317. {
  318. ++rejected;
  319. }
  320. }
  321. std::printf (" fuzz: %d imported, %d rejected\n", parsed, rejected);
  322. CHECK (parsed + rejected == 240);
  323. }
  324. TEST (plugin_state_decoders_survive_garbage)
  325. {
  326. // random blobs (varied sizes, including valid headers followed by garbage)
  327. std::uint32_t seed = 77;
  328. auto rnd = [&] { seed = seed * 1664525u + 1013904223u; return seed >> 8; };
  329. const char* plugins[] = { "Fruity Parametric EQ 2", "Fruity Limiter", "Fruity Reeverb 2", "Fruity Delay 2", "Fruity Delay 3",
  330. "Fruity Compressor", "Fruity Balance", "Fruity PanOMatic", "Fruity Stereo Shaper", "Fruity Phase Inverter" };
  331. int decoded = 0;
  332. for (int round = 0; round < 3000; ++round)
  333. {
  334. std::vector<std::uint8_t> blob ((std::size_t) (rnd() % 600));
  335. for (auto& b : blob)
  336. b = (std::uint8_t) rnd();
  337. if (blob.size() >= 4 && round % 3 == 0) // versions the readers accept
  338. {
  339. const std::uint32_t versions[] = { 8, 10001, 10000, 12, 14, 6, 1 };
  340. const auto v = versions[rnd() % 7];
  341. std::memcpy (blob.data(), &v, 4);
  342. }
  343. if (auto fx = lynx::effectFromFl (plugins[rnd() % 10], blob))
  344. {
  345. ++decoded;
  346. for (auto v : fx->params)
  347. CHECK (std::isfinite (v) && v >= 0.0f && v <= 1.0f);
  348. }
  349. lynx::SynthSettings synth;
  350. if (lynx::synthFrom3xOsc (blob, synth))
  351. for (const auto& o : synth.osc)
  352. CHECK (std::isfinite (o.level) && o.level >= 0.0f && o.level <= 1.0f);
  353. float harmlessGain = 1.0f;
  354. if (lynx::synthFromHarmless (blob, 120.0, synth, harmlessGain))
  355. CHECK (std::isfinite (harmlessGain) && std::isfinite (synth.cutoff) && std::isfinite (synth.attack));
  356. if (lynx::synthFromFruitKick (blob, synth))
  357. CHECK (std::isfinite (synth.decay) && std::isfinite (synth.pitchEnv.amount));
  358. if (lynx::synthFromGms (blob, 120.0, synth, harmlessGain) || lynx::synthFromHarmor (blob, synth, harmlessGain) || lynx::synthFromOgun (blob, synth, harmlessGain)
  359. || lynx::synthFromSakura (blob, synth) || lynx::synthFromKepler (blob, synth, harmlessGain))
  360. CHECK (std::isfinite (synth.cutoff) && std::isfinite (synth.attack) && std::isfinite (harmlessGain));
  361. if (auto sound = lynx::drumSynthLiveSound (blob))
  362. CHECK (sound->size() < 256);
  363. if (auto wave = lynx::waveTravellerSample (blob))
  364. CHECK (! wave->first.empty() && wave->second >= 0 && wave->second <= 127);
  365. lynx::synthFromBooBass (blob, synth);
  366. lynx::gmsStateUsable (blob); // (the exact engines' parsers reject garbage without crashing)
  367. lynx::morphineStateUsable (blob);
  368. lynx::poizoneStateUsable (blob);
  369. if (lynx::synthFromPoiZone (blob, synth, harmlessGain) || lynx::synthFromBassDrum (blob, synth, harmlessGain) || lynx::synthFromToxic (blob, synth, harmlessGain)
  370. || lynx::synthFromMorphine (blob, synth, harmlessGain))
  371. CHECK (std::isfinite (synth.cutoff) && std::isfinite (harmlessGain));
  372. if (auto patch = lynx::drumpadPatchName (blob))
  373. CHECK (patch->size() <= 0x30 && std::isfinite (lynx::drumToneForName (*patch).decay));
  374. if (auto flex = lynx::flexPresetName (blob))
  375. CHECK ((! flex->first.empty() || ! flex->second.empty()) && flex->second.size() <= 1024);
  376. if (lynx::synthFromSytrus (blob, 120.0, synth, harmlessGain))
  377. CHECK (std::isfinite (synth.cutoff) && std::isfinite (synth.attack));
  378. if (lynx::synthFromDX10 (blob, synth))
  379. CHECK (std::isfinite (synth.decay) && std::isfinite (synth.release) && std::isfinite (synth.osc[1].level));
  380. if (blob.size() >= 4 && round % 5 == 0) // MiniSynth header followed by garbage
  381. {
  382. std::memcpy (blob.data(), "SSLF", 4);
  383. if (blob.size() > 220)
  384. {
  385. std::memcpy (blob.data() + 40, "DATA", 4);
  386. std::memcpy (blob.data() + 48, "0rev", 4);
  387. }
  388. }
  389. if (lynx::synthFromMiniSynth (blob, 120.0, synth))
  390. CHECK (std::isfinite (synth.cutoff) && std::isfinite (synth.decay) && std::isfinite (synth.attack));
  391. if (lynx::miniSynthStateUsable (blob)) // the exact engine's parser and a short render with silent tables
  392. {
  393. const auto params = lynx::miniSynthParams (blob);
  394. CHECK (params.size() == 43u && std::all_of (params.begin(), params.end(), [] (float v) { return v >= 0.0f && v <= 1.0f; }));
  395. const auto y = lynx::minisynth::renderReference (blob, { { 0, 60, 1.0, 1.0, 0.0, 0.0, 64 } }, 44100.0, 128, "");
  396. CHECK (y.size() == 256u && std::all_of (y.begin(), y.end(), [] (float v) { return std::isfinite (v); }));
  397. }
  398. lynx::ForeignPlugin foreign;
  399. lynx::parseFlWrapper (blob, foreign);
  400. std::vector<std::uint8_t> component, controller;
  401. lynx::splitFlVst3State (blob, component, controller);
  402. }
  403. CHECK (decoded == 3000); // every known name yields an effect (default when the blob is unusable)
  404. }
  405. TEST (inflate_zlib)
  406. {
  407. // fixed Huffman with back-references
  408. const std::uint8_t fixed[] = { 0x78, 0xda, 0x4b, 0x4c, 0x4a, 0x4e, 0xc4, 0x86, 0x22, 0x00, 0x7c, 0x69, 0x09, 0x89 };
  409. const auto a = lynx::inflateZlib (fixed, sizeof (fixed));
  410. CHECK (a && std::string (a->begin(), a->end()) == "abcabcabcabcabcabcabcabcX");
  411. // stored (uncompressed) block
  412. const std::uint8_t stored[] = { 0x78, 0x01, 0x01, 0x03, 0x00, 0xfc, 0xff, 'L', 'Y', 'N', 0x00, 0x00, 0x00, 0x00 };
  413. const auto b = lynx::inflateZlib (stored, sizeof (stored));
  414. CHECK (b && std::string (b->begin(), b->end()) == "LYN");
  415. // truncated, bad header and size limit
  416. CHECK (! lynx::inflateZlib (fixed, 6));
  417. const std::uint8_t bad[] = { 0x12, 0x34, 0x00 };
  418. CHECK (! lynx::inflateZlib (bad, sizeof (bad)));
  419. CHECK (! lynx::inflateZlib (fixed, sizeof (fixed), false, 10));
  420. }
  421. TEST (fl_round15_effects_convert_known_values)
  422. {
  423. auto blob = [] (std::vector<std::int32_t> words) {
  424. std::vector<std::uint8_t> b (words.size() * 4);
  425. std::memcpy (b.data(), words.data(), b.size());
  426. return b;
  427. };
  428. // Frequency Shifter "+800 Hz": frequency 51542 in the 20 kHz range, 100% wet
  429. const auto fs = lynx::effectFromFl ("Frequency Shifter", blob ({ 4, 10000, 0, 51542, 0, 10000, 10000, 5000, 10000, 0, 5000, 0, 0 }));
  430. CHECK (fs && fs->kind == lynx::EffectKind::freqShift);
  431. if (fs)
  432. {
  433. const auto x = (fs->params[0] - 0.5) * 2.0;
  434. CHECK (std::abs (2000.0 * x * std::abs (x) - 800.0) < 10.0);
  435. CHECK (std::abs (fs->params[1] - 1.0f) < 1e-4f);
  436. }
  437. // Emphasis: input 0.0625 (= +1.5 dB) and ceiling 0.9308 (≈ −1.6 dB) -> limiter
  438. const auto em = lynx::effectFromFl ("Emphasis", blob ({ 8, 0, 0, 2, 1, 0, 0, 625, 10000, 10000, 9308, 0, 2, 7, 0 }));
  439. CHECK (em && em->kind == lynx::EffectKind::limiter && em->enabled);
  440. if (em)
  441. {
  442. CHECK (std::abs (em->params[1] * 24.0f - 1.5f) < 0.05f);
  443. CHECK (std::abs (em->params[0] * 12.0f - 12.0f + 1.6f) < 0.1f);
  444. }
  445. // Pitch Shifter +12 st, mix 100%
  446. const auto ps = lynx::effectFromFl ("Pitch Shifter", blob ({ 7, 1200, 0, 0, 2400, 8500, 200, 0, 0, 768, 0, 0, 65536, 0, 65536, 0, 0, 0, 0 }));
  447. CHECK (ps && ps->kind == lynx::EffectKind::pitchShift && std::abs (ps->params[0] - (0.5f + 12.0f / 48.0f)) < 1e-4f);
  448. }
  449. TEST (frequency_shifter_moves_spectrum_by_hz)
  450. {
  451. auto fx = lynx::makeEffect (lynx::EffectKind::freqShift);
  452. fx->prepare (48000.0);
  453. auto slot = lynx::makeEffectSlot (lynx::EffectKind::freqShift);
  454. // +200 Hz: x·|x|·2000 = 200 -> x = sqrt(0.1)
  455. slot.params[0] = (float) (0.5 + std::sqrt (0.1) / 2.0);
  456. std::vector<float> l (48000), r (48000);
  457. for (int i = 0; i < 48000; ++i)
  458. l[(std::size_t) i] = r[(std::size_t) i] = (float) std::sin (2.0 * 3.141592653589793 * 1000.0 * i / 48000.0);
  459. for (int done = 0; done < 48000; done += 480)
  460. fx->process (l.data() + done, r.data() + done, 480, slot.params, 120.0);
  461. auto energy = [&] (double f) {
  462. const auto w = 2.0 * 3.141592653589793 * f / 48000.0, k = 2.0 * std::cos (w);
  463. double s1 = 0.0, s2 = 0.0;
  464. for (int i = 9600; i < 48000; ++i)
  465. {
  466. const auto s0 = l[(std::size_t) i] + k * s1 - s2;
  467. s2 = s1;
  468. s1 = s0;
  469. }
  470. return s1 * s1 + s2 * s2 - k * s1 * s2;
  471. };
  472. // the energy moves to 1200 Hz; little remains at 1000 and almost nothing at the image (800)
  473. CHECK (energy (1200.0) > 50.0 * energy (1000.0));
  474. CHECK (energy (1200.0) > 50.0 * energy (800.0));
  475. }
  476. TEST (pitch_shift_moves_frequency_keeps_length)
  477. {
  478. auto fx = lynx::makeEffect (lynx::EffectKind::pitchShift);
  479. fx->prepare (48000.0);
  480. auto slot = lynx::makeEffectSlot (lynx::EffectKind::pitchShift);
  481. auto crossingsAfter = [&] (float semitoneParam) {
  482. fx->reset();
  483. slot.params[0] = semitoneParam;
  484. std::vector<float> l (48000), r (48000);
  485. for (int i = 0; i < 48000; ++i)
  486. l[(std::size_t) i] = r[(std::size_t) i] = (float) std::sin (2.0 * 3.141592653589793 * 220.0 * i / 48000.0);
  487. for (int done = 0; done < 48000; done += 512)
  488. fx->process (l.data() + done, r.data() + done, std::min (512, 48000 - done), slot.params, 120.0);
  489. int crossings = 0;
  490. for (int i = 9600; i < 48000; ++i)
  491. crossings += (l[(std::size_t) i - 1] < 0.0f) != (l[(std::size_t) i] < 0.0f);
  492. for (auto v : l)
  493. CHECK (std::isfinite (v) && std::abs (v) < 1.6f);
  494. return crossings / 0.8 / 2.0; // cycles per second
  495. };
  496. CHECK (std::abs (crossingsAfter (0.5f) - 220.0) < 8.0); // 0 st
  497. CHECK (std::abs (crossingsAfter (0.5f + 12.0f / 48.0f) - 440.0) < 25.0); // +12 st
  498. CHECK (std::abs (crossingsAfter (0.5f - 12.0f / 48.0f) - 110.0) < 12.0); // −12 st
  499. }
  500. TEST (volume_gate_follows_bar_position)
  501. {
  502. auto fx = lynx::makeEffect (lynx::EffectKind::volumeGate);
  503. fx->prepare (48000.0);
  504. auto slot = lynx::makeEffectSlot (lynx::EffectKind::volumeGate);
  505. for (int i = 0; i < 32; ++i)
  506. slot.params[(std::size_t) i] = i < 16 ? 1.0f : 0.0f; // first two beats open, two closed
  507. slot.params[33] = slot.params[34] = 0.0f; // no smoothing
  508. // 120 BPM: one beat = 0.5 s = 24000 samples; the 4-beat bar starts at tick 0
  509. std::vector<float> l (96000, 1.0f), r (96000, 1.0f);
  510. fx->setSongPosition (0.0);
  511. for (int done = 0; done < 96000; done += 512)
  512. fx->process (l.data() + done, r.data() + done, std::min (512, 96000 - done), slot.params, 120.0);
  513. CHECK (l[10000] > 0.99f && l[40000] > 0.99f); // beats 1 and 2
  514. CHECK (l[60000] < 0.01f && l[90000] < 0.01f); // beats 3 and 4
  515. // starting mid-bar (beat 3): already closed
  516. std::vector<float> l2 (4800, 1.0f), r2 (4800, 1.0f);
  517. fx->reset();
  518. fx->setSongPosition (2.0 * lynx::ppq);
  519. fx->process (l2.data(), r2.data(), 4800, slot.params, 120.0);
  520. CHECK (l2[4000] < 0.01f);
  521. // mix 0: transparent
  522. slot.params[32] = 0.0f;
  523. std::vector<float> l3 (4800, 1.0f), r3 (4800, 1.0f);
  524. fx->setSongPosition (2.0 * lynx::ppq);
  525. fx->process (l3.data(), r3.data(), 4800, slot.params, 120.0);
  526. CHECK (l3[4000] > 0.99f);
  527. }
  528. TEST (patcher_branch_heads_follow_rea)
  529. {
  530. auto words = [] (std::vector<std::int32_t> v) {
  531. std::vector<std::uint8_t> b (v.size() * 4);
  532. std::memcpy (b.data(), v.data(), b.size());
  533. return b;
  534. };
  535. // Frequency Splitter, 2 bands, destinations 1/2/3, slope combo 3 (24 dB/oct), high send −6 dB
  536. std::vector<std::int32_t> fs (18, 0);
  537. fs[0] = 4;
  538. fs[1] = 32768;
  539. fs[3] = 3;
  540. fs[6] = 0;
  541. fs[11] = -600;
  542. fs[15] = 1;
  543. fs[16] = 2;
  544. fs[17] = 3;
  545. const auto split = words (fs);
  546. const auto low = lynx::patcherBranchHead ("Frequency Splitter", split, 1);
  547. CHECK (low.valid && low.slot && low.note.empty());
  548. if (low.slot)
  549. {
  550. // low band = just the Crossover low-pass, combo 3 (24 dB/oct)
  551. CHECK (low.slot->kind == lynx::EffectKind::crossover && low.slot->params[0] == 0.0f && low.slot->params[3] == 1.0f);
  552. CHECK (std::lround (low.slot->params[5] * 7.0f) == 3);
  553. }
  554. const auto mid = lynx::patcherBranchHead ("Frequency Splitter", split, 2);
  555. CHECK (mid.valid && mid.gain == 0.0f); // with 2 bands the mid is silent
  556. const auto high = lynx::patcherBranchHead ("Frequency Splitter", split, 3);
  557. CHECK (high.valid && std::abs (high.gain - std::pow (10.0f, -6.0f / 20.0f)) < 1e-4f);
  558. CHECK (! lynx::patcherBranchHead ("Frequency Splitter", split, 7).valid);
  559. // Stereo Shaper with identity matrix: the difference (destination 4) is zero
  560. const auto shaper = words ({ 3, 0, 12800, 12800, 0, 0, 0, 4, 0 });
  561. const auto diff = lynx::patcherBranchHead ("Fruity Stereo Shaper", shaper, 4);
  562. CHECK (diff.valid && diff.slot && std::abs (diff.slot->params[2] - 0.5f) < 1e-6f && std::abs (diff.slot->params[5] - 0.5f) < 1e-6f);
  563. CHECK (! lynx::patcherBranchHead ("Fruity Stereo Shaper", shaper, 2).valid);
  564. }
  565. TEST (parallel_container_sums_branches)
  566. {
  567. // two branches: dry with gain 0.5 and a utility that flips phase (matrix −1) with gain 0.5 -> silence
  568. auto slot = lynx::makeEffectSlot (lynx::EffectKind::parallel);
  569. lynx::EffectBranch dry;
  570. dry.gain = 0.5f;
  571. lynx::EffectBranch inverted;
  572. inverted.gain = 0.5f;
  573. auto flip = lynx::makeEffectSlot (lynx::EffectKind::utility);
  574. flip.params[2] = flip.params[5] = 0.25f; // (−1 + 2)/4
  575. inverted.chain.push_back (flip);
  576. slot.branches = { dry, inverted };
  577. auto fx = lynx::makeParallelEffect (slot);
  578. fx->prepare (48000.0);
  579. std::vector<float> l (9000, 0.8f), r (9000, -0.4f);
  580. fx->process (l.data(), r.data(), 9000, slot.params, 120.0);
  581. CHECK (std::abs (l[100]) < 1e-5f && std::abs (r[8999]) < 1e-5f);
  582. // only the dry branch with gain 2: doubles
  583. slot.branches = { lynx::EffectBranch { {}, 2.0f } };
  584. auto doubler = lynx::makeParallelEffect (slot);
  585. doubler->prepare (48000.0);
  586. std::fill (l.begin(), l.end(), 0.25f);
  587. doubler->process (l.data(), r.data(), 9000, slot.params, 120.0);
  588. CHECK (std::abs (l[5000] - 0.5f) < 1e-6f);
  589. }
  590. TEST (fl_controller_constant_outputs)
  591. {
  592. std::vector<std::int32_t> xy { 2, 32768, 0, 65536, 0, 16384, 0, 65536, 0, 1024, 256 };
  593. std::vector<std::uint8_t> blob (xy.size() * 4 + 8, 0);
  594. std::memcpy (blob.data(), xy.data(), xy.size() * 4);
  595. CHECK (std::abs (lynx::controllerConstantOutput ("Fruity X-Y Controller", blob, 0).value_or (-1.0f) - 0.5f) < 1e-4f);
  596. CHECK (std::abs (lynx::controllerConstantOutput ("Fruity X-Y Controller", blob, 1).value_or (-1.0f) - 0.25f) < 1e-4f);
  597. // Control Surface: version + group { 0x836: value 0.75, -, output 2 }
  598. std::vector<std::uint8_t> cs (4, 0);
  599. cs[0] = 1;
  600. auto chunk = [] (std::vector<std::uint8_t>& out, std::uint32_t id, const std::vector<std::uint8_t>& body) {
  601. const auto size = (std::uint64_t) body.size();
  602. out.insert (out.end(), reinterpret_cast<const std::uint8_t*> (&id), reinterpret_cast<const std::uint8_t*> (&id) + 4);
  603. out.insert (out.end(), reinterpret_cast<const std::uint8_t*> (&size), reinterpret_cast<const std::uint8_t*> (&size) + 8);
  604. out.insert (out.end(), body.begin(), body.end());
  605. };
  606. std::vector<std::uint8_t> value (12, 0);
  607. const float v = 0.75f;
  608. const std::int32_t out = 2;
  609. std::memcpy (value.data(), &v, 4);
  610. std::memcpy (value.data() + 8, &out, 4);
  611. std::vector<std::uint8_t> group;
  612. chunk (group, 0x836, value);
  613. chunk (cs, 0, group);
  614. CHECK (std::abs (lynx::controllerConstantOutput ("Control Surface", cs, 2).value_or (-1.0f) - 0.75f) < 1e-6f);
  615. CHECK (! lynx::controllerConstantOutput ("Control Surface", cs, 0).has_value());
  616. }
  617. TEST (flp_midi_out_notes_reach_wrapper_on_same_port)
  618. {
  619. auto record = [] (std::vector<std::uint8_t>& out, std::uint32_t id, std::vector<std::uint8_t> body) {
  620. const auto size = (std::uint64_t) body.size();
  621. out.insert (out.end(), reinterpret_cast<const std::uint8_t*> (&id), reinterpret_cast<const std::uint8_t*> (&id) + 4);
  622. out.insert (out.end(), reinterpret_cast<const std::uint8_t*> (&size), reinterpret_cast<const std::uint8_t*> (&size) + 8);
  623. out.insert (out.end(), body.begin(), body.end());
  624. };
  625. auto i32s = [] (std::vector<std::int32_t> v) {
  626. std::vector<std::uint8_t> b (v.size() * 4);
  627. std::memcpy (b.data(), v.data(), b.size());
  628. return b;
  629. };
  630. flp::Project p;
  631. flp::Channel midiOut;
  632. midiOut.index = 0;
  633. midiOut.type = flp::ChannelType::native;
  634. midiOut.pluginName = "MIDI Out";
  635. midiOut.pluginState = i32s ({ 7, 0, -1, -1, -1, 0, 3, 0 }); // channel 1, port 3 (@24)
  636. flp::Channel wrapper;
  637. wrapper.index = 1;
  638. wrapper.type = flp::ChannelType::native;
  639. wrapper.pluginName = "Fruity Wrapper";
  640. wrapper.pluginState = i32s ({ 9 });
  641. record (wrapper.pluginState, 0x32, i32s ({ 7 })); // VST3
  642. record (wrapper.pluginState, 0x01, i32s ({ 3, -1, 0, 0, 0 })); // input port 3
  643. p.channels = { midiOut, wrapper };
  644. flp::Pattern pattern;
  645. pattern.index = 1;
  646. flp::Note note;
  647. note.channel = 0;
  648. note.key = 64;
  649. note.length = 96;
  650. pattern.notes.push_back (note);
  651. p.patterns.push_back (pattern);
  652. const auto song = lynx::importFlp (p);
  653. CHECK (song.channels.size() == 2 && song.channels[0].muted);
  654. CHECK (song.channels[1].plugin.foreign.midiInPort == 3);
  655. int toWrapper = 0;
  656. for (const auto& pat : song.patterns)
  657. for (const auto& n : pat.notes)
  658. toWrapper += n.channel == 1 && n.key == 64;
  659. CHECK (toWrapper == 1);
  660. }
  661. TEST (flp_effect_automation_follows_slots_and_params)
  662. {
  663. // track 1: slot 0 with an effect that has no equivalent (dropped), slot 2 with Parametric EQ 2
  664. flp::Project p;
  665. flp::MixerInsert insert;
  666. insert.index = 1;
  667. insert.slots.push_back ({ 0, "Some Unknown FX", {} });
  668. insert.slots.push_back ({ 2, "Fruity Parametric EQ 2", {} });
  669. p.mixer.push_back (insert);
  670. auto clip = [&] (int index, std::uint32_t target) {
  671. flp::Channel c;
  672. c.index = index;
  673. c.type = flp::ChannelType::automationClip;
  674. c.automation = { { 0.0, 0.0 }, { 96.0, 1.0 } };
  675. p.channels.push_back (c);
  676. p.automationLinks.push_back ({ index, { target } });
  677. };
  678. const auto mixer = [] (int track, int slot, int param) { return 0x70000000u + ((std::uint32_t) track << 22) + ((std::uint32_t) slot << 16) + (std::uint32_t) param; };
  679. clip (0, mixer (1, 2, 0x8000 + 7)); // EQ band 1 frequency
  680. clip (1, mixer (1, 2, 0x1F01)); // EQ slot mix
  681. clip (2, mixer (1, 0, 0x8000)); // parameter of the dropped effect
  682. clip (3, mixer (1, 2, 0x8000 + 21)); // band type: no equivalent
  683. const auto song = lynx::importFlp (p);
  684. CHECK (song.automations.size() == 4);
  685. CHECK (song.mixer[1].effects.size() == 1 && song.mixer[1].effects[0].kind == lynx::EffectKind::parametricEq);
  686. const auto& freq = song.automations[0];
  687. CHECK (freq.target.kind == lynx::AutomationTargetKind::effectParam && freq.target.index == 1 && freq.target.slot == 0);
  688. CHECK (freq.target.param == lynx::peq::param (0, 1));
  689. // FL's 0..1 (20 Hz..20 kHz) becomes our frequency scale
  690. CHECK (std::abs (freq.points.front().value - lynx::peq::hzValue (20.0)) < 1e-4f);
  691. CHECK (std::abs (freq.points.back().value - lynx::peq::hzValue (20000.0)) < 1e-4f);
  692. CHECK (song.automations[1].target.kind == lynx::AutomationTargetKind::effectMix && song.automations[1].target.slot == 0);
  693. CHECK (song.automations[2].target.kind == lynx::AutomationTargetKind::none);
  694. CHECK (song.automations[3].target.kind == lynx::AutomationTargetKind::none);
  695. // the automated mix is read/written through the target
  696. auto copy = song;
  697. CHECK (copy.setTargetValue (song.automations[1].target, 0.25f) && std::abs (copy.mixer[1].effects[0].mix - 0.25f) < 1e-6f);
  698. CHECK (std::abs (copy.targetValue (song.automations[1].target) - 0.25f) < 1e-6f);
  699. }
  700. TEST (flp_real_files)
  701. {
  702. if (realFlpDir.empty())
  703. {
  704. std::printf (" (skipped: pass a folder with .flp files as argument)\n");
  705. return;
  706. }
  707. int files = 0, withNotes = 0, withPlaylist = 0, automations = 0, automationsWithTarget = 0, sends = 0;
  708. int threeOsc = 0, threeOscDecoded = 0, wrapped = 0, wrappedParsed = 0, markers = 0, signatures = 0, trackEqs = 0;
  709. int harmless = 0, harmlessDecoded = 0, harmlessParsed = 0, harmlessKind = 0, sytrus = 0, sytrusDecoded = 0, sytrusEngine = 0, sytrusRendered = 0,
  710. sytrusSounding = 0;
  711. int harmor = 0, harmorUsable = 0, harmorKind = 0, harmorSounding = 0;
  712. int gms = 0, gmsUsable = 0, gmsKind = 0, gmsSounding = 0, morphine = 0, morphineUsable = 0, morphineKind = 0, morphineSounding = 0, demoGoldens = 0;
  713. int ogun = 0, ogunUsable = 0, ogunKind = 0, ogunSounding = 0;
  714. int poizone = 0, poizoneUsable = 0, poizoneKind = 0, poizoneSounding = 0, poizoneGoldens = 0;
  715. int miniSynth = 0, miniSynthUsable = 0, miniSynthKind = 0, miniSynthSounding = 0, miniSynthGoldens = 0;
  716. int dx10 = 0, dx10Usable = 0, dx10Kind = 0, dx10Sounding = 0;
  717. int kepler = 0, keplerUsable = 0, keplerKind = 0, keplerSounding = 0;
  718. int toxic = 0, toxicUsable = 0, toxicKind = 0, toxicSounding = 0, toxicGoldens = 0; // REA_rodada45
  719. std::map<std::string, std::pair<int, int>> generators; // GMS/Ogun/Autogun/Sakura/DrumSynth: channels, decoded
  720. int ogunTrumpet = 0; // Ogun channels whose spectrum matches the REA "Trumpet" reference
  721. int peqSlots = 0, peqShaped = 0, reverbs = 0, reverbsDryless = 0, reverbsLowCut = 0;
  722. int dwChannels = 0, dwZones = 0, fpcKits = 0, padChannels = 0, padNotes = 0;
  723. int harmlessCompWanted = 0, harmlessCompLoaded = 0; // Harmless rack compressors / presets read from the installation
  724. int maximusSlots = 0, maximusExact = 0, sgzSlots = 0, sgzExact = 0, vocodexSlots = 0, vocodexWithData = 0; // REA_rodada40
  725. int shaperSlots = 0, shaperExact = 0, clipperSlots = 0, clipperExact = 0; // REA_rodada51
  726. int chorusSlots = 0, chorusExact = 0; // REA_rodada59: the exact choruses
  727. // import with the installation's samples (realFlpDir is ...\Data): for DirectWave/FPC
  728. lynx::FlpImportOptions withFiles;
  729. const auto flRoot = std::filesystem::path (realFlpDir).parent_path().string();
  730. withFiles.roots = { flRoot, realFlpDir, "" };
  731. withFiles.fileExists = [] (const std::string& path) {
  732. auto local = path;
  733. std::replace (local.begin(), local.end(), '\\', '/');
  734. return std::filesystem::exists (local);
  735. };
  736. withFiles.readFile = [] (const std::string& path) {
  737. auto local = path;
  738. std::replace (local.begin(), local.end(), '\\', '/');
  739. std::ifstream in (local, std::ios::binary);
  740. return std::vector<std::uint8_t> ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  741. };
  742. int embeddedSaved = 0;
  743. withFiles.saveEmbedded = [&embeddedSaved] (const std::vector<std::uint8_t>& wav, const std::string&) {
  744. ++embeddedSaved;
  745. return wav.size() > 44 ? std::string ("embutido.wav") : std::string();
  746. };
  747. int slicedChannels = 0, flKeysChannels = 0;
  748. for (auto& entry : std::filesystem::recursive_directory_iterator (realFlpDir))
  749. {
  750. if (entry.path().extension() != ".flp")
  751. continue;
  752. std::ifstream in (entry.path(), std::ios::binary);
  753. std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  754. ++files;
  755. try
  756. {
  757. const auto p = flp::parseProject (bytes);
  758. CHECK (! p.version.empty());
  759. CHECK (p.tempo > 10.0 && p.tempo < 1000.0);
  760. const auto song = lynx::importFlp (p);
  761. CHECK (! song.patterns.empty());
  762. {
  763. const auto full = lynx::importFlp (p, withFiles);
  764. bool hasFpc = false;
  765. for (const auto& c : p.channels)
  766. hasFpc = hasFpc || c.pluginName == "FPC";
  767. // GMS and Morphine (REA_rodada39): every state plays on the exact engine (GMS with its waves from the
  768. // installation) and sounds; two demo channels match golden samples of our round-39 models
  769. for (const auto& c : p.channels)
  770. {
  771. if (c.pluginName == "GMS")
  772. ++gms, gmsUsable += lynx::gmsStateUsable (c.pluginState) ? 1 : 0;
  773. if (c.pluginName == "Morphine")
  774. ++morphine, morphineUsable += lynx::morphineStateUsable (c.pluginState) ? 1 : 0;
  775. }
  776. // Kepler: every state in the demos plays on the exact engine (REA_rodada46) when the engine DLL is installed
  777. for (const auto& c : p.channels)
  778. if (c.pluginName == "Kepler")
  779. {
  780. ++kepler;
  781. keplerUsable += lynx::keplerStateUsable (c.pluginState) ? 1 : 0;
  782. }
  783. for (const auto& c : full.channels)
  784. if (c.instrument == lynx::InstrumentKind::kepler)
  785. {
  786. ++keplerKind;
  787. const auto y = lynx::kepler::renderReference (c.pluginState, c.samplePath, { { 0, 60, 1.0, 0.0, 0.0, 6000 }, { 2000, 67, 0.6, 0.0, 0.0, 8000 } },
  788. 44100.0, 11025);
  789. double e = 0.0;
  790. bool finite = ! y.empty();
  791. for (auto v : y)
  792. finite = finite && std::isfinite (v), e += (double) v * v;
  793. CHECK (finite);
  794. keplerSounding += e > 1e-6 ? 1 : 0;
  795. }
  796. const auto file = entry.path().filename().string();
  797. for (const auto& c : full.channels)
  798. {
  799. const auto isGms = c.instrument == lynx::InstrumentKind::gms, isMorphine = c.instrument == lynx::InstrumentKind::morphine;
  800. if (! isGms && ! isMorphine)
  801. continue;
  802. (isGms ? gmsKind : morphineKind)++;
  803. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  804. const auto golden = (isGms && c.name == "GMS" && file.find ("Song For You") != std::string::npos)
  805. || (isMorphine && c.name == "Morphine" && file.find ("SHINE") != std::string::npos);
  806. std::vector<float> y;
  807. if (isGms)
  808. y = lynx::gms::renderReference (c.pluginState,
  809. { { 0, 60, (float) iv (100), 1.0, 0.0, 0.0, 30000 }, { 11025, 64, (float) iv (80), 1.0, 0.0, 0.0, 40000 },
  810. { 22050, 67, (float) iv (110), 1.0, 0.0, 0.0, 52000 }, { 60000, 48, (float) iv (127), 1.0, 0.0, 0.0, 70000 } },
  811. 44100.0, golden ? 88200 : 22050, c.samplePath);
  812. else
  813. y = lynx::morphine::renderReference (c.pluginState, { { 0, 60, 1.0, 1.0, 0.0, 0.0, 30000 }, { 11025, 64, 0.5, 1.0, 0.0, 0.0, 50000 }, { 22050, 67, 2.0, 1.0, 0.0, 0.0, 70000 } },
  814. 44100.0, golden ? 100000 : 22050, c.samplePath);
  815. double e = 0.0;
  816. bool finite = ! y.empty();
  817. for (auto v : y)
  818. finite = finite && std::isfinite (v), e += (double) v * v;
  819. CHECK (finite);
  820. (isGms ? gmsSounding : morphineSounding) += e > 1e-6 ? 1 : 0;
  821. if (golden && ! y.empty())
  822. {
  823. // our model, same events (GMS: tempo 140; Morphine: tempo 120), state as saved, 44.1 kHz
  824. const auto rms = std::sqrt (e / (double) y.size());
  825. CHECK (std::abs (rms - (isGms ? 0.213602668574 : 0.0673606448152)) < 1e-5);
  826. static const struct { int i; float l, r; } gmsAt[] = { { 2000, 0.0890912563f, 0.00406081323f }, { 15000, 0.395968914f, 0.213082865f }, { 30000, -0.33275044f, -0.530703545f }, { 50000, 0.277897179f, 0.482887864f }, { 65000, 0.266831428f, 0.366135627f }, { 80000, 0.00021339106f, 0.000807374308f } },
  827. morAt[] = { { 2000, 0.0620051622f, 0.0791224241f }, { 15000, 0.000745030935f, 0.0800722092f }, { 30000, 0.102371402f, 0.0262862686f }, { 50000, 0.000215340013f, 0.0249186736f }, { 65000, -0.0414967388f, -0.0449344926f }, { 90000, -0.00397050008f, -0.00165477558f } };
  828. for (const auto& g : isGms ? std::vector (std::begin (gmsAt), std::end (gmsAt)) : std::vector (std::begin (morAt), std::end (morAt)))
  829. CHECK (std::abs (y[2 * (std::size_t) g.i] - g.l) < 1e-5f && std::abs (y[2 * (std::size_t) g.i + 1] - g.r) < 1e-5f);
  830. ++demoGoldens;
  831. }
  832. }
  833. // MiniSynth (REA_rodada41): every state plays on the exact engine (tables from the installation's
  834. // oscdata.dat) and sounds; two demo channels match golden samples of our round-41 model (bit-identical to
  835. // renders of the installation's DLL for these states)
  836. for (const auto& c : p.channels)
  837. if (c.pluginName == "MiniSynth")
  838. ++miniSynth, miniSynthUsable += lynx::miniSynthStateUsable (c.pluginState) ? 1 : 0;
  839. for (const auto& c : full.channels)
  840. {
  841. if (c.instrument != lynx::InstrumentKind::minisynth)
  842. continue;
  843. ++miniSynthKind;
  844. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  845. const auto exo = file.find ("Exoplanet") != std::string::npos;
  846. const auto golden = exo && (c.name == "Main Lead 1" || c.name == "Riser");
  847. const auto y = lynx::minisynth::renderReference (c.pluginState,
  848. { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 }, { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  849. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 }, { 60000, 48, iv (127), 1.0, 0.0, 0.0, 70000 } },
  850. 44100.0, golden ? 88200 : 22050, c.samplePath);
  851. double e = 0.0;
  852. bool finite = ! y.empty();
  853. for (auto v : y)
  854. finite = finite && std::isfinite (v), e += (double) v * v;
  855. CHECK (finite);
  856. miniSynthSounding += e > 1e-6 ? 1 : 0;
  857. if (golden && ! y.empty())
  858. {
  859. // our model, same events, tempo 120, state as saved, 44.1 kHz, host blocks of 512, L = R = Final.Vol
  860. const auto lead = c.name == "Main Lead 1";
  861. CHECK (std::abs (std::sqrt (e / (double) y.size()) - (lead ? 0.177495801753 : 0.19006567413)) < 1e-9);
  862. static const struct { int i; float v; } leadAt[] = { { 2000, -0.113459975f }, { 15000, -0.00234968495f }, { 30000, 0.13985157f }, { 50000, -0.267063439f }, { 65000, -0.244418889f }, { 80000, -0.0162940286f } },
  863. riserAt[] = { { 2000, -0.0603227727f }, { 15000, 0.112200871f }, { 30000, 0.0453962795f }, { 50000, 0.234380022f }, { 65000, -0.164545432f }, { 80000, -0.000485120923f } };
  864. for (const auto& g : lead ? std::vector (std::begin (leadAt), std::end (leadAt)) : std::vector (std::begin (riserAt), std::end (riserAt)))
  865. CHECK (y[2 * (std::size_t) g.i] == g.v && y[2 * (std::size_t) g.i + 1] == g.v);
  866. ++miniSynthGoldens;
  867. }
  868. }
  869. // PoiZone (REA_rodada44): every state plays on the exact engine and sounds; the demo channel matches golden
  870. // samples of our round-44 model
  871. for (const auto& c : p.channels)
  872. if (c.pluginName == "PoiZone")
  873. ++poizone, poizoneUsable += lynx::poizoneStateUsable (c.pluginState) ? 1 : 0;
  874. for (const auto& c : full.channels)
  875. {
  876. if (c.instrument != lynx::InstrumentKind::poizone)
  877. continue;
  878. ++poizoneKind;
  879. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  880. const auto golden = c.name == "PoiZone" && file.find ("NIGHT TERROR") != std::string::npos;
  881. const auto y = lynx::poizone::renderReference (c.pluginState,
  882. { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 }, { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  883. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 }, { 60000, 48, iv (127), 1.0, 0.0, 0.0, 70000 } },
  884. 44100.0, golden ? 88200 : 22050);
  885. double e = 0.0;
  886. bool finite = ! y.empty();
  887. for (auto v : y)
  888. finite = finite && std::isfinite (v), e += (double) v * v;
  889. CHECK (finite);
  890. poizoneSounding += e > 1e-6 ? 1 : 0;
  891. if (golden && ! y.empty())
  892. {
  893. // our model, same events, tempo 120, state as saved, 44.1 kHz, L = R = Final.Vol
  894. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.018975758213507415) < 1e-7);
  895. static const struct { int i; float l, r; } at[] = { { 2000, -0.0030244605f, -0.0031888473f }, { 15000, 0.002743553f, -0.013375883f }, { 30000, 0.031268276f, 0.04751329f }, { 50000, 0.049796376f, 0.035944063f }, { 65000, -0.0014251951f, -0.01722545f }, { 80000, 0.01474628f, 0.000680499f } };
  896. for (const auto& g : at)
  897. CHECK (std::abs (y[2 * (std::size_t) g.i] - g.l) < 1e-7f && std::abs (y[2 * (std::size_t) g.i + 1] - g.r) < 1e-7f);
  898. ++poizoneGoldens;
  899. }
  900. }
  901. // Toxic Biohazard (REA_rodada45): every demo state plays on the exact engine with the waves read from the
  902. // installation's DLL and sounds; the "Bombs Away" channel matches golden samples of our round-45 model
  903. for (const auto& c : p.channels)
  904. if (c.pluginName == "Toxic Biohazard")
  905. ++toxic, toxicUsable += lynx::toxicStateUsable (c.pluginState) ? 1 : 0;
  906. for (const auto& c : full.channels)
  907. {
  908. if (c.instrument != lynx::InstrumentKind::toxic)
  909. continue;
  910. ++toxicKind;
  911. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  912. const auto golden = file.find ("Bombs Away") != std::string::npos;
  913. const auto y = lynx::toxic::renderReference (
  914. c.pluginState,
  915. { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 }, { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  916. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 }, { 60000, 48, iv (127), 1.0, 0.0, 0.0, 70000 } },
  917. 44100.0, golden ? 88200 : 22050, c.samplePath, 512, 140.0);
  918. double e = 0.0;
  919. bool finite = ! y.empty();
  920. for (auto v : y)
  921. finite = finite && std::isfinite (v), e += (double) v * v;
  922. CHECK (finite);
  923. toxicSounding += e > 1e-6 ? 1 : 0;
  924. if (golden && ! y.empty())
  925. {
  926. // our model (r45/py/toxmodel.py, which equals the DLL bit for bit), state as saved, tempo 140, 44.1 kHz
  927. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.0832500924945) < 1e-6);
  928. static const struct { int i; float l, r; } at[] = { { 2000, -0.0501383618f, -0.100676455f }, { 15000, -0.0949445963f, -0.0714591742f }, { 30000, -0.0305469204f, 0.037349131f }, { 50000, 0.0255811438f, -0.0238218959f }, { 65000, -0.111861639f, -0.113630719f }, { 80000, 0.0163135566f, 0.0199630354f } };
  929. for (const auto& g : at)
  930. CHECK (std::abs (y[2 * (std::size_t) g.i] - g.l) < 1e-6f && std::abs (y[2 * (std::size_t) g.i + 1] - g.r) < 1e-6f);
  931. ++toxicGoldens;
  932. }
  933. }
  934. for (const auto& c : full.channels)
  935. {
  936. if (c.instrument == lynx::InstrumentKind::harmless && c.modelParams.size() >= (std::size_t) lynx::harmlessParamCount
  937. && c.modelParams[(std::size_t) lynx::harmlessComboIndex + 3] > 0.5f)
  938. {
  939. ++harmlessCompWanted;
  940. harmlessCompLoaded += c.modelParams.size() > (std::size_t) lynx::harmlessCompIndex + 1000 ? 1 : 0;
  941. }
  942. flKeysChannels += c.zones.size() == 30 && c.zones.front().path.find ("CHANGPIANO") != std::string::npos ? 1 : 0;
  943. if (c.sampler.slices.size() >= 4)
  944. {
  945. ++slicedChannels;
  946. CHECK (std::is_sorted (c.sampler.slices.begin(), c.sampler.slices.end()));
  947. CHECK (c.sampler.slices.back() < 1.0f);
  948. }
  949. if (! c.zones.empty() && c.name.find (" - ") == std::string::npos)
  950. {
  951. ++dwChannels;
  952. dwZones += (int) c.zones.size();
  953. }
  954. padChannels += c.name.find (" - ") != std::string::npos && c.instrument == lynx::InstrumentKind::sampler ? 1 : 0;
  955. }
  956. fpcKits += hasFpc;
  957. // Maximus / Soundgoodizer become the exact engine (REA_rodada40); Vocodex carries SG.dat (and its
  958. // carrier's Sytrus preset) read from the installation
  959. for (const auto& insert : p.mixer)
  960. for (const auto& slot : insert.slots)
  961. {
  962. maximusSlots += slot.plugin == "Maximus";
  963. sgzSlots += slot.plugin == "Soundgoodizer";
  964. vocodexSlots += slot.plugin == "Vocodex";
  965. shaperSlots += slot.plugin == "Fruity WaveShaper";
  966. clipperSlots += slot.plugin == "Fruity Soft Clipper";
  967. chorusSlots += lynx::chorusControls (slot.plugin) > 0 && slot.state.size() >= 4 ? 1 : 0;
  968. }
  969. for (const auto& t : full.mixer)
  970. for (const auto& fx : t.effects)
  971. {
  972. if (fx.kind == lynx::EffectKind::maximus)
  973. {
  974. const auto blob = lynx::maximusfx::unpack (fx.state);
  975. CHECK (blob && lynx::maximusfx::presetOf (*blob) != nullptr);
  976. if (blob)
  977. (blob->build == lynx::hlfx::MaximusBuild::soundgoodizer ? sgzExact : maximusExact) += 1;
  978. }
  979. if (fx.kind == lynx::EffectKind::vocodex)
  980. vocodexWithData += ! lynx::vocodex::splitInstallData (fx.state).second.sgDat.empty();
  981. if (fx.kind == lynx::EffectKind::fruityWaveShaper)
  982. shaperExact += lynx::flshape::parseWaveShaper (fx.state).has_value() ? 1 : 0;
  983. clipperExact += fx.kind == lynx::EffectKind::fruitySoftClipper;
  984. chorusExact += fx.kind == lynx::EffectKind::fruityChorus || fx.kind == lynx::EffectKind::hyperChorus || fx.kind == lynx::EffectKind::vintageChorus ? 1 : 0;
  985. }
  986. for (const auto& pat : full.patterns)
  987. for (const auto& n : pat.notes)
  988. {
  989. CHECK (n.channel >= 0 && n.channel < (int) full.channels.size());
  990. padNotes += full.channels[(std::size_t) n.channel].name.find (" - ") != std::string::npos ? 1 : 0;
  991. }
  992. }
  993. const auto peqDefaults = lynx::makeEffectSlot (lynx::EffectKind::parametricEq).params;
  994. for (const auto& t : song.mixer)
  995. for (const auto& fx : t.effects)
  996. {
  997. if (fx.kind == lynx::EffectKind::parametricEq)
  998. {
  999. ++peqSlots;
  1000. peqShaped += fx.params != peqDefaults;
  1001. for (double hz : { 50.0, 1000.0, 10000.0 })
  1002. CHECK (std::isfinite (lynx::peq::responseDb (fx.params, 48000.0, hz)));
  1003. }
  1004. if (fx.kind == lynx::EffectKind::reverb)
  1005. {
  1006. ++reverbs;
  1007. reverbsDryless += fx.params[4] < 0.01f;
  1008. reverbsLowCut += fx.params[5] > 0.0f;
  1009. }
  1010. if (fx.kind == lynx::EffectKind::reeverb2) // FL's exact Reeverb 2 (REA_rodada49): FL's own controls
  1011. {
  1012. const auto r2 = lynx::reeverb2::fromParams (fx.params);
  1013. ++reverbs;
  1014. reverbsDryless += r2.p[10] == 0;
  1015. reverbsLowCut += r2.p[0] >= 20;
  1016. }
  1017. }
  1018. for (const auto& t : song.mixer)
  1019. if (t.eq.active())
  1020. {
  1021. ++trackEqs;
  1022. for (std::size_t b = 0; b < 3; ++b)
  1023. CHECK (t.eq.hz[b] >= 10.0f && t.eq.hz[b] <= 16000.0f && std::abs (t.eq.gainDb[b]) <= 18.0f);
  1024. }
  1025. for (const auto& a : song.arrangements)
  1026. for (const auto& m : a.markers)
  1027. {
  1028. ++markers;
  1029. signatures += m.isSignature();
  1030. CHECK (m.tick >= 0);
  1031. }
  1032. // Fruity Wrapper (channels and mixer slots): every blob in the demos is read
  1033. auto countWrapper = [&] (const std::string& plugin, const std::vector<std::uint8_t>& state) {
  1034. if (plugin != "Fruity Wrapper")
  1035. return;
  1036. ++wrapped;
  1037. lynx::ForeignPlugin f;
  1038. wrappedParsed += lynx::parseFlWrapper (state, f) && f.format != lynx::ForeignPlugin::Format::none && ! f.name.empty();
  1039. };
  1040. for (const auto& c : p.channels)
  1041. countWrapper (c.pluginName, c.pluginState);
  1042. for (const auto& insert : p.mixer)
  1043. for (const auto& slot : insert.slots)
  1044. countWrapper (slot.plugin, slot.state);
  1045. // Harmless: every state in the demos becomes a synth, with finite values
  1046. for (const auto& c : p.channels)
  1047. if (c.pluginName == "Harmless")
  1048. {
  1049. ++harmless;
  1050. lynx::SynthSettings hs;
  1051. float g = 1.0f;
  1052. if (lynx::synthFromHarmless (c.pluginState, 128.0, hs, g))
  1053. {
  1054. ++harmlessDecoded;
  1055. CHECK (std::isfinite (g) && hs.cutoff >= 20.0f && hs.cutoff <= 20000.0f && std::isfinite (hs.decay) && std::isfinite (hs.release));
  1056. }
  1057. if (const auto hp = lynx::harmlessParams (c.pluginState))
  1058. harmlessParsed += (int) hp->size() == lynx::harmlessParamCount ? 1 : 0;
  1059. }
  1060. for (const auto& c : song.channels)
  1061. harmlessKind += c.instrument == lynx::InstrumentKind::harmless ? 1 : 0;
  1062. // Harmor: every state in the demos plays on the exact engine (REA_rodada34) and sounds, with finite samples
  1063. for (const auto& c : p.channels)
  1064. if (c.pluginName == "Harmor")
  1065. {
  1066. ++harmor;
  1067. harmorUsable += lynx::harmorStateUsable (c.pluginState) ? 1 : 0;
  1068. }
  1069. for (const auto& c : song.channels)
  1070. if (c.instrument == lynx::InstrumentKind::harmor)
  1071. {
  1072. ++harmorKind;
  1073. const auto y = lynx::harmor::renderReference (c.pluginState, { { 0, 60, 0.8, 1.0, 0.0, 6000 }, { 2000, 67, 0.6, 1.0, 0.0, 8000 } }, 44100.0, 11025);
  1074. double e = 0.0;
  1075. bool finite = ! y.empty();
  1076. for (auto v : y)
  1077. finite = finite && std::isfinite (v), e += (double) v * v;
  1078. CHECK (finite);
  1079. harmorSounding += e > 1e-6 ? 1 : 0;
  1080. }
  1081. // Ogun: every state in the demos plays on the exact engine (REA_rodada37) and sounds, with finite samples
  1082. for (const auto& c : p.channels)
  1083. if (c.pluginName == "Ogun")
  1084. {
  1085. ++ogun;
  1086. ogunUsable += lynx::ogunStateUsable (c.pluginState) ? 1 : 0;
  1087. }
  1088. for (const auto& c : song.channels)
  1089. if (c.instrument == lynx::InstrumentKind::ogun)
  1090. {
  1091. ++ogunKind;
  1092. const auto y = lynx::ogun::renderReference (c.pluginState, { { 0, 60, 0.8, 1.0, 0.0, 0.0, 0.0, 0.0, 0.5, 6000 },
  1093. { 2000, 67, 0.6, 1.0, 0.0, 0.0, 0.0, 0.0, 0.5, 8000 } }, 44100.0, 11025);
  1094. double e = 0.0;
  1095. bool finite = ! y.empty();
  1096. for (auto v : y)
  1097. finite = finite && std::isfinite (v), e += (double) v * v;
  1098. CHECK (finite);
  1099. ogunSounding += e > 1e-6 ? 1 : 0;
  1100. }
  1101. // Fruity DX10: every state plays on the exact engine when its channel processor is neutral (REA_rodada42)
  1102. for (const auto& c : p.channels)
  1103. if (c.pluginName == "Fruity DX10" || c.pluginName == "DX10")
  1104. ++dx10, dx10Usable += lynx::dx10StateUsable (c.pluginState) ? 1 : 0;
  1105. for (const auto& c : song.channels)
  1106. if (c.instrument == lynx::InstrumentKind::dx10)
  1107. {
  1108. ++dx10Kind;
  1109. const auto y = lynx::dx10::renderReference (c.pluginState, { { 0, 60, 1.0, 1.0, 0.0, 0.0, 6000 }, { 2000, 67, 0.6, 1.0, 0.0, 0.0, 8000 } },
  1110. 44100.0, 11025);
  1111. double e = 0.0;
  1112. bool finite = ! y.empty();
  1113. for (auto v : y)
  1114. finite = finite && std::isfinite (v), e += (double) v * v;
  1115. CHECK (finite);
  1116. dx10Sounding += e > 1e-6 ? 1 : 0;
  1117. }
  1118. // Sytrus: every state in the demos (zlib + 10 units) decodes, with finite values
  1119. for (const auto& c : p.channels)
  1120. if (c.pluginName == "Sytrus")
  1121. {
  1122. ++sytrus;
  1123. lynx::SynthSettings ss;
  1124. float g = 1.0f;
  1125. // the Sytrus engine (REA_rodada31) reads every demo state; the first 24 play a short note (render settings)
  1126. if (lynx::sytrus::stateUsable (c.pluginState))
  1127. {
  1128. ++sytrusEngine;
  1129. if (sytrusRendered < 24)
  1130. {
  1131. ++sytrusRendered;
  1132. lynx::sytrus::Engine e;
  1133. e.prepare (44100.0, true, 512);
  1134. e.setPatch (lynx::sytrus::parsePatch (c.pluginState));
  1135. lynx::sytrus::NoteParams note;
  1136. e.noteOn (note);
  1137. std::vector<float> l (8192), r (8192);
  1138. e.render (l.data(), r.data(), 8192, 0.551f, 0.0f);
  1139. bool finite = true;
  1140. double energy = 0.0;
  1141. for (std::size_t k = 0; k < l.size(); ++k)
  1142. finite = finite && std::isfinite (l[k]) && std::isfinite (r[k]), energy += (double) l[k] * l[k];
  1143. CHECK (finite);
  1144. sytrusSounding += energy > 1e-6;
  1145. }
  1146. }
  1147. if (lynx::synthFromSytrus (c.pluginState, 128.0, ss, g))
  1148. {
  1149. ++sytrusDecoded;
  1150. CHECK (std::isfinite (g) && ss.cutoff >= 20.0f && ss.cutoff <= 20000.0f && std::isfinite (ss.attack) && std::isfinite (ss.decay)
  1151. && std::isfinite (ss.release) && ss.sustain >= 0.0f && ss.sustain <= 1.0f && std::isfinite (ss.filterEnv.amount));
  1152. for (const auto& o : ss.osc)
  1153. CHECK (o.level >= 0.0f && o.level <= 1.0f && o.octave >= -3 && o.octave <= 3);
  1154. }
  1155. }
  1156. // round 12 generators
  1157. for (const auto& c : p.channels)
  1158. {
  1159. lynx::SynthSettings gs;
  1160. float g = 1.0f;
  1161. int decoded = -1;
  1162. if (c.pluginName == "Harmor")
  1163. {
  1164. decoded = lynx::synthFromHarmor (c.pluginState, gs, g);
  1165. if (decoded)
  1166. {
  1167. // REA_rodada25 §4: most resonant settings stay below Q ≈ 3, a few saturate
  1168. CHECK (gs.resonance >= 0.0f && gs.resonance <= 1.0f);
  1169. static int resonant = 0, strong = 0, total = 0;
  1170. resonant += gs.resonance > 0.0f;
  1171. strong += gs.resonance > 0.9f;
  1172. if (++total % 50 == 0)
  1173. std::printf (" Harmor resonance: %d with a peak, %d with Q > ~5 (of %d)\n", resonant, strong, total);
  1174. }
  1175. static int saw = 0, square = 0, other = 0, total = 0;
  1176. if (decoded && std::any_of (gs.osc.begin(), gs.osc.end(), [] (const lynx::SynthOscillator& o) { return o.wave == lynx::Waveform::spectral; }))
  1177. {
  1178. auto isSaw = true, isSquare = true;
  1179. for (int n = 1; n <= 16; ++n)
  1180. {
  1181. isSaw = isSaw && std::abs (gs.spectrum[(std::size_t) n - 1] - 1.0f / n) < 1e-3f;
  1182. isSquare = isSquare && std::abs (gs.spectrum[(std::size_t) n - 1] - (n % 2 == 1 ? 1.1535f / n : 0.0f)) < 2e-3f;
  1183. }
  1184. ++(isSaw ? saw : isSquare ? square : other);
  1185. if (++total % 20 == 0 || total == 100)
  1186. std::printf (" Harmor spectrum: %d saw, %d square, %d other (of %d)\n", saw, square, other, total);
  1187. }
  1188. }
  1189. else if (c.pluginName == "GMS")
  1190. decoded = lynx::synthFromGms (c.pluginState, 128.0, gs, g);
  1191. else if (c.pluginName == "Ogun" || c.pluginName == "Autogun")
  1192. {
  1193. decoded = lynx::synthFromOgun (c.pluginState, gs, g);
  1194. // REA_rodada24 §1.9: the "Trumpet" preset (richness 7, no seeds) has partials 0.9087, 0.8610,
  1195. // 0.8151… at the fixed seed-3 phases 0.919, 0.380, 0.130
  1196. if (decoded && gs.osc[0].wave == lynx::Waveform::spectral)
  1197. {
  1198. CHECK (std::all_of (gs.spectrum.begin(), gs.spectrum.end(), [] (float a) { return std::isfinite (a) && a >= 0.0f; }));
  1199. static constexpr float trumpet[3] { 0.9087f, 0.8610f, 0.8151f }, phases[3] { 0.919f, 0.380f, 0.130f };
  1200. auto match = true;
  1201. for (std::size_t k = 0; k < 3; ++k)
  1202. match = match && std::abs (gs.spectrum[k] * 2.0f / 3.14159265f - trumpet[k]) < 1e-3f && std::abs (gs.spectrumPhase[k] - phases[k]) < 1e-3f;
  1203. ogunTrumpet += match ? 1 : 0;
  1204. }
  1205. }
  1206. else if (c.pluginName == "Sakura")
  1207. decoded = lynx::synthFromSakura (c.pluginState, gs);
  1208. else if (c.pluginName == "Kepler")
  1209. decoded = lynx::synthFromKepler (c.pluginState, gs, g);
  1210. else if (c.pluginName == "Wave Traveller")
  1211. decoded = lynx::waveTravellerSample (c.pluginState).has_value() ? 2 : 0;
  1212. else if (c.pluginName == "PoiZone")
  1213. decoded = lynx::synthFromPoiZone (c.pluginState, gs, g);
  1214. else if (c.pluginName == "BassDrum")
  1215. decoded = lynx::synthFromBassDrum (c.pluginState, gs, g);
  1216. else if (c.pluginName == "Toxic Biohazard")
  1217. decoded = lynx::synthFromToxic (c.pluginState, gs, g);
  1218. else if (c.pluginName == "Morphine")
  1219. decoded = lynx::synthFromMorphine (c.pluginState, gs, g);
  1220. else if (c.pluginName == "BooBass")
  1221. decoded = lynx::synthFromBooBass (c.pluginState, gs);
  1222. else if (c.pluginName == "Drumpad" || c.pluginName == "DrumPad")
  1223. decoded = lynx::drumpadPatchName (c.pluginState).has_value() ? 2 : 0;
  1224. else if (c.pluginName == "Fruity Envelope Controller" && c.pluginState.size() >= 4 && c.pluginState[2] >= 6) // v >= 6
  1225. decoded = lynx::parseEnvelopeController (c.pluginState).has_value() ? 2 : 0;
  1226. else if (c.pluginName == "FLEX")
  1227. decoded = lynx::flexPresetName (c.pluginState).has_value() ? 2 : 0;
  1228. else if (c.pluginName == "Fruity DrumSynth Live")
  1229. decoded = lynx::drumSynthLiveParams (c.pluginState).has_value() ? 2 : 0;
  1230. if (decoded < 0)
  1231. continue;
  1232. auto& counter = generators[c.pluginName];
  1233. ++counter.first;
  1234. counter.second += decoded != 0;
  1235. if (decoded == 1 && c.pluginName != "Fruity DrumSynth Live")
  1236. {
  1237. CHECK (std::isfinite (g) && g >= 0.0f && gs.cutoff >= 20.0f && gs.cutoff <= 20000.0f && gs.sustain >= 0.0f && gs.sustain <= 1.0f);
  1238. CHECK (std::isfinite (gs.attack) && std::isfinite (gs.decay) && std::isfinite (gs.release) && std::isfinite (gs.filterEnv.amount) && std::isfinite (gs.lfo[lynx::lfoCutoff].amount));
  1239. for (const auto& o : gs.osc)
  1240. CHECK (o.level >= 0.0f && o.level <= 4.0f && std::isfinite (o.detune) && std::isfinite (o.pan));
  1241. }
  1242. }
  1243. // 3x Osc: every state in the demos decodes, with levels summing to at most 1
  1244. for (const auto& c : p.channels)
  1245. if (c.pluginName == "3x Osc")
  1246. {
  1247. ++threeOsc;
  1248. lynx::SynthSettings s;
  1249. if (lynx::synthFrom3xOsc (c.pluginState, s))
  1250. {
  1251. ++threeOscDecoded;
  1252. // with AM, osc 3 is the modulation depth and is left out of the sum
  1253. const auto sum = s.osc[0].level + s.osc[1].level + (s.am ? 0.0f : s.osc[2].level);
  1254. CHECK (sum <= 1.001f && s.osc[2].level <= 1.0f);
  1255. for (const auto& o : s.osc)
  1256. CHECK (o.octave >= -2 && o.octave <= 2 && std::abs (o.semitones) <= 11 && std::abs (o.pan) <= 1.0f);
  1257. }
  1258. }
  1259. for (auto& pat : p.patterns)
  1260. if (! pat.notes.empty())
  1261. {
  1262. ++withNotes;
  1263. break;
  1264. }
  1265. for (auto& a : p.arrangements)
  1266. for (auto& item : a.items)
  1267. CHECK (item.track >= 0 && item.track < 500);
  1268. for (auto& clip : song.playlist)
  1269. {
  1270. CHECK (clip.pattern >= 0 && clip.pattern < (int) song.patterns.size());
  1271. CHECK (clip.length > 0 && clip.offset >= 0);
  1272. if (clip.kind == lynx::ClipKind::automation)
  1273. CHECK (clip.automation >= 0 && clip.automation < (int) song.automations.size());
  1274. }
  1275. for (auto& a : song.automations)
  1276. {
  1277. ++automations;
  1278. automationsWithTarget += a.target.kind != lynx::AutomationTargetKind::none;
  1279. CHECK (a.isLive() || ! a.points.empty());
  1280. for (std::size_t i = 1; i < a.points.size(); ++i)
  1281. CHECK (a.points[i].tick >= a.points[i - 1].tick);
  1282. }
  1283. // imported routing has no cycles and valid destinations
  1284. CHECK (song.mixerOrder().size() + 1 == song.mixer.size());
  1285. for (auto& t : song.mixer)
  1286. for (auto& s : t.sends)
  1287. {
  1288. ++sends;
  1289. CHECK (s.target > 0 && s.target < (int) song.mixer.size());
  1290. }
  1291. if (! song.playlist.empty())
  1292. ++withPlaylist;
  1293. }
  1294. catch (const flp::Error& e)
  1295. {
  1296. ++failures;
  1297. std::printf (" FAILED %s: %s\n", entry.path().string().c_str(), e.what());
  1298. }
  1299. }
  1300. CHECK (threeOscDecoded == threeOsc);
  1301. std::printf (" Harmless: %d channels, %d decoded\n", harmless, harmlessDecoded);
  1302. std::printf (" Sytrus: %d channels, %d decoded, %d by the engine (%d of %d rendered sound)\n", sytrus, sytrusDecoded, sytrusEngine,
  1303. sytrusSounding, sytrusRendered);
  1304. CHECK (sytrusEngine == sytrus);
  1305. CHECK (sytrusSounding >= sytrusRendered * 3 / 4);
  1306. for (const auto& [name, counter] : generators)
  1307. {
  1308. std::printf (" %s: %d channels, %d decoded\n", name.c_str(), counter.first, counter.second);
  1309. CHECK (counter.second == counter.first);
  1310. }
  1311. std::printf (" Ogun spectra matching the Trumpet reference: %d\n", ogunTrumpet);
  1312. CHECK (ogunTrumpet >= 1);
  1313. // demo Gross Beat: the volume slot becomes the gate (those using an envelope slot get steps < 1)
  1314. {
  1315. int vocodexes = 0, vocodexSides = 0;
  1316. int lovePhilterLinks[4] {}; // live links: cutoff, resonance; inside containers: cutoff, resonance
  1317. int embeddedZones = 0, lovePhilters = 0, lovePhiltersRead = 0, lovePhiltersConverted = 0, gates = 0, shaped = 0, effectors = 0, patchers = 0, serial = 0, parallel = 0, generatorPatchers = 0;
  1318. for (const auto& entry : std::filesystem::recursive_directory_iterator (std::filesystem::path (realFlpDir) / "Demo projects"))
  1319. if (entry.path().extension() == ".flp")
  1320. {
  1321. std::ifstream in (entry.path(), std::ios::binary);
  1322. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  1323. try
  1324. {
  1325. const auto project = flp::parseProject (file);
  1326. for (const auto& insert : project.mixer)
  1327. for (const auto& slot : insert.slots)
  1328. if (slot.plugin == "Gross Beat")
  1329. {
  1330. const auto fx = lynx::effectFromFl (slot.plugin, slot.state);
  1331. CHECK (fx && fx->kind == lynx::EffectKind::volumeGate);
  1332. ++gates;
  1333. if (fx && std::any_of (fx->params.begin(), fx->params.begin() + 32, [] (float v) { return v < 0.99f; }))
  1334. ++shaped;
  1335. }
  1336. else if (slot.plugin == "Fruity Love Philter")
  1337. {
  1338. ++lovePhilters;
  1339. lovePhiltersRead += lynx::parseLovePhilter (slot.state).has_value();
  1340. if (const auto conv = lynx::lovePhilterEffects (slot.state))
  1341. {
  1342. ++lovePhiltersConverted;
  1343. CHECK (! conv->slots.empty());
  1344. for (const auto& fx : conv->slots)
  1345. for (auto v : fx.params)
  1346. CHECK (std::isfinite (v) && v >= 0.0f && v <= 1.0f);
  1347. for (const auto& fx : conv->slots)
  1348. for (const auto& link : fx.links)
  1349. {
  1350. const auto& chain = fx.branches[(std::size_t) link.branch].chain;
  1351. CHECK (link.source < (int) chain.size() && chain[(std::size_t) link.target].kind == lynx::EffectKind::parametricEq);
  1352. lovePhilterLinks[2 + (link.param == lynx::peq::param (0, 3) ? 1 : 0)]++;
  1353. }
  1354. for (const auto& link : conv->links)
  1355. {
  1356. lovePhilterLinks[link.param == lynx::peq::param (0, 3) ? 1 : 0]++;
  1357. CHECK (link.curve.size() == 33);
  1358. for (auto v : link.curve)
  1359. CHECK (std::isfinite (v) && v >= 0.0f && v <= 1.0f);
  1360. }
  1361. }
  1362. }
  1363. else if (slot.plugin == "Vocodex")
  1364. {
  1365. const auto v = lynx::vocodexEffect (slot.state);
  1366. CHECK (v.has_value());
  1367. vocodexes += v.has_value();
  1368. vocodexSides += v && (v->modIn > 0 || v->carIn > 0);
  1369. }
  1370. else if (slot.plugin == "Effector")
  1371. effectors += lynx::effectFromFl (slot.plugin, slot.state).has_value();
  1372. else if (slot.plugin == "Patcher")
  1373. {
  1374. const auto graph = lynx::parsePatcher (slot.state);
  1375. CHECK (graph.has_value());
  1376. ++patchers;
  1377. if (graph && ! graph->serialChain.empty())
  1378. ++serial;
  1379. if (graph && graph->serialChain.empty() && graph->audio)
  1380. ++parallel;
  1381. }
  1382. for (const auto& c : project.channels)
  1383. if (c.pluginName == "DirectWave")
  1384. if (const auto program = lynx::directWaveProgram (c.pluginState))
  1385. for (const auto& wav : program->embeddedWav)
  1386. if (! wav.empty())
  1387. {
  1388. ++embeddedZones;
  1389. CHECK (wav.size() > 44 && std::memcmp (wav.data(), "RIFF", 4) == 0);
  1390. }
  1391. for (const auto& c : project.channels)
  1392. if (c.pluginName == "Patcher")
  1393. {
  1394. const auto graph = lynx::parsePatcher (c.pluginState);
  1395. CHECK (graph.has_value());
  1396. generatorPatchers += graph && graph->mainGenerator >= 0;
  1397. }
  1398. }
  1399. catch (const flp::Error&) {}
  1400. }
  1401. std::printf (" Gross Beat: %d slots, %d with gate; Effector: %d converted; Love Philter: %d, %d read\n", gates, shaped, effectors,
  1402. lovePhilters, lovePhiltersRead);
  1403. CHECK (lovePhiltersRead == lovePhilters && lovePhiltersConverted == lovePhilters);
  1404. std::printf (" Vocodex: %d converted, %d with side inputs\n", vocodexes, vocodexSides);
  1405. CHECK (vocodexes >= 9);
  1406. std::printf (" Love Philter live links: %d cutoff, %d resonance; in multi-unit containers: %d cutoff, %d resonance\n",
  1407. lovePhilterLinks[0], lovePhilterLinks[1], lovePhilterLinks[2], lovePhilterLinks[3]);
  1408. CHECK (lovePhilterLinks[2] + lovePhilterLinks[3] > 0);
  1409. std::printf (" DirectWave: %d zones with embedded audio\n", embeddedZones);
  1410. CHECK (embeddedZones == 12);
  1411. std::printf (" Patcher: %d in the mixer (%d serial, %d reduced to serial/parallel), %d as instrument with generator\n", patchers, serial,
  1412. parallel, generatorPatchers);
  1413. CHECK (gates == 0 || shaped > 0);
  1414. }
  1415. // Envelope Controller "Basic Sidechain" (Future House): drops on the note and returns to ~1 within ~1/3 s
  1416. {
  1417. const auto path = std::filesystem::path (realFlpDir) / "Demo projects" / "Demo songs" / "Asher Postman - Future House.flp";
  1418. if (std::filesystem::exists (path))
  1419. {
  1420. std::ifstream in (path, std::ios::binary);
  1421. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  1422. const auto song = lynx::importFlp (flp::parseProject (file));
  1423. const lynx::ChannelDef* controller = nullptr;
  1424. for (const auto& c : song.channels)
  1425. if (c.instrument == lynx::InstrumentKind::controller)
  1426. controller = &c;
  1427. CHECK (controller != nullptr);
  1428. if (controller != nullptr)
  1429. {
  1430. auto inst = lynx::makeInstrument (lynx::InstrumentKind::controller);
  1431. inst->prepare (48000.0);
  1432. inst->configure (*controller, song.bpm);
  1433. std::vector<float> l (480), r (480);
  1434. inst->render (l.data(), r.data(), 480, 1.0f, 1.0f);
  1435. inst->noteOn (60, 1.0f);
  1436. inst->render (l.data(), r.data(), 480, 1.0f, 1.0f); // 10 ms
  1437. const auto ducked = inst->controlOutput (0);
  1438. for (int i = 0; i < 100; ++i) // +1 s
  1439. inst->render (l.data(), r.data(), 480, 1.0f, 1.0f);
  1440. const auto recovered = inst->controlOutput (0);
  1441. std::printf (" Envelope Controller (Basic Sidechain): %.2f on the note, %.2f after 1 s\n", ducked, recovered);
  1442. CHECK (ducked < 0.3f && recovered > 0.9f);
  1443. }
  1444. }
  1445. }
  1446. // mutation fuzz: real states with flipped and truncated bytes must not crash the readers
  1447. {
  1448. std::map<std::string, std::vector<std::uint8_t>> samples; // one real blob per plugin
  1449. for (const auto& entry : std::filesystem::recursive_directory_iterator (std::filesystem::path (realFlpDir) / "Demo projects"))
  1450. {
  1451. if (entry.path().extension() != ".flp")
  1452. continue;
  1453. std::ifstream in (entry.path(), std::ios::binary);
  1454. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  1455. try
  1456. {
  1457. const auto project = flp::parseProject (file);
  1458. for (const auto& c : project.channels)
  1459. if (! c.pluginState.empty())
  1460. samples.emplace (c.pluginName, c.pluginState);
  1461. for (const auto& insert : project.mixer)
  1462. for (const auto& slot : insert.slots)
  1463. if (! slot.state.empty())
  1464. samples.emplace (slot.plugin, slot.state);
  1465. }
  1466. catch (const flp::Error&) {}
  1467. }
  1468. std::uint32_t seed = 12345;
  1469. auto rnd = [&seed] { seed = seed * 1664525u + 1013904223u; return seed >> 8; };
  1470. int mutations = 0;
  1471. for (const auto& [name, original] : samples)
  1472. for (int round = 0; round < 40; ++round)
  1473. {
  1474. auto blob = original;
  1475. if (round % 4 == 3 && ! blob.empty())
  1476. blob.resize (rnd() % blob.size()); // truncated
  1477. else
  1478. for (int k = 0; k < 1 + (int) (rnd() % 8) && ! blob.empty(); ++k)
  1479. blob[rnd() % blob.size()] = (std::uint8_t) rnd();
  1480. ++mutations;
  1481. lynx::SynthSettings synth;
  1482. float gain = 1.0f;
  1483. lynx::synthFromSytrus (blob, 120.0, synth, gain);
  1484. lynx::synthFromHarmor (blob, synth, gain);
  1485. lynx::synthFromGms (blob, 120.0, synth, gain);
  1486. lynx::synthFromOgun (blob, synth, gain);
  1487. lynx::synthFromMorphine (blob, synth, gain);
  1488. lynx::synthFromKepler (blob, synth, gain);
  1489. lynx::synthFromPoiZone (blob, synth, gain);
  1490. lynx::synthFromToxic (blob, synth, gain);
  1491. lynx::synthFromBassDrum (blob, synth, gain);
  1492. lynx::synthFromHarmless (blob, 120.0, synth, gain);
  1493. lynx::harmlessParams (blob);
  1494. if (lynx::harmorStateUsable (blob))
  1495. lynx::harmorFallbacks (blob);
  1496. lynx::parseEnvelopeController (blob);
  1497. lynx::parseKeyboardController (blob);
  1498. lynx::parseLovePhilter (blob);
  1499. lynx::parsePatcher (blob);
  1500. lynx::effectFromFl (name, blob);
  1501. lynx::fpcPads (blob);
  1502. lynx::directWaveProgram (blob);
  1503. CHECK (std::isfinite (gain) && std::isfinite (synth.cutoff));
  1504. }
  1505. std::printf (" mutation fuzz: %d blobs from %d plugins\n", mutations, (int) samples.size());
  1506. }
  1507. // WavPack (.wv) samples from FL packs: all decode, with finite, non-silent audio
  1508. {
  1509. int files = 0, decoded = 0, audible = 0;
  1510. double seconds = 0.0;
  1511. for (const auto& entry : std::filesystem::recursive_directory_iterator (std::filesystem::path (realFlpDir) / "Patches" / "Packs"))
  1512. if (entry.path().extension() == ".wv")
  1513. {
  1514. std::ifstream in (entry.path(), std::ios::binary);
  1515. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  1516. ++files;
  1517. lynx::DecodedAudio audio;
  1518. if (! lynx::decodeWavPack (file.data(), file.size(), audio))
  1519. continue;
  1520. ++decoded;
  1521. float peak = 0.0f;
  1522. bool finite = true;
  1523. for (const auto& ch : audio.channels)
  1524. for (auto v : ch)
  1525. {
  1526. finite = finite && std::isfinite (v);
  1527. peak = std::max (peak, std::abs (v));
  1528. }
  1529. CHECK (finite && peak <= 4.0f);
  1530. audible += peak > 1e-4f;
  1531. seconds += (double) audio.channels[0].size() / audio.sampleRate;
  1532. }
  1533. std::printf (" WavPack: %d .wv files, %d decoded, %d audible, %.0f s of audio\n", files, decoded, audible, seconds);
  1534. CHECK (decoded == files);
  1535. }
  1536. // native GMS presets (.gmsynth = the contents of 0xD5 without the "SSLF")
  1537. int gmsPresets = 0, gmsDecoded = 0;
  1538. const auto gmsFolder = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "GMS";
  1539. if (std::filesystem::exists (gmsFolder))
  1540. for (const auto& entry : std::filesystem::recursive_directory_iterator (gmsFolder))
  1541. if (entry.path().extension() == ".gmsynth")
  1542. {
  1543. std::ifstream in (entry.path(), std::ios::binary);
  1544. std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  1545. std::vector<std::uint8_t> blob { 'S', 'S', 'L', 'F' };
  1546. const auto n = (std::uint32_t) file.size();
  1547. blob.insert (blob.end(), reinterpret_cast<const std::uint8_t*> (&n), reinterpret_cast<const std::uint8_t*> (&n) + 4);
  1548. blob.insert (blob.end(), file.begin(), file.end());
  1549. lynx::SynthSettings gs;
  1550. float g = 1.0f;
  1551. ++gmsPresets;
  1552. if (lynx::synthFromGms (blob, 120.0, gs, g))
  1553. {
  1554. ++gmsDecoded;
  1555. CHECK (std::isfinite (g) && gs.cutoff >= 20.0f && std::isfinite (gs.pitchEnv.amount) && std::isfinite (gs.lfo[lynx::lfoPitch].period));
  1556. }
  1557. }
  1558. std::printf (" GMS presets: %d, %d decoded\n", gmsPresets, gmsDecoded);
  1559. // the synth's "FL presets" button: Sytrus/Harmor .fst and .gmsynth become synth settings
  1560. int presetFiles = 0, presetsRead = 0;
  1561. for (const auto* name : { "Sytrus", "Harmor", "GMS", "Harmless", "Ogun", "Sakura", "Fruit Kick", "Fruity DX10", "MiniSynth", "Kepler",
  1562. "PoiZone", "Toxic Biohazard", "BassDrum" })
  1563. {
  1564. const auto folder = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / name;
  1565. if (! std::filesystem::exists (folder))
  1566. continue;
  1567. for (const auto& entry : std::filesystem::recursive_directory_iterator (folder))
  1568. if (entry.path().extension() == ".fst" || entry.path().extension() == ".gmsynth")
  1569. {
  1570. std::ifstream in (entry.path(), std::ios::binary);
  1571. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  1572. ++presetFiles;
  1573. presetsRead += lynx::synthFromFlPreset (file).has_value();
  1574. }
  1575. }
  1576. std::printf (" FL synth presets: %d files, %d read\n", presetFiles, presetsRead);
  1577. CHECK (presetsRead >= presetFiles * 95 / 100);
  1578. CHECK (gmsDecoded == gmsPresets);
  1579. CHECK (sytrusDecoded == sytrus);
  1580. CHECK (harmlessDecoded == harmless && harmless > 100);
  1581. std::printf (" Harmless: %d states parsed for the engine, %d channels imported as Harmless\n", harmlessParsed, harmlessKind);
  1582. CHECK (harmlessParsed == harmless && harmlessKind >= harmless);
  1583. std::printf (" Harmor: %d channels, %d states usable by the engine, %d imported as Harmor (%d sounding)\n", harmor, harmorUsable, harmorKind, harmorSounding);
  1584. CHECK (harmorUsable == harmor && harmor > 90 && harmorKind >= harmor && harmorSounding >= harmorKind * 9 / 10);
  1585. std::printf (" GMS: %d channels, %d usable, %d imported as GMS (%d sounding); Morphine: %d channels, %d usable, %d imported (%d sounding); %d demo goldens\n",
  1586. gms, gmsUsable, gmsKind, gmsSounding, morphine, morphineUsable, morphineKind, morphineSounding, demoGoldens);
  1587. CHECK (gmsUsable == gms && gms >= 9 && gmsKind == gms && gmsSounding == gmsKind);
  1588. std::printf (" MiniSynth channels: %d, %d usable, %d on the exact engine, %d sounding, %d golden\n", miniSynth, miniSynthUsable, miniSynthKind,
  1589. miniSynthSounding, miniSynthGoldens);
  1590. CHECK (miniSynthUsable == miniSynth && miniSynth >= 12 && miniSynthKind == miniSynth && miniSynthSounding == miniSynthKind && miniSynthGoldens == 2);
  1591. CHECK (morphineUsable == morphine && morphine >= 7 && morphineKind == morphine && morphineSounding == morphineKind && demoGoldens == 2);
  1592. std::printf (" PoiZone: %d channels, %d usable, %d imported as PoiZone (%d sounding), %d demo golden\n", poizone, poizoneUsable, poizoneKind,
  1593. poizoneSounding, poizoneGoldens);
  1594. CHECK (poizoneUsable == poizone && poizone >= 2 && poizoneKind == poizone && poizoneSounding == poizoneKind && poizoneGoldens == 1);
  1595. std::printf (" Toxic Biohazard: %d channels, %d usable, %d imported as Toxic (%d sounding), %d golden\n", toxic, toxicUsable, toxicKind,
  1596. toxicSounding, toxicGoldens);
  1597. CHECK (toxicUsable == toxic && toxic >= 3 && toxicKind == toxic && toxicSounding == toxicKind && toxicGoldens == 1);
  1598. std::printf (" Ogun: %d channels, %d states usable by the engine, %d imported as Ogun (%d sounding)\n", ogun, ogunUsable, ogunKind, ogunSounding);
  1599. CHECK (ogunUsable == ogun && ogun >= 20 && ogunKind >= ogun && ogunSounding >= ogunKind * 9 / 10);
  1600. std::printf (" Fruity DX10: %d channels, %d states usable by the engine, %d imported as DX10 (%d sounding)\n", dx10, dx10Usable, dx10Kind, dx10Sounding);
  1601. CHECK (dx10Usable == dx10 && dx10 >= 12 && dx10Kind == dx10 && dx10Sounding == dx10Kind);
  1602. std::printf (" Kepler: %d channels, %d states usable by the engine, %d imported as Kepler (%d sounding)\n", kepler, keplerUsable, keplerKind, keplerSounding);
  1603. CHECK (keplerUsable == kepler && kepler >= 4 && keplerSounding == keplerKind && (keplerKind == 0 || keplerKind == kepler));
  1604. std::printf (" Harmless rack compressor: %d of %d presets read from the installation\n", harmlessCompLoaded, harmlessCompWanted);
  1605. CHECK (harmlessCompLoaded == harmlessCompWanted && harmlessCompWanted > 100);
  1606. std::printf (" Maximus: %d of %d slots on the exact engine; Soundgoodizer: %d of %d; Vocodex with SG.dat: %d of %d\n", maximusExact,
  1607. maximusSlots, sgzExact, sgzSlots, vocodexWithData, vocodexSlots);
  1608. // REA_rodada40 §6: in Demo projects the 4 states left on the approximation use the Frequency Splitter linear-phase
  1609. // option; the other Data folders add states with options not decoded
  1610. CHECK (maximusSlots - maximusExact <= 16 && maximusExact >= 97 && sgzExact == sgzSlots && sgzSlots > 90);
  1611. CHECK (vocodexWithData == vocodexSlots && vocodexSlots >= 10);
  1612. std::printf (" Fruity WaveShaper: %d of %d slots on the exact engine; Fruity Soft Clipper: %d of %d\n", shaperExact, shaperSlots,
  1613. clipperExact, clipperSlots);
  1614. // (Patcher graphs add WaveShaper nodes of their own to the imported mixer)
  1615. CHECK (shaperExact >= shaperSlots && shaperSlots >= 70 && clipperExact >= clipperSlots && clipperSlots >= 25); // REA_rodada51
  1616. std::printf (" Chorus family: %d of %d slots on the exact engines\n", chorusExact, chorusSlots);
  1617. CHECK (chorusExact >= chorusSlots && chorusSlots >= 47); // REA_rodada59
  1618. // 3x Osc channel presets shipped with FL (.fst): all become synth settings
  1619. int presets = 0, presetsDecoded = 0;
  1620. const auto presetDir = std::filesystem::path (realFlpDir) / "Patches" / "Channel presets" / "3x Osc";
  1621. if (std::filesystem::is_directory (presetDir))
  1622. for (auto& entry : std::filesystem::directory_iterator (presetDir))
  1623. if (entry.path().extension() == ".fst")
  1624. {
  1625. ++presets;
  1626. std::ifstream in (entry.path(), std::ios::binary);
  1627. std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  1628. if (auto synth = lynx::synthFromFlPreset (bytes))
  1629. {
  1630. ++presetsDecoded;
  1631. CHECK (synth->osc[0].level + synth->osc[1].level + (synth->am ? 0.0f : synth->osc[2].level) <= 1.001f);
  1632. }
  1633. }
  1634. std::printf (" %d 3x Osc presets, %d read\n", presets, presetsDecoded);
  1635. CHECK (presetsDecoded == presets);
  1636. // FL effect presets that become built-in effects
  1637. int fxPresets = 0, fxDecoded = 0;
  1638. for (auto kind : { lynx::EffectKind::parametricEq, lynx::EffectKind::limiter, lynx::EffectKind::reverb, lynx::EffectKind::delay,
  1639. lynx::EffectKind::utility })
  1640. for (const auto& plugin : lynx::flPluginsForEffect (kind))
  1641. {
  1642. const auto dir = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Effects" / plugin;
  1643. if (! std::filesystem::is_directory (dir))
  1644. continue;
  1645. for (auto& entry : std::filesystem::recursive_directory_iterator (dir))
  1646. if (entry.path().extension() == ".fst")
  1647. {
  1648. ++fxPresets;
  1649. std::ifstream in (entry.path(), std::ios::binary);
  1650. std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  1651. fxDecoded += lynx::effectFromFlPreset (bytes).has_value();
  1652. }
  1653. }
  1654. std::printf (" %d FL effect presets, %d read\n", fxPresets, fxDecoded);
  1655. CHECK (fxDecoded == fxPresets);
  1656. CHECK (wrappedParsed == wrapped);
  1657. std::printf (" %d plugins in Fruity Wrapper, %d read\n", wrapped, wrappedParsed);
  1658. std::printf (" %d markers, %d time signature changes, %d tracks with EQ\n", markers, signatures, trackEqs);
  1659. std::printf (" %d parametric EQs (%d with FL settings), %d reverbs (%d without dry)\n", peqSlots, peqShaped, reverbs, reverbsDryless);
  1660. std::printf (" DirectWave: %d channels, %d zones; FPC: %d projects, %d pad channels, %d pad notes\n", dwChannels, dwZones, fpcKits,
  1661. padChannels, padNotes);
  1662. CHECK (dwZones > 1000 && padChannels > 250 && padNotes > 500);
  1663. std::printf (" sliced channels (Slicer/Slicex): %d; embedded WAVs saved: %d\n", slicedChannels, embeddedSaved);
  1664. CHECK (slicedChannels >= 25 && embeddedSaved >= 10); // without decoding Ogg here, some loops have no duration
  1665. std::printf (" FL Keys (Grand Piano, 30 zones): %d channels\n", flKeysChannels);
  1666. CHECK (flKeysChannels >= 8);
  1667. CHECK (peqSlots > 1000 && peqShaped > 900 && reverbsDryless > 20 && reverbsLowCut > 250);
  1668. CHECK (markers > 300 && signatures > 60);
  1669. std::printf (" %d 3x Osc channels, %d decoded\n", threeOsc, threeOscDecoded);
  1670. std::printf (" %d .flp files read, %d with notes, %d with playlist, %d automations (%d with target), %d sends\n",
  1671. files, withNotes, withPlaylist, automations, automationsWithTarget, sends);
  1672. CHECK (files > 0);
  1673. CHECK (automations > 0 && sends > 0);
  1674. }
  1675. // ---------------------------------------------------------------------------
  1676. // Sequencer
  1677. TEST (pattern_toggle_step_and_length)
  1678. {
  1679. lynx::Pattern p;
  1680. CHECK (! p.hasStep (0, 4));
  1681. p.toggleStep (0, 4);
  1682. CHECK (p.hasStep (0, 4));
  1683. CHECK (p.notes.size() == 1 && p.notes[0].start == 4 * lynx::ticksPerStep);
  1684. CHECK (p.effectiveLength (4) == 4 * lynx::ppq); // one bar
  1685. p.toggleStep (1, 20); // second bar
  1686. CHECK (p.effectiveLength (4) == 8 * lynx::ppq);
  1687. p.toggleStep (0, 4);
  1688. CHECK (! p.hasStep (0, 4) && p.notes.size() == 1);
  1689. }
  1690. TEST (scheduler_places_events_at_exact_samples)
  1691. {
  1692. lynx::Pattern p;
  1693. p.toggleStep (0, 0);
  1694. p.toggleStep (0, 4); // tick 960 = 1 beat
  1695. const double sr = 48000.0, bpm = 120.0; // 1 beat = 0.5 s = 24000 samples
  1696. const auto tps = lynx::ticksPerSample (bpm, sr);
  1697. lynx::EventBuffer ev;
  1698. lynx::schedulePatternLoop (p, p.effectiveLength (4), 23900.0 * tps, 512, tps, ev);
  1699. // the beat-2 note starts at sample 24000 -> offset 100 in this block
  1700. int ons = 0;
  1701. for (auto& e : ev)
  1702. if (e.type == lynx::NoteEvent::Type::on)
  1703. {
  1704. ++ons;
  1705. CHECK (e.sampleOffset == 100);
  1706. }
  1707. CHECK (ons == 1);
  1708. }
  1709. TEST (scheduler_wraps_the_loop)
  1710. {
  1711. lynx::Pattern p;
  1712. p.toggleStep (2, 0);
  1713. const auto len = p.effectiveLength (4); // 3840 ticks
  1714. const double sr = 48000.0;
  1715. const auto tps = lynx::ticksPerSample (120.0, sr);
  1716. lynx::EventBuffer ev;
  1717. // block that crosses the loop end: starts 10 samples before
  1718. const auto start = (double) len - 10.0 * tps;
  1719. lynx::schedulePatternLoop (p, len, start, 64, tps, ev);
  1720. bool found = false;
  1721. for (auto& e : ev)
  1722. if (e.type == lynx::NoteEvent::Type::on && e.channel == 2)
  1723. {
  1724. found = true;
  1725. CHECK (e.sampleOffset == 10);
  1726. }
  1727. CHECK (found);
  1728. }
  1729. TEST (scheduler_off_before_on_at_same_sample)
  1730. {
  1731. lynx::Pattern p;
  1732. lynx::Note a;
  1733. a.start = 0; a.length = 240; a.channel = 0;
  1734. lynx::Note b = a;
  1735. b.start = 240;
  1736. p.notes = { a, b };
  1737. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  1738. lynx::EventBuffer ev;
  1739. lynx::schedulePatternLoop (p, p.effectiveLength (4), 200.0, 4000, tps, ev);
  1740. CHECK (ev.size() >= 2);
  1741. CHECK (ev[0].type == lynx::NoteEvent::Type::off);
  1742. CHECK (ev[1].type == lynx::NoteEvent::Type::on);
  1743. CHECK (ev[0].sampleOffset == ev[1].sampleOffset);
  1744. }
  1745. TEST (snapshot_exchange_hands_over_and_recycles)
  1746. {
  1747. struct Counted
  1748. {
  1749. int value;
  1750. int* alive;
  1751. Counted (int v, int* a) : value (v), alive (a) { ++*alive; }
  1752. ~Counted() { --*alive; }
  1753. };
  1754. int alive = 0;
  1755. {
  1756. lynx::SnapshotExchange<Counted> x;
  1757. CHECK (x.acquire() == nullptr);
  1758. x.publish (std::make_unique<Counted> (1, &alive));
  1759. CHECK (x.acquire()->value == 1);
  1760. x.publish (std::make_unique<Counted> (2, &alive));
  1761. x.publish (std::make_unique<Counted> (3, &alive)); // 2 was never read: freed immediately
  1762. CHECK (alive == 2);
  1763. CHECK (x.acquire()->value == 3);
  1764. CHECK (alive == 2); // 1 was returned, but only the UI frees it
  1765. x.collectGarbage();
  1766. CHECK (alive == 1);
  1767. // without collection, the return slots fill up and the audio side holds on to the current one
  1768. for (int i = 0; i < 20; ++i)
  1769. {
  1770. x.publish (std::make_unique<Counted> (10 + i, &alive));
  1771. CHECK (x.acquire() != nullptr);
  1772. }
  1773. }
  1774. CHECK (alive == 0);
  1775. }
  1776. TEST (rack_renderer_makes_sound_and_reuses_instruments)
  1777. {
  1778. auto song = lynx::Song::makeDefault();
  1779. song.bpm = 120.0;
  1780. const double sr = 48000.0;
  1781. auto state = lynx::RackState::build (song, nullptr, sr);
  1782. lynx::RackRenderer r;
  1783. r.prepare (sr);
  1784. std::vector<float> l (512), rr (512);
  1785. double tick = 0.0;
  1786. float peak = 0.0f;
  1787. int kicks = 0;
  1788. for (int block = 0; block < 93; ++block) // 47616 samples: just before 1 s
  1789. {
  1790. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  1791. for (auto& e : r.lastEvents())
  1792. if (e.type == lynx::NoteEvent::Type::on && e.channel == 0)
  1793. ++kicks;
  1794. for (int i = 0; i < 512; ++i)
  1795. {
  1796. CHECK (std::isfinite (l[(std::size_t) i]));
  1797. peak = std::max (peak, std::abs (l[(std::size_t) i]));
  1798. }
  1799. tick += 512 * lynx::ticksPerSample (song.bpm, sr);
  1800. }
  1801. CHECK (peak > 0.1f && peak < 4.0f);
  1802. CHECK (kicks == 2); // 120 BPM: beats at 0 and 0.5 s
  1803. // editing the song keeps the instruments of channels whose type didn't change
  1804. song.channels[4].instrument = lynx::InstrumentKind::kick;
  1805. auto next = lynx::RackState::build (song, state.get(), sr);
  1806. CHECK (next->instruments[0] == state->instruments[0]);
  1807. CHECK (next->instruments[4] != state->instruments[4]);
  1808. // stopping silences the synth and triggers no new notes
  1809. r.process (next.get(), false, tick, 512, l.data(), rr.data());
  1810. CHECK (r.lastEvents().size() == 0);
  1811. }
  1812. TEST (flp_import_maps_channels_and_rescales)
  1813. {
  1814. flp::Project p;
  1815. p.header.ppq = 96;
  1816. p.tempo = 150.0;
  1817. p.channels = { { 0, flp::ChannelType::sampler, "Kick 808", "", "", 1 },
  1818. { 1, flp::ChannelType::audioClip, "Vocal", "", "", 2 },
  1819. { 2, flp::ChannelType::instrument, "Lead", "FLEX", "", 3 } };
  1820. flp::Pattern pat;
  1821. pat.index = 1;
  1822. flp::Note n;
  1823. n.position = 96;
  1824. n.length = 0;
  1825. n.channel = 2;
  1826. n.key = 72;
  1827. n.velocity = 64;
  1828. pat.notes.push_back (n);
  1829. p.patterns.push_back (pat);
  1830. const auto s = lynx::importFlp (p);
  1831. CHECK (s.bpm == 150.0);
  1832. CHECK (s.channels.size() == 3); // audio clip channels stay in the rack, as in FL
  1833. CHECK (s.channels[0].instrument == lynx::InstrumentKind::kick);
  1834. CHECK (s.channels[1].instrument == lynx::InstrumentKind::audioClip);
  1835. CHECK (s.channels[2].instrument == lynx::InstrumentKind::synth);
  1836. CHECK (s.patterns.size() == 1);
  1837. CHECK (s.patterns[0].notes[0].start == lynx::ppq); // 96 @ PPQ 96 = 1 beat
  1838. CHECK (s.patterns[0].notes[0].channel == 2);
  1839. CHECK (s.patterns[0].notes[0].length == lynx::ticksPerStep); // step note
  1840. CHECK (std::abs (s.patterns[0].notes[0].velocity - 0.5f) < 1e-6f);
  1841. }
  1842. TEST (live_queue_is_fifo_and_bounded)
  1843. {
  1844. lynx::LiveEventQueue q;
  1845. lynx::NoteEvent e;
  1846. CHECK (! q.pop (e));
  1847. for (int i = 0; i < 10; ++i)
  1848. {
  1849. lynx::NoteEvent in;
  1850. in.key = i;
  1851. CHECK (q.push (in));
  1852. }
  1853. for (int i = 0; i < 10; ++i)
  1854. CHECK (q.pop (e) && e.key == i);
  1855. int pushed = 0;
  1856. while (q.push (e))
  1857. ++pushed;
  1858. CHECK (pushed == (int) lynx::LiveEventQueue::capacity - 1); // one slot stays free to tell full from empty
  1859. }
  1860. TEST (sampler_plays_pitched_one_shot)
  1861. {
  1862. // 1000-frame ramp sample: the position can be measured from the amplitude
  1863. auto data = std::make_shared<lynx::SampleData>();
  1864. data->sampleRate = 48000.0;
  1865. for (int i = 0; i < 1000; ++i)
  1866. data->left.push_back (1.0f);
  1867. auto sampler = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  1868. sampler->prepare (48000.0);
  1869. std::vector<float> l (2048, 0.0f), r (2048, 0.0f);
  1870. sampler->noteOn (60, 1.0f);
  1871. sampler->noteOff (60); // one-shot: noteOff doesn't cut
  1872. sampler->render (l.data(), r.data(), 2048, 1.0f, 1.0f);
  1873. int sounding = 0;
  1874. for (auto s : l)
  1875. sounding += s > 0.5f;
  1876. CHECK (sounding >= 995 && sounding <= 1000); // plays the whole sample at the original pitch
  1877. std::fill (l.begin(), l.end(), 0.0f);
  1878. sampler->noteOn (72, 1.0f); // one octave up: half the duration
  1879. sampler->render (l.data(), r.data(), 2048, 1.0f, 1.0f);
  1880. sounding = 0;
  1881. for (auto s : l)
  1882. sounding += s > 0.5f;
  1883. CHECK (sounding >= 495 && sounding <= 500);
  1884. CHECK (r[10] > 0.5f); // mono goes to both sides
  1885. auto silent = lynx::makeInstrument (lynx::InstrumentKind::sampler, nullptr);
  1886. silent->prepare (48000.0);
  1887. silent->noteOn (60, 1.0f);
  1888. std::fill (l.begin(), l.end(), 0.0f);
  1889. silent->render (l.data(), r.data(), 64, 1.0f, 1.0f);
  1890. CHECK (l[0] == 0.0f);
  1891. }
  1892. TEST (rack_live_notes_and_sample_provider)
  1893. {
  1894. lynx::Song song;
  1895. lynx::ChannelDef c;
  1896. c.name = "Smp";
  1897. c.instrument = lynx::InstrumentKind::sampler;
  1898. c.samplePath = "a.wav";
  1899. song.channels.push_back (c);
  1900. song.patterns.emplace_back();
  1901. int loads = 0;
  1902. auto provider = [&] (const std::string& path) -> std::shared_ptr<const lynx::SampleData> {
  1903. ++loads;
  1904. CHECK (path == "a.wav");
  1905. auto d = std::make_shared<lynx::SampleData>();
  1906. d->left.assign (4800, 0.5f);
  1907. return d;
  1908. };
  1909. auto state = lynx::RackState::build (song, nullptr, 48000.0, provider);
  1910. CHECK (loads == 1);
  1911. lynx::RackRenderer renderer;
  1912. renderer.prepare (48000.0);
  1913. lynx::LiveEventQueue live;
  1914. lynx::NoteEvent on;
  1915. on.channel = 0;
  1916. on.velocity = 1.0f;
  1917. live.push (on);
  1918. std::vector<float> l (256), r (256);
  1919. renderer.process (state.get(), false, 0.0, 256, l.data(), r.data(), &live); // stopped, but the live note plays
  1920. CHECK (l[0] > 0.1f);
  1921. // same sample: reused; different sample: reloaded
  1922. auto same = lynx::RackState::build (song, state.get(), 48000.0, provider);
  1923. CHECK (loads == 1 && same->instruments[0] == state->instruments[0]);
  1924. song.channels[0].samplePath = "b.wav";
  1925. auto other = lynx::RackState::build (song, state.get(), 48000.0, [&] (const std::string&) { ++loads; return nullptr; });
  1926. CHECK (loads == 2 && other->instruments[0] != state->instruments[0]);
  1927. }
  1928. TEST (flp_paths_resolve_and_samplers_import)
  1929. {
  1930. lynx::FlPathRoots roots { "C:\\FL", "C:\\FL\\Data", "D:\\User" };
  1931. CHECK (lynx::resolveFlPath ("%FLStudioFactoryData%\\Data\\x.wav", roots) == "C:\\FL\\Data\\x.wav");
  1932. CHECK (lynx::resolveFlPath ("%FLStudioData%\\Patches\\y.wav", roots) == "C:\\FL\\Data\\Patches\\y.wav");
  1933. CHECK (lynx::resolveFlPath ("%FLStudioUserData%\\z.wav", roots) == "D:\\User\\z.wav");
  1934. CHECK (lynx::resolveFlPath ("E:\\abs.wav", roots) == "E:\\abs.wav");
  1935. CHECK (lynx::resolveFlPath ("%FLStudioUserData%\\z.wav", {}) == "");
  1936. flp::Project p;
  1937. p.channels = { { 0, flp::ChannelType::sampler, "Kick", "", "%FLStudioData%\\k.wav", 1 },
  1938. { 1, flp::ChannelType::sampler, "Hat", "", "%FLStudioData%\\missing.wav", 1 } };
  1939. lynx::FlpImportOptions options;
  1940. options.roots = roots;
  1941. options.fileExists = [] (const std::string& path) { return path == "C:\\FL\\Data\\k.wav"; };
  1942. const auto s = lynx::importFlp (p, options);
  1943. CHECK (s.channels[0].instrument == lynx::InstrumentKind::sampler);
  1944. CHECK (s.channels[0].samplePath == "C:\\FL\\Data\\k.wav");
  1945. CHECK (s.channels[1].instrument == lynx::InstrumentKind::hat); // no file: built-in instrument
  1946. }
  1947. TEST (soft_clip_is_transparent_below_knee_and_bounded)
  1948. {
  1949. float s[] = { 0.5f, -0.89f, 1.5f, -3.0f, 0.95f };
  1950. lynx::softClip (s, 5);
  1951. CHECK (s[0] == 0.5f && s[1] == -0.89f);
  1952. CHECK (s[2] > 0.9f && s[2] <= 1.0f);
  1953. CHECK (s[3] < -0.9f && s[3] >= -1.0f);
  1954. CHECK (s[4] > 0.9f && s[4] < 0.95f);
  1955. }
  1956. TEST (song_scheduler_plays_clips_with_offset_and_cut)
  1957. {
  1958. lynx::Song s;
  1959. s.channels.resize (1);
  1960. auto& p = s.patterns.emplace_back();
  1961. for (int step : { 0, 4, 8, 12 }) // one note per beat
  1962. p.toggleStep (0, step);
  1963. // clip at bar 2 playing only the second half of the pattern (beats 3 and 4)
  1964. lynx::Clip c;
  1965. c.pattern = 0;
  1966. c.start = 4 * lynx::ppq;
  1967. c.length = 2 * lynx::ppq;
  1968. c.offset = 2 * lynx::ppq;
  1969. s.playlist.push_back (c);
  1970. CHECK (s.songLength() == 8 * lynx::ppq);
  1971. const double sr = 48000.0;
  1972. const auto tps = lynx::ticksPerSample (120.0, sr); // 1 beat = 24000 samples
  1973. lynx::EventBuffer ev;
  1974. // whole song in a single block (8 beats = 192000 samples)
  1975. lynx::scheduleSong (s, 0.0, 192000, tps, ev);
  1976. std::vector<int> ons;
  1977. for (auto& e : ev)
  1978. if (e.type == lynx::NoteEvent::Type::on)
  1979. ons.push_back (e.sampleOffset);
  1980. CHECK (ons.size() == 2);
  1981. CHECK (ons.size() == 2 && ons[0] == 4 * 24000 && ons[1] == 5 * 24000);
  1982. // crossing the loop: bar 1 is empty, so nothing at the start of the second pass
  1983. ev.clear();
  1984. lynx::scheduleSong (s, 8.0 * lynx::ppq - 100 * tps, 200, tps, ev);
  1985. CHECK (ev.size() == 0);
  1986. // a note longer than the clip is cut at the clip end
  1987. p.notes[3].length = 4 * lynx::ppq;
  1988. ev.clear();
  1989. lynx::scheduleSong (s, 0.0, 192000, tps, ev);
  1990. int lastOff = -1;
  1991. for (auto& e : ev)
  1992. if (e.type == lynx::NoteEvent::Type::off)
  1993. lastOff = std::max (lastOff, e.sampleOffset);
  1994. CHECK (lastOff == 6 * 24000);
  1995. }
  1996. TEST (remove_pattern_fixes_clips)
  1997. {
  1998. lynx::Song s;
  1999. s.patterns.resize (3);
  2000. s.currentPattern = 2;
  2001. s.playlist = { { 0, 0, 0, 100, 0 }, { 1, 0, 100, 100, 0 }, { 2, 1, 0, 100, 0 } };
  2002. s.removePattern (1);
  2003. CHECK (s.patterns.size() == 2);
  2004. CHECK (s.playlist.size() == 2 && s.playlist[1].pattern == 1);
  2005. CHECK (s.currentPattern == 1);
  2006. }
  2007. TEST (flp_import_3x_osc_state_and_channel_filter)
  2008. {
  2009. // new 3x Osc patch (version 14): osc 2 mix 64 coarse -12, osc 3 mix 32 coarse -24
  2010. std::vector<std::uint8_t> blob (460, 0);
  2011. auto put = [&] (std::size_t off, std::int32_t v) {
  2012. for (int b = 0; b < 4; ++b)
  2013. blob[off + (std::size_t) b] = (std::uint8_t) ((std::uint32_t) v >> (8 * b));
  2014. };
  2015. put (0x00, 14);
  2016. put (0x08, 3); // osc 1 saw
  2017. put (0x10, 7); // osc 1 fine +7
  2018. put (0x04, -32); // osc 1 pan -0.5
  2019. put (0x1C, 64); // osc 2 mix
  2020. put (0x28, -12); // osc 2 coarse
  2021. put (0x38, 32); // osc 3 mix
  2022. put (0x40, 2); // osc 3 square
  2023. put (0x44, -19); // osc 3 coarse: -1 octave and -7 semitones
  2024. lynx::SynthSettings s;
  2025. CHECK (lynx::synthFrom3xOsc (blob, s));
  2026. CHECK (std::abs (s.osc[0].level - 0.375f) < 1e-4f && std::abs (s.osc[1].level - 0.375f) < 1e-4f && std::abs (s.osc[2].level - 0.25f) < 1e-4f);
  2027. CHECK (s.osc[0].wave == lynx::Waveform::saw && s.osc[0].detune == 7.0f && s.osc[0].pan == -0.5f);
  2028. CHECK (s.osc[1].wave == lynx::Waveform::sine && s.osc[1].octave == -1 && s.osc[1].semitones == 0);
  2029. CHECK (s.osc[2].wave == lynx::Waveform::square && s.osc[2].octave == -1 && s.osc[2].semitones == -7);
  2030. CHECK (! lynx::synthFrom3xOsc (std::vector<std::uint8_t> (40, 0), s));
  2031. // channel filter: open by default; LP with Mod X 128 ~ 2.71 kHz; Fast LP with Mod X 128 ~ 280 Hz
  2032. float hz = 0.0f, res = 1.0f;
  2033. int mode = -1;
  2034. lynx::synthFilterFromChannel (256, 0, 0, hz, res, mode);
  2035. CHECK (hz >= 19999.0f && res == 0.0f);
  2036. lynx::synthFilterFromChannel (128, 0, 1, hz, res, mode);
  2037. CHECK (std::abs (hz - 2710.0f) < 30.0f && res == 0.0f);
  2038. lynx::synthFilterFromChannel (128, 0, 0, hz, res, mode);
  2039. CHECK (hz > 240.0f && hz < 320.0f);
  2040. lynx::synthFilterFromChannel (128, 256, 1, hz, res, mode);
  2041. CHECK (res > 0.8f);
  2042. CHECK (mode == 0);
  2043. lynx::synthFilterFromChannel (64, 0, 3, hz, res, mode); // high-pass at ~840 Hz
  2044. CHECK (mode == 2 && std::abs (hz - 840.0f) < 40.0f);
  2045. lynx::synthFilterFromChannel (128, 0, 2, hz, res, mode); // band-pass
  2046. CHECK (mode == 1);
  2047. }
  2048. TEST (synth_pitch_and_filter_envelopes)
  2049. {
  2050. auto render = [] (const lynx::SynthSettings& s, int frames) {
  2051. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  2052. inst->prepare (48000.0);
  2053. lynx::ChannelDef c;
  2054. c.synth = s;
  2055. inst->configure (c, 120.0);
  2056. inst->noteOn (45, 1.0f); // A2 = 110 Hz
  2057. std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
  2058. inst->render (l.data(), r.data(), frames, 1.0f, 1.0f);
  2059. return l;
  2060. };
  2061. auto crossings = [] (const std::vector<float>& b, int from, int to) {
  2062. int n = 0;
  2063. for (int i = from + 1; i < to; ++i)
  2064. n += (b[(std::size_t) i - 1] < 0.0f) != (b[(std::size_t) i] < 0.0f);
  2065. return n;
  2066. };
  2067. // 3x Osc "kick": sine with a +24 st pitch envelope falling fast
  2068. lynx::SynthSettings kick;
  2069. kick.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  2070. kick.cutoff = 20000.0f;
  2071. kick.pitchEnv = { 24.0f, 0.0f, 0.0f, 0.05f, 0.0f, 0.05f };
  2072. const auto k = render (kick, 48000);
  2073. const auto early = crossings (k, 0, 2400); // first 50 ms: well above 110 Hz
  2074. const auto late = crossings (k, 24000, 48000); // then back to 110 Hz (~110 crossings in 0.5 s)
  2075. CHECK (early > 20); // +24 st ramp over 50 ms: ~2.2x the average frequency (11 crossings without envelope)
  2076. CHECK (std::abs (late - 110) < 4);
  2077. // cutoff envelope: saw closed at 200 Hz opens +5 octaves on attack and closes again
  2078. lynx::SynthSettings pluck;
  2079. pluck.cutoff = 200.0f;
  2080. pluck.filterEnv = { 5.0f, 0.0f, 0.0f, 0.1f, 0.0f, 0.1f };
  2081. const auto p = render (pluck, 48000);
  2082. auto hf = [&] (int from, int to) { // energy of the difference (highs)
  2083. double e = 0.0;
  2084. for (int i = from + 1; i < to; ++i)
  2085. e += std::pow ((double) p[(std::size_t) i] - p[(std::size_t) i - 1], 2.0);
  2086. return e;
  2087. };
  2088. CHECK (hf (0, 2400) > 4.0 * hf (40000, 42400));
  2089. // Toxic Biohazard's envelope (REA_rodada33 §2): exponential towards 5 clipped at 1, so the cutoff is still near
  2090. // its base early in the attack, fully open at A = 0.2 s and back down after the exponential decay
  2091. lynx::SynthSettings toxic = pluck;
  2092. toxic.filterEnv = { 5.0f, 0.2f, 0.0f, 0.1f, 0.0f, 0.1f, 1 };
  2093. const auto t = render (toxic, 48000);
  2094. auto thf = [&] (int from, int to) {
  2095. double e = 0.0;
  2096. for (int i = from + 1; i < to; ++i)
  2097. e += std::pow ((double) t[(std::size_t) i] - t[(std::size_t) i - 1], 2.0);
  2098. return e;
  2099. };
  2100. CHECK (thf (9000, 9600) > 4.0 * thf (300, 900));
  2101. CHECK (thf (9000, 9600) > 4.0 * thf (40000, 40600));
  2102. }
  2103. TEST (synth_mono_reuses_the_sounding_voice)
  2104. {
  2105. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  2106. inst->prepare (48000.0);
  2107. lynx::ChannelDef c;
  2108. c.synth.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  2109. c.synth.cutoff = 20000.0f;
  2110. c.synth.mono = true;
  2111. inst->configure (c, 120.0);
  2112. std::vector<float> l (48000), r (48000);
  2113. inst->noteOn (57, 1.0f);
  2114. inst->render (l.data(), r.data(), 4800, 1.0f, 1.0f);
  2115. inst->noteOn (69, 1.0f); // A4 takes over the A3 voice: only one note sounding
  2116. inst->render (l.data() + 4800, r.data() + 4800, 43200, 1.0f, 1.0f);
  2117. int crossings = 0;
  2118. float peak = 0.0f;
  2119. for (std::size_t i = 24001; i < 48000; ++i)
  2120. {
  2121. crossings += (l[i - 1] < 0.0f) != (l[i] < 0.0f);
  2122. peak = std::max (peak, std::abs (l[i]));
  2123. }
  2124. CHECK (std::abs (crossings - 440) < 4); // 0.5 s of 440 Hz, without the A3 underneath
  2125. CHECK (peak < 0.35f); // one voice (0.3 level), not two summed
  2126. // no click on the handover: the jump between neighbouring samples stays sine-sized
  2127. float jump = 0.0f;
  2128. for (std::size_t i = 4790; i < 4810; ++i)
  2129. jump = std::max (jump, std::abs (l[i] - l[i - 1]));
  2130. CHECK (jump < 0.05f);
  2131. }
  2132. TEST (synth_porta_glides_and_am_modulates)
  2133. {
  2134. auto make = [] (const lynx::SynthSettings& s) {
  2135. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  2136. inst->prepare (48000.0);
  2137. lynx::ChannelDef c;
  2138. c.synth = s;
  2139. inst->configure (c, 120.0);
  2140. return inst;
  2141. };
  2142. auto crossings = [] (const std::vector<float>& b, std::size_t from, std::size_t to) {
  2143. int n = 0;
  2144. for (auto i = from + 1; i < to; ++i)
  2145. n += (b[i - 1] < 0.0f) != (b[i] < 0.0f);
  2146. return n;
  2147. };
  2148. // mono + 1-beat porta (0.5 s at 120 BPM): from A3 to A4, halfway ~E4 (329.6 Hz)
  2149. lynx::SynthSettings s;
  2150. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  2151. s.cutoff = 20000.0f;
  2152. s.mono = true;
  2153. s.porta = true;
  2154. s.portaBeats = 1.0f;
  2155. auto inst = make (s);
  2156. std::vector<float> l (96000), r (96000);
  2157. inst->noteOn (57, 1.0f);
  2158. inst->render (l.data(), r.data(), 4800, 1.0f, 1.0f);
  2159. inst->noteOn (69, 1.0f);
  2160. inst->render (l.data() + 4800, r.data() + 4800, 91200, 1.0f, 1.0f);
  2161. const auto middle = crossings (l, 4800 + 12000 - 2400, 4800 + 12000 + 2400); // 0.1 s around 0.25 s
  2162. CHECK (std::abs (middle - 66) < 6); // ~330 Hz * 0.1 s * 2
  2163. CHECK (std::abs (crossings (l, 48000, 96000) - 880) < 4); // reached A4 (1 s = 880 crossings)
  2164. // AM: osc 3 at level 1.0, slow sine, modulates osc 1 (the average level drops, the sum doesn't gain osc 3)
  2165. lynx::SynthSettings am;
  2166. am.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  2167. am.osc[2] = { lynx::Waveform::sine, -3, 0.0f, 1.0f };
  2168. am.cutoff = 20000.0f;
  2169. am.am = true;
  2170. auto modulated = make (am);
  2171. modulated->noteOn (69, 1.0f);
  2172. std::fill (l.begin(), l.end(), 0.0f);
  2173. modulated->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
  2174. float peak = 0.0f;
  2175. for (std::size_t i = 4800; i < 48000; ++i)
  2176. peak = std::max (peak, std::abs (l[i]));
  2177. CHECK (peak > 0.45f && peak < 0.65f); // 0.3 · (1 + 1) at most: (1 − o3) goes up to 2
  2178. }
  2179. TEST (channel_lfo_vibrato_and_tremolo)
  2180. {
  2181. auto render = [] (const lynx::SynthSettings& s) {
  2182. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  2183. inst->prepare (48000.0);
  2184. lynx::ChannelDef c;
  2185. c.synth = s;
  2186. inst->configure (c, 120.0);
  2187. inst->noteOn (69, 1.0f);
  2188. std::vector<float> l (48000), r (48000);
  2189. inst->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
  2190. return l;
  2191. };
  2192. lynx::SynthSettings s;
  2193. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  2194. s.cutoff = 20000.0f;
  2195. // tremolo: 100% volume LFO at 4 Hz (0.25 s period) -> the amplitude envelope goes from ~0 to ~2x
  2196. auto tremolo = s;
  2197. tremolo.lfo[lynx::lfoVolume] = { 1.0f, 0.25f, false, 0, 0.0f, 0.0f };
  2198. const auto t = render (tremolo);
  2199. float lowest = 1.0f, highest = 0.0f;
  2200. for (std::size_t w = 4800; w + 480 <= 48000; w += 480) // 10 ms windows (4.4 cycles of 440 Hz)
  2201. {
  2202. float peak = 0.0f;
  2203. for (auto i = w; i < w + 480; ++i)
  2204. peak = std::max (peak, std::abs (t[i]));
  2205. lowest = std::min (lowest, peak);
  2206. highest = std::max (highest, peak);
  2207. }
  2208. CHECK (lowest < 0.08f && highest > 0.5f);
  2209. // synced vibrato: ±1 semitone, 1-beat period (0.5 s at 120 BPM); the average stays at 440 Hz
  2210. auto vibrato = s;
  2211. vibrato.lfo[lynx::lfoPitch] = { 1.0f, 1.0f, true, 0, 0.0f, 0.0f };
  2212. const auto v = render (vibrato);
  2213. int crossings = 0;
  2214. for (std::size_t i = 1; i < 48000; ++i)
  2215. crossings += (v[i - 1] < 0.0f) != (v[i] < 0.0f);
  2216. CHECK (std::abs (crossings - 880) < 12);
  2217. int firstQuarter = 0; // first quarter of the cycle: rising sine -> higher than 440
  2218. for (std::size_t i = 1; i < 6000; ++i)
  2219. firstQuarter += (v[i - 1] < 0.0f) != (v[i] < 0.0f);
  2220. CHECK (firstQuarter > 112);
  2221. }
  2222. TEST (sampler_porta_glides_from_previous_note)
  2223. {
  2224. auto data = std::make_shared<lynx::SampleData>();
  2225. data->sampleRate = 48000.0;
  2226. data->left.resize (96000);
  2227. for (std::size_t i = 0; i < data->left.size(); ++i)
  2228. data->left[i] = 0.5f * (float) std::sin (6.283185307 * 220.0 * (double) i / 48000.0);
  2229. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  2230. inst->prepare (48000.0);
  2231. lynx::ChannelDef c;
  2232. c.sampler.cut = true;
  2233. c.sampler.porta = true;
  2234. c.sampler.portaBeats = 1.0f; // 0.5 s at 120 BPM
  2235. inst->configure (c, 120.0);
  2236. std::vector<float> l (24000), r (24000);
  2237. inst->noteOn (60, 1.0f);
  2238. inst->render (l.data(), r.data(), 2400, 1.0f, 1.0f);
  2239. inst->noteOn (72, 1.0f); // one octave up: starts at 220 Hz and rises
  2240. std::fill (l.begin(), l.end(), 0.0f);
  2241. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  2242. auto crossings = [&] (std::size_t from, std::size_t to) {
  2243. int n = 0;
  2244. for (auto i = from + 1; i < to; ++i)
  2245. n += (l[i - 1] < 0.0f) != (l[i] < 0.0f);
  2246. return n;
  2247. };
  2248. const auto start = crossings (600, 2400); // 37.5 ms still near 220 Hz: ~17 crossings (440 Hz would give 33)
  2249. const auto end = crossings (21600, 24000); // 50 ms at the end of the glide: near 440 Hz (~44; 220 Hz would give 22)
  2250. CHECK (start < 22);
  2251. CHECK (end > 36);
  2252. }
  2253. TEST (zoned_sampler_picks_zone_by_key_and_velocity)
  2254. {
  2255. auto sine = [] (double hz, float amp) {
  2256. auto d = std::make_shared<lynx::SampleData>();
  2257. d->sampleRate = 48000.0;
  2258. d->left.resize (48000);
  2259. for (std::size_t i = 0; i < d->left.size(); ++i)
  2260. d->left[i] = amp * (float) std::sin (6.283185307 * hz * (double) i / 48000.0);
  2261. return d;
  2262. };
  2263. // lows: 220 Hz sine recorded as A3 (57), up to key 69; highs: 880 Hz as A5 (81)
  2264. // hard layer on the highs: same tuning, louder
  2265. std::vector<lynx::LoadedZone> zones {
  2266. { { "graves.wav", 57, 0, 69, 0, 127, 0.0f, 1.0f }, sine (220.0, 0.5f) },
  2267. { { "agudos.wav", 81, 70, 127, 0, 99, 0.0f, 1.0f }, sine (880.0, 0.25f) },
  2268. { { "agudos forte.wav", 81, 70, 127, 100, 127, 0.0f, 1.0f }, sine (880.0, 0.9f) },
  2269. };
  2270. auto inst = lynx::makeZonedSampler (zones);
  2271. inst->prepare (48000.0);
  2272. inst->configure (lynx::ChannelDef {}, 120.0);
  2273. auto play = [&] (int key, float velocity) {
  2274. inst->allNotesOff();
  2275. std::vector<float> l (24000), r (24000);
  2276. inst->render (l.data(), r.data(), 2000, 1.0f, 1.0f); // let the old voices die out
  2277. std::fill (l.begin(), l.end(), 0.0f);
  2278. inst->noteOn (key, velocity);
  2279. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  2280. int crossings = 0;
  2281. float peak = 0.0f;
  2282. for (std::size_t i = 1; i < 24000; ++i)
  2283. {
  2284. crossings += (l[i - 1] < 0.0f) != (l[i] < 0.0f);
  2285. peak = std::max (peak, std::abs (l[i]));
  2286. }
  2287. return std::pair { crossings, peak };
  2288. };
  2289. const auto low = play (69, 0.5f); // A4 from root A3: 440 Hz -> 440 crossings in 0.5 s
  2290. CHECK (std::abs (low.first - 440) < 4);
  2291. const auto soft = play (81, 0.5f); // high zone at the root: 880 Hz
  2292. CHECK (std::abs (soft.first - 880) < 6);
  2293. const auto hard = play (81, 0.95f); // high velocity: hard layer
  2294. CHECK (hard.second > soft.second * 2.0f);
  2295. // overlapping layers round-robin: two identical zones at different levels, notes alternate
  2296. auto rr = lynx::makeZonedSampler ({ { { "a.wav", 60 }, sine (440.0, 0.2f) }, { { "b.wav", 60 }, sine (440.0, 0.8f) } });
  2297. rr->prepare (48000.0);
  2298. rr->configure (lynx::ChannelDef {}, 120.0);
  2299. float peaks[2] {};
  2300. for (int k = 0; k < 2; ++k)
  2301. {
  2302. rr->allNotesOff();
  2303. std::vector<float> l (4800), r (4800);
  2304. rr->render (l.data(), r.data(), 2000, 1.0f, 1.0f);
  2305. std::fill (l.begin(), l.end(), 0.0f);
  2306. rr->noteOn (60, 1.0f);
  2307. rr->render (l.data(), r.data(), 4800, 1.0f, 1.0f);
  2308. for (auto v : l)
  2309. peaks[k] = std::max (peaks[k], std::abs (v));
  2310. }
  2311. CHECK (std::abs (peaks[0] - peaks[1]) > 0.3f);
  2312. }
  2313. TEST (zoned_sampler_loops_while_held)
  2314. {
  2315. // 0.1 s sample with a loop in the middle: holding the note it keeps sounding well past the end of the file;
  2316. // on release, it leaves the loop and ends
  2317. auto data = std::make_shared<lynx::SampleData>();
  2318. data->sampleRate = 48000.0;
  2319. data->left.resize (4800);
  2320. for (std::size_t i = 0; i < data->left.size(); ++i)
  2321. data->left[i] = 0.5f * (float) std::sin (6.283185307 * 480.0 * (double) i / 48000.0); // 100 frames per cycle
  2322. lynx::SampleZone zone { "loop.wav", 60 };
  2323. zone.loopStart = 1000;
  2324. zone.loopEnd = 3000;
  2325. zone.loopMode = 2;
  2326. auto inst = lynx::makeZonedSampler ({ { zone, data } });
  2327. inst->prepare (48000.0);
  2328. lynx::ChannelDef c;
  2329. c.sampler.release = 0.01f;
  2330. inst->configure (c, 120.0);
  2331. inst->noteOn (60, 1.0f);
  2332. std::vector<float> l (48000), r (48000);
  2333. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f); // 0.5 s held
  2334. float held = 0.0f;
  2335. for (std::size_t i = 20000; i < 24000; ++i)
  2336. held = std::max (held, std::abs (l[i]));
  2337. CHECK (held > 0.3f);
  2338. inst->noteOff (60);
  2339. inst->render (l.data() + 24000, r.data() + 24000, 24000, 1.0f, 1.0f);
  2340. float after = 0.0f;
  2341. for (std::size_t i = 40000; i < 48000; ++i)
  2342. after = std::max (after, std::abs (l[i]));
  2343. CHECK (after == 0.0f);
  2344. }
  2345. TEST (global_lfo_follows_song_position)
  2346. {
  2347. // global volume LFO (1-beat period): a note mid-cycle starts mid-wave; the
  2348. // same note with a per-note LFO starts at phase 0
  2349. auto first = [] (bool global, double songTicks) {
  2350. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  2351. inst->prepare (48000.0);
  2352. lynx::ChannelDef c;
  2353. c.synth.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  2354. c.synth.cutoff = 20000.0f;
  2355. c.synth.lfo[lynx::lfoVolume] = { 1.0f, 1.0f, true, 2, 0.0f, 0.0f, global }; // pulse: +1 in the first half
  2356. inst->configure (c, 120.0);
  2357. inst->setSongPosition (songTicks);
  2358. inst->noteOn (69, 1.0f);
  2359. std::vector<float> l (4800), r (4800);
  2360. inst->render (l.data(), r.data(), 4800, 1.0f, 1.0f);
  2361. float peak = 0.0f;
  2362. for (std::size_t i = 2400; i < 4800; ++i)
  2363. peak = std::max (peak, std::abs (l[i]));
  2364. return peak;
  2365. };
  2366. // mid-cycle (phase 0.6) the pulse is at −1: volume ~0; per note (phase 0) it is at +1: ~2x
  2367. const auto globalLate = first (true, 0.6 * lynx::ppq);
  2368. const auto perNote = first (false, 0.6 * lynx::ppq);
  2369. CHECK (globalLate < 0.05f);
  2370. CHECK (perNote > 0.4f);
  2371. }
  2372. TEST (sampler_channel_filter_darkens_and_bypasses_when_open)
  2373. {
  2374. // white noise in the sampler: with a 300 Hz cutoff the energy drops a lot; open passes unchanged
  2375. auto data = std::make_shared<lynx::SampleData>();
  2376. data->sampleRate = 48000.0;
  2377. data->left.resize (48000);
  2378. std::uint32_t seed = 7;
  2379. for (auto& v : data->left)
  2380. {
  2381. seed = seed * 1664525u + 1013904223u;
  2382. v = ((float) (seed >> 8) / 16777216.0f - 0.5f);
  2383. }
  2384. data->right = data->left;
  2385. auto energy = [&] (float cutoff) {
  2386. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  2387. inst->prepare (48000.0);
  2388. lynx::ChannelDef c;
  2389. c.sampler.cutoff = cutoff;
  2390. inst->configure (c, 120.0);
  2391. inst->noteOn (60, 1.0f);
  2392. std::vector<float> l (24000), r (24000);
  2393. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  2394. double e = 0.0;
  2395. for (auto v : l)
  2396. e += (double) v * v;
  2397. return e;
  2398. };
  2399. const auto open = energy (20000.0f), dark = energy (300.0f);
  2400. {
  2401. // high-pass at 10 kHz: also removes most of the noise energy, but from the other side
  2402. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  2403. inst->prepare (48000.0);
  2404. lynx::ChannelDef hp;
  2405. hp.sampler.cutoff = 10000.0f;
  2406. hp.sampler.filterType = 2;
  2407. inst->configure (hp, 120.0);
  2408. inst->noteOn (60, 1.0f);
  2409. std::vector<float> l (24000), r (24000);
  2410. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  2411. double e = 0.0, lowE = 0.0;
  2412. float lp = 0.0f;
  2413. for (auto v : l)
  2414. {
  2415. e += (double) v * v;
  2416. lp += 0.01f * (v - lp); // crude low-pass: almost nothing should remain below
  2417. lowE += (double) lp * lp;
  2418. }
  2419. CHECK (e > 10.0 && e < 0.7 * energy (20000.0f));
  2420. CHECK (lowE < e * 0.01);
  2421. }
  2422. CHECK (open > 100.0);
  2423. CHECK (dark < open * 0.05);
  2424. // +12 st pitch envelope with sustain 1: the sample runs at double speed
  2425. // and ends in half the time (1 s of sample -> 0.5 s)
  2426. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  2427. inst->prepare (48000.0);
  2428. lynx::ChannelDef c;
  2429. c.sampler.pitchEnv = { 12.0f, 0.0f, 0.0f, 0.1f, 1.0f, 0.1f };
  2430. inst->configure (c, 120.0);
  2431. inst->noteOn (60, 1.0f);
  2432. std::vector<float> l (48000), r (48000);
  2433. inst->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
  2434. float tail = 0.0f, body = 0.0f;
  2435. for (std::size_t i = 20000; i < 23000; ++i)
  2436. body = std::max (body, std::abs (l[i]));
  2437. for (std::size_t i = 25000; i < 48000; ++i)
  2438. tail = std::max (tail, std::abs (l[i]));
  2439. CHECK (body > 0.1f);
  2440. CHECK (tail == 0.0f);
  2441. }
  2442. TEST (synth_oscillator_pan_and_semitones)
  2443. {
  2444. auto render = [] (lynx::SynthSettings s, std::vector<float>& l, std::vector<float>& r) {
  2445. auto synth = lynx::makeInstrument (lynx::InstrumentKind::synth);
  2446. synth->prepare (48000.0);
  2447. lynx::ChannelDef c;
  2448. c.synth = s;
  2449. synth->configure (c, 120.0);
  2450. synth->noteOn (69, 1.0f);
  2451. l.assign (24000, 0.0f);
  2452. r.assign (24000, 0.0f);
  2453. synth->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  2454. };
  2455. lynx::SynthSettings s;
  2456. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f, 7, 1.0f }; // a fifth up, fully right
  2457. s.cutoff = 20000.0f;
  2458. std::vector<float> l, r;
  2459. render (s, l, r);
  2460. float peakL = 0.0f, peakR = 0.0f;
  2461. int crossings = 0;
  2462. for (std::size_t i = 4800; i < 24000; ++i)
  2463. {
  2464. peakL = std::max (peakL, std::abs (l[i]));
  2465. peakR = std::max (peakR, std::abs (r[i]));
  2466. crossings += (r[i - 1] < 0.0f) != (r[i] < 0.0f);
  2467. }
  2468. CHECK (peakL < 1e-4f && peakR > 0.1f);
  2469. // 440 * 2^(7/12) = 659.3 Hz; 0.4 s = 263.7 cycles = ~527 crossings
  2470. CHECK (std::abs (crossings - 527) < 6);
  2471. }
  2472. TEST (fl_wrapper_state_parses_records_and_vst3_state)
  2473. {
  2474. // Fruity Wrapper blob (version 12): VST3 generator type, class ID, name, path and state
  2475. std::vector<std::uint8_t> blob;
  2476. auto u32 = [] (std::vector<std::uint8_t>& b, std::uint32_t v) { for (int i = 0; i < 4; ++i) b.push_back ((std::uint8_t) (v >> (8 * i))); };
  2477. auto record = [&] (std::vector<std::uint8_t>& b, std::uint32_t id, const std::vector<std::uint8_t>& payload) {
  2478. u32 (b, id);
  2479. u32 (b, (std::uint32_t) payload.size());
  2480. u32 (b, 0);
  2481. b.insert (b.end(), payload.begin(), payload.end());
  2482. };
  2483. auto bytes = [] (const std::string& s) { return std::vector<std::uint8_t> (s.begin(), s.end()); };
  2484. std::vector<std::uint8_t> vst3State;
  2485. u32 (vst3State, 1);
  2486. record (vst3State, 1, std::vector<std::uint8_t> (64, 0));
  2487. record (vst3State, 3, bytes ("COMPONENTE"));
  2488. record (vst3State, 2, bytes ("CONTROLADOR"));
  2489. // record 4: 3 slots → ParamIDs 700, −1 (empty), 0xFFFFFF01
  2490. std::vector<std::uint8_t> slots;
  2491. for (auto v : { 3u, 700u, 0xFFFFFFFFu, 0xFFFFFF01u })
  2492. u32 (slots, v);
  2493. record (vst3State, 4, slots);
  2494. u32 (blob, 12);
  2495. record (blob, 0x01, std::vector<std::uint8_t> (20, 0xFF));
  2496. record (blob, 0x32, { 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });
  2497. std::vector<std::uint8_t> cid;
  2498. for (int i = 0; i < 16; ++i)
  2499. cid.push_back ((std::uint8_t) (0xA0 + i));
  2500. record (blob, 0x34, cid);
  2501. record (blob, 0x36, bytes ("Surge XT"));
  2502. record (blob, 0x37, bytes ("C:/Program Files/Common Files/VST3/Surge XT.vst3"));
  2503. record (blob, 0x35, vst3State);
  2504. lynx::ForeignPlugin p;
  2505. CHECK (lynx::parseFlWrapper (blob, p));
  2506. CHECK (p.format == lynx::ForeignPlugin::Format::vst3);
  2507. CHECK (p.hasClassId && p.classId[0] == 0xA0 && p.classId[15] == 0xAF);
  2508. CHECK (p.name == "Surge XT" && p.path.find ("Surge XT.vst3") != std::string::npos);
  2509. std::vector<std::uint8_t> component, controller;
  2510. CHECK (lynx::splitFlVst3State (p.state, component, controller));
  2511. CHECK (component == bytes ("COMPONENTE") && controller == bytes ("CONTROLADOR"));
  2512. const auto saved = lynx::flVst3ParamSlots (p.state);
  2513. CHECK (saved == (std::vector<std::int32_t> { 700, -1, (std::int32_t) 0xFFFFFF01u }));
  2514. // FL's slot assignment: saved slots keep their ParamID wherever it now sits; the empty slot stays empty;
  2515. // a vanished ParamID (0xFFFFFF01) leaves its slot dead; parameters without a slot get slots 3, 4… in list order
  2516. const auto positions = lynx::flVst3SlotPositions (saved, { 5u, 700u, 9u });
  2517. CHECK (positions == (std::vector<int> { 1, -1, -1, 0, 2 }));
  2518. CHECK (lynx::flVst3SlotPositions ({}, { 5u, 6u }) == (std::vector<int> { 0, 1 })); // new instance: slot = position
  2519. // version out of range or no type: not a usable wrapper
  2520. blob[0] = 13;
  2521. CHECK (! lynx::parseFlWrapper (blob, p));
  2522. CHECK (! lynx::parseFlWrapper ({ 12, 0, 0, 0 }, p));
  2523. }
  2524. TEST (schedule_index_matches_full_scan)
  2525. {
  2526. // random song (long notes, swing, clips with offset); indexed scheduling must yield
  2527. // exactly the same events as the full scan, including in the block that wraps around
  2528. lynx::Song s = lynx::Song::makeDefault();
  2529. s.swing = 0.6f;
  2530. std::uint32_t seed = 12345;
  2531. auto rnd = [&] (int n) { seed = seed * 1664525u + 1013904223u; return (int) ((seed >> 8) % (std::uint32_t) n); };
  2532. s.patterns.resize (3);
  2533. for (auto& p : s.patterns)
  2534. {
  2535. p.notes.clear();
  2536. for (int i = 0; i < 60; ++i)
  2537. {
  2538. lynx::Note n;
  2539. n.start = rnd (16 * lynx::ppq);
  2540. n.length = rnd (5) == 0 ? rnd (12 * lynx::ppq) + 1 : rnd (lynx::ppq) + 1;
  2541. n.channel = rnd (4);
  2542. n.key = 40 + rnd (40);
  2543. n.slide = rnd (20) == 0;
  2544. p.notes.push_back (n);
  2545. }
  2546. p.sortNotes();
  2547. }
  2548. s.playlist.clear();
  2549. for (int i = 0; i < 12; ++i)
  2550. {
  2551. lynx::Clip c;
  2552. c.pattern = rnd (3);
  2553. c.track = rnd (4);
  2554. c.start = rnd (40) * lynx::ppq;
  2555. c.length = (rnd (12) + 1) * lynx::ppq;
  2556. c.offset = rnd (4) * lynx::ppq;
  2557. s.playlist.push_back (c);
  2558. }
  2559. s.mode = lynx::PlayMode::song;
  2560. const auto index = lynx::makeScheduleIndex (s);
  2561. const auto tps = lynx::ticksPerSample (140.0, 48000.0);
  2562. const auto loop = (double) s.songLength();
  2563. int mismatches = 0, events = 0;
  2564. for (int block = 0; block < 400; ++block)
  2565. {
  2566. const auto start = block == 0 ? loop - 100.0 * tps : (double) rnd ((int) loop * 2);
  2567. const auto samples = 64 + rnd (24000); // up to ~1 beat per block
  2568. lynx::EventBuffer a, b;
  2569. lynx::scheduleSong (s, start, samples, tps, a, false, nullptr);
  2570. lynx::scheduleSong (s, start, samples, tps, b, false, &index);
  2571. events += a.size();
  2572. if (a.size() != b.size())
  2573. {
  2574. ++mismatches;
  2575. continue;
  2576. }
  2577. for (int i = 0; i < a.size(); ++i)
  2578. {
  2579. const auto& x = *(a.begin() + i);
  2580. const auto& y = *(b.begin() + i);
  2581. mismatches += x.sampleOffset != y.sampleOffset || x.type != y.type || x.key != y.key || x.channel != y.channel;
  2582. }
  2583. }
  2584. CHECK (events > 500);
  2585. CHECK (mismatches == 0);
  2586. }
  2587. TEST (mixer_track_eq_defaults_and_boost)
  2588. {
  2589. // FL defaults: 90 Hz, 1.5 kHz and 8 kHz; peak Q 0.632; shelf slope 0.607
  2590. lynx::TrackEq eq;
  2591. CHECK (std::abs (lynx::TrackEq::peakQ (eq.width[1]) - 0.632) < 0.01);
  2592. CHECK (std::abs (lynx::TrackEq::shelfSlope (eq.width[0]) - 0.607) < 0.01);
  2593. CHECK (! eq.active());
  2594. // +12 dB peak at 1.5 kHz on a track: a 1.5 kHz sine rises ~4x
  2595. lynx::Song song = lynx::Song::makeDefault();
  2596. song.mixer[1].eq.gainDb[1] = 12.0f;
  2597. for (auto& c : song.channels)
  2598. c.mixerTrack = 1;
  2599. song.channels[4].instrument = lynx::InstrumentKind::synth;
  2600. song.channels[4].synth = {};
  2601. song.channels[4].synth.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  2602. song.channels[4].synth.cutoff = 20000.0f;
  2603. auto render = [] (const lynx::Song& s) {
  2604. auto state = lynx::RackState::build (s, nullptr, 48000.0);
  2605. lynx::RackRenderer renderer;
  2606. renderer.prepare (48000.0);
  2607. lynx::LiveEventQueue live;
  2608. lynx::NoteEvent on; // 1.5 kHz ~ key 90.6: use 91 (1568 Hz), still within the wide peak
  2609. on.channel = 4;
  2610. on.key = 91;
  2611. on.velocity = 1.0f;
  2612. live.push (on);
  2613. std::vector<float> l (24000), r (24000);
  2614. for (int pos = 0; pos < 24000; pos += 480)
  2615. renderer.process (state.get(), false, 0.0, 480, l.data() + pos, r.data() + pos, pos == 0 ? &live : nullptr);
  2616. float peak = 0.0f;
  2617. for (std::size_t i = 12000; i < 24000; ++i)
  2618. peak = std::max (peak, std::abs (l[i]));
  2619. return peak;
  2620. };
  2621. const auto boosted = render (song);
  2622. song.mixer[1].eq.gainDb[1] = 0.0f;
  2623. const auto flat = render (song);
  2624. CHECK (flat > 0.01f);
  2625. CHECK (boosted / flat > 3.0f && boosted / flat < 4.2f);
  2626. }
  2627. TEST (bars_follow_time_signature_markers)
  2628. {
  2629. lynx::Song song;
  2630. song.beatsPerBar = 4;
  2631. const auto bar4 = (std::int64_t) lynx::ppq * 4;
  2632. auto bars = song.barsBetween (0, bar4 * 3);
  2633. CHECK (bars.size() == 3 && bars[2].tick == bar4 * 2 && bars[2].number == 3);
  2634. // 7/4 from bar 3, and 3/4 in the middle of a 7 bar (cuts and restarts)
  2635. song.addMarker ({ bar4 * 2, "Verso", 7, 4 });
  2636. song.addMarker ({ bar4, "Nome só", 0, 4 });
  2637. const auto seven = (std::int64_t) lynx::ppq * 7;
  2638. song.addMarker ({ bar4 * 2 + seven + lynx::ppq * 2, "", 3, 4 });
  2639. bars = song.barsBetween (0, bar4 * 2 + seven * 2);
  2640. CHECK (bars.size() >= 5);
  2641. CHECK (bars[1].length == bar4 && bars[2].tick == bar4 * 2 && bars[2].length == seven);
  2642. CHECK (bars[3].length == lynx::ppq * 2); // cut short by the change
  2643. CHECK (bars[4].length == lynx::ppq * 3 && bars[4].number == 5);
  2644. CHECK (song.markers()[0].name == "Nome só"); // sorted by tick
  2645. // from far away: starts at the right bar without losing the numbering
  2646. const auto far = song.barsBetween (lynx::ppq * 3000, lynx::ppq * 3001);
  2647. CHECK (far.size() == 1 && far[0].length == lynx::ppq * 3);
  2648. CHECK (song.barAt (lynx::ppq * 3000).number == far[0].number);
  2649. CHECK ((lynx::ppq * 3000 - far[0].tick) < far[0].length);
  2650. }
  2651. TEST (split_drum_channel_moves_notes_to_pads)
  2652. {
  2653. lynx::Song song = lynx::Song::makeDefault();
  2654. const auto channels = (int) song.channels.size();
  2655. song.patterns[0].notes.clear();
  2656. song.patterns[0].notes.push_back ({ 0, 240, 1, 36, 0.9f }); // kit kick (channel 1)
  2657. song.patterns[0].notes.push_back ({ 240, 240, 1, 38, 0.8f }); // snare
  2658. song.patterns[0].notes.push_back ({ 480, 240, 1, 50, 0.8f }); // key without a pad: dropped
  2659. song.patterns[0].notes.push_back ({ 480, 240, 3, 60, 0.8f }); // other channel: shifts by 1 position
  2660. lynx::Automation a;
  2661. a.target = { lynx::AutomationTargetKind::channelVolume, 3, 0, 0 };
  2662. song.automations.push_back (a);
  2663. std::vector<lynx::DrumPad> pads (2);
  2664. pads[0].name = "Kick";
  2665. pads[0].key = 36;
  2666. pads[0].layers = { { "kick.wav" } };
  2667. pads[1].name = "Snare";
  2668. pads[1].key = 38;
  2669. pads[1].layers = { { "snare soft.wav", 60, 0, 127, 0, 90 }, { "snare hard.wav", 60, 0, 127, 91, 127 } };
  2670. pads[1].cutGroup = 2;
  2671. pads[1].cutBy = 2;
  2672. lynx::splitDrumChannel (song, 1, pads);
  2673. CHECK ((int) song.channels.size() == channels + 1);
  2674. CHECK (song.channels[1].samplePath == "kick.wav" && song.channels[1].instrument == lynx::InstrumentKind::sampler);
  2675. CHECK (song.channels[2].zones.size() == 2 && song.channels[2].cutGroup == song.channels[2].cutBy && song.channels[2].cutGroup != 0);
  2676. const auto& notes = song.patterns[0].notes;
  2677. CHECK (notes.size() == 3);
  2678. CHECK (notes[0].channel == 1 && notes[0].key == 60);
  2679. CHECK (notes[1].channel == 2 && notes[1].key == 60);
  2680. CHECK (notes[2].channel == 4); // the old channel 3
  2681. CHECK (song.automations[0].target.index == 4);
  2682. }
  2683. TEST (flp_import_brings_playlist)
  2684. {
  2685. flp::Project p;
  2686. p.header.ppq = 96;
  2687. p.channels = { { 0, flp::ChannelType::sampler, "Kick", "", "", 1 } };
  2688. flp::Pattern empty;
  2689. empty.index = 1;
  2690. flp::Pattern full;
  2691. full.index = 2;
  2692. flp::Note n;
  2693. n.channel = 0;
  2694. full.notes.push_back (n);
  2695. p.patterns = { empty, full };
  2696. flp::Arrangement a;
  2697. flp::PlaylistItem item;
  2698. item.kind = flp::PlaylistItem::Kind::pattern;
  2699. item.index = 2;
  2700. item.position = 384;
  2701. item.length = 192;
  2702. item.track = 3;
  2703. item.patternStartTick = 96;
  2704. a.items.push_back (item);
  2705. item.index = 1; // empty pattern: discarded
  2706. a.items.push_back (item);
  2707. item.kind = flp::PlaylistItem::Kind::channelClip; // audio: not supported yet
  2708. a.items.push_back (item);
  2709. p.arrangements.push_back (a);
  2710. const auto s = lynx::importFlp (p);
  2711. CHECK (s.mode == lynx::PlayMode::song);
  2712. CHECK (s.playlist.size() == 1);
  2713. CHECK (s.playlist[0].pattern == 0 && s.playlist[0].track == 3);
  2714. CHECK (s.playlist[0].start == 4 * lynx::ppq && s.playlist[0].length == 2 * lynx::ppq && s.playlist[0].offset == lynx::ppq);
  2715. }
  2716. TEST (rack_releases_notes_on_position_jump)
  2717. {
  2718. lynx::Song s;
  2719. lynx::ChannelDef c;
  2720. c.instrument = lynx::InstrumentKind::synth;
  2721. s.channels.push_back (c);
  2722. auto& p = s.patterns.emplace_back();
  2723. lynx::Note n;
  2724. n.start = 0;
  2725. n.length = 4 * lynx::ppq; // long note
  2726. p.notes.push_back (n);
  2727. s.bpm = 120.0;
  2728. auto state = lynx::RackState::build (s, nullptr, 48000.0);
  2729. lynx::RackRenderer r;
  2730. r.prepare (48000.0);
  2731. std::vector<float> l (512), rr (512);
  2732. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  2733. double tick = 0.0;
  2734. for (int i = 0; i < 20; ++i, tick += 512 * tps)
  2735. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  2736. // jump to tick 2000 (mid-note, no new "on"): the synth must go silent
  2737. for (int i = 0; i < 40; ++i)
  2738. r.process (state.get(), true, 2000.0 + i * 512 * tps, 512, l.data(), rr.data());
  2739. float tail = 0.0f;
  2740. for (auto v : l)
  2741. tail = std::max (tail, std::abs (v));
  2742. CHECK (tail < 0.001f);
  2743. }
  2744. namespace
  2745. {
  2746. // Two samplers with a constant 0.5 signal on different inserts; returns the output peak.
  2747. float juce_db (float gain)
  2748. {
  2749. return 20.0f * std::log10 (gain);
  2750. }
  2751. float renderMixerCase (const std::function<void (lynx::Song&)>& tweak, lynx::MeterBank* meters = nullptr)
  2752. {
  2753. lynx::Song s;
  2754. s.mixer = lynx::Song::defaultMixer (4);
  2755. for (int i = 0; i < 2; ++i)
  2756. {
  2757. lynx::ChannelDef c;
  2758. c.instrument = lynx::InstrumentKind::sampler;
  2759. c.samplePath = "dc";
  2760. c.volume = 1.0f;
  2761. c.mixerTrack = i + 1;
  2762. s.channels.push_back (c);
  2763. }
  2764. s.patterns.emplace_back();
  2765. tweak (s);
  2766. auto provider = [] (const std::string&) {
  2767. auto d = std::make_shared<lynx::SampleData>();
  2768. d->left.assign (48000, 0.5f);
  2769. return std::shared_ptr<const lynx::SampleData> (d);
  2770. };
  2771. auto state = lynx::RackState::build (s, nullptr, 48000.0, provider);
  2772. lynx::RackRenderer r;
  2773. r.prepare (48000.0, 256);
  2774. lynx::LiveEventQueue live;
  2775. for (int ch = 0; ch < 2; ++ch)
  2776. {
  2777. lynx::NoteEvent on;
  2778. on.channel = ch;
  2779. on.velocity = 1.0f;
  2780. live.push (on);
  2781. }
  2782. std::vector<float> l (1000), rr (1000); // larger than the mixer block: tests the chunking
  2783. r.process (state.get(), false, 0.0, 1000, l.data(), rr.data(), &live, meters);
  2784. return std::max (std::abs (l[999]), std::abs (rr[999]));
  2785. }
  2786. } // namespace
  2787. TEST (mixer_routes_mutes_solos_and_meters)
  2788. {
  2789. // two 0.5 channels summed on the Master at 0 dB
  2790. CHECK (std::abs (renderMixerCase ([] (lynx::Song&) {}) - 1.0f) < 1e-4f);
  2791. // insert 1 muted: only channel 2 remains
  2792. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.mixer[1].muted = true; }) - 0.5f) < 1e-4f);
  2793. // solo on insert 2: same
  2794. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.mixer[2].solo = true; }) - 0.5f) < 1e-4f);
  2795. // FL fader curve: 0.8 = 0 dB, max ≈ +5.6 dB, half travel ≈ -12.7 dB
  2796. CHECK (std::abs (lynx::faderGain (0.8f) - 1.0f) < 1e-5f);
  2797. CHECK (std::abs (juce_db (lynx::faderGain (1.0f)) - 5.59f) < 0.05f);
  2798. CHECK (lynx::faderGain (0.0f) == 0.0f);
  2799. // Master fader at max
  2800. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.mixer[0].volume = 1.0f; }) - lynx::faderGain (1.0f)) < 1e-4f);
  2801. // both channels on the same insert, insert fader at 0.4
  2802. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) {
  2803. s.channels[1].mixerTrack = 1;
  2804. s.mixer[1].volume = 0.4f;
  2805. }) - lynx::faderGain (0.4f)) < 1e-4f);
  2806. // a route outside the mixer falls to the last existing insert, without crashing
  2807. CHECK (renderMixerCase ([] (lynx::Song& s) { s.channels[0].mixerTrack = 100; }) > 0.9f);
  2808. // no mixer: everything straight to the output at 0 dB
  2809. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.mixer.clear(); }) - 1.0f) < 1e-4f);
  2810. lynx::MeterBank meters;
  2811. renderMixerCase ([] (lynx::Song&) {}, &meters);
  2812. CHECK (std::abs (meters.takePeak (1, 0) - 0.5f) < 1e-4f);
  2813. CHECK (std::abs (meters.takePeak (0, 1) - 1.0f) < 1e-4f);
  2814. CHECK (meters.takePeak (1, 0) == 0.0f); // reading resets
  2815. CHECK (meters.takePeak (3, 0) == 0.0f); // insert with no channel
  2816. }
  2817. TEST (next_free_insert_and_flp_mixer_import)
  2818. {
  2819. auto s = lynx::Song::makeDefault();
  2820. CHECK (s.mixer.size() == 17 && s.mixer[0].name == "Master");
  2821. CHECK (s.nextFreeInsert() == 6); // the 5 default channels take 1..5
  2822. flp::Project p;
  2823. p.channels = { { 0, flp::ChannelType::sampler, "Kick", "", "", 30 } };
  2824. p.mixer = { { 0, "" }, { 30, "Drums" } };
  2825. const auto song = lynx::importFlp (p);
  2826. CHECK (song.channels[0].mixerTrack == 30);
  2827. CHECK (song.mixer.size() == 31);
  2828. CHECK (song.mixer[30].name == "Drums" && song.mixer[0].name == "Master");
  2829. }
  2830. namespace
  2831. {
  2832. float runEffectOnSine (lynx::EffectKind kind, const std::function<void (lynx::EffectSlot&)>& tweak, double hz, float amp,
  2833. int frames = 48000)
  2834. {
  2835. auto fx = lynx::makeEffect (kind);
  2836. fx->prepare (48000.0);
  2837. auto slot = lynx::makeEffectSlot (kind);
  2838. tweak (slot);
  2839. std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
  2840. for (int i = 0; i < frames; ++i)
  2841. l[(std::size_t) i] = r[(std::size_t) i] = amp * (float) std::sin (6.283185307 * hz * i / 48000.0);
  2842. for (int i = 0; i < frames; i += 480)
  2843. fx->process (l.data() + i, r.data() + i, std::min (480, frames - i), slot.params, 120.0);
  2844. float peak = 0.0f;
  2845. for (int i = frames / 2; i < frames; ++i) // second half: already settled
  2846. {
  2847. CHECK (std::isfinite (l[(std::size_t) i]));
  2848. peak = std::max (peak, std::abs (l[(std::size_t) i]));
  2849. }
  2850. return peak;
  2851. }
  2852. } // namespace
  2853. TEST (parametric_eq_bands_and_response)
  2854. {
  2855. // default: transparent
  2856. auto slot = lynx::makeEffectSlot (lynx::EffectKind::parametricEq);
  2857. CHECK (std::abs (lynx::peq::responseDb (slot.params, 48000.0, 1000.0)) < 0.01);
  2858. CHECK (std::abs (runEffectOnSine (lynx::EffectKind::parametricEq, [] (auto&) {}, 1000.0, 0.5f) - 0.5f) < 0.005f);
  2859. // +12 dB peak at 1 kHz on band 4: the response and the signal agree
  2860. auto boost = [] (lynx::EffectSlot& s) {
  2861. s.params[(std::size_t) lynx::peq::param (3, 1)] = lynx::peq::hzValue (1000.0);
  2862. s.params[(std::size_t) lynx::peq::param (3, 2)] = lynx::peq::gainValue (12.0);
  2863. };
  2864. boost (slot);
  2865. CHECK (std::abs (lynx::peq::responseDb (slot.params, 48000.0, 1000.0) - 12.0) < 0.2);
  2866. CHECK (std::abs (lynx::peq::responseDb (slot.params, 48000.0, 100.0)) < 1.0);
  2867. const auto peak = runEffectOnSine (lynx::EffectKind::parametricEq, boost, 1000.0, 0.1f);
  2868. CHECK (std::abs (peak - 0.1f * std::pow (10.0f, 12.0f / 20.0f)) < 0.01f);
  2869. // low cut at 48 dB/oct at 200 Hz: 50 Hz (2 octaves below) drops ~96 dB; 2 kHz passes
  2870. auto cut = lynx::makeEffectSlot (lynx::EffectKind::parametricEq);
  2871. cut.params[(std::size_t) lynx::peq::param (0, 0)] = lynx::peq::typeValue (lynx::peq::BandType::lowCut);
  2872. cut.params[(std::size_t) lynx::peq::param (0, 1)] = lynx::peq::hzValue (200.0);
  2873. cut.params[(std::size_t) lynx::peq::param (0, 3)] = lynx::peq::qValue (0.70710678); // Butterworth
  2874. cut.params[(std::size_t) (lynx::peq::slopeParam + 0)] = 1.0f;
  2875. CHECK (lynx::peq::responseDb (cut.params, 48000.0, 50.0) < -80.0);
  2876. CHECK (std::abs (lynx::peq::responseDb (cut.params, 48000.0, 200.0) + 3.0) < 0.5); // -3 dB at the cutoff (Butterworth)
  2877. CHECK (std::abs (lynx::peq::responseDb (cut.params, 48000.0, 2000.0)) < 0.1);
  2878. // round-trip conversions
  2879. CHECK (std::abs (lynx::peq::hz (lynx::peq::hzValue (440.0)) - 440.0) < 0.5);
  2880. CHECK (std::abs (lynx::peq::q (lynx::peq::qValue (2.5)) - 2.5) < 0.01);
  2881. CHECK (lynx::peq::type (lynx::peq::typeValue (lynx::peq::BandType::notch)) == lynx::peq::BandType::notch);
  2882. }
  2883. TEST (fl_parametric_eq2_state_maps_to_bands)
  2884. {
  2885. // v8 blob: band 1 low cut at 100 Hz "Steep 8", band 4 peak +6 dB at 1 kHz, output −3 dB
  2886. std::vector<std::uint8_t> blob (354, 0);
  2887. auto put = [&] (std::size_t off, std::int32_t v) { std::memcpy (blob.data() + off, &v, 4); };
  2888. put (0, 8);
  2889. auto param = [&] (int i, std::int32_t v) { put (4 + 4 * (std::size_t) i, v); };
  2890. auto hzRaw = [] (double hz) { return (std::int32_t) std::lround (std::log (hz / 20.0) / std::log (1000.0) * 65536.0); };
  2891. for (int b = 0; b < 7; ++b)
  2892. param (21 + b, 0); // all off
  2893. param (21 + 0, 3); // high pass (= low cut)
  2894. param (7 + 0, hzRaw (100.0));
  2895. param (14 + 0, 40124); // default width -> Q ~0.68
  2896. param (28 + 0, -3); // Steep 8 -> 4 biquads
  2897. param (21 + 3, 6); // peaking
  2898. param (7 + 3, hzRaw (1000.0));
  2899. param (3, 600); // +6 dB
  2900. param (14 + 3, 25412);
  2901. param (35, -300); // main level −3 dB
  2902. put (0x13E, 0x7F);
  2903. const auto slot = lynx::effectFromFl ("Fruity Parametric EQ 2", blob);
  2904. CHECK (slot.has_value() && slot->kind == lynx::EffectKind::parametricEq);
  2905. const auto& p = slot->params;
  2906. CHECK (lynx::peq::type (p[(std::size_t) lynx::peq::param (0, 0)]) == lynx::peq::BandType::lowCut);
  2907. CHECK (lynx::peq::slopeStages (p[(std::size_t) lynx::peq::slopeParam]) == 4);
  2908. CHECK (lynx::peq::type (p[(std::size_t) lynx::peq::param (3, 0)]) == lynx::peq::BandType::peak);
  2909. CHECK (lynx::peq::type (p[(std::size_t) lynx::peq::param (1, 0)]) == lynx::peq::BandType::off);
  2910. CHECK (std::abs (lynx::peq::responseDb (p, 48000.0, 1000.0) - 3.0) < 0.3); // +6 from the peak −3 from the output
  2911. CHECK (lynx::peq::responseDb (p, 48000.0, 30.0) < -40.0); // 48 dB/oct below 100 Hz
  2912. CHECK (std::abs (lynx::peq::responseDb (p, 48000.0, 8000.0) + 3.0) < 0.3);
  2913. // a band muted by the mask doesn't sound
  2914. put (0x13E, 0x7F & ~(1 << 3));
  2915. const auto muted = lynx::effectFromFl ("Fruity Parametric EQ 2", blob);
  2916. CHECK (lynx::peq::type (muted->params[(std::size_t) lynx::peq::param (3, 0)]) == lynx::peq::BandType::off);
  2917. }
  2918. TEST (reverb_low_cut_and_predelay)
  2919. {
  2920. // impulse: with 100 ms predelay the wet only starts later; with a 3 kHz low cut a
  2921. // 100 Hz sine barely enters the reverb
  2922. auto run = [] (const std::function<void (lynx::EffectSlot&)>& tweak, double hz, bool impulse) {
  2923. auto fx = lynx::makeEffect (lynx::EffectKind::reverb);
  2924. fx->prepare (48000.0);
  2925. auto slot = lynx::makeEffectSlot (lynx::EffectKind::reverb);
  2926. slot.params[4] = 0.0f; // wet only
  2927. slot.params[3] = 1.0f;
  2928. tweak (slot);
  2929. std::vector<float> l (48000), r (48000);
  2930. for (int i = 0; i < 48000; ++i)
  2931. l[(std::size_t) i] = r[(std::size_t) i] = impulse ? (i == 0 ? 1.0f : 0.0f) : 0.5f * (float) std::sin (6.283185307 * hz * i / 48000.0);
  2932. for (int i = 0; i < 48000; i += 480)
  2933. fx->process (l.data() + i, r.data() + i, 480, slot.params, 120.0);
  2934. return l;
  2935. };
  2936. const auto delayed = run ([] (auto& s) { s.params[7] = 0.1f; }, 0.0, true);
  2937. float early = 0.0f, later = 0.0f;
  2938. for (std::size_t i = 0; i < 4800; ++i)
  2939. early = std::max (early, std::abs (delayed[i]));
  2940. for (std::size_t i = 4800; i < 24000; ++i)
  2941. later = std::max (later, std::abs (delayed[i]));
  2942. CHECK (early == 0.0f && later > 0.0f);
  2943. auto energy = [] (const std::vector<float>& b) { double e = 0.0; for (std::size_t i = 24000; i < 48000; ++i) e += (double) b[i] * b[i]; return e; };
  2944. const auto open = energy (run ([] (auto&) {}, 100.0, false));
  2945. const auto cut = energy (run ([] (auto& s) { s.params[5] = 1.0f; }, 100.0, false));
  2946. CHECK (cut < open * 0.01);
  2947. }
  2948. TEST (utility_effect_and_fl_stereo_tools)
  2949. {
  2950. // transparent by default; balance fully right silences the left
  2951. CHECK (std::abs (runEffectOnSine (lynx::EffectKind::utility, [] (auto&) {}, 440.0, 0.5f) - 0.5f) < 0.001f);
  2952. CHECK (runEffectOnSine (lynx::EffectKind::utility, [] (auto& s) { s.params[1] = 1.0f; }, 440.0, 0.5f) < 0.001f);
  2953. // Stereo Shaper in "mono": each output = 0.5 L + 0.5 R
  2954. std::vector<std::uint8_t> blob (36, 0);
  2955. auto put = [&] (std::size_t off, std::int32_t v) { std::memcpy (blob.data() + off, &v, 4); };
  2956. put (4, 6400); put (8, 6400); put (12, 6400); put (16, 6400);
  2957. const auto slot = lynx::effectFromFl ("Fruity Stereo Shaper", blob);
  2958. CHECK (slot && slot->kind == lynx::EffectKind::utility);
  2959. auto fx = lynx::makeEffect (lynx::EffectKind::utility);
  2960. float l = 1.0f, r = 0.0f;
  2961. fx->process (&l, &r, 1, slot->params, 120.0);
  2962. CHECK (std::abs (l - 0.5f) < 1e-4f && std::abs (r - 0.5f) < 1e-4f);
  2963. // Balance: volume 256 = 0 dB, pan −128 = fully left
  2964. std::vector<std::uint8_t> bal (8, 0);
  2965. std::int32_t pan = -128, vol = 256;
  2966. std::memcpy (bal.data(), &pan, 4);
  2967. std::memcpy (bal.data() + 4, &vol, 4);
  2968. const auto b = lynx::effectFromFl ("Fruity Balance", bal);
  2969. l = 0.5f; r = 0.5f;
  2970. fx->process (&l, &r, 1, b->params, 120.0);
  2971. CHECK (std::abs (l - 0.5f) < 1e-3f && std::abs (r) < 1e-4f);
  2972. }
  2973. TEST (effects_eq_compressor_behave)
  2974. {
  2975. // EQ at default is transparent
  2976. CHECK (std::abs (runEffectOnSine (lynx::EffectKind::eq, [] (auto&) {}, 1000.0, 0.5f) - 0.5f) < 0.01f);
  2977. // +18 dB on the lows: 60 Hz rises a lot, 5 kHz barely
  2978. CHECK (runEffectOnSine (lynx::EffectKind::eq, [] (auto& s) { s.params[0] = 1.0f; }, 60.0, 0.05f) > 0.3f);
  2979. CHECK (runEffectOnSine (lynx::EffectKind::eq, [] (auto& s) { s.params[0] = 1.0f; }, 5000.0, 0.05f) < 0.06f);
  2980. // compressor: threshold -20 dB, ratio 20:1 flattens a 0 dBFS signal to near the threshold
  2981. const auto squashed = runEffectOnSine (lynx::EffectKind::compressor, [] (auto& s) {
  2982. s.params[0] = 40.0f / 60.0f;
  2983. s.params[1] = 1.0f;
  2984. }, 200.0, 1.0f);
  2985. CHECK (squashed < 0.2f && squashed > 0.05f);
  2986. // a signal below the threshold passes intact
  2987. CHECK (std::abs (runEffectOnSine (lynx::EffectKind::compressor, [] (auto& s) { s.params[0] = 1.0f; }, 200.0, 0.3f) - 0.3f) < 0.01f);
  2988. }
  2989. TEST (effects_delay_echoes_on_tempo_and_reverb_has_tail)
  2990. {
  2991. auto delay = lynx::makeEffect (lynx::EffectKind::delay);
  2992. delay->prepare (48000.0);
  2993. auto slot = lynx::makeEffectSlot (lynx::EffectKind::delay);
  2994. slot.params[0] = 3.5f / 8.0f; // index 3 = 1/4 = 0.5 s at 120 BPM
  2995. slot.params[1] = 0.0f; // no feedback: a single echo
  2996. slot.params[2] = 1.0f;
  2997. slot.params[4] = 0.0f; // no damping
  2998. std::vector<float> l (48000, 0.0f), r (48000, 0.0f);
  2999. l[0] = r[0] = 1.0f;
  3000. delay->process (l.data(), r.data(), 48000, slot.params, 120.0);
  3001. CHECK (std::abs (l[24000] - 1.0f) < 1e-4f);
  3002. CHECK (std::abs (l[12000]) < 1e-6f);
  3003. auto reverb = lynx::makeEffect (lynx::EffectKind::reverb);
  3004. reverb->prepare (48000.0);
  3005. auto rs = lynx::makeEffectSlot (lynx::EffectKind::reverb);
  3006. rs.params[3] = 1.0f;
  3007. std::fill (l.begin(), l.end(), 0.0f);
  3008. std::fill (r.begin(), r.end(), 0.0f);
  3009. l[0] = r[0] = 1.0f;
  3010. reverb->process (l.data(), r.data(), 48000, rs.params, 120.0);
  3011. float tail = 0.0f;
  3012. for (int i = 4800; i < 9600; ++i) // between 100 and 200 ms after the impulse
  3013. tail = std::max (tail, std::abs (l[(std::size_t) i]));
  3014. CHECK (tail > 1e-4f && tail < 1.0f);
  3015. CHECK (lynx::formatEffectParam (lynx::EffectKind::delay, 0, slot.params[0], 120.0) == "1/4 (500 ms)");
  3016. }
  3017. TEST (rack_runs_insert_effects_and_reuses_them)
  3018. {
  3019. // an insert with EQ at -inf doesn't exist; we use the EQ output at -18 dB to see the effect act
  3020. const auto quiet = renderMixerCase ([] (lynx::Song& s) {
  3021. auto eq = lynx::makeEffectSlot (lynx::EffectKind::eq);
  3022. eq.params[4] = 0.0f; // output -18 dB
  3023. s.mixer[1].effects.push_back (eq);
  3024. });
  3025. CHECK (quiet > 0.55f && quiet < 0.6f); // 0.5 from channel 2 + 0.5 * 0.126 from channel 1
  3026. const auto bypassed = renderMixerCase ([] (lynx::Song& s) {
  3027. auto eq = lynx::makeEffectSlot (lynx::EffectKind::eq);
  3028. eq.params[4] = 0.0f;
  3029. eq.enabled = false;
  3030. s.mixer[1].effects.push_back (eq);
  3031. });
  3032. CHECK (std::abs (bypassed - 1.0f) < 1e-4f);
  3033. auto s = lynx::Song::makeDefault();
  3034. s.mixer[2].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::reverb));
  3035. auto a = lynx::RackState::build (s, nullptr, 48000.0);
  3036. s.mixer[2].effects[0].params[0] = 0.9f; // turning a knob doesn't recreate the effect
  3037. auto b = lynx::RackState::build (s, a.get(), 48000.0);
  3038. CHECK (b->effects[2][0] == a->effects[2][0]);
  3039. s.mixer[2].effects[0] = lynx::makeEffectSlot (lynx::EffectKind::delay);
  3040. auto c = lynx::RackState::build (s, b.get(), 48000.0);
  3041. CHECK (c->effects[2][0] != b->effects[2][0]);
  3042. }
  3043. TEST (audio_clips_trigger_cut_and_resume)
  3044. {
  3045. lynx::Song s;
  3046. s.bpm = 120.0; // 1 beat = 0.5 s = 24000 samples at 48 kHz
  3047. lynx::ChannelDef c;
  3048. c.instrument = lynx::InstrumentKind::audioClip;
  3049. c.samplePath = "ramp";
  3050. c.volume = 1.0f;
  3051. s.channels.push_back (c);
  3052. s.patterns.emplace_back();
  3053. lynx::Clip clip;
  3054. clip.kind = lynx::ClipKind::audio;
  3055. clip.channel = 0;
  3056. clip.start = lynx::ppq; // starts at beat 2
  3057. clip.length = 2 * lynx::ppq; // lasts 2 beats
  3058. clip.offset = lynx::ppq; // skips the first beat of the audio
  3059. s.playlist.push_back (clip);
  3060. s.mode = lynx::PlayMode::song;
  3061. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  3062. lynx::EventBuffer ev;
  3063. lynx::scheduleSong (s, 0.0, 4 * 24000, tps, ev);
  3064. CHECK (ev.size() == 2);
  3065. CHECK (ev[0].type == lynx::NoteEvent::Type::on && ev[0].sampleOffset == 24000 && std::abs (ev[0].startSeconds - 0.5) < 1e-9);
  3066. CHECK (ev[1].type == lynx::NoteEvent::Type::off && ev[1].sampleOffset == 3 * 24000);
  3067. // seek to the middle of the clip (beat 2.5): with resume, starts at the block's first sample
  3068. ev.clear();
  3069. lynx::scheduleSong (s, 1.5 * lynx::ppq, 256, tps, ev, true);
  3070. CHECK (ev.size() == 1 && ev[0].sampleOffset == 0 && std::abs (ev[0].startSeconds - 0.75) < 1e-9);
  3071. ev.clear();
  3072. lynx::scheduleSong (s, 1.5 * lynx::ppq, 256, tps, ev, false);
  3073. CHECK (ev.size() == 0);
  3074. // sample whose value = position in seconds: lets us check where playback started
  3075. auto provider = [] (const std::string&) {
  3076. auto d = std::make_shared<lynx::SampleData>();
  3077. d->sampleRate = 48000.0;
  3078. for (int i = 0; i < 48000 * 3; ++i)
  3079. d->left.push_back ((float) i / 48000.0f);
  3080. return std::shared_ptr<const lynx::SampleData> (d);
  3081. };
  3082. auto state = lynx::RackState::build (s, nullptr, 48000.0, provider);
  3083. lynx::RackRenderer r;
  3084. r.prepare (48000.0, 512);
  3085. std::vector<float> l (256), rr (256);
  3086. // first block already playing mid-clip
  3087. r.process (state.get(), true, 1.5 * lynx::ppq, 256, l.data(), rr.data());
  3088. CHECK (std::abs (l[0] - 0.75f) < 0.001f);
  3089. // the clip ends at beat 3: after the short fade, silence (the sample would still have audio)
  3090. double tick = 3.0 * lynx::ppq - 10 * tps;
  3091. r.process (state.get(), false, tick, 256, l.data(), rr.data()); // stops (fade)
  3092. for (int i = 0; i < 4; ++i)
  3093. r.process (state.get(), false, tick, 256, l.data(), rr.data());
  3094. CHECK (std::abs (l[255]) < 1e-6f);
  3095. }
  3096. TEST (remove_channel_fixes_notes_and_audio_clips)
  3097. {
  3098. lynx::Song s;
  3099. s.channels.resize (3);
  3100. s.patterns.resize (1);
  3101. s.patterns[0].notes = { { 0, 240, 0, 60, 1.0f }, { 0, 240, 1, 60, 1.0f }, { 0, 240, 2, 60, 1.0f } };
  3102. lynx::Clip a;
  3103. a.kind = lynx::ClipKind::audio;
  3104. a.channel = 1;
  3105. a.length = 100;
  3106. lynx::Clip b = a;
  3107. b.channel = 2;
  3108. s.playlist = { a, b };
  3109. s.removeChannel (1);
  3110. CHECK (s.channels.size() == 2);
  3111. CHECK (s.patterns[0].notes.size() == 2 && s.patterns[0].notes[1].channel == 1);
  3112. CHECK (s.playlist.size() == 1 && s.playlist[0].channel == 1);
  3113. }
  3114. TEST (flp_import_audio_clips_with_offsets)
  3115. {
  3116. flp::Project p;
  3117. p.header.ppq = 96;
  3118. p.tempo = 120.0;
  3119. p.channels = { { 0, flp::ChannelType::audioClip, "Vocal", "", "%FLStudioData%\\v.wav", 2 },
  3120. { 1, flp::ChannelType::audioClip, "Loop", "", "%FLStudioData%\\l.wav", 3 } };
  3121. flp::Arrangement arr;
  3122. flp::PlaylistItem beats; // offset in beats: end - start = duration in beats
  3123. beats.kind = flp::PlaylistItem::Kind::channelClip;
  3124. beats.index = 0;
  3125. beats.position = 192;
  3126. beats.length = 384; // 4 beats
  3127. beats.startOffset = 2.0f;
  3128. beats.endOffset = 6.0f;
  3129. flp::PlaylistItem ms = beats; // offset in ms: 4 beats at 120 BPM = 2000 ms
  3130. ms.index = 1;
  3131. ms.startOffset = 500.0f;
  3132. ms.endOffset = 2500.0f;
  3133. arr.items = { beats, ms };
  3134. p.arrangements.push_back (arr);
  3135. lynx::FlpImportOptions options;
  3136. options.roots = { "C:\\FL", "C:\\FL\\Data", "" };
  3137. options.fileExists = [] (const std::string&) { return true; };
  3138. const auto s = lynx::importFlp (p, options);
  3139. CHECK (s.channels.size() == 2 && s.channels[0].instrument == lynx::InstrumentKind::audioClip);
  3140. CHECK (s.channels[0].samplePath == "C:\\FL\\Data\\v.wav");
  3141. CHECK (s.playlist.size() == 2);
  3142. CHECK (s.playlist[0].kind == lynx::ClipKind::audio && s.playlist[0].start == 2 * lynx::ppq);
  3143. CHECK (s.playlist[0].offset == 2 * lynx::ppq);
  3144. CHECK (s.playlist[1].offset == lynx::ppq); // 500 ms at 120 BPM = 1 beat
  3145. CHECK (s.mode == lynx::PlayMode::song);
  3146. }
  3147. TEST (plugin_factories_create_reuse_and_fallback)
  3148. {
  3149. lynx::Song s;
  3150. s.mixer = lynx::Song::defaultMixer (2);
  3151. lynx::ChannelDef c;
  3152. c.instrument = lynx::InstrumentKind::plugin;
  3153. c.plugin.id = "VST3-Synth-123";
  3154. c.mixerTrack = 1;
  3155. s.channels.push_back (c);
  3156. lynx::ChannelDef missing = c;
  3157. missing.plugin.id = "VST3-Ausente-0";
  3158. s.channels.push_back (missing);
  3159. lynx::EffectSlot fx;
  3160. fx.kind = lynx::EffectKind::plugin;
  3161. fx.plugin.id = "VST3-Reverb-9";
  3162. s.mixer[1].effects.push_back (fx);
  3163. s.patterns.emplace_back();
  3164. int instrumentsMade = 0, effectsMade = 0;
  3165. lynx::RackState::Factories f;
  3166. f.pluginInstruments = [&] (const lynx::PluginRef& ref, double) -> std::shared_ptr<lynx::Instrument> {
  3167. if (ref.id != "VST3-Synth-123")
  3168. return nullptr;
  3169. ++instrumentsMade;
  3170. auto synth = lynx::makeInstrument (lynx::InstrumentKind::synth);
  3171. synth->prepare (48000.0);
  3172. return synth;
  3173. };
  3174. f.pluginEffects = [&] (const lynx::PluginRef&, double) -> std::shared_ptr<lynx::Effect> {
  3175. ++effectsMade;
  3176. auto eq = lynx::makeEffect (lynx::EffectKind::eq);
  3177. eq->prepare (48000.0);
  3178. return eq;
  3179. };
  3180. auto a = lynx::RackState::build (s, nullptr, 48000.0, f);
  3181. CHECK (instrumentsMade == 1 && effectsMade == 1);
  3182. CHECK (a->instruments.size() == 2 && a->instruments[1] != nullptr); // missing: muted, but it exists
  3183. // changing the saved state doesn't recreate; swapping the plugin does
  3184. s.channels[0].plugin.state = "abc";
  3185. auto b = lynx::RackState::build (s, a.get(), 48000.0, f);
  3186. CHECK (instrumentsMade == 1 && b->instruments[0] == a->instruments[0] && b->effects[1][0] == a->effects[1][0]);
  3187. s.mixer[1].effects[0].plugin.id = "VST3-Outro-1";
  3188. auto c2 = lynx::RackState::build (s, b.get(), 48000.0, f);
  3189. CHECK (effectsMade == 2);
  3190. // rendering with a missing plugin and a plugin slot breaks nothing
  3191. lynx::RackRenderer r;
  3192. r.prepare (48000.0, 256);
  3193. lynx::LiveEventQueue live;
  3194. lynx::NoteEvent on;
  3195. on.channel = 0;
  3196. on.velocity = 1.0f;
  3197. live.push (on);
  3198. on.channel = 1;
  3199. live.push (on);
  3200. std::vector<float> l (256), rr (256);
  3201. r.process (c2.get(), false, 0.0, 256, l.data(), rr.data(), &live);
  3202. float peak = 0.0f;
  3203. for (auto v : l)
  3204. {
  3205. CHECK (std::isfinite (v));
  3206. peak = std::max (peak, std::abs (v));
  3207. }
  3208. CHECK (peak > 0.0f);
  3209. }
  3210. TEST (export_song_adds_silent_tail_and_never_restarts)
  3211. {
  3212. auto s = lynx::Song::makeDefault();
  3213. s.playlist.clear();
  3214. std::int64_t length = 0;
  3215. auto pat = lynx::makeExportSong (s, true, lynx::ppq * 8, length); // empty Playlist: uses the pattern
  3216. CHECK (length == lynx::ppq * 4);
  3217. CHECK (pat.mode == lynx::PlayMode::song && pat.playlist.size() == 2);
  3218. CHECK (pat.songLength() >= length + lynx::ppq * 8);
  3219. // no new notes after the song ends, only silence until the end of the tail
  3220. lynx::EventBuffer events;
  3221. const auto tps = lynx::ticksPerSample (pat.bpm, 48000.0);
  3222. int onsAfterEnd = 0, onsBefore = 0;
  3223. for (double tick = 0.0; tick < (double) (length + lynx::ppq * 8); tick += 512 * tps)
  3224. {
  3225. events.clear();
  3226. lynx::scheduleSong (pat, tick, 512, tps, events);
  3227. for (const auto& e : events)
  3228. if (e.type == lynx::NoteEvent::Type::on)
  3229. (tick + e.sampleOffset * tps >= (double) length ? onsAfterEnd : onsBefore)++;
  3230. }
  3231. CHECK (onsBefore == (int) s.patterns[0].notes.size());
  3232. CHECK (onsAfterEnd == 0);
  3233. // with Playlist: the end is the last clip's, without rounding
  3234. s.playlist.push_back ({ 0, 0, lynx::ppq * 2, lynx::ppq * 3, 0 });
  3235. auto song = lynx::makeExportSong (s, true, 0, length);
  3236. CHECK (length == lynx::ppq * 5 && song.playlist.size() == 1);
  3237. song = lynx::makeExportSong (s, false, 0, length);
  3238. CHECK (length == lynx::ppq * 4 && song.playlist[0].start == 0);
  3239. }
  3240. TEST (note_edit_selection_operations)
  3241. {
  3242. namespace ne = lynx::noteedit;
  3243. lynx::Pattern p;
  3244. p.notes = { { 0, 240, 0, 60 }, { 480, 240, 0, 64 }, { 960, 480, 1, 67 } };
  3245. // sorting keeps the indices in step
  3246. p.notes[0].start = 1200;
  3247. std::vector<int> tracked { 0, 2 };
  3248. ne::sortTracking (p, tracked);
  3249. CHECK (p.notes[2].start == 1200 && p.notes[(std::size_t) tracked[0]].start == 1200);
  3250. CHECK (p.notes[(std::size_t) tracked[1]].key == 67);
  3251. // group move respects the limits
  3252. std::int64_t dt = -5000;
  3253. int dk = 100;
  3254. ne::move (p, { 0, 1 }, dt, dk);
  3255. CHECK (dt == -480 && dk == 127 - 67 && p.notes[0].start == 0 && p.notes[1].key == 127);
  3256. // duplicate: right after the selection, rounded to the grid
  3257. lynx::Pattern q;
  3258. q.notes = { { 0, 240, 0, 60 }, { 240, 300, 0, 62 } }; // 540-tick selection
  3259. const auto added = ne::duplicate (q, { 0, 1 }, 960);
  3260. CHECK (added.size() == 2 && q.notes[(std::size_t) added[0]].start == 960 && q.notes[(std::size_t) added[1]].start == 1200);
  3261. // paste onto another channel with an offset
  3262. const auto pasted = ne::paste (q, ne::copy (q, { 1 }), 100, 3);
  3263. CHECK (pasted.size() == 1 && q.notes.back().channel == 3 && q.notes.back().start == 340);
  3264. // quantize rounds to the nearest
  3265. q.notes[0].start = 130;
  3266. q.notes[1].start = 100;
  3267. ne::quantize (q, { 0, 1 }, 240);
  3268. CHECK (q.notes[0].start == 240 && q.notes[1].start == 0);
  3269. // remove several (order and duplicates don't matter)
  3270. const auto before = q.notes.size();
  3271. ne::remove (q, { 1, 0, 1 });
  3272. CHECK (q.notes.size() == before - 2);
  3273. }
  3274. TEST (note_edit_quick_operations)
  3275. {
  3276. namespace ne = lynx::noteedit;
  3277. const std::int64_t step = lynx::ticksPerStep, bar = lynx::ppq * 4;
  3278. // split: only strictly inside; the tail is appended and does not slide again
  3279. lynx::Pattern p;
  3280. p.notes = { { 0, 4 * step, 0, 60 } };
  3281. p.notes[0].slide = true;
  3282. CHECK (ne::split (p, 0, 0) == -1 && ne::split (p, 0, 4 * step) == -1);
  3283. CHECK (ne::split (p, 0, step) == 1 && p.notes[0].length == step && p.notes[1].start == step && p.notes[1].length == 3 * step
  3284. && ! p.notes[1].slide);
  3285. // chop to grid: a 2.5-step note from half a step becomes 3 pieces cut on the step lines
  3286. lynx::Pattern c;
  3287. c.notes = { { step / 2, step * 5 / 2, 0, 60 }, { 0, step, 1, 50 } };
  3288. const auto pieces = ne::chop (c, { 0, 1 }, step);
  3289. CHECK (pieces.size() == 4 && c.notes.size() == 4);
  3290. CHECK (c.notes[0].length == step / 2 && c.notes[2].start == step && c.notes[2].length == step && c.notes[3].start == 2 * step
  3291. && c.notes[3].length == step);
  3292. // join: touching and overlapping notes of the same key and channel merge; others stay
  3293. lynx::Pattern j;
  3294. j.notes = { { 0, step, 0, 60 }, { step, step, 0, 60 }, { step * 3 / 2, step * 2, 0, 60 }, { 5 * step, step, 0, 60 },
  3295. { step, step, 0, 62 }, { step, step, 1, 60 } };
  3296. CHECK (ne::join (j, { 0, 1, 2, 3, 4, 5 }) == 2 && j.notes.size() == 4);
  3297. CHECK (j.notes[0].start == 0 && j.notes[0].length == step * 7 / 2);
  3298. // legato: each note reaches the next start (a chord ends together); the last keeps its length
  3299. lynx::Pattern l;
  3300. l.notes = { { 0, 10, 0, 60 }, { 0, 10, 0, 64 }, { step, 10, 0, 62 }, { 3 * step, 10, 0, 65 }, { step, 10, 1, 40 } };
  3301. ne::legato (l, { 0, 1, 2, 3, 4 });
  3302. CHECK (l.notes[0].length == step && l.notes[1].length == step && l.notes[2].length == 2 * step && l.notes[3].length == 10
  3303. && l.notes[4].length == 10);
  3304. // shrink to grid and full quantise (start and end)
  3305. ne::shrinkToGrid (l, { 3 }, step / 2);
  3306. CHECK (l.notes[3].length == step / 2);
  3307. lynx::Pattern q;
  3308. q.notes = { { 10, step - 30, 0, 60 }, { step + 100, 20, 0, 60 } };
  3309. ne::quantizeFull (q, { 0, 1 }, step);
  3310. CHECK (q.notes[0].start == 0 && q.notes[0].length == step && q.notes[1].start == step && q.notes[1].length == step);
  3311. // rotate inside the bar, wrapping
  3312. lynx::Pattern r;
  3313. r.notes = { { 0, step, 0, 60 }, { bar - step, step, 0, 62 } };
  3314. ne::rotate (r, { 0, 1 }, step, bar, 1);
  3315. CHECK (r.notes[0].start == step && r.notes[1].start == 0);
  3316. ne::rotate (r, { 0, 1 }, step, bar, -2);
  3317. CHECK (r.notes[0].start == bar - step && r.notes[1].start == bar - 2 * step);
  3318. r.notes.push_back ({ bar + step, step, 0, 64 }); // alone in the second bar: wraps inside that bar
  3319. ne::rotate (r, { 2 }, step, bar, -2);
  3320. CHECK (r.notes[2].start == 2 * bar - step);
  3321. }
  3322. TEST (note_edit_grid_and_time_text)
  3323. {
  3324. namespace ne = lynx::noteedit;
  3325. const std::int64_t step = lynx::ticksPerStep, bar = lynx::ppq * 4;
  3326. CHECK (ne::snapDown (250.0, step) == step && ne::snapDown (-1.0, step) == -step && ne::snapDown (17.9, 1) == 17);
  3327. CHECK (ne::snapNearest (1000.0, { 400, 1100, 980 }, 50.0, step) == 980);
  3328. CHECK (ne::snapNearest (1000.0, { 400 }, 50.0, step) == 960); // too far: falls back to the grid
  3329. // screen grid: 24 px per step shows steps; zoomed out it climbs to beats, bars and bar multiples
  3330. CHECK (ne::screenStep (24.0 / step, bar) == step);
  3331. CHECK (ne::screenStep (96.0 / bar, bar) == lynx::ppq);
  3332. CHECK (ne::screenStep (8.0 / bar, bar) == 2 * bar);
  3333. CHECK (ne::screenStep (200.0 / step, bar) == step / 4);
  3334. CHECK (ne::formatTime (0, 4) == "1:1:0" && ne::formatTime (bar + 5 * step + 7, 4) == "2:6:7");
  3335. CHECK (ne::formatTime (step * 2, 4, true) == "0:2:0");
  3336. std::int64_t t = 0;
  3337. CHECK (ne::parseTime ("2:6:7", 4, t) && t == bar + 5 * step + 7);
  3338. CHECK (ne::parseTime ("3", 4, t) && t == 2 * bar);
  3339. CHECK (ne::parseTime ("0:2:0", 4, t, true) && t == 2 * step);
  3340. CHECK (! ne::parseTime ("0:1:0", 4, t) && ! ne::parseTime ("1:17:0", 4, t) && ! ne::parseTime ("1:1:240", 4, t)
  3341. && ! ne::parseTime ("a", 4, t) && ! ne::parseTime ("", 4, t));
  3342. CHECK (ne::parseTime ("1:12:0", 3, t) && ! ne::parseTime ("1:13:0", 3, t)); // 3/4: 12 steps per bar
  3343. }
  3344. TEST (playlist_clip_edges_and_time_selection)
  3345. {
  3346. namespace ne = lynx::noteedit;
  3347. const std::int64_t beat = lynx::ppq;
  3348. // left-edge trim: content stays in place, cannot reveal what is before the content's start
  3349. lynx::Clip c { 0, 0, 4 * beat, 4 * beat, beat };
  3350. ne::trimStart (c, 5 * beat, beat);
  3351. CHECK (c.start == 5 * beat && c.offset == 2 * beat && c.length == 3 * beat);
  3352. ne::trimStart (c, 0, beat);
  3353. CHECK (c.start == 3 * beat && c.offset == 0 && c.length == 5 * beat);
  3354. ne::trimStart (c, 100 * beat, beat);
  3355. CHECK (c.length == beat && c.end() == 8 * beat);
  3356. // slip moves the content, not the clip
  3357. lynx::Clip s { 0, 0, 0, 2 * beat, beat };
  3358. ne::slip (s, beat / 2);
  3359. CHECK (s.offset == beat / 2 && s.start == 0 && s.length == 2 * beat);
  3360. ne::slip (s, beat);
  3361. CHECK (s.offset == 0);
  3362. ne::slip (s, -3 * beat);
  3363. CHECK (s.offset == 3 * beat);
  3364. // insert / delete time across all tracks, markers included
  3365. std::vector<lynx::Clip> clips { { 0, 0, 0, 2 * beat, 0 }, { 0, 1, 4 * beat, 2 * beat, 0 }, { 0, 2, beat, 6 * beat, 0 } };
  3366. std::vector<lynx::Marker> markers { { 5 * beat, "A" }, { beat / 2, "B" } };
  3367. ne::insertTime (clips, markers, 2 * beat, 4 * beat);
  3368. CHECK (clips[0].start == 0 && clips[1].start == 8 * beat && clips[2].start == beat && markers[0].tick == 9 * beat
  3369. && markers[1].tick == beat / 2);
  3370. std::vector<lynx::Clip> d { { 0, 0, 0, 2 * beat, 0 }, // before: kept
  3371. { 0, 1, 3 * beat, beat, 0 }, // inside: deleted
  3372. { 0, 2, beat, 6 * beat, 0 }, // spans the cut: head + tail
  3373. { 0, 3, 3 * beat, 4 * beat, 0 }, // tail outside: trimmed and moved
  3374. { 0, 4, 8 * beat, beat, 0 } }; // after: moved left
  3375. std::vector<lynx::Marker> dm { { 3 * beat, "in" }, { 9 * beat, "after" } };
  3376. ne::deleteTime (d, dm, 2 * beat, 5 * beat);
  3377. CHECK (d.size() == 5);
  3378. CHECK (d[1].track == 2 && d[1].start == beat && d[1].length == beat);
  3379. CHECK (d[2].track == 2 && d[2].start == 2 * beat && d[2].offset == 4 * beat && d[2].length == 2 * beat);
  3380. CHECK (d[3].track == 3 && d[3].start == 2 * beat && d[3].offset == 2 * beat && d[3].length == 2 * beat);
  3381. CHECK (d[4].start == 5 * beat && dm.size() == 1 && dm[0].tick == 6 * beat);
  3382. // time selection: overlapping clips; Ctrl+B repeats what starts inside it after itself
  3383. std::vector<lynx::Clip> r { { 0, 0, 0, beat, 0 }, { 0, 1, beat, 2 * beat, 0 }, { 0, 0, 4 * beat, beat, 0 } };
  3384. CHECK ((ne::clipsInTime (r, beat / 2, 2 * beat) == std::vector<int> { 0, 1 }));
  3385. const auto added = ne::repeatTime (r, 0, 4 * beat, {});
  3386. CHECK (added.size() == 2 && r[3].start == 4 * beat && r[4].start == 5 * beat && r[4].track == 1);
  3387. CHECK (ne::repeatTime (r, 0, 2 * beat, { 2 }).size() == 1 && r.back().start == 6 * beat);
  3388. }
  3389. TEST (recorder_builds_notes_in_pattern_and_song_mode)
  3390. {
  3391. auto song = lynx::Song::makeDefault();
  3392. const auto loop = song.patterns[0].effectiveLength (4); // 1 bar = 3840
  3393. lynx::NoteRecorder r;
  3394. r.add (song, { 100.0, 4, 60, 0.5f, true }); // no take: ignored
  3395. CHECK (r.takeFinished().empty());
  3396. r.begin (loop);
  3397. r.add (song, { 4000.0, 4, 60, 0.5f, true }); // second loop pass
  3398. r.add (song, { 4480.0, 4, 60, 0.0f, false });
  3399. r.add (song, { 5000.0, 4, 64, 0.9f, true }); // held until stop
  3400. r.end (song, 5240.0);
  3401. const auto notes = r.takeFinished();
  3402. CHECK (notes.size() == 2);
  3403. CHECK (notes[0].start == 4000 - 3840 && notes[0].length == 480 && notes[0].velocity == 0.5f && notes[0].channel == 4);
  3404. CHECK (notes[1].key == 64 && notes[1].start == 5000 - 3840 && notes[1].length == 240);
  3405. // song mode: position inside the current pattern's clip (with offset)
  3406. song.mode = lynx::PlayMode::song;
  3407. song.playlist = { { 0, 0, 7680, 3840, 960 } };
  3408. CHECK (lynx::patternPositionForTick (song, 7680.0 + 100.0, loop) == 1060);
  3409. CHECK (lynx::patternPositionForTick (song, 100.0, loop) == 100); // outside the clip: like loop
  3410. }
  3411. TEST (automation_curve_and_playback)
  3412. {
  3413. lynx::Automation a;
  3414. a.points = { { 0, 0.0f }, { 960, 1.0f }, { 1920, 0.5f, 0.0f } };
  3415. CHECK (a.valueAt (-10.0) == 0.0f && a.valueAt (480.0) == 0.5f && a.valueAt (5000.0) == 0.5f);
  3416. CHECK (a.length() == 1920);
  3417. a.points[0].tension = 1.0f; // curve: starts slow
  3418. CHECK (a.valueAt (480.0) < 0.2f && a.valueAt (480.0) > 0.0f);
  3419. // automated Master: silence in the first bar, sound in the second, and the last value
  3420. // keeps applying after the automation clip ends
  3421. auto song = lynx::Song::makeDefault();
  3422. song.bpm = 120.0;
  3423. song.mode = lynx::PlayMode::song;
  3424. const auto bar = (std::int64_t) lynx::ppq * 4;
  3425. for (int b = 0; b < 3; ++b)
  3426. song.playlist.push_back ({ 0, 0, bar * b, bar, 0 });
  3427. lynx::Automation master;
  3428. master.target = { lynx::AutomationTargetKind::mixerVolume, 0 };
  3429. master.points = { { 0, 0.0f }, { bar - 1, 0.0f }, { bar, 0.8f } };
  3430. song.automations.push_back (master);
  3431. lynx::Clip ac;
  3432. ac.kind = lynx::ClipKind::automation;
  3433. ac.automation = 0;
  3434. ac.track = 1;
  3435. ac.length = bar + bar / 2;
  3436. song.playlist.push_back (ac);
  3437. const double sr = 48000.0;
  3438. auto state = lynx::RackState::build (song, nullptr, sr);
  3439. CHECK (state->hasAutomation && state->mixerVolumeAuto[0] == 0 && state->automationClips[0].size() == 1);
  3440. lynx::RackRenderer r;
  3441. r.prepare (sr, 512);
  3442. std::vector<float> l (512), rr (512);
  3443. const auto tps = lynx::ticksPerSample (song.bpm, sr);
  3444. float peakBar1 = 0.0f, peakBar2 = 0.0f, peakBar3 = 0.0f;
  3445. for (double tick = 0.0; tick < (double) (bar * 3); tick += 512 * tps)
  3446. {
  3447. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  3448. auto& peak = tick < (double) bar - 512 * tps ? peakBar1 : tick < (double) bar * 2 ? peakBar2 : peakBar3;
  3449. for (auto v : l)
  3450. peak = std::max (peak, std::abs (v));
  3451. }
  3452. CHECK (peakBar1 < 1.0e-6f);
  3453. CHECK (peakBar2 > 0.05f);
  3454. CHECK (peakBar3 > 0.05f); // after the clip: keeps the last value
  3455. // target removed: automation disappears from the tables
  3456. song.removeChannel (0);
  3457. song.automations[0].target = { lynx::AutomationTargetKind::channelVolume, 0 };
  3458. song.removeChannel (0);
  3459. CHECK (song.automations[0].target.kind == lynx::AutomationTargetKind::none);
  3460. song.removeAutomation (0);
  3461. CHECK (song.automations.empty() && song.playlist.size() == 3);
  3462. }
  3463. TEST (mixer_sends_between_inserts)
  3464. {
  3465. // insert 1 goes only to 3; 3 goes to the Master: same sum (0.5 + 0.5)
  3466. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.setRoute (1, 3, true); s.setRoute (1, 0, false); }) - 1.0f) < 1e-4f);
  3467. // send at 50% and muted destination
  3468. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.setRoute (1, 3, true, 0.5f); s.setRoute (1, 0, false); }) - 0.75f) < 1e-4f);
  3469. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.setRoute (1, 3, true); s.setRoute (1, 0, false); s.mixer[3].muted = true; }) - 0.5f) < 1e-4f);
  3470. // going to the Master and to 3: the signal doubles
  3471. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.setRoute (1, 3, true); }) - 1.5f) < 1e-4f);
  3472. // solo on 3 lets what reaches it sound (insert 1), but not 2
  3473. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.setRoute (1, 3, true); s.setRoute (1, 0, false); s.mixer[3].solo = true; }) - 0.5f) < 1e-4f);
  3474. // solo on 1 lets the path to the Master (3) sound
  3475. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.setRoute (1, 3, true); s.setRoute (1, 0, false); s.mixer[1].solo = true; }) - 0.5f) < 1e-4f);
  3476. // order and cycles
  3477. auto song = lynx::Song::makeDefault();
  3478. song.setRoute (4, 2, true);
  3479. song.setRoute (2, 1, true);
  3480. CHECK (! song.canRoute (1, 4) && ! song.canRoute (1, 2) && song.canRoute (1, 5));
  3481. song.setRoute (1, 4, true); // ignored: would create a cycle
  3482. CHECK (! song.routes (1, 4));
  3483. const auto order = song.mixerOrder();
  3484. const auto pos = [&] (int t) { return std::find (order.begin(), order.end(), t) - order.begin(); };
  3485. CHECK ((int) order.size() == (int) song.mixer.size() - 1);
  3486. CHECK (pos (4) < pos (2) && pos (2) < pos (1));
  3487. song.setRoute (4, 2, false);
  3488. CHECK (! song.routes (4, 2) && song.routes (4, 0));
  3489. }
  3490. namespace
  3491. {
  3492. // Test effect: delays the signal by `latency` samples and reports that latency (like a lookahead plugin).
  3493. class LatentEffect final : public lynx::Effect
  3494. {
  3495. public:
  3496. explicit LatentEffect (int samples) : latency (samples), line (samples) {}
  3497. void prepare (double) noexcept override {}
  3498. void reset() noexcept override {}
  3499. void process (float* l, float* r, int n, const lynx::EffectParams&, double) noexcept override { line.process (l, r, n); }
  3500. int latencySamples() const noexcept override { return latency; }
  3501. private:
  3502. int latency;
  3503. lynx::DelayLine line;
  3504. };
  3505. } // namespace
  3506. TEST (delay_line_mix_into_matches_process_and_drains_tail)
  3507. {
  3508. // mixInto = process over source*level, added to the destination; in stretches that cross the ring
  3509. lynx::DelayLine a (37), b (37);
  3510. std::vector<float> sl (300), sr (300), dl (300, 0.5f), dr (300, -0.5f), xl (300), xr (300);
  3511. for (int i = 0; i < 300; ++i)
  3512. {
  3513. sl[(std::size_t) i] = (float) std::sin (i * 0.1);
  3514. sr[(std::size_t) i] = (float) std::cos (i * 0.07);
  3515. }
  3516. for (int start = 0; start < 300; start += 100)
  3517. {
  3518. b.mixInto (sl.data() + start, sr.data() + start, 0.25f, dl.data() + start, dr.data() + start, 100);
  3519. for (int i = start; i < start + 100; ++i)
  3520. {
  3521. xl[(std::size_t) i] = sl[(std::size_t) i] * 0.25f;
  3522. xr[(std::size_t) i] = sr[(std::size_t) i] * 0.25f;
  3523. }
  3524. a.process (xl.data() + start, xr.data() + start, 100);
  3525. }
  3526. float worst = 0.0f;
  3527. for (int i = 0; i < 300; ++i)
  3528. worst = std::max ({ worst, std::abs (dl[(std::size_t) i] - 0.5f - xl[(std::size_t) i]),
  3529. std::abs (dr[(std::size_t) i] + 0.5f - xr[(std::size_t) i]) });
  3530. CHECK (worst < 1e-6f);
  3531. // silenced source: the tail still in the line comes out, then nothing remains
  3532. std::vector<float> tl (64, 0.0f), tr (64, 0.0f);
  3533. b.mixInto (nullptr, nullptr, 1.0f, tl.data(), tr.data(), 64);
  3534. CHECK (std::abs (tl[0] - sl[263] * 0.25f) < 1e-6f);
  3535. std::vector<float> zl (64, 0.0f), zr (64, 0.0f);
  3536. b.mixInto (nullptr, nullptr, 1.0f, zl.data(), zr.data(), 64);
  3537. CHECK (*std::max_element (zl.begin(), zl.end()) == 0.0f && *std::min_element (zl.begin(), zl.end()) == 0.0f);
  3538. }
  3539. TEST (limiter_lookahead_holds_ceiling_on_bursts)
  3540. {
  3541. // short bursts over a low floor: the sliding maximum must see each peak before it leaves
  3542. auto fx = lynx::makeEffect (lynx::EffectKind::limiter);
  3543. fx->prepare (48000.0);
  3544. auto slot = lynx::makeEffectSlot (lynx::EffectKind::limiter);
  3545. slot.params[2] = 0.0f; // 10 ms release
  3546. std::vector<float> l (48000), r (48000);
  3547. std::uint32_t seed = 1;
  3548. for (int i = 0; i < 48000; ++i)
  3549. {
  3550. seed = seed * 1664525u + 1013904223u;
  3551. const auto noise = ((float) (seed >> 8) / 16777216.0f - 0.5f) * 0.1f;
  3552. l[(std::size_t) i] = noise + ((i % 9973) < 3 ? 3.0f : 0.0f);
  3553. r[(std::size_t) i] = -noise;
  3554. }
  3555. for (int i = 0; i < 48000; i += 333)
  3556. fx->process (l.data() + i, r.data() + i, std::min (333, 48000 - i), slot.params, 120.0);
  3557. float peak = 0.0f, quiet = 0.0f;
  3558. for (int i = 0; i < 48000; ++i)
  3559. peak = std::max (peak, std::abs (l[(std::size_t) i]));
  3560. for (int i = 9973 * 2 + 2000; i < 9973 * 2 + 2400; ++i) // 40 ms after the burst the gain has already recovered
  3561. quiet = std::max (quiet, std::abs (r[(std::size_t) i]));
  3562. const auto ceiling = 1.0f; // the default ceiling is at 0 dBFS or below
  3563. CHECK (peak <= ceiling + 1e-5f);
  3564. CHECK (quiet > 0.03f);
  3565. }
  3566. TEST (pdc_aligns_paths_through_latent_effects)
  3567. {
  3568. // plan: channel 0 -> insert 1 (effect of 100) -> insert 3; channel 1 -> insert 2 -> Master
  3569. lynx::Song s;
  3570. s.mixer = lynx::Song::defaultMixer (4);
  3571. s.channels.resize (3);
  3572. s.channels[0].mixerTrack = 1;
  3573. s.channels[1].mixerTrack = 2;
  3574. s.channels[2].mixerTrack = 3;
  3575. s.mixer[1].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::eq));
  3576. s.setRoute (1, 3, true);
  3577. s.setRoute (1, 0, false);
  3578. auto plan = lynx::computePdc (s, { 0, 0, 30 }, { {}, { 100 } });
  3579. CHECK (plan.inAlign[1] == 0 && plan.outLat[1] == 100);
  3580. CHECK (plan.inAlign[3] == 100 && plan.channelDelay[2] == 70); // instrument of 30 on insert 3
  3581. CHECK (plan.sendDelay[1][1] == 0); // the slowest path isn't delayed
  3582. CHECK (plan.sendDelay[2][0] == 100 && plan.sendDelay[3][0] == 0 && plan.total == 100);
  3583. // bypass zeroes the slot's latency
  3584. s.mixer[1].effects[0].enabled = false;
  3585. CHECK (lynx::computePdc (s, { 0, 0, 30 }, { {}, { 100 } }).total == 30);
  3586. // rendered: two impulses played together come out together
  3587. lynx::Song r;
  3588. r.mixer = lynx::Song::defaultMixer (2);
  3589. for (int i = 0; i < 2; ++i)
  3590. {
  3591. lynx::ChannelDef c;
  3592. c.instrument = lynx::InstrumentKind::sampler;
  3593. c.samplePath = "impulso";
  3594. c.volume = 1.0f;
  3595. c.mixerTrack = i + 1;
  3596. r.channels.push_back (c);
  3597. }
  3598. r.patterns.emplace_back();
  3599. lynx::EffectSlot fx;
  3600. fx.kind = lynx::EffectKind::plugin;
  3601. fx.plugin.id = "latente";
  3602. r.mixer[1].effects.push_back (fx);
  3603. lynx::RackState::Factories f;
  3604. f.samples = [] (const std::string&) {
  3605. auto d = std::make_shared<lynx::SampleData>();
  3606. d->left.assign (64, 0.0f);
  3607. d->left[0] = 1.0f;
  3608. return std::shared_ptr<const lynx::SampleData> (d);
  3609. };
  3610. f.pluginEffects = [] (const lynx::PluginRef&, double) -> std::shared_ptr<lynx::Effect> { return std::make_shared<LatentEffect> (100); };
  3611. auto state = lynx::RackState::build (r, nullptr, 48000.0, f);
  3612. CHECK (state->pdc.total == 100 && state->sendDelays[2][0] != nullptr && state->sendDelays[2][0]->length() == 100);
  3613. lynx::RackRenderer renderer;
  3614. renderer.prepare (48000.0, 256);
  3615. lynx::LiveEventQueue live;
  3616. for (int ch = 0; ch < 2; ++ch)
  3617. {
  3618. lynx::NoteEvent on;
  3619. on.channel = ch;
  3620. on.key = 60;
  3621. on.velocity = 1.0f;
  3622. live.push (on);
  3623. }
  3624. std::vector<float> l (256), rr (256);
  3625. renderer.process (state.get(), false, 0.0, 256, l.data(), rr.data(), &live);
  3626. int first = -1;
  3627. for (int i = 0; i < 256 && first < 0; ++i)
  3628. if (std::abs (l[(std::size_t) i]) > 0.01f)
  3629. first = i;
  3630. CHECK (first == 100 && std::abs (l[100] - 2.0f) < 1e-3f); // both impulses arrive together, summed
  3631. // editing without changing latencies reuses the lines
  3632. r.channels[0].volume = 0.5f;
  3633. auto next = lynx::RackState::build (r, state.get(), 48000.0, f);
  3634. CHECK (next->sendDelays[2][0] == state->sendDelays[2][0]);
  3635. }
  3636. TEST (mixer_mute_fades_instead_of_clicking)
  3637. {
  3638. lynx::Song s;
  3639. s.mixer = lynx::Song::defaultMixer (2);
  3640. lynx::ChannelDef c;
  3641. c.instrument = lynx::InstrumentKind::sampler;
  3642. c.samplePath = "dc";
  3643. c.volume = 1.0f;
  3644. c.mixerTrack = 1;
  3645. s.channels.push_back (c);
  3646. s.patterns.emplace_back();
  3647. auto provider = [] (const std::string&) {
  3648. auto d = std::make_shared<lynx::SampleData>();
  3649. d->left.assign (48000, 0.5f);
  3650. return std::shared_ptr<const lynx::SampleData> (d);
  3651. };
  3652. auto on = lynx::RackState::build (s, nullptr, 48000.0, provider);
  3653. s.mixer[1].muted = true;
  3654. auto off = lynx::RackState::build (s, on.get(), 48000.0, provider);
  3655. lynx::RackRenderer r;
  3656. r.prepare (48000.0, 256);
  3657. lynx::LiveEventQueue live;
  3658. lynx::NoteEvent note;
  3659. note.velocity = 1.0f;
  3660. live.push (note);
  3661. std::vector<float> l (256), rr (256);
  3662. r.process (on.get(), false, 0.0, 256, l.data(), rr.data(), &live);
  3663. CHECK (std::abs (l[255] - 0.5f) < 1e-3f);
  3664. r.process (off.get(), false, 0.0, 256, l.data(), rr.data());
  3665. CHECK (l[0] > 0.45f && l[128] > 0.2f && l[128] < 0.3f && std::abs (l[255]) < 1e-6f); // ramp down to zero
  3666. r.process (off.get(), false, 0.0, 256, l.data(), rr.data());
  3667. CHECK (std::abs (l[0]) < 1e-9f);
  3668. }
  3669. TEST (dither_quantizes_to_lsb_grid_with_small_error)
  3670. {
  3671. lynx::Dither d (16);
  3672. std::vector<float> x (4800);
  3673. for (std::size_t i = 0; i < x.size(); ++i)
  3674. x[i] = 0.25f * std::sin ((float) i * 0.05f);
  3675. auto y = x;
  3676. d.process (y.data(), (int) y.size(), 0);
  3677. double errorSum = 0.0;
  3678. bool onGrid = true;
  3679. for (std::size_t i = 0; i < x.size(); ++i)
  3680. {
  3681. const auto steps = y[i] * 32768.0f;
  3682. onGrid = onGrid && std::abs (steps - std::round (steps)) < 1e-3f;
  3683. errorSum += std::abs (y[i] - x[i]);
  3684. }
  3685. CHECK (onGrid);
  3686. CHECK (errorSum / (double) x.size() < 4.0 / 32768.0); // a few LSBs of noise on average
  3687. }
  3688. TEST (parallel_rendering_matches_sequential)
  3689. {
  3690. // 32 channels (worth parallelizing), inserts with chained sends
  3691. auto song = lynx::Song::makeDefault();
  3692. song.mixer = lynx::Song::defaultMixer (16);
  3693. for (int i = 5; i < 32; ++i)
  3694. {
  3695. lynx::ChannelDef c;
  3696. c.instrument = i % 2 ? lynx::InstrumentKind::synth : lynx::InstrumentKind::hat;
  3697. c.mixerTrack = 1 + i % 12;
  3698. c.volume = 0.3f;
  3699. song.channels.push_back (c);
  3700. for (int step = i % 4; step < 16; step += 3)
  3701. song.patterns[0].notes.push_back ({ (std::int64_t) step * lynx::ticksPerStep, 200, i, 48 + i, 0.7f });
  3702. }
  3703. song.patterns[0].sortNotes();
  3704. song.setRoute (3, 7, true, 0.6f);
  3705. song.setRoute (7, 9, true, 0.5f);
  3706. song.setRoute (2, 9, true);
  3707. song.mixer[9].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::reverb));
  3708. auto seqState = lynx::RackState::build (song, nullptr, 48000.0);
  3709. auto parState = lynx::RackState::build (song, nullptr, 48000.0);
  3710. CHECK (parState->parallel && parState->mixerLayers.size() == 3);
  3711. lynx::WorkerPool pool (3);
  3712. lynx::RackRenderer seq, par;
  3713. seq.prepare (48000.0, 512);
  3714. par.prepare (48000.0, 512);
  3715. par.setWorkerPool (&pool);
  3716. std::vector<float> l1 (512), r1 (512), l2 (512), r2 (512);
  3717. const auto tps = lynx::ticksPerSample (song.bpm, 48000.0);
  3718. bool same = true;
  3719. float peak = 0.0f;
  3720. for (int block = 0; block < 200; ++block)
  3721. {
  3722. seq.process (seqState.get(), true, block * 512 * tps, 512, l1.data(), r1.data());
  3723. par.process (parState.get(), true, block * 512 * tps, 512, l2.data(), r2.data());
  3724. same = same && l1 == l2 && r1 == r2;
  3725. for (auto v : l1)
  3726. peak = std::max (peak, std::abs (v));
  3727. }
  3728. CHECK (same);
  3729. CHECK (peak > 0.05f);
  3730. // the pool handles many small rounds in a row
  3731. std::atomic<int> sum { 0 };
  3732. auto add = [&sum] (int i) { sum.fetch_add (i + 1); };
  3733. for (int round = 0; round < 2000; ++round)
  3734. pool.parallelFor (5, add);
  3735. CHECK (sum.load() == 2000 * 15);
  3736. }
  3737. TEST (arrangements_switch_and_keep_clip_indices)
  3738. {
  3739. auto s = lynx::Song::makeDefault();
  3740. s.patterns.emplace_back().name = "B";
  3741. s.playlist = { { 1, 0, 0, 960, 0 } };
  3742. s.loopEnd = 960;
  3743. CHECK (s.arrangements.size() == 1 && s.currentArrangement == 0);
  3744. const auto second = s.addArrangement ("Versão curta", true);
  3745. CHECK (second == 1 && s.currentArrangement == 1 && s.playlist.size() == 1 && s.loopEnd == 960);
  3746. s.playlist.push_back ({ 0, 1, 960, 960, 0 });
  3747. s.loopEnd = 0;
  3748. s.selectArrangement (0);
  3749. CHECK (s.playlist.size() == 1 && s.loopEnd == 960 && s.arrangements[1].clips.size() == 2);
  3750. // removing pattern 0 fixes the clips stored in the other arrangement
  3751. s.removePattern (0);
  3752. CHECK (s.playlist.size() == 1 && s.playlist[0].pattern == 0);
  3753. CHECK (s.arrangements[1].clips.size() == 1 && s.arrangements[1].clips[0].pattern == 0);
  3754. s.removeArrangement (0);
  3755. CHECK (s.arrangements.size() == 1 && s.currentArrangement == 0 && s.playlist.size() == 1 && s.loopEnd == 0);
  3756. }
  3757. namespace
  3758. {
  3759. // Test effect that inverts the signal and counts how many times it processed.
  3760. class CountingInverter final : public lynx::Effect
  3761. {
  3762. public:
  3763. void prepare (double) noexcept override {}
  3764. void reset() noexcept override {}
  3765. void process (float* l, float* r, int n, const lynx::EffectParams&, double) noexcept override
  3766. {
  3767. ++calls;
  3768. for (int i = 0; i < n; ++i)
  3769. {
  3770. l[i] = -l[i];
  3771. r[i] = -r[i];
  3772. }
  3773. }
  3774. int calls = 0;
  3775. };
  3776. } // namespace
  3777. TEST (keyboard_controller_outputs_key_value)
  3778. {
  3779. std::vector<std::uint8_t> blob (554, 0);
  3780. const std::uint32_t version = 3;
  3781. std::memcpy (blob.data(), &version, 4);
  3782. const float key60 = 0.7f, off = 0.2f;
  3783. std::memcpy (blob.data() + 12 + 60 * 4, &key60, 4);
  3784. std::memcpy (blob.data() + 548, &off, 4);
  3785. blob[552] = 1; // auto release
  3786. lynx::ChannelDef c;
  3787. c.instrument = lynx::InstrumentKind::controller;
  3788. c.controllerState = blob;
  3789. auto inst = lynx::makeInstrument (lynx::InstrumentKind::controller);
  3790. inst->prepare (48000.0);
  3791. inst->configure (c, 120.0);
  3792. std::vector<float> l (64), r (64);
  3793. inst->noteOn (60, 1.0f);
  3794. inst->render (l.data(), r.data(), 64, 1.0f, 1.0f);
  3795. CHECK (std::abs (inst->controlOutput (0) - 0.7f) < 1e-6f);
  3796. inst->noteOff (60);
  3797. CHECK (std::abs (inst->controlOutput (0) - 0.2f) < 1e-6f);
  3798. }
  3799. TEST (envelope_controller_ducks_on_each_note)
  3800. {
  3801. // a unipolar articulator like "Basic Sidechain": base 0.05, drops to 0 on the note and rises to 1 in 0.3 s
  3802. lynx::ControllerArticulator a;
  3803. a.envOn = true;
  3804. a.base = 0.05f;
  3805. a.envLevel = 1.0f;
  3806. a.count = 3;
  3807. a.time = { 0.0f, 0.2f, 0.3f };
  3808. a.level = { 0.0f, 0.75f, 1.0f };
  3809. CHECK (std::abs (lynx::controllerValue (a, 0.0, -1.0) - 0.05f) < 1e-4f);
  3810. CHECK (std::abs (lynx::controllerValue (a, 0.1, -1.0) - 0.425f) < 1e-3f);
  3811. CHECK (lynx::controllerValue (a, 1.0, -1.0) > 0.99f); // no sustain: runs to the end and stays
  3812. a.sustain = 1;
  3813. CHECK (std::abs (lynx::controllerValue (a, 1.0, -1.0) - 0.8f) < 1e-3f); // held at sustain
  3814. CHECK (lynx::controllerValue (a, 1.0, 0.5) > 0.99f); // released: continues to the end
  3815. a.bipolar = true;
  3816. a.base = 0.5f;
  3817. a.sustain = -1;
  3818. CHECK (std::abs (lynx::controllerValue (a, 0.0, -1.0) - 0.0f) < 1e-4f); // bipolar: 0.5 + (0 − 0.5)
  3819. }
  3820. TEST (peak_controller_drives_live_automation)
  3821. {
  3822. // track 1: source (DC 0.5) with Peak Controller; track 2: DC 0.5 with volume linked to the controller
  3823. auto render = [] (bool linked) {
  3824. lynx::Song s;
  3825. s.mixer = lynx::Song::defaultMixer (2);
  3826. for (int t = 1; t <= 2; ++t)
  3827. {
  3828. lynx::ChannelDef c;
  3829. c.instrument = lynx::InstrumentKind::sampler;
  3830. c.samplePath = "dc";
  3831. c.volume = 1.0f;
  3832. c.mixerTrack = t;
  3833. s.channels.push_back (c);
  3834. }
  3835. s.patterns.emplace_back();
  3836. auto peak = lynx::makeEffectSlot (lynx::EffectKind::peakController);
  3837. peak.params[0] = 1.0f; // base 1
  3838. peak.params[1] = 0.0f; // amount −1: 1 − peak
  3839. peak.params[2] = 0.5f; // no tension
  3840. s.mixer[1].effects.push_back (peak);
  3841. if (linked)
  3842. {
  3843. lynx::Automation a;
  3844. a.name = "Peak";
  3845. a.target = { lynx::AutomationTargetKind::mixerVolume, 2 };
  3846. a.source = { 1, 0, 0 };
  3847. s.automations.push_back (a);
  3848. }
  3849. lynx::RackState::Factories f;
  3850. f.samples = [] (const std::string&) {
  3851. auto d = std::make_shared<lynx::SampleData>();
  3852. d->left.assign (48000, 0.5f);
  3853. return std::shared_ptr<const lynx::SampleData> (d);
  3854. };
  3855. auto state = lynx::RackState::build (s, nullptr, 48000.0, f);
  3856. lynx::RackRenderer r;
  3857. r.prepare (48000.0, 512);
  3858. lynx::LiveEventQueue live;
  3859. for (int ch = 0; ch < 2; ++ch)
  3860. {
  3861. lynx::NoteEvent on;
  3862. on.channel = ch;
  3863. on.velocity = 1.0f;
  3864. live.push (on);
  3865. }
  3866. std::vector<float> l (4800), rr (4800);
  3867. r.process (state.get(), false, 0.0, 4800, l.data(), rr.data(), &live);
  3868. return l[4000];
  3869. };
  3870. const auto free = render (false), ducked = render (true);
  3871. CHECK (std::isfinite (ducked) && ducked > 0.0f);
  3872. CHECK (ducked < free - 0.05f); // the 0.5 peak on track 1 lowers track 2's volume
  3873. }
  3874. TEST (effect_slot_dry_wet_and_smart_disable)
  3875. {
  3876. lynx::Song s;
  3877. s.mixer = lynx::Song::defaultMixer (1);
  3878. lynx::ChannelDef c;
  3879. c.instrument = lynx::InstrumentKind::sampler;
  3880. c.samplePath = "dc";
  3881. c.volume = 1.0f;
  3882. c.mixerTrack = 1;
  3883. s.channels.push_back (c);
  3884. s.patterns.emplace_back();
  3885. lynx::EffectSlot fx;
  3886. fx.kind = lynx::EffectKind::plugin;
  3887. fx.plugin.id = "inversor";
  3888. fx.mix = 0.25f; // 75% dry (+0.5) and 25% wet (-0.5) = +0.25
  3889. s.mixer[1].effects.push_back (fx);
  3890. auto inverter = std::make_shared<CountingInverter>();
  3891. lynx::RackState::Factories f;
  3892. f.samples = [] (const std::string&) {
  3893. auto d = std::make_shared<lynx::SampleData>();
  3894. d->left.assign (4800, 0.5f);
  3895. return std::shared_ptr<const lynx::SampleData> (d);
  3896. };
  3897. f.pluginEffects = [inverter] (const lynx::PluginRef&, double) -> std::shared_ptr<lynx::Effect> { return inverter; };
  3898. auto state = lynx::RackState::build (s, nullptr, 48000.0, f);
  3899. lynx::RackRenderer r;
  3900. r.prepare (48000.0, 512);
  3901. lynx::LiveEventQueue live;
  3902. lynx::NoteEvent on;
  3903. on.velocity = 1.0f;
  3904. live.push (on);
  3905. std::vector<float> l (480), rr (480);
  3906. r.process (state.get(), false, 0.0, 480, l.data(), rr.data(), &live);
  3907. CHECK (std::abs (l[100] - 0.25f) < 1e-4f);
  3908. // after the sample ends: 4 s of silence and the plugin stops being called
  3909. for (int i = 0; i < 100 * 6; ++i) // 6 s
  3910. r.process (state.get(), false, 0.0, 480, l.data(), rr.data());
  3911. const auto callsAfterSilence = inverter->calls;
  3912. for (int i = 0; i < 100; ++i)
  3913. r.process (state.get(), false, 0.0, 480, l.data(), rr.data());
  3914. CHECK (inverter->calls == callsAfterSilence);
  3915. CHECK (callsAfterSilence < 100 * 6);
  3916. // sound again: processing resumes in the same block
  3917. live.push (on);
  3918. r.process (state.get(), false, 0.0, 480, l.data(), rr.data(), &live);
  3919. CHECK (inverter->calls == callsAfterSilence + 1 && std::abs (l[100] - 0.25f) < 1e-4f);
  3920. }
  3921. TEST (swing_delays_offbeat_sixteenths)
  3922. {
  3923. CHECK (lynx::swingDelay (0, 1.0f) == 0);
  3924. CHECK (lynx::swingDelay (lynx::ticksPerStep, 1.0f) == lynx::ticksPerStep / 2);
  3925. CHECK (lynx::swingDelay (lynx::ticksPerStep * 2, 1.0f) == 0);
  3926. CHECK (lynx::swingDelay (lynx::ticksPerStep + 10, 1.0f) == (lynx::ticksPerStep - 20) / 2); // off the grid: triangular weight (FL)
  3927. CHECK (lynx::swingDelay (lynx::ticksPerStep, 1.0f, 0.5f) == lynx::ticksPerStep / 4); // channel mix
  3928. CHECK (lynx::swingDelay (lynx::ticksPerStep * 3, 0.5f) == lynx::ticksPerStep / 4);
  3929. lynx::Pattern p;
  3930. p.notes = { { 0, 100, 0, 60 }, { lynx::ticksPerStep, 100, 0, 62 } };
  3931. lynx::EventBuffer ev;
  3932. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  3933. lynx::schedulePatternLoop (p, p.effectiveLength (4), 0.0, 12000, tps, ev, 1.0f);
  3934. int onOffset = -1;
  3935. for (auto& e : ev)
  3936. if (e.type == lynx::NoteEvent::Type::on && e.key == 62)
  3937. onOffset = e.sampleOffset;
  3938. CHECK (onOffset == (int) std::floor ((lynx::ticksPerStep * 1.5) / tps));
  3939. }
  3940. TEST (sampler_settings_pitch_reverse_envelope_cut)
  3941. {
  3942. auto ramp = std::make_shared<lynx::SampleData>();
  3943. for (int i = 0; i < 48000; ++i)
  3944. ramp->left.push_back ((float) i / 48000.0f); // ramp: the value tells where the read position is
  3945. ramp->sampleRate = 48000.0;
  3946. auto play = [&] (const lynx::SamplerSettings& s, int key, int frames, bool releaseAfterHalf) {
  3947. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  3948. inst->prepare (48000.0);
  3949. lynx::ChannelDef c;
  3950. c.sampler = s;
  3951. inst->configure (c, 120.0);
  3952. inst->noteOn (key, 1.0f);
  3953. std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
  3954. inst->render (l.data(), r.data(), frames / 2, 1.0f, 1.0f);
  3955. if (releaseAfterHalf)
  3956. inst->noteOff (key);
  3957. inst->render (l.data() + frames / 2, r.data() + frames / 2, frames - frames / 2, 1.0f, 1.0f);
  3958. return l;
  3959. };
  3960. // +12 semitones: reads twice as fast
  3961. lynx::SamplerSettings up;
  3962. up.pitch = 12.0f;
  3963. CHECK (std::abs (play (up, 60, 1000, false)[999] - 2.0f * 999.0f / 48000.0f) < 1e-3f);
  3964. // reversed: starts from the end
  3965. lynx::SamplerSettings back;
  3966. back.reverse = true;
  3967. CHECK (play (back, 60, 100, false)[10] > 0.99f);
  3968. // 10 ms attack: starts silent and reaches full level
  3969. lynx::SamplerSettings slow;
  3970. slow.attack = 0.01f;
  3971. const auto a = play (slow, 60, 2000, false);
  3972. CHECK (a[10] < 0.2f * (10.0f / 48000.0f) + 1e-6f && std::abs (a[1500] - 1500.0f / 48000.0f) < 1e-4f);
  3973. // 5 ms release: gone right after release; without release it plays in full
  3974. lynx::SamplerSettings rel;
  3975. rel.release = 0.005f;
  3976. CHECK (play (rel, 60, 2000, true)[1999] == 0.0f);
  3977. CHECK (play (lynx::SamplerSettings {}, 60, 2000, true)[1999] > 0.0f);
  3978. // cut itself: the second note cuts the first (one voice remains)
  3979. lynx::SamplerSettings cut;
  3980. cut.cut = true;
  3981. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  3982. inst->prepare (48000.0);
  3983. lynx::ChannelDef cutChannel;
  3984. cutChannel.sampler = cut;
  3985. inst->configure (cutChannel, 120.0);
  3986. inst->noteOn (60, 1.0f);
  3987. std::vector<float> l (2000), r (2000);
  3988. inst->render (l.data(), r.data(), 1000, 1.0f, 1.0f);
  3989. inst->noteOn (60, 1.0f);
  3990. std::fill (l.begin(), l.end(), 0.0f);
  3991. inst->render (l.data(), r.data(), 2000, 1.0f, 1.0f);
  3992. CHECK (std::abs (l[1999] - 1999.0f / 48000.0f) < 1e-4f); // only the new note
  3993. }
  3994. TEST (playlist_split_and_track_mute)
  3995. {
  3996. lynx::Clip c { 0, 3, 960, 3840, 480 };
  3997. const auto rest = lynx::splitClip (c, 2000);
  3998. CHECK (c.start == 960 && c.length == 1040 && c.offset == 480);
  3999. CHECK (rest.start == 2000 && rest.length == 2800 && rest.offset == 480 + 1040 && rest.track == 3);
  4000. auto s = lynx::Song::makeDefault();
  4001. s.mode = lynx::PlayMode::song;
  4002. s.playlist = { { 0, 0, 0, 3840, 0 } };
  4003. lynx::EventBuffer ev;
  4004. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  4005. lynx::scheduleSong (s, 0.0, 48000, tps, ev);
  4006. CHECK (ev.size() > 0);
  4007. s.playlistTracks.resize (1);
  4008. s.playlistTracks[0].muted = true;
  4009. ev.clear();
  4010. lynx::scheduleSong (s, 0.0, 48000, tps, ev);
  4011. CHECK (ev.size() == 0);
  4012. }
  4013. TEST (note_pan_pitch_and_mute)
  4014. {
  4015. lynx::Pattern p;
  4016. lynx::Note a { 0, 240, 0, 60 };
  4017. a.pan = -1.0f;
  4018. a.finePitch = 50.0f;
  4019. lynx::Note b { 240, 240, 0, 62 };
  4020. b.muted = true;
  4021. p.notes = { a, b };
  4022. lynx::EventBuffer ev;
  4023. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  4024. lynx::schedulePatternLoop (p, p.effectiveLength (4), 0.0, 48000, tps, ev);
  4025. int ons = 0;
  4026. for (auto& e : ev)
  4027. if (e.type == lynx::NoteEvent::Type::on)
  4028. {
  4029. ++ons;
  4030. CHECK (e.key == 60 && e.pan == -1.0f && e.cents == 50.0f);
  4031. }
  4032. CHECK (ons == 1); // the muted one doesn't play
  4033. // note pan in the sampler: fully left
  4034. auto dc = std::make_shared<lynx::SampleData>();
  4035. dc->left.assign (4800, 0.5f);
  4036. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, dc);
  4037. inst->prepare (48000.0);
  4038. inst->setNextNoteExpression (-1.0f, 0.0f);
  4039. inst->noteOn (60, 1.0f);
  4040. std::vector<float> l (100), r (100);
  4041. inst->render (l.data(), r.data(), 100, 1.0f, 1.0f);
  4042. CHECK (l[50] > 0.6f && std::abs (r[50]) < 1e-6f);
  4043. }
  4044. TEST (tempo_automation_changes_speed_and_duration)
  4045. {
  4046. auto song = lynx::Song::makeDefault();
  4047. song.bpm = 120.0;
  4048. song.mode = lynx::PlayMode::song;
  4049. const auto bar = (std::int64_t) lynx::ppq * 4;
  4050. song.playlist = { { 0, 0, 0, bar, 0 }, { 0, 0, bar, bar, 0 } };
  4051. lynx::Automation tempo;
  4052. tempo.target = { lynx::AutomationTargetKind::tempo, 0 };
  4053. const auto v120 = lynx::valueFromTempo (120.0), v240 = lynx::valueFromTempo (240.0);
  4054. tempo.points = { { 0, v120 }, { bar - 1, v120 }, { bar, v240 } }; // step: 120 -> 240 at bar 2
  4055. song.automations.push_back (tempo);
  4056. lynx::Clip clip;
  4057. clip.kind = lynx::ClipKind::automation;
  4058. clip.automation = 0;
  4059. clip.track = 1;
  4060. clip.length = bar * 2;
  4061. song.playlist.push_back (clip);
  4062. CHECK (std::abs (song.tempoAt (100.0) - 120.0) < 0.01 && std::abs (song.tempoAt ((double) bar + 100.0) - 240.0) < 0.01);
  4063. // 1 bar at 120 (2 s) + 1 at 240 (1 s)
  4064. CHECK (std::abs (song.secondsForTicks (bar * 2) - 3.0) < 0.01);
  4065. auto state = lynx::RackState::build (song, nullptr, 48000.0);
  4066. CHECK (state->tempoAutomation == 0);
  4067. lynx::RackRenderer r;
  4068. r.prepare (48000.0, 512);
  4069. std::vector<float> l (512), rr (512);
  4070. r.process (state.get(), true, 10.0, 512, l.data(), rr.data());
  4071. CHECK (std::abs (r.currentBpm() - 120.0) < 0.01);
  4072. r.process (state.get(), true, (double) bar + 10.0, 512, l.data(), rr.data());
  4073. CHECK (std::abs (r.currentBpm() - 240.0) < 0.01);
  4074. }
  4075. TEST (bounce_insert_song_and_apply)
  4076. {
  4077. auto s = lynx::Song::makeDefault();
  4078. s.setRoute (3, 7, true, 0.5f);
  4079. s.mixer[0].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::reverb));
  4080. s.mixer[2].solo = true;
  4081. const auto b = lynx::makeBounceSong (s, 3);
  4082. CHECK (b.mixer[3].solo && ! b.mixer[2].solo && b.mixer[3].toMaster && b.mixer[3].sends.empty());
  4083. CHECK (b.mixer[0].effects.empty() && b.mode == lynx::PlayMode::song);
  4084. const auto audible = b.soloAudible();
  4085. CHECK (audible[3] && ! audible[1] && ! audible[7]);
  4086. const auto channels = s.channels.size();
  4087. const auto inserts = s.mixer.size();
  4088. const auto created = lynx::applyBounce (s, 3, "C:/x.wav", 3840);
  4089. CHECK (created == (int) inserts && s.mixer.size() == inserts + 1 && s.mixer[3].muted);
  4090. CHECK (s.mixer[(std::size_t) created].sends.size() == 1 && s.mixer[(std::size_t) created].sends[0].target == 7);
  4091. CHECK (s.channels.size() == channels + 1 && s.channels.back().mixerTrack == created);
  4092. CHECK (s.playlist.back().kind == lynx::ClipKind::audio && s.playlist.back().length == 3840);
  4093. }
  4094. TEST (mixer_stereo_separation_phase_swap)
  4095. {
  4096. lynx::MixerTrack t;
  4097. std::vector<float> l { 1.0f }, r { 0.0f };
  4098. t.stereo = 1.0f; // mono
  4099. lynx::RackRenderer::applyStereoForTest (t, l.data(), r.data(), 1);
  4100. CHECK (std::abs (l[0] - 0.5f) < 1e-6f && std::abs (r[0] - 0.5f) < 1e-6f);
  4101. l = { 1.0f };
  4102. r = { 0.0f };
  4103. t.stereo = -1.0f; // doubled side: 1.5 L - 0.5 R
  4104. lynx::RackRenderer::applyStereoForTest (t, l.data(), r.data(), 1);
  4105. CHECK (std::abs (l[0] - 1.5f) < 1e-6f && std::abs (r[0] + 0.5f) < 1e-6f);
  4106. l = { 1.0f };
  4107. r = { 0.25f };
  4108. t.stereo = 0.0f;
  4109. t.swapChannels = true;
  4110. t.invertPhase = true;
  4111. lynx::RackRenderer::applyStereoForTest (t, l.data(), r.data(), 1);
  4112. CHECK (l[0] == -0.25f && r[0] == -1.0f);
  4113. }
  4114. TEST (effects_limiter_filter_distortion_chorus)
  4115. {
  4116. const double sr = 48000.0;
  4117. auto sine = [] (float hz, float amp, int n) {
  4118. std::vector<float> v ((std::size_t) n);
  4119. for (int i = 0; i < n; ++i)
  4120. v[(std::size_t) i] = amp * std::sin (2.0f * 3.14159265f * hz * (float) i / 48000.0f);
  4121. return v;
  4122. };
  4123. auto peak = [] (const std::vector<float>& v, int from) {
  4124. float p = 0.0f;
  4125. for (std::size_t i = (std::size_t) from; i < v.size(); ++i)
  4126. p = std::max (p, std::abs (v[i]));
  4127. return p;
  4128. };
  4129. // limiter: +12 dB on a 0.9 sine never exceeds the ceiling (-1 dB) and reports the latency
  4130. auto lim = lynx::makeEffect (lynx::EffectKind::limiter);
  4131. lim->prepare (sr);
  4132. auto slot = lynx::makeEffectSlot (lynx::EffectKind::limiter);
  4133. slot.params[0] = 11.0f / 12.0f; // -1 dB
  4134. slot.params[1] = 0.5f; // +12 dB
  4135. auto l = sine (100.0f, 0.9f, 48000), r = l;
  4136. lim->process (l.data(), r.data(), 48000, slot.params, 120.0);
  4137. CHECK (peak (l, 0) <= std::pow (10.0f, -1.0f / 20.0f) + 1e-4f);
  4138. CHECK (peak (l, 4800) > 0.8f);
  4139. CHECK (lim->latencySamples() == 72);
  4140. // low-pass filter at 500 Hz knocks down an 8 kHz sine
  4141. auto lp = lynx::makeEffect (lynx::EffectKind::filter);
  4142. lp->prepare (sr);
  4143. auto fs = lynx::makeEffectSlot (lynx::EffectKind::filter);
  4144. fs.params[1] = std::log (500.0f / 20.0f) / std::log (1000.0f);
  4145. l = sine (8000.0f, 1.0f, 9600);
  4146. r = l;
  4147. lp->process (l.data(), r.data(), 9600, fs.params, 120.0);
  4148. CHECK (peak (l, 4800) < 0.05f);
  4149. // distortion and chorus: finite, audible output
  4150. for (auto kind : { lynx::EffectKind::distortion, lynx::EffectKind::chorus })
  4151. {
  4152. auto fx = lynx::makeEffect (kind);
  4153. fx->prepare (sr);
  4154. auto ps = lynx::makeEffectSlot (kind);
  4155. l = sine (220.0f, 0.5f, 9600);
  4156. r = l;
  4157. fx->process (l.data(), r.data(), 9600, ps.params, 120.0);
  4158. bool finite = true;
  4159. for (auto v : l)
  4160. finite = finite && std::isfinite (v);
  4161. CHECK (finite && peak (l, 2400) > 0.1f && peak (l, 0) < 2.0f);
  4162. }
  4163. }
  4164. TEST (slide_notes_glide_without_retrigger)
  4165. {
  4166. // sampler with a ramp: the read speed doubles after a one-octave slide
  4167. auto ramp = std::make_shared<lynx::SampleData>();
  4168. for (int i = 0; i < 96000; ++i)
  4169. ramp->left.push_back ((float) i / 96000.0f);
  4170. ramp->sampleRate = 48000.0;
  4171. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  4172. inst->prepare (48000.0);
  4173. inst->noteOn (60, 1.0f);
  4174. std::vector<float> l (2000), r (2000);
  4175. inst->render (l.data(), r.data(), 1000, 1.0f, 1.0f);
  4176. const auto stepBefore = l[999] - l[998];
  4177. inst->slideTo (72, 0.0f, 100.0f);
  4178. std::fill (l.begin(), l.end(), 0.0f);
  4179. inst->render (l.data(), r.data(), 1000, 1.0f, 1.0f);
  4180. const auto stepAfter = l[999] - l[998];
  4181. CHECK (std::abs (stepAfter / stepBefore - 2.0f) < 0.01f);
  4182. // the sequencer sends "slide" (no off) for the slide note
  4183. lynx::Pattern p;
  4184. lynx::Note base { 0, 960, 0, 48 };
  4185. lynx::Note glide { 480, 240, 0, 55 };
  4186. glide.slide = true;
  4187. p.notes = { base, glide };
  4188. lynx::EventBuffer ev;
  4189. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  4190. lynx::schedulePatternLoop (p, p.effectiveLength (4), 0.0, 30000, tps, ev);
  4191. int ons = 0, offs = 0, slides = 0;
  4192. for (auto& e : ev)
  4193. {
  4194. ons += e.type == lynx::NoteEvent::Type::on;
  4195. offs += e.type == lynx::NoteEvent::Type::off;
  4196. if (e.type == lynx::NoteEvent::Type::slide)
  4197. {
  4198. ++slides;
  4199. CHECK (e.key == 55 && std::abs (e.slideSamples - 240.0f / (float) tps) < 1.0f);
  4200. }
  4201. }
  4202. CHECK (ons == 1 && offs == 1 && slides == 1);
  4203. }
  4204. TEST (synth_fm_phase_modulation)
  4205. {
  4206. auto render = [] (const lynx::SynthSettings& s) {
  4207. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  4208. inst->prepare (48000.0);
  4209. lynx::ChannelDef c;
  4210. c.synth = s;
  4211. inst->configure (c, 120.0);
  4212. inst->noteOn (69, 1.0f); // 440 Hz
  4213. std::vector<float> l (48000), r (48000);
  4214. inst->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
  4215. return l;
  4216. };
  4217. // energy (Goertzel) at frequency f over frames [from, to)
  4218. auto energy = [] (const std::vector<float>& x, double f, int from, int to) {
  4219. const auto w = 2.0 * 3.141592653589793 * f / 48000.0, k = 2.0 * std::cos (w);
  4220. double s1 = 0.0, s2 = 0.0;
  4221. for (int i = from; i < to; ++i)
  4222. {
  4223. const auto s0 = x[(std::size_t) i] + k * s1 - s2;
  4224. s2 = s1;
  4225. s1 = s0;
  4226. }
  4227. return s1 * s1 + s2 * s2 - k * s1 * s2;
  4228. };
  4229. lynx::SynthSettings plain;
  4230. plain.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  4231. plain.osc[2] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  4232. plain.cutoff = 20000.0f;
  4233. auto fm = plain;
  4234. fm.fm = 1.0f;
  4235. const auto a = render (plain), b = render (fm);
  4236. // no FM: osc 3 adds (same pitch) and there's no 2nd harmonic; with FM the sidebands appear
  4237. CHECK (energy (a, 880.0, 4800, 24000) < 1e-3 * energy (a, 440.0, 4800, 24000));
  4238. CHECK (energy (b, 880.0, 4800, 24000) > 0.1 * energy (b, 440.0, 4800, 24000));
  4239. CHECK (energy (b, 1320.0, 4800, 24000) > 0.01 * energy (b, 440.0, 4800, 24000));
  4240. for (auto v : b)
  4241. CHECK (std::isfinite (v) && std::abs (v) < 1.0f);
  4242. // index decaying to 0: the start is bright, the end is nearly a sine again
  4243. auto decaying = fm;
  4244. decaying.fmDecay = 0.1f;
  4245. decaying.fmSustain = 0.0f;
  4246. const auto c = render (decaying);
  4247. const auto early = energy (c, 880.0, 0, 2400) / energy (c, 440.0, 0, 2400);
  4248. const auto late = energy (c, 880.0, 36000, 48000) / energy (c, 440.0, 36000, 48000);
  4249. CHECK (early > 0.05 && late < 1e-3);
  4250. }
  4251. TEST (synth_three_oscillators_adsr_filter)
  4252. {
  4253. auto render = [] (const lynx::SynthSettings& s, int frames, int releaseAt) {
  4254. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  4255. inst->prepare (48000.0);
  4256. lynx::ChannelDef c;
  4257. c.synth = s;
  4258. inst->configure (c, 120.0);
  4259. inst->noteOn (69, 1.0f); // A4 = 440 Hz
  4260. std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
  4261. inst->render (l.data(), r.data(), releaseAt, 1.0f, 1.0f);
  4262. inst->noteOff (69);
  4263. inst->render (l.data() + releaseAt, r.data() + releaseAt, frames - releaseAt, 1.0f, 1.0f);
  4264. return l;
  4265. };
  4266. auto peakIn = [] (const std::vector<float>& v, int from, int to) {
  4267. float p = 0.0f;
  4268. for (int i = from; i < to; ++i)
  4269. p = std::max (p, std::abs (v[(std::size_t) i]));
  4270. return p;
  4271. };
  4272. // default: sounds and fades after the release
  4273. const lynx::SynthSettings standard;
  4274. const auto a = render (standard, 48000, 24000);
  4275. CHECK (peakIn (a, 1000, 20000) > 0.05f && peakIn (a, 40000, 48000) < 1e-3f);
  4276. // pure sine with an open filter: zero-crossing count gives ~440 Hz
  4277. lynx::SynthSettings sine;
  4278. sine.osc[0].wave = lynx::Waveform::sine;
  4279. sine.cutoff = 20000.0f;
  4280. const auto b = render (sine, 48000, 47000);
  4281. int crossings = 0;
  4282. for (int i = 4800; i < 4800 + 48000 / 2; ++i)
  4283. crossings += (b[(std::size_t) i - 1] < 0.0f) != (b[(std::size_t) i] < 0.0f);
  4284. CHECK (std::abs (crossings - 440) < 6); // half a second = 220 cycles = 440 crossings
  4285. float sinePeak = 0.0f;
  4286. for (int i = 4800; i < 9600; ++i)
  4287. sinePeak = std::max (sinePeak, std::abs (b[(std::size_t) i]));
  4288. CHECK (sinePeak > 0.2f && sinePeak < 0.35f); // 0.3 level, approximated sine without clipping
  4289. // second oscillator an octave up adds energy; sustain 0 makes the note decay on its own
  4290. lynx::SynthSettings two = sine;
  4291. two.osc[1] = { lynx::Waveform::sine, 1, 0.0f, 1.0f };
  4292. two.sustain = 0.0f;
  4293. two.decay = 0.1f;
  4294. const auto c = render (two, 48000, 47000);
  4295. CHECK (peakIn (c, 100, 2000) > peakIn (b, 100, 2000) && peakIn (c, 30000, 40000) < 1e-3f);
  4296. }
  4297. TEST (set_target_value_round_trips)
  4298. {
  4299. auto s = lynx::Song::makeDefault();
  4300. s.mixer[2].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::delay));
  4301. const lynx::AutomationTarget targets[] = {
  4302. { lynx::AutomationTargetKind::channelVolume, 1 }, { lynx::AutomationTargetKind::channelPan, 2 },
  4303. { lynx::AutomationTargetKind::mixerVolume, 3 }, { lynx::AutomationTargetKind::mixerPan, 0 },
  4304. { lynx::AutomationTargetKind::effectParam, 2, 0, 1 }, { lynx::AutomationTargetKind::tempo, 0 },
  4305. };
  4306. for (const auto& t : targets)
  4307. {
  4308. CHECK (s.setTargetValue (t, 0.25f));
  4309. CHECK (std::abs (s.targetValue (t) - 0.25f) < 1e-5f);
  4310. }
  4311. CHECK (! s.setTargetValue ({ lynx::AutomationTargetKind::channelParam, 0, 0, 3 }, 0.5f)); // lives in the plugin
  4312. }
  4313. TEST (cut_groups_stretch_and_envelope_import)
  4314. {
  4315. // open hat (cut by 1) is cut when the closed one (group 1) plays
  4316. lynx::Song s;
  4317. s.mixer = lynx::Song::defaultMixer (2);
  4318. for (int i = 0; i < 2; ++i)
  4319. {
  4320. lynx::ChannelDef c;
  4321. c.instrument = lynx::InstrumentKind::sampler;
  4322. c.samplePath = "dc";
  4323. c.volume = 1.0f;
  4324. c.mixerTrack = i + 1;
  4325. s.channels.push_back (c);
  4326. }
  4327. s.channels[0].cutBy = 1; // open
  4328. s.channels[1].cutGroup = 1; // closed
  4329. auto& p = s.patterns.emplace_back();
  4330. p.notes = { { 0, 100, 0, 60 }, { 960, 100, 1, 60 } };
  4331. auto provider = [] (const std::string&) {
  4332. auto d = std::make_shared<lynx::SampleData>();
  4333. d->left.assign (96000, 0.5f);
  4334. return std::shared_ptr<const lynx::SampleData> (d);
  4335. };
  4336. auto state = lynx::RackState::build (s, nullptr, 48000.0, provider);
  4337. CHECK (state->hasCutGroups && state->cutTargets[1].size() == 1 && state->cutTargets[1][0] == 0);
  4338. lynx::RackRenderer r;
  4339. r.prepare (48000.0, 512);
  4340. std::vector<float> l (512), rr (512);
  4341. const auto tps = lynx::ticksPerSample (s.bpm, 48000.0);
  4342. float afterCut = 0.0f;
  4343. for (double tick = 0.0; tick < 960.0 * 3; tick += 512 * tps)
  4344. {
  4345. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  4346. if (tick > 960.0 * 2) // the closed one continues; the open one has stopped
  4347. afterCut = std::max (afterCut, std::abs (l[256]));
  4348. }
  4349. CHECK (std::abs (afterCut - 0.5f * 0.78f) < 0.01f); // only the closed one (default velocity 0.78)
  4350. // resampling stretch: 1 s of sample stretched to 4 beats at 120 BPM = 2 s
  4351. auto ramp = std::make_shared<lynx::SampleData>();
  4352. for (int i = 0; i < 48000; ++i)
  4353. ramp->left.push_back ((float) i / 48000.0f);
  4354. ramp->sampleRate = 48000.0;
  4355. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  4356. inst->prepare (48000.0);
  4357. lynx::ChannelDef c;
  4358. c.sampler.stretchBeats = 4.0f;
  4359. inst->configure (c, 120.0);
  4360. inst->noteOn (72, 1.0f); // the note pitch doesn't change the speed
  4361. std::vector<float> a (48000), b (48000);
  4362. inst->render (a.data(), b.data(), 48000, 1.0f, 1.0f);
  4363. CHECK (std::abs (a[47999] - 0.5f) < 0.01f); // after 1 s it has read half
  4364. // FL envelope time: 32950 = 2 s; < 101 = 0
  4365. CHECK (std::abs (flp::Channel::envelopeTime (32950) - 2.0) < 0.01 && flp::Channel::envelopeTime (50) == 0.0);
  4366. }
  4367. TEST (time_stretch_keeps_pitch)
  4368. {
  4369. lynx::SampleData sine;
  4370. sine.sampleRate = 48000.0;
  4371. for (int i = 0; i < 48000; ++i)
  4372. sine.left.push_back (0.5f * std::sin (2.0f * 3.14159265f * 440.0f * (float) i / 48000.0f));
  4373. auto crossingsPerSecond = [] (const lynx::SampleData& d) {
  4374. int c = 0;
  4375. const auto n = (int) d.frames();
  4376. for (int i = 4800; i < n - 4800; ++i)
  4377. c += (d.left[(std::size_t) i - 1] < 0.0f) != (d.left[(std::size_t) i] < 0.0f);
  4378. return (double) c / ((double) (n - 9600) / d.sampleRate);
  4379. };
  4380. for (double ratio : { 2.0, 0.5, 1.37 })
  4381. {
  4382. const auto out = lynx::timeStretch (sine, ratio);
  4383. CHECK (std::abs ((double) out->frames() - 48000.0 * ratio) < 2.0);
  4384. CHECK (std::abs (crossingsPerSecond (*out) - 880.0) < 15.0); // 440 Hz stays 440 Hz
  4385. float peak = 0.0f;
  4386. for (std::size_t i = 4800; i + 4800 < out->frames(); ++i)
  4387. peak = std::max (peak, std::abs (out->left[i]));
  4388. CHECK (peak > 0.4f && peak < 0.6f); // no gaps or clipping in the overlap
  4389. }
  4390. }
  4391. TEST (sampler_region_start_and_end)
  4392. {
  4393. auto ramp = std::make_shared<lynx::SampleData>();
  4394. for (int i = 0; i < 10000; ++i)
  4395. ramp->left.push_back ((float) i / 10000.0f);
  4396. ramp->sampleRate = 48000.0;
  4397. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  4398. inst->prepare (48000.0);
  4399. lynx::ChannelDef c;
  4400. c.sampler.regionStart = 0.25f;
  4401. c.sampler.regionEnd = 0.5f;
  4402. inst->configure (c, 120.0);
  4403. inst->noteOn (60, 1.0f);
  4404. std::vector<float> l (6000), r (6000);
  4405. inst->render (l.data(), r.data(), 6000, 1.0f, 1.0f);
  4406. CHECK (std::abs (l[0] - 0.25f) < 0.001f); // starts at the beginning of the slice
  4407. CHECK (l[2400] > 0.48f && l[2600] == 0.0f); // and stops at its end
  4408. }
  4409. TEST (arpeggiator_plays_held_chord_in_steps)
  4410. {
  4411. auto song = lynx::Song::makeDefault();
  4412. song.bpm = 120.0;
  4413. song.patterns[0].notes.clear();
  4414. for (int key : { 60, 64, 67 })
  4415. song.patterns[0].notes.push_back ({ 0, lynx::ppq * 4, 4, key, 0.8f }); // chord held for 1 bar
  4416. song.channels[4].arp.direction = 1; // up
  4417. song.channels[4].arp.stepBeats = 0.25f;
  4418. auto state = lynx::RackState::build (song, nullptr, 48000.0);
  4419. CHECK (state->arpeggiators[4] != nullptr && state->arpeggiators[0] == nullptr);
  4420. // the arpeggiator generates the notes; we count what it hands to the instrument
  4421. auto& arp = *state->arpeggiators[4];
  4422. const auto tps = lynx::ticksPerSample (song.bpm, 48000.0);
  4423. std::vector<int> keys;
  4424. lynx::EventBuffer ev;
  4425. for (double tick = 0.0; tick < lynx::ppq * 2.0; tick += 512 * tps)
  4426. {
  4427. ev.clear();
  4428. lynx::schedulePatternLoop (song.patterns[0], song.patterns[0].effectiveLength (4), tick, 512, tps, ev);
  4429. int in = 0, out = 0;
  4430. for (auto& e : ev)
  4431. if (e.channel == 4)
  4432. arp.input[(std::size_t) in++] = e;
  4433. arp.process (4, arp.input.data(), in, tick, tps, 512, song.channels[4].arp, arp.output.data(), out);
  4434. for (int i = 0; i < out; ++i)
  4435. if (arp.output[(std::size_t) i].type == lynx::NoteEvent::Type::on)
  4436. keys.push_back (arp.output[(std::size_t) i].key);
  4437. }
  4438. // 2 beats = 8 sixteenth steps: 60 64 67 60 64 67 60 64
  4439. CHECK (keys.size() >= 8); // the last block goes slightly past 2 beats
  4440. const int expected[] = { 60, 64, 67, 60, 64, 67, 60, 64 };
  4441. for (std::size_t i = 0; i < keys.size() && i < 8; ++i)
  4442. CHECK (keys[i] == expected[i]);
  4443. // actually rendering also makes sound
  4444. lynx::RackRenderer r;
  4445. r.prepare (48000.0, 512);
  4446. std::vector<float> l (512), rr (512);
  4447. float peak = 0.0f;
  4448. for (double tick = 0.0; tick < lynx::ppq; tick += 512 * tps)
  4449. {
  4450. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  4451. for (auto v : l)
  4452. peak = std::max (peak, std::abs (v));
  4453. }
  4454. CHECK (peak > 0.01f);
  4455. }
  4456. TEST (audio_clip_fades_and_gain)
  4457. {
  4458. lynx::Song s;
  4459. s.bpm = 120.0;
  4460. s.mode = lynx::PlayMode::song;
  4461. s.mixer = lynx::Song::defaultMixer (1);
  4462. lynx::ChannelDef c;
  4463. c.instrument = lynx::InstrumentKind::audioClip;
  4464. c.samplePath = "dc";
  4465. c.volume = 1.0f;
  4466. c.mixerTrack = 1;
  4467. s.channels.push_back (c);
  4468. s.patterns.emplace_back();
  4469. lynx::Clip clip;
  4470. clip.kind = lynx::ClipKind::audio;
  4471. clip.channel = 0;
  4472. clip.length = lynx::ppq * 4; // 2 s at 120 BPM
  4473. clip.fadeIn = lynx::ppq; // 0.5 s
  4474. clip.fadeOut = lynx::ppq;
  4475. clip.gain = 0.5f;
  4476. s.playlist.push_back (clip);
  4477. auto provider = [] (const std::string&) {
  4478. auto d = std::make_shared<lynx::SampleData>();
  4479. d->left.assign (48000 * 4, 1.0f);
  4480. return std::shared_ptr<const lynx::SampleData> (d);
  4481. };
  4482. auto state = lynx::RackState::build (s, nullptr, 48000.0, provider);
  4483. lynx::RackRenderer r;
  4484. r.prepare (48000.0, 512);
  4485. std::vector<float> all;
  4486. std::vector<float> l (512), rr (512);
  4487. const auto tps = lynx::ticksPerSample (s.bpm, 48000.0);
  4488. for (double tick = 0.0; tick < lynx::ppq * 4.0; tick += 512 * tps)
  4489. {
  4490. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  4491. all.insert (all.end(), l.begin(), l.end());
  4492. }
  4493. CHECK (all[100] < 0.01f); // fade-in start
  4494. CHECK (std::abs (all[12000] - 0.25f) < 0.02f); // fade-in midpoint: half of 0.5
  4495. CHECK (std::abs (all[48000] - 0.5f) < 0.01f); // body: the clip gain
  4496. CHECK (std::abs (all[96000 - 12000] - 0.25f) < 0.02f); // fade-out midpoint
  4497. }
  4498. TEST (slicer_detects_hits_and_plays_slices_by_key)
  4499. {
  4500. // 4 hits (decaying noise) every 0.25 s
  4501. lynx::SampleData loop;
  4502. loop.sampleRate = 48000.0;
  4503. loop.left.assign (48000, 0.0f);
  4504. lynx::Noise noise;
  4505. for (int hit = 0; hit < 4; ++hit)
  4506. for (int i = 0; i < 6000; ++i)
  4507. loop.left[(std::size_t) (hit * 12000 + i)] = noise.next() * 0.8f * std::exp (-(float) i / 1500.0f);
  4508. const auto slices = lynx::detectSlices (loop, 0.5f);
  4509. CHECK (slices.size() == 4);
  4510. for (std::size_t i = 0; i < slices.size(); ++i)
  4511. CHECK (std::abs (slices[i] - 0.25f * (float) i) < 0.01f);
  4512. // key 61 plays the second slice, which starts at 0.25 and ends before 0.5
  4513. auto ramp = std::make_shared<lynx::SampleData>();
  4514. for (int i = 0; i < 48000; ++i)
  4515. ramp->left.push_back ((float) i / 48000.0f);
  4516. ramp->sampleRate = 48000.0;
  4517. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  4518. inst->prepare (48000.0);
  4519. lynx::ChannelDef c;
  4520. c.sampler.slices = slices;
  4521. inst->configure (c, 120.0);
  4522. inst->noteOn (61, 1.0f);
  4523. std::vector<float> l (20000), r (20000);
  4524. inst->render (l.data(), r.data(), 20000, 1.0f, 1.0f);
  4525. CHECK (std::abs (l[0] - 0.25f) < 0.01f && l[11000] > 0.45f && l[13000] == 0.0f);
  4526. }
  4527. TEST (project_templates_render_sound)
  4528. {
  4529. const auto names = lynx::templateNames();
  4530. CHECK (names.size() == 4);
  4531. for (int t = 1; t < (int) names.size(); ++t)
  4532. {
  4533. auto song = lynx::makeTemplate (t);
  4534. auto state = lynx::RackState::build (song, nullptr, 48000.0);
  4535. lynx::RackRenderer r;
  4536. r.prepare (48000.0, 512);
  4537. std::vector<float> l (512), rr (512);
  4538. const auto tps = lynx::ticksPerSample (song.bpm, 48000.0);
  4539. float peak = 0.0f;
  4540. for (double tick = 0.0; tick < lynx::ppq * 4.0; tick += 512 * tps)
  4541. {
  4542. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  4543. for (auto v : l)
  4544. peak = std::max (peak, std::abs (v));
  4545. }
  4546. CHECK (peak > 0.05f && peak < 4.0f);
  4547. }
  4548. CHECK (lynx::makeTemplate (0).patterns[0].notes.empty());
  4549. }
  4550. // --- interface translations (resources/i18n) -------------------------------------------------------
  4551. namespace i18ntest
  4552. {
  4553. std::string readFile (const std::filesystem::path& path)
  4554. {
  4555. std::ifstream in (path, std::ios::binary);
  4556. return { std::istreambuf_iterator<char> (in), std::istreambuf_iterator<char>() };
  4557. }
  4558. std::string unescape (const std::string& s)
  4559. {
  4560. std::string out;
  4561. for (std::size_t i = 0; i < s.size(); ++i)
  4562. {
  4563. if (s[i] == '\\' && i + 1 < s.size())
  4564. {
  4565. const auto n = s[++i];
  4566. out += n == 'n' ? '\n' : n == 't' ? '\t' : n;
  4567. }
  4568. else
  4569. out += s[i];
  4570. }
  4571. return out;
  4572. }
  4573. // reads a quoted string starting at s[pos] == '"'; returns the raw (escaped) contents and moves pos past the closing quote
  4574. bool quoted (const std::string& s, std::size_t& pos, std::string& raw)
  4575. {
  4576. if (pos >= s.size() || s[pos] != '"')
  4577. return false;
  4578. raw.clear();
  4579. for (++pos; pos < s.size() && s[pos] != '"' && s[pos] != '\n'; ++pos)
  4580. {
  4581. if (s[pos] == '\\' && pos + 1 < s.size())
  4582. raw += s[pos++];
  4583. raw += s[pos];
  4584. }
  4585. if (pos >= s.size() || s[pos] != '"')
  4586. return false;
  4587. ++pos;
  4588. return true;
  4589. }
  4590. // JUCE LocalisedStrings format: "english" = "translation"; duplicates are reported in `duplicates`
  4591. std::map<std::string, std::string> parseTable (const std::string& text, std::vector<std::string>& duplicates)
  4592. {
  4593. std::map<std::string, std::string> table;
  4594. std::istringstream lines (text);
  4595. std::string line;
  4596. while (std::getline (lines, line))
  4597. {
  4598. std::size_t pos = line.find_first_not_of (" \t");
  4599. std::string key, value;
  4600. if (pos == std::string::npos || ! quoted (line, pos, key))
  4601. continue;
  4602. pos = line.find ('"', pos);
  4603. if (pos == std::string::npos || ! quoted (line, pos, value))
  4604. value.clear();
  4605. const auto k = unescape (key);
  4606. if (table.count (k) != 0)
  4607. duplicates.push_back (k);
  4608. table[k] = unescape (value);
  4609. }
  4610. return table;
  4611. }
  4612. // string literals passed to tr() / TR_NOOP() in the sources (adjacent literals are concatenated)
  4613. std::map<std::string, std::string> sourceKeys (const std::filesystem::path& srcDir)
  4614. {
  4615. std::map<std::string, std::string> keys;
  4616. for (const auto& entry : std::filesystem::recursive_directory_iterator (srcDir))
  4617. {
  4618. const auto ext = entry.path().extension().string();
  4619. if (! entry.is_regular_file() || (ext != ".cpp" && ext != ".h"))
  4620. continue;
  4621. // drop // comment lines (they may quote tr ("...") as an example)
  4622. std::string text, line;
  4623. std::istringstream in (readFile (entry.path()));
  4624. while (std::getline (in, line))
  4625. {
  4626. const auto first = line.find_first_not_of (" \t");
  4627. text += (first != std::string::npos && line.compare (first, 2, "//") == 0) ? std::string() : line;
  4628. text += '\n';
  4629. }
  4630. auto isIdent = [] (char c) { return std::isalnum ((unsigned char) c) || c == '_'; };
  4631. for (const std::string name : { "tr", "TR_NOOP" })
  4632. for (auto at = text.find (name); at != std::string::npos; at = text.find (name, at + 1))
  4633. {
  4634. if ((at > 0 && isIdent (text[at - 1])) || isIdent (text[at + name.size()]))
  4635. continue;
  4636. auto pos = text.find_first_not_of (" \t\n", at + name.size());
  4637. if (pos == std::string::npos || text[pos] != '(')
  4638. continue;
  4639. pos = text.find_first_not_of (" \t\n", pos + 1);
  4640. std::string key, raw;
  4641. bool any = false;
  4642. while (pos != std::string::npos && quoted (text, pos, raw))
  4643. {
  4644. key += raw;
  4645. any = true;
  4646. pos = text.find_first_not_of (" \t\n", pos);
  4647. }
  4648. if (any)
  4649. keys.emplace (unescape (key), entry.path().filename().string());
  4650. }
  4651. }
  4652. return keys;
  4653. }
  4654. std::set<std::string> placeholders (const std::string& s)
  4655. {
  4656. std::set<std::string> found;
  4657. for (auto open = s.find ('{'); open != std::string::npos; open = s.find ('{', open + 1))
  4658. if (const auto close = s.find ('}', open); close != std::string::npos && close - open <= 3)
  4659. found.insert (s.substr (open, close - open + 1));
  4660. return found;
  4661. }
  4662. } // namespace i18ntest
  4663. TEST (translations_cover_every_ui_string)
  4664. {
  4665. using namespace i18ntest;
  4666. const auto text = readFile ("resources/i18n/pt-BR.txt");
  4667. CHECK (! text.empty());
  4668. std::vector<std::string> duplicates;
  4669. const auto table = parseTable (text, duplicates);
  4670. CHECK (table.size() > 300);
  4671. CHECK (duplicates.empty());
  4672. for (const auto& d : duplicates)
  4673. std::printf (" duplicate pt-BR entry: \"%s\"\n", d.c_str());
  4674. const auto keys = sourceKeys ("src");
  4675. CHECK (keys.size() > 300);
  4676. int missing = 0;
  4677. for (const auto& [key, file] : keys)
  4678. if (auto it = table.find (key); it == table.end() || it->second.empty())
  4679. {
  4680. ++missing;
  4681. std::printf (" no pt-BR translation for \"%s\" (%s)\n", key.c_str(), file.c_str());
  4682. }
  4683. CHECK (missing == 0);
  4684. // no empty values, and every {n} placeholder survives the translation
  4685. int empty = 0, badPlaceholders = 0;
  4686. for (const auto& [english, portuguese] : table)
  4687. {
  4688. empty += portuguese.empty() ? 1 : 0;
  4689. if (placeholders (english) != placeholders (portuguese))
  4690. {
  4691. ++badPlaceholders;
  4692. std::printf (" placeholders differ: \"%s\"\n", english.c_str());
  4693. }
  4694. }
  4695. CHECK (empty == 0);
  4696. CHECK (badPlaceholders == 0);
  4697. }
  4698. TEST (translations_embedded_table_is_up_to_date)
  4699. {
  4700. // src/ui/I18nData.h must be regenerated (tools/make_i18n.py) whenever the .txt changes
  4701. const auto text = i18ntest::readFile ("resources/i18n/pt-BR.txt");
  4702. CHECK (text.size() == sizeof (lynx::i18n::data::ptBR));
  4703. CHECK (std::memcmp (text.data(), lynx::i18n::data::ptBR, std::min (text.size(), sizeof (lynx::i18n::data::ptBR))) == 0);
  4704. }
  4705. // --- workflow shell: pattern/channel operations, key map, workspace layout, control math -------------
  4706. TEST (workflow_pattern_navigation_and_insert)
  4707. {
  4708. auto song = lynx::Song::makeDefault();
  4709. song.patterns.resize (3);
  4710. song.patterns[1].name = "B";
  4711. song.patterns[2].name = "C";
  4712. song.patterns[2].notes.push_back ({});
  4713. song.currentPattern = 0;
  4714. CHECK (lynx::stepPattern (song, 1) == 1 && lynx::stepPattern (song, -1) == 0 && lynx::stepPattern (song, 10) == 2);
  4715. // pattern 0 has the default notes, 1 is empty, 2 has a note
  4716. CHECK (! lynx::isPatternEmpty (song.patterns[0]) && lynx::isPatternEmpty (song.patterns[1]));
  4717. CHECK (lynx::findNextEmptyPattern (song, 0) == 1 && lynx::findNextEmptyPattern (song, 1) == -1);
  4718. CHECK (lynx::gotoNextEmptyPattern (song) == 1 && song.currentPattern == 1);
  4719. song.patterns[1].notes.push_back ({});
  4720. const auto before = song.patterns.size();
  4721. CHECK (lynx::gotoNextEmptyPattern (song) == (int) before && song.patterns.size() == before + 1);
  4722. CHECK (song.patterns.back().name == "Pattern 1" || song.patterns.back().name.rfind ("Pattern ", 0) == 0);
  4723. // insert keeps Playlist clips on their patterns
  4724. lynx::Clip clip;
  4725. clip.pattern = 2;
  4726. song.playlist = { clip };
  4727. song.arrangements.push_back ({ "Other", { clip }, 0, 0, {} });
  4728. CHECK (lynx::insertPattern (song, 1, "New") == 1 && song.currentPattern == 1);
  4729. CHECK (song.patterns[1].name == "New" && song.patterns[1].notes.empty() && song.patterns[3].name == "C");
  4730. CHECK (song.playlist[0].pattern == 3 && song.arrangements[1].clips[0].pattern == 3);
  4731. }
  4732. TEST (workflow_split_pattern_by_channel)
  4733. {
  4734. auto song = lynx::Song::makeDefault();
  4735. song.patterns.resize (1);
  4736. auto& p = song.patterns[0];
  4737. p.name = "Beat";
  4738. p.notes.clear();
  4739. for (int i = 0; i < 4; ++i)
  4740. p.notes.push_back ({ (std::int64_t) i * lynx::ticksPerStep, lynx::ticksPerStep, i % 2 == 0 ? 0 : 2, 60 });
  4741. song.channels[2].colour = 0xff112233;
  4742. lynx::Clip clip;
  4743. clip.pattern = 0;
  4744. song.playlist = { clip };
  4745. const auto created = lynx::splitPatternByChannel (song, 0);
  4746. CHECK (created == 2 && song.patterns.size() == 3);
  4747. CHECK (song.patterns[0].notes.size() == 4); // the original is unchanged
  4748. CHECK (song.patterns[1].name == "Beat - " + song.channels[0].name && song.patterns[1].notes.size() == 2);
  4749. CHECK (song.patterns[2].colour == 0xff112233u && song.patterns[2].notes.size() == 2 && song.patterns[2].notes[0].channel == 2);
  4750. CHECK (song.playlist[0].pattern == 0);
  4751. CHECK (lynx::splitPatternByChannel (song, 99) == 0);
  4752. }
  4753. TEST (workflow_channel_selection)
  4754. {
  4755. lynx::ChannelSelection s;
  4756. s.selectOnly (2);
  4757. s.toggle (5);
  4758. s.toggle (0);
  4759. CHECK ((s.rows == std::vector<int> { 0, 2, 5 }) && s.contains (5) && ! s.contains (3));
  4760. s.toggle (2);
  4761. CHECK ((s.rows == std::vector<int> { 0, 5 }));
  4762. s.selectOnly (3);
  4763. s.extendTo (6);
  4764. CHECK ((s.rows == std::vector<int> { 3, 4, 5, 6 }));
  4765. s.extendTo (1);
  4766. CHECK ((s.rows == std::vector<int> { 1, 2, 3 }));
  4767. s.selectOnly (4);
  4768. s.toggle (4); // the last one stays
  4769. CHECK ((s.rows == std::vector<int> { 4 }));
  4770. s.selectAll (3);
  4771. CHECK ((s.rows == std::vector<int> { 0, 1, 2 }));
  4772. s.rows = { 7, 9 };
  4773. s.clamp (5);
  4774. CHECK (s.rows.size() == 1 && s.rows[0] < 5);
  4775. }
  4776. TEST (workflow_move_clone_insert_channels)
  4777. {
  4778. auto song = lynx::Song::makeDefault();
  4779. const auto names = [&] {
  4780. std::vector<std::string> n;
  4781. for (const auto& c : song.channels)
  4782. n.push_back (c.name);
  4783. return n;
  4784. };
  4785. const auto original = names();
  4786. const auto count = (int) song.channels.size();
  4787. CHECK (count >= 4);
  4788. // references that must follow their channel
  4789. song.patterns[0].notes.push_back ({ 0, 240, 1, 64 });
  4790. lynx::Automation a;
  4791. a.target = { lynx::AutomationTargetKind::channelVolume, 1 };
  4792. song.automations.push_back (a);
  4793. song.midiLinks.push_back ({ 7, -1, { lynx::AutomationTargetKind::channelPan, 3 } });
  4794. lynx::Clip audio;
  4795. audio.kind = lynx::ClipKind::audio;
  4796. audio.channel = 3;
  4797. song.playlist.push_back (audio);
  4798. std::vector<int> selection { 1, 3 };
  4799. CHECK (lynx::moveChannels (song, selection, -1));
  4800. CHECK ((selection == std::vector<int> { 0, 2 }));
  4801. CHECK (song.channels[0].name == original[1] && song.channels[1].name == original[0] && song.channels[2].name == original[3]
  4802. && song.channels[3].name == original[2]);
  4803. CHECK (song.automations[0].target.index == 0 && song.midiLinks[0].target.index == 2 && song.playlist.back().channel == 2);
  4804. CHECK (std::any_of (song.patterns[0].notes.begin(), song.patterns[0].notes.end(),
  4805. [] (const lynx::Note& n) { return n.channel == 0 && n.key == 64; }));
  4806. CHECK (! lynx::moveChannels (song, selection, -1)); // the block is at the top
  4807. CHECK (lynx::moveChannels (song, selection, 1) && (selection == std::vector<int> { 1, 3 }) && names() == original);
  4808. // clone: settings, not notes, right below, with a new name
  4809. song.channels[1].volume = 0.33f;
  4810. const auto notesBefore = song.patterns[0].notes.size();
  4811. const auto clone = lynx::cloneChannel (song, 1);
  4812. CHECK (clone == 2 && (int) song.channels.size() == count + 1);
  4813. CHECK (song.channels[2].name == original[1] + " #2" && song.channels[2].volume == 0.33f);
  4814. CHECK (song.patterns[0].notes.size() == notesBefore);
  4815. CHECK (song.channels[3].name == original[2] && song.automations[0].target.index == 1);
  4816. CHECK (lynx::cloneChannel (song, 1) == 2 && song.channels[2].name == original[1] + " #3");
  4817. // remove several
  4818. CHECK (lynx::removeChannels (song, { 2, 3, 2 }) == 2 && (int) song.channels.size() == count);
  4819. CHECK (names() == original);
  4820. }
  4821. TEST (workflow_send_channels_to_free_inserts)
  4822. {
  4823. auto song = lynx::Song::makeDefault();
  4824. for (auto& c : song.channels)
  4825. c.mixerTrack = 0;
  4826. song.mixer[1].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::delay)); // not free: has effects
  4827. song.channels[0].mixerTrack = 2; // not free: used
  4828. song.setRoute (5, 3, true); // 3 is not free: receives a send
  4829. CHECK (! lynx::isInsertFree (song, 1) && ! lynx::isInsertFree (song, 2) && ! lynx::isInsertFree (song, 3) && lynx::isInsertFree (song, 4));
  4830. song.channels[1].colour = 0xff445566;
  4831. const auto tracks = lynx::sendChannelsToFreeInserts (song, { 1, 2 });
  4832. CHECK ((tracks == std::vector<int> { 4, 5 }) || (tracks == std::vector<int> { 4, 6 }));
  4833. CHECK (song.channels[1].mixerTrack == 4 && song.mixer[4].name == song.channels[1].name && song.mixer[4].colour == 0xff445566u);
  4834. CHECK (song.mixer[(std::size_t) tracks[1]].name == song.channels[2].name);
  4835. // with no free insert left, a new one is added at the end
  4836. auto full = lynx::Song::makeDefault();
  4837. full.mixer = lynx::Song::defaultMixer (1);
  4838. full.channels[0].mixerTrack = 1;
  4839. const auto added = lynx::sendChannelsToFreeInserts (full, { 1 });
  4840. CHECK (added.size() == 1 && added[0] == 2 && full.mixer.size() == 3 && full.channels[1].mixerTrack == 2);
  4841. }
  4842. TEST (workflow_palette_random_colour)
  4843. {
  4844. const auto& palette = lynx::lynxPalette();
  4845. for (std::uint32_t seed = 0; seed < 50; ++seed)
  4846. {
  4847. const auto current = palette[seed % palette.size()];
  4848. const auto c = lynx::randomPaletteColour (current, seed);
  4849. CHECK (c != current && std::find (palette.begin(), palette.end(), c) != palette.end());
  4850. }
  4851. }
  4852. TEST (workflow_key_map)
  4853. {
  4854. using namespace lynx::keys;
  4855. KeyMap map;
  4856. const auto global = Context::global, rack = Context::channelRack;
  4857. CHECK (map.find ({ space, none }, global, true) == "transport.playStop");
  4858. CHECK (map.find ({ space, ctrl }, global, true) == "transport.pause");
  4859. CHECK (map.find ({ f6, none }, global, true) == "panel.channelRack");
  4860. CHECK (map.find ({ f8, alt }, global, false) == "panel.browser");
  4861. CHECK (map.find ({ insert, ctrl | shift }, global, false) == "pattern.insert");
  4862. CHECK (map.find ({ numpadPlus, none }, rack, true) == "pattern.next");
  4863. // panel context: only while focused
  4864. CHECK (map.find ({ 'L', ctrl }, rack, true) == "rack.sendToInserts");
  4865. CHECK (map.find ({ 'L', ctrl }, global, true).empty());
  4866. CHECK (map.find ({ up, alt }, rack, false) == "rack.moveUp");
  4867. // typing piano: note keys play, other plain keys only for whilePiano actions; chords always work
  4868. CHECK (KeyMap::playsNote ({ 'Z', none }, true) && ! KeyMap::playsNote ({ 'Z', none }, false) && ! KeyMap::playsNote ({ 'Z', ctrl }, true));
  4869. CHECK (map.find ({ 'L', none }, global, true) == "transport.toggleMode");
  4870. CHECK (map.find ({ '1', none }, rack, false) == "rack.mute1" && map.find ({ '0', none }, rack, false) == "rack.mute10");
  4871. CHECK (map.find ({ '1', none }, rack, true).empty()); // a plain digit is not a shortcut while the piano is on
  4872. CHECK (map.find ({ '2', ctrl }, rack, true) == "rack.solo2");
  4873. CHECK (typingPianoNote ('Z') == 60 && typingPianoNote ('q') == 72 && typingPianoNote ('P') == 88 && typingPianoNote ('L') == -1);
  4874. CHECK (map.conflicts().empty());
  4875. // chord text
  4876. CHECK (toString ({ f4, ctrl | shift }) == "Ctrl+Shift+F4" && toString ({ numpadMinus, none }) == "Numpad -");
  4877. CHECK (parseChord ("Shift+Ctrl+Ins") == (Chord { insert, ctrl | shift }));
  4878. CHECK (parseChord ("alt+up") == (Chord { up, alt }) && parseChord ("k") == (Chord { 'K', none }));
  4879. CHECK (parseChord ("Numpad +") == (Chord { numpadPlus, none }) && ! parseChord ("F13") && ! parseChord ("Ctrl+"));
  4880. for (const auto& a : actions())
  4881. for (const auto& c : a.defaults)
  4882. CHECK (parseChord (toString (c)) == c);
  4883. // JSON overrides
  4884. std::vector<std::string> errors;
  4885. CHECK (map.loadJson (R"({ "pattern.next": "Ctrl+Right", "rack.clone": ["Alt+D", "Ctrl+D"], "help.shortcuts": "", "nope": "F1", "file.open": "Hyper+Q" })", &errors));
  4886. CHECK (errors.size() == 2);
  4887. CHECK (map.find ({ right, ctrl }, global, false) == "pattern.next" && map.find ({ numpadPlus, none }, global, false).empty());
  4888. CHECK (map.find ({ 'D', ctrl }, rack, false) == "rack.clone" && map.find ({ f1, none }, global, false).empty());
  4889. map.setChords ("pattern.previous", { { right, ctrl } });
  4890. CHECK (map.conflicts().size() == 1);
  4891. KeyMap reloaded;
  4892. CHECK (reloaded.loadJson (map.toJson()) && reloaded.chordsFor ("rack.clone") == map.chordsFor ("rack.clone"));
  4893. CHECK (! map.loadJson ("[1, 2]") && ! map.loadJson ("{\"a\": }"));
  4894. map.resetToDefaults();
  4895. CHECK (map.find ({ numpadPlus, none }, global, false) == "pattern.next");
  4896. }
  4897. TEST (workflow_workspace_layout)
  4898. {
  4899. using namespace lynx::workspace;
  4900. auto l = Layout::makeDefault (1280, 620);
  4901. // every visible panel is inside the desktop; the default shows Browser, Playlist and Channel Rack
  4902. for (const auto& p : l.panels)
  4903. CHECK (p.bounds.x >= 0 && p.bounds.y >= 0 && p.bounds.right() <= 1280 && p.bounds.bottom() <= 620 && p.bounds.w >= minWidth);
  4904. CHECK (l[Panel::browser].visible && l[Panel::playlist].visible && l[Panel::channelRack].visible);
  4905. CHECK (! l[Panel::pianoRoll].visible && ! l[Panel::mixer].visible);
  4906. CHECK (l[Panel::playlist].bounds.x == l[Panel::browser].bounds.right() && l[Panel::playlist].bounds.right() == 1280);
  4907. CHECK (l.isFrontmost (Panel::channelRack) && l.focused == Panel::channelRack);
  4908. // toggle: show+focus, focus, hide
  4909. CHECK (l.toggle (Panel::mixer) && l.focused == Panel::mixer && l.isFrontmost (Panel::mixer));
  4910. CHECK (l.toggle (Panel::channelRack) && l.focused == Panel::channelRack && l.isFrontmost (Panel::channelRack));
  4911. CHECK (! l.toggle (Panel::channelRack) && ! l[Panel::channelRack].visible && l.focused == Panel::mixer);
  4912. l.toggleMaximised (Panel::playlist);
  4913. CHECK (l[Panel::playlist].maximised && l.focused == Panel::playlist);
  4914. // save and load at another size: positions scale
  4915. l[Panel::mixer].detached = true;
  4916. l[Panel::mixer].windowBounds = { 100, 50, 900, 500 };
  4917. const auto text = l.serialise();
  4918. const auto big = Layout::parse (text, 2560, 1240);
  4919. CHECK (big.has_value());
  4920. if (big)
  4921. {
  4922. CHECK ((*big)[Panel::playlist].maximised && (*big)[Panel::mixer].detached && (*big)[Panel::mixer].windowBounds.w == 900);
  4923. CHECK (std::abs ((*big)[Panel::playlist].bounds.x - 2 * l[Panel::playlist].bounds.x) <= 1);
  4924. CHECK (big->order == l.order && big->focused == l.focused);
  4925. }
  4926. CHECK (! Layout::parse ("garbage", 100, 100) && ! Layout::parse ("lynx-layout 1\npanel nowhere 1 0 0 0 0 1 1 0 0 0 0\n", 100, 100));
  4927. // resize keeps panels inside and edges glued to the border
  4928. l.resize (1000, 500);
  4929. CHECK (l[Panel::playlist].bounds.right() == 1000 && l[Panel::browser].bounds.bottom() == 500);
  4930. for (const auto& p : l.panels)
  4931. CHECK (p.bounds.right() <= 1000 && p.bounds.bottom() <= 500);
  4932. // off-screen panels come back
  4933. l[Panel::pianoRoll].bounds = { 5000, -300, 400, 300 };
  4934. CHECK (l.bringBackOffscreen() == 1 && l[Panel::pianoRoll].bounds.x + 400 <= 1000 && l[Panel::pianoRoll].bounds.y == 0);
  4935. // snapping: to the desktop, to another panel, and resizing one edge
  4936. CHECK ((snap ({ 5, 6, 200, 100 }, {}, 1000, 500) == Rect { 0, 0, 200, 100 }));
  4937. CHECK ((snap ({ 304, 50, 200, 100 }, { { 0, 0, 300, 400 } }, 1000, 500) == Rect { 300, 50, 200, 100 }));
  4938. CHECK ((snap ({ 304, 50, 200, 100 }, { { 0, 0, 300, 400 } }, 1000, 500, 2) == Rect { 304, 50, 200, 100 })); // out of range
  4939. CHECK ((snap ({ 100, 100, 195, 100 }, { { 300, 0, 300, 400 } }, 1000, 500, 8, right) == Rect { 100, 100, 200, 100 }));
  4940. CHECK ((snap ({ 100, 100, 195, 100 }, { { 300, 1000, 300, 400 } }, 2000, 2000, 8, right) == Rect { 100, 100, 195, 100 })); // not facing
  4941. }
  4942. TEST (workflow_control_math)
  4943. {
  4944. using namespace lynx::controls;
  4945. CHECK (std::abs (dragValue (0.5, 75, 0, 1, 150, false) - 1.0) < 1e-9);
  4946. CHECK (std::abs (dragValue (0.5, 75, 0, 1, 150, true) - 0.55) < 1e-9);
  4947. CHECK (dragValue (0.5, -1000, -1, 1, 150, false) == -1.0);
  4948. CHECK (std::abs (wheelValue (0.5, 1, 0, 1, false) - 0.52) < 1e-9 && std::abs (wheelValue (0.5, -1, 0, 1, true) - 0.498) < 1e-9);
  4949. CHECK (wheelValue (3, 1, 0, 10, true, 1) == 4.0); // integer controls move at least one step
  4950. CHECK (std::abs (snapToInterval (0.234, 0, 0.1) - 0.2) < 1e-9);
  4951. CHECK (tempoDrag (130.0, 8, false, false) == 132.0 && tempoDrag (130.0, -3, false, false) == 130.0);
  4952. CHECK (std::abs (tempoDrag (130.0, 25, true, false) - 130.25) < 1e-9);
  4953. CHECK (std::abs (tempoDrag (130.0, 8, false, true) - 130.2) < 1e-9);
  4954. CHECK (tempoDrag (520.0, 400, false, false) == lynx::maxTempo && halfTempo (15.0) == lynx::minTempo && doubleTempo (130.0) == 260.0);
  4955. TapTempo tap;
  4956. CHECK (! tap.tap (1000.0));
  4957. CHECK (tap.tap (1500.0) == 120.0);
  4958. CHECK (tap.tap (2000.0) == 120.0 && tap.count() == 3);
  4959. CHECK (std::abs (*tap.tap (2400.0) - 128.57) < 0.01); // average of 500, 500, 400 ms
  4960. CHECK (! tap.tap (9000.0) && tap.count() == 1); // a long pause starts over
  4961. }
  4962. int main (int argc, char** argv)
  4963. {
  4964. if (argc > 1)
  4965. realFlpDir = argv[1];
  4966. const char* only = std::getenv ("LYNX_TEST"); // run only the tests whose name contains this
  4967. for (auto& [name, fn] : registry())
  4968. {
  4969. if (only != nullptr && std::strstr (name, only) == nullptr)
  4970. continue;
  4971. std::printf ("%s\n", name);
  4972. fn();
  4973. }
  4974. std::printf ("\n%d checks, %d failures\n", checks, failures);
  4975. return failures == 0 ? 0 : 1;
  4976. }
  4977. TEST (synth_cutoff_keytrack)
  4978. {
  4979. // the cutoff follows the note from key 48 (130.81 Hz), like Harmor's "Key to filter frequency"
  4980. auto rms = [] (float keyTrack, int key) {
  4981. lynx::SynthSettings s;
  4982. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  4983. s.cutoff = 130.81f;
  4984. s.keyTrack = keyTrack;
  4985. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  4986. inst->prepare (48000.0);
  4987. lynx::ChannelDef c;
  4988. c.synth = s;
  4989. inst->configure (c, 120.0);
  4990. inst->noteOn (key, 1.0f);
  4991. std::vector<float> l (24000), r (24000);
  4992. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  4993. double e = 0.0;
  4994. for (int i = 4800; i < 24000; ++i)
  4995. e += (double) l[(std::size_t) i] * l[(std::size_t) i];
  4996. return std::sqrt (e / 19200.0);
  4997. };
  4998. const auto fixed = rms (0.0f, 72), tracked = rms (1.0f, 72);
  4999. CHECK (tracked > 4.0 * fixed); // 2 octaves up: without keytrack the low-pass cuts
  5000. CHECK (std::abs (rms (1.0f, 48) - rms (0.0f, 48)) < 0.05 * rms (0.0f, 48)); // unchanged at the reference key
  5001. }
  5002. namespace
  5003. {
  5004. std::vector<float> renderSynthNote (const lynx::SynthSettings& s, int key, int frames)
  5005. {
  5006. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  5007. inst->prepare (48000.0);
  5008. lynx::ChannelDef c;
  5009. c.synth = s;
  5010. inst->configure (c, 120.0);
  5011. inst->noteOn (key, 1.0f);
  5012. std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
  5013. inst->render (l.data(), r.data(), frames, 1.0f, 1.0f);
  5014. return l;
  5015. }
  5016. double goertzel (const std::vector<float>& x, double f, int from, int to)
  5017. {
  5018. const auto w = 2.0 * 3.141592653589793 * f / 48000.0, k = 2.0 * std::cos (w);
  5019. double s1 = 0.0, s2 = 0.0;
  5020. for (int i = from; i < to; ++i)
  5021. {
  5022. const auto s0 = x[(std::size_t) i] + k * s1 - s2;
  5023. s2 = s1;
  5024. s1 = s0;
  5025. }
  5026. return s1 * s1 + s2 * s2 - k * s1 * s2;
  5027. }
  5028. } // namespace
  5029. TEST (synth_pitch_sweep_ring_and_bands)
  5030. {
  5031. lynx::SynthSettings sine;
  5032. sine.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  5033. sine.cutoff = 20000.0f;
  5034. // sweep: starts at f·(1 + sweep) and returns to f (time constant s = 10 ms; s⁴ falls 4× faster)
  5035. auto sweep = sine;
  5036. sweep.sweep = 1.0f;
  5037. sweep.sweepTime = 0.01f;
  5038. const auto a = renderSynthNote (sweep, 69, 24000);
  5039. auto crossings = [] (const std::vector<float>& x, int to) {
  5040. int n = 0;
  5041. for (int i = 1; i < to; ++i)
  5042. n += (x[(std::size_t) i - 1] < 0.0f) != (x[(std::size_t) i] < 0.0f);
  5043. return n;
  5044. };
  5045. CHECK (crossings (a, 480) > 1.3 * crossings (renderSynthNote (sine, 69, 480), 480)); // 10 ms: much higher
  5046. CHECK (goertzel (a, 440.0, 12000, 24000) > 100.0 * goertzel (a, 880.0, 12000, 24000));
  5047. // ring with |a| = 1: carrier 440 × modulator 110 -> only 330 and 550, no 440
  5048. auto ring = sine;
  5049. ring.am = ring.ring = true;
  5050. ring.osc[2] = { lynx::Waveform::sine, -2, 0.0f, 1.0f };
  5051. const auto b = renderSynthNote (ring, 69, 24000);
  5052. CHECK (goertzel (b, 330.0, 4800, 24000) > 100.0 * goertzel (b, 440.0, 4800, 24000));
  5053. CHECK (goertzel (b, 550.0, 4800, 24000) > 100.0 * goertzel (b, 440.0, 4800, 24000));
  5054. // 3 bands: muting the high knocks down a 4 kHz note and barely touches an 80 Hz one
  5055. auto bands = sine;
  5056. bands.bandGain = { 1.0f, 1.0f, 0.0f };
  5057. auto rms = [] (const std::vector<float>& x) {
  5058. double e = 0.0;
  5059. for (std::size_t i = 4800; i < x.size(); ++i)
  5060. e += (double) x[i] * x[i];
  5061. return std::sqrt (e / (double) (x.size() - 4800));
  5062. };
  5063. CHECK (rms (renderSynthNote (bands, 107, 24000)) < 0.3 * rms (renderSynthNote (sine, 107, 24000)));
  5064. CHECK (rms (renderSynthNote (bands, 28, 24000)) > 0.85 * rms (renderSynthNote (sine, 28, 24000)));
  5065. }
  5066. TEST (love_philter_map_evaluation)
  5067. {
  5068. lynx::LovePhilterMap line { { { 0.0, 0.0 }, { 1.0, 1.0 } } };
  5069. CHECK (std::abs (lynx::lovePhilterMapAt (line, 0.5) - 0.5) < 1e-9);
  5070. CHECK (std::abs (lynx::lovePhilterMapAt (line, 1.0) - 1.0) < 1e-9);
  5071. // steps (Δx = 0): 1 until 0.4375, ramp to 0 at 0.5, then 0
  5072. lynx::LovePhilterMap steps { { { 0.0, 1.0 }, { 0.4375, 1.0 }, { 0.0625, 0.0 }, { 0.5, 0.0 } } };
  5073. CHECK (lynx::lovePhilterMapAt (steps, 0.27) == 1.0);
  5074. CHECK (lynx::lovePhilterMapAt (steps, 0.75) == 0.0);
  5075. CHECK (std::abs (lynx::lovePhilterMapAt (steps, 0.46875) - 0.5) < 1e-9);
  5076. CHECK (lynx::lovePhilterMapAt ({}, 0.3) == 1.0);
  5077. }
  5078. TEST (lofi_holds_and_quantizes)
  5079. {
  5080. auto fx = lynx::makeEffect (lynx::EffectKind::lofi);
  5081. fx->prepare (48000.0);
  5082. auto slot = lynx::makeEffectSlot (lynx::EffectKind::lofi);
  5083. slot.params[0] = 0.5f; // 128·0.125 + 1 = 17 samples
  5084. slot.params[1] = 1.0f; // 65536/2049 ≈ 32 levels
  5085. std::vector<float> l (170), r (170);
  5086. for (std::size_t i = 0; i < l.size(); ++i)
  5087. l[i] = r[i] = 0.9f * (float) std::sin (0.05 * (double) i);
  5088. const auto original = l;
  5089. fx->process (l.data(), r.data(), 170, slot.params, 120.0);
  5090. for (int i = 0; i < 17; ++i)
  5091. CHECK (l[(std::size_t) i] == l[0]);
  5092. CHECK (l[17] != l[16]);
  5093. const auto step = 2.0f * 2049.0f / 65536.0f;
  5094. for (auto v : l)
  5095. CHECK (std::abs (v / step - std::round (v / step)) < 1e-3f);
  5096. CHECK (std::abs (l[0] - original[0]) <= step);
  5097. CHECK (lynx::formatEffectParam (lynx::EffectKind::lofi, 1, 0.0f, 120.0) == "16.0 bits");
  5098. }
  5099. namespace
  5100. {
  5101. lynx::EffectParams effectorParams (int type, float x, float y)
  5102. {
  5103. auto slot = lynx::makeEffectSlot (lynx::EffectKind::effector);
  5104. slot.params[0] = (float) type / 11.0f;
  5105. slot.params[3] = x;
  5106. slot.params[4] = y;
  5107. return slot.params;
  5108. }
  5109. } // namespace
  5110. TEST (effector_algorithms)
  5111. {
  5112. CHECK (lynx::effector::typeOf (8.0f / 11.0f) == 8);
  5113. CHECK (std::string (lynx::effector::typeName (11)) == "Ring");
  5114. CHECK (std::abs (lynx::effector::rateBeats (0.5f, 0.0f) - 0.75) < 1e-9); // trunc(8·0.5 + 0.5) = 4 -> 3/4
  5115. CHECK (std::abs (lynx::effector::rateBeats (1.0f, 0.5f) - 2.0) < 1e-9); // 8 × (1 − 0.5)²
  5116. auto run = [] (const lynx::EffectParams& p, std::vector<float> in, double bpm = 120.0) {
  5117. auto fx = lynx::makeEffect (lynx::EffectKind::effector);
  5118. fx->prepare (48000.0);
  5119. fx->setSongPosition (0.0);
  5120. auto r = in;
  5121. fx->process (in.data(), r.data(), (int) in.size(), p, bpm);
  5122. return in;
  5123. };
  5124. // Distort with Y = 1 (everything goes to the cascade): 0.5 comes out 0.5 (compensation G = 0.5/F(0.5))
  5125. {
  5126. auto p = effectorParams (0, 0.5f, 1.0f);
  5127. const auto out = run (p, std::vector<float> (4800, 0.5f));
  5128. CHECK (std::abs (out.back() - 0.5f) < 1e-3f);
  5129. const auto small = run (p, std::vector<float> (4800, 0.05f));
  5130. CHECK (small.back() > 0.45f && small.back() < 0.5f); // saturates near ±0.5
  5131. }
  5132. // Ring: DC 1 becomes the carrier sine, 2^(8.7·0 + 5) = 32 Hz -> 32 cycles per second
  5133. {
  5134. auto p = effectorParams (11, 0.0f, 0.0f);
  5135. const auto out = run (p, std::vector<float> (48000, 1.0f));
  5136. int crossings = 0;
  5137. for (std::size_t i = 4800; i < out.size(); ++i)
  5138. crossings += (out[i - 1] < 0.0f) != (out[i] < 0.0f);
  5139. CHECK (crossings >= 56 && crossings <= 60); // 0.9 s × 32 Hz × 2
  5140. }
  5141. // Trancer, Y = 0: at the start of the period the gain drops to zero; X = 1 returns only at the end of the period
  5142. {
  5143. auto p = effectorParams (8, 1.0f, 0.0f);
  5144. p[5] = 1.0f; // 8/1 = 8 beats = 4 s at 120 BPM
  5145. const auto out = run (p, std::vector<float> (48000, 1.0f));
  5146. CHECK (out[100] > 0.3f); // still rising on the "and" (1% of the period = 40 ms)
  5147. CHECK (out[24000] < 0.05f); // 0.5 s: trough
  5148. }
  5149. // Reverb with Wet = 0: the dry passes intact
  5150. {
  5151. auto p = effectorParams (6, 0.5f, 0.5f);
  5152. p[2] = 0.0f;
  5153. std::vector<float> in (4800);
  5154. for (std::size_t i = 0; i < in.size(); ++i)
  5155. in[i] = (float) std::sin (0.01 * (double) i);
  5156. const auto out = run (p, in);
  5157. for (std::size_t i = 0; i < in.size(); ++i)
  5158. CHECK (std::abs (out[i] - in[i]) < 1e-6f);
  5159. }
  5160. // Delay: the echo arrives after the Mod Rate period (1/4 beat at 120 BPM = 125 ms), at level X²
  5161. {
  5162. auto p = effectorParams (5, 0.7f, 0.5f);
  5163. p[5] = 1.0f / 8.0f; // index 1 = 1/4
  5164. std::vector<float> in (12000, 0.0f);
  5165. in[0] = 1.0f;
  5166. const auto out = run (p, in);
  5167. CHECK (std::abs (out[6000] - 0.49f) < 0.02f);
  5168. CHECK (std::abs (out[3000]) < 1e-6f);
  5169. }
  5170. // Stutter: repeats the recorded start every period
  5171. {
  5172. auto p = effectorParams (9, 0.0f, 0.0f);
  5173. p[5] = 0.0f; // 1/8 beat = 62.5 ms = 3000 samples
  5174. std::vector<float> in (9000);
  5175. for (std::size_t i = 0; i < in.size(); ++i)
  5176. in[i] = (float) i / 9000.0f;
  5177. const auto out = run (p, in);
  5178. CHECK (std::abs (out[3000 + 500] - in[500]) < 0.01f); // after the crossfade, it's the start again
  5179. CHECK (std::abs (out[1000] - in[1000]) < 1e-6f);
  5180. }
  5181. }
  5182. TEST (crossover_linkwitz_riley_sums_flat)
  5183. {
  5184. // low band + high band of the same crossover: sum with magnitude 1 (Linkwitz-Riley; for 12 and 36 dB the HP is inverted)
  5185. for (const auto combo : { 1, 3, 4, 5, 6, 7 })
  5186. for (const auto testHz : { 200.0, 1000.0, 5000.0 })
  5187. {
  5188. auto run = [&] (bool high) {
  5189. auto fx = lynx::makeEffect (lynx::EffectKind::crossover);
  5190. fx->prepare (48000.0);
  5191. auto slot = lynx::makeEffectSlot (lynx::EffectKind::crossover);
  5192. const auto value = (float) (std::log2 (1000.0 / 30.0) / 9.0589);
  5193. slot.params[high ? 0 : 3] = 1.0f;
  5194. slot.params[high ? 1 : 4] = value;
  5195. slot.params[high ? 2 : 5] = (float) combo / 7.0f;
  5196. std::vector<float> l (24000), r (24000);
  5197. for (std::size_t i = 0; i < l.size(); ++i)
  5198. l[i] = r[i] = (float) std::sin (2.0 * 3.141592653589793 * testHz * (double) i / 48000.0);
  5199. fx->process (l.data(), r.data(), 24000, slot.params, 120.0);
  5200. return l;
  5201. };
  5202. const auto low = run (false), high = run (true);
  5203. double peak = 0.0;
  5204. for (std::size_t i = 12000; i < low.size(); ++i)
  5205. peak = std::max (peak, (double) std::abs (low[i] + high[i]));
  5206. CHECK (std::abs (peak - 1.0) < 0.02);
  5207. }
  5208. // 96 dB/oct: two octaves above the cutoff the low-pass is already below −150 dB... in practice, very low
  5209. auto fx = lynx::makeEffect (lynx::EffectKind::crossover);
  5210. fx->prepare (48000.0);
  5211. auto slot = lynx::makeEffectSlot (lynx::EffectKind::crossover);
  5212. slot.params[3] = 1.0f;
  5213. slot.params[4] = (float) (std::log2 (500.0 / 30.0) / 9.0589);
  5214. slot.params[5] = 1.0f;
  5215. std::vector<float> l (24000), r (24000);
  5216. for (std::size_t i = 0; i < l.size(); ++i)
  5217. l[i] = r[i] = (float) std::sin (2.0 * 3.141592653589793 * 2000.0 * (double) i / 48000.0);
  5218. fx->process (l.data(), r.data(), 24000, slot.params, 120.0);
  5219. float peak = 0.0f;
  5220. for (std::size_t i = 12000; i < l.size(); ++i)
  5221. peak = std::max (peak, std::abs (l[i]));
  5222. CHECK (peak < 1e-4f);
  5223. }
  5224. TEST (rhythm_gain_and_autopan)
  5225. {
  5226. // 1-beat gate: 1 → 0 at 0.5 beat (linear) → 1 at the end; at 120 BPM 1 beat = 24000 samples at 48 kHz
  5227. auto slot = lynx::makeEffectSlot (lynx::EffectKind::rhythmGain);
  5228. slot.params[0] = 1.0f / 64.0f;
  5229. slot.params[1] = 3.0f / 12.0f;
  5230. const float pts[3][3] = { { 0.0f, 1.0f, 0.5f }, { 0.5f, 0.0f, 0.5f }, { 1.0f, 1.0f, 0.5f } };
  5231. for (int k = 0; k < 3; ++k)
  5232. for (int j = 0; j < 3; ++j)
  5233. slot.params[(std::size_t) (2 + 3 * k + j)] = pts[k][j];
  5234. auto fx = lynx::makeEffect (lynx::EffectKind::rhythmGain);
  5235. fx->prepare (48000.0);
  5236. fx->setSongPosition (960.0 * 2.0); // beat 2: phase 0
  5237. std::vector<float> l (24000, 1.0f), r (24000, 1.0f);
  5238. fx->process (l.data(), r.data(), 24000, slot.params, 120.0);
  5239. CHECK (std::abs (l[0] - 1.0f) < 1e-3f);
  5240. CHECK (std::abs (l[6000] - 0.5f) < 1e-3f);
  5241. CHECK (l[12000] < 1e-3f);
  5242. CHECK (std::abs (l[18000] - 0.5f) < 1e-3f);
  5243. // positive tension: changes quickly at the start of the segment
  5244. CHECK (lynx::effectInfo (lynx::EffectKind::rhythmGain).params.size() == 39);
  5245. // auto-pan: 1-beat period, full depth; start phase 0.75 -> W = 0 rising (pan centered)
  5246. auto pan = lynx::makeEffectSlot (lynx::EffectKind::autoPan);
  5247. pan.params[0] = 1.0f / 64.0f;
  5248. pan.params[1] = 1.0f;
  5249. auto ap = lynx::makeEffect (lynx::EffectKind::autoPan);
  5250. ap->prepare (48000.0);
  5251. ap->setSongPosition (0.0);
  5252. std::vector<float> a (24000, 1.0f), b (24000, 1.0f);
  5253. ap->process (a.data(), b.data(), 24000, pan.params, 120.0);
  5254. CHECK (std::abs (a[0] - 1.0f) < 1e-3f && std::abs (b[0] - 1.0f) < 1e-3f);
  5255. CHECK (a[6000] < 0.01f && std::abs (b[6000] - 1.0f) < 1e-3f); // ¼ cycle later: fully right
  5256. }
  5257. TEST (vocoder_side_inputs_split_modulator_and_carrier)
  5258. {
  5259. // "Side inputs" mode (Vocodex): modulator from side input 1 (noise in the first half), carrier from side input 2
  5260. // (saw), own input silent → the output follows the side modulator; with input 1 silent, the output is silent
  5261. auto slot = lynx::makeEffectSlot (lynx::EffectKind::vocoder);
  5262. slot.params[16] = 1.0f;
  5263. auto fx = lynx::makeEffect (lynx::EffectKind::vocoder);
  5264. fx->prepare (48000.0);
  5265. std::vector<float> modL (48000), car (48000), l (48000, 0.0f), r (48000, 0.0f);
  5266. std::uint32_t seed = 1;
  5267. for (std::size_t i = 0; i < modL.size(); ++i)
  5268. {
  5269. seed = seed * 1664525u + 1013904223u;
  5270. modL[i] = i < 24000 ? ((float) (seed >> 8) / 16777216.0f - 0.5f) : 0.0f;
  5271. car[i] = (float) (2.0 * std::fmod ((double) i * 220.0 / 48000.0, 1.0) - 1.0) * 0.5f;
  5272. }
  5273. for (int done = 0; done < 48000; done += 480)
  5274. {
  5275. fx->setSidechain (modL.data() + done, modL.data() + done);
  5276. fx->setSidechain2 (car.data() + done, car.data() + done);
  5277. fx->process (l.data() + done, r.data() + done, 480, slot.params, 120.0);
  5278. }
  5279. auto rms = [&] (int from, int to) {
  5280. double e = 0.0;
  5281. for (int i = from; i < to; ++i)
  5282. e += (double) l[(std::size_t) i] * l[(std::size_t) i];
  5283. return std::sqrt (e / (to - from));
  5284. };
  5285. CHECK (rms (6000, 20000) > 0.01);
  5286. CHECK (rms (40000, 48000) < 0.1 * rms (6000, 20000));
  5287. // the side modulator configured but silent: nothing comes out (the own input is not used instead)
  5288. std::vector<float> a (4800, 0.3f), b (4800, 0.3f);
  5289. for (int done = 0; done < 4800; done += 480)
  5290. {
  5291. fx->setSidechain (nullptr, nullptr);
  5292. fx->setSidechain2 (car.data() + done, car.data() + done);
  5293. fx->process (a.data() + done, b.data() + done, 480, slot.params, 120.0);
  5294. }
  5295. CHECK (std::abs (a[4700]) < 0.05f);
  5296. }
  5297. TEST (vocoder_follows_modulator)
  5298. {
  5299. // modulator (L) plays only in the first half; carrier (R) is a continuous saw: the wet follows the modulator
  5300. auto slot = lynx::makeEffectSlot (lynx::EffectKind::vocoder);
  5301. auto fx = lynx::makeEffect (lynx::EffectKind::vocoder);
  5302. fx->prepare (48000.0);
  5303. std::vector<float> l (48000), r (48000);
  5304. std::uint32_t seed = 1;
  5305. for (std::size_t i = 0; i < l.size(); ++i)
  5306. {
  5307. seed = seed * 1664525u + 1013904223u;
  5308. l[i] = i < 24000 ? ((float) (seed >> 8) / 16777216.0f - 0.5f) : 0.0f;
  5309. r[i] = (float) (2.0 * std::fmod ((double) i * 220.0 / 48000.0, 1.0) - 1.0) * 0.5f;
  5310. }
  5311. fx->process (l.data(), r.data(), 48000, slot.params, 120.0);
  5312. auto rms = [&] (int from, int to) {
  5313. double e = 0.0;
  5314. for (int i = from; i < to; ++i)
  5315. e += (double) l[(std::size_t) i] * l[(std::size_t) i];
  5316. return std::sqrt (e / (to - from));
  5317. };
  5318. CHECK (rms (6000, 20000) > 0.01);
  5319. CHECK (rms (40000, 48000) < 0.1 * rms (6000, 20000)); // modulator silent: the vocoder fades out (100 ms release)
  5320. for (std::size_t i = 0; i < l.size(); ++i)
  5321. CHECK (l[i] == r[i] && std::isfinite (l[i])); // mono output
  5322. }
  5323. // Vocodex (REA_rodada35): a synthetic state in FL's layout (version 8, 13 maps, 20 parameters, the 0x24 block,
  5324. // 13 map-axis bytes, tone, notes)
  5325. struct VocodexSpec
  5326. {
  5327. // Wet, SG, ModVol, ModHP, CarVol, CarLP, CarNoise, CarNoiseHP, Hold, Att, AttCurve, Rel, RelCurve, Freeze, BW,
  5328. // Flatness, ModBW, ModPitch, FatPitch, FatPan
  5329. std::array<std::int32_t, 20> params { 1000, 0, 0, 0, 0, 65536, 0, 0, 0, 2000, 0, 3000, 0, 0, 1000, 0, 2000, 0, 0, 0 };
  5330. std::array<std::int32_t, 7> block { 0, 2, 0, 24, 1, 1, 2 }; // hold mode, order, unison, bands, draft field, ModIn, CarIn
  5331. std::array<std::uint8_t, 8> flags { 0, 1, 0, 1, 1, 0, 0, 0 }; // L-R, MIN TIMES, TO MOD, THREADED, subtractive, TO NOISE, FROM CAR, FROM CONS
  5332. std::vector<std::uint8_t> bytes() const
  5333. {
  5334. std::vector<std::uint8_t> d;
  5335. auto put = [&d] (const void* p, std::size_t n) { d.insert (d.end(), (const std::uint8_t*) p, (const std::uint8_t*) p + n); };
  5336. auto i32 = [&] (std::int32_t v) { put (&v, 4); };
  5337. i32 (8 << 16);
  5338. for (int m = 0; m < 13; ++m)
  5339. {
  5340. const double y0 = m == 8 ? 0.1429 : (m == 3 || m == 4 || m == 11) ? 0.0 : 0.5;
  5341. const double y1 = m == 8 ? 0.9918 : y0;
  5342. i32 (1), i32 (0), i32 (2);
  5343. for (const auto& [dx, y] : { std::pair { 0.0, y0 }, std::pair { 1.0, y1 } })
  5344. {
  5345. put (&dx, 8), put (&y, 8);
  5346. i32 (0), i32 (0); // tension (f32 0), flags
  5347. }
  5348. const std::array<std::uint8_t, 20> tail {};
  5349. put (tail.data(), tail.size());
  5350. }
  5351. for (auto v : params)
  5352. i32 (v);
  5353. for (auto v : block)
  5354. i32 (v);
  5355. put (flags.data(), flags.size());
  5356. const std::array<std::uint8_t, 13> axis {};
  5357. put (axis.data(), axis.size());
  5358. d.push_back (3), put ("PWM", 3);
  5359. i32 (1), i32 (60);
  5360. return d;
  5361. }
  5362. };
  5363. std::vector<float> vocodexSaw (int n, double hz, double fs)
  5364. {
  5365. std::vector<float> out ((std::size_t) n);
  5366. for (int i = 0; i < n; ++i)
  5367. out[(std::size_t) i] = (float) (2.0 * std::fmod (i * hz / fs, 1.0) - 1.0) * 0.4f;
  5368. return out;
  5369. }
  5370. TEST (vocodex_state_parse_and_conversion)
  5371. {
  5372. VocodexSpec spec;
  5373. spec.block = { 1, 3, 2, 40, 0, 2, 1 };
  5374. spec.params[17] = -1200;
  5375. const auto raw = spec.bytes();
  5376. const auto s = lynx::vocodex::parseState (raw);
  5377. CHECK (s.has_value());
  5378. if (! s)
  5379. return;
  5380. CHECK (s->version == 8 && s->holdMode == 1 && s->order == 3 && s->unison == 2 && s->bands == 40);
  5381. CHECK (s->draft() && s->modIn == 2 && s->carIn == 1 && s->minTimes && s->subtractiveGate && ! s->lrEncoding);
  5382. CHECK (s->params[lynx::vocodex::modPitch] == -1200 && s->params[lynx::vocodex::carLp] == 65536);
  5383. CHECK (s->maps[8].x.size() == 2 && std::abs (s->maps[8].y[1] - 0.9918) < 1e-12 && s->maps[8].x[1] == 1.0);
  5384. CHECK (s->tone == "PWM" && s->notes.size() == 1 && s->notes[0] == 60);
  5385. const auto v = lynx::vocodexEffect (raw);
  5386. CHECK (v && v->slot.kind == lynx::EffectKind::vocodex && v->slot.state == raw && v->modIn == 2 && v->carIn == 1 && v->notes.empty());
  5387. spec.block[6] = -1; // internal synth carrier: not available yet → reported
  5388. spec.params[1] = 150; // Soundgoodizer stage
  5389. const auto w = lynx::vocodexEffect (spec.bytes());
  5390. CHECK (w && w->carIn == -1 && w->notes.size() == 2);
  5391. CHECK (! lynx::vocodex::parseState ({ 1, 2, 3 }).has_value());
  5392. }
  5393. TEST (vocodex_vocodes_side_inputs)
  5394. {
  5395. // modulator on side input 1 (noise in the first half), carrier on side input 2 (saw), own input silent: the
  5396. // output follows the modulator's envelope and fades after it stops
  5397. VocodexSpec spec;
  5398. lynx::EffectSlot slot = lynx::makeEffectSlot (lynx::EffectKind::vocodex);
  5399. slot.state = spec.bytes();
  5400. auto fx = lynx::makeEffect (slot);
  5401. fx->prepare (48000.0);
  5402. CHECK (fx->latencySamples() == 0);
  5403. const auto car = vocodexSaw (48000, 110.0, 48000.0);
  5404. std::vector<float> mod (48000), l (48000, 0.0f), r (48000, 0.0f);
  5405. std::uint32_t seed = 7;
  5406. for (std::size_t i = 0; i < mod.size(); ++i)
  5407. {
  5408. seed = seed * 1664525u + 1013904223u;
  5409. mod[i] = i < 24000 ? ((float) (seed >> 8) / 16777216.0f - 0.5f) : 0.0f;
  5410. }
  5411. for (int done = 0; done < 48000; done += 512)
  5412. {
  5413. const int n = std::min (512, 48000 - done);
  5414. fx->setSidechain (mod.data() + done, mod.data() + done);
  5415. fx->setSidechain2 (car.data() + done, car.data() + done);
  5416. fx->process (l.data() + done, r.data() + done, n, slot.params, 120.0);
  5417. }
  5418. auto rms = [&] (int from, int to) {
  5419. double e = 0.0;
  5420. for (int i = from; i < to; ++i)
  5421. e += (double) l[(std::size_t) i] * l[(std::size_t) i] + (double) r[(std::size_t) i] * r[(std::size_t) i];
  5422. return std::sqrt (e / (2.0 * (to - from)));
  5423. };
  5424. CHECK (rms (6000, 20000) > 0.02);
  5425. CHECK (rms (40000, 48000) < 0.01 * rms (6000, 20000));
  5426. CHECK (std::abs (l[0]) < 1e-6f); // the gains ramp up from 0 when the plugin is created
  5427. }
  5428. TEST (vocodex_draft_latency_and_pass_through)
  5429. {
  5430. // carrier pass-through at unit gain only (Wet 0): after a Flush the gains snap, so the output is the carrier,
  5431. // 16 samples late in Draft
  5432. VocodexSpec spec;
  5433. spec.params[0] = 0; // Wet
  5434. spec.params[4] = 12800; // CarVol = 1
  5435. for (const int draftField : { 1, 0 })
  5436. {
  5437. spec.block[4] = draftField;
  5438. const auto s = lynx::vocodex::parseState (spec.bytes());
  5439. CHECK (s.has_value());
  5440. lynx::vocodex::Engine engine;
  5441. engine.prepare (*s, 44100.0, 300, s->draft());
  5442. CHECK (engine.latencySamples() == (draftField == 0 ? 16 : 0));
  5443. engine.flush();
  5444. const auto saw = vocodexSaw (3000, 220.0, 44100.0);
  5445. std::vector<float> car (6000), mod (6000, 0.0f), out (6000);
  5446. for (int i = 0; i < 3000; ++i)
  5447. car[(std::size_t) (2 * i)] = car[(std::size_t) (2 * i + 1)] = saw[(std::size_t) i];
  5448. static constexpr int sizes[] { 300, 37, 128, 1, 255, 16, 299 }; // uneven blocks exercise the FIFOs
  5449. for (int done = 0, k = 0, n = 0; done < 3000; done += n, ++k)
  5450. {
  5451. n = std::min (sizes[k % 7], 3000 - done);
  5452. engine.process (mod.data() + 2 * done, mod.data() + 2 * done, car.data() + 2 * done, out.data() + 2 * done, n);
  5453. }
  5454. const int lag = engine.latencySamples();
  5455. auto maxError = 0.0f;
  5456. for (int i = lag; i < 3000; ++i)
  5457. maxError = std::max (maxError, std::abs (out[(std::size_t) (2 * i)] - saw[(std::size_t) (i - lag)]));
  5458. CHECK (maxError == 0.0f);
  5459. }
  5460. }
  5461. TEST (vocodex_soundgoodizer_stage_limits_the_output)
  5462. {
  5463. // SG > 0: the vocoder's output (loud here: Wet 2000) goes through the Soundgoodizer approximation, whose
  5464. // master limiter keeps the peaks under −0.2 dBFS; its look-ahead is reported as latency
  5465. VocodexSpec spec;
  5466. spec.params[0] = 2000;
  5467. spec.params[1] = 250;
  5468. lynx::EffectSlot slot = lynx::makeEffectSlot (lynx::EffectKind::vocodex);
  5469. slot.state = spec.bytes();
  5470. auto fx = lynx::makeEffect (slot);
  5471. fx->prepare (48000.0);
  5472. CHECK (fx->latencySamples() == 72); // 1.5 ms at 48 kHz
  5473. const auto car = vocodexSaw (48000, 110.0, 48000.0);
  5474. std::vector<float> mod (48000), l (48000, 0.0f), r (48000, 0.0f);
  5475. for (std::size_t i = 0; i < mod.size(); ++i)
  5476. mod[i] = 0.9f * (float) std::sin (0.07 * (double) i) * (float) std::sin (0.0003 * (double) i);
  5477. auto peak = 0.0f;
  5478. for (int done = 0; done < 48000; done += 512)
  5479. {
  5480. const int n = std::min (512, 48000 - done);
  5481. fx->setSidechain (mod.data() + done, mod.data() + done);
  5482. fx->setSidechain2 (car.data() + done, car.data() + done);
  5483. fx->process (l.data() + done, r.data() + done, n, slot.params, 120.0);
  5484. }
  5485. for (std::size_t i = 0; i < l.size(); ++i)
  5486. peak = std::max ({ peak, std::abs (l[i]), std::abs (r[i]) });
  5487. CHECK (peak > 0.5f && peak <= 0.9775f);
  5488. }
  5489. TEST (vocodex_tail_bypass_and_noise)
  5490. {
  5491. // both inputs silent for longer than 0.5 s + hold + release: the own input passes unchanged
  5492. VocodexSpec spec;
  5493. const auto s = lynx::vocodex::parseState (spec.bytes());
  5494. CHECK (s.has_value());
  5495. lynx::vocodex::Engine engine;
  5496. engine.prepare (*s, 44100.0, 512, false);
  5497. std::vector<float> zero (1024, 0.0f), own (1024), out (1024);
  5498. for (std::size_t i = 0; i < own.size(); ++i)
  5499. own[i] = 0.25f * (float) std::sin (0.01 * (double) i);
  5500. for (int b = 0; b < 80; ++b)
  5501. engine.process (own.data(), zero.data(), zero.data(), out.data(), 512);
  5502. CHECK (out == own);
  5503. // carrier noise: the IPP-compatible generator restarts from the same seed after a Flush
  5504. spec.params[6] = 9000;
  5505. const auto noisy = lynx::vocodex::parseState (spec.bytes());
  5506. lynx::vocodex::Engine a;
  5507. a.prepare (*noisy, 44100.0, 512, false);
  5508. std::vector<float> mod (1024), first (1024), second (1024);
  5509. for (std::size_t i = 0; i < mod.size(); ++i)
  5510. mod[i] = 0.3f * (float) std::sin (0.05 * (double) i);
  5511. a.flush();
  5512. a.process (own.data(), mod.data(), zero.data(), first.data(), 512);
  5513. a.flush();
  5514. a.process (own.data(), mod.data(), zero.data(), second.data(), 512);
  5515. CHECK (first == second);
  5516. auto energy = 0.0;
  5517. for (auto v : first)
  5518. energy += (double) v * v;
  5519. CHECK (energy > 1e-6 && std::isfinite (energy));
  5520. }
  5521. TEST (synth_spectral_oscillator)
  5522. {
  5523. // 1/n spectrum = saw: same harmonic proportions and same level as LYNX's saw
  5524. lynx::SynthSettings saw;
  5525. saw.osc[0] = { lynx::Waveform::saw, 0, 0.0f, 1.0f };
  5526. saw.cutoff = 20000.0f;
  5527. auto spectral = saw;
  5528. spectral.osc[0].wave = lynx::Waveform::spectral;
  5529. for (int n = 1; n <= lynx::SynthSettings::spectrumSize; ++n)
  5530. spectral.spectrum[(std::size_t) n - 1] = 1.0f / (float) n;
  5531. const auto a = renderSynthNote (saw, 57, 24000), b = renderSynthNote (spectral, 57, 24000); // 220 Hz
  5532. for (const auto h : { 1, 2, 3, 5 })
  5533. {
  5534. const auto ea = goertzel (a, 220.0 * h, 4800, 24000), eb = goertzel (b, 220.0 * h, 4800, 24000);
  5535. CHECK (std::abs (10.0 * std::log10 (eb / ea)) < 0.5); // within 0.5 dB
  5536. }
  5537. // odd only: no 2nd harmonic
  5538. auto odd = spectral;
  5539. for (int n = 2; n <= lynx::SynthSettings::spectrumSize; n += 2)
  5540. odd.spectrum[(std::size_t) n - 1] = 0.0f;
  5541. const auto c = renderSynthNote (odd, 57, 24000);
  5542. CHECK (goertzel (c, 440.0, 4800, 24000) < 1e-4 * goertzel (c, 220.0, 4800, 24000));
  5543. // high note: the band-limited tables don't pass Nyquist (no aliasing below the fundamental)
  5544. const auto d = renderSynthNote (spectral, 105, 24000); // 3520 Hz
  5545. CHECK (goertzel (d, 1000.0, 4800, 24000) < 1e-3 * goertzel (d, 3520.0, 4800, 24000));
  5546. }
  5547. TEST (compressor_sidechain_ducks_and_orders_mixer)
  5548. {
  5549. // the detector listens to the side input: quiet signal on the track, loud kick on the sidechain -> reduction
  5550. auto slot = lynx::makeEffectSlot (lynx::EffectKind::compressor);
  5551. slot.params[0] = 0.5f; // threshold
  5552. slot.params[1] = 1.0f; // high ratio
  5553. slot.params[4] = 0.0f;
  5554. auto run = [&] (bool withSidechain) {
  5555. auto fx = lynx::makeEffect (lynx::EffectKind::compressor);
  5556. fx->prepare (48000.0);
  5557. std::vector<float> l (4800, 0.1f), r (4800, 0.1f), kick (4800, 1.0f);
  5558. if (withSidechain)
  5559. fx->setSidechain (kick.data(), kick.data());
  5560. fx->process (l.data(), r.data(), 4800, slot.params, 120.0);
  5561. return l.back();
  5562. };
  5563. const auto alone = run (false), ducked = run (true);
  5564. CHECK (ducked < 0.5f * alone);
  5565. // order: track 5 listens to 7 via sidechain -> 7 comes before 5
  5566. auto song = lynx::Song::makeDefault();
  5567. while (song.mixer.size() < 8)
  5568. song.mixer.push_back ({});
  5569. auto comp = lynx::makeEffectSlot (lynx::EffectKind::compressor);
  5570. comp.sidechain = 7;
  5571. song.mixer[5].effects.push_back (comp);
  5572. const auto order = song.mixerOrder();
  5573. const auto at = [&] (int t) { return std::find (order.begin(), order.end(), t) - order.begin(); };
  5574. CHECK (at (7) < at (5));
  5575. }
  5576. TEST (pitch_correction_pulls_to_allowed_note)
  5577. {
  5578. auto run = [] (const lynx::EffectParams& p, double hz) {
  5579. auto fx = lynx::makeEffect (lynx::EffectKind::pitchCorrect);
  5580. fx->prepare (48000.0);
  5581. std::vector<float> l (48000 * 2), r;
  5582. for (std::size_t i = 0; i < l.size(); ++i)
  5583. l[i] = 0.5f * (float) std::sin (2.0 * 3.141592653589793 * hz * (double) i / 48000.0);
  5584. r = l;
  5585. fx->process (l.data(), r.data(), (int) l.size(), p, 120.0);
  5586. // frequency from zero crossings in the last second
  5587. int crossings = 0;
  5588. for (std::size_t i = 48000 + 1; i < l.size(); ++i)
  5589. crossings += (l[i - 1] < 0.0f) != (l[i] < 0.0f);
  5590. return crossings / 2.0;
  5591. };
  5592. auto slot = lynx::makeEffectSlot (lynx::EffectKind::pitchCorrect); // chromatic, instant, A 440
  5593. CHECK (std::abs (run (slot.params, 452.0) - 440.0) < 4.0);
  5594. for (int k = 0; k < 12; ++k)
  5595. slot.params[(std::size_t) (5 + k)] = k == 0 ? 1.0f : 0.0f; // only C
  5596. CHECK (std::abs (run (slot.params, 452.0) - 523.25) < 5.0); // C5 (523 Hz) is closer than C4 (262)
  5597. slot.params[2] = 1.0f; // turn everything off: passes unchanged
  5598. CHECK (std::abs (run (slot.params, 452.0) - 452.0) < 4.0);
  5599. }
  5600. TEST (pitch_correction_bypass_is_a_pure_delay)
  5601. {
  5602. // FL's Pitcher with Bypass All still runs its shifter: the plain path is an exact 9-sample delay, the formant
  5603. // path (256-Hann overlap-add) an exact 257-sample delay, and Gender changes nothing without a shift
  5604. // (REA_rodada30 §2.4/2.5); both are reported as latency
  5605. std::vector<float> in (48000);
  5606. for (std::size_t i = 0; i < in.size(); ++i)
  5607. in[i] = 0.5f * (float) std::sin (2.0 * 3.141592653589793 * 440.0 * (double) i / 48000.0)
  5608. + 0.2f * (float) std::sin (2.0 * 3.141592653589793 * 1234.5 * (double) i / 48000.0);
  5609. auto run = [&] (bool formant, float gender, int& latency) {
  5610. auto fx = lynx::makeEffect (lynx::EffectKind::pitchCorrect);
  5611. fx->prepare (48000.0);
  5612. auto slot = lynx::makeEffectSlot (lynx::EffectKind::pitchCorrect);
  5613. slot.params[2] = 1.0f;
  5614. slot.params[29] = formant ? 1.0f : 0.0f;
  5615. slot.params[30] = gender;
  5616. auto l = in, r = in;
  5617. for (std::size_t i = 0; i < l.size(); i += 480)
  5618. fx->process (l.data() + i, r.data() + i, 480, slot.params, 120.0);
  5619. latency = fx->latencySamples();
  5620. return l;
  5621. };
  5622. int latency = 0;
  5623. auto plain = run (false, 0.5f, latency);
  5624. CHECK (latency == 9);
  5625. auto worst = 0.0f;
  5626. for (std::size_t i = 4000; i < in.size(); ++i)
  5627. worst = std::max (worst, std::abs (plain[i] - in[i - 9]));
  5628. CHECK (worst < 1e-5f);
  5629. auto formant = run (true, 0.8f, latency);
  5630. CHECK (latency == 257);
  5631. worst = 0.0f;
  5632. for (std::size_t i = 4000; i < in.size(); ++i)
  5633. worst = std::max (worst, std::abs (formant[i] - in[i - 257]));
  5634. CHECK (worst < 1e-5f);
  5635. }
  5636. TEST (pitch_correction_formant_path)
  5637. {
  5638. // Formant on: PSOLA keeps the correction (output pitch = grain spacing) while Gender resamples the grains:
  5639. // the spectrum of a saw moves up with Gender > 0.5 and down with Gender < 0.5
  5640. auto run = [] (float gender) {
  5641. auto fx = lynx::makeEffect (lynx::EffectKind::pitchCorrect);
  5642. fx->prepare (48000.0);
  5643. auto slot = lynx::makeEffectSlot (lynx::EffectKind::pitchCorrect); // chromatic, instant, A 440
  5644. slot.params[29] = 1.0f;
  5645. slot.params[30] = gender;
  5646. std::vector<float> l (48000 * 2), r;
  5647. for (std::size_t i = 0; i < l.size(); ++i)
  5648. l[i] = 0.5f * (float) (2.0 * std::fmod (452.0 * (double) i / 48000.0, 1.0) - 1.0); // saw
  5649. r = l;
  5650. fx->process (l.data(), r.data(), (int) l.size(), slot.params, 120.0);
  5651. l.erase (l.begin(), l.begin() + 48000);
  5652. return l;
  5653. };
  5654. // period by autocorrelation (lags 60…160 = 300…800 Hz)
  5655. auto pitch = [] (const std::vector<float>& x) {
  5656. auto best = 0.0, bestLag = 0.0;
  5657. for (int lag = 60; lag <= 160; ++lag)
  5658. {
  5659. auto sum = 0.0;
  5660. for (std::size_t i = 0; i + (std::size_t) lag < x.size(); ++i)
  5661. sum += (double) x[i] * x[i + (std::size_t) lag];
  5662. if (sum > best)
  5663. {
  5664. best = sum;
  5665. bestLag = lag;
  5666. }
  5667. }
  5668. return 48000.0 / bestLag;
  5669. };
  5670. // brightness: energy of the first difference relative to the energy
  5671. auto brightness = [] (const std::vector<float>& x) {
  5672. auto e = 0.0, d = 0.0;
  5673. for (std::size_t i = 1; i < x.size(); ++i)
  5674. {
  5675. e += (double) x[i] * x[i];
  5676. d += (double) (x[i] - x[i - 1]) * (x[i] - x[i - 1]);
  5677. }
  5678. return d / e;
  5679. };
  5680. const auto neutral = run (0.5f), up = run (0.8f), down = run (0.3f);
  5681. CHECK (std::abs (pitch (neutral) - 440.0) < 5.0); // 452 Hz pulled to A
  5682. CHECK (std::abs (pitch (up) - 440.0) < 5.0); // Gender does not move the pitch
  5683. CHECK (brightness (up) > 1.5 * brightness (neutral));
  5684. CHECK (brightness (down) < 0.7 * brightness (neutral));
  5685. }
  5686. TEST (parallel_container_inner_links_modulate)
  5687. {
  5688. // one branch: utility (gain) + a control-only auto-pan LFO (1 beat); the link maps the LFO 0..1 onto the
  5689. // utility gain −24 dB..0 dB, so the level must swing; without the link it stays put
  5690. auto container = lynx::makeEffectSlot (lynx::EffectKind::parallel);
  5691. auto gain = lynx::makeEffectSlot (lynx::EffectKind::utility);
  5692. auto lfo = lynx::makeEffectSlot (lynx::EffectKind::autoPan);
  5693. lfo.params[0] = 1.0f / 64.0f;
  5694. lfo.params[4] = 1.0f; // control only: audio passes unchanged
  5695. container.branches.push_back ({ { gain, lfo }, 1.0f });
  5696. lynx::EffectLink link;
  5697. link.source = 1;
  5698. link.param = 0;
  5699. for (int i = 0; i <= 32; ++i)
  5700. link.curve.push_back (0.5f * (float) i / 32.0f);
  5701. auto levels = [&] (bool linked) {
  5702. auto slot = container;
  5703. if (linked)
  5704. slot.links.push_back (link);
  5705. auto fx = lynx::makeParallelEffect (slot);
  5706. fx->prepare (48000.0);
  5707. float lo = 1e9f, hi = 0.0f;
  5708. for (int block = 0; block < 94; ++block) // ~1 beat at 120 BPM in 256-sample blocks
  5709. {
  5710. std::vector<float> l (256, 1.0f), r (256, 1.0f);
  5711. fx->setSongPosition (block * 256.0 / 48000.0 * 2.0 * 960.0); // the engine positions every block
  5712. fx->process (l.data(), r.data(), 256, slot.params, 120.0);
  5713. lo = std::min (lo, std::abs (l[128]));
  5714. hi = std::max (hi, std::abs (l[128]));
  5715. }
  5716. return std::pair { lo, hi };
  5717. };
  5718. const auto [lo, hi] = levels (true);
  5719. CHECK (hi > 0.8f && lo < 0.12f);
  5720. const auto [lo0, hi0] = levels (false);
  5721. CHECK (std::abs (hi0 - lo0) < 1e-4f);
  5722. }
  5723. TEST (synth_spectrum_decays_per_harmonic)
  5724. {
  5725. // harmonic 1 holds, harmonic 3 falls 60 dB in 0.2 s: early the 3rd is as loud as the 1st, later it is gone
  5726. lynx::SynthSettings s;
  5727. s.osc[0] = { lynx::Waveform::spectral, 0, 0.0f, 1.0f };
  5728. s.cutoff = 20000.0f;
  5729. s.decay = 0.0f;
  5730. s.sustain = 1.0f;
  5731. s.spectrum[0] = 1.0f;
  5732. s.spectrum[2] = 1.0f;
  5733. s.spectrumDecay[2] = 0.2f;
  5734. const auto x = renderSynthNote (s, 57, 48000); // 220 Hz, 48 kHz, 1 s
  5735. // equal 2400-sample windows (Goertzel power grows with the window): 10…60 ms and 500…550 ms
  5736. const auto early1 = goertzel (x, 220.0, 480, 2880), early3 = goertzel (x, 660.0, 480, 2880);
  5737. const auto late1 = goertzel (x, 220.0, 24000, 26400), late3 = goertzel (x, 660.0, 24000, 26400);
  5738. CHECK (early3 > 0.05 * early1); // ≈ −9 dB on average over 10…60 ms
  5739. CHECK (late3 < 1e-4 * late1);
  5740. CHECK (std::abs (10.0 * std::log10 (late1 / early1)) < 1.0); // the 1st holds
  5741. // without per-harmonic decay the spectrum stays as it is
  5742. s.spectrumDecay[2] = 0.0f;
  5743. const auto y = renderSynthNote (s, 57, 48000);
  5744. CHECK (std::abs (10.0 * std::log10 (goertzel (y, 660.0, 24000, 26400) / goertzel (y, 660.0, 480, 2880))) < 1.0);
  5745. }
  5746. TEST (worker_pool_runs_each_index_once_per_round)
  5747. {
  5748. // rounds of alternating sizes (a stalled worker must never run an index of a later round), each index
  5749. // exactly once, and nothing running after parallelFor returns
  5750. lynx::WorkerPool pool (3);
  5751. std::vector<std::atomic<int>> hits (64);
  5752. std::atomic<int> round { 0 }, outside { 0 }, wrongRound { 0 };
  5753. std::atomic<bool> open { false };
  5754. int failures = 0;
  5755. for (int r = 1; r <= 20000; ++r)
  5756. {
  5757. const auto n = (r % 3 == 0) ? 64 : (r % 3 == 1 ? 2 : 9);
  5758. for (int i = 0; i < n; ++i)
  5759. hits[(std::size_t) i].store (0);
  5760. round.store (r);
  5761. open.store (true);
  5762. auto fn = [&, r] (int i) {
  5763. if (! open.load())
  5764. outside.fetch_add (1);
  5765. if (round.load() != r)
  5766. wrongRound.fetch_add (1);
  5767. hits[(std::size_t) i].fetch_add (1);
  5768. };
  5769. pool.parallelFor (n, fn);
  5770. open.store (false);
  5771. for (int i = 0; i < n; ++i)
  5772. failures += hits[(std::size_t) i].load() != 1;
  5773. }
  5774. CHECK (failures == 0);
  5775. CHECK (outside.load() == 0);
  5776. CHECK (wrongRound.load() == 0);
  5777. }
  5778. TEST (ladder_filter_follows_toxic_law)
  5779. {
  5780. // the 4-pole ladder: −24 dB/oct far above the cutoff (vs −12 for the SVF), resonance raises the corner, and
  5781. // the input saturation keeps it bounded even at full resonance
  5782. auto level = [] (bool ladder, float res, double hz) {
  5783. lynx::SynthSettings s;
  5784. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  5785. s.cutoff = 500.0f;
  5786. s.resonance = res;
  5787. s.ladder = ladder;
  5788. const auto key = (int) std::lround (69.0 + 12.0 * std::log2 (hz / 440.0));
  5789. const auto x = renderSynthNote (s, key, 48000);
  5790. double e = 0.0, peak = 0.0;
  5791. for (std::size_t i = 24000; i < 48000; ++i)
  5792. e += (double) x[i] * x[i], peak = std::max (peak, (double) std::abs (x[i]));
  5793. return std::pair { std::sqrt (e / 24000.0), peak };
  5794. };
  5795. const auto low = level (true, 0.0f, 110.0).first, high = level (true, 0.0f, 3520.0).first; // 3 octaves above 500 Hz… 2.8
  5796. const auto svfHigh = level (false, 0.0f, 3520.0).first / level (false, 0.0f, 110.0).first;
  5797. CHECK (20.0 * std::log10 (high / low) < -55.0); // ≈ 24 dB/oct over ~2.8 octaves
  5798. CHECK (20.0 * std::log10 (svfHigh) > -45.0); // the 2-pole SVF falls much less
  5799. const auto corner0 = level (true, 0.0f, 523.25).first, corner1 = level (true, 0.6f, 523.25).first;
  5800. CHECK (corner1 > 1.5 * corner0); // resonance boosts near the cutoff
  5801. const auto loud = level (true, 1.0f, 523.25);
  5802. CHECK (std::isfinite (loud.first) && loud.second < 20.0); // self-oscillation stays bounded
  5803. }
  5804. TEST (synth_spectrum_settles_on_target)
  5805. {
  5806. // ALT DECAY-like morph: harmonic 3 starts at 1 and settles on 0.25 (−12 dB) instead of fading out
  5807. lynx::SynthSettings s;
  5808. s.osc[0] = { lynx::Waveform::spectral, 0, 0.0f, 1.0f };
  5809. s.cutoff = 20000.0f;
  5810. s.decay = 0.0f;
  5811. s.sustain = 1.0f;
  5812. s.spectrum[0] = 1.0f;
  5813. s.spectrum[2] = 1.0f;
  5814. s.spectrumDecay[2] = 0.1f;
  5815. s.spectrumTarget[2] = 0.25f;
  5816. const auto x = renderSynthNote (s, 57, 48000);
  5817. const auto ratio = goertzel (x, 660.0, 24000, 26400) / goertzel (x, 220.0, 24000, 26400);
  5818. CHECK (std::abs (10.0 * std::log10 (ratio) - 20.0 * std::log10 (0.25)) < 1.0);
  5819. }
  5820. TEST (kepler_chorus_is_stereo_and_keeps_level)
  5821. {
  5822. // chorus I on a sine: L and R differ (opposite LFO phases), the level stays close to the dry one
  5823. lynx::SynthSettings s;
  5824. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  5825. s.cutoff = 20000.0f;
  5826. auto dry = s;
  5827. s.chorus = 1;
  5828. auto render = [] (const lynx::SynthSettings& set) {
  5829. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  5830. inst->prepare (48000.0);
  5831. lynx::ChannelDef c;
  5832. c.synth = set;
  5833. inst->configure (c, 120.0);
  5834. inst->noteOn (69, 1.0f);
  5835. std::vector<float> l (96000), r (96000);
  5836. inst->render (l.data(), r.data(), 96000, 1.0f, 1.0f);
  5837. return std::pair { l, r };
  5838. };
  5839. const auto [l, r] = render (s);
  5840. const auto [dl, dr] = render (dry);
  5841. double diff = 0.0, e = 0.0, ed = 0.0;
  5842. for (std::size_t i = 48000; i < 96000; ++i)
  5843. diff += std::abs (l[i] - r[i]), e += (double) l[i] * l[i], ed += (double) dl[i] * dl[i];
  5844. CHECK (diff / 48000.0 > 1e-3);
  5845. CHECK (std::abs (10.0 * std::log10 (e / ed)) < 3.0);
  5846. }
  5847. TEST (fl_pan_law_matches_love_philter_dll)
  5848. {
  5849. // values printed by the installation's Love Philter pan routine for vol 0.8 (REA_rodada27 §3.1, lppan.txt)
  5850. struct Row { double pan, ll, rr, rl, lr; };
  5851. const Row rows[] = { { -1.5, 0.239782482, -0.432956904, 0.805467963, 0.0 }, { -0.5, 0.805467963, 0.432956904, 0.239782482, 0.0 },
  5852. { 0.0, 0.8, 0.8, 0.0, 0.0 }, { 0.75, 0.220719516, 0.707889318, 0.0, 0.401742548 },
  5853. { 1.0, 0.0, 0.565685451, 0.0, 0.565685451 } };
  5854. for (const auto& row : rows)
  5855. {
  5856. float ll, rl, lr, rr;
  5857. lynx::flPanMatrix (row.pan, ll, rl, lr, rr);
  5858. CHECK (std::abs (0.8 * ll - row.ll) < 1e-6 && std::abs (0.8 * rr - row.rr) < 1e-6);
  5859. CHECK (std::abs (0.8 * rl - row.rl) < 1e-6 && std::abs (0.8 * lr - row.lr) < 1e-6);
  5860. }
  5861. }
  5862. TEST (effector_units_match_fl_dll_impulse_responses)
  5863. {
  5864. // impulse responses of the installation's Effector engine (REA_rodada27 §2, eu_<kind>_<X>_<Y>.txt; LFO off,
  5865. // wet 1, blocks of 256): samples 1, 10, 100 and 1000
  5866. struct Case { int kind; float x, y; double v[4]; };
  5867. const Case cases[] = { { 3, 0.5f, 0.5f, { -0.603055418, 0.159798339, -0.0055043716, -1.06241259e-06 } },
  5868. { 4, 0.2f, 0.5f, { 4.68885949e-26, 4.20032622e-19, 0.000487731799, 2.27715846e-09 } },
  5869. { 4, 0.8f, 0.3f, { -0.293783396, -0.0306414738, -0.00068128563, 1.358577e-15 } } };
  5870. for (const auto& c : cases)
  5871. {
  5872. auto fx = lynx::effector::make();
  5873. fx->prepare (44100.0);
  5874. lynx::EffectParams p {};
  5875. p[lynx::effector::type] = c.kind / 11.0f;
  5876. p[lynx::effector::wetMix] = 1.0f;
  5877. p[lynx::effector::paramX] = c.x;
  5878. p[lynx::effector::paramY] = c.y;
  5879. p[lynx::effector::modX] = p[lynx::effector::modY] = 0.5f;
  5880. p[lynx::effector::modRate] = 0.5f;
  5881. p[lynx::effector::inputLevel] = 1.0f;
  5882. p[lynx::effector::outputGain] = 0.5f;
  5883. std::vector<float> l (1024, 0.0f), r (1024, 0.0f);
  5884. l[0] = r[0] = 1.0f;
  5885. for (int d = 0; d < 1024; d += 256)
  5886. fx->process (l.data() + d, r.data() + d, 256, p, 120.0);
  5887. const int at[4] { 1, 10, 100, 1000 };
  5888. for (int k = 0; k < 4; ++k)
  5889. CHECK (std::abs (l[(std::size_t) at[k]] - c.v[k]) < 2e-7 + 1e-5 * std::abs (c.v[k]));
  5890. }
  5891. }
  5892. TEST (harmless_stereo_tremolo_and_vibrato)
  5893. {
  5894. // tremolo d = 1: each side swings between 0 and 1.25 at 5 Hz, L and R in anti-phase; vibrato modulates the pitch
  5895. lynx::SynthSettings s;
  5896. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  5897. s.cutoff = 20000.0f;
  5898. s.stereoTremolo = 1.0f;
  5899. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  5900. inst->prepare (48000.0);
  5901. lynx::ChannelDef c;
  5902. c.synth = s;
  5903. inst->configure (c, 120.0);
  5904. inst->noteOn (69, 1.0f);
  5905. std::vector<float> l (48000), r (48000);
  5906. inst->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
  5907. auto envelope = [] (const std::vector<float>& x, int at) { float m = 0.0f; for (int i = at; i < at + 120; ++i) m = std::max (m, std::abs (x[(std::size_t) i])); return m; };
  5908. // θ = 2π·2.5·t: at t = 0.4 s cos = 1 (L peak 1.25×), sin = 0 (R near 0); at t = 0.5 s the opposite
  5909. CHECK (envelope (l, 19200) > 4.0f * envelope (r, 19200));
  5910. CHECK (envelope (r, 24000) > 4.0f * envelope (l, 24000));
  5911. // vibrato 100 cents: zero crossings vary in spacing; without it they are regular
  5912. auto crossingsSpread = [] (float cents) {
  5913. lynx::SynthSettings v;
  5914. v.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  5915. v.cutoff = 20000.0f;
  5916. v.vibratoCents = cents;
  5917. const auto x = renderSynthNote (v, 69, 48000);
  5918. std::vector<int> zc;
  5919. for (int i = 24000; i < 48000; ++i)
  5920. if (x[(std::size_t) i - 1] < 0.0f && x[(std::size_t) i] >= 0.0f)
  5921. zc.push_back (i);
  5922. int lo = 1 << 30, hi = 0;
  5923. for (std::size_t k = 1; k < zc.size(); ++k)
  5924. lo = std::min (lo, zc[k] - zc[k - 1]), hi = std::max (hi, zc[k] - zc[k - 1]);
  5925. return hi - lo;
  5926. };
  5927. CHECK (crossingsSpread (0.0f) <= 1);
  5928. CHECK (crossingsSpread (100.0f) >= 4);
  5929. }
  5930. TEST (physical_models_sound_and_end)
  5931. {
  5932. // Sakura: a plucked string with default-ish parameters rings and fades after the release; Drumpad: a hit decays
  5933. for (const auto kind : { lynx::InstrumentKind::sakura, lynx::InstrumentKind::drumpad })
  5934. {
  5935. lynx::ChannelDef c;
  5936. c.instrument = kind;
  5937. if (kind == lynx::InstrumentKind::sakura)
  5938. {
  5939. c.modelParams.assign (146, 0.5f);
  5940. c.modelParams[111] = 3.0f; // both strings at the note
  5941. c.modelParams[113] = 0.5f; // octave ×1
  5942. c.modelParams[6] = c.modelParams[11] = 0.99f; // long decay
  5943. c.modelParams[31] = 0.0f; // noise exciter
  5944. }
  5945. else
  5946. {
  5947. c.modelParams.assign (38, 0.5f);
  5948. c.modelParams[0] = 1.0f; // retrigger noise
  5949. c.modelParams[30] = c.modelParams[31] = c.modelParams[32] = c.modelParams[33] = 0.0f; // no velocity morph
  5950. }
  5951. auto inst = lynx::makeInstrument (kind);
  5952. inst->prepare (48000.0);
  5953. inst->configure (c, 120.0);
  5954. inst->noteOn (60, 0.8f);
  5955. std::vector<float> l (48000), r (48000);
  5956. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  5957. inst->noteOff (60);
  5958. inst->render (l.data() + 24000, r.data() + 24000, 24000, 1.0f, 1.0f);
  5959. double early = 0.0;
  5960. for (int i = 0; i < 12000; ++i)
  5961. early += (double) l[(std::size_t) i] * l[(std::size_t) i];
  5962. CHECK (early > 1e-6);
  5963. CHECK (std::all_of (l.begin(), l.end(), [] (float v) { return std::isfinite (v); }));
  5964. std::vector<float> tail (480000), tailR (480000);
  5965. inst->render (tail.data(), tailR.data(), 480000, 1.0f, 1.0f); // 10 s later the voice has ended
  5966. CHECK (inst->idle());
  5967. }
  5968. }
  5969. TEST (squeeze_matches_fl_model)
  5970. {
  5971. // Fruity Squeeze: Steps quantises, Mix 0 passes the input dry ×Volume/127; a silent block is left untouched
  5972. auto slot = lynx::makeEffectSlot (lynx::EffectKind::squeeze);
  5973. slot.params[1] = 0.0f; // mix 0: dry only
  5974. auto fx = lynx::makeEffect (lynx::EffectKind::squeeze);
  5975. fx->prepare (44100.0);
  5976. std::vector<float> l (512, 0.3f), r (512, -0.2f);
  5977. fx->process (l.data(), r.data(), 512, slot.params, 120.0);
  5978. CHECK (std::abs (l[100] - 0.3f) < 1e-6f && std::abs (r[100] + 0.2f) < 1e-6f); // Volume 127/127
  5979. slot.params[1] = 1.0f; // wet only
  5980. slot.params[2] = 32000.0f / 32768.0f; // 32768^(1 − 32000/32768) ≈ 1.27 levels per unit → heavy quantisation
  5981. std::vector<float> a (512), b (512);
  5982. for (int i = 0; i < 512; ++i)
  5983. a[(std::size_t) i] = b[(std::size_t) i] = 0.4f * (float) std::sin (i * 0.05);
  5984. fx->process (a.data(), b.data(), 512, slot.params, 120.0);
  5985. const auto levels = std::pow (32768.0f, 1.0f - 32000.0f / 32768.0f);
  5986. for (int i = 0; i < 512; i += 37)
  5987. CHECK (std::abs (a[(std::size_t) i] * levels - std::nearbyint (a[(std::size_t) i] * levels)) < 1e-3f);
  5988. std::vector<float> z (512, 0.0f), z2 (512, 0.0f);
  5989. fx->process (z.data(), z2.data(), 512, slot.params, 120.0);
  5990. CHECK (z[10] == 0.0f);
  5991. }
  5992. TEST (sakura_matrix_and_effects)
  5993. {
  5994. // REA_rodada29 §1: velocity → level follows 10^(2m)·0.05 (vel 1 vs 0.5 → ×10 in amplitude); the delay leaves
  5995. // an echo after the note has ended and keeps the instrument from going idle until its tail is silent
  5996. auto base = [] {
  5997. lynx::ChannelDef c;
  5998. c.instrument = lynx::InstrumentKind::sakura;
  5999. c.modelParams.assign (146, 0.0f);
  6000. const std::pair<int, float> values[] { { 0, 0.375f }, { 1, 0.5f }, { 3, 0.5f }, { 4, 1.0f }, { 6, 0.95f }, { 9, 0.3f },
  6001. { 11, 0.95f }, { 14, 0.3f }, { 18, 0.5f }, { 19, 0.5f }, { 27, 0.5f }, { 28, 1.0f },
  6002. { 29, 0.7f }, { 31, 1.0f }, { 32, 0.5f }, { 111, 3.0f }, { 113, 0.5f } };
  6003. for (const auto& [i, v] : values)
  6004. c.modelParams[(std::size_t) i] = v;
  6005. return c;
  6006. };
  6007. auto render = [] (const lynx::ChannelDef& c, float velocity, int held, int total) {
  6008. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sakura);
  6009. inst->prepare (48000.0);
  6010. inst->configure (c, 120.0);
  6011. inst->noteOn (60, velocity);
  6012. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  6013. for (int s = 0; s < total; s += 256)
  6014. {
  6015. if (s == held)
  6016. inst->noteOff (60);
  6017. inst->render (l.data() + s, r.data() + s, std::min (256, total - s), 1.0f, 1.0f);
  6018. }
  6019. return std::make_pair (l, std::move (inst));
  6020. };
  6021. auto energy = [] (const std::vector<float>& x, int from, int to) {
  6022. double e = 0.0;
  6023. for (int i = from; i < to; ++i)
  6024. e += (double) x[(std::size_t) i] * x[(std::size_t) i];
  6025. return e;
  6026. };
  6027. auto c = base();
  6028. c.modelParams[79] = 6.0f; // slot 1: velocity → level, amount 1
  6029. c.modelParams[80] = 1.0f;
  6030. c.modelParams[82] = 1.0f;
  6031. const auto loud = energy (render (c, 1.0f, 24064, 24064).first, 0, 24064);
  6032. const auto soft = energy (render (c, 0.5f, 24064, 24064).first, 0, 24064);
  6033. CHECK (loud > 1e-6 && std::abs (loud / soft - 100.0) < 1.0);
  6034. auto d = base();
  6035. auto [dry, dryInst] = render (d, 0.8f, 4864, 72000);
  6036. d.modelParams[63] = 1.0f; // delay on: time 0.5 → ≈ 0.52 s, wet 1, no feedback: one echo
  6037. d.modelParams[64] = 0.5f;
  6038. d.modelParams[69] = 1.0f;
  6039. auto [wet, wetInst] = render (d, 0.8f, 4864, 72000);
  6040. CHECK (energy (wet, 26000, 40000) > 100.0 * energy (dry, 26000, 40000) + 1e-9); // the note ends near 0.2 s
  6041. CHECK (dryInst->idle() || energy (dry, 70000, 72000) < 1e-12);
  6042. CHECK (! wetInst->idle()); // the echo tail has not been silent for 3 s yet
  6043. }
  6044. TEST (drumsynth_live_engine)
  6045. {
  6046. // REA_rodada30 §1: key → patch slot, state parsing, the engine against our reference model (no DLL: synthesised
  6047. // sources only, noise from the fallback seed), voice modes, patch level/pan
  6048. CHECK (lynx::drumSynthKeyToSlot (48) == 0 && lynx::drumSynthKeyToSlot (71) == 23 && lynx::drumSynthKeyToSlot (0) == 24);
  6049. CHECK (lynx::drumSynthKeyToSlot (47) == 71 && lynx::drumSynthKeyToSlot (72) == 72 && lynx::drumSynthKeyToSlot (119) == 119);
  6050. CHECK (lynx::drumSynthKeyToSlot (120) == -1);
  6051. // a version 3 state: 120 names, 120 × 38 ints (send track −1 becomes 0 below version 4), flags
  6052. std::vector<std::uint8_t> blob;
  6053. auto put = [&] (std::uint32_t v) { for (int k = 0; k < 4; ++k) blob.push_back ((std::uint8_t) (v >> (8 * k))); };
  6054. put (3);
  6055. put (66);
  6056. for (int i = 0; i < 120; ++i)
  6057. blob.push_back (2), blob.push_back ('a'), blob.push_back ((std::uint8_t) ('0' + i % 10));
  6058. for (int i = 0; i < 120 * 38; ++i)
  6059. put ((std::uint32_t) (i % 38 == 36 ? -1 : i % 38 == 4 ? 300 : 0));
  6060. put (0);
  6061. const auto parsed = lynx::drumSynthLiveParams (blob);
  6062. CHECK (parsed && (int) parsed->size() == lynx::drumSynthParamCount);
  6063. CHECK (parsed && (*parsed)[36] == 0.0f && (*parsed)[38 * 7 + 4] == 300.0f && (*parsed)[(std::size_t) lynx::drumSynthEditorKeyIndex] == 66.0f);
  6064. blob.resize (500);
  6065. CHECK (! lynx::drumSynthLiveParams (blob));
  6066. // patch on key 60 (slot 12): tone-mode Osc1 with sweep and click, noise-band Osc2, ring, 2 retriggers
  6067. const int patch[38] { 100, 0, 10, -20, 300, 40, 50, 70, 90, 30, 600, 60, 10, 60, 80, -1, 0, 0, 2, 40, 60, 127, 0, 20, 0, 0, 0,
  6068. 0, 0, 0, 0, 0, 0, 30, -1, 0, 0, 64 };
  6069. lynx::ChannelDef c;
  6070. c.instrument = lynx::InstrumentKind::drumsynth;
  6071. c.modelParams.assign ((std::size_t) lynx::drumSynthParamCount, 0.0f);
  6072. for (int s = 0; s < 120; ++s)
  6073. c.modelParams[(std::size_t) (38 * s + 15)] = c.modelParams[(std::size_t) (38 * s + 34)] = -1.0f;
  6074. for (int i = 0; i < 38; ++i)
  6075. c.modelParams[(std::size_t) (38 * 12 + i)] = (float) patch[i];
  6076. auto render = [] (const lynx::ChannelDef& def, std::vector<int> keys, int total, float gl = 1.0f, float gr = 1.0f) {
  6077. auto inst = lynx::makeInstrument (lynx::InstrumentKind::drumsynth);
  6078. inst->configure (def, 120.0);
  6079. inst->prepare (48000.0);
  6080. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  6081. for (int s = 0; s < total; s += 512)
  6082. {
  6083. for (std::size_t k = 0; k + 1 < keys.size(); k += 2)
  6084. if (keys[k + 1] == s)
  6085. inst->noteOn (keys[k], 100.0f / 128.0f);
  6086. inst->render (l.data() + s, r.data() + s, std::min (512, total - s), gl, gr);
  6087. }
  6088. return std::make_tuple (l, r, std::move (inst));
  6089. };
  6090. {
  6091. auto [l, r, inst] = render (c, { 60, 0 }, 9600);
  6092. // reference model (REA round 30 dsl_model.py, seed 0x56CE, FL velocity 100, V = 1, centre: × g(100)·cos(π/4))
  6093. const std::pair<int, float> golden[] { { 0, -0.0074092713f }, { 10, -0.13961405f }, { 100, -0.47290668f }, { 1000, -0.29188696f },
  6094. { 3000, -0.22130175f }, { 6000, -0.049537566f }, { 9599, 0.010392102f } };
  6095. for (const auto& [i, v] : golden)
  6096. CHECK (std::abs (l[(std::size_t) i] - v) < 2e-6f && std::abs (r[(std::size_t) i] - v) < 2e-6f);
  6097. std::vector<float> tail (48000 * 4), tailR (tail.size());
  6098. inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
  6099. CHECK (inst->idle()); // the engine reported the voice finished
  6100. }
  6101. auto energy = [] (const std::vector<float>& x, int from, int to) {
  6102. double e = 0.0;
  6103. for (int i = from; i < to; ++i)
  6104. e += (double) x[(std::size_t) i] * x[(std::size_t) i];
  6105. return e;
  6106. };
  6107. // a key without a patch level (slot 0, level 0) and a key outside the 120 patches are silent
  6108. CHECK (energy (std::get<0> (render (c, { 48, 0, 125, 0 }, 4096)), 0, 4096) == 0.0);
  6109. // mono: a second hit on the same key cuts the first; poly: both ring together (Osc2 off: no noise, deterministic)
  6110. auto tone = c;
  6111. tone.modelParams[38 * 12 + 14] = 0.0f;
  6112. const auto single = std::get<0> (render (tone, { 60, 0 }, 8192));
  6113. const auto mono = std::get<0> (render (tone, { 60, 0, 60, 2048 }, 8192));
  6114. auto poly = tone;
  6115. poly.modelParams[38 * 12 + 24] = 1.0f;
  6116. const auto both = std::get<0> (render (poly, { 60, 0, 60, 2048 }, 8192));
  6117. double monoDiff = 0.0, polyDiff = 0.0;
  6118. for (int i = 2048 + 300; i < 8192; ++i) // after the cut fade: mono = only the second hit, a copy of the first
  6119. monoDiff += std::abs (mono[(std::size_t) i] - single[(std::size_t) i - 2048]), polyDiff += std::abs (both[(std::size_t) i] - single[(std::size_t) i - 2048]);
  6120. CHECK (monoDiff < 1e-3 && polyDiff > 1.0);
  6121. // patch pan +64 → right only; channel volume counts twice (FL applies Final.Vol in the engine and at the host)
  6122. auto right = c;
  6123. right.modelParams[38 * 12 + 1] = 64.0f;
  6124. const auto [pl, pr, pi] = render (right, { 60, 0 }, 4096);
  6125. CHECK (energy (pl, 0, 4096) < 1e-12 * energy (pr, 0, 4096) && energy (pr, 0, 4096) > 1.0);
  6126. const auto full = std::get<0> (render (tone, { 60, 0 }, 4096));
  6127. const auto half = std::get<0> (render (tone, { 60, 0 }, 4096, 0.5f, 0.5f));
  6128. CHECK (std::abs (half[100] - 0.25f * full[100]) < 1e-6f && std::abs (full[100]) > 0.01f);
  6129. }
  6130. TEST (kepler_lfo_bpm_sync)
  6131. {
  6132. // REA_rodada33 §3.4: with BPM Sync (k6 ≥ 0.5) the LFO1 period is n/192 beats, n = trunc((2^(7(1 − x)) − 1)·48.370079 + 1.1)
  6133. std::vector<std::uint8_t> state (0x21 + 34 * 4, 0);
  6134. auto putU32 = [&] (std::size_t o, std::uint32_t v) { std::memcpy (state.data() + o, &v, 4); };
  6135. putU32 (12, 9);
  6136. std::memcpy (state.data() + 16, "Kepler-1", 9);
  6137. putU32 (0x19, 4);
  6138. putU32 (0x1D, 34);
  6139. float k[34] {};
  6140. k[5] = 0.602085769f; // "Pads - Kepler" demo: n = 286
  6141. k[6] = 1.0f;
  6142. k[10] = 1.0f; // vibrato on
  6143. k[33] = 1.0f;
  6144. std::memcpy (state.data() + 0x21, k, sizeof k);
  6145. lynx::SynthSettings s;
  6146. float gain = 0.0f;
  6147. CHECK (lynx::synthFromKepler (state, s, gain));
  6148. CHECK (s.lfo[lynx::lfoPitch].sync && s.lfo[lynx::lfoPitch].secondsTiming);
  6149. CHECK (std::abs (s.lfo[lynx::lfoPitch].period - 286.0f / 192.0f) < 1e-6f);
  6150. k[5] = 0.0f; // slowest: 32 beats
  6151. std::memcpy (state.data() + 0x21, k, sizeof k);
  6152. CHECK (lynx::synthFromKepler (state, s, gain) && std::abs (s.lfo[lynx::lfoPitch].period - 32.0f) < 1e-6f);
  6153. k[6] = 0.0f; // free rate in seconds
  6154. std::memcpy (state.data() + 0x21, k, sizeof k);
  6155. CHECK (lynx::synthFromKepler (state, s, gain) && ! s.lfo[lynx::lfoPitch].sync);
  6156. }
  6157. namespace
  6158. {
  6159. // A Harmor state written from scratch (REA_rodada11 §4.1 layout, version 18): part A only — a decaying-harmonics
  6160. // timbre, low-pass + resonance, filter envelope, 3-voice unison, vibrato, pluck, tremolo, volume envelope with a
  6161. // velocity map — and the global block. Every other curve is neutral.
  6162. std::vector<std::uint8_t> harmorTestState (bool panLfo = true)
  6163. {
  6164. std::vector<std::uint8_t> b;
  6165. auto i32 = [&] (std::int32_t v) { for (int k = 0; k < 4; ++k) b.push_back ((std::uint8_t) ((std::uint32_t) v >> (8 * k))); };
  6166. auto f64 = [&] (double v) { std::uint64_t u; std::memcpy (&u, &v, 8); for (int k = 0; k < 8; ++k) b.push_back ((std::uint8_t) (u >> (8 * k))); };
  6167. struct Pt { double dx, y, tension; int mode; };
  6168. auto curve = [&] (const std::vector<Pt>& pts, std::array<std::int32_t, 5> hdr) {
  6169. i32 (3), i32 (3), i32 ((std::int32_t) pts.size());
  6170. for (const auto& p : pts)
  6171. {
  6172. f64 (p.dx), f64 (p.y);
  6173. const auto t = (float) p.tension;
  6174. std::uint32_t u;
  6175. std::memcpy (&u, &t, 4);
  6176. i32 ((std::int32_t) u);
  6177. b.push_back ((std::uint8_t) p.mode), b.push_back (0), b.push_back (0), b.push_back (0);
  6178. }
  6179. for (auto h : hdr)
  6180. i32 (h);
  6181. };
  6182. const std::array<std::int32_t, 5> off { 0, 0, -1, -1, -1 }, on { 0, 1, -1, -1, -1 };
  6183. auto flat = [&] (double y, std::array<std::int32_t, 5> hdr = { 0, 0, -1, -1, -1 }) { curve ({ { 0, y, 0, 0 }, { 1, y, 0, 2 } }, hdr); };
  6184. i32 (18);
  6185. i32 (1 | 4);
  6186. std::vector<std::uint8_t> part;
  6187. std::swap (b, part);
  6188. std::array<std::int32_t, 386> p {};
  6189. const std::pair<int, int> values[] = { { 0, 500 }, { 3, 1000 }, { 9, 1024 }, { 11, 1000 }, { 12, 1000 }, { 17, 300 }, { 18, 4000 },
  6190. { 19, 500 }, { 20, 1000 }, { 24, 500 }, { 25, 500 }, { 28, 500 }, { 29, 256 }, { 30, -1 }, { 31, -1 }, { 33, -1 }, { 34, 3 },
  6191. { 35, 1 }, { 37, 192 }, { 38, 128 }, { 39, 500 }, { 40, 10000 }, { 41, 1 }, { 42, 10000 }, { 43, 1 }, { 45, 200 }, { 46, 500 },
  6192. { 47, 1 }, { 49, -140 }, { 50, 1200 }, { 51, 0 }, { 52, 5000 }, { 54, 500 }, { 56, 600 }, { 57, 300 }, { 59, 500 }, { 60, 600 },
  6193. { 64, 8192 }, { 66, 500 }, { 72, 600 }, { 74, 1000 }, { 75, 600 }, { 78, 5000 }, { 79, 500 }, { 80, 3000 } };
  6194. for (const auto& [i, v] : values)
  6195. p[(std::size_t) i] = v;
  6196. for (auto v : p)
  6197. i32 (v);
  6198. for (int i = 0; i < 12; ++i)
  6199. i32 (i); // module order
  6200. std::array<std::uint8_t, 0x4b> misc {};
  6201. auto put = [&] (int o, std::int32_t v) { std::memcpy (misc.data() + o, &v, 4); };
  6202. const float one = 1.0f;
  6203. put (0x08, 107), put (0x1c, 1), put (0x28, 200), std::memcpy (misc.data() + 0x30, &one, 4);
  6204. put (0x34, 0), put (0x38, 4), put (0x3c, 100), put (0x40, 108);
  6205. misc[0x44] = 1;
  6206. b.insert (b.end(), misc.begin(), misc.end());
  6207. static const int mode1[] = { 0, 2, 4, 5, 12, 13, 14, 15, 17, 18, 23, 24, 27, 28, 29, 30, 33, 36, 38, 39, 40, 41, 42 };
  6208. for (int k = 0; k < 43; ++k)
  6209. {
  6210. static const int mode2[] = { 2, 14, 23, 28, 30, 38, 39, 41, 42 }; // ×2 around 0.5: 1.0 is neutral
  6211. const bool centred = std::find (std::begin (mode1), std::end (mode1), k) != std::end (mode1);
  6212. const bool doubled = std::find (std::begin (mode2), std::end (mode2), k) != std::end (mode2);
  6213. const double neutral = centred && ! doubled ? 0.5 : 1.0;
  6214. i32 (3);
  6215. i32 (1065353216), i32 (1065353216), i32 (1065353216);
  6216. if (k == 1) // volume envelope: attack, decay to the sustain point, release
  6217. curve ({ { 0, 0, 0, 0 }, { 0.01, 1, 0.3, 0 }, { 0.3, 0.6, -0.4, 0 }, { 0.4, 0, 0.2, 0 } }, { 0, 1, 1, -1, 2 });
  6218. else if (k == 4) // filter-1 envelope
  6219. curve ({ { 0, 1, 0, 0 }, { 0.25, 0.2, -0.5, 0 } }, { 0, 1, -1, -1, -1 });
  6220. else
  6221. flat (neutral);
  6222. for (int a : { 128, 128, 0, 128 })
  6223. i32 (a);
  6224. if (k == 0 && panLfo) // pan LFO
  6225. flat (1.0, on);
  6226. else
  6227. flat (0.5);
  6228. for (int a : { k == 0 ? 40000 : 0, 0, 0, 0 })
  6229. i32 (a);
  6230. i32 (0);
  6231. for (int m = 2; m < 11; ++m)
  6232. if (k == 1 && m == 3)
  6233. curve ({ { 0, 0.2, 0, 0 }, { 1, 1, -0.3, 0 } }, off); // velocity → volume
  6234. else
  6235. flat (neutral);
  6236. }
  6237. for (int c = 0; c < 26; ++c)
  6238. if (c == 2 || c == 3)
  6239. curve ({ { 0, 0.25, 0, 0 }, { 0.006, 0.12, -0.46, 0 }, { 0.994, 0, 0, 0 } }, off); // timbres
  6240. else if (c == 14)
  6241. curve ({ { 0, 1, 0, 0 }, { 1, 0, 0, 0 } }, off); // pluck curve
  6242. else
  6243. flat (c == 0 || c == 17 ? 0.25 : (c == 5 ? 0.0 : (c == 6 || c == 10 || c == 11 || (c >= 18 && c <= 24) ? 1.0 : 0.5)));
  6244. std::swap (b, part);
  6245. i32 ((std::int32_t) part.size());
  6246. b.insert (b.end(), part.begin(), part.end());
  6247. std::array<std::int32_t, 53> g {};
  6248. g[0] = g[1] = g[2] = 256, g[10] = 500, g[11] = 1000, g[12] = 1000, g[15] = 1000;
  6249. for (auto v : g)
  6250. i32 (v);
  6251. for (int i = 0; i < 6; ++i)
  6252. i32 (i);
  6253. std::array<std::uint8_t, 0x3d> gm {};
  6254. const std::int32_t cr = 200, vr = 1;
  6255. std::memcpy (gm.data() + 12, &vr, 4);
  6256. std::memcpy (gm.data() + 24, &cr, 4);
  6257. b.insert (b.end(), gm.begin(), gm.end());
  6258. return b;
  6259. }
  6260. } // namespace
  6261. TEST (harmor_engine)
  6262. {
  6263. // REA_rodada34: state parsing, the additive engine against our reference model (hmodel.py), host levels, voice end
  6264. const auto blob = harmorTestState();
  6265. if (const char* dump = std::getenv ("LYNX_DUMP_HARMOR"))
  6266. std::ofstream (dump, std::ios::binary).write ((const char*) blob.data(), (std::streamsize) blob.size());
  6267. CHECK (lynx::harmorStateUsable (blob));
  6268. CHECK (lynx::harmorFallbacks (blob).size() == 1); // (only the harmonic-randomness note)
  6269. const auto at = lynx::harmorParamOffset (blob, 0, 52);
  6270. std::int32_t cutoff = 0;
  6271. CHECK (at > 0 && (std::memcpy (&cutoff, blob.data() + at, 4), cutoff == 5000));
  6272. CHECK (lynx::harmorParamOffset (blob, 1, 52) < 0 && lynx::harmorParamOffset (blob, -1, 12) > at);
  6273. auto broken = blob;
  6274. broken.resize (broken.size() / 2);
  6275. CHECK (! lynx::harmorStateUsable (broken));
  6276. auto old = blob;
  6277. old[0] = 15; // articulators and curves are stored from version 16
  6278. CHECK (! lynx::harmorStateUsable (old));
  6279. lynx::ChannelDef c;
  6280. c.instrument = lynx::InstrumentKind::harmor;
  6281. c.pluginState = blob;
  6282. auto render = [] (const lynx::ChannelDef& def, int total, float gl, float gr, int offAt) {
  6283. auto inst = lynx::makeInstrument (lynx::InstrumentKind::harmor);
  6284. inst->configure (def, 120.0);
  6285. inst->prepare (44100.0);
  6286. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  6287. inst->noteOn (60, 0.78125f);
  6288. for (int s = 0; s < total; s += 512)
  6289. {
  6290. if (s == offAt)
  6291. inst->noteOff (60);
  6292. inst->render (l.data() + s, r.data() + s, std::min (512, total - s), gl, gr);
  6293. }
  6294. return std::make_tuple (l, r, std::move (inst));
  6295. };
  6296. {
  6297. // reference model (REA round 34 hmodel.py; C5 at FL velocity 100, Final.Vol 1, centre, released at 6144, the
  6298. // Mersenne Twister seeded like LYNX's)
  6299. auto [l, r, inst] = render (c, 8192, 1.0f, 1.0f, 6144);
  6300. const std::tuple<int, float, float> golden[] {
  6301. { 0, 0.0f, 0.0f }, { 100, 0.00947693f, 0.04249846f }, { 1000, 0.08140575f, -0.03251422f }, { 3000, 0.05928446f, -0.00247572f },
  6302. { 6000, 0.02997839f, 0.14549091f }, { 7000, -0.01121871f, 0.04197952f }, { 8191, 0.00314545f, 0.03857505f } };
  6303. for (const auto& [i, gl, gr] : golden)
  6304. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  6305. // after the release the volume envelope ends, the record declicks and the instrument goes idle
  6306. std::vector<float> tail (44100), tailR (tail.size());
  6307. inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
  6308. CHECK (inst->idle());
  6309. CHECK (std::abs (tail.back()) == 0.0f);
  6310. }
  6311. // the reference renderer gives the same samples as the instrument
  6312. {
  6313. const auto ref = lynx::harmor::renderReference (blob, { { 0, 60, 0.78125, 1.0, 0.0, 6144 } }, 44100.0, 8192);
  6314. const auto [l, r, inst] = render (c, 8192, 1.0f, 1.0f, 6144);
  6315. double worst = 0.0;
  6316. for (int i = 0; i < 8192; ++i)
  6317. worst = std::max ({ worst, (double) std::abs (ref[2 * (std::size_t) i] - l[(std::size_t) i]), (double) std::abs (ref[2 * (std::size_t) i + 1] - r[(std::size_t) i]) });
  6318. CHECK (worst == 0.0);
  6319. }
  6320. // FL's level law applied once: half the channel volume halves the output; full right pan silences the left side
  6321. const auto full = std::get<0> (render (c, 2048, 1.0f, 1.0f, -1));
  6322. const auto half = std::get<0> (render (c, 2048, 0.5f, 0.5f, -1));
  6323. CHECK (std::abs (half[1000] - 0.5f * full[1000]) < 1e-6f && std::abs (full[1000]) > 0.01f);
  6324. auto mono = c;
  6325. mono.pluginState = harmorTestState (false); // no pan LFO ...
  6326. const std::int32_t one = 1;
  6327. std::memcpy (mono.pluginState.data() + lynx::harmorParamOffset (mono.pluginState, 0, 34), &one, 4); // ... nor unison spread
  6328. const auto [pl, pr, pi] = render (mono, 2048, 0.0f, 1.41421356f, -1);
  6329. double el = 0.0, er = 0.0;
  6330. for (int i = 0; i < 2048; ++i)
  6331. el += (double) pl[(std::size_t) i] * pl[(std::size_t) i], er += (double) pr[(std::size_t) i] * pr[(std::size_t) i];
  6332. CHECK (el < 1e-10 * er && er > 1e-3);
  6333. }
  6334. namespace
  6335. {
  6336. // An Ogun state written from scratch (REA_rodada37 §1.3 layout, version 18; zlib with stored blocks): richness 4
  6337. // (255 partials), pre-decay, decay/release, fullness, Seed 1 + fixed Seed 2 with morph, 3-voice unison, a 2-stage
  6338. // low-pass with an LFO on its cutoff, a volume envelope with a sustain point and a velocity map, declick, normalisation,
  6339. // Impulse/Decay maps, and every plugin effect (EQ band, chorus, ping-pong delay, reverb). Everything else is neutral.
  6340. std::vector<std::uint8_t> ogunTestState()
  6341. {
  6342. std::vector<std::uint8_t> b;
  6343. auto i32 = [&] (std::int32_t v) { for (int k = 0; k < 4; ++k) b.push_back ((std::uint8_t) ((std::uint32_t) v >> (8 * k))); };
  6344. auto f32v = [&] (float v) { std::uint32_t u; std::memcpy (&u, &v, 4); i32 ((std::int32_t) u); };
  6345. auto f64 = [&] (double v) { std::uint64_t u; std::memcpy (&u, &v, 8); for (int k = 0; k < 8; ++k) b.push_back ((std::uint8_t) (u >> (8 * k))); };
  6346. struct Pt { double dx, y, tension; int mode; };
  6347. auto curve = [&] (int type, const std::vector<Pt>& pts, std::array<std::int32_t, 5> hdr) {
  6348. i32 (type), i32 (3), i32 ((std::int32_t) pts.size());
  6349. for (const auto& p : pts)
  6350. {
  6351. f64 (p.dx), f64 (p.y), f32v ((float) p.tension);
  6352. b.push_back ((std::uint8_t) p.mode), b.push_back (0), b.push_back (0), b.push_back (0);
  6353. }
  6354. for (auto h : hdr)
  6355. i32 (h);
  6356. };
  6357. const std::array<std::int32_t, 5> off { 0, 0, -1, -1, -1 }, on { 0, 1, -1, -1, -1 };
  6358. auto flat = [&] (int type, double y) { curve (type, { { 0, y, 0, 0 }, { 1, y, 0, 0 } }, off); };
  6359. std::array<std::int32_t, 64> P {};
  6360. const std::pair<int, int> values[] = { { 0, 128 }, { 5, 300 }, { 6, 3000 }, { 7, 2000 }, { 8, 200 }, { 9, 500 }, { 10, 12 },
  6361. { 11, 34 }, { 12, 3 }, { 13, 64 }, { 14, 64 }, { 15, 40 }, { 16, 64 }, { 17, 180 }, { 18, 60 }, { 19, 2 }, { 20, 300 },
  6362. { 21, 400 }, { 22, 40 }, { 23, 30 }, { 24, 20 }, { 25, 64 }, { 26, 40 }, { 27, 96 }, { 28, 100 }, { 29, 64 }, { 30, 2 },
  6363. { 31, 19 }, { 32, 221 }, { 33, 0 }, { 34, 50 }, { 35, 100 }, { 36, 20 }, { 37, 60 }, { 38, 2 }, { 39, 64 }, { 40, 300 },
  6364. { 43, 30000 }, { 46, 20000 }, { 49, 0 }, { 50, 2 }, { 51, 0 }, { 52, 9 }, { 53, 1 }, { 54, 0 },
  6365. { 55, 0x01000000 }, // +0x20b: pitch follows the key
  6366. { 56, 0x00010000 }, // +0x20e: declick
  6367. { 62, 0x00000100 } }; // +0x225: normalise
  6368. for (const auto& [i, v] : values)
  6369. P[(std::size_t) i] = v;
  6370. for (auto v : P)
  6371. i32 (v);
  6372. b.push_back (0), b.push_back (1), b.push_back (0); // delay: not tempo-based, on
  6373. i32 (1), i32 (1), i32 (1); // EQ band types: peaking
  6374. b.push_back (0); // comment: 0 characters
  6375. for (int k = 0; k < 13; ++k)
  6376. {
  6377. static constexpr int sum[] = { 1, 2, 3, 9, 11, 12 };
  6378. const double neutral = std::find (std::begin (sum), std::end (sum), k) != std::end (sum) ? 0.5 : 1.0;
  6379. i32 (1);
  6380. f32v (0.0f), f32v (1.0f), f32v (k == 1 ? 0.3f : 0.0f);
  6381. if (k == 0) // volume envelope: attack, decay to the sustain point, release
  6382. curve (1, { { 0, 0, 0, 0 }, { 0.02, 1, 0.3, 0 }, { 0.3, 0.5, -0.4, 0 }, { 0.5, 0, 0.2, 0 } }, { 0, 1, 2, -1, 2 });
  6383. else
  6384. flat (1, neutral);
  6385. for (int a : { 128, 128, 128, 128 })
  6386. i32 (a);
  6387. if (k == 1) // cutoff LFO
  6388. curve (2, { { 0, 0.8, 0, 0 } }, on);
  6389. else
  6390. flat (2, 0.5);
  6391. for (int a : { k == 1 ? 40000 : 0, 0, 0, 0 })
  6392. i32 (a);
  6393. i32 (0);
  6394. for (int m = 0; m < 4; ++m)
  6395. if (k == 0 && m == 1)
  6396. curve (3, { { 0, 0.3, 0, 0 }, { 1, 1, -0.2, 0 } }, off); // velocity → volume
  6397. else
  6398. flat (3, neutral);
  6399. }
  6400. flat (3, 0.0); // Forced: none
  6401. curve (3, { { 0, 0.6, 0, 0 }, { 1, 0.4, 0.3, 0 } }, off); // Impulse EQ
  6402. curve (3, { { 0, 0.5, 0, 0 }, { 1, 0.3, 0, 0 } }, off); // Decay EQ
  6403. flat (3, 0.5); // Release EQ
  6404. // u32 version + zlib (stored blocks)
  6405. std::vector<std::uint8_t> s { 18, 0, 0, 0, 0x78, 0x01 };
  6406. for (std::size_t i = 0; i < b.size();)
  6407. {
  6408. const auto len = std::min<std::size_t> (65535, b.size() - i);
  6409. s.push_back (i + len >= b.size() ? 1 : 0);
  6410. s.push_back ((std::uint8_t) len), s.push_back ((std::uint8_t) (len >> 8));
  6411. s.push_back ((std::uint8_t) ~len), s.push_back ((std::uint8_t) (~len >> 8));
  6412. s.insert (s.end(), b.begin() + (std::ptrdiff_t) i, b.begin() + (std::ptrdiff_t) (i + len));
  6413. i += len;
  6414. }
  6415. std::uint32_t a1 = 1, a2 = 0;
  6416. for (auto c : b)
  6417. a1 = (a1 + c) % 65521u, a2 = (a2 + a1) % 65521u;
  6418. for (int sh = 24; sh >= 0; sh -= 8)
  6419. s.push_back ((std::uint8_t) (((a2 << 16) | a1) >> sh));
  6420. return s;
  6421. }
  6422. } // namespace
  6423. TEST (ogun_engine)
  6424. {
  6425. // REA_rodada37: state parsing, the engine against our reference model (ogmodel.py), host levels, export quality
  6426. const auto blob = ogunTestState();
  6427. if (const char* dump = std::getenv ("LYNX_DUMP_OGUN"))
  6428. std::ofstream (dump, std::ios::binary).write ((const char*) blob.data(), (std::streamsize) blob.size());
  6429. CHECK (lynx::ogunStateUsable (blob));
  6430. CHECK (lynx::ogunFallbacks (blob).empty());
  6431. CHECK (lynx::ogunParam (blob, 17) == 180 && lynx::ogunParam (blob, 52) == 9 && ! lynx::ogunParam (blob, 64));
  6432. auto broken = blob;
  6433. broken.resize (broken.size() - 300);
  6434. CHECK (! lynx::ogunStateUsable (broken));
  6435. auto old = blob;
  6436. old[0] = 13; // the articulator layout is from version 14
  6437. CHECK (! lynx::ogunStateUsable (old));
  6438. // reference model (REA round 37 ogmodel.Plugin(state).run: C5 velocity 0.75 released at 6144, G5 velocity 0.625 at 2048
  6439. // released at 8192; host block 512)
  6440. const std::vector<lynx::ogun::RefNote> notes { { 0, 60, 0.75, 1.0, 0.0, 0.0, 0.0, 0.0, 0.5, 6144 }, { 2048, 67, 0.625, 1.0, 0.0, 0.0, 0.0, 0.0, 0.5, 8192 } };
  6441. const auto ref = lynx::ogun::renderReference (blob, notes, 44100.0, 16384);
  6442. CHECK (ref.size() == 2 * 16384);
  6443. if (const char* dump = std::getenv ("LYNX_DUMP_OGUN_OUT"))
  6444. std::ofstream (dump, std::ios::binary).write ((const char*) ref.data(), (std::streamsize) (ref.size() * 4));
  6445. const std::tuple<int, float, float> golden[] {
  6446. { 100, -7.763202e-05f, -8.919523e-05f }, { 1000, 0.06195267f, 0.03858230f }, { 3000, 0.01363476f, -0.00378433f },
  6447. { 6000, -0.06601223f, -0.03084030f }, { 9000, 0.04204506f, 0.02452896f }, { 12000, 0.03420147f, -0.01837870f },
  6448. { 16383, -0.00465311f, -0.00119627f } };
  6449. for (const auto& [i, gl, gr] : golden)
  6450. CHECK (std::abs (ref[2 * (std::size_t) i] - gl) < 2e-6f && std::abs (ref[2 * (std::size_t) i + 1] - gr) < 2e-6f);
  6451. // the instrument plays like the reference renderer (Final.Vol 1, centre)
  6452. lynx::ChannelDef c;
  6453. c.instrument = lynx::InstrumentKind::ogun;
  6454. c.pluginState = blob;
  6455. auto render = [&] (const lynx::ChannelDef& def, int total, float gl, float gr, bool held = false) {
  6456. auto inst = lynx::makeInstrument (lynx::InstrumentKind::ogun);
  6457. inst->configure (def, 120.0);
  6458. inst->prepare (44100.0);
  6459. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  6460. for (int s = 0; s < total; s += 512)
  6461. {
  6462. if (s == 0)
  6463. inst->noteOn (60, 0.75f);
  6464. if (s == 2048)
  6465. inst->noteOn (67, 0.625f);
  6466. if (s == 6144 && ! held)
  6467. inst->noteOff (60);
  6468. if (s == 8192 && ! held)
  6469. inst->noteOff (67);
  6470. inst->render (l.data() + s, r.data() + s, std::min (512, total - s), gl, gr);
  6471. }
  6472. return std::make_tuple (l, r, std::move (inst));
  6473. };
  6474. {
  6475. auto [l, r, inst] = render (c, 16384, 1.0f, 1.0f);
  6476. double worst = 0.0;
  6477. for (int i = 0; i < 16384; ++i)
  6478. worst = std::max ({ worst, (double) std::abs (ref[2 * (std::size_t) i] - l[(std::size_t) i]), (double) std::abs (ref[2 * (std::size_t) i + 1] - r[(std::size_t) i]) });
  6479. CHECK (worst == 0.0);
  6480. // the voices end and the effect tails die out: the instrument goes idle
  6481. std::vector<float> tail (44100), tailR (tail.size());
  6482. for (int k = 0; k < 20 && ! inst->idle(); ++k)
  6483. inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
  6484. CHECK (inst->idle());
  6485. }
  6486. // FL's level law applied once (effects off): half the channel volume halves the output once the gain ramps
  6487. // (limited to 0.001 per sample) have settled on the sustain level; full right pan silences the left side
  6488. {
  6489. auto dry = c;
  6490. for (int i : { 19, 29, 40, 53 })
  6491. CHECK (lynx::ogunSetParam (dry.pluginState, i, 0)); // chorus, delay wet, EQ band and reverb off
  6492. CHECK (lynx::ogunStateUsable (dry.pluginState) && lynx::ogunParam (dry.pluginState, 19) == 0);
  6493. const auto full = std::get<0> (render (dry, 32768, 1.0f, 1.0f, true));
  6494. const auto half = std::get<0> (render (dry, 32768, 0.5f, 0.5f, true));
  6495. double worst = 0.0, peak = 0.0;
  6496. for (int i = 30000; i < 32768; ++i)
  6497. worst = std::max (worst, (double) std::abs (half[(std::size_t) i] - 0.5f * full[(std::size_t) i])),
  6498. peak = std::max (peak, (double) std::abs (full[(std::size_t) i]));
  6499. CHECK (worst <= 1e-7 * peak && peak > 1e-3);
  6500. // FL's default channel volume (100/128): FL hands Ogun Final.Vol = (11^v − 1)/10 (REA_rodada28 §4.1); LYNX's
  6501. // centred channel gains (√2·V·cos(π/4) each) give the reference model's level at that Final.Vol
  6502. const auto V = lynx::channelGain (0.78125f);
  6503. const auto g = V * std::cos (0.25f * 3.14159265f) * 1.41421356f;
  6504. const auto atDefault = std::get<0> (render (dry, 32768, g, g, true));
  6505. const auto refDefault = lynx::ogun::renderReference (dry.pluginState, { { 0, 60, 0.75, V, 0.0, 0.0, 0.0, 0.0, 0.5, -1 },
  6506. { 2048, 67, 0.625, V, 0.0, 0.0, 0.0, 0.0, 0.5, -1 } }, 44100.0, 32768);
  6507. double ei = 0.0, er0 = 0.0;
  6508. for (int i = 30000; i < 32768; ++i)
  6509. ei += (double) atDefault[(std::size_t) i] * atDefault[(std::size_t) i], er0 += (double) refDefault[2 * (std::size_t) i] * refDefault[2 * (std::size_t) i];
  6510. CHECK (er0 > 0.0 && std::abs (10.0 * std::log10 (ei / er0)) < 0.001);
  6511. const auto [pl, pr, pi] = render (dry, 4096, 0.0f, 1.41421356f);
  6512. double el = 0.0, er = 0.0;
  6513. for (int i = 0; i < 4096; ++i)
  6514. el += (double) pl[(std::size_t) i] * pl[(std::size_t) i], er += (double) pr[(std::size_t) i] * pr[(std::size_t) i];
  6515. CHECK (el < 1e-10 * er && er > 1e-3);
  6516. }
  6517. // export: FL renders Ogun at quality 4 (×2 rate, 32-point reader), LYNX too while it renders offline
  6518. {
  6519. lynx::setOfflineRendering (true);
  6520. auto [l, r, inst] = render (c, 8192, 1.0f, 1.0f);
  6521. lynx::setOfflineRendering (false);
  6522. lynx::ogun::RefOptions q4;
  6523. q4.quality = 4;
  6524. const auto ref4 = lynx::ogun::renderReference (blob, notes, 44100.0, 8192, q4);
  6525. double worst = 0.0, diff = 0.0;
  6526. for (int i = 0; i < 8192; ++i)
  6527. worst = std::max (worst, (double) std::abs (ref4[2 * (std::size_t) i] - l[(std::size_t) i])),
  6528. diff = std::max (diff, (double) std::abs (ref[2 * (std::size_t) i] - l[(std::size_t) i]));
  6529. CHECK (worst == 0.0 && diff > 1e-3);
  6530. const std::tuple<int, float, float> golden4[] {
  6531. { 100, 0.00605961f, 0.00628969f }, { 1000, 0.03729841f, 0.01727954f }, { 3000, -0.09010383f, -0.02551294f },
  6532. { 6000, 0.02167798f, -0.01115632f }, { 8191, 0.00320288f, -0.00615792f } };
  6533. for (const auto& [i, gl, gr] : golden4)
  6534. CHECK (std::abs (ref4[2 * (std::size_t) i] - gl) < 2e-6f && std::abs (ref4[2 * (std::size_t) i + 1] - gr) < 2e-6f);
  6535. }
  6536. // feature variants of the same state against the reference model (energy per side over 8192 samples, three
  6537. // samples; C1 released at 6144): qualities, richness, unison, the filter types, ALT DECAY, 1/√h, Seed 2 Random,
  6538. // infinite decay, logarithmic axis, velocity → position, other rates. Goldens from ogmodel.py (REA round 37).
  6539. {
  6540. struct Sample { int at; float l, r; };
  6541. struct Variant { const char* name; std::vector<std::pair<int, int>> params; int rate; double el, er; Sample s[3]; };
  6542. const Variant variants[] {
  6543. { "q1", { { 54, 1 } }, 44100, 11.0970402, 6.87820714, { { 1500, 0.0240363069f, -0.0148524037f }, { 4000, 0.0873190612f, -0.000715861097f }, { 7000, -0.0378603041f, -0.00338819111f } } },
  6544. { "q2", { { 54, 2 } }, 44100, 11.2405446, 6.68045082, { { 1500, 0.0448660068f, -0.00560745923f }, { 4000, -0.0122657465f, 0.0104285963f }, { 7000, -0.0216034129f, -0.0134632047f } } },
  6545. { "q3", { { 54, 3 } }, 44100, 11.3200608, 6.86196449, { { 1500, -0.0031537537f, -0.0242648814f }, { 4000, 0.0426301025f, 0.0364697985f }, { 7000, 0.00843803957f, 0.0302239582f } } },
  6546. { "rich16_q0", { { 52, 16 }, { 54, 0 } }, 44100, 26.7616786, 19.2591256, { { 1500, 0.0728010684f, -0.0116148153f }, { 4000, 0.0797849894f, 0.0765359923f }, { 7000, -0.00811834447f, -0.0104534971f } } },
  6547. { "uni9", { { 12, 9 }, { 15, 20 }, { 16, 0 }, { 14, 16 } }, 44100, 61.7485898, 36.6693432, { { 1500, 0.0531249829f, -0.0374071151f }, { 4000, 0.165054232f, -0.0192129575f }, { 7000, -0.0373774841f, -0.00196591392f } } },
  6548. { "lp1", { { 50, 1 }, { 49, 0 } }, 44100, 7.81941168, 4.72659869, { { 1500, 0.0377117358f, -0.0157920271f }, { 4000, 0.0747607499f, 0.00154248625f }, { 7000, -0.0510631353f, 0.00884498842f } } },
  6549. { "bp3flat", { { 50, 3 }, { 49, 1 }, { 51, 1 } }, 44100, 2.19181431, 1.29483907, { { 1500, 0.0291239917f, -0.00288665155f }, { 4000, -0.0084345676f, -0.00400509313f }, { 7000, -0.00592403999f, 0.00150578981f } } },
  6550. { "bs2", { { 50, 2 }, { 49, 2 } }, 44100, 32.8440514, 25.7202184, { { 1500, 0.0730303749f, -0.0496222489f }, { 4000, 0.0696200803f, 0.0595823973f }, { 7000, -0.0482789353f, 0.0764200836f } } },
  6551. { "hp3", { { 50, 3 }, { 49, 3 } }, 44100, 8.623214, 7.95253188, { { 1500, -0.0325722694f, 0.00703159999f }, { 4000, 0.0183976218f, -0.0372007042f }, { 7000, -0.0126833525f, -0.00160143059f } } },
  6552. { "alt", { { 62, 257 } }, 44100, 17.2485711, 9.99223979, { { 1500, -0.0319156498f, -0.0186850764f }, { 4000, -0.0769012421f, -0.0375530645f }, { 7000, 0.0310976505f, 0.0445244014f } } },
  6553. { "isqrt", { { 62, 65792 } }, 44100, 45.7309307, 27.1224593, { { 1500, -0.04376591f, -0.00845711026f }, { 4000, 0.0351300463f, -0.129557073f }, { 7000, 0.0096148327f, 0.00123740174f } } },
  6554. { "seed2rnd", { { 11, 0 } }, 44100, 16.8228819, 11.5433061, { { 1500, -0.0117614279f, -0.026497297f }, { 4000, 0.00265733898f, -0.07069581f }, { 7000, 0.00576663949f, 0.0565217957f } } },
  6555. { "decinf", { { 6, 10000 } }, 44100, 19.1879226, 11.0487311, { { 1500, -0.0324105881f, -0.0190765262f }, { 4000, -0.0809780285f, -0.0395730771f }, { 7000, 0.034197174f, 0.048585102f } } },
  6556. { "logx", { { 57, 16777216 } }, 44100, 12.3064326, 7.31519289, { { 1500, -0.0438990779f, 0.0096983267f }, { 4000, -0.0175748877f, 0.0313935578f }, { 7000, 0.0639200211f, 0.024419222f } } },
  6557. { "velpos", { { 56, 66304 } }, 44100, 4.9966925, 3.16540662, { { 1500, 0.0083851777f, -0.0165472236f }, { 4000, 0.0630451962f, 0.0156494901f }, { 7000, -0.0293043777f, -0.00182877108f } } },
  6558. { "fs48", { }, 48000, 10.8813888, 6.53592373, { { 1500, -0.0485184453f, -0.0319588967f }, { 4000, 0.0230421089f, -0.0286604818f }, { 7000, -0.0117630213f, -0.017459048f } } },
  6559. { "fs96", { }, 96000, 11.0189683, 6.39821883, { { 1500, -0.0267860368f, -0.0227564126f }, { 4000, 0.0238709711f, -0.0100849615f }, { 7000, -0.0034619756f, 0.0451687202f } } },
  6560. };
  6561. for (const auto& v : variants)
  6562. {
  6563. auto st = blob;
  6564. for (const auto& [i, x] : v.params)
  6565. CHECK (lynx::ogunSetParam (st, i, x));
  6566. const auto y = lynx::ogun::renderReference (st, { { 0, 60, 0.75, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 6144 },
  6567. { 2048, 67, 0.625, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1 } }, (double) v.rate, 8192);
  6568. double el = 0.0, er = 0.0, peak = 0.0;
  6569. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  6570. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1], peak = std::max ({ peak, (double) std::abs (y[i]), (double) std::abs (y[i + 1]) });
  6571. bool ok = y.size() == 2 * 8192 && std::abs (el - v.el) <= 1e-5 * v.el && std::abs (er - v.er) <= 1e-5 * v.er;
  6572. for (const auto& smp : v.s)
  6573. ok = ok && std::abs (y[2 * (std::size_t) smp.at] - smp.l) <= 1e-5 * peak && std::abs (y[2 * (std::size_t) smp.at + 1] - smp.r) <= 1e-5 * peak;
  6574. if (! ok)
  6575. std::printf (" Ogun variant %s differs from the model: energy %.9g %.9g (model %.9g %.9g)\n", v.name, el, er, v.el, v.er);
  6576. CHECK (ok);
  6577. }
  6578. }
  6579. }
  6580. // our own Maximus v7 state (layout REA_rodada8 §1.1): 7 globals, 4 × 13 band parameters, 8 envelopes (dx, y, tension, type)
  6581. static std::vector<std::uint8_t> maximusTestState (const std::array<int, 7>& g, const std::array<std::array<int, 13>, 4>& bands,
  6582. const std::array<std::vector<std::array<double, 4>>, 8>& envs, int oversampling)
  6583. {
  6584. std::vector<std::uint8_t> d;
  6585. auto put = [&] (const void* v, std::size_t n) { d.insert (d.end(), (const std::uint8_t*) v, (const std::uint8_t*) v + n); };
  6586. auto i32 = [&] (std::int32_t v) { put (&v, 4); };
  6587. i32 (7);
  6588. for (int copy = 0; copy < 2; ++copy)
  6589. {
  6590. for (auto v : g)
  6591. i32 (v);
  6592. for (const auto& b : bands)
  6593. for (auto v : b)
  6594. i32 (v);
  6595. }
  6596. d.resize (0x1fe, 0);
  6597. std::memcpy (d.data() + 0x1f4, &oversampling, 4);
  6598. for (const auto& e : envs)
  6599. {
  6600. i32 (3), i32 (2), i32 ((std::int32_t) e.size());
  6601. for (const auto& pt : e)
  6602. {
  6603. const double dx = pt[0], y = pt[1];
  6604. const float t = (float) pt[2];
  6605. put (&dx, 8), put (&y, 8), put (&t, 4);
  6606. const std::uint8_t flags[4] { (std::uint8_t) pt[3], 0, 0, 0 };
  6607. put (flags, 4);
  6608. }
  6609. i32 (0), i32 (-1), i32 (-1), i32 (-1), i32 (-1);
  6610. }
  6611. return d;
  6612. }
  6613. // A "Kepler-1" state written from scratch (REA_rodada46 §1.1 layout, version 2): the default panel values
  6614. std::vector<std::uint8_t> keplerTestState()
  6615. {
  6616. const float panel[34] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0.5f, 0.5f, 0, 0, 0, 0.5f, 0.5f };
  6617. std::vector<std::uint8_t> st;
  6618. auto u32 = [&] (std::uint32_t v) { for (int i = 0; i < 4; ++i) st.push_back ((std::uint8_t) (v >> (8 * i))); };
  6619. auto f32 = [&] (float f) { std::uint32_t v; std::memcpy (&v, &f, 4); u32 (v); };
  6620. u32 (0), u32 (0), u32 (0), u32 (9);
  6621. for (char c : std::string ("Kepler-1"))
  6622. st.push_back ((std::uint8_t) c);
  6623. st.push_back (0);
  6624. u32 (2), u32 (34);
  6625. for (auto f : panel)
  6626. f32 (f);
  6627. st.push_back (0);
  6628. f32 (1.0f), f32 (0.5f);
  6629. const auto size = (std::uint32_t) st.size() - 12;
  6630. std::memcpy (st.data() + 4, &size, 4);
  6631. return st;
  6632. }
  6633. std::string keplerDll()
  6634. {
  6635. if (realFlpDir.empty())
  6636. return {};
  6637. return (std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "Kepler" / "engine_x64.dll").string();
  6638. }
  6639. TEST (kepler_engine)
  6640. {
  6641. const auto st = keplerTestState();
  6642. CHECK (st.size() == 178 && lynx::keplerStateUsable (st));
  6643. CHECK (lynx::keplerPanelOffset (st, 19) == 0x21 + 4 * 19 && lynx::keplerPanelOffset (st, 34) == -1);
  6644. auto broken = st;
  6645. broken[17] = 'X'; // not "Kepler-1"
  6646. CHECK (! lynx::keplerStateUsable (broken) && lynx::keplerPanelOffset (broken, 0) == -1);
  6647. auto v3 = st;
  6648. v3[0x19] = 3; // version 3 needs the gate / noise / oversampling bytes
  6649. CHECK (! lynx::keplerStateUsable (v3));
  6650. const auto dll = keplerDll();
  6651. if (dll.empty() || ! lynx::keplerDllUsable (dll))
  6652. return; // no FL installation (the engine needs the user's own DLL tables)
  6653. // against FL's engine DLL (EXEC, REA_rodada46 §6): three notes, 44.1 kHz, host blocks of 512 — bit-exact
  6654. const std::vector<lynx::kepler::RefNote> notes { { 0, 60, 1.0, 0.0, 0.0, 20000 }, { 8000, 67, 0.6, 0.0, 0.0, 26000 }, { 16000, 55, 0.95, 0.0, 0.0, 30000 } };
  6655. const auto y = lynx::kepler::renderReference (st, dll, notes, 44100.0, 39690);
  6656. CHECK (y.size() == 2 * 39690);
  6657. if (y.size() != 2 * 39690)
  6658. return;
  6659. double el = 0.0, er = 0.0;
  6660. for (std::size_t i = 0; i < y.size(); i += 2)
  6661. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  6662. std::printf (" Kepler default patch: energy %.9g %.9g (FL's DLL 250.641268)\n", el, er);
  6663. CHECK (std::abs (el - 250.641268) < 1e-6 && std::abs (er - 250.641268) < 1e-6);
  6664. const std::tuple<int, float, float> golden[] { { 5000, 0.0467273332f, 0.0467273332f }, { 12000, -0.119412214f, -0.119412214f },
  6665. { 25000, 0.0855825394f, 0.0855825394f }, { 35000, -3.34166579e-08f, -3.34166579e-08f } };
  6666. for (const auto& [i, l, r] : golden)
  6667. CHECK (y[2 * (std::size_t) i] == l && y[2 * (std::size_t) i + 1] == r);
  6668. // the instrument: FL's wrapper velocity, channel volume as an output gain, idle after the release
  6669. lynx::ChannelDef c;
  6670. c.instrument = lynx::InstrumentKind::kepler;
  6671. c.pluginState = st;
  6672. c.samplePath = dll;
  6673. auto inst = lynx::makeInstrument (lynx::InstrumentKind::kepler);
  6674. inst->configure (c, 120.0);
  6675. inst->prepare (44100.0);
  6676. std::vector<float> l (4096), r (4096);
  6677. inst->noteOn (60, 0.8f);
  6678. double e = 0.0;
  6679. bool finite = true;
  6680. for (int b = 0; b < 8; ++b)
  6681. {
  6682. std::fill (l.begin(), l.end(), 0.0f);
  6683. std::fill (r.begin(), r.end(), 0.0f);
  6684. inst->render (l.data(), r.data(), 4096, 0.5f, 0.5f);
  6685. for (int i = 0; i < 4096; ++i)
  6686. finite = finite && std::isfinite (l[(std::size_t) i]) && std::isfinite (r[(std::size_t) i]), e += (double) l[(std::size_t) i] * l[(std::size_t) i];
  6687. if (b == 3)
  6688. inst->noteOff (60);
  6689. }
  6690. CHECK (finite && e > 1.0);
  6691. for (int b = 0; b < 40; ++b)
  6692. inst->render (l.data(), r.data(), 4096, 0.5f, 0.5f);
  6693. CHECK (inst->idle());
  6694. }
  6695. TEST (kepler_demo_channels)
  6696. {
  6697. // every Kepler channel of FL's demo projects on LYNX's engine against FL's engine DLL (EXEC fingerprints, REA_rodada46 §6;
  6698. // the engine differs from the DLL by −127 dB or less on these, from the DLL library's pow rounding)
  6699. const auto dll = keplerDll();
  6700. if (dll.empty() || ! lynx::keplerDllUsable (dll))
  6701. return;
  6702. struct Sample { int at; float l, r; };
  6703. struct Golden { const char* file; const char* channel; std::size_t stateSize; double el, er; Sample s[4]; };
  6704. const Golden goldens[] {
  6705. { "Demo songs/Edlan & Ella Noël - Song For You.flp", "Pads - Kepler", 181, 2595.34411, 1181.70052, { { 5000, 0.258449465f, 0.284368932f }, { 12000, -0.0648983642f, 0.312173635f }, { 25000, -0.271028787f, 0.103852645f }, { 35000, -0.00873807631f, -0.00636588456f } } },
  6706. { "Demo songs/Purps On The Beat Feat Luclover - Stroll Up.flp", "Kepler", 189, 2.77173241, 1.36643586, { { 5000, 0.00653714081f, 0.00885556825f }, { 12000, 0.0316556357f, 0.0255686305f }, { 25000, -0.00359999924f, -0.0027268643f }, { 35000, 3.38630743e-05f, 3.35237855e-05f } } },
  6707. { "Demo songs/Sid De La Cruz - Secret Agent Rendezvous.flp", "AceBass", 181, 491.893665, 813.858274, { { 5000, 0.00933594909f, 0.0551614426f }, { 12000, 0.11930909f, -0.387893438f }, { 25000, -0.0520403236f, -0.131727993f }, { 35000, 2.78028492e-05f, 2.75182774e-05f } } },
  6708. { "Demo songs/Sid De La Cruz - Secret Agent Rendezvous.flp", "Badious", 181, 19.0406926, 20.1997196, { { 5000, 0.00332317385f, 0.00785329007f }, { 12000, 0.0234184917f, -0.0034834384f }, { 25000, -0.0167671479f, -0.0142839653f }, { 35000, -0.0352748372f, -0.00481338287f } } },
  6709. };
  6710. int checked = 0;
  6711. for (const auto& g : goldens)
  6712. {
  6713. std::ifstream in (std::filesystem::path (realFlpDir) / "Demo projects" / g.file, std::ios::binary);
  6714. const std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  6715. if (bytes.empty())
  6716. continue;
  6717. const flp::Channel* ch = nullptr;
  6718. const auto project = flp::parseProject (bytes);
  6719. for (const auto& c : project.channels)
  6720. if (c.pluginName == "Kepler" && c.name == g.channel)
  6721. ch = &c;
  6722. CHECK (ch != nullptr);
  6723. if (ch == nullptr)
  6724. continue;
  6725. CHECK (ch->pluginState.size() == g.stateSize && lynx::keplerStateUsable (ch->pluginState));
  6726. const auto y = lynx::kepler::renderReference (ch->pluginState, dll, { { 0, 60, 1.0, 0.0, 0.0, 20000 }, { 8000, 67, 0.6, 0.0, 0.0, 26000 },
  6727. { 16000, 55, 0.95, 0.0, 0.0, 30000 } }, 44100.0, 39690);
  6728. double el = 0.0, er = 0.0, peak = 0.0;
  6729. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  6730. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1], peak = std::max ({ peak, (double) std::abs (y[i]), (double) std::abs (y[i + 1]) });
  6731. bool ok = y.size() == 2 * 39690 && std::abs (el - g.el) <= 1e-5 * g.el && std::abs (er - g.er) <= 1e-5 * g.er;
  6732. for (const auto& smp : g.s)
  6733. ok = ok && std::abs (y[2 * (std::size_t) smp.at] - smp.l) <= 1e-5 * peak && std::abs (y[2 * (std::size_t) smp.at + 1] - smp.r) <= 1e-5 * peak;
  6734. if (! ok)
  6735. std::printf (" Kepler %s / %s differs from FL's DLL: energy %.9g %.9g (DLL %.9g %.9g)\n", g.file, g.channel, el, er, g.el, g.er);
  6736. CHECK (ok);
  6737. ++checked;
  6738. }
  6739. std::printf (" Kepler: %d demo channels checked against FL's engine DLL\n", checked);
  6740. CHECK (checked == (int) std::size (goldens));
  6741. }
  6742. TEST (ogun_demo_channels)
  6743. {
  6744. // every Ogun channel of FL's demo projects (with its plugin effects) against the round-37 reference model, which is
  6745. // within −60 dB of FL's DLL on all of them: three notes (C5 1.0 0–20000, G5 0.6 8000–26000, G4 0.95 16000–30000),
  6746. // 39 690 samples at 44.1 kHz, at the state's quality and at the export quality 4. Fingerprint: energy per side and
  6747. // four samples (the engine matches the model within float rounding, −125 dB or better on the whole signal).
  6748. if (realFlpDir.empty())
  6749. return;
  6750. struct Sample { int at; float l, r; };
  6751. struct Golden { const char* file; int ordinal; std::size_t stateSize; bool export4; double el, er; Sample s[4]; };
  6752. const Golden goldens[] {
  6753. { "Demo songs/Astes - Bien Duro.flp", 0, 64728, false, 710.708198, 710.708198, { { 5000, 0.133627325f, 0.133627325f }, { 12000, -0.143534407f, -0.143534407f }, { 25000, 0.079226315f, 0.079226315f }, { 35000, -0.0128487358f, -0.0128487358f } } }, // 3.3.1 Metal Drums: Drum
  6754. { "Demo songs/Astes - Bien Duro.flp", 0, 64728, true, 794.506558, 794.506558, { { 5000, -0.0112881307f, -0.0112881307f }, { 12000, -0.068779245f, -0.068779245f }, { 25000, -0.0289028231f, -0.0289028231f }, { 35000, -0.0161623377f, -0.0161623377f } } }, // 3.3.1 Metal Drums: Drum
  6755. { "Demo songs/Astes - Bien Duro.flp", 1, 4261, false, 297.957631, 297.957631, { { 5000, -0.000330464332f, -0.000330464332f }, { 12000, -0.00156057056f, -0.00156057056f }, { 25000, -9.24126434e-05f, -9.24126434e-05f }, { 35000, 0.0f, 0.0f } } }, // 3.3.1 Metal Drums: Click
  6756. { "Demo songs/Astes - Bien Duro.flp", 1, 4261, true, 189.545711, 189.545711, { { 5000, 0.00102557975f, 0.00102557975f }, { 12000, -0.00522088073f, -0.00522088073f }, { 25000, 5.81103013e-05f, 5.81103013e-05f }, { 35000, 0.0f, 0.0f } } }, // 3.3.1 Metal Drums: Click
  6757. { "Demo songs/Astes - Bien Duro.flp", 2, 133836, false, 125.683557, 125.683557, { { 5000, 0.0176561493f, 0.0176561493f }, { 12000, 0.019205872f, 0.019205872f }, { 25000, 0.0046372693f, 0.0046372693f }, { 35000, -0.00165430119f, -0.00165430119f } } }, // 3.3.2 Tibetan
  6758. { "Demo songs/Astes - Bien Duro.flp", 2, 133836, true, 105.621798, 105.621798, { { 5000, 0.00897848886f, 0.00897848886f }, { 12000, -0.0171215143f, -0.0171215143f }, { 25000, -0.00821319036f, -0.00821319036f }, { 35000, -0.00245537539f, -0.00245537539f } } }, // 3.3.2 Tibetan
  6759. { "Demo songs/Astes - Bien Duro.flp", 3, 97085, false, 2066.75611, 2066.75611, { { 5000, -0.101155393f, -0.101155393f }, { 12000, 0.355391592f, 0.355391592f }, { 25000, 0.00365939317f, 0.00365939317f }, { 35000, -0.0191957876f, -0.0191957876f } } }, // 3.3.3 Chase
  6760. { "Demo songs/Astes - Bien Duro.flp", 3, 97085, true, 1667.28674, 1667.28674, { { 5000, -0.236701503f, -0.236701503f }, { 12000, -0.126374632f, -0.126374632f }, { 25000, 0.0533663221f, 0.0533663221f }, { 35000, -0.0100225089f, -0.0100225089f } } }, // 3.3.3 Chase
  6761. { "Demo songs/Edlan & Ella Noël - Song For You.flp", 0, 819, false, 551.263856, 371.038684, { { 5000, 0.0253486633f, 0.160556272f }, { 12000, 0.194370747f, -0.0359776132f }, { 25000, -0.00528950989f, 0.01478827f }, { 35000, 0.110768802f, -0.0237054806f } } }, // Bellish 11
  6762. { "Demo songs/Edlan & Ella Noël - Song For You.flp", 0, 819, true, 547.662092, 381.048456, { { 5000, 0.0401367024f, 0.0261611789f }, { 12000, -0.157383129f, -0.0282031279f }, { 25000, -0.000249146833f, 0.0388034321f }, { 35000, -0.105665907f, 0.0320193283f } } }, // Bellish 11
  6763. { "Demo songs/LollieVox & Lezina J - I'll Be Your Madonna.flp", 0, 105571, false, 833.288142, 909.880193, { { 5000, 0.0276863724f, 0.214677185f }, { 12000, 0.00827352796f, 0.141611829f }, { 25000, -0.160458148f, 0.0101522766f }, { 35000, -0.0157076102f, 0.00129696913f } } }, // High Chords
  6764. { "Demo songs/LollieVox & Lezina J - I'll Be Your Madonna.flp", 0, 105571, true, 928.749154, 967.940976, { { 5000, -0.0623336285f, -0.0959333256f }, { 12000, 0.0601816922f, -0.0748261437f }, { 25000, -0.0872222036f, -0.101655915f }, { 35000, -0.002591094f, 0.0107459351f } } }, // High Chords
  6765. { "Product demos/Electric Loop - The Ogun Dream.flp", 0, 813, false, 21576.8988, 21576.8988, { { 5000, 0.00724618789f, 0.00724618789f }, { 12000, 0.0337664448f, 0.0337664448f }, { 25000, -1.44279329e-05f, -1.44279329e-05f }, { 35000, 0.0f, 0.0f } } }, // Kick
  6766. { "Product demos/Electric Loop - The Ogun Dream.flp", 0, 813, true, 17857.5027, 17857.5027, { { 5000, -0.0114536202f, -0.0114536202f }, { 12000, -0.00848954543f, -0.00848954543f }, { 25000, 1.20320046e-05f, 1.20320046e-05f }, { 35000, 0.0f, 0.0f } } }, // Kick
  6767. { "Product demos/Electric Loop - The Ogun Dream.flp", 1, 789, false, 172.127165, 170.39981, { { 5000, -0.00470701791f, -0.000373305753f }, { 12000, 0.0417476855f, 0.0339940563f }, { 25000, 0.0204325765f, 0.0191226583f }, { 35000, -0.000425090868f, -0.000144533216f } } }, // Clap
  6768. { "Product demos/Electric Loop - The Ogun Dream.flp", 1, 789, true, 163.557022, 149.810459, { { 5000, 0.00246403809f, 0.0228729937f }, { 12000, 0.0378073864f, 0.0445459485f }, { 25000, -0.0242712852f, -0.0313444398f }, { 35000, -0.00112735468f, -0.00138637482f } } }, // Clap
  6769. { "Product demos/Electric Loop - The Ogun Dream.flp", 2, 635, false, 125.48103, 127.519836, { { 5000, -0.00389297446f, 0.00181007839f }, { 12000, -0.0543250106f, -0.0736973509f }, { 25000, -0.00448145531f, 0.000426211918f }, { 35000, 5.35470353e-05f, 4.34564136e-05f } } }, // High Hat
  6770. { "Product demos/Electric Loop - The Ogun Dream.flp", 2, 635, true, 245.916518, 254.34816, { { 5000, 0.00147765165f, 0.00267027458f }, { 12000, 0.0883170143f, 0.173417449f }, { 25000, -0.00118842872f, 0.000863731664f }, { 35000, -0.000298409053f, -9.04561821e-05f } } }, // High Hat
  6771. { "Product demos/Electric Loop - The Ogun Dream.flp", 3, 45647, false, 271.486026, 271.486026, { { 5000, -4.96093298e-06f, -4.96093298e-06f }, { 12000, 0.000292191253f, 0.000292191253f }, { 25000, -8.18665136e-10f, -8.18665136e-10f }, { 35000, 0.0f, 0.0f } } }, // HiHat
  6772. { "Product demos/Electric Loop - The Ogun Dream.flp", 3, 45647, true, 167.753037, 167.753037, { { 5000, 2.2048649e-05f, 2.2048649e-05f }, { 12000, -1.77328075e-05f, -1.77328075e-05f }, { 25000, -3.00809999e-09f, -3.00809999e-09f }, { 35000, 0.0f, 0.0f } } }, // HiHat
  6773. { "Product demos/Electric Loop - The Ogun Dream.flp", 4, 887, false, 6005.75168, 6293.90961, { { 5000, -0.19636783f, -0.0313744769f }, { 12000, 0.178472087f, 0.601032794f }, { 25000, -0.351912051f, -0.0497151017f }, { 35000, -0.182245821f, -0.181938142f } } }, // Snare
  6774. { "Product demos/Electric Loop - The Ogun Dream.flp", 4, 887, true, 6151.47444, 6049.47136, { { 5000, -0.102814399f, 0.0690701902f }, { 12000, 0.0902677849f, 0.114889771f }, { 25000, -0.173990116f, 0.140671447f }, { 35000, 0.0744562522f, 0.0291433111f } } }, // Snare
  6775. { "Product demos/Electric Loop - The Ogun Dream.flp", 5, 705, false, 6511.3723, 5580.42134, { { 5000, 0.478397369f, 0.536334693f }, { 12000, -0.312448859f, -0.301919997f }, { 25000, 0.260874003f, -0.0129742175f }, { 35000, 0.0223956257f, 0.033103168f } } }, // Swebass 1
  6776. { "Product demos/Electric Loop - The Ogun Dream.flp", 5, 705, true, 6415.0768, 5859.44881, { { 5000, 0.0342692807f, 0.00727743423f }, { 12000, -0.0324966274f, 0.125386745f }, { 25000, -0.0785191655f, -0.25017795f }, { 35000, -0.0524680056f, -0.0384391509f } } }, // Swebass 1
  6777. { "Product demos/Electric Loop - The Ogun Dream.flp", 6, 708, false, 2487.6041, 2190.17262, { { 5000, -0.0770967156f, -0.00940297544f }, { 12000, 0.329836905f, 0.183736563f }, { 25000, 0.0137981772f, -0.0334252715f }, { 35000, 0.0132095823f, 0.0277119074f } } }, // Swebass 2
  6778. { "Product demos/Electric Loop - The Ogun Dream.flp", 6, 708, true, 2519.26975, 2230.77243, { { 5000, -0.204216316f, -0.16732122f }, { 12000, 0.0762225091f, 0.0964485779f }, { 25000, -0.0833523944f, -0.132980049f }, { 35000, -0.015998248f, 0.0112942485f } } }, // Swebass 2
  6779. { "Product demos/Electric Loop - The Ogun Dream.flp", 7, 109827, false, 1326.11782, 1397.71125, { { 5000, 0.0306534693f, 0.026510708f }, { 12000, -0.139238164f, -0.104713492f }, { 25000, 0.106734768f, 0.0295313746f }, { 35000, 0.258899271f, 0.260215104f } } }, // Midnight Lady
  6780. { "Product demos/Electric Loop - The Ogun Dream.flp", 7, 109827, true, 1252.058, 1287.83169, { { 5000, -0.0980920121f, -0.108575724f }, { 12000, 0.131130368f, 0.176118359f }, { 25000, 0.0581845529f, 0.174699515f }, { 35000, -0.17453593f, 0.0978319347f } } }, // Midnight Lady
  6781. { "Product demos/Electric Loop - The Ogun Dream.flp", 8, 34816, false, 10793.0192, 11904.6549, { { 5000, 0.36459741f, -0.0714551434f }, { 12000, 0.565215111f, -0.205066338f }, { 25000, -0.652977228f, -0.432422459f }, { 35000, -0.284279644f, -0.122116163f } } }, // Supersaw
  6782. { "Product demos/Electric Loop - The Ogun Dream.flp", 8, 34816, true, 10132.5107, 11590.7617, { { 5000, 0.192168862f, 0.0883051828f }, { 12000, -0.590734482f, 0.512839496f }, { 25000, 0.682625771f, 0.287306368f }, { 35000, -0.00662239315f, 0.700808227f } } }, // Supersaw
  6783. { "Product demos/Electric Loop - The Ogun Dream.flp", 9, 94266, false, 826.69243, 1157.08939, { { 5000, 0.0348474756f, -0.168766886f }, { 12000, -0.0719366521f, 0.288826376f }, { 25000, 0.181558818f, 0.0424098521f }, { 35000, -0.321783781f, -0.0548994206f } } }, // Airguitar 3
  6784. { "Product demos/Electric Loop - The Ogun Dream.flp", 9, 94266, true, 830.517691, 772.62552, { { 5000, -0.208309069f, 0.152180359f }, { 12000, -0.264171928f, 0.163982511f }, { 25000, 0.0313629843f, -0.187507361f }, { 35000, 0.133838147f, 0.0704783946f } } }, // Airguitar 3
  6785. { "Product demos/Electric Loop - The Ogun Dream.flp", 10, 1004, false, 12678.7464, 11831.2032, { { 5000, 0.192205578f, 0.613839507f }, { 12000, 0.269521117f, -0.585603476f }, { 25000, 0.0944993123f, 0.0806781948f }, { 35000, -0.16715017f, 0.412193805f } } }, // Trumpet
  6786. { "Product demos/Electric Loop - The Ogun Dream.flp", 10, 1004, true, 12261.8266, 9962.482, { { 5000, 0.422391087f, -0.251674503f }, { 12000, -0.46900028f, -0.57652849f }, { 25000, -0.535878778f, 0.115671799f }, { 35000, -0.545713186f, 0.0383624472f } } }, // Trumpet
  6787. { "Product demos/Electric Loop - The Ogun Dream.flp", 11, 30605, false, 1358.26232, 1520.29691, { { 5000, -0.0350573957f, 0.0372484438f }, { 12000, 0.0963054374f, -0.130206674f }, { 25000, 0.0404771306f, -0.270439237f }, { 35000, -0.0870027244f, 0.0245792065f } } }, // Silverswirll
  6788. { "Product demos/Electric Loop - The Ogun Dream.flp", 11, 30605, true, 964.637209, 1040.25756, { { 5000, 0.111636952f, -0.00114471605f }, { 12000, 0.0483313873f, 0.0637735277f }, { 25000, -0.152803347f, 0.101604536f }, { 35000, 0.0288163591f, 0.0194431469f } } }, // Silverswirll
  6789. { "Product demos/Electric Loop - The Ogun Dream.flp", 12, 34708, false, 21463.1948, 22013.1021, { { 5000, -0.00810255855f, -0.0122786947f }, { 12000, -0.163549975f, -0.203571558f }, { 25000, 0.419101298f, -1.1141516f }, { 35000, 0.498232335f, -0.394363374f } } }, // Padsaw
  6790. { "Product demos/Electric Loop - The Ogun Dream.flp", 12, 34708, true, 22903.9209, 21556.3509, { { 5000, -0.00977799948f, -0.0114432834f }, { 12000, -0.0759382695f, 0.077038914f }, { 25000, -0.556497335f, -1.22787583f }, { 35000, -1.31076968f, -1.73392022f } } }, // Padsaw
  6791. { "Product demos/Electric Loop - The Ogun Dream.flp", 13, 6395, false, 3516.18148, 3565.45486, { { 5000, -0.0564712696f, -0.262329876f }, { 12000, 0.113444217f, 0.168150783f }, { 25000, -0.45024398f, -0.694699228f }, { 35000, 0.151807249f, -0.00405737339f } } }, // Turbular Bells 2
  6792. { "Product demos/Electric Loop - The Ogun Dream.flp", 13, 6395, true, 3571.34372, 3626.15163, { { 5000, -0.0878480822f, -0.275067806f }, { 12000, 0.230907321f, 0.28320539f }, { 25000, -0.470044613f, -0.701287091f }, { 35000, 0.195627496f, 0.0175700653f } } }, // Turbular Bells 2
  6793. };
  6794. std::map<std::string, std::vector<std::vector<std::uint8_t>>> states;
  6795. int checked = 0;
  6796. for (const auto& g : goldens)
  6797. {
  6798. if (! states.contains (g.file))
  6799. {
  6800. std::ifstream in (std::filesystem::path (realFlpDir) / "Demo projects" / g.file, std::ios::binary);
  6801. const std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  6802. auto& list = states[g.file];
  6803. if (! bytes.empty())
  6804. for (const auto& c : flp::parseProject (bytes).channels)
  6805. if (c.pluginName == "Ogun")
  6806. list.push_back (c.pluginState);
  6807. }
  6808. const auto& list = states[g.file];
  6809. if (list.empty())
  6810. continue; // not this installation
  6811. CHECK ((int) list.size() > g.ordinal);
  6812. if ((int) list.size() <= g.ordinal)
  6813. continue;
  6814. const auto& st = list[(std::size_t) g.ordinal];
  6815. CHECK (st.size() == g.stateSize);
  6816. lynx::ogun::RefOptions opt;
  6817. opt.quality = g.export4 ? 4 : -1;
  6818. const auto y = lynx::ogun::renderReference (st, { { 0, 60, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 20000 },
  6819. { 8000, 67, 0.6, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 26000 },
  6820. { 16000, 55, 0.95, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 30000 } }, 44100.0, 39690, opt);
  6821. double el = 0.0, er = 0.0, peak = 0.0;
  6822. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  6823. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1], peak = std::max ({ peak, (double) std::abs (y[i]), (double) std::abs (y[i + 1]) });
  6824. bool ok = y.size() == 2 * 39690 && std::abs (el - g.el) <= 1e-5 * g.el && std::abs (er - g.er) <= 1e-5 * g.er;
  6825. for (const auto& smp : g.s)
  6826. ok = ok && std::abs (y[2 * (std::size_t) smp.at] - smp.l) <= 2e-5 * peak && std::abs (y[2 * (std::size_t) smp.at + 1] - smp.r) <= 2e-5 * peak;
  6827. if (! ok)
  6828. std::printf (" Ogun %s #%d (q%s) differs from the model: energy %.9g %.9g (model %.9g %.9g)\n", g.file, g.ordinal,
  6829. g.export4 ? "4" : "-state", el, er, g.el, g.er);
  6830. CHECK (ok);
  6831. ++checked;
  6832. }
  6833. std::printf (" Ogun: %d demo renders checked against the reference model\n", checked);
  6834. CHECK (checked == (int) std::size (goldens));
  6835. }
  6836. TEST (harmless_rack)
  6837. {
  6838. // REA_rodada36: Harmless's effects rack against our reference model (rack.py / maxmodel.py, which match the
  6839. // installation's Harmless_x64.dll and SG_x64.dll), 44.1 kHz, host blocks of 512
  6840. std::vector<float> inL (8192), inR (8192);
  6841. for (int i = 0; i < 4096; ++i)
  6842. {
  6843. inL[(std::size_t) i] = (float) (1.5 * std::sin (2.0 * 3.141592653589793 * 440.0 * i / 44100.0) * std::min (1.0, i / 400.0));
  6844. inR[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / 44100.0));
  6845. }
  6846. auto run = [&] (const std::array<int, 100>& p, int compType, std::shared_ptr<const lynx::hlfx::MaximusPreset> comp, int total, int voicesUntil) {
  6847. lynx::hlfx::Rack rack;
  6848. std::array<int, 7> t {};
  6849. t[3] = compType;
  6850. rack.prepare (p, t, 44100.0, 120.0, std::move (comp));
  6851. std::vector<float> l (inL.begin(), inL.begin() + total), r (inR.begin(), inR.begin() + total);
  6852. for (int s = 0; s < total; s += 512)
  6853. rack.process (l.data() + s, r.data() + s, std::min (512, total - s), s < voicesUntil);
  6854. return std::make_pair (l, r);
  6855. };
  6856. {
  6857. // chorus type 3 (mix 400), ping-pong delay type 2 (vol 600, feedback 500, 1/4 beat), reverb type 4 (vol 700)
  6858. std::array<int, 100> p {};
  6859. p[90] = 3, p[91] = 400, p[92] = 2, p[93] = 600, p[94] = 500, p[95] = 48, p[96] = 4, p[97] = 700;
  6860. const auto [l, r] = run (p, 0, nullptr, 8192, 4096);
  6861. const std::tuple<int, float, float> golden[] { { 0, 0.0f, 0.0f }, { 500, -0.25248247f, 0.15141529f }, { 3000, -0.65128994f, -0.64854866f },
  6862. { 5000, -0.54308629f, -0.36827448f }, { 6000, -0.98378551f, -0.72790027f },
  6863. { 8191, 0.50599128f, -0.43637198f } };
  6864. for (const auto& [i, gl, gr] : golden)
  6865. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  6866. }
  6867. {
  6868. // the delay alone: an impulse comes back after round(time·fs·0.3125/bpm) samples × (11^0.6 − 1)/10, then × feedback
  6869. std::array<int, 100> p {};
  6870. p[92] = 1, p[93] = 600, p[94] = 500, p[95] = 48;
  6871. lynx::hlfx::Rack rack;
  6872. rack.prepare (p, {}, 44100.0, 120.0, nullptr);
  6873. std::vector<float> l (16384), r (16384);
  6874. l[0] = 1.0f;
  6875. for (int s = 0; s < 16384; s += 512)
  6876. rack.process (l.data() + s, r.data() + s, 512, s == 0);
  6877. const auto D = 5512; // 48·44100·0.3125/120 = 5512.5 → 5512 (round half to even)
  6878. const auto vol = (float) ((std::exp ((double) (600 * 0.001f) * (double) (float) std::log (11.0)) - 1.0) * (double) 0.1f);
  6879. CHECK (std::abs (l[(std::size_t) D] - vol) < 1e-7f && std::abs (l[(std::size_t) D - 1]) < 1e-12f);
  6880. // the feedback ramps from 0.5 toward 0.5: the second echo is vol·0.5
  6881. CHECK (std::abs (l[(std::size_t) (2 * D)] - vol * 0.5f) < 1e-7f && std::abs (r[(std::size_t) D]) < 1e-12f);
  6882. }
  6883. // the compressor: Maximus states of our own (no preset data), checked against SG_x64.dll by REA round 36
  6884. using Env = std::vector<std::array<double, 4>>;
  6885. const Env def { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 } };
  6886. auto band = [] (int mode, int pre = 1000, int post = 1000, int sep = 0, int sat = 0, int ceil = 1000, int att = 1115, int rel2 = 0,
  6887. int sus = 2882) { return std::array<int, 13> { mode, pre, post, sep, sat, ceil, att, rel2, 2, 5000, 3, sus, 0 }; };
  6888. {
  6889. // master brickwall at −0.22 dB only (bands off / compression off)
  6890. const auto st = maximusTestState ({ 19800, 1, 48065, 1, 1115, 1000, -1 }, { band (3), band (1), band (3), band (0) },
  6891. { def, def, def, def, def, def, def, def }, 0);
  6892. const auto preset = lynx::hlfx::makeMaximusPreset (st);
  6893. CHECK (preset != nullptr);
  6894. std::array<int, 100> p {};
  6895. p[98] = 1000;
  6896. const auto [l, r] = run (p, 1, preset, 4096, 4096);
  6897. // SG_x64.dll's own output (EXEC, REA_rodada40: bit-identical to the port)
  6898. const std::tuple<int, float, float> golden[] { { 100, 0.024997942f, 0.88166064f }, { 1000, 0.56971401f, -0.62808102f },
  6899. { 2047, -0.27541628f, 0.709135f }, { 3000, 0.32431623f, -0.37985197f },
  6900. { 4095, -0.12743522f, -0.15237485f } };
  6901. for (const auto& [i, gl, gr] : golden)
  6902. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  6903. float peak = 0.0f;
  6904. for (int i = 2048; i < 4096; ++i)
  6905. peak = std::max ({ peak, std::abs (l[(std::size_t) i]), std::abs (r[(std::size_t) i]) });
  6906. CHECK (peak < 0.9751f);
  6907. }
  6908. {
  6909. // 24 dB crossovers, ×2 oversampling, low cut, stereo separation, saturation A, a knee with tension (type 7) and
  6910. // exponential tensions (type 0), release 2 on the master, band mix 600
  6911. const Env knee { { 0.0, 0.0, 0.0, 0 }, { 0.35, 0.35, 0.0, 0 }, { 0.65, 0.6, 0.36, 7 } };
  6912. const Env t0 { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.14, 0 }, { 0.5, 0.5, 0.0, 0 } };
  6913. const Env m0 { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 }, { 0.5, 0.6, 0.085, 0 } };
  6914. const auto st = maximusTestState ({ 19800, 1, 48065, 1, 891, 600, 32768 },
  6915. { band (1, 1264, 1296, 500, -500, 1200), band (0, 1340, 1000, -348, 0, 1000, 1115, 0, 1570),
  6916. band (0, 1364, 1152, 0, 0, 1000, 1115, 0, 1186), band (0, 1000, 1000, 0, -196, 1000, 1115, 4464, 1538) },
  6917. { def, def, knee, def, t0, def, m0, def }, 1);
  6918. const auto preset = lynx::hlfx::makeMaximusPreset (st);
  6919. CHECK (preset != nullptr);
  6920. std::array<int, 100> p {};
  6921. p[98] = 600;
  6922. const auto [l, r] = run (p, 2, preset, 4096, 4096);
  6923. // SG_x64.dll's own output (EXEC, REA_rodada40; the port is within −142 dB of it)
  6924. const std::tuple<int, float, float> golden[] { { 100, 0.0f, 0.0f }, { 1000, -0.46856543f, -0.63262129f },
  6925. { 2047, 0.20268451f, 0.54145539f }, { 3000, -0.25613472f, -0.49985155f },
  6926. { 4095, 0.10172445f, 0.050564945f } };
  6927. for (const auto& [i, gl, gr] : golden)
  6928. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  6929. }
  6930. {
  6931. // an .fst wrapper around a state is found; unsupported states are rejected
  6932. const auto st = maximusTestState ({ 19800, 1, 48065, 1, 1115, 1000, -1 }, { band (3), band (1), band (3), band (0) },
  6933. { def, def, def, def, def, def, def, def }, 0);
  6934. std::vector<std::uint8_t> fst { 'F', 'L', 'h', 'd', 6, 0, 0, 0, 0, 0, 1, 0, 0x60, 0, 'F', 'L', 'd', 't' };
  6935. std::vector<std::uint8_t> body { 0xC9, 8, 'M', 'a', 'x', 'i', 'm', 'u', 's', 0, 0xD5 };
  6936. for (auto n = st.size(); ; n >>= 7)
  6937. {
  6938. body.push_back ((std::uint8_t) ((n & 0x7f) | (n >= 0x80 ? 0x80 : 0)));
  6939. if (n < 0x80)
  6940. break;
  6941. }
  6942. body.insert (body.end(), st.begin(), st.end());
  6943. const auto len = (std::uint32_t) body.size();
  6944. for (int k = 0; k < 4; ++k)
  6945. fst.push_back ((std::uint8_t) (len >> (8 * k)));
  6946. fst.insert (fst.end(), body.begin(), body.end());
  6947. const auto found = lynx::hlfx::maximusStateFromFst (fst);
  6948. CHECK (found && *found == st);
  6949. auto rms = st;
  6950. rms[4 + 4 * (7 + 13 * 3 + 12)] = 1; // RMS detector on the master (decoded in REA_rodada40)
  6951. CHECK (lynx::hlfx::makeMaximusPreset (rms) != nullptr);
  6952. auto masterMid = st;
  6953. masterMid[0x1fc] = 1; // "Master Mid mode": not decoded
  6954. CHECK (lynx::hlfx::makeMaximusPreset (masterMid) == nullptr);
  6955. }
  6956. }
  6957. TEST (fruity_dynamics_engines)
  6958. {
  6959. // REA_rodada48: Fruity Limiter / Fruity Compressor, states of our own against the installation's DLLs
  6960. // (goldens = the DLL's output for the same input, EXEC; blocks of 512 at 44.1 kHz)
  6961. std::vector<float> inL (4096), inR (4096), side (4096);
  6962. for (int i = 0; i < 4096; ++i)
  6963. {
  6964. inL[(std::size_t) i] = (float) (1.5 * std::sin (2.0 * 3.141592653589793 * 440.0 * i / 44100.0) * std::min (1.0, i / 400.0));
  6965. inR[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / 44100.0));
  6966. side[(std::size_t) i] = (float) (0.9 * std::sin (2.0 * 3.141592653589793 * 55.0 * i / 44100.0));
  6967. }
  6968. auto put = [] (std::vector<std::uint8_t>& b, std::int32_t v) {
  6969. std::uint8_t q[4];
  6970. std::memcpy (q, &v, 4);
  6971. b.insert (b.end(), q, q + 4);
  6972. };
  6973. auto limiterState = [&] (std::array<int, 18> p, int k = 0, int exact = 0) {
  6974. std::vector<std::uint8_t> b;
  6975. put (b, 7);
  6976. for (auto v : p)
  6977. put (b, v);
  6978. b.resize (0xA4, 0);
  6979. put (b, k);
  6980. b.push_back ((std::uint8_t) exact);
  6981. return b;
  6982. };
  6983. auto compressorState = [&] (int version, std::array<int, 6> p, int flags, int k = 0) {
  6984. std::vector<std::uint8_t> b;
  6985. put (b, version);
  6986. for (auto v : p)
  6987. put (b, v);
  6988. put (b, flags);
  6989. if (version > 2)
  6990. put (b, k);
  6991. return b;
  6992. };
  6993. struct Out { std::vector<float> l, r; int latency = 0; };
  6994. auto run = [&] (const lynx::EffectSlot& slot, bool sidechain) {
  6995. auto fx = lynx::makeEffect (slot);
  6996. fx->prepare (44100.0);
  6997. Out o { inL, inR };
  6998. for (int s = 0; s < 4096; s += 512)
  6999. {
  7000. if (sidechain)
  7001. fx->setSidechain (side.data() + s, side.data() + s);
  7002. fx->process (o.l.data() + s, o.r.data() + s, 512, slot.params, 120.0);
  7003. }
  7004. o.latency = fx->latencySamples();
  7005. return o;
  7006. };
  7007. auto check = [] (const Out& out, const std::vector<std::tuple<int, float, float>>& golden) {
  7008. for (const auto& [i, gl, gr] : golden)
  7009. CHECK (std::abs (out.l[(std::size_t) i] - gl) < 2e-6f && std::abs (out.r[(std::size_t) i] - gr) < 2e-6f);
  7010. };
  7011. auto limiter = [] (const std::vector<std::uint8_t>& st) {
  7012. auto slot = lynx::effectFromFl ("Fruity Limiter", st);
  7013. CHECK (slot && slot->kind == lynx::EffectKind::fruityLimiter);
  7014. return *slot;
  7015. };
  7016. // look-ahead limiter: +4 dB gain into a −2.5 dB ceiling, 2 ms attack (88 samples of latency); bit-identical
  7017. const auto a = run (limiter (limiterState ({ 1300, 1000, 800, 1115, 3, 5000, 3, 2882, 1000, 0, 0, 0, 6666, 1, 650, 1000, 0, 4187 })), false);
  7018. CHECK (a.latency == 88);
  7019. check (a, { { 100, 0.0176312141f, 0.621841073f }, { 1000, 0.401142687f, -0.442239583f }, { 2047, -0.19392401f, 0.499310732f },
  7020. { 3000, 0.22835508f, -0.267458469f }, { 4095, -0.089728713f, -0.107289009f } });
  7021. // compressor section with a positive knee, 2 compressor curve stages and soft saturation (port within −143 dB)
  7022. check (run (limiter (limiterState ({ 1000, 900, 1000, 0, 3, 5000, 3, 2882, 400, 600, 400, 1115, 6666, 2, 650, 1000, 0, 4187 })), false),
  7023. { { 100, -0.00208989787f, 0.0100310864f }, { 1000, -0.0545219742f, -0.0651496574f }, { 2047, 0.177322432f, -0.230846375f },
  7024. { 3000, -0.159136176f, -0.183688983f }, { 4095, -0.300143838f, 0.299418598f } });
  7025. // sidechained compressor (side input 1) with a negative knee, noise gate and the exact-ceiling option
  7026. check (run (limiter (limiterState ({ 1000, 1000, 1000, 0, 3, 3000, 5, 2882, 300, 1000, -500, 500, 5000, 1, 1000, 0, 250, 4187 }, 1, 1)), true),
  7027. { { 100, -0.00186592329f, 0.00895605329f }, { 1000, -0.0375732854f, -0.0448972471f }, { 2047, 0.124697573f, -0.162336946f },
  7028. { 3000, -0.111069582f, -0.128206298f }, { 4095, -0.218446419f, 0.21791859f } });
  7029. auto compressor = [] (const std::vector<std::uint8_t>& st) {
  7030. auto slot = lynx::effectFromFl ("Fruity Compressor", st);
  7031. CHECK (slot && slot->kind == lynx::EffectKind::fruityCompressor);
  7032. return *slot;
  7033. };
  7034. // Vintage type, look-ahead flag, side input 1: bit-identical
  7035. const auto ca = run (compressor (compressorState (3, { -200, 40, 30, 50, 300, 2 }, 1, 1)), true);
  7036. CHECK (ca.latency == 64);
  7037. check (ca, { { 100, 0.0795554966f, -0.230549783f }, { 1000, 0.483903229f, 0.0241768137f }, { 2047, -0.596430957f, -0.441769004f },
  7038. { 3000, 0.597724319f, -0.18129006f }, { 4095, 0.607042372f, 0.435067475f } });
  7039. // Soft/R, version 2 without the flag (the output is still 64 frames late, but not reported): bit-identical
  7040. const auto cb = run (compressor (compressorState (2, { -150, 80, 0, 150, 100, 7 }, 0)), false);
  7041. CHECK (cb.latency == 0);
  7042. check (cb, { { 100, 0.0813739002f, -0.235819489f }, { 1000, 0.264188439f, 0.0131994039f }, { 2047, -0.260424435f, -0.192893147f },
  7043. { 3000, 0.251500547f, -0.0762802213f }, { 4095, 0.252972007f, 0.181305125f } });
  7044. // the slot's parameters carry FL's raw controls exactly (and survive the 4-decimal project file)
  7045. const auto st = limiterState ({ 1261, 995, 1303, 1115, 3, 5000, 3, 2882, 264, 244, -590, 1115, 6666, 1, 650, 1000, 0, 4187 }, 1);
  7046. const auto parsed = lynx::fldyn::parseLimiter (st);
  7047. CHECK (parsed && parsed->sidechain == 1 && ! parsed->exactCeiling);
  7048. const auto slot = limiter (st);
  7049. for (int i = 0; i < lynx::fldyn::limiterControls; ++i)
  7050. {
  7051. const auto rounded = std::round (slot.params[(std::size_t) i] * 10000.0f) / 10000.0f;
  7052. CHECK (lynx::fldyn::raw (rounded, lynx::fldyn::limiterMin[(std::size_t) i], lynx::fldyn::limiterMax[(std::size_t) i]) == parsed->p[(std::size_t) i]);
  7053. }
  7054. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityLimiter, 3, slot.params[3], 120.0) == "2.0 ms");
  7055. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityCompressor, 5, 2.0f / 7.0f, 120.0) == "Vintage");
  7056. // unreadable states keep the old approximation
  7057. CHECK (lynx::effectFromFl ("Fruity Limiter", { 7, 0, 0, 0 }).has_value() == false
  7058. || lynx::effectFromFl ("Fruity Limiter", { 7, 0, 0, 0 })->kind != lynx::EffectKind::fruityLimiter);
  7059. CHECK (! lynx::fldyn::parseCompressor ({ 4, 0, 0, 0 }));
  7060. }
  7061. TEST (maximus_engine)
  7062. {
  7063. // REA_rodada40: the engine on its own (MaximusProcessor), states of our own against the installation's DLLs
  7064. // (goldens = the DLL's output for the same input, EXEC), and the containers of the mixer effect and Vocodex
  7065. std::vector<float> inL (4096), inR (4096);
  7066. for (int i = 0; i < 4096; ++i)
  7067. {
  7068. inL[(std::size_t) i] = (float) (1.5 * std::sin (2.0 * 3.141592653589793 * 440.0 * i / 44100.0) * std::min (1.0, i / 400.0));
  7069. inR[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / 44100.0));
  7070. }
  7071. auto run = [&] (std::shared_ptr<const lynx::hlfx::MaximusPreset> preset, int mix) {
  7072. lynx::hlfx::MaximusProcessor m;
  7073. m.prepare (std::move (preset), 44100.0, mix);
  7074. std::vector<float> l (inL), r (inR);
  7075. for (int s = 0; s < 4096; s += 512)
  7076. m.process (l.data() + s, r.data() + s, 512);
  7077. return std::make_pair (l, r);
  7078. };
  7079. using Env = std::vector<std::array<double, 4>>;
  7080. const Env def { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 } };
  7081. const Env knee { { 0.0, 0.0, 0.0, 0 }, { 0.35, 0.35, 0.0, 0 }, { 0.65, 0.6, 0.36, 7 } };
  7082. const Env t0 { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.14, 0 }, { 0.5, 0.5, 0.0, 0 } };
  7083. const Env m0 { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 }, { 0.5, 0.6, 0.085, 0 } };
  7084. auto band = [] (int mode, int pre = 1000, int post = 1000, int sep = 0, int sat = 0, int ceil = 1000, int att = 1115, int rel2 = 0,
  7085. int sus = 2882, int rms = 0) { return std::array<int, 13> { mode, pre, post, sep, sat, ceil, att, rel2, 2, 5000, 3, sus, rms }; };
  7086. auto check = [] (const std::pair<std::vector<float>, std::vector<float>>& out, const std::vector<std::tuple<int, float, float>>& golden) {
  7087. for (const auto& [i, gl, gr] : golden)
  7088. CHECK (std::abs (out.first[(std::size_t) i] - gl) < 2e-6f && std::abs (out.second[(std::size_t) i] - gr) < 2e-6f);
  7089. };
  7090. // RMS detectors, saturation mode B, a 5 Hz low cut, 12 dB high split (SG_x64.dll: port within −152 dB)
  7091. auto st3 = maximusTestState ({ 19800, 1, 48065, 0, 1115, 1000, 0 },
  7092. { band (0, 1500, 1000, 0, 400, 900, 1115, 0, 2882, 1), band (0, 1340, 1000, 0, 0, 1000, 1115, 0, 1570, 1),
  7093. band (1, 1000, 1200, 0, 1000, 1000), band (0, 1000, 1000, 0, 250, 1000, 1115, 4464, 2882, 1) },
  7094. { def, def, knee, def, t0, def, m0, def }, 0);
  7095. const auto p3 = lynx::hlfx::makeMaximusPreset (st3);
  7096. CHECK (p3 != nullptr);
  7097. check (run (p3, 1000), { { 100, 0.0f, 0.0f }, { 1000, 0.87061065f, 0.64004499f }, { 2047, -0.97266865f, 0.60940075f },
  7098. { 3000, 0.96658099f, 0.9250561f }, { 4095, 0.99094611f, -0.964059f } });
  7099. // the same with solo on (UI byte 0x1EF) and the low band selected (0x1E0): bit-identical to SG_x64.dll
  7100. auto solo = st3;
  7101. solo[0x1ef] = 1;
  7102. std::memset (solo.data() + 0x1e0, 0, 4);
  7103. check (run (lynx::hlfx::makeMaximusPreset (solo), 1000),
  7104. { { 100, 3.2922967e-06f, 0.00036916698f }, { 1000, 0.15653539f, -0.047913823f }, { 2047, -0.17336531f, 0.019099623f },
  7105. { 3000, 0.17185682f, -0.017173048f }, { 4095, 0.16714551f, 0.019473195f } });
  7106. // the legacy linear-phase FIR band split (UI byte 0x1FD; SG_x64.dll: port within −148 dB)
  7107. auto fir = st3;
  7108. fir[0x1fd] = 1;
  7109. check (run (lynx::hlfx::makeMaximusPreset (fir), 1000),
  7110. { { 100, 0.0f, 0.0f }, { 1000, 0.97986746f, 0.18555209f }, { 2047, -0.98930448f, -0.95970845f },
  7111. { 3000, 0.98856646f, -0.44592777f }, { 4095, 0.99069679f, 0.94382554f } });
  7112. // Soundgoodizer_x64.dll's own build with a v4 file (its presets' layout): a copy of the round-36 state without
  7113. // oversampling, through a byte-identical copy of the installation's DLL with this file as its preset (EXEC)
  7114. const auto st2 = maximusTestState ({ 19800, 1, 48065, 1, 891, 600, 32768 },
  7115. { band (1, 1264, 1296, 500, -500, 1200), band (0, 1340, 1000, -348, 0, 1000, 1115, 0, 1570),
  7116. band (0, 1364, 1152, 0, 0, 1000, 1115, 0, 1186), band (0, 1000, 1000, 0, -196, 1000, 1115, 4464, 1538) },
  7117. { def, def, knee, def, t0, def, m0, def }, 0);
  7118. std::vector<std::uint8_t> v4 { 4, 0, 0, 0 };
  7119. v4.insert (v4.end(), st2.begin() + 4, st2.begin() + 0x1f5);
  7120. v4.insert (v4.end(), st2.begin() + 0x1fe, st2.end());
  7121. const auto p4 = lynx::hlfx::makeMaximusPreset (v4, lynx::hlfx::MaximusBuild::soundgoodizer);
  7122. CHECK (p4 != nullptr);
  7123. check (run (p4, 600), { { 100, 0.0f, 0.0f }, { 1000, -0.60926896f, -0.6652832f }, { 2047, 0.40872875f, 0.36375371f },
  7124. { 3000, -0.41941205f, -0.61312866f }, { 4095, -0.12878418f, 0.27944946f } });
  7125. CHECK (lynx::hlfx::makeMaximusPreset (st2, lynx::hlfx::MaximusBuild::soundgoodizer) != nullptr); // v7: also read by that build
  7126. // the mixer effect's container and the effect itself
  7127. lynx::maximusfx::Blob blob;
  7128. blob.build = lynx::hlfx::MaximusBuild::soundgoodizer;
  7129. blob.mix = 600;
  7130. blob.state = v4;
  7131. const auto packed = lynx::maximusfx::pack (blob);
  7132. const auto back = lynx::maximusfx::unpack (packed);
  7133. CHECK (back && back->build == blob.build && back->mix == 600 && back->state == v4);
  7134. auto slot = lynx::makeEffectSlot (lynx::EffectKind::maximus);
  7135. slot.state = packed;
  7136. auto fx = lynx::makeEffect (slot);
  7137. fx->prepare (44100.0);
  7138. {
  7139. std::vector<float> l (inL), r (inR);
  7140. for (int s = 0; s < 4096; s += 1024) // longer host blocks are split into the engine's 512-frame calls
  7141. fx->process (l.data() + s, r.data() + s, 1024, slot.params, 120.0);
  7142. const auto ref = run (p4, 600);
  7143. CHECK (l == ref.first && r == ref.second);
  7144. }
  7145. const std::vector<std::uint8_t> sgState { 3, 0, 0, 0, 2, 0, 0, 0, 0x78, 2, 0, 0 };
  7146. const auto sg = lynx::maximusfx::parseSoundgoodizer (sgState);
  7147. CHECK (sg && sg->preset == 2 && sg->amount == 632 && std::string (lynx::maximusfx::soundgoodizerPresetFile (2)) == "c.dat");
  7148. CHECK (lynx::soundgoodizerPresetPath (sgState) == "Plugins\\Fruity\\Effects\\Soundgoodizer\\Data\\Presets\\c.dat");
  7149. CHECK (lynx::soundgoodizerExact (sgState, v4).has_value() && ! lynx::soundgoodizerExact (sgState, {}).has_value());
  7150. // Vocodex's appended installation data survives a round trip and leaves FL's state untouched
  7151. const std::vector<std::uint8_t> flState { 8, 0, 0, 0, 1, 2, 3 };
  7152. lynx::vocodex::InstallData data;
  7153. data.sgDat = st2;
  7154. data.carrierState = { 9, 9, 9 };
  7155. const auto joined = lynx::vocodex::appendInstallData (flState, data);
  7156. const auto [raw, files] = lynx::vocodex::splitInstallData (joined);
  7157. CHECK (raw == flState && files.sgDat == st2 && files.carrierState == data.carrierState);
  7158. CHECK (lynx::vocodex::splitInstallData (flState).first == flState);
  7159. }
  7160. // ---- Fruity Reeverb 2 (REA_rodada49) --------------------------------------------------------------------------------
  7161. // Goldens: the installation's Fruity Reeverb 2_x64.dll (unmodified, rate set before the state, flushed, blocks of 512)
  7162. // on our own states and input; LYNX's engine is bit-identical to it.
  7163. TEST (reeverb2_engine)
  7164. {
  7165. auto hex = [] (const char* h) {
  7166. std::vector<std::uint8_t> v;
  7167. for (std::size_t i = 0; h[i] != 0 && h[i + 1] != 0; i += 2)
  7168. v.push_back ((std::uint8_t) std::stoi (std::string (h + i, 2), nullptr, 16));
  7169. return v;
  7170. };
  7171. constexpr int N = 8192;
  7172. std::vector<float> inL (N), inR (N);
  7173. std::uint32_t seed = 12345;
  7174. for (int i = 0; i < N; ++i)
  7175. {
  7176. seed = (seed * 1103515245u + 12345u) & 0x7fffffffu;
  7177. const auto nz = (float) ((double) seed / 2147483648.0 - 0.5);
  7178. const auto tri = (float) ((i % 200) - 100) / 100.0f;
  7179. const float g = i < 4000 ? 1.0f : 0.0f;
  7180. inL[(std::size_t) i] = 0.5f * tri * g + (i >= 100 && i < 300 ? nz * 0.6f : 0.0f);
  7181. inR[(std::size_t) i] = 0.25f * tri * g;
  7182. }
  7183. struct Golden
  7184. {
  7185. int i;
  7186. float l, r;
  7187. };
  7188. auto run = [&] (const std::vector<std::uint8_t>& state, double bpm, const std::vector<Golden>& gold) {
  7189. const auto slot = lynx::reeverb2FromFl (state);
  7190. CHECK (slot.has_value() && slot->kind == lynx::EffectKind::reeverb2);
  7191. if (! slot)
  7192. return;
  7193. auto fx = lynx::makeEffect (*slot);
  7194. fx->prepare (44100.0);
  7195. fx->reset();
  7196. std::vector<float> l (inL), r (inR);
  7197. for (int s = 0; s < N; s += 512)
  7198. fx->process (l.data() + s, r.data() + s, 512, slot->params, bpm);
  7199. for (const auto& g : gold)
  7200. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  7201. };
  7202. // modulation, early reflections, negative separation, filters, predelay
  7203. run (hex ("112700004b000000780000001e0000004600000055000000190000003c0000009600000090010000e2ffffff6e000000640000008c00000084030000b00400000000"),
  7204. 120.0, { { 777, 0.263778746f, 0.131889373f }, { 1555, 0.187972173f, 0.0587911084f }, { 3333, 0.10398978f, 0.0566705838f },
  7205. { 5000, 0.087177977f, -0.0197417438f }, { 8191, 0.0769432634f, -0.282023013f } });
  7206. // Mid/Side input, tempo-synced predelay (27 ticks at 128 BPM), positive separation
  7207. run (hex ("112700004b000000280000001b00000032000000640000000f0000002800000064000000f401000028000000800000000000000040000000ac020000000000000101"),
  7208. 128.0, { { 777, 0.38499999f, 0.192499995f }, { 1555, 0.275000006f, 0.137500003f }, { 3333, 0.164420277f, 0.0826604739f },
  7209. { 5000, -0.00861243438f, -0.00140450173f }, { 8191, -0.00873835851f, 0.00888271444f } });
  7210. // version 10000 (no modulation controls), everything off or at its extreme, dry 0
  7211. run (hex ("1027000013000000dd000000000000006400000000000000c8000000dd00000014000000d00700004000000000000000a0000000a00000000000"),
  7212. 120.0, { { 777, 0.0f, 0.0f }, { 1555, 0.0f, 0.0f }, { 3333, 0.0632086769f, 0.0632086769f }, { 5000, -0.108072646f, -0.108072646f },
  7213. { 8191, 0.144686908f, 0.144686908f } });
  7214. // the parameters map back to FL's integers; unsupported states fall back to the approximation
  7215. lynx::reeverb2::Settings s;
  7216. s.p = { 3000, 5, 384, 1, 0, 200, 221, 20, 2000, -64, 160, 0, 160, 2048, 1 };
  7217. s.tempoPredelay = true;
  7218. lynx::EffectParams p {};
  7219. lynx::reeverb2::toParams (s, p);
  7220. CHECK (lynx::reeverb2::fromParams (p) == s);
  7221. CHECK (! lynx::reeverb2FromFl ({ 0x11, 0x27, 0, 0, 1, 0 }).has_value());
  7222. CHECK (! lynx::reeverb2FromFl ({ 0x12, 0x27, 0, 0 }).has_value());
  7223. const auto approx = lynx::effectFromFl ("Fruity Reeverb 2", { 0x11, 0x27, 0, 0 }, 120.0);
  7224. CHECK (! approx || approx->kind != lynx::EffectKind::reeverb2);
  7225. CHECK (lynx::reeverb2ParamText (0, 0.0f) == "off" && lynx::reeverb2ParamText (5, 1.0f) == "20.0 s");
  7226. // Fruity Reeverb (the first version): Fruity Reeverb_x64.dll goldens on our own states (denormals flushed, as the
  7227. // plugin runs and as LYNX renders)
  7228. lynx::ScopedFlushDenormals ftz;
  7229. auto run1 = [&] (const std::vector<std::uint8_t>& state, const std::vector<Golden>& gold) {
  7230. const auto slot = lynx::reeverb1FromFl (state);
  7231. CHECK (slot.has_value() && slot->kind == lynx::EffectKind::reeverb1);
  7232. if (! slot)
  7233. return;
  7234. auto fx = lynx::makeEffect (*slot);
  7235. fx->prepare (44100.0);
  7236. std::vector<float> l (inL), r (inR);
  7237. for (int s = 0; s < N; s += 512)
  7238. fx->process (l.data() + s, r.data() + s, 512, slot->params, 120.0);
  7239. for (const auto& g : gold)
  7240. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  7241. };
  7242. run1 (hex ("01000000204e0000204e000025000000409c000050c300000a00000030750000a861000050c3000060ea0000"),
  7243. { { 777, 0.293731689f, 0.146865845f }, { 1555, 0.20980835f, 0.104904175f }, { 3333, 0.0998796821f, 0.0754205957f },
  7244. { 5000, -0.0994476378f, 0.0116700521f }, { 8191, -0.141061321f, 0.0601170398f } });
  7245. run1 (hex ("0100000000000000000001000000000000000100000001002800000000000100000001000000000000000100"),
  7246. { { 777, -0.016678432f, -4.58686594e-31f }, { 1555, 0.0221031085f, 0.0230635051f }, { 3333, 0.0875115693f, 0.1695389f },
  7247. { 5000, -0.111421227f, -0.295867324f }, { 8191, 0.503498077f, 0.190456569f } });
  7248. CHECK (lynx::reeverb1FromFl (std::vector<std::uint8_t> (44, 0)).has_value());
  7249. CHECK (! lynx::reeverb1FromFl (std::vector<std::uint8_t> (40, 0)).has_value());
  7250. CHECK (lynx::reeverb1ParamText (5, 0.0f) == "Brighter" && lynx::reeverb1ParamText (0, 0.0f) == "off");
  7251. }
  7252. // ---- Fruity WaveShaper / Fruity Soft Clipper (REA_rodada51) ------------------------------------------------------------
  7253. // States written here from our own values (no FL preset data); goldens from the installation's DLLs (EXEC, blocks of 512)
  7254. namespace
  7255. {
  7256. std::vector<std::uint8_t> waveShaperTestState (int pre, int mix, int post, bool uni, bool center, int os, bool hq, bool smooth)
  7257. {
  7258. std::vector<std::uint8_t> b;
  7259. auto i32 = [&b] (std::int32_t v) { for (int k = 0; k < 4; ++k) b.push_back ((std::uint8_t) ((std::uint32_t) v >> (8 * k))); };
  7260. auto f64 = [&b] (double v) { std::uint64_t u; std::memcpy (&u, &v, 8); for (int k = 0; k < 8; ++k) b.push_back ((std::uint8_t) (u >> (8 * k))); };
  7261. auto f32 = [&b] (float v) { std::uint32_t u; std::memcpy (&u, &v, 4); for (int k = 0; k < 4; ++k) b.push_back ((std::uint8_t) (u >> (8 * k))); };
  7262. i32 (0x40003);
  7263. i32 (pre), i32 (mix), i32 (post);
  7264. b.push_back (uni ? 1 : 0), b.push_back (center ? 1 : 0);
  7265. i32 (os);
  7266. const struct { double dt, y; float t; int mode; } pts[] = { { 0.0, 0.1, 0.0f, 0 }, { 0.3, 0.8, 0.4f, 0 }, { 0.25, 0.35, 0.0f, 10 },
  7267. { 0.25, 0.9, -0.3f, 3 }, { 0.2, 1.0, 0.5f, 9 } };
  7268. i32 (3), i32 (3), i32 (5);
  7269. for (const auto& p : pts)
  7270. {
  7271. f64 (p.dt), f64 (p.y), f32 (p.t);
  7272. b.push_back ((std::uint8_t) p.mode), b.push_back (0), b.push_back (0), b.push_back (0);
  7273. }
  7274. i32 (0), i32 (-1), i32 (-1), i32 (-1), i32 (-1);
  7275. i32 (1);
  7276. b.push_back (hq ? 1 : 0), b.push_back (smooth ? 1 : 0);
  7277. return b;
  7278. }
  7279. } // namespace
  7280. TEST (fruity_waveshaper_and_soft_clipper)
  7281. {
  7282. std::vector<float> inL (4096), inR (4096);
  7283. for (int n = 0; n < 4096; ++n)
  7284. {
  7285. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  7286. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  7287. }
  7288. struct Gold { int i; float l, r; };
  7289. auto close = [] (float a, float b) { return std::abs (a - b) <= 2.0e-7f * std::max (1.0f, std::abs (b)); };
  7290. // Soft Clipper: bit-exact against Fruity Soft Clipper_x64.dll
  7291. auto clip = [&] (int thr, int post, std::initializer_list<Gold> gold) {
  7292. std::vector<float> l (inL), r (inR);
  7293. for (int s = 0; s < 4096; s += 512)
  7294. {
  7295. lynx::flshape::softClip (l.data() + s, 512, thr, post);
  7296. lynx::flshape::softClip (r.data() + s, 512, thr, post);
  7297. }
  7298. for (const auto& g : gold)
  7299. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  7300. };
  7301. clip (100, 128, { { 100, 0.0f, -0.949052989f }, { 1000, -0.99617976f, 0.872887492f }, { 2047, 0.649999976f, -0.944175363f },
  7302. { 3000, -0.99617976f, -0.699999988f }, { 4095, 0.25f, 0.600000024f } });
  7303. clip (1, 150, { { 100, 0.0f, -1.04588532f }, { 1000, -1.27003884f, 0.930835366f }, { 2047, 0.750244498f, -1.03539562f },
  7304. { 3000, -1.27003884f, -0.790091455f }, { 4095, 0.347655922f, 0.708338201f } });
  7305. clip (64, 140, { { 100, 0.0f, -1.08492827f }, { 1000, -1.21535313f, 0.990323186f }, { 2047, 0.804165959f, -1.0770781f },
  7306. { 3000, -1.21535313f, -0.849189103f }, { 4095, 0.319320858f, 0.754407823f } });
  7307. {
  7308. const std::vector<std::uint8_t> st { 64, 0, 0, 0, 140, 0, 0, 0 };
  7309. const auto slot = lynx::effectFromFl ("Fruity Soft Clipper", st, 120.0);
  7310. CHECK (slot && slot->kind == lynx::EffectKind::fruitySoftClipper);
  7311. if (slot)
  7312. {
  7313. const auto s = lynx::flshape::softClipperFromParams (slot->params.data());
  7314. CHECK (s.threshold == 64 && s.post == 140);
  7315. auto fx = lynx::makeEffect (*slot);
  7316. fx->prepare (44100.0);
  7317. std::vector<float> l (inL), r (inR);
  7318. fx->process (l.data(), r.data(), 4096, slot->params, 120.0);
  7319. CHECK (l[3000] == -1.21535313f && r[4095] == 0.754407823f);
  7320. }
  7321. }
  7322. // WaveShaper: tables (bipolar / unipolar) and the engine on three of our states, live and as FL renders
  7323. const auto stA = waveShaperTestState (150, 100, 120, false, true, 1, true, true); // CENTER, ×2, HQ, smooth
  7324. const auto stB = waveShaperTestState (100, 128, 128, true, false, 0, false, false); // unipolar
  7325. const auto stC = waveShaperTestState (192, 90, 100, false, false, 2, false, false); // ×4
  7326. const auto pA = lynx::flshape::parseWaveShaper (stA);
  7327. CHECK (pA && pA->points.size() == 5 && pA->pre == 150 && pA->center && pA->hq && pA->smooth && pA->oversampling == 1);
  7328. if (pA)
  7329. {
  7330. const auto T = lynx::flshape::shaperTable (pA->points, false);
  7331. CHECK (T.size() == 16386 && T[1] == 3277 && T[1001] == 18824 && T[5001] == 26195 && T[9001] == 11469 && T[12001] == 24614
  7332. && T[16384] == 32767 && T[0] == T[1]);
  7333. const auto U = lynx::flshape::shaperTable (pA->points, true);
  7334. CHECK (U.size() == 32770 && U[1] == -26214 && U[1001] == -6408 && U[5001] == 17073 && U[9001] == 19551 && U[12001] == 14549);
  7335. }
  7336. auto shape = [&] (const std::vector<std::uint8_t>& st, bool render, std::initializer_list<Gold> gold) {
  7337. const auto s = lynx::flshape::parseWaveShaper (st);
  7338. CHECK (s.has_value());
  7339. if (! s)
  7340. return;
  7341. lynx::flshape::WaveShaperEngine e;
  7342. e.setCurve (s->points);
  7343. e.prepare (44100.0);
  7344. e.setControls (s->pre, s->mix, s->post, s->oversampling, s->hq, s->smooth, s->center, s->unipolar, render);
  7345. std::vector<float> l (inL), r (inR);
  7346. for (int i = 0; i < 4096; i += 512)
  7347. e.process (l.data() + i, r.data() + i, 512);
  7348. for (const auto& g : gold)
  7349. CHECK (close (l[(std::size_t) g.i], g.l) && close (r[(std::size_t) g.i], g.r));
  7350. };
  7351. shape (stA, false, { { 100, 0.59108448f, 0.469315082f }, { 1000, 0.284353405f, 1.02621937f }, { 2047, -0.533613563f, 0.117038473f },
  7352. { 3000, 0.22673659f, -1.13572407f }, { 4095, -0.267153352f, 0.822866499f } });
  7353. shape (stA, true, { { 100, 0.794869065f, -0.127601087f }, { 1000, -0.00841622613f, 0.900729597f }, { 2047, 0.450603426f, -0.325943232f },
  7354. { 3000, -0.0530530401f, -0.32303831f }, { 4095, 1.0959363f, 0.946009099f } });
  7355. shape (stB, false, { { 100, -0.206451416f, 0.502471924f }, { 1000, -0.799987793f, 0.761169434f }, { 2047, 0.411743164f, 0.514160156f },
  7356. { 3000, -0.799987793f, 0.596221924f }, { 4095, -0.157653809f, 0.334106445f } });
  7357. shape (stB, true, { { 100, -0.206390381f, 0.502493739f }, { 1000, -0.799987793f, 0.761169434f }, { 2047, 0.411743164f, 0.514144361f },
  7358. { 3000, -0.799987793f, 0.596221924f }, { 4095, -0.157653809f, 0.334106445f } });
  7359. shape (stC, false, { { 100, 0.317797363f, 0.0483714938f }, { 1000, -0.445489585f, -0.533142388f }, { 2047, 0.634525597f, 0.942175806f },
  7360. { 3000, -0.445489585f, 0.514707327f }, { 4095, 0.200391173f, -0.937523127f } });
  7361. shape (stC, true, { { 100, 0.61165309f, -0.0993360579f }, { 1000, 0.0375693105f, 0.667849183f }, { 2047, 0.353716731f, -0.223758236f },
  7362. { 3000, 0.0375693105f, -0.223787636f }, { 4095, 0.843308687f, 0.688474894f } });
  7363. // the mixer effect from the FL import: controls as parameters, the curve in the slot's state; host blocks of any size
  7364. const auto slot = lynx::effectFromFl ("Fruity WaveShaper", stC, 120.0);
  7365. CHECK (slot && slot->kind == lynx::EffectKind::fruityWaveShaper && slot->state == stC);
  7366. if (slot)
  7367. {
  7368. auto fx = lynx::makeEffect (*slot);
  7369. fx->prepare (44100.0);
  7370. std::vector<float> l (inL), r (inR);
  7371. fx->process (l.data(), r.data(), 1024, slot->params, 120.0);
  7372. fx->process (l.data() + 1024, r.data() + 1024, 1024, slot->params, 120.0);
  7373. fx->process (l.data() + 2048, r.data() + 2048, 2048, slot->params, 120.0);
  7374. CHECK (close (l[1000], -0.445489585f) && close (r[2047], 0.942175806f) && close (l[4095], 0.200391173f));
  7375. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityWaveShaper, 3, slot->params[3], 120.0) == "x4");
  7376. }
  7377. // an unreadable state keeps the old approximation
  7378. CHECK (lynx::effectFromFl ("Fruity WaveShaper", { 1, 2, 3 }, 120.0)->kind == lynx::EffectKind::distortion);
  7379. CHECK (lynx::effectFromFl ("Fruity Soft Clipper", { 1, 2, 3 }, 120.0)->kind == lynx::EffectKind::distortion);
  7380. }
  7381. // ---- Sytrus (REA_rodada31) -------------------------------------------------------------------------------------------
  7382. // A Sytrus state built from scratch (version 25 layout, our own values — no FL preset data), zlib "stored" blocks.
  7383. namespace
  7384. {
  7385. struct SytrusTestPatch
  7386. {
  7387. std::vector<std::int32_t> main = std::vector<std::int32_t> (240, 0); // 962-byte main block as ints (+ mute bytes)
  7388. struct Op
  7389. {
  7390. int p[10] { 0, 256, 0, 0, 0, 0, 0, 0, 0, 0 };
  7391. int ratio = 20000, offset = 0, noise = 0;
  7392. std::vector<float> amps { 1.0f };
  7393. std::uint8_t flags[8] {};
  7394. int env1[3] { -1, -1, -1 }; // volume envelope: on (≠ −1 → on), decay point, sustain point
  7395. std::vector<std::pair<double, double>> env1pts;
  7396. } ops[6];
  7397. struct Filt
  7398. {
  7399. int p[10] { 0, 0, 128, 0, 128, 0, 0, 128, 128, 0 };
  7400. int type = 3;
  7401. } filts[3];
  7402. std::array<std::array<std::uint8_t, 23>, 3> delays {};
  7403. std::array<std::int32_t, 9> reverb { 19, 221, 0, 50, 80, 30, 221, 2, 0 };
  7404. bool reverbOn = false;
  7405. int eqmode = 0;
  7406. SytrusTestPatch()
  7407. {
  7408. main[0] = 128, main[1] = 128;
  7409. for (int i = 0; i < 8; ++i)
  7410. main[(std::size_t) (165 + i)] = (i % 4 == 2) ? 0 : 128;
  7411. main[173] = 1, main[175] = 128, main[188] = 128, main[189] = -1, main[190] = main[191] = 256;
  7412. }
  7413. void cell (int layer, int row, int col, int v) { main[(std::size_t) (3 + layer * 81 + row * 9 + col)] = v; }
  7414. std::vector<std::uint8_t> blob() const
  7415. {
  7416. std::vector<std::uint8_t> b;
  7417. auto i32 = [&] (std::int32_t v) { for (int k = 0; k < 4; ++k) b.push_back ((std::uint8_t) ((std::uint32_t) v >> (8 * k))); };
  7418. auto f32 = [&] (float v) { std::int32_t x; std::memcpy (&x, &v, 4); i32 (x); };
  7419. auto f64 = [&] (double v) { std::uint64_t x; std::memcpy (&x, &v, 8); for (int k = 0; k < 8; ++k) b.push_back ((std::uint8_t) (x >> (8 * k))); };
  7420. // point lists: kind 1 envelope, 2 LFO, 3 map / shaper curve; format version 3
  7421. auto env = [&] (int kind, const std::vector<std::pair<double, double>>& pts, int on, int decay, int sus) {
  7422. i32 (kind), i32 (3), i32 ((std::int32_t) pts.size());
  7423. for (const auto& [dt, y] : pts)
  7424. f64 (dt), f64 (y), f32 (0.0f), i32 (0);
  7425. i32 (0), i32 (on), i32 (decay), i32 (-1), i32 (sus);
  7426. };
  7427. for (int i = 0; i < 240; ++i)
  7428. if (4 * i + 4 <= 0x320)
  7429. i32 (main[(std::size_t) i]);
  7430. b.resize (962, 0); // mute bytes: none muted
  7431. i32 (0); // global declick amount
  7432. b.push_back (0), b.push_back (0), b.push_back (0); // OSC grouping, DC stage, GIBBS OFF
  7433. for (int k = 0; k < 9; ++k)
  7434. b.push_back (k == 7 ? 1 : 0); // flag group (release multiplier on, as Default.fst)
  7435. for (const auto& d : delays)
  7436. b.insert (b.end(), d.begin(), d.end());
  7437. for (auto v : reverb)
  7438. i32 (v);
  7439. b.push_back (reverbOn ? 1 : 0), b.push_back (0);
  7440. i32 (eqmode);
  7441. for (int k = 0; k < 3; ++k)
  7442. i32 (0), i32 (30000), i32 (20000), i32 (k);
  7443. b.push_back (0); // no info text
  7444. for (int u = 0; u < 10; ++u)
  7445. {
  7446. const int narts = u < 6 ? 6 : (u < 9 ? 8 : 2);
  7447. for (int j = 0; j < 10; ++j)
  7448. i32 (u < 6 ? ops[u].p[j] : (u < 9 ? filts[u - 6].p[j] : 0));
  7449. if (u < 6)
  7450. {
  7451. const auto& o = ops[u];
  7452. const auto at = b.size();
  7453. b.resize (at + 0x828, 0);
  7454. auto put = [&] (std::size_t off, const void* src, std::size_t n) { std::memcpy (b.data() + at + off, src, n); };
  7455. put (0, &o.ratio, 4), put (4, &o.offset, 4), put (8, &o.noise, 4);
  7456. put (0xc, o.amps.data(), 4 * o.amps.size());
  7457. const std::int32_t shape[5] { 0, 0, 0, 0, 128 };
  7458. put (0x80c, shape, sizeof shape);
  7459. put (0x820, o.flags, 8);
  7460. }
  7461. else if (u < 9)
  7462. {
  7463. const auto at = b.size();
  7464. b.resize (at + 0x30, 0);
  7465. std::memcpy (b.data() + at + 0x2c, &filts[u - 6].type, 4);
  7466. env (3, { { 0.0, 0.0 }, { 1.0, 1.0 } }, 0, -1, -1);
  7467. b.push_back (0);
  7468. }
  7469. for (int k = 0; k < narts; ++k)
  7470. {
  7471. const bool add = k == 0 || k == 3 || (u >= 6 && u <= 8 && (k == 2 || k == 3)) || (u < 6 && k == 4);
  7472. const double neutral = add ? 0.5 : 1.0;
  7473. i32 (3), i32 (0), f32 (1.0f), f32 (1.0f);
  7474. if (u < 6 && k == 1 && ops[u].env1[0] != -1)
  7475. env (1, ops[u].env1pts, -1, ops[u].env1[1], ops[u].env1[2]);
  7476. else
  7477. env (1, { { 0.0, 0.0 }, { 1.0, 1.0 } }, 0, -1, -1);
  7478. i32 (128), i32 (128), i32 (0), i32 (128);
  7479. env (2, { { 0.0, 1.0 }, { 1.0, 1.0 } }, 0, -1, -1);
  7480. i32 (40000), i32 (0), i32 (0), i32 (0), i32 (0);
  7481. for (int m = 0; m < 6; ++m)
  7482. env (3, { { 0.0, neutral }, { 1.0, neutral } }, 0, -1, -1);
  7483. }
  7484. }
  7485. b.push_back (0), b.push_back (0);
  7486. // zlib with stored blocks
  7487. std::vector<std::uint8_t> z { 0x78, 0x01 };
  7488. for (std::size_t at = 0; at < b.size(); at += 65535)
  7489. {
  7490. const auto n = std::min<std::size_t> (65535, b.size() - at);
  7491. z.push_back (at + n >= b.size() ? 1 : 0);
  7492. z.push_back ((std::uint8_t) n), z.push_back ((std::uint8_t) (n >> 8));
  7493. z.push_back ((std::uint8_t) ~n), z.push_back ((std::uint8_t) (~n >> 8));
  7494. z.insert (z.end(), b.begin() + (std::ptrdiff_t) at, b.begin() + (std::ptrdiff_t) (at + n));
  7495. }
  7496. std::uint32_t a = 1, c = 0;
  7497. for (auto x : b)
  7498. a = (a + x) % 65521, c = (c + a) % 65521;
  7499. for (int k = 3; k >= 0; --k)
  7500. z.push_back ((std::uint8_t) (((c << 16) | a) >> (8 * k)));
  7501. std::vector<std::uint8_t> out { 25, 0, 0, 0 };
  7502. out.insert (out.end(), z.begin(), z.end());
  7503. return out;
  7504. }
  7505. };
  7506. // renders like FL's Gen_Render calls: notes start/release at the block that contains them
  7507. std::pair<std::vector<float>, std::vector<float>> sytrusRender (const std::vector<std::uint8_t>& blob, bool renderMode, int n,
  7508. std::vector<std::array<int, 3>> notes, double fs = 44100.0)
  7509. {
  7510. lynx::sytrus::Engine e;
  7511. e.setTempo (120.0);
  7512. e.prepare (fs, renderMode, 512);
  7513. e.setPatch (lynx::sytrus::parsePatch (blob));
  7514. std::vector<float> l ((std::size_t) n), r ((std::size_t) n);
  7515. for (int n0 = 0; n0 < n; n0 += 512)
  7516. {
  7517. for (std::size_t k = 0; k < notes.size(); ++k)
  7518. {
  7519. if (notes[k][1] >= n0 && notes[k][1] < n0 + 512)
  7520. {
  7521. lynx::sytrus::NoteParams p;
  7522. p.key = (float) notes[k][0];
  7523. p.id = (std::int64_t) k + 1;
  7524. e.noteOn (p);
  7525. }
  7526. if (notes[k][2] >= n0 && notes[k][2] < n0 + 512)
  7527. e.release ((std::int64_t) k + 1);
  7528. }
  7529. e.render (l.data() + n0, r.data() + n0, std::min (512, n - n0), 1.0f, 0.0f);
  7530. }
  7531. return { l, r };
  7532. }
  7533. } // namespace
  7534. TEST (sytrus_engine_sine)
  7535. {
  7536. // one sine operator, ratio 1, OUT 256, no envelopes: C5 = 523.2511 Hz at 0.7071 (centre of the pan law, §6.1)
  7537. SytrusTestPatch t;
  7538. t.cell (0, 0, 8, 256);
  7539. const auto blob = t.blob();
  7540. CHECK (lynx::sytrus::stateUsable (blob));
  7541. CHECK (lynx::sytrus::parsePatch (blob) != nullptr);
  7542. auto broken = blob;
  7543. broken.resize (broken.size() / 2);
  7544. CHECK (! lynx::sytrus::stateUsable (broken));
  7545. const auto [l, r] = sytrusRender (blob, false, 8192, { { 60, 0, 4096 } });
  7546. double worst = 0.0;
  7547. for (int k = 0; k < 4000; ++k)
  7548. worst = std::max (worst, std::abs ((double) l[(std::size_t) k] - 0.70710678 * std::sin (2.0 * 3.14159265358979 * 523.251130601197 * k / 44100.0)));
  7549. CHECK (worst < 2e-5);
  7550. CHECK (l == r);
  7551. // envelope-less voice end (§4.9, §6.2): finished at a tick after the note-off, then the L/R gains (0.7071) ramp to 0
  7552. // at 0.001 per sample: the last non-zero sample is 4895 for a release at 4096 — the DLL's own figure (EXEC, §6.2)
  7553. int last = 0;
  7554. for (int k = 0; k < 8192; ++k)
  7555. if (l[(std::size_t) k] != 0.0f)
  7556. last = k;
  7557. CHECK (last == 4895);
  7558. // key 69 = 880 Hz; 48 kHz
  7559. const auto [l2, r2] = sytrusRender (blob, false, 4096, { { 69, 0, -1 } }, 48000.0);
  7560. worst = 0.0;
  7561. for (int k = 0; k < 4000; ++k)
  7562. worst = std::max (worst, std::abs ((double) l2[(std::size_t) k] - 0.70710678 * std::sin (2.0 * 3.14159265358979 * 880.0 * k / 48000.0)));
  7563. CHECK (worst < 5e-5);
  7564. (void) r2;
  7565. }
  7566. TEST (sytrus_engine_reference)
  7567. {
  7568. // FM 2→1, Vanilla LP filter, volume envelope, unison 3, delay and reverb, 2× oversampling with the 8-sample
  7569. // envelope tick (render settings): against our Python reference model of REA_rodada31 (sytmodel.render)
  7570. SytrusTestPatch t;
  7571. t.main[195] = 0, t.main[196] = 1, t.main[193] = 0, t.main[194] = 1;
  7572. t.main[173] = 3, t.main[174] = 64, t.main[176] = 100;
  7573. t.ops[1].ratio = 40000;
  7574. t.cell (0, 0, 1, 128); // op 2 → op 1 FM
  7575. t.cell (0, 0, 8, 128);
  7576. t.cell (0, 6, 0, 256); // op 1 → filter 1
  7577. t.cell (0, 6, 8, 256);
  7578. t.cell (0, 0, 7, 200); // FX send
  7579. t.filts[0].p[0] = 1, t.filts[0].p[2] = 180, t.filts[0].p[3] = 60;
  7580. t.ops[0].env1[0] = 0, t.ops[0].env1[1] = 1, t.ops[0].env1[2] = 2;
  7581. t.ops[0].env1pts = { { 0.0, 0.0 }, { 0.05, 1.0 }, { 0.1, 0.6 }, { 0.2, 0.0 } };
  7582. const std::int32_t d0[5] { 40, 100, 50, 100, 0 };
  7583. std::memcpy (t.delays[0].data(), d0, 20);
  7584. t.delays[0][22] = 1;
  7585. t.reverb = { 19, 221, 10, 50, 80, 30, 100, 2, 100 };
  7586. t.reverbOn = true;
  7587. const auto blob = t.blob();
  7588. const auto [l, r] = sytrusRender (blob, true, 12000, { { 60, 0, 6000 } });
  7589. // reference values (model render_mode=True, 44.1 kHz, block 512, key 60, velocity 0.78125, release at 6000); the same
  7590. // state rendered by FL's own Sytrus DLL in render mode differs from this by −136 dB (EXEC, REA round 31 harness)
  7591. const std::array<std::pair<int, float>, 8> golden { { { 100, -0.00235529384f }, { 777, -0.175126165f }, { 2000, 0.30976975f }, { 4321, -0.0213534199f },
  7592. { 6100, 0.202372357f }, { 7000, 0.249530748f }, { 9000, 0.0307101421f }, { 11999, 0.176390275f } } };
  7593. for (const auto& [i, v] : golden)
  7594. CHECK (std::abs (l[(std::size_t) i] - v) < 1e-6f);
  7595. const std::array<std::pair<int, float>, 3> goldenR { { { 777, -0.166890964f }, { 4321, -0.212358952f }, { 9000, -0.0251909085f } } };
  7596. for (const auto& [i, v] : goldenR)
  7597. CHECK (std::abs (r[(std::size_t) i] - v) < 1e-6f);
  7598. }
  7599. TEST (sytrus_instrument)
  7600. {
  7601. // the LYNX instrument: state in ChannelDef::pluginState, channel volume/pan recovered from the rack gains
  7602. SytrusTestPatch t;
  7603. t.cell (0, 0, 8, 256);
  7604. lynx::ChannelDef c;
  7605. c.instrument = lynx::InstrumentKind::sytrus;
  7606. c.pluginState = t.blob();
  7607. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sytrus);
  7608. inst->prepare (44100.0);
  7609. inst->configure (c, 120.0);
  7610. CHECK (inst->idle());
  7611. std::vector<float> l (4096), r (4096);
  7612. inst->noteOn (60, 100.0f / 128.0f);
  7613. // rack gains for volume V and pan p: √2·V·cos((1 + p)π/4), √2·V·sin(…); V = 0.5, p = 0 → Final.Vol 0.5
  7614. inst->render (l.data(), r.data(), 4096, 0.5f, 0.5f);
  7615. double peak = 0.0;
  7616. for (int k = 1000; k < 4096; ++k)
  7617. peak = std::max (peak, (double) std::abs (l[(std::size_t) k]));
  7618. CHECK (std::abs (peak - 0.5 * 0.70710678) < 1e-3); // Final.Vol 0.5 × the plugin's centre pan law
  7619. CHECK (! inst->idle());
  7620. inst->noteOff (60);
  7621. std::vector<float> tail (44100 * 3), tailR (tail.size());
  7622. inst->render (tail.data(), tailR.data(), (int) tail.size(), 0.5f, 0.5f);
  7623. CHECK (inst->idle());
  7624. CHECK (tail.back() == 0.0f);
  7625. }
  7626. TEST (harmor_effects_rack)
  7627. {
  7628. // REA_rodada38: the rack runs in the state's unit order and the records' FX send joins it at unit 4 — with the FX
  7629. // amount at 1000 every record output bypasses a reverb placed before unit 4 (EXEC: −600 dB vs the rack off)
  7630. auto blob = harmorTestState (false);
  7631. auto set = [&] (std::vector<std::uint8_t>& st, int part, int index, std::int32_t v) {
  7632. std::memcpy (st.data() + lynx::harmorParamOffset (st, part, index), &v, 4);
  7633. };
  7634. set (blob, 0, 10, 1000); // all to the FX send
  7635. auto render = [] (const std::vector<std::uint8_t>& st) {
  7636. return lynx::harmor::renderReference (st, { { 0, 60, 0.8, 1.0, 0.0, 6000 } }, 44100.0, 12000);
  7637. };
  7638. const auto dry = render (blob);
  7639. auto rev = blob;
  7640. set (rev, -1, 22, 1); // reverb on: room 50, diffusion 100, decay 2^3·0.1 s, high cut / damping off, wet 0.8
  7641. set (rev, -1, 24, 1050), set (rev, -1, 26, 50), set (rev, -1, 27, 100), set (rev, -1, 28, 300), set (rev, -1, 29, 1050);
  7642. set (rev, -1, 30, 2), set (rev, -1, 31, 800);
  7643. const auto bypassed = render (rev);
  7644. double eb = 0.0, db = 0.0;
  7645. for (std::size_t i = 0; i < dry.size(); ++i)
  7646. eb += (double) (bypassed[i] - dry[i]) * (bypassed[i] - dry[i]), db += (double) dry[i] * dry[i];
  7647. CHECK (eb < 1e-12 * db && db > 1e-3); // (float rounding of the rack's buffers only)
  7648. auto after = rev;
  7649. const std::int32_t order[6] { 0, 1, 2, 4, 3, 5 }; // the join before the reverb
  7650. for (int k = 0; k < 6; ++k)
  7651. set (after, -1, 53 + k, order[k]);
  7652. const auto wet = render (after);
  7653. double e = 0.0, d = 0.0;
  7654. bool finite = true;
  7655. for (std::size_t i = 0; i < wet.size(); ++i)
  7656. e += (double) (wet[i] - dry[i]) * (wet[i] - dry[i]), d += (double) dry[i] * dry[i], finite = finite && std::isfinite (wet[i]);
  7657. CHECK (finite && e > 1e-4 * d && d > 1e-3);
  7658. // a distortion (type 6) and a delay on the dry path change the sound, stay finite, and the instrument ends quiet
  7659. auto dist = harmorTestState (false);
  7660. {
  7661. const auto at = lynx::harmorParamOffset (dist, -1, 0) + 0xd4 + 0x18 + 48; // global misc +48: distortion type
  7662. const std::int32_t six = 6;
  7663. std::memcpy (dist.data() + at, &six, 4);
  7664. }
  7665. set (dist, -1, 17, 600), set (dist, -1, 19, 1000), set (dist, -1, 21, 1100);
  7666. set (dist, -1, 39, 1), set (dist, -1, 40, 2), set (dist, -1, 41, 400), set (dist, -1, 42, 96), set (dist, -1, 44, 800);
  7667. set (dist, -1, 47, 1050);
  7668. lynx::ChannelDef c;
  7669. c.instrument = lynx::InstrumentKind::harmor;
  7670. c.pluginState = dist;
  7671. auto inst = lynx::makeInstrument (lynx::InstrumentKind::harmor);
  7672. inst->configure (c, 120.0);
  7673. inst->prepare (44100.0);
  7674. std::vector<float> l (44100 * 4), r (l.size());
  7675. inst->noteOn (60, 0.8f);
  7676. double energy = 0.0;
  7677. for (int s = 0; s < (int) l.size(); s += 512)
  7678. {
  7679. if (s == 8192)
  7680. inst->noteOff (60);
  7681. inst->render (l.data() + s, r.data() + s, 512, 1.0f, 1.0f);
  7682. }
  7683. for (std::size_t i = 0; i < l.size(); ++i)
  7684. finite = finite && std::isfinite (l[i]) && std::isfinite (r[i]), energy += (double) l[i] * l[i];
  7685. CHECK (finite && energy > 1.0);
  7686. std::vector<float> tail (44100 * 6), tailR (tail.size());
  7687. for (int s = 0; s < (int) tail.size(); s += 512)
  7688. inst->render (tail.data() + s, tailR.data() + s, 512, 1.0f, 1.0f);
  7689. CHECK (inst->idle());
  7690. }
  7691. TEST (harmless_engine)
  7692. {
  7693. // REA_rodada32: state parsing, the additive engine against our reference model (hlmodel.py), host levels, voice end
  7694. std::vector<std::uint8_t> blob;
  7695. auto put = [&] (std::int32_t v) { for (int k = 0; k < 4; ++k) blob.push_back ((std::uint8_t) ((std::uint32_t) v >> (8 * k))); };
  7696. std::array<std::int32_t, 100> p {}; // the reference's clean patch: Saw, open filter, no modulation
  7697. for (int i = 11; i < 23; ++i)
  7698. p[(std::size_t) i] = 1000;
  7699. p[27] = 1000, p[28] = 250, p[31] = 1000, p[36] = 1000, p[37] = 1, p[47] = 1, p[49] = 500, p[51] = -300, p[54] = 8192;
  7700. p[56] = 500, p[65] = 1, p[66] = 128, p[67] = 128, p[68] = 64, p[89] = 3000, p[91] = 500, p[95] = 144, p[97] = 500, p[98] = 500;
  7701. // + resonance, pluck, 3-voice unison, tremolo, LFO on pitch
  7702. p[54] = 3500, p[59] = 600, p[31] = 500, p[65] = 3, p[67] = 150, p[25] = 300, p[86] = 0, p[42] = 200, p[89] = 5000;
  7703. put (12);
  7704. for (auto v : p)
  7705. put (v);
  7706. for (int i = 0; i < 7; ++i)
  7707. put (0);
  7708. put (524); // Custom colour table: 524 × (amplitude, phase)
  7709. for (int i = 0; i < 1048; ++i)
  7710. {
  7711. const auto f = i < 524 ? 1.0f / (float) (i + 1) : 0.25f;
  7712. std::uint32_t bits = 0;
  7713. std::memcpy (&bits, &f, 4);
  7714. put ((std::int32_t) bits);
  7715. }
  7716. const auto parsed = lynx::harmlessParams (blob);
  7717. CHECK (parsed && (int) parsed->size() == lynx::harmlessParamCount);
  7718. CHECK (parsed && (*parsed)[54] == 3500.0f && (*parsed)[99] == 0.0f && (*parsed)[lynx::harmlessCustomFlagIndex] == 1.0f);
  7719. CHECK (parsed && (*parsed)[lynx::harmlessCustomIndex + 1] == 0.5f && (*parsed)[lynx::harmlessCustomIndex + 524] == 0.25f);
  7720. auto old = blob;
  7721. old[0] = 3; // versions below 4 are not Harmless states we know
  7722. CHECK (! lynx::harmlessParams (old));
  7723. old = blob;
  7724. old.resize (300);
  7725. CHECK (! lynx::harmlessParams (old));
  7726. lynx::ChannelDef c;
  7727. c.instrument = lynx::InstrumentKind::harmless;
  7728. c.modelParams = *parsed;
  7729. c.modelParams[lynx::harmlessCustomFlagIndex] = 0.0f; // colour 0 (Saw): the custom table is not read
  7730. auto render = [] (const lynx::ChannelDef& def, int total, float gl, float gr, int offAt) {
  7731. auto inst = lynx::makeInstrument (lynx::InstrumentKind::harmless);
  7732. inst->configure (def, 120.0);
  7733. inst->prepare (44100.0);
  7734. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  7735. inst->noteOn (60, 0.78125f);
  7736. for (int s = 0; s < total; s += 512)
  7737. {
  7738. if (s == offAt)
  7739. inst->noteOff (60);
  7740. inst->render (l.data() + s, r.data() + s, std::min (512, total - s), gl, gr);
  7741. }
  7742. return std::make_tuple (l, r, std::move (inst));
  7743. };
  7744. {
  7745. // reference model (REA round 32 hlmodel.py, C5 at FL velocity 100, channel volume 1, centre, released at 6144)
  7746. auto [l, r, inst] = render (c, 8192, 1.0f, 1.0f, 6144);
  7747. const std::tuple<int, float, float> golden[] {
  7748. { 0, 0.0f, 0.0f }, { 10, -0.02220994f, -0.06232700f }, { 100, 0.40623373f, 0.17308202f }, { 1000, -0.04091963f, -0.26821208f },
  7749. { 3000, 0.14519004f, 0.11027778f }, { 6000, 0.50411958f, 0.27901855f }, { 8191, 0.02117692f, -0.01466602f } };
  7750. for (const auto& [i, gl, gr] : golden)
  7751. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  7752. // after the release the record fades out (below 1e-4, then 30 ms) and the instrument goes idle
  7753. std::vector<float> tail (44100), tailR (tail.size());
  7754. inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
  7755. CHECK (inst->idle());
  7756. }
  7757. // FL's level law applied once: half the channel volume halves the output; full right pan silences the left side
  7758. const auto full = std::get<0> (render (c, 2048, 1.0f, 1.0f, -1));
  7759. const auto half = std::get<0> (render (c, 2048, 0.5f, 0.5f, -1));
  7760. CHECK (std::abs (half[100] - 0.5f * full[100]) < 1e-6f && std::abs (full[100]) > 0.1f);
  7761. auto mono = c;
  7762. mono.modelParams[25] = 0.0f; // no tremolo, no unison pan spread
  7763. mono.modelParams[65] = 1.0f;
  7764. const auto [pl, pr, pi] = render (mono, 2048, 0.0f, 1.41421356f, -1);
  7765. double el = 0.0, er = 0.0;
  7766. for (int i = 0; i < 2048; ++i)
  7767. el += (double) pl[(std::size_t) i] * pl[(std::size_t) i], er += (double) pr[(std::size_t) i] * pr[(std::size_t) i];
  7768. CHECK (el < 1e-10 * er && er > 1.0);
  7769. }
  7770. TEST (startup_sound)
  7771. {
  7772. // the splash's sound: the right length, finite, at its peak level, quiet before the whoosh swells,
  7773. // a landing "pop" at impactTime and a chime ringing after it
  7774. using namespace lynx::startup;
  7775. const auto sound = makeSound (48000.0);
  7776. CHECK (sound.left.size() == (std::size_t) (soundLength * 48000.0) && sound.right.size() == sound.left.size());
  7777. float peak = 0.0f;
  7778. bool finite = true;
  7779. for (std::size_t i = 0; i < sound.left.size(); ++i)
  7780. {
  7781. finite = finite && std::isfinite (sound.left[i]) && std::isfinite (sound.right[i]);
  7782. peak = std::max ({ peak, std::abs (sound.left[i]), std::abs (sound.right[i]) });
  7783. }
  7784. CHECK (finite && std::abs (peak - peakLevel) < 1.0e-4f);
  7785. auto rms = [&sound] (double from, double to) {
  7786. double e = 0.0;
  7787. const auto a = (std::size_t) (from * 48000.0), b = (std::size_t) (to * 48000.0);
  7788. for (auto i = a; i < b; ++i)
  7789. e += (double) sound.left[i] * sound.left[i];
  7790. return std::sqrt (e / (double) (b - a));
  7791. };
  7792. CHECK (rms (0.0, 0.05) < 0.2 * rms (impactTime - 0.05, impactTime)); // the whoosh swells
  7793. CHECK (rms (impactTime, impactTime + 0.05) > rms (impactTime - 0.05, impactTime)); // the landing
  7794. CHECK (rms (0.8, 1.0) > 0.005 && rms (soundLength - 0.02, soundLength) < rms (0.8, 1.0)); // chime rings, tail fades
  7795. CHECK (makeSound (48000.0).left == sound.left); // the same sound on every start
  7796. }
  7797. namespace
  7798. {
  7799. // little-endian byte writer for the state builders below
  7800. struct Bytes
  7801. {
  7802. std::vector<std::uint8_t> b;
  7803. void raw (const char* s, std::size_t n) { b.insert (b.end(), s, s + n); }
  7804. void i32 (std::int32_t v) { b.insert (b.end(), reinterpret_cast<const std::uint8_t*> (&v), reinterpret_cast<const std::uint8_t*> (&v) + 4); }
  7805. void f32 (float v) { b.insert (b.end(), reinterpret_cast<const std::uint8_t*> (&v), reinterpret_cast<const std::uint8_t*> (&v) + 4); }
  7806. void zeros (std::size_t n) { b.insert (b.end(), n, 0); }
  7807. void bytes (const std::vector<std::uint8_t>& o) { b.insert (b.end(), o.begin(), o.end()); }
  7808. };
  7809. // A GMS state written from scratch (REA_rodada12 §1.1 layout, "Syn4"): oscillator waves that don't exist (silent
  7810. // tables, so the result does not depend on an FL installation), noise through the ladder with envelope and LFO,
  7811. // unison, slide, distortion, delay, reverb and the output EQ.
  7812. std::vector<std::uint8_t> gmsTestState()
  7813. {
  7814. std::array<float, 212> P {};
  7815. for (int i : { 1, 2, 44, 55, 56, 57, 58, 59, 60, 207, 208, 209, 210, 211 })
  7816. P[(std::size_t) i] = 0.5f;
  7817. P[0] = 0.75f, P[4] = 0.1f, P[5] = 0.4f, P[6] = 0.6f, P[7] = 0.3f, P[8] = 0.8f;
  7818. P[10] = 0.2f, P[11] = 1.0f, P[12] = 0.45f, P[13] = 0.55f, P[16] = 0.6f, P[20] = 0.2f, P[22] = 0.5f;
  7819. P[28] = 0.5f, P[34] = 0.4f, P[41] = 0.2f, P[42] = 0.6f, P[43] = 0.5f, P[45] = 0.8f, P[199] = 1.0f, P[198] = 0.8f, P[200] = 1.0f;
  7820. P[147] = 1.0f, P[117] = 0.3f, P[127] = 0.6f; // distortion
  7821. P[153] = 1.0f, P[123] = 0.5f, P[133] = 0.4f, P[143] = 0.4f; // delay (1/2 beat)
  7822. P[154] = 1.0f, P[124] = 0.6f, P[134] = 0.5f; // reverb
  7823. P[206] = 1.0f, P[207] = 0.7f, P[211] = 0.3f; // output EQ
  7824. Bytes preset;
  7825. preset.raw ("4nyS", 4);
  7826. for (const char* path : { "NoSuchDir\\NoWave A.wav", "NoSuchDir\\NoWave B.wav", "NoSuchDir\\NoWave C.wav", "", "", "", "" })
  7827. {
  7828. const auto n = std::strlen (path);
  7829. preset.raw (path, n);
  7830. preset.zeros (512 - n);
  7831. }
  7832. preset.i32 (0), preset.i32 (0), preset.i32 (0);
  7833. for (auto v : P)
  7834. preset.f32 (v);
  7835. preset.raw ("LYNX test", 9);
  7836. preset.zeros (512 - 9);
  7837. Bytes s;
  7838. s.raw ("SSLF", 4);
  7839. s.i32 ((std::int32_t) preset.b.size());
  7840. s.bytes (preset.b);
  7841. return s.b;
  7842. }
  7843. // A Morphine state written from scratch (REA_rodada16 §3 / REA_rodada39 §2.2 layout): generators A and B with a few
  7844. // frames each (A loops, PWM on A), C and D silent, a morph line A→B, four envelopes (Eg1 → pitch and velocity →
  7845. // amplitude through the matrix), the master ADSR and every plugin effect on.
  7846. std::vector<std::uint8_t> morphineTestState()
  7847. {
  7848. auto mbp = [] (int seed) {
  7849. Bytes b;
  7850. b.raw ("0PBM", 4);
  7851. for (int a = 0; a < 3; ++a)
  7852. for (int byte = 0; byte < 16; ++byte)
  7853. {
  7854. b.b.push_back (0xff);
  7855. for (int bit = 0; bit < 8; ++bit)
  7856. {
  7857. const auto k = byte * 8 + bit;
  7858. const float v = a == 0 ? std::cbrt (1.0f / (float) (k + 1 + seed * (k % 3))) * (k < 40 ? 1.0f : 0.2f)
  7859. : a == 1 ? 0.1f * (float) ((k * 7 + seed) % 11 - 5) / 5.0f
  7860. : 0.02f * (float) ((k * 5 + seed) % 7 - 3);
  7861. b.f32 (v);
  7862. }
  7863. }
  7864. return b.b;
  7865. };
  7866. auto msp = [&] (int frames, int seed, int loopMode) {
  7867. Bytes b;
  7868. b.raw ("0PSM", 4);
  7869. b.i32 (frames);
  7870. for (int f = 0; f < frames; ++f)
  7871. {
  7872. const auto m = mbp (seed + f);
  7873. b.i32 (20 + 7 * f), b.f32 (1.0f - 0.2f * (float) f), b.i32 ((std::int32_t) m.size());
  7874. b.bytes (m);
  7875. }
  7876. b.i32 (0x424);
  7877. b.i32 (10), b.i32 (40), b.i32 (0), b.i32 (loopMode);
  7878. b.f32 (0.45f), b.f32 (0.6f), b.f32 (0.5f), b.i32 (0);
  7879. for (int k = 0; k < 128; ++k)
  7880. b.f32 (0.3f);
  7881. for (int k = 0; k < 128; ++k)
  7882. b.f32 (1.0f);
  7883. return b.b;
  7884. };
  7885. auto mvc = [&] (int frames, int seed, int pwmMode) {
  7886. Bytes z;
  7887. z.raw ("0HZM", 4);
  7888. z.i32 (1);
  7889. const auto s = msp (frames, seed, 1);
  7890. z.i32 (128), z.f32 (frames > 1 ? 0.9f : 0.0f), z.i32 ((std::int32_t) s.size());
  7891. z.bytes (s);
  7892. Bytes b;
  7893. b.raw ("0CVM", 4);
  7894. b.i32 ((std::int32_t) z.b.size());
  7895. b.bytes (z.b);
  7896. b.i32 (28), b.f32 (0.4f), b.i32 (pwmMode), b.f32 (0.3f), b.i32 (0), b.f32 (0.5f), b.f32 (0.6f);
  7897. return b.b;
  7898. };
  7899. Bytes p;
  7900. p.raw ("0roM", 4);
  7901. p.raw ("0RPM", 4);
  7902. for (const auto& g : { mvc (3, 1, 2), mvc (2, 7, 0), mvc (1, 3, 0), mvc (1, 5, 0) })
  7903. {
  7904. p.i32 ((std::int32_t) g.size());
  7905. p.bytes (g);
  7906. }
  7907. {
  7908. Bytes m;
  7909. m.raw ("0LMM", 4);
  7910. m.i32 (2), m.i32 (12);
  7911. m.f32 (0.0f), m.f32 (1.0f), m.i32 (60);
  7912. m.f32 (1.0f), m.f32 (1.0f), m.i32 (60);
  7913. m.i32 (0x54), m.i32 (0), m.i32 (0), m.i32 (0), m.i32 (0);
  7914. for (float v : { 1.0f, 0.8f, 0.0f, 0.0f, 0.5f, 0.4f, 0.5f, 0.5f })
  7915. m.f32 (v);
  7916. for (int k = 0; k < 4; ++k)
  7917. m.i32 (0);
  7918. m.f32 (0.5f), m.f32 (0.5f), m.f32 (0.5f), m.i32 (0);
  7919. p.i32 ((std::int32_t) m.b.size());
  7920. p.bytes (m.b);
  7921. }
  7922. {
  7923. Bytes d;
  7924. d.raw ("0DMM", 4);
  7925. d.i32 (4);
  7926. for (int e = 0; e < 4; ++e)
  7927. {
  7928. Bytes g;
  7929. g.raw ("0GEM", 4);
  7930. g.i32 (3), g.i32 (8);
  7931. g.f32 (0.0f), g.i32 (10), g.f32 (1.0f), g.i32 (40 + 10 * e), g.f32 (0.3f), g.i32 (50);
  7932. g.i32 (0x2c), g.i32 (0), g.i32 (0), g.i32 (0), g.f32 (0.5f), g.i32 (0), g.f32 (0.45f), g.i32 (0), g.f32 (0.5f), g.f32 (0.6f),
  7933. g.i32 (e == 0 ? 1 : 0);
  7934. d.i32 ((std::int32_t) g.b.size());
  7935. d.bytes (g.b);
  7936. }
  7937. d.i32 (0x94);
  7938. const int src[12] = { 5, 2 }, dst[12] = { 3, 1 };
  7939. const float amt[12] = { 0.56f, 0.7f };
  7940. for (int k = 0; k < 12; ++k)
  7941. d.i32 (src[k]);
  7942. for (int k = 0; k < 12; ++k)
  7943. d.i32 (dst[k]);
  7944. for (int k = 0; k < 12; ++k)
  7945. d.f32 (amt[k]);
  7946. p.i32 ((std::int32_t) d.b.size());
  7947. p.bytes (d.b);
  7948. }
  7949. {
  7950. const float A[40] = { 0.5f, 0.0f, 0.0f, -1.0f, 0.2f, 0.5f, 0.55f, 0.1f, 0.17f, 0.2f, // master … velocity curve, ADSR A
  7951. 0.5f, 0.7f, 0.4f, 0.5f, 0.5f, 0.3f, 1.0f, 0.4f, 0.6f, 0.3f, // D S R, chorus, delay …
  7952. 0.4f, 0.3f, 1.0f, 0.5f, 0.5f, 0.4f, 1.0f, 0.6f, 0.4f, 0.5f, // … delay, reverb, EQ …
  7953. 0.5f, 0.5f, 0.55f, 0.5f, 0.45f, 1.0f, 0.0f, 0.5f, 1.0f, 0.0f }; // EQ on, noise (muted)
  7954. Bytes g;
  7955. g.i32 (0x5c4);
  7956. for (auto v : A)
  7957. g.f32 (v);
  7958. g.zeros (0x5c4 - g.b.size());
  7959. p.bytes (g.b);
  7960. }
  7961. Bytes s;
  7962. s.raw ("SSLF", 4);
  7963. s.i32 ((std::int32_t) p.b.size());
  7964. s.bytes (p.b);
  7965. return s.b;
  7966. }
  7967. void dumpIfAsked (const char* var, const std::vector<std::uint8_t>& blob)
  7968. {
  7969. if (const char* path = std::getenv (var))
  7970. {
  7971. std::ofstream out (path, std::ios::binary);
  7972. out.write (reinterpret_cast<const char*> (blob.data()), (std::streamsize) blob.size());
  7973. }
  7974. }
  7975. } // namespace
  7976. TEST (dx10_engine)
  7977. {
  7978. // REA_rodada42: a Fruity DX10 state written from scratch (version 1: 22 raw parameters) against our reference model
  7979. // (dxmodel.py + dxproc.py, bit-identical to the installation's DLL), the channel processor, host levels and the
  7980. // instrument front-end
  7981. const std::int32_t P[22] = { 1000, 40000, 30000, 30000, 40000, 30000, 35000, 8000, 40000, 50000, 20000,
  7982. 25000, 15000, 30000, 20000, 10000, 20000, 30000, 5000, 30000, 20000, -1 };
  7983. std::vector<std::uint8_t> blob (4 + 22 * 4, 0);
  7984. blob[0] = 1;
  7985. std::memcpy (blob.data() + 4, P, sizeof (P));
  7986. CHECK (lynx::dx10StateUsable (blob) && lynx::dx10ProcessorNeutral (blob));
  7987. CHECK (lynx::dx10Param (blob, 21) == -1 && lynx::dx10Param (blob, 3) == 30000 && ! lynx::dx10Param (blob, 22));
  7988. CHECK (! lynx::dx10StateUsable (std::vector<std::uint8_t> (40, 0)));
  7989. {
  7990. auto bad = blob;
  7991. bad[0] = 3; // versions ≥ 3 are ignored by the plugin
  7992. CHECK (! lynx::dx10StateUsable (bad));
  7993. }
  7994. using N = lynx::dx10::RefNote;
  7995. const std::vector<N> notes { { 0, 60, 1.0, 0.8, 0.3, 0.0, 6144 }, { 2048, 67, 0.4, 1.0, -0.5, 25.0, 8192 }, { 3000, 40, 2.06, 0.6, 1.0, 0.0, -1 } };
  7996. struct Golden { std::vector<std::uint8_t> state; lynx::dx10::RefOptions opt; int rate; double el, er; std::tuple<int, float, float> at[6]; };
  7997. auto check = [&] (const Golden& g) {
  7998. const auto y = lynx::dx10::renderReference (g.state, notes, (double) g.rate, 16384, g.opt);
  7999. CHECK (y.size() == 2 * 16384);
  8000. if (y.size() != 2 * 16384)
  8001. return y;
  8002. double el = 0.0, er = 0.0;
  8003. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  8004. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  8005. CHECK (std::abs (el - g.el) <= 1e-7 * g.el && std::abs (er - g.er) <= 1e-7 * g.er);
  8006. for (const auto& [i, l, r] : g.at)
  8007. CHECK (std::abs (y[2 * (std::size_t) i] - l) <= 1e-7f * std::max (1e-16f, std::abs (l))
  8008. && std::abs (y[2 * (std::size_t) i + 1] - r) <= 1e-7f * std::max (1e-16f, std::abs (r)));
  8009. return y;
  8010. };
  8011. lynx::dx10::RefOptions plain, odd;
  8012. odd.block = 333, odd.maxPoly = 1;
  8013. check ({ blob, plain, 44100, 672.293115, 778.393071,
  8014. { { 100, -0.0943799689f, -0.154014125f }, { 1000, -0.0814286768f, -0.132879525f }, { 2500, 0.499493301f, 0.312498778f },
  8015. { 5000, -0.184045464f, 0.171641544f }, { 9000, -0.153369367f, -0.20805186f }, { 16383, -0.00441304967f, 0.200126961f } } });
  8016. // 48 kHz, odd host blocks, max polyphony 1 (the last note steals); the hard-right note keeps FL's x87 left gain
  8017. check ({ blob, odd, 48000, 122.232761, 493.320297,
  8018. { { 100, 0.0236493405f, 0.0385922194f }, { 1000, 0.189806491f, 0.309736073f }, { 2500, -0.425576478f, -0.176279545f },
  8019. { 5000, -3.54030076e-18f, -0.0578194074f }, { 9000, 1.37052479e-17f, 0.223831087f }, { 16383, 9.248742e-18f, 0.151048407f } } });
  8020. // the channel processor (§3): our own settings snapshot — pan LFO (triangle, delayed, faded in), volume envelope
  8021. // with tensions and LFO, cutoff envelope + square LFO, resonance LFO, pitch envelope, SVF LP ×2 (type 7)
  8022. auto record = [] (std::vector<std::uint8_t>& s, int k, std::initializer_list<std::pair<int, int>> fields) {
  8023. const std::int32_t neutral[17] = { 0, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, 0 };
  8024. std::memcpy (s.data() + 0x44 * k, neutral, sizeof (neutral));
  8025. for (const auto& [i, v] : fields)
  8026. std::memcpy (s.data() + 0x44 * k + 4 * i, &v, 4);
  8027. };
  8028. auto settings = [] (std::vector<std::uint8_t>& s, std::int32_t modX, std::int32_t modY, std::int32_t type) {
  8029. std::memcpy (s.data() + 0x15c, &modX, 4);
  8030. std::memcpy (s.data() + 0x160, &modY, 4);
  8031. std::memcpy (s.data() + 0x168, &type, 4);
  8032. };
  8033. std::vector<std::uint8_t> snap (364, 0);
  8034. record (snap, 0, { { 9, 5000 }, { 10, 9000 }, { 11, -40 }, { 12, 15000 }, { 13, 1 } });
  8035. record (snap, 1, { { 0, 4 }, { 1, 1 }, { 3, 12000 }, { 4, 3000 }, { 5, 15000 }, { 6, 40 }, { 11, 30 }, { 12, 20000 }, { 14, 60 }, { 15, -90 }, { 16, -101 } });
  8036. record (snap, 2, { { 1, 1 }, { 3, 6000 }, { 4, 100 }, { 5, 16000 }, { 6, 20 }, { 8, -60 }, { 11, 20 }, { 12, 18000 }, { 13, 2 } });
  8037. record (snap, 3, { { 11, 40 }, { 12, 17000 } });
  8038. record (snap, 4, { { 1, 1 }, { 3, 5000 }, { 4, 100 }, { 5, 14000 }, { 6, 30 }, { 8, -700 } });
  8039. settings (snap, 140, 120, 7);
  8040. const auto withSnap = lynx::dx10StateWithSnapshot (blob, snap);
  8041. CHECK (withSnap.size() == 460 && withSnap[0] == 2 && ! lynx::dx10ProcessorNeutral (withSnap));
  8042. CHECK (lynx::dx10StateWithSnapshot (withSnap, std::vector<std::uint8_t> (364, 0)) == withSnap); // a v2 state keeps its own
  8043. lynx::dx10::RefOptions ticks;
  8044. ticks.tick = 230, ticks.ppq = 96, ticks.tempo = 120.0;
  8045. check ({ withSnap, ticks, 44100, 346.852062, 554.699375,
  8046. { { 100, -0.00162654708f, -0.00265428401f }, { 1000, 0.0310285408f, 0.0506339073f }, { 2500, -0.118068494f, -0.0915558636f },
  8047. { 5000, -0.297166437f, -0.256465197f }, { 9000, 0.199509233f, 0.166108996f }, { 16383, -0.00392446062f, 0.0410436988f } } });
  8048. // a volume envelope decaying to zero: the 10 ms end fade, then every voice is gone (silence from 14950 on)
  8049. std::vector<std::uint8_t> decay (364, 0);
  8050. for (int k = 0; k < 5; ++k)
  8051. record (decay, k, {});
  8052. record (decay, 1, { { 0, 4 }, { 1, 1 }, { 3, 9000 }, { 4, 100 }, { 5, 12000 }, { 6, 0 }, { 15, 50 }, { 16, -101 } });
  8053. settings (decay, 256, 0, 0);
  8054. auto ticks48 = ticks;
  8055. ticks48.block = 333;
  8056. const auto y = check ({ lynx::dx10StateWithSnapshot (blob, decay), ticks48, 48000, 338.361707, 350.520208,
  8057. { { 100, 0.000514116196f, 0.000838961045f }, { 1000, 0.0412623473f, 0.0673340634f }, { 2500, -0.141532451f, -0.156581759f },
  8058. { 5000, 0.382550895f, 0.23462519f }, { 7000, -0.189829409f, 0.144132584f }, { 9000, -0.13638559f, 0.142912149f } } });
  8059. bool silentTail = y.size() == 2 * 16384 && (y[2 * 14949] != 0.0f || y[2 * 14949 + 1] != 0.0f);
  8060. for (std::size_t i = 2 * 14950; i < y.size(); ++i)
  8061. silentTail = silentTail && y[i] == 0.0f;
  8062. CHECK (silentTail);
  8063. // the instrument: LYNX velocity v → Init.Vol = (11^(1.28·v) − 1)/10 and Final.Vol = channel volume × Init.Vol; channel
  8064. // gains √2·V·(cos, sin)((1 + p)π/4) → V, p; notes on block boundaries play like the reference renderer (neutral
  8065. // channel settings: the instrument's sequencer ticks change nothing; vibrato off, since the plugin-wide vibrato
  8066. // oscillator is stepped by each voice in turn, so its interleaving follows the host block layout, as in FL)
  8067. auto noVib = blob;
  8068. CHECK (lynx::dx10SetParam (noVib, 10, 0));
  8069. auto initVol = [] (double v) { return (double) (float) ((std::pow (11.0, 1.28 * v) - 1.0) / 10.0); }; // Init.Vol is a float
  8070. lynx::ChannelDef c;
  8071. c.instrument = lynx::InstrumentKind::dx10;
  8072. c.pluginState = noVib;
  8073. auto inst = lynx::makeInstrument (lynx::InstrumentKind::dx10);
  8074. inst->configure (c, 120.0);
  8075. inst->prepare (44100.0);
  8076. const float V = 0.6f, pan = 0.25f;
  8077. const auto gl = (float) (std::sqrt (2.0) * V * std::cos ((1.0 + pan) * 3.141592653589793 / 4.0));
  8078. const auto gr = (float) (std::sqrt (2.0) * V * std::sin ((1.0 + pan) * 3.141592653589793 / 4.0));
  8079. std::vector<float> l (16384), r (16384);
  8080. for (int s = 0; s < 16384; s += 512)
  8081. {
  8082. if (s == 0)
  8083. inst->noteOn (60, 0.78125f);
  8084. if (s == 2048)
  8085. {
  8086. inst->setNextNoteExpression (-0.5f, 25.0f);
  8087. inst->noteOn (67, 0.5f);
  8088. }
  8089. if (s == 6144)
  8090. inst->noteOff (60);
  8091. if (s == 8192)
  8092. inst->noteOff (67);
  8093. inst->render (l.data() + s, r.data() + s, 512, gl, gr);
  8094. }
  8095. // the channel level reaches the engine as FL's (recovered to float precision)
  8096. const auto Vr = std::sqrt (((double) gl * gl + (double) gr * gr) * 0.5);
  8097. const auto pr = std::atan2 ((double) gr, (double) gl) * (4.0 / 3.141592653589793) - 1.0;
  8098. const auto ref = lynx::dx10::renderReference (noVib, { { 0, 60, initVol (0.78125), Vr * initVol (0.78125), pr, 0.0, 6144 },
  8099. { 2048, 67, initVol (0.5), Vr * initVol (0.5), std::clamp (-0.5 + pr, -1.0, 1.0), 25.0, 8192 } },
  8100. 44100.0, 16384);
  8101. double worst = 0.0, peak = 0.0;
  8102. for (int i = 0; i < 16384; ++i)
  8103. worst = std::max ({ worst, (double) std::abs (ref[2 * (std::size_t) i] - l[(std::size_t) i]), (double) std::abs (ref[2 * (std::size_t) i + 1] - r[(std::size_t) i]) }),
  8104. peak = std::max (peak, (double) std::abs (l[(std::size_t) i]));
  8105. CHECK (worst == 0.0 && peak > 0.05);
  8106. // the released voices end (DX10's own release) and the instrument goes idle
  8107. std::vector<float> tail (44100), tailR (tail.size());
  8108. for (int k = 0; k < 20 && ! inst->idle(); ++k)
  8109. inst->render (tail.data(), tailR.data(), (int) tail.size(), gl, gr);
  8110. CHECK (inst->idle());
  8111. // with the channel settings above the instrument follows FL's ticks: the cutoff envelope darkens the note
  8112. {
  8113. lynx::ChannelDef cs = c;
  8114. cs.pluginState = withSnap;
  8115. cs.modelParams = { 96.0f };
  8116. auto si = lynx::makeInstrument (lynx::InstrumentKind::dx10);
  8117. si->configure (cs, 120.0);
  8118. si->prepare (44100.0);
  8119. std::vector<float> sl (32768), sr (32768);
  8120. si->noteOn (48, 0.78125f);
  8121. for (int s = 0; s < 32768; s += 512)
  8122. si->render (sl.data() + s, sr.data() + s, 512, 1.0f, 1.0f);
  8123. double e1 = 0.0, e2 = 0.0;
  8124. bool finite = true;
  8125. for (int i = 0; i < 32768; ++i)
  8126. finite = finite && std::isfinite (sl[(std::size_t) i]), (i < 4096 ? e1 : e2) += (double) sl[(std::size_t) i] * sl[(std::size_t) i];
  8127. CHECK (finite && e1 > 1e-3 && e2 > 1e-3);
  8128. }
  8129. // parameter edits (the channel settings panel) write the raw integers, clamped to the knob ranges
  8130. auto edited = blob;
  8131. CHECK (lynx::dx10SetParam (edited, 21, 7) && lynx::dx10Param (edited, 21) == 3);
  8132. CHECK (lynx::dx10SetParam (edited, 12, 70000) && lynx::dx10Param (edited, 12) == 65536);
  8133. }
  8134. TEST (gms_engine)
  8135. {
  8136. const auto blob = gmsTestState();
  8137. dumpIfAsked ("LYNX_DUMP_GMS", blob);
  8138. CHECK (lynx::gmsStateUsable (blob));
  8139. CHECK (lynx::gmsWaveNames (blob) == std::vector<std::string> ({ "NoWave A", "NoWave B", "NoWave C" }));
  8140. CHECK (lynx::gmsParamOffset (blob, 12) == 8 + 4 + 7 * 512 + 12 + 4 * 12 && lynx::gmsParamOffset (blob, 212) < 0);
  8141. auto broken = blob;
  8142. broken[8] = 'X';
  8143. CHECK (! lynx::gmsStateUsable (broken));
  8144. CHECK (! lynx::gmsStateUsable (std::vector<std::uint8_t> (blob.begin(), blob.begin() + 2000)));
  8145. // golden samples of our round-39 model (gmsmodel.GmsPlugin, waves missing → silent tables, tempo 140, 44.1 kHz,
  8146. // host blocks of 512): C5 vel 100 0–30000, E5 vel 80 11025–40000, G5 vel 110 22050–52000 (Init.Vol law A)
  8147. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  8148. const std::vector<lynx::gms::RefNote> notes = { { 0, 60, (float) iv (100), 1.0, 0.0, 0.0, 30000 },
  8149. { 11025, 64, (float) iv (80), 1.0, 0.0, 0.0, 40000 },
  8150. { 22050, 67, (float) iv (110), 1.0, 0.0, 0.0, 52000 } };
  8151. const auto y = lynx::gms::renderReference (blob, notes, 44100.0, 66150, "");
  8152. CHECK (y.size() == 2 * 66150u);
  8153. static const struct { int i; float l, r; } golden[] = { { 100, 0.0298744496f, 0.0198248848f }, { 5000, 0.0669416264f, 0.0666475743f }, { 15000, -0.005992427f, 0.013085857f }, { 25000, 0.0102018276f, 0.0130400788f }, { 35000, -0.0679694712f, -0.0050432696f }, { 45000, -0.0161557719f, 0.154340729f }, { 55000, -0.00719845621f, 0.0182181317f }, { 66000, 0.0389953516f, 0.0104107196f } };
  8154. for (const auto& g : golden)
  8155. CHECK (std::abs (y[2 * (std::size_t) g.i] - g.l) < 1e-6f && std::abs (y[2 * (std::size_t) g.i + 1] - g.r) < 1e-6f);
  8156. double e = 0.0;
  8157. for (auto v : y)
  8158. e += (double) v * v;
  8159. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.0790270452795) < 1e-6);
  8160. // the instrument: same engine behind LYNX's channel gains (V = 1, centre → law A), velocity → Init.Vol
  8161. lynx::ChannelDef c;
  8162. c.instrument = lynx::InstrumentKind::gms;
  8163. c.pluginState = blob;
  8164. auto inst = lynx::makeInstrument (lynx::InstrumentKind::gms);
  8165. inst->configure (c, 140.0);
  8166. inst->prepare (44100.0);
  8167. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  8168. for (int b = 0; b < 16; ++b)
  8169. {
  8170. if (b == 0)
  8171. inst->noteOn (60, 0.78125f);
  8172. if (b == 10)
  8173. inst->noteOff (60);
  8174. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  8175. }
  8176. const auto ref = lynx::gms::renderReference (blob, { { 0, 60, (std::pow (11.0, 1.28 * 0.78125) - 1.0) / 10.0, 1.0, 0.0, 0.0, 5120 } }, 44100.0, 8192, "");
  8177. double diff = 0.0, energy = 0.0;
  8178. for (int i = 0; i < 8192; ++i)
  8179. {
  8180. diff = std::max ({ diff, (double) std::abs (l[(std::size_t) i] - ref[2 * (std::size_t) i]), (double) std::abs (r[(std::size_t) i] - ref[2 * (std::size_t) i + 1]) });
  8181. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  8182. }
  8183. CHECK (diff == 0.0 && energy > 1e-3);
  8184. }
  8185. // A Toxic Biohazard state written from scratch (REA_rodada45; r45/py/mktest.py builds the same bytes): our own drawn
  8186. // spectra on operators 1 and 3 (wave 0) and the sine on operator 2, so no wave of the installation is needed
  8187. std::vector<std::uint8_t> toxicTestState()
  8188. {
  8189. auto s = lynx::toxicDefaultState();
  8190. static const std::pair<int, double> set[] = {
  8191. { 0, 0.62 }, { 1, 0.3 }, { 3, 0.05 }, { 4, 0.6 }, { 5, 0.7 }, { 6, 0.4 }, { 7, 0.3 }, { 8, 0.4 }, { 9, 0.8 }, { 10, 0.2 },
  8192. { 13, 0.45 }, { 15, 0.45 }, { 16, 0.55 }, { 17, 0.5 }, { 19, 0.1 }, { 20, 0.5 }, { 21, 0.4 }, { 22, 0.4 }, { 23, 0.8 },
  8193. { 24, 0.6 }, { 25, 1 }, { 26, 1 }, { 27, 1 }, { 32, 0.5606 }, { 33, 0.4394 }, { 38, 0.53 }, { 49, 0 }, { 55, 0.25 },
  8194. { 62, 0.5 }, { 75, 0.08 }, { 81, 0.5 }, { 87, 0.3 }, { 104, 0.8 }, { 105, 0.6 }, { 110, 0.35 }, { 115, 0.22 },
  8195. { 127, 0.18 }, { 147, 0.7 }, { 227, 1 }, { 229, 0.65 }, { 233, 0.4 }, { 236, 1 }, { 237, 1 }, { 252, 0.5 }, { 254, 0.3 },
  8196. { 256, 0.4 }, { 267, 0.4 }, { 269, 0.5 }, { 277, 0.3 }, { 300, 1 }, { 301, 8 }, { 302, 2 }, { 303, 0 }, { 305, 3 },
  8197. { 306, 0 }, { 307, 1 }, { 308, 0 }, { 314, 1 }, { 315, 0 }, { 316, 1 }, { 324, 106 }, { 332, 0.7 } };
  8198. for (const auto& [index, value] : set)
  8199. {
  8200. const auto v = (float) value;
  8201. std::memcpy (s.data() + lynx::toxicParamOffset (s, index), &v, 4);
  8202. }
  8203. auto spectrum = [&] (int slot, int h, float amp, float phase) {
  8204. const auto at = 12 + 0x40 + 1024 * (std::size_t) slot + 4 * (std::size_t) h;
  8205. std::memcpy (s.data() + at, &amp, 4);
  8206. std::memcpy (s.data() + at + 512, &phase, 4);
  8207. };
  8208. for (int h = 0; h < 24; ++h)
  8209. spectrum (0, h, (float) (1.0 / (h + 1)), 0.0f);
  8210. for (int k = 0; k < 16; ++k)
  8211. spectrum (2, 2 * k, (float) (1.0 / (2 * k + 1)), 0.25f);
  8212. return s;
  8213. }
  8214. TEST (toxic_engine)
  8215. {
  8216. const auto blob = toxicTestState();
  8217. dumpIfAsked ("LYNX_DUMP_TOXIC", blob);
  8218. CHECK (blob.size() == 10050u && lynx::toxicStateUsable (blob) && ! lynx::toxicNeedsDll (blob));
  8219. CHECK (lynx::toxicParamOffset (blob, 15) == 12 + 0x40 + 8192 + 4 * 15 && lynx::toxicParamOffset (blob, 340) < 0);
  8220. CHECK (! lynx::toxicDllUsable ("") && ! lynx::toxicDllUsable ("/nonexistent/Toxic Biohazard_x64.dll"));
  8221. auto broken = blob;
  8222. broken[8] = 'X';
  8223. CHECK (! lynx::toxicStateUsable (broken));
  8224. CHECK (! lynx::toxicStateUsable (std::vector<std::uint8_t> (blob.begin(), blob.begin() + 5000)));
  8225. auto needs = blob;
  8226. const auto w = 15.0f; // a built-in wave of the installation: without the DLL the engine refuses the state
  8227. std::memcpy (needs.data() + lynx::toxicParamOffset (needs, 307), &w, 4);
  8228. CHECK (lynx::toxicNeedsDll (needs) && lynx::toxic::renderReference (needs, { { 0, 60 } }, 44100.0, 64, "").empty());
  8229. // golden samples of our round-45 model (toxmodel.run, which equals the DLL bit for bit on this state), effects on,
  8230. // tempo 140, 44.1 kHz, host blocks of 512: C5 vel 100 0–30000, E5 vel 80 11025–40000, G5 vel 110 22050–52000
  8231. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  8232. const std::vector<lynx::toxic::RefNote> notes = { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 },
  8233. { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  8234. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 } };
  8235. const auto y = lynx::toxic::renderReference (blob, notes, 44100.0, 66150, "", 512, 140.0);
  8236. CHECK (y.size() == 2 * 66150u);
  8237. static const struct { int i; float l, r; } golden[] = { { 100, 0.264782101f, 0.189374551f }, { 5000, -0.605827034f, 0.43244195f }, { 15000, -0.343959779f, -0.111227378f }, { 25000, 0.0127612427f, -0.354315311f }, { 35000, -0.353289753f, -0.307500422f }, { 45000, -0.260735184f, 0.202133358f }, { 55000, -0.0751226544f, -0.0776180848f }, { 66000, -0.0294751395f, 0.00692501338f } };
  8238. for (const auto& g : golden)
  8239. CHECK (std::abs (y[2 * (std::size_t) g.i] - g.l) < 1e-6f && std::abs (y[2 * (std::size_t) g.i + 1] - g.r) < 1e-6f);
  8240. double e = 0.0;
  8241. for (auto v : y)
  8242. e += (double) v * v;
  8243. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.307227390984) < 1e-6);
  8244. // the instrument: same engine behind LYNX's channel gains (V = 1, centre → law A), velocity → Init.Vol
  8245. lynx::ChannelDef c;
  8246. c.instrument = lynx::InstrumentKind::toxic;
  8247. c.pluginState = blob;
  8248. auto inst = lynx::makeInstrument (lynx::InstrumentKind::toxic);
  8249. inst->configure (c, 140.0);
  8250. inst->prepare (44100.0);
  8251. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  8252. for (int b = 0; b < 16; ++b)
  8253. {
  8254. if (b == 0)
  8255. inst->noteOn (60, 0.78125f);
  8256. if (b == 10)
  8257. inst->noteOff (60);
  8258. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  8259. }
  8260. const auto ref = lynx::toxic::renderReference (blob, { { 0, 60, (std::pow (11.0, 1.28 * 0.78125) - 1.0) / 10.0, 1.0, 0.0, 0.0, 5120 } },
  8261. 44100.0, 8192, "", 512, 140.0);
  8262. double diff = 0.0, energy = 0.0;
  8263. for (int i = 0; i < 8192; ++i)
  8264. {
  8265. diff = std::max ({ diff, (double) std::abs (l[(std::size_t) i] - ref[2 * (std::size_t) i]), (double) std::abs (r[(std::size_t) i] - ref[2 * (std::size_t) i + 1]) });
  8266. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  8267. }
  8268. CHECK (diff == 0.0 && energy > 1e-3);
  8269. }
  8270. TEST (morphine_engine)
  8271. {
  8272. const auto blob = morphineTestState();
  8273. dumpIfAsked ("LYNX_DUMP_MORPHINE", blob);
  8274. CHECK (lynx::morphineStateUsable (blob));
  8275. CHECK (lynx::morphineNoiseName (blob).empty());
  8276. const auto at = lynx::morphineParamOffset (blob, 9);
  8277. CHECK (at > 0);
  8278. float a9 = 0.0f;
  8279. std::memcpy (&a9, blob.data() + at, 4);
  8280. CHECK (a9 == 0.2f && lynx::morphineParamOffset (blob, 40) < 0);
  8281. auto broken = blob;
  8282. broken[12] = 'X'; // the MPR0 tag
  8283. CHECK (! lynx::morphineStateUsable (broken));
  8284. CHECK (! lynx::morphineStateUsable (std::vector<std::uint8_t> (blob.begin(), blob.end() - 10)));
  8285. // golden samples of our round-39 model (mormodel.Synth, tempo 120, 44.1 kHz, host blocks of 512): keys 60/64/67,
  8286. // Init.Vol 1.0/0.5/2.0, note-offs at 30000/50000/70000
  8287. const std::vector<lynx::morphine::RefNote> notes = { { 0, 60, 1.0, 1.0, 0.0, 0.0, 30000 },
  8288. { 11025, 64, 0.5, 1.0, 0.0, 0.0, 50000 },
  8289. { 22050, 67, 2.0, 1.0, 0.0, 0.0, 70000 } };
  8290. const auto y = lynx::morphine::renderReference (blob, notes, 44100.0, 80000, "");
  8291. CHECK (y.size() == 2 * 80000u);
  8292. static const struct { int i; float l, r; } golden[] = { { 100, 0.00094500737f, 0.00085531926f }, { 5000, 0.0311509669f, -0.11552836f }, { 15000, 0.0843918771f, 0.0369227529f }, { 25000, 0.60864079f, 1.03062224f }, { 35000, -0.346013308f, 0.135865599f }, { 45000, -0.0373920798f, -0.13064307f }, { 60000, -0.138871655f, -0.163586512f }, { 75000, 0.0251323506f, -0.00735046482f } };
  8293. for (const auto& g : golden)
  8294. CHECK (std::abs (y[2 * (std::size_t) g.i] - g.l) < 1e-6f && std::abs (y[2 * (std::size_t) g.i + 1] - g.r) < 1e-6f);
  8295. double e = 0.0;
  8296. for (auto v : y)
  8297. e += (double) v * v;
  8298. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.18467099041) < 1e-6);
  8299. // the instrument: same engine behind LYNX's channel gains
  8300. lynx::ChannelDef c;
  8301. c.instrument = lynx::InstrumentKind::morphine;
  8302. c.pluginState = blob;
  8303. auto inst = lynx::makeInstrument (lynx::InstrumentKind::morphine);
  8304. inst->configure (c, 120.0);
  8305. inst->prepare (44100.0);
  8306. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  8307. for (int b = 0; b < 16; ++b)
  8308. {
  8309. if (b == 0)
  8310. inst->noteOn (60, 0.78125f);
  8311. if (b == 10)
  8312. inst->noteOff (60);
  8313. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  8314. }
  8315. const auto ref = lynx::morphine::renderReference (blob, { { 0, 60, (std::pow (11.0, 1.28 * 0.78125) - 1.0) / 10.0, 1.0, 0.0, 0.0, 5120 } }, 44100.0, 8192, "");
  8316. double diff = 0.0, energy = 0.0;
  8317. for (int i = 0; i < 8192; ++i)
  8318. {
  8319. diff = std::max ({ diff, (double) std::abs (l[(std::size_t) i] - ref[2 * (std::size_t) i]), (double) std::abs (r[(std::size_t) i] - ref[2 * (std::size_t) i + 1]) });
  8320. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  8321. }
  8322. CHECK (diff == 0.0 && energy > 1e-3);
  8323. }
  8324. namespace
  8325. {
  8326. // A MiniSynth state written from scratch (REA_rodada41 §1.1 layout: "SSLF" + the .flms preset)
  8327. std::vector<std::uint8_t> miniSynthTestState (const std::array<float, 43>& P)
  8328. {
  8329. const std::string name = "LYNX test";
  8330. Bytes body;
  8331. body.raw ("\x99" "FLS\r\n\x1a\n", 8);
  8332. body.raw ("HEAD", 4);
  8333. body.i32 (6 + 2 * (std::int32_t) name.size());
  8334. const std::uint16_t head[3] = { 1, 1, (std::uint16_t) name.size() };
  8335. body.raw (reinterpret_cast<const char*> (head), 6);
  8336. for (char ch : name)
  8337. body.raw (&ch, 1), body.zeros (1);
  8338. body.raw ("DATA", 4);
  8339. body.i32 (176);
  8340. body.raw ("0rev", 4);
  8341. for (auto v : P)
  8342. body.f32 (v);
  8343. Bytes s;
  8344. s.raw ("SSLF", 4);
  8345. s.i32 ((std::int32_t) body.b.size());
  8346. s.bytes (body.b);
  8347. return s.b;
  8348. }
  8349. std::array<float, 43> miniSynthDefaults()
  8350. {
  8351. return { 0.375f, 0.5f, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0.5f, 0, 0, 0.25f, 0.5f, 0, 1, 1, 0.07f, 0, 0, 0, 0, 0.7f, 0.5f, 1,
  8352. 0.5f, 0, 0.25f, 0.5f, 0.5f, 0, 0.11222222f, 0.4f, 0.1f, 1, 0.5f };
  8353. }
  8354. // a synthetic oscdata.dat (amplitudes 0.3/k, phases −π/2 + 0.001·(k − 1)): the test does not need an FL installation
  8355. std::string miniSynthTestOscdata()
  8356. {
  8357. const auto path = (std::filesystem::temp_directory_path() / "lynx_test_oscdata.dat").string();
  8358. std::vector<float> d (2048);
  8359. for (int h = 0; h < 1024; ++h)
  8360. d[(std::size_t) h] = (float) (0.3 / (h + 1)), d[1024 + (std::size_t) h] = (float) (-1.5707963267948966 + 0.001 * h);
  8361. std::ofstream out (path, std::ios::binary);
  8362. out.write (reinterpret_cast<const char*> (d.data()), 8192);
  8363. return path;
  8364. }
  8365. } // namespace
  8366. TEST (minisynth_engine)
  8367. {
  8368. auto A = miniSynthDefaults(), B = miniSynthDefaults();
  8369. // A: poly, SQR+SQR, noise, filter env/velocity/keytracking, LFO → cutoff, distortion + decimator, delay, chorus, phaser
  8370. const std::pair<int, float> a[] = { { 5, 0.45f }, { 6, 0.6f }, { 7, 0.4f }, { 8, 0.45f }, { 9, 0.6f }, { 10, 0.5f }, { 11, 0.7f },
  8371. { 12, 0.1f }, { 13, 0.4f }, { 14, 0.3f }, { 15, 0.5f }, { 16, 0.8f }, { 17, 0.6f }, { 18, 0.1f },
  8372. { 19, 0.5f }, { 20, 0.85f }, { 21, 0.05f }, { 22, 0.5f }, { 23, 0.6f }, { 24, 0.4f }, { 25, 1 },
  8373. { 26, 0.3f }, { 27, 0.1f }, { 28, 1 }, { 29, 0.4f }, { 30, 0.5f }, { 31, 0.6f }, { 32, 0.6f },
  8374. { 33, 1 }, { 34, 0.4f }, { 35, 0.6f }, { 36, 0.5f }, { 37, 1 }, { 38, 0.3f }, { 39, 0.6f },
  8375. { 40, 0.5f }, { 41, 0.7f }, { 42, 0.4f } };
  8376. // B: lead (legato glide), BELL, LFO → pitch
  8377. const std::pair<int, float> b[] = { { 0, 1 }, { 2, 0.4f }, { 5, 0.7f }, { 6, 0.3f }, { 7, 0.1f }, { 8, 0.6f }, { 9, 0.3f }, { 16, 0.7f },
  8378. { 17, 0.1f }, { 18, 0.5f }, { 19, 0.6f }, { 20, 0.7f }, { 22, 0.6f }, { 23, 0.5f }, { 24, 0.3f } };
  8379. for (const auto& [i, v] : a)
  8380. A[(std::size_t) i] = v;
  8381. for (const auto& [i, v] : b)
  8382. B[(std::size_t) i] = v;
  8383. const auto blobA = miniSynthTestState (A), blobB = miniSynthTestState (B);
  8384. dumpIfAsked ("LYNX_DUMP_MINISYNTH", blobA);
  8385. CHECK (lynx::miniSynthStateUsable (blobA) && lynx::miniSynthParams (blobA).size() == 43u && lynx::miniSynthParams (blobA)[26] == 0.3f);
  8386. CHECK (lynx::miniSynthParamOffset (blobA, 9) == 8 + 8 + 8 + 6 + 18 + 12 + 4 * 9 && lynx::miniSynthParamOffset (blobA, 43) < 0);
  8387. CHECK (! lynx::miniSynthStateUsable (std::vector<std::uint8_t> (blobA.begin(), blobA.begin() + 20)));
  8388. auto shortData = blobA;
  8389. shortData.resize (shortData.size() - 4 * 40); // only 3 parameters present: the rest keeps the defaults
  8390. CHECK (lynx::miniSynthParams (shortData)[20] == 0.5f && lynx::miniSynthParams (shortData)[1] == 0.5f);
  8391. // golden samples of our round-41 model (msmodel.Plugin on the same synthetic oscdata, 44.1 kHz, host blocks of 512,
  8392. // L = R = Final.Vol): C5 vel 100 0–30000, E5 vel 80 11025–40000, G5 vel 110 22050–52000 (Init.Vol law A)
  8393. const auto osc = miniSynthTestOscdata();
  8394. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  8395. const std::vector<lynx::minisynth::RefNote> notes = { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 },
  8396. { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  8397. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 } };
  8398. struct Golden { int i; float l, r; };
  8399. static const Golden goldenA[] = { { 100, 0.183149248f, -0.226551995f }, { 5000, -0.00842505973f, 0.254078597f }, { 15000, -0.180335894f, 0.0890596062f }, { 25000, 0.465151846f, 0.396314442f }, { 35000, 0.272939265f, 0.18288976f }, { 45000, -0.127651259f, -0.34875524f }, { 55000, -0.153043061f, -0.248885199f }, { 66000, -0.0197723396f, -0.0807986259f } };
  8400. static const Golden goldenB[] = { { 100, 0.175394624f, 0.175394624f }, { 5000, 0.0503161065f, 0.0503161065f }, { 15000, 0.0470628254f, 0.0470628254f }, { 25000, 0.0486912318f, 0.0486912318f }, { 35000, 0.021853026f, 0.021853026f }, { 45000, 0.0362072662f, 0.0362072662f }, { 55000, -0.000109080342f, -0.000109080342f }, { 66000, 0.0f, 0.0f } };
  8401. for (int which = 0; which < 2; ++which)
  8402. {
  8403. const auto y = lynx::minisynth::renderReference (which == 0 ? blobA : blobB, notes, 44100.0, 66150, osc);
  8404. CHECK (y.size() == 2 * 66150u);
  8405. if (y.size() != 2 * 66150u)
  8406. continue;
  8407. for (const auto& g : which == 0 ? std::vector (std::begin (goldenA), std::end (goldenA)) : std::vector (std::begin (goldenB), std::end (goldenB)))
  8408. CHECK (y[2 * (std::size_t) g.i] == g.l && y[2 * (std::size_t) g.i + 1] == g.r);
  8409. double e = 0.0;
  8410. for (auto v : y)
  8411. e += (double) v * v;
  8412. CHECK (std::abs (std::sqrt (e / (double) y.size()) - (which == 0 ? 0.315552434392 : 0.03926116287)) < 1e-9);
  8413. }
  8414. // missing oscdata.dat: silent tables, still finite
  8415. const auto silent = lynx::minisynth::renderReference (blobB, notes, 44100.0, 4096, "");
  8416. CHECK (silent.size() == 2 * 4096u && std::all_of (silent.begin(), silent.end(), [] (float v) { return std::isfinite (v) && std::abs (v) < 1.0f; }));
  8417. // the instrument: same engine behind LYNX's channel gains (V = 1, centre → law A), velocity → Init.Vol
  8418. lynx::ChannelDef c;
  8419. c.instrument = lynx::InstrumentKind::minisynth;
  8420. c.pluginState = blobA;
  8421. c.samplePath = osc;
  8422. auto inst = lynx::makeInstrument (lynx::InstrumentKind::minisynth);
  8423. inst->configure (c, 120.0);
  8424. inst->prepare (44100.0);
  8425. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  8426. for (int k = 0; k < 16; ++k)
  8427. {
  8428. if (k == 0)
  8429. inst->noteOn (60, 0.78125f);
  8430. if (k == 10)
  8431. inst->noteOff (60);
  8432. inst->render (l.data() + 512 * k, r.data() + 512 * k, 512, 1.0f, 1.0f);
  8433. }
  8434. const auto ref = lynx::minisynth::renderReference (blobA, { { 0, 60, (std::pow (11.0, 1.28 * 0.78125) - 1.0) / 10.0, 1.0, 0.0, 0.0, 5120 } },
  8435. 44100.0, 8192, osc, 512, 120.0, 0, -1.0, true);
  8436. double diff = 0.0, energy = 0.0;
  8437. for (int i = 0; i < 8192; ++i)
  8438. {
  8439. diff = std::max ({ diff, (double) std::abs (l[(std::size_t) i] - ref[2 * (std::size_t) i]), (double) std::abs (r[(std::size_t) i] - ref[2 * (std::size_t) i + 1]) });
  8440. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  8441. }
  8442. CHECK (diff == 0.0 && energy > 1e-3 && ! inst->idle());
  8443. }
  8444. namespace
  8445. {
  8446. // A PoiZone state written from scratch (REA_rodada44 §1.3 layout, "Ver4" + 156 floats + name): pulse A synced to saw B,
  8447. // noise, envelope to the cutoff, triangle LFO to oscillator A's pitch, 3-voice unison, gate, chorus and tape delay.
  8448. std::vector<std::uint8_t> poizoneTestState()
  8449. {
  8450. std::array<float, 156> P {};
  8451. const std::pair<int, float> values[] = { { 0, 1.0f }, { 1, 0.7f }, { 2, 1.0f }, { 5, 0.6f }, { 6, 0.75f }, { 7, 0.2f }, { 8, 0.4f }, { 9, 0.1f },
  8452. { 10, 0.8f }, { 11, 0.3f }, { 12, 0.5f }, { 13, 0.4f }, { 14, 0.5f }, { 15, 0.5f }, { 16, 0.5f }, { 17, 0.5f },
  8453. { 18, 0.35f }, { 19, 0.6f }, { 20, 0.5f }, { 21, 0.3f }, { 22, 0.2f }, { 23, 0.6f }, { 24, 0.7f }, { 25, 0.5f },
  8454. { 26, 0.7f }, { 28, 0.6f }, { 29, 0.3f }, { 30, 0.8f }, { 31, 1.0f }, { 32, 0.3f }, { 33, 0.5f }, { 34, 0.5f },
  8455. { 35, 0.3f }, { 37, 1.0f }, { 38, 0.6f }, { 39, 1.0f }, { 40, 0.4f }, { 41, 0.5f }, { 42, 0.5f }, { 43, 0.5f },
  8456. { 47, 0.5f }, { 48, 0.3f }, { 67, 0.2f }, { 68, 0.5f }, { 75, 2.0f }, { 76, 15.0f }, { 77, 1.0f }, { 78, 1.0f },
  8457. { 79, 1.0f } };
  8458. for (const auto& [i, v] : values)
  8459. P[(std::size_t) i] = v;
  8460. for (int i = 49; i < 65; ++i)
  8461. P[(std::size_t) i] = (i - 49) % 3 != 2 ? 1.0f : 0.0f;
  8462. for (int i = 81; i < 155; ++i)
  8463. P[(std::size_t) i] = -1.0f;
  8464. std::vector<std::uint8_t> blob { 'S', 'S', 'L', 'F', 0xb4, 0x02, 0x00, 0x00, '4', 'r', 'e', 'V' };
  8465. blob.resize (12 + 4 * 156 + 64, 0);
  8466. std::memcpy (blob.data() + 12, P.data(), 4 * 156);
  8467. std::memcpy (blob.data() + 12 + 4 * 156, "LYNX test", 9);
  8468. return blob;
  8469. }
  8470. } // namespace
  8471. TEST (poizone_engine)
  8472. {
  8473. const auto blob = poizoneTestState();
  8474. CHECK (lynx::poizoneStateUsable (blob));
  8475. CHECK (lynx::poizoneParamOffset (blob, 18) == 12 + 4 * 18 && lynx::poizoneParamOffset (blob, 156) < 0);
  8476. CHECK (lynx::poizoneParam (blob, 75) == 2.0f && ! lynx::poizoneParam (blob, -1));
  8477. auto broken = blob;
  8478. broken[8] = 'X'; // the "Ver4" tag
  8479. CHECK (! lynx::poizoneStateUsable (broken));
  8480. CHECK (! lynx::poizoneStateUsable (std::vector<std::uint8_t> (blob.begin(), blob.begin() + 10)));
  8481. // golden samples of our round-44 model (pzmodel.Plugin, tempo 120, 44.1 kHz, host blocks of 512, L = R = Final.Vol):
  8482. // C5 vel 100 0–30000, E5 vel 80 11025–40000, G5 vel 110 22050–52000 (Init.Vol = (11^(vel/100) − 1)/10)
  8483. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  8484. const std::vector<lynx::poizone::RefNote> notes = { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 },
  8485. { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  8486. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 } };
  8487. const auto y = lynx::poizone::renderReference (blob, notes, 44100.0, 66150);
  8488. CHECK (y.size() == 2 * 66150u);
  8489. static const struct { int i; float l, r; } golden[] = { { 100, 0.0058150403f, 0.00038307323f }, { 5000, 0.01855834f, 0.09409425f }, { 15000, -0.01985861f, -0.013393694f }, { 25000, 0.121419534f, -0.12814318f }, { 35000, -0.09936376f, 0.034945987f }, { 45000, -0.09803468f, 0.05587045f }, { 55000, -0.07215294f, 0.02417629f }, { 66000, -0.007312033f, -0.047375992f } };
  8490. for (const auto& g : golden)
  8491. CHECK (std::abs (y[2 * (std::size_t) g.i] - g.l) < 1e-7f && std::abs (y[2 * (std::size_t) g.i + 1] - g.r) < 1e-7f);
  8492. double e = 0.0;
  8493. for (auto v : y)
  8494. e += (double) v * v;
  8495. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.10990940878400073) < 1e-7);
  8496. // the instrument: same engine behind LYNX's channel gains (V = 1, centre → law A), velocity → Init.Vol
  8497. lynx::ChannelDef c;
  8498. c.instrument = lynx::InstrumentKind::poizone;
  8499. c.pluginState = blob;
  8500. auto inst = lynx::makeInstrument (lynx::InstrumentKind::poizone);
  8501. inst->configure (c, 120.0);
  8502. inst->prepare (44100.0);
  8503. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  8504. for (int b = 0; b < 16; ++b)
  8505. {
  8506. if (b == 0)
  8507. inst->noteOn (60, 0.78125f);
  8508. if (b == 10)
  8509. inst->noteOff (60);
  8510. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  8511. }
  8512. const auto ref = lynx::poizone::renderReference (blob, { { 0, 60, (std::pow (11.0, 1.28 * 0.78125) - 1.0) / 10.0, 1.0, 0.0, 0.0, 5120 } }, 44100.0, 8192,
  8513. 512, 120.0, 0, true);
  8514. double diff = 0.0, energy = 0.0;
  8515. for (int i = 0; i < 8192; ++i)
  8516. {
  8517. diff = std::max ({ diff, (double) std::abs (l[(std::size_t) i] - ref[2 * (std::size_t) i]), (double) std::abs (r[(std::size_t) i] - ref[2 * (std::size_t) i + 1]) });
  8518. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  8519. }
  8520. CHECK (diff == 0.0 && energy > 1e-3);
  8521. }
  8522. // ---- Fruit Kick / BassDrum (REA_rodada43): synthetic states and data, golden values of our round-43 models ----
  8523. namespace
  8524. {
  8525. std::vector<std::uint8_t> fruitKickStateA()
  8526. {
  8527. const std::int32_t w[8] = { 0, 0, -600, 64, 128, 32, 20, 0 };
  8528. std::vector<std::uint8_t> b (32);
  8529. std::memcpy (b.data(), w, 32);
  8530. return b;
  8531. }
  8532. std::vector<std::uint8_t> fruitKickStateB()
  8533. {
  8534. std::vector<std::uint8_t> b (400, 0);
  8535. const std::int32_t head[7] = { 1, 624, -8, 42, 86, 64, 0 };
  8536. std::memcpy (b.data(), head, sizeof (head));
  8537. std::int32_t recs[5][17];
  8538. for (auto& r : recs)
  8539. {
  8540. const std::int32_t d[17] = { 0, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, 0 };
  8541. std::memcpy (r, d, sizeof (d));
  8542. }
  8543. const std::int32_t pan[17] = { 0, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 3000, 64, 20000, 1, 0, 0, 0 };
  8544. const std::int32_t vol[17] = { 4, -1, 100, 3000, 100, 25000, 64, 15000, 0, 100, 20000, 0, 32950, 0, 0, 40, -101 };
  8545. std::memcpy (recs[0], pan, sizeof (pan));
  8546. std::memcpy (recs[1], vol, sizeof (vol));
  8547. for (int k = 0; k < 5; ++k)
  8548. std::memcpy (b.data() + 32 + 68 * k, recs[k], 68);
  8549. const std::int32_t cut = 256;
  8550. std::memcpy (b.data() + 32 + 0x15c, &cut, 4);
  8551. return b;
  8552. }
  8553. std::string bassDrumTestFolder()
  8554. {
  8555. // our own oscillator table and a pseudo-random 16-bit noise sample, laid out like FL's BassDrum folder
  8556. const auto dir = std::filesystem::temp_directory_path() / "lynx_bassdrum_test";
  8557. std::filesystem::create_directories (dir / "Noise Samples");
  8558. auto wav = [] (const std::filesystem::path& path, const void* data, std::uint32_t bytes, std::uint16_t fmt, std::uint16_t bits) {
  8559. std::ofstream o (path, std::ios::binary);
  8560. auto u32 = [&] (std::uint32_t v) { o.write ((const char*) &v, 4); };
  8561. auto u16 = [&] (std::uint16_t v) { o.write ((const char*) &v, 2); };
  8562. o.write ("RIFF", 4), u32 (36 + bytes), o.write ("WAVEfmt ", 8), u32 (16), u16 (fmt), u16 (1), u32 (44100),
  8563. u32 (44100u * bits / 8), u16 ((std::uint16_t) (bits / 8)), u16 (bits), o.write ("data", 4), u32 (bytes);
  8564. o.write ((const char*) data, bytes);
  8565. };
  8566. std::vector<float> table (1024);
  8567. for (int i = 0; i < 1024; ++i)
  8568. table[(std::size_t) i] = (float) (0.9 * std::sin (2.0 * 3.141592653589793 * i / 1024.0) + 0.05 * std::sin (6.0 * 3.141592653589793 * i / 1024.0));
  8569. wav (dir / "sounddata", table.data(), 4096, 3, 32);
  8570. std::vector<std::int16_t> noise (3000);
  8571. std::uint32_t s = 12345;
  8572. for (auto& v : noise)
  8573. {
  8574. s = (s * 1103515245u + 12345u) & 0x7fffffffu;
  8575. v = (std::int16_t) ((int) ((s >> 8) % 20001u) - 10000);
  8576. }
  8577. wav (dir / "Noise Samples" / "test noise.wav", noise.data(), 6000, 1, 16);
  8578. return dir.string();
  8579. }
  8580. std::vector<std::uint8_t> bassDrumTestState()
  8581. {
  8582. std::vector<std::uint8_t> b (0x50C + 92, 0);
  8583. std::memcpy (b.data(), "SSLF", 4);
  8584. const std::uint32_t size = 0x560;
  8585. std::memcpy (b.data() + 4, &size, 4);
  8586. std::memcpy (b.data() + 8, "2kiK", 4);
  8587. std::memcpy (b.data() + 12, "Test kick", 9);
  8588. const char path[] = "C:/Somewhere/Noise Samples/test noise.wav";
  8589. std::memcpy (b.data() + 268, path, sizeof (path) - 1);
  8590. const float P[23] = { 0.55f, 0.5f, 0.6f, 0.6f, 0.45f, 0.1f, 0.3f, 0.4f, 0.3f, 0.6f, 0.4f, 0.65f,
  8591. 0.5f, 0.8f, 0.15f, 0.9f, 0.3f, 0.5f, 0.6f, 0.62f, 0.4f, 0.5f, 0.85f };
  8592. std::memcpy (b.data() + 0x50C, P, sizeof (P));
  8593. return b;
  8594. }
  8595. struct Golden
  8596. {
  8597. int i;
  8598. float l, r;
  8599. };
  8600. bool matchesGolden (const std::vector<float>& y, std::initializer_list<Golden> golden, double rms)
  8601. {
  8602. bool ok = true;
  8603. for (const auto& g : golden)
  8604. ok = ok && std::abs (y[2 * (std::size_t) g.i] - g.l) < 1e-6f && std::abs (y[2 * (std::size_t) g.i + 1] - g.r) < 1e-6f;
  8605. double e = 0.0;
  8606. for (auto v : y)
  8607. e += (double) v * v;
  8608. return ok && std::abs (std::sqrt (e / (double) y.size()) - rms) < 1e-6;
  8609. }
  8610. } // namespace
  8611. TEST (fruitkick_engine)
  8612. {
  8613. const auto A = fruitKickStateA(), B = fruitKickStateB();
  8614. CHECK (lynx::fruitKickStateUsable (A) && lynx::fruitKickStateUsable (B));
  8615. CHECK (! lynx::fruitKickStateUsable (std::vector<std::uint8_t> (A.begin(), A.begin() + 20)));
  8616. CHECK (lynx::fruitKickParam (A, 1) == -600 && lynx::fruitKickParam (B, 0) == 624);
  8617. CHECK (lynx::fruitKickFallbacks (A).empty() && lynx::fruitKickFallbacks (B).empty());
  8618. // a version-0 state takes the channel's envelope records as its snapshot (and becomes version 1)
  8619. std::vector<std::vector<std::uint8_t>> recs;
  8620. for (int k = 0; k < 5; ++k)
  8621. recs.emplace_back (B.begin() + 32 + 68 * k, B.begin() + 32 + 68 * (k + 1));
  8622. const auto withEnv = lynx::fruitKickWithChannelEnvelope (A, recs, 256, 0, 0);
  8623. CHECK (withEnv.size() == 400 && lynx::fruitKickParam (withEnv, 5) == 20 && std::memcmp (withEnv.data() + 32, B.data() + 32, 364) == 0);
  8624. CHECK (lynx::fruitKickWithChannelEnvelope (B, recs, 256, 0, 0) == B);
  8625. // golden samples of our round-43 model (fkmodel.run, 44.1 kHz, host blocks of 512): C5 at 0 held, C6 at 3000
  8626. // (Final.Vol 0.5, pan 0.3) released at 9000; B with the plugin's tick every 230 samples from 115
  8627. const std::vector<lynx::fruitkick::RefNote> notes = { { 0, 60, 1.0, 0.0, 0.0, -1 }, { 3000, 72, 0.5, 0.3, 0.0, 9000 } };
  8628. const auto ya = lynx::fruitkick::renderReference (A, notes, 44100.0, 20000);
  8629. CHECK (ya.size() == 40000u);
  8630. CHECK (matchesGolden (ya, { { 10, 0.803596497f, 0.803596497f }, { 700, 0.18540436f, 0.18540436f }, { 2900, -0.63492924f, -0.63492924f },
  8631. { 3100, 0.339005232f, 0.544316053f }, { 5000, -0.097384721f, -0.150579333f }, { 9100, 0.124680087f, 0.0544927865f },
  8632. { 9400, -0.146474078f, -0.144857526f }, { 15000, 0.00938183628f, 0.00938183628f } },
  8633. 0.284267726379));
  8634. std::vector<int> ticks;
  8635. for (int t = 115; t < 20000; t += 230)
  8636. ticks.push_back (t);
  8637. const auto yb = lynx::fruitkick::renderReference (B, notes, 44100.0, 20000, 512, ticks);
  8638. CHECK (matchesGolden (yb, { { 10, -0.00760388654f, -0.00760388654f }, { 700, 0.386696398f, 0.442232877f }, { 2900, -0.123097703f, -0.302172035f },
  8639. { 3100, 0.0523624197f, 0.0877688825f }, { 5000, 0.045177035f, 0.100505695f }, { 9100, -0.00423920294f, 0.00810315367f },
  8640. { 9400, 0.00482427329f, -0.010199016f }, { 15000, -0.000171193431f, 1.01026744e-05f } },
  8641. 0.117964927585));
  8642. // the instrument: same engine behind LYNX's channel gains (V = 1, centre), velocity → Init.Vol (link velocity on)
  8643. lynx::ChannelDef c;
  8644. c.instrument = lynx::InstrumentKind::fruitkick;
  8645. c.pluginState = A;
  8646. auto inst = lynx::makeInstrument (lynx::InstrumentKind::fruitkick);
  8647. inst->configure (c, 140.0);
  8648. inst->prepare (44100.0);
  8649. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  8650. for (int b = 0; b < 16; ++b)
  8651. {
  8652. if (b == 0)
  8653. inst->noteOn (60, 0.78125f);
  8654. if (b == 10)
  8655. inst->noteOff (60);
  8656. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  8657. }
  8658. const auto iv = (std::pow (11.0, 1.28 * 0.78125) - 1.0) / 10.0;
  8659. const auto ref = lynx::fruitkick::renderReference (A, { { 0, 60, iv, 0.0, 0.0, 5120 } }, 44100.0, 8192);
  8660. double diff = 0.0, energy = 0.0;
  8661. for (int i = 0; i < 8192; ++i)
  8662. {
  8663. diff = std::max ({ diff, (double) std::abs (l[(std::size_t) i] - ref[2 * (std::size_t) i]), (double) std::abs (r[(std::size_t) i] - ref[2 * (std::size_t) i + 1]) });
  8664. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  8665. }
  8666. CHECK (diff < 1e-6 && energy > 1e-2);
  8667. }
  8668. TEST (bassdrum_engine)
  8669. {
  8670. // FL's CRT powf as the engine computes it: a case where its last bit differs from the correctly rounded power
  8671. const float x = 0.87011606f;
  8672. std::uint32_t bits = 0;
  8673. const auto p = lynx::bassdrum::powfMsvc (x, 0.125f);
  8674. std::memcpy (&bits, &p, 4);
  8675. CHECK (bits == 1065063964u);
  8676. CHECK (lynx::bassdrum::powfMsvc (2.0f, 1.0f) == 2.0f && lynx::bassdrum::powfMsvc (10.0f, 0.0f) == 1.0f);
  8677. const auto st = bassDrumTestState();
  8678. CHECK (lynx::bassDrumStateUsable (st) && lynx::bassDrumNoiseName (st) == "test noise.wav");
  8679. CHECK (lynx::bassDrumParamOffset (st, 6) == 0x50C + 24 && lynx::bassDrumParamOffset (st, 23) < 0);
  8680. auto broken = st;
  8681. broken[8] = 'X';
  8682. CHECK (! lynx::bassDrumStateUsable (broken));
  8683. // golden samples of our round-43 model (bdmodel.run with its powf port, 44.1 kHz, host blocks of 512)
  8684. const auto dir = bassDrumTestFolder();
  8685. const std::vector<lynx::bassdrum::RefNote> notes = { { 0, 60, 1.0, 1.0, 0.0, 0.0, -1 }, { 6000, 64, 0.6, 0.8, 0.4, 25.0, 9000 },
  8686. { 6100, 55, 1.5, 1.0, -0.3, 0.0, -1 } };
  8687. const auto y = lynx::bassdrum::renderReference (st, notes, 44100.0, 30000, dir);
  8688. CHECK (y.size() == 60000u);
  8689. CHECK (matchesGolden (y, { { 50, 0.0122119058f, 0.0122119058f }, { 400, -0.0886279643f, -0.0886279643f }, { 3000, -0.0428197235f, -0.0428197235f },
  8690. { 6050, 0.0199260078f, 0.0240648985f }, { 6300, -0.257363439f, -0.185516387f }, { 9000, 0.268954486f, 0.10506025f },
  8691. { 15000, -0.0102827176f, -0.0380715057f }, { 25000, 0.0f, 0.0f } },
  8692. 0.230007588169));
  8693. CHECK (lynx::bassdrum::renderReference (st, notes, 44100.0, 100, dir + "/nowhere").empty()); // no table: no engine
  8694. lynx::ChannelDef c;
  8695. c.instrument = lynx::InstrumentKind::bassdrum;
  8696. c.pluginState = st;
  8697. c.samplePath = dir;
  8698. auto inst = lynx::makeInstrument (lynx::InstrumentKind::bassdrum);
  8699. inst->configure (c, 120.0);
  8700. inst->prepare (44100.0);
  8701. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  8702. for (int b = 0; b < 16; ++b)
  8703. {
  8704. if (b == 0)
  8705. inst->noteOn (60, 0.78125f);
  8706. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  8707. }
  8708. const auto ref = lynx::bassdrum::renderReference (st, { { 0, 60, (std::pow (11.0, 1.28 * 0.78125) - 1.0) / 10.0, 1.0, 0.0, 0.0, -1 } }, 44100.0, 8192, dir);
  8709. double diff = 0.0, energy = 0.0;
  8710. for (int i = 0; i < 8192; ++i)
  8711. {
  8712. diff = std::max ({ diff, (double) std::abs (l[(std::size_t) i] - ref[2 * (std::size_t) i]), (double) std::abs (r[(std::size_t) i] - ref[2 * (std::size_t) i + 1]) });
  8713. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  8714. }
  8715. CHECK (diff == 0.0 && energy > 1e-2);
  8716. }
  8717. // Workflow P2 (docs/WORKFLOW_FL.md)
  8718. #include "WorkflowP2Tests.inc"
  8719. TEST (chorus_family_engines)
  8720. {
  8721. // REA_rodada59: goldens from the installation's DLLs (44.1 kHz, blocks of 512) on this signal
  8722. std::vector<float> inL (4096), inR (4096);
  8723. for (int n = 0; n < 4096; ++n)
  8724. {
  8725. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0) * 0.5f;
  8726. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0) * 0.5f;
  8727. }
  8728. struct Gold { int i; float l, r; };
  8729. auto close = [] (float a, float b) { return std::abs (a - b) <= 2.0e-7f * std::max (1.0f, std::abs (b)); };
  8730. auto run = [&] (const char* plugin, const std::vector<std::uint8_t>& st, lynx::EffectKind kind, std::initializer_list<Gold> gold) {
  8731. const auto slot = lynx::effectFromFl (plugin, st, 120.0);
  8732. CHECK (slot && slot->kind == kind);
  8733. if (! slot)
  8734. return;
  8735. auto fx = lynx::makeEffect (*slot);
  8736. fx->prepare (44100.0);
  8737. std::vector<float> l (inL), r (inR);
  8738. fx->process (l.data(), r.data(), 1024, slot->params, 120.0);
  8739. fx->process (l.data() + 1024, r.data() + 1024, 3072, slot->params, 120.0);
  8740. for (const auto& g : gold)
  8741. CHECK (close (l[(std::size_t) g.i], g.l) && close (r[(std::size_t) g.i], g.r));
  8742. };
  8743. auto ints = [] (std::initializer_list<int> v) {
  8744. std::vector<std::uint8_t> b;
  8745. for (int x : v)
  8746. for (int k = 0; k < 4; ++k)
  8747. b.push_back ((std::uint8_t) ((unsigned) x >> (8 * k)));
  8748. return b;
  8749. };
  8750. // Fruity Chorus (start phases 0, which FL draws at random)
  8751. run ("Fruity Chorus", ints ({ 1, 512, 2250, 341, 1500, 2500, 3500, 23, 0, 0, 0, 2500, 0 }), lynx::EffectKind::fruityChorus,
  8752. { { 100, 0.0f, -0.3850000202655792f }, { 1000, -0.6181581020355225f, 0.2707735002040863f }, { 2047, -0.01424609124660492f, 0.24228274822235107f },
  8753. { 3000, -1.1650776863098145f, -0.1809898018836975f }, { 4095, -0.012790083885192871f, 0.060571759939193726f } });
  8754. run ("Fruity Chorus", ints ({ 1, 700, 4000, 900, 4000, 3000, 2000, 400, 700, 1000, 30, 4999, 0 }), lynx::EffectKind::fruityChorus,
  8755. { { 100, 0.0f, -0.3850000202655792f }, { 1000, -0.5833333134651184f, 0.36345374584198f }, { 2047, -0.0336097776889801f, -0.3360772430896759f },
  8756. { 3000, -0.40196430683135986f, -0.5073210597038269f }, { 4095, -0.1176539734005928f, 0.444706529378891f } });
  8757. run ("Fruity Chorus", ints ({ 1, 36, 469, 1004, 2832, 293, 3500, 0, 428, 0, 0, 3887, 1024 }), lynx::EffectKind::fruityChorus,
  8758. { { 100, 0.2975943684577942f, 0.3747217655181885f }, { 1000, -0.39463338255882263f, 0.18099871277809143f }, { 2047, 0.3894335627555847f, 0.35111793875694275f },
  8759. { 3000, -0.1450725942850113f, 0.2847272455692291f }, { 4095, 0.33678582310676575f, -0.13365517556667328f } });
  8760. {
  8761. const auto slot = lynx::effectFromFl ("Fruity Chorus", ints ({ 1, 36, 469, 1004, 2832, 293, 3500, 0, 428, 0, 0, 3887, 1024 }), 120.0);
  8762. CHECK (slot && lynx::fruitychorusfx::fromParams (slot->params.data())[2] == 1004);
  8763. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityChorus, 11, 1.0f, 120.0) == "on");
  8764. CHECK (lynx::effectParamFromFl ("Fruity Chorus", 11).has_value() && ! lynx::effectParamFromFl ("Fruity Chorus", 12).has_value());
  8765. }
  8766. // a state FL does not read (version 2) or too short keeps the generic chorus
  8767. CHECK (! lynx::effectFromFl ("Fruity Chorus", ints ({ 2, 512, 2250, 341, 1500, 2500, 3500, 23, 0, 0, 0, 2500, 0 }), 120.0).has_value()
  8768. || lynx::effectFromFl ("Fruity Chorus", ints ({ 2, 512, 2250, 341, 1500, 2500, 3500, 23, 0, 0, 0, 2500, 0 }), 120.0)->kind != lynx::EffectKind::fruityChorus);
  8769. CHECK (! lynx::effectFromFl ("Fruity Chorus", { 1, 0, 0, 0 }, 120.0).has_value()
  8770. || lynx::effectFromFl ("Fruity Chorus", { 1, 0, 0, 0 }, 120.0)->kind != lynx::EffectKind::fruityChorus);
  8771. // Hyper Chorus engine in FL's load sequence (defaults, then the state: the parameters glide), instance k's phases
  8772. auto hyper = [&] (std::initializer_list<int> raws, int instance, std::initializer_list<Gold> gold) {
  8773. namespace hc = lynx::hyperchorusfx;
  8774. hc::Engine e (instance);
  8775. const std::vector<int> r (raws);
  8776. auto val = [] (int i, int raw) { return (float) std::clamp ((double) (raw - hc::rawMin[(std::size_t) i]) * (1.0 / (double) (hc::rawMax[(std::size_t) i] - hc::rawMin[(std::size_t) i])), 0.0, 1.0); };
  8777. for (int i = 0; i < hc::params; ++i)
  8778. e.setParam (i, val (i, hc::defaults[(std::size_t) i]));
  8779. for (int i = 0; i < hc::params; ++i)
  8780. e.setParam (i, val (i, r[(std::size_t) i]));
  8781. e.setRate (44100.0);
  8782. std::vector<float> io (8192);
  8783. for (int n = 0; n < 4096; ++n)
  8784. io[2 * (std::size_t) n] = inL[(std::size_t) n], io[2 * (std::size_t) n + 1] = inR[(std::size_t) n];
  8785. for (int s = 0; s < 4096; s += 512)
  8786. e.render (io.data() + 2 * s, io.data() + 2 * s, 512);
  8787. for (const auto& g : gold)
  8788. CHECK (close (io[2 * (std::size_t) g.i], g.l) && close (io[2 * (std::size_t) g.i + 1], g.r));
  8789. };
  8790. hyper ({ 562, 919, 499, 505, 709, 716, 1, 19247, 10900, 191, 1000 }, 0,
  8791. { { 100, 0.0f, -0.4115639328956604f }, { 1000, -0.658551037311554f, 0.28647756576538086f }, { 2047, 0.24743860960006714f, -0.5088979005813599f },
  8792. { 3000, -0.5171564221382141f, -0.3630276620388031f }, { 4095, 0.08128821104764938f, 0.18811003863811493f } });
  8793. hyper ({ 1000, 555, 0, 400, 300, 300, 0, 22000, 120, 368, 750 }, 2,
  8794. { { 100, 0.0f, -0.398922324180603f }, { 1000, -1.1528236865997314f, 0.2839684784412384f }, { 2047, 0.5115746855735779f, -0.3330395221710205f },
  8795. { 3000, -0.6094785928726196f, 0.17040175199508667f }, { 4095, -0.11666109412908554f, 0.21931695938110352f } });
  8796. CHECK (lynx::hyperchorusfx::randState (1) == 1u * 214013u + 2531011u);
  8797. {
  8798. // the import: controls as parameters; a song's Hyper Chorus slots are numbered in mixer order
  8799. const auto st = ints ({ 2, 1000, 555, 0, 400, 300, 300, 0, 22000, 120, 368, 750, 0 });
  8800. const auto slot = lynx::effectFromFl ("Hyper Chorus", st, 120.0);
  8801. CHECK (slot && slot->kind == lynx::EffectKind::hyperChorus && lynx::hyperchorusfx::fromParams (slot->params.data())[9] == 368);
  8802. lynx::Song song;
  8803. song.mixer.resize (3);
  8804. song.mixer[1].effects = { *slot, *slot };
  8805. song.mixer[2].effects = { *slot };
  8806. lynx::numberChorusInstances (song);
  8807. CHECK (lynx::hyperchorusfx::instanceOf (song.mixer[2].effects[0].state) == 2);
  8808. auto fx = lynx::makeEffect (song.mixer[2].effects[0]);
  8809. fx->prepare (48000.0);
  8810. std::vector<float> l (inL), r (inR);
  8811. fx->process (l.data(), r.data(), 4096, slot->params, 120.0);
  8812. bool finite = true, wet = false;
  8813. for (int n = 0; n < 4096; ++n)
  8814. {
  8815. finite = finite && std::isfinite (l[(std::size_t) n]) && std::isfinite (r[(std::size_t) n]);
  8816. wet = wet || l[(std::size_t) n] != inL[(std::size_t) n];
  8817. }
  8818. CHECK (finite && wet);
  8819. CHECK (lynx::formatEffectParam (lynx::EffectKind::hyperChorus, 8, 0.0f, 120.0) == "off");
  8820. CHECK (! lynx::effectFromFl ("Hyper Chorus", ints ({ 3, 1000, 555, 0, 400, 300, 300, 0, 22000, 120, 368, 750, 0 }), 120.0).has_value()
  8821. || lynx::effectFromFl ("Hyper Chorus", ints ({ 3, 1000, 555, 0, 400, 300, 300, 0, 22000, 120, 368, 750, 0 }), 120.0)->kind != lynx::EffectKind::hyperChorus);
  8822. }
  8823. // Vintage Chorus engine as FL drives it (created, sample rate, each control), fresh instance (its smoothed values
  8824. // start from the engine's initial ones)
  8825. auto vintage = [&] (std::initializer_list<int> raws, std::initializer_list<Gold> gold) {
  8826. namespace vc = lynx::vintagechorusfx;
  8827. vc::Engine e;
  8828. e.setRate (44100.0);
  8829. int i = 0;
  8830. for (int raw : raws)
  8831. {
  8832. e.setParam (i, (float) std::clamp ((double) (raw - vc::rawMin[(std::size_t) i]) * (1.0 / (double) (vc::rawMax[(std::size_t) i] - vc::rawMin[(std::size_t) i])), 0.0, 1.0));
  8833. ++i;
  8834. }
  8835. std::vector<float> io (8192);
  8836. for (int n = 0; n < 4096; ++n)
  8837. io[2 * (std::size_t) n] = inL[(std::size_t) n], io[2 * (std::size_t) n + 1] = inR[(std::size_t) n];
  8838. for (int s = 0; s < 4096; s += 512)
  8839. e.render (io.data() + 2 * s, io.data() + 2 * s, 512);
  8840. for (const auto& g : gold)
  8841. CHECK (close (io[2 * (std::size_t) g.i], g.l) && close (io[2 * (std::size_t) g.i + 1], g.r));
  8842. };
  8843. vintage ({ 2, 0, 211046, 1, 2411, 960, 1985, 5121, 0, 1, 4420, 6000, 273, 0, 0, 0, 0 },
  8844. { { 100, 0.0f, -0.05606573447585106f }, { 1000, -0.3413679003715515f, 0.20469821989536285f }, { 2047, 0.1487489938735962f, -0.28199973702430725f },
  8845. { 3000, -0.31699514389038086f, -0.43230289220809937f }, { 4095, 0.1255197525024414f, 0.0810704305768013f } });
  8846. vintage ({ 2, 0, 80, 0, 0, 60, 3000, 3000, 0, 0, 0, 3000, 0, 3000, 0, 0, 0 }, // hiss
  8847. { { 100, -1.2527654007499223e-06f, -0.053655341267585754f }, { 1000, -0.44306400418281555f, 0.25606146454811096f }, { 2047, 0.23425261676311493f, -0.21375307440757751f },
  8848. { 3000, -0.5793180465698242f, -0.13371579349040985f }, { 4095, 0.05595953390002251f, 0.11362095922231674f } });
  8849. vintage ({ 0, 1, 49152, 0, 3000, 60, 3000, 3000, 0, 0, 0, 3000, 0, 0, 0, 0, 0 }, // Chorus 1 + 2
  8850. { { 100, 0.0f, -0.05365874245762825f }, { 1000, -0.5095958113670349f, 0.2809423506259918f }, { 2047, 0.17256303131580353f, -0.3563305139541626f },
  8851. { 3000, -0.471851646900177f, -0.30123069882392883f }, { 4095, 0.0959278866648674f, 0.13436323404312134f } });
  8852. {
  8853. const auto st = ints ({ 1, 2, 0, 211046, 1, 2411, 960, 1985, 5121, 0, 1, 4420, 6000, 273, 0, 0, 0, 0 });
  8854. const auto slot = lynx::effectFromFl ("Vintage Chorus", st, 120.0);
  8855. CHECK (slot && slot->kind == lynx::EffectKind::vintageChorus && lynx::vintagechorusfx::fromParams (slot->params.data())[2] == 211046);
  8856. auto fx = lynx::makeEffect (*slot);
  8857. fx->prepare (44100.0);
  8858. std::vector<float> l (inL), r (inR);
  8859. fx->process (l.data(), r.data(), 4096, slot->params, 120.0);
  8860. bool finite = true;
  8861. for (int n = 0; n < 4096; ++n)
  8862. finite = finite && std::isfinite (l[(std::size_t) n]) && std::isfinite (r[(std::size_t) n]);
  8863. CHECK (finite && l[3000] != inL[3000]);
  8864. CHECK (lynx::formatEffectParam (lynx::EffectKind::vintageChorus, 0, 0.0f, 120.0) == "Chorus 1");
  8865. CHECK (! lynx::effectFromFl ("Vintage Chorus", ints ({ 2, 2, 0, 211046, 1, 2411, 960, 1985, 5121, 0, 1, 4420, 6000, 273, 0, 0, 0, 0 }), 120.0).has_value()
  8866. || lynx::effectFromFl ("Vintage Chorus", ints ({ 2, 2, 0, 211046, 1, 2411, 960, 1985, 5121, 0, 1, 4420, 6000, 273, 0, 0, 0, 0 }), 120.0)->kind != lynx::EffectKind::vintageChorus);
  8867. }
  8868. }