tests.cpp 771 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835
  1. // Core tests (no JUCE). Usage: lynx_tests [folder-with-flp-files]
  2. #include <optional>
  3. #include <atomic>
  4. #include <chrono>
  5. #include <random>
  6. #include <cctype>
  7. #include <cmath>
  8. #include <cstdio>
  9. #include <cstring>
  10. #include <filesystem>
  11. #include <map>
  12. #include <set>
  13. #include <sstream>
  14. #include <fstream>
  15. #include <functional>
  16. #include <iterator>
  17. #include <string>
  18. #include <tuple>
  19. #include <vector>
  20. #include "core/Dither.h"
  21. #include "core/DrumSynthLive.h"
  22. #include "core/Effects.h"
  23. #include "core/Effector.h"
  24. #include "core/FruityDelayBank.h"
  25. #include "core/FruityMultiband.h"
  26. #include "core/Denormals.h"
  27. #include "core/FlpImport.h"
  28. #include "core/FlEffects.h"
  29. #include "core/FlPatcher.h"
  30. #include "core/FlSamplers.h"
  31. #include "core/FlSynths.h"
  32. #include "core/Inflate.h"
  33. #include "core/WavPack.h"
  34. #include "core/FlWrapper.h"
  35. #include "core/FlPluginDb.h"
  36. #include "core/MissingFiles.h"
  37. #include "core/PianoRollTools.h"
  38. #include "core/EventEdit.h"
  39. #include "core/Preferences.h"
  40. #include "core/History.h"
  41. #include "core/Gms.h"
  42. #include "core/PoiZone.h"
  43. #include "core/MiniSynth.h"
  44. #include "core/SimSynth.h"
  45. #include "core/Granulizer.h"
  46. #include "core/Drumaxx.h"
  47. #include "core/Wasp.h"
  48. #include "core/Plucked.h"
  49. #include "core/BeepMap.h"
  50. #include "core/Sawer.h"
  51. #include "core/Slayer.h"
  52. #include "core/Toxic.h"
  53. #include "core/Harmless.h"
  54. #include "core/Harmor.h"
  55. #include "core/Morphine.h"
  56. #include "core/Ogun.h"
  57. #include "core/BassDrum.h"
  58. #include "core/FruitKick.h"
  59. #include "core/IppCos.h"
  60. #include "core/Dx10.h"
  61. #include "core/Kepler.h"
  62. #include "core/ThreeOsc.h"
  63. #include "core/FlResampler.h"
  64. #include "core/FlKeys.h"
  65. #include "core/DirectWave.h"
  66. #include "core/OggVorbis.h"
  67. #include "core/FlVoiceProcessor.h"
  68. #include "core/TransistorBass.h"
  69. #include "core/HarmlessFx.h"
  70. #include "core/MaximusFx.h"
  71. #include "core/Reeverb2.h"
  72. #include "core/LuxeVerb.h"
  73. #include "core/Denormals.h"
  74. #include "core/FruityDynamics.h"
  75. #include "core/FruityShapers.h"
  76. #include "core/ParametricEqFl.h"
  77. #include "core/FruityBasicFx.h"
  78. #include "core/EquoFx.h"
  79. #include "core/DistructorFx.h"
  80. #include "core/MultibandDelayFx.h"
  81. #include "core/PitchShifterFl.h"
  82. #include "core/FruityFilters.h"
  83. #include "core/NewtoneFx.h"
  84. #include "core/FruityDelay.h"
  85. #include "core/StereoFx.h"
  86. #include "core/FrequencySplitter.h"
  87. #include "core/ChorusFx.h"
  88. #include "core/GrossBeat.h"
  89. #include "core/LovePhilter.h"
  90. #include "core/FlangusFastDist.h"
  91. #include "core/FruityVocoder.h"
  92. #include "core/DelphiMath.h"
  93. #include "core/LiveEventQueue.h"
  94. #include "core/RackRenderer.h"
  95. #include "core/Scheduler.h"
  96. #include "core/SnapshotExchange.h"
  97. #include "core/SoftClip.h"
  98. #include "core/Slicer.h"
  99. #include "core/SytrusEngine.h"
  100. #include "core/Templates.h"
  101. #include "core/Vocodex.h"
  102. #include "core/TimeStretch.h"
  103. #include "core/WorkerPool.h"
  104. #include "core/Song.h"
  105. #include "core/NoteEdit.h"
  106. #include "core/Recorder.h"
  107. #include "core/KeyMap.h"
  108. #include "core/RackOps.h"
  109. #include "core/ValueMath.h"
  110. #include "core/Workspace.h"
  111. #include "core/StartupSound.h"
  112. #include "core/ExportOptions.h"
  113. #include "core/Mp3Encoder.h"
  114. #include "core/PluginMonitor.h"
  115. #include "core/PlaylistOps.h"
  116. #include "core/TouchKeyboard.h"
  117. #include "core/HeldNotes.h"
  118. #include "core/Templates.h"
  119. #include "flp/Flp.h"
  120. #include "ui/I18nData.h"
  121. namespace
  122. {
  123. int failures = 0;
  124. int checks = 0;
  125. std::vector<std::pair<const char*, std::function<void()>>>& registry()
  126. {
  127. static std::vector<std::pair<const char*, std::function<void()>>> r;
  128. return r;
  129. }
  130. struct Register
  131. {
  132. Register (const char* name, std::function<void()> fn) { registry().emplace_back (name, std::move (fn)); }
  133. };
  134. #define TEST(name) \
  135. static void name(); \
  136. static Register reg_##name (#name, name); \
  137. static void name()
  138. #define CHECK(cond) \
  139. do \
  140. { \
  141. ++checks; \
  142. if (! (cond)) \
  143. { \
  144. ++failures; \
  145. std::printf (" FAILED %s:%d: %s\n", __FILE__, __LINE__, #cond); \
  146. } \
  147. } while (false)
  148. std::string realFlpDir;
  149. // --- helpers for building synthetic .flp files -----------------------------
  150. struct FlpBuilder
  151. {
  152. std::vector<std::uint8_t> bytes;
  153. FlpBuilder (std::uint16_t ppq = 96)
  154. {
  155. put ("FLhd");
  156. u32 (6);
  157. u16 (0);
  158. u16 (1);
  159. u16 (ppq);
  160. put ("FLdt");
  161. u32 (0); // size is not used by the reader
  162. }
  163. void put (const char* s) { bytes.insert (bytes.end(), s, s + std::strlen (s)); }
  164. void u8 (std::uint8_t v) { bytes.push_back (v); }
  165. void u16 (std::uint16_t v) { u8 ((std::uint8_t) v); u8 ((std::uint8_t) (v >> 8)); }
  166. void u32 (std::uint32_t v) { u16 ((std::uint16_t) v); u16 ((std::uint16_t) (v >> 16)); }
  167. void varint (std::size_t n)
  168. {
  169. do
  170. {
  171. auto b = (std::uint8_t) (n & 0x7F);
  172. n >>= 7;
  173. u8 (n ? (std::uint8_t) (b | 0x80) : b);
  174. } while (n);
  175. }
  176. void data (std::uint8_t id, const std::vector<std::uint8_t>& payload)
  177. {
  178. u8 (id);
  179. varint (payload.size());
  180. bytes.insert (bytes.end(), payload.begin(), payload.end());
  181. }
  182. void ascii (std::uint8_t id, const std::string& s)
  183. {
  184. std::vector<std::uint8_t> p (s.begin(), s.end());
  185. p.push_back (0);
  186. data (id, p);
  187. }
  188. void utf16 (std::uint8_t id, const std::u16string& s)
  189. {
  190. std::vector<std::uint8_t> p;
  191. for (auto c : s)
  192. {
  193. p.push_back ((std::uint8_t) c);
  194. p.push_back ((std::uint8_t) (c >> 8));
  195. }
  196. p.push_back (0);
  197. p.push_back (0);
  198. data (id, p);
  199. }
  200. };
  201. std::vector<std::uint8_t> noteRecord (std::uint32_t pos, std::uint16_t channel, std::uint32_t len, std::uint16_t key,
  202. std::uint8_t velocity)
  203. {
  204. std::vector<std::uint8_t> r (24, 0);
  205. std::memcpy (&r[0], &pos, 4);
  206. std::memcpy (&r[6], &channel, 2);
  207. std::memcpy (&r[8], &len, 4);
  208. std::memcpy (&r[12], &key, 2);
  209. r[16] = 120;
  210. r[20] = 64;
  211. r[21] = velocity;
  212. return r;
  213. }
  214. } // namespace
  215. // ---------------------------------------------------------------------------
  216. // FLP
  217. TEST (flp_rejects_garbage)
  218. {
  219. std::vector<std::uint8_t> junk (64, 0x41);
  220. bool threw = false;
  221. try { flp::EventReader r (junk); } catch (const flp::Error&) { threw = true; }
  222. CHECK (threw);
  223. }
  224. TEST (flp_reads_size_classes_varint_and_extended_ids)
  225. {
  226. FlpBuilder b;
  227. b.u8 (0x15); b.u8 (2); // u8
  228. b.u8 (0x40); b.u16 (513); // u16
  229. b.u8 (0x9C); b.u32 (128500); // u32
  230. b.data (0xD5, std::vector<std::uint8_t> (300, 7)); // 2-byte varint
  231. b.u8 (0xAC); b.u32 (0xC0000100); b.varint (3); b.u8 (1); b.u8 (2); b.u8 (3); // extended ID
  232. flp::EventReader r (b.bytes);
  233. flp::Event e;
  234. CHECK (r.header().ppq == 96);
  235. CHECK (r.next (e) && e.id == 0x15 && e.value == 2);
  236. CHECK (r.next (e) && e.id == 0x40 && e.value == 513);
  237. CHECK (r.next (e) && e.id == 0x9C && e.value == 128500);
  238. CHECK (r.next (e) && e.id == 0xD5 && e.data.size() == 300 && e.data[299] == 7);
  239. CHECK (r.next (e) && e.id == 0xC0000100 && e.isData() && e.data.size() == 3 && e.data[2] == 3);
  240. CHECK (! r.next (e));
  241. }
  242. TEST (flp_truncated_file_throws)
  243. {
  244. FlpBuilder b;
  245. b.u8 (0x9C); b.u8 (1); // u32 with only 1 byte
  246. flp::EventReader r (b.bytes);
  247. flp::Event e;
  248. bool threw = false;
  249. try { r.next (e); } catch (const flp::Error&) { threw = true; }
  250. CHECK (threw);
  251. }
  252. TEST (flp_version_numbers)
  253. {
  254. CHECK (flp::parseVersionNumber ("25.2.5.5319") == 25002005u);
  255. CHECK (flp::parseVersionNumber ("20.99.3000.3031") == 20102000u);
  256. CHECK (flp::parseVersionNumber ("11.0.3") == 11000003u);
  257. }
  258. TEST (flp_playlist_record_size_by_version)
  259. {
  260. CHECK (flp::playlistRecordSize (12009003, 64) == 32);
  261. CHECK (flp::playlistRecordSize (21000000, 120) == 60);
  262. CHECK (flp::playlistRecordSize (25002005, 160) == 80);
  263. CHECK (flp::playlistRecordSize (25002005, 60) == 60); // size doesn't match the version -> tries the others
  264. CHECK (flp::playlistRecordSize (25002005, 7) == 0);
  265. }
  266. TEST (flp_parses_project_model)
  267. {
  268. FlpBuilder b (96);
  269. b.ascii (0xC7, "25.2.5.5319");
  270. b.u8 (0x9C); b.u32 (174500);
  271. b.utf16 (0xC2, u"Título");
  272. b.u8 (0x41); b.u16 (3);
  273. b.u8 (0x96); b.u32 (0x00A14547); // BGR in the file
  274. auto notes = noteRecord (96, 1, 48, 64, 100);
  275. auto n2 = noteRecord (0, 0, 0, 60, 128);
  276. notes.insert (notes.end(), n2.begin(), n2.end());
  277. b.data (0xE0, notes);
  278. b.u8 (0x40); b.u16 (0);
  279. b.u8 (0x15); b.u8 (0);
  280. b.utf16 (0xCB, u"Kick");
  281. {
  282. std::vector<std::uint8_t> levels (24, 0);
  283. const std::int32_t lv[] = { 3200, 12800, -150 };
  284. std::memcpy (levels.data(), lv, sizeof (lv));
  285. b.data (0xDB, levels);
  286. }
  287. b.u8 (0x68); b.u16 (5);
  288. b.u8 (0x40); b.u16 (1);
  289. b.u8 (0x15); b.u8 (2);
  290. b.utf16 (0xC9, u"FLEX");
  291. b.utf16 (0xCB, u"Lead");
  292. b.u8 (0x63); b.u16 (0);
  293. std::vector<std::uint8_t> item (80, 0);
  294. std::uint32_t pos = 384, len = 768;
  295. std::uint16_t base = 20480, idx = 20480 + 3, rv = 499 - 2;
  296. std::memcpy (&item[0], &pos, 4);
  297. std::memcpy (&item[4], &base, 2);
  298. std::memcpy (&item[6], &idx, 2);
  299. std::memcpy (&item[8], &len, 4);
  300. std::memcpy (&item[12], &rv, 2);
  301. b.data (0xE9, item);
  302. b.utf16 (0xCC, u"Drums");
  303. b.data (0xEC, std::vector<std::uint8_t> (12, 0));
  304. b.data (0xEC, std::vector<std::uint8_t> (12, 0));
  305. const auto p = flp::parseProject (b.bytes);
  306. CHECK (p.version == "25.2.5.5319");
  307. CHECK (std::abs (p.tempo - 174.5) < 1e-9);
  308. CHECK (p.title == "Título");
  309. CHECK (p.channels.size() == 2);
  310. CHECK (p.channels[0].name == "Kick" && p.channels[0].mixerTrack == 5);
  311. CHECK (p.channels[0].hasLevels && p.channels[0].pan == 3200 && p.channels[0].volume == 12800 && p.channels[0].pitchCents == -150);
  312. CHECK (p.channels[1].type == flp::ChannelType::instrument && p.channels[1].pluginName == "FLEX");
  313. CHECK (p.patterns.size() == 1 && p.patterns[0].index == 3);
  314. CHECK (p.patterns[0].colour && *p.patterns[0].colour == 0x4745A1);
  315. CHECK (p.patterns[0].notes.size() == 2 && p.patterns[0].notes[0].position == 0); // sorted
  316. CHECK (p.patterns[0].notes[1].key == 64 && p.patterns[0].notes[1].length == 48);
  317. CHECK (p.arrangements.size() == 1 && p.arrangements[0].items.size() == 1);
  318. const auto& it = p.arrangements[0].items[0];
  319. CHECK (it.kind == flp::PlaylistItem::Kind::pattern && it.index == 3 && it.track == 2 && it.position == 384);
  320. CHECK (p.mixer.size() == 2 && p.mixer[0].name == "Drums" && p.mixer[1].name.empty());
  321. CHECK (p.warnings.empty());
  322. }
  323. TEST (flp_fuzz_corrupted_files_do_not_crash)
  324. {
  325. if (realFlpDir.empty())
  326. return;
  327. // some real .flp files, corrupted in various ways: the parser may reject them (flp::Error), but
  328. // must never hang, read past the buffer or produce a Song with invalid indices
  329. std::vector<std::vector<std::uint8_t>> sources;
  330. for (auto& entry : std::filesystem::recursive_directory_iterator (realFlpDir))
  331. if (entry.path().extension() == ".flp" && sources.size() < 6 && std::filesystem::file_size (entry.path()) < 3'000'000)
  332. {
  333. std::ifstream in (entry.path(), std::ios::binary);
  334. sources.emplace_back ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  335. }
  336. std::uint32_t seed = 2024;
  337. auto rnd = [&] (std::uint32_t n) { seed = seed * 1664525u + 1013904223u; return n == 0 ? 0u : (seed >> 8) % n; };
  338. int parsed = 0, rejected = 0;
  339. for (int round = 0; round < 240 && ! sources.empty(); ++round)
  340. {
  341. auto bytes = sources[(std::size_t) rnd ((std::uint32_t) sources.size())];
  342. switch (round % 4)
  343. {
  344. case 0: bytes.resize (rnd ((std::uint32_t) bytes.size())); break; // truncated
  345. 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;
  346. case 2: // absurd event size
  347. for (int k = 0; k < 5; ++k)
  348. bytes[(std::size_t) rnd ((std::uint32_t) bytes.size())] = 0xFF;
  349. break;
  350. default: // shuffled block
  351. {
  352. const auto a = rnd ((std::uint32_t) bytes.size()), b = rnd ((std::uint32_t) bytes.size());
  353. std::reverse (bytes.begin() + std::min (a, b), bytes.begin() + std::max (a, b));
  354. break;
  355. }
  356. }
  357. try
  358. {
  359. const auto project = flp::parseProject (bytes);
  360. const auto song = lynx::importFlp (project);
  361. ++parsed;
  362. for (const auto& p : song.patterns)
  363. for (const auto& n : p.notes)
  364. CHECK (n.channel >= 0 && n.channel < (int) song.channels.size());
  365. for (const auto& c : song.playlist)
  366. CHECK (c.kind != lynx::ClipKind::pattern || (c.pattern >= 0 && c.pattern < (int) song.patterns.size()));
  367. }
  368. catch (const flp::Error&)
  369. {
  370. ++rejected;
  371. }
  372. }
  373. std::printf (" fuzz: %d imported, %d rejected\n", parsed, rejected);
  374. CHECK (parsed + rejected == 240);
  375. }
  376. TEST (plugin_state_decoders_survive_garbage)
  377. {
  378. // random blobs (varied sizes, including valid headers followed by garbage)
  379. std::uint32_t seed = 77;
  380. auto rnd = [&] { seed = seed * 1664525u + 1013904223u; return seed >> 8; };
  381. const char* plugins[] = { "Fruity Parametric EQ 2", "Fruity Limiter", "Fruity Reeverb 2", "Fruity Delay 2", "Fruity Delay 3",
  382. "Fruity Compressor", "Fruity Balance", "Fruity PanOMatic", "Fruity Stereo Shaper", "Fruity Phase Inverter" };
  383. int decoded = 0;
  384. for (int round = 0; round < 3000; ++round)
  385. {
  386. std::vector<std::uint8_t> blob ((std::size_t) (rnd() % 600));
  387. for (auto& b : blob)
  388. b = (std::uint8_t) rnd();
  389. if (blob.size() >= 4 && round % 3 == 0) // versions the readers accept
  390. {
  391. const std::uint32_t versions[] = { 8, 10001, 10000, 12, 14, 6, 1 };
  392. const auto v = versions[rnd() % 7];
  393. std::memcpy (blob.data(), &v, 4);
  394. }
  395. if (auto fx = lynx::effectFromFl (plugins[rnd() % 10], blob))
  396. {
  397. ++decoded;
  398. for (auto v : fx->params)
  399. CHECK (std::isfinite (v) && v >= 0.0f && v <= 1.0f);
  400. }
  401. lynx::SynthSettings synth;
  402. if (lynx::synthFrom3xOsc (blob, synth))
  403. for (const auto& o : synth.osc)
  404. CHECK (std::isfinite (o.level) && o.level >= 0.0f && o.level <= 1.0f);
  405. float harmlessGain = 1.0f;
  406. if (lynx::synthFromHarmless (blob, 120.0, synth, harmlessGain))
  407. CHECK (std::isfinite (harmlessGain) && std::isfinite (synth.cutoff) && std::isfinite (synth.attack));
  408. if (lynx::synthFromFruitKick (blob, synth))
  409. CHECK (std::isfinite (synth.decay) && std::isfinite (synth.pitchEnv.amount));
  410. if (lynx::synthFromGms (blob, 120.0, synth, harmlessGain) || lynx::synthFromHarmor (blob, synth, harmlessGain) || lynx::synthFromOgun (blob, synth, harmlessGain)
  411. || lynx::synthFromSakura (blob, synth) || lynx::synthFromKepler (blob, synth, harmlessGain))
  412. CHECK (std::isfinite (synth.cutoff) && std::isfinite (synth.attack) && std::isfinite (harmlessGain));
  413. if (auto sound = lynx::drumSynthLiveSound (blob))
  414. CHECK (sound->size() < 256);
  415. if (auto wave = lynx::waveTravellerSample (blob))
  416. CHECK (! wave->first.empty() && wave->second >= 0 && wave->second <= 127);
  417. lynx::synthFromBooBass (blob, synth);
  418. lynx::gmsStateUsable (blob); // (the exact engines' parsers reject garbage without crashing)
  419. lynx::morphineStateUsable (blob);
  420. lynx::poizoneStateUsable (blob);
  421. if (lynx::synthFromPoiZone (blob, synth, harmlessGain) || lynx::synthFromBassDrum (blob, synth, harmlessGain) || lynx::synthFromToxic (blob, synth, harmlessGain)
  422. || lynx::synthFromMorphine (blob, synth, harmlessGain))
  423. CHECK (std::isfinite (synth.cutoff) && std::isfinite (harmlessGain));
  424. if (auto patch = lynx::drumpadPatchName (blob))
  425. CHECK (patch->size() <= 0x30 && std::isfinite (lynx::drumToneForName (*patch).decay));
  426. if (auto flex = lynx::flexPresetName (blob))
  427. CHECK ((! flex->first.empty() || ! flex->second.empty()) && flex->second.size() <= 1024);
  428. if (lynx::synthFromSytrus (blob, 120.0, synth, harmlessGain))
  429. CHECK (std::isfinite (synth.cutoff) && std::isfinite (synth.attack));
  430. if (lynx::synthFromDX10 (blob, synth))
  431. CHECK (std::isfinite (synth.decay) && std::isfinite (synth.release) && std::isfinite (synth.osc[1].level));
  432. if (blob.size() >= 4 && round % 5 == 0) // MiniSynth header followed by garbage
  433. {
  434. std::memcpy (blob.data(), "SSLF", 4);
  435. if (blob.size() > 220)
  436. {
  437. std::memcpy (blob.data() + 40, "DATA", 4);
  438. std::memcpy (blob.data() + 48, "0rev", 4);
  439. }
  440. }
  441. if (lynx::synthFromMiniSynth (blob, 120.0, synth))
  442. CHECK (std::isfinite (synth.cutoff) && std::isfinite (synth.decay) && std::isfinite (synth.attack));
  443. if (lynx::miniSynthStateUsable (blob)) // the exact engine's parser and a short render with silent tables
  444. {
  445. const auto params = lynx::miniSynthParams (blob);
  446. CHECK (params.size() == 43u && std::all_of (params.begin(), params.end(), [] (float v) { return v >= 0.0f && v <= 1.0f; }));
  447. const auto y = lynx::minisynth::renderReference (blob, { { 0, 60, 1.0, 1.0, 0.0, 0.0, 64 } }, 44100.0, 128, "");
  448. CHECK (y.size() == 256u && std::all_of (y.begin(), y.end(), [] (float v) { return std::isfinite (v); }));
  449. }
  450. lynx::ForeignPlugin foreign;
  451. lynx::parseFlWrapper (blob, foreign);
  452. std::vector<std::uint8_t> component, controller;
  453. lynx::splitFlVst3State (blob, component, controller);
  454. }
  455. CHECK (decoded == 3000); // every known name yields an effect (default when the blob is unusable)
  456. }
  457. TEST (inflate_zlib)
  458. {
  459. // fixed Huffman with back-references
  460. const std::uint8_t fixed[] = { 0x78, 0xda, 0x4b, 0x4c, 0x4a, 0x4e, 0xc4, 0x86, 0x22, 0x00, 0x7c, 0x69, 0x09, 0x89 };
  461. const auto a = lynx::inflateZlib (fixed, sizeof (fixed));
  462. CHECK (a && std::string (a->begin(), a->end()) == "abcabcabcabcabcabcabcabcX");
  463. // stored (uncompressed) block
  464. const std::uint8_t stored[] = { 0x78, 0x01, 0x01, 0x03, 0x00, 0xfc, 0xff, 'L', 'Y', 'N', 0x00, 0x00, 0x00, 0x00 };
  465. const auto b = lynx::inflateZlib (stored, sizeof (stored));
  466. CHECK (b && std::string (b->begin(), b->end()) == "LYN");
  467. // truncated, bad header and size limit
  468. CHECK (! lynx::inflateZlib (fixed, 6));
  469. const std::uint8_t bad[] = { 0x12, 0x34, 0x00 };
  470. CHECK (! lynx::inflateZlib (bad, sizeof (bad)));
  471. CHECK (! lynx::inflateZlib (fixed, sizeof (fixed), false, 10));
  472. }
  473. TEST (fl_round15_effects_convert_known_values)
  474. {
  475. auto blob = [] (std::vector<std::int32_t> words) {
  476. std::vector<std::uint8_t> b (words.size() * 4);
  477. std::memcpy (b.data(), words.data(), b.size());
  478. return b;
  479. };
  480. // Frequency Shifter "+800 Hz": frequency 51542 in the 20 kHz range, 100% wet
  481. const auto fs = lynx::effectFromFl ("Frequency Shifter", blob ({ 4, 10000, 0, 51542, 0, 10000, 10000, 5000, 10000, 0, 5000, 0, 0 }));
  482. CHECK (fs && fs->kind == lynx::EffectKind::freqShift);
  483. if (fs)
  484. {
  485. const auto x = (fs->params[0] - 0.5) * 2.0;
  486. CHECK (std::abs (2000.0 * x * std::abs (x) - 800.0) < 10.0);
  487. CHECK (std::abs (fs->params[1] - 1.0f) < 1e-4f);
  488. }
  489. // Emphasis: input 0.0625 (= +1.5 dB) and ceiling 0.9308 (≈ −1.6 dB) -> limiter
  490. const auto em = lynx::effectFromFl ("Emphasis", blob ({ 8, 0, 0, 2, 1, 0, 0, 625, 10000, 10000, 9308, 0, 2, 7, 0 }));
  491. CHECK (em && em->kind == lynx::EffectKind::limiter && em->enabled);
  492. if (em)
  493. {
  494. CHECK (std::abs (em->params[1] * 24.0f - 1.5f) < 0.05f);
  495. CHECK (std::abs (em->params[0] * 12.0f - 12.0f + 1.6f) < 0.1f);
  496. }
  497. // Pitch Shifter +12 st, mix 100%
  498. 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 }));
  499. CHECK (ps && ps->kind == lynx::EffectKind::flPitchShifter && ps->params[0] == 1.0f && ps->params[3] == 1.0f); // REA_rodada61
  500. }
  501. TEST (frequency_shifter_moves_spectrum_by_hz)
  502. {
  503. auto fx = lynx::makeEffect (lynx::EffectKind::freqShift);
  504. fx->prepare (48000.0);
  505. auto slot = lynx::makeEffectSlot (lynx::EffectKind::freqShift);
  506. // +200 Hz: x·|x|·2000 = 200 -> x = sqrt(0.1)
  507. slot.params[0] = (float) (0.5 + std::sqrt (0.1) / 2.0);
  508. std::vector<float> l (48000), r (48000);
  509. for (int i = 0; i < 48000; ++i)
  510. l[(std::size_t) i] = r[(std::size_t) i] = (float) std::sin (2.0 * 3.141592653589793 * 1000.0 * i / 48000.0);
  511. for (int done = 0; done < 48000; done += 480)
  512. fx->process (l.data() + done, r.data() + done, 480, slot.params, 120.0);
  513. auto energy = [&] (double f) {
  514. const auto w = 2.0 * 3.141592653589793 * f / 48000.0, k = 2.0 * std::cos (w);
  515. double s1 = 0.0, s2 = 0.0;
  516. for (int i = 9600; i < 48000; ++i)
  517. {
  518. const auto s0 = l[(std::size_t) i] + k * s1 - s2;
  519. s2 = s1;
  520. s1 = s0;
  521. }
  522. return s1 * s1 + s2 * s2 - k * s1 * s2;
  523. };
  524. // the energy moves to 1200 Hz; little remains at 1000 and almost nothing at the image (800)
  525. CHECK (energy (1200.0) > 50.0 * energy (1000.0));
  526. CHECK (energy (1200.0) > 50.0 * energy (800.0));
  527. }
  528. TEST (pitch_shift_moves_frequency_keeps_length)
  529. {
  530. auto fx = lynx::makeEffect (lynx::EffectKind::pitchShift);
  531. fx->prepare (48000.0);
  532. auto slot = lynx::makeEffectSlot (lynx::EffectKind::pitchShift);
  533. auto crossingsAfter = [&] (float semitoneParam) {
  534. fx->reset();
  535. slot.params[0] = semitoneParam;
  536. std::vector<float> l (48000), r (48000);
  537. for (int i = 0; i < 48000; ++i)
  538. l[(std::size_t) i] = r[(std::size_t) i] = (float) std::sin (2.0 * 3.141592653589793 * 220.0 * i / 48000.0);
  539. for (int done = 0; done < 48000; done += 512)
  540. fx->process (l.data() + done, r.data() + done, std::min (512, 48000 - done), slot.params, 120.0);
  541. int crossings = 0;
  542. for (int i = 9600; i < 48000; ++i)
  543. crossings += (l[(std::size_t) i - 1] < 0.0f) != (l[(std::size_t) i] < 0.0f);
  544. for (auto v : l)
  545. CHECK (std::isfinite (v) && std::abs (v) < 1.6f);
  546. return crossings / 0.8 / 2.0; // cycles per second
  547. };
  548. CHECK (std::abs (crossingsAfter (0.5f) - 220.0) < 8.0); // 0 st
  549. CHECK (std::abs (crossingsAfter (0.5f + 12.0f / 48.0f) - 440.0) < 25.0); // +12 st
  550. CHECK (std::abs (crossingsAfter (0.5f - 12.0f / 48.0f) - 110.0) < 12.0); // −12 st
  551. }
  552. // A Gross Beat state in FL's layout (REA_rodada53 §1), built here: version 8, parameters, options, 72 slots with a name,
  553. // sync bytes and a map envelope (kind 3, version 3, points: Δx in beats, level, tension, curve type)
  554. namespace
  555. {
  556. struct GbPoint
  557. {
  558. double dx, y;
  559. std::uint8_t type;
  560. };
  561. std::vector<std::uint8_t> grossBeatState (const std::array<int, 9>& params, const std::array<std::uint8_t, 9>& options,
  562. const std::map<int, std::vector<GbPoint>>& slots)
  563. {
  564. std::vector<std::uint8_t> b;
  565. auto put = [&b] (const void* p, std::size_t n) { b.insert (b.end(), (const std::uint8_t*) p, (const std::uint8_t*) p + n); };
  566. auto i32 = [&put] (std::int32_t v) { put (&v, 4); };
  567. i32 (0x00080003);
  568. for (auto v : params)
  569. i32 (v);
  570. put (options.data(), options.size());
  571. for (int k = 0; k < 72; ++k)
  572. {
  573. b.push_back (0); // empty name
  574. i32 (0), i32 (5), i32 (0); // trigger sync off, position sync 4 beats, no hold
  575. b.push_back (0), b.push_back (0), b.push_back (0);
  576. const auto it = slots.find (k);
  577. const std::vector<GbPoint> flat { { 0.0, 1.0, 0 }, { 4.0, 1.0, 0 } };
  578. const auto& pts = it != slots.end() ? it->second : flat;
  579. i32 (3), i32 (3), i32 ((std::int32_t) pts.size());
  580. for (const auto& p : pts)
  581. {
  582. put (&p.dx, 8), put (&p.y, 8);
  583. const float tension = 0.0f;
  584. put (&tension, 4);
  585. b.push_back (p.type), b.push_back (0), b.push_back (0), b.push_back (2);
  586. }
  587. i32 (0), i32 (-1), i32 (-1), i32 (-1), i32 (-1);
  588. }
  589. return b;
  590. }
  591. std::vector<float> renderGrossBeat (const lynx::EffectSlot& slot, const std::vector<float>& in, double rate, double bpm)
  592. {
  593. auto fx = lynx::makeEffect (slot);
  594. fx->prepare (rate);
  595. auto l = in, r = in;
  596. const auto tps = bpm / 60.0 * lynx::ppq / rate;
  597. for (std::size_t done = 0; done < l.size(); done += 512)
  598. {
  599. const auto n = (int) std::min<std::size_t> (512, l.size() - done);
  600. fx->setSongPosition ((double) done * tps);
  601. fx->process (l.data() + done, r.data() + done, n, slot.params, bpm);
  602. }
  603. return l;
  604. }
  605. } // namespace
  606. TEST (gross_beat_state_container_and_parameters)
  607. {
  608. const auto state = grossBeatState ({ 3, 40, 128, 256, 1765, 5000, -77, 0, 0x10000 }, { 0, 0, 0, 0, 0, 0, 0, 1, 1 }, {});
  609. const auto parsed = lynx::grossbeat::parseState (state.data(), state.size());
  610. CHECK (parsed.has_value());
  611. CHECK (parsed && parsed->version == 8 && parsed->params[6] == -77 && parsed->slots[0].flat && parsed->slots[71].flat);
  612. const auto slot = lynx::grossbeat::slotFromFl (state, 960);
  613. CHECK (slot && slot->kind == lynx::EffectKind::grossBeat);
  614. std::vector<std::uint8_t> fl;
  615. int ppq = 0;
  616. CHECK (slot && lynx::grossbeat::unpack (slot->state, fl, ppq) && fl == state && ppq == 960);
  617. // every raw FL value survives the 0..1 parameter
  618. const int lo[8] { 0, 0, 0, 0, 0, 0, -256, 0 }, hi[8] { 35, 35, 256, 256, 10000, 10000, 256, 1 };
  619. bool roundTrip = true;
  620. for (int i = 0; i < 8; ++i)
  621. for (int raw = lo[i]; raw <= hi[i]; ++raw)
  622. roundTrip = roundTrip && lynx::grossbeat::rawFromParam (i, lynx::grossbeat::paramFromRaw (i, raw)) == raw;
  623. CHECK (roundTrip);
  624. CHECK (slot && slot->params[1] == lynx::grossbeat::paramFromRaw (1, 35)); // the volume slot 40 is clamped to 35
  625. CHECK (! lynx::grossbeat::parseState (state.data(), 100).has_value());
  626. }
  627. TEST (gross_beat_volume_slot_gates_the_bar)
  628. {
  629. // volume slot 1: open for two beats, closed for two (hold segments); no smoothing; dry path 15 samples late
  630. const std::vector<GbPoint> gate { { 0.0, 1.0, 0 }, { 2.0, 1.0, 2 }, { 0.0, 0.0, 0 }, { 2.0, 0.0, 2 } };
  631. const auto state = grossBeatState ({ 0, 1, 256, 256, 0, 0, 0, 0, 0x10000 }, { 0, 0, 0, 0, 0, 0, 0, 1, 1 }, { { 37, gate } });
  632. const auto slot = lynx::grossbeat::slotFromFl (state);
  633. CHECK (slot.has_value());
  634. if (! slot)
  635. return;
  636. const auto out = renderGrossBeat (*slot, std::vector<float> (200000, 1.0f), 48000.0, 120.0); // a beat = 24000 samples
  637. CHECK (out[14] == 0.0f && out[15] == 1.0f);
  638. CHECK (out[30000] == 1.0f && out[47990] == 1.0f);
  639. CHECK (out[48010] == 0.0f && out[95990] == 0.0f);
  640. CHECK (out[96010] == 1.0f && out[150000] == 0.0f);
  641. }
  642. TEST (gross_beat_time_slot_reads_the_past_bar)
  643. {
  644. // time slot 1 at level 0.5: the buffer is read one bar back (two bars = level 0), plus the 15-sample latency
  645. const std::vector<GbPoint> half { { 0.0, 0.5, 0 }, { 4.0, 0.5, 2 } };
  646. const auto state = grossBeatState ({ 1, 0, 256, 256, 0, 0, 0, 0, 0x10000 }, { 0, 0, 0, 0, 0, 0, 0, 1, 1 }, { { 1, half } });
  647. const auto slot = lynx::grossbeat::slotFromFl (state);
  648. CHECK (slot.has_value());
  649. if (! slot)
  650. return;
  651. std::vector<float> in (150000, 0.0f);
  652. in[1000] = 1.0f;
  653. const auto out = renderGrossBeat (*slot, in, 48000.0, 120.0); // a bar = 96000 samples
  654. std::size_t peak = 0;
  655. for (std::size_t i = 0; i < out.size(); ++i)
  656. if (std::abs (out[i]) > std::abs (out[peak]))
  657. peak = i;
  658. CHECK (peak == 1000 + 96000 + 15);
  659. CHECK (std::abs (out[peak] - 1.0f) < 1e-6f);
  660. float elsewhere = 0.0f;
  661. for (std::size_t i = 0; i < out.size(); ++i)
  662. if (i + 20 < peak || i > peak + 20)
  663. elsewhere = std::max (elsewhere, std::abs (out[i]));
  664. CHECK (elsewhere == 0.0f);
  665. }
  666. TEST (volume_gate_follows_bar_position)
  667. {
  668. auto fx = lynx::makeEffect (lynx::EffectKind::volumeGate);
  669. fx->prepare (48000.0);
  670. auto slot = lynx::makeEffectSlot (lynx::EffectKind::volumeGate);
  671. for (int i = 0; i < 32; ++i)
  672. slot.params[(std::size_t) i] = i < 16 ? 1.0f : 0.0f; // first two beats open, two closed
  673. slot.params[33] = slot.params[34] = 0.0f; // no smoothing
  674. // 120 BPM: one beat = 0.5 s = 24000 samples; the 4-beat bar starts at tick 0
  675. std::vector<float> l (96000, 1.0f), r (96000, 1.0f);
  676. fx->setSongPosition (0.0);
  677. for (int done = 0; done < 96000; done += 512)
  678. fx->process (l.data() + done, r.data() + done, std::min (512, 96000 - done), slot.params, 120.0);
  679. CHECK (l[10000] > 0.99f && l[40000] > 0.99f); // beats 1 and 2
  680. CHECK (l[60000] < 0.01f && l[90000] < 0.01f); // beats 3 and 4
  681. // starting mid-bar (beat 3): already closed
  682. std::vector<float> l2 (4800, 1.0f), r2 (4800, 1.0f);
  683. fx->reset();
  684. fx->setSongPosition (2.0 * lynx::ppq);
  685. fx->process (l2.data(), r2.data(), 4800, slot.params, 120.0);
  686. CHECK (l2[4000] < 0.01f);
  687. // mix 0: transparent
  688. slot.params[32] = 0.0f;
  689. std::vector<float> l3 (4800, 1.0f), r3 (4800, 1.0f);
  690. fx->setSongPosition (2.0 * lynx::ppq);
  691. fx->process (l3.data(), r3.data(), 4800, slot.params, 120.0);
  692. CHECK (l3[4000] > 0.99f);
  693. }
  694. TEST (patcher_branch_heads_follow_rea)
  695. {
  696. auto words = [] (std::vector<std::int32_t> v) {
  697. std::vector<std::uint8_t> b (v.size() * 4);
  698. std::memcpy (b.data(), v.data(), b.size());
  699. return b;
  700. };
  701. // Frequency Splitter, 2 bands, destinations 1/2/3, slope combo 3 (24 dB/oct), high send −6 dB
  702. std::vector<std::int32_t> fs (18, 0);
  703. fs[0] = 4;
  704. fs[1] = 32768;
  705. fs[3] = 3;
  706. fs[6] = 0;
  707. fs[11] = -600;
  708. fs[15] = 1;
  709. fs[16] = 2;
  710. fs[17] = 3;
  711. const auto split = words (fs);
  712. const auto low = lynx::patcherBranchHead ("Frequency Splitter", split, 1);
  713. CHECK (low.valid && low.slot && low.note.empty());
  714. if (low.slot)
  715. {
  716. // the exact engine (REA_rodada62): the low band's send, 24 dB/oct
  717. CHECK (low.slot->kind == lynx::EffectKind::frequencySplitter && low.slot->params[26] == 1.0f && low.slot->params[27] == 0.0f);
  718. CHECK (low.slot->params[2] == 3.0f / 7.0f);
  719. }
  720. const auto mid = lynx::patcherBranchHead ("Frequency Splitter", split, 2);
  721. CHECK (mid.valid && mid.slot && mid.slot->params[27] == 1.0f && mid.slot->params[5] == 0.0f); // 2 bands: the engine's mid is silent
  722. const auto high = lynx::patcherBranchHead ("Frequency Splitter", split, 3);
  723. CHECK (high.valid && high.slot && high.slot->params[28] == 1.0f && high.slot->params[10] == (float) (1200.0 / 3600.0));
  724. CHECK (! lynx::patcherBranchHead ("Frequency Splitter", split, 7).valid);
  725. // Stereo Shaper: the difference destination (4) is the exact engine with its difference output (REA_rodada54)
  726. const auto shaper = words ({ 3, 0, 12800, 12800, 0, 0, 0, 4, 0 });
  727. const auto diff = lynx::patcherBranchHead ("Fruity Stereo Shaper", shaper, 4);
  728. CHECK (diff.valid && diff.slot && diff.slot->kind == lynx::EffectKind::stereoShaper && diff.slot->params[7] == 1.0f);
  729. CHECK (! lynx::patcherBranchHead ("Fruity Stereo Shaper", shaper, 2).valid);
  730. }
  731. TEST (parallel_container_sums_branches)
  732. {
  733. // two branches: dry with gain 0.5 and a utility that flips phase (matrix −1) with gain 0.5 -> silence
  734. auto slot = lynx::makeEffectSlot (lynx::EffectKind::parallel);
  735. lynx::EffectBranch dry;
  736. dry.gain = 0.5f;
  737. lynx::EffectBranch inverted;
  738. inverted.gain = 0.5f;
  739. auto flip = lynx::makeEffectSlot (lynx::EffectKind::utility);
  740. flip.params[2] = flip.params[5] = 0.25f; // (−1 + 2)/4
  741. inverted.chain.push_back (flip);
  742. slot.branches = { dry, inverted };
  743. auto fx = lynx::makeParallelEffect (slot);
  744. fx->prepare (48000.0);
  745. std::vector<float> l (9000, 0.8f), r (9000, -0.4f);
  746. fx->process (l.data(), r.data(), 9000, slot.params, 120.0);
  747. CHECK (std::abs (l[100]) < 1e-5f && std::abs (r[8999]) < 1e-5f);
  748. // only the dry branch with gain 2: doubles
  749. slot.branches = { lynx::EffectBranch { {}, 2.0f } };
  750. auto doubler = lynx::makeParallelEffect (slot);
  751. doubler->prepare (48000.0);
  752. std::fill (l.begin(), l.end(), 0.25f);
  753. doubler->process (l.data(), r.data(), 9000, slot.params, 120.0);
  754. CHECK (std::abs (l[5000] - 0.5f) < 1e-6f);
  755. }
  756. TEST (fl_controller_constant_outputs)
  757. {
  758. std::vector<std::int32_t> xy { 2, 32768, 0, 65536, 0, 16384, 0, 65536, 0, 1024, 256 };
  759. std::vector<std::uint8_t> blob (xy.size() * 4 + 8, 0);
  760. std::memcpy (blob.data(), xy.data(), xy.size() * 4);
  761. CHECK (std::abs (lynx::controllerConstantOutput ("Fruity X-Y Controller", blob, 0).value_or (-1.0f) - 0.5f) < 1e-4f);
  762. CHECK (std::abs (lynx::controllerConstantOutput ("Fruity X-Y Controller", blob, 1).value_or (-1.0f) - 0.25f) < 1e-4f);
  763. // Control Surface: version + group { 0x836: value 0.75, -, output 2 }
  764. std::vector<std::uint8_t> cs (4, 0);
  765. cs[0] = 1;
  766. auto chunk = [] (std::vector<std::uint8_t>& out, std::uint32_t id, const std::vector<std::uint8_t>& body) {
  767. const auto size = (std::uint64_t) body.size();
  768. out.insert (out.end(), reinterpret_cast<const std::uint8_t*> (&id), reinterpret_cast<const std::uint8_t*> (&id) + 4);
  769. out.insert (out.end(), reinterpret_cast<const std::uint8_t*> (&size), reinterpret_cast<const std::uint8_t*> (&size) + 8);
  770. out.insert (out.end(), body.begin(), body.end());
  771. };
  772. std::vector<std::uint8_t> value (12, 0);
  773. const float v = 0.75f;
  774. const std::int32_t out = 2;
  775. std::memcpy (value.data(), &v, 4);
  776. std::memcpy (value.data() + 8, &out, 4);
  777. std::vector<std::uint8_t> group;
  778. chunk (group, 0x836, value);
  779. chunk (cs, 0, group);
  780. CHECK (std::abs (lynx::controllerConstantOutput ("Control Surface", cs, 2).value_or (-1.0f) - 0.75f) < 1e-6f);
  781. CHECK (! lynx::controllerConstantOutput ("Control Surface", cs, 0).has_value());
  782. }
  783. TEST (flp_midi_out_notes_reach_wrapper_on_same_port)
  784. {
  785. auto record = [] (std::vector<std::uint8_t>& out, std::uint32_t id, std::vector<std::uint8_t> body) {
  786. const auto size = (std::uint64_t) body.size();
  787. out.insert (out.end(), reinterpret_cast<const std::uint8_t*> (&id), reinterpret_cast<const std::uint8_t*> (&id) + 4);
  788. out.insert (out.end(), reinterpret_cast<const std::uint8_t*> (&size), reinterpret_cast<const std::uint8_t*> (&size) + 8);
  789. out.insert (out.end(), body.begin(), body.end());
  790. };
  791. auto i32s = [] (std::vector<std::int32_t> v) {
  792. std::vector<std::uint8_t> b (v.size() * 4);
  793. std::memcpy (b.data(), v.data(), b.size());
  794. return b;
  795. };
  796. flp::Project p;
  797. flp::Channel midiOut;
  798. midiOut.index = 0;
  799. midiOut.type = flp::ChannelType::native;
  800. midiOut.pluginName = "MIDI Out";
  801. midiOut.pluginState = i32s ({ 7, 0, -1, -1, -1, 0, 3, 0 }); // channel 1, port 3 (@24)
  802. flp::Channel wrapper;
  803. wrapper.index = 1;
  804. wrapper.type = flp::ChannelType::native;
  805. wrapper.pluginName = "Fruity Wrapper";
  806. wrapper.pluginState = i32s ({ 9 });
  807. record (wrapper.pluginState, 0x32, i32s ({ 7 })); // VST3
  808. record (wrapper.pluginState, 0x01, i32s ({ 3, -1, 0, 0, 0 })); // input port 3
  809. p.channels = { midiOut, wrapper };
  810. flp::Pattern pattern;
  811. pattern.index = 1;
  812. flp::Note note;
  813. note.channel = 0;
  814. note.key = 64;
  815. note.length = 96;
  816. pattern.notes.push_back (note);
  817. p.patterns.push_back (pattern);
  818. const auto song = lynx::importFlp (p);
  819. CHECK (song.channels.size() == 2 && song.channels[0].muted);
  820. CHECK (song.channels[1].plugin.foreign.midiInPort == 3);
  821. int toWrapper = 0;
  822. for (const auto& pat : song.patterns)
  823. for (const auto& n : pat.notes)
  824. toWrapper += n.channel == 1 && n.key == 64;
  825. CHECK (toWrapper == 1);
  826. }
  827. TEST (flp_effect_automation_follows_slots_and_params)
  828. {
  829. // track 1: slot 0 with an effect that has no equivalent (dropped), slot 2 with Parametric EQ 2
  830. flp::Project p;
  831. flp::MixerInsert insert;
  832. insert.index = 1;
  833. insert.slots.push_back ({ 0, "Some Unknown FX", {} });
  834. insert.slots.push_back ({ 2, "Fruity Parametric EQ 2", {} });
  835. p.mixer.push_back (insert);
  836. auto clip = [&] (int index, std::uint32_t target) {
  837. flp::Channel c;
  838. c.index = index;
  839. c.type = flp::ChannelType::automationClip;
  840. c.automation = { { 0.0, 0.0 }, { 96.0, 1.0 } };
  841. p.channels.push_back (c);
  842. p.automationLinks.push_back ({ index, { target } });
  843. };
  844. 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; };
  845. clip (0, mixer (1, 2, 0x8000 + 7)); // EQ band 1 frequency
  846. clip (1, mixer (1, 2, 0x1F01)); // EQ slot mix
  847. clip (2, mixer (1, 0, 0x8000)); // parameter of the dropped effect
  848. clip (3, mixer (1, 2, 0x8000 + 21)); // band type: no equivalent
  849. const auto song = lynx::importFlp (p);
  850. CHECK (song.automations.size() == 4);
  851. CHECK (song.mixer[1].effects.size() == 1 && song.mixer[1].effects[0].kind == lynx::EffectKind::parametricEq);
  852. const auto& freq = song.automations[0];
  853. CHECK (freq.target.kind == lynx::AutomationTargetKind::effectParam && freq.target.index == 1 && freq.target.slot == 0);
  854. CHECK (freq.target.param == lynx::peq::param (0, 1));
  855. // FL's 0..1 (20 Hz..20 kHz) becomes our frequency scale
  856. CHECK (std::abs (freq.points.front().value - lynx::peq::hzValue (20.0)) < 1e-4f);
  857. CHECK (std::abs (freq.points.back().value - lynx::peq::hzValue (20000.0)) < 1e-4f);
  858. CHECK (song.automations[1].target.kind == lynx::AutomationTargetKind::effectMix && song.automations[1].target.slot == 0);
  859. CHECK (song.automations[2].target.kind == lynx::AutomationTargetKind::none);
  860. CHECK (song.automations[3].target.kind == lynx::AutomationTargetKind::none);
  861. // the automated mix is read/written through the target
  862. auto copy = song;
  863. CHECK (copy.setTargetValue (song.automations[1].target, 0.25f) && std::abs (copy.mixer[1].effects[0].mix - 0.25f) < 1e-6f);
  864. CHECK (std::abs (copy.targetValue (song.automations[1].target) - 0.25f) < 1e-6f);
  865. }
  866. TEST (flp_real_files)
  867. {
  868. if (realFlpDir.empty())
  869. {
  870. std::printf (" (skipped: pass a folder with .flp files as argument)\n");
  871. return;
  872. }
  873. int files = 0, withNotes = 0, withPlaylist = 0, automations = 0, automationsWithTarget = 0, sends = 0;
  874. int threeOsc = 0, threeOscDecoded = 0, wrapped = 0, wrappedParsed = 0, wrappedVst2 = 0, wrappedVst2State = 0, markers = 0, signatures = 0, trackEqs = 0;
  875. int harmless = 0, harmlessDecoded = 0, harmlessParsed = 0, harmlessKind = 0, sytrus = 0, sytrusDecoded = 0, sytrusEngine = 0, sytrusRendered = 0,
  876. sytrusSounding = 0;
  877. int harmor = 0, harmorUsable = 0, harmorKind = 0, harmorSounding = 0;
  878. int gms = 0, gmsUsable = 0, gmsKind = 0, gmsSounding = 0, morphine = 0, morphineUsable = 0, morphineKind = 0, morphineSounding = 0, demoGoldens = 0;
  879. int ogun = 0, ogunUsable = 0, ogunKind = 0, ogunSounding = 0;
  880. int autogun = 0, autogunUsable = 0, autogunKind = 0, autogunWithSg = 0, autogunSounding = 0; // REA_rodada57
  881. int poizone = 0, poizoneUsable = 0, poizoneKind = 0, poizoneSounding = 0, poizoneGoldens = 0;
  882. int miniSynth = 0, miniSynthUsable = 0, miniSynthKind = 0, miniSynthSounding = 0, miniSynthGoldens = 0;
  883. int dx10 = 0, dx10Usable = 0, dx10Kind = 0, dx10Sounding = 0;
  884. int kepler = 0, keplerUsable = 0, keplerKind = 0, keplerSounding = 0;
  885. int toxic = 0, toxicUsable = 0, toxicKind = 0, toxicSounding = 0, toxicGoldens = 0; // REA_rodada45
  886. int threeOscKind = 0, threeOscSounding = 0;
  887. std::map<std::string, std::pair<int, int>> generators; // GMS/Ogun/Autogun/Sakura/DrumSynth: channels, decoded
  888. int ogunTrumpet = 0; // Ogun channels whose spectrum matches the REA "Trumpet" reference
  889. int peqSlots = 0, peqShaped = 0, reverbs = 0, reverbsDryless = 0, reverbsLowCut = 0;
  890. int dwChannels = 0, dwZones = 0, fpcKits = 0, padChannels = 0, padNotes = 0, dwExact = 0;
  891. int harmlessCompWanted = 0, harmlessCompLoaded = 0; // Harmless rack compressors / presets read from the installation
  892. int maximusSlots = 0, maximusExact = 0, sgzSlots = 0, sgzExact = 0, vocodexSlots = 0, vocodexWithData = 0; // REA_rodada40
  893. int shaperSlots = 0, shaperExact = 0, clipperSlots = 0, clipperExact = 0; // REA_rodada51
  894. int flPeqSlots = 0, flPeqExact = 0; // REA_rodada50
  895. int basicSlots = 0, basicExact = 0; // REA_rodada58: PanOMatic, Balance, Blood Overdrive
  896. int equoSlots = 0, equoExact = 0, pitchShifterSlots = 0, pitchShifterExact = 0; // REA_rodada61
  897. int filterSlots = 0, filterExact = 0; // REA_rodada63: Fast LP, Free Filter, Fruity Filter
  898. int distructorSlots = 0, distructorExact = 0, distructorImpulses = 0; // REA_rodada71
  899. int delay2Slots = 0, delay2Exact = 0, delay3Slots = 0, delay3Exact = 0; // REA_rodada52
  900. int chorusSlots = 0, chorusExact = 0; // REA_rodada59: the exact choruses
  901. int fastDistSlots = 0, fastDistExact = 0, flangusSlots = 0, flangusExact = 0, flangerSlots = 0, flangerExact = 0; // REA_rodada55
  902. // import with the installation's samples (realFlpDir is ...\Data): for DirectWave/FPC
  903. lynx::FlpImportOptions withFiles;
  904. const auto flRoot = std::filesystem::path (realFlpDir).parent_path().string();
  905. withFiles.roots = { flRoot, realFlpDir, "" };
  906. withFiles.fileExists = [] (const std::string& path) {
  907. auto local = path;
  908. std::replace (local.begin(), local.end(), '\\', '/');
  909. return std::filesystem::exists (local);
  910. };
  911. withFiles.readFile = [] (const std::string& path) {
  912. auto local = path;
  913. std::replace (local.begin(), local.end(), '\\', '/');
  914. std::ifstream in (local, std::ios::binary);
  915. return std::vector<std::uint8_t> ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  916. };
  917. int embeddedSaved = 0;
  918. withFiles.saveEmbedded = [&embeddedSaved] (const std::vector<std::uint8_t>& wav, const std::string&) {
  919. ++embeddedSaved;
  920. return wav.size() > 44 ? std::string ("embutido.wav") : std::string();
  921. };
  922. int slicedChannels = 0, flKeysChannels = 0;
  923. for (auto& entry : std::filesystem::recursive_directory_iterator (realFlpDir))
  924. {
  925. if (entry.path().extension() != ".flp")
  926. continue;
  927. std::ifstream in (entry.path(), std::ios::binary);
  928. std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  929. ++files;
  930. try
  931. {
  932. const auto p = flp::parseProject (bytes);
  933. CHECK (! p.version.empty());
  934. CHECK (p.tempo > 10.0 && p.tempo < 1000.0);
  935. const auto song = lynx::importFlp (p);
  936. CHECK (! song.patterns.empty());
  937. {
  938. const auto full = lynx::importFlp (p, withFiles);
  939. bool hasFpc = false;
  940. for (const auto& c : p.channels)
  941. hasFpc = hasFpc || c.pluginName == "FPC";
  942. // GMS and Morphine (REA_rodada39): every state plays on the exact engine (GMS with its waves from the
  943. // installation) and sounds; two demo channels match golden samples of our round-39 models
  944. for (const auto& c : p.channels)
  945. {
  946. if (c.pluginName == "GMS")
  947. ++gms, gmsUsable += lynx::gmsStateUsable (c.pluginState) ? 1 : 0;
  948. if (c.pluginName == "Morphine")
  949. ++morphine, morphineUsable += lynx::morphineStateUsable (c.pluginState) ? 1 : 0;
  950. }
  951. // Kepler: every state in the demos plays on the exact engine (REA_rodada46) when the engine DLL is installed
  952. for (const auto& c : p.channels)
  953. if (c.pluginName == "Kepler")
  954. {
  955. ++kepler;
  956. keplerUsable += lynx::keplerStateUsable (c.pluginState) ? 1 : 0;
  957. }
  958. for (const auto& c : full.channels)
  959. if (c.instrument == lynx::InstrumentKind::kepler)
  960. {
  961. ++keplerKind;
  962. 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 } },
  963. 44100.0, 11025);
  964. double e = 0.0;
  965. bool finite = ! y.empty();
  966. for (auto v : y)
  967. finite = finite && std::isfinite (v), e += (double) v * v;
  968. CHECK (finite);
  969. keplerSounding += e > 1e-6 ? 1 : 0;
  970. }
  971. const auto file = entry.path().filename().string();
  972. for (const auto& c : full.channels)
  973. {
  974. const auto isGms = c.instrument == lynx::InstrumentKind::gms, isMorphine = c.instrument == lynx::InstrumentKind::morphine;
  975. if (! isGms && ! isMorphine)
  976. continue;
  977. (isGms ? gmsKind : morphineKind)++;
  978. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  979. const auto golden = (isGms && c.name == "GMS" && file.find ("Song For You") != std::string::npos)
  980. || (isMorphine && c.name == "Morphine" && file.find ("SHINE") != std::string::npos);
  981. std::vector<float> y;
  982. if (isGms)
  983. y = lynx::gms::renderReference (c.pluginState,
  984. { { 0, 60, (float) iv (100), 1.0, 0.0, 0.0, 30000 }, { 11025, 64, (float) iv (80), 1.0, 0.0, 0.0, 40000 },
  985. { 22050, 67, (float) iv (110), 1.0, 0.0, 0.0, 52000 }, { 60000, 48, (float) iv (127), 1.0, 0.0, 0.0, 70000 } },
  986. 44100.0, golden ? 88200 : 22050, c.samplePath);
  987. else
  988. 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 } },
  989. 44100.0, golden ? 100000 : 22050, c.samplePath);
  990. double e = 0.0;
  991. bool finite = ! y.empty();
  992. for (auto v : y)
  993. finite = finite && std::isfinite (v), e += (double) v * v;
  994. CHECK (finite);
  995. (isGms ? gmsSounding : morphineSounding) += e > 1e-6 ? 1 : 0;
  996. if (golden && ! y.empty())
  997. {
  998. // our model, same events (GMS: tempo 140; Morphine: tempo 120), state as saved, 44.1 kHz
  999. const auto rms = std::sqrt (e / (double) y.size());
  1000. CHECK (std::abs (rms - (isGms ? 0.213602668574 : 0.0673606448152)) < 1e-5);
  1001. 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 } },
  1002. 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 } };
  1003. for (const auto& g : isGms ? std::vector (std::begin (gmsAt), std::end (gmsAt)) : std::vector (std::begin (morAt), std::end (morAt)))
  1004. 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);
  1005. ++demoGoldens;
  1006. }
  1007. }
  1008. // MiniSynth (REA_rodada41): every state plays on the exact engine (tables from the installation's
  1009. // oscdata.dat) and sounds; two demo channels match golden samples of our round-41 model (bit-identical to
  1010. // renders of the installation's DLL for these states)
  1011. for (const auto& c : p.channels)
  1012. if (c.pluginName == "MiniSynth")
  1013. ++miniSynth, miniSynthUsable += lynx::miniSynthStateUsable (c.pluginState) ? 1 : 0;
  1014. for (const auto& c : full.channels)
  1015. {
  1016. if (c.instrument != lynx::InstrumentKind::minisynth)
  1017. continue;
  1018. ++miniSynthKind;
  1019. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  1020. const auto exo = file.find ("Exoplanet") != std::string::npos;
  1021. const auto golden = exo && (c.name == "Main Lead 1" || c.name == "Riser");
  1022. const auto y = lynx::minisynth::renderReference (c.pluginState,
  1023. { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 }, { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  1024. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 }, { 60000, 48, iv (127), 1.0, 0.0, 0.0, 70000 } },
  1025. 44100.0, golden ? 88200 : 22050, c.samplePath);
  1026. double e = 0.0;
  1027. bool finite = ! y.empty();
  1028. for (auto v : y)
  1029. finite = finite && std::isfinite (v), e += (double) v * v;
  1030. CHECK (finite);
  1031. miniSynthSounding += e > 1e-6 ? 1 : 0;
  1032. if (golden && ! y.empty())
  1033. {
  1034. // our model, same events, tempo 120, state as saved, 44.1 kHz, host blocks of 512, L = R = Final.Vol
  1035. const auto lead = c.name == "Main Lead 1";
  1036. CHECK (std::abs (std::sqrt (e / (double) y.size()) - (lead ? 0.177495801753 : 0.19006567413)) < 1e-9);
  1037. 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 } },
  1038. riserAt[] = { { 2000, -0.0603227727f }, { 15000, 0.112200871f }, { 30000, 0.0453962795f }, { 50000, 0.234380022f }, { 65000, -0.164545432f }, { 80000, -0.000485120923f } };
  1039. for (const auto& g : lead ? std::vector (std::begin (leadAt), std::end (leadAt)) : std::vector (std::begin (riserAt), std::end (riserAt)))
  1040. CHECK (y[2 * (std::size_t) g.i] == g.v && y[2 * (std::size_t) g.i + 1] == g.v);
  1041. ++miniSynthGoldens;
  1042. }
  1043. }
  1044. // PoiZone (REA_rodada44): every state plays on the exact engine and sounds; the demo channel matches golden
  1045. // samples of our round-44 model
  1046. for (const auto& c : p.channels)
  1047. if (c.pluginName == "PoiZone")
  1048. ++poizone, poizoneUsable += lynx::poizoneStateUsable (c.pluginState) ? 1 : 0;
  1049. for (const auto& c : full.channels)
  1050. {
  1051. if (c.instrument != lynx::InstrumentKind::poizone)
  1052. continue;
  1053. ++poizoneKind;
  1054. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  1055. const auto golden = c.name == "PoiZone" && file.find ("NIGHT TERROR") != std::string::npos;
  1056. const auto y = lynx::poizone::renderReference (c.pluginState,
  1057. { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 }, { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  1058. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 }, { 60000, 48, iv (127), 1.0, 0.0, 0.0, 70000 } },
  1059. 44100.0, golden ? 88200 : 22050);
  1060. double e = 0.0;
  1061. bool finite = ! y.empty();
  1062. for (auto v : y)
  1063. finite = finite && std::isfinite (v), e += (double) v * v;
  1064. CHECK (finite);
  1065. poizoneSounding += e > 1e-6 ? 1 : 0;
  1066. if (golden && ! y.empty())
  1067. {
  1068. // our model, same events, tempo 120, state as saved, 44.1 kHz, L = R = Final.Vol
  1069. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.018975758213507415) < 1e-7);
  1070. 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 } };
  1071. for (const auto& g : at)
  1072. 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);
  1073. ++poizoneGoldens;
  1074. }
  1075. }
  1076. // Toxic Biohazard (REA_rodada45): every demo state plays on the exact engine with the waves read from the
  1077. // installation's DLL and sounds; the "Bombs Away" channel matches golden samples of our round-45 model
  1078. for (const auto& c : p.channels)
  1079. if (c.pluginName == "Toxic Biohazard")
  1080. ++toxic, toxicUsable += lynx::toxicStateUsable (c.pluginState) ? 1 : 0;
  1081. for (const auto& c : full.channels)
  1082. {
  1083. if (c.instrument != lynx::InstrumentKind::toxic)
  1084. continue;
  1085. ++toxicKind;
  1086. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  1087. const auto golden = file.find ("Bombs Away") != std::string::npos;
  1088. const auto y = lynx::toxic::renderReference (
  1089. c.pluginState,
  1090. { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 }, { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  1091. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 }, { 60000, 48, iv (127), 1.0, 0.0, 0.0, 70000 } },
  1092. 44100.0, golden ? 88200 : 22050, c.samplePath, 512, 140.0);
  1093. double e = 0.0;
  1094. bool finite = ! y.empty();
  1095. for (auto v : y)
  1096. finite = finite && std::isfinite (v), e += (double) v * v;
  1097. CHECK (finite);
  1098. toxicSounding += e > 1e-6 ? 1 : 0;
  1099. if (golden && ! y.empty())
  1100. {
  1101. // our model (r45/py/toxmodel.py, which equals the DLL bit for bit), state as saved, tempo 140, 44.1 kHz
  1102. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.0832500924945) < 1e-6);
  1103. 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 } };
  1104. for (const auto& g : at)
  1105. 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);
  1106. ++toxicGoldens;
  1107. }
  1108. }
  1109. for (const auto& c : full.channels)
  1110. {
  1111. if (c.instrument == lynx::InstrumentKind::harmless && c.modelParams.size() >= (std::size_t) lynx::harmlessParamCount
  1112. && c.modelParams[(std::size_t) lynx::harmlessComboIndex + 3] > 0.5f)
  1113. {
  1114. ++harmlessCompWanted;
  1115. harmlessCompLoaded += c.modelParams.size() > (std::size_t) lynx::harmlessCompIndex + 1000 ? 1 : 0;
  1116. }
  1117. flKeysChannels += c.zones.size() == 30 && c.zones.front().path.find ("CHANGPIANO") != std::string::npos ? 1 : 0;
  1118. if (c.sampler.slices.size() >= 4)
  1119. {
  1120. ++slicedChannels;
  1121. CHECK (std::is_sorted (c.sampler.slices.begin(), c.sampler.slices.end()));
  1122. CHECK (c.sampler.slices.back() < 1.0f);
  1123. }
  1124. dwExact += c.instrument == lynx::InstrumentKind::directwave ? 1 : 0;
  1125. if (! c.zones.empty() && c.name.find (" - ") == std::string::npos)
  1126. {
  1127. ++dwChannels;
  1128. dwZones += (int) c.zones.size();
  1129. }
  1130. padChannels += c.name.find (" - ") != std::string::npos && c.instrument == lynx::InstrumentKind::sampler ? 1 : 0;
  1131. }
  1132. fpcKits += hasFpc;
  1133. // Maximus / Soundgoodizer become the exact engine (REA_rodada40); Vocodex carries SG.dat (and its
  1134. // carrier's Sytrus preset) read from the installation
  1135. const int basicSlots0 = basicSlots, basicExact0 = basicExact;
  1136. for (const auto& insert : p.mixer)
  1137. for (const auto& slot : insert.slots)
  1138. {
  1139. maximusSlots += slot.plugin == "Maximus";
  1140. delay2Slots += slot.plugin == "Fruity Delay 2";
  1141. delay3Slots += slot.plugin == "Fruity Delay 3";
  1142. sgzSlots += slot.plugin == "Soundgoodizer";
  1143. vocodexSlots += slot.plugin == "Vocodex";
  1144. shaperSlots += slot.plugin == "Fruity WaveShaper";
  1145. flPeqSlots += slot.plugin == "Fruity Parametric EQ 2" || slot.plugin == "Fruity Parametric EQ";
  1146. equoSlots += slot.plugin == "EQUO";
  1147. distructorSlots += slot.plugin == "Distructor";
  1148. pitchShifterSlots += slot.plugin == "Pitch Shifter";
  1149. clipperSlots += slot.plugin == "Fruity Soft Clipper";
  1150. basicSlots += slot.plugin == "Fruity PanOMatic" || slot.plugin == "Fruity Balance" || slot.plugin == "Fruity Blood Overdrive";
  1151. filterSlots += slot.plugin == "Fruity Fast LP" || slot.plugin == "Fruity Free Filter" || slot.plugin == "Fruity Filter";
  1152. chorusSlots += lynx::chorusControls (slot.plugin) > 0 && slot.state.size() >= 4 ? 1 : 0;
  1153. fastDistSlots += slot.plugin == "Fruity Fast Dist";
  1154. flangusSlots += slot.plugin == "Fruity Flangus";
  1155. flangerSlots += slot.plugin == "Fruity Flanger";
  1156. }
  1157. for (const auto& t : full.mixer)
  1158. for (const auto& fx : t.effects)
  1159. {
  1160. if (fx.kind == lynx::EffectKind::maximus)
  1161. {
  1162. const auto blob = lynx::maximusfx::unpack (fx.state);
  1163. CHECK (blob && lynx::maximusfx::presetOf (*blob) != nullptr);
  1164. if (blob)
  1165. (blob->build == lynx::hlfx::MaximusBuild::soundgoodizer ? sgzExact : maximusExact) += 1;
  1166. }
  1167. fastDistExact += fx.kind == lynx::EffectKind::fruityFastDist;
  1168. flangusExact += fx.kind == lynx::EffectKind::fruityFlangus;
  1169. flangerExact += fx.kind == lynx::EffectKind::fruityFlanger;
  1170. if (fx.kind == lynx::EffectKind::vocodex)
  1171. vocodexWithData += ! lynx::vocodex::splitInstallData (fx.state).second.sgDat.empty();
  1172. if (fx.kind == lynx::EffectKind::fruityWaveShaper)
  1173. shaperExact += lynx::flshape::parseWaveShaper (fx.state).has_value() ? 1 : 0;
  1174. clipperExact += fx.kind == lynx::EffectKind::fruitySoftClipper;
  1175. flPeqExact += fx.kind == lynx::EffectKind::parametricEq && lynx::peq::flLaw (fx.params);
  1176. basicExact += fx.kind == lynx::EffectKind::fruityPanOMatic || fx.kind == lynx::EffectKind::fruityBalance
  1177. || fx.kind == lynx::EffectKind::fruityBloodOverdrive;
  1178. equoExact += fx.kind == lynx::EffectKind::equo && lynx::equofx::parse (fx.state).has_value();
  1179. if (fx.kind == lynx::EffectKind::distructor)
  1180. if (const auto d = lynx::distructor::parseBlob (fx.state))
  1181. {
  1182. ++distructorExact;
  1183. for (const auto& m : d->modules)
  1184. distructorImpulses += m.type == 3 && ! m.impulseFile.empty();
  1185. }
  1186. pitchShifterExact += fx.kind == lynx::EffectKind::flPitchShifter;
  1187. filterExact += fx.kind == lynx::EffectKind::fruityFastLP || fx.kind == lynx::EffectKind::fruityFreeFilter
  1188. || fx.kind == lynx::EffectKind::fruityFilter;
  1189. delay2Exact += fx.kind == lynx::EffectKind::fruityDelay2;
  1190. delay3Exact += fx.kind == lynx::EffectKind::fruityDelay3;
  1191. chorusExact += fx.kind == lynx::EffectKind::fruityChorus || fx.kind == lynx::EffectKind::hyperChorus || fx.kind == lynx::EffectKind::vintageChorus ? 1 : 0;
  1192. }
  1193. if (basicSlots - basicSlots0 != basicExact - basicExact0)
  1194. std::printf (" %s: %d PanOMatic/Balance/Blood Overdrive slots, %d exact effects\n", file.c_str(),
  1195. basicSlots - basicSlots0, basicExact - basicExact0);
  1196. for (const auto& pat : full.patterns)
  1197. for (const auto& n : pat.notes)
  1198. {
  1199. CHECK (n.channel >= 0 && n.channel < (int) full.channels.size());
  1200. padNotes += full.channels[(std::size_t) n.channel].name.find (" - ") != std::string::npos ? 1 : 0;
  1201. }
  1202. }
  1203. const auto peqDefaults = lynx::makeEffectSlot (lynx::EffectKind::parametricEq).params;
  1204. for (const auto& t : song.mixer)
  1205. for (const auto& fx : t.effects)
  1206. {
  1207. if (fx.kind == lynx::EffectKind::parametricEq)
  1208. {
  1209. ++peqSlots;
  1210. peqShaped += fx.params != peqDefaults;
  1211. for (double hz : { 50.0, 1000.0, 10000.0 })
  1212. CHECK (std::isfinite (lynx::peq::responseDb (fx.params, 48000.0, hz)));
  1213. }
  1214. if (fx.kind == lynx::EffectKind::reverb)
  1215. {
  1216. ++reverbs;
  1217. reverbsDryless += fx.params[4] < 0.01f;
  1218. reverbsLowCut += fx.params[5] > 0.0f;
  1219. }
  1220. if (fx.kind == lynx::EffectKind::reeverb2) // FL's exact Reeverb 2 (REA_rodada49): FL's own controls
  1221. {
  1222. const auto r2 = lynx::reeverb2::fromParams (fx.params);
  1223. ++reverbs;
  1224. reverbsDryless += r2.p[10] == 0;
  1225. reverbsLowCut += r2.p[0] >= 20;
  1226. }
  1227. }
  1228. for (const auto& t : song.mixer)
  1229. if (t.eq.active())
  1230. {
  1231. ++trackEqs;
  1232. for (std::size_t b = 0; b < 3; ++b)
  1233. CHECK (t.eq.hz[b] >= 10.0f && t.eq.hz[b] <= 16000.0f && std::abs (t.eq.gainDb[b]) <= 18.0f);
  1234. }
  1235. for (const auto& a : song.arrangements)
  1236. for (const auto& m : a.markers)
  1237. {
  1238. ++markers;
  1239. signatures += m.isSignature();
  1240. CHECK (m.tick >= 0);
  1241. }
  1242. // Fruity Wrapper (channels and mixer slots): every blob in the demos is read
  1243. auto countWrapper = [&] (const std::string& plugin, const std::vector<std::uint8_t>& state) {
  1244. if (plugin != "Fruity Wrapper")
  1245. return;
  1246. ++wrapped;
  1247. lynx::ForeignPlugin f;
  1248. wrappedParsed += lynx::parseFlWrapper (state, f) && f.format != lynx::ForeignPlugin::Format::none && ! f.name.empty();
  1249. if (f.format == lynx::ForeignPlugin::Format::vst2)
  1250. {
  1251. // VST2 state (REA_rodada89 §2): every one decodes and writes back to the same bytes
  1252. ++wrappedVst2;
  1253. lynx::FlVst2State st;
  1254. if (lynx::parseFlVst2State (f.state, st) && st.version == -9 && lynx::writeFlVst2State (st) == f.state)
  1255. ++wrappedVst2State;
  1256. }
  1257. };
  1258. for (const auto& c : p.channels)
  1259. countWrapper (c.pluginName, c.pluginState);
  1260. for (const auto& insert : p.mixer)
  1261. for (const auto& slot : insert.slots)
  1262. countWrapper (slot.plugin, slot.state);
  1263. // Harmless: every state in the demos becomes a synth, with finite values
  1264. for (const auto& c : p.channels)
  1265. if (c.pluginName == "Harmless")
  1266. {
  1267. ++harmless;
  1268. lynx::SynthSettings hs;
  1269. float g = 1.0f;
  1270. if (lynx::synthFromHarmless (c.pluginState, 128.0, hs, g))
  1271. {
  1272. ++harmlessDecoded;
  1273. CHECK (std::isfinite (g) && hs.cutoff >= 20.0f && hs.cutoff <= 20000.0f && std::isfinite (hs.decay) && std::isfinite (hs.release));
  1274. }
  1275. if (const auto hp = lynx::harmlessParams (c.pluginState))
  1276. harmlessParsed += (int) hp->size() == lynx::harmlessParamCount ? 1 : 0;
  1277. }
  1278. for (const auto& c : song.channels)
  1279. harmlessKind += c.instrument == lynx::InstrumentKind::harmless ? 1 : 0;
  1280. // Harmor: every state in the demos plays on the exact engine (REA_rodada34) and sounds, with finite samples
  1281. for (const auto& c : p.channels)
  1282. if (c.pluginName == "Harmor")
  1283. {
  1284. ++harmor;
  1285. harmorUsable += lynx::harmorStateUsable (c.pluginState) ? 1 : 0;
  1286. }
  1287. for (const auto& c : song.channels)
  1288. if (c.instrument == lynx::InstrumentKind::harmor)
  1289. {
  1290. ++harmorKind;
  1291. 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);
  1292. double e = 0.0;
  1293. bool finite = ! y.empty();
  1294. for (auto v : y)
  1295. finite = finite && std::isfinite (v), e += (double) v * v;
  1296. CHECK (finite);
  1297. harmorSounding += e > 1e-6 ? 1 : 0;
  1298. }
  1299. // Ogun: every state in the demos plays on the exact engine (REA_rodada37) and sounds, with finite samples
  1300. for (const auto& c : p.channels)
  1301. if (c.pluginName == "Ogun")
  1302. {
  1303. ++ogun;
  1304. ogunUsable += lynx::ogunStateUsable (c.pluginState) ? 1 : 0;
  1305. }
  1306. for (const auto& c : song.channels)
  1307. if (c.instrument == lynx::InstrumentKind::ogun)
  1308. {
  1309. ++ogunKind;
  1310. 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 },
  1311. { 2000, 67, 0.6, 1.0, 0.0, 0.0, 0.0, 0.0, 0.5, 8000 } }, 44100.0, 11025);
  1312. double e = 0.0;
  1313. bool finite = ! y.empty();
  1314. for (auto v : y)
  1315. finite = finite && std::isfinite (v), e += (double) v * v;
  1316. CHECK (finite);
  1317. ogunSounding += e > 1e-6 ? 1 : 0;
  1318. }
  1319. // Autogun (REA_rodada57): every state plays on Ogun's engine in its Autogun variant; with the installation, the
  1320. // Magic Dust stage gets Autogun\Data\SG.dat
  1321. {
  1322. bool any = false;
  1323. for (const auto& c : p.channels)
  1324. if (c.pluginName == "Autogun")
  1325. ++autogun, autogunUsable += lynx::autogunStateUsable (c.pluginState) ? 1 : 0, any = true;
  1326. if (any)
  1327. for (const auto& c : lynx::importFlp (p, withFiles).channels)
  1328. if (c.instrument == lynx::InstrumentKind::autogun)
  1329. {
  1330. ++autogunKind;
  1331. lynx::ogun::RefOptions opt;
  1332. opt.autogun = true;
  1333. if (! c.samplePath.empty())
  1334. {
  1335. auto local = c.samplePath;
  1336. std::replace (local.begin(), local.end(), '\\', '/');
  1337. std::ifstream sg (local, std::ios::binary);
  1338. opt.sgState.assign ((std::istreambuf_iterator<char> (sg)), std::istreambuf_iterator<char>());
  1339. autogunWithSg += opt.sgState.size() == 1342 ? 1 : 0;
  1340. }
  1341. const auto y = lynx::ogun::renderReference (c.pluginState, { { 0, 48, 0.8, 1.0, 0.0, 0.0, 0.0, 0.0, 0.5, 6000 } },
  1342. 44100.0, 11025, opt);
  1343. double e = 0.0;
  1344. bool finite = ! y.empty();
  1345. for (auto v : y)
  1346. finite = finite && std::isfinite (v), e += (double) v * v;
  1347. CHECK (finite);
  1348. autogunSounding += e > 1e-6 ? 1 : 0;
  1349. }
  1350. }
  1351. // Fruity DX10: every state plays on the exact engine when its channel processor is neutral (REA_rodada42)
  1352. for (const auto& c : p.channels)
  1353. if (c.pluginName == "Fruity DX10" || c.pluginName == "DX10")
  1354. ++dx10, dx10Usable += lynx::dx10StateUsable (c.pluginState) ? 1 : 0;
  1355. for (const auto& c : song.channels)
  1356. if (c.instrument == lynx::InstrumentKind::dx10)
  1357. {
  1358. ++dx10Kind;
  1359. 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 } },
  1360. 44100.0, 11025);
  1361. double e = 0.0;
  1362. bool finite = ! y.empty();
  1363. for (auto v : y)
  1364. finite = finite && std::isfinite (v), e += (double) v * v;
  1365. CHECK (finite);
  1366. dx10Sounding += e > 1e-6 ? 1 : 0;
  1367. }
  1368. // 3x Osc: every channel plays on the exact engine (REA_rodada47)
  1369. for (const auto& c : song.channels)
  1370. if (c.instrument == lynx::InstrumentKind::threeosc)
  1371. {
  1372. ++threeOscKind;
  1373. const auto y = lynx::threeosc::renderReference (c.pluginState, { { 0, 60, 1.0, 0.0, 0.0, 6000 }, { 2000, 67, 0.6, 0.0, 0.0, 8000 } },
  1374. 44100.0, 11025);
  1375. double e = 0.0;
  1376. bool finite = ! y.empty();
  1377. for (auto v : y)
  1378. finite = finite && std::isfinite (v), e += (double) v * v;
  1379. CHECK (finite);
  1380. threeOscSounding += e > 1e-6 ? 1 : 0;
  1381. }
  1382. // Sytrus: every state in the demos (zlib + 10 units) decodes, with finite values
  1383. for (const auto& c : p.channels)
  1384. if (c.pluginName == "Sytrus")
  1385. {
  1386. ++sytrus;
  1387. lynx::SynthSettings ss;
  1388. float g = 1.0f;
  1389. // the Sytrus engine (REA_rodada31) reads every demo state; the first 24 play a short note (render settings)
  1390. if (lynx::sytrus::stateUsable (c.pluginState))
  1391. {
  1392. ++sytrusEngine;
  1393. if (sytrusRendered < 24)
  1394. {
  1395. ++sytrusRendered;
  1396. lynx::sytrus::Engine e;
  1397. e.prepare (44100.0, true, 512);
  1398. e.setPatch (lynx::sytrus::parsePatch (c.pluginState));
  1399. lynx::sytrus::NoteParams note;
  1400. e.noteOn (note);
  1401. std::vector<float> l (8192), r (8192);
  1402. e.render (l.data(), r.data(), 8192, 0.551f, 0.0f);
  1403. bool finite = true;
  1404. double energy = 0.0;
  1405. for (std::size_t k = 0; k < l.size(); ++k)
  1406. finite = finite && std::isfinite (l[k]) && std::isfinite (r[k]), energy += (double) l[k] * l[k];
  1407. CHECK (finite);
  1408. sytrusSounding += energy > 1e-6;
  1409. }
  1410. }
  1411. if (lynx::synthFromSytrus (c.pluginState, 128.0, ss, g))
  1412. {
  1413. ++sytrusDecoded;
  1414. CHECK (std::isfinite (g) && ss.cutoff >= 20.0f && ss.cutoff <= 20000.0f && std::isfinite (ss.attack) && std::isfinite (ss.decay)
  1415. && std::isfinite (ss.release) && ss.sustain >= 0.0f && ss.sustain <= 1.0f && std::isfinite (ss.filterEnv.amount));
  1416. for (const auto& o : ss.osc)
  1417. CHECK (o.level >= 0.0f && o.level <= 1.0f && o.octave >= -3 && o.octave <= 3);
  1418. }
  1419. }
  1420. // round 12 generators
  1421. for (const auto& c : p.channels)
  1422. {
  1423. lynx::SynthSettings gs;
  1424. float g = 1.0f;
  1425. int decoded = -1;
  1426. if (c.pluginName == "Harmor")
  1427. {
  1428. decoded = lynx::synthFromHarmor (c.pluginState, gs, g);
  1429. if (decoded)
  1430. {
  1431. // REA_rodada25 §4: most resonant settings stay below Q ≈ 3, a few saturate
  1432. CHECK (gs.resonance >= 0.0f && gs.resonance <= 1.0f);
  1433. static int resonant = 0, strong = 0, total = 0;
  1434. resonant += gs.resonance > 0.0f;
  1435. strong += gs.resonance > 0.9f;
  1436. if (++total % 50 == 0)
  1437. std::printf (" Harmor resonance: %d with a peak, %d with Q > ~5 (of %d)\n", resonant, strong, total);
  1438. }
  1439. static int saw = 0, square = 0, other = 0, total = 0;
  1440. if (decoded && std::any_of (gs.osc.begin(), gs.osc.end(), [] (const lynx::SynthOscillator& o) { return o.wave == lynx::Waveform::spectral; }))
  1441. {
  1442. auto isSaw = true, isSquare = true;
  1443. for (int n = 1; n <= 16; ++n)
  1444. {
  1445. isSaw = isSaw && std::abs (gs.spectrum[(std::size_t) n - 1] - 1.0f / n) < 1e-3f;
  1446. isSquare = isSquare && std::abs (gs.spectrum[(std::size_t) n - 1] - (n % 2 == 1 ? 1.1535f / n : 0.0f)) < 2e-3f;
  1447. }
  1448. ++(isSaw ? saw : isSquare ? square : other);
  1449. if (++total % 20 == 0 || total == 100)
  1450. std::printf (" Harmor spectrum: %d saw, %d square, %d other (of %d)\n", saw, square, other, total);
  1451. }
  1452. }
  1453. else if (c.pluginName == "GMS")
  1454. decoded = lynx::synthFromGms (c.pluginState, 128.0, gs, g);
  1455. else if (c.pluginName == "Ogun" || c.pluginName == "Autogun")
  1456. {
  1457. decoded = lynx::synthFromOgun (c.pluginState, gs, g);
  1458. // REA_rodada24 §1.9: the "Trumpet" preset (richness 7, no seeds) has partials 0.9087, 0.8610,
  1459. // 0.8151… at the fixed seed-3 phases 0.919, 0.380, 0.130
  1460. if (decoded && gs.osc[0].wave == lynx::Waveform::spectral)
  1461. {
  1462. CHECK (std::all_of (gs.spectrum.begin(), gs.spectrum.end(), [] (float a) { return std::isfinite (a) && a >= 0.0f; }));
  1463. static constexpr float trumpet[3] { 0.9087f, 0.8610f, 0.8151f }, phases[3] { 0.919f, 0.380f, 0.130f };
  1464. auto match = true;
  1465. for (std::size_t k = 0; k < 3; ++k)
  1466. match = match && std::abs (gs.spectrum[k] * 2.0f / 3.14159265f - trumpet[k]) < 1e-3f && std::abs (gs.spectrumPhase[k] - phases[k]) < 1e-3f;
  1467. ogunTrumpet += match ? 1 : 0;
  1468. }
  1469. }
  1470. else if (c.pluginName == "Sakura")
  1471. decoded = lynx::synthFromSakura (c.pluginState, gs);
  1472. else if (c.pluginName == "Kepler")
  1473. decoded = lynx::synthFromKepler (c.pluginState, gs, g);
  1474. else if (c.pluginName == "Wave Traveller")
  1475. decoded = lynx::waveTravellerSample (c.pluginState).has_value() ? 2 : 0;
  1476. else if (c.pluginName == "PoiZone")
  1477. decoded = lynx::synthFromPoiZone (c.pluginState, gs, g);
  1478. else if (c.pluginName == "BassDrum")
  1479. decoded = lynx::synthFromBassDrum (c.pluginState, gs, g);
  1480. else if (c.pluginName == "Toxic Biohazard")
  1481. decoded = lynx::synthFromToxic (c.pluginState, gs, g);
  1482. else if (c.pluginName == "Morphine")
  1483. decoded = lynx::synthFromMorphine (c.pluginState, gs, g);
  1484. else if (c.pluginName == "BooBass")
  1485. decoded = lynx::synthFromBooBass (c.pluginState, gs);
  1486. else if (c.pluginName == "Drumpad" || c.pluginName == "DrumPad")
  1487. decoded = lynx::drumpadPatchName (c.pluginState).has_value() ? 2 : 0;
  1488. else if (c.pluginName == "Fruity Envelope Controller" && c.pluginState.size() >= 4 && c.pluginState[2] >= 6) // v >= 6
  1489. decoded = lynx::parseEnvelopeController (c.pluginState).has_value() ? 2 : 0;
  1490. else if (c.pluginName == "FLEX")
  1491. decoded = lynx::flexPresetName (c.pluginState).has_value() ? 2 : 0;
  1492. else if (c.pluginName == "Fruity DrumSynth Live")
  1493. decoded = lynx::drumSynthLiveParams (c.pluginState).has_value() ? 2 : 0;
  1494. if (decoded < 0)
  1495. continue;
  1496. auto& counter = generators[c.pluginName];
  1497. ++counter.first;
  1498. counter.second += decoded != 0;
  1499. if (decoded == 1 && c.pluginName != "Fruity DrumSynth Live")
  1500. {
  1501. CHECK (std::isfinite (g) && g >= 0.0f && gs.cutoff >= 20.0f && gs.cutoff <= 20000.0f && gs.sustain >= 0.0f && gs.sustain <= 1.0f);
  1502. 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));
  1503. for (const auto& o : gs.osc)
  1504. CHECK (o.level >= 0.0f && o.level <= 4.0f && std::isfinite (o.detune) && std::isfinite (o.pan));
  1505. }
  1506. }
  1507. // 3x Osc: every state in the demos decodes, with levels summing to at most 1
  1508. for (const auto& c : p.channels)
  1509. if (c.pluginName == "3x Osc")
  1510. {
  1511. ++threeOsc;
  1512. lynx::SynthSettings s;
  1513. if (lynx::synthFrom3xOsc (c.pluginState, s))
  1514. {
  1515. ++threeOscDecoded;
  1516. // with AM, osc 3 is the modulation depth and is left out of the sum
  1517. const auto sum = s.osc[0].level + s.osc[1].level + (s.am ? 0.0f : s.osc[2].level);
  1518. CHECK (sum <= 1.001f && s.osc[2].level <= 1.0f);
  1519. for (const auto& o : s.osc)
  1520. CHECK (o.octave >= -2 && o.octave <= 2 && std::abs (o.semitones) <= 11 && std::abs (o.pan) <= 1.0f);
  1521. }
  1522. }
  1523. for (auto& pat : p.patterns)
  1524. if (! pat.notes.empty())
  1525. {
  1526. ++withNotes;
  1527. break;
  1528. }
  1529. for (auto& a : p.arrangements)
  1530. for (auto& item : a.items)
  1531. CHECK (item.track >= 0 && item.track < 500);
  1532. for (auto& clip : song.playlist)
  1533. {
  1534. CHECK (clip.pattern >= 0 && clip.pattern < (int) song.patterns.size());
  1535. CHECK (clip.length > 0 && clip.offset >= 0);
  1536. if (clip.kind == lynx::ClipKind::automation)
  1537. CHECK (clip.automation >= 0 && clip.automation < (int) song.automations.size());
  1538. }
  1539. for (auto& a : song.automations)
  1540. {
  1541. ++automations;
  1542. automationsWithTarget += a.target.kind != lynx::AutomationTargetKind::none;
  1543. CHECK (a.isLive() || ! a.points.empty());
  1544. for (std::size_t i = 1; i < a.points.size(); ++i)
  1545. CHECK (a.points[i].tick >= a.points[i - 1].tick);
  1546. }
  1547. // imported routing has no cycles and valid destinations
  1548. CHECK (song.mixerOrder().size() + 1 == song.mixer.size());
  1549. for (auto& t : song.mixer)
  1550. for (auto& s : t.sends)
  1551. {
  1552. ++sends;
  1553. CHECK (s.target > 0 && s.target < (int) song.mixer.size());
  1554. }
  1555. if (! song.playlist.empty())
  1556. ++withPlaylist;
  1557. }
  1558. catch (const flp::Error& e)
  1559. {
  1560. ++failures;
  1561. std::printf (" FAILED %s: %s\n", entry.path().string().c_str(), e.what());
  1562. }
  1563. }
  1564. CHECK (threeOscDecoded == threeOsc);
  1565. std::printf (" Harmless: %d channels, %d decoded\n", harmless, harmlessDecoded);
  1566. std::printf (" Sytrus: %d channels, %d decoded, %d by the engine (%d of %d rendered sound)\n", sytrus, sytrusDecoded, sytrusEngine,
  1567. sytrusSounding, sytrusRendered);
  1568. CHECK (sytrusEngine == sytrus);
  1569. CHECK (sytrusSounding >= sytrusRendered * 3 / 4);
  1570. for (const auto& [name, counter] : generators)
  1571. {
  1572. std::printf (" %s: %d channels, %d decoded\n", name.c_str(), counter.first, counter.second);
  1573. CHECK (counter.second == counter.first);
  1574. }
  1575. std::printf (" Ogun spectra matching the Trumpet reference: %d\n", ogunTrumpet);
  1576. CHECK (ogunTrumpet >= 1);
  1577. // demo Gross Beat: the volume slot becomes the gate (those using an envelope slot get steps < 1)
  1578. {
  1579. int vocodexes = 0, vocodexSides = 0;
  1580. int lovePhilterLinks[4] {}; // live links: cutoff, resonance; inside containers: cutoff, resonance
  1581. int embeddedZones = 0, lovePhilters = 0, lovePhiltersRead = 0, lovePhiltersConverted = 0, gates = 0, shaped = 0, effectors = 0, patchers = 0, serial = 0, parallel = 0, generatorPatchers = 0;
  1582. for (const auto& entry : std::filesystem::recursive_directory_iterator (std::filesystem::path (realFlpDir) / "Demo projects"))
  1583. if (entry.path().extension() == ".flp")
  1584. {
  1585. std::ifstream in (entry.path(), std::ios::binary);
  1586. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  1587. try
  1588. {
  1589. const auto project = flp::parseProject (file);
  1590. for (const auto& insert : project.mixer)
  1591. for (const auto& slot : insert.slots)
  1592. if (slot.plugin == "Gross Beat")
  1593. {
  1594. // FL's own engine (REA_rodada53): every demo state is read
  1595. const auto fx = lynx::effectFromFl (slot.plugin, slot.state);
  1596. CHECK (fx && fx->kind == lynx::EffectKind::grossBeat);
  1597. ++gates;
  1598. if (fx && fx->kind == lynx::EffectKind::grossBeat && lynx::grossbeat::slotNames (fx->state, true).size() == 36)
  1599. ++shaped;
  1600. }
  1601. else if (slot.plugin == "Fruity Love Philter")
  1602. {
  1603. ++lovePhilters;
  1604. lovePhiltersRead += lynx::parseLovePhilter (slot.state).has_value();
  1605. if (const auto conv = lynx::lovePhilterEffects (slot.state))
  1606. {
  1607. ++lovePhiltersConverted;
  1608. CHECK (! conv->slots.empty());
  1609. for (const auto& fx : conv->slots)
  1610. for (auto v : fx.params)
  1611. CHECK (std::isfinite (v) && v >= 0.0f && v <= 1.0f);
  1612. for (const auto& fx : conv->slots)
  1613. for (const auto& link : fx.links)
  1614. {
  1615. const auto& chain = fx.branches[(std::size_t) link.branch].chain;
  1616. CHECK (link.source < (int) chain.size() && chain[(std::size_t) link.target].kind == lynx::EffectKind::parametricEq);
  1617. lovePhilterLinks[2 + (link.param == lynx::peq::param (0, 3) ? 1 : 0)]++;
  1618. }
  1619. for (const auto& link : conv->links)
  1620. {
  1621. lovePhilterLinks[link.param == lynx::peq::param (0, 3) ? 1 : 0]++;
  1622. CHECK (link.curve.size() == 33);
  1623. for (auto v : link.curve)
  1624. CHECK (std::isfinite (v) && v >= 0.0f && v <= 1.0f);
  1625. }
  1626. }
  1627. }
  1628. else if (slot.plugin == "Vocodex")
  1629. {
  1630. const auto v = lynx::vocodexEffect (slot.state);
  1631. CHECK (v.has_value());
  1632. vocodexes += v.has_value();
  1633. vocodexSides += v && (v->modIn > 0 || v->carIn > 0);
  1634. }
  1635. else if (slot.plugin == "Effector")
  1636. effectors += lynx::effectFromFl (slot.plugin, slot.state).has_value();
  1637. else if (slot.plugin == "Patcher")
  1638. {
  1639. const auto graph = lynx::parsePatcher (slot.state);
  1640. CHECK (graph.has_value());
  1641. ++patchers;
  1642. if (graph && ! graph->serialChain.empty())
  1643. ++serial;
  1644. if (graph && graph->serialChain.empty() && graph->audio)
  1645. ++parallel;
  1646. }
  1647. for (const auto& c : project.channels)
  1648. if (c.pluginName == "DirectWave")
  1649. if (const auto program = lynx::directWaveProgram (c.pluginState))
  1650. for (const auto& wav : program->embeddedWav)
  1651. if (! wav.empty())
  1652. {
  1653. ++embeddedZones;
  1654. CHECK (wav.size() > 44 && std::memcmp (wav.data(), "RIFF", 4) == 0);
  1655. }
  1656. for (const auto& c : project.channels)
  1657. if (c.pluginName == "Patcher")
  1658. {
  1659. const auto graph = lynx::parsePatcher (c.pluginState);
  1660. CHECK (graph.has_value());
  1661. generatorPatchers += graph && graph->mainGenerator >= 0;
  1662. }
  1663. }
  1664. catch (const flp::Error&) {}
  1665. }
  1666. CHECK (gates >= 30 && shaped == gates);
  1667. std::printf (" Gross Beat: %d slots, %d read exactly; Effector: %d converted; Love Philter: %d, %d read\n", gates, shaped, effectors,
  1668. lovePhilters, lovePhiltersRead);
  1669. CHECK (lovePhiltersRead == lovePhilters && lovePhiltersConverted == lovePhilters);
  1670. std::printf (" Vocodex: %d converted, %d with side inputs\n", vocodexes, vocodexSides);
  1671. CHECK (vocodexes >= 9);
  1672. std::printf (" Love Philter live links: %d cutoff, %d resonance; in multi-unit containers: %d cutoff, %d resonance\n",
  1673. lovePhilterLinks[0], lovePhilterLinks[1], lovePhilterLinks[2], lovePhilterLinks[3]);
  1674. CHECK (lovePhilterLinks[2] + lovePhilterLinks[3] > 0);
  1675. std::printf (" DirectWave: %d zones with embedded audio\n", embeddedZones);
  1676. CHECK (embeddedZones == 12);
  1677. std::printf (" Patcher: %d in the mixer (%d serial, %d reduced to serial/parallel), %d as instrument with generator\n", patchers, serial,
  1678. parallel, generatorPatchers);
  1679. CHECK (gates == 0 || shaped > 0);
  1680. }
  1681. // Envelope Controller "Basic Sidechain" (Future House): drops on the note and returns to ~1 within ~1/3 s
  1682. {
  1683. const auto path = std::filesystem::path (realFlpDir) / "Demo projects" / "Demo songs" / "Asher Postman - Future House.flp";
  1684. if (std::filesystem::exists (path))
  1685. {
  1686. std::ifstream in (path, std::ios::binary);
  1687. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  1688. const auto song = lynx::importFlp (flp::parseProject (file));
  1689. const lynx::ChannelDef* controller = nullptr;
  1690. for (const auto& c : song.channels)
  1691. if (c.instrument == lynx::InstrumentKind::controller)
  1692. controller = &c;
  1693. CHECK (controller != nullptr);
  1694. if (controller != nullptr)
  1695. {
  1696. auto inst = lynx::makeInstrument (lynx::InstrumentKind::controller);
  1697. inst->prepare (48000.0);
  1698. inst->configure (*controller, song.bpm);
  1699. std::vector<float> l (480), r (480);
  1700. inst->render (l.data(), r.data(), 480, 1.0f, 1.0f);
  1701. inst->noteOn (60, 1.0f);
  1702. inst->render (l.data(), r.data(), 480, 1.0f, 1.0f); // 10 ms
  1703. const auto ducked = inst->controlOutput (0);
  1704. for (int i = 0; i < 100; ++i) // +1 s
  1705. inst->render (l.data(), r.data(), 480, 1.0f, 1.0f);
  1706. const auto recovered = inst->controlOutput (0);
  1707. std::printf (" Envelope Controller (Basic Sidechain): %.2f on the note, %.2f after 1 s\n", ducked, recovered);
  1708. CHECK (ducked < 0.3f && recovered > 0.9f);
  1709. }
  1710. }
  1711. }
  1712. // mutation fuzz: real states with flipped and truncated bytes must not crash the readers
  1713. {
  1714. std::map<std::string, std::vector<std::uint8_t>> samples; // one real blob per plugin
  1715. for (const auto& entry : std::filesystem::recursive_directory_iterator (std::filesystem::path (realFlpDir) / "Demo projects"))
  1716. {
  1717. if (entry.path().extension() != ".flp")
  1718. continue;
  1719. std::ifstream in (entry.path(), std::ios::binary);
  1720. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  1721. try
  1722. {
  1723. const auto project = flp::parseProject (file);
  1724. for (const auto& c : project.channels)
  1725. if (! c.pluginState.empty())
  1726. samples.emplace (c.pluginName, c.pluginState);
  1727. for (const auto& insert : project.mixer)
  1728. for (const auto& slot : insert.slots)
  1729. if (! slot.state.empty())
  1730. samples.emplace (slot.plugin, slot.state);
  1731. }
  1732. catch (const flp::Error&) {}
  1733. }
  1734. std::uint32_t seed = 12345;
  1735. auto rnd = [&seed] { seed = seed * 1664525u + 1013904223u; return seed >> 8; };
  1736. int mutations = 0;
  1737. for (const auto& [name, original] : samples)
  1738. for (int round = 0; round < 40; ++round)
  1739. {
  1740. auto blob = original;
  1741. if (round % 4 == 3 && ! blob.empty())
  1742. blob.resize (rnd() % blob.size()); // truncated
  1743. else
  1744. for (int k = 0; k < 1 + (int) (rnd() % 8) && ! blob.empty(); ++k)
  1745. blob[rnd() % blob.size()] = (std::uint8_t) rnd();
  1746. ++mutations;
  1747. lynx::SynthSettings synth;
  1748. float gain = 1.0f;
  1749. lynx::synthFromSytrus (blob, 120.0, synth, gain);
  1750. lynx::synthFromHarmor (blob, synth, gain);
  1751. lynx::synthFromGms (blob, 120.0, synth, gain);
  1752. lynx::synthFromOgun (blob, synth, gain);
  1753. lynx::synthFromMorphine (blob, synth, gain);
  1754. lynx::synthFromKepler (blob, synth, gain);
  1755. lynx::synthFromPoiZone (blob, synth, gain);
  1756. lynx::synthFromToxic (blob, synth, gain);
  1757. lynx::synthFromBassDrum (blob, synth, gain);
  1758. lynx::synthFromHarmless (blob, 120.0, synth, gain);
  1759. lynx::harmlessParams (blob);
  1760. if (lynx::harmorStateUsable (blob))
  1761. lynx::harmorFallbacks (blob);
  1762. lynx::parseEnvelopeController (blob);
  1763. lynx::parseKeyboardController (blob);
  1764. lynx::parseLovePhilter (blob);
  1765. lynx::parsePatcher (blob);
  1766. lynx::effectFromFl (name, blob);
  1767. lynx::fpcPads (blob);
  1768. lynx::directWaveProgram (blob);
  1769. CHECK (std::isfinite (gain) && std::isfinite (synth.cutoff));
  1770. }
  1771. std::printf (" mutation fuzz: %d blobs from %d plugins\n", mutations, (int) samples.size());
  1772. }
  1773. // WavPack (.wv) samples from FL packs: all decode, with finite, non-silent audio
  1774. {
  1775. int files = 0, decoded = 0, audible = 0;
  1776. double seconds = 0.0;
  1777. for (const auto& entry : std::filesystem::recursive_directory_iterator (std::filesystem::path (realFlpDir) / "Patches" / "Packs"))
  1778. if (entry.path().extension() == ".wv")
  1779. {
  1780. std::ifstream in (entry.path(), std::ios::binary);
  1781. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  1782. ++files;
  1783. lynx::DecodedAudio audio;
  1784. if (! lynx::decodeWavPack (file.data(), file.size(), audio))
  1785. continue;
  1786. ++decoded;
  1787. float peak = 0.0f;
  1788. bool finite = true;
  1789. for (const auto& ch : audio.channels)
  1790. for (auto v : ch)
  1791. {
  1792. finite = finite && std::isfinite (v);
  1793. peak = std::max (peak, std::abs (v));
  1794. }
  1795. CHECK (finite && peak <= 4.0f);
  1796. audible += peak > 1e-4f;
  1797. seconds += (double) audio.channels[0].size() / audio.sampleRate;
  1798. }
  1799. std::printf (" WavPack: %d .wv files, %d decoded, %d audible, %.0f s of audio\n", files, decoded, audible, seconds);
  1800. CHECK (decoded == files);
  1801. }
  1802. // native GMS presets (.gmsynth = the contents of 0xD5 without the "SSLF")
  1803. int gmsPresets = 0, gmsDecoded = 0;
  1804. const auto gmsFolder = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "GMS";
  1805. if (std::filesystem::exists (gmsFolder))
  1806. for (const auto& entry : std::filesystem::recursive_directory_iterator (gmsFolder))
  1807. if (entry.path().extension() == ".gmsynth")
  1808. {
  1809. std::ifstream in (entry.path(), std::ios::binary);
  1810. std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  1811. std::vector<std::uint8_t> blob { 'S', 'S', 'L', 'F' };
  1812. const auto n = (std::uint32_t) file.size();
  1813. blob.insert (blob.end(), reinterpret_cast<const std::uint8_t*> (&n), reinterpret_cast<const std::uint8_t*> (&n) + 4);
  1814. blob.insert (blob.end(), file.begin(), file.end());
  1815. lynx::SynthSettings gs;
  1816. float g = 1.0f;
  1817. ++gmsPresets;
  1818. if (lynx::synthFromGms (blob, 120.0, gs, g))
  1819. {
  1820. ++gmsDecoded;
  1821. CHECK (std::isfinite (g) && gs.cutoff >= 20.0f && std::isfinite (gs.pitchEnv.amount) && std::isfinite (gs.lfo[lynx::lfoPitch].period));
  1822. }
  1823. }
  1824. std::printf (" GMS presets: %d, %d decoded\n", gmsPresets, gmsDecoded);
  1825. // the synth's "FL presets" button: Sytrus/Harmor .fst and .gmsynth become synth settings
  1826. int presetFiles = 0, presetsRead = 0;
  1827. for (const auto* name : { "Sytrus", "Harmor", "GMS", "Harmless", "Ogun", "Sakura", "Fruit Kick", "Fruity DX10", "MiniSynth", "Kepler",
  1828. "PoiZone", "Toxic Biohazard", "BassDrum" })
  1829. {
  1830. const auto folder = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / name;
  1831. if (! std::filesystem::exists (folder))
  1832. continue;
  1833. for (const auto& entry : std::filesystem::recursive_directory_iterator (folder))
  1834. if (entry.path().extension() == ".fst" || entry.path().extension() == ".gmsynth")
  1835. {
  1836. std::ifstream in (entry.path(), std::ios::binary);
  1837. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  1838. ++presetFiles;
  1839. presetsRead += lynx::synthFromFlPreset (file).has_value();
  1840. }
  1841. }
  1842. std::printf (" FL synth presets: %d files, %d read\n", presetFiles, presetsRead);
  1843. CHECK (presetsRead >= presetFiles * 95 / 100);
  1844. CHECK (gmsDecoded == gmsPresets);
  1845. CHECK (sytrusDecoded == sytrus);
  1846. CHECK (harmlessDecoded == harmless && harmless > 100);
  1847. std::printf (" Harmless: %d states parsed for the engine, %d channels imported as Harmless\n", harmlessParsed, harmlessKind);
  1848. CHECK (harmlessParsed == harmless && harmlessKind >= harmless);
  1849. std::printf (" Harmor: %d channels, %d states usable by the engine, %d imported as Harmor (%d sounding)\n", harmor, harmorUsable, harmorKind, harmorSounding);
  1850. CHECK (harmorUsable == harmor && harmor > 90 && harmorKind >= harmor && harmorSounding >= harmorKind * 9 / 10);
  1851. 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",
  1852. gms, gmsUsable, gmsKind, gmsSounding, morphine, morphineUsable, morphineKind, morphineSounding, demoGoldens);
  1853. CHECK (gmsUsable == gms && gms >= 9 && gmsKind == gms && gmsSounding == gmsKind);
  1854. std::printf (" MiniSynth channels: %d, %d usable, %d on the exact engine, %d sounding, %d golden\n", miniSynth, miniSynthUsable, miniSynthKind,
  1855. miniSynthSounding, miniSynthGoldens);
  1856. CHECK (miniSynthUsable == miniSynth && miniSynth >= 12 && miniSynthKind == miniSynth && miniSynthSounding == miniSynthKind && miniSynthGoldens == 2);
  1857. CHECK (morphineUsable == morphine && morphine >= 7 && morphineKind == morphine && morphineSounding == morphineKind && demoGoldens == 2);
  1858. std::printf (" PoiZone: %d channels, %d usable, %d imported as PoiZone (%d sounding), %d demo golden\n", poizone, poizoneUsable, poizoneKind,
  1859. poizoneSounding, poizoneGoldens);
  1860. CHECK (poizoneUsable == poizone && poizone >= 2 && poizoneKind == poizone && poizoneSounding == poizoneKind && poizoneGoldens == 1);
  1861. std::printf (" Toxic Biohazard: %d channels, %d usable, %d imported as Toxic (%d sounding), %d golden\n", toxic, toxicUsable, toxicKind,
  1862. toxicSounding, toxicGoldens);
  1863. CHECK (toxicUsable == toxic && toxic >= 3 && toxicKind == toxic && toxicSounding == toxicKind && toxicGoldens == 1);
  1864. std::printf (" Ogun: %d channels, %d states usable by the engine, %d imported as Ogun (%d sounding)\n", ogun, ogunUsable, ogunKind, ogunSounding);
  1865. CHECK (ogunUsable == ogun && ogun >= 20 && ogunKind >= ogun && ogunSounding >= ogunKind * 9 / 10);
  1866. std::printf (" Autogun: %d channels, %d states usable by the engine, %d imported as Autogun (%d with SG.dat, %d sounding)\n", autogun,
  1867. autogunUsable, autogunKind, autogunWithSg, autogunSounding);
  1868. CHECK (autogunUsable == autogun && autogun >= 9 && autogunKind == autogun && autogunWithSg == autogun && autogunSounding == autogun);
  1869. std::printf (" Fruity DX10: %d channels, %d states usable by the engine, %d imported as DX10 (%d sounding)\n", dx10, dx10Usable, dx10Kind, dx10Sounding);
  1870. CHECK (dx10Usable == dx10 && dx10 >= 12 && dx10Kind == dx10 && dx10Sounding == dx10Kind);
  1871. std::printf (" Kepler: %d channels, %d states usable by the engine, %d imported as Kepler (%d sounding)\n", kepler, keplerUsable, keplerKind, keplerSounding);
  1872. CHECK (keplerUsable == kepler && kepler >= 4 && keplerSounding == keplerKind && (keplerKind == 0 || keplerKind == kepler));
  1873. std::printf (" Harmless rack compressor: %d of %d presets read from the installation\n", harmlessCompLoaded, harmlessCompWanted);
  1874. CHECK (harmlessCompLoaded == harmlessCompWanted && harmlessCompWanted > 100);
  1875. std::printf (" Maximus: %d of %d slots on the exact engine; Soundgoodizer: %d of %d; Vocodex with SG.dat: %d of %d\n", maximusExact,
  1876. maximusSlots, sgzExact, sgzSlots, vocodexWithData, vocodexSlots);
  1877. // REA_rodada40 §6: in Demo projects the 4 states left on the approximation use the Frequency Splitter linear-phase
  1878. // option; the other Data folders add states with options not decoded
  1879. CHECK (maximusSlots - maximusExact <= 16 && maximusExact >= 97 && sgzExact == sgzSlots && sgzSlots > 90);
  1880. CHECK (vocodexWithData == vocodexSlots && vocodexSlots >= 10);
  1881. std::printf (" Fruity WaveShaper: %d of %d slots on the exact engine; Fruity Soft Clipper: %d of %d\n", shaperExact, shaperSlots,
  1882. clipperExact, clipperSlots);
  1883. // (Patcher graphs add WaveShaper nodes of their own to the imported mixer)
  1884. CHECK (shaperExact >= shaperSlots && shaperSlots >= 70 && clipperExact >= clipperSlots && clipperSlots >= 25); // REA_rodada51
  1885. std::printf (" Fruity Parametric EQ 2 / Parametric EQ: %d of %d slots on the exact engine\n", flPeqExact, flPeqSlots);
  1886. CHECK (flPeqExact >= flPeqSlots && flPeqSlots >= 1100); // REA_rodada50
  1887. std::printf (" Fruity PanOMatic / Balance / Blood Overdrive: %d of %d slots on the exact engines\n", basicExact, basicSlots);
  1888. // one PanOMatic of "Bombs Away" (insert 69, last slot) falls off LYNX's 10-slot limit after a serial Patcher expands
  1889. CHECK (basicExact + 1 >= basicSlots && basicSlots >= 270); // REA_rodada58
  1890. // (Patcher graphs can add Distructor nodes of their own to the imported mixer)
  1891. std::printf (" Distructor: %d of %d slots on the exact engine (%d cabinet impulses read from the installation)\n",
  1892. distructorExact, distructorSlots, distructorImpulses);
  1893. CHECK (distructorExact >= distructorSlots && distructorSlots >= 4 && distructorImpulses >= 1); // REA_rodada71
  1894. std::printf (" EQUO: %d of %d slots on the exact engine\n", equoExact, equoSlots);
  1895. CHECK (equoExact == equoSlots && equoSlots >= 10); // REA_rodada61
  1896. std::printf (" Pitch Shifter: %d of %d slots on the exact engine\n", pitchShifterExact, pitchShifterSlots);
  1897. CHECK (pitchShifterExact == pitchShifterSlots && pitchShifterSlots >= 15); // REA_rodada61
  1898. std::printf (" Fruity Fast LP / Free Filter / Filter: %d of %d slots on the exact engines\n", filterExact, filterSlots);
  1899. CHECK (filterExact >= filterSlots && filterSlots >= 53); // REA_rodada63
  1900. std::printf (" Fruity Delay 2: %d of %d slots on the exact engine; Fruity Delay 3: %d of %d\n", delay2Exact, delay2Slots,
  1901. delay3Exact, delay3Slots);
  1902. CHECK (delay2Exact == delay2Slots && delay2Slots >= 60 && delay3Exact == delay3Slots && delay3Slots >= 100); // REA_rodada52
  1903. std::printf (" Chorus family: %d of %d slots on the exact engines\n", chorusExact, chorusSlots);
  1904. CHECK (chorusExact >= chorusSlots && chorusSlots >= 47); // REA_rodada59
  1905. std::printf (" Fruity Fast Dist: %d of %d slots on the exact engine; Fruity Flangus: %d of %d; Fruity Flanger: %d of %d\n", fastDistExact,
  1906. fastDistSlots, flangusExact, flangusSlots, flangerExact, flangerSlots);
  1907. CHECK (fastDistExact >= fastDistSlots && fastDistSlots >= 30 && flangusExact >= flangusSlots && flangusSlots >= 30
  1908. && flangerExact >= flangerSlots && flangerSlots >= 3); // REA_rodada55
  1909. // 3x Osc channel presets shipped with FL (.fst): all become synth settings
  1910. int presets = 0, presetsDecoded = 0;
  1911. const auto presetDir = std::filesystem::path (realFlpDir) / "Patches" / "Channel presets" / "3x Osc";
  1912. if (std::filesystem::is_directory (presetDir))
  1913. for (auto& entry : std::filesystem::directory_iterator (presetDir))
  1914. if (entry.path().extension() == ".fst")
  1915. {
  1916. ++presets;
  1917. std::ifstream in (entry.path(), std::ios::binary);
  1918. std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  1919. if (auto synth = lynx::synthFromFlPreset (bytes))
  1920. {
  1921. ++presetsDecoded;
  1922. CHECK (synth->osc[0].level + synth->osc[1].level + (synth->am ? 0.0f : synth->osc[2].level) <= 1.001f);
  1923. }
  1924. }
  1925. std::printf (" %d 3x Osc presets, %d read\n", presets, presetsDecoded);
  1926. CHECK (presetsDecoded == presets);
  1927. // FL effect presets that become built-in effects
  1928. int fxPresets = 0, fxDecoded = 0;
  1929. for (auto kind : { lynx::EffectKind::parametricEq, lynx::EffectKind::limiter, lynx::EffectKind::reverb, lynx::EffectKind::delay,
  1930. lynx::EffectKind::utility })
  1931. for (const auto& plugin : lynx::flPluginsForEffect (kind))
  1932. {
  1933. const auto dir = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Effects" / plugin;
  1934. if (! std::filesystem::is_directory (dir))
  1935. continue;
  1936. for (auto& entry : std::filesystem::recursive_directory_iterator (dir))
  1937. if (entry.path().extension() == ".fst")
  1938. {
  1939. ++fxPresets;
  1940. std::ifstream in (entry.path(), std::ios::binary);
  1941. std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  1942. fxDecoded += lynx::effectFromFlPreset (bytes).has_value();
  1943. }
  1944. }
  1945. std::printf (" %d FL effect presets, %d read\n", fxPresets, fxDecoded);
  1946. CHECK (fxDecoded == fxPresets);
  1947. CHECK (wrappedParsed == wrapped);
  1948. CHECK (wrappedVst2State == wrappedVst2);
  1949. std::printf (" %d plugins in Fruity Wrapper, %d read; %d VST2 states, %d decoded\n", wrapped, wrappedParsed, wrappedVst2, wrappedVst2State);
  1950. std::printf (" %d markers, %d time signature changes, %d tracks with EQ\n", markers, signatures, trackEqs);
  1951. std::printf (" %d parametric EQs (%d with FL settings), %d reverbs (%d without dry)\n", peqSlots, peqShaped, reverbs, reverbsDryless);
  1952. std::printf (" DirectWave: %d channels, %d zones; FPC: %d projects, %d pad channels, %d pad notes\n", dwChannels, dwZones, fpcKits,
  1953. padChannels, padNotes);
  1954. CHECK (dwZones > 1000 && padChannels > 250 && padNotes > 500);
  1955. std::printf (" DirectWave on the exact engine (REA_rodada72): %d channels\n", dwExact);
  1956. CHECK (dwExact >= 57);
  1957. std::printf (" sliced channels (Slicer/Slicex): %d; embedded WAVs saved: %d\n", slicedChannels, embeddedSaved);
  1958. CHECK (slicedChannels >= 25 && embeddedSaved >= 10); // without decoding Ogg here, some loops have no duration
  1959. std::printf (" FL Keys (Grand Piano, 30 zones): %d channels\n", flKeysChannels);
  1960. CHECK (flKeysChannels >= 8);
  1961. CHECK (peqSlots > 1000 && peqShaped > 900 && reverbsDryless > 20 && reverbsLowCut > 250);
  1962. CHECK (markers > 300 && signatures > 60);
  1963. std::printf (" %d 3x Osc channels, %d decoded, %d on the exact engine (%d sounding)\n", threeOsc, threeOscDecoded, threeOscKind, threeOscSounding);
  1964. // one demo channel (a noise layer of "Right Night feeling") has the channel filter closed (fast LP, Mod X 0): silent
  1965. // with its saved settings, in FL too
  1966. CHECK (threeOscKind == threeOsc && threeOscSounding == threeOscKind - 1);
  1967. std::printf (" %d .flp files read, %d with notes, %d with playlist, %d automations (%d with target), %d sends\n",
  1968. files, withNotes, withPlaylist, automations, automationsWithTarget, sends);
  1969. CHECK (files > 0);
  1970. CHECK (automations > 0 && sends > 0);
  1971. }
  1972. // ---------------------------------------------------------------------------
  1973. // Sequencer
  1974. TEST (pattern_toggle_step_and_length)
  1975. {
  1976. lynx::Pattern p;
  1977. CHECK (! p.hasStep (0, 4));
  1978. p.toggleStep (0, 4);
  1979. CHECK (p.hasStep (0, 4));
  1980. CHECK (p.notes.size() == 1 && p.notes[0].start == 4 * lynx::ticksPerStep);
  1981. CHECK (p.effectiveLength (4) == 4 * lynx::ppq); // one bar
  1982. p.toggleStep (1, 20); // second bar
  1983. CHECK (p.effectiveLength (4) == 8 * lynx::ppq);
  1984. p.toggleStep (0, 4);
  1985. CHECK (! p.hasStep (0, 4) && p.notes.size() == 1);
  1986. }
  1987. TEST (scheduler_places_events_at_exact_samples)
  1988. {
  1989. lynx::Pattern p;
  1990. p.toggleStep (0, 0);
  1991. p.toggleStep (0, 4); // tick 960 = 1 beat
  1992. const double sr = 48000.0, bpm = 120.0; // 1 beat = 0.5 s = 24000 samples
  1993. const auto tps = lynx::ticksPerSample (bpm, sr);
  1994. lynx::EventBuffer ev;
  1995. lynx::schedulePatternLoop (p, p.effectiveLength (4), 23900.0 * tps, 512, tps, ev);
  1996. // the beat-2 note starts at sample 24000 -> offset 100 in this block
  1997. int ons = 0;
  1998. for (auto& e : ev)
  1999. if (e.type == lynx::NoteEvent::Type::on)
  2000. {
  2001. ++ons;
  2002. CHECK (e.sampleOffset == 100);
  2003. }
  2004. CHECK (ons == 1);
  2005. }
  2006. TEST (scheduler_wraps_the_loop)
  2007. {
  2008. lynx::Pattern p;
  2009. p.toggleStep (2, 0);
  2010. const auto len = p.effectiveLength (4); // 3840 ticks
  2011. const double sr = 48000.0;
  2012. const auto tps = lynx::ticksPerSample (120.0, sr);
  2013. lynx::EventBuffer ev;
  2014. // block that crosses the loop end: starts 10 samples before
  2015. const auto start = (double) len - 10.0 * tps;
  2016. lynx::schedulePatternLoop (p, len, start, 64, tps, ev);
  2017. bool found = false;
  2018. for (auto& e : ev)
  2019. if (e.type == lynx::NoteEvent::Type::on && e.channel == 2)
  2020. {
  2021. found = true;
  2022. CHECK (e.sampleOffset == 10);
  2023. }
  2024. CHECK (found);
  2025. }
  2026. TEST (scheduler_off_before_on_at_same_sample)
  2027. {
  2028. lynx::Pattern p;
  2029. lynx::Note a;
  2030. a.start = 0; a.length = 240; a.channel = 0;
  2031. lynx::Note b = a;
  2032. b.start = 240;
  2033. p.notes = { a, b };
  2034. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  2035. lynx::EventBuffer ev;
  2036. lynx::schedulePatternLoop (p, p.effectiveLength (4), 200.0, 4000, tps, ev);
  2037. CHECK (ev.size() >= 2);
  2038. CHECK (ev[0].type == lynx::NoteEvent::Type::off);
  2039. CHECK (ev[1].type == lynx::NoteEvent::Type::on);
  2040. CHECK (ev[0].sampleOffset == ev[1].sampleOffset);
  2041. }
  2042. TEST (snapshot_exchange_hands_over_and_recycles)
  2043. {
  2044. struct Counted
  2045. {
  2046. int value;
  2047. int* alive;
  2048. Counted (int v, int* a) : value (v), alive (a) { ++*alive; }
  2049. ~Counted() { --*alive; }
  2050. };
  2051. int alive = 0;
  2052. {
  2053. lynx::SnapshotExchange<Counted> x;
  2054. CHECK (x.acquire() == nullptr);
  2055. x.publish (std::make_unique<Counted> (1, &alive));
  2056. CHECK (x.acquire()->value == 1);
  2057. x.publish (std::make_unique<Counted> (2, &alive));
  2058. x.publish (std::make_unique<Counted> (3, &alive)); // 2 was never read: freed immediately
  2059. CHECK (alive == 2);
  2060. CHECK (x.acquire()->value == 3);
  2061. CHECK (alive == 2); // 1 was returned, but only the UI frees it
  2062. x.collectGarbage();
  2063. CHECK (alive == 1);
  2064. // without collection, the return slots fill up and the audio side holds on to the current one
  2065. for (int i = 0; i < 20; ++i)
  2066. {
  2067. x.publish (std::make_unique<Counted> (10 + i, &alive));
  2068. CHECK (x.acquire() != nullptr);
  2069. }
  2070. }
  2071. CHECK (alive == 0);
  2072. }
  2073. TEST (rack_renderer_makes_sound_and_reuses_instruments)
  2074. {
  2075. auto song = lynx::Song::makeDefault();
  2076. song.bpm = 120.0;
  2077. const double sr = 48000.0;
  2078. auto state = lynx::RackState::build (song, nullptr, sr);
  2079. lynx::RackRenderer r;
  2080. r.prepare (sr);
  2081. std::vector<float> l (512), rr (512);
  2082. double tick = 0.0;
  2083. float peak = 0.0f;
  2084. int kicks = 0;
  2085. for (int block = 0; block < 93; ++block) // 47616 samples: just before 1 s
  2086. {
  2087. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  2088. for (auto& e : r.lastEvents())
  2089. if (e.type == lynx::NoteEvent::Type::on && e.channel == 0)
  2090. ++kicks;
  2091. for (int i = 0; i < 512; ++i)
  2092. {
  2093. CHECK (std::isfinite (l[(std::size_t) i]));
  2094. peak = std::max (peak, std::abs (l[(std::size_t) i]));
  2095. }
  2096. tick += 512 * lynx::ticksPerSample (song.bpm, sr);
  2097. }
  2098. CHECK (peak > 0.1f && peak < 4.0f);
  2099. CHECK (kicks == 2); // 120 BPM: beats at 0 and 0.5 s
  2100. // editing the song keeps the instruments of channels whose type didn't change
  2101. song.channels[4].instrument = lynx::InstrumentKind::kick;
  2102. auto next = lynx::RackState::build (song, state.get(), sr);
  2103. CHECK (next->instruments[0] == state->instruments[0]);
  2104. CHECK (next->instruments[4] != state->instruments[4]);
  2105. // stopping silences the synth and triggers no new notes
  2106. r.process (next.get(), false, tick, 512, l.data(), rr.data());
  2107. CHECK (r.lastEvents().size() == 0);
  2108. }
  2109. TEST (flp_import_maps_channels_and_rescales)
  2110. {
  2111. flp::Project p;
  2112. p.header.ppq = 96;
  2113. p.tempo = 150.0;
  2114. p.channels = { { 0, flp::ChannelType::sampler, "Kick 808", "", "", 1 },
  2115. { 1, flp::ChannelType::audioClip, "Vocal", "", "", 2 },
  2116. { 2, flp::ChannelType::instrument, "Lead", "FLEX", "", 3 } };
  2117. flp::Pattern pat;
  2118. pat.index = 1;
  2119. flp::Note n;
  2120. n.position = 96;
  2121. n.length = 0;
  2122. n.channel = 2;
  2123. n.key = 72;
  2124. n.velocity = 64;
  2125. pat.notes.push_back (n);
  2126. p.patterns.push_back (pat);
  2127. const auto s = lynx::importFlp (p);
  2128. CHECK (s.bpm == 150.0);
  2129. CHECK (s.channels.size() == 3); // audio clip channels stay in the rack, as in FL
  2130. CHECK (s.channels[0].instrument == lynx::InstrumentKind::kick);
  2131. CHECK (s.channels[1].instrument == lynx::InstrumentKind::audioClip);
  2132. CHECK (s.channels[2].instrument == lynx::InstrumentKind::synth);
  2133. CHECK (s.patterns.size() == 1);
  2134. CHECK (s.patterns[0].notes[0].start == lynx::ppq); // 96 @ PPQ 96 = 1 beat
  2135. CHECK (s.patterns[0].notes[0].channel == 2);
  2136. CHECK (s.patterns[0].notes[0].length == lynx::ticksPerStep); // step note
  2137. CHECK (std::abs (s.patterns[0].notes[0].velocity - 0.5f) < 1e-6f);
  2138. }
  2139. TEST (live_queue_is_fifo_and_bounded)
  2140. {
  2141. lynx::LiveEventQueue q;
  2142. lynx::NoteEvent e;
  2143. CHECK (! q.pop (e));
  2144. for (int i = 0; i < 10; ++i)
  2145. {
  2146. lynx::NoteEvent in;
  2147. in.key = i;
  2148. CHECK (q.push (in));
  2149. }
  2150. for (int i = 0; i < 10; ++i)
  2151. CHECK (q.pop (e) && e.key == i);
  2152. int pushed = 0;
  2153. while (q.push (e))
  2154. ++pushed;
  2155. CHECK (pushed == (int) lynx::LiveEventQueue::capacity - 1); // one slot stays free to tell full from empty
  2156. }
  2157. // FL's sampler interpolators (REA_rodada69 §1): outputs of FLEngine's own kernels (EXEC, r69/py/gold.py) on a synthetic
  2158. // 16-bit stereo signal; the port must give the same bits (linear, 6-point, 24-point sinc, 32-point sinc at speed 1.37,
  2159. // 16-point sinc widened to 24 points with the cutoff at π/9.5 at speed 9.5)
  2160. TEST (fl_sampler_interpolators_match_fl)
  2161. {
  2162. std::vector<float> l, r;
  2163. std::uint32_t x = 12345;
  2164. for (int k = 0; k < 2 * 144; ++k)
  2165. {
  2166. x = x * 1103515245u + 12345u;
  2167. const auto v = (float) (std::int16_t) (std::uint16_t) (x >> 16) / 32768.0f;
  2168. (k % 2 == 0 ? l : r).push_back (v);
  2169. }
  2170. const int pad = 40;
  2171. const std::pair<int, double> positions[] = { { 20, 0.0 }, { 21, 0.25 }, { 22, 0.5 }, { 23, 0.71828 }, { 30, 0.999 }, { 31, 1e-7 }, { 40, 0.3333 }, { 41, 0.875 } };
  2172. struct Case { int quality; double speed; std::uint32_t bits[16]; };
  2173. const Case cases[] = {
  2174. { 1, 1.0, { 0x3ec1a000, 0x3d774000, 0x3ca1e000, 0x3ec39900, 0xbf2d4600, 0xbe096800, 0xbea0cd17, 0x3f433097, 0x3f2796da, 0xbd9fbd17, 0x3f27a3ff, 0xbda03ff2, 0x3dace746, 0x3eba19c4, 0x3e477a00, 0x3f0f2a80 } },
  2175. { 2, 1.0, { 0x3ec1a000, 0x3d773fff, 0x3d34885d, 0x3eedeaa0, 0xbf49e298, 0xbea52fc4, 0xbeba55fc, 0x3f7c58da, 0x3f27c5c2, 0xbda139af, 0x3f27a3ff, 0xbda03ffa, 0x3dd8e7b1, 0x3ee872c1, 0x3e05b2d5, 0x3f144c4f } },
  2176. { 4, 1.0, { 0x3ec19fff, 0x3d773fff, 0x3d889c32, 0x3ef6b184, 0xbf57cf0d, 0xbf0abecd, 0xbea14a13, 0x3f94f11b, 0x3f27edc4, 0xbda10d8c, 0x3f27a3fd, 0xbda03ffa, 0x3e5baf41, 0x3edb298c, 0x3dd76cbf, 0x3ef9d212 } },
  2177. { 5, 1.37, { 0x3f19d373, 0x3ea9b6a3, 0xbdcbe56e, 0x3e2af6c6, 0xbf476f7a, 0xbe566a13, 0xbe9f166a, 0x3f52b5ad, 0x3f27ad16, 0xbce48792, 0x3f27960a, 0xbce01ebe, 0x3e1b96be, 0x3e163fd8, 0x3f042e42, 0x3ec4be83 } },
  2178. { 3, 9.5, { 0xbd97308e, 0x3dd8783c, 0xbd911374, 0x3e162a41, 0xbd8a959e, 0x3e38b220, 0xbd7e6785, 0x3e58895c, 0xbd380c4f, 0x3e7e3008, 0xbd380e4e, 0x3e7e2696, 0x3d00c6d0, 0xbd627d87, 0x3e308cb8, 0xbcece918 } },
  2179. };
  2180. const lynx::flvp::X87Extended precision;
  2181. for (const auto& c : cases)
  2182. {
  2183. lynx::flrs::Kernel k;
  2184. k.setQuality (c.quality);
  2185. k.setSpeed (c.speed);
  2186. int same = 0;
  2187. for (int p = 0; p < 8; ++p)
  2188. {
  2189. const auto lo = pad + positions[p].first - k.halfWidth + 1;
  2190. float a, b;
  2191. lynx::flrs::interpolate (k, l.data() + lo, r.data() + lo, positions[p].second, a, b);
  2192. std::uint32_t ba, bb;
  2193. std::memcpy (&ba, &a, 4);
  2194. std::memcpy (&bb, &b, 4);
  2195. same += (ba == c.bits[2 * p]) + (bb == c.bits[2 * p + 1]);
  2196. }
  2197. CHECK (same == 16);
  2198. }
  2199. CHECK (lynx::flrs::halfWidthFor (4) == 12 && lynx::flrs::halfWidthFor (5) == 16 && lynx::flrs::halfWidthFor (9) == 256);
  2200. CHECK (lynx::flrs::speedFor (0.0f, 1.0) == 1.0 && std::abs (lynx::flrs::speedFor (1200.0f, 0.5) - 1.0) < 1e-12);
  2201. }
  2202. // The sampler at the original rate copies the sample (FL's copy path), and plays until the read position passes the
  2203. // last frame
  2204. TEST (fl_sampler_copies_at_original_rate)
  2205. {
  2206. auto data = std::make_shared<lynx::SampleData>();
  2207. data->sampleRate = 48000.0;
  2208. for (int i = 0; i < 300; ++i)
  2209. data->left.push_back (std::sin (0.05f * (float) i) * 0.5f);
  2210. auto sampler = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  2211. sampler->prepare (48000.0);
  2212. sampler->noteOn (60, 1.0f);
  2213. std::vector<float> l (400), r (400);
  2214. sampler->render (l.data(), r.data(), 400, 1.0f, 1.0f);
  2215. const auto panL = std::cos ((0.0f + 1.0f) * 0.25f * 3.14159265f) * 1.41421356f; // the sampler's centre pan gain
  2216. int exact = 0;
  2217. for (int i = 0; i < 300; ++i)
  2218. exact += l[(std::size_t) i] == data->left[(std::size_t) i] * panL ? 1 : 0;
  2219. CHECK (exact == 300);
  2220. CHECK (l[300] == 0.0f);
  2221. }
  2222. // FL's volume envelope on the sampler (REA_rodada69 §3): the articulator is stepped once per sequencer tick and the level
  2223. // ramps linearly to each tick's value; 96 PPQ at 120 BPM and 48 kHz = 250 samples per tick, attack 20000 = 89 ticks
  2224. TEST (fl_sampler_volume_envelope_steps_per_tick)
  2225. {
  2226. auto data = std::make_shared<lynx::SampleData>();
  2227. data->sampleRate = 48000.0;
  2228. data->left.assign (96000, 0.5f);
  2229. auto sampler = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  2230. sampler->prepare (48000.0);
  2231. lynx::ChannelDef c;
  2232. c.instrument = lynx::InstrumentKind::sampler;
  2233. c.sampler.flVolumeEnvelope = { 0, 1, 100, 20000, 0, 30000, 128, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, 0, 96 };
  2234. sampler->configure (c, 120.0);
  2235. sampler->noteOn (60, 1.0f);
  2236. std::vector<float> l (40000), r (40000);
  2237. sampler->render (l.data(), r.data(), 40000, 1.0f, 1.0f);
  2238. const auto panL = std::cos ((0.0f + 1.0f) * 0.25f * 3.14159265f) * 1.41421356f;
  2239. auto level = [&] (int n) { return l[(std::size_t) n] / (0.5f * panL); };
  2240. CHECK (std::abs (level (1000) - 1000.0f / 22500.0f) < 2e-5f);
  2241. CHECK (std::abs (level (10125) - 10125.0f / 22500.0f) < 2e-5f);
  2242. CHECK (std::abs (level (39000) - 1.0f) < 1e-6f); // the attack ended after 90 ticks; sustain 128 = 1.0
  2243. // release: 20000 = 89 ticks, linear (tension 0), from the current level
  2244. sampler->noteOff (60);
  2245. std::fill (l.begin(), l.end(), 0.0f);
  2246. sampler->render (l.data(), r.data(), 40000, 1.0f, 1.0f);
  2247. CHECK (std::abs (level (2500) - (1.0f - 10.0f / 90.0f)) < 1e-3f);
  2248. CHECK (l[39999] == 0.0f && sampler->idle());
  2249. }
  2250. // FL's native sampler voice (REA_rodada76): FL's own command-line renders of crafted projects (one sampler channel,
  2251. // our noise sample, notes at ticks 0/192/384 of 96 PPQ at 120 BPM, 16-bit output) — the fast LP with resonance and a
  2252. // note pan/velocity set; the 12 dB LP with volume, cutoff and pitch envelopes and their releases. FL's int16 codes at
  2253. // a few frames; LYNX within one code. Also the velocity law and the pan law with the channel's levels inside the voice.
  2254. TEST (fl_native_sampler_voice_matches_fl_renders)
  2255. {
  2256. auto noise = std::make_shared<lynx::SampleData>();
  2257. noise->sampleRate = 44100.0;
  2258. {
  2259. std::uint32_t seed = 1;
  2260. for (int i = 0; i < 26460; ++i)
  2261. {
  2262. seed = (seed * 1103515245u + 12345u) & 0x7fffffffu;
  2263. noise->left.push_back ((float) (((double) seed / 2147483647.0 * 2.0 - 1.0) * 0.3));
  2264. }
  2265. }
  2266. struct Ev { int on, off; float vel, pan; };
  2267. auto render = [&] (std::vector<std::int32_t> recs, std::array<int, 3> filter, const std::vector<Ev>& notes,
  2268. std::shared_ptr<lynx::SampleData> data, float chanGain) {
  2269. lynx::ChannelDef c;
  2270. c.instrument = lynx::InstrumentKind::sampler;
  2271. c.sampler.flChannel = recs;
  2272. c.sampler.flChannel.insert (c.sampler.flChannel.end(), { filter[0], filter[1], filter[2], 96 });
  2273. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  2274. lynx::setOfflineRendering (true); // FL's render dialog: 32-point sinc (the pitch envelope interpolates)
  2275. inst->prepare (44100.0);
  2276. lynx::setOfflineRendering (false);
  2277. inst->configure (c, 120.0);
  2278. inst->setSongPosition (0.0);
  2279. std::vector<std::pair<int, int>> events; // (sample, index ≥ 0 on / < 0 off)
  2280. for (int i = 0; i < (int) notes.size(); ++i)
  2281. events.push_back ({ notes[(std::size_t) i].on, i }), events.push_back ({ notes[(std::size_t) i].off, -1 - i });
  2282. std::sort (events.begin(), events.end());
  2283. std::vector<float> l (120000), r (120000);
  2284. int at = 0;
  2285. for (const auto& [when, i] : events)
  2286. {
  2287. inst->render (l.data() + at, r.data() + at, when - at, chanGain, chanGain);
  2288. at = when;
  2289. if (i >= 0)
  2290. {
  2291. inst->setNextNoteExpression (notes[(std::size_t) i].pan, 0.0f, false);
  2292. inst->noteOn (60, notes[(std::size_t) i].vel);
  2293. }
  2294. else
  2295. inst->noteOff (60);
  2296. }
  2297. inst->render (l.data() + at, r.data() + at, 120000 - at, chanGain, chanGain);
  2298. return std::make_pair (l, r);
  2299. };
  2300. const std::vector<std::int32_t> plain = { 0, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, 0 };
  2301. auto records = [&] (std::vector<std::int32_t> vol, std::vector<std::int32_t> cut, std::vector<std::int32_t> pitch) {
  2302. std::vector<std::int32_t> all = plain;
  2303. for (const auto* r : std::initializer_list<const std::vector<std::int32_t>*> { &vol, &cut, &plain, &pitch })
  2304. all.insert (all.end(), r->begin(), r->end());
  2305. return all;
  2306. };
  2307. const auto volPlain = std::vector<std::int32_t> { 4, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, -101 };
  2308. const auto chan = lynx::channelGain (10000.0f / 12800.0f); // the rack's gain at pan 0 (panGains: gain·√2·cos(π/4))
  2309. const int at[] = { 5, 300, 2000, 9000, 11500, 15000, 22100, 26000 };
  2310. auto check = [&] (const std::pair<std::vector<float>, std::vector<float>>& out, const int* goldL, const int* goldR) {
  2311. int bad = 0;
  2312. for (int n = 0; n < 3; ++n)
  2313. for (int k = 0; k < 8; ++k)
  2314. {
  2315. const auto p = (std::size_t) (44100 * n + at[k]);
  2316. bad += std::abs ((int) std::lround (out.first[p] * 32767.0f) - goldL[8 * n + k]) > 1;
  2317. bad += std::abs ((int) std::lround (out.second[p] * 32767.0f) - goldR[8 * n + k]) > 1;
  2318. }
  2319. return bad;
  2320. };
  2321. {
  2322. // fast LP (Mod X 100, Mod Y 200); notes: len 96, (vel 80, pan 40) len 48, (vel 120, pan 90) len 144
  2323. const auto out = render (records (volPlain, plain, plain), { 100, 200, 0 },
  2324. { { 0, 22050, 100.0f / 128, 0.0f }, { 44100, 55125, 80.0f / 128, -24.0f / 64 }, { 88200, 121275, 120.0f / 128, 26.0f / 64 } },
  2325. noise, chan);
  2326. const int L[] = { -194, -789, 883, 803, 614, -557, -983, -1641, -141, -572, 640, 582, 445, -403, -712, -1189, -207, -841, 942, 856, 655, -593, -1048, -1750 };
  2327. const int R[] = { -194, -789, 883, 803, 614, -557, -983, -1641, -75, -305, 342, 311, 238, -216, -381, -636, -411, -1671, 1871, 1700, 1300, -1179, -2082, -3477 };
  2328. CHECK (check (out, L, R) == 0);
  2329. }
  2330. {
  2331. // 12 dB LP (Mod X 90, Mod Y 60), volume / cutoff / pitch envelopes; notes len 48, (vel 80, pan 30) len 48, len 30
  2332. const auto out = render (records ({ 4, 1, 100, 3000, 20000, 30000, 100, 6000, 0, 100, 20000, 0, 32950, 0, 0, 0, -101 },
  2333. { 0, 1, 100, 6000, 2000, 15000, 40, 8000, 100, 100, 20000, 0, 32950, 0, 0, 0, 0 },
  2334. { 0, 1, 100, 4000, 20000, 9000, 0, 20000, 600, 100, 20000, 0, 32950, 0, 0, 0, 0 }),
  2335. { 90, 60, 1 },
  2336. { { 0, 11025, 100.0f / 128, 0.0f }, { 44100, 55125, 80.0f / 128, -34.0f / 64 }, { 88200, 95091, 100.0f / 128, 0.0f } },
  2337. noise, chan);
  2338. const int L[] = { -1, -203, 29, -298, 97, 0, 0, 0, 0, -156, 22, -228, 74, 0, 0, 0, -1, -203, 29, -2, 0, 0, 0, 0 };
  2339. const int R[] = { -1, -203, 29, -298, 97, 0, 0, 0, 0, -60, 9, -88, 29, 0, 0, 0, -1, -203, 29, -2, 0, 0, 0, 0 };
  2340. CHECK (check (out, L, R) == 0);
  2341. }
  2342. {
  2343. // a global volume LFO (flag 0x20, amount 80, speed 24000): one phase for the channel, (k + 1)·increment at song
  2344. // tick k (notes on ticks 0, 100, 300)
  2345. auto vol = volPlain;
  2346. vol[0] = 4 | 0x20, vol[11] = 80, vol[12] = 24000;
  2347. const auto out = render (records (vol, plain, plain), { 256, 0, 0 },
  2348. { { 0, 22050, 100.0f / 128, 0.0f }, { 22969, 45019, 80.0f / 128, 0.0f }, { 68906, 90956, 100.0f / 128, 0.0f } },
  2349. noise, chan);
  2350. const int pos[] = { 500, 3000, 7000, 12000, 23469, 26000, 30000, 69406, 72000, 80000 };
  2351. const int gold[] = { 1149, 765, 374, 4530, 846, -911, -646, 1160, 2316, 727 };
  2352. int bad = 0;
  2353. for (int k = 0; k < 10; ++k)
  2354. bad += std::abs ((int) std::lround (out.first[(std::size_t) pos[k]] * 32767.0f) - gold[k]) > 1;
  2355. CHECK (bad == 0);
  2356. }
  2357. {
  2358. // a sample with its own loop (WAV smpl 4410…8819): FL loops it while the note lasts and fades it out over 10 ms
  2359. // after the note-off (no volume envelope)
  2360. auto looped = std::make_shared<lynx::SampleData>();
  2361. looped->sampleRate = 44100.0;
  2362. for (int i = 0; i < 8820; ++i)
  2363. looped->left.push_back ((float) ((0.5 * (1.0 - 0.5 * i / 8820.0)) * std::sin (2.0 * 3.141592653589793 * 441.0 * i / 44100.0)));
  2364. looped->loopStart = 4410, looped->loopEnd = 8820;
  2365. const auto out = render (records (volPlain, plain, plain), { 256, 0, 0 },
  2366. { { 0, 22050, 100.0f / 128, 0.0f }, { 44100, 49612, 100.0f / 128, 0.0f } }, looped, chan);
  2367. const int pos[] = { 125, 4525, 8925, 13337, 17725, 22025, 22125, 22325, 22437, 49625, 49737, 50025 };
  2368. const int gold[] = { 6338, 4746, 3842, 4161, -1470, -989, -3577, -1174, -32, 4373, 2573, 42 };
  2369. int bad = 0;
  2370. for (int k = 0; k < 12; ++k)
  2371. bad += std::abs ((int) std::lround (out.first[(std::size_t) pos[k]] * 32767.0f) - gold[k]) > 1;
  2372. CHECK (bad == 0);
  2373. }
  2374. {
  2375. // a constant sample: velocity law (FL: vel 100 → 0.389608, 128 → 0.799753, 64 → 0.141805 at the default
  2376. // channel volume, pan centre) and a hard-left note (0.550997 / 0)
  2377. auto dc = std::make_shared<lynx::SampleData>();
  2378. dc->sampleRate = 44100.0;
  2379. dc->left.assign (20000, 0.5f);
  2380. auto level = [&] (float vel, float pan, bool right) {
  2381. const auto out = render (records (volPlain, plain, plain), { 256, 0, 0 }, { { 0, 10000, vel, pan } }, dc, chan);
  2382. return (right ? out.second : out.first)[5000] / 0.5f;
  2383. };
  2384. CHECK (std::abs (level (100.0f / 128, 0.0f, false) - 0.389608f) < 3e-5f);
  2385. CHECK (std::abs (level (1.0f, 0.0f, false) - 0.799753f) < 3e-5f);
  2386. CHECK (std::abs (level (64.0f / 128, 0.0f, false) - 0.141805f) < 3e-5f);
  2387. CHECK (std::abs (level (100.0f / 128, -1.0f, false) - 0.550997f) < 3e-5f);
  2388. CHECK (std::abs (level (100.0f / 128, -1.0f, true)) < 1e-6f);
  2389. }
  2390. }
  2391. // FPC's "cycle" layers (REA_rodada69 §4): among the layers covering the velocity, in order, the one after the last
  2392. // chosen, wrapping; FL's velocity index is round(v·128). Slicex's master volume uses FL's L11 law (§5).
  2393. TEST (fl_fpc_cycle_layers_and_slicex_master_volume)
  2394. {
  2395. auto level = [] (float v) {
  2396. auto d = std::make_shared<lynx::SampleData>();
  2397. d->sampleRate = 48000.0;
  2398. d->left.assign (4800, v);
  2399. return d;
  2400. };
  2401. // A covers 0…63, B 32…95, C 64…127 (all keys)
  2402. auto inst = lynx::makeZonedSampler ({ { { "a.wav", 60, 0, 127, 0, 63 }, level (0.1f) },
  2403. { { "b.wav", 60, 0, 127, 32, 95 }, level (0.2f) },
  2404. { { "c.wav", 60, 0, 127, 64, 127 }, level (0.3f) } });
  2405. inst->prepare (48000.0);
  2406. inst->configure (lynx::ChannelDef {}, 120.0);
  2407. auto play = [&] (float velocity) {
  2408. inst->allNotesOff();
  2409. std::vector<float> l (2000), r (2000);
  2410. inst->render (l.data(), r.data(), 2000, 1.0f, 1.0f);
  2411. std::fill (l.begin(), l.end(), 0.0f);
  2412. inst->noteOn (60, velocity);
  2413. inst->render (l.data(), r.data(), 10, 1.0f, 1.0f);
  2414. return l[5] / velocity;
  2415. };
  2416. auto which = [] (float x) { return (int) std::lround (x / 0.1f / std::cos (0.25f * 3.14159265f) / 1.41421356f); };
  2417. CHECK (which (play (64.0f / 128.0f)) == 2); // candidates B, C: first B
  2418. CHECK (which (play (64.0f / 128.0f)) == 3); // then C
  2419. CHECK (which (play (40.0f / 128.0f)) == 1); // candidates A, B: C is not among them → the first, A
  2420. CHECK (which (play (40.0f / 128.0f)) == 2); // then B
  2421. CHECK (which (play (64.0f / 128.0f)) == 3); // B was last → C
  2422. // Slicex state: version, 8 master values (volume 16000), options and UI state, deck A referencing a file
  2423. std::vector<std::uint8_t> state (4 + 32 + 47 + 12, 0);
  2424. const std::int32_t version = 0x00060003, volume = 16000;
  2425. std::memcpy (state.data(), &version, 4);
  2426. std::memcpy (state.data() + 4 + 8, &volume, 4);
  2427. state.push_back (1);
  2428. state.push_back (5);
  2429. for (char ch : std::string ("x.wav"))
  2430. state.push_back ((std::uint8_t) ch);
  2431. const auto sliced = lynx::slicex (state);
  2432. CHECK (sliced.has_value() && sliced->path == "x.wav");
  2433. CHECK (sliced && std::abs (sliced->gain - (float) ((std::pow (11.0, 1.25) - 1.0) / 10.0)) < 1e-5f);
  2434. }
  2435. TEST (fl_keys_state_wti_and_loader)
  2436. {
  2437. using namespace lynx::flkeys;
  2438. // state: u32 1, the wavetable name, 15 int32 knobs
  2439. State st;
  2440. st.wavetable = "Grand Piano";
  2441. for (int i = 0; i < 15; ++i)
  2442. st.knobs[(std::size_t) i] = 10 * i - 50;
  2443. const auto bytes = writeState (st);
  2444. CHECK (bytes.size() == 4 + 1 + 11 + 60);
  2445. const auto back = parseState (bytes);
  2446. CHECK (back && back->wavetable == "Grand Piano" && back->knobs == st.knobs);
  2447. // one WAV per keygroup: the file's own loop gives the default end and loop; a keygroup without a file reuses
  2448. // the previous one; a repeated file name copies the earlier keygroup; each file is followed by a guard frame
  2449. // that repeats the frame `loop` before the end (REA_rodada69 §8.2)
  2450. std::vector<std::string> names;
  2451. auto riff = parseWti ("[info]\nsamples=3\nfiletype=riff\npath=W\nvelocityfactor=2\nsamplerate=22050\n"
  2452. "[sample0]\nname=a.wav\nroot=40\nhigh=50\n[sample1]\nroot=51\nhigh=999\npos=2\nend=5\nloop=3\n"
  2453. "[sample2]\nname=A.WAV\nroot=60\nhigh=70\n", names);
  2454. CHECK (riff && names.size() == 1 && names[0] == "W\\a.wav");
  2455. CHECK (riff && riff->keygroups.size() == 3 && riff->keygroups[2].duplicateOf == 0 && riff->thresholds == std::vector<int> { 0 });
  2456. if (riff)
  2457. {
  2458. riff->files[0].loopStart = 4, riff->files[0].loopEnd = 8;
  2459. FileSamples a;
  2460. for (int i = 0; i < 10; ++i)
  2461. a.samples.push_back ((std::int16_t) (100 * (i + 1)));
  2462. const auto decoded = FlKeysTable::decode (riff->encode());
  2463. CHECK (decoded.has_value());
  2464. const auto w = buildWavetable (*decoded, { a });
  2465. CHECK (w != nullptr);
  2466. if (w != nullptr)
  2467. {
  2468. auto same = [] (const Keygroup& k, Keygroup e) { return k.root == e.root && k.high == e.high && k.pos == e.pos && k.end == e.end && k.loop == e.loop; };
  2469. CHECK (same (w->keygroups[0], { 40, 50, 0, 8, 5 }));
  2470. CHECK (same (w->keygroups[1], { 51, 999, 2, 5, 3 }));
  2471. CHECK (same (w->keygroups[2], { 60, 70, 0, 8, 5 }));
  2472. CHECK (w->data[9] == 400 && w->data[10] == 0 && w->data[8] == 900);
  2473. CHECK (w->sampleRate == 22050.0f && w->velocityFactor == 2.0f && ! w->stereo);
  2474. }
  2475. }
  2476. // one raw file: every keygroup's part is copied one after another with a guard sample; "canloop=false" makes the
  2477. // loop as long as the keygroup's end, so the voice stops there
  2478. auto raw = parseWti ("[info]\nsamples=2\nsamplefile=x.dat\nfiletype=raw\n[sample0]\nroot=36\nhigh=47\npos=0\nend=3\nloop=2\n"
  2479. "[sample1]\nroot=48\nhigh=999\npos=4\nend=7\nloop=4\ncanloop=false\n", names);
  2480. CHECK (raw && raw->raw && names.size() == 1 && names[0] == "x.dat");
  2481. if (raw)
  2482. {
  2483. FileSamples x;
  2484. for (int i = 0; i < 10; ++i)
  2485. x.samples.push_back ((std::int16_t) (10 * (i + 1)));
  2486. const auto w = buildWavetable (*raw, { x });
  2487. CHECK (w != nullptr);
  2488. if (w != nullptr)
  2489. {
  2490. CHECK (w->keygroups[0].pos == 0 && w->keygroups[0].end == 3 && w->keygroups[0].loop == 2);
  2491. CHECK (w->keygroups[1].pos == 5 && w->keygroups[1].end == 8 && w->keygroups[1].loop == 8);
  2492. const std::vector<std::int16_t> expected { 10, 20, 30, 40, 30, 50, 60, 70, 80, 50, 0, 0 };
  2493. CHECK (std::equal (expected.begin(), expected.end(), w->data.begin()));
  2494. // a held note on the non-looping keygroup ends by itself; 33 notes leave 32 sounding (FL's MaxPoly)
  2495. State s2;
  2496. s2.knobs = { -1000, 0, 0, -1000, 500, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  2497. RefNote n;
  2498. n.key = 60;
  2499. const auto out = renderReference (*w, s2, { n }, 44100.0, 4096, 512, true);
  2500. double early = 0, late = 0;
  2501. for (int i = 0; i < 64; ++i)
  2502. early += std::fabs (out[(std::size_t) (2 * i)]);
  2503. for (int i = 2048; i < 4096; ++i)
  2504. late += std::fabs (out[(std::size_t) (2 * i)]);
  2505. CHECK (early > 0.0 && late == 0.0);
  2506. }
  2507. }
  2508. // instrument front-end without a wavetable: silent and idle
  2509. auto inst = makeFlKeysInstrument (nullptr);
  2510. inst->prepare (44100.0);
  2511. inst->noteOn (60, 0.78f);
  2512. std::vector<float> l (256), r (256);
  2513. inst->render (l.data(), r.data(), 256, 1.0f, 1.0f);
  2514. CHECK (inst->idle() && l[100] == 0.0f);
  2515. }
  2516. TEST (ogg_vorbis_decodes_like_fl)
  2517. {
  2518. using namespace lynx::oggvorbis;
  2519. const std::uint8_t wavHeader[] = { 'R', 'I', 'F', 'F', 30, 0, 0, 0, 'W', 'A', 'V', 'E', 'f', 'm', 't', ' ', 2, 0, 0, 0, 0x4F, 0x67 };
  2520. CHECK (isOggWav (wavHeader, sizeof wavHeader));
  2521. CHECK (! isOggStream (wavHeader, sizeof wavHeader));
  2522. Decoded none;
  2523. CHECK (! decode (wavHeader, sizeof wavHeader, none) && none.channels == 0);
  2524. // FNV-1a 64 of the planar float32 samples FL's oggio_x64.dll decodes from installation files (REA_rodada77 §3):
  2525. // a plain file, one with a second (PCM) stream multiplexed in, one where an empty stream's end-of-stream page
  2526. // ends the decode early (FL keeps 17 728 of 20 691 frames), and an FL Keys wavetable
  2527. if (realFlpDir.empty())
  2528. return;
  2529. const auto root = std::filesystem::path (realFlpDir).parent_path();
  2530. const struct { const char* file; std::uint64_t hash; std::size_t frames; int channels; } cases[] = {
  2531. { "Data/Patches/Packs/Drums/Percussion/FPC Wood Hit 2.wav", 0xe6137a6141cd5946ull, 29664, 1 },
  2532. { "Data/Patches/Packs/Instruments/Keyboard/Grand/FL Piano (11).wav", 0x3f60df388ac0c636ull, 264600, 2 },
  2533. { "Data/Patches/Packs/Drums/Percussion/Grv Perc 08.wav", 0x7f235f467e90efe0ull, 17728, 0 },
  2534. { "Plugins/Fruity/Generators/FL Keys/Wavtables/Grand Piano/CHANGPIANOHARD_A8.wav", 0x00bbf4cc42f5f72cull, 153280, 0 },
  2535. };
  2536. for (const auto& c : cases)
  2537. {
  2538. const auto path = root / c.file;
  2539. if (! std::filesystem::exists (path))
  2540. continue;
  2541. std::ifstream in (path, std::ios::binary);
  2542. const std::vector<std::uint8_t> bytes { std::istreambuf_iterator<char> (in), {} };
  2543. CHECK (isOggWav (bytes.data(), bytes.size()));
  2544. Decoded d;
  2545. CHECK (decode (bytes.data(), bytes.size(), d));
  2546. if (d.channels == 0)
  2547. continue;
  2548. std::uint64_t h = 1469598103934665603ULL;
  2549. for (const auto& ch : d.data)
  2550. {
  2551. const auto* p = reinterpret_cast<const std::uint8_t*> (ch.data());
  2552. for (std::size_t i = 0; i < ch.size() * sizeof (float); ++i)
  2553. h = (h ^ p[i]) * 1099511628211ULL;
  2554. }
  2555. CHECK (d.data[0].size() == c.frames && d.sampleRate == 44100);
  2556. CHECK (c.channels == 0 || d.channels == c.channels);
  2557. CHECK (h == c.hash);
  2558. }
  2559. }
  2560. TEST (directwave_crt_math_and_state)
  2561. {
  2562. using namespace lynx::dw;
  2563. auto f = [] (std::uint32_t b) { float x; std::memcpy (&x, &b, 4); return x; };
  2564. auto u = [] (float x) { std::uint32_t b; std::memcpy (&b, &x, 4); return b; };
  2565. // the plugin's own run-time (r72/run/mt.exe on DirectWave_x64.dll): values where the libm result differs
  2566. CHECK (u (sinfCrt (f (0x40b1a97fu))) == 0xbf2af4bbu);
  2567. CHECK (u (sinfCrt (f (0xc0ad8469u))) == 0x3f4222f6u);
  2568. CHECK (u (sinfCrt (f (0xc016e8c7u))) == 0xbf34b32du);
  2569. CHECK (u (cosfCrt (f (0xc0af1cc0u))) == 0x3f305702u);
  2570. CHECK (u (cosfCrt (f (0x3f15cba3u))) == 0x3f556924u);
  2571. CHECK (u (cosfCrt (f (0xc0aeb047u))) == 0x3f2dddfeu);
  2572. CHECK (u (expfCrt (f (0xbfb679c0u))) == 0x3e7622a1u);
  2573. CHECK (encodeFormats ({ true, false }) == std::vector<float> ({ 1.0f, 2.0f, 1.0f, 0.0f }));
  2574. CHECK (decodeFormats ({ 1.0f, 2.0f, 1.0f, 0.0f }) == std::vector<bool> ({ true, false }));
  2575. CHECK (! parseState ({ 1, 2, 3 }).has_value());
  2576. }
  2577. TEST (directwave_matches_fl_with_demo_programs)
  2578. {
  2579. // Demo programs (voices, mod matrix, filters, chorus, reverb, shaper) with our own 16-bit samples in every zone:
  2580. // FNV-1a 64 of DirectWave_x64.dll's output (r72/py/synth.py + cmp.py: 44.1 kHz, host blocks of 512, tempo 120)
  2581. if (realFlpDir.empty())
  2582. return;
  2583. using namespace lynx::dw;
  2584. auto readAll = [] (const std::filesystem::path& p) {
  2585. std::ifstream in (p, std::ios::binary);
  2586. return std::vector<std::uint8_t> { std::istreambuf_iterator<char> (in), {} };
  2587. };
  2588. auto stateOf = [&] (const std::string& file, const std::string& channel) {
  2589. const auto path = std::filesystem::path (realFlpDir) / file;
  2590. if (! std::filesystem::exists (path))
  2591. return std::vector<std::uint8_t>();
  2592. const auto p = flp::parseProject (readAll (path));
  2593. for (const auto& c : p.channels)
  2594. if (c.pluginName == "DirectWave" && c.name == channel)
  2595. return c.pluginState;
  2596. return std::vector<std::uint8_t>();
  2597. };
  2598. auto sampleValue = [] (int z, std::int64_t j) {
  2599. const auto v = (j * (13 + z * 7)) % 4096 - 2048;
  2600. const auto w = ((j * j + z * 31) % 997) - 498;
  2601. return (std::int16_t) std::clamp<std::int64_t> (v * 6 + w * 8, -32768, 32767);
  2602. };
  2603. std::vector<RefNote> notes;
  2604. auto note = [&] (int start, int key, double vel, double fv, double fp, double cents) {
  2605. RefNote n; n.start = start; n.key = key; n.velocity = vel; n.finalVol = fv; n.finalPan = fp; n.cents = cents;
  2606. notes.push_back (n);
  2607. };
  2608. note (0, 48, 0.8, 0.7, -0.3, 0); note (11025, 60, 0.5, 0.9, 0.4, 25); note (22050, 67, 1.0, 0.5, 0.0, 0);
  2609. note (44100, 36, 0.3, 0.8, 0.8, -40); note (80000, 72, 0.9, 0.6, -0.6, 0);
  2610. notes[0].release = 40000; notes[1].release = 70000; notes[2].release = 60000;
  2611. auto run = [&] (const std::string& file, const std::string& channel, std::uint64_t expected) {
  2612. const auto st = parseState (stateOf (file, channel));
  2613. if (! st)
  2614. return;
  2615. const auto* prog = playedProgram (*st);
  2616. CHECK (prog != nullptr);
  2617. if (prog == nullptr)
  2618. return;
  2619. std::vector<std::shared_ptr<const ZoneSamples>> zs;
  2620. for (int z = 0; z < (int) prog->zones.size(); ++z)
  2621. {
  2622. auto s = std::make_shared<ZoneSamples>();
  2623. s->bits = 16;
  2624. s->channels = 1;
  2625. s->frames = prog->zones[(std::size_t) z].frames;
  2626. std::vector<std::int16_t> v ((std::size_t) s->frames + 512, 0);
  2627. for (std::int64_t j = 0; j < s->frames; ++j)
  2628. v[(std::size_t) j + 256] = sampleValue (z, j);
  2629. s->i16.push_back (std::move (v));
  2630. zs.push_back (s);
  2631. }
  2632. EngineOptions o;
  2633. o.velocityAsInitVol = (st->flags & 2) != 0;
  2634. o.noGainClamp = (st->flags & 1) != 0;
  2635. const auto out = renderReference (*prog, zs, o, notes, 44100.0, 120000, 512, 1);
  2636. std::uint64_t h = 14695981039346656037ull;
  2637. for (const float x : out)
  2638. {
  2639. std::uint32_t b;
  2640. std::memcpy (&b, &x, 4);
  2641. for (int k = 0; k < 4; ++k)
  2642. h = (h ^ ((b >> (8 * k)) & 0xffu)) * 1099511628211ull;
  2643. }
  2644. CHECK (h == expected);
  2645. if (h != expected)
  2646. std::printf (" DirectWave %s: %016llx\n", channel.c_str(), (unsigned long long) h);
  2647. };
  2648. run ("Demo projects/Visual/ZGameEditor Visualizer/Treben - Umziky - All the same.flp", "Gtr", 0xa1de3dd6be53d665ull);
  2649. run ("Demo projects/Visual/ZGameEditor Visualizer/Treben - Umziky - All the same.flp", "Rhodes", 0xcd458ec6a857f9c7ull);
  2650. run ("System/Render tests/Directwave.flp", "DW", 0x95b4c2e29b664dd1ull);
  2651. }
  2652. TEST (fl_keys_matches_fl_with_the_installation)
  2653. {
  2654. // FL Keys' own output (r69/keys/kcmp3.py: the installation's DLL, FL's x87 host pan law, blocks of 512 at
  2655. // 44.1 kHz): FNV-1a 64 of the interleaved float bits of 24 000 frames
  2656. if (realFlpDir.empty())
  2657. return;
  2658. using namespace lynx::flkeys;
  2659. const auto folder = std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "FL Keys" / "Wavtables";
  2660. if (! std::filesystem::exists (folder / "mda Piano.wti"))
  2661. return;
  2662. auto readAll = [] (const std::filesystem::path& p) {
  2663. std::ifstream f (p, std::ios::binary);
  2664. return std::vector<std::uint8_t> { std::istreambuf_iterator<char> (f), {} };
  2665. };
  2666. auto pcm = [] (const std::vector<std::uint8_t>& w) {
  2667. FileSamples f;
  2668. std::uint16_t channels = 1;
  2669. for (std::size_t pos = 12; pos + 8 <= w.size();)
  2670. {
  2671. std::uint32_t size = 0;
  2672. std::memcpy (&size, w.data() + pos + 4, 4);
  2673. if (std::memcmp (w.data() + pos, "fmt ", 4) == 0)
  2674. std::memcpy (&channels, w.data() + pos + 10, 2);
  2675. else if (std::memcmp (w.data() + pos, "data", 4) == 0)
  2676. {
  2677. f.samples.resize (std::min<std::size_t> (size, w.size() - pos - 8) / 2);
  2678. std::memcpy (f.samples.data(), w.data() + pos + 8, f.samples.size() * 2);
  2679. f.channels = channels;
  2680. }
  2681. pos += 8 + size + (size & 1);
  2682. }
  2683. return f;
  2684. };
  2685. auto run = [&] (const std::string& name, std::array<int, 15> knobs, std::uint64_t expected) {
  2686. const auto wti = readAll (folder / (name + ".wti"));
  2687. std::vector<std::string> names;
  2688. auto table = parseWti (std::string (wti.begin(), wti.end()), names);
  2689. CHECK (table.has_value());
  2690. if (! table)
  2691. return;
  2692. std::vector<FileSamples> files;
  2693. for (std::size_t i = 0; i < names.size(); ++i)
  2694. {
  2695. auto rel = names[i];
  2696. std::replace (rel.begin(), rel.end(), '\\', '/');
  2697. const auto bytes = readAll (folder / rel);
  2698. if (table->raw)
  2699. {
  2700. FileSamples f;
  2701. f.samples.resize (bytes.size() / 2);
  2702. std::memcpy (f.samples.data(), bytes.data(), f.samples.size() * 2);
  2703. files.push_back (std::move (f));
  2704. }
  2705. else
  2706. {
  2707. table->files[i] = wavFileInfo (bytes);
  2708. files.push_back (pcm (bytes));
  2709. }
  2710. }
  2711. const auto w = buildWavetable (*table, files);
  2712. CHECK (w != nullptr);
  2713. if (w == nullptr)
  2714. return;
  2715. State s;
  2716. s.wavetable = name;
  2717. s.knobs = knobs;
  2718. auto note = [] (int start, int key, double iv, double fv, double fp, double cents) {
  2719. RefNote n;
  2720. n.start = start, n.key = key, n.initVol = iv, n.finalVol = fv, n.finalPan = fp, n.cents = cents;
  2721. return n;
  2722. };
  2723. std::vector<RefNote> notes { note (0, 60, 1, 0.78, 0, 0), note (700, 72, 0.6, 0.5, -0.3, 0), note (1500, 30, 0.2, 0.9, 0.5, 25),
  2724. note (2100, 100, 1.3, 0.7, 1, -40), note (2500, 48, 0.05, 0.8, -1, 0), note (16000, 96, 0.9, 0.8, 0, 0) };
  2725. // (floats as the harness parses them)
  2726. for (auto& n : notes)
  2727. n.initVol = (float) n.initVol, n.finalVol = (float) n.finalVol, n.finalPan = (float) n.finalPan;
  2728. notes[0].release = 9000, notes[2].release = 12000, notes[3].release = 15000, notes[5].release = 20000;
  2729. const auto out = renderReference (*w, s, notes, 44100.0, 24000, 512, true);
  2730. std::uint64_t h = 0xcbf29ce484222325ull;
  2731. for (auto v : out)
  2732. {
  2733. std::uint32_t b;
  2734. std::memcpy (&b, &v, 4);
  2735. for (int k = 0; k < 4; ++k)
  2736. h = (h ^ ((b >> (8 * k)) & 0xff)) * 0x100000001b3ull;
  2737. }
  2738. CHECK (h == expected);
  2739. };
  2740. run ("mda Piano", { 1000, 138, 388, -1000, 716, 0, -1000, 256, 0, 0, 0, 0, 0, 0, 0 }, 0x78268ec007f31309ull);
  2741. run ("Rhodes", { 200, 300, 500, 800, 300, 1500, 300, 1500, 300, 400, 400, 800, 900, 30000, 700 }, 0xcc91c93d917f1d31ull);
  2742. run ("Roto Organ", { 1000, 1000, 1000, 1000, 0, 0, -1000, 0, 1000, 0, 500, 1000, 1000, 0, 300 }, 0x99eb13e87640a1c6ull);
  2743. }
  2744. TEST (sampler_plays_pitched_one_shot)
  2745. {
  2746. // 1000-frame ramp sample: the position can be measured from the amplitude
  2747. auto data = std::make_shared<lynx::SampleData>();
  2748. data->sampleRate = 48000.0;
  2749. for (int i = 0; i < 1000; ++i)
  2750. data->left.push_back (1.0f);
  2751. auto sampler = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  2752. sampler->prepare (48000.0);
  2753. std::vector<float> l (2048, 0.0f), r (2048, 0.0f);
  2754. sampler->noteOn (60, 1.0f);
  2755. sampler->noteOff (60); // one-shot: noteOff doesn't cut
  2756. sampler->render (l.data(), r.data(), 2048, 1.0f, 1.0f);
  2757. int sounding = 0;
  2758. for (auto s : l)
  2759. sounding += s > 0.5f;
  2760. CHECK (sounding >= 995 && sounding <= 1000); // plays the whole sample at the original pitch
  2761. std::fill (l.begin(), l.end(), 0.0f);
  2762. sampler->noteOn (72, 1.0f); // one octave up: half the duration
  2763. sampler->render (l.data(), r.data(), 2048, 1.0f, 1.0f);
  2764. sounding = 0;
  2765. for (auto s : l)
  2766. sounding += s > 0.5f;
  2767. CHECK (sounding >= 495 && sounding <= 500);
  2768. CHECK (r[10] > 0.5f); // mono goes to both sides
  2769. auto silent = lynx::makeInstrument (lynx::InstrumentKind::sampler, nullptr);
  2770. silent->prepare (48000.0);
  2771. silent->noteOn (60, 1.0f);
  2772. std::fill (l.begin(), l.end(), 0.0f);
  2773. silent->render (l.data(), r.data(), 64, 1.0f, 1.0f);
  2774. CHECK (l[0] == 0.0f);
  2775. }
  2776. TEST (rack_live_notes_and_sample_provider)
  2777. {
  2778. lynx::Song song;
  2779. lynx::ChannelDef c;
  2780. c.name = "Smp";
  2781. c.instrument = lynx::InstrumentKind::sampler;
  2782. c.samplePath = "a.wav";
  2783. song.channels.push_back (c);
  2784. song.patterns.emplace_back();
  2785. int loads = 0;
  2786. auto provider = [&] (const std::string& path) -> std::shared_ptr<const lynx::SampleData> {
  2787. ++loads;
  2788. CHECK (path == "a.wav");
  2789. auto d = std::make_shared<lynx::SampleData>();
  2790. d->left.assign (4800, 0.5f);
  2791. return d;
  2792. };
  2793. auto state = lynx::RackState::build (song, nullptr, 48000.0, provider);
  2794. CHECK (loads == 1);
  2795. lynx::RackRenderer renderer;
  2796. renderer.prepare (48000.0);
  2797. lynx::LiveEventQueue live;
  2798. lynx::NoteEvent on;
  2799. on.channel = 0;
  2800. on.velocity = 1.0f;
  2801. live.push (on);
  2802. std::vector<float> l (256), r (256);
  2803. renderer.process (state.get(), false, 0.0, 256, l.data(), r.data(), &live); // stopped, but the live note plays
  2804. CHECK (l[0] > 0.1f);
  2805. // same sample: reused; different sample: reloaded
  2806. auto same = lynx::RackState::build (song, state.get(), 48000.0, provider);
  2807. CHECK (loads == 1 && same->instruments[0] == state->instruments[0]);
  2808. song.channels[0].samplePath = "b.wav";
  2809. auto other = lynx::RackState::build (song, state.get(), 48000.0, [&] (const std::string&) { ++loads; return nullptr; });
  2810. CHECK (loads == 2 && other->instruments[0] != state->instruments[0]);
  2811. }
  2812. TEST (flp_paths_resolve_and_samplers_import)
  2813. {
  2814. lynx::FlPathRoots roots { "C:\\FL", "C:\\FL\\Data", "D:\\User" };
  2815. CHECK (lynx::resolveFlPath ("%FLStudioFactoryData%\\Data\\x.wav", roots) == "C:\\FL\\Data\\x.wav");
  2816. CHECK (lynx::resolveFlPath ("%FLStudioData%\\Patches\\y.wav", roots) == "C:\\FL\\Data\\Patches\\y.wav");
  2817. CHECK (lynx::resolveFlPath ("%FLStudioUserData%\\z.wav", roots) == "D:\\User\\z.wav");
  2818. CHECK (lynx::resolveFlPath ("E:\\abs.wav", roots) == "E:\\abs.wav");
  2819. CHECK (lynx::resolveFlPath ("%FLStudioUserData%\\z.wav", {}) == "");
  2820. flp::Project p;
  2821. p.channels = { { 0, flp::ChannelType::sampler, "Kick", "", "%FLStudioData%\\k.wav", 1 },
  2822. { 1, flp::ChannelType::sampler, "Hat", "", "%FLStudioData%\\missing.wav", 1 } };
  2823. lynx::FlpImportOptions options;
  2824. options.roots = roots;
  2825. options.fileExists = [] (const std::string& path) { return path == "C:\\FL\\Data\\k.wav"; };
  2826. const auto s = lynx::importFlp (p, options);
  2827. CHECK (s.channels[0].instrument == lynx::InstrumentKind::sampler);
  2828. CHECK (s.channels[0].samplePath == "C:\\FL\\Data\\k.wav");
  2829. CHECK (s.channels[1].instrument == lynx::InstrumentKind::hat); // no file: built-in instrument
  2830. }
  2831. TEST (soft_clip_is_transparent_below_knee_and_bounded)
  2832. {
  2833. float s[] = { 0.5f, -0.89f, 1.5f, -3.0f, 0.95f };
  2834. lynx::softClip (s, 5);
  2835. CHECK (s[0] == 0.5f && s[1] == -0.89f);
  2836. CHECK (s[2] > 0.9f && s[2] <= 1.0f);
  2837. CHECK (s[3] < -0.9f && s[3] >= -1.0f);
  2838. CHECK (s[4] > 0.9f && s[4] < 0.95f);
  2839. }
  2840. TEST (song_scheduler_plays_clips_with_offset_and_cut)
  2841. {
  2842. lynx::Song s;
  2843. s.channels.resize (1);
  2844. auto& p = s.patterns.emplace_back();
  2845. for (int step : { 0, 4, 8, 12 }) // one note per beat
  2846. p.toggleStep (0, step);
  2847. // clip at bar 2 playing only the second half of the pattern (beats 3 and 4)
  2848. lynx::Clip c;
  2849. c.pattern = 0;
  2850. c.start = 4 * lynx::ppq;
  2851. c.length = 2 * lynx::ppq;
  2852. c.offset = 2 * lynx::ppq;
  2853. s.playlist.push_back (c);
  2854. CHECK (s.songLength() == 8 * lynx::ppq);
  2855. const double sr = 48000.0;
  2856. const auto tps = lynx::ticksPerSample (120.0, sr); // 1 beat = 24000 samples
  2857. lynx::EventBuffer ev;
  2858. // whole song in a single block (8 beats = 192000 samples)
  2859. lynx::scheduleSong (s, 0.0, 192000, tps, ev);
  2860. std::vector<int> ons;
  2861. for (auto& e : ev)
  2862. if (e.type == lynx::NoteEvent::Type::on)
  2863. ons.push_back (e.sampleOffset);
  2864. CHECK (ons.size() == 2);
  2865. CHECK (ons.size() == 2 && ons[0] == 4 * 24000 && ons[1] == 5 * 24000);
  2866. // crossing the loop: bar 1 is empty, so nothing at the start of the second pass
  2867. ev.clear();
  2868. lynx::scheduleSong (s, 8.0 * lynx::ppq - 100 * tps, 200, tps, ev);
  2869. CHECK (ev.size() == 0);
  2870. // a note longer than the clip is cut at the clip end
  2871. p.notes[3].length = 4 * lynx::ppq;
  2872. ev.clear();
  2873. lynx::scheduleSong (s, 0.0, 192000, tps, ev);
  2874. int lastOff = -1;
  2875. for (auto& e : ev)
  2876. if (e.type == lynx::NoteEvent::Type::off)
  2877. lastOff = std::max (lastOff, e.sampleOffset);
  2878. CHECK (lastOff == 6 * 24000);
  2879. }
  2880. TEST (remove_pattern_fixes_clips)
  2881. {
  2882. lynx::Song s;
  2883. s.patterns.resize (3);
  2884. s.currentPattern = 2;
  2885. s.playlist = { { 0, 0, 0, 100, 0 }, { 1, 0, 100, 100, 0 }, { 2, 1, 0, 100, 0 } };
  2886. s.removePattern (1);
  2887. CHECK (s.patterns.size() == 2);
  2888. CHECK (s.playlist.size() == 2 && s.playlist[1].pattern == 1);
  2889. CHECK (s.currentPattern == 1);
  2890. }
  2891. TEST (flp_import_3x_osc_state_and_channel_filter)
  2892. {
  2893. // new 3x Osc patch (version 14): osc 2 mix 64 coarse -12, osc 3 mix 32 coarse -24
  2894. std::vector<std::uint8_t> blob (460, 0);
  2895. auto put = [&] (std::size_t off, std::int32_t v) {
  2896. for (int b = 0; b < 4; ++b)
  2897. blob[off + (std::size_t) b] = (std::uint8_t) ((std::uint32_t) v >> (8 * b));
  2898. };
  2899. put (0x00, 14);
  2900. put (0x08, 3); // osc 1 saw
  2901. put (0x10, 7); // osc 1 fine +7
  2902. put (0x04, -32); // osc 1 pan -0.5
  2903. put (0x1C, 64); // osc 2 mix
  2904. put (0x28, -12); // osc 2 coarse
  2905. put (0x38, 32); // osc 3 mix
  2906. put (0x40, 2); // osc 3 square
  2907. put (0x44, -19); // osc 3 coarse: -1 octave and -7 semitones
  2908. lynx::SynthSettings s;
  2909. CHECK (lynx::synthFrom3xOsc (blob, s));
  2910. 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);
  2911. CHECK (s.osc[0].wave == lynx::Waveform::saw && s.osc[0].detune == 7.0f && s.osc[0].pan == -0.5f);
  2912. CHECK (s.osc[1].wave == lynx::Waveform::sine && s.osc[1].octave == -1 && s.osc[1].semitones == 0);
  2913. CHECK (s.osc[2].wave == lynx::Waveform::square && s.osc[2].octave == -1 && s.osc[2].semitones == -7);
  2914. CHECK (! lynx::synthFrom3xOsc (std::vector<std::uint8_t> (40, 0), s));
  2915. // channel filter: open by default; LP with Mod X 128 ~ 2.71 kHz; Fast LP with Mod X 128 ~ 280 Hz
  2916. float hz = 0.0f, res = 1.0f;
  2917. int mode = -1;
  2918. lynx::synthFilterFromChannel (256, 0, 0, hz, res, mode);
  2919. CHECK (hz >= 19999.0f && res == 0.0f);
  2920. lynx::synthFilterFromChannel (128, 0, 1, hz, res, mode);
  2921. CHECK (std::abs (hz - 2710.0f) < 30.0f && res == 0.0f);
  2922. lynx::synthFilterFromChannel (128, 0, 0, hz, res, mode);
  2923. CHECK (hz > 240.0f && hz < 320.0f);
  2924. lynx::synthFilterFromChannel (128, 256, 1, hz, res, mode);
  2925. CHECK (res > 0.8f);
  2926. CHECK (mode == 0);
  2927. lynx::synthFilterFromChannel (64, 0, 3, hz, res, mode); // high-pass at ~840 Hz
  2928. CHECK (mode == 2 && std::abs (hz - 840.0f) < 40.0f);
  2929. lynx::synthFilterFromChannel (128, 0, 2, hz, res, mode); // band-pass
  2930. CHECK (mode == 1);
  2931. }
  2932. TEST (synth_pitch_and_filter_envelopes)
  2933. {
  2934. auto render = [] (const lynx::SynthSettings& s, int frames) {
  2935. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  2936. inst->prepare (48000.0);
  2937. lynx::ChannelDef c;
  2938. c.synth = s;
  2939. inst->configure (c, 120.0);
  2940. inst->noteOn (45, 1.0f); // A2 = 110 Hz
  2941. std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
  2942. inst->render (l.data(), r.data(), frames, 1.0f, 1.0f);
  2943. return l;
  2944. };
  2945. auto crossings = [] (const std::vector<float>& b, int from, int to) {
  2946. int n = 0;
  2947. for (int i = from + 1; i < to; ++i)
  2948. n += (b[(std::size_t) i - 1] < 0.0f) != (b[(std::size_t) i] < 0.0f);
  2949. return n;
  2950. };
  2951. // 3x Osc "kick": sine with a +24 st pitch envelope falling fast
  2952. lynx::SynthSettings kick;
  2953. kick.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  2954. kick.cutoff = 20000.0f;
  2955. kick.pitchEnv = { 24.0f, 0.0f, 0.0f, 0.05f, 0.0f, 0.05f };
  2956. const auto k = render (kick, 48000);
  2957. const auto early = crossings (k, 0, 2400); // first 50 ms: well above 110 Hz
  2958. const auto late = crossings (k, 24000, 48000); // then back to 110 Hz (~110 crossings in 0.5 s)
  2959. CHECK (early > 20); // +24 st ramp over 50 ms: ~2.2x the average frequency (11 crossings without envelope)
  2960. CHECK (std::abs (late - 110) < 4);
  2961. // cutoff envelope: saw closed at 200 Hz opens +5 octaves on attack and closes again
  2962. lynx::SynthSettings pluck;
  2963. pluck.cutoff = 200.0f;
  2964. pluck.filterEnv = { 5.0f, 0.0f, 0.0f, 0.1f, 0.0f, 0.1f };
  2965. const auto p = render (pluck, 48000);
  2966. auto hf = [&] (int from, int to) { // energy of the difference (highs)
  2967. double e = 0.0;
  2968. for (int i = from + 1; i < to; ++i)
  2969. e += std::pow ((double) p[(std::size_t) i] - p[(std::size_t) i - 1], 2.0);
  2970. return e;
  2971. };
  2972. CHECK (hf (0, 2400) > 4.0 * hf (40000, 42400));
  2973. // Toxic Biohazard's envelope (REA_rodada33 §2): exponential towards 5 clipped at 1, so the cutoff is still near
  2974. // its base early in the attack, fully open at A = 0.2 s and back down after the exponential decay
  2975. lynx::SynthSettings toxic = pluck;
  2976. toxic.filterEnv = { 5.0f, 0.2f, 0.0f, 0.1f, 0.0f, 0.1f, 1 };
  2977. const auto t = render (toxic, 48000);
  2978. auto thf = [&] (int from, int to) {
  2979. double e = 0.0;
  2980. for (int i = from + 1; i < to; ++i)
  2981. e += std::pow ((double) t[(std::size_t) i] - t[(std::size_t) i - 1], 2.0);
  2982. return e;
  2983. };
  2984. CHECK (thf (9000, 9600) > 4.0 * thf (300, 900));
  2985. CHECK (thf (9000, 9600) > 4.0 * thf (40000, 40600));
  2986. }
  2987. TEST (synth_mono_reuses_the_sounding_voice)
  2988. {
  2989. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  2990. inst->prepare (48000.0);
  2991. lynx::ChannelDef c;
  2992. c.synth.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  2993. c.synth.cutoff = 20000.0f;
  2994. c.synth.mono = true;
  2995. inst->configure (c, 120.0);
  2996. std::vector<float> l (48000), r (48000);
  2997. inst->noteOn (57, 1.0f);
  2998. inst->render (l.data(), r.data(), 4800, 1.0f, 1.0f);
  2999. inst->noteOn (69, 1.0f); // A4 takes over the A3 voice: only one note sounding
  3000. inst->render (l.data() + 4800, r.data() + 4800, 43200, 1.0f, 1.0f);
  3001. int crossings = 0;
  3002. float peak = 0.0f;
  3003. for (std::size_t i = 24001; i < 48000; ++i)
  3004. {
  3005. crossings += (l[i - 1] < 0.0f) != (l[i] < 0.0f);
  3006. peak = std::max (peak, std::abs (l[i]));
  3007. }
  3008. CHECK (std::abs (crossings - 440) < 4); // 0.5 s of 440 Hz, without the A3 underneath
  3009. CHECK (peak < 0.35f); // one voice (0.3 level), not two summed
  3010. // no click on the handover: the jump between neighbouring samples stays sine-sized
  3011. float jump = 0.0f;
  3012. for (std::size_t i = 4790; i < 4810; ++i)
  3013. jump = std::max (jump, std::abs (l[i] - l[i - 1]));
  3014. CHECK (jump < 0.05f);
  3015. }
  3016. TEST (synth_porta_glides_and_am_modulates)
  3017. {
  3018. auto make = [] (const lynx::SynthSettings& s) {
  3019. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  3020. inst->prepare (48000.0);
  3021. lynx::ChannelDef c;
  3022. c.synth = s;
  3023. inst->configure (c, 120.0);
  3024. return inst;
  3025. };
  3026. auto crossings = [] (const std::vector<float>& b, std::size_t from, std::size_t to) {
  3027. int n = 0;
  3028. for (auto i = from + 1; i < to; ++i)
  3029. n += (b[i - 1] < 0.0f) != (b[i] < 0.0f);
  3030. return n;
  3031. };
  3032. // mono + 1-beat porta (0.5 s at 120 BPM): from A3 to A4, halfway ~E4 (329.6 Hz)
  3033. lynx::SynthSettings s;
  3034. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  3035. s.cutoff = 20000.0f;
  3036. s.mono = true;
  3037. s.porta = true;
  3038. s.portaBeats = 1.0f;
  3039. auto inst = make (s);
  3040. std::vector<float> l (96000), r (96000);
  3041. inst->noteOn (57, 1.0f);
  3042. inst->render (l.data(), r.data(), 4800, 1.0f, 1.0f);
  3043. inst->noteOn (69, 1.0f);
  3044. inst->render (l.data() + 4800, r.data() + 4800, 91200, 1.0f, 1.0f);
  3045. const auto middle = crossings (l, 4800 + 12000 - 2400, 4800 + 12000 + 2400); // 0.1 s around 0.25 s
  3046. CHECK (std::abs (middle - 66) < 6); // ~330 Hz * 0.1 s * 2
  3047. CHECK (std::abs (crossings (l, 48000, 96000) - 880) < 4); // reached A4 (1 s = 880 crossings)
  3048. // AM: osc 3 at level 1.0, slow sine, modulates osc 1 (the average level drops, the sum doesn't gain osc 3)
  3049. lynx::SynthSettings am;
  3050. am.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  3051. am.osc[2] = { lynx::Waveform::sine, -3, 0.0f, 1.0f };
  3052. am.cutoff = 20000.0f;
  3053. am.am = true;
  3054. auto modulated = make (am);
  3055. modulated->noteOn (69, 1.0f);
  3056. std::fill (l.begin(), l.end(), 0.0f);
  3057. modulated->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
  3058. float peak = 0.0f;
  3059. for (std::size_t i = 4800; i < 48000; ++i)
  3060. peak = std::max (peak, std::abs (l[i]));
  3061. CHECK (peak > 0.45f && peak < 0.65f); // 0.3 · (1 + 1) at most: (1 − o3) goes up to 2
  3062. }
  3063. TEST (channel_lfo_vibrato_and_tremolo)
  3064. {
  3065. auto render = [] (const lynx::SynthSettings& s) {
  3066. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  3067. inst->prepare (48000.0);
  3068. lynx::ChannelDef c;
  3069. c.synth = s;
  3070. inst->configure (c, 120.0);
  3071. inst->noteOn (69, 1.0f);
  3072. std::vector<float> l (48000), r (48000);
  3073. inst->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
  3074. return l;
  3075. };
  3076. lynx::SynthSettings s;
  3077. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  3078. s.cutoff = 20000.0f;
  3079. // tremolo: 100% volume LFO at 4 Hz (0.25 s period) -> the amplitude envelope goes from ~0 to ~2x
  3080. auto tremolo = s;
  3081. tremolo.lfo[lynx::lfoVolume] = { 1.0f, 0.25f, false, 0, 0.0f, 0.0f };
  3082. const auto t = render (tremolo);
  3083. float lowest = 1.0f, highest = 0.0f;
  3084. for (std::size_t w = 4800; w + 480 <= 48000; w += 480) // 10 ms windows (4.4 cycles of 440 Hz)
  3085. {
  3086. float peak = 0.0f;
  3087. for (auto i = w; i < w + 480; ++i)
  3088. peak = std::max (peak, std::abs (t[i]));
  3089. lowest = std::min (lowest, peak);
  3090. highest = std::max (highest, peak);
  3091. }
  3092. CHECK (lowest < 0.08f && highest > 0.5f);
  3093. // synced vibrato: ±1 semitone, 1-beat period (0.5 s at 120 BPM); the average stays at 440 Hz
  3094. auto vibrato = s;
  3095. vibrato.lfo[lynx::lfoPitch] = { 1.0f, 1.0f, true, 0, 0.0f, 0.0f };
  3096. const auto v = render (vibrato);
  3097. int crossings = 0;
  3098. for (std::size_t i = 1; i < 48000; ++i)
  3099. crossings += (v[i - 1] < 0.0f) != (v[i] < 0.0f);
  3100. CHECK (std::abs (crossings - 880) < 12);
  3101. int firstQuarter = 0; // first quarter of the cycle: rising sine -> higher than 440
  3102. for (std::size_t i = 1; i < 6000; ++i)
  3103. firstQuarter += (v[i - 1] < 0.0f) != (v[i] < 0.0f);
  3104. CHECK (firstQuarter > 112);
  3105. }
  3106. TEST (sampler_porta_glides_from_previous_note)
  3107. {
  3108. auto data = std::make_shared<lynx::SampleData>();
  3109. data->sampleRate = 48000.0;
  3110. data->left.resize (96000);
  3111. for (std::size_t i = 0; i < data->left.size(); ++i)
  3112. data->left[i] = 0.5f * (float) std::sin (6.283185307 * 220.0 * (double) i / 48000.0);
  3113. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  3114. inst->prepare (48000.0);
  3115. lynx::ChannelDef c;
  3116. c.sampler.cut = true;
  3117. c.sampler.porta = true;
  3118. c.sampler.portaBeats = 1.0f; // 0.5 s at 120 BPM
  3119. inst->configure (c, 120.0);
  3120. std::vector<float> l (24000), r (24000);
  3121. inst->noteOn (60, 1.0f);
  3122. inst->render (l.data(), r.data(), 2400, 1.0f, 1.0f);
  3123. inst->noteOn (72, 1.0f); // one octave up: starts at 220 Hz and rises
  3124. std::fill (l.begin(), l.end(), 0.0f);
  3125. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  3126. auto crossings = [&] (std::size_t from, std::size_t to) {
  3127. int n = 0;
  3128. for (auto i = from + 1; i < to; ++i)
  3129. n += (l[i - 1] < 0.0f) != (l[i] < 0.0f);
  3130. return n;
  3131. };
  3132. const auto start = crossings (600, 2400); // 37.5 ms still near 220 Hz: ~17 crossings (440 Hz would give 33)
  3133. const auto end = crossings (21600, 24000); // 50 ms at the end of the glide: near 440 Hz (~44; 220 Hz would give 22)
  3134. CHECK (start < 22);
  3135. CHECK (end > 36);
  3136. }
  3137. TEST (zoned_sampler_picks_zone_by_key_and_velocity)
  3138. {
  3139. auto sine = [] (double hz, float amp) {
  3140. auto d = std::make_shared<lynx::SampleData>();
  3141. d->sampleRate = 48000.0;
  3142. d->left.resize (48000);
  3143. for (std::size_t i = 0; i < d->left.size(); ++i)
  3144. d->left[i] = amp * (float) std::sin (6.283185307 * hz * (double) i / 48000.0);
  3145. return d;
  3146. };
  3147. // lows: 220 Hz sine recorded as A3 (57), up to key 69; highs: 880 Hz as A5 (81)
  3148. // hard layer on the highs: same tuning, louder
  3149. std::vector<lynx::LoadedZone> zones {
  3150. { { "graves.wav", 57, 0, 69, 0, 127, 0.0f, 1.0f }, sine (220.0, 0.5f) },
  3151. { { "agudos.wav", 81, 70, 127, 0, 99, 0.0f, 1.0f }, sine (880.0, 0.25f) },
  3152. { { "agudos forte.wav", 81, 70, 127, 100, 127, 0.0f, 1.0f }, sine (880.0, 0.9f) },
  3153. };
  3154. auto inst = lynx::makeZonedSampler (zones);
  3155. inst->prepare (48000.0);
  3156. inst->configure (lynx::ChannelDef {}, 120.0);
  3157. auto play = [&] (int key, float velocity) {
  3158. inst->allNotesOff();
  3159. std::vector<float> l (24000), r (24000);
  3160. inst->render (l.data(), r.data(), 2000, 1.0f, 1.0f); // let the old voices die out
  3161. std::fill (l.begin(), l.end(), 0.0f);
  3162. inst->noteOn (key, velocity);
  3163. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  3164. int crossings = 0;
  3165. float peak = 0.0f;
  3166. for (std::size_t i = 1; i < 24000; ++i)
  3167. {
  3168. crossings += (l[i - 1] < 0.0f) != (l[i] < 0.0f);
  3169. peak = std::max (peak, std::abs (l[i]));
  3170. }
  3171. return std::pair { crossings, peak };
  3172. };
  3173. const auto low = play (69, 0.5f); // A4 from root A3: 440 Hz -> 440 crossings in 0.5 s
  3174. CHECK (std::abs (low.first - 440) < 4);
  3175. const auto soft = play (81, 0.5f); // high zone at the root: 880 Hz
  3176. CHECK (std::abs (soft.first - 880) < 6);
  3177. const auto hard = play (81, 0.95f); // high velocity: hard layer
  3178. CHECK (hard.second > soft.second * 2.0f);
  3179. // overlapping layers round-robin: two identical zones at different levels, notes alternate
  3180. auto rr = lynx::makeZonedSampler ({ { { "a.wav", 60 }, sine (440.0, 0.2f) }, { { "b.wav", 60 }, sine (440.0, 0.8f) } });
  3181. rr->prepare (48000.0);
  3182. rr->configure (lynx::ChannelDef {}, 120.0);
  3183. float peaks[2] {};
  3184. for (int k = 0; k < 2; ++k)
  3185. {
  3186. rr->allNotesOff();
  3187. std::vector<float> l (4800), r (4800);
  3188. rr->render (l.data(), r.data(), 2000, 1.0f, 1.0f);
  3189. std::fill (l.begin(), l.end(), 0.0f);
  3190. rr->noteOn (60, 1.0f);
  3191. rr->render (l.data(), r.data(), 4800, 1.0f, 1.0f);
  3192. for (auto v : l)
  3193. peaks[k] = std::max (peaks[k], std::abs (v));
  3194. }
  3195. CHECK (std::abs (peaks[0] - peaks[1]) > 0.3f);
  3196. }
  3197. TEST (zoned_sampler_loops_while_held)
  3198. {
  3199. // 0.1 s sample with a loop in the middle: holding the note it keeps sounding well past the end of the file;
  3200. // on release, it leaves the loop and ends
  3201. auto data = std::make_shared<lynx::SampleData>();
  3202. data->sampleRate = 48000.0;
  3203. data->left.resize (4800);
  3204. for (std::size_t i = 0; i < data->left.size(); ++i)
  3205. data->left[i] = 0.5f * (float) std::sin (6.283185307 * 480.0 * (double) i / 48000.0); // 100 frames per cycle
  3206. lynx::SampleZone zone { "loop.wav", 60 };
  3207. zone.loopStart = 1000;
  3208. zone.loopEnd = 3000;
  3209. zone.loopMode = 2;
  3210. auto inst = lynx::makeZonedSampler ({ { zone, data } });
  3211. inst->prepare (48000.0);
  3212. lynx::ChannelDef c;
  3213. c.sampler.release = 0.01f;
  3214. inst->configure (c, 120.0);
  3215. inst->noteOn (60, 1.0f);
  3216. std::vector<float> l (48000), r (48000);
  3217. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f); // 0.5 s held
  3218. float held = 0.0f;
  3219. for (std::size_t i = 20000; i < 24000; ++i)
  3220. held = std::max (held, std::abs (l[i]));
  3221. CHECK (held > 0.3f);
  3222. inst->noteOff (60);
  3223. inst->render (l.data() + 24000, r.data() + 24000, 24000, 1.0f, 1.0f);
  3224. float after = 0.0f;
  3225. for (std::size_t i = 40000; i < 48000; ++i)
  3226. after = std::max (after, std::abs (l[i]));
  3227. CHECK (after == 0.0f);
  3228. }
  3229. TEST (global_lfo_follows_song_position)
  3230. {
  3231. // global volume LFO (1-beat period): a note mid-cycle starts mid-wave; the
  3232. // same note with a per-note LFO starts at phase 0
  3233. auto first = [] (bool global, double songTicks) {
  3234. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  3235. inst->prepare (48000.0);
  3236. lynx::ChannelDef c;
  3237. c.synth.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  3238. c.synth.cutoff = 20000.0f;
  3239. c.synth.lfo[lynx::lfoVolume] = { 1.0f, 1.0f, true, 2, 0.0f, 0.0f, global }; // pulse: +1 in the first half
  3240. inst->configure (c, 120.0);
  3241. inst->setSongPosition (songTicks);
  3242. inst->noteOn (69, 1.0f);
  3243. std::vector<float> l (4800), r (4800);
  3244. inst->render (l.data(), r.data(), 4800, 1.0f, 1.0f);
  3245. float peak = 0.0f;
  3246. for (std::size_t i = 2400; i < 4800; ++i)
  3247. peak = std::max (peak, std::abs (l[i]));
  3248. return peak;
  3249. };
  3250. // mid-cycle (phase 0.6) the pulse is at −1: volume ~0; per note (phase 0) it is at +1: ~2x
  3251. const auto globalLate = first (true, 0.6 * lynx::ppq);
  3252. const auto perNote = first (false, 0.6 * lynx::ppq);
  3253. CHECK (globalLate < 0.05f);
  3254. CHECK (perNote > 0.4f);
  3255. }
  3256. TEST (sampler_channel_filter_darkens_and_bypasses_when_open)
  3257. {
  3258. // white noise in the sampler: with a 300 Hz cutoff the energy drops a lot; open passes unchanged
  3259. auto data = std::make_shared<lynx::SampleData>();
  3260. data->sampleRate = 48000.0;
  3261. data->left.resize (48000);
  3262. std::uint32_t seed = 7;
  3263. for (auto& v : data->left)
  3264. {
  3265. seed = seed * 1664525u + 1013904223u;
  3266. v = ((float) (seed >> 8) / 16777216.0f - 0.5f);
  3267. }
  3268. data->right = data->left;
  3269. auto energy = [&] (float cutoff) {
  3270. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  3271. inst->prepare (48000.0);
  3272. lynx::ChannelDef c;
  3273. c.sampler.cutoff = cutoff;
  3274. inst->configure (c, 120.0);
  3275. inst->noteOn (60, 1.0f);
  3276. std::vector<float> l (24000), r (24000);
  3277. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  3278. double e = 0.0;
  3279. for (auto v : l)
  3280. e += (double) v * v;
  3281. return e;
  3282. };
  3283. const auto open = energy (20000.0f), dark = energy (300.0f);
  3284. {
  3285. // high-pass at 10 kHz: also removes most of the noise energy, but from the other side
  3286. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  3287. inst->prepare (48000.0);
  3288. lynx::ChannelDef hp;
  3289. hp.sampler.cutoff = 10000.0f;
  3290. hp.sampler.filterType = 2;
  3291. inst->configure (hp, 120.0);
  3292. inst->noteOn (60, 1.0f);
  3293. std::vector<float> l (24000), r (24000);
  3294. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  3295. double e = 0.0, lowE = 0.0;
  3296. float lp = 0.0f;
  3297. for (auto v : l)
  3298. {
  3299. e += (double) v * v;
  3300. lp += 0.01f * (v - lp); // crude low-pass: almost nothing should remain below
  3301. lowE += (double) lp * lp;
  3302. }
  3303. CHECK (e > 10.0 && e < 0.7 * energy (20000.0f));
  3304. CHECK (lowE < e * 0.01);
  3305. }
  3306. CHECK (open > 100.0);
  3307. CHECK (dark < open * 0.05);
  3308. // +12 st pitch envelope with sustain 1: the sample runs at double speed
  3309. // and ends in half the time (1 s of sample -> 0.5 s)
  3310. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  3311. inst->prepare (48000.0);
  3312. lynx::ChannelDef c;
  3313. c.sampler.pitchEnv = { 12.0f, 0.0f, 0.0f, 0.1f, 1.0f, 0.1f };
  3314. inst->configure (c, 120.0);
  3315. inst->noteOn (60, 1.0f);
  3316. std::vector<float> l (48000), r (48000);
  3317. inst->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
  3318. float tail = 0.0f, body = 0.0f;
  3319. for (std::size_t i = 20000; i < 23000; ++i)
  3320. body = std::max (body, std::abs (l[i]));
  3321. for (std::size_t i = 25000; i < 48000; ++i)
  3322. tail = std::max (tail, std::abs (l[i]));
  3323. CHECK (body > 0.1f);
  3324. CHECK (tail == 0.0f);
  3325. }
  3326. TEST (synth_oscillator_pan_and_semitones)
  3327. {
  3328. auto render = [] (lynx::SynthSettings s, std::vector<float>& l, std::vector<float>& r) {
  3329. auto synth = lynx::makeInstrument (lynx::InstrumentKind::synth);
  3330. synth->prepare (48000.0);
  3331. lynx::ChannelDef c;
  3332. c.synth = s;
  3333. synth->configure (c, 120.0);
  3334. synth->noteOn (69, 1.0f);
  3335. l.assign (24000, 0.0f);
  3336. r.assign (24000, 0.0f);
  3337. synth->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  3338. };
  3339. lynx::SynthSettings s;
  3340. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f, 7, 1.0f }; // a fifth up, fully right
  3341. s.cutoff = 20000.0f;
  3342. std::vector<float> l, r;
  3343. render (s, l, r);
  3344. float peakL = 0.0f, peakR = 0.0f;
  3345. int crossings = 0;
  3346. for (std::size_t i = 4800; i < 24000; ++i)
  3347. {
  3348. peakL = std::max (peakL, std::abs (l[i]));
  3349. peakR = std::max (peakR, std::abs (r[i]));
  3350. crossings += (r[i - 1] < 0.0f) != (r[i] < 0.0f);
  3351. }
  3352. CHECK (peakL < 1e-4f && peakR > 0.1f);
  3353. // 440 * 2^(7/12) = 659.3 Hz; 0.4 s = 263.7 cycles = ~527 crossings
  3354. CHECK (std::abs (crossings - 527) < 6);
  3355. }
  3356. TEST (fl_wrapper_state_parses_records_and_vst3_state)
  3357. {
  3358. // Fruity Wrapper blob (version 12): VST3 generator type, class ID, name, path and state
  3359. std::vector<std::uint8_t> blob;
  3360. 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))); };
  3361. auto record = [&] (std::vector<std::uint8_t>& b, std::uint32_t id, const std::vector<std::uint8_t>& payload) {
  3362. u32 (b, id);
  3363. u32 (b, (std::uint32_t) payload.size());
  3364. u32 (b, 0);
  3365. b.insert (b.end(), payload.begin(), payload.end());
  3366. };
  3367. auto bytes = [] (const std::string& s) { return std::vector<std::uint8_t> (s.begin(), s.end()); };
  3368. std::vector<std::uint8_t> vst3State;
  3369. u32 (vst3State, 1);
  3370. record (vst3State, 1, std::vector<std::uint8_t> (64, 0));
  3371. record (vst3State, 3, bytes ("COMPONENTE"));
  3372. record (vst3State, 2, bytes ("CONTROLADOR"));
  3373. // record 4: 3 slots → ParamIDs 700, −1 (empty), 0xFFFFFF01
  3374. std::vector<std::uint8_t> slots;
  3375. for (auto v : { 3u, 700u, 0xFFFFFFFFu, 0xFFFFFF01u })
  3376. u32 (slots, v);
  3377. record (vst3State, 4, slots);
  3378. u32 (blob, 12);
  3379. record (blob, 0x01, std::vector<std::uint8_t> (20, 0xFF));
  3380. record (blob, 0x32, { 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });
  3381. std::vector<std::uint8_t> cid;
  3382. for (int i = 0; i < 16; ++i)
  3383. cid.push_back ((std::uint8_t) (0xA0 + i));
  3384. record (blob, 0x34, cid);
  3385. record (blob, 0x36, bytes ("Surge XT"));
  3386. record (blob, 0x37, bytes ("C:/Program Files/Common Files/VST3/Surge XT.vst3"));
  3387. record (blob, 0x35, vst3State);
  3388. lynx::ForeignPlugin p;
  3389. CHECK (lynx::parseFlWrapper (blob, p));
  3390. CHECK (p.format == lynx::ForeignPlugin::Format::vst3);
  3391. CHECK (p.hasClassId && p.classId[0] == 0xA0 && p.classId[15] == 0xAF);
  3392. CHECK (p.name == "Surge XT" && p.path.find ("Surge XT.vst3") != std::string::npos);
  3393. std::vector<std::uint8_t> component, controller;
  3394. CHECK (lynx::splitFlVst3State (p.state, component, controller));
  3395. CHECK (component == bytes ("COMPONENTE") && controller == bytes ("CONTROLADOR"));
  3396. const auto saved = lynx::flVst3ParamSlots (p.state);
  3397. CHECK (saved == (std::vector<std::int32_t> { 700, -1, (std::int32_t) 0xFFFFFF01u }));
  3398. // FL's slot assignment: saved slots keep their ParamID wherever it now sits; the empty slot stays empty;
  3399. // a vanished ParamID (0xFFFFFF01) leaves its slot dead; parameters without a slot get slots 3, 4… in list order
  3400. const auto positions = lynx::flVst3SlotPositions (saved, { 5u, 700u, 9u });
  3401. CHECK (positions == (std::vector<int> { 1, -1, -1, 0, 2 }));
  3402. CHECK (lynx::flVst3SlotPositions ({}, { 5u, 6u }) == (std::vector<int> { 0, 1 })); // new instance: slot = position
  3403. // VST2 state (REA_rodada89 §2): chunk and raw-parameter layouts, current (−9) and older versions
  3404. {
  3405. lynx::FlVst2State st;
  3406. st.flags = 8;
  3407. st.currentProgram = 3;
  3408. st.chunk = bytes ("BANKCHUNK");
  3409. const auto withChunk = lynx::writeFlVst2State (st);
  3410. lynx::FlVst2State back;
  3411. CHECK (lynx::parseFlVst2State (withChunk, back));
  3412. CHECK (back.version == -9 && back.flags == 8 && back.currentProgram == 3 && back.chunk == bytes ("BANKCHUNK") && back.params.empty());
  3413. CHECK (! back.chunkIsProgram());
  3414. lynx::FlVst2State raw;
  3415. raw.flags = 1;
  3416. raw.params = { 0.25f, 0.5f, 1.0f };
  3417. raw.programNames = { "Init" };
  3418. const auto withParams = lynx::writeFlVst2State (raw);
  3419. CHECK (withParams.size() == 4 + 1 + 4 + 8 + 4 + 4 + 12 + 4 + 25);
  3420. CHECK (lynx::parseFlVst2State (withParams, back));
  3421. CHECK (back.chunk.empty() && back.params == raw.params && back.allPrograms() && back.programNames == std::vector<std::string> { "Init" });
  3422. // version −5: no flags, i32 size, then i32 program; −6 without "all programs": the chunk is one program
  3423. std::vector<std::uint8_t> v5;
  3424. for (auto v : { 0xFFFFFFFBu, 4u, 7u })
  3425. u32 (v5, v);
  3426. for (auto c : bytes ("ABCD"))
  3427. v5.push_back (c);
  3428. CHECK (lynx::parseFlVst2State (v5, back) && back.version == -5 && back.currentProgram == 7 && back.chunk == bytes ("ABCD"));
  3429. std::vector<std::uint8_t> v6;
  3430. u32 (v6, 0xFFFFFFFAu);
  3431. v6.push_back (0);
  3432. for (auto v : { 2u, 0u })
  3433. u32 (v6, v);
  3434. v6.push_back ('x');
  3435. v6.push_back ('y');
  3436. CHECK (lynx::parseFlVst2State (v6, back) && back.chunkIsProgram() && back.chunk == bytes ("xy"));
  3437. // nothing to restore (123456), unknown old layouts, truncated chunks
  3438. std::vector<std::uint8_t> marker;
  3439. u32 (marker, 123456u);
  3440. CHECK (! lynx::parseFlVst2State (marker, back));
  3441. auto cut = withChunk;
  3442. cut.pop_back();
  3443. CHECK (! lynx::parseFlVst2State (cut, back));
  3444. CHECK (! lynx::parseFlVst2State ({}, back));
  3445. }
  3446. // version out of range or no type: not a usable wrapper
  3447. blob[0] = 13;
  3448. CHECK (! lynx::parseFlWrapper (blob, p));
  3449. CHECK (! lynx::parseFlWrapper ({ 12, 0, 0, 0 }, p));
  3450. }
  3451. TEST (schedule_index_matches_full_scan)
  3452. {
  3453. // random song (long notes, swing, clips with offset); indexed scheduling must yield
  3454. // exactly the same events as the full scan, including in the block that wraps around
  3455. lynx::Song s = lynx::Song::makeDefault();
  3456. s.swing = 0.6f;
  3457. std::uint32_t seed = 12345;
  3458. auto rnd = [&] (int n) { seed = seed * 1664525u + 1013904223u; return (int) ((seed >> 8) % (std::uint32_t) n); };
  3459. s.patterns.resize (3);
  3460. for (auto& p : s.patterns)
  3461. {
  3462. p.notes.clear();
  3463. for (int i = 0; i < 60; ++i)
  3464. {
  3465. lynx::Note n;
  3466. n.start = rnd (16 * lynx::ppq);
  3467. n.length = rnd (5) == 0 ? rnd (12 * lynx::ppq) + 1 : rnd (lynx::ppq) + 1;
  3468. n.channel = rnd (4);
  3469. n.key = 40 + rnd (40);
  3470. n.slide = rnd (20) == 0;
  3471. p.notes.push_back (n);
  3472. }
  3473. p.sortNotes();
  3474. }
  3475. s.playlist.clear();
  3476. for (int i = 0; i < 12; ++i)
  3477. {
  3478. lynx::Clip c;
  3479. c.pattern = rnd (3);
  3480. c.track = rnd (4);
  3481. c.start = rnd (40) * lynx::ppq;
  3482. c.length = (rnd (12) + 1) * lynx::ppq;
  3483. c.offset = rnd (4) * lynx::ppq;
  3484. s.playlist.push_back (c);
  3485. }
  3486. s.mode = lynx::PlayMode::song;
  3487. const auto index = lynx::makeScheduleIndex (s);
  3488. const auto tps = lynx::ticksPerSample (140.0, 48000.0);
  3489. const auto loop = (double) s.songLength();
  3490. int mismatches = 0, events = 0;
  3491. for (int block = 0; block < 400; ++block)
  3492. {
  3493. const auto start = block == 0 ? loop - 100.0 * tps : (double) rnd ((int) loop * 2);
  3494. const auto samples = 64 + rnd (24000); // up to ~1 beat per block
  3495. lynx::EventBuffer a, b;
  3496. lynx::scheduleSong (s, start, samples, tps, a, false, nullptr);
  3497. lynx::scheduleSong (s, start, samples, tps, b, false, &index);
  3498. events += a.size();
  3499. if (a.size() != b.size())
  3500. {
  3501. ++mismatches;
  3502. continue;
  3503. }
  3504. for (int i = 0; i < a.size(); ++i)
  3505. {
  3506. const auto& x = *(a.begin() + i);
  3507. const auto& y = *(b.begin() + i);
  3508. mismatches += x.sampleOffset != y.sampleOffset || x.type != y.type || x.key != y.key || x.channel != y.channel;
  3509. }
  3510. }
  3511. CHECK (events > 500);
  3512. CHECK (mismatches == 0);
  3513. }
  3514. TEST (mixer_track_eq_defaults_and_boost)
  3515. {
  3516. // FL defaults: 90 Hz, 1.5 kHz and 8 kHz; peak Q 0.632; shelf slope 0.607
  3517. lynx::TrackEq eq;
  3518. CHECK (std::abs (lynx::TrackEq::peakQ (eq.width[1]) - 0.632) < 0.01);
  3519. CHECK (std::abs (lynx::TrackEq::shelfSlope (eq.width[0]) - 0.607) < 0.01);
  3520. CHECK (! eq.active());
  3521. // +12 dB peak at 1.5 kHz on a track: a 1.5 kHz sine rises ~4x
  3522. lynx::Song song = lynx::Song::makeDefault();
  3523. song.mixer[1].eq.gainDb[1] = 12.0f;
  3524. for (auto& c : song.channels)
  3525. c.mixerTrack = 1;
  3526. song.channels[4].instrument = lynx::InstrumentKind::synth;
  3527. song.channels[4].synth = {};
  3528. song.channels[4].synth.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  3529. song.channels[4].synth.cutoff = 20000.0f;
  3530. auto render = [] (const lynx::Song& s) {
  3531. auto state = lynx::RackState::build (s, nullptr, 48000.0);
  3532. lynx::RackRenderer renderer;
  3533. renderer.prepare (48000.0);
  3534. lynx::LiveEventQueue live;
  3535. lynx::NoteEvent on; // 1.5 kHz ~ key 90.6: use 91 (1568 Hz), still within the wide peak
  3536. on.channel = 4;
  3537. on.key = 91;
  3538. on.velocity = 1.0f;
  3539. live.push (on);
  3540. std::vector<float> l (24000), r (24000);
  3541. for (int pos = 0; pos < 24000; pos += 480)
  3542. renderer.process (state.get(), false, 0.0, 480, l.data() + pos, r.data() + pos, pos == 0 ? &live : nullptr);
  3543. float peak = 0.0f;
  3544. for (std::size_t i = 12000; i < 24000; ++i)
  3545. peak = std::max (peak, std::abs (l[i]));
  3546. return peak;
  3547. };
  3548. const auto boosted = render (song);
  3549. song.mixer[1].eq.gainDb[1] = 0.0f;
  3550. const auto flat = render (song);
  3551. CHECK (flat > 0.01f);
  3552. CHECK (boosted / flat > 3.0f && boosted / flat < 4.2f);
  3553. }
  3554. TEST (bars_follow_time_signature_markers)
  3555. {
  3556. lynx::Song song;
  3557. song.beatsPerBar = 4;
  3558. const auto bar4 = (std::int64_t) lynx::ppq * 4;
  3559. auto bars = song.barsBetween (0, bar4 * 3);
  3560. CHECK (bars.size() == 3 && bars[2].tick == bar4 * 2 && bars[2].number == 3);
  3561. // 7/4 from bar 3, and 3/4 in the middle of a 7 bar (cuts and restarts)
  3562. song.addMarker ({ bar4 * 2, "Verso", 7, 4 });
  3563. song.addMarker ({ bar4, "Nome só", 0, 4 });
  3564. const auto seven = (std::int64_t) lynx::ppq * 7;
  3565. song.addMarker ({ bar4 * 2 + seven + lynx::ppq * 2, "", 3, 4 });
  3566. bars = song.barsBetween (0, bar4 * 2 + seven * 2);
  3567. CHECK (bars.size() >= 5);
  3568. CHECK (bars[1].length == bar4 && bars[2].tick == bar4 * 2 && bars[2].length == seven);
  3569. CHECK (bars[3].length == lynx::ppq * 2); // cut short by the change
  3570. CHECK (bars[4].length == lynx::ppq * 3 && bars[4].number == 5);
  3571. CHECK (song.markers()[0].name == "Nome só"); // sorted by tick
  3572. // from far away: starts at the right bar without losing the numbering
  3573. const auto far = song.barsBetween (lynx::ppq * 3000, lynx::ppq * 3001);
  3574. CHECK (far.size() == 1 && far[0].length == lynx::ppq * 3);
  3575. CHECK (song.barAt (lynx::ppq * 3000).number == far[0].number);
  3576. CHECK ((lynx::ppq * 3000 - far[0].tick) < far[0].length);
  3577. }
  3578. TEST (split_drum_channel_moves_notes_to_pads)
  3579. {
  3580. lynx::Song song = lynx::Song::makeDefault();
  3581. const auto channels = (int) song.channels.size();
  3582. song.patterns[0].notes.clear();
  3583. song.patterns[0].notes.push_back ({ 0, 240, 1, 36, 0.9f }); // kit kick (channel 1)
  3584. song.patterns[0].notes.push_back ({ 240, 240, 1, 38, 0.8f }); // snare
  3585. song.patterns[0].notes.push_back ({ 480, 240, 1, 50, 0.8f }); // key without a pad: dropped
  3586. song.patterns[0].notes.push_back ({ 480, 240, 3, 60, 0.8f }); // other channel: shifts by 1 position
  3587. lynx::Automation a;
  3588. a.target = { lynx::AutomationTargetKind::channelVolume, 3, 0, 0 };
  3589. song.automations.push_back (a);
  3590. std::vector<lynx::DrumPad> pads (2);
  3591. pads[0].name = "Kick";
  3592. pads[0].key = 36;
  3593. pads[0].layers = { { "kick.wav" } };
  3594. pads[1].name = "Snare";
  3595. pads[1].key = 38;
  3596. pads[1].layers = { { "snare soft.wav", 60, 0, 127, 0, 90 }, { "snare hard.wav", 60, 0, 127, 91, 127 } };
  3597. pads[1].cutGroup = 2;
  3598. pads[1].cutBy = 2;
  3599. lynx::splitDrumChannel (song, 1, pads);
  3600. CHECK ((int) song.channels.size() == channels + 1);
  3601. CHECK (song.channels[1].samplePath == "kick.wav" && song.channels[1].instrument == lynx::InstrumentKind::sampler);
  3602. CHECK (song.channels[2].zones.size() == 2 && song.channels[2].cutGroup == song.channels[2].cutBy && song.channels[2].cutGroup != 0);
  3603. const auto& notes = song.patterns[0].notes;
  3604. CHECK (notes.size() == 3);
  3605. CHECK (notes[0].channel == 1 && notes[0].key == 60);
  3606. CHECK (notes[1].channel == 2 && notes[1].key == 60);
  3607. CHECK (notes[2].channel == 4); // the old channel 3
  3608. CHECK (song.automations[0].target.index == 4);
  3609. }
  3610. TEST (flp_import_brings_playlist)
  3611. {
  3612. flp::Project p;
  3613. p.header.ppq = 96;
  3614. p.channels = { { 0, flp::ChannelType::sampler, "Kick", "", "", 1 } };
  3615. flp::Pattern empty;
  3616. empty.index = 1;
  3617. flp::Pattern full;
  3618. full.index = 2;
  3619. flp::Note n;
  3620. n.channel = 0;
  3621. full.notes.push_back (n);
  3622. p.patterns = { empty, full };
  3623. flp::Arrangement a;
  3624. flp::PlaylistItem item;
  3625. item.kind = flp::PlaylistItem::Kind::pattern;
  3626. item.index = 2;
  3627. item.position = 384;
  3628. item.length = 192;
  3629. item.track = 3;
  3630. item.patternStartTick = 96;
  3631. a.items.push_back (item);
  3632. item.index = 1; // empty pattern: discarded
  3633. a.items.push_back (item);
  3634. item.kind = flp::PlaylistItem::Kind::channelClip; // audio: not supported yet
  3635. a.items.push_back (item);
  3636. p.arrangements.push_back (a);
  3637. const auto s = lynx::importFlp (p);
  3638. CHECK (s.mode == lynx::PlayMode::song);
  3639. CHECK (s.playlist.size() == 1);
  3640. CHECK (s.playlist[0].pattern == 0 && s.playlist[0].track == 3);
  3641. CHECK (s.playlist[0].start == 4 * lynx::ppq && s.playlist[0].length == 2 * lynx::ppq && s.playlist[0].offset == lynx::ppq);
  3642. }
  3643. TEST (rack_releases_notes_on_position_jump)
  3644. {
  3645. lynx::Song s;
  3646. lynx::ChannelDef c;
  3647. c.instrument = lynx::InstrumentKind::synth;
  3648. s.channels.push_back (c);
  3649. auto& p = s.patterns.emplace_back();
  3650. lynx::Note n;
  3651. n.start = 0;
  3652. n.length = 4 * lynx::ppq; // long note
  3653. p.notes.push_back (n);
  3654. s.bpm = 120.0;
  3655. auto state = lynx::RackState::build (s, nullptr, 48000.0);
  3656. lynx::RackRenderer r;
  3657. r.prepare (48000.0);
  3658. std::vector<float> l (512), rr (512);
  3659. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  3660. double tick = 0.0;
  3661. for (int i = 0; i < 20; ++i, tick += 512 * tps)
  3662. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  3663. // jump to tick 2000 (mid-note, no new "on"): the synth must go silent
  3664. for (int i = 0; i < 40; ++i)
  3665. r.process (state.get(), true, 2000.0 + i * 512 * tps, 512, l.data(), rr.data());
  3666. float tail = 0.0f;
  3667. for (auto v : l)
  3668. tail = std::max (tail, std::abs (v));
  3669. CHECK (tail < 0.001f);
  3670. }
  3671. namespace
  3672. {
  3673. // Two samplers with a constant 0.5 signal on different inserts; returns the output peak.
  3674. float juce_db (float gain)
  3675. {
  3676. return 20.0f * std::log10 (gain);
  3677. }
  3678. float renderMixerCase (const std::function<void (lynx::Song&)>& tweak, lynx::MeterBank* meters = nullptr)
  3679. {
  3680. lynx::Song s;
  3681. s.mixer = lynx::Song::defaultMixer (4);
  3682. for (int i = 0; i < 2; ++i)
  3683. {
  3684. lynx::ChannelDef c;
  3685. c.instrument = lynx::InstrumentKind::sampler;
  3686. c.samplePath = "dc";
  3687. c.volume = 1.0f;
  3688. c.mixerTrack = i + 1;
  3689. s.channels.push_back (c);
  3690. }
  3691. s.patterns.emplace_back();
  3692. tweak (s);
  3693. auto provider = [] (const std::string&) {
  3694. auto d = std::make_shared<lynx::SampleData>();
  3695. d->left.assign (48000, 0.5f);
  3696. return std::shared_ptr<const lynx::SampleData> (d);
  3697. };
  3698. auto state = lynx::RackState::build (s, nullptr, 48000.0, provider);
  3699. lynx::RackRenderer r;
  3700. r.prepare (48000.0, 256);
  3701. lynx::LiveEventQueue live;
  3702. for (int ch = 0; ch < 2; ++ch)
  3703. {
  3704. lynx::NoteEvent on;
  3705. on.channel = ch;
  3706. on.velocity = 1.0f;
  3707. live.push (on);
  3708. }
  3709. std::vector<float> l (1000), rr (1000); // larger than the mixer block: tests the chunking
  3710. r.process (state.get(), false, 0.0, 1000, l.data(), rr.data(), &live, meters);
  3711. return std::max (std::abs (l[999]), std::abs (rr[999]));
  3712. }
  3713. } // namespace
  3714. TEST (mixer_routes_mutes_solos_and_meters)
  3715. {
  3716. // two 0.5 channels summed on the Master at 0 dB
  3717. CHECK (std::abs (renderMixerCase ([] (lynx::Song&) {}) - 1.0f) < 1e-4f);
  3718. // insert 1 muted: only channel 2 remains
  3719. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.mixer[1].muted = true; }) - 0.5f) < 1e-4f);
  3720. // solo on insert 2: same
  3721. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.mixer[2].solo = true; }) - 0.5f) < 1e-4f);
  3722. // FL fader curve: 0.8 = 0 dB, max ≈ +5.6 dB, half travel ≈ -12.7 dB
  3723. CHECK (std::abs (lynx::faderGain (0.8f) - 1.0f) < 1e-5f);
  3724. CHECK (std::abs (juce_db (lynx::faderGain (1.0f)) - 5.59f) < 0.05f);
  3725. CHECK (lynx::faderGain (0.0f) == 0.0f);
  3726. // Master fader at max
  3727. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.mixer[0].volume = 1.0f; }) - lynx::faderGain (1.0f)) < 1e-4f);
  3728. // both channels on the same insert, insert fader at 0.4
  3729. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) {
  3730. s.channels[1].mixerTrack = 1;
  3731. s.mixer[1].volume = 0.4f;
  3732. }) - lynx::faderGain (0.4f)) < 1e-4f);
  3733. // a route outside the mixer falls to the last existing insert, without crashing
  3734. CHECK (renderMixerCase ([] (lynx::Song& s) { s.channels[0].mixerTrack = 100; }) > 0.9f);
  3735. // no mixer: everything straight to the output at 0 dB
  3736. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.mixer.clear(); }) - 1.0f) < 1e-4f);
  3737. lynx::MeterBank meters;
  3738. renderMixerCase ([] (lynx::Song&) {}, &meters);
  3739. CHECK (std::abs (meters.takePeak (1, 0) - 0.5f) < 1e-4f);
  3740. CHECK (std::abs (meters.takePeak (0, 1) - 1.0f) < 1e-4f);
  3741. CHECK (meters.takePeak (1, 0) == 0.0f); // reading resets
  3742. CHECK (meters.takePeak (3, 0) == 0.0f); // insert with no channel
  3743. }
  3744. TEST (next_free_insert_and_flp_mixer_import)
  3745. {
  3746. auto s = lynx::Song::makeDefault();
  3747. CHECK (s.mixer.size() == 17 && s.mixer[0].name == "Master");
  3748. CHECK (s.nextFreeInsert() == 6); // the 5 default channels take 1..5
  3749. flp::Project p;
  3750. p.channels = { { 0, flp::ChannelType::sampler, "Kick", "", "", 30 } };
  3751. p.mixer = { { 0, "" }, { 30, "Drums" } };
  3752. const auto song = lynx::importFlp (p);
  3753. CHECK (song.channels[0].mixerTrack == 30);
  3754. CHECK (song.mixer.size() == 31);
  3755. CHECK (song.mixer[30].name == "Drums" && song.mixer[0].name == "Master");
  3756. }
  3757. namespace
  3758. {
  3759. float runEffectOnSine (lynx::EffectKind kind, const std::function<void (lynx::EffectSlot&)>& tweak, double hz, float amp,
  3760. int frames = 48000)
  3761. {
  3762. auto fx = lynx::makeEffect (kind);
  3763. fx->prepare (48000.0);
  3764. auto slot = lynx::makeEffectSlot (kind);
  3765. tweak (slot);
  3766. std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
  3767. for (int i = 0; i < frames; ++i)
  3768. l[(std::size_t) i] = r[(std::size_t) i] = amp * (float) std::sin (6.283185307 * hz * i / 48000.0);
  3769. for (int i = 0; i < frames; i += 480)
  3770. fx->process (l.data() + i, r.data() + i, std::min (480, frames - i), slot.params, 120.0);
  3771. float peak = 0.0f;
  3772. for (int i = frames / 2; i < frames; ++i) // second half: already settled
  3773. {
  3774. CHECK (std::isfinite (l[(std::size_t) i]));
  3775. peak = std::max (peak, std::abs (l[(std::size_t) i]));
  3776. }
  3777. return peak;
  3778. }
  3779. } // namespace
  3780. TEST (parametric_eq_bands_and_response)
  3781. {
  3782. // default: transparent
  3783. auto slot = lynx::makeEffectSlot (lynx::EffectKind::parametricEq);
  3784. CHECK (std::abs (lynx::peq::responseDb (slot.params, 48000.0, 1000.0)) < 0.01);
  3785. CHECK (std::abs (runEffectOnSine (lynx::EffectKind::parametricEq, [] (auto&) {}, 1000.0, 0.5f) - 0.5f) < 0.005f);
  3786. // +12 dB peak at 1 kHz on band 4: the response and the signal agree
  3787. auto boost = [] (lynx::EffectSlot& s) {
  3788. s.params[(std::size_t) lynx::peq::param (3, 1)] = lynx::peq::hzValue (1000.0);
  3789. s.params[(std::size_t) lynx::peq::param (3, 2)] = lynx::peq::gainValue (12.0);
  3790. };
  3791. boost (slot);
  3792. CHECK (std::abs (lynx::peq::responseDb (slot.params, 48000.0, 1000.0) - 12.0) < 0.2);
  3793. CHECK (std::abs (lynx::peq::responseDb (slot.params, 48000.0, 100.0)) < 1.0);
  3794. const auto peak = runEffectOnSine (lynx::EffectKind::parametricEq, boost, 1000.0, 0.1f);
  3795. CHECK (std::abs (peak - 0.1f * std::pow (10.0f, 12.0f / 20.0f)) < 0.01f);
  3796. // low cut at 48 dB/oct at 200 Hz: 50 Hz (2 octaves below) drops ~96 dB; 2 kHz passes
  3797. auto cut = lynx::makeEffectSlot (lynx::EffectKind::parametricEq);
  3798. cut.params[(std::size_t) lynx::peq::param (0, 0)] = lynx::peq::typeValue (lynx::peq::BandType::lowCut);
  3799. cut.params[(std::size_t) lynx::peq::param (0, 1)] = lynx::peq::hzValue (200.0);
  3800. cut.params[(std::size_t) lynx::peq::param (0, 3)] = lynx::peq::qValue (0.70710678); // Butterworth
  3801. cut.params[(std::size_t) (lynx::peq::slopeParam + 0)] = 1.0f;
  3802. CHECK (lynx::peq::responseDb (cut.params, 48000.0, 50.0) < -80.0);
  3803. CHECK (std::abs (lynx::peq::responseDb (cut.params, 48000.0, 200.0) + 3.0) < 0.5); // -3 dB at the cutoff (Butterworth)
  3804. CHECK (std::abs (lynx::peq::responseDb (cut.params, 48000.0, 2000.0)) < 0.1);
  3805. // round-trip conversions
  3806. CHECK (std::abs (lynx::peq::hz (lynx::peq::hzValue (440.0)) - 440.0) < 0.5);
  3807. CHECK (std::abs (lynx::peq::q (lynx::peq::qValue (2.5)) - 2.5) < 0.01);
  3808. CHECK (lynx::peq::type (lynx::peq::typeValue (lynx::peq::BandType::notch)) == lynx::peq::BandType::notch);
  3809. }
  3810. TEST (fl_parametric_eq2_state_maps_to_bands)
  3811. {
  3812. // v8 blob: band 1 low cut at 100 Hz "Steep 8", band 4 peak +6 dB at 1 kHz, output −3 dB
  3813. std::vector<std::uint8_t> blob (354, 0);
  3814. auto put = [&] (std::size_t off, std::int32_t v) { std::memcpy (blob.data() + off, &v, 4); };
  3815. put (0, 8);
  3816. auto param = [&] (int i, std::int32_t v) { put (4 + 4 * (std::size_t) i, v); };
  3817. auto hzRaw = [] (double hz) { return (std::int32_t) std::lround (std::log (hz / 20.0) / std::log (1000.0) * 65536.0); };
  3818. for (int b = 0; b < 7; ++b)
  3819. param (21 + b, 0); // all off
  3820. param (21 + 0, 3); // high pass (= low cut)
  3821. param (7 + 0, hzRaw (100.0));
  3822. param (14 + 0, 40124); // default width -> Q ~0.68
  3823. param (28 + 0, -3); // Steep 8 -> 4 biquads
  3824. param (21 + 3, 6); // peaking
  3825. param (7 + 3, hzRaw (1000.0));
  3826. param (3, 600); // +6 dB
  3827. param (14 + 3, 25412);
  3828. param (35, -300); // main level −3 dB
  3829. put (0x13E, 0x7F);
  3830. const auto slot = lynx::effectFromFl ("Fruity Parametric EQ 2", blob);
  3831. CHECK (slot.has_value() && slot->kind == lynx::EffectKind::parametricEq);
  3832. const auto& p = slot->params;
  3833. CHECK (lynx::peq::type (p[(std::size_t) lynx::peq::param (0, 0)]) == lynx::peq::BandType::lowCut);
  3834. CHECK (lynx::peq::flLaw (p) && lynx::peqfl::fromParams (p).order[0] == -3); // FL's own order (REA_rodada50)
  3835. CHECK (lynx::peq::type (p[(std::size_t) lynx::peq::param (3, 0)]) == lynx::peq::BandType::peak);
  3836. CHECK (lynx::peq::type (p[(std::size_t) lynx::peq::param (1, 0)]) == lynx::peq::BandType::off);
  3837. CHECK (std::abs (lynx::peq::responseDb (p, 48000.0, 1000.0) - 3.0) < 0.3); // +6 from the peak −3 from the output
  3838. CHECK (lynx::peq::responseDb (p, 48000.0, 30.0) < -40.0); // 48 dB/oct below 100 Hz
  3839. CHECK (std::abs (lynx::peq::responseDb (p, 48000.0, 8000.0) + 3.0) < 0.3);
  3840. // a band muted by the mask doesn't sound (FL's mask travels with the slot, REA_rodada50)
  3841. put (0x13E, 0x7F & ~(1 << 3));
  3842. const auto muted = lynx::effectFromFl ("Fruity Parametric EQ 2", blob);
  3843. CHECK ((lynx::peqfl::fromParams (muted->params).mask & (1u << 3)) == 0);
  3844. CHECK (std::abs (lynx::peq::responseDb (muted->params, 48000.0, 1000.0) + 3.0) < 0.3);
  3845. }
  3846. TEST (reverb_low_cut_and_predelay)
  3847. {
  3848. // impulse: with 100 ms predelay the wet only starts later; with a 3 kHz low cut a
  3849. // 100 Hz sine barely enters the reverb
  3850. auto run = [] (const std::function<void (lynx::EffectSlot&)>& tweak, double hz, bool impulse) {
  3851. auto fx = lynx::makeEffect (lynx::EffectKind::reverb);
  3852. fx->prepare (48000.0);
  3853. auto slot = lynx::makeEffectSlot (lynx::EffectKind::reverb);
  3854. slot.params[4] = 0.0f; // wet only
  3855. slot.params[3] = 1.0f;
  3856. tweak (slot);
  3857. std::vector<float> l (48000), r (48000);
  3858. for (int i = 0; i < 48000; ++i)
  3859. 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);
  3860. for (int i = 0; i < 48000; i += 480)
  3861. fx->process (l.data() + i, r.data() + i, 480, slot.params, 120.0);
  3862. return l;
  3863. };
  3864. const auto delayed = run ([] (auto& s) { s.params[7] = 0.1f; }, 0.0, true);
  3865. float early = 0.0f, later = 0.0f;
  3866. for (std::size_t i = 0; i < 4800; ++i)
  3867. early = std::max (early, std::abs (delayed[i]));
  3868. for (std::size_t i = 4800; i < 24000; ++i)
  3869. later = std::max (later, std::abs (delayed[i]));
  3870. CHECK (early == 0.0f && later > 0.0f);
  3871. 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; };
  3872. const auto open = energy (run ([] (auto&) {}, 100.0, false));
  3873. const auto cut = energy (run ([] (auto& s) { s.params[5] = 1.0f; }, 100.0, false));
  3874. CHECK (cut < open * 0.01);
  3875. }
  3876. TEST (utility_effect_and_fl_stereo_tools)
  3877. {
  3878. // transparent by default; balance fully right silences the left
  3879. CHECK (std::abs (runEffectOnSine (lynx::EffectKind::utility, [] (auto&) {}, 440.0, 0.5f) - 0.5f) < 0.001f);
  3880. CHECK (runEffectOnSine (lynx::EffectKind::utility, [] (auto& s) { s.params[1] = 1.0f; }, 440.0, 0.5f) < 0.001f);
  3881. // Stereo Shaper at half the knob range (6400): each gain = (3^0.5 − 1)/2 (FL's knob law, REA_rodada54 §2.2)
  3882. std::vector<std::uint8_t> blob (36, 0);
  3883. auto put = [&] (std::size_t off, std::int32_t v) { std::memcpy (blob.data() + off, &v, 4); };
  3884. put (4, 6400); put (8, 6400); put (12, 6400); put (16, 6400);
  3885. const auto slot = lynx::effectFromFl ("Fruity Stereo Shaper", blob);
  3886. CHECK (slot && slot->kind == lynx::EffectKind::stereoShaper);
  3887. auto shaperFx = lynx::makeEffect (*slot);
  3888. shaperFx->prepare (44100.0);
  3889. float zl = 0.0f, zr = 0.0f;
  3890. shaperFx->process (&zl, &zr, 0, slot->params, 120.0); // takes the controls
  3891. shaperFx->reset(); // FL's Flush: the gains jump to their targets
  3892. float l = 1.0f, r = 0.0f;
  3893. shaperFx->process (&l, &r, 1, slot->params, 120.0);
  3894. const auto half = (float) ((std::sqrt (3.0) - 1.0) / 2.0);
  3895. CHECK (std::abs (l - half) < 1e-4f && std::abs (r - half) < 1e-4f);
  3896. auto fx = lynx::makeEffect (lynx::EffectKind::utility);
  3897. // Balance: volume 256 = 0 dB, pan −128 = fully left
  3898. std::vector<std::uint8_t> bal (8, 0);
  3899. std::int32_t pan = -128, vol = 256;
  3900. std::memcpy (bal.data(), &pan, 4);
  3901. std::memcpy (bal.data() + 4, &vol, 4);
  3902. const auto b = lynx::effectFromFl ("Fruity Balance", bal);
  3903. CHECK (b && b->kind == lynx::EffectKind::fruityBalance); // FL's exact law (REA_rodada58 §2): fully left sums R into L
  3904. if (b)
  3905. {
  3906. auto balance = lynx::makeEffect (*b);
  3907. balance->prepare (44100.0);
  3908. l = 0.5f; r = 0.5f;
  3909. balance->process (&l, &r, 1, b->params, 120.0);
  3910. CHECK (std::abs (l - 0.70710678f) < 1e-6f && r == 0.0f);
  3911. }
  3912. }
  3913. TEST (effects_eq_compressor_behave)
  3914. {
  3915. // EQ at default is transparent
  3916. CHECK (std::abs (runEffectOnSine (lynx::EffectKind::eq, [] (auto&) {}, 1000.0, 0.5f) - 0.5f) < 0.01f);
  3917. // +18 dB on the lows: 60 Hz rises a lot, 5 kHz barely
  3918. CHECK (runEffectOnSine (lynx::EffectKind::eq, [] (auto& s) { s.params[0] = 1.0f; }, 60.0, 0.05f) > 0.3f);
  3919. CHECK (runEffectOnSine (lynx::EffectKind::eq, [] (auto& s) { s.params[0] = 1.0f; }, 5000.0, 0.05f) < 0.06f);
  3920. // compressor: threshold -20 dB, ratio 20:1 flattens a 0 dBFS signal to near the threshold
  3921. const auto squashed = runEffectOnSine (lynx::EffectKind::compressor, [] (auto& s) {
  3922. s.params[0] = 40.0f / 60.0f;
  3923. s.params[1] = 1.0f;
  3924. }, 200.0, 1.0f);
  3925. CHECK (squashed < 0.2f && squashed > 0.05f);
  3926. // a signal below the threshold passes intact
  3927. CHECK (std::abs (runEffectOnSine (lynx::EffectKind::compressor, [] (auto& s) { s.params[0] = 1.0f; }, 200.0, 0.3f) - 0.3f) < 0.01f);
  3928. }
  3929. TEST (effects_delay_echoes_on_tempo_and_reverb_has_tail)
  3930. {
  3931. auto delay = lynx::makeEffect (lynx::EffectKind::delay);
  3932. delay->prepare (48000.0);
  3933. auto slot = lynx::makeEffectSlot (lynx::EffectKind::delay);
  3934. slot.params[0] = 3.5f / 8.0f; // index 3 = 1/4 = 0.5 s at 120 BPM
  3935. slot.params[1] = 0.0f; // no feedback: a single echo
  3936. slot.params[2] = 1.0f;
  3937. slot.params[4] = 0.0f; // no damping
  3938. std::vector<float> l (48000, 0.0f), r (48000, 0.0f);
  3939. l[0] = r[0] = 1.0f;
  3940. delay->process (l.data(), r.data(), 48000, slot.params, 120.0);
  3941. CHECK (std::abs (l[24000] - 1.0f) < 1e-4f);
  3942. CHECK (std::abs (l[12000]) < 1e-6f);
  3943. auto reverb = lynx::makeEffect (lynx::EffectKind::reverb);
  3944. reverb->prepare (48000.0);
  3945. auto rs = lynx::makeEffectSlot (lynx::EffectKind::reverb);
  3946. rs.params[3] = 1.0f;
  3947. std::fill (l.begin(), l.end(), 0.0f);
  3948. std::fill (r.begin(), r.end(), 0.0f);
  3949. l[0] = r[0] = 1.0f;
  3950. reverb->process (l.data(), r.data(), 48000, rs.params, 120.0);
  3951. float tail = 0.0f;
  3952. for (int i = 4800; i < 9600; ++i) // between 100 and 200 ms after the impulse
  3953. tail = std::max (tail, std::abs (l[(std::size_t) i]));
  3954. CHECK (tail > 1e-4f && tail < 1.0f);
  3955. CHECK (lynx::formatEffectParam (lynx::EffectKind::delay, 0, slot.params[0], 120.0) == "1/4 (500 ms)");
  3956. }
  3957. TEST (rack_runs_insert_effects_and_reuses_them)
  3958. {
  3959. // an insert with EQ at -inf doesn't exist; we use the EQ output at -18 dB to see the effect act
  3960. const auto quiet = renderMixerCase ([] (lynx::Song& s) {
  3961. auto eq = lynx::makeEffectSlot (lynx::EffectKind::eq);
  3962. eq.params[4] = 0.0f; // output -18 dB
  3963. s.mixer[1].effects.push_back (eq);
  3964. });
  3965. CHECK (quiet > 0.55f && quiet < 0.6f); // 0.5 from channel 2 + 0.5 * 0.126 from channel 1
  3966. const auto bypassed = renderMixerCase ([] (lynx::Song& s) {
  3967. auto eq = lynx::makeEffectSlot (lynx::EffectKind::eq);
  3968. eq.params[4] = 0.0f;
  3969. eq.enabled = false;
  3970. s.mixer[1].effects.push_back (eq);
  3971. });
  3972. CHECK (std::abs (bypassed - 1.0f) < 1e-4f);
  3973. auto s = lynx::Song::makeDefault();
  3974. s.mixer[2].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::reverb));
  3975. auto a = lynx::RackState::build (s, nullptr, 48000.0);
  3976. s.mixer[2].effects[0].params[0] = 0.9f; // turning a knob doesn't recreate the effect
  3977. auto b = lynx::RackState::build (s, a.get(), 48000.0);
  3978. CHECK (b->effects[2][0] == a->effects[2][0]);
  3979. s.mixer[2].effects[0] = lynx::makeEffectSlot (lynx::EffectKind::delay);
  3980. auto c = lynx::RackState::build (s, b.get(), 48000.0);
  3981. CHECK (c->effects[2][0] != b->effects[2][0]);
  3982. }
  3983. TEST (audio_clips_trigger_cut_and_resume)
  3984. {
  3985. lynx::Song s;
  3986. s.bpm = 120.0; // 1 beat = 0.5 s = 24000 samples at 48 kHz
  3987. lynx::ChannelDef c;
  3988. c.instrument = lynx::InstrumentKind::audioClip;
  3989. c.samplePath = "ramp";
  3990. c.volume = 1.0f;
  3991. s.channels.push_back (c);
  3992. s.patterns.emplace_back();
  3993. lynx::Clip clip;
  3994. clip.kind = lynx::ClipKind::audio;
  3995. clip.channel = 0;
  3996. clip.start = lynx::ppq; // starts at beat 2
  3997. clip.length = 2 * lynx::ppq; // lasts 2 beats
  3998. clip.offset = lynx::ppq; // skips the first beat of the audio
  3999. s.playlist.push_back (clip);
  4000. s.mode = lynx::PlayMode::song;
  4001. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  4002. lynx::EventBuffer ev;
  4003. lynx::scheduleSong (s, 0.0, 4 * 24000, tps, ev);
  4004. CHECK (ev.size() == 2);
  4005. CHECK (ev[0].type == lynx::NoteEvent::Type::on && ev[0].sampleOffset == 24000 && std::abs (ev[0].startSeconds - 0.5) < 1e-9);
  4006. CHECK (ev[1].type == lynx::NoteEvent::Type::off && ev[1].sampleOffset == 3 * 24000);
  4007. // seek to the middle of the clip (beat 2.5): with resume, starts at the block's first sample
  4008. ev.clear();
  4009. lynx::scheduleSong (s, 1.5 * lynx::ppq, 256, tps, ev, true);
  4010. CHECK (ev.size() == 1 && ev[0].sampleOffset == 0 && std::abs (ev[0].startSeconds - 0.75) < 1e-9);
  4011. ev.clear();
  4012. lynx::scheduleSong (s, 1.5 * lynx::ppq, 256, tps, ev, false);
  4013. CHECK (ev.size() == 0);
  4014. // sample whose value = position in seconds: lets us check where playback started
  4015. auto provider = [] (const std::string&) {
  4016. auto d = std::make_shared<lynx::SampleData>();
  4017. d->sampleRate = 48000.0;
  4018. for (int i = 0; i < 48000 * 3; ++i)
  4019. d->left.push_back ((float) i / 48000.0f);
  4020. return std::shared_ptr<const lynx::SampleData> (d);
  4021. };
  4022. auto state = lynx::RackState::build (s, nullptr, 48000.0, provider);
  4023. lynx::RackRenderer r;
  4024. r.prepare (48000.0, 512);
  4025. std::vector<float> l (256), rr (256);
  4026. // first block already playing mid-clip
  4027. r.process (state.get(), true, 1.5 * lynx::ppq, 256, l.data(), rr.data());
  4028. const auto unity = 1.0f / lynx::channelGain (10000.0f / 12800.0f); // clip level at 100% channel volume (REA_rodada89 §4.2)
  4029. CHECK (std::abs (l[0] - 0.75f * unity) < 0.002f);
  4030. // the clip ends at beat 3: after the short fade, silence (the sample would still have audio)
  4031. double tick = 3.0 * lynx::ppq - 10 * tps;
  4032. r.process (state.get(), false, tick, 256, l.data(), rr.data()); // stops (fade)
  4033. for (int i = 0; i < 4; ++i)
  4034. r.process (state.get(), false, tick, 256, l.data(), rr.data());
  4035. CHECK (std::abs (l[255]) < 1e-6f);
  4036. }
  4037. TEST (remove_channel_fixes_notes_and_audio_clips)
  4038. {
  4039. lynx::Song s;
  4040. s.channels.resize (3);
  4041. s.patterns.resize (1);
  4042. s.patterns[0].notes = { { 0, 240, 0, 60, 1.0f }, { 0, 240, 1, 60, 1.0f }, { 0, 240, 2, 60, 1.0f } };
  4043. lynx::Clip a;
  4044. a.kind = lynx::ClipKind::audio;
  4045. a.channel = 1;
  4046. a.length = 100;
  4047. lynx::Clip b = a;
  4048. b.channel = 2;
  4049. s.playlist = { a, b };
  4050. s.removeChannel (1);
  4051. CHECK (s.channels.size() == 2);
  4052. CHECK (s.patterns[0].notes.size() == 2 && s.patterns[0].notes[1].channel == 1);
  4053. CHECK (s.playlist.size() == 1 && s.playlist[0].channel == 1);
  4054. }
  4055. TEST (flp_import_audio_clips_with_offsets)
  4056. {
  4057. flp::Project p;
  4058. p.header.ppq = 96;
  4059. p.tempo = 120.0;
  4060. p.channels = { { 0, flp::ChannelType::audioClip, "Vocal", "", "%FLStudioData%\\v.wav", 2 },
  4061. { 1, flp::ChannelType::audioClip, "Loop", "", "%FLStudioData%\\l.wav", 3 } };
  4062. flp::Arrangement arr;
  4063. flp::PlaylistItem beats; // offset in beats: end - start = duration in beats
  4064. beats.kind = flp::PlaylistItem::Kind::channelClip;
  4065. beats.index = 0;
  4066. beats.position = 192;
  4067. beats.length = 384; // 4 beats
  4068. beats.startOffset = 2.0f;
  4069. beats.endOffset = 6.0f;
  4070. flp::PlaylistItem ms = beats; // offset in ms: 4 beats at 120 BPM = 2000 ms
  4071. ms.index = 1;
  4072. ms.startOffset = 500.0f;
  4073. ms.endOffset = 2500.0f;
  4074. arr.items = { beats, ms };
  4075. p.arrangements.push_back (arr);
  4076. lynx::FlpImportOptions options;
  4077. options.roots = { "C:\\FL", "C:\\FL\\Data", "" };
  4078. options.fileExists = [] (const std::string&) { return true; };
  4079. const auto s = lynx::importFlp (p, options);
  4080. CHECK (s.channels.size() == 2 && s.channels[0].instrument == lynx::InstrumentKind::audioClip);
  4081. CHECK (s.channels[0].samplePath == "C:\\FL\\Data\\v.wav");
  4082. CHECK (s.playlist.size() == 2);
  4083. CHECK (s.playlist[0].kind == lynx::ClipKind::audio && s.playlist[0].start == 2 * lynx::ppq);
  4084. CHECK (s.playlist[0].offset == 2 * lynx::ppq);
  4085. CHECK (s.playlist[1].offset == lynx::ppq); // 500 ms at 120 BPM = 1 beat
  4086. CHECK (s.mode == lynx::PlayMode::song);
  4087. }
  4088. TEST (plugin_factories_create_reuse_and_fallback)
  4089. {
  4090. lynx::Song s;
  4091. s.mixer = lynx::Song::defaultMixer (2);
  4092. lynx::ChannelDef c;
  4093. c.instrument = lynx::InstrumentKind::plugin;
  4094. c.plugin.id = "VST3-Synth-123";
  4095. c.mixerTrack = 1;
  4096. s.channels.push_back (c);
  4097. lynx::ChannelDef missing = c;
  4098. missing.plugin.id = "VST3-Ausente-0";
  4099. s.channels.push_back (missing);
  4100. lynx::EffectSlot fx;
  4101. fx.kind = lynx::EffectKind::plugin;
  4102. fx.plugin.id = "VST3-Reverb-9";
  4103. s.mixer[1].effects.push_back (fx);
  4104. s.patterns.emplace_back();
  4105. int instrumentsMade = 0, effectsMade = 0;
  4106. lynx::RackState::Factories f;
  4107. f.pluginInstruments = [&] (const lynx::PluginRef& ref, double) -> std::shared_ptr<lynx::Instrument> {
  4108. if (ref.id != "VST3-Synth-123")
  4109. return nullptr;
  4110. ++instrumentsMade;
  4111. auto synth = lynx::makeInstrument (lynx::InstrumentKind::synth);
  4112. synth->prepare (48000.0);
  4113. return synth;
  4114. };
  4115. f.pluginEffects = [&] (const lynx::PluginRef&, double) -> std::shared_ptr<lynx::Effect> {
  4116. ++effectsMade;
  4117. auto eq = lynx::makeEffect (lynx::EffectKind::eq);
  4118. eq->prepare (48000.0);
  4119. return eq;
  4120. };
  4121. auto a = lynx::RackState::build (s, nullptr, 48000.0, f);
  4122. CHECK (instrumentsMade == 1 && effectsMade == 1);
  4123. CHECK (a->instruments.size() == 2 && a->instruments[1] != nullptr); // missing: muted, but it exists
  4124. // changing the saved state doesn't recreate; swapping the plugin does
  4125. s.channels[0].plugin.state = "abc";
  4126. auto b = lynx::RackState::build (s, a.get(), 48000.0, f);
  4127. CHECK (instrumentsMade == 1 && b->instruments[0] == a->instruments[0] && b->effects[1][0] == a->effects[1][0]);
  4128. s.mixer[1].effects[0].plugin.id = "VST3-Outro-1";
  4129. auto c2 = lynx::RackState::build (s, b.get(), 48000.0, f);
  4130. CHECK (effectsMade == 2);
  4131. // rendering with a missing plugin and a plugin slot breaks nothing
  4132. lynx::RackRenderer r;
  4133. r.prepare (48000.0, 256);
  4134. lynx::LiveEventQueue live;
  4135. lynx::NoteEvent on;
  4136. on.channel = 0;
  4137. on.velocity = 1.0f;
  4138. live.push (on);
  4139. on.channel = 1;
  4140. live.push (on);
  4141. std::vector<float> l (256), rr (256);
  4142. r.process (c2.get(), false, 0.0, 256, l.data(), rr.data(), &live);
  4143. float peak = 0.0f;
  4144. for (auto v : l)
  4145. {
  4146. CHECK (std::isfinite (v));
  4147. peak = std::max (peak, std::abs (v));
  4148. }
  4149. CHECK (peak > 0.0f);
  4150. }
  4151. TEST (export_song_adds_silent_tail_and_never_restarts)
  4152. {
  4153. auto s = lynx::Song::makeDefault();
  4154. s.playlist.clear();
  4155. std::int64_t length = 0;
  4156. auto pat = lynx::makeExportSong (s, true, lynx::ppq * 8, length); // empty Playlist: uses the pattern
  4157. CHECK (length == lynx::ppq * 4);
  4158. CHECK (pat.mode == lynx::PlayMode::song && pat.playlist.size() == 2);
  4159. CHECK (pat.songLength() >= length + lynx::ppq * 8);
  4160. // no new notes after the song ends, only silence until the end of the tail
  4161. lynx::EventBuffer events;
  4162. const auto tps = lynx::ticksPerSample (pat.bpm, 48000.0);
  4163. int onsAfterEnd = 0, onsBefore = 0;
  4164. for (double tick = 0.0; tick < (double) (length + lynx::ppq * 8); tick += 512 * tps)
  4165. {
  4166. events.clear();
  4167. lynx::scheduleSong (pat, tick, 512, tps, events);
  4168. for (const auto& e : events)
  4169. if (e.type == lynx::NoteEvent::Type::on)
  4170. (tick + e.sampleOffset * tps >= (double) length ? onsAfterEnd : onsBefore)++;
  4171. }
  4172. CHECK (onsBefore == (int) s.patterns[0].notes.size());
  4173. CHECK (onsAfterEnd == 0);
  4174. // with Playlist: the end is the last clip's, without rounding
  4175. s.playlist.push_back ({ 0, 0, lynx::ppq * 2, lynx::ppq * 3, 0 });
  4176. auto song = lynx::makeExportSong (s, true, 0, length);
  4177. CHECK (length == lynx::ppq * 5 && song.playlist.size() == 1);
  4178. song = lynx::makeExportSong (s, false, 0, length);
  4179. CHECK (length == lynx::ppq * 4 && song.playlist[0].start == 0);
  4180. }
  4181. TEST (note_edit_selection_operations)
  4182. {
  4183. namespace ne = lynx::noteedit;
  4184. lynx::Pattern p;
  4185. p.notes = { { 0, 240, 0, 60 }, { 480, 240, 0, 64 }, { 960, 480, 1, 67 } };
  4186. // sorting keeps the indices in step
  4187. p.notes[0].start = 1200;
  4188. std::vector<int> tracked { 0, 2 };
  4189. ne::sortTracking (p, tracked);
  4190. CHECK (p.notes[2].start == 1200 && p.notes[(std::size_t) tracked[0]].start == 1200);
  4191. CHECK (p.notes[(std::size_t) tracked[1]].key == 67);
  4192. // group move respects the limits
  4193. std::int64_t dt = -5000;
  4194. int dk = 100;
  4195. ne::move (p, { 0, 1 }, dt, dk);
  4196. CHECK (dt == -480 && dk == 127 - 67 && p.notes[0].start == 0 && p.notes[1].key == 127);
  4197. // duplicate: right after the selection, rounded to the grid
  4198. lynx::Pattern q;
  4199. q.notes = { { 0, 240, 0, 60 }, { 240, 300, 0, 62 } }; // 540-tick selection
  4200. const auto added = ne::duplicate (q, { 0, 1 }, 960);
  4201. CHECK (added.size() == 2 && q.notes[(std::size_t) added[0]].start == 960 && q.notes[(std::size_t) added[1]].start == 1200);
  4202. // paste onto another channel with an offset
  4203. const auto pasted = ne::paste (q, ne::copy (q, { 1 }), 100, 3);
  4204. CHECK (pasted.size() == 1 && q.notes.back().channel == 3 && q.notes.back().start == 340);
  4205. // quantize rounds to the nearest
  4206. q.notes[0].start = 130;
  4207. q.notes[1].start = 100;
  4208. ne::quantize (q, { 0, 1 }, 240);
  4209. CHECK (q.notes[0].start == 240 && q.notes[1].start == 0);
  4210. // remove several (order and duplicates don't matter)
  4211. const auto before = q.notes.size();
  4212. ne::remove (q, { 1, 0, 1 });
  4213. CHECK (q.notes.size() == before - 2);
  4214. }
  4215. TEST (note_edit_quick_operations)
  4216. {
  4217. namespace ne = lynx::noteedit;
  4218. const std::int64_t step = lynx::ticksPerStep, bar = lynx::ppq * 4;
  4219. // split: only strictly inside; the tail is appended and does not slide again
  4220. lynx::Pattern p;
  4221. p.notes = { { 0, 4 * step, 0, 60 } };
  4222. p.notes[0].slide = true;
  4223. CHECK (ne::split (p, 0, 0) == -1 && ne::split (p, 0, 4 * step) == -1);
  4224. CHECK (ne::split (p, 0, step) == 1 && p.notes[0].length == step && p.notes[1].start == step && p.notes[1].length == 3 * step
  4225. && ! p.notes[1].slide);
  4226. // chop to grid: a 2.5-step note from half a step becomes 3 pieces cut on the step lines
  4227. lynx::Pattern c;
  4228. c.notes = { { step / 2, step * 5 / 2, 0, 60 }, { 0, step, 1, 50 } };
  4229. const auto pieces = ne::chop (c, { 0, 1 }, step);
  4230. CHECK (pieces.size() == 4 && c.notes.size() == 4);
  4231. CHECK (c.notes[0].length == step / 2 && c.notes[2].start == step && c.notes[2].length == step && c.notes[3].start == 2 * step
  4232. && c.notes[3].length == step);
  4233. // join: touching and overlapping notes of the same key and channel merge; others stay
  4234. lynx::Pattern j;
  4235. j.notes = { { 0, step, 0, 60 }, { step, step, 0, 60 }, { step * 3 / 2, step * 2, 0, 60 }, { 5 * step, step, 0, 60 },
  4236. { step, step, 0, 62 }, { step, step, 1, 60 } };
  4237. CHECK (ne::join (j, { 0, 1, 2, 3, 4, 5 }) == 2 && j.notes.size() == 4);
  4238. CHECK (j.notes[0].start == 0 && j.notes[0].length == step * 7 / 2);
  4239. // legato: each note reaches the next start (a chord ends together); the last keeps its length
  4240. lynx::Pattern l;
  4241. l.notes = { { 0, 10, 0, 60 }, { 0, 10, 0, 64 }, { step, 10, 0, 62 }, { 3 * step, 10, 0, 65 }, { step, 10, 1, 40 } };
  4242. ne::legato (l, { 0, 1, 2, 3, 4 });
  4243. CHECK (l.notes[0].length == step && l.notes[1].length == step && l.notes[2].length == 2 * step && l.notes[3].length == 10
  4244. && l.notes[4].length == 10);
  4245. // shrink to grid and full quantise (start and end)
  4246. ne::shrinkToGrid (l, { 3 }, step / 2);
  4247. CHECK (l.notes[3].length == step / 2);
  4248. lynx::Pattern q;
  4249. q.notes = { { 10, step - 30, 0, 60 }, { step + 100, 20, 0, 60 } };
  4250. ne::quantizeFull (q, { 0, 1 }, step);
  4251. CHECK (q.notes[0].start == 0 && q.notes[0].length == step && q.notes[1].start == step && q.notes[1].length == step);
  4252. // rotate inside the bar, wrapping
  4253. lynx::Pattern r;
  4254. r.notes = { { 0, step, 0, 60 }, { bar - step, step, 0, 62 } };
  4255. ne::rotate (r, { 0, 1 }, step, bar, 1);
  4256. CHECK (r.notes[0].start == step && r.notes[1].start == 0);
  4257. ne::rotate (r, { 0, 1 }, step, bar, -2);
  4258. CHECK (r.notes[0].start == bar - step && r.notes[1].start == bar - 2 * step);
  4259. r.notes.push_back ({ bar + step, step, 0, 64 }); // alone in the second bar: wraps inside that bar
  4260. ne::rotate (r, { 2 }, step, bar, -2);
  4261. CHECK (r.notes[2].start == 2 * bar - step);
  4262. }
  4263. TEST (note_edit_grid_and_time_text)
  4264. {
  4265. namespace ne = lynx::noteedit;
  4266. const std::int64_t step = lynx::ticksPerStep, bar = lynx::ppq * 4;
  4267. CHECK (ne::snapDown (250.0, step) == step && ne::snapDown (-1.0, step) == -step && ne::snapDown (17.9, 1) == 17);
  4268. CHECK (ne::snapNearest (1000.0, { 400, 1100, 980 }, 50.0, step) == 980);
  4269. CHECK (ne::snapNearest (1000.0, { 400 }, 50.0, step) == 960); // too far: falls back to the grid
  4270. // screen grid: 24 px per step shows steps; zoomed out it climbs to beats, bars and bar multiples
  4271. CHECK (ne::screenStep (24.0 / step, bar) == step);
  4272. CHECK (ne::screenStep (96.0 / bar, bar) == lynx::ppq);
  4273. CHECK (ne::screenStep (8.0 / bar, bar) == 2 * bar);
  4274. CHECK (ne::screenStep (200.0 / step, bar) == step / 4);
  4275. CHECK (ne::formatTime (0, 4) == "1:1:0" && ne::formatTime (bar + 5 * step + 7, 4) == "2:6:7");
  4276. CHECK (ne::formatTime (step * 2, 4, true) == "0:2:0");
  4277. std::int64_t t = 0;
  4278. CHECK (ne::parseTime ("2:6:7", 4, t) && t == bar + 5 * step + 7);
  4279. CHECK (ne::parseTime ("3", 4, t) && t == 2 * bar);
  4280. CHECK (ne::parseTime ("0:2:0", 4, t, true) && t == 2 * step);
  4281. CHECK (! ne::parseTime ("0:1:0", 4, t) && ! ne::parseTime ("1:17:0", 4, t) && ! ne::parseTime ("1:1:240", 4, t)
  4282. && ! ne::parseTime ("a", 4, t) && ! ne::parseTime ("", 4, t));
  4283. CHECK (ne::parseTime ("1:12:0", 3, t) && ! ne::parseTime ("1:13:0", 3, t)); // 3/4: 12 steps per bar
  4284. }
  4285. TEST (playlist_clip_edges_and_time_selection)
  4286. {
  4287. namespace ne = lynx::noteedit;
  4288. const std::int64_t beat = lynx::ppq;
  4289. // left-edge trim: content stays in place, cannot reveal what is before the content's start
  4290. lynx::Clip c { 0, 0, 4 * beat, 4 * beat, beat };
  4291. ne::trimStart (c, 5 * beat, beat);
  4292. CHECK (c.start == 5 * beat && c.offset == 2 * beat && c.length == 3 * beat);
  4293. ne::trimStart (c, 0, beat);
  4294. CHECK (c.start == 3 * beat && c.offset == 0 && c.length == 5 * beat);
  4295. ne::trimStart (c, 100 * beat, beat);
  4296. CHECK (c.length == beat && c.end() == 8 * beat);
  4297. // slip moves the content, not the clip
  4298. lynx::Clip s { 0, 0, 0, 2 * beat, beat };
  4299. ne::slip (s, beat / 2);
  4300. CHECK (s.offset == beat / 2 && s.start == 0 && s.length == 2 * beat);
  4301. ne::slip (s, beat);
  4302. CHECK (s.offset == 0);
  4303. ne::slip (s, -3 * beat);
  4304. CHECK (s.offset == 3 * beat);
  4305. // insert / delete time across all tracks, markers included
  4306. std::vector<lynx::Clip> clips { { 0, 0, 0, 2 * beat, 0 }, { 0, 1, 4 * beat, 2 * beat, 0 }, { 0, 2, beat, 6 * beat, 0 } };
  4307. std::vector<lynx::Marker> markers { { 5 * beat, "A" }, { beat / 2, "B" } };
  4308. ne::insertTime (clips, markers, 2 * beat, 4 * beat);
  4309. CHECK (clips[0].start == 0 && clips[1].start == 8 * beat && clips[2].start == beat && markers[0].tick == 9 * beat
  4310. && markers[1].tick == beat / 2);
  4311. std::vector<lynx::Clip> d { { 0, 0, 0, 2 * beat, 0 }, // before: kept
  4312. { 0, 1, 3 * beat, beat, 0 }, // inside: deleted
  4313. { 0, 2, beat, 6 * beat, 0 }, // spans the cut: head + tail
  4314. { 0, 3, 3 * beat, 4 * beat, 0 }, // tail outside: trimmed and moved
  4315. { 0, 4, 8 * beat, beat, 0 } }; // after: moved left
  4316. std::vector<lynx::Marker> dm { { 3 * beat, "in" }, { 9 * beat, "after" } };
  4317. ne::deleteTime (d, dm, 2 * beat, 5 * beat);
  4318. CHECK (d.size() == 5);
  4319. CHECK (d[1].track == 2 && d[1].start == beat && d[1].length == beat);
  4320. CHECK (d[2].track == 2 && d[2].start == 2 * beat && d[2].offset == 4 * beat && d[2].length == 2 * beat);
  4321. CHECK (d[3].track == 3 && d[3].start == 2 * beat && d[3].offset == 2 * beat && d[3].length == 2 * beat);
  4322. CHECK (d[4].start == 5 * beat && dm.size() == 1 && dm[0].tick == 6 * beat);
  4323. // time selection: overlapping clips; Ctrl+B repeats what starts inside it after itself
  4324. std::vector<lynx::Clip> r { { 0, 0, 0, beat, 0 }, { 0, 1, beat, 2 * beat, 0 }, { 0, 0, 4 * beat, beat, 0 } };
  4325. CHECK ((ne::clipsInTime (r, beat / 2, 2 * beat) == std::vector<int> { 0, 1 }));
  4326. const auto added = ne::repeatTime (r, 0, 4 * beat, {});
  4327. CHECK (added.size() == 2 && r[3].start == 4 * beat && r[4].start == 5 * beat && r[4].track == 1);
  4328. CHECK (ne::repeatTime (r, 0, 2 * beat, { 2 }).size() == 1 && r.back().start == 6 * beat);
  4329. }
  4330. TEST (recorder_builds_notes_in_pattern_and_song_mode)
  4331. {
  4332. auto song = lynx::Song::makeDefault();
  4333. const auto loop = song.patterns[0].effectiveLength (4); // 1 bar = 3840
  4334. lynx::NoteRecorder r;
  4335. r.add (song, { 100.0, 4, 60, 0.5f, true }); // no take: ignored
  4336. CHECK (r.takeFinished().empty());
  4337. r.begin (loop);
  4338. r.add (song, { 4000.0, 4, 60, 0.5f, true }); // second loop pass
  4339. r.add (song, { 4480.0, 4, 60, 0.0f, false });
  4340. r.add (song, { 5000.0, 4, 64, 0.9f, true }); // held until stop
  4341. r.end (song, 5240.0);
  4342. const auto notes = r.takeFinished();
  4343. CHECK (notes.size() == 2);
  4344. CHECK (notes[0].start == 4000 - 3840 && notes[0].length == 480 && notes[0].velocity == 0.5f && notes[0].channel == 4);
  4345. CHECK (notes[1].key == 64 && notes[1].start == 5000 - 3840 && notes[1].length == 240);
  4346. // song mode: position inside the current pattern's clip (with offset)
  4347. song.mode = lynx::PlayMode::song;
  4348. song.playlist = { { 0, 0, 7680, 3840, 960 } };
  4349. CHECK (lynx::patternPositionForTick (song, 7680.0 + 100.0, loop) == 1060);
  4350. CHECK (lynx::patternPositionForTick (song, 100.0, loop) == 100); // outside the clip: like loop
  4351. }
  4352. TEST (automation_curve_and_playback)
  4353. {
  4354. lynx::Automation a;
  4355. a.points = { { 0, 0.0f }, { 960, 1.0f }, { 1920, 0.5f, 0.0f } };
  4356. CHECK (a.valueAt (-10.0) == 0.0f && a.valueAt (480.0) == 0.5f && a.valueAt (5000.0) == 0.5f);
  4357. CHECK (a.length() == 1920);
  4358. a.points[0].tension = 1.0f; // curve: starts slow
  4359. CHECK (a.valueAt (480.0) < 0.2f && a.valueAt (480.0) > 0.0f);
  4360. // automated Master: silence in the first bar, sound in the second, and the last value
  4361. // keeps applying after the automation clip ends
  4362. auto song = lynx::Song::makeDefault();
  4363. song.bpm = 120.0;
  4364. song.mode = lynx::PlayMode::song;
  4365. const auto bar = (std::int64_t) lynx::ppq * 4;
  4366. for (int b = 0; b < 3; ++b)
  4367. song.playlist.push_back ({ 0, 0, bar * b, bar, 0 });
  4368. lynx::Automation master;
  4369. master.target = { lynx::AutomationTargetKind::mixerVolume, 0 };
  4370. master.points = { { 0, 0.0f }, { bar - 1, 0.0f }, { bar, 0.8f } };
  4371. song.automations.push_back (master);
  4372. lynx::Clip ac;
  4373. ac.kind = lynx::ClipKind::automation;
  4374. ac.automation = 0;
  4375. ac.track = 1;
  4376. ac.length = bar + bar / 2;
  4377. song.playlist.push_back (ac);
  4378. const double sr = 48000.0;
  4379. auto state = lynx::RackState::build (song, nullptr, sr);
  4380. CHECK (state->hasAutomation && state->mixerVolumeAuto[0] == 0 && state->automationClips[0].size() == 1);
  4381. lynx::RackRenderer r;
  4382. r.prepare (sr, 512);
  4383. std::vector<float> l (512), rr (512);
  4384. const auto tps = lynx::ticksPerSample (song.bpm, sr);
  4385. float peakBar1 = 0.0f, peakBar2 = 0.0f, peakBar3 = 0.0f;
  4386. for (double tick = 0.0; tick < (double) (bar * 3); tick += 512 * tps)
  4387. {
  4388. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  4389. auto& peak = tick < (double) bar - 512 * tps ? peakBar1 : tick < (double) bar * 2 ? peakBar2 : peakBar3;
  4390. for (auto v : l)
  4391. peak = std::max (peak, std::abs (v));
  4392. }
  4393. CHECK (peakBar1 < 1.0e-6f);
  4394. CHECK (peakBar2 > 0.05f);
  4395. CHECK (peakBar3 > 0.05f); // after the clip: keeps the last value
  4396. // target removed: automation disappears from the tables
  4397. song.removeChannel (0);
  4398. song.automations[0].target = { lynx::AutomationTargetKind::channelVolume, 0 };
  4399. song.removeChannel (0);
  4400. CHECK (song.automations[0].target.kind == lynx::AutomationTargetKind::none);
  4401. song.removeAutomation (0);
  4402. CHECK (song.automations.empty() && song.playlist.size() == 3);
  4403. }
  4404. TEST (mixer_sends_between_inserts)
  4405. {
  4406. // insert 1 goes only to 3; 3 goes to the Master: same sum (0.5 + 0.5)
  4407. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.setRoute (1, 3, true); s.setRoute (1, 0, false); }) - 1.0f) < 1e-4f);
  4408. // send at 50% and muted destination
  4409. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.setRoute (1, 3, true, 0.5f); s.setRoute (1, 0, false); }) - 0.75f) < 1e-4f);
  4410. 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);
  4411. // going to the Master and to 3: the signal doubles
  4412. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.setRoute (1, 3, true); }) - 1.5f) < 1e-4f);
  4413. // solo on 3 lets what reaches it sound (insert 1), but not 2
  4414. 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);
  4415. // solo on 1 lets the path to the Master (3) sound
  4416. 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);
  4417. // order and cycles
  4418. auto song = lynx::Song::makeDefault();
  4419. song.setRoute (4, 2, true);
  4420. song.setRoute (2, 1, true);
  4421. CHECK (! song.canRoute (1, 4) && ! song.canRoute (1, 2) && song.canRoute (1, 5));
  4422. song.setRoute (1, 4, true); // ignored: would create a cycle
  4423. CHECK (! song.routes (1, 4));
  4424. const auto order = song.mixerOrder();
  4425. const auto pos = [&] (int t) { return std::find (order.begin(), order.end(), t) - order.begin(); };
  4426. CHECK ((int) order.size() == (int) song.mixer.size() - 1);
  4427. CHECK (pos (4) < pos (2) && pos (2) < pos (1));
  4428. song.setRoute (4, 2, false);
  4429. CHECK (! song.routes (4, 2) && song.routes (4, 0));
  4430. }
  4431. namespace
  4432. {
  4433. // Test effect: delays the signal by `latency` samples and reports that latency (like a lookahead plugin).
  4434. class LatentEffect final : public lynx::Effect
  4435. {
  4436. public:
  4437. explicit LatentEffect (int samples) : latency (samples), line (samples) {}
  4438. void prepare (double) noexcept override {}
  4439. void reset() noexcept override {}
  4440. void process (float* l, float* r, int n, const lynx::EffectParams&, double) noexcept override { line.process (l, r, n); }
  4441. int latencySamples() const noexcept override { return latency; }
  4442. private:
  4443. int latency;
  4444. lynx::DelayLine line;
  4445. };
  4446. } // namespace
  4447. TEST (delay_line_mix_into_matches_process_and_drains_tail)
  4448. {
  4449. // mixInto = process over source*level, added to the destination; in stretches that cross the ring
  4450. lynx::DelayLine a (37), b (37);
  4451. std::vector<float> sl (300), sr (300), dl (300, 0.5f), dr (300, -0.5f), xl (300), xr (300);
  4452. for (int i = 0; i < 300; ++i)
  4453. {
  4454. sl[(std::size_t) i] = (float) std::sin (i * 0.1);
  4455. sr[(std::size_t) i] = (float) std::cos (i * 0.07);
  4456. }
  4457. for (int start = 0; start < 300; start += 100)
  4458. {
  4459. b.mixInto (sl.data() + start, sr.data() + start, 0.25f, dl.data() + start, dr.data() + start, 100);
  4460. for (int i = start; i < start + 100; ++i)
  4461. {
  4462. xl[(std::size_t) i] = sl[(std::size_t) i] * 0.25f;
  4463. xr[(std::size_t) i] = sr[(std::size_t) i] * 0.25f;
  4464. }
  4465. a.process (xl.data() + start, xr.data() + start, 100);
  4466. }
  4467. float worst = 0.0f;
  4468. for (int i = 0; i < 300; ++i)
  4469. worst = std::max ({ worst, std::abs (dl[(std::size_t) i] - 0.5f - xl[(std::size_t) i]),
  4470. std::abs (dr[(std::size_t) i] + 0.5f - xr[(std::size_t) i]) });
  4471. CHECK (worst < 1e-6f);
  4472. // silenced source: the tail still in the line comes out, then nothing remains
  4473. std::vector<float> tl (64, 0.0f), tr (64, 0.0f);
  4474. b.mixInto (nullptr, nullptr, 1.0f, tl.data(), tr.data(), 64);
  4475. CHECK (std::abs (tl[0] - sl[263] * 0.25f) < 1e-6f);
  4476. std::vector<float> zl (64, 0.0f), zr (64, 0.0f);
  4477. b.mixInto (nullptr, nullptr, 1.0f, zl.data(), zr.data(), 64);
  4478. CHECK (*std::max_element (zl.begin(), zl.end()) == 0.0f && *std::min_element (zl.begin(), zl.end()) == 0.0f);
  4479. }
  4480. TEST (limiter_lookahead_holds_ceiling_on_bursts)
  4481. {
  4482. // short bursts over a low floor: the sliding maximum must see each peak before it leaves
  4483. auto fx = lynx::makeEffect (lynx::EffectKind::limiter);
  4484. fx->prepare (48000.0);
  4485. auto slot = lynx::makeEffectSlot (lynx::EffectKind::limiter);
  4486. slot.params[2] = 0.0f; // 10 ms release
  4487. std::vector<float> l (48000), r (48000);
  4488. std::uint32_t seed = 1;
  4489. for (int i = 0; i < 48000; ++i)
  4490. {
  4491. seed = seed * 1664525u + 1013904223u;
  4492. const auto noise = ((float) (seed >> 8) / 16777216.0f - 0.5f) * 0.1f;
  4493. l[(std::size_t) i] = noise + ((i % 9973) < 3 ? 3.0f : 0.0f);
  4494. r[(std::size_t) i] = -noise;
  4495. }
  4496. for (int i = 0; i < 48000; i += 333)
  4497. fx->process (l.data() + i, r.data() + i, std::min (333, 48000 - i), slot.params, 120.0);
  4498. float peak = 0.0f, quiet = 0.0f;
  4499. for (int i = 0; i < 48000; ++i)
  4500. peak = std::max (peak, std::abs (l[(std::size_t) i]));
  4501. for (int i = 9973 * 2 + 2000; i < 9973 * 2 + 2400; ++i) // 40 ms after the burst the gain has already recovered
  4502. quiet = std::max (quiet, std::abs (r[(std::size_t) i]));
  4503. const auto ceiling = 1.0f; // the default ceiling is at 0 dBFS or below
  4504. CHECK (peak <= ceiling + 1e-5f);
  4505. CHECK (quiet > 0.03f);
  4506. }
  4507. TEST (pdc_aligns_paths_through_latent_effects)
  4508. {
  4509. // plan: channel 0 -> insert 1 (effect of 100) -> insert 3; channel 1 -> insert 2 -> Master
  4510. lynx::Song s;
  4511. s.mixer = lynx::Song::defaultMixer (4);
  4512. s.channels.resize (3);
  4513. s.channels[0].mixerTrack = 1;
  4514. s.channels[1].mixerTrack = 2;
  4515. s.channels[2].mixerTrack = 3;
  4516. s.mixer[1].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::eq));
  4517. s.setRoute (1, 3, true);
  4518. s.setRoute (1, 0, false);
  4519. auto plan = lynx::computePdc (s, { 0, 0, 30 }, { {}, { 100 } });
  4520. CHECK (plan.inAlign[1] == 0 && plan.outLat[1] == 100);
  4521. CHECK (plan.inAlign[3] == 100 && plan.channelDelay[2] == 70); // instrument of 30 on insert 3
  4522. CHECK (plan.sendDelay[1][1] == 0); // the slowest path isn't delayed
  4523. CHECK (plan.sendDelay[2][0] == 100 && plan.sendDelay[3][0] == 0 && plan.total == 100);
  4524. // bypass zeroes the slot's latency
  4525. s.mixer[1].effects[0].enabled = false;
  4526. CHECK (lynx::computePdc (s, { 0, 0, 30 }, { {}, { 100 } }).total == 30);
  4527. // rendered: two impulses played together come out together
  4528. lynx::Song r;
  4529. r.mixer = lynx::Song::defaultMixer (2);
  4530. for (int i = 0; i < 2; ++i)
  4531. {
  4532. lynx::ChannelDef c;
  4533. c.instrument = lynx::InstrumentKind::sampler;
  4534. c.samplePath = "impulso";
  4535. c.volume = 1.0f;
  4536. c.mixerTrack = i + 1;
  4537. r.channels.push_back (c);
  4538. }
  4539. r.patterns.emplace_back();
  4540. lynx::EffectSlot fx;
  4541. fx.kind = lynx::EffectKind::plugin;
  4542. fx.plugin.id = "latente";
  4543. r.mixer[1].effects.push_back (fx);
  4544. lynx::RackState::Factories f;
  4545. f.samples = [] (const std::string&) {
  4546. auto d = std::make_shared<lynx::SampleData>();
  4547. d->left.assign (64, 0.0f);
  4548. d->left[0] = 1.0f;
  4549. return std::shared_ptr<const lynx::SampleData> (d);
  4550. };
  4551. f.pluginEffects = [] (const lynx::PluginRef&, double) -> std::shared_ptr<lynx::Effect> { return std::make_shared<LatentEffect> (100); };
  4552. auto state = lynx::RackState::build (r, nullptr, 48000.0, f);
  4553. CHECK (state->pdc.total == 100 && state->sendDelays[2][0] != nullptr && state->sendDelays[2][0]->length() == 100);
  4554. lynx::RackRenderer renderer;
  4555. renderer.prepare (48000.0, 256);
  4556. lynx::LiveEventQueue live;
  4557. for (int ch = 0; ch < 2; ++ch)
  4558. {
  4559. lynx::NoteEvent on;
  4560. on.channel = ch;
  4561. on.key = 60;
  4562. on.velocity = 1.0f;
  4563. live.push (on);
  4564. }
  4565. std::vector<float> l (256), rr (256);
  4566. renderer.process (state.get(), false, 0.0, 256, l.data(), rr.data(), &live);
  4567. int first = -1;
  4568. for (int i = 0; i < 256 && first < 0; ++i)
  4569. if (std::abs (l[(std::size_t) i]) > 0.01f)
  4570. first = i;
  4571. CHECK (first == 100 && std::abs (l[100] - 2.0f) < 1e-3f); // both impulses arrive together, summed
  4572. // editing without changing latencies reuses the lines
  4573. r.channels[0].volume = 0.5f;
  4574. auto next = lynx::RackState::build (r, state.get(), 48000.0, f);
  4575. CHECK (next->sendDelays[2][0] == state->sendDelays[2][0]);
  4576. }
  4577. TEST (mixer_mute_fades_instead_of_clicking)
  4578. {
  4579. lynx::Song s;
  4580. s.mixer = lynx::Song::defaultMixer (2);
  4581. lynx::ChannelDef c;
  4582. c.instrument = lynx::InstrumentKind::sampler;
  4583. c.samplePath = "dc";
  4584. c.volume = 1.0f;
  4585. c.mixerTrack = 1;
  4586. s.channels.push_back (c);
  4587. s.patterns.emplace_back();
  4588. auto provider = [] (const std::string&) {
  4589. auto d = std::make_shared<lynx::SampleData>();
  4590. d->left.assign (48000, 0.5f);
  4591. return std::shared_ptr<const lynx::SampleData> (d);
  4592. };
  4593. auto on = lynx::RackState::build (s, nullptr, 48000.0, provider);
  4594. s.mixer[1].muted = true;
  4595. auto off = lynx::RackState::build (s, on.get(), 48000.0, provider);
  4596. lynx::RackRenderer r;
  4597. r.prepare (48000.0, 256);
  4598. lynx::LiveEventQueue live;
  4599. lynx::NoteEvent note;
  4600. note.velocity = 1.0f;
  4601. live.push (note);
  4602. std::vector<float> l (256), rr (256);
  4603. r.process (on.get(), false, 0.0, 256, l.data(), rr.data(), &live);
  4604. CHECK (std::abs (l[255] - 0.5f) < 1e-3f);
  4605. r.process (off.get(), false, 0.0, 256, l.data(), rr.data());
  4606. CHECK (l[0] > 0.45f && l[128] > 0.2f && l[128] < 0.3f && std::abs (l[255]) < 1e-6f); // ramp down to zero
  4607. r.process (off.get(), false, 0.0, 256, l.data(), rr.data());
  4608. CHECK (std::abs (l[0]) < 1e-9f);
  4609. }
  4610. TEST (dither_quantizes_to_lsb_grid_with_small_error)
  4611. {
  4612. lynx::Dither d (16);
  4613. std::vector<float> x (4800);
  4614. for (std::size_t i = 0; i < x.size(); ++i)
  4615. x[i] = 0.25f * std::sin ((float) i * 0.05f);
  4616. auto y = x;
  4617. d.process (y.data(), (int) y.size(), 0);
  4618. double errorSum = 0.0;
  4619. bool onGrid = true;
  4620. for (std::size_t i = 0; i < x.size(); ++i)
  4621. {
  4622. const auto steps = y[i] * 32768.0f;
  4623. onGrid = onGrid && std::abs (steps - std::round (steps)) < 1e-3f;
  4624. errorSum += std::abs (y[i] - x[i]);
  4625. }
  4626. CHECK (onGrid);
  4627. CHECK (errorSum / (double) x.size() < 4.0 / 32768.0); // a few LSBs of noise on average
  4628. }
  4629. TEST (parallel_rendering_matches_sequential)
  4630. {
  4631. // 32 channels (worth parallelizing), inserts with chained sends
  4632. auto song = lynx::Song::makeDefault();
  4633. song.mixer = lynx::Song::defaultMixer (16);
  4634. for (int i = 5; i < 32; ++i)
  4635. {
  4636. lynx::ChannelDef c;
  4637. c.instrument = i % 2 ? lynx::InstrumentKind::synth : lynx::InstrumentKind::hat;
  4638. c.mixerTrack = 1 + i % 12;
  4639. c.volume = 0.3f;
  4640. song.channels.push_back (c);
  4641. for (int step = i % 4; step < 16; step += 3)
  4642. song.patterns[0].notes.push_back ({ (std::int64_t) step * lynx::ticksPerStep, 200, i, 48 + i, 0.7f });
  4643. }
  4644. song.patterns[0].sortNotes();
  4645. song.setRoute (3, 7, true, 0.6f);
  4646. song.setRoute (7, 9, true, 0.5f);
  4647. song.setRoute (2, 9, true);
  4648. song.mixer[9].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::reverb));
  4649. auto seqState = lynx::RackState::build (song, nullptr, 48000.0);
  4650. auto parState = lynx::RackState::build (song, nullptr, 48000.0);
  4651. CHECK (parState->parallel && parState->mixerLayers.size() == 3);
  4652. lynx::WorkerPool pool (3);
  4653. lynx::RackRenderer seq, par;
  4654. seq.prepare (48000.0, 512);
  4655. par.prepare (48000.0, 512);
  4656. par.setWorkerPool (&pool);
  4657. std::vector<float> l1 (512), r1 (512), l2 (512), r2 (512);
  4658. const auto tps = lynx::ticksPerSample (song.bpm, 48000.0);
  4659. bool same = true;
  4660. float peak = 0.0f;
  4661. for (int block = 0; block < 200; ++block)
  4662. {
  4663. seq.process (seqState.get(), true, block * 512 * tps, 512, l1.data(), r1.data());
  4664. par.process (parState.get(), true, block * 512 * tps, 512, l2.data(), r2.data());
  4665. same = same && l1 == l2 && r1 == r2;
  4666. for (auto v : l1)
  4667. peak = std::max (peak, std::abs (v));
  4668. }
  4669. CHECK (same);
  4670. CHECK (peak > 0.05f);
  4671. // the pool handles many small rounds in a row
  4672. std::atomic<int> sum { 0 };
  4673. auto add = [&sum] (int i) { sum.fetch_add (i + 1); };
  4674. for (int round = 0; round < 2000; ++round)
  4675. pool.parallelFor (5, add);
  4676. CHECK (sum.load() == 2000 * 15);
  4677. }
  4678. TEST (arrangements_switch_and_keep_clip_indices)
  4679. {
  4680. auto s = lynx::Song::makeDefault();
  4681. s.patterns.emplace_back().name = "B";
  4682. s.playlist = { { 1, 0, 0, 960, 0 } };
  4683. s.loopEnd = 960;
  4684. CHECK (s.arrangements.size() == 1 && s.currentArrangement == 0);
  4685. const auto second = s.addArrangement ("Versão curta", true);
  4686. CHECK (second == 1 && s.currentArrangement == 1 && s.playlist.size() == 1 && s.loopEnd == 960);
  4687. s.playlist.push_back ({ 0, 1, 960, 960, 0 });
  4688. s.loopEnd = 0;
  4689. s.selectArrangement (0);
  4690. CHECK (s.playlist.size() == 1 && s.loopEnd == 960 && s.arrangements[1].clips.size() == 2);
  4691. // removing pattern 0 fixes the clips stored in the other arrangement
  4692. s.removePattern (0);
  4693. CHECK (s.playlist.size() == 1 && s.playlist[0].pattern == 0);
  4694. CHECK (s.arrangements[1].clips.size() == 1 && s.arrangements[1].clips[0].pattern == 0);
  4695. s.removeArrangement (0);
  4696. CHECK (s.arrangements.size() == 1 && s.currentArrangement == 0 && s.playlist.size() == 1 && s.loopEnd == 0);
  4697. }
  4698. namespace
  4699. {
  4700. // Test effect that inverts the signal and counts how many times it processed.
  4701. class CountingInverter final : public lynx::Effect
  4702. {
  4703. public:
  4704. void prepare (double) noexcept override {}
  4705. void reset() noexcept override {}
  4706. void process (float* l, float* r, int n, const lynx::EffectParams&, double) noexcept override
  4707. {
  4708. ++calls;
  4709. for (int i = 0; i < n; ++i)
  4710. {
  4711. l[i] = -l[i];
  4712. r[i] = -r[i];
  4713. }
  4714. }
  4715. int calls = 0;
  4716. };
  4717. } // namespace
  4718. TEST (keyboard_controller_outputs_key_value)
  4719. {
  4720. std::vector<std::uint8_t> blob (554, 0);
  4721. const std::uint32_t version = 3;
  4722. std::memcpy (blob.data(), &version, 4);
  4723. const float key60 = 0.7f, off = 0.2f;
  4724. std::memcpy (blob.data() + 12 + 60 * 4, &key60, 4);
  4725. std::memcpy (blob.data() + 548, &off, 4);
  4726. blob[552] = 1; // auto release
  4727. lynx::ChannelDef c;
  4728. c.instrument = lynx::InstrumentKind::controller;
  4729. c.controllerState = blob;
  4730. auto inst = lynx::makeInstrument (lynx::InstrumentKind::controller);
  4731. inst->prepare (48000.0);
  4732. inst->configure (c, 120.0);
  4733. std::vector<float> l (64), r (64);
  4734. inst->noteOn (60, 1.0f);
  4735. inst->render (l.data(), r.data(), 64, 1.0f, 1.0f);
  4736. CHECK (std::abs (inst->controlOutput (0) - 0.7f) < 1e-6f);
  4737. inst->noteOff (60);
  4738. CHECK (std::abs (inst->controlOutput (0) - 0.2f) < 1e-6f);
  4739. }
  4740. TEST (envelope_controller_ducks_on_each_note)
  4741. {
  4742. // a unipolar articulator like "Basic Sidechain": base 0.05, drops to 0 on the note and rises to 1 in 0.3 s
  4743. lynx::ControllerArticulator a;
  4744. a.envOn = true;
  4745. a.base = 0.05f;
  4746. a.envLevel = 1.0f;
  4747. a.count = 3;
  4748. a.time = { 0.0f, 0.2f, 0.3f };
  4749. a.level = { 0.0f, 0.75f, 1.0f };
  4750. CHECK (std::abs (lynx::controllerValue (a, 0.0, -1.0) - 0.05f) < 1e-4f);
  4751. CHECK (std::abs (lynx::controllerValue (a, 0.1, -1.0) - 0.425f) < 1e-3f);
  4752. CHECK (lynx::controllerValue (a, 1.0, -1.0) > 0.99f); // no sustain: runs to the end and stays
  4753. a.sustain = 1;
  4754. CHECK (std::abs (lynx::controllerValue (a, 1.0, -1.0) - 0.8f) < 1e-3f); // held at sustain
  4755. CHECK (lynx::controllerValue (a, 1.0, 0.5) > 0.99f); // released: continues to the end
  4756. a.bipolar = true;
  4757. a.base = 0.5f;
  4758. a.sustain = -1;
  4759. CHECK (std::abs (lynx::controllerValue (a, 0.0, -1.0) - 0.0f) < 1e-4f); // bipolar: 0.5 + (0 − 0.5)
  4760. }
  4761. TEST (peak_controller_drives_live_automation)
  4762. {
  4763. // track 1: source (DC 0.5) with Peak Controller; track 2: DC 0.5 with volume linked to the controller
  4764. auto render = [] (bool linked) {
  4765. lynx::Song s;
  4766. s.mixer = lynx::Song::defaultMixer (2);
  4767. for (int t = 1; t <= 2; ++t)
  4768. {
  4769. lynx::ChannelDef c;
  4770. c.instrument = lynx::InstrumentKind::sampler;
  4771. c.samplePath = "dc";
  4772. c.volume = 1.0f;
  4773. c.mixerTrack = t;
  4774. s.channels.push_back (c);
  4775. }
  4776. s.patterns.emplace_back();
  4777. auto peak = lynx::makeEffectSlot (lynx::EffectKind::peakController);
  4778. peak.params[0] = 1.0f; // base 1
  4779. peak.params[1] = 0.0f; // amount −1: 1 − peak
  4780. peak.params[2] = 0.5f; // no tension
  4781. s.mixer[1].effects.push_back (peak);
  4782. if (linked)
  4783. {
  4784. lynx::Automation a;
  4785. a.name = "Peak";
  4786. a.target = { lynx::AutomationTargetKind::mixerVolume, 2 };
  4787. a.source = { 1, 0, 0 };
  4788. s.automations.push_back (a);
  4789. }
  4790. lynx::RackState::Factories f;
  4791. f.samples = [] (const std::string&) {
  4792. auto d = std::make_shared<lynx::SampleData>();
  4793. d->left.assign (48000, 0.5f);
  4794. return std::shared_ptr<const lynx::SampleData> (d);
  4795. };
  4796. auto state = lynx::RackState::build (s, nullptr, 48000.0, f);
  4797. lynx::RackRenderer r;
  4798. r.prepare (48000.0, 512);
  4799. lynx::LiveEventQueue live;
  4800. for (int ch = 0; ch < 2; ++ch)
  4801. {
  4802. lynx::NoteEvent on;
  4803. on.channel = ch;
  4804. on.velocity = 1.0f;
  4805. live.push (on);
  4806. }
  4807. std::vector<float> l (4800), rr (4800);
  4808. r.process (state.get(), false, 0.0, 4800, l.data(), rr.data(), &live);
  4809. return l[4000];
  4810. };
  4811. const auto free = render (false), ducked = render (true);
  4812. CHECK (std::isfinite (ducked) && ducked > 0.0f);
  4813. CHECK (ducked < free - 0.05f); // the 0.5 peak on track 1 lowers track 2's volume
  4814. }
  4815. TEST (effect_slot_dry_wet_and_smart_disable)
  4816. {
  4817. lynx::Song s;
  4818. s.mixer = lynx::Song::defaultMixer (1);
  4819. lynx::ChannelDef c;
  4820. c.instrument = lynx::InstrumentKind::sampler;
  4821. c.samplePath = "dc";
  4822. c.volume = 1.0f;
  4823. c.mixerTrack = 1;
  4824. s.channels.push_back (c);
  4825. s.patterns.emplace_back();
  4826. lynx::EffectSlot fx;
  4827. fx.kind = lynx::EffectKind::plugin;
  4828. fx.plugin.id = "inversor";
  4829. fx.mix = 0.25f; // 75% dry (+0.5) and 25% wet (-0.5) = +0.25
  4830. s.mixer[1].effects.push_back (fx);
  4831. auto inverter = std::make_shared<CountingInverter>();
  4832. lynx::RackState::Factories f;
  4833. f.samples = [] (const std::string&) {
  4834. auto d = std::make_shared<lynx::SampleData>();
  4835. d->left.assign (4800, 0.5f);
  4836. return std::shared_ptr<const lynx::SampleData> (d);
  4837. };
  4838. f.pluginEffects = [inverter] (const lynx::PluginRef&, double) -> std::shared_ptr<lynx::Effect> { return inverter; };
  4839. auto state = lynx::RackState::build (s, nullptr, 48000.0, f);
  4840. lynx::RackRenderer r;
  4841. r.prepare (48000.0, 512);
  4842. lynx::LiveEventQueue live;
  4843. lynx::NoteEvent on;
  4844. on.velocity = 1.0f;
  4845. live.push (on);
  4846. std::vector<float> l (480), rr (480);
  4847. r.process (state.get(), false, 0.0, 480, l.data(), rr.data(), &live);
  4848. CHECK (std::abs (l[100] - 0.25f) < 1e-4f);
  4849. // after the sample ends: 4 s of silence and the plugin stops being called
  4850. for (int i = 0; i < 100 * 6; ++i) // 6 s
  4851. r.process (state.get(), false, 0.0, 480, l.data(), rr.data());
  4852. const auto callsAfterSilence = inverter->calls;
  4853. for (int i = 0; i < 100; ++i)
  4854. r.process (state.get(), false, 0.0, 480, l.data(), rr.data());
  4855. CHECK (inverter->calls == callsAfterSilence);
  4856. CHECK (callsAfterSilence < 100 * 6);
  4857. // sound again: processing resumes in the same block
  4858. live.push (on);
  4859. r.process (state.get(), false, 0.0, 480, l.data(), rr.data(), &live);
  4860. CHECK (inverter->calls == callsAfterSilence + 1 && std::abs (l[100] - 0.25f) < 1e-4f);
  4861. }
  4862. TEST (swing_delays_offbeat_sixteenths)
  4863. {
  4864. CHECK (lynx::swingDelay (0, 1.0f) == 0);
  4865. CHECK (lynx::swingDelay (lynx::ticksPerStep, 1.0f) == lynx::ticksPerStep / 2);
  4866. CHECK (lynx::swingDelay (lynx::ticksPerStep * 2, 1.0f) == 0);
  4867. CHECK (lynx::swingDelay (lynx::ticksPerStep + 10, 1.0f) == (lynx::ticksPerStep - 20) / 2); // off the grid: triangular weight (FL)
  4868. CHECK (lynx::swingDelay (lynx::ticksPerStep, 1.0f, 0.5f) == lynx::ticksPerStep / 4); // channel mix
  4869. CHECK (lynx::swingDelay (lynx::ticksPerStep * 3, 0.5f) == lynx::ticksPerStep / 4);
  4870. lynx::Pattern p;
  4871. p.notes = { { 0, 100, 0, 60 }, { lynx::ticksPerStep, 100, 0, 62 } };
  4872. lynx::EventBuffer ev;
  4873. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  4874. lynx::schedulePatternLoop (p, p.effectiveLength (4), 0.0, 12000, tps, ev, 1.0f);
  4875. int onOffset = -1;
  4876. for (auto& e : ev)
  4877. if (e.type == lynx::NoteEvent::Type::on && e.key == 62)
  4878. onOffset = e.sampleOffset;
  4879. CHECK (onOffset == (int) std::floor ((lynx::ticksPerStep * 1.5) / tps));
  4880. }
  4881. TEST (sampler_settings_pitch_reverse_envelope_cut)
  4882. {
  4883. auto ramp = std::make_shared<lynx::SampleData>();
  4884. for (int i = 0; i < 48000; ++i)
  4885. ramp->left.push_back ((float) i / 48000.0f); // ramp: the value tells where the read position is
  4886. ramp->sampleRate = 48000.0;
  4887. auto play = [&] (const lynx::SamplerSettings& s, int key, int frames, bool releaseAfterHalf) {
  4888. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  4889. inst->prepare (48000.0);
  4890. lynx::ChannelDef c;
  4891. c.sampler = s;
  4892. inst->configure (c, 120.0);
  4893. inst->noteOn (key, 1.0f);
  4894. std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
  4895. inst->render (l.data(), r.data(), frames / 2, 1.0f, 1.0f);
  4896. if (releaseAfterHalf)
  4897. inst->noteOff (key);
  4898. inst->render (l.data() + frames / 2, r.data() + frames / 2, frames - frames / 2, 1.0f, 1.0f);
  4899. return l;
  4900. };
  4901. // +12 semitones: reads twice as fast
  4902. lynx::SamplerSettings up;
  4903. up.pitch = 12.0f;
  4904. CHECK (std::abs (play (up, 60, 1000, false)[999] - 2.0f * 999.0f / 48000.0f) < 1e-3f);
  4905. // reversed: starts from the end
  4906. lynx::SamplerSettings back;
  4907. back.reverse = true;
  4908. CHECK (play (back, 60, 100, false)[10] > 0.99f);
  4909. // 10 ms attack: starts silent and reaches full level
  4910. lynx::SamplerSettings slow;
  4911. slow.attack = 0.01f;
  4912. const auto a = play (slow, 60, 2000, false);
  4913. CHECK (a[10] < 0.2f * (10.0f / 48000.0f) + 1e-6f && std::abs (a[1500] - 1500.0f / 48000.0f) < 1e-4f);
  4914. // 5 ms release: gone right after release; without release it plays in full
  4915. lynx::SamplerSettings rel;
  4916. rel.release = 0.005f;
  4917. CHECK (play (rel, 60, 2000, true)[1999] == 0.0f);
  4918. CHECK (play (lynx::SamplerSettings {}, 60, 2000, true)[1999] > 0.0f);
  4919. // cut itself: the second note cuts the first (one voice remains)
  4920. lynx::SamplerSettings cut;
  4921. cut.cut = true;
  4922. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  4923. inst->prepare (48000.0);
  4924. lynx::ChannelDef cutChannel;
  4925. cutChannel.sampler = cut;
  4926. inst->configure (cutChannel, 120.0);
  4927. inst->noteOn (60, 1.0f);
  4928. std::vector<float> l (2000), r (2000);
  4929. inst->render (l.data(), r.data(), 1000, 1.0f, 1.0f);
  4930. inst->noteOn (60, 1.0f);
  4931. std::fill (l.begin(), l.end(), 0.0f);
  4932. inst->render (l.data(), r.data(), 2000, 1.0f, 1.0f);
  4933. CHECK (std::abs (l[1999] - 1999.0f / 48000.0f) < 1e-4f); // only the new note
  4934. }
  4935. TEST (playlist_split_and_track_mute)
  4936. {
  4937. lynx::Clip c { 0, 3, 960, 3840, 480 };
  4938. const auto rest = lynx::splitClip (c, 2000);
  4939. CHECK (c.start == 960 && c.length == 1040 && c.offset == 480);
  4940. CHECK (rest.start == 2000 && rest.length == 2800 && rest.offset == 480 + 1040 && rest.track == 3);
  4941. auto s = lynx::Song::makeDefault();
  4942. s.mode = lynx::PlayMode::song;
  4943. s.playlist = { { 0, 0, 0, 3840, 0 } };
  4944. lynx::EventBuffer ev;
  4945. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  4946. lynx::scheduleSong (s, 0.0, 48000, tps, ev);
  4947. CHECK (ev.size() > 0);
  4948. s.playlistTracks.resize (1);
  4949. s.playlistTracks[0].muted = true;
  4950. ev.clear();
  4951. lynx::scheduleSong (s, 0.0, 48000, tps, ev);
  4952. CHECK (ev.size() == 0);
  4953. }
  4954. TEST (note_pan_pitch_and_mute)
  4955. {
  4956. lynx::Pattern p;
  4957. lynx::Note a { 0, 240, 0, 60 };
  4958. a.pan = -1.0f;
  4959. a.finePitch = 50.0f;
  4960. lynx::Note b { 240, 240, 0, 62 };
  4961. b.muted = true;
  4962. p.notes = { a, b };
  4963. lynx::EventBuffer ev;
  4964. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  4965. lynx::schedulePatternLoop (p, p.effectiveLength (4), 0.0, 48000, tps, ev);
  4966. int ons = 0;
  4967. for (auto& e : ev)
  4968. if (e.type == lynx::NoteEvent::Type::on)
  4969. {
  4970. ++ons;
  4971. CHECK (e.key == 60 && e.pan == -1.0f && e.cents == 50.0f);
  4972. }
  4973. CHECK (ons == 1); // the muted one doesn't play
  4974. // note pan in the sampler: fully left
  4975. auto dc = std::make_shared<lynx::SampleData>();
  4976. dc->left.assign (4800, 0.5f);
  4977. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, dc);
  4978. inst->prepare (48000.0);
  4979. inst->setNextNoteExpression (-1.0f, 0.0f);
  4980. inst->noteOn (60, 1.0f);
  4981. std::vector<float> l (100), r (100);
  4982. inst->render (l.data(), r.data(), 100, 1.0f, 1.0f);
  4983. CHECK (l[50] > 0.6f && std::abs (r[50]) < 1e-6f);
  4984. }
  4985. TEST (tempo_automation_changes_speed_and_duration)
  4986. {
  4987. auto song = lynx::Song::makeDefault();
  4988. song.bpm = 120.0;
  4989. song.mode = lynx::PlayMode::song;
  4990. const auto bar = (std::int64_t) lynx::ppq * 4;
  4991. song.playlist = { { 0, 0, 0, bar, 0 }, { 0, 0, bar, bar, 0 } };
  4992. lynx::Automation tempo;
  4993. tempo.target = { lynx::AutomationTargetKind::tempo, 0 };
  4994. const auto v120 = lynx::valueFromTempo (120.0), v240 = lynx::valueFromTempo (240.0);
  4995. tempo.points = { { 0, v120 }, { bar - 1, v120 }, { bar, v240 } }; // step: 120 -> 240 at bar 2
  4996. song.automations.push_back (tempo);
  4997. lynx::Clip clip;
  4998. clip.kind = lynx::ClipKind::automation;
  4999. clip.automation = 0;
  5000. clip.track = 1;
  5001. clip.length = bar * 2;
  5002. song.playlist.push_back (clip);
  5003. CHECK (std::abs (song.tempoAt (100.0) - 120.0) < 0.01 && std::abs (song.tempoAt ((double) bar + 100.0) - 240.0) < 0.01);
  5004. // 1 bar at 120 (2 s) + 1 at 240 (1 s)
  5005. CHECK (std::abs (song.secondsForTicks (bar * 2) - 3.0) < 0.01);
  5006. auto state = lynx::RackState::build (song, nullptr, 48000.0);
  5007. CHECK (state->tempoAutomation == 0);
  5008. lynx::RackRenderer r;
  5009. r.prepare (48000.0, 512);
  5010. std::vector<float> l (512), rr (512);
  5011. r.process (state.get(), true, 10.0, 512, l.data(), rr.data());
  5012. CHECK (std::abs (r.currentBpm() - 120.0) < 0.01);
  5013. r.process (state.get(), true, (double) bar + 10.0, 512, l.data(), rr.data());
  5014. CHECK (std::abs (r.currentBpm() - 240.0) < 0.01);
  5015. }
  5016. TEST (bounce_insert_song_and_apply)
  5017. {
  5018. auto s = lynx::Song::makeDefault();
  5019. s.setRoute (3, 7, true, 0.5f);
  5020. s.mixer[0].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::reverb));
  5021. s.mixer[2].solo = true;
  5022. const auto b = lynx::makeBounceSong (s, 3);
  5023. CHECK (b.mixer[3].solo && ! b.mixer[2].solo && b.mixer[3].toMaster && b.mixer[3].sends.empty());
  5024. CHECK (b.mixer[0].effects.empty() && b.mode == lynx::PlayMode::song);
  5025. const auto audible = b.soloAudible();
  5026. CHECK (audible[3] && ! audible[1] && ! audible[7]);
  5027. const auto channels = s.channels.size();
  5028. const auto inserts = s.mixer.size();
  5029. const auto created = lynx::applyBounce (s, 3, "C:/x.wav", 3840);
  5030. CHECK (created == (int) inserts && s.mixer.size() == inserts + 1 && s.mixer[3].muted);
  5031. CHECK (s.mixer[(std::size_t) created].sends.size() == 1 && s.mixer[(std::size_t) created].sends[0].target == 7);
  5032. CHECK (s.channels.size() == channels + 1 && s.channels.back().mixerTrack == created);
  5033. CHECK (s.playlist.back().kind == lynx::ClipKind::audio && s.playlist.back().length == 3840);
  5034. }
  5035. TEST (mixer_stereo_separation_phase_swap)
  5036. {
  5037. lynx::MixerTrack t;
  5038. std::vector<float> l { 1.0f }, r { 0.0f };
  5039. t.stereo = 1.0f; // mono
  5040. lynx::RackRenderer::applyStereoForTest (t, l.data(), r.data(), 1);
  5041. CHECK (std::abs (l[0] - 0.5f) < 1e-6f && std::abs (r[0] - 0.5f) < 1e-6f);
  5042. l = { 1.0f };
  5043. r = { 0.0f };
  5044. t.stereo = -1.0f; // doubled side: 1.5 L - 0.5 R
  5045. lynx::RackRenderer::applyStereoForTest (t, l.data(), r.data(), 1);
  5046. CHECK (std::abs (l[0] - 1.5f) < 1e-6f && std::abs (r[0] + 0.5f) < 1e-6f);
  5047. l = { 1.0f };
  5048. r = { 0.25f };
  5049. t.stereo = 0.0f;
  5050. t.swapChannels = true;
  5051. t.invertPhase = true;
  5052. lynx::RackRenderer::applyStereoForTest (t, l.data(), r.data(), 1);
  5053. CHECK (l[0] == -0.25f && r[0] == -1.0f);
  5054. }
  5055. TEST (effects_limiter_filter_distortion_chorus)
  5056. {
  5057. const double sr = 48000.0;
  5058. auto sine = [] (float hz, float amp, int n) {
  5059. std::vector<float> v ((std::size_t) n);
  5060. for (int i = 0; i < n; ++i)
  5061. v[(std::size_t) i] = amp * std::sin (2.0f * 3.14159265f * hz * (float) i / 48000.0f);
  5062. return v;
  5063. };
  5064. auto peak = [] (const std::vector<float>& v, int from) {
  5065. float p = 0.0f;
  5066. for (std::size_t i = (std::size_t) from; i < v.size(); ++i)
  5067. p = std::max (p, std::abs (v[i]));
  5068. return p;
  5069. };
  5070. // limiter: +12 dB on a 0.9 sine never exceeds the ceiling (-1 dB) and reports the latency
  5071. auto lim = lynx::makeEffect (lynx::EffectKind::limiter);
  5072. lim->prepare (sr);
  5073. auto slot = lynx::makeEffectSlot (lynx::EffectKind::limiter);
  5074. slot.params[0] = 11.0f / 12.0f; // -1 dB
  5075. slot.params[1] = 0.5f; // +12 dB
  5076. auto l = sine (100.0f, 0.9f, 48000), r = l;
  5077. lim->process (l.data(), r.data(), 48000, slot.params, 120.0);
  5078. CHECK (peak (l, 0) <= std::pow (10.0f, -1.0f / 20.0f) + 1e-4f);
  5079. CHECK (peak (l, 4800) > 0.8f);
  5080. CHECK (lim->latencySamples() == 72);
  5081. // low-pass filter at 500 Hz knocks down an 8 kHz sine
  5082. auto lp = lynx::makeEffect (lynx::EffectKind::filter);
  5083. lp->prepare (sr);
  5084. auto fs = lynx::makeEffectSlot (lynx::EffectKind::filter);
  5085. fs.params[1] = std::log (500.0f / 20.0f) / std::log (1000.0f);
  5086. l = sine (8000.0f, 1.0f, 9600);
  5087. r = l;
  5088. lp->process (l.data(), r.data(), 9600, fs.params, 120.0);
  5089. CHECK (peak (l, 4800) < 0.05f);
  5090. // distortion and chorus: finite, audible output
  5091. for (auto kind : { lynx::EffectKind::distortion, lynx::EffectKind::chorus })
  5092. {
  5093. auto fx = lynx::makeEffect (kind);
  5094. fx->prepare (sr);
  5095. auto ps = lynx::makeEffectSlot (kind);
  5096. l = sine (220.0f, 0.5f, 9600);
  5097. r = l;
  5098. fx->process (l.data(), r.data(), 9600, ps.params, 120.0);
  5099. bool finite = true;
  5100. for (auto v : l)
  5101. finite = finite && std::isfinite (v);
  5102. CHECK (finite && peak (l, 2400) > 0.1f && peak (l, 0) < 2.0f);
  5103. }
  5104. }
  5105. TEST (slide_notes_glide_without_retrigger)
  5106. {
  5107. // sampler with a ramp: the read speed doubles after a one-octave slide
  5108. auto ramp = std::make_shared<lynx::SampleData>();
  5109. for (int i = 0; i < 96000; ++i)
  5110. ramp->left.push_back ((float) i / 96000.0f);
  5111. ramp->sampleRate = 48000.0;
  5112. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  5113. inst->prepare (48000.0);
  5114. inst->noteOn (60, 1.0f);
  5115. std::vector<float> l (2000), r (2000);
  5116. inst->render (l.data(), r.data(), 1000, 1.0f, 1.0f);
  5117. const auto stepBefore = l[999] - l[998];
  5118. inst->slideTo (72, 0.0f, 100.0f);
  5119. std::fill (l.begin(), l.end(), 0.0f);
  5120. inst->render (l.data(), r.data(), 1000, 1.0f, 1.0f);
  5121. const auto stepAfter = l[999] - l[998];
  5122. CHECK (std::abs (stepAfter / stepBefore - 2.0f) < 0.01f);
  5123. // the sequencer sends "slide" (no off) for the slide note
  5124. lynx::Pattern p;
  5125. lynx::Note base { 0, 960, 0, 48 };
  5126. lynx::Note glide { 480, 240, 0, 55 };
  5127. glide.slide = true;
  5128. p.notes = { base, glide };
  5129. lynx::EventBuffer ev;
  5130. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  5131. lynx::schedulePatternLoop (p, p.effectiveLength (4), 0.0, 30000, tps, ev);
  5132. int ons = 0, offs = 0, slides = 0;
  5133. for (auto& e : ev)
  5134. {
  5135. ons += e.type == lynx::NoteEvent::Type::on;
  5136. offs += e.type == lynx::NoteEvent::Type::off;
  5137. if (e.type == lynx::NoteEvent::Type::slide)
  5138. {
  5139. ++slides;
  5140. CHECK (e.key == 55 && std::abs (e.slideSamples - 240.0f / (float) tps) < 1.0f);
  5141. }
  5142. }
  5143. CHECK (ons == 1 && offs == 1 && slides == 1);
  5144. }
  5145. TEST (synth_fm_phase_modulation)
  5146. {
  5147. auto render = [] (const lynx::SynthSettings& s) {
  5148. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  5149. inst->prepare (48000.0);
  5150. lynx::ChannelDef c;
  5151. c.synth = s;
  5152. inst->configure (c, 120.0);
  5153. inst->noteOn (69, 1.0f); // 440 Hz
  5154. std::vector<float> l (48000), r (48000);
  5155. inst->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
  5156. return l;
  5157. };
  5158. // energy (Goertzel) at frequency f over frames [from, to)
  5159. auto energy = [] (const std::vector<float>& x, double f, int from, int to) {
  5160. const auto w = 2.0 * 3.141592653589793 * f / 48000.0, k = 2.0 * std::cos (w);
  5161. double s1 = 0.0, s2 = 0.0;
  5162. for (int i = from; i < to; ++i)
  5163. {
  5164. const auto s0 = x[(std::size_t) i] + k * s1 - s2;
  5165. s2 = s1;
  5166. s1 = s0;
  5167. }
  5168. return s1 * s1 + s2 * s2 - k * s1 * s2;
  5169. };
  5170. lynx::SynthSettings plain;
  5171. plain.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  5172. plain.osc[2] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  5173. plain.cutoff = 20000.0f;
  5174. auto fm = plain;
  5175. fm.fm = 1.0f;
  5176. const auto a = render (plain), b = render (fm);
  5177. // no FM: osc 3 adds (same pitch) and there's no 2nd harmonic; with FM the sidebands appear
  5178. CHECK (energy (a, 880.0, 4800, 24000) < 1e-3 * energy (a, 440.0, 4800, 24000));
  5179. CHECK (energy (b, 880.0, 4800, 24000) > 0.1 * energy (b, 440.0, 4800, 24000));
  5180. CHECK (energy (b, 1320.0, 4800, 24000) > 0.01 * energy (b, 440.0, 4800, 24000));
  5181. for (auto v : b)
  5182. CHECK (std::isfinite (v) && std::abs (v) < 1.0f);
  5183. // index decaying to 0: the start is bright, the end is nearly a sine again
  5184. auto decaying = fm;
  5185. decaying.fmDecay = 0.1f;
  5186. decaying.fmSustain = 0.0f;
  5187. const auto c = render (decaying);
  5188. const auto early = energy (c, 880.0, 0, 2400) / energy (c, 440.0, 0, 2400);
  5189. const auto late = energy (c, 880.0, 36000, 48000) / energy (c, 440.0, 36000, 48000);
  5190. CHECK (early > 0.05 && late < 1e-3);
  5191. }
  5192. TEST (synth_three_oscillators_adsr_filter)
  5193. {
  5194. auto render = [] (const lynx::SynthSettings& s, int frames, int releaseAt) {
  5195. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  5196. inst->prepare (48000.0);
  5197. lynx::ChannelDef c;
  5198. c.synth = s;
  5199. inst->configure (c, 120.0);
  5200. inst->noteOn (69, 1.0f); // A4 = 440 Hz
  5201. std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
  5202. inst->render (l.data(), r.data(), releaseAt, 1.0f, 1.0f);
  5203. inst->noteOff (69);
  5204. inst->render (l.data() + releaseAt, r.data() + releaseAt, frames - releaseAt, 1.0f, 1.0f);
  5205. return l;
  5206. };
  5207. auto peakIn = [] (const std::vector<float>& v, int from, int to) {
  5208. float p = 0.0f;
  5209. for (int i = from; i < to; ++i)
  5210. p = std::max (p, std::abs (v[(std::size_t) i]));
  5211. return p;
  5212. };
  5213. // default: sounds and fades after the release
  5214. const lynx::SynthSettings standard;
  5215. const auto a = render (standard, 48000, 24000);
  5216. CHECK (peakIn (a, 1000, 20000) > 0.05f && peakIn (a, 40000, 48000) < 1e-3f);
  5217. // pure sine with an open filter: zero-crossing count gives ~440 Hz
  5218. lynx::SynthSettings sine;
  5219. sine.osc[0].wave = lynx::Waveform::sine;
  5220. sine.cutoff = 20000.0f;
  5221. const auto b = render (sine, 48000, 47000);
  5222. int crossings = 0;
  5223. for (int i = 4800; i < 4800 + 48000 / 2; ++i)
  5224. crossings += (b[(std::size_t) i - 1] < 0.0f) != (b[(std::size_t) i] < 0.0f);
  5225. CHECK (std::abs (crossings - 440) < 6); // half a second = 220 cycles = 440 crossings
  5226. float sinePeak = 0.0f;
  5227. for (int i = 4800; i < 9600; ++i)
  5228. sinePeak = std::max (sinePeak, std::abs (b[(std::size_t) i]));
  5229. CHECK (sinePeak > 0.2f && sinePeak < 0.35f); // 0.3 level, approximated sine without clipping
  5230. // second oscillator an octave up adds energy; sustain 0 makes the note decay on its own
  5231. lynx::SynthSettings two = sine;
  5232. two.osc[1] = { lynx::Waveform::sine, 1, 0.0f, 1.0f };
  5233. two.sustain = 0.0f;
  5234. two.decay = 0.1f;
  5235. const auto c = render (two, 48000, 47000);
  5236. CHECK (peakIn (c, 100, 2000) > peakIn (b, 100, 2000) && peakIn (c, 30000, 40000) < 1e-3f);
  5237. }
  5238. TEST (set_target_value_round_trips)
  5239. {
  5240. auto s = lynx::Song::makeDefault();
  5241. s.mixer[2].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::delay));
  5242. const lynx::AutomationTarget targets[] = {
  5243. { lynx::AutomationTargetKind::channelVolume, 1 }, { lynx::AutomationTargetKind::channelPan, 2 },
  5244. { lynx::AutomationTargetKind::mixerVolume, 3 }, { lynx::AutomationTargetKind::mixerPan, 0 },
  5245. { lynx::AutomationTargetKind::effectParam, 2, 0, 1 }, { lynx::AutomationTargetKind::tempo, 0 },
  5246. };
  5247. for (const auto& t : targets)
  5248. {
  5249. CHECK (s.setTargetValue (t, 0.25f));
  5250. CHECK (std::abs (s.targetValue (t) - 0.25f) < 1e-5f);
  5251. }
  5252. CHECK (! s.setTargetValue ({ lynx::AutomationTargetKind::channelParam, 0, 0, 3 }, 0.5f)); // lives in the plugin
  5253. }
  5254. TEST (cut_groups_stretch_and_envelope_import)
  5255. {
  5256. // open hat (cut by 1) is cut when the closed one (group 1) plays
  5257. lynx::Song s;
  5258. s.mixer = lynx::Song::defaultMixer (2);
  5259. for (int i = 0; i < 2; ++i)
  5260. {
  5261. lynx::ChannelDef c;
  5262. c.instrument = lynx::InstrumentKind::sampler;
  5263. c.samplePath = "dc";
  5264. c.volume = 1.0f;
  5265. c.mixerTrack = i + 1;
  5266. s.channels.push_back (c);
  5267. }
  5268. s.channels[0].cutBy = 1; // open
  5269. s.channels[1].cutGroup = 1; // closed
  5270. auto& p = s.patterns.emplace_back();
  5271. p.notes = { { 0, 100, 0, 60 }, { 960, 100, 1, 60 } };
  5272. auto provider = [] (const std::string&) {
  5273. auto d = std::make_shared<lynx::SampleData>();
  5274. d->left.assign (96000, 0.5f);
  5275. return std::shared_ptr<const lynx::SampleData> (d);
  5276. };
  5277. auto state = lynx::RackState::build (s, nullptr, 48000.0, provider);
  5278. CHECK (state->hasCutGroups && state->cutTargets[1].size() == 1 && state->cutTargets[1][0] == 0);
  5279. lynx::RackRenderer r;
  5280. r.prepare (48000.0, 512);
  5281. std::vector<float> l (512), rr (512);
  5282. const auto tps = lynx::ticksPerSample (s.bpm, 48000.0);
  5283. float afterCut = 0.0f;
  5284. for (double tick = 0.0; tick < 960.0 * 3; tick += 512 * tps)
  5285. {
  5286. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  5287. if (tick > 960.0 * 2) // the closed one continues; the open one has stopped
  5288. afterCut = std::max (afterCut, std::abs (l[256]));
  5289. }
  5290. CHECK (std::abs (afterCut - 0.5f * 0.78f) < 0.01f); // only the closed one (default velocity 0.78)
  5291. // resampling stretch: 1 s of sample stretched to 4 beats at 120 BPM = 2 s
  5292. auto ramp = std::make_shared<lynx::SampleData>();
  5293. for (int i = 0; i < 48000; ++i)
  5294. ramp->left.push_back ((float) i / 48000.0f);
  5295. ramp->sampleRate = 48000.0;
  5296. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  5297. inst->prepare (48000.0);
  5298. lynx::ChannelDef c;
  5299. c.sampler.stretchBeats = 4.0f;
  5300. inst->configure (c, 120.0);
  5301. inst->noteOn (72, 1.0f); // the note pitch doesn't change the speed
  5302. std::vector<float> a (48000), b (48000);
  5303. inst->render (a.data(), b.data(), 48000, 1.0f, 1.0f);
  5304. CHECK (std::abs (a[47999] - 0.5f) < 0.01f); // after 1 s it has read half
  5305. // FL envelope time: 32950 = 2 s; < 101 = 0
  5306. CHECK (std::abs (flp::Channel::envelopeTime (32950) - 2.0) < 0.01 && flp::Channel::envelopeTime (50) == 0.0);
  5307. }
  5308. TEST (time_stretch_keeps_pitch)
  5309. {
  5310. lynx::SampleData sine;
  5311. sine.sampleRate = 48000.0;
  5312. for (int i = 0; i < 48000; ++i)
  5313. sine.left.push_back (0.5f * std::sin (2.0f * 3.14159265f * 440.0f * (float) i / 48000.0f));
  5314. auto crossingsPerSecond = [] (const lynx::SampleData& d) {
  5315. int c = 0;
  5316. const auto n = (int) d.frames();
  5317. for (int i = 4800; i < n - 4800; ++i)
  5318. c += (d.left[(std::size_t) i - 1] < 0.0f) != (d.left[(std::size_t) i] < 0.0f);
  5319. return (double) c / ((double) (n - 9600) / d.sampleRate);
  5320. };
  5321. for (double ratio : { 2.0, 0.5, 1.37 })
  5322. {
  5323. const auto out = lynx::timeStretch (sine, ratio);
  5324. CHECK (std::abs ((double) out->frames() - 48000.0 * ratio) < 2.0);
  5325. CHECK (std::abs (crossingsPerSecond (*out) - 880.0) < 15.0); // 440 Hz stays 440 Hz
  5326. float peak = 0.0f;
  5327. for (std::size_t i = 4800; i + 4800 < out->frames(); ++i)
  5328. peak = std::max (peak, std::abs (out->left[i]));
  5329. CHECK (peak > 0.4f && peak < 0.6f); // no gaps or clipping in the overlap
  5330. }
  5331. }
  5332. TEST (sampler_region_start_and_end)
  5333. {
  5334. auto ramp = std::make_shared<lynx::SampleData>();
  5335. for (int i = 0; i < 10000; ++i)
  5336. ramp->left.push_back ((float) i / 10000.0f);
  5337. ramp->sampleRate = 48000.0;
  5338. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  5339. inst->prepare (48000.0);
  5340. lynx::ChannelDef c;
  5341. c.sampler.regionStart = 0.25f;
  5342. c.sampler.regionEnd = 0.5f;
  5343. inst->configure (c, 120.0);
  5344. inst->noteOn (60, 1.0f);
  5345. std::vector<float> l (6000), r (6000);
  5346. inst->render (l.data(), r.data(), 6000, 1.0f, 1.0f);
  5347. CHECK (std::abs (l[0] - 0.25f) < 0.001f); // starts at the beginning of the slice
  5348. CHECK (l[2400] > 0.48f && l[2600] == 0.0f); // and stops at its end
  5349. }
  5350. TEST (arpeggiator_plays_held_chord_in_steps)
  5351. {
  5352. auto song = lynx::Song::makeDefault();
  5353. song.bpm = 120.0;
  5354. song.patterns[0].notes.clear();
  5355. for (int key : { 60, 64, 67 })
  5356. song.patterns[0].notes.push_back ({ 0, lynx::ppq * 4, 4, key, 0.8f }); // chord held for 1 bar
  5357. song.channels[4].arp.direction = 1; // up
  5358. song.channels[4].arp.stepBeats = 0.25f;
  5359. auto state = lynx::RackState::build (song, nullptr, 48000.0);
  5360. CHECK (state->arpeggiators[4] != nullptr && state->arpeggiators[0] == nullptr);
  5361. // the arpeggiator generates the notes; we count what it hands to the instrument
  5362. auto& arp = *state->arpeggiators[4];
  5363. const auto tps = lynx::ticksPerSample (song.bpm, 48000.0);
  5364. std::vector<int> keys;
  5365. lynx::EventBuffer ev;
  5366. for (double tick = 0.0; tick < lynx::ppq * 2.0; tick += 512 * tps)
  5367. {
  5368. ev.clear();
  5369. lynx::schedulePatternLoop (song.patterns[0], song.patterns[0].effectiveLength (4), tick, 512, tps, ev);
  5370. int in = 0, out = 0;
  5371. for (auto& e : ev)
  5372. if (e.channel == 4)
  5373. arp.input[(std::size_t) in++] = e;
  5374. arp.process (4, arp.input.data(), in, tick, tps, 512, song.channels[4].arp, arp.output.data(), out);
  5375. for (int i = 0; i < out; ++i)
  5376. if (arp.output[(std::size_t) i].type == lynx::NoteEvent::Type::on)
  5377. keys.push_back (arp.output[(std::size_t) i].key);
  5378. }
  5379. // 2 beats = 8 sixteenth steps: 60 64 67 60 64 67 60 64
  5380. CHECK (keys.size() >= 8); // the last block goes slightly past 2 beats
  5381. const int expected[] = { 60, 64, 67, 60, 64, 67, 60, 64 };
  5382. for (std::size_t i = 0; i < keys.size() && i < 8; ++i)
  5383. CHECK (keys[i] == expected[i]);
  5384. // actually rendering also makes sound
  5385. lynx::RackRenderer r;
  5386. r.prepare (48000.0, 512);
  5387. std::vector<float> l (512), rr (512);
  5388. float peak = 0.0f;
  5389. for (double tick = 0.0; tick < lynx::ppq; tick += 512 * tps)
  5390. {
  5391. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  5392. for (auto v : l)
  5393. peak = std::max (peak, std::abs (v));
  5394. }
  5395. CHECK (peak > 0.01f);
  5396. }
  5397. TEST (audio_clip_fades_and_gain)
  5398. {
  5399. lynx::Song s;
  5400. s.bpm = 120.0;
  5401. s.mode = lynx::PlayMode::song;
  5402. s.mixer = lynx::Song::defaultMixer (1);
  5403. lynx::ChannelDef c;
  5404. c.instrument = lynx::InstrumentKind::audioClip;
  5405. c.samplePath = "dc";
  5406. c.volume = 1.0f;
  5407. c.mixerTrack = 1;
  5408. s.channels.push_back (c);
  5409. s.patterns.emplace_back();
  5410. lynx::Clip clip;
  5411. clip.kind = lynx::ClipKind::audio;
  5412. clip.channel = 0;
  5413. clip.length = lynx::ppq * 4; // 2 s at 120 BPM
  5414. clip.fadeIn = lynx::ppq; // 0.5 s
  5415. clip.fadeOut = lynx::ppq;
  5416. clip.gain = 0.5f;
  5417. s.playlist.push_back (clip);
  5418. auto provider = [] (const std::string&) {
  5419. auto d = std::make_shared<lynx::SampleData>();
  5420. d->left.assign (48000 * 4, 1.0f);
  5421. return std::shared_ptr<const lynx::SampleData> (d);
  5422. };
  5423. auto state = lynx::RackState::build (s, nullptr, 48000.0, provider);
  5424. lynx::RackRenderer r;
  5425. r.prepare (48000.0, 512);
  5426. std::vector<float> all;
  5427. std::vector<float> l (512), rr (512);
  5428. const auto tps = lynx::ticksPerSample (s.bpm, 48000.0);
  5429. for (double tick = 0.0; tick < lynx::ppq * 4.0; tick += 512 * tps)
  5430. {
  5431. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  5432. all.insert (all.end(), l.begin(), l.end());
  5433. }
  5434. // FL plays a clip at unity at the default channel volume (78%): at 100% it is that much louder (REA_rodada89 §4.2)
  5435. const auto unity = 1.0f / lynx::channelGain (10000.0f / 12800.0f);
  5436. CHECK (all[100] < 0.01f); // fade-in start
  5437. CHECK (std::abs (all[12000] - 0.25f * unity) < 0.02f); // fade-in midpoint: half of 0.5
  5438. CHECK (std::abs (all[48000] - 0.5f * unity) < 0.01f); // body: the clip gain
  5439. CHECK (std::abs (all[96000 - 12000] - 0.25f * unity) < 0.02f); // fade-out midpoint
  5440. }
  5441. TEST (audio_clip_unity_at_fl_default_volume)
  5442. {
  5443. // a Playlist audio clip at FL's default channel volume (10000/12800) and centre pan plays its sample unchanged
  5444. auto dc = std::make_shared<lynx::SampleData>();
  5445. dc->left.assign (48000, 0.5f);
  5446. auto clip = lynx::makeInstrument (lynx::InstrumentKind::audioClip, dc);
  5447. clip->prepare (48000.0);
  5448. lynx::ChannelDef c;
  5449. c.instrument = lynx::InstrumentKind::audioClip;
  5450. c.volume = 10000.0f / 12800.0f;
  5451. clip->configure (c, 120.0);
  5452. clip->noteOn (60, 1.0f);
  5453. std::vector<float> l (4800, 0.0f), r (4800, 0.0f);
  5454. const auto g = lynx::channelGain (c.volume);
  5455. clip->render (l.data(), r.data(), 4800, g, g);
  5456. CHECK (std::abs (l[2400] - 0.5f) < 1.0e-3f && std::abs (r[2400] - 0.5f) < 1.0e-3f);
  5457. // a sampler channel (pattern notes) keeps the channel volume curve as it was
  5458. auto sampler = lynx::makeInstrument (lynx::InstrumentKind::sampler, dc);
  5459. sampler->prepare (48000.0);
  5460. c.instrument = lynx::InstrumentKind::sampler;
  5461. sampler->configure (c, 120.0);
  5462. sampler->noteOn (60, 1.0f);
  5463. std::fill (l.begin(), l.end(), 0.0f);
  5464. std::fill (r.begin(), r.end(), 0.0f);
  5465. sampler->render (l.data(), r.data(), 4800, g, g);
  5466. CHECK (std::abs (l[2400] - 0.5f * g) < 0.02f);
  5467. }
  5468. TEST (slicer_detects_hits_and_plays_slices_by_key)
  5469. {
  5470. // 4 hits (decaying noise) every 0.25 s
  5471. lynx::SampleData loop;
  5472. loop.sampleRate = 48000.0;
  5473. loop.left.assign (48000, 0.0f);
  5474. lynx::Noise noise;
  5475. for (int hit = 0; hit < 4; ++hit)
  5476. for (int i = 0; i < 6000; ++i)
  5477. loop.left[(std::size_t) (hit * 12000 + i)] = noise.next() * 0.8f * std::exp (-(float) i / 1500.0f);
  5478. const auto slices = lynx::detectSlices (loop, 0.5f);
  5479. CHECK (slices.size() == 4);
  5480. for (std::size_t i = 0; i < slices.size(); ++i)
  5481. CHECK (std::abs (slices[i] - 0.25f * (float) i) < 0.01f);
  5482. // key 61 plays the second slice, which starts at 0.25 and ends before 0.5
  5483. auto ramp = std::make_shared<lynx::SampleData>();
  5484. for (int i = 0; i < 48000; ++i)
  5485. ramp->left.push_back ((float) i / 48000.0f);
  5486. ramp->sampleRate = 48000.0;
  5487. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  5488. inst->prepare (48000.0);
  5489. lynx::ChannelDef c;
  5490. c.sampler.slices = slices;
  5491. inst->configure (c, 120.0);
  5492. inst->noteOn (61, 1.0f);
  5493. std::vector<float> l (20000), r (20000);
  5494. inst->render (l.data(), r.data(), 20000, 1.0f, 1.0f);
  5495. CHECK (std::abs (l[0] - 0.25f) < 0.01f && l[11000] > 0.45f && l[13000] == 0.0f);
  5496. }
  5497. TEST (project_templates_render_sound)
  5498. {
  5499. const auto names = lynx::templateNames();
  5500. CHECK (names.size() == 4);
  5501. for (int t = 1; t < (int) names.size(); ++t)
  5502. {
  5503. auto song = lynx::makeTemplate (t);
  5504. auto state = lynx::RackState::build (song, nullptr, 48000.0);
  5505. lynx::RackRenderer r;
  5506. r.prepare (48000.0, 512);
  5507. std::vector<float> l (512), rr (512);
  5508. const auto tps = lynx::ticksPerSample (song.bpm, 48000.0);
  5509. float peak = 0.0f;
  5510. for (double tick = 0.0; tick < lynx::ppq * 4.0; tick += 512 * tps)
  5511. {
  5512. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  5513. for (auto v : l)
  5514. peak = std::max (peak, std::abs (v));
  5515. }
  5516. CHECK (peak > 0.05f && peak < 4.0f);
  5517. }
  5518. CHECK (lynx::makeTemplate (0).patterns[0].notes.empty());
  5519. }
  5520. // --- interface translations (resources/i18n) -------------------------------------------------------
  5521. namespace i18ntest
  5522. {
  5523. std::string readFile (const std::filesystem::path& path)
  5524. {
  5525. std::ifstream in (path, std::ios::binary);
  5526. return { std::istreambuf_iterator<char> (in), std::istreambuf_iterator<char>() };
  5527. }
  5528. std::string unescape (const std::string& s)
  5529. {
  5530. std::string out;
  5531. for (std::size_t i = 0; i < s.size(); ++i)
  5532. {
  5533. if (s[i] == '\\' && i + 1 < s.size())
  5534. {
  5535. const auto n = s[++i];
  5536. out += n == 'n' ? '\n' : n == 't' ? '\t' : n;
  5537. }
  5538. else
  5539. out += s[i];
  5540. }
  5541. return out;
  5542. }
  5543. // reads a quoted string starting at s[pos] == '"'; returns the raw (escaped) contents and moves pos past the closing quote
  5544. bool quoted (const std::string& s, std::size_t& pos, std::string& raw)
  5545. {
  5546. if (pos >= s.size() || s[pos] != '"')
  5547. return false;
  5548. raw.clear();
  5549. for (++pos; pos < s.size() && s[pos] != '"' && s[pos] != '\n'; ++pos)
  5550. {
  5551. if (s[pos] == '\\' && pos + 1 < s.size())
  5552. raw += s[pos++];
  5553. raw += s[pos];
  5554. }
  5555. if (pos >= s.size() || s[pos] != '"')
  5556. return false;
  5557. ++pos;
  5558. return true;
  5559. }
  5560. // JUCE LocalisedStrings format: "english" = "translation"; duplicates are reported in `duplicates`
  5561. std::map<std::string, std::string> parseTable (const std::string& text, std::vector<std::string>& duplicates)
  5562. {
  5563. std::map<std::string, std::string> table;
  5564. std::istringstream lines (text);
  5565. std::string line;
  5566. while (std::getline (lines, line))
  5567. {
  5568. std::size_t pos = line.find_first_not_of (" \t");
  5569. std::string key, value;
  5570. if (pos == std::string::npos || ! quoted (line, pos, key))
  5571. continue;
  5572. pos = line.find ('"', pos);
  5573. if (pos == std::string::npos || ! quoted (line, pos, value))
  5574. value.clear();
  5575. const auto k = unescape (key);
  5576. if (table.count (k) != 0)
  5577. duplicates.push_back (k);
  5578. table[k] = unescape (value);
  5579. }
  5580. return table;
  5581. }
  5582. // string literals passed to tr() / TR_NOOP() in the sources (adjacent literals are concatenated)
  5583. std::map<std::string, std::string> sourceKeys (const std::filesystem::path& srcDir)
  5584. {
  5585. std::map<std::string, std::string> keys;
  5586. for (const auto& entry : std::filesystem::recursive_directory_iterator (srcDir))
  5587. {
  5588. const auto ext = entry.path().extension().string();
  5589. if (! entry.is_regular_file() || (ext != ".cpp" && ext != ".h"))
  5590. continue;
  5591. // drop // comment lines (they may quote tr ("...") as an example)
  5592. std::string text, line;
  5593. std::istringstream in (readFile (entry.path()));
  5594. while (std::getline (in, line))
  5595. {
  5596. const auto first = line.find_first_not_of (" \t");
  5597. text += (first != std::string::npos && line.compare (first, 2, "//") == 0) ? std::string() : line;
  5598. text += '\n';
  5599. }
  5600. auto isIdent = [] (char c) { return std::isalnum ((unsigned char) c) || c == '_'; };
  5601. for (const std::string name : { "tr", "TR_NOOP" })
  5602. for (auto at = text.find (name); at != std::string::npos; at = text.find (name, at + 1))
  5603. {
  5604. if ((at > 0 && isIdent (text[at - 1])) || isIdent (text[at + name.size()]))
  5605. continue;
  5606. auto pos = text.find_first_not_of (" \t\n", at + name.size());
  5607. if (pos == std::string::npos || text[pos] != '(')
  5608. continue;
  5609. pos = text.find_first_not_of (" \t\n", pos + 1);
  5610. std::string key, raw;
  5611. bool any = false;
  5612. while (pos != std::string::npos && quoted (text, pos, raw))
  5613. {
  5614. key += raw;
  5615. any = true;
  5616. pos = text.find_first_not_of (" \t\n", pos);
  5617. }
  5618. if (any)
  5619. keys.emplace (unescape (key), entry.path().filename().string());
  5620. }
  5621. }
  5622. return keys;
  5623. }
  5624. std::set<std::string> placeholders (const std::string& s)
  5625. {
  5626. std::set<std::string> found;
  5627. for (auto open = s.find ('{'); open != std::string::npos; open = s.find ('{', open + 1))
  5628. if (const auto close = s.find ('}', open); close != std::string::npos && close - open <= 3)
  5629. found.insert (s.substr (open, close - open + 1));
  5630. return found;
  5631. }
  5632. } // namespace i18ntest
  5633. TEST (translations_cover_every_ui_string)
  5634. {
  5635. using namespace i18ntest;
  5636. const auto text = readFile ("resources/i18n/pt-BR.txt");
  5637. CHECK (! text.empty());
  5638. std::vector<std::string> duplicates;
  5639. const auto table = parseTable (text, duplicates);
  5640. CHECK (table.size() > 300);
  5641. CHECK (duplicates.empty());
  5642. for (const auto& d : duplicates)
  5643. std::printf (" duplicate pt-BR entry: \"%s\"\n", d.c_str());
  5644. const auto keys = sourceKeys ("src");
  5645. CHECK (keys.size() > 300);
  5646. int missing = 0;
  5647. for (const auto& [key, file] : keys)
  5648. if (auto it = table.find (key); it == table.end() || it->second.empty())
  5649. {
  5650. ++missing;
  5651. std::printf (" no pt-BR translation for \"%s\" (%s)\n", key.c_str(), file.c_str());
  5652. }
  5653. CHECK (missing == 0);
  5654. // no empty values, and every {n} placeholder survives the translation
  5655. int empty = 0, badPlaceholders = 0;
  5656. for (const auto& [english, portuguese] : table)
  5657. {
  5658. empty += portuguese.empty() ? 1 : 0;
  5659. if (placeholders (english) != placeholders (portuguese))
  5660. {
  5661. ++badPlaceholders;
  5662. std::printf (" placeholders differ: \"%s\"\n", english.c_str());
  5663. }
  5664. }
  5665. CHECK (empty == 0);
  5666. CHECK (badPlaceholders == 0);
  5667. }
  5668. TEST (translations_embedded_table_is_up_to_date)
  5669. {
  5670. // src/ui/I18nData.h must be regenerated (tools/make_i18n.py) whenever the .txt changes
  5671. const auto text = i18ntest::readFile ("resources/i18n/pt-BR.txt");
  5672. CHECK (text.size() == sizeof (lynx::i18n::data::ptBR));
  5673. CHECK (std::memcmp (text.data(), lynx::i18n::data::ptBR, std::min (text.size(), sizeof (lynx::i18n::data::ptBR))) == 0);
  5674. }
  5675. // --- workflow shell: pattern/channel operations, key map, workspace layout, control math -------------
  5676. TEST (workflow_pattern_navigation_and_insert)
  5677. {
  5678. auto song = lynx::Song::makeDefault();
  5679. song.patterns.resize (3);
  5680. song.patterns[1].name = "B";
  5681. song.patterns[2].name = "C";
  5682. song.patterns[2].notes.push_back ({});
  5683. song.currentPattern = 0;
  5684. CHECK (lynx::stepPattern (song, 1) == 1 && lynx::stepPattern (song, -1) == 0 && lynx::stepPattern (song, 10) == 2);
  5685. // pattern 0 has the default notes, 1 is empty, 2 has a note
  5686. CHECK (! lynx::isPatternEmpty (song.patterns[0]) && lynx::isPatternEmpty (song.patterns[1]));
  5687. CHECK (lynx::findNextEmptyPattern (song, 0) == 1 && lynx::findNextEmptyPattern (song, 1) == -1);
  5688. CHECK (lynx::gotoNextEmptyPattern (song) == 1 && song.currentPattern == 1);
  5689. song.patterns[1].notes.push_back ({});
  5690. const auto before = song.patterns.size();
  5691. CHECK (lynx::gotoNextEmptyPattern (song) == (int) before && song.patterns.size() == before + 1);
  5692. CHECK (song.patterns.back().name == "Pattern 1" || song.patterns.back().name.rfind ("Pattern ", 0) == 0);
  5693. // insert keeps Playlist clips on their patterns
  5694. lynx::Clip clip;
  5695. clip.pattern = 2;
  5696. song.playlist = { clip };
  5697. song.arrangements.push_back ({ "Other", { clip }, 0, 0, {} });
  5698. CHECK (lynx::insertPattern (song, 1, "New") == 1 && song.currentPattern == 1);
  5699. CHECK (song.patterns[1].name == "New" && song.patterns[1].notes.empty() && song.patterns[3].name == "C");
  5700. CHECK (song.playlist[0].pattern == 3 && song.arrangements[1].clips[0].pattern == 3);
  5701. }
  5702. TEST (workflow_split_pattern_by_channel)
  5703. {
  5704. auto song = lynx::Song::makeDefault();
  5705. song.patterns.resize (1);
  5706. auto& p = song.patterns[0];
  5707. p.name = "Beat";
  5708. p.notes.clear();
  5709. for (int i = 0; i < 4; ++i)
  5710. p.notes.push_back ({ (std::int64_t) i * lynx::ticksPerStep, lynx::ticksPerStep, i % 2 == 0 ? 0 : 2, 60 });
  5711. song.channels[2].colour = 0xff112233;
  5712. lynx::Clip clip;
  5713. clip.pattern = 0;
  5714. song.playlist = { clip };
  5715. const auto created = lynx::splitPatternByChannel (song, 0);
  5716. CHECK (created == 2 && song.patterns.size() == 3);
  5717. CHECK (song.patterns[0].notes.size() == 4); // the original is unchanged
  5718. CHECK (song.patterns[1].name == "Beat - " + song.channels[0].name && song.patterns[1].notes.size() == 2);
  5719. CHECK (song.patterns[2].colour == 0xff112233u && song.patterns[2].notes.size() == 2 && song.patterns[2].notes[0].channel == 2);
  5720. CHECK (song.playlist[0].pattern == 0);
  5721. CHECK (lynx::splitPatternByChannel (song, 99) == 0);
  5722. }
  5723. TEST (workflow_channel_selection)
  5724. {
  5725. lynx::ChannelSelection s;
  5726. s.selectOnly (2);
  5727. s.toggle (5);
  5728. s.toggle (0);
  5729. CHECK ((s.rows == std::vector<int> { 0, 2, 5 }) && s.contains (5) && ! s.contains (3));
  5730. s.toggle (2);
  5731. CHECK ((s.rows == std::vector<int> { 0, 5 }));
  5732. s.selectOnly (3);
  5733. s.extendTo (6);
  5734. CHECK ((s.rows == std::vector<int> { 3, 4, 5, 6 }));
  5735. s.extendTo (1);
  5736. CHECK ((s.rows == std::vector<int> { 1, 2, 3 }));
  5737. s.selectOnly (4);
  5738. s.toggle (4); // the last one stays
  5739. CHECK ((s.rows == std::vector<int> { 4 }));
  5740. s.selectAll (3);
  5741. CHECK ((s.rows == std::vector<int> { 0, 1, 2 }));
  5742. s.rows = { 7, 9 };
  5743. s.clamp (5);
  5744. CHECK (s.rows.size() == 1 && s.rows[0] < 5);
  5745. }
  5746. TEST (workflow_move_clone_insert_channels)
  5747. {
  5748. auto song = lynx::Song::makeDefault();
  5749. const auto names = [&] {
  5750. std::vector<std::string> n;
  5751. for (const auto& c : song.channels)
  5752. n.push_back (c.name);
  5753. return n;
  5754. };
  5755. const auto original = names();
  5756. const auto count = (int) song.channels.size();
  5757. CHECK (count >= 4);
  5758. // references that must follow their channel
  5759. song.patterns[0].notes.push_back ({ 0, 240, 1, 64 });
  5760. lynx::Automation a;
  5761. a.target = { lynx::AutomationTargetKind::channelVolume, 1 };
  5762. song.automations.push_back (a);
  5763. song.midiLinks.push_back ({ 7, -1, { lynx::AutomationTargetKind::channelPan, 3 } });
  5764. lynx::Clip audio;
  5765. audio.kind = lynx::ClipKind::audio;
  5766. audio.channel = 3;
  5767. song.playlist.push_back (audio);
  5768. std::vector<int> selection { 1, 3 };
  5769. CHECK (lynx::moveChannels (song, selection, -1));
  5770. CHECK ((selection == std::vector<int> { 0, 2 }));
  5771. CHECK (song.channels[0].name == original[1] && song.channels[1].name == original[0] && song.channels[2].name == original[3]
  5772. && song.channels[3].name == original[2]);
  5773. CHECK (song.automations[0].target.index == 0 && song.midiLinks[0].target.index == 2 && song.playlist.back().channel == 2);
  5774. CHECK (std::any_of (song.patterns[0].notes.begin(), song.patterns[0].notes.end(),
  5775. [] (const lynx::Note& n) { return n.channel == 0 && n.key == 64; }));
  5776. CHECK (! lynx::moveChannels (song, selection, -1)); // the block is at the top
  5777. CHECK (lynx::moveChannels (song, selection, 1) && (selection == std::vector<int> { 1, 3 }) && names() == original);
  5778. // clone: settings, not notes, right below, with a new name
  5779. song.channels[1].volume = 0.33f;
  5780. const auto notesBefore = song.patterns[0].notes.size();
  5781. const auto clone = lynx::cloneChannel (song, 1);
  5782. CHECK (clone == 2 && (int) song.channels.size() == count + 1);
  5783. CHECK (song.channels[2].name == original[1] + " #2" && song.channels[2].volume == 0.33f);
  5784. CHECK (song.patterns[0].notes.size() == notesBefore);
  5785. CHECK (song.channels[3].name == original[2] && song.automations[0].target.index == 1);
  5786. CHECK (lynx::cloneChannel (song, 1) == 2 && song.channels[2].name == original[1] + " #3");
  5787. // remove several
  5788. CHECK (lynx::removeChannels (song, { 2, 3, 2 }) == 2 && (int) song.channels.size() == count);
  5789. CHECK (names() == original);
  5790. }
  5791. TEST (workflow_send_channels_to_free_inserts)
  5792. {
  5793. auto song = lynx::Song::makeDefault();
  5794. for (auto& c : song.channels)
  5795. c.mixerTrack = 0;
  5796. song.mixer[1].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::delay)); // not free: has effects
  5797. song.channels[0].mixerTrack = 2; // not free: used
  5798. song.setRoute (5, 3, true); // 3 is not free: receives a send
  5799. CHECK (! lynx::isInsertFree (song, 1) && ! lynx::isInsertFree (song, 2) && ! lynx::isInsertFree (song, 3) && lynx::isInsertFree (song, 4));
  5800. song.channels[1].colour = 0xff445566;
  5801. const auto tracks = lynx::sendChannelsToFreeInserts (song, { 1, 2 });
  5802. CHECK ((tracks == std::vector<int> { 4, 5 }) || (tracks == std::vector<int> { 4, 6 }));
  5803. CHECK (song.channels[1].mixerTrack == 4 && song.mixer[4].name == song.channels[1].name && song.mixer[4].colour == 0xff445566u);
  5804. CHECK (song.mixer[(std::size_t) tracks[1]].name == song.channels[2].name);
  5805. // with no free insert left, a new one is added at the end
  5806. auto full = lynx::Song::makeDefault();
  5807. full.mixer = lynx::Song::defaultMixer (1);
  5808. full.channels[0].mixerTrack = 1;
  5809. const auto added = lynx::sendChannelsToFreeInserts (full, { 1 });
  5810. CHECK (added.size() == 1 && added[0] == 2 && full.mixer.size() == 3 && full.channels[1].mixerTrack == 2);
  5811. }
  5812. TEST (workflow_palette_random_colour)
  5813. {
  5814. const auto& palette = lynx::lynxPalette();
  5815. for (std::uint32_t seed = 0; seed < 50; ++seed)
  5816. {
  5817. const auto current = palette[seed % palette.size()];
  5818. const auto c = lynx::randomPaletteColour (current, seed);
  5819. CHECK (c != current && std::find (palette.begin(), palette.end(), c) != palette.end());
  5820. }
  5821. }
  5822. TEST (workflow_key_map)
  5823. {
  5824. using namespace lynx::keys;
  5825. KeyMap map;
  5826. const auto global = Context::global, rack = Context::channelRack;
  5827. CHECK (map.find ({ space, none }, global, true) == "transport.playStop");
  5828. CHECK (map.find ({ space, ctrl }, global, true) == "transport.pause");
  5829. CHECK (map.find ({ f6, none }, global, true) == "panel.channelRack");
  5830. CHECK (map.find ({ f8, alt }, global, false) == "panel.browser");
  5831. CHECK (map.find ({ insert, ctrl | shift }, global, false) == "pattern.insert");
  5832. CHECK (map.find ({ numpadPlus, none }, rack, true) == "pattern.next");
  5833. // panel context: only while focused
  5834. CHECK (map.find ({ 'L', ctrl }, rack, true) == "rack.sendToInserts");
  5835. CHECK (map.find ({ 'L', ctrl }, global, true).empty());
  5836. CHECK (map.find ({ up, alt }, rack, false) == "rack.moveUp");
  5837. // typing piano: note keys play, other plain keys only for whilePiano actions; chords always work
  5838. CHECK (KeyMap::playsNote ({ 'Z', none }, true) && ! KeyMap::playsNote ({ 'Z', none }, false) && ! KeyMap::playsNote ({ 'Z', ctrl }, true));
  5839. CHECK (map.find ({ 'L', none }, global, true) == "transport.toggleMode");
  5840. CHECK (map.find ({ '1', none }, rack, false) == "rack.mute1" && map.find ({ '0', none }, rack, false) == "rack.mute10");
  5841. CHECK (map.find ({ '1', none }, rack, true).empty()); // a plain digit is not a shortcut while the piano is on
  5842. CHECK (map.find ({ '2', ctrl }, rack, true) == "rack.solo2");
  5843. CHECK (typingPianoNote ('Z') == 60 && typingPianoNote ('q') == 72 && typingPianoNote ('P') == 88 && typingPianoNote ('L') == -1);
  5844. CHECK (map.conflicts().empty());
  5845. // chord text
  5846. CHECK (toString ({ f4, ctrl | shift }) == "Ctrl+Shift+F4" && toString ({ numpadMinus, none }) == "Numpad -");
  5847. CHECK (parseChord ("Shift+Ctrl+Ins") == (Chord { insert, ctrl | shift }));
  5848. CHECK (parseChord ("alt+up") == (Chord { up, alt }) && parseChord ("k") == (Chord { 'K', none }));
  5849. CHECK (parseChord ("Numpad +") == (Chord { numpadPlus, none }) && ! parseChord ("F13") && ! parseChord ("Ctrl+"));
  5850. for (const auto& a : actions())
  5851. for (const auto& c : a.defaults)
  5852. CHECK (parseChord (toString (c)) == c);
  5853. // JSON overrides
  5854. std::vector<std::string> errors;
  5855. CHECK (map.loadJson (R"({ "pattern.next": "Ctrl+Right", "rack.clone": ["Alt+D", "Ctrl+D"], "help.shortcuts": "", "nope": "F1", "file.open": "Hyper+Q" })", &errors));
  5856. CHECK (errors.size() == 2);
  5857. CHECK (map.find ({ right, ctrl }, global, false) == "pattern.next" && map.find ({ numpadPlus, none }, global, false).empty());
  5858. CHECK (map.find ({ 'D', ctrl }, rack, false) == "rack.clone" && map.find ({ f1, none }, global, false).empty());
  5859. map.setChords ("pattern.previous", { { right, ctrl } });
  5860. CHECK (map.conflicts().size() == 1);
  5861. KeyMap reloaded;
  5862. CHECK (reloaded.loadJson (map.toJson()) && reloaded.chordsFor ("rack.clone") == map.chordsFor ("rack.clone"));
  5863. CHECK (! map.loadJson ("[1, 2]") && ! map.loadJson ("{\"a\": }"));
  5864. map.resetToDefaults();
  5865. CHECK (map.find ({ numpadPlus, none }, global, false) == "pattern.next");
  5866. }
  5867. TEST (workflow_workspace_layout)
  5868. {
  5869. using namespace lynx::workspace;
  5870. auto l = Layout::makeDefault (1280, 620);
  5871. // every visible panel is inside the desktop; the default shows Browser, Playlist and Channel Rack
  5872. for (const auto& p : l.panels)
  5873. CHECK (p.bounds.x >= 0 && p.bounds.y >= 0 && p.bounds.right() <= 1280 && p.bounds.bottom() <= 620 && p.bounds.w >= minWidth);
  5874. CHECK (l[Panel::browser].visible && l[Panel::playlist].visible && l[Panel::channelRack].visible);
  5875. CHECK (! l[Panel::pianoRoll].visible && ! l[Panel::mixer].visible);
  5876. CHECK (l[Panel::playlist].bounds.x == l[Panel::browser].bounds.right() && l[Panel::playlist].bounds.right() == 1280);
  5877. CHECK (l.isFrontmost (Panel::channelRack) && l.focused == Panel::channelRack);
  5878. // toggle: show+focus, focus, hide
  5879. CHECK (l.toggle (Panel::mixer) && l.focused == Panel::mixer && l.isFrontmost (Panel::mixer));
  5880. CHECK (l.toggle (Panel::channelRack) && l.focused == Panel::channelRack && l.isFrontmost (Panel::channelRack));
  5881. CHECK (! l.toggle (Panel::channelRack) && ! l[Panel::channelRack].visible && l.focused == Panel::mixer);
  5882. l.toggleMaximised (Panel::playlist);
  5883. CHECK (l[Panel::playlist].maximised && l.focused == Panel::playlist);
  5884. // save and load at another size: positions scale
  5885. l[Panel::mixer].detached = true;
  5886. l[Panel::mixer].windowBounds = { 100, 50, 900, 500 };
  5887. const auto text = l.serialise();
  5888. const auto big = Layout::parse (text, 2560, 1240);
  5889. CHECK (big.has_value());
  5890. if (big)
  5891. {
  5892. CHECK ((*big)[Panel::playlist].maximised && (*big)[Panel::mixer].detached && (*big)[Panel::mixer].windowBounds.w == 900);
  5893. CHECK (std::abs ((*big)[Panel::playlist].bounds.x - 2 * l[Panel::playlist].bounds.x) <= 1);
  5894. CHECK (big->order == l.order && big->focused == l.focused);
  5895. }
  5896. 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));
  5897. // resize keeps panels inside and edges glued to the border
  5898. l.resize (1000, 500);
  5899. CHECK (l[Panel::playlist].bounds.right() == 1000 && l[Panel::browser].bounds.bottom() == 500);
  5900. for (const auto& p : l.panels)
  5901. CHECK (p.bounds.right() <= 1000 && p.bounds.bottom() <= 500);
  5902. // off-screen panels come back
  5903. l[Panel::pianoRoll].bounds = { 5000, -300, 400, 300 };
  5904. CHECK (l.bringBackOffscreen() == 1 && l[Panel::pianoRoll].bounds.x + 400 <= 1000 && l[Panel::pianoRoll].bounds.y == 0);
  5905. // snapping: to the desktop, to another panel, and resizing one edge
  5906. CHECK ((snap ({ 5, 6, 200, 100 }, {}, 1000, 500) == Rect { 0, 0, 200, 100 }));
  5907. CHECK ((snap ({ 304, 50, 200, 100 }, { { 0, 0, 300, 400 } }, 1000, 500) == Rect { 300, 50, 200, 100 }));
  5908. CHECK ((snap ({ 304, 50, 200, 100 }, { { 0, 0, 300, 400 } }, 1000, 500, 2) == Rect { 304, 50, 200, 100 })); // out of range
  5909. CHECK ((snap ({ 100, 100, 195, 100 }, { { 300, 0, 300, 400 } }, 1000, 500, 8, right) == Rect { 100, 100, 200, 100 }));
  5910. CHECK ((snap ({ 100, 100, 195, 100 }, { { 300, 1000, 300, 400 } }, 2000, 2000, 8, right) == Rect { 100, 100, 195, 100 })); // not facing
  5911. }
  5912. TEST (workflow_control_math)
  5913. {
  5914. using namespace lynx::controls;
  5915. CHECK (std::abs (dragValue (0.5, 75, 0, 1, 150, false) - 1.0) < 1e-9);
  5916. CHECK (std::abs (dragValue (0.5, 75, 0, 1, 150, true) - 0.55) < 1e-9);
  5917. CHECK (dragValue (0.5, -1000, -1, 1, 150, false) == -1.0);
  5918. 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);
  5919. CHECK (wheelValue (3, 1, 0, 10, true, 1) == 4.0); // integer controls move at least one step
  5920. CHECK (std::abs (snapToInterval (0.234, 0, 0.1) - 0.2) < 1e-9);
  5921. CHECK (tempoDrag (130.0, 8, false, false) == 132.0 && tempoDrag (130.0, -3, false, false) == 130.0);
  5922. CHECK (std::abs (tempoDrag (130.0, 25, true, false) - 130.25) < 1e-9);
  5923. CHECK (std::abs (tempoDrag (130.0, 8, false, true) - 130.2) < 1e-9);
  5924. CHECK (tempoDrag (520.0, 400, false, false) == lynx::maxTempo && halfTempo (15.0) == lynx::minTempo && doubleTempo (130.0) == 260.0);
  5925. TapTempo tap;
  5926. CHECK (! tap.tap (1000.0));
  5927. CHECK (tap.tap (1500.0) == 120.0);
  5928. CHECK (tap.tap (2000.0) == 120.0 && tap.count() == 3);
  5929. CHECK (std::abs (*tap.tap (2400.0) - 128.57) < 0.01); // average of 500, 500, 400 ms
  5930. CHECK (! tap.tap (9000.0) && tap.count() == 1); // a long pause starts over
  5931. }
  5932. int main (int argc, char** argv)
  5933. {
  5934. if (argc > 1)
  5935. realFlpDir = argv[1];
  5936. const char* only = std::getenv ("LYNX_TEST"); // run only the tests whose name contains this
  5937. for (auto& [name, fn] : registry())
  5938. {
  5939. if (only != nullptr && std::strstr (name, only) == nullptr)
  5940. continue;
  5941. std::printf ("%s\n", name);
  5942. fn();
  5943. }
  5944. std::printf ("\n%d checks, %d failures\n", checks, failures);
  5945. return failures == 0 ? 0 : 1;
  5946. }
  5947. TEST (synth_cutoff_keytrack)
  5948. {
  5949. // the cutoff follows the note from key 48 (130.81 Hz), like Harmor's "Key to filter frequency"
  5950. auto rms = [] (float keyTrack, int key) {
  5951. lynx::SynthSettings s;
  5952. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  5953. s.cutoff = 130.81f;
  5954. s.keyTrack = keyTrack;
  5955. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  5956. inst->prepare (48000.0);
  5957. lynx::ChannelDef c;
  5958. c.synth = s;
  5959. inst->configure (c, 120.0);
  5960. inst->noteOn (key, 1.0f);
  5961. std::vector<float> l (24000), r (24000);
  5962. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  5963. double e = 0.0;
  5964. for (int i = 4800; i < 24000; ++i)
  5965. e += (double) l[(std::size_t) i] * l[(std::size_t) i];
  5966. return std::sqrt (e / 19200.0);
  5967. };
  5968. const auto fixed = rms (0.0f, 72), tracked = rms (1.0f, 72);
  5969. CHECK (tracked > 4.0 * fixed); // 2 octaves up: without keytrack the low-pass cuts
  5970. CHECK (std::abs (rms (1.0f, 48) - rms (0.0f, 48)) < 0.05 * rms (0.0f, 48)); // unchanged at the reference key
  5971. }
  5972. namespace
  5973. {
  5974. std::vector<float> renderSynthNote (const lynx::SynthSettings& s, int key, int frames)
  5975. {
  5976. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  5977. inst->prepare (48000.0);
  5978. lynx::ChannelDef c;
  5979. c.synth = s;
  5980. inst->configure (c, 120.0);
  5981. inst->noteOn (key, 1.0f);
  5982. std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
  5983. inst->render (l.data(), r.data(), frames, 1.0f, 1.0f);
  5984. return l;
  5985. }
  5986. double goertzel (const std::vector<float>& x, double f, int from, int to)
  5987. {
  5988. const auto w = 2.0 * 3.141592653589793 * f / 48000.0, k = 2.0 * std::cos (w);
  5989. double s1 = 0.0, s2 = 0.0;
  5990. for (int i = from; i < to; ++i)
  5991. {
  5992. const auto s0 = x[(std::size_t) i] + k * s1 - s2;
  5993. s2 = s1;
  5994. s1 = s0;
  5995. }
  5996. return s1 * s1 + s2 * s2 - k * s1 * s2;
  5997. }
  5998. } // namespace
  5999. TEST (synth_pitch_sweep_ring_and_bands)
  6000. {
  6001. lynx::SynthSettings sine;
  6002. sine.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  6003. sine.cutoff = 20000.0f;
  6004. // sweep: starts at f·(1 + sweep) and returns to f (time constant s = 10 ms; s⁴ falls 4× faster)
  6005. auto sweep = sine;
  6006. sweep.sweep = 1.0f;
  6007. sweep.sweepTime = 0.01f;
  6008. const auto a = renderSynthNote (sweep, 69, 24000);
  6009. auto crossings = [] (const std::vector<float>& x, int to) {
  6010. int n = 0;
  6011. for (int i = 1; i < to; ++i)
  6012. n += (x[(std::size_t) i - 1] < 0.0f) != (x[(std::size_t) i] < 0.0f);
  6013. return n;
  6014. };
  6015. CHECK (crossings (a, 480) > 1.3 * crossings (renderSynthNote (sine, 69, 480), 480)); // 10 ms: much higher
  6016. CHECK (goertzel (a, 440.0, 12000, 24000) > 100.0 * goertzel (a, 880.0, 12000, 24000));
  6017. // ring with |a| = 1: carrier 440 × modulator 110 -> only 330 and 550, no 440
  6018. auto ring = sine;
  6019. ring.am = ring.ring = true;
  6020. ring.osc[2] = { lynx::Waveform::sine, -2, 0.0f, 1.0f };
  6021. const auto b = renderSynthNote (ring, 69, 24000);
  6022. CHECK (goertzel (b, 330.0, 4800, 24000) > 100.0 * goertzel (b, 440.0, 4800, 24000));
  6023. CHECK (goertzel (b, 550.0, 4800, 24000) > 100.0 * goertzel (b, 440.0, 4800, 24000));
  6024. // 3 bands: muting the high knocks down a 4 kHz note and barely touches an 80 Hz one
  6025. auto bands = sine;
  6026. bands.bandGain = { 1.0f, 1.0f, 0.0f };
  6027. auto rms = [] (const std::vector<float>& x) {
  6028. double e = 0.0;
  6029. for (std::size_t i = 4800; i < x.size(); ++i)
  6030. e += (double) x[i] * x[i];
  6031. return std::sqrt (e / (double) (x.size() - 4800));
  6032. };
  6033. CHECK (rms (renderSynthNote (bands, 107, 24000)) < 0.3 * rms (renderSynthNote (sine, 107, 24000)));
  6034. CHECK (rms (renderSynthNote (bands, 28, 24000)) > 0.85 * rms (renderSynthNote (sine, 28, 24000)));
  6035. }
  6036. TEST (love_philter_map_evaluation)
  6037. {
  6038. lynx::LovePhilterMap line { { { 0.0, 0.0 }, { 1.0, 1.0 } } };
  6039. CHECK (std::abs (lynx::lovePhilterMapAt (line, 0.5) - 0.5) < 1e-9);
  6040. CHECK (std::abs (lynx::lovePhilterMapAt (line, 1.0) - 1.0) < 1e-9);
  6041. // steps (Δx = 0): 1 until 0.4375, ramp to 0 at 0.5, then 0
  6042. lynx::LovePhilterMap steps { { { 0.0, 1.0 }, { 0.4375, 1.0 }, { 0.0625, 0.0 }, { 0.5, 0.0 } } };
  6043. CHECK (lynx::lovePhilterMapAt (steps, 0.27) == 1.0);
  6044. CHECK (lynx::lovePhilterMapAt (steps, 0.75) == 0.0);
  6045. CHECK (std::abs (lynx::lovePhilterMapAt (steps, 0.46875) - 0.5) < 1e-9);
  6046. CHECK (lynx::lovePhilterMapAt ({}, 0.3) == 1.0);
  6047. }
  6048. TEST (lofi_holds_and_quantizes)
  6049. {
  6050. auto fx = lynx::makeEffect (lynx::EffectKind::lofi);
  6051. fx->prepare (48000.0);
  6052. auto slot = lynx::makeEffectSlot (lynx::EffectKind::lofi);
  6053. slot.params[0] = 0.5f; // 128·0.125 + 1 = 17 samples
  6054. slot.params[1] = 1.0f; // 65536/2049 ≈ 32 levels
  6055. std::vector<float> l (170), r (170);
  6056. for (std::size_t i = 0; i < l.size(); ++i)
  6057. l[i] = r[i] = 0.9f * (float) std::sin (0.05 * (double) i);
  6058. const auto original = l;
  6059. fx->process (l.data(), r.data(), 170, slot.params, 120.0);
  6060. for (int i = 0; i < 17; ++i)
  6061. CHECK (l[(std::size_t) i] == l[0]);
  6062. CHECK (l[17] != l[16]);
  6063. const auto step = 2.0f * 2049.0f / 65536.0f;
  6064. for (auto v : l)
  6065. CHECK (std::abs (v / step - std::round (v / step)) < 1e-3f);
  6066. CHECK (std::abs (l[0] - original[0]) <= step);
  6067. CHECK (lynx::formatEffectParam (lynx::EffectKind::lofi, 1, 0.0f, 120.0) == "16.0 bits");
  6068. }
  6069. namespace
  6070. {
  6071. lynx::EffectParams effectorParams (int type, float x, float y)
  6072. {
  6073. auto slot = lynx::makeEffectSlot (lynx::EffectKind::effector);
  6074. slot.params[0] = (float) type / 11.0f;
  6075. slot.params[3] = x;
  6076. slot.params[4] = y;
  6077. return slot.params;
  6078. }
  6079. } // namespace
  6080. TEST (effector_algorithms)
  6081. {
  6082. CHECK (lynx::effector::typeOf (8.0f / 11.0f) == 8);
  6083. CHECK (std::string (lynx::effector::typeName (11)) == "Ring");
  6084. CHECK (std::abs (lynx::effector::rateBeats (0.5f, 0.0f) - 0.75) < 1e-9); // trunc(8·0.5 + 0.5) = 4 -> 3/4
  6085. CHECK (std::abs (lynx::effector::rateBeats (1.0f, 0.5f) - 2.0) < 1e-9); // 8 × (1 − 0.5)²
  6086. auto run = [] (const lynx::EffectParams& p, std::vector<float> in, double bpm = 120.0) {
  6087. auto fx = lynx::makeEffect (lynx::EffectKind::effector);
  6088. fx->prepare (48000.0);
  6089. fx->setSongPosition (0.0);
  6090. auto r = in;
  6091. fx->process (in.data(), r.data(), (int) in.size(), p, bpm);
  6092. return in;
  6093. };
  6094. // Distort with Y = 1 (everything goes to the cascade): 0.5 comes out 0.5 (compensation G = 0.5/F(0.5))
  6095. {
  6096. auto p = effectorParams (0, 0.5f, 1.0f);
  6097. const auto out = run (p, std::vector<float> (4800, 0.5f));
  6098. CHECK (std::abs (out.back() - 0.5f) < 1e-3f);
  6099. const auto small = run (p, std::vector<float> (4800, 0.05f));
  6100. CHECK (small.back() > 0.45f && small.back() < 0.5f); // saturates near ±0.5
  6101. }
  6102. // Ring: DC 1 becomes the carrier sine, 2^(8.7·0 + 5) = 32 Hz -> 32 cycles per second
  6103. {
  6104. auto p = effectorParams (11, 0.0f, 0.0f);
  6105. const auto out = run (p, std::vector<float> (48000, 1.0f));
  6106. int crossings = 0;
  6107. for (std::size_t i = 4800; i < out.size(); ++i)
  6108. crossings += (out[i - 1] < 0.0f) != (out[i] < 0.0f);
  6109. CHECK (crossings >= 56 && crossings <= 60); // 0.9 s × 32 Hz × 2
  6110. }
  6111. // Trancer, Y = 0: at the start of the period the gain drops to zero; X = 1 returns only at the end of the period
  6112. {
  6113. auto p = effectorParams (8, 1.0f, 0.0f);
  6114. p[5] = 1.0f; // 8/1 = 8 beats = 4 s at 120 BPM
  6115. const auto out = run (p, std::vector<float> (48000, 1.0f));
  6116. CHECK (out[100] > 0.3f); // still rising on the "and" (1% of the period = 40 ms)
  6117. CHECK (out[24000] < 0.05f); // 0.5 s: trough
  6118. }
  6119. // Reverb with Wet = 0: the dry passes intact
  6120. {
  6121. auto p = effectorParams (6, 0.5f, 0.5f);
  6122. p[2] = 0.0f;
  6123. std::vector<float> in (4800);
  6124. for (std::size_t i = 0; i < in.size(); ++i)
  6125. in[i] = (float) std::sin (0.01 * (double) i);
  6126. const auto out = run (p, in);
  6127. for (std::size_t i = 0; i < in.size(); ++i)
  6128. CHECK (std::abs (out[i] - in[i]) < 1e-6f);
  6129. }
  6130. // Delay: the echo arrives after the Mod Rate period (1/4 beat at 120 BPM = 125 ms), at level X²
  6131. {
  6132. auto p = effectorParams (5, 0.7f, 0.5f);
  6133. p[5] = 1.0f / 8.0f; // index 1 = 1/4
  6134. std::vector<float> in (12000, 0.0f);
  6135. in[0] = 1.0f;
  6136. const auto out = run (p, in);
  6137. CHECK (std::abs (out[6000] - 0.49f) < 0.02f);
  6138. CHECK (std::abs (out[3000]) < 1e-6f);
  6139. }
  6140. // Stutter: repeats the recorded start every period
  6141. {
  6142. auto p = effectorParams (9, 0.0f, 0.0f);
  6143. p[5] = 0.0f; // 1/8 beat = 62.5 ms = 3000 samples
  6144. std::vector<float> in (9000);
  6145. for (std::size_t i = 0; i < in.size(); ++i)
  6146. in[i] = (float) i / 9000.0f;
  6147. const auto out = run (p, in);
  6148. CHECK (std::abs (out[3000 + 500] - in[500]) < 0.01f); // after the crossfade, it's the start again
  6149. CHECK (std::abs (out[1000] - in[1000]) < 1e-6f);
  6150. }
  6151. }
  6152. TEST (crossover_linkwitz_riley_sums_flat)
  6153. {
  6154. // low band + high band of the same crossover: sum with magnitude 1 (Linkwitz-Riley; for 12 and 36 dB the HP is inverted)
  6155. for (const auto combo : { 1, 3, 4, 5, 6, 7 })
  6156. for (const auto testHz : { 200.0, 1000.0, 5000.0 })
  6157. {
  6158. auto run = [&] (bool high) {
  6159. auto fx = lynx::makeEffect (lynx::EffectKind::crossover);
  6160. fx->prepare (48000.0);
  6161. auto slot = lynx::makeEffectSlot (lynx::EffectKind::crossover);
  6162. const auto value = (float) (std::log2 (1000.0 / 30.0) / 9.0589);
  6163. slot.params[high ? 0 : 3] = 1.0f;
  6164. slot.params[high ? 1 : 4] = value;
  6165. slot.params[high ? 2 : 5] = (float) combo / 7.0f;
  6166. std::vector<float> l (24000), r (24000);
  6167. for (std::size_t i = 0; i < l.size(); ++i)
  6168. l[i] = r[i] = (float) std::sin (2.0 * 3.141592653589793 * testHz * (double) i / 48000.0);
  6169. fx->process (l.data(), r.data(), 24000, slot.params, 120.0);
  6170. return l;
  6171. };
  6172. const auto low = run (false), high = run (true);
  6173. double peak = 0.0;
  6174. for (std::size_t i = 12000; i < low.size(); ++i)
  6175. peak = std::max (peak, (double) std::abs (low[i] + high[i]));
  6176. CHECK (std::abs (peak - 1.0) < 0.02);
  6177. }
  6178. // 96 dB/oct: two octaves above the cutoff the low-pass is already below −150 dB... in practice, very low
  6179. auto fx = lynx::makeEffect (lynx::EffectKind::crossover);
  6180. fx->prepare (48000.0);
  6181. auto slot = lynx::makeEffectSlot (lynx::EffectKind::crossover);
  6182. slot.params[3] = 1.0f;
  6183. slot.params[4] = (float) (std::log2 (500.0 / 30.0) / 9.0589);
  6184. slot.params[5] = 1.0f;
  6185. std::vector<float> l (24000), r (24000);
  6186. for (std::size_t i = 0; i < l.size(); ++i)
  6187. l[i] = r[i] = (float) std::sin (2.0 * 3.141592653589793 * 2000.0 * (double) i / 48000.0);
  6188. fx->process (l.data(), r.data(), 24000, slot.params, 120.0);
  6189. float peak = 0.0f;
  6190. for (std::size_t i = 12000; i < l.size(); ++i)
  6191. peak = std::max (peak, std::abs (l[i]));
  6192. CHECK (peak < 1e-4f);
  6193. }
  6194. TEST (rhythm_gain_and_autopan)
  6195. {
  6196. // 1-beat gate: 1 → 0 at 0.5 beat (linear) → 1 at the end; at 120 BPM 1 beat = 24000 samples at 48 kHz
  6197. auto slot = lynx::makeEffectSlot (lynx::EffectKind::rhythmGain);
  6198. slot.params[0] = 1.0f / 64.0f;
  6199. slot.params[1] = 3.0f / 12.0f;
  6200. const float pts[3][3] = { { 0.0f, 1.0f, 0.5f }, { 0.5f, 0.0f, 0.5f }, { 1.0f, 1.0f, 0.5f } };
  6201. for (int k = 0; k < 3; ++k)
  6202. for (int j = 0; j < 3; ++j)
  6203. slot.params[(std::size_t) (2 + 3 * k + j)] = pts[k][j];
  6204. auto fx = lynx::makeEffect (lynx::EffectKind::rhythmGain);
  6205. fx->prepare (48000.0);
  6206. fx->setSongPosition (960.0 * 2.0); // beat 2: phase 0
  6207. std::vector<float> l (24000, 1.0f), r (24000, 1.0f);
  6208. fx->process (l.data(), r.data(), 24000, slot.params, 120.0);
  6209. CHECK (std::abs (l[0] - 1.0f) < 1e-3f);
  6210. CHECK (std::abs (l[6000] - 0.5f) < 1e-3f);
  6211. CHECK (l[12000] < 1e-3f);
  6212. CHECK (std::abs (l[18000] - 0.5f) < 1e-3f);
  6213. // positive tension: changes quickly at the start of the segment
  6214. CHECK (lynx::effectInfo (lynx::EffectKind::rhythmGain).params.size() == 39);
  6215. // auto-pan: 1-beat period, full depth; start phase 0.75 -> W = 0 rising (pan centered)
  6216. auto pan = lynx::makeEffectSlot (lynx::EffectKind::autoPan);
  6217. pan.params[0] = 1.0f / 64.0f;
  6218. pan.params[1] = 1.0f;
  6219. auto ap = lynx::makeEffect (lynx::EffectKind::autoPan);
  6220. ap->prepare (48000.0);
  6221. ap->setSongPosition (0.0);
  6222. std::vector<float> a (24000, 1.0f), b (24000, 1.0f);
  6223. ap->process (a.data(), b.data(), 24000, pan.params, 120.0);
  6224. CHECK (std::abs (a[0] - 1.0f) < 1e-3f && std::abs (b[0] - 1.0f) < 1e-3f);
  6225. CHECK (a[6000] < 0.01f && std::abs (b[6000] - 1.0f) < 1e-3f); // ¼ cycle later: fully right
  6226. }
  6227. TEST (vocoder_side_inputs_split_modulator_and_carrier)
  6228. {
  6229. // "Side inputs" mode (Vocodex): modulator from side input 1 (noise in the first half), carrier from side input 2
  6230. // (saw), own input silent → the output follows the side modulator; with input 1 silent, the output is silent
  6231. auto slot = lynx::makeEffectSlot (lynx::EffectKind::vocoder);
  6232. slot.params[16] = 1.0f;
  6233. auto fx = lynx::makeEffect (lynx::EffectKind::vocoder);
  6234. fx->prepare (48000.0);
  6235. std::vector<float> modL (48000), car (48000), l (48000, 0.0f), r (48000, 0.0f);
  6236. std::uint32_t seed = 1;
  6237. for (std::size_t i = 0; i < modL.size(); ++i)
  6238. {
  6239. seed = seed * 1664525u + 1013904223u;
  6240. modL[i] = i < 24000 ? ((float) (seed >> 8) / 16777216.0f - 0.5f) : 0.0f;
  6241. car[i] = (float) (2.0 * std::fmod ((double) i * 220.0 / 48000.0, 1.0) - 1.0) * 0.5f;
  6242. }
  6243. for (int done = 0; done < 48000; done += 480)
  6244. {
  6245. fx->setSidechain (modL.data() + done, modL.data() + done);
  6246. fx->setSidechain2 (car.data() + done, car.data() + done);
  6247. fx->process (l.data() + done, r.data() + done, 480, slot.params, 120.0);
  6248. }
  6249. auto rms = [&] (int from, int to) {
  6250. double e = 0.0;
  6251. for (int i = from; i < to; ++i)
  6252. e += (double) l[(std::size_t) i] * l[(std::size_t) i];
  6253. return std::sqrt (e / (to - from));
  6254. };
  6255. CHECK (rms (6000, 20000) > 0.01);
  6256. CHECK (rms (40000, 48000) < 0.1 * rms (6000, 20000));
  6257. // the side modulator configured but silent: nothing comes out (the own input is not used instead)
  6258. std::vector<float> a (4800, 0.3f), b (4800, 0.3f);
  6259. for (int done = 0; done < 4800; done += 480)
  6260. {
  6261. fx->setSidechain (nullptr, nullptr);
  6262. fx->setSidechain2 (car.data() + done, car.data() + done);
  6263. fx->process (a.data() + done, b.data() + done, 480, slot.params, 120.0);
  6264. }
  6265. CHECK (std::abs (a[4700]) < 0.05f);
  6266. }
  6267. TEST (vocoder_follows_modulator)
  6268. {
  6269. // modulator (L) plays only in the first half; carrier (R) is a continuous saw: the wet follows the modulator
  6270. auto slot = lynx::makeEffectSlot (lynx::EffectKind::vocoder);
  6271. auto fx = lynx::makeEffect (lynx::EffectKind::vocoder);
  6272. fx->prepare (48000.0);
  6273. std::vector<float> l (48000), r (48000);
  6274. std::uint32_t seed = 1;
  6275. for (std::size_t i = 0; i < l.size(); ++i)
  6276. {
  6277. seed = seed * 1664525u + 1013904223u;
  6278. l[i] = i < 24000 ? ((float) (seed >> 8) / 16777216.0f - 0.5f) : 0.0f;
  6279. r[i] = (float) (2.0 * std::fmod ((double) i * 220.0 / 48000.0, 1.0) - 1.0) * 0.5f;
  6280. }
  6281. fx->process (l.data(), r.data(), 48000, slot.params, 120.0);
  6282. auto rms = [&] (int from, int to) {
  6283. double e = 0.0;
  6284. for (int i = from; i < to; ++i)
  6285. e += (double) l[(std::size_t) i] * l[(std::size_t) i];
  6286. return std::sqrt (e / (to - from));
  6287. };
  6288. CHECK (rms (6000, 20000) > 0.01);
  6289. CHECK (rms (40000, 48000) < 0.1 * rms (6000, 20000)); // modulator silent: the vocoder fades out (100 ms release)
  6290. for (std::size_t i = 0; i < l.size(); ++i)
  6291. CHECK (l[i] == r[i] && std::isfinite (l[i])); // mono output
  6292. }
  6293. TEST (fruity_vocoder_engine)
  6294. {
  6295. // REA_rodada70: Fruity Vocoder, states of our own against the installation's DLL (goldens = the DLL's output for
  6296. // the same input, EXEC; all three bit-identical over the whole 4096 frames)
  6297. std::vector<float> inL (4096), inR (4096);
  6298. for (int i = 0; i < 4096; ++i)
  6299. {
  6300. inL[(std::size_t) i] = i < 3000 ? (float) (((i * 37) % 200) - 100) / 128.0f : 0.0f; // modulator, then silence
  6301. inR[(std::size_t) i] = (i * 13) % 64 < 32 ? 0.5f : -0.5f; // carrier
  6302. }
  6303. auto make = [] (int bands, int order, int listen, bool swap, std::vector<float> levels, std::array<int, 12> c) {
  6304. lynx::fvocoder::State s;
  6305. s.bands = bands;
  6306. s.order = order;
  6307. s.listen = listen;
  6308. s.swap = swap;
  6309. for (std::size_t k = 0; k < levels.size(); ++k)
  6310. s.levels[k] = levels[k];
  6311. s.c = c;
  6312. return s;
  6313. };
  6314. auto run = [&] (const lynx::fvocoder::State& s, double rate, int block, int holdAt) {
  6315. std::vector<float> l = inL, r = inR;
  6316. lynx::fvocoder::Engine e;
  6317. e.setState (s);
  6318. e.prepare (rate);
  6319. for (int i = 0; i < 4096;)
  6320. {
  6321. auto len = std::min (block, 4096 - i);
  6322. if (holdAt > i && holdAt < i + len)
  6323. len = holdAt - i;
  6324. if (i == holdAt)
  6325. {
  6326. auto t = e.state();
  6327. t.c[lynx::fvocoder::hold] = 1;
  6328. e.setState (t);
  6329. }
  6330. e.processStereo (l.data() + i, r.data() + i, len);
  6331. i += len;
  6332. }
  6333. return std::make_pair (l, r);
  6334. };
  6335. auto check = [] (const std::pair<std::vector<float>, std::vector<float>>& out, const std::vector<std::pair<int, float>>& golden) {
  6336. for (const auto& [i, g] : golden)
  6337. CHECK (out.first[(std::size_t) i] == g && out.second[(std::size_t) i] == g);
  6338. };
  6339. // 16 bands, order 2, edited band levels, modulator and carrier levels in the mix
  6340. const auto a = make (16, 2, 2, false, { 1.0f, 0.5f, 1.5f, 1.0f, 0.25f, 2.0f, 1.0f, 1.0f, 0.75f, 1.25f, 1.0f, 0.0f, 1.0f, 1.0f, 1.75f, 1.0f },
  6341. { 0, 65536, 64, 0, 0, 50, 20, 100, 0, 20, 10, 128 });
  6342. const auto outA = run (a, 44100.0, 512, -1);
  6343. check (outA, { { 100, -0.23145467042922974f }, { 1000, 0.2901436686515808f }, { 2047, 0.03407000005245209f }, { 2500, 0.8314149379730225f },
  6344. { 3000, 0.606562614440918f }, { 3500, 0.2637328505516052f }, { 4095, -0.001206810586154461f } });
  6345. // 64 bands, order 3, inverted synthesis bank shifted by −300 cents, 48 kHz in blocks of 300, Hold from frame 2048
  6346. const auto b = make (64, 3, 2, false, {}, { 2000, 60000, 90, 1, -300, 120, 15, 300, 0, 0, 5, 150 });
  6347. check (run (b, 48000.0, 300, 2048), { { 100, 0.005184958688914776f }, { 1000, -0.0008608861826360226f }, { 2047, -0.026466617360711098f },
  6348. { 2500, -0.009371745400130749f }, { 3000, -0.018949978053569794f }, { 3500, -0.02667868137359619f },
  6349. { 4095, -0.021947938948869705f } });
  6350. // 4 bands, order 1, swapped inputs, narrow 30000…50000 range with the linear scale, 22.05 kHz in blocks of 77
  6351. const auto c = make (4, 1, 2, true, { 1.0f, 0.8f, 1.2f, 1.0f }, { 30000, 50000, 0, 0, 700, 180, 300, 40, 0, 64, 0, 160 });
  6352. check (run (c, 22050.0, 77, -1), { { 100, 0.31434211134910583f }, { 1000, -1.953513264656067f }, { 2047, 1.4135023355484009f },
  6353. { 2500, 1.539392352104187f }, { 3000, -0.27189597487449646f }, { 3500, -0.11583124846220016f },
  6354. { 4095, -0.11583124846220016f } });
  6355. // FL's state imports into the LYNX slot (controls as parameters, the levels in the state) and the slot's effect
  6356. // (one 4096-frame call, split into 512-frame engine blocks) gives the same output
  6357. const auto bytes = lynx::fvocoder::serialise (a);
  6358. CHECK (bytes.size() == 17 + 16 * 4 + 12 * 4);
  6359. const auto slot = lynx::effectFromFl ("Fruity Vocoder", bytes);
  6360. CHECK (slot && slot->kind == lynx::EffectKind::vocoder && slot->state == bytes);
  6361. CHECK (slot && slot->params[0] == 0.4f && slot->params[1] == 0.5f && slot->params[2] == 1.0f && slot->params[15] == 0.8f);
  6362. if (slot)
  6363. {
  6364. auto fx = lynx::makeEffect (*slot);
  6365. fx->prepare (44100.0);
  6366. std::vector<float> l = inL, r = inR;
  6367. fx->process (l.data(), r.data(), 4096, slot->params, 120.0);
  6368. CHECK (l == outA.first && r == outA.second);
  6369. CHECK (lynx::formatEffectParam (lynx::EffectKind::vocoder, 5, slot->params[5], 120.0) == "16000 Hz");
  6370. CHECK (lynx::formatEffectParam (lynx::EffectKind::vocoder, 10, slot->params[10], 120.0) == "20 ms");
  6371. CHECK (lynx::formatEffectParam (lynx::EffectKind::vocoder, 15, slot->params[15], 120.0) == "1.00");
  6372. }
  6373. const auto parsed = lynx::fvocoder::parse (bytes);
  6374. CHECK (parsed && parsed->bands == 16 && parsed->levels[5] == 2.0f && parsed->c[lynx::fvocoder::release] == 100);
  6375. CHECK (! lynx::fvocoder::parse ({ 2, 0, 0, 0, 12, 0, 0, 0 })); // 12 bands is not a valid count
  6376. // a raw control outside the knob range (FL keeps it, REA_rodada78 §1) survives while the knob shows it, clamped
  6377. CHECK (lynx::fvocoder::rawFromKnob (0.0f, lynx::fvocoder::attack, 5) == 5);
  6378. CHECK (lynx::fvocoder::rawFromKnob (0.5f, lynx::fvocoder::attack, 5) == 505);
  6379. CHECK (lynx::fvocoder::rawFromKnob (1.0f, lynx::fvocoder::wetLevel, 250) == 250);
  6380. CHECK (lynx::fvocoder::rawFromKnob (0.5f, lynx::fvocoder::freqScale, 64) == 64);
  6381. }
  6382. // Delphi run-time math (REA_rodada70 §2): values the plugin's own exp / ln / SinCos return (EXEC, called in place)
  6383. TEST (delphi_math_matches_plugin_runtime)
  6384. {
  6385. // arguments where the C library differs from the Delphi run-time in the last bit
  6386. CHECK (lynx::dmath::exp (0x1.f6e5c5a26a7f8p+3) == 0x1.98170c3deea30p+22);
  6387. CHECK (lynx::dmath::exp (-0x1.12fc91a51d906p+4) == 0x1.27108f934a58cp-25);
  6388. CHECK (lynx::dmath::exp (-0x1.2e9e4c229e93ap+4) == 0x1.a3bf2e4ee3f1ep-28);
  6389. CHECK (lynx::dmath::ln (0x1.76a77e2a09abep-1) == -0x1.3fd111c5297f8p-2);
  6390. CHECK (lynx::dmath::ln (0x1.da1feaa8547dfp+0) == 0x1.3b8abd2756774p-1);
  6391. CHECK (lynx::dmath::ln (0x1.a1e28772a0d78p-1) == -0x1.9ffcaa3a11e30p-3);
  6392. double s = 0.0, c = 0.0;
  6393. lynx::dmath::sinCos (0x1.91b4a19e65a9bp-1, s, c);
  6394. CHECK (s == 0x1.69be278761916p-1 && c == 0x1.6a559572e1ac9p-1);
  6395. lynx::dmath::sinCos (0x1.3284a93d70a8ap-1, s, c);
  6396. CHECK (s == 0x1.2088b313ea643p-1 && c == 0x1.a6f4d889fd91ap-1);
  6397. lynx::dmath::sinCos (0x1.f9b2f53d6ce02p+0, s, c);
  6398. CHECK (s == 0x1.d6a9b8f170994p-1 && c == -0x1.931701be7b5f3p-2);
  6399. }
  6400. // Vocodex (REA_rodada35): a synthetic state in FL's layout (version 8, 13 maps, 20 parameters, the 0x24 block,
  6401. // 13 map-axis bytes, tone, notes)
  6402. struct VocodexSpec
  6403. {
  6404. // Wet, SG, ModVol, ModHP, CarVol, CarLP, CarNoise, CarNoiseHP, Hold, Att, AttCurve, Rel, RelCurve, Freeze, BW,
  6405. // Flatness, ModBW, ModPitch, FatPitch, FatPan
  6406. 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 };
  6407. std::array<std::int32_t, 7> block { 0, 2, 0, 24, 1, 1, 2 }; // hold mode, order, unison, bands, draft field, ModIn, CarIn
  6408. 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
  6409. std::vector<std::uint8_t> bytes() const
  6410. {
  6411. std::vector<std::uint8_t> d;
  6412. 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); };
  6413. auto i32 = [&] (std::int32_t v) { put (&v, 4); };
  6414. i32 (8 << 16);
  6415. for (int m = 0; m < 13; ++m)
  6416. {
  6417. const double y0 = m == 8 ? 0.1429 : (m == 3 || m == 4 || m == 11) ? 0.0 : 0.5;
  6418. const double y1 = m == 8 ? 0.9918 : y0;
  6419. i32 (1), i32 (0), i32 (2);
  6420. for (const auto& [dx, y] : { std::pair { 0.0, y0 }, std::pair { 1.0, y1 } })
  6421. {
  6422. put (&dx, 8), put (&y, 8);
  6423. i32 (0), i32 (0); // tension (f32 0), flags
  6424. }
  6425. const std::array<std::uint8_t, 20> tail {};
  6426. put (tail.data(), tail.size());
  6427. }
  6428. for (auto v : params)
  6429. i32 (v);
  6430. for (auto v : block)
  6431. i32 (v);
  6432. put (flags.data(), flags.size());
  6433. const std::array<std::uint8_t, 13> axis {};
  6434. put (axis.data(), axis.size());
  6435. d.push_back (3), put ("PWM", 3);
  6436. i32 (1), i32 (60);
  6437. return d;
  6438. }
  6439. };
  6440. std::vector<float> vocodexSaw (int n, double hz, double fs)
  6441. {
  6442. std::vector<float> out ((std::size_t) n);
  6443. for (int i = 0; i < n; ++i)
  6444. out[(std::size_t) i] = (float) (2.0 * std::fmod (i * hz / fs, 1.0) - 1.0) * 0.4f;
  6445. return out;
  6446. }
  6447. TEST (vocodex_state_parse_and_conversion)
  6448. {
  6449. VocodexSpec spec;
  6450. spec.block = { 1, 3, 2, 40, 0, 2, 1 };
  6451. spec.params[17] = -1200;
  6452. const auto raw = spec.bytes();
  6453. const auto s = lynx::vocodex::parseState (raw);
  6454. CHECK (s.has_value());
  6455. if (! s)
  6456. return;
  6457. CHECK (s->version == 8 && s->holdMode == 1 && s->order == 3 && s->unison == 2 && s->bands == 40);
  6458. CHECK (s->draft() && s->modIn == 2 && s->carIn == 1 && s->minTimes && s->subtractiveGate && ! s->lrEncoding);
  6459. CHECK (s->params[lynx::vocodex::modPitch] == -1200 && s->params[lynx::vocodex::carLp] == 65536);
  6460. 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);
  6461. CHECK (s->tone == "PWM" && s->notes.size() == 1 && s->notes[0] == 60);
  6462. const auto v = lynx::vocodexEffect (raw);
  6463. CHECK (v && v->slot.kind == lynx::EffectKind::vocodex && v->slot.state == raw && v->modIn == 2 && v->carIn == 1 && v->notes.empty());
  6464. spec.block[6] = -1; // internal synth carrier: not available yet → reported
  6465. spec.params[1] = 150; // Soundgoodizer stage
  6466. const auto w = lynx::vocodexEffect (spec.bytes());
  6467. CHECK (w && w->carIn == -1 && w->notes.size() == 2);
  6468. CHECK (! lynx::vocodex::parseState ({ 1, 2, 3 }).has_value());
  6469. }
  6470. TEST (vocodex_vocodes_side_inputs)
  6471. {
  6472. // modulator on side input 1 (noise in the first half), carrier on side input 2 (saw), own input silent: the
  6473. // output follows the modulator's envelope and fades after it stops
  6474. VocodexSpec spec;
  6475. lynx::EffectSlot slot = lynx::makeEffectSlot (lynx::EffectKind::vocodex);
  6476. slot.state = spec.bytes();
  6477. auto fx = lynx::makeEffect (slot);
  6478. fx->prepare (48000.0);
  6479. CHECK (fx->latencySamples() == 0);
  6480. const auto car = vocodexSaw (48000, 110.0, 48000.0);
  6481. std::vector<float> mod (48000), l (48000, 0.0f), r (48000, 0.0f);
  6482. std::uint32_t seed = 7;
  6483. for (std::size_t i = 0; i < mod.size(); ++i)
  6484. {
  6485. seed = seed * 1664525u + 1013904223u;
  6486. mod[i] = i < 24000 ? ((float) (seed >> 8) / 16777216.0f - 0.5f) : 0.0f;
  6487. }
  6488. for (int done = 0; done < 48000; done += 512)
  6489. {
  6490. const int n = std::min (512, 48000 - done);
  6491. fx->setSidechain (mod.data() + done, mod.data() + done);
  6492. fx->setSidechain2 (car.data() + done, car.data() + done);
  6493. fx->process (l.data() + done, r.data() + done, n, slot.params, 120.0);
  6494. }
  6495. auto rms = [&] (int from, int to) {
  6496. double e = 0.0;
  6497. for (int i = from; i < to; ++i)
  6498. e += (double) l[(std::size_t) i] * l[(std::size_t) i] + (double) r[(std::size_t) i] * r[(std::size_t) i];
  6499. return std::sqrt (e / (2.0 * (to - from)));
  6500. };
  6501. CHECK (rms (6000, 20000) > 0.02);
  6502. CHECK (rms (40000, 48000) < 0.01 * rms (6000, 20000));
  6503. CHECK (std::abs (l[0]) < 1e-6f); // the gains ramp up from 0 when the plugin is created
  6504. }
  6505. TEST (vocodex_draft_latency_and_pass_through)
  6506. {
  6507. // carrier pass-through at unit gain only (Wet 0): after a Flush the gains snap, so the output is the carrier,
  6508. // 16 samples late in Draft
  6509. VocodexSpec spec;
  6510. spec.params[0] = 0; // Wet
  6511. spec.params[4] = 12800; // CarVol = 1
  6512. for (const int draftField : { 1, 0 })
  6513. {
  6514. spec.block[4] = draftField;
  6515. const auto s = lynx::vocodex::parseState (spec.bytes());
  6516. CHECK (s.has_value());
  6517. lynx::vocodex::Engine engine;
  6518. engine.prepare (*s, 44100.0, 300, s->draft());
  6519. CHECK (engine.latencySamples() == (draftField == 0 ? 16 : 0));
  6520. engine.flush();
  6521. const auto saw = vocodexSaw (3000, 220.0, 44100.0);
  6522. std::vector<float> car (6000), mod (6000, 0.0f), out (6000);
  6523. for (int i = 0; i < 3000; ++i)
  6524. car[(std::size_t) (2 * i)] = car[(std::size_t) (2 * i + 1)] = saw[(std::size_t) i];
  6525. static constexpr int sizes[] { 300, 37, 128, 1, 255, 16, 299 }; // uneven blocks exercise the FIFOs
  6526. for (int done = 0, k = 0, n = 0; done < 3000; done += n, ++k)
  6527. {
  6528. n = std::min (sizes[k % 7], 3000 - done);
  6529. engine.process (mod.data() + 2 * done, mod.data() + 2 * done, car.data() + 2 * done, out.data() + 2 * done, n);
  6530. }
  6531. const int lag = engine.latencySamples();
  6532. auto maxError = 0.0f;
  6533. for (int i = lag; i < 3000; ++i)
  6534. maxError = std::max (maxError, std::abs (out[(std::size_t) (2 * i)] - saw[(std::size_t) (i - lag)]));
  6535. CHECK (maxError == 0.0f);
  6536. }
  6537. }
  6538. TEST (vocodex_soundgoodizer_stage_limits_the_output)
  6539. {
  6540. // SG > 0: the vocoder's output (loud here: Wet 2000) goes through the Soundgoodizer approximation, whose
  6541. // master limiter keeps the peaks under −0.2 dBFS; its look-ahead is reported as latency
  6542. VocodexSpec spec;
  6543. spec.params[0] = 2000;
  6544. spec.params[1] = 250;
  6545. lynx::EffectSlot slot = lynx::makeEffectSlot (lynx::EffectKind::vocodex);
  6546. slot.state = spec.bytes();
  6547. auto fx = lynx::makeEffect (slot);
  6548. fx->prepare (48000.0);
  6549. CHECK (fx->latencySamples() == 72); // 1.5 ms at 48 kHz
  6550. const auto car = vocodexSaw (48000, 110.0, 48000.0);
  6551. std::vector<float> mod (48000), l (48000, 0.0f), r (48000, 0.0f);
  6552. for (std::size_t i = 0; i < mod.size(); ++i)
  6553. mod[i] = 0.9f * (float) std::sin (0.07 * (double) i) * (float) std::sin (0.0003 * (double) i);
  6554. auto peak = 0.0f;
  6555. for (int done = 0; done < 48000; done += 512)
  6556. {
  6557. const int n = std::min (512, 48000 - done);
  6558. fx->setSidechain (mod.data() + done, mod.data() + done);
  6559. fx->setSidechain2 (car.data() + done, car.data() + done);
  6560. fx->process (l.data() + done, r.data() + done, n, slot.params, 120.0);
  6561. }
  6562. for (std::size_t i = 0; i < l.size(); ++i)
  6563. peak = std::max ({ peak, std::abs (l[i]), std::abs (r[i]) });
  6564. CHECK (peak > 0.5f && peak <= 0.9775f);
  6565. }
  6566. TEST (vocodex_tail_bypass_and_noise)
  6567. {
  6568. // both inputs silent for longer than 0.5 s + hold + release: the own input passes unchanged
  6569. VocodexSpec spec;
  6570. const auto s = lynx::vocodex::parseState (spec.bytes());
  6571. CHECK (s.has_value());
  6572. lynx::vocodex::Engine engine;
  6573. engine.prepare (*s, 44100.0, 512, false);
  6574. std::vector<float> zero (1024, 0.0f), own (1024), out (1024);
  6575. for (std::size_t i = 0; i < own.size(); ++i)
  6576. own[i] = 0.25f * (float) std::sin (0.01 * (double) i);
  6577. for (int b = 0; b < 80; ++b)
  6578. engine.process (own.data(), zero.data(), zero.data(), out.data(), 512);
  6579. CHECK (out == own);
  6580. // carrier noise: the IPP-compatible generator restarts from the same seed after a Flush
  6581. spec.params[6] = 9000;
  6582. const auto noisy = lynx::vocodex::parseState (spec.bytes());
  6583. lynx::vocodex::Engine a;
  6584. a.prepare (*noisy, 44100.0, 512, false);
  6585. std::vector<float> mod (1024), first (1024), second (1024);
  6586. for (std::size_t i = 0; i < mod.size(); ++i)
  6587. mod[i] = 0.3f * (float) std::sin (0.05 * (double) i);
  6588. a.flush();
  6589. a.process (own.data(), mod.data(), zero.data(), first.data(), 512);
  6590. a.flush();
  6591. a.process (own.data(), mod.data(), zero.data(), second.data(), 512);
  6592. CHECK (first == second);
  6593. auto energy = 0.0;
  6594. for (auto v : first)
  6595. energy += (double) v * v;
  6596. CHECK (energy > 1e-6 && std::isfinite (energy));
  6597. }
  6598. TEST (synth_spectral_oscillator)
  6599. {
  6600. // 1/n spectrum = saw: same harmonic proportions and same level as LYNX's saw
  6601. lynx::SynthSettings saw;
  6602. saw.osc[0] = { lynx::Waveform::saw, 0, 0.0f, 1.0f };
  6603. saw.cutoff = 20000.0f;
  6604. auto spectral = saw;
  6605. spectral.osc[0].wave = lynx::Waveform::spectral;
  6606. for (int n = 1; n <= lynx::SynthSettings::spectrumSize; ++n)
  6607. spectral.spectrum[(std::size_t) n - 1] = 1.0f / (float) n;
  6608. const auto a = renderSynthNote (saw, 57, 24000), b = renderSynthNote (spectral, 57, 24000); // 220 Hz
  6609. for (const auto h : { 1, 2, 3, 5 })
  6610. {
  6611. const auto ea = goertzel (a, 220.0 * h, 4800, 24000), eb = goertzel (b, 220.0 * h, 4800, 24000);
  6612. CHECK (std::abs (10.0 * std::log10 (eb / ea)) < 0.5); // within 0.5 dB
  6613. }
  6614. // odd only: no 2nd harmonic
  6615. auto odd = spectral;
  6616. for (int n = 2; n <= lynx::SynthSettings::spectrumSize; n += 2)
  6617. odd.spectrum[(std::size_t) n - 1] = 0.0f;
  6618. const auto c = renderSynthNote (odd, 57, 24000);
  6619. CHECK (goertzel (c, 440.0, 4800, 24000) < 1e-4 * goertzel (c, 220.0, 4800, 24000));
  6620. // high note: the band-limited tables don't pass Nyquist (no aliasing below the fundamental)
  6621. const auto d = renderSynthNote (spectral, 105, 24000); // 3520 Hz
  6622. CHECK (goertzel (d, 1000.0, 4800, 24000) < 1e-3 * goertzel (d, 3520.0, 4800, 24000));
  6623. }
  6624. TEST (compressor_sidechain_ducks_and_orders_mixer)
  6625. {
  6626. // the detector listens to the side input: quiet signal on the track, loud kick on the sidechain -> reduction
  6627. auto slot = lynx::makeEffectSlot (lynx::EffectKind::compressor);
  6628. slot.params[0] = 0.5f; // threshold
  6629. slot.params[1] = 1.0f; // high ratio
  6630. slot.params[4] = 0.0f;
  6631. auto run = [&] (bool withSidechain) {
  6632. auto fx = lynx::makeEffect (lynx::EffectKind::compressor);
  6633. fx->prepare (48000.0);
  6634. std::vector<float> l (4800, 0.1f), r (4800, 0.1f), kick (4800, 1.0f);
  6635. if (withSidechain)
  6636. fx->setSidechain (kick.data(), kick.data());
  6637. fx->process (l.data(), r.data(), 4800, slot.params, 120.0);
  6638. return l.back();
  6639. };
  6640. const auto alone = run (false), ducked = run (true);
  6641. CHECK (ducked < 0.5f * alone);
  6642. // order: track 5 listens to 7 via sidechain -> 7 comes before 5
  6643. auto song = lynx::Song::makeDefault();
  6644. while (song.mixer.size() < 8)
  6645. song.mixer.push_back ({});
  6646. auto comp = lynx::makeEffectSlot (lynx::EffectKind::compressor);
  6647. comp.sidechain = 7;
  6648. song.mixer[5].effects.push_back (comp);
  6649. const auto order = song.mixerOrder();
  6650. const auto at = [&] (int t) { return std::find (order.begin(), order.end(), t) - order.begin(); };
  6651. CHECK (at (7) < at (5));
  6652. }
  6653. TEST (pitch_correction_pulls_to_allowed_note)
  6654. {
  6655. auto run = [] (const lynx::EffectParams& p, double hz) {
  6656. auto fx = lynx::makeEffect (lynx::EffectKind::pitchCorrect);
  6657. fx->prepare (48000.0);
  6658. std::vector<float> l (48000 * 2), r;
  6659. for (std::size_t i = 0; i < l.size(); ++i)
  6660. l[i] = 0.5f * (float) std::sin (2.0 * 3.141592653589793 * hz * (double) i / 48000.0);
  6661. r = l;
  6662. fx->process (l.data(), r.data(), (int) l.size(), p, 120.0);
  6663. // frequency from zero crossings in the last second
  6664. int crossings = 0;
  6665. for (std::size_t i = 48000 + 1; i < l.size(); ++i)
  6666. crossings += (l[i - 1] < 0.0f) != (l[i] < 0.0f);
  6667. return crossings / 2.0;
  6668. };
  6669. auto slot = lynx::makeEffectSlot (lynx::EffectKind::pitchCorrect); // chromatic, instant, A 440
  6670. CHECK (std::abs (run (slot.params, 452.0) - 440.0) < 4.0);
  6671. for (int k = 0; k < 12; ++k)
  6672. slot.params[(std::size_t) (5 + k)] = k == 0 ? 1.0f : 0.0f; // only C
  6673. CHECK (std::abs (run (slot.params, 452.0) - 523.25) < 5.0); // C5 (523 Hz) is closer than C4 (262)
  6674. slot.params[2] = 1.0f; // turn everything off: passes unchanged
  6675. CHECK (std::abs (run (slot.params, 452.0) - 452.0) < 4.0);
  6676. }
  6677. TEST (pitch_correction_bypass_is_a_pure_delay)
  6678. {
  6679. // FL's Pitcher with Bypass All still runs its shifter: the plain path is an exact 9-sample delay, the formant
  6680. // path (256-Hann overlap-add) an exact 257-sample delay, and Gender changes nothing without a shift
  6681. // (REA_rodada30 §2.4/2.5); both are reported as latency
  6682. std::vector<float> in (48000);
  6683. for (std::size_t i = 0; i < in.size(); ++i)
  6684. in[i] = 0.5f * (float) std::sin (2.0 * 3.141592653589793 * 440.0 * (double) i / 48000.0)
  6685. + 0.2f * (float) std::sin (2.0 * 3.141592653589793 * 1234.5 * (double) i / 48000.0);
  6686. auto run = [&] (bool formant, float gender, int& latency) {
  6687. auto fx = lynx::makeEffect (lynx::EffectKind::pitchCorrect);
  6688. fx->prepare (48000.0);
  6689. auto slot = lynx::makeEffectSlot (lynx::EffectKind::pitchCorrect);
  6690. slot.params[2] = 1.0f;
  6691. slot.params[29] = formant ? 1.0f : 0.0f;
  6692. slot.params[30] = gender;
  6693. auto l = in, r = in;
  6694. for (std::size_t i = 0; i < l.size(); i += 480)
  6695. fx->process (l.data() + i, r.data() + i, 480, slot.params, 120.0);
  6696. latency = fx->latencySamples();
  6697. return l;
  6698. };
  6699. int latency = 0;
  6700. auto plain = run (false, 0.5f, latency);
  6701. CHECK (latency == 9);
  6702. auto worst = 0.0f;
  6703. for (std::size_t i = 4000; i < in.size(); ++i)
  6704. worst = std::max (worst, std::abs (plain[i] - in[i - 9]));
  6705. CHECK (worst < 1e-5f);
  6706. auto formant = run (true, 0.8f, latency);
  6707. CHECK (latency == 257);
  6708. worst = 0.0f;
  6709. for (std::size_t i = 4000; i < in.size(); ++i)
  6710. worst = std::max (worst, std::abs (formant[i] - in[i - 257]));
  6711. CHECK (worst < 1e-5f);
  6712. }
  6713. TEST (pitch_correction_formant_path)
  6714. {
  6715. // Formant on: PSOLA keeps the correction (output pitch = grain spacing) while Gender resamples the grains:
  6716. // the spectrum of a saw moves up with Gender > 0.5 and down with Gender < 0.5
  6717. auto run = [] (float gender) {
  6718. auto fx = lynx::makeEffect (lynx::EffectKind::pitchCorrect);
  6719. fx->prepare (48000.0);
  6720. auto slot = lynx::makeEffectSlot (lynx::EffectKind::pitchCorrect); // chromatic, instant, A 440
  6721. slot.params[29] = 1.0f;
  6722. slot.params[30] = gender;
  6723. std::vector<float> l (48000 * 2), r;
  6724. for (std::size_t i = 0; i < l.size(); ++i)
  6725. l[i] = 0.5f * (float) (2.0 * std::fmod (452.0 * (double) i / 48000.0, 1.0) - 1.0); // saw
  6726. r = l;
  6727. fx->process (l.data(), r.data(), (int) l.size(), slot.params, 120.0);
  6728. l.erase (l.begin(), l.begin() + 48000);
  6729. return l;
  6730. };
  6731. // period by autocorrelation (lags 60…160 = 300…800 Hz)
  6732. auto pitch = [] (const std::vector<float>& x) {
  6733. auto best = 0.0, bestLag = 0.0;
  6734. for (int lag = 60; lag <= 160; ++lag)
  6735. {
  6736. auto sum = 0.0;
  6737. for (std::size_t i = 0; i + (std::size_t) lag < x.size(); ++i)
  6738. sum += (double) x[i] * x[i + (std::size_t) lag];
  6739. if (sum > best)
  6740. {
  6741. best = sum;
  6742. bestLag = lag;
  6743. }
  6744. }
  6745. return 48000.0 / bestLag;
  6746. };
  6747. // brightness: energy of the first difference relative to the energy
  6748. auto brightness = [] (const std::vector<float>& x) {
  6749. auto e = 0.0, d = 0.0;
  6750. for (std::size_t i = 1; i < x.size(); ++i)
  6751. {
  6752. e += (double) x[i] * x[i];
  6753. d += (double) (x[i] - x[i - 1]) * (x[i] - x[i - 1]);
  6754. }
  6755. return d / e;
  6756. };
  6757. const auto neutral = run (0.5f), up = run (0.8f), down = run (0.3f);
  6758. CHECK (std::abs (pitch (neutral) - 440.0) < 5.0); // 452 Hz pulled to A
  6759. CHECK (std::abs (pitch (up) - 440.0) < 5.0); // Gender does not move the pitch
  6760. CHECK (brightness (up) > 1.5 * brightness (neutral));
  6761. CHECK (brightness (down) < 0.7 * brightness (neutral));
  6762. }
  6763. TEST (parallel_container_inner_links_modulate)
  6764. {
  6765. // one branch: utility (gain) + a control-only auto-pan LFO (1 beat); the link maps the LFO 0..1 onto the
  6766. // utility gain −24 dB..0 dB, so the level must swing; without the link it stays put
  6767. auto container = lynx::makeEffectSlot (lynx::EffectKind::parallel);
  6768. auto gain = lynx::makeEffectSlot (lynx::EffectKind::utility);
  6769. auto lfo = lynx::makeEffectSlot (lynx::EffectKind::autoPan);
  6770. lfo.params[0] = 1.0f / 64.0f;
  6771. lfo.params[4] = 1.0f; // control only: audio passes unchanged
  6772. container.branches.push_back ({ { gain, lfo }, 1.0f });
  6773. lynx::EffectLink link;
  6774. link.source = 1;
  6775. link.param = 0;
  6776. for (int i = 0; i <= 32; ++i)
  6777. link.curve.push_back (0.5f * (float) i / 32.0f);
  6778. auto levels = [&] (bool linked) {
  6779. auto slot = container;
  6780. if (linked)
  6781. slot.links.push_back (link);
  6782. auto fx = lynx::makeParallelEffect (slot);
  6783. fx->prepare (48000.0);
  6784. float lo = 1e9f, hi = 0.0f;
  6785. for (int block = 0; block < 94; ++block) // ~1 beat at 120 BPM in 256-sample blocks
  6786. {
  6787. std::vector<float> l (256, 1.0f), r (256, 1.0f);
  6788. fx->setSongPosition (block * 256.0 / 48000.0 * 2.0 * 960.0); // the engine positions every block
  6789. fx->process (l.data(), r.data(), 256, slot.params, 120.0);
  6790. lo = std::min (lo, std::abs (l[128]));
  6791. hi = std::max (hi, std::abs (l[128]));
  6792. }
  6793. return std::pair { lo, hi };
  6794. };
  6795. const auto [lo, hi] = levels (true);
  6796. CHECK (hi > 0.8f && lo < 0.12f);
  6797. const auto [lo0, hi0] = levels (false);
  6798. CHECK (std::abs (hi0 - lo0) < 1e-4f);
  6799. }
  6800. TEST (synth_spectrum_decays_per_harmonic)
  6801. {
  6802. // 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
  6803. lynx::SynthSettings s;
  6804. s.osc[0] = { lynx::Waveform::spectral, 0, 0.0f, 1.0f };
  6805. s.cutoff = 20000.0f;
  6806. s.decay = 0.0f;
  6807. s.sustain = 1.0f;
  6808. s.spectrum[0] = 1.0f;
  6809. s.spectrum[2] = 1.0f;
  6810. s.spectrumDecay[2] = 0.2f;
  6811. const auto x = renderSynthNote (s, 57, 48000); // 220 Hz, 48 kHz, 1 s
  6812. // equal 2400-sample windows (Goertzel power grows with the window): 10…60 ms and 500…550 ms
  6813. const auto early1 = goertzel (x, 220.0, 480, 2880), early3 = goertzel (x, 660.0, 480, 2880);
  6814. const auto late1 = goertzel (x, 220.0, 24000, 26400), late3 = goertzel (x, 660.0, 24000, 26400);
  6815. CHECK (early3 > 0.05 * early1); // ≈ −9 dB on average over 10…60 ms
  6816. CHECK (late3 < 1e-4 * late1);
  6817. CHECK (std::abs (10.0 * std::log10 (late1 / early1)) < 1.0); // the 1st holds
  6818. // without per-harmonic decay the spectrum stays as it is
  6819. s.spectrumDecay[2] = 0.0f;
  6820. const auto y = renderSynthNote (s, 57, 48000);
  6821. CHECK (std::abs (10.0 * std::log10 (goertzel (y, 660.0, 24000, 26400) / goertzel (y, 660.0, 480, 2880))) < 1.0);
  6822. }
  6823. TEST (worker_pool_runs_each_index_once_per_round)
  6824. {
  6825. // rounds of alternating sizes (a stalled worker must never run an index of a later round), each index
  6826. // exactly once, and nothing running after parallelFor returns
  6827. lynx::WorkerPool pool (3);
  6828. std::vector<std::atomic<int>> hits (64);
  6829. std::atomic<int> round { 0 }, outside { 0 }, wrongRound { 0 };
  6830. std::atomic<bool> open { false };
  6831. int failures = 0;
  6832. for (int r = 1; r <= 20000; ++r)
  6833. {
  6834. const auto n = (r % 3 == 0) ? 64 : (r % 3 == 1 ? 2 : 9);
  6835. for (int i = 0; i < n; ++i)
  6836. hits[(std::size_t) i].store (0);
  6837. round.store (r);
  6838. open.store (true);
  6839. auto fn = [&, r] (int i) {
  6840. if (! open.load())
  6841. outside.fetch_add (1);
  6842. if (round.load() != r)
  6843. wrongRound.fetch_add (1);
  6844. hits[(std::size_t) i].fetch_add (1);
  6845. };
  6846. pool.parallelFor (n, fn);
  6847. open.store (false);
  6848. for (int i = 0; i < n; ++i)
  6849. failures += hits[(std::size_t) i].load() != 1;
  6850. }
  6851. CHECK (failures == 0);
  6852. CHECK (outside.load() == 0);
  6853. CHECK (wrongRound.load() == 0);
  6854. }
  6855. TEST (ladder_filter_follows_toxic_law)
  6856. {
  6857. // the 4-pole ladder: −24 dB/oct far above the cutoff (vs −12 for the SVF), resonance raises the corner, and
  6858. // the input saturation keeps it bounded even at full resonance
  6859. auto level = [] (bool ladder, float res, double hz) {
  6860. lynx::SynthSettings s;
  6861. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  6862. s.cutoff = 500.0f;
  6863. s.resonance = res;
  6864. s.ladder = ladder;
  6865. const auto key = (int) std::lround (69.0 + 12.0 * std::log2 (hz / 440.0));
  6866. const auto x = renderSynthNote (s, key, 48000);
  6867. double e = 0.0, peak = 0.0;
  6868. for (std::size_t i = 24000; i < 48000; ++i)
  6869. e += (double) x[i] * x[i], peak = std::max (peak, (double) std::abs (x[i]));
  6870. return std::pair { std::sqrt (e / 24000.0), peak };
  6871. };
  6872. 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
  6873. const auto svfHigh = level (false, 0.0f, 3520.0).first / level (false, 0.0f, 110.0).first;
  6874. CHECK (20.0 * std::log10 (high / low) < -55.0); // ≈ 24 dB/oct over ~2.8 octaves
  6875. CHECK (20.0 * std::log10 (svfHigh) > -45.0); // the 2-pole SVF falls much less
  6876. const auto corner0 = level (true, 0.0f, 523.25).first, corner1 = level (true, 0.6f, 523.25).first;
  6877. CHECK (corner1 > 1.5 * corner0); // resonance boosts near the cutoff
  6878. const auto loud = level (true, 1.0f, 523.25);
  6879. CHECK (std::isfinite (loud.first) && loud.second < 20.0); // self-oscillation stays bounded
  6880. }
  6881. TEST (synth_spectrum_settles_on_target)
  6882. {
  6883. // ALT DECAY-like morph: harmonic 3 starts at 1 and settles on 0.25 (−12 dB) instead of fading out
  6884. lynx::SynthSettings s;
  6885. s.osc[0] = { lynx::Waveform::spectral, 0, 0.0f, 1.0f };
  6886. s.cutoff = 20000.0f;
  6887. s.decay = 0.0f;
  6888. s.sustain = 1.0f;
  6889. s.spectrum[0] = 1.0f;
  6890. s.spectrum[2] = 1.0f;
  6891. s.spectrumDecay[2] = 0.1f;
  6892. s.spectrumTarget[2] = 0.25f;
  6893. const auto x = renderSynthNote (s, 57, 48000);
  6894. const auto ratio = goertzel (x, 660.0, 24000, 26400) / goertzel (x, 220.0, 24000, 26400);
  6895. CHECK (std::abs (10.0 * std::log10 (ratio) - 20.0 * std::log10 (0.25)) < 1.0);
  6896. }
  6897. TEST (kepler_chorus_is_stereo_and_keeps_level)
  6898. {
  6899. // chorus I on a sine: L and R differ (opposite LFO phases), the level stays close to the dry one
  6900. lynx::SynthSettings s;
  6901. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  6902. s.cutoff = 20000.0f;
  6903. auto dry = s;
  6904. s.chorus = 1;
  6905. auto render = [] (const lynx::SynthSettings& set) {
  6906. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  6907. inst->prepare (48000.0);
  6908. lynx::ChannelDef c;
  6909. c.synth = set;
  6910. inst->configure (c, 120.0);
  6911. inst->noteOn (69, 1.0f);
  6912. std::vector<float> l (96000), r (96000);
  6913. inst->render (l.data(), r.data(), 96000, 1.0f, 1.0f);
  6914. return std::pair { l, r };
  6915. };
  6916. const auto [l, r] = render (s);
  6917. const auto [dl, dr] = render (dry);
  6918. double diff = 0.0, e = 0.0, ed = 0.0;
  6919. for (std::size_t i = 48000; i < 96000; ++i)
  6920. diff += std::abs (l[i] - r[i]), e += (double) l[i] * l[i], ed += (double) dl[i] * dl[i];
  6921. CHECK (diff / 48000.0 > 1e-3);
  6922. CHECK (std::abs (10.0 * std::log10 (e / ed)) < 3.0);
  6923. }
  6924. TEST (fl_pan_law_matches_love_philter_dll)
  6925. {
  6926. // values printed by the installation's Love Philter pan routine for vol 0.8 (REA_rodada27 §3.1, lppan.txt)
  6927. struct Row { double pan, ll, rr, rl, lr; };
  6928. const Row rows[] = { { -1.5, 0.239782482, -0.432956904, 0.805467963, 0.0 }, { -0.5, 0.805467963, 0.432956904, 0.239782482, 0.0 },
  6929. { 0.0, 0.8, 0.8, 0.0, 0.0 }, { 0.75, 0.220719516, 0.707889318, 0.0, 0.401742548 },
  6930. { 1.0, 0.0, 0.565685451, 0.0, 0.565685451 } };
  6931. for (const auto& row : rows)
  6932. {
  6933. float ll, rl, lr, rr;
  6934. lynx::flPanMatrix (row.pan, ll, rl, lr, rr);
  6935. CHECK (std::abs (0.8 * ll - row.ll) < 1e-6 && std::abs (0.8 * rr - row.rr) < 1e-6);
  6936. CHECK (std::abs (0.8 * rl - row.rl) < 1e-6 && std::abs (0.8 * lr - row.lr) < 1e-6);
  6937. }
  6938. }
  6939. TEST (effector_units_match_fl_dll_impulse_responses)
  6940. {
  6941. // impulse responses of the installation's Effector engine (REA_rodada27 §2, eu_<kind>_<X>_<Y>.txt; LFO off,
  6942. // wet 1, blocks of 256): samples 1, 10, 100 and 1000
  6943. struct Case { int kind; float x, y; double v[4]; };
  6944. const Case cases[] = { { 3, 0.5f, 0.5f, { -0.603055418, 0.159798339, -0.0055043716, -1.06241259e-06 } },
  6945. { 4, 0.2f, 0.5f, { 4.68885949e-26, 4.20032622e-19, 0.000487731799, 2.27715846e-09 } },
  6946. { 4, 0.8f, 0.3f, { -0.293783396, -0.0306414738, -0.00068128563, 1.358577e-15 } } };
  6947. for (const auto& c : cases)
  6948. {
  6949. auto fx = lynx::effector::make();
  6950. fx->prepare (44100.0);
  6951. lynx::EffectParams p {};
  6952. p[lynx::effector::type] = c.kind / 11.0f;
  6953. p[lynx::effector::wetMix] = 1.0f;
  6954. p[lynx::effector::paramX] = c.x;
  6955. p[lynx::effector::paramY] = c.y;
  6956. p[lynx::effector::modX] = p[lynx::effector::modY] = 0.5f;
  6957. p[lynx::effector::modRate] = 0.5f;
  6958. p[lynx::effector::inputLevel] = 1.0f;
  6959. p[lynx::effector::outputGain] = 0.5f;
  6960. std::vector<float> l (1024, 0.0f), r (1024, 0.0f);
  6961. l[0] = r[0] = 1.0f;
  6962. for (int d = 0; d < 1024; d += 256)
  6963. fx->process (l.data() + d, r.data() + d, 256, p, 120.0);
  6964. const int at[4] { 1, 10, 100, 1000 };
  6965. for (int k = 0; k < 4; ++k)
  6966. CHECK (std::abs (l[(std::size_t) at[k]] - c.v[k]) < 2e-7 + 1e-5 * std::abs (c.v[k]));
  6967. }
  6968. }
  6969. TEST (harmless_stereo_tremolo_and_vibrato)
  6970. {
  6971. // tremolo d = 1: each side swings between 0 and 1.25 at 5 Hz, L and R in anti-phase; vibrato modulates the pitch
  6972. lynx::SynthSettings s;
  6973. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  6974. s.cutoff = 20000.0f;
  6975. s.stereoTremolo = 1.0f;
  6976. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  6977. inst->prepare (48000.0);
  6978. lynx::ChannelDef c;
  6979. c.synth = s;
  6980. inst->configure (c, 120.0);
  6981. inst->noteOn (69, 1.0f);
  6982. std::vector<float> l (48000), r (48000);
  6983. inst->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
  6984. 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; };
  6985. // θ = 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
  6986. CHECK (envelope (l, 19200) > 4.0f * envelope (r, 19200));
  6987. CHECK (envelope (r, 24000) > 4.0f * envelope (l, 24000));
  6988. // vibrato 100 cents: zero crossings vary in spacing; without it they are regular
  6989. auto crossingsSpread = [] (float cents) {
  6990. lynx::SynthSettings v;
  6991. v.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  6992. v.cutoff = 20000.0f;
  6993. v.vibratoCents = cents;
  6994. const auto x = renderSynthNote (v, 69, 48000);
  6995. std::vector<int> zc;
  6996. for (int i = 24000; i < 48000; ++i)
  6997. if (x[(std::size_t) i - 1] < 0.0f && x[(std::size_t) i] >= 0.0f)
  6998. zc.push_back (i);
  6999. int lo = 1 << 30, hi = 0;
  7000. for (std::size_t k = 1; k < zc.size(); ++k)
  7001. lo = std::min (lo, zc[k] - zc[k - 1]), hi = std::max (hi, zc[k] - zc[k - 1]);
  7002. return hi - lo;
  7003. };
  7004. CHECK (crossingsSpread (0.0f) <= 1);
  7005. CHECK (crossingsSpread (100.0f) >= 4);
  7006. }
  7007. TEST (physical_models_sound_and_end)
  7008. {
  7009. // Sakura: a plucked string with default-ish parameters rings and fades after the release; Drumpad: a hit decays
  7010. for (const auto kind : { lynx::InstrumentKind::sakura, lynx::InstrumentKind::drumpad })
  7011. {
  7012. lynx::ChannelDef c;
  7013. c.instrument = kind;
  7014. if (kind == lynx::InstrumentKind::sakura)
  7015. {
  7016. c.modelParams.assign (146, 0.5f);
  7017. c.modelParams[111] = 3.0f; // both strings at the note
  7018. c.modelParams[113] = 0.5f; // octave ×1
  7019. c.modelParams[6] = c.modelParams[11] = 0.99f; // long decay
  7020. c.modelParams[31] = 0.0f; // noise exciter
  7021. }
  7022. else
  7023. {
  7024. c.modelParams.assign (38, 0.5f);
  7025. c.modelParams[0] = 1.0f; // retrigger noise
  7026. c.modelParams[30] = c.modelParams[31] = c.modelParams[32] = c.modelParams[33] = 0.0f; // no velocity morph
  7027. }
  7028. auto inst = lynx::makeInstrument (kind);
  7029. inst->prepare (48000.0);
  7030. inst->configure (c, 120.0);
  7031. inst->noteOn (60, 0.8f);
  7032. std::vector<float> l (48000), r (48000);
  7033. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  7034. inst->noteOff (60);
  7035. inst->render (l.data() + 24000, r.data() + 24000, 24000, 1.0f, 1.0f);
  7036. double early = 0.0;
  7037. for (int i = 0; i < 12000; ++i)
  7038. early += (double) l[(std::size_t) i] * l[(std::size_t) i];
  7039. CHECK (early > 1e-6);
  7040. CHECK (std::all_of (l.begin(), l.end(), [] (float v) { return std::isfinite (v); }));
  7041. std::vector<float> tail (480000), tailR (480000);
  7042. inst->render (tail.data(), tailR.data(), 480000, 1.0f, 1.0f); // 10 s later the voice has ended
  7043. CHECK (inst->idle());
  7044. }
  7045. }
  7046. TEST (squeeze_matches_fl_model)
  7047. {
  7048. // Fruity Squeeze: Steps quantises, Mix 0 passes the input dry ×Volume/127; a silent block is left untouched
  7049. auto slot = lynx::makeEffectSlot (lynx::EffectKind::squeeze);
  7050. slot.params[1] = 0.0f; // mix 0: dry only
  7051. auto fx = lynx::makeEffect (lynx::EffectKind::squeeze);
  7052. fx->prepare (44100.0);
  7053. std::vector<float> l (512, 0.3f), r (512, -0.2f);
  7054. fx->process (l.data(), r.data(), 512, slot.params, 120.0);
  7055. CHECK (std::abs (l[100] - 0.3f) < 1e-6f && std::abs (r[100] + 0.2f) < 1e-6f); // Volume 127/127
  7056. slot.params[1] = 1.0f; // wet only
  7057. slot.params[2] = 32000.0f / 32768.0f; // 32768^(1 − 32000/32768) ≈ 1.27 levels per unit → heavy quantisation
  7058. std::vector<float> a (512), b (512);
  7059. for (int i = 0; i < 512; ++i)
  7060. a[(std::size_t) i] = b[(std::size_t) i] = 0.4f * (float) std::sin (i * 0.05);
  7061. fx->process (a.data(), b.data(), 512, slot.params, 120.0);
  7062. const auto levels = std::pow (32768.0f, 1.0f - 32000.0f / 32768.0f);
  7063. for (int i = 0; i < 512; i += 37)
  7064. CHECK (std::abs (a[(std::size_t) i] * levels - std::nearbyint (a[(std::size_t) i] * levels)) < 1e-3f);
  7065. std::vector<float> z (512, 0.0f), z2 (512, 0.0f);
  7066. fx->process (z.data(), z2.data(), 512, slot.params, 120.0);
  7067. CHECK (z[10] == 0.0f);
  7068. }
  7069. TEST (sakura_matrix_and_effects)
  7070. {
  7071. // REA_rodada29 §1: velocity → level follows 10^(2m)·0.05 (vel 1 vs 0.5 → ×10 in amplitude); the delay leaves
  7072. // an echo after the note has ended and keeps the instrument from going idle until its tail is silent
  7073. auto base = [] {
  7074. lynx::ChannelDef c;
  7075. c.instrument = lynx::InstrumentKind::sakura;
  7076. c.modelParams.assign (146, 0.0f);
  7077. const std::pair<int, float> values[] { { 0, 0.375f }, { 1, 0.5f }, { 3, 0.5f }, { 4, 1.0f }, { 6, 0.95f }, { 9, 0.3f },
  7078. { 11, 0.95f }, { 14, 0.3f }, { 18, 0.5f }, { 19, 0.5f }, { 27, 0.5f }, { 28, 1.0f },
  7079. { 29, 0.7f }, { 31, 1.0f }, { 32, 0.5f }, { 111, 3.0f }, { 113, 0.5f } };
  7080. for (const auto& [i, v] : values)
  7081. c.modelParams[(std::size_t) i] = v;
  7082. return c;
  7083. };
  7084. auto render = [] (const lynx::ChannelDef& c, float velocity, int held, int total) {
  7085. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sakura);
  7086. inst->prepare (48000.0);
  7087. inst->configure (c, 120.0);
  7088. inst->noteOn (60, velocity);
  7089. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  7090. for (int s = 0; s < total; s += 256)
  7091. {
  7092. if (s == held)
  7093. inst->noteOff (60);
  7094. inst->render (l.data() + s, r.data() + s, std::min (256, total - s), 1.0f, 1.0f);
  7095. }
  7096. return std::make_pair (l, std::move (inst));
  7097. };
  7098. auto energy = [] (const std::vector<float>& x, int from, int to) {
  7099. double e = 0.0;
  7100. for (int i = from; i < to; ++i)
  7101. e += (double) x[(std::size_t) i] * x[(std::size_t) i];
  7102. return e;
  7103. };
  7104. auto c = base();
  7105. c.modelParams[79] = 6.0f; // slot 1: velocity → level, amount 1
  7106. c.modelParams[80] = 1.0f;
  7107. c.modelParams[82] = 1.0f;
  7108. const auto loud = energy (render (c, 1.0f, 24064, 24064).first, 0, 24064);
  7109. const auto soft = energy (render (c, 0.5f, 24064, 24064).first, 0, 24064);
  7110. CHECK (loud > 1e-6 && std::abs (loud / soft - 100.0) < 1.0);
  7111. auto d = base();
  7112. auto [dry, dryInst] = render (d, 0.8f, 4864, 72000);
  7113. d.modelParams[63] = 1.0f; // delay on: time 0.5 → ≈ 0.52 s, wet 1, no feedback: one echo
  7114. d.modelParams[64] = 0.5f;
  7115. d.modelParams[69] = 1.0f;
  7116. auto [wet, wetInst] = render (d, 0.8f, 4864, 72000);
  7117. CHECK (energy (wet, 26000, 40000) > 100.0 * energy (dry, 26000, 40000) + 1e-9); // the note ends near 0.2 s
  7118. CHECK (dryInst->idle() || energy (dry, 70000, 72000) < 1e-12);
  7119. CHECK (! wetInst->idle()); // the echo tail has not been silent for 3 s yet
  7120. }
  7121. TEST (drumsynth_live_engine)
  7122. {
  7123. // REA_rodada30 §1: key → patch slot, state parsing, the engine against our reference model (no DLL: synthesised
  7124. // sources only, noise from the fallback seed), voice modes, patch level/pan
  7125. CHECK (lynx::drumSynthKeyToSlot (48) == 0 && lynx::drumSynthKeyToSlot (71) == 23 && lynx::drumSynthKeyToSlot (0) == 24);
  7126. CHECK (lynx::drumSynthKeyToSlot (47) == 71 && lynx::drumSynthKeyToSlot (72) == 72 && lynx::drumSynthKeyToSlot (119) == 119);
  7127. CHECK (lynx::drumSynthKeyToSlot (120) == -1);
  7128. // a version 3 state: 120 names, 120 × 38 ints (send track −1 becomes 0 below version 4), flags
  7129. std::vector<std::uint8_t> blob;
  7130. auto put = [&] (std::uint32_t v) { for (int k = 0; k < 4; ++k) blob.push_back ((std::uint8_t) (v >> (8 * k))); };
  7131. put (3);
  7132. put (66);
  7133. for (int i = 0; i < 120; ++i)
  7134. blob.push_back (2), blob.push_back ('a'), blob.push_back ((std::uint8_t) ('0' + i % 10));
  7135. for (int i = 0; i < 120 * 38; ++i)
  7136. put ((std::uint32_t) (i % 38 == 36 ? -1 : i % 38 == 4 ? 300 : 0));
  7137. put (0);
  7138. const auto parsed = lynx::drumSynthLiveParams (blob);
  7139. CHECK (parsed && (int) parsed->size() == lynx::drumSynthParamCount);
  7140. CHECK (parsed && (*parsed)[36] == 0.0f && (*parsed)[38 * 7 + 4] == 300.0f && (*parsed)[(std::size_t) lynx::drumSynthEditorKeyIndex] == 66.0f);
  7141. blob.resize (500);
  7142. CHECK (! lynx::drumSynthLiveParams (blob));
  7143. // patch on key 60 (slot 12): tone-mode Osc1 with sweep and click, noise-band Osc2, ring, 2 retriggers
  7144. 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,
  7145. 0, 0, 0, 0, 0, 0, 30, -1, 0, 0, 64 };
  7146. lynx::ChannelDef c;
  7147. c.instrument = lynx::InstrumentKind::drumsynth;
  7148. c.modelParams.assign ((std::size_t) lynx::drumSynthParamCount, 0.0f);
  7149. for (int s = 0; s < 120; ++s)
  7150. c.modelParams[(std::size_t) (38 * s + 15)] = c.modelParams[(std::size_t) (38 * s + 34)] = -1.0f;
  7151. for (int i = 0; i < 38; ++i)
  7152. c.modelParams[(std::size_t) (38 * 12 + i)] = (float) patch[i];
  7153. auto render = [] (const lynx::ChannelDef& def, std::vector<int> keys, int total, float gl = 1.0f, float gr = 1.0f) {
  7154. auto inst = lynx::makeInstrument (lynx::InstrumentKind::drumsynth);
  7155. inst->configure (def, 120.0);
  7156. inst->prepare (48000.0);
  7157. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  7158. for (int s = 0; s < total; s += 512)
  7159. {
  7160. for (std::size_t k = 0; k + 1 < keys.size(); k += 2)
  7161. if (keys[k + 1] == s)
  7162. inst->noteOn (keys[k], 100.0f / 128.0f);
  7163. inst->render (l.data() + s, r.data() + s, std::min (512, total - s), gl, gr);
  7164. }
  7165. return std::make_tuple (l, r, std::move (inst));
  7166. };
  7167. {
  7168. auto [l, r, inst] = render (c, { 60, 0 }, 9600);
  7169. // reference model (REA round 30 dsl_model.py, seed 0x56CE, FL velocity 100, V = 1, centre: × g(100)·cos(π/4))
  7170. const std::pair<int, float> golden[] { { 0, -0.0074092713f }, { 10, -0.13961405f }, { 100, -0.47290668f }, { 1000, -0.29188696f },
  7171. { 3000, -0.22130175f }, { 6000, -0.049537566f }, { 9599, 0.010392102f } };
  7172. for (const auto& [i, v] : golden)
  7173. CHECK (std::abs (l[(std::size_t) i] - v) < 2e-6f && std::abs (r[(std::size_t) i] - v) < 2e-6f);
  7174. std::vector<float> tail (48000 * 4), tailR (tail.size());
  7175. inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
  7176. CHECK (inst->idle()); // the engine reported the voice finished
  7177. }
  7178. auto energy = [] (const std::vector<float>& x, int from, int to) {
  7179. double e = 0.0;
  7180. for (int i = from; i < to; ++i)
  7181. e += (double) x[(std::size_t) i] * x[(std::size_t) i];
  7182. return e;
  7183. };
  7184. // a key without a patch level (slot 0, level 0) and a key outside the 120 patches are silent
  7185. CHECK (energy (std::get<0> (render (c, { 48, 0, 125, 0 }, 4096)), 0, 4096) == 0.0);
  7186. // mono: a second hit on the same key cuts the first; poly: both ring together (Osc2 off: no noise, deterministic)
  7187. auto tone = c;
  7188. tone.modelParams[38 * 12 + 14] = 0.0f;
  7189. const auto single = std::get<0> (render (tone, { 60, 0 }, 8192));
  7190. const auto mono = std::get<0> (render (tone, { 60, 0, 60, 2048 }, 8192));
  7191. auto poly = tone;
  7192. poly.modelParams[38 * 12 + 24] = 1.0f;
  7193. const auto both = std::get<0> (render (poly, { 60, 0, 60, 2048 }, 8192));
  7194. double monoDiff = 0.0, polyDiff = 0.0;
  7195. for (int i = 2048 + 300; i < 8192; ++i) // after the cut fade: mono = only the second hit, a copy of the first
  7196. 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]);
  7197. CHECK (monoDiff < 1e-3 && polyDiff > 1.0);
  7198. // patch pan +64 → right only; channel volume counts twice (FL applies Final.Vol in the engine and at the host)
  7199. auto right = c;
  7200. right.modelParams[38 * 12 + 1] = 64.0f;
  7201. const auto [pl, pr, pi] = render (right, { 60, 0 }, 4096);
  7202. CHECK (energy (pl, 0, 4096) < 1e-12 * energy (pr, 0, 4096) && energy (pr, 0, 4096) > 1.0);
  7203. const auto full = std::get<0> (render (tone, { 60, 0 }, 4096));
  7204. const auto half = std::get<0> (render (tone, { 60, 0 }, 4096, 0.5f, 0.5f));
  7205. CHECK (std::abs (half[100] - 0.25f * full[100]) < 1e-6f && std::abs (full[100]) > 0.01f);
  7206. }
  7207. TEST (kepler_lfo_bpm_sync)
  7208. {
  7209. // 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)
  7210. std::vector<std::uint8_t> state (0x21 + 34 * 4, 0);
  7211. auto putU32 = [&] (std::size_t o, std::uint32_t v) { std::memcpy (state.data() + o, &v, 4); };
  7212. putU32 (12, 9);
  7213. std::memcpy (state.data() + 16, "Kepler-1", 9);
  7214. putU32 (0x19, 4);
  7215. putU32 (0x1D, 34);
  7216. float k[34] {};
  7217. k[5] = 0.602085769f; // "Pads - Kepler" demo: n = 286
  7218. k[6] = 1.0f;
  7219. k[10] = 1.0f; // vibrato on
  7220. k[33] = 1.0f;
  7221. std::memcpy (state.data() + 0x21, k, sizeof k);
  7222. lynx::SynthSettings s;
  7223. float gain = 0.0f;
  7224. CHECK (lynx::synthFromKepler (state, s, gain));
  7225. CHECK (s.lfo[lynx::lfoPitch].sync && s.lfo[lynx::lfoPitch].secondsTiming);
  7226. CHECK (std::abs (s.lfo[lynx::lfoPitch].period - 286.0f / 192.0f) < 1e-6f);
  7227. k[5] = 0.0f; // slowest: 32 beats
  7228. std::memcpy (state.data() + 0x21, k, sizeof k);
  7229. CHECK (lynx::synthFromKepler (state, s, gain) && std::abs (s.lfo[lynx::lfoPitch].period - 32.0f) < 1e-6f);
  7230. k[6] = 0.0f; // free rate in seconds
  7231. std::memcpy (state.data() + 0x21, k, sizeof k);
  7232. CHECK (lynx::synthFromKepler (state, s, gain) && ! s.lfo[lynx::lfoPitch].sync);
  7233. }
  7234. namespace
  7235. {
  7236. // A Harmor state written from scratch (REA_rodada11 §4.1 layout, version 18): part A only — a decaying-harmonics
  7237. // timbre, low-pass + resonance, filter envelope, 3-voice unison, vibrato, pluck, tremolo, volume envelope with a
  7238. // velocity map — and the global block. Every other curve is neutral.
  7239. // features (REA_rodada75): 1 arpeggio markers on the volume envelope, 2 noise resonance on filter 1, 4 "pluck replaces
  7240. // blur decay", 8 pluck blend, 16 harmonic randomness, 32 phaser + harmonizer masks
  7241. std::vector<std::uint8_t> harmorTestState (bool panLfo = true, bool image = false, int features = 0)
  7242. {
  7243. std::vector<std::uint8_t> b;
  7244. 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))); };
  7245. 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))); };
  7246. struct Pt { double dx, y, tension; int mode; int arp = 0; };
  7247. auto curve = [&] (const std::vector<Pt>& pts, std::array<std::int32_t, 5> hdr) {
  7248. i32 (3), i32 (3), i32 ((std::int32_t) pts.size());
  7249. for (const auto& p : pts)
  7250. {
  7251. f64 (p.dx), f64 (p.y);
  7252. const auto t = (float) p.tension;
  7253. std::uint32_t u;
  7254. std::memcpy (&u, &t, 4);
  7255. i32 ((std::int32_t) u);
  7256. b.push_back ((std::uint8_t) p.mode), b.push_back ((std::uint8_t) p.arp), b.push_back (0), b.push_back (0);
  7257. }
  7258. for (auto h : hdr)
  7259. i32 (h);
  7260. };
  7261. const std::array<std::int32_t, 5> off { 0, 0, -1, -1, -1 }, on { 0, 1, -1, -1, -1 };
  7262. 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); };
  7263. i32 (18);
  7264. i32 (1 | 4);
  7265. std::vector<std::uint8_t> part;
  7266. std::swap (b, part);
  7267. std::array<std::int32_t, 386> p {};
  7268. const std::pair<int, int> values[] = { { 0, 500 }, { 3, 1000 }, { 9, 1024 }, { 11, 1000 }, { 12, 1000 }, { 17, 300 }, { 18, 4000 },
  7269. { 19, 500 }, { 20, 1000 }, { 24, 500 }, { 25, 500 }, { 28, 500 }, { 29, 256 }, { 30, -1 }, { 31, -1 }, { 33, -1 }, { 34, 3 },
  7270. { 35, 1 }, { 37, 192 }, { 38, 128 }, { 39, 500 }, { 40, 10000 }, { 41, 1 }, { 42, 10000 }, { 43, 1 }, { 45, 200 }, { 46, 500 },
  7271. { 47, 1 }, { 49, -140 }, { 50, 1200 }, { 51, 0 }, { 52, 5000 }, { 54, 500 }, { 56, 600 }, { 57, 300 }, { 59, 500 }, { 60, 600 },
  7272. { 64, 8192 }, { 66, 500 }, { 72, 600 }, { 74, 1000 }, { 75, 600 }, { 78, 5000 }, { 79, 500 }, { 80, 3000 } };
  7273. for (const auto& [i, v] : values)
  7274. p[(std::size_t) i] = v;
  7275. if ((features & 2) != 0)
  7276. p[57] = 400, p[59] = 300; // filter-1 resonance amount and width (noise type set in the combo block)
  7277. if ((features & 4) != 0)
  7278. p[75] = 700, p[21] = 300; // pluck value, time-blur attack
  7279. if ((features & 8) != 0)
  7280. p[75] = 650, p[4] = 600; // pluck, timbre mix
  7281. if ((features & 32) != 0)
  7282. p[76] = 700, p[27] = 600; // phaser mix, harmonizer mix
  7283. if (image) // image synthesis (REA_rodada75): speed, cubic gain / linear freq interpolation, Hz freq mode, formant
  7284. for (const auto& [i, v] : std::initializer_list<std::pair<int, int>> { { 82, 200 }, { 83, 100000 }, { 84, 10000 }, { 85, 20000 },
  7285. { 86, 750 }, { 87, 4 }, { 88, 600 }, { 89, 900 }, { 90, 1 }, { 91, 3 }, { 92, 1500 }, { 93, -300 }, { 94, 800 } })
  7286. p[(std::size_t) i] = v;
  7287. for (auto v : p)
  7288. i32 (v);
  7289. for (int i = 0; i < 12; ++i)
  7290. i32 (i); // module order
  7291. std::array<std::uint8_t, 0x4b> misc {};
  7292. auto put = [&] (int o, std::int32_t v) { std::memcpy (misc.data() + o, &v, 4); };
  7293. const float one = 1.0f;
  7294. put (0x08, 107), put (0x1c, 1), put (0x28, 200), std::memcpy (misc.data() + 0x30, &one, 4);
  7295. put (0x34, 0), put (0x38, 4), put (0x3c, (features & 2) != 0 ? 109 : 100), put (0x40, 108);
  7296. if ((features & 8) != 0)
  7297. put (0x00, 5);
  7298. misc[0x44] = 1;
  7299. if ((features & 4) != 0)
  7300. misc[0x46] = 1;
  7301. b.insert (b.end(), misc.begin(), misc.end());
  7302. 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 };
  7303. for (int k = 0; k < 43; ++k)
  7304. {
  7305. static const int mode2[] = { 2, 14, 23, 28, 30, 38, 39, 41, 42 }; // ×2 around 0.5: 1.0 is neutral
  7306. const bool centred = std::find (std::begin (mode1), std::end (mode1), k) != std::end (mode1);
  7307. const bool doubled = std::find (std::begin (mode2), std::end (mode2), k) != std::end (mode2);
  7308. const double neutral = centred && ! doubled ? 0.5 : 1.0;
  7309. i32 (3);
  7310. i32 (1065353216), i32 (1065353216), i32 (1065353216);
  7311. if (k == 1 && (features & 1) != 0) // arpeggio volume envelope: a looping pattern with chord markers
  7312. curve ({ { 0, 0, 0, 0, 2 }, { 0.005, 1, 0.3, 0 }, { 0.08, 0.2, -0.4, 0, 3 }, { 0.005, 1, 0.2, 0 }, { 0.08, 0.1, 0, 0, 1 },
  7313. { 0.1, 0, 0.2, 0 } },
  7314. { 0, 1, -1, 0, 4 });
  7315. else if (k == 1) // volume envelope: attack, decay to the sustain point, release
  7316. 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 });
  7317. else if (k == 4) // filter-1 envelope
  7318. curve ({ { 0, 1, 0, 0 }, { 0.25, 0.2, -0.5, 0 } }, { 0, 1, -1, -1, -1 });
  7319. else
  7320. flat (neutral);
  7321. for (int a : { 128, 128, 0, 128 })
  7322. i32 (a);
  7323. if (k == 0 && panLfo) // pan LFO
  7324. flat (1.0, on);
  7325. else
  7326. flat (0.5);
  7327. for (int a : { k == 0 ? 40000 : 0, 0, 0, 0 })
  7328. i32 (a);
  7329. i32 (0);
  7330. for (int m = 2; m < 11; ++m)
  7331. if (k == 1 && m == 3)
  7332. curve ({ { 0, 0.2, 0, 0 }, { 1, 1, -0.3, 0 } }, off); // velocity → volume
  7333. else
  7334. flat (neutral);
  7335. }
  7336. for (int c = 0; c < 26; ++c)
  7337. if ((features & 16) != 0 && c == 4)
  7338. curve ({ { 0, 0.5, 0, 0 }, { 0.2, 0.8, 0.3, 0 }, { 0.8, 0.3, 0, 0 } }, off); // harmonic randomness
  7339. else if ((features & 32) != 0 && (c == 22 || c == 23))
  7340. curve ({ { 0, 0.3, 0, 0 }, { 0.6, 1.0, 0.2, 0 }, { 0.4, 0.5, 0, 0 } }, off); // phaser / harmonizer masks
  7341. else if (c == 3 && (features & 8) != 0)
  7342. curve ({ { 0, 0.2, 0, 0 }, { 1, 0.05, 0.4, 0 } }, off); // timbre 2 for the pluck blend
  7343. else if (c == 2 || c == 3)
  7344. curve ({ { 0, 0.25, 0, 0 }, { 0.006, 0.12, -0.46, 0 }, { 0.994, 0, 0, 0 } }, off); // timbres
  7345. else if (c == 14)
  7346. curve ({ { 0, 1, 0, 0 }, { 1, 0, 0, 0 } }, off); // pluck curve
  7347. else
  7348. flat (c == 0 || c == 17 ? 0.25 : (c == 5 ? 0.0 : (c == 6 || c == 10 || c == 11 || (c >= 18 && c <= 25) ? 1.0 : 0.5)));
  7349. if (image)
  7350. {
  7351. // the image object (FUN_00793300 layout): version 0, image mode, looping, 6 columns × 40 rows, gain + freq planes
  7352. const int cols = 6, rows = 40;
  7353. i32 (0), i32 (0), i32 (1), i32 (0);
  7354. f64 (-1.0), f64 (-1.0), f64 (0.0);
  7355. i32 (cols), i32 (rows);
  7356. i32 (cols * rows);
  7357. for (int c2 = 0; c2 < cols; ++c2)
  7358. for (int r2 = 0; r2 < rows; ++r2)
  7359. {
  7360. const auto v = (float) (255.0 * std::pow (0.93, r2) * (0.6 + 0.4 * std::cos (0.9 * c2 + 0.37 * r2)));
  7361. std::uint32_t u;
  7362. std::memcpy (&u, &v, 4);
  7363. i32 ((std::int32_t) u);
  7364. }
  7365. i32 (cols * rows);
  7366. for (int c2 = 0; c2 < cols; ++c2)
  7367. for (int r2 = 0; r2 < rows; ++r2)
  7368. {
  7369. const auto v = (float) (0.3 * std::sin (1.3 * c2 + 0.5 * r2));
  7370. std::uint32_t u;
  7371. std::memcpy (&u, &v, 4);
  7372. i32 ((std::int32_t) u);
  7373. }
  7374. i32 (0);
  7375. b.push_back (0); // (file name: empty)
  7376. }
  7377. std::swap (b, part);
  7378. i32 ((std::int32_t) part.size());
  7379. b.insert (b.end(), part.begin(), part.end());
  7380. std::array<std::int32_t, 53> g {};
  7381. g[0] = g[1] = g[2] = 256, g[10] = 500, g[11] = 1000, g[12] = 1000, g[15] = 1000;
  7382. for (auto v : g)
  7383. i32 (v);
  7384. for (int i = 0; i < 6; ++i)
  7385. i32 (i);
  7386. std::array<std::uint8_t, 0x3d> gm {};
  7387. const std::int32_t cr = 200, vr = 1;
  7388. std::memcpy (gm.data() + 12, &vr, 4);
  7389. std::memcpy (gm.data() + 24, &cr, 4);
  7390. b.insert (b.end(), gm.begin(), gm.end());
  7391. return b;
  7392. }
  7393. } // namespace
  7394. TEST (harmor_engine)
  7395. {
  7396. // REA_rodada34: state parsing, the additive engine against our reference model (hmodel.py), host levels, voice end
  7397. const auto blob = harmorTestState();
  7398. if (const char* dump = std::getenv ("LYNX_DUMP_HARMOR"))
  7399. std::ofstream (dump, std::ios::binary).write ((const char*) blob.data(), (std::streamsize) blob.size());
  7400. CHECK (lynx::harmorStateUsable (blob));
  7401. CHECK (lynx::harmorFallbacks (blob).empty());
  7402. const auto at = lynx::harmorParamOffset (blob, 0, 52);
  7403. std::int32_t cutoff = 0;
  7404. CHECK (at > 0 && (std::memcpy (&cutoff, blob.data() + at, 4), cutoff == 5000));
  7405. CHECK (lynx::harmorParamOffset (blob, 1, 52) < 0 && lynx::harmorParamOffset (blob, -1, 12) > at);
  7406. auto broken = blob;
  7407. broken.resize (broken.size() / 2);
  7408. CHECK (! lynx::harmorStateUsable (broken));
  7409. auto old = blob;
  7410. old[0] = 15; // articulators and curves are stored from version 16
  7411. CHECK (! lynx::harmorStateUsable (old));
  7412. lynx::ChannelDef c;
  7413. c.instrument = lynx::InstrumentKind::harmor;
  7414. c.pluginState = blob;
  7415. auto render = [] (const lynx::ChannelDef& def, int total, float gl, float gr, int offAt) {
  7416. auto inst = lynx::makeInstrument (lynx::InstrumentKind::harmor);
  7417. inst->configure (def, 120.0);
  7418. inst->prepare (44100.0);
  7419. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  7420. inst->noteOn (60, 0.78125f);
  7421. for (int s = 0; s < total; s += 512)
  7422. {
  7423. if (s == offAt)
  7424. inst->noteOff (60);
  7425. inst->render (l.data() + s, r.data() + s, std::min (512, total - s), gl, gr);
  7426. }
  7427. return std::make_tuple (l, r, std::move (inst));
  7428. };
  7429. {
  7430. // reference model (REA round 34 hmodel.py; C5 at FL velocity 100, Final.Vol 1, centre, released at 6144, the
  7431. // Mersenne Twister seeded like LYNX's)
  7432. auto [l, r, inst] = render (c, 8192, 1.0f, 1.0f, 6144);
  7433. const std::tuple<int, float, float> golden[] {
  7434. { 0, 0.0f, 0.0f }, { 100, 0.00947693f, 0.04249846f }, { 1000, 0.08140575f, -0.03251422f }, { 3000, 0.05928446f, -0.00247572f },
  7435. { 6000, 0.02997839f, 0.14549091f }, { 7000, -0.01121871f, 0.04197952f }, { 8191, 0.00314545f, 0.03857505f } };
  7436. for (const auto& [i, gl, gr] : golden)
  7437. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  7438. // after the release the volume envelope ends, the record declicks and the instrument goes idle
  7439. std::vector<float> tail (44100), tailR (tail.size());
  7440. inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
  7441. CHECK (inst->idle());
  7442. CHECK (std::abs (tail.back()) == 0.0f);
  7443. }
  7444. // the reference renderer gives the same samples as the instrument
  7445. {
  7446. const auto ref = lynx::harmor::renderReference (blob, { { 0, 60, 0.78125, 1.0, 0.0, 6144 } }, 44100.0, 8192);
  7447. const auto [l, r, inst] = render (c, 8192, 1.0f, 1.0f, 6144);
  7448. double worst = 0.0;
  7449. for (int i = 0; i < 8192; ++i)
  7450. 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]) });
  7451. CHECK (worst == 0.0);
  7452. }
  7453. // FL's level law applied once: half the channel volume halves the output; full right pan silences the left side
  7454. const auto full = std::get<0> (render (c, 2048, 1.0f, 1.0f, -1));
  7455. const auto half = std::get<0> (render (c, 2048, 0.5f, 0.5f, -1));
  7456. CHECK (std::abs (half[1000] - 0.5f * full[1000]) < 1e-6f && std::abs (full[1000]) > 0.01f);
  7457. auto mono = c;
  7458. mono.pluginState = harmorTestState (false); // no pan LFO ...
  7459. const std::int32_t one = 1;
  7460. std::memcpy (mono.pluginState.data() + lynx::harmorParamOffset (mono.pluginState, 0, 34), &one, 4); // ... nor unison spread
  7461. const auto [pl, pr, pi] = render (mono, 2048, 0.0f, 1.41421356f, -1);
  7462. double el = 0.0, er = 0.0;
  7463. for (int i = 0; i < 2048; ++i)
  7464. el += (double) pl[(std::size_t) i] * pl[(std::size_t) i], er += (double) pr[(std::size_t) i] * pr[(std::size_t) i];
  7465. CHECK (el < 1e-10 * er && er > 1e-3);
  7466. }
  7467. namespace
  7468. {
  7469. // An Ogun state written from scratch (REA_rodada37 §1.3 layout, version 18; zlib with stored blocks): richness 4
  7470. // (255 partials), pre-decay, decay/release, fullness, Seed 1 + fixed Seed 2 with morph, 3-voice unison, a 2-stage
  7471. // low-pass with an LFO on its cutoff, a volume envelope with a sustain point and a velocity map, declick, normalisation,
  7472. // Impulse/Decay maps, and every plugin effect (EQ band, chorus, ping-pong delay, reverb). Everything else is neutral.
  7473. std::vector<std::uint8_t> ogunTestState()
  7474. {
  7475. std::vector<std::uint8_t> b;
  7476. 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))); };
  7477. auto f32v = [&] (float v) { std::uint32_t u; std::memcpy (&u, &v, 4); i32 ((std::int32_t) u); };
  7478. 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))); };
  7479. struct Pt { double dx, y, tension; int mode; };
  7480. auto curve = [&] (int type, const std::vector<Pt>& pts, std::array<std::int32_t, 5> hdr) {
  7481. i32 (type), i32 (3), i32 ((std::int32_t) pts.size());
  7482. for (const auto& p : pts)
  7483. {
  7484. f64 (p.dx), f64 (p.y), f32v ((float) p.tension);
  7485. b.push_back ((std::uint8_t) p.mode), b.push_back (0), b.push_back (0), b.push_back (0);
  7486. }
  7487. for (auto h : hdr)
  7488. i32 (h);
  7489. };
  7490. const std::array<std::int32_t, 5> off { 0, 0, -1, -1, -1 }, on { 0, 1, -1, -1, -1 };
  7491. auto flat = [&] (int type, double y) { curve (type, { { 0, y, 0, 0 }, { 1, y, 0, 0 } }, off); };
  7492. std::array<std::int32_t, 64> P {};
  7493. const std::pair<int, int> values[] = { { 0, 128 }, { 5, 300 }, { 6, 3000 }, { 7, 2000 }, { 8, 200 }, { 9, 500 }, { 10, 12 },
  7494. { 11, 34 }, { 12, 3 }, { 13, 64 }, { 14, 64 }, { 15, 40 }, { 16, 64 }, { 17, 180 }, { 18, 60 }, { 19, 2 }, { 20, 300 },
  7495. { 21, 400 }, { 22, 40 }, { 23, 30 }, { 24, 20 }, { 25, 64 }, { 26, 40 }, { 27, 96 }, { 28, 100 }, { 29, 64 }, { 30, 2 },
  7496. { 31, 19 }, { 32, 221 }, { 33, 0 }, { 34, 50 }, { 35, 100 }, { 36, 20 }, { 37, 60 }, { 38, 2 }, { 39, 64 }, { 40, 300 },
  7497. { 43, 30000 }, { 46, 20000 }, { 49, 0 }, { 50, 2 }, { 51, 0 }, { 52, 9 }, { 53, 1 }, { 54, 0 },
  7498. { 55, 0x01000000 }, // +0x20b: pitch follows the key
  7499. { 56, 0x00010000 }, // +0x20e: declick
  7500. { 62, 0x00000100 } }; // +0x225: normalise
  7501. for (const auto& [i, v] : values)
  7502. P[(std::size_t) i] = v;
  7503. for (auto v : P)
  7504. i32 (v);
  7505. b.push_back (0), b.push_back (1), b.push_back (0); // delay: not tempo-based, on
  7506. i32 (1), i32 (1), i32 (1); // EQ band types: peaking
  7507. b.push_back (0); // comment: 0 characters
  7508. for (int k = 0; k < 13; ++k)
  7509. {
  7510. static constexpr int sum[] = { 1, 2, 3, 9, 11, 12 };
  7511. const double neutral = std::find (std::begin (sum), std::end (sum), k) != std::end (sum) ? 0.5 : 1.0;
  7512. i32 (1);
  7513. f32v (0.0f), f32v (1.0f), f32v (k == 1 ? 0.3f : 0.0f);
  7514. if (k == 0) // volume envelope: attack, decay to the sustain point, release
  7515. 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 });
  7516. else
  7517. flat (1, neutral);
  7518. for (int a : { 128, 128, 128, 128 })
  7519. i32 (a);
  7520. if (k == 1) // cutoff LFO
  7521. curve (2, { { 0, 0.8, 0, 0 } }, on);
  7522. else
  7523. flat (2, 0.5);
  7524. for (int a : { k == 1 ? 40000 : 0, 0, 0, 0 })
  7525. i32 (a);
  7526. i32 (0);
  7527. for (int m = 0; m < 4; ++m)
  7528. if (k == 0 && m == 1)
  7529. curve (3, { { 0, 0.3, 0, 0 }, { 1, 1, -0.2, 0 } }, off); // velocity → volume
  7530. else
  7531. flat (3, neutral);
  7532. }
  7533. flat (3, 0.0); // Forced: none
  7534. curve (3, { { 0, 0.6, 0, 0 }, { 1, 0.4, 0.3, 0 } }, off); // Impulse EQ
  7535. curve (3, { { 0, 0.5, 0, 0 }, { 1, 0.3, 0, 0 } }, off); // Decay EQ
  7536. flat (3, 0.5); // Release EQ
  7537. // u32 version + zlib (stored blocks)
  7538. std::vector<std::uint8_t> s { 18, 0, 0, 0, 0x78, 0x01 };
  7539. for (std::size_t i = 0; i < b.size();)
  7540. {
  7541. const auto len = std::min<std::size_t> (65535, b.size() - i);
  7542. s.push_back (i + len >= b.size() ? 1 : 0);
  7543. s.push_back ((std::uint8_t) len), s.push_back ((std::uint8_t) (len >> 8));
  7544. s.push_back ((std::uint8_t) ~len), s.push_back ((std::uint8_t) (~len >> 8));
  7545. s.insert (s.end(), b.begin() + (std::ptrdiff_t) i, b.begin() + (std::ptrdiff_t) (i + len));
  7546. i += len;
  7547. }
  7548. std::uint32_t a1 = 1, a2 = 0;
  7549. for (auto c : b)
  7550. a1 = (a1 + c) % 65521u, a2 = (a2 + a1) % 65521u;
  7551. for (int sh = 24; sh >= 0; sh -= 8)
  7552. s.push_back ((std::uint8_t) (((a2 << 16) | a1) >> sh));
  7553. return s;
  7554. }
  7555. } // namespace
  7556. TEST (ogun_engine)
  7557. {
  7558. // REA_rodada37: state parsing, the engine against our reference model (ogmodel.py), host levels, export quality
  7559. const auto blob = ogunTestState();
  7560. if (const char* dump = std::getenv ("LYNX_DUMP_OGUN"))
  7561. std::ofstream (dump, std::ios::binary).write ((const char*) blob.data(), (std::streamsize) blob.size());
  7562. CHECK (lynx::ogunStateUsable (blob));
  7563. CHECK (lynx::ogunFallbacks (blob).empty());
  7564. CHECK (lynx::ogunParam (blob, 17) == 180 && lynx::ogunParam (blob, 52) == 9 && ! lynx::ogunParam (blob, 64));
  7565. auto broken = blob;
  7566. broken.resize (broken.size() - 300);
  7567. CHECK (! lynx::ogunStateUsable (broken));
  7568. auto old = blob;
  7569. old[0] = 13; // the articulator layout is from version 14
  7570. CHECK (! lynx::ogunStateUsable (old));
  7571. // reference model (REA round 37 ogmodel.Plugin(state).run: C5 velocity 0.75 released at 6144, G5 velocity 0.625 at 2048
  7572. // released at 8192; host block 512)
  7573. 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 } };
  7574. const auto ref = lynx::ogun::renderReference (blob, notes, 44100.0, 16384);
  7575. CHECK (ref.size() == 2 * 16384);
  7576. if (const char* dump = std::getenv ("LYNX_DUMP_OGUN_OUT"))
  7577. std::ofstream (dump, std::ios::binary).write ((const char*) ref.data(), (std::streamsize) (ref.size() * 4));
  7578. const std::tuple<int, float, float> golden[] {
  7579. { 100, -7.763202e-05f, -8.919523e-05f }, { 1000, 0.06195267f, 0.03858230f }, { 3000, 0.01363476f, -0.00378433f },
  7580. { 6000, -0.06601223f, -0.03084030f }, { 9000, 0.04204506f, 0.02452896f }, { 12000, 0.03420147f, -0.01837870f },
  7581. { 16383, -0.00465311f, -0.00119627f } };
  7582. for (const auto& [i, gl, gr] : golden)
  7583. CHECK (std::abs (ref[2 * (std::size_t) i] - gl) < 2e-6f && std::abs (ref[2 * (std::size_t) i + 1] - gr) < 2e-6f);
  7584. // the instrument plays like the reference renderer (Final.Vol 1, centre)
  7585. lynx::ChannelDef c;
  7586. c.instrument = lynx::InstrumentKind::ogun;
  7587. c.pluginState = blob;
  7588. auto render = [&] (const lynx::ChannelDef& def, int total, float gl, float gr, bool held = false) {
  7589. auto inst = lynx::makeInstrument (lynx::InstrumentKind::ogun);
  7590. inst->configure (def, 120.0);
  7591. inst->prepare (44100.0);
  7592. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  7593. for (int s = 0; s < total; s += 512)
  7594. {
  7595. if (s == 0)
  7596. inst->noteOn (60, 0.75f);
  7597. if (s == 2048)
  7598. inst->noteOn (67, 0.625f);
  7599. if (s == 6144 && ! held)
  7600. inst->noteOff (60);
  7601. if (s == 8192 && ! held)
  7602. inst->noteOff (67);
  7603. inst->render (l.data() + s, r.data() + s, std::min (512, total - s), gl, gr);
  7604. }
  7605. return std::make_tuple (l, r, std::move (inst));
  7606. };
  7607. {
  7608. auto [l, r, inst] = render (c, 16384, 1.0f, 1.0f);
  7609. double worst = 0.0;
  7610. for (int i = 0; i < 16384; ++i)
  7611. 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]) });
  7612. CHECK (worst == 0.0);
  7613. // the voices end and the effect tails die out: the instrument goes idle
  7614. std::vector<float> tail (44100), tailR (tail.size());
  7615. for (int k = 0; k < 20 && ! inst->idle(); ++k)
  7616. inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
  7617. CHECK (inst->idle());
  7618. }
  7619. // FL's level law applied once (effects off): half the channel volume halves the output once the gain ramps
  7620. // (limited to 0.001 per sample) have settled on the sustain level; full right pan silences the left side
  7621. {
  7622. auto dry = c;
  7623. for (int i : { 19, 29, 40, 53 })
  7624. CHECK (lynx::ogunSetParam (dry.pluginState, i, 0)); // chorus, delay wet, EQ band and reverb off
  7625. CHECK (lynx::ogunStateUsable (dry.pluginState) && lynx::ogunParam (dry.pluginState, 19) == 0);
  7626. const auto full = std::get<0> (render (dry, 32768, 1.0f, 1.0f, true));
  7627. const auto half = std::get<0> (render (dry, 32768, 0.5f, 0.5f, true));
  7628. double worst = 0.0, peak = 0.0;
  7629. for (int i = 30000; i < 32768; ++i)
  7630. worst = std::max (worst, (double) std::abs (half[(std::size_t) i] - 0.5f * full[(std::size_t) i])),
  7631. peak = std::max (peak, (double) std::abs (full[(std::size_t) i]));
  7632. CHECK (worst <= 1e-7 * peak && peak > 1e-3);
  7633. // FL's default channel volume (100/128): FL hands Ogun Final.Vol = (11^v − 1)/10 (REA_rodada28 §4.1); LYNX's
  7634. // centred channel gains (√2·V·cos(π/4) each) give the reference model's level at that Final.Vol
  7635. const auto V = lynx::channelGain (0.78125f);
  7636. const auto g = V * std::cos (0.25f * 3.14159265f) * 1.41421356f;
  7637. const auto atDefault = std::get<0> (render (dry, 32768, g, g, true));
  7638. const auto refDefault = lynx::ogun::renderReference (dry.pluginState, { { 0, 60, 0.75, V, 0.0, 0.0, 0.0, 0.0, 0.5, -1 },
  7639. { 2048, 67, 0.625, V, 0.0, 0.0, 0.0, 0.0, 0.5, -1 } }, 44100.0, 32768);
  7640. double ei = 0.0, er0 = 0.0;
  7641. for (int i = 30000; i < 32768; ++i)
  7642. 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];
  7643. CHECK (er0 > 0.0 && std::abs (10.0 * std::log10 (ei / er0)) < 0.001);
  7644. const auto [pl, pr, pi] = render (dry, 4096, 0.0f, 1.41421356f);
  7645. double el = 0.0, er = 0.0;
  7646. for (int i = 0; i < 4096; ++i)
  7647. el += (double) pl[(std::size_t) i] * pl[(std::size_t) i], er += (double) pr[(std::size_t) i] * pr[(std::size_t) i];
  7648. CHECK (el < 1e-10 * er && er > 1e-3);
  7649. }
  7650. // export: FL renders Ogun at quality 4 (×2 rate, 32-point reader), LYNX too while it renders offline
  7651. {
  7652. lynx::setOfflineRendering (true);
  7653. auto [l, r, inst] = render (c, 8192, 1.0f, 1.0f);
  7654. lynx::setOfflineRendering (false);
  7655. lynx::ogun::RefOptions q4;
  7656. q4.quality = 4;
  7657. const auto ref4 = lynx::ogun::renderReference (blob, notes, 44100.0, 8192, q4);
  7658. double worst = 0.0, diff = 0.0;
  7659. for (int i = 0; i < 8192; ++i)
  7660. worst = std::max (worst, (double) std::abs (ref4[2 * (std::size_t) i] - l[(std::size_t) i])),
  7661. diff = std::max (diff, (double) std::abs (ref[2 * (std::size_t) i] - l[(std::size_t) i]));
  7662. CHECK (worst == 0.0 && diff > 1e-3);
  7663. const std::tuple<int, float, float> golden4[] {
  7664. { 100, 0.00605961f, 0.00628969f }, { 1000, 0.03729841f, 0.01727954f }, { 3000, -0.09010383f, -0.02551294f },
  7665. { 6000, 0.02167798f, -0.01115632f }, { 8191, 0.00320288f, -0.00615792f } };
  7666. for (const auto& [i, gl, gr] : golden4)
  7667. CHECK (std::abs (ref4[2 * (std::size_t) i] - gl) < 2e-6f && std::abs (ref4[2 * (std::size_t) i + 1] - gr) < 2e-6f);
  7668. }
  7669. // feature variants of the same state against the reference model (energy per side over 8192 samples, three
  7670. // samples; C1 released at 6144): qualities, richness, unison, the filter types, ALT DECAY, 1/√h, Seed 2 Random,
  7671. // infinite decay, logarithmic axis, velocity → position, other rates. Goldens from ogmodel.py (REA round 37).
  7672. {
  7673. struct Sample { int at; float l, r; };
  7674. struct Variant { const char* name; std::vector<std::pair<int, int>> params; int rate; double el, er; Sample s[3]; };
  7675. const Variant variants[] {
  7676. { "q1", { { 54, 1 } }, 44100, 11.0970402, 6.87820714, { { 1500, 0.0240363069f, -0.0148524037f }, { 4000, 0.0873190612f, -0.000715861097f }, { 7000, -0.0378603041f, -0.00338819111f } } },
  7677. { "q2", { { 54, 2 } }, 44100, 11.2405446, 6.68045082, { { 1500, 0.0448660068f, -0.00560745923f }, { 4000, -0.0122657465f, 0.0104285963f }, { 7000, -0.0216034129f, -0.0134632047f } } },
  7678. { "q3", { { 54, 3 } }, 44100, 11.3200608, 6.86196449, { { 1500, -0.0031537537f, -0.0242648814f }, { 4000, 0.0426301025f, 0.0364697985f }, { 7000, 0.00843803957f, 0.0302239582f } } },
  7679. { "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 } } },
  7680. { "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 } } },
  7681. { "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 } } },
  7682. { "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 } } },
  7683. { "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 } } },
  7684. { "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 } } },
  7685. { "alt", { { 62, 257 } }, 44100, 17.2485711, 9.99223979, { { 1500, -0.0319156498f, -0.0186850764f }, { 4000, -0.0769012421f, -0.0375530645f }, { 7000, 0.0310976505f, 0.0445244014f } } },
  7686. { "isqrt", { { 62, 65792 } }, 44100, 45.7309307, 27.1224593, { { 1500, -0.04376591f, -0.00845711026f }, { 4000, 0.0351300463f, -0.129557073f }, { 7000, 0.0096148327f, 0.00123740174f } } },
  7687. { "seed2rnd", { { 11, 0 } }, 44100, 16.8228819, 11.5433061, { { 1500, -0.0117614279f, -0.026497297f }, { 4000, 0.00265733898f, -0.07069581f }, { 7000, 0.00576663949f, 0.0565217957f } } },
  7688. { "decinf", { { 6, 10000 } }, 44100, 19.1879226, 11.0487311, { { 1500, -0.0324105881f, -0.0190765262f }, { 4000, -0.0809780285f, -0.0395730771f }, { 7000, 0.034197174f, 0.048585102f } } },
  7689. { "logx", { { 57, 16777216 } }, 44100, 12.3064326, 7.31519289, { { 1500, -0.0438990779f, 0.0096983267f }, { 4000, -0.0175748877f, 0.0313935578f }, { 7000, 0.0639200211f, 0.024419222f } } },
  7690. { "velpos", { { 56, 66304 } }, 44100, 4.9966925, 3.16540662, { { 1500, 0.0083851777f, -0.0165472236f }, { 4000, 0.0630451962f, 0.0156494901f }, { 7000, -0.0293043777f, -0.00182877108f } } },
  7691. { "fs48", { }, 48000, 10.8813888, 6.53592373, { { 1500, -0.0485184453f, -0.0319588967f }, { 4000, 0.0230421089f, -0.0286604818f }, { 7000, -0.0117630213f, -0.017459048f } } },
  7692. { "fs96", { }, 96000, 11.0189683, 6.39821883, { { 1500, -0.0267860368f, -0.0227564126f }, { 4000, 0.0238709711f, -0.0100849615f }, { 7000, -0.0034619756f, 0.0451687202f } } },
  7693. };
  7694. for (const auto& v : variants)
  7695. {
  7696. auto st = blob;
  7697. for (const auto& [i, x] : v.params)
  7698. CHECK (lynx::ogunSetParam (st, i, x));
  7699. const auto y = lynx::ogun::renderReference (st, { { 0, 60, 0.75, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 6144 },
  7700. { 2048, 67, 0.625, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1 } }, (double) v.rate, 8192);
  7701. double el = 0.0, er = 0.0, peak = 0.0;
  7702. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  7703. 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]) });
  7704. bool ok = y.size() == 2 * 8192 && std::abs (el - v.el) <= 1e-5 * v.el && std::abs (er - v.er) <= 1e-5 * v.er;
  7705. for (const auto& smp : v.s)
  7706. 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;
  7707. if (! ok)
  7708. std::printf (" Ogun variant %s differs from the model: energy %.9g %.9g (model %.9g %.9g)\n", v.name, el, er, v.el, v.er);
  7709. CHECK (ok);
  7710. }
  7711. }
  7712. }
  7713. // our own Maximus v7 state (layout REA_rodada8 §1.1): 7 globals, 4 × 13 band parameters, 8 envelopes (dx, y, tension, type)
  7714. static std::vector<std::uint8_t> maximusTestState (const std::array<int, 7>& g, const std::array<std::array<int, 13>, 4>& bands,
  7715. const std::array<std::vector<std::array<double, 4>>, 8>& envs, int oversampling)
  7716. {
  7717. std::vector<std::uint8_t> d;
  7718. auto put = [&] (const void* v, std::size_t n) { d.insert (d.end(), (const std::uint8_t*) v, (const std::uint8_t*) v + n); };
  7719. auto i32 = [&] (std::int32_t v) { put (&v, 4); };
  7720. i32 (7);
  7721. for (int copy = 0; copy < 2; ++copy)
  7722. {
  7723. for (auto v : g)
  7724. i32 (v);
  7725. for (const auto& b : bands)
  7726. for (auto v : b)
  7727. i32 (v);
  7728. }
  7729. d.resize (0x1fe, 0);
  7730. std::memcpy (d.data() + 0x1f4, &oversampling, 4);
  7731. for (const auto& e : envs)
  7732. {
  7733. i32 (3), i32 (2), i32 ((std::int32_t) e.size());
  7734. for (const auto& pt : e)
  7735. {
  7736. const double dx = pt[0], y = pt[1];
  7737. const float t = (float) pt[2];
  7738. put (&dx, 8), put (&y, 8), put (&t, 4);
  7739. const std::uint8_t flags[4] { (std::uint8_t) pt[3], 0, 0, 0 };
  7740. put (flags, 4);
  7741. }
  7742. i32 (0), i32 (-1), i32 (-1), i32 (-1), i32 (-1);
  7743. }
  7744. return d;
  7745. }
  7746. // A "Kepler-1" state written from scratch (REA_rodada46 §1.1 layout, version 2): the default panel values
  7747. std::vector<std::uint8_t> keplerTestState()
  7748. {
  7749. 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 };
  7750. std::vector<std::uint8_t> st;
  7751. auto u32 = [&] (std::uint32_t v) { for (int i = 0; i < 4; ++i) st.push_back ((std::uint8_t) (v >> (8 * i))); };
  7752. auto f32 = [&] (float f) { std::uint32_t v; std::memcpy (&v, &f, 4); u32 (v); };
  7753. u32 (0), u32 (0), u32 (0), u32 (9);
  7754. for (char c : std::string ("Kepler-1"))
  7755. st.push_back ((std::uint8_t) c);
  7756. st.push_back (0);
  7757. u32 (2), u32 (34);
  7758. for (auto f : panel)
  7759. f32 (f);
  7760. st.push_back (0);
  7761. f32 (1.0f), f32 (0.5f);
  7762. const auto size = (std::uint32_t) st.size() - 12;
  7763. std::memcpy (st.data() + 4, &size, 4);
  7764. return st;
  7765. }
  7766. std::string keplerDll()
  7767. {
  7768. if (realFlpDir.empty())
  7769. return {};
  7770. return (std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "Kepler" / "engine_x64.dll").string();
  7771. }
  7772. TEST (kepler_engine)
  7773. {
  7774. const auto st = keplerTestState();
  7775. CHECK (st.size() == 178 && lynx::keplerStateUsable (st));
  7776. CHECK (lynx::keplerPanelOffset (st, 19) == 0x21 + 4 * 19 && lynx::keplerPanelOffset (st, 34) == -1);
  7777. auto broken = st;
  7778. broken[17] = 'X'; // not "Kepler-1"
  7779. CHECK (! lynx::keplerStateUsable (broken) && lynx::keplerPanelOffset (broken, 0) == -1);
  7780. auto v3 = st;
  7781. v3[0x19] = 3; // version 3 needs the gate / noise / oversampling bytes
  7782. CHECK (! lynx::keplerStateUsable (v3));
  7783. const auto dll = keplerDll();
  7784. if (dll.empty() || ! lynx::keplerDllUsable (dll))
  7785. return; // no FL installation (the engine needs the user's own DLL tables)
  7786. // against FL's engine DLL (EXEC, REA_rodada46 §6): three notes, 44.1 kHz, host blocks of 512 — bit-exact
  7787. 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 } };
  7788. const auto y = lynx::kepler::renderReference (st, dll, notes, 44100.0, 39690);
  7789. CHECK (y.size() == 2 * 39690);
  7790. if (y.size() != 2 * 39690)
  7791. return;
  7792. double el = 0.0, er = 0.0;
  7793. for (std::size_t i = 0; i < y.size(); i += 2)
  7794. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  7795. std::printf (" Kepler default patch: energy %.9g %.9g (FL's DLL 250.641268)\n", el, er);
  7796. CHECK (std::abs (el - 250.641268) < 1e-6 && std::abs (er - 250.641268) < 1e-6);
  7797. const std::tuple<int, float, float> golden[] { { 5000, 0.0467273332f, 0.0467273332f }, { 12000, -0.119412214f, -0.119412214f },
  7798. { 25000, 0.0855825394f, 0.0855825394f }, { 35000, -3.34166579e-08f, -3.34166579e-08f } };
  7799. for (const auto& [i, l, r] : golden)
  7800. CHECK (y[2 * (std::size_t) i] == l && y[2 * (std::size_t) i + 1] == r);
  7801. // the instrument: FL's wrapper velocity, channel volume as an output gain, idle after the release
  7802. lynx::ChannelDef c;
  7803. c.instrument = lynx::InstrumentKind::kepler;
  7804. c.pluginState = st;
  7805. c.samplePath = dll;
  7806. auto inst = lynx::makeInstrument (lynx::InstrumentKind::kepler);
  7807. inst->configure (c, 120.0);
  7808. inst->prepare (44100.0);
  7809. std::vector<float> l (4096), r (4096);
  7810. inst->noteOn (60, 0.8f);
  7811. double e = 0.0;
  7812. bool finite = true;
  7813. for (int b = 0; b < 8; ++b)
  7814. {
  7815. std::fill (l.begin(), l.end(), 0.0f);
  7816. std::fill (r.begin(), r.end(), 0.0f);
  7817. inst->render (l.data(), r.data(), 4096, 0.5f, 0.5f);
  7818. for (int i = 0; i < 4096; ++i)
  7819. 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];
  7820. if (b == 3)
  7821. inst->noteOff (60);
  7822. }
  7823. CHECK (finite && e > 1.0);
  7824. for (int b = 0; b < 40; ++b)
  7825. inst->render (l.data(), r.data(), 4096, 0.5f, 0.5f);
  7826. CHECK (inst->idle());
  7827. }
  7828. // REA round 57 §2: Transistor Bass
  7829. // the plugin state (event 0xD5) of an FL preset file (.fst: the FLhd/FLdt event stream)
  7830. std::vector<std::uint8_t> tbassPresetState (const char* name)
  7831. {
  7832. if (realFlpDir.empty())
  7833. return {};
  7834. std::ifstream in (std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "Transistor Bass" / name, std::ios::binary);
  7835. const std::vector<std::uint8_t> b ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  7836. std::size_t i = 0;
  7837. while (i + 8 <= b.size() && std::memcmp (b.data() + i, "FLdt", 4) != 0)
  7838. ++i;
  7839. i += 8;
  7840. while (i < b.size())
  7841. {
  7842. const auto id = b[i++];
  7843. std::size_t n = id < 64 ? 1 : id < 128 ? 2 : id < 192 ? 4 : 0;
  7844. if (id >= 192)
  7845. for (int shift = 0; i < b.size(); shift += 7)
  7846. {
  7847. const auto c = b[i++];
  7848. n |= (std::size_t) (c & 0x7F) << shift;
  7849. if (c < 0x80)
  7850. break;
  7851. }
  7852. if (id == 0xD5 && i + n <= b.size())
  7853. return { b.begin() + (std::ptrdiff_t) i, b.begin() + (std::ptrdiff_t) (i + n) };
  7854. i += n;
  7855. }
  7856. return {};
  7857. }
  7858. // the same state re-saved as engine-stream version 5 with an engine selector (REA_rodada57 §2.2)
  7859. std::vector<std::uint8_t> tbassWithEngine (const std::vector<std::uint8_t>& s, std::int32_t engine)
  7860. {
  7861. std::vector<std::uint8_t> out (s.begin(), s.begin() + 4);
  7862. auto put = [&] (const void* p, std::size_t n) { out.insert (out.end(), (const std::uint8_t*) p, (const std::uint8_t*) p + n); };
  7863. for (std::size_t at = 4; at + 12 <= s.size();)
  7864. {
  7865. std::uint32_t id;
  7866. std::uint64_t n;
  7867. std::memcpy (&id, s.data() + at, 4);
  7868. std::memcpy (&n, s.data() + at + 4, 8);
  7869. std::vector<std::uint8_t> d (s.begin() + (std::ptrdiff_t) (at + 12), s.begin() + (std::ptrdiff_t) (at + 12 + n));
  7870. at += 12 + (std::size_t) n;
  7871. if (id == 2)
  7872. {
  7873. std::uint64_t m;
  7874. std::memcpy (&m, d.data() + 4, 8);
  7875. std::vector<std::uint8_t> e (d.begin() + 12, d.begin() + 12 + (std::ptrdiff_t) m);
  7876. std::int16_t v;
  7877. std::memcpy (&v, e.data(), 2);
  7878. if (v < 5)
  7879. {
  7880. v = 5;
  7881. std::memcpy (e.data(), &v, 2);
  7882. e.insert (e.end(), 4, 0);
  7883. }
  7884. std::memcpy (e.data() + e.size() - 4, &engine, 4);
  7885. m = e.size();
  7886. d.resize (12);
  7887. std::memcpy (d.data() + 4, &m, 8);
  7888. d.insert (d.end(), e.begin(), e.end());
  7889. }
  7890. const std::uint64_t size = d.size();
  7891. put (&id, 4);
  7892. put (&size, 8);
  7893. put (d.data(), d.size());
  7894. }
  7895. return out;
  7896. }
  7897. std::string tbassDll()
  7898. {
  7899. if (realFlpDir.empty())
  7900. return {};
  7901. return (std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "Transistor Bass" / "engine_x64.dll").string();
  7902. }
  7903. // Plucked! (REA_rodada65 §1): the C++ engine against FL's DLL (EXEC: same notes, the DLL's RandSeed set before the first
  7904. // note, the host killing voices when the plugin asks). LYNX renders these bit-identically to the DLL.
  7905. TEST (plucked_engine)
  7906. {
  7907. auto stateOf = [] (std::initializer_list<std::int32_t> k) {
  7908. std::vector<std::uint8_t> st (20);
  7909. std::memcpy (st.data(), std::data (k), 20);
  7910. return st;
  7911. };
  7912. CHECK (! lynx::pluckedStateUsable (std::vector<std::uint8_t> (19)));
  7913. CHECK (lynx::pluckedStateUsable (stateOf ({ 128, 64, 1, 0, 1 })));
  7914. CHECK (lynx::pluckedParams (stateOf ({ 7, 9, 0, 1, 0 })) == std::vector<int> ({ 7, 9, 0, 1, 0 }));
  7915. using N = lynx::plucked::RefNote;
  7916. struct Sample { int at; float l, r; };
  7917. struct Golden { int set; std::int32_t P[5]; double fs; int block, law, maxPoly; double el, er; Sample s[4]; };
  7918. const std::vector<std::vector<N>> sets {
  7919. { { 0, 60, 1.0, 0.0, 0.0, 0.0, 0.0, -1 } },
  7920. { { 0, 60, 1.0, 0.0, 0.0, 0.0, 0.0, 30000 }, { 5000, 67, 0.6, -0.4, 13.0, 0.0, 0.0, 20000 } },
  7921. { { 0, 48, 0.8, 0.5, 0.0, 0.1, -0.2, 22050 }, { 100, 52, 0.8, 0.0, 0.0, 0.0, 0.0, 22050 }, { 200, 55, 0.8, -1.0, -50.0, 0.0, 0.0, 22050 } },
  7922. { { 0, 24, 1.0, 0.0, 0.0, 0.0, 0.0, -1 }, { 3000, 100, 1.0, 0.2, 0.0, 0.0, 0.0, -1 } },
  7923. { { 0, 72, 1.0, 0.0, 0.0, -0.3, 0.6, -1 }, { 77, 84, 0.5, 0.0, 0.0, 0.0, 0.0, -1 } },
  7924. };
  7925. const Golden goldens[] {
  7926. { 0, { 128, 64, 1, 0, 1 }, 44100, 512, 0, 0, 455.867624, 313.77463, { { 1000, -0.1914210170507431f, -0.2735292315483093f }, { 10000, -0.037063151597976685f, 0.00110959867015481f }, { 30000, -0.009626023471355438f, -0.001515754614956677f }, { 60000, 0.0f, 0.0f } } },
  7927. { 1, { 128, 64, 1, 1, 1 }, 44100, 512, 1, 0, 377.99866, 237.526831, { { 1000, -0.11432178318500519f, -0.14189107716083527f }, { 10000, 0.08135657012462616f, 0.027565963566303253f }, { 30000, 0.0f, 0.0f }, { 60000, 0.0f, 0.0f } } },
  7928. { 2, { 60, 20, 0, 1, 0 }, 48000, 256, 1, 2, 1570.38626, 550.991338, { { 1000, 0.4817328155040741f, 0.19528728723526f }, { 10000, -0.34781914949417114f, -0.15463846921920776f }, { 30000, 0.0f, 0.0f }, { 60000, 0.0f, 0.0f } } },
  7929. { 3, { 256, 128, 1, 0, 0 }, 44100, 1000, 1, 0, 2007.37978, 2089.11642, { { 1000, 0.3288099467754364f, 0.3288099467754364f }, { 10000, 0.04112838953733444f, 0.05718187615275383f }, { 30000, -0.08563447743654251f, -0.08728141337633133f }, { 60000, -0.0894850566983223f, -0.09222060441970825f } } },
  7930. { 4, { 0, 0, 1, 0, 1 }, 96000, 512, 0, 0, 222.608052, 243.338078, { { 1000, -0.20827248692512512f, -0.13200917840003967f }, { 10000, 0.01809507980942726f, 0.013305095955729485f }, { 30000, 0.0f, 0.0f }, { 60000, 0.0f, 0.0f } } },
  7931. };
  7932. for (const auto& g : goldens)
  7933. {
  7934. const auto st = stateOf ({ g.P[0], g.P[1], g.P[2], g.P[3], g.P[4] });
  7935. const auto length = (int) g.fs * 2;
  7936. const auto y = lynx::plucked::renderReference (st, sets[(std::size_t) g.set], g.fs, length, g.block, 777u + (std::uint32_t) g.set,
  7937. g.law != 0, g.maxPoly);
  7938. double el = 0.0, er = 0.0;
  7939. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  7940. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  7941. bool ok = y.size() == 2 * (std::size_t) length && std::abs (el - g.el) <= 1e-7 * g.el && std::abs (er - g.er) <= 1e-7 * g.er;
  7942. for (const auto& smp : g.s)
  7943. ok = ok && std::abs (y[2 * (std::size_t) smp.at] - smp.l) <= 1e-7f && std::abs (y[2 * (std::size_t) smp.at + 1] - smp.r) <= 1e-7f;
  7944. if (! ok)
  7945. std::printf (" Plucked! case %d differs from FL's DLL: energy %.9g %.9g (DLL %.9g %.9g)\n", g.set, el, er, g.el, g.er);
  7946. CHECK (ok);
  7947. }
  7948. // the instrument: plays, the gate makes a note-off silence the string, a held string dies out by itself
  7949. lynx::ChannelDef c;
  7950. c.instrument = lynx::InstrumentKind::plucked;
  7951. c.pluginState = stateOf ({ 200, 40, 1, 1, 1 });
  7952. auto inst = lynx::makeInstrument (lynx::InstrumentKind::plucked);
  7953. inst->configure (c, 120.0);
  7954. inst->prepare (44100.0);
  7955. std::vector<float> l (1024), r (1024);
  7956. inst->noteOn (57, 0.8f);
  7957. double e = 0.0;
  7958. for (int b = 0; b < 8; ++b)
  7959. {
  7960. std::fill (l.begin(), l.end(), 0.0f);
  7961. std::fill (r.begin(), r.end(), 0.0f);
  7962. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  7963. for (int i = 0; i < 1024; ++i)
  7964. e += (double) l[(std::size_t) i] * l[(std::size_t) i] + (double) r[(std::size_t) i] * r[(std::size_t) i];
  7965. }
  7966. CHECK (e > 1.0 && ! inst->idle());
  7967. inst->noteOff (57);
  7968. for (int b = 0; b < 4; ++b)
  7969. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  7970. CHECK (inst->idle());
  7971. c.pluginState = stateOf ({ 0, 64, 0, 0, 0 });
  7972. inst->configure (c, 120.0);
  7973. inst->noteOn (60, 1.0f);
  7974. inst->noteOff (60); // (no gate: the string rings on)
  7975. for (int b = 0; b < 2000 && ! inst->idle(); ++b)
  7976. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  7977. CHECK (inst->idle());
  7978. }
  7979. // BeepMap (REA_rodada65 §2): the C++ engine against FL's DLL (EXEC: same notes, the DLL's RandSeed set before the first
  7980. // note, the host killing voices when the plugin asks): a synthetic 9×6 bitmap with every Scale mode and switch, and two
  7981. // of the installation's presets. LYNX renders these bit-identically to the DLL.
  7982. std::vector<std::uint8_t> beepMapTestState (std::initializer_list<std::int32_t> settings)
  7983. {
  7984. constexpr int W = 9, H = 6;
  7985. std::vector<std::uint8_t> img (54 + 4 * W * H, 0);
  7986. auto put = [&] (std::size_t at, std::uint32_t v, int bytes) { for (int i = 0; i < bytes; ++i) img[at + (std::size_t) i] = (std::uint8_t) (v >> (8 * i)); };
  7987. img[0] = 'B', img[1] = 'M';
  7988. put (2, (std::uint32_t) img.size(), 4), put (10, 54, 4), put (14, 40, 4), put (18, W, 4), put (22, H, 4), put (26, 1, 2), put (28, 32, 2);
  7989. put (34, 4 * W * H, 4);
  7990. for (int y = 0; y < H; ++y) // bottom-up rows
  7991. for (int x = 0; x < W; ++x)
  7992. {
  7993. const auto yy = H - 1 - y;
  7994. auto* px = img.data() + 54 + 4 * (y * W + x);
  7995. px[0] = (std::uint8_t) ((x * 91 + yy * 29) & 255), px[1] = (std::uint8_t) ((x * 13 + yy * 71) & 255);
  7996. px[2] = (std::uint8_t) ((x * 37 + yy * 11) & 255), px[3] = 255;
  7997. }
  7998. std::vector<std::uint8_t> st (4 + 60 + 4, 0);
  7999. std::size_t k = 0;
  8000. for (auto v : settings)
  8001. std::memcpy (st.data() + 4 + 4 * k++, &v, 4);
  8002. const auto len = (std::int32_t) img.size();
  8003. std::memcpy (st.data() + 64, &len, 4);
  8004. st.insert (st.end(), img.begin(), img.end());
  8005. return st;
  8006. }
  8007. // the 0xD5 state of one of the installation's BeepMap presets (empty without an installation)
  8008. std::vector<std::uint8_t> beepMapPresetState (const char* name)
  8009. {
  8010. if (realFlpDir.empty())
  8011. return {};
  8012. std::ifstream in (std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "BeepMap" / name, std::ios::binary);
  8013. const std::vector<std::uint8_t> b ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  8014. std::size_t i = 0;
  8015. while (i + 8 <= b.size() && std::memcmp (b.data() + i, "FLdt", 4) != 0)
  8016. ++i;
  8017. i += 8;
  8018. while (i < b.size())
  8019. {
  8020. const auto id = b[i++];
  8021. std::size_t n = id < 64 ? 1 : id < 128 ? 2 : id < 192 ? 4 : 0;
  8022. if (id >= 192)
  8023. for (int shift = 0; i < b.size(); shift += 7)
  8024. {
  8025. const auto c = b[i++];
  8026. n |= (std::size_t) (c & 0x7F) << shift;
  8027. if (c < 0x80)
  8028. break;
  8029. }
  8030. if (id == 0xD5 && i + n <= b.size())
  8031. return { b.begin() + (std::ptrdiff_t) i, b.begin() + (std::ptrdiff_t) (i + n) };
  8032. i += n;
  8033. }
  8034. return {};
  8035. }
  8036. TEST (beepmap_engine)
  8037. {
  8038. const auto synthA = beepMapTestState ({ 3000, 200, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64 });
  8039. CHECK (lynx::beepMapStateUsable (synthA));
  8040. CHECK (lynx::beepMapParams (synthA)->at (1) == 200 && lynx::beepMapParamOffset (synthA, 2) == 12);
  8041. auto broken = synthA;
  8042. broken[0] = 2; // versions above 1 are refused
  8043. CHECK (! lynx::beepMapStateUsable (broken));
  8044. broken = synthA;
  8045. broken.resize (broken.size() - 20); // truncated bitmap
  8046. CHECK (! lynx::beepMapStateUsable (broken));
  8047. using N = lynx::beepmap::RefNote;
  8048. struct Sample { int at; float l, r; };
  8049. struct Golden { int set; double fs; int block; double el, er; Sample s[4]; };
  8050. std::vector<std::vector<std::uint8_t>> states {
  8051. synthA,
  8052. beepMapTestState ({ 5000, 600, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 64 }),
  8053. beepMapTestState ({ 8000, 100, 2, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 64 }),
  8054. beepMapTestState ({ 1000, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 64 }),
  8055. beepMapPresetState ("Default.fst"),
  8056. beepMapPresetState ("Voice.fst"),
  8057. };
  8058. const std::vector<std::vector<N>> sets {
  8059. { { 0, 60, 1.0, 0.0, 0.0, -1 } },
  8060. { { 0, 57, 0.8, 0.3, 25.0, -1 }, { 7000, 64, 0.6, -0.5, 0.0, -1 } },
  8061. { { 0, 48, 1.0, 0.0, 0.0, -1 }, { 300, 72, 0.5, 0.0, -40.0, -1 } },
  8062. { { 0, 67, 1.0, -0.2, 0.0, -1 } },
  8063. { { 0, 60, 1.0, 0.0, 0.0, -1 } },
  8064. { { 0, 55, 0.8, 0.0, 0.0, -1 }, { 20000, 62, 0.8, 0.4, 0.0, -1 } },
  8065. };
  8066. const Golden goldens[] {
  8067. { 0, 44100, 512, 70.02738, 69.0877973, { { 1000, 0.030967699363827705f, -0.218118816614151f }, { 10000, 0.0f, 0.0f }, { 30000, 0.0f, 0.0f }, { 60000, 0.0f, 0.0f } } },
  8068. { 1, 48000, 256, 2814.53446, 3514.46151, { { 1000, 0.02367428131401539f, 0.06526239961385727f }, { 10000, -0.06188536435365677f, 0.062191300094127655f }, { 30000, -0.153680220246315f, -0.27799028158187866f }, { 60000, 0.03855478763580322f, 0.1298033744096756f } } },
  8069. { 2, 44100, 1000, 6644.01869, 7606.70378, { { 1000, 0.06019101291894913f, 0.2049546241760254f }, { 10000, -0.852304220199585f, -0.13111725449562073f }, { 30000, 0.11700113117694855f, -0.6130681037902832f }, { 60000, -0.2660403549671173f, -0.09453065693378448f } } },
  8070. { 3, 96000, 512, 51.7332962, 28.9913476, { { 1000, -0.025792766362428665f, -0.07221365720033646f }, { 10000, 0.0f, 0.0f }, { 30000, 0.0f, 0.0f }, { 60000, 0.0f, 0.0f } } },
  8071. { 4, 44100, 512, 8142.09413, 5639.86691, { { 1000, 0.050016239285469055f, 0.05289545655250549f }, { 10000, -0.258514404296875f, -0.16095803678035736f }, { 30000, 0.765974223613739f, 0.477584570646286f }, { 60000, 0.040680620819330215f, 0.17371401190757751f } } },
  8072. { 5, 48000, 512, 2568.60856, 3975.24938, { { 1000, 0.07109857350587845f, 0.07109857350587845f }, { 10000, -0.057797715067863464f, -0.057797715067863464f }, { 30000, 0.08789601176977158f, 0.04657084494829178f }, { 60000, 0.08154996484518051f, 0.12709519267082214f } } },
  8073. };
  8074. for (const auto& g : goldens)
  8075. {
  8076. const auto& st = states[(std::size_t) g.set];
  8077. if (st.empty())
  8078. continue; // (no FL installation: the presets are skipped)
  8079. const auto length = (int) g.fs * 2;
  8080. const auto y = lynx::beepmap::renderReference (st, sets[(std::size_t) g.set], g.fs, length, g.block, 4321u + (std::uint32_t) g.set);
  8081. double el = 0.0, er = 0.0;
  8082. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  8083. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  8084. bool ok = y.size() == 2 * (std::size_t) length && std::abs (el - g.el) <= 1e-7 * g.el && std::abs (er - g.er) <= 1e-7 * g.er;
  8085. for (const auto& smp : g.s)
  8086. ok = ok && std::abs (y[2 * (std::size_t) smp.at] - smp.l) <= 1e-7f && std::abs (y[2 * (std::size_t) smp.at + 1] - smp.r) <= 1e-7f;
  8087. if (! ok)
  8088. std::printf (" BeepMap case %d differs from FL's DLL: energy %.9g %.9g (DLL %.9g %.9g)\n", g.set, el, er, g.el, g.er);
  8089. CHECK (ok);
  8090. }
  8091. // the instrument: plays the image, ends after the last column, a note-off fades the voice out
  8092. lynx::ChannelDef c;
  8093. c.instrument = lynx::InstrumentKind::beepmap;
  8094. c.pluginState = states[1];
  8095. auto inst = lynx::makeInstrument (lynx::InstrumentKind::beepmap);
  8096. inst->configure (c, 120.0);
  8097. inst->prepare (44100.0);
  8098. std::vector<float> l (1024), r (1024);
  8099. inst->noteOn (60, 0.8f);
  8100. double e = 0.0;
  8101. for (int b = 0; b < 8; ++b)
  8102. {
  8103. std::fill (l.begin(), l.end(), 0.0f);
  8104. std::fill (r.begin(), r.end(), 0.0f);
  8105. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  8106. for (int i = 0; i < 1024; ++i)
  8107. e += (double) l[(std::size_t) i] * l[(std::size_t) i] + (double) r[(std::size_t) i] * r[(std::size_t) i];
  8108. }
  8109. CHECK (e > 0.01 && ! inst->idle()); // (Loop on)
  8110. inst->noteOff (60);
  8111. for (int b = 0; b < 4; ++b)
  8112. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  8113. CHECK (inst->idle());
  8114. c.pluginState = synthA; // (no loop: 9 columns of 64·(e^(200/4096·ln 33) − 1)… samples, then the voice ends)
  8115. inst->configure (c, 120.0);
  8116. inst->noteOn (60, 1.0f);
  8117. for (int b = 0; b < 200 && ! inst->idle(); ++b)
  8118. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  8119. CHECK (inst->idle());
  8120. }
  8121. // ---- Sawer (REA_rodada65 §3): installation presets and data.saw read at run time, fingerprints of engine_x64.dll ----
  8122. namespace
  8123. {
  8124. std::vector<std::uint8_t> sawerPresetState (const char* rel)
  8125. {
  8126. if (realFlpDir.empty())
  8127. return {};
  8128. std::ifstream in (std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "Sawer" / rel, std::ios::binary);
  8129. const std::vector<std::uint8_t> b ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  8130. std::size_t i = 0;
  8131. while (i + 8 <= b.size() && std::memcmp (b.data() + i, "FLdt", 4) != 0)
  8132. ++i;
  8133. i += 8;
  8134. while (i < b.size())
  8135. {
  8136. const auto id = b[i++];
  8137. std::size_t n = id < 64 ? 1 : id < 128 ? 2 : id < 192 ? 4 : 0;
  8138. if (id >= 192)
  8139. for (int shift = 0; i < b.size(); shift += 7)
  8140. {
  8141. const auto c = b[i++];
  8142. n |= (std::size_t) (c & 0x7F) << shift;
  8143. if (c < 0x80)
  8144. break;
  8145. }
  8146. if (id == 0xD5 && i + n <= b.size())
  8147. return { b.begin() + (std::ptrdiff_t) i, b.begin() + (std::ptrdiff_t) (i + n) };
  8148. i += n;
  8149. }
  8150. return {};
  8151. }
  8152. } // namespace
  8153. TEST (sawer_engine)
  8154. {
  8155. // a synthetic header: "SSLF" + size + "1rwS" + 96 floats
  8156. std::vector<std::uint8_t> synth (12 + 4 * 96, 0);
  8157. std::memcpy (synth.data(), "SSLF", 4);
  8158. const std::uint32_t size = 4 + 4 * 96;
  8159. std::memcpy (synth.data() + 4, &size, 4);
  8160. std::memcpy (synth.data() + 8, "1rwS", 4);
  8161. const float p27 = 0.75f;
  8162. std::memcpy (synth.data() + 12 + 4 * 27, &p27, 4);
  8163. CHECK (lynx::sawerStateUsable (synth));
  8164. CHECK (lynx::sawerParam (synth, 27) == 0.75f && lynx::sawerParamOffset (synth, 27) == 12 + 4 * 27);
  8165. CHECK (! lynx::sawerParam (synth, 96) && lynx::sawerParamOffset (synth, -1) < 0);
  8166. auto broken = synth;
  8167. broken[8] = 'X';
  8168. CHECK (! lynx::sawerStateUsable (broken));
  8169. CHECK (! lynx::sawerStateUsable (std::vector<std::uint8_t> (synth.begin(), synth.end() - 4)));
  8170. const auto dflt = sawerPresetState ("Default.fst");
  8171. const auto slap = sawerPresetState ("Arpeggios/FG Velocity Slapper.fst");
  8172. const auto tablePath = realFlpDir.empty() ? std::string() : (std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "Sawer" / "data.saw").string();
  8173. const auto table = tablePath.empty() ? std::vector<float> {} : lynx::sawerReadTable (tablePath);
  8174. if (dflt.empty() || slap.empty() || table.size() != 8192u)
  8175. {
  8176. std::printf (" (sawer_engine: FL Studio installation not found, DLL fingerprints skipped)\n");
  8177. return;
  8178. }
  8179. CHECK (lynx::sawerStateUsable (dflt) && lynx::sawerStateUsable (slap));
  8180. // DLL fingerprints (py/gold_sw.py: the EXEC harness, L = R = Final.Vol); "syn" = Default with our own settings
  8181. // (unison 4, sub pulse at 0.6, sync, band-pass, noise, resonance, chorus)
  8182. auto setP = [] (std::vector<std::uint8_t> s, int k, float v) { std::memcpy (s.data() + 12 + 4 * k, &v, 4); return s; };
  8183. auto syn = dflt;
  8184. for (const auto& kv : { std::pair { 14, 1.0f }, { 23, 1.0f }, { 25, 1.0f }, { 24, 0.5f }, { 31, 0.4f }, { 47, 1.0f }, { 19, 0.6f },
  8185. { 26, 0.3f }, { 28, 0.7f }, { 45, 0.5f }, { 46, 0.0f } })
  8186. syn = setP (syn, kv.first, kv.second);
  8187. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  8188. using N = lynx::sawer::RefNote;
  8189. struct Sample { int at; float l, r; };
  8190. struct Golden { const std::vector<std::uint8_t>* state; std::vector<N> notes; double fs; int block; double el, er; Sample s[4]; };
  8191. const Golden goldens[] {
  8192. { &dflt, { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 }, { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 }, { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 } },
  8193. 44100, 512, 1172.343107, 1169.673445,
  8194. { { 1000, 0.0349505879f, 0.0349650607f }, { 10000, -0.0326882228f, -0.0326587185f }, { 30000, 0.139422998f, 0.139268339f }, { 60000, -1.01108653e-05f, -1.01210762e-05f } } },
  8195. { &slap, { { 0, 57, iv (90), 1.0, 0.0, 0.0, 40000 }, { 500, 64, iv (70), 1.0, 0.0, 0.0, 40000 } }, 48000, 256, 62.85129835, 62.26892467,
  8196. { { 1000, 0.00919434987f, 0.0769683793f }, { 10000, 0.0363935083f, 0.0227626693f }, { 30000, -0.00462511741f, -0.0105419969f }, { 60000, -0.00727002183f, -0.00159690797f } } },
  8197. { &syn, { { 0, 48, iv (100), 1.0, 0.0, 0.0, 20000 }, { 5000, 55, iv (60), 1.0, 0.0, 25.0, 30000 } }, 44100, 1000, 579.5093366, 593.7384022,
  8198. { { 1000, -0.162745371f, -0.172861978f }, { 10000, 0.0468756184f, 0.112415045f }, { 30000, -0.123887017f, -0.132816017f }, { 60000, 0.0f, 0.0f } } },
  8199. };
  8200. for (const auto& g : goldens)
  8201. {
  8202. const auto y = lynx::sawer::renderReference (*g.state, table, g.notes, g.fs, 66150, g.block);
  8203. CHECK (y.size() == 2 * 66150u);
  8204. if (y.size() != 2 * 66150u)
  8205. continue;
  8206. double el = 0.0, er = 0.0;
  8207. for (std::size_t i = 0; i < 66150; ++i)
  8208. el += (double) y[2 * i] * y[2 * i], er += (double) y[2 * i + 1] * y[2 * i + 1];
  8209. CHECK (std::abs (el - g.el) < 1e-5 * g.el && std::abs (er - g.er) < 1e-5 * g.er);
  8210. for (const auto& s : g.s)
  8211. CHECK (std::abs (y[2 * (std::size_t) s.at] - s.l) < 1e-7f && std::abs (y[2 * (std::size_t) s.at + 1] - s.r) < 1e-7f);
  8212. }
  8213. // the instrument: the same engine behind LYNX's channel gains (centre → law A), velocity → Init.Vol; it goes idle
  8214. lynx::ChannelDef c;
  8215. c.instrument = lynx::InstrumentKind::sawer;
  8216. c.pluginState = dflt;
  8217. c.samplePath = tablePath;
  8218. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sawer);
  8219. inst->configure (c, 120.0);
  8220. inst->prepare (44100.0);
  8221. std::vector<float> l (512 * 40, 0.0f), r (512 * 40, 0.0f);
  8222. for (int b = 0; b < 40; ++b)
  8223. {
  8224. if (b == 0)
  8225. inst->noteOn (60, 0.78125f);
  8226. if (b == 10)
  8227. inst->noteOff (60);
  8228. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  8229. }
  8230. const auto ref = lynx::sawer::renderReference (dflt, table, { { 0, 60, (std::pow (11.0, 1.28 * 0.78125) - 1.0) / 10.0, 1.0, 0.0, 0.0, 5120 } }, 44100.0,
  8231. 8192, 512, 120.0, 0, true);
  8232. double diff = 0.0, energy = 0.0;
  8233. for (int i = 0; i < 8192; ++i)
  8234. {
  8235. 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]) });
  8236. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  8237. }
  8238. CHECK (diff == 0.0 && energy > 1e-3);
  8239. CHECK (inst->idle());
  8240. }
  8241. TEST (transistorbass_engine)
  8242. {
  8243. const auto factory = tbassPresetState ("Factory programs.fst");
  8244. const auto seqOff = tbassPresetState ("Sequencer off.fst");
  8245. if (factory.empty() || seqOff.empty())
  8246. return; // no FL installation
  8247. CHECK (factory.size() == 197887 && seqOff.size() == 196051);
  8248. CHECK (lynx::transistorBassStateUsable (factory) && lynx::transistorBassStateUsable (seqOff));
  8249. CHECK (lynx::transistorBassStateUsable (tbassWithEngine (factory, 2)));
  8250. auto broken = factory;
  8251. broken[0] = 3; // the wrapper refuses versions above 2
  8252. CHECK (! lynx::transistorBassStateUsable (broken));
  8253. auto truncated = factory;
  8254. truncated.resize (truncated.size() - 1000);
  8255. CHECK (! lynx::transistorBassStateUsable (truncated));
  8256. const auto dll = tbassDll();
  8257. if (dll.empty() || ! lynx::transistorBassDllUsable (dll))
  8258. return;
  8259. // against FL's engine DLL (EXEC, REA_rodada57 §2.11): programs of the factory bank (sequencer, accents, slides,
  8260. // distortion, delay, reverb) on the three engines and the sequencer-off preset with legato and accent notes; host
  8261. // blocks of 512 at 44.1 kHz, tempo 125. LYNX differs from the DLL by −121 dB or less here (library pow rounding).
  8262. struct Sample { int at; float l, r; };
  8263. struct Golden { const char* preset; int engine, key; double el, er; Sample s[4]; };
  8264. const Golden goldens[] {
  8265. { "Factory", -1, 0, 84.1648489, 84.1648489, { { 5000, -2.72742806e-09f, -2.72742806e-09f }, { 20000, -0.0366424844f, -0.0366424844f }, { 47000, 2.15875261e-07f, 2.15875261e-07f }, { 80000, 0.0f, 0.0f } } },
  8266. { "Factory", -1, 3, 140.915804, 135.290403, { { 5000, 6.46460592e-07f, 6.46460592e-07f }, { 20000, 0.000612073636f, 0.00883315224f }, { 47000, 0.00334156211f, 0.0349708796f }, { 80000, -0.0180665664f, -0.00647424255f } } },
  8267. { "Factory", -1, 9, 74.6013145, 74.6013145, { { 5000, 1.17278e-06f, 1.17278e-06f }, { 20000, 4.45517001e-09f, 4.45517001e-09f }, { 47000, -0.00680137333f, -0.00680137333f }, { 80000, 9.70963379e-11f, 9.70963379e-11f } } },
  8268. { "Factory", -1, 21, 672.27801, 665.683557, { { 5000, 9.88614047e-05f, -0.00434152689f }, { 20000, 0.00984471664f, -0.00176824001f }, { 47000, -0.00745432731f, -0.00174837175f }, { 80000, 0.00471894164f, -0.00477111898f } } },
  8269. { "Factory", -1, 65, 534.422868, 543.176189, { { 5000, -0.00286525022f, -0.00691002142f }, { 20000, -0.0952312946f, -0.0940120071f }, { 47000, -0.109519325f, -0.115677707f }, { 80000, 0.0193505734f, 0.0466997996f } } },
  8270. { "Factory", 1, 65, 531.095944, 535.778912, { { 5000, 0.00939375907f, 0.000506323006f }, { 20000, 0.0880209357f, 0.0815288723f }, { 47000, 0.0840308368f, 0.0360141024f }, { 80000, -0.0445356891f, -0.058858037f } } },
  8271. { "Factory", 2, 65, 577.993833, 585.388538, { { 5000, -0.000922904757f, 0.0082119517f }, { 20000, 0.100198291f, 0.0890608132f }, { 47000, 0.0754022077f, 0.133105934f }, { 80000, -0.0728695691f, -0.0721700341f } } },
  8272. { "SeqOff", -1, 48, 27.8141841, 27.8141841, { { 5000, -0.0331321731f, -0.0331321731f }, { 20000, -0.016056465f, -0.016056465f }, { 47000, -0.0118189175f, -0.0118189175f }, { 80000, 0.0f, 0.0f } } },
  8273. { "SeqOff", 1, 48, 21.7931524, 21.7931524, { { 5000, 0.0268912539f, 0.0268912539f }, { 20000, 0.0150698861f, 0.0150698861f }, { 47000, 0.011147175f, 0.011147175f }, { 80000, -1.04869307e-23f, -1.04869307e-23f } } },
  8274. { "SeqOff", 2, 48, 61.3736312, 61.3736312, { { 5000, 0.0370108262f, 0.0370108262f }, { 20000, 0.0206214525f, 0.0206214525f }, { 47000, 0.0157183651f, 0.0157183651f }, { 80000, -1.3009924e-23f, -1.3009924e-23f } } },
  8275. };
  8276. constexpr int length = 88200;
  8277. for (const auto& g : goldens)
  8278. {
  8279. const bool factoryBank = std::string (g.preset) == "Factory";
  8280. auto st = factoryBank ? factory : seqOff;
  8281. if (g.engine >= 0)
  8282. st = tbassWithEngine (st, g.engine);
  8283. std::vector<lynx::tbass::RefNote> notes;
  8284. if (factoryBank)
  8285. notes.push_back ({ 0, g.key, 0.5, 0.0, 0.0, 0.0, 0.0, 70560 });
  8286. else
  8287. {
  8288. notes.push_back ({ 0, g.key, 0.5, 0.0, 0.0, 0.0, 0.0, 30000 });
  8289. notes.push_back ({ 20000, g.key + 7, 1.6, 0.0, 0.0, 0.0, 0.0, 50000 });
  8290. notes.push_back ({ 45000, g.key - 5, 0.3, 0.0, 0.0, 0.0, 0.0, 70560 });
  8291. }
  8292. const auto y = lynx::tbass::renderReference (st, dll, notes, 44100.0, length);
  8293. double el = 0.0, er = 0.0, peak = 0.0;
  8294. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  8295. 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]) });
  8296. bool ok = y.size() == 2 * (std::size_t) length && std::abs (el - g.el) <= 1e-5 * g.el && std::abs (er - g.er) <= 1e-5 * g.er;
  8297. for (const auto& smp : g.s)
  8298. 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;
  8299. if (! ok)
  8300. std::printf (" Transistor Bass %s engine %d key %d differs from FL's DLL: energy %.9g %.9g (DLL %.9g %.9g)\n", g.preset, g.engine, g.key, el, er, g.el, g.er);
  8301. CHECK (ok);
  8302. }
  8303. // the instrument: FL's wrapper velocity, the channel volume as an output gain, the sequencer plays while the key is held
  8304. lynx::ChannelDef c;
  8305. c.instrument = lynx::InstrumentKind::transistorbass;
  8306. c.pluginState = factory;
  8307. c.samplePath = dll;
  8308. auto inst = lynx::makeInstrument (lynx::InstrumentKind::transistorbass);
  8309. inst->configure (c, 128.0);
  8310. inst->prepare (44100.0);
  8311. std::vector<float> l (4096), r (4096);
  8312. inst->noteOn (65, 0.8f);
  8313. double e = 0.0;
  8314. bool finite = true;
  8315. for (int b = 0; b < 24; ++b)
  8316. {
  8317. std::fill (l.begin(), l.end(), 0.0f);
  8318. std::fill (r.begin(), r.end(), 0.0f);
  8319. inst->render (l.data(), r.data(), 4096, 0.5f, 0.5f);
  8320. for (int i = 0; i < 4096; ++i)
  8321. 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];
  8322. if (b == 20)
  8323. inst->noteOff (65);
  8324. }
  8325. CHECK (finite && e > 1.0 && ! inst->idle());
  8326. for (int b = 0; b < 400 && ! inst->idle(); ++b)
  8327. inst->render (l.data(), r.data(), 4096, 0.5f, 0.5f);
  8328. CHECK (inst->idle());
  8329. }
  8330. TEST (kepler_demo_channels)
  8331. {
  8332. // every Kepler channel of FL's demo projects on LYNX's engine against FL's engine DLL (EXEC fingerprints, REA_rodada46 §6;
  8333. // the engine differs from the DLL by −127 dB or less on these, from the DLL library's pow rounding)
  8334. const auto dll = keplerDll();
  8335. if (dll.empty() || ! lynx::keplerDllUsable (dll))
  8336. return;
  8337. struct Sample { int at; float l, r; };
  8338. struct Golden { const char* file; const char* channel; std::size_t stateSize; double el, er; Sample s[4]; };
  8339. const Golden goldens[] {
  8340. { "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 } } },
  8341. { "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 } } },
  8342. { "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 } } },
  8343. { "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 } } },
  8344. };
  8345. int checked = 0;
  8346. for (const auto& g : goldens)
  8347. {
  8348. std::ifstream in (std::filesystem::path (realFlpDir) / "Demo projects" / g.file, std::ios::binary);
  8349. const std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  8350. if (bytes.empty())
  8351. continue;
  8352. const flp::Channel* ch = nullptr;
  8353. const auto project = flp::parseProject (bytes);
  8354. for (const auto& c : project.channels)
  8355. if (c.pluginName == "Kepler" && c.name == g.channel)
  8356. ch = &c;
  8357. CHECK (ch != nullptr);
  8358. if (ch == nullptr)
  8359. continue;
  8360. CHECK (ch->pluginState.size() == g.stateSize && lynx::keplerStateUsable (ch->pluginState));
  8361. 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 },
  8362. { 16000, 55, 0.95, 0.0, 0.0, 30000 } }, 44100.0, 39690);
  8363. double el = 0.0, er = 0.0, peak = 0.0;
  8364. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  8365. 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]) });
  8366. bool ok = y.size() == 2 * 39690 && std::abs (el - g.el) <= 1e-5 * g.el && std::abs (er - g.er) <= 1e-5 * g.er;
  8367. for (const auto& smp : g.s)
  8368. 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;
  8369. if (! ok)
  8370. 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);
  8371. CHECK (ok);
  8372. ++checked;
  8373. }
  8374. std::printf (" Kepler: %d demo channels checked against FL's engine DLL\n", checked);
  8375. CHECK (checked == (int) std::size (goldens));
  8376. }
  8377. TEST (ogun_demo_channels)
  8378. {
  8379. // every Ogun channel of FL's demo projects (with its plugin effects) against the round-37 reference model, which is
  8380. // 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),
  8381. // 39 690 samples at 44.1 kHz, at the state's quality and at the export quality 4. Fingerprint: energy per side and
  8382. // four samples (the engine matches the model within float rounding, −125 dB or better on the whole signal).
  8383. if (realFlpDir.empty())
  8384. return;
  8385. struct Sample { int at; float l, r; };
  8386. struct Golden { const char* file; int ordinal; std::size_t stateSize; bool export4; double el, er; Sample s[4]; };
  8387. const Golden goldens[] {
  8388. { "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
  8389. { "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
  8390. { "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
  8391. { "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
  8392. { "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
  8393. { "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
  8394. { "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
  8395. { "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
  8396. { "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
  8397. { "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
  8398. { "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
  8399. { "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
  8400. { "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
  8401. { "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
  8402. { "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
  8403. { "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
  8404. { "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
  8405. { "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
  8406. { "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
  8407. { "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
  8408. { "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
  8409. { "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
  8410. { "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
  8411. { "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
  8412. { "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
  8413. { "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
  8414. { "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
  8415. { "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
  8416. { "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
  8417. { "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
  8418. { "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
  8419. { "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
  8420. { "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
  8421. { "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
  8422. { "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
  8423. { "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
  8424. { "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
  8425. { "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
  8426. { "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
  8427. { "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
  8428. };
  8429. std::map<std::string, std::vector<std::vector<std::uint8_t>>> states;
  8430. int checked = 0;
  8431. for (const auto& g : goldens)
  8432. {
  8433. if (! states.contains (g.file))
  8434. {
  8435. std::ifstream in (std::filesystem::path (realFlpDir) / "Demo projects" / g.file, std::ios::binary);
  8436. const std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  8437. auto& list = states[g.file];
  8438. if (! bytes.empty())
  8439. for (const auto& c : flp::parseProject (bytes).channels)
  8440. if (c.pluginName == "Ogun")
  8441. list.push_back (c.pluginState);
  8442. }
  8443. const auto& list = states[g.file];
  8444. if (list.empty())
  8445. continue; // not this installation
  8446. CHECK ((int) list.size() > g.ordinal);
  8447. if ((int) list.size() <= g.ordinal)
  8448. continue;
  8449. const auto& st = list[(std::size_t) g.ordinal];
  8450. CHECK (st.size() == g.stateSize);
  8451. lynx::ogun::RefOptions opt;
  8452. opt.quality = g.export4 ? 4 : -1;
  8453. const auto y = lynx::ogun::renderReference (st, { { 0, 60, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 20000 },
  8454. { 8000, 67, 0.6, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 26000 },
  8455. { 16000, 55, 0.95, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 30000 } }, 44100.0, 39690, opt);
  8456. double el = 0.0, er = 0.0, peak = 0.0;
  8457. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  8458. 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]) });
  8459. bool ok = y.size() == 2 * 39690 && std::abs (el - g.el) <= 1e-5 * g.el && std::abs (er - g.er) <= 1e-5 * g.er;
  8460. for (const auto& smp : g.s)
  8461. 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;
  8462. if (! ok)
  8463. std::printf (" Ogun %s #%d (q%s) differs from the model: energy %.9g %.9g (model %.9g %.9g)\n", g.file, g.ordinal,
  8464. g.export4 ? "4" : "-state", el, er, g.el, g.er);
  8465. CHECK (ok);
  8466. ++checked;
  8467. }
  8468. std::printf (" Ogun: %d demo renders checked against the reference model\n", checked);
  8469. CHECK (checked == (int) std::size (goldens));
  8470. }
  8471. TEST (autogun_template_channels)
  8472. {
  8473. // REA_rodada57: every distinct Autogun state of the installation's projects (templates and the render test), with its
  8474. // effects and the Magic Dust stage (SG.dat from the installation), three notes (C5 1.0 0–20000, G5 0.6 8000–26000,
  8475. // G4 0.95 16000–30000), 39 690 samples at 44.1 kHz, at the state's quality and at the export quality 4. The template
  8476. // goldens are renders of the installation's Autogun_x64.dll (LYNX is within −127 dB of them); the render-test state
  8477. // uses Seed 2 "Random", so its golden is LYNX's own render with the counter at 0 (a fingerprint of the engine).
  8478. if (realFlpDir.empty())
  8479. return;
  8480. const auto sgPath = std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "Autogun" / "Data" / "SG.dat";
  8481. std::ifstream sgIn (sgPath, std::ios::binary);
  8482. const std::vector<std::uint8_t> sgState ((std::istreambuf_iterator<char> (sgIn)), std::istreambuf_iterator<char>());
  8483. if (sgState.size() != 1342)
  8484. return; // not this installation
  8485. struct Sample { int at; float l, r; };
  8486. struct Golden { const char* file; std::size_t stateSize; bool export4; double el, er; Sample s[4]; };
  8487. const Golden goldens[] {
  8488. { "System/Render tests/Autogun.flp", 2403, false, 1898.57144, 1609.64764, { { 5000, 0.189644396f, 0.104252934f }, { 12000, 0.0354045518f, 0.002853852f }, { 25000, -0.212527201f, -0.364677101f }, { 35000, 0.00715163164f, -0.0779336989f } } }, // Autogun (LYNX engine, counter 0)
  8489. { "System/Render tests/Autogun.flp", 2403, true, 1883.83096, 1696.41005, { { 5000, -0.229550153f, -0.0656554326f }, { 12000, -0.243209839f, -0.191908702f }, { 25000, 0.174803019f, 0.125847161f }, { 35000, 0.0609342083f, 0.0861448273f } } }, // Autogun (LYNX engine, counter 0)
  8490. { "Templates/Advanced/Electronic/Breakbeat.flp", 43615, false, 1156.8126, 667.1251, { { 5000, -0.122245498f, -0.00968936551f }, { 12000, 0.088016659f, 0.0382440798f }, { 25000, -0.0438547991f, -0.0149139315f }, { 35000, -0.0162006877f, 0.0219070036f } } }, // Pad (Autogun_x64.dll)
  8491. { "Templates/Advanced/Electronic/Breakbeat.flp", 43615, true, 541.425656, 476.875716, { { 5000, 0.15942347f, 0.0947837308f }, { 12000, 0.172008425f, 0.197265282f }, { 25000, 0.0506084785f, 0.0015097193f }, { 35000, 0.0400503464f, 0.0195946041f } } }, // Pad (Autogun_x64.dll)
  8492. { "Templates/Advanced/Electronic/Chillout.flp", 171641, false, 1463.17228, 1370.65276, { { 5000, 0.158808231f, -0.128741398f }, { 12000, 0.208011121f, -0.153484732f }, { 25000, -0.184889361f, 0.0409393087f }, { 35000, 0.0255694874f, 0.0495453961f } } }, // Pad 5 (Autogun_x64.dll)
  8493. { "Templates/Advanced/Electronic/Chillout.flp", 171641, true, 1412.70901, 1469.84358, { { 5000, -0.0218960885f, -0.0802078545f }, { 12000, -0.458583713f, 0.146813422f }, { 25000, -0.198403969f, 0.0604776815f }, { 35000, -0.097854577f, 0.0806856826f } } }, // Pad 5 (Autogun_x64.dll)
  8494. { "Templates/Advanced/Electronic/HipHop-Trap.flp", 9787, false, 2807.37714, 3754.76445, { { 5000, -0.0391585156f, 0.332697481f }, { 12000, -0.128277287f, -0.632223666f }, { 25000, 0.103559501f, 0.521154523f }, { 35000, 0.309904903f, 0.280873239f } } }, // Pad (Autogun_x64.dll)
  8495. { "Templates/Advanced/Electronic/HipHop-Trap.flp", 9787, true, 3526.66329, 4199.20992, { { 5000, 0.256542385f, 0.0431160629f }, { 12000, -0.283133686f, -0.732375383f }, { 25000, -0.210154608f, -0.276185691f }, { 35000, -0.144943774f, -0.315269828f } } }, // Pad (Autogun_x64.dll)
  8496. { "Templates/Advanced/Electronic/Trap.flp", 9780, false, 2807.37714, 3754.76445, { { 5000, -0.0391585156f, 0.332697481f }, { 12000, -0.128277287f, -0.632223666f }, { 25000, 0.103559501f, 0.521154523f }, { 35000, 0.309904903f, 0.280873239f } } }, // Pad (Autogun_x64.dll)
  8497. { "Templates/Advanced/Electronic/Trap.flp", 9780, true, 3526.66329, 4199.20992, { { 5000, 0.256542385f, 0.0431160629f }, { 12000, -0.283133686f, -0.732375383f }, { 25000, -0.210154608f, -0.276185691f }, { 35000, -0.144943774f, -0.315269828f } } }, // Pad (Autogun_x64.dll)
  8498. };
  8499. int checked = 0;
  8500. for (const auto& g : goldens)
  8501. {
  8502. std::ifstream in (std::filesystem::path (realFlpDir) / g.file, std::ios::binary);
  8503. const std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  8504. if (bytes.empty())
  8505. continue;
  8506. std::vector<std::uint8_t> st;
  8507. for (const auto& c : flp::parseProject (bytes).channels)
  8508. if (c.pluginName == "Autogun" && st.empty())
  8509. st = c.pluginState;
  8510. CHECK (st.size() == g.stateSize);
  8511. CHECK (lynx::autogunStateUsable (st));
  8512. lynx::ogun::RefOptions opt;
  8513. opt.autogun = true;
  8514. opt.sgState = sgState;
  8515. opt.quality = g.export4 ? 4 : -1;
  8516. const auto y = lynx::ogun::renderReference (st, { { 0, 60, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 20000 },
  8517. { 8000, 67, 0.6, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 26000 },
  8518. { 16000, 55, 0.95, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 30000 } }, 44100.0, 39690, opt);
  8519. double el = 0.0, er = 0.0, peak = 0.0;
  8520. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  8521. 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]) });
  8522. bool ok = y.size() == 2 * 39690 && std::abs (el - g.el) <= 1e-5 * g.el && std::abs (er - g.er) <= 1e-5 * g.er;
  8523. for (const auto& smp : g.s)
  8524. 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;
  8525. if (! ok)
  8526. std::printf (" Autogun %s (q%s) differs: energy %.9g %.9g (golden %.9g %.9g)\n", g.file, g.export4 ? "4" : "-state", el, er, g.el, g.er);
  8527. CHECK (ok);
  8528. ++checked;
  8529. }
  8530. std::printf (" Autogun: %d template/render-test renders checked (DLL goldens for the fixed-spectrum states)\n", checked);
  8531. CHECK (checked == (int) std::size (goldens));
  8532. // the instrument (SG.dat read from ChannelDef::samplePath) plays like the reference renderer, then goes idle
  8533. {
  8534. std::ifstream in (std::filesystem::path (realFlpDir) / "Templates" / "Advanced" / "Electronic" / "Trap.flp", std::ios::binary);
  8535. const std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  8536. lynx::ChannelDef c;
  8537. c.instrument = lynx::InstrumentKind::autogun;
  8538. for (const auto& ch : flp::parseProject (bytes).channels)
  8539. if (ch.pluginName == "Autogun")
  8540. c.pluginState = ch.pluginState;
  8541. c.samplePath = sgPath.string();
  8542. lynx::ogun::RefOptions opt;
  8543. opt.autogun = true;
  8544. opt.sgState = sgState;
  8545. opt.tempo = 120.0; // the instrument is configured at 120 BPM
  8546. const auto ref = lynx::ogun::renderReference (c.pluginState, { { 0, 60, 0.75, 1.0, 0.0, 0.0, 0.0, 0.0, 0.5, 6144 } }, 44100.0, 16384, opt);
  8547. auto inst = lynx::makeInstrument (lynx::InstrumentKind::autogun);
  8548. inst->configure (c, 120.0);
  8549. inst->prepare (44100.0);
  8550. std::vector<float> l (16384), r (16384);
  8551. for (int s = 0; s < 16384; s += 512)
  8552. {
  8553. if (s == 0)
  8554. inst->noteOn (60, 0.75f);
  8555. if (s == 6144)
  8556. inst->noteOff (60);
  8557. inst->render (l.data() + s, r.data() + s, 512, 1.0f, 1.0f);
  8558. }
  8559. double worst = 0.0, peak = 0.0;
  8560. for (int i = 0; i < 16384; ++i)
  8561. 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]) }),
  8562. peak = std::max (peak, (double) std::abs (l[(std::size_t) i]));
  8563. if (worst != 0.0)
  8564. std::printf (" Autogun instrument vs reference: worst %.3g (peak %.3g)\n", worst, peak);
  8565. CHECK (ref.size() == 2 * 16384 && peak > 0.01 && worst == 0.0);
  8566. std::vector<float> tail (44100), tailR (tail.size());
  8567. for (int k = 0; k < 40 && ! inst->idle(); ++k)
  8568. inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
  8569. CHECK (inst->idle());
  8570. }
  8571. // the state variants: Ogun's parser rejects the version-19 Autogun layout, the comment length is one byte or 0xff + i32
  8572. std::ifstream rt (std::filesystem::path (realFlpDir) / "System" / "Render tests" / "Autogun.flp", std::ios::binary);
  8573. const std::vector<std::uint8_t> rtBytes ((std::istreambuf_iterator<char> (rt)), std::istreambuf_iterator<char>());
  8574. for (const auto& c : flp::parseProject (rtBytes).channels)
  8575. if (c.pluginName == "Autogun")
  8576. CHECK (lynx::autogunStateUsable (c.pluginState) && lynx::ogunParam (c.pluginState, 63) == 430);
  8577. }
  8578. TEST (harmless_rack)
  8579. {
  8580. // REA_rodada36: Harmless's effects rack against our reference model (rack.py / maxmodel.py, which match the
  8581. // installation's Harmless_x64.dll and SG_x64.dll), 44.1 kHz, host blocks of 512
  8582. std::vector<float> inL (8192), inR (8192);
  8583. for (int i = 0; i < 4096; ++i)
  8584. {
  8585. 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));
  8586. inR[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / 44100.0));
  8587. }
  8588. auto run = [&] (const std::array<int, 100>& p, int compType, std::shared_ptr<const lynx::hlfx::MaximusPreset> comp, int total, int voicesUntil) {
  8589. lynx::hlfx::Rack rack;
  8590. std::array<int, 7> t {};
  8591. t[3] = compType;
  8592. rack.prepare (p, t, 44100.0, 120.0, std::move (comp));
  8593. std::vector<float> l (inL.begin(), inL.begin() + total), r (inR.begin(), inR.begin() + total);
  8594. for (int s = 0; s < total; s += 512)
  8595. rack.process (l.data() + s, r.data() + s, std::min (512, total - s), s < voicesUntil);
  8596. return std::make_pair (l, r);
  8597. };
  8598. {
  8599. // chorus type 3 (mix 400), ping-pong delay type 2 (vol 600, feedback 500, 1/4 beat), reverb type 4 (vol 700)
  8600. std::array<int, 100> p {};
  8601. p[90] = 3, p[91] = 400, p[92] = 2, p[93] = 600, p[94] = 500, p[95] = 48, p[96] = 4, p[97] = 700;
  8602. const auto [l, r] = run (p, 0, nullptr, 8192, 4096);
  8603. const std::tuple<int, float, float> golden[] { { 0, 0.0f, 0.0f }, { 500, -0.25248247f, 0.15141529f }, { 3000, -0.65128994f, -0.64854866f },
  8604. { 5000, -0.54308629f, -0.36827448f }, { 6000, -0.98378551f, -0.72790027f },
  8605. { 8191, 0.50599128f, -0.43637198f } };
  8606. for (const auto& [i, gl, gr] : golden)
  8607. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  8608. }
  8609. {
  8610. // the delay alone: an impulse comes back after round(time·fs·0.3125/bpm) samples × (11^0.6 − 1)/10, then × feedback
  8611. std::array<int, 100> p {};
  8612. p[92] = 1, p[93] = 600, p[94] = 500, p[95] = 48;
  8613. lynx::hlfx::Rack rack;
  8614. rack.prepare (p, {}, 44100.0, 120.0, nullptr);
  8615. std::vector<float> l (16384), r (16384);
  8616. l[0] = 1.0f;
  8617. for (int s = 0; s < 16384; s += 512)
  8618. rack.process (l.data() + s, r.data() + s, 512, s == 0);
  8619. const auto D = 5512; // 48·44100·0.3125/120 = 5512.5 → 5512 (round half to even)
  8620. const auto vol = (float) ((std::exp ((double) (600 * 0.001f) * (double) (float) std::log (11.0)) - 1.0) * (double) 0.1f);
  8621. CHECK (std::abs (l[(std::size_t) D] - vol) < 1e-7f && std::abs (l[(std::size_t) D - 1]) < 1e-12f);
  8622. // the feedback ramps from 0.5 toward 0.5: the second echo is vol·0.5
  8623. CHECK (std::abs (l[(std::size_t) (2 * D)] - vol * 0.5f) < 1e-7f && std::abs (r[(std::size_t) D]) < 1e-12f);
  8624. }
  8625. // the compressor: Maximus states of our own (no preset data), checked against SG_x64.dll by REA round 36
  8626. using Env = std::vector<std::array<double, 4>>;
  8627. const Env def { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 } };
  8628. 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,
  8629. int sus = 2882) { return std::array<int, 13> { mode, pre, post, sep, sat, ceil, att, rel2, 2, 5000, 3, sus, 0 }; };
  8630. {
  8631. // master brickwall at −0.22 dB only (bands off / compression off)
  8632. const auto st = maximusTestState ({ 19800, 1, 48065, 1, 1115, 1000, -1 }, { band (3), band (1), band (3), band (0) },
  8633. { def, def, def, def, def, def, def, def }, 0);
  8634. const auto preset = lynx::hlfx::makeMaximusPreset (st);
  8635. CHECK (preset != nullptr);
  8636. std::array<int, 100> p {};
  8637. p[98] = 1000;
  8638. const auto [l, r] = run (p, 1, preset, 4096, 4096);
  8639. // SG_x64.dll's own output (EXEC, REA_rodada40: bit-identical to the port)
  8640. const std::tuple<int, float, float> golden[] { { 100, 0.024997942f, 0.88166064f }, { 1000, 0.56971401f, -0.62808102f },
  8641. { 2047, -0.27541628f, 0.709135f }, { 3000, 0.32431623f, -0.37985197f },
  8642. { 4095, -0.12743522f, -0.15237485f } };
  8643. for (const auto& [i, gl, gr] : golden)
  8644. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  8645. float peak = 0.0f;
  8646. for (int i = 2048; i < 4096; ++i)
  8647. peak = std::max ({ peak, std::abs (l[(std::size_t) i]), std::abs (r[(std::size_t) i]) });
  8648. CHECK (peak < 0.9751f);
  8649. }
  8650. {
  8651. // 24 dB crossovers, ×2 oversampling, low cut, stereo separation, saturation A, a knee with tension (type 7) and
  8652. // exponential tensions (type 0), release 2 on the master, band mix 600
  8653. const Env knee { { 0.0, 0.0, 0.0, 0 }, { 0.35, 0.35, 0.0, 0 }, { 0.65, 0.6, 0.36, 7 } };
  8654. const Env t0 { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.14, 0 }, { 0.5, 0.5, 0.0, 0 } };
  8655. const Env m0 { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 }, { 0.5, 0.6, 0.085, 0 } };
  8656. const auto st = maximusTestState ({ 19800, 1, 48065, 1, 891, 600, 32768 },
  8657. { band (1, 1264, 1296, 500, -500, 1200), band (0, 1340, 1000, -348, 0, 1000, 1115, 0, 1570),
  8658. band (0, 1364, 1152, 0, 0, 1000, 1115, 0, 1186), band (0, 1000, 1000, 0, -196, 1000, 1115, 4464, 1538) },
  8659. { def, def, knee, def, t0, def, m0, def }, 1);
  8660. const auto preset = lynx::hlfx::makeMaximusPreset (st);
  8661. CHECK (preset != nullptr);
  8662. std::array<int, 100> p {};
  8663. p[98] = 600;
  8664. const auto [l, r] = run (p, 2, preset, 4096, 4096);
  8665. // SG_x64.dll's own output (EXEC, REA_rodada40; the port is within −142 dB of it)
  8666. const std::tuple<int, float, float> golden[] { { 100, 0.0f, 0.0f }, { 1000, -0.46856543f, -0.63262129f },
  8667. { 2047, 0.20268451f, 0.54145539f }, { 3000, -0.25613472f, -0.49985155f },
  8668. { 4095, 0.10172445f, 0.050564945f } };
  8669. for (const auto& [i, gl, gr] : golden)
  8670. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  8671. }
  8672. {
  8673. // an .fst wrapper around a state is found; unsupported states are rejected
  8674. const auto st = maximusTestState ({ 19800, 1, 48065, 1, 1115, 1000, -1 }, { band (3), band (1), band (3), band (0) },
  8675. { def, def, def, def, def, def, def, def }, 0);
  8676. std::vector<std::uint8_t> fst { 'F', 'L', 'h', 'd', 6, 0, 0, 0, 0, 0, 1, 0, 0x60, 0, 'F', 'L', 'd', 't' };
  8677. std::vector<std::uint8_t> body { 0xC9, 8, 'M', 'a', 'x', 'i', 'm', 'u', 's', 0, 0xD5 };
  8678. for (auto n = st.size(); ; n >>= 7)
  8679. {
  8680. body.push_back ((std::uint8_t) ((n & 0x7f) | (n >= 0x80 ? 0x80 : 0)));
  8681. if (n < 0x80)
  8682. break;
  8683. }
  8684. body.insert (body.end(), st.begin(), st.end());
  8685. const auto len = (std::uint32_t) body.size();
  8686. for (int k = 0; k < 4; ++k)
  8687. fst.push_back ((std::uint8_t) (len >> (8 * k)));
  8688. fst.insert (fst.end(), body.begin(), body.end());
  8689. const auto found = lynx::hlfx::maximusStateFromFst (fst);
  8690. CHECK (found && *found == st);
  8691. auto rms = st;
  8692. rms[4 + 4 * (7 + 13 * 3 + 12)] = 1; // RMS detector on the master (decoded in REA_rodada40)
  8693. CHECK (lynx::hlfx::makeMaximusPreset (rms) != nullptr);
  8694. auto masterMid = st;
  8695. masterMid[0x1fc] = 1; // "Master Mid mode": not decoded
  8696. CHECK (lynx::hlfx::makeMaximusPreset (masterMid) == nullptr);
  8697. }
  8698. }
  8699. TEST (fruity_delay_bank_engine)
  8700. {
  8701. // REA_rodada56: Fruity Delay Bank, two states of our own against the installation's DLL (goldens = the DLL's output
  8702. // for the same input, EXEC; a new instance, FTZ/DAZ as the plugin's thread). Both are bit-identical.
  8703. lynx::ScopedFlushDenormals ftz;
  8704. struct Bank
  8705. {
  8706. std::array<int, 16> ctl;
  8707. lynx::fldbank::BankOptions opt;
  8708. };
  8709. auto build = [] (std::array<int, 4> globals, std::map<int, Bank> banks, int oversampling) {
  8710. auto s = lynx::fldbank::defaultState();
  8711. for (int i = 0; i < 4; ++i)
  8712. s.p[(std::size_t) i] = globals[(std::size_t) i];
  8713. s.p[132] = oversampling;
  8714. for (auto& [b, k] : banks)
  8715. {
  8716. for (int c = 0; c < 16; ++c)
  8717. if (16 * b + 4 + c < lynx::fldbank::automatable)
  8718. s.p[(std::size_t) (16 * b + 4 + c)] = k.ctl[(std::size_t) c];
  8719. s.bank[(std::size_t) b] = k.opt;
  8720. s.bank[(std::size_t) b].enabled = true;
  8721. }
  8722. return s;
  8723. };
  8724. auto opt = [] (int f1, int o1, int f2, int o2, bool after, int mode, int steps, int amount, bool tap, bool sync) {
  8725. lynx::fldbank::BankOptions o;
  8726. o.filterType[0] = f1;
  8727. o.filterOrder[0] = o1;
  8728. o.filterType[1] = f2;
  8729. o.filterOrder[1] = o2;
  8730. o.filterAfter = after;
  8731. o.mode = mode;
  8732. o.steps = steps;
  8733. o.amount = amount;
  8734. o.tapBeforeFeedback = tap;
  8735. o.sync = sync;
  8736. return o;
  8737. };
  8738. auto run = [] (const lynx::fldbank::State& s, double rate, int block, double bpm) {
  8739. std::vector<float> l (4096), r (4096);
  8740. for (int i = 0; i < 4096; ++i)
  8741. {
  8742. l[(std::size_t) i] = (float) (1.5 * std::sin (2.0 * 3.141592653589793 * 440.0 * i / rate) * std::min (1.0, i / 400.0));
  8743. r[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / rate));
  8744. }
  8745. lynx::fldbank::Engine e (s, rate, bpm, block);
  8746. for (int i = 0; i < 4096; i += block)
  8747. e.process (l.data() + i, r.data() + i, std::min (block, 4096 - i));
  8748. return std::make_pair (l, r);
  8749. };
  8750. auto check = [] (const std::pair<std::vector<float>, std::vector<float>>& out, const std::vector<std::tuple<int, float, float>>& golden) {
  8751. for (const auto& [i, gl, gr] : golden)
  8752. CHECK (out.first[(std::size_t) i] == gl && out.second[(std::size_t) i] == gr);
  8753. };
  8754. // controls: in pan, in vol, f1 cutoff, f1 res, f1 gain, fb pan, fb level, fb cross, time, offset, f2 cutoff, f2 res,
  8755. // f2 gain, out pan, out vol, send. Three banks: ping-pong with low-pass ×2 / peaking feedback filter, pattern and send;
  8756. // free time with a high shelf after the delay, band-pass feedback, negative feedback/cross, tap before the feedback;
  8757. // swapped outputs with a 3-section high-pass in the loop and a negative pattern shape.
  8758. const auto a = build ({ 100, 128, 120, 110 },
  8759. { { 0, { { -40, 140, 40000, 30000, 0, 30, 90, 50, 240, -200, 30000, 20000, 180, 20, 128, 100 }, opt (1, 1, 6, 0, false, 3, 4, 60, false, true) } },
  8760. { 1, { { 0, 128, 50000, 10000, 60, 0, -70, -40, 150, 150, 25000, 40000, 0, 0, -100, 0 }, opt (7, 0, 2, 0, true, 1, 0, 0, true, false) } },
  8761. { 2, { { 10, 110, 33145, 17500, 0, -60, 100, 0, 400, 0, 20000, 50000, 0, -30, 120, 0 }, opt (0, 0, 4, 2, false, 2, 3, -90, false, true) } } },
  8762. 0);
  8763. check (run (a, 44100.0, 512, 120.0), { { 100, -0.00294386433f, 0.0141299525f }, { 1000, -0.117360562f, -0.140237048f },
  8764. { 2047, 0.172598198f, -0.401046395f }, { 3000, 0.303020209f, -0.127763838f },
  8765. { 4095, 0.0093755126f, 0.241272509f } });
  8766. // 2× oversampling at 48 kHz in blocks of 300, low shelf before the delay, 2-section notch in the loop, a send into a
  8767. // ping-pong bank 3 (bank 1 and 2 off: no send) with a 2-step pattern
  8768. const auto b = build ({ 0, 128, 128, 128 },
  8769. { { 0, { { 0, 128, 45000, 20000, 200, 0, 80, 0, 180, 60, 28000, 30000, 0, 0, 128, 64 }, opt (5, 0, 3, 1, false, 1, 0, 0, false, true) } },
  8770. { 3, { { 64, 128, 33145, 17500, 0, 0, -64, 90, 100, -90, 33145, 17500, 0, 0, 128, 0 }, opt (0, 0, 0, 0, false, 3, 2, 0, true, true) } } },
  8771. 1);
  8772. check (run (b, 48000.0, 300, 93.0), { { 100, 0.0f, 0.0f }, { 1000, 0.0f, 0.0f }, { 2047, 0.201803684f, -0.327834547f },
  8773. { 3000, 0.412344277f, -0.101866335f }, { 4095, 0.204572573f, 0.71171385f } });
  8774. // FL's state round-trips through the import (version 15 times are converted), the globals are the slot's parameters
  8775. auto bytes = lynx::fldbank::serialise (a);
  8776. const auto slot = lynx::effectFromFl ("Fruity Delay Bank", bytes);
  8777. CHECK (slot && slot->kind == lynx::EffectKind::fruityDelayBank && slot->state == bytes);
  8778. CHECK (slot && slot->params[0] == 100.0f / 128.0f && slot->params[3] == 110.0f / 128.0f);
  8779. bytes[0] = 15;
  8780. const auto v15 = lynx::fldbank::parse (bytes);
  8781. CHECK (v15 && v15->p[12] == 2400 && v15->p[28] == 1500);
  8782. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityDelayBank, 3, 0.5f, 120.0) == "50%");
  8783. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityDelayBank, 0, 1.0f, 120.0) == "0.0 dB");
  8784. CHECK (! lynx::fldbank::parse ({ 16, 0, 0, 0 }));
  8785. // the LYNX effect (settled ramps, 512-frame engine blocks) runs from the slot
  8786. auto fx = lynx::makeEffect (*slot);
  8787. fx->prepare (44100.0);
  8788. std::vector<float> l (1024, 0.5f), r (1024, -0.25f);
  8789. fx->process (l.data(), r.data(), 1024, slot->params, 120.0);
  8790. CHECK (std::isfinite (l[1023]) && std::isfinite (r[1023]));
  8791. }
  8792. TEST (fruity_multiband_engine)
  8793. {
  8794. // REA_rodada56: Fruity Multiband Compressor, states of our own against the installation's DLL
  8795. // (goldens = the DLL's output for the same input, EXEC; blocks of 512 at 44.1 kHz; all three bit-identical)
  8796. std::vector<float> inL (4096), inR (4096);
  8797. for (int i = 0; i < 4096; ++i)
  8798. {
  8799. 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)
  8800. + 0.5 * std::sin (2.0 * 3.141592653589793 * 90.0 * i / 44100.0));
  8801. inR[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / 44100.0)
  8802. + 0.4 * std::sin (2.0 * 3.141592653589793 * 7000.0 * i / 44100.0));
  8803. }
  8804. auto multiband = [] (std::array<int, 28> c) {
  8805. std::vector<std::uint8_t> b;
  8806. auto put = [&b] (std::int32_t v) {
  8807. std::uint8_t q[4];
  8808. std::memcpy (q, &v, 4);
  8809. b.insert (b.end(), q, q + 4);
  8810. };
  8811. put (0);
  8812. for (auto v : c)
  8813. put (v);
  8814. put (0); // UI words of the state (not used by the engine)
  8815. put (50);
  8816. return b;
  8817. };
  8818. struct Out { std::vector<float> l, r; int latency = 0; };
  8819. auto run = [&] (const std::vector<std::uint8_t>& st) {
  8820. const auto s = lynx::flmb::parse (st);
  8821. CHECK (s.has_value());
  8822. lynx::EffectParams p {};
  8823. lynx::flmb::toParams (*s, p.data());
  8824. auto fx = lynx::flmb::make();
  8825. fx->prepare (44100.0);
  8826. Out o { inL, inR };
  8827. for (int s0 = 0; s0 < 4096; s0 += 512)
  8828. fx->process (o.l.data() + s0, o.r.data() + s0, 512, p, 120.0);
  8829. o.latency = fx->latencySamples();
  8830. return o;
  8831. };
  8832. auto check = [] (const Out& out, const std::vector<std::tuple<int, float, float>>& golden) {
  8833. for (const auto& [i, gl, gr] : golden)
  8834. CHECK (std::abs (out.l[(std::size_t) i] - gl) < 2e-6f && std::abs (out.r[(std::size_t) i] - gr) < 2e-6f);
  8835. };
  8836. // IIR crossovers, limiter on, high band compressing, mid bypassed, low compressing, +2 dB output
  8837. const auto oa = run (multiband ({ 42598, 1, 0, 0, 15604, 25782, 53524, 32768, 11, 298, 51118, 2, 14994, 5037, 24736, 55755, 26870, 221, 1090, 49807, 0, 5037, 21845, 47074, 32768, 550, 1090, 40632 }));
  8838. CHECK (oa.latency == 0);
  8839. check (oa,
  8840. { { 100, 0.3061366081237793f, 0.14554384350776672f }, { 1000, 0.4117552638053894f, 0.3191826045513153f }, { 2047, -0.46868836879730225f, -0.5676097869873047f }, { 3000, 0.24988797307014465f, -0.42287692427635193f }, { 4095, -0.022194014862179756f, 0.8433372378349304f } });
  8841. // linear phase (FIR, 52 frames of latency), overlapping mid transitions, low band off, limiter off
  8842. const auto ob = run (multiband ({ 32768, 0, 1, 0, 12000, 21845, 40000, 50000, 300, 5000, 30000, 0, 4000, 3600, 30000, 45875, 60000, 50, 500, 52429, 1, 6500, 21845, 45875, 32768, 100, 1000, 52429 }));
  8843. CHECK (ob.latency == 52);
  8844. check (ob,
  8845. { { 100, 0.0176423080265522f, -0.22197140753269196f }, { 1000, -0.002046747598797083f, 0.021436547860503197f }, { 2047, -0.010928931646049023f, -0.08079832047224045f }, { 3000, 0.027733327820897102f, -0.026119394227862358f }, { 4095, 0.07469408959150314f, -0.08492052555084229f } });
  8846. // linear phase, hard compression everywhere, limiter on, mid crossovers crossed (the FIR mid band is silent)
  8847. const auto oc = run (multiband ({ 50000, 1, 1, 0, 17000, 30000, 30000, 65536, 1, 100, 0, 0, 3000, 9000, 30000, 20000, 65536, 1, 100, 65536, 0, 6500, 30000, 25000, 65536, 1, 100, 10000 }));
  8848. CHECK (oc.latency == 52);
  8849. check (oc,
  8850. { { 100, 0.028349297121167183f, -0.02347063086926937f }, { 1000, -0.00018560001626610756f, 0.0079029630869627f }, { 2047, -0.00953709613531828f, -0.00774149876087904f }, { 3000, 0.019576910883188248f, 0.006520440336316824f }, { 4095, 0.03874344751238823f, -0.000356332486262545f } });
  8851. // the slot's parameters carry FL's raw controls exactly; after the project file's 4-decimal rounding the
  8852. // effect built with FL's state still uses the exact controls
  8853. const auto st = multiband ({ 42598, 1, 1, 0, 15604, 25782, 53524, 32768, 11, 298, 51118, 0, 14994, 5037, 24736, 55755, 26870, 221,
  8854. 1090, 49807, 0, 5037, 21845, 47074, 32768, 550, 1090, 40632 });
  8855. const auto parsed = lynx::flmb::parse (st);
  8856. CHECK (parsed && parsed->c[4] == 15604);
  8857. lynx::EffectParams p {};
  8858. lynx::flmb::toParams (*parsed, p.data());
  8859. for (int i = 0; i < lynx::flmb::controls; ++i)
  8860. CHECK (lynx::flmb::raw (p[(std::size_t) i], lynx::flmb::controlMin[(std::size_t) i], lynx::flmb::controlMax[(std::size_t) i]) == parsed->c[(std::size_t) i]);
  8861. lynx::EffectParams rounded = p;
  8862. for (auto& v : rounded)
  8863. v = std::round (v * 10000.0f) / 10000.0f;
  8864. std::vector<float> l1 (inL), r1 (inR), l2 (inL), r2 (inR);
  8865. auto exact = lynx::flmb::make();
  8866. auto fromState = lynx::flmb::make (st);
  8867. exact->prepare (44100.0);
  8868. fromState->prepare (44100.0);
  8869. for (int s0 = 0; s0 < 4096; s0 += 512)
  8870. {
  8871. exact->process (l1.data() + s0, r1.data() + s0, 512, p, 120.0);
  8872. fromState->process (l2.data() + s0, r2.data() + s0, 512, rounded, 120.0);
  8873. }
  8874. CHECK (l1 == l2 && r1 == r2);
  8875. CHECK (lynx::flmb::paramText (4, p[4]) == "6302 Hz");
  8876. CHECK (lynx::flmb::paramText (3, 0.5f) == "off");
  8877. // the FL import builds the exact effect with FL's state kept
  8878. const auto slot = lynx::effectFromFl ("Fruity Multiband Compressor", st);
  8879. CHECK (slot && slot->kind == lynx::EffectKind::fruityMultiband && slot->state == st && slot->params[4] == p[4]);
  8880. // unknown versions are rejected
  8881. CHECK (! lynx::flmb::parse ({ 1, 0, 0, 0 }));
  8882. }
  8883. // REA_rodada54: input of the stereo-effect goldens (the installation DLLs' output for the same input, EXEC)
  8884. static std::pair<std::vector<float>, std::vector<float>> stereoFxInput()
  8885. {
  8886. std::vector<float> l (4096), r (4096);
  8887. for (int i = 0; i < 4096; ++i)
  8888. {
  8889. l[(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));
  8890. r[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / 44100.0));
  8891. }
  8892. return { l, r };
  8893. }
  8894. static std::vector<std::uint8_t> i32State (std::initializer_list<int> words)
  8895. {
  8896. std::vector<std::uint8_t> d;
  8897. for (const auto w : words)
  8898. for (int b = 0; b < 4; ++b)
  8899. d.push_back ((std::uint8_t) ((std::uint32_t) w >> (8 * b)));
  8900. return d;
  8901. }
  8902. static void checkStereoFx (lynx::EffectSlot slot, const std::vector<std::tuple<int, float, float>>& golden)
  8903. {
  8904. auto [l, r] = stereoFxInput();
  8905. auto fx = lynx::makeEffect (slot);
  8906. fx->prepare (44100.0);
  8907. for (int s = 0; s < 4096; s += 512)
  8908. fx->process (l.data() + s, r.data() + s, 512, slot.params, 120.0);
  8909. for (const auto& [i, gl, gr] : golden)
  8910. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  8911. }
  8912. TEST (stereo_shaper_engine)
  8913. {
  8914. // Fruity Stereo Shaper (REA_rodada54 §2): matrix with cross terms, delay on R, all-pass on L, effects post / pre
  8915. auto slot = lynx::effectFromFl ("Fruity Stereo Shaper", i32State ({ 0, -3050, 12800, 9000, 2000, 1408, -2496, 0, 1 }));
  8916. CHECK (slot && slot->kind == lynx::EffectKind::stereoShaper);
  8917. if (slot)
  8918. checkStereoFx (*slot, { { 100, 0.0438031591f, 0.0f }, { 1000, 1.11109531f, -0.781641483f }, { 2047, -0.73191303f, 0.483852893f },
  8919. { 3000, 0.950346172f, -0.769737065f }, { 4095, 0.251555145f, 0.153306276f } });
  8920. slot = lynx::effectFromFl ("Fruity Stereo Shaper", i32State ({ 0, 0, 12800, 12800, 0, -2656, 2184, 0, 0 }));
  8921. CHECK (slot.has_value());
  8922. if (slot)
  8923. checkStereoFx (*slot, { { 100, 0.0f, -0.104282036f }, { 1000, 0.0f, 1.08441091f }, { 2047, 0.463494211f, -0.93754214f },
  8924. { 3000, -1.49942112f, 0.820726812f }, { 4095, -1.31784308f, -0.057449434f } });
  8925. // controls round-trip through the slot parameters; the Patcher's difference bus is the same engine flagged
  8926. const auto st = lynx::stereofx::parseStereoShaper (i32State ({ 0, -3050, 12800, 9000, 2000, 1408, -2496, 2, 1 }));
  8927. CHECK (st.has_value());
  8928. if (st)
  8929. {
  8930. lynx::EffectParams p {};
  8931. lynx::stereofx::stereoShaperToParams (*st, p.data());
  8932. const auto back = lynx::stereofx::stereoShaperFromParams (p.data());
  8933. CHECK (back.rightToLeft == -3050 && back.left == 12800 && back.right == 9000 && back.leftToRight == 2000 && back.delay == 1408
  8934. && back.phase == -2496 && back.post == 1);
  8935. const auto head = lynx::patcherBranchHead ("Fruity Stereo Shaper", i32State ({ 0, -3050, 12800, 9000, 2000, 1408, -2496, 2, 1 }), 2);
  8936. CHECK (head.valid && head.slot && head.slot->kind == lynx::EffectKind::stereoShaper && head.slot->params[7] == 1.0f);
  8937. }
  8938. CHECK (! lynx::stereofx::parseStereoShaper ({ 1, 2, 3 }));
  8939. }
  8940. TEST (frequency_splitter_engine)
  8941. {
  8942. // Frequency Splitter (REA_rodada62): our own states through Frequency Splitter_x64.dll from its creation (gains
  8943. // ramping up from 0, FFT response faded in), blocks of 512 at 44.1 kHz; every band sent to its own destination
  8944. // (1 low, 2 mid, 3 high). IIR modes are bit-exact; the FFT mode's residual is the plugin's single-precision FFT.
  8945. using Gold = std::vector<std::tuple<int, float, float>>;
  8946. struct Case
  8947. {
  8948. std::vector<int> words;
  8949. int mute, latency;
  8950. std::array<Gold, 4> gold; // low, mid, high sends, summed output
  8951. };
  8952. const std::vector<Case> cases {
  8953. { { 19800, 48065, 3, 3, 0, 1, 2, 45820, 0, 0, 0, 300, -600, 0, 1, 2, 3 }, 0, 0, { Gold { { 100, 0.0090178391f, 0.0807745308f }, { 1000, 0.0443177596f, 0.000449771265f }, { 2047, -0.0277729966f, -0.00895843748f }, { 3000, 0.0306171384f, -0.00373467989f }, { 4095, 0.00322337216f, 0.00886098202f } }, Gold { { 100, 0.121696919f, -0.131051332f }, { 1000, 1.03903234f, 0.0533506982f }, { 2047, -0.651134431f, -1.06304717f }, { 3000, 0.717791438f, -0.443113774f }, { 4095, 0.0754866973f, 1.05140531f } }, Gold { { 100, 1.72075161e-05f, -0.00040520617f }, { 1000, 0.000452454377f, 0.000117809883f }, { 2047, -0.000283558591f, -0.00234806887f }, { 3000, 0.000312690681f, -0.00097864517f }, { 4095, 3.28165661e-05f, 0.00232247845f } }, Gold { { 100, 0.100601904f, 0.0864751786f }, { 1000, 0.788743436f, 0.0366551876f }, { 2047, -0.494284719f, -0.730370402f }, { 3000, 0.544887185f, -0.304443926f }, { 4095, 0.0573106967f, 0.722373366f } } } }, // A
  8954. { { 30000, 48065, 2, 3, 0, 0, 2, 45820, -900, 0, 0, 0, 0, 900, 1, 2, 3 }, 8, 0, { Gold { { 100, -0.06870462f, 0.0430903696f }, { 1000, -0.581374586f, -0.0143744675f }, { 2047, 0.450544685f, 0.249298006f }, { 3000, -0.47477895f, 0.101792879f }, { 4095, -0.220592439f, -0.244846866f } }, Gold { { 100, 0.0f, 0.0f }, { 1000, 0.0f, 0.0f }, { 2047, 0.0f, 0.0f }, { 3000, 0.0f, 0.0f }, { 4095, 0.0f, 0.0f } }, Gold { { 100, -0.0655785128f, 0.674460888f }, { 1000, -0.335665613f, -1.06249368f }, { 2047, 0.535273552f, 0.467255145f }, { 3000, -0.507586002f, -0.989780664f }, { 4095, -0.696575642f, 0.500667155f } }, Gold { { 100, -0.293243587f, 3.01594687f }, { 1000, -1.50097573f, -4.7510891f }, { 2047, 2.39354587f, 2.08939266f }, { 3000, -2.26973724f, -4.42593384f }, { 4095, -3.11482906f, 2.23879886f } } } }, // B
  8955. { { 40000, 10000, 0, 7, 0, 1, 2, 45820, 0, 200, 0, 0, 0, 0, 1, 2, 3 }, 0, 0, { Gold { { 100, 0.0171653964f, 0.0719543025f }, { 1000, 0.260783046f, -0.0546497703f }, { 2047, -0.236395031f, -0.087237671f }, { 3000, 0.242068797f, -0.103661537f }, { 4095, 0.169910118f, 0.140984371f } }, Gold { { 100, 0.00201279344f, -1.5109539f }, { 1000, 0.290754616f, 1.94614983f }, { 2047, -1.13570976f, -1.65917897f }, { 3000, 1.00995052f, 1.43215704f }, { 4095, 1.99373484f, -0.085176453f } }, Gold { { 100, -0.000444336678f, -0.000979390694f }, { 1000, -5.45688827e-08f, 7.50747904e-06f }, { 2047, 3.00044505e-08f, -6.34059734e-06f }, { 3000, 6.5076847e-08f, 5.3382596e-06f }, { 4095, -2.39666864e-08f, -2.14470973e-07f } }, Gold { { 100, 0.0178572834f, -0.781959713f }, { 1000, 0.424914241f, 1.04396117f }, { 2047, -0.877504408f, -1.02385247f }, { 3000, 0.812187135f, 0.704798162f }, { 4095, 1.2953757f, 0.0929019526f } } } }, // C
  8956. { { 15000, 45000, 1, 4, 0, 1, 2, 45820, 0, 0, 0, -1800, 0, 1800, 1, 2, 3 }, 2, 0, { Gold { { 100, -0.00273601874f, 0.0286290813f }, { 1000, 0.0418281667f, 0.0362898856f }, { 2047, -0.0745894462f, 0.00119503727f }, { 3000, 0.069945626f, 0.0419373475f }, { 4095, 0.103130348f, -0.0348644778f } }, Gold { { 100, 0.0f, 0.0f }, { 1000, 0.0f, 0.0f }, { 2047, 0.0f, 0.0f }, { 3000, 0.0f, 0.0f }, { 4095, 0.0f, 0.0f } }, Gold { { 100, 1.42701356e-05f, -0.000351782364f }, { 1000, 2.80272197e-05f, 0.000625129556f }, { 2047, -4.9891918e-05f, 2.06105178e-05f }, { 3000, 4.68442995e-05f, 0.000722356897f }, { 4095, 6.91349633e-05f, -0.000600588392f } }, Gold { { 100, 0.0475500524f, -0.553698778f }, { 1000, 0.508395135f, 0.997257531f }, { 2047, -0.906660676f, 0.0328744054f }, { 3000, 0.850252867f, 1.15249836f }, { 4095, 1.25358307f, -0.958129764f } } } }, // D
  8957. { { 22000, 50000, 6, 5, 0, 1, 2, 45820, 0, 0, 0, 0, 0, 0, 1, 2, 3 }, 0, 0, { Gold { { 100, 3.86351439e-06f, 0.000115638592f }, { 1000, -0.00223262212f, -4.85793389e-05f }, { 2047, 0.000411115587f, -2.57531383e-06f }, { 3000, -0.000485932804f, 7.87844056e-06f }, { 4095, 0.000189639119f, -4.06740628e-06f } }, Gold { { 100, -0.00987921655f, -0.683375061f }, { 1000, -0.876032948f, 1.17169452f }, { 2047, 0.424173862f, -0.340972483f }, { 3000, -0.499300569f, 1.1738565f }, { 4095, 0.195038065f, -0.732422411f } }, Gold { { 100, -2.29351427e-09f, -9.91509751e-07f }, { 1000, 2.00191561e-08f, 1.13525857e-06f }, { 2047, 2.50544758e-07f, -4.20314734e-07f }, { 3000, 1.65429199e-07f, 1.16024421e-06f }, { 4095, -7.31824983e-08f, -5.04818445e-07f } }, Gold { { 100, -0.00987535529f, -0.683260441f }, { 1000, -0.87826556f, 1.17164707f }, { 2047, 0.424585223f, -0.340975463f }, { 3000, -0.499786317f, 1.17386556f }, { 4095, 0.195227623f, -0.732426941f } } } }, // E
  8958. { { 25000, 48065, 3, 3, 1, 0, 1, 30000, 0, 0, 0, 0, 0, 0, 1, 2, 3 }, 0, 768, { Gold { { 100, 0.0f, 0.0f }, { 1000, 0.119619228f, 0.00655745994f }, { 2047, -0.258448333f, 0.0300254505f }, { 3000, 0.257131875f, 0.0218825918f }, { 4095, 0.243620604f, -0.0373107828f } }, Gold { { 100, 0.0f, 0.0f }, { 1000, 0.0f, 0.0f }, { 2047, 0.0f, 0.0f }, { 3000, 0.0f, 0.0f }, { 4095, 0.0f, 0.0f } }, Gold { { 100, 0.0f, 0.0f }, { 1000, 0.679369271f, 0.20265615f }, { 2047, -1.23796761f, 0.901739538f }, { 3000, 1.23174798f, 0.658331215f }, { 4095, 1.16627932f, -1.12146437f } }, Gold { { 100, 0.0f, 0.0f }, { 1000, 0.798988521f, 0.209213614f }, { 2047, -1.49641597f, 0.93176496f }, { 3000, 1.48887992f, 0.680213809f }, { 4095, 1.40989995f, -1.15877521f } } } }, // F
  8959. { { 20000, 45000, 2, 6, 1, 1, 1, 10000, 0, 0, 0, 0, 0, 0, 1, 2, 3 }, 0, 768, { Gold { { 100, 0.0f, 0.0f }, { 1000, 0.0390846208f, 0.00320863165f }, { 2047, -0.0802573487f, 0.0139643671f }, { 3000, 0.0798483118f, 0.0101885898f }, { 4095, 0.0756546259f, -0.0173618793f } }, Gold { { 100, 0.0f, 0.0f }, { 1000, 0.75990212f, 0.206010818f }, { 2047, -1.41615629f, 0.91779983f }, { 3000, 1.40902948f, 0.670024157f }, { 4095, 1.33424342f, -1.14141202f } }, Gold { { 100, 0.0f, 0.0f }, { 1000, 1.7614849e-06f, -5.86210172e-06f }, { 2047, -2.34208687e-06f, 7.51287303e-07f }, { 3000, 2.19281083e-06f, 1.06230493e-06f }, { 4095, 1.8608597e-06f, -1.35364621e-06f } }, Gold { { 100, 0.0f, 0.0f }, { 1000, 0.798988521f, 0.2092136f }, { 2047, -1.49641597f, 0.93176496f }, { 3000, 1.48887992f, 0.680213809f }, { 4095, 1.40989995f, -1.15877521f } } } }, // G
  8960. };
  8961. auto [l, r] = stereoFxInput();
  8962. for (const auto& c : cases)
  8963. {
  8964. auto words = c.words;
  8965. words.insert (words.begin(), 4);
  8966. for (const int w : { 0, 0, 0, c.mute })
  8967. words.push_back (w);
  8968. std::vector<std::uint8_t> st;
  8969. for (const auto w : words)
  8970. for (int b = 0; b < 4; ++b)
  8971. st.push_back ((std::uint8_t) ((std::uint32_t) w >> (8 * b)));
  8972. st.push_back (0);
  8973. st.resize (st.size() + 24, 0);
  8974. const auto s = lynx::fsplit::parseState (st);
  8975. CHECK (s.has_value());
  8976. if (! s)
  8977. continue;
  8978. lynx::fsplit::Engine e;
  8979. for (int i = 0; i < lynx::fsplit::engineParams; ++i)
  8980. e.setParameter (i, s->params[(std::size_t) i]);
  8981. e.prepare (44100.0);
  8982. e.reset (false);
  8983. std::array<std::vector<float>, 8> o;
  8984. for (auto& v : o)
  8985. v.assign (4096, 0.0f);
  8986. for (int b = 0; b < 4096; b += 512)
  8987. {
  8988. float* out[8];
  8989. for (int k = 0; k < 8; ++k)
  8990. out[k] = o[(std::size_t) k].data() + b;
  8991. e.process (l.data() + b, r.data() + b, out, 512);
  8992. }
  8993. CHECK (e.latencySamples() == c.latency);
  8994. const auto tol = c.latency > 0 ? 2e-5f : 2e-6f;
  8995. for (int k = 0; k < 4; ++k)
  8996. for (const auto& [i, gl, gr] : c.gold[(std::size_t) k])
  8997. CHECK (std::abs (o[(std::size_t) (2 * k)][(std::size_t) i] - gl) <= tol * std::max (1.0f, std::abs (gl))
  8998. && std::abs (o[(std::size_t) (2 * k + 1)][(std::size_t) i] - gr) <= tol * std::max (1.0f, std::abs (gr)));
  8999. // the mixer effect: the summed output (FL's main output) of an instance that has been running, and a
  9000. // Patcher bus = the bands sent to it
  9001. auto slot = lynx::effectFromFl ("Frequency Splitter", st);
  9002. CHECK (slot && slot->kind == lynx::EffectKind::frequencySplitter);
  9003. const auto head = lynx::patcherBranchHead ("Frequency Splitter", st, 3);
  9004. CHECK (head.valid && head.slot && head.slot->params[lynx::fsplit::outputHigh] == 1.0f
  9005. && head.slot->params[lynx::fsplit::outputLow] == 0.0f);
  9006. if (slot && head.slot)
  9007. {
  9008. lynx::fsplit::Engine settled;
  9009. for (int i = 0; i < lynx::fsplit::engineParams; ++i)
  9010. settled.setParameter (i, s->params[(std::size_t) i]);
  9011. settled.prepare (44100.0);
  9012. settled.reset (true);
  9013. std::vector<float> sl (4096), sr (4096), hl (4096), hr (4096);
  9014. float* out[8] { nullptr, nullptr, nullptr, nullptr, hl.data(), hr.data(), sl.data(), sr.data() };
  9015. settled.process (l.data(), r.data(), out, 4096);
  9016. const lynx::EffectSlot* both[2] { &*slot, &*head.slot };
  9017. for (const auto* p : both)
  9018. {
  9019. auto fx = lynx::makeEffect (*p);
  9020. fx->prepare (44100.0);
  9021. auto ml = l, mr = r;
  9022. for (int b = 0; b < 4096; b += 300)
  9023. fx->process (ml.data() + b, mr.data() + b, std::min (300, 4096 - b), p->params, 120.0);
  9024. const auto& wl = p == &*slot ? sl : hl;
  9025. const auto& wr = p == &*slot ? sr : hr;
  9026. CHECK (ml == wl && mr == wr);
  9027. CHECK (fx->latencySamples() == c.latency);
  9028. }
  9029. }
  9030. }
  9031. CHECK (! lynx::patcherBranchHead ("Frequency Splitter", std::vector<std::uint8_t> (0x58, 0), 2).valid); // no band sent to bus 2
  9032. CHECK (lynx::fsplit::paramText (2, 3.0f / 7.0f) == "24 dB/oct" && lynx::fsplit::paramText (11, 0.5f) == "0.0 dB");
  9033. CHECK (lynx::fsplit::paramText (0, 0.0f) == "30.0 Hz" && lynx::fsplit::paramText (4, 1.0f) == "FFT");
  9034. }
  9035. TEST (stereo_enhancer_transient_spreader_engines)
  9036. {
  9037. // REA_rodada54 §3–5: own states against the installation's DLLs (goldens = the DLL's output, EXEC)
  9038. auto se = lynx::effectFromFl ("Fruity Stereo Enhancer", i32State ({ -18, 300, -54, -148, 1, 1 }));
  9039. CHECK (se && se->kind == lynx::EffectKind::stereoEnhancer);
  9040. if (se)
  9041. checkStereoFx (*se, { { 100, 0.0f, 0.00309863733f }, { 1000, 0.0101111531f, -0.110266283f }, { 2047, -0.688752949f, -1.93998075f },
  9042. { 3000, -0.283846498f, -0.739818454f }, { 4095, 0.191147864f, 2.70425582f } });
  9043. auto tpState = i32State ({ 1, 138, 116, 64, 90, 2, 0, 70, 100, 1 });
  9044. tpState.push_back (5);
  9045. for (const auto w : i32State ({ 0, 50 }))
  9046. tpState.push_back (w);
  9047. auto tp = lynx::effectFromFl ("Transient Processor", tpState);
  9048. CHECK (tp && tp->kind == lynx::EffectKind::transientProcessor);
  9049. if (tp)
  9050. checkStereoFx (*tp, { { 100, 0.0568947345f, 0.124077775f }, { 1000, -0.326598823f, -0.193128616f }, { 2047, 0.612462878f, -0.0517415144f },
  9051. { 3000, -0.535302937f, -0.446303189f }, { 4095, -0.634670973f, 0.316044629f } });
  9052. auto spState = i32State ({ 0x10003, 7861, 3215, 106000, 1, 1, 0 });
  9053. spState.push_back (1);
  9054. spState.push_back (1);
  9055. auto sp = lynx::effectFromFl ("Spreader", spState);
  9056. CHECK (sp && sp->kind == lynx::EffectKind::spreader);
  9057. if (sp)
  9058. checkStereoFx (*sp, { { 100, 0.11841587f, -0.0958916694f }, { 1000, 1.40852344f, 0.087452054f }, { 2047, 1.07267976f, -2.02493715f },
  9059. { 3000, -0.781082511f, 0.811192274f }, { 4095, -1.80673707f, 0.89967829f } });
  9060. // unknown layouts keep the earlier fallback; FL automation maps one to one
  9061. CHECK (! lynx::stereofx::parseSpreader (i32State ({ 0x10001, 1, 2, 3, 4, 5 })));
  9062. CHECK (! lynx::stereofx::parseTransientProcessor (i32State ({ 2, 1, 2, 3, 4, 5, 6, 7, 8, 9 })));
  9063. const auto map = lynx::effectParamFromFl ("Transient Processor", 6);
  9064. CHECK (map && map->param == 6 && map->convert (0.25f) == 0.25f);
  9065. }
  9066. TEST (fruity_dynamics_engines)
  9067. {
  9068. // REA_rodada48: Fruity Limiter / Fruity Compressor, states of our own against the installation's DLLs
  9069. // (goldens = the DLL's output for the same input, EXEC; blocks of 512 at 44.1 kHz)
  9070. std::vector<float> inL (4096), inR (4096), side (4096);
  9071. for (int i = 0; i < 4096; ++i)
  9072. {
  9073. 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));
  9074. inR[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / 44100.0));
  9075. side[(std::size_t) i] = (float) (0.9 * std::sin (2.0 * 3.141592653589793 * 55.0 * i / 44100.0));
  9076. }
  9077. auto put = [] (std::vector<std::uint8_t>& b, std::int32_t v) {
  9078. std::uint8_t q[4];
  9079. std::memcpy (q, &v, 4);
  9080. b.insert (b.end(), q, q + 4);
  9081. };
  9082. auto limiterState = [&] (std::array<int, 18> p, int k = 0, int exact = 0) {
  9083. std::vector<std::uint8_t> b;
  9084. put (b, 7);
  9085. for (auto v : p)
  9086. put (b, v);
  9087. b.resize (0xA4, 0);
  9088. put (b, k);
  9089. b.push_back ((std::uint8_t) exact);
  9090. return b;
  9091. };
  9092. auto compressorState = [&] (int version, std::array<int, 6> p, int flags, int k = 0) {
  9093. std::vector<std::uint8_t> b;
  9094. put (b, version);
  9095. for (auto v : p)
  9096. put (b, v);
  9097. put (b, flags);
  9098. if (version > 2)
  9099. put (b, k);
  9100. return b;
  9101. };
  9102. struct Out { std::vector<float> l, r; int latency = 0; };
  9103. auto run = [&] (const lynx::EffectSlot& slot, bool sidechain) {
  9104. auto fx = lynx::makeEffect (slot);
  9105. fx->prepare (44100.0);
  9106. Out o { inL, inR };
  9107. for (int s = 0; s < 4096; s += 512)
  9108. {
  9109. if (sidechain)
  9110. fx->setSidechain (side.data() + s, side.data() + s);
  9111. fx->process (o.l.data() + s, o.r.data() + s, 512, slot.params, 120.0);
  9112. }
  9113. o.latency = fx->latencySamples();
  9114. return o;
  9115. };
  9116. auto check = [] (const Out& out, const std::vector<std::tuple<int, float, float>>& golden) {
  9117. for (const auto& [i, gl, gr] : golden)
  9118. CHECK (std::abs (out.l[(std::size_t) i] - gl) < 2e-6f && std::abs (out.r[(std::size_t) i] - gr) < 2e-6f);
  9119. };
  9120. auto limiter = [] (const std::vector<std::uint8_t>& st) {
  9121. auto slot = lynx::effectFromFl ("Fruity Limiter", st);
  9122. CHECK (slot && slot->kind == lynx::EffectKind::fruityLimiter);
  9123. return *slot;
  9124. };
  9125. // look-ahead limiter: +4 dB gain into a −2.5 dB ceiling, 2 ms attack (88 samples of latency); bit-identical
  9126. 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);
  9127. CHECK (a.latency == 88);
  9128. check (a, { { 100, 0.0176312141f, 0.621841073f }, { 1000, 0.401142687f, -0.442239583f }, { 2047, -0.19392401f, 0.499310732f },
  9129. { 3000, 0.22835508f, -0.267458469f }, { 4095, -0.089728713f, -0.107289009f } });
  9130. // compressor section with a positive knee, 2 compressor curve stages and soft saturation (port within −143 dB)
  9131. check (run (limiter (limiterState ({ 1000, 900, 1000, 0, 3, 5000, 3, 2882, 400, 600, 400, 1115, 6666, 2, 650, 1000, 0, 4187 })), false),
  9132. { { 100, -0.00208989787f, 0.0100310864f }, { 1000, -0.0545219742f, -0.0651496574f }, { 2047, 0.177322432f, -0.230846375f },
  9133. { 3000, -0.159136176f, -0.183688983f }, { 4095, -0.300143838f, 0.299418598f } });
  9134. // sidechained compressor (side input 1) with a negative knee, noise gate and the exact-ceiling option
  9135. 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),
  9136. { { 100, -0.00186592329f, 0.00895605329f }, { 1000, -0.0375732854f, -0.0448972471f }, { 2047, 0.124697573f, -0.162336946f },
  9137. { 3000, -0.111069582f, -0.128206298f }, { 4095, -0.218446419f, 0.21791859f } });
  9138. auto compressor = [] (const std::vector<std::uint8_t>& st) {
  9139. auto slot = lynx::effectFromFl ("Fruity Compressor", st);
  9140. CHECK (slot && slot->kind == lynx::EffectKind::fruityCompressor);
  9141. return *slot;
  9142. };
  9143. // Vintage type, look-ahead flag, side input 1: bit-identical
  9144. const auto ca = run (compressor (compressorState (3, { -200, 40, 30, 50, 300, 2 }, 1, 1)), true);
  9145. CHECK (ca.latency == 64);
  9146. check (ca, { { 100, 0.0795554966f, -0.230549783f }, { 1000, 0.483903229f, 0.0241768137f }, { 2047, -0.596430957f, -0.441769004f },
  9147. { 3000, 0.597724319f, -0.18129006f }, { 4095, 0.607042372f, 0.435067475f } });
  9148. // Soft/R, version 2 without the flag (the output is still 64 frames late, but not reported): bit-identical
  9149. const auto cb = run (compressor (compressorState (2, { -150, 80, 0, 150, 100, 7 }, 0)), false);
  9150. CHECK (cb.latency == 0);
  9151. check (cb, { { 100, 0.0813739002f, -0.235819489f }, { 1000, 0.264188439f, 0.0131994039f }, { 2047, -0.260424435f, -0.192893147f },
  9152. { 3000, 0.251500547f, -0.0762802213f }, { 4095, 0.252972007f, 0.181305125f } });
  9153. // the slot's parameters carry FL's raw controls exactly (and survive the 4-decimal project file)
  9154. const auto st = limiterState ({ 1261, 995, 1303, 1115, 3, 5000, 3, 2882, 264, 244, -590, 1115, 6666, 1, 650, 1000, 0, 4187 }, 1);
  9155. const auto parsed = lynx::fldyn::parseLimiter (st);
  9156. CHECK (parsed && parsed->sidechain == 1 && ! parsed->exactCeiling);
  9157. const auto slot = limiter (st);
  9158. for (int i = 0; i < lynx::fldyn::limiterControls; ++i)
  9159. {
  9160. const auto rounded = std::round (slot.params[(std::size_t) i] * 10000.0f) / 10000.0f;
  9161. 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]);
  9162. }
  9163. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityLimiter, 3, slot.params[3], 120.0) == "2.0 ms");
  9164. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityCompressor, 5, 2.0f / 7.0f, 120.0) == "Vintage");
  9165. // unreadable states keep the old approximation
  9166. CHECK (lynx::effectFromFl ("Fruity Limiter", { 7, 0, 0, 0 }).has_value() == false
  9167. || lynx::effectFromFl ("Fruity Limiter", { 7, 0, 0, 0 })->kind != lynx::EffectKind::fruityLimiter);
  9168. CHECK (! lynx::fldyn::parseCompressor ({ 4, 0, 0, 0 }));
  9169. }
  9170. TEST (maximus_engine)
  9171. {
  9172. // REA_rodada40: the engine on its own (MaximusProcessor), states of our own against the installation's DLLs
  9173. // (goldens = the DLL's output for the same input, EXEC), and the containers of the mixer effect and Vocodex
  9174. std::vector<float> inL (4096), inR (4096);
  9175. for (int i = 0; i < 4096; ++i)
  9176. {
  9177. 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));
  9178. inR[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / 44100.0));
  9179. }
  9180. auto run = [&] (std::shared_ptr<const lynx::hlfx::MaximusPreset> preset, int mix) {
  9181. lynx::hlfx::MaximusProcessor m;
  9182. m.prepare (std::move (preset), 44100.0, mix);
  9183. std::vector<float> l (inL), r (inR);
  9184. for (int s = 0; s < 4096; s += 512)
  9185. m.process (l.data() + s, r.data() + s, 512);
  9186. return std::make_pair (l, r);
  9187. };
  9188. using Env = std::vector<std::array<double, 4>>;
  9189. const Env def { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 } };
  9190. const Env knee { { 0.0, 0.0, 0.0, 0 }, { 0.35, 0.35, 0.0, 0 }, { 0.65, 0.6, 0.36, 7 } };
  9191. const Env t0 { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.14, 0 }, { 0.5, 0.5, 0.0, 0 } };
  9192. const Env m0 { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 }, { 0.5, 0.6, 0.085, 0 } };
  9193. 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,
  9194. int sus = 2882, int rms = 0) { return std::array<int, 13> { mode, pre, post, sep, sat, ceil, att, rel2, 2, 5000, 3, sus, rms }; };
  9195. auto check = [] (const std::pair<std::vector<float>, std::vector<float>>& out, const std::vector<std::tuple<int, float, float>>& golden) {
  9196. for (const auto& [i, gl, gr] : golden)
  9197. CHECK (std::abs (out.first[(std::size_t) i] - gl) < 2e-6f && std::abs (out.second[(std::size_t) i] - gr) < 2e-6f);
  9198. };
  9199. // RMS detectors, saturation mode B, a 5 Hz low cut, 12 dB high split (SG_x64.dll: port within −152 dB)
  9200. auto st3 = maximusTestState ({ 19800, 1, 48065, 0, 1115, 1000, 0 },
  9201. { band (0, 1500, 1000, 0, 400, 900, 1115, 0, 2882, 1), band (0, 1340, 1000, 0, 0, 1000, 1115, 0, 1570, 1),
  9202. band (1, 1000, 1200, 0, 1000, 1000), band (0, 1000, 1000, 0, 250, 1000, 1115, 4464, 2882, 1) },
  9203. { def, def, knee, def, t0, def, m0, def }, 0);
  9204. const auto p3 = lynx::hlfx::makeMaximusPreset (st3);
  9205. CHECK (p3 != nullptr);
  9206. check (run (p3, 1000), { { 100, 0.0f, 0.0f }, { 1000, 0.87061065f, 0.64004499f }, { 2047, -0.97266865f, 0.60940075f },
  9207. { 3000, 0.96658099f, 0.9250561f }, { 4095, 0.99094611f, -0.964059f } });
  9208. // the same with solo on (UI byte 0x1EF) and the low band selected (0x1E0): bit-identical to SG_x64.dll
  9209. auto solo = st3;
  9210. solo[0x1ef] = 1;
  9211. std::memset (solo.data() + 0x1e0, 0, 4);
  9212. check (run (lynx::hlfx::makeMaximusPreset (solo), 1000),
  9213. { { 100, 3.2922967e-06f, 0.00036916698f }, { 1000, 0.15653539f, -0.047913823f }, { 2047, -0.17336531f, 0.019099623f },
  9214. { 3000, 0.17185682f, -0.017173048f }, { 4095, 0.16714551f, 0.019473195f } });
  9215. // the legacy linear-phase FIR band split (UI byte 0x1FD; SG_x64.dll: port within −148 dB)
  9216. auto fir = st3;
  9217. fir[0x1fd] = 1;
  9218. check (run (lynx::hlfx::makeMaximusPreset (fir), 1000),
  9219. { { 100, 0.0f, 0.0f }, { 1000, 0.97986746f, 0.18555209f }, { 2047, -0.98930448f, -0.95970845f },
  9220. { 3000, 0.98856646f, -0.44592777f }, { 4095, 0.99069679f, 0.94382554f } });
  9221. // Soundgoodizer_x64.dll's own build with a v4 file (its presets' layout): a copy of the round-36 state without
  9222. // oversampling, through a byte-identical copy of the installation's DLL with this file as its preset (EXEC)
  9223. const auto st2 = maximusTestState ({ 19800, 1, 48065, 1, 891, 600, 32768 },
  9224. { band (1, 1264, 1296, 500, -500, 1200), band (0, 1340, 1000, -348, 0, 1000, 1115, 0, 1570),
  9225. band (0, 1364, 1152, 0, 0, 1000, 1115, 0, 1186), band (0, 1000, 1000, 0, -196, 1000, 1115, 4464, 1538) },
  9226. { def, def, knee, def, t0, def, m0, def }, 0);
  9227. std::vector<std::uint8_t> v4 { 4, 0, 0, 0 };
  9228. v4.insert (v4.end(), st2.begin() + 4, st2.begin() + 0x1f5);
  9229. v4.insert (v4.end(), st2.begin() + 0x1fe, st2.end());
  9230. const auto p4 = lynx::hlfx::makeMaximusPreset (v4, lynx::hlfx::MaximusBuild::soundgoodizer);
  9231. CHECK (p4 != nullptr);
  9232. check (run (p4, 600), { { 100, 0.0f, 0.0f }, { 1000, -0.60926896f, -0.6652832f }, { 2047, 0.40872875f, 0.36375371f },
  9233. { 3000, -0.41941205f, -0.61312866f }, { 4095, -0.12878418f, 0.27944946f } });
  9234. CHECK (lynx::hlfx::makeMaximusPreset (st2, lynx::hlfx::MaximusBuild::soundgoodizer) != nullptr); // v7: also read by that build
  9235. // the mixer effect's container and the effect itself
  9236. lynx::maximusfx::Blob blob;
  9237. blob.build = lynx::hlfx::MaximusBuild::soundgoodizer;
  9238. blob.mix = 600;
  9239. blob.state = v4;
  9240. const auto packed = lynx::maximusfx::pack (blob);
  9241. const auto back = lynx::maximusfx::unpack (packed);
  9242. CHECK (back && back->build == blob.build && back->mix == 600 && back->state == v4);
  9243. auto slot = lynx::makeEffectSlot (lynx::EffectKind::maximus);
  9244. slot.state = packed;
  9245. auto fx = lynx::makeEffect (slot);
  9246. fx->prepare (44100.0);
  9247. {
  9248. std::vector<float> l (inL), r (inR);
  9249. for (int s = 0; s < 4096; s += 1024) // longer host blocks are split into the engine's 512-frame calls
  9250. fx->process (l.data() + s, r.data() + s, 1024, slot.params, 120.0);
  9251. const auto ref = run (p4, 600);
  9252. CHECK (l == ref.first && r == ref.second);
  9253. }
  9254. const std::vector<std::uint8_t> sgState { 3, 0, 0, 0, 2, 0, 0, 0, 0x78, 2, 0, 0 };
  9255. const auto sg = lynx::maximusfx::parseSoundgoodizer (sgState);
  9256. CHECK (sg && sg->preset == 2 && sg->amount == 632 && std::string (lynx::maximusfx::soundgoodizerPresetFile (2)) == "c.dat");
  9257. CHECK (lynx::soundgoodizerPresetPath (sgState) == "Plugins\\Fruity\\Effects\\Soundgoodizer\\Data\\Presets\\c.dat");
  9258. CHECK (lynx::soundgoodizerExact (sgState, v4).has_value() && ! lynx::soundgoodizerExact (sgState, {}).has_value());
  9259. // Vocodex's appended installation data survives a round trip and leaves FL's state untouched
  9260. const std::vector<std::uint8_t> flState { 8, 0, 0, 0, 1, 2, 3 };
  9261. lynx::vocodex::InstallData data;
  9262. data.sgDat = st2;
  9263. data.carrierState = { 9, 9, 9 };
  9264. const auto joined = lynx::vocodex::appendInstallData (flState, data);
  9265. const auto [raw, files] = lynx::vocodex::splitInstallData (joined);
  9266. CHECK (raw == flState && files.sgDat == st2 && files.carrierState == data.carrierState);
  9267. CHECK (lynx::vocodex::splitInstallData (flState).first == flState);
  9268. }
  9269. // ---- Fruity Reeverb 2 (REA_rodada49) --------------------------------------------------------------------------------
  9270. // Goldens: the installation's Fruity Reeverb 2_x64.dll (unmodified, rate set before the state, flushed, blocks of 512)
  9271. // on our own states and input; LYNX's engine is bit-identical to it.
  9272. TEST (reeverb2_engine)
  9273. {
  9274. auto hex = [] (const char* h) {
  9275. std::vector<std::uint8_t> v;
  9276. for (std::size_t i = 0; h[i] != 0 && h[i + 1] != 0; i += 2)
  9277. v.push_back ((std::uint8_t) std::stoi (std::string (h + i, 2), nullptr, 16));
  9278. return v;
  9279. };
  9280. constexpr int N = 8192;
  9281. std::vector<float> inL (N), inR (N);
  9282. std::uint32_t seed = 12345;
  9283. for (int i = 0; i < N; ++i)
  9284. {
  9285. seed = (seed * 1103515245u + 12345u) & 0x7fffffffu;
  9286. const auto nz = (float) ((double) seed / 2147483648.0 - 0.5);
  9287. const auto tri = (float) ((i % 200) - 100) / 100.0f;
  9288. const float g = i < 4000 ? 1.0f : 0.0f;
  9289. inL[(std::size_t) i] = 0.5f * tri * g + (i >= 100 && i < 300 ? nz * 0.6f : 0.0f);
  9290. inR[(std::size_t) i] = 0.25f * tri * g;
  9291. }
  9292. struct Golden
  9293. {
  9294. int i;
  9295. float l, r;
  9296. };
  9297. auto run = [&] (const std::vector<std::uint8_t>& state, double bpm, const std::vector<Golden>& gold) {
  9298. const auto slot = lynx::reeverb2FromFl (state);
  9299. CHECK (slot.has_value() && slot->kind == lynx::EffectKind::reeverb2);
  9300. if (! slot)
  9301. return;
  9302. auto fx = lynx::makeEffect (*slot);
  9303. fx->prepare (44100.0);
  9304. fx->reset();
  9305. std::vector<float> l (inL), r (inR);
  9306. for (int s = 0; s < N; s += 512)
  9307. fx->process (l.data() + s, r.data() + s, 512, slot->params, bpm);
  9308. for (const auto& g : gold)
  9309. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  9310. };
  9311. // modulation, early reflections, negative separation, filters, predelay
  9312. run (hex ("112700004b000000780000001e0000004600000055000000190000003c0000009600000090010000e2ffffff6e000000640000008c00000084030000b00400000000"),
  9313. 120.0, { { 777, 0.263778746f, 0.131889373f }, { 1555, 0.187972173f, 0.0587911084f }, { 3333, 0.10398978f, 0.0566705838f },
  9314. { 5000, 0.087177977f, -0.0197417438f }, { 8191, 0.0769432634f, -0.282023013f } });
  9315. // Mid/Side input, tempo-synced predelay (27 ticks at 128 BPM), positive separation
  9316. run (hex ("112700004b000000280000001b00000032000000640000000f0000002800000064000000f401000028000000800000000000000040000000ac020000000000000101"),
  9317. 128.0, { { 777, 0.38499999f, 0.192499995f }, { 1555, 0.275000006f, 0.137500003f }, { 3333, 0.164420277f, 0.0826604739f },
  9318. { 5000, -0.00861243438f, -0.00140450173f }, { 8191, -0.00873835851f, 0.00888271444f } });
  9319. // version 10000 (no modulation controls), everything off or at its extreme, dry 0
  9320. run (hex ("1027000013000000dd000000000000006400000000000000c8000000dd00000014000000d00700004000000000000000a0000000a00000000000"),
  9321. 120.0, { { 777, 0.0f, 0.0f }, { 1555, 0.0f, 0.0f }, { 3333, 0.0632086769f, 0.0632086769f }, { 5000, -0.108072646f, -0.108072646f },
  9322. { 8191, 0.144686908f, 0.144686908f } });
  9323. // the parameters map back to FL's integers; unsupported states fall back to the approximation
  9324. lynx::reeverb2::Settings s;
  9325. s.p = { 3000, 5, 384, 1, 0, 200, 221, 20, 2000, -64, 160, 0, 160, 2048, 1 };
  9326. s.tempoPredelay = true;
  9327. lynx::EffectParams p {};
  9328. lynx::reeverb2::toParams (s, p);
  9329. CHECK (lynx::reeverb2::fromParams (p) == s);
  9330. CHECK (! lynx::reeverb2FromFl ({ 0x11, 0x27, 0, 0, 1, 0 }).has_value());
  9331. CHECK (! lynx::reeverb2FromFl ({ 0x12, 0x27, 0, 0 }).has_value());
  9332. const auto approx = lynx::effectFromFl ("Fruity Reeverb 2", { 0x11, 0x27, 0, 0 }, 120.0);
  9333. CHECK (! approx || approx->kind != lynx::EffectKind::reeverb2);
  9334. CHECK (lynx::reeverb2ParamText (0, 0.0f) == "off" && lynx::reeverb2ParamText (5, 1.0f) == "20.0 s");
  9335. // Fruity Reeverb (the first version): Fruity Reeverb_x64.dll goldens on our own states (denormals flushed, as the
  9336. // plugin runs and as LYNX renders)
  9337. lynx::ScopedFlushDenormals ftz;
  9338. auto run1 = [&] (const std::vector<std::uint8_t>& state, const std::vector<Golden>& gold) {
  9339. const auto slot = lynx::reeverb1FromFl (state);
  9340. CHECK (slot.has_value() && slot->kind == lynx::EffectKind::reeverb1);
  9341. if (! slot)
  9342. return;
  9343. auto fx = lynx::makeEffect (*slot);
  9344. fx->prepare (44100.0);
  9345. std::vector<float> l (inL), r (inR);
  9346. for (int s = 0; s < N; s += 512)
  9347. fx->process (l.data() + s, r.data() + s, 512, slot->params, 120.0);
  9348. for (const auto& g : gold)
  9349. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  9350. };
  9351. run1 (hex ("01000000204e0000204e000025000000409c000050c300000a00000030750000a861000050c3000060ea0000"),
  9352. { { 777, 0.293731689f, 0.146865845f }, { 1555, 0.20980835f, 0.104904175f }, { 3333, 0.0998796821f, 0.0754205957f },
  9353. { 5000, -0.0994476378f, 0.0116700521f }, { 8191, -0.141061321f, 0.0601170398f } });
  9354. run1 (hex ("0100000000000000000001000000000000000100000001002800000000000100000001000000000000000100"),
  9355. { { 777, -0.016678432f, -4.58686594e-31f }, { 1555, 0.0221031085f, 0.0230635051f }, { 3333, 0.0875115693f, 0.1695389f },
  9356. { 5000, -0.111421227f, -0.295867324f }, { 8191, 0.503498077f, 0.190456569f } });
  9357. CHECK (lynx::reeverb1FromFl (std::vector<std::uint8_t> (44, 0)).has_value());
  9358. CHECK (! lynx::reeverb1FromFl (std::vector<std::uint8_t> (40, 0)).has_value());
  9359. CHECK (lynx::reeverb1ParamText (5, 0.0f) == "Brighter" && lynx::reeverb1ParamText (0, 0.0f) == "off");
  9360. }
  9361. // ---- Fruity Love Philter (REA_rodada66) ---------------------------------------------------------------------------------
  9362. TEST (love_philter_engine)
  9363. {
  9364. // goldens: Fruity Love Philter_x64.dll on our own synthetic states (44.1 kHz, blocks of 512, 120 BPM, live mode),
  9365. // bit-identical on the test machine (denormals flushed, as the plugin runs and as LYNX renders)
  9366. lynx::ScopedFlushDenormals ftz;
  9367. auto hex = [] (const char* h) {
  9368. std::vector<std::uint8_t> v;
  9369. for (std::size_t i = 0; h[i] != 0 && h[i + 1] != 0; i += 2)
  9370. v.push_back ((std::uint8_t) std::stoi (std::string (h + i, 2), nullptr, 16));
  9371. return v;
  9372. };
  9373. constexpr int N = 8192;
  9374. std::vector<float> inL (N), inR (N);
  9375. std::uint32_t seed = 12345;
  9376. for (int i = 0; i < N; ++i)
  9377. {
  9378. seed = (seed * 1103515245u + 12345u) & 0x7fffffffu;
  9379. const auto nz = (float) ((double) seed / 2147483648.0 - 0.5);
  9380. const auto tri = (float) ((i % 200) - 100) / 100.0f;
  9381. const float g = i < 4000 ? 1.0f : 0.0f;
  9382. inL[(std::size_t) i] = 0.5f * tri * g + (i >= 100 && i < 300 ? nz * 0.6f : 0.0f);
  9383. inR[(std::size_t) i] = 0.25f * tri * g;
  9384. }
  9385. struct Golden
  9386. {
  9387. int i;
  9388. float l, r;
  9389. };
  9390. auto run = [&] (const std::vector<std::uint8_t>& state, const std::vector<Golden>& gold) {
  9391. const auto slot = lynx::effectFromFl ("Fruity Love Philter", state, 120.0);
  9392. CHECK (slot.has_value() && slot->kind == lynx::EffectKind::lovePhilter);
  9393. if (! slot || slot->kind != lynx::EffectKind::lovePhilter)
  9394. return;
  9395. auto fx = lynx::makeEffect (*slot);
  9396. fx->prepare (44100.0);
  9397. std::vector<float> l (inL), r (inR);
  9398. for (int s = 0; s < N; s += 512)
  9399. {
  9400. fx->setSongPosition (s * 2.0 * 960.0 / 44100.0);
  9401. fx->process (l.data() + s, r.data() + s, 512, slot->params, 120.0);
  9402. }
  9403. for (const auto& g : gold)
  9404. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  9405. };
  9406. // SVF triple with Low/Band/High, a song-locked volume pattern with tension, a cutoff LFO; Lime HP fed by NEXT, panned
  9407. run (hex (
  9408. "03000c00789cedda316ed44018c5f1cfd90b5050234a0e81702a1a6e4045c11d68e7005c00c40928e96810254590380225e552538471ec"
  9409. "d97c314c841e0259cfff91463bf66e7e3b9ac42fdf582e1151961e776abf1f314ce3b3a5d7f6e4fb8bf1b2b6693c5cf56118e6b79ed5fe"
  9410. "b2f6b7b53fadfd41ed8f1379378d9fd77e88a50d57c3c3f597dcd28ee3fcfaadbe9e8fcb24e272d56238fd4019a73e2c5f3074c1ebf732"
  9411. "745a8ee51507670bce99e89cbff9e5076eb9f8be8e378f9b7df3fcfae2dbba37afcb9c0b391b0eb16ec795ff657cfd6a6aefc7cf17178f"
  9412. "f2f9f6f9fafe8769342c7dfabe58e65e62db7f5338fb74fe4f96acafa5e3f8bbf3eb6b75eb1e75068eb3d3b241b95ef2ffc0d67ebc8bb8"
  9413. "b792b65e2ffc9bfa636a64068e9fc3de44f77a7b93adfc6e7170fec661afa17b64038eb34336e81ed980e3ec900dba4736e0383b6483ee"
  9414. "957aee61ed9fe27a4d63194ffd63fdccf44cc7f972fef42408cf6ce0e06c30a7b6711f93fba238387fee50c3e81e75038eb343dda07b64"
  9415. "038eb34336e81e7b0a1c67873d85ee910d38ce0ed9a07b64038eb34336e81ed980e3ec900dba57625ec3d65bcbc7799cd6b83f95ded478"
  9416. "d60267c70ef745758f1a06c7d9a186d13dea061c6787ba41f7c8061c67876cd03df61438ce0e7b0add231b709c1db241f7c8061c67876c"
  9417. "d03db201c7d9211b74afc4bc86adb7968ff338ad717f2abda9f1ac05ce8e1dee8bea1e350c8eb3430da37bd40d38ce0e7583ee910d38ce"
  9418. "0ed9a07bec29709c1df614ba4736e0383b6483ee910d38ce0ed9a07b64038eb34336e85e89790d5b6f2d1fe7715ae3fe547a53e3590b9c"
  9419. "1d3bdc17d53d6a181c67871a46f7a81b709c1dea06dd231b709c1db241f7d853e0383bec29748f6cc07176c806dd231b709c1db241f7c8"
  9420. "061c67876cd0bd12f31ab6de5a3ecee3b4c6fda9f4a6c6b316383b76b82faa7bd43038ce0e358cee5137e0383bd40dba4736e0383b6483"
  9421. "eeb1a7c07176d853e81ed980e3ec900dba4736e0383b6483ee910d38ce0ed9a07b25e6356cbdb57c9cc7698dfb53e94d8d672d7076ec70"
  9422. "5f54f7a861709c1d6a18dda36ec07176a81b748f6cc07176c806dd634f81e3ecb0a7d03db201c7d9211b748f6cc07176c806dd231b709c"
  9423. "1db241f74acc6bd87a6bf9388fd31af7a7d29b1acf5ae0ecd8e1bea8ee51c3e0383bd430ba47dd80e3ec5037e81ed980e3ec900dbac79e"
  9424. "02c7d9614fa17b64038eb34336e81ed980e3ec900dba4736e0383b6483eec54ff7970e90"),
  9425. { { 777, 0.710252046585083f, 0.19971221685409546f }, { 1555, 0.2758101224899292f, 0.07770585268735886f }, { 2222, -0.5605983734130859f, -0.14688193798065186f }, { 3333, -0.33108609914779663f, -0.07413796335458755f }, { 3999, 0.3015991151332855f, 0.07990600168704987f }, { 4100, 0.09223534911870956f, 0.021577393636107445f } });
  9426. // Mango alternate triple with drive, waveshaper, Mod X/Y maps (smoothed), follower map on the volume; ×4 oversampling
  9427. run (hex (
  9428. "03000c00789cedda4d8ed33018c671a788151b8ec0013803993dd7e00e6c7d0a24100bae81c40590e00e5d82c46238c1e0c879db7752b9"
  9429. "8b470c1f4ffe963c759af6371e4b79e675945a4aa96b2f4f5b7f56cad45e3e1d4a79be0c5a7bf9f3f57cd7da326e6fb7f3d3b49e5abef6"
  9430. "a1f537adbf2ae7968f4f7c6b8f5b7fb20ca6e951fb19fd7a3bceefde2eedfbf2fa22bfdf5f6fe7fce9bb4d2bd3e9549d973eadbf742adb"
  9431. "7686e25c86f2df51d7cfe0e0fc6de7203a37ef2fbe70ba200f17678ef3fde3b08f572fbe7fddebebd273816cc07173fe4c36dc0eaebdeb"
  9432. "ff981fcabb2c28cedfeb85c4c70befeb97a5fd580b8dcfcbfba773dbdf433d81e3ec3c7466a46bec9e1d457e7e777bedfd1ff5c4d2c806"
  9433. "1c3f87bd86eeb1d7c07176f6b8d7f85d1ed980e3ec900dba4736e0383b6483ee910d38ce0ed9a07bb5f4358c1e2d1fe7715ae3f1544653"
  9434. "fbd65e6fe650b65349363985e3e7705f54f7a861709c1d6a18dda36ec07176a81b748f6cc07176c806dd634f81e3ecb0a7d03db201c7d9"
  9435. "211b748f6cc07176c806dd231b709c1db241f76ae96b183d5a3ecee3b4c6e3a98ca6c6b316383b76b82faa7bd43038ce0e358cee5137e0"
  9436. "383bd40dba4736e0383b6483eeb1a7c07176d853e81ed980e3ec900dba4736e0383b6483ee910d38ce0ed9a07bb5f4358c1e2d1fe7715a"
  9437. "e3f1544653e3590b9c1d3bdc17d53d6a181c67871a46f7a81b709c1dea06dd231b709c1db241f7d853e0383bec29748f6cc07176c806dd"
  9438. "231b709c1db241f7c8061c67876cd0bd5afa1a468f968ff338adf1782aa3a9f1ac05ce8e1dee8bea1e350c8eb3430da37bd40d38ce0e75"
  9439. "83ee910d38ce0ed9a07bec29709c1df614ba4736e0383b6483ee910d38ce0ed9a07b64038eb34336e85e2d7d0da347cbc7799cd6783c95"
  9440. "d1d478d60267c70ef745758f1a06c7d9a186d13dea061c6787ba41f7c8061c67876cd03df61438ce0e7b0add231b709c1db241f7c8061c"
  9441. "67876cd03db201c7d9211b74af96be86d1a3e5e33c4e6b3c9eca686a3c6b81b36387fba2ba470d83e3ec50c3e81e75038eb343dda07b64"
  9442. "038eb34336e81e7b0a1c67873d85ee910d38ce0ed9a07b64038eb34336e81ed980e3ec900dba574b5fc3e8d1f2711ea7351e4f6534359e"
  9443. "b5c0d9b1c37d51dda386c17176a861748fba01c7d9a16ed03db201c7d9211b748f3d058eb3c39e42f7c8061c67876cd03db201c7d9211b"
  9444. "748f6cc07176c806dd2bbf0040771f82"),
  9445. { { 777, 0.30564039945602417f, 0.28222185373306274f }, { 1555, 0.262472540140152f, 0.23502512276172638f }, { 2222, 0.3336450159549713f, -0.030735457316040993f }, { 3333, 0.23046477138996124f, 0.20232810080051422f }, { 3999, 0.3282865881919861f, 0.300540566444397f }, { 4100, -1.5846843837152846e-07f, 2.583440448233887e-07f } });
  9446. // band-pass alternate dual with FLAT, low shelf, all-pass with a pan LFO and inverted input, CENTER, no oversampling
  9447. run (hex (
  9448. "03000c00789ceddd418ad44018c5f12f3303baf404e2d243083d7b6fe0dab55b5dd64106bc8427f024aea497ed09da8a95225ff75883bc"
  9449. "01092fff82a22b19fb97a2248faf62184b4494a5c7abdadf444cf3f866e9b5bdfff5f970ae2d9636cdad0de7af7da9fd6327623d5ffe32"
  9450. "be5b7afdfe6dfdb85d2ff2443b1ddae7b17edecfe33f173f5fb598d6ab1fe63e2d179886e0fab30c5dcf1d07670bce8de8dc3f3cfac213"
  9451. "37df8fc3e571b72fcf5fdf7c5bf7dabab45c201b70dc9cff930da7c1bd7712efe56d78d40d38ce0e7583ee910d38ce0ed9a07bec29709c"
  9452. "1df614ba4736e0383b6483ee910d38ce0ed9a07b64038eb34336e85e89b686bdf7968ff338adf1782aa3a9f1ae05ce8e1d9e8bea1e350c"
  9453. "8eb3430da37bd40d38ce0e7583ee910d38ce0ed9a07bec29709c1df614ba4736e0383b6483ee910d38ce0ed9a07b64038eb34336e85ea9"
  9454. "e73ed4feaef64fe9cf95a5bf8ef67b2fca727ebeeecb79c0efb5c0c1d9604e6de33926cf457170fedda186d13dea061c6787ba41f7c806"
  9455. "1c67876cd03df61438ce0e7b0add231b709c1db241f7c8061c67876cd03db201c7d9211b74afd473df6aff19eb9ac6322e69fc706eff05"
  9456. "c9bc8e2fda603c95d1d478d70267c74ecfa9c737cc7170ef1e2ffce9cafffeb5fdec6db477a222785ebab5bf731c9ce738d436ba473d81"
  9457. "e3ecf0efacba4736e0383b6483eeb1a7c07176d853e81ed980e3ec900dba4736e0383b6483ee910d38ce0ed9a07b25da1af6de5b3ecee3"
  9458. "b4c6e3a98ca6c63b18383b76782eaa7bd43038ce0e358cee5137e0383bd40dba4736e0383b6483eeb1a7c07176d853e81ed980e3ec900d"
  9459. "ba4736e0383b6483ee910d38ce0ed9a07b25da1af6de5b3ecee3b4c6e3a98ca6c6bb16383b76782eaa7bd43038ce0e358cee5137e0383b"
  9460. "d40dba4736e0383b6483eeb1a7c07176d853e81ed980e3ec900dba4736e0383b6483ee910d38ce0ed9a07b25da1af6de5b3ecee3b4c6e3"
  9461. "a98ca6c6bb16383b76782eaa7bd43038ce0e358cee5137e0383bd40dba4736e0383b6483eeb1a7c07176d853e81ed980e3ec900dba4736"
  9462. "e0383b6483ee910d38ce0ed9a07b25da1af6de5b3ecee3b4c6e3a98ca6c6bb16383b76782eaa7bd43038ce0e358cee5137e0383bd40dba"
  9463. "4736e0383b6483eeb1a7c07176d853e81ed980e3ec900dba4736e0383b6483ee910d38ce0ed9a07bf11b286e0820"),
  9464. { { 777, 0.5369307994842529f, 0.483487993478775f }, { 1555, -0.17133042216300964f, -0.10717462003231049f }, { 2222, 0.8671267032623291f, 0.6469616889953613f }, { 3333, -0.6548674702644348f, -0.5142713189125061f }, { 3999, 1.4368892908096313f, 1.1726332902908325f }, { 4100, 1.1042776107788086f, 0.8842349648475647f } });
  9465. // the slot exposes FL's controls; FL indices map both ways; unreadable states keep the old conversion
  9466. CHECK (lynx::philter::flIndexOf (6) == 6 && lynx::philter::flIndexOf (8) == 9 && lynx::philter::flIndexOf (8 + 5 * 7 + 4) == 8 + 44 * 7 + 9);
  9467. for (int i = 0; i < lynx::philter::paramCount; ++i)
  9468. CHECK (lynx::philter::paramOfFlIndex (lynx::philter::flIndexOf (i)) == i);
  9469. CHECK (lynx::philter::paramOfFlIndex (8 + 3) == -1 && lynx::philter::paramOfFlIndex (8 + 12) == -1);
  9470. CHECK (lynx::philter::parse ({ 3, 0, 12, 0, 1, 2, 3, 4 }) == nullptr);
  9471. CHECK (lynx::effectInfo (lynx::EffectKind::lovePhilter).params.size() == (std::size_t) lynx::philter::paramCount);
  9472. CHECK (lynx::philter::paramText (5, 0.0f) == "pattern 1" && lynx::philter::paramText (8 + 3, 0.5f) == "centred");
  9473. // a control edited in LYNX is applied when the effect is built from the slot (the state keeps FL's value)
  9474. {
  9475. const auto stateA = hex (
  9476. "03000c00789cedda316ed44018c5f1cfd90b5050234a0e81702a1a6e4045c11d68e7005c00c40928e96810254590380225e552538471ec"
  9477. "d97c314c841e0259cfff91463bf66e7e3b9ac42fdf582e1151961e776abf1f314ce3b3a5d7f6e4fb8bf1b2b6693c5cf56118e6b79ed5fe"
  9478. "b2f6b7b53fadfd41ed8f1379378d9fd77e88a50d57c3c3f597dcd28ee3fcfaadbe9e8fcb24e272d56238fd4019a73e2c5f3074c1ebf732"
  9479. "745a8ee51507670bce99e89cbff9e5076eb9f8be8e378f9b7df3fcfae2dbba37afcb9c0b391b0eb16ec795ff657cfd6a6aefc7cf17178f"
  9480. "f2f9f6f9fafe8769342c7dfabe58e65e62db7f5338fb74fe4f96acafa5e3f8bbf3eb6b75eb1e75068eb3d3b241b95ef2ffc0d67ebc8bb8"
  9481. "b792b65e2ffc9bfa636a64068e9fc3de44f77a7b93adfc6e7170fec661afa17b64038eb34336e81ed980e3ec900dba4736e0383b6483ee"
  9482. "957aee61ed9fe27a4d63194ffd63fdccf44cc7f972fef42408cf6ce0e06c30a7b6711f93fba238387fee50c3e81e75038eb343dda07b64"
  9483. "038eb34336e81e7b0a1c67873d85ee910d38ce0ed9a07b64038eb34336e81ed980e3ec900dba57625ec3d65bcbc7799cd6b83f95ded478"
  9484. "d60267c70ef745758f1a06c7d9a186d13dea061c6787ba41f7c8061c67876cd03df61438ce0e7b0add231b709c1db241f7c8061c67876c"
  9485. "d03db201c7d9211b74afc4bc86adb7968ff338ad717f2abda9f1ac05ce8e1dee8bea1e350c8eb3430da37bd40d38ce0e7583ee910d38ce"
  9486. "0ed9a07bec29709c1df614ba4736e0383b6483ee910d38ce0ed9a07b64038eb34336e85e89790d5b6f2d1fe7715ae3fe547a53e3590b9c"
  9487. "1d3bdc17d53d6a181c67871a46f7a81b709c1dea06dd231b709c1db241f7d853e0383bec29748f6cc07176c806dd231b709c1db241f7c8"
  9488. "061c67876cd0bd12f31ab6de5a3ecee3b4c6fda9f4a6c6b316383b76b82faa7bd43038ce0e358cee5137e0383bd40dba4736e0383b6483"
  9489. "eeb1a7c07176d853e81ed980e3ec900dba4736e0383b6483ee910d38ce0ed9a07b25e6356cbdb57c9cc7698dfb53e94d8d672d7076ec70"
  9490. "5f54f7a861709c1d6a18dda36ec07176a81b748f6cc07176c806dd634f81e3ecb0a7d03db201c7d9211b748f6cc07176c806dd231b709c"
  9491. "1db241f74acc6bd87a6bf9388fd31af7a7d29b1acf5ae0ecd8e1bea8ee51c3e0383bd430ba47dd80e3ec5037e81ed980e3ec900dbac79e"
  9492. "02c7d9614fa17b64038eb34336e81ed980e3ec900dba4736e0383b6483eec54ff7970e90");
  9493. auto slot = lynx::philter::slotFromFl (stateA);
  9494. CHECK (slot.has_value() && ! lynx::philter::slotFromFl (hex ("0300000078")).has_value());
  9495. if (slot)
  9496. {
  9497. auto render = [&] (const lynx::EffectSlot& sl) {
  9498. auto fx = lynx::makeEffect (sl);
  9499. fx->prepare (44100.0);
  9500. std::vector<float> l (inL), r (inR);
  9501. for (int s = 0; s < 4096; s += 512)
  9502. {
  9503. fx->setSongPosition (s * 2.0 * 960.0 / 44100.0);
  9504. fx->process (l.data() + s, r.data() + s, 512, sl.params, 120.0);
  9505. }
  9506. return l[3333];
  9507. };
  9508. const auto base = render (*slot);
  9509. auto edited = *slot;
  9510. edited.params[8] = 0.1f; // unit 1 cutoff
  9511. CHECK (base == 0.0f || base != render (edited));
  9512. CHECK (base == render (*slot));
  9513. }
  9514. }
  9515. }
  9516. TEST (fruity_vocoder_round66_goldens)
  9517. {
  9518. // REA_rodada66 §6 goldens against the round-70 engine (core/FruityVocoder), through FL's state import and the mixer
  9519. // effect: Fruity Vocoder_x64.dll on our own states (44.1 kHz, blocks of 512): left = modulator, right = carrier.
  9520. // The first state's attack (5 ms) is below the knob range (10…1000): the plugin uses it as it is (REA_rodada78 §1)
  9521. lynx::ScopedFlushDenormals ftz;
  9522. auto hex = [] (const char* h) {
  9523. std::vector<std::uint8_t> v;
  9524. for (std::size_t i = 0; h[i] != 0 && h[i + 1] != 0; i += 2)
  9525. v.push_back ((std::uint8_t) std::stoi (std::string (h + i, 2), nullptr, 16));
  9526. return v;
  9527. };
  9528. constexpr int N = 8192;
  9529. std::vector<float> inL (N), inR (N);
  9530. std::uint32_t seed = 12345;
  9531. for (int i = 0; i < N; ++i)
  9532. {
  9533. seed = (seed * 1103515245u + 12345u) & 0x7fffffffu;
  9534. const auto nz = (float) ((double) seed / 2147483648.0 - 0.5);
  9535. const auto tri = (float) ((i % 200) - 100) / 100.0f;
  9536. const float g = i < 4000 ? 1.0f : 0.0f;
  9537. inL[(std::size_t) i] = 0.5f * tri * g + (i >= 100 && i < 300 ? nz * 0.6f : 0.0f);
  9538. inR[(std::size_t) i] = 0.25f * tri * g;
  9539. }
  9540. struct Golden
  9541. {
  9542. int i;
  9543. float v;
  9544. };
  9545. auto run = [&] (const std::vector<std::uint8_t>& state, const std::vector<Golden>& gold) {
  9546. const auto slot = lynx::effectFromFl ("Fruity Vocoder", state, 120.0);
  9547. CHECK (slot.has_value() && slot->kind == lynx::EffectKind::vocoder);
  9548. if (! slot || slot->kind != lynx::EffectKind::vocoder)
  9549. return;
  9550. auto fx = lynx::makeEffect (*slot);
  9551. fx->prepare (44100.0);
  9552. std::vector<float> l (inL), r (inR);
  9553. for (int s = 0; s < N; s += 512)
  9554. fx->process (l.data() + s, r.data() + s, 512, slot->params, 120.0);
  9555. for (const auto& g : gold)
  9556. CHECK (l[(std::size_t) g.i] == g.v && r[(std::size_t) g.i] == g.v);
  9557. };
  9558. // 32 bands, order 3, inverted carrier bank with a formant shift, fast attack (5 ms, outside the knob range)
  9559. run (hex (
  9560. "02000000200000000300000002000000000000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000"
  9561. "803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f00"
  9562. "00803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803fb80b000050c30000640000000100000044fdffff"
  9563. "78000000050000002c010000000000008c0000003c000000a0000000"),
  9564. { { 777, 0.47870129346847534f }, { 1555, 0.2731437087059021f }, { 2222, -0.41599133610725403f }, { 3333, 0.083399198949337f }, { 3999, 0.7295553088188171f }, { 4100, 0.0871216207742691f } });
  9565. // 8 bands, swapped inputs, per-band levels, modulator level 0
  9566. run (hex (
  9567. "02000000080000000200000002000000010000003f9a99193f3333333fcdcc4c3f6666663f0000803fcdcc8c3f9a99993f1027000060ea"
  9568. "00001e000000000000002c010000500000000f0000003c00000000000000000000001400000080000000"),
  9569. { { 777, 0.07233863323926926f }, { 1555, 0.1183714047074318f }, { 2222, -0.6131120920181274f }, { 3333, 0.18050849437713623f }, { 3999, 0.056527279317379f }, { 4100, 0.05711518973112106f } });
  9570. }
  9571. // ---- Fruity WaveShaper / Fruity Soft Clipper (REA_rodada51) ------------------------------------------------------------
  9572. // States written here from our own values (no FL preset data); goldens from the installation's DLLs (EXEC, blocks of 512)
  9573. namespace
  9574. {
  9575. std::vector<std::uint8_t> waveShaperTestState (int pre, int mix, int post, bool uni, bool center, int os, bool hq, bool smooth)
  9576. {
  9577. std::vector<std::uint8_t> b;
  9578. 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))); };
  9579. 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))); };
  9580. 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))); };
  9581. i32 (0x40003);
  9582. i32 (pre), i32 (mix), i32 (post);
  9583. b.push_back (uni ? 1 : 0), b.push_back (center ? 1 : 0);
  9584. i32 (os);
  9585. 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 },
  9586. { 0.25, 0.9, -0.3f, 3 }, { 0.2, 1.0, 0.5f, 9 } };
  9587. i32 (3), i32 (3), i32 (5);
  9588. for (const auto& p : pts)
  9589. {
  9590. f64 (p.dt), f64 (p.y), f32 (p.t);
  9591. b.push_back ((std::uint8_t) p.mode), b.push_back (0), b.push_back (0), b.push_back (0);
  9592. }
  9593. i32 (0), i32 (-1), i32 (-1), i32 (-1), i32 (-1);
  9594. i32 (1);
  9595. b.push_back (hq ? 1 : 0), b.push_back (smooth ? 1 : 0);
  9596. return b;
  9597. }
  9598. } // namespace
  9599. TEST (fruity_waveshaper_and_soft_clipper)
  9600. {
  9601. std::vector<float> inL (4096), inR (4096);
  9602. for (int n = 0; n < 4096; ++n)
  9603. {
  9604. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  9605. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  9606. }
  9607. struct Gold { int i; float l, r; };
  9608. auto close = [] (float a, float b) { return std::abs (a - b) <= 2.0e-7f * std::max (1.0f, std::abs (b)); };
  9609. // Soft Clipper: bit-exact against Fruity Soft Clipper_x64.dll
  9610. auto clip = [&] (int thr, int post, std::initializer_list<Gold> gold) {
  9611. std::vector<float> l (inL), r (inR);
  9612. for (int s = 0; s < 4096; s += 512)
  9613. {
  9614. lynx::flshape::softClip (l.data() + s, 512, thr, post);
  9615. lynx::flshape::softClip (r.data() + s, 512, thr, post);
  9616. }
  9617. for (const auto& g : gold)
  9618. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  9619. };
  9620. clip (100, 128, { { 100, 0.0f, -0.949052989f }, { 1000, -0.99617976f, 0.872887492f }, { 2047, 0.649999976f, -0.944175363f },
  9621. { 3000, -0.99617976f, -0.699999988f }, { 4095, 0.25f, 0.600000024f } });
  9622. clip (1, 150, { { 100, 0.0f, -1.04588532f }, { 1000, -1.27003884f, 0.930835366f }, { 2047, 0.750244498f, -1.03539562f },
  9623. { 3000, -1.27003884f, -0.790091455f }, { 4095, 0.347655922f, 0.708338201f } });
  9624. clip (64, 140, { { 100, 0.0f, -1.08492827f }, { 1000, -1.21535313f, 0.990323186f }, { 2047, 0.804165959f, -1.0770781f },
  9625. { 3000, -1.21535313f, -0.849189103f }, { 4095, 0.319320858f, 0.754407823f } });
  9626. {
  9627. const std::vector<std::uint8_t> st { 64, 0, 0, 0, 140, 0, 0, 0 };
  9628. const auto slot = lynx::effectFromFl ("Fruity Soft Clipper", st, 120.0);
  9629. CHECK (slot && slot->kind == lynx::EffectKind::fruitySoftClipper);
  9630. if (slot)
  9631. {
  9632. const auto s = lynx::flshape::softClipperFromParams (slot->params.data());
  9633. CHECK (s.threshold == 64 && s.post == 140);
  9634. auto fx = lynx::makeEffect (*slot);
  9635. fx->prepare (44100.0);
  9636. std::vector<float> l (inL), r (inR);
  9637. fx->process (l.data(), r.data(), 4096, slot->params, 120.0);
  9638. CHECK (l[3000] == -1.21535313f && r[4095] == 0.754407823f);
  9639. }
  9640. }
  9641. // WaveShaper: tables (bipolar / unipolar) and the engine on three of our states, live and as FL renders
  9642. const auto stA = waveShaperTestState (150, 100, 120, false, true, 1, true, true); // CENTER, ×2, HQ, smooth
  9643. const auto stB = waveShaperTestState (100, 128, 128, true, false, 0, false, false); // unipolar
  9644. const auto stC = waveShaperTestState (192, 90, 100, false, false, 2, false, false); // ×4
  9645. const auto pA = lynx::flshape::parseWaveShaper (stA);
  9646. CHECK (pA && pA->points.size() == 5 && pA->pre == 150 && pA->center && pA->hq && pA->smooth && pA->oversampling == 1);
  9647. if (pA)
  9648. {
  9649. const auto T = lynx::flshape::shaperTable (pA->points, false);
  9650. CHECK (T.size() == 16386 && T[1] == 3277 && T[1001] == 18824 && T[5001] == 26195 && T[9001] == 11469 && T[12001] == 24614
  9651. && T[16384] == 32767 && T[0] == T[1]);
  9652. const auto U = lynx::flshape::shaperTable (pA->points, true);
  9653. CHECK (U.size() == 32770 && U[1] == -26214 && U[1001] == -6408 && U[5001] == 17073 && U[9001] == 19551 && U[12001] == 14549);
  9654. }
  9655. auto shape = [&] (const std::vector<std::uint8_t>& st, bool render, std::initializer_list<Gold> gold) {
  9656. const auto s = lynx::flshape::parseWaveShaper (st);
  9657. CHECK (s.has_value());
  9658. if (! s)
  9659. return;
  9660. lynx::flshape::WaveShaperEngine e;
  9661. e.setCurve (s->points);
  9662. e.prepare (44100.0);
  9663. e.setControls (s->pre, s->mix, s->post, s->oversampling, s->hq, s->smooth, s->center, s->unipolar, render);
  9664. std::vector<float> l (inL), r (inR);
  9665. for (int i = 0; i < 4096; i += 512)
  9666. e.process (l.data() + i, r.data() + i, 512);
  9667. for (const auto& g : gold)
  9668. CHECK (close (l[(std::size_t) g.i], g.l) && close (r[(std::size_t) g.i], g.r));
  9669. };
  9670. shape (stA, false, { { 100, 0.59108448f, 0.469315082f }, { 1000, 0.284353405f, 1.02621937f }, { 2047, -0.533613563f, 0.117038473f },
  9671. { 3000, 0.22673659f, -1.13572407f }, { 4095, -0.267153352f, 0.822866499f } });
  9672. shape (stA, true, { { 100, 0.794869065f, -0.127601087f }, { 1000, -0.00841622613f, 0.900729597f }, { 2047, 0.450603426f, -0.325943232f },
  9673. { 3000, -0.0530530401f, -0.32303831f }, { 4095, 1.0959363f, 0.946009099f } });
  9674. shape (stB, false, { { 100, -0.206451416f, 0.502471924f }, { 1000, -0.799987793f, 0.761169434f }, { 2047, 0.411743164f, 0.514160156f },
  9675. { 3000, -0.799987793f, 0.596221924f }, { 4095, -0.157653809f, 0.334106445f } });
  9676. shape (stB, true, { { 100, -0.206390381f, 0.502493739f }, { 1000, -0.799987793f, 0.761169434f }, { 2047, 0.411743164f, 0.514144361f },
  9677. { 3000, -0.799987793f, 0.596221924f }, { 4095, -0.157653809f, 0.334106445f } });
  9678. shape (stC, false, { { 100, 0.317797363f, 0.0483714938f }, { 1000, -0.445489585f, -0.533142388f }, { 2047, 0.634525597f, 0.942175806f },
  9679. { 3000, -0.445489585f, 0.514707327f }, { 4095, 0.200391173f, -0.937523127f } });
  9680. shape (stC, true, { { 100, 0.61165309f, -0.0993360579f }, { 1000, 0.0375693105f, 0.667849183f }, { 2047, 0.353716731f, -0.223758236f },
  9681. { 3000, 0.0375693105f, -0.223787636f }, { 4095, 0.843308687f, 0.688474894f } });
  9682. // the mixer effect from the FL import: controls as parameters, the curve in the slot's state; host blocks of any size
  9683. const auto slot = lynx::effectFromFl ("Fruity WaveShaper", stC, 120.0);
  9684. CHECK (slot && slot->kind == lynx::EffectKind::fruityWaveShaper && slot->state == stC);
  9685. if (slot)
  9686. {
  9687. auto fx = lynx::makeEffect (*slot);
  9688. fx->prepare (44100.0);
  9689. std::vector<float> l (inL), r (inR);
  9690. fx->process (l.data(), r.data(), 1024, slot->params, 120.0);
  9691. fx->process (l.data() + 1024, r.data() + 1024, 1024, slot->params, 120.0);
  9692. fx->process (l.data() + 2048, r.data() + 2048, 2048, slot->params, 120.0);
  9693. CHECK (close (l[1000], -0.445489585f) && close (r[2047], 0.942175806f) && close (l[4095], 0.200391173f));
  9694. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityWaveShaper, 3, slot->params[3], 120.0) == "x4");
  9695. }
  9696. // an unreadable state keeps the old approximation
  9697. CHECK (lynx::effectFromFl ("Fruity WaveShaper", { 1, 2, 3 }, 120.0)->kind == lynx::EffectKind::distortion);
  9698. CHECK (lynx::effectFromFl ("Fruity Soft Clipper", { 1, 2, 3 }, 120.0)->kind == lynx::EffectKind::distortion);
  9699. }
  9700. // ---- Fruity Parametric EQ 2 / Fruity Parametric EQ (REA_rodada50) -----------------------------------------------------
  9701. // States written here from our own values (no FL preset data); goldens from the installation's DLLs (EXEC, blocks of 512)
  9702. namespace
  9703. {
  9704. struct PeqTestBand { int type, freq, width, gain, order; };
  9705. std::vector<std::uint8_t> peq2TestState (const PeqTestBand (&bands)[7], int main, int uiFlags, int options, int mask)
  9706. {
  9707. std::vector<std::uint8_t> b;
  9708. 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))); };
  9709. i32 (8);
  9710. for (int copy = 0; copy < 2; ++copy)
  9711. {
  9712. for (const auto& x : bands) i32 (x.gain);
  9713. for (const auto& x : bands) i32 (x.freq);
  9714. for (const auto& x : bands) i32 (x.width);
  9715. for (const auto& x : bands) i32 (x.type);
  9716. for (const auto& x : bands) i32 (x.order);
  9717. i32 (main);
  9718. }
  9719. i32 (uiFlags), i32 (0), b.push_back (0), i32 (2), i32 (0), i32 (2), i32 (0);
  9720. b.push_back ((std::uint8_t) options);
  9721. i32 (mask), i32 (1), i32 (0);
  9722. for (int v : { 3, 1, 2, 2, 2 }) i32 (v);
  9723. i32 (0);
  9724. return b;
  9725. }
  9726. std::vector<std::uint8_t> peq1TestState (const PeqTestBand (&bands)[7], int main)
  9727. {
  9728. std::vector<std::uint8_t> b;
  9729. 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))); };
  9730. for (const auto& x : bands) i32 (x.gain);
  9731. for (const auto& x : bands) i32 (x.freq);
  9732. for (const auto& x : bands) i32 (x.width);
  9733. for (const auto& x : bands) i32 (x.type);
  9734. i32 (main);
  9735. return b;
  9736. }
  9737. } // namespace
  9738. // EQUO test state (our own LCG curves, no FL data; REA_rodada61 equo/teststate.py builds the same bytes)
  9739. static std::vector<std::uint8_t> equoTestState (int ovs, int bw, bool add)
  9740. {
  9741. std::vector<std::uint8_t> b;
  9742. auto put = [&b] (const void* p, std::size_t n) { b.insert (b.end(), (const std::uint8_t*) p, (const std::uint8_t*) p + n); };
  9743. auto putI = [&put] (std::int32_t v) { put (&v, 4); };
  9744. auto putF = [&put] (float v) { put (&v, 4); };
  9745. std::uint32_t seed = 12345;
  9746. auto rnd = [&seed] {
  9747. seed = (std::uint32_t) (((std::uint64_t) seed * 1103515245u + 12345u) & 0x7fffffffu);
  9748. return (float) ((double) (seed % 20001) / 10000.0);
  9749. };
  9750. putI (4);
  9751. for (int k = 0; k < 93; ++k)
  9752. {
  9753. putF (rnd());
  9754. putI (k % 11 == 0 ? 1 : (k % 13 == 0 ? 2 : 0));
  9755. }
  9756. for (int bank = 0; bank < 9; ++bank)
  9757. for (int k = 0; k < 93; ++k)
  9758. {
  9759. const float v = rnd();
  9760. putF (bank == 8 ? 0.75f + v * 0.25f : v);
  9761. }
  9762. for (int v : { 1500, 90, 2600, 170, 150, ovs, bw, -1, 0, 0 })
  9763. putI (v);
  9764. for (int v : { 0, add ? 1 : 0, 0 })
  9765. b.push_back ((std::uint8_t) v);
  9766. putI (1);
  9767. return b;
  9768. }
  9769. // Pitch Shifter state (version 7, our own settings; REA_rodada61 ps/teststate.py builds the same bytes)
  9770. static std::vector<std::uint8_t> pitchShifterTestState (std::array<int, 13> p, int engine, int detector)
  9771. {
  9772. std::vector<std::uint8_t> b;
  9773. auto putI = [&b] (std::int32_t v) { const auto* q = (const std::uint8_t*) &v; b.insert (b.end(), q, q + 4); };
  9774. putI (7);
  9775. for (int v : p)
  9776. putI (v);
  9777. putI (p[11]);
  9778. b.push_back (0);
  9779. putI (engine);
  9780. putI (detector);
  9781. putI (1000);
  9782. b.push_back (1);
  9783. b.push_back (0);
  9784. return b;
  9785. }
  9786. TEST (pitch_shifter_exact)
  9787. {
  9788. std::vector<float> inL (8192), inR (8192);
  9789. for (int n = 0; n < 8192; ++n)
  9790. {
  9791. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0) * 0.3f;
  9792. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0) * 0.3f;
  9793. }
  9794. struct Gold { int i; float l, r; };
  9795. // goldens: Pitch Shifter_x64.dll on the same states and signal (REA_rodada61)
  9796. auto run = [&] (const std::vector<std::uint8_t>& st, int latency, std::initializer_list<Gold> gold) {
  9797. lynx::psfl::State s;
  9798. CHECK (st.size() == 75 && lynx::psfl::parsePitchShifter (st.data(), st.size(), s));
  9799. // through the mixer effect: FL import → slot parameters → effect, odd host blocks
  9800. const auto slot = lynx::effectFromFl ("Pitch Shifter", st, 120.0);
  9801. CHECK (slot && slot->kind == lynx::EffectKind::flPitchShifter);
  9802. if (! slot)
  9803. return;
  9804. auto fx = lynx::makeEffect (*slot);
  9805. fx->prepare (44100.0);
  9806. std::vector<float> l (inL), r (inR);
  9807. for (int i = 0, k = 0; i < 8192; ++k)
  9808. {
  9809. const int n = std::min (8192 - i, k % 2 == 0 ? 700 : 333);
  9810. fx->process (l.data() + i, r.data() + i, n, slot->params, 120.0);
  9811. i += n;
  9812. }
  9813. CHECK (fx->latencySamples() == latency);
  9814. for (const auto& g : gold)
  9815. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  9816. };
  9817. // "FFT" (pitch-synchronous) engine: +5 st, formant −3 st, correction 50 %, mix 79 %, accurate detector
  9818. run (pitchShifterTestState ({ 500, -300, 2, 1900, 8500, 200, 0, 0, 768, 0, 0, 32768, 0 }, 0, 1), 2457,
  9819. { { 1000, 0.0f, 0.0f }, { 3000, 0.142123833f, 0.0878119618f }, { 5000, 0.139498621f, -0.365035176f },
  9820. { 7000, 0.139498621f, 0.161137447f }, { 8191, 0.449655473f, 0.453882188f } });
  9821. // granular engine: −7 st, 60 ms grains, density 300 %, jitter 30 %, random 20 %, post delay with feedback 40 %
  9822. run (pitchShifterTestState ({ -700, 0, 2, 2400, 6000, 300, 30, 20, 200, 40, 1, 65536, 0 }, 1, 0), 0,
  9823. { { 1000, -0.0736689344f, -0.349420458f }, { 3000, 0.086801365f, 0.0471927896f }, { 5000, -0.0939825475f, 0.31712544f },
  9824. { 7000, -0.28462702f, -0.437854528f }, { 8191, 0.138916701f, -0.0426892526f } });
  9825. CHECK (lynx::formatEffectParam (lynx::EffectKind::flPitchShifter, 0, 0.75f, 120.0) == "+6.00 st");
  9826. }
  9827. TEST (equo_exact)
  9828. {
  9829. std::vector<float> inL (4096), inR (4096);
  9830. for (int n = 0; n < 4096; ++n)
  9831. {
  9832. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  9833. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  9834. }
  9835. struct Gold { int i; float l, r; };
  9836. // goldens: EQUO_x64.dll run on the same states and signal (REA_rodada61 §5)
  9837. auto run = [&] (const std::vector<std::uint8_t>& st, bool hq, bool ticks, std::initializer_list<Gold> gold) {
  9838. const auto s = lynx::equofx::parse (st);
  9839. CHECK (s.has_value());
  9840. if (! s)
  9841. return;
  9842. lynx::equofx::Engine e;
  9843. e.prepare (44100.0, hq);
  9844. e.set (*s);
  9845. std::vector<float> l (inL), r (inR);
  9846. if (! ticks)
  9847. e.process (l.data(), r.data(), 4096);
  9848. else
  9849. {
  9850. auto k = *s;
  9851. e.startFromConstruction();
  9852. for (int i = 0; i < 4096; i += 256)
  9853. {
  9854. if (i == 2048)
  9855. k.knob[2] = 5300, e.setKnobs (k);
  9856. e.tick();
  9857. e.process (l.data() + i, r.data() + i, 256);
  9858. }
  9859. }
  9860. for (const auto& g : gold)
  9861. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  9862. };
  9863. const auto plain = equoTestState (0, 150, false);
  9864. run (plain, false, false, { { 100, -0.00154058414f, -0.00550224911f }, { 1000, -0.908149242f, 0.619277894f }, { 2047, 0.411961317f, -0.364051819f },
  9865. { 3000, -1.42823684f, -0.0602950938f }, { 4095, -0.0661701784f, 0.300809383f } });
  9866. run (equoTestState (1, 300, true), false, false,
  9867. { { 100, 0.0312003642f, 0.0184187777f }, { 1000, 0.0379521511f, 0.224126667f }, { 2047, -0.135689721f, 0.0444660783f },
  9868. { 3000, 0.113949843f, -1.00256324f }, { 4095, -0.176232055f, 0.79159987f } });
  9869. run (plain, true, false, { { 100, -0.00104017637f, 0.000872685807f }, { 1000, -0.0426855832f, 0.0402083024f }, { 2047, 0.191993102f, 0.174184501f },
  9870. { 3000, -0.114048228f, 0.419817507f }, { 4095, 0.0376945846f, -0.274040252f } });
  9871. run (plain, false, true, { { 100, -0.0019897793f, -0.00981196482f }, { 1000, -1.09574401f, 0.83203429f }, { 2047, 0.839113712f, -0.691361725f } });
  9872. // the mixer effect from the FL import: FL's state in the slot, the knobs in its parameters, any host block size
  9873. const auto slot = lynx::effectFromFl ("EQUO", plain, 140.0);
  9874. CHECK (slot && slot->kind == lynx::EffectKind::equo && slot->state == plain);
  9875. if (slot)
  9876. {
  9877. auto back = *lynx::equofx::parse (plain);
  9878. lynx::equofx::applyParams (slot->params, back);
  9879. CHECK (back.knob == lynx::equofx::parse (plain)->knob && back.bandwidth == 150 && back.oversample == 0);
  9880. auto fx = lynx::makeEffect (*slot);
  9881. fx->prepare (44100.0);
  9882. std::vector<float> l (inL), r (inR);
  9883. fx->process (l.data(), r.data(), 700, slot->params, 0.0);
  9884. fx->process (l.data() + 700, r.data() + 700, 3396, slot->params, 0.0);
  9885. // the engine glides its bands per processing call, as FL does per host buffer: compare with the engine run on
  9886. // the same 700 + 3396 split (sample 1000 differs from the single-call golden), and with the golden once settled
  9887. lynx::equofx::Engine ref;
  9888. ref.prepare (44100.0, false);
  9889. ref.set (*lynx::equofx::parse (plain));
  9890. std::vector<float> rl (inL), rr (inR);
  9891. ref.process (rl.data(), rr.data(), 700);
  9892. ref.process (rl.data() + 700, rr.data() + 700, 3396);
  9893. CHECK (l == rl && rr == r && r[3000] == -0.0602950938f);
  9894. CHECK (lynx::formatEffectParam (lynx::EffectKind::equo, 4, slot->params[4], 120.0, slot->params).find ("dB") != std::string::npos);
  9895. }
  9896. CHECK (! lynx::equofx::parse (std::vector<std::uint8_t> (100, 0)).has_value());
  9897. }
  9898. // Multiband Delay test state (version 7, our own LCG cells, no FL data; REA_rodada71 mbd/py/teststate.py builds the same bytes)
  9899. static std::vector<std::uint8_t> multibandDelayTestState (std::array<int, 12> globals, std::uint32_t seed)
  9900. {
  9901. std::vector<std::uint8_t> b;
  9902. auto putI = [&b] (std::int32_t v) { const auto* q = (const std::uint8_t*) &v; b.insert (b.end(), q, q + 4); };
  9903. putI (7);
  9904. putI (396);
  9905. std::uint64_t s = seed;
  9906. for (int i = 0; i < 384; ++i)
  9907. {
  9908. s = (s * 1103515245u + 12345u) & 0x7fffffffu;
  9909. putI ((std::int32_t) ((s % 100001u) * (1u << 30) / 100000u));
  9910. }
  9911. for (int v : globals)
  9912. putI (v);
  9913. putI (0);
  9914. putI (3);
  9915. for (int v : { 1, 1, 0 })
  9916. b.push_back ((std::uint8_t) v);
  9917. b.insert (b.end(), 192, 0);
  9918. return b;
  9919. }
  9920. TEST (multiband_delay_exact)
  9921. {
  9922. std::vector<float> inL (8192), inR (8192);
  9923. for (int n = 0; n < 8192; ++n)
  9924. {
  9925. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0 * 0.3);
  9926. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0 * 0.3);
  9927. }
  9928. struct Gold { int i; float l, r; };
  9929. // goldens: Multiband Delay_x64.dll (+ its engine_x64.dll) on the same states and signal, 512-frame blocks (REA_rodada71)
  9930. auto run = [&] (const std::vector<std::uint8_t>& st, int latency, float tol, std::initializer_list<Gold> gold) {
  9931. CHECK (st.size() == 1795 && lynx::mbdelay::parse (st).has_value());
  9932. const auto slot = lynx::effectFromFl ("Multiband Delay", st, 120.0);
  9933. CHECK (slot && slot->kind == lynx::EffectKind::multibandDelay && slot->state == st);
  9934. if (! slot)
  9935. return;
  9936. auto fx = lynx::makeEffect (*slot);
  9937. fx->prepare (44100.0);
  9938. std::vector<float> l (inL), r (inR);
  9939. for (int i = 0, k = 0; i < 8192; ++k)
  9940. {
  9941. const int n = std::min (8192 - i, k % 2 == 0 ? 700 : 333);
  9942. fx->process (l.data() + i, r.data() + i, n, slot->params, 120.0);
  9943. i += n;
  9944. }
  9945. CHECK (fx->latencySamples() == latency);
  9946. for (const auto& g : gold)
  9947. CHECK (std::fabs (l[(std::size_t) g.i] - g.l) <= tol && std::fabs (r[(std::size_t) g.i] - g.r) <= tol);
  9948. };
  9949. // steep crossover, keep pitch, weighted feedback, compressor
  9950. run (multibandDelayTestState ({ 2000, 0, 3000, 600, 1, 1, 3500, 64, 0, 1, 30000, 0 }, 777), 0, 0.0f,
  9951. { { 1000, -0.125012308f, 0.0675066486f }, { 3000, -0.125002861f, -0.0525011979f }, { 5000, -0.110439189f, 0.0597563162f },
  9952. { 7000, -0.146480903f, 0.0139919929f }, { 8191, -0.0454496294f, 0.0398242511f } });
  9953. // gentle crossover, harmonic scale, delay ×0.1, saturation
  9954. run (multibandDelayTestState ({ -2500, 1, 4500, 0, 0, 0, 0, 128, 1, 0, 1000, 1 }, 4242), 0, 0.0f,
  9955. { { 1000, 0.0679653957f, 0.0137896547f }, { 3000, 0.0620588325f, 0.0558352768f }, { 5000, 0.0558495969f, -0.0220566839f },
  9956. { 7000, 0.057009533f, 0.00332171144f }, { 8191, 0.0673644915f, 0.00163267844f } });
  9957. // linear-phase crossover (FL's IPP FFTs are approximated: about −120 dB), clipper
  9958. run (multibandDelayTestState ({ 1500, 0, 2000, 1500, 2, 0, 1000, 100, 0, 0, 5000, 2 }, 99), 1536, 2e-6f,
  9959. { { 1000, 0.0f, 0.0f }, { 3000, 0.103910387f, -0.0521105044f }, { 5000, 0.0516818762f, -0.0134486742f },
  9960. { 7000, 0.065563485f, -0.00765838847f }, { 8191, -0.0376662463f, -0.0434602089f } });
  9961. const auto st = multibandDelayTestState ({ 2000, 0, 3000, 600, 1, 1, 3500, 64, 0, 1, 30000, 0 }, 777);
  9962. const auto s = *lynx::mbdelay::parse (st);
  9963. CHECK (lynx::mbdelay::parse (lynx::mbdelay::serialize (s))->raw == s.raw);
  9964. lynx::EffectParams p {};
  9965. lynx::mbdelay::toParams (s, p);
  9966. auto back = s;
  9967. lynx::mbdelay::applyParams (p, back);
  9968. CHECK (back.raw == s.raw);
  9969. CHECK (lynx::mbdelay::engineValue (s, 384 + 7) == (float) (192.0 / 256.0));
  9970. CHECK (lynx::formatEffectParam (lynx::EffectKind::multibandDelay, 4, p[4], 120.0) == "steep");
  9971. CHECK (lynx::formatEffectParam (lynx::EffectKind::multibandDelay, 7, 1.0f, 120.0) == "100% wet");
  9972. CHECK (! lynx::mbdelay::parse (std::vector<std::uint8_t> (16, 0)).has_value());
  9973. }
  9974. TEST (fruity_parametric_eq_exact)
  9975. {
  9976. std::vector<float> inL (4096), inR (4096);
  9977. for (int n = 0; n < 4096; ++n)
  9978. {
  9979. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  9980. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  9981. }
  9982. struct Gold { int i; float l, r; };
  9983. // high pass Steep 8, low shelf, steep peaking, a 0 dB peaking (0.2 s countdown), notch Gentle 4, high shelf Gentle 8,
  9984. // low pass Gentle 6
  9985. const PeqTestBand bands[7] = { { 3, 8000, 30000, 0, -3 }, { 5, 15000, 40124, 600, 0 }, { 6, 30000, 20000, -900, -2 },
  9986. { 6, 35000, 25412, 0, 0 }, { 4, 40000, 10000, 0, 1 }, { 7, 50000, 30000, 450, 3 },
  9987. { 1, 60000, 45000, 0, 2 } };
  9988. const PeqTestBand bands1[7] = { { 3, 6000, 20000, 0, 0 }, { 5, 15000, 30000, 600, 0 }, { 6, 30000, 20000, -900, 0 },
  9989. { 6, 35000, 25000, 0, 0 }, { 4, 40000, 10000, 0, 0 }, { 7, 50000, 30000, 450, 0 },
  9990. { 1, 60000, 45000, 0, 0 } };
  9991. auto run = [&] (const std::optional<lynx::peqfl::Settings>& s, std::initializer_list<Gold> gold) {
  9992. CHECK (s.has_value());
  9993. if (! s)
  9994. return;
  9995. lynx::peqfl::Engine e;
  9996. e.set (*s);
  9997. e.prepare (44100.0);
  9998. std::vector<float> l (inL), r (inR);
  9999. for (int i = 0; i < 4096; i += 512)
  10000. e.process (l.data() + i, r.data() + i, 512);
  10001. for (const auto& g : gold)
  10002. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  10003. };
  10004. const auto plain = peq2TestState (bands, 300, 0, 0x60, 0x7F);
  10005. const auto hq = peq2TestState (bands, 300, 1, 0x60, 0x7F);
  10006. const auto ext = peq2TestState (bands, 300, 1, 0x62, 0x7F);
  10007. run (lynx::peqfl::parsePeq2 (plain), { { 100, 0.1340574324131012f, -0.3040016293525696f }, { 1000, 0.28402701020240784f, -0.23813000321388245f },
  10008. { 2047, -0.9301530122756958f, 0.13881002366542816f }, { 3000, 0.2846762537956238f, 0.48893973231315613f },
  10009. { 4095, -0.8481860756874084f, -0.2641225755214691f } });
  10010. run (lynx::peqfl::parsePeq2 (hq), { { 100, 0.19229719042778015f, 0.29938483238220215f }, { 1000, 0.12543821334838867f, -0.055617764592170715f },
  10011. { 2047, 1.0342068672180176f, 0.10901166498661041f }, { 3000, 0.12598788738250732f, -0.12920552492141724f },
  10012. { 4095, 0.1619236022233963f, -0.23385579884052277f } });
  10013. run (lynx::peqfl::parsePeq2 (ext), { { 100, 1.2178642749786377f, -0.21160493791103363f }, { 1000, -0.8292768001556396f, -0.23916664719581604f },
  10014. { 2047, -0.7030635476112366f, -0.1978478729724884f }, { 3000, -0.8299900889396667f, 0.9836107492446899f },
  10015. { 4095, 0.06754066050052643f, -0.34364908933639526f } });
  10016. run (lynx::peqfl::parsePeq2 (peq2TestState (bands, -250, 1, 0x60, 0x5B)), // bands 3 and 6 muted
  10017. { { 100, -0.02959616854786873f, 0.227821946144104f }, { 1000, 0.053224340081214905f, 0.06922465562820435f },
  10018. { 2047, 0.3365536034107208f, -0.05899651721119881f }, { 3000, 0.053670305758714676f, -0.07452142238616943f },
  10019. { 4095, -0.04406970739364624f, -0.12635089457035065f } });
  10020. run (lynx::peqfl::parsePeq1 (peq1TestState (bands1, 150)),
  10021. { { 100, -0.6533108353614807f, -1.6903921365737915f }, { 1000, -4.313726902008057f, 5.973916053771973f },
  10022. { 2047, 1.0222549438476562f, 4.427900314331055f }, { 3000, -4.310323238372803f, 7.825916767120361f },
  10023. { 4095, 8.344571113586426f, -0.49086111783981323f } });
  10024. // "alternative design" (Frequency Splitter's matched biquads), bit-exact
  10025. run (lynx::peqfl::parsePeq2 (peq2TestState (bands, 300, 3, 0x60, 0x7F)),
  10026. { { 100, -0.790181577205658f, -0.08691816031932831f }, { 1000, 0.4960691034793854f, -0.24172282218933105f },
  10027. { 2047, -0.42601829767227173f, 0.027932533994317055f }, { 3000, 0.49674150347709656f, 1.0799059867858887f },
  10028. { 4095, -0.35040369629859924f, -0.28149813413619995f } });
  10029. // linear phase (plain and HQ response): FFT convolution, 1536-sample latency, 0.1 s fade-in; FL's IPP FFT rounding
  10030. // is not replicated, so within 1e-6 of the DLL
  10031. for (const bool hq : { false, true })
  10032. {
  10033. const auto s = lynx::peqfl::parsePeq2 (peq2TestState (bands, hq ? -200 : 300, hq ? 1 : 0, 0x61, 0x7F));
  10034. CHECK (s && s->linearPhase);
  10035. if (! s)
  10036. continue;
  10037. std::vector<float> l (12288), r (12288);
  10038. for (int n = 0; n < 12288; ++n)
  10039. {
  10040. l[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  10041. r[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  10042. }
  10043. lynx::peqfl::Engine e;
  10044. e.set (*s);
  10045. e.prepare (44100.0);
  10046. CHECK (e.latencySamples() == 1536);
  10047. for (int i = 0; i < 12288; i += 512)
  10048. e.process (l.data() + i, r.data() + i, 512);
  10049. const std::initializer_list<Gold> gold = hq ? std::initializer_list<Gold> {
  10050. { 2000, 0.08618364483118057f, 0.11248858273029327f }, { 5000, 0.18998730182647705f, 0.428600937128067f },
  10051. { 8191, -0.1412065178155899f, -0.002996620489284396f }, { 10000, 0.18998737633228302f, -0.012570115737617016f },
  10052. { 12287, 0.40586185455322266f, -0.014698396436870098f } } : std::initializer_list<Gold> {
  10053. { 2000, 0.18538671731948853f, 0.206522598862648f }, { 5000, 0.4086928963661194f, 0.8167467713356018f },
  10054. { 8191, -0.3218550980091095f, -0.06812914460897446f }, { 10000, 0.40869277715682983f, -0.08537150919437408f },
  10055. { 12287, 0.775394082069397f, -0.08931504935026169f } };
  10056. for (const auto& g : gold)
  10057. CHECK (std::abs (l[(std::size_t) g.i] - g.l) < 1.0e-6f && std::abs (r[(std::size_t) g.i] - g.r) < 1.0e-6f);
  10058. }
  10059. // the mixer effect from the FL import: FL's values in the slot's parameters (exact round trip), any host block size
  10060. for (const auto* name : { "Fruity Parametric EQ 2", "Fruity Parametric EQ" })
  10061. {
  10062. const bool v1 = std::string (name) == "Fruity Parametric EQ";
  10063. const auto st = v1 ? peq1TestState (bands1, 150) : ext;
  10064. const auto slot = lynx::effectFromFl (name, st, 120.0);
  10065. CHECK (slot && slot->kind == lynx::EffectKind::parametricEq && lynx::peq::flLaw (slot->params));
  10066. if (! slot)
  10067. continue;
  10068. const auto back = lynx::peqfl::fromParams (slot->params);
  10069. const auto orig = v1 ? lynx::peqfl::parsePeq1 (st) : lynx::peqfl::parsePeq2 (st);
  10070. CHECK (back.freq == orig->freq && back.gain == orig->gain && back.width == orig->width && back.type == orig->type
  10071. && back.order == orig->order && back.main == orig->main && back.processing == orig->processing && back.mask == orig->mask);
  10072. auto fx = lynx::makeEffect (*slot);
  10073. fx->prepare (44100.0);
  10074. CHECK (fx->latencySamples() == (v1 ? 0 : 44));
  10075. std::vector<float> l (inL), r (inR);
  10076. fx->process (l.data(), r.data(), 1000, slot->params, 120.0);
  10077. fx->process (l.data() + 1000, r.data() + 1000, 3096, slot->params, 120.0);
  10078. if (v1)
  10079. CHECK (l[3000] == -4.310323238372803f && r[4095] == -0.49086111783981323f);
  10080. else
  10081. CHECK (l[1000] == -0.8292768001556396f && r[3000] == 0.9836107492446899f && l[4095] == 0.06754066050052643f);
  10082. // editor texts and graph
  10083. CHECK (lynx::formatEffectParam (lynx::EffectKind::parametricEq, lynx::peq::slopeParam, slot->params[lynx::peq::slopeParam], 120.0, slot->params)
  10084. == (v1 ? "12 dB/oct" : "Steep 8"));
  10085. CHECK (lynx::peq::widthText (slot->params, 0).rfind ("BW ", 0) == 0);
  10086. const auto lowCutDb = lynx::peq::responseDb (slot->params, 44100.0, 25.0), midDb = lynx::peq::responseDb (slot->params, 44100.0, 1000.0);
  10087. CHECK (lowCutDb < midDb - 5.0); // high pass below ~100 Hz (Steep 8 in the EQ 2 state, 12 dB/oct in v1)
  10088. }
  10089. // a native LYNX EQ is untouched by the FL law
  10090. CHECK (! lynx::peq::flLaw (lynx::makeEffectSlot (lynx::EffectKind::parametricEq).params));
  10091. }
  10092. TEST (fruity_panomatic_balance_blood_overdrive)
  10093. {
  10094. // REA_rodada58: goldens from the installation's DLLs (EXEC) on our own states, 44.1 kHz, blocks of 512
  10095. const lynx::ScopedFlushDenormals ftz;
  10096. std::vector<float> inL (4096), inR (4096);
  10097. for (int n = 0; n < 4096; ++n)
  10098. {
  10099. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  10100. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  10101. }
  10102. struct Gold { int i; float l, r; };
  10103. auto close = [] (float a, float b) { return std::abs (a - b) <= 2.0e-7f * std::max (1.0f, std::abs (b)); };
  10104. auto check = [&] (const std::vector<float>& l, const std::vector<float>& r, std::initializer_list<Gold> gold) {
  10105. for (const auto& g : gold)
  10106. CHECK (close (l[(std::size_t) g.i], g.l) && close (r[(std::size_t) g.i], g.r));
  10107. };
  10108. auto st32 = [] (std::initializer_list<int> v) {
  10109. std::vector<std::uint8_t> b;
  10110. for (int x : v)
  10111. for (int k = 0; k < 4; ++k)
  10112. b.push_back ((std::uint8_t) ((unsigned) x >> (8 * k)));
  10113. return b;
  10114. };
  10115. // PanOMatic: the engine started as LYNX starts it (one silent frame, then FL's flush), and from silence (fade-in)
  10116. auto pom = [&] (lynx::flbasic::PanOMaticState s, bool prime, std::initializer_list<Gold> gold) {
  10117. lynx::flbasic::PanOMaticEngine e;
  10118. e.prepare (44100);
  10119. e.set (s);
  10120. if (prime)
  10121. e.prime();
  10122. std::vector<float> l (inL), r (inR);
  10123. for (int i = 0; i < 4096; i += 512)
  10124. e.process (l.data() + i, r.data() + i, 512);
  10125. check (l, r, gold);
  10126. };
  10127. pom ({ 40, 300, 2, 1, 128, 50000 }, true, { { 100, 0.0f, -1.44884729f }, { 1000, -1.78870034f, 0.778897762f }, { 2047, 0.697593153f, -1.26396084f },
  10128. { 3000, -1.78870034f, -1.32851648f }, { 4095, 0.268305063f, 0.851258814f } });
  10129. pom ({ 200, 180, 1, 2, 100, 30000 }, true, { { 100, 0.0f, -0.347919852f }, { 1000, -0.322590947f, 0.0788071603f }, { 2047, 0.126614109f, -0.26421836f },
  10130. { 3000, -0.326532423f, -0.435213327f }, { 4095, 0.0493027233f, 0.226084977f } });
  10131. pom ({ 90, 256, 0, 1, 77, 40000 }, false, { { 100, -0.0292895138f, -0.110000029f }, { 1000, -1.31911683f, 0.666457176f }, { 2047, 0.477211952f, -0.864247262f },
  10132. { 3000, -1.59167564f, -0.591367304f }, { 4095, 0.268362761f, 0.537029684f } });
  10133. // Balance: flushed and from silence
  10134. auto bal = [&] (lynx::flbasic::BalanceState s, bool flush, std::initializer_list<Gold> gold) {
  10135. lynx::flbasic::BalanceEngine e;
  10136. e.prepare (44100);
  10137. e.set (s);
  10138. if (flush)
  10139. e.flush();
  10140. std::vector<float> l (inL), r (inR);
  10141. for (int i = 0; i < 4096; i += 512)
  10142. e.process (l.data() + i, r.data() + i, 512);
  10143. check (l, r, gold);
  10144. };
  10145. bal ({ 77, 100 }, true, { { 100, 0.0f, -0.136579692f }, { 1000, -0.0669578835f, -0.0282340273f }, { 2047, 0.0261135735f, -0.0795038193f },
  10146. { 3000, -0.0669578835f, -0.226895392f }, { 4095, 0.0100436825f, 0.0954951793f } });
  10147. bal ({ -128, 320 }, false, { { 100, -0.28914085f, 0.0f }, { 1000, -1.03179514f, 0.0f }, { 2047, -0.578702569f, 0.0f },
  10148. { 3000, -3.18510628f, 0.0f }, { 4095, 1.14394665f, 0.0f } });
  10149. // Blood Overdrive: rate first, then the state (its smoothers settle for one second of samples)
  10150. auto od = [&] (std::array<int, 6> raw, std::initializer_list<Gold> gold) {
  10151. lynx::flbasic::BloodOverdriveEngine e;
  10152. e.start (44100, { raw });
  10153. std::vector<float> l (inL), r (inR);
  10154. for (int i = 0; i < 4096; i += 512)
  10155. e.process (l.data() + i, r.data() + i, 512);
  10156. check (l, r, gold);
  10157. };
  10158. od ({ 3000, 7000, 5000, 1, 9000, 5000 }, { { 100, -0.0442250744f, 0.170355186f }, { 1000, 0.0470311977f, 0.0765217245f },
  10159. { 2047, 0.0261161141f, -0.0341798998f }, { 3000, 0.0470311977f, 0.0213619489f },
  10160. { 4095, 0.0402069278f, 0.0499244779f } });
  10161. // the mixer effects from the FL import: controls as parameters; host blocks of any size
  10162. auto viaImport = [&] (const char* plugin, const std::vector<std::uint8_t>& st, lynx::EffectKind kind, std::initializer_list<Gold> gold) {
  10163. const auto slot = lynx::effectFromFl (plugin, st, 120.0);
  10164. CHECK (slot && slot->kind == kind);
  10165. if (! slot)
  10166. return;
  10167. auto fx = lynx::makeEffect (*slot);
  10168. fx->prepare (44100.0);
  10169. std::vector<float> l (inL), r (inR);
  10170. fx->process (l.data(), r.data(), 1024, slot->params, 120.0);
  10171. fx->process (l.data() + 1024, r.data() + 1024, 1024, slot->params, 120.0);
  10172. fx->process (l.data() + 2048, r.data() + 2048, 2048, slot->params, 120.0);
  10173. check (l, r, gold);
  10174. };
  10175. viaImport ("Fruity PanOMatic", st32 ({ 40, 300, 2, 1, 128, 50000 }), lynx::EffectKind::fruityPanOMatic,
  10176. { { 1000, -1.78870034f, 0.778897762f }, { 4095, 0.268305063f, 0.851258814f } });
  10177. viaImport ("Fruity Balance", st32 ({ 0, 256 }), lynx::EffectKind::fruityBalance,
  10178. { { 100, 0.0f, -1.10000002f }, { 1000, -1.66666663f, 0.899999976f }, { 4095, 0.25f, 0.600000024f } });
  10179. viaImport ("Fruity Blood Overdrive", st32 ({ 3, 0, 5000, 2578, 1, 0, 2911, 0, 0 }), lynx::EffectKind::fruityBloodOverdrive,
  10180. { { 100, -0.122698173f, 0.0206716377f }, { 2047, 0.0128381588f, -0.0274834726f }, { 4095, -0.0899387598f, -0.121109024f } });
  10181. {
  10182. const auto slot = lynx::effectFromFl ("Fruity PanOMatic", st32 ({ 119, 256, 1, 2, 64, 27500 }), 120.0);
  10183. CHECK (slot && lynx::flbasic::panOMaticFromParams (slot->params.data()).pan == 119
  10184. && lynx::flbasic::panOMaticFromParams (slot->params.data()).target == 2);
  10185. if (slot)
  10186. {
  10187. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityPanOMatic, 2, slot->params[2], 120.0) == "Triangle");
  10188. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityPanOMatic, 1, slot->params[1], 120.0) == "+0.0 dB");
  10189. }
  10190. }
  10191. // unreadable states keep the old approximations
  10192. CHECK (lynx::effectFromFl ("Fruity PanOMatic", { 1, 2, 3 }, 120.0)->kind == lynx::EffectKind::utility);
  10193. CHECK (lynx::effectFromFl ("Fruity Balance", { 1, 2, 3 }, 120.0)->kind == lynx::EffectKind::utility);
  10194. CHECK (lynx::effectFromFl ("Fruity Blood Overdrive", { 1, 2, 3 }, 120.0)->kind == lynx::EffectKind::distortion);
  10195. }
  10196. TEST (fruity_fast_lp_free_filter_filter)
  10197. {
  10198. // REA_rodada63: goldens from the installation's DLLs (EXEC) on our own states, 44.1 kHz, blocks of 512 (the wrapper
  10199. // splits the host blocks below into calls of 512, as the DLL ran)
  10200. const lynx::ScopedFlushDenormals ftz;
  10201. std::vector<float> inL (4096), inR (4096);
  10202. for (int n = 0; n < 4096; ++n)
  10203. {
  10204. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  10205. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  10206. }
  10207. struct Gold { int i; float l, r; };
  10208. auto state = [] (const char* hex) {
  10209. std::vector<std::uint8_t> b;
  10210. for (const char* p = hex; p[0] && p[1]; p += 2)
  10211. b.push_back ((std::uint8_t) std::strtol (std::string (p, 2).c_str(), nullptr, 16));
  10212. return b;
  10213. };
  10214. auto viaImport = [&] (const char* plugin, const char* hex, lynx::EffectKind kind, std::initializer_list<Gold> gold) {
  10215. const auto slot = lynx::effectFromFl (plugin, state (hex), 120.0);
  10216. CHECK (slot && slot->kind == kind);
  10217. if (! slot)
  10218. return;
  10219. auto fx = lynx::makeEffect (*slot);
  10220. fx->prepare (44100.0);
  10221. std::vector<float> l (inL), r (inR);
  10222. fx->process (l.data(), r.data(), 1024, slot->params, 120.0);
  10223. fx->process (l.data() + 1024, r.data() + 1024, 1024, slot->params, 120.0);
  10224. fx->process (l.data() + 2048, r.data() + 2048, 2048, slot->params, 120.0);
  10225. for (const auto& g : gold)
  10226. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  10227. };
  10228. using K = lynx::EffectKind;
  10229. viaImport ("Fruity Fast LP", "01000000e0010000bc020000", K::fruityFastLP,
  10230. { { 100, -2.876206159591675f, 1.5413906574249268f }, { 1000, 0.12972253561019897f, 0.02936629205942154f },
  10231. { 2047, -0.2262123078107834f, -0.15169914066791534f }, { 3000, 0.12972258031368256f, -0.0798468142747879f },
  10232. { 4095, 0.0011161785805597901f, -0.08101501315832138f } });
  10233. viaImport ("Fruity Fast LP", "01000000890000008a000000", K::fruityFastLP,
  10234. { { 100, -0.7748895883560181f, -0.2310182899236679f }, { 1000, -0.01256702933460474f, -0.0086913350969553f },
  10235. { 2047, -0.010225297883152962f, -0.014700239524245262f }, { 3000, -0.012712989933788776f, -0.008916046470403671f },
  10236. { 4095, -0.008918046951293945f, -0.020280027762055397f } });
  10237. viaImport ("Fruity Free Filter", "02000000c2010000200300002c0100008403000001", K::fruityFreeFilter, // low shelf, CENTER
  10238. { { 100, -0.6178444623947144f, -0.13762974739074707f }, { 1000, -1.6557703018188477f, 0.8969771862030029f },
  10239. { 2047, 0.6539966464042664f, -0.21659433841705322f }, { 3000, -1.6404345035552979f, 0.4286947548389435f },
  10240. { 4095, -1.2272151708602905f, -0.5219485759735107f } });
  10241. viaImport ("Fruity Free Filter", "020000008a02000058020000bc020000c800000000", K::fruityFreeFilter, // high shelf
  10242. { { 100, -0.06996861100196838f, -0.11112016439437866f }, { 1000, -0.47636136412620544f, 0.31258222460746765f },
  10243. { 2047, 0.1628853678703308f, -0.3169582486152649f }, { 3000, -0.47636136412620544f, -0.22028093039989471f },
  10244. { 4095, -0.0975298061966896f, 0.05710025131702423f } });
  10245. viaImport ("Fruity Free Filter", "020000006400000000040000a9000000a402000001", K::fruityFreeFilter, // low pass 16 kHz
  10246. { { 100, 1.3032524585723877f, -0.007953938096761703f }, { 1000, 0.18662987649440765f, 2.155099391937256f },
  10247. { 2047, 0.5824014544487f, 0.05704556778073311f }, { 3000, 0.18981599807739258f, -0.6362011432647705f },
  10248. { 4095, 1.2624475955963135f, 2.497816324234009f } });
  10249. viaImport ("Fruity Filter", "020000004b01000076000000000400002c010000580200000100000001", K::fruityFilter, // x2, CENTER
  10250. { { 100, 0.2685631811618805f, 0.16885951161384583f }, { 1000, -0.20637311041355133f, 0.13557639718055725f },
  10251. { 2047, 0.07038643211126328f, -0.15532609820365906f }, { 3000, -0.20311756432056427f, -0.09958365559577942f },
  10252. { 4095, 0.08275680989027023f, 0.010792487300932407f } });
  10253. viaImport ("Fruity Filter", "01000000bc020000f401000000000000000400000000000000000000", K::fruityFilter, // band pass
  10254. { { 100, -0.4368843734264374f, -0.12492841482162476f }, { 1000, -0.3362128436565399f, -0.09686881303787231f },
  10255. { 2047, 0.21920710802078247f, 0.3213832974433899f }, { 3000, -0.3362128436565399f, 0.4016203284263611f },
  10256. { 4095, -0.14230486750602722f, -0.22842440009117126f } });
  10257. // an old Free Filter layout (type·10) is converted to the current scale; parameter texts
  10258. {
  10259. const auto slot = lynx::effectFromFl ("Fruity Free Filter", state ("010000003c00000000020000a9000000a4020000"), 120.0);
  10260. CHECK (slot && slot->kind == K::fruityFreeFilter);
  10261. if (slot)
  10262. {
  10263. CHECK (lynx::flfilter::freeFilterFromParams (slot->params.data()).type == 100 + (int) std::nearbyint (5.5 / 7.0 * 600.0));
  10264. CHECK (lynx::formatEffectParam (K::fruityFreeFilter, 0, slot->params[0], 120.0) == "Peaking");
  10265. }
  10266. CHECK (lynx::formatEffectParam (K::fruityFreeFilter, 1, 1.0f, 120.0) == "16000 Hz");
  10267. CHECK (lynx::formatEffectParam (K::fruityFilter, 0, 1.0f, 120.0) == "7350 Hz");
  10268. }
  10269. // unreadable states keep the old approximations
  10270. CHECK (lynx::effectFromFl ("Fruity Fast LP", { 1, 2, 3 }, 120.0)->kind == K::filter);
  10271. CHECK (lynx::effectFromFl ("Fruity Free Filter", { 1, 2, 3 }, 120.0)->kind == K::filter);
  10272. CHECK (lynx::effectFromFl ("Fruity Filter", { 9, 0, 0, 0 }, 120.0)->kind == K::filter);
  10273. }
  10274. // ---- Sytrus (REA_rodada31) -------------------------------------------------------------------------------------------
  10275. // A Sytrus state built from scratch (version 25 layout, our own values — no FL preset data), zlib "stored" blocks.
  10276. namespace
  10277. {
  10278. struct SytrusTestPatch
  10279. {
  10280. std::vector<std::int32_t> main = std::vector<std::int32_t> (240, 0); // 962-byte main block as ints (+ mute bytes)
  10281. struct Op
  10282. {
  10283. int p[10] { 0, 256, 0, 0, 0, 0, 0, 0, 0, 0 };
  10284. int ratio = 20000, offset = 0, noise = 0;
  10285. std::vector<float> amps { 1.0f };
  10286. std::uint8_t flags[8] {};
  10287. int env1[3] { -1, -1, -1 }; // volume envelope: on (≠ −1 → on), decay point, sustain point
  10288. std::vector<std::pair<double, double>> env1pts;
  10289. } ops[6];
  10290. struct Filt
  10291. {
  10292. int p[10] { 0, 0, 128, 0, 128, 0, 0, 128, 128, 0 };
  10293. int type = 3;
  10294. } filts[3];
  10295. std::array<std::array<std::uint8_t, 23>, 3> delays {};
  10296. std::array<std::int32_t, 9> reverb { 19, 221, 0, 50, 80, 30, 221, 2, 0 };
  10297. bool reverbOn = false;
  10298. int eqmode = 0;
  10299. SytrusTestPatch()
  10300. {
  10301. main[0] = 128, main[1] = 128;
  10302. for (int i = 0; i < 8; ++i)
  10303. main[(std::size_t) (165 + i)] = (i % 4 == 2) ? 0 : 128;
  10304. main[173] = 1, main[175] = 128, main[188] = 128, main[189] = -1, main[190] = main[191] = 256;
  10305. }
  10306. void cell (int layer, int row, int col, int v) { main[(std::size_t) (3 + layer * 81 + row * 9 + col)] = v; }
  10307. std::vector<std::uint8_t> blob() const
  10308. {
  10309. std::vector<std::uint8_t> b;
  10310. 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))); };
  10311. auto f32 = [&] (float v) { std::int32_t x; std::memcpy (&x, &v, 4); i32 (x); };
  10312. 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))); };
  10313. // point lists: kind 1 envelope, 2 LFO, 3 map / shaper curve; format version 3
  10314. auto env = [&] (int kind, const std::vector<std::pair<double, double>>& pts, int on, int decay, int sus) {
  10315. i32 (kind), i32 (3), i32 ((std::int32_t) pts.size());
  10316. for (const auto& [dt, y] : pts)
  10317. f64 (dt), f64 (y), f32 (0.0f), i32 (0);
  10318. i32 (0), i32 (on), i32 (decay), i32 (-1), i32 (sus);
  10319. };
  10320. for (int i = 0; i < 240; ++i)
  10321. if (4 * i + 4 <= 0x320)
  10322. i32 (main[(std::size_t) i]);
  10323. b.resize (962, 0); // mute bytes: none muted
  10324. i32 (0); // global declick amount
  10325. b.push_back (0), b.push_back (0), b.push_back (0); // OSC grouping, DC stage, GIBBS OFF
  10326. for (int k = 0; k < 9; ++k)
  10327. b.push_back (k == 7 ? 1 : 0); // flag group (release multiplier on, as Default.fst)
  10328. for (const auto& d : delays)
  10329. b.insert (b.end(), d.begin(), d.end());
  10330. for (auto v : reverb)
  10331. i32 (v);
  10332. b.push_back (reverbOn ? 1 : 0), b.push_back (0);
  10333. i32 (eqmode);
  10334. for (int k = 0; k < 3; ++k)
  10335. i32 (0), i32 (30000), i32 (20000), i32 (k);
  10336. b.push_back (0); // no info text
  10337. for (int u = 0; u < 10; ++u)
  10338. {
  10339. const int narts = u < 6 ? 6 : (u < 9 ? 8 : 2);
  10340. for (int j = 0; j < 10; ++j)
  10341. i32 (u < 6 ? ops[u].p[j] : (u < 9 ? filts[u - 6].p[j] : 0));
  10342. if (u < 6)
  10343. {
  10344. const auto& o = ops[u];
  10345. const auto at = b.size();
  10346. b.resize (at + 0x828, 0);
  10347. auto put = [&] (std::size_t off, const void* src, std::size_t n) { std::memcpy (b.data() + at + off, src, n); };
  10348. put (0, &o.ratio, 4), put (4, &o.offset, 4), put (8, &o.noise, 4);
  10349. put (0xc, o.amps.data(), 4 * o.amps.size());
  10350. const std::int32_t shape[5] { 0, 0, 0, 0, 128 };
  10351. put (0x80c, shape, sizeof shape);
  10352. put (0x820, o.flags, 8);
  10353. }
  10354. else if (u < 9)
  10355. {
  10356. const auto at = b.size();
  10357. b.resize (at + 0x30, 0);
  10358. std::memcpy (b.data() + at + 0x2c, &filts[u - 6].type, 4);
  10359. env (3, { { 0.0, 0.0 }, { 1.0, 1.0 } }, 0, -1, -1);
  10360. b.push_back (0);
  10361. }
  10362. for (int k = 0; k < narts; ++k)
  10363. {
  10364. const bool add = k == 0 || k == 3 || (u >= 6 && u <= 8 && (k == 2 || k == 3)) || (u < 6 && k == 4);
  10365. const double neutral = add ? 0.5 : 1.0;
  10366. i32 (3), i32 (0), f32 (1.0f), f32 (1.0f);
  10367. if (u < 6 && k == 1 && ops[u].env1[0] != -1)
  10368. env (1, ops[u].env1pts, -1, ops[u].env1[1], ops[u].env1[2]);
  10369. else
  10370. env (1, { { 0.0, 0.0 }, { 1.0, 1.0 } }, 0, -1, -1);
  10371. i32 (128), i32 (128), i32 (0), i32 (128);
  10372. env (2, { { 0.0, 1.0 }, { 1.0, 1.0 } }, 0, -1, -1);
  10373. i32 (40000), i32 (0), i32 (0), i32 (0), i32 (0);
  10374. for (int m = 0; m < 6; ++m)
  10375. env (3, { { 0.0, neutral }, { 1.0, neutral } }, 0, -1, -1);
  10376. }
  10377. }
  10378. b.push_back (0), b.push_back (0);
  10379. // zlib with stored blocks
  10380. std::vector<std::uint8_t> z { 0x78, 0x01 };
  10381. for (std::size_t at = 0; at < b.size(); at += 65535)
  10382. {
  10383. const auto n = std::min<std::size_t> (65535, b.size() - at);
  10384. z.push_back (at + n >= b.size() ? 1 : 0);
  10385. z.push_back ((std::uint8_t) n), z.push_back ((std::uint8_t) (n >> 8));
  10386. z.push_back ((std::uint8_t) ~n), z.push_back ((std::uint8_t) (~n >> 8));
  10387. z.insert (z.end(), b.begin() + (std::ptrdiff_t) at, b.begin() + (std::ptrdiff_t) (at + n));
  10388. }
  10389. std::uint32_t a = 1, c = 0;
  10390. for (auto x : b)
  10391. a = (a + x) % 65521, c = (c + a) % 65521;
  10392. for (int k = 3; k >= 0; --k)
  10393. z.push_back ((std::uint8_t) (((c << 16) | a) >> (8 * k)));
  10394. std::vector<std::uint8_t> out { 25, 0, 0, 0 };
  10395. out.insert (out.end(), z.begin(), z.end());
  10396. return out;
  10397. }
  10398. };
  10399. // renders like FL's Gen_Render calls: notes start/release at the block that contains them
  10400. std::pair<std::vector<float>, std::vector<float>> sytrusRender (const std::vector<std::uint8_t>& blob, bool renderMode, int n,
  10401. std::vector<std::array<int, 3>> notes, double fs = 44100.0)
  10402. {
  10403. lynx::sytrus::Engine e;
  10404. e.setTempo (120.0);
  10405. e.prepare (fs, renderMode, 512);
  10406. e.setPatch (lynx::sytrus::parsePatch (blob));
  10407. std::vector<float> l ((std::size_t) n), r ((std::size_t) n);
  10408. for (int n0 = 0; n0 < n; n0 += 512)
  10409. {
  10410. for (std::size_t k = 0; k < notes.size(); ++k)
  10411. {
  10412. if (notes[k][1] >= n0 && notes[k][1] < n0 + 512)
  10413. {
  10414. lynx::sytrus::NoteParams p;
  10415. p.key = (float) notes[k][0];
  10416. p.id = (std::int64_t) k + 1;
  10417. e.noteOn (p);
  10418. }
  10419. if (notes[k][2] >= n0 && notes[k][2] < n0 + 512)
  10420. e.release ((std::int64_t) k + 1);
  10421. }
  10422. e.render (l.data() + n0, r.data() + n0, std::min (512, n - n0), 1.0f, 0.0f);
  10423. }
  10424. return { l, r };
  10425. }
  10426. } // namespace
  10427. TEST (sytrus_engine_sine)
  10428. {
  10429. // one sine operator, ratio 1, OUT 256, no envelopes: C5 = 523.2511 Hz at 0.7071 (centre of the pan law, §6.1)
  10430. SytrusTestPatch t;
  10431. t.cell (0, 0, 8, 256);
  10432. const auto blob = t.blob();
  10433. CHECK (lynx::sytrus::stateUsable (blob));
  10434. CHECK (lynx::sytrus::parsePatch (blob) != nullptr);
  10435. auto broken = blob;
  10436. broken.resize (broken.size() / 2);
  10437. CHECK (! lynx::sytrus::stateUsable (broken));
  10438. const auto [l, r] = sytrusRender (blob, false, 8192, { { 60, 0, 4096 } });
  10439. double worst = 0.0;
  10440. for (int k = 0; k < 4000; ++k)
  10441. 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)));
  10442. CHECK (worst < 2e-5);
  10443. CHECK (l == r);
  10444. // 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
  10445. // 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)
  10446. int last = 0;
  10447. for (int k = 0; k < 8192; ++k)
  10448. if (l[(std::size_t) k] != 0.0f)
  10449. last = k;
  10450. CHECK (last == 4895);
  10451. // key 69 = 880 Hz; 48 kHz
  10452. const auto [l2, r2] = sytrusRender (blob, false, 4096, { { 69, 0, -1 } }, 48000.0);
  10453. worst = 0.0;
  10454. for (int k = 0; k < 4000; ++k)
  10455. 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)));
  10456. CHECK (worst < 5e-5);
  10457. (void) r2;
  10458. }
  10459. TEST (sytrus_engine_reference)
  10460. {
  10461. // FM 2→1, Vanilla LP filter, volume envelope, unison 3, delay and reverb, 2× oversampling with the 8-sample
  10462. // envelope tick (render settings): against our Python reference model of REA_rodada31 (sytmodel.render)
  10463. SytrusTestPatch t;
  10464. t.main[195] = 0, t.main[196] = 1, t.main[193] = 0, t.main[194] = 1;
  10465. t.main[173] = 3, t.main[174] = 64, t.main[176] = 100;
  10466. t.ops[1].ratio = 40000;
  10467. t.cell (0, 0, 1, 128); // op 2 → op 1 FM
  10468. t.cell (0, 0, 8, 128);
  10469. t.cell (0, 6, 0, 256); // op 1 → filter 1
  10470. t.cell (0, 6, 8, 256);
  10471. t.cell (0, 0, 7, 200); // FX send
  10472. t.filts[0].p[0] = 1, t.filts[0].p[2] = 180, t.filts[0].p[3] = 60;
  10473. t.ops[0].env1[0] = 0, t.ops[0].env1[1] = 1, t.ops[0].env1[2] = 2;
  10474. t.ops[0].env1pts = { { 0.0, 0.0 }, { 0.05, 1.0 }, { 0.1, 0.6 }, { 0.2, 0.0 } };
  10475. const std::int32_t d0[5] { 40, 100, 50, 100, 0 };
  10476. std::memcpy (t.delays[0].data(), d0, 20);
  10477. t.delays[0][22] = 1;
  10478. t.reverb = { 19, 221, 10, 50, 80, 30, 100, 2, 100 };
  10479. t.reverbOn = true;
  10480. const auto blob = t.blob();
  10481. const auto [l, r] = sytrusRender (blob, true, 12000, { { 60, 0, 6000 } });
  10482. // reference values (model render_mode=True, 44.1 kHz, block 512, key 60, velocity 0.78125, release at 6000); the same
  10483. // state rendered by FL's own Sytrus DLL in render mode differs from this by −136 dB (EXEC, REA round 31 harness)
  10484. const std::array<std::pair<int, float>, 8> golden { { { 100, -0.00235529384f }, { 777, -0.175126165f }, { 2000, 0.30976975f }, { 4321, -0.0213534199f },
  10485. { 6100, 0.202372357f }, { 7000, 0.249530748f }, { 9000, 0.0307101421f }, { 11999, 0.176390275f } } };
  10486. for (const auto& [i, v] : golden)
  10487. CHECK (std::abs (l[(std::size_t) i] - v) < 1e-6f);
  10488. const std::array<std::pair<int, float>, 3> goldenR { { { 777, -0.166890964f }, { 4321, -0.212358952f }, { 9000, -0.0251909085f } } };
  10489. for (const auto& [i, v] : goldenR)
  10490. CHECK (std::abs (r[(std::size_t) i] - v) < 1e-6f);
  10491. }
  10492. TEST (sytrus_instrument)
  10493. {
  10494. // the LYNX instrument: state in ChannelDef::pluginState, channel volume/pan recovered from the rack gains
  10495. SytrusTestPatch t;
  10496. t.cell (0, 0, 8, 256);
  10497. lynx::ChannelDef c;
  10498. c.instrument = lynx::InstrumentKind::sytrus;
  10499. c.pluginState = t.blob();
  10500. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sytrus);
  10501. inst->prepare (44100.0);
  10502. inst->configure (c, 120.0);
  10503. CHECK (inst->idle());
  10504. std::vector<float> l (4096), r (4096);
  10505. inst->noteOn (60, 100.0f / 128.0f);
  10506. // 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
  10507. inst->render (l.data(), r.data(), 4096, 0.5f, 0.5f);
  10508. double peak = 0.0;
  10509. for (int k = 1000; k < 4096; ++k)
  10510. peak = std::max (peak, (double) std::abs (l[(std::size_t) k]));
  10511. CHECK (std::abs (peak - 0.5 * 0.70710678) < 1e-3); // Final.Vol 0.5 × the plugin's centre pan law
  10512. CHECK (! inst->idle());
  10513. inst->noteOff (60);
  10514. std::vector<float> tail (44100 * 3), tailR (tail.size());
  10515. inst->render (tail.data(), tailR.data(), (int) tail.size(), 0.5f, 0.5f);
  10516. CHECK (inst->idle());
  10517. CHECK (tail.back() == 0.0f);
  10518. }
  10519. TEST (harmor_image)
  10520. {
  10521. // REA_rodada75: image synthesis — a looping 6-column image (gain + frequency planes) read at a speed, cubic gain and
  10522. // linear frequency interpolation, Hz frequency mode, formant shift — against our reference model (r75 imgm.py)
  10523. const auto blob = harmorTestState (false, true);
  10524. if (const char* dump = std::getenv ("LYNX_DUMP_HARMOR_IMAGE"))
  10525. std::ofstream (dump, std::ios::binary).write ((const char*) blob.data(), (std::streamsize) blob.size());
  10526. CHECK (lynx::harmorStateUsable (blob));
  10527. CHECK (lynx::harmorFallbacks (blob).empty()); // (the image is modelled)
  10528. const auto ref = lynx::harmor::renderReference (blob, { { 0, 60, 0.78125, 1.0, 0.0, 6144 }, { 2000, 64, 0.6, 1.0, 0.0, 7000 } }, 44100.0, 8192);
  10529. CHECK (ref.size() == 2 * 8192);
  10530. if (std::getenv ("LYNX_PRINT_HARMOR_IMAGE") != nullptr)
  10531. for (int i : { 100, 1000, 2500, 3000, 5000, 6500, 8191 })
  10532. std::printf ("harmor image %d %.8g %.8g\n", i, (double) ref[2 * (std::size_t) i], (double) ref[2 * (std::size_t) i + 1]);
  10533. const std::tuple<int, float, float> golden[] {
  10534. { 100, 0.0022269291f, 0.018017211f }, { 1000, 0.013962966f, -0.028602127f }, { 2500, 0.016781429f, -0.028931717f },
  10535. { 3000, -0.010532441f, -0.0061010128f }, { 5000, -0.017426063f, 0.016474312f }, { 6500, 0.1689423f, 0.078417062f },
  10536. { 8191, -0.045403875f, -0.049045475f } };
  10537. for (const auto& [i, gl, gr] : golden)
  10538. CHECK (std::abs (ref[2 * (std::size_t) i] - gl) < 2e-6f && std::abs (ref[2 * (std::size_t) i + 1] - gr) < 2e-6f);
  10539. // the image changes the sound: without it (gain mix 0, frequency scale 0) the same notes render differently
  10540. auto plain = blob;
  10541. const std::int32_t zero = 0;
  10542. std::memcpy (plain.data() + lynx::harmorParamOffset (plain, 0, 89), &zero, 4);
  10543. std::memcpy (plain.data() + lynx::harmorParamOffset (plain, 0, 92), &zero, 4);
  10544. std::memcpy (plain.data() + lynx::harmorParamOffset (plain, 0, 94), &zero, 4);
  10545. const auto ref0 = lynx::harmor::renderReference (plain, { { 0, 60, 0.78125, 1.0, 0.0, 6144 }, { 2000, 64, 0.6, 1.0, 0.0, 7000 } }, 44100.0, 8192);
  10546. double diff = 0.0, energy = 0.0;
  10547. for (std::size_t i = 0; i < ref.size(); ++i)
  10548. diff += (double) (ref[i] - ref0[i]) * (ref[i] - ref0[i]), energy += (double) ref[i] * ref[i];
  10549. CHECK (energy > 1.0 && diff > 0.01 * energy);
  10550. // a resynthesis-mode image (a sample analysed at load time) is not modelled: reported, the part plays its timbres
  10551. auto resyn = blob;
  10552. {
  10553. // the image object follows the 26 curves: find its header (version 0, mode 0, loop 1) from the end of part A
  10554. const std::uint8_t hdr[] = { 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xf0, 0xbf };
  10555. const auto it = std::search (resyn.begin(), resyn.end(), std::begin (hdr), std::end (hdr));
  10556. CHECK (it != resyn.end());
  10557. if (it != resyn.end())
  10558. *(it + 4) = 1;
  10559. }
  10560. const auto fb = lynx::harmorFallbacks (resyn);
  10561. CHECK (std::any_of (fb.begin(), fb.end(), [] (const std::string& f) { return f.find ("resynthesis") != std::string::npos; }));
  10562. }
  10563. TEST (harmor_round75_features)
  10564. {
  10565. // REA_rodada75: arpeggio markers, noise resonance, pluck blur mode, pluck blend, harmonic randomness, phaser and
  10566. // harmonizer masks — LYNX's engine against our reference model (r75 hmodel.py, the Mersenne Twister seeded like LYNX's)
  10567. const std::vector<lynx::harmor::RefNote> chord { { 0, 60, 0.8, 1.0, 0.0, 9000 }, { 1500, 64, 0.7, 1.0, 0.0, 9500 }, { 3000, 67, 0.9, 1.0, 0.0, 10000 } };
  10568. struct Case { int features; std::array<std::tuple<int, float, float>, 4> golden; };
  10569. const Case cases[] {
  10570. { 1, { { { 2000, 0.00326227601f, -0.00143631078f }, { 4500, 0.104633873f, 0.0262026641f }, { 7000, -0.00242280135f, 0.00364600495f }, { 11000, -0.0029983636f, -0.00308720154f } } } },
  10571. { 2, { { { 2000, 0.294110536f, -0.229805974f }, { 4500, -0.00492941859f, -0.00132748827f }, { 7000, 0.0132401136f, 0.00544014934f }, { 11000, -0.00105760543f, -0.00210882883f } } } },
  10572. { 4, { { { 2000, 0.0720629904f, -0.0296807697f }, { 4500, -0.10764533f, 0.113311439f }, { 7000, -0.229827746f, -0.101087983f }, { 11000, -0.0519010639f, -0.0665841137f } } } },
  10573. { 8, { { { 2000, 0.0720039901f, -0.0297230242f }, { 4500, -0.107470558f, 0.113653905f }, { 7000, -0.228478677f, -0.152556434f }, { 11000, -0.0412466244f, -0.0504480202f } } } },
  10574. { 16, { { { 2000, 0.0634379551f, -0.0309438618f }, { 4500, -0.112099687f, 0.112784195f }, { 7000, -0.231666087f, -0.167064955f }, { 11000, -0.0411841346f, -0.0503804788f } } } },
  10575. { 32, { { { 2000, 0.0862953135f, -0.0358822417f }, { 4500, 0.0694009664f, 0.141198453f }, { 7000, -0.158969622f, -0.103794064f }, { 11000, -0.0294273162f, -0.0417808629f } } } },
  10576. { 63, { { { 2000, 0.0172010472f, -0.0106831535f }, { 4500, 0.556600683f, 0.0811084217f }, { 7000, -0.00255421684f, 0.020610704f }, { 11000, -0.00318711501f, 0.000247763831f } } } } };
  10577. for (const auto& c : cases)
  10578. {
  10579. const auto blob = harmorTestState (false, false, c.features);
  10580. if (const char* dump = std::getenv ("LYNX_DUMP_HARMOR_R75"))
  10581. std::ofstream (std::string (dump) + std::to_string (c.features) + ".bin", std::ios::binary).write ((const char*) blob.data(), (std::streamsize) blob.size());
  10582. CHECK (lynx::harmorStateUsable (blob));
  10583. CHECK (lynx::harmorFallbacks (blob).empty());
  10584. const auto ref = lynx::harmor::renderReference (blob, chord, 44100.0, 12000);
  10585. if (std::getenv ("LYNX_PRINT_HARMOR_R75") != nullptr)
  10586. for (int i : { 2000, 4500, 7000, 11000 })
  10587. std::printf ("harmor r75 %d %d %.8g %.8g\n", c.features, i, (double) ref[2 * (std::size_t) i], (double) ref[2 * (std::size_t) i + 1]);
  10588. for (const auto& [i, gl, gr] : c.golden)
  10589. CHECK (std::abs (ref[2 * (std::size_t) i] - gl) < 2e-6f && std::abs (ref[2 * (std::size_t) i + 1] - gr) < 2e-6f);
  10590. }
  10591. }
  10592. TEST (sytrus_main_settings)
  10593. {
  10594. // the channel settings panel's controls (REA_rodada74 §3): FL's "Main settings" ints inside the zlib state
  10595. SytrusTestPatch t;
  10596. t.cell (0, 0, 8, 256);
  10597. t.main[173] = 3;
  10598. const auto blob = t.blob();
  10599. const auto& S = lynx::sytrus::settings();
  10600. auto find = [&] (const char* name) {
  10601. for (int i = 0; i < (int) S.size(); ++i)
  10602. if (std::string (S[(std::size_t) i].name) == name)
  10603. return i;
  10604. return -1;
  10605. };
  10606. const int master = find ("Master level"), unison = find ("Unison"), porta = find ("Porta"), live = find ("Live oversampling");
  10607. CHECK (master >= 0 && unison >= 0 && porta >= 0 && live >= 0 && S.size() == 25u);
  10608. CHECK (lynx::sytrus::settingValue (blob, master) == 128 && lynx::sytrus::settingValue (blob, unison) == 3);
  10609. CHECK (lynx::sytrus::settingValue (blob, porta) == 0 && lynx::sytrus::settingValue (blob, find ("Mod X")) == 256);
  10610. CHECK (! lynx::sytrus::settingValue (blob, -1) && ! lynx::sytrus::settingValue (blob, (int) S.size()));
  10611. auto broken = blob;
  10612. broken.resize (broken.size() / 2);
  10613. CHECK (! lynx::sytrus::settingValue (broken, master) && ! lynx::sytrus::setSetting (broken, master, 64));
  10614. // master level 64: gain (11^(64/128) − 1)/10 instead of 1 (REA_rodada11 §1.3); the state stays readable
  10615. auto quiet = blob;
  10616. CHECK (lynx::sytrus::setSetting (quiet, master, 64) && lynx::sytrus::stateUsable (quiet));
  10617. CHECK (lynx::sytrus::settingValue (quiet, master) == 64 && lynx::sytrus::settingValue (quiet, unison) == 3);
  10618. CHECK (quiet[0] == blob[0] && quiet[1] == blob[1]); // version word kept
  10619. const auto [l1, r1] = sytrusRender (blob, false, 4096, { { 60, 0, -1 } });
  10620. const auto [l2, r2] = sytrusRender (quiet, false, 4096, { { 60, 0, -1 } });
  10621. const auto g = (std::sqrt (11.0) - 1.0) / 10.0;
  10622. double worst = 0.0;
  10623. for (int k = 0; k < 4096; ++k)
  10624. worst = std::max (worst, std::abs ((double) l2[(std::size_t) k] - g * l1[(std::size_t) k]));
  10625. CHECK (worst < 1e-5);
  10626. (void) r1, (void) r2;
  10627. // clamped to the control's range; PORTA is the flag byte the engine reads
  10628. auto edited = blob;
  10629. CHECK (lynx::sytrus::setSetting (edited, unison, 42) && lynx::sytrus::settingValue (edited, unison) == 9);
  10630. CHECK (lynx::sytrus::setSetting (edited, porta, 1) && lynx::sytrus::settingValue (edited, porta) == 1);
  10631. CHECK (lynx::sytrus::setSetting (edited, live, 2) && lynx::sytrus::settingValue (edited, live) == 2);
  10632. CHECK (lynx::sytrus::parsePatch (edited) != nullptr);
  10633. }
  10634. TEST (harmor_effects_rack)
  10635. {
  10636. // REA_rodada38: the rack runs in the state's unit order and the records' FX send joins it at unit 4 — with the FX
  10637. // amount at 1000 every record output bypasses a reverb placed before unit 4 (EXEC: −600 dB vs the rack off)
  10638. auto blob = harmorTestState (false);
  10639. auto set = [&] (std::vector<std::uint8_t>& st, int part, int index, std::int32_t v) {
  10640. std::memcpy (st.data() + lynx::harmorParamOffset (st, part, index), &v, 4);
  10641. };
  10642. set (blob, 0, 10, 1000); // all to the FX send
  10643. auto render = [] (const std::vector<std::uint8_t>& st) {
  10644. return lynx::harmor::renderReference (st, { { 0, 60, 0.8, 1.0, 0.0, 6000 } }, 44100.0, 12000);
  10645. };
  10646. const auto dry = render (blob);
  10647. auto rev = blob;
  10648. set (rev, -1, 22, 1); // reverb on: room 50, diffusion 100, decay 2^3·0.1 s, high cut / damping off, wet 0.8
  10649. 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);
  10650. set (rev, -1, 30, 2), set (rev, -1, 31, 800);
  10651. const auto bypassed = render (rev);
  10652. double eb = 0.0, db = 0.0;
  10653. for (std::size_t i = 0; i < dry.size(); ++i)
  10654. eb += (double) (bypassed[i] - dry[i]) * (bypassed[i] - dry[i]), db += (double) dry[i] * dry[i];
  10655. CHECK (eb < 1e-12 * db && db > 1e-3); // (float rounding of the rack's buffers only)
  10656. auto after = rev;
  10657. const std::int32_t order[6] { 0, 1, 2, 4, 3, 5 }; // the join before the reverb
  10658. for (int k = 0; k < 6; ++k)
  10659. set (after, -1, 53 + k, order[k]);
  10660. const auto wet = render (after);
  10661. double e = 0.0, d = 0.0;
  10662. bool finite = true;
  10663. for (std::size_t i = 0; i < wet.size(); ++i)
  10664. e += (double) (wet[i] - dry[i]) * (wet[i] - dry[i]), d += (double) dry[i] * dry[i], finite = finite && std::isfinite (wet[i]);
  10665. CHECK (finite && e > 1e-4 * d && d > 1e-3);
  10666. // a distortion (type 6) and a delay on the dry path change the sound, stay finite, and the instrument ends quiet
  10667. auto dist = harmorTestState (false);
  10668. {
  10669. const auto at = lynx::harmorParamOffset (dist, -1, 0) + 0xd4 + 0x18 + 48; // global misc +48: distortion type
  10670. const std::int32_t six = 6;
  10671. std::memcpy (dist.data() + at, &six, 4);
  10672. }
  10673. set (dist, -1, 17, 600), set (dist, -1, 19, 1000), set (dist, -1, 21, 1100);
  10674. 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);
  10675. set (dist, -1, 47, 1050);
  10676. lynx::ChannelDef c;
  10677. c.instrument = lynx::InstrumentKind::harmor;
  10678. c.pluginState = dist;
  10679. auto inst = lynx::makeInstrument (lynx::InstrumentKind::harmor);
  10680. inst->configure (c, 120.0);
  10681. inst->prepare (44100.0);
  10682. std::vector<float> l (44100 * 4), r (l.size());
  10683. inst->noteOn (60, 0.8f);
  10684. double energy = 0.0;
  10685. for (int s = 0; s < (int) l.size(); s += 512)
  10686. {
  10687. if (s == 8192)
  10688. inst->noteOff (60);
  10689. inst->render (l.data() + s, r.data() + s, std::min (512, (int) l.size() - s), 1.0f, 1.0f);
  10690. }
  10691. for (std::size_t i = 0; i < l.size(); ++i)
  10692. finite = finite && std::isfinite (l[i]) && std::isfinite (r[i]), energy += (double) l[i] * l[i];
  10693. CHECK (finite && energy > 1.0);
  10694. std::vector<float> tail (44100 * 6), tailR (tail.size());
  10695. for (int s = 0; s < (int) tail.size(); s += 512)
  10696. inst->render (tail.data() + s, tailR.data() + s, std::min (512, (int) tail.size() - s), 1.0f, 1.0f);
  10697. CHECK (inst->idle());
  10698. }
  10699. TEST (harmless_engine)
  10700. {
  10701. // REA_rodada32: state parsing, the additive engine against our reference model (hlmodel.py), host levels, voice end
  10702. std::vector<std::uint8_t> blob;
  10703. 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))); };
  10704. std::array<std::int32_t, 100> p {}; // the reference's clean patch: Saw, open filter, no modulation
  10705. for (int i = 11; i < 23; ++i)
  10706. p[(std::size_t) i] = 1000;
  10707. 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;
  10708. 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;
  10709. // + resonance, pluck, 3-voice unison, tremolo, LFO on pitch
  10710. 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;
  10711. put (12);
  10712. for (auto v : p)
  10713. put (v);
  10714. for (int i = 0; i < 7; ++i)
  10715. put (0);
  10716. put (524); // Custom colour table: 524 × (amplitude, phase)
  10717. for (int i = 0; i < 1048; ++i)
  10718. {
  10719. const auto f = i < 524 ? 1.0f / (float) (i + 1) : 0.25f;
  10720. std::uint32_t bits = 0;
  10721. std::memcpy (&bits, &f, 4);
  10722. put ((std::int32_t) bits);
  10723. }
  10724. const auto parsed = lynx::harmlessParams (blob);
  10725. CHECK (parsed && (int) parsed->size() == lynx::harmlessParamCount);
  10726. CHECK (parsed && (*parsed)[54] == 3500.0f && (*parsed)[99] == 0.0f && (*parsed)[lynx::harmlessCustomFlagIndex] == 1.0f);
  10727. CHECK (parsed && (*parsed)[lynx::harmlessCustomIndex + 1] == 0.5f && (*parsed)[lynx::harmlessCustomIndex + 524] == 0.25f);
  10728. auto old = blob;
  10729. old[0] = 3; // versions below 4 are not Harmless states we know
  10730. CHECK (! lynx::harmlessParams (old));
  10731. old = blob;
  10732. old.resize (300);
  10733. CHECK (! lynx::harmlessParams (old));
  10734. lynx::ChannelDef c;
  10735. c.instrument = lynx::InstrumentKind::harmless;
  10736. c.modelParams = *parsed;
  10737. c.modelParams[lynx::harmlessCustomFlagIndex] = 0.0f; // colour 0 (Saw): the custom table is not read
  10738. auto render = [] (const lynx::ChannelDef& def, int total, float gl, float gr, int offAt) {
  10739. auto inst = lynx::makeInstrument (lynx::InstrumentKind::harmless);
  10740. inst->configure (def, 120.0);
  10741. inst->prepare (44100.0);
  10742. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  10743. inst->noteOn (60, 0.78125f);
  10744. for (int s = 0; s < total; s += 512)
  10745. {
  10746. if (s == offAt)
  10747. inst->noteOff (60);
  10748. inst->render (l.data() + s, r.data() + s, std::min (512, total - s), gl, gr);
  10749. }
  10750. return std::make_tuple (l, r, std::move (inst));
  10751. };
  10752. {
  10753. // reference model (REA round 32 hlmodel.py, C5 at FL velocity 100, channel volume 1, centre, released at 6144)
  10754. auto [l, r, inst] = render (c, 8192, 1.0f, 1.0f, 6144);
  10755. const std::tuple<int, float, float> golden[] {
  10756. { 0, 0.0f, 0.0f }, { 10, -0.02220994f, -0.06232700f }, { 100, 0.40623373f, 0.17308202f }, { 1000, -0.04091963f, -0.26821208f },
  10757. { 3000, 0.14519004f, 0.11027778f }, { 6000, 0.50411958f, 0.27901855f }, { 8191, 0.02117692f, -0.01466602f } };
  10758. for (const auto& [i, gl, gr] : golden)
  10759. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  10760. // after the release the record fades out (below 1e-4, then 30 ms) and the instrument goes idle
  10761. std::vector<float> tail (44100), tailR (tail.size());
  10762. inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
  10763. CHECK (inst->idle());
  10764. }
  10765. // FL's level law applied once: half the channel volume halves the output; full right pan silences the left side
  10766. const auto full = std::get<0> (render (c, 2048, 1.0f, 1.0f, -1));
  10767. const auto half = std::get<0> (render (c, 2048, 0.5f, 0.5f, -1));
  10768. CHECK (std::abs (half[100] - 0.5f * full[100]) < 1e-6f && std::abs (full[100]) > 0.1f);
  10769. auto mono = c;
  10770. mono.modelParams[25] = 0.0f; // no tremolo, no unison pan spread
  10771. mono.modelParams[65] = 1.0f;
  10772. const auto [pl, pr, pi] = render (mono, 2048, 0.0f, 1.41421356f, -1);
  10773. double el = 0.0, er = 0.0;
  10774. for (int i = 0; i < 2048; ++i)
  10775. el += (double) pl[(std::size_t) i] * pl[(std::size_t) i], er += (double) pr[(std::size_t) i] * pr[(std::size_t) i];
  10776. CHECK (el < 1e-10 * er && er > 1.0);
  10777. }
  10778. TEST (startup_sound)
  10779. {
  10780. // the splash's sound: the right length, finite, at its peak level, quiet before the whoosh swells,
  10781. // a landing "pop" at impactTime and a chime ringing after it
  10782. using namespace lynx::startup;
  10783. const auto sound = makeSound (48000.0);
  10784. CHECK (sound.left.size() == (std::size_t) (soundLength * 48000.0) && sound.right.size() == sound.left.size());
  10785. float peak = 0.0f;
  10786. bool finite = true;
  10787. for (std::size_t i = 0; i < sound.left.size(); ++i)
  10788. {
  10789. finite = finite && std::isfinite (sound.left[i]) && std::isfinite (sound.right[i]);
  10790. peak = std::max ({ peak, std::abs (sound.left[i]), std::abs (sound.right[i]) });
  10791. }
  10792. CHECK (finite && std::abs (peak - peakLevel) < 1.0e-4f);
  10793. auto rms = [&sound] (double from, double to) {
  10794. double e = 0.0;
  10795. const auto a = (std::size_t) (from * 48000.0), b = (std::size_t) (to * 48000.0);
  10796. for (auto i = a; i < b; ++i)
  10797. e += (double) sound.left[i] * sound.left[i];
  10798. return std::sqrt (e / (double) (b - a));
  10799. };
  10800. CHECK (rms (0.0, 0.05) < 0.2 * rms (impactTime - 0.05, impactTime)); // the whoosh swells
  10801. CHECK (rms (impactTime, impactTime + 0.05) > rms (impactTime - 0.05, impactTime)); // the landing
  10802. CHECK (rms (0.8, 1.0) > 0.005 && rms (soundLength - 0.02, soundLength) < rms (0.8, 1.0)); // chime rings, tail fades
  10803. CHECK (makeSound (48000.0).left == sound.left); // the same sound on every start
  10804. }
  10805. namespace
  10806. {
  10807. // little-endian byte writer for the state builders below
  10808. struct Bytes
  10809. {
  10810. std::vector<std::uint8_t> b;
  10811. void raw (const char* s, std::size_t n) { b.insert (b.end(), s, s + n); }
  10812. 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); }
  10813. void f32 (float v) { b.insert (b.end(), reinterpret_cast<const std::uint8_t*> (&v), reinterpret_cast<const std::uint8_t*> (&v) + 4); }
  10814. void zeros (std::size_t n) { b.insert (b.end(), n, 0); }
  10815. void bytes (const std::vector<std::uint8_t>& o) { b.insert (b.end(), o.begin(), o.end()); }
  10816. };
  10817. // A GMS state written from scratch (REA_rodada12 §1.1 layout, "Syn4"): oscillator waves that don't exist (silent
  10818. // tables, so the result does not depend on an FL installation), noise through the ladder with envelope and LFO,
  10819. // unison, slide, distortion, delay, reverb and the output EQ.
  10820. std::vector<std::uint8_t> gmsTestState()
  10821. {
  10822. std::array<float, 212> P {};
  10823. for (int i : { 1, 2, 44, 55, 56, 57, 58, 59, 60, 207, 208, 209, 210, 211 })
  10824. P[(std::size_t) i] = 0.5f;
  10825. P[0] = 0.75f, P[4] = 0.1f, P[5] = 0.4f, P[6] = 0.6f, P[7] = 0.3f, P[8] = 0.8f;
  10826. 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;
  10827. 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;
  10828. P[147] = 1.0f, P[117] = 0.3f, P[127] = 0.6f; // distortion
  10829. P[153] = 1.0f, P[123] = 0.5f, P[133] = 0.4f, P[143] = 0.4f; // delay (1/2 beat)
  10830. P[154] = 1.0f, P[124] = 0.6f, P[134] = 0.5f; // reverb
  10831. P[206] = 1.0f, P[207] = 0.7f, P[211] = 0.3f; // output EQ
  10832. Bytes preset;
  10833. preset.raw ("4nyS", 4);
  10834. for (const char* path : { "NoSuchDir\\NoWave A.wav", "NoSuchDir\\NoWave B.wav", "NoSuchDir\\NoWave C.wav", "", "", "", "" })
  10835. {
  10836. const auto n = std::strlen (path);
  10837. preset.raw (path, n);
  10838. preset.zeros (512 - n);
  10839. }
  10840. preset.i32 (0), preset.i32 (0), preset.i32 (0);
  10841. for (auto v : P)
  10842. preset.f32 (v);
  10843. preset.raw ("LYNX test", 9);
  10844. preset.zeros (512 - 9);
  10845. Bytes s;
  10846. s.raw ("SSLF", 4);
  10847. s.i32 ((std::int32_t) preset.b.size());
  10848. s.bytes (preset.b);
  10849. return s.b;
  10850. }
  10851. // A Morphine state written from scratch (REA_rodada16 §3 / REA_rodada39 §2.2 layout): generators A and B with a few
  10852. // frames each (A loops, PWM on A), C and D silent, a morph line A→B, four envelopes (Eg1 → pitch and velocity →
  10853. // amplitude through the matrix), the master ADSR and every plugin effect on.
  10854. std::vector<std::uint8_t> morphineTestState()
  10855. {
  10856. auto mbp = [] (int seed) {
  10857. Bytes b;
  10858. b.raw ("0PBM", 4);
  10859. for (int a = 0; a < 3; ++a)
  10860. for (int byte = 0; byte < 16; ++byte)
  10861. {
  10862. b.b.push_back (0xff);
  10863. for (int bit = 0; bit < 8; ++bit)
  10864. {
  10865. const auto k = byte * 8 + bit;
  10866. const float v = a == 0 ? std::cbrt (1.0f / (float) (k + 1 + seed * (k % 3))) * (k < 40 ? 1.0f : 0.2f)
  10867. : a == 1 ? 0.1f * (float) ((k * 7 + seed) % 11 - 5) / 5.0f
  10868. : 0.02f * (float) ((k * 5 + seed) % 7 - 3);
  10869. b.f32 (v);
  10870. }
  10871. }
  10872. return b.b;
  10873. };
  10874. auto msp = [&] (int frames, int seed, int loopMode) {
  10875. Bytes b;
  10876. b.raw ("0PSM", 4);
  10877. b.i32 (frames);
  10878. for (int f = 0; f < frames; ++f)
  10879. {
  10880. const auto m = mbp (seed + f);
  10881. b.i32 (20 + 7 * f), b.f32 (1.0f - 0.2f * (float) f), b.i32 ((std::int32_t) m.size());
  10882. b.bytes (m);
  10883. }
  10884. b.i32 (0x424);
  10885. b.i32 (10), b.i32 (40), b.i32 (0), b.i32 (loopMode);
  10886. b.f32 (0.45f), b.f32 (0.6f), b.f32 (0.5f), b.i32 (0);
  10887. for (int k = 0; k < 128; ++k)
  10888. b.f32 (0.3f);
  10889. for (int k = 0; k < 128; ++k)
  10890. b.f32 (1.0f);
  10891. return b.b;
  10892. };
  10893. auto mvc = [&] (int frames, int seed, int pwmMode) {
  10894. Bytes z;
  10895. z.raw ("0HZM", 4);
  10896. z.i32 (1);
  10897. const auto s = msp (frames, seed, 1);
  10898. z.i32 (128), z.f32 (frames > 1 ? 0.9f : 0.0f), z.i32 ((std::int32_t) s.size());
  10899. z.bytes (s);
  10900. Bytes b;
  10901. b.raw ("0CVM", 4);
  10902. b.i32 ((std::int32_t) z.b.size());
  10903. b.bytes (z.b);
  10904. 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);
  10905. return b.b;
  10906. };
  10907. Bytes p;
  10908. p.raw ("0roM", 4);
  10909. p.raw ("0RPM", 4);
  10910. for (const auto& g : { mvc (3, 1, 2), mvc (2, 7, 0), mvc (1, 3, 0), mvc (1, 5, 0) })
  10911. {
  10912. p.i32 ((std::int32_t) g.size());
  10913. p.bytes (g);
  10914. }
  10915. {
  10916. Bytes m;
  10917. m.raw ("0LMM", 4);
  10918. m.i32 (2), m.i32 (12);
  10919. m.f32 (0.0f), m.f32 (1.0f), m.i32 (60);
  10920. m.f32 (1.0f), m.f32 (1.0f), m.i32 (60);
  10921. m.i32 (0x54), m.i32 (0), m.i32 (0), m.i32 (0), m.i32 (0);
  10922. for (float v : { 1.0f, 0.8f, 0.0f, 0.0f, 0.5f, 0.4f, 0.5f, 0.5f })
  10923. m.f32 (v);
  10924. for (int k = 0; k < 4; ++k)
  10925. m.i32 (0);
  10926. m.f32 (0.5f), m.f32 (0.5f), m.f32 (0.5f), m.i32 (0);
  10927. p.i32 ((std::int32_t) m.b.size());
  10928. p.bytes (m.b);
  10929. }
  10930. {
  10931. Bytes d;
  10932. d.raw ("0DMM", 4);
  10933. d.i32 (4);
  10934. for (int e = 0; e < 4; ++e)
  10935. {
  10936. Bytes g;
  10937. g.raw ("0GEM", 4);
  10938. g.i32 (3), g.i32 (8);
  10939. g.f32 (0.0f), g.i32 (10), g.f32 (1.0f), g.i32 (40 + 10 * e), g.f32 (0.3f), g.i32 (50);
  10940. 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),
  10941. g.i32 (e == 0 ? 1 : 0);
  10942. d.i32 ((std::int32_t) g.b.size());
  10943. d.bytes (g.b);
  10944. }
  10945. d.i32 (0x94);
  10946. const int src[12] = { 5, 2 }, dst[12] = { 3, 1 };
  10947. const float amt[12] = { 0.56f, 0.7f };
  10948. for (int k = 0; k < 12; ++k)
  10949. d.i32 (src[k]);
  10950. for (int k = 0; k < 12; ++k)
  10951. d.i32 (dst[k]);
  10952. for (int k = 0; k < 12; ++k)
  10953. d.f32 (amt[k]);
  10954. p.i32 ((std::int32_t) d.b.size());
  10955. p.bytes (d.b);
  10956. }
  10957. {
  10958. 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
  10959. 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 …
  10960. 0.4f, 0.3f, 1.0f, 0.5f, 0.5f, 0.4f, 1.0f, 0.6f, 0.4f, 0.5f, // … delay, reverb, EQ …
  10961. 0.5f, 0.5f, 0.55f, 0.5f, 0.45f, 1.0f, 0.0f, 0.5f, 1.0f, 0.0f }; // EQ on, noise (muted)
  10962. Bytes g;
  10963. g.i32 (0x5c4);
  10964. for (auto v : A)
  10965. g.f32 (v);
  10966. g.zeros (0x5c4 - g.b.size());
  10967. p.bytes (g.b);
  10968. }
  10969. Bytes s;
  10970. s.raw ("SSLF", 4);
  10971. s.i32 ((std::int32_t) p.b.size());
  10972. s.bytes (p.b);
  10973. return s.b;
  10974. }
  10975. void dumpIfAsked (const char* var, const std::vector<std::uint8_t>& blob)
  10976. {
  10977. if (const char* path = std::getenv (var))
  10978. {
  10979. std::ofstream out (path, std::ios::binary);
  10980. out.write (reinterpret_cast<const char*> (blob.data()), (std::streamsize) blob.size());
  10981. }
  10982. }
  10983. } // namespace
  10984. TEST (dx10_engine)
  10985. {
  10986. // REA_rodada42: a Fruity DX10 state written from scratch (version 1: 22 raw parameters) against our reference model
  10987. // (dxmodel.py + dxproc.py, bit-identical to the installation's DLL), the channel processor, host levels and the
  10988. // instrument front-end
  10989. const std::int32_t P[22] = { 1000, 40000, 30000, 30000, 40000, 30000, 35000, 8000, 40000, 50000, 20000,
  10990. 25000, 15000, 30000, 20000, 10000, 20000, 30000, 5000, 30000, 20000, -1 };
  10991. std::vector<std::uint8_t> blob (4 + 22 * 4, 0);
  10992. blob[0] = 1;
  10993. std::memcpy (blob.data() + 4, P, sizeof (P));
  10994. CHECK (lynx::dx10StateUsable (blob) && lynx::dx10ProcessorNeutral (blob));
  10995. CHECK (lynx::dx10Param (blob, 21) == -1 && lynx::dx10Param (blob, 3) == 30000 && ! lynx::dx10Param (blob, 22));
  10996. CHECK (! lynx::dx10StateUsable (std::vector<std::uint8_t> (40, 0)));
  10997. {
  10998. auto bad = blob;
  10999. bad[0] = 3; // versions ≥ 3 are ignored by the plugin
  11000. CHECK (! lynx::dx10StateUsable (bad));
  11001. }
  11002. using N = lynx::dx10::RefNote;
  11003. 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 } };
  11004. struct Golden { std::vector<std::uint8_t> state; lynx::dx10::RefOptions opt; int rate; double el, er; std::tuple<int, float, float> at[6]; };
  11005. auto check = [&] (const Golden& g) {
  11006. const auto y = lynx::dx10::renderReference (g.state, notes, (double) g.rate, 16384, g.opt);
  11007. CHECK (y.size() == 2 * 16384);
  11008. if (y.size() != 2 * 16384)
  11009. return y;
  11010. double el = 0.0, er = 0.0;
  11011. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  11012. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  11013. CHECK (std::abs (el - g.el) <= 1e-7 * g.el && std::abs (er - g.er) <= 1e-7 * g.er);
  11014. for (const auto& [i, l, r] : g.at)
  11015. CHECK (std::abs (y[2 * (std::size_t) i] - l) <= 1e-7f * std::max (1e-16f, std::abs (l))
  11016. && std::abs (y[2 * (std::size_t) i + 1] - r) <= 1e-7f * std::max (1e-16f, std::abs (r)));
  11017. return y;
  11018. };
  11019. lynx::dx10::RefOptions plain, odd;
  11020. odd.block = 333, odd.maxPoly = 1;
  11021. check ({ blob, plain, 44100, 672.293115, 778.393071,
  11022. { { 100, -0.0943799689f, -0.154014125f }, { 1000, -0.0814286768f, -0.132879525f }, { 2500, 0.499493301f, 0.312498778f },
  11023. { 5000, -0.184045464f, 0.171641544f }, { 9000, -0.153369367f, -0.20805186f }, { 16383, -0.00441304967f, 0.200126961f } } });
  11024. // 48 kHz, odd host blocks, max polyphony 1 (the last note steals); the hard-right note keeps FL's x87 left gain
  11025. check ({ blob, odd, 48000, 122.232761, 493.320297,
  11026. { { 100, 0.0236493405f, 0.0385922194f }, { 1000, 0.189806491f, 0.309736073f }, { 2500, -0.425576478f, -0.176279545f },
  11027. { 5000, -3.54030076e-18f, -0.0578194074f }, { 9000, 1.37052479e-17f, 0.223831087f }, { 16383, 9.248742e-18f, 0.151048407f } } });
  11028. // the channel processor (§3): our own settings snapshot — pan LFO (triangle, delayed, faded in), volume envelope
  11029. // with tensions and LFO, cutoff envelope + square LFO, resonance LFO, pitch envelope, SVF LP ×2 (type 7)
  11030. auto record = [] (std::vector<std::uint8_t>& s, int k, std::initializer_list<std::pair<int, int>> fields) {
  11031. const std::int32_t neutral[17] = { 0, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, 0 };
  11032. std::memcpy (s.data() + 0x44 * k, neutral, sizeof (neutral));
  11033. for (const auto& [i, v] : fields)
  11034. std::memcpy (s.data() + 0x44 * k + 4 * i, &v, 4);
  11035. };
  11036. auto settings = [] (std::vector<std::uint8_t>& s, std::int32_t modX, std::int32_t modY, std::int32_t type) {
  11037. std::memcpy (s.data() + 0x15c, &modX, 4);
  11038. std::memcpy (s.data() + 0x160, &modY, 4);
  11039. std::memcpy (s.data() + 0x168, &type, 4);
  11040. };
  11041. std::vector<std::uint8_t> snap (364, 0);
  11042. record (snap, 0, { { 9, 5000 }, { 10, 9000 }, { 11, -40 }, { 12, 15000 }, { 13, 1 } });
  11043. 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 } });
  11044. record (snap, 2, { { 1, 1 }, { 3, 6000 }, { 4, 100 }, { 5, 16000 }, { 6, 20 }, { 8, -60 }, { 11, 20 }, { 12, 18000 }, { 13, 2 } });
  11045. record (snap, 3, { { 11, 40 }, { 12, 17000 } });
  11046. record (snap, 4, { { 1, 1 }, { 3, 5000 }, { 4, 100 }, { 5, 14000 }, { 6, 30 }, { 8, -700 } });
  11047. settings (snap, 140, 120, 7);
  11048. const auto withSnap = lynx::dx10StateWithSnapshot (blob, snap);
  11049. CHECK (withSnap.size() == 460 && withSnap[0] == 2 && ! lynx::dx10ProcessorNeutral (withSnap));
  11050. CHECK (lynx::dx10StateWithSnapshot (withSnap, std::vector<std::uint8_t> (364, 0)) == withSnap); // a v2 state keeps its own
  11051. lynx::dx10::RefOptions ticks;
  11052. ticks.tick = 230, ticks.ppq = 96, ticks.tempo = 120.0;
  11053. check ({ withSnap, ticks, 44100, 346.852062, 554.699375,
  11054. { { 100, -0.00162654708f, -0.00265428401f }, { 1000, 0.0310285408f, 0.0506339073f }, { 2500, -0.118068494f, -0.0915558636f },
  11055. { 5000, -0.297166437f, -0.256465197f }, { 9000, 0.199509233f, 0.166108996f }, { 16383, -0.00392446062f, 0.0410436988f } } });
  11056. // a volume envelope decaying to zero: the 10 ms end fade, then every voice is gone (silence from 14950 on)
  11057. std::vector<std::uint8_t> decay (364, 0);
  11058. for (int k = 0; k < 5; ++k)
  11059. record (decay, k, {});
  11060. record (decay, 1, { { 0, 4 }, { 1, 1 }, { 3, 9000 }, { 4, 100 }, { 5, 12000 }, { 6, 0 }, { 15, 50 }, { 16, -101 } });
  11061. settings (decay, 256, 0, 0);
  11062. auto ticks48 = ticks;
  11063. ticks48.block = 333;
  11064. const auto y = check ({ lynx::dx10StateWithSnapshot (blob, decay), ticks48, 48000, 338.361707, 350.520208,
  11065. { { 100, 0.000514116196f, 0.000838961045f }, { 1000, 0.0412623473f, 0.0673340634f }, { 2500, -0.141532451f, -0.156581759f },
  11066. { 5000, 0.382550895f, 0.23462519f }, { 7000, -0.189829409f, 0.144132584f }, { 9000, -0.13638559f, 0.142912149f } } });
  11067. bool silentTail = y.size() == 2 * 16384 && (y[2 * 14949] != 0.0f || y[2 * 14949 + 1] != 0.0f);
  11068. for (std::size_t i = 2 * 14950; i < y.size(); ++i)
  11069. silentTail = silentTail && y[i] == 0.0f;
  11070. CHECK (silentTail);
  11071. // the instrument: LYNX velocity v → Init.Vol = (11^(1.28·v) − 1)/10 and Final.Vol = channel volume × Init.Vol; channel
  11072. // gains √2·V·(cos, sin)((1 + p)π/4) → V, p; notes on block boundaries play like the reference renderer (neutral
  11073. // channel settings: the instrument's sequencer ticks change nothing; vibrato off, since the plugin-wide vibrato
  11074. // oscillator is stepped by each voice in turn, so its interleaving follows the host block layout, as in FL)
  11075. auto noVib = blob;
  11076. CHECK (lynx::dx10SetParam (noVib, 10, 0));
  11077. auto initVol = [] (double v) { return (double) (float) ((std::pow (11.0, 1.28 * v) - 1.0) / 10.0); }; // Init.Vol is a float
  11078. lynx::ChannelDef c;
  11079. c.instrument = lynx::InstrumentKind::dx10;
  11080. c.pluginState = noVib;
  11081. auto inst = lynx::makeInstrument (lynx::InstrumentKind::dx10);
  11082. inst->configure (c, 120.0);
  11083. inst->prepare (44100.0);
  11084. const float V = 0.6f, pan = 0.25f;
  11085. const auto gl = (float) (std::sqrt (2.0) * V * std::cos ((1.0 + pan) * 3.141592653589793 / 4.0));
  11086. const auto gr = (float) (std::sqrt (2.0) * V * std::sin ((1.0 + pan) * 3.141592653589793 / 4.0));
  11087. std::vector<float> l (16384), r (16384);
  11088. for (int s = 0; s < 16384; s += 512)
  11089. {
  11090. if (s == 0)
  11091. inst->noteOn (60, 0.78125f);
  11092. if (s == 2048)
  11093. {
  11094. inst->setNextNoteExpression (-0.5f, 25.0f);
  11095. inst->noteOn (67, 0.5f);
  11096. }
  11097. if (s == 6144)
  11098. inst->noteOff (60);
  11099. if (s == 8192)
  11100. inst->noteOff (67);
  11101. inst->render (l.data() + s, r.data() + s, 512, gl, gr);
  11102. }
  11103. // the channel level reaches the engine as FL's (recovered to float precision)
  11104. const auto Vr = std::sqrt (((double) gl * gl + (double) gr * gr) * 0.5);
  11105. const auto pr = std::atan2 ((double) gr, (double) gl) * (4.0 / 3.141592653589793) - 1.0;
  11106. const auto ref = lynx::dx10::renderReference (noVib, { { 0, 60, initVol (0.78125), Vr * initVol (0.78125), pr, 0.0, 6144 },
  11107. { 2048, 67, initVol (0.5), Vr * initVol (0.5), std::clamp (-0.5 + pr, -1.0, 1.0), 25.0, 8192 } },
  11108. 44100.0, 16384);
  11109. double worst = 0.0, peak = 0.0;
  11110. for (int i = 0; i < 16384; ++i)
  11111. 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]) }),
  11112. peak = std::max (peak, (double) std::abs (l[(std::size_t) i]));
  11113. CHECK (worst == 0.0 && peak > 0.05);
  11114. // the released voices end (DX10's own release) and the instrument goes idle
  11115. std::vector<float> tail (44100), tailR (tail.size());
  11116. for (int k = 0; k < 20 && ! inst->idle(); ++k)
  11117. inst->render (tail.data(), tailR.data(), (int) tail.size(), gl, gr);
  11118. CHECK (inst->idle());
  11119. // with the channel settings above the instrument follows FL's ticks: the cutoff envelope darkens the note
  11120. {
  11121. lynx::ChannelDef cs = c;
  11122. cs.pluginState = withSnap;
  11123. cs.modelParams = { 96.0f };
  11124. auto si = lynx::makeInstrument (lynx::InstrumentKind::dx10);
  11125. si->configure (cs, 120.0);
  11126. si->prepare (44100.0);
  11127. std::vector<float> sl (32768), sr (32768);
  11128. si->noteOn (48, 0.78125f);
  11129. for (int s = 0; s < 32768; s += 512)
  11130. si->render (sl.data() + s, sr.data() + s, 512, 1.0f, 1.0f);
  11131. double e1 = 0.0, e2 = 0.0;
  11132. bool finite = true;
  11133. for (int i = 0; i < 32768; ++i)
  11134. finite = finite && std::isfinite (sl[(std::size_t) i]), (i < 4096 ? e1 : e2) += (double) sl[(std::size_t) i] * sl[(std::size_t) i];
  11135. CHECK (finite && e1 > 1e-3 && e2 > 1e-3);
  11136. }
  11137. // parameter edits (the channel settings panel) write the raw integers, clamped to the knob ranges
  11138. auto edited = blob;
  11139. CHECK (lynx::dx10SetParam (edited, 21, 7) && lynx::dx10Param (edited, 21) == 3);
  11140. CHECK (lynx::dx10SetParam (edited, 12, 70000) && lynx::dx10Param (edited, 12) == 65536);
  11141. }
  11142. TEST (threeosc_engine)
  11143. {
  11144. // REA_rodada47: a 3x Osc state written from scratch (version 14) against our reference model (osc3model.py,
  11145. // bit-identical to the installation's DLL), the plugin voice processor with its release path, the instrument
  11146. std::int32_t P[21] = { -20, 3, 0, 7, 10, -15, 64, 30, 2, -12, 0, 0, 20, 40, 0, 4, 12, -30, -8, 0, 16 };
  11147. auto make = [] (const std::int32_t* p, std::array<std::uint8_t, 3> inv, std::uint8_t am, const std::vector<std::uint8_t>& snap) {
  11148. std::vector<std::uint8_t> st (0x60, 0);
  11149. const std::int32_t version = 14;
  11150. std::memcpy (st.data(), &version, 4);
  11151. std::memcpy (st.data() + 4, p, 21 * 4);
  11152. st[0x58] = inv[0], st[0x59] = inv[1], st[0x5a] = inv[2], st[0x5b] = am;
  11153. st.insert (st.end(), snap.begin(), snap.end());
  11154. return st;
  11155. };
  11156. auto record = [] (std::vector<std::uint8_t>& s, int k, std::initializer_list<std::pair<int, int>> fields) {
  11157. const std::int32_t neutral[17] = { 0, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, 0 };
  11158. std::memcpy (s.data() + 0x44 * k, neutral, sizeof (neutral));
  11159. for (const auto& [i, v] : fields)
  11160. std::memcpy (s.data() + 0x44 * k + 4 * i, &v, 4);
  11161. };
  11162. auto settings = [] (std::vector<std::uint8_t>& s, std::int32_t modX, std::int32_t modY, std::int32_t type) {
  11163. std::memcpy (s.data() + 0x15c, &modX, 4);
  11164. std::memcpy (s.data() + 0x160, &modY, 4);
  11165. std::memcpy (s.data() + 0x168, &type, 4);
  11166. };
  11167. std::vector<std::uint8_t> neutral (364, 0);
  11168. for (int k = 0; k < 5; ++k)
  11169. record (neutral, k, k == 1 ? std::initializer_list<std::pair<int, int>> { { 0, 4 }, { 16, -101 } } : std::initializer_list<std::pair<int, int>> {});
  11170. settings (neutral, 256, 0, 0);
  11171. const auto blob = make (P, { 0, 0, 0 }, 0, neutral);
  11172. CHECK (lynx::threeOscStateUsable (blob) && lynx::threeOscNormalisedState (blob, {}) == blob);
  11173. CHECK (lynx::threeOscParam (blob, 9) == -12 && lynx::threeOscParam (blob, 20) == 16 && lynx::threeOscParam (blob, 24) == 0 && ! lynx::threeOscParam (blob, 26));
  11174. // older versions are normalised like the plugin reads them: version 12 (no snapshot → the plugin's defaults),
  11175. // version 10 (20 parameters, no invert/AM bytes)
  11176. {
  11177. std::vector<std::uint8_t> v12 (blob.begin(), blob.begin() + 92);
  11178. v12[0] = 12;
  11179. v12[0x58] = 1;
  11180. const auto n12 = lynx::threeOscNormalisedState (v12, {});
  11181. CHECK (n12.size() == 460 && n12[0] == 14 && lynx::threeOscParam (n12, 21) == 1 && lynx::threeOscParam (n12, 20) == 16
  11182. && std::equal (n12.begin() + 0x60, n12.end(), neutral.begin()));
  11183. std::vector<std::uint8_t> v10 (blob.begin(), blob.begin() + 84);
  11184. v10[0] = 10;
  11185. const auto n10 = lynx::threeOscNormalisedState (v10, {});
  11186. CHECK (lynx::threeOscParam (n10, 20) == 0 && lynx::threeOscParam (n10, 13) == 40 && lynx::threeOscParam (n10, 21) == 0);
  11187. }
  11188. using N = lynx::threeosc::RefNote;
  11189. const std::vector<N> notes { { 0, 60, 0.8, 0.3, 0.0, 6144 }, { 2048, 67, 1.0, -0.5, 25.0, 8192 }, { 3000, 40, 0.6, 1.0, 0.0, -1 } };
  11190. struct Golden { std::vector<std::uint8_t> state; lynx::threeosc::RefOptions opt; int rate; double el, er; std::tuple<int, float, float> at[6]; };
  11191. auto check = [&] (const Golden& g) {
  11192. const auto y = lynx::threeosc::renderReference (g.state, notes, (double) g.rate, 16384, g.opt);
  11193. CHECK (y.size() == 2 * 16384);
  11194. if (y.size() != 2 * 16384)
  11195. return y;
  11196. double el = 0.0, er = 0.0;
  11197. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  11198. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  11199. CHECK (std::abs (el - g.el) <= 1e-7 * g.el && std::abs (er - g.er) <= 1e-7 * g.er);
  11200. for (const auto& [i, l, r] : g.at)
  11201. CHECK (std::abs (y[2 * (std::size_t) i] - l) <= 1e-7f * std::max (1e-16f, std::abs (l))
  11202. && std::abs (y[2 * (std::size_t) i + 1] - r) <= 1e-7f * std::max (1e-16f, std::abs (r)));
  11203. return y;
  11204. };
  11205. // saw / square / triangle-ish mix with pans, stereo detune and phase, random start phases (seed 12345)
  11206. lynx::threeosc::RefOptions plain;
  11207. plain.seed = 12345;
  11208. check ({ blob, plain, 44100, 681.6491972612481, 1509.7245664533937,
  11209. { { 100, 0.0674247071146965f, -0.13440212607383728f }, { 1000, 0.12890009582042694f, 0.446039617061615f },
  11210. { 2500, -0.007686644792556763f, 0.3574123680591583f }, { 5000, 0.08792756497859955f, 0.4948147237300873f },
  11211. { 9000, 1.2136793475613885e-17f, 0.3116455078125f }, { 16383, 5.7305530145831346e-18f, 0.4134995639324188f } } });
  11212. // AM (osc 3 noise modulating osc 1 + 2), inverted oscillators, 48 kHz, odd blocks, max polyphony 1
  11213. const std::int32_t Pam[21] = { 0, 2, 0, 0, 0, 0, 30, 0, 3, 7, 0, 0, 0, 77, 0, 5, -12, 0, 0, 0, 0 };
  11214. lynx::threeosc::RefOptions odd;
  11215. odd.block = 333, odd.maxPoly = 1, odd.seed = 777;
  11216. check ({ make (Pam, { 1, 0, 1 }, 1, neutral), odd, 48000, 453.27241060751771, 1996.4548848690094,
  11217. { { 100, -0.2589033246040344f, -0.310184121131897f }, { 1000, 0.22021546959877014f, 0.3591070771217346f },
  11218. { 2500, -0.5378497242927551f, -0.15602676570415497f }, { 5000, 1.2423200626807308e-17f, 0.3293072581291199f },
  11219. { 9000, 2.5857780099332177e-17f, 0.42474493384361267f }, { 16383, 2.941168384618868e-17f, 0.3569665551185608f } } });
  11220. // the channel processor with its release path: pan LFO, volume envelope released from its current value, cutoff
  11221. // envelope + square LFO, resonance LFO, pitch envelope, SVF LP ×2 (type 7)
  11222. std::vector<std::uint8_t> snap (364, 0);
  11223. record (snap, 0, { { 9, 5000 }, { 10, 9000 }, { 11, -40 }, { 12, 15000 }, { 13, 1 } });
  11224. record (snap, 1, { { 0, 4 }, { 1, 1 }, { 3, 12000 }, { 4, 3000 }, { 5, 15000 }, { 6, 40 }, { 7, 20000 }, { 11, 30 }, { 12, 20000 }, { 14, 60 }, { 15, -90 }, { 16, 50 } });
  11225. record (snap, 2, { { 1, 1 }, { 3, 6000 }, { 4, 100 }, { 5, 16000 }, { 6, 20 }, { 7, 9000 }, { 8, -60 }, { 11, 20 }, { 12, 18000 }, { 13, 2 } });
  11226. record (snap, 3, { { 11, 40 }, { 12, 17000 } });
  11227. record (snap, 4, { { 1, 1 }, { 3, 5000 }, { 4, 100 }, { 5, 14000 }, { 6, 30 }, { 7, 12000 }, { 8, -700 } });
  11228. settings (snap, 140, 120, 7);
  11229. lynx::threeosc::RefOptions ticks;
  11230. ticks.tick = 230, ticks.ppq = 96, ticks.tempo = 120.0, ticks.seed = 99;
  11231. check ({ make (P, { 0, 0, 0 }, 0, snap), ticks, 44100, 977.9928196811321, 2072.965349921462,
  11232. { { 100, -0.001351095037534833f, 0.0029758031014353037f }, { 1000, 0.002833257894963026f, 0.10575854033231735f },
  11233. { 2500, 0.020360738039016724f, 0.3181821405887604f }, { 5000, 0.3340669274330139f, 0.4912331700325012f },
  11234. { 9000, 0.26940545439720154f, 0.27708977460861206f }, { 16383, 0.16570518910884857f, -0.10794822871685028f } } });
  11235. auto ticks48 = ticks;
  11236. ticks48.block = 333, ticks48.tick = 250, ticks48.seed = 5;
  11237. // (at 48 kHz the library's rate ratio 44100/fs scales the filter coefficients and the ramp limit)
  11238. check ({ make (P, { 0, 1, 0 }, 0, snap), ticks48, 48000, 864.9859578029492, 1895.6281818565287,
  11239. { { 100, 0.0031638722866773605f, -0.001987025374546647f }, { 1000, 0.049239177256822586f, 0.08794696629047394f },
  11240. { 2500, 0.13807585835456848f, 0.3554498553276062f }, { 5000, -0.08793184161186218f, -0.1596519947052002f },
  11241. { 9000, 0.08728396147489548f, 0.540290117263794f }, { 16383, 0.039887525141239166f, 0.21270982921123505f } } });
  11242. // HQ oscillators (engine_x64.dll: band-limited tables built with an FFT; FL's single-precision FFT and LYNX's double
  11243. // one agree to float rounding, so the golden values hold to 1e-5)
  11244. {
  11245. const std::int32_t Phq[21] = { 0, 3, 0, 0, 0, 0, 64, 20, 2, -12, 0, 0, 0, 40, -20, 4, 7, 0, 0, 0, 0 };
  11246. auto hq = make (Phq, { 0, 0, 0 }, 0, neutral);
  11247. hq[0x5c] = 1;
  11248. CHECK (lynx::threeOscParam (hq, 25) == 1);
  11249. lynx::threeosc::RefOptions o;
  11250. o.seed = 3;
  11251. const auto y = lynx::threeosc::renderReference (hq, notes, 44100.0, 16384, o);
  11252. CHECK (y.size() == 2 * 16384);
  11253. if (y.size() == 2 * 16384)
  11254. {
  11255. double el = 0.0, er = 0.0;
  11256. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  11257. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  11258. CHECK (std::abs (el - 862.7912087773848) < 1e-5 * 862.8 && std::abs (er - 1232.8046929286115) < 1e-5 * 1232.8);
  11259. const std::tuple<int, float, float> at[] = { { 100, -0.019788391888141632f, -0.19209949672222137f }, { 1000, 0.12019827216863632f, 0.03852222114801407f },
  11260. { 2500, 0.3239150643348694f, 0.07325324416160583f }, { 5000, -0.2809634208679199f, -0.5693862438201904f },
  11261. { 16383, -1.1937656360586644e-17f, -0.061853960156440735f } };
  11262. for (const auto& [i, l, r] : at)
  11263. CHECK (std::abs (y[2 * (std::size_t) i] - l) < 1e-5f && std::abs (y[2 * (std::size_t) i + 1] - r) < 1e-5f);
  11264. }
  11265. }
  11266. // the instrument: Final.Vol = channel volume × Init.Vol, channel gains → FL's volume/pan; notes on block boundaries
  11267. // play like the reference renderer (seed 0, neutral channel settings: ticks change nothing)
  11268. auto initVol = [] (double v) { return (double) (float) ((std::pow (11.0, 1.28 * v) - 1.0) / 10.0); };
  11269. lynx::ChannelDef c;
  11270. c.instrument = lynx::InstrumentKind::threeosc;
  11271. c.pluginState = blob;
  11272. auto inst = lynx::makeInstrument (lynx::InstrumentKind::threeosc);
  11273. inst->configure (c, 120.0);
  11274. inst->prepare (44100.0);
  11275. const float V = 0.6f, pan = 0.25f;
  11276. const auto gl = (float) (std::sqrt (2.0) * V * std::cos ((1.0 + pan) * 3.141592653589793 / 4.0));
  11277. const auto gr = (float) (std::sqrt (2.0) * V * std::sin ((1.0 + pan) * 3.141592653589793 / 4.0));
  11278. std::vector<float> l (16384), r (16384);
  11279. for (int s = 0; s < 16384; s += 512)
  11280. {
  11281. if (s == 0)
  11282. inst->noteOn (60, 0.78125f);
  11283. if (s == 2048)
  11284. {
  11285. inst->setNextNoteExpression (-0.5f, 25.0f);
  11286. inst->noteOn (67, 0.5f);
  11287. }
  11288. if (s == 6144)
  11289. inst->noteOff (60);
  11290. if (s == 8192)
  11291. inst->noteOff (67);
  11292. inst->render (l.data() + s, r.data() + s, 512, gl, gr);
  11293. }
  11294. const auto Vr = std::sqrt (((double) gl * gl + (double) gr * gr) * 0.5);
  11295. const auto pr = std::atan2 ((double) gr, (double) gl) * (4.0 / 3.141592653589793) - 1.0;
  11296. const auto ref = lynx::threeosc::renderReference (blob, { { 0, 60, Vr * initVol (0.78125), pr, 0.0, 6144 },
  11297. { 2048, 67, Vr * initVol (0.5), std::clamp (-0.5 + pr, -1.0, 1.0), 25.0, 8192 } },
  11298. 44100.0, 16384);
  11299. double worst = 0.0, peak = 0.0;
  11300. for (int i = 0; i < 16384; ++i)
  11301. 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]) }),
  11302. peak = std::max (peak, (double) std::abs (l[(std::size_t) i]));
  11303. CHECK (worst == 0.0 && peak > 0.05);
  11304. // released notes end with the processor's 10 ms fade (volume envelope off) and the instrument goes idle
  11305. CHECK (inst->idle());
  11306. // parameter edits write the raw integers, clamped to the knob ranges
  11307. auto edited = blob;
  11308. CHECK (lynx::threeOscSetParam (edited, 9, -40) && lynx::threeOscParam (edited, 9) == -24);
  11309. CHECK (lynx::threeOscSetParam (edited, 13, 500) && lynx::threeOscParam (edited, 13) == 128);
  11310. CHECK (lynx::threeOscSetParam (edited, 24, 1) && edited[0x5b] == 1);
  11311. CHECK (! lynx::threeOscStateUsable (std::vector<std::uint8_t> (2, 0)));
  11312. }
  11313. TEST (gms_engine)
  11314. {
  11315. const auto blob = gmsTestState();
  11316. dumpIfAsked ("LYNX_DUMP_GMS", blob);
  11317. CHECK (lynx::gmsStateUsable (blob));
  11318. CHECK (lynx::gmsWaveNames (blob) == std::vector<std::string> ({ "NoWave A", "NoWave B", "NoWave C" }));
  11319. CHECK (lynx::gmsParamOffset (blob, 12) == 8 + 4 + 7 * 512 + 12 + 4 * 12 && lynx::gmsParamOffset (blob, 212) < 0);
  11320. auto broken = blob;
  11321. broken[8] = 'X';
  11322. CHECK (! lynx::gmsStateUsable (broken));
  11323. CHECK (! lynx::gmsStateUsable (std::vector<std::uint8_t> (blob.begin(), blob.begin() + 2000)));
  11324. // golden samples of our round-39 model (gmsmodel.GmsPlugin, waves missing → silent tables, tempo 140, 44.1 kHz,
  11325. // host blocks of 512): C5 vel 100 0–30000, E5 vel 80 11025–40000, G5 vel 110 22050–52000 (Init.Vol law A)
  11326. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  11327. const std::vector<lynx::gms::RefNote> notes = { { 0, 60, (float) iv (100), 1.0, 0.0, 0.0, 30000 },
  11328. { 11025, 64, (float) iv (80), 1.0, 0.0, 0.0, 40000 },
  11329. { 22050, 67, (float) iv (110), 1.0, 0.0, 0.0, 52000 } };
  11330. const auto y = lynx::gms::renderReference (blob, notes, 44100.0, 66150, "");
  11331. CHECK (y.size() == 2 * 66150u);
  11332. 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 } };
  11333. for (const auto& g : golden)
  11334. 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);
  11335. double e = 0.0;
  11336. for (auto v : y)
  11337. e += (double) v * v;
  11338. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.0790270452795) < 1e-6);
  11339. // the instrument: same engine behind LYNX's channel gains (V = 1, centre → law A), velocity → Init.Vol
  11340. lynx::ChannelDef c;
  11341. c.instrument = lynx::InstrumentKind::gms;
  11342. c.pluginState = blob;
  11343. auto inst = lynx::makeInstrument (lynx::InstrumentKind::gms);
  11344. inst->configure (c, 140.0);
  11345. inst->prepare (44100.0);
  11346. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  11347. for (int b = 0; b < 16; ++b)
  11348. {
  11349. if (b == 0)
  11350. inst->noteOn (60, 0.78125f);
  11351. if (b == 10)
  11352. inst->noteOff (60);
  11353. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  11354. }
  11355. 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, "");
  11356. double diff = 0.0, energy = 0.0;
  11357. for (int i = 0; i < 8192; ++i)
  11358. {
  11359. 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]) });
  11360. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  11361. }
  11362. CHECK (diff == 0.0 && energy > 1e-3);
  11363. }
  11364. // A Toxic Biohazard state written from scratch (REA_rodada45; r45/py/mktest.py builds the same bytes): our own drawn
  11365. // spectra on operators 1 and 3 (wave 0) and the sine on operator 2, so no wave of the installation is needed
  11366. std::vector<std::uint8_t> toxicTestState()
  11367. {
  11368. auto s = lynx::toxicDefaultState();
  11369. static const std::pair<int, double> set[] = {
  11370. { 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 },
  11371. { 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 },
  11372. { 24, 0.6 }, { 25, 1 }, { 26, 1 }, { 27, 1 }, { 32, 0.5606 }, { 33, 0.4394 }, { 38, 0.53 }, { 49, 0 }, { 55, 0.25 },
  11373. { 62, 0.5 }, { 75, 0.08 }, { 81, 0.5 }, { 87, 0.3 }, { 104, 0.8 }, { 105, 0.6 }, { 110, 0.35 }, { 115, 0.22 },
  11374. { 127, 0.18 }, { 147, 0.7 }, { 227, 1 }, { 229, 0.65 }, { 233, 0.4 }, { 236, 1 }, { 237, 1 }, { 252, 0.5 }, { 254, 0.3 },
  11375. { 256, 0.4 }, { 267, 0.4 }, { 269, 0.5 }, { 277, 0.3 }, { 300, 1 }, { 301, 8 }, { 302, 2 }, { 303, 0 }, { 305, 3 },
  11376. { 306, 0 }, { 307, 1 }, { 308, 0 }, { 314, 1 }, { 315, 0 }, { 316, 1 }, { 324, 106 }, { 332, 0.7 } };
  11377. for (const auto& [index, value] : set)
  11378. {
  11379. const auto v = (float) value;
  11380. std::memcpy (s.data() + lynx::toxicParamOffset (s, index), &v, 4);
  11381. }
  11382. auto spectrum = [&] (int slot, int h, float amp, float phase) {
  11383. const auto at = 12 + 0x40 + 1024 * (std::size_t) slot + 4 * (std::size_t) h;
  11384. std::memcpy (s.data() + at, &amp, 4);
  11385. std::memcpy (s.data() + at + 512, &phase, 4);
  11386. };
  11387. for (int h = 0; h < 24; ++h)
  11388. spectrum (0, h, (float) (1.0 / (h + 1)), 0.0f);
  11389. for (int k = 0; k < 16; ++k)
  11390. spectrum (2, 2 * k, (float) (1.0 / (2 * k + 1)), 0.25f);
  11391. return s;
  11392. }
  11393. TEST (toxic_engine)
  11394. {
  11395. const auto blob = toxicTestState();
  11396. dumpIfAsked ("LYNX_DUMP_TOXIC", blob);
  11397. CHECK (blob.size() == 10050u && lynx::toxicStateUsable (blob) && ! lynx::toxicNeedsDll (blob));
  11398. CHECK (lynx::toxicParamOffset (blob, 15) == 12 + 0x40 + 8192 + 4 * 15 && lynx::toxicParamOffset (blob, 340) < 0);
  11399. CHECK (! lynx::toxicDllUsable ("") && ! lynx::toxicDllUsable ("/nonexistent/Toxic Biohazard_x64.dll"));
  11400. auto broken = blob;
  11401. broken[8] = 'X';
  11402. CHECK (! lynx::toxicStateUsable (broken));
  11403. CHECK (! lynx::toxicStateUsable (std::vector<std::uint8_t> (blob.begin(), blob.begin() + 5000)));
  11404. auto needs = blob;
  11405. const auto w = 15.0f; // a built-in wave of the installation: without the DLL the engine refuses the state
  11406. std::memcpy (needs.data() + lynx::toxicParamOffset (needs, 307), &w, 4);
  11407. CHECK (lynx::toxicNeedsDll (needs) && lynx::toxic::renderReference (needs, { { 0, 60 } }, 44100.0, 64, "").empty());
  11408. // golden samples of our round-45 model (toxmodel.run, which equals the DLL bit for bit on this state), effects on,
  11409. // tempo 140, 44.1 kHz, host blocks of 512: C5 vel 100 0–30000, E5 vel 80 11025–40000, G5 vel 110 22050–52000
  11410. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  11411. const std::vector<lynx::toxic::RefNote> notes = { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 },
  11412. { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  11413. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 } };
  11414. const auto y = lynx::toxic::renderReference (blob, notes, 44100.0, 66150, "", 512, 140.0);
  11415. CHECK (y.size() == 2 * 66150u);
  11416. 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 } };
  11417. for (const auto& g : golden)
  11418. 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);
  11419. double e = 0.0;
  11420. for (auto v : y)
  11421. e += (double) v * v;
  11422. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.307227390984) < 1e-6);
  11423. // the instrument: same engine behind LYNX's channel gains (V = 1, centre → law A), velocity → Init.Vol
  11424. lynx::ChannelDef c;
  11425. c.instrument = lynx::InstrumentKind::toxic;
  11426. c.pluginState = blob;
  11427. auto inst = lynx::makeInstrument (lynx::InstrumentKind::toxic);
  11428. inst->configure (c, 140.0);
  11429. inst->prepare (44100.0);
  11430. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  11431. for (int b = 0; b < 16; ++b)
  11432. {
  11433. if (b == 0)
  11434. inst->noteOn (60, 0.78125f);
  11435. if (b == 10)
  11436. inst->noteOff (60);
  11437. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  11438. }
  11439. 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 } },
  11440. 44100.0, 8192, "", 512, 140.0);
  11441. double diff = 0.0, energy = 0.0;
  11442. for (int i = 0; i < 8192; ++i)
  11443. {
  11444. 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]) });
  11445. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  11446. }
  11447. CHECK (diff == 0.0 && energy > 1e-3);
  11448. }
  11449. TEST (morphine_engine)
  11450. {
  11451. const auto blob = morphineTestState();
  11452. dumpIfAsked ("LYNX_DUMP_MORPHINE", blob);
  11453. CHECK (lynx::morphineStateUsable (blob));
  11454. CHECK (lynx::morphineNoiseName (blob).empty());
  11455. const auto at = lynx::morphineParamOffset (blob, 9);
  11456. CHECK (at > 0);
  11457. float a9 = 0.0f;
  11458. std::memcpy (&a9, blob.data() + at, 4);
  11459. CHECK (a9 == 0.2f && lynx::morphineParamOffset (blob, 40) < 0);
  11460. auto broken = blob;
  11461. broken[12] = 'X'; // the MPR0 tag
  11462. CHECK (! lynx::morphineStateUsable (broken));
  11463. CHECK (! lynx::morphineStateUsable (std::vector<std::uint8_t> (blob.begin(), blob.end() - 10)));
  11464. // golden samples of our round-39 model (mormodel.Synth, tempo 120, 44.1 kHz, host blocks of 512): keys 60/64/67,
  11465. // Init.Vol 1.0/0.5/2.0, note-offs at 30000/50000/70000
  11466. const std::vector<lynx::morphine::RefNote> notes = { { 0, 60, 1.0, 1.0, 0.0, 0.0, 30000 },
  11467. { 11025, 64, 0.5, 1.0, 0.0, 0.0, 50000 },
  11468. { 22050, 67, 2.0, 1.0, 0.0, 0.0, 70000 } };
  11469. const auto y = lynx::morphine::renderReference (blob, notes, 44100.0, 80000, "");
  11470. CHECK (y.size() == 2 * 80000u);
  11471. 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 } };
  11472. for (const auto& g : golden)
  11473. 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);
  11474. double e = 0.0;
  11475. for (auto v : y)
  11476. e += (double) v * v;
  11477. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.18467099041) < 1e-6);
  11478. // the instrument: same engine behind LYNX's channel gains
  11479. lynx::ChannelDef c;
  11480. c.instrument = lynx::InstrumentKind::morphine;
  11481. c.pluginState = blob;
  11482. auto inst = lynx::makeInstrument (lynx::InstrumentKind::morphine);
  11483. inst->configure (c, 120.0);
  11484. inst->prepare (44100.0);
  11485. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  11486. for (int b = 0; b < 16; ++b)
  11487. {
  11488. if (b == 0)
  11489. inst->noteOn (60, 0.78125f);
  11490. if (b == 10)
  11491. inst->noteOff (60);
  11492. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  11493. }
  11494. 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, "");
  11495. double diff = 0.0, energy = 0.0;
  11496. for (int i = 0; i < 8192; ++i)
  11497. {
  11498. 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]) });
  11499. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  11500. }
  11501. CHECK (diff == 0.0 && energy > 1e-3);
  11502. }
  11503. namespace
  11504. {
  11505. // A MiniSynth state written from scratch (REA_rodada41 §1.1 layout: "SSLF" + the .flms preset)
  11506. std::vector<std::uint8_t> miniSynthTestState (const std::array<float, 43>& P)
  11507. {
  11508. const std::string name = "LYNX test";
  11509. Bytes body;
  11510. body.raw ("\x99" "FLS\r\n\x1a\n", 8);
  11511. body.raw ("HEAD", 4);
  11512. body.i32 (6 + 2 * (std::int32_t) name.size());
  11513. const std::uint16_t head[3] = { 1, 1, (std::uint16_t) name.size() };
  11514. body.raw (reinterpret_cast<const char*> (head), 6);
  11515. for (char ch : name)
  11516. body.raw (&ch, 1), body.zeros (1);
  11517. body.raw ("DATA", 4);
  11518. body.i32 (176);
  11519. body.raw ("0rev", 4);
  11520. for (auto v : P)
  11521. body.f32 (v);
  11522. Bytes s;
  11523. s.raw ("SSLF", 4);
  11524. s.i32 ((std::int32_t) body.b.size());
  11525. s.bytes (body.b);
  11526. return s.b;
  11527. }
  11528. std::array<float, 43> miniSynthDefaults()
  11529. {
  11530. 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,
  11531. 0.5f, 0, 0.25f, 0.5f, 0.5f, 0, 0.11222222f, 0.4f, 0.1f, 1, 0.5f };
  11532. }
  11533. // per-process suffix for scratch files: several test binaries (one per worktree) may run at the same time, and a
  11534. // fixed name let one process read a file another one was rewriting
  11535. std::string processTempTag()
  11536. {
  11537. static const std::string tag = std::to_string (std::random_device {}()) + "_"
  11538. + std::to_string (std::chrono::steady_clock::now().time_since_epoch().count());
  11539. static const struct Cleanup
  11540. {
  11541. ~Cleanup()
  11542. {
  11543. std::error_code ec;
  11544. std::filesystem::remove (std::filesystem::temp_directory_path (ec) / ("lynx_test_oscdata_" + tag + ".dat"), ec);
  11545. std::filesystem::remove_all (std::filesystem::temp_directory_path (ec) / ("lynx_bassdrum_test_" + tag), ec);
  11546. }
  11547. } cleanup;
  11548. (void) cleanup;
  11549. return tag;
  11550. }
  11551. // a synthetic oscdata.dat (amplitudes 0.3/k, phases −π/2 + 0.001·(k − 1)): the test does not need an FL installation
  11552. std::string miniSynthTestOscdata()
  11553. {
  11554. const auto path = (std::filesystem::temp_directory_path() / ("lynx_test_oscdata_" + processTempTag() + ".dat")).string();
  11555. std::vector<float> d (2048);
  11556. for (int h = 0; h < 1024; ++h)
  11557. d[(std::size_t) h] = (float) (0.3 / (h + 1)), d[1024 + (std::size_t) h] = (float) (-1.5707963267948966 + 0.001 * h);
  11558. std::ofstream out (path, std::ios::binary);
  11559. out.write (reinterpret_cast<const char*> (d.data()), 8192);
  11560. return path;
  11561. }
  11562. } // namespace
  11563. TEST (minisynth_engine)
  11564. {
  11565. auto A = miniSynthDefaults(), B = miniSynthDefaults();
  11566. // A: poly, SQR+SQR, noise, filter env/velocity/keytracking, LFO → cutoff, distortion + decimator, delay, chorus, phaser
  11567. 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 },
  11568. { 12, 0.1f }, { 13, 0.4f }, { 14, 0.3f }, { 15, 0.5f }, { 16, 0.8f }, { 17, 0.6f }, { 18, 0.1f },
  11569. { 19, 0.5f }, { 20, 0.85f }, { 21, 0.05f }, { 22, 0.5f }, { 23, 0.6f }, { 24, 0.4f }, { 25, 1 },
  11570. { 26, 0.3f }, { 27, 0.1f }, { 28, 1 }, { 29, 0.4f }, { 30, 0.5f }, { 31, 0.6f }, { 32, 0.6f },
  11571. { 33, 1 }, { 34, 0.4f }, { 35, 0.6f }, { 36, 0.5f }, { 37, 1 }, { 38, 0.3f }, { 39, 0.6f },
  11572. { 40, 0.5f }, { 41, 0.7f }, { 42, 0.4f } };
  11573. // B: lead (legato glide), BELL, LFO → pitch
  11574. 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 },
  11575. { 17, 0.1f }, { 18, 0.5f }, { 19, 0.6f }, { 20, 0.7f }, { 22, 0.6f }, { 23, 0.5f }, { 24, 0.3f } };
  11576. for (const auto& [i, v] : a)
  11577. A[(std::size_t) i] = v;
  11578. for (const auto& [i, v] : b)
  11579. B[(std::size_t) i] = v;
  11580. const auto blobA = miniSynthTestState (A), blobB = miniSynthTestState (B);
  11581. dumpIfAsked ("LYNX_DUMP_MINISYNTH", blobA);
  11582. CHECK (lynx::miniSynthStateUsable (blobA) && lynx::miniSynthParams (blobA).size() == 43u && lynx::miniSynthParams (blobA)[26] == 0.3f);
  11583. CHECK (lynx::miniSynthParamOffset (blobA, 9) == 8 + 8 + 8 + 6 + 18 + 12 + 4 * 9 && lynx::miniSynthParamOffset (blobA, 43) < 0);
  11584. CHECK (! lynx::miniSynthStateUsable (std::vector<std::uint8_t> (blobA.begin(), blobA.begin() + 20)));
  11585. auto shortData = blobA;
  11586. shortData.resize (shortData.size() - 4 * 40); // only 3 parameters present: the rest keeps the defaults
  11587. CHECK (lynx::miniSynthParams (shortData)[20] == 0.5f && lynx::miniSynthParams (shortData)[1] == 0.5f);
  11588. // golden samples of our round-41 model (msmodel.Plugin on the same synthetic oscdata, 44.1 kHz, host blocks of 512,
  11589. // L = R = Final.Vol): C5 vel 100 0–30000, E5 vel 80 11025–40000, G5 vel 110 22050–52000 (Init.Vol law A)
  11590. const auto osc = miniSynthTestOscdata();
  11591. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  11592. const std::vector<lynx::minisynth::RefNote> notes = { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 },
  11593. { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  11594. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 } };
  11595. struct Golden { int i; float l, r; };
  11596. 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 } };
  11597. 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 } };
  11598. for (int which = 0; which < 2; ++which)
  11599. {
  11600. const auto y = lynx::minisynth::renderReference (which == 0 ? blobA : blobB, notes, 44100.0, 66150, osc);
  11601. CHECK (y.size() == 2 * 66150u);
  11602. if (y.size() != 2 * 66150u)
  11603. continue;
  11604. for (const auto& g : which == 0 ? std::vector (std::begin (goldenA), std::end (goldenA)) : std::vector (std::begin (goldenB), std::end (goldenB)))
  11605. CHECK (y[2 * (std::size_t) g.i] == g.l && y[2 * (std::size_t) g.i + 1] == g.r);
  11606. double e = 0.0;
  11607. for (auto v : y)
  11608. e += (double) v * v;
  11609. CHECK (std::abs (std::sqrt (e / (double) y.size()) - (which == 0 ? 0.315552434392 : 0.03926116287)) < 1e-9);
  11610. }
  11611. // missing oscdata.dat: silent tables, still finite
  11612. const auto silent = lynx::minisynth::renderReference (blobB, notes, 44100.0, 4096, "");
  11613. CHECK (silent.size() == 2 * 4096u && std::all_of (silent.begin(), silent.end(), [] (float v) { return std::isfinite (v) && std::abs (v) < 1.0f; }));
  11614. // the instrument: same engine behind LYNX's channel gains (V = 1, centre → law A), velocity → Init.Vol
  11615. lynx::ChannelDef c;
  11616. c.instrument = lynx::InstrumentKind::minisynth;
  11617. c.pluginState = blobA;
  11618. c.samplePath = osc;
  11619. auto inst = lynx::makeInstrument (lynx::InstrumentKind::minisynth);
  11620. inst->configure (c, 120.0);
  11621. inst->prepare (44100.0);
  11622. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  11623. for (int k = 0; k < 16; ++k)
  11624. {
  11625. if (k == 0)
  11626. inst->noteOn (60, 0.78125f);
  11627. if (k == 10)
  11628. inst->noteOff (60);
  11629. inst->render (l.data() + 512 * k, r.data() + 512 * k, 512, 1.0f, 1.0f);
  11630. }
  11631. 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 } },
  11632. 44100.0, 8192, osc, 512, 120.0, 0, -1.0, true);
  11633. double diff = 0.0, energy = 0.0;
  11634. for (int i = 0; i < 8192; ++i)
  11635. {
  11636. 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]) });
  11637. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  11638. }
  11639. CHECK (diff == 0.0 && energy > 1e-3 && ! inst->idle());
  11640. }
  11641. namespace
  11642. {
  11643. // A PoiZone state written from scratch (REA_rodada44 §1.3 layout, "Ver4" + 156 floats + name): pulse A synced to saw B,
  11644. // noise, envelope to the cutoff, triangle LFO to oscillator A's pitch, 3-voice unison, gate, chorus and tape delay.
  11645. std::vector<std::uint8_t> poizoneTestState()
  11646. {
  11647. std::array<float, 156> P {};
  11648. 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 },
  11649. { 10, 0.8f }, { 11, 0.3f }, { 12, 0.5f }, { 13, 0.4f }, { 14, 0.5f }, { 15, 0.5f }, { 16, 0.5f }, { 17, 0.5f },
  11650. { 18, 0.35f }, { 19, 0.6f }, { 20, 0.5f }, { 21, 0.3f }, { 22, 0.2f }, { 23, 0.6f }, { 24, 0.7f }, { 25, 0.5f },
  11651. { 26, 0.7f }, { 28, 0.6f }, { 29, 0.3f }, { 30, 0.8f }, { 31, 1.0f }, { 32, 0.3f }, { 33, 0.5f }, { 34, 0.5f },
  11652. { 35, 0.3f }, { 37, 1.0f }, { 38, 0.6f }, { 39, 1.0f }, { 40, 0.4f }, { 41, 0.5f }, { 42, 0.5f }, { 43, 0.5f },
  11653. { 47, 0.5f }, { 48, 0.3f }, { 67, 0.2f }, { 68, 0.5f }, { 75, 2.0f }, { 76, 15.0f }, { 77, 1.0f }, { 78, 1.0f },
  11654. { 79, 1.0f } };
  11655. for (const auto& [i, v] : values)
  11656. P[(std::size_t) i] = v;
  11657. for (int i = 49; i < 65; ++i)
  11658. P[(std::size_t) i] = (i - 49) % 3 != 2 ? 1.0f : 0.0f;
  11659. for (int i = 81; i < 155; ++i)
  11660. P[(std::size_t) i] = -1.0f;
  11661. std::vector<std::uint8_t> blob { 'S', 'S', 'L', 'F', 0xb4, 0x02, 0x00, 0x00, '4', 'r', 'e', 'V' };
  11662. blob.resize (12 + 4 * 156 + 64, 0);
  11663. std::memcpy (blob.data() + 12, P.data(), 4 * 156);
  11664. std::memcpy (blob.data() + 12 + 4 * 156, "LYNX test", 9);
  11665. return blob;
  11666. }
  11667. } // namespace
  11668. TEST (poizone_engine)
  11669. {
  11670. const auto blob = poizoneTestState();
  11671. CHECK (lynx::poizoneStateUsable (blob));
  11672. CHECK (lynx::poizoneParamOffset (blob, 18) == 12 + 4 * 18 && lynx::poizoneParamOffset (blob, 156) < 0);
  11673. CHECK (lynx::poizoneParam (blob, 75) == 2.0f && ! lynx::poizoneParam (blob, -1));
  11674. auto broken = blob;
  11675. broken[8] = 'X'; // the "Ver4" tag
  11676. CHECK (! lynx::poizoneStateUsable (broken));
  11677. CHECK (! lynx::poizoneStateUsable (std::vector<std::uint8_t> (blob.begin(), blob.begin() + 10)));
  11678. // golden samples of our round-44 model (pzmodel.Plugin, tempo 120, 44.1 kHz, host blocks of 512, L = R = Final.Vol):
  11679. // C5 vel 100 0–30000, E5 vel 80 11025–40000, G5 vel 110 22050–52000 (Init.Vol = (11^(vel/100) − 1)/10)
  11680. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  11681. const std::vector<lynx::poizone::RefNote> notes = { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 },
  11682. { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  11683. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 } };
  11684. const auto y = lynx::poizone::renderReference (blob, notes, 44100.0, 66150);
  11685. CHECK (y.size() == 2 * 66150u);
  11686. 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 } };
  11687. for (const auto& g : golden)
  11688. 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);
  11689. double e = 0.0;
  11690. for (auto v : y)
  11691. e += (double) v * v;
  11692. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.10990940878400073) < 1e-7);
  11693. // the instrument: same engine behind LYNX's channel gains (V = 1, centre → law A), velocity → Init.Vol
  11694. lynx::ChannelDef c;
  11695. c.instrument = lynx::InstrumentKind::poizone;
  11696. c.pluginState = blob;
  11697. auto inst = lynx::makeInstrument (lynx::InstrumentKind::poizone);
  11698. inst->configure (c, 120.0);
  11699. inst->prepare (44100.0);
  11700. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  11701. for (int b = 0; b < 16; ++b)
  11702. {
  11703. if (b == 0)
  11704. inst->noteOn (60, 0.78125f);
  11705. if (b == 10)
  11706. inst->noteOff (60);
  11707. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  11708. }
  11709. 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,
  11710. 512, 120.0, 0, true);
  11711. double diff = 0.0, energy = 0.0;
  11712. for (int i = 0; i < 8192; ++i)
  11713. {
  11714. 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]) });
  11715. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  11716. }
  11717. CHECK (diff == 0.0 && energy > 1e-3);
  11718. }
  11719. // ---- Fruit Kick / BassDrum (REA_rodada43): synthetic states and data, golden values of our round-43 models ----
  11720. namespace
  11721. {
  11722. std::vector<std::uint8_t> fruitKickStateA()
  11723. {
  11724. const std::int32_t w[8] = { 0, 0, -600, 64, 128, 32, 20, 0 };
  11725. std::vector<std::uint8_t> b (32);
  11726. std::memcpy (b.data(), w, 32);
  11727. return b;
  11728. }
  11729. std::vector<std::uint8_t> fruitKickStateB()
  11730. {
  11731. std::vector<std::uint8_t> b (400, 0);
  11732. const std::int32_t head[7] = { 1, 624, -8, 42, 86, 64, 0 };
  11733. std::memcpy (b.data(), head, sizeof (head));
  11734. std::int32_t recs[5][17];
  11735. for (auto& r : recs)
  11736. {
  11737. const std::int32_t d[17] = { 0, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, 0 };
  11738. std::memcpy (r, d, sizeof (d));
  11739. }
  11740. const std::int32_t pan[17] = { 0, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 3000, 64, 20000, 1, 0, 0, 0 };
  11741. const std::int32_t vol[17] = { 4, -1, 100, 3000, 100, 25000, 64, 15000, 0, 100, 20000, 0, 32950, 0, 0, 40, -101 };
  11742. std::memcpy (recs[0], pan, sizeof (pan));
  11743. std::memcpy (recs[1], vol, sizeof (vol));
  11744. for (int k = 0; k < 5; ++k)
  11745. std::memcpy (b.data() + 32 + 68 * k, recs[k], 68);
  11746. const std::int32_t cut = 256;
  11747. std::memcpy (b.data() + 32 + 0x15c, &cut, 4);
  11748. return b;
  11749. }
  11750. std::string bassDrumTestFolder()
  11751. {
  11752. // our own oscillator table and a pseudo-random 16-bit noise sample, laid out like FL's BassDrum folder
  11753. const auto dir = std::filesystem::temp_directory_path() / ("lynx_bassdrum_test_" + processTempTag());
  11754. std::filesystem::create_directories (dir / "Noise Samples");
  11755. auto wav = [] (const std::filesystem::path& path, const void* data, std::uint32_t bytes, std::uint16_t fmt, std::uint16_t bits) {
  11756. std::ofstream o (path, std::ios::binary);
  11757. auto u32 = [&] (std::uint32_t v) { o.write ((const char*) &v, 4); };
  11758. auto u16 = [&] (std::uint16_t v) { o.write ((const char*) &v, 2); };
  11759. o.write ("RIFF", 4), u32 (36 + bytes), o.write ("WAVEfmt ", 8), u32 (16), u16 (fmt), u16 (1), u32 (44100),
  11760. u32 (44100u * bits / 8), u16 ((std::uint16_t) (bits / 8)), u16 (bits), o.write ("data", 4), u32 (bytes);
  11761. o.write ((const char*) data, bytes);
  11762. };
  11763. std::vector<float> table (1024);
  11764. for (int i = 0; i < 1024; ++i)
  11765. 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));
  11766. wav (dir / "sounddata", table.data(), 4096, 3, 32);
  11767. std::vector<std::int16_t> noise (3000);
  11768. std::uint32_t s = 12345;
  11769. for (auto& v : noise)
  11770. {
  11771. s = (s * 1103515245u + 12345u) & 0x7fffffffu;
  11772. v = (std::int16_t) ((int) ((s >> 8) % 20001u) - 10000);
  11773. }
  11774. wav (dir / "Noise Samples" / "test noise.wav", noise.data(), 6000, 1, 16);
  11775. return dir.string();
  11776. }
  11777. std::vector<std::uint8_t> bassDrumTestState()
  11778. {
  11779. std::vector<std::uint8_t> b (0x50C + 92, 0);
  11780. std::memcpy (b.data(), "SSLF", 4);
  11781. const std::uint32_t size = 0x560;
  11782. std::memcpy (b.data() + 4, &size, 4);
  11783. std::memcpy (b.data() + 8, "2kiK", 4);
  11784. std::memcpy (b.data() + 12, "Test kick", 9);
  11785. const char path[] = "C:/Somewhere/Noise Samples/test noise.wav";
  11786. std::memcpy (b.data() + 268, path, sizeof (path) - 1);
  11787. 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,
  11788. 0.5f, 0.8f, 0.15f, 0.9f, 0.3f, 0.5f, 0.6f, 0.62f, 0.4f, 0.5f, 0.85f };
  11789. std::memcpy (b.data() + 0x50C, P, sizeof (P));
  11790. return b;
  11791. }
  11792. struct Golden
  11793. {
  11794. int i;
  11795. float l, r;
  11796. };
  11797. bool matchesGolden (const std::vector<float>& y, std::initializer_list<Golden> golden, double rms)
  11798. {
  11799. bool ok = true;
  11800. for (const auto& g : golden)
  11801. 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;
  11802. double e = 0.0;
  11803. for (auto v : y)
  11804. e += (double) v * v;
  11805. return ok && std::abs (std::sqrt (e / (double) y.size()) - rms) < 1e-6;
  11806. }
  11807. } // namespace
  11808. TEST (fruitkick_engine)
  11809. {
  11810. const auto A = fruitKickStateA(), B = fruitKickStateB();
  11811. CHECK (lynx::fruitKickStateUsable (A) && lynx::fruitKickStateUsable (B));
  11812. CHECK (! lynx::fruitKickStateUsable (std::vector<std::uint8_t> (A.begin(), A.begin() + 20)));
  11813. CHECK (lynx::fruitKickParam (A, 1) == -600 && lynx::fruitKickParam (B, 0) == 624);
  11814. CHECK (lynx::fruitKickFallbacks (A).empty() && lynx::fruitKickFallbacks (B).empty());
  11815. // a version-0 state takes the channel's envelope records as its snapshot (and becomes version 1)
  11816. std::vector<std::vector<std::uint8_t>> recs;
  11817. for (int k = 0; k < 5; ++k)
  11818. recs.emplace_back (B.begin() + 32 + 68 * k, B.begin() + 32 + 68 * (k + 1));
  11819. const auto withEnv = lynx::fruitKickWithChannelEnvelope (A, recs, 256, 0, 0);
  11820. CHECK (withEnv.size() == 400 && lynx::fruitKickParam (withEnv, 5) == 20 && std::memcmp (withEnv.data() + 32, B.data() + 32, 364) == 0);
  11821. CHECK (lynx::fruitKickWithChannelEnvelope (B, recs, 256, 0, 0) == B);
  11822. // golden samples of our round-43 model (fkmodel.run, 44.1 kHz, host blocks of 512): C5 at 0 held, C6 at 3000
  11823. // (Final.Vol 0.5, pan 0.3) released at 9000; B with the plugin's tick every 230 samples from 115
  11824. 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 } };
  11825. const auto ya = lynx::fruitkick::renderReference (A, notes, 44100.0, 20000);
  11826. CHECK (ya.size() == 40000u);
  11827. // (A uses the distortion: values since REA_rodada55 with FL's x87 live host waveshaper, checked against FLEngine's own;
  11828. // the release fade since REA_rodada74 with IPP's cosine, checked against the DLL)
  11829. CHECK (matchesGolden (ya, { { 10, 0.803601742f, 0.803601742f }, { 700, 0.185467482f, 0.185467482f }, { 2900, -0.63492614f, -0.63492614f },
  11830. { 3100, 0.339092523f, 0.544403195f }, { 5000, -0.0974622667f, -0.150664076f }, { 9100, 0.124622017f, 0.0544089526f },
  11831. { 9400, -0.146487325f, -0.144867823f }, { 15000, 0.009439799f, 0.009439799f } },
  11832. 0.284267665614));
  11833. std::vector<int> ticks;
  11834. for (int t = 115; t < 20000; t += 230)
  11835. ticks.push_back (t);
  11836. const auto yb = lynx::fruitkick::renderReference (B, notes, 44100.0, 20000, 512, ticks);
  11837. CHECK (matchesGolden (yb, { { 10, -0.00760388654f, -0.00760388654f }, { 700, 0.386696398f, 0.442232877f }, { 2900, -0.123097703f, -0.302172035f },
  11838. { 3100, 0.0523624197f, 0.0877688825f }, { 5000, 0.045177035f, 0.100505695f }, { 9100, -0.00423920294f, 0.00810315367f },
  11839. { 9400, 0.00482427329f, -0.010199016f }, { 15000, -0.000171193431f, 1.01026744e-05f } },
  11840. 0.117964927585));
  11841. // the instrument: same engine behind LYNX's channel gains (V = 1, centre), velocity → Init.Vol (link velocity on)
  11842. lynx::ChannelDef c;
  11843. c.instrument = lynx::InstrumentKind::fruitkick;
  11844. c.pluginState = A;
  11845. auto inst = lynx::makeInstrument (lynx::InstrumentKind::fruitkick);
  11846. inst->configure (c, 140.0);
  11847. inst->prepare (44100.0);
  11848. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  11849. for (int b = 0; b < 16; ++b)
  11850. {
  11851. if (b == 0)
  11852. inst->noteOn (60, 0.78125f);
  11853. if (b == 10)
  11854. inst->noteOff (60);
  11855. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  11856. }
  11857. const auto iv = (std::pow (11.0, 1.28 * 0.78125) - 1.0) / 10.0;
  11858. const auto ref = lynx::fruitkick::renderReference (A, { { 0, 60, iv, 0.0, 0.0, 5120 } }, 44100.0, 8192);
  11859. double diff = 0.0, energy = 0.0;
  11860. for (int i = 0; i < 8192; ++i)
  11861. {
  11862. 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]) });
  11863. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  11864. }
  11865. CHECK (diff < 1e-6 && energy > 1e-2);
  11866. }
  11867. TEST (ipp_cos_and_end_fade)
  11868. {
  11869. // ippsCos_32f_A11 of the installation's dsp_ippv2_x64.dll (REA_rodada74 §1): outputs the DLL gave on its two paths
  11870. const auto bits = [] (float v) { return ippcos::floatToBits (v); };
  11871. const auto f = ippcos::bitsToFloat;
  11872. CHECK (bits (ippcos::cosFma (0.1f)) == 0x3f7eaf8bu && bits (ippcos::cosFma (1.0f)) == 0x3f0a534cu);
  11873. CHECK (bits (ippcos::cosFma (2.0f)) == 0xbed51118u && bits (ippcos::cosFma (-3.0f)) == 0xbf7d6646u);
  11874. CHECK (bits (ippcos::cosFma (f (0x3e000007))) == 0x3f7df6f9u && bits (ippcos::cosNoFma (f (0x3e000007))) == 0x3f7df6f8u);
  11875. CHECK (bits (ippcos::cosFma (f (0x3e00007c))) == 0x3f7df6f4u && bits (ippcos::cosNoFma (f (0x3e00007c))) == 0x3f7df6f5u);
  11876. CHECK (ippcos::cosFma (1e-9f) == 1.0f && ippcos::cosNoFma (0.0f) == 1.0f);
  11877. // the portable fused multiply-add rounds once: (1 + 2^-12)^2 = 1 + 2^-11 + 2^-24 is a float halfway case that a
  11878. // tiny addend (lost in a plain double sum) must decide
  11879. const float a = 1.0f + 0x1p-12f;
  11880. CHECK (ippcos::fusedMulAdd (a, a, 0x1p-60f) == 1.0f + 0x1p-11f + 0x1p-23f);
  11881. CHECK (ippcos::fusedMulAdd (a, a, -0x1p-60f) == 1.0f + 0x1p-11f && ippcos::fusedMulAdd (a, a, 0.0f) == 1.0f + 0x1p-11f);
  11882. CHECK (ippcos::fusedMulAdd (3.0f, 5.0f, -1.0f) == 14.0f);
  11883. // the end-fade table: within 1.5e-4 of the exact cosine, ends near zero
  11884. std::vector<float> t (441);
  11885. ippcos::endFadeTable (t.data(), 441);
  11886. double worst = 0.0;
  11887. for (int i = 0; i < 441; ++i)
  11888. worst = std::max (worst, std::abs ((double) t[(std::size_t) i] - 0.5 * (1.0 + std::cos ((i + 1) * 3.141592653589793 / 442.0))));
  11889. CHECK (worst < 1.5e-4 && t[0] > 0.9999f && t[440] < 1e-4f);
  11890. }
  11891. TEST (bassdrum_engine)
  11892. {
  11893. // FL's CRT powf as the engine computes it: a case where its last bit differs from the correctly rounded power
  11894. const float x = 0.87011606f;
  11895. std::uint32_t bits = 0;
  11896. const auto p = lynx::bassdrum::powfMsvc (x, 0.125f);
  11897. std::memcpy (&bits, &p, 4);
  11898. CHECK (bits == 1065063964u);
  11899. CHECK (lynx::bassdrum::powfMsvc (2.0f, 1.0f) == 2.0f && lynx::bassdrum::powfMsvc (10.0f, 0.0f) == 1.0f);
  11900. const auto st = bassDrumTestState();
  11901. CHECK (lynx::bassDrumStateUsable (st) && lynx::bassDrumNoiseName (st) == "test noise.wav");
  11902. CHECK (lynx::bassDrumParamOffset (st, 6) == 0x50C + 24 && lynx::bassDrumParamOffset (st, 23) < 0);
  11903. auto broken = st;
  11904. broken[8] = 'X';
  11905. CHECK (! lynx::bassDrumStateUsable (broken));
  11906. // golden samples of our round-43 model (bdmodel.run with its powf port, 44.1 kHz, host blocks of 512)
  11907. const auto dir = bassDrumTestFolder();
  11908. 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 },
  11909. { 6100, 55, 1.5, 1.0, -0.3, 0.0, -1 } };
  11910. const auto y = lynx::bassdrum::renderReference (st, notes, 44100.0, 30000, dir);
  11911. CHECK (y.size() == 60000u);
  11912. CHECK (matchesGolden (y, { { 50, 0.0122119058f, 0.0122119058f }, { 400, -0.0886279643f, -0.0886279643f }, { 3000, -0.0428197235f, -0.0428197235f },
  11913. { 6050, 0.0199260078f, 0.0240648985f }, { 6300, -0.257363439f, -0.185516387f }, { 9000, 0.268954486f, 0.10506025f },
  11914. { 15000, -0.0102827176f, -0.0380715057f }, { 25000, 0.0f, 0.0f } },
  11915. 0.230007588169));
  11916. CHECK (lynx::bassdrum::renderReference (st, notes, 44100.0, 100, dir + "/nowhere").empty()); // no table: no engine
  11917. lynx::ChannelDef c;
  11918. c.instrument = lynx::InstrumentKind::bassdrum;
  11919. c.pluginState = st;
  11920. c.samplePath = dir;
  11921. auto inst = lynx::makeInstrument (lynx::InstrumentKind::bassdrum);
  11922. inst->configure (c, 120.0);
  11923. inst->prepare (44100.0);
  11924. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  11925. for (int b = 0; b < 16; ++b)
  11926. {
  11927. if (b == 0)
  11928. inst->noteOn (60, 0.78125f);
  11929. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  11930. }
  11931. 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);
  11932. double diff = 0.0, energy = 0.0;
  11933. for (int i = 0; i < 8192; ++i)
  11934. {
  11935. 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]) });
  11936. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  11937. }
  11938. CHECK (diff == 0.0 && energy > 1e-2);
  11939. }
  11940. // REA_rodada55: Fruity Fast Dist, Fruity Flangus and Fruity Flanger, goldens from the installation's DLLs (EXEC, our own states)
  11941. TEST (fruity_fast_dist_flangus_flanger)
  11942. {
  11943. std::vector<float> inL (4096), inR (4096);
  11944. for (int n = 0; n < 4096; ++n)
  11945. {
  11946. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  11947. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  11948. }
  11949. struct Gold { int i; float l, r; };
  11950. auto close = [] (float a, float b) { return std::abs (a - b) <= 2.0e-7f * std::max (1.0f, std::abs (b)); };
  11951. // Fast Dist (+ FL's host waveshaper): live and render kernels
  11952. auto dist = [&] (lynx::fastdistfx::State s, bool render, std::initializer_list<Gold> gold) {
  11953. std::vector<float> l (inL), r (inR);
  11954. lynx::fastdistfx::process (l.data(), 4096, s, render);
  11955. lynx::fastdistfx::process (r.data(), 4096, s, render);
  11956. for (const auto& g : gold)
  11957. CHECK (close (l[(std::size_t) g.i], g.l) && close (r[(std::size_t) g.i], g.r));
  11958. };
  11959. dist ({ 128, 10, 0, 128, 128 }, false, { { 100, 0.0f, -0.39352417f }, { 1000, -0.39352417f, 0.389404297f }, { 2047, 0.376953125f, -0.39352417f },
  11960. { 3000, -0.39352417f, -0.379760742f }, { 4095, 0.342590332f, 0.373931885f } });
  11961. dist ({ 128, 10, 0, 128, 128 }, true, { { 3000, -0.39352417f, -0.379751563f }, { 4095, 0.342590332f, 0.373931885f } });
  11962. dist ({ 192, 4, 1, 77, 97 }, false, { { 100, 0.0f, -0.868428111f }, { 1000, -1.23000622f, 0.740812242f }, { 2047, 0.58129251f, -0.855666518f },
  11963. { 3000, -1.23000622f, -0.613196433f }, { 4095, 0.307311207f, 0.549388587f } });
  11964. dist ({ 192, 4, 1, 77, 97 }, true, { { 2047, 0.58129245f, -0.855666518f }, { 4095, 0.307311267f, 0.549388528f } });
  11965. dist ({ 90, 7, 0, 40, 120 }, false, { { 100, 0.0f, -0.346225947f }, { 1000, -0.511893928f, 0.287246376f }, { 2047, 0.212910682f, -0.340344131f },
  11966. { 3000, -0.511893928f, -0.227848753f }, { 4095, 0.0910475403f, 0.197926477f } });
  11967. dist ({ 90, 7, 0, 40, 120 }, true, { { 100, 0.0f, -0.346223623f }, { 1000, -0.511893749f, 0.287246734f }, { 2047, 0.212909341f, -0.340341568f },
  11968. { 3000, -0.511893749f, -0.227846757f }, { 4095, 0.0910428166f, 0.197928891f } });
  11969. {
  11970. const auto* T = lynx::fastdistfx::curve (0, 10);
  11971. CHECK (T[0] == 0 && T[8191] == T[8192] && lynx::fastdistfx::curve (1, 1)[8191] == 32767);
  11972. }
  11973. // Flangus: FL's call order (state, sample rate, block size of 512), no flush
  11974. auto flange = [&] (lynx::flangusfx::State s, int fs, std::initializer_list<Gold> gold) {
  11975. lynx::flangusfx::Engine e;
  11976. e.prepare (fs);
  11977. e.load (s);
  11978. e.setRate (fs);
  11979. e.setBlockSize (512);
  11980. std::vector<float> io (8192);
  11981. for (int n = 0; n < 4096; ++n)
  11982. {
  11983. io[2 * (std::size_t) n] = inL[(std::size_t) n];
  11984. io[2 * (std::size_t) n + 1] = inR[(std::size_t) n];
  11985. }
  11986. for (int o = 0; o < 4096; o += 512)
  11987. e.render (io.data() + 2 * o, io.data() + 2 * o, 512);
  11988. for (const auto& g : gold)
  11989. CHECK (close (io[2 * (std::size_t) g.i], g.l) && close (io[2 * (std::size_t) g.i + 1], g.r));
  11990. };
  11991. flange ({ 4, 512, 768, 8, 128, -32, 96, 96 }, 44100, { { 100, 0.0249633491f, -0.119510099f }, { 1000, -1.26988697f, 0.687134743f },
  11992. { 2047, 1.03176403f, -0.278752804f }, { 3000, -0.940228283f, -0.853936791f },
  11993. { 4095, 0.201686561f, 0.607052445f } });
  11994. flange ({ 7, 300, 900, 40, 30, 100, -60, 128 }, 44100, { { 100, 0.0f, 0.0446268953f }, { 1000, 1.68992829f, 2.06982398f },
  11995. { 2047, 0.400214791f, 0.838502884f }, { 3000, 1.20018816f, 1.18875635f },
  11996. { 4095, -0.354708612f, 0.503573656f } });
  11997. flange ({ 2, 1024, 200, 0, 0, -128, 128, -90 }, 48000, { { 100, 0.172592998f, -0.230506495f }, { 1000, -1.56807125f, 0.854493499f },
  11998. { 2047, 1.02325416f, -1.17055321f }, { 3000, -1.74136555f, -0.535462856f },
  11999. { 4095, 1.49345946f, -0.537866116f } });
  12000. // Flanger: state, then the sample rate (FL's call order)
  12001. auto flanger = [&] (lynx::flangerfx::State s, int fs, std::initializer_list<Gold> gold) {
  12002. lynx::flangerfx::Engine e;
  12003. e.load (s);
  12004. e.prepare (fs);
  12005. std::vector<float> io (8192);
  12006. for (int n = 0; n < 4096; ++n)
  12007. {
  12008. io[2 * (std::size_t) n] = inL[(std::size_t) n];
  12009. io[2 * (std::size_t) n + 1] = inR[(std::size_t) n];
  12010. }
  12011. for (int o = 0; o < 4096; o += 512)
  12012. e.render (io.data() + 2 * o, io.data() + 2 * o, 512);
  12013. for (const auto& g : gold)
  12014. CHECK (close (io[2 * (std::size_t) g.i], g.l) && close (io[2 * (std::size_t) g.i + 1], g.r));
  12015. };
  12016. flanger ({ 0, 2000, 2000, 118, 0, 0, 23, 0, 1024, 3500, 3000, 2500 }, 44100,
  12017. { { 100, -1.22955823f, -1.98144293f }, { 1000, -0.975861192f, 0.646231353f }, { 2047, 0.518505156f, -0.857361794f },
  12018. { 3000, -0.546559215f, -0.0157452822f }, { 4095, 0.104127884f, 0.206255436f } });
  12019. flanger ({ 300, 4000, 4500, 90, 1000, 300, 99, 1024, 1024, 2000, 4000, 1000 }, 48000,
  12020. { { 100, 0.0f, -0.440000027f }, { 1000, 0.24619779f, 0.775473535f }, { 2047, 1.11609459f, -0.0639692694f },
  12021. { 3000, -1.22629714f, -1.41118503f }, { 4095, -0.263562649f, 0.80683732f } });
  12022. // import: FL's states become the exact effects with the controls as parameters; short states keep the approximations
  12023. {
  12024. const std::vector<std::uint8_t> fd { 192, 0, 0, 0, 4, 0, 0, 0, 1, 0, 0, 0, 77, 0, 0, 0, 97, 0, 0, 0 };
  12025. const auto slot = lynx::effectFromFl ("Fruity Fast Dist", fd, 120.0);
  12026. CHECK (slot && slot->kind == lynx::EffectKind::fruityFastDist);
  12027. if (slot)
  12028. {
  12029. const auto s = lynx::fastdistfx::fromParams (slot->params.data());
  12030. CHECK (s.pre == 192 && s.threshold == 4 && s.type == 1 && s.mix == 77 && s.post == 97);
  12031. auto fx = lynx::makeEffect (*slot);
  12032. fx->prepare (44100.0);
  12033. std::vector<float> l (inL), r (inR);
  12034. fx->process (l.data(), r.data(), 4096, slot->params, 120.0);
  12035. CHECK (l[1000] == -1.23000622f && r[4095] == 0.549388587f);
  12036. }
  12037. std::vector<std::uint8_t> fg (32);
  12038. const int v[8] { 7, 300, 900, 40, 30, 100, -60, 128 };
  12039. std::memcpy (fg.data(), v, 32);
  12040. const auto f2 = lynx::effectFromFl ("Fruity Flangus", fg, 120.0);
  12041. CHECK (f2 && f2->kind == lynx::EffectKind::fruityFlangus);
  12042. if (f2)
  12043. {
  12044. const auto s = lynx::flangusfx::fromParams (f2->params.data());
  12045. for (int i = 0; i < 8; ++i)
  12046. CHECK (s[(std::size_t) i] == v[i]);
  12047. auto fx = lynx::makeEffect (*f2);
  12048. fx->prepare (48000.0);
  12049. std::vector<float> l (inL), r (inR);
  12050. fx->process (l.data(), r.data(), 4096, f2->params, 120.0);
  12051. CHECK (std::isfinite (l[4095]) && std::isfinite (r[4095]) && std::abs (l[2000]) > 0.0f);
  12052. }
  12053. std::vector<std::uint8_t> fl (52);
  12054. const int w[13] { 1, 300, 4000, 4500, 90, 1000, 300, 99, 1024, 1024, 2000, 4000, 1000 };
  12055. std::memcpy (fl.data(), w, 52);
  12056. const auto f3 = lynx::effectFromFl ("Fruity Flanger", fl, 120.0);
  12057. CHECK (f3 && f3->kind == lynx::EffectKind::fruityFlanger);
  12058. if (f3)
  12059. {
  12060. const auto s = lynx::flangerfx::fromParams (f3->params.data());
  12061. for (int i = 0; i < 12; ++i)
  12062. CHECK (s[(std::size_t) i] == w[i + 1]);
  12063. auto fx = lynx::makeEffect (*f3);
  12064. fx->prepare (48000.0);
  12065. std::vector<float> l (inL), r (inR);
  12066. fx->process (l.data(), r.data(), 4096, f3->params, 120.0);
  12067. CHECK (l[1000] == 0.24619779f && r[4095] == 0.80683732f);
  12068. }
  12069. const auto shortFd = lynx::effectFromFl ("Fruity Fast Dist", std::vector<std::uint8_t> (8, 0), 120.0);
  12070. CHECK (! shortFd || shortFd->kind != lynx::EffectKind::fruityFastDist);
  12071. }
  12072. }
  12073. // Workflow P2 (docs/WORKFLOW_FL.md)
  12074. #include "WorkflowP2Tests.inc"
  12075. // Editor mouse grammar (Piano Roll / Playlist)
  12076. #include "EditorGestureTests.inc"
  12077. TEST (chorus_family_engines)
  12078. {
  12079. // REA_rodada59: goldens from the installation's DLLs (44.1 kHz, blocks of 512) on this signal
  12080. std::vector<float> inL (4096), inR (4096);
  12081. for (int n = 0; n < 4096; ++n)
  12082. {
  12083. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0) * 0.5f;
  12084. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0) * 0.5f;
  12085. }
  12086. struct Gold { int i; float l, r; };
  12087. auto close = [] (float a, float b) { return std::abs (a - b) <= 2.0e-7f * std::max (1.0f, std::abs (b)); };
  12088. auto run = [&] (const char* plugin, const std::vector<std::uint8_t>& st, lynx::EffectKind kind, std::initializer_list<Gold> gold) {
  12089. const auto slot = lynx::effectFromFl (plugin, st, 120.0);
  12090. CHECK (slot && slot->kind == kind);
  12091. if (! slot)
  12092. return;
  12093. auto fx = lynx::makeEffect (*slot);
  12094. fx->prepare (44100.0);
  12095. std::vector<float> l (inL), r (inR);
  12096. fx->process (l.data(), r.data(), 1024, slot->params, 120.0);
  12097. fx->process (l.data() + 1024, r.data() + 1024, 3072, slot->params, 120.0);
  12098. for (const auto& g : gold)
  12099. CHECK (close (l[(std::size_t) g.i], g.l) && close (r[(std::size_t) g.i], g.r));
  12100. };
  12101. auto ints = [] (std::initializer_list<int> v) {
  12102. std::vector<std::uint8_t> b;
  12103. for (int x : v)
  12104. for (int k = 0; k < 4; ++k)
  12105. b.push_back ((std::uint8_t) ((unsigned) x >> (8 * k)));
  12106. return b;
  12107. };
  12108. // Fruity Chorus (start phases 0, which FL draws at random)
  12109. run ("Fruity Chorus", ints ({ 1, 512, 2250, 341, 1500, 2500, 3500, 23, 0, 0, 0, 2500, 0 }), lynx::EffectKind::fruityChorus,
  12110. { { 100, 0.0f, -0.3850000202655792f }, { 1000, -0.6181581020355225f, 0.2707735002040863f }, { 2047, -0.01424609124660492f, 0.24228274822235107f },
  12111. { 3000, -1.1650776863098145f, -0.1809898018836975f }, { 4095, -0.012790083885192871f, 0.060571759939193726f } });
  12112. run ("Fruity Chorus", ints ({ 1, 700, 4000, 900, 4000, 3000, 2000, 400, 700, 1000, 30, 4999, 0 }), lynx::EffectKind::fruityChorus,
  12113. { { 100, 0.0f, -0.3850000202655792f }, { 1000, -0.5833333134651184f, 0.36345374584198f }, { 2047, -0.0336097776889801f, -0.3360772430896759f },
  12114. { 3000, -0.40196430683135986f, -0.5073210597038269f }, { 4095, -0.1176539734005928f, 0.444706529378891f } });
  12115. run ("Fruity Chorus", ints ({ 1, 36, 469, 1004, 2832, 293, 3500, 0, 428, 0, 0, 3887, 1024 }), lynx::EffectKind::fruityChorus,
  12116. { { 100, 0.2975943684577942f, 0.3747217655181885f }, { 1000, -0.39463338255882263f, 0.18099871277809143f }, { 2047, 0.3894335627555847f, 0.35111793875694275f },
  12117. { 3000, -0.1450725942850113f, 0.2847272455692291f }, { 4095, 0.33678582310676575f, -0.13365517556667328f } });
  12118. {
  12119. const auto slot = lynx::effectFromFl ("Fruity Chorus", ints ({ 1, 36, 469, 1004, 2832, 293, 3500, 0, 428, 0, 0, 3887, 1024 }), 120.0);
  12120. CHECK (slot && lynx::fruitychorusfx::fromParams (slot->params.data())[2] == 1004);
  12121. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityChorus, 11, 1.0f, 120.0) == "on");
  12122. CHECK (lynx::effectParamFromFl ("Fruity Chorus", 11).has_value() && ! lynx::effectParamFromFl ("Fruity Chorus", 12).has_value());
  12123. }
  12124. // a state FL does not read (version 2) or too short keeps the generic chorus
  12125. CHECK (! lynx::effectFromFl ("Fruity Chorus", ints ({ 2, 512, 2250, 341, 1500, 2500, 3500, 23, 0, 0, 0, 2500, 0 }), 120.0).has_value()
  12126. || lynx::effectFromFl ("Fruity Chorus", ints ({ 2, 512, 2250, 341, 1500, 2500, 3500, 23, 0, 0, 0, 2500, 0 }), 120.0)->kind != lynx::EffectKind::fruityChorus);
  12127. CHECK (! lynx::effectFromFl ("Fruity Chorus", { 1, 0, 0, 0 }, 120.0).has_value()
  12128. || lynx::effectFromFl ("Fruity Chorus", { 1, 0, 0, 0 }, 120.0)->kind != lynx::EffectKind::fruityChorus);
  12129. // Hyper Chorus engine in FL's load sequence (defaults, then the state: the parameters glide), instance k's phases
  12130. auto hyper = [&] (std::initializer_list<int> raws, int instance, std::initializer_list<Gold> gold) {
  12131. namespace hc = lynx::hyperchorusfx;
  12132. hc::Engine e (instance);
  12133. const std::vector<int> r (raws);
  12134. 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); };
  12135. for (int i = 0; i < hc::params; ++i)
  12136. e.setParam (i, val (i, hc::defaults[(std::size_t) i]));
  12137. for (int i = 0; i < hc::params; ++i)
  12138. e.setParam (i, val (i, r[(std::size_t) i]));
  12139. e.setRate (44100.0);
  12140. std::vector<float> io (8192);
  12141. for (int n = 0; n < 4096; ++n)
  12142. io[2 * (std::size_t) n] = inL[(std::size_t) n], io[2 * (std::size_t) n + 1] = inR[(std::size_t) n];
  12143. for (int s = 0; s < 4096; s += 512)
  12144. e.render (io.data() + 2 * s, io.data() + 2 * s, 512);
  12145. for (const auto& g : gold)
  12146. CHECK (close (io[2 * (std::size_t) g.i], g.l) && close (io[2 * (std::size_t) g.i + 1], g.r));
  12147. };
  12148. hyper ({ 562, 919, 499, 505, 709, 716, 1, 19247, 10900, 191, 1000 }, 0,
  12149. { { 100, 0.0f, -0.4115639328956604f }, { 1000, -0.658551037311554f, 0.28647756576538086f }, { 2047, 0.24743860960006714f, -0.5088979005813599f },
  12150. { 3000, -0.5171564221382141f, -0.3630276620388031f }, { 4095, 0.08128821104764938f, 0.18811003863811493f } });
  12151. hyper ({ 1000, 555, 0, 400, 300, 300, 0, 22000, 120, 368, 750 }, 2,
  12152. { { 100, 0.0f, -0.398922324180603f }, { 1000, -1.1528236865997314f, 0.2839684784412384f }, { 2047, 0.5115746855735779f, -0.3330395221710205f },
  12153. { 3000, -0.6094785928726196f, 0.17040175199508667f }, { 4095, -0.11666109412908554f, 0.21931695938110352f } });
  12154. CHECK (lynx::hyperchorusfx::randState (1) == 1u * 214013u + 2531011u);
  12155. {
  12156. // the import: controls as parameters; a song's Hyper Chorus slots are numbered in mixer order
  12157. const auto st = ints ({ 2, 1000, 555, 0, 400, 300, 300, 0, 22000, 120, 368, 750, 0 });
  12158. const auto slot = lynx::effectFromFl ("Hyper Chorus", st, 120.0);
  12159. CHECK (slot && slot->kind == lynx::EffectKind::hyperChorus && lynx::hyperchorusfx::fromParams (slot->params.data())[9] == 368);
  12160. lynx::Song song;
  12161. song.mixer.resize (3);
  12162. song.mixer[1].effects = { *slot, *slot };
  12163. song.mixer[2].effects = { *slot };
  12164. lynx::numberChorusInstances (song);
  12165. CHECK (lynx::hyperchorusfx::instanceOf (song.mixer[2].effects[0].state) == 2);
  12166. auto fx = lynx::makeEffect (song.mixer[2].effects[0]);
  12167. fx->prepare (48000.0);
  12168. std::vector<float> l (inL), r (inR);
  12169. fx->process (l.data(), r.data(), 4096, slot->params, 120.0);
  12170. bool finite = true, wet = false;
  12171. for (int n = 0; n < 4096; ++n)
  12172. {
  12173. finite = finite && std::isfinite (l[(std::size_t) n]) && std::isfinite (r[(std::size_t) n]);
  12174. wet = wet || l[(std::size_t) n] != inL[(std::size_t) n];
  12175. }
  12176. CHECK (finite && wet);
  12177. CHECK (lynx::formatEffectParam (lynx::EffectKind::hyperChorus, 8, 0.0f, 120.0) == "off");
  12178. CHECK (! lynx::effectFromFl ("Hyper Chorus", ints ({ 3, 1000, 555, 0, 400, 300, 300, 0, 22000, 120, 368, 750, 0 }), 120.0).has_value()
  12179. || lynx::effectFromFl ("Hyper Chorus", ints ({ 3, 1000, 555, 0, 400, 300, 300, 0, 22000, 120, 368, 750, 0 }), 120.0)->kind != lynx::EffectKind::hyperChorus);
  12180. }
  12181. // Vintage Chorus engine as FL drives it (created, sample rate, each control), fresh instance (its smoothed values
  12182. // start from the engine's initial ones)
  12183. auto vintage = [&] (std::initializer_list<int> raws, std::initializer_list<Gold> gold) {
  12184. namespace vc = lynx::vintagechorusfx;
  12185. vc::Engine e;
  12186. e.setRate (44100.0);
  12187. int i = 0;
  12188. for (int raw : raws)
  12189. {
  12190. 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));
  12191. ++i;
  12192. }
  12193. std::vector<float> io (8192);
  12194. for (int n = 0; n < 4096; ++n)
  12195. io[2 * (std::size_t) n] = inL[(std::size_t) n], io[2 * (std::size_t) n + 1] = inR[(std::size_t) n];
  12196. for (int s = 0; s < 4096; s += 512)
  12197. e.render (io.data() + 2 * s, io.data() + 2 * s, 512);
  12198. for (const auto& g : gold)
  12199. CHECK (close (io[2 * (std::size_t) g.i], g.l) && close (io[2 * (std::size_t) g.i + 1], g.r));
  12200. };
  12201. vintage ({ 2, 0, 211046, 1, 2411, 960, 1985, 5121, 0, 1, 4420, 6000, 273, 0, 0, 0, 0 },
  12202. { { 100, 0.0f, -0.05606573447585106f }, { 1000, -0.3413679003715515f, 0.20469821989536285f }, { 2047, 0.1487489938735962f, -0.28199973702430725f },
  12203. { 3000, -0.31699514389038086f, -0.43230289220809937f }, { 4095, 0.1255197525024414f, 0.0810704305768013f } });
  12204. vintage ({ 2, 0, 80, 0, 0, 60, 3000, 3000, 0, 0, 0, 3000, 0, 3000, 0, 0, 0 }, // hiss
  12205. { { 100, -1.2527654007499223e-06f, -0.053655341267585754f }, { 1000, -0.44306400418281555f, 0.25606146454811096f }, { 2047, 0.23425261676311493f, -0.21375307440757751f },
  12206. { 3000, -0.5793180465698242f, -0.13371579349040985f }, { 4095, 0.05595953390002251f, 0.11362095922231674f } });
  12207. vintage ({ 0, 1, 49152, 0, 3000, 60, 3000, 3000, 0, 0, 0, 3000, 0, 0, 0, 0, 0 }, // Chorus 1 + 2
  12208. { { 100, 0.0f, -0.05365874245762825f }, { 1000, -0.5095958113670349f, 0.2809423506259918f }, { 2047, 0.17256303131580353f, -0.3563305139541626f },
  12209. { 3000, -0.471851646900177f, -0.30123069882392883f }, { 4095, 0.0959278866648674f, 0.13436323404312134f } });
  12210. {
  12211. const auto st = ints ({ 1, 2, 0, 211046, 1, 2411, 960, 1985, 5121, 0, 1, 4420, 6000, 273, 0, 0, 0, 0 });
  12212. const auto slot = lynx::effectFromFl ("Vintage Chorus", st, 120.0);
  12213. CHECK (slot && slot->kind == lynx::EffectKind::vintageChorus && lynx::vintagechorusfx::fromParams (slot->params.data())[2] == 211046);
  12214. auto fx = lynx::makeEffect (*slot);
  12215. fx->prepare (44100.0);
  12216. std::vector<float> l (inL), r (inR);
  12217. fx->process (l.data(), r.data(), 4096, slot->params, 120.0);
  12218. bool finite = true;
  12219. for (int n = 0; n < 4096; ++n)
  12220. finite = finite && std::isfinite (l[(std::size_t) n]) && std::isfinite (r[(std::size_t) n]);
  12221. CHECK (finite && l[3000] != inL[3000]);
  12222. CHECK (lynx::formatEffectParam (lynx::EffectKind::vintageChorus, 0, 0.0f, 120.0) == "Chorus 1");
  12223. 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()
  12224. || 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);
  12225. }
  12226. }
  12227. #include "UiChromeTests.inc"
  12228. TEST (fruity_delay_engines)
  12229. {
  12230. // REA_rodada52: the engines on their own against the installation's DLLs (goldens = the DLL's output for the same
  12231. // input with FL's set-up order, EXEC: bit-identical), then the mixer effects built from FL states
  12232. const int n = 16384;
  12233. std::vector<float> inL ((std::size_t) n, 0.0f), inR ((std::size_t) n, 0.0f);
  12234. std::uint32_t seed = 1;
  12235. for (int i = 0; i < 4096; ++i) // a noise burst, then the echoes
  12236. {
  12237. seed = seed * 1664525u + 1013904223u;
  12238. inL[(std::size_t) i] = (float) ((int) (seed >> 16) - 32768) / 32768.0f * 0.5f;
  12239. seed = seed * 1664525u + 1013904223u;
  12240. inR[(std::size_t) i] = (float) ((int) (seed >> 16) - 32768) / 32768.0f * 0.5f;
  12241. }
  12242. using Golden = std::vector<std::tuple<int, float, float>>;
  12243. auto check = [&] (const std::vector<float>& l, const std::vector<float>& r, const Golden& golden) {
  12244. for (const auto& [i, gl, gr] : golden)
  12245. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  12246. };
  12247. auto run3 = [&] (const std::array<int, 26>& raw, double bpm) {
  12248. lynx::fldelay::Delay3Engine e (44100.0);
  12249. e.setTempo (bpm);
  12250. for (int i = 0; i < 26; ++i)
  12251. e.setParameter (i, lynx::fldelay::delay3EngineValue (i, raw[(std::size_t) i]));
  12252. e.setSampleRate (44100.0); // FL's wrapper sets the rate up again after the state
  12253. e.setSampleRate (44100.0);
  12254. std::vector<float> l (inL), r (inR);
  12255. for (int s = 0; s < n; s += 512)
  12256. e.process (l.data() + s, r.data() + s, 512);
  12257. return std::make_pair (l, r);
  12258. };
  12259. auto run2 = [&] (const std::array<int, 8>& raw, double bpm) {
  12260. lynx::fldelay::Delay2State st;
  12261. st.v = raw;
  12262. lynx::fldelay::Delay2Engine e (st, 44100.0, bpm, 512);
  12263. std::vector<float> l (inL), r (inR);
  12264. for (int s = 0; s < n; s += 512)
  12265. e.process (l.data() + s, r.data() + s, 512);
  12266. return std::make_pair (l, r);
  12267. };
  12268. // Delay 3, true stereo, synced 1/8 beat, cutoff + resonance (Fruity Delay 3_x64.dll at 120 and 100 BPM)
  12269. const std::array<int, 26> a { 6000, 1, 1, 0, 24, 4000, 500, 5000, 3000, 0, 0, 0, 0, 1, 4200, 0, 4500, 1500, 0, 0, 0, 60000, 6000, 6000, 0, 6000 };
  12270. auto o = run3 (a, 120.0);
  12271. check (o.first, o.second, { { 3000, 0.399949551f, 0.598533869f }, { 6000, 0.116703831f, -0.315510482f }, { 9000, -0.150520369f, -0.250620633f },
  12272. { 13000, 0.193608373f, 0.0598564595f }, { 16000, -0.0524701327f, 0.022601109f } });
  12273. o = run3 (a, 100.0);
  12274. check (o.first, o.second, { { 5000, 0.377422422f, 0.346570343f }, { 6000, -0.182619616f, 0.0488029197f }, { 11000, -0.00120426295f, 0.0253051519f },
  12275. { 16000, 0.0806550607f, -0.0991330743f } });
  12276. // ping pong, keep pitch, modulation, cutoff LFO, diffusion, saturation, rate and bit reduction, colour
  12277. o = run3 ({ 5000, 2, 1, 1, 36, 2100, 500, 6000, 4500, 3000, 2000, 3500, 4000, 1, 3800, 2, 3500, 2500, 2500, 1200, 3000, 40000, 4000, 5500, -1200, 4000 }, 120.0);
  12278. check (o.first, o.second, { { 1500, 0.288540125f, -0.148862943f }, { 5000, 5.90502168e-06f, 0.111382999f }, { 8000, -0.0736631379f, -0.0305093247f },
  12279. { 11000, -0.00831047911f, 0.0102323527f }, { 16000, 0.0175791476f, 0.0273917634f } });
  12280. // fake stereo, unsynced, high-pass, limit distortion, no dry
  12281. o = run3 ({ 6000, 0, 0, 0, 200, 3700, 0, 3000, 1500, 0, 0, 1500, 2000, 0, 3000, 1, 3000, 0, 3000, 0, 0, 60000, 6000, 6000, 1500, 0 }, 120.0);
  12282. check (o.first, o.second, { { 1500, 0.0825810209f, 0.0228505582f }, { 6000, -0.0427959524f, -0.0408911295f }, { 9000, 0.0114790015f, 0.019227922f },
  12283. { 16000, -0.000312746735f, 0.00101647642f } });
  12284. // delay type "Off": filter, distortion and the bit/rate reduction on the dry path
  12285. o = run3 ({ 6000, 3, 1, 0, 24, 3000, 500, 6000, 3000, 0, 0, 0, 0, 1, 3000, 2, 2500, 4000, 4000, 3000, 1000, 30000, 2000, 6000, 800, 3000 }, 120.0);
  12286. check (o.first, o.second, { { 1500, 0.238686681f, -0.106820084f }, { 3000, 0.131528616f, 0.0665975735f }, { 5000, 9.94473215e-09f, -8.22819057e-09f },
  12287. { 11000, 0.0f, 0.0f } });
  12288. // Delay 2: default, ping pong with a stereo offset, swapped without dry (Fruity Delay 2_x64.dll)
  12289. o = run2 ({ 0, 128, 128, 64, 36, 0, 0, 128 }, 120.0);
  12290. check (o.first, o.second, { { 3000, 0.211380005f, 0.277908325f }, { 5000, -0.223615795f, -0.0189359933f }, { 9000, 0.120054141f, -0.00297877868f },
  12291. { 16000, 0.0182533264f, 0.105384827f } });
  12292. o = run2 ({ -40, 110, 90, 90, 24, -300, 2, 90 }, 100.0);
  12293. check (o.first, o.second, { { 3000, 0.0929639712f, 0.122222826f }, { 5000, -0.0884733275f, -0.0507739782f }, { 11000, 0.0589251742f, 0.0f },
  12294. { 16000, -0.029524127f, 0.0128910914f } });
  12295. o = run2 ({ 60, 140, 0, 100, 30, 250, 1, 110 }, 120.0);
  12296. check (o.first, o.second, { { 1500, 0.0f, 0.0f }, { 5000, 0.176379532f, -0.212506086f }, { 8000, -0.324207425f, 0.0675852001f },
  12297. { 13000, 0.145023212f, -0.104788139f } });
  12298. // the mixer effects from FL states: time law (Delay 3 synced 192 = 1 beat; Delay 2 144 = 3/4 beat), settled start
  12299. auto state3 = std::vector<std::uint8_t> (4 + 26 * 4, 0);
  12300. const std::array<int, 26> def3 { 6000, 1, 1, 0, 192, 3000, 500, 6000, 3000, 0, 0, 0, 0, 1, 0, 0, 6000, 0, 0, 0, 0, 60000, 6000, 6000, 0, 6000 };
  12301. state3[0] = 1;
  12302. std::memcpy (state3.data() + 4, def3.data(), 26 * 4);
  12303. const auto slot3 = lynx::effectFromFl ("Fruity Delay 3", state3, 120.0);
  12304. CHECK (slot3 && slot3->kind == lynx::EffectKind::fruityDelay3);
  12305. auto state2 = std::vector<std::uint8_t> (32, 0);
  12306. const std::array<int, 8> def2 { 0, 128, 128, 0, 144, 0, 0, 128 };
  12307. std::memcpy (state2.data(), def2.data(), 32);
  12308. const auto slot2 = lynx::effectFromFl ("Fruity delay 2", state2, 120.0);
  12309. CHECK (slot2 && slot2->kind == lynx::EffectKind::fruityDelay2);
  12310. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityDelay2, 4, slot2->params[4], 120.0) == "3:00");
  12311. auto echoAt = [] (const lynx::EffectSlot& slot, double bpm) {
  12312. auto fx = lynx::makeEffect (slot);
  12313. fx->prepare (44100.0);
  12314. std::vector<float> l (48000, 0.0f), r (48000, 0.0f);
  12315. l[100] = 1.0f;
  12316. for (int s = 0; s < 48000; s += 1024)
  12317. fx->process (l.data() + s, r.data() + s, std::min (1024, 48000 - s), slot.params, bpm);
  12318. int best = 200;
  12319. for (int i = 200; i < 48000; ++i)
  12320. if (std::abs (l[(std::size_t) i]) > std::abs (l[(std::size_t) best]))
  12321. best = i;
  12322. return best - 100;
  12323. };
  12324. if (slot3 && slot2)
  12325. {
  12326. CHECK (echoAt (*slot3, 120.0) == 22050);
  12327. CHECK (echoAt (*slot3, 150.0) == 17640);
  12328. CHECK (echoAt (*slot2, 120.0) == 16538);
  12329. CHECK (echoAt (*slot2, 140.0) == 14175);
  12330. }
  12331. }
  12332. #include "UiRackMixerTests.inc"
  12333. // ---------------------------------------------------------------------------
  12334. // FL Studio integration (core/FlPluginDb.h): synthetic plugin-database fixtures, no Image-Line files
  12335. namespace
  12336. {
  12337. std::vector<std::uint8_t> testWrapperBlob (std::uint32_t type, const std::string& name, const std::string& path,
  12338. const std::vector<std::uint8_t>& classId = {}, const std::string& clapId = {})
  12339. {
  12340. std::vector<std::uint8_t> b;
  12341. auto u32 = [&b] (std::uint32_t v) { for (int i = 0; i < 4; ++i) b.push_back ((std::uint8_t) (v >> (8 * i))); };
  12342. auto record = [&] (std::uint32_t id, const std::vector<std::uint8_t>& payload) {
  12343. u32 (id);
  12344. u32 ((std::uint32_t) payload.size());
  12345. u32 (0);
  12346. b.insert (b.end(), payload.begin(), payload.end());
  12347. };
  12348. u32 (12);
  12349. record (0x32, { (std::uint8_t) type, 0, 0, 0 });
  12350. if (! classId.empty())
  12351. record (0x34, classId);
  12352. if (! clapId.empty())
  12353. record (0x3A, std::vector<std::uint8_t> (clapId.begin(), clapId.end()));
  12354. record (0x36, std::vector<std::uint8_t> (name.begin(), name.end()));
  12355. record (0x37, std::vector<std::uint8_t> (path.begin(), path.end()));
  12356. return b;
  12357. }
  12358. } // namespace
  12359. TEST (fl_plugin_db_nfo_parsing)
  12360. {
  12361. const std::string nfo = "ps_name=Vital\r\n"
  12362. "ps_file_name_0=C:\\Program Files\\Common Files\\VST3\\Vital.vst3\r\n"
  12363. "ps_file_vendorname_0=Vital Audio\r\n"
  12364. "ps_file_type_0=2\r\n"
  12365. "ps_file_name_1=C:\\VSTPlugins\\Vital_x64.dll\r\n"
  12366. "ps_file_type_1=2\r\n"
  12367. "ps_file_magic_1=Vita\r\n"
  12368. "ps_file_name_2=not a plugin\r\n"
  12369. "garbage line without equals\r\n";
  12370. const auto entries = lynx::parseFlPluginNfo (nfo, "D:\\FL\\Presets\\Plugin database\\Installed\\Generators\\VST3\\Vital.nfo");
  12371. CHECK (entries.size() == 2);
  12372. if (entries.size() == 2)
  12373. {
  12374. CHECK (entries[0].name == "Vital" && entries[0].vendor == "Vital Audio" && entries[0].role == 2);
  12375. CHECK (entries[0].format == lynx::ForeignPlugin::Format::vst3);
  12376. CHECK (entries[1].format == lynx::ForeignPlugin::Format::vst2 && entries[1].vst2Id == 0x56697461u);
  12377. }
  12378. // no type field: the role comes from the Effects / Generators folder; an ID in GUID form is read
  12379. const auto fx = lynx::parseFlPluginNfo ("ps_name=Fancy Comp\nps_file_filename_0=C:/CLAP/Fancy Comp.clap\n"
  12380. "ps_file_guid_0={01234567-89AB-CDEF-0123-456789ABCDEF}\n",
  12381. "C:\\x\\Installed\\Effects\\CLAP\\Fancy Comp.nfo");
  12382. CHECK (fx.size() == 1 && fx[0].role == 1 && fx[0].format == lynx::ForeignPlugin::Format::clap && fx[0].hasClassId
  12383. && fx[0].classId[0] == 0x01 && fx[0].classId[15] == 0xEF);
  12384. CHECK (lynx::parseFlPluginNfo ("Tip=All of the plugins found in your system.").empty());
  12385. CHECK (lynx::pluginNameKey ("Serum_x64") == "serum");
  12386. CHECK (lynx::pluginNameKey ("Vital.VST3") == "vital");
  12387. CHECK (lynx::pluginNameKey ("Pro-Q 3") == "proq3");
  12388. CHECK (lynx::pluginFormatFromPath ("a\\b.CLAP") == lynx::ForeignPlugin::Format::clap);
  12389. }
  12390. TEST (fl_plugin_db_fst_parsing_and_matching)
  12391. {
  12392. std::vector<std::uint8_t> cid;
  12393. for (int i = 0; i < 16; ++i)
  12394. cid.push_back ((std::uint8_t) (0x10 + i));
  12395. // an .fst of the database: an FL preset whose 0xD5 holds the wrapper record
  12396. FlpBuilder fst;
  12397. fst.ascii (0xC9, "Fruity Wrapper");
  12398. fst.data (0xD5, testWrapperBlob (8, "Surge XT", "E:\\Plugins\\VST3\\Surge XT.vst3", cid));
  12399. lynx::FlPluginDbEntry e;
  12400. CHECK (lynx::parseFlPluginFst (fst.bytes, e));
  12401. CHECK (e.name == "Surge XT" && e.format == lynx::ForeignPlugin::Format::vst3 && e.hasClassId && e.classId[0] == 0x10);
  12402. lynx::FlPluginDbEntry bad;
  12403. CHECK (! lynx::parseFlPluginFst (std::vector<std::uint8_t> { 'F', 'L', 'h', 'd' }, bad));
  12404. lynx::FlPluginDb db;
  12405. db.add (e);
  12406. // the .nfo of the same plugin fills in vendor and role
  12407. for (const auto& n : lynx::parseFlPluginNfo ("ps_name=Surge XT\nps_file_name_0=E:/Plugins/VST3/Surge XT.vst3\n"
  12408. "ps_file_vendorname_0=Surge Synth Team\nps_file_type_0=2\n"))
  12409. db.add (n);
  12410. db.add ({ "Clapper", "", "E:\\Plugins\\CLAP\\Clapper.clap", "", lynx::ForeignPlugin::Format::clap, 1, {}, false, "org.clapper", 0 });
  12411. db.add ({ "Big Verb", "", "E:\\Old VST\\BigVerb_x64.dll", "", lynx::ForeignPlugin::Format::vst2, 1, {}, false, "", 0x42566262u });
  12412. db.add ({ "Bundle", "", "F:\\VST3\\Bundle.vst3\\Contents\\x86_64-win\\Bundle.vst3", "", lynx::ForeignPlugin::Format::vst3, 2, {}, false, "", 0 });
  12413. CHECK (db.entries().size() == 4);
  12414. CHECK (db.entries()[0].vendor == "Surge Synth Team" && db.entries()[0].role == 2);
  12415. // by VST3 class ID even when the project saved another path and name
  12416. lynx::ForeignPlugin p;
  12417. p.format = lynx::ForeignPlugin::Format::vst3;
  12418. p.hasClassId = true;
  12419. std::copy (cid.begin(), cid.end(), p.classId.begin());
  12420. p.name = "Renamed";
  12421. p.path = "C:\\Other PC\\Surge.vst3";
  12422. CHECK (db.match (p, true) == &db.entries()[0]);
  12423. // by CLAP id, by VST2 id
  12424. lynx::ForeignPlugin c;
  12425. c.format = lynx::ForeignPlugin::Format::clap;
  12426. c.clapId = "org.clapper";
  12427. CHECK (db.match (c, false) == &db.entries()[1]);
  12428. lynx::ForeignPlugin v;
  12429. v.format = lynx::ForeignPlugin::Format::vst2;
  12430. v.vst2Id = 0x42566262u;
  12431. CHECK (db.match (v, false) == &db.entries()[2]);
  12432. // by name (architecture suffix ignored), only in the same role
  12433. lynx::ForeignPlugin byName;
  12434. byName.format = lynx::ForeignPlugin::Format::vst2;
  12435. byName.name = "BigVerb x64";
  12436. CHECK (db.match (byName, false) == &db.entries()[2]);
  12437. CHECK (db.match (byName, true) == nullptr);
  12438. lynx::ForeignPlugin unknown;
  12439. unknown.format = lynx::ForeignPlugin::Format::vst3;
  12440. unknown.name = "Nothing Like It";
  12441. CHECK (db.match (unknown, true) == nullptr);
  12442. // folders: the bundle's parent, deduplicated
  12443. const auto folders = db.pluginFolders();
  12444. CHECK (folders.size() == 4);
  12445. CHECK (std::find (folders.begin(), folders.end(), "F:\\VST3") != folders.end());
  12446. CHECK (std::find (folders.begin(), folders.end(), "E:\\Plugins\\VST3") != folders.end());
  12447. // targeted scan picks: same file, or same name key
  12448. CHECK (lynx::pluginFileMatches ("E:/Plugins/VST3/Surge XT.vst3", p, &db.entries()[0]));
  12449. CHECK (lynx::pluginFileMatches ("C:\\Program Files\\Common Files\\VST3\\BigVerb.vst3", byName, nullptr));
  12450. CHECK (! lynx::pluginFileMatches ("C:\\VST3\\Other.vst3", byName, nullptr));
  12451. // the plugins an .flp uses: wrapper channels and mixer slots
  12452. flp::Project project;
  12453. flp::Channel ch;
  12454. ch.pluginName = "Fruity Wrapper";
  12455. ch.pluginState = testWrapperBlob (8, "Surge XT", "C:\\VST3\\Surge XT.vst3", cid);
  12456. project.channels.push_back (ch);
  12457. ch.pluginName = "3x Osc";
  12458. project.channels.push_back (ch);
  12459. flp::MixerInsert insert;
  12460. insert.slots.push_back ({ 0, "Fruity Wrapper", testWrapperBlob (12, "Clapper", "C:\\CLAP\\Clapper.clap", {}, "org.clapper") });
  12461. insert.slots.push_back ({ 1, "Fruity Wrapper", testWrapperBlob (3, "DX thing", "C:\\dx.dll") }); // DirectX: not hosted
  12462. project.mixer.push_back (insert);
  12463. const auto uses = lynx::flpForeignPlugins (project);
  12464. CHECK (uses.size() == 2);
  12465. CHECK (uses.size() == 2 && uses[0].instrument && ! uses[1].instrument && uses[1].plugin.clapId == "org.clapper");
  12466. }
  12467. TEST (fl_sample_relocation)
  12468. {
  12469. std::set<std::string> files { "D:\\Docs\\Image-Line\\FL Studio\\Audio\\Recorded\\take.wav", "C:\\Program Files\\FL 2026\\Data\\Patches\\kick.wav",
  12470. "C:\\Users\\me\\Music\\loop.wav", "E:\\Songs\\Track\\Samples\\snare.wav", "E:\\Songs\\Track\\hat.wav" };
  12471. auto exists = [&] (const std::string& p) { return files.count (p) > 0; };
  12472. lynx::FlPathRoots roots;
  12473. roots.userData = "D:\\Docs\\Image-Line\\FL Studio";
  12474. roots.factoryData = "C:\\Program Files\\FL 2026";
  12475. roots.home = "C:\\Users\\me";
  12476. const std::string project = "E:\\Songs\\Track";
  12477. // variable resolved, file in place
  12478. CHECK (lynx::locateFlSample ("%FLStudioFactoryData%\\Data\\Patches\\kick.wav", roots, project, exists) == "C:\\Program Files\\FL 2026\\Data\\Patches\\kick.wav");
  12479. // another PC's FL user data / FL install / user profile
  12480. CHECK (lynx::locateFlSample ("C:\\Users\\bob\\Documents\\Image-Line\\FL Studio\\Audio\\Recorded\\take.wav", roots, project, exists)
  12481. == "D:\\Docs\\Image-Line\\FL Studio\\Audio\\Recorded\\take.wav");
  12482. CHECK (lynx::locateFlSample ("C:\\Program Files (x86)\\Image-Line\\FL Studio 20\\Data\\Patches\\kick.wav", roots, project, exists)
  12483. == "C:\\Program Files\\FL 2026\\Data\\Patches\\kick.wav");
  12484. CHECK (lynx::locateFlSample ("C:/Users/bob/Music/loop.wav", roots, project, exists) == "C:\\Users\\me\\Music\\loop.wav");
  12485. // next to the project, also in a sub-folder named in the original path
  12486. CHECK (lynx::locateFlSample ("Z:\\Old\\Samples\\snare.wav", roots, project, exists) == "E:\\Songs\\Track\\Samples\\snare.wav");
  12487. CHECK (lynx::locateFlSample ("Z:\\Old\\Stuff\\hat.wav", roots, project, exists) == "E:\\Songs\\Track\\hat.wav");
  12488. CHECK (lynx::locateFlSample ("Z:\\nowhere.wav", roots, project, exists).empty());
  12489. CHECK (lynx::locateFlSample ("", roots, project, exists).empty());
  12490. }
  12491. // --- REA round 64: SimSynth (exact engine) and Wasp / Wasp XT (FL 2026's silent placeholder) ---------------------
  12492. namespace
  12493. {
  12494. // a SimSynth state written from scratch (REA_rodada64 §1.1 layout: 45 doubles, 19 unused ints, 19 switches, 12 unused
  12495. // ints, version)
  12496. std::vector<std::uint8_t> simSynthTestState (const std::array<double, 45>& P, const std::array<int, 19>& S, int version = 14)
  12497. {
  12498. std::vector<std::uint8_t> b (564, 0);
  12499. std::memcpy (b.data(), P.data(), 360);
  12500. std::memcpy (b.data() + 360 + 76, S.data(), 76);
  12501. std::memcpy (b.data() + 560, &version, 4);
  12502. return b;
  12503. }
  12504. // the 0xD5 records of an FL preset file (.fst: the FLhd/FLdt event stream) that follow a plugin name `plugin`
  12505. std::vector<std::vector<std::uint8_t>> pluginStatesIn (const std::filesystem::path& file, const std::string& plugin)
  12506. {
  12507. std::vector<std::vector<std::uint8_t>> out;
  12508. std::ifstream in (file, std::ios::binary);
  12509. const std::vector<std::uint8_t> b ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  12510. std::size_t i = 0;
  12511. while (i + 8 <= b.size() && std::memcmp (b.data() + i, "FLdt", 4) != 0)
  12512. ++i;
  12513. i += 8;
  12514. std::string name;
  12515. while (i < b.size())
  12516. {
  12517. const auto id = b[i++];
  12518. std::size_t n = id < 64 ? 1 : id < 128 ? 2 : id < 192 ? 4 : 0;
  12519. if (id >= 192)
  12520. for (int shift = 0; i < b.size(); shift += 7)
  12521. {
  12522. const auto c = b[i++];
  12523. n |= (std::size_t) (c & 0x7F) << shift;
  12524. if (c < 0x80)
  12525. break;
  12526. }
  12527. if (i + n > b.size())
  12528. break;
  12529. if (id == 0xC9)
  12530. {
  12531. name.clear();
  12532. for (std::size_t k = 0; k < n; ++k)
  12533. if (b[i + k] != 0)
  12534. name += (char) b[i + k]; // ASCII or UTF-16LE (zero bytes skipped)
  12535. }
  12536. if (id == 0xD5 && name == plugin)
  12537. out.emplace_back (b.begin() + (std::ptrdiff_t) i, b.begin() + (std::ptrdiff_t) (i + n));
  12538. i += n;
  12539. }
  12540. return out;
  12541. }
  12542. } // namespace
  12543. TEST (simsynth_engine)
  12544. {
  12545. // A: pulse + octave layer, half-cosine with unison and two octave layers, sine/sine⁴ mix, square LFO → pitch with
  12546. // delay, filter envelope, LP/BP mix, chorus. B (version 7): triangle ring-modulated by filtered noise, sample &
  12547. // hold LFO → cutoff, retrigger, own filter envelope, chorus off
  12548. const std::array<double, 45> A = { 0.3, 0.5, 0.55, 0.8, 0.6, 0.5, 0.2, 0.5, 0.75, 0.45, 0.6, 0.5, 0.5, 0.1, 0.7, 0.25, 0.5, 0.5, 0.5, 0.7, 0.3, 0.3, 0.8,
  12549. 0.5, 0.2, 0.0, 0.35, 0.55, 0.8, 0.6, 0.4, 0.0, 0.3, 0.2, 0.0, 0.2, 0.5, 0.6, 0.4, 0.7, 1.0, 0.3, 0.4, 0.5, 0.3 };
  12550. const std::array<int, 19> SA = { 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1 };
  12551. const std::array<double, 45> B = { 0.5, 0.5, 0.5, 0.9, 0.5, 0.5, 0.0, 0.5, 0.6, 0.5, 0.5, 0.5, 0.5, 0.4, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.0, 0.6,
  12552. 0.5, 0.4, 0.0, 0.5, 0.8, 0.6, 0.85, 0.0, 0.0, 0.0, 0.0, 0.0, 0.1, 0.3, 0.2, 0.5, 0.6, 1.0, 0.0, 0.5, 0.0, 0.0 };
  12553. const std::array<int, 19> SB = { 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0 };
  12554. const auto blobA = simSynthTestState (A, SA), blobB = simSynthTestState (B, SB, 7);
  12555. dumpIfAsked ("LYNX_DUMP_SIMSYNTH", blobA);
  12556. CHECK (lynx::simSynthStateUsable (blobA) && ! lynx::simSynthStateUsable (std::vector<std::uint8_t> (blobA.begin(), blobA.begin() + 563)));
  12557. const auto hp = lynx::simSynthHostParams (blobA);
  12558. // knobs go through ⌊65535·v⌋, the coarse pitches through 48 steps, the switches and wave selectors as they are
  12559. CHECK (hp.size() == 58u && hp[0] == 52428 && hp[27] == 10080 && hp[41] == 1 && hp[54] == 0 && hp[56] == 4);
  12560. // golden samples of the installation's SimSynth_x64.dll (round-64 EXEC harness: created, state restored, 44.1 kHz,
  12561. // host blocks of 512, old voice record) — LYNX's engine renders bit-identically: C5 0–30000, E5 Final.Vol 0.8 pan
  12562. // 16 11025–40000, G5 pan −20 +25 cents 22050–52000
  12563. const std::vector<lynx::simsynth::RefNote> notes = { { 0, 60, 1.0f, 0, 0, 0.0f, 0.0f, 30000 },
  12564. { 11025, 64, 0.8f, 16, 0, 0.0f, 0.0f, 40000 },
  12565. { 22050, 67, 1.0f, -20, 25, 0.0f, 0.0f, 52000 } };
  12566. struct Golden { int i; float l, r; };
  12567. static const Golden goldenA[] = { { 100, 0.0f, 0.000322219421f }, { 5000, -0.110216402f, -0.194837824f }, { 15000, 0.517085433f, -0.551030278f }, { 25000, -0.443528295f, 0.226643473f }, { 35000, -0.708713174f, -0.291932464f }, { 45000, -0.0335822627f, -0.00557783246f }, { 55000, 0.0926972628f, 0.076306425f }, { 66000, 0.0415042415f, -0.0125555545f } };
  12568. static const Golden goldenB[] = { { 100, 0.00706971437f, 0.00706971437f }, { 5000, -0.0311671626f, -0.0311671626f }, { 15000, 0.508298159f, 0.964286387f }, { 25000, -3.07005596f, -1.8650744f }, { 35000, -0.23775965f, -0.14130348f }, { 45000, -0.00415330986f, -0.00409662398f }, { 55000, 0.00124766713f, -0.00180800457f }, { 66000, 0.0196316559f, 0.0166722424f } };
  12569. for (int which = 0; which < 2; ++which)
  12570. {
  12571. std::vector<std::int64_t> counters;
  12572. const auto y = lynx::simsynth::renderReference (which == 0 ? blobA : blobB, notes, 44100.0, 66150, 512, &counters);
  12573. CHECK (y.size() == 2 * 66150u);
  12574. if (y.size() != 2 * 66150u)
  12575. continue;
  12576. // the random-table index after creation / state / rate set equals the DLL's (276 / 696 / 756 and 276 / 492 / 528)
  12577. CHECK (counters.size() == 4u && counters[2] == 276 && counters[3] == (which == 0 ? 696 : 492) && counters[0] == (which == 0 ? 756 : 528));
  12578. for (const auto& g : which == 0 ? std::vector (std::begin (goldenA), std::end (goldenA)) : std::vector (std::begin (goldenB), std::end (goldenB)))
  12579. CHECK (y[2 * (std::size_t) g.i] == g.l && y[2 * (std::size_t) g.i + 1] == g.r);
  12580. double e = 0.0;
  12581. for (auto v : y)
  12582. e += (double) v * v;
  12583. CHECK (std::abs (std::sqrt (e / (double) y.size()) - (which == 0 ? 0.290665331870 : 0.992116840643)) < 1e-9);
  12584. }
  12585. // the instrument: same engine behind LYNX's channel gains (V = 1, centre → Final.Vol 1, Final.Pan 0)
  12586. lynx::ChannelDef c;
  12587. c.instrument = lynx::InstrumentKind::simsynth;
  12588. c.pluginState = blobA;
  12589. auto inst = lynx::makeInstrument (lynx::InstrumentKind::simsynth);
  12590. inst->prepare (44100.0);
  12591. inst->configure (c, 120.0);
  12592. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  12593. for (int k = 0; k < 16; ++k)
  12594. {
  12595. if (k == 0)
  12596. inst->noteOn (60, 0.8f);
  12597. if (k == 10)
  12598. inst->noteOff (60);
  12599. inst->render (l.data() + 512 * k, r.data() + 512 * k, 512, 1.0f, 1.0f);
  12600. }
  12601. const auto ref = lynx::simsynth::renderReference (blobA, { { 0, 60, 1.0f, 0, 0, 0.0f, 0.0f, 5120 } }, 44100.0, 8192);
  12602. double diff = 0.0, energy = 0.0;
  12603. for (int i = 0; i < 8192; ++i)
  12604. {
  12605. 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]) });
  12606. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  12607. }
  12608. CHECK (diff == 0.0 && energy > 1e-3);
  12609. // the installation's SimSynth presets (when the FL data folder is given): every state usable and sounding
  12610. if (! realFlpDir.empty())
  12611. {
  12612. const auto folder = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "SimSynth";
  12613. int presets = 0, usable = 0, sounding = 0;
  12614. if (std::filesystem::exists (folder))
  12615. for (const auto& entry : std::filesystem::directory_iterator (folder))
  12616. for (const auto& st : pluginStatesIn (entry.path(), "SimSynth"))
  12617. {
  12618. ++presets;
  12619. if (! lynx::simSynthStateUsable (st))
  12620. continue;
  12621. ++usable;
  12622. const auto y = lynx::simsynth::renderReference (st, { { 0, 48, 1.0f, 0, 0, 0.0f, 0.0f, 11025 } }, 44100.0, 22050);
  12623. double e = 0.0;
  12624. bool finite = true;
  12625. for (auto v : y)
  12626. finite = finite && std::isfinite (v), e += (double) v * v;
  12627. CHECK (finite);
  12628. sounding += e > 1e-9 ? 1 : 0;
  12629. }
  12630. std::printf (" SimSynth presets: %d, %d usable, %d sounding\n", presets, usable, sounding);
  12631. CHECK (presets == 0 || (usable == presets && presets >= 70 && sounding >= presets - 2));
  12632. }
  12633. }
  12634. TEST (wasp_is_a_silent_placeholder)
  12635. {
  12636. // FL 2026's Wasp / Wasp XT DLL keeps the state and plays nothing: so does LYNX (REA_rodada64 §5)
  12637. auto inst = lynx::makeInstrument (lynx::InstrumentKind::wasp);
  12638. inst->prepare (44100.0);
  12639. std::vector<float> l (1024, 0.0f), r (1024, 0.0f);
  12640. inst->noteOn (60, 1.0f);
  12641. inst->render (l.data(), r.data(), 1024, 1.0f, 1.0f);
  12642. CHECK (inst->idle() && std::all_of (l.begin(), l.end(), [] (float v) { return v == 0.0f; }));
  12643. // import: a SimSynth channel plays on the exact engine, Wasp and Wasp XT channels become the silent placeholder with
  12644. // their state kept
  12645. std::vector<std::uint8_t> ss (564, 0), wasp (169, 1);
  12646. const int version = 14;
  12647. std::memcpy (ss.data() + 560, &version, 4);
  12648. FlpBuilder b;
  12649. b.ascii (0xC7, "25.2.5.5319");
  12650. int index = 0;
  12651. for (const auto& [name, state] : { std::pair<std::u16string, std::vector<std::uint8_t>> { u"SimSynth", ss }, { u"Wasp", wasp }, { u"Wasp XT", wasp } })
  12652. {
  12653. b.u8 (0x40); b.u16 ((std::uint16_t) index++);
  12654. b.u8 (0x15); b.u8 (2);
  12655. b.utf16 (0xC9, name);
  12656. b.data (0xD5, state);
  12657. }
  12658. const auto song = lynx::importFlp (flp::parseProject (b.bytes));
  12659. CHECK (song.channels.size() == 3u);
  12660. if (song.channels.size() == 3u)
  12661. {
  12662. CHECK (song.channels[0].instrument == lynx::InstrumentKind::simsynth && song.channels[0].pluginState == ss);
  12663. CHECK (song.channels[1].instrument == lynx::InstrumentKind::wasp && song.channels[1].pluginState == wasp);
  12664. CHECK (song.channels[2].instrument == lynx::InstrumentKind::wasp);
  12665. }
  12666. }
  12667. // --- REA round 67: Fruity Granulizer (exact engine) --------------------------------------------------------------
  12668. namespace
  12669. {
  12670. // our own deterministic test sample (a fading 220 Hz sine plus LCG noise per channel), as r67/py/gold67.py builds it
  12671. lynx::SampleData granulizerTestSample (int n, int ch)
  12672. {
  12673. lynx::SampleData d;
  12674. std::uint32_t s = 12345;
  12675. for (int i = 0; i < n; ++i)
  12676. {
  12677. const auto base = 0.5 * std::sin (2.0 * 3.141592653589793 * 220.0 * i / 44100.0) * (1.0 - 0.7 * i / n);
  12678. for (int c = 0; c < ch; ++c)
  12679. {
  12680. s = s * 1664525u + 1013904223u;
  12681. const auto v = (float) (base + 0.2 * ((double) (std::int32_t) s * (1.0 / 2147483648.0)));
  12682. (c == 0 ? d.left : d.right).push_back (v);
  12683. }
  12684. }
  12685. return d;
  12686. }
  12687. std::vector<std::uint8_t> granulizerTestState (const std::array<int, 12>& P, int keyMode = 0)
  12688. {
  12689. std::vector<std::uint8_t> b (60, 0);
  12690. const std::int32_t v = 5;
  12691. std::memcpy (b.data(), &v, 4);
  12692. std::memcpy (b.data() + 4, P.data(), 48);
  12693. std::memcpy (b.data() + 56, &keyMode, 4);
  12694. return b;
  12695. }
  12696. } // namespace
  12697. TEST (granulizer_engine)
  12698. {
  12699. // REA_rodada67: states written from scratch against our reference model (granmodel.py, which renders like the
  12700. // installation's DLL within −128 dB: the DLL's rcpps and IPP sines are not replicated)
  12701. const auto v5 = granulizerTestState ({ 204, 50, 611, 611, 0, 180, 0, 0, 0, 0, 0, 228 });
  12702. CHECK (lynx::granulizerStateUsable (v5));
  12703. const auto n5 = lynx::granulizerNormalisedState (v5, {});
  12704. CHECK (n5.size() == 428 && n5[0] == 7 && lynx::granulizerParam (n5, 0) == 204 && lynx::granulizerParam (n5, 11) == 228
  12705. && ! lynx::granulizerParam (n5, 12));
  12706. // version 1 (10 parameters): parameter 11 takes the plugin's 0xf8
  12707. {
  12708. std::vector<std::uint8_t> v1 (v5.begin(), v5.begin() + 44);
  12709. v1[0] = 1;
  12710. const auto n1 = lynx::granulizerNormalisedState (v1, {});
  12711. CHECK (lynx::granulizerParam (n1, 9) == 0 && lynx::granulizerParam (n1, 11) == 0xf8 && lynx::granulizerParam (n1, 3) == 611);
  12712. std::vector<std::uint8_t> bad (v5);
  12713. bad[0] = 9;
  12714. CHECK (! lynx::granulizerStateUsable (bad) && lynx::granulizerNormalisedState (bad, {}).empty());
  12715. auto edited = n5;
  12716. CHECK (lynx::granulizerSetParam (edited, 3, -5000) && lynx::granulizerParam (edited, 3) == -1000);
  12717. }
  12718. using N = lynx::granulizer::RefNote;
  12719. struct Golden { std::vector<std::uint8_t> state; int ch, fs, rate; lynx::granulizer::RefOptions opt; std::vector<N> notes;
  12720. double el, er; std::tuple<int, float, float> at[6]; };
  12721. auto check = [] (const Golden& g) {
  12722. auto smp = granulizerTestSample (20000, g.ch);
  12723. smp.sampleRate = g.rate;
  12724. const auto y = lynx::granulizer::renderReference (lynx::granulizerNormalisedState (g.state, {}), smp, g.notes, (double) g.fs, 16384, g.opt);
  12725. CHECK (y.size() == 2 * 16384);
  12726. if (y.size() != 2 * 16384)
  12727. return;
  12728. double el = 0.0, er = 0.0;
  12729. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  12730. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  12731. CHECK (std::abs (el - g.el) <= 1e-6 * g.el && std::abs (er - g.er) <= 1e-6 * g.er);
  12732. for (const auto& [i, l, r] : g.at)
  12733. CHECK (std::abs (y[2 * (std::size_t) i] - l) <= 1e-6f * std::max (1e-16f, std::abs (l))
  12734. && std::abs (y[2 * (std::size_t) i + 1] - r) <= 1e-6f * std::max (1e-16f, std::abs (r)));
  12735. };
  12736. lynx::granulizer::RefOptions o1, o2, o3;
  12737. o2.block = 333, o2.seed = 777;
  12738. o3.block = 64, o3.seed = 5;
  12739. // the plugin's defaults, two notes (one pitched by +23 cents and released: the processor's end fade)
  12740. check ({ v5, 2, 44100, 44100, o1, { { 0, 60, 0.8, 0.0, 0.0, -1 }, { 3000, 64, 0.6, -0.3, 23.0, 9000 } }, 553.4541810458171, 470.3837027429263,
  12741. { { 100, 0.057010360062122345f, 0.08960580080747604f }, { 1000, 0.07210570573806763f, -0.048257164657115936f },
  12742. { 2500, 0.14769265055656433f, -0.0194698516279459f }, { 4000, 0.11169229447841644f, 0.17487481236457825f },
  12743. { 7777, -0.4309714436531067f, -0.17027156054973602f }, { 12000, -0.11129666864871979f, -0.19622492790222168f } } });
  12744. // backwards, randomness, depth/speed wobble, stereo separation, loop, sample start; a mono 44.1 kHz sample at 48 kHz
  12745. check ({ granulizerTestState ({ 150, 120, 300, -480, 200, 90, 120, 40, 1, 0, 900, 300 }), 1, 48000, 44100, o2,
  12746. { { 0, 55, 1.0, 0.2, -37.0, 12000 }, { 5000, 67, 0.5, 0.0, 0.0, -1 } }, 959.5011214226065, 1294.4823684530272,
  12747. { { 100, 0.02567935362458229f, 0.010721667669713497f }, { 1000, 0.3007732629776001f, 0.08214527368545532f },
  12748. { 2500, 0.435413658618927f, 0.09068503230810165f }, { 4000, -0.24611327052116394f, -0.20149046182632446f },
  12749. { 7777, -0.008478119969367981f, 0.31221628189086914f }, { 12000, 0.203709632f, 0.324944794f } } });
  12750. // key mode 1 (the sample spread over two octaves), freeze, hard stereo separation; 48 kHz sample at 96 kHz, 64-sample blocks
  12751. check ({ granulizerTestState ({ 300, 0, 800, 700, 0, 180, 0, 64, 0, 1, 2000, 100 }, 1), 2, 96000, 48000, o3,
  12752. { { 0, 62, 0.9, 0.0, 0.0, -1 }, { 4000, 50, 0.7, 0.0, 11.0, -1 } }, 366.79556669810813, 0.00825590391720353,
  12753. { { 100, 0.004204070195555687f, 7.680895437786243e-20f }, { 1000, 0.008516596630215645f, 3.0263170437258543e-19f },
  12754. { 2500, 0.06948182731866837f, 4.5391312775082004e-18f }, { 4000, -0.01754589192569256f, -4.895453905423713e-19f },
  12755. { 7777, 0.10338862985372543f, 2.344363859234514e-18f }, { 12000, -0.09385982155799866f, -1.3493021437162966e-17f } } });
  12756. // the instrument: silent without a sample (FL's plugin refuses the note), sounding with one
  12757. {
  12758. auto silent = lynx::makeInstrument (lynx::InstrumentKind::granulizer);
  12759. silent->prepare (44100.0);
  12760. std::vector<float> l (2048, 0.0f), r (2048, 0.0f);
  12761. silent->noteOn (60, 1.0f);
  12762. silent->render (l.data(), r.data(), 2048, 1.0f, 1.0f);
  12763. CHECK (silent->idle() && std::all_of (l.begin(), l.end(), [] (float v) { return v == 0.0f; }));
  12764. auto smp = std::make_shared<lynx::SampleData> (granulizerTestSample (20000, 2));
  12765. auto inst = lynx::makeInstrument (lynx::InstrumentKind::granulizer, smp);
  12766. lynx::ChannelDef def;
  12767. def.instrument = lynx::InstrumentKind::granulizer;
  12768. def.pluginState = n5;
  12769. inst->configure (def, 140.0);
  12770. inst->prepare (44100.0);
  12771. inst->noteOn (60, 0.8f);
  12772. inst->render (l.data(), r.data(), 2048, 1.0f, 1.0f);
  12773. double e = 0.0;
  12774. for (auto v : l)
  12775. e += (double) v * v;
  12776. CHECK (! inst->idle() && e > 1.0);
  12777. inst->noteOff (60);
  12778. for (int k = 0; k < 4; ++k)
  12779. inst->render (l.data(), r.data(), 512, 1.0f, 1.0f);
  12780. CHECK (inst->idle()); // the 10 ms end fade of the plugin's voice processor
  12781. }
  12782. // import: a Fruity Granulizer channel plays on the exact engine with its sample
  12783. {
  12784. FlpBuilder b;
  12785. b.ascii (0xC7, "25.2.5.5319");
  12786. b.u8 (0x40); b.u16 (0);
  12787. b.u8 (0x15); b.u8 (2);
  12788. b.utf16 (0xC9, u"Fruity Granulizer");
  12789. b.data (0xD5, v5);
  12790. b.utf16 (0xC4, u"%FLStudioFactoryData%\\Data\\Patches\\Packs\\Drums\\Kicks\\808 Kick.wav");
  12791. lynx::FlpImportOptions options;
  12792. options.roots.factoryData = "C:\\FL";
  12793. options.fileExists = [] (const std::string& path) { return path == "C:\\FL\\Data\\Patches\\Packs\\Drums\\Kicks\\808 Kick.wav"; };
  12794. lynx::FlpImportReport report;
  12795. options.report = &report;
  12796. const auto song = lynx::importFlp (flp::parseProject (b.bytes), options);
  12797. CHECK (song.channels.size() == 1u);
  12798. if (song.channels.size() == 1u)
  12799. {
  12800. CHECK (song.channels[0].instrument == lynx::InstrumentKind::granulizer && song.channels[0].pluginState == n5);
  12801. CHECK (song.channels[0].samplePath == "C:\\FL\\Data\\Patches\\Packs\\Drums\\Kicks\\808 Kick.wav");
  12802. }
  12803. CHECK (report.converted["Fruity Granulizer"] == 1);
  12804. }
  12805. }
  12806. // --- REA round 67: Drumaxx (exact engine) -------------------------------------------------------------------------
  12807. namespace
  12808. {
  12809. // our own deterministic 16-pad kit (LCG-chosen parameters), as r67/py/gold67d.py builds it for the DLL
  12810. std::vector<std::uint8_t> drumaxxTestState (bool eq)
  12811. {
  12812. std::uint32_t s = 20261015u;
  12813. auto u = [&s] {
  12814. s = s * 1664525u + 1013904223u;
  12815. return (float) ((double) (s >> 8) * (1.0 / 16777216.0));
  12816. };
  12817. // (a product rounded on its own: no fused multiply-add)
  12818. auto mul = [] (float a, float b) {
  12819. volatile float p = a * b;
  12820. return (float) p;
  12821. };
  12822. static const int targets[] = { 0, 9, 10, 16, 21, 23, 26 };
  12823. std::array<float, 720> P {};
  12824. for (int k = 0; k < 16; ++k)
  12825. for (int i = 0; i < 44; ++i)
  12826. {
  12827. auto v = u();
  12828. if (i == 0)
  12829. v = 0.5f + mul (v, 0.5f);
  12830. else if ((i >= 7 && i <= 18) || (i >= 20 && i <= 34))
  12831. v = 0.2f + mul (v, 0.6f);
  12832. else if (i == 2 || i == 3)
  12833. v = 0.0f;
  12834. else if (i == 4)
  12835. v = (float) (36 + k);
  12836. else if (i == 5)
  12837. v = v < 0.5f ? 0.0f : 1.0f;
  12838. else if (i == 6)
  12839. v = (float) (int) (v * 4.0f);
  12840. else if (i >= 35 && i <= 38)
  12841. v = (float) targets[(int) (v * 7.0f)];
  12842. P[(std::size_t) (44 * k + i)] = v;
  12843. }
  12844. for (int i = 0; i < 16; ++i)
  12845. {
  12846. auto v = u();
  12847. if (i == 1)
  12848. v = 0.3f + mul (v, 0.4f);
  12849. else if (i == 3)
  12850. v = eq ? 1.0f : 0.0f;
  12851. else if (i >= 4 && i <= 11)
  12852. v = 0.35f + mul (v, 0.3f);
  12853. else if (i == 12)
  12854. v = 1.0f;
  12855. else if (i == 13)
  12856. v = v * 0.1f;
  12857. else if (i == 0 || i == 15)
  12858. v = 0.0f;
  12859. P[(std::size_t) (704 + i)] = v;
  12860. }
  12861. std::vector<std::uint8_t> b;
  12862. auto put32 = [&b] (std::uint32_t v) { for (int i = 0; i < 4; ++i) b.push_back ((std::uint8_t) (v >> (8 * i))); };
  12863. b.insert (b.end(), { 'S', 'S', 'L', 'F' });
  12864. put32 (21636u);
  12865. put32 (0x444d5333u); // engine stream version 3
  12866. const std::string name = "LYNX test kit";
  12867. b.insert (b.end(), name.begin(), name.end());
  12868. b.resize (b.size() + 256 - name.size(), 0);
  12869. const auto at = b.size();
  12870. b.resize (at + 720 * 4);
  12871. std::memcpy (b.data() + at, P.data(), 720 * 4);
  12872. for (int k = 0; k < 16; ++k)
  12873. {
  12874. b.resize (b.size() + 0x400, 0);
  12875. b.push_back (100), b.push_back (100); // pattern length, trigger key
  12876. }
  12877. b.resize (b.size() + 0x800, 0);
  12878. for (int k = 0; k < 16; ++k)
  12879. b.push_back (k >= 2 && k <= 4 ? 1 : 0); // pads 3…5 in cut group 1
  12880. b.resize (b.size() + 16, 0); // every pad on the master
  12881. return b;
  12882. }
  12883. } // namespace
  12884. TEST (drumaxx_engine)
  12885. {
  12886. const auto st = drumaxxTestState (true);
  12887. CHECK (st.size() == 21644u && lynx::drumaxxStateUsable (st) && lynx::drumaxxKitName (st) == "LYNX test kit");
  12888. CHECK (lynx::drumaxxParam (st, 4) == 36.0f && lynx::drumaxxParam (st, 44 * 15 + 4) == 51.0f && lynx::drumaxxParam (st, 0x2c3) == 1.0f);
  12889. {
  12890. auto edited = st;
  12891. lynx::drumaxxSetParam (edited, 0x2c1, 0.25f);
  12892. CHECK (lynx::drumaxxParam (edited, 0x2c1) == 0.25f && std::isnan (lynx::drumaxxParam (edited, 720)));
  12893. auto bad = st;
  12894. bad[8] = 'X';
  12895. CHECK (! lynx::drumaxxStateUsable (bad) && std::isnan (lynx::drumaxxParam (bad, 0)));
  12896. }
  12897. // goldens: LYNX's renders, bit-identical to Drumaxx_x64.dll + engine_x64.dll driven by the round-67 harness
  12898. using N = lynx::drumaxx::RefNote;
  12899. struct Golden { bool eq; int fs, block; std::vector<N> notes; double el, er; std::tuple<int, float, float> at[6]; };
  12900. auto check = [] (const Golden& g) {
  12901. const auto y = lynx::drumaxx::renderReference (drumaxxTestState (g.eq), g.notes, (double) g.fs, 20000, g.block);
  12902. CHECK (y.size() == 2 * 20000);
  12903. if (y.size() != 2 * 20000)
  12904. return;
  12905. double el = 0.0, er = 0.0;
  12906. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  12907. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  12908. CHECK (std::abs (el - g.el) <= 1e-6 * g.el && std::abs (er - g.er) <= 1e-6 * g.er);
  12909. for (const auto& [i, l, r] : g.at)
  12910. CHECK (std::abs (y[2 * (std::size_t) i] - l) <= 1e-6f * std::max (1e-16f, std::abs (l))
  12911. && std::abs (y[2 * (std::size_t) i + 1] - r) <= 1e-6f * std::max (1e-16f, std::abs (r)));
  12912. };
  12913. // master EQ and limiter on: velocities, Final.Vol, pitch offsets, the cut group (pads 3…5)
  12914. check ({ true, 44100, 512,
  12915. { { 0, 36, 1.0, 1.0, 0.0 }, { 1500, 38, 0.7, 0.8, 0.0 }, { 3000, 37, 0.5, 1.0, -120.0 }, { 4000, 38, 1.0, 1.0, 0.0 },
  12916. { 5200, 42, 0.9, 1.0, 0.0 }, { 6000, 43, 0.6, 1.0, 35.0 }, { 7000, 44, 1.0, 0.5, 0.0 }, { 9000, 40, 0.8, 1.0, 0.0 },
  12917. { 11000, 45, 1.0, 1.0, 250.0 }, { 12500, 48, 0.4, 1.0, 0.0 }, { 14000, 51, 1.0, 1.0, 0.0 }, { 15000, 39, 1.0, 0.7, -40.0 } },
  12918. 2698.372377185949, 3678.285731410973,
  12919. { { 100, 0.0f, 0.0f }, { 1000, -0.0034233136102557182f, -0.005708245560526848f }, { 2500, -0.41297006607055664f, -0.0276018138974905f },
  12920. { 4000, -0.04906991869211197f, -0.7206904292106628f }, { 7777, 0.7544509172439575f, 0.7584841847419739f },
  12921. { 15000, -0.13438990712165833f, -0.6798369884490967f } } });
  12922. // EQ off, 48 kHz in 333-sample blocks, two pads on the same sample
  12923. check ({ false, 48000, 333,
  12924. { { 0, 46, 1.0, 1.0, 0.0 }, { 800, 47, 0.9, 1.0, 0.0 }, { 2600, 41, 1.0, 1.0, -300.0 }, { 5000, 49, 0.75, 1.0, 0.0 },
  12925. { 7000, 50, 1.0, 0.9, 77.0 }, { 9000, 36, 1.0, 1.0, 0.0 }, { 9001, 42, 1.0, 1.0, 0.0 }, { 13000, 43, 0.3, 1.0, 0.0 } },
  12926. 2241.008810618806, 1814.5219804412964,
  12927. { { 100, 0.0f, 0.0f }, { 1000, -0.32539352774620056f, -0.7439078688621521f }, { 2500, -0.1711389720439911f, -0.39125433564186096f },
  12928. { 4000, 0.15524788200855255f, 0.3445436358451843f }, { 7777, 0.5472834706306458f, 0.08737402409315109f },
  12929. { 15000, -0.03079034946858883f, -0.06140122935175896f } } });
  12930. // 96 kHz in 64-sample blocks, an octave down
  12931. check ({ true, 96000, 64,
  12932. { { 0, 36, 1.0, 1.0, 0.0 }, { 500, 44, 1.0, 1.0, 0.0 }, { 1000, 38, 0.85, 1.0, 12.0 }, { 6000, 37, 1.0, 1.0, 0.0 },
  12933. { 10000, 51, 1.0, 1.0, -1200.0 } },
  12934. 3522.8420617665834, 5746.555795626526,
  12935. { { 100, 0.0f, 0.0f }, { 1000, -0.04133236035704613f, -0.09954667836427689f }, { 2500, 0.2800408899784088f, -0.9350972175598145f },
  12936. { 4000, 0.9537044167518616f, 0.8892703056335449f }, { 7777, 0.8376388549804688f, -0.14166420698165894f },
  12937. { 15000, -0.05926012247800827f, -0.5840796828269958f } } });
  12938. // the instrument: a pad sounds on its key and dies away; keys without a pad stay silent
  12939. {
  12940. auto inst = lynx::makeInstrument (lynx::InstrumentKind::drumaxx);
  12941. lynx::ChannelDef def;
  12942. def.instrument = lynx::InstrumentKind::drumaxx;
  12943. def.pluginState = st;
  12944. inst->configure (def, 140.0);
  12945. inst->prepare (44100.0);
  12946. std::vector<float> l (512, 0.0f), r (512, 0.0f);
  12947. inst->noteOn (90, 1.0f);
  12948. inst->render (l.data(), r.data(), 512, 1.0f, 1.0f);
  12949. CHECK (inst->idle() && std::all_of (l.begin(), l.end(), [] (float v) { return v == 0.0f; }));
  12950. inst->noteOn (36, 0.8f);
  12951. inst->render (l.data(), r.data(), 512, 1.0f, 1.0f);
  12952. double e = 0.0;
  12953. for (auto v : l)
  12954. e += (double) v * v;
  12955. CHECK (! inst->idle() && e > 1e-3);
  12956. for (int k = 0; k < 200 && ! inst->idle(); ++k)
  12957. inst->render (l.data(), r.data(), 512, 1.0f, 1.0f);
  12958. CHECK (inst->idle());
  12959. }
  12960. // import: a Drumaxx channel plays on the exact engine with its raw state
  12961. {
  12962. FlpBuilder b;
  12963. b.ascii (0xC7, "25.2.5.5319");
  12964. b.u8 (0x40); b.u16 (0);
  12965. b.u8 (0x15); b.u8 (2);
  12966. b.utf16 (0xC9, u"Drumaxx");
  12967. b.data (0xD5, st);
  12968. lynx::FlpImportOptions options;
  12969. lynx::FlpImportReport report;
  12970. options.report = &report;
  12971. const auto song = lynx::importFlp (flp::parseProject (b.bytes), options);
  12972. CHECK (song.channels.size() == 1u);
  12973. if (song.channels.size() == 1u)
  12974. CHECK (song.channels[0].instrument == lynx::InstrumentKind::drumaxx && song.channels[0].pluginState == st);
  12975. CHECK (report.converted["Drumaxx"] == 1);
  12976. }
  12977. }
  12978. // --- REA round 68: FL Slayer (exact engine) and Fruity Dance (visual only) ------------------------------------------
  12979. namespace
  12980. {
  12981. std::string slayerDll()
  12982. {
  12983. if (realFlpDir.empty())
  12984. return {};
  12985. return (std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "FL Slayer" / "Slayer_Engine.dll").string();
  12986. }
  12987. std::vector<std::uint8_t> slayerPreset (const char* name)
  12988. {
  12989. if (realFlpDir.empty())
  12990. return {};
  12991. const auto file = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "FL Slayer" / (std::string (name) + ".fst");
  12992. const auto states = pluginStatesIn (file, "FL Slayer");
  12993. return states.empty() ? std::vector<std::uint8_t> {} : states.front();
  12994. }
  12995. } // namespace
  12996. TEST (slayer_state)
  12997. {
  12998. // the saved state: int version, then the parameter integers (version 0: 28, version 1: 29 with the mod wheel)
  12999. std::vector<std::uint8_t> v0 (4 + 28 * 4, 0), v1 (4 + 29 * 4, 0);
  13000. const std::int32_t one = 1, wheel = 77, mode = 5;
  13001. std::memcpy (v1.data(), &one, 4);
  13002. std::memcpy (v1.data() + 4 + 28 * 4, &wheel, 4);
  13003. std::memcpy (v0.data() + 4 + 24 * 4, &mode, 4);
  13004. CHECK (lynx::slayerStateUsable (v0) && lynx::slayerStateUsable (v1));
  13005. CHECK (! lynx::slayerStateUsable (std::vector<std::uint8_t> (v0.begin(), v0.end() - 1)));
  13006. auto v2 = v1;
  13007. v2[0] = 2;
  13008. CHECK (! lynx::slayerStateUsable (v2));
  13009. const auto p0 = lynx::slayerStateParams (v0), p1 = lynx::slayerStateParams (v1);
  13010. CHECK (p0.size() == 29u && p0[24] == 5 && p0[28] == 0 && p1.size() == 29u && p1[28] == 77);
  13011. // the controls' maxima (the front-end sends value / max)
  13012. CHECK (lynx::slayerParamMax (0) == 6 && lynx::slayerParamMax (20) == 15 && lynx::slayerParamMax (24) == 5 && lynx::slayerParamMax (28) == 127
  13013. && lynx::slayerParamMax (3) == 65535);
  13014. // MIDI velocity from Init.Vol (0 → 0, which the engine takes as a note-off; FL's full velocity → 127)
  13015. CHECK (lynx::slayer::midiVelocity (0.0) == 0 && lynx::slayer::midiVelocity (1.0) == 99 && lynx::slayer::midiVelocity (2.0) == 126
  13016. && lynx::slayer::midiVelocity ((std::pow (11.0, 1.28) - 1.0) / 10.0) == 127);
  13017. }
  13018. TEST (slayer_engine)
  13019. {
  13020. const auto dll = slayerDll();
  13021. if (dll.empty() || ! lynx::slayerDllUsable (dll))
  13022. return; // no FL installation
  13023. // against the installation's Slayer_Engine.dll (EXEC, REA_rodada68 §7: the DLL driven like FL's front-end drives it,
  13024. // host blocks of 512 at 44.1 kHz, tempo 120): bit-identical output. C3 vel 90 1024–20000, G3 vel 120 8000–30000,
  13025. // E4 vel 40 16000–40000, C5 vel 127 from 30000.
  13026. struct Sample { int at; float l, r; };
  13027. struct Golden { const char* preset; double el, er; Sample s[4]; };
  13028. const Golden goldens[] {
  13029. { "BASS Ebass 2", 15073.5614896, 15073.5614896, { { 5000, 0.39340171217918396f, 0.39340171217918396f }, { 12000, -1.6453207731246948f, -1.6453207731246948f }, { 25000, 0.4477591812610626f, 0.4477591812610626f }, { 40000, 0.15478527545928955f, 0.15478527545928955f } } },
  13030. { "DIST Dire Straits", 1667.57549156, 1687.24007956, { { 5000, 0.026657210662961006f, -0.10683870315551758f }, { 12000, 0.16261409223079681f, -0.402504026889801f }, { 25000, 0.06702516227960587f, -0.04462721571326256f }, { 40000, -0.35892999172210693f, 0.29433923959732056f } } },
  13031. { "POWER Metal", 2041.24961449, 2041.24961449, { { 5000, -0.045430999249219894f, -0.045430999249219894f }, { 12000, 0.009085401892662048f, 0.009085401892662048f }, { 25000, -0.15170709788799286f, -0.15170709788799286f }, { 40000, -0.0430934801697731f, -0.0430934801697731f } } },
  13032. { "SINGLE Chorus", 1199.72408266, 1228.23838997, { { 5000, 0.08928016573190689f, 0.07585708051919937f }, { 12000, 0.22826193273067474f, 0.16860444843769073f }, { 25000, 0.14708775281906128f, 0.12602058053016663f }, { 40000, -0.02422964572906494f, -0.021596161648631096f } } },
  13033. { "AUTO Leslie", 2045.59821914, 16901.1532986, { { 5000, 0.09575536102056503f, -0.0019274931401014328f }, { 12000, -0.0070709832943975925f, -0.3028614819049835f }, { 25000, 0.07535621523857117f, 0.24308738112449646f }, { 40000, -0.09379560500383377f, -0.008114430122077465f } } },
  13034. { "FX Mad Photons", 1849.74988002, 1849.74988002, { { 5000, -0.11505282670259476f, -0.11505282670259476f }, { 12000, 0.3610122501850128f, 0.3610122501850128f }, { 25000, 0.36028820276260376f, 0.36028820276260376f }, { 40000, -0.5374203324317932f, -0.5374203324317932f } } },
  13035. { "ELEKTRO Jungle", 3929.94173335, 3929.94173335, { { 5000, 0.3158963918685913f, 0.3158963918685913f }, { 12000, 0.2988760769367218f, 0.2988760769367218f }, { 25000, 0.15749095380306244f, 0.15749095380306244f }, { 40000, -0.12722519040107727f, -0.12722519040107727f } } },
  13036. { "DIST Sololead Glide", 988.454469266, 1208.61893055, { { 5000, -0.03612576052546501f, -0.015017407014966011f }, { 12000, -0.008314903825521469f, 0.03836013376712799f }, { 25000, -0.047041457146406174f, -0.13751298189163208f }, { 40000, -0.03302673622965813f, -0.23574480414390564f } } },
  13037. { "SINGLE Funky", 1393.67536587, 4191.35072624, { { 5000, -0.09534210711717606f, 0.36347872018814087f }, { 12000, 0.3368682265281677f, -0.9259893894195557f }, { 25000, -0.022296791896224022f, 0.01075129397213459f }, { 40000, 0.044511131942272186f, -0.37124285101890564f } } },
  13038. { "AUTO Chorus Chords", 26161.9618548, 26396.2394554, { { 5000, -0.19687165319919586f, 0.08371803164482117f }, { 12000, 1.0583257675170898f, 1.5273079872131348f }, { 25000, -0.88118577003479f, -0.0774342492222786f }, { 40000, -0.3456699848175049f, -0.4140896201133728f } } },
  13039. { "POWER Rock Taps", 1306.21610814, 1164.18639692, { { 5000, 0.019661661237478256f, 0.07336095720529556f }, { 12000, -0.06010562554001808f, -0.3300100862979889f }, { 25000, 0.04404725879430771f, 0.0837465152144432f }, { 40000, -0.029322467744350433f, -0.1399042010307312f } } },
  13040. { "FX Atari 2600 Computer", 1391.44227691, 1391.44227691, { { 5000, 0.18123838305473328f, 0.18123838305473328f }, { 12000, -0.1220453679561615f, -0.1220453679561615f }, { 25000, 0.17565210163593292f, 0.17565210163593292f }, { 40000, 0.21183894574642181f, 0.21183894574642181f } } },
  13041. };
  13042. const std::vector<lynx::slayer::RefNote> notes = { { 1024, 48, 90, 20000 }, { 8000, 55, 120, 30000 }, { 16000, 64, 40, 40000 }, { 30000, 72, 127, -1 } };
  13043. constexpr int length = 44100;
  13044. int checked = 0;
  13045. for (const auto& g : goldens)
  13046. {
  13047. const auto st = slayerPreset (g.preset);
  13048. CHECK (lynx::slayerStateUsable (st));
  13049. if (! lynx::slayerStateUsable (st))
  13050. continue;
  13051. const auto y = lynx::slayer::renderReference (lynx::slayerStateParams (st), dll, notes, 44100.0, length);
  13052. double el = 0.0, er = 0.0;
  13053. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  13054. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  13055. bool ok = y.size() == 2 * (std::size_t) length && std::abs (el - g.el) <= 1e-10 * g.el && std::abs (er - g.er) <= 1e-10 * g.er;
  13056. for (const auto& smp : g.s)
  13057. ok = ok && y[2 * (std::size_t) smp.at] == smp.l && y[2 * (std::size_t) smp.at + 1] == smp.r;
  13058. if (! ok)
  13059. std::printf (" FL Slayer %s differs from FL's DLL: energy %.12g %.12g (DLL %.12g %.12g)\n", g.preset, el, er, g.el, g.er);
  13060. CHECK (ok);
  13061. ++checked;
  13062. }
  13063. CHECK (checked == 12);
  13064. // the instrument: the engine primed with one sample, FL's velocity curve, the front-end's gains from the channel's
  13065. // MIDI pan / volume (REA_rodada79 §2: twice the channel gain through the host's integer-pan law); a note with
  13066. // velocity 0.8 is the reference note one sample later
  13067. const auto st = slayerPreset ("BASS Ebass 2");
  13068. lynx::ChannelDef c;
  13069. c.instrument = lynx::InstrumentKind::slayer;
  13070. c.pluginState = st;
  13071. c.samplePath = dll;
  13072. auto inst = lynx::makeInstrument (lynx::InstrumentKind::slayer);
  13073. inst->configure (c, 120.0);
  13074. inst->prepare (44100.0);
  13075. std::vector<float> l (16384, 0.0f), r (16384, 0.0f);
  13076. for (int b = 0; b < 32; ++b)
  13077. {
  13078. if (b == 0)
  13079. inst->noteOn (60, 0.8f);
  13080. if (b == 20)
  13081. inst->noteOff (60);
  13082. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 0.5f, 0.25f);
  13083. }
  13084. const int vel = lynx::slayer::midiVelocity ((std::pow (11.0, 1.28 * 0.8) - 1.0) / 10.0);
  13085. const auto ref = lynx::slayer::renderReference (lynx::slayerStateParams (st), dll, { { 1, 60, vel, 1 + 20 * 512 } }, 44100.0, 16385);
  13086. // LYNX's channel gains 0.5 / 0.25 = gain 0.395…, pan −0.4097 → MIDI pan −26
  13087. const auto chanVol = (float) std::sqrt ((0.25 + 0.0625) * 0.5);
  13088. float hl = 0.0f, hr = 0.0f;
  13089. lynx::slayer::hostGains (lynx::slayer::midiPan (std::atan2 (0.25, 0.5) * (4.0 / 3.141592653589793) - 1.0), chanVol, hl, hr);
  13090. CHECK (lynx::slayer::midiPan (std::atan2 (0.25, 0.5) * (4.0 / 3.141592653589793) - 1.0) == -26);
  13091. double diff = 0.0, energy = 0.0;
  13092. for (int i = 0; i < 16384; ++i)
  13093. {
  13094. diff = std::max ({ diff, (double) std::abs (l[(std::size_t) i] - ref[2 * (std::size_t) (i + 1)] * hl),
  13095. (double) std::abs (r[(std::size_t) i] - ref[2 * (std::size_t) (i + 1) + 1] * hr) });
  13096. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  13097. }
  13098. CHECK (diff == 0.0 && energy > 1.0 && vel == 102);
  13099. // every preset of the installation: usable and sounding
  13100. int presets = 0, sounding = 0;
  13101. const auto folder = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "FL Slayer";
  13102. if (std::filesystem::exists (folder))
  13103. for (const auto& entry : std::filesystem::directory_iterator (folder))
  13104. for (const auto& s : pluginStatesIn (entry.path(), "FL Slayer"))
  13105. {
  13106. ++presets;
  13107. const auto y = lynx::slayer::renderReference (lynx::slayerStateParams (s), dll, { { 1024, 52, 100, 20000 } }, 44100.0, 22050);
  13108. double e = 0.0;
  13109. bool finite = true;
  13110. for (auto v : y)
  13111. finite = finite && std::isfinite (v), e += (double) v * v;
  13112. CHECK (finite);
  13113. sounding += e > 1.0 ? 1 : 0;
  13114. }
  13115. std::printf (" FL Slayer presets: %d, %d sounding\n", presets, sounding);
  13116. CHECK (presets == 64 && sounding == 64);
  13117. }
  13118. // FL Slayer's host levels and live parameters (REA_rodada79): channel pan / volume / pitch arrive as MIDI events
  13119. // (ProcessEvent 2/3/4), automation and settings edits as setParameter calls on the running engine
  13120. TEST (slayer_host_levels_and_automation)
  13121. {
  13122. using namespace lynx::slayer;
  13123. CHECK (pitchBend (0) == 8192 && pitchBend (100) == 8874 && pitchBend (-100) == 7510 && pitchBend (1200) == 0x3fff
  13124. && pitchBend (-1200) == 0 && pitchBend (-1199) == 7 && pitchBend (5000) == 0x3fff);
  13125. CHECK (midiPan (0.0) == 0 && midiPan (1.0) == 64 && midiPan (-1.0) == -64 && midiPan (-0.5) == -32
  13126. && midiPan (99.0 / 6400.0) == 0 && midiPan (-199.0 / 6400.0) == -1 && midiPan (2.0) == 64);
  13127. float l0 = 0.0f, r0 = 0.0f, l1 = 0.0f, r1 = 0.0f;
  13128. hostGains (0, 0.5f, l0, r0); // the front-end's own start values (volume 1 after the ×2, centre)
  13129. hostGains (64, 1.0f, l1, r1);
  13130. CHECK (l0 == 0.70710677f && r0 == 0.70710677f && std::abs (l1) < 1e-15f && r1 == 2.0f);
  13131. const auto dll = slayerDll();
  13132. if (dll.empty() || ! lynx::slayerDllUsable (dll))
  13133. return;
  13134. const auto st = slayerPreset ("DIST Dire Straits");
  13135. const auto params = lynx::slayerStateParams (st);
  13136. lynx::ChannelDef c;
  13137. c.instrument = lynx::InstrumentKind::slayer;
  13138. c.pluginState = st;
  13139. c.samplePath = dll;
  13140. c.sampler.pitch = 1.5f; // channel pitch +150 cents → bend 9216; no root shift
  13141. c.modelParams = { 0.0f };
  13142. auto inst = lynx::makeInstrument (lynx::InstrumentKind::slayer);
  13143. inst->configure (c, 120.0);
  13144. inst->prepare (44100.0);
  13145. const int blocks = 40, n = 512;
  13146. std::vector<float> l ((std::size_t) (blocks * n), 0.0f), r ((std::size_t) (blocks * n), 0.0f);
  13147. auto edited = st;
  13148. const std::int32_t newDrive = 60000; // parameter 13 (amp drive), edited in the settings at block 25
  13149. std::memcpy (edited.data() + 4 + 4 * 13, &newDrive, 4);
  13150. for (int b = 0; b < blocks; ++b)
  13151. {
  13152. if (b == 0)
  13153. inst->noteOn (52, 0.8f);
  13154. if (b == 10)
  13155. inst->setParameter (20, 9.0f / 15.0f); // automation: effect 9 (chorus)
  13156. if (b == 18)
  13157. inst->setParameter (28, 0.5f); // automation: the mod wheel → CC 1 64
  13158. if (b == 25)
  13159. {
  13160. c.pluginState = edited;
  13161. inst->configure (c, 120.0);
  13162. }
  13163. inst->render (l.data() + n * b, r.data() + n * b, n, 1.0f, 1.0f);
  13164. }
  13165. lynx::slayer::RefOptions opt;
  13166. opt.midi.push_back ({ 1, 0xe0, 9216 & 0x7f, 9216 >> 7 });
  13167. opt.params = { { 1 + 10 * n, 20, 9 }, { 1 + 18 * n, 28, 64 }, { 1 + 25 * n, 13, newDrive } };
  13168. const int vel = lynx::slayer::midiVelocity ((std::pow (11.0, 1.28 * 0.8) - 1.0) / 10.0);
  13169. const auto ref = lynx::slayer::renderReference (params, dll, { { 1, 52, vel, -1 } }, 44100.0, blocks * n + 1, opt);
  13170. float hl = 0.0f, hr = 0.0f;
  13171. hostGains (0, 1.0f, hl, hr);
  13172. double diff = 0.0, energy = 0.0;
  13173. for (int i = 0; i < blocks * n; ++i)
  13174. {
  13175. diff = std::max ({ diff, (double) std::abs (l[(std::size_t) i] - ref[2 * (std::size_t) (i + 1)] * hl),
  13176. (double) std::abs (r[(std::size_t) i] - ref[2 * (std::size_t) (i + 1) + 1] * hr) });
  13177. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  13178. }
  13179. CHECK (diff == 0.0 && energy > 1.0);
  13180. // the changes really change the sound: the same render without them differs
  13181. const auto plain = lynx::slayer::renderReference (params, dll, { { 1, 52, vel, -1 } }, 44100.0, blocks * n + 1);
  13182. CHECK (plain != ref);
  13183. }
  13184. TEST (slayer_and_fruity_dance_import)
  13185. {
  13186. // FL Slayer plays on the exact engine when the installation's engine DLL is there (otherwise it stays unsupported);
  13187. // Fruity Dance (a visual plugin) becomes a silent channel that keeps its state
  13188. std::vector<std::uint8_t> slayer (4 + 28 * 4, 0), dance (40, 1);
  13189. FlpBuilder b;
  13190. b.ascii (0xC7, "25.2.5.5319");
  13191. int index = 0;
  13192. for (const auto& [name, state] : { std::pair<std::u16string, std::vector<std::uint8_t>> { u"FL Slayer", slayer }, { u"Fruity Dance", dance } })
  13193. {
  13194. b.u8 (0x40); b.u16 ((std::uint16_t) index++);
  13195. b.u8 (0x15); b.u8 (2);
  13196. b.utf16 (0xC9, name);
  13197. b.data (0xD5, state);
  13198. }
  13199. const auto project = flp::parseProject (b.bytes);
  13200. const auto plain = lynx::importFlp (project);
  13201. CHECK (plain.channels.size() == 2u);
  13202. if (plain.channels.size() == 2u)
  13203. {
  13204. CHECK (plain.channels[0].instrument != lynx::InstrumentKind::slayer);
  13205. CHECK (plain.channels[1].instrument == lynx::InstrumentKind::fruitydance && plain.channels[1].pluginState == dance);
  13206. }
  13207. auto inst = lynx::makeInstrument (lynx::InstrumentKind::fruitydance);
  13208. inst->prepare (44100.0);
  13209. std::vector<float> l (1024, 0.0f), r (1024, 0.0f);
  13210. inst->noteOn (60, 1.0f);
  13211. inst->render (l.data(), r.data(), 1024, 1.0f, 1.0f);
  13212. CHECK (inst->idle() && std::all_of (l.begin(), l.end(), [] (float v) { return v == 0.0f; }));
  13213. const auto dll = slayerDll();
  13214. if (dll.empty() || ! lynx::slayerDllUsable (dll))
  13215. return;
  13216. lynx::FlpImportOptions withFiles;
  13217. withFiles.roots = { std::filesystem::path (realFlpDir).parent_path().string(), realFlpDir, "" };
  13218. withFiles.fileExists = [] (const std::string& path) {
  13219. auto local = path;
  13220. std::replace (local.begin(), local.end(), '\\', '/');
  13221. return std::filesystem::exists (local);
  13222. };
  13223. const auto song = lynx::importFlp (project, withFiles);
  13224. CHECK (song.channels.size() == 2u && song.channels[0].instrument == lynx::InstrumentKind::slayer && song.channels[0].pluginState == slayer
  13225. && ! song.channels[0].samplePath.empty());
  13226. }
  13227. #include "WaveAnalysisTests.inc"
  13228. #include "UiPolish2Tests.inc"
  13229. #include "UiToolbarTests.inc"
  13230. #include "LuxeVerbTests.inc"
  13231. #include "ConvolverTests.inc"
  13232. // --- Newtone / Newtime (REA_rodada73) -----------------------------------------------------------------------------
  13233. namespace newtonetest
  13234. {
  13235. void put32 (std::vector<std::uint8_t>& b, std::int32_t v) { for (int i = 0; i < 4; ++i) b.push_back ((std::uint8_t) ((std::uint32_t) v >> (8 * i))); }
  13236. void put64 (std::vector<std::uint8_t>& b, std::int64_t v) { for (int i = 0; i < 8; ++i) b.push_back ((std::uint8_t) ((std::uint64_t) v >> (8 * i))); }
  13237. void record (std::vector<std::uint8_t>& b, int id, const std::vector<std::uint8_t>& d)
  13238. {
  13239. put32 (b, id);
  13240. put64 (b, (std::int64_t) d.size());
  13241. b.insert (b.end(), d.begin(), d.end());
  13242. }
  13243. // a state with our own layout reading (REA_rodada73 §2): options, the wrapper record around the engine XML, the source path
  13244. std::vector<std::uint8_t> state (int version, std::uint32_t flags, const std::string& xml, const std::string& source)
  13245. {
  13246. std::vector<std::uint8_t> b, r1, r2;
  13247. put32 (b, version);
  13248. put32 (r1, 727);
  13249. put32 (r1, 582);
  13250. put32 (r1, (std::int32_t) flags);
  13251. r1.resize (0x28, 0);
  13252. record (b, 1, r1);
  13253. std::vector<std::uint8_t> chunk { 'V', 'C', '2', '!' };
  13254. put32 (chunk, (std::int32_t) xml.size() + 1);
  13255. chunk.insert (chunk.end(), xml.begin(), xml.end());
  13256. chunk.push_back (0);
  13257. put32 (r2, -9);
  13258. r2.push_back (0x0c);
  13259. put32 (r2, -2);
  13260. put64 (r2, (std::int64_t) chunk.size());
  13261. put32 (r2, 0);
  13262. r2.insert (r2.end(), chunk.begin(), chunk.end());
  13263. record (b, 2, r2);
  13264. record (b, 3, std::vector<std::uint8_t> (source.begin(), source.end()));
  13265. record (b, 4, std::vector<std::uint8_t> (12, 0));
  13266. record (b, 5, std::vector<std::uint8_t> (16, 0));
  13267. record (b, 6, std::vector<std::uint8_t> (32, 0));
  13268. return b;
  13269. }
  13270. const char* xmlWithTrack (bool edited)
  13271. {
  13272. return edited ? "<NewToneProject NumOfTracks=\"1\"><Tracks><Trk_0 TrackNum=\"0\" Type=\"class CzplJPositionablePSOLASource\" "
  13273. "FilePath=\"C:\\tmp\\a&amp;b.wav\"><AudioObject SelfBPM=\"120.0\" OriginalFileLength=\"1000\" GlobalTune=\"0.0\">"
  13274. "<Pitch_0 fOrigPitch=\"60.0\" fCurPitch=\"60.0\" iOrigLength=\"500\" iCurLength=\"500\"/>"
  13275. "<Pitch_1 fOrigPitch=\"62.0\" fCurPitch=\"64.0\" iOrigLength=\"500\" iCurLength=\"500\"/></AudioObject></Trk_0></Tracks></NewToneProject>"
  13276. : "<NewToneProject NumOfTracks=\"1\"><Tracks><Trk_0 TrackNum=\"0\" Type=\"class CzplJTimelineWarpSource\" "
  13277. "FilePath=\"x.wav\"><AudioObject SelfBPM=\"80.0\" OriginalFileLength=\"1000\">"
  13278. "<Pitch_0 fOrigPitch=\"60.0\" fCurPitch=\"60.0\" fCurVolume=\"1.0\"/></AudioObject></Trk_0></Tracks></NewToneProject>";
  13279. }
  13280. } // namespace newtonetest
  13281. TEST (newtone_state)
  13282. {
  13283. using namespace newtonetest;
  13284. const auto a = lynx::newtonefx::parse (state (0, 5, xmlWithTrack (true), "%FLStudioUserData%\\vox.wav"));
  13285. CHECK (a.has_value());
  13286. CHECK (a->version == 0 && a->editorWidth == 727 && a->editorHeight == 582 && a->slave());
  13287. CHECK (a->sourcePath == "%FLStudioUserData%\\vox.wav");
  13288. CHECK (a->tracks.size() == 1 && a->tracks[0].notes == 2 && a->tracks[0].frames == 1000 && ! a->tracks[0].timeWarp);
  13289. CHECK (a->tracks[0].filePath == "C:\\tmp\\a&b.wav" && a->tracks[0].selfBpm == 120.0);
  13290. CHECK (a->tracks[0].edited && a->edited()); // note 1 moved from 62 to 64
  13291. const auto b = lynx::newtonefx::parse (state (1, 0x41, xmlWithTrack (false), ""));
  13292. CHECK (b.has_value() && ! b->slave() && b->sourcePath.empty());
  13293. CHECK (b->tracks.size() == 1 && b->tracks[0].timeWarp && ! b->edited());
  13294. // an empty project, as the installation's presets
  13295. const auto c = lynx::newtonefx::parse (state (0, 1, "<NewToneProject NumOfTracks=\"0\"><Tracks/></NewToneProject>", ""));
  13296. CHECK (c.has_value() && c->tracks.empty());
  13297. // broken: other version, truncated record, no options record
  13298. CHECK (! lynx::newtonefx::parse (state (2, 1, "", "")));
  13299. auto cut = state (0, 1, xmlWithTrack (true), "");
  13300. cut.resize (cut.size() - 3);
  13301. CHECK (! lynx::newtonefx::parse (cut));
  13302. CHECK (! lynx::newtonefx::parse ({ 0, 0, 0, 0 }));
  13303. // the FL import keeps the state and takes the slave switch as the editable parameter
  13304. const auto raw = state (0, 5, xmlWithTrack (false), "");
  13305. const auto slot = lynx::effectFromFl ("Newtone", raw, 120.0);
  13306. CHECK (slot && slot->kind == lynx::EffectKind::newtone && slot->state == raw && slot->params[0] == 1.0f);
  13307. const auto slot2 = lynx::effectFromFl ("Newtime", state (1, 0x41, xmlWithTrack (false), ""), 120.0);
  13308. CHECK (slot2 && slot2->kind == lynx::EffectKind::newtime && slot2->params[0] == 0.0f);
  13309. CHECK (lynx::formatEffectParam (lynx::EffectKind::newtone, 0, 1.0f, 120.0) == "on");
  13310. // the installation's own states (plugin database and the playlist's pitch/warp correction tracks) [DATA]
  13311. if (! realFlpDir.empty())
  13312. {
  13313. int found = 0, read = 0;
  13314. for (const char* rel : { "System/PitchCorrectionTrack.fst", "System/WarpCorrectionTrack.fst",
  13315. "Patches/Plugin database/Effects/Misc/Newtone.fst", "Patches/Plugin database/Effects/Misc/Newtime.fst" })
  13316. {
  13317. const auto path = std::filesystem::path (realFlpDir) / rel;
  13318. if (! std::filesystem::exists (path))
  13319. continue;
  13320. ++found;
  13321. std::ifstream f (path, std::ios::binary);
  13322. std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (f)), std::istreambuf_iterator<char>());
  13323. const auto fx = lynx::effectFromFlPreset (bytes, 120.0);
  13324. read += fx && (fx->kind == lynx::EffectKind::newtone || fx->kind == lynx::EffectKind::newtime) && ! fx->state.empty()
  13325. && fx->params[0] == 0.0f && lynx::newtonefx::parse (fx->state)->tracks.empty();
  13326. }
  13327. std::printf (" %d Newtone/Newtime states, %d read\n", found, read);
  13328. CHECK (read == found);
  13329. }
  13330. }
  13331. TEST (newtone_engine)
  13332. {
  13333. // REA_rodada73 §3-4 [EXEC]: the input passes unchanged unless slave mode is on and the song plays; then the source
  13334. // sample is added from the song time on (at the source's own rate)
  13335. auto sample = std::make_shared<lynx::SampleData>();
  13336. sample->sampleRate = 44100.0;
  13337. for (int i = 0; i < 3000; ++i)
  13338. sample->left.push_back (0.001f * (float) (i % 97) - 0.04f);
  13339. const double sr = 44100.0, bpm = 120.0;
  13340. const auto tps = lynx::ticksPerSample (bpm, sr);
  13341. auto fx = lynx::newtonefx::make (sample);
  13342. fx->prepare (sr);
  13343. lynx::EffectParams on {}, off {};
  13344. on[0] = 1.0f;
  13345. std::vector<float> l (256), r (256), in (256);
  13346. auto block = [&] (double tick, bool playing, const lynx::EffectParams& p) {
  13347. for (int i = 0; i < 256; ++i)
  13348. l[(std::size_t) i] = r[(std::size_t) i] = in[(std::size_t) i] = 0.25f * std::sin (0.01f * (float) (i + tick));
  13349. fx->setSongPosition (tick);
  13350. fx->setPlaying (playing);
  13351. fx->process (l.data(), r.data(), 256, p, bpm);
  13352. };
  13353. auto same = [&] { return std::equal (l.begin(), l.end(), in.begin()) && std::equal (r.begin(), r.end(), in.begin()); };
  13354. block (0.0, true, off);
  13355. CHECK (same()); // slave off
  13356. block (0.0, false, on);
  13357. CHECK (same()); // not playing
  13358. // playing from the start: in + source, sample by sample, over consecutive blocks
  13359. bool exact = true;
  13360. for (int b = 0; b < 4; ++b)
  13361. {
  13362. block (b * 256 * tps, true, on);
  13363. for (int i = 0; i < 256; ++i)
  13364. exact = exact && l[(std::size_t) i] == in[(std::size_t) i] + sample->left[(std::size_t) (b * 256 + i)]
  13365. && r[(std::size_t) i] == l[(std::size_t) i];
  13366. }
  13367. CHECK (exact);
  13368. // a jump re-seeks to the song time; past the end of the sample only the input remains
  13369. block (2000 * tps, true, on);
  13370. CHECK (l[0] == in[0] + sample->left[2000] && l[255] == in[255] + sample->left[2255]);
  13371. block (2900 * tps, true, on);
  13372. CHECK (l[99] == in[99] + sample->left[2999] && l[100] == in[100]);
  13373. // stopping and playing again: from the new song time
  13374. block (0.0, false, on);
  13375. CHECK (same());
  13376. block (10 * tps, true, on);
  13377. CHECK (l[0] == in[0] + sample->left[10]);
  13378. // without its sample: a pass-through
  13379. auto none = lynx::makeEffect (lynx::EffectKind::newtime);
  13380. none->prepare (sr);
  13381. for (int i = 0; i < 256; ++i)
  13382. l[(std::size_t) i] = r[(std::size_t) i] = in[(std::size_t) i] = 0.1f * (float) i / 256.0f;
  13383. none->setPlaying (true);
  13384. none->process (l.data(), r.data(), 256, on, bpm);
  13385. CHECK (same());
  13386. }
  13387. TEST (newtone_rack_follows_transport)
  13388. {
  13389. // the mixer gives the effect the transport, and keeps it running on a silent insert (no smart disable)
  13390. auto song = lynx::Song::makeDefault();
  13391. for (auto& p : song.patterns)
  13392. p.notes.clear();
  13393. song.bpm = 120.0;
  13394. auto slot = lynx::makeEffectSlot (lynx::EffectKind::newtone);
  13395. slot.params[0] = 1.0f;
  13396. slot.samplePath = "newtone-test.wav";
  13397. song.mixer[1].effects.push_back (slot);
  13398. auto sample = std::make_shared<lynx::SampleData>();
  13399. sample->sampleRate = 48000.0;
  13400. sample->left.assign (48000, 0.5f);
  13401. int loads = 0;
  13402. lynx::RackState::SampleProvider provider = [&] (const std::string& path) -> std::shared_ptr<const lynx::SampleData> {
  13403. ++loads;
  13404. return path == "newtone-test.wav" ? sample : nullptr;
  13405. };
  13406. const double sr = 48000.0;
  13407. auto state = lynx::RackState::build (song, nullptr, sr, provider);
  13408. CHECK (loads == 1);
  13409. lynx::RackRenderer rr;
  13410. rr.prepare (sr);
  13411. std::vector<float> l (512), r (512);
  13412. auto peakOf = [&] (bool playing, double tick) {
  13413. float peak = 0.0f;
  13414. for (int b = 0; b < 8; ++b)
  13415. {
  13416. rr.process (state.get(), playing, tick + b * 512 * lynx::ticksPerSample (song.bpm, sr), 512, l.data(), r.data());
  13417. for (auto v : l)
  13418. peak = std::max (peak, std::abs (v));
  13419. }
  13420. return peak;
  13421. };
  13422. CHECK (peakOf (false, 0.0) == 0.0f);
  13423. CHECK (peakOf (true, 0.0) > 0.1f);
  13424. }
  13425. #include "HardcoreTests.inc"
  13426. #include "DistructorTests.inc"
  13427. // --- missing files (core/MissingFiles.h) ------------------------------------------------------------------------
  13428. TEST (missing_files_names_and_scores)
  13429. {
  13430. CHECK (lynx::pathFileName ("C:\\Samples\\Drums\\Kick 01.wav") == "Kick 01.wav");
  13431. CHECK (lynx::pathFileName ("/home/u/loops/beat.flac") == "beat.flac");
  13432. CHECK (lynx::pathFileName ("D:/mixed\\sep/x.wav/") == "x.wav");
  13433. CHECK (lynx::pathFileName ("plain.wav") == "plain.wav");
  13434. CHECK (lynx::relocationScore ("C:\\Old\\Drums\\Kicks\\kick.wav", "/new/Drums/Kicks/KICK.WAV") == 3);
  13435. CHECK (lynx::relocationScore ("C:\\Old\\Drums\\Kicks\\kick.wav", "/new/other/kick.wav") == 1);
  13436. CHECK (lynx::relocationScore ("C:\\Old\\kick.wav", "/new/kick2.wav") == 0);
  13437. }
  13438. TEST (missing_files_find_in_song)
  13439. {
  13440. lynx::Song song;
  13441. lynx::ChannelDef a, b, c, d, e;
  13442. a.name = "Kick", a.instrument = lynx::InstrumentKind::sampler, a.samplePath = "C:\\gone\\kick.wav";
  13443. b.name = "Vox", b.instrument = lynx::InstrumentKind::audioClip, b.samplePath = "C:\\gone\\vox.wav";
  13444. c.name = "Here", c.instrument = lynx::InstrumentKind::sampler, c.samplePath = "C:\\here\\snare.wav";
  13445. d.name = "GMS", d.instrument = lynx::InstrumentKind::gms, d.samplePath = "C:\\FL\\gms waves"; // installation data: not listed
  13446. e.name = "Kick 2", e.instrument = lynx::InstrumentKind::sampler, e.samplePath = "c:/GONE/KICK.wav"; // same file, other spelling
  13447. lynx::SampleZone z;
  13448. z.path = "C:\\gone\\piano_c4.wav";
  13449. a.zones.push_back (z);
  13450. song.channels = { a, b, c, d, e };
  13451. song.mixer.resize (3);
  13452. lynx::EffectSlot fx;
  13453. fx.samplePath = "C:\\gone\\tune.wav";
  13454. lynx::EffectSlot parallel;
  13455. parallel.branches.push_back ({ { fx }, 1.0f });
  13456. song.mixer[2].effects.push_back (parallel);
  13457. song.mixer[2].name = "Lead";
  13458. const auto exists = [] (const std::string& p) { return p.find ("here") != std::string::npos || p.find ("FL") != std::string::npos; };
  13459. auto missing = lynx::findMissingSamples (song, exists);
  13460. CHECK (missing.size() == 4);
  13461. CHECK (missing.size() == 4 && missing[0].path == "C:\\gone\\kick.wav" && missing[0].usedBy.size() == 2 && missing[0].usedBy[1] == "Kick 2");
  13462. CHECK (missing.size() == 4 && missing[1].path == "C:\\gone\\piano_c4.wav" && missing[1].usedBy[0] == "Kick");
  13463. CHECK (missing.size() == 4 && missing[2].usedBy[0] == "Audio clip: Vox");
  13464. CHECK (missing.size() == 4 && missing[3].usedBy[0] == "Lead: effect");
  13465. // candidates from a search: the deeper path match wins; one stays missing
  13466. const std::vector<std::string> hits { "/lib/other/kick.wav", "/lib/gone/kick.wav", "/lib/x/vox.wav", "/lib/y/tune.wav" };
  13467. CHECK (lynx::relocateByName (missing, hits) == 3);
  13468. CHECK (missing[0].found == "/lib/gone/kick.wav");
  13469. CHECK (missing[1].found.empty());
  13470. missing[3].skipped = true; // skipped: not applied
  13471. const auto map = lynx::relocationMap (missing);
  13472. CHECK (map.size() == 2);
  13473. CHECK (lynx::applyRelocations (song, map) == 3); // both spellings of kick.wav + vox
  13474. CHECK (song.channels[0].samplePath == "/lib/gone/kick.wav" && song.channels[4].samplePath == "/lib/gone/kick.wav");
  13475. CHECK (song.channels[1].samplePath == "/lib/x/vox.wav");
  13476. CHECK (song.channels[2].samplePath == "C:\\here\\snare.wav");
  13477. CHECK (song.mixer[2].effects[0].branches[0].chain[0].samplePath == "C:\\gone\\tune.wav");
  13478. lynx::Song copy = song;
  13479. CHECK (lynx::applyRelocations (copy, { { "C:\\gone\\tune.wav", "/t.wav" } }) == 1 && copy.mixer[2].effects[0].branches[0].chain[0].samplePath == "/t.wav");
  13480. }
  13481. TEST (missing_files_folder_search)
  13482. {
  13483. namespace fs = std::filesystem;
  13484. const auto root = fs::temp_directory_path() / "lynx-missing-test";
  13485. fs::remove_all (root);
  13486. fs::create_directories (root / "a" / "deep" / "Kicks");
  13487. fs::create_directories (root / "b");
  13488. for (const auto& f : { root / "a" / "deep" / "Kicks" / "Kick.WAV", root / "b" / "kick.wav", root / "b" / "hat.wav", root / "b" / "unrelated.wav" })
  13489. std::ofstream (f) << "x";
  13490. std::vector<lynx::MissingFile> missing { { "C:\\Old\\Kicks\\kick.wav", { "Kick" }, {}, false }, { "C:\\Old\\hat.wav", { "Hat" }, {}, false },
  13491. { "C:\\Old\\ride.wav", { "Ride" }, {}, false } };
  13492. int listed = 0;
  13493. auto list = [&] (const std::string& folder, const std::function<bool (const std::string&, bool)>& onEntry) {
  13494. for (const auto& entry : fs::directory_iterator (folder))
  13495. {
  13496. ++listed;
  13497. if (! onEntry (entry.path().string(), entry.is_directory()))
  13498. return;
  13499. }
  13500. };
  13501. const auto hits = lynx::searchFolderForMissing (missing, root.string(), list);
  13502. CHECK (hits.size() == 3);
  13503. CHECK (lynx::relocateByName (missing, hits) == 2);
  13504. CHECK (missing[0].found.find ("Kicks") != std::string::npos); // the copy in a "Kicks" folder matches better
  13505. CHECK (missing[1].found.find ("hat.wav") != std::string::npos);
  13506. CHECK (missing[2].found.empty());
  13507. // a bounded walk stops early; a cancelled one finds nothing
  13508. CHECK (lynx::searchFolderForMissing (missing, root.string(), list, 2).size() <= 1);
  13509. CHECK (lynx::searchFolderForMissing (missing, root.string(), list, 0, [] { return true; }).empty());
  13510. // nothing left to find: no walk at all
  13511. missing[2].skipped = true;
  13512. listed = 0;
  13513. CHECK (lynx::searchFolderForMissing (missing, root.string(), list).empty() && listed == 0);
  13514. fs::remove_all (root);
  13515. }
  13516. TEST (missing_files_folder_list)
  13517. {
  13518. const auto folders = lynx::parseFolderList ("C:\\A\r\n C:\\B \n\nc:\\a\nD:/x\n");
  13519. CHECK (folders.size() == 3 && folders[0] == "C:\\A" && folders[1] == "C:\\B" && folders[2] == "D:/x");
  13520. auto list = folders;
  13521. lynx::rememberFolder (list, "D:\\x");
  13522. CHECK (list.size() == 3 && list[0] == "D:\\x" && list[1] == "C:\\A");
  13523. lynx::rememberFolder (list, "E:\\new", 2);
  13524. CHECK (list.size() == 2 && list[0] == "E:\\new" && list[1] == "D:\\x");
  13525. CHECK (lynx::parseFolderList (lynx::folderListText (list)) == list);
  13526. }
  13527. TEST (missing_files_flp_report_and_relocation)
  13528. {
  13529. // an .flp sampler whose sample is gone: listed with its channel; a relocation map makes the import use the copy
  13530. flp::Project project;
  13531. flp::Channel ch;
  13532. ch.index = 0;
  13533. ch.type = flp::ChannelType::sampler;
  13534. ch.name = "Snare";
  13535. ch.samplePath = "C:\\Producer\\Packs\\snare 7.wav";
  13536. project.channels.push_back (ch);
  13537. lynx::FlpImportOptions options;
  13538. options.fileExists = [] (const std::string& p) { return p == "/found/snare 7.wav"; };
  13539. lynx::FlpImportReport report;
  13540. options.report = &report;
  13541. auto song = lynx::importFlp (project, options);
  13542. CHECK (report.missingFiles.size() == 1);
  13543. CHECK (report.missingFiles.size() == 1 && report.missingFiles[0].path == "C:\\Producer\\Packs\\snare 7.wav" && report.missingFiles[0].usedBy[0] == "Snare");
  13544. CHECK (song.channels.size() == 1 && song.channels[0].samplePath.empty());
  13545. lynx::FlpImportReport again;
  13546. options.report = &again;
  13547. options.relocations = { { "C:\\Producer\\Packs\\snare 7.wav", "/found/snare 7.wav" } };
  13548. song = lynx::importFlp (project, options);
  13549. CHECK (again.missingFiles.empty());
  13550. CHECK (song.channels.size() == 1 && song.channels[0].samplePath == "/found/snare 7.wav" && song.channels[0].instrument == lynx::InstrumentKind::sampler);
  13551. }
  13552. TEST (project_info_history_and_flp_import)
  13553. {
  13554. lynx::Song a;
  13555. auto b = a;
  13556. b.info.title = "Night Drive";
  13557. CHECK (lynx::describeChangeText (a, b) == "Project info");
  13558. auto c = a;
  13559. c.beatsPerBar = 3;
  13560. CHECK (lynx::describeChangeText (a, c) == "Time signature");
  13561. flp::Project project;
  13562. project.title = "Song", project.artist = "Me", project.genre = "House", project.url = "https://x", project.comments = "line 1\rline 2";
  13563. const auto song = lynx::importFlp (project);
  13564. CHECK (song.info.title == "Song" && song.info.author == "Me" && song.info.genre == "House" && song.info.url == "https://x");
  13565. CHECK (song.info.comments == "line 1\nline 2");
  13566. }
  13567. #include "UiFixTests.inc"
  13568. // ---------------------------------------------------------------------------
  13569. // Piano Roll tools of round 83 (expected values worked out from FL's formulas, docs/analise/REA_rodada83.md)
  13570. namespace
  13571. {
  13572. lynx::Note prNote (std::int64_t start, std::int64_t length, int key = 60, float velocity = 100.0f / 128.0f)
  13573. {
  13574. lynx::Note n;
  13575. n.start = start;
  13576. n.length = length;
  13577. n.key = key;
  13578. n.velocity = velocity;
  13579. return n;
  13580. }
  13581. std::vector<int> allOf (const lynx::Pattern& p)
  13582. {
  13583. std::vector<int> v;
  13584. for (int i = 0; i < (int) p.notes.size(); ++i)
  13585. v.push_back (i);
  13586. return v;
  13587. }
  13588. } // namespace
  13589. TEST (prtools_score_file)
  13590. {
  13591. using namespace lynx::prtools;
  13592. FlpBuilder b (96);
  13593. b.ascii (0xC7, "20.9.0");
  13594. b.u8 (0x41); b.u16 (0);
  13595. auto notes = noteRecord (0, 0, 24, 60, 100);
  13596. auto n2 = noteRecord (48, 0, 24, 67, 64);
  13597. n2[19] = 4; // colour 4
  13598. notes.insert (notes.end(), n2.begin(), n2.end());
  13599. b.data (0xE0, notes);
  13600. const auto score = parseScore (b.bytes);
  13601. CHECK (score && score->notes.size() == 2);
  13602. if (score && score->notes.size() == 2)
  13603. {
  13604. CHECK (score->notes[0].start == 0 && score->notes[0].length == 240 && score->notes[0].key == 60);
  13605. CHECK (score->notes[1].start == 480 && score->notes[1].colour == 4 && score->notes[1].velocity == 0.5f);
  13606. CHECK (score->length() == 720);
  13607. }
  13608. CHECK (! parseScore (std::vector<std::uint8_t> { 1, 2, 3 }));
  13609. const auto grid = gridScore (240);
  13610. CHECK (grid.notes.size() == 4 && grid.length() == 960 && grid.notes[3].start == 720);
  13611. CHECK (gridScore (240, 1.0f).notes[1].start == 240 + 120);
  13612. if (! realFlpDir.empty()) // the installation's templates
  13613. {
  13614. std::ifstream f (realFlpDir + "/Patches/Scores/Quantization/Default.fsc", std::ios::binary);
  13615. std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (f)), {});
  13616. const auto fsc = parseScore (bytes);
  13617. CHECK (fsc && fsc->notes.size() == 16 && fsc->length() == 3840 && fsc->notes[1].start == 240);
  13618. // an old (FL 3.5, 20-byte records) chopping pattern with colour-3 notes, read as colour 12
  13619. std::ifstream g (realFlpDir + "/Patches/Scores/Chopping/Default.fsc", std::ios::binary);
  13620. std::vector<std::uint8_t> chopBytes ((std::istreambuf_iterator<char> (g)), {});
  13621. const auto chop = parseScore (chopBytes);
  13622. CHECK (chop && ! chop->notes.empty());
  13623. }
  13624. }
  13625. TEST (prtools_quantize)
  13626. {
  13627. using namespace lynx;
  13628. using namespace lynx::prtools;
  13629. const auto grid = gridScore (240);
  13630. Pattern p;
  13631. p.notes = { prNote (1010, 500), prNote (1130, 100) };
  13632. QuantizeOptions o; // start strength 64/128, sensitivity 36 (180 ticks)
  13633. quantize (p, allOf (p), grid, o);
  13634. CHECK (p.notes[0].start == 985 && p.notes[0].length == 500); // 1010 - (50 * 64) / 128
  13635. CHECK (p.notes[1].start == 1165); // toward 1200: 1130 + (70 * 64) / 128
  13636. p.notes = { prNote (1010, 500), prNote (1130, 100) };
  13637. o.start = 1.0f;
  13638. o.sensitivity = 12; // 60 ticks: 1130 is 70 away from 1200
  13639. quantize (p, allOf (p), grid, o);
  13640. CHECK (p.notes[0].start == 960 && p.notes[1].start == 1130);
  13641. p.notes = { prNote (1010, 500) };
  13642. o.sensitivity = 36;
  13643. o.durationMode = QuantizeOptions::leaveEnd;
  13644. quantize (p, allOf (p), grid, o);
  13645. CHECK (p.notes[0].start == 960 && p.notes[0].length == 550); // the end (1509) stays
  13646. p.notes = { prNote (1010, 500) };
  13647. o.durationMode = QuantizeOptions::quantizeEnd;
  13648. o.duration = 1.0f;
  13649. quantize (p, allOf (p), grid, o);
  13650. CHECK (p.notes[0].start == 960 && p.notes[0].length == 1439 - 960 + 1); // end 1509 -> the nearest end 1439
  13651. // levels: a template velocity of 50/100 halves the velocity; 0 removes the note
  13652. Score half = grid;
  13653. for (auto& n : half.notes)
  13654. n.velocity = 50.0f / 128.0f;
  13655. p.notes = { prNote (960, 100, 60, 0.8f) };
  13656. o = {};
  13657. quantize (p, allOf (p), half, o);
  13658. CHECK (std::abs (p.notes[0].velocity - 0.4f) < 1e-5f);
  13659. for (auto& n : half.notes)
  13660. n.velocity = 0.0f;
  13661. quantize (p, allOf (p), half, o);
  13662. CHECK (p.notes.empty());
  13663. }
  13664. TEST (prtools_chop)
  13665. {
  13666. using namespace lynx;
  13667. using namespace lynx::prtools;
  13668. Score pat;
  13669. pat.notes = { ScoreNote { 0, 240, 60 }, ScoreNote { 480, 240, 67 } }; // period 720
  13670. Pattern p;
  13671. p.notes = { prNote (0, 1920, 50) };
  13672. ChopOptions o;
  13673. chop (p, allOf (p), pat, o);
  13674. CHECK (p.notes.size() == 5);
  13675. if (p.notes.size() == 5)
  13676. {
  13677. CHECK (p.notes[0].start == 0 && p.notes[0].key == 50 && p.notes[1].start == 480 && p.notes[1].key == 57);
  13678. CHECK (p.notes[2].start == 720 && p.notes[3].start == 1200 && p.notes[4].start == 1440 && p.notes[4].length == 240);
  13679. }
  13680. // absolute: the pattern stays on the song grid; a piece before the note start is cut
  13681. p.notes = { prNote (100, 600, 50) };
  13682. chop (p, allOf (p), pat, o);
  13683. CHECK (p.notes.size() == 2 && p.notes[0].start == 100 && p.notes[0].length == 140 && p.notes[1].start == 480);
  13684. o.absolute = false;
  13685. p.notes = { prNote (100, 600, 50) };
  13686. chop (p, allOf (p), pat, o);
  13687. CHECK (p.notes.size() == 2 && p.notes[0].start == 100 && p.notes[0].length == 240 && p.notes[1].start == 580
  13688. && p.notes[1].length == 120);
  13689. // time mul x2 (wheel 5); colour 4 holds to the end in the first round only
  13690. o = {};
  13691. o.timeMul = 5;
  13692. p.notes = { prNote (0, 1920, 60) };
  13693. chop (p, allOf (p), pat, o);
  13694. CHECK (p.notes.size() == 3 && p.notes[1].start == 960 && p.notes[1].length == 480 && p.notes[2].start == 1440);
  13695. pat.notes[1].colour = 4;
  13696. o.timeMul = 4;
  13697. p.notes = { prNote (0, 1920, 60) };
  13698. chop (p, allOf (p), pat, o);
  13699. CHECK (p.notes.size() == 4 && p.notes[1].length == 1920 - 480 && p.notes[3].start == 1440);
  13700. }
  13701. TEST (prtools_flam)
  13702. {
  13703. using namespace lynx;
  13704. using namespace lynx::prtools;
  13705. Pattern p;
  13706. p.notes = { prNote (960, 480, 60, 0.75f) };
  13707. flam (p, allOf (p), flamPreset (FlamPreset::doubleStroke), 120.0); // +1 step after
  13708. CHECK (p.notes.size() == 2 && p.notes[0].start == 960 && p.notes[1].start == 1200 && p.notes[1].length == 480);
  13709. CHECK (std::abs (p.notes[1].velocity - 0.75f * 85.0f / 128.0f) < 1e-6f);
  13710. p.notes = { prNote (960, 480) };
  13711. flam (p, allOf (p), flamPreset (FlamPreset::earlyDoubleStroke), 120.0); // the flam one step before, shortened
  13712. CHECK (p.notes.size() == 2 && p.notes[0].start == 720 && p.notes[0].length == 240 && p.notes[1].start == 960);
  13713. // absolute: -64 = -33.3 ms = -64 ticks at 120 BPM (960 PPQ); the original moves, the flam stays on the grid
  13714. CHECK (flamOffset (flamPreset (FlamPreset::flam), 120.0) == -64);
  13715. p.notes = { prNote (960, 480) };
  13716. flam (p, allOf (p), flamPreset (FlamPreset::flam), 120.0);
  13717. CHECK (p.notes.size() == 2 && p.notes[0].start == 960 && p.notes[0].length == 64 && p.notes[1].start == 1024);
  13718. p.notes = { prNote (960, 480) };
  13719. flam (p, allOf (p), flamPreset (FlamPreset::earlyFlam), 120.0);
  13720. CHECK (p.notes.size() == 2 && p.notes[0].start == 896 && p.notes[1].start == 960 && p.notes[1].length == 480);
  13721. }
  13722. TEST (prtools_lfo)
  13723. {
  13724. using namespace lynx;
  13725. using namespace lynx::prtools;
  13726. CHECK (lfoPeriod (20480) == 3840.0 && lfoPeriod (0) == 960.0 * 128 && lfoPeriod (40960) == 120.0);
  13727. LfoOptions o; // sine, phase 1/2: starts at the bottom (value 1/2, range 1/4)
  13728. const auto c = lfoCurve (o, 3840);
  13729. CHECK (std::abs (c[0] - 0.25f) < 1e-4f && std::abs (c[960] - 0.5f) < 1e-3f && std::abs (c[1920] - 0.75f) < 1e-3f);
  13730. o.shape = LfoOptions::square;
  13731. o.phase = 0; // phase 0.5: Round (0.5) = 0 (half to even) -> +1, then -1
  13732. const auto sq = lfoCurve (o, 3840);
  13733. CHECK (sq[0] == 0.75f && sq[10] == 0.25f && sq[2000] == 0.75f);
  13734. o = {};
  13735. o.useEnd = true;
  13736. o.endRange = 0; // the range fades out
  13737. const auto fade = lfoCurve (o, 3840);
  13738. CHECK (std::abs (fade[3839] - 0.5f) < 0.01f);
  13739. Pattern p;
  13740. p.notes = { prNote (0, 100), prNote (960, 100), prNote (1920, 100) };
  13741. lfo (p, allOf (p), notetools::Level::velocity, LfoOptions {});
  13742. CHECK (std::abs (p.notes[0].velocity - 0.25f) < 1e-4f && std::abs (p.notes[1].velocity - 0.5f) < 1e-3f
  13743. && std::abs (p.notes[2].velocity - 0.75f) < 1e-3f);
  13744. }
  13745. TEST (prtools_claw)
  13746. {
  13747. using namespace lynx;
  13748. using namespace lynx::prtools;
  13749. ClawOptions o; // beat, trash the 4th quarter, stretch, remove short notes
  13750. Pattern p;
  13751. p.notes = { prNote (0, 240), prNote (960, 240), prNote (750, 100), prNote (1440, 240) };
  13752. claw (p, allOf (p), o);
  13753. CHECK (p.notes.size() == 3); // the note inside the trashed slice is gone
  13754. if (p.notes.size() == 3)
  13755. {
  13756. CHECK (p.notes[0].start == 0 && p.notes[0].length == 320);
  13757. CHECK (p.notes[1].start == 960 && p.notes[1].length == 320);
  13758. CHECK (p.notes[2].start == 1600 && p.notes[2].length == 320); // 1440 - 240 = 1200 -> x 4/3
  13759. }
  13760. o.trash = 9; // none: only the warp, which is neutral
  13761. p.notes = { prNote (100, 300) };
  13762. claw (p, allOf (p), o);
  13763. CHECK (p.notes.size() == 1 && p.notes[0].start == 100 && p.notes[0].length == 300);
  13764. o.timeDistortion = 1024; // strong bend: the period's ends stay, the middle moves
  13765. p.notes = { prNote (0, 480), prNote (960, 480) };
  13766. claw (p, allOf (p), o);
  13767. CHECK (p.notes[0].start == 0 && p.notes[1].start == 960 && p.notes[0].length > 480 && p.notes[1].length > 480);
  13768. o.trash = 0; // the first quarter
  13769. o.timeDistortion = 0;
  13770. o.stretch = false;
  13771. p.notes = { prNote (0, 240), prNote (240, 240) };
  13772. claw (p, allOf (p), o);
  13773. CHECK (p.notes.size() == 1 && p.notes[0].start == 0 && p.notes[0].length == 240);
  13774. }
  13775. TEST (prtools_articulate)
  13776. {
  13777. using namespace lynx;
  13778. using namespace lynx::prtools;
  13779. Pattern p;
  13780. p.notes = { prNote (0, 100), prNote (480, 100), prNote (960, 100) };
  13781. articulate (p, allOf (p), articulatePreset (ArticulatePreset::legato), 4);
  13782. CHECK (p.notes[0].length == 480 && p.notes[1].length == 480 && p.notes[2].length == 3840 - 960); // to the bar line
  13783. p.notes = { prNote (0, 100), prNote (480, 100), prNote (960, 2880) };
  13784. articulate (p, allOf (p), articulatePreset (ArticulatePreset::staccato), 4);
  13785. CHECK (p.notes[0].length == 96 && p.notes[1].length == 96 && p.notes[2].length == 576); // ends on a bar: own length
  13786. auto o = articulatePreset (ArticulatePreset::legato);
  13787. o.gap = 20;
  13788. p.notes = { prNote (0, 100), prNote (480, 100) };
  13789. articulate (p, allOf (p), o, 4);
  13790. CHECK (p.notes[0].length == 460);
  13791. // chop chords: a held note is cut where the next one starts
  13792. p.notes = { prNote (0, 960, 48), prNote (480, 480, 60) };
  13793. articulate (p, allOf (p), articulatePreset (ArticulatePreset::chopChordsOnly), 4);
  13794. CHECK (p.notes.size() == 3 && p.notes[0].length == 480 && p.notes[1].start == 480 && p.notes[2].start == 480);
  13795. // variation is seeded (MT19937): same seed, same result; lengths only get shorter
  13796. o = articulatePreset (ArticulatePreset::legato);
  13797. o.variation = 50;
  13798. o.seed = 7;
  13799. Pattern a, b2;
  13800. a.notes = b2.notes = { prNote (0, 100), prNote (480, 100), prNote (960, 100) };
  13801. articulate (a, allOf (a), o, 4);
  13802. articulate (b2, allOf (b2), o, 4);
  13803. CHECK (a.notes[0].length == b2.notes[0].length && a.notes[0].length <= 480 && a.notes[0].length >= 240);
  13804. }
  13805. TEST (prtools_chords)
  13806. {
  13807. using namespace lynx::prtools;
  13808. auto c = parseProgression ("I-V-vi-IV", 0, false);
  13809. CHECK (c && c->size() == 4);
  13810. if (c && c->size() == 4)
  13811. {
  13812. CHECK ((*c)[0].root == 0 && (*c)[1].root == 7 && (*c)[2].root == 9 && (*c)[3].root == 5);
  13813. CHECK ((*c)[2].intervals == std::vector<int> ({ 0, 3, 7 }) && chordName ((*c)[2]) == "Am");
  13814. }
  13815. c = parseProgression ("ii 7-V 7-I \xE2\x88\x86-IV/\xE2\x99\xADVII", 0, false);
  13816. CHECK (c && c->size() == 4);
  13817. if (c && c->size() == 4)
  13818. {
  13819. CHECK ((*c)[0].intervals == std::vector<int> ({ 0, 3, 7, 10 }) && chordName ((*c)[0]) == "Dm7");
  13820. CHECK ((*c)[2].intervals == std::vector<int> ({ 0, 4, 7, 11 }) && chordName ((*c)[2]) == "Cmaj7");
  13821. CHECK ((*c)[3].root == 5 && (*c)[3].bass == 10 && chordName ((*c)[3]) == "F/A#");
  13822. }
  13823. c = parseProgression ("i-v-\xE2\x99\xADVI-iv", 9, true); // A minor: Am Em F Dm
  13824. CHECK (c && c->size() == 4 && (*c)[2].root == 5 && (*c)[3].root == 2 && (*c)[1].intervals[1] == 3);
  13825. CHECK (parseProgression ("Isus2 add9", 0, false) && parseProgression ("Isus2 add9", 0, false)->front().intervals
  13826. == std::vector<int> ({ 0, 2, 7, 14 }));
  13827. CHECK (! parseProgression ("I-X-V", 0, false));
  13828. const auto presets = parseProgressionPresets ("-Major\r\n\r\nI-V-vi-IV: Hopeful\r\nI-vi-IV-V: Optimistic\r\n\r\n-Minor\r\n\r\ni-iv-v-i\r\n");
  13829. CHECK (presets.size() == 3 && presets[0].numerals == "I-V-vi-IV" && ! presets[0].minor && presets[2].minor);
  13830. const auto rhythms = parseRhythmPresets ("Default,4,>,-,-,-,>,-,-,-,>,-,-,-,>,-,-,-\nRandom 1,4,r,16,2,1.5\n");
  13831. CHECK (rhythms.size() == 1 && rhythms[0].cells == ">--->--->--->---");
  13832. const auto g = generateProgression (0, false, 8, 0.3f, 5);
  13833. CHECK (g.size() == 8 && g[0].root == 0 && g == generateProgression (0, false, 8, 0.3f, 5));
  13834. for (std::uint32_t seed = 1; seed < 40; ++seed) // numerals read back as the same chords
  13835. {
  13836. const auto gen = generateProgression (2, seed % 2 == 0, 8, 1.0f, seed);
  13837. const auto back = parseProgression (toNumerals (gen, 2), 2, seed % 2 == 0);
  13838. CHECK (back && *back == gen);
  13839. }
  13840. CHECK (toNumerals (*parseProgression ("I-V-vi-IV/bVII", 0, false), 0) == "I-V-vi-IV/bVII");
  13841. lynx::Pattern p;
  13842. const auto chords = *parseProgression ("I-IV", 0, false);
  13843. ChordWriteOptions w;
  13844. const auto added = writeProgression (p, 2, 0, chords, w);
  13845. CHECK (added.size() == 8 && p.notes.size() == 8 && p.notes[0].channel == 2);
  13846. CHECK (std::any_of (p.notes.begin(), p.notes.end(), [] (auto& n) { return n.key == 48; })); // bass C3
  13847. w.rhythm = ">-`->-`->-`->-`-";
  13848. lynx::Pattern r;
  13849. writeProgression (r, 0, 0, chords, w);
  13850. CHECK (r.notes.size() == 8 && r.notes[0].length == 1920); // '>' + '-' of 960-tick cells
  13851. }
  13852. TEST (prtools_riff_machine)
  13853. {
  13854. using namespace lynx;
  13855. using namespace lynx::prtools;
  13856. Pattern p;
  13857. p.notes = { prNote (0, 100, 70) };
  13858. p.notes[0].channel = 1;
  13859. RiffOptions o;
  13860. o.channel = 1;
  13861. o.bars = 2;
  13862. const auto notes = riffMachine (p, { 0 }, o, 4);
  13863. CHECK (! notes.empty() && notes.size() == p.notes.size());
  13864. CHECK (std::all_of (p.notes.begin(), p.notes.end(), [] (auto& n) {
  13865. return n.channel == 1 && n.start >= 0 && n.start < 2 * 3840 && n.key >= 36 && n.key <= 84;
  13866. }));
  13867. // only the first step: the progression shape, chopped from one note at key 48
  13868. o.stepsUpTo = 0;
  13869. Pattern q;
  13870. const auto first = riffMachine (q, {}, o, 4);
  13871. CHECK (first.size() == builtInRiffProgression (0).notes.size() * 2 && q.notes[0].key == 48);
  13872. }
  13873. TEST (event_editor_lanes)
  13874. {
  13875. using namespace lynx;
  13876. Pattern p;
  13877. const AutomationTarget vol { AutomationTargetKind::channelVolume, 2 };
  13878. auto& lane = eventedit::laneFor (p, vol);
  13879. CHECK (p.events.size() == 1 && &eventedit::laneFor (p, vol) == &lane && eventedit::findLane (p, { AutomationTargetKind::channelPan, 2 }) == nullptr);
  13880. eventedit::fill (lane, 0, 960, 0.25f, 240);
  13881. CHECK (lane.events.size() == 4 && lane.events[3].tick == 720 && lane.events[3].value == 0.25f);
  13882. CHECK (lane.valueAt (-5.0) == 0.25f && lane.valueAt (5000.0) == 0.25f);
  13883. eventedit::line (lane, 960, 1920, 0.0f, 1.0f, 240); // 4 cells: 0, 1/3, 2/3, 1
  13884. CHECK (lane.events.size() == 8 && lane.events[4].value == 0.0f && lane.events[7].value == 1.0f);
  13885. CHECK (std::abs (lane.valueAt (1500.0) - 2.0f / 3.0f) < 1e-6f); // held from 1440
  13886. eventedit::setEvent (lane, 1440, 0.1f);
  13887. CHECK (lane.events.size() == 8 && lane.valueAt (1440.0) == 0.1f);
  13888. eventedit::setEvent (lane, 100, 0.9f);
  13889. CHECK (lane.events.size() == 9 && lane.events[1].tick == 100);
  13890. eventedit::scale (lane, 0, 960, 2.0f, 0.0f, false);
  13891. CHECK (lane.events[0].value == 0.5f && lane.events[1].value == 1.0f); // clamped
  13892. eventedit::scale (lane, 0, 960, 0.0f, 0.0f, true); // bipolar: toward the centre
  13893. CHECK (lane.events[0].value == 0.5f && lane.events[2].value == 0.5f);
  13894. eventedit::erase (lane, 0, 960);
  13895. CHECK (lane.events.size() == 4 && lane.events.front().tick == 960);
  13896. auto copy = lane;
  13897. eventedit::randomize (lane, 0, 4000, 0.2f, 3);
  13898. eventedit::randomize (copy, 0, 4000, 0.2f, 3);
  13899. CHECK (lane.events[1].value == copy.events[1].value && std::abs (lane.events[1].value - 1.0f / 3.0f) <= 0.2f + 1e-6f);
  13900. prtools::LfoOptions o;
  13901. eventedit::applyLfo (lane, 0, 3840, o, 960); // FL's LFO: bottom, middle, top, middle
  13902. CHECK (lane.events.size() == 4 && std::abs (lane.events[0].value - 0.25f) < 1e-4f && std::abs (lane.events[2].value - 0.75f) < 1e-3f);
  13903. p.events.push_back ({ { AutomationTargetKind::channelPan, 1 }, {} });
  13904. eventedit::removeEmptyLanes (p);
  13905. CHECK (p.events.size() == 1);
  13906. // undo label
  13907. auto before = Song::makeDefault();
  13908. auto after = before;
  13909. after.patterns[0].events.push_back ({ vol, { { 0, 0.5f } } });
  13910. CHECK (describeChangeText (before, after) == "Edit events: " + after.patterns[0].name);
  13911. // channel removal re-targets the lanes like automations
  13912. auto song = Song::makeDefault();
  13913. song.patterns[0].events.push_back ({ { AutomationTargetKind::channelVolume, 3 }, { { 0, 0.5f } } });
  13914. song.removeChannel (1);
  13915. CHECK (song.patterns[0].events[0].target.index == 2);
  13916. }
  13917. TEST (event_editor_playback)
  13918. {
  13919. // pattern events on the Master volume: silent first half of each pattern, sound in the second half
  13920. auto song = lynx::Song::makeDefault();
  13921. song.bpm = 120.0;
  13922. const auto bar = (std::int64_t) lynx::ppq * 4;
  13923. song.patterns[0].lengthTicks = bar;
  13924. song.patterns[0].notes.clear();
  13925. for (int s = 0; s < 16; ++s)
  13926. song.patterns[0].notes.push_back ({ s * 240, 200, 0, 60 });
  13927. song.patterns[0].events.push_back ({ { lynx::AutomationTargetKind::mixerVolume, 0 }, { { 0, 0.0f }, { bar / 2, 0.8f } } });
  13928. const auto peaks = [&] (lynx::PlayMode mode) {
  13929. song.mode = mode;
  13930. song.playlist.clear();
  13931. song.playlist.push_back ({ 0, 0, bar, bar, 0 }); // one clip, in the second bar
  13932. const double sr = 48000.0;
  13933. auto state = lynx::RackState::build (song, nullptr, sr);
  13934. lynx::RackRenderer r;
  13935. r.prepare (sr, 512);
  13936. std::vector<float> l (512), rr (512);
  13937. const auto tps = lynx::ticksPerSample (song.bpm, sr);
  13938. std::array<float, 6> peak {}; // half bars
  13939. for (double tick = 0.0; tick < (double) (bar * 3); tick += 512 * tps)
  13940. {
  13941. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  13942. // blocks inside a half bar, away from its edges (the gain glides over one chunk at a change)
  13943. const auto blockEnd = tick + 512 * tps;
  13944. const auto half = (std::size_t) (tick / (double) (bar / 2));
  13945. const auto edge = (double) half * (double) (bar / 2);
  13946. if (half > 5 || tick - edge < 2 * 512 * tps || blockEnd > edge + (double) (bar / 2))
  13947. continue;
  13948. for (auto v : l)
  13949. peak[half] = std::max (peak[half], std::abs (v));
  13950. }
  13951. return std::make_pair (state->eventLanes.size(), peak);
  13952. };
  13953. const auto [lanesSong, song3] = peaks (lynx::PlayMode::song);
  13954. CHECK (lanesSong == 1);
  13955. CHECK (song3[2] < 1.0e-4f && song3[3] > 0.01f); // inside the clip: silent, then sound
  13956. const auto [lanesPattern, loop] = peaks (lynx::PlayMode::pattern);
  13957. CHECK (lanesPattern == 1);
  13958. CHECK (loop[0] < 1.0e-4f && loop[1] > 0.01f && loop[2] < 1.0e-4f && loop[3] > 0.01f);
  13959. }
  13960. #include "PrefsTests.inc"
  13961. TEST (level_audit_demo_imports_round89)
  13962. {
  13963. // REA_rodada89 §4: two demo projects whose FL renders were much louder than LYNX's
  13964. if (realFlpDir.empty())
  13965. return;
  13966. auto load = [] (const std::filesystem::path& path) -> std::optional<lynx::Song> {
  13967. if (! std::filesystem::exists (path))
  13968. return std::nullopt;
  13969. std::ifstream in (path, std::ios::binary);
  13970. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  13971. return lynx::importFlp (flp::parseProject (file));
  13972. };
  13973. const auto demos = std::filesystem::path (realFlpDir) / "Demo projects";
  13974. // FL 12+: a route to insert 100+ without a level record plays at 100% (the percussion bus of "Lightning")
  13975. if (const auto song = load (demos / "Product demos" / "Square a Saw - Lightning (VEELA Vocals Resynthesized).flp"))
  13976. {
  13977. int highRoutes = 0, highAtZero = 0;
  13978. for (const auto& t : song->mixer)
  13979. for (const auto& send : t.sends)
  13980. if (send.target >= 100)
  13981. {
  13982. ++highRoutes;
  13983. highAtZero += send.level <= 0.0f;
  13984. }
  13985. std::printf (" Lightning: %d routes to inserts 100+, %d at level 0\n", highRoutes, highAtZero);
  13986. CHECK (highRoutes > 0 && highAtZero == 0);
  13987. }
  13988. // Fruity Compressors FL saved as VST2 inside Fruity Wrapper (RawFL): the exact engine with the saved controls
  13989. if (const auto song = load (demos / "Visual" / "ZGameEditor Visualizer" / "Treben - Gimbal and Sinan - RawFL.flp"))
  13990. {
  13991. int compressors = 0, withSettings = 0;
  13992. for (const auto& t : song->mixer)
  13993. for (const auto& fx : t.effects)
  13994. if (fx.kind == lynx::EffectKind::fruityCompressor)
  13995. {
  13996. ++compressors;
  13997. const auto defaults = lynx::makeEffectSlot (lynx::EffectKind::fruityCompressor);
  13998. withSettings += fx.params != defaults.params;
  13999. }
  14000. std::printf (" RawFL: %d wrapped Fruity Compressors on the exact engine, %d with their own settings\n", compressors, withSettings);
  14001. CHECK (compressors >= 10 && withSettings >= 8);
  14002. }
  14003. }
  14004. #include "DialogsTests84.inc"