| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601 |
- // Core tests (no JUCE). Usage: lynx_tests [folder-with-flp-files]
- #include <atomic>
- #include <cctype>
- #include <cmath>
- #include <cstdio>
- #include <cstring>
- #include <filesystem>
- #include <map>
- #include <set>
- #include <sstream>
- #include <fstream>
- #include <functional>
- #include <iterator>
- #include <string>
- #include <tuple>
- #include <vector>
- #include "core/Dither.h"
- #include "core/DrumSynthLive.h"
- #include "core/Effects.h"
- #include "core/Effector.h"
- #include "core/FlpImport.h"
- #include "core/FlEffects.h"
- #include "core/FlPatcher.h"
- #include "core/FlSamplers.h"
- #include "core/FlSynths.h"
- #include "core/Inflate.h"
- #include "core/WavPack.h"
- #include "core/FlWrapper.h"
- #include "core/Gms.h"
- #include "core/MiniSynth.h"
- #include "core/Harmless.h"
- #include "core/Harmor.h"
- #include "core/Morphine.h"
- #include "core/Ogun.h"
- #include "core/Dx10.h"
- #include "core/Kepler.h"
- #include "core/HarmlessFx.h"
- #include "core/MaximusFx.h"
- #include "core/FruityDelay.h"
- #include "core/LiveEventQueue.h"
- #include "core/RackRenderer.h"
- #include "core/Scheduler.h"
- #include "core/SnapshotExchange.h"
- #include "core/SoftClip.h"
- #include "core/Slicer.h"
- #include "core/SytrusEngine.h"
- #include "core/Templates.h"
- #include "core/Vocodex.h"
- #include "core/TimeStretch.h"
- #include "core/WorkerPool.h"
- #include "core/Song.h"
- #include "core/NoteEdit.h"
- #include "core/Recorder.h"
- #include "core/KeyMap.h"
- #include "core/RackOps.h"
- #include "core/ValueMath.h"
- #include "core/Workspace.h"
- #include "core/StartupSound.h"
- #include "flp/Flp.h"
- #include "ui/I18nData.h"
- namespace
- {
- int failures = 0;
- int checks = 0;
- std::vector<std::pair<const char*, std::function<void()>>>& registry()
- {
- static std::vector<std::pair<const char*, std::function<void()>>> r;
- return r;
- }
- struct Register
- {
- Register (const char* name, std::function<void()> fn) { registry().emplace_back (name, std::move (fn)); }
- };
- #define TEST(name) \
- static void name(); \
- static Register reg_##name (#name, name); \
- static void name()
- #define CHECK(cond) \
- do \
- { \
- ++checks; \
- if (! (cond)) \
- { \
- ++failures; \
- std::printf (" FAILED %s:%d: %s\n", __FILE__, __LINE__, #cond); \
- } \
- } while (false)
- std::string realFlpDir;
- // --- helpers for building synthetic .flp files -----------------------------
- struct FlpBuilder
- {
- std::vector<std::uint8_t> bytes;
- FlpBuilder (std::uint16_t ppq = 96)
- {
- put ("FLhd");
- u32 (6);
- u16 (0);
- u16 (1);
- u16 (ppq);
- put ("FLdt");
- u32 (0); // size is not used by the reader
- }
- void put (const char* s) { bytes.insert (bytes.end(), s, s + std::strlen (s)); }
- void u8 (std::uint8_t v) { bytes.push_back (v); }
- void u16 (std::uint16_t v) { u8 ((std::uint8_t) v); u8 ((std::uint8_t) (v >> 8)); }
- void u32 (std::uint32_t v) { u16 ((std::uint16_t) v); u16 ((std::uint16_t) (v >> 16)); }
- void varint (std::size_t n)
- {
- do
- {
- auto b = (std::uint8_t) (n & 0x7F);
- n >>= 7;
- u8 (n ? (std::uint8_t) (b | 0x80) : b);
- } while (n);
- }
- void data (std::uint8_t id, const std::vector<std::uint8_t>& payload)
- {
- u8 (id);
- varint (payload.size());
- bytes.insert (bytes.end(), payload.begin(), payload.end());
- }
- void ascii (std::uint8_t id, const std::string& s)
- {
- std::vector<std::uint8_t> p (s.begin(), s.end());
- p.push_back (0);
- data (id, p);
- }
- void utf16 (std::uint8_t id, const std::u16string& s)
- {
- std::vector<std::uint8_t> p;
- for (auto c : s)
- {
- p.push_back ((std::uint8_t) c);
- p.push_back ((std::uint8_t) (c >> 8));
- }
- p.push_back (0);
- p.push_back (0);
- data (id, p);
- }
- };
- std::vector<std::uint8_t> noteRecord (std::uint32_t pos, std::uint16_t channel, std::uint32_t len, std::uint16_t key,
- std::uint8_t velocity)
- {
- std::vector<std::uint8_t> r (24, 0);
- std::memcpy (&r[0], &pos, 4);
- std::memcpy (&r[6], &channel, 2);
- std::memcpy (&r[8], &len, 4);
- std::memcpy (&r[12], &key, 2);
- r[16] = 120;
- r[20] = 64;
- r[21] = velocity;
- return r;
- }
- } // namespace
- // ---------------------------------------------------------------------------
- // FLP
- TEST (flp_rejects_garbage)
- {
- std::vector<std::uint8_t> junk (64, 0x41);
- bool threw = false;
- try { flp::EventReader r (junk); } catch (const flp::Error&) { threw = true; }
- CHECK (threw);
- }
- TEST (flp_reads_size_classes_varint_and_extended_ids)
- {
- FlpBuilder b;
- b.u8 (0x15); b.u8 (2); // u8
- b.u8 (0x40); b.u16 (513); // u16
- b.u8 (0x9C); b.u32 (128500); // u32
- b.data (0xD5, std::vector<std::uint8_t> (300, 7)); // 2-byte varint
- b.u8 (0xAC); b.u32 (0xC0000100); b.varint (3); b.u8 (1); b.u8 (2); b.u8 (3); // extended ID
- flp::EventReader r (b.bytes);
- flp::Event e;
- CHECK (r.header().ppq == 96);
- CHECK (r.next (e) && e.id == 0x15 && e.value == 2);
- CHECK (r.next (e) && e.id == 0x40 && e.value == 513);
- CHECK (r.next (e) && e.id == 0x9C && e.value == 128500);
- CHECK (r.next (e) && e.id == 0xD5 && e.data.size() == 300 && e.data[299] == 7);
- CHECK (r.next (e) && e.id == 0xC0000100 && e.isData() && e.data.size() == 3 && e.data[2] == 3);
- CHECK (! r.next (e));
- }
- TEST (flp_truncated_file_throws)
- {
- FlpBuilder b;
- b.u8 (0x9C); b.u8 (1); // u32 with only 1 byte
- flp::EventReader r (b.bytes);
- flp::Event e;
- bool threw = false;
- try { r.next (e); } catch (const flp::Error&) { threw = true; }
- CHECK (threw);
- }
- TEST (flp_version_numbers)
- {
- CHECK (flp::parseVersionNumber ("25.2.5.5319") == 25002005u);
- CHECK (flp::parseVersionNumber ("20.99.3000.3031") == 20102000u);
- CHECK (flp::parseVersionNumber ("11.0.3") == 11000003u);
- }
- TEST (flp_playlist_record_size_by_version)
- {
- CHECK (flp::playlistRecordSize (12009003, 64) == 32);
- CHECK (flp::playlistRecordSize (21000000, 120) == 60);
- CHECK (flp::playlistRecordSize (25002005, 160) == 80);
- CHECK (flp::playlistRecordSize (25002005, 60) == 60); // size doesn't match the version -> tries the others
- CHECK (flp::playlistRecordSize (25002005, 7) == 0);
- }
- TEST (flp_parses_project_model)
- {
- FlpBuilder b (96);
- b.ascii (0xC7, "25.2.5.5319");
- b.u8 (0x9C); b.u32 (174500);
- b.utf16 (0xC2, u"Título");
- b.u8 (0x41); b.u16 (3);
- b.u8 (0x96); b.u32 (0x00A14547); // BGR in the file
- auto notes = noteRecord (96, 1, 48, 64, 100);
- auto n2 = noteRecord (0, 0, 0, 60, 128);
- notes.insert (notes.end(), n2.begin(), n2.end());
- b.data (0xE0, notes);
- b.u8 (0x40); b.u16 (0);
- b.u8 (0x15); b.u8 (0);
- b.utf16 (0xCB, u"Kick");
- {
- std::vector<std::uint8_t> levels (24, 0);
- const std::int32_t lv[] = { 3200, 12800, -150 };
- std::memcpy (levels.data(), lv, sizeof (lv));
- b.data (0xDB, levels);
- }
- b.u8 (0x68); b.u16 (5);
- b.u8 (0x40); b.u16 (1);
- b.u8 (0x15); b.u8 (2);
- b.utf16 (0xC9, u"FLEX");
- b.utf16 (0xCB, u"Lead");
- b.u8 (0x63); b.u16 (0);
- std::vector<std::uint8_t> item (80, 0);
- std::uint32_t pos = 384, len = 768;
- std::uint16_t base = 20480, idx = 20480 + 3, rv = 499 - 2;
- std::memcpy (&item[0], &pos, 4);
- std::memcpy (&item[4], &base, 2);
- std::memcpy (&item[6], &idx, 2);
- std::memcpy (&item[8], &len, 4);
- std::memcpy (&item[12], &rv, 2);
- b.data (0xE9, item);
- b.utf16 (0xCC, u"Drums");
- b.data (0xEC, std::vector<std::uint8_t> (12, 0));
- b.data (0xEC, std::vector<std::uint8_t> (12, 0));
- const auto p = flp::parseProject (b.bytes);
- CHECK (p.version == "25.2.5.5319");
- CHECK (std::abs (p.tempo - 174.5) < 1e-9);
- CHECK (p.title == "Título");
- CHECK (p.channels.size() == 2);
- CHECK (p.channels[0].name == "Kick" && p.channels[0].mixerTrack == 5);
- CHECK (p.channels[0].hasLevels && p.channels[0].pan == 3200 && p.channels[0].volume == 12800 && p.channels[0].pitchCents == -150);
- CHECK (p.channels[1].type == flp::ChannelType::instrument && p.channels[1].pluginName == "FLEX");
- CHECK (p.patterns.size() == 1 && p.patterns[0].index == 3);
- CHECK (p.patterns[0].colour && *p.patterns[0].colour == 0x4745A1);
- CHECK (p.patterns[0].notes.size() == 2 && p.patterns[0].notes[0].position == 0); // sorted
- CHECK (p.patterns[0].notes[1].key == 64 && p.patterns[0].notes[1].length == 48);
- CHECK (p.arrangements.size() == 1 && p.arrangements[0].items.size() == 1);
- const auto& it = p.arrangements[0].items[0];
- CHECK (it.kind == flp::PlaylistItem::Kind::pattern && it.index == 3 && it.track == 2 && it.position == 384);
- CHECK (p.mixer.size() == 2 && p.mixer[0].name == "Drums" && p.mixer[1].name.empty());
- CHECK (p.warnings.empty());
- }
- TEST (flp_fuzz_corrupted_files_do_not_crash)
- {
- if (realFlpDir.empty())
- return;
- // some real .flp files, corrupted in various ways: the parser may reject them (flp::Error), but
- // must never hang, read past the buffer or produce a Song with invalid indices
- std::vector<std::vector<std::uint8_t>> sources;
- for (auto& entry : std::filesystem::recursive_directory_iterator (realFlpDir))
- if (entry.path().extension() == ".flp" && sources.size() < 6 && std::filesystem::file_size (entry.path()) < 3'000'000)
- {
- std::ifstream in (entry.path(), std::ios::binary);
- sources.emplace_back ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
- }
- std::uint32_t seed = 2024;
- auto rnd = [&] (std::uint32_t n) { seed = seed * 1664525u + 1013904223u; return n == 0 ? 0u : (seed >> 8) % n; };
- int parsed = 0, rejected = 0;
- for (int round = 0; round < 240 && ! sources.empty(); ++round)
- {
- auto bytes = sources[(std::size_t) rnd ((std::uint32_t) sources.size())];
- switch (round % 4)
- {
- case 0: bytes.resize (rnd ((std::uint32_t) bytes.size())); break; // truncated
- 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;
- case 2: // absurd event size
- for (int k = 0; k < 5; ++k)
- bytes[(std::size_t) rnd ((std::uint32_t) bytes.size())] = 0xFF;
- break;
- default: // shuffled block
- {
- const auto a = rnd ((std::uint32_t) bytes.size()), b = rnd ((std::uint32_t) bytes.size());
- std::reverse (bytes.begin() + std::min (a, b), bytes.begin() + std::max (a, b));
- break;
- }
- }
- try
- {
- const auto project = flp::parseProject (bytes);
- const auto song = lynx::importFlp (project);
- ++parsed;
- for (const auto& p : song.patterns)
- for (const auto& n : p.notes)
- CHECK (n.channel >= 0 && n.channel < (int) song.channels.size());
- for (const auto& c : song.playlist)
- CHECK (c.kind != lynx::ClipKind::pattern || (c.pattern >= 0 && c.pattern < (int) song.patterns.size()));
- }
- catch (const flp::Error&)
- {
- ++rejected;
- }
- }
- std::printf (" fuzz: %d imported, %d rejected\n", parsed, rejected);
- CHECK (parsed + rejected == 240);
- }
- TEST (plugin_state_decoders_survive_garbage)
- {
- // random blobs (varied sizes, including valid headers followed by garbage)
- std::uint32_t seed = 77;
- auto rnd = [&] { seed = seed * 1664525u + 1013904223u; return seed >> 8; };
- const char* plugins[] = { "Fruity Parametric EQ 2", "Fruity Limiter", "Fruity Reeverb 2", "Fruity Delay 2", "Fruity Delay 3",
- "Fruity Compressor", "Fruity Balance", "Fruity PanOMatic", "Fruity Stereo Shaper", "Fruity Phase Inverter" };
- int decoded = 0;
- for (int round = 0; round < 3000; ++round)
- {
- std::vector<std::uint8_t> blob ((std::size_t) (rnd() % 600));
- for (auto& b : blob)
- b = (std::uint8_t) rnd();
- if (blob.size() >= 4 && round % 3 == 0) // versions the readers accept
- {
- const std::uint32_t versions[] = { 8, 10001, 10000, 12, 14, 6, 1 };
- const auto v = versions[rnd() % 7];
- std::memcpy (blob.data(), &v, 4);
- }
- if (auto fx = lynx::effectFromFl (plugins[rnd() % 10], blob))
- {
- ++decoded;
- for (auto v : fx->params)
- CHECK (std::isfinite (v) && v >= 0.0f && v <= 1.0f);
- }
- lynx::SynthSettings synth;
- if (lynx::synthFrom3xOsc (blob, synth))
- for (const auto& o : synth.osc)
- CHECK (std::isfinite (o.level) && o.level >= 0.0f && o.level <= 1.0f);
- float harmlessGain = 1.0f;
- if (lynx::synthFromHarmless (blob, 120.0, synth, harmlessGain))
- CHECK (std::isfinite (harmlessGain) && std::isfinite (synth.cutoff) && std::isfinite (synth.attack));
- if (lynx::synthFromFruitKick (blob, synth))
- CHECK (std::isfinite (synth.decay) && std::isfinite (synth.pitchEnv.amount));
- if (lynx::synthFromGms (blob, 120.0, synth, harmlessGain) || lynx::synthFromHarmor (blob, synth, harmlessGain) || lynx::synthFromOgun (blob, synth, harmlessGain)
- || lynx::synthFromSakura (blob, synth) || lynx::synthFromKepler (blob, synth, harmlessGain))
- CHECK (std::isfinite (synth.cutoff) && std::isfinite (synth.attack) && std::isfinite (harmlessGain));
- if (auto sound = lynx::drumSynthLiveSound (blob))
- CHECK (sound->size() < 256);
- if (auto wave = lynx::waveTravellerSample (blob))
- CHECK (! wave->first.empty() && wave->second >= 0 && wave->second <= 127);
- lynx::synthFromBooBass (blob, synth);
- lynx::gmsStateUsable (blob); // (the exact engines' parsers reject garbage without crashing)
- lynx::morphineStateUsable (blob);
- if (lynx::synthFromPoiZone (blob, synth, harmlessGain) || lynx::synthFromBassDrum (blob, synth, harmlessGain) || lynx::synthFromToxic (blob, synth, harmlessGain)
- || lynx::synthFromMorphine (blob, synth, harmlessGain))
- CHECK (std::isfinite (synth.cutoff) && std::isfinite (harmlessGain));
- if (auto patch = lynx::drumpadPatchName (blob))
- CHECK (patch->size() <= 0x30 && std::isfinite (lynx::drumToneForName (*patch).decay));
- if (auto flex = lynx::flexPresetName (blob))
- CHECK ((! flex->first.empty() || ! flex->second.empty()) && flex->second.size() <= 1024);
- if (lynx::synthFromSytrus (blob, 120.0, synth, harmlessGain))
- CHECK (std::isfinite (synth.cutoff) && std::isfinite (synth.attack));
- if (lynx::synthFromDX10 (blob, synth))
- CHECK (std::isfinite (synth.decay) && std::isfinite (synth.release) && std::isfinite (synth.osc[1].level));
- if (blob.size() >= 4 && round % 5 == 0) // MiniSynth header followed by garbage
- {
- std::memcpy (blob.data(), "SSLF", 4);
- if (blob.size() > 220)
- {
- std::memcpy (blob.data() + 40, "DATA", 4);
- std::memcpy (blob.data() + 48, "0rev", 4);
- }
- }
- if (lynx::synthFromMiniSynth (blob, 120.0, synth))
- CHECK (std::isfinite (synth.cutoff) && std::isfinite (synth.decay) && std::isfinite (synth.attack));
- if (lynx::miniSynthStateUsable (blob)) // the exact engine's parser and a short render with silent tables
- {
- const auto params = lynx::miniSynthParams (blob);
- CHECK (params.size() == 43u && std::all_of (params.begin(), params.end(), [] (float v) { return v >= 0.0f && v <= 1.0f; }));
- const auto y = lynx::minisynth::renderReference (blob, { { 0, 60, 1.0, 1.0, 0.0, 0.0, 64 } }, 44100.0, 128, "");
- CHECK (y.size() == 256u && std::all_of (y.begin(), y.end(), [] (float v) { return std::isfinite (v); }));
- }
- lynx::ForeignPlugin foreign;
- lynx::parseFlWrapper (blob, foreign);
- std::vector<std::uint8_t> component, controller;
- lynx::splitFlVst3State (blob, component, controller);
- }
- CHECK (decoded == 3000); // every known name yields an effect (default when the blob is unusable)
- }
- TEST (inflate_zlib)
- {
- // fixed Huffman with back-references
- const std::uint8_t fixed[] = { 0x78, 0xda, 0x4b, 0x4c, 0x4a, 0x4e, 0xc4, 0x86, 0x22, 0x00, 0x7c, 0x69, 0x09, 0x89 };
- const auto a = lynx::inflateZlib (fixed, sizeof (fixed));
- CHECK (a && std::string (a->begin(), a->end()) == "abcabcabcabcabcabcabcabcX");
- // stored (uncompressed) block
- const std::uint8_t stored[] = { 0x78, 0x01, 0x01, 0x03, 0x00, 0xfc, 0xff, 'L', 'Y', 'N', 0x00, 0x00, 0x00, 0x00 };
- const auto b = lynx::inflateZlib (stored, sizeof (stored));
- CHECK (b && std::string (b->begin(), b->end()) == "LYN");
- // truncated, bad header and size limit
- CHECK (! lynx::inflateZlib (fixed, 6));
- const std::uint8_t bad[] = { 0x12, 0x34, 0x00 };
- CHECK (! lynx::inflateZlib (bad, sizeof (bad)));
- CHECK (! lynx::inflateZlib (fixed, sizeof (fixed), false, 10));
- }
- TEST (fl_round15_effects_convert_known_values)
- {
- auto blob = [] (std::vector<std::int32_t> words) {
- std::vector<std::uint8_t> b (words.size() * 4);
- std::memcpy (b.data(), words.data(), b.size());
- return b;
- };
- // Frequency Shifter "+800 Hz": frequency 51542 in the 20 kHz range, 100% wet
- const auto fs = lynx::effectFromFl ("Frequency Shifter", blob ({ 4, 10000, 0, 51542, 0, 10000, 10000, 5000, 10000, 0, 5000, 0, 0 }));
- CHECK (fs && fs->kind == lynx::EffectKind::freqShift);
- if (fs)
- {
- const auto x = (fs->params[0] - 0.5) * 2.0;
- CHECK (std::abs (2000.0 * x * std::abs (x) - 800.0) < 10.0);
- CHECK (std::abs (fs->params[1] - 1.0f) < 1e-4f);
- }
- // Emphasis: input 0.0625 (= +1.5 dB) and ceiling 0.9308 (≈ −1.6 dB) -> limiter
- const auto em = lynx::effectFromFl ("Emphasis", blob ({ 8, 0, 0, 2, 1, 0, 0, 625, 10000, 10000, 9308, 0, 2, 7, 0 }));
- CHECK (em && em->kind == lynx::EffectKind::limiter && em->enabled);
- if (em)
- {
- CHECK (std::abs (em->params[1] * 24.0f - 1.5f) < 0.05f);
- CHECK (std::abs (em->params[0] * 12.0f - 12.0f + 1.6f) < 0.1f);
- }
- // Pitch Shifter +12 st, mix 100%
- 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 }));
- CHECK (ps && ps->kind == lynx::EffectKind::pitchShift && std::abs (ps->params[0] - (0.5f + 12.0f / 48.0f)) < 1e-4f);
- }
- TEST (frequency_shifter_moves_spectrum_by_hz)
- {
- auto fx = lynx::makeEffect (lynx::EffectKind::freqShift);
- fx->prepare (48000.0);
- auto slot = lynx::makeEffectSlot (lynx::EffectKind::freqShift);
- // +200 Hz: x·|x|·2000 = 200 -> x = sqrt(0.1)
- slot.params[0] = (float) (0.5 + std::sqrt (0.1) / 2.0);
- std::vector<float> l (48000), r (48000);
- for (int i = 0; i < 48000; ++i)
- l[(std::size_t) i] = r[(std::size_t) i] = (float) std::sin (2.0 * 3.141592653589793 * 1000.0 * i / 48000.0);
- for (int done = 0; done < 48000; done += 480)
- fx->process (l.data() + done, r.data() + done, 480, slot.params, 120.0);
- auto energy = [&] (double f) {
- const auto w = 2.0 * 3.141592653589793 * f / 48000.0, k = 2.0 * std::cos (w);
- double s1 = 0.0, s2 = 0.0;
- for (int i = 9600; i < 48000; ++i)
- {
- const auto s0 = l[(std::size_t) i] + k * s1 - s2;
- s2 = s1;
- s1 = s0;
- }
- return s1 * s1 + s2 * s2 - k * s1 * s2;
- };
- // the energy moves to 1200 Hz; little remains at 1000 and almost nothing at the image (800)
- CHECK (energy (1200.0) > 50.0 * energy (1000.0));
- CHECK (energy (1200.0) > 50.0 * energy (800.0));
- }
- TEST (pitch_shift_moves_frequency_keeps_length)
- {
- auto fx = lynx::makeEffect (lynx::EffectKind::pitchShift);
- fx->prepare (48000.0);
- auto slot = lynx::makeEffectSlot (lynx::EffectKind::pitchShift);
- auto crossingsAfter = [&] (float semitoneParam) {
- fx->reset();
- slot.params[0] = semitoneParam;
- std::vector<float> l (48000), r (48000);
- for (int i = 0; i < 48000; ++i)
- l[(std::size_t) i] = r[(std::size_t) i] = (float) std::sin (2.0 * 3.141592653589793 * 220.0 * i / 48000.0);
- for (int done = 0; done < 48000; done += 512)
- fx->process (l.data() + done, r.data() + done, std::min (512, 48000 - done), slot.params, 120.0);
- int crossings = 0;
- for (int i = 9600; i < 48000; ++i)
- crossings += (l[(std::size_t) i - 1] < 0.0f) != (l[(std::size_t) i] < 0.0f);
- for (auto v : l)
- CHECK (std::isfinite (v) && std::abs (v) < 1.6f);
- return crossings / 0.8 / 2.0; // cycles per second
- };
- CHECK (std::abs (crossingsAfter (0.5f) - 220.0) < 8.0); // 0 st
- CHECK (std::abs (crossingsAfter (0.5f + 12.0f / 48.0f) - 440.0) < 25.0); // +12 st
- CHECK (std::abs (crossingsAfter (0.5f - 12.0f / 48.0f) - 110.0) < 12.0); // −12 st
- }
- TEST (volume_gate_follows_bar_position)
- {
- auto fx = lynx::makeEffect (lynx::EffectKind::volumeGate);
- fx->prepare (48000.0);
- auto slot = lynx::makeEffectSlot (lynx::EffectKind::volumeGate);
- for (int i = 0; i < 32; ++i)
- slot.params[(std::size_t) i] = i < 16 ? 1.0f : 0.0f; // first two beats open, two closed
- slot.params[33] = slot.params[34] = 0.0f; // no smoothing
- // 120 BPM: one beat = 0.5 s = 24000 samples; the 4-beat bar starts at tick 0
- std::vector<float> l (96000, 1.0f), r (96000, 1.0f);
- fx->setSongPosition (0.0);
- for (int done = 0; done < 96000; done += 512)
- fx->process (l.data() + done, r.data() + done, std::min (512, 96000 - done), slot.params, 120.0);
- CHECK (l[10000] > 0.99f && l[40000] > 0.99f); // beats 1 and 2
- CHECK (l[60000] < 0.01f && l[90000] < 0.01f); // beats 3 and 4
- // starting mid-bar (beat 3): already closed
- std::vector<float> l2 (4800, 1.0f), r2 (4800, 1.0f);
- fx->reset();
- fx->setSongPosition (2.0 * lynx::ppq);
- fx->process (l2.data(), r2.data(), 4800, slot.params, 120.0);
- CHECK (l2[4000] < 0.01f);
- // mix 0: transparent
- slot.params[32] = 0.0f;
- std::vector<float> l3 (4800, 1.0f), r3 (4800, 1.0f);
- fx->setSongPosition (2.0 * lynx::ppq);
- fx->process (l3.data(), r3.data(), 4800, slot.params, 120.0);
- CHECK (l3[4000] > 0.99f);
- }
- TEST (patcher_branch_heads_follow_rea)
- {
- auto words = [] (std::vector<std::int32_t> v) {
- std::vector<std::uint8_t> b (v.size() * 4);
- std::memcpy (b.data(), v.data(), b.size());
- return b;
- };
- // Frequency Splitter, 2 bands, destinations 1/2/3, slope combo 3 (24 dB/oct), high send −6 dB
- std::vector<std::int32_t> fs (18, 0);
- fs[0] = 4;
- fs[1] = 32768;
- fs[3] = 3;
- fs[6] = 0;
- fs[11] = -600;
- fs[15] = 1;
- fs[16] = 2;
- fs[17] = 3;
- const auto split = words (fs);
- const auto low = lynx::patcherBranchHead ("Frequency Splitter", split, 1);
- CHECK (low.valid && low.slot && low.note.empty());
- if (low.slot)
- {
- // low band = just the Crossover low-pass, combo 3 (24 dB/oct)
- CHECK (low.slot->kind == lynx::EffectKind::crossover && low.slot->params[0] == 0.0f && low.slot->params[3] == 1.0f);
- CHECK (std::lround (low.slot->params[5] * 7.0f) == 3);
- }
- const auto mid = lynx::patcherBranchHead ("Frequency Splitter", split, 2);
- CHECK (mid.valid && mid.gain == 0.0f); // with 2 bands the mid is silent
- const auto high = lynx::patcherBranchHead ("Frequency Splitter", split, 3);
- CHECK (high.valid && std::abs (high.gain - std::pow (10.0f, -6.0f / 20.0f)) < 1e-4f);
- CHECK (! lynx::patcherBranchHead ("Frequency Splitter", split, 7).valid);
- // Stereo Shaper with identity matrix: the difference (destination 4) is zero
- const auto shaper = words ({ 3, 0, 12800, 12800, 0, 0, 0, 4, 0 });
- const auto diff = lynx::patcherBranchHead ("Fruity Stereo Shaper", shaper, 4);
- CHECK (diff.valid && diff.slot && std::abs (diff.slot->params[2] - 0.5f) < 1e-6f && std::abs (diff.slot->params[5] - 0.5f) < 1e-6f);
- CHECK (! lynx::patcherBranchHead ("Fruity Stereo Shaper", shaper, 2).valid);
- }
- TEST (parallel_container_sums_branches)
- {
- // two branches: dry with gain 0.5 and a utility that flips phase (matrix −1) with gain 0.5 -> silence
- auto slot = lynx::makeEffectSlot (lynx::EffectKind::parallel);
- lynx::EffectBranch dry;
- dry.gain = 0.5f;
- lynx::EffectBranch inverted;
- inverted.gain = 0.5f;
- auto flip = lynx::makeEffectSlot (lynx::EffectKind::utility);
- flip.params[2] = flip.params[5] = 0.25f; // (−1 + 2)/4
- inverted.chain.push_back (flip);
- slot.branches = { dry, inverted };
- auto fx = lynx::makeParallelEffect (slot);
- fx->prepare (48000.0);
- std::vector<float> l (9000, 0.8f), r (9000, -0.4f);
- fx->process (l.data(), r.data(), 9000, slot.params, 120.0);
- CHECK (std::abs (l[100]) < 1e-5f && std::abs (r[8999]) < 1e-5f);
- // only the dry branch with gain 2: doubles
- slot.branches = { lynx::EffectBranch { {}, 2.0f } };
- auto doubler = lynx::makeParallelEffect (slot);
- doubler->prepare (48000.0);
- std::fill (l.begin(), l.end(), 0.25f);
- doubler->process (l.data(), r.data(), 9000, slot.params, 120.0);
- CHECK (std::abs (l[5000] - 0.5f) < 1e-6f);
- }
- TEST (fl_controller_constant_outputs)
- {
- std::vector<std::int32_t> xy { 2, 32768, 0, 65536, 0, 16384, 0, 65536, 0, 1024, 256 };
- std::vector<std::uint8_t> blob (xy.size() * 4 + 8, 0);
- std::memcpy (blob.data(), xy.data(), xy.size() * 4);
- CHECK (std::abs (lynx::controllerConstantOutput ("Fruity X-Y Controller", blob, 0).value_or (-1.0f) - 0.5f) < 1e-4f);
- CHECK (std::abs (lynx::controllerConstantOutput ("Fruity X-Y Controller", blob, 1).value_or (-1.0f) - 0.25f) < 1e-4f);
- // Control Surface: version + group { 0x836: value 0.75, -, output 2 }
- std::vector<std::uint8_t> cs (4, 0);
- cs[0] = 1;
- auto chunk = [] (std::vector<std::uint8_t>& out, std::uint32_t id, const std::vector<std::uint8_t>& body) {
- const auto size = (std::uint64_t) body.size();
- out.insert (out.end(), reinterpret_cast<const std::uint8_t*> (&id), reinterpret_cast<const std::uint8_t*> (&id) + 4);
- out.insert (out.end(), reinterpret_cast<const std::uint8_t*> (&size), reinterpret_cast<const std::uint8_t*> (&size) + 8);
- out.insert (out.end(), body.begin(), body.end());
- };
- std::vector<std::uint8_t> value (12, 0);
- const float v = 0.75f;
- const std::int32_t out = 2;
- std::memcpy (value.data(), &v, 4);
- std::memcpy (value.data() + 8, &out, 4);
- std::vector<std::uint8_t> group;
- chunk (group, 0x836, value);
- chunk (cs, 0, group);
- CHECK (std::abs (lynx::controllerConstantOutput ("Control Surface", cs, 2).value_or (-1.0f) - 0.75f) < 1e-6f);
- CHECK (! lynx::controllerConstantOutput ("Control Surface", cs, 0).has_value());
- }
- TEST (flp_midi_out_notes_reach_wrapper_on_same_port)
- {
- auto record = [] (std::vector<std::uint8_t>& out, std::uint32_t id, std::vector<std::uint8_t> body) {
- const auto size = (std::uint64_t) body.size();
- out.insert (out.end(), reinterpret_cast<const std::uint8_t*> (&id), reinterpret_cast<const std::uint8_t*> (&id) + 4);
- out.insert (out.end(), reinterpret_cast<const std::uint8_t*> (&size), reinterpret_cast<const std::uint8_t*> (&size) + 8);
- out.insert (out.end(), body.begin(), body.end());
- };
- auto i32s = [] (std::vector<std::int32_t> v) {
- std::vector<std::uint8_t> b (v.size() * 4);
- std::memcpy (b.data(), v.data(), b.size());
- return b;
- };
- flp::Project p;
- flp::Channel midiOut;
- midiOut.index = 0;
- midiOut.type = flp::ChannelType::native;
- midiOut.pluginName = "MIDI Out";
- midiOut.pluginState = i32s ({ 7, 0, -1, -1, -1, 0, 3, 0 }); // channel 1, port 3 (@24)
- flp::Channel wrapper;
- wrapper.index = 1;
- wrapper.type = flp::ChannelType::native;
- wrapper.pluginName = "Fruity Wrapper";
- wrapper.pluginState = i32s ({ 9 });
- record (wrapper.pluginState, 0x32, i32s ({ 7 })); // VST3
- record (wrapper.pluginState, 0x01, i32s ({ 3, -1, 0, 0, 0 })); // input port 3
- p.channels = { midiOut, wrapper };
- flp::Pattern pattern;
- pattern.index = 1;
- flp::Note note;
- note.channel = 0;
- note.key = 64;
- note.length = 96;
- pattern.notes.push_back (note);
- p.patterns.push_back (pattern);
- const auto song = lynx::importFlp (p);
- CHECK (song.channels.size() == 2 && song.channels[0].muted);
- CHECK (song.channels[1].plugin.foreign.midiInPort == 3);
- int toWrapper = 0;
- for (const auto& pat : song.patterns)
- for (const auto& n : pat.notes)
- toWrapper += n.channel == 1 && n.key == 64;
- CHECK (toWrapper == 1);
- }
- TEST (flp_effect_automation_follows_slots_and_params)
- {
- // track 1: slot 0 with an effect that has no equivalent (dropped), slot 2 with Parametric EQ 2
- flp::Project p;
- flp::MixerInsert insert;
- insert.index = 1;
- insert.slots.push_back ({ 0, "Some Unknown FX", {} });
- insert.slots.push_back ({ 2, "Fruity Parametric EQ 2", {} });
- p.mixer.push_back (insert);
- auto clip = [&] (int index, std::uint32_t target) {
- flp::Channel c;
- c.index = index;
- c.type = flp::ChannelType::automationClip;
- c.automation = { { 0.0, 0.0 }, { 96.0, 1.0 } };
- p.channels.push_back (c);
- p.automationLinks.push_back ({ index, { target } });
- };
- 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; };
- clip (0, mixer (1, 2, 0x8000 + 7)); // EQ band 1 frequency
- clip (1, mixer (1, 2, 0x1F01)); // EQ slot mix
- clip (2, mixer (1, 0, 0x8000)); // parameter of the dropped effect
- clip (3, mixer (1, 2, 0x8000 + 21)); // band type: no equivalent
- const auto song = lynx::importFlp (p);
- CHECK (song.automations.size() == 4);
- CHECK (song.mixer[1].effects.size() == 1 && song.mixer[1].effects[0].kind == lynx::EffectKind::parametricEq);
- const auto& freq = song.automations[0];
- CHECK (freq.target.kind == lynx::AutomationTargetKind::effectParam && freq.target.index == 1 && freq.target.slot == 0);
- CHECK (freq.target.param == lynx::peq::param (0, 1));
- // FL's 0..1 (20 Hz..20 kHz) becomes our frequency scale
- CHECK (std::abs (freq.points.front().value - lynx::peq::hzValue (20.0)) < 1e-4f);
- CHECK (std::abs (freq.points.back().value - lynx::peq::hzValue (20000.0)) < 1e-4f);
- CHECK (song.automations[1].target.kind == lynx::AutomationTargetKind::effectMix && song.automations[1].target.slot == 0);
- CHECK (song.automations[2].target.kind == lynx::AutomationTargetKind::none);
- CHECK (song.automations[3].target.kind == lynx::AutomationTargetKind::none);
- // the automated mix is read/written through the target
- auto copy = song;
- CHECK (copy.setTargetValue (song.automations[1].target, 0.25f) && std::abs (copy.mixer[1].effects[0].mix - 0.25f) < 1e-6f);
- CHECK (std::abs (copy.targetValue (song.automations[1].target) - 0.25f) < 1e-6f);
- }
- TEST (flp_real_files)
- {
- if (realFlpDir.empty())
- {
- std::printf (" (skipped: pass a folder with .flp files as argument)\n");
- return;
- }
- int files = 0, withNotes = 0, withPlaylist = 0, automations = 0, automationsWithTarget = 0, sends = 0;
- int threeOsc = 0, threeOscDecoded = 0, wrapped = 0, wrappedParsed = 0, markers = 0, signatures = 0, trackEqs = 0;
- int harmless = 0, harmlessDecoded = 0, harmlessParsed = 0, harmlessKind = 0, sytrus = 0, sytrusDecoded = 0, sytrusEngine = 0, sytrusRendered = 0,
- sytrusSounding = 0;
- int harmor = 0, harmorUsable = 0, harmorKind = 0, harmorSounding = 0;
- int gms = 0, gmsUsable = 0, gmsKind = 0, gmsSounding = 0, morphine = 0, morphineUsable = 0, morphineKind = 0, morphineSounding = 0, demoGoldens = 0;
- int ogun = 0, ogunUsable = 0, ogunKind = 0, ogunSounding = 0;
- int miniSynth = 0, miniSynthUsable = 0, miniSynthKind = 0, miniSynthSounding = 0, miniSynthGoldens = 0;
- int dx10 = 0, dx10Usable = 0, dx10Kind = 0, dx10Sounding = 0;
- int kepler = 0, keplerUsable = 0, keplerKind = 0, keplerSounding = 0;
- std::map<std::string, std::pair<int, int>> generators; // GMS/Ogun/Autogun/Sakura/DrumSynth: channels, decoded
- int ogunTrumpet = 0; // Ogun channels whose spectrum matches the REA "Trumpet" reference
- int peqSlots = 0, peqShaped = 0, reverbs = 0, reverbsDryless = 0, reverbsLowCut = 0;
- int dwChannels = 0, dwZones = 0, fpcKits = 0, padChannels = 0, padNotes = 0;
- int harmlessCompWanted = 0, harmlessCompLoaded = 0; // Harmless rack compressors / presets read from the installation
- int maximusSlots = 0, maximusExact = 0, sgzSlots = 0, sgzExact = 0, vocodexSlots = 0, vocodexWithData = 0; // REA_rodada40
- int delay2Slots = 0, delay2Exact = 0, delay3Slots = 0, delay3Exact = 0; // REA_rodada52
- // import with the installation's samples (realFlpDir is ...\Data): for DirectWave/FPC
- lynx::FlpImportOptions withFiles;
- const auto flRoot = std::filesystem::path (realFlpDir).parent_path().string();
- withFiles.roots = { flRoot, realFlpDir, "" };
- withFiles.fileExists = [] (const std::string& path) {
- auto local = path;
- std::replace (local.begin(), local.end(), '\\', '/');
- return std::filesystem::exists (local);
- };
- withFiles.readFile = [] (const std::string& path) {
- auto local = path;
- std::replace (local.begin(), local.end(), '\\', '/');
- std::ifstream in (local, std::ios::binary);
- return std::vector<std::uint8_t> ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
- };
- int embeddedSaved = 0;
- withFiles.saveEmbedded = [&embeddedSaved] (const std::vector<std::uint8_t>& wav, const std::string&) {
- ++embeddedSaved;
- return wav.size() > 44 ? std::string ("embutido.wav") : std::string();
- };
- int slicedChannels = 0, flKeysChannels = 0;
- for (auto& entry : std::filesystem::recursive_directory_iterator (realFlpDir))
- {
- if (entry.path().extension() != ".flp")
- continue;
- std::ifstream in (entry.path(), std::ios::binary);
- std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
- ++files;
- try
- {
- const auto p = flp::parseProject (bytes);
- CHECK (! p.version.empty());
- CHECK (p.tempo > 10.0 && p.tempo < 1000.0);
- const auto song = lynx::importFlp (p);
- CHECK (! song.patterns.empty());
- {
- const auto full = lynx::importFlp (p, withFiles);
- bool hasFpc = false;
- for (const auto& c : p.channels)
- hasFpc = hasFpc || c.pluginName == "FPC";
- // GMS and Morphine (REA_rodada39): every state plays on the exact engine (GMS with its waves from the
- // installation) and sounds; two demo channels match golden samples of our round-39 models
- for (const auto& c : p.channels)
- {
- if (c.pluginName == "GMS")
- ++gms, gmsUsable += lynx::gmsStateUsable (c.pluginState) ? 1 : 0;
- if (c.pluginName == "Morphine")
- ++morphine, morphineUsable += lynx::morphineStateUsable (c.pluginState) ? 1 : 0;
- }
- // Kepler: every state in the demos plays on the exact engine (REA_rodada46) when the engine DLL is installed
- for (const auto& c : p.channels)
- if (c.pluginName == "Kepler")
- {
- ++kepler;
- keplerUsable += lynx::keplerStateUsable (c.pluginState) ? 1 : 0;
- }
- for (const auto& c : full.channels)
- if (c.instrument == lynx::InstrumentKind::kepler)
- {
- ++keplerKind;
- 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 } },
- 44100.0, 11025);
- double e = 0.0;
- bool finite = ! y.empty();
- for (auto v : y)
- finite = finite && std::isfinite (v), e += (double) v * v;
- CHECK (finite);
- keplerSounding += e > 1e-6 ? 1 : 0;
- }
- const auto file = entry.path().filename().string();
- for (const auto& c : full.channels)
- {
- const auto isGms = c.instrument == lynx::InstrumentKind::gms, isMorphine = c.instrument == lynx::InstrumentKind::morphine;
- if (! isGms && ! isMorphine)
- continue;
- (isGms ? gmsKind : morphineKind)++;
- auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
- const auto golden = (isGms && c.name == "GMS" && file.find ("Song For You") != std::string::npos)
- || (isMorphine && c.name == "Morphine" && file.find ("SHINE") != std::string::npos);
- std::vector<float> y;
- if (isGms)
- y = lynx::gms::renderReference (c.pluginState,
- { { 0, 60, (float) iv (100), 1.0, 0.0, 0.0, 30000 }, { 11025, 64, (float) iv (80), 1.0, 0.0, 0.0, 40000 },
- { 22050, 67, (float) iv (110), 1.0, 0.0, 0.0, 52000 }, { 60000, 48, (float) iv (127), 1.0, 0.0, 0.0, 70000 } },
- 44100.0, golden ? 88200 : 22050, c.samplePath);
- else
- 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 } },
- 44100.0, golden ? 100000 : 22050, c.samplePath);
- double e = 0.0;
- bool finite = ! y.empty();
- for (auto v : y)
- finite = finite && std::isfinite (v), e += (double) v * v;
- CHECK (finite);
- (isGms ? gmsSounding : morphineSounding) += e > 1e-6 ? 1 : 0;
- if (golden && ! y.empty())
- {
- // our model, same events (GMS: tempo 140; Morphine: tempo 120), state as saved, 44.1 kHz
- const auto rms = std::sqrt (e / (double) y.size());
- CHECK (std::abs (rms - (isGms ? 0.213602668574 : 0.0673606448152)) < 1e-5);
- 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 } },
- 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 } };
- for (const auto& g : isGms ? std::vector (std::begin (gmsAt), std::end (gmsAt)) : std::vector (std::begin (morAt), std::end (morAt)))
- 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);
- ++demoGoldens;
- }
- }
- // MiniSynth (REA_rodada41): every state plays on the exact engine (tables from the installation's
- // oscdata.dat) and sounds; two demo channels match golden samples of our round-41 model (bit-identical to
- // renders of the installation's DLL for these states)
- for (const auto& c : p.channels)
- if (c.pluginName == "MiniSynth")
- ++miniSynth, miniSynthUsable += lynx::miniSynthStateUsable (c.pluginState) ? 1 : 0;
- for (const auto& c : full.channels)
- {
- if (c.instrument != lynx::InstrumentKind::minisynth)
- continue;
- ++miniSynthKind;
- auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
- const auto exo = file.find ("Exoplanet") != std::string::npos;
- const auto golden = exo && (c.name == "Main Lead 1" || c.name == "Riser");
- const auto y = lynx::minisynth::renderReference (c.pluginState,
- { { 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 }, { 60000, 48, iv (127), 1.0, 0.0, 0.0, 70000 } },
- 44100.0, golden ? 88200 : 22050, c.samplePath);
- double e = 0.0;
- bool finite = ! y.empty();
- for (auto v : y)
- finite = finite && std::isfinite (v), e += (double) v * v;
- CHECK (finite);
- miniSynthSounding += e > 1e-6 ? 1 : 0;
- if (golden && ! y.empty())
- {
- // our model, same events, tempo 120, state as saved, 44.1 kHz, host blocks of 512, L = R = Final.Vol
- const auto lead = c.name == "Main Lead 1";
- CHECK (std::abs (std::sqrt (e / (double) y.size()) - (lead ? 0.177495801753 : 0.19006567413)) < 1e-9);
- 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 } },
- riserAt[] = { { 2000, -0.0603227727f }, { 15000, 0.112200871f }, { 30000, 0.0453962795f }, { 50000, 0.234380022f }, { 65000, -0.164545432f }, { 80000, -0.000485120923f } };
- for (const auto& g : lead ? std::vector (std::begin (leadAt), std::end (leadAt)) : std::vector (std::begin (riserAt), std::end (riserAt)))
- CHECK (y[2 * (std::size_t) g.i] == g.v && y[2 * (std::size_t) g.i + 1] == g.v);
- ++miniSynthGoldens;
- }
- }
- for (const auto& c : full.channels)
- {
- if (c.instrument == lynx::InstrumentKind::harmless && c.modelParams.size() >= (std::size_t) lynx::harmlessParamCount
- && c.modelParams[(std::size_t) lynx::harmlessComboIndex + 3] > 0.5f)
- {
- ++harmlessCompWanted;
- harmlessCompLoaded += c.modelParams.size() > (std::size_t) lynx::harmlessCompIndex + 1000 ? 1 : 0;
- }
- flKeysChannels += c.zones.size() == 30 && c.zones.front().path.find ("CHANGPIANO") != std::string::npos ? 1 : 0;
- if (c.sampler.slices.size() >= 4)
- {
- ++slicedChannels;
- CHECK (std::is_sorted (c.sampler.slices.begin(), c.sampler.slices.end()));
- CHECK (c.sampler.slices.back() < 1.0f);
- }
- if (! c.zones.empty() && c.name.find (" - ") == std::string::npos)
- {
- ++dwChannels;
- dwZones += (int) c.zones.size();
- }
- padChannels += c.name.find (" - ") != std::string::npos && c.instrument == lynx::InstrumentKind::sampler ? 1 : 0;
- }
- fpcKits += hasFpc;
- // Maximus / Soundgoodizer become the exact engine (REA_rodada40); Vocodex carries SG.dat (and its
- // carrier's Sytrus preset) read from the installation
- for (const auto& insert : p.mixer)
- for (const auto& slot : insert.slots)
- {
- maximusSlots += slot.plugin == "Maximus";
- delay2Slots += slot.plugin == "Fruity Delay 2";
- delay3Slots += slot.plugin == "Fruity Delay 3";
- sgzSlots += slot.plugin == "Soundgoodizer";
- vocodexSlots += slot.plugin == "Vocodex";
- }
- for (const auto& t : full.mixer)
- for (const auto& fx : t.effects)
- {
- if (fx.kind == lynx::EffectKind::maximus)
- {
- const auto blob = lynx::maximusfx::unpack (fx.state);
- CHECK (blob && lynx::maximusfx::presetOf (*blob) != nullptr);
- if (blob)
- (blob->build == lynx::hlfx::MaximusBuild::soundgoodizer ? sgzExact : maximusExact) += 1;
- }
- if (fx.kind == lynx::EffectKind::vocodex)
- vocodexWithData += ! lynx::vocodex::splitInstallData (fx.state).second.sgDat.empty();
- delay2Exact += fx.kind == lynx::EffectKind::fruityDelay2;
- delay3Exact += fx.kind == lynx::EffectKind::fruityDelay3;
- }
- for (const auto& pat : full.patterns)
- for (const auto& n : pat.notes)
- {
- CHECK (n.channel >= 0 && n.channel < (int) full.channels.size());
- padNotes += full.channels[(std::size_t) n.channel].name.find (" - ") != std::string::npos ? 1 : 0;
- }
- }
- const auto peqDefaults = lynx::makeEffectSlot (lynx::EffectKind::parametricEq).params;
- for (const auto& t : song.mixer)
- for (const auto& fx : t.effects)
- {
- if (fx.kind == lynx::EffectKind::parametricEq)
- {
- ++peqSlots;
- peqShaped += fx.params != peqDefaults;
- for (double hz : { 50.0, 1000.0, 10000.0 })
- CHECK (std::isfinite (lynx::peq::responseDb (fx.params, 48000.0, hz)));
- }
- if (fx.kind == lynx::EffectKind::reverb)
- {
- ++reverbs;
- reverbsDryless += fx.params[4] < 0.01f;
- reverbsLowCut += fx.params[5] > 0.0f;
- }
- }
- for (const auto& t : song.mixer)
- if (t.eq.active())
- {
- ++trackEqs;
- for (std::size_t b = 0; b < 3; ++b)
- CHECK (t.eq.hz[b] >= 10.0f && t.eq.hz[b] <= 16000.0f && std::abs (t.eq.gainDb[b]) <= 18.0f);
- }
- for (const auto& a : song.arrangements)
- for (const auto& m : a.markers)
- {
- ++markers;
- signatures += m.isSignature();
- CHECK (m.tick >= 0);
- }
- // Fruity Wrapper (channels and mixer slots): every blob in the demos is read
- auto countWrapper = [&] (const std::string& plugin, const std::vector<std::uint8_t>& state) {
- if (plugin != "Fruity Wrapper")
- return;
- ++wrapped;
- lynx::ForeignPlugin f;
- wrappedParsed += lynx::parseFlWrapper (state, f) && f.format != lynx::ForeignPlugin::Format::none && ! f.name.empty();
- };
- for (const auto& c : p.channels)
- countWrapper (c.pluginName, c.pluginState);
- for (const auto& insert : p.mixer)
- for (const auto& slot : insert.slots)
- countWrapper (slot.plugin, slot.state);
- // Harmless: every state in the demos becomes a synth, with finite values
- for (const auto& c : p.channels)
- if (c.pluginName == "Harmless")
- {
- ++harmless;
- lynx::SynthSettings hs;
- float g = 1.0f;
- if (lynx::synthFromHarmless (c.pluginState, 128.0, hs, g))
- {
- ++harmlessDecoded;
- CHECK (std::isfinite (g) && hs.cutoff >= 20.0f && hs.cutoff <= 20000.0f && std::isfinite (hs.decay) && std::isfinite (hs.release));
- }
- if (const auto hp = lynx::harmlessParams (c.pluginState))
- harmlessParsed += (int) hp->size() == lynx::harmlessParamCount ? 1 : 0;
- }
- for (const auto& c : song.channels)
- harmlessKind += c.instrument == lynx::InstrumentKind::harmless ? 1 : 0;
- // Harmor: every state in the demos plays on the exact engine (REA_rodada34) and sounds, with finite samples
- for (const auto& c : p.channels)
- if (c.pluginName == "Harmor")
- {
- ++harmor;
- harmorUsable += lynx::harmorStateUsable (c.pluginState) ? 1 : 0;
- }
- for (const auto& c : song.channels)
- if (c.instrument == lynx::InstrumentKind::harmor)
- {
- ++harmorKind;
- 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);
- double e = 0.0;
- bool finite = ! y.empty();
- for (auto v : y)
- finite = finite && std::isfinite (v), e += (double) v * v;
- CHECK (finite);
- harmorSounding += e > 1e-6 ? 1 : 0;
- }
- // Ogun: every state in the demos plays on the exact engine (REA_rodada37) and sounds, with finite samples
- for (const auto& c : p.channels)
- if (c.pluginName == "Ogun")
- {
- ++ogun;
- ogunUsable += lynx::ogunStateUsable (c.pluginState) ? 1 : 0;
- }
- for (const auto& c : song.channels)
- if (c.instrument == lynx::InstrumentKind::ogun)
- {
- ++ogunKind;
- 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 },
- { 2000, 67, 0.6, 1.0, 0.0, 0.0, 0.0, 0.0, 0.5, 8000 } }, 44100.0, 11025);
- double e = 0.0;
- bool finite = ! y.empty();
- for (auto v : y)
- finite = finite && std::isfinite (v), e += (double) v * v;
- CHECK (finite);
- ogunSounding += e > 1e-6 ? 1 : 0;
- }
- // Fruity DX10: every state plays on the exact engine when its channel processor is neutral (REA_rodada42)
- for (const auto& c : p.channels)
- if (c.pluginName == "Fruity DX10" || c.pluginName == "DX10")
- ++dx10, dx10Usable += lynx::dx10StateUsable (c.pluginState) ? 1 : 0;
- for (const auto& c : song.channels)
- if (c.instrument == lynx::InstrumentKind::dx10)
- {
- ++dx10Kind;
- 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 } },
- 44100.0, 11025);
- double e = 0.0;
- bool finite = ! y.empty();
- for (auto v : y)
- finite = finite && std::isfinite (v), e += (double) v * v;
- CHECK (finite);
- dx10Sounding += e > 1e-6 ? 1 : 0;
- }
- // Sytrus: every state in the demos (zlib + 10 units) decodes, with finite values
- for (const auto& c : p.channels)
- if (c.pluginName == "Sytrus")
- {
- ++sytrus;
- lynx::SynthSettings ss;
- float g = 1.0f;
- // the Sytrus engine (REA_rodada31) reads every demo state; the first 24 play a short note (render settings)
- if (lynx::sytrus::stateUsable (c.pluginState))
- {
- ++sytrusEngine;
- if (sytrusRendered < 24)
- {
- ++sytrusRendered;
- lynx::sytrus::Engine e;
- e.prepare (44100.0, true, 512);
- e.setPatch (lynx::sytrus::parsePatch (c.pluginState));
- lynx::sytrus::NoteParams note;
- e.noteOn (note);
- std::vector<float> l (8192), r (8192);
- e.render (l.data(), r.data(), 8192, 0.551f, 0.0f);
- bool finite = true;
- double energy = 0.0;
- for (std::size_t k = 0; k < l.size(); ++k)
- finite = finite && std::isfinite (l[k]) && std::isfinite (r[k]), energy += (double) l[k] * l[k];
- CHECK (finite);
- sytrusSounding += energy > 1e-6;
- }
- }
- if (lynx::synthFromSytrus (c.pluginState, 128.0, ss, g))
- {
- ++sytrusDecoded;
- CHECK (std::isfinite (g) && ss.cutoff >= 20.0f && ss.cutoff <= 20000.0f && std::isfinite (ss.attack) && std::isfinite (ss.decay)
- && std::isfinite (ss.release) && ss.sustain >= 0.0f && ss.sustain <= 1.0f && std::isfinite (ss.filterEnv.amount));
- for (const auto& o : ss.osc)
- CHECK (o.level >= 0.0f && o.level <= 1.0f && o.octave >= -3 && o.octave <= 3);
- }
- }
- // round 12 generators
- for (const auto& c : p.channels)
- {
- lynx::SynthSettings gs;
- float g = 1.0f;
- int decoded = -1;
- if (c.pluginName == "Harmor")
- {
- decoded = lynx::synthFromHarmor (c.pluginState, gs, g);
- if (decoded)
- {
- // REA_rodada25 §4: most resonant settings stay below Q ≈ 3, a few saturate
- CHECK (gs.resonance >= 0.0f && gs.resonance <= 1.0f);
- static int resonant = 0, strong = 0, total = 0;
- resonant += gs.resonance > 0.0f;
- strong += gs.resonance > 0.9f;
- if (++total % 50 == 0)
- std::printf (" Harmor resonance: %d with a peak, %d with Q > ~5 (of %d)\n", resonant, strong, total);
- }
- static int saw = 0, square = 0, other = 0, total = 0;
- if (decoded && std::any_of (gs.osc.begin(), gs.osc.end(), [] (const lynx::SynthOscillator& o) { return o.wave == lynx::Waveform::spectral; }))
- {
- auto isSaw = true, isSquare = true;
- for (int n = 1; n <= 16; ++n)
- {
- isSaw = isSaw && std::abs (gs.spectrum[(std::size_t) n - 1] - 1.0f / n) < 1e-3f;
- isSquare = isSquare && std::abs (gs.spectrum[(std::size_t) n - 1] - (n % 2 == 1 ? 1.1535f / n : 0.0f)) < 2e-3f;
- }
- ++(isSaw ? saw : isSquare ? square : other);
- if (++total % 20 == 0 || total == 100)
- std::printf (" Harmor spectrum: %d saw, %d square, %d other (of %d)\n", saw, square, other, total);
- }
- }
- else if (c.pluginName == "GMS")
- decoded = lynx::synthFromGms (c.pluginState, 128.0, gs, g);
- else if (c.pluginName == "Ogun" || c.pluginName == "Autogun")
- {
- decoded = lynx::synthFromOgun (c.pluginState, gs, g);
- // REA_rodada24 §1.9: the "Trumpet" preset (richness 7, no seeds) has partials 0.9087, 0.8610,
- // 0.8151… at the fixed seed-3 phases 0.919, 0.380, 0.130
- if (decoded && gs.osc[0].wave == lynx::Waveform::spectral)
- {
- CHECK (std::all_of (gs.spectrum.begin(), gs.spectrum.end(), [] (float a) { return std::isfinite (a) && a >= 0.0f; }));
- static constexpr float trumpet[3] { 0.9087f, 0.8610f, 0.8151f }, phases[3] { 0.919f, 0.380f, 0.130f };
- auto match = true;
- for (std::size_t k = 0; k < 3; ++k)
- match = match && std::abs (gs.spectrum[k] * 2.0f / 3.14159265f - trumpet[k]) < 1e-3f && std::abs (gs.spectrumPhase[k] - phases[k]) < 1e-3f;
- ogunTrumpet += match ? 1 : 0;
- }
- }
- else if (c.pluginName == "Sakura")
- decoded = lynx::synthFromSakura (c.pluginState, gs);
- else if (c.pluginName == "Kepler")
- decoded = lynx::synthFromKepler (c.pluginState, gs, g);
- else if (c.pluginName == "Wave Traveller")
- decoded = lynx::waveTravellerSample (c.pluginState).has_value() ? 2 : 0;
- else if (c.pluginName == "PoiZone")
- decoded = lynx::synthFromPoiZone (c.pluginState, gs, g);
- else if (c.pluginName == "BassDrum")
- decoded = lynx::synthFromBassDrum (c.pluginState, gs, g);
- else if (c.pluginName == "Toxic Biohazard")
- decoded = lynx::synthFromToxic (c.pluginState, gs, g);
- else if (c.pluginName == "Morphine")
- decoded = lynx::synthFromMorphine (c.pluginState, gs, g);
- else if (c.pluginName == "BooBass")
- decoded = lynx::synthFromBooBass (c.pluginState, gs);
- else if (c.pluginName == "Drumpad" || c.pluginName == "DrumPad")
- decoded = lynx::drumpadPatchName (c.pluginState).has_value() ? 2 : 0;
- else if (c.pluginName == "Fruity Envelope Controller" && c.pluginState.size() >= 4 && c.pluginState[2] >= 6) // v >= 6
- decoded = lynx::parseEnvelopeController (c.pluginState).has_value() ? 2 : 0;
- else if (c.pluginName == "FLEX")
- decoded = lynx::flexPresetName (c.pluginState).has_value() ? 2 : 0;
- else if (c.pluginName == "Fruity DrumSynth Live")
- decoded = lynx::drumSynthLiveParams (c.pluginState).has_value() ? 2 : 0;
- if (decoded < 0)
- continue;
- auto& counter = generators[c.pluginName];
- ++counter.first;
- counter.second += decoded != 0;
- if (decoded == 1 && c.pluginName != "Fruity DrumSynth Live")
- {
- CHECK (std::isfinite (g) && g >= 0.0f && gs.cutoff >= 20.0f && gs.cutoff <= 20000.0f && gs.sustain >= 0.0f && gs.sustain <= 1.0f);
- 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));
- for (const auto& o : gs.osc)
- CHECK (o.level >= 0.0f && o.level <= 4.0f && std::isfinite (o.detune) && std::isfinite (o.pan));
- }
- }
- // 3x Osc: every state in the demos decodes, with levels summing to at most 1
- for (const auto& c : p.channels)
- if (c.pluginName == "3x Osc")
- {
- ++threeOsc;
- lynx::SynthSettings s;
- if (lynx::synthFrom3xOsc (c.pluginState, s))
- {
- ++threeOscDecoded;
- // with AM, osc 3 is the modulation depth and is left out of the sum
- const auto sum = s.osc[0].level + s.osc[1].level + (s.am ? 0.0f : s.osc[2].level);
- CHECK (sum <= 1.001f && s.osc[2].level <= 1.0f);
- for (const auto& o : s.osc)
- CHECK (o.octave >= -2 && o.octave <= 2 && std::abs (o.semitones) <= 11 && std::abs (o.pan) <= 1.0f);
- }
- }
- for (auto& pat : p.patterns)
- if (! pat.notes.empty())
- {
- ++withNotes;
- break;
- }
- for (auto& a : p.arrangements)
- for (auto& item : a.items)
- CHECK (item.track >= 0 && item.track < 500);
- for (auto& clip : song.playlist)
- {
- CHECK (clip.pattern >= 0 && clip.pattern < (int) song.patterns.size());
- CHECK (clip.length > 0 && clip.offset >= 0);
- if (clip.kind == lynx::ClipKind::automation)
- CHECK (clip.automation >= 0 && clip.automation < (int) song.automations.size());
- }
- for (auto& a : song.automations)
- {
- ++automations;
- automationsWithTarget += a.target.kind != lynx::AutomationTargetKind::none;
- CHECK (a.isLive() || ! a.points.empty());
- for (std::size_t i = 1; i < a.points.size(); ++i)
- CHECK (a.points[i].tick >= a.points[i - 1].tick);
- }
- // imported routing has no cycles and valid destinations
- CHECK (song.mixerOrder().size() + 1 == song.mixer.size());
- for (auto& t : song.mixer)
- for (auto& s : t.sends)
- {
- ++sends;
- CHECK (s.target > 0 && s.target < (int) song.mixer.size());
- }
- if (! song.playlist.empty())
- ++withPlaylist;
- }
- catch (const flp::Error& e)
- {
- ++failures;
- std::printf (" FAILED %s: %s\n", entry.path().string().c_str(), e.what());
- }
- }
- CHECK (threeOscDecoded == threeOsc);
- std::printf (" Harmless: %d channels, %d decoded\n", harmless, harmlessDecoded);
- std::printf (" Sytrus: %d channels, %d decoded, %d by the engine (%d of %d rendered sound)\n", sytrus, sytrusDecoded, sytrusEngine,
- sytrusSounding, sytrusRendered);
- CHECK (sytrusEngine == sytrus);
- CHECK (sytrusSounding >= sytrusRendered * 3 / 4);
- for (const auto& [name, counter] : generators)
- {
- std::printf (" %s: %d channels, %d decoded\n", name.c_str(), counter.first, counter.second);
- CHECK (counter.second == counter.first);
- }
- std::printf (" Ogun spectra matching the Trumpet reference: %d\n", ogunTrumpet);
- CHECK (ogunTrumpet >= 1);
- // demo Gross Beat: the volume slot becomes the gate (those using an envelope slot get steps < 1)
- {
- int vocodexes = 0, vocodexSides = 0;
- int lovePhilterLinks[4] {}; // live links: cutoff, resonance; inside containers: cutoff, resonance
- int embeddedZones = 0, lovePhilters = 0, lovePhiltersRead = 0, lovePhiltersConverted = 0, gates = 0, shaped = 0, effectors = 0, patchers = 0, serial = 0, parallel = 0, generatorPatchers = 0;
- for (const auto& entry : std::filesystem::recursive_directory_iterator (std::filesystem::path (realFlpDir) / "Demo projects"))
- if (entry.path().extension() == ".flp")
- {
- std::ifstream in (entry.path(), std::ios::binary);
- const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
- try
- {
- const auto project = flp::parseProject (file);
- for (const auto& insert : project.mixer)
- for (const auto& slot : insert.slots)
- if (slot.plugin == "Gross Beat")
- {
- const auto fx = lynx::effectFromFl (slot.plugin, slot.state);
- CHECK (fx && fx->kind == lynx::EffectKind::volumeGate);
- ++gates;
- if (fx && std::any_of (fx->params.begin(), fx->params.begin() + 32, [] (float v) { return v < 0.99f; }))
- ++shaped;
- }
- else if (slot.plugin == "Fruity Love Philter")
- {
- ++lovePhilters;
- lovePhiltersRead += lynx::parseLovePhilter (slot.state).has_value();
- if (const auto conv = lynx::lovePhilterEffects (slot.state))
- {
- ++lovePhiltersConverted;
- CHECK (! conv->slots.empty());
- for (const auto& fx : conv->slots)
- for (auto v : fx.params)
- CHECK (std::isfinite (v) && v >= 0.0f && v <= 1.0f);
- for (const auto& fx : conv->slots)
- for (const auto& link : fx.links)
- {
- const auto& chain = fx.branches[(std::size_t) link.branch].chain;
- CHECK (link.source < (int) chain.size() && chain[(std::size_t) link.target].kind == lynx::EffectKind::parametricEq);
- lovePhilterLinks[2 + (link.param == lynx::peq::param (0, 3) ? 1 : 0)]++;
- }
- for (const auto& link : conv->links)
- {
- lovePhilterLinks[link.param == lynx::peq::param (0, 3) ? 1 : 0]++;
- CHECK (link.curve.size() == 33);
- for (auto v : link.curve)
- CHECK (std::isfinite (v) && v >= 0.0f && v <= 1.0f);
- }
- }
- }
- else if (slot.plugin == "Vocodex")
- {
- const auto v = lynx::vocodexEffect (slot.state);
- CHECK (v.has_value());
- vocodexes += v.has_value();
- vocodexSides += v && (v->modIn > 0 || v->carIn > 0);
- }
- else if (slot.plugin == "Effector")
- effectors += lynx::effectFromFl (slot.plugin, slot.state).has_value();
- else if (slot.plugin == "Patcher")
- {
- const auto graph = lynx::parsePatcher (slot.state);
- CHECK (graph.has_value());
- ++patchers;
- if (graph && ! graph->serialChain.empty())
- ++serial;
- if (graph && graph->serialChain.empty() && graph->audio)
- ++parallel;
- }
- for (const auto& c : project.channels)
- if (c.pluginName == "DirectWave")
- if (const auto program = lynx::directWaveProgram (c.pluginState))
- for (const auto& wav : program->embeddedWav)
- if (! wav.empty())
- {
- ++embeddedZones;
- CHECK (wav.size() > 44 && std::memcmp (wav.data(), "RIFF", 4) == 0);
- }
- for (const auto& c : project.channels)
- if (c.pluginName == "Patcher")
- {
- const auto graph = lynx::parsePatcher (c.pluginState);
- CHECK (graph.has_value());
- generatorPatchers += graph && graph->mainGenerator >= 0;
- }
- }
- catch (const flp::Error&) {}
- }
- std::printf (" Gross Beat: %d slots, %d with gate; Effector: %d converted; Love Philter: %d, %d read\n", gates, shaped, effectors,
- lovePhilters, lovePhiltersRead);
- CHECK (lovePhiltersRead == lovePhilters && lovePhiltersConverted == lovePhilters);
- std::printf (" Vocodex: %d converted, %d with side inputs\n", vocodexes, vocodexSides);
- CHECK (vocodexes >= 9);
- std::printf (" Love Philter live links: %d cutoff, %d resonance; in multi-unit containers: %d cutoff, %d resonance\n",
- lovePhilterLinks[0], lovePhilterLinks[1], lovePhilterLinks[2], lovePhilterLinks[3]);
- CHECK (lovePhilterLinks[2] + lovePhilterLinks[3] > 0);
- std::printf (" DirectWave: %d zones with embedded audio\n", embeddedZones);
- CHECK (embeddedZones == 12);
- std::printf (" Patcher: %d in the mixer (%d serial, %d reduced to serial/parallel), %d as instrument with generator\n", patchers, serial,
- parallel, generatorPatchers);
- CHECK (gates == 0 || shaped > 0);
- }
- // Envelope Controller "Basic Sidechain" (Future House): drops on the note and returns to ~1 within ~1/3 s
- {
- const auto path = std::filesystem::path (realFlpDir) / "Demo projects" / "Demo songs" / "Asher Postman - Future House.flp";
- if (std::filesystem::exists (path))
- {
- std::ifstream in (path, std::ios::binary);
- const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
- const auto song = lynx::importFlp (flp::parseProject (file));
- const lynx::ChannelDef* controller = nullptr;
- for (const auto& c : song.channels)
- if (c.instrument == lynx::InstrumentKind::controller)
- controller = &c;
- CHECK (controller != nullptr);
- if (controller != nullptr)
- {
- auto inst = lynx::makeInstrument (lynx::InstrumentKind::controller);
- inst->prepare (48000.0);
- inst->configure (*controller, song.bpm);
- std::vector<float> l (480), r (480);
- inst->render (l.data(), r.data(), 480, 1.0f, 1.0f);
- inst->noteOn (60, 1.0f);
- inst->render (l.data(), r.data(), 480, 1.0f, 1.0f); // 10 ms
- const auto ducked = inst->controlOutput (0);
- for (int i = 0; i < 100; ++i) // +1 s
- inst->render (l.data(), r.data(), 480, 1.0f, 1.0f);
- const auto recovered = inst->controlOutput (0);
- std::printf (" Envelope Controller (Basic Sidechain): %.2f on the note, %.2f after 1 s\n", ducked, recovered);
- CHECK (ducked < 0.3f && recovered > 0.9f);
- }
- }
- }
- // mutation fuzz: real states with flipped and truncated bytes must not crash the readers
- {
- std::map<std::string, std::vector<std::uint8_t>> samples; // one real blob per plugin
- for (const auto& entry : std::filesystem::recursive_directory_iterator (std::filesystem::path (realFlpDir) / "Demo projects"))
- {
- if (entry.path().extension() != ".flp")
- continue;
- std::ifstream in (entry.path(), std::ios::binary);
- const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
- try
- {
- const auto project = flp::parseProject (file);
- for (const auto& c : project.channels)
- if (! c.pluginState.empty())
- samples.emplace (c.pluginName, c.pluginState);
- for (const auto& insert : project.mixer)
- for (const auto& slot : insert.slots)
- if (! slot.state.empty())
- samples.emplace (slot.plugin, slot.state);
- }
- catch (const flp::Error&) {}
- }
- std::uint32_t seed = 12345;
- auto rnd = [&seed] { seed = seed * 1664525u + 1013904223u; return seed >> 8; };
- int mutations = 0;
- for (const auto& [name, original] : samples)
- for (int round = 0; round < 40; ++round)
- {
- auto blob = original;
- if (round % 4 == 3 && ! blob.empty())
- blob.resize (rnd() % blob.size()); // truncated
- else
- for (int k = 0; k < 1 + (int) (rnd() % 8) && ! blob.empty(); ++k)
- blob[rnd() % blob.size()] = (std::uint8_t) rnd();
- ++mutations;
- lynx::SynthSettings synth;
- float gain = 1.0f;
- lynx::synthFromSytrus (blob, 120.0, synth, gain);
- lynx::synthFromHarmor (blob, synth, gain);
- lynx::synthFromGms (blob, 120.0, synth, gain);
- lynx::synthFromOgun (blob, synth, gain);
- lynx::synthFromMorphine (blob, synth, gain);
- lynx::synthFromKepler (blob, synth, gain);
- lynx::synthFromPoiZone (blob, synth, gain);
- lynx::synthFromToxic (blob, synth, gain);
- lynx::synthFromBassDrum (blob, synth, gain);
- lynx::synthFromHarmless (blob, 120.0, synth, gain);
- lynx::harmlessParams (blob);
- if (lynx::harmorStateUsable (blob))
- lynx::harmorFallbacks (blob);
- lynx::parseEnvelopeController (blob);
- lynx::parseKeyboardController (blob);
- lynx::parseLovePhilter (blob);
- lynx::parsePatcher (blob);
- lynx::effectFromFl (name, blob);
- lynx::fpcPads (blob);
- lynx::directWaveProgram (blob);
- CHECK (std::isfinite (gain) && std::isfinite (synth.cutoff));
- }
- std::printf (" mutation fuzz: %d blobs from %d plugins\n", mutations, (int) samples.size());
- }
- // WavPack (.wv) samples from FL packs: all decode, with finite, non-silent audio
- {
- int files = 0, decoded = 0, audible = 0;
- double seconds = 0.0;
- for (const auto& entry : std::filesystem::recursive_directory_iterator (std::filesystem::path (realFlpDir) / "Patches" / "Packs"))
- if (entry.path().extension() == ".wv")
- {
- std::ifstream in (entry.path(), std::ios::binary);
- const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
- ++files;
- lynx::DecodedAudio audio;
- if (! lynx::decodeWavPack (file.data(), file.size(), audio))
- continue;
- ++decoded;
- float peak = 0.0f;
- bool finite = true;
- for (const auto& ch : audio.channels)
- for (auto v : ch)
- {
- finite = finite && std::isfinite (v);
- peak = std::max (peak, std::abs (v));
- }
- CHECK (finite && peak <= 4.0f);
- audible += peak > 1e-4f;
- seconds += (double) audio.channels[0].size() / audio.sampleRate;
- }
- std::printf (" WavPack: %d .wv files, %d decoded, %d audible, %.0f s of audio\n", files, decoded, audible, seconds);
- CHECK (decoded == files);
- }
- // native GMS presets (.gmsynth = the contents of 0xD5 without the "SSLF")
- int gmsPresets = 0, gmsDecoded = 0;
- const auto gmsFolder = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "GMS";
- if (std::filesystem::exists (gmsFolder))
- for (const auto& entry : std::filesystem::recursive_directory_iterator (gmsFolder))
- if (entry.path().extension() == ".gmsynth")
- {
- std::ifstream in (entry.path(), std::ios::binary);
- std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
- std::vector<std::uint8_t> blob { 'S', 'S', 'L', 'F' };
- const auto n = (std::uint32_t) file.size();
- blob.insert (blob.end(), reinterpret_cast<const std::uint8_t*> (&n), reinterpret_cast<const std::uint8_t*> (&n) + 4);
- blob.insert (blob.end(), file.begin(), file.end());
- lynx::SynthSettings gs;
- float g = 1.0f;
- ++gmsPresets;
- if (lynx::synthFromGms (blob, 120.0, gs, g))
- {
- ++gmsDecoded;
- CHECK (std::isfinite (g) && gs.cutoff >= 20.0f && std::isfinite (gs.pitchEnv.amount) && std::isfinite (gs.lfo[lynx::lfoPitch].period));
- }
- }
- std::printf (" GMS presets: %d, %d decoded\n", gmsPresets, gmsDecoded);
- // the synth's "FL presets" button: Sytrus/Harmor .fst and .gmsynth become synth settings
- int presetFiles = 0, presetsRead = 0;
- for (const auto* name : { "Sytrus", "Harmor", "GMS", "Harmless", "Ogun", "Sakura", "Fruit Kick", "Fruity DX10", "MiniSynth", "Kepler",
- "PoiZone", "Toxic Biohazard", "BassDrum" })
- {
- const auto folder = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / name;
- if (! std::filesystem::exists (folder))
- continue;
- for (const auto& entry : std::filesystem::recursive_directory_iterator (folder))
- if (entry.path().extension() == ".fst" || entry.path().extension() == ".gmsynth")
- {
- std::ifstream in (entry.path(), std::ios::binary);
- const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
- ++presetFiles;
- presetsRead += lynx::synthFromFlPreset (file).has_value();
- }
- }
- std::printf (" FL synth presets: %d files, %d read\n", presetFiles, presetsRead);
- CHECK (presetsRead >= presetFiles * 95 / 100);
- CHECK (gmsDecoded == gmsPresets);
- CHECK (sytrusDecoded == sytrus);
- CHECK (harmlessDecoded == harmless && harmless > 100);
- std::printf (" Harmless: %d states parsed for the engine, %d channels imported as Harmless\n", harmlessParsed, harmlessKind);
- CHECK (harmlessParsed == harmless && harmlessKind >= harmless);
- std::printf (" Harmor: %d channels, %d states usable by the engine, %d imported as Harmor (%d sounding)\n", harmor, harmorUsable, harmorKind, harmorSounding);
- CHECK (harmorUsable == harmor && harmor > 90 && harmorKind >= harmor && harmorSounding >= harmorKind * 9 / 10);
- 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",
- gms, gmsUsable, gmsKind, gmsSounding, morphine, morphineUsable, morphineKind, morphineSounding, demoGoldens);
- CHECK (gmsUsable == gms && gms >= 9 && gmsKind == gms && gmsSounding == gmsKind);
- std::printf (" MiniSynth channels: %d, %d usable, %d on the exact engine, %d sounding, %d golden\n", miniSynth, miniSynthUsable, miniSynthKind,
- miniSynthSounding, miniSynthGoldens);
- CHECK (miniSynthUsable == miniSynth && miniSynth >= 12 && miniSynthKind == miniSynth && miniSynthSounding == miniSynthKind && miniSynthGoldens == 2);
- CHECK (morphineUsable == morphine && morphine >= 7 && morphineKind == morphine && morphineSounding == morphineKind && demoGoldens == 2);
- std::printf (" Ogun: %d channels, %d states usable by the engine, %d imported as Ogun (%d sounding)\n", ogun, ogunUsable, ogunKind, ogunSounding);
- CHECK (ogunUsable == ogun && ogun >= 20 && ogunKind >= ogun && ogunSounding >= ogunKind * 9 / 10);
- std::printf (" Fruity DX10: %d channels, %d states usable by the engine, %d imported as DX10 (%d sounding)\n", dx10, dx10Usable, dx10Kind, dx10Sounding);
- CHECK (dx10Usable == dx10 && dx10 >= 12 && dx10Kind == dx10 && dx10Sounding == dx10Kind);
- std::printf (" Kepler: %d channels, %d states usable by the engine, %d imported as Kepler (%d sounding)\n", kepler, keplerUsable, keplerKind, keplerSounding);
- CHECK (keplerUsable == kepler && kepler >= 4 && keplerSounding == keplerKind && (keplerKind == 0 || keplerKind == kepler));
- std::printf (" Harmless rack compressor: %d of %d presets read from the installation\n", harmlessCompLoaded, harmlessCompWanted);
- CHECK (harmlessCompLoaded == harmlessCompWanted && harmlessCompWanted > 100);
- std::printf (" Maximus: %d of %d slots on the exact engine; Soundgoodizer: %d of %d; Vocodex with SG.dat: %d of %d\n", maximusExact,
- maximusSlots, sgzExact, sgzSlots, vocodexWithData, vocodexSlots);
- // REA_rodada40 §6: in Demo projects the 4 states left on the approximation use the Frequency Splitter linear-phase
- // option; the other Data folders add states with options not decoded
- CHECK (maximusSlots - maximusExact <= 16 && maximusExact >= 97 && sgzExact == sgzSlots && sgzSlots > 90);
- CHECK (vocodexWithData == vocodexSlots && vocodexSlots >= 10);
- std::printf (" Fruity Delay 2: %d of %d slots on the exact engine; Fruity Delay 3: %d of %d\n", delay2Exact, delay2Slots,
- delay3Exact, delay3Slots);
- CHECK (delay2Exact == delay2Slots && delay2Slots >= 60 && delay3Exact == delay3Slots && delay3Slots >= 100); // REA_rodada52
- // 3x Osc channel presets shipped with FL (.fst): all become synth settings
- int presets = 0, presetsDecoded = 0;
- const auto presetDir = std::filesystem::path (realFlpDir) / "Patches" / "Channel presets" / "3x Osc";
- if (std::filesystem::is_directory (presetDir))
- for (auto& entry : std::filesystem::directory_iterator (presetDir))
- if (entry.path().extension() == ".fst")
- {
- ++presets;
- std::ifstream in (entry.path(), std::ios::binary);
- std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
- if (auto synth = lynx::synthFromFlPreset (bytes))
- {
- ++presetsDecoded;
- CHECK (synth->osc[0].level + synth->osc[1].level + (synth->am ? 0.0f : synth->osc[2].level) <= 1.001f);
- }
- }
- std::printf (" %d 3x Osc presets, %d read\n", presets, presetsDecoded);
- CHECK (presetsDecoded == presets);
- // FL effect presets that become built-in effects
- int fxPresets = 0, fxDecoded = 0;
- for (auto kind : { lynx::EffectKind::parametricEq, lynx::EffectKind::limiter, lynx::EffectKind::reverb, lynx::EffectKind::delay,
- lynx::EffectKind::utility })
- for (const auto& plugin : lynx::flPluginsForEffect (kind))
- {
- const auto dir = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Effects" / plugin;
- if (! std::filesystem::is_directory (dir))
- continue;
- for (auto& entry : std::filesystem::recursive_directory_iterator (dir))
- if (entry.path().extension() == ".fst")
- {
- ++fxPresets;
- std::ifstream in (entry.path(), std::ios::binary);
- std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
- fxDecoded += lynx::effectFromFlPreset (bytes).has_value();
- }
- }
- std::printf (" %d FL effect presets, %d read\n", fxPresets, fxDecoded);
- CHECK (fxDecoded == fxPresets);
- CHECK (wrappedParsed == wrapped);
- std::printf (" %d plugins in Fruity Wrapper, %d read\n", wrapped, wrappedParsed);
- std::printf (" %d markers, %d time signature changes, %d tracks with EQ\n", markers, signatures, trackEqs);
- std::printf (" %d parametric EQs (%d with FL settings), %d reverbs (%d without dry)\n", peqSlots, peqShaped, reverbs, reverbsDryless);
- std::printf (" DirectWave: %d channels, %d zones; FPC: %d projects, %d pad channels, %d pad notes\n", dwChannels, dwZones, fpcKits,
- padChannels, padNotes);
- CHECK (dwZones > 1000 && padChannels > 250 && padNotes > 500);
- std::printf (" sliced channels (Slicer/Slicex): %d; embedded WAVs saved: %d\n", slicedChannels, embeddedSaved);
- CHECK (slicedChannels >= 25 && embeddedSaved >= 10); // without decoding Ogg here, some loops have no duration
- std::printf (" FL Keys (Grand Piano, 30 zones): %d channels\n", flKeysChannels);
- CHECK (flKeysChannels >= 8);
- CHECK (peqSlots > 1000 && peqShaped > 900 && reverbsDryless > 20 && reverbsLowCut > 250);
- CHECK (markers > 300 && signatures > 60);
- std::printf (" %d 3x Osc channels, %d decoded\n", threeOsc, threeOscDecoded);
- std::printf (" %d .flp files read, %d with notes, %d with playlist, %d automations (%d with target), %d sends\n",
- files, withNotes, withPlaylist, automations, automationsWithTarget, sends);
- CHECK (files > 0);
- CHECK (automations > 0 && sends > 0);
- }
- // ---------------------------------------------------------------------------
- // Sequencer
- TEST (pattern_toggle_step_and_length)
- {
- lynx::Pattern p;
- CHECK (! p.hasStep (0, 4));
- p.toggleStep (0, 4);
- CHECK (p.hasStep (0, 4));
- CHECK (p.notes.size() == 1 && p.notes[0].start == 4 * lynx::ticksPerStep);
- CHECK (p.effectiveLength (4) == 4 * lynx::ppq); // one bar
- p.toggleStep (1, 20); // second bar
- CHECK (p.effectiveLength (4) == 8 * lynx::ppq);
- p.toggleStep (0, 4);
- CHECK (! p.hasStep (0, 4) && p.notes.size() == 1);
- }
- TEST (scheduler_places_events_at_exact_samples)
- {
- lynx::Pattern p;
- p.toggleStep (0, 0);
- p.toggleStep (0, 4); // tick 960 = 1 beat
- const double sr = 48000.0, bpm = 120.0; // 1 beat = 0.5 s = 24000 samples
- const auto tps = lynx::ticksPerSample (bpm, sr);
- lynx::EventBuffer ev;
- lynx::schedulePatternLoop (p, p.effectiveLength (4), 23900.0 * tps, 512, tps, ev);
- // the beat-2 note starts at sample 24000 -> offset 100 in this block
- int ons = 0;
- for (auto& e : ev)
- if (e.type == lynx::NoteEvent::Type::on)
- {
- ++ons;
- CHECK (e.sampleOffset == 100);
- }
- CHECK (ons == 1);
- }
- TEST (scheduler_wraps_the_loop)
- {
- lynx::Pattern p;
- p.toggleStep (2, 0);
- const auto len = p.effectiveLength (4); // 3840 ticks
- const double sr = 48000.0;
- const auto tps = lynx::ticksPerSample (120.0, sr);
- lynx::EventBuffer ev;
- // block that crosses the loop end: starts 10 samples before
- const auto start = (double) len - 10.0 * tps;
- lynx::schedulePatternLoop (p, len, start, 64, tps, ev);
- bool found = false;
- for (auto& e : ev)
- if (e.type == lynx::NoteEvent::Type::on && e.channel == 2)
- {
- found = true;
- CHECK (e.sampleOffset == 10);
- }
- CHECK (found);
- }
- TEST (scheduler_off_before_on_at_same_sample)
- {
- lynx::Pattern p;
- lynx::Note a;
- a.start = 0; a.length = 240; a.channel = 0;
- lynx::Note b = a;
- b.start = 240;
- p.notes = { a, b };
- const auto tps = lynx::ticksPerSample (120.0, 48000.0);
- lynx::EventBuffer ev;
- lynx::schedulePatternLoop (p, p.effectiveLength (4), 200.0, 4000, tps, ev);
- CHECK (ev.size() >= 2);
- CHECK (ev[0].type == lynx::NoteEvent::Type::off);
- CHECK (ev[1].type == lynx::NoteEvent::Type::on);
- CHECK (ev[0].sampleOffset == ev[1].sampleOffset);
- }
- TEST (snapshot_exchange_hands_over_and_recycles)
- {
- struct Counted
- {
- int value;
- int* alive;
- Counted (int v, int* a) : value (v), alive (a) { ++*alive; }
- ~Counted() { --*alive; }
- };
- int alive = 0;
- {
- lynx::SnapshotExchange<Counted> x;
- CHECK (x.acquire() == nullptr);
- x.publish (std::make_unique<Counted> (1, &alive));
- CHECK (x.acquire()->value == 1);
- x.publish (std::make_unique<Counted> (2, &alive));
- x.publish (std::make_unique<Counted> (3, &alive)); // 2 was never read: freed immediately
- CHECK (alive == 2);
- CHECK (x.acquire()->value == 3);
- CHECK (alive == 2); // 1 was returned, but only the UI frees it
- x.collectGarbage();
- CHECK (alive == 1);
- // without collection, the return slots fill up and the audio side holds on to the current one
- for (int i = 0; i < 20; ++i)
- {
- x.publish (std::make_unique<Counted> (10 + i, &alive));
- CHECK (x.acquire() != nullptr);
- }
- }
- CHECK (alive == 0);
- }
- TEST (rack_renderer_makes_sound_and_reuses_instruments)
- {
- auto song = lynx::Song::makeDefault();
- song.bpm = 120.0;
- const double sr = 48000.0;
- auto state = lynx::RackState::build (song, nullptr, sr);
- lynx::RackRenderer r;
- r.prepare (sr);
- std::vector<float> l (512), rr (512);
- double tick = 0.0;
- float peak = 0.0f;
- int kicks = 0;
- for (int block = 0; block < 93; ++block) // 47616 samples: just before 1 s
- {
- r.process (state.get(), true, tick, 512, l.data(), rr.data());
- for (auto& e : r.lastEvents())
- if (e.type == lynx::NoteEvent::Type::on && e.channel == 0)
- ++kicks;
- for (int i = 0; i < 512; ++i)
- {
- CHECK (std::isfinite (l[(std::size_t) i]));
- peak = std::max (peak, std::abs (l[(std::size_t) i]));
- }
- tick += 512 * lynx::ticksPerSample (song.bpm, sr);
- }
- CHECK (peak > 0.1f && peak < 4.0f);
- CHECK (kicks == 2); // 120 BPM: beats at 0 and 0.5 s
- // editing the song keeps the instruments of channels whose type didn't change
- song.channels[4].instrument = lynx::InstrumentKind::kick;
- auto next = lynx::RackState::build (song, state.get(), sr);
- CHECK (next->instruments[0] == state->instruments[0]);
- CHECK (next->instruments[4] != state->instruments[4]);
- // stopping silences the synth and triggers no new notes
- r.process (next.get(), false, tick, 512, l.data(), rr.data());
- CHECK (r.lastEvents().size() == 0);
- }
- TEST (flp_import_maps_channels_and_rescales)
- {
- flp::Project p;
- p.header.ppq = 96;
- p.tempo = 150.0;
- p.channels = { { 0, flp::ChannelType::sampler, "Kick 808", "", "", 1 },
- { 1, flp::ChannelType::audioClip, "Vocal", "", "", 2 },
- { 2, flp::ChannelType::instrument, "Lead", "FLEX", "", 3 } };
- flp::Pattern pat;
- pat.index = 1;
- flp::Note n;
- n.position = 96;
- n.length = 0;
- n.channel = 2;
- n.key = 72;
- n.velocity = 64;
- pat.notes.push_back (n);
- p.patterns.push_back (pat);
- const auto s = lynx::importFlp (p);
- CHECK (s.bpm == 150.0);
- CHECK (s.channels.size() == 3); // audio clip channels stay in the rack, as in FL
- CHECK (s.channels[0].instrument == lynx::InstrumentKind::kick);
- CHECK (s.channels[1].instrument == lynx::InstrumentKind::audioClip);
- CHECK (s.channels[2].instrument == lynx::InstrumentKind::synth);
- CHECK (s.patterns.size() == 1);
- CHECK (s.patterns[0].notes[0].start == lynx::ppq); // 96 @ PPQ 96 = 1 beat
- CHECK (s.patterns[0].notes[0].channel == 2);
- CHECK (s.patterns[0].notes[0].length == lynx::ticksPerStep); // step note
- CHECK (std::abs (s.patterns[0].notes[0].velocity - 0.5f) < 1e-6f);
- }
- TEST (live_queue_is_fifo_and_bounded)
- {
- lynx::LiveEventQueue q;
- lynx::NoteEvent e;
- CHECK (! q.pop (e));
- for (int i = 0; i < 10; ++i)
- {
- lynx::NoteEvent in;
- in.key = i;
- CHECK (q.push (in));
- }
- for (int i = 0; i < 10; ++i)
- CHECK (q.pop (e) && e.key == i);
- int pushed = 0;
- while (q.push (e))
- ++pushed;
- CHECK (pushed == (int) lynx::LiveEventQueue::capacity - 1); // one slot stays free to tell full from empty
- }
- TEST (sampler_plays_pitched_one_shot)
- {
- // 1000-frame ramp sample: the position can be measured from the amplitude
- auto data = std::make_shared<lynx::SampleData>();
- data->sampleRate = 48000.0;
- for (int i = 0; i < 1000; ++i)
- data->left.push_back (1.0f);
- auto sampler = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
- sampler->prepare (48000.0);
- std::vector<float> l (2048, 0.0f), r (2048, 0.0f);
- sampler->noteOn (60, 1.0f);
- sampler->noteOff (60); // one-shot: noteOff doesn't cut
- sampler->render (l.data(), r.data(), 2048, 1.0f, 1.0f);
- int sounding = 0;
- for (auto s : l)
- sounding += s > 0.5f;
- CHECK (sounding >= 995 && sounding <= 1000); // plays the whole sample at the original pitch
- std::fill (l.begin(), l.end(), 0.0f);
- sampler->noteOn (72, 1.0f); // one octave up: half the duration
- sampler->render (l.data(), r.data(), 2048, 1.0f, 1.0f);
- sounding = 0;
- for (auto s : l)
- sounding += s > 0.5f;
- CHECK (sounding >= 495 && sounding <= 500);
- CHECK (r[10] > 0.5f); // mono goes to both sides
- auto silent = lynx::makeInstrument (lynx::InstrumentKind::sampler, nullptr);
- silent->prepare (48000.0);
- silent->noteOn (60, 1.0f);
- std::fill (l.begin(), l.end(), 0.0f);
- silent->render (l.data(), r.data(), 64, 1.0f, 1.0f);
- CHECK (l[0] == 0.0f);
- }
- TEST (rack_live_notes_and_sample_provider)
- {
- lynx::Song song;
- lynx::ChannelDef c;
- c.name = "Smp";
- c.instrument = lynx::InstrumentKind::sampler;
- c.samplePath = "a.wav";
- song.channels.push_back (c);
- song.patterns.emplace_back();
- int loads = 0;
- auto provider = [&] (const std::string& path) -> std::shared_ptr<const lynx::SampleData> {
- ++loads;
- CHECK (path == "a.wav");
- auto d = std::make_shared<lynx::SampleData>();
- d->left.assign (4800, 0.5f);
- return d;
- };
- auto state = lynx::RackState::build (song, nullptr, 48000.0, provider);
- CHECK (loads == 1);
- lynx::RackRenderer renderer;
- renderer.prepare (48000.0);
- lynx::LiveEventQueue live;
- lynx::NoteEvent on;
- on.channel = 0;
- on.velocity = 1.0f;
- live.push (on);
- std::vector<float> l (256), r (256);
- renderer.process (state.get(), false, 0.0, 256, l.data(), r.data(), &live); // stopped, but the live note plays
- CHECK (l[0] > 0.1f);
- // same sample: reused; different sample: reloaded
- auto same = lynx::RackState::build (song, state.get(), 48000.0, provider);
- CHECK (loads == 1 && same->instruments[0] == state->instruments[0]);
- song.channels[0].samplePath = "b.wav";
- auto other = lynx::RackState::build (song, state.get(), 48000.0, [&] (const std::string&) { ++loads; return nullptr; });
- CHECK (loads == 2 && other->instruments[0] != state->instruments[0]);
- }
- TEST (flp_paths_resolve_and_samplers_import)
- {
- lynx::FlPathRoots roots { "C:\\FL", "C:\\FL\\Data", "D:\\User" };
- CHECK (lynx::resolveFlPath ("%FLStudioFactoryData%\\Data\\x.wav", roots) == "C:\\FL\\Data\\x.wav");
- CHECK (lynx::resolveFlPath ("%FLStudioData%\\Patches\\y.wav", roots) == "C:\\FL\\Data\\Patches\\y.wav");
- CHECK (lynx::resolveFlPath ("%FLStudioUserData%\\z.wav", roots) == "D:\\User\\z.wav");
- CHECK (lynx::resolveFlPath ("E:\\abs.wav", roots) == "E:\\abs.wav");
- CHECK (lynx::resolveFlPath ("%FLStudioUserData%\\z.wav", {}) == "");
- flp::Project p;
- p.channels = { { 0, flp::ChannelType::sampler, "Kick", "", "%FLStudioData%\\k.wav", 1 },
- { 1, flp::ChannelType::sampler, "Hat", "", "%FLStudioData%\\missing.wav", 1 } };
- lynx::FlpImportOptions options;
- options.roots = roots;
- options.fileExists = [] (const std::string& path) { return path == "C:\\FL\\Data\\k.wav"; };
- const auto s = lynx::importFlp (p, options);
- CHECK (s.channels[0].instrument == lynx::InstrumentKind::sampler);
- CHECK (s.channels[0].samplePath == "C:\\FL\\Data\\k.wav");
- CHECK (s.channels[1].instrument == lynx::InstrumentKind::hat); // no file: built-in instrument
- }
- TEST (soft_clip_is_transparent_below_knee_and_bounded)
- {
- float s[] = { 0.5f, -0.89f, 1.5f, -3.0f, 0.95f };
- lynx::softClip (s, 5);
- CHECK (s[0] == 0.5f && s[1] == -0.89f);
- CHECK (s[2] > 0.9f && s[2] <= 1.0f);
- CHECK (s[3] < -0.9f && s[3] >= -1.0f);
- CHECK (s[4] > 0.9f && s[4] < 0.95f);
- }
- TEST (song_scheduler_plays_clips_with_offset_and_cut)
- {
- lynx::Song s;
- s.channels.resize (1);
- auto& p = s.patterns.emplace_back();
- for (int step : { 0, 4, 8, 12 }) // one note per beat
- p.toggleStep (0, step);
- // clip at bar 2 playing only the second half of the pattern (beats 3 and 4)
- lynx::Clip c;
- c.pattern = 0;
- c.start = 4 * lynx::ppq;
- c.length = 2 * lynx::ppq;
- c.offset = 2 * lynx::ppq;
- s.playlist.push_back (c);
- CHECK (s.songLength() == 8 * lynx::ppq);
- const double sr = 48000.0;
- const auto tps = lynx::ticksPerSample (120.0, sr); // 1 beat = 24000 samples
- lynx::EventBuffer ev;
- // whole song in a single block (8 beats = 192000 samples)
- lynx::scheduleSong (s, 0.0, 192000, tps, ev);
- std::vector<int> ons;
- for (auto& e : ev)
- if (e.type == lynx::NoteEvent::Type::on)
- ons.push_back (e.sampleOffset);
- CHECK (ons.size() == 2);
- CHECK (ons.size() == 2 && ons[0] == 4 * 24000 && ons[1] == 5 * 24000);
- // crossing the loop: bar 1 is empty, so nothing at the start of the second pass
- ev.clear();
- lynx::scheduleSong (s, 8.0 * lynx::ppq - 100 * tps, 200, tps, ev);
- CHECK (ev.size() == 0);
- // a note longer than the clip is cut at the clip end
- p.notes[3].length = 4 * lynx::ppq;
- ev.clear();
- lynx::scheduleSong (s, 0.0, 192000, tps, ev);
- int lastOff = -1;
- for (auto& e : ev)
- if (e.type == lynx::NoteEvent::Type::off)
- lastOff = std::max (lastOff, e.sampleOffset);
- CHECK (lastOff == 6 * 24000);
- }
- TEST (remove_pattern_fixes_clips)
- {
- lynx::Song s;
- s.patterns.resize (3);
- s.currentPattern = 2;
- s.playlist = { { 0, 0, 0, 100, 0 }, { 1, 0, 100, 100, 0 }, { 2, 1, 0, 100, 0 } };
- s.removePattern (1);
- CHECK (s.patterns.size() == 2);
- CHECK (s.playlist.size() == 2 && s.playlist[1].pattern == 1);
- CHECK (s.currentPattern == 1);
- }
- TEST (flp_import_3x_osc_state_and_channel_filter)
- {
- // new 3x Osc patch (version 14): osc 2 mix 64 coarse -12, osc 3 mix 32 coarse -24
- std::vector<std::uint8_t> blob (460, 0);
- auto put = [&] (std::size_t off, std::int32_t v) {
- for (int b = 0; b < 4; ++b)
- blob[off + (std::size_t) b] = (std::uint8_t) ((std::uint32_t) v >> (8 * b));
- };
- put (0x00, 14);
- put (0x08, 3); // osc 1 saw
- put (0x10, 7); // osc 1 fine +7
- put (0x04, -32); // osc 1 pan -0.5
- put (0x1C, 64); // osc 2 mix
- put (0x28, -12); // osc 2 coarse
- put (0x38, 32); // osc 3 mix
- put (0x40, 2); // osc 3 square
- put (0x44, -19); // osc 3 coarse: -1 octave and -7 semitones
- lynx::SynthSettings s;
- CHECK (lynx::synthFrom3xOsc (blob, s));
- 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);
- CHECK (s.osc[0].wave == lynx::Waveform::saw && s.osc[0].detune == 7.0f && s.osc[0].pan == -0.5f);
- CHECK (s.osc[1].wave == lynx::Waveform::sine && s.osc[1].octave == -1 && s.osc[1].semitones == 0);
- CHECK (s.osc[2].wave == lynx::Waveform::square && s.osc[2].octave == -1 && s.osc[2].semitones == -7);
- CHECK (! lynx::synthFrom3xOsc (std::vector<std::uint8_t> (40, 0), s));
- // channel filter: open by default; LP with Mod X 128 ~ 2.71 kHz; Fast LP with Mod X 128 ~ 280 Hz
- float hz = 0.0f, res = 1.0f;
- int mode = -1;
- lynx::synthFilterFromChannel (256, 0, 0, hz, res, mode);
- CHECK (hz >= 19999.0f && res == 0.0f);
- lynx::synthFilterFromChannel (128, 0, 1, hz, res, mode);
- CHECK (std::abs (hz - 2710.0f) < 30.0f && res == 0.0f);
- lynx::synthFilterFromChannel (128, 0, 0, hz, res, mode);
- CHECK (hz > 240.0f && hz < 320.0f);
- lynx::synthFilterFromChannel (128, 256, 1, hz, res, mode);
- CHECK (res > 0.8f);
- CHECK (mode == 0);
- lynx::synthFilterFromChannel (64, 0, 3, hz, res, mode); // high-pass at ~840 Hz
- CHECK (mode == 2 && std::abs (hz - 840.0f) < 40.0f);
- lynx::synthFilterFromChannel (128, 0, 2, hz, res, mode); // band-pass
- CHECK (mode == 1);
- }
- TEST (synth_pitch_and_filter_envelopes)
- {
- auto render = [] (const lynx::SynthSettings& s, int frames) {
- auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
- inst->prepare (48000.0);
- lynx::ChannelDef c;
- c.synth = s;
- inst->configure (c, 120.0);
- inst->noteOn (45, 1.0f); // A2 = 110 Hz
- std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
- inst->render (l.data(), r.data(), frames, 1.0f, 1.0f);
- return l;
- };
- auto crossings = [] (const std::vector<float>& b, int from, int to) {
- int n = 0;
- for (int i = from + 1; i < to; ++i)
- n += (b[(std::size_t) i - 1] < 0.0f) != (b[(std::size_t) i] < 0.0f);
- return n;
- };
- // 3x Osc "kick": sine with a +24 st pitch envelope falling fast
- lynx::SynthSettings kick;
- kick.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
- kick.cutoff = 20000.0f;
- kick.pitchEnv = { 24.0f, 0.0f, 0.0f, 0.05f, 0.0f, 0.05f };
- const auto k = render (kick, 48000);
- const auto early = crossings (k, 0, 2400); // first 50 ms: well above 110 Hz
- const auto late = crossings (k, 24000, 48000); // then back to 110 Hz (~110 crossings in 0.5 s)
- CHECK (early > 20); // +24 st ramp over 50 ms: ~2.2x the average frequency (11 crossings without envelope)
- CHECK (std::abs (late - 110) < 4);
- // cutoff envelope: saw closed at 200 Hz opens +5 octaves on attack and closes again
- lynx::SynthSettings pluck;
- pluck.cutoff = 200.0f;
- pluck.filterEnv = { 5.0f, 0.0f, 0.0f, 0.1f, 0.0f, 0.1f };
- const auto p = render (pluck, 48000);
- auto hf = [&] (int from, int to) { // energy of the difference (highs)
- double e = 0.0;
- for (int i = from + 1; i < to; ++i)
- e += std::pow ((double) p[(std::size_t) i] - p[(std::size_t) i - 1], 2.0);
- return e;
- };
- CHECK (hf (0, 2400) > 4.0 * hf (40000, 42400));
- // Toxic Biohazard's envelope (REA_rodada33 §2): exponential towards 5 clipped at 1, so the cutoff is still near
- // its base early in the attack, fully open at A = 0.2 s and back down after the exponential decay
- lynx::SynthSettings toxic = pluck;
- toxic.filterEnv = { 5.0f, 0.2f, 0.0f, 0.1f, 0.0f, 0.1f, 1 };
- const auto t = render (toxic, 48000);
- auto thf = [&] (int from, int to) {
- double e = 0.0;
- for (int i = from + 1; i < to; ++i)
- e += std::pow ((double) t[(std::size_t) i] - t[(std::size_t) i - 1], 2.0);
- return e;
- };
- CHECK (thf (9000, 9600) > 4.0 * thf (300, 900));
- CHECK (thf (9000, 9600) > 4.0 * thf (40000, 40600));
- }
- TEST (synth_mono_reuses_the_sounding_voice)
- {
- auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
- inst->prepare (48000.0);
- lynx::ChannelDef c;
- c.synth.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
- c.synth.cutoff = 20000.0f;
- c.synth.mono = true;
- inst->configure (c, 120.0);
- std::vector<float> l (48000), r (48000);
- inst->noteOn (57, 1.0f);
- inst->render (l.data(), r.data(), 4800, 1.0f, 1.0f);
- inst->noteOn (69, 1.0f); // A4 takes over the A3 voice: only one note sounding
- inst->render (l.data() + 4800, r.data() + 4800, 43200, 1.0f, 1.0f);
- int crossings = 0;
- float peak = 0.0f;
- for (std::size_t i = 24001; i < 48000; ++i)
- {
- crossings += (l[i - 1] < 0.0f) != (l[i] < 0.0f);
- peak = std::max (peak, std::abs (l[i]));
- }
- CHECK (std::abs (crossings - 440) < 4); // 0.5 s of 440 Hz, without the A3 underneath
- CHECK (peak < 0.35f); // one voice (0.3 level), not two summed
- // no click on the handover: the jump between neighbouring samples stays sine-sized
- float jump = 0.0f;
- for (std::size_t i = 4790; i < 4810; ++i)
- jump = std::max (jump, std::abs (l[i] - l[i - 1]));
- CHECK (jump < 0.05f);
- }
- TEST (synth_porta_glides_and_am_modulates)
- {
- auto make = [] (const lynx::SynthSettings& s) {
- auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
- inst->prepare (48000.0);
- lynx::ChannelDef c;
- c.synth = s;
- inst->configure (c, 120.0);
- return inst;
- };
- auto crossings = [] (const std::vector<float>& b, std::size_t from, std::size_t to) {
- int n = 0;
- for (auto i = from + 1; i < to; ++i)
- n += (b[i - 1] < 0.0f) != (b[i] < 0.0f);
- return n;
- };
- // mono + 1-beat porta (0.5 s at 120 BPM): from A3 to A4, halfway ~E4 (329.6 Hz)
- lynx::SynthSettings s;
- s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
- s.cutoff = 20000.0f;
- s.mono = true;
- s.porta = true;
- s.portaBeats = 1.0f;
- auto inst = make (s);
- std::vector<float> l (96000), r (96000);
- inst->noteOn (57, 1.0f);
- inst->render (l.data(), r.data(), 4800, 1.0f, 1.0f);
- inst->noteOn (69, 1.0f);
- inst->render (l.data() + 4800, r.data() + 4800, 91200, 1.0f, 1.0f);
- const auto middle = crossings (l, 4800 + 12000 - 2400, 4800 + 12000 + 2400); // 0.1 s around 0.25 s
- CHECK (std::abs (middle - 66) < 6); // ~330 Hz * 0.1 s * 2
- CHECK (std::abs (crossings (l, 48000, 96000) - 880) < 4); // reached A4 (1 s = 880 crossings)
- // AM: osc 3 at level 1.0, slow sine, modulates osc 1 (the average level drops, the sum doesn't gain osc 3)
- lynx::SynthSettings am;
- am.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
- am.osc[2] = { lynx::Waveform::sine, -3, 0.0f, 1.0f };
- am.cutoff = 20000.0f;
- am.am = true;
- auto modulated = make (am);
- modulated->noteOn (69, 1.0f);
- std::fill (l.begin(), l.end(), 0.0f);
- modulated->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
- float peak = 0.0f;
- for (std::size_t i = 4800; i < 48000; ++i)
- peak = std::max (peak, std::abs (l[i]));
- CHECK (peak > 0.45f && peak < 0.65f); // 0.3 · (1 + 1) at most: (1 − o3) goes up to 2
- }
- TEST (channel_lfo_vibrato_and_tremolo)
- {
- auto render = [] (const lynx::SynthSettings& s) {
- auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
- inst->prepare (48000.0);
- lynx::ChannelDef c;
- c.synth = s;
- inst->configure (c, 120.0);
- inst->noteOn (69, 1.0f);
- std::vector<float> l (48000), r (48000);
- inst->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
- return l;
- };
- lynx::SynthSettings s;
- s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
- s.cutoff = 20000.0f;
- // tremolo: 100% volume LFO at 4 Hz (0.25 s period) -> the amplitude envelope goes from ~0 to ~2x
- auto tremolo = s;
- tremolo.lfo[lynx::lfoVolume] = { 1.0f, 0.25f, false, 0, 0.0f, 0.0f };
- const auto t = render (tremolo);
- float lowest = 1.0f, highest = 0.0f;
- for (std::size_t w = 4800; w + 480 <= 48000; w += 480) // 10 ms windows (4.4 cycles of 440 Hz)
- {
- float peak = 0.0f;
- for (auto i = w; i < w + 480; ++i)
- peak = std::max (peak, std::abs (t[i]));
- lowest = std::min (lowest, peak);
- highest = std::max (highest, peak);
- }
- CHECK (lowest < 0.08f && highest > 0.5f);
- // synced vibrato: ±1 semitone, 1-beat period (0.5 s at 120 BPM); the average stays at 440 Hz
- auto vibrato = s;
- vibrato.lfo[lynx::lfoPitch] = { 1.0f, 1.0f, true, 0, 0.0f, 0.0f };
- const auto v = render (vibrato);
- int crossings = 0;
- for (std::size_t i = 1; i < 48000; ++i)
- crossings += (v[i - 1] < 0.0f) != (v[i] < 0.0f);
- CHECK (std::abs (crossings - 880) < 12);
- int firstQuarter = 0; // first quarter of the cycle: rising sine -> higher than 440
- for (std::size_t i = 1; i < 6000; ++i)
- firstQuarter += (v[i - 1] < 0.0f) != (v[i] < 0.0f);
- CHECK (firstQuarter > 112);
- }
- TEST (sampler_porta_glides_from_previous_note)
- {
- auto data = std::make_shared<lynx::SampleData>();
- data->sampleRate = 48000.0;
- data->left.resize (96000);
- for (std::size_t i = 0; i < data->left.size(); ++i)
- data->left[i] = 0.5f * (float) std::sin (6.283185307 * 220.0 * (double) i / 48000.0);
- auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
- inst->prepare (48000.0);
- lynx::ChannelDef c;
- c.sampler.cut = true;
- c.sampler.porta = true;
- c.sampler.portaBeats = 1.0f; // 0.5 s at 120 BPM
- inst->configure (c, 120.0);
- std::vector<float> l (24000), r (24000);
- inst->noteOn (60, 1.0f);
- inst->render (l.data(), r.data(), 2400, 1.0f, 1.0f);
- inst->noteOn (72, 1.0f); // one octave up: starts at 220 Hz and rises
- std::fill (l.begin(), l.end(), 0.0f);
- inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
- auto crossings = [&] (std::size_t from, std::size_t to) {
- int n = 0;
- for (auto i = from + 1; i < to; ++i)
- n += (l[i - 1] < 0.0f) != (l[i] < 0.0f);
- return n;
- };
- const auto start = crossings (600, 2400); // 37.5 ms still near 220 Hz: ~17 crossings (440 Hz would give 33)
- const auto end = crossings (21600, 24000); // 50 ms at the end of the glide: near 440 Hz (~44; 220 Hz would give 22)
- CHECK (start < 22);
- CHECK (end > 36);
- }
- TEST (zoned_sampler_picks_zone_by_key_and_velocity)
- {
- auto sine = [] (double hz, float amp) {
- auto d = std::make_shared<lynx::SampleData>();
- d->sampleRate = 48000.0;
- d->left.resize (48000);
- for (std::size_t i = 0; i < d->left.size(); ++i)
- d->left[i] = amp * (float) std::sin (6.283185307 * hz * (double) i / 48000.0);
- return d;
- };
- // lows: 220 Hz sine recorded as A3 (57), up to key 69; highs: 880 Hz as A5 (81)
- // hard layer on the highs: same tuning, louder
- std::vector<lynx::LoadedZone> zones {
- { { "graves.wav", 57, 0, 69, 0, 127, 0.0f, 1.0f }, sine (220.0, 0.5f) },
- { { "agudos.wav", 81, 70, 127, 0, 99, 0.0f, 1.0f }, sine (880.0, 0.25f) },
- { { "agudos forte.wav", 81, 70, 127, 100, 127, 0.0f, 1.0f }, sine (880.0, 0.9f) },
- };
- auto inst = lynx::makeZonedSampler (zones);
- inst->prepare (48000.0);
- inst->configure (lynx::ChannelDef {}, 120.0);
- auto play = [&] (int key, float velocity) {
- inst->allNotesOff();
- std::vector<float> l (24000), r (24000);
- inst->render (l.data(), r.data(), 2000, 1.0f, 1.0f); // let the old voices die out
- std::fill (l.begin(), l.end(), 0.0f);
- inst->noteOn (key, velocity);
- inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
- int crossings = 0;
- float peak = 0.0f;
- for (std::size_t i = 1; i < 24000; ++i)
- {
- crossings += (l[i - 1] < 0.0f) != (l[i] < 0.0f);
- peak = std::max (peak, std::abs (l[i]));
- }
- return std::pair { crossings, peak };
- };
- const auto low = play (69, 0.5f); // A4 from root A3: 440 Hz -> 440 crossings in 0.5 s
- CHECK (std::abs (low.first - 440) < 4);
- const auto soft = play (81, 0.5f); // high zone at the root: 880 Hz
- CHECK (std::abs (soft.first - 880) < 6);
- const auto hard = play (81, 0.95f); // high velocity: hard layer
- CHECK (hard.second > soft.second * 2.0f);
- // overlapping layers round-robin: two identical zones at different levels, notes alternate
- auto rr = lynx::makeZonedSampler ({ { { "a.wav", 60 }, sine (440.0, 0.2f) }, { { "b.wav", 60 }, sine (440.0, 0.8f) } });
- rr->prepare (48000.0);
- rr->configure (lynx::ChannelDef {}, 120.0);
- float peaks[2] {};
- for (int k = 0; k < 2; ++k)
- {
- rr->allNotesOff();
- std::vector<float> l (4800), r (4800);
- rr->render (l.data(), r.data(), 2000, 1.0f, 1.0f);
- std::fill (l.begin(), l.end(), 0.0f);
- rr->noteOn (60, 1.0f);
- rr->render (l.data(), r.data(), 4800, 1.0f, 1.0f);
- for (auto v : l)
- peaks[k] = std::max (peaks[k], std::abs (v));
- }
- CHECK (std::abs (peaks[0] - peaks[1]) > 0.3f);
- }
- TEST (zoned_sampler_loops_while_held)
- {
- // 0.1 s sample with a loop in the middle: holding the note it keeps sounding well past the end of the file;
- // on release, it leaves the loop and ends
- auto data = std::make_shared<lynx::SampleData>();
- data->sampleRate = 48000.0;
- data->left.resize (4800);
- for (std::size_t i = 0; i < data->left.size(); ++i)
- data->left[i] = 0.5f * (float) std::sin (6.283185307 * 480.0 * (double) i / 48000.0); // 100 frames per cycle
- lynx::SampleZone zone { "loop.wav", 60 };
- zone.loopStart = 1000;
- zone.loopEnd = 3000;
- zone.loopMode = 2;
- auto inst = lynx::makeZonedSampler ({ { zone, data } });
- inst->prepare (48000.0);
- lynx::ChannelDef c;
- c.sampler.release = 0.01f;
- inst->configure (c, 120.0);
- inst->noteOn (60, 1.0f);
- std::vector<float> l (48000), r (48000);
- inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f); // 0.5 s held
- float held = 0.0f;
- for (std::size_t i = 20000; i < 24000; ++i)
- held = std::max (held, std::abs (l[i]));
- CHECK (held > 0.3f);
- inst->noteOff (60);
- inst->render (l.data() + 24000, r.data() + 24000, 24000, 1.0f, 1.0f);
- float after = 0.0f;
- for (std::size_t i = 40000; i < 48000; ++i)
- after = std::max (after, std::abs (l[i]));
- CHECK (after == 0.0f);
- }
- TEST (global_lfo_follows_song_position)
- {
- // global volume LFO (1-beat period): a note mid-cycle starts mid-wave; the
- // same note with a per-note LFO starts at phase 0
- auto first = [] (bool global, double songTicks) {
- auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
- inst->prepare (48000.0);
- lynx::ChannelDef c;
- c.synth.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
- c.synth.cutoff = 20000.0f;
- c.synth.lfo[lynx::lfoVolume] = { 1.0f, 1.0f, true, 2, 0.0f, 0.0f, global }; // pulse: +1 in the first half
- inst->configure (c, 120.0);
- inst->setSongPosition (songTicks);
- inst->noteOn (69, 1.0f);
- std::vector<float> l (4800), r (4800);
- inst->render (l.data(), r.data(), 4800, 1.0f, 1.0f);
- float peak = 0.0f;
- for (std::size_t i = 2400; i < 4800; ++i)
- peak = std::max (peak, std::abs (l[i]));
- return peak;
- };
- // mid-cycle (phase 0.6) the pulse is at −1: volume ~0; per note (phase 0) it is at +1: ~2x
- const auto globalLate = first (true, 0.6 * lynx::ppq);
- const auto perNote = first (false, 0.6 * lynx::ppq);
- CHECK (globalLate < 0.05f);
- CHECK (perNote > 0.4f);
- }
- TEST (sampler_channel_filter_darkens_and_bypasses_when_open)
- {
- // white noise in the sampler: with a 300 Hz cutoff the energy drops a lot; open passes unchanged
- auto data = std::make_shared<lynx::SampleData>();
- data->sampleRate = 48000.0;
- data->left.resize (48000);
- std::uint32_t seed = 7;
- for (auto& v : data->left)
- {
- seed = seed * 1664525u + 1013904223u;
- v = ((float) (seed >> 8) / 16777216.0f - 0.5f);
- }
- data->right = data->left;
- auto energy = [&] (float cutoff) {
- auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
- inst->prepare (48000.0);
- lynx::ChannelDef c;
- c.sampler.cutoff = cutoff;
- inst->configure (c, 120.0);
- inst->noteOn (60, 1.0f);
- std::vector<float> l (24000), r (24000);
- inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
- double e = 0.0;
- for (auto v : l)
- e += (double) v * v;
- return e;
- };
- const auto open = energy (20000.0f), dark = energy (300.0f);
- {
- // high-pass at 10 kHz: also removes most of the noise energy, but from the other side
- auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
- inst->prepare (48000.0);
- lynx::ChannelDef hp;
- hp.sampler.cutoff = 10000.0f;
- hp.sampler.filterType = 2;
- inst->configure (hp, 120.0);
- inst->noteOn (60, 1.0f);
- std::vector<float> l (24000), r (24000);
- inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
- double e = 0.0, lowE = 0.0;
- float lp = 0.0f;
- for (auto v : l)
- {
- e += (double) v * v;
- lp += 0.01f * (v - lp); // crude low-pass: almost nothing should remain below
- lowE += (double) lp * lp;
- }
- CHECK (e > 10.0 && e < 0.7 * energy (20000.0f));
- CHECK (lowE < e * 0.01);
- }
- CHECK (open > 100.0);
- CHECK (dark < open * 0.05);
- // +12 st pitch envelope with sustain 1: the sample runs at double speed
- // and ends in half the time (1 s of sample -> 0.5 s)
- auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
- inst->prepare (48000.0);
- lynx::ChannelDef c;
- c.sampler.pitchEnv = { 12.0f, 0.0f, 0.0f, 0.1f, 1.0f, 0.1f };
- inst->configure (c, 120.0);
- inst->noteOn (60, 1.0f);
- std::vector<float> l (48000), r (48000);
- inst->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
- float tail = 0.0f, body = 0.0f;
- for (std::size_t i = 20000; i < 23000; ++i)
- body = std::max (body, std::abs (l[i]));
- for (std::size_t i = 25000; i < 48000; ++i)
- tail = std::max (tail, std::abs (l[i]));
- CHECK (body > 0.1f);
- CHECK (tail == 0.0f);
- }
- TEST (synth_oscillator_pan_and_semitones)
- {
- auto render = [] (lynx::SynthSettings s, std::vector<float>& l, std::vector<float>& r) {
- auto synth = lynx::makeInstrument (lynx::InstrumentKind::synth);
- synth->prepare (48000.0);
- lynx::ChannelDef c;
- c.synth = s;
- synth->configure (c, 120.0);
- synth->noteOn (69, 1.0f);
- l.assign (24000, 0.0f);
- r.assign (24000, 0.0f);
- synth->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
- };
- lynx::SynthSettings s;
- s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f, 7, 1.0f }; // a fifth up, fully right
- s.cutoff = 20000.0f;
- std::vector<float> l, r;
- render (s, l, r);
- float peakL = 0.0f, peakR = 0.0f;
- int crossings = 0;
- for (std::size_t i = 4800; i < 24000; ++i)
- {
- peakL = std::max (peakL, std::abs (l[i]));
- peakR = std::max (peakR, std::abs (r[i]));
- crossings += (r[i - 1] < 0.0f) != (r[i] < 0.0f);
- }
- CHECK (peakL < 1e-4f && peakR > 0.1f);
- // 440 * 2^(7/12) = 659.3 Hz; 0.4 s = 263.7 cycles = ~527 crossings
- CHECK (std::abs (crossings - 527) < 6);
- }
- TEST (fl_wrapper_state_parses_records_and_vst3_state)
- {
- // Fruity Wrapper blob (version 12): VST3 generator type, class ID, name, path and state
- std::vector<std::uint8_t> blob;
- 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))); };
- auto record = [&] (std::vector<std::uint8_t>& b, std::uint32_t id, const std::vector<std::uint8_t>& payload) {
- u32 (b, id);
- u32 (b, (std::uint32_t) payload.size());
- u32 (b, 0);
- b.insert (b.end(), payload.begin(), payload.end());
- };
- auto bytes = [] (const std::string& s) { return std::vector<std::uint8_t> (s.begin(), s.end()); };
- std::vector<std::uint8_t> vst3State;
- u32 (vst3State, 1);
- record (vst3State, 1, std::vector<std::uint8_t> (64, 0));
- record (vst3State, 3, bytes ("COMPONENTE"));
- record (vst3State, 2, bytes ("CONTROLADOR"));
- // record 4: 3 slots → ParamIDs 700, −1 (empty), 0xFFFFFF01
- std::vector<std::uint8_t> slots;
- for (auto v : { 3u, 700u, 0xFFFFFFFFu, 0xFFFFFF01u })
- u32 (slots, v);
- record (vst3State, 4, slots);
- u32 (blob, 12);
- record (blob, 0x01, std::vector<std::uint8_t> (20, 0xFF));
- record (blob, 0x32, { 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });
- std::vector<std::uint8_t> cid;
- for (int i = 0; i < 16; ++i)
- cid.push_back ((std::uint8_t) (0xA0 + i));
- record (blob, 0x34, cid);
- record (blob, 0x36, bytes ("Surge XT"));
- record (blob, 0x37, bytes ("C:/Program Files/Common Files/VST3/Surge XT.vst3"));
- record (blob, 0x35, vst3State);
- lynx::ForeignPlugin p;
- CHECK (lynx::parseFlWrapper (blob, p));
- CHECK (p.format == lynx::ForeignPlugin::Format::vst3);
- CHECK (p.hasClassId && p.classId[0] == 0xA0 && p.classId[15] == 0xAF);
- CHECK (p.name == "Surge XT" && p.path.find ("Surge XT.vst3") != std::string::npos);
- std::vector<std::uint8_t> component, controller;
- CHECK (lynx::splitFlVst3State (p.state, component, controller));
- CHECK (component == bytes ("COMPONENTE") && controller == bytes ("CONTROLADOR"));
- const auto saved = lynx::flVst3ParamSlots (p.state);
- CHECK (saved == (std::vector<std::int32_t> { 700, -1, (std::int32_t) 0xFFFFFF01u }));
- // FL's slot assignment: saved slots keep their ParamID wherever it now sits; the empty slot stays empty;
- // a vanished ParamID (0xFFFFFF01) leaves its slot dead; parameters without a slot get slots 3, 4… in list order
- const auto positions = lynx::flVst3SlotPositions (saved, { 5u, 700u, 9u });
- CHECK (positions == (std::vector<int> { 1, -1, -1, 0, 2 }));
- CHECK (lynx::flVst3SlotPositions ({}, { 5u, 6u }) == (std::vector<int> { 0, 1 })); // new instance: slot = position
- // version out of range or no type: not a usable wrapper
- blob[0] = 13;
- CHECK (! lynx::parseFlWrapper (blob, p));
- CHECK (! lynx::parseFlWrapper ({ 12, 0, 0, 0 }, p));
- }
- TEST (schedule_index_matches_full_scan)
- {
- // random song (long notes, swing, clips with offset); indexed scheduling must yield
- // exactly the same events as the full scan, including in the block that wraps around
- lynx::Song s = lynx::Song::makeDefault();
- s.swing = 0.6f;
- std::uint32_t seed = 12345;
- auto rnd = [&] (int n) { seed = seed * 1664525u + 1013904223u; return (int) ((seed >> 8) % (std::uint32_t) n); };
- s.patterns.resize (3);
- for (auto& p : s.patterns)
- {
- p.notes.clear();
- for (int i = 0; i < 60; ++i)
- {
- lynx::Note n;
- n.start = rnd (16 * lynx::ppq);
- n.length = rnd (5) == 0 ? rnd (12 * lynx::ppq) + 1 : rnd (lynx::ppq) + 1;
- n.channel = rnd (4);
- n.key = 40 + rnd (40);
- n.slide = rnd (20) == 0;
- p.notes.push_back (n);
- }
- p.sortNotes();
- }
- s.playlist.clear();
- for (int i = 0; i < 12; ++i)
- {
- lynx::Clip c;
- c.pattern = rnd (3);
- c.track = rnd (4);
- c.start = rnd (40) * lynx::ppq;
- c.length = (rnd (12) + 1) * lynx::ppq;
- c.offset = rnd (4) * lynx::ppq;
- s.playlist.push_back (c);
- }
- s.mode = lynx::PlayMode::song;
- const auto index = lynx::makeScheduleIndex (s);
- const auto tps = lynx::ticksPerSample (140.0, 48000.0);
- const auto loop = (double) s.songLength();
- int mismatches = 0, events = 0;
- for (int block = 0; block < 400; ++block)
- {
- const auto start = block == 0 ? loop - 100.0 * tps : (double) rnd ((int) loop * 2);
- const auto samples = 64 + rnd (24000); // up to ~1 beat per block
- lynx::EventBuffer a, b;
- lynx::scheduleSong (s, start, samples, tps, a, false, nullptr);
- lynx::scheduleSong (s, start, samples, tps, b, false, &index);
- events += a.size();
- if (a.size() != b.size())
- {
- ++mismatches;
- continue;
- }
- for (int i = 0; i < a.size(); ++i)
- {
- const auto& x = *(a.begin() + i);
- const auto& y = *(b.begin() + i);
- mismatches += x.sampleOffset != y.sampleOffset || x.type != y.type || x.key != y.key || x.channel != y.channel;
- }
- }
- CHECK (events > 500);
- CHECK (mismatches == 0);
- }
- TEST (mixer_track_eq_defaults_and_boost)
- {
- // FL defaults: 90 Hz, 1.5 kHz and 8 kHz; peak Q 0.632; shelf slope 0.607
- lynx::TrackEq eq;
- CHECK (std::abs (lynx::TrackEq::peakQ (eq.width[1]) - 0.632) < 0.01);
- CHECK (std::abs (lynx::TrackEq::shelfSlope (eq.width[0]) - 0.607) < 0.01);
- CHECK (! eq.active());
- // +12 dB peak at 1.5 kHz on a track: a 1.5 kHz sine rises ~4x
- lynx::Song song = lynx::Song::makeDefault();
- song.mixer[1].eq.gainDb[1] = 12.0f;
- for (auto& c : song.channels)
- c.mixerTrack = 1;
- song.channels[4].instrument = lynx::InstrumentKind::synth;
- song.channels[4].synth = {};
- song.channels[4].synth.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
- song.channels[4].synth.cutoff = 20000.0f;
- auto render = [] (const lynx::Song& s) {
- auto state = lynx::RackState::build (s, nullptr, 48000.0);
- lynx::RackRenderer renderer;
- renderer.prepare (48000.0);
- lynx::LiveEventQueue live;
- lynx::NoteEvent on; // 1.5 kHz ~ key 90.6: use 91 (1568 Hz), still within the wide peak
- on.channel = 4;
- on.key = 91;
- on.velocity = 1.0f;
- live.push (on);
- std::vector<float> l (24000), r (24000);
- for (int pos = 0; pos < 24000; pos += 480)
- renderer.process (state.get(), false, 0.0, 480, l.data() + pos, r.data() + pos, pos == 0 ? &live : nullptr);
- float peak = 0.0f;
- for (std::size_t i = 12000; i < 24000; ++i)
- peak = std::max (peak, std::abs (l[i]));
- return peak;
- };
- const auto boosted = render (song);
- song.mixer[1].eq.gainDb[1] = 0.0f;
- const auto flat = render (song);
- CHECK (flat > 0.01f);
- CHECK (boosted / flat > 3.0f && boosted / flat < 4.2f);
- }
- TEST (bars_follow_time_signature_markers)
- {
- lynx::Song song;
- song.beatsPerBar = 4;
- const auto bar4 = (std::int64_t) lynx::ppq * 4;
- auto bars = song.barsBetween (0, bar4 * 3);
- CHECK (bars.size() == 3 && bars[2].tick == bar4 * 2 && bars[2].number == 3);
- // 7/4 from bar 3, and 3/4 in the middle of a 7 bar (cuts and restarts)
- song.addMarker ({ bar4 * 2, "Verso", 7, 4 });
- song.addMarker ({ bar4, "Nome só", 0, 4 });
- const auto seven = (std::int64_t) lynx::ppq * 7;
- song.addMarker ({ bar4 * 2 + seven + lynx::ppq * 2, "", 3, 4 });
- bars = song.barsBetween (0, bar4 * 2 + seven * 2);
- CHECK (bars.size() >= 5);
- CHECK (bars[1].length == bar4 && bars[2].tick == bar4 * 2 && bars[2].length == seven);
- CHECK (bars[3].length == lynx::ppq * 2); // cut short by the change
- CHECK (bars[4].length == lynx::ppq * 3 && bars[4].number == 5);
- CHECK (song.markers()[0].name == "Nome só"); // sorted by tick
- // from far away: starts at the right bar without losing the numbering
- const auto far = song.barsBetween (lynx::ppq * 3000, lynx::ppq * 3001);
- CHECK (far.size() == 1 && far[0].length == lynx::ppq * 3);
- CHECK (song.barAt (lynx::ppq * 3000).number == far[0].number);
- CHECK ((lynx::ppq * 3000 - far[0].tick) < far[0].length);
- }
- TEST (split_drum_channel_moves_notes_to_pads)
- {
- lynx::Song song = lynx::Song::makeDefault();
- const auto channels = (int) song.channels.size();
- song.patterns[0].notes.clear();
- song.patterns[0].notes.push_back ({ 0, 240, 1, 36, 0.9f }); // kit kick (channel 1)
- song.patterns[0].notes.push_back ({ 240, 240, 1, 38, 0.8f }); // snare
- song.patterns[0].notes.push_back ({ 480, 240, 1, 50, 0.8f }); // key without a pad: dropped
- song.patterns[0].notes.push_back ({ 480, 240, 3, 60, 0.8f }); // other channel: shifts by 1 position
- lynx::Automation a;
- a.target = { lynx::AutomationTargetKind::channelVolume, 3, 0, 0 };
- song.automations.push_back (a);
- std::vector<lynx::DrumPad> pads (2);
- pads[0].name = "Kick";
- pads[0].key = 36;
- pads[0].layers = { { "kick.wav" } };
- pads[1].name = "Snare";
- pads[1].key = 38;
- pads[1].layers = { { "snare soft.wav", 60, 0, 127, 0, 90 }, { "snare hard.wav", 60, 0, 127, 91, 127 } };
- pads[1].cutGroup = 2;
- pads[1].cutBy = 2;
- lynx::splitDrumChannel (song, 1, pads);
- CHECK ((int) song.channels.size() == channels + 1);
- CHECK (song.channels[1].samplePath == "kick.wav" && song.channels[1].instrument == lynx::InstrumentKind::sampler);
- CHECK (song.channels[2].zones.size() == 2 && song.channels[2].cutGroup == song.channels[2].cutBy && song.channels[2].cutGroup != 0);
- const auto& notes = song.patterns[0].notes;
- CHECK (notes.size() == 3);
- CHECK (notes[0].channel == 1 && notes[0].key == 60);
- CHECK (notes[1].channel == 2 && notes[1].key == 60);
- CHECK (notes[2].channel == 4); // the old channel 3
- CHECK (song.automations[0].target.index == 4);
- }
- TEST (flp_import_brings_playlist)
- {
- flp::Project p;
- p.header.ppq = 96;
- p.channels = { { 0, flp::ChannelType::sampler, "Kick", "", "", 1 } };
- flp::Pattern empty;
- empty.index = 1;
- flp::Pattern full;
- full.index = 2;
- flp::Note n;
- n.channel = 0;
- full.notes.push_back (n);
- p.patterns = { empty, full };
- flp::Arrangement a;
- flp::PlaylistItem item;
- item.kind = flp::PlaylistItem::Kind::pattern;
- item.index = 2;
- item.position = 384;
- item.length = 192;
- item.track = 3;
- item.patternStartTick = 96;
- a.items.push_back (item);
- item.index = 1; // empty pattern: discarded
- a.items.push_back (item);
- item.kind = flp::PlaylistItem::Kind::channelClip; // audio: not supported yet
- a.items.push_back (item);
- p.arrangements.push_back (a);
- const auto s = lynx::importFlp (p);
- CHECK (s.mode == lynx::PlayMode::song);
- CHECK (s.playlist.size() == 1);
- CHECK (s.playlist[0].pattern == 0 && s.playlist[0].track == 3);
- CHECK (s.playlist[0].start == 4 * lynx::ppq && s.playlist[0].length == 2 * lynx::ppq && s.playlist[0].offset == lynx::ppq);
- }
- TEST (rack_releases_notes_on_position_jump)
- {
- lynx::Song s;
- lynx::ChannelDef c;
- c.instrument = lynx::InstrumentKind::synth;
- s.channels.push_back (c);
- auto& p = s.patterns.emplace_back();
- lynx::Note n;
- n.start = 0;
- n.length = 4 * lynx::ppq; // long note
- p.notes.push_back (n);
- s.bpm = 120.0;
- auto state = lynx::RackState::build (s, nullptr, 48000.0);
- lynx::RackRenderer r;
- r.prepare (48000.0);
- std::vector<float> l (512), rr (512);
- const auto tps = lynx::ticksPerSample (120.0, 48000.0);
- double tick = 0.0;
- for (int i = 0; i < 20; ++i, tick += 512 * tps)
- r.process (state.get(), true, tick, 512, l.data(), rr.data());
- // jump to tick 2000 (mid-note, no new "on"): the synth must go silent
- for (int i = 0; i < 40; ++i)
- r.process (state.get(), true, 2000.0 + i * 512 * tps, 512, l.data(), rr.data());
- float tail = 0.0f;
- for (auto v : l)
- tail = std::max (tail, std::abs (v));
- CHECK (tail < 0.001f);
- }
- namespace
- {
- // Two samplers with a constant 0.5 signal on different inserts; returns the output peak.
- float juce_db (float gain)
- {
- return 20.0f * std::log10 (gain);
- }
- float renderMixerCase (const std::function<void (lynx::Song&)>& tweak, lynx::MeterBank* meters = nullptr)
- {
- lynx::Song s;
- s.mixer = lynx::Song::defaultMixer (4);
- for (int i = 0; i < 2; ++i)
- {
- lynx::ChannelDef c;
- c.instrument = lynx::InstrumentKind::sampler;
- c.samplePath = "dc";
- c.volume = 1.0f;
- c.mixerTrack = i + 1;
- s.channels.push_back (c);
- }
- s.patterns.emplace_back();
- tweak (s);
- auto provider = [] (const std::string&) {
- auto d = std::make_shared<lynx::SampleData>();
- d->left.assign (48000, 0.5f);
- return std::shared_ptr<const lynx::SampleData> (d);
- };
- auto state = lynx::RackState::build (s, nullptr, 48000.0, provider);
- lynx::RackRenderer r;
- r.prepare (48000.0, 256);
- lynx::LiveEventQueue live;
- for (int ch = 0; ch < 2; ++ch)
- {
- lynx::NoteEvent on;
- on.channel = ch;
- on.velocity = 1.0f;
- live.push (on);
- }
- std::vector<float> l (1000), rr (1000); // larger than the mixer block: tests the chunking
- r.process (state.get(), false, 0.0, 1000, l.data(), rr.data(), &live, meters);
- return std::max (std::abs (l[999]), std::abs (rr[999]));
- }
- } // namespace
- TEST (mixer_routes_mutes_solos_and_meters)
- {
- // two 0.5 channels summed on the Master at 0 dB
- CHECK (std::abs (renderMixerCase ([] (lynx::Song&) {}) - 1.0f) < 1e-4f);
- // insert 1 muted: only channel 2 remains
- CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.mixer[1].muted = true; }) - 0.5f) < 1e-4f);
- // solo on insert 2: same
- CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.mixer[2].solo = true; }) - 0.5f) < 1e-4f);
- // FL fader curve: 0.8 = 0 dB, max ≈ +5.6 dB, half travel ≈ -12.7 dB
- CHECK (std::abs (lynx::faderGain (0.8f) - 1.0f) < 1e-5f);
- CHECK (std::abs (juce_db (lynx::faderGain (1.0f)) - 5.59f) < 0.05f);
- CHECK (lynx::faderGain (0.0f) == 0.0f);
- // Master fader at max
- CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.mixer[0].volume = 1.0f; }) - lynx::faderGain (1.0f)) < 1e-4f);
- // both channels on the same insert, insert fader at 0.4
- CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) {
- s.channels[1].mixerTrack = 1;
- s.mixer[1].volume = 0.4f;
- }) - lynx::faderGain (0.4f)) < 1e-4f);
- // a route outside the mixer falls to the last existing insert, without crashing
- CHECK (renderMixerCase ([] (lynx::Song& s) { s.channels[0].mixerTrack = 100; }) > 0.9f);
- // no mixer: everything straight to the output at 0 dB
- CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.mixer.clear(); }) - 1.0f) < 1e-4f);
- lynx::MeterBank meters;
- renderMixerCase ([] (lynx::Song&) {}, &meters);
- CHECK (std::abs (meters.takePeak (1, 0) - 0.5f) < 1e-4f);
- CHECK (std::abs (meters.takePeak (0, 1) - 1.0f) < 1e-4f);
- CHECK (meters.takePeak (1, 0) == 0.0f); // reading resets
- CHECK (meters.takePeak (3, 0) == 0.0f); // insert with no channel
- }
- TEST (next_free_insert_and_flp_mixer_import)
- {
- auto s = lynx::Song::makeDefault();
- CHECK (s.mixer.size() == 17 && s.mixer[0].name == "Master");
- CHECK (s.nextFreeInsert() == 6); // the 5 default channels take 1..5
- flp::Project p;
- p.channels = { { 0, flp::ChannelType::sampler, "Kick", "", "", 30 } };
- p.mixer = { { 0, "" }, { 30, "Drums" } };
- const auto song = lynx::importFlp (p);
- CHECK (song.channels[0].mixerTrack == 30);
- CHECK (song.mixer.size() == 31);
- CHECK (song.mixer[30].name == "Drums" && song.mixer[0].name == "Master");
- }
- namespace
- {
- float runEffectOnSine (lynx::EffectKind kind, const std::function<void (lynx::EffectSlot&)>& tweak, double hz, float amp,
- int frames = 48000)
- {
- auto fx = lynx::makeEffect (kind);
- fx->prepare (48000.0);
- auto slot = lynx::makeEffectSlot (kind);
- tweak (slot);
- std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
- for (int i = 0; i < frames; ++i)
- l[(std::size_t) i] = r[(std::size_t) i] = amp * (float) std::sin (6.283185307 * hz * i / 48000.0);
- for (int i = 0; i < frames; i += 480)
- fx->process (l.data() + i, r.data() + i, std::min (480, frames - i), slot.params, 120.0);
- float peak = 0.0f;
- for (int i = frames / 2; i < frames; ++i) // second half: already settled
- {
- CHECK (std::isfinite (l[(std::size_t) i]));
- peak = std::max (peak, std::abs (l[(std::size_t) i]));
- }
- return peak;
- }
- } // namespace
- TEST (parametric_eq_bands_and_response)
- {
- // default: transparent
- auto slot = lynx::makeEffectSlot (lynx::EffectKind::parametricEq);
- CHECK (std::abs (lynx::peq::responseDb (slot.params, 48000.0, 1000.0)) < 0.01);
- CHECK (std::abs (runEffectOnSine (lynx::EffectKind::parametricEq, [] (auto&) {}, 1000.0, 0.5f) - 0.5f) < 0.005f);
- // +12 dB peak at 1 kHz on band 4: the response and the signal agree
- auto boost = [] (lynx::EffectSlot& s) {
- s.params[(std::size_t) lynx::peq::param (3, 1)] = lynx::peq::hzValue (1000.0);
- s.params[(std::size_t) lynx::peq::param (3, 2)] = lynx::peq::gainValue (12.0);
- };
- boost (slot);
- CHECK (std::abs (lynx::peq::responseDb (slot.params, 48000.0, 1000.0) - 12.0) < 0.2);
- CHECK (std::abs (lynx::peq::responseDb (slot.params, 48000.0, 100.0)) < 1.0);
- const auto peak = runEffectOnSine (lynx::EffectKind::parametricEq, boost, 1000.0, 0.1f);
- CHECK (std::abs (peak - 0.1f * std::pow (10.0f, 12.0f / 20.0f)) < 0.01f);
- // low cut at 48 dB/oct at 200 Hz: 50 Hz (2 octaves below) drops ~96 dB; 2 kHz passes
- auto cut = lynx::makeEffectSlot (lynx::EffectKind::parametricEq);
- cut.params[(std::size_t) lynx::peq::param (0, 0)] = lynx::peq::typeValue (lynx::peq::BandType::lowCut);
- cut.params[(std::size_t) lynx::peq::param (0, 1)] = lynx::peq::hzValue (200.0);
- cut.params[(std::size_t) lynx::peq::param (0, 3)] = lynx::peq::qValue (0.70710678); // Butterworth
- cut.params[(std::size_t) (lynx::peq::slopeParam + 0)] = 1.0f;
- CHECK (lynx::peq::responseDb (cut.params, 48000.0, 50.0) < -80.0);
- CHECK (std::abs (lynx::peq::responseDb (cut.params, 48000.0, 200.0) + 3.0) < 0.5); // -3 dB at the cutoff (Butterworth)
- CHECK (std::abs (lynx::peq::responseDb (cut.params, 48000.0, 2000.0)) < 0.1);
- // round-trip conversions
- CHECK (std::abs (lynx::peq::hz (lynx::peq::hzValue (440.0)) - 440.0) < 0.5);
- CHECK (std::abs (lynx::peq::q (lynx::peq::qValue (2.5)) - 2.5) < 0.01);
- CHECK (lynx::peq::type (lynx::peq::typeValue (lynx::peq::BandType::notch)) == lynx::peq::BandType::notch);
- }
- TEST (fl_parametric_eq2_state_maps_to_bands)
- {
- // v8 blob: band 1 low cut at 100 Hz "Steep 8", band 4 peak +6 dB at 1 kHz, output −3 dB
- std::vector<std::uint8_t> blob (354, 0);
- auto put = [&] (std::size_t off, std::int32_t v) { std::memcpy (blob.data() + off, &v, 4); };
- put (0, 8);
- auto param = [&] (int i, std::int32_t v) { put (4 + 4 * (std::size_t) i, v); };
- auto hzRaw = [] (double hz) { return (std::int32_t) std::lround (std::log (hz / 20.0) / std::log (1000.0) * 65536.0); };
- for (int b = 0; b < 7; ++b)
- param (21 + b, 0); // all off
- param (21 + 0, 3); // high pass (= low cut)
- param (7 + 0, hzRaw (100.0));
- param (14 + 0, 40124); // default width -> Q ~0.68
- param (28 + 0, -3); // Steep 8 -> 4 biquads
- param (21 + 3, 6); // peaking
- param (7 + 3, hzRaw (1000.0));
- param (3, 600); // +6 dB
- param (14 + 3, 25412);
- param (35, -300); // main level −3 dB
- put (0x13E, 0x7F);
- const auto slot = lynx::effectFromFl ("Fruity Parametric EQ 2", blob);
- CHECK (slot.has_value() && slot->kind == lynx::EffectKind::parametricEq);
- const auto& p = slot->params;
- CHECK (lynx::peq::type (p[(std::size_t) lynx::peq::param (0, 0)]) == lynx::peq::BandType::lowCut);
- CHECK (lynx::peq::slopeStages (p[(std::size_t) lynx::peq::slopeParam]) == 4);
- CHECK (lynx::peq::type (p[(std::size_t) lynx::peq::param (3, 0)]) == lynx::peq::BandType::peak);
- CHECK (lynx::peq::type (p[(std::size_t) lynx::peq::param (1, 0)]) == lynx::peq::BandType::off);
- CHECK (std::abs (lynx::peq::responseDb (p, 48000.0, 1000.0) - 3.0) < 0.3); // +6 from the peak −3 from the output
- CHECK (lynx::peq::responseDb (p, 48000.0, 30.0) < -40.0); // 48 dB/oct below 100 Hz
- CHECK (std::abs (lynx::peq::responseDb (p, 48000.0, 8000.0) + 3.0) < 0.3);
- // a band muted by the mask doesn't sound
- put (0x13E, 0x7F & ~(1 << 3));
- const auto muted = lynx::effectFromFl ("Fruity Parametric EQ 2", blob);
- CHECK (lynx::peq::type (muted->params[(std::size_t) lynx::peq::param (3, 0)]) == lynx::peq::BandType::off);
- }
- TEST (reverb_low_cut_and_predelay)
- {
- // impulse: with 100 ms predelay the wet only starts later; with a 3 kHz low cut a
- // 100 Hz sine barely enters the reverb
- auto run = [] (const std::function<void (lynx::EffectSlot&)>& tweak, double hz, bool impulse) {
- auto fx = lynx::makeEffect (lynx::EffectKind::reverb);
- fx->prepare (48000.0);
- auto slot = lynx::makeEffectSlot (lynx::EffectKind::reverb);
- slot.params[4] = 0.0f; // wet only
- slot.params[3] = 1.0f;
- tweak (slot);
- std::vector<float> l (48000), r (48000);
- for (int i = 0; i < 48000; ++i)
- 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);
- for (int i = 0; i < 48000; i += 480)
- fx->process (l.data() + i, r.data() + i, 480, slot.params, 120.0);
- return l;
- };
- const auto delayed = run ([] (auto& s) { s.params[7] = 0.1f; }, 0.0, true);
- float early = 0.0f, later = 0.0f;
- for (std::size_t i = 0; i < 4800; ++i)
- early = std::max (early, std::abs (delayed[i]));
- for (std::size_t i = 4800; i < 24000; ++i)
- later = std::max (later, std::abs (delayed[i]));
- CHECK (early == 0.0f && later > 0.0f);
- 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; };
- const auto open = energy (run ([] (auto&) {}, 100.0, false));
- const auto cut = energy (run ([] (auto& s) { s.params[5] = 1.0f; }, 100.0, false));
- CHECK (cut < open * 0.01);
- }
- TEST (utility_effect_and_fl_stereo_tools)
- {
- // transparent by default; balance fully right silences the left
- CHECK (std::abs (runEffectOnSine (lynx::EffectKind::utility, [] (auto&) {}, 440.0, 0.5f) - 0.5f) < 0.001f);
- CHECK (runEffectOnSine (lynx::EffectKind::utility, [] (auto& s) { s.params[1] = 1.0f; }, 440.0, 0.5f) < 0.001f);
- // Stereo Shaper in "mono": each output = 0.5 L + 0.5 R
- std::vector<std::uint8_t> blob (36, 0);
- auto put = [&] (std::size_t off, std::int32_t v) { std::memcpy (blob.data() + off, &v, 4); };
- put (4, 6400); put (8, 6400); put (12, 6400); put (16, 6400);
- const auto slot = lynx::effectFromFl ("Fruity Stereo Shaper", blob);
- CHECK (slot && slot->kind == lynx::EffectKind::utility);
- auto fx = lynx::makeEffect (lynx::EffectKind::utility);
- float l = 1.0f, r = 0.0f;
- fx->process (&l, &r, 1, slot->params, 120.0);
- CHECK (std::abs (l - 0.5f) < 1e-4f && std::abs (r - 0.5f) < 1e-4f);
- // Balance: volume 256 = 0 dB, pan −128 = fully left
- std::vector<std::uint8_t> bal (8, 0);
- std::int32_t pan = -128, vol = 256;
- std::memcpy (bal.data(), &pan, 4);
- std::memcpy (bal.data() + 4, &vol, 4);
- const auto b = lynx::effectFromFl ("Fruity Balance", bal);
- l = 0.5f; r = 0.5f;
- fx->process (&l, &r, 1, b->params, 120.0);
- CHECK (std::abs (l - 0.5f) < 1e-3f && std::abs (r) < 1e-4f);
- }
- TEST (effects_eq_compressor_behave)
- {
- // EQ at default is transparent
- CHECK (std::abs (runEffectOnSine (lynx::EffectKind::eq, [] (auto&) {}, 1000.0, 0.5f) - 0.5f) < 0.01f);
- // +18 dB on the lows: 60 Hz rises a lot, 5 kHz barely
- CHECK (runEffectOnSine (lynx::EffectKind::eq, [] (auto& s) { s.params[0] = 1.0f; }, 60.0, 0.05f) > 0.3f);
- CHECK (runEffectOnSine (lynx::EffectKind::eq, [] (auto& s) { s.params[0] = 1.0f; }, 5000.0, 0.05f) < 0.06f);
- // compressor: threshold -20 dB, ratio 20:1 flattens a 0 dBFS signal to near the threshold
- const auto squashed = runEffectOnSine (lynx::EffectKind::compressor, [] (auto& s) {
- s.params[0] = 40.0f / 60.0f;
- s.params[1] = 1.0f;
- }, 200.0, 1.0f);
- CHECK (squashed < 0.2f && squashed > 0.05f);
- // a signal below the threshold passes intact
- CHECK (std::abs (runEffectOnSine (lynx::EffectKind::compressor, [] (auto& s) { s.params[0] = 1.0f; }, 200.0, 0.3f) - 0.3f) < 0.01f);
- }
- TEST (effects_delay_echoes_on_tempo_and_reverb_has_tail)
- {
- auto delay = lynx::makeEffect (lynx::EffectKind::delay);
- delay->prepare (48000.0);
- auto slot = lynx::makeEffectSlot (lynx::EffectKind::delay);
- slot.params[0] = 3.5f / 8.0f; // index 3 = 1/4 = 0.5 s at 120 BPM
- slot.params[1] = 0.0f; // no feedback: a single echo
- slot.params[2] = 1.0f;
- slot.params[4] = 0.0f; // no damping
- std::vector<float> l (48000, 0.0f), r (48000, 0.0f);
- l[0] = r[0] = 1.0f;
- delay->process (l.data(), r.data(), 48000, slot.params, 120.0);
- CHECK (std::abs (l[24000] - 1.0f) < 1e-4f);
- CHECK (std::abs (l[12000]) < 1e-6f);
- auto reverb = lynx::makeEffect (lynx::EffectKind::reverb);
- reverb->prepare (48000.0);
- auto rs = lynx::makeEffectSlot (lynx::EffectKind::reverb);
- rs.params[3] = 1.0f;
- std::fill (l.begin(), l.end(), 0.0f);
- std::fill (r.begin(), r.end(), 0.0f);
- l[0] = r[0] = 1.0f;
- reverb->process (l.data(), r.data(), 48000, rs.params, 120.0);
- float tail = 0.0f;
- for (int i = 4800; i < 9600; ++i) // between 100 and 200 ms after the impulse
- tail = std::max (tail, std::abs (l[(std::size_t) i]));
- CHECK (tail > 1e-4f && tail < 1.0f);
- CHECK (lynx::formatEffectParam (lynx::EffectKind::delay, 0, slot.params[0], 120.0) == "1/4 (500 ms)");
- }
- TEST (rack_runs_insert_effects_and_reuses_them)
- {
- // an insert with EQ at -inf doesn't exist; we use the EQ output at -18 dB to see the effect act
- const auto quiet = renderMixerCase ([] (lynx::Song& s) {
- auto eq = lynx::makeEffectSlot (lynx::EffectKind::eq);
- eq.params[4] = 0.0f; // output -18 dB
- s.mixer[1].effects.push_back (eq);
- });
- CHECK (quiet > 0.55f && quiet < 0.6f); // 0.5 from channel 2 + 0.5 * 0.126 from channel 1
- const auto bypassed = renderMixerCase ([] (lynx::Song& s) {
- auto eq = lynx::makeEffectSlot (lynx::EffectKind::eq);
- eq.params[4] = 0.0f;
- eq.enabled = false;
- s.mixer[1].effects.push_back (eq);
- });
- CHECK (std::abs (bypassed - 1.0f) < 1e-4f);
- auto s = lynx::Song::makeDefault();
- s.mixer[2].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::reverb));
- auto a = lynx::RackState::build (s, nullptr, 48000.0);
- s.mixer[2].effects[0].params[0] = 0.9f; // turning a knob doesn't recreate the effect
- auto b = lynx::RackState::build (s, a.get(), 48000.0);
- CHECK (b->effects[2][0] == a->effects[2][0]);
- s.mixer[2].effects[0] = lynx::makeEffectSlot (lynx::EffectKind::delay);
- auto c = lynx::RackState::build (s, b.get(), 48000.0);
- CHECK (c->effects[2][0] != b->effects[2][0]);
- }
- TEST (audio_clips_trigger_cut_and_resume)
- {
- lynx::Song s;
- s.bpm = 120.0; // 1 beat = 0.5 s = 24000 samples at 48 kHz
- lynx::ChannelDef c;
- c.instrument = lynx::InstrumentKind::audioClip;
- c.samplePath = "ramp";
- c.volume = 1.0f;
- s.channels.push_back (c);
- s.patterns.emplace_back();
- lynx::Clip clip;
- clip.kind = lynx::ClipKind::audio;
- clip.channel = 0;
- clip.start = lynx::ppq; // starts at beat 2
- clip.length = 2 * lynx::ppq; // lasts 2 beats
- clip.offset = lynx::ppq; // skips the first beat of the audio
- s.playlist.push_back (clip);
- s.mode = lynx::PlayMode::song;
- const auto tps = lynx::ticksPerSample (120.0, 48000.0);
- lynx::EventBuffer ev;
- lynx::scheduleSong (s, 0.0, 4 * 24000, tps, ev);
- CHECK (ev.size() == 2);
- CHECK (ev[0].type == lynx::NoteEvent::Type::on && ev[0].sampleOffset == 24000 && std::abs (ev[0].startSeconds - 0.5) < 1e-9);
- CHECK (ev[1].type == lynx::NoteEvent::Type::off && ev[1].sampleOffset == 3 * 24000);
- // seek to the middle of the clip (beat 2.5): with resume, starts at the block's first sample
- ev.clear();
- lynx::scheduleSong (s, 1.5 * lynx::ppq, 256, tps, ev, true);
- CHECK (ev.size() == 1 && ev[0].sampleOffset == 0 && std::abs (ev[0].startSeconds - 0.75) < 1e-9);
- ev.clear();
- lynx::scheduleSong (s, 1.5 * lynx::ppq, 256, tps, ev, false);
- CHECK (ev.size() == 0);
- // sample whose value = position in seconds: lets us check where playback started
- auto provider = [] (const std::string&) {
- auto d = std::make_shared<lynx::SampleData>();
- d->sampleRate = 48000.0;
- for (int i = 0; i < 48000 * 3; ++i)
- d->left.push_back ((float) i / 48000.0f);
- return std::shared_ptr<const lynx::SampleData> (d);
- };
- auto state = lynx::RackState::build (s, nullptr, 48000.0, provider);
- lynx::RackRenderer r;
- r.prepare (48000.0, 512);
- std::vector<float> l (256), rr (256);
- // first block already playing mid-clip
- r.process (state.get(), true, 1.5 * lynx::ppq, 256, l.data(), rr.data());
- CHECK (std::abs (l[0] - 0.75f) < 0.001f);
- // the clip ends at beat 3: after the short fade, silence (the sample would still have audio)
- double tick = 3.0 * lynx::ppq - 10 * tps;
- r.process (state.get(), false, tick, 256, l.data(), rr.data()); // stops (fade)
- for (int i = 0; i < 4; ++i)
- r.process (state.get(), false, tick, 256, l.data(), rr.data());
- CHECK (std::abs (l[255]) < 1e-6f);
- }
- TEST (remove_channel_fixes_notes_and_audio_clips)
- {
- lynx::Song s;
- s.channels.resize (3);
- s.patterns.resize (1);
- s.patterns[0].notes = { { 0, 240, 0, 60, 1.0f }, { 0, 240, 1, 60, 1.0f }, { 0, 240, 2, 60, 1.0f } };
- lynx::Clip a;
- a.kind = lynx::ClipKind::audio;
- a.channel = 1;
- a.length = 100;
- lynx::Clip b = a;
- b.channel = 2;
- s.playlist = { a, b };
- s.removeChannel (1);
- CHECK (s.channels.size() == 2);
- CHECK (s.patterns[0].notes.size() == 2 && s.patterns[0].notes[1].channel == 1);
- CHECK (s.playlist.size() == 1 && s.playlist[0].channel == 1);
- }
- TEST (flp_import_audio_clips_with_offsets)
- {
- flp::Project p;
- p.header.ppq = 96;
- p.tempo = 120.0;
- p.channels = { { 0, flp::ChannelType::audioClip, "Vocal", "", "%FLStudioData%\\v.wav", 2 },
- { 1, flp::ChannelType::audioClip, "Loop", "", "%FLStudioData%\\l.wav", 3 } };
- flp::Arrangement arr;
- flp::PlaylistItem beats; // offset in beats: end - start = duration in beats
- beats.kind = flp::PlaylistItem::Kind::channelClip;
- beats.index = 0;
- beats.position = 192;
- beats.length = 384; // 4 beats
- beats.startOffset = 2.0f;
- beats.endOffset = 6.0f;
- flp::PlaylistItem ms = beats; // offset in ms: 4 beats at 120 BPM = 2000 ms
- ms.index = 1;
- ms.startOffset = 500.0f;
- ms.endOffset = 2500.0f;
- arr.items = { beats, ms };
- p.arrangements.push_back (arr);
- lynx::FlpImportOptions options;
- options.roots = { "C:\\FL", "C:\\FL\\Data", "" };
- options.fileExists = [] (const std::string&) { return true; };
- const auto s = lynx::importFlp (p, options);
- CHECK (s.channels.size() == 2 && s.channels[0].instrument == lynx::InstrumentKind::audioClip);
- CHECK (s.channels[0].samplePath == "C:\\FL\\Data\\v.wav");
- CHECK (s.playlist.size() == 2);
- CHECK (s.playlist[0].kind == lynx::ClipKind::audio && s.playlist[0].start == 2 * lynx::ppq);
- CHECK (s.playlist[0].offset == 2 * lynx::ppq);
- CHECK (s.playlist[1].offset == lynx::ppq); // 500 ms at 120 BPM = 1 beat
- CHECK (s.mode == lynx::PlayMode::song);
- }
- TEST (plugin_factories_create_reuse_and_fallback)
- {
- lynx::Song s;
- s.mixer = lynx::Song::defaultMixer (2);
- lynx::ChannelDef c;
- c.instrument = lynx::InstrumentKind::plugin;
- c.plugin.id = "VST3-Synth-123";
- c.mixerTrack = 1;
- s.channels.push_back (c);
- lynx::ChannelDef missing = c;
- missing.plugin.id = "VST3-Ausente-0";
- s.channels.push_back (missing);
- lynx::EffectSlot fx;
- fx.kind = lynx::EffectKind::plugin;
- fx.plugin.id = "VST3-Reverb-9";
- s.mixer[1].effects.push_back (fx);
- s.patterns.emplace_back();
- int instrumentsMade = 0, effectsMade = 0;
- lynx::RackState::Factories f;
- f.pluginInstruments = [&] (const lynx::PluginRef& ref, double) -> std::shared_ptr<lynx::Instrument> {
- if (ref.id != "VST3-Synth-123")
- return nullptr;
- ++instrumentsMade;
- auto synth = lynx::makeInstrument (lynx::InstrumentKind::synth);
- synth->prepare (48000.0);
- return synth;
- };
- f.pluginEffects = [&] (const lynx::PluginRef&, double) -> std::shared_ptr<lynx::Effect> {
- ++effectsMade;
- auto eq = lynx::makeEffect (lynx::EffectKind::eq);
- eq->prepare (48000.0);
- return eq;
- };
- auto a = lynx::RackState::build (s, nullptr, 48000.0, f);
- CHECK (instrumentsMade == 1 && effectsMade == 1);
- CHECK (a->instruments.size() == 2 && a->instruments[1] != nullptr); // missing: muted, but it exists
- // changing the saved state doesn't recreate; swapping the plugin does
- s.channels[0].plugin.state = "abc";
- auto b = lynx::RackState::build (s, a.get(), 48000.0, f);
- CHECK (instrumentsMade == 1 && b->instruments[0] == a->instruments[0] && b->effects[1][0] == a->effects[1][0]);
- s.mixer[1].effects[0].plugin.id = "VST3-Outro-1";
- auto c2 = lynx::RackState::build (s, b.get(), 48000.0, f);
- CHECK (effectsMade == 2);
- // rendering with a missing plugin and a plugin slot breaks nothing
- lynx::RackRenderer r;
- r.prepare (48000.0, 256);
- lynx::LiveEventQueue live;
- lynx::NoteEvent on;
- on.channel = 0;
- on.velocity = 1.0f;
- live.push (on);
- on.channel = 1;
- live.push (on);
- std::vector<float> l (256), rr (256);
- r.process (c2.get(), false, 0.0, 256, l.data(), rr.data(), &live);
- float peak = 0.0f;
- for (auto v : l)
- {
- CHECK (std::isfinite (v));
- peak = std::max (peak, std::abs (v));
- }
- CHECK (peak > 0.0f);
- }
- TEST (export_song_adds_silent_tail_and_never_restarts)
- {
- auto s = lynx::Song::makeDefault();
- s.playlist.clear();
- std::int64_t length = 0;
- auto pat = lynx::makeExportSong (s, true, lynx::ppq * 8, length); // empty Playlist: uses the pattern
- CHECK (length == lynx::ppq * 4);
- CHECK (pat.mode == lynx::PlayMode::song && pat.playlist.size() == 2);
- CHECK (pat.songLength() >= length + lynx::ppq * 8);
- // no new notes after the song ends, only silence until the end of the tail
- lynx::EventBuffer events;
- const auto tps = lynx::ticksPerSample (pat.bpm, 48000.0);
- int onsAfterEnd = 0, onsBefore = 0;
- for (double tick = 0.0; tick < (double) (length + lynx::ppq * 8); tick += 512 * tps)
- {
- events.clear();
- lynx::scheduleSong (pat, tick, 512, tps, events);
- for (const auto& e : events)
- if (e.type == lynx::NoteEvent::Type::on)
- (tick + e.sampleOffset * tps >= (double) length ? onsAfterEnd : onsBefore)++;
- }
- CHECK (onsBefore == (int) s.patterns[0].notes.size());
- CHECK (onsAfterEnd == 0);
- // with Playlist: the end is the last clip's, without rounding
- s.playlist.push_back ({ 0, 0, lynx::ppq * 2, lynx::ppq * 3, 0 });
- auto song = lynx::makeExportSong (s, true, 0, length);
- CHECK (length == lynx::ppq * 5 && song.playlist.size() == 1);
- song = lynx::makeExportSong (s, false, 0, length);
- CHECK (length == lynx::ppq * 4 && song.playlist[0].start == 0);
- }
- TEST (note_edit_selection_operations)
- {
- namespace ne = lynx::noteedit;
- lynx::Pattern p;
- p.notes = { { 0, 240, 0, 60 }, { 480, 240, 0, 64 }, { 960, 480, 1, 67 } };
- // sorting keeps the indices in step
- p.notes[0].start = 1200;
- std::vector<int> tracked { 0, 2 };
- ne::sortTracking (p, tracked);
- CHECK (p.notes[2].start == 1200 && p.notes[(std::size_t) tracked[0]].start == 1200);
- CHECK (p.notes[(std::size_t) tracked[1]].key == 67);
- // group move respects the limits
- std::int64_t dt = -5000;
- int dk = 100;
- ne::move (p, { 0, 1 }, dt, dk);
- CHECK (dt == -480 && dk == 127 - 67 && p.notes[0].start == 0 && p.notes[1].key == 127);
- // duplicate: right after the selection, rounded to the grid
- lynx::Pattern q;
- q.notes = { { 0, 240, 0, 60 }, { 240, 300, 0, 62 } }; // 540-tick selection
- const auto added = ne::duplicate (q, { 0, 1 }, 960);
- CHECK (added.size() == 2 && q.notes[(std::size_t) added[0]].start == 960 && q.notes[(std::size_t) added[1]].start == 1200);
- // paste onto another channel with an offset
- const auto pasted = ne::paste (q, ne::copy (q, { 1 }), 100, 3);
- CHECK (pasted.size() == 1 && q.notes.back().channel == 3 && q.notes.back().start == 340);
- // quantize rounds to the nearest
- q.notes[0].start = 130;
- q.notes[1].start = 100;
- ne::quantize (q, { 0, 1 }, 240);
- CHECK (q.notes[0].start == 240 && q.notes[1].start == 0);
- // remove several (order and duplicates don't matter)
- const auto before = q.notes.size();
- ne::remove (q, { 1, 0, 1 });
- CHECK (q.notes.size() == before - 2);
- }
- TEST (note_edit_quick_operations)
- {
- namespace ne = lynx::noteedit;
- const std::int64_t step = lynx::ticksPerStep, bar = lynx::ppq * 4;
- // split: only strictly inside; the tail is appended and does not slide again
- lynx::Pattern p;
- p.notes = { { 0, 4 * step, 0, 60 } };
- p.notes[0].slide = true;
- CHECK (ne::split (p, 0, 0) == -1 && ne::split (p, 0, 4 * step) == -1);
- CHECK (ne::split (p, 0, step) == 1 && p.notes[0].length == step && p.notes[1].start == step && p.notes[1].length == 3 * step
- && ! p.notes[1].slide);
- // chop to grid: a 2.5-step note from half a step becomes 3 pieces cut on the step lines
- lynx::Pattern c;
- c.notes = { { step / 2, step * 5 / 2, 0, 60 }, { 0, step, 1, 50 } };
- const auto pieces = ne::chop (c, { 0, 1 }, step);
- CHECK (pieces.size() == 4 && c.notes.size() == 4);
- CHECK (c.notes[0].length == step / 2 && c.notes[2].start == step && c.notes[2].length == step && c.notes[3].start == 2 * step
- && c.notes[3].length == step);
- // join: touching and overlapping notes of the same key and channel merge; others stay
- lynx::Pattern j;
- j.notes = { { 0, step, 0, 60 }, { step, step, 0, 60 }, { step * 3 / 2, step * 2, 0, 60 }, { 5 * step, step, 0, 60 },
- { step, step, 0, 62 }, { step, step, 1, 60 } };
- CHECK (ne::join (j, { 0, 1, 2, 3, 4, 5 }) == 2 && j.notes.size() == 4);
- CHECK (j.notes[0].start == 0 && j.notes[0].length == step * 7 / 2);
- // legato: each note reaches the next start (a chord ends together); the last keeps its length
- lynx::Pattern l;
- l.notes = { { 0, 10, 0, 60 }, { 0, 10, 0, 64 }, { step, 10, 0, 62 }, { 3 * step, 10, 0, 65 }, { step, 10, 1, 40 } };
- ne::legato (l, { 0, 1, 2, 3, 4 });
- CHECK (l.notes[0].length == step && l.notes[1].length == step && l.notes[2].length == 2 * step && l.notes[3].length == 10
- && l.notes[4].length == 10);
- // shrink to grid and full quantise (start and end)
- ne::shrinkToGrid (l, { 3 }, step / 2);
- CHECK (l.notes[3].length == step / 2);
- lynx::Pattern q;
- q.notes = { { 10, step - 30, 0, 60 }, { step + 100, 20, 0, 60 } };
- ne::quantizeFull (q, { 0, 1 }, step);
- CHECK (q.notes[0].start == 0 && q.notes[0].length == step && q.notes[1].start == step && q.notes[1].length == step);
- // rotate inside the bar, wrapping
- lynx::Pattern r;
- r.notes = { { 0, step, 0, 60 }, { bar - step, step, 0, 62 } };
- ne::rotate (r, { 0, 1 }, step, bar, 1);
- CHECK (r.notes[0].start == step && r.notes[1].start == 0);
- ne::rotate (r, { 0, 1 }, step, bar, -2);
- CHECK (r.notes[0].start == bar - step && r.notes[1].start == bar - 2 * step);
- r.notes.push_back ({ bar + step, step, 0, 64 }); // alone in the second bar: wraps inside that bar
- ne::rotate (r, { 2 }, step, bar, -2);
- CHECK (r.notes[2].start == 2 * bar - step);
- }
- TEST (note_edit_grid_and_time_text)
- {
- namespace ne = lynx::noteedit;
- const std::int64_t step = lynx::ticksPerStep, bar = lynx::ppq * 4;
- CHECK (ne::snapDown (250.0, step) == step && ne::snapDown (-1.0, step) == -step && ne::snapDown (17.9, 1) == 17);
- CHECK (ne::snapNearest (1000.0, { 400, 1100, 980 }, 50.0, step) == 980);
- CHECK (ne::snapNearest (1000.0, { 400 }, 50.0, step) == 960); // too far: falls back to the grid
- // screen grid: 24 px per step shows steps; zoomed out it climbs to beats, bars and bar multiples
- CHECK (ne::screenStep (24.0 / step, bar) == step);
- CHECK (ne::screenStep (96.0 / bar, bar) == lynx::ppq);
- CHECK (ne::screenStep (8.0 / bar, bar) == 2 * bar);
- CHECK (ne::screenStep (200.0 / step, bar) == step / 4);
- CHECK (ne::formatTime (0, 4) == "1:1:0" && ne::formatTime (bar + 5 * step + 7, 4) == "2:6:7");
- CHECK (ne::formatTime (step * 2, 4, true) == "0:2:0");
- std::int64_t t = 0;
- CHECK (ne::parseTime ("2:6:7", 4, t) && t == bar + 5 * step + 7);
- CHECK (ne::parseTime ("3", 4, t) && t == 2 * bar);
- CHECK (ne::parseTime ("0:2:0", 4, t, true) && t == 2 * step);
- CHECK (! ne::parseTime ("0:1:0", 4, t) && ! ne::parseTime ("1:17:0", 4, t) && ! ne::parseTime ("1:1:240", 4, t)
- && ! ne::parseTime ("a", 4, t) && ! ne::parseTime ("", 4, t));
- CHECK (ne::parseTime ("1:12:0", 3, t) && ! ne::parseTime ("1:13:0", 3, t)); // 3/4: 12 steps per bar
- }
- TEST (playlist_clip_edges_and_time_selection)
- {
- namespace ne = lynx::noteedit;
- const std::int64_t beat = lynx::ppq;
- // left-edge trim: content stays in place, cannot reveal what is before the content's start
- lynx::Clip c { 0, 0, 4 * beat, 4 * beat, beat };
- ne::trimStart (c, 5 * beat, beat);
- CHECK (c.start == 5 * beat && c.offset == 2 * beat && c.length == 3 * beat);
- ne::trimStart (c, 0, beat);
- CHECK (c.start == 3 * beat && c.offset == 0 && c.length == 5 * beat);
- ne::trimStart (c, 100 * beat, beat);
- CHECK (c.length == beat && c.end() == 8 * beat);
- // slip moves the content, not the clip
- lynx::Clip s { 0, 0, 0, 2 * beat, beat };
- ne::slip (s, beat / 2);
- CHECK (s.offset == beat / 2 && s.start == 0 && s.length == 2 * beat);
- ne::slip (s, beat);
- CHECK (s.offset == 0);
- ne::slip (s, -3 * beat);
- CHECK (s.offset == 3 * beat);
- // insert / delete time across all tracks, markers included
- std::vector<lynx::Clip> clips { { 0, 0, 0, 2 * beat, 0 }, { 0, 1, 4 * beat, 2 * beat, 0 }, { 0, 2, beat, 6 * beat, 0 } };
- std::vector<lynx::Marker> markers { { 5 * beat, "A" }, { beat / 2, "B" } };
- ne::insertTime (clips, markers, 2 * beat, 4 * beat);
- CHECK (clips[0].start == 0 && clips[1].start == 8 * beat && clips[2].start == beat && markers[0].tick == 9 * beat
- && markers[1].tick == beat / 2);
- std::vector<lynx::Clip> d { { 0, 0, 0, 2 * beat, 0 }, // before: kept
- { 0, 1, 3 * beat, beat, 0 }, // inside: deleted
- { 0, 2, beat, 6 * beat, 0 }, // spans the cut: head + tail
- { 0, 3, 3 * beat, 4 * beat, 0 }, // tail outside: trimmed and moved
- { 0, 4, 8 * beat, beat, 0 } }; // after: moved left
- std::vector<lynx::Marker> dm { { 3 * beat, "in" }, { 9 * beat, "after" } };
- ne::deleteTime (d, dm, 2 * beat, 5 * beat);
- CHECK (d.size() == 5);
- CHECK (d[1].track == 2 && d[1].start == beat && d[1].length == beat);
- CHECK (d[2].track == 2 && d[2].start == 2 * beat && d[2].offset == 4 * beat && d[2].length == 2 * beat);
- CHECK (d[3].track == 3 && d[3].start == 2 * beat && d[3].offset == 2 * beat && d[3].length == 2 * beat);
- CHECK (d[4].start == 5 * beat && dm.size() == 1 && dm[0].tick == 6 * beat);
- // time selection: overlapping clips; Ctrl+B repeats what starts inside it after itself
- std::vector<lynx::Clip> r { { 0, 0, 0, beat, 0 }, { 0, 1, beat, 2 * beat, 0 }, { 0, 0, 4 * beat, beat, 0 } };
- CHECK ((ne::clipsInTime (r, beat / 2, 2 * beat) == std::vector<int> { 0, 1 }));
- const auto added = ne::repeatTime (r, 0, 4 * beat, {});
- CHECK (added.size() == 2 && r[3].start == 4 * beat && r[4].start == 5 * beat && r[4].track == 1);
- CHECK (ne::repeatTime (r, 0, 2 * beat, { 2 }).size() == 1 && r.back().start == 6 * beat);
- }
- TEST (recorder_builds_notes_in_pattern_and_song_mode)
- {
- auto song = lynx::Song::makeDefault();
- const auto loop = song.patterns[0].effectiveLength (4); // 1 bar = 3840
- lynx::NoteRecorder r;
- r.add (song, { 100.0, 4, 60, 0.5f, true }); // no take: ignored
- CHECK (r.takeFinished().empty());
- r.begin (loop);
- r.add (song, { 4000.0, 4, 60, 0.5f, true }); // second loop pass
- r.add (song, { 4480.0, 4, 60, 0.0f, false });
- r.add (song, { 5000.0, 4, 64, 0.9f, true }); // held until stop
- r.end (song, 5240.0);
- const auto notes = r.takeFinished();
- CHECK (notes.size() == 2);
- CHECK (notes[0].start == 4000 - 3840 && notes[0].length == 480 && notes[0].velocity == 0.5f && notes[0].channel == 4);
- CHECK (notes[1].key == 64 && notes[1].start == 5000 - 3840 && notes[1].length == 240);
- // song mode: position inside the current pattern's clip (with offset)
- song.mode = lynx::PlayMode::song;
- song.playlist = { { 0, 0, 7680, 3840, 960 } };
- CHECK (lynx::patternPositionForTick (song, 7680.0 + 100.0, loop) == 1060);
- CHECK (lynx::patternPositionForTick (song, 100.0, loop) == 100); // outside the clip: like loop
- }
- TEST (automation_curve_and_playback)
- {
- lynx::Automation a;
- a.points = { { 0, 0.0f }, { 960, 1.0f }, { 1920, 0.5f, 0.0f } };
- CHECK (a.valueAt (-10.0) == 0.0f && a.valueAt (480.0) == 0.5f && a.valueAt (5000.0) == 0.5f);
- CHECK (a.length() == 1920);
- a.points[0].tension = 1.0f; // curve: starts slow
- CHECK (a.valueAt (480.0) < 0.2f && a.valueAt (480.0) > 0.0f);
- // automated Master: silence in the first bar, sound in the second, and the last value
- // keeps applying after the automation clip ends
- auto song = lynx::Song::makeDefault();
- song.bpm = 120.0;
- song.mode = lynx::PlayMode::song;
- const auto bar = (std::int64_t) lynx::ppq * 4;
- for (int b = 0; b < 3; ++b)
- song.playlist.push_back ({ 0, 0, bar * b, bar, 0 });
- lynx::Automation master;
- master.target = { lynx::AutomationTargetKind::mixerVolume, 0 };
- master.points = { { 0, 0.0f }, { bar - 1, 0.0f }, { bar, 0.8f } };
- song.automations.push_back (master);
- lynx::Clip ac;
- ac.kind = lynx::ClipKind::automation;
- ac.automation = 0;
- ac.track = 1;
- ac.length = bar + bar / 2;
- song.playlist.push_back (ac);
- const double sr = 48000.0;
- auto state = lynx::RackState::build (song, nullptr, sr);
- CHECK (state->hasAutomation && state->mixerVolumeAuto[0] == 0 && state->automationClips[0].size() == 1);
- lynx::RackRenderer r;
- r.prepare (sr, 512);
- std::vector<float> l (512), rr (512);
- const auto tps = lynx::ticksPerSample (song.bpm, sr);
- float peakBar1 = 0.0f, peakBar2 = 0.0f, peakBar3 = 0.0f;
- for (double tick = 0.0; tick < (double) (bar * 3); tick += 512 * tps)
- {
- r.process (state.get(), true, tick, 512, l.data(), rr.data());
- auto& peak = tick < (double) bar - 512 * tps ? peakBar1 : tick < (double) bar * 2 ? peakBar2 : peakBar3;
- for (auto v : l)
- peak = std::max (peak, std::abs (v));
- }
- CHECK (peakBar1 < 1.0e-6f);
- CHECK (peakBar2 > 0.05f);
- CHECK (peakBar3 > 0.05f); // after the clip: keeps the last value
- // target removed: automation disappears from the tables
- song.removeChannel (0);
- song.automations[0].target = { lynx::AutomationTargetKind::channelVolume, 0 };
- song.removeChannel (0);
- CHECK (song.automations[0].target.kind == lynx::AutomationTargetKind::none);
- song.removeAutomation (0);
- CHECK (song.automations.empty() && song.playlist.size() == 3);
- }
- TEST (mixer_sends_between_inserts)
- {
- // insert 1 goes only to 3; 3 goes to the Master: same sum (0.5 + 0.5)
- CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.setRoute (1, 3, true); s.setRoute (1, 0, false); }) - 1.0f) < 1e-4f);
- // send at 50% and muted destination
- CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.setRoute (1, 3, true, 0.5f); s.setRoute (1, 0, false); }) - 0.75f) < 1e-4f);
- 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);
- // going to the Master and to 3: the signal doubles
- CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.setRoute (1, 3, true); }) - 1.5f) < 1e-4f);
- // solo on 3 lets what reaches it sound (insert 1), but not 2
- 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);
- // solo on 1 lets the path to the Master (3) sound
- 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);
- // order and cycles
- auto song = lynx::Song::makeDefault();
- song.setRoute (4, 2, true);
- song.setRoute (2, 1, true);
- CHECK (! song.canRoute (1, 4) && ! song.canRoute (1, 2) && song.canRoute (1, 5));
- song.setRoute (1, 4, true); // ignored: would create a cycle
- CHECK (! song.routes (1, 4));
- const auto order = song.mixerOrder();
- const auto pos = [&] (int t) { return std::find (order.begin(), order.end(), t) - order.begin(); };
- CHECK ((int) order.size() == (int) song.mixer.size() - 1);
- CHECK (pos (4) < pos (2) && pos (2) < pos (1));
- song.setRoute (4, 2, false);
- CHECK (! song.routes (4, 2) && song.routes (4, 0));
- }
- namespace
- {
- // Test effect: delays the signal by `latency` samples and reports that latency (like a lookahead plugin).
- class LatentEffect final : public lynx::Effect
- {
- public:
- explicit LatentEffect (int samples) : latency (samples), line (samples) {}
- void prepare (double) noexcept override {}
- void reset() noexcept override {}
- void process (float* l, float* r, int n, const lynx::EffectParams&, double) noexcept override { line.process (l, r, n); }
- int latencySamples() const noexcept override { return latency; }
- private:
- int latency;
- lynx::DelayLine line;
- };
- } // namespace
- TEST (delay_line_mix_into_matches_process_and_drains_tail)
- {
- // mixInto = process over source*level, added to the destination; in stretches that cross the ring
- lynx::DelayLine a (37), b (37);
- std::vector<float> sl (300), sr (300), dl (300, 0.5f), dr (300, -0.5f), xl (300), xr (300);
- for (int i = 0; i < 300; ++i)
- {
- sl[(std::size_t) i] = (float) std::sin (i * 0.1);
- sr[(std::size_t) i] = (float) std::cos (i * 0.07);
- }
- for (int start = 0; start < 300; start += 100)
- {
- b.mixInto (sl.data() + start, sr.data() + start, 0.25f, dl.data() + start, dr.data() + start, 100);
- for (int i = start; i < start + 100; ++i)
- {
- xl[(std::size_t) i] = sl[(std::size_t) i] * 0.25f;
- xr[(std::size_t) i] = sr[(std::size_t) i] * 0.25f;
- }
- a.process (xl.data() + start, xr.data() + start, 100);
- }
- float worst = 0.0f;
- for (int i = 0; i < 300; ++i)
- worst = std::max ({ worst, std::abs (dl[(std::size_t) i] - 0.5f - xl[(std::size_t) i]),
- std::abs (dr[(std::size_t) i] + 0.5f - xr[(std::size_t) i]) });
- CHECK (worst < 1e-6f);
- // silenced source: the tail still in the line comes out, then nothing remains
- std::vector<float> tl (64, 0.0f), tr (64, 0.0f);
- b.mixInto (nullptr, nullptr, 1.0f, tl.data(), tr.data(), 64);
- CHECK (std::abs (tl[0] - sl[263] * 0.25f) < 1e-6f);
- std::vector<float> zl (64, 0.0f), zr (64, 0.0f);
- b.mixInto (nullptr, nullptr, 1.0f, zl.data(), zr.data(), 64);
- CHECK (*std::max_element (zl.begin(), zl.end()) == 0.0f && *std::min_element (zl.begin(), zl.end()) == 0.0f);
- }
- TEST (limiter_lookahead_holds_ceiling_on_bursts)
- {
- // short bursts over a low floor: the sliding maximum must see each peak before it leaves
- auto fx = lynx::makeEffect (lynx::EffectKind::limiter);
- fx->prepare (48000.0);
- auto slot = lynx::makeEffectSlot (lynx::EffectKind::limiter);
- slot.params[2] = 0.0f; // 10 ms release
- std::vector<float> l (48000), r (48000);
- std::uint32_t seed = 1;
- for (int i = 0; i < 48000; ++i)
- {
- seed = seed * 1664525u + 1013904223u;
- const auto noise = ((float) (seed >> 8) / 16777216.0f - 0.5f) * 0.1f;
- l[(std::size_t) i] = noise + ((i % 9973) < 3 ? 3.0f : 0.0f);
- r[(std::size_t) i] = -noise;
- }
- for (int i = 0; i < 48000; i += 333)
- fx->process (l.data() + i, r.data() + i, std::min (333, 48000 - i), slot.params, 120.0);
- float peak = 0.0f, quiet = 0.0f;
- for (int i = 0; i < 48000; ++i)
- peak = std::max (peak, std::abs (l[(std::size_t) i]));
- for (int i = 9973 * 2 + 2000; i < 9973 * 2 + 2400; ++i) // 40 ms after the burst the gain has already recovered
- quiet = std::max (quiet, std::abs (r[(std::size_t) i]));
- const auto ceiling = 1.0f; // the default ceiling is at 0 dBFS or below
- CHECK (peak <= ceiling + 1e-5f);
- CHECK (quiet > 0.03f);
- }
- TEST (pdc_aligns_paths_through_latent_effects)
- {
- // plan: channel 0 -> insert 1 (effect of 100) -> insert 3; channel 1 -> insert 2 -> Master
- lynx::Song s;
- s.mixer = lynx::Song::defaultMixer (4);
- s.channels.resize (3);
- s.channels[0].mixerTrack = 1;
- s.channels[1].mixerTrack = 2;
- s.channels[2].mixerTrack = 3;
- s.mixer[1].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::eq));
- s.setRoute (1, 3, true);
- s.setRoute (1, 0, false);
- auto plan = lynx::computePdc (s, { 0, 0, 30 }, { {}, { 100 } });
- CHECK (plan.inAlign[1] == 0 && plan.outLat[1] == 100);
- CHECK (plan.inAlign[3] == 100 && plan.channelDelay[2] == 70); // instrument of 30 on insert 3
- CHECK (plan.sendDelay[1][1] == 0); // the slowest path isn't delayed
- CHECK (plan.sendDelay[2][0] == 100 && plan.sendDelay[3][0] == 0 && plan.total == 100);
- // bypass zeroes the slot's latency
- s.mixer[1].effects[0].enabled = false;
- CHECK (lynx::computePdc (s, { 0, 0, 30 }, { {}, { 100 } }).total == 30);
- // rendered: two impulses played together come out together
- lynx::Song r;
- r.mixer = lynx::Song::defaultMixer (2);
- for (int i = 0; i < 2; ++i)
- {
- lynx::ChannelDef c;
- c.instrument = lynx::InstrumentKind::sampler;
- c.samplePath = "impulso";
- c.volume = 1.0f;
- c.mixerTrack = i + 1;
- r.channels.push_back (c);
- }
- r.patterns.emplace_back();
- lynx::EffectSlot fx;
- fx.kind = lynx::EffectKind::plugin;
- fx.plugin.id = "latente";
- r.mixer[1].effects.push_back (fx);
- lynx::RackState::Factories f;
- f.samples = [] (const std::string&) {
- auto d = std::make_shared<lynx::SampleData>();
- d->left.assign (64, 0.0f);
- d->left[0] = 1.0f;
- return std::shared_ptr<const lynx::SampleData> (d);
- };
- f.pluginEffects = [] (const lynx::PluginRef&, double) -> std::shared_ptr<lynx::Effect> { return std::make_shared<LatentEffect> (100); };
- auto state = lynx::RackState::build (r, nullptr, 48000.0, f);
- CHECK (state->pdc.total == 100 && state->sendDelays[2][0] != nullptr && state->sendDelays[2][0]->length() == 100);
- lynx::RackRenderer renderer;
- renderer.prepare (48000.0, 256);
- lynx::LiveEventQueue live;
- for (int ch = 0; ch < 2; ++ch)
- {
- lynx::NoteEvent on;
- on.channel = ch;
- on.key = 60;
- on.velocity = 1.0f;
- live.push (on);
- }
- std::vector<float> l (256), rr (256);
- renderer.process (state.get(), false, 0.0, 256, l.data(), rr.data(), &live);
- int first = -1;
- for (int i = 0; i < 256 && first < 0; ++i)
- if (std::abs (l[(std::size_t) i]) > 0.01f)
- first = i;
- CHECK (first == 100 && std::abs (l[100] - 2.0f) < 1e-3f); // both impulses arrive together, summed
- // editing without changing latencies reuses the lines
- r.channels[0].volume = 0.5f;
- auto next = lynx::RackState::build (r, state.get(), 48000.0, f);
- CHECK (next->sendDelays[2][0] == state->sendDelays[2][0]);
- }
- TEST (mixer_mute_fades_instead_of_clicking)
- {
- lynx::Song s;
- s.mixer = lynx::Song::defaultMixer (2);
- lynx::ChannelDef c;
- c.instrument = lynx::InstrumentKind::sampler;
- c.samplePath = "dc";
- c.volume = 1.0f;
- c.mixerTrack = 1;
- s.channels.push_back (c);
- s.patterns.emplace_back();
- auto provider = [] (const std::string&) {
- auto d = std::make_shared<lynx::SampleData>();
- d->left.assign (48000, 0.5f);
- return std::shared_ptr<const lynx::SampleData> (d);
- };
- auto on = lynx::RackState::build (s, nullptr, 48000.0, provider);
- s.mixer[1].muted = true;
- auto off = lynx::RackState::build (s, on.get(), 48000.0, provider);
- lynx::RackRenderer r;
- r.prepare (48000.0, 256);
- lynx::LiveEventQueue live;
- lynx::NoteEvent note;
- note.velocity = 1.0f;
- live.push (note);
- std::vector<float> l (256), rr (256);
- r.process (on.get(), false, 0.0, 256, l.data(), rr.data(), &live);
- CHECK (std::abs (l[255] - 0.5f) < 1e-3f);
- r.process (off.get(), false, 0.0, 256, l.data(), rr.data());
- CHECK (l[0] > 0.45f && l[128] > 0.2f && l[128] < 0.3f && std::abs (l[255]) < 1e-6f); // ramp down to zero
- r.process (off.get(), false, 0.0, 256, l.data(), rr.data());
- CHECK (std::abs (l[0]) < 1e-9f);
- }
- TEST (dither_quantizes_to_lsb_grid_with_small_error)
- {
- lynx::Dither d (16);
- std::vector<float> x (4800);
- for (std::size_t i = 0; i < x.size(); ++i)
- x[i] = 0.25f * std::sin ((float) i * 0.05f);
- auto y = x;
- d.process (y.data(), (int) y.size(), 0);
- double errorSum = 0.0;
- bool onGrid = true;
- for (std::size_t i = 0; i < x.size(); ++i)
- {
- const auto steps = y[i] * 32768.0f;
- onGrid = onGrid && std::abs (steps - std::round (steps)) < 1e-3f;
- errorSum += std::abs (y[i] - x[i]);
- }
- CHECK (onGrid);
- CHECK (errorSum / (double) x.size() < 4.0 / 32768.0); // a few LSBs of noise on average
- }
- TEST (parallel_rendering_matches_sequential)
- {
- // 32 channels (worth parallelizing), inserts with chained sends
- auto song = lynx::Song::makeDefault();
- song.mixer = lynx::Song::defaultMixer (16);
- for (int i = 5; i < 32; ++i)
- {
- lynx::ChannelDef c;
- c.instrument = i % 2 ? lynx::InstrumentKind::synth : lynx::InstrumentKind::hat;
- c.mixerTrack = 1 + i % 12;
- c.volume = 0.3f;
- song.channels.push_back (c);
- for (int step = i % 4; step < 16; step += 3)
- song.patterns[0].notes.push_back ({ (std::int64_t) step * lynx::ticksPerStep, 200, i, 48 + i, 0.7f });
- }
- song.patterns[0].sortNotes();
- song.setRoute (3, 7, true, 0.6f);
- song.setRoute (7, 9, true, 0.5f);
- song.setRoute (2, 9, true);
- song.mixer[9].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::reverb));
- auto seqState = lynx::RackState::build (song, nullptr, 48000.0);
- auto parState = lynx::RackState::build (song, nullptr, 48000.0);
- CHECK (parState->parallel && parState->mixerLayers.size() == 3);
- lynx::WorkerPool pool (3);
- lynx::RackRenderer seq, par;
- seq.prepare (48000.0, 512);
- par.prepare (48000.0, 512);
- par.setWorkerPool (&pool);
- std::vector<float> l1 (512), r1 (512), l2 (512), r2 (512);
- const auto tps = lynx::ticksPerSample (song.bpm, 48000.0);
- bool same = true;
- float peak = 0.0f;
- for (int block = 0; block < 200; ++block)
- {
- seq.process (seqState.get(), true, block * 512 * tps, 512, l1.data(), r1.data());
- par.process (parState.get(), true, block * 512 * tps, 512, l2.data(), r2.data());
- same = same && l1 == l2 && r1 == r2;
- for (auto v : l1)
- peak = std::max (peak, std::abs (v));
- }
- CHECK (same);
- CHECK (peak > 0.05f);
- // the pool handles many small rounds in a row
- std::atomic<int> sum { 0 };
- auto add = [&sum] (int i) { sum.fetch_add (i + 1); };
- for (int round = 0; round < 2000; ++round)
- pool.parallelFor (5, add);
- CHECK (sum.load() == 2000 * 15);
- }
- TEST (arrangements_switch_and_keep_clip_indices)
- {
- auto s = lynx::Song::makeDefault();
- s.patterns.emplace_back().name = "B";
- s.playlist = { { 1, 0, 0, 960, 0 } };
- s.loopEnd = 960;
- CHECK (s.arrangements.size() == 1 && s.currentArrangement == 0);
- const auto second = s.addArrangement ("Versão curta", true);
- CHECK (second == 1 && s.currentArrangement == 1 && s.playlist.size() == 1 && s.loopEnd == 960);
- s.playlist.push_back ({ 0, 1, 960, 960, 0 });
- s.loopEnd = 0;
- s.selectArrangement (0);
- CHECK (s.playlist.size() == 1 && s.loopEnd == 960 && s.arrangements[1].clips.size() == 2);
- // removing pattern 0 fixes the clips stored in the other arrangement
- s.removePattern (0);
- CHECK (s.playlist.size() == 1 && s.playlist[0].pattern == 0);
- CHECK (s.arrangements[1].clips.size() == 1 && s.arrangements[1].clips[0].pattern == 0);
- s.removeArrangement (0);
- CHECK (s.arrangements.size() == 1 && s.currentArrangement == 0 && s.playlist.size() == 1 && s.loopEnd == 0);
- }
- namespace
- {
- // Test effect that inverts the signal and counts how many times it processed.
- class CountingInverter final : public lynx::Effect
- {
- public:
- void prepare (double) noexcept override {}
- void reset() noexcept override {}
- void process (float* l, float* r, int n, const lynx::EffectParams&, double) noexcept override
- {
- ++calls;
- for (int i = 0; i < n; ++i)
- {
- l[i] = -l[i];
- r[i] = -r[i];
- }
- }
- int calls = 0;
- };
- } // namespace
- TEST (keyboard_controller_outputs_key_value)
- {
- std::vector<std::uint8_t> blob (554, 0);
- const std::uint32_t version = 3;
- std::memcpy (blob.data(), &version, 4);
- const float key60 = 0.7f, off = 0.2f;
- std::memcpy (blob.data() + 12 + 60 * 4, &key60, 4);
- std::memcpy (blob.data() + 548, &off, 4);
- blob[552] = 1; // auto release
- lynx::ChannelDef c;
- c.instrument = lynx::InstrumentKind::controller;
- c.controllerState = blob;
- auto inst = lynx::makeInstrument (lynx::InstrumentKind::controller);
- inst->prepare (48000.0);
- inst->configure (c, 120.0);
- std::vector<float> l (64), r (64);
- inst->noteOn (60, 1.0f);
- inst->render (l.data(), r.data(), 64, 1.0f, 1.0f);
- CHECK (std::abs (inst->controlOutput (0) - 0.7f) < 1e-6f);
- inst->noteOff (60);
- CHECK (std::abs (inst->controlOutput (0) - 0.2f) < 1e-6f);
- }
- TEST (envelope_controller_ducks_on_each_note)
- {
- // a unipolar articulator like "Basic Sidechain": base 0.05, drops to 0 on the note and rises to 1 in 0.3 s
- lynx::ControllerArticulator a;
- a.envOn = true;
- a.base = 0.05f;
- a.envLevel = 1.0f;
- a.count = 3;
- a.time = { 0.0f, 0.2f, 0.3f };
- a.level = { 0.0f, 0.75f, 1.0f };
- CHECK (std::abs (lynx::controllerValue (a, 0.0, -1.0) - 0.05f) < 1e-4f);
- CHECK (std::abs (lynx::controllerValue (a, 0.1, -1.0) - 0.425f) < 1e-3f);
- CHECK (lynx::controllerValue (a, 1.0, -1.0) > 0.99f); // no sustain: runs to the end and stays
- a.sustain = 1;
- CHECK (std::abs (lynx::controllerValue (a, 1.0, -1.0) - 0.8f) < 1e-3f); // held at sustain
- CHECK (lynx::controllerValue (a, 1.0, 0.5) > 0.99f); // released: continues to the end
- a.bipolar = true;
- a.base = 0.5f;
- a.sustain = -1;
- CHECK (std::abs (lynx::controllerValue (a, 0.0, -1.0) - 0.0f) < 1e-4f); // bipolar: 0.5 + (0 − 0.5)
- }
- TEST (peak_controller_drives_live_automation)
- {
- // track 1: source (DC 0.5) with Peak Controller; track 2: DC 0.5 with volume linked to the controller
- auto render = [] (bool linked) {
- lynx::Song s;
- s.mixer = lynx::Song::defaultMixer (2);
- for (int t = 1; t <= 2; ++t)
- {
- lynx::ChannelDef c;
- c.instrument = lynx::InstrumentKind::sampler;
- c.samplePath = "dc";
- c.volume = 1.0f;
- c.mixerTrack = t;
- s.channels.push_back (c);
- }
- s.patterns.emplace_back();
- auto peak = lynx::makeEffectSlot (lynx::EffectKind::peakController);
- peak.params[0] = 1.0f; // base 1
- peak.params[1] = 0.0f; // amount −1: 1 − peak
- peak.params[2] = 0.5f; // no tension
- s.mixer[1].effects.push_back (peak);
- if (linked)
- {
- lynx::Automation a;
- a.name = "Peak";
- a.target = { lynx::AutomationTargetKind::mixerVolume, 2 };
- a.source = { 1, 0, 0 };
- s.automations.push_back (a);
- }
- lynx::RackState::Factories f;
- f.samples = [] (const std::string&) {
- auto d = std::make_shared<lynx::SampleData>();
- d->left.assign (48000, 0.5f);
- return std::shared_ptr<const lynx::SampleData> (d);
- };
- auto state = lynx::RackState::build (s, nullptr, 48000.0, f);
- lynx::RackRenderer r;
- r.prepare (48000.0, 512);
- lynx::LiveEventQueue live;
- for (int ch = 0; ch < 2; ++ch)
- {
- lynx::NoteEvent on;
- on.channel = ch;
- on.velocity = 1.0f;
- live.push (on);
- }
- std::vector<float> l (4800), rr (4800);
- r.process (state.get(), false, 0.0, 4800, l.data(), rr.data(), &live);
- return l[4000];
- };
- const auto free = render (false), ducked = render (true);
- CHECK (std::isfinite (ducked) && ducked > 0.0f);
- CHECK (ducked < free - 0.05f); // the 0.5 peak on track 1 lowers track 2's volume
- }
- TEST (effect_slot_dry_wet_and_smart_disable)
- {
- lynx::Song s;
- s.mixer = lynx::Song::defaultMixer (1);
- lynx::ChannelDef c;
- c.instrument = lynx::InstrumentKind::sampler;
- c.samplePath = "dc";
- c.volume = 1.0f;
- c.mixerTrack = 1;
- s.channels.push_back (c);
- s.patterns.emplace_back();
- lynx::EffectSlot fx;
- fx.kind = lynx::EffectKind::plugin;
- fx.plugin.id = "inversor";
- fx.mix = 0.25f; // 75% dry (+0.5) and 25% wet (-0.5) = +0.25
- s.mixer[1].effects.push_back (fx);
- auto inverter = std::make_shared<CountingInverter>();
- lynx::RackState::Factories f;
- f.samples = [] (const std::string&) {
- auto d = std::make_shared<lynx::SampleData>();
- d->left.assign (4800, 0.5f);
- return std::shared_ptr<const lynx::SampleData> (d);
- };
- f.pluginEffects = [inverter] (const lynx::PluginRef&, double) -> std::shared_ptr<lynx::Effect> { return inverter; };
- auto state = lynx::RackState::build (s, nullptr, 48000.0, f);
- lynx::RackRenderer r;
- r.prepare (48000.0, 512);
- lynx::LiveEventQueue live;
- lynx::NoteEvent on;
- on.velocity = 1.0f;
- live.push (on);
- std::vector<float> l (480), rr (480);
- r.process (state.get(), false, 0.0, 480, l.data(), rr.data(), &live);
- CHECK (std::abs (l[100] - 0.25f) < 1e-4f);
- // after the sample ends: 4 s of silence and the plugin stops being called
- for (int i = 0; i < 100 * 6; ++i) // 6 s
- r.process (state.get(), false, 0.0, 480, l.data(), rr.data());
- const auto callsAfterSilence = inverter->calls;
- for (int i = 0; i < 100; ++i)
- r.process (state.get(), false, 0.0, 480, l.data(), rr.data());
- CHECK (inverter->calls == callsAfterSilence);
- CHECK (callsAfterSilence < 100 * 6);
- // sound again: processing resumes in the same block
- live.push (on);
- r.process (state.get(), false, 0.0, 480, l.data(), rr.data(), &live);
- CHECK (inverter->calls == callsAfterSilence + 1 && std::abs (l[100] - 0.25f) < 1e-4f);
- }
- TEST (swing_delays_offbeat_sixteenths)
- {
- CHECK (lynx::swingDelay (0, 1.0f) == 0);
- CHECK (lynx::swingDelay (lynx::ticksPerStep, 1.0f) == lynx::ticksPerStep / 2);
- CHECK (lynx::swingDelay (lynx::ticksPerStep * 2, 1.0f) == 0);
- CHECK (lynx::swingDelay (lynx::ticksPerStep + 10, 1.0f) == (lynx::ticksPerStep - 20) / 2); // off the grid: triangular weight (FL)
- CHECK (lynx::swingDelay (lynx::ticksPerStep, 1.0f, 0.5f) == lynx::ticksPerStep / 4); // channel mix
- CHECK (lynx::swingDelay (lynx::ticksPerStep * 3, 0.5f) == lynx::ticksPerStep / 4);
- lynx::Pattern p;
- p.notes = { { 0, 100, 0, 60 }, { lynx::ticksPerStep, 100, 0, 62 } };
- lynx::EventBuffer ev;
- const auto tps = lynx::ticksPerSample (120.0, 48000.0);
- lynx::schedulePatternLoop (p, p.effectiveLength (4), 0.0, 12000, tps, ev, 1.0f);
- int onOffset = -1;
- for (auto& e : ev)
- if (e.type == lynx::NoteEvent::Type::on && e.key == 62)
- onOffset = e.sampleOffset;
- CHECK (onOffset == (int) std::floor ((lynx::ticksPerStep * 1.5) / tps));
- }
- TEST (sampler_settings_pitch_reverse_envelope_cut)
- {
- auto ramp = std::make_shared<lynx::SampleData>();
- for (int i = 0; i < 48000; ++i)
- ramp->left.push_back ((float) i / 48000.0f); // ramp: the value tells where the read position is
- ramp->sampleRate = 48000.0;
- auto play = [&] (const lynx::SamplerSettings& s, int key, int frames, bool releaseAfterHalf) {
- auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
- inst->prepare (48000.0);
- lynx::ChannelDef c;
- c.sampler = s;
- inst->configure (c, 120.0);
- inst->noteOn (key, 1.0f);
- std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
- inst->render (l.data(), r.data(), frames / 2, 1.0f, 1.0f);
- if (releaseAfterHalf)
- inst->noteOff (key);
- inst->render (l.data() + frames / 2, r.data() + frames / 2, frames - frames / 2, 1.0f, 1.0f);
- return l;
- };
- // +12 semitones: reads twice as fast
- lynx::SamplerSettings up;
- up.pitch = 12.0f;
- CHECK (std::abs (play (up, 60, 1000, false)[999] - 2.0f * 999.0f / 48000.0f) < 1e-3f);
- // reversed: starts from the end
- lynx::SamplerSettings back;
- back.reverse = true;
- CHECK (play (back, 60, 100, false)[10] > 0.99f);
- // 10 ms attack: starts silent and reaches full level
- lynx::SamplerSettings slow;
- slow.attack = 0.01f;
- const auto a = play (slow, 60, 2000, false);
- CHECK (a[10] < 0.2f * (10.0f / 48000.0f) + 1e-6f && std::abs (a[1500] - 1500.0f / 48000.0f) < 1e-4f);
- // 5 ms release: gone right after release; without release it plays in full
- lynx::SamplerSettings rel;
- rel.release = 0.005f;
- CHECK (play (rel, 60, 2000, true)[1999] == 0.0f);
- CHECK (play (lynx::SamplerSettings {}, 60, 2000, true)[1999] > 0.0f);
- // cut itself: the second note cuts the first (one voice remains)
- lynx::SamplerSettings cut;
- cut.cut = true;
- auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
- inst->prepare (48000.0);
- lynx::ChannelDef cutChannel;
- cutChannel.sampler = cut;
- inst->configure (cutChannel, 120.0);
- inst->noteOn (60, 1.0f);
- std::vector<float> l (2000), r (2000);
- inst->render (l.data(), r.data(), 1000, 1.0f, 1.0f);
- inst->noteOn (60, 1.0f);
- std::fill (l.begin(), l.end(), 0.0f);
- inst->render (l.data(), r.data(), 2000, 1.0f, 1.0f);
- CHECK (std::abs (l[1999] - 1999.0f / 48000.0f) < 1e-4f); // only the new note
- }
- TEST (playlist_split_and_track_mute)
- {
- lynx::Clip c { 0, 3, 960, 3840, 480 };
- const auto rest = lynx::splitClip (c, 2000);
- CHECK (c.start == 960 && c.length == 1040 && c.offset == 480);
- CHECK (rest.start == 2000 && rest.length == 2800 && rest.offset == 480 + 1040 && rest.track == 3);
- auto s = lynx::Song::makeDefault();
- s.mode = lynx::PlayMode::song;
- s.playlist = { { 0, 0, 0, 3840, 0 } };
- lynx::EventBuffer ev;
- const auto tps = lynx::ticksPerSample (120.0, 48000.0);
- lynx::scheduleSong (s, 0.0, 48000, tps, ev);
- CHECK (ev.size() > 0);
- s.playlistTracks.resize (1);
- s.playlistTracks[0].muted = true;
- ev.clear();
- lynx::scheduleSong (s, 0.0, 48000, tps, ev);
- CHECK (ev.size() == 0);
- }
- TEST (note_pan_pitch_and_mute)
- {
- lynx::Pattern p;
- lynx::Note a { 0, 240, 0, 60 };
- a.pan = -1.0f;
- a.finePitch = 50.0f;
- lynx::Note b { 240, 240, 0, 62 };
- b.muted = true;
- p.notes = { a, b };
- lynx::EventBuffer ev;
- const auto tps = lynx::ticksPerSample (120.0, 48000.0);
- lynx::schedulePatternLoop (p, p.effectiveLength (4), 0.0, 48000, tps, ev);
- int ons = 0;
- for (auto& e : ev)
- if (e.type == lynx::NoteEvent::Type::on)
- {
- ++ons;
- CHECK (e.key == 60 && e.pan == -1.0f && e.cents == 50.0f);
- }
- CHECK (ons == 1); // the muted one doesn't play
- // note pan in the sampler: fully left
- auto dc = std::make_shared<lynx::SampleData>();
- dc->left.assign (4800, 0.5f);
- auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, dc);
- inst->prepare (48000.0);
- inst->setNextNoteExpression (-1.0f, 0.0f);
- inst->noteOn (60, 1.0f);
- std::vector<float> l (100), r (100);
- inst->render (l.data(), r.data(), 100, 1.0f, 1.0f);
- CHECK (l[50] > 0.6f && std::abs (r[50]) < 1e-6f);
- }
- TEST (tempo_automation_changes_speed_and_duration)
- {
- auto song = lynx::Song::makeDefault();
- song.bpm = 120.0;
- song.mode = lynx::PlayMode::song;
- const auto bar = (std::int64_t) lynx::ppq * 4;
- song.playlist = { { 0, 0, 0, bar, 0 }, { 0, 0, bar, bar, 0 } };
- lynx::Automation tempo;
- tempo.target = { lynx::AutomationTargetKind::tempo, 0 };
- const auto v120 = lynx::valueFromTempo (120.0), v240 = lynx::valueFromTempo (240.0);
- tempo.points = { { 0, v120 }, { bar - 1, v120 }, { bar, v240 } }; // step: 120 -> 240 at bar 2
- song.automations.push_back (tempo);
- lynx::Clip clip;
- clip.kind = lynx::ClipKind::automation;
- clip.automation = 0;
- clip.track = 1;
- clip.length = bar * 2;
- song.playlist.push_back (clip);
- CHECK (std::abs (song.tempoAt (100.0) - 120.0) < 0.01 && std::abs (song.tempoAt ((double) bar + 100.0) - 240.0) < 0.01);
- // 1 bar at 120 (2 s) + 1 at 240 (1 s)
- CHECK (std::abs (song.secondsForTicks (bar * 2) - 3.0) < 0.01);
- auto state = lynx::RackState::build (song, nullptr, 48000.0);
- CHECK (state->tempoAutomation == 0);
- lynx::RackRenderer r;
- r.prepare (48000.0, 512);
- std::vector<float> l (512), rr (512);
- r.process (state.get(), true, 10.0, 512, l.data(), rr.data());
- CHECK (std::abs (r.currentBpm() - 120.0) < 0.01);
- r.process (state.get(), true, (double) bar + 10.0, 512, l.data(), rr.data());
- CHECK (std::abs (r.currentBpm() - 240.0) < 0.01);
- }
- TEST (bounce_insert_song_and_apply)
- {
- auto s = lynx::Song::makeDefault();
- s.setRoute (3, 7, true, 0.5f);
- s.mixer[0].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::reverb));
- s.mixer[2].solo = true;
- const auto b = lynx::makeBounceSong (s, 3);
- CHECK (b.mixer[3].solo && ! b.mixer[2].solo && b.mixer[3].toMaster && b.mixer[3].sends.empty());
- CHECK (b.mixer[0].effects.empty() && b.mode == lynx::PlayMode::song);
- const auto audible = b.soloAudible();
- CHECK (audible[3] && ! audible[1] && ! audible[7]);
- const auto channels = s.channels.size();
- const auto inserts = s.mixer.size();
- const auto created = lynx::applyBounce (s, 3, "C:/x.wav", 3840);
- CHECK (created == (int) inserts && s.mixer.size() == inserts + 1 && s.mixer[3].muted);
- CHECK (s.mixer[(std::size_t) created].sends.size() == 1 && s.mixer[(std::size_t) created].sends[0].target == 7);
- CHECK (s.channels.size() == channels + 1 && s.channels.back().mixerTrack == created);
- CHECK (s.playlist.back().kind == lynx::ClipKind::audio && s.playlist.back().length == 3840);
- }
- TEST (mixer_stereo_separation_phase_swap)
- {
- lynx::MixerTrack t;
- std::vector<float> l { 1.0f }, r { 0.0f };
- t.stereo = 1.0f; // mono
- lynx::RackRenderer::applyStereoForTest (t, l.data(), r.data(), 1);
- CHECK (std::abs (l[0] - 0.5f) < 1e-6f && std::abs (r[0] - 0.5f) < 1e-6f);
- l = { 1.0f };
- r = { 0.0f };
- t.stereo = -1.0f; // doubled side: 1.5 L - 0.5 R
- lynx::RackRenderer::applyStereoForTest (t, l.data(), r.data(), 1);
- CHECK (std::abs (l[0] - 1.5f) < 1e-6f && std::abs (r[0] + 0.5f) < 1e-6f);
- l = { 1.0f };
- r = { 0.25f };
- t.stereo = 0.0f;
- t.swapChannels = true;
- t.invertPhase = true;
- lynx::RackRenderer::applyStereoForTest (t, l.data(), r.data(), 1);
- CHECK (l[0] == -0.25f && r[0] == -1.0f);
- }
- TEST (effects_limiter_filter_distortion_chorus)
- {
- const double sr = 48000.0;
- auto sine = [] (float hz, float amp, int n) {
- std::vector<float> v ((std::size_t) n);
- for (int i = 0; i < n; ++i)
- v[(std::size_t) i] = amp * std::sin (2.0f * 3.14159265f * hz * (float) i / 48000.0f);
- return v;
- };
- auto peak = [] (const std::vector<float>& v, int from) {
- float p = 0.0f;
- for (std::size_t i = (std::size_t) from; i < v.size(); ++i)
- p = std::max (p, std::abs (v[i]));
- return p;
- };
- // limiter: +12 dB on a 0.9 sine never exceeds the ceiling (-1 dB) and reports the latency
- auto lim = lynx::makeEffect (lynx::EffectKind::limiter);
- lim->prepare (sr);
- auto slot = lynx::makeEffectSlot (lynx::EffectKind::limiter);
- slot.params[0] = 11.0f / 12.0f; // -1 dB
- slot.params[1] = 0.5f; // +12 dB
- auto l = sine (100.0f, 0.9f, 48000), r = l;
- lim->process (l.data(), r.data(), 48000, slot.params, 120.0);
- CHECK (peak (l, 0) <= std::pow (10.0f, -1.0f / 20.0f) + 1e-4f);
- CHECK (peak (l, 4800) > 0.8f);
- CHECK (lim->latencySamples() == 72);
- // low-pass filter at 500 Hz knocks down an 8 kHz sine
- auto lp = lynx::makeEffect (lynx::EffectKind::filter);
- lp->prepare (sr);
- auto fs = lynx::makeEffectSlot (lynx::EffectKind::filter);
- fs.params[1] = std::log (500.0f / 20.0f) / std::log (1000.0f);
- l = sine (8000.0f, 1.0f, 9600);
- r = l;
- lp->process (l.data(), r.data(), 9600, fs.params, 120.0);
- CHECK (peak (l, 4800) < 0.05f);
- // distortion and chorus: finite, audible output
- for (auto kind : { lynx::EffectKind::distortion, lynx::EffectKind::chorus })
- {
- auto fx = lynx::makeEffect (kind);
- fx->prepare (sr);
- auto ps = lynx::makeEffectSlot (kind);
- l = sine (220.0f, 0.5f, 9600);
- r = l;
- fx->process (l.data(), r.data(), 9600, ps.params, 120.0);
- bool finite = true;
- for (auto v : l)
- finite = finite && std::isfinite (v);
- CHECK (finite && peak (l, 2400) > 0.1f && peak (l, 0) < 2.0f);
- }
- }
- TEST (slide_notes_glide_without_retrigger)
- {
- // sampler with a ramp: the read speed doubles after a one-octave slide
- auto ramp = std::make_shared<lynx::SampleData>();
- for (int i = 0; i < 96000; ++i)
- ramp->left.push_back ((float) i / 96000.0f);
- ramp->sampleRate = 48000.0;
- auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
- inst->prepare (48000.0);
- inst->noteOn (60, 1.0f);
- std::vector<float> l (2000), r (2000);
- inst->render (l.data(), r.data(), 1000, 1.0f, 1.0f);
- const auto stepBefore = l[999] - l[998];
- inst->slideTo (72, 0.0f, 100.0f);
- std::fill (l.begin(), l.end(), 0.0f);
- inst->render (l.data(), r.data(), 1000, 1.0f, 1.0f);
- const auto stepAfter = l[999] - l[998];
- CHECK (std::abs (stepAfter / stepBefore - 2.0f) < 0.01f);
- // the sequencer sends "slide" (no off) for the slide note
- lynx::Pattern p;
- lynx::Note base { 0, 960, 0, 48 };
- lynx::Note glide { 480, 240, 0, 55 };
- glide.slide = true;
- p.notes = { base, glide };
- lynx::EventBuffer ev;
- const auto tps = lynx::ticksPerSample (120.0, 48000.0);
- lynx::schedulePatternLoop (p, p.effectiveLength (4), 0.0, 30000, tps, ev);
- int ons = 0, offs = 0, slides = 0;
- for (auto& e : ev)
- {
- ons += e.type == lynx::NoteEvent::Type::on;
- offs += e.type == lynx::NoteEvent::Type::off;
- if (e.type == lynx::NoteEvent::Type::slide)
- {
- ++slides;
- CHECK (e.key == 55 && std::abs (e.slideSamples - 240.0f / (float) tps) < 1.0f);
- }
- }
- CHECK (ons == 1 && offs == 1 && slides == 1);
- }
- TEST (synth_fm_phase_modulation)
- {
- auto render = [] (const lynx::SynthSettings& s) {
- auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
- inst->prepare (48000.0);
- lynx::ChannelDef c;
- c.synth = s;
- inst->configure (c, 120.0);
- inst->noteOn (69, 1.0f); // 440 Hz
- std::vector<float> l (48000), r (48000);
- inst->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
- return l;
- };
- // energy (Goertzel) at frequency f over frames [from, to)
- auto energy = [] (const std::vector<float>& x, double f, int from, int to) {
- const auto w = 2.0 * 3.141592653589793 * f / 48000.0, k = 2.0 * std::cos (w);
- double s1 = 0.0, s2 = 0.0;
- for (int i = from; i < to; ++i)
- {
- const auto s0 = x[(std::size_t) i] + k * s1 - s2;
- s2 = s1;
- s1 = s0;
- }
- return s1 * s1 + s2 * s2 - k * s1 * s2;
- };
- lynx::SynthSettings plain;
- plain.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
- plain.osc[2] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
- plain.cutoff = 20000.0f;
- auto fm = plain;
- fm.fm = 1.0f;
- const auto a = render (plain), b = render (fm);
- // no FM: osc 3 adds (same pitch) and there's no 2nd harmonic; with FM the sidebands appear
- CHECK (energy (a, 880.0, 4800, 24000) < 1e-3 * energy (a, 440.0, 4800, 24000));
- CHECK (energy (b, 880.0, 4800, 24000) > 0.1 * energy (b, 440.0, 4800, 24000));
- CHECK (energy (b, 1320.0, 4800, 24000) > 0.01 * energy (b, 440.0, 4800, 24000));
- for (auto v : b)
- CHECK (std::isfinite (v) && std::abs (v) < 1.0f);
- // index decaying to 0: the start is bright, the end is nearly a sine again
- auto decaying = fm;
- decaying.fmDecay = 0.1f;
- decaying.fmSustain = 0.0f;
- const auto c = render (decaying);
- const auto early = energy (c, 880.0, 0, 2400) / energy (c, 440.0, 0, 2400);
- const auto late = energy (c, 880.0, 36000, 48000) / energy (c, 440.0, 36000, 48000);
- CHECK (early > 0.05 && late < 1e-3);
- }
- TEST (synth_three_oscillators_adsr_filter)
- {
- auto render = [] (const lynx::SynthSettings& s, int frames, int releaseAt) {
- auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
- inst->prepare (48000.0);
- lynx::ChannelDef c;
- c.synth = s;
- inst->configure (c, 120.0);
- inst->noteOn (69, 1.0f); // A4 = 440 Hz
- std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
- inst->render (l.data(), r.data(), releaseAt, 1.0f, 1.0f);
- inst->noteOff (69);
- inst->render (l.data() + releaseAt, r.data() + releaseAt, frames - releaseAt, 1.0f, 1.0f);
- return l;
- };
- auto peakIn = [] (const std::vector<float>& v, int from, int to) {
- float p = 0.0f;
- for (int i = from; i < to; ++i)
- p = std::max (p, std::abs (v[(std::size_t) i]));
- return p;
- };
- // default: sounds and fades after the release
- const lynx::SynthSettings standard;
- const auto a = render (standard, 48000, 24000);
- CHECK (peakIn (a, 1000, 20000) > 0.05f && peakIn (a, 40000, 48000) < 1e-3f);
- // pure sine with an open filter: zero-crossing count gives ~440 Hz
- lynx::SynthSettings sine;
- sine.osc[0].wave = lynx::Waveform::sine;
- sine.cutoff = 20000.0f;
- const auto b = render (sine, 48000, 47000);
- int crossings = 0;
- for (int i = 4800; i < 4800 + 48000 / 2; ++i)
- crossings += (b[(std::size_t) i - 1] < 0.0f) != (b[(std::size_t) i] < 0.0f);
- CHECK (std::abs (crossings - 440) < 6); // half a second = 220 cycles = 440 crossings
- float sinePeak = 0.0f;
- for (int i = 4800; i < 9600; ++i)
- sinePeak = std::max (sinePeak, std::abs (b[(std::size_t) i]));
- CHECK (sinePeak > 0.2f && sinePeak < 0.35f); // 0.3 level, approximated sine without clipping
- // second oscillator an octave up adds energy; sustain 0 makes the note decay on its own
- lynx::SynthSettings two = sine;
- two.osc[1] = { lynx::Waveform::sine, 1, 0.0f, 1.0f };
- two.sustain = 0.0f;
- two.decay = 0.1f;
- const auto c = render (two, 48000, 47000);
- CHECK (peakIn (c, 100, 2000) > peakIn (b, 100, 2000) && peakIn (c, 30000, 40000) < 1e-3f);
- }
- TEST (set_target_value_round_trips)
- {
- auto s = lynx::Song::makeDefault();
- s.mixer[2].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::delay));
- const lynx::AutomationTarget targets[] = {
- { lynx::AutomationTargetKind::channelVolume, 1 }, { lynx::AutomationTargetKind::channelPan, 2 },
- { lynx::AutomationTargetKind::mixerVolume, 3 }, { lynx::AutomationTargetKind::mixerPan, 0 },
- { lynx::AutomationTargetKind::effectParam, 2, 0, 1 }, { lynx::AutomationTargetKind::tempo, 0 },
- };
- for (const auto& t : targets)
- {
- CHECK (s.setTargetValue (t, 0.25f));
- CHECK (std::abs (s.targetValue (t) - 0.25f) < 1e-5f);
- }
- CHECK (! s.setTargetValue ({ lynx::AutomationTargetKind::channelParam, 0, 0, 3 }, 0.5f)); // lives in the plugin
- }
- TEST (cut_groups_stretch_and_envelope_import)
- {
- // open hat (cut by 1) is cut when the closed one (group 1) plays
- lynx::Song s;
- s.mixer = lynx::Song::defaultMixer (2);
- for (int i = 0; i < 2; ++i)
- {
- lynx::ChannelDef c;
- c.instrument = lynx::InstrumentKind::sampler;
- c.samplePath = "dc";
- c.volume = 1.0f;
- c.mixerTrack = i + 1;
- s.channels.push_back (c);
- }
- s.channels[0].cutBy = 1; // open
- s.channels[1].cutGroup = 1; // closed
- auto& p = s.patterns.emplace_back();
- p.notes = { { 0, 100, 0, 60 }, { 960, 100, 1, 60 } };
- auto provider = [] (const std::string&) {
- auto d = std::make_shared<lynx::SampleData>();
- d->left.assign (96000, 0.5f);
- return std::shared_ptr<const lynx::SampleData> (d);
- };
- auto state = lynx::RackState::build (s, nullptr, 48000.0, provider);
- CHECK (state->hasCutGroups && state->cutTargets[1].size() == 1 && state->cutTargets[1][0] == 0);
- lynx::RackRenderer r;
- r.prepare (48000.0, 512);
- std::vector<float> l (512), rr (512);
- const auto tps = lynx::ticksPerSample (s.bpm, 48000.0);
- float afterCut = 0.0f;
- for (double tick = 0.0; tick < 960.0 * 3; tick += 512 * tps)
- {
- r.process (state.get(), true, tick, 512, l.data(), rr.data());
- if (tick > 960.0 * 2) // the closed one continues; the open one has stopped
- afterCut = std::max (afterCut, std::abs (l[256]));
- }
- CHECK (std::abs (afterCut - 0.5f * 0.78f) < 0.01f); // only the closed one (default velocity 0.78)
- // resampling stretch: 1 s of sample stretched to 4 beats at 120 BPM = 2 s
- auto ramp = std::make_shared<lynx::SampleData>();
- for (int i = 0; i < 48000; ++i)
- ramp->left.push_back ((float) i / 48000.0f);
- ramp->sampleRate = 48000.0;
- auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
- inst->prepare (48000.0);
- lynx::ChannelDef c;
- c.sampler.stretchBeats = 4.0f;
- inst->configure (c, 120.0);
- inst->noteOn (72, 1.0f); // the note pitch doesn't change the speed
- std::vector<float> a (48000), b (48000);
- inst->render (a.data(), b.data(), 48000, 1.0f, 1.0f);
- CHECK (std::abs (a[47999] - 0.5f) < 0.01f); // after 1 s it has read half
- // FL envelope time: 32950 = 2 s; < 101 = 0
- CHECK (std::abs (flp::Channel::envelopeTime (32950) - 2.0) < 0.01 && flp::Channel::envelopeTime (50) == 0.0);
- }
- TEST (time_stretch_keeps_pitch)
- {
- lynx::SampleData sine;
- sine.sampleRate = 48000.0;
- for (int i = 0; i < 48000; ++i)
- sine.left.push_back (0.5f * std::sin (2.0f * 3.14159265f * 440.0f * (float) i / 48000.0f));
- auto crossingsPerSecond = [] (const lynx::SampleData& d) {
- int c = 0;
- const auto n = (int) d.frames();
- for (int i = 4800; i < n - 4800; ++i)
- c += (d.left[(std::size_t) i - 1] < 0.0f) != (d.left[(std::size_t) i] < 0.0f);
- return (double) c / ((double) (n - 9600) / d.sampleRate);
- };
- for (double ratio : { 2.0, 0.5, 1.37 })
- {
- const auto out = lynx::timeStretch (sine, ratio);
- CHECK (std::abs ((double) out->frames() - 48000.0 * ratio) < 2.0);
- CHECK (std::abs (crossingsPerSecond (*out) - 880.0) < 15.0); // 440 Hz stays 440 Hz
- float peak = 0.0f;
- for (std::size_t i = 4800; i + 4800 < out->frames(); ++i)
- peak = std::max (peak, std::abs (out->left[i]));
- CHECK (peak > 0.4f && peak < 0.6f); // no gaps or clipping in the overlap
- }
- }
- TEST (sampler_region_start_and_end)
- {
- auto ramp = std::make_shared<lynx::SampleData>();
- for (int i = 0; i < 10000; ++i)
- ramp->left.push_back ((float) i / 10000.0f);
- ramp->sampleRate = 48000.0;
- auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
- inst->prepare (48000.0);
- lynx::ChannelDef c;
- c.sampler.regionStart = 0.25f;
- c.sampler.regionEnd = 0.5f;
- inst->configure (c, 120.0);
- inst->noteOn (60, 1.0f);
- std::vector<float> l (6000), r (6000);
- inst->render (l.data(), r.data(), 6000, 1.0f, 1.0f);
- CHECK (std::abs (l[0] - 0.25f) < 0.001f); // starts at the beginning of the slice
- CHECK (l[2400] > 0.48f && l[2600] == 0.0f); // and stops at its end
- }
- TEST (arpeggiator_plays_held_chord_in_steps)
- {
- auto song = lynx::Song::makeDefault();
- song.bpm = 120.0;
- song.patterns[0].notes.clear();
- for (int key : { 60, 64, 67 })
- song.patterns[0].notes.push_back ({ 0, lynx::ppq * 4, 4, key, 0.8f }); // chord held for 1 bar
- song.channels[4].arp.direction = 1; // up
- song.channels[4].arp.stepBeats = 0.25f;
- auto state = lynx::RackState::build (song, nullptr, 48000.0);
- CHECK (state->arpeggiators[4] != nullptr && state->arpeggiators[0] == nullptr);
- // the arpeggiator generates the notes; we count what it hands to the instrument
- auto& arp = *state->arpeggiators[4];
- const auto tps = lynx::ticksPerSample (song.bpm, 48000.0);
- std::vector<int> keys;
- lynx::EventBuffer ev;
- for (double tick = 0.0; tick < lynx::ppq * 2.0; tick += 512 * tps)
- {
- ev.clear();
- lynx::schedulePatternLoop (song.patterns[0], song.patterns[0].effectiveLength (4), tick, 512, tps, ev);
- int in = 0, out = 0;
- for (auto& e : ev)
- if (e.channel == 4)
- arp.input[(std::size_t) in++] = e;
- arp.process (4, arp.input.data(), in, tick, tps, 512, song.channels[4].arp, arp.output.data(), out);
- for (int i = 0; i < out; ++i)
- if (arp.output[(std::size_t) i].type == lynx::NoteEvent::Type::on)
- keys.push_back (arp.output[(std::size_t) i].key);
- }
- // 2 beats = 8 sixteenth steps: 60 64 67 60 64 67 60 64
- CHECK (keys.size() >= 8); // the last block goes slightly past 2 beats
- const int expected[] = { 60, 64, 67, 60, 64, 67, 60, 64 };
- for (std::size_t i = 0; i < keys.size() && i < 8; ++i)
- CHECK (keys[i] == expected[i]);
- // actually rendering also makes sound
- lynx::RackRenderer r;
- r.prepare (48000.0, 512);
- std::vector<float> l (512), rr (512);
- float peak = 0.0f;
- for (double tick = 0.0; tick < lynx::ppq; tick += 512 * tps)
- {
- r.process (state.get(), true, tick, 512, l.data(), rr.data());
- for (auto v : l)
- peak = std::max (peak, std::abs (v));
- }
- CHECK (peak > 0.01f);
- }
- TEST (audio_clip_fades_and_gain)
- {
- lynx::Song s;
- s.bpm = 120.0;
- s.mode = lynx::PlayMode::song;
- s.mixer = lynx::Song::defaultMixer (1);
- lynx::ChannelDef c;
- c.instrument = lynx::InstrumentKind::audioClip;
- c.samplePath = "dc";
- c.volume = 1.0f;
- c.mixerTrack = 1;
- s.channels.push_back (c);
- s.patterns.emplace_back();
- lynx::Clip clip;
- clip.kind = lynx::ClipKind::audio;
- clip.channel = 0;
- clip.length = lynx::ppq * 4; // 2 s at 120 BPM
- clip.fadeIn = lynx::ppq; // 0.5 s
- clip.fadeOut = lynx::ppq;
- clip.gain = 0.5f;
- s.playlist.push_back (clip);
- auto provider = [] (const std::string&) {
- auto d = std::make_shared<lynx::SampleData>();
- d->left.assign (48000 * 4, 1.0f);
- return std::shared_ptr<const lynx::SampleData> (d);
- };
- auto state = lynx::RackState::build (s, nullptr, 48000.0, provider);
- lynx::RackRenderer r;
- r.prepare (48000.0, 512);
- std::vector<float> all;
- std::vector<float> l (512), rr (512);
- const auto tps = lynx::ticksPerSample (s.bpm, 48000.0);
- for (double tick = 0.0; tick < lynx::ppq * 4.0; tick += 512 * tps)
- {
- r.process (state.get(), true, tick, 512, l.data(), rr.data());
- all.insert (all.end(), l.begin(), l.end());
- }
- CHECK (all[100] < 0.01f); // fade-in start
- CHECK (std::abs (all[12000] - 0.25f) < 0.02f); // fade-in midpoint: half of 0.5
- CHECK (std::abs (all[48000] - 0.5f) < 0.01f); // body: the clip gain
- CHECK (std::abs (all[96000 - 12000] - 0.25f) < 0.02f); // fade-out midpoint
- }
- TEST (slicer_detects_hits_and_plays_slices_by_key)
- {
- // 4 hits (decaying noise) every 0.25 s
- lynx::SampleData loop;
- loop.sampleRate = 48000.0;
- loop.left.assign (48000, 0.0f);
- lynx::Noise noise;
- for (int hit = 0; hit < 4; ++hit)
- for (int i = 0; i < 6000; ++i)
- loop.left[(std::size_t) (hit * 12000 + i)] = noise.next() * 0.8f * std::exp (-(float) i / 1500.0f);
- const auto slices = lynx::detectSlices (loop, 0.5f);
- CHECK (slices.size() == 4);
- for (std::size_t i = 0; i < slices.size(); ++i)
- CHECK (std::abs (slices[i] - 0.25f * (float) i) < 0.01f);
- // key 61 plays the second slice, which starts at 0.25 and ends before 0.5
- auto ramp = std::make_shared<lynx::SampleData>();
- for (int i = 0; i < 48000; ++i)
- ramp->left.push_back ((float) i / 48000.0f);
- ramp->sampleRate = 48000.0;
- auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
- inst->prepare (48000.0);
- lynx::ChannelDef c;
- c.sampler.slices = slices;
- inst->configure (c, 120.0);
- inst->noteOn (61, 1.0f);
- std::vector<float> l (20000), r (20000);
- inst->render (l.data(), r.data(), 20000, 1.0f, 1.0f);
- CHECK (std::abs (l[0] - 0.25f) < 0.01f && l[11000] > 0.45f && l[13000] == 0.0f);
- }
- TEST (project_templates_render_sound)
- {
- const auto names = lynx::templateNames();
- CHECK (names.size() == 4);
- for (int t = 1; t < (int) names.size(); ++t)
- {
- auto song = lynx::makeTemplate (t);
- auto state = lynx::RackState::build (song, nullptr, 48000.0);
- lynx::RackRenderer r;
- r.prepare (48000.0, 512);
- std::vector<float> l (512), rr (512);
- const auto tps = lynx::ticksPerSample (song.bpm, 48000.0);
- float peak = 0.0f;
- for (double tick = 0.0; tick < lynx::ppq * 4.0; tick += 512 * tps)
- {
- r.process (state.get(), true, tick, 512, l.data(), rr.data());
- for (auto v : l)
- peak = std::max (peak, std::abs (v));
- }
- CHECK (peak > 0.05f && peak < 4.0f);
- }
- CHECK (lynx::makeTemplate (0).patterns[0].notes.empty());
- }
- // --- interface translations (resources/i18n) -------------------------------------------------------
- namespace i18ntest
- {
- std::string readFile (const std::filesystem::path& path)
- {
- std::ifstream in (path, std::ios::binary);
- return { std::istreambuf_iterator<char> (in), std::istreambuf_iterator<char>() };
- }
- std::string unescape (const std::string& s)
- {
- std::string out;
- for (std::size_t i = 0; i < s.size(); ++i)
- {
- if (s[i] == '\\' && i + 1 < s.size())
- {
- const auto n = s[++i];
- out += n == 'n' ? '\n' : n == 't' ? '\t' : n;
- }
- else
- out += s[i];
- }
- return out;
- }
- // reads a quoted string starting at s[pos] == '"'; returns the raw (escaped) contents and moves pos past the closing quote
- bool quoted (const std::string& s, std::size_t& pos, std::string& raw)
- {
- if (pos >= s.size() || s[pos] != '"')
- return false;
- raw.clear();
- for (++pos; pos < s.size() && s[pos] != '"' && s[pos] != '\n'; ++pos)
- {
- if (s[pos] == '\\' && pos + 1 < s.size())
- raw += s[pos++];
- raw += s[pos];
- }
- if (pos >= s.size() || s[pos] != '"')
- return false;
- ++pos;
- return true;
- }
- // JUCE LocalisedStrings format: "english" = "translation"; duplicates are reported in `duplicates`
- std::map<std::string, std::string> parseTable (const std::string& text, std::vector<std::string>& duplicates)
- {
- std::map<std::string, std::string> table;
- std::istringstream lines (text);
- std::string line;
- while (std::getline (lines, line))
- {
- std::size_t pos = line.find_first_not_of (" \t");
- std::string key, value;
- if (pos == std::string::npos || ! quoted (line, pos, key))
- continue;
- pos = line.find ('"', pos);
- if (pos == std::string::npos || ! quoted (line, pos, value))
- value.clear();
- const auto k = unescape (key);
- if (table.count (k) != 0)
- duplicates.push_back (k);
- table[k] = unescape (value);
- }
- return table;
- }
- // string literals passed to tr() / TR_NOOP() in the sources (adjacent literals are concatenated)
- std::map<std::string, std::string> sourceKeys (const std::filesystem::path& srcDir)
- {
- std::map<std::string, std::string> keys;
- for (const auto& entry : std::filesystem::recursive_directory_iterator (srcDir))
- {
- const auto ext = entry.path().extension().string();
- if (! entry.is_regular_file() || (ext != ".cpp" && ext != ".h"))
- continue;
- // drop // comment lines (they may quote tr ("...") as an example)
- std::string text, line;
- std::istringstream in (readFile (entry.path()));
- while (std::getline (in, line))
- {
- const auto first = line.find_first_not_of (" \t");
- text += (first != std::string::npos && line.compare (first, 2, "//") == 0) ? std::string() : line;
- text += '\n';
- }
- auto isIdent = [] (char c) { return std::isalnum ((unsigned char) c) || c == '_'; };
- for (const std::string name : { "tr", "TR_NOOP" })
- for (auto at = text.find (name); at != std::string::npos; at = text.find (name, at + 1))
- {
- if ((at > 0 && isIdent (text[at - 1])) || isIdent (text[at + name.size()]))
- continue;
- auto pos = text.find_first_not_of (" \t\n", at + name.size());
- if (pos == std::string::npos || text[pos] != '(')
- continue;
- pos = text.find_first_not_of (" \t\n", pos + 1);
- std::string key, raw;
- bool any = false;
- while (pos != std::string::npos && quoted (text, pos, raw))
- {
- key += raw;
- any = true;
- pos = text.find_first_not_of (" \t\n", pos);
- }
- if (any)
- keys.emplace (unescape (key), entry.path().filename().string());
- }
- }
- return keys;
- }
- std::set<std::string> placeholders (const std::string& s)
- {
- std::set<std::string> found;
- for (auto open = s.find ('{'); open != std::string::npos; open = s.find ('{', open + 1))
- if (const auto close = s.find ('}', open); close != std::string::npos && close - open <= 3)
- found.insert (s.substr (open, close - open + 1));
- return found;
- }
- } // namespace i18ntest
- TEST (translations_cover_every_ui_string)
- {
- using namespace i18ntest;
- const auto text = readFile ("resources/i18n/pt-BR.txt");
- CHECK (! text.empty());
- std::vector<std::string> duplicates;
- const auto table = parseTable (text, duplicates);
- CHECK (table.size() > 300);
- CHECK (duplicates.empty());
- for (const auto& d : duplicates)
- std::printf (" duplicate pt-BR entry: \"%s\"\n", d.c_str());
- const auto keys = sourceKeys ("src");
- CHECK (keys.size() > 300);
- int missing = 0;
- for (const auto& [key, file] : keys)
- if (auto it = table.find (key); it == table.end() || it->second.empty())
- {
- ++missing;
- std::printf (" no pt-BR translation for \"%s\" (%s)\n", key.c_str(), file.c_str());
- }
- CHECK (missing == 0);
- // no empty values, and every {n} placeholder survives the translation
- int empty = 0, badPlaceholders = 0;
- for (const auto& [english, portuguese] : table)
- {
- empty += portuguese.empty() ? 1 : 0;
- if (placeholders (english) != placeholders (portuguese))
- {
- ++badPlaceholders;
- std::printf (" placeholders differ: \"%s\"\n", english.c_str());
- }
- }
- CHECK (empty == 0);
- CHECK (badPlaceholders == 0);
- }
- TEST (translations_embedded_table_is_up_to_date)
- {
- // src/ui/I18nData.h must be regenerated (tools/make_i18n.py) whenever the .txt changes
- const auto text = i18ntest::readFile ("resources/i18n/pt-BR.txt");
- CHECK (text.size() == sizeof (lynx::i18n::data::ptBR));
- CHECK (std::memcmp (text.data(), lynx::i18n::data::ptBR, std::min (text.size(), sizeof (lynx::i18n::data::ptBR))) == 0);
- }
- // --- workflow shell: pattern/channel operations, key map, workspace layout, control math -------------
- TEST (workflow_pattern_navigation_and_insert)
- {
- auto song = lynx::Song::makeDefault();
- song.patterns.resize (3);
- song.patterns[1].name = "B";
- song.patterns[2].name = "C";
- song.patterns[2].notes.push_back ({});
- song.currentPattern = 0;
- CHECK (lynx::stepPattern (song, 1) == 1 && lynx::stepPattern (song, -1) == 0 && lynx::stepPattern (song, 10) == 2);
- // pattern 0 has the default notes, 1 is empty, 2 has a note
- CHECK (! lynx::isPatternEmpty (song.patterns[0]) && lynx::isPatternEmpty (song.patterns[1]));
- CHECK (lynx::findNextEmptyPattern (song, 0) == 1 && lynx::findNextEmptyPattern (song, 1) == -1);
- CHECK (lynx::gotoNextEmptyPattern (song) == 1 && song.currentPattern == 1);
- song.patterns[1].notes.push_back ({});
- const auto before = song.patterns.size();
- CHECK (lynx::gotoNextEmptyPattern (song) == (int) before && song.patterns.size() == before + 1);
- CHECK (song.patterns.back().name == "Pattern 1" || song.patterns.back().name.rfind ("Pattern ", 0) == 0);
- // insert keeps Playlist clips on their patterns
- lynx::Clip clip;
- clip.pattern = 2;
- song.playlist = { clip };
- song.arrangements.push_back ({ "Other", { clip }, 0, 0, {} });
- CHECK (lynx::insertPattern (song, 1, "New") == 1 && song.currentPattern == 1);
- CHECK (song.patterns[1].name == "New" && song.patterns[1].notes.empty() && song.patterns[3].name == "C");
- CHECK (song.playlist[0].pattern == 3 && song.arrangements[1].clips[0].pattern == 3);
- }
- TEST (workflow_split_pattern_by_channel)
- {
- auto song = lynx::Song::makeDefault();
- song.patterns.resize (1);
- auto& p = song.patterns[0];
- p.name = "Beat";
- p.notes.clear();
- for (int i = 0; i < 4; ++i)
- p.notes.push_back ({ (std::int64_t) i * lynx::ticksPerStep, lynx::ticksPerStep, i % 2 == 0 ? 0 : 2, 60 });
- song.channels[2].colour = 0xff112233;
- lynx::Clip clip;
- clip.pattern = 0;
- song.playlist = { clip };
- const auto created = lynx::splitPatternByChannel (song, 0);
- CHECK (created == 2 && song.patterns.size() == 3);
- CHECK (song.patterns[0].notes.size() == 4); // the original is unchanged
- CHECK (song.patterns[1].name == "Beat - " + song.channels[0].name && song.patterns[1].notes.size() == 2);
- CHECK (song.patterns[2].colour == 0xff112233u && song.patterns[2].notes.size() == 2 && song.patterns[2].notes[0].channel == 2);
- CHECK (song.playlist[0].pattern == 0);
- CHECK (lynx::splitPatternByChannel (song, 99) == 0);
- }
- TEST (workflow_channel_selection)
- {
- lynx::ChannelSelection s;
- s.selectOnly (2);
- s.toggle (5);
- s.toggle (0);
- CHECK ((s.rows == std::vector<int> { 0, 2, 5 }) && s.contains (5) && ! s.contains (3));
- s.toggle (2);
- CHECK ((s.rows == std::vector<int> { 0, 5 }));
- s.selectOnly (3);
- s.extendTo (6);
- CHECK ((s.rows == std::vector<int> { 3, 4, 5, 6 }));
- s.extendTo (1);
- CHECK ((s.rows == std::vector<int> { 1, 2, 3 }));
- s.selectOnly (4);
- s.toggle (4); // the last one stays
- CHECK ((s.rows == std::vector<int> { 4 }));
- s.selectAll (3);
- CHECK ((s.rows == std::vector<int> { 0, 1, 2 }));
- s.rows = { 7, 9 };
- s.clamp (5);
- CHECK (s.rows.size() == 1 && s.rows[0] < 5);
- }
- TEST (workflow_move_clone_insert_channels)
- {
- auto song = lynx::Song::makeDefault();
- const auto names = [&] {
- std::vector<std::string> n;
- for (const auto& c : song.channels)
- n.push_back (c.name);
- return n;
- };
- const auto original = names();
- const auto count = (int) song.channels.size();
- CHECK (count >= 4);
- // references that must follow their channel
- song.patterns[0].notes.push_back ({ 0, 240, 1, 64 });
- lynx::Automation a;
- a.target = { lynx::AutomationTargetKind::channelVolume, 1 };
- song.automations.push_back (a);
- song.midiLinks.push_back ({ 7, -1, { lynx::AutomationTargetKind::channelPan, 3 } });
- lynx::Clip audio;
- audio.kind = lynx::ClipKind::audio;
- audio.channel = 3;
- song.playlist.push_back (audio);
- std::vector<int> selection { 1, 3 };
- CHECK (lynx::moveChannels (song, selection, -1));
- CHECK ((selection == std::vector<int> { 0, 2 }));
- CHECK (song.channels[0].name == original[1] && song.channels[1].name == original[0] && song.channels[2].name == original[3]
- && song.channels[3].name == original[2]);
- CHECK (song.automations[0].target.index == 0 && song.midiLinks[0].target.index == 2 && song.playlist.back().channel == 2);
- CHECK (std::any_of (song.patterns[0].notes.begin(), song.patterns[0].notes.end(),
- [] (const lynx::Note& n) { return n.channel == 0 && n.key == 64; }));
- CHECK (! lynx::moveChannels (song, selection, -1)); // the block is at the top
- CHECK (lynx::moveChannels (song, selection, 1) && (selection == std::vector<int> { 1, 3 }) && names() == original);
- // clone: settings, not notes, right below, with a new name
- song.channels[1].volume = 0.33f;
- const auto notesBefore = song.patterns[0].notes.size();
- const auto clone = lynx::cloneChannel (song, 1);
- CHECK (clone == 2 && (int) song.channels.size() == count + 1);
- CHECK (song.channels[2].name == original[1] + " #2" && song.channels[2].volume == 0.33f);
- CHECK (song.patterns[0].notes.size() == notesBefore);
- CHECK (song.channels[3].name == original[2] && song.automations[0].target.index == 1);
- CHECK (lynx::cloneChannel (song, 1) == 2 && song.channels[2].name == original[1] + " #3");
- // remove several
- CHECK (lynx::removeChannels (song, { 2, 3, 2 }) == 2 && (int) song.channels.size() == count);
- CHECK (names() == original);
- }
- TEST (workflow_send_channels_to_free_inserts)
- {
- auto song = lynx::Song::makeDefault();
- for (auto& c : song.channels)
- c.mixerTrack = 0;
- song.mixer[1].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::delay)); // not free: has effects
- song.channels[0].mixerTrack = 2; // not free: used
- song.setRoute (5, 3, true); // 3 is not free: receives a send
- CHECK (! lynx::isInsertFree (song, 1) && ! lynx::isInsertFree (song, 2) && ! lynx::isInsertFree (song, 3) && lynx::isInsertFree (song, 4));
- song.channels[1].colour = 0xff445566;
- const auto tracks = lynx::sendChannelsToFreeInserts (song, { 1, 2 });
- CHECK ((tracks == std::vector<int> { 4, 5 }) || (tracks == std::vector<int> { 4, 6 }));
- CHECK (song.channels[1].mixerTrack == 4 && song.mixer[4].name == song.channels[1].name && song.mixer[4].colour == 0xff445566u);
- CHECK (song.mixer[(std::size_t) tracks[1]].name == song.channels[2].name);
- // with no free insert left, a new one is added at the end
- auto full = lynx::Song::makeDefault();
- full.mixer = lynx::Song::defaultMixer (1);
- full.channels[0].mixerTrack = 1;
- const auto added = lynx::sendChannelsToFreeInserts (full, { 1 });
- CHECK (added.size() == 1 && added[0] == 2 && full.mixer.size() == 3 && full.channels[1].mixerTrack == 2);
- }
- TEST (workflow_palette_random_colour)
- {
- const auto& palette = lynx::lynxPalette();
- for (std::uint32_t seed = 0; seed < 50; ++seed)
- {
- const auto current = palette[seed % palette.size()];
- const auto c = lynx::randomPaletteColour (current, seed);
- CHECK (c != current && std::find (palette.begin(), palette.end(), c) != palette.end());
- }
- }
- TEST (workflow_key_map)
- {
- using namespace lynx::keys;
- KeyMap map;
- const auto global = Context::global, rack = Context::channelRack;
- CHECK (map.find ({ space, none }, global, true) == "transport.playStop");
- CHECK (map.find ({ space, ctrl }, global, true) == "transport.pause");
- CHECK (map.find ({ f6, none }, global, true) == "panel.channelRack");
- CHECK (map.find ({ f8, alt }, global, false) == "panel.browser");
- CHECK (map.find ({ insert, ctrl | shift }, global, false) == "pattern.insert");
- CHECK (map.find ({ numpadPlus, none }, rack, true) == "pattern.next");
- // panel context: only while focused
- CHECK (map.find ({ 'L', ctrl }, rack, true) == "rack.sendToInserts");
- CHECK (map.find ({ 'L', ctrl }, global, true).empty());
- CHECK (map.find ({ up, alt }, rack, false) == "rack.moveUp");
- // typing piano: note keys play, other plain keys only for whilePiano actions; chords always work
- CHECK (KeyMap::playsNote ({ 'Z', none }, true) && ! KeyMap::playsNote ({ 'Z', none }, false) && ! KeyMap::playsNote ({ 'Z', ctrl }, true));
- CHECK (map.find ({ 'L', none }, global, true) == "transport.toggleMode");
- CHECK (map.find ({ '1', none }, rack, false) == "rack.mute1" && map.find ({ '0', none }, rack, false) == "rack.mute10");
- CHECK (map.find ({ '1', none }, rack, true).empty()); // a plain digit is not a shortcut while the piano is on
- CHECK (map.find ({ '2', ctrl }, rack, true) == "rack.solo2");
- CHECK (typingPianoNote ('Z') == 60 && typingPianoNote ('q') == 72 && typingPianoNote ('P') == 88 && typingPianoNote ('L') == -1);
- CHECK (map.conflicts().empty());
- // chord text
- CHECK (toString ({ f4, ctrl | shift }) == "Ctrl+Shift+F4" && toString ({ numpadMinus, none }) == "Numpad -");
- CHECK (parseChord ("Shift+Ctrl+Ins") == (Chord { insert, ctrl | shift }));
- CHECK (parseChord ("alt+up") == (Chord { up, alt }) && parseChord ("k") == (Chord { 'K', none }));
- CHECK (parseChord ("Numpad +") == (Chord { numpadPlus, none }) && ! parseChord ("F13") && ! parseChord ("Ctrl+"));
- for (const auto& a : actions())
- for (const auto& c : a.defaults)
- CHECK (parseChord (toString (c)) == c);
- // JSON overrides
- std::vector<std::string> errors;
- CHECK (map.loadJson (R"({ "pattern.next": "Ctrl+Right", "rack.clone": ["Alt+D", "Ctrl+D"], "help.shortcuts": "", "nope": "F1", "file.open": "Hyper+Q" })", &errors));
- CHECK (errors.size() == 2);
- CHECK (map.find ({ right, ctrl }, global, false) == "pattern.next" && map.find ({ numpadPlus, none }, global, false).empty());
- CHECK (map.find ({ 'D', ctrl }, rack, false) == "rack.clone" && map.find ({ f1, none }, global, false).empty());
- map.setChords ("pattern.previous", { { right, ctrl } });
- CHECK (map.conflicts().size() == 1);
- KeyMap reloaded;
- CHECK (reloaded.loadJson (map.toJson()) && reloaded.chordsFor ("rack.clone") == map.chordsFor ("rack.clone"));
- CHECK (! map.loadJson ("[1, 2]") && ! map.loadJson ("{\"a\": }"));
- map.resetToDefaults();
- CHECK (map.find ({ numpadPlus, none }, global, false) == "pattern.next");
- }
- TEST (workflow_workspace_layout)
- {
- using namespace lynx::workspace;
- auto l = Layout::makeDefault (1280, 620);
- // every visible panel is inside the desktop; the default shows Browser, Playlist and Channel Rack
- for (const auto& p : l.panels)
- CHECK (p.bounds.x >= 0 && p.bounds.y >= 0 && p.bounds.right() <= 1280 && p.bounds.bottom() <= 620 && p.bounds.w >= minWidth);
- CHECK (l[Panel::browser].visible && l[Panel::playlist].visible && l[Panel::channelRack].visible);
- CHECK (! l[Panel::pianoRoll].visible && ! l[Panel::mixer].visible);
- CHECK (l[Panel::playlist].bounds.x == l[Panel::browser].bounds.right() && l[Panel::playlist].bounds.right() == 1280);
- CHECK (l.isFrontmost (Panel::channelRack) && l.focused == Panel::channelRack);
- // toggle: show+focus, focus, hide
- CHECK (l.toggle (Panel::mixer) && l.focused == Panel::mixer && l.isFrontmost (Panel::mixer));
- CHECK (l.toggle (Panel::channelRack) && l.focused == Panel::channelRack && l.isFrontmost (Panel::channelRack));
- CHECK (! l.toggle (Panel::channelRack) && ! l[Panel::channelRack].visible && l.focused == Panel::mixer);
- l.toggleMaximised (Panel::playlist);
- CHECK (l[Panel::playlist].maximised && l.focused == Panel::playlist);
- // save and load at another size: positions scale
- l[Panel::mixer].detached = true;
- l[Panel::mixer].windowBounds = { 100, 50, 900, 500 };
- const auto text = l.serialise();
- const auto big = Layout::parse (text, 2560, 1240);
- CHECK (big.has_value());
- if (big)
- {
- CHECK ((*big)[Panel::playlist].maximised && (*big)[Panel::mixer].detached && (*big)[Panel::mixer].windowBounds.w == 900);
- CHECK (std::abs ((*big)[Panel::playlist].bounds.x - 2 * l[Panel::playlist].bounds.x) <= 1);
- CHECK (big->order == l.order && big->focused == l.focused);
- }
- 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));
- // resize keeps panels inside and edges glued to the border
- l.resize (1000, 500);
- CHECK (l[Panel::playlist].bounds.right() == 1000 && l[Panel::browser].bounds.bottom() == 500);
- for (const auto& p : l.panels)
- CHECK (p.bounds.right() <= 1000 && p.bounds.bottom() <= 500);
- // off-screen panels come back
- l[Panel::pianoRoll].bounds = { 5000, -300, 400, 300 };
- CHECK (l.bringBackOffscreen() == 1 && l[Panel::pianoRoll].bounds.x + 400 <= 1000 && l[Panel::pianoRoll].bounds.y == 0);
- // snapping: to the desktop, to another panel, and resizing one edge
- CHECK ((snap ({ 5, 6, 200, 100 }, {}, 1000, 500) == Rect { 0, 0, 200, 100 }));
- CHECK ((snap ({ 304, 50, 200, 100 }, { { 0, 0, 300, 400 } }, 1000, 500) == Rect { 300, 50, 200, 100 }));
- CHECK ((snap ({ 304, 50, 200, 100 }, { { 0, 0, 300, 400 } }, 1000, 500, 2) == Rect { 304, 50, 200, 100 })); // out of range
- CHECK ((snap ({ 100, 100, 195, 100 }, { { 300, 0, 300, 400 } }, 1000, 500, 8, right) == Rect { 100, 100, 200, 100 }));
- CHECK ((snap ({ 100, 100, 195, 100 }, { { 300, 1000, 300, 400 } }, 2000, 2000, 8, right) == Rect { 100, 100, 195, 100 })); // not facing
- }
- TEST (workflow_control_math)
- {
- using namespace lynx::controls;
- CHECK (std::abs (dragValue (0.5, 75, 0, 1, 150, false) - 1.0) < 1e-9);
- CHECK (std::abs (dragValue (0.5, 75, 0, 1, 150, true) - 0.55) < 1e-9);
- CHECK (dragValue (0.5, -1000, -1, 1, 150, false) == -1.0);
- 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);
- CHECK (wheelValue (3, 1, 0, 10, true, 1) == 4.0); // integer controls move at least one step
- CHECK (std::abs (snapToInterval (0.234, 0, 0.1) - 0.2) < 1e-9);
- CHECK (tempoDrag (130.0, 8, false, false) == 132.0 && tempoDrag (130.0, -3, false, false) == 130.0);
- CHECK (std::abs (tempoDrag (130.0, 25, true, false) - 130.25) < 1e-9);
- CHECK (std::abs (tempoDrag (130.0, 8, false, true) - 130.2) < 1e-9);
- CHECK (tempoDrag (520.0, 400, false, false) == lynx::maxTempo && halfTempo (15.0) == lynx::minTempo && doubleTempo (130.0) == 260.0);
- TapTempo tap;
- CHECK (! tap.tap (1000.0));
- CHECK (tap.tap (1500.0) == 120.0);
- CHECK (tap.tap (2000.0) == 120.0 && tap.count() == 3);
- CHECK (std::abs (*tap.tap (2400.0) - 128.57) < 0.01); // average of 500, 500, 400 ms
- CHECK (! tap.tap (9000.0) && tap.count() == 1); // a long pause starts over
- }
- int main (int argc, char** argv)
- {
- if (argc > 1)
- realFlpDir = argv[1];
- const char* only = std::getenv ("LYNX_TEST"); // run only the tests whose name contains this
- for (auto& [name, fn] : registry())
- {
- if (only != nullptr && std::strstr (name, only) == nullptr)
- continue;
- std::printf ("%s\n", name);
- fn();
- }
- std::printf ("\n%d checks, %d failures\n", checks, failures);
- return failures == 0 ? 0 : 1;
- }
- TEST (synth_cutoff_keytrack)
- {
- // the cutoff follows the note from key 48 (130.81 Hz), like Harmor's "Key to filter frequency"
- auto rms = [] (float keyTrack, int key) {
- lynx::SynthSettings s;
- s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
- s.cutoff = 130.81f;
- s.keyTrack = keyTrack;
- auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
- inst->prepare (48000.0);
- lynx::ChannelDef c;
- c.synth = s;
- inst->configure (c, 120.0);
- inst->noteOn (key, 1.0f);
- std::vector<float> l (24000), r (24000);
- inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
- double e = 0.0;
- for (int i = 4800; i < 24000; ++i)
- e += (double) l[(std::size_t) i] * l[(std::size_t) i];
- return std::sqrt (e / 19200.0);
- };
- const auto fixed = rms (0.0f, 72), tracked = rms (1.0f, 72);
- CHECK (tracked > 4.0 * fixed); // 2 octaves up: without keytrack the low-pass cuts
- CHECK (std::abs (rms (1.0f, 48) - rms (0.0f, 48)) < 0.05 * rms (0.0f, 48)); // unchanged at the reference key
- }
- namespace
- {
- std::vector<float> renderSynthNote (const lynx::SynthSettings& s, int key, int frames)
- {
- auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
- inst->prepare (48000.0);
- lynx::ChannelDef c;
- c.synth = s;
- inst->configure (c, 120.0);
- inst->noteOn (key, 1.0f);
- std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
- inst->render (l.data(), r.data(), frames, 1.0f, 1.0f);
- return l;
- }
- double goertzel (const std::vector<float>& x, double f, int from, int to)
- {
- const auto w = 2.0 * 3.141592653589793 * f / 48000.0, k = 2.0 * std::cos (w);
- double s1 = 0.0, s2 = 0.0;
- for (int i = from; i < to; ++i)
- {
- const auto s0 = x[(std::size_t) i] + k * s1 - s2;
- s2 = s1;
- s1 = s0;
- }
- return s1 * s1 + s2 * s2 - k * s1 * s2;
- }
- } // namespace
- TEST (synth_pitch_sweep_ring_and_bands)
- {
- lynx::SynthSettings sine;
- sine.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
- sine.cutoff = 20000.0f;
- // sweep: starts at f·(1 + sweep) and returns to f (time constant s = 10 ms; s⁴ falls 4× faster)
- auto sweep = sine;
- sweep.sweep = 1.0f;
- sweep.sweepTime = 0.01f;
- const auto a = renderSynthNote (sweep, 69, 24000);
- auto crossings = [] (const std::vector<float>& x, int to) {
- int n = 0;
- for (int i = 1; i < to; ++i)
- n += (x[(std::size_t) i - 1] < 0.0f) != (x[(std::size_t) i] < 0.0f);
- return n;
- };
- CHECK (crossings (a, 480) > 1.3 * crossings (renderSynthNote (sine, 69, 480), 480)); // 10 ms: much higher
- CHECK (goertzel (a, 440.0, 12000, 24000) > 100.0 * goertzel (a, 880.0, 12000, 24000));
- // ring with |a| = 1: carrier 440 × modulator 110 -> only 330 and 550, no 440
- auto ring = sine;
- ring.am = ring.ring = true;
- ring.osc[2] = { lynx::Waveform::sine, -2, 0.0f, 1.0f };
- const auto b = renderSynthNote (ring, 69, 24000);
- CHECK (goertzel (b, 330.0, 4800, 24000) > 100.0 * goertzel (b, 440.0, 4800, 24000));
- CHECK (goertzel (b, 550.0, 4800, 24000) > 100.0 * goertzel (b, 440.0, 4800, 24000));
- // 3 bands: muting the high knocks down a 4 kHz note and barely touches an 80 Hz one
- auto bands = sine;
- bands.bandGain = { 1.0f, 1.0f, 0.0f };
- auto rms = [] (const std::vector<float>& x) {
- double e = 0.0;
- for (std::size_t i = 4800; i < x.size(); ++i)
- e += (double) x[i] * x[i];
- return std::sqrt (e / (double) (x.size() - 4800));
- };
- CHECK (rms (renderSynthNote (bands, 107, 24000)) < 0.3 * rms (renderSynthNote (sine, 107, 24000)));
- CHECK (rms (renderSynthNote (bands, 28, 24000)) > 0.85 * rms (renderSynthNote (sine, 28, 24000)));
- }
- TEST (love_philter_map_evaluation)
- {
- lynx::LovePhilterMap line { { { 0.0, 0.0 }, { 1.0, 1.0 } } };
- CHECK (std::abs (lynx::lovePhilterMapAt (line, 0.5) - 0.5) < 1e-9);
- CHECK (std::abs (lynx::lovePhilterMapAt (line, 1.0) - 1.0) < 1e-9);
- // steps (Δx = 0): 1 until 0.4375, ramp to 0 at 0.5, then 0
- lynx::LovePhilterMap steps { { { 0.0, 1.0 }, { 0.4375, 1.0 }, { 0.0625, 0.0 }, { 0.5, 0.0 } } };
- CHECK (lynx::lovePhilterMapAt (steps, 0.27) == 1.0);
- CHECK (lynx::lovePhilterMapAt (steps, 0.75) == 0.0);
- CHECK (std::abs (lynx::lovePhilterMapAt (steps, 0.46875) - 0.5) < 1e-9);
- CHECK (lynx::lovePhilterMapAt ({}, 0.3) == 1.0);
- }
- TEST (lofi_holds_and_quantizes)
- {
- auto fx = lynx::makeEffect (lynx::EffectKind::lofi);
- fx->prepare (48000.0);
- auto slot = lynx::makeEffectSlot (lynx::EffectKind::lofi);
- slot.params[0] = 0.5f; // 128·0.125 + 1 = 17 samples
- slot.params[1] = 1.0f; // 65536/2049 ≈ 32 levels
- std::vector<float> l (170), r (170);
- for (std::size_t i = 0; i < l.size(); ++i)
- l[i] = r[i] = 0.9f * (float) std::sin (0.05 * (double) i);
- const auto original = l;
- fx->process (l.data(), r.data(), 170, slot.params, 120.0);
- for (int i = 0; i < 17; ++i)
- CHECK (l[(std::size_t) i] == l[0]);
- CHECK (l[17] != l[16]);
- const auto step = 2.0f * 2049.0f / 65536.0f;
- for (auto v : l)
- CHECK (std::abs (v / step - std::round (v / step)) < 1e-3f);
- CHECK (std::abs (l[0] - original[0]) <= step);
- CHECK (lynx::formatEffectParam (lynx::EffectKind::lofi, 1, 0.0f, 120.0) == "16.0 bits");
- }
- namespace
- {
- lynx::EffectParams effectorParams (int type, float x, float y)
- {
- auto slot = lynx::makeEffectSlot (lynx::EffectKind::effector);
- slot.params[0] = (float) type / 11.0f;
- slot.params[3] = x;
- slot.params[4] = y;
- return slot.params;
- }
- } // namespace
- TEST (effector_algorithms)
- {
- CHECK (lynx::effector::typeOf (8.0f / 11.0f) == 8);
- CHECK (std::string (lynx::effector::typeName (11)) == "Ring");
- CHECK (std::abs (lynx::effector::rateBeats (0.5f, 0.0f) - 0.75) < 1e-9); // trunc(8·0.5 + 0.5) = 4 -> 3/4
- CHECK (std::abs (lynx::effector::rateBeats (1.0f, 0.5f) - 2.0) < 1e-9); // 8 × (1 − 0.5)²
- auto run = [] (const lynx::EffectParams& p, std::vector<float> in, double bpm = 120.0) {
- auto fx = lynx::makeEffect (lynx::EffectKind::effector);
- fx->prepare (48000.0);
- fx->setSongPosition (0.0);
- auto r = in;
- fx->process (in.data(), r.data(), (int) in.size(), p, bpm);
- return in;
- };
- // Distort with Y = 1 (everything goes to the cascade): 0.5 comes out 0.5 (compensation G = 0.5/F(0.5))
- {
- auto p = effectorParams (0, 0.5f, 1.0f);
- const auto out = run (p, std::vector<float> (4800, 0.5f));
- CHECK (std::abs (out.back() - 0.5f) < 1e-3f);
- const auto small = run (p, std::vector<float> (4800, 0.05f));
- CHECK (small.back() > 0.45f && small.back() < 0.5f); // saturates near ±0.5
- }
- // Ring: DC 1 becomes the carrier sine, 2^(8.7·0 + 5) = 32 Hz -> 32 cycles per second
- {
- auto p = effectorParams (11, 0.0f, 0.0f);
- const auto out = run (p, std::vector<float> (48000, 1.0f));
- int crossings = 0;
- for (std::size_t i = 4800; i < out.size(); ++i)
- crossings += (out[i - 1] < 0.0f) != (out[i] < 0.0f);
- CHECK (crossings >= 56 && crossings <= 60); // 0.9 s × 32 Hz × 2
- }
- // Trancer, Y = 0: at the start of the period the gain drops to zero; X = 1 returns only at the end of the period
- {
- auto p = effectorParams (8, 1.0f, 0.0f);
- p[5] = 1.0f; // 8/1 = 8 beats = 4 s at 120 BPM
- const auto out = run (p, std::vector<float> (48000, 1.0f));
- CHECK (out[100] > 0.3f); // still rising on the "and" (1% of the period = 40 ms)
- CHECK (out[24000] < 0.05f); // 0.5 s: trough
- }
- // Reverb with Wet = 0: the dry passes intact
- {
- auto p = effectorParams (6, 0.5f, 0.5f);
- p[2] = 0.0f;
- std::vector<float> in (4800);
- for (std::size_t i = 0; i < in.size(); ++i)
- in[i] = (float) std::sin (0.01 * (double) i);
- const auto out = run (p, in);
- for (std::size_t i = 0; i < in.size(); ++i)
- CHECK (std::abs (out[i] - in[i]) < 1e-6f);
- }
- // Delay: the echo arrives after the Mod Rate period (1/4 beat at 120 BPM = 125 ms), at level X²
- {
- auto p = effectorParams (5, 0.7f, 0.5f);
- p[5] = 1.0f / 8.0f; // index 1 = 1/4
- std::vector<float> in (12000, 0.0f);
- in[0] = 1.0f;
- const auto out = run (p, in);
- CHECK (std::abs (out[6000] - 0.49f) < 0.02f);
- CHECK (std::abs (out[3000]) < 1e-6f);
- }
- // Stutter: repeats the recorded start every period
- {
- auto p = effectorParams (9, 0.0f, 0.0f);
- p[5] = 0.0f; // 1/8 beat = 62.5 ms = 3000 samples
- std::vector<float> in (9000);
- for (std::size_t i = 0; i < in.size(); ++i)
- in[i] = (float) i / 9000.0f;
- const auto out = run (p, in);
- CHECK (std::abs (out[3000 + 500] - in[500]) < 0.01f); // after the crossfade, it's the start again
- CHECK (std::abs (out[1000] - in[1000]) < 1e-6f);
- }
- }
- TEST (crossover_linkwitz_riley_sums_flat)
- {
- // low band + high band of the same crossover: sum with magnitude 1 (Linkwitz-Riley; for 12 and 36 dB the HP is inverted)
- for (const auto combo : { 1, 3, 4, 5, 6, 7 })
- for (const auto testHz : { 200.0, 1000.0, 5000.0 })
- {
- auto run = [&] (bool high) {
- auto fx = lynx::makeEffect (lynx::EffectKind::crossover);
- fx->prepare (48000.0);
- auto slot = lynx::makeEffectSlot (lynx::EffectKind::crossover);
- const auto value = (float) (std::log2 (1000.0 / 30.0) / 9.0589);
- slot.params[high ? 0 : 3] = 1.0f;
- slot.params[high ? 1 : 4] = value;
- slot.params[high ? 2 : 5] = (float) combo / 7.0f;
- std::vector<float> l (24000), r (24000);
- for (std::size_t i = 0; i < l.size(); ++i)
- l[i] = r[i] = (float) std::sin (2.0 * 3.141592653589793 * testHz * (double) i / 48000.0);
- fx->process (l.data(), r.data(), 24000, slot.params, 120.0);
- return l;
- };
- const auto low = run (false), high = run (true);
- double peak = 0.0;
- for (std::size_t i = 12000; i < low.size(); ++i)
- peak = std::max (peak, (double) std::abs (low[i] + high[i]));
- CHECK (std::abs (peak - 1.0) < 0.02);
- }
- // 96 dB/oct: two octaves above the cutoff the low-pass is already below −150 dB... in practice, very low
- auto fx = lynx::makeEffect (lynx::EffectKind::crossover);
- fx->prepare (48000.0);
- auto slot = lynx::makeEffectSlot (lynx::EffectKind::crossover);
- slot.params[3] = 1.0f;
- slot.params[4] = (float) (std::log2 (500.0 / 30.0) / 9.0589);
- slot.params[5] = 1.0f;
- std::vector<float> l (24000), r (24000);
- for (std::size_t i = 0; i < l.size(); ++i)
- l[i] = r[i] = (float) std::sin (2.0 * 3.141592653589793 * 2000.0 * (double) i / 48000.0);
- fx->process (l.data(), r.data(), 24000, slot.params, 120.0);
- float peak = 0.0f;
- for (std::size_t i = 12000; i < l.size(); ++i)
- peak = std::max (peak, std::abs (l[i]));
- CHECK (peak < 1e-4f);
- }
- TEST (rhythm_gain_and_autopan)
- {
- // 1-beat gate: 1 → 0 at 0.5 beat (linear) → 1 at the end; at 120 BPM 1 beat = 24000 samples at 48 kHz
- auto slot = lynx::makeEffectSlot (lynx::EffectKind::rhythmGain);
- slot.params[0] = 1.0f / 64.0f;
- slot.params[1] = 3.0f / 12.0f;
- const float pts[3][3] = { { 0.0f, 1.0f, 0.5f }, { 0.5f, 0.0f, 0.5f }, { 1.0f, 1.0f, 0.5f } };
- for (int k = 0; k < 3; ++k)
- for (int j = 0; j < 3; ++j)
- slot.params[(std::size_t) (2 + 3 * k + j)] = pts[k][j];
- auto fx = lynx::makeEffect (lynx::EffectKind::rhythmGain);
- fx->prepare (48000.0);
- fx->setSongPosition (960.0 * 2.0); // beat 2: phase 0
- std::vector<float> l (24000, 1.0f), r (24000, 1.0f);
- fx->process (l.data(), r.data(), 24000, slot.params, 120.0);
- CHECK (std::abs (l[0] - 1.0f) < 1e-3f);
- CHECK (std::abs (l[6000] - 0.5f) < 1e-3f);
- CHECK (l[12000] < 1e-3f);
- CHECK (std::abs (l[18000] - 0.5f) < 1e-3f);
- // positive tension: changes quickly at the start of the segment
- CHECK (lynx::effectInfo (lynx::EffectKind::rhythmGain).params.size() == 39);
- // auto-pan: 1-beat period, full depth; start phase 0.75 -> W = 0 rising (pan centered)
- auto pan = lynx::makeEffectSlot (lynx::EffectKind::autoPan);
- pan.params[0] = 1.0f / 64.0f;
- pan.params[1] = 1.0f;
- auto ap = lynx::makeEffect (lynx::EffectKind::autoPan);
- ap->prepare (48000.0);
- ap->setSongPosition (0.0);
- std::vector<float> a (24000, 1.0f), b (24000, 1.0f);
- ap->process (a.data(), b.data(), 24000, pan.params, 120.0);
- CHECK (std::abs (a[0] - 1.0f) < 1e-3f && std::abs (b[0] - 1.0f) < 1e-3f);
- CHECK (a[6000] < 0.01f && std::abs (b[6000] - 1.0f) < 1e-3f); // ¼ cycle later: fully right
- }
- TEST (vocoder_side_inputs_split_modulator_and_carrier)
- {
- // "Side inputs" mode (Vocodex): modulator from side input 1 (noise in the first half), carrier from side input 2
- // (saw), own input silent → the output follows the side modulator; with input 1 silent, the output is silent
- auto slot = lynx::makeEffectSlot (lynx::EffectKind::vocoder);
- slot.params[16] = 1.0f;
- auto fx = lynx::makeEffect (lynx::EffectKind::vocoder);
- fx->prepare (48000.0);
- std::vector<float> modL (48000), car (48000), l (48000, 0.0f), r (48000, 0.0f);
- std::uint32_t seed = 1;
- for (std::size_t i = 0; i < modL.size(); ++i)
- {
- seed = seed * 1664525u + 1013904223u;
- modL[i] = i < 24000 ? ((float) (seed >> 8) / 16777216.0f - 0.5f) : 0.0f;
- car[i] = (float) (2.0 * std::fmod ((double) i * 220.0 / 48000.0, 1.0) - 1.0) * 0.5f;
- }
- for (int done = 0; done < 48000; done += 480)
- {
- fx->setSidechain (modL.data() + done, modL.data() + done);
- fx->setSidechain2 (car.data() + done, car.data() + done);
- fx->process (l.data() + done, r.data() + done, 480, slot.params, 120.0);
- }
- auto rms = [&] (int from, int to) {
- double e = 0.0;
- for (int i = from; i < to; ++i)
- e += (double) l[(std::size_t) i] * l[(std::size_t) i];
- return std::sqrt (e / (to - from));
- };
- CHECK (rms (6000, 20000) > 0.01);
- CHECK (rms (40000, 48000) < 0.1 * rms (6000, 20000));
- // the side modulator configured but silent: nothing comes out (the own input is not used instead)
- std::vector<float> a (4800, 0.3f), b (4800, 0.3f);
- for (int done = 0; done < 4800; done += 480)
- {
- fx->setSidechain (nullptr, nullptr);
- fx->setSidechain2 (car.data() + done, car.data() + done);
- fx->process (a.data() + done, b.data() + done, 480, slot.params, 120.0);
- }
- CHECK (std::abs (a[4700]) < 0.05f);
- }
- TEST (vocoder_follows_modulator)
- {
- // modulator (L) plays only in the first half; carrier (R) is a continuous saw: the wet follows the modulator
- auto slot = lynx::makeEffectSlot (lynx::EffectKind::vocoder);
- auto fx = lynx::makeEffect (lynx::EffectKind::vocoder);
- fx->prepare (48000.0);
- std::vector<float> l (48000), r (48000);
- std::uint32_t seed = 1;
- for (std::size_t i = 0; i < l.size(); ++i)
- {
- seed = seed * 1664525u + 1013904223u;
- l[i] = i < 24000 ? ((float) (seed >> 8) / 16777216.0f - 0.5f) : 0.0f;
- r[i] = (float) (2.0 * std::fmod ((double) i * 220.0 / 48000.0, 1.0) - 1.0) * 0.5f;
- }
- fx->process (l.data(), r.data(), 48000, slot.params, 120.0);
- auto rms = [&] (int from, int to) {
- double e = 0.0;
- for (int i = from; i < to; ++i)
- e += (double) l[(std::size_t) i] * l[(std::size_t) i];
- return std::sqrt (e / (to - from));
- };
- CHECK (rms (6000, 20000) > 0.01);
- CHECK (rms (40000, 48000) < 0.1 * rms (6000, 20000)); // modulator silent: the vocoder fades out (100 ms release)
- for (std::size_t i = 0; i < l.size(); ++i)
- CHECK (l[i] == r[i] && std::isfinite (l[i])); // mono output
- }
- // Vocodex (REA_rodada35): a synthetic state in FL's layout (version 8, 13 maps, 20 parameters, the 0x24 block,
- // 13 map-axis bytes, tone, notes)
- struct VocodexSpec
- {
- // Wet, SG, ModVol, ModHP, CarVol, CarLP, CarNoise, CarNoiseHP, Hold, Att, AttCurve, Rel, RelCurve, Freeze, BW,
- // Flatness, ModBW, ModPitch, FatPitch, FatPan
- 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 };
- std::array<std::int32_t, 7> block { 0, 2, 0, 24, 1, 1, 2 }; // hold mode, order, unison, bands, draft field, ModIn, CarIn
- 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
- std::vector<std::uint8_t> bytes() const
- {
- std::vector<std::uint8_t> d;
- 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); };
- auto i32 = [&] (std::int32_t v) { put (&v, 4); };
- i32 (8 << 16);
- for (int m = 0; m < 13; ++m)
- {
- const double y0 = m == 8 ? 0.1429 : (m == 3 || m == 4 || m == 11) ? 0.0 : 0.5;
- const double y1 = m == 8 ? 0.9918 : y0;
- i32 (1), i32 (0), i32 (2);
- for (const auto& [dx, y] : { std::pair { 0.0, y0 }, std::pair { 1.0, y1 } })
- {
- put (&dx, 8), put (&y, 8);
- i32 (0), i32 (0); // tension (f32 0), flags
- }
- const std::array<std::uint8_t, 20> tail {};
- put (tail.data(), tail.size());
- }
- for (auto v : params)
- i32 (v);
- for (auto v : block)
- i32 (v);
- put (flags.data(), flags.size());
- const std::array<std::uint8_t, 13> axis {};
- put (axis.data(), axis.size());
- d.push_back (3), put ("PWM", 3);
- i32 (1), i32 (60);
- return d;
- }
- };
- std::vector<float> vocodexSaw (int n, double hz, double fs)
- {
- std::vector<float> out ((std::size_t) n);
- for (int i = 0; i < n; ++i)
- out[(std::size_t) i] = (float) (2.0 * std::fmod (i * hz / fs, 1.0) - 1.0) * 0.4f;
- return out;
- }
- TEST (vocodex_state_parse_and_conversion)
- {
- VocodexSpec spec;
- spec.block = { 1, 3, 2, 40, 0, 2, 1 };
- spec.params[17] = -1200;
- const auto raw = spec.bytes();
- const auto s = lynx::vocodex::parseState (raw);
- CHECK (s.has_value());
- if (! s)
- return;
- CHECK (s->version == 8 && s->holdMode == 1 && s->order == 3 && s->unison == 2 && s->bands == 40);
- CHECK (s->draft() && s->modIn == 2 && s->carIn == 1 && s->minTimes && s->subtractiveGate && ! s->lrEncoding);
- CHECK (s->params[lynx::vocodex::modPitch] == -1200 && s->params[lynx::vocodex::carLp] == 65536);
- 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);
- CHECK (s->tone == "PWM" && s->notes.size() == 1 && s->notes[0] == 60);
- const auto v = lynx::vocodexEffect (raw);
- CHECK (v && v->slot.kind == lynx::EffectKind::vocodex && v->slot.state == raw && v->modIn == 2 && v->carIn == 1 && v->notes.empty());
- spec.block[6] = -1; // internal synth carrier: not available yet → reported
- spec.params[1] = 150; // Soundgoodizer stage
- const auto w = lynx::vocodexEffect (spec.bytes());
- CHECK (w && w->carIn == -1 && w->notes.size() == 2);
- CHECK (! lynx::vocodex::parseState ({ 1, 2, 3 }).has_value());
- }
- TEST (vocodex_vocodes_side_inputs)
- {
- // modulator on side input 1 (noise in the first half), carrier on side input 2 (saw), own input silent: the
- // output follows the modulator's envelope and fades after it stops
- VocodexSpec spec;
- lynx::EffectSlot slot = lynx::makeEffectSlot (lynx::EffectKind::vocodex);
- slot.state = spec.bytes();
- auto fx = lynx::makeEffect (slot);
- fx->prepare (48000.0);
- CHECK (fx->latencySamples() == 0);
- const auto car = vocodexSaw (48000, 110.0, 48000.0);
- std::vector<float> mod (48000), l (48000, 0.0f), r (48000, 0.0f);
- std::uint32_t seed = 7;
- for (std::size_t i = 0; i < mod.size(); ++i)
- {
- seed = seed * 1664525u + 1013904223u;
- mod[i] = i < 24000 ? ((float) (seed >> 8) / 16777216.0f - 0.5f) : 0.0f;
- }
- for (int done = 0; done < 48000; done += 512)
- {
- const int n = std::min (512, 48000 - done);
- fx->setSidechain (mod.data() + done, mod.data() + done);
- fx->setSidechain2 (car.data() + done, car.data() + done);
- fx->process (l.data() + done, r.data() + done, n, slot.params, 120.0);
- }
- auto rms = [&] (int from, int to) {
- double e = 0.0;
- for (int i = from; i < to; ++i)
- e += (double) l[(std::size_t) i] * l[(std::size_t) i] + (double) r[(std::size_t) i] * r[(std::size_t) i];
- return std::sqrt (e / (2.0 * (to - from)));
- };
- CHECK (rms (6000, 20000) > 0.02);
- CHECK (rms (40000, 48000) < 0.01 * rms (6000, 20000));
- CHECK (std::abs (l[0]) < 1e-6f); // the gains ramp up from 0 when the plugin is created
- }
- TEST (vocodex_draft_latency_and_pass_through)
- {
- // carrier pass-through at unit gain only (Wet 0): after a Flush the gains snap, so the output is the carrier,
- // 16 samples late in Draft
- VocodexSpec spec;
- spec.params[0] = 0; // Wet
- spec.params[4] = 12800; // CarVol = 1
- for (const int draftField : { 1, 0 })
- {
- spec.block[4] = draftField;
- const auto s = lynx::vocodex::parseState (spec.bytes());
- CHECK (s.has_value());
- lynx::vocodex::Engine engine;
- engine.prepare (*s, 44100.0, 300, s->draft());
- CHECK (engine.latencySamples() == (draftField == 0 ? 16 : 0));
- engine.flush();
- const auto saw = vocodexSaw (3000, 220.0, 44100.0);
- std::vector<float> car (6000), mod (6000, 0.0f), out (6000);
- for (int i = 0; i < 3000; ++i)
- car[(std::size_t) (2 * i)] = car[(std::size_t) (2 * i + 1)] = saw[(std::size_t) i];
- static constexpr int sizes[] { 300, 37, 128, 1, 255, 16, 299 }; // uneven blocks exercise the FIFOs
- for (int done = 0, k = 0, n = 0; done < 3000; done += n, ++k)
- {
- n = std::min (sizes[k % 7], 3000 - done);
- engine.process (mod.data() + 2 * done, mod.data() + 2 * done, car.data() + 2 * done, out.data() + 2 * done, n);
- }
- const int lag = engine.latencySamples();
- auto maxError = 0.0f;
- for (int i = lag; i < 3000; ++i)
- maxError = std::max (maxError, std::abs (out[(std::size_t) (2 * i)] - saw[(std::size_t) (i - lag)]));
- CHECK (maxError == 0.0f);
- }
- }
- TEST (vocodex_soundgoodizer_stage_limits_the_output)
- {
- // SG > 0: the vocoder's output (loud here: Wet 2000) goes through the Soundgoodizer approximation, whose
- // master limiter keeps the peaks under −0.2 dBFS; its look-ahead is reported as latency
- VocodexSpec spec;
- spec.params[0] = 2000;
- spec.params[1] = 250;
- lynx::EffectSlot slot = lynx::makeEffectSlot (lynx::EffectKind::vocodex);
- slot.state = spec.bytes();
- auto fx = lynx::makeEffect (slot);
- fx->prepare (48000.0);
- CHECK (fx->latencySamples() == 72); // 1.5 ms at 48 kHz
- const auto car = vocodexSaw (48000, 110.0, 48000.0);
- std::vector<float> mod (48000), l (48000, 0.0f), r (48000, 0.0f);
- for (std::size_t i = 0; i < mod.size(); ++i)
- mod[i] = 0.9f * (float) std::sin (0.07 * (double) i) * (float) std::sin (0.0003 * (double) i);
- auto peak = 0.0f;
- for (int done = 0; done < 48000; done += 512)
- {
- const int n = std::min (512, 48000 - done);
- fx->setSidechain (mod.data() + done, mod.data() + done);
- fx->setSidechain2 (car.data() + done, car.data() + done);
- fx->process (l.data() + done, r.data() + done, n, slot.params, 120.0);
- }
- for (std::size_t i = 0; i < l.size(); ++i)
- peak = std::max ({ peak, std::abs (l[i]), std::abs (r[i]) });
- CHECK (peak > 0.5f && peak <= 0.9775f);
- }
- TEST (vocodex_tail_bypass_and_noise)
- {
- // both inputs silent for longer than 0.5 s + hold + release: the own input passes unchanged
- VocodexSpec spec;
- const auto s = lynx::vocodex::parseState (spec.bytes());
- CHECK (s.has_value());
- lynx::vocodex::Engine engine;
- engine.prepare (*s, 44100.0, 512, false);
- std::vector<float> zero (1024, 0.0f), own (1024), out (1024);
- for (std::size_t i = 0; i < own.size(); ++i)
- own[i] = 0.25f * (float) std::sin (0.01 * (double) i);
- for (int b = 0; b < 80; ++b)
- engine.process (own.data(), zero.data(), zero.data(), out.data(), 512);
- CHECK (out == own);
- // carrier noise: the IPP-compatible generator restarts from the same seed after a Flush
- spec.params[6] = 9000;
- const auto noisy = lynx::vocodex::parseState (spec.bytes());
- lynx::vocodex::Engine a;
- a.prepare (*noisy, 44100.0, 512, false);
- std::vector<float> mod (1024), first (1024), second (1024);
- for (std::size_t i = 0; i < mod.size(); ++i)
- mod[i] = 0.3f * (float) std::sin (0.05 * (double) i);
- a.flush();
- a.process (own.data(), mod.data(), zero.data(), first.data(), 512);
- a.flush();
- a.process (own.data(), mod.data(), zero.data(), second.data(), 512);
- CHECK (first == second);
- auto energy = 0.0;
- for (auto v : first)
- energy += (double) v * v;
- CHECK (energy > 1e-6 && std::isfinite (energy));
- }
- TEST (synth_spectral_oscillator)
- {
- // 1/n spectrum = saw: same harmonic proportions and same level as LYNX's saw
- lynx::SynthSettings saw;
- saw.osc[0] = { lynx::Waveform::saw, 0, 0.0f, 1.0f };
- saw.cutoff = 20000.0f;
- auto spectral = saw;
- spectral.osc[0].wave = lynx::Waveform::spectral;
- for (int n = 1; n <= lynx::SynthSettings::spectrumSize; ++n)
- spectral.spectrum[(std::size_t) n - 1] = 1.0f / (float) n;
- const auto a = renderSynthNote (saw, 57, 24000), b = renderSynthNote (spectral, 57, 24000); // 220 Hz
- for (const auto h : { 1, 2, 3, 5 })
- {
- const auto ea = goertzel (a, 220.0 * h, 4800, 24000), eb = goertzel (b, 220.0 * h, 4800, 24000);
- CHECK (std::abs (10.0 * std::log10 (eb / ea)) < 0.5); // within 0.5 dB
- }
- // odd only: no 2nd harmonic
- auto odd = spectral;
- for (int n = 2; n <= lynx::SynthSettings::spectrumSize; n += 2)
- odd.spectrum[(std::size_t) n - 1] = 0.0f;
- const auto c = renderSynthNote (odd, 57, 24000);
- CHECK (goertzel (c, 440.0, 4800, 24000) < 1e-4 * goertzel (c, 220.0, 4800, 24000));
- // high note: the band-limited tables don't pass Nyquist (no aliasing below the fundamental)
- const auto d = renderSynthNote (spectral, 105, 24000); // 3520 Hz
- CHECK (goertzel (d, 1000.0, 4800, 24000) < 1e-3 * goertzel (d, 3520.0, 4800, 24000));
- }
- TEST (compressor_sidechain_ducks_and_orders_mixer)
- {
- // the detector listens to the side input: quiet signal on the track, loud kick on the sidechain -> reduction
- auto slot = lynx::makeEffectSlot (lynx::EffectKind::compressor);
- slot.params[0] = 0.5f; // threshold
- slot.params[1] = 1.0f; // high ratio
- slot.params[4] = 0.0f;
- auto run = [&] (bool withSidechain) {
- auto fx = lynx::makeEffect (lynx::EffectKind::compressor);
- fx->prepare (48000.0);
- std::vector<float> l (4800, 0.1f), r (4800, 0.1f), kick (4800, 1.0f);
- if (withSidechain)
- fx->setSidechain (kick.data(), kick.data());
- fx->process (l.data(), r.data(), 4800, slot.params, 120.0);
- return l.back();
- };
- const auto alone = run (false), ducked = run (true);
- CHECK (ducked < 0.5f * alone);
- // order: track 5 listens to 7 via sidechain -> 7 comes before 5
- auto song = lynx::Song::makeDefault();
- while (song.mixer.size() < 8)
- song.mixer.push_back ({});
- auto comp = lynx::makeEffectSlot (lynx::EffectKind::compressor);
- comp.sidechain = 7;
- song.mixer[5].effects.push_back (comp);
- const auto order = song.mixerOrder();
- const auto at = [&] (int t) { return std::find (order.begin(), order.end(), t) - order.begin(); };
- CHECK (at (7) < at (5));
- }
- TEST (pitch_correction_pulls_to_allowed_note)
- {
- auto run = [] (const lynx::EffectParams& p, double hz) {
- auto fx = lynx::makeEffect (lynx::EffectKind::pitchCorrect);
- fx->prepare (48000.0);
- std::vector<float> l (48000 * 2), r;
- for (std::size_t i = 0; i < l.size(); ++i)
- l[i] = 0.5f * (float) std::sin (2.0 * 3.141592653589793 * hz * (double) i / 48000.0);
- r = l;
- fx->process (l.data(), r.data(), (int) l.size(), p, 120.0);
- // frequency from zero crossings in the last second
- int crossings = 0;
- for (std::size_t i = 48000 + 1; i < l.size(); ++i)
- crossings += (l[i - 1] < 0.0f) != (l[i] < 0.0f);
- return crossings / 2.0;
- };
- auto slot = lynx::makeEffectSlot (lynx::EffectKind::pitchCorrect); // chromatic, instant, A 440
- CHECK (std::abs (run (slot.params, 452.0) - 440.0) < 4.0);
- for (int k = 0; k < 12; ++k)
- slot.params[(std::size_t) (5 + k)] = k == 0 ? 1.0f : 0.0f; // only C
- CHECK (std::abs (run (slot.params, 452.0) - 523.25) < 5.0); // C5 (523 Hz) is closer than C4 (262)
- slot.params[2] = 1.0f; // turn everything off: passes unchanged
- CHECK (std::abs (run (slot.params, 452.0) - 452.0) < 4.0);
- }
- TEST (pitch_correction_bypass_is_a_pure_delay)
- {
- // FL's Pitcher with Bypass All still runs its shifter: the plain path is an exact 9-sample delay, the formant
- // path (256-Hann overlap-add) an exact 257-sample delay, and Gender changes nothing without a shift
- // (REA_rodada30 §2.4/2.5); both are reported as latency
- std::vector<float> in (48000);
- for (std::size_t i = 0; i < in.size(); ++i)
- in[i] = 0.5f * (float) std::sin (2.0 * 3.141592653589793 * 440.0 * (double) i / 48000.0)
- + 0.2f * (float) std::sin (2.0 * 3.141592653589793 * 1234.5 * (double) i / 48000.0);
- auto run = [&] (bool formant, float gender, int& latency) {
- auto fx = lynx::makeEffect (lynx::EffectKind::pitchCorrect);
- fx->prepare (48000.0);
- auto slot = lynx::makeEffectSlot (lynx::EffectKind::pitchCorrect);
- slot.params[2] = 1.0f;
- slot.params[29] = formant ? 1.0f : 0.0f;
- slot.params[30] = gender;
- auto l = in, r = in;
- for (std::size_t i = 0; i < l.size(); i += 480)
- fx->process (l.data() + i, r.data() + i, 480, slot.params, 120.0);
- latency = fx->latencySamples();
- return l;
- };
- int latency = 0;
- auto plain = run (false, 0.5f, latency);
- CHECK (latency == 9);
- auto worst = 0.0f;
- for (std::size_t i = 4000; i < in.size(); ++i)
- worst = std::max (worst, std::abs (plain[i] - in[i - 9]));
- CHECK (worst < 1e-5f);
- auto formant = run (true, 0.8f, latency);
- CHECK (latency == 257);
- worst = 0.0f;
- for (std::size_t i = 4000; i < in.size(); ++i)
- worst = std::max (worst, std::abs (formant[i] - in[i - 257]));
- CHECK (worst < 1e-5f);
- }
- TEST (pitch_correction_formant_path)
- {
- // Formant on: PSOLA keeps the correction (output pitch = grain spacing) while Gender resamples the grains:
- // the spectrum of a saw moves up with Gender > 0.5 and down with Gender < 0.5
- auto run = [] (float gender) {
- auto fx = lynx::makeEffect (lynx::EffectKind::pitchCorrect);
- fx->prepare (48000.0);
- auto slot = lynx::makeEffectSlot (lynx::EffectKind::pitchCorrect); // chromatic, instant, A 440
- slot.params[29] = 1.0f;
- slot.params[30] = gender;
- std::vector<float> l (48000 * 2), r;
- for (std::size_t i = 0; i < l.size(); ++i)
- l[i] = 0.5f * (float) (2.0 * std::fmod (452.0 * (double) i / 48000.0, 1.0) - 1.0); // saw
- r = l;
- fx->process (l.data(), r.data(), (int) l.size(), slot.params, 120.0);
- l.erase (l.begin(), l.begin() + 48000);
- return l;
- };
- // period by autocorrelation (lags 60…160 = 300…800 Hz)
- auto pitch = [] (const std::vector<float>& x) {
- auto best = 0.0, bestLag = 0.0;
- for (int lag = 60; lag <= 160; ++lag)
- {
- auto sum = 0.0;
- for (std::size_t i = 0; i + (std::size_t) lag < x.size(); ++i)
- sum += (double) x[i] * x[i + (std::size_t) lag];
- if (sum > best)
- {
- best = sum;
- bestLag = lag;
- }
- }
- return 48000.0 / bestLag;
- };
- // brightness: energy of the first difference relative to the energy
- auto brightness = [] (const std::vector<float>& x) {
- auto e = 0.0, d = 0.0;
- for (std::size_t i = 1; i < x.size(); ++i)
- {
- e += (double) x[i] * x[i];
- d += (double) (x[i] - x[i - 1]) * (x[i] - x[i - 1]);
- }
- return d / e;
- };
- const auto neutral = run (0.5f), up = run (0.8f), down = run (0.3f);
- CHECK (std::abs (pitch (neutral) - 440.0) < 5.0); // 452 Hz pulled to A
- CHECK (std::abs (pitch (up) - 440.0) < 5.0); // Gender does not move the pitch
- CHECK (brightness (up) > 1.5 * brightness (neutral));
- CHECK (brightness (down) < 0.7 * brightness (neutral));
- }
- TEST (parallel_container_inner_links_modulate)
- {
- // one branch: utility (gain) + a control-only auto-pan LFO (1 beat); the link maps the LFO 0..1 onto the
- // utility gain −24 dB..0 dB, so the level must swing; without the link it stays put
- auto container = lynx::makeEffectSlot (lynx::EffectKind::parallel);
- auto gain = lynx::makeEffectSlot (lynx::EffectKind::utility);
- auto lfo = lynx::makeEffectSlot (lynx::EffectKind::autoPan);
- lfo.params[0] = 1.0f / 64.0f;
- lfo.params[4] = 1.0f; // control only: audio passes unchanged
- container.branches.push_back ({ { gain, lfo }, 1.0f });
- lynx::EffectLink link;
- link.source = 1;
- link.param = 0;
- for (int i = 0; i <= 32; ++i)
- link.curve.push_back (0.5f * (float) i / 32.0f);
- auto levels = [&] (bool linked) {
- auto slot = container;
- if (linked)
- slot.links.push_back (link);
- auto fx = lynx::makeParallelEffect (slot);
- fx->prepare (48000.0);
- float lo = 1e9f, hi = 0.0f;
- for (int block = 0; block < 94; ++block) // ~1 beat at 120 BPM in 256-sample blocks
- {
- std::vector<float> l (256, 1.0f), r (256, 1.0f);
- fx->setSongPosition (block * 256.0 / 48000.0 * 2.0 * 960.0); // the engine positions every block
- fx->process (l.data(), r.data(), 256, slot.params, 120.0);
- lo = std::min (lo, std::abs (l[128]));
- hi = std::max (hi, std::abs (l[128]));
- }
- return std::pair { lo, hi };
- };
- const auto [lo, hi] = levels (true);
- CHECK (hi > 0.8f && lo < 0.12f);
- const auto [lo0, hi0] = levels (false);
- CHECK (std::abs (hi0 - lo0) < 1e-4f);
- }
- TEST (synth_spectrum_decays_per_harmonic)
- {
- // 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
- lynx::SynthSettings s;
- s.osc[0] = { lynx::Waveform::spectral, 0, 0.0f, 1.0f };
- s.cutoff = 20000.0f;
- s.decay = 0.0f;
- s.sustain = 1.0f;
- s.spectrum[0] = 1.0f;
- s.spectrum[2] = 1.0f;
- s.spectrumDecay[2] = 0.2f;
- const auto x = renderSynthNote (s, 57, 48000); // 220 Hz, 48 kHz, 1 s
- // equal 2400-sample windows (Goertzel power grows with the window): 10…60 ms and 500…550 ms
- const auto early1 = goertzel (x, 220.0, 480, 2880), early3 = goertzel (x, 660.0, 480, 2880);
- const auto late1 = goertzel (x, 220.0, 24000, 26400), late3 = goertzel (x, 660.0, 24000, 26400);
- CHECK (early3 > 0.05 * early1); // ≈ −9 dB on average over 10…60 ms
- CHECK (late3 < 1e-4 * late1);
- CHECK (std::abs (10.0 * std::log10 (late1 / early1)) < 1.0); // the 1st holds
- // without per-harmonic decay the spectrum stays as it is
- s.spectrumDecay[2] = 0.0f;
- const auto y = renderSynthNote (s, 57, 48000);
- CHECK (std::abs (10.0 * std::log10 (goertzel (y, 660.0, 24000, 26400) / goertzel (y, 660.0, 480, 2880))) < 1.0);
- }
- TEST (worker_pool_runs_each_index_once_per_round)
- {
- // rounds of alternating sizes (a stalled worker must never run an index of a later round), each index
- // exactly once, and nothing running after parallelFor returns
- lynx::WorkerPool pool (3);
- std::vector<std::atomic<int>> hits (64);
- std::atomic<int> round { 0 }, outside { 0 }, wrongRound { 0 };
- std::atomic<bool> open { false };
- int failures = 0;
- for (int r = 1; r <= 20000; ++r)
- {
- const auto n = (r % 3 == 0) ? 64 : (r % 3 == 1 ? 2 : 9);
- for (int i = 0; i < n; ++i)
- hits[(std::size_t) i].store (0);
- round.store (r);
- open.store (true);
- auto fn = [&, r] (int i) {
- if (! open.load())
- outside.fetch_add (1);
- if (round.load() != r)
- wrongRound.fetch_add (1);
- hits[(std::size_t) i].fetch_add (1);
- };
- pool.parallelFor (n, fn);
- open.store (false);
- for (int i = 0; i < n; ++i)
- failures += hits[(std::size_t) i].load() != 1;
- }
- CHECK (failures == 0);
- CHECK (outside.load() == 0);
- CHECK (wrongRound.load() == 0);
- }
- TEST (ladder_filter_follows_toxic_law)
- {
- // the 4-pole ladder: −24 dB/oct far above the cutoff (vs −12 for the SVF), resonance raises the corner, and
- // the input saturation keeps it bounded even at full resonance
- auto level = [] (bool ladder, float res, double hz) {
- lynx::SynthSettings s;
- s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
- s.cutoff = 500.0f;
- s.resonance = res;
- s.ladder = ladder;
- const auto key = (int) std::lround (69.0 + 12.0 * std::log2 (hz / 440.0));
- const auto x = renderSynthNote (s, key, 48000);
- double e = 0.0, peak = 0.0;
- for (std::size_t i = 24000; i < 48000; ++i)
- e += (double) x[i] * x[i], peak = std::max (peak, (double) std::abs (x[i]));
- return std::pair { std::sqrt (e / 24000.0), peak };
- };
- 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
- const auto svfHigh = level (false, 0.0f, 3520.0).first / level (false, 0.0f, 110.0).first;
- CHECK (20.0 * std::log10 (high / low) < -55.0); // ≈ 24 dB/oct over ~2.8 octaves
- CHECK (20.0 * std::log10 (svfHigh) > -45.0); // the 2-pole SVF falls much less
- const auto corner0 = level (true, 0.0f, 523.25).first, corner1 = level (true, 0.6f, 523.25).first;
- CHECK (corner1 > 1.5 * corner0); // resonance boosts near the cutoff
- const auto loud = level (true, 1.0f, 523.25);
- CHECK (std::isfinite (loud.first) && loud.second < 20.0); // self-oscillation stays bounded
- }
- TEST (synth_spectrum_settles_on_target)
- {
- // ALT DECAY-like morph: harmonic 3 starts at 1 and settles on 0.25 (−12 dB) instead of fading out
- lynx::SynthSettings s;
- s.osc[0] = { lynx::Waveform::spectral, 0, 0.0f, 1.0f };
- s.cutoff = 20000.0f;
- s.decay = 0.0f;
- s.sustain = 1.0f;
- s.spectrum[0] = 1.0f;
- s.spectrum[2] = 1.0f;
- s.spectrumDecay[2] = 0.1f;
- s.spectrumTarget[2] = 0.25f;
- const auto x = renderSynthNote (s, 57, 48000);
- const auto ratio = goertzel (x, 660.0, 24000, 26400) / goertzel (x, 220.0, 24000, 26400);
- CHECK (std::abs (10.0 * std::log10 (ratio) - 20.0 * std::log10 (0.25)) < 1.0);
- }
- TEST (kepler_chorus_is_stereo_and_keeps_level)
- {
- // chorus I on a sine: L and R differ (opposite LFO phases), the level stays close to the dry one
- lynx::SynthSettings s;
- s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
- s.cutoff = 20000.0f;
- auto dry = s;
- s.chorus = 1;
- auto render = [] (const lynx::SynthSettings& set) {
- auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
- inst->prepare (48000.0);
- lynx::ChannelDef c;
- c.synth = set;
- inst->configure (c, 120.0);
- inst->noteOn (69, 1.0f);
- std::vector<float> l (96000), r (96000);
- inst->render (l.data(), r.data(), 96000, 1.0f, 1.0f);
- return std::pair { l, r };
- };
- const auto [l, r] = render (s);
- const auto [dl, dr] = render (dry);
- double diff = 0.0, e = 0.0, ed = 0.0;
- for (std::size_t i = 48000; i < 96000; ++i)
- diff += std::abs (l[i] - r[i]), e += (double) l[i] * l[i], ed += (double) dl[i] * dl[i];
- CHECK (diff / 48000.0 > 1e-3);
- CHECK (std::abs (10.0 * std::log10 (e / ed)) < 3.0);
- }
- TEST (fl_pan_law_matches_love_philter_dll)
- {
- // values printed by the installation's Love Philter pan routine for vol 0.8 (REA_rodada27 §3.1, lppan.txt)
- struct Row { double pan, ll, rr, rl, lr; };
- const Row rows[] = { { -1.5, 0.239782482, -0.432956904, 0.805467963, 0.0 }, { -0.5, 0.805467963, 0.432956904, 0.239782482, 0.0 },
- { 0.0, 0.8, 0.8, 0.0, 0.0 }, { 0.75, 0.220719516, 0.707889318, 0.0, 0.401742548 },
- { 1.0, 0.0, 0.565685451, 0.0, 0.565685451 } };
- for (const auto& row : rows)
- {
- float ll, rl, lr, rr;
- lynx::flPanMatrix (row.pan, ll, rl, lr, rr);
- CHECK (std::abs (0.8 * ll - row.ll) < 1e-6 && std::abs (0.8 * rr - row.rr) < 1e-6);
- CHECK (std::abs (0.8 * rl - row.rl) < 1e-6 && std::abs (0.8 * lr - row.lr) < 1e-6);
- }
- }
- TEST (effector_units_match_fl_dll_impulse_responses)
- {
- // impulse responses of the installation's Effector engine (REA_rodada27 §2, eu_<kind>_<X>_<Y>.txt; LFO off,
- // wet 1, blocks of 256): samples 1, 10, 100 and 1000
- struct Case { int kind; float x, y; double v[4]; };
- const Case cases[] = { { 3, 0.5f, 0.5f, { -0.603055418, 0.159798339, -0.0055043716, -1.06241259e-06 } },
- { 4, 0.2f, 0.5f, { 4.68885949e-26, 4.20032622e-19, 0.000487731799, 2.27715846e-09 } },
- { 4, 0.8f, 0.3f, { -0.293783396, -0.0306414738, -0.00068128563, 1.358577e-15 } } };
- for (const auto& c : cases)
- {
- auto fx = lynx::effector::make();
- fx->prepare (44100.0);
- lynx::EffectParams p {};
- p[lynx::effector::type] = c.kind / 11.0f;
- p[lynx::effector::wetMix] = 1.0f;
- p[lynx::effector::paramX] = c.x;
- p[lynx::effector::paramY] = c.y;
- p[lynx::effector::modX] = p[lynx::effector::modY] = 0.5f;
- p[lynx::effector::modRate] = 0.5f;
- p[lynx::effector::inputLevel] = 1.0f;
- p[lynx::effector::outputGain] = 0.5f;
- std::vector<float> l (1024, 0.0f), r (1024, 0.0f);
- l[0] = r[0] = 1.0f;
- for (int d = 0; d < 1024; d += 256)
- fx->process (l.data() + d, r.data() + d, 256, p, 120.0);
- const int at[4] { 1, 10, 100, 1000 };
- for (int k = 0; k < 4; ++k)
- CHECK (std::abs (l[(std::size_t) at[k]] - c.v[k]) < 2e-7 + 1e-5 * std::abs (c.v[k]));
- }
- }
- TEST (harmless_stereo_tremolo_and_vibrato)
- {
- // tremolo d = 1: each side swings between 0 and 1.25 at 5 Hz, L and R in anti-phase; vibrato modulates the pitch
- lynx::SynthSettings s;
- s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
- s.cutoff = 20000.0f;
- s.stereoTremolo = 1.0f;
- auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
- inst->prepare (48000.0);
- lynx::ChannelDef c;
- c.synth = s;
- inst->configure (c, 120.0);
- inst->noteOn (69, 1.0f);
- std::vector<float> l (48000), r (48000);
- inst->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
- 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; };
- // θ = 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
- CHECK (envelope (l, 19200) > 4.0f * envelope (r, 19200));
- CHECK (envelope (r, 24000) > 4.0f * envelope (l, 24000));
- // vibrato 100 cents: zero crossings vary in spacing; without it they are regular
- auto crossingsSpread = [] (float cents) {
- lynx::SynthSettings v;
- v.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
- v.cutoff = 20000.0f;
- v.vibratoCents = cents;
- const auto x = renderSynthNote (v, 69, 48000);
- std::vector<int> zc;
- for (int i = 24000; i < 48000; ++i)
- if (x[(std::size_t) i - 1] < 0.0f && x[(std::size_t) i] >= 0.0f)
- zc.push_back (i);
- int lo = 1 << 30, hi = 0;
- for (std::size_t k = 1; k < zc.size(); ++k)
- lo = std::min (lo, zc[k] - zc[k - 1]), hi = std::max (hi, zc[k] - zc[k - 1]);
- return hi - lo;
- };
- CHECK (crossingsSpread (0.0f) <= 1);
- CHECK (crossingsSpread (100.0f) >= 4);
- }
- TEST (physical_models_sound_and_end)
- {
- // Sakura: a plucked string with default-ish parameters rings and fades after the release; Drumpad: a hit decays
- for (const auto kind : { lynx::InstrumentKind::sakura, lynx::InstrumentKind::drumpad })
- {
- lynx::ChannelDef c;
- c.instrument = kind;
- if (kind == lynx::InstrumentKind::sakura)
- {
- c.modelParams.assign (146, 0.5f);
- c.modelParams[111] = 3.0f; // both strings at the note
- c.modelParams[113] = 0.5f; // octave ×1
- c.modelParams[6] = c.modelParams[11] = 0.99f; // long decay
- c.modelParams[31] = 0.0f; // noise exciter
- }
- else
- {
- c.modelParams.assign (38, 0.5f);
- c.modelParams[0] = 1.0f; // retrigger noise
- c.modelParams[30] = c.modelParams[31] = c.modelParams[32] = c.modelParams[33] = 0.0f; // no velocity morph
- }
- auto inst = lynx::makeInstrument (kind);
- inst->prepare (48000.0);
- inst->configure (c, 120.0);
- inst->noteOn (60, 0.8f);
- std::vector<float> l (48000), r (48000);
- inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
- inst->noteOff (60);
- inst->render (l.data() + 24000, r.data() + 24000, 24000, 1.0f, 1.0f);
- double early = 0.0;
- for (int i = 0; i < 12000; ++i)
- early += (double) l[(std::size_t) i] * l[(std::size_t) i];
- CHECK (early > 1e-6);
- CHECK (std::all_of (l.begin(), l.end(), [] (float v) { return std::isfinite (v); }));
- std::vector<float> tail (480000), tailR (480000);
- inst->render (tail.data(), tailR.data(), 480000, 1.0f, 1.0f); // 10 s later the voice has ended
- CHECK (inst->idle());
- }
- }
- TEST (squeeze_matches_fl_model)
- {
- // Fruity Squeeze: Steps quantises, Mix 0 passes the input dry ×Volume/127; a silent block is left untouched
- auto slot = lynx::makeEffectSlot (lynx::EffectKind::squeeze);
- slot.params[1] = 0.0f; // mix 0: dry only
- auto fx = lynx::makeEffect (lynx::EffectKind::squeeze);
- fx->prepare (44100.0);
- std::vector<float> l (512, 0.3f), r (512, -0.2f);
- fx->process (l.data(), r.data(), 512, slot.params, 120.0);
- CHECK (std::abs (l[100] - 0.3f) < 1e-6f && std::abs (r[100] + 0.2f) < 1e-6f); // Volume 127/127
- slot.params[1] = 1.0f; // wet only
- slot.params[2] = 32000.0f / 32768.0f; // 32768^(1 − 32000/32768) ≈ 1.27 levels per unit → heavy quantisation
- std::vector<float> a (512), b (512);
- for (int i = 0; i < 512; ++i)
- a[(std::size_t) i] = b[(std::size_t) i] = 0.4f * (float) std::sin (i * 0.05);
- fx->process (a.data(), b.data(), 512, slot.params, 120.0);
- const auto levels = std::pow (32768.0f, 1.0f - 32000.0f / 32768.0f);
- for (int i = 0; i < 512; i += 37)
- CHECK (std::abs (a[(std::size_t) i] * levels - std::nearbyint (a[(std::size_t) i] * levels)) < 1e-3f);
- std::vector<float> z (512, 0.0f), z2 (512, 0.0f);
- fx->process (z.data(), z2.data(), 512, slot.params, 120.0);
- CHECK (z[10] == 0.0f);
- }
- TEST (sakura_matrix_and_effects)
- {
- // REA_rodada29 §1: velocity → level follows 10^(2m)·0.05 (vel 1 vs 0.5 → ×10 in amplitude); the delay leaves
- // an echo after the note has ended and keeps the instrument from going idle until its tail is silent
- auto base = [] {
- lynx::ChannelDef c;
- c.instrument = lynx::InstrumentKind::sakura;
- c.modelParams.assign (146, 0.0f);
- const std::pair<int, float> values[] { { 0, 0.375f }, { 1, 0.5f }, { 3, 0.5f }, { 4, 1.0f }, { 6, 0.95f }, { 9, 0.3f },
- { 11, 0.95f }, { 14, 0.3f }, { 18, 0.5f }, { 19, 0.5f }, { 27, 0.5f }, { 28, 1.0f },
- { 29, 0.7f }, { 31, 1.0f }, { 32, 0.5f }, { 111, 3.0f }, { 113, 0.5f } };
- for (const auto& [i, v] : values)
- c.modelParams[(std::size_t) i] = v;
- return c;
- };
- auto render = [] (const lynx::ChannelDef& c, float velocity, int held, int total) {
- auto inst = lynx::makeInstrument (lynx::InstrumentKind::sakura);
- inst->prepare (48000.0);
- inst->configure (c, 120.0);
- inst->noteOn (60, velocity);
- std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
- for (int s = 0; s < total; s += 256)
- {
- if (s == held)
- inst->noteOff (60);
- inst->render (l.data() + s, r.data() + s, std::min (256, total - s), 1.0f, 1.0f);
- }
- return std::make_pair (l, std::move (inst));
- };
- auto energy = [] (const std::vector<float>& x, int from, int to) {
- double e = 0.0;
- for (int i = from; i < to; ++i)
- e += (double) x[(std::size_t) i] * x[(std::size_t) i];
- return e;
- };
- auto c = base();
- c.modelParams[79] = 6.0f; // slot 1: velocity → level, amount 1
- c.modelParams[80] = 1.0f;
- c.modelParams[82] = 1.0f;
- const auto loud = energy (render (c, 1.0f, 24064, 24064).first, 0, 24064);
- const auto soft = energy (render (c, 0.5f, 24064, 24064).first, 0, 24064);
- CHECK (loud > 1e-6 && std::abs (loud / soft - 100.0) < 1.0);
- auto d = base();
- auto [dry, dryInst] = render (d, 0.8f, 4864, 72000);
- d.modelParams[63] = 1.0f; // delay on: time 0.5 → ≈ 0.52 s, wet 1, no feedback: one echo
- d.modelParams[64] = 0.5f;
- d.modelParams[69] = 1.0f;
- auto [wet, wetInst] = render (d, 0.8f, 4864, 72000);
- CHECK (energy (wet, 26000, 40000) > 100.0 * energy (dry, 26000, 40000) + 1e-9); // the note ends near 0.2 s
- CHECK (dryInst->idle() || energy (dry, 70000, 72000) < 1e-12);
- CHECK (! wetInst->idle()); // the echo tail has not been silent for 3 s yet
- }
- TEST (drumsynth_live_engine)
- {
- // REA_rodada30 §1: key → patch slot, state parsing, the engine against our reference model (no DLL: synthesised
- // sources only, noise from the fallback seed), voice modes, patch level/pan
- CHECK (lynx::drumSynthKeyToSlot (48) == 0 && lynx::drumSynthKeyToSlot (71) == 23 && lynx::drumSynthKeyToSlot (0) == 24);
- CHECK (lynx::drumSynthKeyToSlot (47) == 71 && lynx::drumSynthKeyToSlot (72) == 72 && lynx::drumSynthKeyToSlot (119) == 119);
- CHECK (lynx::drumSynthKeyToSlot (120) == -1);
- // a version 3 state: 120 names, 120 × 38 ints (send track −1 becomes 0 below version 4), flags
- std::vector<std::uint8_t> blob;
- auto put = [&] (std::uint32_t v) { for (int k = 0; k < 4; ++k) blob.push_back ((std::uint8_t) (v >> (8 * k))); };
- put (3);
- put (66);
- for (int i = 0; i < 120; ++i)
- blob.push_back (2), blob.push_back ('a'), blob.push_back ((std::uint8_t) ('0' + i % 10));
- for (int i = 0; i < 120 * 38; ++i)
- put ((std::uint32_t) (i % 38 == 36 ? -1 : i % 38 == 4 ? 300 : 0));
- put (0);
- const auto parsed = lynx::drumSynthLiveParams (blob);
- CHECK (parsed && (int) parsed->size() == lynx::drumSynthParamCount);
- CHECK (parsed && (*parsed)[36] == 0.0f && (*parsed)[38 * 7 + 4] == 300.0f && (*parsed)[(std::size_t) lynx::drumSynthEditorKeyIndex] == 66.0f);
- blob.resize (500);
- CHECK (! lynx::drumSynthLiveParams (blob));
- // patch on key 60 (slot 12): tone-mode Osc1 with sweep and click, noise-band Osc2, ring, 2 retriggers
- 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,
- 0, 0, 0, 0, 0, 0, 30, -1, 0, 0, 64 };
- lynx::ChannelDef c;
- c.instrument = lynx::InstrumentKind::drumsynth;
- c.modelParams.assign ((std::size_t) lynx::drumSynthParamCount, 0.0f);
- for (int s = 0; s < 120; ++s)
- c.modelParams[(std::size_t) (38 * s + 15)] = c.modelParams[(std::size_t) (38 * s + 34)] = -1.0f;
- for (int i = 0; i < 38; ++i)
- c.modelParams[(std::size_t) (38 * 12 + i)] = (float) patch[i];
- auto render = [] (const lynx::ChannelDef& def, std::vector<int> keys, int total, float gl = 1.0f, float gr = 1.0f) {
- auto inst = lynx::makeInstrument (lynx::InstrumentKind::drumsynth);
- inst->configure (def, 120.0);
- inst->prepare (48000.0);
- std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
- for (int s = 0; s < total; s += 512)
- {
- for (std::size_t k = 0; k + 1 < keys.size(); k += 2)
- if (keys[k + 1] == s)
- inst->noteOn (keys[k], 100.0f / 128.0f);
- inst->render (l.data() + s, r.data() + s, std::min (512, total - s), gl, gr);
- }
- return std::make_tuple (l, r, std::move (inst));
- };
- {
- auto [l, r, inst] = render (c, { 60, 0 }, 9600);
- // reference model (REA round 30 dsl_model.py, seed 0x56CE, FL velocity 100, V = 1, centre: × g(100)·cos(π/4))
- const std::pair<int, float> golden[] { { 0, -0.0074092713f }, { 10, -0.13961405f }, { 100, -0.47290668f }, { 1000, -0.29188696f },
- { 3000, -0.22130175f }, { 6000, -0.049537566f }, { 9599, 0.010392102f } };
- for (const auto& [i, v] : golden)
- CHECK (std::abs (l[(std::size_t) i] - v) < 2e-6f && std::abs (r[(std::size_t) i] - v) < 2e-6f);
- std::vector<float> tail (48000 * 4), tailR (tail.size());
- inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
- CHECK (inst->idle()); // the engine reported the voice finished
- }
- auto energy = [] (const std::vector<float>& x, int from, int to) {
- double e = 0.0;
- for (int i = from; i < to; ++i)
- e += (double) x[(std::size_t) i] * x[(std::size_t) i];
- return e;
- };
- // a key without a patch level (slot 0, level 0) and a key outside the 120 patches are silent
- CHECK (energy (std::get<0> (render (c, { 48, 0, 125, 0 }, 4096)), 0, 4096) == 0.0);
- // mono: a second hit on the same key cuts the first; poly: both ring together (Osc2 off: no noise, deterministic)
- auto tone = c;
- tone.modelParams[38 * 12 + 14] = 0.0f;
- const auto single = std::get<0> (render (tone, { 60, 0 }, 8192));
- const auto mono = std::get<0> (render (tone, { 60, 0, 60, 2048 }, 8192));
- auto poly = tone;
- poly.modelParams[38 * 12 + 24] = 1.0f;
- const auto both = std::get<0> (render (poly, { 60, 0, 60, 2048 }, 8192));
- double monoDiff = 0.0, polyDiff = 0.0;
- for (int i = 2048 + 300; i < 8192; ++i) // after the cut fade: mono = only the second hit, a copy of the first
- 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]);
- CHECK (monoDiff < 1e-3 && polyDiff > 1.0);
- // patch pan +64 → right only; channel volume counts twice (FL applies Final.Vol in the engine and at the host)
- auto right = c;
- right.modelParams[38 * 12 + 1] = 64.0f;
- const auto [pl, pr, pi] = render (right, { 60, 0 }, 4096);
- CHECK (energy (pl, 0, 4096) < 1e-12 * energy (pr, 0, 4096) && energy (pr, 0, 4096) > 1.0);
- const auto full = std::get<0> (render (tone, { 60, 0 }, 4096));
- const auto half = std::get<0> (render (tone, { 60, 0 }, 4096, 0.5f, 0.5f));
- CHECK (std::abs (half[100] - 0.25f * full[100]) < 1e-6f && std::abs (full[100]) > 0.01f);
- }
- TEST (kepler_lfo_bpm_sync)
- {
- // 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)
- std::vector<std::uint8_t> state (0x21 + 34 * 4, 0);
- auto putU32 = [&] (std::size_t o, std::uint32_t v) { std::memcpy (state.data() + o, &v, 4); };
- putU32 (12, 9);
- std::memcpy (state.data() + 16, "Kepler-1", 9);
- putU32 (0x19, 4);
- putU32 (0x1D, 34);
- float k[34] {};
- k[5] = 0.602085769f; // "Pads - Kepler" demo: n = 286
- k[6] = 1.0f;
- k[10] = 1.0f; // vibrato on
- k[33] = 1.0f;
- std::memcpy (state.data() + 0x21, k, sizeof k);
- lynx::SynthSettings s;
- float gain = 0.0f;
- CHECK (lynx::synthFromKepler (state, s, gain));
- CHECK (s.lfo[lynx::lfoPitch].sync && s.lfo[lynx::lfoPitch].secondsTiming);
- CHECK (std::abs (s.lfo[lynx::lfoPitch].period - 286.0f / 192.0f) < 1e-6f);
- k[5] = 0.0f; // slowest: 32 beats
- std::memcpy (state.data() + 0x21, k, sizeof k);
- CHECK (lynx::synthFromKepler (state, s, gain) && std::abs (s.lfo[lynx::lfoPitch].period - 32.0f) < 1e-6f);
- k[6] = 0.0f; // free rate in seconds
- std::memcpy (state.data() + 0x21, k, sizeof k);
- CHECK (lynx::synthFromKepler (state, s, gain) && ! s.lfo[lynx::lfoPitch].sync);
- }
- namespace
- {
- // A Harmor state written from scratch (REA_rodada11 §4.1 layout, version 18): part A only — a decaying-harmonics
- // timbre, low-pass + resonance, filter envelope, 3-voice unison, vibrato, pluck, tremolo, volume envelope with a
- // velocity map — and the global block. Every other curve is neutral.
- std::vector<std::uint8_t> harmorTestState (bool panLfo = true)
- {
- std::vector<std::uint8_t> b;
- 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))); };
- 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))); };
- struct Pt { double dx, y, tension; int mode; };
- auto curve = [&] (const std::vector<Pt>& pts, std::array<std::int32_t, 5> hdr) {
- i32 (3), i32 (3), i32 ((std::int32_t) pts.size());
- for (const auto& p : pts)
- {
- f64 (p.dx), f64 (p.y);
- const auto t = (float) p.tension;
- std::uint32_t u;
- std::memcpy (&u, &t, 4);
- i32 ((std::int32_t) u);
- b.push_back ((std::uint8_t) p.mode), b.push_back (0), b.push_back (0), b.push_back (0);
- }
- for (auto h : hdr)
- i32 (h);
- };
- const std::array<std::int32_t, 5> off { 0, 0, -1, -1, -1 }, on { 0, 1, -1, -1, -1 };
- 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); };
- i32 (18);
- i32 (1 | 4);
- std::vector<std::uint8_t> part;
- std::swap (b, part);
- std::array<std::int32_t, 386> p {};
- const std::pair<int, int> values[] = { { 0, 500 }, { 3, 1000 }, { 9, 1024 }, { 11, 1000 }, { 12, 1000 }, { 17, 300 }, { 18, 4000 },
- { 19, 500 }, { 20, 1000 }, { 24, 500 }, { 25, 500 }, { 28, 500 }, { 29, 256 }, { 30, -1 }, { 31, -1 }, { 33, -1 }, { 34, 3 },
- { 35, 1 }, { 37, 192 }, { 38, 128 }, { 39, 500 }, { 40, 10000 }, { 41, 1 }, { 42, 10000 }, { 43, 1 }, { 45, 200 }, { 46, 500 },
- { 47, 1 }, { 49, -140 }, { 50, 1200 }, { 51, 0 }, { 52, 5000 }, { 54, 500 }, { 56, 600 }, { 57, 300 }, { 59, 500 }, { 60, 600 },
- { 64, 8192 }, { 66, 500 }, { 72, 600 }, { 74, 1000 }, { 75, 600 }, { 78, 5000 }, { 79, 500 }, { 80, 3000 } };
- for (const auto& [i, v] : values)
- p[(std::size_t) i] = v;
- for (auto v : p)
- i32 (v);
- for (int i = 0; i < 12; ++i)
- i32 (i); // module order
- std::array<std::uint8_t, 0x4b> misc {};
- auto put = [&] (int o, std::int32_t v) { std::memcpy (misc.data() + o, &v, 4); };
- const float one = 1.0f;
- put (0x08, 107), put (0x1c, 1), put (0x28, 200), std::memcpy (misc.data() + 0x30, &one, 4);
- put (0x34, 0), put (0x38, 4), put (0x3c, 100), put (0x40, 108);
- misc[0x44] = 1;
- b.insert (b.end(), misc.begin(), misc.end());
- 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 };
- for (int k = 0; k < 43; ++k)
- {
- static const int mode2[] = { 2, 14, 23, 28, 30, 38, 39, 41, 42 }; // ×2 around 0.5: 1.0 is neutral
- const bool centred = std::find (std::begin (mode1), std::end (mode1), k) != std::end (mode1);
- const bool doubled = std::find (std::begin (mode2), std::end (mode2), k) != std::end (mode2);
- const double neutral = centred && ! doubled ? 0.5 : 1.0;
- i32 (3);
- i32 (1065353216), i32 (1065353216), i32 (1065353216);
- if (k == 1) // volume envelope: attack, decay to the sustain point, release
- 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 });
- else if (k == 4) // filter-1 envelope
- curve ({ { 0, 1, 0, 0 }, { 0.25, 0.2, -0.5, 0 } }, { 0, 1, -1, -1, -1 });
- else
- flat (neutral);
- for (int a : { 128, 128, 0, 128 })
- i32 (a);
- if (k == 0 && panLfo) // pan LFO
- flat (1.0, on);
- else
- flat (0.5);
- for (int a : { k == 0 ? 40000 : 0, 0, 0, 0 })
- i32 (a);
- i32 (0);
- for (int m = 2; m < 11; ++m)
- if (k == 1 && m == 3)
- curve ({ { 0, 0.2, 0, 0 }, { 1, 1, -0.3, 0 } }, off); // velocity → volume
- else
- flat (neutral);
- }
- for (int c = 0; c < 26; ++c)
- if (c == 2 || c == 3)
- curve ({ { 0, 0.25, 0, 0 }, { 0.006, 0.12, -0.46, 0 }, { 0.994, 0, 0, 0 } }, off); // timbres
- else if (c == 14)
- curve ({ { 0, 1, 0, 0 }, { 1, 0, 0, 0 } }, off); // pluck curve
- else
- flat (c == 0 || c == 17 ? 0.25 : (c == 5 ? 0.0 : (c == 6 || c == 10 || c == 11 || (c >= 18 && c <= 24) ? 1.0 : 0.5)));
- std::swap (b, part);
- i32 ((std::int32_t) part.size());
- b.insert (b.end(), part.begin(), part.end());
- std::array<std::int32_t, 53> g {};
- g[0] = g[1] = g[2] = 256, g[10] = 500, g[11] = 1000, g[12] = 1000, g[15] = 1000;
- for (auto v : g)
- i32 (v);
- for (int i = 0; i < 6; ++i)
- i32 (i);
- std::array<std::uint8_t, 0x3d> gm {};
- const std::int32_t cr = 200, vr = 1;
- std::memcpy (gm.data() + 12, &vr, 4);
- std::memcpy (gm.data() + 24, &cr, 4);
- b.insert (b.end(), gm.begin(), gm.end());
- return b;
- }
- } // namespace
- TEST (harmor_engine)
- {
- // REA_rodada34: state parsing, the additive engine against our reference model (hmodel.py), host levels, voice end
- const auto blob = harmorTestState();
- if (const char* dump = std::getenv ("LYNX_DUMP_HARMOR"))
- std::ofstream (dump, std::ios::binary).write ((const char*) blob.data(), (std::streamsize) blob.size());
- CHECK (lynx::harmorStateUsable (blob));
- CHECK (lynx::harmorFallbacks (blob).size() == 1); // (only the harmonic-randomness note)
- const auto at = lynx::harmorParamOffset (blob, 0, 52);
- std::int32_t cutoff = 0;
- CHECK (at > 0 && (std::memcpy (&cutoff, blob.data() + at, 4), cutoff == 5000));
- CHECK (lynx::harmorParamOffset (blob, 1, 52) < 0 && lynx::harmorParamOffset (blob, -1, 12) > at);
- auto broken = blob;
- broken.resize (broken.size() / 2);
- CHECK (! lynx::harmorStateUsable (broken));
- auto old = blob;
- old[0] = 15; // articulators and curves are stored from version 16
- CHECK (! lynx::harmorStateUsable (old));
- lynx::ChannelDef c;
- c.instrument = lynx::InstrumentKind::harmor;
- c.pluginState = blob;
- auto render = [] (const lynx::ChannelDef& def, int total, float gl, float gr, int offAt) {
- auto inst = lynx::makeInstrument (lynx::InstrumentKind::harmor);
- inst->configure (def, 120.0);
- inst->prepare (44100.0);
- std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
- inst->noteOn (60, 0.78125f);
- for (int s = 0; s < total; s += 512)
- {
- if (s == offAt)
- inst->noteOff (60);
- inst->render (l.data() + s, r.data() + s, std::min (512, total - s), gl, gr);
- }
- return std::make_tuple (l, r, std::move (inst));
- };
- {
- // reference model (REA round 34 hmodel.py; C5 at FL velocity 100, Final.Vol 1, centre, released at 6144, the
- // Mersenne Twister seeded like LYNX's)
- auto [l, r, inst] = render (c, 8192, 1.0f, 1.0f, 6144);
- const std::tuple<int, float, float> golden[] {
- { 0, 0.0f, 0.0f }, { 100, 0.00947693f, 0.04249846f }, { 1000, 0.08140575f, -0.03251422f }, { 3000, 0.05928446f, -0.00247572f },
- { 6000, 0.02997839f, 0.14549091f }, { 7000, -0.01121871f, 0.04197952f }, { 8191, 0.00314545f, 0.03857505f } };
- for (const auto& [i, gl, gr] : golden)
- CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
- // after the release the volume envelope ends, the record declicks and the instrument goes idle
- std::vector<float> tail (44100), tailR (tail.size());
- inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
- CHECK (inst->idle());
- CHECK (std::abs (tail.back()) == 0.0f);
- }
- // the reference renderer gives the same samples as the instrument
- {
- const auto ref = lynx::harmor::renderReference (blob, { { 0, 60, 0.78125, 1.0, 0.0, 6144 } }, 44100.0, 8192);
- const auto [l, r, inst] = render (c, 8192, 1.0f, 1.0f, 6144);
- double worst = 0.0;
- for (int i = 0; i < 8192; ++i)
- 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]) });
- CHECK (worst == 0.0);
- }
- // FL's level law applied once: half the channel volume halves the output; full right pan silences the left side
- const auto full = std::get<0> (render (c, 2048, 1.0f, 1.0f, -1));
- const auto half = std::get<0> (render (c, 2048, 0.5f, 0.5f, -1));
- CHECK (std::abs (half[1000] - 0.5f * full[1000]) < 1e-6f && std::abs (full[1000]) > 0.01f);
- auto mono = c;
- mono.pluginState = harmorTestState (false); // no pan LFO ...
- const std::int32_t one = 1;
- std::memcpy (mono.pluginState.data() + lynx::harmorParamOffset (mono.pluginState, 0, 34), &one, 4); // ... nor unison spread
- const auto [pl, pr, pi] = render (mono, 2048, 0.0f, 1.41421356f, -1);
- double el = 0.0, er = 0.0;
- for (int i = 0; i < 2048; ++i)
- el += (double) pl[(std::size_t) i] * pl[(std::size_t) i], er += (double) pr[(std::size_t) i] * pr[(std::size_t) i];
- CHECK (el < 1e-10 * er && er > 1e-3);
- }
- namespace
- {
- // An Ogun state written from scratch (REA_rodada37 §1.3 layout, version 18; zlib with stored blocks): richness 4
- // (255 partials), pre-decay, decay/release, fullness, Seed 1 + fixed Seed 2 with morph, 3-voice unison, a 2-stage
- // low-pass with an LFO on its cutoff, a volume envelope with a sustain point and a velocity map, declick, normalisation,
- // Impulse/Decay maps, and every plugin effect (EQ band, chorus, ping-pong delay, reverb). Everything else is neutral.
- std::vector<std::uint8_t> ogunTestState()
- {
- std::vector<std::uint8_t> b;
- 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))); };
- auto f32v = [&] (float v) { std::uint32_t u; std::memcpy (&u, &v, 4); i32 ((std::int32_t) u); };
- 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))); };
- struct Pt { double dx, y, tension; int mode; };
- auto curve = [&] (int type, const std::vector<Pt>& pts, std::array<std::int32_t, 5> hdr) {
- i32 (type), i32 (3), i32 ((std::int32_t) pts.size());
- for (const auto& p : pts)
- {
- f64 (p.dx), f64 (p.y), f32v ((float) p.tension);
- b.push_back ((std::uint8_t) p.mode), b.push_back (0), b.push_back (0), b.push_back (0);
- }
- for (auto h : hdr)
- i32 (h);
- };
- const std::array<std::int32_t, 5> off { 0, 0, -1, -1, -1 }, on { 0, 1, -1, -1, -1 };
- auto flat = [&] (int type, double y) { curve (type, { { 0, y, 0, 0 }, { 1, y, 0, 0 } }, off); };
- std::array<std::int32_t, 64> P {};
- const std::pair<int, int> values[] = { { 0, 128 }, { 5, 300 }, { 6, 3000 }, { 7, 2000 }, { 8, 200 }, { 9, 500 }, { 10, 12 },
- { 11, 34 }, { 12, 3 }, { 13, 64 }, { 14, 64 }, { 15, 40 }, { 16, 64 }, { 17, 180 }, { 18, 60 }, { 19, 2 }, { 20, 300 },
- { 21, 400 }, { 22, 40 }, { 23, 30 }, { 24, 20 }, { 25, 64 }, { 26, 40 }, { 27, 96 }, { 28, 100 }, { 29, 64 }, { 30, 2 },
- { 31, 19 }, { 32, 221 }, { 33, 0 }, { 34, 50 }, { 35, 100 }, { 36, 20 }, { 37, 60 }, { 38, 2 }, { 39, 64 }, { 40, 300 },
- { 43, 30000 }, { 46, 20000 }, { 49, 0 }, { 50, 2 }, { 51, 0 }, { 52, 9 }, { 53, 1 }, { 54, 0 },
- { 55, 0x01000000 }, // +0x20b: pitch follows the key
- { 56, 0x00010000 }, // +0x20e: declick
- { 62, 0x00000100 } }; // +0x225: normalise
- for (const auto& [i, v] : values)
- P[(std::size_t) i] = v;
- for (auto v : P)
- i32 (v);
- b.push_back (0), b.push_back (1), b.push_back (0); // delay: not tempo-based, on
- i32 (1), i32 (1), i32 (1); // EQ band types: peaking
- b.push_back (0); // comment: 0 characters
- for (int k = 0; k < 13; ++k)
- {
- static constexpr int sum[] = { 1, 2, 3, 9, 11, 12 };
- const double neutral = std::find (std::begin (sum), std::end (sum), k) != std::end (sum) ? 0.5 : 1.0;
- i32 (1);
- f32v (0.0f), f32v (1.0f), f32v (k == 1 ? 0.3f : 0.0f);
- if (k == 0) // volume envelope: attack, decay to the sustain point, release
- 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 });
- else
- flat (1, neutral);
- for (int a : { 128, 128, 128, 128 })
- i32 (a);
- if (k == 1) // cutoff LFO
- curve (2, { { 0, 0.8, 0, 0 } }, on);
- else
- flat (2, 0.5);
- for (int a : { k == 1 ? 40000 : 0, 0, 0, 0 })
- i32 (a);
- i32 (0);
- for (int m = 0; m < 4; ++m)
- if (k == 0 && m == 1)
- curve (3, { { 0, 0.3, 0, 0 }, { 1, 1, -0.2, 0 } }, off); // velocity → volume
- else
- flat (3, neutral);
- }
- flat (3, 0.0); // Forced: none
- curve (3, { { 0, 0.6, 0, 0 }, { 1, 0.4, 0.3, 0 } }, off); // Impulse EQ
- curve (3, { { 0, 0.5, 0, 0 }, { 1, 0.3, 0, 0 } }, off); // Decay EQ
- flat (3, 0.5); // Release EQ
- // u32 version + zlib (stored blocks)
- std::vector<std::uint8_t> s { 18, 0, 0, 0, 0x78, 0x01 };
- for (std::size_t i = 0; i < b.size();)
- {
- const auto len = std::min<std::size_t> (65535, b.size() - i);
- s.push_back (i + len >= b.size() ? 1 : 0);
- s.push_back ((std::uint8_t) len), s.push_back ((std::uint8_t) (len >> 8));
- s.push_back ((std::uint8_t) ~len), s.push_back ((std::uint8_t) (~len >> 8));
- s.insert (s.end(), b.begin() + (std::ptrdiff_t) i, b.begin() + (std::ptrdiff_t) (i + len));
- i += len;
- }
- std::uint32_t a1 = 1, a2 = 0;
- for (auto c : b)
- a1 = (a1 + c) % 65521u, a2 = (a2 + a1) % 65521u;
- for (int sh = 24; sh >= 0; sh -= 8)
- s.push_back ((std::uint8_t) (((a2 << 16) | a1) >> sh));
- return s;
- }
- } // namespace
- TEST (ogun_engine)
- {
- // REA_rodada37: state parsing, the engine against our reference model (ogmodel.py), host levels, export quality
- const auto blob = ogunTestState();
- if (const char* dump = std::getenv ("LYNX_DUMP_OGUN"))
- std::ofstream (dump, std::ios::binary).write ((const char*) blob.data(), (std::streamsize) blob.size());
- CHECK (lynx::ogunStateUsable (blob));
- CHECK (lynx::ogunFallbacks (blob).empty());
- CHECK (lynx::ogunParam (blob, 17) == 180 && lynx::ogunParam (blob, 52) == 9 && ! lynx::ogunParam (blob, 64));
- auto broken = blob;
- broken.resize (broken.size() - 300);
- CHECK (! lynx::ogunStateUsable (broken));
- auto old = blob;
- old[0] = 13; // the articulator layout is from version 14
- CHECK (! lynx::ogunStateUsable (old));
- // reference model (REA round 37 ogmodel.Plugin(state).run: C5 velocity 0.75 released at 6144, G5 velocity 0.625 at 2048
- // released at 8192; host block 512)
- 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 } };
- const auto ref = lynx::ogun::renderReference (blob, notes, 44100.0, 16384);
- CHECK (ref.size() == 2 * 16384);
- if (const char* dump = std::getenv ("LYNX_DUMP_OGUN_OUT"))
- std::ofstream (dump, std::ios::binary).write ((const char*) ref.data(), (std::streamsize) (ref.size() * 4));
- const std::tuple<int, float, float> golden[] {
- { 100, -7.763202e-05f, -8.919523e-05f }, { 1000, 0.06195267f, 0.03858230f }, { 3000, 0.01363476f, -0.00378433f },
- { 6000, -0.06601223f, -0.03084030f }, { 9000, 0.04204506f, 0.02452896f }, { 12000, 0.03420147f, -0.01837870f },
- { 16383, -0.00465311f, -0.00119627f } };
- for (const auto& [i, gl, gr] : golden)
- CHECK (std::abs (ref[2 * (std::size_t) i] - gl) < 2e-6f && std::abs (ref[2 * (std::size_t) i + 1] - gr) < 2e-6f);
- // the instrument plays like the reference renderer (Final.Vol 1, centre)
- lynx::ChannelDef c;
- c.instrument = lynx::InstrumentKind::ogun;
- c.pluginState = blob;
- auto render = [&] (const lynx::ChannelDef& def, int total, float gl, float gr, bool held = false) {
- auto inst = lynx::makeInstrument (lynx::InstrumentKind::ogun);
- inst->configure (def, 120.0);
- inst->prepare (44100.0);
- std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
- for (int s = 0; s < total; s += 512)
- {
- if (s == 0)
- inst->noteOn (60, 0.75f);
- if (s == 2048)
- inst->noteOn (67, 0.625f);
- if (s == 6144 && ! held)
- inst->noteOff (60);
- if (s == 8192 && ! held)
- inst->noteOff (67);
- inst->render (l.data() + s, r.data() + s, std::min (512, total - s), gl, gr);
- }
- return std::make_tuple (l, r, std::move (inst));
- };
- {
- auto [l, r, inst] = render (c, 16384, 1.0f, 1.0f);
- double worst = 0.0;
- for (int i = 0; i < 16384; ++i)
- 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]) });
- CHECK (worst == 0.0);
- // the voices end and the effect tails die out: the instrument goes idle
- std::vector<float> tail (44100), tailR (tail.size());
- for (int k = 0; k < 20 && ! inst->idle(); ++k)
- inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
- CHECK (inst->idle());
- }
- // FL's level law applied once (effects off): half the channel volume halves the output once the gain ramps
- // (limited to 0.001 per sample) have settled on the sustain level; full right pan silences the left side
- {
- auto dry = c;
- for (int i : { 19, 29, 40, 53 })
- CHECK (lynx::ogunSetParam (dry.pluginState, i, 0)); // chorus, delay wet, EQ band and reverb off
- CHECK (lynx::ogunStateUsable (dry.pluginState) && lynx::ogunParam (dry.pluginState, 19) == 0);
- const auto full = std::get<0> (render (dry, 32768, 1.0f, 1.0f, true));
- const auto half = std::get<0> (render (dry, 32768, 0.5f, 0.5f, true));
- double worst = 0.0, peak = 0.0;
- for (int i = 30000; i < 32768; ++i)
- worst = std::max (worst, (double) std::abs (half[(std::size_t) i] - 0.5f * full[(std::size_t) i])),
- peak = std::max (peak, (double) std::abs (full[(std::size_t) i]));
- CHECK (worst <= 1e-7 * peak && peak > 1e-3);
- // FL's default channel volume (100/128): FL hands Ogun Final.Vol = (11^v − 1)/10 (REA_rodada28 §4.1); LYNX's
- // centred channel gains (√2·V·cos(π/4) each) give the reference model's level at that Final.Vol
- const auto V = lynx::channelGain (0.78125f);
- const auto g = V * std::cos (0.25f * 3.14159265f) * 1.41421356f;
- const auto atDefault = std::get<0> (render (dry, 32768, g, g, true));
- const auto refDefault = lynx::ogun::renderReference (dry.pluginState, { { 0, 60, 0.75, V, 0.0, 0.0, 0.0, 0.0, 0.5, -1 },
- { 2048, 67, 0.625, V, 0.0, 0.0, 0.0, 0.0, 0.5, -1 } }, 44100.0, 32768);
- double ei = 0.0, er0 = 0.0;
- for (int i = 30000; i < 32768; ++i)
- 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];
- CHECK (er0 > 0.0 && std::abs (10.0 * std::log10 (ei / er0)) < 0.001);
- const auto [pl, pr, pi] = render (dry, 4096, 0.0f, 1.41421356f);
- double el = 0.0, er = 0.0;
- for (int i = 0; i < 4096; ++i)
- el += (double) pl[(std::size_t) i] * pl[(std::size_t) i], er += (double) pr[(std::size_t) i] * pr[(std::size_t) i];
- CHECK (el < 1e-10 * er && er > 1e-3);
- }
- // export: FL renders Ogun at quality 4 (×2 rate, 32-point reader), LYNX too while it renders offline
- {
- lynx::setOfflineRendering (true);
- auto [l, r, inst] = render (c, 8192, 1.0f, 1.0f);
- lynx::setOfflineRendering (false);
- lynx::ogun::RefOptions q4;
- q4.quality = 4;
- const auto ref4 = lynx::ogun::renderReference (blob, notes, 44100.0, 8192, q4);
- double worst = 0.0, diff = 0.0;
- for (int i = 0; i < 8192; ++i)
- worst = std::max (worst, (double) std::abs (ref4[2 * (std::size_t) i] - l[(std::size_t) i])),
- diff = std::max (diff, (double) std::abs (ref[2 * (std::size_t) i] - l[(std::size_t) i]));
- CHECK (worst == 0.0 && diff > 1e-3);
- const std::tuple<int, float, float> golden4[] {
- { 100, 0.00605961f, 0.00628969f }, { 1000, 0.03729841f, 0.01727954f }, { 3000, -0.09010383f, -0.02551294f },
- { 6000, 0.02167798f, -0.01115632f }, { 8191, 0.00320288f, -0.00615792f } };
- for (const auto& [i, gl, gr] : golden4)
- CHECK (std::abs (ref4[2 * (std::size_t) i] - gl) < 2e-6f && std::abs (ref4[2 * (std::size_t) i + 1] - gr) < 2e-6f);
- }
- // feature variants of the same state against the reference model (energy per side over 8192 samples, three
- // samples; C1 released at 6144): qualities, richness, unison, the filter types, ALT DECAY, 1/√h, Seed 2 Random,
- // infinite decay, logarithmic axis, velocity → position, other rates. Goldens from ogmodel.py (REA round 37).
- {
- struct Sample { int at; float l, r; };
- struct Variant { const char* name; std::vector<std::pair<int, int>> params; int rate; double el, er; Sample s[3]; };
- const Variant variants[] {
- { "q1", { { 54, 1 } }, 44100, 11.0970402, 6.87820714, { { 1500, 0.0240363069f, -0.0148524037f }, { 4000, 0.0873190612f, -0.000715861097f }, { 7000, -0.0378603041f, -0.00338819111f } } },
- { "q2", { { 54, 2 } }, 44100, 11.2405446, 6.68045082, { { 1500, 0.0448660068f, -0.00560745923f }, { 4000, -0.0122657465f, 0.0104285963f }, { 7000, -0.0216034129f, -0.0134632047f } } },
- { "q3", { { 54, 3 } }, 44100, 11.3200608, 6.86196449, { { 1500, -0.0031537537f, -0.0242648814f }, { 4000, 0.0426301025f, 0.0364697985f }, { 7000, 0.00843803957f, 0.0302239582f } } },
- { "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 } } },
- { "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 } } },
- { "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 } } },
- { "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 } } },
- { "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 } } },
- { "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 } } },
- { "alt", { { 62, 257 } }, 44100, 17.2485711, 9.99223979, { { 1500, -0.0319156498f, -0.0186850764f }, { 4000, -0.0769012421f, -0.0375530645f }, { 7000, 0.0310976505f, 0.0445244014f } } },
- { "isqrt", { { 62, 65792 } }, 44100, 45.7309307, 27.1224593, { { 1500, -0.04376591f, -0.00845711026f }, { 4000, 0.0351300463f, -0.129557073f }, { 7000, 0.0096148327f, 0.00123740174f } } },
- { "seed2rnd", { { 11, 0 } }, 44100, 16.8228819, 11.5433061, { { 1500, -0.0117614279f, -0.026497297f }, { 4000, 0.00265733898f, -0.07069581f }, { 7000, 0.00576663949f, 0.0565217957f } } },
- { "decinf", { { 6, 10000 } }, 44100, 19.1879226, 11.0487311, { { 1500, -0.0324105881f, -0.0190765262f }, { 4000, -0.0809780285f, -0.0395730771f }, { 7000, 0.034197174f, 0.048585102f } } },
- { "logx", { { 57, 16777216 } }, 44100, 12.3064326, 7.31519289, { { 1500, -0.0438990779f, 0.0096983267f }, { 4000, -0.0175748877f, 0.0313935578f }, { 7000, 0.0639200211f, 0.024419222f } } },
- { "velpos", { { 56, 66304 } }, 44100, 4.9966925, 3.16540662, { { 1500, 0.0083851777f, -0.0165472236f }, { 4000, 0.0630451962f, 0.0156494901f }, { 7000, -0.0293043777f, -0.00182877108f } } },
- { "fs48", { }, 48000, 10.8813888, 6.53592373, { { 1500, -0.0485184453f, -0.0319588967f }, { 4000, 0.0230421089f, -0.0286604818f }, { 7000, -0.0117630213f, -0.017459048f } } },
- { "fs96", { }, 96000, 11.0189683, 6.39821883, { { 1500, -0.0267860368f, -0.0227564126f }, { 4000, 0.0238709711f, -0.0100849615f }, { 7000, -0.0034619756f, 0.0451687202f } } },
- };
- for (const auto& v : variants)
- {
- auto st = blob;
- for (const auto& [i, x] : v.params)
- CHECK (lynx::ogunSetParam (st, i, x));
- const auto y = lynx::ogun::renderReference (st, { { 0, 60, 0.75, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 6144 },
- { 2048, 67, 0.625, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1 } }, (double) v.rate, 8192);
- double el = 0.0, er = 0.0, peak = 0.0;
- for (std::size_t i = 0; i + 1 < y.size(); i += 2)
- 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]) });
- bool ok = y.size() == 2 * 8192 && std::abs (el - v.el) <= 1e-5 * v.el && std::abs (er - v.er) <= 1e-5 * v.er;
- for (const auto& smp : v.s)
- 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;
- if (! ok)
- std::printf (" Ogun variant %s differs from the model: energy %.9g %.9g (model %.9g %.9g)\n", v.name, el, er, v.el, v.er);
- CHECK (ok);
- }
- }
- }
- // our own Maximus v7 state (layout REA_rodada8 §1.1): 7 globals, 4 × 13 band parameters, 8 envelopes (dx, y, tension, type)
- static std::vector<std::uint8_t> maximusTestState (const std::array<int, 7>& g, const std::array<std::array<int, 13>, 4>& bands,
- const std::array<std::vector<std::array<double, 4>>, 8>& envs, int oversampling)
- {
- std::vector<std::uint8_t> d;
- auto put = [&] (const void* v, std::size_t n) { d.insert (d.end(), (const std::uint8_t*) v, (const std::uint8_t*) v + n); };
- auto i32 = [&] (std::int32_t v) { put (&v, 4); };
- i32 (7);
- for (int copy = 0; copy < 2; ++copy)
- {
- for (auto v : g)
- i32 (v);
- for (const auto& b : bands)
- for (auto v : b)
- i32 (v);
- }
- d.resize (0x1fe, 0);
- std::memcpy (d.data() + 0x1f4, &oversampling, 4);
- for (const auto& e : envs)
- {
- i32 (3), i32 (2), i32 ((std::int32_t) e.size());
- for (const auto& pt : e)
- {
- const double dx = pt[0], y = pt[1];
- const float t = (float) pt[2];
- put (&dx, 8), put (&y, 8), put (&t, 4);
- const std::uint8_t flags[4] { (std::uint8_t) pt[3], 0, 0, 0 };
- put (flags, 4);
- }
- i32 (0), i32 (-1), i32 (-1), i32 (-1), i32 (-1);
- }
- return d;
- }
- // A "Kepler-1" state written from scratch (REA_rodada46 §1.1 layout, version 2): the default panel values
- std::vector<std::uint8_t> keplerTestState()
- {
- 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 };
- std::vector<std::uint8_t> st;
- auto u32 = [&] (std::uint32_t v) { for (int i = 0; i < 4; ++i) st.push_back ((std::uint8_t) (v >> (8 * i))); };
- auto f32 = [&] (float f) { std::uint32_t v; std::memcpy (&v, &f, 4); u32 (v); };
- u32 (0), u32 (0), u32 (0), u32 (9);
- for (char c : std::string ("Kepler-1"))
- st.push_back ((std::uint8_t) c);
- st.push_back (0);
- u32 (2), u32 (34);
- for (auto f : panel)
- f32 (f);
- st.push_back (0);
- f32 (1.0f), f32 (0.5f);
- const auto size = (std::uint32_t) st.size() - 12;
- std::memcpy (st.data() + 4, &size, 4);
- return st;
- }
- std::string keplerDll()
- {
- if (realFlpDir.empty())
- return {};
- return (std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "Kepler" / "engine_x64.dll").string();
- }
- TEST (kepler_engine)
- {
- const auto st = keplerTestState();
- CHECK (st.size() == 178 && lynx::keplerStateUsable (st));
- CHECK (lynx::keplerPanelOffset (st, 19) == 0x21 + 4 * 19 && lynx::keplerPanelOffset (st, 34) == -1);
- auto broken = st;
- broken[17] = 'X'; // not "Kepler-1"
- CHECK (! lynx::keplerStateUsable (broken) && lynx::keplerPanelOffset (broken, 0) == -1);
- auto v3 = st;
- v3[0x19] = 3; // version 3 needs the gate / noise / oversampling bytes
- CHECK (! lynx::keplerStateUsable (v3));
- const auto dll = keplerDll();
- if (dll.empty() || ! lynx::keplerDllUsable (dll))
- return; // no FL installation (the engine needs the user's own DLL tables)
- // against FL's engine DLL (EXEC, REA_rodada46 §6): three notes, 44.1 kHz, host blocks of 512 — bit-exact
- 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 } };
- const auto y = lynx::kepler::renderReference (st, dll, notes, 44100.0, 39690);
- CHECK (y.size() == 2 * 39690);
- if (y.size() != 2 * 39690)
- return;
- double el = 0.0, er = 0.0;
- for (std::size_t i = 0; i < y.size(); i += 2)
- el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
- std::printf (" Kepler default patch: energy %.9g %.9g (FL's DLL 250.641268)\n", el, er);
- CHECK (std::abs (el - 250.641268) < 1e-6 && std::abs (er - 250.641268) < 1e-6);
- const std::tuple<int, float, float> golden[] { { 5000, 0.0467273332f, 0.0467273332f }, { 12000, -0.119412214f, -0.119412214f },
- { 25000, 0.0855825394f, 0.0855825394f }, { 35000, -3.34166579e-08f, -3.34166579e-08f } };
- for (const auto& [i, l, r] : golden)
- CHECK (y[2 * (std::size_t) i] == l && y[2 * (std::size_t) i + 1] == r);
- // the instrument: FL's wrapper velocity, channel volume as an output gain, idle after the release
- lynx::ChannelDef c;
- c.instrument = lynx::InstrumentKind::kepler;
- c.pluginState = st;
- c.samplePath = dll;
- auto inst = lynx::makeInstrument (lynx::InstrumentKind::kepler);
- inst->configure (c, 120.0);
- inst->prepare (44100.0);
- std::vector<float> l (4096), r (4096);
- inst->noteOn (60, 0.8f);
- double e = 0.0;
- bool finite = true;
- for (int b = 0; b < 8; ++b)
- {
- std::fill (l.begin(), l.end(), 0.0f);
- std::fill (r.begin(), r.end(), 0.0f);
- inst->render (l.data(), r.data(), 4096, 0.5f, 0.5f);
- for (int i = 0; i < 4096; ++i)
- 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];
- if (b == 3)
- inst->noteOff (60);
- }
- CHECK (finite && e > 1.0);
- for (int b = 0; b < 40; ++b)
- inst->render (l.data(), r.data(), 4096, 0.5f, 0.5f);
- CHECK (inst->idle());
- }
- TEST (kepler_demo_channels)
- {
- // every Kepler channel of FL's demo projects on LYNX's engine against FL's engine DLL (EXEC fingerprints, REA_rodada46 §6;
- // the engine differs from the DLL by −127 dB or less on these, from the DLL library's pow rounding)
- const auto dll = keplerDll();
- if (dll.empty() || ! lynx::keplerDllUsable (dll))
- return;
- struct Sample { int at; float l, r; };
- struct Golden { const char* file; const char* channel; std::size_t stateSize; double el, er; Sample s[4]; };
- const Golden goldens[] {
- { "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 } } },
- { "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 } } },
- { "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 } } },
- { "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 } } },
- };
- int checked = 0;
- for (const auto& g : goldens)
- {
- std::ifstream in (std::filesystem::path (realFlpDir) / "Demo projects" / g.file, std::ios::binary);
- const std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
- if (bytes.empty())
- continue;
- const flp::Channel* ch = nullptr;
- const auto project = flp::parseProject (bytes);
- for (const auto& c : project.channels)
- if (c.pluginName == "Kepler" && c.name == g.channel)
- ch = &c;
- CHECK (ch != nullptr);
- if (ch == nullptr)
- continue;
- CHECK (ch->pluginState.size() == g.stateSize && lynx::keplerStateUsable (ch->pluginState));
- 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 },
- { 16000, 55, 0.95, 0.0, 0.0, 30000 } }, 44100.0, 39690);
- double el = 0.0, er = 0.0, peak = 0.0;
- for (std::size_t i = 0; i + 1 < y.size(); i += 2)
- 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]) });
- bool ok = y.size() == 2 * 39690 && std::abs (el - g.el) <= 1e-5 * g.el && std::abs (er - g.er) <= 1e-5 * g.er;
- for (const auto& smp : g.s)
- 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;
- if (! ok)
- 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);
- CHECK (ok);
- ++checked;
- }
- std::printf (" Kepler: %d demo channels checked against FL's engine DLL\n", checked);
- CHECK (checked == (int) std::size (goldens));
- }
- TEST (ogun_demo_channels)
- {
- // every Ogun channel of FL's demo projects (with its plugin effects) against the round-37 reference model, which is
- // 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),
- // 39 690 samples at 44.1 kHz, at the state's quality and at the export quality 4. Fingerprint: energy per side and
- // four samples (the engine matches the model within float rounding, −125 dB or better on the whole signal).
- if (realFlpDir.empty())
- return;
- struct Sample { int at; float l, r; };
- struct Golden { const char* file; int ordinal; std::size_t stateSize; bool export4; double el, er; Sample s[4]; };
- const Golden goldens[] {
- { "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
- { "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
- { "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
- { "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
- { "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
- { "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
- { "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
- { "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
- { "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
- { "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
- { "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
- { "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
- { "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
- { "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
- { "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
- { "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
- { "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
- { "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
- { "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
- { "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
- { "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
- { "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
- { "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
- { "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
- { "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
- { "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
- { "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
- { "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
- { "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
- { "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
- { "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
- { "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
- { "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
- { "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
- { "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
- { "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
- { "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
- { "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
- { "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
- { "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
- };
- std::map<std::string, std::vector<std::vector<std::uint8_t>>> states;
- int checked = 0;
- for (const auto& g : goldens)
- {
- if (! states.contains (g.file))
- {
- std::ifstream in (std::filesystem::path (realFlpDir) / "Demo projects" / g.file, std::ios::binary);
- const std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
- auto& list = states[g.file];
- if (! bytes.empty())
- for (const auto& c : flp::parseProject (bytes).channels)
- if (c.pluginName == "Ogun")
- list.push_back (c.pluginState);
- }
- const auto& list = states[g.file];
- if (list.empty())
- continue; // not this installation
- CHECK ((int) list.size() > g.ordinal);
- if ((int) list.size() <= g.ordinal)
- continue;
- const auto& st = list[(std::size_t) g.ordinal];
- CHECK (st.size() == g.stateSize);
- lynx::ogun::RefOptions opt;
- opt.quality = g.export4 ? 4 : -1;
- const auto y = lynx::ogun::renderReference (st, { { 0, 60, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 20000 },
- { 8000, 67, 0.6, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 26000 },
- { 16000, 55, 0.95, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 30000 } }, 44100.0, 39690, opt);
- double el = 0.0, er = 0.0, peak = 0.0;
- for (std::size_t i = 0; i + 1 < y.size(); i += 2)
- 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]) });
- bool ok = y.size() == 2 * 39690 && std::abs (el - g.el) <= 1e-5 * g.el && std::abs (er - g.er) <= 1e-5 * g.er;
- for (const auto& smp : g.s)
- 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;
- if (! ok)
- std::printf (" Ogun %s #%d (q%s) differs from the model: energy %.9g %.9g (model %.9g %.9g)\n", g.file, g.ordinal,
- g.export4 ? "4" : "-state", el, er, g.el, g.er);
- CHECK (ok);
- ++checked;
- }
- std::printf (" Ogun: %d demo renders checked against the reference model\n", checked);
- CHECK (checked == (int) std::size (goldens));
- }
- TEST (harmless_rack)
- {
- // REA_rodada36: Harmless's effects rack against our reference model (rack.py / maxmodel.py, which match the
- // installation's Harmless_x64.dll and SG_x64.dll), 44.1 kHz, host blocks of 512
- std::vector<float> inL (8192), inR (8192);
- for (int i = 0; i < 4096; ++i)
- {
- 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));
- inR[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / 44100.0));
- }
- auto run = [&] (const std::array<int, 100>& p, int compType, std::shared_ptr<const lynx::hlfx::MaximusPreset> comp, int total, int voicesUntil) {
- lynx::hlfx::Rack rack;
- std::array<int, 7> t {};
- t[3] = compType;
- rack.prepare (p, t, 44100.0, 120.0, std::move (comp));
- std::vector<float> l (inL.begin(), inL.begin() + total), r (inR.begin(), inR.begin() + total);
- for (int s = 0; s < total; s += 512)
- rack.process (l.data() + s, r.data() + s, std::min (512, total - s), s < voicesUntil);
- return std::make_pair (l, r);
- };
- {
- // chorus type 3 (mix 400), ping-pong delay type 2 (vol 600, feedback 500, 1/4 beat), reverb type 4 (vol 700)
- std::array<int, 100> p {};
- p[90] = 3, p[91] = 400, p[92] = 2, p[93] = 600, p[94] = 500, p[95] = 48, p[96] = 4, p[97] = 700;
- const auto [l, r] = run (p, 0, nullptr, 8192, 4096);
- const std::tuple<int, float, float> golden[] { { 0, 0.0f, 0.0f }, { 500, -0.25248247f, 0.15141529f }, { 3000, -0.65128994f, -0.64854866f },
- { 5000, -0.54308629f, -0.36827448f }, { 6000, -0.98378551f, -0.72790027f },
- { 8191, 0.50599128f, -0.43637198f } };
- for (const auto& [i, gl, gr] : golden)
- CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
- }
- {
- // the delay alone: an impulse comes back after round(time·fs·0.3125/bpm) samples × (11^0.6 − 1)/10, then × feedback
- std::array<int, 100> p {};
- p[92] = 1, p[93] = 600, p[94] = 500, p[95] = 48;
- lynx::hlfx::Rack rack;
- rack.prepare (p, {}, 44100.0, 120.0, nullptr);
- std::vector<float> l (16384), r (16384);
- l[0] = 1.0f;
- for (int s = 0; s < 16384; s += 512)
- rack.process (l.data() + s, r.data() + s, 512, s == 0);
- const auto D = 5512; // 48·44100·0.3125/120 = 5512.5 → 5512 (round half to even)
- const auto vol = (float) ((std::exp ((double) (600 * 0.001f) * (double) (float) std::log (11.0)) - 1.0) * (double) 0.1f);
- CHECK (std::abs (l[(std::size_t) D] - vol) < 1e-7f && std::abs (l[(std::size_t) D - 1]) < 1e-12f);
- // the feedback ramps from 0.5 toward 0.5: the second echo is vol·0.5
- CHECK (std::abs (l[(std::size_t) (2 * D)] - vol * 0.5f) < 1e-7f && std::abs (r[(std::size_t) D]) < 1e-12f);
- }
- // the compressor: Maximus states of our own (no preset data), checked against SG_x64.dll by REA round 36
- using Env = std::vector<std::array<double, 4>>;
- const Env def { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 } };
- 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,
- int sus = 2882) { return std::array<int, 13> { mode, pre, post, sep, sat, ceil, att, rel2, 2, 5000, 3, sus, 0 }; };
- {
- // master brickwall at −0.22 dB only (bands off / compression off)
- const auto st = maximusTestState ({ 19800, 1, 48065, 1, 1115, 1000, -1 }, { band (3), band (1), band (3), band (0) },
- { def, def, def, def, def, def, def, def }, 0);
- const auto preset = lynx::hlfx::makeMaximusPreset (st);
- CHECK (preset != nullptr);
- std::array<int, 100> p {};
- p[98] = 1000;
- const auto [l, r] = run (p, 1, preset, 4096, 4096);
- // SG_x64.dll's own output (EXEC, REA_rodada40: bit-identical to the port)
- const std::tuple<int, float, float> golden[] { { 100, 0.024997942f, 0.88166064f }, { 1000, 0.56971401f, -0.62808102f },
- { 2047, -0.27541628f, 0.709135f }, { 3000, 0.32431623f, -0.37985197f },
- { 4095, -0.12743522f, -0.15237485f } };
- for (const auto& [i, gl, gr] : golden)
- CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
- float peak = 0.0f;
- for (int i = 2048; i < 4096; ++i)
- peak = std::max ({ peak, std::abs (l[(std::size_t) i]), std::abs (r[(std::size_t) i]) });
- CHECK (peak < 0.9751f);
- }
- {
- // 24 dB crossovers, ×2 oversampling, low cut, stereo separation, saturation A, a knee with tension (type 7) and
- // exponential tensions (type 0), release 2 on the master, band mix 600
- const Env knee { { 0.0, 0.0, 0.0, 0 }, { 0.35, 0.35, 0.0, 0 }, { 0.65, 0.6, 0.36, 7 } };
- const Env t0 { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.14, 0 }, { 0.5, 0.5, 0.0, 0 } };
- const Env m0 { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 }, { 0.5, 0.6, 0.085, 0 } };
- const auto st = maximusTestState ({ 19800, 1, 48065, 1, 891, 600, 32768 },
- { band (1, 1264, 1296, 500, -500, 1200), band (0, 1340, 1000, -348, 0, 1000, 1115, 0, 1570),
- band (0, 1364, 1152, 0, 0, 1000, 1115, 0, 1186), band (0, 1000, 1000, 0, -196, 1000, 1115, 4464, 1538) },
- { def, def, knee, def, t0, def, m0, def }, 1);
- const auto preset = lynx::hlfx::makeMaximusPreset (st);
- CHECK (preset != nullptr);
- std::array<int, 100> p {};
- p[98] = 600;
- const auto [l, r] = run (p, 2, preset, 4096, 4096);
- // SG_x64.dll's own output (EXEC, REA_rodada40; the port is within −142 dB of it)
- const std::tuple<int, float, float> golden[] { { 100, 0.0f, 0.0f }, { 1000, -0.46856543f, -0.63262129f },
- { 2047, 0.20268451f, 0.54145539f }, { 3000, -0.25613472f, -0.49985155f },
- { 4095, 0.10172445f, 0.050564945f } };
- for (const auto& [i, gl, gr] : golden)
- CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
- }
- {
- // an .fst wrapper around a state is found; unsupported states are rejected
- const auto st = maximusTestState ({ 19800, 1, 48065, 1, 1115, 1000, -1 }, { band (3), band (1), band (3), band (0) },
- { def, def, def, def, def, def, def, def }, 0);
- std::vector<std::uint8_t> fst { 'F', 'L', 'h', 'd', 6, 0, 0, 0, 0, 0, 1, 0, 0x60, 0, 'F', 'L', 'd', 't' };
- std::vector<std::uint8_t> body { 0xC9, 8, 'M', 'a', 'x', 'i', 'm', 'u', 's', 0, 0xD5 };
- for (auto n = st.size(); ; n >>= 7)
- {
- body.push_back ((std::uint8_t) ((n & 0x7f) | (n >= 0x80 ? 0x80 : 0)));
- if (n < 0x80)
- break;
- }
- body.insert (body.end(), st.begin(), st.end());
- const auto len = (std::uint32_t) body.size();
- for (int k = 0; k < 4; ++k)
- fst.push_back ((std::uint8_t) (len >> (8 * k)));
- fst.insert (fst.end(), body.begin(), body.end());
- const auto found = lynx::hlfx::maximusStateFromFst (fst);
- CHECK (found && *found == st);
- auto rms = st;
- rms[4 + 4 * (7 + 13 * 3 + 12)] = 1; // RMS detector on the master (decoded in REA_rodada40)
- CHECK (lynx::hlfx::makeMaximusPreset (rms) != nullptr);
- auto masterMid = st;
- masterMid[0x1fc] = 1; // "Master Mid mode": not decoded
- CHECK (lynx::hlfx::makeMaximusPreset (masterMid) == nullptr);
- }
- }
- TEST (maximus_engine)
- {
- // REA_rodada40: the engine on its own (MaximusProcessor), states of our own against the installation's DLLs
- // (goldens = the DLL's output for the same input, EXEC), and the containers of the mixer effect and Vocodex
- std::vector<float> inL (4096), inR (4096);
- for (int i = 0; i < 4096; ++i)
- {
- 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));
- inR[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / 44100.0));
- }
- auto run = [&] (std::shared_ptr<const lynx::hlfx::MaximusPreset> preset, int mix) {
- lynx::hlfx::MaximusProcessor m;
- m.prepare (std::move (preset), 44100.0, mix);
- std::vector<float> l (inL), r (inR);
- for (int s = 0; s < 4096; s += 512)
- m.process (l.data() + s, r.data() + s, 512);
- return std::make_pair (l, r);
- };
- using Env = std::vector<std::array<double, 4>>;
- const Env def { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 } };
- const Env knee { { 0.0, 0.0, 0.0, 0 }, { 0.35, 0.35, 0.0, 0 }, { 0.65, 0.6, 0.36, 7 } };
- const Env t0 { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.14, 0 }, { 0.5, 0.5, 0.0, 0 } };
- const Env m0 { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 }, { 0.5, 0.6, 0.085, 0 } };
- 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,
- int sus = 2882, int rms = 0) { return std::array<int, 13> { mode, pre, post, sep, sat, ceil, att, rel2, 2, 5000, 3, sus, rms }; };
- auto check = [] (const std::pair<std::vector<float>, std::vector<float>>& out, const std::vector<std::tuple<int, float, float>>& golden) {
- for (const auto& [i, gl, gr] : golden)
- CHECK (std::abs (out.first[(std::size_t) i] - gl) < 2e-6f && std::abs (out.second[(std::size_t) i] - gr) < 2e-6f);
- };
- // RMS detectors, saturation mode B, a 5 Hz low cut, 12 dB high split (SG_x64.dll: port within −152 dB)
- auto st3 = maximusTestState ({ 19800, 1, 48065, 0, 1115, 1000, 0 },
- { band (0, 1500, 1000, 0, 400, 900, 1115, 0, 2882, 1), band (0, 1340, 1000, 0, 0, 1000, 1115, 0, 1570, 1),
- band (1, 1000, 1200, 0, 1000, 1000), band (0, 1000, 1000, 0, 250, 1000, 1115, 4464, 2882, 1) },
- { def, def, knee, def, t0, def, m0, def }, 0);
- const auto p3 = lynx::hlfx::makeMaximusPreset (st3);
- CHECK (p3 != nullptr);
- check (run (p3, 1000), { { 100, 0.0f, 0.0f }, { 1000, 0.87061065f, 0.64004499f }, { 2047, -0.97266865f, 0.60940075f },
- { 3000, 0.96658099f, 0.9250561f }, { 4095, 0.99094611f, -0.964059f } });
- // the same with solo on (UI byte 0x1EF) and the low band selected (0x1E0): bit-identical to SG_x64.dll
- auto solo = st3;
- solo[0x1ef] = 1;
- std::memset (solo.data() + 0x1e0, 0, 4);
- check (run (lynx::hlfx::makeMaximusPreset (solo), 1000),
- { { 100, 3.2922967e-06f, 0.00036916698f }, { 1000, 0.15653539f, -0.047913823f }, { 2047, -0.17336531f, 0.019099623f },
- { 3000, 0.17185682f, -0.017173048f }, { 4095, 0.16714551f, 0.019473195f } });
- // the legacy linear-phase FIR band split (UI byte 0x1FD; SG_x64.dll: port within −148 dB)
- auto fir = st3;
- fir[0x1fd] = 1;
- check (run (lynx::hlfx::makeMaximusPreset (fir), 1000),
- { { 100, 0.0f, 0.0f }, { 1000, 0.97986746f, 0.18555209f }, { 2047, -0.98930448f, -0.95970845f },
- { 3000, 0.98856646f, -0.44592777f }, { 4095, 0.99069679f, 0.94382554f } });
- // Soundgoodizer_x64.dll's own build with a v4 file (its presets' layout): a copy of the round-36 state without
- // oversampling, through a byte-identical copy of the installation's DLL with this file as its preset (EXEC)
- const auto st2 = maximusTestState ({ 19800, 1, 48065, 1, 891, 600, 32768 },
- { band (1, 1264, 1296, 500, -500, 1200), band (0, 1340, 1000, -348, 0, 1000, 1115, 0, 1570),
- band (0, 1364, 1152, 0, 0, 1000, 1115, 0, 1186), band (0, 1000, 1000, 0, -196, 1000, 1115, 4464, 1538) },
- { def, def, knee, def, t0, def, m0, def }, 0);
- std::vector<std::uint8_t> v4 { 4, 0, 0, 0 };
- v4.insert (v4.end(), st2.begin() + 4, st2.begin() + 0x1f5);
- v4.insert (v4.end(), st2.begin() + 0x1fe, st2.end());
- const auto p4 = lynx::hlfx::makeMaximusPreset (v4, lynx::hlfx::MaximusBuild::soundgoodizer);
- CHECK (p4 != nullptr);
- check (run (p4, 600), { { 100, 0.0f, 0.0f }, { 1000, -0.60926896f, -0.6652832f }, { 2047, 0.40872875f, 0.36375371f },
- { 3000, -0.41941205f, -0.61312866f }, { 4095, -0.12878418f, 0.27944946f } });
- CHECK (lynx::hlfx::makeMaximusPreset (st2, lynx::hlfx::MaximusBuild::soundgoodizer) != nullptr); // v7: also read by that build
- // the mixer effect's container and the effect itself
- lynx::maximusfx::Blob blob;
- blob.build = lynx::hlfx::MaximusBuild::soundgoodizer;
- blob.mix = 600;
- blob.state = v4;
- const auto packed = lynx::maximusfx::pack (blob);
- const auto back = lynx::maximusfx::unpack (packed);
- CHECK (back && back->build == blob.build && back->mix == 600 && back->state == v4);
- auto slot = lynx::makeEffectSlot (lynx::EffectKind::maximus);
- slot.state = packed;
- auto fx = lynx::makeEffect (slot);
- fx->prepare (44100.0);
- {
- std::vector<float> l (inL), r (inR);
- for (int s = 0; s < 4096; s += 1024) // longer host blocks are split into the engine's 512-frame calls
- fx->process (l.data() + s, r.data() + s, 1024, slot.params, 120.0);
- const auto ref = run (p4, 600);
- CHECK (l == ref.first && r == ref.second);
- }
- const std::vector<std::uint8_t> sgState { 3, 0, 0, 0, 2, 0, 0, 0, 0x78, 2, 0, 0 };
- const auto sg = lynx::maximusfx::parseSoundgoodizer (sgState);
- CHECK (sg && sg->preset == 2 && sg->amount == 632 && std::string (lynx::maximusfx::soundgoodizerPresetFile (2)) == "c.dat");
- CHECK (lynx::soundgoodizerPresetPath (sgState) == "Plugins\\Fruity\\Effects\\Soundgoodizer\\Data\\Presets\\c.dat");
- CHECK (lynx::soundgoodizerExact (sgState, v4).has_value() && ! lynx::soundgoodizerExact (sgState, {}).has_value());
- // Vocodex's appended installation data survives a round trip and leaves FL's state untouched
- const std::vector<std::uint8_t> flState { 8, 0, 0, 0, 1, 2, 3 };
- lynx::vocodex::InstallData data;
- data.sgDat = st2;
- data.carrierState = { 9, 9, 9 };
- const auto joined = lynx::vocodex::appendInstallData (flState, data);
- const auto [raw, files] = lynx::vocodex::splitInstallData (joined);
- CHECK (raw == flState && files.sgDat == st2 && files.carrierState == data.carrierState);
- CHECK (lynx::vocodex::splitInstallData (flState).first == flState);
- }
- // ---- Sytrus (REA_rodada31) -------------------------------------------------------------------------------------------
- // A Sytrus state built from scratch (version 25 layout, our own values — no FL preset data), zlib "stored" blocks.
- namespace
- {
- struct SytrusTestPatch
- {
- std::vector<std::int32_t> main = std::vector<std::int32_t> (240, 0); // 962-byte main block as ints (+ mute bytes)
- struct Op
- {
- int p[10] { 0, 256, 0, 0, 0, 0, 0, 0, 0, 0 };
- int ratio = 20000, offset = 0, noise = 0;
- std::vector<float> amps { 1.0f };
- std::uint8_t flags[8] {};
- int env1[3] { -1, -1, -1 }; // volume envelope: on (≠ −1 → on), decay point, sustain point
- std::vector<std::pair<double, double>> env1pts;
- } ops[6];
- struct Filt
- {
- int p[10] { 0, 0, 128, 0, 128, 0, 0, 128, 128, 0 };
- int type = 3;
- } filts[3];
- std::array<std::array<std::uint8_t, 23>, 3> delays {};
- std::array<std::int32_t, 9> reverb { 19, 221, 0, 50, 80, 30, 221, 2, 0 };
- bool reverbOn = false;
- int eqmode = 0;
- SytrusTestPatch()
- {
- main[0] = 128, main[1] = 128;
- for (int i = 0; i < 8; ++i)
- main[(std::size_t) (165 + i)] = (i % 4 == 2) ? 0 : 128;
- main[173] = 1, main[175] = 128, main[188] = 128, main[189] = -1, main[190] = main[191] = 256;
- }
- void cell (int layer, int row, int col, int v) { main[(std::size_t) (3 + layer * 81 + row * 9 + col)] = v; }
- std::vector<std::uint8_t> blob() const
- {
- std::vector<std::uint8_t> b;
- 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))); };
- auto f32 = [&] (float v) { std::int32_t x; std::memcpy (&x, &v, 4); i32 (x); };
- 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))); };
- // point lists: kind 1 envelope, 2 LFO, 3 map / shaper curve; format version 3
- auto env = [&] (int kind, const std::vector<std::pair<double, double>>& pts, int on, int decay, int sus) {
- i32 (kind), i32 (3), i32 ((std::int32_t) pts.size());
- for (const auto& [dt, y] : pts)
- f64 (dt), f64 (y), f32 (0.0f), i32 (0);
- i32 (0), i32 (on), i32 (decay), i32 (-1), i32 (sus);
- };
- for (int i = 0; i < 240; ++i)
- if (4 * i + 4 <= 0x320)
- i32 (main[(std::size_t) i]);
- b.resize (962, 0); // mute bytes: none muted
- i32 (0); // global declick amount
- b.push_back (0), b.push_back (0), b.push_back (0); // OSC grouping, DC stage, GIBBS OFF
- for (int k = 0; k < 9; ++k)
- b.push_back (k == 7 ? 1 : 0); // flag group (release multiplier on, as Default.fst)
- for (const auto& d : delays)
- b.insert (b.end(), d.begin(), d.end());
- for (auto v : reverb)
- i32 (v);
- b.push_back (reverbOn ? 1 : 0), b.push_back (0);
- i32 (eqmode);
- for (int k = 0; k < 3; ++k)
- i32 (0), i32 (30000), i32 (20000), i32 (k);
- b.push_back (0); // no info text
- for (int u = 0; u < 10; ++u)
- {
- const int narts = u < 6 ? 6 : (u < 9 ? 8 : 2);
- for (int j = 0; j < 10; ++j)
- i32 (u < 6 ? ops[u].p[j] : (u < 9 ? filts[u - 6].p[j] : 0));
- if (u < 6)
- {
- const auto& o = ops[u];
- const auto at = b.size();
- b.resize (at + 0x828, 0);
- auto put = [&] (std::size_t off, const void* src, std::size_t n) { std::memcpy (b.data() + at + off, src, n); };
- put (0, &o.ratio, 4), put (4, &o.offset, 4), put (8, &o.noise, 4);
- put (0xc, o.amps.data(), 4 * o.amps.size());
- const std::int32_t shape[5] { 0, 0, 0, 0, 128 };
- put (0x80c, shape, sizeof shape);
- put (0x820, o.flags, 8);
- }
- else if (u < 9)
- {
- const auto at = b.size();
- b.resize (at + 0x30, 0);
- std::memcpy (b.data() + at + 0x2c, &filts[u - 6].type, 4);
- env (3, { { 0.0, 0.0 }, { 1.0, 1.0 } }, 0, -1, -1);
- b.push_back (0);
- }
- for (int k = 0; k < narts; ++k)
- {
- const bool add = k == 0 || k == 3 || (u >= 6 && u <= 8 && (k == 2 || k == 3)) || (u < 6 && k == 4);
- const double neutral = add ? 0.5 : 1.0;
- i32 (3), i32 (0), f32 (1.0f), f32 (1.0f);
- if (u < 6 && k == 1 && ops[u].env1[0] != -1)
- env (1, ops[u].env1pts, -1, ops[u].env1[1], ops[u].env1[2]);
- else
- env (1, { { 0.0, 0.0 }, { 1.0, 1.0 } }, 0, -1, -1);
- i32 (128), i32 (128), i32 (0), i32 (128);
- env (2, { { 0.0, 1.0 }, { 1.0, 1.0 } }, 0, -1, -1);
- i32 (40000), i32 (0), i32 (0), i32 (0), i32 (0);
- for (int m = 0; m < 6; ++m)
- env (3, { { 0.0, neutral }, { 1.0, neutral } }, 0, -1, -1);
- }
- }
- b.push_back (0), b.push_back (0);
- // zlib with stored blocks
- std::vector<std::uint8_t> z { 0x78, 0x01 };
- for (std::size_t at = 0; at < b.size(); at += 65535)
- {
- const auto n = std::min<std::size_t> (65535, b.size() - at);
- z.push_back (at + n >= b.size() ? 1 : 0);
- z.push_back ((std::uint8_t) n), z.push_back ((std::uint8_t) (n >> 8));
- z.push_back ((std::uint8_t) ~n), z.push_back ((std::uint8_t) (~n >> 8));
- z.insert (z.end(), b.begin() + (std::ptrdiff_t) at, b.begin() + (std::ptrdiff_t) (at + n));
- }
- std::uint32_t a = 1, c = 0;
- for (auto x : b)
- a = (a + x) % 65521, c = (c + a) % 65521;
- for (int k = 3; k >= 0; --k)
- z.push_back ((std::uint8_t) (((c << 16) | a) >> (8 * k)));
- std::vector<std::uint8_t> out { 25, 0, 0, 0 };
- out.insert (out.end(), z.begin(), z.end());
- return out;
- }
- };
- // renders like FL's Gen_Render calls: notes start/release at the block that contains them
- std::pair<std::vector<float>, std::vector<float>> sytrusRender (const std::vector<std::uint8_t>& blob, bool renderMode, int n,
- std::vector<std::array<int, 3>> notes, double fs = 44100.0)
- {
- lynx::sytrus::Engine e;
- e.setTempo (120.0);
- e.prepare (fs, renderMode, 512);
- e.setPatch (lynx::sytrus::parsePatch (blob));
- std::vector<float> l ((std::size_t) n), r ((std::size_t) n);
- for (int n0 = 0; n0 < n; n0 += 512)
- {
- for (std::size_t k = 0; k < notes.size(); ++k)
- {
- if (notes[k][1] >= n0 && notes[k][1] < n0 + 512)
- {
- lynx::sytrus::NoteParams p;
- p.key = (float) notes[k][0];
- p.id = (std::int64_t) k + 1;
- e.noteOn (p);
- }
- if (notes[k][2] >= n0 && notes[k][2] < n0 + 512)
- e.release ((std::int64_t) k + 1);
- }
- e.render (l.data() + n0, r.data() + n0, std::min (512, n - n0), 1.0f, 0.0f);
- }
- return { l, r };
- }
- } // namespace
- TEST (sytrus_engine_sine)
- {
- // one sine operator, ratio 1, OUT 256, no envelopes: C5 = 523.2511 Hz at 0.7071 (centre of the pan law, §6.1)
- SytrusTestPatch t;
- t.cell (0, 0, 8, 256);
- const auto blob = t.blob();
- CHECK (lynx::sytrus::stateUsable (blob));
- CHECK (lynx::sytrus::parsePatch (blob) != nullptr);
- auto broken = blob;
- broken.resize (broken.size() / 2);
- CHECK (! lynx::sytrus::stateUsable (broken));
- const auto [l, r] = sytrusRender (blob, false, 8192, { { 60, 0, 4096 } });
- double worst = 0.0;
- for (int k = 0; k < 4000; ++k)
- 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)));
- CHECK (worst < 2e-5);
- CHECK (l == r);
- // 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
- // 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)
- int last = 0;
- for (int k = 0; k < 8192; ++k)
- if (l[(std::size_t) k] != 0.0f)
- last = k;
- CHECK (last == 4895);
- // key 69 = 880 Hz; 48 kHz
- const auto [l2, r2] = sytrusRender (blob, false, 4096, { { 69, 0, -1 } }, 48000.0);
- worst = 0.0;
- for (int k = 0; k < 4000; ++k)
- 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)));
- CHECK (worst < 5e-5);
- (void) r2;
- }
- TEST (sytrus_engine_reference)
- {
- // FM 2→1, Vanilla LP filter, volume envelope, unison 3, delay and reverb, 2× oversampling with the 8-sample
- // envelope tick (render settings): against our Python reference model of REA_rodada31 (sytmodel.render)
- SytrusTestPatch t;
- t.main[195] = 0, t.main[196] = 1, t.main[193] = 0, t.main[194] = 1;
- t.main[173] = 3, t.main[174] = 64, t.main[176] = 100;
- t.ops[1].ratio = 40000;
- t.cell (0, 0, 1, 128); // op 2 → op 1 FM
- t.cell (0, 0, 8, 128);
- t.cell (0, 6, 0, 256); // op 1 → filter 1
- t.cell (0, 6, 8, 256);
- t.cell (0, 0, 7, 200); // FX send
- t.filts[0].p[0] = 1, t.filts[0].p[2] = 180, t.filts[0].p[3] = 60;
- t.ops[0].env1[0] = 0, t.ops[0].env1[1] = 1, t.ops[0].env1[2] = 2;
- t.ops[0].env1pts = { { 0.0, 0.0 }, { 0.05, 1.0 }, { 0.1, 0.6 }, { 0.2, 0.0 } };
- const std::int32_t d0[5] { 40, 100, 50, 100, 0 };
- std::memcpy (t.delays[0].data(), d0, 20);
- t.delays[0][22] = 1;
- t.reverb = { 19, 221, 10, 50, 80, 30, 100, 2, 100 };
- t.reverbOn = true;
- const auto blob = t.blob();
- const auto [l, r] = sytrusRender (blob, true, 12000, { { 60, 0, 6000 } });
- // reference values (model render_mode=True, 44.1 kHz, block 512, key 60, velocity 0.78125, release at 6000); the same
- // state rendered by FL's own Sytrus DLL in render mode differs from this by −136 dB (EXEC, REA round 31 harness)
- const std::array<std::pair<int, float>, 8> golden { { { 100, -0.00235529384f }, { 777, -0.175126165f }, { 2000, 0.30976975f }, { 4321, -0.0213534199f },
- { 6100, 0.202372357f }, { 7000, 0.249530748f }, { 9000, 0.0307101421f }, { 11999, 0.176390275f } } };
- for (const auto& [i, v] : golden)
- CHECK (std::abs (l[(std::size_t) i] - v) < 1e-6f);
- const std::array<std::pair<int, float>, 3> goldenR { { { 777, -0.166890964f }, { 4321, -0.212358952f }, { 9000, -0.0251909085f } } };
- for (const auto& [i, v] : goldenR)
- CHECK (std::abs (r[(std::size_t) i] - v) < 1e-6f);
- }
- TEST (sytrus_instrument)
- {
- // the LYNX instrument: state in ChannelDef::pluginState, channel volume/pan recovered from the rack gains
- SytrusTestPatch t;
- t.cell (0, 0, 8, 256);
- lynx::ChannelDef c;
- c.instrument = lynx::InstrumentKind::sytrus;
- c.pluginState = t.blob();
- auto inst = lynx::makeInstrument (lynx::InstrumentKind::sytrus);
- inst->prepare (44100.0);
- inst->configure (c, 120.0);
- CHECK (inst->idle());
- std::vector<float> l (4096), r (4096);
- inst->noteOn (60, 100.0f / 128.0f);
- // 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
- inst->render (l.data(), r.data(), 4096, 0.5f, 0.5f);
- double peak = 0.0;
- for (int k = 1000; k < 4096; ++k)
- peak = std::max (peak, (double) std::abs (l[(std::size_t) k]));
- CHECK (std::abs (peak - 0.5 * 0.70710678) < 1e-3); // Final.Vol 0.5 × the plugin's centre pan law
- CHECK (! inst->idle());
- inst->noteOff (60);
- std::vector<float> tail (44100 * 3), tailR (tail.size());
- inst->render (tail.data(), tailR.data(), (int) tail.size(), 0.5f, 0.5f);
- CHECK (inst->idle());
- CHECK (tail.back() == 0.0f);
- }
- TEST (harmor_effects_rack)
- {
- // REA_rodada38: the rack runs in the state's unit order and the records' FX send joins it at unit 4 — with the FX
- // amount at 1000 every record output bypasses a reverb placed before unit 4 (EXEC: −600 dB vs the rack off)
- auto blob = harmorTestState (false);
- auto set = [&] (std::vector<std::uint8_t>& st, int part, int index, std::int32_t v) {
- std::memcpy (st.data() + lynx::harmorParamOffset (st, part, index), &v, 4);
- };
- set (blob, 0, 10, 1000); // all to the FX send
- auto render = [] (const std::vector<std::uint8_t>& st) {
- return lynx::harmor::renderReference (st, { { 0, 60, 0.8, 1.0, 0.0, 6000 } }, 44100.0, 12000);
- };
- const auto dry = render (blob);
- auto rev = blob;
- set (rev, -1, 22, 1); // reverb on: room 50, diffusion 100, decay 2^3·0.1 s, high cut / damping off, wet 0.8
- 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);
- set (rev, -1, 30, 2), set (rev, -1, 31, 800);
- const auto bypassed = render (rev);
- double eb = 0.0, db = 0.0;
- for (std::size_t i = 0; i < dry.size(); ++i)
- eb += (double) (bypassed[i] - dry[i]) * (bypassed[i] - dry[i]), db += (double) dry[i] * dry[i];
- CHECK (eb < 1e-12 * db && db > 1e-3); // (float rounding of the rack's buffers only)
- auto after = rev;
- const std::int32_t order[6] { 0, 1, 2, 4, 3, 5 }; // the join before the reverb
- for (int k = 0; k < 6; ++k)
- set (after, -1, 53 + k, order[k]);
- const auto wet = render (after);
- double e = 0.0, d = 0.0;
- bool finite = true;
- for (std::size_t i = 0; i < wet.size(); ++i)
- e += (double) (wet[i] - dry[i]) * (wet[i] - dry[i]), d += (double) dry[i] * dry[i], finite = finite && std::isfinite (wet[i]);
- CHECK (finite && e > 1e-4 * d && d > 1e-3);
- // a distortion (type 6) and a delay on the dry path change the sound, stay finite, and the instrument ends quiet
- auto dist = harmorTestState (false);
- {
- const auto at = lynx::harmorParamOffset (dist, -1, 0) + 0xd4 + 0x18 + 48; // global misc +48: distortion type
- const std::int32_t six = 6;
- std::memcpy (dist.data() + at, &six, 4);
- }
- set (dist, -1, 17, 600), set (dist, -1, 19, 1000), set (dist, -1, 21, 1100);
- 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);
- set (dist, -1, 47, 1050);
- lynx::ChannelDef c;
- c.instrument = lynx::InstrumentKind::harmor;
- c.pluginState = dist;
- auto inst = lynx::makeInstrument (lynx::InstrumentKind::harmor);
- inst->configure (c, 120.0);
- inst->prepare (44100.0);
- std::vector<float> l (44100 * 4), r (l.size());
- inst->noteOn (60, 0.8f);
- double energy = 0.0;
- for (int s = 0; s < (int) l.size(); s += 512)
- {
- if (s == 8192)
- inst->noteOff (60);
- inst->render (l.data() + s, r.data() + s, 512, 1.0f, 1.0f);
- }
- for (std::size_t i = 0; i < l.size(); ++i)
- finite = finite && std::isfinite (l[i]) && std::isfinite (r[i]), energy += (double) l[i] * l[i];
- CHECK (finite && energy > 1.0);
- std::vector<float> tail (44100 * 6), tailR (tail.size());
- for (int s = 0; s < (int) tail.size(); s += 512)
- inst->render (tail.data() + s, tailR.data() + s, 512, 1.0f, 1.0f);
- CHECK (inst->idle());
- }
- TEST (harmless_engine)
- {
- // REA_rodada32: state parsing, the additive engine against our reference model (hlmodel.py), host levels, voice end
- std::vector<std::uint8_t> blob;
- 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))); };
- std::array<std::int32_t, 100> p {}; // the reference's clean patch: Saw, open filter, no modulation
- for (int i = 11; i < 23; ++i)
- p[(std::size_t) i] = 1000;
- 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;
- 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;
- // + resonance, pluck, 3-voice unison, tremolo, LFO on pitch
- 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;
- put (12);
- for (auto v : p)
- put (v);
- for (int i = 0; i < 7; ++i)
- put (0);
- put (524); // Custom colour table: 524 × (amplitude, phase)
- for (int i = 0; i < 1048; ++i)
- {
- const auto f = i < 524 ? 1.0f / (float) (i + 1) : 0.25f;
- std::uint32_t bits = 0;
- std::memcpy (&bits, &f, 4);
- put ((std::int32_t) bits);
- }
- const auto parsed = lynx::harmlessParams (blob);
- CHECK (parsed && (int) parsed->size() == lynx::harmlessParamCount);
- CHECK (parsed && (*parsed)[54] == 3500.0f && (*parsed)[99] == 0.0f && (*parsed)[lynx::harmlessCustomFlagIndex] == 1.0f);
- CHECK (parsed && (*parsed)[lynx::harmlessCustomIndex + 1] == 0.5f && (*parsed)[lynx::harmlessCustomIndex + 524] == 0.25f);
- auto old = blob;
- old[0] = 3; // versions below 4 are not Harmless states we know
- CHECK (! lynx::harmlessParams (old));
- old = blob;
- old.resize (300);
- CHECK (! lynx::harmlessParams (old));
- lynx::ChannelDef c;
- c.instrument = lynx::InstrumentKind::harmless;
- c.modelParams = *parsed;
- c.modelParams[lynx::harmlessCustomFlagIndex] = 0.0f; // colour 0 (Saw): the custom table is not read
- auto render = [] (const lynx::ChannelDef& def, int total, float gl, float gr, int offAt) {
- auto inst = lynx::makeInstrument (lynx::InstrumentKind::harmless);
- inst->configure (def, 120.0);
- inst->prepare (44100.0);
- std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
- inst->noteOn (60, 0.78125f);
- for (int s = 0; s < total; s += 512)
- {
- if (s == offAt)
- inst->noteOff (60);
- inst->render (l.data() + s, r.data() + s, std::min (512, total - s), gl, gr);
- }
- return std::make_tuple (l, r, std::move (inst));
- };
- {
- // reference model (REA round 32 hlmodel.py, C5 at FL velocity 100, channel volume 1, centre, released at 6144)
- auto [l, r, inst] = render (c, 8192, 1.0f, 1.0f, 6144);
- const std::tuple<int, float, float> golden[] {
- { 0, 0.0f, 0.0f }, { 10, -0.02220994f, -0.06232700f }, { 100, 0.40623373f, 0.17308202f }, { 1000, -0.04091963f, -0.26821208f },
- { 3000, 0.14519004f, 0.11027778f }, { 6000, 0.50411958f, 0.27901855f }, { 8191, 0.02117692f, -0.01466602f } };
- for (const auto& [i, gl, gr] : golden)
- CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
- // after the release the record fades out (below 1e-4, then 30 ms) and the instrument goes idle
- std::vector<float> tail (44100), tailR (tail.size());
- inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
- CHECK (inst->idle());
- }
- // FL's level law applied once: half the channel volume halves the output; full right pan silences the left side
- const auto full = std::get<0> (render (c, 2048, 1.0f, 1.0f, -1));
- const auto half = std::get<0> (render (c, 2048, 0.5f, 0.5f, -1));
- CHECK (std::abs (half[100] - 0.5f * full[100]) < 1e-6f && std::abs (full[100]) > 0.1f);
- auto mono = c;
- mono.modelParams[25] = 0.0f; // no tremolo, no unison pan spread
- mono.modelParams[65] = 1.0f;
- const auto [pl, pr, pi] = render (mono, 2048, 0.0f, 1.41421356f, -1);
- double el = 0.0, er = 0.0;
- for (int i = 0; i < 2048; ++i)
- el += (double) pl[(std::size_t) i] * pl[(std::size_t) i], er += (double) pr[(std::size_t) i] * pr[(std::size_t) i];
- CHECK (el < 1e-10 * er && er > 1.0);
- }
- TEST (startup_sound)
- {
- // the splash's sound: the right length, finite, at its peak level, quiet before the whoosh swells,
- // a landing "pop" at impactTime and a chime ringing after it
- using namespace lynx::startup;
- const auto sound = makeSound (48000.0);
- CHECK (sound.left.size() == (std::size_t) (soundLength * 48000.0) && sound.right.size() == sound.left.size());
- float peak = 0.0f;
- bool finite = true;
- for (std::size_t i = 0; i < sound.left.size(); ++i)
- {
- finite = finite && std::isfinite (sound.left[i]) && std::isfinite (sound.right[i]);
- peak = std::max ({ peak, std::abs (sound.left[i]), std::abs (sound.right[i]) });
- }
- CHECK (finite && std::abs (peak - peakLevel) < 1.0e-4f);
- auto rms = [&sound] (double from, double to) {
- double e = 0.0;
- const auto a = (std::size_t) (from * 48000.0), b = (std::size_t) (to * 48000.0);
- for (auto i = a; i < b; ++i)
- e += (double) sound.left[i] * sound.left[i];
- return std::sqrt (e / (double) (b - a));
- };
- CHECK (rms (0.0, 0.05) < 0.2 * rms (impactTime - 0.05, impactTime)); // the whoosh swells
- CHECK (rms (impactTime, impactTime + 0.05) > rms (impactTime - 0.05, impactTime)); // the landing
- CHECK (rms (0.8, 1.0) > 0.005 && rms (soundLength - 0.02, soundLength) < rms (0.8, 1.0)); // chime rings, tail fades
- CHECK (makeSound (48000.0).left == sound.left); // the same sound on every start
- }
- namespace
- {
- // little-endian byte writer for the state builders below
- struct Bytes
- {
- std::vector<std::uint8_t> b;
- void raw (const char* s, std::size_t n) { b.insert (b.end(), s, s + n); }
- 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); }
- void f32 (float v) { b.insert (b.end(), reinterpret_cast<const std::uint8_t*> (&v), reinterpret_cast<const std::uint8_t*> (&v) + 4); }
- void zeros (std::size_t n) { b.insert (b.end(), n, 0); }
- void bytes (const std::vector<std::uint8_t>& o) { b.insert (b.end(), o.begin(), o.end()); }
- };
- // A GMS state written from scratch (REA_rodada12 §1.1 layout, "Syn4"): oscillator waves that don't exist (silent
- // tables, so the result does not depend on an FL installation), noise through the ladder with envelope and LFO,
- // unison, slide, distortion, delay, reverb and the output EQ.
- std::vector<std::uint8_t> gmsTestState()
- {
- std::array<float, 212> P {};
- for (int i : { 1, 2, 44, 55, 56, 57, 58, 59, 60, 207, 208, 209, 210, 211 })
- P[(std::size_t) i] = 0.5f;
- P[0] = 0.75f, P[4] = 0.1f, P[5] = 0.4f, P[6] = 0.6f, P[7] = 0.3f, P[8] = 0.8f;
- 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;
- 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;
- P[147] = 1.0f, P[117] = 0.3f, P[127] = 0.6f; // distortion
- P[153] = 1.0f, P[123] = 0.5f, P[133] = 0.4f, P[143] = 0.4f; // delay (1/2 beat)
- P[154] = 1.0f, P[124] = 0.6f, P[134] = 0.5f; // reverb
- P[206] = 1.0f, P[207] = 0.7f, P[211] = 0.3f; // output EQ
- Bytes preset;
- preset.raw ("4nyS", 4);
- for (const char* path : { "NoSuchDir\\NoWave A.wav", "NoSuchDir\\NoWave B.wav", "NoSuchDir\\NoWave C.wav", "", "", "", "" })
- {
- const auto n = std::strlen (path);
- preset.raw (path, n);
- preset.zeros (512 - n);
- }
- preset.i32 (0), preset.i32 (0), preset.i32 (0);
- for (auto v : P)
- preset.f32 (v);
- preset.raw ("LYNX test", 9);
- preset.zeros (512 - 9);
- Bytes s;
- s.raw ("SSLF", 4);
- s.i32 ((std::int32_t) preset.b.size());
- s.bytes (preset.b);
- return s.b;
- }
- // A Morphine state written from scratch (REA_rodada16 §3 / REA_rodada39 §2.2 layout): generators A and B with a few
- // frames each (A loops, PWM on A), C and D silent, a morph line A→B, four envelopes (Eg1 → pitch and velocity →
- // amplitude through the matrix), the master ADSR and every plugin effect on.
- std::vector<std::uint8_t> morphineTestState()
- {
- auto mbp = [] (int seed) {
- Bytes b;
- b.raw ("0PBM", 4);
- for (int a = 0; a < 3; ++a)
- for (int byte = 0; byte < 16; ++byte)
- {
- b.b.push_back (0xff);
- for (int bit = 0; bit < 8; ++bit)
- {
- const auto k = byte * 8 + bit;
- const float v = a == 0 ? std::cbrt (1.0f / (float) (k + 1 + seed * (k % 3))) * (k < 40 ? 1.0f : 0.2f)
- : a == 1 ? 0.1f * (float) ((k * 7 + seed) % 11 - 5) / 5.0f
- : 0.02f * (float) ((k * 5 + seed) % 7 - 3);
- b.f32 (v);
- }
- }
- return b.b;
- };
- auto msp = [&] (int frames, int seed, int loopMode) {
- Bytes b;
- b.raw ("0PSM", 4);
- b.i32 (frames);
- for (int f = 0; f < frames; ++f)
- {
- const auto m = mbp (seed + f);
- b.i32 (20 + 7 * f), b.f32 (1.0f - 0.2f * (float) f), b.i32 ((std::int32_t) m.size());
- b.bytes (m);
- }
- b.i32 (0x424);
- b.i32 (10), b.i32 (40), b.i32 (0), b.i32 (loopMode);
- b.f32 (0.45f), b.f32 (0.6f), b.f32 (0.5f), b.i32 (0);
- for (int k = 0; k < 128; ++k)
- b.f32 (0.3f);
- for (int k = 0; k < 128; ++k)
- b.f32 (1.0f);
- return b.b;
- };
- auto mvc = [&] (int frames, int seed, int pwmMode) {
- Bytes z;
- z.raw ("0HZM", 4);
- z.i32 (1);
- const auto s = msp (frames, seed, 1);
- z.i32 (128), z.f32 (frames > 1 ? 0.9f : 0.0f), z.i32 ((std::int32_t) s.size());
- z.bytes (s);
- Bytes b;
- b.raw ("0CVM", 4);
- b.i32 ((std::int32_t) z.b.size());
- b.bytes (z.b);
- 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);
- return b.b;
- };
- Bytes p;
- p.raw ("0roM", 4);
- p.raw ("0RPM", 4);
- for (const auto& g : { mvc (3, 1, 2), mvc (2, 7, 0), mvc (1, 3, 0), mvc (1, 5, 0) })
- {
- p.i32 ((std::int32_t) g.size());
- p.bytes (g);
- }
- {
- Bytes m;
- m.raw ("0LMM", 4);
- m.i32 (2), m.i32 (12);
- m.f32 (0.0f), m.f32 (1.0f), m.i32 (60);
- m.f32 (1.0f), m.f32 (1.0f), m.i32 (60);
- m.i32 (0x54), m.i32 (0), m.i32 (0), m.i32 (0), m.i32 (0);
- for (float v : { 1.0f, 0.8f, 0.0f, 0.0f, 0.5f, 0.4f, 0.5f, 0.5f })
- m.f32 (v);
- for (int k = 0; k < 4; ++k)
- m.i32 (0);
- m.f32 (0.5f), m.f32 (0.5f), m.f32 (0.5f), m.i32 (0);
- p.i32 ((std::int32_t) m.b.size());
- p.bytes (m.b);
- }
- {
- Bytes d;
- d.raw ("0DMM", 4);
- d.i32 (4);
- for (int e = 0; e < 4; ++e)
- {
- Bytes g;
- g.raw ("0GEM", 4);
- g.i32 (3), g.i32 (8);
- g.f32 (0.0f), g.i32 (10), g.f32 (1.0f), g.i32 (40 + 10 * e), g.f32 (0.3f), g.i32 (50);
- 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),
- g.i32 (e == 0 ? 1 : 0);
- d.i32 ((std::int32_t) g.b.size());
- d.bytes (g.b);
- }
- d.i32 (0x94);
- const int src[12] = { 5, 2 }, dst[12] = { 3, 1 };
- const float amt[12] = { 0.56f, 0.7f };
- for (int k = 0; k < 12; ++k)
- d.i32 (src[k]);
- for (int k = 0; k < 12; ++k)
- d.i32 (dst[k]);
- for (int k = 0; k < 12; ++k)
- d.f32 (amt[k]);
- p.i32 ((std::int32_t) d.b.size());
- p.bytes (d.b);
- }
- {
- 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
- 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 …
- 0.4f, 0.3f, 1.0f, 0.5f, 0.5f, 0.4f, 1.0f, 0.6f, 0.4f, 0.5f, // … delay, reverb, EQ …
- 0.5f, 0.5f, 0.55f, 0.5f, 0.45f, 1.0f, 0.0f, 0.5f, 1.0f, 0.0f }; // EQ on, noise (muted)
- Bytes g;
- g.i32 (0x5c4);
- for (auto v : A)
- g.f32 (v);
- g.zeros (0x5c4 - g.b.size());
- p.bytes (g.b);
- }
- Bytes s;
- s.raw ("SSLF", 4);
- s.i32 ((std::int32_t) p.b.size());
- s.bytes (p.b);
- return s.b;
- }
- void dumpIfAsked (const char* var, const std::vector<std::uint8_t>& blob)
- {
- if (const char* path = std::getenv (var))
- {
- std::ofstream out (path, std::ios::binary);
- out.write (reinterpret_cast<const char*> (blob.data()), (std::streamsize) blob.size());
- }
- }
- } // namespace
- TEST (dx10_engine)
- {
- // REA_rodada42: a Fruity DX10 state written from scratch (version 1: 22 raw parameters) against our reference model
- // (dxmodel.py + dxproc.py, bit-identical to the installation's DLL), the channel processor, host levels and the
- // instrument front-end
- const std::int32_t P[22] = { 1000, 40000, 30000, 30000, 40000, 30000, 35000, 8000, 40000, 50000, 20000,
- 25000, 15000, 30000, 20000, 10000, 20000, 30000, 5000, 30000, 20000, -1 };
- std::vector<std::uint8_t> blob (4 + 22 * 4, 0);
- blob[0] = 1;
- std::memcpy (blob.data() + 4, P, sizeof (P));
- CHECK (lynx::dx10StateUsable (blob) && lynx::dx10ProcessorNeutral (blob));
- CHECK (lynx::dx10Param (blob, 21) == -1 && lynx::dx10Param (blob, 3) == 30000 && ! lynx::dx10Param (blob, 22));
- CHECK (! lynx::dx10StateUsable (std::vector<std::uint8_t> (40, 0)));
- {
- auto bad = blob;
- bad[0] = 3; // versions ≥ 3 are ignored by the plugin
- CHECK (! lynx::dx10StateUsable (bad));
- }
- using N = lynx::dx10::RefNote;
- 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 } };
- struct Golden { std::vector<std::uint8_t> state; lynx::dx10::RefOptions opt; int rate; double el, er; std::tuple<int, float, float> at[6]; };
- auto check = [&] (const Golden& g) {
- const auto y = lynx::dx10::renderReference (g.state, notes, (double) g.rate, 16384, g.opt);
- CHECK (y.size() == 2 * 16384);
- if (y.size() != 2 * 16384)
- return y;
- double el = 0.0, er = 0.0;
- for (std::size_t i = 0; i + 1 < y.size(); i += 2)
- el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
- CHECK (std::abs (el - g.el) <= 1e-7 * g.el && std::abs (er - g.er) <= 1e-7 * g.er);
- for (const auto& [i, l, r] : g.at)
- CHECK (std::abs (y[2 * (std::size_t) i] - l) <= 1e-7f * std::max (1e-16f, std::abs (l))
- && std::abs (y[2 * (std::size_t) i + 1] - r) <= 1e-7f * std::max (1e-16f, std::abs (r)));
- return y;
- };
- lynx::dx10::RefOptions plain, odd;
- odd.block = 333, odd.maxPoly = 1;
- check ({ blob, plain, 44100, 672.293115, 778.393071,
- { { 100, -0.0943799689f, -0.154014125f }, { 1000, -0.0814286768f, -0.132879525f }, { 2500, 0.499493301f, 0.312498778f },
- { 5000, -0.184045464f, 0.171641544f }, { 9000, -0.153369367f, -0.20805186f }, { 16383, -0.00441304967f, 0.200126961f } } });
- // 48 kHz, odd host blocks, max polyphony 1 (the last note steals); the hard-right note keeps FL's x87 left gain
- check ({ blob, odd, 48000, 122.232761, 493.320297,
- { { 100, 0.0236493405f, 0.0385922194f }, { 1000, 0.189806491f, 0.309736073f }, { 2500, -0.425576478f, -0.176279545f },
- { 5000, -3.54030076e-18f, -0.0578194074f }, { 9000, 1.37052479e-17f, 0.223831087f }, { 16383, 9.248742e-18f, 0.151048407f } } });
- // the channel processor (§3): our own settings snapshot — pan LFO (triangle, delayed, faded in), volume envelope
- // with tensions and LFO, cutoff envelope + square LFO, resonance LFO, pitch envelope, SVF LP ×2 (type 7)
- auto record = [] (std::vector<std::uint8_t>& s, int k, std::initializer_list<std::pair<int, int>> fields) {
- const std::int32_t neutral[17] = { 0, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, 0 };
- std::memcpy (s.data() + 0x44 * k, neutral, sizeof (neutral));
- for (const auto& [i, v] : fields)
- std::memcpy (s.data() + 0x44 * k + 4 * i, &v, 4);
- };
- auto settings = [] (std::vector<std::uint8_t>& s, std::int32_t modX, std::int32_t modY, std::int32_t type) {
- std::memcpy (s.data() + 0x15c, &modX, 4);
- std::memcpy (s.data() + 0x160, &modY, 4);
- std::memcpy (s.data() + 0x168, &type, 4);
- };
- std::vector<std::uint8_t> snap (364, 0);
- record (snap, 0, { { 9, 5000 }, { 10, 9000 }, { 11, -40 }, { 12, 15000 }, { 13, 1 } });
- 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 } });
- record (snap, 2, { { 1, 1 }, { 3, 6000 }, { 4, 100 }, { 5, 16000 }, { 6, 20 }, { 8, -60 }, { 11, 20 }, { 12, 18000 }, { 13, 2 } });
- record (snap, 3, { { 11, 40 }, { 12, 17000 } });
- record (snap, 4, { { 1, 1 }, { 3, 5000 }, { 4, 100 }, { 5, 14000 }, { 6, 30 }, { 8, -700 } });
- settings (snap, 140, 120, 7);
- const auto withSnap = lynx::dx10StateWithSnapshot (blob, snap);
- CHECK (withSnap.size() == 460 && withSnap[0] == 2 && ! lynx::dx10ProcessorNeutral (withSnap));
- CHECK (lynx::dx10StateWithSnapshot (withSnap, std::vector<std::uint8_t> (364, 0)) == withSnap); // a v2 state keeps its own
- lynx::dx10::RefOptions ticks;
- ticks.tick = 230, ticks.ppq = 96, ticks.tempo = 120.0;
- check ({ withSnap, ticks, 44100, 346.852062, 554.699375,
- { { 100, -0.00162654708f, -0.00265428401f }, { 1000, 0.0310285408f, 0.0506339073f }, { 2500, -0.118068494f, -0.0915558636f },
- { 5000, -0.297166437f, -0.256465197f }, { 9000, 0.199509233f, 0.166108996f }, { 16383, -0.00392446062f, 0.0410436988f } } });
- // a volume envelope decaying to zero: the 10 ms end fade, then every voice is gone (silence from 14950 on)
- std::vector<std::uint8_t> decay (364, 0);
- for (int k = 0; k < 5; ++k)
- record (decay, k, {});
- record (decay, 1, { { 0, 4 }, { 1, 1 }, { 3, 9000 }, { 4, 100 }, { 5, 12000 }, { 6, 0 }, { 15, 50 }, { 16, -101 } });
- settings (decay, 256, 0, 0);
- auto ticks48 = ticks;
- ticks48.block = 333;
- const auto y = check ({ lynx::dx10StateWithSnapshot (blob, decay), ticks48, 48000, 338.361707, 350.520208,
- { { 100, 0.000514116196f, 0.000838961045f }, { 1000, 0.0412623473f, 0.0673340634f }, { 2500, -0.141532451f, -0.156581759f },
- { 5000, 0.382550895f, 0.23462519f }, { 7000, -0.189829409f, 0.144132584f }, { 9000, -0.13638559f, 0.142912149f } } });
- bool silentTail = y.size() == 2 * 16384 && (y[2 * 14949] != 0.0f || y[2 * 14949 + 1] != 0.0f);
- for (std::size_t i = 2 * 14950; i < y.size(); ++i)
- silentTail = silentTail && y[i] == 0.0f;
- CHECK (silentTail);
- // the instrument: LYNX velocity v → Init.Vol = (11^(1.28·v) − 1)/10 and Final.Vol = channel volume × Init.Vol; channel
- // gains √2·V·(cos, sin)((1 + p)π/4) → V, p; notes on block boundaries play like the reference renderer (neutral
- // channel settings: the instrument's sequencer ticks change nothing; vibrato off, since the plugin-wide vibrato
- // oscillator is stepped by each voice in turn, so its interleaving follows the host block layout, as in FL)
- auto noVib = blob;
- CHECK (lynx::dx10SetParam (noVib, 10, 0));
- auto initVol = [] (double v) { return (double) (float) ((std::pow (11.0, 1.28 * v) - 1.0) / 10.0); }; // Init.Vol is a float
- lynx::ChannelDef c;
- c.instrument = lynx::InstrumentKind::dx10;
- c.pluginState = noVib;
- auto inst = lynx::makeInstrument (lynx::InstrumentKind::dx10);
- inst->configure (c, 120.0);
- inst->prepare (44100.0);
- const float V = 0.6f, pan = 0.25f;
- const auto gl = (float) (std::sqrt (2.0) * V * std::cos ((1.0 + pan) * 3.141592653589793 / 4.0));
- const auto gr = (float) (std::sqrt (2.0) * V * std::sin ((1.0 + pan) * 3.141592653589793 / 4.0));
- std::vector<float> l (16384), r (16384);
- for (int s = 0; s < 16384; s += 512)
- {
- if (s == 0)
- inst->noteOn (60, 0.78125f);
- if (s == 2048)
- {
- inst->setNextNoteExpression (-0.5f, 25.0f);
- inst->noteOn (67, 0.5f);
- }
- if (s == 6144)
- inst->noteOff (60);
- if (s == 8192)
- inst->noteOff (67);
- inst->render (l.data() + s, r.data() + s, 512, gl, gr);
- }
- // the channel level reaches the engine as FL's (recovered to float precision)
- const auto Vr = std::sqrt (((double) gl * gl + (double) gr * gr) * 0.5);
- const auto pr = std::atan2 ((double) gr, (double) gl) * (4.0 / 3.141592653589793) - 1.0;
- const auto ref = lynx::dx10::renderReference (noVib, { { 0, 60, initVol (0.78125), Vr * initVol (0.78125), pr, 0.0, 6144 },
- { 2048, 67, initVol (0.5), Vr * initVol (0.5), std::clamp (-0.5 + pr, -1.0, 1.0), 25.0, 8192 } },
- 44100.0, 16384);
- double worst = 0.0, peak = 0.0;
- for (int i = 0; i < 16384; ++i)
- 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]) }),
- peak = std::max (peak, (double) std::abs (l[(std::size_t) i]));
- CHECK (worst == 0.0 && peak > 0.05);
- // the released voices end (DX10's own release) and the instrument goes idle
- std::vector<float> tail (44100), tailR (tail.size());
- for (int k = 0; k < 20 && ! inst->idle(); ++k)
- inst->render (tail.data(), tailR.data(), (int) tail.size(), gl, gr);
- CHECK (inst->idle());
- // with the channel settings above the instrument follows FL's ticks: the cutoff envelope darkens the note
- {
- lynx::ChannelDef cs = c;
- cs.pluginState = withSnap;
- cs.modelParams = { 96.0f };
- auto si = lynx::makeInstrument (lynx::InstrumentKind::dx10);
- si->configure (cs, 120.0);
- si->prepare (44100.0);
- std::vector<float> sl (32768), sr (32768);
- si->noteOn (48, 0.78125f);
- for (int s = 0; s < 32768; s += 512)
- si->render (sl.data() + s, sr.data() + s, 512, 1.0f, 1.0f);
- double e1 = 0.0, e2 = 0.0;
- bool finite = true;
- for (int i = 0; i < 32768; ++i)
- finite = finite && std::isfinite (sl[(std::size_t) i]), (i < 4096 ? e1 : e2) += (double) sl[(std::size_t) i] * sl[(std::size_t) i];
- CHECK (finite && e1 > 1e-3 && e2 > 1e-3);
- }
- // parameter edits (the channel settings panel) write the raw integers, clamped to the knob ranges
- auto edited = blob;
- CHECK (lynx::dx10SetParam (edited, 21, 7) && lynx::dx10Param (edited, 21) == 3);
- CHECK (lynx::dx10SetParam (edited, 12, 70000) && lynx::dx10Param (edited, 12) == 65536);
- }
- TEST (gms_engine)
- {
- const auto blob = gmsTestState();
- dumpIfAsked ("LYNX_DUMP_GMS", blob);
- CHECK (lynx::gmsStateUsable (blob));
- CHECK (lynx::gmsWaveNames (blob) == std::vector<std::string> ({ "NoWave A", "NoWave B", "NoWave C" }));
- CHECK (lynx::gmsParamOffset (blob, 12) == 8 + 4 + 7 * 512 + 12 + 4 * 12 && lynx::gmsParamOffset (blob, 212) < 0);
- auto broken = blob;
- broken[8] = 'X';
- CHECK (! lynx::gmsStateUsable (broken));
- CHECK (! lynx::gmsStateUsable (std::vector<std::uint8_t> (blob.begin(), blob.begin() + 2000)));
- // golden samples of our round-39 model (gmsmodel.GmsPlugin, waves missing → silent tables, tempo 140, 44.1 kHz,
- // host blocks of 512): C5 vel 100 0–30000, E5 vel 80 11025–40000, G5 vel 110 22050–52000 (Init.Vol law A)
- auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
- const std::vector<lynx::gms::RefNote> notes = { { 0, 60, (float) iv (100), 1.0, 0.0, 0.0, 30000 },
- { 11025, 64, (float) iv (80), 1.0, 0.0, 0.0, 40000 },
- { 22050, 67, (float) iv (110), 1.0, 0.0, 0.0, 52000 } };
- const auto y = lynx::gms::renderReference (blob, notes, 44100.0, 66150, "");
- CHECK (y.size() == 2 * 66150u);
- 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 } };
- for (const auto& g : golden)
- 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);
- double e = 0.0;
- for (auto v : y)
- e += (double) v * v;
- CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.0790270452795) < 1e-6);
- // the instrument: same engine behind LYNX's channel gains (V = 1, centre → law A), velocity → Init.Vol
- lynx::ChannelDef c;
- c.instrument = lynx::InstrumentKind::gms;
- c.pluginState = blob;
- auto inst = lynx::makeInstrument (lynx::InstrumentKind::gms);
- inst->configure (c, 140.0);
- inst->prepare (44100.0);
- std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
- for (int b = 0; b < 16; ++b)
- {
- if (b == 0)
- inst->noteOn (60, 0.78125f);
- if (b == 10)
- inst->noteOff (60);
- inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
- }
- 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, "");
- double diff = 0.0, energy = 0.0;
- for (int i = 0; i < 8192; ++i)
- {
- 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]) });
- energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
- }
- CHECK (diff == 0.0 && energy > 1e-3);
- }
- TEST (morphine_engine)
- {
- const auto blob = morphineTestState();
- dumpIfAsked ("LYNX_DUMP_MORPHINE", blob);
- CHECK (lynx::morphineStateUsable (blob));
- CHECK (lynx::morphineNoiseName (blob).empty());
- const auto at = lynx::morphineParamOffset (blob, 9);
- CHECK (at > 0);
- float a9 = 0.0f;
- std::memcpy (&a9, blob.data() + at, 4);
- CHECK (a9 == 0.2f && lynx::morphineParamOffset (blob, 40) < 0);
- auto broken = blob;
- broken[12] = 'X'; // the MPR0 tag
- CHECK (! lynx::morphineStateUsable (broken));
- CHECK (! lynx::morphineStateUsable (std::vector<std::uint8_t> (blob.begin(), blob.end() - 10)));
- // golden samples of our round-39 model (mormodel.Synth, tempo 120, 44.1 kHz, host blocks of 512): keys 60/64/67,
- // Init.Vol 1.0/0.5/2.0, note-offs at 30000/50000/70000
- const std::vector<lynx::morphine::RefNote> notes = { { 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 } };
- const auto y = lynx::morphine::renderReference (blob, notes, 44100.0, 80000, "");
- CHECK (y.size() == 2 * 80000u);
- 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 } };
- for (const auto& g : golden)
- 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);
- double e = 0.0;
- for (auto v : y)
- e += (double) v * v;
- CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.18467099041) < 1e-6);
- // the instrument: same engine behind LYNX's channel gains
- lynx::ChannelDef c;
- c.instrument = lynx::InstrumentKind::morphine;
- c.pluginState = blob;
- auto inst = lynx::makeInstrument (lynx::InstrumentKind::morphine);
- inst->configure (c, 120.0);
- inst->prepare (44100.0);
- std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
- for (int b = 0; b < 16; ++b)
- {
- if (b == 0)
- inst->noteOn (60, 0.78125f);
- if (b == 10)
- inst->noteOff (60);
- inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
- }
- 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, "");
- double diff = 0.0, energy = 0.0;
- for (int i = 0; i < 8192; ++i)
- {
- 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]) });
- energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
- }
- CHECK (diff == 0.0 && energy > 1e-3);
- }
- namespace
- {
- // A MiniSynth state written from scratch (REA_rodada41 §1.1 layout: "SSLF" + the .flms preset)
- std::vector<std::uint8_t> miniSynthTestState (const std::array<float, 43>& P)
- {
- const std::string name = "LYNX test";
- Bytes body;
- body.raw ("\x99" "FLS\r\n\x1a\n", 8);
- body.raw ("HEAD", 4);
- body.i32 (6 + 2 * (std::int32_t) name.size());
- const std::uint16_t head[3] = { 1, 1, (std::uint16_t) name.size() };
- body.raw (reinterpret_cast<const char*> (head), 6);
- for (char ch : name)
- body.raw (&ch, 1), body.zeros (1);
- body.raw ("DATA", 4);
- body.i32 (176);
- body.raw ("0rev", 4);
- for (auto v : P)
- body.f32 (v);
- Bytes s;
- s.raw ("SSLF", 4);
- s.i32 ((std::int32_t) body.b.size());
- s.bytes (body.b);
- return s.b;
- }
- std::array<float, 43> miniSynthDefaults()
- {
- 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,
- 0.5f, 0, 0.25f, 0.5f, 0.5f, 0, 0.11222222f, 0.4f, 0.1f, 1, 0.5f };
- }
- // a synthetic oscdata.dat (amplitudes 0.3/k, phases −π/2 + 0.001·(k − 1)): the test does not need an FL installation
- std::string miniSynthTestOscdata()
- {
- const auto path = (std::filesystem::temp_directory_path() / "lynx_test_oscdata.dat").string();
- std::vector<float> d (2048);
- for (int h = 0; h < 1024; ++h)
- d[(std::size_t) h] = (float) (0.3 / (h + 1)), d[1024 + (std::size_t) h] = (float) (-1.5707963267948966 + 0.001 * h);
- std::ofstream out (path, std::ios::binary);
- out.write (reinterpret_cast<const char*> (d.data()), 8192);
- return path;
- }
- } // namespace
- TEST (minisynth_engine)
- {
- auto A = miniSynthDefaults(), B = miniSynthDefaults();
- // A: poly, SQR+SQR, noise, filter env/velocity/keytracking, LFO → cutoff, distortion + decimator, delay, chorus, phaser
- 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 },
- { 12, 0.1f }, { 13, 0.4f }, { 14, 0.3f }, { 15, 0.5f }, { 16, 0.8f }, { 17, 0.6f }, { 18, 0.1f },
- { 19, 0.5f }, { 20, 0.85f }, { 21, 0.05f }, { 22, 0.5f }, { 23, 0.6f }, { 24, 0.4f }, { 25, 1 },
- { 26, 0.3f }, { 27, 0.1f }, { 28, 1 }, { 29, 0.4f }, { 30, 0.5f }, { 31, 0.6f }, { 32, 0.6f },
- { 33, 1 }, { 34, 0.4f }, { 35, 0.6f }, { 36, 0.5f }, { 37, 1 }, { 38, 0.3f }, { 39, 0.6f },
- { 40, 0.5f }, { 41, 0.7f }, { 42, 0.4f } };
- // B: lead (legato glide), BELL, LFO → pitch
- 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 },
- { 17, 0.1f }, { 18, 0.5f }, { 19, 0.6f }, { 20, 0.7f }, { 22, 0.6f }, { 23, 0.5f }, { 24, 0.3f } };
- for (const auto& [i, v] : a)
- A[(std::size_t) i] = v;
- for (const auto& [i, v] : b)
- B[(std::size_t) i] = v;
- const auto blobA = miniSynthTestState (A), blobB = miniSynthTestState (B);
- dumpIfAsked ("LYNX_DUMP_MINISYNTH", blobA);
- CHECK (lynx::miniSynthStateUsable (blobA) && lynx::miniSynthParams (blobA).size() == 43u && lynx::miniSynthParams (blobA)[26] == 0.3f);
- CHECK (lynx::miniSynthParamOffset (blobA, 9) == 8 + 8 + 8 + 6 + 18 + 12 + 4 * 9 && lynx::miniSynthParamOffset (blobA, 43) < 0);
- CHECK (! lynx::miniSynthStateUsable (std::vector<std::uint8_t> (blobA.begin(), blobA.begin() + 20)));
- auto shortData = blobA;
- shortData.resize (shortData.size() - 4 * 40); // only 3 parameters present: the rest keeps the defaults
- CHECK (lynx::miniSynthParams (shortData)[20] == 0.5f && lynx::miniSynthParams (shortData)[1] == 0.5f);
- // golden samples of our round-41 model (msmodel.Plugin on the same synthetic oscdata, 44.1 kHz, host blocks of 512,
- // L = R = Final.Vol): C5 vel 100 0–30000, E5 vel 80 11025–40000, G5 vel 110 22050–52000 (Init.Vol law A)
- const auto osc = miniSynthTestOscdata();
- auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
- const std::vector<lynx::minisynth::RefNote> notes = { { 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 } };
- struct Golden { int i; float l, r; };
- 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 } };
- 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 } };
- for (int which = 0; which < 2; ++which)
- {
- const auto y = lynx::minisynth::renderReference (which == 0 ? blobA : blobB, notes, 44100.0, 66150, osc);
- CHECK (y.size() == 2 * 66150u);
- if (y.size() != 2 * 66150u)
- continue;
- for (const auto& g : which == 0 ? std::vector (std::begin (goldenA), std::end (goldenA)) : std::vector (std::begin (goldenB), std::end (goldenB)))
- CHECK (y[2 * (std::size_t) g.i] == g.l && y[2 * (std::size_t) g.i + 1] == g.r);
- double e = 0.0;
- for (auto v : y)
- e += (double) v * v;
- CHECK (std::abs (std::sqrt (e / (double) y.size()) - (which == 0 ? 0.315552434392 : 0.03926116287)) < 1e-9);
- }
- // missing oscdata.dat: silent tables, still finite
- const auto silent = lynx::minisynth::renderReference (blobB, notes, 44100.0, 4096, "");
- CHECK (silent.size() == 2 * 4096u && std::all_of (silent.begin(), silent.end(), [] (float v) { return std::isfinite (v) && std::abs (v) < 1.0f; }));
- // the instrument: same engine behind LYNX's channel gains (V = 1, centre → law A), velocity → Init.Vol
- lynx::ChannelDef c;
- c.instrument = lynx::InstrumentKind::minisynth;
- c.pluginState = blobA;
- c.samplePath = osc;
- auto inst = lynx::makeInstrument (lynx::InstrumentKind::minisynth);
- inst->configure (c, 120.0);
- inst->prepare (44100.0);
- std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
- for (int k = 0; k < 16; ++k)
- {
- if (k == 0)
- inst->noteOn (60, 0.78125f);
- if (k == 10)
- inst->noteOff (60);
- inst->render (l.data() + 512 * k, r.data() + 512 * k, 512, 1.0f, 1.0f);
- }
- 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 } },
- 44100.0, 8192, osc, 512, 120.0, 0, -1.0, true);
- double diff = 0.0, energy = 0.0;
- for (int i = 0; i < 8192; ++i)
- {
- 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]) });
- energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
- }
- CHECK (diff == 0.0 && energy > 1e-3 && ! inst->idle());
- }
- // Workflow P2 (docs/WORKFLOW_FL.md)
- #include "WorkflowP2Tests.inc"
- TEST (fruity_delay_engines)
- {
- // REA_rodada52: the engines on their own against the installation's DLLs (goldens = the DLL's output for the same
- // input with FL's set-up order, EXEC: bit-identical), then the mixer effects built from FL states
- const int n = 16384;
- std::vector<float> inL ((std::size_t) n, 0.0f), inR ((std::size_t) n, 0.0f);
- std::uint32_t seed = 1;
- for (int i = 0; i < 4096; ++i) // a noise burst, then the echoes
- {
- seed = seed * 1664525u + 1013904223u;
- inL[(std::size_t) i] = (float) ((int) (seed >> 16) - 32768) / 32768.0f * 0.5f;
- seed = seed * 1664525u + 1013904223u;
- inR[(std::size_t) i] = (float) ((int) (seed >> 16) - 32768) / 32768.0f * 0.5f;
- }
- using Golden = std::vector<std::tuple<int, float, float>>;
- auto check = [&] (const std::vector<float>& l, const std::vector<float>& r, const Golden& golden) {
- for (const auto& [i, gl, gr] : golden)
- CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
- };
- auto run3 = [&] (const std::array<int, 26>& raw, double bpm) {
- lynx::fldelay::Delay3Engine e (44100.0);
- e.setTempo (bpm);
- for (int i = 0; i < 26; ++i)
- e.setParameter (i, lynx::fldelay::delay3EngineValue (i, raw[(std::size_t) i]));
- e.setSampleRate (44100.0); // FL's wrapper sets the rate up again after the state
- e.setSampleRate (44100.0);
- std::vector<float> l (inL), r (inR);
- for (int s = 0; s < n; s += 512)
- e.process (l.data() + s, r.data() + s, 512);
- return std::make_pair (l, r);
- };
- auto run2 = [&] (const std::array<int, 8>& raw, double bpm) {
- lynx::fldelay::Delay2State st;
- st.v = raw;
- lynx::fldelay::Delay2Engine e (st, 44100.0, bpm, 512);
- std::vector<float> l (inL), r (inR);
- for (int s = 0; s < n; s += 512)
- e.process (l.data() + s, r.data() + s, 512);
- return std::make_pair (l, r);
- };
- // Delay 3, true stereo, synced 1/8 beat, cutoff + resonance (Fruity Delay 3_x64.dll at 120 and 100 BPM)
- 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 };
- auto o = run3 (a, 120.0);
- check (o.first, o.second, { { 3000, 0.399949551f, 0.598533869f }, { 6000, 0.116703831f, -0.315510482f }, { 9000, -0.150520369f, -0.250620633f },
- { 13000, 0.193608373f, 0.0598564595f }, { 16000, -0.0524701327f, 0.022601109f } });
- o = run3 (a, 100.0);
- check (o.first, o.second, { { 5000, 0.377422422f, 0.346570343f }, { 6000, -0.182619616f, 0.0488029197f }, { 11000, -0.00120426295f, 0.0253051519f },
- { 16000, 0.0806550607f, -0.0991330743f } });
- // ping pong, keep pitch, modulation, cutoff LFO, diffusion, saturation, rate and bit reduction, colour
- 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);
- check (o.first, o.second, { { 1500, 0.288540125f, -0.148862943f }, { 5000, 5.90502168e-06f, 0.111382999f }, { 8000, -0.0736631379f, -0.0305093247f },
- { 11000, -0.00831047911f, 0.0102323527f }, { 16000, 0.0175791476f, 0.0273917634f } });
- // fake stereo, unsynced, high-pass, limit distortion, no dry
- 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);
- check (o.first, o.second, { { 1500, 0.0825810209f, 0.0228505582f }, { 6000, -0.0427959524f, -0.0408911295f }, { 9000, 0.0114790015f, 0.019227922f },
- { 16000, -0.000312746735f, 0.00101647642f } });
- // delay type "Off": filter, distortion and the bit/rate reduction on the dry path
- 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);
- check (o.first, o.second, { { 1500, 0.238686681f, -0.106820084f }, { 3000, 0.131528616f, 0.0665975735f }, { 5000, 9.94473215e-09f, -8.22819057e-09f },
- { 11000, 0.0f, 0.0f } });
- // Delay 2: default, ping pong with a stereo offset, swapped without dry (Fruity Delay 2_x64.dll)
- o = run2 ({ 0, 128, 128, 64, 36, 0, 0, 128 }, 120.0);
- check (o.first, o.second, { { 3000, 0.211380005f, 0.277908325f }, { 5000, -0.223615795f, -0.0189359933f }, { 9000, 0.120054141f, -0.00297877868f },
- { 16000, 0.0182533264f, 0.105384827f } });
- o = run2 ({ -40, 110, 90, 90, 24, -300, 2, 90 }, 100.0);
- check (o.first, o.second, { { 3000, 0.0929639712f, 0.122222826f }, { 5000, -0.0884733275f, -0.0507739782f }, { 11000, 0.0589251742f, 0.0f },
- { 16000, -0.029524127f, 0.0128910914f } });
- o = run2 ({ 60, 140, 0, 100, 30, 250, 1, 110 }, 120.0);
- check (o.first, o.second, { { 1500, 0.0f, 0.0f }, { 5000, 0.176379532f, -0.212506086f }, { 8000, -0.324207425f, 0.0675852001f },
- { 13000, 0.145023212f, -0.104788139f } });
- // the mixer effects from FL states: time law (Delay 3 synced 192 = 1 beat; Delay 2 144 = 3/4 beat), settled start
- auto state3 = std::vector<std::uint8_t> (4 + 26 * 4, 0);
- 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 };
- state3[0] = 1;
- std::memcpy (state3.data() + 4, def3.data(), 26 * 4);
- const auto slot3 = lynx::effectFromFl ("Fruity Delay 3", state3, 120.0);
- CHECK (slot3 && slot3->kind == lynx::EffectKind::fruityDelay3);
- auto state2 = std::vector<std::uint8_t> (32, 0);
- const std::array<int, 8> def2 { 0, 128, 128, 0, 144, 0, 0, 128 };
- std::memcpy (state2.data(), def2.data(), 32);
- const auto slot2 = lynx::effectFromFl ("Fruity delay 2", state2, 120.0);
- CHECK (slot2 && slot2->kind == lynx::EffectKind::fruityDelay2);
- CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityDelay2, 4, slot2->params[4], 120.0) == "3:00");
- auto echoAt = [] (const lynx::EffectSlot& slot, double bpm) {
- auto fx = lynx::makeEffect (slot);
- fx->prepare (44100.0);
- std::vector<float> l (48000, 0.0f), r (48000, 0.0f);
- l[100] = 1.0f;
- for (int s = 0; s < 48000; s += 1024)
- fx->process (l.data() + s, r.data() + s, std::min (1024, 48000 - s), slot.params, bpm);
- int best = 200;
- for (int i = 200; i < 48000; ++i)
- if (std::abs (l[(std::size_t) i]) > std::abs (l[(std::size_t) best]))
- best = i;
- return best - 100;
- };
- if (slot3 && slot2)
- {
- CHECK (echoAt (*slot3, 120.0) == 22050);
- CHECK (echoAt (*slot3, 150.0) == 17640);
- CHECK (echoAt (*slot2, 120.0) == 16538);
- CHECK (echoAt (*slot2, 140.0) == 14175);
- }
- }
|