tests.cpp 605 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771
  1. // Core tests (no JUCE). Usage: lynx_tests [folder-with-flp-files]
  2. #include <atomic>
  3. #include <cctype>
  4. #include <cmath>
  5. #include <cstdio>
  6. #include <cstring>
  7. #include <filesystem>
  8. #include <map>
  9. #include <set>
  10. #include <sstream>
  11. #include <fstream>
  12. #include <functional>
  13. #include <iterator>
  14. #include <string>
  15. #include <tuple>
  16. #include <vector>
  17. #include "core/Dither.h"
  18. #include "core/DrumSynthLive.h"
  19. #include "core/Effects.h"
  20. #include "core/Effector.h"
  21. #include "core/FruityDelayBank.h"
  22. #include "core/FruityMultiband.h"
  23. #include "core/Denormals.h"
  24. #include "core/FlpImport.h"
  25. #include "core/FlEffects.h"
  26. #include "core/FlPatcher.h"
  27. #include "core/FlSamplers.h"
  28. #include "core/FlSynths.h"
  29. #include "core/Inflate.h"
  30. #include "core/WavPack.h"
  31. #include "core/FlWrapper.h"
  32. #include "core/Gms.h"
  33. #include "core/PoiZone.h"
  34. #include "core/MiniSynth.h"
  35. #include "core/SimSynth.h"
  36. #include "core/Wasp.h"
  37. #include "core/Plucked.h"
  38. #include "core/BeepMap.h"
  39. #include "core/Sawer.h"
  40. #include "core/Toxic.h"
  41. #include "core/Harmless.h"
  42. #include "core/Harmor.h"
  43. #include "core/Morphine.h"
  44. #include "core/Ogun.h"
  45. #include "core/BassDrum.h"
  46. #include "core/FruitKick.h"
  47. #include "core/Dx10.h"
  48. #include "core/Kepler.h"
  49. #include "core/ThreeOsc.h"
  50. #include "core/TransistorBass.h"
  51. #include "core/HarmlessFx.h"
  52. #include "core/MaximusFx.h"
  53. #include "core/Reeverb2.h"
  54. #include "core/LuxeVerb.h"
  55. #include "core/Denormals.h"
  56. #include "core/FruityDynamics.h"
  57. #include "core/FruityShapers.h"
  58. #include "core/ParametricEqFl.h"
  59. #include "core/FruityBasicFx.h"
  60. #include "core/EquoFx.h"
  61. #include "core/PitchShifterFl.h"
  62. #include "core/FruityDelay.h"
  63. #include "core/StereoFx.h"
  64. #include "core/FrequencySplitter.h"
  65. #include "core/ChorusFx.h"
  66. #include "core/GrossBeat.h"
  67. #include "core/FlangusFastDist.h"
  68. #include "core/LiveEventQueue.h"
  69. #include "core/RackRenderer.h"
  70. #include "core/Scheduler.h"
  71. #include "core/SnapshotExchange.h"
  72. #include "core/SoftClip.h"
  73. #include "core/Slicer.h"
  74. #include "core/SytrusEngine.h"
  75. #include "core/Templates.h"
  76. #include "core/Vocodex.h"
  77. #include "core/TimeStretch.h"
  78. #include "core/WorkerPool.h"
  79. #include "core/Song.h"
  80. #include "core/NoteEdit.h"
  81. #include "core/Recorder.h"
  82. #include "core/KeyMap.h"
  83. #include "core/RackOps.h"
  84. #include "core/ValueMath.h"
  85. #include "core/Workspace.h"
  86. #include "core/StartupSound.h"
  87. #include "flp/Flp.h"
  88. #include "ui/I18nData.h"
  89. namespace
  90. {
  91. int failures = 0;
  92. int checks = 0;
  93. std::vector<std::pair<const char*, std::function<void()>>>& registry()
  94. {
  95. static std::vector<std::pair<const char*, std::function<void()>>> r;
  96. return r;
  97. }
  98. struct Register
  99. {
  100. Register (const char* name, std::function<void()> fn) { registry().emplace_back (name, std::move (fn)); }
  101. };
  102. #define TEST(name) \
  103. static void name(); \
  104. static Register reg_##name (#name, name); \
  105. static void name()
  106. #define CHECK(cond) \
  107. do \
  108. { \
  109. ++checks; \
  110. if (! (cond)) \
  111. { \
  112. ++failures; \
  113. std::printf (" FAILED %s:%d: %s\n", __FILE__, __LINE__, #cond); \
  114. } \
  115. } while (false)
  116. std::string realFlpDir;
  117. // --- helpers for building synthetic .flp files -----------------------------
  118. struct FlpBuilder
  119. {
  120. std::vector<std::uint8_t> bytes;
  121. FlpBuilder (std::uint16_t ppq = 96)
  122. {
  123. put ("FLhd");
  124. u32 (6);
  125. u16 (0);
  126. u16 (1);
  127. u16 (ppq);
  128. put ("FLdt");
  129. u32 (0); // size is not used by the reader
  130. }
  131. void put (const char* s) { bytes.insert (bytes.end(), s, s + std::strlen (s)); }
  132. void u8 (std::uint8_t v) { bytes.push_back (v); }
  133. void u16 (std::uint16_t v) { u8 ((std::uint8_t) v); u8 ((std::uint8_t) (v >> 8)); }
  134. void u32 (std::uint32_t v) { u16 ((std::uint16_t) v); u16 ((std::uint16_t) (v >> 16)); }
  135. void varint (std::size_t n)
  136. {
  137. do
  138. {
  139. auto b = (std::uint8_t) (n & 0x7F);
  140. n >>= 7;
  141. u8 (n ? (std::uint8_t) (b | 0x80) : b);
  142. } while (n);
  143. }
  144. void data (std::uint8_t id, const std::vector<std::uint8_t>& payload)
  145. {
  146. u8 (id);
  147. varint (payload.size());
  148. bytes.insert (bytes.end(), payload.begin(), payload.end());
  149. }
  150. void ascii (std::uint8_t id, const std::string& s)
  151. {
  152. std::vector<std::uint8_t> p (s.begin(), s.end());
  153. p.push_back (0);
  154. data (id, p);
  155. }
  156. void utf16 (std::uint8_t id, const std::u16string& s)
  157. {
  158. std::vector<std::uint8_t> p;
  159. for (auto c : s)
  160. {
  161. p.push_back ((std::uint8_t) c);
  162. p.push_back ((std::uint8_t) (c >> 8));
  163. }
  164. p.push_back (0);
  165. p.push_back (0);
  166. data (id, p);
  167. }
  168. };
  169. std::vector<std::uint8_t> noteRecord (std::uint32_t pos, std::uint16_t channel, std::uint32_t len, std::uint16_t key,
  170. std::uint8_t velocity)
  171. {
  172. std::vector<std::uint8_t> r (24, 0);
  173. std::memcpy (&r[0], &pos, 4);
  174. std::memcpy (&r[6], &channel, 2);
  175. std::memcpy (&r[8], &len, 4);
  176. std::memcpy (&r[12], &key, 2);
  177. r[16] = 120;
  178. r[20] = 64;
  179. r[21] = velocity;
  180. return r;
  181. }
  182. } // namespace
  183. // ---------------------------------------------------------------------------
  184. // FLP
  185. TEST (flp_rejects_garbage)
  186. {
  187. std::vector<std::uint8_t> junk (64, 0x41);
  188. bool threw = false;
  189. try { flp::EventReader r (junk); } catch (const flp::Error&) { threw = true; }
  190. CHECK (threw);
  191. }
  192. TEST (flp_reads_size_classes_varint_and_extended_ids)
  193. {
  194. FlpBuilder b;
  195. b.u8 (0x15); b.u8 (2); // u8
  196. b.u8 (0x40); b.u16 (513); // u16
  197. b.u8 (0x9C); b.u32 (128500); // u32
  198. b.data (0xD5, std::vector<std::uint8_t> (300, 7)); // 2-byte varint
  199. b.u8 (0xAC); b.u32 (0xC0000100); b.varint (3); b.u8 (1); b.u8 (2); b.u8 (3); // extended ID
  200. flp::EventReader r (b.bytes);
  201. flp::Event e;
  202. CHECK (r.header().ppq == 96);
  203. CHECK (r.next (e) && e.id == 0x15 && e.value == 2);
  204. CHECK (r.next (e) && e.id == 0x40 && e.value == 513);
  205. CHECK (r.next (e) && e.id == 0x9C && e.value == 128500);
  206. CHECK (r.next (e) && e.id == 0xD5 && e.data.size() == 300 && e.data[299] == 7);
  207. CHECK (r.next (e) && e.id == 0xC0000100 && e.isData() && e.data.size() == 3 && e.data[2] == 3);
  208. CHECK (! r.next (e));
  209. }
  210. TEST (flp_truncated_file_throws)
  211. {
  212. FlpBuilder b;
  213. b.u8 (0x9C); b.u8 (1); // u32 with only 1 byte
  214. flp::EventReader r (b.bytes);
  215. flp::Event e;
  216. bool threw = false;
  217. try { r.next (e); } catch (const flp::Error&) { threw = true; }
  218. CHECK (threw);
  219. }
  220. TEST (flp_version_numbers)
  221. {
  222. CHECK (flp::parseVersionNumber ("25.2.5.5319") == 25002005u);
  223. CHECK (flp::parseVersionNumber ("20.99.3000.3031") == 20102000u);
  224. CHECK (flp::parseVersionNumber ("11.0.3") == 11000003u);
  225. }
  226. TEST (flp_playlist_record_size_by_version)
  227. {
  228. CHECK (flp::playlistRecordSize (12009003, 64) == 32);
  229. CHECK (flp::playlistRecordSize (21000000, 120) == 60);
  230. CHECK (flp::playlistRecordSize (25002005, 160) == 80);
  231. CHECK (flp::playlistRecordSize (25002005, 60) == 60); // size doesn't match the version -> tries the others
  232. CHECK (flp::playlistRecordSize (25002005, 7) == 0);
  233. }
  234. TEST (flp_parses_project_model)
  235. {
  236. FlpBuilder b (96);
  237. b.ascii (0xC7, "25.2.5.5319");
  238. b.u8 (0x9C); b.u32 (174500);
  239. b.utf16 (0xC2, u"Título");
  240. b.u8 (0x41); b.u16 (3);
  241. b.u8 (0x96); b.u32 (0x00A14547); // BGR in the file
  242. auto notes = noteRecord (96, 1, 48, 64, 100);
  243. auto n2 = noteRecord (0, 0, 0, 60, 128);
  244. notes.insert (notes.end(), n2.begin(), n2.end());
  245. b.data (0xE0, notes);
  246. b.u8 (0x40); b.u16 (0);
  247. b.u8 (0x15); b.u8 (0);
  248. b.utf16 (0xCB, u"Kick");
  249. {
  250. std::vector<std::uint8_t> levels (24, 0);
  251. const std::int32_t lv[] = { 3200, 12800, -150 };
  252. std::memcpy (levels.data(), lv, sizeof (lv));
  253. b.data (0xDB, levels);
  254. }
  255. b.u8 (0x68); b.u16 (5);
  256. b.u8 (0x40); b.u16 (1);
  257. b.u8 (0x15); b.u8 (2);
  258. b.utf16 (0xC9, u"FLEX");
  259. b.utf16 (0xCB, u"Lead");
  260. b.u8 (0x63); b.u16 (0);
  261. std::vector<std::uint8_t> item (80, 0);
  262. std::uint32_t pos = 384, len = 768;
  263. std::uint16_t base = 20480, idx = 20480 + 3, rv = 499 - 2;
  264. std::memcpy (&item[0], &pos, 4);
  265. std::memcpy (&item[4], &base, 2);
  266. std::memcpy (&item[6], &idx, 2);
  267. std::memcpy (&item[8], &len, 4);
  268. std::memcpy (&item[12], &rv, 2);
  269. b.data (0xE9, item);
  270. b.utf16 (0xCC, u"Drums");
  271. b.data (0xEC, std::vector<std::uint8_t> (12, 0));
  272. b.data (0xEC, std::vector<std::uint8_t> (12, 0));
  273. const auto p = flp::parseProject (b.bytes);
  274. CHECK (p.version == "25.2.5.5319");
  275. CHECK (std::abs (p.tempo - 174.5) < 1e-9);
  276. CHECK (p.title == "Título");
  277. CHECK (p.channels.size() == 2);
  278. CHECK (p.channels[0].name == "Kick" && p.channels[0].mixerTrack == 5);
  279. CHECK (p.channels[0].hasLevels && p.channels[0].pan == 3200 && p.channels[0].volume == 12800 && p.channels[0].pitchCents == -150);
  280. CHECK (p.channels[1].type == flp::ChannelType::instrument && p.channels[1].pluginName == "FLEX");
  281. CHECK (p.patterns.size() == 1 && p.patterns[0].index == 3);
  282. CHECK (p.patterns[0].colour && *p.patterns[0].colour == 0x4745A1);
  283. CHECK (p.patterns[0].notes.size() == 2 && p.patterns[0].notes[0].position == 0); // sorted
  284. CHECK (p.patterns[0].notes[1].key == 64 && p.patterns[0].notes[1].length == 48);
  285. CHECK (p.arrangements.size() == 1 && p.arrangements[0].items.size() == 1);
  286. const auto& it = p.arrangements[0].items[0];
  287. CHECK (it.kind == flp::PlaylistItem::Kind::pattern && it.index == 3 && it.track == 2 && it.position == 384);
  288. CHECK (p.mixer.size() == 2 && p.mixer[0].name == "Drums" && p.mixer[1].name.empty());
  289. CHECK (p.warnings.empty());
  290. }
  291. TEST (flp_fuzz_corrupted_files_do_not_crash)
  292. {
  293. if (realFlpDir.empty())
  294. return;
  295. // some real .flp files, corrupted in various ways: the parser may reject them (flp::Error), but
  296. // must never hang, read past the buffer or produce a Song with invalid indices
  297. std::vector<std::vector<std::uint8_t>> sources;
  298. for (auto& entry : std::filesystem::recursive_directory_iterator (realFlpDir))
  299. if (entry.path().extension() == ".flp" && sources.size() < 6 && std::filesystem::file_size (entry.path()) < 3'000'000)
  300. {
  301. std::ifstream in (entry.path(), std::ios::binary);
  302. sources.emplace_back ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  303. }
  304. std::uint32_t seed = 2024;
  305. auto rnd = [&] (std::uint32_t n) { seed = seed * 1664525u + 1013904223u; return n == 0 ? 0u : (seed >> 8) % n; };
  306. int parsed = 0, rejected = 0;
  307. for (int round = 0; round < 240 && ! sources.empty(); ++round)
  308. {
  309. auto bytes = sources[(std::size_t) rnd ((std::uint32_t) sources.size())];
  310. switch (round % 4)
  311. {
  312. case 0: bytes.resize (rnd ((std::uint32_t) bytes.size())); break; // truncated
  313. 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;
  314. case 2: // absurd event size
  315. for (int k = 0; k < 5; ++k)
  316. bytes[(std::size_t) rnd ((std::uint32_t) bytes.size())] = 0xFF;
  317. break;
  318. default: // shuffled block
  319. {
  320. const auto a = rnd ((std::uint32_t) bytes.size()), b = rnd ((std::uint32_t) bytes.size());
  321. std::reverse (bytes.begin() + std::min (a, b), bytes.begin() + std::max (a, b));
  322. break;
  323. }
  324. }
  325. try
  326. {
  327. const auto project = flp::parseProject (bytes);
  328. const auto song = lynx::importFlp (project);
  329. ++parsed;
  330. for (const auto& p : song.patterns)
  331. for (const auto& n : p.notes)
  332. CHECK (n.channel >= 0 && n.channel < (int) song.channels.size());
  333. for (const auto& c : song.playlist)
  334. CHECK (c.kind != lynx::ClipKind::pattern || (c.pattern >= 0 && c.pattern < (int) song.patterns.size()));
  335. }
  336. catch (const flp::Error&)
  337. {
  338. ++rejected;
  339. }
  340. }
  341. std::printf (" fuzz: %d imported, %d rejected\n", parsed, rejected);
  342. CHECK (parsed + rejected == 240);
  343. }
  344. TEST (plugin_state_decoders_survive_garbage)
  345. {
  346. // random blobs (varied sizes, including valid headers followed by garbage)
  347. std::uint32_t seed = 77;
  348. auto rnd = [&] { seed = seed * 1664525u + 1013904223u; return seed >> 8; };
  349. const char* plugins[] = { "Fruity Parametric EQ 2", "Fruity Limiter", "Fruity Reeverb 2", "Fruity Delay 2", "Fruity Delay 3",
  350. "Fruity Compressor", "Fruity Balance", "Fruity PanOMatic", "Fruity Stereo Shaper", "Fruity Phase Inverter" };
  351. int decoded = 0;
  352. for (int round = 0; round < 3000; ++round)
  353. {
  354. std::vector<std::uint8_t> blob ((std::size_t) (rnd() % 600));
  355. for (auto& b : blob)
  356. b = (std::uint8_t) rnd();
  357. if (blob.size() >= 4 && round % 3 == 0) // versions the readers accept
  358. {
  359. const std::uint32_t versions[] = { 8, 10001, 10000, 12, 14, 6, 1 };
  360. const auto v = versions[rnd() % 7];
  361. std::memcpy (blob.data(), &v, 4);
  362. }
  363. if (auto fx = lynx::effectFromFl (plugins[rnd() % 10], blob))
  364. {
  365. ++decoded;
  366. for (auto v : fx->params)
  367. CHECK (std::isfinite (v) && v >= 0.0f && v <= 1.0f);
  368. }
  369. lynx::SynthSettings synth;
  370. if (lynx::synthFrom3xOsc (blob, synth))
  371. for (const auto& o : synth.osc)
  372. CHECK (std::isfinite (o.level) && o.level >= 0.0f && o.level <= 1.0f);
  373. float harmlessGain = 1.0f;
  374. if (lynx::synthFromHarmless (blob, 120.0, synth, harmlessGain))
  375. CHECK (std::isfinite (harmlessGain) && std::isfinite (synth.cutoff) && std::isfinite (synth.attack));
  376. if (lynx::synthFromFruitKick (blob, synth))
  377. CHECK (std::isfinite (synth.decay) && std::isfinite (synth.pitchEnv.amount));
  378. if (lynx::synthFromGms (blob, 120.0, synth, harmlessGain) || lynx::synthFromHarmor (blob, synth, harmlessGain) || lynx::synthFromOgun (blob, synth, harmlessGain)
  379. || lynx::synthFromSakura (blob, synth) || lynx::synthFromKepler (blob, synth, harmlessGain))
  380. CHECK (std::isfinite (synth.cutoff) && std::isfinite (synth.attack) && std::isfinite (harmlessGain));
  381. if (auto sound = lynx::drumSynthLiveSound (blob))
  382. CHECK (sound->size() < 256);
  383. if (auto wave = lynx::waveTravellerSample (blob))
  384. CHECK (! wave->first.empty() && wave->second >= 0 && wave->second <= 127);
  385. lynx::synthFromBooBass (blob, synth);
  386. lynx::gmsStateUsable (blob); // (the exact engines' parsers reject garbage without crashing)
  387. lynx::morphineStateUsable (blob);
  388. lynx::poizoneStateUsable (blob);
  389. if (lynx::synthFromPoiZone (blob, synth, harmlessGain) || lynx::synthFromBassDrum (blob, synth, harmlessGain) || lynx::synthFromToxic (blob, synth, harmlessGain)
  390. || lynx::synthFromMorphine (blob, synth, harmlessGain))
  391. CHECK (std::isfinite (synth.cutoff) && std::isfinite (harmlessGain));
  392. if (auto patch = lynx::drumpadPatchName (blob))
  393. CHECK (patch->size() <= 0x30 && std::isfinite (lynx::drumToneForName (*patch).decay));
  394. if (auto flex = lynx::flexPresetName (blob))
  395. CHECK ((! flex->first.empty() || ! flex->second.empty()) && flex->second.size() <= 1024);
  396. if (lynx::synthFromSytrus (blob, 120.0, synth, harmlessGain))
  397. CHECK (std::isfinite (synth.cutoff) && std::isfinite (synth.attack));
  398. if (lynx::synthFromDX10 (blob, synth))
  399. CHECK (std::isfinite (synth.decay) && std::isfinite (synth.release) && std::isfinite (synth.osc[1].level));
  400. if (blob.size() >= 4 && round % 5 == 0) // MiniSynth header followed by garbage
  401. {
  402. std::memcpy (blob.data(), "SSLF", 4);
  403. if (blob.size() > 220)
  404. {
  405. std::memcpy (blob.data() + 40, "DATA", 4);
  406. std::memcpy (blob.data() + 48, "0rev", 4);
  407. }
  408. }
  409. if (lynx::synthFromMiniSynth (blob, 120.0, synth))
  410. CHECK (std::isfinite (synth.cutoff) && std::isfinite (synth.decay) && std::isfinite (synth.attack));
  411. if (lynx::miniSynthStateUsable (blob)) // the exact engine's parser and a short render with silent tables
  412. {
  413. const auto params = lynx::miniSynthParams (blob);
  414. CHECK (params.size() == 43u && std::all_of (params.begin(), params.end(), [] (float v) { return v >= 0.0f && v <= 1.0f; }));
  415. const auto y = lynx::minisynth::renderReference (blob, { { 0, 60, 1.0, 1.0, 0.0, 0.0, 64 } }, 44100.0, 128, "");
  416. CHECK (y.size() == 256u && std::all_of (y.begin(), y.end(), [] (float v) { return std::isfinite (v); }));
  417. }
  418. lynx::ForeignPlugin foreign;
  419. lynx::parseFlWrapper (blob, foreign);
  420. std::vector<std::uint8_t> component, controller;
  421. lynx::splitFlVst3State (blob, component, controller);
  422. }
  423. CHECK (decoded == 3000); // every known name yields an effect (default when the blob is unusable)
  424. }
  425. TEST (inflate_zlib)
  426. {
  427. // fixed Huffman with back-references
  428. const std::uint8_t fixed[] = { 0x78, 0xda, 0x4b, 0x4c, 0x4a, 0x4e, 0xc4, 0x86, 0x22, 0x00, 0x7c, 0x69, 0x09, 0x89 };
  429. const auto a = lynx::inflateZlib (fixed, sizeof (fixed));
  430. CHECK (a && std::string (a->begin(), a->end()) == "abcabcabcabcabcabcabcabcX");
  431. // stored (uncompressed) block
  432. const std::uint8_t stored[] = { 0x78, 0x01, 0x01, 0x03, 0x00, 0xfc, 0xff, 'L', 'Y', 'N', 0x00, 0x00, 0x00, 0x00 };
  433. const auto b = lynx::inflateZlib (stored, sizeof (stored));
  434. CHECK (b && std::string (b->begin(), b->end()) == "LYN");
  435. // truncated, bad header and size limit
  436. CHECK (! lynx::inflateZlib (fixed, 6));
  437. const std::uint8_t bad[] = { 0x12, 0x34, 0x00 };
  438. CHECK (! lynx::inflateZlib (bad, sizeof (bad)));
  439. CHECK (! lynx::inflateZlib (fixed, sizeof (fixed), false, 10));
  440. }
  441. TEST (fl_round15_effects_convert_known_values)
  442. {
  443. auto blob = [] (std::vector<std::int32_t> words) {
  444. std::vector<std::uint8_t> b (words.size() * 4);
  445. std::memcpy (b.data(), words.data(), b.size());
  446. return b;
  447. };
  448. // Frequency Shifter "+800 Hz": frequency 51542 in the 20 kHz range, 100% wet
  449. const auto fs = lynx::effectFromFl ("Frequency Shifter", blob ({ 4, 10000, 0, 51542, 0, 10000, 10000, 5000, 10000, 0, 5000, 0, 0 }));
  450. CHECK (fs && fs->kind == lynx::EffectKind::freqShift);
  451. if (fs)
  452. {
  453. const auto x = (fs->params[0] - 0.5) * 2.0;
  454. CHECK (std::abs (2000.0 * x * std::abs (x) - 800.0) < 10.0);
  455. CHECK (std::abs (fs->params[1] - 1.0f) < 1e-4f);
  456. }
  457. // Emphasis: input 0.0625 (= +1.5 dB) and ceiling 0.9308 (≈ −1.6 dB) -> limiter
  458. const auto em = lynx::effectFromFl ("Emphasis", blob ({ 8, 0, 0, 2, 1, 0, 0, 625, 10000, 10000, 9308, 0, 2, 7, 0 }));
  459. CHECK (em && em->kind == lynx::EffectKind::limiter && em->enabled);
  460. if (em)
  461. {
  462. CHECK (std::abs (em->params[1] * 24.0f - 1.5f) < 0.05f);
  463. CHECK (std::abs (em->params[0] * 12.0f - 12.0f + 1.6f) < 0.1f);
  464. }
  465. // Pitch Shifter +12 st, mix 100%
  466. 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 }));
  467. CHECK (ps && ps->kind == lynx::EffectKind::flPitchShifter && ps->params[0] == 1.0f && ps->params[3] == 1.0f); // REA_rodada61
  468. }
  469. TEST (frequency_shifter_moves_spectrum_by_hz)
  470. {
  471. auto fx = lynx::makeEffect (lynx::EffectKind::freqShift);
  472. fx->prepare (48000.0);
  473. auto slot = lynx::makeEffectSlot (lynx::EffectKind::freqShift);
  474. // +200 Hz: x·|x|·2000 = 200 -> x = sqrt(0.1)
  475. slot.params[0] = (float) (0.5 + std::sqrt (0.1) / 2.0);
  476. std::vector<float> l (48000), r (48000);
  477. for (int i = 0; i < 48000; ++i)
  478. l[(std::size_t) i] = r[(std::size_t) i] = (float) std::sin (2.0 * 3.141592653589793 * 1000.0 * i / 48000.0);
  479. for (int done = 0; done < 48000; done += 480)
  480. fx->process (l.data() + done, r.data() + done, 480, slot.params, 120.0);
  481. auto energy = [&] (double f) {
  482. const auto w = 2.0 * 3.141592653589793 * f / 48000.0, k = 2.0 * std::cos (w);
  483. double s1 = 0.0, s2 = 0.0;
  484. for (int i = 9600; i < 48000; ++i)
  485. {
  486. const auto s0 = l[(std::size_t) i] + k * s1 - s2;
  487. s2 = s1;
  488. s1 = s0;
  489. }
  490. return s1 * s1 + s2 * s2 - k * s1 * s2;
  491. };
  492. // the energy moves to 1200 Hz; little remains at 1000 and almost nothing at the image (800)
  493. CHECK (energy (1200.0) > 50.0 * energy (1000.0));
  494. CHECK (energy (1200.0) > 50.0 * energy (800.0));
  495. }
  496. TEST (pitch_shift_moves_frequency_keeps_length)
  497. {
  498. auto fx = lynx::makeEffect (lynx::EffectKind::pitchShift);
  499. fx->prepare (48000.0);
  500. auto slot = lynx::makeEffectSlot (lynx::EffectKind::pitchShift);
  501. auto crossingsAfter = [&] (float semitoneParam) {
  502. fx->reset();
  503. slot.params[0] = semitoneParam;
  504. std::vector<float> l (48000), r (48000);
  505. for (int i = 0; i < 48000; ++i)
  506. l[(std::size_t) i] = r[(std::size_t) i] = (float) std::sin (2.0 * 3.141592653589793 * 220.0 * i / 48000.0);
  507. for (int done = 0; done < 48000; done += 512)
  508. fx->process (l.data() + done, r.data() + done, std::min (512, 48000 - done), slot.params, 120.0);
  509. int crossings = 0;
  510. for (int i = 9600; i < 48000; ++i)
  511. crossings += (l[(std::size_t) i - 1] < 0.0f) != (l[(std::size_t) i] < 0.0f);
  512. for (auto v : l)
  513. CHECK (std::isfinite (v) && std::abs (v) < 1.6f);
  514. return crossings / 0.8 / 2.0; // cycles per second
  515. };
  516. CHECK (std::abs (crossingsAfter (0.5f) - 220.0) < 8.0); // 0 st
  517. CHECK (std::abs (crossingsAfter (0.5f + 12.0f / 48.0f) - 440.0) < 25.0); // +12 st
  518. CHECK (std::abs (crossingsAfter (0.5f - 12.0f / 48.0f) - 110.0) < 12.0); // −12 st
  519. }
  520. // A Gross Beat state in FL's layout (REA_rodada53 §1), built here: version 8, parameters, options, 72 slots with a name,
  521. // sync bytes and a map envelope (kind 3, version 3, points: Δx in beats, level, tension, curve type)
  522. namespace
  523. {
  524. struct GbPoint
  525. {
  526. double dx, y;
  527. std::uint8_t type;
  528. };
  529. std::vector<std::uint8_t> grossBeatState (const std::array<int, 9>& params, const std::array<std::uint8_t, 9>& options,
  530. const std::map<int, std::vector<GbPoint>>& slots)
  531. {
  532. std::vector<std::uint8_t> b;
  533. auto put = [&b] (const void* p, std::size_t n) { b.insert (b.end(), (const std::uint8_t*) p, (const std::uint8_t*) p + n); };
  534. auto i32 = [&put] (std::int32_t v) { put (&v, 4); };
  535. i32 (0x00080003);
  536. for (auto v : params)
  537. i32 (v);
  538. put (options.data(), options.size());
  539. for (int k = 0; k < 72; ++k)
  540. {
  541. b.push_back (0); // empty name
  542. i32 (0), i32 (5), i32 (0); // trigger sync off, position sync 4 beats, no hold
  543. b.push_back (0), b.push_back (0), b.push_back (0);
  544. const auto it = slots.find (k);
  545. const std::vector<GbPoint> flat { { 0.0, 1.0, 0 }, { 4.0, 1.0, 0 } };
  546. const auto& pts = it != slots.end() ? it->second : flat;
  547. i32 (3), i32 (3), i32 ((std::int32_t) pts.size());
  548. for (const auto& p : pts)
  549. {
  550. put (&p.dx, 8), put (&p.y, 8);
  551. const float tension = 0.0f;
  552. put (&tension, 4);
  553. b.push_back (p.type), b.push_back (0), b.push_back (0), b.push_back (2);
  554. }
  555. i32 (0), i32 (-1), i32 (-1), i32 (-1), i32 (-1);
  556. }
  557. return b;
  558. }
  559. std::vector<float> renderGrossBeat (const lynx::EffectSlot& slot, const std::vector<float>& in, double rate, double bpm)
  560. {
  561. auto fx = lynx::makeEffect (slot);
  562. fx->prepare (rate);
  563. auto l = in, r = in;
  564. const auto tps = bpm / 60.0 * lynx::ppq / rate;
  565. for (std::size_t done = 0; done < l.size(); done += 512)
  566. {
  567. const auto n = (int) std::min<std::size_t> (512, l.size() - done);
  568. fx->setSongPosition ((double) done * tps);
  569. fx->process (l.data() + done, r.data() + done, n, slot.params, bpm);
  570. }
  571. return l;
  572. }
  573. } // namespace
  574. TEST (gross_beat_state_container_and_parameters)
  575. {
  576. const auto state = grossBeatState ({ 3, 40, 128, 256, 1765, 5000, -77, 0, 0x10000 }, { 0, 0, 0, 0, 0, 0, 0, 1, 1 }, {});
  577. const auto parsed = lynx::grossbeat::parseState (state.data(), state.size());
  578. CHECK (parsed.has_value());
  579. CHECK (parsed && parsed->version == 8 && parsed->params[6] == -77 && parsed->slots[0].flat && parsed->slots[71].flat);
  580. const auto slot = lynx::grossbeat::slotFromFl (state, 960);
  581. CHECK (slot && slot->kind == lynx::EffectKind::grossBeat);
  582. std::vector<std::uint8_t> fl;
  583. int ppq = 0;
  584. CHECK (slot && lynx::grossbeat::unpack (slot->state, fl, ppq) && fl == state && ppq == 960);
  585. // every raw FL value survives the 0..1 parameter
  586. const int lo[8] { 0, 0, 0, 0, 0, 0, -256, 0 }, hi[8] { 35, 35, 256, 256, 10000, 10000, 256, 1 };
  587. bool roundTrip = true;
  588. for (int i = 0; i < 8; ++i)
  589. for (int raw = lo[i]; raw <= hi[i]; ++raw)
  590. roundTrip = roundTrip && lynx::grossbeat::rawFromParam (i, lynx::grossbeat::paramFromRaw (i, raw)) == raw;
  591. CHECK (roundTrip);
  592. CHECK (slot && slot->params[1] == lynx::grossbeat::paramFromRaw (1, 35)); // the volume slot 40 is clamped to 35
  593. CHECK (! lynx::grossbeat::parseState (state.data(), 100).has_value());
  594. }
  595. TEST (gross_beat_volume_slot_gates_the_bar)
  596. {
  597. // volume slot 1: open for two beats, closed for two (hold segments); no smoothing; dry path 15 samples late
  598. const std::vector<GbPoint> gate { { 0.0, 1.0, 0 }, { 2.0, 1.0, 2 }, { 0.0, 0.0, 0 }, { 2.0, 0.0, 2 } };
  599. const auto state = grossBeatState ({ 0, 1, 256, 256, 0, 0, 0, 0, 0x10000 }, { 0, 0, 0, 0, 0, 0, 0, 1, 1 }, { { 37, gate } });
  600. const auto slot = lynx::grossbeat::slotFromFl (state);
  601. CHECK (slot.has_value());
  602. if (! slot)
  603. return;
  604. const auto out = renderGrossBeat (*slot, std::vector<float> (200000, 1.0f), 48000.0, 120.0); // a beat = 24000 samples
  605. CHECK (out[14] == 0.0f && out[15] == 1.0f);
  606. CHECK (out[30000] == 1.0f && out[47990] == 1.0f);
  607. CHECK (out[48010] == 0.0f && out[95990] == 0.0f);
  608. CHECK (out[96010] == 1.0f && out[150000] == 0.0f);
  609. }
  610. TEST (gross_beat_time_slot_reads_the_past_bar)
  611. {
  612. // time slot 1 at level 0.5: the buffer is read one bar back (two bars = level 0), plus the 15-sample latency
  613. const std::vector<GbPoint> half { { 0.0, 0.5, 0 }, { 4.0, 0.5, 2 } };
  614. const auto state = grossBeatState ({ 1, 0, 256, 256, 0, 0, 0, 0, 0x10000 }, { 0, 0, 0, 0, 0, 0, 0, 1, 1 }, { { 1, half } });
  615. const auto slot = lynx::grossbeat::slotFromFl (state);
  616. CHECK (slot.has_value());
  617. if (! slot)
  618. return;
  619. std::vector<float> in (150000, 0.0f);
  620. in[1000] = 1.0f;
  621. const auto out = renderGrossBeat (*slot, in, 48000.0, 120.0); // a bar = 96000 samples
  622. std::size_t peak = 0;
  623. for (std::size_t i = 0; i < out.size(); ++i)
  624. if (std::abs (out[i]) > std::abs (out[peak]))
  625. peak = i;
  626. CHECK (peak == 1000 + 96000 + 15);
  627. CHECK (std::abs (out[peak] - 1.0f) < 1e-6f);
  628. float elsewhere = 0.0f;
  629. for (std::size_t i = 0; i < out.size(); ++i)
  630. if (i + 20 < peak || i > peak + 20)
  631. elsewhere = std::max (elsewhere, std::abs (out[i]));
  632. CHECK (elsewhere == 0.0f);
  633. }
  634. TEST (volume_gate_follows_bar_position)
  635. {
  636. auto fx = lynx::makeEffect (lynx::EffectKind::volumeGate);
  637. fx->prepare (48000.0);
  638. auto slot = lynx::makeEffectSlot (lynx::EffectKind::volumeGate);
  639. for (int i = 0; i < 32; ++i)
  640. slot.params[(std::size_t) i] = i < 16 ? 1.0f : 0.0f; // first two beats open, two closed
  641. slot.params[33] = slot.params[34] = 0.0f; // no smoothing
  642. // 120 BPM: one beat = 0.5 s = 24000 samples; the 4-beat bar starts at tick 0
  643. std::vector<float> l (96000, 1.0f), r (96000, 1.0f);
  644. fx->setSongPosition (0.0);
  645. for (int done = 0; done < 96000; done += 512)
  646. fx->process (l.data() + done, r.data() + done, std::min (512, 96000 - done), slot.params, 120.0);
  647. CHECK (l[10000] > 0.99f && l[40000] > 0.99f); // beats 1 and 2
  648. CHECK (l[60000] < 0.01f && l[90000] < 0.01f); // beats 3 and 4
  649. // starting mid-bar (beat 3): already closed
  650. std::vector<float> l2 (4800, 1.0f), r2 (4800, 1.0f);
  651. fx->reset();
  652. fx->setSongPosition (2.0 * lynx::ppq);
  653. fx->process (l2.data(), r2.data(), 4800, slot.params, 120.0);
  654. CHECK (l2[4000] < 0.01f);
  655. // mix 0: transparent
  656. slot.params[32] = 0.0f;
  657. std::vector<float> l3 (4800, 1.0f), r3 (4800, 1.0f);
  658. fx->setSongPosition (2.0 * lynx::ppq);
  659. fx->process (l3.data(), r3.data(), 4800, slot.params, 120.0);
  660. CHECK (l3[4000] > 0.99f);
  661. }
  662. TEST (patcher_branch_heads_follow_rea)
  663. {
  664. auto words = [] (std::vector<std::int32_t> v) {
  665. std::vector<std::uint8_t> b (v.size() * 4);
  666. std::memcpy (b.data(), v.data(), b.size());
  667. return b;
  668. };
  669. // Frequency Splitter, 2 bands, destinations 1/2/3, slope combo 3 (24 dB/oct), high send −6 dB
  670. std::vector<std::int32_t> fs (18, 0);
  671. fs[0] = 4;
  672. fs[1] = 32768;
  673. fs[3] = 3;
  674. fs[6] = 0;
  675. fs[11] = -600;
  676. fs[15] = 1;
  677. fs[16] = 2;
  678. fs[17] = 3;
  679. const auto split = words (fs);
  680. const auto low = lynx::patcherBranchHead ("Frequency Splitter", split, 1);
  681. CHECK (low.valid && low.slot && low.note.empty());
  682. if (low.slot)
  683. {
  684. // the exact engine (REA_rodada62): the low band's send, 24 dB/oct
  685. CHECK (low.slot->kind == lynx::EffectKind::frequencySplitter && low.slot->params[26] == 1.0f && low.slot->params[27] == 0.0f);
  686. CHECK (low.slot->params[2] == 3.0f / 7.0f);
  687. }
  688. const auto mid = lynx::patcherBranchHead ("Frequency Splitter", split, 2);
  689. CHECK (mid.valid && mid.slot && mid.slot->params[27] == 1.0f && mid.slot->params[5] == 0.0f); // 2 bands: the engine's mid is silent
  690. const auto high = lynx::patcherBranchHead ("Frequency Splitter", split, 3);
  691. CHECK (high.valid && high.slot && high.slot->params[28] == 1.0f && high.slot->params[10] == (float) (1200.0 / 3600.0));
  692. CHECK (! lynx::patcherBranchHead ("Frequency Splitter", split, 7).valid);
  693. // Stereo Shaper: the difference destination (4) is the exact engine with its difference output (REA_rodada54)
  694. const auto shaper = words ({ 3, 0, 12800, 12800, 0, 0, 0, 4, 0 });
  695. const auto diff = lynx::patcherBranchHead ("Fruity Stereo Shaper", shaper, 4);
  696. CHECK (diff.valid && diff.slot && diff.slot->kind == lynx::EffectKind::stereoShaper && diff.slot->params[7] == 1.0f);
  697. CHECK (! lynx::patcherBranchHead ("Fruity Stereo Shaper", shaper, 2).valid);
  698. }
  699. TEST (parallel_container_sums_branches)
  700. {
  701. // two branches: dry with gain 0.5 and a utility that flips phase (matrix −1) with gain 0.5 -> silence
  702. auto slot = lynx::makeEffectSlot (lynx::EffectKind::parallel);
  703. lynx::EffectBranch dry;
  704. dry.gain = 0.5f;
  705. lynx::EffectBranch inverted;
  706. inverted.gain = 0.5f;
  707. auto flip = lynx::makeEffectSlot (lynx::EffectKind::utility);
  708. flip.params[2] = flip.params[5] = 0.25f; // (−1 + 2)/4
  709. inverted.chain.push_back (flip);
  710. slot.branches = { dry, inverted };
  711. auto fx = lynx::makeParallelEffect (slot);
  712. fx->prepare (48000.0);
  713. std::vector<float> l (9000, 0.8f), r (9000, -0.4f);
  714. fx->process (l.data(), r.data(), 9000, slot.params, 120.0);
  715. CHECK (std::abs (l[100]) < 1e-5f && std::abs (r[8999]) < 1e-5f);
  716. // only the dry branch with gain 2: doubles
  717. slot.branches = { lynx::EffectBranch { {}, 2.0f } };
  718. auto doubler = lynx::makeParallelEffect (slot);
  719. doubler->prepare (48000.0);
  720. std::fill (l.begin(), l.end(), 0.25f);
  721. doubler->process (l.data(), r.data(), 9000, slot.params, 120.0);
  722. CHECK (std::abs (l[5000] - 0.5f) < 1e-6f);
  723. }
  724. TEST (fl_controller_constant_outputs)
  725. {
  726. std::vector<std::int32_t> xy { 2, 32768, 0, 65536, 0, 16384, 0, 65536, 0, 1024, 256 };
  727. std::vector<std::uint8_t> blob (xy.size() * 4 + 8, 0);
  728. std::memcpy (blob.data(), xy.data(), xy.size() * 4);
  729. CHECK (std::abs (lynx::controllerConstantOutput ("Fruity X-Y Controller", blob, 0).value_or (-1.0f) - 0.5f) < 1e-4f);
  730. CHECK (std::abs (lynx::controllerConstantOutput ("Fruity X-Y Controller", blob, 1).value_or (-1.0f) - 0.25f) < 1e-4f);
  731. // Control Surface: version + group { 0x836: value 0.75, -, output 2 }
  732. std::vector<std::uint8_t> cs (4, 0);
  733. cs[0] = 1;
  734. auto chunk = [] (std::vector<std::uint8_t>& out, std::uint32_t id, const std::vector<std::uint8_t>& body) {
  735. const auto size = (std::uint64_t) body.size();
  736. out.insert (out.end(), reinterpret_cast<const std::uint8_t*> (&id), reinterpret_cast<const std::uint8_t*> (&id) + 4);
  737. out.insert (out.end(), reinterpret_cast<const std::uint8_t*> (&size), reinterpret_cast<const std::uint8_t*> (&size) + 8);
  738. out.insert (out.end(), body.begin(), body.end());
  739. };
  740. std::vector<std::uint8_t> value (12, 0);
  741. const float v = 0.75f;
  742. const std::int32_t out = 2;
  743. std::memcpy (value.data(), &v, 4);
  744. std::memcpy (value.data() + 8, &out, 4);
  745. std::vector<std::uint8_t> group;
  746. chunk (group, 0x836, value);
  747. chunk (cs, 0, group);
  748. CHECK (std::abs (lynx::controllerConstantOutput ("Control Surface", cs, 2).value_or (-1.0f) - 0.75f) < 1e-6f);
  749. CHECK (! lynx::controllerConstantOutput ("Control Surface", cs, 0).has_value());
  750. }
  751. TEST (flp_midi_out_notes_reach_wrapper_on_same_port)
  752. {
  753. auto record = [] (std::vector<std::uint8_t>& out, std::uint32_t id, std::vector<std::uint8_t> body) {
  754. const auto size = (std::uint64_t) body.size();
  755. out.insert (out.end(), reinterpret_cast<const std::uint8_t*> (&id), reinterpret_cast<const std::uint8_t*> (&id) + 4);
  756. out.insert (out.end(), reinterpret_cast<const std::uint8_t*> (&size), reinterpret_cast<const std::uint8_t*> (&size) + 8);
  757. out.insert (out.end(), body.begin(), body.end());
  758. };
  759. auto i32s = [] (std::vector<std::int32_t> v) {
  760. std::vector<std::uint8_t> b (v.size() * 4);
  761. std::memcpy (b.data(), v.data(), b.size());
  762. return b;
  763. };
  764. flp::Project p;
  765. flp::Channel midiOut;
  766. midiOut.index = 0;
  767. midiOut.type = flp::ChannelType::native;
  768. midiOut.pluginName = "MIDI Out";
  769. midiOut.pluginState = i32s ({ 7, 0, -1, -1, -1, 0, 3, 0 }); // channel 1, port 3 (@24)
  770. flp::Channel wrapper;
  771. wrapper.index = 1;
  772. wrapper.type = flp::ChannelType::native;
  773. wrapper.pluginName = "Fruity Wrapper";
  774. wrapper.pluginState = i32s ({ 9 });
  775. record (wrapper.pluginState, 0x32, i32s ({ 7 })); // VST3
  776. record (wrapper.pluginState, 0x01, i32s ({ 3, -1, 0, 0, 0 })); // input port 3
  777. p.channels = { midiOut, wrapper };
  778. flp::Pattern pattern;
  779. pattern.index = 1;
  780. flp::Note note;
  781. note.channel = 0;
  782. note.key = 64;
  783. note.length = 96;
  784. pattern.notes.push_back (note);
  785. p.patterns.push_back (pattern);
  786. const auto song = lynx::importFlp (p);
  787. CHECK (song.channels.size() == 2 && song.channels[0].muted);
  788. CHECK (song.channels[1].plugin.foreign.midiInPort == 3);
  789. int toWrapper = 0;
  790. for (const auto& pat : song.patterns)
  791. for (const auto& n : pat.notes)
  792. toWrapper += n.channel == 1 && n.key == 64;
  793. CHECK (toWrapper == 1);
  794. }
  795. TEST (flp_effect_automation_follows_slots_and_params)
  796. {
  797. // track 1: slot 0 with an effect that has no equivalent (dropped), slot 2 with Parametric EQ 2
  798. flp::Project p;
  799. flp::MixerInsert insert;
  800. insert.index = 1;
  801. insert.slots.push_back ({ 0, "Some Unknown FX", {} });
  802. insert.slots.push_back ({ 2, "Fruity Parametric EQ 2", {} });
  803. p.mixer.push_back (insert);
  804. auto clip = [&] (int index, std::uint32_t target) {
  805. flp::Channel c;
  806. c.index = index;
  807. c.type = flp::ChannelType::automationClip;
  808. c.automation = { { 0.0, 0.0 }, { 96.0, 1.0 } };
  809. p.channels.push_back (c);
  810. p.automationLinks.push_back ({ index, { target } });
  811. };
  812. 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; };
  813. clip (0, mixer (1, 2, 0x8000 + 7)); // EQ band 1 frequency
  814. clip (1, mixer (1, 2, 0x1F01)); // EQ slot mix
  815. clip (2, mixer (1, 0, 0x8000)); // parameter of the dropped effect
  816. clip (3, mixer (1, 2, 0x8000 + 21)); // band type: no equivalent
  817. const auto song = lynx::importFlp (p);
  818. CHECK (song.automations.size() == 4);
  819. CHECK (song.mixer[1].effects.size() == 1 && song.mixer[1].effects[0].kind == lynx::EffectKind::parametricEq);
  820. const auto& freq = song.automations[0];
  821. CHECK (freq.target.kind == lynx::AutomationTargetKind::effectParam && freq.target.index == 1 && freq.target.slot == 0);
  822. CHECK (freq.target.param == lynx::peq::param (0, 1));
  823. // FL's 0..1 (20 Hz..20 kHz) becomes our frequency scale
  824. CHECK (std::abs (freq.points.front().value - lynx::peq::hzValue (20.0)) < 1e-4f);
  825. CHECK (std::abs (freq.points.back().value - lynx::peq::hzValue (20000.0)) < 1e-4f);
  826. CHECK (song.automations[1].target.kind == lynx::AutomationTargetKind::effectMix && song.automations[1].target.slot == 0);
  827. CHECK (song.automations[2].target.kind == lynx::AutomationTargetKind::none);
  828. CHECK (song.automations[3].target.kind == lynx::AutomationTargetKind::none);
  829. // the automated mix is read/written through the target
  830. auto copy = song;
  831. CHECK (copy.setTargetValue (song.automations[1].target, 0.25f) && std::abs (copy.mixer[1].effects[0].mix - 0.25f) < 1e-6f);
  832. CHECK (std::abs (copy.targetValue (song.automations[1].target) - 0.25f) < 1e-6f);
  833. }
  834. TEST (flp_real_files)
  835. {
  836. if (realFlpDir.empty())
  837. {
  838. std::printf (" (skipped: pass a folder with .flp files as argument)\n");
  839. return;
  840. }
  841. int files = 0, withNotes = 0, withPlaylist = 0, automations = 0, automationsWithTarget = 0, sends = 0;
  842. int threeOsc = 0, threeOscDecoded = 0, wrapped = 0, wrappedParsed = 0, markers = 0, signatures = 0, trackEqs = 0;
  843. int harmless = 0, harmlessDecoded = 0, harmlessParsed = 0, harmlessKind = 0, sytrus = 0, sytrusDecoded = 0, sytrusEngine = 0, sytrusRendered = 0,
  844. sytrusSounding = 0;
  845. int harmor = 0, harmorUsable = 0, harmorKind = 0, harmorSounding = 0;
  846. int gms = 0, gmsUsable = 0, gmsKind = 0, gmsSounding = 0, morphine = 0, morphineUsable = 0, morphineKind = 0, morphineSounding = 0, demoGoldens = 0;
  847. int ogun = 0, ogunUsable = 0, ogunKind = 0, ogunSounding = 0;
  848. int autogun = 0, autogunUsable = 0, autogunKind = 0, autogunWithSg = 0, autogunSounding = 0; // REA_rodada57
  849. int poizone = 0, poizoneUsable = 0, poizoneKind = 0, poizoneSounding = 0, poizoneGoldens = 0;
  850. int miniSynth = 0, miniSynthUsable = 0, miniSynthKind = 0, miniSynthSounding = 0, miniSynthGoldens = 0;
  851. int dx10 = 0, dx10Usable = 0, dx10Kind = 0, dx10Sounding = 0;
  852. int kepler = 0, keplerUsable = 0, keplerKind = 0, keplerSounding = 0;
  853. int toxic = 0, toxicUsable = 0, toxicKind = 0, toxicSounding = 0, toxicGoldens = 0; // REA_rodada45
  854. int threeOscKind = 0, threeOscSounding = 0;
  855. std::map<std::string, std::pair<int, int>> generators; // GMS/Ogun/Autogun/Sakura/DrumSynth: channels, decoded
  856. int ogunTrumpet = 0; // Ogun channels whose spectrum matches the REA "Trumpet" reference
  857. int peqSlots = 0, peqShaped = 0, reverbs = 0, reverbsDryless = 0, reverbsLowCut = 0;
  858. int dwChannels = 0, dwZones = 0, fpcKits = 0, padChannels = 0, padNotes = 0;
  859. int harmlessCompWanted = 0, harmlessCompLoaded = 0; // Harmless rack compressors / presets read from the installation
  860. int maximusSlots = 0, maximusExact = 0, sgzSlots = 0, sgzExact = 0, vocodexSlots = 0, vocodexWithData = 0; // REA_rodada40
  861. int shaperSlots = 0, shaperExact = 0, clipperSlots = 0, clipperExact = 0; // REA_rodada51
  862. int flPeqSlots = 0, flPeqExact = 0; // REA_rodada50
  863. int basicSlots = 0, basicExact = 0; // REA_rodada58: PanOMatic, Balance, Blood Overdrive
  864. int equoSlots = 0, equoExact = 0, pitchShifterSlots = 0, pitchShifterExact = 0; // REA_rodada61
  865. int delay2Slots = 0, delay2Exact = 0, delay3Slots = 0, delay3Exact = 0; // REA_rodada52
  866. int chorusSlots = 0, chorusExact = 0; // REA_rodada59: the exact choruses
  867. int fastDistSlots = 0, fastDistExact = 0, flangusSlots = 0, flangusExact = 0, flangerSlots = 0, flangerExact = 0; // REA_rodada55
  868. // import with the installation's samples (realFlpDir is ...\Data): for DirectWave/FPC
  869. lynx::FlpImportOptions withFiles;
  870. const auto flRoot = std::filesystem::path (realFlpDir).parent_path().string();
  871. withFiles.roots = { flRoot, realFlpDir, "" };
  872. withFiles.fileExists = [] (const std::string& path) {
  873. auto local = path;
  874. std::replace (local.begin(), local.end(), '\\', '/');
  875. return std::filesystem::exists (local);
  876. };
  877. withFiles.readFile = [] (const std::string& path) {
  878. auto local = path;
  879. std::replace (local.begin(), local.end(), '\\', '/');
  880. std::ifstream in (local, std::ios::binary);
  881. return std::vector<std::uint8_t> ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  882. };
  883. int embeddedSaved = 0;
  884. withFiles.saveEmbedded = [&embeddedSaved] (const std::vector<std::uint8_t>& wav, const std::string&) {
  885. ++embeddedSaved;
  886. return wav.size() > 44 ? std::string ("embutido.wav") : std::string();
  887. };
  888. int slicedChannels = 0, flKeysChannels = 0;
  889. for (auto& entry : std::filesystem::recursive_directory_iterator (realFlpDir))
  890. {
  891. if (entry.path().extension() != ".flp")
  892. continue;
  893. std::ifstream in (entry.path(), std::ios::binary);
  894. std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  895. ++files;
  896. try
  897. {
  898. const auto p = flp::parseProject (bytes);
  899. CHECK (! p.version.empty());
  900. CHECK (p.tempo > 10.0 && p.tempo < 1000.0);
  901. const auto song = lynx::importFlp (p);
  902. CHECK (! song.patterns.empty());
  903. {
  904. const auto full = lynx::importFlp (p, withFiles);
  905. bool hasFpc = false;
  906. for (const auto& c : p.channels)
  907. hasFpc = hasFpc || c.pluginName == "FPC";
  908. // GMS and Morphine (REA_rodada39): every state plays on the exact engine (GMS with its waves from the
  909. // installation) and sounds; two demo channels match golden samples of our round-39 models
  910. for (const auto& c : p.channels)
  911. {
  912. if (c.pluginName == "GMS")
  913. ++gms, gmsUsable += lynx::gmsStateUsable (c.pluginState) ? 1 : 0;
  914. if (c.pluginName == "Morphine")
  915. ++morphine, morphineUsable += lynx::morphineStateUsable (c.pluginState) ? 1 : 0;
  916. }
  917. // Kepler: every state in the demos plays on the exact engine (REA_rodada46) when the engine DLL is installed
  918. for (const auto& c : p.channels)
  919. if (c.pluginName == "Kepler")
  920. {
  921. ++kepler;
  922. keplerUsable += lynx::keplerStateUsable (c.pluginState) ? 1 : 0;
  923. }
  924. for (const auto& c : full.channels)
  925. if (c.instrument == lynx::InstrumentKind::kepler)
  926. {
  927. ++keplerKind;
  928. 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 } },
  929. 44100.0, 11025);
  930. double e = 0.0;
  931. bool finite = ! y.empty();
  932. for (auto v : y)
  933. finite = finite && std::isfinite (v), e += (double) v * v;
  934. CHECK (finite);
  935. keplerSounding += e > 1e-6 ? 1 : 0;
  936. }
  937. const auto file = entry.path().filename().string();
  938. for (const auto& c : full.channels)
  939. {
  940. const auto isGms = c.instrument == lynx::InstrumentKind::gms, isMorphine = c.instrument == lynx::InstrumentKind::morphine;
  941. if (! isGms && ! isMorphine)
  942. continue;
  943. (isGms ? gmsKind : morphineKind)++;
  944. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  945. const auto golden = (isGms && c.name == "GMS" && file.find ("Song For You") != std::string::npos)
  946. || (isMorphine && c.name == "Morphine" && file.find ("SHINE") != std::string::npos);
  947. std::vector<float> y;
  948. if (isGms)
  949. y = lynx::gms::renderReference (c.pluginState,
  950. { { 0, 60, (float) iv (100), 1.0, 0.0, 0.0, 30000 }, { 11025, 64, (float) iv (80), 1.0, 0.0, 0.0, 40000 },
  951. { 22050, 67, (float) iv (110), 1.0, 0.0, 0.0, 52000 }, { 60000, 48, (float) iv (127), 1.0, 0.0, 0.0, 70000 } },
  952. 44100.0, golden ? 88200 : 22050, c.samplePath);
  953. else
  954. 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 } },
  955. 44100.0, golden ? 100000 : 22050, c.samplePath);
  956. double e = 0.0;
  957. bool finite = ! y.empty();
  958. for (auto v : y)
  959. finite = finite && std::isfinite (v), e += (double) v * v;
  960. CHECK (finite);
  961. (isGms ? gmsSounding : morphineSounding) += e > 1e-6 ? 1 : 0;
  962. if (golden && ! y.empty())
  963. {
  964. // our model, same events (GMS: tempo 140; Morphine: tempo 120), state as saved, 44.1 kHz
  965. const auto rms = std::sqrt (e / (double) y.size());
  966. CHECK (std::abs (rms - (isGms ? 0.213602668574 : 0.0673606448152)) < 1e-5);
  967. 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 } },
  968. 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 } };
  969. for (const auto& g : isGms ? std::vector (std::begin (gmsAt), std::end (gmsAt)) : std::vector (std::begin (morAt), std::end (morAt)))
  970. 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);
  971. ++demoGoldens;
  972. }
  973. }
  974. // MiniSynth (REA_rodada41): every state plays on the exact engine (tables from the installation's
  975. // oscdata.dat) and sounds; two demo channels match golden samples of our round-41 model (bit-identical to
  976. // renders of the installation's DLL for these states)
  977. for (const auto& c : p.channels)
  978. if (c.pluginName == "MiniSynth")
  979. ++miniSynth, miniSynthUsable += lynx::miniSynthStateUsable (c.pluginState) ? 1 : 0;
  980. for (const auto& c : full.channels)
  981. {
  982. if (c.instrument != lynx::InstrumentKind::minisynth)
  983. continue;
  984. ++miniSynthKind;
  985. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  986. const auto exo = file.find ("Exoplanet") != std::string::npos;
  987. const auto golden = exo && (c.name == "Main Lead 1" || c.name == "Riser");
  988. const auto y = lynx::minisynth::renderReference (c.pluginState,
  989. { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 }, { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  990. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 }, { 60000, 48, iv (127), 1.0, 0.0, 0.0, 70000 } },
  991. 44100.0, golden ? 88200 : 22050, c.samplePath);
  992. double e = 0.0;
  993. bool finite = ! y.empty();
  994. for (auto v : y)
  995. finite = finite && std::isfinite (v), e += (double) v * v;
  996. CHECK (finite);
  997. miniSynthSounding += e > 1e-6 ? 1 : 0;
  998. if (golden && ! y.empty())
  999. {
  1000. // our model, same events, tempo 120, state as saved, 44.1 kHz, host blocks of 512, L = R = Final.Vol
  1001. const auto lead = c.name == "Main Lead 1";
  1002. CHECK (std::abs (std::sqrt (e / (double) y.size()) - (lead ? 0.177495801753 : 0.19006567413)) < 1e-9);
  1003. 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 } },
  1004. riserAt[] = { { 2000, -0.0603227727f }, { 15000, 0.112200871f }, { 30000, 0.0453962795f }, { 50000, 0.234380022f }, { 65000, -0.164545432f }, { 80000, -0.000485120923f } };
  1005. for (const auto& g : lead ? std::vector (std::begin (leadAt), std::end (leadAt)) : std::vector (std::begin (riserAt), std::end (riserAt)))
  1006. CHECK (y[2 * (std::size_t) g.i] == g.v && y[2 * (std::size_t) g.i + 1] == g.v);
  1007. ++miniSynthGoldens;
  1008. }
  1009. }
  1010. // PoiZone (REA_rodada44): every state plays on the exact engine and sounds; the demo channel matches golden
  1011. // samples of our round-44 model
  1012. for (const auto& c : p.channels)
  1013. if (c.pluginName == "PoiZone")
  1014. ++poizone, poizoneUsable += lynx::poizoneStateUsable (c.pluginState) ? 1 : 0;
  1015. for (const auto& c : full.channels)
  1016. {
  1017. if (c.instrument != lynx::InstrumentKind::poizone)
  1018. continue;
  1019. ++poizoneKind;
  1020. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  1021. const auto golden = c.name == "PoiZone" && file.find ("NIGHT TERROR") != std::string::npos;
  1022. const auto y = lynx::poizone::renderReference (c.pluginState,
  1023. { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 }, { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  1024. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 }, { 60000, 48, iv (127), 1.0, 0.0, 0.0, 70000 } },
  1025. 44100.0, golden ? 88200 : 22050);
  1026. double e = 0.0;
  1027. bool finite = ! y.empty();
  1028. for (auto v : y)
  1029. finite = finite && std::isfinite (v), e += (double) v * v;
  1030. CHECK (finite);
  1031. poizoneSounding += e > 1e-6 ? 1 : 0;
  1032. if (golden && ! y.empty())
  1033. {
  1034. // our model, same events, tempo 120, state as saved, 44.1 kHz, L = R = Final.Vol
  1035. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.018975758213507415) < 1e-7);
  1036. static const struct { int i; float l, r; } at[] = { { 2000, -0.0030244605f, -0.0031888473f }, { 15000, 0.002743553f, -0.013375883f }, { 30000, 0.031268276f, 0.04751329f }, { 50000, 0.049796376f, 0.035944063f }, { 65000, -0.0014251951f, -0.01722545f }, { 80000, 0.01474628f, 0.000680499f } };
  1037. for (const auto& g : at)
  1038. CHECK (std::abs (y[2 * (std::size_t) g.i] - g.l) < 1e-7f && std::abs (y[2 * (std::size_t) g.i + 1] - g.r) < 1e-7f);
  1039. ++poizoneGoldens;
  1040. }
  1041. }
  1042. // Toxic Biohazard (REA_rodada45): every demo state plays on the exact engine with the waves read from the
  1043. // installation's DLL and sounds; the "Bombs Away" channel matches golden samples of our round-45 model
  1044. for (const auto& c : p.channels)
  1045. if (c.pluginName == "Toxic Biohazard")
  1046. ++toxic, toxicUsable += lynx::toxicStateUsable (c.pluginState) ? 1 : 0;
  1047. for (const auto& c : full.channels)
  1048. {
  1049. if (c.instrument != lynx::InstrumentKind::toxic)
  1050. continue;
  1051. ++toxicKind;
  1052. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  1053. const auto golden = file.find ("Bombs Away") != std::string::npos;
  1054. const auto y = lynx::toxic::renderReference (
  1055. c.pluginState,
  1056. { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 }, { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  1057. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 }, { 60000, 48, iv (127), 1.0, 0.0, 0.0, 70000 } },
  1058. 44100.0, golden ? 88200 : 22050, c.samplePath, 512, 140.0);
  1059. double e = 0.0;
  1060. bool finite = ! y.empty();
  1061. for (auto v : y)
  1062. finite = finite && std::isfinite (v), e += (double) v * v;
  1063. CHECK (finite);
  1064. toxicSounding += e > 1e-6 ? 1 : 0;
  1065. if (golden && ! y.empty())
  1066. {
  1067. // our model (r45/py/toxmodel.py, which equals the DLL bit for bit), state as saved, tempo 140, 44.1 kHz
  1068. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.0832500924945) < 1e-6);
  1069. static const struct { int i; float l, r; } at[] = { { 2000, -0.0501383618f, -0.100676455f }, { 15000, -0.0949445963f, -0.0714591742f }, { 30000, -0.0305469204f, 0.037349131f }, { 50000, 0.0255811438f, -0.0238218959f }, { 65000, -0.111861639f, -0.113630719f }, { 80000, 0.0163135566f, 0.0199630354f } };
  1070. for (const auto& g : at)
  1071. 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);
  1072. ++toxicGoldens;
  1073. }
  1074. }
  1075. for (const auto& c : full.channels)
  1076. {
  1077. if (c.instrument == lynx::InstrumentKind::harmless && c.modelParams.size() >= (std::size_t) lynx::harmlessParamCount
  1078. && c.modelParams[(std::size_t) lynx::harmlessComboIndex + 3] > 0.5f)
  1079. {
  1080. ++harmlessCompWanted;
  1081. harmlessCompLoaded += c.modelParams.size() > (std::size_t) lynx::harmlessCompIndex + 1000 ? 1 : 0;
  1082. }
  1083. flKeysChannels += c.zones.size() == 30 && c.zones.front().path.find ("CHANGPIANO") != std::string::npos ? 1 : 0;
  1084. if (c.sampler.slices.size() >= 4)
  1085. {
  1086. ++slicedChannels;
  1087. CHECK (std::is_sorted (c.sampler.slices.begin(), c.sampler.slices.end()));
  1088. CHECK (c.sampler.slices.back() < 1.0f);
  1089. }
  1090. if (! c.zones.empty() && c.name.find (" - ") == std::string::npos)
  1091. {
  1092. ++dwChannels;
  1093. dwZones += (int) c.zones.size();
  1094. }
  1095. padChannels += c.name.find (" - ") != std::string::npos && c.instrument == lynx::InstrumentKind::sampler ? 1 : 0;
  1096. }
  1097. fpcKits += hasFpc;
  1098. // Maximus / Soundgoodizer become the exact engine (REA_rodada40); Vocodex carries SG.dat (and its
  1099. // carrier's Sytrus preset) read from the installation
  1100. const int basicSlots0 = basicSlots, basicExact0 = basicExact;
  1101. for (const auto& insert : p.mixer)
  1102. for (const auto& slot : insert.slots)
  1103. {
  1104. maximusSlots += slot.plugin == "Maximus";
  1105. delay2Slots += slot.plugin == "Fruity Delay 2";
  1106. delay3Slots += slot.plugin == "Fruity Delay 3";
  1107. sgzSlots += slot.plugin == "Soundgoodizer";
  1108. vocodexSlots += slot.plugin == "Vocodex";
  1109. shaperSlots += slot.plugin == "Fruity WaveShaper";
  1110. flPeqSlots += slot.plugin == "Fruity Parametric EQ 2" || slot.plugin == "Fruity Parametric EQ";
  1111. equoSlots += slot.plugin == "EQUO";
  1112. pitchShifterSlots += slot.plugin == "Pitch Shifter";
  1113. clipperSlots += slot.plugin == "Fruity Soft Clipper";
  1114. basicSlots += slot.plugin == "Fruity PanOMatic" || slot.plugin == "Fruity Balance" || slot.plugin == "Fruity Blood Overdrive";
  1115. chorusSlots += lynx::chorusControls (slot.plugin) > 0 && slot.state.size() >= 4 ? 1 : 0;
  1116. fastDistSlots += slot.plugin == "Fruity Fast Dist";
  1117. flangusSlots += slot.plugin == "Fruity Flangus";
  1118. flangerSlots += slot.plugin == "Fruity Flanger";
  1119. }
  1120. for (const auto& t : full.mixer)
  1121. for (const auto& fx : t.effects)
  1122. {
  1123. if (fx.kind == lynx::EffectKind::maximus)
  1124. {
  1125. const auto blob = lynx::maximusfx::unpack (fx.state);
  1126. CHECK (blob && lynx::maximusfx::presetOf (*blob) != nullptr);
  1127. if (blob)
  1128. (blob->build == lynx::hlfx::MaximusBuild::soundgoodizer ? sgzExact : maximusExact) += 1;
  1129. }
  1130. fastDistExact += fx.kind == lynx::EffectKind::fruityFastDist;
  1131. flangusExact += fx.kind == lynx::EffectKind::fruityFlangus;
  1132. flangerExact += fx.kind == lynx::EffectKind::fruityFlanger;
  1133. if (fx.kind == lynx::EffectKind::vocodex)
  1134. vocodexWithData += ! lynx::vocodex::splitInstallData (fx.state).second.sgDat.empty();
  1135. if (fx.kind == lynx::EffectKind::fruityWaveShaper)
  1136. shaperExact += lynx::flshape::parseWaveShaper (fx.state).has_value() ? 1 : 0;
  1137. clipperExact += fx.kind == lynx::EffectKind::fruitySoftClipper;
  1138. flPeqExact += fx.kind == lynx::EffectKind::parametricEq && lynx::peq::flLaw (fx.params);
  1139. basicExact += fx.kind == lynx::EffectKind::fruityPanOMatic || fx.kind == lynx::EffectKind::fruityBalance
  1140. || fx.kind == lynx::EffectKind::fruityBloodOverdrive;
  1141. equoExact += fx.kind == lynx::EffectKind::equo && lynx::equofx::parse (fx.state).has_value();
  1142. pitchShifterExact += fx.kind == lynx::EffectKind::flPitchShifter;
  1143. delay2Exact += fx.kind == lynx::EffectKind::fruityDelay2;
  1144. delay3Exact += fx.kind == lynx::EffectKind::fruityDelay3;
  1145. chorusExact += fx.kind == lynx::EffectKind::fruityChorus || fx.kind == lynx::EffectKind::hyperChorus || fx.kind == lynx::EffectKind::vintageChorus ? 1 : 0;
  1146. }
  1147. if (basicSlots - basicSlots0 != basicExact - basicExact0)
  1148. std::printf (" %s: %d PanOMatic/Balance/Blood Overdrive slots, %d exact effects\n", file.c_str(),
  1149. basicSlots - basicSlots0, basicExact - basicExact0);
  1150. for (const auto& pat : full.patterns)
  1151. for (const auto& n : pat.notes)
  1152. {
  1153. CHECK (n.channel >= 0 && n.channel < (int) full.channels.size());
  1154. padNotes += full.channels[(std::size_t) n.channel].name.find (" - ") != std::string::npos ? 1 : 0;
  1155. }
  1156. }
  1157. const auto peqDefaults = lynx::makeEffectSlot (lynx::EffectKind::parametricEq).params;
  1158. for (const auto& t : song.mixer)
  1159. for (const auto& fx : t.effects)
  1160. {
  1161. if (fx.kind == lynx::EffectKind::parametricEq)
  1162. {
  1163. ++peqSlots;
  1164. peqShaped += fx.params != peqDefaults;
  1165. for (double hz : { 50.0, 1000.0, 10000.0 })
  1166. CHECK (std::isfinite (lynx::peq::responseDb (fx.params, 48000.0, hz)));
  1167. }
  1168. if (fx.kind == lynx::EffectKind::reverb)
  1169. {
  1170. ++reverbs;
  1171. reverbsDryless += fx.params[4] < 0.01f;
  1172. reverbsLowCut += fx.params[5] > 0.0f;
  1173. }
  1174. if (fx.kind == lynx::EffectKind::reeverb2) // FL's exact Reeverb 2 (REA_rodada49): FL's own controls
  1175. {
  1176. const auto r2 = lynx::reeverb2::fromParams (fx.params);
  1177. ++reverbs;
  1178. reverbsDryless += r2.p[10] == 0;
  1179. reverbsLowCut += r2.p[0] >= 20;
  1180. }
  1181. }
  1182. for (const auto& t : song.mixer)
  1183. if (t.eq.active())
  1184. {
  1185. ++trackEqs;
  1186. for (std::size_t b = 0; b < 3; ++b)
  1187. CHECK (t.eq.hz[b] >= 10.0f && t.eq.hz[b] <= 16000.0f && std::abs (t.eq.gainDb[b]) <= 18.0f);
  1188. }
  1189. for (const auto& a : song.arrangements)
  1190. for (const auto& m : a.markers)
  1191. {
  1192. ++markers;
  1193. signatures += m.isSignature();
  1194. CHECK (m.tick >= 0);
  1195. }
  1196. // Fruity Wrapper (channels and mixer slots): every blob in the demos is read
  1197. auto countWrapper = [&] (const std::string& plugin, const std::vector<std::uint8_t>& state) {
  1198. if (plugin != "Fruity Wrapper")
  1199. return;
  1200. ++wrapped;
  1201. lynx::ForeignPlugin f;
  1202. wrappedParsed += lynx::parseFlWrapper (state, f) && f.format != lynx::ForeignPlugin::Format::none && ! f.name.empty();
  1203. };
  1204. for (const auto& c : p.channels)
  1205. countWrapper (c.pluginName, c.pluginState);
  1206. for (const auto& insert : p.mixer)
  1207. for (const auto& slot : insert.slots)
  1208. countWrapper (slot.plugin, slot.state);
  1209. // Harmless: every state in the demos becomes a synth, with finite values
  1210. for (const auto& c : p.channels)
  1211. if (c.pluginName == "Harmless")
  1212. {
  1213. ++harmless;
  1214. lynx::SynthSettings hs;
  1215. float g = 1.0f;
  1216. if (lynx::synthFromHarmless (c.pluginState, 128.0, hs, g))
  1217. {
  1218. ++harmlessDecoded;
  1219. CHECK (std::isfinite (g) && hs.cutoff >= 20.0f && hs.cutoff <= 20000.0f && std::isfinite (hs.decay) && std::isfinite (hs.release));
  1220. }
  1221. if (const auto hp = lynx::harmlessParams (c.pluginState))
  1222. harmlessParsed += (int) hp->size() == lynx::harmlessParamCount ? 1 : 0;
  1223. }
  1224. for (const auto& c : song.channels)
  1225. harmlessKind += c.instrument == lynx::InstrumentKind::harmless ? 1 : 0;
  1226. // Harmor: every state in the demos plays on the exact engine (REA_rodada34) and sounds, with finite samples
  1227. for (const auto& c : p.channels)
  1228. if (c.pluginName == "Harmor")
  1229. {
  1230. ++harmor;
  1231. harmorUsable += lynx::harmorStateUsable (c.pluginState) ? 1 : 0;
  1232. }
  1233. for (const auto& c : song.channels)
  1234. if (c.instrument == lynx::InstrumentKind::harmor)
  1235. {
  1236. ++harmorKind;
  1237. 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);
  1238. double e = 0.0;
  1239. bool finite = ! y.empty();
  1240. for (auto v : y)
  1241. finite = finite && std::isfinite (v), e += (double) v * v;
  1242. CHECK (finite);
  1243. harmorSounding += e > 1e-6 ? 1 : 0;
  1244. }
  1245. // Ogun: every state in the demos plays on the exact engine (REA_rodada37) and sounds, with finite samples
  1246. for (const auto& c : p.channels)
  1247. if (c.pluginName == "Ogun")
  1248. {
  1249. ++ogun;
  1250. ogunUsable += lynx::ogunStateUsable (c.pluginState) ? 1 : 0;
  1251. }
  1252. for (const auto& c : song.channels)
  1253. if (c.instrument == lynx::InstrumentKind::ogun)
  1254. {
  1255. ++ogunKind;
  1256. 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 },
  1257. { 2000, 67, 0.6, 1.0, 0.0, 0.0, 0.0, 0.0, 0.5, 8000 } }, 44100.0, 11025);
  1258. double e = 0.0;
  1259. bool finite = ! y.empty();
  1260. for (auto v : y)
  1261. finite = finite && std::isfinite (v), e += (double) v * v;
  1262. CHECK (finite);
  1263. ogunSounding += e > 1e-6 ? 1 : 0;
  1264. }
  1265. // Autogun (REA_rodada57): every state plays on Ogun's engine in its Autogun variant; with the installation, the
  1266. // Magic Dust stage gets Autogun\Data\SG.dat
  1267. {
  1268. bool any = false;
  1269. for (const auto& c : p.channels)
  1270. if (c.pluginName == "Autogun")
  1271. ++autogun, autogunUsable += lynx::autogunStateUsable (c.pluginState) ? 1 : 0, any = true;
  1272. if (any)
  1273. for (const auto& c : lynx::importFlp (p, withFiles).channels)
  1274. if (c.instrument == lynx::InstrumentKind::autogun)
  1275. {
  1276. ++autogunKind;
  1277. lynx::ogun::RefOptions opt;
  1278. opt.autogun = true;
  1279. if (! c.samplePath.empty())
  1280. {
  1281. auto local = c.samplePath;
  1282. std::replace (local.begin(), local.end(), '\\', '/');
  1283. std::ifstream sg (local, std::ios::binary);
  1284. opt.sgState.assign ((std::istreambuf_iterator<char> (sg)), std::istreambuf_iterator<char>());
  1285. autogunWithSg += opt.sgState.size() == 1342 ? 1 : 0;
  1286. }
  1287. const auto y = lynx::ogun::renderReference (c.pluginState, { { 0, 48, 0.8, 1.0, 0.0, 0.0, 0.0, 0.0, 0.5, 6000 } },
  1288. 44100.0, 11025, opt);
  1289. double e = 0.0;
  1290. bool finite = ! y.empty();
  1291. for (auto v : y)
  1292. finite = finite && std::isfinite (v), e += (double) v * v;
  1293. CHECK (finite);
  1294. autogunSounding += e > 1e-6 ? 1 : 0;
  1295. }
  1296. }
  1297. // Fruity DX10: every state plays on the exact engine when its channel processor is neutral (REA_rodada42)
  1298. for (const auto& c : p.channels)
  1299. if (c.pluginName == "Fruity DX10" || c.pluginName == "DX10")
  1300. ++dx10, dx10Usable += lynx::dx10StateUsable (c.pluginState) ? 1 : 0;
  1301. for (const auto& c : song.channels)
  1302. if (c.instrument == lynx::InstrumentKind::dx10)
  1303. {
  1304. ++dx10Kind;
  1305. 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 } },
  1306. 44100.0, 11025);
  1307. double e = 0.0;
  1308. bool finite = ! y.empty();
  1309. for (auto v : y)
  1310. finite = finite && std::isfinite (v), e += (double) v * v;
  1311. CHECK (finite);
  1312. dx10Sounding += e > 1e-6 ? 1 : 0;
  1313. }
  1314. // 3x Osc: every channel plays on the exact engine (REA_rodada47)
  1315. for (const auto& c : song.channels)
  1316. if (c.instrument == lynx::InstrumentKind::threeosc)
  1317. {
  1318. ++threeOscKind;
  1319. const auto y = lynx::threeosc::renderReference (c.pluginState, { { 0, 60, 1.0, 0.0, 0.0, 6000 }, { 2000, 67, 0.6, 0.0, 0.0, 8000 } },
  1320. 44100.0, 11025);
  1321. double e = 0.0;
  1322. bool finite = ! y.empty();
  1323. for (auto v : y)
  1324. finite = finite && std::isfinite (v), e += (double) v * v;
  1325. CHECK (finite);
  1326. threeOscSounding += e > 1e-6 ? 1 : 0;
  1327. }
  1328. // Sytrus: every state in the demos (zlib + 10 units) decodes, with finite values
  1329. for (const auto& c : p.channels)
  1330. if (c.pluginName == "Sytrus")
  1331. {
  1332. ++sytrus;
  1333. lynx::SynthSettings ss;
  1334. float g = 1.0f;
  1335. // the Sytrus engine (REA_rodada31) reads every demo state; the first 24 play a short note (render settings)
  1336. if (lynx::sytrus::stateUsable (c.pluginState))
  1337. {
  1338. ++sytrusEngine;
  1339. if (sytrusRendered < 24)
  1340. {
  1341. ++sytrusRendered;
  1342. lynx::sytrus::Engine e;
  1343. e.prepare (44100.0, true, 512);
  1344. e.setPatch (lynx::sytrus::parsePatch (c.pluginState));
  1345. lynx::sytrus::NoteParams note;
  1346. e.noteOn (note);
  1347. std::vector<float> l (8192), r (8192);
  1348. e.render (l.data(), r.data(), 8192, 0.551f, 0.0f);
  1349. bool finite = true;
  1350. double energy = 0.0;
  1351. for (std::size_t k = 0; k < l.size(); ++k)
  1352. finite = finite && std::isfinite (l[k]) && std::isfinite (r[k]), energy += (double) l[k] * l[k];
  1353. CHECK (finite);
  1354. sytrusSounding += energy > 1e-6;
  1355. }
  1356. }
  1357. if (lynx::synthFromSytrus (c.pluginState, 128.0, ss, g))
  1358. {
  1359. ++sytrusDecoded;
  1360. CHECK (std::isfinite (g) && ss.cutoff >= 20.0f && ss.cutoff <= 20000.0f && std::isfinite (ss.attack) && std::isfinite (ss.decay)
  1361. && std::isfinite (ss.release) && ss.sustain >= 0.0f && ss.sustain <= 1.0f && std::isfinite (ss.filterEnv.amount));
  1362. for (const auto& o : ss.osc)
  1363. CHECK (o.level >= 0.0f && o.level <= 1.0f && o.octave >= -3 && o.octave <= 3);
  1364. }
  1365. }
  1366. // round 12 generators
  1367. for (const auto& c : p.channels)
  1368. {
  1369. lynx::SynthSettings gs;
  1370. float g = 1.0f;
  1371. int decoded = -1;
  1372. if (c.pluginName == "Harmor")
  1373. {
  1374. decoded = lynx::synthFromHarmor (c.pluginState, gs, g);
  1375. if (decoded)
  1376. {
  1377. // REA_rodada25 §4: most resonant settings stay below Q ≈ 3, a few saturate
  1378. CHECK (gs.resonance >= 0.0f && gs.resonance <= 1.0f);
  1379. static int resonant = 0, strong = 0, total = 0;
  1380. resonant += gs.resonance > 0.0f;
  1381. strong += gs.resonance > 0.9f;
  1382. if (++total % 50 == 0)
  1383. std::printf (" Harmor resonance: %d with a peak, %d with Q > ~5 (of %d)\n", resonant, strong, total);
  1384. }
  1385. static int saw = 0, square = 0, other = 0, total = 0;
  1386. if (decoded && std::any_of (gs.osc.begin(), gs.osc.end(), [] (const lynx::SynthOscillator& o) { return o.wave == lynx::Waveform::spectral; }))
  1387. {
  1388. auto isSaw = true, isSquare = true;
  1389. for (int n = 1; n <= 16; ++n)
  1390. {
  1391. isSaw = isSaw && std::abs (gs.spectrum[(std::size_t) n - 1] - 1.0f / n) < 1e-3f;
  1392. isSquare = isSquare && std::abs (gs.spectrum[(std::size_t) n - 1] - (n % 2 == 1 ? 1.1535f / n : 0.0f)) < 2e-3f;
  1393. }
  1394. ++(isSaw ? saw : isSquare ? square : other);
  1395. if (++total % 20 == 0 || total == 100)
  1396. std::printf (" Harmor spectrum: %d saw, %d square, %d other (of %d)\n", saw, square, other, total);
  1397. }
  1398. }
  1399. else if (c.pluginName == "GMS")
  1400. decoded = lynx::synthFromGms (c.pluginState, 128.0, gs, g);
  1401. else if (c.pluginName == "Ogun" || c.pluginName == "Autogun")
  1402. {
  1403. decoded = lynx::synthFromOgun (c.pluginState, gs, g);
  1404. // REA_rodada24 §1.9: the "Trumpet" preset (richness 7, no seeds) has partials 0.9087, 0.8610,
  1405. // 0.8151… at the fixed seed-3 phases 0.919, 0.380, 0.130
  1406. if (decoded && gs.osc[0].wave == lynx::Waveform::spectral)
  1407. {
  1408. CHECK (std::all_of (gs.spectrum.begin(), gs.spectrum.end(), [] (float a) { return std::isfinite (a) && a >= 0.0f; }));
  1409. static constexpr float trumpet[3] { 0.9087f, 0.8610f, 0.8151f }, phases[3] { 0.919f, 0.380f, 0.130f };
  1410. auto match = true;
  1411. for (std::size_t k = 0; k < 3; ++k)
  1412. match = match && std::abs (gs.spectrum[k] * 2.0f / 3.14159265f - trumpet[k]) < 1e-3f && std::abs (gs.spectrumPhase[k] - phases[k]) < 1e-3f;
  1413. ogunTrumpet += match ? 1 : 0;
  1414. }
  1415. }
  1416. else if (c.pluginName == "Sakura")
  1417. decoded = lynx::synthFromSakura (c.pluginState, gs);
  1418. else if (c.pluginName == "Kepler")
  1419. decoded = lynx::synthFromKepler (c.pluginState, gs, g);
  1420. else if (c.pluginName == "Wave Traveller")
  1421. decoded = lynx::waveTravellerSample (c.pluginState).has_value() ? 2 : 0;
  1422. else if (c.pluginName == "PoiZone")
  1423. decoded = lynx::synthFromPoiZone (c.pluginState, gs, g);
  1424. else if (c.pluginName == "BassDrum")
  1425. decoded = lynx::synthFromBassDrum (c.pluginState, gs, g);
  1426. else if (c.pluginName == "Toxic Biohazard")
  1427. decoded = lynx::synthFromToxic (c.pluginState, gs, g);
  1428. else if (c.pluginName == "Morphine")
  1429. decoded = lynx::synthFromMorphine (c.pluginState, gs, g);
  1430. else if (c.pluginName == "BooBass")
  1431. decoded = lynx::synthFromBooBass (c.pluginState, gs);
  1432. else if (c.pluginName == "Drumpad" || c.pluginName == "DrumPad")
  1433. decoded = lynx::drumpadPatchName (c.pluginState).has_value() ? 2 : 0;
  1434. else if (c.pluginName == "Fruity Envelope Controller" && c.pluginState.size() >= 4 && c.pluginState[2] >= 6) // v >= 6
  1435. decoded = lynx::parseEnvelopeController (c.pluginState).has_value() ? 2 : 0;
  1436. else if (c.pluginName == "FLEX")
  1437. decoded = lynx::flexPresetName (c.pluginState).has_value() ? 2 : 0;
  1438. else if (c.pluginName == "Fruity DrumSynth Live")
  1439. decoded = lynx::drumSynthLiveParams (c.pluginState).has_value() ? 2 : 0;
  1440. if (decoded < 0)
  1441. continue;
  1442. auto& counter = generators[c.pluginName];
  1443. ++counter.first;
  1444. counter.second += decoded != 0;
  1445. if (decoded == 1 && c.pluginName != "Fruity DrumSynth Live")
  1446. {
  1447. CHECK (std::isfinite (g) && g >= 0.0f && gs.cutoff >= 20.0f && gs.cutoff <= 20000.0f && gs.sustain >= 0.0f && gs.sustain <= 1.0f);
  1448. 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));
  1449. for (const auto& o : gs.osc)
  1450. CHECK (o.level >= 0.0f && o.level <= 4.0f && std::isfinite (o.detune) && std::isfinite (o.pan));
  1451. }
  1452. }
  1453. // 3x Osc: every state in the demos decodes, with levels summing to at most 1
  1454. for (const auto& c : p.channels)
  1455. if (c.pluginName == "3x Osc")
  1456. {
  1457. ++threeOsc;
  1458. lynx::SynthSettings s;
  1459. if (lynx::synthFrom3xOsc (c.pluginState, s))
  1460. {
  1461. ++threeOscDecoded;
  1462. // with AM, osc 3 is the modulation depth and is left out of the sum
  1463. const auto sum = s.osc[0].level + s.osc[1].level + (s.am ? 0.0f : s.osc[2].level);
  1464. CHECK (sum <= 1.001f && s.osc[2].level <= 1.0f);
  1465. for (const auto& o : s.osc)
  1466. CHECK (o.octave >= -2 && o.octave <= 2 && std::abs (o.semitones) <= 11 && std::abs (o.pan) <= 1.0f);
  1467. }
  1468. }
  1469. for (auto& pat : p.patterns)
  1470. if (! pat.notes.empty())
  1471. {
  1472. ++withNotes;
  1473. break;
  1474. }
  1475. for (auto& a : p.arrangements)
  1476. for (auto& item : a.items)
  1477. CHECK (item.track >= 0 && item.track < 500);
  1478. for (auto& clip : song.playlist)
  1479. {
  1480. CHECK (clip.pattern >= 0 && clip.pattern < (int) song.patterns.size());
  1481. CHECK (clip.length > 0 && clip.offset >= 0);
  1482. if (clip.kind == lynx::ClipKind::automation)
  1483. CHECK (clip.automation >= 0 && clip.automation < (int) song.automations.size());
  1484. }
  1485. for (auto& a : song.automations)
  1486. {
  1487. ++automations;
  1488. automationsWithTarget += a.target.kind != lynx::AutomationTargetKind::none;
  1489. CHECK (a.isLive() || ! a.points.empty());
  1490. for (std::size_t i = 1; i < a.points.size(); ++i)
  1491. CHECK (a.points[i].tick >= a.points[i - 1].tick);
  1492. }
  1493. // imported routing has no cycles and valid destinations
  1494. CHECK (song.mixerOrder().size() + 1 == song.mixer.size());
  1495. for (auto& t : song.mixer)
  1496. for (auto& s : t.sends)
  1497. {
  1498. ++sends;
  1499. CHECK (s.target > 0 && s.target < (int) song.mixer.size());
  1500. }
  1501. if (! song.playlist.empty())
  1502. ++withPlaylist;
  1503. }
  1504. catch (const flp::Error& e)
  1505. {
  1506. ++failures;
  1507. std::printf (" FAILED %s: %s\n", entry.path().string().c_str(), e.what());
  1508. }
  1509. }
  1510. CHECK (threeOscDecoded == threeOsc);
  1511. std::printf (" Harmless: %d channels, %d decoded\n", harmless, harmlessDecoded);
  1512. std::printf (" Sytrus: %d channels, %d decoded, %d by the engine (%d of %d rendered sound)\n", sytrus, sytrusDecoded, sytrusEngine,
  1513. sytrusSounding, sytrusRendered);
  1514. CHECK (sytrusEngine == sytrus);
  1515. CHECK (sytrusSounding >= sytrusRendered * 3 / 4);
  1516. for (const auto& [name, counter] : generators)
  1517. {
  1518. std::printf (" %s: %d channels, %d decoded\n", name.c_str(), counter.first, counter.second);
  1519. CHECK (counter.second == counter.first);
  1520. }
  1521. std::printf (" Ogun spectra matching the Trumpet reference: %d\n", ogunTrumpet);
  1522. CHECK (ogunTrumpet >= 1);
  1523. // demo Gross Beat: the volume slot becomes the gate (those using an envelope slot get steps < 1)
  1524. {
  1525. int vocodexes = 0, vocodexSides = 0;
  1526. int lovePhilterLinks[4] {}; // live links: cutoff, resonance; inside containers: cutoff, resonance
  1527. int embeddedZones = 0, lovePhilters = 0, lovePhiltersRead = 0, lovePhiltersConverted = 0, gates = 0, shaped = 0, effectors = 0, patchers = 0, serial = 0, parallel = 0, generatorPatchers = 0;
  1528. for (const auto& entry : std::filesystem::recursive_directory_iterator (std::filesystem::path (realFlpDir) / "Demo projects"))
  1529. if (entry.path().extension() == ".flp")
  1530. {
  1531. std::ifstream in (entry.path(), std::ios::binary);
  1532. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  1533. try
  1534. {
  1535. const auto project = flp::parseProject (file);
  1536. for (const auto& insert : project.mixer)
  1537. for (const auto& slot : insert.slots)
  1538. if (slot.plugin == "Gross Beat")
  1539. {
  1540. // FL's own engine (REA_rodada53): every demo state is read
  1541. const auto fx = lynx::effectFromFl (slot.plugin, slot.state);
  1542. CHECK (fx && fx->kind == lynx::EffectKind::grossBeat);
  1543. ++gates;
  1544. if (fx && fx->kind == lynx::EffectKind::grossBeat && lynx::grossbeat::slotNames (fx->state, true).size() == 36)
  1545. ++shaped;
  1546. }
  1547. else if (slot.plugin == "Fruity Love Philter")
  1548. {
  1549. ++lovePhilters;
  1550. lovePhiltersRead += lynx::parseLovePhilter (slot.state).has_value();
  1551. if (const auto conv = lynx::lovePhilterEffects (slot.state))
  1552. {
  1553. ++lovePhiltersConverted;
  1554. CHECK (! conv->slots.empty());
  1555. for (const auto& fx : conv->slots)
  1556. for (auto v : fx.params)
  1557. CHECK (std::isfinite (v) && v >= 0.0f && v <= 1.0f);
  1558. for (const auto& fx : conv->slots)
  1559. for (const auto& link : fx.links)
  1560. {
  1561. const auto& chain = fx.branches[(std::size_t) link.branch].chain;
  1562. CHECK (link.source < (int) chain.size() && chain[(std::size_t) link.target].kind == lynx::EffectKind::parametricEq);
  1563. lovePhilterLinks[2 + (link.param == lynx::peq::param (0, 3) ? 1 : 0)]++;
  1564. }
  1565. for (const auto& link : conv->links)
  1566. {
  1567. lovePhilterLinks[link.param == lynx::peq::param (0, 3) ? 1 : 0]++;
  1568. CHECK (link.curve.size() == 33);
  1569. for (auto v : link.curve)
  1570. CHECK (std::isfinite (v) && v >= 0.0f && v <= 1.0f);
  1571. }
  1572. }
  1573. }
  1574. else if (slot.plugin == "Vocodex")
  1575. {
  1576. const auto v = lynx::vocodexEffect (slot.state);
  1577. CHECK (v.has_value());
  1578. vocodexes += v.has_value();
  1579. vocodexSides += v && (v->modIn > 0 || v->carIn > 0);
  1580. }
  1581. else if (slot.plugin == "Effector")
  1582. effectors += lynx::effectFromFl (slot.plugin, slot.state).has_value();
  1583. else if (slot.plugin == "Patcher")
  1584. {
  1585. const auto graph = lynx::parsePatcher (slot.state);
  1586. CHECK (graph.has_value());
  1587. ++patchers;
  1588. if (graph && ! graph->serialChain.empty())
  1589. ++serial;
  1590. if (graph && graph->serialChain.empty() && graph->audio)
  1591. ++parallel;
  1592. }
  1593. for (const auto& c : project.channels)
  1594. if (c.pluginName == "DirectWave")
  1595. if (const auto program = lynx::directWaveProgram (c.pluginState))
  1596. for (const auto& wav : program->embeddedWav)
  1597. if (! wav.empty())
  1598. {
  1599. ++embeddedZones;
  1600. CHECK (wav.size() > 44 && std::memcmp (wav.data(), "RIFF", 4) == 0);
  1601. }
  1602. for (const auto& c : project.channels)
  1603. if (c.pluginName == "Patcher")
  1604. {
  1605. const auto graph = lynx::parsePatcher (c.pluginState);
  1606. CHECK (graph.has_value());
  1607. generatorPatchers += graph && graph->mainGenerator >= 0;
  1608. }
  1609. }
  1610. catch (const flp::Error&) {}
  1611. }
  1612. CHECK (gates >= 30 && shaped == gates);
  1613. std::printf (" Gross Beat: %d slots, %d read exactly; Effector: %d converted; Love Philter: %d, %d read\n", gates, shaped, effectors,
  1614. lovePhilters, lovePhiltersRead);
  1615. CHECK (lovePhiltersRead == lovePhilters && lovePhiltersConverted == lovePhilters);
  1616. std::printf (" Vocodex: %d converted, %d with side inputs\n", vocodexes, vocodexSides);
  1617. CHECK (vocodexes >= 9);
  1618. std::printf (" Love Philter live links: %d cutoff, %d resonance; in multi-unit containers: %d cutoff, %d resonance\n",
  1619. lovePhilterLinks[0], lovePhilterLinks[1], lovePhilterLinks[2], lovePhilterLinks[3]);
  1620. CHECK (lovePhilterLinks[2] + lovePhilterLinks[3] > 0);
  1621. std::printf (" DirectWave: %d zones with embedded audio\n", embeddedZones);
  1622. CHECK (embeddedZones == 12);
  1623. std::printf (" Patcher: %d in the mixer (%d serial, %d reduced to serial/parallel), %d as instrument with generator\n", patchers, serial,
  1624. parallel, generatorPatchers);
  1625. CHECK (gates == 0 || shaped > 0);
  1626. }
  1627. // Envelope Controller "Basic Sidechain" (Future House): drops on the note and returns to ~1 within ~1/3 s
  1628. {
  1629. const auto path = std::filesystem::path (realFlpDir) / "Demo projects" / "Demo songs" / "Asher Postman - Future House.flp";
  1630. if (std::filesystem::exists (path))
  1631. {
  1632. std::ifstream in (path, std::ios::binary);
  1633. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  1634. const auto song = lynx::importFlp (flp::parseProject (file));
  1635. const lynx::ChannelDef* controller = nullptr;
  1636. for (const auto& c : song.channels)
  1637. if (c.instrument == lynx::InstrumentKind::controller)
  1638. controller = &c;
  1639. CHECK (controller != nullptr);
  1640. if (controller != nullptr)
  1641. {
  1642. auto inst = lynx::makeInstrument (lynx::InstrumentKind::controller);
  1643. inst->prepare (48000.0);
  1644. inst->configure (*controller, song.bpm);
  1645. std::vector<float> l (480), r (480);
  1646. inst->render (l.data(), r.data(), 480, 1.0f, 1.0f);
  1647. inst->noteOn (60, 1.0f);
  1648. inst->render (l.data(), r.data(), 480, 1.0f, 1.0f); // 10 ms
  1649. const auto ducked = inst->controlOutput (0);
  1650. for (int i = 0; i < 100; ++i) // +1 s
  1651. inst->render (l.data(), r.data(), 480, 1.0f, 1.0f);
  1652. const auto recovered = inst->controlOutput (0);
  1653. std::printf (" Envelope Controller (Basic Sidechain): %.2f on the note, %.2f after 1 s\n", ducked, recovered);
  1654. CHECK (ducked < 0.3f && recovered > 0.9f);
  1655. }
  1656. }
  1657. }
  1658. // mutation fuzz: real states with flipped and truncated bytes must not crash the readers
  1659. {
  1660. std::map<std::string, std::vector<std::uint8_t>> samples; // one real blob per plugin
  1661. for (const auto& entry : std::filesystem::recursive_directory_iterator (std::filesystem::path (realFlpDir) / "Demo projects"))
  1662. {
  1663. if (entry.path().extension() != ".flp")
  1664. continue;
  1665. std::ifstream in (entry.path(), std::ios::binary);
  1666. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  1667. try
  1668. {
  1669. const auto project = flp::parseProject (file);
  1670. for (const auto& c : project.channels)
  1671. if (! c.pluginState.empty())
  1672. samples.emplace (c.pluginName, c.pluginState);
  1673. for (const auto& insert : project.mixer)
  1674. for (const auto& slot : insert.slots)
  1675. if (! slot.state.empty())
  1676. samples.emplace (slot.plugin, slot.state);
  1677. }
  1678. catch (const flp::Error&) {}
  1679. }
  1680. std::uint32_t seed = 12345;
  1681. auto rnd = [&seed] { seed = seed * 1664525u + 1013904223u; return seed >> 8; };
  1682. int mutations = 0;
  1683. for (const auto& [name, original] : samples)
  1684. for (int round = 0; round < 40; ++round)
  1685. {
  1686. auto blob = original;
  1687. if (round % 4 == 3 && ! blob.empty())
  1688. blob.resize (rnd() % blob.size()); // truncated
  1689. else
  1690. for (int k = 0; k < 1 + (int) (rnd() % 8) && ! blob.empty(); ++k)
  1691. blob[rnd() % blob.size()] = (std::uint8_t) rnd();
  1692. ++mutations;
  1693. lynx::SynthSettings synth;
  1694. float gain = 1.0f;
  1695. lynx::synthFromSytrus (blob, 120.0, synth, gain);
  1696. lynx::synthFromHarmor (blob, synth, gain);
  1697. lynx::synthFromGms (blob, 120.0, synth, gain);
  1698. lynx::synthFromOgun (blob, synth, gain);
  1699. lynx::synthFromMorphine (blob, synth, gain);
  1700. lynx::synthFromKepler (blob, synth, gain);
  1701. lynx::synthFromPoiZone (blob, synth, gain);
  1702. lynx::synthFromToxic (blob, synth, gain);
  1703. lynx::synthFromBassDrum (blob, synth, gain);
  1704. lynx::synthFromHarmless (blob, 120.0, synth, gain);
  1705. lynx::harmlessParams (blob);
  1706. if (lynx::harmorStateUsable (blob))
  1707. lynx::harmorFallbacks (blob);
  1708. lynx::parseEnvelopeController (blob);
  1709. lynx::parseKeyboardController (blob);
  1710. lynx::parseLovePhilter (blob);
  1711. lynx::parsePatcher (blob);
  1712. lynx::effectFromFl (name, blob);
  1713. lynx::fpcPads (blob);
  1714. lynx::directWaveProgram (blob);
  1715. CHECK (std::isfinite (gain) && std::isfinite (synth.cutoff));
  1716. }
  1717. std::printf (" mutation fuzz: %d blobs from %d plugins\n", mutations, (int) samples.size());
  1718. }
  1719. // WavPack (.wv) samples from FL packs: all decode, with finite, non-silent audio
  1720. {
  1721. int files = 0, decoded = 0, audible = 0;
  1722. double seconds = 0.0;
  1723. for (const auto& entry : std::filesystem::recursive_directory_iterator (std::filesystem::path (realFlpDir) / "Patches" / "Packs"))
  1724. if (entry.path().extension() == ".wv")
  1725. {
  1726. std::ifstream in (entry.path(), std::ios::binary);
  1727. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  1728. ++files;
  1729. lynx::DecodedAudio audio;
  1730. if (! lynx::decodeWavPack (file.data(), file.size(), audio))
  1731. continue;
  1732. ++decoded;
  1733. float peak = 0.0f;
  1734. bool finite = true;
  1735. for (const auto& ch : audio.channels)
  1736. for (auto v : ch)
  1737. {
  1738. finite = finite && std::isfinite (v);
  1739. peak = std::max (peak, std::abs (v));
  1740. }
  1741. CHECK (finite && peak <= 4.0f);
  1742. audible += peak > 1e-4f;
  1743. seconds += (double) audio.channels[0].size() / audio.sampleRate;
  1744. }
  1745. std::printf (" WavPack: %d .wv files, %d decoded, %d audible, %.0f s of audio\n", files, decoded, audible, seconds);
  1746. CHECK (decoded == files);
  1747. }
  1748. // native GMS presets (.gmsynth = the contents of 0xD5 without the "SSLF")
  1749. int gmsPresets = 0, gmsDecoded = 0;
  1750. const auto gmsFolder = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "GMS";
  1751. if (std::filesystem::exists (gmsFolder))
  1752. for (const auto& entry : std::filesystem::recursive_directory_iterator (gmsFolder))
  1753. if (entry.path().extension() == ".gmsynth")
  1754. {
  1755. std::ifstream in (entry.path(), std::ios::binary);
  1756. std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  1757. std::vector<std::uint8_t> blob { 'S', 'S', 'L', 'F' };
  1758. const auto n = (std::uint32_t) file.size();
  1759. blob.insert (blob.end(), reinterpret_cast<const std::uint8_t*> (&n), reinterpret_cast<const std::uint8_t*> (&n) + 4);
  1760. blob.insert (blob.end(), file.begin(), file.end());
  1761. lynx::SynthSettings gs;
  1762. float g = 1.0f;
  1763. ++gmsPresets;
  1764. if (lynx::synthFromGms (blob, 120.0, gs, g))
  1765. {
  1766. ++gmsDecoded;
  1767. CHECK (std::isfinite (g) && gs.cutoff >= 20.0f && std::isfinite (gs.pitchEnv.amount) && std::isfinite (gs.lfo[lynx::lfoPitch].period));
  1768. }
  1769. }
  1770. std::printf (" GMS presets: %d, %d decoded\n", gmsPresets, gmsDecoded);
  1771. // the synth's "FL presets" button: Sytrus/Harmor .fst and .gmsynth become synth settings
  1772. int presetFiles = 0, presetsRead = 0;
  1773. for (const auto* name : { "Sytrus", "Harmor", "GMS", "Harmless", "Ogun", "Sakura", "Fruit Kick", "Fruity DX10", "MiniSynth", "Kepler",
  1774. "PoiZone", "Toxic Biohazard", "BassDrum" })
  1775. {
  1776. const auto folder = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / name;
  1777. if (! std::filesystem::exists (folder))
  1778. continue;
  1779. for (const auto& entry : std::filesystem::recursive_directory_iterator (folder))
  1780. if (entry.path().extension() == ".fst" || entry.path().extension() == ".gmsynth")
  1781. {
  1782. std::ifstream in (entry.path(), std::ios::binary);
  1783. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  1784. ++presetFiles;
  1785. presetsRead += lynx::synthFromFlPreset (file).has_value();
  1786. }
  1787. }
  1788. std::printf (" FL synth presets: %d files, %d read\n", presetFiles, presetsRead);
  1789. CHECK (presetsRead >= presetFiles * 95 / 100);
  1790. CHECK (gmsDecoded == gmsPresets);
  1791. CHECK (sytrusDecoded == sytrus);
  1792. CHECK (harmlessDecoded == harmless && harmless > 100);
  1793. std::printf (" Harmless: %d states parsed for the engine, %d channels imported as Harmless\n", harmlessParsed, harmlessKind);
  1794. CHECK (harmlessParsed == harmless && harmlessKind >= harmless);
  1795. std::printf (" Harmor: %d channels, %d states usable by the engine, %d imported as Harmor (%d sounding)\n", harmor, harmorUsable, harmorKind, harmorSounding);
  1796. CHECK (harmorUsable == harmor && harmor > 90 && harmorKind >= harmor && harmorSounding >= harmorKind * 9 / 10);
  1797. 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",
  1798. gms, gmsUsable, gmsKind, gmsSounding, morphine, morphineUsable, morphineKind, morphineSounding, demoGoldens);
  1799. CHECK (gmsUsable == gms && gms >= 9 && gmsKind == gms && gmsSounding == gmsKind);
  1800. std::printf (" MiniSynth channels: %d, %d usable, %d on the exact engine, %d sounding, %d golden\n", miniSynth, miniSynthUsable, miniSynthKind,
  1801. miniSynthSounding, miniSynthGoldens);
  1802. CHECK (miniSynthUsable == miniSynth && miniSynth >= 12 && miniSynthKind == miniSynth && miniSynthSounding == miniSynthKind && miniSynthGoldens == 2);
  1803. CHECK (morphineUsable == morphine && morphine >= 7 && morphineKind == morphine && morphineSounding == morphineKind && demoGoldens == 2);
  1804. std::printf (" PoiZone: %d channels, %d usable, %d imported as PoiZone (%d sounding), %d demo golden\n", poizone, poizoneUsable, poizoneKind,
  1805. poizoneSounding, poizoneGoldens);
  1806. CHECK (poizoneUsable == poizone && poizone >= 2 && poizoneKind == poizone && poizoneSounding == poizoneKind && poizoneGoldens == 1);
  1807. std::printf (" Toxic Biohazard: %d channels, %d usable, %d imported as Toxic (%d sounding), %d golden\n", toxic, toxicUsable, toxicKind,
  1808. toxicSounding, toxicGoldens);
  1809. CHECK (toxicUsable == toxic && toxic >= 3 && toxicKind == toxic && toxicSounding == toxicKind && toxicGoldens == 1);
  1810. std::printf (" Ogun: %d channels, %d states usable by the engine, %d imported as Ogun (%d sounding)\n", ogun, ogunUsable, ogunKind, ogunSounding);
  1811. CHECK (ogunUsable == ogun && ogun >= 20 && ogunKind >= ogun && ogunSounding >= ogunKind * 9 / 10);
  1812. std::printf (" Autogun: %d channels, %d states usable by the engine, %d imported as Autogun (%d with SG.dat, %d sounding)\n", autogun,
  1813. autogunUsable, autogunKind, autogunWithSg, autogunSounding);
  1814. CHECK (autogunUsable == autogun && autogun >= 9 && autogunKind == autogun && autogunWithSg == autogun && autogunSounding == autogun);
  1815. std::printf (" Fruity DX10: %d channels, %d states usable by the engine, %d imported as DX10 (%d sounding)\n", dx10, dx10Usable, dx10Kind, dx10Sounding);
  1816. CHECK (dx10Usable == dx10 && dx10 >= 12 && dx10Kind == dx10 && dx10Sounding == dx10Kind);
  1817. std::printf (" Kepler: %d channels, %d states usable by the engine, %d imported as Kepler (%d sounding)\n", kepler, keplerUsable, keplerKind, keplerSounding);
  1818. CHECK (keplerUsable == kepler && kepler >= 4 && keplerSounding == keplerKind && (keplerKind == 0 || keplerKind == kepler));
  1819. std::printf (" Harmless rack compressor: %d of %d presets read from the installation\n", harmlessCompLoaded, harmlessCompWanted);
  1820. CHECK (harmlessCompLoaded == harmlessCompWanted && harmlessCompWanted > 100);
  1821. std::printf (" Maximus: %d of %d slots on the exact engine; Soundgoodizer: %d of %d; Vocodex with SG.dat: %d of %d\n", maximusExact,
  1822. maximusSlots, sgzExact, sgzSlots, vocodexWithData, vocodexSlots);
  1823. // REA_rodada40 §6: in Demo projects the 4 states left on the approximation use the Frequency Splitter linear-phase
  1824. // option; the other Data folders add states with options not decoded
  1825. CHECK (maximusSlots - maximusExact <= 16 && maximusExact >= 97 && sgzExact == sgzSlots && sgzSlots > 90);
  1826. CHECK (vocodexWithData == vocodexSlots && vocodexSlots >= 10);
  1827. std::printf (" Fruity WaveShaper: %d of %d slots on the exact engine; Fruity Soft Clipper: %d of %d\n", shaperExact, shaperSlots,
  1828. clipperExact, clipperSlots);
  1829. // (Patcher graphs add WaveShaper nodes of their own to the imported mixer)
  1830. CHECK (shaperExact >= shaperSlots && shaperSlots >= 70 && clipperExact >= clipperSlots && clipperSlots >= 25); // REA_rodada51
  1831. std::printf (" Fruity Parametric EQ 2 / Parametric EQ: %d of %d slots on the exact engine\n", flPeqExact, flPeqSlots);
  1832. CHECK (flPeqExact >= flPeqSlots && flPeqSlots >= 1100); // REA_rodada50
  1833. std::printf (" Fruity PanOMatic / Balance / Blood Overdrive: %d of %d slots on the exact engines\n", basicExact, basicSlots);
  1834. // one PanOMatic of "Bombs Away" (insert 69, last slot) falls off LYNX's 10-slot limit after a serial Patcher expands
  1835. CHECK (basicExact + 1 >= basicSlots && basicSlots >= 270); // REA_rodada58
  1836. std::printf (" EQUO: %d of %d slots on the exact engine\n", equoExact, equoSlots);
  1837. CHECK (equoExact == equoSlots && equoSlots >= 10); // REA_rodada61
  1838. std::printf (" Pitch Shifter: %d of %d slots on the exact engine\n", pitchShifterExact, pitchShifterSlots);
  1839. CHECK (pitchShifterExact == pitchShifterSlots && pitchShifterSlots >= 15); // REA_rodada61
  1840. std::printf (" Fruity Delay 2: %d of %d slots on the exact engine; Fruity Delay 3: %d of %d\n", delay2Exact, delay2Slots,
  1841. delay3Exact, delay3Slots);
  1842. CHECK (delay2Exact == delay2Slots && delay2Slots >= 60 && delay3Exact == delay3Slots && delay3Slots >= 100); // REA_rodada52
  1843. std::printf (" Chorus family: %d of %d slots on the exact engines\n", chorusExact, chorusSlots);
  1844. CHECK (chorusExact >= chorusSlots && chorusSlots >= 47); // REA_rodada59
  1845. std::printf (" Fruity Fast Dist: %d of %d slots on the exact engine; Fruity Flangus: %d of %d; Fruity Flanger: %d of %d\n", fastDistExact,
  1846. fastDistSlots, flangusExact, flangusSlots, flangerExact, flangerSlots);
  1847. CHECK (fastDistExact >= fastDistSlots && fastDistSlots >= 30 && flangusExact >= flangusSlots && flangusSlots >= 30
  1848. && flangerExact >= flangerSlots && flangerSlots >= 3); // REA_rodada55
  1849. // 3x Osc channel presets shipped with FL (.fst): all become synth settings
  1850. int presets = 0, presetsDecoded = 0;
  1851. const auto presetDir = std::filesystem::path (realFlpDir) / "Patches" / "Channel presets" / "3x Osc";
  1852. if (std::filesystem::is_directory (presetDir))
  1853. for (auto& entry : std::filesystem::directory_iterator (presetDir))
  1854. if (entry.path().extension() == ".fst")
  1855. {
  1856. ++presets;
  1857. std::ifstream in (entry.path(), std::ios::binary);
  1858. std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  1859. if (auto synth = lynx::synthFromFlPreset (bytes))
  1860. {
  1861. ++presetsDecoded;
  1862. CHECK (synth->osc[0].level + synth->osc[1].level + (synth->am ? 0.0f : synth->osc[2].level) <= 1.001f);
  1863. }
  1864. }
  1865. std::printf (" %d 3x Osc presets, %d read\n", presets, presetsDecoded);
  1866. CHECK (presetsDecoded == presets);
  1867. // FL effect presets that become built-in effects
  1868. int fxPresets = 0, fxDecoded = 0;
  1869. for (auto kind : { lynx::EffectKind::parametricEq, lynx::EffectKind::limiter, lynx::EffectKind::reverb, lynx::EffectKind::delay,
  1870. lynx::EffectKind::utility })
  1871. for (const auto& plugin : lynx::flPluginsForEffect (kind))
  1872. {
  1873. const auto dir = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Effects" / plugin;
  1874. if (! std::filesystem::is_directory (dir))
  1875. continue;
  1876. for (auto& entry : std::filesystem::recursive_directory_iterator (dir))
  1877. if (entry.path().extension() == ".fst")
  1878. {
  1879. ++fxPresets;
  1880. std::ifstream in (entry.path(), std::ios::binary);
  1881. std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  1882. fxDecoded += lynx::effectFromFlPreset (bytes).has_value();
  1883. }
  1884. }
  1885. std::printf (" %d FL effect presets, %d read\n", fxPresets, fxDecoded);
  1886. CHECK (fxDecoded == fxPresets);
  1887. CHECK (wrappedParsed == wrapped);
  1888. std::printf (" %d plugins in Fruity Wrapper, %d read\n", wrapped, wrappedParsed);
  1889. std::printf (" %d markers, %d time signature changes, %d tracks with EQ\n", markers, signatures, trackEqs);
  1890. std::printf (" %d parametric EQs (%d with FL settings), %d reverbs (%d without dry)\n", peqSlots, peqShaped, reverbs, reverbsDryless);
  1891. std::printf (" DirectWave: %d channels, %d zones; FPC: %d projects, %d pad channels, %d pad notes\n", dwChannels, dwZones, fpcKits,
  1892. padChannels, padNotes);
  1893. CHECK (dwZones > 1000 && padChannels > 250 && padNotes > 500);
  1894. std::printf (" sliced channels (Slicer/Slicex): %d; embedded WAVs saved: %d\n", slicedChannels, embeddedSaved);
  1895. CHECK (slicedChannels >= 25 && embeddedSaved >= 10); // without decoding Ogg here, some loops have no duration
  1896. std::printf (" FL Keys (Grand Piano, 30 zones): %d channels\n", flKeysChannels);
  1897. CHECK (flKeysChannels >= 8);
  1898. CHECK (peqSlots > 1000 && peqShaped > 900 && reverbsDryless > 20 && reverbsLowCut > 250);
  1899. CHECK (markers > 300 && signatures > 60);
  1900. std::printf (" %d 3x Osc channels, %d decoded, %d on the exact engine (%d sounding)\n", threeOsc, threeOscDecoded, threeOscKind, threeOscSounding);
  1901. // one demo channel (a noise layer of "Right Night feeling") has the channel filter closed (fast LP, Mod X 0): silent
  1902. // with its saved settings, in FL too
  1903. CHECK (threeOscKind == threeOsc && threeOscSounding == threeOscKind - 1);
  1904. std::printf (" %d .flp files read, %d with notes, %d with playlist, %d automations (%d with target), %d sends\n",
  1905. files, withNotes, withPlaylist, automations, automationsWithTarget, sends);
  1906. CHECK (files > 0);
  1907. CHECK (automations > 0 && sends > 0);
  1908. }
  1909. // ---------------------------------------------------------------------------
  1910. // Sequencer
  1911. TEST (pattern_toggle_step_and_length)
  1912. {
  1913. lynx::Pattern p;
  1914. CHECK (! p.hasStep (0, 4));
  1915. p.toggleStep (0, 4);
  1916. CHECK (p.hasStep (0, 4));
  1917. CHECK (p.notes.size() == 1 && p.notes[0].start == 4 * lynx::ticksPerStep);
  1918. CHECK (p.effectiveLength (4) == 4 * lynx::ppq); // one bar
  1919. p.toggleStep (1, 20); // second bar
  1920. CHECK (p.effectiveLength (4) == 8 * lynx::ppq);
  1921. p.toggleStep (0, 4);
  1922. CHECK (! p.hasStep (0, 4) && p.notes.size() == 1);
  1923. }
  1924. TEST (scheduler_places_events_at_exact_samples)
  1925. {
  1926. lynx::Pattern p;
  1927. p.toggleStep (0, 0);
  1928. p.toggleStep (0, 4); // tick 960 = 1 beat
  1929. const double sr = 48000.0, bpm = 120.0; // 1 beat = 0.5 s = 24000 samples
  1930. const auto tps = lynx::ticksPerSample (bpm, sr);
  1931. lynx::EventBuffer ev;
  1932. lynx::schedulePatternLoop (p, p.effectiveLength (4), 23900.0 * tps, 512, tps, ev);
  1933. // the beat-2 note starts at sample 24000 -> offset 100 in this block
  1934. int ons = 0;
  1935. for (auto& e : ev)
  1936. if (e.type == lynx::NoteEvent::Type::on)
  1937. {
  1938. ++ons;
  1939. CHECK (e.sampleOffset == 100);
  1940. }
  1941. CHECK (ons == 1);
  1942. }
  1943. TEST (scheduler_wraps_the_loop)
  1944. {
  1945. lynx::Pattern p;
  1946. p.toggleStep (2, 0);
  1947. const auto len = p.effectiveLength (4); // 3840 ticks
  1948. const double sr = 48000.0;
  1949. const auto tps = lynx::ticksPerSample (120.0, sr);
  1950. lynx::EventBuffer ev;
  1951. // block that crosses the loop end: starts 10 samples before
  1952. const auto start = (double) len - 10.0 * tps;
  1953. lynx::schedulePatternLoop (p, len, start, 64, tps, ev);
  1954. bool found = false;
  1955. for (auto& e : ev)
  1956. if (e.type == lynx::NoteEvent::Type::on && e.channel == 2)
  1957. {
  1958. found = true;
  1959. CHECK (e.sampleOffset == 10);
  1960. }
  1961. CHECK (found);
  1962. }
  1963. TEST (scheduler_off_before_on_at_same_sample)
  1964. {
  1965. lynx::Pattern p;
  1966. lynx::Note a;
  1967. a.start = 0; a.length = 240; a.channel = 0;
  1968. lynx::Note b = a;
  1969. b.start = 240;
  1970. p.notes = { a, b };
  1971. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  1972. lynx::EventBuffer ev;
  1973. lynx::schedulePatternLoop (p, p.effectiveLength (4), 200.0, 4000, tps, ev);
  1974. CHECK (ev.size() >= 2);
  1975. CHECK (ev[0].type == lynx::NoteEvent::Type::off);
  1976. CHECK (ev[1].type == lynx::NoteEvent::Type::on);
  1977. CHECK (ev[0].sampleOffset == ev[1].sampleOffset);
  1978. }
  1979. TEST (snapshot_exchange_hands_over_and_recycles)
  1980. {
  1981. struct Counted
  1982. {
  1983. int value;
  1984. int* alive;
  1985. Counted (int v, int* a) : value (v), alive (a) { ++*alive; }
  1986. ~Counted() { --*alive; }
  1987. };
  1988. int alive = 0;
  1989. {
  1990. lynx::SnapshotExchange<Counted> x;
  1991. CHECK (x.acquire() == nullptr);
  1992. x.publish (std::make_unique<Counted> (1, &alive));
  1993. CHECK (x.acquire()->value == 1);
  1994. x.publish (std::make_unique<Counted> (2, &alive));
  1995. x.publish (std::make_unique<Counted> (3, &alive)); // 2 was never read: freed immediately
  1996. CHECK (alive == 2);
  1997. CHECK (x.acquire()->value == 3);
  1998. CHECK (alive == 2); // 1 was returned, but only the UI frees it
  1999. x.collectGarbage();
  2000. CHECK (alive == 1);
  2001. // without collection, the return slots fill up and the audio side holds on to the current one
  2002. for (int i = 0; i < 20; ++i)
  2003. {
  2004. x.publish (std::make_unique<Counted> (10 + i, &alive));
  2005. CHECK (x.acquire() != nullptr);
  2006. }
  2007. }
  2008. CHECK (alive == 0);
  2009. }
  2010. TEST (rack_renderer_makes_sound_and_reuses_instruments)
  2011. {
  2012. auto song = lynx::Song::makeDefault();
  2013. song.bpm = 120.0;
  2014. const double sr = 48000.0;
  2015. auto state = lynx::RackState::build (song, nullptr, sr);
  2016. lynx::RackRenderer r;
  2017. r.prepare (sr);
  2018. std::vector<float> l (512), rr (512);
  2019. double tick = 0.0;
  2020. float peak = 0.0f;
  2021. int kicks = 0;
  2022. for (int block = 0; block < 93; ++block) // 47616 samples: just before 1 s
  2023. {
  2024. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  2025. for (auto& e : r.lastEvents())
  2026. if (e.type == lynx::NoteEvent::Type::on && e.channel == 0)
  2027. ++kicks;
  2028. for (int i = 0; i < 512; ++i)
  2029. {
  2030. CHECK (std::isfinite (l[(std::size_t) i]));
  2031. peak = std::max (peak, std::abs (l[(std::size_t) i]));
  2032. }
  2033. tick += 512 * lynx::ticksPerSample (song.bpm, sr);
  2034. }
  2035. CHECK (peak > 0.1f && peak < 4.0f);
  2036. CHECK (kicks == 2); // 120 BPM: beats at 0 and 0.5 s
  2037. // editing the song keeps the instruments of channels whose type didn't change
  2038. song.channels[4].instrument = lynx::InstrumentKind::kick;
  2039. auto next = lynx::RackState::build (song, state.get(), sr);
  2040. CHECK (next->instruments[0] == state->instruments[0]);
  2041. CHECK (next->instruments[4] != state->instruments[4]);
  2042. // stopping silences the synth and triggers no new notes
  2043. r.process (next.get(), false, tick, 512, l.data(), rr.data());
  2044. CHECK (r.lastEvents().size() == 0);
  2045. }
  2046. TEST (flp_import_maps_channels_and_rescales)
  2047. {
  2048. flp::Project p;
  2049. p.header.ppq = 96;
  2050. p.tempo = 150.0;
  2051. p.channels = { { 0, flp::ChannelType::sampler, "Kick 808", "", "", 1 },
  2052. { 1, flp::ChannelType::audioClip, "Vocal", "", "", 2 },
  2053. { 2, flp::ChannelType::instrument, "Lead", "FLEX", "", 3 } };
  2054. flp::Pattern pat;
  2055. pat.index = 1;
  2056. flp::Note n;
  2057. n.position = 96;
  2058. n.length = 0;
  2059. n.channel = 2;
  2060. n.key = 72;
  2061. n.velocity = 64;
  2062. pat.notes.push_back (n);
  2063. p.patterns.push_back (pat);
  2064. const auto s = lynx::importFlp (p);
  2065. CHECK (s.bpm == 150.0);
  2066. CHECK (s.channels.size() == 3); // audio clip channels stay in the rack, as in FL
  2067. CHECK (s.channels[0].instrument == lynx::InstrumentKind::kick);
  2068. CHECK (s.channels[1].instrument == lynx::InstrumentKind::audioClip);
  2069. CHECK (s.channels[2].instrument == lynx::InstrumentKind::synth);
  2070. CHECK (s.patterns.size() == 1);
  2071. CHECK (s.patterns[0].notes[0].start == lynx::ppq); // 96 @ PPQ 96 = 1 beat
  2072. CHECK (s.patterns[0].notes[0].channel == 2);
  2073. CHECK (s.patterns[0].notes[0].length == lynx::ticksPerStep); // step note
  2074. CHECK (std::abs (s.patterns[0].notes[0].velocity - 0.5f) < 1e-6f);
  2075. }
  2076. TEST (live_queue_is_fifo_and_bounded)
  2077. {
  2078. lynx::LiveEventQueue q;
  2079. lynx::NoteEvent e;
  2080. CHECK (! q.pop (e));
  2081. for (int i = 0; i < 10; ++i)
  2082. {
  2083. lynx::NoteEvent in;
  2084. in.key = i;
  2085. CHECK (q.push (in));
  2086. }
  2087. for (int i = 0; i < 10; ++i)
  2088. CHECK (q.pop (e) && e.key == i);
  2089. int pushed = 0;
  2090. while (q.push (e))
  2091. ++pushed;
  2092. CHECK (pushed == (int) lynx::LiveEventQueue::capacity - 1); // one slot stays free to tell full from empty
  2093. }
  2094. TEST (sampler_plays_pitched_one_shot)
  2095. {
  2096. // 1000-frame ramp sample: the position can be measured from the amplitude
  2097. auto data = std::make_shared<lynx::SampleData>();
  2098. data->sampleRate = 48000.0;
  2099. for (int i = 0; i < 1000; ++i)
  2100. data->left.push_back (1.0f);
  2101. auto sampler = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  2102. sampler->prepare (48000.0);
  2103. std::vector<float> l (2048, 0.0f), r (2048, 0.0f);
  2104. sampler->noteOn (60, 1.0f);
  2105. sampler->noteOff (60); // one-shot: noteOff doesn't cut
  2106. sampler->render (l.data(), r.data(), 2048, 1.0f, 1.0f);
  2107. int sounding = 0;
  2108. for (auto s : l)
  2109. sounding += s > 0.5f;
  2110. CHECK (sounding >= 995 && sounding <= 1000); // plays the whole sample at the original pitch
  2111. std::fill (l.begin(), l.end(), 0.0f);
  2112. sampler->noteOn (72, 1.0f); // one octave up: half the duration
  2113. sampler->render (l.data(), r.data(), 2048, 1.0f, 1.0f);
  2114. sounding = 0;
  2115. for (auto s : l)
  2116. sounding += s > 0.5f;
  2117. CHECK (sounding >= 495 && sounding <= 500);
  2118. CHECK (r[10] > 0.5f); // mono goes to both sides
  2119. auto silent = lynx::makeInstrument (lynx::InstrumentKind::sampler, nullptr);
  2120. silent->prepare (48000.0);
  2121. silent->noteOn (60, 1.0f);
  2122. std::fill (l.begin(), l.end(), 0.0f);
  2123. silent->render (l.data(), r.data(), 64, 1.0f, 1.0f);
  2124. CHECK (l[0] == 0.0f);
  2125. }
  2126. TEST (rack_live_notes_and_sample_provider)
  2127. {
  2128. lynx::Song song;
  2129. lynx::ChannelDef c;
  2130. c.name = "Smp";
  2131. c.instrument = lynx::InstrumentKind::sampler;
  2132. c.samplePath = "a.wav";
  2133. song.channels.push_back (c);
  2134. song.patterns.emplace_back();
  2135. int loads = 0;
  2136. auto provider = [&] (const std::string& path) -> std::shared_ptr<const lynx::SampleData> {
  2137. ++loads;
  2138. CHECK (path == "a.wav");
  2139. auto d = std::make_shared<lynx::SampleData>();
  2140. d->left.assign (4800, 0.5f);
  2141. return d;
  2142. };
  2143. auto state = lynx::RackState::build (song, nullptr, 48000.0, provider);
  2144. CHECK (loads == 1);
  2145. lynx::RackRenderer renderer;
  2146. renderer.prepare (48000.0);
  2147. lynx::LiveEventQueue live;
  2148. lynx::NoteEvent on;
  2149. on.channel = 0;
  2150. on.velocity = 1.0f;
  2151. live.push (on);
  2152. std::vector<float> l (256), r (256);
  2153. renderer.process (state.get(), false, 0.0, 256, l.data(), r.data(), &live); // stopped, but the live note plays
  2154. CHECK (l[0] > 0.1f);
  2155. // same sample: reused; different sample: reloaded
  2156. auto same = lynx::RackState::build (song, state.get(), 48000.0, provider);
  2157. CHECK (loads == 1 && same->instruments[0] == state->instruments[0]);
  2158. song.channels[0].samplePath = "b.wav";
  2159. auto other = lynx::RackState::build (song, state.get(), 48000.0, [&] (const std::string&) { ++loads; return nullptr; });
  2160. CHECK (loads == 2 && other->instruments[0] != state->instruments[0]);
  2161. }
  2162. TEST (flp_paths_resolve_and_samplers_import)
  2163. {
  2164. lynx::FlPathRoots roots { "C:\\FL", "C:\\FL\\Data", "D:\\User" };
  2165. CHECK (lynx::resolveFlPath ("%FLStudioFactoryData%\\Data\\x.wav", roots) == "C:\\FL\\Data\\x.wav");
  2166. CHECK (lynx::resolveFlPath ("%FLStudioData%\\Patches\\y.wav", roots) == "C:\\FL\\Data\\Patches\\y.wav");
  2167. CHECK (lynx::resolveFlPath ("%FLStudioUserData%\\z.wav", roots) == "D:\\User\\z.wav");
  2168. CHECK (lynx::resolveFlPath ("E:\\abs.wav", roots) == "E:\\abs.wav");
  2169. CHECK (lynx::resolveFlPath ("%FLStudioUserData%\\z.wav", {}) == "");
  2170. flp::Project p;
  2171. p.channels = { { 0, flp::ChannelType::sampler, "Kick", "", "%FLStudioData%\\k.wav", 1 },
  2172. { 1, flp::ChannelType::sampler, "Hat", "", "%FLStudioData%\\missing.wav", 1 } };
  2173. lynx::FlpImportOptions options;
  2174. options.roots = roots;
  2175. options.fileExists = [] (const std::string& path) { return path == "C:\\FL\\Data\\k.wav"; };
  2176. const auto s = lynx::importFlp (p, options);
  2177. CHECK (s.channels[0].instrument == lynx::InstrumentKind::sampler);
  2178. CHECK (s.channels[0].samplePath == "C:\\FL\\Data\\k.wav");
  2179. CHECK (s.channels[1].instrument == lynx::InstrumentKind::hat); // no file: built-in instrument
  2180. }
  2181. TEST (soft_clip_is_transparent_below_knee_and_bounded)
  2182. {
  2183. float s[] = { 0.5f, -0.89f, 1.5f, -3.0f, 0.95f };
  2184. lynx::softClip (s, 5);
  2185. CHECK (s[0] == 0.5f && s[1] == -0.89f);
  2186. CHECK (s[2] > 0.9f && s[2] <= 1.0f);
  2187. CHECK (s[3] < -0.9f && s[3] >= -1.0f);
  2188. CHECK (s[4] > 0.9f && s[4] < 0.95f);
  2189. }
  2190. TEST (song_scheduler_plays_clips_with_offset_and_cut)
  2191. {
  2192. lynx::Song s;
  2193. s.channels.resize (1);
  2194. auto& p = s.patterns.emplace_back();
  2195. for (int step : { 0, 4, 8, 12 }) // one note per beat
  2196. p.toggleStep (0, step);
  2197. // clip at bar 2 playing only the second half of the pattern (beats 3 and 4)
  2198. lynx::Clip c;
  2199. c.pattern = 0;
  2200. c.start = 4 * lynx::ppq;
  2201. c.length = 2 * lynx::ppq;
  2202. c.offset = 2 * lynx::ppq;
  2203. s.playlist.push_back (c);
  2204. CHECK (s.songLength() == 8 * lynx::ppq);
  2205. const double sr = 48000.0;
  2206. const auto tps = lynx::ticksPerSample (120.0, sr); // 1 beat = 24000 samples
  2207. lynx::EventBuffer ev;
  2208. // whole song in a single block (8 beats = 192000 samples)
  2209. lynx::scheduleSong (s, 0.0, 192000, tps, ev);
  2210. std::vector<int> ons;
  2211. for (auto& e : ev)
  2212. if (e.type == lynx::NoteEvent::Type::on)
  2213. ons.push_back (e.sampleOffset);
  2214. CHECK (ons.size() == 2);
  2215. CHECK (ons.size() == 2 && ons[0] == 4 * 24000 && ons[1] == 5 * 24000);
  2216. // crossing the loop: bar 1 is empty, so nothing at the start of the second pass
  2217. ev.clear();
  2218. lynx::scheduleSong (s, 8.0 * lynx::ppq - 100 * tps, 200, tps, ev);
  2219. CHECK (ev.size() == 0);
  2220. // a note longer than the clip is cut at the clip end
  2221. p.notes[3].length = 4 * lynx::ppq;
  2222. ev.clear();
  2223. lynx::scheduleSong (s, 0.0, 192000, tps, ev);
  2224. int lastOff = -1;
  2225. for (auto& e : ev)
  2226. if (e.type == lynx::NoteEvent::Type::off)
  2227. lastOff = std::max (lastOff, e.sampleOffset);
  2228. CHECK (lastOff == 6 * 24000);
  2229. }
  2230. TEST (remove_pattern_fixes_clips)
  2231. {
  2232. lynx::Song s;
  2233. s.patterns.resize (3);
  2234. s.currentPattern = 2;
  2235. s.playlist = { { 0, 0, 0, 100, 0 }, { 1, 0, 100, 100, 0 }, { 2, 1, 0, 100, 0 } };
  2236. s.removePattern (1);
  2237. CHECK (s.patterns.size() == 2);
  2238. CHECK (s.playlist.size() == 2 && s.playlist[1].pattern == 1);
  2239. CHECK (s.currentPattern == 1);
  2240. }
  2241. TEST (flp_import_3x_osc_state_and_channel_filter)
  2242. {
  2243. // new 3x Osc patch (version 14): osc 2 mix 64 coarse -12, osc 3 mix 32 coarse -24
  2244. std::vector<std::uint8_t> blob (460, 0);
  2245. auto put = [&] (std::size_t off, std::int32_t v) {
  2246. for (int b = 0; b < 4; ++b)
  2247. blob[off + (std::size_t) b] = (std::uint8_t) ((std::uint32_t) v >> (8 * b));
  2248. };
  2249. put (0x00, 14);
  2250. put (0x08, 3); // osc 1 saw
  2251. put (0x10, 7); // osc 1 fine +7
  2252. put (0x04, -32); // osc 1 pan -0.5
  2253. put (0x1C, 64); // osc 2 mix
  2254. put (0x28, -12); // osc 2 coarse
  2255. put (0x38, 32); // osc 3 mix
  2256. put (0x40, 2); // osc 3 square
  2257. put (0x44, -19); // osc 3 coarse: -1 octave and -7 semitones
  2258. lynx::SynthSettings s;
  2259. CHECK (lynx::synthFrom3xOsc (blob, s));
  2260. 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);
  2261. CHECK (s.osc[0].wave == lynx::Waveform::saw && s.osc[0].detune == 7.0f && s.osc[0].pan == -0.5f);
  2262. CHECK (s.osc[1].wave == lynx::Waveform::sine && s.osc[1].octave == -1 && s.osc[1].semitones == 0);
  2263. CHECK (s.osc[2].wave == lynx::Waveform::square && s.osc[2].octave == -1 && s.osc[2].semitones == -7);
  2264. CHECK (! lynx::synthFrom3xOsc (std::vector<std::uint8_t> (40, 0), s));
  2265. // channel filter: open by default; LP with Mod X 128 ~ 2.71 kHz; Fast LP with Mod X 128 ~ 280 Hz
  2266. float hz = 0.0f, res = 1.0f;
  2267. int mode = -1;
  2268. lynx::synthFilterFromChannel (256, 0, 0, hz, res, mode);
  2269. CHECK (hz >= 19999.0f && res == 0.0f);
  2270. lynx::synthFilterFromChannel (128, 0, 1, hz, res, mode);
  2271. CHECK (std::abs (hz - 2710.0f) < 30.0f && res == 0.0f);
  2272. lynx::synthFilterFromChannel (128, 0, 0, hz, res, mode);
  2273. CHECK (hz > 240.0f && hz < 320.0f);
  2274. lynx::synthFilterFromChannel (128, 256, 1, hz, res, mode);
  2275. CHECK (res > 0.8f);
  2276. CHECK (mode == 0);
  2277. lynx::synthFilterFromChannel (64, 0, 3, hz, res, mode); // high-pass at ~840 Hz
  2278. CHECK (mode == 2 && std::abs (hz - 840.0f) < 40.0f);
  2279. lynx::synthFilterFromChannel (128, 0, 2, hz, res, mode); // band-pass
  2280. CHECK (mode == 1);
  2281. }
  2282. TEST (synth_pitch_and_filter_envelopes)
  2283. {
  2284. auto render = [] (const lynx::SynthSettings& s, int frames) {
  2285. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  2286. inst->prepare (48000.0);
  2287. lynx::ChannelDef c;
  2288. c.synth = s;
  2289. inst->configure (c, 120.0);
  2290. inst->noteOn (45, 1.0f); // A2 = 110 Hz
  2291. std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
  2292. inst->render (l.data(), r.data(), frames, 1.0f, 1.0f);
  2293. return l;
  2294. };
  2295. auto crossings = [] (const std::vector<float>& b, int from, int to) {
  2296. int n = 0;
  2297. for (int i = from + 1; i < to; ++i)
  2298. n += (b[(std::size_t) i - 1] < 0.0f) != (b[(std::size_t) i] < 0.0f);
  2299. return n;
  2300. };
  2301. // 3x Osc "kick": sine with a +24 st pitch envelope falling fast
  2302. lynx::SynthSettings kick;
  2303. kick.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  2304. kick.cutoff = 20000.0f;
  2305. kick.pitchEnv = { 24.0f, 0.0f, 0.0f, 0.05f, 0.0f, 0.05f };
  2306. const auto k = render (kick, 48000);
  2307. const auto early = crossings (k, 0, 2400); // first 50 ms: well above 110 Hz
  2308. const auto late = crossings (k, 24000, 48000); // then back to 110 Hz (~110 crossings in 0.5 s)
  2309. CHECK (early > 20); // +24 st ramp over 50 ms: ~2.2x the average frequency (11 crossings without envelope)
  2310. CHECK (std::abs (late - 110) < 4);
  2311. // cutoff envelope: saw closed at 200 Hz opens +5 octaves on attack and closes again
  2312. lynx::SynthSettings pluck;
  2313. pluck.cutoff = 200.0f;
  2314. pluck.filterEnv = { 5.0f, 0.0f, 0.0f, 0.1f, 0.0f, 0.1f };
  2315. const auto p = render (pluck, 48000);
  2316. auto hf = [&] (int from, int to) { // energy of the difference (highs)
  2317. double e = 0.0;
  2318. for (int i = from + 1; i < to; ++i)
  2319. e += std::pow ((double) p[(std::size_t) i] - p[(std::size_t) i - 1], 2.0);
  2320. return e;
  2321. };
  2322. CHECK (hf (0, 2400) > 4.0 * hf (40000, 42400));
  2323. // Toxic Biohazard's envelope (REA_rodada33 §2): exponential towards 5 clipped at 1, so the cutoff is still near
  2324. // its base early in the attack, fully open at A = 0.2 s and back down after the exponential decay
  2325. lynx::SynthSettings toxic = pluck;
  2326. toxic.filterEnv = { 5.0f, 0.2f, 0.0f, 0.1f, 0.0f, 0.1f, 1 };
  2327. const auto t = render (toxic, 48000);
  2328. auto thf = [&] (int from, int to) {
  2329. double e = 0.0;
  2330. for (int i = from + 1; i < to; ++i)
  2331. e += std::pow ((double) t[(std::size_t) i] - t[(std::size_t) i - 1], 2.0);
  2332. return e;
  2333. };
  2334. CHECK (thf (9000, 9600) > 4.0 * thf (300, 900));
  2335. CHECK (thf (9000, 9600) > 4.0 * thf (40000, 40600));
  2336. }
  2337. TEST (synth_mono_reuses_the_sounding_voice)
  2338. {
  2339. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  2340. inst->prepare (48000.0);
  2341. lynx::ChannelDef c;
  2342. c.synth.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  2343. c.synth.cutoff = 20000.0f;
  2344. c.synth.mono = true;
  2345. inst->configure (c, 120.0);
  2346. std::vector<float> l (48000), r (48000);
  2347. inst->noteOn (57, 1.0f);
  2348. inst->render (l.data(), r.data(), 4800, 1.0f, 1.0f);
  2349. inst->noteOn (69, 1.0f); // A4 takes over the A3 voice: only one note sounding
  2350. inst->render (l.data() + 4800, r.data() + 4800, 43200, 1.0f, 1.0f);
  2351. int crossings = 0;
  2352. float peak = 0.0f;
  2353. for (std::size_t i = 24001; i < 48000; ++i)
  2354. {
  2355. crossings += (l[i - 1] < 0.0f) != (l[i] < 0.0f);
  2356. peak = std::max (peak, std::abs (l[i]));
  2357. }
  2358. CHECK (std::abs (crossings - 440) < 4); // 0.5 s of 440 Hz, without the A3 underneath
  2359. CHECK (peak < 0.35f); // one voice (0.3 level), not two summed
  2360. // no click on the handover: the jump between neighbouring samples stays sine-sized
  2361. float jump = 0.0f;
  2362. for (std::size_t i = 4790; i < 4810; ++i)
  2363. jump = std::max (jump, std::abs (l[i] - l[i - 1]));
  2364. CHECK (jump < 0.05f);
  2365. }
  2366. TEST (synth_porta_glides_and_am_modulates)
  2367. {
  2368. auto make = [] (const lynx::SynthSettings& s) {
  2369. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  2370. inst->prepare (48000.0);
  2371. lynx::ChannelDef c;
  2372. c.synth = s;
  2373. inst->configure (c, 120.0);
  2374. return inst;
  2375. };
  2376. auto crossings = [] (const std::vector<float>& b, std::size_t from, std::size_t to) {
  2377. int n = 0;
  2378. for (auto i = from + 1; i < to; ++i)
  2379. n += (b[i - 1] < 0.0f) != (b[i] < 0.0f);
  2380. return n;
  2381. };
  2382. // mono + 1-beat porta (0.5 s at 120 BPM): from A3 to A4, halfway ~E4 (329.6 Hz)
  2383. lynx::SynthSettings s;
  2384. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  2385. s.cutoff = 20000.0f;
  2386. s.mono = true;
  2387. s.porta = true;
  2388. s.portaBeats = 1.0f;
  2389. auto inst = make (s);
  2390. std::vector<float> l (96000), r (96000);
  2391. inst->noteOn (57, 1.0f);
  2392. inst->render (l.data(), r.data(), 4800, 1.0f, 1.0f);
  2393. inst->noteOn (69, 1.0f);
  2394. inst->render (l.data() + 4800, r.data() + 4800, 91200, 1.0f, 1.0f);
  2395. const auto middle = crossings (l, 4800 + 12000 - 2400, 4800 + 12000 + 2400); // 0.1 s around 0.25 s
  2396. CHECK (std::abs (middle - 66) < 6); // ~330 Hz * 0.1 s * 2
  2397. CHECK (std::abs (crossings (l, 48000, 96000) - 880) < 4); // reached A4 (1 s = 880 crossings)
  2398. // AM: osc 3 at level 1.0, slow sine, modulates osc 1 (the average level drops, the sum doesn't gain osc 3)
  2399. lynx::SynthSettings am;
  2400. am.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  2401. am.osc[2] = { lynx::Waveform::sine, -3, 0.0f, 1.0f };
  2402. am.cutoff = 20000.0f;
  2403. am.am = true;
  2404. auto modulated = make (am);
  2405. modulated->noteOn (69, 1.0f);
  2406. std::fill (l.begin(), l.end(), 0.0f);
  2407. modulated->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
  2408. float peak = 0.0f;
  2409. for (std::size_t i = 4800; i < 48000; ++i)
  2410. peak = std::max (peak, std::abs (l[i]));
  2411. CHECK (peak > 0.45f && peak < 0.65f); // 0.3 · (1 + 1) at most: (1 − o3) goes up to 2
  2412. }
  2413. TEST (channel_lfo_vibrato_and_tremolo)
  2414. {
  2415. auto render = [] (const lynx::SynthSettings& s) {
  2416. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  2417. inst->prepare (48000.0);
  2418. lynx::ChannelDef c;
  2419. c.synth = s;
  2420. inst->configure (c, 120.0);
  2421. inst->noteOn (69, 1.0f);
  2422. std::vector<float> l (48000), r (48000);
  2423. inst->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
  2424. return l;
  2425. };
  2426. lynx::SynthSettings s;
  2427. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  2428. s.cutoff = 20000.0f;
  2429. // tremolo: 100% volume LFO at 4 Hz (0.25 s period) -> the amplitude envelope goes from ~0 to ~2x
  2430. auto tremolo = s;
  2431. tremolo.lfo[lynx::lfoVolume] = { 1.0f, 0.25f, false, 0, 0.0f, 0.0f };
  2432. const auto t = render (tremolo);
  2433. float lowest = 1.0f, highest = 0.0f;
  2434. for (std::size_t w = 4800; w + 480 <= 48000; w += 480) // 10 ms windows (4.4 cycles of 440 Hz)
  2435. {
  2436. float peak = 0.0f;
  2437. for (auto i = w; i < w + 480; ++i)
  2438. peak = std::max (peak, std::abs (t[i]));
  2439. lowest = std::min (lowest, peak);
  2440. highest = std::max (highest, peak);
  2441. }
  2442. CHECK (lowest < 0.08f && highest > 0.5f);
  2443. // synced vibrato: ±1 semitone, 1-beat period (0.5 s at 120 BPM); the average stays at 440 Hz
  2444. auto vibrato = s;
  2445. vibrato.lfo[lynx::lfoPitch] = { 1.0f, 1.0f, true, 0, 0.0f, 0.0f };
  2446. const auto v = render (vibrato);
  2447. int crossings = 0;
  2448. for (std::size_t i = 1; i < 48000; ++i)
  2449. crossings += (v[i - 1] < 0.0f) != (v[i] < 0.0f);
  2450. CHECK (std::abs (crossings - 880) < 12);
  2451. int firstQuarter = 0; // first quarter of the cycle: rising sine -> higher than 440
  2452. for (std::size_t i = 1; i < 6000; ++i)
  2453. firstQuarter += (v[i - 1] < 0.0f) != (v[i] < 0.0f);
  2454. CHECK (firstQuarter > 112);
  2455. }
  2456. TEST (sampler_porta_glides_from_previous_note)
  2457. {
  2458. auto data = std::make_shared<lynx::SampleData>();
  2459. data->sampleRate = 48000.0;
  2460. data->left.resize (96000);
  2461. for (std::size_t i = 0; i < data->left.size(); ++i)
  2462. data->left[i] = 0.5f * (float) std::sin (6.283185307 * 220.0 * (double) i / 48000.0);
  2463. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  2464. inst->prepare (48000.0);
  2465. lynx::ChannelDef c;
  2466. c.sampler.cut = true;
  2467. c.sampler.porta = true;
  2468. c.sampler.portaBeats = 1.0f; // 0.5 s at 120 BPM
  2469. inst->configure (c, 120.0);
  2470. std::vector<float> l (24000), r (24000);
  2471. inst->noteOn (60, 1.0f);
  2472. inst->render (l.data(), r.data(), 2400, 1.0f, 1.0f);
  2473. inst->noteOn (72, 1.0f); // one octave up: starts at 220 Hz and rises
  2474. std::fill (l.begin(), l.end(), 0.0f);
  2475. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  2476. auto crossings = [&] (std::size_t from, std::size_t to) {
  2477. int n = 0;
  2478. for (auto i = from + 1; i < to; ++i)
  2479. n += (l[i - 1] < 0.0f) != (l[i] < 0.0f);
  2480. return n;
  2481. };
  2482. const auto start = crossings (600, 2400); // 37.5 ms still near 220 Hz: ~17 crossings (440 Hz would give 33)
  2483. const auto end = crossings (21600, 24000); // 50 ms at the end of the glide: near 440 Hz (~44; 220 Hz would give 22)
  2484. CHECK (start < 22);
  2485. CHECK (end > 36);
  2486. }
  2487. TEST (zoned_sampler_picks_zone_by_key_and_velocity)
  2488. {
  2489. auto sine = [] (double hz, float amp) {
  2490. auto d = std::make_shared<lynx::SampleData>();
  2491. d->sampleRate = 48000.0;
  2492. d->left.resize (48000);
  2493. for (std::size_t i = 0; i < d->left.size(); ++i)
  2494. d->left[i] = amp * (float) std::sin (6.283185307 * hz * (double) i / 48000.0);
  2495. return d;
  2496. };
  2497. // lows: 220 Hz sine recorded as A3 (57), up to key 69; highs: 880 Hz as A5 (81)
  2498. // hard layer on the highs: same tuning, louder
  2499. std::vector<lynx::LoadedZone> zones {
  2500. { { "graves.wav", 57, 0, 69, 0, 127, 0.0f, 1.0f }, sine (220.0, 0.5f) },
  2501. { { "agudos.wav", 81, 70, 127, 0, 99, 0.0f, 1.0f }, sine (880.0, 0.25f) },
  2502. { { "agudos forte.wav", 81, 70, 127, 100, 127, 0.0f, 1.0f }, sine (880.0, 0.9f) },
  2503. };
  2504. auto inst = lynx::makeZonedSampler (zones);
  2505. inst->prepare (48000.0);
  2506. inst->configure (lynx::ChannelDef {}, 120.0);
  2507. auto play = [&] (int key, float velocity) {
  2508. inst->allNotesOff();
  2509. std::vector<float> l (24000), r (24000);
  2510. inst->render (l.data(), r.data(), 2000, 1.0f, 1.0f); // let the old voices die out
  2511. std::fill (l.begin(), l.end(), 0.0f);
  2512. inst->noteOn (key, velocity);
  2513. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  2514. int crossings = 0;
  2515. float peak = 0.0f;
  2516. for (std::size_t i = 1; i < 24000; ++i)
  2517. {
  2518. crossings += (l[i - 1] < 0.0f) != (l[i] < 0.0f);
  2519. peak = std::max (peak, std::abs (l[i]));
  2520. }
  2521. return std::pair { crossings, peak };
  2522. };
  2523. const auto low = play (69, 0.5f); // A4 from root A3: 440 Hz -> 440 crossings in 0.5 s
  2524. CHECK (std::abs (low.first - 440) < 4);
  2525. const auto soft = play (81, 0.5f); // high zone at the root: 880 Hz
  2526. CHECK (std::abs (soft.first - 880) < 6);
  2527. const auto hard = play (81, 0.95f); // high velocity: hard layer
  2528. CHECK (hard.second > soft.second * 2.0f);
  2529. // overlapping layers round-robin: two identical zones at different levels, notes alternate
  2530. auto rr = lynx::makeZonedSampler ({ { { "a.wav", 60 }, sine (440.0, 0.2f) }, { { "b.wav", 60 }, sine (440.0, 0.8f) } });
  2531. rr->prepare (48000.0);
  2532. rr->configure (lynx::ChannelDef {}, 120.0);
  2533. float peaks[2] {};
  2534. for (int k = 0; k < 2; ++k)
  2535. {
  2536. rr->allNotesOff();
  2537. std::vector<float> l (4800), r (4800);
  2538. rr->render (l.data(), r.data(), 2000, 1.0f, 1.0f);
  2539. std::fill (l.begin(), l.end(), 0.0f);
  2540. rr->noteOn (60, 1.0f);
  2541. rr->render (l.data(), r.data(), 4800, 1.0f, 1.0f);
  2542. for (auto v : l)
  2543. peaks[k] = std::max (peaks[k], std::abs (v));
  2544. }
  2545. CHECK (std::abs (peaks[0] - peaks[1]) > 0.3f);
  2546. }
  2547. TEST (zoned_sampler_loops_while_held)
  2548. {
  2549. // 0.1 s sample with a loop in the middle: holding the note it keeps sounding well past the end of the file;
  2550. // on release, it leaves the loop and ends
  2551. auto data = std::make_shared<lynx::SampleData>();
  2552. data->sampleRate = 48000.0;
  2553. data->left.resize (4800);
  2554. for (std::size_t i = 0; i < data->left.size(); ++i)
  2555. data->left[i] = 0.5f * (float) std::sin (6.283185307 * 480.0 * (double) i / 48000.0); // 100 frames per cycle
  2556. lynx::SampleZone zone { "loop.wav", 60 };
  2557. zone.loopStart = 1000;
  2558. zone.loopEnd = 3000;
  2559. zone.loopMode = 2;
  2560. auto inst = lynx::makeZonedSampler ({ { zone, data } });
  2561. inst->prepare (48000.0);
  2562. lynx::ChannelDef c;
  2563. c.sampler.release = 0.01f;
  2564. inst->configure (c, 120.0);
  2565. inst->noteOn (60, 1.0f);
  2566. std::vector<float> l (48000), r (48000);
  2567. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f); // 0.5 s held
  2568. float held = 0.0f;
  2569. for (std::size_t i = 20000; i < 24000; ++i)
  2570. held = std::max (held, std::abs (l[i]));
  2571. CHECK (held > 0.3f);
  2572. inst->noteOff (60);
  2573. inst->render (l.data() + 24000, r.data() + 24000, 24000, 1.0f, 1.0f);
  2574. float after = 0.0f;
  2575. for (std::size_t i = 40000; i < 48000; ++i)
  2576. after = std::max (after, std::abs (l[i]));
  2577. CHECK (after == 0.0f);
  2578. }
  2579. TEST (global_lfo_follows_song_position)
  2580. {
  2581. // global volume LFO (1-beat period): a note mid-cycle starts mid-wave; the
  2582. // same note with a per-note LFO starts at phase 0
  2583. auto first = [] (bool global, double songTicks) {
  2584. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  2585. inst->prepare (48000.0);
  2586. lynx::ChannelDef c;
  2587. c.synth.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  2588. c.synth.cutoff = 20000.0f;
  2589. c.synth.lfo[lynx::lfoVolume] = { 1.0f, 1.0f, true, 2, 0.0f, 0.0f, global }; // pulse: +1 in the first half
  2590. inst->configure (c, 120.0);
  2591. inst->setSongPosition (songTicks);
  2592. inst->noteOn (69, 1.0f);
  2593. std::vector<float> l (4800), r (4800);
  2594. inst->render (l.data(), r.data(), 4800, 1.0f, 1.0f);
  2595. float peak = 0.0f;
  2596. for (std::size_t i = 2400; i < 4800; ++i)
  2597. peak = std::max (peak, std::abs (l[i]));
  2598. return peak;
  2599. };
  2600. // mid-cycle (phase 0.6) the pulse is at −1: volume ~0; per note (phase 0) it is at +1: ~2x
  2601. const auto globalLate = first (true, 0.6 * lynx::ppq);
  2602. const auto perNote = first (false, 0.6 * lynx::ppq);
  2603. CHECK (globalLate < 0.05f);
  2604. CHECK (perNote > 0.4f);
  2605. }
  2606. TEST (sampler_channel_filter_darkens_and_bypasses_when_open)
  2607. {
  2608. // white noise in the sampler: with a 300 Hz cutoff the energy drops a lot; open passes unchanged
  2609. auto data = std::make_shared<lynx::SampleData>();
  2610. data->sampleRate = 48000.0;
  2611. data->left.resize (48000);
  2612. std::uint32_t seed = 7;
  2613. for (auto& v : data->left)
  2614. {
  2615. seed = seed * 1664525u + 1013904223u;
  2616. v = ((float) (seed >> 8) / 16777216.0f - 0.5f);
  2617. }
  2618. data->right = data->left;
  2619. auto energy = [&] (float cutoff) {
  2620. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  2621. inst->prepare (48000.0);
  2622. lynx::ChannelDef c;
  2623. c.sampler.cutoff = cutoff;
  2624. inst->configure (c, 120.0);
  2625. inst->noteOn (60, 1.0f);
  2626. std::vector<float> l (24000), r (24000);
  2627. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  2628. double e = 0.0;
  2629. for (auto v : l)
  2630. e += (double) v * v;
  2631. return e;
  2632. };
  2633. const auto open = energy (20000.0f), dark = energy (300.0f);
  2634. {
  2635. // high-pass at 10 kHz: also removes most of the noise energy, but from the other side
  2636. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  2637. inst->prepare (48000.0);
  2638. lynx::ChannelDef hp;
  2639. hp.sampler.cutoff = 10000.0f;
  2640. hp.sampler.filterType = 2;
  2641. inst->configure (hp, 120.0);
  2642. inst->noteOn (60, 1.0f);
  2643. std::vector<float> l (24000), r (24000);
  2644. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  2645. double e = 0.0, lowE = 0.0;
  2646. float lp = 0.0f;
  2647. for (auto v : l)
  2648. {
  2649. e += (double) v * v;
  2650. lp += 0.01f * (v - lp); // crude low-pass: almost nothing should remain below
  2651. lowE += (double) lp * lp;
  2652. }
  2653. CHECK (e > 10.0 && e < 0.7 * energy (20000.0f));
  2654. CHECK (lowE < e * 0.01);
  2655. }
  2656. CHECK (open > 100.0);
  2657. CHECK (dark < open * 0.05);
  2658. // +12 st pitch envelope with sustain 1: the sample runs at double speed
  2659. // and ends in half the time (1 s of sample -> 0.5 s)
  2660. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  2661. inst->prepare (48000.0);
  2662. lynx::ChannelDef c;
  2663. c.sampler.pitchEnv = { 12.0f, 0.0f, 0.0f, 0.1f, 1.0f, 0.1f };
  2664. inst->configure (c, 120.0);
  2665. inst->noteOn (60, 1.0f);
  2666. std::vector<float> l (48000), r (48000);
  2667. inst->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
  2668. float tail = 0.0f, body = 0.0f;
  2669. for (std::size_t i = 20000; i < 23000; ++i)
  2670. body = std::max (body, std::abs (l[i]));
  2671. for (std::size_t i = 25000; i < 48000; ++i)
  2672. tail = std::max (tail, std::abs (l[i]));
  2673. CHECK (body > 0.1f);
  2674. CHECK (tail == 0.0f);
  2675. }
  2676. TEST (synth_oscillator_pan_and_semitones)
  2677. {
  2678. auto render = [] (lynx::SynthSettings s, std::vector<float>& l, std::vector<float>& r) {
  2679. auto synth = lynx::makeInstrument (lynx::InstrumentKind::synth);
  2680. synth->prepare (48000.0);
  2681. lynx::ChannelDef c;
  2682. c.synth = s;
  2683. synth->configure (c, 120.0);
  2684. synth->noteOn (69, 1.0f);
  2685. l.assign (24000, 0.0f);
  2686. r.assign (24000, 0.0f);
  2687. synth->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  2688. };
  2689. lynx::SynthSettings s;
  2690. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f, 7, 1.0f }; // a fifth up, fully right
  2691. s.cutoff = 20000.0f;
  2692. std::vector<float> l, r;
  2693. render (s, l, r);
  2694. float peakL = 0.0f, peakR = 0.0f;
  2695. int crossings = 0;
  2696. for (std::size_t i = 4800; i < 24000; ++i)
  2697. {
  2698. peakL = std::max (peakL, std::abs (l[i]));
  2699. peakR = std::max (peakR, std::abs (r[i]));
  2700. crossings += (r[i - 1] < 0.0f) != (r[i] < 0.0f);
  2701. }
  2702. CHECK (peakL < 1e-4f && peakR > 0.1f);
  2703. // 440 * 2^(7/12) = 659.3 Hz; 0.4 s = 263.7 cycles = ~527 crossings
  2704. CHECK (std::abs (crossings - 527) < 6);
  2705. }
  2706. TEST (fl_wrapper_state_parses_records_and_vst3_state)
  2707. {
  2708. // Fruity Wrapper blob (version 12): VST3 generator type, class ID, name, path and state
  2709. std::vector<std::uint8_t> blob;
  2710. 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))); };
  2711. auto record = [&] (std::vector<std::uint8_t>& b, std::uint32_t id, const std::vector<std::uint8_t>& payload) {
  2712. u32 (b, id);
  2713. u32 (b, (std::uint32_t) payload.size());
  2714. u32 (b, 0);
  2715. b.insert (b.end(), payload.begin(), payload.end());
  2716. };
  2717. auto bytes = [] (const std::string& s) { return std::vector<std::uint8_t> (s.begin(), s.end()); };
  2718. std::vector<std::uint8_t> vst3State;
  2719. u32 (vst3State, 1);
  2720. record (vst3State, 1, std::vector<std::uint8_t> (64, 0));
  2721. record (vst3State, 3, bytes ("COMPONENTE"));
  2722. record (vst3State, 2, bytes ("CONTROLADOR"));
  2723. // record 4: 3 slots → ParamIDs 700, −1 (empty), 0xFFFFFF01
  2724. std::vector<std::uint8_t> slots;
  2725. for (auto v : { 3u, 700u, 0xFFFFFFFFu, 0xFFFFFF01u })
  2726. u32 (slots, v);
  2727. record (vst3State, 4, slots);
  2728. u32 (blob, 12);
  2729. record (blob, 0x01, std::vector<std::uint8_t> (20, 0xFF));
  2730. record (blob, 0x32, { 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });
  2731. std::vector<std::uint8_t> cid;
  2732. for (int i = 0; i < 16; ++i)
  2733. cid.push_back ((std::uint8_t) (0xA0 + i));
  2734. record (blob, 0x34, cid);
  2735. record (blob, 0x36, bytes ("Surge XT"));
  2736. record (blob, 0x37, bytes ("C:/Program Files/Common Files/VST3/Surge XT.vst3"));
  2737. record (blob, 0x35, vst3State);
  2738. lynx::ForeignPlugin p;
  2739. CHECK (lynx::parseFlWrapper (blob, p));
  2740. CHECK (p.format == lynx::ForeignPlugin::Format::vst3);
  2741. CHECK (p.hasClassId && p.classId[0] == 0xA0 && p.classId[15] == 0xAF);
  2742. CHECK (p.name == "Surge XT" && p.path.find ("Surge XT.vst3") != std::string::npos);
  2743. std::vector<std::uint8_t> component, controller;
  2744. CHECK (lynx::splitFlVst3State (p.state, component, controller));
  2745. CHECK (component == bytes ("COMPONENTE") && controller == bytes ("CONTROLADOR"));
  2746. const auto saved = lynx::flVst3ParamSlots (p.state);
  2747. CHECK (saved == (std::vector<std::int32_t> { 700, -1, (std::int32_t) 0xFFFFFF01u }));
  2748. // FL's slot assignment: saved slots keep their ParamID wherever it now sits; the empty slot stays empty;
  2749. // a vanished ParamID (0xFFFFFF01) leaves its slot dead; parameters without a slot get slots 3, 4… in list order
  2750. const auto positions = lynx::flVst3SlotPositions (saved, { 5u, 700u, 9u });
  2751. CHECK (positions == (std::vector<int> { 1, -1, -1, 0, 2 }));
  2752. CHECK (lynx::flVst3SlotPositions ({}, { 5u, 6u }) == (std::vector<int> { 0, 1 })); // new instance: slot = position
  2753. // version out of range or no type: not a usable wrapper
  2754. blob[0] = 13;
  2755. CHECK (! lynx::parseFlWrapper (blob, p));
  2756. CHECK (! lynx::parseFlWrapper ({ 12, 0, 0, 0 }, p));
  2757. }
  2758. TEST (schedule_index_matches_full_scan)
  2759. {
  2760. // random song (long notes, swing, clips with offset); indexed scheduling must yield
  2761. // exactly the same events as the full scan, including in the block that wraps around
  2762. lynx::Song s = lynx::Song::makeDefault();
  2763. s.swing = 0.6f;
  2764. std::uint32_t seed = 12345;
  2765. auto rnd = [&] (int n) { seed = seed * 1664525u + 1013904223u; return (int) ((seed >> 8) % (std::uint32_t) n); };
  2766. s.patterns.resize (3);
  2767. for (auto& p : s.patterns)
  2768. {
  2769. p.notes.clear();
  2770. for (int i = 0; i < 60; ++i)
  2771. {
  2772. lynx::Note n;
  2773. n.start = rnd (16 * lynx::ppq);
  2774. n.length = rnd (5) == 0 ? rnd (12 * lynx::ppq) + 1 : rnd (lynx::ppq) + 1;
  2775. n.channel = rnd (4);
  2776. n.key = 40 + rnd (40);
  2777. n.slide = rnd (20) == 0;
  2778. p.notes.push_back (n);
  2779. }
  2780. p.sortNotes();
  2781. }
  2782. s.playlist.clear();
  2783. for (int i = 0; i < 12; ++i)
  2784. {
  2785. lynx::Clip c;
  2786. c.pattern = rnd (3);
  2787. c.track = rnd (4);
  2788. c.start = rnd (40) * lynx::ppq;
  2789. c.length = (rnd (12) + 1) * lynx::ppq;
  2790. c.offset = rnd (4) * lynx::ppq;
  2791. s.playlist.push_back (c);
  2792. }
  2793. s.mode = lynx::PlayMode::song;
  2794. const auto index = lynx::makeScheduleIndex (s);
  2795. const auto tps = lynx::ticksPerSample (140.0, 48000.0);
  2796. const auto loop = (double) s.songLength();
  2797. int mismatches = 0, events = 0;
  2798. for (int block = 0; block < 400; ++block)
  2799. {
  2800. const auto start = block == 0 ? loop - 100.0 * tps : (double) rnd ((int) loop * 2);
  2801. const auto samples = 64 + rnd (24000); // up to ~1 beat per block
  2802. lynx::EventBuffer a, b;
  2803. lynx::scheduleSong (s, start, samples, tps, a, false, nullptr);
  2804. lynx::scheduleSong (s, start, samples, tps, b, false, &index);
  2805. events += a.size();
  2806. if (a.size() != b.size())
  2807. {
  2808. ++mismatches;
  2809. continue;
  2810. }
  2811. for (int i = 0; i < a.size(); ++i)
  2812. {
  2813. const auto& x = *(a.begin() + i);
  2814. const auto& y = *(b.begin() + i);
  2815. mismatches += x.sampleOffset != y.sampleOffset || x.type != y.type || x.key != y.key || x.channel != y.channel;
  2816. }
  2817. }
  2818. CHECK (events > 500);
  2819. CHECK (mismatches == 0);
  2820. }
  2821. TEST (mixer_track_eq_defaults_and_boost)
  2822. {
  2823. // FL defaults: 90 Hz, 1.5 kHz and 8 kHz; peak Q 0.632; shelf slope 0.607
  2824. lynx::TrackEq eq;
  2825. CHECK (std::abs (lynx::TrackEq::peakQ (eq.width[1]) - 0.632) < 0.01);
  2826. CHECK (std::abs (lynx::TrackEq::shelfSlope (eq.width[0]) - 0.607) < 0.01);
  2827. CHECK (! eq.active());
  2828. // +12 dB peak at 1.5 kHz on a track: a 1.5 kHz sine rises ~4x
  2829. lynx::Song song = lynx::Song::makeDefault();
  2830. song.mixer[1].eq.gainDb[1] = 12.0f;
  2831. for (auto& c : song.channels)
  2832. c.mixerTrack = 1;
  2833. song.channels[4].instrument = lynx::InstrumentKind::synth;
  2834. song.channels[4].synth = {};
  2835. song.channels[4].synth.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  2836. song.channels[4].synth.cutoff = 20000.0f;
  2837. auto render = [] (const lynx::Song& s) {
  2838. auto state = lynx::RackState::build (s, nullptr, 48000.0);
  2839. lynx::RackRenderer renderer;
  2840. renderer.prepare (48000.0);
  2841. lynx::LiveEventQueue live;
  2842. lynx::NoteEvent on; // 1.5 kHz ~ key 90.6: use 91 (1568 Hz), still within the wide peak
  2843. on.channel = 4;
  2844. on.key = 91;
  2845. on.velocity = 1.0f;
  2846. live.push (on);
  2847. std::vector<float> l (24000), r (24000);
  2848. for (int pos = 0; pos < 24000; pos += 480)
  2849. renderer.process (state.get(), false, 0.0, 480, l.data() + pos, r.data() + pos, pos == 0 ? &live : nullptr);
  2850. float peak = 0.0f;
  2851. for (std::size_t i = 12000; i < 24000; ++i)
  2852. peak = std::max (peak, std::abs (l[i]));
  2853. return peak;
  2854. };
  2855. const auto boosted = render (song);
  2856. song.mixer[1].eq.gainDb[1] = 0.0f;
  2857. const auto flat = render (song);
  2858. CHECK (flat > 0.01f);
  2859. CHECK (boosted / flat > 3.0f && boosted / flat < 4.2f);
  2860. }
  2861. TEST (bars_follow_time_signature_markers)
  2862. {
  2863. lynx::Song song;
  2864. song.beatsPerBar = 4;
  2865. const auto bar4 = (std::int64_t) lynx::ppq * 4;
  2866. auto bars = song.barsBetween (0, bar4 * 3);
  2867. CHECK (bars.size() == 3 && bars[2].tick == bar4 * 2 && bars[2].number == 3);
  2868. // 7/4 from bar 3, and 3/4 in the middle of a 7 bar (cuts and restarts)
  2869. song.addMarker ({ bar4 * 2, "Verso", 7, 4 });
  2870. song.addMarker ({ bar4, "Nome só", 0, 4 });
  2871. const auto seven = (std::int64_t) lynx::ppq * 7;
  2872. song.addMarker ({ bar4 * 2 + seven + lynx::ppq * 2, "", 3, 4 });
  2873. bars = song.barsBetween (0, bar4 * 2 + seven * 2);
  2874. CHECK (bars.size() >= 5);
  2875. CHECK (bars[1].length == bar4 && bars[2].tick == bar4 * 2 && bars[2].length == seven);
  2876. CHECK (bars[3].length == lynx::ppq * 2); // cut short by the change
  2877. CHECK (bars[4].length == lynx::ppq * 3 && bars[4].number == 5);
  2878. CHECK (song.markers()[0].name == "Nome só"); // sorted by tick
  2879. // from far away: starts at the right bar without losing the numbering
  2880. const auto far = song.barsBetween (lynx::ppq * 3000, lynx::ppq * 3001);
  2881. CHECK (far.size() == 1 && far[0].length == lynx::ppq * 3);
  2882. CHECK (song.barAt (lynx::ppq * 3000).number == far[0].number);
  2883. CHECK ((lynx::ppq * 3000 - far[0].tick) < far[0].length);
  2884. }
  2885. TEST (split_drum_channel_moves_notes_to_pads)
  2886. {
  2887. lynx::Song song = lynx::Song::makeDefault();
  2888. const auto channels = (int) song.channels.size();
  2889. song.patterns[0].notes.clear();
  2890. song.patterns[0].notes.push_back ({ 0, 240, 1, 36, 0.9f }); // kit kick (channel 1)
  2891. song.patterns[0].notes.push_back ({ 240, 240, 1, 38, 0.8f }); // snare
  2892. song.patterns[0].notes.push_back ({ 480, 240, 1, 50, 0.8f }); // key without a pad: dropped
  2893. song.patterns[0].notes.push_back ({ 480, 240, 3, 60, 0.8f }); // other channel: shifts by 1 position
  2894. lynx::Automation a;
  2895. a.target = { lynx::AutomationTargetKind::channelVolume, 3, 0, 0 };
  2896. song.automations.push_back (a);
  2897. std::vector<lynx::DrumPad> pads (2);
  2898. pads[0].name = "Kick";
  2899. pads[0].key = 36;
  2900. pads[0].layers = { { "kick.wav" } };
  2901. pads[1].name = "Snare";
  2902. pads[1].key = 38;
  2903. pads[1].layers = { { "snare soft.wav", 60, 0, 127, 0, 90 }, { "snare hard.wav", 60, 0, 127, 91, 127 } };
  2904. pads[1].cutGroup = 2;
  2905. pads[1].cutBy = 2;
  2906. lynx::splitDrumChannel (song, 1, pads);
  2907. CHECK ((int) song.channels.size() == channels + 1);
  2908. CHECK (song.channels[1].samplePath == "kick.wav" && song.channels[1].instrument == lynx::InstrumentKind::sampler);
  2909. CHECK (song.channels[2].zones.size() == 2 && song.channels[2].cutGroup == song.channels[2].cutBy && song.channels[2].cutGroup != 0);
  2910. const auto& notes = song.patterns[0].notes;
  2911. CHECK (notes.size() == 3);
  2912. CHECK (notes[0].channel == 1 && notes[0].key == 60);
  2913. CHECK (notes[1].channel == 2 && notes[1].key == 60);
  2914. CHECK (notes[2].channel == 4); // the old channel 3
  2915. CHECK (song.automations[0].target.index == 4);
  2916. }
  2917. TEST (flp_import_brings_playlist)
  2918. {
  2919. flp::Project p;
  2920. p.header.ppq = 96;
  2921. p.channels = { { 0, flp::ChannelType::sampler, "Kick", "", "", 1 } };
  2922. flp::Pattern empty;
  2923. empty.index = 1;
  2924. flp::Pattern full;
  2925. full.index = 2;
  2926. flp::Note n;
  2927. n.channel = 0;
  2928. full.notes.push_back (n);
  2929. p.patterns = { empty, full };
  2930. flp::Arrangement a;
  2931. flp::PlaylistItem item;
  2932. item.kind = flp::PlaylistItem::Kind::pattern;
  2933. item.index = 2;
  2934. item.position = 384;
  2935. item.length = 192;
  2936. item.track = 3;
  2937. item.patternStartTick = 96;
  2938. a.items.push_back (item);
  2939. item.index = 1; // empty pattern: discarded
  2940. a.items.push_back (item);
  2941. item.kind = flp::PlaylistItem::Kind::channelClip; // audio: not supported yet
  2942. a.items.push_back (item);
  2943. p.arrangements.push_back (a);
  2944. const auto s = lynx::importFlp (p);
  2945. CHECK (s.mode == lynx::PlayMode::song);
  2946. CHECK (s.playlist.size() == 1);
  2947. CHECK (s.playlist[0].pattern == 0 && s.playlist[0].track == 3);
  2948. CHECK (s.playlist[0].start == 4 * lynx::ppq && s.playlist[0].length == 2 * lynx::ppq && s.playlist[0].offset == lynx::ppq);
  2949. }
  2950. TEST (rack_releases_notes_on_position_jump)
  2951. {
  2952. lynx::Song s;
  2953. lynx::ChannelDef c;
  2954. c.instrument = lynx::InstrumentKind::synth;
  2955. s.channels.push_back (c);
  2956. auto& p = s.patterns.emplace_back();
  2957. lynx::Note n;
  2958. n.start = 0;
  2959. n.length = 4 * lynx::ppq; // long note
  2960. p.notes.push_back (n);
  2961. s.bpm = 120.0;
  2962. auto state = lynx::RackState::build (s, nullptr, 48000.0);
  2963. lynx::RackRenderer r;
  2964. r.prepare (48000.0);
  2965. std::vector<float> l (512), rr (512);
  2966. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  2967. double tick = 0.0;
  2968. for (int i = 0; i < 20; ++i, tick += 512 * tps)
  2969. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  2970. // jump to tick 2000 (mid-note, no new "on"): the synth must go silent
  2971. for (int i = 0; i < 40; ++i)
  2972. r.process (state.get(), true, 2000.0 + i * 512 * tps, 512, l.data(), rr.data());
  2973. float tail = 0.0f;
  2974. for (auto v : l)
  2975. tail = std::max (tail, std::abs (v));
  2976. CHECK (tail < 0.001f);
  2977. }
  2978. namespace
  2979. {
  2980. // Two samplers with a constant 0.5 signal on different inserts; returns the output peak.
  2981. float juce_db (float gain)
  2982. {
  2983. return 20.0f * std::log10 (gain);
  2984. }
  2985. float renderMixerCase (const std::function<void (lynx::Song&)>& tweak, lynx::MeterBank* meters = nullptr)
  2986. {
  2987. lynx::Song s;
  2988. s.mixer = lynx::Song::defaultMixer (4);
  2989. for (int i = 0; i < 2; ++i)
  2990. {
  2991. lynx::ChannelDef c;
  2992. c.instrument = lynx::InstrumentKind::sampler;
  2993. c.samplePath = "dc";
  2994. c.volume = 1.0f;
  2995. c.mixerTrack = i + 1;
  2996. s.channels.push_back (c);
  2997. }
  2998. s.patterns.emplace_back();
  2999. tweak (s);
  3000. auto provider = [] (const std::string&) {
  3001. auto d = std::make_shared<lynx::SampleData>();
  3002. d->left.assign (48000, 0.5f);
  3003. return std::shared_ptr<const lynx::SampleData> (d);
  3004. };
  3005. auto state = lynx::RackState::build (s, nullptr, 48000.0, provider);
  3006. lynx::RackRenderer r;
  3007. r.prepare (48000.0, 256);
  3008. lynx::LiveEventQueue live;
  3009. for (int ch = 0; ch < 2; ++ch)
  3010. {
  3011. lynx::NoteEvent on;
  3012. on.channel = ch;
  3013. on.velocity = 1.0f;
  3014. live.push (on);
  3015. }
  3016. std::vector<float> l (1000), rr (1000); // larger than the mixer block: tests the chunking
  3017. r.process (state.get(), false, 0.0, 1000, l.data(), rr.data(), &live, meters);
  3018. return std::max (std::abs (l[999]), std::abs (rr[999]));
  3019. }
  3020. } // namespace
  3021. TEST (mixer_routes_mutes_solos_and_meters)
  3022. {
  3023. // two 0.5 channels summed on the Master at 0 dB
  3024. CHECK (std::abs (renderMixerCase ([] (lynx::Song&) {}) - 1.0f) < 1e-4f);
  3025. // insert 1 muted: only channel 2 remains
  3026. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.mixer[1].muted = true; }) - 0.5f) < 1e-4f);
  3027. // solo on insert 2: same
  3028. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.mixer[2].solo = true; }) - 0.5f) < 1e-4f);
  3029. // FL fader curve: 0.8 = 0 dB, max ≈ +5.6 dB, half travel ≈ -12.7 dB
  3030. CHECK (std::abs (lynx::faderGain (0.8f) - 1.0f) < 1e-5f);
  3031. CHECK (std::abs (juce_db (lynx::faderGain (1.0f)) - 5.59f) < 0.05f);
  3032. CHECK (lynx::faderGain (0.0f) == 0.0f);
  3033. // Master fader at max
  3034. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.mixer[0].volume = 1.0f; }) - lynx::faderGain (1.0f)) < 1e-4f);
  3035. // both channels on the same insert, insert fader at 0.4
  3036. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) {
  3037. s.channels[1].mixerTrack = 1;
  3038. s.mixer[1].volume = 0.4f;
  3039. }) - lynx::faderGain (0.4f)) < 1e-4f);
  3040. // a route outside the mixer falls to the last existing insert, without crashing
  3041. CHECK (renderMixerCase ([] (lynx::Song& s) { s.channels[0].mixerTrack = 100; }) > 0.9f);
  3042. // no mixer: everything straight to the output at 0 dB
  3043. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.mixer.clear(); }) - 1.0f) < 1e-4f);
  3044. lynx::MeterBank meters;
  3045. renderMixerCase ([] (lynx::Song&) {}, &meters);
  3046. CHECK (std::abs (meters.takePeak (1, 0) - 0.5f) < 1e-4f);
  3047. CHECK (std::abs (meters.takePeak (0, 1) - 1.0f) < 1e-4f);
  3048. CHECK (meters.takePeak (1, 0) == 0.0f); // reading resets
  3049. CHECK (meters.takePeak (3, 0) == 0.0f); // insert with no channel
  3050. }
  3051. TEST (next_free_insert_and_flp_mixer_import)
  3052. {
  3053. auto s = lynx::Song::makeDefault();
  3054. CHECK (s.mixer.size() == 17 && s.mixer[0].name == "Master");
  3055. CHECK (s.nextFreeInsert() == 6); // the 5 default channels take 1..5
  3056. flp::Project p;
  3057. p.channels = { { 0, flp::ChannelType::sampler, "Kick", "", "", 30 } };
  3058. p.mixer = { { 0, "" }, { 30, "Drums" } };
  3059. const auto song = lynx::importFlp (p);
  3060. CHECK (song.channels[0].mixerTrack == 30);
  3061. CHECK (song.mixer.size() == 31);
  3062. CHECK (song.mixer[30].name == "Drums" && song.mixer[0].name == "Master");
  3063. }
  3064. namespace
  3065. {
  3066. float runEffectOnSine (lynx::EffectKind kind, const std::function<void (lynx::EffectSlot&)>& tweak, double hz, float amp,
  3067. int frames = 48000)
  3068. {
  3069. auto fx = lynx::makeEffect (kind);
  3070. fx->prepare (48000.0);
  3071. auto slot = lynx::makeEffectSlot (kind);
  3072. tweak (slot);
  3073. std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
  3074. for (int i = 0; i < frames; ++i)
  3075. l[(std::size_t) i] = r[(std::size_t) i] = amp * (float) std::sin (6.283185307 * hz * i / 48000.0);
  3076. for (int i = 0; i < frames; i += 480)
  3077. fx->process (l.data() + i, r.data() + i, std::min (480, frames - i), slot.params, 120.0);
  3078. float peak = 0.0f;
  3079. for (int i = frames / 2; i < frames; ++i) // second half: already settled
  3080. {
  3081. CHECK (std::isfinite (l[(std::size_t) i]));
  3082. peak = std::max (peak, std::abs (l[(std::size_t) i]));
  3083. }
  3084. return peak;
  3085. }
  3086. } // namespace
  3087. TEST (parametric_eq_bands_and_response)
  3088. {
  3089. // default: transparent
  3090. auto slot = lynx::makeEffectSlot (lynx::EffectKind::parametricEq);
  3091. CHECK (std::abs (lynx::peq::responseDb (slot.params, 48000.0, 1000.0)) < 0.01);
  3092. CHECK (std::abs (runEffectOnSine (lynx::EffectKind::parametricEq, [] (auto&) {}, 1000.0, 0.5f) - 0.5f) < 0.005f);
  3093. // +12 dB peak at 1 kHz on band 4: the response and the signal agree
  3094. auto boost = [] (lynx::EffectSlot& s) {
  3095. s.params[(std::size_t) lynx::peq::param (3, 1)] = lynx::peq::hzValue (1000.0);
  3096. s.params[(std::size_t) lynx::peq::param (3, 2)] = lynx::peq::gainValue (12.0);
  3097. };
  3098. boost (slot);
  3099. CHECK (std::abs (lynx::peq::responseDb (slot.params, 48000.0, 1000.0) - 12.0) < 0.2);
  3100. CHECK (std::abs (lynx::peq::responseDb (slot.params, 48000.0, 100.0)) < 1.0);
  3101. const auto peak = runEffectOnSine (lynx::EffectKind::parametricEq, boost, 1000.0, 0.1f);
  3102. CHECK (std::abs (peak - 0.1f * std::pow (10.0f, 12.0f / 20.0f)) < 0.01f);
  3103. // low cut at 48 dB/oct at 200 Hz: 50 Hz (2 octaves below) drops ~96 dB; 2 kHz passes
  3104. auto cut = lynx::makeEffectSlot (lynx::EffectKind::parametricEq);
  3105. cut.params[(std::size_t) lynx::peq::param (0, 0)] = lynx::peq::typeValue (lynx::peq::BandType::lowCut);
  3106. cut.params[(std::size_t) lynx::peq::param (0, 1)] = lynx::peq::hzValue (200.0);
  3107. cut.params[(std::size_t) lynx::peq::param (0, 3)] = lynx::peq::qValue (0.70710678); // Butterworth
  3108. cut.params[(std::size_t) (lynx::peq::slopeParam + 0)] = 1.0f;
  3109. CHECK (lynx::peq::responseDb (cut.params, 48000.0, 50.0) < -80.0);
  3110. CHECK (std::abs (lynx::peq::responseDb (cut.params, 48000.0, 200.0) + 3.0) < 0.5); // -3 dB at the cutoff (Butterworth)
  3111. CHECK (std::abs (lynx::peq::responseDb (cut.params, 48000.0, 2000.0)) < 0.1);
  3112. // round-trip conversions
  3113. CHECK (std::abs (lynx::peq::hz (lynx::peq::hzValue (440.0)) - 440.0) < 0.5);
  3114. CHECK (std::abs (lynx::peq::q (lynx::peq::qValue (2.5)) - 2.5) < 0.01);
  3115. CHECK (lynx::peq::type (lynx::peq::typeValue (lynx::peq::BandType::notch)) == lynx::peq::BandType::notch);
  3116. }
  3117. TEST (fl_parametric_eq2_state_maps_to_bands)
  3118. {
  3119. // v8 blob: band 1 low cut at 100 Hz "Steep 8", band 4 peak +6 dB at 1 kHz, output −3 dB
  3120. std::vector<std::uint8_t> blob (354, 0);
  3121. auto put = [&] (std::size_t off, std::int32_t v) { std::memcpy (blob.data() + off, &v, 4); };
  3122. put (0, 8);
  3123. auto param = [&] (int i, std::int32_t v) { put (4 + 4 * (std::size_t) i, v); };
  3124. auto hzRaw = [] (double hz) { return (std::int32_t) std::lround (std::log (hz / 20.0) / std::log (1000.0) * 65536.0); };
  3125. for (int b = 0; b < 7; ++b)
  3126. param (21 + b, 0); // all off
  3127. param (21 + 0, 3); // high pass (= low cut)
  3128. param (7 + 0, hzRaw (100.0));
  3129. param (14 + 0, 40124); // default width -> Q ~0.68
  3130. param (28 + 0, -3); // Steep 8 -> 4 biquads
  3131. param (21 + 3, 6); // peaking
  3132. param (7 + 3, hzRaw (1000.0));
  3133. param (3, 600); // +6 dB
  3134. param (14 + 3, 25412);
  3135. param (35, -300); // main level −3 dB
  3136. put (0x13E, 0x7F);
  3137. const auto slot = lynx::effectFromFl ("Fruity Parametric EQ 2", blob);
  3138. CHECK (slot.has_value() && slot->kind == lynx::EffectKind::parametricEq);
  3139. const auto& p = slot->params;
  3140. CHECK (lynx::peq::type (p[(std::size_t) lynx::peq::param (0, 0)]) == lynx::peq::BandType::lowCut);
  3141. CHECK (lynx::peq::flLaw (p) && lynx::peqfl::fromParams (p).order[0] == -3); // FL's own order (REA_rodada50)
  3142. CHECK (lynx::peq::type (p[(std::size_t) lynx::peq::param (3, 0)]) == lynx::peq::BandType::peak);
  3143. CHECK (lynx::peq::type (p[(std::size_t) lynx::peq::param (1, 0)]) == lynx::peq::BandType::off);
  3144. CHECK (std::abs (lynx::peq::responseDb (p, 48000.0, 1000.0) - 3.0) < 0.3); // +6 from the peak −3 from the output
  3145. CHECK (lynx::peq::responseDb (p, 48000.0, 30.0) < -40.0); // 48 dB/oct below 100 Hz
  3146. CHECK (std::abs (lynx::peq::responseDb (p, 48000.0, 8000.0) + 3.0) < 0.3);
  3147. // a band muted by the mask doesn't sound (FL's mask travels with the slot, REA_rodada50)
  3148. put (0x13E, 0x7F & ~(1 << 3));
  3149. const auto muted = lynx::effectFromFl ("Fruity Parametric EQ 2", blob);
  3150. CHECK ((lynx::peqfl::fromParams (muted->params).mask & (1u << 3)) == 0);
  3151. CHECK (std::abs (lynx::peq::responseDb (muted->params, 48000.0, 1000.0) + 3.0) < 0.3);
  3152. }
  3153. TEST (reverb_low_cut_and_predelay)
  3154. {
  3155. // impulse: with 100 ms predelay the wet only starts later; with a 3 kHz low cut a
  3156. // 100 Hz sine barely enters the reverb
  3157. auto run = [] (const std::function<void (lynx::EffectSlot&)>& tweak, double hz, bool impulse) {
  3158. auto fx = lynx::makeEffect (lynx::EffectKind::reverb);
  3159. fx->prepare (48000.0);
  3160. auto slot = lynx::makeEffectSlot (lynx::EffectKind::reverb);
  3161. slot.params[4] = 0.0f; // wet only
  3162. slot.params[3] = 1.0f;
  3163. tweak (slot);
  3164. std::vector<float> l (48000), r (48000);
  3165. for (int i = 0; i < 48000; ++i)
  3166. 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);
  3167. for (int i = 0; i < 48000; i += 480)
  3168. fx->process (l.data() + i, r.data() + i, 480, slot.params, 120.0);
  3169. return l;
  3170. };
  3171. const auto delayed = run ([] (auto& s) { s.params[7] = 0.1f; }, 0.0, true);
  3172. float early = 0.0f, later = 0.0f;
  3173. for (std::size_t i = 0; i < 4800; ++i)
  3174. early = std::max (early, std::abs (delayed[i]));
  3175. for (std::size_t i = 4800; i < 24000; ++i)
  3176. later = std::max (later, std::abs (delayed[i]));
  3177. CHECK (early == 0.0f && later > 0.0f);
  3178. 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; };
  3179. const auto open = energy (run ([] (auto&) {}, 100.0, false));
  3180. const auto cut = energy (run ([] (auto& s) { s.params[5] = 1.0f; }, 100.0, false));
  3181. CHECK (cut < open * 0.01);
  3182. }
  3183. TEST (utility_effect_and_fl_stereo_tools)
  3184. {
  3185. // transparent by default; balance fully right silences the left
  3186. CHECK (std::abs (runEffectOnSine (lynx::EffectKind::utility, [] (auto&) {}, 440.0, 0.5f) - 0.5f) < 0.001f);
  3187. CHECK (runEffectOnSine (lynx::EffectKind::utility, [] (auto& s) { s.params[1] = 1.0f; }, 440.0, 0.5f) < 0.001f);
  3188. // Stereo Shaper at half the knob range (6400): each gain = (3^0.5 − 1)/2 (FL's knob law, REA_rodada54 §2.2)
  3189. std::vector<std::uint8_t> blob (36, 0);
  3190. auto put = [&] (std::size_t off, std::int32_t v) { std::memcpy (blob.data() + off, &v, 4); };
  3191. put (4, 6400); put (8, 6400); put (12, 6400); put (16, 6400);
  3192. const auto slot = lynx::effectFromFl ("Fruity Stereo Shaper", blob);
  3193. CHECK (slot && slot->kind == lynx::EffectKind::stereoShaper);
  3194. auto shaperFx = lynx::makeEffect (*slot);
  3195. shaperFx->prepare (44100.0);
  3196. float zl = 0.0f, zr = 0.0f;
  3197. shaperFx->process (&zl, &zr, 0, slot->params, 120.0); // takes the controls
  3198. shaperFx->reset(); // FL's Flush: the gains jump to their targets
  3199. float l = 1.0f, r = 0.0f;
  3200. shaperFx->process (&l, &r, 1, slot->params, 120.0);
  3201. const auto half = (float) ((std::sqrt (3.0) - 1.0) / 2.0);
  3202. CHECK (std::abs (l - half) < 1e-4f && std::abs (r - half) < 1e-4f);
  3203. auto fx = lynx::makeEffect (lynx::EffectKind::utility);
  3204. // Balance: volume 256 = 0 dB, pan −128 = fully left
  3205. std::vector<std::uint8_t> bal (8, 0);
  3206. std::int32_t pan = -128, vol = 256;
  3207. std::memcpy (bal.data(), &pan, 4);
  3208. std::memcpy (bal.data() + 4, &vol, 4);
  3209. const auto b = lynx::effectFromFl ("Fruity Balance", bal);
  3210. CHECK (b && b->kind == lynx::EffectKind::fruityBalance); // FL's exact law (REA_rodada58 §2): fully left sums R into L
  3211. if (b)
  3212. {
  3213. auto balance = lynx::makeEffect (*b);
  3214. balance->prepare (44100.0);
  3215. l = 0.5f; r = 0.5f;
  3216. balance->process (&l, &r, 1, b->params, 120.0);
  3217. CHECK (std::abs (l - 0.70710678f) < 1e-6f && r == 0.0f);
  3218. }
  3219. }
  3220. TEST (effects_eq_compressor_behave)
  3221. {
  3222. // EQ at default is transparent
  3223. CHECK (std::abs (runEffectOnSine (lynx::EffectKind::eq, [] (auto&) {}, 1000.0, 0.5f) - 0.5f) < 0.01f);
  3224. // +18 dB on the lows: 60 Hz rises a lot, 5 kHz barely
  3225. CHECK (runEffectOnSine (lynx::EffectKind::eq, [] (auto& s) { s.params[0] = 1.0f; }, 60.0, 0.05f) > 0.3f);
  3226. CHECK (runEffectOnSine (lynx::EffectKind::eq, [] (auto& s) { s.params[0] = 1.0f; }, 5000.0, 0.05f) < 0.06f);
  3227. // compressor: threshold -20 dB, ratio 20:1 flattens a 0 dBFS signal to near the threshold
  3228. const auto squashed = runEffectOnSine (lynx::EffectKind::compressor, [] (auto& s) {
  3229. s.params[0] = 40.0f / 60.0f;
  3230. s.params[1] = 1.0f;
  3231. }, 200.0, 1.0f);
  3232. CHECK (squashed < 0.2f && squashed > 0.05f);
  3233. // a signal below the threshold passes intact
  3234. CHECK (std::abs (runEffectOnSine (lynx::EffectKind::compressor, [] (auto& s) { s.params[0] = 1.0f; }, 200.0, 0.3f) - 0.3f) < 0.01f);
  3235. }
  3236. TEST (effects_delay_echoes_on_tempo_and_reverb_has_tail)
  3237. {
  3238. auto delay = lynx::makeEffect (lynx::EffectKind::delay);
  3239. delay->prepare (48000.0);
  3240. auto slot = lynx::makeEffectSlot (lynx::EffectKind::delay);
  3241. slot.params[0] = 3.5f / 8.0f; // index 3 = 1/4 = 0.5 s at 120 BPM
  3242. slot.params[1] = 0.0f; // no feedback: a single echo
  3243. slot.params[2] = 1.0f;
  3244. slot.params[4] = 0.0f; // no damping
  3245. std::vector<float> l (48000, 0.0f), r (48000, 0.0f);
  3246. l[0] = r[0] = 1.0f;
  3247. delay->process (l.data(), r.data(), 48000, slot.params, 120.0);
  3248. CHECK (std::abs (l[24000] - 1.0f) < 1e-4f);
  3249. CHECK (std::abs (l[12000]) < 1e-6f);
  3250. auto reverb = lynx::makeEffect (lynx::EffectKind::reverb);
  3251. reverb->prepare (48000.0);
  3252. auto rs = lynx::makeEffectSlot (lynx::EffectKind::reverb);
  3253. rs.params[3] = 1.0f;
  3254. std::fill (l.begin(), l.end(), 0.0f);
  3255. std::fill (r.begin(), r.end(), 0.0f);
  3256. l[0] = r[0] = 1.0f;
  3257. reverb->process (l.data(), r.data(), 48000, rs.params, 120.0);
  3258. float tail = 0.0f;
  3259. for (int i = 4800; i < 9600; ++i) // between 100 and 200 ms after the impulse
  3260. tail = std::max (tail, std::abs (l[(std::size_t) i]));
  3261. CHECK (tail > 1e-4f && tail < 1.0f);
  3262. CHECK (lynx::formatEffectParam (lynx::EffectKind::delay, 0, slot.params[0], 120.0) == "1/4 (500 ms)");
  3263. }
  3264. TEST (rack_runs_insert_effects_and_reuses_them)
  3265. {
  3266. // an insert with EQ at -inf doesn't exist; we use the EQ output at -18 dB to see the effect act
  3267. const auto quiet = renderMixerCase ([] (lynx::Song& s) {
  3268. auto eq = lynx::makeEffectSlot (lynx::EffectKind::eq);
  3269. eq.params[4] = 0.0f; // output -18 dB
  3270. s.mixer[1].effects.push_back (eq);
  3271. });
  3272. CHECK (quiet > 0.55f && quiet < 0.6f); // 0.5 from channel 2 + 0.5 * 0.126 from channel 1
  3273. const auto bypassed = renderMixerCase ([] (lynx::Song& s) {
  3274. auto eq = lynx::makeEffectSlot (lynx::EffectKind::eq);
  3275. eq.params[4] = 0.0f;
  3276. eq.enabled = false;
  3277. s.mixer[1].effects.push_back (eq);
  3278. });
  3279. CHECK (std::abs (bypassed - 1.0f) < 1e-4f);
  3280. auto s = lynx::Song::makeDefault();
  3281. s.mixer[2].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::reverb));
  3282. auto a = lynx::RackState::build (s, nullptr, 48000.0);
  3283. s.mixer[2].effects[0].params[0] = 0.9f; // turning a knob doesn't recreate the effect
  3284. auto b = lynx::RackState::build (s, a.get(), 48000.0);
  3285. CHECK (b->effects[2][0] == a->effects[2][0]);
  3286. s.mixer[2].effects[0] = lynx::makeEffectSlot (lynx::EffectKind::delay);
  3287. auto c = lynx::RackState::build (s, b.get(), 48000.0);
  3288. CHECK (c->effects[2][0] != b->effects[2][0]);
  3289. }
  3290. TEST (audio_clips_trigger_cut_and_resume)
  3291. {
  3292. lynx::Song s;
  3293. s.bpm = 120.0; // 1 beat = 0.5 s = 24000 samples at 48 kHz
  3294. lynx::ChannelDef c;
  3295. c.instrument = lynx::InstrumentKind::audioClip;
  3296. c.samplePath = "ramp";
  3297. c.volume = 1.0f;
  3298. s.channels.push_back (c);
  3299. s.patterns.emplace_back();
  3300. lynx::Clip clip;
  3301. clip.kind = lynx::ClipKind::audio;
  3302. clip.channel = 0;
  3303. clip.start = lynx::ppq; // starts at beat 2
  3304. clip.length = 2 * lynx::ppq; // lasts 2 beats
  3305. clip.offset = lynx::ppq; // skips the first beat of the audio
  3306. s.playlist.push_back (clip);
  3307. s.mode = lynx::PlayMode::song;
  3308. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  3309. lynx::EventBuffer ev;
  3310. lynx::scheduleSong (s, 0.0, 4 * 24000, tps, ev);
  3311. CHECK (ev.size() == 2);
  3312. CHECK (ev[0].type == lynx::NoteEvent::Type::on && ev[0].sampleOffset == 24000 && std::abs (ev[0].startSeconds - 0.5) < 1e-9);
  3313. CHECK (ev[1].type == lynx::NoteEvent::Type::off && ev[1].sampleOffset == 3 * 24000);
  3314. // seek to the middle of the clip (beat 2.5): with resume, starts at the block's first sample
  3315. ev.clear();
  3316. lynx::scheduleSong (s, 1.5 * lynx::ppq, 256, tps, ev, true);
  3317. CHECK (ev.size() == 1 && ev[0].sampleOffset == 0 && std::abs (ev[0].startSeconds - 0.75) < 1e-9);
  3318. ev.clear();
  3319. lynx::scheduleSong (s, 1.5 * lynx::ppq, 256, tps, ev, false);
  3320. CHECK (ev.size() == 0);
  3321. // sample whose value = position in seconds: lets us check where playback started
  3322. auto provider = [] (const std::string&) {
  3323. auto d = std::make_shared<lynx::SampleData>();
  3324. d->sampleRate = 48000.0;
  3325. for (int i = 0; i < 48000 * 3; ++i)
  3326. d->left.push_back ((float) i / 48000.0f);
  3327. return std::shared_ptr<const lynx::SampleData> (d);
  3328. };
  3329. auto state = lynx::RackState::build (s, nullptr, 48000.0, provider);
  3330. lynx::RackRenderer r;
  3331. r.prepare (48000.0, 512);
  3332. std::vector<float> l (256), rr (256);
  3333. // first block already playing mid-clip
  3334. r.process (state.get(), true, 1.5 * lynx::ppq, 256, l.data(), rr.data());
  3335. CHECK (std::abs (l[0] - 0.75f) < 0.001f);
  3336. // the clip ends at beat 3: after the short fade, silence (the sample would still have audio)
  3337. double tick = 3.0 * lynx::ppq - 10 * tps;
  3338. r.process (state.get(), false, tick, 256, l.data(), rr.data()); // stops (fade)
  3339. for (int i = 0; i < 4; ++i)
  3340. r.process (state.get(), false, tick, 256, l.data(), rr.data());
  3341. CHECK (std::abs (l[255]) < 1e-6f);
  3342. }
  3343. TEST (remove_channel_fixes_notes_and_audio_clips)
  3344. {
  3345. lynx::Song s;
  3346. s.channels.resize (3);
  3347. s.patterns.resize (1);
  3348. s.patterns[0].notes = { { 0, 240, 0, 60, 1.0f }, { 0, 240, 1, 60, 1.0f }, { 0, 240, 2, 60, 1.0f } };
  3349. lynx::Clip a;
  3350. a.kind = lynx::ClipKind::audio;
  3351. a.channel = 1;
  3352. a.length = 100;
  3353. lynx::Clip b = a;
  3354. b.channel = 2;
  3355. s.playlist = { a, b };
  3356. s.removeChannel (1);
  3357. CHECK (s.channels.size() == 2);
  3358. CHECK (s.patterns[0].notes.size() == 2 && s.patterns[0].notes[1].channel == 1);
  3359. CHECK (s.playlist.size() == 1 && s.playlist[0].channel == 1);
  3360. }
  3361. TEST (flp_import_audio_clips_with_offsets)
  3362. {
  3363. flp::Project p;
  3364. p.header.ppq = 96;
  3365. p.tempo = 120.0;
  3366. p.channels = { { 0, flp::ChannelType::audioClip, "Vocal", "", "%FLStudioData%\\v.wav", 2 },
  3367. { 1, flp::ChannelType::audioClip, "Loop", "", "%FLStudioData%\\l.wav", 3 } };
  3368. flp::Arrangement arr;
  3369. flp::PlaylistItem beats; // offset in beats: end - start = duration in beats
  3370. beats.kind = flp::PlaylistItem::Kind::channelClip;
  3371. beats.index = 0;
  3372. beats.position = 192;
  3373. beats.length = 384; // 4 beats
  3374. beats.startOffset = 2.0f;
  3375. beats.endOffset = 6.0f;
  3376. flp::PlaylistItem ms = beats; // offset in ms: 4 beats at 120 BPM = 2000 ms
  3377. ms.index = 1;
  3378. ms.startOffset = 500.0f;
  3379. ms.endOffset = 2500.0f;
  3380. arr.items = { beats, ms };
  3381. p.arrangements.push_back (arr);
  3382. lynx::FlpImportOptions options;
  3383. options.roots = { "C:\\FL", "C:\\FL\\Data", "" };
  3384. options.fileExists = [] (const std::string&) { return true; };
  3385. const auto s = lynx::importFlp (p, options);
  3386. CHECK (s.channels.size() == 2 && s.channels[0].instrument == lynx::InstrumentKind::audioClip);
  3387. CHECK (s.channels[0].samplePath == "C:\\FL\\Data\\v.wav");
  3388. CHECK (s.playlist.size() == 2);
  3389. CHECK (s.playlist[0].kind == lynx::ClipKind::audio && s.playlist[0].start == 2 * lynx::ppq);
  3390. CHECK (s.playlist[0].offset == 2 * lynx::ppq);
  3391. CHECK (s.playlist[1].offset == lynx::ppq); // 500 ms at 120 BPM = 1 beat
  3392. CHECK (s.mode == lynx::PlayMode::song);
  3393. }
  3394. TEST (plugin_factories_create_reuse_and_fallback)
  3395. {
  3396. lynx::Song s;
  3397. s.mixer = lynx::Song::defaultMixer (2);
  3398. lynx::ChannelDef c;
  3399. c.instrument = lynx::InstrumentKind::plugin;
  3400. c.plugin.id = "VST3-Synth-123";
  3401. c.mixerTrack = 1;
  3402. s.channels.push_back (c);
  3403. lynx::ChannelDef missing = c;
  3404. missing.plugin.id = "VST3-Ausente-0";
  3405. s.channels.push_back (missing);
  3406. lynx::EffectSlot fx;
  3407. fx.kind = lynx::EffectKind::plugin;
  3408. fx.plugin.id = "VST3-Reverb-9";
  3409. s.mixer[1].effects.push_back (fx);
  3410. s.patterns.emplace_back();
  3411. int instrumentsMade = 0, effectsMade = 0;
  3412. lynx::RackState::Factories f;
  3413. f.pluginInstruments = [&] (const lynx::PluginRef& ref, double) -> std::shared_ptr<lynx::Instrument> {
  3414. if (ref.id != "VST3-Synth-123")
  3415. return nullptr;
  3416. ++instrumentsMade;
  3417. auto synth = lynx::makeInstrument (lynx::InstrumentKind::synth);
  3418. synth->prepare (48000.0);
  3419. return synth;
  3420. };
  3421. f.pluginEffects = [&] (const lynx::PluginRef&, double) -> std::shared_ptr<lynx::Effect> {
  3422. ++effectsMade;
  3423. auto eq = lynx::makeEffect (lynx::EffectKind::eq);
  3424. eq->prepare (48000.0);
  3425. return eq;
  3426. };
  3427. auto a = lynx::RackState::build (s, nullptr, 48000.0, f);
  3428. CHECK (instrumentsMade == 1 && effectsMade == 1);
  3429. CHECK (a->instruments.size() == 2 && a->instruments[1] != nullptr); // missing: muted, but it exists
  3430. // changing the saved state doesn't recreate; swapping the plugin does
  3431. s.channels[0].plugin.state = "abc";
  3432. auto b = lynx::RackState::build (s, a.get(), 48000.0, f);
  3433. CHECK (instrumentsMade == 1 && b->instruments[0] == a->instruments[0] && b->effects[1][0] == a->effects[1][0]);
  3434. s.mixer[1].effects[0].plugin.id = "VST3-Outro-1";
  3435. auto c2 = lynx::RackState::build (s, b.get(), 48000.0, f);
  3436. CHECK (effectsMade == 2);
  3437. // rendering with a missing plugin and a plugin slot breaks nothing
  3438. lynx::RackRenderer r;
  3439. r.prepare (48000.0, 256);
  3440. lynx::LiveEventQueue live;
  3441. lynx::NoteEvent on;
  3442. on.channel = 0;
  3443. on.velocity = 1.0f;
  3444. live.push (on);
  3445. on.channel = 1;
  3446. live.push (on);
  3447. std::vector<float> l (256), rr (256);
  3448. r.process (c2.get(), false, 0.0, 256, l.data(), rr.data(), &live);
  3449. float peak = 0.0f;
  3450. for (auto v : l)
  3451. {
  3452. CHECK (std::isfinite (v));
  3453. peak = std::max (peak, std::abs (v));
  3454. }
  3455. CHECK (peak > 0.0f);
  3456. }
  3457. TEST (export_song_adds_silent_tail_and_never_restarts)
  3458. {
  3459. auto s = lynx::Song::makeDefault();
  3460. s.playlist.clear();
  3461. std::int64_t length = 0;
  3462. auto pat = lynx::makeExportSong (s, true, lynx::ppq * 8, length); // empty Playlist: uses the pattern
  3463. CHECK (length == lynx::ppq * 4);
  3464. CHECK (pat.mode == lynx::PlayMode::song && pat.playlist.size() == 2);
  3465. CHECK (pat.songLength() >= length + lynx::ppq * 8);
  3466. // no new notes after the song ends, only silence until the end of the tail
  3467. lynx::EventBuffer events;
  3468. const auto tps = lynx::ticksPerSample (pat.bpm, 48000.0);
  3469. int onsAfterEnd = 0, onsBefore = 0;
  3470. for (double tick = 0.0; tick < (double) (length + lynx::ppq * 8); tick += 512 * tps)
  3471. {
  3472. events.clear();
  3473. lynx::scheduleSong (pat, tick, 512, tps, events);
  3474. for (const auto& e : events)
  3475. if (e.type == lynx::NoteEvent::Type::on)
  3476. (tick + e.sampleOffset * tps >= (double) length ? onsAfterEnd : onsBefore)++;
  3477. }
  3478. CHECK (onsBefore == (int) s.patterns[0].notes.size());
  3479. CHECK (onsAfterEnd == 0);
  3480. // with Playlist: the end is the last clip's, without rounding
  3481. s.playlist.push_back ({ 0, 0, lynx::ppq * 2, lynx::ppq * 3, 0 });
  3482. auto song = lynx::makeExportSong (s, true, 0, length);
  3483. CHECK (length == lynx::ppq * 5 && song.playlist.size() == 1);
  3484. song = lynx::makeExportSong (s, false, 0, length);
  3485. CHECK (length == lynx::ppq * 4 && song.playlist[0].start == 0);
  3486. }
  3487. TEST (note_edit_selection_operations)
  3488. {
  3489. namespace ne = lynx::noteedit;
  3490. lynx::Pattern p;
  3491. p.notes = { { 0, 240, 0, 60 }, { 480, 240, 0, 64 }, { 960, 480, 1, 67 } };
  3492. // sorting keeps the indices in step
  3493. p.notes[0].start = 1200;
  3494. std::vector<int> tracked { 0, 2 };
  3495. ne::sortTracking (p, tracked);
  3496. CHECK (p.notes[2].start == 1200 && p.notes[(std::size_t) tracked[0]].start == 1200);
  3497. CHECK (p.notes[(std::size_t) tracked[1]].key == 67);
  3498. // group move respects the limits
  3499. std::int64_t dt = -5000;
  3500. int dk = 100;
  3501. ne::move (p, { 0, 1 }, dt, dk);
  3502. CHECK (dt == -480 && dk == 127 - 67 && p.notes[0].start == 0 && p.notes[1].key == 127);
  3503. // duplicate: right after the selection, rounded to the grid
  3504. lynx::Pattern q;
  3505. q.notes = { { 0, 240, 0, 60 }, { 240, 300, 0, 62 } }; // 540-tick selection
  3506. const auto added = ne::duplicate (q, { 0, 1 }, 960);
  3507. CHECK (added.size() == 2 && q.notes[(std::size_t) added[0]].start == 960 && q.notes[(std::size_t) added[1]].start == 1200);
  3508. // paste onto another channel with an offset
  3509. const auto pasted = ne::paste (q, ne::copy (q, { 1 }), 100, 3);
  3510. CHECK (pasted.size() == 1 && q.notes.back().channel == 3 && q.notes.back().start == 340);
  3511. // quantize rounds to the nearest
  3512. q.notes[0].start = 130;
  3513. q.notes[1].start = 100;
  3514. ne::quantize (q, { 0, 1 }, 240);
  3515. CHECK (q.notes[0].start == 240 && q.notes[1].start == 0);
  3516. // remove several (order and duplicates don't matter)
  3517. const auto before = q.notes.size();
  3518. ne::remove (q, { 1, 0, 1 });
  3519. CHECK (q.notes.size() == before - 2);
  3520. }
  3521. TEST (note_edit_quick_operations)
  3522. {
  3523. namespace ne = lynx::noteedit;
  3524. const std::int64_t step = lynx::ticksPerStep, bar = lynx::ppq * 4;
  3525. // split: only strictly inside; the tail is appended and does not slide again
  3526. lynx::Pattern p;
  3527. p.notes = { { 0, 4 * step, 0, 60 } };
  3528. p.notes[0].slide = true;
  3529. CHECK (ne::split (p, 0, 0) == -1 && ne::split (p, 0, 4 * step) == -1);
  3530. CHECK (ne::split (p, 0, step) == 1 && p.notes[0].length == step && p.notes[1].start == step && p.notes[1].length == 3 * step
  3531. && ! p.notes[1].slide);
  3532. // chop to grid: a 2.5-step note from half a step becomes 3 pieces cut on the step lines
  3533. lynx::Pattern c;
  3534. c.notes = { { step / 2, step * 5 / 2, 0, 60 }, { 0, step, 1, 50 } };
  3535. const auto pieces = ne::chop (c, { 0, 1 }, step);
  3536. CHECK (pieces.size() == 4 && c.notes.size() == 4);
  3537. CHECK (c.notes[0].length == step / 2 && c.notes[2].start == step && c.notes[2].length == step && c.notes[3].start == 2 * step
  3538. && c.notes[3].length == step);
  3539. // join: touching and overlapping notes of the same key and channel merge; others stay
  3540. lynx::Pattern j;
  3541. j.notes = { { 0, step, 0, 60 }, { step, step, 0, 60 }, { step * 3 / 2, step * 2, 0, 60 }, { 5 * step, step, 0, 60 },
  3542. { step, step, 0, 62 }, { step, step, 1, 60 } };
  3543. CHECK (ne::join (j, { 0, 1, 2, 3, 4, 5 }) == 2 && j.notes.size() == 4);
  3544. CHECK (j.notes[0].start == 0 && j.notes[0].length == step * 7 / 2);
  3545. // legato: each note reaches the next start (a chord ends together); the last keeps its length
  3546. lynx::Pattern l;
  3547. l.notes = { { 0, 10, 0, 60 }, { 0, 10, 0, 64 }, { step, 10, 0, 62 }, { 3 * step, 10, 0, 65 }, { step, 10, 1, 40 } };
  3548. ne::legato (l, { 0, 1, 2, 3, 4 });
  3549. CHECK (l.notes[0].length == step && l.notes[1].length == step && l.notes[2].length == 2 * step && l.notes[3].length == 10
  3550. && l.notes[4].length == 10);
  3551. // shrink to grid and full quantise (start and end)
  3552. ne::shrinkToGrid (l, { 3 }, step / 2);
  3553. CHECK (l.notes[3].length == step / 2);
  3554. lynx::Pattern q;
  3555. q.notes = { { 10, step - 30, 0, 60 }, { step + 100, 20, 0, 60 } };
  3556. ne::quantizeFull (q, { 0, 1 }, step);
  3557. CHECK (q.notes[0].start == 0 && q.notes[0].length == step && q.notes[1].start == step && q.notes[1].length == step);
  3558. // rotate inside the bar, wrapping
  3559. lynx::Pattern r;
  3560. r.notes = { { 0, step, 0, 60 }, { bar - step, step, 0, 62 } };
  3561. ne::rotate (r, { 0, 1 }, step, bar, 1);
  3562. CHECK (r.notes[0].start == step && r.notes[1].start == 0);
  3563. ne::rotate (r, { 0, 1 }, step, bar, -2);
  3564. CHECK (r.notes[0].start == bar - step && r.notes[1].start == bar - 2 * step);
  3565. r.notes.push_back ({ bar + step, step, 0, 64 }); // alone in the second bar: wraps inside that bar
  3566. ne::rotate (r, { 2 }, step, bar, -2);
  3567. CHECK (r.notes[2].start == 2 * bar - step);
  3568. }
  3569. TEST (note_edit_grid_and_time_text)
  3570. {
  3571. namespace ne = lynx::noteedit;
  3572. const std::int64_t step = lynx::ticksPerStep, bar = lynx::ppq * 4;
  3573. CHECK (ne::snapDown (250.0, step) == step && ne::snapDown (-1.0, step) == -step && ne::snapDown (17.9, 1) == 17);
  3574. CHECK (ne::snapNearest (1000.0, { 400, 1100, 980 }, 50.0, step) == 980);
  3575. CHECK (ne::snapNearest (1000.0, { 400 }, 50.0, step) == 960); // too far: falls back to the grid
  3576. // screen grid: 24 px per step shows steps; zoomed out it climbs to beats, bars and bar multiples
  3577. CHECK (ne::screenStep (24.0 / step, bar) == step);
  3578. CHECK (ne::screenStep (96.0 / bar, bar) == lynx::ppq);
  3579. CHECK (ne::screenStep (8.0 / bar, bar) == 2 * bar);
  3580. CHECK (ne::screenStep (200.0 / step, bar) == step / 4);
  3581. CHECK (ne::formatTime (0, 4) == "1:1:0" && ne::formatTime (bar + 5 * step + 7, 4) == "2:6:7");
  3582. CHECK (ne::formatTime (step * 2, 4, true) == "0:2:0");
  3583. std::int64_t t = 0;
  3584. CHECK (ne::parseTime ("2:6:7", 4, t) && t == bar + 5 * step + 7);
  3585. CHECK (ne::parseTime ("3", 4, t) && t == 2 * bar);
  3586. CHECK (ne::parseTime ("0:2:0", 4, t, true) && t == 2 * step);
  3587. CHECK (! ne::parseTime ("0:1:0", 4, t) && ! ne::parseTime ("1:17:0", 4, t) && ! ne::parseTime ("1:1:240", 4, t)
  3588. && ! ne::parseTime ("a", 4, t) && ! ne::parseTime ("", 4, t));
  3589. CHECK (ne::parseTime ("1:12:0", 3, t) && ! ne::parseTime ("1:13:0", 3, t)); // 3/4: 12 steps per bar
  3590. }
  3591. TEST (playlist_clip_edges_and_time_selection)
  3592. {
  3593. namespace ne = lynx::noteedit;
  3594. const std::int64_t beat = lynx::ppq;
  3595. // left-edge trim: content stays in place, cannot reveal what is before the content's start
  3596. lynx::Clip c { 0, 0, 4 * beat, 4 * beat, beat };
  3597. ne::trimStart (c, 5 * beat, beat);
  3598. CHECK (c.start == 5 * beat && c.offset == 2 * beat && c.length == 3 * beat);
  3599. ne::trimStart (c, 0, beat);
  3600. CHECK (c.start == 3 * beat && c.offset == 0 && c.length == 5 * beat);
  3601. ne::trimStart (c, 100 * beat, beat);
  3602. CHECK (c.length == beat && c.end() == 8 * beat);
  3603. // slip moves the content, not the clip
  3604. lynx::Clip s { 0, 0, 0, 2 * beat, beat };
  3605. ne::slip (s, beat / 2);
  3606. CHECK (s.offset == beat / 2 && s.start == 0 && s.length == 2 * beat);
  3607. ne::slip (s, beat);
  3608. CHECK (s.offset == 0);
  3609. ne::slip (s, -3 * beat);
  3610. CHECK (s.offset == 3 * beat);
  3611. // insert / delete time across all tracks, markers included
  3612. std::vector<lynx::Clip> clips { { 0, 0, 0, 2 * beat, 0 }, { 0, 1, 4 * beat, 2 * beat, 0 }, { 0, 2, beat, 6 * beat, 0 } };
  3613. std::vector<lynx::Marker> markers { { 5 * beat, "A" }, { beat / 2, "B" } };
  3614. ne::insertTime (clips, markers, 2 * beat, 4 * beat);
  3615. CHECK (clips[0].start == 0 && clips[1].start == 8 * beat && clips[2].start == beat && markers[0].tick == 9 * beat
  3616. && markers[1].tick == beat / 2);
  3617. std::vector<lynx::Clip> d { { 0, 0, 0, 2 * beat, 0 }, // before: kept
  3618. { 0, 1, 3 * beat, beat, 0 }, // inside: deleted
  3619. { 0, 2, beat, 6 * beat, 0 }, // spans the cut: head + tail
  3620. { 0, 3, 3 * beat, 4 * beat, 0 }, // tail outside: trimmed and moved
  3621. { 0, 4, 8 * beat, beat, 0 } }; // after: moved left
  3622. std::vector<lynx::Marker> dm { { 3 * beat, "in" }, { 9 * beat, "after" } };
  3623. ne::deleteTime (d, dm, 2 * beat, 5 * beat);
  3624. CHECK (d.size() == 5);
  3625. CHECK (d[1].track == 2 && d[1].start == beat && d[1].length == beat);
  3626. CHECK (d[2].track == 2 && d[2].start == 2 * beat && d[2].offset == 4 * beat && d[2].length == 2 * beat);
  3627. CHECK (d[3].track == 3 && d[3].start == 2 * beat && d[3].offset == 2 * beat && d[3].length == 2 * beat);
  3628. CHECK (d[4].start == 5 * beat && dm.size() == 1 && dm[0].tick == 6 * beat);
  3629. // time selection: overlapping clips; Ctrl+B repeats what starts inside it after itself
  3630. std::vector<lynx::Clip> r { { 0, 0, 0, beat, 0 }, { 0, 1, beat, 2 * beat, 0 }, { 0, 0, 4 * beat, beat, 0 } };
  3631. CHECK ((ne::clipsInTime (r, beat / 2, 2 * beat) == std::vector<int> { 0, 1 }));
  3632. const auto added = ne::repeatTime (r, 0, 4 * beat, {});
  3633. CHECK (added.size() == 2 && r[3].start == 4 * beat && r[4].start == 5 * beat && r[4].track == 1);
  3634. CHECK (ne::repeatTime (r, 0, 2 * beat, { 2 }).size() == 1 && r.back().start == 6 * beat);
  3635. }
  3636. TEST (recorder_builds_notes_in_pattern_and_song_mode)
  3637. {
  3638. auto song = lynx::Song::makeDefault();
  3639. const auto loop = song.patterns[0].effectiveLength (4); // 1 bar = 3840
  3640. lynx::NoteRecorder r;
  3641. r.add (song, { 100.0, 4, 60, 0.5f, true }); // no take: ignored
  3642. CHECK (r.takeFinished().empty());
  3643. r.begin (loop);
  3644. r.add (song, { 4000.0, 4, 60, 0.5f, true }); // second loop pass
  3645. r.add (song, { 4480.0, 4, 60, 0.0f, false });
  3646. r.add (song, { 5000.0, 4, 64, 0.9f, true }); // held until stop
  3647. r.end (song, 5240.0);
  3648. const auto notes = r.takeFinished();
  3649. CHECK (notes.size() == 2);
  3650. CHECK (notes[0].start == 4000 - 3840 && notes[0].length == 480 && notes[0].velocity == 0.5f && notes[0].channel == 4);
  3651. CHECK (notes[1].key == 64 && notes[1].start == 5000 - 3840 && notes[1].length == 240);
  3652. // song mode: position inside the current pattern's clip (with offset)
  3653. song.mode = lynx::PlayMode::song;
  3654. song.playlist = { { 0, 0, 7680, 3840, 960 } };
  3655. CHECK (lynx::patternPositionForTick (song, 7680.0 + 100.0, loop) == 1060);
  3656. CHECK (lynx::patternPositionForTick (song, 100.0, loop) == 100); // outside the clip: like loop
  3657. }
  3658. TEST (automation_curve_and_playback)
  3659. {
  3660. lynx::Automation a;
  3661. a.points = { { 0, 0.0f }, { 960, 1.0f }, { 1920, 0.5f, 0.0f } };
  3662. CHECK (a.valueAt (-10.0) == 0.0f && a.valueAt (480.0) == 0.5f && a.valueAt (5000.0) == 0.5f);
  3663. CHECK (a.length() == 1920);
  3664. a.points[0].tension = 1.0f; // curve: starts slow
  3665. CHECK (a.valueAt (480.0) < 0.2f && a.valueAt (480.0) > 0.0f);
  3666. // automated Master: silence in the first bar, sound in the second, and the last value
  3667. // keeps applying after the automation clip ends
  3668. auto song = lynx::Song::makeDefault();
  3669. song.bpm = 120.0;
  3670. song.mode = lynx::PlayMode::song;
  3671. const auto bar = (std::int64_t) lynx::ppq * 4;
  3672. for (int b = 0; b < 3; ++b)
  3673. song.playlist.push_back ({ 0, 0, bar * b, bar, 0 });
  3674. lynx::Automation master;
  3675. master.target = { lynx::AutomationTargetKind::mixerVolume, 0 };
  3676. master.points = { { 0, 0.0f }, { bar - 1, 0.0f }, { bar, 0.8f } };
  3677. song.automations.push_back (master);
  3678. lynx::Clip ac;
  3679. ac.kind = lynx::ClipKind::automation;
  3680. ac.automation = 0;
  3681. ac.track = 1;
  3682. ac.length = bar + bar / 2;
  3683. song.playlist.push_back (ac);
  3684. const double sr = 48000.0;
  3685. auto state = lynx::RackState::build (song, nullptr, sr);
  3686. CHECK (state->hasAutomation && state->mixerVolumeAuto[0] == 0 && state->automationClips[0].size() == 1);
  3687. lynx::RackRenderer r;
  3688. r.prepare (sr, 512);
  3689. std::vector<float> l (512), rr (512);
  3690. const auto tps = lynx::ticksPerSample (song.bpm, sr);
  3691. float peakBar1 = 0.0f, peakBar2 = 0.0f, peakBar3 = 0.0f;
  3692. for (double tick = 0.0; tick < (double) (bar * 3); tick += 512 * tps)
  3693. {
  3694. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  3695. auto& peak = tick < (double) bar - 512 * tps ? peakBar1 : tick < (double) bar * 2 ? peakBar2 : peakBar3;
  3696. for (auto v : l)
  3697. peak = std::max (peak, std::abs (v));
  3698. }
  3699. CHECK (peakBar1 < 1.0e-6f);
  3700. CHECK (peakBar2 > 0.05f);
  3701. CHECK (peakBar3 > 0.05f); // after the clip: keeps the last value
  3702. // target removed: automation disappears from the tables
  3703. song.removeChannel (0);
  3704. song.automations[0].target = { lynx::AutomationTargetKind::channelVolume, 0 };
  3705. song.removeChannel (0);
  3706. CHECK (song.automations[0].target.kind == lynx::AutomationTargetKind::none);
  3707. song.removeAutomation (0);
  3708. CHECK (song.automations.empty() && song.playlist.size() == 3);
  3709. }
  3710. TEST (mixer_sends_between_inserts)
  3711. {
  3712. // insert 1 goes only to 3; 3 goes to the Master: same sum (0.5 + 0.5)
  3713. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.setRoute (1, 3, true); s.setRoute (1, 0, false); }) - 1.0f) < 1e-4f);
  3714. // send at 50% and muted destination
  3715. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.setRoute (1, 3, true, 0.5f); s.setRoute (1, 0, false); }) - 0.75f) < 1e-4f);
  3716. 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);
  3717. // going to the Master and to 3: the signal doubles
  3718. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.setRoute (1, 3, true); }) - 1.5f) < 1e-4f);
  3719. // solo on 3 lets what reaches it sound (insert 1), but not 2
  3720. 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);
  3721. // solo on 1 lets the path to the Master (3) sound
  3722. 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);
  3723. // order and cycles
  3724. auto song = lynx::Song::makeDefault();
  3725. song.setRoute (4, 2, true);
  3726. song.setRoute (2, 1, true);
  3727. CHECK (! song.canRoute (1, 4) && ! song.canRoute (1, 2) && song.canRoute (1, 5));
  3728. song.setRoute (1, 4, true); // ignored: would create a cycle
  3729. CHECK (! song.routes (1, 4));
  3730. const auto order = song.mixerOrder();
  3731. const auto pos = [&] (int t) { return std::find (order.begin(), order.end(), t) - order.begin(); };
  3732. CHECK ((int) order.size() == (int) song.mixer.size() - 1);
  3733. CHECK (pos (4) < pos (2) && pos (2) < pos (1));
  3734. song.setRoute (4, 2, false);
  3735. CHECK (! song.routes (4, 2) && song.routes (4, 0));
  3736. }
  3737. namespace
  3738. {
  3739. // Test effect: delays the signal by `latency` samples and reports that latency (like a lookahead plugin).
  3740. class LatentEffect final : public lynx::Effect
  3741. {
  3742. public:
  3743. explicit LatentEffect (int samples) : latency (samples), line (samples) {}
  3744. void prepare (double) noexcept override {}
  3745. void reset() noexcept override {}
  3746. void process (float* l, float* r, int n, const lynx::EffectParams&, double) noexcept override { line.process (l, r, n); }
  3747. int latencySamples() const noexcept override { return latency; }
  3748. private:
  3749. int latency;
  3750. lynx::DelayLine line;
  3751. };
  3752. } // namespace
  3753. TEST (delay_line_mix_into_matches_process_and_drains_tail)
  3754. {
  3755. // mixInto = process over source*level, added to the destination; in stretches that cross the ring
  3756. lynx::DelayLine a (37), b (37);
  3757. std::vector<float> sl (300), sr (300), dl (300, 0.5f), dr (300, -0.5f), xl (300), xr (300);
  3758. for (int i = 0; i < 300; ++i)
  3759. {
  3760. sl[(std::size_t) i] = (float) std::sin (i * 0.1);
  3761. sr[(std::size_t) i] = (float) std::cos (i * 0.07);
  3762. }
  3763. for (int start = 0; start < 300; start += 100)
  3764. {
  3765. b.mixInto (sl.data() + start, sr.data() + start, 0.25f, dl.data() + start, dr.data() + start, 100);
  3766. for (int i = start; i < start + 100; ++i)
  3767. {
  3768. xl[(std::size_t) i] = sl[(std::size_t) i] * 0.25f;
  3769. xr[(std::size_t) i] = sr[(std::size_t) i] * 0.25f;
  3770. }
  3771. a.process (xl.data() + start, xr.data() + start, 100);
  3772. }
  3773. float worst = 0.0f;
  3774. for (int i = 0; i < 300; ++i)
  3775. worst = std::max ({ worst, std::abs (dl[(std::size_t) i] - 0.5f - xl[(std::size_t) i]),
  3776. std::abs (dr[(std::size_t) i] + 0.5f - xr[(std::size_t) i]) });
  3777. CHECK (worst < 1e-6f);
  3778. // silenced source: the tail still in the line comes out, then nothing remains
  3779. std::vector<float> tl (64, 0.0f), tr (64, 0.0f);
  3780. b.mixInto (nullptr, nullptr, 1.0f, tl.data(), tr.data(), 64);
  3781. CHECK (std::abs (tl[0] - sl[263] * 0.25f) < 1e-6f);
  3782. std::vector<float> zl (64, 0.0f), zr (64, 0.0f);
  3783. b.mixInto (nullptr, nullptr, 1.0f, zl.data(), zr.data(), 64);
  3784. CHECK (*std::max_element (zl.begin(), zl.end()) == 0.0f && *std::min_element (zl.begin(), zl.end()) == 0.0f);
  3785. }
  3786. TEST (limiter_lookahead_holds_ceiling_on_bursts)
  3787. {
  3788. // short bursts over a low floor: the sliding maximum must see each peak before it leaves
  3789. auto fx = lynx::makeEffect (lynx::EffectKind::limiter);
  3790. fx->prepare (48000.0);
  3791. auto slot = lynx::makeEffectSlot (lynx::EffectKind::limiter);
  3792. slot.params[2] = 0.0f; // 10 ms release
  3793. std::vector<float> l (48000), r (48000);
  3794. std::uint32_t seed = 1;
  3795. for (int i = 0; i < 48000; ++i)
  3796. {
  3797. seed = seed * 1664525u + 1013904223u;
  3798. const auto noise = ((float) (seed >> 8) / 16777216.0f - 0.5f) * 0.1f;
  3799. l[(std::size_t) i] = noise + ((i % 9973) < 3 ? 3.0f : 0.0f);
  3800. r[(std::size_t) i] = -noise;
  3801. }
  3802. for (int i = 0; i < 48000; i += 333)
  3803. fx->process (l.data() + i, r.data() + i, std::min (333, 48000 - i), slot.params, 120.0);
  3804. float peak = 0.0f, quiet = 0.0f;
  3805. for (int i = 0; i < 48000; ++i)
  3806. peak = std::max (peak, std::abs (l[(std::size_t) i]));
  3807. for (int i = 9973 * 2 + 2000; i < 9973 * 2 + 2400; ++i) // 40 ms after the burst the gain has already recovered
  3808. quiet = std::max (quiet, std::abs (r[(std::size_t) i]));
  3809. const auto ceiling = 1.0f; // the default ceiling is at 0 dBFS or below
  3810. CHECK (peak <= ceiling + 1e-5f);
  3811. CHECK (quiet > 0.03f);
  3812. }
  3813. TEST (pdc_aligns_paths_through_latent_effects)
  3814. {
  3815. // plan: channel 0 -> insert 1 (effect of 100) -> insert 3; channel 1 -> insert 2 -> Master
  3816. lynx::Song s;
  3817. s.mixer = lynx::Song::defaultMixer (4);
  3818. s.channels.resize (3);
  3819. s.channels[0].mixerTrack = 1;
  3820. s.channels[1].mixerTrack = 2;
  3821. s.channels[2].mixerTrack = 3;
  3822. s.mixer[1].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::eq));
  3823. s.setRoute (1, 3, true);
  3824. s.setRoute (1, 0, false);
  3825. auto plan = lynx::computePdc (s, { 0, 0, 30 }, { {}, { 100 } });
  3826. CHECK (plan.inAlign[1] == 0 && plan.outLat[1] == 100);
  3827. CHECK (plan.inAlign[3] == 100 && plan.channelDelay[2] == 70); // instrument of 30 on insert 3
  3828. CHECK (plan.sendDelay[1][1] == 0); // the slowest path isn't delayed
  3829. CHECK (plan.sendDelay[2][0] == 100 && plan.sendDelay[3][0] == 0 && plan.total == 100);
  3830. // bypass zeroes the slot's latency
  3831. s.mixer[1].effects[0].enabled = false;
  3832. CHECK (lynx::computePdc (s, { 0, 0, 30 }, { {}, { 100 } }).total == 30);
  3833. // rendered: two impulses played together come out together
  3834. lynx::Song r;
  3835. r.mixer = lynx::Song::defaultMixer (2);
  3836. for (int i = 0; i < 2; ++i)
  3837. {
  3838. lynx::ChannelDef c;
  3839. c.instrument = lynx::InstrumentKind::sampler;
  3840. c.samplePath = "impulso";
  3841. c.volume = 1.0f;
  3842. c.mixerTrack = i + 1;
  3843. r.channels.push_back (c);
  3844. }
  3845. r.patterns.emplace_back();
  3846. lynx::EffectSlot fx;
  3847. fx.kind = lynx::EffectKind::plugin;
  3848. fx.plugin.id = "latente";
  3849. r.mixer[1].effects.push_back (fx);
  3850. lynx::RackState::Factories f;
  3851. f.samples = [] (const std::string&) {
  3852. auto d = std::make_shared<lynx::SampleData>();
  3853. d->left.assign (64, 0.0f);
  3854. d->left[0] = 1.0f;
  3855. return std::shared_ptr<const lynx::SampleData> (d);
  3856. };
  3857. f.pluginEffects = [] (const lynx::PluginRef&, double) -> std::shared_ptr<lynx::Effect> { return std::make_shared<LatentEffect> (100); };
  3858. auto state = lynx::RackState::build (r, nullptr, 48000.0, f);
  3859. CHECK (state->pdc.total == 100 && state->sendDelays[2][0] != nullptr && state->sendDelays[2][0]->length() == 100);
  3860. lynx::RackRenderer renderer;
  3861. renderer.prepare (48000.0, 256);
  3862. lynx::LiveEventQueue live;
  3863. for (int ch = 0; ch < 2; ++ch)
  3864. {
  3865. lynx::NoteEvent on;
  3866. on.channel = ch;
  3867. on.key = 60;
  3868. on.velocity = 1.0f;
  3869. live.push (on);
  3870. }
  3871. std::vector<float> l (256), rr (256);
  3872. renderer.process (state.get(), false, 0.0, 256, l.data(), rr.data(), &live);
  3873. int first = -1;
  3874. for (int i = 0; i < 256 && first < 0; ++i)
  3875. if (std::abs (l[(std::size_t) i]) > 0.01f)
  3876. first = i;
  3877. CHECK (first == 100 && std::abs (l[100] - 2.0f) < 1e-3f); // both impulses arrive together, summed
  3878. // editing without changing latencies reuses the lines
  3879. r.channels[0].volume = 0.5f;
  3880. auto next = lynx::RackState::build (r, state.get(), 48000.0, f);
  3881. CHECK (next->sendDelays[2][0] == state->sendDelays[2][0]);
  3882. }
  3883. TEST (mixer_mute_fades_instead_of_clicking)
  3884. {
  3885. lynx::Song s;
  3886. s.mixer = lynx::Song::defaultMixer (2);
  3887. lynx::ChannelDef c;
  3888. c.instrument = lynx::InstrumentKind::sampler;
  3889. c.samplePath = "dc";
  3890. c.volume = 1.0f;
  3891. c.mixerTrack = 1;
  3892. s.channels.push_back (c);
  3893. s.patterns.emplace_back();
  3894. auto provider = [] (const std::string&) {
  3895. auto d = std::make_shared<lynx::SampleData>();
  3896. d->left.assign (48000, 0.5f);
  3897. return std::shared_ptr<const lynx::SampleData> (d);
  3898. };
  3899. auto on = lynx::RackState::build (s, nullptr, 48000.0, provider);
  3900. s.mixer[1].muted = true;
  3901. auto off = lynx::RackState::build (s, on.get(), 48000.0, provider);
  3902. lynx::RackRenderer r;
  3903. r.prepare (48000.0, 256);
  3904. lynx::LiveEventQueue live;
  3905. lynx::NoteEvent note;
  3906. note.velocity = 1.0f;
  3907. live.push (note);
  3908. std::vector<float> l (256), rr (256);
  3909. r.process (on.get(), false, 0.0, 256, l.data(), rr.data(), &live);
  3910. CHECK (std::abs (l[255] - 0.5f) < 1e-3f);
  3911. r.process (off.get(), false, 0.0, 256, l.data(), rr.data());
  3912. CHECK (l[0] > 0.45f && l[128] > 0.2f && l[128] < 0.3f && std::abs (l[255]) < 1e-6f); // ramp down to zero
  3913. r.process (off.get(), false, 0.0, 256, l.data(), rr.data());
  3914. CHECK (std::abs (l[0]) < 1e-9f);
  3915. }
  3916. TEST (dither_quantizes_to_lsb_grid_with_small_error)
  3917. {
  3918. lynx::Dither d (16);
  3919. std::vector<float> x (4800);
  3920. for (std::size_t i = 0; i < x.size(); ++i)
  3921. x[i] = 0.25f * std::sin ((float) i * 0.05f);
  3922. auto y = x;
  3923. d.process (y.data(), (int) y.size(), 0);
  3924. double errorSum = 0.0;
  3925. bool onGrid = true;
  3926. for (std::size_t i = 0; i < x.size(); ++i)
  3927. {
  3928. const auto steps = y[i] * 32768.0f;
  3929. onGrid = onGrid && std::abs (steps - std::round (steps)) < 1e-3f;
  3930. errorSum += std::abs (y[i] - x[i]);
  3931. }
  3932. CHECK (onGrid);
  3933. CHECK (errorSum / (double) x.size() < 4.0 / 32768.0); // a few LSBs of noise on average
  3934. }
  3935. TEST (parallel_rendering_matches_sequential)
  3936. {
  3937. // 32 channels (worth parallelizing), inserts with chained sends
  3938. auto song = lynx::Song::makeDefault();
  3939. song.mixer = lynx::Song::defaultMixer (16);
  3940. for (int i = 5; i < 32; ++i)
  3941. {
  3942. lynx::ChannelDef c;
  3943. c.instrument = i % 2 ? lynx::InstrumentKind::synth : lynx::InstrumentKind::hat;
  3944. c.mixerTrack = 1 + i % 12;
  3945. c.volume = 0.3f;
  3946. song.channels.push_back (c);
  3947. for (int step = i % 4; step < 16; step += 3)
  3948. song.patterns[0].notes.push_back ({ (std::int64_t) step * lynx::ticksPerStep, 200, i, 48 + i, 0.7f });
  3949. }
  3950. song.patterns[0].sortNotes();
  3951. song.setRoute (3, 7, true, 0.6f);
  3952. song.setRoute (7, 9, true, 0.5f);
  3953. song.setRoute (2, 9, true);
  3954. song.mixer[9].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::reverb));
  3955. auto seqState = lynx::RackState::build (song, nullptr, 48000.0);
  3956. auto parState = lynx::RackState::build (song, nullptr, 48000.0);
  3957. CHECK (parState->parallel && parState->mixerLayers.size() == 3);
  3958. lynx::WorkerPool pool (3);
  3959. lynx::RackRenderer seq, par;
  3960. seq.prepare (48000.0, 512);
  3961. par.prepare (48000.0, 512);
  3962. par.setWorkerPool (&pool);
  3963. std::vector<float> l1 (512), r1 (512), l2 (512), r2 (512);
  3964. const auto tps = lynx::ticksPerSample (song.bpm, 48000.0);
  3965. bool same = true;
  3966. float peak = 0.0f;
  3967. for (int block = 0; block < 200; ++block)
  3968. {
  3969. seq.process (seqState.get(), true, block * 512 * tps, 512, l1.data(), r1.data());
  3970. par.process (parState.get(), true, block * 512 * tps, 512, l2.data(), r2.data());
  3971. same = same && l1 == l2 && r1 == r2;
  3972. for (auto v : l1)
  3973. peak = std::max (peak, std::abs (v));
  3974. }
  3975. CHECK (same);
  3976. CHECK (peak > 0.05f);
  3977. // the pool handles many small rounds in a row
  3978. std::atomic<int> sum { 0 };
  3979. auto add = [&sum] (int i) { sum.fetch_add (i + 1); };
  3980. for (int round = 0; round < 2000; ++round)
  3981. pool.parallelFor (5, add);
  3982. CHECK (sum.load() == 2000 * 15);
  3983. }
  3984. TEST (arrangements_switch_and_keep_clip_indices)
  3985. {
  3986. auto s = lynx::Song::makeDefault();
  3987. s.patterns.emplace_back().name = "B";
  3988. s.playlist = { { 1, 0, 0, 960, 0 } };
  3989. s.loopEnd = 960;
  3990. CHECK (s.arrangements.size() == 1 && s.currentArrangement == 0);
  3991. const auto second = s.addArrangement ("Versão curta", true);
  3992. CHECK (second == 1 && s.currentArrangement == 1 && s.playlist.size() == 1 && s.loopEnd == 960);
  3993. s.playlist.push_back ({ 0, 1, 960, 960, 0 });
  3994. s.loopEnd = 0;
  3995. s.selectArrangement (0);
  3996. CHECK (s.playlist.size() == 1 && s.loopEnd == 960 && s.arrangements[1].clips.size() == 2);
  3997. // removing pattern 0 fixes the clips stored in the other arrangement
  3998. s.removePattern (0);
  3999. CHECK (s.playlist.size() == 1 && s.playlist[0].pattern == 0);
  4000. CHECK (s.arrangements[1].clips.size() == 1 && s.arrangements[1].clips[0].pattern == 0);
  4001. s.removeArrangement (0);
  4002. CHECK (s.arrangements.size() == 1 && s.currentArrangement == 0 && s.playlist.size() == 1 && s.loopEnd == 0);
  4003. }
  4004. namespace
  4005. {
  4006. // Test effect that inverts the signal and counts how many times it processed.
  4007. class CountingInverter final : public lynx::Effect
  4008. {
  4009. public:
  4010. void prepare (double) noexcept override {}
  4011. void reset() noexcept override {}
  4012. void process (float* l, float* r, int n, const lynx::EffectParams&, double) noexcept override
  4013. {
  4014. ++calls;
  4015. for (int i = 0; i < n; ++i)
  4016. {
  4017. l[i] = -l[i];
  4018. r[i] = -r[i];
  4019. }
  4020. }
  4021. int calls = 0;
  4022. };
  4023. } // namespace
  4024. TEST (keyboard_controller_outputs_key_value)
  4025. {
  4026. std::vector<std::uint8_t> blob (554, 0);
  4027. const std::uint32_t version = 3;
  4028. std::memcpy (blob.data(), &version, 4);
  4029. const float key60 = 0.7f, off = 0.2f;
  4030. std::memcpy (blob.data() + 12 + 60 * 4, &key60, 4);
  4031. std::memcpy (blob.data() + 548, &off, 4);
  4032. blob[552] = 1; // auto release
  4033. lynx::ChannelDef c;
  4034. c.instrument = lynx::InstrumentKind::controller;
  4035. c.controllerState = blob;
  4036. auto inst = lynx::makeInstrument (lynx::InstrumentKind::controller);
  4037. inst->prepare (48000.0);
  4038. inst->configure (c, 120.0);
  4039. std::vector<float> l (64), r (64);
  4040. inst->noteOn (60, 1.0f);
  4041. inst->render (l.data(), r.data(), 64, 1.0f, 1.0f);
  4042. CHECK (std::abs (inst->controlOutput (0) - 0.7f) < 1e-6f);
  4043. inst->noteOff (60);
  4044. CHECK (std::abs (inst->controlOutput (0) - 0.2f) < 1e-6f);
  4045. }
  4046. TEST (envelope_controller_ducks_on_each_note)
  4047. {
  4048. // a unipolar articulator like "Basic Sidechain": base 0.05, drops to 0 on the note and rises to 1 in 0.3 s
  4049. lynx::ControllerArticulator a;
  4050. a.envOn = true;
  4051. a.base = 0.05f;
  4052. a.envLevel = 1.0f;
  4053. a.count = 3;
  4054. a.time = { 0.0f, 0.2f, 0.3f };
  4055. a.level = { 0.0f, 0.75f, 1.0f };
  4056. CHECK (std::abs (lynx::controllerValue (a, 0.0, -1.0) - 0.05f) < 1e-4f);
  4057. CHECK (std::abs (lynx::controllerValue (a, 0.1, -1.0) - 0.425f) < 1e-3f);
  4058. CHECK (lynx::controllerValue (a, 1.0, -1.0) > 0.99f); // no sustain: runs to the end and stays
  4059. a.sustain = 1;
  4060. CHECK (std::abs (lynx::controllerValue (a, 1.0, -1.0) - 0.8f) < 1e-3f); // held at sustain
  4061. CHECK (lynx::controllerValue (a, 1.0, 0.5) > 0.99f); // released: continues to the end
  4062. a.bipolar = true;
  4063. a.base = 0.5f;
  4064. a.sustain = -1;
  4065. CHECK (std::abs (lynx::controllerValue (a, 0.0, -1.0) - 0.0f) < 1e-4f); // bipolar: 0.5 + (0 − 0.5)
  4066. }
  4067. TEST (peak_controller_drives_live_automation)
  4068. {
  4069. // track 1: source (DC 0.5) with Peak Controller; track 2: DC 0.5 with volume linked to the controller
  4070. auto render = [] (bool linked) {
  4071. lynx::Song s;
  4072. s.mixer = lynx::Song::defaultMixer (2);
  4073. for (int t = 1; t <= 2; ++t)
  4074. {
  4075. lynx::ChannelDef c;
  4076. c.instrument = lynx::InstrumentKind::sampler;
  4077. c.samplePath = "dc";
  4078. c.volume = 1.0f;
  4079. c.mixerTrack = t;
  4080. s.channels.push_back (c);
  4081. }
  4082. s.patterns.emplace_back();
  4083. auto peak = lynx::makeEffectSlot (lynx::EffectKind::peakController);
  4084. peak.params[0] = 1.0f; // base 1
  4085. peak.params[1] = 0.0f; // amount −1: 1 − peak
  4086. peak.params[2] = 0.5f; // no tension
  4087. s.mixer[1].effects.push_back (peak);
  4088. if (linked)
  4089. {
  4090. lynx::Automation a;
  4091. a.name = "Peak";
  4092. a.target = { lynx::AutomationTargetKind::mixerVolume, 2 };
  4093. a.source = { 1, 0, 0 };
  4094. s.automations.push_back (a);
  4095. }
  4096. lynx::RackState::Factories f;
  4097. f.samples = [] (const std::string&) {
  4098. auto d = std::make_shared<lynx::SampleData>();
  4099. d->left.assign (48000, 0.5f);
  4100. return std::shared_ptr<const lynx::SampleData> (d);
  4101. };
  4102. auto state = lynx::RackState::build (s, nullptr, 48000.0, f);
  4103. lynx::RackRenderer r;
  4104. r.prepare (48000.0, 512);
  4105. lynx::LiveEventQueue live;
  4106. for (int ch = 0; ch < 2; ++ch)
  4107. {
  4108. lynx::NoteEvent on;
  4109. on.channel = ch;
  4110. on.velocity = 1.0f;
  4111. live.push (on);
  4112. }
  4113. std::vector<float> l (4800), rr (4800);
  4114. r.process (state.get(), false, 0.0, 4800, l.data(), rr.data(), &live);
  4115. return l[4000];
  4116. };
  4117. const auto free = render (false), ducked = render (true);
  4118. CHECK (std::isfinite (ducked) && ducked > 0.0f);
  4119. CHECK (ducked < free - 0.05f); // the 0.5 peak on track 1 lowers track 2's volume
  4120. }
  4121. TEST (effect_slot_dry_wet_and_smart_disable)
  4122. {
  4123. lynx::Song s;
  4124. s.mixer = lynx::Song::defaultMixer (1);
  4125. lynx::ChannelDef c;
  4126. c.instrument = lynx::InstrumentKind::sampler;
  4127. c.samplePath = "dc";
  4128. c.volume = 1.0f;
  4129. c.mixerTrack = 1;
  4130. s.channels.push_back (c);
  4131. s.patterns.emplace_back();
  4132. lynx::EffectSlot fx;
  4133. fx.kind = lynx::EffectKind::plugin;
  4134. fx.plugin.id = "inversor";
  4135. fx.mix = 0.25f; // 75% dry (+0.5) and 25% wet (-0.5) = +0.25
  4136. s.mixer[1].effects.push_back (fx);
  4137. auto inverter = std::make_shared<CountingInverter>();
  4138. lynx::RackState::Factories f;
  4139. f.samples = [] (const std::string&) {
  4140. auto d = std::make_shared<lynx::SampleData>();
  4141. d->left.assign (4800, 0.5f);
  4142. return std::shared_ptr<const lynx::SampleData> (d);
  4143. };
  4144. f.pluginEffects = [inverter] (const lynx::PluginRef&, double) -> std::shared_ptr<lynx::Effect> { return inverter; };
  4145. auto state = lynx::RackState::build (s, nullptr, 48000.0, f);
  4146. lynx::RackRenderer r;
  4147. r.prepare (48000.0, 512);
  4148. lynx::LiveEventQueue live;
  4149. lynx::NoteEvent on;
  4150. on.velocity = 1.0f;
  4151. live.push (on);
  4152. std::vector<float> l (480), rr (480);
  4153. r.process (state.get(), false, 0.0, 480, l.data(), rr.data(), &live);
  4154. CHECK (std::abs (l[100] - 0.25f) < 1e-4f);
  4155. // after the sample ends: 4 s of silence and the plugin stops being called
  4156. for (int i = 0; i < 100 * 6; ++i) // 6 s
  4157. r.process (state.get(), false, 0.0, 480, l.data(), rr.data());
  4158. const auto callsAfterSilence = inverter->calls;
  4159. for (int i = 0; i < 100; ++i)
  4160. r.process (state.get(), false, 0.0, 480, l.data(), rr.data());
  4161. CHECK (inverter->calls == callsAfterSilence);
  4162. CHECK (callsAfterSilence < 100 * 6);
  4163. // sound again: processing resumes in the same block
  4164. live.push (on);
  4165. r.process (state.get(), false, 0.0, 480, l.data(), rr.data(), &live);
  4166. CHECK (inverter->calls == callsAfterSilence + 1 && std::abs (l[100] - 0.25f) < 1e-4f);
  4167. }
  4168. TEST (swing_delays_offbeat_sixteenths)
  4169. {
  4170. CHECK (lynx::swingDelay (0, 1.0f) == 0);
  4171. CHECK (lynx::swingDelay (lynx::ticksPerStep, 1.0f) == lynx::ticksPerStep / 2);
  4172. CHECK (lynx::swingDelay (lynx::ticksPerStep * 2, 1.0f) == 0);
  4173. CHECK (lynx::swingDelay (lynx::ticksPerStep + 10, 1.0f) == (lynx::ticksPerStep - 20) / 2); // off the grid: triangular weight (FL)
  4174. CHECK (lynx::swingDelay (lynx::ticksPerStep, 1.0f, 0.5f) == lynx::ticksPerStep / 4); // channel mix
  4175. CHECK (lynx::swingDelay (lynx::ticksPerStep * 3, 0.5f) == lynx::ticksPerStep / 4);
  4176. lynx::Pattern p;
  4177. p.notes = { { 0, 100, 0, 60 }, { lynx::ticksPerStep, 100, 0, 62 } };
  4178. lynx::EventBuffer ev;
  4179. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  4180. lynx::schedulePatternLoop (p, p.effectiveLength (4), 0.0, 12000, tps, ev, 1.0f);
  4181. int onOffset = -1;
  4182. for (auto& e : ev)
  4183. if (e.type == lynx::NoteEvent::Type::on && e.key == 62)
  4184. onOffset = e.sampleOffset;
  4185. CHECK (onOffset == (int) std::floor ((lynx::ticksPerStep * 1.5) / tps));
  4186. }
  4187. TEST (sampler_settings_pitch_reverse_envelope_cut)
  4188. {
  4189. auto ramp = std::make_shared<lynx::SampleData>();
  4190. for (int i = 0; i < 48000; ++i)
  4191. ramp->left.push_back ((float) i / 48000.0f); // ramp: the value tells where the read position is
  4192. ramp->sampleRate = 48000.0;
  4193. auto play = [&] (const lynx::SamplerSettings& s, int key, int frames, bool releaseAfterHalf) {
  4194. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  4195. inst->prepare (48000.0);
  4196. lynx::ChannelDef c;
  4197. c.sampler = s;
  4198. inst->configure (c, 120.0);
  4199. inst->noteOn (key, 1.0f);
  4200. std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
  4201. inst->render (l.data(), r.data(), frames / 2, 1.0f, 1.0f);
  4202. if (releaseAfterHalf)
  4203. inst->noteOff (key);
  4204. inst->render (l.data() + frames / 2, r.data() + frames / 2, frames - frames / 2, 1.0f, 1.0f);
  4205. return l;
  4206. };
  4207. // +12 semitones: reads twice as fast
  4208. lynx::SamplerSettings up;
  4209. up.pitch = 12.0f;
  4210. CHECK (std::abs (play (up, 60, 1000, false)[999] - 2.0f * 999.0f / 48000.0f) < 1e-3f);
  4211. // reversed: starts from the end
  4212. lynx::SamplerSettings back;
  4213. back.reverse = true;
  4214. CHECK (play (back, 60, 100, false)[10] > 0.99f);
  4215. // 10 ms attack: starts silent and reaches full level
  4216. lynx::SamplerSettings slow;
  4217. slow.attack = 0.01f;
  4218. const auto a = play (slow, 60, 2000, false);
  4219. CHECK (a[10] < 0.2f * (10.0f / 48000.0f) + 1e-6f && std::abs (a[1500] - 1500.0f / 48000.0f) < 1e-4f);
  4220. // 5 ms release: gone right after release; without release it plays in full
  4221. lynx::SamplerSettings rel;
  4222. rel.release = 0.005f;
  4223. CHECK (play (rel, 60, 2000, true)[1999] == 0.0f);
  4224. CHECK (play (lynx::SamplerSettings {}, 60, 2000, true)[1999] > 0.0f);
  4225. // cut itself: the second note cuts the first (one voice remains)
  4226. lynx::SamplerSettings cut;
  4227. cut.cut = true;
  4228. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  4229. inst->prepare (48000.0);
  4230. lynx::ChannelDef cutChannel;
  4231. cutChannel.sampler = cut;
  4232. inst->configure (cutChannel, 120.0);
  4233. inst->noteOn (60, 1.0f);
  4234. std::vector<float> l (2000), r (2000);
  4235. inst->render (l.data(), r.data(), 1000, 1.0f, 1.0f);
  4236. inst->noteOn (60, 1.0f);
  4237. std::fill (l.begin(), l.end(), 0.0f);
  4238. inst->render (l.data(), r.data(), 2000, 1.0f, 1.0f);
  4239. CHECK (std::abs (l[1999] - 1999.0f / 48000.0f) < 1e-4f); // only the new note
  4240. }
  4241. TEST (playlist_split_and_track_mute)
  4242. {
  4243. lynx::Clip c { 0, 3, 960, 3840, 480 };
  4244. const auto rest = lynx::splitClip (c, 2000);
  4245. CHECK (c.start == 960 && c.length == 1040 && c.offset == 480);
  4246. CHECK (rest.start == 2000 && rest.length == 2800 && rest.offset == 480 + 1040 && rest.track == 3);
  4247. auto s = lynx::Song::makeDefault();
  4248. s.mode = lynx::PlayMode::song;
  4249. s.playlist = { { 0, 0, 0, 3840, 0 } };
  4250. lynx::EventBuffer ev;
  4251. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  4252. lynx::scheduleSong (s, 0.0, 48000, tps, ev);
  4253. CHECK (ev.size() > 0);
  4254. s.playlistTracks.resize (1);
  4255. s.playlistTracks[0].muted = true;
  4256. ev.clear();
  4257. lynx::scheduleSong (s, 0.0, 48000, tps, ev);
  4258. CHECK (ev.size() == 0);
  4259. }
  4260. TEST (note_pan_pitch_and_mute)
  4261. {
  4262. lynx::Pattern p;
  4263. lynx::Note a { 0, 240, 0, 60 };
  4264. a.pan = -1.0f;
  4265. a.finePitch = 50.0f;
  4266. lynx::Note b { 240, 240, 0, 62 };
  4267. b.muted = true;
  4268. p.notes = { a, b };
  4269. lynx::EventBuffer ev;
  4270. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  4271. lynx::schedulePatternLoop (p, p.effectiveLength (4), 0.0, 48000, tps, ev);
  4272. int ons = 0;
  4273. for (auto& e : ev)
  4274. if (e.type == lynx::NoteEvent::Type::on)
  4275. {
  4276. ++ons;
  4277. CHECK (e.key == 60 && e.pan == -1.0f && e.cents == 50.0f);
  4278. }
  4279. CHECK (ons == 1); // the muted one doesn't play
  4280. // note pan in the sampler: fully left
  4281. auto dc = std::make_shared<lynx::SampleData>();
  4282. dc->left.assign (4800, 0.5f);
  4283. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, dc);
  4284. inst->prepare (48000.0);
  4285. inst->setNextNoteExpression (-1.0f, 0.0f);
  4286. inst->noteOn (60, 1.0f);
  4287. std::vector<float> l (100), r (100);
  4288. inst->render (l.data(), r.data(), 100, 1.0f, 1.0f);
  4289. CHECK (l[50] > 0.6f && std::abs (r[50]) < 1e-6f);
  4290. }
  4291. TEST (tempo_automation_changes_speed_and_duration)
  4292. {
  4293. auto song = lynx::Song::makeDefault();
  4294. song.bpm = 120.0;
  4295. song.mode = lynx::PlayMode::song;
  4296. const auto bar = (std::int64_t) lynx::ppq * 4;
  4297. song.playlist = { { 0, 0, 0, bar, 0 }, { 0, 0, bar, bar, 0 } };
  4298. lynx::Automation tempo;
  4299. tempo.target = { lynx::AutomationTargetKind::tempo, 0 };
  4300. const auto v120 = lynx::valueFromTempo (120.0), v240 = lynx::valueFromTempo (240.0);
  4301. tempo.points = { { 0, v120 }, { bar - 1, v120 }, { bar, v240 } }; // step: 120 -> 240 at bar 2
  4302. song.automations.push_back (tempo);
  4303. lynx::Clip clip;
  4304. clip.kind = lynx::ClipKind::automation;
  4305. clip.automation = 0;
  4306. clip.track = 1;
  4307. clip.length = bar * 2;
  4308. song.playlist.push_back (clip);
  4309. CHECK (std::abs (song.tempoAt (100.0) - 120.0) < 0.01 && std::abs (song.tempoAt ((double) bar + 100.0) - 240.0) < 0.01);
  4310. // 1 bar at 120 (2 s) + 1 at 240 (1 s)
  4311. CHECK (std::abs (song.secondsForTicks (bar * 2) - 3.0) < 0.01);
  4312. auto state = lynx::RackState::build (song, nullptr, 48000.0);
  4313. CHECK (state->tempoAutomation == 0);
  4314. lynx::RackRenderer r;
  4315. r.prepare (48000.0, 512);
  4316. std::vector<float> l (512), rr (512);
  4317. r.process (state.get(), true, 10.0, 512, l.data(), rr.data());
  4318. CHECK (std::abs (r.currentBpm() - 120.0) < 0.01);
  4319. r.process (state.get(), true, (double) bar + 10.0, 512, l.data(), rr.data());
  4320. CHECK (std::abs (r.currentBpm() - 240.0) < 0.01);
  4321. }
  4322. TEST (bounce_insert_song_and_apply)
  4323. {
  4324. auto s = lynx::Song::makeDefault();
  4325. s.setRoute (3, 7, true, 0.5f);
  4326. s.mixer[0].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::reverb));
  4327. s.mixer[2].solo = true;
  4328. const auto b = lynx::makeBounceSong (s, 3);
  4329. CHECK (b.mixer[3].solo && ! b.mixer[2].solo && b.mixer[3].toMaster && b.mixer[3].sends.empty());
  4330. CHECK (b.mixer[0].effects.empty() && b.mode == lynx::PlayMode::song);
  4331. const auto audible = b.soloAudible();
  4332. CHECK (audible[3] && ! audible[1] && ! audible[7]);
  4333. const auto channels = s.channels.size();
  4334. const auto inserts = s.mixer.size();
  4335. const auto created = lynx::applyBounce (s, 3, "C:/x.wav", 3840);
  4336. CHECK (created == (int) inserts && s.mixer.size() == inserts + 1 && s.mixer[3].muted);
  4337. CHECK (s.mixer[(std::size_t) created].sends.size() == 1 && s.mixer[(std::size_t) created].sends[0].target == 7);
  4338. CHECK (s.channels.size() == channels + 1 && s.channels.back().mixerTrack == created);
  4339. CHECK (s.playlist.back().kind == lynx::ClipKind::audio && s.playlist.back().length == 3840);
  4340. }
  4341. TEST (mixer_stereo_separation_phase_swap)
  4342. {
  4343. lynx::MixerTrack t;
  4344. std::vector<float> l { 1.0f }, r { 0.0f };
  4345. t.stereo = 1.0f; // mono
  4346. lynx::RackRenderer::applyStereoForTest (t, l.data(), r.data(), 1);
  4347. CHECK (std::abs (l[0] - 0.5f) < 1e-6f && std::abs (r[0] - 0.5f) < 1e-6f);
  4348. l = { 1.0f };
  4349. r = { 0.0f };
  4350. t.stereo = -1.0f; // doubled side: 1.5 L - 0.5 R
  4351. lynx::RackRenderer::applyStereoForTest (t, l.data(), r.data(), 1);
  4352. CHECK (std::abs (l[0] - 1.5f) < 1e-6f && std::abs (r[0] + 0.5f) < 1e-6f);
  4353. l = { 1.0f };
  4354. r = { 0.25f };
  4355. t.stereo = 0.0f;
  4356. t.swapChannels = true;
  4357. t.invertPhase = true;
  4358. lynx::RackRenderer::applyStereoForTest (t, l.data(), r.data(), 1);
  4359. CHECK (l[0] == -0.25f && r[0] == -1.0f);
  4360. }
  4361. TEST (effects_limiter_filter_distortion_chorus)
  4362. {
  4363. const double sr = 48000.0;
  4364. auto sine = [] (float hz, float amp, int n) {
  4365. std::vector<float> v ((std::size_t) n);
  4366. for (int i = 0; i < n; ++i)
  4367. v[(std::size_t) i] = amp * std::sin (2.0f * 3.14159265f * hz * (float) i / 48000.0f);
  4368. return v;
  4369. };
  4370. auto peak = [] (const std::vector<float>& v, int from) {
  4371. float p = 0.0f;
  4372. for (std::size_t i = (std::size_t) from; i < v.size(); ++i)
  4373. p = std::max (p, std::abs (v[i]));
  4374. return p;
  4375. };
  4376. // limiter: +12 dB on a 0.9 sine never exceeds the ceiling (-1 dB) and reports the latency
  4377. auto lim = lynx::makeEffect (lynx::EffectKind::limiter);
  4378. lim->prepare (sr);
  4379. auto slot = lynx::makeEffectSlot (lynx::EffectKind::limiter);
  4380. slot.params[0] = 11.0f / 12.0f; // -1 dB
  4381. slot.params[1] = 0.5f; // +12 dB
  4382. auto l = sine (100.0f, 0.9f, 48000), r = l;
  4383. lim->process (l.data(), r.data(), 48000, slot.params, 120.0);
  4384. CHECK (peak (l, 0) <= std::pow (10.0f, -1.0f / 20.0f) + 1e-4f);
  4385. CHECK (peak (l, 4800) > 0.8f);
  4386. CHECK (lim->latencySamples() == 72);
  4387. // low-pass filter at 500 Hz knocks down an 8 kHz sine
  4388. auto lp = lynx::makeEffect (lynx::EffectKind::filter);
  4389. lp->prepare (sr);
  4390. auto fs = lynx::makeEffectSlot (lynx::EffectKind::filter);
  4391. fs.params[1] = std::log (500.0f / 20.0f) / std::log (1000.0f);
  4392. l = sine (8000.0f, 1.0f, 9600);
  4393. r = l;
  4394. lp->process (l.data(), r.data(), 9600, fs.params, 120.0);
  4395. CHECK (peak (l, 4800) < 0.05f);
  4396. // distortion and chorus: finite, audible output
  4397. for (auto kind : { lynx::EffectKind::distortion, lynx::EffectKind::chorus })
  4398. {
  4399. auto fx = lynx::makeEffect (kind);
  4400. fx->prepare (sr);
  4401. auto ps = lynx::makeEffectSlot (kind);
  4402. l = sine (220.0f, 0.5f, 9600);
  4403. r = l;
  4404. fx->process (l.data(), r.data(), 9600, ps.params, 120.0);
  4405. bool finite = true;
  4406. for (auto v : l)
  4407. finite = finite && std::isfinite (v);
  4408. CHECK (finite && peak (l, 2400) > 0.1f && peak (l, 0) < 2.0f);
  4409. }
  4410. }
  4411. TEST (slide_notes_glide_without_retrigger)
  4412. {
  4413. // sampler with a ramp: the read speed doubles after a one-octave slide
  4414. auto ramp = std::make_shared<lynx::SampleData>();
  4415. for (int i = 0; i < 96000; ++i)
  4416. ramp->left.push_back ((float) i / 96000.0f);
  4417. ramp->sampleRate = 48000.0;
  4418. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  4419. inst->prepare (48000.0);
  4420. inst->noteOn (60, 1.0f);
  4421. std::vector<float> l (2000), r (2000);
  4422. inst->render (l.data(), r.data(), 1000, 1.0f, 1.0f);
  4423. const auto stepBefore = l[999] - l[998];
  4424. inst->slideTo (72, 0.0f, 100.0f);
  4425. std::fill (l.begin(), l.end(), 0.0f);
  4426. inst->render (l.data(), r.data(), 1000, 1.0f, 1.0f);
  4427. const auto stepAfter = l[999] - l[998];
  4428. CHECK (std::abs (stepAfter / stepBefore - 2.0f) < 0.01f);
  4429. // the sequencer sends "slide" (no off) for the slide note
  4430. lynx::Pattern p;
  4431. lynx::Note base { 0, 960, 0, 48 };
  4432. lynx::Note glide { 480, 240, 0, 55 };
  4433. glide.slide = true;
  4434. p.notes = { base, glide };
  4435. lynx::EventBuffer ev;
  4436. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  4437. lynx::schedulePatternLoop (p, p.effectiveLength (4), 0.0, 30000, tps, ev);
  4438. int ons = 0, offs = 0, slides = 0;
  4439. for (auto& e : ev)
  4440. {
  4441. ons += e.type == lynx::NoteEvent::Type::on;
  4442. offs += e.type == lynx::NoteEvent::Type::off;
  4443. if (e.type == lynx::NoteEvent::Type::slide)
  4444. {
  4445. ++slides;
  4446. CHECK (e.key == 55 && std::abs (e.slideSamples - 240.0f / (float) tps) < 1.0f);
  4447. }
  4448. }
  4449. CHECK (ons == 1 && offs == 1 && slides == 1);
  4450. }
  4451. TEST (synth_fm_phase_modulation)
  4452. {
  4453. auto render = [] (const lynx::SynthSettings& s) {
  4454. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  4455. inst->prepare (48000.0);
  4456. lynx::ChannelDef c;
  4457. c.synth = s;
  4458. inst->configure (c, 120.0);
  4459. inst->noteOn (69, 1.0f); // 440 Hz
  4460. std::vector<float> l (48000), r (48000);
  4461. inst->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
  4462. return l;
  4463. };
  4464. // energy (Goertzel) at frequency f over frames [from, to)
  4465. auto energy = [] (const std::vector<float>& x, double f, int from, int to) {
  4466. const auto w = 2.0 * 3.141592653589793 * f / 48000.0, k = 2.0 * std::cos (w);
  4467. double s1 = 0.0, s2 = 0.0;
  4468. for (int i = from; i < to; ++i)
  4469. {
  4470. const auto s0 = x[(std::size_t) i] + k * s1 - s2;
  4471. s2 = s1;
  4472. s1 = s0;
  4473. }
  4474. return s1 * s1 + s2 * s2 - k * s1 * s2;
  4475. };
  4476. lynx::SynthSettings plain;
  4477. plain.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  4478. plain.osc[2] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  4479. plain.cutoff = 20000.0f;
  4480. auto fm = plain;
  4481. fm.fm = 1.0f;
  4482. const auto a = render (plain), b = render (fm);
  4483. // no FM: osc 3 adds (same pitch) and there's no 2nd harmonic; with FM the sidebands appear
  4484. CHECK (energy (a, 880.0, 4800, 24000) < 1e-3 * energy (a, 440.0, 4800, 24000));
  4485. CHECK (energy (b, 880.0, 4800, 24000) > 0.1 * energy (b, 440.0, 4800, 24000));
  4486. CHECK (energy (b, 1320.0, 4800, 24000) > 0.01 * energy (b, 440.0, 4800, 24000));
  4487. for (auto v : b)
  4488. CHECK (std::isfinite (v) && std::abs (v) < 1.0f);
  4489. // index decaying to 0: the start is bright, the end is nearly a sine again
  4490. auto decaying = fm;
  4491. decaying.fmDecay = 0.1f;
  4492. decaying.fmSustain = 0.0f;
  4493. const auto c = render (decaying);
  4494. const auto early = energy (c, 880.0, 0, 2400) / energy (c, 440.0, 0, 2400);
  4495. const auto late = energy (c, 880.0, 36000, 48000) / energy (c, 440.0, 36000, 48000);
  4496. CHECK (early > 0.05 && late < 1e-3);
  4497. }
  4498. TEST (synth_three_oscillators_adsr_filter)
  4499. {
  4500. auto render = [] (const lynx::SynthSettings& s, int frames, int releaseAt) {
  4501. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  4502. inst->prepare (48000.0);
  4503. lynx::ChannelDef c;
  4504. c.synth = s;
  4505. inst->configure (c, 120.0);
  4506. inst->noteOn (69, 1.0f); // A4 = 440 Hz
  4507. std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
  4508. inst->render (l.data(), r.data(), releaseAt, 1.0f, 1.0f);
  4509. inst->noteOff (69);
  4510. inst->render (l.data() + releaseAt, r.data() + releaseAt, frames - releaseAt, 1.0f, 1.0f);
  4511. return l;
  4512. };
  4513. auto peakIn = [] (const std::vector<float>& v, int from, int to) {
  4514. float p = 0.0f;
  4515. for (int i = from; i < to; ++i)
  4516. p = std::max (p, std::abs (v[(std::size_t) i]));
  4517. return p;
  4518. };
  4519. // default: sounds and fades after the release
  4520. const lynx::SynthSettings standard;
  4521. const auto a = render (standard, 48000, 24000);
  4522. CHECK (peakIn (a, 1000, 20000) > 0.05f && peakIn (a, 40000, 48000) < 1e-3f);
  4523. // pure sine with an open filter: zero-crossing count gives ~440 Hz
  4524. lynx::SynthSettings sine;
  4525. sine.osc[0].wave = lynx::Waveform::sine;
  4526. sine.cutoff = 20000.0f;
  4527. const auto b = render (sine, 48000, 47000);
  4528. int crossings = 0;
  4529. for (int i = 4800; i < 4800 + 48000 / 2; ++i)
  4530. crossings += (b[(std::size_t) i - 1] < 0.0f) != (b[(std::size_t) i] < 0.0f);
  4531. CHECK (std::abs (crossings - 440) < 6); // half a second = 220 cycles = 440 crossings
  4532. float sinePeak = 0.0f;
  4533. for (int i = 4800; i < 9600; ++i)
  4534. sinePeak = std::max (sinePeak, std::abs (b[(std::size_t) i]));
  4535. CHECK (sinePeak > 0.2f && sinePeak < 0.35f); // 0.3 level, approximated sine without clipping
  4536. // second oscillator an octave up adds energy; sustain 0 makes the note decay on its own
  4537. lynx::SynthSettings two = sine;
  4538. two.osc[1] = { lynx::Waveform::sine, 1, 0.0f, 1.0f };
  4539. two.sustain = 0.0f;
  4540. two.decay = 0.1f;
  4541. const auto c = render (two, 48000, 47000);
  4542. CHECK (peakIn (c, 100, 2000) > peakIn (b, 100, 2000) && peakIn (c, 30000, 40000) < 1e-3f);
  4543. }
  4544. TEST (set_target_value_round_trips)
  4545. {
  4546. auto s = lynx::Song::makeDefault();
  4547. s.mixer[2].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::delay));
  4548. const lynx::AutomationTarget targets[] = {
  4549. { lynx::AutomationTargetKind::channelVolume, 1 }, { lynx::AutomationTargetKind::channelPan, 2 },
  4550. { lynx::AutomationTargetKind::mixerVolume, 3 }, { lynx::AutomationTargetKind::mixerPan, 0 },
  4551. { lynx::AutomationTargetKind::effectParam, 2, 0, 1 }, { lynx::AutomationTargetKind::tempo, 0 },
  4552. };
  4553. for (const auto& t : targets)
  4554. {
  4555. CHECK (s.setTargetValue (t, 0.25f));
  4556. CHECK (std::abs (s.targetValue (t) - 0.25f) < 1e-5f);
  4557. }
  4558. CHECK (! s.setTargetValue ({ lynx::AutomationTargetKind::channelParam, 0, 0, 3 }, 0.5f)); // lives in the plugin
  4559. }
  4560. TEST (cut_groups_stretch_and_envelope_import)
  4561. {
  4562. // open hat (cut by 1) is cut when the closed one (group 1) plays
  4563. lynx::Song s;
  4564. s.mixer = lynx::Song::defaultMixer (2);
  4565. for (int i = 0; i < 2; ++i)
  4566. {
  4567. lynx::ChannelDef c;
  4568. c.instrument = lynx::InstrumentKind::sampler;
  4569. c.samplePath = "dc";
  4570. c.volume = 1.0f;
  4571. c.mixerTrack = i + 1;
  4572. s.channels.push_back (c);
  4573. }
  4574. s.channels[0].cutBy = 1; // open
  4575. s.channels[1].cutGroup = 1; // closed
  4576. auto& p = s.patterns.emplace_back();
  4577. p.notes = { { 0, 100, 0, 60 }, { 960, 100, 1, 60 } };
  4578. auto provider = [] (const std::string&) {
  4579. auto d = std::make_shared<lynx::SampleData>();
  4580. d->left.assign (96000, 0.5f);
  4581. return std::shared_ptr<const lynx::SampleData> (d);
  4582. };
  4583. auto state = lynx::RackState::build (s, nullptr, 48000.0, provider);
  4584. CHECK (state->hasCutGroups && state->cutTargets[1].size() == 1 && state->cutTargets[1][0] == 0);
  4585. lynx::RackRenderer r;
  4586. r.prepare (48000.0, 512);
  4587. std::vector<float> l (512), rr (512);
  4588. const auto tps = lynx::ticksPerSample (s.bpm, 48000.0);
  4589. float afterCut = 0.0f;
  4590. for (double tick = 0.0; tick < 960.0 * 3; tick += 512 * tps)
  4591. {
  4592. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  4593. if (tick > 960.0 * 2) // the closed one continues; the open one has stopped
  4594. afterCut = std::max (afterCut, std::abs (l[256]));
  4595. }
  4596. CHECK (std::abs (afterCut - 0.5f * 0.78f) < 0.01f); // only the closed one (default velocity 0.78)
  4597. // resampling stretch: 1 s of sample stretched to 4 beats at 120 BPM = 2 s
  4598. auto ramp = std::make_shared<lynx::SampleData>();
  4599. for (int i = 0; i < 48000; ++i)
  4600. ramp->left.push_back ((float) i / 48000.0f);
  4601. ramp->sampleRate = 48000.0;
  4602. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  4603. inst->prepare (48000.0);
  4604. lynx::ChannelDef c;
  4605. c.sampler.stretchBeats = 4.0f;
  4606. inst->configure (c, 120.0);
  4607. inst->noteOn (72, 1.0f); // the note pitch doesn't change the speed
  4608. std::vector<float> a (48000), b (48000);
  4609. inst->render (a.data(), b.data(), 48000, 1.0f, 1.0f);
  4610. CHECK (std::abs (a[47999] - 0.5f) < 0.01f); // after 1 s it has read half
  4611. // FL envelope time: 32950 = 2 s; < 101 = 0
  4612. CHECK (std::abs (flp::Channel::envelopeTime (32950) - 2.0) < 0.01 && flp::Channel::envelopeTime (50) == 0.0);
  4613. }
  4614. TEST (time_stretch_keeps_pitch)
  4615. {
  4616. lynx::SampleData sine;
  4617. sine.sampleRate = 48000.0;
  4618. for (int i = 0; i < 48000; ++i)
  4619. sine.left.push_back (0.5f * std::sin (2.0f * 3.14159265f * 440.0f * (float) i / 48000.0f));
  4620. auto crossingsPerSecond = [] (const lynx::SampleData& d) {
  4621. int c = 0;
  4622. const auto n = (int) d.frames();
  4623. for (int i = 4800; i < n - 4800; ++i)
  4624. c += (d.left[(std::size_t) i - 1] < 0.0f) != (d.left[(std::size_t) i] < 0.0f);
  4625. return (double) c / ((double) (n - 9600) / d.sampleRate);
  4626. };
  4627. for (double ratio : { 2.0, 0.5, 1.37 })
  4628. {
  4629. const auto out = lynx::timeStretch (sine, ratio);
  4630. CHECK (std::abs ((double) out->frames() - 48000.0 * ratio) < 2.0);
  4631. CHECK (std::abs (crossingsPerSecond (*out) - 880.0) < 15.0); // 440 Hz stays 440 Hz
  4632. float peak = 0.0f;
  4633. for (std::size_t i = 4800; i + 4800 < out->frames(); ++i)
  4634. peak = std::max (peak, std::abs (out->left[i]));
  4635. CHECK (peak > 0.4f && peak < 0.6f); // no gaps or clipping in the overlap
  4636. }
  4637. }
  4638. TEST (sampler_region_start_and_end)
  4639. {
  4640. auto ramp = std::make_shared<lynx::SampleData>();
  4641. for (int i = 0; i < 10000; ++i)
  4642. ramp->left.push_back ((float) i / 10000.0f);
  4643. ramp->sampleRate = 48000.0;
  4644. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  4645. inst->prepare (48000.0);
  4646. lynx::ChannelDef c;
  4647. c.sampler.regionStart = 0.25f;
  4648. c.sampler.regionEnd = 0.5f;
  4649. inst->configure (c, 120.0);
  4650. inst->noteOn (60, 1.0f);
  4651. std::vector<float> l (6000), r (6000);
  4652. inst->render (l.data(), r.data(), 6000, 1.0f, 1.0f);
  4653. CHECK (std::abs (l[0] - 0.25f) < 0.001f); // starts at the beginning of the slice
  4654. CHECK (l[2400] > 0.48f && l[2600] == 0.0f); // and stops at its end
  4655. }
  4656. TEST (arpeggiator_plays_held_chord_in_steps)
  4657. {
  4658. auto song = lynx::Song::makeDefault();
  4659. song.bpm = 120.0;
  4660. song.patterns[0].notes.clear();
  4661. for (int key : { 60, 64, 67 })
  4662. song.patterns[0].notes.push_back ({ 0, lynx::ppq * 4, 4, key, 0.8f }); // chord held for 1 bar
  4663. song.channels[4].arp.direction = 1; // up
  4664. song.channels[4].arp.stepBeats = 0.25f;
  4665. auto state = lynx::RackState::build (song, nullptr, 48000.0);
  4666. CHECK (state->arpeggiators[4] != nullptr && state->arpeggiators[0] == nullptr);
  4667. // the arpeggiator generates the notes; we count what it hands to the instrument
  4668. auto& arp = *state->arpeggiators[4];
  4669. const auto tps = lynx::ticksPerSample (song.bpm, 48000.0);
  4670. std::vector<int> keys;
  4671. lynx::EventBuffer ev;
  4672. for (double tick = 0.0; tick < lynx::ppq * 2.0; tick += 512 * tps)
  4673. {
  4674. ev.clear();
  4675. lynx::schedulePatternLoop (song.patterns[0], song.patterns[0].effectiveLength (4), tick, 512, tps, ev);
  4676. int in = 0, out = 0;
  4677. for (auto& e : ev)
  4678. if (e.channel == 4)
  4679. arp.input[(std::size_t) in++] = e;
  4680. arp.process (4, arp.input.data(), in, tick, tps, 512, song.channels[4].arp, arp.output.data(), out);
  4681. for (int i = 0; i < out; ++i)
  4682. if (arp.output[(std::size_t) i].type == lynx::NoteEvent::Type::on)
  4683. keys.push_back (arp.output[(std::size_t) i].key);
  4684. }
  4685. // 2 beats = 8 sixteenth steps: 60 64 67 60 64 67 60 64
  4686. CHECK (keys.size() >= 8); // the last block goes slightly past 2 beats
  4687. const int expected[] = { 60, 64, 67, 60, 64, 67, 60, 64 };
  4688. for (std::size_t i = 0; i < keys.size() && i < 8; ++i)
  4689. CHECK (keys[i] == expected[i]);
  4690. // actually rendering also makes sound
  4691. lynx::RackRenderer r;
  4692. r.prepare (48000.0, 512);
  4693. std::vector<float> l (512), rr (512);
  4694. float peak = 0.0f;
  4695. for (double tick = 0.0; tick < lynx::ppq; tick += 512 * tps)
  4696. {
  4697. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  4698. for (auto v : l)
  4699. peak = std::max (peak, std::abs (v));
  4700. }
  4701. CHECK (peak > 0.01f);
  4702. }
  4703. TEST (audio_clip_fades_and_gain)
  4704. {
  4705. lynx::Song s;
  4706. s.bpm = 120.0;
  4707. s.mode = lynx::PlayMode::song;
  4708. s.mixer = lynx::Song::defaultMixer (1);
  4709. lynx::ChannelDef c;
  4710. c.instrument = lynx::InstrumentKind::audioClip;
  4711. c.samplePath = "dc";
  4712. c.volume = 1.0f;
  4713. c.mixerTrack = 1;
  4714. s.channels.push_back (c);
  4715. s.patterns.emplace_back();
  4716. lynx::Clip clip;
  4717. clip.kind = lynx::ClipKind::audio;
  4718. clip.channel = 0;
  4719. clip.length = lynx::ppq * 4; // 2 s at 120 BPM
  4720. clip.fadeIn = lynx::ppq; // 0.5 s
  4721. clip.fadeOut = lynx::ppq;
  4722. clip.gain = 0.5f;
  4723. s.playlist.push_back (clip);
  4724. auto provider = [] (const std::string&) {
  4725. auto d = std::make_shared<lynx::SampleData>();
  4726. d->left.assign (48000 * 4, 1.0f);
  4727. return std::shared_ptr<const lynx::SampleData> (d);
  4728. };
  4729. auto state = lynx::RackState::build (s, nullptr, 48000.0, provider);
  4730. lynx::RackRenderer r;
  4731. r.prepare (48000.0, 512);
  4732. std::vector<float> all;
  4733. std::vector<float> l (512), rr (512);
  4734. const auto tps = lynx::ticksPerSample (s.bpm, 48000.0);
  4735. for (double tick = 0.0; tick < lynx::ppq * 4.0; tick += 512 * tps)
  4736. {
  4737. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  4738. all.insert (all.end(), l.begin(), l.end());
  4739. }
  4740. CHECK (all[100] < 0.01f); // fade-in start
  4741. CHECK (std::abs (all[12000] - 0.25f) < 0.02f); // fade-in midpoint: half of 0.5
  4742. CHECK (std::abs (all[48000] - 0.5f) < 0.01f); // body: the clip gain
  4743. CHECK (std::abs (all[96000 - 12000] - 0.25f) < 0.02f); // fade-out midpoint
  4744. }
  4745. TEST (slicer_detects_hits_and_plays_slices_by_key)
  4746. {
  4747. // 4 hits (decaying noise) every 0.25 s
  4748. lynx::SampleData loop;
  4749. loop.sampleRate = 48000.0;
  4750. loop.left.assign (48000, 0.0f);
  4751. lynx::Noise noise;
  4752. for (int hit = 0; hit < 4; ++hit)
  4753. for (int i = 0; i < 6000; ++i)
  4754. loop.left[(std::size_t) (hit * 12000 + i)] = noise.next() * 0.8f * std::exp (-(float) i / 1500.0f);
  4755. const auto slices = lynx::detectSlices (loop, 0.5f);
  4756. CHECK (slices.size() == 4);
  4757. for (std::size_t i = 0; i < slices.size(); ++i)
  4758. CHECK (std::abs (slices[i] - 0.25f * (float) i) < 0.01f);
  4759. // key 61 plays the second slice, which starts at 0.25 and ends before 0.5
  4760. auto ramp = std::make_shared<lynx::SampleData>();
  4761. for (int i = 0; i < 48000; ++i)
  4762. ramp->left.push_back ((float) i / 48000.0f);
  4763. ramp->sampleRate = 48000.0;
  4764. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  4765. inst->prepare (48000.0);
  4766. lynx::ChannelDef c;
  4767. c.sampler.slices = slices;
  4768. inst->configure (c, 120.0);
  4769. inst->noteOn (61, 1.0f);
  4770. std::vector<float> l (20000), r (20000);
  4771. inst->render (l.data(), r.data(), 20000, 1.0f, 1.0f);
  4772. CHECK (std::abs (l[0] - 0.25f) < 0.01f && l[11000] > 0.45f && l[13000] == 0.0f);
  4773. }
  4774. TEST (project_templates_render_sound)
  4775. {
  4776. const auto names = lynx::templateNames();
  4777. CHECK (names.size() == 4);
  4778. for (int t = 1; t < (int) names.size(); ++t)
  4779. {
  4780. auto song = lynx::makeTemplate (t);
  4781. auto state = lynx::RackState::build (song, nullptr, 48000.0);
  4782. lynx::RackRenderer r;
  4783. r.prepare (48000.0, 512);
  4784. std::vector<float> l (512), rr (512);
  4785. const auto tps = lynx::ticksPerSample (song.bpm, 48000.0);
  4786. float peak = 0.0f;
  4787. for (double tick = 0.0; tick < lynx::ppq * 4.0; tick += 512 * tps)
  4788. {
  4789. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  4790. for (auto v : l)
  4791. peak = std::max (peak, std::abs (v));
  4792. }
  4793. CHECK (peak > 0.05f && peak < 4.0f);
  4794. }
  4795. CHECK (lynx::makeTemplate (0).patterns[0].notes.empty());
  4796. }
  4797. // --- interface translations (resources/i18n) -------------------------------------------------------
  4798. namespace i18ntest
  4799. {
  4800. std::string readFile (const std::filesystem::path& path)
  4801. {
  4802. std::ifstream in (path, std::ios::binary);
  4803. return { std::istreambuf_iterator<char> (in), std::istreambuf_iterator<char>() };
  4804. }
  4805. std::string unescape (const std::string& s)
  4806. {
  4807. std::string out;
  4808. for (std::size_t i = 0; i < s.size(); ++i)
  4809. {
  4810. if (s[i] == '\\' && i + 1 < s.size())
  4811. {
  4812. const auto n = s[++i];
  4813. out += n == 'n' ? '\n' : n == 't' ? '\t' : n;
  4814. }
  4815. else
  4816. out += s[i];
  4817. }
  4818. return out;
  4819. }
  4820. // reads a quoted string starting at s[pos] == '"'; returns the raw (escaped) contents and moves pos past the closing quote
  4821. bool quoted (const std::string& s, std::size_t& pos, std::string& raw)
  4822. {
  4823. if (pos >= s.size() || s[pos] != '"')
  4824. return false;
  4825. raw.clear();
  4826. for (++pos; pos < s.size() && s[pos] != '"' && s[pos] != '\n'; ++pos)
  4827. {
  4828. if (s[pos] == '\\' && pos + 1 < s.size())
  4829. raw += s[pos++];
  4830. raw += s[pos];
  4831. }
  4832. if (pos >= s.size() || s[pos] != '"')
  4833. return false;
  4834. ++pos;
  4835. return true;
  4836. }
  4837. // JUCE LocalisedStrings format: "english" = "translation"; duplicates are reported in `duplicates`
  4838. std::map<std::string, std::string> parseTable (const std::string& text, std::vector<std::string>& duplicates)
  4839. {
  4840. std::map<std::string, std::string> table;
  4841. std::istringstream lines (text);
  4842. std::string line;
  4843. while (std::getline (lines, line))
  4844. {
  4845. std::size_t pos = line.find_first_not_of (" \t");
  4846. std::string key, value;
  4847. if (pos == std::string::npos || ! quoted (line, pos, key))
  4848. continue;
  4849. pos = line.find ('"', pos);
  4850. if (pos == std::string::npos || ! quoted (line, pos, value))
  4851. value.clear();
  4852. const auto k = unescape (key);
  4853. if (table.count (k) != 0)
  4854. duplicates.push_back (k);
  4855. table[k] = unescape (value);
  4856. }
  4857. return table;
  4858. }
  4859. // string literals passed to tr() / TR_NOOP() in the sources (adjacent literals are concatenated)
  4860. std::map<std::string, std::string> sourceKeys (const std::filesystem::path& srcDir)
  4861. {
  4862. std::map<std::string, std::string> keys;
  4863. for (const auto& entry : std::filesystem::recursive_directory_iterator (srcDir))
  4864. {
  4865. const auto ext = entry.path().extension().string();
  4866. if (! entry.is_regular_file() || (ext != ".cpp" && ext != ".h"))
  4867. continue;
  4868. // drop // comment lines (they may quote tr ("...") as an example)
  4869. std::string text, line;
  4870. std::istringstream in (readFile (entry.path()));
  4871. while (std::getline (in, line))
  4872. {
  4873. const auto first = line.find_first_not_of (" \t");
  4874. text += (first != std::string::npos && line.compare (first, 2, "//") == 0) ? std::string() : line;
  4875. text += '\n';
  4876. }
  4877. auto isIdent = [] (char c) { return std::isalnum ((unsigned char) c) || c == '_'; };
  4878. for (const std::string name : { "tr", "TR_NOOP" })
  4879. for (auto at = text.find (name); at != std::string::npos; at = text.find (name, at + 1))
  4880. {
  4881. if ((at > 0 && isIdent (text[at - 1])) || isIdent (text[at + name.size()]))
  4882. continue;
  4883. auto pos = text.find_first_not_of (" \t\n", at + name.size());
  4884. if (pos == std::string::npos || text[pos] != '(')
  4885. continue;
  4886. pos = text.find_first_not_of (" \t\n", pos + 1);
  4887. std::string key, raw;
  4888. bool any = false;
  4889. while (pos != std::string::npos && quoted (text, pos, raw))
  4890. {
  4891. key += raw;
  4892. any = true;
  4893. pos = text.find_first_not_of (" \t\n", pos);
  4894. }
  4895. if (any)
  4896. keys.emplace (unescape (key), entry.path().filename().string());
  4897. }
  4898. }
  4899. return keys;
  4900. }
  4901. std::set<std::string> placeholders (const std::string& s)
  4902. {
  4903. std::set<std::string> found;
  4904. for (auto open = s.find ('{'); open != std::string::npos; open = s.find ('{', open + 1))
  4905. if (const auto close = s.find ('}', open); close != std::string::npos && close - open <= 3)
  4906. found.insert (s.substr (open, close - open + 1));
  4907. return found;
  4908. }
  4909. } // namespace i18ntest
  4910. TEST (translations_cover_every_ui_string)
  4911. {
  4912. using namespace i18ntest;
  4913. const auto text = readFile ("resources/i18n/pt-BR.txt");
  4914. CHECK (! text.empty());
  4915. std::vector<std::string> duplicates;
  4916. const auto table = parseTable (text, duplicates);
  4917. CHECK (table.size() > 300);
  4918. CHECK (duplicates.empty());
  4919. for (const auto& d : duplicates)
  4920. std::printf (" duplicate pt-BR entry: \"%s\"\n", d.c_str());
  4921. const auto keys = sourceKeys ("src");
  4922. CHECK (keys.size() > 300);
  4923. int missing = 0;
  4924. for (const auto& [key, file] : keys)
  4925. if (auto it = table.find (key); it == table.end() || it->second.empty())
  4926. {
  4927. ++missing;
  4928. std::printf (" no pt-BR translation for \"%s\" (%s)\n", key.c_str(), file.c_str());
  4929. }
  4930. CHECK (missing == 0);
  4931. // no empty values, and every {n} placeholder survives the translation
  4932. int empty = 0, badPlaceholders = 0;
  4933. for (const auto& [english, portuguese] : table)
  4934. {
  4935. empty += portuguese.empty() ? 1 : 0;
  4936. if (placeholders (english) != placeholders (portuguese))
  4937. {
  4938. ++badPlaceholders;
  4939. std::printf (" placeholders differ: \"%s\"\n", english.c_str());
  4940. }
  4941. }
  4942. CHECK (empty == 0);
  4943. CHECK (badPlaceholders == 0);
  4944. }
  4945. TEST (translations_embedded_table_is_up_to_date)
  4946. {
  4947. // src/ui/I18nData.h must be regenerated (tools/make_i18n.py) whenever the .txt changes
  4948. const auto text = i18ntest::readFile ("resources/i18n/pt-BR.txt");
  4949. CHECK (text.size() == sizeof (lynx::i18n::data::ptBR));
  4950. CHECK (std::memcmp (text.data(), lynx::i18n::data::ptBR, std::min (text.size(), sizeof (lynx::i18n::data::ptBR))) == 0);
  4951. }
  4952. // --- workflow shell: pattern/channel operations, key map, workspace layout, control math -------------
  4953. TEST (workflow_pattern_navigation_and_insert)
  4954. {
  4955. auto song = lynx::Song::makeDefault();
  4956. song.patterns.resize (3);
  4957. song.patterns[1].name = "B";
  4958. song.patterns[2].name = "C";
  4959. song.patterns[2].notes.push_back ({});
  4960. song.currentPattern = 0;
  4961. CHECK (lynx::stepPattern (song, 1) == 1 && lynx::stepPattern (song, -1) == 0 && lynx::stepPattern (song, 10) == 2);
  4962. // pattern 0 has the default notes, 1 is empty, 2 has a note
  4963. CHECK (! lynx::isPatternEmpty (song.patterns[0]) && lynx::isPatternEmpty (song.patterns[1]));
  4964. CHECK (lynx::findNextEmptyPattern (song, 0) == 1 && lynx::findNextEmptyPattern (song, 1) == -1);
  4965. CHECK (lynx::gotoNextEmptyPattern (song) == 1 && song.currentPattern == 1);
  4966. song.patterns[1].notes.push_back ({});
  4967. const auto before = song.patterns.size();
  4968. CHECK (lynx::gotoNextEmptyPattern (song) == (int) before && song.patterns.size() == before + 1);
  4969. CHECK (song.patterns.back().name == "Pattern 1" || song.patterns.back().name.rfind ("Pattern ", 0) == 0);
  4970. // insert keeps Playlist clips on their patterns
  4971. lynx::Clip clip;
  4972. clip.pattern = 2;
  4973. song.playlist = { clip };
  4974. song.arrangements.push_back ({ "Other", { clip }, 0, 0, {} });
  4975. CHECK (lynx::insertPattern (song, 1, "New") == 1 && song.currentPattern == 1);
  4976. CHECK (song.patterns[1].name == "New" && song.patterns[1].notes.empty() && song.patterns[3].name == "C");
  4977. CHECK (song.playlist[0].pattern == 3 && song.arrangements[1].clips[0].pattern == 3);
  4978. }
  4979. TEST (workflow_split_pattern_by_channel)
  4980. {
  4981. auto song = lynx::Song::makeDefault();
  4982. song.patterns.resize (1);
  4983. auto& p = song.patterns[0];
  4984. p.name = "Beat";
  4985. p.notes.clear();
  4986. for (int i = 0; i < 4; ++i)
  4987. p.notes.push_back ({ (std::int64_t) i * lynx::ticksPerStep, lynx::ticksPerStep, i % 2 == 0 ? 0 : 2, 60 });
  4988. song.channels[2].colour = 0xff112233;
  4989. lynx::Clip clip;
  4990. clip.pattern = 0;
  4991. song.playlist = { clip };
  4992. const auto created = lynx::splitPatternByChannel (song, 0);
  4993. CHECK (created == 2 && song.patterns.size() == 3);
  4994. CHECK (song.patterns[0].notes.size() == 4); // the original is unchanged
  4995. CHECK (song.patterns[1].name == "Beat - " + song.channels[0].name && song.patterns[1].notes.size() == 2);
  4996. CHECK (song.patterns[2].colour == 0xff112233u && song.patterns[2].notes.size() == 2 && song.patterns[2].notes[0].channel == 2);
  4997. CHECK (song.playlist[0].pattern == 0);
  4998. CHECK (lynx::splitPatternByChannel (song, 99) == 0);
  4999. }
  5000. TEST (workflow_channel_selection)
  5001. {
  5002. lynx::ChannelSelection s;
  5003. s.selectOnly (2);
  5004. s.toggle (5);
  5005. s.toggle (0);
  5006. CHECK ((s.rows == std::vector<int> { 0, 2, 5 }) && s.contains (5) && ! s.contains (3));
  5007. s.toggle (2);
  5008. CHECK ((s.rows == std::vector<int> { 0, 5 }));
  5009. s.selectOnly (3);
  5010. s.extendTo (6);
  5011. CHECK ((s.rows == std::vector<int> { 3, 4, 5, 6 }));
  5012. s.extendTo (1);
  5013. CHECK ((s.rows == std::vector<int> { 1, 2, 3 }));
  5014. s.selectOnly (4);
  5015. s.toggle (4); // the last one stays
  5016. CHECK ((s.rows == std::vector<int> { 4 }));
  5017. s.selectAll (3);
  5018. CHECK ((s.rows == std::vector<int> { 0, 1, 2 }));
  5019. s.rows = { 7, 9 };
  5020. s.clamp (5);
  5021. CHECK (s.rows.size() == 1 && s.rows[0] < 5);
  5022. }
  5023. TEST (workflow_move_clone_insert_channels)
  5024. {
  5025. auto song = lynx::Song::makeDefault();
  5026. const auto names = [&] {
  5027. std::vector<std::string> n;
  5028. for (const auto& c : song.channels)
  5029. n.push_back (c.name);
  5030. return n;
  5031. };
  5032. const auto original = names();
  5033. const auto count = (int) song.channels.size();
  5034. CHECK (count >= 4);
  5035. // references that must follow their channel
  5036. song.patterns[0].notes.push_back ({ 0, 240, 1, 64 });
  5037. lynx::Automation a;
  5038. a.target = { lynx::AutomationTargetKind::channelVolume, 1 };
  5039. song.automations.push_back (a);
  5040. song.midiLinks.push_back ({ 7, -1, { lynx::AutomationTargetKind::channelPan, 3 } });
  5041. lynx::Clip audio;
  5042. audio.kind = lynx::ClipKind::audio;
  5043. audio.channel = 3;
  5044. song.playlist.push_back (audio);
  5045. std::vector<int> selection { 1, 3 };
  5046. CHECK (lynx::moveChannels (song, selection, -1));
  5047. CHECK ((selection == std::vector<int> { 0, 2 }));
  5048. CHECK (song.channels[0].name == original[1] && song.channels[1].name == original[0] && song.channels[2].name == original[3]
  5049. && song.channels[3].name == original[2]);
  5050. CHECK (song.automations[0].target.index == 0 && song.midiLinks[0].target.index == 2 && song.playlist.back().channel == 2);
  5051. CHECK (std::any_of (song.patterns[0].notes.begin(), song.patterns[0].notes.end(),
  5052. [] (const lynx::Note& n) { return n.channel == 0 && n.key == 64; }));
  5053. CHECK (! lynx::moveChannels (song, selection, -1)); // the block is at the top
  5054. CHECK (lynx::moveChannels (song, selection, 1) && (selection == std::vector<int> { 1, 3 }) && names() == original);
  5055. // clone: settings, not notes, right below, with a new name
  5056. song.channels[1].volume = 0.33f;
  5057. const auto notesBefore = song.patterns[0].notes.size();
  5058. const auto clone = lynx::cloneChannel (song, 1);
  5059. CHECK (clone == 2 && (int) song.channels.size() == count + 1);
  5060. CHECK (song.channels[2].name == original[1] + " #2" && song.channels[2].volume == 0.33f);
  5061. CHECK (song.patterns[0].notes.size() == notesBefore);
  5062. CHECK (song.channels[3].name == original[2] && song.automations[0].target.index == 1);
  5063. CHECK (lynx::cloneChannel (song, 1) == 2 && song.channels[2].name == original[1] + " #3");
  5064. // remove several
  5065. CHECK (lynx::removeChannels (song, { 2, 3, 2 }) == 2 && (int) song.channels.size() == count);
  5066. CHECK (names() == original);
  5067. }
  5068. TEST (workflow_send_channels_to_free_inserts)
  5069. {
  5070. auto song = lynx::Song::makeDefault();
  5071. for (auto& c : song.channels)
  5072. c.mixerTrack = 0;
  5073. song.mixer[1].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::delay)); // not free: has effects
  5074. song.channels[0].mixerTrack = 2; // not free: used
  5075. song.setRoute (5, 3, true); // 3 is not free: receives a send
  5076. CHECK (! lynx::isInsertFree (song, 1) && ! lynx::isInsertFree (song, 2) && ! lynx::isInsertFree (song, 3) && lynx::isInsertFree (song, 4));
  5077. song.channels[1].colour = 0xff445566;
  5078. const auto tracks = lynx::sendChannelsToFreeInserts (song, { 1, 2 });
  5079. CHECK ((tracks == std::vector<int> { 4, 5 }) || (tracks == std::vector<int> { 4, 6 }));
  5080. CHECK (song.channels[1].mixerTrack == 4 && song.mixer[4].name == song.channels[1].name && song.mixer[4].colour == 0xff445566u);
  5081. CHECK (song.mixer[(std::size_t) tracks[1]].name == song.channels[2].name);
  5082. // with no free insert left, a new one is added at the end
  5083. auto full = lynx::Song::makeDefault();
  5084. full.mixer = lynx::Song::defaultMixer (1);
  5085. full.channels[0].mixerTrack = 1;
  5086. const auto added = lynx::sendChannelsToFreeInserts (full, { 1 });
  5087. CHECK (added.size() == 1 && added[0] == 2 && full.mixer.size() == 3 && full.channels[1].mixerTrack == 2);
  5088. }
  5089. TEST (workflow_palette_random_colour)
  5090. {
  5091. const auto& palette = lynx::lynxPalette();
  5092. for (std::uint32_t seed = 0; seed < 50; ++seed)
  5093. {
  5094. const auto current = palette[seed % palette.size()];
  5095. const auto c = lynx::randomPaletteColour (current, seed);
  5096. CHECK (c != current && std::find (palette.begin(), palette.end(), c) != palette.end());
  5097. }
  5098. }
  5099. TEST (workflow_key_map)
  5100. {
  5101. using namespace lynx::keys;
  5102. KeyMap map;
  5103. const auto global = Context::global, rack = Context::channelRack;
  5104. CHECK (map.find ({ space, none }, global, true) == "transport.playStop");
  5105. CHECK (map.find ({ space, ctrl }, global, true) == "transport.pause");
  5106. CHECK (map.find ({ f6, none }, global, true) == "panel.channelRack");
  5107. CHECK (map.find ({ f8, alt }, global, false) == "panel.browser");
  5108. CHECK (map.find ({ insert, ctrl | shift }, global, false) == "pattern.insert");
  5109. CHECK (map.find ({ numpadPlus, none }, rack, true) == "pattern.next");
  5110. // panel context: only while focused
  5111. CHECK (map.find ({ 'L', ctrl }, rack, true) == "rack.sendToInserts");
  5112. CHECK (map.find ({ 'L', ctrl }, global, true).empty());
  5113. CHECK (map.find ({ up, alt }, rack, false) == "rack.moveUp");
  5114. // typing piano: note keys play, other plain keys only for whilePiano actions; chords always work
  5115. CHECK (KeyMap::playsNote ({ 'Z', none }, true) && ! KeyMap::playsNote ({ 'Z', none }, false) && ! KeyMap::playsNote ({ 'Z', ctrl }, true));
  5116. CHECK (map.find ({ 'L', none }, global, true) == "transport.toggleMode");
  5117. CHECK (map.find ({ '1', none }, rack, false) == "rack.mute1" && map.find ({ '0', none }, rack, false) == "rack.mute10");
  5118. CHECK (map.find ({ '1', none }, rack, true).empty()); // a plain digit is not a shortcut while the piano is on
  5119. CHECK (map.find ({ '2', ctrl }, rack, true) == "rack.solo2");
  5120. CHECK (typingPianoNote ('Z') == 60 && typingPianoNote ('q') == 72 && typingPianoNote ('P') == 88 && typingPianoNote ('L') == -1);
  5121. CHECK (map.conflicts().empty());
  5122. // chord text
  5123. CHECK (toString ({ f4, ctrl | shift }) == "Ctrl+Shift+F4" && toString ({ numpadMinus, none }) == "Numpad -");
  5124. CHECK (parseChord ("Shift+Ctrl+Ins") == (Chord { insert, ctrl | shift }));
  5125. CHECK (parseChord ("alt+up") == (Chord { up, alt }) && parseChord ("k") == (Chord { 'K', none }));
  5126. CHECK (parseChord ("Numpad +") == (Chord { numpadPlus, none }) && ! parseChord ("F13") && ! parseChord ("Ctrl+"));
  5127. for (const auto& a : actions())
  5128. for (const auto& c : a.defaults)
  5129. CHECK (parseChord (toString (c)) == c);
  5130. // JSON overrides
  5131. std::vector<std::string> errors;
  5132. CHECK (map.loadJson (R"({ "pattern.next": "Ctrl+Right", "rack.clone": ["Alt+D", "Ctrl+D"], "help.shortcuts": "", "nope": "F1", "file.open": "Hyper+Q" })", &errors));
  5133. CHECK (errors.size() == 2);
  5134. CHECK (map.find ({ right, ctrl }, global, false) == "pattern.next" && map.find ({ numpadPlus, none }, global, false).empty());
  5135. CHECK (map.find ({ 'D', ctrl }, rack, false) == "rack.clone" && map.find ({ f1, none }, global, false).empty());
  5136. map.setChords ("pattern.previous", { { right, ctrl } });
  5137. CHECK (map.conflicts().size() == 1);
  5138. KeyMap reloaded;
  5139. CHECK (reloaded.loadJson (map.toJson()) && reloaded.chordsFor ("rack.clone") == map.chordsFor ("rack.clone"));
  5140. CHECK (! map.loadJson ("[1, 2]") && ! map.loadJson ("{\"a\": }"));
  5141. map.resetToDefaults();
  5142. CHECK (map.find ({ numpadPlus, none }, global, false) == "pattern.next");
  5143. }
  5144. TEST (workflow_workspace_layout)
  5145. {
  5146. using namespace lynx::workspace;
  5147. auto l = Layout::makeDefault (1280, 620);
  5148. // every visible panel is inside the desktop; the default shows Browser, Playlist and Channel Rack
  5149. for (const auto& p : l.panels)
  5150. CHECK (p.bounds.x >= 0 && p.bounds.y >= 0 && p.bounds.right() <= 1280 && p.bounds.bottom() <= 620 && p.bounds.w >= minWidth);
  5151. CHECK (l[Panel::browser].visible && l[Panel::playlist].visible && l[Panel::channelRack].visible);
  5152. CHECK (! l[Panel::pianoRoll].visible && ! l[Panel::mixer].visible);
  5153. CHECK (l[Panel::playlist].bounds.x == l[Panel::browser].bounds.right() && l[Panel::playlist].bounds.right() == 1280);
  5154. CHECK (l.isFrontmost (Panel::channelRack) && l.focused == Panel::channelRack);
  5155. // toggle: show+focus, focus, hide
  5156. CHECK (l.toggle (Panel::mixer) && l.focused == Panel::mixer && l.isFrontmost (Panel::mixer));
  5157. CHECK (l.toggle (Panel::channelRack) && l.focused == Panel::channelRack && l.isFrontmost (Panel::channelRack));
  5158. CHECK (! l.toggle (Panel::channelRack) && ! l[Panel::channelRack].visible && l.focused == Panel::mixer);
  5159. l.toggleMaximised (Panel::playlist);
  5160. CHECK (l[Panel::playlist].maximised && l.focused == Panel::playlist);
  5161. // save and load at another size: positions scale
  5162. l[Panel::mixer].detached = true;
  5163. l[Panel::mixer].windowBounds = { 100, 50, 900, 500 };
  5164. const auto text = l.serialise();
  5165. const auto big = Layout::parse (text, 2560, 1240);
  5166. CHECK (big.has_value());
  5167. if (big)
  5168. {
  5169. CHECK ((*big)[Panel::playlist].maximised && (*big)[Panel::mixer].detached && (*big)[Panel::mixer].windowBounds.w == 900);
  5170. CHECK (std::abs ((*big)[Panel::playlist].bounds.x - 2 * l[Panel::playlist].bounds.x) <= 1);
  5171. CHECK (big->order == l.order && big->focused == l.focused);
  5172. }
  5173. 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));
  5174. // resize keeps panels inside and edges glued to the border
  5175. l.resize (1000, 500);
  5176. CHECK (l[Panel::playlist].bounds.right() == 1000 && l[Panel::browser].bounds.bottom() == 500);
  5177. for (const auto& p : l.panels)
  5178. CHECK (p.bounds.right() <= 1000 && p.bounds.bottom() <= 500);
  5179. // off-screen panels come back
  5180. l[Panel::pianoRoll].bounds = { 5000, -300, 400, 300 };
  5181. CHECK (l.bringBackOffscreen() == 1 && l[Panel::pianoRoll].bounds.x + 400 <= 1000 && l[Panel::pianoRoll].bounds.y == 0);
  5182. // snapping: to the desktop, to another panel, and resizing one edge
  5183. CHECK ((snap ({ 5, 6, 200, 100 }, {}, 1000, 500) == Rect { 0, 0, 200, 100 }));
  5184. CHECK ((snap ({ 304, 50, 200, 100 }, { { 0, 0, 300, 400 } }, 1000, 500) == Rect { 300, 50, 200, 100 }));
  5185. CHECK ((snap ({ 304, 50, 200, 100 }, { { 0, 0, 300, 400 } }, 1000, 500, 2) == Rect { 304, 50, 200, 100 })); // out of range
  5186. CHECK ((snap ({ 100, 100, 195, 100 }, { { 300, 0, 300, 400 } }, 1000, 500, 8, right) == Rect { 100, 100, 200, 100 }));
  5187. CHECK ((snap ({ 100, 100, 195, 100 }, { { 300, 1000, 300, 400 } }, 2000, 2000, 8, right) == Rect { 100, 100, 195, 100 })); // not facing
  5188. }
  5189. TEST (workflow_control_math)
  5190. {
  5191. using namespace lynx::controls;
  5192. CHECK (std::abs (dragValue (0.5, 75, 0, 1, 150, false) - 1.0) < 1e-9);
  5193. CHECK (std::abs (dragValue (0.5, 75, 0, 1, 150, true) - 0.55) < 1e-9);
  5194. CHECK (dragValue (0.5, -1000, -1, 1, 150, false) == -1.0);
  5195. 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);
  5196. CHECK (wheelValue (3, 1, 0, 10, true, 1) == 4.0); // integer controls move at least one step
  5197. CHECK (std::abs (snapToInterval (0.234, 0, 0.1) - 0.2) < 1e-9);
  5198. CHECK (tempoDrag (130.0, 8, false, false) == 132.0 && tempoDrag (130.0, -3, false, false) == 130.0);
  5199. CHECK (std::abs (tempoDrag (130.0, 25, true, false) - 130.25) < 1e-9);
  5200. CHECK (std::abs (tempoDrag (130.0, 8, false, true) - 130.2) < 1e-9);
  5201. CHECK (tempoDrag (520.0, 400, false, false) == lynx::maxTempo && halfTempo (15.0) == lynx::minTempo && doubleTempo (130.0) == 260.0);
  5202. TapTempo tap;
  5203. CHECK (! tap.tap (1000.0));
  5204. CHECK (tap.tap (1500.0) == 120.0);
  5205. CHECK (tap.tap (2000.0) == 120.0 && tap.count() == 3);
  5206. CHECK (std::abs (*tap.tap (2400.0) - 128.57) < 0.01); // average of 500, 500, 400 ms
  5207. CHECK (! tap.tap (9000.0) && tap.count() == 1); // a long pause starts over
  5208. }
  5209. int main (int argc, char** argv)
  5210. {
  5211. if (argc > 1)
  5212. realFlpDir = argv[1];
  5213. const char* only = std::getenv ("LYNX_TEST"); // run only the tests whose name contains this
  5214. for (auto& [name, fn] : registry())
  5215. {
  5216. if (only != nullptr && std::strstr (name, only) == nullptr)
  5217. continue;
  5218. std::printf ("%s\n", name);
  5219. fn();
  5220. }
  5221. std::printf ("\n%d checks, %d failures\n", checks, failures);
  5222. return failures == 0 ? 0 : 1;
  5223. }
  5224. TEST (synth_cutoff_keytrack)
  5225. {
  5226. // the cutoff follows the note from key 48 (130.81 Hz), like Harmor's "Key to filter frequency"
  5227. auto rms = [] (float keyTrack, int key) {
  5228. lynx::SynthSettings s;
  5229. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  5230. s.cutoff = 130.81f;
  5231. s.keyTrack = keyTrack;
  5232. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  5233. inst->prepare (48000.0);
  5234. lynx::ChannelDef c;
  5235. c.synth = s;
  5236. inst->configure (c, 120.0);
  5237. inst->noteOn (key, 1.0f);
  5238. std::vector<float> l (24000), r (24000);
  5239. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  5240. double e = 0.0;
  5241. for (int i = 4800; i < 24000; ++i)
  5242. e += (double) l[(std::size_t) i] * l[(std::size_t) i];
  5243. return std::sqrt (e / 19200.0);
  5244. };
  5245. const auto fixed = rms (0.0f, 72), tracked = rms (1.0f, 72);
  5246. CHECK (tracked > 4.0 * fixed); // 2 octaves up: without keytrack the low-pass cuts
  5247. CHECK (std::abs (rms (1.0f, 48) - rms (0.0f, 48)) < 0.05 * rms (0.0f, 48)); // unchanged at the reference key
  5248. }
  5249. namespace
  5250. {
  5251. std::vector<float> renderSynthNote (const lynx::SynthSettings& s, int key, int frames)
  5252. {
  5253. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  5254. inst->prepare (48000.0);
  5255. lynx::ChannelDef c;
  5256. c.synth = s;
  5257. inst->configure (c, 120.0);
  5258. inst->noteOn (key, 1.0f);
  5259. std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
  5260. inst->render (l.data(), r.data(), frames, 1.0f, 1.0f);
  5261. return l;
  5262. }
  5263. double goertzel (const std::vector<float>& x, double f, int from, int to)
  5264. {
  5265. const auto w = 2.0 * 3.141592653589793 * f / 48000.0, k = 2.0 * std::cos (w);
  5266. double s1 = 0.0, s2 = 0.0;
  5267. for (int i = from; i < to; ++i)
  5268. {
  5269. const auto s0 = x[(std::size_t) i] + k * s1 - s2;
  5270. s2 = s1;
  5271. s1 = s0;
  5272. }
  5273. return s1 * s1 + s2 * s2 - k * s1 * s2;
  5274. }
  5275. } // namespace
  5276. TEST (synth_pitch_sweep_ring_and_bands)
  5277. {
  5278. lynx::SynthSettings sine;
  5279. sine.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  5280. sine.cutoff = 20000.0f;
  5281. // sweep: starts at f·(1 + sweep) and returns to f (time constant s = 10 ms; s⁴ falls 4× faster)
  5282. auto sweep = sine;
  5283. sweep.sweep = 1.0f;
  5284. sweep.sweepTime = 0.01f;
  5285. const auto a = renderSynthNote (sweep, 69, 24000);
  5286. auto crossings = [] (const std::vector<float>& x, int to) {
  5287. int n = 0;
  5288. for (int i = 1; i < to; ++i)
  5289. n += (x[(std::size_t) i - 1] < 0.0f) != (x[(std::size_t) i] < 0.0f);
  5290. return n;
  5291. };
  5292. CHECK (crossings (a, 480) > 1.3 * crossings (renderSynthNote (sine, 69, 480), 480)); // 10 ms: much higher
  5293. CHECK (goertzel (a, 440.0, 12000, 24000) > 100.0 * goertzel (a, 880.0, 12000, 24000));
  5294. // ring with |a| = 1: carrier 440 × modulator 110 -> only 330 and 550, no 440
  5295. auto ring = sine;
  5296. ring.am = ring.ring = true;
  5297. ring.osc[2] = { lynx::Waveform::sine, -2, 0.0f, 1.0f };
  5298. const auto b = renderSynthNote (ring, 69, 24000);
  5299. CHECK (goertzel (b, 330.0, 4800, 24000) > 100.0 * goertzel (b, 440.0, 4800, 24000));
  5300. CHECK (goertzel (b, 550.0, 4800, 24000) > 100.0 * goertzel (b, 440.0, 4800, 24000));
  5301. // 3 bands: muting the high knocks down a 4 kHz note and barely touches an 80 Hz one
  5302. auto bands = sine;
  5303. bands.bandGain = { 1.0f, 1.0f, 0.0f };
  5304. auto rms = [] (const std::vector<float>& x) {
  5305. double e = 0.0;
  5306. for (std::size_t i = 4800; i < x.size(); ++i)
  5307. e += (double) x[i] * x[i];
  5308. return std::sqrt (e / (double) (x.size() - 4800));
  5309. };
  5310. CHECK (rms (renderSynthNote (bands, 107, 24000)) < 0.3 * rms (renderSynthNote (sine, 107, 24000)));
  5311. CHECK (rms (renderSynthNote (bands, 28, 24000)) > 0.85 * rms (renderSynthNote (sine, 28, 24000)));
  5312. }
  5313. TEST (love_philter_map_evaluation)
  5314. {
  5315. lynx::LovePhilterMap line { { { 0.0, 0.0 }, { 1.0, 1.0 } } };
  5316. CHECK (std::abs (lynx::lovePhilterMapAt (line, 0.5) - 0.5) < 1e-9);
  5317. CHECK (std::abs (lynx::lovePhilterMapAt (line, 1.0) - 1.0) < 1e-9);
  5318. // steps (Δx = 0): 1 until 0.4375, ramp to 0 at 0.5, then 0
  5319. lynx::LovePhilterMap steps { { { 0.0, 1.0 }, { 0.4375, 1.0 }, { 0.0625, 0.0 }, { 0.5, 0.0 } } };
  5320. CHECK (lynx::lovePhilterMapAt (steps, 0.27) == 1.0);
  5321. CHECK (lynx::lovePhilterMapAt (steps, 0.75) == 0.0);
  5322. CHECK (std::abs (lynx::lovePhilterMapAt (steps, 0.46875) - 0.5) < 1e-9);
  5323. CHECK (lynx::lovePhilterMapAt ({}, 0.3) == 1.0);
  5324. }
  5325. TEST (lofi_holds_and_quantizes)
  5326. {
  5327. auto fx = lynx::makeEffect (lynx::EffectKind::lofi);
  5328. fx->prepare (48000.0);
  5329. auto slot = lynx::makeEffectSlot (lynx::EffectKind::lofi);
  5330. slot.params[0] = 0.5f; // 128·0.125 + 1 = 17 samples
  5331. slot.params[1] = 1.0f; // 65536/2049 ≈ 32 levels
  5332. std::vector<float> l (170), r (170);
  5333. for (std::size_t i = 0; i < l.size(); ++i)
  5334. l[i] = r[i] = 0.9f * (float) std::sin (0.05 * (double) i);
  5335. const auto original = l;
  5336. fx->process (l.data(), r.data(), 170, slot.params, 120.0);
  5337. for (int i = 0; i < 17; ++i)
  5338. CHECK (l[(std::size_t) i] == l[0]);
  5339. CHECK (l[17] != l[16]);
  5340. const auto step = 2.0f * 2049.0f / 65536.0f;
  5341. for (auto v : l)
  5342. CHECK (std::abs (v / step - std::round (v / step)) < 1e-3f);
  5343. CHECK (std::abs (l[0] - original[0]) <= step);
  5344. CHECK (lynx::formatEffectParam (lynx::EffectKind::lofi, 1, 0.0f, 120.0) == "16.0 bits");
  5345. }
  5346. namespace
  5347. {
  5348. lynx::EffectParams effectorParams (int type, float x, float y)
  5349. {
  5350. auto slot = lynx::makeEffectSlot (lynx::EffectKind::effector);
  5351. slot.params[0] = (float) type / 11.0f;
  5352. slot.params[3] = x;
  5353. slot.params[4] = y;
  5354. return slot.params;
  5355. }
  5356. } // namespace
  5357. TEST (effector_algorithms)
  5358. {
  5359. CHECK (lynx::effector::typeOf (8.0f / 11.0f) == 8);
  5360. CHECK (std::string (lynx::effector::typeName (11)) == "Ring");
  5361. CHECK (std::abs (lynx::effector::rateBeats (0.5f, 0.0f) - 0.75) < 1e-9); // trunc(8·0.5 + 0.5) = 4 -> 3/4
  5362. CHECK (std::abs (lynx::effector::rateBeats (1.0f, 0.5f) - 2.0) < 1e-9); // 8 × (1 − 0.5)²
  5363. auto run = [] (const lynx::EffectParams& p, std::vector<float> in, double bpm = 120.0) {
  5364. auto fx = lynx::makeEffect (lynx::EffectKind::effector);
  5365. fx->prepare (48000.0);
  5366. fx->setSongPosition (0.0);
  5367. auto r = in;
  5368. fx->process (in.data(), r.data(), (int) in.size(), p, bpm);
  5369. return in;
  5370. };
  5371. // Distort with Y = 1 (everything goes to the cascade): 0.5 comes out 0.5 (compensation G = 0.5/F(0.5))
  5372. {
  5373. auto p = effectorParams (0, 0.5f, 1.0f);
  5374. const auto out = run (p, std::vector<float> (4800, 0.5f));
  5375. CHECK (std::abs (out.back() - 0.5f) < 1e-3f);
  5376. const auto small = run (p, std::vector<float> (4800, 0.05f));
  5377. CHECK (small.back() > 0.45f && small.back() < 0.5f); // saturates near ±0.5
  5378. }
  5379. // Ring: DC 1 becomes the carrier sine, 2^(8.7·0 + 5) = 32 Hz -> 32 cycles per second
  5380. {
  5381. auto p = effectorParams (11, 0.0f, 0.0f);
  5382. const auto out = run (p, std::vector<float> (48000, 1.0f));
  5383. int crossings = 0;
  5384. for (std::size_t i = 4800; i < out.size(); ++i)
  5385. crossings += (out[i - 1] < 0.0f) != (out[i] < 0.0f);
  5386. CHECK (crossings >= 56 && crossings <= 60); // 0.9 s × 32 Hz × 2
  5387. }
  5388. // Trancer, Y = 0: at the start of the period the gain drops to zero; X = 1 returns only at the end of the period
  5389. {
  5390. auto p = effectorParams (8, 1.0f, 0.0f);
  5391. p[5] = 1.0f; // 8/1 = 8 beats = 4 s at 120 BPM
  5392. const auto out = run (p, std::vector<float> (48000, 1.0f));
  5393. CHECK (out[100] > 0.3f); // still rising on the "and" (1% of the period = 40 ms)
  5394. CHECK (out[24000] < 0.05f); // 0.5 s: trough
  5395. }
  5396. // Reverb with Wet = 0: the dry passes intact
  5397. {
  5398. auto p = effectorParams (6, 0.5f, 0.5f);
  5399. p[2] = 0.0f;
  5400. std::vector<float> in (4800);
  5401. for (std::size_t i = 0; i < in.size(); ++i)
  5402. in[i] = (float) std::sin (0.01 * (double) i);
  5403. const auto out = run (p, in);
  5404. for (std::size_t i = 0; i < in.size(); ++i)
  5405. CHECK (std::abs (out[i] - in[i]) < 1e-6f);
  5406. }
  5407. // Delay: the echo arrives after the Mod Rate period (1/4 beat at 120 BPM = 125 ms), at level X²
  5408. {
  5409. auto p = effectorParams (5, 0.7f, 0.5f);
  5410. p[5] = 1.0f / 8.0f; // index 1 = 1/4
  5411. std::vector<float> in (12000, 0.0f);
  5412. in[0] = 1.0f;
  5413. const auto out = run (p, in);
  5414. CHECK (std::abs (out[6000] - 0.49f) < 0.02f);
  5415. CHECK (std::abs (out[3000]) < 1e-6f);
  5416. }
  5417. // Stutter: repeats the recorded start every period
  5418. {
  5419. auto p = effectorParams (9, 0.0f, 0.0f);
  5420. p[5] = 0.0f; // 1/8 beat = 62.5 ms = 3000 samples
  5421. std::vector<float> in (9000);
  5422. for (std::size_t i = 0; i < in.size(); ++i)
  5423. in[i] = (float) i / 9000.0f;
  5424. const auto out = run (p, in);
  5425. CHECK (std::abs (out[3000 + 500] - in[500]) < 0.01f); // after the crossfade, it's the start again
  5426. CHECK (std::abs (out[1000] - in[1000]) < 1e-6f);
  5427. }
  5428. }
  5429. TEST (crossover_linkwitz_riley_sums_flat)
  5430. {
  5431. // low band + high band of the same crossover: sum with magnitude 1 (Linkwitz-Riley; for 12 and 36 dB the HP is inverted)
  5432. for (const auto combo : { 1, 3, 4, 5, 6, 7 })
  5433. for (const auto testHz : { 200.0, 1000.0, 5000.0 })
  5434. {
  5435. auto run = [&] (bool high) {
  5436. auto fx = lynx::makeEffect (lynx::EffectKind::crossover);
  5437. fx->prepare (48000.0);
  5438. auto slot = lynx::makeEffectSlot (lynx::EffectKind::crossover);
  5439. const auto value = (float) (std::log2 (1000.0 / 30.0) / 9.0589);
  5440. slot.params[high ? 0 : 3] = 1.0f;
  5441. slot.params[high ? 1 : 4] = value;
  5442. slot.params[high ? 2 : 5] = (float) combo / 7.0f;
  5443. std::vector<float> l (24000), r (24000);
  5444. for (std::size_t i = 0; i < l.size(); ++i)
  5445. l[i] = r[i] = (float) std::sin (2.0 * 3.141592653589793 * testHz * (double) i / 48000.0);
  5446. fx->process (l.data(), r.data(), 24000, slot.params, 120.0);
  5447. return l;
  5448. };
  5449. const auto low = run (false), high = run (true);
  5450. double peak = 0.0;
  5451. for (std::size_t i = 12000; i < low.size(); ++i)
  5452. peak = std::max (peak, (double) std::abs (low[i] + high[i]));
  5453. CHECK (std::abs (peak - 1.0) < 0.02);
  5454. }
  5455. // 96 dB/oct: two octaves above the cutoff the low-pass is already below −150 dB... in practice, very low
  5456. auto fx = lynx::makeEffect (lynx::EffectKind::crossover);
  5457. fx->prepare (48000.0);
  5458. auto slot = lynx::makeEffectSlot (lynx::EffectKind::crossover);
  5459. slot.params[3] = 1.0f;
  5460. slot.params[4] = (float) (std::log2 (500.0 / 30.0) / 9.0589);
  5461. slot.params[5] = 1.0f;
  5462. std::vector<float> l (24000), r (24000);
  5463. for (std::size_t i = 0; i < l.size(); ++i)
  5464. l[i] = r[i] = (float) std::sin (2.0 * 3.141592653589793 * 2000.0 * (double) i / 48000.0);
  5465. fx->process (l.data(), r.data(), 24000, slot.params, 120.0);
  5466. float peak = 0.0f;
  5467. for (std::size_t i = 12000; i < l.size(); ++i)
  5468. peak = std::max (peak, std::abs (l[i]));
  5469. CHECK (peak < 1e-4f);
  5470. }
  5471. TEST (rhythm_gain_and_autopan)
  5472. {
  5473. // 1-beat gate: 1 → 0 at 0.5 beat (linear) → 1 at the end; at 120 BPM 1 beat = 24000 samples at 48 kHz
  5474. auto slot = lynx::makeEffectSlot (lynx::EffectKind::rhythmGain);
  5475. slot.params[0] = 1.0f / 64.0f;
  5476. slot.params[1] = 3.0f / 12.0f;
  5477. const float pts[3][3] = { { 0.0f, 1.0f, 0.5f }, { 0.5f, 0.0f, 0.5f }, { 1.0f, 1.0f, 0.5f } };
  5478. for (int k = 0; k < 3; ++k)
  5479. for (int j = 0; j < 3; ++j)
  5480. slot.params[(std::size_t) (2 + 3 * k + j)] = pts[k][j];
  5481. auto fx = lynx::makeEffect (lynx::EffectKind::rhythmGain);
  5482. fx->prepare (48000.0);
  5483. fx->setSongPosition (960.0 * 2.0); // beat 2: phase 0
  5484. std::vector<float> l (24000, 1.0f), r (24000, 1.0f);
  5485. fx->process (l.data(), r.data(), 24000, slot.params, 120.0);
  5486. CHECK (std::abs (l[0] - 1.0f) < 1e-3f);
  5487. CHECK (std::abs (l[6000] - 0.5f) < 1e-3f);
  5488. CHECK (l[12000] < 1e-3f);
  5489. CHECK (std::abs (l[18000] - 0.5f) < 1e-3f);
  5490. // positive tension: changes quickly at the start of the segment
  5491. CHECK (lynx::effectInfo (lynx::EffectKind::rhythmGain).params.size() == 39);
  5492. // auto-pan: 1-beat period, full depth; start phase 0.75 -> W = 0 rising (pan centered)
  5493. auto pan = lynx::makeEffectSlot (lynx::EffectKind::autoPan);
  5494. pan.params[0] = 1.0f / 64.0f;
  5495. pan.params[1] = 1.0f;
  5496. auto ap = lynx::makeEffect (lynx::EffectKind::autoPan);
  5497. ap->prepare (48000.0);
  5498. ap->setSongPosition (0.0);
  5499. std::vector<float> a (24000, 1.0f), b (24000, 1.0f);
  5500. ap->process (a.data(), b.data(), 24000, pan.params, 120.0);
  5501. CHECK (std::abs (a[0] - 1.0f) < 1e-3f && std::abs (b[0] - 1.0f) < 1e-3f);
  5502. CHECK (a[6000] < 0.01f && std::abs (b[6000] - 1.0f) < 1e-3f); // ¼ cycle later: fully right
  5503. }
  5504. TEST (vocoder_side_inputs_split_modulator_and_carrier)
  5505. {
  5506. // "Side inputs" mode (Vocodex): modulator from side input 1 (noise in the first half), carrier from side input 2
  5507. // (saw), own input silent → the output follows the side modulator; with input 1 silent, the output is silent
  5508. auto slot = lynx::makeEffectSlot (lynx::EffectKind::vocoder);
  5509. slot.params[16] = 1.0f;
  5510. auto fx = lynx::makeEffect (lynx::EffectKind::vocoder);
  5511. fx->prepare (48000.0);
  5512. std::vector<float> modL (48000), car (48000), l (48000, 0.0f), r (48000, 0.0f);
  5513. std::uint32_t seed = 1;
  5514. for (std::size_t i = 0; i < modL.size(); ++i)
  5515. {
  5516. seed = seed * 1664525u + 1013904223u;
  5517. modL[i] = i < 24000 ? ((float) (seed >> 8) / 16777216.0f - 0.5f) : 0.0f;
  5518. car[i] = (float) (2.0 * std::fmod ((double) i * 220.0 / 48000.0, 1.0) - 1.0) * 0.5f;
  5519. }
  5520. for (int done = 0; done < 48000; done += 480)
  5521. {
  5522. fx->setSidechain (modL.data() + done, modL.data() + done);
  5523. fx->setSidechain2 (car.data() + done, car.data() + done);
  5524. fx->process (l.data() + done, r.data() + done, 480, slot.params, 120.0);
  5525. }
  5526. auto rms = [&] (int from, int to) {
  5527. double e = 0.0;
  5528. for (int i = from; i < to; ++i)
  5529. e += (double) l[(std::size_t) i] * l[(std::size_t) i];
  5530. return std::sqrt (e / (to - from));
  5531. };
  5532. CHECK (rms (6000, 20000) > 0.01);
  5533. CHECK (rms (40000, 48000) < 0.1 * rms (6000, 20000));
  5534. // the side modulator configured but silent: nothing comes out (the own input is not used instead)
  5535. std::vector<float> a (4800, 0.3f), b (4800, 0.3f);
  5536. for (int done = 0; done < 4800; done += 480)
  5537. {
  5538. fx->setSidechain (nullptr, nullptr);
  5539. fx->setSidechain2 (car.data() + done, car.data() + done);
  5540. fx->process (a.data() + done, b.data() + done, 480, slot.params, 120.0);
  5541. }
  5542. CHECK (std::abs (a[4700]) < 0.05f);
  5543. }
  5544. TEST (vocoder_follows_modulator)
  5545. {
  5546. // modulator (L) plays only in the first half; carrier (R) is a continuous saw: the wet follows the modulator
  5547. auto slot = lynx::makeEffectSlot (lynx::EffectKind::vocoder);
  5548. auto fx = lynx::makeEffect (lynx::EffectKind::vocoder);
  5549. fx->prepare (48000.0);
  5550. std::vector<float> l (48000), r (48000);
  5551. std::uint32_t seed = 1;
  5552. for (std::size_t i = 0; i < l.size(); ++i)
  5553. {
  5554. seed = seed * 1664525u + 1013904223u;
  5555. l[i] = i < 24000 ? ((float) (seed >> 8) / 16777216.0f - 0.5f) : 0.0f;
  5556. r[i] = (float) (2.0 * std::fmod ((double) i * 220.0 / 48000.0, 1.0) - 1.0) * 0.5f;
  5557. }
  5558. fx->process (l.data(), r.data(), 48000, slot.params, 120.0);
  5559. auto rms = [&] (int from, int to) {
  5560. double e = 0.0;
  5561. for (int i = from; i < to; ++i)
  5562. e += (double) l[(std::size_t) i] * l[(std::size_t) i];
  5563. return std::sqrt (e / (to - from));
  5564. };
  5565. CHECK (rms (6000, 20000) > 0.01);
  5566. CHECK (rms (40000, 48000) < 0.1 * rms (6000, 20000)); // modulator silent: the vocoder fades out (100 ms release)
  5567. for (std::size_t i = 0; i < l.size(); ++i)
  5568. CHECK (l[i] == r[i] && std::isfinite (l[i])); // mono output
  5569. }
  5570. // Vocodex (REA_rodada35): a synthetic state in FL's layout (version 8, 13 maps, 20 parameters, the 0x24 block,
  5571. // 13 map-axis bytes, tone, notes)
  5572. struct VocodexSpec
  5573. {
  5574. // Wet, SG, ModVol, ModHP, CarVol, CarLP, CarNoise, CarNoiseHP, Hold, Att, AttCurve, Rel, RelCurve, Freeze, BW,
  5575. // Flatness, ModBW, ModPitch, FatPitch, FatPan
  5576. 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 };
  5577. std::array<std::int32_t, 7> block { 0, 2, 0, 24, 1, 1, 2 }; // hold mode, order, unison, bands, draft field, ModIn, CarIn
  5578. 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
  5579. std::vector<std::uint8_t> bytes() const
  5580. {
  5581. std::vector<std::uint8_t> d;
  5582. 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); };
  5583. auto i32 = [&] (std::int32_t v) { put (&v, 4); };
  5584. i32 (8 << 16);
  5585. for (int m = 0; m < 13; ++m)
  5586. {
  5587. const double y0 = m == 8 ? 0.1429 : (m == 3 || m == 4 || m == 11) ? 0.0 : 0.5;
  5588. const double y1 = m == 8 ? 0.9918 : y0;
  5589. i32 (1), i32 (0), i32 (2);
  5590. for (const auto& [dx, y] : { std::pair { 0.0, y0 }, std::pair { 1.0, y1 } })
  5591. {
  5592. put (&dx, 8), put (&y, 8);
  5593. i32 (0), i32 (0); // tension (f32 0), flags
  5594. }
  5595. const std::array<std::uint8_t, 20> tail {};
  5596. put (tail.data(), tail.size());
  5597. }
  5598. for (auto v : params)
  5599. i32 (v);
  5600. for (auto v : block)
  5601. i32 (v);
  5602. put (flags.data(), flags.size());
  5603. const std::array<std::uint8_t, 13> axis {};
  5604. put (axis.data(), axis.size());
  5605. d.push_back (3), put ("PWM", 3);
  5606. i32 (1), i32 (60);
  5607. return d;
  5608. }
  5609. };
  5610. std::vector<float> vocodexSaw (int n, double hz, double fs)
  5611. {
  5612. std::vector<float> out ((std::size_t) n);
  5613. for (int i = 0; i < n; ++i)
  5614. out[(std::size_t) i] = (float) (2.0 * std::fmod (i * hz / fs, 1.0) - 1.0) * 0.4f;
  5615. return out;
  5616. }
  5617. TEST (vocodex_state_parse_and_conversion)
  5618. {
  5619. VocodexSpec spec;
  5620. spec.block = { 1, 3, 2, 40, 0, 2, 1 };
  5621. spec.params[17] = -1200;
  5622. const auto raw = spec.bytes();
  5623. const auto s = lynx::vocodex::parseState (raw);
  5624. CHECK (s.has_value());
  5625. if (! s)
  5626. return;
  5627. CHECK (s->version == 8 && s->holdMode == 1 && s->order == 3 && s->unison == 2 && s->bands == 40);
  5628. CHECK (s->draft() && s->modIn == 2 && s->carIn == 1 && s->minTimes && s->subtractiveGate && ! s->lrEncoding);
  5629. CHECK (s->params[lynx::vocodex::modPitch] == -1200 && s->params[lynx::vocodex::carLp] == 65536);
  5630. 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);
  5631. CHECK (s->tone == "PWM" && s->notes.size() == 1 && s->notes[0] == 60);
  5632. const auto v = lynx::vocodexEffect (raw);
  5633. CHECK (v && v->slot.kind == lynx::EffectKind::vocodex && v->slot.state == raw && v->modIn == 2 && v->carIn == 1 && v->notes.empty());
  5634. spec.block[6] = -1; // internal synth carrier: not available yet → reported
  5635. spec.params[1] = 150; // Soundgoodizer stage
  5636. const auto w = lynx::vocodexEffect (spec.bytes());
  5637. CHECK (w && w->carIn == -1 && w->notes.size() == 2);
  5638. CHECK (! lynx::vocodex::parseState ({ 1, 2, 3 }).has_value());
  5639. }
  5640. TEST (vocodex_vocodes_side_inputs)
  5641. {
  5642. // modulator on side input 1 (noise in the first half), carrier on side input 2 (saw), own input silent: the
  5643. // output follows the modulator's envelope and fades after it stops
  5644. VocodexSpec spec;
  5645. lynx::EffectSlot slot = lynx::makeEffectSlot (lynx::EffectKind::vocodex);
  5646. slot.state = spec.bytes();
  5647. auto fx = lynx::makeEffect (slot);
  5648. fx->prepare (48000.0);
  5649. CHECK (fx->latencySamples() == 0);
  5650. const auto car = vocodexSaw (48000, 110.0, 48000.0);
  5651. std::vector<float> mod (48000), l (48000, 0.0f), r (48000, 0.0f);
  5652. std::uint32_t seed = 7;
  5653. for (std::size_t i = 0; i < mod.size(); ++i)
  5654. {
  5655. seed = seed * 1664525u + 1013904223u;
  5656. mod[i] = i < 24000 ? ((float) (seed >> 8) / 16777216.0f - 0.5f) : 0.0f;
  5657. }
  5658. for (int done = 0; done < 48000; done += 512)
  5659. {
  5660. const int n = std::min (512, 48000 - done);
  5661. fx->setSidechain (mod.data() + done, mod.data() + done);
  5662. fx->setSidechain2 (car.data() + done, car.data() + done);
  5663. fx->process (l.data() + done, r.data() + done, n, slot.params, 120.0);
  5664. }
  5665. auto rms = [&] (int from, int to) {
  5666. double e = 0.0;
  5667. for (int i = from; i < to; ++i)
  5668. e += (double) l[(std::size_t) i] * l[(std::size_t) i] + (double) r[(std::size_t) i] * r[(std::size_t) i];
  5669. return std::sqrt (e / (2.0 * (to - from)));
  5670. };
  5671. CHECK (rms (6000, 20000) > 0.02);
  5672. CHECK (rms (40000, 48000) < 0.01 * rms (6000, 20000));
  5673. CHECK (std::abs (l[0]) < 1e-6f); // the gains ramp up from 0 when the plugin is created
  5674. }
  5675. TEST (vocodex_draft_latency_and_pass_through)
  5676. {
  5677. // carrier pass-through at unit gain only (Wet 0): after a Flush the gains snap, so the output is the carrier,
  5678. // 16 samples late in Draft
  5679. VocodexSpec spec;
  5680. spec.params[0] = 0; // Wet
  5681. spec.params[4] = 12800; // CarVol = 1
  5682. for (const int draftField : { 1, 0 })
  5683. {
  5684. spec.block[4] = draftField;
  5685. const auto s = lynx::vocodex::parseState (spec.bytes());
  5686. CHECK (s.has_value());
  5687. lynx::vocodex::Engine engine;
  5688. engine.prepare (*s, 44100.0, 300, s->draft());
  5689. CHECK (engine.latencySamples() == (draftField == 0 ? 16 : 0));
  5690. engine.flush();
  5691. const auto saw = vocodexSaw (3000, 220.0, 44100.0);
  5692. std::vector<float> car (6000), mod (6000, 0.0f), out (6000);
  5693. for (int i = 0; i < 3000; ++i)
  5694. car[(std::size_t) (2 * i)] = car[(std::size_t) (2 * i + 1)] = saw[(std::size_t) i];
  5695. static constexpr int sizes[] { 300, 37, 128, 1, 255, 16, 299 }; // uneven blocks exercise the FIFOs
  5696. for (int done = 0, k = 0, n = 0; done < 3000; done += n, ++k)
  5697. {
  5698. n = std::min (sizes[k % 7], 3000 - done);
  5699. engine.process (mod.data() + 2 * done, mod.data() + 2 * done, car.data() + 2 * done, out.data() + 2 * done, n);
  5700. }
  5701. const int lag = engine.latencySamples();
  5702. auto maxError = 0.0f;
  5703. for (int i = lag; i < 3000; ++i)
  5704. maxError = std::max (maxError, std::abs (out[(std::size_t) (2 * i)] - saw[(std::size_t) (i - lag)]));
  5705. CHECK (maxError == 0.0f);
  5706. }
  5707. }
  5708. TEST (vocodex_soundgoodizer_stage_limits_the_output)
  5709. {
  5710. // SG > 0: the vocoder's output (loud here: Wet 2000) goes through the Soundgoodizer approximation, whose
  5711. // master limiter keeps the peaks under −0.2 dBFS; its look-ahead is reported as latency
  5712. VocodexSpec spec;
  5713. spec.params[0] = 2000;
  5714. spec.params[1] = 250;
  5715. lynx::EffectSlot slot = lynx::makeEffectSlot (lynx::EffectKind::vocodex);
  5716. slot.state = spec.bytes();
  5717. auto fx = lynx::makeEffect (slot);
  5718. fx->prepare (48000.0);
  5719. CHECK (fx->latencySamples() == 72); // 1.5 ms at 48 kHz
  5720. const auto car = vocodexSaw (48000, 110.0, 48000.0);
  5721. std::vector<float> mod (48000), l (48000, 0.0f), r (48000, 0.0f);
  5722. for (std::size_t i = 0; i < mod.size(); ++i)
  5723. mod[i] = 0.9f * (float) std::sin (0.07 * (double) i) * (float) std::sin (0.0003 * (double) i);
  5724. auto peak = 0.0f;
  5725. for (int done = 0; done < 48000; done += 512)
  5726. {
  5727. const int n = std::min (512, 48000 - done);
  5728. fx->setSidechain (mod.data() + done, mod.data() + done);
  5729. fx->setSidechain2 (car.data() + done, car.data() + done);
  5730. fx->process (l.data() + done, r.data() + done, n, slot.params, 120.0);
  5731. }
  5732. for (std::size_t i = 0; i < l.size(); ++i)
  5733. peak = std::max ({ peak, std::abs (l[i]), std::abs (r[i]) });
  5734. CHECK (peak > 0.5f && peak <= 0.9775f);
  5735. }
  5736. TEST (vocodex_tail_bypass_and_noise)
  5737. {
  5738. // both inputs silent for longer than 0.5 s + hold + release: the own input passes unchanged
  5739. VocodexSpec spec;
  5740. const auto s = lynx::vocodex::parseState (spec.bytes());
  5741. CHECK (s.has_value());
  5742. lynx::vocodex::Engine engine;
  5743. engine.prepare (*s, 44100.0, 512, false);
  5744. std::vector<float> zero (1024, 0.0f), own (1024), out (1024);
  5745. for (std::size_t i = 0; i < own.size(); ++i)
  5746. own[i] = 0.25f * (float) std::sin (0.01 * (double) i);
  5747. for (int b = 0; b < 80; ++b)
  5748. engine.process (own.data(), zero.data(), zero.data(), out.data(), 512);
  5749. CHECK (out == own);
  5750. // carrier noise: the IPP-compatible generator restarts from the same seed after a Flush
  5751. spec.params[6] = 9000;
  5752. const auto noisy = lynx::vocodex::parseState (spec.bytes());
  5753. lynx::vocodex::Engine a;
  5754. a.prepare (*noisy, 44100.0, 512, false);
  5755. std::vector<float> mod (1024), first (1024), second (1024);
  5756. for (std::size_t i = 0; i < mod.size(); ++i)
  5757. mod[i] = 0.3f * (float) std::sin (0.05 * (double) i);
  5758. a.flush();
  5759. a.process (own.data(), mod.data(), zero.data(), first.data(), 512);
  5760. a.flush();
  5761. a.process (own.data(), mod.data(), zero.data(), second.data(), 512);
  5762. CHECK (first == second);
  5763. auto energy = 0.0;
  5764. for (auto v : first)
  5765. energy += (double) v * v;
  5766. CHECK (energy > 1e-6 && std::isfinite (energy));
  5767. }
  5768. TEST (synth_spectral_oscillator)
  5769. {
  5770. // 1/n spectrum = saw: same harmonic proportions and same level as LYNX's saw
  5771. lynx::SynthSettings saw;
  5772. saw.osc[0] = { lynx::Waveform::saw, 0, 0.0f, 1.0f };
  5773. saw.cutoff = 20000.0f;
  5774. auto spectral = saw;
  5775. spectral.osc[0].wave = lynx::Waveform::spectral;
  5776. for (int n = 1; n <= lynx::SynthSettings::spectrumSize; ++n)
  5777. spectral.spectrum[(std::size_t) n - 1] = 1.0f / (float) n;
  5778. const auto a = renderSynthNote (saw, 57, 24000), b = renderSynthNote (spectral, 57, 24000); // 220 Hz
  5779. for (const auto h : { 1, 2, 3, 5 })
  5780. {
  5781. const auto ea = goertzel (a, 220.0 * h, 4800, 24000), eb = goertzel (b, 220.0 * h, 4800, 24000);
  5782. CHECK (std::abs (10.0 * std::log10 (eb / ea)) < 0.5); // within 0.5 dB
  5783. }
  5784. // odd only: no 2nd harmonic
  5785. auto odd = spectral;
  5786. for (int n = 2; n <= lynx::SynthSettings::spectrumSize; n += 2)
  5787. odd.spectrum[(std::size_t) n - 1] = 0.0f;
  5788. const auto c = renderSynthNote (odd, 57, 24000);
  5789. CHECK (goertzel (c, 440.0, 4800, 24000) < 1e-4 * goertzel (c, 220.0, 4800, 24000));
  5790. // high note: the band-limited tables don't pass Nyquist (no aliasing below the fundamental)
  5791. const auto d = renderSynthNote (spectral, 105, 24000); // 3520 Hz
  5792. CHECK (goertzel (d, 1000.0, 4800, 24000) < 1e-3 * goertzel (d, 3520.0, 4800, 24000));
  5793. }
  5794. TEST (compressor_sidechain_ducks_and_orders_mixer)
  5795. {
  5796. // the detector listens to the side input: quiet signal on the track, loud kick on the sidechain -> reduction
  5797. auto slot = lynx::makeEffectSlot (lynx::EffectKind::compressor);
  5798. slot.params[0] = 0.5f; // threshold
  5799. slot.params[1] = 1.0f; // high ratio
  5800. slot.params[4] = 0.0f;
  5801. auto run = [&] (bool withSidechain) {
  5802. auto fx = lynx::makeEffect (lynx::EffectKind::compressor);
  5803. fx->prepare (48000.0);
  5804. std::vector<float> l (4800, 0.1f), r (4800, 0.1f), kick (4800, 1.0f);
  5805. if (withSidechain)
  5806. fx->setSidechain (kick.data(), kick.data());
  5807. fx->process (l.data(), r.data(), 4800, slot.params, 120.0);
  5808. return l.back();
  5809. };
  5810. const auto alone = run (false), ducked = run (true);
  5811. CHECK (ducked < 0.5f * alone);
  5812. // order: track 5 listens to 7 via sidechain -> 7 comes before 5
  5813. auto song = lynx::Song::makeDefault();
  5814. while (song.mixer.size() < 8)
  5815. song.mixer.push_back ({});
  5816. auto comp = lynx::makeEffectSlot (lynx::EffectKind::compressor);
  5817. comp.sidechain = 7;
  5818. song.mixer[5].effects.push_back (comp);
  5819. const auto order = song.mixerOrder();
  5820. const auto at = [&] (int t) { return std::find (order.begin(), order.end(), t) - order.begin(); };
  5821. CHECK (at (7) < at (5));
  5822. }
  5823. TEST (pitch_correction_pulls_to_allowed_note)
  5824. {
  5825. auto run = [] (const lynx::EffectParams& p, double hz) {
  5826. auto fx = lynx::makeEffect (lynx::EffectKind::pitchCorrect);
  5827. fx->prepare (48000.0);
  5828. std::vector<float> l (48000 * 2), r;
  5829. for (std::size_t i = 0; i < l.size(); ++i)
  5830. l[i] = 0.5f * (float) std::sin (2.0 * 3.141592653589793 * hz * (double) i / 48000.0);
  5831. r = l;
  5832. fx->process (l.data(), r.data(), (int) l.size(), p, 120.0);
  5833. // frequency from zero crossings in the last second
  5834. int crossings = 0;
  5835. for (std::size_t i = 48000 + 1; i < l.size(); ++i)
  5836. crossings += (l[i - 1] < 0.0f) != (l[i] < 0.0f);
  5837. return crossings / 2.0;
  5838. };
  5839. auto slot = lynx::makeEffectSlot (lynx::EffectKind::pitchCorrect); // chromatic, instant, A 440
  5840. CHECK (std::abs (run (slot.params, 452.0) - 440.0) < 4.0);
  5841. for (int k = 0; k < 12; ++k)
  5842. slot.params[(std::size_t) (5 + k)] = k == 0 ? 1.0f : 0.0f; // only C
  5843. CHECK (std::abs (run (slot.params, 452.0) - 523.25) < 5.0); // C5 (523 Hz) is closer than C4 (262)
  5844. slot.params[2] = 1.0f; // turn everything off: passes unchanged
  5845. CHECK (std::abs (run (slot.params, 452.0) - 452.0) < 4.0);
  5846. }
  5847. TEST (pitch_correction_bypass_is_a_pure_delay)
  5848. {
  5849. // FL's Pitcher with Bypass All still runs its shifter: the plain path is an exact 9-sample delay, the formant
  5850. // path (256-Hann overlap-add) an exact 257-sample delay, and Gender changes nothing without a shift
  5851. // (REA_rodada30 §2.4/2.5); both are reported as latency
  5852. std::vector<float> in (48000);
  5853. for (std::size_t i = 0; i < in.size(); ++i)
  5854. in[i] = 0.5f * (float) std::sin (2.0 * 3.141592653589793 * 440.0 * (double) i / 48000.0)
  5855. + 0.2f * (float) std::sin (2.0 * 3.141592653589793 * 1234.5 * (double) i / 48000.0);
  5856. auto run = [&] (bool formant, float gender, int& latency) {
  5857. auto fx = lynx::makeEffect (lynx::EffectKind::pitchCorrect);
  5858. fx->prepare (48000.0);
  5859. auto slot = lynx::makeEffectSlot (lynx::EffectKind::pitchCorrect);
  5860. slot.params[2] = 1.0f;
  5861. slot.params[29] = formant ? 1.0f : 0.0f;
  5862. slot.params[30] = gender;
  5863. auto l = in, r = in;
  5864. for (std::size_t i = 0; i < l.size(); i += 480)
  5865. fx->process (l.data() + i, r.data() + i, 480, slot.params, 120.0);
  5866. latency = fx->latencySamples();
  5867. return l;
  5868. };
  5869. int latency = 0;
  5870. auto plain = run (false, 0.5f, latency);
  5871. CHECK (latency == 9);
  5872. auto worst = 0.0f;
  5873. for (std::size_t i = 4000; i < in.size(); ++i)
  5874. worst = std::max (worst, std::abs (plain[i] - in[i - 9]));
  5875. CHECK (worst < 1e-5f);
  5876. auto formant = run (true, 0.8f, latency);
  5877. CHECK (latency == 257);
  5878. worst = 0.0f;
  5879. for (std::size_t i = 4000; i < in.size(); ++i)
  5880. worst = std::max (worst, std::abs (formant[i] - in[i - 257]));
  5881. CHECK (worst < 1e-5f);
  5882. }
  5883. TEST (pitch_correction_formant_path)
  5884. {
  5885. // Formant on: PSOLA keeps the correction (output pitch = grain spacing) while Gender resamples the grains:
  5886. // the spectrum of a saw moves up with Gender > 0.5 and down with Gender < 0.5
  5887. auto run = [] (float gender) {
  5888. auto fx = lynx::makeEffect (lynx::EffectKind::pitchCorrect);
  5889. fx->prepare (48000.0);
  5890. auto slot = lynx::makeEffectSlot (lynx::EffectKind::pitchCorrect); // chromatic, instant, A 440
  5891. slot.params[29] = 1.0f;
  5892. slot.params[30] = gender;
  5893. std::vector<float> l (48000 * 2), r;
  5894. for (std::size_t i = 0; i < l.size(); ++i)
  5895. l[i] = 0.5f * (float) (2.0 * std::fmod (452.0 * (double) i / 48000.0, 1.0) - 1.0); // saw
  5896. r = l;
  5897. fx->process (l.data(), r.data(), (int) l.size(), slot.params, 120.0);
  5898. l.erase (l.begin(), l.begin() + 48000);
  5899. return l;
  5900. };
  5901. // period by autocorrelation (lags 60…160 = 300…800 Hz)
  5902. auto pitch = [] (const std::vector<float>& x) {
  5903. auto best = 0.0, bestLag = 0.0;
  5904. for (int lag = 60; lag <= 160; ++lag)
  5905. {
  5906. auto sum = 0.0;
  5907. for (std::size_t i = 0; i + (std::size_t) lag < x.size(); ++i)
  5908. sum += (double) x[i] * x[i + (std::size_t) lag];
  5909. if (sum > best)
  5910. {
  5911. best = sum;
  5912. bestLag = lag;
  5913. }
  5914. }
  5915. return 48000.0 / bestLag;
  5916. };
  5917. // brightness: energy of the first difference relative to the energy
  5918. auto brightness = [] (const std::vector<float>& x) {
  5919. auto e = 0.0, d = 0.0;
  5920. for (std::size_t i = 1; i < x.size(); ++i)
  5921. {
  5922. e += (double) x[i] * x[i];
  5923. d += (double) (x[i] - x[i - 1]) * (x[i] - x[i - 1]);
  5924. }
  5925. return d / e;
  5926. };
  5927. const auto neutral = run (0.5f), up = run (0.8f), down = run (0.3f);
  5928. CHECK (std::abs (pitch (neutral) - 440.0) < 5.0); // 452 Hz pulled to A
  5929. CHECK (std::abs (pitch (up) - 440.0) < 5.0); // Gender does not move the pitch
  5930. CHECK (brightness (up) > 1.5 * brightness (neutral));
  5931. CHECK (brightness (down) < 0.7 * brightness (neutral));
  5932. }
  5933. TEST (parallel_container_inner_links_modulate)
  5934. {
  5935. // one branch: utility (gain) + a control-only auto-pan LFO (1 beat); the link maps the LFO 0..1 onto the
  5936. // utility gain −24 dB..0 dB, so the level must swing; without the link it stays put
  5937. auto container = lynx::makeEffectSlot (lynx::EffectKind::parallel);
  5938. auto gain = lynx::makeEffectSlot (lynx::EffectKind::utility);
  5939. auto lfo = lynx::makeEffectSlot (lynx::EffectKind::autoPan);
  5940. lfo.params[0] = 1.0f / 64.0f;
  5941. lfo.params[4] = 1.0f; // control only: audio passes unchanged
  5942. container.branches.push_back ({ { gain, lfo }, 1.0f });
  5943. lynx::EffectLink link;
  5944. link.source = 1;
  5945. link.param = 0;
  5946. for (int i = 0; i <= 32; ++i)
  5947. link.curve.push_back (0.5f * (float) i / 32.0f);
  5948. auto levels = [&] (bool linked) {
  5949. auto slot = container;
  5950. if (linked)
  5951. slot.links.push_back (link);
  5952. auto fx = lynx::makeParallelEffect (slot);
  5953. fx->prepare (48000.0);
  5954. float lo = 1e9f, hi = 0.0f;
  5955. for (int block = 0; block < 94; ++block) // ~1 beat at 120 BPM in 256-sample blocks
  5956. {
  5957. std::vector<float> l (256, 1.0f), r (256, 1.0f);
  5958. fx->setSongPosition (block * 256.0 / 48000.0 * 2.0 * 960.0); // the engine positions every block
  5959. fx->process (l.data(), r.data(), 256, slot.params, 120.0);
  5960. lo = std::min (lo, std::abs (l[128]));
  5961. hi = std::max (hi, std::abs (l[128]));
  5962. }
  5963. return std::pair { lo, hi };
  5964. };
  5965. const auto [lo, hi] = levels (true);
  5966. CHECK (hi > 0.8f && lo < 0.12f);
  5967. const auto [lo0, hi0] = levels (false);
  5968. CHECK (std::abs (hi0 - lo0) < 1e-4f);
  5969. }
  5970. TEST (synth_spectrum_decays_per_harmonic)
  5971. {
  5972. // 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
  5973. lynx::SynthSettings s;
  5974. s.osc[0] = { lynx::Waveform::spectral, 0, 0.0f, 1.0f };
  5975. s.cutoff = 20000.0f;
  5976. s.decay = 0.0f;
  5977. s.sustain = 1.0f;
  5978. s.spectrum[0] = 1.0f;
  5979. s.spectrum[2] = 1.0f;
  5980. s.spectrumDecay[2] = 0.2f;
  5981. const auto x = renderSynthNote (s, 57, 48000); // 220 Hz, 48 kHz, 1 s
  5982. // equal 2400-sample windows (Goertzel power grows with the window): 10…60 ms and 500…550 ms
  5983. const auto early1 = goertzel (x, 220.0, 480, 2880), early3 = goertzel (x, 660.0, 480, 2880);
  5984. const auto late1 = goertzel (x, 220.0, 24000, 26400), late3 = goertzel (x, 660.0, 24000, 26400);
  5985. CHECK (early3 > 0.05 * early1); // ≈ −9 dB on average over 10…60 ms
  5986. CHECK (late3 < 1e-4 * late1);
  5987. CHECK (std::abs (10.0 * std::log10 (late1 / early1)) < 1.0); // the 1st holds
  5988. // without per-harmonic decay the spectrum stays as it is
  5989. s.spectrumDecay[2] = 0.0f;
  5990. const auto y = renderSynthNote (s, 57, 48000);
  5991. CHECK (std::abs (10.0 * std::log10 (goertzel (y, 660.0, 24000, 26400) / goertzel (y, 660.0, 480, 2880))) < 1.0);
  5992. }
  5993. TEST (worker_pool_runs_each_index_once_per_round)
  5994. {
  5995. // rounds of alternating sizes (a stalled worker must never run an index of a later round), each index
  5996. // exactly once, and nothing running after parallelFor returns
  5997. lynx::WorkerPool pool (3);
  5998. std::vector<std::atomic<int>> hits (64);
  5999. std::atomic<int> round { 0 }, outside { 0 }, wrongRound { 0 };
  6000. std::atomic<bool> open { false };
  6001. int failures = 0;
  6002. for (int r = 1; r <= 20000; ++r)
  6003. {
  6004. const auto n = (r % 3 == 0) ? 64 : (r % 3 == 1 ? 2 : 9);
  6005. for (int i = 0; i < n; ++i)
  6006. hits[(std::size_t) i].store (0);
  6007. round.store (r);
  6008. open.store (true);
  6009. auto fn = [&, r] (int i) {
  6010. if (! open.load())
  6011. outside.fetch_add (1);
  6012. if (round.load() != r)
  6013. wrongRound.fetch_add (1);
  6014. hits[(std::size_t) i].fetch_add (1);
  6015. };
  6016. pool.parallelFor (n, fn);
  6017. open.store (false);
  6018. for (int i = 0; i < n; ++i)
  6019. failures += hits[(std::size_t) i].load() != 1;
  6020. }
  6021. CHECK (failures == 0);
  6022. CHECK (outside.load() == 0);
  6023. CHECK (wrongRound.load() == 0);
  6024. }
  6025. TEST (ladder_filter_follows_toxic_law)
  6026. {
  6027. // the 4-pole ladder: −24 dB/oct far above the cutoff (vs −12 for the SVF), resonance raises the corner, and
  6028. // the input saturation keeps it bounded even at full resonance
  6029. auto level = [] (bool ladder, float res, double hz) {
  6030. lynx::SynthSettings s;
  6031. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  6032. s.cutoff = 500.0f;
  6033. s.resonance = res;
  6034. s.ladder = ladder;
  6035. const auto key = (int) std::lround (69.0 + 12.0 * std::log2 (hz / 440.0));
  6036. const auto x = renderSynthNote (s, key, 48000);
  6037. double e = 0.0, peak = 0.0;
  6038. for (std::size_t i = 24000; i < 48000; ++i)
  6039. e += (double) x[i] * x[i], peak = std::max (peak, (double) std::abs (x[i]));
  6040. return std::pair { std::sqrt (e / 24000.0), peak };
  6041. };
  6042. 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
  6043. const auto svfHigh = level (false, 0.0f, 3520.0).first / level (false, 0.0f, 110.0).first;
  6044. CHECK (20.0 * std::log10 (high / low) < -55.0); // ≈ 24 dB/oct over ~2.8 octaves
  6045. CHECK (20.0 * std::log10 (svfHigh) > -45.0); // the 2-pole SVF falls much less
  6046. const auto corner0 = level (true, 0.0f, 523.25).first, corner1 = level (true, 0.6f, 523.25).first;
  6047. CHECK (corner1 > 1.5 * corner0); // resonance boosts near the cutoff
  6048. const auto loud = level (true, 1.0f, 523.25);
  6049. CHECK (std::isfinite (loud.first) && loud.second < 20.0); // self-oscillation stays bounded
  6050. }
  6051. TEST (synth_spectrum_settles_on_target)
  6052. {
  6053. // ALT DECAY-like morph: harmonic 3 starts at 1 and settles on 0.25 (−12 dB) instead of fading out
  6054. lynx::SynthSettings s;
  6055. s.osc[0] = { lynx::Waveform::spectral, 0, 0.0f, 1.0f };
  6056. s.cutoff = 20000.0f;
  6057. s.decay = 0.0f;
  6058. s.sustain = 1.0f;
  6059. s.spectrum[0] = 1.0f;
  6060. s.spectrum[2] = 1.0f;
  6061. s.spectrumDecay[2] = 0.1f;
  6062. s.spectrumTarget[2] = 0.25f;
  6063. const auto x = renderSynthNote (s, 57, 48000);
  6064. const auto ratio = goertzel (x, 660.0, 24000, 26400) / goertzel (x, 220.0, 24000, 26400);
  6065. CHECK (std::abs (10.0 * std::log10 (ratio) - 20.0 * std::log10 (0.25)) < 1.0);
  6066. }
  6067. TEST (kepler_chorus_is_stereo_and_keeps_level)
  6068. {
  6069. // chorus I on a sine: L and R differ (opposite LFO phases), the level stays close to the dry one
  6070. lynx::SynthSettings s;
  6071. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  6072. s.cutoff = 20000.0f;
  6073. auto dry = s;
  6074. s.chorus = 1;
  6075. auto render = [] (const lynx::SynthSettings& set) {
  6076. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  6077. inst->prepare (48000.0);
  6078. lynx::ChannelDef c;
  6079. c.synth = set;
  6080. inst->configure (c, 120.0);
  6081. inst->noteOn (69, 1.0f);
  6082. std::vector<float> l (96000), r (96000);
  6083. inst->render (l.data(), r.data(), 96000, 1.0f, 1.0f);
  6084. return std::pair { l, r };
  6085. };
  6086. const auto [l, r] = render (s);
  6087. const auto [dl, dr] = render (dry);
  6088. double diff = 0.0, e = 0.0, ed = 0.0;
  6089. for (std::size_t i = 48000; i < 96000; ++i)
  6090. diff += std::abs (l[i] - r[i]), e += (double) l[i] * l[i], ed += (double) dl[i] * dl[i];
  6091. CHECK (diff / 48000.0 > 1e-3);
  6092. CHECK (std::abs (10.0 * std::log10 (e / ed)) < 3.0);
  6093. }
  6094. TEST (fl_pan_law_matches_love_philter_dll)
  6095. {
  6096. // values printed by the installation's Love Philter pan routine for vol 0.8 (REA_rodada27 §3.1, lppan.txt)
  6097. struct Row { double pan, ll, rr, rl, lr; };
  6098. const Row rows[] = { { -1.5, 0.239782482, -0.432956904, 0.805467963, 0.0 }, { -0.5, 0.805467963, 0.432956904, 0.239782482, 0.0 },
  6099. { 0.0, 0.8, 0.8, 0.0, 0.0 }, { 0.75, 0.220719516, 0.707889318, 0.0, 0.401742548 },
  6100. { 1.0, 0.0, 0.565685451, 0.0, 0.565685451 } };
  6101. for (const auto& row : rows)
  6102. {
  6103. float ll, rl, lr, rr;
  6104. lynx::flPanMatrix (row.pan, ll, rl, lr, rr);
  6105. CHECK (std::abs (0.8 * ll - row.ll) < 1e-6 && std::abs (0.8 * rr - row.rr) < 1e-6);
  6106. CHECK (std::abs (0.8 * rl - row.rl) < 1e-6 && std::abs (0.8 * lr - row.lr) < 1e-6);
  6107. }
  6108. }
  6109. TEST (effector_units_match_fl_dll_impulse_responses)
  6110. {
  6111. // impulse responses of the installation's Effector engine (REA_rodada27 §2, eu_<kind>_<X>_<Y>.txt; LFO off,
  6112. // wet 1, blocks of 256): samples 1, 10, 100 and 1000
  6113. struct Case { int kind; float x, y; double v[4]; };
  6114. const Case cases[] = { { 3, 0.5f, 0.5f, { -0.603055418, 0.159798339, -0.0055043716, -1.06241259e-06 } },
  6115. { 4, 0.2f, 0.5f, { 4.68885949e-26, 4.20032622e-19, 0.000487731799, 2.27715846e-09 } },
  6116. { 4, 0.8f, 0.3f, { -0.293783396, -0.0306414738, -0.00068128563, 1.358577e-15 } } };
  6117. for (const auto& c : cases)
  6118. {
  6119. auto fx = lynx::effector::make();
  6120. fx->prepare (44100.0);
  6121. lynx::EffectParams p {};
  6122. p[lynx::effector::type] = c.kind / 11.0f;
  6123. p[lynx::effector::wetMix] = 1.0f;
  6124. p[lynx::effector::paramX] = c.x;
  6125. p[lynx::effector::paramY] = c.y;
  6126. p[lynx::effector::modX] = p[lynx::effector::modY] = 0.5f;
  6127. p[lynx::effector::modRate] = 0.5f;
  6128. p[lynx::effector::inputLevel] = 1.0f;
  6129. p[lynx::effector::outputGain] = 0.5f;
  6130. std::vector<float> l (1024, 0.0f), r (1024, 0.0f);
  6131. l[0] = r[0] = 1.0f;
  6132. for (int d = 0; d < 1024; d += 256)
  6133. fx->process (l.data() + d, r.data() + d, 256, p, 120.0);
  6134. const int at[4] { 1, 10, 100, 1000 };
  6135. for (int k = 0; k < 4; ++k)
  6136. CHECK (std::abs (l[(std::size_t) at[k]] - c.v[k]) < 2e-7 + 1e-5 * std::abs (c.v[k]));
  6137. }
  6138. }
  6139. TEST (harmless_stereo_tremolo_and_vibrato)
  6140. {
  6141. // tremolo d = 1: each side swings between 0 and 1.25 at 5 Hz, L and R in anti-phase; vibrato modulates the pitch
  6142. lynx::SynthSettings s;
  6143. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  6144. s.cutoff = 20000.0f;
  6145. s.stereoTremolo = 1.0f;
  6146. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  6147. inst->prepare (48000.0);
  6148. lynx::ChannelDef c;
  6149. c.synth = s;
  6150. inst->configure (c, 120.0);
  6151. inst->noteOn (69, 1.0f);
  6152. std::vector<float> l (48000), r (48000);
  6153. inst->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
  6154. 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; };
  6155. // θ = 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
  6156. CHECK (envelope (l, 19200) > 4.0f * envelope (r, 19200));
  6157. CHECK (envelope (r, 24000) > 4.0f * envelope (l, 24000));
  6158. // vibrato 100 cents: zero crossings vary in spacing; without it they are regular
  6159. auto crossingsSpread = [] (float cents) {
  6160. lynx::SynthSettings v;
  6161. v.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  6162. v.cutoff = 20000.0f;
  6163. v.vibratoCents = cents;
  6164. const auto x = renderSynthNote (v, 69, 48000);
  6165. std::vector<int> zc;
  6166. for (int i = 24000; i < 48000; ++i)
  6167. if (x[(std::size_t) i - 1] < 0.0f && x[(std::size_t) i] >= 0.0f)
  6168. zc.push_back (i);
  6169. int lo = 1 << 30, hi = 0;
  6170. for (std::size_t k = 1; k < zc.size(); ++k)
  6171. lo = std::min (lo, zc[k] - zc[k - 1]), hi = std::max (hi, zc[k] - zc[k - 1]);
  6172. return hi - lo;
  6173. };
  6174. CHECK (crossingsSpread (0.0f) <= 1);
  6175. CHECK (crossingsSpread (100.0f) >= 4);
  6176. }
  6177. TEST (physical_models_sound_and_end)
  6178. {
  6179. // Sakura: a plucked string with default-ish parameters rings and fades after the release; Drumpad: a hit decays
  6180. for (const auto kind : { lynx::InstrumentKind::sakura, lynx::InstrumentKind::drumpad })
  6181. {
  6182. lynx::ChannelDef c;
  6183. c.instrument = kind;
  6184. if (kind == lynx::InstrumentKind::sakura)
  6185. {
  6186. c.modelParams.assign (146, 0.5f);
  6187. c.modelParams[111] = 3.0f; // both strings at the note
  6188. c.modelParams[113] = 0.5f; // octave ×1
  6189. c.modelParams[6] = c.modelParams[11] = 0.99f; // long decay
  6190. c.modelParams[31] = 0.0f; // noise exciter
  6191. }
  6192. else
  6193. {
  6194. c.modelParams.assign (38, 0.5f);
  6195. c.modelParams[0] = 1.0f; // retrigger noise
  6196. c.modelParams[30] = c.modelParams[31] = c.modelParams[32] = c.modelParams[33] = 0.0f; // no velocity morph
  6197. }
  6198. auto inst = lynx::makeInstrument (kind);
  6199. inst->prepare (48000.0);
  6200. inst->configure (c, 120.0);
  6201. inst->noteOn (60, 0.8f);
  6202. std::vector<float> l (48000), r (48000);
  6203. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  6204. inst->noteOff (60);
  6205. inst->render (l.data() + 24000, r.data() + 24000, 24000, 1.0f, 1.0f);
  6206. double early = 0.0;
  6207. for (int i = 0; i < 12000; ++i)
  6208. early += (double) l[(std::size_t) i] * l[(std::size_t) i];
  6209. CHECK (early > 1e-6);
  6210. CHECK (std::all_of (l.begin(), l.end(), [] (float v) { return std::isfinite (v); }));
  6211. std::vector<float> tail (480000), tailR (480000);
  6212. inst->render (tail.data(), tailR.data(), 480000, 1.0f, 1.0f); // 10 s later the voice has ended
  6213. CHECK (inst->idle());
  6214. }
  6215. }
  6216. TEST (squeeze_matches_fl_model)
  6217. {
  6218. // Fruity Squeeze: Steps quantises, Mix 0 passes the input dry ×Volume/127; a silent block is left untouched
  6219. auto slot = lynx::makeEffectSlot (lynx::EffectKind::squeeze);
  6220. slot.params[1] = 0.0f; // mix 0: dry only
  6221. auto fx = lynx::makeEffect (lynx::EffectKind::squeeze);
  6222. fx->prepare (44100.0);
  6223. std::vector<float> l (512, 0.3f), r (512, -0.2f);
  6224. fx->process (l.data(), r.data(), 512, slot.params, 120.0);
  6225. CHECK (std::abs (l[100] - 0.3f) < 1e-6f && std::abs (r[100] + 0.2f) < 1e-6f); // Volume 127/127
  6226. slot.params[1] = 1.0f; // wet only
  6227. slot.params[2] = 32000.0f / 32768.0f; // 32768^(1 − 32000/32768) ≈ 1.27 levels per unit → heavy quantisation
  6228. std::vector<float> a (512), b (512);
  6229. for (int i = 0; i < 512; ++i)
  6230. a[(std::size_t) i] = b[(std::size_t) i] = 0.4f * (float) std::sin (i * 0.05);
  6231. fx->process (a.data(), b.data(), 512, slot.params, 120.0);
  6232. const auto levels = std::pow (32768.0f, 1.0f - 32000.0f / 32768.0f);
  6233. for (int i = 0; i < 512; i += 37)
  6234. CHECK (std::abs (a[(std::size_t) i] * levels - std::nearbyint (a[(std::size_t) i] * levels)) < 1e-3f);
  6235. std::vector<float> z (512, 0.0f), z2 (512, 0.0f);
  6236. fx->process (z.data(), z2.data(), 512, slot.params, 120.0);
  6237. CHECK (z[10] == 0.0f);
  6238. }
  6239. TEST (sakura_matrix_and_effects)
  6240. {
  6241. // REA_rodada29 §1: velocity → level follows 10^(2m)·0.05 (vel 1 vs 0.5 → ×10 in amplitude); the delay leaves
  6242. // an echo after the note has ended and keeps the instrument from going idle until its tail is silent
  6243. auto base = [] {
  6244. lynx::ChannelDef c;
  6245. c.instrument = lynx::InstrumentKind::sakura;
  6246. c.modelParams.assign (146, 0.0f);
  6247. const std::pair<int, float> values[] { { 0, 0.375f }, { 1, 0.5f }, { 3, 0.5f }, { 4, 1.0f }, { 6, 0.95f }, { 9, 0.3f },
  6248. { 11, 0.95f }, { 14, 0.3f }, { 18, 0.5f }, { 19, 0.5f }, { 27, 0.5f }, { 28, 1.0f },
  6249. { 29, 0.7f }, { 31, 1.0f }, { 32, 0.5f }, { 111, 3.0f }, { 113, 0.5f } };
  6250. for (const auto& [i, v] : values)
  6251. c.modelParams[(std::size_t) i] = v;
  6252. return c;
  6253. };
  6254. auto render = [] (const lynx::ChannelDef& c, float velocity, int held, int total) {
  6255. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sakura);
  6256. inst->prepare (48000.0);
  6257. inst->configure (c, 120.0);
  6258. inst->noteOn (60, velocity);
  6259. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  6260. for (int s = 0; s < total; s += 256)
  6261. {
  6262. if (s == held)
  6263. inst->noteOff (60);
  6264. inst->render (l.data() + s, r.data() + s, std::min (256, total - s), 1.0f, 1.0f);
  6265. }
  6266. return std::make_pair (l, std::move (inst));
  6267. };
  6268. auto energy = [] (const std::vector<float>& x, int from, int to) {
  6269. double e = 0.0;
  6270. for (int i = from; i < to; ++i)
  6271. e += (double) x[(std::size_t) i] * x[(std::size_t) i];
  6272. return e;
  6273. };
  6274. auto c = base();
  6275. c.modelParams[79] = 6.0f; // slot 1: velocity → level, amount 1
  6276. c.modelParams[80] = 1.0f;
  6277. c.modelParams[82] = 1.0f;
  6278. const auto loud = energy (render (c, 1.0f, 24064, 24064).first, 0, 24064);
  6279. const auto soft = energy (render (c, 0.5f, 24064, 24064).first, 0, 24064);
  6280. CHECK (loud > 1e-6 && std::abs (loud / soft - 100.0) < 1.0);
  6281. auto d = base();
  6282. auto [dry, dryInst] = render (d, 0.8f, 4864, 72000);
  6283. d.modelParams[63] = 1.0f; // delay on: time 0.5 → ≈ 0.52 s, wet 1, no feedback: one echo
  6284. d.modelParams[64] = 0.5f;
  6285. d.modelParams[69] = 1.0f;
  6286. auto [wet, wetInst] = render (d, 0.8f, 4864, 72000);
  6287. CHECK (energy (wet, 26000, 40000) > 100.0 * energy (dry, 26000, 40000) + 1e-9); // the note ends near 0.2 s
  6288. CHECK (dryInst->idle() || energy (dry, 70000, 72000) < 1e-12);
  6289. CHECK (! wetInst->idle()); // the echo tail has not been silent for 3 s yet
  6290. }
  6291. TEST (drumsynth_live_engine)
  6292. {
  6293. // REA_rodada30 §1: key → patch slot, state parsing, the engine against our reference model (no DLL: synthesised
  6294. // sources only, noise from the fallback seed), voice modes, patch level/pan
  6295. CHECK (lynx::drumSynthKeyToSlot (48) == 0 && lynx::drumSynthKeyToSlot (71) == 23 && lynx::drumSynthKeyToSlot (0) == 24);
  6296. CHECK (lynx::drumSynthKeyToSlot (47) == 71 && lynx::drumSynthKeyToSlot (72) == 72 && lynx::drumSynthKeyToSlot (119) == 119);
  6297. CHECK (lynx::drumSynthKeyToSlot (120) == -1);
  6298. // a version 3 state: 120 names, 120 × 38 ints (send track −1 becomes 0 below version 4), flags
  6299. std::vector<std::uint8_t> blob;
  6300. auto put = [&] (std::uint32_t v) { for (int k = 0; k < 4; ++k) blob.push_back ((std::uint8_t) (v >> (8 * k))); };
  6301. put (3);
  6302. put (66);
  6303. for (int i = 0; i < 120; ++i)
  6304. blob.push_back (2), blob.push_back ('a'), blob.push_back ((std::uint8_t) ('0' + i % 10));
  6305. for (int i = 0; i < 120 * 38; ++i)
  6306. put ((std::uint32_t) (i % 38 == 36 ? -1 : i % 38 == 4 ? 300 : 0));
  6307. put (0);
  6308. const auto parsed = lynx::drumSynthLiveParams (blob);
  6309. CHECK (parsed && (int) parsed->size() == lynx::drumSynthParamCount);
  6310. CHECK (parsed && (*parsed)[36] == 0.0f && (*parsed)[38 * 7 + 4] == 300.0f && (*parsed)[(std::size_t) lynx::drumSynthEditorKeyIndex] == 66.0f);
  6311. blob.resize (500);
  6312. CHECK (! lynx::drumSynthLiveParams (blob));
  6313. // patch on key 60 (slot 12): tone-mode Osc1 with sweep and click, noise-band Osc2, ring, 2 retriggers
  6314. 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,
  6315. 0, 0, 0, 0, 0, 0, 30, -1, 0, 0, 64 };
  6316. lynx::ChannelDef c;
  6317. c.instrument = lynx::InstrumentKind::drumsynth;
  6318. c.modelParams.assign ((std::size_t) lynx::drumSynthParamCount, 0.0f);
  6319. for (int s = 0; s < 120; ++s)
  6320. c.modelParams[(std::size_t) (38 * s + 15)] = c.modelParams[(std::size_t) (38 * s + 34)] = -1.0f;
  6321. for (int i = 0; i < 38; ++i)
  6322. c.modelParams[(std::size_t) (38 * 12 + i)] = (float) patch[i];
  6323. auto render = [] (const lynx::ChannelDef& def, std::vector<int> keys, int total, float gl = 1.0f, float gr = 1.0f) {
  6324. auto inst = lynx::makeInstrument (lynx::InstrumentKind::drumsynth);
  6325. inst->configure (def, 120.0);
  6326. inst->prepare (48000.0);
  6327. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  6328. for (int s = 0; s < total; s += 512)
  6329. {
  6330. for (std::size_t k = 0; k + 1 < keys.size(); k += 2)
  6331. if (keys[k + 1] == s)
  6332. inst->noteOn (keys[k], 100.0f / 128.0f);
  6333. inst->render (l.data() + s, r.data() + s, std::min (512, total - s), gl, gr);
  6334. }
  6335. return std::make_tuple (l, r, std::move (inst));
  6336. };
  6337. {
  6338. auto [l, r, inst] = render (c, { 60, 0 }, 9600);
  6339. // reference model (REA round 30 dsl_model.py, seed 0x56CE, FL velocity 100, V = 1, centre: × g(100)·cos(π/4))
  6340. const std::pair<int, float> golden[] { { 0, -0.0074092713f }, { 10, -0.13961405f }, { 100, -0.47290668f }, { 1000, -0.29188696f },
  6341. { 3000, -0.22130175f }, { 6000, -0.049537566f }, { 9599, 0.010392102f } };
  6342. for (const auto& [i, v] : golden)
  6343. CHECK (std::abs (l[(std::size_t) i] - v) < 2e-6f && std::abs (r[(std::size_t) i] - v) < 2e-6f);
  6344. std::vector<float> tail (48000 * 4), tailR (tail.size());
  6345. inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
  6346. CHECK (inst->idle()); // the engine reported the voice finished
  6347. }
  6348. auto energy = [] (const std::vector<float>& x, int from, int to) {
  6349. double e = 0.0;
  6350. for (int i = from; i < to; ++i)
  6351. e += (double) x[(std::size_t) i] * x[(std::size_t) i];
  6352. return e;
  6353. };
  6354. // a key without a patch level (slot 0, level 0) and a key outside the 120 patches are silent
  6355. CHECK (energy (std::get<0> (render (c, { 48, 0, 125, 0 }, 4096)), 0, 4096) == 0.0);
  6356. // mono: a second hit on the same key cuts the first; poly: both ring together (Osc2 off: no noise, deterministic)
  6357. auto tone = c;
  6358. tone.modelParams[38 * 12 + 14] = 0.0f;
  6359. const auto single = std::get<0> (render (tone, { 60, 0 }, 8192));
  6360. const auto mono = std::get<0> (render (tone, { 60, 0, 60, 2048 }, 8192));
  6361. auto poly = tone;
  6362. poly.modelParams[38 * 12 + 24] = 1.0f;
  6363. const auto both = std::get<0> (render (poly, { 60, 0, 60, 2048 }, 8192));
  6364. double monoDiff = 0.0, polyDiff = 0.0;
  6365. for (int i = 2048 + 300; i < 8192; ++i) // after the cut fade: mono = only the second hit, a copy of the first
  6366. 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]);
  6367. CHECK (monoDiff < 1e-3 && polyDiff > 1.0);
  6368. // patch pan +64 → right only; channel volume counts twice (FL applies Final.Vol in the engine and at the host)
  6369. auto right = c;
  6370. right.modelParams[38 * 12 + 1] = 64.0f;
  6371. const auto [pl, pr, pi] = render (right, { 60, 0 }, 4096);
  6372. CHECK (energy (pl, 0, 4096) < 1e-12 * energy (pr, 0, 4096) && energy (pr, 0, 4096) > 1.0);
  6373. const auto full = std::get<0> (render (tone, { 60, 0 }, 4096));
  6374. const auto half = std::get<0> (render (tone, { 60, 0 }, 4096, 0.5f, 0.5f));
  6375. CHECK (std::abs (half[100] - 0.25f * full[100]) < 1e-6f && std::abs (full[100]) > 0.01f);
  6376. }
  6377. TEST (kepler_lfo_bpm_sync)
  6378. {
  6379. // 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)
  6380. std::vector<std::uint8_t> state (0x21 + 34 * 4, 0);
  6381. auto putU32 = [&] (std::size_t o, std::uint32_t v) { std::memcpy (state.data() + o, &v, 4); };
  6382. putU32 (12, 9);
  6383. std::memcpy (state.data() + 16, "Kepler-1", 9);
  6384. putU32 (0x19, 4);
  6385. putU32 (0x1D, 34);
  6386. float k[34] {};
  6387. k[5] = 0.602085769f; // "Pads - Kepler" demo: n = 286
  6388. k[6] = 1.0f;
  6389. k[10] = 1.0f; // vibrato on
  6390. k[33] = 1.0f;
  6391. std::memcpy (state.data() + 0x21, k, sizeof k);
  6392. lynx::SynthSettings s;
  6393. float gain = 0.0f;
  6394. CHECK (lynx::synthFromKepler (state, s, gain));
  6395. CHECK (s.lfo[lynx::lfoPitch].sync && s.lfo[lynx::lfoPitch].secondsTiming);
  6396. CHECK (std::abs (s.lfo[lynx::lfoPitch].period - 286.0f / 192.0f) < 1e-6f);
  6397. k[5] = 0.0f; // slowest: 32 beats
  6398. std::memcpy (state.data() + 0x21, k, sizeof k);
  6399. CHECK (lynx::synthFromKepler (state, s, gain) && std::abs (s.lfo[lynx::lfoPitch].period - 32.0f) < 1e-6f);
  6400. k[6] = 0.0f; // free rate in seconds
  6401. std::memcpy (state.data() + 0x21, k, sizeof k);
  6402. CHECK (lynx::synthFromKepler (state, s, gain) && ! s.lfo[lynx::lfoPitch].sync);
  6403. }
  6404. namespace
  6405. {
  6406. // A Harmor state written from scratch (REA_rodada11 §4.1 layout, version 18): part A only — a decaying-harmonics
  6407. // timbre, low-pass + resonance, filter envelope, 3-voice unison, vibrato, pluck, tremolo, volume envelope with a
  6408. // velocity map — and the global block. Every other curve is neutral.
  6409. std::vector<std::uint8_t> harmorTestState (bool panLfo = true)
  6410. {
  6411. std::vector<std::uint8_t> b;
  6412. 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))); };
  6413. 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))); };
  6414. struct Pt { double dx, y, tension; int mode; };
  6415. auto curve = [&] (const std::vector<Pt>& pts, std::array<std::int32_t, 5> hdr) {
  6416. i32 (3), i32 (3), i32 ((std::int32_t) pts.size());
  6417. for (const auto& p : pts)
  6418. {
  6419. f64 (p.dx), f64 (p.y);
  6420. const auto t = (float) p.tension;
  6421. std::uint32_t u;
  6422. std::memcpy (&u, &t, 4);
  6423. i32 ((std::int32_t) u);
  6424. b.push_back ((std::uint8_t) p.mode), b.push_back (0), b.push_back (0), b.push_back (0);
  6425. }
  6426. for (auto h : hdr)
  6427. i32 (h);
  6428. };
  6429. const std::array<std::int32_t, 5> off { 0, 0, -1, -1, -1 }, on { 0, 1, -1, -1, -1 };
  6430. 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); };
  6431. i32 (18);
  6432. i32 (1 | 4);
  6433. std::vector<std::uint8_t> part;
  6434. std::swap (b, part);
  6435. std::array<std::int32_t, 386> p {};
  6436. const std::pair<int, int> values[] = { { 0, 500 }, { 3, 1000 }, { 9, 1024 }, { 11, 1000 }, { 12, 1000 }, { 17, 300 }, { 18, 4000 },
  6437. { 19, 500 }, { 20, 1000 }, { 24, 500 }, { 25, 500 }, { 28, 500 }, { 29, 256 }, { 30, -1 }, { 31, -1 }, { 33, -1 }, { 34, 3 },
  6438. { 35, 1 }, { 37, 192 }, { 38, 128 }, { 39, 500 }, { 40, 10000 }, { 41, 1 }, { 42, 10000 }, { 43, 1 }, { 45, 200 }, { 46, 500 },
  6439. { 47, 1 }, { 49, -140 }, { 50, 1200 }, { 51, 0 }, { 52, 5000 }, { 54, 500 }, { 56, 600 }, { 57, 300 }, { 59, 500 }, { 60, 600 },
  6440. { 64, 8192 }, { 66, 500 }, { 72, 600 }, { 74, 1000 }, { 75, 600 }, { 78, 5000 }, { 79, 500 }, { 80, 3000 } };
  6441. for (const auto& [i, v] : values)
  6442. p[(std::size_t) i] = v;
  6443. for (auto v : p)
  6444. i32 (v);
  6445. for (int i = 0; i < 12; ++i)
  6446. i32 (i); // module order
  6447. std::array<std::uint8_t, 0x4b> misc {};
  6448. auto put = [&] (int o, std::int32_t v) { std::memcpy (misc.data() + o, &v, 4); };
  6449. const float one = 1.0f;
  6450. put (0x08, 107), put (0x1c, 1), put (0x28, 200), std::memcpy (misc.data() + 0x30, &one, 4);
  6451. put (0x34, 0), put (0x38, 4), put (0x3c, 100), put (0x40, 108);
  6452. misc[0x44] = 1;
  6453. b.insert (b.end(), misc.begin(), misc.end());
  6454. 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 };
  6455. for (int k = 0; k < 43; ++k)
  6456. {
  6457. static const int mode2[] = { 2, 14, 23, 28, 30, 38, 39, 41, 42 }; // ×2 around 0.5: 1.0 is neutral
  6458. const bool centred = std::find (std::begin (mode1), std::end (mode1), k) != std::end (mode1);
  6459. const bool doubled = std::find (std::begin (mode2), std::end (mode2), k) != std::end (mode2);
  6460. const double neutral = centred && ! doubled ? 0.5 : 1.0;
  6461. i32 (3);
  6462. i32 (1065353216), i32 (1065353216), i32 (1065353216);
  6463. if (k == 1) // volume envelope: attack, decay to the sustain point, release
  6464. 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 });
  6465. else if (k == 4) // filter-1 envelope
  6466. curve ({ { 0, 1, 0, 0 }, { 0.25, 0.2, -0.5, 0 } }, { 0, 1, -1, -1, -1 });
  6467. else
  6468. flat (neutral);
  6469. for (int a : { 128, 128, 0, 128 })
  6470. i32 (a);
  6471. if (k == 0 && panLfo) // pan LFO
  6472. flat (1.0, on);
  6473. else
  6474. flat (0.5);
  6475. for (int a : { k == 0 ? 40000 : 0, 0, 0, 0 })
  6476. i32 (a);
  6477. i32 (0);
  6478. for (int m = 2; m < 11; ++m)
  6479. if (k == 1 && m == 3)
  6480. curve ({ { 0, 0.2, 0, 0 }, { 1, 1, -0.3, 0 } }, off); // velocity → volume
  6481. else
  6482. flat (neutral);
  6483. }
  6484. for (int c = 0; c < 26; ++c)
  6485. if (c == 2 || c == 3)
  6486. curve ({ { 0, 0.25, 0, 0 }, { 0.006, 0.12, -0.46, 0 }, { 0.994, 0, 0, 0 } }, off); // timbres
  6487. else if (c == 14)
  6488. curve ({ { 0, 1, 0, 0 }, { 1, 0, 0, 0 } }, off); // pluck curve
  6489. else
  6490. 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)));
  6491. std::swap (b, part);
  6492. i32 ((std::int32_t) part.size());
  6493. b.insert (b.end(), part.begin(), part.end());
  6494. std::array<std::int32_t, 53> g {};
  6495. g[0] = g[1] = g[2] = 256, g[10] = 500, g[11] = 1000, g[12] = 1000, g[15] = 1000;
  6496. for (auto v : g)
  6497. i32 (v);
  6498. for (int i = 0; i < 6; ++i)
  6499. i32 (i);
  6500. std::array<std::uint8_t, 0x3d> gm {};
  6501. const std::int32_t cr = 200, vr = 1;
  6502. std::memcpy (gm.data() + 12, &vr, 4);
  6503. std::memcpy (gm.data() + 24, &cr, 4);
  6504. b.insert (b.end(), gm.begin(), gm.end());
  6505. return b;
  6506. }
  6507. } // namespace
  6508. TEST (harmor_engine)
  6509. {
  6510. // REA_rodada34: state parsing, the additive engine against our reference model (hmodel.py), host levels, voice end
  6511. const auto blob = harmorTestState();
  6512. if (const char* dump = std::getenv ("LYNX_DUMP_HARMOR"))
  6513. std::ofstream (dump, std::ios::binary).write ((const char*) blob.data(), (std::streamsize) blob.size());
  6514. CHECK (lynx::harmorStateUsable (blob));
  6515. CHECK (lynx::harmorFallbacks (blob).size() == 1); // (only the harmonic-randomness note)
  6516. const auto at = lynx::harmorParamOffset (blob, 0, 52);
  6517. std::int32_t cutoff = 0;
  6518. CHECK (at > 0 && (std::memcpy (&cutoff, blob.data() + at, 4), cutoff == 5000));
  6519. CHECK (lynx::harmorParamOffset (blob, 1, 52) < 0 && lynx::harmorParamOffset (blob, -1, 12) > at);
  6520. auto broken = blob;
  6521. broken.resize (broken.size() / 2);
  6522. CHECK (! lynx::harmorStateUsable (broken));
  6523. auto old = blob;
  6524. old[0] = 15; // articulators and curves are stored from version 16
  6525. CHECK (! lynx::harmorStateUsable (old));
  6526. lynx::ChannelDef c;
  6527. c.instrument = lynx::InstrumentKind::harmor;
  6528. c.pluginState = blob;
  6529. auto render = [] (const lynx::ChannelDef& def, int total, float gl, float gr, int offAt) {
  6530. auto inst = lynx::makeInstrument (lynx::InstrumentKind::harmor);
  6531. inst->configure (def, 120.0);
  6532. inst->prepare (44100.0);
  6533. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  6534. inst->noteOn (60, 0.78125f);
  6535. for (int s = 0; s < total; s += 512)
  6536. {
  6537. if (s == offAt)
  6538. inst->noteOff (60);
  6539. inst->render (l.data() + s, r.data() + s, std::min (512, total - s), gl, gr);
  6540. }
  6541. return std::make_tuple (l, r, std::move (inst));
  6542. };
  6543. {
  6544. // reference model (REA round 34 hmodel.py; C5 at FL velocity 100, Final.Vol 1, centre, released at 6144, the
  6545. // Mersenne Twister seeded like LYNX's)
  6546. auto [l, r, inst] = render (c, 8192, 1.0f, 1.0f, 6144);
  6547. const std::tuple<int, float, float> golden[] {
  6548. { 0, 0.0f, 0.0f }, { 100, 0.00947693f, 0.04249846f }, { 1000, 0.08140575f, -0.03251422f }, { 3000, 0.05928446f, -0.00247572f },
  6549. { 6000, 0.02997839f, 0.14549091f }, { 7000, -0.01121871f, 0.04197952f }, { 8191, 0.00314545f, 0.03857505f } };
  6550. for (const auto& [i, gl, gr] : golden)
  6551. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  6552. // after the release the volume envelope ends, the record declicks and the instrument goes idle
  6553. std::vector<float> tail (44100), tailR (tail.size());
  6554. inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
  6555. CHECK (inst->idle());
  6556. CHECK (std::abs (tail.back()) == 0.0f);
  6557. }
  6558. // the reference renderer gives the same samples as the instrument
  6559. {
  6560. const auto ref = lynx::harmor::renderReference (blob, { { 0, 60, 0.78125, 1.0, 0.0, 6144 } }, 44100.0, 8192);
  6561. const auto [l, r, inst] = render (c, 8192, 1.0f, 1.0f, 6144);
  6562. double worst = 0.0;
  6563. for (int i = 0; i < 8192; ++i)
  6564. 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]) });
  6565. CHECK (worst == 0.0);
  6566. }
  6567. // FL's level law applied once: half the channel volume halves the output; full right pan silences the left side
  6568. const auto full = std::get<0> (render (c, 2048, 1.0f, 1.0f, -1));
  6569. const auto half = std::get<0> (render (c, 2048, 0.5f, 0.5f, -1));
  6570. CHECK (std::abs (half[1000] - 0.5f * full[1000]) < 1e-6f && std::abs (full[1000]) > 0.01f);
  6571. auto mono = c;
  6572. mono.pluginState = harmorTestState (false); // no pan LFO ...
  6573. const std::int32_t one = 1;
  6574. std::memcpy (mono.pluginState.data() + lynx::harmorParamOffset (mono.pluginState, 0, 34), &one, 4); // ... nor unison spread
  6575. const auto [pl, pr, pi] = render (mono, 2048, 0.0f, 1.41421356f, -1);
  6576. double el = 0.0, er = 0.0;
  6577. for (int i = 0; i < 2048; ++i)
  6578. el += (double) pl[(std::size_t) i] * pl[(std::size_t) i], er += (double) pr[(std::size_t) i] * pr[(std::size_t) i];
  6579. CHECK (el < 1e-10 * er && er > 1e-3);
  6580. }
  6581. namespace
  6582. {
  6583. // An Ogun state written from scratch (REA_rodada37 §1.3 layout, version 18; zlib with stored blocks): richness 4
  6584. // (255 partials), pre-decay, decay/release, fullness, Seed 1 + fixed Seed 2 with morph, 3-voice unison, a 2-stage
  6585. // low-pass with an LFO on its cutoff, a volume envelope with a sustain point and a velocity map, declick, normalisation,
  6586. // Impulse/Decay maps, and every plugin effect (EQ band, chorus, ping-pong delay, reverb). Everything else is neutral.
  6587. std::vector<std::uint8_t> ogunTestState()
  6588. {
  6589. std::vector<std::uint8_t> b;
  6590. 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))); };
  6591. auto f32v = [&] (float v) { std::uint32_t u; std::memcpy (&u, &v, 4); i32 ((std::int32_t) u); };
  6592. 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))); };
  6593. struct Pt { double dx, y, tension; int mode; };
  6594. auto curve = [&] (int type, const std::vector<Pt>& pts, std::array<std::int32_t, 5> hdr) {
  6595. i32 (type), i32 (3), i32 ((std::int32_t) pts.size());
  6596. for (const auto& p : pts)
  6597. {
  6598. f64 (p.dx), f64 (p.y), f32v ((float) p.tension);
  6599. b.push_back ((std::uint8_t) p.mode), b.push_back (0), b.push_back (0), b.push_back (0);
  6600. }
  6601. for (auto h : hdr)
  6602. i32 (h);
  6603. };
  6604. const std::array<std::int32_t, 5> off { 0, 0, -1, -1, -1 }, on { 0, 1, -1, -1, -1 };
  6605. auto flat = [&] (int type, double y) { curve (type, { { 0, y, 0, 0 }, { 1, y, 0, 0 } }, off); };
  6606. std::array<std::int32_t, 64> P {};
  6607. const std::pair<int, int> values[] = { { 0, 128 }, { 5, 300 }, { 6, 3000 }, { 7, 2000 }, { 8, 200 }, { 9, 500 }, { 10, 12 },
  6608. { 11, 34 }, { 12, 3 }, { 13, 64 }, { 14, 64 }, { 15, 40 }, { 16, 64 }, { 17, 180 }, { 18, 60 }, { 19, 2 }, { 20, 300 },
  6609. { 21, 400 }, { 22, 40 }, { 23, 30 }, { 24, 20 }, { 25, 64 }, { 26, 40 }, { 27, 96 }, { 28, 100 }, { 29, 64 }, { 30, 2 },
  6610. { 31, 19 }, { 32, 221 }, { 33, 0 }, { 34, 50 }, { 35, 100 }, { 36, 20 }, { 37, 60 }, { 38, 2 }, { 39, 64 }, { 40, 300 },
  6611. { 43, 30000 }, { 46, 20000 }, { 49, 0 }, { 50, 2 }, { 51, 0 }, { 52, 9 }, { 53, 1 }, { 54, 0 },
  6612. { 55, 0x01000000 }, // +0x20b: pitch follows the key
  6613. { 56, 0x00010000 }, // +0x20e: declick
  6614. { 62, 0x00000100 } }; // +0x225: normalise
  6615. for (const auto& [i, v] : values)
  6616. P[(std::size_t) i] = v;
  6617. for (auto v : P)
  6618. i32 (v);
  6619. b.push_back (0), b.push_back (1), b.push_back (0); // delay: not tempo-based, on
  6620. i32 (1), i32 (1), i32 (1); // EQ band types: peaking
  6621. b.push_back (0); // comment: 0 characters
  6622. for (int k = 0; k < 13; ++k)
  6623. {
  6624. static constexpr int sum[] = { 1, 2, 3, 9, 11, 12 };
  6625. const double neutral = std::find (std::begin (sum), std::end (sum), k) != std::end (sum) ? 0.5 : 1.0;
  6626. i32 (1);
  6627. f32v (0.0f), f32v (1.0f), f32v (k == 1 ? 0.3f : 0.0f);
  6628. if (k == 0) // volume envelope: attack, decay to the sustain point, release
  6629. 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 });
  6630. else
  6631. flat (1, neutral);
  6632. for (int a : { 128, 128, 128, 128 })
  6633. i32 (a);
  6634. if (k == 1) // cutoff LFO
  6635. curve (2, { { 0, 0.8, 0, 0 } }, on);
  6636. else
  6637. flat (2, 0.5);
  6638. for (int a : { k == 1 ? 40000 : 0, 0, 0, 0 })
  6639. i32 (a);
  6640. i32 (0);
  6641. for (int m = 0; m < 4; ++m)
  6642. if (k == 0 && m == 1)
  6643. curve (3, { { 0, 0.3, 0, 0 }, { 1, 1, -0.2, 0 } }, off); // velocity → volume
  6644. else
  6645. flat (3, neutral);
  6646. }
  6647. flat (3, 0.0); // Forced: none
  6648. curve (3, { { 0, 0.6, 0, 0 }, { 1, 0.4, 0.3, 0 } }, off); // Impulse EQ
  6649. curve (3, { { 0, 0.5, 0, 0 }, { 1, 0.3, 0, 0 } }, off); // Decay EQ
  6650. flat (3, 0.5); // Release EQ
  6651. // u32 version + zlib (stored blocks)
  6652. std::vector<std::uint8_t> s { 18, 0, 0, 0, 0x78, 0x01 };
  6653. for (std::size_t i = 0; i < b.size();)
  6654. {
  6655. const auto len = std::min<std::size_t> (65535, b.size() - i);
  6656. s.push_back (i + len >= b.size() ? 1 : 0);
  6657. s.push_back ((std::uint8_t) len), s.push_back ((std::uint8_t) (len >> 8));
  6658. s.push_back ((std::uint8_t) ~len), s.push_back ((std::uint8_t) (~len >> 8));
  6659. s.insert (s.end(), b.begin() + (std::ptrdiff_t) i, b.begin() + (std::ptrdiff_t) (i + len));
  6660. i += len;
  6661. }
  6662. std::uint32_t a1 = 1, a2 = 0;
  6663. for (auto c : b)
  6664. a1 = (a1 + c) % 65521u, a2 = (a2 + a1) % 65521u;
  6665. for (int sh = 24; sh >= 0; sh -= 8)
  6666. s.push_back ((std::uint8_t) (((a2 << 16) | a1) >> sh));
  6667. return s;
  6668. }
  6669. } // namespace
  6670. TEST (ogun_engine)
  6671. {
  6672. // REA_rodada37: state parsing, the engine against our reference model (ogmodel.py), host levels, export quality
  6673. const auto blob = ogunTestState();
  6674. if (const char* dump = std::getenv ("LYNX_DUMP_OGUN"))
  6675. std::ofstream (dump, std::ios::binary).write ((const char*) blob.data(), (std::streamsize) blob.size());
  6676. CHECK (lynx::ogunStateUsable (blob));
  6677. CHECK (lynx::ogunFallbacks (blob).empty());
  6678. CHECK (lynx::ogunParam (blob, 17) == 180 && lynx::ogunParam (blob, 52) == 9 && ! lynx::ogunParam (blob, 64));
  6679. auto broken = blob;
  6680. broken.resize (broken.size() - 300);
  6681. CHECK (! lynx::ogunStateUsable (broken));
  6682. auto old = blob;
  6683. old[0] = 13; // the articulator layout is from version 14
  6684. CHECK (! lynx::ogunStateUsable (old));
  6685. // reference model (REA round 37 ogmodel.Plugin(state).run: C5 velocity 0.75 released at 6144, G5 velocity 0.625 at 2048
  6686. // released at 8192; host block 512)
  6687. 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 } };
  6688. const auto ref = lynx::ogun::renderReference (blob, notes, 44100.0, 16384);
  6689. CHECK (ref.size() == 2 * 16384);
  6690. if (const char* dump = std::getenv ("LYNX_DUMP_OGUN_OUT"))
  6691. std::ofstream (dump, std::ios::binary).write ((const char*) ref.data(), (std::streamsize) (ref.size() * 4));
  6692. const std::tuple<int, float, float> golden[] {
  6693. { 100, -7.763202e-05f, -8.919523e-05f }, { 1000, 0.06195267f, 0.03858230f }, { 3000, 0.01363476f, -0.00378433f },
  6694. { 6000, -0.06601223f, -0.03084030f }, { 9000, 0.04204506f, 0.02452896f }, { 12000, 0.03420147f, -0.01837870f },
  6695. { 16383, -0.00465311f, -0.00119627f } };
  6696. for (const auto& [i, gl, gr] : golden)
  6697. CHECK (std::abs (ref[2 * (std::size_t) i] - gl) < 2e-6f && std::abs (ref[2 * (std::size_t) i + 1] - gr) < 2e-6f);
  6698. // the instrument plays like the reference renderer (Final.Vol 1, centre)
  6699. lynx::ChannelDef c;
  6700. c.instrument = lynx::InstrumentKind::ogun;
  6701. c.pluginState = blob;
  6702. auto render = [&] (const lynx::ChannelDef& def, int total, float gl, float gr, bool held = false) {
  6703. auto inst = lynx::makeInstrument (lynx::InstrumentKind::ogun);
  6704. inst->configure (def, 120.0);
  6705. inst->prepare (44100.0);
  6706. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  6707. for (int s = 0; s < total; s += 512)
  6708. {
  6709. if (s == 0)
  6710. inst->noteOn (60, 0.75f);
  6711. if (s == 2048)
  6712. inst->noteOn (67, 0.625f);
  6713. if (s == 6144 && ! held)
  6714. inst->noteOff (60);
  6715. if (s == 8192 && ! held)
  6716. inst->noteOff (67);
  6717. inst->render (l.data() + s, r.data() + s, std::min (512, total - s), gl, gr);
  6718. }
  6719. return std::make_tuple (l, r, std::move (inst));
  6720. };
  6721. {
  6722. auto [l, r, inst] = render (c, 16384, 1.0f, 1.0f);
  6723. double worst = 0.0;
  6724. for (int i = 0; i < 16384; ++i)
  6725. 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]) });
  6726. CHECK (worst == 0.0);
  6727. // the voices end and the effect tails die out: the instrument goes idle
  6728. std::vector<float> tail (44100), tailR (tail.size());
  6729. for (int k = 0; k < 20 && ! inst->idle(); ++k)
  6730. inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
  6731. CHECK (inst->idle());
  6732. }
  6733. // FL's level law applied once (effects off): half the channel volume halves the output once the gain ramps
  6734. // (limited to 0.001 per sample) have settled on the sustain level; full right pan silences the left side
  6735. {
  6736. auto dry = c;
  6737. for (int i : { 19, 29, 40, 53 })
  6738. CHECK (lynx::ogunSetParam (dry.pluginState, i, 0)); // chorus, delay wet, EQ band and reverb off
  6739. CHECK (lynx::ogunStateUsable (dry.pluginState) && lynx::ogunParam (dry.pluginState, 19) == 0);
  6740. const auto full = std::get<0> (render (dry, 32768, 1.0f, 1.0f, true));
  6741. const auto half = std::get<0> (render (dry, 32768, 0.5f, 0.5f, true));
  6742. double worst = 0.0, peak = 0.0;
  6743. for (int i = 30000; i < 32768; ++i)
  6744. worst = std::max (worst, (double) std::abs (half[(std::size_t) i] - 0.5f * full[(std::size_t) i])),
  6745. peak = std::max (peak, (double) std::abs (full[(std::size_t) i]));
  6746. CHECK (worst <= 1e-7 * peak && peak > 1e-3);
  6747. // FL's default channel volume (100/128): FL hands Ogun Final.Vol = (11^v − 1)/10 (REA_rodada28 §4.1); LYNX's
  6748. // centred channel gains (√2·V·cos(π/4) each) give the reference model's level at that Final.Vol
  6749. const auto V = lynx::channelGain (0.78125f);
  6750. const auto g = V * std::cos (0.25f * 3.14159265f) * 1.41421356f;
  6751. const auto atDefault = std::get<0> (render (dry, 32768, g, g, true));
  6752. const auto refDefault = lynx::ogun::renderReference (dry.pluginState, { { 0, 60, 0.75, V, 0.0, 0.0, 0.0, 0.0, 0.5, -1 },
  6753. { 2048, 67, 0.625, V, 0.0, 0.0, 0.0, 0.0, 0.5, -1 } }, 44100.0, 32768);
  6754. double ei = 0.0, er0 = 0.0;
  6755. for (int i = 30000; i < 32768; ++i)
  6756. 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];
  6757. CHECK (er0 > 0.0 && std::abs (10.0 * std::log10 (ei / er0)) < 0.001);
  6758. const auto [pl, pr, pi] = render (dry, 4096, 0.0f, 1.41421356f);
  6759. double el = 0.0, er = 0.0;
  6760. for (int i = 0; i < 4096; ++i)
  6761. el += (double) pl[(std::size_t) i] * pl[(std::size_t) i], er += (double) pr[(std::size_t) i] * pr[(std::size_t) i];
  6762. CHECK (el < 1e-10 * er && er > 1e-3);
  6763. }
  6764. // export: FL renders Ogun at quality 4 (×2 rate, 32-point reader), LYNX too while it renders offline
  6765. {
  6766. lynx::setOfflineRendering (true);
  6767. auto [l, r, inst] = render (c, 8192, 1.0f, 1.0f);
  6768. lynx::setOfflineRendering (false);
  6769. lynx::ogun::RefOptions q4;
  6770. q4.quality = 4;
  6771. const auto ref4 = lynx::ogun::renderReference (blob, notes, 44100.0, 8192, q4);
  6772. double worst = 0.0, diff = 0.0;
  6773. for (int i = 0; i < 8192; ++i)
  6774. worst = std::max (worst, (double) std::abs (ref4[2 * (std::size_t) i] - l[(std::size_t) i])),
  6775. diff = std::max (diff, (double) std::abs (ref[2 * (std::size_t) i] - l[(std::size_t) i]));
  6776. CHECK (worst == 0.0 && diff > 1e-3);
  6777. const std::tuple<int, float, float> golden4[] {
  6778. { 100, 0.00605961f, 0.00628969f }, { 1000, 0.03729841f, 0.01727954f }, { 3000, -0.09010383f, -0.02551294f },
  6779. { 6000, 0.02167798f, -0.01115632f }, { 8191, 0.00320288f, -0.00615792f } };
  6780. for (const auto& [i, gl, gr] : golden4)
  6781. CHECK (std::abs (ref4[2 * (std::size_t) i] - gl) < 2e-6f && std::abs (ref4[2 * (std::size_t) i + 1] - gr) < 2e-6f);
  6782. }
  6783. // feature variants of the same state against the reference model (energy per side over 8192 samples, three
  6784. // samples; C1 released at 6144): qualities, richness, unison, the filter types, ALT DECAY, 1/√h, Seed 2 Random,
  6785. // infinite decay, logarithmic axis, velocity → position, other rates. Goldens from ogmodel.py (REA round 37).
  6786. {
  6787. struct Sample { int at; float l, r; };
  6788. struct Variant { const char* name; std::vector<std::pair<int, int>> params; int rate; double el, er; Sample s[3]; };
  6789. const Variant variants[] {
  6790. { "q1", { { 54, 1 } }, 44100, 11.0970402, 6.87820714, { { 1500, 0.0240363069f, -0.0148524037f }, { 4000, 0.0873190612f, -0.000715861097f }, { 7000, -0.0378603041f, -0.00338819111f } } },
  6791. { "q2", { { 54, 2 } }, 44100, 11.2405446, 6.68045082, { { 1500, 0.0448660068f, -0.00560745923f }, { 4000, -0.0122657465f, 0.0104285963f }, { 7000, -0.0216034129f, -0.0134632047f } } },
  6792. { "q3", { { 54, 3 } }, 44100, 11.3200608, 6.86196449, { { 1500, -0.0031537537f, -0.0242648814f }, { 4000, 0.0426301025f, 0.0364697985f }, { 7000, 0.00843803957f, 0.0302239582f } } },
  6793. { "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 } } },
  6794. { "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 } } },
  6795. { "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 } } },
  6796. { "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 } } },
  6797. { "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 } } },
  6798. { "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 } } },
  6799. { "alt", { { 62, 257 } }, 44100, 17.2485711, 9.99223979, { { 1500, -0.0319156498f, -0.0186850764f }, { 4000, -0.0769012421f, -0.0375530645f }, { 7000, 0.0310976505f, 0.0445244014f } } },
  6800. { "isqrt", { { 62, 65792 } }, 44100, 45.7309307, 27.1224593, { { 1500, -0.04376591f, -0.00845711026f }, { 4000, 0.0351300463f, -0.129557073f }, { 7000, 0.0096148327f, 0.00123740174f } } },
  6801. { "seed2rnd", { { 11, 0 } }, 44100, 16.8228819, 11.5433061, { { 1500, -0.0117614279f, -0.026497297f }, { 4000, 0.00265733898f, -0.07069581f }, { 7000, 0.00576663949f, 0.0565217957f } } },
  6802. { "decinf", { { 6, 10000 } }, 44100, 19.1879226, 11.0487311, { { 1500, -0.0324105881f, -0.0190765262f }, { 4000, -0.0809780285f, -0.0395730771f }, { 7000, 0.034197174f, 0.048585102f } } },
  6803. { "logx", { { 57, 16777216 } }, 44100, 12.3064326, 7.31519289, { { 1500, -0.0438990779f, 0.0096983267f }, { 4000, -0.0175748877f, 0.0313935578f }, { 7000, 0.0639200211f, 0.024419222f } } },
  6804. { "velpos", { { 56, 66304 } }, 44100, 4.9966925, 3.16540662, { { 1500, 0.0083851777f, -0.0165472236f }, { 4000, 0.0630451962f, 0.0156494901f }, { 7000, -0.0293043777f, -0.00182877108f } } },
  6805. { "fs48", { }, 48000, 10.8813888, 6.53592373, { { 1500, -0.0485184453f, -0.0319588967f }, { 4000, 0.0230421089f, -0.0286604818f }, { 7000, -0.0117630213f, -0.017459048f } } },
  6806. { "fs96", { }, 96000, 11.0189683, 6.39821883, { { 1500, -0.0267860368f, -0.0227564126f }, { 4000, 0.0238709711f, -0.0100849615f }, { 7000, -0.0034619756f, 0.0451687202f } } },
  6807. };
  6808. for (const auto& v : variants)
  6809. {
  6810. auto st = blob;
  6811. for (const auto& [i, x] : v.params)
  6812. CHECK (lynx::ogunSetParam (st, i, x));
  6813. const auto y = lynx::ogun::renderReference (st, { { 0, 60, 0.75, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 6144 },
  6814. { 2048, 67, 0.625, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1 } }, (double) v.rate, 8192);
  6815. double el = 0.0, er = 0.0, peak = 0.0;
  6816. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  6817. 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]) });
  6818. bool ok = y.size() == 2 * 8192 && std::abs (el - v.el) <= 1e-5 * v.el && std::abs (er - v.er) <= 1e-5 * v.er;
  6819. for (const auto& smp : v.s)
  6820. 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;
  6821. if (! ok)
  6822. std::printf (" Ogun variant %s differs from the model: energy %.9g %.9g (model %.9g %.9g)\n", v.name, el, er, v.el, v.er);
  6823. CHECK (ok);
  6824. }
  6825. }
  6826. }
  6827. // our own Maximus v7 state (layout REA_rodada8 §1.1): 7 globals, 4 × 13 band parameters, 8 envelopes (dx, y, tension, type)
  6828. static std::vector<std::uint8_t> maximusTestState (const std::array<int, 7>& g, const std::array<std::array<int, 13>, 4>& bands,
  6829. const std::array<std::vector<std::array<double, 4>>, 8>& envs, int oversampling)
  6830. {
  6831. std::vector<std::uint8_t> d;
  6832. auto put = [&] (const void* v, std::size_t n) { d.insert (d.end(), (const std::uint8_t*) v, (const std::uint8_t*) v + n); };
  6833. auto i32 = [&] (std::int32_t v) { put (&v, 4); };
  6834. i32 (7);
  6835. for (int copy = 0; copy < 2; ++copy)
  6836. {
  6837. for (auto v : g)
  6838. i32 (v);
  6839. for (const auto& b : bands)
  6840. for (auto v : b)
  6841. i32 (v);
  6842. }
  6843. d.resize (0x1fe, 0);
  6844. std::memcpy (d.data() + 0x1f4, &oversampling, 4);
  6845. for (const auto& e : envs)
  6846. {
  6847. i32 (3), i32 (2), i32 ((std::int32_t) e.size());
  6848. for (const auto& pt : e)
  6849. {
  6850. const double dx = pt[0], y = pt[1];
  6851. const float t = (float) pt[2];
  6852. put (&dx, 8), put (&y, 8), put (&t, 4);
  6853. const std::uint8_t flags[4] { (std::uint8_t) pt[3], 0, 0, 0 };
  6854. put (flags, 4);
  6855. }
  6856. i32 (0), i32 (-1), i32 (-1), i32 (-1), i32 (-1);
  6857. }
  6858. return d;
  6859. }
  6860. // A "Kepler-1" state written from scratch (REA_rodada46 §1.1 layout, version 2): the default panel values
  6861. std::vector<std::uint8_t> keplerTestState()
  6862. {
  6863. 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 };
  6864. std::vector<std::uint8_t> st;
  6865. auto u32 = [&] (std::uint32_t v) { for (int i = 0; i < 4; ++i) st.push_back ((std::uint8_t) (v >> (8 * i))); };
  6866. auto f32 = [&] (float f) { std::uint32_t v; std::memcpy (&v, &f, 4); u32 (v); };
  6867. u32 (0), u32 (0), u32 (0), u32 (9);
  6868. for (char c : std::string ("Kepler-1"))
  6869. st.push_back ((std::uint8_t) c);
  6870. st.push_back (0);
  6871. u32 (2), u32 (34);
  6872. for (auto f : panel)
  6873. f32 (f);
  6874. st.push_back (0);
  6875. f32 (1.0f), f32 (0.5f);
  6876. const auto size = (std::uint32_t) st.size() - 12;
  6877. std::memcpy (st.data() + 4, &size, 4);
  6878. return st;
  6879. }
  6880. std::string keplerDll()
  6881. {
  6882. if (realFlpDir.empty())
  6883. return {};
  6884. return (std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "Kepler" / "engine_x64.dll").string();
  6885. }
  6886. TEST (kepler_engine)
  6887. {
  6888. const auto st = keplerTestState();
  6889. CHECK (st.size() == 178 && lynx::keplerStateUsable (st));
  6890. CHECK (lynx::keplerPanelOffset (st, 19) == 0x21 + 4 * 19 && lynx::keplerPanelOffset (st, 34) == -1);
  6891. auto broken = st;
  6892. broken[17] = 'X'; // not "Kepler-1"
  6893. CHECK (! lynx::keplerStateUsable (broken) && lynx::keplerPanelOffset (broken, 0) == -1);
  6894. auto v3 = st;
  6895. v3[0x19] = 3; // version 3 needs the gate / noise / oversampling bytes
  6896. CHECK (! lynx::keplerStateUsable (v3));
  6897. const auto dll = keplerDll();
  6898. if (dll.empty() || ! lynx::keplerDllUsable (dll))
  6899. return; // no FL installation (the engine needs the user's own DLL tables)
  6900. // against FL's engine DLL (EXEC, REA_rodada46 §6): three notes, 44.1 kHz, host blocks of 512 — bit-exact
  6901. 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 } };
  6902. const auto y = lynx::kepler::renderReference (st, dll, notes, 44100.0, 39690);
  6903. CHECK (y.size() == 2 * 39690);
  6904. if (y.size() != 2 * 39690)
  6905. return;
  6906. double el = 0.0, er = 0.0;
  6907. for (std::size_t i = 0; i < y.size(); i += 2)
  6908. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  6909. std::printf (" Kepler default patch: energy %.9g %.9g (FL's DLL 250.641268)\n", el, er);
  6910. CHECK (std::abs (el - 250.641268) < 1e-6 && std::abs (er - 250.641268) < 1e-6);
  6911. const std::tuple<int, float, float> golden[] { { 5000, 0.0467273332f, 0.0467273332f }, { 12000, -0.119412214f, -0.119412214f },
  6912. { 25000, 0.0855825394f, 0.0855825394f }, { 35000, -3.34166579e-08f, -3.34166579e-08f } };
  6913. for (const auto& [i, l, r] : golden)
  6914. CHECK (y[2 * (std::size_t) i] == l && y[2 * (std::size_t) i + 1] == r);
  6915. // the instrument: FL's wrapper velocity, channel volume as an output gain, idle after the release
  6916. lynx::ChannelDef c;
  6917. c.instrument = lynx::InstrumentKind::kepler;
  6918. c.pluginState = st;
  6919. c.samplePath = dll;
  6920. auto inst = lynx::makeInstrument (lynx::InstrumentKind::kepler);
  6921. inst->configure (c, 120.0);
  6922. inst->prepare (44100.0);
  6923. std::vector<float> l (4096), r (4096);
  6924. inst->noteOn (60, 0.8f);
  6925. double e = 0.0;
  6926. bool finite = true;
  6927. for (int b = 0; b < 8; ++b)
  6928. {
  6929. std::fill (l.begin(), l.end(), 0.0f);
  6930. std::fill (r.begin(), r.end(), 0.0f);
  6931. inst->render (l.data(), r.data(), 4096, 0.5f, 0.5f);
  6932. for (int i = 0; i < 4096; ++i)
  6933. 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];
  6934. if (b == 3)
  6935. inst->noteOff (60);
  6936. }
  6937. CHECK (finite && e > 1.0);
  6938. for (int b = 0; b < 40; ++b)
  6939. inst->render (l.data(), r.data(), 4096, 0.5f, 0.5f);
  6940. CHECK (inst->idle());
  6941. }
  6942. // REA round 57 §2: Transistor Bass
  6943. // the plugin state (event 0xD5) of an FL preset file (.fst: the FLhd/FLdt event stream)
  6944. std::vector<std::uint8_t> tbassPresetState (const char* name)
  6945. {
  6946. if (realFlpDir.empty())
  6947. return {};
  6948. std::ifstream in (std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "Transistor Bass" / name, std::ios::binary);
  6949. const std::vector<std::uint8_t> b ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  6950. std::size_t i = 0;
  6951. while (i + 8 <= b.size() && std::memcmp (b.data() + i, "FLdt", 4) != 0)
  6952. ++i;
  6953. i += 8;
  6954. while (i < b.size())
  6955. {
  6956. const auto id = b[i++];
  6957. std::size_t n = id < 64 ? 1 : id < 128 ? 2 : id < 192 ? 4 : 0;
  6958. if (id >= 192)
  6959. for (int shift = 0; i < b.size(); shift += 7)
  6960. {
  6961. const auto c = b[i++];
  6962. n |= (std::size_t) (c & 0x7F) << shift;
  6963. if (c < 0x80)
  6964. break;
  6965. }
  6966. if (id == 0xD5 && i + n <= b.size())
  6967. return { b.begin() + (std::ptrdiff_t) i, b.begin() + (std::ptrdiff_t) (i + n) };
  6968. i += n;
  6969. }
  6970. return {};
  6971. }
  6972. // the same state re-saved as engine-stream version 5 with an engine selector (REA_rodada57 §2.2)
  6973. std::vector<std::uint8_t> tbassWithEngine (const std::vector<std::uint8_t>& s, std::int32_t engine)
  6974. {
  6975. std::vector<std::uint8_t> out (s.begin(), s.begin() + 4);
  6976. auto put = [&] (const void* p, std::size_t n) { out.insert (out.end(), (const std::uint8_t*) p, (const std::uint8_t*) p + n); };
  6977. for (std::size_t at = 4; at + 12 <= s.size();)
  6978. {
  6979. std::uint32_t id;
  6980. std::uint64_t n;
  6981. std::memcpy (&id, s.data() + at, 4);
  6982. std::memcpy (&n, s.data() + at + 4, 8);
  6983. std::vector<std::uint8_t> d (s.begin() + (std::ptrdiff_t) (at + 12), s.begin() + (std::ptrdiff_t) (at + 12 + n));
  6984. at += 12 + (std::size_t) n;
  6985. if (id == 2)
  6986. {
  6987. std::uint64_t m;
  6988. std::memcpy (&m, d.data() + 4, 8);
  6989. std::vector<std::uint8_t> e (d.begin() + 12, d.begin() + 12 + (std::ptrdiff_t) m);
  6990. std::int16_t v;
  6991. std::memcpy (&v, e.data(), 2);
  6992. if (v < 5)
  6993. {
  6994. v = 5;
  6995. std::memcpy (e.data(), &v, 2);
  6996. e.insert (e.end(), 4, 0);
  6997. }
  6998. std::memcpy (e.data() + e.size() - 4, &engine, 4);
  6999. m = e.size();
  7000. d.resize (12);
  7001. std::memcpy (d.data() + 4, &m, 8);
  7002. d.insert (d.end(), e.begin(), e.end());
  7003. }
  7004. const std::uint64_t size = d.size();
  7005. put (&id, 4);
  7006. put (&size, 8);
  7007. put (d.data(), d.size());
  7008. }
  7009. return out;
  7010. }
  7011. std::string tbassDll()
  7012. {
  7013. if (realFlpDir.empty())
  7014. return {};
  7015. return (std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "Transistor Bass" / "engine_x64.dll").string();
  7016. }
  7017. // Plucked! (REA_rodada65 §1): the C++ engine against FL's DLL (EXEC: same notes, the DLL's RandSeed set before the first
  7018. // note, the host killing voices when the plugin asks). LYNX renders these bit-identically to the DLL.
  7019. TEST (plucked_engine)
  7020. {
  7021. auto stateOf = [] (std::initializer_list<std::int32_t> k) {
  7022. std::vector<std::uint8_t> st (20);
  7023. std::memcpy (st.data(), std::data (k), 20);
  7024. return st;
  7025. };
  7026. CHECK (! lynx::pluckedStateUsable (std::vector<std::uint8_t> (19)));
  7027. CHECK (lynx::pluckedStateUsable (stateOf ({ 128, 64, 1, 0, 1 })));
  7028. CHECK (lynx::pluckedParams (stateOf ({ 7, 9, 0, 1, 0 })) == std::vector<int> ({ 7, 9, 0, 1, 0 }));
  7029. using N = lynx::plucked::RefNote;
  7030. struct Sample { int at; float l, r; };
  7031. struct Golden { int set; std::int32_t P[5]; double fs; int block, law, maxPoly; double el, er; Sample s[4]; };
  7032. const std::vector<std::vector<N>> sets {
  7033. { { 0, 60, 1.0, 0.0, 0.0, 0.0, 0.0, -1 } },
  7034. { { 0, 60, 1.0, 0.0, 0.0, 0.0, 0.0, 30000 }, { 5000, 67, 0.6, -0.4, 13.0, 0.0, 0.0, 20000 } },
  7035. { { 0, 48, 0.8, 0.5, 0.0, 0.1, -0.2, 22050 }, { 100, 52, 0.8, 0.0, 0.0, 0.0, 0.0, 22050 }, { 200, 55, 0.8, -1.0, -50.0, 0.0, 0.0, 22050 } },
  7036. { { 0, 24, 1.0, 0.0, 0.0, 0.0, 0.0, -1 }, { 3000, 100, 1.0, 0.2, 0.0, 0.0, 0.0, -1 } },
  7037. { { 0, 72, 1.0, 0.0, 0.0, -0.3, 0.6, -1 }, { 77, 84, 0.5, 0.0, 0.0, 0.0, 0.0, -1 } },
  7038. };
  7039. const Golden goldens[] {
  7040. { 0, { 128, 64, 1, 0, 1 }, 44100, 512, 0, 0, 455.867624, 313.77463, { { 1000, -0.1914210170507431f, -0.2735292315483093f }, { 10000, -0.037063151597976685f, 0.00110959867015481f }, { 30000, -0.009626023471355438f, -0.001515754614956677f }, { 60000, 0.0f, 0.0f } } },
  7041. { 1, { 128, 64, 1, 1, 1 }, 44100, 512, 1, 0, 377.99866, 237.526831, { { 1000, -0.11432178318500519f, -0.14189107716083527f }, { 10000, 0.08135657012462616f, 0.027565963566303253f }, { 30000, 0.0f, 0.0f }, { 60000, 0.0f, 0.0f } } },
  7042. { 2, { 60, 20, 0, 1, 0 }, 48000, 256, 1, 2, 1570.38626, 550.991338, { { 1000, 0.4817328155040741f, 0.19528728723526f }, { 10000, -0.34781914949417114f, -0.15463846921920776f }, { 30000, 0.0f, 0.0f }, { 60000, 0.0f, 0.0f } } },
  7043. { 3, { 256, 128, 1, 0, 0 }, 44100, 1000, 1, 0, 2007.37978, 2089.11642, { { 1000, 0.3288099467754364f, 0.3288099467754364f }, { 10000, 0.04112838953733444f, 0.05718187615275383f }, { 30000, -0.08563447743654251f, -0.08728141337633133f }, { 60000, -0.0894850566983223f, -0.09222060441970825f } } },
  7044. { 4, { 0, 0, 1, 0, 1 }, 96000, 512, 0, 0, 222.608052, 243.338078, { { 1000, -0.20827248692512512f, -0.13200917840003967f }, { 10000, 0.01809507980942726f, 0.013305095955729485f }, { 30000, 0.0f, 0.0f }, { 60000, 0.0f, 0.0f } } },
  7045. };
  7046. for (const auto& g : goldens)
  7047. {
  7048. const auto st = stateOf ({ g.P[0], g.P[1], g.P[2], g.P[3], g.P[4] });
  7049. const auto length = (int) g.fs * 2;
  7050. const auto y = lynx::plucked::renderReference (st, sets[(std::size_t) g.set], g.fs, length, g.block, 777u + (std::uint32_t) g.set,
  7051. g.law != 0, g.maxPoly);
  7052. double el = 0.0, er = 0.0;
  7053. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  7054. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  7055. bool ok = y.size() == 2 * (std::size_t) length && std::abs (el - g.el) <= 1e-7 * g.el && std::abs (er - g.er) <= 1e-7 * g.er;
  7056. for (const auto& smp : g.s)
  7057. ok = ok && std::abs (y[2 * (std::size_t) smp.at] - smp.l) <= 1e-7f && std::abs (y[2 * (std::size_t) smp.at + 1] - smp.r) <= 1e-7f;
  7058. if (! ok)
  7059. std::printf (" Plucked! case %d differs from FL's DLL: energy %.9g %.9g (DLL %.9g %.9g)\n", g.set, el, er, g.el, g.er);
  7060. CHECK (ok);
  7061. }
  7062. // the instrument: plays, the gate makes a note-off silence the string, a held string dies out by itself
  7063. lynx::ChannelDef c;
  7064. c.instrument = lynx::InstrumentKind::plucked;
  7065. c.pluginState = stateOf ({ 200, 40, 1, 1, 1 });
  7066. auto inst = lynx::makeInstrument (lynx::InstrumentKind::plucked);
  7067. inst->configure (c, 120.0);
  7068. inst->prepare (44100.0);
  7069. std::vector<float> l (1024), r (1024);
  7070. inst->noteOn (57, 0.8f);
  7071. double e = 0.0;
  7072. for (int b = 0; b < 8; ++b)
  7073. {
  7074. std::fill (l.begin(), l.end(), 0.0f);
  7075. std::fill (r.begin(), r.end(), 0.0f);
  7076. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  7077. for (int i = 0; i < 1024; ++i)
  7078. e += (double) l[(std::size_t) i] * l[(std::size_t) i] + (double) r[(std::size_t) i] * r[(std::size_t) i];
  7079. }
  7080. CHECK (e > 1.0 && ! inst->idle());
  7081. inst->noteOff (57);
  7082. for (int b = 0; b < 4; ++b)
  7083. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  7084. CHECK (inst->idle());
  7085. c.pluginState = stateOf ({ 0, 64, 0, 0, 0 });
  7086. inst->configure (c, 120.0);
  7087. inst->noteOn (60, 1.0f);
  7088. inst->noteOff (60); // (no gate: the string rings on)
  7089. for (int b = 0; b < 2000 && ! inst->idle(); ++b)
  7090. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  7091. CHECK (inst->idle());
  7092. }
  7093. // BeepMap (REA_rodada65 §2): the C++ engine against FL's DLL (EXEC: same notes, the DLL's RandSeed set before the first
  7094. // note, the host killing voices when the plugin asks): a synthetic 9×6 bitmap with every Scale mode and switch, and two
  7095. // of the installation's presets. LYNX renders these bit-identically to the DLL.
  7096. std::vector<std::uint8_t> beepMapTestState (std::initializer_list<std::int32_t> settings)
  7097. {
  7098. constexpr int W = 9, H = 6;
  7099. std::vector<std::uint8_t> img (54 + 4 * W * H, 0);
  7100. auto put = [&] (std::size_t at, std::uint32_t v, int bytes) { for (int i = 0; i < bytes; ++i) img[at + (std::size_t) i] = (std::uint8_t) (v >> (8 * i)); };
  7101. img[0] = 'B', img[1] = 'M';
  7102. put (2, (std::uint32_t) img.size(), 4), put (10, 54, 4), put (14, 40, 4), put (18, W, 4), put (22, H, 4), put (26, 1, 2), put (28, 32, 2);
  7103. put (34, 4 * W * H, 4);
  7104. for (int y = 0; y < H; ++y) // bottom-up rows
  7105. for (int x = 0; x < W; ++x)
  7106. {
  7107. const auto yy = H - 1 - y;
  7108. auto* px = img.data() + 54 + 4 * (y * W + x);
  7109. px[0] = (std::uint8_t) ((x * 91 + yy * 29) & 255), px[1] = (std::uint8_t) ((x * 13 + yy * 71) & 255);
  7110. px[2] = (std::uint8_t) ((x * 37 + yy * 11) & 255), px[3] = 255;
  7111. }
  7112. std::vector<std::uint8_t> st (4 + 60 + 4, 0);
  7113. std::size_t k = 0;
  7114. for (auto v : settings)
  7115. std::memcpy (st.data() + 4 + 4 * k++, &v, 4);
  7116. const auto len = (std::int32_t) img.size();
  7117. std::memcpy (st.data() + 64, &len, 4);
  7118. st.insert (st.end(), img.begin(), img.end());
  7119. return st;
  7120. }
  7121. // the 0xD5 state of one of the installation's BeepMap presets (empty without an installation)
  7122. std::vector<std::uint8_t> beepMapPresetState (const char* name)
  7123. {
  7124. if (realFlpDir.empty())
  7125. return {};
  7126. std::ifstream in (std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "BeepMap" / name, std::ios::binary);
  7127. const std::vector<std::uint8_t> b ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  7128. std::size_t i = 0;
  7129. while (i + 8 <= b.size() && std::memcmp (b.data() + i, "FLdt", 4) != 0)
  7130. ++i;
  7131. i += 8;
  7132. while (i < b.size())
  7133. {
  7134. const auto id = b[i++];
  7135. std::size_t n = id < 64 ? 1 : id < 128 ? 2 : id < 192 ? 4 : 0;
  7136. if (id >= 192)
  7137. for (int shift = 0; i < b.size(); shift += 7)
  7138. {
  7139. const auto c = b[i++];
  7140. n |= (std::size_t) (c & 0x7F) << shift;
  7141. if (c < 0x80)
  7142. break;
  7143. }
  7144. if (id == 0xD5 && i + n <= b.size())
  7145. return { b.begin() + (std::ptrdiff_t) i, b.begin() + (std::ptrdiff_t) (i + n) };
  7146. i += n;
  7147. }
  7148. return {};
  7149. }
  7150. TEST (beepmap_engine)
  7151. {
  7152. const auto synthA = beepMapTestState ({ 3000, 200, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64 });
  7153. CHECK (lynx::beepMapStateUsable (synthA));
  7154. CHECK (lynx::beepMapParams (synthA)->at (1) == 200 && lynx::beepMapParamOffset (synthA, 2) == 12);
  7155. auto broken = synthA;
  7156. broken[0] = 2; // versions above 1 are refused
  7157. CHECK (! lynx::beepMapStateUsable (broken));
  7158. broken = synthA;
  7159. broken.resize (broken.size() - 20); // truncated bitmap
  7160. CHECK (! lynx::beepMapStateUsable (broken));
  7161. using N = lynx::beepmap::RefNote;
  7162. struct Sample { int at; float l, r; };
  7163. struct Golden { int set; double fs; int block; double el, er; Sample s[4]; };
  7164. std::vector<std::vector<std::uint8_t>> states {
  7165. synthA,
  7166. beepMapTestState ({ 5000, 600, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 64 }),
  7167. beepMapTestState ({ 8000, 100, 2, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 64 }),
  7168. beepMapTestState ({ 1000, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 64 }),
  7169. beepMapPresetState ("Default.fst"),
  7170. beepMapPresetState ("Voice.fst"),
  7171. };
  7172. const std::vector<std::vector<N>> sets {
  7173. { { 0, 60, 1.0, 0.0, 0.0, -1 } },
  7174. { { 0, 57, 0.8, 0.3, 25.0, -1 }, { 7000, 64, 0.6, -0.5, 0.0, -1 } },
  7175. { { 0, 48, 1.0, 0.0, 0.0, -1 }, { 300, 72, 0.5, 0.0, -40.0, -1 } },
  7176. { { 0, 67, 1.0, -0.2, 0.0, -1 } },
  7177. { { 0, 60, 1.0, 0.0, 0.0, -1 } },
  7178. { { 0, 55, 0.8, 0.0, 0.0, -1 }, { 20000, 62, 0.8, 0.4, 0.0, -1 } },
  7179. };
  7180. const Golden goldens[] {
  7181. { 0, 44100, 512, 70.02738, 69.0877973, { { 1000, 0.030967699363827705f, -0.218118816614151f }, { 10000, 0.0f, 0.0f }, { 30000, 0.0f, 0.0f }, { 60000, 0.0f, 0.0f } } },
  7182. { 1, 48000, 256, 2814.53446, 3514.46151, { { 1000, 0.02367428131401539f, 0.06526239961385727f }, { 10000, -0.06188536435365677f, 0.062191300094127655f }, { 30000, -0.153680220246315f, -0.27799028158187866f }, { 60000, 0.03855478763580322f, 0.1298033744096756f } } },
  7183. { 2, 44100, 1000, 6644.01869, 7606.70378, { { 1000, 0.06019101291894913f, 0.2049546241760254f }, { 10000, -0.852304220199585f, -0.13111725449562073f }, { 30000, 0.11700113117694855f, -0.6130681037902832f }, { 60000, -0.2660403549671173f, -0.09453065693378448f } } },
  7184. { 3, 96000, 512, 51.7332962, 28.9913476, { { 1000, -0.025792766362428665f, -0.07221365720033646f }, { 10000, 0.0f, 0.0f }, { 30000, 0.0f, 0.0f }, { 60000, 0.0f, 0.0f } } },
  7185. { 4, 44100, 512, 8142.09413, 5639.86691, { { 1000, 0.050016239285469055f, 0.05289545655250549f }, { 10000, -0.258514404296875f, -0.16095803678035736f }, { 30000, 0.765974223613739f, 0.477584570646286f }, { 60000, 0.040680620819330215f, 0.17371401190757751f } } },
  7186. { 5, 48000, 512, 2568.60856, 3975.24938, { { 1000, 0.07109857350587845f, 0.07109857350587845f }, { 10000, -0.057797715067863464f, -0.057797715067863464f }, { 30000, 0.08789601176977158f, 0.04657084494829178f }, { 60000, 0.08154996484518051f, 0.12709519267082214f } } },
  7187. };
  7188. for (const auto& g : goldens)
  7189. {
  7190. const auto& st = states[(std::size_t) g.set];
  7191. if (st.empty())
  7192. continue; // (no FL installation: the presets are skipped)
  7193. const auto length = (int) g.fs * 2;
  7194. const auto y = lynx::beepmap::renderReference (st, sets[(std::size_t) g.set], g.fs, length, g.block, 4321u + (std::uint32_t) g.set);
  7195. double el = 0.0, er = 0.0;
  7196. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  7197. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  7198. bool ok = y.size() == 2 * (std::size_t) length && std::abs (el - g.el) <= 1e-7 * g.el && std::abs (er - g.er) <= 1e-7 * g.er;
  7199. for (const auto& smp : g.s)
  7200. ok = ok && std::abs (y[2 * (std::size_t) smp.at] - smp.l) <= 1e-7f && std::abs (y[2 * (std::size_t) smp.at + 1] - smp.r) <= 1e-7f;
  7201. if (! ok)
  7202. std::printf (" BeepMap case %d differs from FL's DLL: energy %.9g %.9g (DLL %.9g %.9g)\n", g.set, el, er, g.el, g.er);
  7203. CHECK (ok);
  7204. }
  7205. // the instrument: plays the image, ends after the last column, a note-off fades the voice out
  7206. lynx::ChannelDef c;
  7207. c.instrument = lynx::InstrumentKind::beepmap;
  7208. c.pluginState = states[1];
  7209. auto inst = lynx::makeInstrument (lynx::InstrumentKind::beepmap);
  7210. inst->configure (c, 120.0);
  7211. inst->prepare (44100.0);
  7212. std::vector<float> l (1024), r (1024);
  7213. inst->noteOn (60, 0.8f);
  7214. double e = 0.0;
  7215. for (int b = 0; b < 8; ++b)
  7216. {
  7217. std::fill (l.begin(), l.end(), 0.0f);
  7218. std::fill (r.begin(), r.end(), 0.0f);
  7219. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  7220. for (int i = 0; i < 1024; ++i)
  7221. e += (double) l[(std::size_t) i] * l[(std::size_t) i] + (double) r[(std::size_t) i] * r[(std::size_t) i];
  7222. }
  7223. CHECK (e > 0.01 && ! inst->idle()); // (Loop on)
  7224. inst->noteOff (60);
  7225. for (int b = 0; b < 4; ++b)
  7226. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  7227. CHECK (inst->idle());
  7228. c.pluginState = synthA; // (no loop: 9 columns of 64·(e^(200/4096·ln 33) − 1)… samples, then the voice ends)
  7229. inst->configure (c, 120.0);
  7230. inst->noteOn (60, 1.0f);
  7231. for (int b = 0; b < 200 && ! inst->idle(); ++b)
  7232. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  7233. CHECK (inst->idle());
  7234. }
  7235. // ---- Sawer (REA_rodada65 §3): installation presets and data.saw read at run time, fingerprints of engine_x64.dll ----
  7236. namespace
  7237. {
  7238. std::vector<std::uint8_t> sawerPresetState (const char* rel)
  7239. {
  7240. if (realFlpDir.empty())
  7241. return {};
  7242. std::ifstream in (std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "Sawer" / rel, std::ios::binary);
  7243. const std::vector<std::uint8_t> b ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  7244. std::size_t i = 0;
  7245. while (i + 8 <= b.size() && std::memcmp (b.data() + i, "FLdt", 4) != 0)
  7246. ++i;
  7247. i += 8;
  7248. while (i < b.size())
  7249. {
  7250. const auto id = b[i++];
  7251. std::size_t n = id < 64 ? 1 : id < 128 ? 2 : id < 192 ? 4 : 0;
  7252. if (id >= 192)
  7253. for (int shift = 0; i < b.size(); shift += 7)
  7254. {
  7255. const auto c = b[i++];
  7256. n |= (std::size_t) (c & 0x7F) << shift;
  7257. if (c < 0x80)
  7258. break;
  7259. }
  7260. if (id == 0xD5 && i + n <= b.size())
  7261. return { b.begin() + (std::ptrdiff_t) i, b.begin() + (std::ptrdiff_t) (i + n) };
  7262. i += n;
  7263. }
  7264. return {};
  7265. }
  7266. } // namespace
  7267. TEST (sawer_engine)
  7268. {
  7269. // a synthetic header: "SSLF" + size + "1rwS" + 96 floats
  7270. std::vector<std::uint8_t> synth (12 + 4 * 96, 0);
  7271. std::memcpy (synth.data(), "SSLF", 4);
  7272. const std::uint32_t size = 4 + 4 * 96;
  7273. std::memcpy (synth.data() + 4, &size, 4);
  7274. std::memcpy (synth.data() + 8, "1rwS", 4);
  7275. const float p27 = 0.75f;
  7276. std::memcpy (synth.data() + 12 + 4 * 27, &p27, 4);
  7277. CHECK (lynx::sawerStateUsable (synth));
  7278. CHECK (lynx::sawerParam (synth, 27) == 0.75f && lynx::sawerParamOffset (synth, 27) == 12 + 4 * 27);
  7279. CHECK (! lynx::sawerParam (synth, 96) && lynx::sawerParamOffset (synth, -1) < 0);
  7280. auto broken = synth;
  7281. broken[8] = 'X';
  7282. CHECK (! lynx::sawerStateUsable (broken));
  7283. CHECK (! lynx::sawerStateUsable (std::vector<std::uint8_t> (synth.begin(), synth.end() - 4)));
  7284. const auto dflt = sawerPresetState ("Default.fst");
  7285. const auto slap = sawerPresetState ("Arpeggios/FG Velocity Slapper.fst");
  7286. const auto tablePath = realFlpDir.empty() ? std::string() : (std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "Sawer" / "data.saw").string();
  7287. const auto table = tablePath.empty() ? std::vector<float> {} : lynx::sawerReadTable (tablePath);
  7288. if (dflt.empty() || slap.empty() || table.size() != 8192u)
  7289. {
  7290. std::printf (" (sawer_engine: FL Studio installation not found, DLL fingerprints skipped)\n");
  7291. return;
  7292. }
  7293. CHECK (lynx::sawerStateUsable (dflt) && lynx::sawerStateUsable (slap));
  7294. // DLL fingerprints (py/gold_sw.py: the EXEC harness, L = R = Final.Vol); "syn" = Default with our own settings
  7295. // (unison 4, sub pulse at 0.6, sync, band-pass, noise, resonance, chorus)
  7296. auto setP = [] (std::vector<std::uint8_t> s, int k, float v) { std::memcpy (s.data() + 12 + 4 * k, &v, 4); return s; };
  7297. auto syn = dflt;
  7298. for (const auto& kv : { std::pair { 14, 1.0f }, { 23, 1.0f }, { 25, 1.0f }, { 24, 0.5f }, { 31, 0.4f }, { 47, 1.0f }, { 19, 0.6f },
  7299. { 26, 0.3f }, { 28, 0.7f }, { 45, 0.5f }, { 46, 0.0f } })
  7300. syn = setP (syn, kv.first, kv.second);
  7301. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  7302. using N = lynx::sawer::RefNote;
  7303. struct Sample { int at; float l, r; };
  7304. struct Golden { const std::vector<std::uint8_t>* state; std::vector<N> notes; double fs; int block; double el, er; Sample s[4]; };
  7305. const Golden goldens[] {
  7306. { &dflt, { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 }, { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 }, { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 } },
  7307. 44100, 512, 1172.343107, 1169.673445,
  7308. { { 1000, 0.0349505879f, 0.0349650607f }, { 10000, -0.0326882228f, -0.0326587185f }, { 30000, 0.139422998f, 0.139268339f }, { 60000, -1.01108653e-05f, -1.01210762e-05f } } },
  7309. { &slap, { { 0, 57, iv (90), 1.0, 0.0, 0.0, 40000 }, { 500, 64, iv (70), 1.0, 0.0, 0.0, 40000 } }, 48000, 256, 62.85129835, 62.26892467,
  7310. { { 1000, 0.00919434987f, 0.0769683793f }, { 10000, 0.0363935083f, 0.0227626693f }, { 30000, -0.00462511741f, -0.0105419969f }, { 60000, -0.00727002183f, -0.00159690797f } } },
  7311. { &syn, { { 0, 48, iv (100), 1.0, 0.0, 0.0, 20000 }, { 5000, 55, iv (60), 1.0, 0.0, 25.0, 30000 } }, 44100, 1000, 579.5093366, 593.7384022,
  7312. { { 1000, -0.162745371f, -0.172861978f }, { 10000, 0.0468756184f, 0.112415045f }, { 30000, -0.123887017f, -0.132816017f }, { 60000, 0.0f, 0.0f } } },
  7313. };
  7314. for (const auto& g : goldens)
  7315. {
  7316. const auto y = lynx::sawer::renderReference (*g.state, table, g.notes, g.fs, 66150, g.block);
  7317. CHECK (y.size() == 2 * 66150u);
  7318. if (y.size() != 2 * 66150u)
  7319. continue;
  7320. double el = 0.0, er = 0.0;
  7321. for (std::size_t i = 0; i < 66150; ++i)
  7322. el += (double) y[2 * i] * y[2 * i], er += (double) y[2 * i + 1] * y[2 * i + 1];
  7323. CHECK (std::abs (el - g.el) < 1e-5 * g.el && std::abs (er - g.er) < 1e-5 * g.er);
  7324. for (const auto& s : g.s)
  7325. CHECK (std::abs (y[2 * (std::size_t) s.at] - s.l) < 1e-7f && std::abs (y[2 * (std::size_t) s.at + 1] - s.r) < 1e-7f);
  7326. }
  7327. // the instrument: the same engine behind LYNX's channel gains (centre → law A), velocity → Init.Vol; it goes idle
  7328. lynx::ChannelDef c;
  7329. c.instrument = lynx::InstrumentKind::sawer;
  7330. c.pluginState = dflt;
  7331. c.samplePath = tablePath;
  7332. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sawer);
  7333. inst->configure (c, 120.0);
  7334. inst->prepare (44100.0);
  7335. std::vector<float> l (512 * 40, 0.0f), r (512 * 40, 0.0f);
  7336. for (int b = 0; b < 40; ++b)
  7337. {
  7338. if (b == 0)
  7339. inst->noteOn (60, 0.78125f);
  7340. if (b == 10)
  7341. inst->noteOff (60);
  7342. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  7343. }
  7344. const auto ref = lynx::sawer::renderReference (dflt, table, { { 0, 60, (std::pow (11.0, 1.28 * 0.78125) - 1.0) / 10.0, 1.0, 0.0, 0.0, 5120 } }, 44100.0,
  7345. 8192, 512, 120.0, 0, true);
  7346. double diff = 0.0, energy = 0.0;
  7347. for (int i = 0; i < 8192; ++i)
  7348. {
  7349. 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]) });
  7350. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  7351. }
  7352. CHECK (diff == 0.0 && energy > 1e-3);
  7353. CHECK (inst->idle());
  7354. }
  7355. TEST (transistorbass_engine)
  7356. {
  7357. const auto factory = tbassPresetState ("Factory programs.fst");
  7358. const auto seqOff = tbassPresetState ("Sequencer off.fst");
  7359. if (factory.empty() || seqOff.empty())
  7360. return; // no FL installation
  7361. CHECK (factory.size() == 197887 && seqOff.size() == 196051);
  7362. CHECK (lynx::transistorBassStateUsable (factory) && lynx::transistorBassStateUsable (seqOff));
  7363. CHECK (lynx::transistorBassStateUsable (tbassWithEngine (factory, 2)));
  7364. auto broken = factory;
  7365. broken[0] = 3; // the wrapper refuses versions above 2
  7366. CHECK (! lynx::transistorBassStateUsable (broken));
  7367. auto truncated = factory;
  7368. truncated.resize (truncated.size() - 1000);
  7369. CHECK (! lynx::transistorBassStateUsable (truncated));
  7370. const auto dll = tbassDll();
  7371. if (dll.empty() || ! lynx::transistorBassDllUsable (dll))
  7372. return;
  7373. // against FL's engine DLL (EXEC, REA_rodada57 §2.11): programs of the factory bank (sequencer, accents, slides,
  7374. // distortion, delay, reverb) on the three engines and the sequencer-off preset with legato and accent notes; host
  7375. // blocks of 512 at 44.1 kHz, tempo 125. LYNX differs from the DLL by −121 dB or less here (library pow rounding).
  7376. struct Sample { int at; float l, r; };
  7377. struct Golden { const char* preset; int engine, key; double el, er; Sample s[4]; };
  7378. const Golden goldens[] {
  7379. { "Factory", -1, 0, 84.1648489, 84.1648489, { { 5000, -2.72742806e-09f, -2.72742806e-09f }, { 20000, -0.0366424844f, -0.0366424844f }, { 47000, 2.15875261e-07f, 2.15875261e-07f }, { 80000, 0.0f, 0.0f } } },
  7380. { "Factory", -1, 3, 140.915804, 135.290403, { { 5000, 6.46460592e-07f, 6.46460592e-07f }, { 20000, 0.000612073636f, 0.00883315224f }, { 47000, 0.00334156211f, 0.0349708796f }, { 80000, -0.0180665664f, -0.00647424255f } } },
  7381. { "Factory", -1, 9, 74.6013145, 74.6013145, { { 5000, 1.17278e-06f, 1.17278e-06f }, { 20000, 4.45517001e-09f, 4.45517001e-09f }, { 47000, -0.00680137333f, -0.00680137333f }, { 80000, 9.70963379e-11f, 9.70963379e-11f } } },
  7382. { "Factory", -1, 21, 672.27801, 665.683557, { { 5000, 9.88614047e-05f, -0.00434152689f }, { 20000, 0.00984471664f, -0.00176824001f }, { 47000, -0.00745432731f, -0.00174837175f }, { 80000, 0.00471894164f, -0.00477111898f } } },
  7383. { "Factory", -1, 65, 534.422868, 543.176189, { { 5000, -0.00286525022f, -0.00691002142f }, { 20000, -0.0952312946f, -0.0940120071f }, { 47000, -0.109519325f, -0.115677707f }, { 80000, 0.0193505734f, 0.0466997996f } } },
  7384. { "Factory", 1, 65, 531.095944, 535.778912, { { 5000, 0.00939375907f, 0.000506323006f }, { 20000, 0.0880209357f, 0.0815288723f }, { 47000, 0.0840308368f, 0.0360141024f }, { 80000, -0.0445356891f, -0.058858037f } } },
  7385. { "Factory", 2, 65, 577.993833, 585.388538, { { 5000, -0.000922904757f, 0.0082119517f }, { 20000, 0.100198291f, 0.0890608132f }, { 47000, 0.0754022077f, 0.133105934f }, { 80000, -0.0728695691f, -0.0721700341f } } },
  7386. { "SeqOff", -1, 48, 27.8141841, 27.8141841, { { 5000, -0.0331321731f, -0.0331321731f }, { 20000, -0.016056465f, -0.016056465f }, { 47000, -0.0118189175f, -0.0118189175f }, { 80000, 0.0f, 0.0f } } },
  7387. { "SeqOff", 1, 48, 21.7931524, 21.7931524, { { 5000, 0.0268912539f, 0.0268912539f }, { 20000, 0.0150698861f, 0.0150698861f }, { 47000, 0.011147175f, 0.011147175f }, { 80000, -1.04869307e-23f, -1.04869307e-23f } } },
  7388. { "SeqOff", 2, 48, 61.3736312, 61.3736312, { { 5000, 0.0370108262f, 0.0370108262f }, { 20000, 0.0206214525f, 0.0206214525f }, { 47000, 0.0157183651f, 0.0157183651f }, { 80000, -1.3009924e-23f, -1.3009924e-23f } } },
  7389. };
  7390. constexpr int length = 88200;
  7391. for (const auto& g : goldens)
  7392. {
  7393. const bool factoryBank = std::string (g.preset) == "Factory";
  7394. auto st = factoryBank ? factory : seqOff;
  7395. if (g.engine >= 0)
  7396. st = tbassWithEngine (st, g.engine);
  7397. std::vector<lynx::tbass::RefNote> notes;
  7398. if (factoryBank)
  7399. notes.push_back ({ 0, g.key, 0.5, 0.0, 0.0, 0.0, 0.0, 70560 });
  7400. else
  7401. {
  7402. notes.push_back ({ 0, g.key, 0.5, 0.0, 0.0, 0.0, 0.0, 30000 });
  7403. notes.push_back ({ 20000, g.key + 7, 1.6, 0.0, 0.0, 0.0, 0.0, 50000 });
  7404. notes.push_back ({ 45000, g.key - 5, 0.3, 0.0, 0.0, 0.0, 0.0, 70560 });
  7405. }
  7406. const auto y = lynx::tbass::renderReference (st, dll, notes, 44100.0, length);
  7407. double el = 0.0, er = 0.0, peak = 0.0;
  7408. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  7409. 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]) });
  7410. bool ok = y.size() == 2 * (std::size_t) length && std::abs (el - g.el) <= 1e-5 * g.el && std::abs (er - g.er) <= 1e-5 * g.er;
  7411. for (const auto& smp : g.s)
  7412. 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;
  7413. if (! ok)
  7414. std::printf (" Transistor Bass %s engine %d key %d differs from FL's DLL: energy %.9g %.9g (DLL %.9g %.9g)\n", g.preset, g.engine, g.key, el, er, g.el, g.er);
  7415. CHECK (ok);
  7416. }
  7417. // the instrument: FL's wrapper velocity, the channel volume as an output gain, the sequencer plays while the key is held
  7418. lynx::ChannelDef c;
  7419. c.instrument = lynx::InstrumentKind::transistorbass;
  7420. c.pluginState = factory;
  7421. c.samplePath = dll;
  7422. auto inst = lynx::makeInstrument (lynx::InstrumentKind::transistorbass);
  7423. inst->configure (c, 128.0);
  7424. inst->prepare (44100.0);
  7425. std::vector<float> l (4096), r (4096);
  7426. inst->noteOn (65, 0.8f);
  7427. double e = 0.0;
  7428. bool finite = true;
  7429. for (int b = 0; b < 24; ++b)
  7430. {
  7431. std::fill (l.begin(), l.end(), 0.0f);
  7432. std::fill (r.begin(), r.end(), 0.0f);
  7433. inst->render (l.data(), r.data(), 4096, 0.5f, 0.5f);
  7434. for (int i = 0; i < 4096; ++i)
  7435. 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];
  7436. if (b == 20)
  7437. inst->noteOff (65);
  7438. }
  7439. CHECK (finite && e > 1.0 && ! inst->idle());
  7440. for (int b = 0; b < 400 && ! inst->idle(); ++b)
  7441. inst->render (l.data(), r.data(), 4096, 0.5f, 0.5f);
  7442. CHECK (inst->idle());
  7443. }
  7444. TEST (kepler_demo_channels)
  7445. {
  7446. // every Kepler channel of FL's demo projects on LYNX's engine against FL's engine DLL (EXEC fingerprints, REA_rodada46 §6;
  7447. // the engine differs from the DLL by −127 dB or less on these, from the DLL library's pow rounding)
  7448. const auto dll = keplerDll();
  7449. if (dll.empty() || ! lynx::keplerDllUsable (dll))
  7450. return;
  7451. struct Sample { int at; float l, r; };
  7452. struct Golden { const char* file; const char* channel; std::size_t stateSize; double el, er; Sample s[4]; };
  7453. const Golden goldens[] {
  7454. { "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 } } },
  7455. { "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 } } },
  7456. { "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 } } },
  7457. { "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 } } },
  7458. };
  7459. int checked = 0;
  7460. for (const auto& g : goldens)
  7461. {
  7462. std::ifstream in (std::filesystem::path (realFlpDir) / "Demo projects" / g.file, std::ios::binary);
  7463. const std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  7464. if (bytes.empty())
  7465. continue;
  7466. const flp::Channel* ch = nullptr;
  7467. const auto project = flp::parseProject (bytes);
  7468. for (const auto& c : project.channels)
  7469. if (c.pluginName == "Kepler" && c.name == g.channel)
  7470. ch = &c;
  7471. CHECK (ch != nullptr);
  7472. if (ch == nullptr)
  7473. continue;
  7474. CHECK (ch->pluginState.size() == g.stateSize && lynx::keplerStateUsable (ch->pluginState));
  7475. 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 },
  7476. { 16000, 55, 0.95, 0.0, 0.0, 30000 } }, 44100.0, 39690);
  7477. double el = 0.0, er = 0.0, peak = 0.0;
  7478. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  7479. 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]) });
  7480. bool ok = y.size() == 2 * 39690 && std::abs (el - g.el) <= 1e-5 * g.el && std::abs (er - g.er) <= 1e-5 * g.er;
  7481. for (const auto& smp : g.s)
  7482. 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;
  7483. if (! ok)
  7484. 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);
  7485. CHECK (ok);
  7486. ++checked;
  7487. }
  7488. std::printf (" Kepler: %d demo channels checked against FL's engine DLL\n", checked);
  7489. CHECK (checked == (int) std::size (goldens));
  7490. }
  7491. TEST (ogun_demo_channels)
  7492. {
  7493. // every Ogun channel of FL's demo projects (with its plugin effects) against the round-37 reference model, which is
  7494. // 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),
  7495. // 39 690 samples at 44.1 kHz, at the state's quality and at the export quality 4. Fingerprint: energy per side and
  7496. // four samples (the engine matches the model within float rounding, −125 dB or better on the whole signal).
  7497. if (realFlpDir.empty())
  7498. return;
  7499. struct Sample { int at; float l, r; };
  7500. struct Golden { const char* file; int ordinal; std::size_t stateSize; bool export4; double el, er; Sample s[4]; };
  7501. const Golden goldens[] {
  7502. { "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
  7503. { "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
  7504. { "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
  7505. { "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
  7506. { "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
  7507. { "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
  7508. { "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
  7509. { "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
  7510. { "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
  7511. { "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
  7512. { "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
  7513. { "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
  7514. { "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
  7515. { "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
  7516. { "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
  7517. { "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
  7518. { "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
  7519. { "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
  7520. { "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
  7521. { "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
  7522. { "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
  7523. { "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
  7524. { "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
  7525. { "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
  7526. { "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
  7527. { "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
  7528. { "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
  7529. { "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
  7530. { "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
  7531. { "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
  7532. { "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
  7533. { "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
  7534. { "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
  7535. { "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
  7536. { "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
  7537. { "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
  7538. { "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
  7539. { "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
  7540. { "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
  7541. { "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
  7542. };
  7543. std::map<std::string, std::vector<std::vector<std::uint8_t>>> states;
  7544. int checked = 0;
  7545. for (const auto& g : goldens)
  7546. {
  7547. if (! states.contains (g.file))
  7548. {
  7549. std::ifstream in (std::filesystem::path (realFlpDir) / "Demo projects" / g.file, std::ios::binary);
  7550. const std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  7551. auto& list = states[g.file];
  7552. if (! bytes.empty())
  7553. for (const auto& c : flp::parseProject (bytes).channels)
  7554. if (c.pluginName == "Ogun")
  7555. list.push_back (c.pluginState);
  7556. }
  7557. const auto& list = states[g.file];
  7558. if (list.empty())
  7559. continue; // not this installation
  7560. CHECK ((int) list.size() > g.ordinal);
  7561. if ((int) list.size() <= g.ordinal)
  7562. continue;
  7563. const auto& st = list[(std::size_t) g.ordinal];
  7564. CHECK (st.size() == g.stateSize);
  7565. lynx::ogun::RefOptions opt;
  7566. opt.quality = g.export4 ? 4 : -1;
  7567. const auto y = lynx::ogun::renderReference (st, { { 0, 60, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 20000 },
  7568. { 8000, 67, 0.6, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 26000 },
  7569. { 16000, 55, 0.95, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 30000 } }, 44100.0, 39690, opt);
  7570. double el = 0.0, er = 0.0, peak = 0.0;
  7571. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  7572. 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]) });
  7573. bool ok = y.size() == 2 * 39690 && std::abs (el - g.el) <= 1e-5 * g.el && std::abs (er - g.er) <= 1e-5 * g.er;
  7574. for (const auto& smp : g.s)
  7575. 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;
  7576. if (! ok)
  7577. std::printf (" Ogun %s #%d (q%s) differs from the model: energy %.9g %.9g (model %.9g %.9g)\n", g.file, g.ordinal,
  7578. g.export4 ? "4" : "-state", el, er, g.el, g.er);
  7579. CHECK (ok);
  7580. ++checked;
  7581. }
  7582. std::printf (" Ogun: %d demo renders checked against the reference model\n", checked);
  7583. CHECK (checked == (int) std::size (goldens));
  7584. }
  7585. TEST (autogun_template_channels)
  7586. {
  7587. // REA_rodada57: every distinct Autogun state of the installation's projects (templates and the render test), with its
  7588. // effects and the Magic Dust stage (SG.dat from the installation), three notes (C5 1.0 0–20000, G5 0.6 8000–26000,
  7589. // G4 0.95 16000–30000), 39 690 samples at 44.1 kHz, at the state's quality and at the export quality 4. The template
  7590. // goldens are renders of the installation's Autogun_x64.dll (LYNX is within −127 dB of them); the render-test state
  7591. // uses Seed 2 "Random", so its golden is LYNX's own render with the counter at 0 (a fingerprint of the engine).
  7592. if (realFlpDir.empty())
  7593. return;
  7594. const auto sgPath = std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "Autogun" / "Data" / "SG.dat";
  7595. std::ifstream sgIn (sgPath, std::ios::binary);
  7596. const std::vector<std::uint8_t> sgState ((std::istreambuf_iterator<char> (sgIn)), std::istreambuf_iterator<char>());
  7597. if (sgState.size() != 1342)
  7598. return; // not this installation
  7599. struct Sample { int at; float l, r; };
  7600. struct Golden { const char* file; std::size_t stateSize; bool export4; double el, er; Sample s[4]; };
  7601. const Golden goldens[] {
  7602. { "System/Render tests/Autogun.flp", 2403, false, 1898.57144, 1609.64764, { { 5000, 0.189644396f, 0.104252934f }, { 12000, 0.0354045518f, 0.002853852f }, { 25000, -0.212527201f, -0.364677101f }, { 35000, 0.00715163164f, -0.0779336989f } } }, // Autogun (LYNX engine, counter 0)
  7603. { "System/Render tests/Autogun.flp", 2403, true, 1883.83096, 1696.41005, { { 5000, -0.229550153f, -0.0656554326f }, { 12000, -0.243209839f, -0.191908702f }, { 25000, 0.174803019f, 0.125847161f }, { 35000, 0.0609342083f, 0.0861448273f } } }, // Autogun (LYNX engine, counter 0)
  7604. { "Templates/Advanced/Electronic/Breakbeat.flp", 43615, false, 1156.8126, 667.1251, { { 5000, -0.122245498f, -0.00968936551f }, { 12000, 0.088016659f, 0.0382440798f }, { 25000, -0.0438547991f, -0.0149139315f }, { 35000, -0.0162006877f, 0.0219070036f } } }, // Pad (Autogun_x64.dll)
  7605. { "Templates/Advanced/Electronic/Breakbeat.flp", 43615, true, 541.425656, 476.875716, { { 5000, 0.15942347f, 0.0947837308f }, { 12000, 0.172008425f, 0.197265282f }, { 25000, 0.0506084785f, 0.0015097193f }, { 35000, 0.0400503464f, 0.0195946041f } } }, // Pad (Autogun_x64.dll)
  7606. { "Templates/Advanced/Electronic/Chillout.flp", 171641, false, 1463.17228, 1370.65276, { { 5000, 0.158808231f, -0.128741398f }, { 12000, 0.208011121f, -0.153484732f }, { 25000, -0.184889361f, 0.0409393087f }, { 35000, 0.0255694874f, 0.0495453961f } } }, // Pad 5 (Autogun_x64.dll)
  7607. { "Templates/Advanced/Electronic/Chillout.flp", 171641, true, 1412.70901, 1469.84358, { { 5000, -0.0218960885f, -0.0802078545f }, { 12000, -0.458583713f, 0.146813422f }, { 25000, -0.198403969f, 0.0604776815f }, { 35000, -0.097854577f, 0.0806856826f } } }, // Pad 5 (Autogun_x64.dll)
  7608. { "Templates/Advanced/Electronic/HipHop-Trap.flp", 9787, false, 2807.37714, 3754.76445, { { 5000, -0.0391585156f, 0.332697481f }, { 12000, -0.128277287f, -0.632223666f }, { 25000, 0.103559501f, 0.521154523f }, { 35000, 0.309904903f, 0.280873239f } } }, // Pad (Autogun_x64.dll)
  7609. { "Templates/Advanced/Electronic/HipHop-Trap.flp", 9787, true, 3526.66329, 4199.20992, { { 5000, 0.256542385f, 0.0431160629f }, { 12000, -0.283133686f, -0.732375383f }, { 25000, -0.210154608f, -0.276185691f }, { 35000, -0.144943774f, -0.315269828f } } }, // Pad (Autogun_x64.dll)
  7610. { "Templates/Advanced/Electronic/Trap.flp", 9780, false, 2807.37714, 3754.76445, { { 5000, -0.0391585156f, 0.332697481f }, { 12000, -0.128277287f, -0.632223666f }, { 25000, 0.103559501f, 0.521154523f }, { 35000, 0.309904903f, 0.280873239f } } }, // Pad (Autogun_x64.dll)
  7611. { "Templates/Advanced/Electronic/Trap.flp", 9780, true, 3526.66329, 4199.20992, { { 5000, 0.256542385f, 0.0431160629f }, { 12000, -0.283133686f, -0.732375383f }, { 25000, -0.210154608f, -0.276185691f }, { 35000, -0.144943774f, -0.315269828f } } }, // Pad (Autogun_x64.dll)
  7612. };
  7613. int checked = 0;
  7614. for (const auto& g : goldens)
  7615. {
  7616. std::ifstream in (std::filesystem::path (realFlpDir) / g.file, std::ios::binary);
  7617. const std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  7618. if (bytes.empty())
  7619. continue;
  7620. std::vector<std::uint8_t> st;
  7621. for (const auto& c : flp::parseProject (bytes).channels)
  7622. if (c.pluginName == "Autogun" && st.empty())
  7623. st = c.pluginState;
  7624. CHECK (st.size() == g.stateSize);
  7625. CHECK (lynx::autogunStateUsable (st));
  7626. lynx::ogun::RefOptions opt;
  7627. opt.autogun = true;
  7628. opt.sgState = sgState;
  7629. opt.quality = g.export4 ? 4 : -1;
  7630. const auto y = lynx::ogun::renderReference (st, { { 0, 60, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 20000 },
  7631. { 8000, 67, 0.6, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 26000 },
  7632. { 16000, 55, 0.95, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 30000 } }, 44100.0, 39690, opt);
  7633. double el = 0.0, er = 0.0, peak = 0.0;
  7634. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  7635. 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]) });
  7636. bool ok = y.size() == 2 * 39690 && std::abs (el - g.el) <= 1e-5 * g.el && std::abs (er - g.er) <= 1e-5 * g.er;
  7637. for (const auto& smp : g.s)
  7638. 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;
  7639. if (! ok)
  7640. std::printf (" Autogun %s (q%s) differs: energy %.9g %.9g (golden %.9g %.9g)\n", g.file, g.export4 ? "4" : "-state", el, er, g.el, g.er);
  7641. CHECK (ok);
  7642. ++checked;
  7643. }
  7644. std::printf (" Autogun: %d template/render-test renders checked (DLL goldens for the fixed-spectrum states)\n", checked);
  7645. CHECK (checked == (int) std::size (goldens));
  7646. // the instrument (SG.dat read from ChannelDef::samplePath) plays like the reference renderer, then goes idle
  7647. {
  7648. std::ifstream in (std::filesystem::path (realFlpDir) / "Templates" / "Advanced" / "Electronic" / "Trap.flp", std::ios::binary);
  7649. const std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  7650. lynx::ChannelDef c;
  7651. c.instrument = lynx::InstrumentKind::autogun;
  7652. for (const auto& ch : flp::parseProject (bytes).channels)
  7653. if (ch.pluginName == "Autogun")
  7654. c.pluginState = ch.pluginState;
  7655. c.samplePath = sgPath.string();
  7656. lynx::ogun::RefOptions opt;
  7657. opt.autogun = true;
  7658. opt.sgState = sgState;
  7659. opt.tempo = 120.0; // the instrument is configured at 120 BPM
  7660. const auto ref = lynx::ogun::renderReference (c.pluginState, { { 0, 60, 0.75, 1.0, 0.0, 0.0, 0.0, 0.0, 0.5, 6144 } }, 44100.0, 16384, opt);
  7661. auto inst = lynx::makeInstrument (lynx::InstrumentKind::autogun);
  7662. inst->configure (c, 120.0);
  7663. inst->prepare (44100.0);
  7664. std::vector<float> l (16384), r (16384);
  7665. for (int s = 0; s < 16384; s += 512)
  7666. {
  7667. if (s == 0)
  7668. inst->noteOn (60, 0.75f);
  7669. if (s == 6144)
  7670. inst->noteOff (60);
  7671. inst->render (l.data() + s, r.data() + s, 512, 1.0f, 1.0f);
  7672. }
  7673. double worst = 0.0, peak = 0.0;
  7674. for (int i = 0; i < 16384; ++i)
  7675. 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]) }),
  7676. peak = std::max (peak, (double) std::abs (l[(std::size_t) i]));
  7677. if (worst != 0.0)
  7678. std::printf (" Autogun instrument vs reference: worst %.3g (peak %.3g)\n", worst, peak);
  7679. CHECK (ref.size() == 2 * 16384 && peak > 0.01 && worst == 0.0);
  7680. std::vector<float> tail (44100), tailR (tail.size());
  7681. for (int k = 0; k < 40 && ! inst->idle(); ++k)
  7682. inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
  7683. CHECK (inst->idle());
  7684. }
  7685. // the state variants: Ogun's parser rejects the version-19 Autogun layout, the comment length is one byte or 0xff + i32
  7686. std::ifstream rt (std::filesystem::path (realFlpDir) / "System" / "Render tests" / "Autogun.flp", std::ios::binary);
  7687. const std::vector<std::uint8_t> rtBytes ((std::istreambuf_iterator<char> (rt)), std::istreambuf_iterator<char>());
  7688. for (const auto& c : flp::parseProject (rtBytes).channels)
  7689. if (c.pluginName == "Autogun")
  7690. CHECK (lynx::autogunStateUsable (c.pluginState) && lynx::ogunParam (c.pluginState, 63) == 430);
  7691. }
  7692. TEST (harmless_rack)
  7693. {
  7694. // REA_rodada36: Harmless's effects rack against our reference model (rack.py / maxmodel.py, which match the
  7695. // installation's Harmless_x64.dll and SG_x64.dll), 44.1 kHz, host blocks of 512
  7696. std::vector<float> inL (8192), inR (8192);
  7697. for (int i = 0; i < 4096; ++i)
  7698. {
  7699. 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));
  7700. inR[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / 44100.0));
  7701. }
  7702. auto run = [&] (const std::array<int, 100>& p, int compType, std::shared_ptr<const lynx::hlfx::MaximusPreset> comp, int total, int voicesUntil) {
  7703. lynx::hlfx::Rack rack;
  7704. std::array<int, 7> t {};
  7705. t[3] = compType;
  7706. rack.prepare (p, t, 44100.0, 120.0, std::move (comp));
  7707. std::vector<float> l (inL.begin(), inL.begin() + total), r (inR.begin(), inR.begin() + total);
  7708. for (int s = 0; s < total; s += 512)
  7709. rack.process (l.data() + s, r.data() + s, std::min (512, total - s), s < voicesUntil);
  7710. return std::make_pair (l, r);
  7711. };
  7712. {
  7713. // chorus type 3 (mix 400), ping-pong delay type 2 (vol 600, feedback 500, 1/4 beat), reverb type 4 (vol 700)
  7714. std::array<int, 100> p {};
  7715. p[90] = 3, p[91] = 400, p[92] = 2, p[93] = 600, p[94] = 500, p[95] = 48, p[96] = 4, p[97] = 700;
  7716. const auto [l, r] = run (p, 0, nullptr, 8192, 4096);
  7717. const std::tuple<int, float, float> golden[] { { 0, 0.0f, 0.0f }, { 500, -0.25248247f, 0.15141529f }, { 3000, -0.65128994f, -0.64854866f },
  7718. { 5000, -0.54308629f, -0.36827448f }, { 6000, -0.98378551f, -0.72790027f },
  7719. { 8191, 0.50599128f, -0.43637198f } };
  7720. for (const auto& [i, gl, gr] : golden)
  7721. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  7722. }
  7723. {
  7724. // the delay alone: an impulse comes back after round(time·fs·0.3125/bpm) samples × (11^0.6 − 1)/10, then × feedback
  7725. std::array<int, 100> p {};
  7726. p[92] = 1, p[93] = 600, p[94] = 500, p[95] = 48;
  7727. lynx::hlfx::Rack rack;
  7728. rack.prepare (p, {}, 44100.0, 120.0, nullptr);
  7729. std::vector<float> l (16384), r (16384);
  7730. l[0] = 1.0f;
  7731. for (int s = 0; s < 16384; s += 512)
  7732. rack.process (l.data() + s, r.data() + s, 512, s == 0);
  7733. const auto D = 5512; // 48·44100·0.3125/120 = 5512.5 → 5512 (round half to even)
  7734. const auto vol = (float) ((std::exp ((double) (600 * 0.001f) * (double) (float) std::log (11.0)) - 1.0) * (double) 0.1f);
  7735. CHECK (std::abs (l[(std::size_t) D] - vol) < 1e-7f && std::abs (l[(std::size_t) D - 1]) < 1e-12f);
  7736. // the feedback ramps from 0.5 toward 0.5: the second echo is vol·0.5
  7737. CHECK (std::abs (l[(std::size_t) (2 * D)] - vol * 0.5f) < 1e-7f && std::abs (r[(std::size_t) D]) < 1e-12f);
  7738. }
  7739. // the compressor: Maximus states of our own (no preset data), checked against SG_x64.dll by REA round 36
  7740. using Env = std::vector<std::array<double, 4>>;
  7741. const Env def { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 } };
  7742. 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,
  7743. int sus = 2882) { return std::array<int, 13> { mode, pre, post, sep, sat, ceil, att, rel2, 2, 5000, 3, sus, 0 }; };
  7744. {
  7745. // master brickwall at −0.22 dB only (bands off / compression off)
  7746. const auto st = maximusTestState ({ 19800, 1, 48065, 1, 1115, 1000, -1 }, { band (3), band (1), band (3), band (0) },
  7747. { def, def, def, def, def, def, def, def }, 0);
  7748. const auto preset = lynx::hlfx::makeMaximusPreset (st);
  7749. CHECK (preset != nullptr);
  7750. std::array<int, 100> p {};
  7751. p[98] = 1000;
  7752. const auto [l, r] = run (p, 1, preset, 4096, 4096);
  7753. // SG_x64.dll's own output (EXEC, REA_rodada40: bit-identical to the port)
  7754. const std::tuple<int, float, float> golden[] { { 100, 0.024997942f, 0.88166064f }, { 1000, 0.56971401f, -0.62808102f },
  7755. { 2047, -0.27541628f, 0.709135f }, { 3000, 0.32431623f, -0.37985197f },
  7756. { 4095, -0.12743522f, -0.15237485f } };
  7757. for (const auto& [i, gl, gr] : golden)
  7758. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  7759. float peak = 0.0f;
  7760. for (int i = 2048; i < 4096; ++i)
  7761. peak = std::max ({ peak, std::abs (l[(std::size_t) i]), std::abs (r[(std::size_t) i]) });
  7762. CHECK (peak < 0.9751f);
  7763. }
  7764. {
  7765. // 24 dB crossovers, ×2 oversampling, low cut, stereo separation, saturation A, a knee with tension (type 7) and
  7766. // exponential tensions (type 0), release 2 on the master, band mix 600
  7767. const Env knee { { 0.0, 0.0, 0.0, 0 }, { 0.35, 0.35, 0.0, 0 }, { 0.65, 0.6, 0.36, 7 } };
  7768. const Env t0 { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.14, 0 }, { 0.5, 0.5, 0.0, 0 } };
  7769. const Env m0 { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 }, { 0.5, 0.6, 0.085, 0 } };
  7770. const auto st = maximusTestState ({ 19800, 1, 48065, 1, 891, 600, 32768 },
  7771. { band (1, 1264, 1296, 500, -500, 1200), band (0, 1340, 1000, -348, 0, 1000, 1115, 0, 1570),
  7772. band (0, 1364, 1152, 0, 0, 1000, 1115, 0, 1186), band (0, 1000, 1000, 0, -196, 1000, 1115, 4464, 1538) },
  7773. { def, def, knee, def, t0, def, m0, def }, 1);
  7774. const auto preset = lynx::hlfx::makeMaximusPreset (st);
  7775. CHECK (preset != nullptr);
  7776. std::array<int, 100> p {};
  7777. p[98] = 600;
  7778. const auto [l, r] = run (p, 2, preset, 4096, 4096);
  7779. // SG_x64.dll's own output (EXEC, REA_rodada40; the port is within −142 dB of it)
  7780. const std::tuple<int, float, float> golden[] { { 100, 0.0f, 0.0f }, { 1000, -0.46856543f, -0.63262129f },
  7781. { 2047, 0.20268451f, 0.54145539f }, { 3000, -0.25613472f, -0.49985155f },
  7782. { 4095, 0.10172445f, 0.050564945f } };
  7783. for (const auto& [i, gl, gr] : golden)
  7784. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  7785. }
  7786. {
  7787. // an .fst wrapper around a state is found; unsupported states are rejected
  7788. const auto st = maximusTestState ({ 19800, 1, 48065, 1, 1115, 1000, -1 }, { band (3), band (1), band (3), band (0) },
  7789. { def, def, def, def, def, def, def, def }, 0);
  7790. std::vector<std::uint8_t> fst { 'F', 'L', 'h', 'd', 6, 0, 0, 0, 0, 0, 1, 0, 0x60, 0, 'F', 'L', 'd', 't' };
  7791. std::vector<std::uint8_t> body { 0xC9, 8, 'M', 'a', 'x', 'i', 'm', 'u', 's', 0, 0xD5 };
  7792. for (auto n = st.size(); ; n >>= 7)
  7793. {
  7794. body.push_back ((std::uint8_t) ((n & 0x7f) | (n >= 0x80 ? 0x80 : 0)));
  7795. if (n < 0x80)
  7796. break;
  7797. }
  7798. body.insert (body.end(), st.begin(), st.end());
  7799. const auto len = (std::uint32_t) body.size();
  7800. for (int k = 0; k < 4; ++k)
  7801. fst.push_back ((std::uint8_t) (len >> (8 * k)));
  7802. fst.insert (fst.end(), body.begin(), body.end());
  7803. const auto found = lynx::hlfx::maximusStateFromFst (fst);
  7804. CHECK (found && *found == st);
  7805. auto rms = st;
  7806. rms[4 + 4 * (7 + 13 * 3 + 12)] = 1; // RMS detector on the master (decoded in REA_rodada40)
  7807. CHECK (lynx::hlfx::makeMaximusPreset (rms) != nullptr);
  7808. auto masterMid = st;
  7809. masterMid[0x1fc] = 1; // "Master Mid mode": not decoded
  7810. CHECK (lynx::hlfx::makeMaximusPreset (masterMid) == nullptr);
  7811. }
  7812. }
  7813. TEST (fruity_delay_bank_engine)
  7814. {
  7815. // REA_rodada56: Fruity Delay Bank, two states of our own against the installation's DLL (goldens = the DLL's output
  7816. // for the same input, EXEC; a new instance, FTZ/DAZ as the plugin's thread). Both are bit-identical.
  7817. lynx::ScopedFlushDenormals ftz;
  7818. struct Bank
  7819. {
  7820. std::array<int, 16> ctl;
  7821. lynx::fldbank::BankOptions opt;
  7822. };
  7823. auto build = [] (std::array<int, 4> globals, std::map<int, Bank> banks, int oversampling) {
  7824. auto s = lynx::fldbank::defaultState();
  7825. for (int i = 0; i < 4; ++i)
  7826. s.p[(std::size_t) i] = globals[(std::size_t) i];
  7827. s.p[132] = oversampling;
  7828. for (auto& [b, k] : banks)
  7829. {
  7830. for (int c = 0; c < 16; ++c)
  7831. if (16 * b + 4 + c < lynx::fldbank::automatable)
  7832. s.p[(std::size_t) (16 * b + 4 + c)] = k.ctl[(std::size_t) c];
  7833. s.bank[(std::size_t) b] = k.opt;
  7834. s.bank[(std::size_t) b].enabled = true;
  7835. }
  7836. return s;
  7837. };
  7838. auto opt = [] (int f1, int o1, int f2, int o2, bool after, int mode, int steps, int amount, bool tap, bool sync) {
  7839. lynx::fldbank::BankOptions o;
  7840. o.filterType[0] = f1;
  7841. o.filterOrder[0] = o1;
  7842. o.filterType[1] = f2;
  7843. o.filterOrder[1] = o2;
  7844. o.filterAfter = after;
  7845. o.mode = mode;
  7846. o.steps = steps;
  7847. o.amount = amount;
  7848. o.tapBeforeFeedback = tap;
  7849. o.sync = sync;
  7850. return o;
  7851. };
  7852. auto run = [] (const lynx::fldbank::State& s, double rate, int block, double bpm) {
  7853. std::vector<float> l (4096), r (4096);
  7854. for (int i = 0; i < 4096; ++i)
  7855. {
  7856. l[(std::size_t) i] = (float) (1.5 * std::sin (2.0 * 3.141592653589793 * 440.0 * i / rate) * std::min (1.0, i / 400.0));
  7857. r[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / rate));
  7858. }
  7859. lynx::fldbank::Engine e (s, rate, bpm, block);
  7860. for (int i = 0; i < 4096; i += block)
  7861. e.process (l.data() + i, r.data() + i, std::min (block, 4096 - i));
  7862. return std::make_pair (l, r);
  7863. };
  7864. auto check = [] (const std::pair<std::vector<float>, std::vector<float>>& out, const std::vector<std::tuple<int, float, float>>& golden) {
  7865. for (const auto& [i, gl, gr] : golden)
  7866. CHECK (out.first[(std::size_t) i] == gl && out.second[(std::size_t) i] == gr);
  7867. };
  7868. // controls: in pan, in vol, f1 cutoff, f1 res, f1 gain, fb pan, fb level, fb cross, time, offset, f2 cutoff, f2 res,
  7869. // f2 gain, out pan, out vol, send. Three banks: ping-pong with low-pass ×2 / peaking feedback filter, pattern and send;
  7870. // free time with a high shelf after the delay, band-pass feedback, negative feedback/cross, tap before the feedback;
  7871. // swapped outputs with a 3-section high-pass in the loop and a negative pattern shape.
  7872. const auto a = build ({ 100, 128, 120, 110 },
  7873. { { 0, { { -40, 140, 40000, 30000, 0, 30, 90, 50, 240, -200, 30000, 20000, 180, 20, 128, 100 }, opt (1, 1, 6, 0, false, 3, 4, 60, false, true) } },
  7874. { 1, { { 0, 128, 50000, 10000, 60, 0, -70, -40, 150, 150, 25000, 40000, 0, 0, -100, 0 }, opt (7, 0, 2, 0, true, 1, 0, 0, true, false) } },
  7875. { 2, { { 10, 110, 33145, 17500, 0, -60, 100, 0, 400, 0, 20000, 50000, 0, -30, 120, 0 }, opt (0, 0, 4, 2, false, 2, 3, -90, false, true) } } },
  7876. 0);
  7877. check (run (a, 44100.0, 512, 120.0), { { 100, -0.00294386433f, 0.0141299525f }, { 1000, -0.117360562f, -0.140237048f },
  7878. { 2047, 0.172598198f, -0.401046395f }, { 3000, 0.303020209f, -0.127763838f },
  7879. { 4095, 0.0093755126f, 0.241272509f } });
  7880. // 2× oversampling at 48 kHz in blocks of 300, low shelf before the delay, 2-section notch in the loop, a send into a
  7881. // ping-pong bank 3 (bank 1 and 2 off: no send) with a 2-step pattern
  7882. const auto b = build ({ 0, 128, 128, 128 },
  7883. { { 0, { { 0, 128, 45000, 20000, 200, 0, 80, 0, 180, 60, 28000, 30000, 0, 0, 128, 64 }, opt (5, 0, 3, 1, false, 1, 0, 0, false, true) } },
  7884. { 3, { { 64, 128, 33145, 17500, 0, 0, -64, 90, 100, -90, 33145, 17500, 0, 0, 128, 0 }, opt (0, 0, 0, 0, false, 3, 2, 0, true, true) } } },
  7885. 1);
  7886. check (run (b, 48000.0, 300, 93.0), { { 100, 0.0f, 0.0f }, { 1000, 0.0f, 0.0f }, { 2047, 0.201803684f, -0.327834547f },
  7887. { 3000, 0.412344277f, -0.101866335f }, { 4095, 0.204572573f, 0.71171385f } });
  7888. // FL's state round-trips through the import (version 15 times are converted), the globals are the slot's parameters
  7889. auto bytes = lynx::fldbank::serialise (a);
  7890. const auto slot = lynx::effectFromFl ("Fruity Delay Bank", bytes);
  7891. CHECK (slot && slot->kind == lynx::EffectKind::fruityDelayBank && slot->state == bytes);
  7892. CHECK (slot && slot->params[0] == 100.0f / 128.0f && slot->params[3] == 110.0f / 128.0f);
  7893. bytes[0] = 15;
  7894. const auto v15 = lynx::fldbank::parse (bytes);
  7895. CHECK (v15 && v15->p[12] == 2400 && v15->p[28] == 1500);
  7896. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityDelayBank, 3, 0.5f, 120.0) == "50%");
  7897. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityDelayBank, 0, 1.0f, 120.0) == "0.0 dB");
  7898. CHECK (! lynx::fldbank::parse ({ 16, 0, 0, 0 }));
  7899. // the LYNX effect (settled ramps, 512-frame engine blocks) runs from the slot
  7900. auto fx = lynx::makeEffect (*slot);
  7901. fx->prepare (44100.0);
  7902. std::vector<float> l (1024, 0.5f), r (1024, -0.25f);
  7903. fx->process (l.data(), r.data(), 1024, slot->params, 120.0);
  7904. CHECK (std::isfinite (l[1023]) && std::isfinite (r[1023]));
  7905. }
  7906. TEST (fruity_multiband_engine)
  7907. {
  7908. // REA_rodada56: Fruity Multiband Compressor, states of our own against the installation's DLL
  7909. // (goldens = the DLL's output for the same input, EXEC; blocks of 512 at 44.1 kHz; all three bit-identical)
  7910. std::vector<float> inL (4096), inR (4096);
  7911. for (int i = 0; i < 4096; ++i)
  7912. {
  7913. 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)
  7914. + 0.5 * std::sin (2.0 * 3.141592653589793 * 90.0 * i / 44100.0));
  7915. inR[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / 44100.0)
  7916. + 0.4 * std::sin (2.0 * 3.141592653589793 * 7000.0 * i / 44100.0));
  7917. }
  7918. auto multiband = [] (std::array<int, 28> c) {
  7919. std::vector<std::uint8_t> b;
  7920. auto put = [&b] (std::int32_t v) {
  7921. std::uint8_t q[4];
  7922. std::memcpy (q, &v, 4);
  7923. b.insert (b.end(), q, q + 4);
  7924. };
  7925. put (0);
  7926. for (auto v : c)
  7927. put (v);
  7928. put (0); // UI words of the state (not used by the engine)
  7929. put (50);
  7930. return b;
  7931. };
  7932. struct Out { std::vector<float> l, r; int latency = 0; };
  7933. auto run = [&] (const std::vector<std::uint8_t>& st) {
  7934. const auto s = lynx::flmb::parse (st);
  7935. CHECK (s.has_value());
  7936. lynx::EffectParams p {};
  7937. lynx::flmb::toParams (*s, p.data());
  7938. auto fx = lynx::flmb::make();
  7939. fx->prepare (44100.0);
  7940. Out o { inL, inR };
  7941. for (int s0 = 0; s0 < 4096; s0 += 512)
  7942. fx->process (o.l.data() + s0, o.r.data() + s0, 512, p, 120.0);
  7943. o.latency = fx->latencySamples();
  7944. return o;
  7945. };
  7946. auto check = [] (const Out& out, const std::vector<std::tuple<int, float, float>>& golden) {
  7947. for (const auto& [i, gl, gr] : golden)
  7948. CHECK (std::abs (out.l[(std::size_t) i] - gl) < 2e-6f && std::abs (out.r[(std::size_t) i] - gr) < 2e-6f);
  7949. };
  7950. // IIR crossovers, limiter on, high band compressing, mid bypassed, low compressing, +2 dB output
  7951. const auto oa = run (multiband ({ 42598, 1, 0, 0, 15604, 25782, 53524, 32768, 11, 298, 51118, 2, 14994, 5037, 24736, 55755, 26870, 221, 1090, 49807, 0, 5037, 21845, 47074, 32768, 550, 1090, 40632 }));
  7952. CHECK (oa.latency == 0);
  7953. check (oa,
  7954. { { 100, 0.3061366081237793f, 0.14554384350776672f }, { 1000, 0.4117552638053894f, 0.3191826045513153f }, { 2047, -0.46868836879730225f, -0.5676097869873047f }, { 3000, 0.24988797307014465f, -0.42287692427635193f }, { 4095, -0.022194014862179756f, 0.8433372378349304f } });
  7955. // linear phase (FIR, 52 frames of latency), overlapping mid transitions, low band off, limiter off
  7956. const auto ob = run (multiband ({ 32768, 0, 1, 0, 12000, 21845, 40000, 50000, 300, 5000, 30000, 0, 4000, 3600, 30000, 45875, 60000, 50, 500, 52429, 1, 6500, 21845, 45875, 32768, 100, 1000, 52429 }));
  7957. CHECK (ob.latency == 52);
  7958. check (ob,
  7959. { { 100, 0.0176423080265522f, -0.22197140753269196f }, { 1000, -0.002046747598797083f, 0.021436547860503197f }, { 2047, -0.010928931646049023f, -0.08079832047224045f }, { 3000, 0.027733327820897102f, -0.026119394227862358f }, { 4095, 0.07469408959150314f, -0.08492052555084229f } });
  7960. // linear phase, hard compression everywhere, limiter on, mid crossovers crossed (the FIR mid band is silent)
  7961. const auto oc = run (multiband ({ 50000, 1, 1, 0, 17000, 30000, 30000, 65536, 1, 100, 0, 0, 3000, 9000, 30000, 20000, 65536, 1, 100, 65536, 0, 6500, 30000, 25000, 65536, 1, 100, 10000 }));
  7962. CHECK (oc.latency == 52);
  7963. check (oc,
  7964. { { 100, 0.028349297121167183f, -0.02347063086926937f }, { 1000, -0.00018560001626610756f, 0.0079029630869627f }, { 2047, -0.00953709613531828f, -0.00774149876087904f }, { 3000, 0.019576910883188248f, 0.006520440336316824f }, { 4095, 0.03874344751238823f, -0.000356332486262545f } });
  7965. // the slot's parameters carry FL's raw controls exactly; after the project file's 4-decimal rounding the
  7966. // effect built with FL's state still uses the exact controls
  7967. const auto st = multiband ({ 42598, 1, 1, 0, 15604, 25782, 53524, 32768, 11, 298, 51118, 0, 14994, 5037, 24736, 55755, 26870, 221,
  7968. 1090, 49807, 0, 5037, 21845, 47074, 32768, 550, 1090, 40632 });
  7969. const auto parsed = lynx::flmb::parse (st);
  7970. CHECK (parsed && parsed->c[4] == 15604);
  7971. lynx::EffectParams p {};
  7972. lynx::flmb::toParams (*parsed, p.data());
  7973. for (int i = 0; i < lynx::flmb::controls; ++i)
  7974. CHECK (lynx::flmb::raw (p[(std::size_t) i], lynx::flmb::controlMin[(std::size_t) i], lynx::flmb::controlMax[(std::size_t) i]) == parsed->c[(std::size_t) i]);
  7975. lynx::EffectParams rounded = p;
  7976. for (auto& v : rounded)
  7977. v = std::round (v * 10000.0f) / 10000.0f;
  7978. std::vector<float> l1 (inL), r1 (inR), l2 (inL), r2 (inR);
  7979. auto exact = lynx::flmb::make();
  7980. auto fromState = lynx::flmb::make (st);
  7981. exact->prepare (44100.0);
  7982. fromState->prepare (44100.0);
  7983. for (int s0 = 0; s0 < 4096; s0 += 512)
  7984. {
  7985. exact->process (l1.data() + s0, r1.data() + s0, 512, p, 120.0);
  7986. fromState->process (l2.data() + s0, r2.data() + s0, 512, rounded, 120.0);
  7987. }
  7988. CHECK (l1 == l2 && r1 == r2);
  7989. CHECK (lynx::flmb::paramText (4, p[4]) == "6302 Hz");
  7990. CHECK (lynx::flmb::paramText (3, 0.5f) == "off");
  7991. // the FL import builds the exact effect with FL's state kept
  7992. const auto slot = lynx::effectFromFl ("Fruity Multiband Compressor", st);
  7993. CHECK (slot && slot->kind == lynx::EffectKind::fruityMultiband && slot->state == st && slot->params[4] == p[4]);
  7994. // unknown versions are rejected
  7995. CHECK (! lynx::flmb::parse ({ 1, 0, 0, 0 }));
  7996. }
  7997. // REA_rodada54: input of the stereo-effect goldens (the installation DLLs' output for the same input, EXEC)
  7998. static std::pair<std::vector<float>, std::vector<float>> stereoFxInput()
  7999. {
  8000. std::vector<float> l (4096), r (4096);
  8001. for (int i = 0; i < 4096; ++i)
  8002. {
  8003. l[(std::size_t) i] = (float) (1.5 * std::sin (2.0 * 3.141592653589793 * 440.0 * i / 44100.0) * std::min (1.0, i / 400.0));
  8004. r[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / 44100.0));
  8005. }
  8006. return { l, r };
  8007. }
  8008. static std::vector<std::uint8_t> i32State (std::initializer_list<int> words)
  8009. {
  8010. std::vector<std::uint8_t> d;
  8011. for (const auto w : words)
  8012. for (int b = 0; b < 4; ++b)
  8013. d.push_back ((std::uint8_t) ((std::uint32_t) w >> (8 * b)));
  8014. return d;
  8015. }
  8016. static void checkStereoFx (lynx::EffectSlot slot, const std::vector<std::tuple<int, float, float>>& golden)
  8017. {
  8018. auto [l, r] = stereoFxInput();
  8019. auto fx = lynx::makeEffect (slot);
  8020. fx->prepare (44100.0);
  8021. for (int s = 0; s < 4096; s += 512)
  8022. fx->process (l.data() + s, r.data() + s, 512, slot.params, 120.0);
  8023. for (const auto& [i, gl, gr] : golden)
  8024. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  8025. }
  8026. TEST (stereo_shaper_engine)
  8027. {
  8028. // Fruity Stereo Shaper (REA_rodada54 §2): matrix with cross terms, delay on R, all-pass on L, effects post / pre
  8029. auto slot = lynx::effectFromFl ("Fruity Stereo Shaper", i32State ({ 0, -3050, 12800, 9000, 2000, 1408, -2496, 0, 1 }));
  8030. CHECK (slot && slot->kind == lynx::EffectKind::stereoShaper);
  8031. if (slot)
  8032. checkStereoFx (*slot, { { 100, 0.0438031591f, 0.0f }, { 1000, 1.11109531f, -0.781641483f }, { 2047, -0.73191303f, 0.483852893f },
  8033. { 3000, 0.950346172f, -0.769737065f }, { 4095, 0.251555145f, 0.153306276f } });
  8034. slot = lynx::effectFromFl ("Fruity Stereo Shaper", i32State ({ 0, 0, 12800, 12800, 0, -2656, 2184, 0, 0 }));
  8035. CHECK (slot.has_value());
  8036. if (slot)
  8037. checkStereoFx (*slot, { { 100, 0.0f, -0.104282036f }, { 1000, 0.0f, 1.08441091f }, { 2047, 0.463494211f, -0.93754214f },
  8038. { 3000, -1.49942112f, 0.820726812f }, { 4095, -1.31784308f, -0.057449434f } });
  8039. // controls round-trip through the slot parameters; the Patcher's difference bus is the same engine flagged
  8040. const auto st = lynx::stereofx::parseStereoShaper (i32State ({ 0, -3050, 12800, 9000, 2000, 1408, -2496, 2, 1 }));
  8041. CHECK (st.has_value());
  8042. if (st)
  8043. {
  8044. lynx::EffectParams p {};
  8045. lynx::stereofx::stereoShaperToParams (*st, p.data());
  8046. const auto back = lynx::stereofx::stereoShaperFromParams (p.data());
  8047. CHECK (back.rightToLeft == -3050 && back.left == 12800 && back.right == 9000 && back.leftToRight == 2000 && back.delay == 1408
  8048. && back.phase == -2496 && back.post == 1);
  8049. const auto head = lynx::patcherBranchHead ("Fruity Stereo Shaper", i32State ({ 0, -3050, 12800, 9000, 2000, 1408, -2496, 2, 1 }), 2);
  8050. CHECK (head.valid && head.slot && head.slot->kind == lynx::EffectKind::stereoShaper && head.slot->params[7] == 1.0f);
  8051. }
  8052. CHECK (! lynx::stereofx::parseStereoShaper ({ 1, 2, 3 }));
  8053. }
  8054. TEST (frequency_splitter_engine)
  8055. {
  8056. // Frequency Splitter (REA_rodada62): our own states through Frequency Splitter_x64.dll from its creation (gains
  8057. // ramping up from 0, FFT response faded in), blocks of 512 at 44.1 kHz; every band sent to its own destination
  8058. // (1 low, 2 mid, 3 high). IIR modes are bit-exact; the FFT mode's residual is the plugin's single-precision FFT.
  8059. using Gold = std::vector<std::tuple<int, float, float>>;
  8060. struct Case
  8061. {
  8062. std::vector<int> words;
  8063. int mute, latency;
  8064. std::array<Gold, 4> gold; // low, mid, high sends, summed output
  8065. };
  8066. const std::vector<Case> cases {
  8067. { { 19800, 48065, 3, 3, 0, 1, 2, 45820, 0, 0, 0, 300, -600, 0, 1, 2, 3 }, 0, 0, { Gold { { 100, 0.0090178391f, 0.0807745308f }, { 1000, 0.0443177596f, 0.000449771265f }, { 2047, -0.0277729966f, -0.00895843748f }, { 3000, 0.0306171384f, -0.00373467989f }, { 4095, 0.00322337216f, 0.00886098202f } }, Gold { { 100, 0.121696919f, -0.131051332f }, { 1000, 1.03903234f, 0.0533506982f }, { 2047, -0.651134431f, -1.06304717f }, { 3000, 0.717791438f, -0.443113774f }, { 4095, 0.0754866973f, 1.05140531f } }, Gold { { 100, 1.72075161e-05f, -0.00040520617f }, { 1000, 0.000452454377f, 0.000117809883f }, { 2047, -0.000283558591f, -0.00234806887f }, { 3000, 0.000312690681f, -0.00097864517f }, { 4095, 3.28165661e-05f, 0.00232247845f } }, Gold { { 100, 0.100601904f, 0.0864751786f }, { 1000, 0.788743436f, 0.0366551876f }, { 2047, -0.494284719f, -0.730370402f }, { 3000, 0.544887185f, -0.304443926f }, { 4095, 0.0573106967f, 0.722373366f } } } }, // A
  8068. { { 30000, 48065, 2, 3, 0, 0, 2, 45820, -900, 0, 0, 0, 0, 900, 1, 2, 3 }, 8, 0, { Gold { { 100, -0.06870462f, 0.0430903696f }, { 1000, -0.581374586f, -0.0143744675f }, { 2047, 0.450544685f, 0.249298006f }, { 3000, -0.47477895f, 0.101792879f }, { 4095, -0.220592439f, -0.244846866f } }, Gold { { 100, 0.0f, 0.0f }, { 1000, 0.0f, 0.0f }, { 2047, 0.0f, 0.0f }, { 3000, 0.0f, 0.0f }, { 4095, 0.0f, 0.0f } }, Gold { { 100, -0.0655785128f, 0.674460888f }, { 1000, -0.335665613f, -1.06249368f }, { 2047, 0.535273552f, 0.467255145f }, { 3000, -0.507586002f, -0.989780664f }, { 4095, -0.696575642f, 0.500667155f } }, Gold { { 100, -0.293243587f, 3.01594687f }, { 1000, -1.50097573f, -4.7510891f }, { 2047, 2.39354587f, 2.08939266f }, { 3000, -2.26973724f, -4.42593384f }, { 4095, -3.11482906f, 2.23879886f } } } }, // B
  8069. { { 40000, 10000, 0, 7, 0, 1, 2, 45820, 0, 200, 0, 0, 0, 0, 1, 2, 3 }, 0, 0, { Gold { { 100, 0.0171653964f, 0.0719543025f }, { 1000, 0.260783046f, -0.0546497703f }, { 2047, -0.236395031f, -0.087237671f }, { 3000, 0.242068797f, -0.103661537f }, { 4095, 0.169910118f, 0.140984371f } }, Gold { { 100, 0.00201279344f, -1.5109539f }, { 1000, 0.290754616f, 1.94614983f }, { 2047, -1.13570976f, -1.65917897f }, { 3000, 1.00995052f, 1.43215704f }, { 4095, 1.99373484f, -0.085176453f } }, Gold { { 100, -0.000444336678f, -0.000979390694f }, { 1000, -5.45688827e-08f, 7.50747904e-06f }, { 2047, 3.00044505e-08f, -6.34059734e-06f }, { 3000, 6.5076847e-08f, 5.3382596e-06f }, { 4095, -2.39666864e-08f, -2.14470973e-07f } }, Gold { { 100, 0.0178572834f, -0.781959713f }, { 1000, 0.424914241f, 1.04396117f }, { 2047, -0.877504408f, -1.02385247f }, { 3000, 0.812187135f, 0.704798162f }, { 4095, 1.2953757f, 0.0929019526f } } } }, // C
  8070. { { 15000, 45000, 1, 4, 0, 1, 2, 45820, 0, 0, 0, -1800, 0, 1800, 1, 2, 3 }, 2, 0, { Gold { { 100, -0.00273601874f, 0.0286290813f }, { 1000, 0.0418281667f, 0.0362898856f }, { 2047, -0.0745894462f, 0.00119503727f }, { 3000, 0.069945626f, 0.0419373475f }, { 4095, 0.103130348f, -0.0348644778f } }, Gold { { 100, 0.0f, 0.0f }, { 1000, 0.0f, 0.0f }, { 2047, 0.0f, 0.0f }, { 3000, 0.0f, 0.0f }, { 4095, 0.0f, 0.0f } }, Gold { { 100, 1.42701356e-05f, -0.000351782364f }, { 1000, 2.80272197e-05f, 0.000625129556f }, { 2047, -4.9891918e-05f, 2.06105178e-05f }, { 3000, 4.68442995e-05f, 0.000722356897f }, { 4095, 6.91349633e-05f, -0.000600588392f } }, Gold { { 100, 0.0475500524f, -0.553698778f }, { 1000, 0.508395135f, 0.997257531f }, { 2047, -0.906660676f, 0.0328744054f }, { 3000, 0.850252867f, 1.15249836f }, { 4095, 1.25358307f, -0.958129764f } } } }, // D
  8071. { { 22000, 50000, 6, 5, 0, 1, 2, 45820, 0, 0, 0, 0, 0, 0, 1, 2, 3 }, 0, 0, { Gold { { 100, 3.86351439e-06f, 0.000115638592f }, { 1000, -0.00223262212f, -4.85793389e-05f }, { 2047, 0.000411115587f, -2.57531383e-06f }, { 3000, -0.000485932804f, 7.87844056e-06f }, { 4095, 0.000189639119f, -4.06740628e-06f } }, Gold { { 100, -0.00987921655f, -0.683375061f }, { 1000, -0.876032948f, 1.17169452f }, { 2047, 0.424173862f, -0.340972483f }, { 3000, -0.499300569f, 1.1738565f }, { 4095, 0.195038065f, -0.732422411f } }, Gold { { 100, -2.29351427e-09f, -9.91509751e-07f }, { 1000, 2.00191561e-08f, 1.13525857e-06f }, { 2047, 2.50544758e-07f, -4.20314734e-07f }, { 3000, 1.65429199e-07f, 1.16024421e-06f }, { 4095, -7.31824983e-08f, -5.04818445e-07f } }, Gold { { 100, -0.00987535529f, -0.683260441f }, { 1000, -0.87826556f, 1.17164707f }, { 2047, 0.424585223f, -0.340975463f }, { 3000, -0.499786317f, 1.17386556f }, { 4095, 0.195227623f, -0.732426941f } } } }, // E
  8072. { { 25000, 48065, 3, 3, 1, 0, 1, 30000, 0, 0, 0, 0, 0, 0, 1, 2, 3 }, 0, 768, { Gold { { 100, 0.0f, 0.0f }, { 1000, 0.119619228f, 0.00655745994f }, { 2047, -0.258448333f, 0.0300254505f }, { 3000, 0.257131875f, 0.0218825918f }, { 4095, 0.243620604f, -0.0373107828f } }, Gold { { 100, 0.0f, 0.0f }, { 1000, 0.0f, 0.0f }, { 2047, 0.0f, 0.0f }, { 3000, 0.0f, 0.0f }, { 4095, 0.0f, 0.0f } }, Gold { { 100, 0.0f, 0.0f }, { 1000, 0.679369271f, 0.20265615f }, { 2047, -1.23796761f, 0.901739538f }, { 3000, 1.23174798f, 0.658331215f }, { 4095, 1.16627932f, -1.12146437f } }, Gold { { 100, 0.0f, 0.0f }, { 1000, 0.798988521f, 0.209213614f }, { 2047, -1.49641597f, 0.93176496f }, { 3000, 1.48887992f, 0.680213809f }, { 4095, 1.40989995f, -1.15877521f } } } }, // F
  8073. { { 20000, 45000, 2, 6, 1, 1, 1, 10000, 0, 0, 0, 0, 0, 0, 1, 2, 3 }, 0, 768, { Gold { { 100, 0.0f, 0.0f }, { 1000, 0.0390846208f, 0.00320863165f }, { 2047, -0.0802573487f, 0.0139643671f }, { 3000, 0.0798483118f, 0.0101885898f }, { 4095, 0.0756546259f, -0.0173618793f } }, Gold { { 100, 0.0f, 0.0f }, { 1000, 0.75990212f, 0.206010818f }, { 2047, -1.41615629f, 0.91779983f }, { 3000, 1.40902948f, 0.670024157f }, { 4095, 1.33424342f, -1.14141202f } }, Gold { { 100, 0.0f, 0.0f }, { 1000, 1.7614849e-06f, -5.86210172e-06f }, { 2047, -2.34208687e-06f, 7.51287303e-07f }, { 3000, 2.19281083e-06f, 1.06230493e-06f }, { 4095, 1.8608597e-06f, -1.35364621e-06f } }, Gold { { 100, 0.0f, 0.0f }, { 1000, 0.798988521f, 0.2092136f }, { 2047, -1.49641597f, 0.93176496f }, { 3000, 1.48887992f, 0.680213809f }, { 4095, 1.40989995f, -1.15877521f } } } }, // G
  8074. };
  8075. auto [l, r] = stereoFxInput();
  8076. for (const auto& c : cases)
  8077. {
  8078. auto words = c.words;
  8079. words.insert (words.begin(), 4);
  8080. for (const int w : { 0, 0, 0, c.mute })
  8081. words.push_back (w);
  8082. std::vector<std::uint8_t> st;
  8083. for (const auto w : words)
  8084. for (int b = 0; b < 4; ++b)
  8085. st.push_back ((std::uint8_t) ((std::uint32_t) w >> (8 * b)));
  8086. st.push_back (0);
  8087. st.resize (st.size() + 24, 0);
  8088. const auto s = lynx::fsplit::parseState (st);
  8089. CHECK (s.has_value());
  8090. if (! s)
  8091. continue;
  8092. lynx::fsplit::Engine e;
  8093. for (int i = 0; i < lynx::fsplit::engineParams; ++i)
  8094. e.setParameter (i, s->params[(std::size_t) i]);
  8095. e.prepare (44100.0);
  8096. e.reset (false);
  8097. std::array<std::vector<float>, 8> o;
  8098. for (auto& v : o)
  8099. v.assign (4096, 0.0f);
  8100. for (int b = 0; b < 4096; b += 512)
  8101. {
  8102. float* out[8];
  8103. for (int k = 0; k < 8; ++k)
  8104. out[k] = o[(std::size_t) k].data() + b;
  8105. e.process (l.data() + b, r.data() + b, out, 512);
  8106. }
  8107. CHECK (e.latencySamples() == c.latency);
  8108. const auto tol = c.latency > 0 ? 2e-5f : 2e-6f;
  8109. for (int k = 0; k < 4; ++k)
  8110. for (const auto& [i, gl, gr] : c.gold[(std::size_t) k])
  8111. CHECK (std::abs (o[(std::size_t) (2 * k)][(std::size_t) i] - gl) <= tol * std::max (1.0f, std::abs (gl))
  8112. && std::abs (o[(std::size_t) (2 * k + 1)][(std::size_t) i] - gr) <= tol * std::max (1.0f, std::abs (gr)));
  8113. // the mixer effect: the summed output (FL's main output) of an instance that has been running, and a
  8114. // Patcher bus = the bands sent to it
  8115. auto slot = lynx::effectFromFl ("Frequency Splitter", st);
  8116. CHECK (slot && slot->kind == lynx::EffectKind::frequencySplitter);
  8117. const auto head = lynx::patcherBranchHead ("Frequency Splitter", st, 3);
  8118. CHECK (head.valid && head.slot && head.slot->params[lynx::fsplit::outputHigh] == 1.0f
  8119. && head.slot->params[lynx::fsplit::outputLow] == 0.0f);
  8120. if (slot && head.slot)
  8121. {
  8122. lynx::fsplit::Engine settled;
  8123. for (int i = 0; i < lynx::fsplit::engineParams; ++i)
  8124. settled.setParameter (i, s->params[(std::size_t) i]);
  8125. settled.prepare (44100.0);
  8126. settled.reset (true);
  8127. std::vector<float> sl (4096), sr (4096), hl (4096), hr (4096);
  8128. float* out[8] { nullptr, nullptr, nullptr, nullptr, hl.data(), hr.data(), sl.data(), sr.data() };
  8129. settled.process (l.data(), r.data(), out, 4096);
  8130. const lynx::EffectSlot* both[2] { &*slot, &*head.slot };
  8131. for (const auto* p : both)
  8132. {
  8133. auto fx = lynx::makeEffect (*p);
  8134. fx->prepare (44100.0);
  8135. auto ml = l, mr = r;
  8136. for (int b = 0; b < 4096; b += 300)
  8137. fx->process (ml.data() + b, mr.data() + b, std::min (300, 4096 - b), p->params, 120.0);
  8138. const auto& wl = p == &*slot ? sl : hl;
  8139. const auto& wr = p == &*slot ? sr : hr;
  8140. CHECK (ml == wl && mr == wr);
  8141. CHECK (fx->latencySamples() == c.latency);
  8142. }
  8143. }
  8144. }
  8145. CHECK (! lynx::patcherBranchHead ("Frequency Splitter", std::vector<std::uint8_t> (0x58, 0), 2).valid); // no band sent to bus 2
  8146. CHECK (lynx::fsplit::paramText (2, 3.0f / 7.0f) == "24 dB/oct" && lynx::fsplit::paramText (11, 0.5f) == "0.0 dB");
  8147. CHECK (lynx::fsplit::paramText (0, 0.0f) == "30.0 Hz" && lynx::fsplit::paramText (4, 1.0f) == "FFT");
  8148. }
  8149. TEST (stereo_enhancer_transient_spreader_engines)
  8150. {
  8151. // REA_rodada54 §3–5: own states against the installation's DLLs (goldens = the DLL's output, EXEC)
  8152. auto se = lynx::effectFromFl ("Fruity Stereo Enhancer", i32State ({ -18, 300, -54, -148, 1, 1 }));
  8153. CHECK (se && se->kind == lynx::EffectKind::stereoEnhancer);
  8154. if (se)
  8155. checkStereoFx (*se, { { 100, 0.0f, 0.00309863733f }, { 1000, 0.0101111531f, -0.110266283f }, { 2047, -0.688752949f, -1.93998075f },
  8156. { 3000, -0.283846498f, -0.739818454f }, { 4095, 0.191147864f, 2.70425582f } });
  8157. auto tpState = i32State ({ 1, 138, 116, 64, 90, 2, 0, 70, 100, 1 });
  8158. tpState.push_back (5);
  8159. for (const auto w : i32State ({ 0, 50 }))
  8160. tpState.push_back (w);
  8161. auto tp = lynx::effectFromFl ("Transient Processor", tpState);
  8162. CHECK (tp && tp->kind == lynx::EffectKind::transientProcessor);
  8163. if (tp)
  8164. checkStereoFx (*tp, { { 100, 0.0568947345f, 0.124077775f }, { 1000, -0.326598823f, -0.193128616f }, { 2047, 0.612462878f, -0.0517415144f },
  8165. { 3000, -0.535302937f, -0.446303189f }, { 4095, -0.634670973f, 0.316044629f } });
  8166. auto spState = i32State ({ 0x10003, 7861, 3215, 106000, 1, 1, 0 });
  8167. spState.push_back (1);
  8168. spState.push_back (1);
  8169. auto sp = lynx::effectFromFl ("Spreader", spState);
  8170. CHECK (sp && sp->kind == lynx::EffectKind::spreader);
  8171. if (sp)
  8172. checkStereoFx (*sp, { { 100, 0.11841587f, -0.0958916694f }, { 1000, 1.40852344f, 0.087452054f }, { 2047, 1.07267976f, -2.02493715f },
  8173. { 3000, -0.781082511f, 0.811192274f }, { 4095, -1.80673707f, 0.89967829f } });
  8174. // unknown layouts keep the earlier fallback; FL automation maps one to one
  8175. CHECK (! lynx::stereofx::parseSpreader (i32State ({ 0x10001, 1, 2, 3, 4, 5 })));
  8176. CHECK (! lynx::stereofx::parseTransientProcessor (i32State ({ 2, 1, 2, 3, 4, 5, 6, 7, 8, 9 })));
  8177. const auto map = lynx::effectParamFromFl ("Transient Processor", 6);
  8178. CHECK (map && map->param == 6 && map->convert (0.25f) == 0.25f);
  8179. }
  8180. TEST (fruity_dynamics_engines)
  8181. {
  8182. // REA_rodada48: Fruity Limiter / Fruity Compressor, states of our own against the installation's DLLs
  8183. // (goldens = the DLL's output for the same input, EXEC; blocks of 512 at 44.1 kHz)
  8184. std::vector<float> inL (4096), inR (4096), side (4096);
  8185. for (int i = 0; i < 4096; ++i)
  8186. {
  8187. 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));
  8188. inR[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / 44100.0));
  8189. side[(std::size_t) i] = (float) (0.9 * std::sin (2.0 * 3.141592653589793 * 55.0 * i / 44100.0));
  8190. }
  8191. auto put = [] (std::vector<std::uint8_t>& b, std::int32_t v) {
  8192. std::uint8_t q[4];
  8193. std::memcpy (q, &v, 4);
  8194. b.insert (b.end(), q, q + 4);
  8195. };
  8196. auto limiterState = [&] (std::array<int, 18> p, int k = 0, int exact = 0) {
  8197. std::vector<std::uint8_t> b;
  8198. put (b, 7);
  8199. for (auto v : p)
  8200. put (b, v);
  8201. b.resize (0xA4, 0);
  8202. put (b, k);
  8203. b.push_back ((std::uint8_t) exact);
  8204. return b;
  8205. };
  8206. auto compressorState = [&] (int version, std::array<int, 6> p, int flags, int k = 0) {
  8207. std::vector<std::uint8_t> b;
  8208. put (b, version);
  8209. for (auto v : p)
  8210. put (b, v);
  8211. put (b, flags);
  8212. if (version > 2)
  8213. put (b, k);
  8214. return b;
  8215. };
  8216. struct Out { std::vector<float> l, r; int latency = 0; };
  8217. auto run = [&] (const lynx::EffectSlot& slot, bool sidechain) {
  8218. auto fx = lynx::makeEffect (slot);
  8219. fx->prepare (44100.0);
  8220. Out o { inL, inR };
  8221. for (int s = 0; s < 4096; s += 512)
  8222. {
  8223. if (sidechain)
  8224. fx->setSidechain (side.data() + s, side.data() + s);
  8225. fx->process (o.l.data() + s, o.r.data() + s, 512, slot.params, 120.0);
  8226. }
  8227. o.latency = fx->latencySamples();
  8228. return o;
  8229. };
  8230. auto check = [] (const Out& out, const std::vector<std::tuple<int, float, float>>& golden) {
  8231. for (const auto& [i, gl, gr] : golden)
  8232. CHECK (std::abs (out.l[(std::size_t) i] - gl) < 2e-6f && std::abs (out.r[(std::size_t) i] - gr) < 2e-6f);
  8233. };
  8234. auto limiter = [] (const std::vector<std::uint8_t>& st) {
  8235. auto slot = lynx::effectFromFl ("Fruity Limiter", st);
  8236. CHECK (slot && slot->kind == lynx::EffectKind::fruityLimiter);
  8237. return *slot;
  8238. };
  8239. // look-ahead limiter: +4 dB gain into a −2.5 dB ceiling, 2 ms attack (88 samples of latency); bit-identical
  8240. const auto a = run (limiter (limiterState ({ 1300, 1000, 800, 1115, 3, 5000, 3, 2882, 1000, 0, 0, 0, 6666, 1, 650, 1000, 0, 4187 })), false);
  8241. CHECK (a.latency == 88);
  8242. check (a, { { 100, 0.0176312141f, 0.621841073f }, { 1000, 0.401142687f, -0.442239583f }, { 2047, -0.19392401f, 0.499310732f },
  8243. { 3000, 0.22835508f, -0.267458469f }, { 4095, -0.089728713f, -0.107289009f } });
  8244. // compressor section with a positive knee, 2 compressor curve stages and soft saturation (port within −143 dB)
  8245. check (run (limiter (limiterState ({ 1000, 900, 1000, 0, 3, 5000, 3, 2882, 400, 600, 400, 1115, 6666, 2, 650, 1000, 0, 4187 })), false),
  8246. { { 100, -0.00208989787f, 0.0100310864f }, { 1000, -0.0545219742f, -0.0651496574f }, { 2047, 0.177322432f, -0.230846375f },
  8247. { 3000, -0.159136176f, -0.183688983f }, { 4095, -0.300143838f, 0.299418598f } });
  8248. // sidechained compressor (side input 1) with a negative knee, noise gate and the exact-ceiling option
  8249. check (run (limiter (limiterState ({ 1000, 1000, 1000, 0, 3, 3000, 5, 2882, 300, 1000, -500, 500, 5000, 1, 1000, 0, 250, 4187 }, 1, 1)), true),
  8250. { { 100, -0.00186592329f, 0.00895605329f }, { 1000, -0.0375732854f, -0.0448972471f }, { 2047, 0.124697573f, -0.162336946f },
  8251. { 3000, -0.111069582f, -0.128206298f }, { 4095, -0.218446419f, 0.21791859f } });
  8252. auto compressor = [] (const std::vector<std::uint8_t>& st) {
  8253. auto slot = lynx::effectFromFl ("Fruity Compressor", st);
  8254. CHECK (slot && slot->kind == lynx::EffectKind::fruityCompressor);
  8255. return *slot;
  8256. };
  8257. // Vintage type, look-ahead flag, side input 1: bit-identical
  8258. const auto ca = run (compressor (compressorState (3, { -200, 40, 30, 50, 300, 2 }, 1, 1)), true);
  8259. CHECK (ca.latency == 64);
  8260. check (ca, { { 100, 0.0795554966f, -0.230549783f }, { 1000, 0.483903229f, 0.0241768137f }, { 2047, -0.596430957f, -0.441769004f },
  8261. { 3000, 0.597724319f, -0.18129006f }, { 4095, 0.607042372f, 0.435067475f } });
  8262. // Soft/R, version 2 without the flag (the output is still 64 frames late, but not reported): bit-identical
  8263. const auto cb = run (compressor (compressorState (2, { -150, 80, 0, 150, 100, 7 }, 0)), false);
  8264. CHECK (cb.latency == 0);
  8265. check (cb, { { 100, 0.0813739002f, -0.235819489f }, { 1000, 0.264188439f, 0.0131994039f }, { 2047, -0.260424435f, -0.192893147f },
  8266. { 3000, 0.251500547f, -0.0762802213f }, { 4095, 0.252972007f, 0.181305125f } });
  8267. // the slot's parameters carry FL's raw controls exactly (and survive the 4-decimal project file)
  8268. const auto st = limiterState ({ 1261, 995, 1303, 1115, 3, 5000, 3, 2882, 264, 244, -590, 1115, 6666, 1, 650, 1000, 0, 4187 }, 1);
  8269. const auto parsed = lynx::fldyn::parseLimiter (st);
  8270. CHECK (parsed && parsed->sidechain == 1 && ! parsed->exactCeiling);
  8271. const auto slot = limiter (st);
  8272. for (int i = 0; i < lynx::fldyn::limiterControls; ++i)
  8273. {
  8274. const auto rounded = std::round (slot.params[(std::size_t) i] * 10000.0f) / 10000.0f;
  8275. CHECK (lynx::fldyn::raw (rounded, lynx::fldyn::limiterMin[(std::size_t) i], lynx::fldyn::limiterMax[(std::size_t) i]) == parsed->p[(std::size_t) i]);
  8276. }
  8277. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityLimiter, 3, slot.params[3], 120.0) == "2.0 ms");
  8278. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityCompressor, 5, 2.0f / 7.0f, 120.0) == "Vintage");
  8279. // unreadable states keep the old approximation
  8280. CHECK (lynx::effectFromFl ("Fruity Limiter", { 7, 0, 0, 0 }).has_value() == false
  8281. || lynx::effectFromFl ("Fruity Limiter", { 7, 0, 0, 0 })->kind != lynx::EffectKind::fruityLimiter);
  8282. CHECK (! lynx::fldyn::parseCompressor ({ 4, 0, 0, 0 }));
  8283. }
  8284. TEST (maximus_engine)
  8285. {
  8286. // REA_rodada40: the engine on its own (MaximusProcessor), states of our own against the installation's DLLs
  8287. // (goldens = the DLL's output for the same input, EXEC), and the containers of the mixer effect and Vocodex
  8288. std::vector<float> inL (4096), inR (4096);
  8289. for (int i = 0; i < 4096; ++i)
  8290. {
  8291. 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));
  8292. inR[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / 44100.0));
  8293. }
  8294. auto run = [&] (std::shared_ptr<const lynx::hlfx::MaximusPreset> preset, int mix) {
  8295. lynx::hlfx::MaximusProcessor m;
  8296. m.prepare (std::move (preset), 44100.0, mix);
  8297. std::vector<float> l (inL), r (inR);
  8298. for (int s = 0; s < 4096; s += 512)
  8299. m.process (l.data() + s, r.data() + s, 512);
  8300. return std::make_pair (l, r);
  8301. };
  8302. using Env = std::vector<std::array<double, 4>>;
  8303. const Env def { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 } };
  8304. const Env knee { { 0.0, 0.0, 0.0, 0 }, { 0.35, 0.35, 0.0, 0 }, { 0.65, 0.6, 0.36, 7 } };
  8305. const Env t0 { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.14, 0 }, { 0.5, 0.5, 0.0, 0 } };
  8306. const Env m0 { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 }, { 0.5, 0.6, 0.085, 0 } };
  8307. 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,
  8308. int sus = 2882, int rms = 0) { return std::array<int, 13> { mode, pre, post, sep, sat, ceil, att, rel2, 2, 5000, 3, sus, rms }; };
  8309. auto check = [] (const std::pair<std::vector<float>, std::vector<float>>& out, const std::vector<std::tuple<int, float, float>>& golden) {
  8310. for (const auto& [i, gl, gr] : golden)
  8311. CHECK (std::abs (out.first[(std::size_t) i] - gl) < 2e-6f && std::abs (out.second[(std::size_t) i] - gr) < 2e-6f);
  8312. };
  8313. // RMS detectors, saturation mode B, a 5 Hz low cut, 12 dB high split (SG_x64.dll: port within −152 dB)
  8314. auto st3 = maximusTestState ({ 19800, 1, 48065, 0, 1115, 1000, 0 },
  8315. { band (0, 1500, 1000, 0, 400, 900, 1115, 0, 2882, 1), band (0, 1340, 1000, 0, 0, 1000, 1115, 0, 1570, 1),
  8316. band (1, 1000, 1200, 0, 1000, 1000), band (0, 1000, 1000, 0, 250, 1000, 1115, 4464, 2882, 1) },
  8317. { def, def, knee, def, t0, def, m0, def }, 0);
  8318. const auto p3 = lynx::hlfx::makeMaximusPreset (st3);
  8319. CHECK (p3 != nullptr);
  8320. check (run (p3, 1000), { { 100, 0.0f, 0.0f }, { 1000, 0.87061065f, 0.64004499f }, { 2047, -0.97266865f, 0.60940075f },
  8321. { 3000, 0.96658099f, 0.9250561f }, { 4095, 0.99094611f, -0.964059f } });
  8322. // the same with solo on (UI byte 0x1EF) and the low band selected (0x1E0): bit-identical to SG_x64.dll
  8323. auto solo = st3;
  8324. solo[0x1ef] = 1;
  8325. std::memset (solo.data() + 0x1e0, 0, 4);
  8326. check (run (lynx::hlfx::makeMaximusPreset (solo), 1000),
  8327. { { 100, 3.2922967e-06f, 0.00036916698f }, { 1000, 0.15653539f, -0.047913823f }, { 2047, -0.17336531f, 0.019099623f },
  8328. { 3000, 0.17185682f, -0.017173048f }, { 4095, 0.16714551f, 0.019473195f } });
  8329. // the legacy linear-phase FIR band split (UI byte 0x1FD; SG_x64.dll: port within −148 dB)
  8330. auto fir = st3;
  8331. fir[0x1fd] = 1;
  8332. check (run (lynx::hlfx::makeMaximusPreset (fir), 1000),
  8333. { { 100, 0.0f, 0.0f }, { 1000, 0.97986746f, 0.18555209f }, { 2047, -0.98930448f, -0.95970845f },
  8334. { 3000, 0.98856646f, -0.44592777f }, { 4095, 0.99069679f, 0.94382554f } });
  8335. // Soundgoodizer_x64.dll's own build with a v4 file (its presets' layout): a copy of the round-36 state without
  8336. // oversampling, through a byte-identical copy of the installation's DLL with this file as its preset (EXEC)
  8337. const auto st2 = maximusTestState ({ 19800, 1, 48065, 1, 891, 600, 32768 },
  8338. { band (1, 1264, 1296, 500, -500, 1200), band (0, 1340, 1000, -348, 0, 1000, 1115, 0, 1570),
  8339. band (0, 1364, 1152, 0, 0, 1000, 1115, 0, 1186), band (0, 1000, 1000, 0, -196, 1000, 1115, 4464, 1538) },
  8340. { def, def, knee, def, t0, def, m0, def }, 0);
  8341. std::vector<std::uint8_t> v4 { 4, 0, 0, 0 };
  8342. v4.insert (v4.end(), st2.begin() + 4, st2.begin() + 0x1f5);
  8343. v4.insert (v4.end(), st2.begin() + 0x1fe, st2.end());
  8344. const auto p4 = lynx::hlfx::makeMaximusPreset (v4, lynx::hlfx::MaximusBuild::soundgoodizer);
  8345. CHECK (p4 != nullptr);
  8346. check (run (p4, 600), { { 100, 0.0f, 0.0f }, { 1000, -0.60926896f, -0.6652832f }, { 2047, 0.40872875f, 0.36375371f },
  8347. { 3000, -0.41941205f, -0.61312866f }, { 4095, -0.12878418f, 0.27944946f } });
  8348. CHECK (lynx::hlfx::makeMaximusPreset (st2, lynx::hlfx::MaximusBuild::soundgoodizer) != nullptr); // v7: also read by that build
  8349. // the mixer effect's container and the effect itself
  8350. lynx::maximusfx::Blob blob;
  8351. blob.build = lynx::hlfx::MaximusBuild::soundgoodizer;
  8352. blob.mix = 600;
  8353. blob.state = v4;
  8354. const auto packed = lynx::maximusfx::pack (blob);
  8355. const auto back = lynx::maximusfx::unpack (packed);
  8356. CHECK (back && back->build == blob.build && back->mix == 600 && back->state == v4);
  8357. auto slot = lynx::makeEffectSlot (lynx::EffectKind::maximus);
  8358. slot.state = packed;
  8359. auto fx = lynx::makeEffect (slot);
  8360. fx->prepare (44100.0);
  8361. {
  8362. std::vector<float> l (inL), r (inR);
  8363. for (int s = 0; s < 4096; s += 1024) // longer host blocks are split into the engine's 512-frame calls
  8364. fx->process (l.data() + s, r.data() + s, 1024, slot.params, 120.0);
  8365. const auto ref = run (p4, 600);
  8366. CHECK (l == ref.first && r == ref.second);
  8367. }
  8368. const std::vector<std::uint8_t> sgState { 3, 0, 0, 0, 2, 0, 0, 0, 0x78, 2, 0, 0 };
  8369. const auto sg = lynx::maximusfx::parseSoundgoodizer (sgState);
  8370. CHECK (sg && sg->preset == 2 && sg->amount == 632 && std::string (lynx::maximusfx::soundgoodizerPresetFile (2)) == "c.dat");
  8371. CHECK (lynx::soundgoodizerPresetPath (sgState) == "Plugins\\Fruity\\Effects\\Soundgoodizer\\Data\\Presets\\c.dat");
  8372. CHECK (lynx::soundgoodizerExact (sgState, v4).has_value() && ! lynx::soundgoodizerExact (sgState, {}).has_value());
  8373. // Vocodex's appended installation data survives a round trip and leaves FL's state untouched
  8374. const std::vector<std::uint8_t> flState { 8, 0, 0, 0, 1, 2, 3 };
  8375. lynx::vocodex::InstallData data;
  8376. data.sgDat = st2;
  8377. data.carrierState = { 9, 9, 9 };
  8378. const auto joined = lynx::vocodex::appendInstallData (flState, data);
  8379. const auto [raw, files] = lynx::vocodex::splitInstallData (joined);
  8380. CHECK (raw == flState && files.sgDat == st2 && files.carrierState == data.carrierState);
  8381. CHECK (lynx::vocodex::splitInstallData (flState).first == flState);
  8382. }
  8383. // ---- Fruity Reeverb 2 (REA_rodada49) --------------------------------------------------------------------------------
  8384. // Goldens: the installation's Fruity Reeverb 2_x64.dll (unmodified, rate set before the state, flushed, blocks of 512)
  8385. // on our own states and input; LYNX's engine is bit-identical to it.
  8386. TEST (reeverb2_engine)
  8387. {
  8388. auto hex = [] (const char* h) {
  8389. std::vector<std::uint8_t> v;
  8390. for (std::size_t i = 0; h[i] != 0 && h[i + 1] != 0; i += 2)
  8391. v.push_back ((std::uint8_t) std::stoi (std::string (h + i, 2), nullptr, 16));
  8392. return v;
  8393. };
  8394. constexpr int N = 8192;
  8395. std::vector<float> inL (N), inR (N);
  8396. std::uint32_t seed = 12345;
  8397. for (int i = 0; i < N; ++i)
  8398. {
  8399. seed = (seed * 1103515245u + 12345u) & 0x7fffffffu;
  8400. const auto nz = (float) ((double) seed / 2147483648.0 - 0.5);
  8401. const auto tri = (float) ((i % 200) - 100) / 100.0f;
  8402. const float g = i < 4000 ? 1.0f : 0.0f;
  8403. inL[(std::size_t) i] = 0.5f * tri * g + (i >= 100 && i < 300 ? nz * 0.6f : 0.0f);
  8404. inR[(std::size_t) i] = 0.25f * tri * g;
  8405. }
  8406. struct Golden
  8407. {
  8408. int i;
  8409. float l, r;
  8410. };
  8411. auto run = [&] (const std::vector<std::uint8_t>& state, double bpm, const std::vector<Golden>& gold) {
  8412. const auto slot = lynx::reeverb2FromFl (state);
  8413. CHECK (slot.has_value() && slot->kind == lynx::EffectKind::reeverb2);
  8414. if (! slot)
  8415. return;
  8416. auto fx = lynx::makeEffect (*slot);
  8417. fx->prepare (44100.0);
  8418. fx->reset();
  8419. std::vector<float> l (inL), r (inR);
  8420. for (int s = 0; s < N; s += 512)
  8421. fx->process (l.data() + s, r.data() + s, 512, slot->params, bpm);
  8422. for (const auto& g : gold)
  8423. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  8424. };
  8425. // modulation, early reflections, negative separation, filters, predelay
  8426. run (hex ("112700004b000000780000001e0000004600000055000000190000003c0000009600000090010000e2ffffff6e000000640000008c00000084030000b00400000000"),
  8427. 120.0, { { 777, 0.263778746f, 0.131889373f }, { 1555, 0.187972173f, 0.0587911084f }, { 3333, 0.10398978f, 0.0566705838f },
  8428. { 5000, 0.087177977f, -0.0197417438f }, { 8191, 0.0769432634f, -0.282023013f } });
  8429. // Mid/Side input, tempo-synced predelay (27 ticks at 128 BPM), positive separation
  8430. run (hex ("112700004b000000280000001b00000032000000640000000f0000002800000064000000f401000028000000800000000000000040000000ac020000000000000101"),
  8431. 128.0, { { 777, 0.38499999f, 0.192499995f }, { 1555, 0.275000006f, 0.137500003f }, { 3333, 0.164420277f, 0.0826604739f },
  8432. { 5000, -0.00861243438f, -0.00140450173f }, { 8191, -0.00873835851f, 0.00888271444f } });
  8433. // version 10000 (no modulation controls), everything off or at its extreme, dry 0
  8434. run (hex ("1027000013000000dd000000000000006400000000000000c8000000dd00000014000000d00700004000000000000000a0000000a00000000000"),
  8435. 120.0, { { 777, 0.0f, 0.0f }, { 1555, 0.0f, 0.0f }, { 3333, 0.0632086769f, 0.0632086769f }, { 5000, -0.108072646f, -0.108072646f },
  8436. { 8191, 0.144686908f, 0.144686908f } });
  8437. // the parameters map back to FL's integers; unsupported states fall back to the approximation
  8438. lynx::reeverb2::Settings s;
  8439. s.p = { 3000, 5, 384, 1, 0, 200, 221, 20, 2000, -64, 160, 0, 160, 2048, 1 };
  8440. s.tempoPredelay = true;
  8441. lynx::EffectParams p {};
  8442. lynx::reeverb2::toParams (s, p);
  8443. CHECK (lynx::reeverb2::fromParams (p) == s);
  8444. CHECK (! lynx::reeverb2FromFl ({ 0x11, 0x27, 0, 0, 1, 0 }).has_value());
  8445. CHECK (! lynx::reeverb2FromFl ({ 0x12, 0x27, 0, 0 }).has_value());
  8446. const auto approx = lynx::effectFromFl ("Fruity Reeverb 2", { 0x11, 0x27, 0, 0 }, 120.0);
  8447. CHECK (! approx || approx->kind != lynx::EffectKind::reeverb2);
  8448. CHECK (lynx::reeverb2ParamText (0, 0.0f) == "off" && lynx::reeverb2ParamText (5, 1.0f) == "20.0 s");
  8449. // Fruity Reeverb (the first version): Fruity Reeverb_x64.dll goldens on our own states (denormals flushed, as the
  8450. // plugin runs and as LYNX renders)
  8451. lynx::ScopedFlushDenormals ftz;
  8452. auto run1 = [&] (const std::vector<std::uint8_t>& state, const std::vector<Golden>& gold) {
  8453. const auto slot = lynx::reeverb1FromFl (state);
  8454. CHECK (slot.has_value() && slot->kind == lynx::EffectKind::reeverb1);
  8455. if (! slot)
  8456. return;
  8457. auto fx = lynx::makeEffect (*slot);
  8458. fx->prepare (44100.0);
  8459. std::vector<float> l (inL), r (inR);
  8460. for (int s = 0; s < N; s += 512)
  8461. fx->process (l.data() + s, r.data() + s, 512, slot->params, 120.0);
  8462. for (const auto& g : gold)
  8463. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  8464. };
  8465. run1 (hex ("01000000204e0000204e000025000000409c000050c300000a00000030750000a861000050c3000060ea0000"),
  8466. { { 777, 0.293731689f, 0.146865845f }, { 1555, 0.20980835f, 0.104904175f }, { 3333, 0.0998796821f, 0.0754205957f },
  8467. { 5000, -0.0994476378f, 0.0116700521f }, { 8191, -0.141061321f, 0.0601170398f } });
  8468. run1 (hex ("0100000000000000000001000000000000000100000001002800000000000100000001000000000000000100"),
  8469. { { 777, -0.016678432f, -4.58686594e-31f }, { 1555, 0.0221031085f, 0.0230635051f }, { 3333, 0.0875115693f, 0.1695389f },
  8470. { 5000, -0.111421227f, -0.295867324f }, { 8191, 0.503498077f, 0.190456569f } });
  8471. CHECK (lynx::reeverb1FromFl (std::vector<std::uint8_t> (44, 0)).has_value());
  8472. CHECK (! lynx::reeverb1FromFl (std::vector<std::uint8_t> (40, 0)).has_value());
  8473. CHECK (lynx::reeverb1ParamText (5, 0.0f) == "Brighter" && lynx::reeverb1ParamText (0, 0.0f) == "off");
  8474. }
  8475. // ---- Fruity WaveShaper / Fruity Soft Clipper (REA_rodada51) ------------------------------------------------------------
  8476. // States written here from our own values (no FL preset data); goldens from the installation's DLLs (EXEC, blocks of 512)
  8477. namespace
  8478. {
  8479. std::vector<std::uint8_t> waveShaperTestState (int pre, int mix, int post, bool uni, bool center, int os, bool hq, bool smooth)
  8480. {
  8481. std::vector<std::uint8_t> b;
  8482. auto i32 = [&b] (std::int32_t v) { for (int k = 0; k < 4; ++k) b.push_back ((std::uint8_t) ((std::uint32_t) v >> (8 * k))); };
  8483. auto f64 = [&b] (double v) { std::uint64_t u; std::memcpy (&u, &v, 8); for (int k = 0; k < 8; ++k) b.push_back ((std::uint8_t) (u >> (8 * k))); };
  8484. auto f32 = [&b] (float v) { std::uint32_t u; std::memcpy (&u, &v, 4); for (int k = 0; k < 4; ++k) b.push_back ((std::uint8_t) (u >> (8 * k))); };
  8485. i32 (0x40003);
  8486. i32 (pre), i32 (mix), i32 (post);
  8487. b.push_back (uni ? 1 : 0), b.push_back (center ? 1 : 0);
  8488. i32 (os);
  8489. const struct { double dt, y; float t; int mode; } pts[] = { { 0.0, 0.1, 0.0f, 0 }, { 0.3, 0.8, 0.4f, 0 }, { 0.25, 0.35, 0.0f, 10 },
  8490. { 0.25, 0.9, -0.3f, 3 }, { 0.2, 1.0, 0.5f, 9 } };
  8491. i32 (3), i32 (3), i32 (5);
  8492. for (const auto& p : pts)
  8493. {
  8494. f64 (p.dt), f64 (p.y), f32 (p.t);
  8495. b.push_back ((std::uint8_t) p.mode), b.push_back (0), b.push_back (0), b.push_back (0);
  8496. }
  8497. i32 (0), i32 (-1), i32 (-1), i32 (-1), i32 (-1);
  8498. i32 (1);
  8499. b.push_back (hq ? 1 : 0), b.push_back (smooth ? 1 : 0);
  8500. return b;
  8501. }
  8502. } // namespace
  8503. TEST (fruity_waveshaper_and_soft_clipper)
  8504. {
  8505. std::vector<float> inL (4096), inR (4096);
  8506. for (int n = 0; n < 4096; ++n)
  8507. {
  8508. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  8509. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  8510. }
  8511. struct Gold { int i; float l, r; };
  8512. auto close = [] (float a, float b) { return std::abs (a - b) <= 2.0e-7f * std::max (1.0f, std::abs (b)); };
  8513. // Soft Clipper: bit-exact against Fruity Soft Clipper_x64.dll
  8514. auto clip = [&] (int thr, int post, std::initializer_list<Gold> gold) {
  8515. std::vector<float> l (inL), r (inR);
  8516. for (int s = 0; s < 4096; s += 512)
  8517. {
  8518. lynx::flshape::softClip (l.data() + s, 512, thr, post);
  8519. lynx::flshape::softClip (r.data() + s, 512, thr, post);
  8520. }
  8521. for (const auto& g : gold)
  8522. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  8523. };
  8524. clip (100, 128, { { 100, 0.0f, -0.949052989f }, { 1000, -0.99617976f, 0.872887492f }, { 2047, 0.649999976f, -0.944175363f },
  8525. { 3000, -0.99617976f, -0.699999988f }, { 4095, 0.25f, 0.600000024f } });
  8526. clip (1, 150, { { 100, 0.0f, -1.04588532f }, { 1000, -1.27003884f, 0.930835366f }, { 2047, 0.750244498f, -1.03539562f },
  8527. { 3000, -1.27003884f, -0.790091455f }, { 4095, 0.347655922f, 0.708338201f } });
  8528. clip (64, 140, { { 100, 0.0f, -1.08492827f }, { 1000, -1.21535313f, 0.990323186f }, { 2047, 0.804165959f, -1.0770781f },
  8529. { 3000, -1.21535313f, -0.849189103f }, { 4095, 0.319320858f, 0.754407823f } });
  8530. {
  8531. const std::vector<std::uint8_t> st { 64, 0, 0, 0, 140, 0, 0, 0 };
  8532. const auto slot = lynx::effectFromFl ("Fruity Soft Clipper", st, 120.0);
  8533. CHECK (slot && slot->kind == lynx::EffectKind::fruitySoftClipper);
  8534. if (slot)
  8535. {
  8536. const auto s = lynx::flshape::softClipperFromParams (slot->params.data());
  8537. CHECK (s.threshold == 64 && s.post == 140);
  8538. auto fx = lynx::makeEffect (*slot);
  8539. fx->prepare (44100.0);
  8540. std::vector<float> l (inL), r (inR);
  8541. fx->process (l.data(), r.data(), 4096, slot->params, 120.0);
  8542. CHECK (l[3000] == -1.21535313f && r[4095] == 0.754407823f);
  8543. }
  8544. }
  8545. // WaveShaper: tables (bipolar / unipolar) and the engine on three of our states, live and as FL renders
  8546. const auto stA = waveShaperTestState (150, 100, 120, false, true, 1, true, true); // CENTER, ×2, HQ, smooth
  8547. const auto stB = waveShaperTestState (100, 128, 128, true, false, 0, false, false); // unipolar
  8548. const auto stC = waveShaperTestState (192, 90, 100, false, false, 2, false, false); // ×4
  8549. const auto pA = lynx::flshape::parseWaveShaper (stA);
  8550. CHECK (pA && pA->points.size() == 5 && pA->pre == 150 && pA->center && pA->hq && pA->smooth && pA->oversampling == 1);
  8551. if (pA)
  8552. {
  8553. const auto T = lynx::flshape::shaperTable (pA->points, false);
  8554. CHECK (T.size() == 16386 && T[1] == 3277 && T[1001] == 18824 && T[5001] == 26195 && T[9001] == 11469 && T[12001] == 24614
  8555. && T[16384] == 32767 && T[0] == T[1]);
  8556. const auto U = lynx::flshape::shaperTable (pA->points, true);
  8557. CHECK (U.size() == 32770 && U[1] == -26214 && U[1001] == -6408 && U[5001] == 17073 && U[9001] == 19551 && U[12001] == 14549);
  8558. }
  8559. auto shape = [&] (const std::vector<std::uint8_t>& st, bool render, std::initializer_list<Gold> gold) {
  8560. const auto s = lynx::flshape::parseWaveShaper (st);
  8561. CHECK (s.has_value());
  8562. if (! s)
  8563. return;
  8564. lynx::flshape::WaveShaperEngine e;
  8565. e.setCurve (s->points);
  8566. e.prepare (44100.0);
  8567. e.setControls (s->pre, s->mix, s->post, s->oversampling, s->hq, s->smooth, s->center, s->unipolar, render);
  8568. std::vector<float> l (inL), r (inR);
  8569. for (int i = 0; i < 4096; i += 512)
  8570. e.process (l.data() + i, r.data() + i, 512);
  8571. for (const auto& g : gold)
  8572. CHECK (close (l[(std::size_t) g.i], g.l) && close (r[(std::size_t) g.i], g.r));
  8573. };
  8574. shape (stA, false, { { 100, 0.59108448f, 0.469315082f }, { 1000, 0.284353405f, 1.02621937f }, { 2047, -0.533613563f, 0.117038473f },
  8575. { 3000, 0.22673659f, -1.13572407f }, { 4095, -0.267153352f, 0.822866499f } });
  8576. shape (stA, true, { { 100, 0.794869065f, -0.127601087f }, { 1000, -0.00841622613f, 0.900729597f }, { 2047, 0.450603426f, -0.325943232f },
  8577. { 3000, -0.0530530401f, -0.32303831f }, { 4095, 1.0959363f, 0.946009099f } });
  8578. shape (stB, false, { { 100, -0.206451416f, 0.502471924f }, { 1000, -0.799987793f, 0.761169434f }, { 2047, 0.411743164f, 0.514160156f },
  8579. { 3000, -0.799987793f, 0.596221924f }, { 4095, -0.157653809f, 0.334106445f } });
  8580. shape (stB, true, { { 100, -0.206390381f, 0.502493739f }, { 1000, -0.799987793f, 0.761169434f }, { 2047, 0.411743164f, 0.514144361f },
  8581. { 3000, -0.799987793f, 0.596221924f }, { 4095, -0.157653809f, 0.334106445f } });
  8582. shape (stC, false, { { 100, 0.317797363f, 0.0483714938f }, { 1000, -0.445489585f, -0.533142388f }, { 2047, 0.634525597f, 0.942175806f },
  8583. { 3000, -0.445489585f, 0.514707327f }, { 4095, 0.200391173f, -0.937523127f } });
  8584. shape (stC, true, { { 100, 0.61165309f, -0.0993360579f }, { 1000, 0.0375693105f, 0.667849183f }, { 2047, 0.353716731f, -0.223758236f },
  8585. { 3000, 0.0375693105f, -0.223787636f }, { 4095, 0.843308687f, 0.688474894f } });
  8586. // the mixer effect from the FL import: controls as parameters, the curve in the slot's state; host blocks of any size
  8587. const auto slot = lynx::effectFromFl ("Fruity WaveShaper", stC, 120.0);
  8588. CHECK (slot && slot->kind == lynx::EffectKind::fruityWaveShaper && slot->state == stC);
  8589. if (slot)
  8590. {
  8591. auto fx = lynx::makeEffect (*slot);
  8592. fx->prepare (44100.0);
  8593. std::vector<float> l (inL), r (inR);
  8594. fx->process (l.data(), r.data(), 1024, slot->params, 120.0);
  8595. fx->process (l.data() + 1024, r.data() + 1024, 1024, slot->params, 120.0);
  8596. fx->process (l.data() + 2048, r.data() + 2048, 2048, slot->params, 120.0);
  8597. CHECK (close (l[1000], -0.445489585f) && close (r[2047], 0.942175806f) && close (l[4095], 0.200391173f));
  8598. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityWaveShaper, 3, slot->params[3], 120.0) == "x4");
  8599. }
  8600. // an unreadable state keeps the old approximation
  8601. CHECK (lynx::effectFromFl ("Fruity WaveShaper", { 1, 2, 3 }, 120.0)->kind == lynx::EffectKind::distortion);
  8602. CHECK (lynx::effectFromFl ("Fruity Soft Clipper", { 1, 2, 3 }, 120.0)->kind == lynx::EffectKind::distortion);
  8603. }
  8604. // ---- Fruity Parametric EQ 2 / Fruity Parametric EQ (REA_rodada50) -----------------------------------------------------
  8605. // States written here from our own values (no FL preset data); goldens from the installation's DLLs (EXEC, blocks of 512)
  8606. namespace
  8607. {
  8608. struct PeqTestBand { int type, freq, width, gain, order; };
  8609. std::vector<std::uint8_t> peq2TestState (const PeqTestBand (&bands)[7], int main, int uiFlags, int options, int mask)
  8610. {
  8611. std::vector<std::uint8_t> b;
  8612. auto i32 = [&b] (std::int32_t v) { for (int k = 0; k < 4; ++k) b.push_back ((std::uint8_t) ((std::uint32_t) v >> (8 * k))); };
  8613. i32 (8);
  8614. for (int copy = 0; copy < 2; ++copy)
  8615. {
  8616. for (const auto& x : bands) i32 (x.gain);
  8617. for (const auto& x : bands) i32 (x.freq);
  8618. for (const auto& x : bands) i32 (x.width);
  8619. for (const auto& x : bands) i32 (x.type);
  8620. for (const auto& x : bands) i32 (x.order);
  8621. i32 (main);
  8622. }
  8623. i32 (uiFlags), i32 (0), b.push_back (0), i32 (2), i32 (0), i32 (2), i32 (0);
  8624. b.push_back ((std::uint8_t) options);
  8625. i32 (mask), i32 (1), i32 (0);
  8626. for (int v : { 3, 1, 2, 2, 2 }) i32 (v);
  8627. i32 (0);
  8628. return b;
  8629. }
  8630. std::vector<std::uint8_t> peq1TestState (const PeqTestBand (&bands)[7], int main)
  8631. {
  8632. std::vector<std::uint8_t> b;
  8633. auto i32 = [&b] (std::int32_t v) { for (int k = 0; k < 4; ++k) b.push_back ((std::uint8_t) ((std::uint32_t) v >> (8 * k))); };
  8634. for (const auto& x : bands) i32 (x.gain);
  8635. for (const auto& x : bands) i32 (x.freq);
  8636. for (const auto& x : bands) i32 (x.width);
  8637. for (const auto& x : bands) i32 (x.type);
  8638. i32 (main);
  8639. return b;
  8640. }
  8641. } // namespace
  8642. // EQUO test state (our own LCG curves, no FL data; REA_rodada61 equo/teststate.py builds the same bytes)
  8643. static std::vector<std::uint8_t> equoTestState (int ovs, int bw, bool add)
  8644. {
  8645. std::vector<std::uint8_t> b;
  8646. auto put = [&b] (const void* p, std::size_t n) { b.insert (b.end(), (const std::uint8_t*) p, (const std::uint8_t*) p + n); };
  8647. auto putI = [&put] (std::int32_t v) { put (&v, 4); };
  8648. auto putF = [&put] (float v) { put (&v, 4); };
  8649. std::uint32_t seed = 12345;
  8650. auto rnd = [&seed] {
  8651. seed = (std::uint32_t) (((std::uint64_t) seed * 1103515245u + 12345u) & 0x7fffffffu);
  8652. return (float) ((double) (seed % 20001) / 10000.0);
  8653. };
  8654. putI (4);
  8655. for (int k = 0; k < 93; ++k)
  8656. {
  8657. putF (rnd());
  8658. putI (k % 11 == 0 ? 1 : (k % 13 == 0 ? 2 : 0));
  8659. }
  8660. for (int bank = 0; bank < 9; ++bank)
  8661. for (int k = 0; k < 93; ++k)
  8662. {
  8663. const float v = rnd();
  8664. putF (bank == 8 ? 0.75f + v * 0.25f : v);
  8665. }
  8666. for (int v : { 1500, 90, 2600, 170, 150, ovs, bw, -1, 0, 0 })
  8667. putI (v);
  8668. for (int v : { 0, add ? 1 : 0, 0 })
  8669. b.push_back ((std::uint8_t) v);
  8670. putI (1);
  8671. return b;
  8672. }
  8673. // Pitch Shifter state (version 7, our own settings; REA_rodada61 ps/teststate.py builds the same bytes)
  8674. static std::vector<std::uint8_t> pitchShifterTestState (std::array<int, 13> p, int engine, int detector)
  8675. {
  8676. std::vector<std::uint8_t> b;
  8677. auto putI = [&b] (std::int32_t v) { const auto* q = (const std::uint8_t*) &v; b.insert (b.end(), q, q + 4); };
  8678. putI (7);
  8679. for (int v : p)
  8680. putI (v);
  8681. putI (p[11]);
  8682. b.push_back (0);
  8683. putI (engine);
  8684. putI (detector);
  8685. putI (1000);
  8686. b.push_back (1);
  8687. b.push_back (0);
  8688. return b;
  8689. }
  8690. TEST (pitch_shifter_exact)
  8691. {
  8692. std::vector<float> inL (8192), inR (8192);
  8693. for (int n = 0; n < 8192; ++n)
  8694. {
  8695. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0) * 0.3f;
  8696. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0) * 0.3f;
  8697. }
  8698. struct Gold { int i; float l, r; };
  8699. // goldens: Pitch Shifter_x64.dll on the same states and signal (REA_rodada61)
  8700. auto run = [&] (const std::vector<std::uint8_t>& st, int latency, std::initializer_list<Gold> gold) {
  8701. lynx::psfl::State s;
  8702. CHECK (st.size() == 75 && lynx::psfl::parsePitchShifter (st.data(), st.size(), s));
  8703. // through the mixer effect: FL import → slot parameters → effect, odd host blocks
  8704. const auto slot = lynx::effectFromFl ("Pitch Shifter", st, 120.0);
  8705. CHECK (slot && slot->kind == lynx::EffectKind::flPitchShifter);
  8706. if (! slot)
  8707. return;
  8708. auto fx = lynx::makeEffect (*slot);
  8709. fx->prepare (44100.0);
  8710. std::vector<float> l (inL), r (inR);
  8711. for (int i = 0, k = 0; i < 8192; ++k)
  8712. {
  8713. const int n = std::min (8192 - i, k % 2 == 0 ? 700 : 333);
  8714. fx->process (l.data() + i, r.data() + i, n, slot->params, 120.0);
  8715. i += n;
  8716. }
  8717. CHECK (fx->latencySamples() == latency);
  8718. for (const auto& g : gold)
  8719. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  8720. };
  8721. // "FFT" (pitch-synchronous) engine: +5 st, formant −3 st, correction 50 %, mix 79 %, accurate detector
  8722. run (pitchShifterTestState ({ 500, -300, 2, 1900, 8500, 200, 0, 0, 768, 0, 0, 32768, 0 }, 0, 1), 2457,
  8723. { { 1000, 0.0f, 0.0f }, { 3000, 0.142123833f, 0.0878119618f }, { 5000, 0.139498621f, -0.365035176f },
  8724. { 7000, 0.139498621f, 0.161137447f }, { 8191, 0.449655473f, 0.453882188f } });
  8725. // granular engine: −7 st, 60 ms grains, density 300 %, jitter 30 %, random 20 %, post delay with feedback 40 %
  8726. run (pitchShifterTestState ({ -700, 0, 2, 2400, 6000, 300, 30, 20, 200, 40, 1, 65536, 0 }, 1, 0), 0,
  8727. { { 1000, -0.0736689344f, -0.349420458f }, { 3000, 0.086801365f, 0.0471927896f }, { 5000, -0.0939825475f, 0.31712544f },
  8728. { 7000, -0.28462702f, -0.437854528f }, { 8191, 0.138916701f, -0.0426892526f } });
  8729. CHECK (lynx::formatEffectParam (lynx::EffectKind::flPitchShifter, 0, 0.75f, 120.0) == "+6.00 st");
  8730. }
  8731. TEST (equo_exact)
  8732. {
  8733. std::vector<float> inL (4096), inR (4096);
  8734. for (int n = 0; n < 4096; ++n)
  8735. {
  8736. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  8737. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  8738. }
  8739. struct Gold { int i; float l, r; };
  8740. // goldens: EQUO_x64.dll run on the same states and signal (REA_rodada61 §5)
  8741. auto run = [&] (const std::vector<std::uint8_t>& st, bool hq, bool ticks, std::initializer_list<Gold> gold) {
  8742. const auto s = lynx::equofx::parse (st);
  8743. CHECK (s.has_value());
  8744. if (! s)
  8745. return;
  8746. lynx::equofx::Engine e;
  8747. e.prepare (44100.0, hq);
  8748. e.set (*s);
  8749. std::vector<float> l (inL), r (inR);
  8750. if (! ticks)
  8751. e.process (l.data(), r.data(), 4096);
  8752. else
  8753. {
  8754. auto k = *s;
  8755. e.startFromConstruction();
  8756. for (int i = 0; i < 4096; i += 256)
  8757. {
  8758. if (i == 2048)
  8759. k.knob[2] = 5300, e.setKnobs (k);
  8760. e.tick();
  8761. e.process (l.data() + i, r.data() + i, 256);
  8762. }
  8763. }
  8764. for (const auto& g : gold)
  8765. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  8766. };
  8767. const auto plain = equoTestState (0, 150, false);
  8768. run (plain, false, false, { { 100, -0.00154058414f, -0.00550224911f }, { 1000, -0.908149242f, 0.619277894f }, { 2047, 0.411961317f, -0.364051819f },
  8769. { 3000, -1.42823684f, -0.0602950938f }, { 4095, -0.0661701784f, 0.300809383f } });
  8770. run (equoTestState (1, 300, true), false, false,
  8771. { { 100, 0.0312003642f, 0.0184187777f }, { 1000, 0.0379521511f, 0.224126667f }, { 2047, -0.135689721f, 0.0444660783f },
  8772. { 3000, 0.113949843f, -1.00256324f }, { 4095, -0.176232055f, 0.79159987f } });
  8773. run (plain, true, false, { { 100, -0.00104017637f, 0.000872685807f }, { 1000, -0.0426855832f, 0.0402083024f }, { 2047, 0.191993102f, 0.174184501f },
  8774. { 3000, -0.114048228f, 0.419817507f }, { 4095, 0.0376945846f, -0.274040252f } });
  8775. run (plain, false, true, { { 100, -0.0019897793f, -0.00981196482f }, { 1000, -1.09574401f, 0.83203429f }, { 2047, 0.839113712f, -0.691361725f } });
  8776. // the mixer effect from the FL import: FL's state in the slot, the knobs in its parameters, any host block size
  8777. const auto slot = lynx::effectFromFl ("EQUO", plain, 140.0);
  8778. CHECK (slot && slot->kind == lynx::EffectKind::equo && slot->state == plain);
  8779. if (slot)
  8780. {
  8781. auto back = *lynx::equofx::parse (plain);
  8782. lynx::equofx::applyParams (slot->params, back);
  8783. CHECK (back.knob == lynx::equofx::parse (plain)->knob && back.bandwidth == 150 && back.oversample == 0);
  8784. auto fx = lynx::makeEffect (*slot);
  8785. fx->prepare (44100.0);
  8786. std::vector<float> l (inL), r (inR);
  8787. fx->process (l.data(), r.data(), 700, slot->params, 0.0);
  8788. fx->process (l.data() + 700, r.data() + 700, 3396, slot->params, 0.0);
  8789. // the engine glides its bands per processing call, as FL does per host buffer: compare with the engine run on
  8790. // the same 700 + 3396 split (sample 1000 differs from the single-call golden), and with the golden once settled
  8791. lynx::equofx::Engine ref;
  8792. ref.prepare (44100.0, false);
  8793. ref.set (*lynx::equofx::parse (plain));
  8794. std::vector<float> rl (inL), rr (inR);
  8795. ref.process (rl.data(), rr.data(), 700);
  8796. ref.process (rl.data() + 700, rr.data() + 700, 3396);
  8797. CHECK (l == rl && rr == r && r[3000] == -0.0602950938f);
  8798. CHECK (lynx::formatEffectParam (lynx::EffectKind::equo, 4, slot->params[4], 120.0, slot->params).find ("dB") != std::string::npos);
  8799. }
  8800. CHECK (! lynx::equofx::parse (std::vector<std::uint8_t> (100, 0)).has_value());
  8801. }
  8802. TEST (fruity_parametric_eq_exact)
  8803. {
  8804. std::vector<float> inL (4096), inR (4096);
  8805. for (int n = 0; n < 4096; ++n)
  8806. {
  8807. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  8808. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  8809. }
  8810. struct Gold { int i; float l, r; };
  8811. // high pass Steep 8, low shelf, steep peaking, a 0 dB peaking (0.2 s countdown), notch Gentle 4, high shelf Gentle 8,
  8812. // low pass Gentle 6
  8813. const PeqTestBand bands[7] = { { 3, 8000, 30000, 0, -3 }, { 5, 15000, 40124, 600, 0 }, { 6, 30000, 20000, -900, -2 },
  8814. { 6, 35000, 25412, 0, 0 }, { 4, 40000, 10000, 0, 1 }, { 7, 50000, 30000, 450, 3 },
  8815. { 1, 60000, 45000, 0, 2 } };
  8816. const PeqTestBand bands1[7] = { { 3, 6000, 20000, 0, 0 }, { 5, 15000, 30000, 600, 0 }, { 6, 30000, 20000, -900, 0 },
  8817. { 6, 35000, 25000, 0, 0 }, { 4, 40000, 10000, 0, 0 }, { 7, 50000, 30000, 450, 0 },
  8818. { 1, 60000, 45000, 0, 0 } };
  8819. auto run = [&] (const std::optional<lynx::peqfl::Settings>& s, std::initializer_list<Gold> gold) {
  8820. CHECK (s.has_value());
  8821. if (! s)
  8822. return;
  8823. lynx::peqfl::Engine e;
  8824. e.set (*s);
  8825. e.prepare (44100.0);
  8826. std::vector<float> l (inL), r (inR);
  8827. for (int i = 0; i < 4096; i += 512)
  8828. e.process (l.data() + i, r.data() + i, 512);
  8829. for (const auto& g : gold)
  8830. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  8831. };
  8832. const auto plain = peq2TestState (bands, 300, 0, 0x60, 0x7F);
  8833. const auto hq = peq2TestState (bands, 300, 1, 0x60, 0x7F);
  8834. const auto ext = peq2TestState (bands, 300, 1, 0x62, 0x7F);
  8835. run (lynx::peqfl::parsePeq2 (plain), { { 100, 0.1340574324131012f, -0.3040016293525696f }, { 1000, 0.28402701020240784f, -0.23813000321388245f },
  8836. { 2047, -0.9301530122756958f, 0.13881002366542816f }, { 3000, 0.2846762537956238f, 0.48893973231315613f },
  8837. { 4095, -0.8481860756874084f, -0.2641225755214691f } });
  8838. run (lynx::peqfl::parsePeq2 (hq), { { 100, 0.19229719042778015f, 0.29938483238220215f }, { 1000, 0.12543821334838867f, -0.055617764592170715f },
  8839. { 2047, 1.0342068672180176f, 0.10901166498661041f }, { 3000, 0.12598788738250732f, -0.12920552492141724f },
  8840. { 4095, 0.1619236022233963f, -0.23385579884052277f } });
  8841. run (lynx::peqfl::parsePeq2 (ext), { { 100, 1.2178642749786377f, -0.21160493791103363f }, { 1000, -0.8292768001556396f, -0.23916664719581604f },
  8842. { 2047, -0.7030635476112366f, -0.1978478729724884f }, { 3000, -0.8299900889396667f, 0.9836107492446899f },
  8843. { 4095, 0.06754066050052643f, -0.34364908933639526f } });
  8844. run (lynx::peqfl::parsePeq2 (peq2TestState (bands, -250, 1, 0x60, 0x5B)), // bands 3 and 6 muted
  8845. { { 100, -0.02959616854786873f, 0.227821946144104f }, { 1000, 0.053224340081214905f, 0.06922465562820435f },
  8846. { 2047, 0.3365536034107208f, -0.05899651721119881f }, { 3000, 0.053670305758714676f, -0.07452142238616943f },
  8847. { 4095, -0.04406970739364624f, -0.12635089457035065f } });
  8848. run (lynx::peqfl::parsePeq1 (peq1TestState (bands1, 150)),
  8849. { { 100, -0.6533108353614807f, -1.6903921365737915f }, { 1000, -4.313726902008057f, 5.973916053771973f },
  8850. { 2047, 1.0222549438476562f, 4.427900314331055f }, { 3000, -4.310323238372803f, 7.825916767120361f },
  8851. { 4095, 8.344571113586426f, -0.49086111783981323f } });
  8852. // "alternative design" (Frequency Splitter's matched biquads), bit-exact
  8853. run (lynx::peqfl::parsePeq2 (peq2TestState (bands, 300, 3, 0x60, 0x7F)),
  8854. { { 100, -0.790181577205658f, -0.08691816031932831f }, { 1000, 0.4960691034793854f, -0.24172282218933105f },
  8855. { 2047, -0.42601829767227173f, 0.027932533994317055f }, { 3000, 0.49674150347709656f, 1.0799059867858887f },
  8856. { 4095, -0.35040369629859924f, -0.28149813413619995f } });
  8857. // linear phase (plain and HQ response): FFT convolution, 1536-sample latency, 0.1 s fade-in; FL's IPP FFT rounding
  8858. // is not replicated, so within 1e-6 of the DLL
  8859. for (const bool hq : { false, true })
  8860. {
  8861. const auto s = lynx::peqfl::parsePeq2 (peq2TestState (bands, hq ? -200 : 300, hq ? 1 : 0, 0x61, 0x7F));
  8862. CHECK (s && s->linearPhase);
  8863. if (! s)
  8864. continue;
  8865. std::vector<float> l (12288), r (12288);
  8866. for (int n = 0; n < 12288; ++n)
  8867. {
  8868. l[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  8869. r[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  8870. }
  8871. lynx::peqfl::Engine e;
  8872. e.set (*s);
  8873. e.prepare (44100.0);
  8874. CHECK (e.latencySamples() == 1536);
  8875. for (int i = 0; i < 12288; i += 512)
  8876. e.process (l.data() + i, r.data() + i, 512);
  8877. const std::initializer_list<Gold> gold = hq ? std::initializer_list<Gold> {
  8878. { 2000, 0.08618364483118057f, 0.11248858273029327f }, { 5000, 0.18998730182647705f, 0.428600937128067f },
  8879. { 8191, -0.1412065178155899f, -0.002996620489284396f }, { 10000, 0.18998737633228302f, -0.012570115737617016f },
  8880. { 12287, 0.40586185455322266f, -0.014698396436870098f } } : std::initializer_list<Gold> {
  8881. { 2000, 0.18538671731948853f, 0.206522598862648f }, { 5000, 0.4086928963661194f, 0.8167467713356018f },
  8882. { 8191, -0.3218550980091095f, -0.06812914460897446f }, { 10000, 0.40869277715682983f, -0.08537150919437408f },
  8883. { 12287, 0.775394082069397f, -0.08931504935026169f } };
  8884. for (const auto& g : gold)
  8885. CHECK (std::abs (l[(std::size_t) g.i] - g.l) < 1.0e-6f && std::abs (r[(std::size_t) g.i] - g.r) < 1.0e-6f);
  8886. }
  8887. // the mixer effect from the FL import: FL's values in the slot's parameters (exact round trip), any host block size
  8888. for (const auto* name : { "Fruity Parametric EQ 2", "Fruity Parametric EQ" })
  8889. {
  8890. const bool v1 = std::string (name) == "Fruity Parametric EQ";
  8891. const auto st = v1 ? peq1TestState (bands1, 150) : ext;
  8892. const auto slot = lynx::effectFromFl (name, st, 120.0);
  8893. CHECK (slot && slot->kind == lynx::EffectKind::parametricEq && lynx::peq::flLaw (slot->params));
  8894. if (! slot)
  8895. continue;
  8896. const auto back = lynx::peqfl::fromParams (slot->params);
  8897. const auto orig = v1 ? lynx::peqfl::parsePeq1 (st) : lynx::peqfl::parsePeq2 (st);
  8898. CHECK (back.freq == orig->freq && back.gain == orig->gain && back.width == orig->width && back.type == orig->type
  8899. && back.order == orig->order && back.main == orig->main && back.processing == orig->processing && back.mask == orig->mask);
  8900. auto fx = lynx::makeEffect (*slot);
  8901. fx->prepare (44100.0);
  8902. CHECK (fx->latencySamples() == (v1 ? 0 : 44));
  8903. std::vector<float> l (inL), r (inR);
  8904. fx->process (l.data(), r.data(), 1000, slot->params, 120.0);
  8905. fx->process (l.data() + 1000, r.data() + 1000, 3096, slot->params, 120.0);
  8906. if (v1)
  8907. CHECK (l[3000] == -4.310323238372803f && r[4095] == -0.49086111783981323f);
  8908. else
  8909. CHECK (l[1000] == -0.8292768001556396f && r[3000] == 0.9836107492446899f && l[4095] == 0.06754066050052643f);
  8910. // editor texts and graph
  8911. CHECK (lynx::formatEffectParam (lynx::EffectKind::parametricEq, lynx::peq::slopeParam, slot->params[lynx::peq::slopeParam], 120.0, slot->params)
  8912. == (v1 ? "12 dB/oct" : "Steep 8"));
  8913. CHECK (lynx::peq::widthText (slot->params, 0).rfind ("BW ", 0) == 0);
  8914. const auto lowCutDb = lynx::peq::responseDb (slot->params, 44100.0, 25.0), midDb = lynx::peq::responseDb (slot->params, 44100.0, 1000.0);
  8915. CHECK (lowCutDb < midDb - 5.0); // high pass below ~100 Hz (Steep 8 in the EQ 2 state, 12 dB/oct in v1)
  8916. }
  8917. // a native LYNX EQ is untouched by the FL law
  8918. CHECK (! lynx::peq::flLaw (lynx::makeEffectSlot (lynx::EffectKind::parametricEq).params));
  8919. }
  8920. TEST (fruity_panomatic_balance_blood_overdrive)
  8921. {
  8922. // REA_rodada58: goldens from the installation's DLLs (EXEC) on our own states, 44.1 kHz, blocks of 512
  8923. const lynx::ScopedFlushDenormals ftz;
  8924. std::vector<float> inL (4096), inR (4096);
  8925. for (int n = 0; n < 4096; ++n)
  8926. {
  8927. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  8928. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  8929. }
  8930. struct Gold { int i; float l, r; };
  8931. auto close = [] (float a, float b) { return std::abs (a - b) <= 2.0e-7f * std::max (1.0f, std::abs (b)); };
  8932. auto check = [&] (const std::vector<float>& l, const std::vector<float>& r, std::initializer_list<Gold> gold) {
  8933. for (const auto& g : gold)
  8934. CHECK (close (l[(std::size_t) g.i], g.l) && close (r[(std::size_t) g.i], g.r));
  8935. };
  8936. auto st32 = [] (std::initializer_list<int> v) {
  8937. std::vector<std::uint8_t> b;
  8938. for (int x : v)
  8939. for (int k = 0; k < 4; ++k)
  8940. b.push_back ((std::uint8_t) ((unsigned) x >> (8 * k)));
  8941. return b;
  8942. };
  8943. // PanOMatic: the engine started as LYNX starts it (one silent frame, then FL's flush), and from silence (fade-in)
  8944. auto pom = [&] (lynx::flbasic::PanOMaticState s, bool prime, std::initializer_list<Gold> gold) {
  8945. lynx::flbasic::PanOMaticEngine e;
  8946. e.prepare (44100);
  8947. e.set (s);
  8948. if (prime)
  8949. e.prime();
  8950. std::vector<float> l (inL), r (inR);
  8951. for (int i = 0; i < 4096; i += 512)
  8952. e.process (l.data() + i, r.data() + i, 512);
  8953. check (l, r, gold);
  8954. };
  8955. pom ({ 40, 300, 2, 1, 128, 50000 }, true, { { 100, 0.0f, -1.44884729f }, { 1000, -1.78870034f, 0.778897762f }, { 2047, 0.697593153f, -1.26396084f },
  8956. { 3000, -1.78870034f, -1.32851648f }, { 4095, 0.268305063f, 0.851258814f } });
  8957. pom ({ 200, 180, 1, 2, 100, 30000 }, true, { { 100, 0.0f, -0.347919852f }, { 1000, -0.322590947f, 0.0788071603f }, { 2047, 0.126614109f, -0.26421836f },
  8958. { 3000, -0.326532423f, -0.435213327f }, { 4095, 0.0493027233f, 0.226084977f } });
  8959. pom ({ 90, 256, 0, 1, 77, 40000 }, false, { { 100, -0.0292895138f, -0.110000029f }, { 1000, -1.31911683f, 0.666457176f }, { 2047, 0.477211952f, -0.864247262f },
  8960. { 3000, -1.59167564f, -0.591367304f }, { 4095, 0.268362761f, 0.537029684f } });
  8961. // Balance: flushed and from silence
  8962. auto bal = [&] (lynx::flbasic::BalanceState s, bool flush, std::initializer_list<Gold> gold) {
  8963. lynx::flbasic::BalanceEngine e;
  8964. e.prepare (44100);
  8965. e.set (s);
  8966. if (flush)
  8967. e.flush();
  8968. std::vector<float> l (inL), r (inR);
  8969. for (int i = 0; i < 4096; i += 512)
  8970. e.process (l.data() + i, r.data() + i, 512);
  8971. check (l, r, gold);
  8972. };
  8973. bal ({ 77, 100 }, true, { { 100, 0.0f, -0.136579692f }, { 1000, -0.0669578835f, -0.0282340273f }, { 2047, 0.0261135735f, -0.0795038193f },
  8974. { 3000, -0.0669578835f, -0.226895392f }, { 4095, 0.0100436825f, 0.0954951793f } });
  8975. bal ({ -128, 320 }, false, { { 100, -0.28914085f, 0.0f }, { 1000, -1.03179514f, 0.0f }, { 2047, -0.578702569f, 0.0f },
  8976. { 3000, -3.18510628f, 0.0f }, { 4095, 1.14394665f, 0.0f } });
  8977. // Blood Overdrive: rate first, then the state (its smoothers settle for one second of samples)
  8978. auto od = [&] (std::array<int, 6> raw, std::initializer_list<Gold> gold) {
  8979. lynx::flbasic::BloodOverdriveEngine e;
  8980. e.start (44100, { raw });
  8981. std::vector<float> l (inL), r (inR);
  8982. for (int i = 0; i < 4096; i += 512)
  8983. e.process (l.data() + i, r.data() + i, 512);
  8984. check (l, r, gold);
  8985. };
  8986. od ({ 3000, 7000, 5000, 1, 9000, 5000 }, { { 100, -0.0442250744f, 0.170355186f }, { 1000, 0.0470311977f, 0.0765217245f },
  8987. { 2047, 0.0261161141f, -0.0341798998f }, { 3000, 0.0470311977f, 0.0213619489f },
  8988. { 4095, 0.0402069278f, 0.0499244779f } });
  8989. // the mixer effects from the FL import: controls as parameters; host blocks of any size
  8990. auto viaImport = [&] (const char* plugin, const std::vector<std::uint8_t>& st, lynx::EffectKind kind, std::initializer_list<Gold> gold) {
  8991. const auto slot = lynx::effectFromFl (plugin, st, 120.0);
  8992. CHECK (slot && slot->kind == kind);
  8993. if (! slot)
  8994. return;
  8995. auto fx = lynx::makeEffect (*slot);
  8996. fx->prepare (44100.0);
  8997. std::vector<float> l (inL), r (inR);
  8998. fx->process (l.data(), r.data(), 1024, slot->params, 120.0);
  8999. fx->process (l.data() + 1024, r.data() + 1024, 1024, slot->params, 120.0);
  9000. fx->process (l.data() + 2048, r.data() + 2048, 2048, slot->params, 120.0);
  9001. check (l, r, gold);
  9002. };
  9003. viaImport ("Fruity PanOMatic", st32 ({ 40, 300, 2, 1, 128, 50000 }), lynx::EffectKind::fruityPanOMatic,
  9004. { { 1000, -1.78870034f, 0.778897762f }, { 4095, 0.268305063f, 0.851258814f } });
  9005. viaImport ("Fruity Balance", st32 ({ 0, 256 }), lynx::EffectKind::fruityBalance,
  9006. { { 100, 0.0f, -1.10000002f }, { 1000, -1.66666663f, 0.899999976f }, { 4095, 0.25f, 0.600000024f } });
  9007. viaImport ("Fruity Blood Overdrive", st32 ({ 3, 0, 5000, 2578, 1, 0, 2911, 0, 0 }), lynx::EffectKind::fruityBloodOverdrive,
  9008. { { 100, -0.122698173f, 0.0206716377f }, { 2047, 0.0128381588f, -0.0274834726f }, { 4095, -0.0899387598f, -0.121109024f } });
  9009. {
  9010. const auto slot = lynx::effectFromFl ("Fruity PanOMatic", st32 ({ 119, 256, 1, 2, 64, 27500 }), 120.0);
  9011. CHECK (slot && lynx::flbasic::panOMaticFromParams (slot->params.data()).pan == 119
  9012. && lynx::flbasic::panOMaticFromParams (slot->params.data()).target == 2);
  9013. if (slot)
  9014. {
  9015. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityPanOMatic, 2, slot->params[2], 120.0) == "Triangle");
  9016. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityPanOMatic, 1, slot->params[1], 120.0) == "+0.0 dB");
  9017. }
  9018. }
  9019. // unreadable states keep the old approximations
  9020. CHECK (lynx::effectFromFl ("Fruity PanOMatic", { 1, 2, 3 }, 120.0)->kind == lynx::EffectKind::utility);
  9021. CHECK (lynx::effectFromFl ("Fruity Balance", { 1, 2, 3 }, 120.0)->kind == lynx::EffectKind::utility);
  9022. CHECK (lynx::effectFromFl ("Fruity Blood Overdrive", { 1, 2, 3 }, 120.0)->kind == lynx::EffectKind::distortion);
  9023. }
  9024. // ---- Sytrus (REA_rodada31) -------------------------------------------------------------------------------------------
  9025. // A Sytrus state built from scratch (version 25 layout, our own values — no FL preset data), zlib "stored" blocks.
  9026. namespace
  9027. {
  9028. struct SytrusTestPatch
  9029. {
  9030. std::vector<std::int32_t> main = std::vector<std::int32_t> (240, 0); // 962-byte main block as ints (+ mute bytes)
  9031. struct Op
  9032. {
  9033. int p[10] { 0, 256, 0, 0, 0, 0, 0, 0, 0, 0 };
  9034. int ratio = 20000, offset = 0, noise = 0;
  9035. std::vector<float> amps { 1.0f };
  9036. std::uint8_t flags[8] {};
  9037. int env1[3] { -1, -1, -1 }; // volume envelope: on (≠ −1 → on), decay point, sustain point
  9038. std::vector<std::pair<double, double>> env1pts;
  9039. } ops[6];
  9040. struct Filt
  9041. {
  9042. int p[10] { 0, 0, 128, 0, 128, 0, 0, 128, 128, 0 };
  9043. int type = 3;
  9044. } filts[3];
  9045. std::array<std::array<std::uint8_t, 23>, 3> delays {};
  9046. std::array<std::int32_t, 9> reverb { 19, 221, 0, 50, 80, 30, 221, 2, 0 };
  9047. bool reverbOn = false;
  9048. int eqmode = 0;
  9049. SytrusTestPatch()
  9050. {
  9051. main[0] = 128, main[1] = 128;
  9052. for (int i = 0; i < 8; ++i)
  9053. main[(std::size_t) (165 + i)] = (i % 4 == 2) ? 0 : 128;
  9054. main[173] = 1, main[175] = 128, main[188] = 128, main[189] = -1, main[190] = main[191] = 256;
  9055. }
  9056. void cell (int layer, int row, int col, int v) { main[(std::size_t) (3 + layer * 81 + row * 9 + col)] = v; }
  9057. std::vector<std::uint8_t> blob() const
  9058. {
  9059. std::vector<std::uint8_t> b;
  9060. 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))); };
  9061. auto f32 = [&] (float v) { std::int32_t x; std::memcpy (&x, &v, 4); i32 (x); };
  9062. 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))); };
  9063. // point lists: kind 1 envelope, 2 LFO, 3 map / shaper curve; format version 3
  9064. auto env = [&] (int kind, const std::vector<std::pair<double, double>>& pts, int on, int decay, int sus) {
  9065. i32 (kind), i32 (3), i32 ((std::int32_t) pts.size());
  9066. for (const auto& [dt, y] : pts)
  9067. f64 (dt), f64 (y), f32 (0.0f), i32 (0);
  9068. i32 (0), i32 (on), i32 (decay), i32 (-1), i32 (sus);
  9069. };
  9070. for (int i = 0; i < 240; ++i)
  9071. if (4 * i + 4 <= 0x320)
  9072. i32 (main[(std::size_t) i]);
  9073. b.resize (962, 0); // mute bytes: none muted
  9074. i32 (0); // global declick amount
  9075. b.push_back (0), b.push_back (0), b.push_back (0); // OSC grouping, DC stage, GIBBS OFF
  9076. for (int k = 0; k < 9; ++k)
  9077. b.push_back (k == 7 ? 1 : 0); // flag group (release multiplier on, as Default.fst)
  9078. for (const auto& d : delays)
  9079. b.insert (b.end(), d.begin(), d.end());
  9080. for (auto v : reverb)
  9081. i32 (v);
  9082. b.push_back (reverbOn ? 1 : 0), b.push_back (0);
  9083. i32 (eqmode);
  9084. for (int k = 0; k < 3; ++k)
  9085. i32 (0), i32 (30000), i32 (20000), i32 (k);
  9086. b.push_back (0); // no info text
  9087. for (int u = 0; u < 10; ++u)
  9088. {
  9089. const int narts = u < 6 ? 6 : (u < 9 ? 8 : 2);
  9090. for (int j = 0; j < 10; ++j)
  9091. i32 (u < 6 ? ops[u].p[j] : (u < 9 ? filts[u - 6].p[j] : 0));
  9092. if (u < 6)
  9093. {
  9094. const auto& o = ops[u];
  9095. const auto at = b.size();
  9096. b.resize (at + 0x828, 0);
  9097. auto put = [&] (std::size_t off, const void* src, std::size_t n) { std::memcpy (b.data() + at + off, src, n); };
  9098. put (0, &o.ratio, 4), put (4, &o.offset, 4), put (8, &o.noise, 4);
  9099. put (0xc, o.amps.data(), 4 * o.amps.size());
  9100. const std::int32_t shape[5] { 0, 0, 0, 0, 128 };
  9101. put (0x80c, shape, sizeof shape);
  9102. put (0x820, o.flags, 8);
  9103. }
  9104. else if (u < 9)
  9105. {
  9106. const auto at = b.size();
  9107. b.resize (at + 0x30, 0);
  9108. std::memcpy (b.data() + at + 0x2c, &filts[u - 6].type, 4);
  9109. env (3, { { 0.0, 0.0 }, { 1.0, 1.0 } }, 0, -1, -1);
  9110. b.push_back (0);
  9111. }
  9112. for (int k = 0; k < narts; ++k)
  9113. {
  9114. const bool add = k == 0 || k == 3 || (u >= 6 && u <= 8 && (k == 2 || k == 3)) || (u < 6 && k == 4);
  9115. const double neutral = add ? 0.5 : 1.0;
  9116. i32 (3), i32 (0), f32 (1.0f), f32 (1.0f);
  9117. if (u < 6 && k == 1 && ops[u].env1[0] != -1)
  9118. env (1, ops[u].env1pts, -1, ops[u].env1[1], ops[u].env1[2]);
  9119. else
  9120. env (1, { { 0.0, 0.0 }, { 1.0, 1.0 } }, 0, -1, -1);
  9121. i32 (128), i32 (128), i32 (0), i32 (128);
  9122. env (2, { { 0.0, 1.0 }, { 1.0, 1.0 } }, 0, -1, -1);
  9123. i32 (40000), i32 (0), i32 (0), i32 (0), i32 (0);
  9124. for (int m = 0; m < 6; ++m)
  9125. env (3, { { 0.0, neutral }, { 1.0, neutral } }, 0, -1, -1);
  9126. }
  9127. }
  9128. b.push_back (0), b.push_back (0);
  9129. // zlib with stored blocks
  9130. std::vector<std::uint8_t> z { 0x78, 0x01 };
  9131. for (std::size_t at = 0; at < b.size(); at += 65535)
  9132. {
  9133. const auto n = std::min<std::size_t> (65535, b.size() - at);
  9134. z.push_back (at + n >= b.size() ? 1 : 0);
  9135. z.push_back ((std::uint8_t) n), z.push_back ((std::uint8_t) (n >> 8));
  9136. z.push_back ((std::uint8_t) ~n), z.push_back ((std::uint8_t) (~n >> 8));
  9137. z.insert (z.end(), b.begin() + (std::ptrdiff_t) at, b.begin() + (std::ptrdiff_t) (at + n));
  9138. }
  9139. std::uint32_t a = 1, c = 0;
  9140. for (auto x : b)
  9141. a = (a + x) % 65521, c = (c + a) % 65521;
  9142. for (int k = 3; k >= 0; --k)
  9143. z.push_back ((std::uint8_t) (((c << 16) | a) >> (8 * k)));
  9144. std::vector<std::uint8_t> out { 25, 0, 0, 0 };
  9145. out.insert (out.end(), z.begin(), z.end());
  9146. return out;
  9147. }
  9148. };
  9149. // renders like FL's Gen_Render calls: notes start/release at the block that contains them
  9150. std::pair<std::vector<float>, std::vector<float>> sytrusRender (const std::vector<std::uint8_t>& blob, bool renderMode, int n,
  9151. std::vector<std::array<int, 3>> notes, double fs = 44100.0)
  9152. {
  9153. lynx::sytrus::Engine e;
  9154. e.setTempo (120.0);
  9155. e.prepare (fs, renderMode, 512);
  9156. e.setPatch (lynx::sytrus::parsePatch (blob));
  9157. std::vector<float> l ((std::size_t) n), r ((std::size_t) n);
  9158. for (int n0 = 0; n0 < n; n0 += 512)
  9159. {
  9160. for (std::size_t k = 0; k < notes.size(); ++k)
  9161. {
  9162. if (notes[k][1] >= n0 && notes[k][1] < n0 + 512)
  9163. {
  9164. lynx::sytrus::NoteParams p;
  9165. p.key = (float) notes[k][0];
  9166. p.id = (std::int64_t) k + 1;
  9167. e.noteOn (p);
  9168. }
  9169. if (notes[k][2] >= n0 && notes[k][2] < n0 + 512)
  9170. e.release ((std::int64_t) k + 1);
  9171. }
  9172. e.render (l.data() + n0, r.data() + n0, std::min (512, n - n0), 1.0f, 0.0f);
  9173. }
  9174. return { l, r };
  9175. }
  9176. } // namespace
  9177. TEST (sytrus_engine_sine)
  9178. {
  9179. // one sine operator, ratio 1, OUT 256, no envelopes: C5 = 523.2511 Hz at 0.7071 (centre of the pan law, §6.1)
  9180. SytrusTestPatch t;
  9181. t.cell (0, 0, 8, 256);
  9182. const auto blob = t.blob();
  9183. CHECK (lynx::sytrus::stateUsable (blob));
  9184. CHECK (lynx::sytrus::parsePatch (blob) != nullptr);
  9185. auto broken = blob;
  9186. broken.resize (broken.size() / 2);
  9187. CHECK (! lynx::sytrus::stateUsable (broken));
  9188. const auto [l, r] = sytrusRender (blob, false, 8192, { { 60, 0, 4096 } });
  9189. double worst = 0.0;
  9190. for (int k = 0; k < 4000; ++k)
  9191. 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)));
  9192. CHECK (worst < 2e-5);
  9193. CHECK (l == r);
  9194. // 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
  9195. // 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)
  9196. int last = 0;
  9197. for (int k = 0; k < 8192; ++k)
  9198. if (l[(std::size_t) k] != 0.0f)
  9199. last = k;
  9200. CHECK (last == 4895);
  9201. // key 69 = 880 Hz; 48 kHz
  9202. const auto [l2, r2] = sytrusRender (blob, false, 4096, { { 69, 0, -1 } }, 48000.0);
  9203. worst = 0.0;
  9204. for (int k = 0; k < 4000; ++k)
  9205. 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)));
  9206. CHECK (worst < 5e-5);
  9207. (void) r2;
  9208. }
  9209. TEST (sytrus_engine_reference)
  9210. {
  9211. // FM 2→1, Vanilla LP filter, volume envelope, unison 3, delay and reverb, 2× oversampling with the 8-sample
  9212. // envelope tick (render settings): against our Python reference model of REA_rodada31 (sytmodel.render)
  9213. SytrusTestPatch t;
  9214. t.main[195] = 0, t.main[196] = 1, t.main[193] = 0, t.main[194] = 1;
  9215. t.main[173] = 3, t.main[174] = 64, t.main[176] = 100;
  9216. t.ops[1].ratio = 40000;
  9217. t.cell (0, 0, 1, 128); // op 2 → op 1 FM
  9218. t.cell (0, 0, 8, 128);
  9219. t.cell (0, 6, 0, 256); // op 1 → filter 1
  9220. t.cell (0, 6, 8, 256);
  9221. t.cell (0, 0, 7, 200); // FX send
  9222. t.filts[0].p[0] = 1, t.filts[0].p[2] = 180, t.filts[0].p[3] = 60;
  9223. t.ops[0].env1[0] = 0, t.ops[0].env1[1] = 1, t.ops[0].env1[2] = 2;
  9224. t.ops[0].env1pts = { { 0.0, 0.0 }, { 0.05, 1.0 }, { 0.1, 0.6 }, { 0.2, 0.0 } };
  9225. const std::int32_t d0[5] { 40, 100, 50, 100, 0 };
  9226. std::memcpy (t.delays[0].data(), d0, 20);
  9227. t.delays[0][22] = 1;
  9228. t.reverb = { 19, 221, 10, 50, 80, 30, 100, 2, 100 };
  9229. t.reverbOn = true;
  9230. const auto blob = t.blob();
  9231. const auto [l, r] = sytrusRender (blob, true, 12000, { { 60, 0, 6000 } });
  9232. // reference values (model render_mode=True, 44.1 kHz, block 512, key 60, velocity 0.78125, release at 6000); the same
  9233. // state rendered by FL's own Sytrus DLL in render mode differs from this by −136 dB (EXEC, REA round 31 harness)
  9234. const std::array<std::pair<int, float>, 8> golden { { { 100, -0.00235529384f }, { 777, -0.175126165f }, { 2000, 0.30976975f }, { 4321, -0.0213534199f },
  9235. { 6100, 0.202372357f }, { 7000, 0.249530748f }, { 9000, 0.0307101421f }, { 11999, 0.176390275f } } };
  9236. for (const auto& [i, v] : golden)
  9237. CHECK (std::abs (l[(std::size_t) i] - v) < 1e-6f);
  9238. const std::array<std::pair<int, float>, 3> goldenR { { { 777, -0.166890964f }, { 4321, -0.212358952f }, { 9000, -0.0251909085f } } };
  9239. for (const auto& [i, v] : goldenR)
  9240. CHECK (std::abs (r[(std::size_t) i] - v) < 1e-6f);
  9241. }
  9242. TEST (sytrus_instrument)
  9243. {
  9244. // the LYNX instrument: state in ChannelDef::pluginState, channel volume/pan recovered from the rack gains
  9245. SytrusTestPatch t;
  9246. t.cell (0, 0, 8, 256);
  9247. lynx::ChannelDef c;
  9248. c.instrument = lynx::InstrumentKind::sytrus;
  9249. c.pluginState = t.blob();
  9250. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sytrus);
  9251. inst->prepare (44100.0);
  9252. inst->configure (c, 120.0);
  9253. CHECK (inst->idle());
  9254. std::vector<float> l (4096), r (4096);
  9255. inst->noteOn (60, 100.0f / 128.0f);
  9256. // 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
  9257. inst->render (l.data(), r.data(), 4096, 0.5f, 0.5f);
  9258. double peak = 0.0;
  9259. for (int k = 1000; k < 4096; ++k)
  9260. peak = std::max (peak, (double) std::abs (l[(std::size_t) k]));
  9261. CHECK (std::abs (peak - 0.5 * 0.70710678) < 1e-3); // Final.Vol 0.5 × the plugin's centre pan law
  9262. CHECK (! inst->idle());
  9263. inst->noteOff (60);
  9264. std::vector<float> tail (44100 * 3), tailR (tail.size());
  9265. inst->render (tail.data(), tailR.data(), (int) tail.size(), 0.5f, 0.5f);
  9266. CHECK (inst->idle());
  9267. CHECK (tail.back() == 0.0f);
  9268. }
  9269. TEST (harmor_effects_rack)
  9270. {
  9271. // REA_rodada38: the rack runs in the state's unit order and the records' FX send joins it at unit 4 — with the FX
  9272. // amount at 1000 every record output bypasses a reverb placed before unit 4 (EXEC: −600 dB vs the rack off)
  9273. auto blob = harmorTestState (false);
  9274. auto set = [&] (std::vector<std::uint8_t>& st, int part, int index, std::int32_t v) {
  9275. std::memcpy (st.data() + lynx::harmorParamOffset (st, part, index), &v, 4);
  9276. };
  9277. set (blob, 0, 10, 1000); // all to the FX send
  9278. auto render = [] (const std::vector<std::uint8_t>& st) {
  9279. return lynx::harmor::renderReference (st, { { 0, 60, 0.8, 1.0, 0.0, 6000 } }, 44100.0, 12000);
  9280. };
  9281. const auto dry = render (blob);
  9282. auto rev = blob;
  9283. set (rev, -1, 22, 1); // reverb on: room 50, diffusion 100, decay 2^3·0.1 s, high cut / damping off, wet 0.8
  9284. 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);
  9285. set (rev, -1, 30, 2), set (rev, -1, 31, 800);
  9286. const auto bypassed = render (rev);
  9287. double eb = 0.0, db = 0.0;
  9288. for (std::size_t i = 0; i < dry.size(); ++i)
  9289. eb += (double) (bypassed[i] - dry[i]) * (bypassed[i] - dry[i]), db += (double) dry[i] * dry[i];
  9290. CHECK (eb < 1e-12 * db && db > 1e-3); // (float rounding of the rack's buffers only)
  9291. auto after = rev;
  9292. const std::int32_t order[6] { 0, 1, 2, 4, 3, 5 }; // the join before the reverb
  9293. for (int k = 0; k < 6; ++k)
  9294. set (after, -1, 53 + k, order[k]);
  9295. const auto wet = render (after);
  9296. double e = 0.0, d = 0.0;
  9297. bool finite = true;
  9298. for (std::size_t i = 0; i < wet.size(); ++i)
  9299. e += (double) (wet[i] - dry[i]) * (wet[i] - dry[i]), d += (double) dry[i] * dry[i], finite = finite && std::isfinite (wet[i]);
  9300. CHECK (finite && e > 1e-4 * d && d > 1e-3);
  9301. // a distortion (type 6) and a delay on the dry path change the sound, stay finite, and the instrument ends quiet
  9302. auto dist = harmorTestState (false);
  9303. {
  9304. const auto at = lynx::harmorParamOffset (dist, -1, 0) + 0xd4 + 0x18 + 48; // global misc +48: distortion type
  9305. const std::int32_t six = 6;
  9306. std::memcpy (dist.data() + at, &six, 4);
  9307. }
  9308. set (dist, -1, 17, 600), set (dist, -1, 19, 1000), set (dist, -1, 21, 1100);
  9309. 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);
  9310. set (dist, -1, 47, 1050);
  9311. lynx::ChannelDef c;
  9312. c.instrument = lynx::InstrumentKind::harmor;
  9313. c.pluginState = dist;
  9314. auto inst = lynx::makeInstrument (lynx::InstrumentKind::harmor);
  9315. inst->configure (c, 120.0);
  9316. inst->prepare (44100.0);
  9317. std::vector<float> l (44100 * 4), r (l.size());
  9318. inst->noteOn (60, 0.8f);
  9319. double energy = 0.0;
  9320. for (int s = 0; s < (int) l.size(); s += 512)
  9321. {
  9322. if (s == 8192)
  9323. inst->noteOff (60);
  9324. inst->render (l.data() + s, r.data() + s, 512, 1.0f, 1.0f);
  9325. }
  9326. for (std::size_t i = 0; i < l.size(); ++i)
  9327. finite = finite && std::isfinite (l[i]) && std::isfinite (r[i]), energy += (double) l[i] * l[i];
  9328. CHECK (finite && energy > 1.0);
  9329. std::vector<float> tail (44100 * 6), tailR (tail.size());
  9330. for (int s = 0; s < (int) tail.size(); s += 512)
  9331. inst->render (tail.data() + s, tailR.data() + s, 512, 1.0f, 1.0f);
  9332. CHECK (inst->idle());
  9333. }
  9334. TEST (harmless_engine)
  9335. {
  9336. // REA_rodada32: state parsing, the additive engine against our reference model (hlmodel.py), host levels, voice end
  9337. std::vector<std::uint8_t> blob;
  9338. 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))); };
  9339. std::array<std::int32_t, 100> p {}; // the reference's clean patch: Saw, open filter, no modulation
  9340. for (int i = 11; i < 23; ++i)
  9341. p[(std::size_t) i] = 1000;
  9342. 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;
  9343. 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;
  9344. // + resonance, pluck, 3-voice unison, tremolo, LFO on pitch
  9345. 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;
  9346. put (12);
  9347. for (auto v : p)
  9348. put (v);
  9349. for (int i = 0; i < 7; ++i)
  9350. put (0);
  9351. put (524); // Custom colour table: 524 × (amplitude, phase)
  9352. for (int i = 0; i < 1048; ++i)
  9353. {
  9354. const auto f = i < 524 ? 1.0f / (float) (i + 1) : 0.25f;
  9355. std::uint32_t bits = 0;
  9356. std::memcpy (&bits, &f, 4);
  9357. put ((std::int32_t) bits);
  9358. }
  9359. const auto parsed = lynx::harmlessParams (blob);
  9360. CHECK (parsed && (int) parsed->size() == lynx::harmlessParamCount);
  9361. CHECK (parsed && (*parsed)[54] == 3500.0f && (*parsed)[99] == 0.0f && (*parsed)[lynx::harmlessCustomFlagIndex] == 1.0f);
  9362. CHECK (parsed && (*parsed)[lynx::harmlessCustomIndex + 1] == 0.5f && (*parsed)[lynx::harmlessCustomIndex + 524] == 0.25f);
  9363. auto old = blob;
  9364. old[0] = 3; // versions below 4 are not Harmless states we know
  9365. CHECK (! lynx::harmlessParams (old));
  9366. old = blob;
  9367. old.resize (300);
  9368. CHECK (! lynx::harmlessParams (old));
  9369. lynx::ChannelDef c;
  9370. c.instrument = lynx::InstrumentKind::harmless;
  9371. c.modelParams = *parsed;
  9372. c.modelParams[lynx::harmlessCustomFlagIndex] = 0.0f; // colour 0 (Saw): the custom table is not read
  9373. auto render = [] (const lynx::ChannelDef& def, int total, float gl, float gr, int offAt) {
  9374. auto inst = lynx::makeInstrument (lynx::InstrumentKind::harmless);
  9375. inst->configure (def, 120.0);
  9376. inst->prepare (44100.0);
  9377. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  9378. inst->noteOn (60, 0.78125f);
  9379. for (int s = 0; s < total; s += 512)
  9380. {
  9381. if (s == offAt)
  9382. inst->noteOff (60);
  9383. inst->render (l.data() + s, r.data() + s, std::min (512, total - s), gl, gr);
  9384. }
  9385. return std::make_tuple (l, r, std::move (inst));
  9386. };
  9387. {
  9388. // reference model (REA round 32 hlmodel.py, C5 at FL velocity 100, channel volume 1, centre, released at 6144)
  9389. auto [l, r, inst] = render (c, 8192, 1.0f, 1.0f, 6144);
  9390. const std::tuple<int, float, float> golden[] {
  9391. { 0, 0.0f, 0.0f }, { 10, -0.02220994f, -0.06232700f }, { 100, 0.40623373f, 0.17308202f }, { 1000, -0.04091963f, -0.26821208f },
  9392. { 3000, 0.14519004f, 0.11027778f }, { 6000, 0.50411958f, 0.27901855f }, { 8191, 0.02117692f, -0.01466602f } };
  9393. for (const auto& [i, gl, gr] : golden)
  9394. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  9395. // after the release the record fades out (below 1e-4, then 30 ms) and the instrument goes idle
  9396. std::vector<float> tail (44100), tailR (tail.size());
  9397. inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
  9398. CHECK (inst->idle());
  9399. }
  9400. // FL's level law applied once: half the channel volume halves the output; full right pan silences the left side
  9401. const auto full = std::get<0> (render (c, 2048, 1.0f, 1.0f, -1));
  9402. const auto half = std::get<0> (render (c, 2048, 0.5f, 0.5f, -1));
  9403. CHECK (std::abs (half[100] - 0.5f * full[100]) < 1e-6f && std::abs (full[100]) > 0.1f);
  9404. auto mono = c;
  9405. mono.modelParams[25] = 0.0f; // no tremolo, no unison pan spread
  9406. mono.modelParams[65] = 1.0f;
  9407. const auto [pl, pr, pi] = render (mono, 2048, 0.0f, 1.41421356f, -1);
  9408. double el = 0.0, er = 0.0;
  9409. for (int i = 0; i < 2048; ++i)
  9410. el += (double) pl[(std::size_t) i] * pl[(std::size_t) i], er += (double) pr[(std::size_t) i] * pr[(std::size_t) i];
  9411. CHECK (el < 1e-10 * er && er > 1.0);
  9412. }
  9413. TEST (startup_sound)
  9414. {
  9415. // the splash's sound: the right length, finite, at its peak level, quiet before the whoosh swells,
  9416. // a landing "pop" at impactTime and a chime ringing after it
  9417. using namespace lynx::startup;
  9418. const auto sound = makeSound (48000.0);
  9419. CHECK (sound.left.size() == (std::size_t) (soundLength * 48000.0) && sound.right.size() == sound.left.size());
  9420. float peak = 0.0f;
  9421. bool finite = true;
  9422. for (std::size_t i = 0; i < sound.left.size(); ++i)
  9423. {
  9424. finite = finite && std::isfinite (sound.left[i]) && std::isfinite (sound.right[i]);
  9425. peak = std::max ({ peak, std::abs (sound.left[i]), std::abs (sound.right[i]) });
  9426. }
  9427. CHECK (finite && std::abs (peak - peakLevel) < 1.0e-4f);
  9428. auto rms = [&sound] (double from, double to) {
  9429. double e = 0.0;
  9430. const auto a = (std::size_t) (from * 48000.0), b = (std::size_t) (to * 48000.0);
  9431. for (auto i = a; i < b; ++i)
  9432. e += (double) sound.left[i] * sound.left[i];
  9433. return std::sqrt (e / (double) (b - a));
  9434. };
  9435. CHECK (rms (0.0, 0.05) < 0.2 * rms (impactTime - 0.05, impactTime)); // the whoosh swells
  9436. CHECK (rms (impactTime, impactTime + 0.05) > rms (impactTime - 0.05, impactTime)); // the landing
  9437. CHECK (rms (0.8, 1.0) > 0.005 && rms (soundLength - 0.02, soundLength) < rms (0.8, 1.0)); // chime rings, tail fades
  9438. CHECK (makeSound (48000.0).left == sound.left); // the same sound on every start
  9439. }
  9440. namespace
  9441. {
  9442. // little-endian byte writer for the state builders below
  9443. struct Bytes
  9444. {
  9445. std::vector<std::uint8_t> b;
  9446. void raw (const char* s, std::size_t n) { b.insert (b.end(), s, s + n); }
  9447. 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); }
  9448. void f32 (float v) { b.insert (b.end(), reinterpret_cast<const std::uint8_t*> (&v), reinterpret_cast<const std::uint8_t*> (&v) + 4); }
  9449. void zeros (std::size_t n) { b.insert (b.end(), n, 0); }
  9450. void bytes (const std::vector<std::uint8_t>& o) { b.insert (b.end(), o.begin(), o.end()); }
  9451. };
  9452. // A GMS state written from scratch (REA_rodada12 §1.1 layout, "Syn4"): oscillator waves that don't exist (silent
  9453. // tables, so the result does not depend on an FL installation), noise through the ladder with envelope and LFO,
  9454. // unison, slide, distortion, delay, reverb and the output EQ.
  9455. std::vector<std::uint8_t> gmsTestState()
  9456. {
  9457. std::array<float, 212> P {};
  9458. for (int i : { 1, 2, 44, 55, 56, 57, 58, 59, 60, 207, 208, 209, 210, 211 })
  9459. P[(std::size_t) i] = 0.5f;
  9460. P[0] = 0.75f, P[4] = 0.1f, P[5] = 0.4f, P[6] = 0.6f, P[7] = 0.3f, P[8] = 0.8f;
  9461. 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;
  9462. 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;
  9463. P[147] = 1.0f, P[117] = 0.3f, P[127] = 0.6f; // distortion
  9464. P[153] = 1.0f, P[123] = 0.5f, P[133] = 0.4f, P[143] = 0.4f; // delay (1/2 beat)
  9465. P[154] = 1.0f, P[124] = 0.6f, P[134] = 0.5f; // reverb
  9466. P[206] = 1.0f, P[207] = 0.7f, P[211] = 0.3f; // output EQ
  9467. Bytes preset;
  9468. preset.raw ("4nyS", 4);
  9469. for (const char* path : { "NoSuchDir\\NoWave A.wav", "NoSuchDir\\NoWave B.wav", "NoSuchDir\\NoWave C.wav", "", "", "", "" })
  9470. {
  9471. const auto n = std::strlen (path);
  9472. preset.raw (path, n);
  9473. preset.zeros (512 - n);
  9474. }
  9475. preset.i32 (0), preset.i32 (0), preset.i32 (0);
  9476. for (auto v : P)
  9477. preset.f32 (v);
  9478. preset.raw ("LYNX test", 9);
  9479. preset.zeros (512 - 9);
  9480. Bytes s;
  9481. s.raw ("SSLF", 4);
  9482. s.i32 ((std::int32_t) preset.b.size());
  9483. s.bytes (preset.b);
  9484. return s.b;
  9485. }
  9486. // A Morphine state written from scratch (REA_rodada16 §3 / REA_rodada39 §2.2 layout): generators A and B with a few
  9487. // frames each (A loops, PWM on A), C and D silent, a morph line A→B, four envelopes (Eg1 → pitch and velocity →
  9488. // amplitude through the matrix), the master ADSR and every plugin effect on.
  9489. std::vector<std::uint8_t> morphineTestState()
  9490. {
  9491. auto mbp = [] (int seed) {
  9492. Bytes b;
  9493. b.raw ("0PBM", 4);
  9494. for (int a = 0; a < 3; ++a)
  9495. for (int byte = 0; byte < 16; ++byte)
  9496. {
  9497. b.b.push_back (0xff);
  9498. for (int bit = 0; bit < 8; ++bit)
  9499. {
  9500. const auto k = byte * 8 + bit;
  9501. const float v = a == 0 ? std::cbrt (1.0f / (float) (k + 1 + seed * (k % 3))) * (k < 40 ? 1.0f : 0.2f)
  9502. : a == 1 ? 0.1f * (float) ((k * 7 + seed) % 11 - 5) / 5.0f
  9503. : 0.02f * (float) ((k * 5 + seed) % 7 - 3);
  9504. b.f32 (v);
  9505. }
  9506. }
  9507. return b.b;
  9508. };
  9509. auto msp = [&] (int frames, int seed, int loopMode) {
  9510. Bytes b;
  9511. b.raw ("0PSM", 4);
  9512. b.i32 (frames);
  9513. for (int f = 0; f < frames; ++f)
  9514. {
  9515. const auto m = mbp (seed + f);
  9516. b.i32 (20 + 7 * f), b.f32 (1.0f - 0.2f * (float) f), b.i32 ((std::int32_t) m.size());
  9517. b.bytes (m);
  9518. }
  9519. b.i32 (0x424);
  9520. b.i32 (10), b.i32 (40), b.i32 (0), b.i32 (loopMode);
  9521. b.f32 (0.45f), b.f32 (0.6f), b.f32 (0.5f), b.i32 (0);
  9522. for (int k = 0; k < 128; ++k)
  9523. b.f32 (0.3f);
  9524. for (int k = 0; k < 128; ++k)
  9525. b.f32 (1.0f);
  9526. return b.b;
  9527. };
  9528. auto mvc = [&] (int frames, int seed, int pwmMode) {
  9529. Bytes z;
  9530. z.raw ("0HZM", 4);
  9531. z.i32 (1);
  9532. const auto s = msp (frames, seed, 1);
  9533. z.i32 (128), z.f32 (frames > 1 ? 0.9f : 0.0f), z.i32 ((std::int32_t) s.size());
  9534. z.bytes (s);
  9535. Bytes b;
  9536. b.raw ("0CVM", 4);
  9537. b.i32 ((std::int32_t) z.b.size());
  9538. b.bytes (z.b);
  9539. 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);
  9540. return b.b;
  9541. };
  9542. Bytes p;
  9543. p.raw ("0roM", 4);
  9544. p.raw ("0RPM", 4);
  9545. for (const auto& g : { mvc (3, 1, 2), mvc (2, 7, 0), mvc (1, 3, 0), mvc (1, 5, 0) })
  9546. {
  9547. p.i32 ((std::int32_t) g.size());
  9548. p.bytes (g);
  9549. }
  9550. {
  9551. Bytes m;
  9552. m.raw ("0LMM", 4);
  9553. m.i32 (2), m.i32 (12);
  9554. m.f32 (0.0f), m.f32 (1.0f), m.i32 (60);
  9555. m.f32 (1.0f), m.f32 (1.0f), m.i32 (60);
  9556. m.i32 (0x54), m.i32 (0), m.i32 (0), m.i32 (0), m.i32 (0);
  9557. for (float v : { 1.0f, 0.8f, 0.0f, 0.0f, 0.5f, 0.4f, 0.5f, 0.5f })
  9558. m.f32 (v);
  9559. for (int k = 0; k < 4; ++k)
  9560. m.i32 (0);
  9561. m.f32 (0.5f), m.f32 (0.5f), m.f32 (0.5f), m.i32 (0);
  9562. p.i32 ((std::int32_t) m.b.size());
  9563. p.bytes (m.b);
  9564. }
  9565. {
  9566. Bytes d;
  9567. d.raw ("0DMM", 4);
  9568. d.i32 (4);
  9569. for (int e = 0; e < 4; ++e)
  9570. {
  9571. Bytes g;
  9572. g.raw ("0GEM", 4);
  9573. g.i32 (3), g.i32 (8);
  9574. g.f32 (0.0f), g.i32 (10), g.f32 (1.0f), g.i32 (40 + 10 * e), g.f32 (0.3f), g.i32 (50);
  9575. 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),
  9576. g.i32 (e == 0 ? 1 : 0);
  9577. d.i32 ((std::int32_t) g.b.size());
  9578. d.bytes (g.b);
  9579. }
  9580. d.i32 (0x94);
  9581. const int src[12] = { 5, 2 }, dst[12] = { 3, 1 };
  9582. const float amt[12] = { 0.56f, 0.7f };
  9583. for (int k = 0; k < 12; ++k)
  9584. d.i32 (src[k]);
  9585. for (int k = 0; k < 12; ++k)
  9586. d.i32 (dst[k]);
  9587. for (int k = 0; k < 12; ++k)
  9588. d.f32 (amt[k]);
  9589. p.i32 ((std::int32_t) d.b.size());
  9590. p.bytes (d.b);
  9591. }
  9592. {
  9593. 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
  9594. 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 …
  9595. 0.4f, 0.3f, 1.0f, 0.5f, 0.5f, 0.4f, 1.0f, 0.6f, 0.4f, 0.5f, // … delay, reverb, EQ …
  9596. 0.5f, 0.5f, 0.55f, 0.5f, 0.45f, 1.0f, 0.0f, 0.5f, 1.0f, 0.0f }; // EQ on, noise (muted)
  9597. Bytes g;
  9598. g.i32 (0x5c4);
  9599. for (auto v : A)
  9600. g.f32 (v);
  9601. g.zeros (0x5c4 - g.b.size());
  9602. p.bytes (g.b);
  9603. }
  9604. Bytes s;
  9605. s.raw ("SSLF", 4);
  9606. s.i32 ((std::int32_t) p.b.size());
  9607. s.bytes (p.b);
  9608. return s.b;
  9609. }
  9610. void dumpIfAsked (const char* var, const std::vector<std::uint8_t>& blob)
  9611. {
  9612. if (const char* path = std::getenv (var))
  9613. {
  9614. std::ofstream out (path, std::ios::binary);
  9615. out.write (reinterpret_cast<const char*> (blob.data()), (std::streamsize) blob.size());
  9616. }
  9617. }
  9618. } // namespace
  9619. TEST (dx10_engine)
  9620. {
  9621. // REA_rodada42: a Fruity DX10 state written from scratch (version 1: 22 raw parameters) against our reference model
  9622. // (dxmodel.py + dxproc.py, bit-identical to the installation's DLL), the channel processor, host levels and the
  9623. // instrument front-end
  9624. const std::int32_t P[22] = { 1000, 40000, 30000, 30000, 40000, 30000, 35000, 8000, 40000, 50000, 20000,
  9625. 25000, 15000, 30000, 20000, 10000, 20000, 30000, 5000, 30000, 20000, -1 };
  9626. std::vector<std::uint8_t> blob (4 + 22 * 4, 0);
  9627. blob[0] = 1;
  9628. std::memcpy (blob.data() + 4, P, sizeof (P));
  9629. CHECK (lynx::dx10StateUsable (blob) && lynx::dx10ProcessorNeutral (blob));
  9630. CHECK (lynx::dx10Param (blob, 21) == -1 && lynx::dx10Param (blob, 3) == 30000 && ! lynx::dx10Param (blob, 22));
  9631. CHECK (! lynx::dx10StateUsable (std::vector<std::uint8_t> (40, 0)));
  9632. {
  9633. auto bad = blob;
  9634. bad[0] = 3; // versions ≥ 3 are ignored by the plugin
  9635. CHECK (! lynx::dx10StateUsable (bad));
  9636. }
  9637. using N = lynx::dx10::RefNote;
  9638. 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 } };
  9639. struct Golden { std::vector<std::uint8_t> state; lynx::dx10::RefOptions opt; int rate; double el, er; std::tuple<int, float, float> at[6]; };
  9640. auto check = [&] (const Golden& g) {
  9641. const auto y = lynx::dx10::renderReference (g.state, notes, (double) g.rate, 16384, g.opt);
  9642. CHECK (y.size() == 2 * 16384);
  9643. if (y.size() != 2 * 16384)
  9644. return y;
  9645. double el = 0.0, er = 0.0;
  9646. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  9647. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  9648. CHECK (std::abs (el - g.el) <= 1e-7 * g.el && std::abs (er - g.er) <= 1e-7 * g.er);
  9649. for (const auto& [i, l, r] : g.at)
  9650. CHECK (std::abs (y[2 * (std::size_t) i] - l) <= 1e-7f * std::max (1e-16f, std::abs (l))
  9651. && std::abs (y[2 * (std::size_t) i + 1] - r) <= 1e-7f * std::max (1e-16f, std::abs (r)));
  9652. return y;
  9653. };
  9654. lynx::dx10::RefOptions plain, odd;
  9655. odd.block = 333, odd.maxPoly = 1;
  9656. check ({ blob, plain, 44100, 672.293115, 778.393071,
  9657. { { 100, -0.0943799689f, -0.154014125f }, { 1000, -0.0814286768f, -0.132879525f }, { 2500, 0.499493301f, 0.312498778f },
  9658. { 5000, -0.184045464f, 0.171641544f }, { 9000, -0.153369367f, -0.20805186f }, { 16383, -0.00441304967f, 0.200126961f } } });
  9659. // 48 kHz, odd host blocks, max polyphony 1 (the last note steals); the hard-right note keeps FL's x87 left gain
  9660. check ({ blob, odd, 48000, 122.232761, 493.320297,
  9661. { { 100, 0.0236493405f, 0.0385922194f }, { 1000, 0.189806491f, 0.309736073f }, { 2500, -0.425576478f, -0.176279545f },
  9662. { 5000, -3.54030076e-18f, -0.0578194074f }, { 9000, 1.37052479e-17f, 0.223831087f }, { 16383, 9.248742e-18f, 0.151048407f } } });
  9663. // the channel processor (§3): our own settings snapshot — pan LFO (triangle, delayed, faded in), volume envelope
  9664. // with tensions and LFO, cutoff envelope + square LFO, resonance LFO, pitch envelope, SVF LP ×2 (type 7)
  9665. auto record = [] (std::vector<std::uint8_t>& s, int k, std::initializer_list<std::pair<int, int>> fields) {
  9666. const std::int32_t neutral[17] = { 0, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, 0 };
  9667. std::memcpy (s.data() + 0x44 * k, neutral, sizeof (neutral));
  9668. for (const auto& [i, v] : fields)
  9669. std::memcpy (s.data() + 0x44 * k + 4 * i, &v, 4);
  9670. };
  9671. auto settings = [] (std::vector<std::uint8_t>& s, std::int32_t modX, std::int32_t modY, std::int32_t type) {
  9672. std::memcpy (s.data() + 0x15c, &modX, 4);
  9673. std::memcpy (s.data() + 0x160, &modY, 4);
  9674. std::memcpy (s.data() + 0x168, &type, 4);
  9675. };
  9676. std::vector<std::uint8_t> snap (364, 0);
  9677. record (snap, 0, { { 9, 5000 }, { 10, 9000 }, { 11, -40 }, { 12, 15000 }, { 13, 1 } });
  9678. 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 } });
  9679. record (snap, 2, { { 1, 1 }, { 3, 6000 }, { 4, 100 }, { 5, 16000 }, { 6, 20 }, { 8, -60 }, { 11, 20 }, { 12, 18000 }, { 13, 2 } });
  9680. record (snap, 3, { { 11, 40 }, { 12, 17000 } });
  9681. record (snap, 4, { { 1, 1 }, { 3, 5000 }, { 4, 100 }, { 5, 14000 }, { 6, 30 }, { 8, -700 } });
  9682. settings (snap, 140, 120, 7);
  9683. const auto withSnap = lynx::dx10StateWithSnapshot (blob, snap);
  9684. CHECK (withSnap.size() == 460 && withSnap[0] == 2 && ! lynx::dx10ProcessorNeutral (withSnap));
  9685. CHECK (lynx::dx10StateWithSnapshot (withSnap, std::vector<std::uint8_t> (364, 0)) == withSnap); // a v2 state keeps its own
  9686. lynx::dx10::RefOptions ticks;
  9687. ticks.tick = 230, ticks.ppq = 96, ticks.tempo = 120.0;
  9688. check ({ withSnap, ticks, 44100, 346.852062, 554.699375,
  9689. { { 100, -0.00162654708f, -0.00265428401f }, { 1000, 0.0310285408f, 0.0506339073f }, { 2500, -0.118068494f, -0.0915558636f },
  9690. { 5000, -0.297166437f, -0.256465197f }, { 9000, 0.199509233f, 0.166108996f }, { 16383, -0.00392446062f, 0.0410436988f } } });
  9691. // a volume envelope decaying to zero: the 10 ms end fade, then every voice is gone (silence from 14950 on)
  9692. std::vector<std::uint8_t> decay (364, 0);
  9693. for (int k = 0; k < 5; ++k)
  9694. record (decay, k, {});
  9695. record (decay, 1, { { 0, 4 }, { 1, 1 }, { 3, 9000 }, { 4, 100 }, { 5, 12000 }, { 6, 0 }, { 15, 50 }, { 16, -101 } });
  9696. settings (decay, 256, 0, 0);
  9697. auto ticks48 = ticks;
  9698. ticks48.block = 333;
  9699. const auto y = check ({ lynx::dx10StateWithSnapshot (blob, decay), ticks48, 48000, 338.361707, 350.520208,
  9700. { { 100, 0.000514116196f, 0.000838961045f }, { 1000, 0.0412623473f, 0.0673340634f }, { 2500, -0.141532451f, -0.156581759f },
  9701. { 5000, 0.382550895f, 0.23462519f }, { 7000, -0.189829409f, 0.144132584f }, { 9000, -0.13638559f, 0.142912149f } } });
  9702. bool silentTail = y.size() == 2 * 16384 && (y[2 * 14949] != 0.0f || y[2 * 14949 + 1] != 0.0f);
  9703. for (std::size_t i = 2 * 14950; i < y.size(); ++i)
  9704. silentTail = silentTail && y[i] == 0.0f;
  9705. CHECK (silentTail);
  9706. // the instrument: LYNX velocity v → Init.Vol = (11^(1.28·v) − 1)/10 and Final.Vol = channel volume × Init.Vol; channel
  9707. // gains √2·V·(cos, sin)((1 + p)π/4) → V, p; notes on block boundaries play like the reference renderer (neutral
  9708. // channel settings: the instrument's sequencer ticks change nothing; vibrato off, since the plugin-wide vibrato
  9709. // oscillator is stepped by each voice in turn, so its interleaving follows the host block layout, as in FL)
  9710. auto noVib = blob;
  9711. CHECK (lynx::dx10SetParam (noVib, 10, 0));
  9712. auto initVol = [] (double v) { return (double) (float) ((std::pow (11.0, 1.28 * v) - 1.0) / 10.0); }; // Init.Vol is a float
  9713. lynx::ChannelDef c;
  9714. c.instrument = lynx::InstrumentKind::dx10;
  9715. c.pluginState = noVib;
  9716. auto inst = lynx::makeInstrument (lynx::InstrumentKind::dx10);
  9717. inst->configure (c, 120.0);
  9718. inst->prepare (44100.0);
  9719. const float V = 0.6f, pan = 0.25f;
  9720. const auto gl = (float) (std::sqrt (2.0) * V * std::cos ((1.0 + pan) * 3.141592653589793 / 4.0));
  9721. const auto gr = (float) (std::sqrt (2.0) * V * std::sin ((1.0 + pan) * 3.141592653589793 / 4.0));
  9722. std::vector<float> l (16384), r (16384);
  9723. for (int s = 0; s < 16384; s += 512)
  9724. {
  9725. if (s == 0)
  9726. inst->noteOn (60, 0.78125f);
  9727. if (s == 2048)
  9728. {
  9729. inst->setNextNoteExpression (-0.5f, 25.0f);
  9730. inst->noteOn (67, 0.5f);
  9731. }
  9732. if (s == 6144)
  9733. inst->noteOff (60);
  9734. if (s == 8192)
  9735. inst->noteOff (67);
  9736. inst->render (l.data() + s, r.data() + s, 512, gl, gr);
  9737. }
  9738. // the channel level reaches the engine as FL's (recovered to float precision)
  9739. const auto Vr = std::sqrt (((double) gl * gl + (double) gr * gr) * 0.5);
  9740. const auto pr = std::atan2 ((double) gr, (double) gl) * (4.0 / 3.141592653589793) - 1.0;
  9741. const auto ref = lynx::dx10::renderReference (noVib, { { 0, 60, initVol (0.78125), Vr * initVol (0.78125), pr, 0.0, 6144 },
  9742. { 2048, 67, initVol (0.5), Vr * initVol (0.5), std::clamp (-0.5 + pr, -1.0, 1.0), 25.0, 8192 } },
  9743. 44100.0, 16384);
  9744. double worst = 0.0, peak = 0.0;
  9745. for (int i = 0; i < 16384; ++i)
  9746. 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]) }),
  9747. peak = std::max (peak, (double) std::abs (l[(std::size_t) i]));
  9748. CHECK (worst == 0.0 && peak > 0.05);
  9749. // the released voices end (DX10's own release) and the instrument goes idle
  9750. std::vector<float> tail (44100), tailR (tail.size());
  9751. for (int k = 0; k < 20 && ! inst->idle(); ++k)
  9752. inst->render (tail.data(), tailR.data(), (int) tail.size(), gl, gr);
  9753. CHECK (inst->idle());
  9754. // with the channel settings above the instrument follows FL's ticks: the cutoff envelope darkens the note
  9755. {
  9756. lynx::ChannelDef cs = c;
  9757. cs.pluginState = withSnap;
  9758. cs.modelParams = { 96.0f };
  9759. auto si = lynx::makeInstrument (lynx::InstrumentKind::dx10);
  9760. si->configure (cs, 120.0);
  9761. si->prepare (44100.0);
  9762. std::vector<float> sl (32768), sr (32768);
  9763. si->noteOn (48, 0.78125f);
  9764. for (int s = 0; s < 32768; s += 512)
  9765. si->render (sl.data() + s, sr.data() + s, 512, 1.0f, 1.0f);
  9766. double e1 = 0.0, e2 = 0.0;
  9767. bool finite = true;
  9768. for (int i = 0; i < 32768; ++i)
  9769. finite = finite && std::isfinite (sl[(std::size_t) i]), (i < 4096 ? e1 : e2) += (double) sl[(std::size_t) i] * sl[(std::size_t) i];
  9770. CHECK (finite && e1 > 1e-3 && e2 > 1e-3);
  9771. }
  9772. // parameter edits (the channel settings panel) write the raw integers, clamped to the knob ranges
  9773. auto edited = blob;
  9774. CHECK (lynx::dx10SetParam (edited, 21, 7) && lynx::dx10Param (edited, 21) == 3);
  9775. CHECK (lynx::dx10SetParam (edited, 12, 70000) && lynx::dx10Param (edited, 12) == 65536);
  9776. }
  9777. TEST (threeosc_engine)
  9778. {
  9779. // REA_rodada47: a 3x Osc state written from scratch (version 14) against our reference model (osc3model.py,
  9780. // bit-identical to the installation's DLL), the plugin voice processor with its release path, the instrument
  9781. std::int32_t P[21] = { -20, 3, 0, 7, 10, -15, 64, 30, 2, -12, 0, 0, 20, 40, 0, 4, 12, -30, -8, 0, 16 };
  9782. auto make = [] (const std::int32_t* p, std::array<std::uint8_t, 3> inv, std::uint8_t am, const std::vector<std::uint8_t>& snap) {
  9783. std::vector<std::uint8_t> st (0x60, 0);
  9784. const std::int32_t version = 14;
  9785. std::memcpy (st.data(), &version, 4);
  9786. std::memcpy (st.data() + 4, p, 21 * 4);
  9787. st[0x58] = inv[0], st[0x59] = inv[1], st[0x5a] = inv[2], st[0x5b] = am;
  9788. st.insert (st.end(), snap.begin(), snap.end());
  9789. return st;
  9790. };
  9791. auto record = [] (std::vector<std::uint8_t>& s, int k, std::initializer_list<std::pair<int, int>> fields) {
  9792. const std::int32_t neutral[17] = { 0, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, 0 };
  9793. std::memcpy (s.data() + 0x44 * k, neutral, sizeof (neutral));
  9794. for (const auto& [i, v] : fields)
  9795. std::memcpy (s.data() + 0x44 * k + 4 * i, &v, 4);
  9796. };
  9797. auto settings = [] (std::vector<std::uint8_t>& s, std::int32_t modX, std::int32_t modY, std::int32_t type) {
  9798. std::memcpy (s.data() + 0x15c, &modX, 4);
  9799. std::memcpy (s.data() + 0x160, &modY, 4);
  9800. std::memcpy (s.data() + 0x168, &type, 4);
  9801. };
  9802. std::vector<std::uint8_t> neutral (364, 0);
  9803. for (int k = 0; k < 5; ++k)
  9804. record (neutral, k, k == 1 ? std::initializer_list<std::pair<int, int>> { { 0, 4 }, { 16, -101 } } : std::initializer_list<std::pair<int, int>> {});
  9805. settings (neutral, 256, 0, 0);
  9806. const auto blob = make (P, { 0, 0, 0 }, 0, neutral);
  9807. CHECK (lynx::threeOscStateUsable (blob) && lynx::threeOscNormalisedState (blob, {}) == blob);
  9808. CHECK (lynx::threeOscParam (blob, 9) == -12 && lynx::threeOscParam (blob, 20) == 16 && lynx::threeOscParam (blob, 24) == 0 && ! lynx::threeOscParam (blob, 26));
  9809. // older versions are normalised like the plugin reads them: version 12 (no snapshot → the plugin's defaults),
  9810. // version 10 (20 parameters, no invert/AM bytes)
  9811. {
  9812. std::vector<std::uint8_t> v12 (blob.begin(), blob.begin() + 92);
  9813. v12[0] = 12;
  9814. v12[0x58] = 1;
  9815. const auto n12 = lynx::threeOscNormalisedState (v12, {});
  9816. CHECK (n12.size() == 460 && n12[0] == 14 && lynx::threeOscParam (n12, 21) == 1 && lynx::threeOscParam (n12, 20) == 16
  9817. && std::equal (n12.begin() + 0x60, n12.end(), neutral.begin()));
  9818. std::vector<std::uint8_t> v10 (blob.begin(), blob.begin() + 84);
  9819. v10[0] = 10;
  9820. const auto n10 = lynx::threeOscNormalisedState (v10, {});
  9821. CHECK (lynx::threeOscParam (n10, 20) == 0 && lynx::threeOscParam (n10, 13) == 40 && lynx::threeOscParam (n10, 21) == 0);
  9822. }
  9823. using N = lynx::threeosc::RefNote;
  9824. const std::vector<N> notes { { 0, 60, 0.8, 0.3, 0.0, 6144 }, { 2048, 67, 1.0, -0.5, 25.0, 8192 }, { 3000, 40, 0.6, 1.0, 0.0, -1 } };
  9825. struct Golden { std::vector<std::uint8_t> state; lynx::threeosc::RefOptions opt; int rate; double el, er; std::tuple<int, float, float> at[6]; };
  9826. auto check = [&] (const Golden& g) {
  9827. const auto y = lynx::threeosc::renderReference (g.state, notes, (double) g.rate, 16384, g.opt);
  9828. CHECK (y.size() == 2 * 16384);
  9829. if (y.size() != 2 * 16384)
  9830. return y;
  9831. double el = 0.0, er = 0.0;
  9832. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  9833. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  9834. CHECK (std::abs (el - g.el) <= 1e-7 * g.el && std::abs (er - g.er) <= 1e-7 * g.er);
  9835. for (const auto& [i, l, r] : g.at)
  9836. CHECK (std::abs (y[2 * (std::size_t) i] - l) <= 1e-7f * std::max (1e-16f, std::abs (l))
  9837. && std::abs (y[2 * (std::size_t) i + 1] - r) <= 1e-7f * std::max (1e-16f, std::abs (r)));
  9838. return y;
  9839. };
  9840. // saw / square / triangle-ish mix with pans, stereo detune and phase, random start phases (seed 12345)
  9841. lynx::threeosc::RefOptions plain;
  9842. plain.seed = 12345;
  9843. check ({ blob, plain, 44100, 681.6493225566775, 1509.7243534586487,
  9844. { { 100, 0.0674247071146965f, -0.13440212607383728f }, { 1000, 0.12890009582042694f, 0.446039617061615f },
  9845. { 2500, -0.007686644792556763f, 0.3574123680591583f }, { 5000, 0.08792756497859955f, 0.4948147237300873f },
  9846. { 9000, 1.2136793475613885e-17f, 0.3116455078125f }, { 16383, 5.7305530145831346e-18f, 0.4134995639324188f } } });
  9847. // AM (osc 3 noise modulating osc 1 + 2), inverted oscillators, 48 kHz, odd blocks, max polyphony 1
  9848. const std::int32_t Pam[21] = { 0, 2, 0, 0, 0, 0, 30, 0, 3, 7, 0, 0, 0, 77, 0, 5, -12, 0, 0, 0, 0 };
  9849. lynx::threeosc::RefOptions odd;
  9850. odd.block = 333, odd.maxPoly = 1, odd.seed = 777;
  9851. check ({ make (Pam, { 1, 0, 1 }, 1, neutral), odd, 48000, 453.2728551610344, 1996.4554357771258,
  9852. { { 100, -0.2589033246040344f, -0.310184121131897f }, { 1000, 0.22021546959877014f, 0.3591070771217346f },
  9853. { 2500, -0.5378497242927551f, -0.15602676570415497f }, { 5000, 1.2423200626807308e-17f, 0.3293072581291199f },
  9854. { 9000, 2.5857780099332177e-17f, 0.42474493384361267f }, { 16383, 2.941168384618868e-17f, 0.3569665551185608f } } });
  9855. // the channel processor with its release path: pan LFO, volume envelope released from its current value, cutoff
  9856. // envelope + square LFO, resonance LFO, pitch envelope, SVF LP ×2 (type 7)
  9857. std::vector<std::uint8_t> snap (364, 0);
  9858. record (snap, 0, { { 9, 5000 }, { 10, 9000 }, { 11, -40 }, { 12, 15000 }, { 13, 1 } });
  9859. record (snap, 1, { { 0, 4 }, { 1, 1 }, { 3, 12000 }, { 4, 3000 }, { 5, 15000 }, { 6, 40 }, { 7, 20000 }, { 11, 30 }, { 12, 20000 }, { 14, 60 }, { 15, -90 }, { 16, 50 } });
  9860. record (snap, 2, { { 1, 1 }, { 3, 6000 }, { 4, 100 }, { 5, 16000 }, { 6, 20 }, { 7, 9000 }, { 8, -60 }, { 11, 20 }, { 12, 18000 }, { 13, 2 } });
  9861. record (snap, 3, { { 11, 40 }, { 12, 17000 } });
  9862. record (snap, 4, { { 1, 1 }, { 3, 5000 }, { 4, 100 }, { 5, 14000 }, { 6, 30 }, { 7, 12000 }, { 8, -700 } });
  9863. settings (snap, 140, 120, 7);
  9864. lynx::threeosc::RefOptions ticks;
  9865. ticks.tick = 230, ticks.ppq = 96, ticks.tempo = 120.0, ticks.seed = 99;
  9866. check ({ make (P, { 0, 0, 0 }, 0, snap), ticks, 44100, 977.9928196811321, 2072.965349921462,
  9867. { { 100, -0.001351095037534833f, 0.0029758031014353037f }, { 1000, 0.002833257894963026f, 0.10575854033231735f },
  9868. { 2500, 0.020360738039016724f, 0.3181821405887604f }, { 5000, 0.3340669274330139f, 0.4912331700325012f },
  9869. { 9000, 0.26940545439720154f, 0.27708977460861206f }, { 16383, 0.16570518910884857f, -0.10794822871685028f } } });
  9870. auto ticks48 = ticks;
  9871. ticks48.block = 333, ticks48.tick = 250, ticks48.seed = 5;
  9872. // (at 48 kHz the library's rate ratio 44100/fs scales the filter coefficients and the ramp limit)
  9873. check ({ make (P, { 0, 1, 0 }, 0, snap), ticks48, 48000, 864.9859578029492, 1895.6281818565287,
  9874. { { 100, 0.0031638722866773605f, -0.001987025374546647f }, { 1000, 0.049239177256822586f, 0.08794696629047394f },
  9875. { 2500, 0.13807585835456848f, 0.3554498553276062f }, { 5000, -0.08793184161186218f, -0.1596519947052002f },
  9876. { 9000, 0.08728396147489548f, 0.540290117263794f }, { 16383, 0.039887525141239166f, 0.21270982921123505f } } });
  9877. // HQ oscillators (engine_x64.dll: band-limited tables built with an FFT; FL's single-precision FFT and LYNX's double
  9878. // one agree to float rounding, so the golden values hold to 1e-5)
  9879. {
  9880. const std::int32_t Phq[21] = { 0, 3, 0, 0, 0, 0, 64, 20, 2, -12, 0, 0, 0, 40, -20, 4, 7, 0, 0, 0, 0 };
  9881. auto hq = make (Phq, { 0, 0, 0 }, 0, neutral);
  9882. hq[0x5c] = 1;
  9883. CHECK (lynx::threeOscParam (hq, 25) == 1);
  9884. lynx::threeosc::RefOptions o;
  9885. o.seed = 3;
  9886. const auto y = lynx::threeosc::renderReference (hq, notes, 44100.0, 16384, o);
  9887. CHECK (y.size() == 2 * 16384);
  9888. if (y.size() == 2 * 16384)
  9889. {
  9890. double el = 0.0, er = 0.0;
  9891. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  9892. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  9893. CHECK (std::abs (el - 862.7912087773848) < 1e-5 * 862.8 && std::abs (er - 1232.8046929286115) < 1e-5 * 1232.8);
  9894. const std::tuple<int, float, float> at[] = { { 100, -0.019788391888141632f, -0.19209949672222137f }, { 1000, 0.12019827216863632f, 0.03852222114801407f },
  9895. { 2500, 0.3239150643348694f, 0.07325324416160583f }, { 5000, -0.2809634208679199f, -0.5693862438201904f },
  9896. { 16383, -1.1937656360586644e-17f, -0.061853960156440735f } };
  9897. for (const auto& [i, l, r] : at)
  9898. CHECK (std::abs (y[2 * (std::size_t) i] - l) < 1e-5f && std::abs (y[2 * (std::size_t) i + 1] - r) < 1e-5f);
  9899. }
  9900. }
  9901. // the instrument: Final.Vol = channel volume × Init.Vol, channel gains → FL's volume/pan; notes on block boundaries
  9902. // play like the reference renderer (seed 0, neutral channel settings: ticks change nothing)
  9903. auto initVol = [] (double v) { return (double) (float) ((std::pow (11.0, 1.28 * v) - 1.0) / 10.0); };
  9904. lynx::ChannelDef c;
  9905. c.instrument = lynx::InstrumentKind::threeosc;
  9906. c.pluginState = blob;
  9907. auto inst = lynx::makeInstrument (lynx::InstrumentKind::threeosc);
  9908. inst->configure (c, 120.0);
  9909. inst->prepare (44100.0);
  9910. const float V = 0.6f, pan = 0.25f;
  9911. const auto gl = (float) (std::sqrt (2.0) * V * std::cos ((1.0 + pan) * 3.141592653589793 / 4.0));
  9912. const auto gr = (float) (std::sqrt (2.0) * V * std::sin ((1.0 + pan) * 3.141592653589793 / 4.0));
  9913. std::vector<float> l (16384), r (16384);
  9914. for (int s = 0; s < 16384; s += 512)
  9915. {
  9916. if (s == 0)
  9917. inst->noteOn (60, 0.78125f);
  9918. if (s == 2048)
  9919. {
  9920. inst->setNextNoteExpression (-0.5f, 25.0f);
  9921. inst->noteOn (67, 0.5f);
  9922. }
  9923. if (s == 6144)
  9924. inst->noteOff (60);
  9925. if (s == 8192)
  9926. inst->noteOff (67);
  9927. inst->render (l.data() + s, r.data() + s, 512, gl, gr);
  9928. }
  9929. const auto Vr = std::sqrt (((double) gl * gl + (double) gr * gr) * 0.5);
  9930. const auto pr = std::atan2 ((double) gr, (double) gl) * (4.0 / 3.141592653589793) - 1.0;
  9931. const auto ref = lynx::threeosc::renderReference (blob, { { 0, 60, Vr * initVol (0.78125), pr, 0.0, 6144 },
  9932. { 2048, 67, Vr * initVol (0.5), std::clamp (-0.5 + pr, -1.0, 1.0), 25.0, 8192 } },
  9933. 44100.0, 16384);
  9934. double worst = 0.0, peak = 0.0;
  9935. for (int i = 0; i < 16384; ++i)
  9936. 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]) }),
  9937. peak = std::max (peak, (double) std::abs (l[(std::size_t) i]));
  9938. CHECK (worst == 0.0 && peak > 0.05);
  9939. // released notes end with the processor's 10 ms fade (volume envelope off) and the instrument goes idle
  9940. CHECK (inst->idle());
  9941. // parameter edits write the raw integers, clamped to the knob ranges
  9942. auto edited = blob;
  9943. CHECK (lynx::threeOscSetParam (edited, 9, -40) && lynx::threeOscParam (edited, 9) == -24);
  9944. CHECK (lynx::threeOscSetParam (edited, 13, 500) && lynx::threeOscParam (edited, 13) == 128);
  9945. CHECK (lynx::threeOscSetParam (edited, 24, 1) && edited[0x5b] == 1);
  9946. CHECK (! lynx::threeOscStateUsable (std::vector<std::uint8_t> (2, 0)));
  9947. }
  9948. TEST (gms_engine)
  9949. {
  9950. const auto blob = gmsTestState();
  9951. dumpIfAsked ("LYNX_DUMP_GMS", blob);
  9952. CHECK (lynx::gmsStateUsable (blob));
  9953. CHECK (lynx::gmsWaveNames (blob) == std::vector<std::string> ({ "NoWave A", "NoWave B", "NoWave C" }));
  9954. CHECK (lynx::gmsParamOffset (blob, 12) == 8 + 4 + 7 * 512 + 12 + 4 * 12 && lynx::gmsParamOffset (blob, 212) < 0);
  9955. auto broken = blob;
  9956. broken[8] = 'X';
  9957. CHECK (! lynx::gmsStateUsable (broken));
  9958. CHECK (! lynx::gmsStateUsable (std::vector<std::uint8_t> (blob.begin(), blob.begin() + 2000)));
  9959. // golden samples of our round-39 model (gmsmodel.GmsPlugin, waves missing → silent tables, tempo 140, 44.1 kHz,
  9960. // host blocks of 512): C5 vel 100 0–30000, E5 vel 80 11025–40000, G5 vel 110 22050–52000 (Init.Vol law A)
  9961. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  9962. const std::vector<lynx::gms::RefNote> notes = { { 0, 60, (float) iv (100), 1.0, 0.0, 0.0, 30000 },
  9963. { 11025, 64, (float) iv (80), 1.0, 0.0, 0.0, 40000 },
  9964. { 22050, 67, (float) iv (110), 1.0, 0.0, 0.0, 52000 } };
  9965. const auto y = lynx::gms::renderReference (blob, notes, 44100.0, 66150, "");
  9966. CHECK (y.size() == 2 * 66150u);
  9967. 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 } };
  9968. for (const auto& g : golden)
  9969. 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);
  9970. double e = 0.0;
  9971. for (auto v : y)
  9972. e += (double) v * v;
  9973. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.0790270452795) < 1e-6);
  9974. // the instrument: same engine behind LYNX's channel gains (V = 1, centre → law A), velocity → Init.Vol
  9975. lynx::ChannelDef c;
  9976. c.instrument = lynx::InstrumentKind::gms;
  9977. c.pluginState = blob;
  9978. auto inst = lynx::makeInstrument (lynx::InstrumentKind::gms);
  9979. inst->configure (c, 140.0);
  9980. inst->prepare (44100.0);
  9981. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  9982. for (int b = 0; b < 16; ++b)
  9983. {
  9984. if (b == 0)
  9985. inst->noteOn (60, 0.78125f);
  9986. if (b == 10)
  9987. inst->noteOff (60);
  9988. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  9989. }
  9990. 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, "");
  9991. double diff = 0.0, energy = 0.0;
  9992. for (int i = 0; i < 8192; ++i)
  9993. {
  9994. 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]) });
  9995. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  9996. }
  9997. CHECK (diff == 0.0 && energy > 1e-3);
  9998. }
  9999. // A Toxic Biohazard state written from scratch (REA_rodada45; r45/py/mktest.py builds the same bytes): our own drawn
  10000. // spectra on operators 1 and 3 (wave 0) and the sine on operator 2, so no wave of the installation is needed
  10001. std::vector<std::uint8_t> toxicTestState()
  10002. {
  10003. auto s = lynx::toxicDefaultState();
  10004. static const std::pair<int, double> set[] = {
  10005. { 0, 0.62 }, { 1, 0.3 }, { 3, 0.05 }, { 4, 0.6 }, { 5, 0.7 }, { 6, 0.4 }, { 7, 0.3 }, { 8, 0.4 }, { 9, 0.8 }, { 10, 0.2 },
  10006. { 13, 0.45 }, { 15, 0.45 }, { 16, 0.55 }, { 17, 0.5 }, { 19, 0.1 }, { 20, 0.5 }, { 21, 0.4 }, { 22, 0.4 }, { 23, 0.8 },
  10007. { 24, 0.6 }, { 25, 1 }, { 26, 1 }, { 27, 1 }, { 32, 0.5606 }, { 33, 0.4394 }, { 38, 0.53 }, { 49, 0 }, { 55, 0.25 },
  10008. { 62, 0.5 }, { 75, 0.08 }, { 81, 0.5 }, { 87, 0.3 }, { 104, 0.8 }, { 105, 0.6 }, { 110, 0.35 }, { 115, 0.22 },
  10009. { 127, 0.18 }, { 147, 0.7 }, { 227, 1 }, { 229, 0.65 }, { 233, 0.4 }, { 236, 1 }, { 237, 1 }, { 252, 0.5 }, { 254, 0.3 },
  10010. { 256, 0.4 }, { 267, 0.4 }, { 269, 0.5 }, { 277, 0.3 }, { 300, 1 }, { 301, 8 }, { 302, 2 }, { 303, 0 }, { 305, 3 },
  10011. { 306, 0 }, { 307, 1 }, { 308, 0 }, { 314, 1 }, { 315, 0 }, { 316, 1 }, { 324, 106 }, { 332, 0.7 } };
  10012. for (const auto& [index, value] : set)
  10013. {
  10014. const auto v = (float) value;
  10015. std::memcpy (s.data() + lynx::toxicParamOffset (s, index), &v, 4);
  10016. }
  10017. auto spectrum = [&] (int slot, int h, float amp, float phase) {
  10018. const auto at = 12 + 0x40 + 1024 * (std::size_t) slot + 4 * (std::size_t) h;
  10019. std::memcpy (s.data() + at, &amp, 4);
  10020. std::memcpy (s.data() + at + 512, &phase, 4);
  10021. };
  10022. for (int h = 0; h < 24; ++h)
  10023. spectrum (0, h, (float) (1.0 / (h + 1)), 0.0f);
  10024. for (int k = 0; k < 16; ++k)
  10025. spectrum (2, 2 * k, (float) (1.0 / (2 * k + 1)), 0.25f);
  10026. return s;
  10027. }
  10028. TEST (toxic_engine)
  10029. {
  10030. const auto blob = toxicTestState();
  10031. dumpIfAsked ("LYNX_DUMP_TOXIC", blob);
  10032. CHECK (blob.size() == 10050u && lynx::toxicStateUsable (blob) && ! lynx::toxicNeedsDll (blob));
  10033. CHECK (lynx::toxicParamOffset (blob, 15) == 12 + 0x40 + 8192 + 4 * 15 && lynx::toxicParamOffset (blob, 340) < 0);
  10034. CHECK (! lynx::toxicDllUsable ("") && ! lynx::toxicDllUsable ("/nonexistent/Toxic Biohazard_x64.dll"));
  10035. auto broken = blob;
  10036. broken[8] = 'X';
  10037. CHECK (! lynx::toxicStateUsable (broken));
  10038. CHECK (! lynx::toxicStateUsable (std::vector<std::uint8_t> (blob.begin(), blob.begin() + 5000)));
  10039. auto needs = blob;
  10040. const auto w = 15.0f; // a built-in wave of the installation: without the DLL the engine refuses the state
  10041. std::memcpy (needs.data() + lynx::toxicParamOffset (needs, 307), &w, 4);
  10042. CHECK (lynx::toxicNeedsDll (needs) && lynx::toxic::renderReference (needs, { { 0, 60 } }, 44100.0, 64, "").empty());
  10043. // golden samples of our round-45 model (toxmodel.run, which equals the DLL bit for bit on this state), effects on,
  10044. // tempo 140, 44.1 kHz, host blocks of 512: C5 vel 100 0–30000, E5 vel 80 11025–40000, G5 vel 110 22050–52000
  10045. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  10046. const std::vector<lynx::toxic::RefNote> notes = { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 },
  10047. { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  10048. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 } };
  10049. const auto y = lynx::toxic::renderReference (blob, notes, 44100.0, 66150, "", 512, 140.0);
  10050. CHECK (y.size() == 2 * 66150u);
  10051. static const struct { int i; float l, r; } golden[] = { { 100, 0.264782101f, 0.189374551f }, { 5000, -0.605827034f, 0.43244195f }, { 15000, -0.343959779f, -0.111227378f }, { 25000, 0.0127612427f, -0.354315311f }, { 35000, -0.353289753f, -0.307500422f }, { 45000, -0.260735184f, 0.202133358f }, { 55000, -0.0751226544f, -0.0776180848f }, { 66000, -0.0294751395f, 0.00692501338f } };
  10052. for (const auto& g : golden)
  10053. 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);
  10054. double e = 0.0;
  10055. for (auto v : y)
  10056. e += (double) v * v;
  10057. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.307227390984) < 1e-6);
  10058. // the instrument: same engine behind LYNX's channel gains (V = 1, centre → law A), velocity → Init.Vol
  10059. lynx::ChannelDef c;
  10060. c.instrument = lynx::InstrumentKind::toxic;
  10061. c.pluginState = blob;
  10062. auto inst = lynx::makeInstrument (lynx::InstrumentKind::toxic);
  10063. inst->configure (c, 140.0);
  10064. inst->prepare (44100.0);
  10065. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  10066. for (int b = 0; b < 16; ++b)
  10067. {
  10068. if (b == 0)
  10069. inst->noteOn (60, 0.78125f);
  10070. if (b == 10)
  10071. inst->noteOff (60);
  10072. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  10073. }
  10074. const auto ref = lynx::toxic::renderReference (blob, { { 0, 60, (std::pow (11.0, 1.28 * 0.78125) - 1.0) / 10.0, 1.0, 0.0, 0.0, 5120 } },
  10075. 44100.0, 8192, "", 512, 140.0);
  10076. double diff = 0.0, energy = 0.0;
  10077. for (int i = 0; i < 8192; ++i)
  10078. {
  10079. 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]) });
  10080. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  10081. }
  10082. CHECK (diff == 0.0 && energy > 1e-3);
  10083. }
  10084. TEST (morphine_engine)
  10085. {
  10086. const auto blob = morphineTestState();
  10087. dumpIfAsked ("LYNX_DUMP_MORPHINE", blob);
  10088. CHECK (lynx::morphineStateUsable (blob));
  10089. CHECK (lynx::morphineNoiseName (blob).empty());
  10090. const auto at = lynx::morphineParamOffset (blob, 9);
  10091. CHECK (at > 0);
  10092. float a9 = 0.0f;
  10093. std::memcpy (&a9, blob.data() + at, 4);
  10094. CHECK (a9 == 0.2f && lynx::morphineParamOffset (blob, 40) < 0);
  10095. auto broken = blob;
  10096. broken[12] = 'X'; // the MPR0 tag
  10097. CHECK (! lynx::morphineStateUsable (broken));
  10098. CHECK (! lynx::morphineStateUsable (std::vector<std::uint8_t> (blob.begin(), blob.end() - 10)));
  10099. // golden samples of our round-39 model (mormodel.Synth, tempo 120, 44.1 kHz, host blocks of 512): keys 60/64/67,
  10100. // Init.Vol 1.0/0.5/2.0, note-offs at 30000/50000/70000
  10101. const std::vector<lynx::morphine::RefNote> notes = { { 0, 60, 1.0, 1.0, 0.0, 0.0, 30000 },
  10102. { 11025, 64, 0.5, 1.0, 0.0, 0.0, 50000 },
  10103. { 22050, 67, 2.0, 1.0, 0.0, 0.0, 70000 } };
  10104. const auto y = lynx::morphine::renderReference (blob, notes, 44100.0, 80000, "");
  10105. CHECK (y.size() == 2 * 80000u);
  10106. 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 } };
  10107. for (const auto& g : golden)
  10108. 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);
  10109. double e = 0.0;
  10110. for (auto v : y)
  10111. e += (double) v * v;
  10112. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.18467099041) < 1e-6);
  10113. // the instrument: same engine behind LYNX's channel gains
  10114. lynx::ChannelDef c;
  10115. c.instrument = lynx::InstrumentKind::morphine;
  10116. c.pluginState = blob;
  10117. auto inst = lynx::makeInstrument (lynx::InstrumentKind::morphine);
  10118. inst->configure (c, 120.0);
  10119. inst->prepare (44100.0);
  10120. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  10121. for (int b = 0; b < 16; ++b)
  10122. {
  10123. if (b == 0)
  10124. inst->noteOn (60, 0.78125f);
  10125. if (b == 10)
  10126. inst->noteOff (60);
  10127. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  10128. }
  10129. 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, "");
  10130. double diff = 0.0, energy = 0.0;
  10131. for (int i = 0; i < 8192; ++i)
  10132. {
  10133. 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]) });
  10134. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  10135. }
  10136. CHECK (diff == 0.0 && energy > 1e-3);
  10137. }
  10138. namespace
  10139. {
  10140. // A MiniSynth state written from scratch (REA_rodada41 §1.1 layout: "SSLF" + the .flms preset)
  10141. std::vector<std::uint8_t> miniSynthTestState (const std::array<float, 43>& P)
  10142. {
  10143. const std::string name = "LYNX test";
  10144. Bytes body;
  10145. body.raw ("\x99" "FLS\r\n\x1a\n", 8);
  10146. body.raw ("HEAD", 4);
  10147. body.i32 (6 + 2 * (std::int32_t) name.size());
  10148. const std::uint16_t head[3] = { 1, 1, (std::uint16_t) name.size() };
  10149. body.raw (reinterpret_cast<const char*> (head), 6);
  10150. for (char ch : name)
  10151. body.raw (&ch, 1), body.zeros (1);
  10152. body.raw ("DATA", 4);
  10153. body.i32 (176);
  10154. body.raw ("0rev", 4);
  10155. for (auto v : P)
  10156. body.f32 (v);
  10157. Bytes s;
  10158. s.raw ("SSLF", 4);
  10159. s.i32 ((std::int32_t) body.b.size());
  10160. s.bytes (body.b);
  10161. return s.b;
  10162. }
  10163. std::array<float, 43> miniSynthDefaults()
  10164. {
  10165. 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,
  10166. 0.5f, 0, 0.25f, 0.5f, 0.5f, 0, 0.11222222f, 0.4f, 0.1f, 1, 0.5f };
  10167. }
  10168. // a synthetic oscdata.dat (amplitudes 0.3/k, phases −π/2 + 0.001·(k − 1)): the test does not need an FL installation
  10169. std::string miniSynthTestOscdata()
  10170. {
  10171. const auto path = (std::filesystem::temp_directory_path() / "lynx_test_oscdata.dat").string();
  10172. std::vector<float> d (2048);
  10173. for (int h = 0; h < 1024; ++h)
  10174. d[(std::size_t) h] = (float) (0.3 / (h + 1)), d[1024 + (std::size_t) h] = (float) (-1.5707963267948966 + 0.001 * h);
  10175. std::ofstream out (path, std::ios::binary);
  10176. out.write (reinterpret_cast<const char*> (d.data()), 8192);
  10177. return path;
  10178. }
  10179. } // namespace
  10180. TEST (minisynth_engine)
  10181. {
  10182. auto A = miniSynthDefaults(), B = miniSynthDefaults();
  10183. // A: poly, SQR+SQR, noise, filter env/velocity/keytracking, LFO → cutoff, distortion + decimator, delay, chorus, phaser
  10184. 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 },
  10185. { 12, 0.1f }, { 13, 0.4f }, { 14, 0.3f }, { 15, 0.5f }, { 16, 0.8f }, { 17, 0.6f }, { 18, 0.1f },
  10186. { 19, 0.5f }, { 20, 0.85f }, { 21, 0.05f }, { 22, 0.5f }, { 23, 0.6f }, { 24, 0.4f }, { 25, 1 },
  10187. { 26, 0.3f }, { 27, 0.1f }, { 28, 1 }, { 29, 0.4f }, { 30, 0.5f }, { 31, 0.6f }, { 32, 0.6f },
  10188. { 33, 1 }, { 34, 0.4f }, { 35, 0.6f }, { 36, 0.5f }, { 37, 1 }, { 38, 0.3f }, { 39, 0.6f },
  10189. { 40, 0.5f }, { 41, 0.7f }, { 42, 0.4f } };
  10190. // B: lead (legato glide), BELL, LFO → pitch
  10191. 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 },
  10192. { 17, 0.1f }, { 18, 0.5f }, { 19, 0.6f }, { 20, 0.7f }, { 22, 0.6f }, { 23, 0.5f }, { 24, 0.3f } };
  10193. for (const auto& [i, v] : a)
  10194. A[(std::size_t) i] = v;
  10195. for (const auto& [i, v] : b)
  10196. B[(std::size_t) i] = v;
  10197. const auto blobA = miniSynthTestState (A), blobB = miniSynthTestState (B);
  10198. dumpIfAsked ("LYNX_DUMP_MINISYNTH", blobA);
  10199. CHECK (lynx::miniSynthStateUsable (blobA) && lynx::miniSynthParams (blobA).size() == 43u && lynx::miniSynthParams (blobA)[26] == 0.3f);
  10200. CHECK (lynx::miniSynthParamOffset (blobA, 9) == 8 + 8 + 8 + 6 + 18 + 12 + 4 * 9 && lynx::miniSynthParamOffset (blobA, 43) < 0);
  10201. CHECK (! lynx::miniSynthStateUsable (std::vector<std::uint8_t> (blobA.begin(), blobA.begin() + 20)));
  10202. auto shortData = blobA;
  10203. shortData.resize (shortData.size() - 4 * 40); // only 3 parameters present: the rest keeps the defaults
  10204. CHECK (lynx::miniSynthParams (shortData)[20] == 0.5f && lynx::miniSynthParams (shortData)[1] == 0.5f);
  10205. // golden samples of our round-41 model (msmodel.Plugin on the same synthetic oscdata, 44.1 kHz, host blocks of 512,
  10206. // L = R = Final.Vol): C5 vel 100 0–30000, E5 vel 80 11025–40000, G5 vel 110 22050–52000 (Init.Vol law A)
  10207. const auto osc = miniSynthTestOscdata();
  10208. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  10209. const std::vector<lynx::minisynth::RefNote> notes = { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 },
  10210. { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  10211. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 } };
  10212. struct Golden { int i; float l, r; };
  10213. 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 } };
  10214. 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 } };
  10215. for (int which = 0; which < 2; ++which)
  10216. {
  10217. const auto y = lynx::minisynth::renderReference (which == 0 ? blobA : blobB, notes, 44100.0, 66150, osc);
  10218. CHECK (y.size() == 2 * 66150u);
  10219. if (y.size() != 2 * 66150u)
  10220. continue;
  10221. for (const auto& g : which == 0 ? std::vector (std::begin (goldenA), std::end (goldenA)) : std::vector (std::begin (goldenB), std::end (goldenB)))
  10222. CHECK (y[2 * (std::size_t) g.i] == g.l && y[2 * (std::size_t) g.i + 1] == g.r);
  10223. double e = 0.0;
  10224. for (auto v : y)
  10225. e += (double) v * v;
  10226. CHECK (std::abs (std::sqrt (e / (double) y.size()) - (which == 0 ? 0.315552434392 : 0.03926116287)) < 1e-9);
  10227. }
  10228. // missing oscdata.dat: silent tables, still finite
  10229. const auto silent = lynx::minisynth::renderReference (blobB, notes, 44100.0, 4096, "");
  10230. CHECK (silent.size() == 2 * 4096u && std::all_of (silent.begin(), silent.end(), [] (float v) { return std::isfinite (v) && std::abs (v) < 1.0f; }));
  10231. // the instrument: same engine behind LYNX's channel gains (V = 1, centre → law A), velocity → Init.Vol
  10232. lynx::ChannelDef c;
  10233. c.instrument = lynx::InstrumentKind::minisynth;
  10234. c.pluginState = blobA;
  10235. c.samplePath = osc;
  10236. auto inst = lynx::makeInstrument (lynx::InstrumentKind::minisynth);
  10237. inst->configure (c, 120.0);
  10238. inst->prepare (44100.0);
  10239. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  10240. for (int k = 0; k < 16; ++k)
  10241. {
  10242. if (k == 0)
  10243. inst->noteOn (60, 0.78125f);
  10244. if (k == 10)
  10245. inst->noteOff (60);
  10246. inst->render (l.data() + 512 * k, r.data() + 512 * k, 512, 1.0f, 1.0f);
  10247. }
  10248. 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 } },
  10249. 44100.0, 8192, osc, 512, 120.0, 0, -1.0, true);
  10250. double diff = 0.0, energy = 0.0;
  10251. for (int i = 0; i < 8192; ++i)
  10252. {
  10253. 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]) });
  10254. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  10255. }
  10256. CHECK (diff == 0.0 && energy > 1e-3 && ! inst->idle());
  10257. }
  10258. namespace
  10259. {
  10260. // A PoiZone state written from scratch (REA_rodada44 §1.3 layout, "Ver4" + 156 floats + name): pulse A synced to saw B,
  10261. // noise, envelope to the cutoff, triangle LFO to oscillator A's pitch, 3-voice unison, gate, chorus and tape delay.
  10262. std::vector<std::uint8_t> poizoneTestState()
  10263. {
  10264. std::array<float, 156> P {};
  10265. const std::pair<int, float> values[] = { { 0, 1.0f }, { 1, 0.7f }, { 2, 1.0f }, { 5, 0.6f }, { 6, 0.75f }, { 7, 0.2f }, { 8, 0.4f }, { 9, 0.1f },
  10266. { 10, 0.8f }, { 11, 0.3f }, { 12, 0.5f }, { 13, 0.4f }, { 14, 0.5f }, { 15, 0.5f }, { 16, 0.5f }, { 17, 0.5f },
  10267. { 18, 0.35f }, { 19, 0.6f }, { 20, 0.5f }, { 21, 0.3f }, { 22, 0.2f }, { 23, 0.6f }, { 24, 0.7f }, { 25, 0.5f },
  10268. { 26, 0.7f }, { 28, 0.6f }, { 29, 0.3f }, { 30, 0.8f }, { 31, 1.0f }, { 32, 0.3f }, { 33, 0.5f }, { 34, 0.5f },
  10269. { 35, 0.3f }, { 37, 1.0f }, { 38, 0.6f }, { 39, 1.0f }, { 40, 0.4f }, { 41, 0.5f }, { 42, 0.5f }, { 43, 0.5f },
  10270. { 47, 0.5f }, { 48, 0.3f }, { 67, 0.2f }, { 68, 0.5f }, { 75, 2.0f }, { 76, 15.0f }, { 77, 1.0f }, { 78, 1.0f },
  10271. { 79, 1.0f } };
  10272. for (const auto& [i, v] : values)
  10273. P[(std::size_t) i] = v;
  10274. for (int i = 49; i < 65; ++i)
  10275. P[(std::size_t) i] = (i - 49) % 3 != 2 ? 1.0f : 0.0f;
  10276. for (int i = 81; i < 155; ++i)
  10277. P[(std::size_t) i] = -1.0f;
  10278. std::vector<std::uint8_t> blob { 'S', 'S', 'L', 'F', 0xb4, 0x02, 0x00, 0x00, '4', 'r', 'e', 'V' };
  10279. blob.resize (12 + 4 * 156 + 64, 0);
  10280. std::memcpy (blob.data() + 12, P.data(), 4 * 156);
  10281. std::memcpy (blob.data() + 12 + 4 * 156, "LYNX test", 9);
  10282. return blob;
  10283. }
  10284. } // namespace
  10285. TEST (poizone_engine)
  10286. {
  10287. const auto blob = poizoneTestState();
  10288. CHECK (lynx::poizoneStateUsable (blob));
  10289. CHECK (lynx::poizoneParamOffset (blob, 18) == 12 + 4 * 18 && lynx::poizoneParamOffset (blob, 156) < 0);
  10290. CHECK (lynx::poizoneParam (blob, 75) == 2.0f && ! lynx::poizoneParam (blob, -1));
  10291. auto broken = blob;
  10292. broken[8] = 'X'; // the "Ver4" tag
  10293. CHECK (! lynx::poizoneStateUsable (broken));
  10294. CHECK (! lynx::poizoneStateUsable (std::vector<std::uint8_t> (blob.begin(), blob.begin() + 10)));
  10295. // golden samples of our round-44 model (pzmodel.Plugin, tempo 120, 44.1 kHz, host blocks of 512, L = R = Final.Vol):
  10296. // C5 vel 100 0–30000, E5 vel 80 11025–40000, G5 vel 110 22050–52000 (Init.Vol = (11^(vel/100) − 1)/10)
  10297. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  10298. const std::vector<lynx::poizone::RefNote> notes = { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 },
  10299. { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  10300. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 } };
  10301. const auto y = lynx::poizone::renderReference (blob, notes, 44100.0, 66150);
  10302. CHECK (y.size() == 2 * 66150u);
  10303. static const struct { int i; float l, r; } golden[] = { { 100, 0.0058150403f, 0.00038307323f }, { 5000, 0.01855834f, 0.09409425f }, { 15000, -0.01985861f, -0.013393694f }, { 25000, 0.121419534f, -0.12814318f }, { 35000, -0.09936376f, 0.034945987f }, { 45000, -0.09803468f, 0.05587045f }, { 55000, -0.07215294f, 0.02417629f }, { 66000, -0.007312033f, -0.047375992f } };
  10304. for (const auto& g : golden)
  10305. CHECK (std::abs (y[2 * (std::size_t) g.i] - g.l) < 1e-7f && std::abs (y[2 * (std::size_t) g.i + 1] - g.r) < 1e-7f);
  10306. double e = 0.0;
  10307. for (auto v : y)
  10308. e += (double) v * v;
  10309. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.10990940878400073) < 1e-7);
  10310. // the instrument: same engine behind LYNX's channel gains (V = 1, centre → law A), velocity → Init.Vol
  10311. lynx::ChannelDef c;
  10312. c.instrument = lynx::InstrumentKind::poizone;
  10313. c.pluginState = blob;
  10314. auto inst = lynx::makeInstrument (lynx::InstrumentKind::poizone);
  10315. inst->configure (c, 120.0);
  10316. inst->prepare (44100.0);
  10317. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  10318. for (int b = 0; b < 16; ++b)
  10319. {
  10320. if (b == 0)
  10321. inst->noteOn (60, 0.78125f);
  10322. if (b == 10)
  10323. inst->noteOff (60);
  10324. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  10325. }
  10326. const auto ref = lynx::poizone::renderReference (blob, { { 0, 60, (std::pow (11.0, 1.28 * 0.78125) - 1.0) / 10.0, 1.0, 0.0, 0.0, 5120 } }, 44100.0, 8192,
  10327. 512, 120.0, 0, true);
  10328. double diff = 0.0, energy = 0.0;
  10329. for (int i = 0; i < 8192; ++i)
  10330. {
  10331. 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]) });
  10332. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  10333. }
  10334. CHECK (diff == 0.0 && energy > 1e-3);
  10335. }
  10336. // ---- Fruit Kick / BassDrum (REA_rodada43): synthetic states and data, golden values of our round-43 models ----
  10337. namespace
  10338. {
  10339. std::vector<std::uint8_t> fruitKickStateA()
  10340. {
  10341. const std::int32_t w[8] = { 0, 0, -600, 64, 128, 32, 20, 0 };
  10342. std::vector<std::uint8_t> b (32);
  10343. std::memcpy (b.data(), w, 32);
  10344. return b;
  10345. }
  10346. std::vector<std::uint8_t> fruitKickStateB()
  10347. {
  10348. std::vector<std::uint8_t> b (400, 0);
  10349. const std::int32_t head[7] = { 1, 624, -8, 42, 86, 64, 0 };
  10350. std::memcpy (b.data(), head, sizeof (head));
  10351. std::int32_t recs[5][17];
  10352. for (auto& r : recs)
  10353. {
  10354. const std::int32_t d[17] = { 0, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, 0 };
  10355. std::memcpy (r, d, sizeof (d));
  10356. }
  10357. const std::int32_t pan[17] = { 0, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 3000, 64, 20000, 1, 0, 0, 0 };
  10358. const std::int32_t vol[17] = { 4, -1, 100, 3000, 100, 25000, 64, 15000, 0, 100, 20000, 0, 32950, 0, 0, 40, -101 };
  10359. std::memcpy (recs[0], pan, sizeof (pan));
  10360. std::memcpy (recs[1], vol, sizeof (vol));
  10361. for (int k = 0; k < 5; ++k)
  10362. std::memcpy (b.data() + 32 + 68 * k, recs[k], 68);
  10363. const std::int32_t cut = 256;
  10364. std::memcpy (b.data() + 32 + 0x15c, &cut, 4);
  10365. return b;
  10366. }
  10367. std::string bassDrumTestFolder()
  10368. {
  10369. // our own oscillator table and a pseudo-random 16-bit noise sample, laid out like FL's BassDrum folder
  10370. const auto dir = std::filesystem::temp_directory_path() / "lynx_bassdrum_test";
  10371. std::filesystem::create_directories (dir / "Noise Samples");
  10372. auto wav = [] (const std::filesystem::path& path, const void* data, std::uint32_t bytes, std::uint16_t fmt, std::uint16_t bits) {
  10373. std::ofstream o (path, std::ios::binary);
  10374. auto u32 = [&] (std::uint32_t v) { o.write ((const char*) &v, 4); };
  10375. auto u16 = [&] (std::uint16_t v) { o.write ((const char*) &v, 2); };
  10376. o.write ("RIFF", 4), u32 (36 + bytes), o.write ("WAVEfmt ", 8), u32 (16), u16 (fmt), u16 (1), u32 (44100),
  10377. u32 (44100u * bits / 8), u16 ((std::uint16_t) (bits / 8)), u16 (bits), o.write ("data", 4), u32 (bytes);
  10378. o.write ((const char*) data, bytes);
  10379. };
  10380. std::vector<float> table (1024);
  10381. for (int i = 0; i < 1024; ++i)
  10382. table[(std::size_t) i] = (float) (0.9 * std::sin (2.0 * 3.141592653589793 * i / 1024.0) + 0.05 * std::sin (6.0 * 3.141592653589793 * i / 1024.0));
  10383. wav (dir / "sounddata", table.data(), 4096, 3, 32);
  10384. std::vector<std::int16_t> noise (3000);
  10385. std::uint32_t s = 12345;
  10386. for (auto& v : noise)
  10387. {
  10388. s = (s * 1103515245u + 12345u) & 0x7fffffffu;
  10389. v = (std::int16_t) ((int) ((s >> 8) % 20001u) - 10000);
  10390. }
  10391. wav (dir / "Noise Samples" / "test noise.wav", noise.data(), 6000, 1, 16);
  10392. return dir.string();
  10393. }
  10394. std::vector<std::uint8_t> bassDrumTestState()
  10395. {
  10396. std::vector<std::uint8_t> b (0x50C + 92, 0);
  10397. std::memcpy (b.data(), "SSLF", 4);
  10398. const std::uint32_t size = 0x560;
  10399. std::memcpy (b.data() + 4, &size, 4);
  10400. std::memcpy (b.data() + 8, "2kiK", 4);
  10401. std::memcpy (b.data() + 12, "Test kick", 9);
  10402. const char path[] = "C:/Somewhere/Noise Samples/test noise.wav";
  10403. std::memcpy (b.data() + 268, path, sizeof (path) - 1);
  10404. const float P[23] = { 0.55f, 0.5f, 0.6f, 0.6f, 0.45f, 0.1f, 0.3f, 0.4f, 0.3f, 0.6f, 0.4f, 0.65f,
  10405. 0.5f, 0.8f, 0.15f, 0.9f, 0.3f, 0.5f, 0.6f, 0.62f, 0.4f, 0.5f, 0.85f };
  10406. std::memcpy (b.data() + 0x50C, P, sizeof (P));
  10407. return b;
  10408. }
  10409. struct Golden
  10410. {
  10411. int i;
  10412. float l, r;
  10413. };
  10414. bool matchesGolden (const std::vector<float>& y, std::initializer_list<Golden> golden, double rms)
  10415. {
  10416. bool ok = true;
  10417. for (const auto& g : golden)
  10418. ok = ok && std::abs (y[2 * (std::size_t) g.i] - g.l) < 1e-6f && std::abs (y[2 * (std::size_t) g.i + 1] - g.r) < 1e-6f;
  10419. double e = 0.0;
  10420. for (auto v : y)
  10421. e += (double) v * v;
  10422. return ok && std::abs (std::sqrt (e / (double) y.size()) - rms) < 1e-6;
  10423. }
  10424. } // namespace
  10425. TEST (fruitkick_engine)
  10426. {
  10427. const auto A = fruitKickStateA(), B = fruitKickStateB();
  10428. CHECK (lynx::fruitKickStateUsable (A) && lynx::fruitKickStateUsable (B));
  10429. CHECK (! lynx::fruitKickStateUsable (std::vector<std::uint8_t> (A.begin(), A.begin() + 20)));
  10430. CHECK (lynx::fruitKickParam (A, 1) == -600 && lynx::fruitKickParam (B, 0) == 624);
  10431. CHECK (lynx::fruitKickFallbacks (A).empty() && lynx::fruitKickFallbacks (B).empty());
  10432. // a version-0 state takes the channel's envelope records as its snapshot (and becomes version 1)
  10433. std::vector<std::vector<std::uint8_t>> recs;
  10434. for (int k = 0; k < 5; ++k)
  10435. recs.emplace_back (B.begin() + 32 + 68 * k, B.begin() + 32 + 68 * (k + 1));
  10436. const auto withEnv = lynx::fruitKickWithChannelEnvelope (A, recs, 256, 0, 0);
  10437. CHECK (withEnv.size() == 400 && lynx::fruitKickParam (withEnv, 5) == 20 && std::memcmp (withEnv.data() + 32, B.data() + 32, 364) == 0);
  10438. CHECK (lynx::fruitKickWithChannelEnvelope (B, recs, 256, 0, 0) == B);
  10439. // golden samples of our round-43 model (fkmodel.run, 44.1 kHz, host blocks of 512): C5 at 0 held, C6 at 3000
  10440. // (Final.Vol 0.5, pan 0.3) released at 9000; B with the plugin's tick every 230 samples from 115
  10441. const std::vector<lynx::fruitkick::RefNote> notes = { { 0, 60, 1.0, 0.0, 0.0, -1 }, { 3000, 72, 0.5, 0.3, 0.0, 9000 } };
  10442. const auto ya = lynx::fruitkick::renderReference (A, notes, 44100.0, 20000);
  10443. CHECK (ya.size() == 40000u);
  10444. // (A uses the distortion: values since REA_rodada55 with FL's x87 live host waveshaper, checked against FLEngine's own)
  10445. CHECK (matchesGolden (ya, { { 10, 0.803601742f, 0.803601742f }, { 700, 0.185467482f, 0.185467482f }, { 2900, -0.63492614f, -0.63492614f },
  10446. { 3100, 0.339092523f, 0.544403195f }, { 5000, -0.0974622667f, -0.150664076f }, { 9100, 0.124627575f, 0.0544180274f },
  10447. { 9400, -0.146492869f, -0.144876853f }, { 15000, 0.009439799f, 0.009439799f } },
  10448. 0.284267678986));
  10449. std::vector<int> ticks;
  10450. for (int t = 115; t < 20000; t += 230)
  10451. ticks.push_back (t);
  10452. const auto yb = lynx::fruitkick::renderReference (B, notes, 44100.0, 20000, 512, ticks);
  10453. CHECK (matchesGolden (yb, { { 10, -0.00760388654f, -0.00760388654f }, { 700, 0.386696398f, 0.442232877f }, { 2900, -0.123097703f, -0.302172035f },
  10454. { 3100, 0.0523624197f, 0.0877688825f }, { 5000, 0.045177035f, 0.100505695f }, { 9100, -0.00423920294f, 0.00810315367f },
  10455. { 9400, 0.00482427329f, -0.010199016f }, { 15000, -0.000171193431f, 1.01026744e-05f } },
  10456. 0.117964927585));
  10457. // the instrument: same engine behind LYNX's channel gains (V = 1, centre), velocity → Init.Vol (link velocity on)
  10458. lynx::ChannelDef c;
  10459. c.instrument = lynx::InstrumentKind::fruitkick;
  10460. c.pluginState = A;
  10461. auto inst = lynx::makeInstrument (lynx::InstrumentKind::fruitkick);
  10462. inst->configure (c, 140.0);
  10463. inst->prepare (44100.0);
  10464. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  10465. for (int b = 0; b < 16; ++b)
  10466. {
  10467. if (b == 0)
  10468. inst->noteOn (60, 0.78125f);
  10469. if (b == 10)
  10470. inst->noteOff (60);
  10471. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  10472. }
  10473. const auto iv = (std::pow (11.0, 1.28 * 0.78125) - 1.0) / 10.0;
  10474. const auto ref = lynx::fruitkick::renderReference (A, { { 0, 60, iv, 0.0, 0.0, 5120 } }, 44100.0, 8192);
  10475. double diff = 0.0, energy = 0.0;
  10476. for (int i = 0; i < 8192; ++i)
  10477. {
  10478. 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]) });
  10479. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  10480. }
  10481. CHECK (diff < 1e-6 && energy > 1e-2);
  10482. }
  10483. TEST (bassdrum_engine)
  10484. {
  10485. // FL's CRT powf as the engine computes it: a case where its last bit differs from the correctly rounded power
  10486. const float x = 0.87011606f;
  10487. std::uint32_t bits = 0;
  10488. const auto p = lynx::bassdrum::powfMsvc (x, 0.125f);
  10489. std::memcpy (&bits, &p, 4);
  10490. CHECK (bits == 1065063964u);
  10491. CHECK (lynx::bassdrum::powfMsvc (2.0f, 1.0f) == 2.0f && lynx::bassdrum::powfMsvc (10.0f, 0.0f) == 1.0f);
  10492. const auto st = bassDrumTestState();
  10493. CHECK (lynx::bassDrumStateUsable (st) && lynx::bassDrumNoiseName (st) == "test noise.wav");
  10494. CHECK (lynx::bassDrumParamOffset (st, 6) == 0x50C + 24 && lynx::bassDrumParamOffset (st, 23) < 0);
  10495. auto broken = st;
  10496. broken[8] = 'X';
  10497. CHECK (! lynx::bassDrumStateUsable (broken));
  10498. // golden samples of our round-43 model (bdmodel.run with its powf port, 44.1 kHz, host blocks of 512)
  10499. const auto dir = bassDrumTestFolder();
  10500. const std::vector<lynx::bassdrum::RefNote> notes = { { 0, 60, 1.0, 1.0, 0.0, 0.0, -1 }, { 6000, 64, 0.6, 0.8, 0.4, 25.0, 9000 },
  10501. { 6100, 55, 1.5, 1.0, -0.3, 0.0, -1 } };
  10502. const auto y = lynx::bassdrum::renderReference (st, notes, 44100.0, 30000, dir);
  10503. CHECK (y.size() == 60000u);
  10504. CHECK (matchesGolden (y, { { 50, 0.0122119058f, 0.0122119058f }, { 400, -0.0886279643f, -0.0886279643f }, { 3000, -0.0428197235f, -0.0428197235f },
  10505. { 6050, 0.0199260078f, 0.0240648985f }, { 6300, -0.257363439f, -0.185516387f }, { 9000, 0.268954486f, 0.10506025f },
  10506. { 15000, -0.0102827176f, -0.0380715057f }, { 25000, 0.0f, 0.0f } },
  10507. 0.230007588169));
  10508. CHECK (lynx::bassdrum::renderReference (st, notes, 44100.0, 100, dir + "/nowhere").empty()); // no table: no engine
  10509. lynx::ChannelDef c;
  10510. c.instrument = lynx::InstrumentKind::bassdrum;
  10511. c.pluginState = st;
  10512. c.samplePath = dir;
  10513. auto inst = lynx::makeInstrument (lynx::InstrumentKind::bassdrum);
  10514. inst->configure (c, 120.0);
  10515. inst->prepare (44100.0);
  10516. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  10517. for (int b = 0; b < 16; ++b)
  10518. {
  10519. if (b == 0)
  10520. inst->noteOn (60, 0.78125f);
  10521. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  10522. }
  10523. const auto ref = lynx::bassdrum::renderReference (st, { { 0, 60, (std::pow (11.0, 1.28 * 0.78125) - 1.0) / 10.0, 1.0, 0.0, 0.0, -1 } }, 44100.0, 8192, dir);
  10524. double diff = 0.0, energy = 0.0;
  10525. for (int i = 0; i < 8192; ++i)
  10526. {
  10527. 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]) });
  10528. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  10529. }
  10530. CHECK (diff == 0.0 && energy > 1e-2);
  10531. }
  10532. // REA_rodada55: Fruity Fast Dist, Fruity Flangus and Fruity Flanger, goldens from the installation's DLLs (EXEC, our own states)
  10533. TEST (fruity_fast_dist_flangus_flanger)
  10534. {
  10535. std::vector<float> inL (4096), inR (4096);
  10536. for (int n = 0; n < 4096; ++n)
  10537. {
  10538. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  10539. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  10540. }
  10541. struct Gold { int i; float l, r; };
  10542. auto close = [] (float a, float b) { return std::abs (a - b) <= 2.0e-7f * std::max (1.0f, std::abs (b)); };
  10543. // Fast Dist (+ FL's host waveshaper): live and render kernels
  10544. auto dist = [&] (lynx::fastdistfx::State s, bool render, std::initializer_list<Gold> gold) {
  10545. std::vector<float> l (inL), r (inR);
  10546. lynx::fastdistfx::process (l.data(), 4096, s, render);
  10547. lynx::fastdistfx::process (r.data(), 4096, s, render);
  10548. for (const auto& g : gold)
  10549. CHECK (close (l[(std::size_t) g.i], g.l) && close (r[(std::size_t) g.i], g.r));
  10550. };
  10551. dist ({ 128, 10, 0, 128, 128 }, false, { { 100, 0.0f, -0.39352417f }, { 1000, -0.39352417f, 0.389404297f }, { 2047, 0.376953125f, -0.39352417f },
  10552. { 3000, -0.39352417f, -0.379760742f }, { 4095, 0.342590332f, 0.373931885f } });
  10553. dist ({ 128, 10, 0, 128, 128 }, true, { { 3000, -0.39352417f, -0.379751563f }, { 4095, 0.342590332f, 0.373931885f } });
  10554. dist ({ 192, 4, 1, 77, 97 }, false, { { 100, 0.0f, -0.868428111f }, { 1000, -1.23000622f, 0.740812242f }, { 2047, 0.58129251f, -0.855666518f },
  10555. { 3000, -1.23000622f, -0.613196433f }, { 4095, 0.307311207f, 0.549388587f } });
  10556. dist ({ 192, 4, 1, 77, 97 }, true, { { 2047, 0.58129245f, -0.855666518f }, { 4095, 0.307311267f, 0.549388528f } });
  10557. dist ({ 90, 7, 0, 40, 120 }, false, { { 100, 0.0f, -0.346225947f }, { 1000, -0.511893928f, 0.287246376f }, { 2047, 0.212910682f, -0.340344131f },
  10558. { 3000, -0.511893928f, -0.227848753f }, { 4095, 0.0910475403f, 0.197926477f } });
  10559. dist ({ 90, 7, 0, 40, 120 }, true, { { 100, 0.0f, -0.346223623f }, { 1000, -0.511893749f, 0.287246734f }, { 2047, 0.212909341f, -0.340341568f },
  10560. { 3000, -0.511893749f, -0.227846757f }, { 4095, 0.0910428166f, 0.197928891f } });
  10561. {
  10562. const auto* T = lynx::fastdistfx::curve (0, 10);
  10563. CHECK (T[0] == 0 && T[8191] == T[8192] && lynx::fastdistfx::curve (1, 1)[8191] == 32767);
  10564. }
  10565. // Flangus: FL's call order (state, sample rate, block size of 512), no flush
  10566. auto flange = [&] (lynx::flangusfx::State s, int fs, std::initializer_list<Gold> gold) {
  10567. lynx::flangusfx::Engine e;
  10568. e.prepare (fs);
  10569. e.load (s);
  10570. e.setRate (fs);
  10571. e.setBlockSize (512);
  10572. std::vector<float> io (8192);
  10573. for (int n = 0; n < 4096; ++n)
  10574. {
  10575. io[2 * (std::size_t) n] = inL[(std::size_t) n];
  10576. io[2 * (std::size_t) n + 1] = inR[(std::size_t) n];
  10577. }
  10578. for (int o = 0; o < 4096; o += 512)
  10579. e.render (io.data() + 2 * o, io.data() + 2 * o, 512);
  10580. for (const auto& g : gold)
  10581. CHECK (close (io[2 * (std::size_t) g.i], g.l) && close (io[2 * (std::size_t) g.i + 1], g.r));
  10582. };
  10583. flange ({ 4, 512, 768, 8, 128, -32, 96, 96 }, 44100, { { 100, 0.0249633491f, -0.119510099f }, { 1000, -1.26988697f, 0.687134743f },
  10584. { 2047, 1.03176403f, -0.278752804f }, { 3000, -0.940228283f, -0.853936791f },
  10585. { 4095, 0.201686561f, 0.607052445f } });
  10586. flange ({ 7, 300, 900, 40, 30, 100, -60, 128 }, 44100, { { 100, 0.0f, 0.0446268953f }, { 1000, 1.68992829f, 2.06982398f },
  10587. { 2047, 0.400214791f, 0.838502884f }, { 3000, 1.20018816f, 1.18875635f },
  10588. { 4095, -0.354708612f, 0.503573656f } });
  10589. flange ({ 2, 1024, 200, 0, 0, -128, 128, -90 }, 48000, { { 100, 0.172592998f, -0.230506495f }, { 1000, -1.56807125f, 0.854493499f },
  10590. { 2047, 1.02325416f, -1.17055321f }, { 3000, -1.74136555f, -0.535462856f },
  10591. { 4095, 1.49345946f, -0.537866116f } });
  10592. // Flanger: state, then the sample rate (FL's call order)
  10593. auto flanger = [&] (lynx::flangerfx::State s, int fs, std::initializer_list<Gold> gold) {
  10594. lynx::flangerfx::Engine e;
  10595. e.load (s);
  10596. e.prepare (fs);
  10597. std::vector<float> io (8192);
  10598. for (int n = 0; n < 4096; ++n)
  10599. {
  10600. io[2 * (std::size_t) n] = inL[(std::size_t) n];
  10601. io[2 * (std::size_t) n + 1] = inR[(std::size_t) n];
  10602. }
  10603. for (int o = 0; o < 4096; o += 512)
  10604. e.render (io.data() + 2 * o, io.data() + 2 * o, 512);
  10605. for (const auto& g : gold)
  10606. CHECK (close (io[2 * (std::size_t) g.i], g.l) && close (io[2 * (std::size_t) g.i + 1], g.r));
  10607. };
  10608. flanger ({ 0, 2000, 2000, 118, 0, 0, 23, 0, 1024, 3500, 3000, 2500 }, 44100,
  10609. { { 100, -1.22955823f, -1.98144293f }, { 1000, -0.975861192f, 0.646231353f }, { 2047, 0.518505156f, -0.857361794f },
  10610. { 3000, -0.546559215f, -0.0157452822f }, { 4095, 0.104127884f, 0.206255436f } });
  10611. flanger ({ 300, 4000, 4500, 90, 1000, 300, 99, 1024, 1024, 2000, 4000, 1000 }, 48000,
  10612. { { 100, 0.0f, -0.440000027f }, { 1000, 0.24619779f, 0.775473535f }, { 2047, 1.11609459f, -0.0639692694f },
  10613. { 3000, -1.22629714f, -1.41118503f }, { 4095, -0.263562649f, 0.80683732f } });
  10614. // import: FL's states become the exact effects with the controls as parameters; short states keep the approximations
  10615. {
  10616. const std::vector<std::uint8_t> fd { 192, 0, 0, 0, 4, 0, 0, 0, 1, 0, 0, 0, 77, 0, 0, 0, 97, 0, 0, 0 };
  10617. const auto slot = lynx::effectFromFl ("Fruity Fast Dist", fd, 120.0);
  10618. CHECK (slot && slot->kind == lynx::EffectKind::fruityFastDist);
  10619. if (slot)
  10620. {
  10621. const auto s = lynx::fastdistfx::fromParams (slot->params.data());
  10622. CHECK (s.pre == 192 && s.threshold == 4 && s.type == 1 && s.mix == 77 && s.post == 97);
  10623. auto fx = lynx::makeEffect (*slot);
  10624. fx->prepare (44100.0);
  10625. std::vector<float> l (inL), r (inR);
  10626. fx->process (l.data(), r.data(), 4096, slot->params, 120.0);
  10627. CHECK (l[1000] == -1.23000622f && r[4095] == 0.549388587f);
  10628. }
  10629. std::vector<std::uint8_t> fg (32);
  10630. const int v[8] { 7, 300, 900, 40, 30, 100, -60, 128 };
  10631. std::memcpy (fg.data(), v, 32);
  10632. const auto f2 = lynx::effectFromFl ("Fruity Flangus", fg, 120.0);
  10633. CHECK (f2 && f2->kind == lynx::EffectKind::fruityFlangus);
  10634. if (f2)
  10635. {
  10636. const auto s = lynx::flangusfx::fromParams (f2->params.data());
  10637. for (int i = 0; i < 8; ++i)
  10638. CHECK (s[(std::size_t) i] == v[i]);
  10639. auto fx = lynx::makeEffect (*f2);
  10640. fx->prepare (48000.0);
  10641. std::vector<float> l (inL), r (inR);
  10642. fx->process (l.data(), r.data(), 4096, f2->params, 120.0);
  10643. CHECK (std::isfinite (l[4095]) && std::isfinite (r[4095]) && std::abs (l[2000]) > 0.0f);
  10644. }
  10645. std::vector<std::uint8_t> fl (52);
  10646. const int w[13] { 1, 300, 4000, 4500, 90, 1000, 300, 99, 1024, 1024, 2000, 4000, 1000 };
  10647. std::memcpy (fl.data(), w, 52);
  10648. const auto f3 = lynx::effectFromFl ("Fruity Flanger", fl, 120.0);
  10649. CHECK (f3 && f3->kind == lynx::EffectKind::fruityFlanger);
  10650. if (f3)
  10651. {
  10652. const auto s = lynx::flangerfx::fromParams (f3->params.data());
  10653. for (int i = 0; i < 12; ++i)
  10654. CHECK (s[(std::size_t) i] == w[i + 1]);
  10655. auto fx = lynx::makeEffect (*f3);
  10656. fx->prepare (48000.0);
  10657. std::vector<float> l (inL), r (inR);
  10658. fx->process (l.data(), r.data(), 4096, f3->params, 120.0);
  10659. CHECK (l[1000] == 0.24619779f && r[4095] == 0.80683732f);
  10660. }
  10661. const auto shortFd = lynx::effectFromFl ("Fruity Fast Dist", std::vector<std::uint8_t> (8, 0), 120.0);
  10662. CHECK (! shortFd || shortFd->kind != lynx::EffectKind::fruityFastDist);
  10663. }
  10664. }
  10665. // Workflow P2 (docs/WORKFLOW_FL.md)
  10666. #include "WorkflowP2Tests.inc"
  10667. // Editor mouse grammar (Piano Roll / Playlist)
  10668. #include "EditorGestureTests.inc"
  10669. TEST (chorus_family_engines)
  10670. {
  10671. // REA_rodada59: goldens from the installation's DLLs (44.1 kHz, blocks of 512) on this signal
  10672. std::vector<float> inL (4096), inR (4096);
  10673. for (int n = 0; n < 4096; ++n)
  10674. {
  10675. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0) * 0.5f;
  10676. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0) * 0.5f;
  10677. }
  10678. struct Gold { int i; float l, r; };
  10679. auto close = [] (float a, float b) { return std::abs (a - b) <= 2.0e-7f * std::max (1.0f, std::abs (b)); };
  10680. auto run = [&] (const char* plugin, const std::vector<std::uint8_t>& st, lynx::EffectKind kind, std::initializer_list<Gold> gold) {
  10681. const auto slot = lynx::effectFromFl (plugin, st, 120.0);
  10682. CHECK (slot && slot->kind == kind);
  10683. if (! slot)
  10684. return;
  10685. auto fx = lynx::makeEffect (*slot);
  10686. fx->prepare (44100.0);
  10687. std::vector<float> l (inL), r (inR);
  10688. fx->process (l.data(), r.data(), 1024, slot->params, 120.0);
  10689. fx->process (l.data() + 1024, r.data() + 1024, 3072, slot->params, 120.0);
  10690. for (const auto& g : gold)
  10691. CHECK (close (l[(std::size_t) g.i], g.l) && close (r[(std::size_t) g.i], g.r));
  10692. };
  10693. auto ints = [] (std::initializer_list<int> v) {
  10694. std::vector<std::uint8_t> b;
  10695. for (int x : v)
  10696. for (int k = 0; k < 4; ++k)
  10697. b.push_back ((std::uint8_t) ((unsigned) x >> (8 * k)));
  10698. return b;
  10699. };
  10700. // Fruity Chorus (start phases 0, which FL draws at random)
  10701. run ("Fruity Chorus", ints ({ 1, 512, 2250, 341, 1500, 2500, 3500, 23, 0, 0, 0, 2500, 0 }), lynx::EffectKind::fruityChorus,
  10702. { { 100, 0.0f, -0.3850000202655792f }, { 1000, -0.6181581020355225f, 0.2707735002040863f }, { 2047, -0.01424609124660492f, 0.24228274822235107f },
  10703. { 3000, -1.1650776863098145f, -0.1809898018836975f }, { 4095, -0.012790083885192871f, 0.060571759939193726f } });
  10704. run ("Fruity Chorus", ints ({ 1, 700, 4000, 900, 4000, 3000, 2000, 400, 700, 1000, 30, 4999, 0 }), lynx::EffectKind::fruityChorus,
  10705. { { 100, 0.0f, -0.3850000202655792f }, { 1000, -0.5833333134651184f, 0.36345374584198f }, { 2047, -0.0336097776889801f, -0.3360772430896759f },
  10706. { 3000, -0.40196430683135986f, -0.5073210597038269f }, { 4095, -0.1176539734005928f, 0.444706529378891f } });
  10707. run ("Fruity Chorus", ints ({ 1, 36, 469, 1004, 2832, 293, 3500, 0, 428, 0, 0, 3887, 1024 }), lynx::EffectKind::fruityChorus,
  10708. { { 100, 0.2975943684577942f, 0.3747217655181885f }, { 1000, -0.39463338255882263f, 0.18099871277809143f }, { 2047, 0.3894335627555847f, 0.35111793875694275f },
  10709. { 3000, -0.1450725942850113f, 0.2847272455692291f }, { 4095, 0.33678582310676575f, -0.13365517556667328f } });
  10710. {
  10711. const auto slot = lynx::effectFromFl ("Fruity Chorus", ints ({ 1, 36, 469, 1004, 2832, 293, 3500, 0, 428, 0, 0, 3887, 1024 }), 120.0);
  10712. CHECK (slot && lynx::fruitychorusfx::fromParams (slot->params.data())[2] == 1004);
  10713. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityChorus, 11, 1.0f, 120.0) == "on");
  10714. CHECK (lynx::effectParamFromFl ("Fruity Chorus", 11).has_value() && ! lynx::effectParamFromFl ("Fruity Chorus", 12).has_value());
  10715. }
  10716. // a state FL does not read (version 2) or too short keeps the generic chorus
  10717. CHECK (! lynx::effectFromFl ("Fruity Chorus", ints ({ 2, 512, 2250, 341, 1500, 2500, 3500, 23, 0, 0, 0, 2500, 0 }), 120.0).has_value()
  10718. || lynx::effectFromFl ("Fruity Chorus", ints ({ 2, 512, 2250, 341, 1500, 2500, 3500, 23, 0, 0, 0, 2500, 0 }), 120.0)->kind != lynx::EffectKind::fruityChorus);
  10719. CHECK (! lynx::effectFromFl ("Fruity Chorus", { 1, 0, 0, 0 }, 120.0).has_value()
  10720. || lynx::effectFromFl ("Fruity Chorus", { 1, 0, 0, 0 }, 120.0)->kind != lynx::EffectKind::fruityChorus);
  10721. // Hyper Chorus engine in FL's load sequence (defaults, then the state: the parameters glide), instance k's phases
  10722. auto hyper = [&] (std::initializer_list<int> raws, int instance, std::initializer_list<Gold> gold) {
  10723. namespace hc = lynx::hyperchorusfx;
  10724. hc::Engine e (instance);
  10725. const std::vector<int> r (raws);
  10726. auto val = [] (int i, int raw) { return (float) std::clamp ((double) (raw - hc::rawMin[(std::size_t) i]) * (1.0 / (double) (hc::rawMax[(std::size_t) i] - hc::rawMin[(std::size_t) i])), 0.0, 1.0); };
  10727. for (int i = 0; i < hc::params; ++i)
  10728. e.setParam (i, val (i, hc::defaults[(std::size_t) i]));
  10729. for (int i = 0; i < hc::params; ++i)
  10730. e.setParam (i, val (i, r[(std::size_t) i]));
  10731. e.setRate (44100.0);
  10732. std::vector<float> io (8192);
  10733. for (int n = 0; n < 4096; ++n)
  10734. io[2 * (std::size_t) n] = inL[(std::size_t) n], io[2 * (std::size_t) n + 1] = inR[(std::size_t) n];
  10735. for (int s = 0; s < 4096; s += 512)
  10736. e.render (io.data() + 2 * s, io.data() + 2 * s, 512);
  10737. for (const auto& g : gold)
  10738. CHECK (close (io[2 * (std::size_t) g.i], g.l) && close (io[2 * (std::size_t) g.i + 1], g.r));
  10739. };
  10740. hyper ({ 562, 919, 499, 505, 709, 716, 1, 19247, 10900, 191, 1000 }, 0,
  10741. { { 100, 0.0f, -0.4115639328956604f }, { 1000, -0.658551037311554f, 0.28647756576538086f }, { 2047, 0.24743860960006714f, -0.5088979005813599f },
  10742. { 3000, -0.5171564221382141f, -0.3630276620388031f }, { 4095, 0.08128821104764938f, 0.18811003863811493f } });
  10743. hyper ({ 1000, 555, 0, 400, 300, 300, 0, 22000, 120, 368, 750 }, 2,
  10744. { { 100, 0.0f, -0.398922324180603f }, { 1000, -1.1528236865997314f, 0.2839684784412384f }, { 2047, 0.5115746855735779f, -0.3330395221710205f },
  10745. { 3000, -0.6094785928726196f, 0.17040175199508667f }, { 4095, -0.11666109412908554f, 0.21931695938110352f } });
  10746. CHECK (lynx::hyperchorusfx::randState (1) == 1u * 214013u + 2531011u);
  10747. {
  10748. // the import: controls as parameters; a song's Hyper Chorus slots are numbered in mixer order
  10749. const auto st = ints ({ 2, 1000, 555, 0, 400, 300, 300, 0, 22000, 120, 368, 750, 0 });
  10750. const auto slot = lynx::effectFromFl ("Hyper Chorus", st, 120.0);
  10751. CHECK (slot && slot->kind == lynx::EffectKind::hyperChorus && lynx::hyperchorusfx::fromParams (slot->params.data())[9] == 368);
  10752. lynx::Song song;
  10753. song.mixer.resize (3);
  10754. song.mixer[1].effects = { *slot, *slot };
  10755. song.mixer[2].effects = { *slot };
  10756. lynx::numberChorusInstances (song);
  10757. CHECK (lynx::hyperchorusfx::instanceOf (song.mixer[2].effects[0].state) == 2);
  10758. auto fx = lynx::makeEffect (song.mixer[2].effects[0]);
  10759. fx->prepare (48000.0);
  10760. std::vector<float> l (inL), r (inR);
  10761. fx->process (l.data(), r.data(), 4096, slot->params, 120.0);
  10762. bool finite = true, wet = false;
  10763. for (int n = 0; n < 4096; ++n)
  10764. {
  10765. finite = finite && std::isfinite (l[(std::size_t) n]) && std::isfinite (r[(std::size_t) n]);
  10766. wet = wet || l[(std::size_t) n] != inL[(std::size_t) n];
  10767. }
  10768. CHECK (finite && wet);
  10769. CHECK (lynx::formatEffectParam (lynx::EffectKind::hyperChorus, 8, 0.0f, 120.0) == "off");
  10770. CHECK (! lynx::effectFromFl ("Hyper Chorus", ints ({ 3, 1000, 555, 0, 400, 300, 300, 0, 22000, 120, 368, 750, 0 }), 120.0).has_value()
  10771. || lynx::effectFromFl ("Hyper Chorus", ints ({ 3, 1000, 555, 0, 400, 300, 300, 0, 22000, 120, 368, 750, 0 }), 120.0)->kind != lynx::EffectKind::hyperChorus);
  10772. }
  10773. // Vintage Chorus engine as FL drives it (created, sample rate, each control), fresh instance (its smoothed values
  10774. // start from the engine's initial ones)
  10775. auto vintage = [&] (std::initializer_list<int> raws, std::initializer_list<Gold> gold) {
  10776. namespace vc = lynx::vintagechorusfx;
  10777. vc::Engine e;
  10778. e.setRate (44100.0);
  10779. int i = 0;
  10780. for (int raw : raws)
  10781. {
  10782. e.setParam (i, (float) std::clamp ((double) (raw - vc::rawMin[(std::size_t) i]) * (1.0 / (double) (vc::rawMax[(std::size_t) i] - vc::rawMin[(std::size_t) i])), 0.0, 1.0));
  10783. ++i;
  10784. }
  10785. std::vector<float> io (8192);
  10786. for (int n = 0; n < 4096; ++n)
  10787. io[2 * (std::size_t) n] = inL[(std::size_t) n], io[2 * (std::size_t) n + 1] = inR[(std::size_t) n];
  10788. for (int s = 0; s < 4096; s += 512)
  10789. e.render (io.data() + 2 * s, io.data() + 2 * s, 512);
  10790. for (const auto& g : gold)
  10791. CHECK (close (io[2 * (std::size_t) g.i], g.l) && close (io[2 * (std::size_t) g.i + 1], g.r));
  10792. };
  10793. vintage ({ 2, 0, 211046, 1, 2411, 960, 1985, 5121, 0, 1, 4420, 6000, 273, 0, 0, 0, 0 },
  10794. { { 100, 0.0f, -0.05606573447585106f }, { 1000, -0.3413679003715515f, 0.20469821989536285f }, { 2047, 0.1487489938735962f, -0.28199973702430725f },
  10795. { 3000, -0.31699514389038086f, -0.43230289220809937f }, { 4095, 0.1255197525024414f, 0.0810704305768013f } });
  10796. vintage ({ 2, 0, 80, 0, 0, 60, 3000, 3000, 0, 0, 0, 3000, 0, 3000, 0, 0, 0 }, // hiss
  10797. { { 100, -1.2527654007499223e-06f, -0.053655341267585754f }, { 1000, -0.44306400418281555f, 0.25606146454811096f }, { 2047, 0.23425261676311493f, -0.21375307440757751f },
  10798. { 3000, -0.5793180465698242f, -0.13371579349040985f }, { 4095, 0.05595953390002251f, 0.11362095922231674f } });
  10799. vintage ({ 0, 1, 49152, 0, 3000, 60, 3000, 3000, 0, 0, 0, 3000, 0, 0, 0, 0, 0 }, // Chorus 1 + 2
  10800. { { 100, 0.0f, -0.05365874245762825f }, { 1000, -0.5095958113670349f, 0.2809423506259918f }, { 2047, 0.17256303131580353f, -0.3563305139541626f },
  10801. { 3000, -0.471851646900177f, -0.30123069882392883f }, { 4095, 0.0959278866648674f, 0.13436323404312134f } });
  10802. {
  10803. const auto st = ints ({ 1, 2, 0, 211046, 1, 2411, 960, 1985, 5121, 0, 1, 4420, 6000, 273, 0, 0, 0, 0 });
  10804. const auto slot = lynx::effectFromFl ("Vintage Chorus", st, 120.0);
  10805. CHECK (slot && slot->kind == lynx::EffectKind::vintageChorus && lynx::vintagechorusfx::fromParams (slot->params.data())[2] == 211046);
  10806. auto fx = lynx::makeEffect (*slot);
  10807. fx->prepare (44100.0);
  10808. std::vector<float> l (inL), r (inR);
  10809. fx->process (l.data(), r.data(), 4096, slot->params, 120.0);
  10810. bool finite = true;
  10811. for (int n = 0; n < 4096; ++n)
  10812. finite = finite && std::isfinite (l[(std::size_t) n]) && std::isfinite (r[(std::size_t) n]);
  10813. CHECK (finite && l[3000] != inL[3000]);
  10814. CHECK (lynx::formatEffectParam (lynx::EffectKind::vintageChorus, 0, 0.0f, 120.0) == "Chorus 1");
  10815. CHECK (! lynx::effectFromFl ("Vintage Chorus", ints ({ 2, 2, 0, 211046, 1, 2411, 960, 1985, 5121, 0, 1, 4420, 6000, 273, 0, 0, 0, 0 }), 120.0).has_value()
  10816. || lynx::effectFromFl ("Vintage Chorus", ints ({ 2, 2, 0, 211046, 1, 2411, 960, 1985, 5121, 0, 1, 4420, 6000, 273, 0, 0, 0, 0 }), 120.0)->kind != lynx::EffectKind::vintageChorus);
  10817. }
  10818. }
  10819. #include "UiChromeTests.inc"
  10820. TEST (fruity_delay_engines)
  10821. {
  10822. // REA_rodada52: the engines on their own against the installation's DLLs (goldens = the DLL's output for the same
  10823. // input with FL's set-up order, EXEC: bit-identical), then the mixer effects built from FL states
  10824. const int n = 16384;
  10825. std::vector<float> inL ((std::size_t) n, 0.0f), inR ((std::size_t) n, 0.0f);
  10826. std::uint32_t seed = 1;
  10827. for (int i = 0; i < 4096; ++i) // a noise burst, then the echoes
  10828. {
  10829. seed = seed * 1664525u + 1013904223u;
  10830. inL[(std::size_t) i] = (float) ((int) (seed >> 16) - 32768) / 32768.0f * 0.5f;
  10831. seed = seed * 1664525u + 1013904223u;
  10832. inR[(std::size_t) i] = (float) ((int) (seed >> 16) - 32768) / 32768.0f * 0.5f;
  10833. }
  10834. using Golden = std::vector<std::tuple<int, float, float>>;
  10835. auto check = [&] (const std::vector<float>& l, const std::vector<float>& r, const Golden& golden) {
  10836. for (const auto& [i, gl, gr] : golden)
  10837. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  10838. };
  10839. auto run3 = [&] (const std::array<int, 26>& raw, double bpm) {
  10840. lynx::fldelay::Delay3Engine e (44100.0);
  10841. e.setTempo (bpm);
  10842. for (int i = 0; i < 26; ++i)
  10843. e.setParameter (i, lynx::fldelay::delay3EngineValue (i, raw[(std::size_t) i]));
  10844. e.setSampleRate (44100.0); // FL's wrapper sets the rate up again after the state
  10845. e.setSampleRate (44100.0);
  10846. std::vector<float> l (inL), r (inR);
  10847. for (int s = 0; s < n; s += 512)
  10848. e.process (l.data() + s, r.data() + s, 512);
  10849. return std::make_pair (l, r);
  10850. };
  10851. auto run2 = [&] (const std::array<int, 8>& raw, double bpm) {
  10852. lynx::fldelay::Delay2State st;
  10853. st.v = raw;
  10854. lynx::fldelay::Delay2Engine e (st, 44100.0, bpm, 512);
  10855. std::vector<float> l (inL), r (inR);
  10856. for (int s = 0; s < n; s += 512)
  10857. e.process (l.data() + s, r.data() + s, 512);
  10858. return std::make_pair (l, r);
  10859. };
  10860. // Delay 3, true stereo, synced 1/8 beat, cutoff + resonance (Fruity Delay 3_x64.dll at 120 and 100 BPM)
  10861. 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 };
  10862. auto o = run3 (a, 120.0);
  10863. check (o.first, o.second, { { 3000, 0.399949551f, 0.598533869f }, { 6000, 0.116703831f, -0.315510482f }, { 9000, -0.150520369f, -0.250620633f },
  10864. { 13000, 0.193608373f, 0.0598564595f }, { 16000, -0.0524701327f, 0.022601109f } });
  10865. o = run3 (a, 100.0);
  10866. check (o.first, o.second, { { 5000, 0.377422422f, 0.346570343f }, { 6000, -0.182619616f, 0.0488029197f }, { 11000, -0.00120426295f, 0.0253051519f },
  10867. { 16000, 0.0806550607f, -0.0991330743f } });
  10868. // ping pong, keep pitch, modulation, cutoff LFO, diffusion, saturation, rate and bit reduction, colour
  10869. 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);
  10870. check (o.first, o.second, { { 1500, 0.288540125f, -0.148862943f }, { 5000, 5.90502168e-06f, 0.111382999f }, { 8000, -0.0736631379f, -0.0305093247f },
  10871. { 11000, -0.00831047911f, 0.0102323527f }, { 16000, 0.0175791476f, 0.0273917634f } });
  10872. // fake stereo, unsynced, high-pass, limit distortion, no dry
  10873. 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);
  10874. check (o.first, o.second, { { 1500, 0.0825810209f, 0.0228505582f }, { 6000, -0.0427959524f, -0.0408911295f }, { 9000, 0.0114790015f, 0.019227922f },
  10875. { 16000, -0.000312746735f, 0.00101647642f } });
  10876. // delay type "Off": filter, distortion and the bit/rate reduction on the dry path
  10877. 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);
  10878. check (o.first, o.second, { { 1500, 0.238686681f, -0.106820084f }, { 3000, 0.131528616f, 0.0665975735f }, { 5000, 9.94473215e-09f, -8.22819057e-09f },
  10879. { 11000, 0.0f, 0.0f } });
  10880. // Delay 2: default, ping pong with a stereo offset, swapped without dry (Fruity Delay 2_x64.dll)
  10881. o = run2 ({ 0, 128, 128, 64, 36, 0, 0, 128 }, 120.0);
  10882. check (o.first, o.second, { { 3000, 0.211380005f, 0.277908325f }, { 5000, -0.223615795f, -0.0189359933f }, { 9000, 0.120054141f, -0.00297877868f },
  10883. { 16000, 0.0182533264f, 0.105384827f } });
  10884. o = run2 ({ -40, 110, 90, 90, 24, -300, 2, 90 }, 100.0);
  10885. check (o.first, o.second, { { 3000, 0.0929639712f, 0.122222826f }, { 5000, -0.0884733275f, -0.0507739782f }, { 11000, 0.0589251742f, 0.0f },
  10886. { 16000, -0.029524127f, 0.0128910914f } });
  10887. o = run2 ({ 60, 140, 0, 100, 30, 250, 1, 110 }, 120.0);
  10888. check (o.first, o.second, { { 1500, 0.0f, 0.0f }, { 5000, 0.176379532f, -0.212506086f }, { 8000, -0.324207425f, 0.0675852001f },
  10889. { 13000, 0.145023212f, -0.104788139f } });
  10890. // the mixer effects from FL states: time law (Delay 3 synced 192 = 1 beat; Delay 2 144 = 3/4 beat), settled start
  10891. auto state3 = std::vector<std::uint8_t> (4 + 26 * 4, 0);
  10892. 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 };
  10893. state3[0] = 1;
  10894. std::memcpy (state3.data() + 4, def3.data(), 26 * 4);
  10895. const auto slot3 = lynx::effectFromFl ("Fruity Delay 3", state3, 120.0);
  10896. CHECK (slot3 && slot3->kind == lynx::EffectKind::fruityDelay3);
  10897. auto state2 = std::vector<std::uint8_t> (32, 0);
  10898. const std::array<int, 8> def2 { 0, 128, 128, 0, 144, 0, 0, 128 };
  10899. std::memcpy (state2.data(), def2.data(), 32);
  10900. const auto slot2 = lynx::effectFromFl ("Fruity delay 2", state2, 120.0);
  10901. CHECK (slot2 && slot2->kind == lynx::EffectKind::fruityDelay2);
  10902. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityDelay2, 4, slot2->params[4], 120.0) == "3:00");
  10903. auto echoAt = [] (const lynx::EffectSlot& slot, double bpm) {
  10904. auto fx = lynx::makeEffect (slot);
  10905. fx->prepare (44100.0);
  10906. std::vector<float> l (48000, 0.0f), r (48000, 0.0f);
  10907. l[100] = 1.0f;
  10908. for (int s = 0; s < 48000; s += 1024)
  10909. fx->process (l.data() + s, r.data() + s, std::min (1024, 48000 - s), slot.params, bpm);
  10910. int best = 200;
  10911. for (int i = 200; i < 48000; ++i)
  10912. if (std::abs (l[(std::size_t) i]) > std::abs (l[(std::size_t) best]))
  10913. best = i;
  10914. return best - 100;
  10915. };
  10916. if (slot3 && slot2)
  10917. {
  10918. CHECK (echoAt (*slot3, 120.0) == 22050);
  10919. CHECK (echoAt (*slot3, 150.0) == 17640);
  10920. CHECK (echoAt (*slot2, 120.0) == 16538);
  10921. CHECK (echoAt (*slot2, 140.0) == 14175);
  10922. }
  10923. }
  10924. #include "UiRackMixerTests.inc"
  10925. // --- REA round 64: SimSynth (exact engine) and Wasp / Wasp XT (FL 2026's silent placeholder) ---------------------
  10926. namespace
  10927. {
  10928. // a SimSynth state written from scratch (REA_rodada64 §1.1 layout: 45 doubles, 19 unused ints, 19 switches, 12 unused
  10929. // ints, version)
  10930. std::vector<std::uint8_t> simSynthTestState (const std::array<double, 45>& P, const std::array<int, 19>& S, int version = 14)
  10931. {
  10932. std::vector<std::uint8_t> b (564, 0);
  10933. std::memcpy (b.data(), P.data(), 360);
  10934. std::memcpy (b.data() + 360 + 76, S.data(), 76);
  10935. std::memcpy (b.data() + 560, &version, 4);
  10936. return b;
  10937. }
  10938. // the 0xD5 records of an FL preset file (.fst: the FLhd/FLdt event stream) that follow a plugin name `plugin`
  10939. std::vector<std::vector<std::uint8_t>> pluginStatesIn (const std::filesystem::path& file, const std::string& plugin)
  10940. {
  10941. std::vector<std::vector<std::uint8_t>> out;
  10942. std::ifstream in (file, std::ios::binary);
  10943. const std::vector<std::uint8_t> b ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  10944. std::size_t i = 0;
  10945. while (i + 8 <= b.size() && std::memcmp (b.data() + i, "FLdt", 4) != 0)
  10946. ++i;
  10947. i += 8;
  10948. std::string name;
  10949. while (i < b.size())
  10950. {
  10951. const auto id = b[i++];
  10952. std::size_t n = id < 64 ? 1 : id < 128 ? 2 : id < 192 ? 4 : 0;
  10953. if (id >= 192)
  10954. for (int shift = 0; i < b.size(); shift += 7)
  10955. {
  10956. const auto c = b[i++];
  10957. n |= (std::size_t) (c & 0x7F) << shift;
  10958. if (c < 0x80)
  10959. break;
  10960. }
  10961. if (i + n > b.size())
  10962. break;
  10963. if (id == 0xC9)
  10964. {
  10965. name.clear();
  10966. for (std::size_t k = 0; k < n; ++k)
  10967. if (b[i + k] != 0)
  10968. name += (char) b[i + k]; // ASCII or UTF-16LE (zero bytes skipped)
  10969. }
  10970. if (id == 0xD5 && name == plugin)
  10971. out.emplace_back (b.begin() + (std::ptrdiff_t) i, b.begin() + (std::ptrdiff_t) (i + n));
  10972. i += n;
  10973. }
  10974. return out;
  10975. }
  10976. } // namespace
  10977. TEST (simsynth_engine)
  10978. {
  10979. // A: pulse + octave layer, half-cosine with unison and two octave layers, sine/sine⁴ mix, square LFO → pitch with
  10980. // delay, filter envelope, LP/BP mix, chorus. B (version 7): triangle ring-modulated by filtered noise, sample &
  10981. // hold LFO → cutoff, retrigger, own filter envelope, chorus off
  10982. const std::array<double, 45> A = { 0.3, 0.5, 0.55, 0.8, 0.6, 0.5, 0.2, 0.5, 0.75, 0.45, 0.6, 0.5, 0.5, 0.1, 0.7, 0.25, 0.5, 0.5, 0.5, 0.7, 0.3, 0.3, 0.8,
  10983. 0.5, 0.2, 0.0, 0.35, 0.55, 0.8, 0.6, 0.4, 0.0, 0.3, 0.2, 0.0, 0.2, 0.5, 0.6, 0.4, 0.7, 1.0, 0.3, 0.4, 0.5, 0.3 };
  10984. const std::array<int, 19> SA = { 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1 };
  10985. const std::array<double, 45> B = { 0.5, 0.5, 0.5, 0.9, 0.5, 0.5, 0.0, 0.5, 0.6, 0.5, 0.5, 0.5, 0.5, 0.4, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.0, 0.6,
  10986. 0.5, 0.4, 0.0, 0.5, 0.8, 0.6, 0.85, 0.0, 0.0, 0.0, 0.0, 0.0, 0.1, 0.3, 0.2, 0.5, 0.6, 1.0, 0.0, 0.5, 0.0, 0.0 };
  10987. const std::array<int, 19> SB = { 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0 };
  10988. const auto blobA = simSynthTestState (A, SA), blobB = simSynthTestState (B, SB, 7);
  10989. dumpIfAsked ("LYNX_DUMP_SIMSYNTH", blobA);
  10990. CHECK (lynx::simSynthStateUsable (blobA) && ! lynx::simSynthStateUsable (std::vector<std::uint8_t> (blobA.begin(), blobA.begin() + 563)));
  10991. const auto hp = lynx::simSynthHostParams (blobA);
  10992. // knobs go through ⌊65535·v⌋, the coarse pitches through 48 steps, the switches and wave selectors as they are
  10993. CHECK (hp.size() == 58u && hp[0] == 52428 && hp[27] == 10080 && hp[41] == 1 && hp[54] == 0 && hp[56] == 4);
  10994. // golden samples of the installation's SimSynth_x64.dll (round-64 EXEC harness: created, state restored, 44.1 kHz,
  10995. // host blocks of 512, old voice record) — LYNX's engine renders bit-identically: C5 0–30000, E5 Final.Vol 0.8 pan
  10996. // 16 11025–40000, G5 pan −20 +25 cents 22050–52000
  10997. const std::vector<lynx::simsynth::RefNote> notes = { { 0, 60, 1.0f, 0, 0, 0.0f, 0.0f, 30000 },
  10998. { 11025, 64, 0.8f, 16, 0, 0.0f, 0.0f, 40000 },
  10999. { 22050, 67, 1.0f, -20, 25, 0.0f, 0.0f, 52000 } };
  11000. struct Golden { int i; float l, r; };
  11001. static const Golden goldenA[] = { { 100, 0.0f, 0.000322219421f }, { 5000, -0.110216402f, -0.194837824f }, { 15000, 0.517085433f, -0.551030278f }, { 25000, -0.443528295f, 0.226643473f }, { 35000, -0.708713174f, -0.291932464f }, { 45000, -0.0335822627f, -0.00557783246f }, { 55000, 0.0926972628f, 0.076306425f }, { 66000, 0.0415042415f, -0.0125555545f } };
  11002. static const Golden goldenB[] = { { 100, 0.00706971437f, 0.00706971437f }, { 5000, -0.0311671626f, -0.0311671626f }, { 15000, 0.508298159f, 0.964286387f }, { 25000, -3.07005596f, -1.8650744f }, { 35000, -0.23775965f, -0.14130348f }, { 45000, -0.00415330986f, -0.00409662398f }, { 55000, 0.00124766713f, -0.00180800457f }, { 66000, 0.0196316559f, 0.0166722424f } };
  11003. for (int which = 0; which < 2; ++which)
  11004. {
  11005. std::vector<std::int64_t> counters;
  11006. const auto y = lynx::simsynth::renderReference (which == 0 ? blobA : blobB, notes, 44100.0, 66150, 512, &counters);
  11007. CHECK (y.size() == 2 * 66150u);
  11008. if (y.size() != 2 * 66150u)
  11009. continue;
  11010. // the random-table index after creation / state / rate set equals the DLL's (276 / 696 / 756 and 276 / 492 / 528)
  11011. CHECK (counters.size() == 4u && counters[2] == 276 && counters[3] == (which == 0 ? 696 : 492) && counters[0] == (which == 0 ? 756 : 528));
  11012. for (const auto& g : which == 0 ? std::vector (std::begin (goldenA), std::end (goldenA)) : std::vector (std::begin (goldenB), std::end (goldenB)))
  11013. CHECK (y[2 * (std::size_t) g.i] == g.l && y[2 * (std::size_t) g.i + 1] == g.r);
  11014. double e = 0.0;
  11015. for (auto v : y)
  11016. e += (double) v * v;
  11017. CHECK (std::abs (std::sqrt (e / (double) y.size()) - (which == 0 ? 0.290665331870 : 0.992116840643)) < 1e-9);
  11018. }
  11019. // the instrument: same engine behind LYNX's channel gains (V = 1, centre → Final.Vol 1, Final.Pan 0)
  11020. lynx::ChannelDef c;
  11021. c.instrument = lynx::InstrumentKind::simsynth;
  11022. c.pluginState = blobA;
  11023. auto inst = lynx::makeInstrument (lynx::InstrumentKind::simsynth);
  11024. inst->prepare (44100.0);
  11025. inst->configure (c, 120.0);
  11026. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  11027. for (int k = 0; k < 16; ++k)
  11028. {
  11029. if (k == 0)
  11030. inst->noteOn (60, 0.8f);
  11031. if (k == 10)
  11032. inst->noteOff (60);
  11033. inst->render (l.data() + 512 * k, r.data() + 512 * k, 512, 1.0f, 1.0f);
  11034. }
  11035. const auto ref = lynx::simsynth::renderReference (blobA, { { 0, 60, 1.0f, 0, 0, 0.0f, 0.0f, 5120 } }, 44100.0, 8192);
  11036. double diff = 0.0, energy = 0.0;
  11037. for (int i = 0; i < 8192; ++i)
  11038. {
  11039. 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]) });
  11040. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  11041. }
  11042. CHECK (diff == 0.0 && energy > 1e-3);
  11043. // the installation's SimSynth presets (when the FL data folder is given): every state usable and sounding
  11044. if (! realFlpDir.empty())
  11045. {
  11046. const auto folder = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "SimSynth";
  11047. int presets = 0, usable = 0, sounding = 0;
  11048. if (std::filesystem::exists (folder))
  11049. for (const auto& entry : std::filesystem::directory_iterator (folder))
  11050. for (const auto& st : pluginStatesIn (entry.path(), "SimSynth"))
  11051. {
  11052. ++presets;
  11053. if (! lynx::simSynthStateUsable (st))
  11054. continue;
  11055. ++usable;
  11056. const auto y = lynx::simsynth::renderReference (st, { { 0, 48, 1.0f, 0, 0, 0.0f, 0.0f, 11025 } }, 44100.0, 22050);
  11057. double e = 0.0;
  11058. bool finite = true;
  11059. for (auto v : y)
  11060. finite = finite && std::isfinite (v), e += (double) v * v;
  11061. CHECK (finite);
  11062. sounding += e > 1e-9 ? 1 : 0;
  11063. }
  11064. std::printf (" SimSynth presets: %d, %d usable, %d sounding\n", presets, usable, sounding);
  11065. CHECK (presets == 0 || (usable == presets && presets >= 70 && sounding >= presets - 2));
  11066. }
  11067. }
  11068. TEST (wasp_is_a_silent_placeholder)
  11069. {
  11070. // FL 2026's Wasp / Wasp XT DLL keeps the state and plays nothing: so does LYNX (REA_rodada64 §5)
  11071. auto inst = lynx::makeInstrument (lynx::InstrumentKind::wasp);
  11072. inst->prepare (44100.0);
  11073. std::vector<float> l (1024, 0.0f), r (1024, 0.0f);
  11074. inst->noteOn (60, 1.0f);
  11075. inst->render (l.data(), r.data(), 1024, 1.0f, 1.0f);
  11076. CHECK (inst->idle() && std::all_of (l.begin(), l.end(), [] (float v) { return v == 0.0f; }));
  11077. // import: a SimSynth channel plays on the exact engine, Wasp and Wasp XT channels become the silent placeholder with
  11078. // their state kept
  11079. std::vector<std::uint8_t> ss (564, 0), wasp (169, 1);
  11080. const int version = 14;
  11081. std::memcpy (ss.data() + 560, &version, 4);
  11082. FlpBuilder b;
  11083. b.ascii (0xC7, "25.2.5.5319");
  11084. int index = 0;
  11085. for (const auto& [name, state] : { std::pair<std::u16string, std::vector<std::uint8_t>> { u"SimSynth", ss }, { u"Wasp", wasp }, { u"Wasp XT", wasp } })
  11086. {
  11087. b.u8 (0x40); b.u16 ((std::uint16_t) index++);
  11088. b.u8 (0x15); b.u8 (2);
  11089. b.utf16 (0xC9, name);
  11090. b.data (0xD5, state);
  11091. }
  11092. const auto song = lynx::importFlp (flp::parseProject (b.bytes));
  11093. CHECK (song.channels.size() == 3u);
  11094. if (song.channels.size() == 3u)
  11095. {
  11096. CHECK (song.channels[0].instrument == lynx::InstrumentKind::simsynth && song.channels[0].pluginState == ss);
  11097. CHECK (song.channels[1].instrument == lynx::InstrumentKind::wasp && song.channels[1].pluginState == wasp);
  11098. CHECK (song.channels[2].instrument == lynx::InstrumentKind::wasp);
  11099. }
  11100. }
  11101. #include "WaveAnalysisTests.inc"
  11102. #include "UiPolish2Tests.inc"
  11103. #include "UiToolbarTests.inc"
  11104. #include "LuxeVerbTests.inc"
  11105. #include "ConvolverTests.inc"
  11106. #include "HardcoreTests.inc"