tests.cpp 899 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406
  1. // Core tests (no JUCE). Usage: lynx_tests [folder-with-flp-files] [--list] [--filter a,b] [--exclude a,b] [--shard i/n] [--weights f] [--times f] [--no-times]
  2. #include <algorithm>
  3. #include <optional>
  4. #include <atomic>
  5. #include <chrono>
  6. #include <thread>
  7. #include <random>
  8. #include <cctype>
  9. #include <bit>
  10. #include <cmath>
  11. #include <cstdio>
  12. #include <cstring>
  13. #include <filesystem>
  14. #include <map>
  15. #include <set>
  16. #include <sstream>
  17. #include <fstream>
  18. #include <functional>
  19. #include <iterator>
  20. #include <string>
  21. #include <tuple>
  22. #include <vector>
  23. #if defined(__SSE__) || defined(_M_X64) || defined(__x86_64__) || defined(__aarch64__) || defined(_M_ARM64) // x86 SSE, or ARM64 through core/Simd.h (sse2neon)
  24. #include "core/Multilink.h"
  25. #include "core/Simd.h"
  26. #endif
  27. #include "core/AutomationDisplay.h"
  28. #include "core/ShownEffectSlot.h"
  29. #include "core/Dither.h"
  30. #include "core/DrumSynthLive.h"
  31. #include "core/Effects.h"
  32. #include "core/MixSuite.h"
  33. #include "core/StrikeShaper.h"
  34. #include "core/EmberClassic.h"
  35. #include "core/Effector.h"
  36. #include "core/FruityDelayBank.h"
  37. #include "core/FruityMultiband.h"
  38. #include "core/Denormals.h"
  39. #include "core/TestProfile.h"
  40. #include "core/FlpImport.h"
  41. #include "core/MidiControls.h"
  42. #include "core/MidiImport.h"
  43. #include "core/ChannelMiscFx.h"
  44. #include "core/FlEffects.h"
  45. #include "core/ProjectContents.h"
  46. #include "core/FlPatcher.h"
  47. #include "core/Patcher.h"
  48. #include "core/Layer.h"
  49. #include "core/XyController.h"
  50. #include "core/ControlSurface.h"
  51. #include "core/VoltageController.h"
  52. #include "core/FlSamplers.h"
  53. #include "core/FlSynths.h"
  54. #include "core/Inflate.h"
  55. #include "core/WavPack.h"
  56. #include "core/SampleFiles.h"
  57. #include "core/FlWrapper.h"
  58. #include "core/FlPluginDb.h"
  59. #include "core/MissingFiles.h"
  60. #include "core/PianoKeys.h"
  61. #include "core/PianoRollTools.h"
  62. #include "core/EventEdit.h"
  63. #include "core/Preferences.h"
  64. #include "core/MidiSync.h"
  65. #include "core/History.h"
  66. #include "core/Gms.h"
  67. #include "core/PoiZone.h"
  68. #include "core/MiniSynth.h"
  69. #include "core/SimSynth.h"
  70. #include "core/Granulizer.h"
  71. #include "core/WaveTraveller.h"
  72. #include "core/Drumaxx.h"
  73. #include "core/Wasp.h"
  74. #include "core/PluginLayout.h"
  75. #include "core/Plucked.h"
  76. #include "core/BeepMap.h"
  77. #include "core/Sawer.h"
  78. #include "core/Slayer.h"
  79. #include "core/Toxic.h"
  80. #include "core/Harmless.h"
  81. #include "core/Harmor.h"
  82. #include "core/Morphine.h"
  83. #include "core/Ogun.h"
  84. #include "core/BassDrum.h"
  85. #include "core/FruitKick.h"
  86. #include "core/IppCos.h"
  87. #include "core/Dx10.h"
  88. #include "core/Kepler.h"
  89. #include "core/ThreeOsc.h"
  90. #include "core/FlResampler.h"
  91. #include "core/FlKeys.h"
  92. #include "core/DirectWave.h"
  93. #include "core/OggVorbis.h"
  94. #include "core/FlVoiceProcessor.h"
  95. #include "core/TransistorBass.h"
  96. #include "core/HarmlessFx.h"
  97. #include "core/MaximusFx.h"
  98. #include "core/EmphasisFx.h"
  99. #include "core/VisualFx.h"
  100. #include "core/EmphasizerFx.h"
  101. #include "core/FlLegacyFx.h"
  102. #include "core/FreqShifterFl.h"
  103. #include "core/LowLifterFl.h"
  104. #include "core/Reeverb2.h"
  105. #include "core/LuxeVerb.h"
  106. #include "core/Denormals.h"
  107. #include "core/FruityDynamics.h"
  108. #include "core/FruityShapers.h"
  109. #include "core/ParametricEqFl.h"
  110. #include "core/FruityBasicFx.h"
  111. #include "core/EquoFx.h"
  112. #include "core/DistructorFx.h"
  113. #include "core/MultibandDelayFx.h"
  114. #include "core/PitchShifterFl.h"
  115. #include "core/FruityFilters.h"
  116. #include "core/NewtoneFx.h"
  117. #include "core/FruityDelay.h"
  118. #include "core/StereoFx.h"
  119. #include "core/FrequencySplitter.h"
  120. #include "core/ChorusFx.h"
  121. #include "core/GrossBeat.h"
  122. #include "core/LovePhilter.h"
  123. #include "core/FlangusFastDist.h"
  124. #include "core/FruityPhaser.h"
  125. #include "core/FruityVocoder.h"
  126. #include "core/DelphiMath.h"
  127. #include "core/LiveEventQueue.h"
  128. #include "core/RackRenderer.h"
  129. #include "core/MetronomeVoice.h"
  130. #include "engine/Transport.h"
  131. #include "core/Patcherize.h"
  132. #include "core/Performance.h"
  133. #include "core/Scheduler.h"
  134. #include "core/MarkerPlan.h"
  135. #include "core/SnapshotExchange.h"
  136. #include "core/SoftClip.h"
  137. #include "core/Slicer.h"
  138. #include "core/SytrusEngine.h"
  139. #include "core/Templates.h"
  140. #include "core/Vocodex.h"
  141. #include "core/TimeStretch.h"
  142. #include "core/WorkerPool.h"
  143. #include "core/RtStats.h"
  144. #include "core/FixedBlock.h"
  145. #include "core/ParametricEqLinear.h"
  146. #include "core/Song.h"
  147. #include "core/NoteEdit.h"
  148. #include "core/LiveRecordView.h"
  149. #include "core/Recorder.h"
  150. #include "core/KeyMap.h"
  151. #include "core/UndoKeys.h"
  152. #include "core/SortOnce.h"
  153. #include "core/RackOps.h"
  154. #include "core/PlaylistTrackOps.h"
  155. #include "core/ChannelLoops.h"
  156. #include "core/ChannelBurn.h"
  157. #include "core/AutoSampler.h"
  158. #include "core/ValueMath.h"
  159. #include "core/Workspace.h"
  160. #include "core/StartupSound.h"
  161. #include "core/ExportOptions.h"
  162. #include "core/Mp3Encoder.h"
  163. #include "core/PluginMonitor.h"
  164. #include "core/PluginSubstitutes.h"
  165. #include "core/ClipOps.h"
  166. #include "core/PlaylistOps.h"
  167. #include "core/QuickRender.h"
  168. #include "core/TouchKeyboard.h"
  169. #include "core/HeldNotes.h"
  170. #include "core/Templates.h"
  171. #include "flp/Flp.h"
  172. #include "ui/I18nData.h"
  173. #include "ui/I18nLocale.h"
  174. #include "core/CrashReport.h"
  175. namespace
  176. {
  177. int failures = 0;
  178. int checks = 0;
  179. std::string processTempTag(); // per-process suffix for scratch files (defined further down)
  180. std::vector<std::pair<const char*, std::function<void()>>>& registry()
  181. {
  182. static std::vector<std::pair<const char*, std::function<void()>>> r;
  183. return r;
  184. }
  185. struct Register
  186. {
  187. Register (const char* name, std::function<void()> fn) { registry().emplace_back (name, std::move (fn)); }
  188. };
  189. #define TEST(name) \
  190. static void name(); \
  191. static Register reg_##name (#name, name); \
  192. static void name()
  193. #define CHECK(cond) \
  194. do \
  195. { \
  196. ++checks; \
  197. if (! (cond)) \
  198. { \
  199. ++failures; \
  200. std::printf (" FAILED %s:%d: %s\n", __FILE__, __LINE__, #cond); \
  201. } \
  202. } while (false)
  203. // Engines that compute in x87 80-bit long double (FL's own arithmetic) match their goldens to the last bit on x86. On
  204. // ARM64 (Apple Silicon) long double is not the x87 format, so there they hold to a looser bound (round 117).
  205. // LYNX_X87_TOL overrides it (measuring).
  206. inline float armTolerance (float x86, float arm)
  207. {
  208. #if defined(__x86_64__) || defined(__i386__) || defined(_M_X64) || defined(_M_IX86)
  209. (void) arm;
  210. return x86;
  211. #else
  212. if (const char* t = std::getenv ("LYNX_X87_TOL"))
  213. return (float) std::atof (t);
  214. return arm;
  215. #endif
  216. }
  217. inline float x87Tolerance() { return armTolerance (2.0e-7f, 2.0e-4f); }
  218. inline float granulizerTolerance()
  219. {
  220. #if defined(__APPLE__)
  221. return 2.0e-4f;
  222. #else
  223. return armTolerance (1.0e-6f, 1.0e-5f);
  224. #endif
  225. }
  226. // Engines whose goldens depend on the C library's sin/cos/exp/pow: Windows (mingw), glibc and musl hold the 1e-7
  227. // goldens, Apple's libm does not (round 117), so on macOS the per-sample bound is looser.
  228. inline float libmTolerance (float strict)
  229. {
  230. #if defined(__APPLE__)
  231. (void) strict;
  232. return 2.0e-4f;
  233. #else
  234. return strict;
  235. #endif
  236. }
  237. // One golden sample: relative bound (with a 1e-3 floor on macOS, where near-zero samples carry libm noise); prints the
  238. // measured error when it fails.
  239. inline bool goldenSampleClose (float y, float golden, float tol)
  240. {
  241. #if defined(__APPLE__)
  242. const float scale = std::max (1.0e-3f, std::abs (golden));
  243. #else
  244. const float scale = std::max (1.0e-16f, std::abs (golden));
  245. #endif
  246. if (std::abs (y - golden) <= tol * scale)
  247. return true;
  248. std::fprintf (stderr, " golden sample %.9g vs %.9g (relative error %.3g, bound %.3g)\n", (double) y, (double) golden,
  249. (double) (std::abs (y - golden) / scale), (double) tol);
  250. return false;
  251. }
  252. std::string realFlpDir;
  253. // --- helpers for building synthetic .flp files -----------------------------
  254. struct FlpBuilder
  255. {
  256. std::vector<std::uint8_t> bytes;
  257. FlpBuilder (std::uint16_t ppq = 96)
  258. {
  259. put ("FLhd");
  260. u32 (6);
  261. u16 (0);
  262. u16 (1);
  263. u16 (ppq);
  264. put ("FLdt");
  265. u32 (0); // size is not used by the reader
  266. }
  267. void put (const char* s) { bytes.insert (bytes.end(), s, s + std::strlen (s)); }
  268. void u8 (std::uint8_t v) { bytes.push_back (v); }
  269. void u16 (std::uint16_t v) { u8 ((std::uint8_t) v); u8 ((std::uint8_t) (v >> 8)); }
  270. void u32 (std::uint32_t v) { u16 ((std::uint16_t) v); u16 ((std::uint16_t) (v >> 16)); }
  271. void varint (std::size_t n)
  272. {
  273. do
  274. {
  275. auto b = (std::uint8_t) (n & 0x7F);
  276. n >>= 7;
  277. u8 (n ? (std::uint8_t) (b | 0x80) : b);
  278. } while (n);
  279. }
  280. void data (std::uint8_t id, const std::vector<std::uint8_t>& payload)
  281. {
  282. u8 (id);
  283. varint (payload.size());
  284. bytes.insert (bytes.end(), payload.begin(), payload.end());
  285. }
  286. void ascii (std::uint8_t id, const std::string& s)
  287. {
  288. std::vector<std::uint8_t> p (s.begin(), s.end());
  289. p.push_back (0);
  290. data (id, p);
  291. }
  292. void utf16 (std::uint8_t id, const std::u16string& s)
  293. {
  294. std::vector<std::uint8_t> p;
  295. for (auto c : s)
  296. {
  297. p.push_back ((std::uint8_t) c);
  298. p.push_back ((std::uint8_t) (c >> 8));
  299. }
  300. p.push_back (0);
  301. p.push_back (0);
  302. data (id, p);
  303. }
  304. };
  305. std::vector<std::uint8_t> noteRecord (std::uint32_t pos, std::uint16_t channel, std::uint32_t len, std::uint16_t key,
  306. std::uint8_t velocity)
  307. {
  308. std::vector<std::uint8_t> r (24, 0);
  309. std::memcpy (&r[0], &pos, 4);
  310. std::memcpy (&r[6], &channel, 2);
  311. std::memcpy (&r[8], &len, 4);
  312. std::memcpy (&r[12], &key, 2);
  313. r[16] = 120;
  314. r[20] = 64;
  315. r[21] = velocity;
  316. return r;
  317. }
  318. } // namespace
  319. // ---------------------------------------------------------------------------
  320. // FLP
  321. TEST (flp_rejects_garbage)
  322. {
  323. std::vector<std::uint8_t> junk (64, 0x41);
  324. bool threw = false;
  325. try { flp::EventReader r (junk); } catch (const flp::Error&) { threw = true; }
  326. CHECK (threw);
  327. }
  328. TEST (flp_reads_size_classes_varint_and_extended_ids)
  329. {
  330. FlpBuilder b;
  331. b.u8 (0x15); b.u8 (2); // u8
  332. b.u8 (0x40); b.u16 (513); // u16
  333. b.u8 (0x9C); b.u32 (128500); // u32
  334. b.data (0xD5, std::vector<std::uint8_t> (300, 7)); // 2-byte varint
  335. b.u8 (0xAC); b.u32 (0xC0000100); b.varint (3); b.u8 (1); b.u8 (2); b.u8 (3); // extended ID
  336. flp::EventReader r (b.bytes);
  337. flp::Event e;
  338. CHECK (r.header().ppq == 96);
  339. CHECK (r.next (e) && e.id == 0x15 && e.value == 2);
  340. CHECK (r.next (e) && e.id == 0x40 && e.value == 513);
  341. CHECK (r.next (e) && e.id == 0x9C && e.value == 128500);
  342. CHECK (r.next (e) && e.id == 0xD5 && e.data.size() == 300 && e.data[299] == 7);
  343. CHECK (r.next (e) && e.id == 0xC0000100 && e.isData() && e.data.size() == 3 && e.data[2] == 3);
  344. CHECK (! r.next (e));
  345. }
  346. TEST (flp_truncated_file_throws)
  347. {
  348. FlpBuilder b;
  349. b.u8 (0x9C); b.u8 (1); // u32 with only 1 byte
  350. flp::EventReader r (b.bytes);
  351. flp::Event e;
  352. bool threw = false;
  353. try { r.next (e); } catch (const flp::Error&) { threw = true; }
  354. CHECK (threw);
  355. }
  356. TEST (flp_version_numbers)
  357. {
  358. CHECK (flp::parseVersionNumber ("25.2.5.5319") == 25002005u);
  359. CHECK (flp::parseVersionNumber ("20.99.3000.3031") == 20102000u);
  360. CHECK (flp::parseVersionNumber ("11.0.3") == 11000003u);
  361. }
  362. TEST (flp_playlist_record_size_by_version)
  363. {
  364. CHECK (flp::playlistRecordSize (12009003, 64) == 32);
  365. CHECK (flp::playlistRecordSize (21000000, 120) == 60);
  366. CHECK (flp::playlistRecordSize (25002005, 160) == 80);
  367. CHECK (flp::playlistRecordSize (25002005, 60) == 60); // size doesn't match the version -> tries the others
  368. CHECK (flp::playlistRecordSize (25002005, 7) == 0);
  369. }
  370. TEST (flp_playlist_record_size_by_file_flags)
  371. {
  372. // REA_rodada2 §4.2: 0xA9 bit 1 -> 80 B, bits 1 and 3 -> the native 88 B record (FL 25.2.99 / 26.x)
  373. CHECK (flp::playlistRecordSize (26001003, 88 * 4, 15) == 88); // 352 is also a multiple of 32
  374. CHECK (flp::playlistRecordSize (26001003, 88 * 10, 15) == 88); // ... and of 80
  375. CHECK (flp::playlistRecordSize (25002005, 160, 7) == 80);
  376. CHECK (flp::playlistRecordSize (21000000, 120, 1) == 60);
  377. CHECK (flp::playlistRecordSize (26001003, 63712) == 88); // no flags: the only size that fits besides 32
  378. }
  379. TEST (flp_native_88_byte_playlist_items_with_clip_gain_and_fades)
  380. {
  381. // an FL 26.1 file: 0xA9 = 15, four 88 B items (352 B: the old 32 B guess read garbage from it)
  382. FlpBuilder b (96);
  383. b.ascii (0xC7, "26.1.3.5570");
  384. b.u8 (0xA9); b.u32 (15);
  385. b.u8 (0x9C); b.u32 (130000);
  386. b.u8 (0x40); b.u16 (0);
  387. b.u8 (0x15); b.u8 (4); // audio clip channel
  388. b.utf16 (0xC4, u"C:\\nowhere\\clip.wav");
  389. std::vector<std::uint8_t> items;
  390. auto add = [&] (std::uint32_t pos, std::uint32_t len, std::uint16_t track, float fadeIn, float fadeOut, float gain) {
  391. std::vector<std::uint8_t> r (88, 0);
  392. const std::uint16_t base = 20480, idx = 0, rv = (std::uint16_t) (499 - track);
  393. const float offs[] = { -1.0f, -1.0f };
  394. std::memcpy (&r[0], &pos, 4);
  395. std::memcpy (&r[4], &base, 2);
  396. std::memcpy (&r[6], &idx, 2);
  397. std::memcpy (&r[8], &len, 4);
  398. std::memcpy (&r[12], &rv, 2);
  399. std::memcpy (&r[0x18], offs, 8);
  400. std::memcpy (&r[0x24], &fadeIn, 4);
  401. std::memcpy (&r[0x2C], &fadeOut, 4);
  402. std::memcpy (&r[0x34], &gain, 4);
  403. r[0x38] = (std::uint8_t) ((fadeIn > 0.0f ? 1 : 0) | (fadeOut > 0.0f ? 2 : 0)); // manual fades (round 180d)
  404. const double stretch = 1.0;
  405. const std::int32_t minusOne = -1;
  406. std::memcpy (&r[0x40], &stretch, 8);
  407. std::memcpy (&r[0x4C], &minusOne, 4);
  408. items.insert (items.end(), r.begin(), r.end());
  409. };
  410. add (0, 384, 0, 0.0f, 0.0f, 1.0f);
  411. add (384, 384, 1, 923.0769f, 461.5385f, 1.65f); // 2 beats in, 1 beat out at 130 BPM
  412. add (768, 96, 2, 0.0f, 0.0f, 0.0f);
  413. add (1536, 192, 3, 0.0f, 5000.0f, 0.5f); // fade longer than the clip
  414. b.data (0xE9, items);
  415. const auto p = flp::parseProject (b.bytes);
  416. CHECK (p.flagsA9 == 15);
  417. CHECK (p.arrangements.size() == 1 && p.arrangements[0].items.size() == 4);
  418. CHECK (p.warnings.empty());
  419. if (p.arrangements.size() == 1 && p.arrangements[0].items.size() == 4)
  420. {
  421. const auto& it = p.arrangements[0].items;
  422. CHECK (it[1].position == 384 && it[1].length == 384 && it[1].track == 1 && it[1].kind == flp::PlaylistItem::Kind::channelClip);
  423. CHECK (it[3].position == 1536 && it[3].track == 3);
  424. CHECK (std::abs (it[1].gain - 1.65f) < 1e-6f && std::abs (it[1].fadeInMs - 923.0769f) < 1e-3f);
  425. const auto song = lynx::importFlp (p);
  426. CHECK (song.playlist.size() == 4);
  427. if (song.playlist.size() == 4)
  428. {
  429. const auto beat = (std::int64_t) lynx::ppq;
  430. CHECK (song.playlist[0].gain == 1.0f && song.playlist[0].fadeIn == 0 && song.playlist[0].fadeOut == 0);
  431. CHECK (std::abs (song.playlist[1].gain - 1.65f) < 1e-6f);
  432. CHECK (std::llabs (song.playlist[1].fadeIn - 2 * beat) <= 1 && std::llabs (song.playlist[1].fadeOut - beat) <= 1);
  433. CHECK (song.playlist[2].gain == 0.0f);
  434. CHECK (song.playlist[3].fadeOut == song.playlist[3].length); // clamped
  435. }
  436. }
  437. }
  438. TEST (flp_channel_level_adjustments_and_stretched_clip_start)
  439. {
  440. // FL 26.1 audio clip channel (gf92 follow-up): 0xE5 level adjustments and a time-stretched clip whose item
  441. // start/end are in the sample's milliseconds ("Liwn Passar voz" ch5: 788 ms into a clip stretched by 1.0569)
  442. FlpBuilder b (96);
  443. b.ascii (0xC7, "26.1.3.5570");
  444. b.u8 (0xA9); b.u32 (15);
  445. b.u8 (0x9C); b.u32 (130000);
  446. b.u8 (0x40); b.u16 (0);
  447. b.u8 (0x15); b.u8 (4); // audio clip channel
  448. b.utf16 (0xC4, u"C:\\nowhere\\clip.wav");
  449. {
  450. std::vector<std::uint8_t> levels (24, 0);
  451. const std::int32_t pan = 6400, vol = 10000;
  452. std::memcpy (&levels[0], &pan, 4);
  453. std::memcpy (&levels[4], &vol, 4);
  454. b.data (0xDB, levels);
  455. std::vector<std::uint8_t> adjust (20, 0);
  456. const std::int32_t panAdj = 3200, volAdj = 7286;
  457. std::memcpy (&adjust[0], &panAdj, 4);
  458. std::memcpy (&adjust[4], &volAdj, 4);
  459. b.data (0xE5, adjust);
  460. std::vector<std::uint8_t> params (0x98, 0);
  461. const float time = 64.0f * 192.0f; // stretched to 64 beats (float: 0xA9 bit 0)
  462. const double end = 1.0;
  463. std::memcpy (&params[0x60], &time, 4);
  464. std::memcpy (&params[0x88], &end, 8);
  465. b.data (0xD7, params);
  466. }
  467. {
  468. std::vector<std::uint8_t> r (88, 0);
  469. const std::uint32_t pos = 0, len = 6144; // 64 beats
  470. const std::uint16_t base = 20480, idx = 0, rv = 499;
  471. const float offs[] = { 788.4615f, 788.4615f + 31218.92f }; // sample ms: 64 beats at 130 BPM x 1.056892
  472. const float gain = 1.0f;
  473. std::memcpy (&r[0], &pos, 4);
  474. std::memcpy (&r[4], &base, 2);
  475. std::memcpy (&r[6], &idx, 2);
  476. std::memcpy (&r[8], &len, 4);
  477. std::memcpy (&r[12], &rv, 2);
  478. std::memcpy (&r[0x18], offs, 8);
  479. std::memcpy (&r[0x34], &gain, 4);
  480. b.data (0xE9, r);
  481. }
  482. const auto p = flp::parseProject (b.bytes);
  483. CHECK (p.channels.size() == 1);
  484. if (p.channels.size() != 1)
  485. return;
  486. CHECK (p.channels[0].hasLevelAdjust && p.channels[0].panAdj == 3200 && p.channels[0].volAdj == 7286);
  487. const auto song = lynx::importFlp (p);
  488. CHECK (song.channels.size() == 1 && song.playlist.size() == 1);
  489. if (song.channels.size() != 1 || song.playlist.size() != 1)
  490. return;
  491. // gain = f(volume)·f(volAdj), pan = pan + panAdj
  492. const auto expected = lynx::channelGain (10000.0f / 12800.0f) * lynx::channelGain (7286.0f / 12800.0f);
  493. CHECK (std::abs (lynx::channelGain (song.channels[0].volume) - expected) < 1e-5f);
  494. CHECK (std::abs (song.channels[0].pan - 0.5f) < 1e-6f);
  495. // 788.46 sample ms / 1.056892 = 746.0 ms = 1.6164 beats into the clip
  496. CHECK (song.channels[0].sampler.stretchBeats > 0.0f);
  497. CHECK (std::llabs (song.playlist[0].offset - (std::int64_t) std::llround (1.61638 * lynx::ppq)) <= 1);
  498. // REA_rodada110 §1: the sub-tick rest is kept (offset + fraction = the exact position, to the sample)
  499. const auto exact = 788.4615 / ((double) (788.4615f + 31218.92f - 788.4615f) / (64.0 * 60000.0 / 130.0)) / 1000.0 * 130.0 / 60.0 * lynx::ppq;
  500. CHECK (std::abs ((double) song.playlist[0].offset + (double) song.playlist[0].offsetFraction - exact) < 1e-3);
  501. CHECK (std::abs (song.playlist[0].offsetFraction) <= 0.5f);
  502. }
  503. TEST (flp_parses_project_model)
  504. {
  505. FlpBuilder b (96);
  506. b.ascii (0xC7, "25.2.5.5319");
  507. b.u8 (0x9C); b.u32 (174500);
  508. b.utf16 (0xC2, u"Título");
  509. b.u8 (0x41); b.u16 (3);
  510. b.u8 (0x96); b.u32 (0x00A14547); // BGR in the file
  511. auto notes = noteRecord (96, 1, 48, 64, 100);
  512. auto n2 = noteRecord (0, 0, 0, 60, 128);
  513. notes.insert (notes.end(), n2.begin(), n2.end());
  514. b.data (0xE0, notes);
  515. b.u8 (0x40); b.u16 (0);
  516. b.u8 (0x15); b.u8 (0);
  517. b.utf16 (0xCB, u"Kick");
  518. {
  519. std::vector<std::uint8_t> levels (24, 0);
  520. const std::int32_t lv[] = { 3200, 12800, -150 };
  521. std::memcpy (levels.data(), lv, sizeof (lv));
  522. b.data (0xDB, levels);
  523. }
  524. b.u8 (0x68); b.u16 (5);
  525. b.u8 (0x40); b.u16 (1);
  526. b.u8 (0x15); b.u8 (2);
  527. b.utf16 (0xC9, u"FLEX");
  528. b.utf16 (0xCB, u"Lead");
  529. b.u8 (0x63); b.u16 (0);
  530. std::vector<std::uint8_t> item (80, 0);
  531. std::uint32_t pos = 384, len = 768;
  532. std::uint16_t base = 20480, idx = 20480 + 3, rv = 499 - 2;
  533. std::memcpy (&item[0], &pos, 4);
  534. std::memcpy (&item[4], &base, 2);
  535. std::memcpy (&item[6], &idx, 2);
  536. std::memcpy (&item[8], &len, 4);
  537. std::memcpy (&item[12], &rv, 2);
  538. b.data (0xE9, item);
  539. b.utf16 (0xCC, u"Drums");
  540. b.data (0xEC, std::vector<std::uint8_t> (12, 0));
  541. b.data (0xEC, std::vector<std::uint8_t> (12, 0));
  542. const auto p = flp::parseProject (b.bytes);
  543. CHECK (p.version == "25.2.5.5319");
  544. CHECK (std::abs (p.tempo - 174.5) < 1e-9);
  545. CHECK (p.title == "Título");
  546. CHECK (p.channels.size() == 2);
  547. CHECK (p.channels[0].name == "Kick" && p.channels[0].mixerTrack == 5);
  548. CHECK (p.channels[0].hasLevels && p.channels[0].pan == 3200 && p.channels[0].volume == 12800 && p.channels[0].pitchCents == -150);
  549. CHECK (p.channels[1].type == flp::ChannelType::instrument && p.channels[1].pluginName == "FLEX");
  550. CHECK (p.patterns.size() == 1 && p.patterns[0].index == 3);
  551. CHECK (p.patterns[0].colour && *p.patterns[0].colour == 0x4745A1);
  552. CHECK (p.patterns[0].notes.size() == 2 && p.patterns[0].notes[0].position == 0); // sorted
  553. CHECK (p.patterns[0].notes[1].key == 64 && p.patterns[0].notes[1].length == 48);
  554. CHECK (p.arrangements.size() == 1 && p.arrangements[0].items.size() == 1);
  555. const auto& it = p.arrangements[0].items[0];
  556. CHECK (it.kind == flp::PlaylistItem::Kind::pattern && it.index == 3 && it.track == 2 && it.position == 384);
  557. CHECK (p.mixer.size() == 2 && p.mixer[0].name == "Drums" && p.mixer[1].name.empty());
  558. CHECK (p.warnings.empty());
  559. }
  560. TEST (flp_fuzz_corrupted_files_do_not_crash)
  561. {
  562. if (realFlpDir.empty())
  563. return;
  564. // some real .flp files, corrupted in various ways: the parser may reject them (flp::Error), but
  565. // must never hang, read past the buffer or produce a Song with invalid indices
  566. std::vector<std::vector<std::uint8_t>> sources;
  567. for (auto& entry : std::filesystem::recursive_directory_iterator (realFlpDir))
  568. if (entry.path().extension() == ".flp" && sources.size() < 6 && std::filesystem::file_size (entry.path()) < 3'000'000)
  569. {
  570. std::ifstream in (entry.path(), std::ios::binary);
  571. sources.emplace_back ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  572. }
  573. std::uint32_t seed = 2024;
  574. auto rnd = [&] (std::uint32_t n) { seed = seed * 1664525u + 1013904223u; return n == 0 ? 0u : (seed >> 8) % n; };
  575. int parsed = 0, rejected = 0;
  576. for (int round = 0; round < 240 && ! sources.empty(); ++round)
  577. {
  578. auto bytes = sources[(std::size_t) rnd ((std::uint32_t) sources.size())];
  579. switch (round % 4)
  580. {
  581. case 0: bytes.resize (rnd ((std::uint32_t) bytes.size())); break; // truncated
  582. 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;
  583. case 2: // absurd event size
  584. for (int k = 0; k < 5; ++k)
  585. bytes[(std::size_t) rnd ((std::uint32_t) bytes.size())] = 0xFF;
  586. break;
  587. default: // shuffled block
  588. {
  589. const auto a = rnd ((std::uint32_t) bytes.size()), b = rnd ((std::uint32_t) bytes.size());
  590. std::reverse (bytes.begin() + std::min (a, b), bytes.begin() + std::max (a, b));
  591. break;
  592. }
  593. }
  594. try
  595. {
  596. const auto project = flp::parseProject (bytes);
  597. const auto song = lynx::importFlp (project);
  598. ++parsed;
  599. for (const auto& p : song.patterns)
  600. for (const auto& n : p.notes)
  601. CHECK (n.channel >= 0 && n.channel < (int) song.channels.size());
  602. for (const auto& c : song.playlist)
  603. CHECK (c.kind != lynx::ClipKind::pattern || (c.pattern >= 0 && c.pattern < (int) song.patterns.size()));
  604. }
  605. catch (const flp::Error&)
  606. {
  607. ++rejected;
  608. }
  609. }
  610. std::printf (" fuzz: %d imported, %d rejected\n", parsed, rejected);
  611. CHECK (parsed + rejected == 240);
  612. }
  613. TEST (plugin_state_decoders_survive_garbage)
  614. {
  615. // random blobs (varied sizes, including valid headers followed by garbage)
  616. std::uint32_t seed = 77;
  617. auto rnd = [&] { seed = seed * 1664525u + 1013904223u; return seed >> 8; };
  618. const char* plugins[] = { "Fruity Parametric EQ 2", "Fruity Limiter", "Fruity Reeverb 2", "Fruity Delay 2", "Fruity Delay 3",
  619. "Fruity Compressor", "Fruity Balance", "Fruity PanOMatic", "Fruity Stereo Shaper", "Fruity Phase Inverter" };
  620. int decoded = 0;
  621. for (int round = 0; round < 3000; ++round)
  622. {
  623. std::vector<std::uint8_t> blob ((std::size_t) (rnd() % 600));
  624. for (auto& b : blob)
  625. b = (std::uint8_t) rnd();
  626. if (blob.size() >= 4 && round % 3 == 0) // versions the readers accept
  627. {
  628. const std::uint32_t versions[] = { 8, 10001, 10000, 12, 14, 6, 1 };
  629. const auto v = versions[rnd() % 7];
  630. std::memcpy (blob.data(), &v, 4);
  631. }
  632. if (auto fx = lynx::effectFromFl (plugins[rnd() % 10], blob))
  633. {
  634. ++decoded;
  635. for (auto v : fx->params)
  636. CHECK (std::isfinite (v) && v >= 0.0f && v <= 1.0f);
  637. }
  638. lynx::SynthSettings synth;
  639. if (lynx::synthFrom3xOsc (blob, synth))
  640. for (const auto& o : synth.osc)
  641. CHECK (std::isfinite (o.level) && o.level >= 0.0f && o.level <= 1.0f);
  642. float harmlessGain = 1.0f;
  643. if (lynx::synthFromHarmless (blob, 120.0, synth, harmlessGain))
  644. CHECK (std::isfinite (harmlessGain) && std::isfinite (synth.cutoff) && std::isfinite (synth.attack));
  645. if (lynx::synthFromFruitKick (blob, synth))
  646. CHECK (std::isfinite (synth.decay) && std::isfinite (synth.pitchEnv.amount));
  647. if (lynx::synthFromGms (blob, 120.0, synth, harmlessGain) || lynx::synthFromHarmor (blob, synth, harmlessGain) || lynx::synthFromOgun (blob, synth, harmlessGain)
  648. || lynx::synthFromSakura (blob, synth) || lynx::synthFromKepler (blob, synth, harmlessGain))
  649. CHECK (std::isfinite (synth.cutoff) && std::isfinite (synth.attack) && std::isfinite (harmlessGain));
  650. if (auto sound = lynx::drumSynthLiveSound (blob))
  651. CHECK (sound->size() < 256);
  652. if (lynx::wavetrav::State wt; lynx::wavetrav::parseState (blob, wt))
  653. CHECK (wt.patches[60].waveN >= 0 && wt.patches[60].waveN < lynx::wavetrav::tableCapacity);
  654. lynx::synthFromBooBass (blob, synth);
  655. lynx::gmsStateUsable (blob); // (the exact engines' parsers reject garbage without crashing)
  656. lynx::morphineStateUsable (blob);
  657. lynx::poizoneStateUsable (blob);
  658. if (lynx::synthFromPoiZone (blob, synth, harmlessGain) || lynx::synthFromBassDrum (blob, synth, harmlessGain) || lynx::synthFromToxic (blob, synth, harmlessGain)
  659. || lynx::synthFromMorphine (blob, synth, harmlessGain))
  660. CHECK (std::isfinite (synth.cutoff) && std::isfinite (harmlessGain));
  661. if (auto patch = lynx::drumpadPatchName (blob))
  662. CHECK (patch->size() <= 0x30 && std::isfinite (lynx::drumToneForName (*patch).decay));
  663. if (auto flex = lynx::flexPresetName (blob))
  664. CHECK ((! flex->first.empty() || ! flex->second.empty()) && flex->second.size() <= 1024);
  665. if (lynx::synthFromSytrus (blob, 120.0, synth, harmlessGain))
  666. CHECK (std::isfinite (synth.cutoff) && std::isfinite (synth.attack));
  667. if (lynx::synthFromDX10 (blob, synth))
  668. CHECK (std::isfinite (synth.decay) && std::isfinite (synth.release) && std::isfinite (synth.osc[1].level));
  669. if (blob.size() >= 4 && round % 5 == 0) // MiniSynth header followed by garbage
  670. {
  671. std::memcpy (blob.data(), "SSLF", 4);
  672. if (blob.size() > 220)
  673. {
  674. std::memcpy (blob.data() + 40, "DATA", 4);
  675. std::memcpy (blob.data() + 48, "0rev", 4);
  676. }
  677. }
  678. if (lynx::synthFromMiniSynth (blob, 120.0, synth))
  679. CHECK (std::isfinite (synth.cutoff) && std::isfinite (synth.decay) && std::isfinite (synth.attack));
  680. if (lynx::miniSynthStateUsable (blob)) // the exact engine's parser and a short render with silent tables
  681. {
  682. const auto params = lynx::miniSynthParams (blob);
  683. CHECK (params.size() == 43u && std::all_of (params.begin(), params.end(), [] (float v) { return v >= 0.0f && v <= 1.0f; }));
  684. const auto y = lynx::minisynth::renderReference (blob, { { 0, 60, 1.0, 1.0, 0.0, 0.0, 64 } }, 44100.0, 128, "");
  685. CHECK (y.size() == 256u && std::all_of (y.begin(), y.end(), [] (float v) { return std::isfinite (v); }));
  686. }
  687. lynx::ForeignPlugin foreign;
  688. lynx::parseFlWrapper (blob, foreign);
  689. std::vector<std::uint8_t> component, controller;
  690. lynx::splitFlVst3State (blob, component, controller);
  691. }
  692. CHECK (decoded == 3000); // every known name yields an effect (default when the blob is unusable)
  693. }
  694. TEST (inflate_zlib)
  695. {
  696. // fixed Huffman with back-references
  697. const std::uint8_t fixed[] = { 0x78, 0xda, 0x4b, 0x4c, 0x4a, 0x4e, 0xc4, 0x86, 0x22, 0x00, 0x7c, 0x69, 0x09, 0x89 };
  698. const auto a = lynx::inflateZlib (fixed, sizeof (fixed));
  699. CHECK (a && std::string (a->begin(), a->end()) == "abcabcabcabcabcabcabcabcX");
  700. // stored (uncompressed) block
  701. const std::uint8_t stored[] = { 0x78, 0x01, 0x01, 0x03, 0x00, 0xfc, 0xff, 'L', 'Y', 'N', 0x00, 0x00, 0x00, 0x00 };
  702. const auto b = lynx::inflateZlib (stored, sizeof (stored));
  703. CHECK (b && std::string (b->begin(), b->end()) == "LYN");
  704. // truncated, bad header and size limit
  705. CHECK (! lynx::inflateZlib (fixed, 6));
  706. const std::uint8_t bad[] = { 0x12, 0x34, 0x00 };
  707. CHECK (! lynx::inflateZlib (bad, sizeof (bad)));
  708. CHECK (! lynx::inflateZlib (fixed, sizeof (fixed), false, 10));
  709. }
  710. TEST (fl_round15_effects_convert_known_values)
  711. {
  712. auto blob = [] (std::vector<std::int32_t> words) {
  713. std::vector<std::uint8_t> b (words.size() * 4);
  714. std::memcpy (b.data(), words.data(), b.size());
  715. return b;
  716. };
  717. // Frequency Shifter "+800 Hz": frequency 51542 in the 20 kHz range, 100% wet — FL's own engine since round 154
  718. // (its controls as (raw − min) / range); unreadable states keep the generic shifter
  719. const auto fs = lynx::effectFromFl ("Frequency Shifter", blob ({ 4, 10000, 0, 51542, 0, 10000, 10000, 5000, 10000, 0, 5000, 0, 0 }));
  720. CHECK (fs && fs->kind == lynx::EffectKind::flFrequencyShifter);
  721. if (fs)
  722. {
  723. CHECK (std::abs (fs->params[0] - 1.0f) < 1e-6f && fs->params[1] == 0.0f);
  724. CHECK (std::abs (fs->params[2] - (51542.0f + 70000.0f) / 140000.0f) < 1e-6f);
  725. }
  726. const auto fsOld = lynx::effectFromFl ("Frequency Shifter", blob ({ 4, 10000, 0 }));
  727. CHECK (fsOld && fsOld->kind == lynx::EffectKind::freqShift);
  728. // Emphasis (Bien Duro's Master): FL's own engine since round 167f, its controls as (raw − min) / range: input 625
  729. // (+1.5 dB), ceiling 9308 (−1.60 dB), envelope order 2, true peak; an unreadable state keeps the limiter
  730. const auto em = lynx::effectFromFl ("Emphasis", blob ({ 8, 0, 0, 2, 1, 0, 0, 625, 10000, 10000, 9308, 0, 2, 7, 0 }));
  731. CHECK (em && em->kind == lynx::EffectKind::flEmphasis && em->enabled);
  732. if (em)
  733. {
  734. CHECK (std::abs (em->params[6] - 0.0625f) < 1e-6f && std::abs (em->params[9] - 0.9308f) < 1e-6f);
  735. CHECK (std::abs (em->params[2] - 1.0f / 3.0f) < 1e-6f && em->params[3] == 1.0f && std::abs (em->params[11] - 0.5f) < 1e-6f);
  736. CHECK (lynx::formatEffectParam (lynx::EffectKind::flEmphasis, 6, em->params[6], 120.0) == "+1.5 dB");
  737. CHECK (lynx::formatEffectParam (lynx::EffectKind::flEmphasis, 9, em->params[9], 120.0) == "-1.60 dB");
  738. CHECK (em->params[14] == 0.0f && em->params[15] == 0.0f); // a version-8 state has no option bytes: off
  739. }
  740. const auto emOld = lynx::effectFromFl ("Emphasis", blob ({ 8, 0, 0, 2 }));
  741. CHECK (emOld && emOld->kind == lynx::EffectKind::limiter);
  742. // round 167f: FL's legacy dB Meter / Spectroman keep their slot as LYNX's analyser in the matching view
  743. const auto dbm = lynx::effectFromFl ("Fruity dB Meter", {});
  744. CHECK (dbm && dbm->kind == lynx::EffectKind::waveCandy
  745. && dbm->params[0] == lynx::visualfx::wavecandy::modeValue (lynx::visualfx::wavecandy::Mode::peakMeter));
  746. const auto spm = lynx::effectFromFl ("Fruity Spectroman", {});
  747. CHECK (spm && spm->kind == lynx::EffectKind::waveCandy
  748. && spm->params[0] == lynx::visualfx::wavecandy::modeValue (lynx::visualfx::wavecandy::Mode::spectrum));
  749. // Pitch Shifter +12 st, mix 100%
  750. 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 }));
  751. CHECK (ps && ps->kind == lynx::EffectKind::flPitchShifter && ps->params[0] == 1.0f && ps->params[3] == 1.0f); // REA_rodada61
  752. }
  753. TEST (frequency_shifter_moves_spectrum_by_hz)
  754. {
  755. auto fx = lynx::makeEffect (lynx::EffectKind::freqShift);
  756. fx->prepare (48000.0);
  757. auto slot = lynx::makeEffectSlot (lynx::EffectKind::freqShift);
  758. // +200 Hz: x·|x|·2000 = 200 -> x = sqrt(0.1)
  759. slot.params[0] = (float) (0.5 + std::sqrt (0.1) / 2.0);
  760. std::vector<float> l (48000), r (48000);
  761. for (int i = 0; i < 48000; ++i)
  762. l[(std::size_t) i] = r[(std::size_t) i] = (float) std::sin (2.0 * 3.141592653589793 * 1000.0 * i / 48000.0);
  763. for (int done = 0; done < 48000; done += 480)
  764. fx->process (l.data() + done, r.data() + done, 480, slot.params, 120.0);
  765. auto energy = [&] (double f) {
  766. const auto w = 2.0 * 3.141592653589793 * f / 48000.0, k = 2.0 * std::cos (w);
  767. double s1 = 0.0, s2 = 0.0;
  768. for (int i = 9600; i < 48000; ++i)
  769. {
  770. const auto s0 = l[(std::size_t) i] + k * s1 - s2;
  771. s2 = s1;
  772. s1 = s0;
  773. }
  774. return s1 * s1 + s2 * s2 - k * s1 * s2;
  775. };
  776. // the energy moves to 1200 Hz; little remains at 1000 and almost nothing at the image (800)
  777. CHECK (energy (1200.0) > 50.0 * energy (1000.0));
  778. CHECK (energy (1200.0) > 50.0 * energy (800.0));
  779. }
  780. TEST (pitch_shift_moves_frequency_keeps_length)
  781. {
  782. auto fx = lynx::makeEffect (lynx::EffectKind::pitchShift);
  783. fx->prepare (48000.0);
  784. auto slot = lynx::makeEffectSlot (lynx::EffectKind::pitchShift);
  785. auto crossingsAfter = [&] (float semitoneParam) {
  786. fx->reset();
  787. slot.params[0] = semitoneParam;
  788. std::vector<float> l (48000), r (48000);
  789. for (int i = 0; i < 48000; ++i)
  790. l[(std::size_t) i] = r[(std::size_t) i] = (float) std::sin (2.0 * 3.141592653589793 * 220.0 * i / 48000.0);
  791. for (int done = 0; done < 48000; done += 512)
  792. fx->process (l.data() + done, r.data() + done, std::min (512, 48000 - done), slot.params, 120.0);
  793. int crossings = 0;
  794. for (int i = 9600; i < 48000; ++i)
  795. crossings += (l[(std::size_t) i - 1] < 0.0f) != (l[(std::size_t) i] < 0.0f);
  796. for (auto v : l)
  797. CHECK (std::isfinite (v) && std::abs (v) < 1.6f);
  798. return crossings / 0.8 / 2.0; // cycles per second
  799. };
  800. CHECK (std::abs (crossingsAfter (0.5f) - 220.0) < 8.0); // 0 st
  801. CHECK (std::abs (crossingsAfter (0.5f + 12.0f / 48.0f) - 440.0) < 25.0); // +12 st
  802. CHECK (std::abs (crossingsAfter (0.5f - 12.0f / 48.0f) - 110.0) < 12.0); // −12 st
  803. }
  804. // A Gross Beat state in FL's layout (REA_rodada53 §1), built here: version 8, parameters, options, 72 slots with a name,
  805. // sync bytes and a map envelope (kind 3, version 3, points: Δx in beats, level, tension, curve type)
  806. namespace
  807. {
  808. struct GbPoint
  809. {
  810. double dx, y;
  811. std::uint8_t type;
  812. };
  813. std::vector<std::uint8_t> grossBeatState (const std::array<int, 9>& params, const std::array<std::uint8_t, 9>& options,
  814. const std::map<int, std::vector<GbPoint>>& slots)
  815. {
  816. std::vector<std::uint8_t> b;
  817. 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); };
  818. auto i32 = [&put] (std::int32_t v) { put (&v, 4); };
  819. i32 (0x00080003);
  820. for (auto v : params)
  821. i32 (v);
  822. put (options.data(), options.size());
  823. for (int k = 0; k < 72; ++k)
  824. {
  825. b.push_back (0); // empty name
  826. i32 (0), i32 (5), i32 (0); // trigger sync off, position sync 4 beats, no hold
  827. b.push_back (0), b.push_back (0), b.push_back (0);
  828. const auto it = slots.find (k);
  829. const std::vector<GbPoint> flat { { 0.0, 1.0, 0 }, { 4.0, 1.0, 0 } };
  830. const auto& pts = it != slots.end() ? it->second : flat;
  831. i32 (3), i32 (3), i32 ((std::int32_t) pts.size());
  832. for (const auto& p : pts)
  833. {
  834. put (&p.dx, 8), put (&p.y, 8);
  835. const float tension = 0.0f;
  836. put (&tension, 4);
  837. b.push_back (p.type), b.push_back (0), b.push_back (0), b.push_back (2);
  838. }
  839. i32 (0), i32 (-1), i32 (-1), i32 (-1), i32 (-1);
  840. }
  841. return b;
  842. }
  843. std::vector<float> renderGrossBeat (const lynx::EffectSlot& slot, const std::vector<float>& in, double rate, double bpm)
  844. {
  845. auto fx = lynx::makeEffect (slot);
  846. fx->prepare (rate);
  847. auto l = in, r = in;
  848. const auto tps = bpm / 60.0 * lynx::ppq / rate;
  849. for (std::size_t done = 0; done < l.size(); done += 512)
  850. {
  851. const auto n = (int) std::min<std::size_t> (512, l.size() - done);
  852. fx->setSongPosition ((double) done * tps);
  853. fx->process (l.data() + done, r.data() + done, n, slot.params, bpm);
  854. }
  855. return l;
  856. }
  857. } // namespace
  858. TEST (gross_beat_state_container_and_parameters)
  859. {
  860. const auto state = grossBeatState ({ 3, 40, 128, 256, 1765, 5000, -77, 0, 0x10000 }, { 0, 0, 0, 0, 0, 0, 0, 1, 1 }, {});
  861. const auto parsed = lynx::grossbeat::parseState (state.data(), state.size());
  862. CHECK (parsed.has_value());
  863. CHECK (parsed && parsed->version == 8 && parsed->params[6] == -77 && parsed->slots[0].flat && parsed->slots[71].flat);
  864. const auto slot = lynx::grossbeat::slotFromFl (state, 960);
  865. CHECK (slot && slot->kind == lynx::EffectKind::grossBeat);
  866. std::vector<std::uint8_t> fl;
  867. int ppq = 0;
  868. CHECK (slot && lynx::grossbeat::unpack (slot->state, fl, ppq) && fl == state && ppq == 960);
  869. // every raw FL value survives the 0..1 parameter
  870. const int lo[8] { 0, 0, 0, 0, 0, 0, -256, 0 }, hi[8] { 35, 35, 256, 256, 10000, 10000, 256, 1 };
  871. bool roundTrip = true;
  872. for (int i = 0; i < 8; ++i)
  873. for (int raw = lo[i]; raw <= hi[i]; ++raw)
  874. roundTrip = roundTrip && lynx::grossbeat::rawFromParam (i, lynx::grossbeat::paramFromRaw (i, raw)) == raw;
  875. CHECK (roundTrip);
  876. CHECK (slot && slot->params[1] == lynx::grossbeat::paramFromRaw (1, 35)); // the volume slot 40 is clamped to 35
  877. CHECK (! lynx::grossbeat::parseState (state.data(), 100).has_value());
  878. }
  879. TEST (gross_beat_volume_slot_gates_the_bar)
  880. {
  881. // volume slot 1: open for two beats, closed for two (hold segments); no smoothing; dry path 15 samples late
  882. 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 } };
  883. const auto state = grossBeatState ({ 0, 1, 256, 256, 0, 0, 0, 0, 0x10000 }, { 0, 0, 0, 0, 0, 0, 0, 1, 1 }, { { 37, gate } });
  884. const auto slot = lynx::grossbeat::slotFromFl (state);
  885. CHECK (slot.has_value());
  886. if (! slot)
  887. return;
  888. const auto out = renderGrossBeat (*slot, std::vector<float> (200000, 1.0f), 48000.0, 120.0); // a beat = 24000 samples
  889. CHECK (out[14] == 0.0f && out[15] == 1.0f);
  890. CHECK (out[30000] == 1.0f && out[47990] == 1.0f);
  891. CHECK (out[48010] == 0.0f && out[95990] == 0.0f);
  892. CHECK (out[96010] == 1.0f && out[150000] == 0.0f);
  893. }
  894. TEST (gross_beat_time_slot_reads_the_past_bar)
  895. {
  896. // time slot 1 at level 0.5: the buffer is read one bar back (two bars = level 0), plus the 15-sample latency
  897. const std::vector<GbPoint> half { { 0.0, 0.5, 0 }, { 4.0, 0.5, 2 } };
  898. const auto state = grossBeatState ({ 1, 0, 256, 256, 0, 0, 0, 0, 0x10000 }, { 0, 0, 0, 0, 0, 0, 0, 1, 1 }, { { 1, half } });
  899. const auto slot = lynx::grossbeat::slotFromFl (state);
  900. CHECK (slot.has_value());
  901. if (! slot)
  902. return;
  903. std::vector<float> in (150000, 0.0f);
  904. in[1000] = 1.0f;
  905. const auto out = renderGrossBeat (*slot, in, 48000.0, 120.0); // a bar = 96000 samples
  906. std::size_t peak = 0;
  907. for (std::size_t i = 0; i < out.size(); ++i)
  908. if (std::abs (out[i]) > std::abs (out[peak]))
  909. peak = i;
  910. CHECK (peak == 1000 + 96000 + 15);
  911. CHECK (std::abs (out[peak] - 1.0f) < 1e-6f);
  912. float elsewhere = 0.0f;
  913. for (std::size_t i = 0; i < out.size(); ++i)
  914. if (i + 20 < peak || i > peak + 20)
  915. elsewhere = std::max (elsewhere, std::abs (out[i]));
  916. CHECK (elsewhere == 0.0f);
  917. }
  918. TEST (volume_gate_follows_bar_position)
  919. {
  920. auto fx = lynx::makeEffect (lynx::EffectKind::volumeGate);
  921. fx->prepare (48000.0);
  922. auto slot = lynx::makeEffectSlot (lynx::EffectKind::volumeGate);
  923. for (int i = 0; i < 32; ++i)
  924. slot.params[(std::size_t) i] = i < 16 ? 1.0f : 0.0f; // first two beats open, two closed
  925. slot.params[33] = slot.params[34] = 0.0f; // no smoothing
  926. // 120 BPM: one beat = 0.5 s = 24000 samples; the 4-beat bar starts at tick 0
  927. std::vector<float> l (96000, 1.0f), r (96000, 1.0f);
  928. fx->setSongPosition (0.0);
  929. for (int done = 0; done < 96000; done += 512)
  930. fx->process (l.data() + done, r.data() + done, std::min (512, 96000 - done), slot.params, 120.0);
  931. CHECK (l[10000] > 0.99f && l[40000] > 0.99f); // beats 1 and 2
  932. CHECK (l[60000] < 0.01f && l[90000] < 0.01f); // beats 3 and 4
  933. // starting mid-bar (beat 3): already closed
  934. std::vector<float> l2 (4800, 1.0f), r2 (4800, 1.0f);
  935. fx->reset();
  936. fx->setSongPosition (2.0 * lynx::ppq);
  937. fx->process (l2.data(), r2.data(), 4800, slot.params, 120.0);
  938. CHECK (l2[4000] < 0.01f);
  939. // mix 0: transparent
  940. slot.params[32] = 0.0f;
  941. std::vector<float> l3 (4800, 1.0f), r3 (4800, 1.0f);
  942. fx->setSongPosition (2.0 * lynx::ppq);
  943. fx->process (l3.data(), r3.data(), 4800, slot.params, 120.0);
  944. CHECK (l3[4000] > 0.99f);
  945. }
  946. TEST (patcher_branch_heads_follow_rea)
  947. {
  948. auto words = [] (std::vector<std::int32_t> v) {
  949. std::vector<std::uint8_t> b (v.size() * 4);
  950. std::memcpy (b.data(), v.data(), b.size());
  951. return b;
  952. };
  953. // Frequency Splitter, 2 bands, destinations 1/2/3, slope combo 3 (24 dB/oct), high send −6 dB
  954. std::vector<std::int32_t> fs (18, 0);
  955. fs[0] = 4;
  956. fs[1] = 32768;
  957. fs[3] = 3;
  958. fs[6] = 0;
  959. fs[11] = -600;
  960. fs[15] = 1;
  961. fs[16] = 2;
  962. fs[17] = 3;
  963. const auto split = words (fs);
  964. const auto low = lynx::patcherBranchHead ("Frequency Splitter", split, 1);
  965. CHECK (low.valid && low.slot && low.note.empty());
  966. if (low.slot)
  967. {
  968. // the exact engine (REA_rodada62): the low band's send, 24 dB/oct
  969. CHECK (low.slot->kind == lynx::EffectKind::frequencySplitter && low.slot->params[26] == 1.0f && low.slot->params[27] == 0.0f);
  970. CHECK (low.slot->params[2] == 3.0f / 7.0f);
  971. }
  972. const auto mid = lynx::patcherBranchHead ("Frequency Splitter", split, 2);
  973. 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
  974. const auto high = lynx::patcherBranchHead ("Frequency Splitter", split, 3);
  975. CHECK (high.valid && high.slot && high.slot->params[28] == 1.0f && high.slot->params[10] == (float) (1200.0 / 3600.0));
  976. CHECK (! lynx::patcherBranchHead ("Frequency Splitter", split, 7).valid);
  977. // Stereo Shaper: the difference destination (4) is the exact engine with its difference output (REA_rodada54)
  978. const auto shaper = words ({ 3, 0, 12800, 12800, 0, 0, 0, 4, 0 });
  979. const auto diff = lynx::patcherBranchHead ("Fruity Stereo Shaper", shaper, 4);
  980. CHECK (diff.valid && diff.slot && diff.slot->kind == lynx::EffectKind::stereoShaper && diff.slot->params[7] == 1.0f);
  981. CHECK (! lynx::patcherBranchHead ("Fruity Stereo Shaper", shaper, 2).valid);
  982. }
  983. TEST (parallel_container_sums_branches)
  984. {
  985. // two branches: dry with gain 0.5 and a utility that flips phase (matrix −1) with gain 0.5 -> silence
  986. auto slot = lynx::makeEffectSlot (lynx::EffectKind::parallel);
  987. lynx::EffectBranch dry;
  988. dry.gain = 0.5f;
  989. lynx::EffectBranch inverted;
  990. inverted.gain = 0.5f;
  991. auto flip = lynx::makeEffectSlot (lynx::EffectKind::utility);
  992. flip.params[2] = flip.params[5] = 0.25f; // (−1 + 2)/4
  993. inverted.chain.push_back (flip);
  994. slot.branches = { dry, inverted };
  995. auto fx = lynx::makeParallelEffect (slot);
  996. fx->prepare (48000.0);
  997. std::vector<float> l (9000, 0.8f), r (9000, -0.4f);
  998. fx->process (l.data(), r.data(), 9000, slot.params, 120.0);
  999. CHECK (std::abs (l[100]) < 1e-5f && std::abs (r[8999]) < 1e-5f);
  1000. // only the dry branch with gain 2: doubles
  1001. slot.branches = { lynx::EffectBranch { {}, 2.0f } };
  1002. auto doubler = lynx::makeParallelEffect (slot);
  1003. doubler->prepare (48000.0);
  1004. std::fill (l.begin(), l.end(), 0.25f);
  1005. doubler->process (l.data(), r.data(), 9000, slot.params, 120.0);
  1006. CHECK (std::abs (l[5000] - 0.5f) < 1e-6f);
  1007. }
  1008. TEST (fl_controller_constant_outputs)
  1009. {
  1010. std::vector<std::int32_t> xy { 2, 32768, 0, 65536, 0, 16384, 0, 65536, 0, 1024, 256 };
  1011. std::vector<std::uint8_t> blob (xy.size() * 4 + 8, 0);
  1012. std::memcpy (blob.data(), xy.data(), xy.size() * 4);
  1013. CHECK (std::abs (lynx::controllerConstantOutput ("Fruity X-Y Controller", blob, 0).value_or (-1.0f) - 0.5f) < 1e-4f);
  1014. CHECK (std::abs (lynx::controllerConstantOutput ("Fruity X-Y Controller", blob, 1).value_or (-1.0f) - 0.25f) < 1e-4f);
  1015. // Control Surface: version + group { 0x836: value 0.75, -, output 2 }
  1016. std::vector<std::uint8_t> cs (4, 0);
  1017. cs[0] = 1;
  1018. auto chunk = [] (std::vector<std::uint8_t>& out, std::uint32_t id, const std::vector<std::uint8_t>& body) {
  1019. const auto size = (std::uint64_t) body.size();
  1020. out.insert (out.end(), reinterpret_cast<const std::uint8_t*> (&id), reinterpret_cast<const std::uint8_t*> (&id) + 4);
  1021. out.insert (out.end(), reinterpret_cast<const std::uint8_t*> (&size), reinterpret_cast<const std::uint8_t*> (&size) + 8);
  1022. out.insert (out.end(), body.begin(), body.end());
  1023. };
  1024. std::vector<std::uint8_t> value (12, 0);
  1025. const float v = 0.75f;
  1026. const std::int32_t out = 2;
  1027. std::memcpy (value.data(), &v, 4);
  1028. std::memcpy (value.data() + 8, &out, 4);
  1029. std::vector<std::uint8_t> group;
  1030. chunk (group, 0x836, value);
  1031. chunk (cs, 0, group);
  1032. CHECK (std::abs (lynx::controllerConstantOutput ("Control Surface", cs, 2).value_or (-1.0f) - 0.75f) < 1e-6f);
  1033. CHECK (! lynx::controllerConstantOutput ("Control Surface", cs, 0).has_value());
  1034. }
  1035. TEST (flp_midi_out_notes_reach_wrapper_on_same_port)
  1036. {
  1037. auto record = [] (std::vector<std::uint8_t>& out, std::uint32_t id, std::vector<std::uint8_t> body) {
  1038. const auto size = (std::uint64_t) body.size();
  1039. out.insert (out.end(), reinterpret_cast<const std::uint8_t*> (&id), reinterpret_cast<const std::uint8_t*> (&id) + 4);
  1040. out.insert (out.end(), reinterpret_cast<const std::uint8_t*> (&size), reinterpret_cast<const std::uint8_t*> (&size) + 8);
  1041. out.insert (out.end(), body.begin(), body.end());
  1042. };
  1043. auto i32s = [] (std::vector<std::int32_t> v) {
  1044. std::vector<std::uint8_t> b (v.size() * 4);
  1045. std::memcpy (b.data(), v.data(), b.size());
  1046. return b;
  1047. };
  1048. flp::Project p;
  1049. flp::Channel midiOut;
  1050. midiOut.index = 0;
  1051. midiOut.type = flp::ChannelType::native;
  1052. midiOut.pluginName = "MIDI Out";
  1053. midiOut.pluginState = i32s ({ 7, 0, -1, -1, -1, 0, 3, 0 }); // channel 1, port 3 (@24)
  1054. flp::Channel wrapper;
  1055. wrapper.index = 1;
  1056. wrapper.type = flp::ChannelType::native;
  1057. wrapper.pluginName = "Fruity Wrapper";
  1058. wrapper.pluginState = i32s ({ 9 });
  1059. record (wrapper.pluginState, 0x32, i32s ({ 7 })); // VST3
  1060. record (wrapper.pluginState, 0x01, i32s ({ 3, -1, 0, 0, 0 })); // input port 3
  1061. p.channels = { midiOut, wrapper };
  1062. flp::Pattern pattern;
  1063. pattern.index = 1;
  1064. flp::Note note;
  1065. note.channel = 0;
  1066. note.key = 64;
  1067. note.length = 96;
  1068. pattern.notes.push_back (note);
  1069. p.patterns.push_back (pattern);
  1070. const auto song = lynx::importFlp (p);
  1071. // round 133: the MIDI Out channel stays a live route (its notes are sent at play time, with its MIDI channel)
  1072. CHECK (song.channels.size() == 2 && song.channels[0].instrument == lynx::InstrumentKind::midiOut && ! song.channels[0].muted);
  1073. CHECK (song.channels[1].plugin.foreign.midiInPort == 3 && song.channels[1].plugin.midiInPort == 3);
  1074. CHECK (lynx::midictl::parseMidiOut (song.channels[0].pluginState).port == 3);
  1075. int toWrapper = 0;
  1076. for (const auto& pat : song.patterns)
  1077. for (const auto& n : pat.notes)
  1078. toWrapper += n.channel == 1 && n.key == 64;
  1079. CHECK (toWrapper == 0);
  1080. }
  1081. TEST (flp_effect_automation_follows_slots_and_params)
  1082. {
  1083. // track 1: slot 0 with an effect that has no equivalent (dropped), slot 2 with Parametric EQ 2
  1084. flp::Project p;
  1085. flp::MixerInsert insert;
  1086. insert.index = 1;
  1087. insert.slots.push_back ({ 0, "Some Unknown FX", {} });
  1088. insert.slots.push_back ({ 2, "Fruity Parametric EQ 2", {} });
  1089. p.mixer.push_back (insert);
  1090. auto clip = [&] (int index, std::uint32_t target) {
  1091. flp::Channel c;
  1092. c.index = index;
  1093. c.type = flp::ChannelType::automationClip;
  1094. c.automation = { { 0.0, 0.0 }, { 96.0, 1.0 } };
  1095. p.channels.push_back (c);
  1096. p.automationLinks.push_back ({ index, { target } });
  1097. };
  1098. 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; };
  1099. clip (0, mixer (1, 2, 0x8000 + 7)); // EQ band 1 frequency
  1100. clip (1, mixer (1, 2, 0x1F01)); // EQ slot mix
  1101. clip (2, mixer (1, 0, 0x8000)); // parameter of the dropped effect
  1102. clip (3, mixer (1, 2, 0x8000 + 21)); // band type: no equivalent
  1103. const auto song = lynx::importFlp (p);
  1104. CHECK (song.automations.size() == 4);
  1105. CHECK (song.mixer[1].effects.size() == 1 && song.mixer[1].effects[0].kind == lynx::EffectKind::parametricEq);
  1106. const auto& freq = song.automations[0];
  1107. CHECK (freq.target.kind == lynx::AutomationTargetKind::effectParam && freq.target.index == 1 && freq.target.slot == 0);
  1108. CHECK (freq.target.param == lynx::peq::param (0, 1));
  1109. // FL's 0..1 (20 Hz..20 kHz) becomes our frequency scale
  1110. CHECK (std::abs (freq.points.front().value - lynx::peq::hzValue (20.0)) < 1e-4f);
  1111. CHECK (std::abs (freq.points.back().value - lynx::peq::hzValue (20000.0)) < 1e-4f);
  1112. CHECK (song.automations[1].target.kind == lynx::AutomationTargetKind::effectMix && song.automations[1].target.slot == 0);
  1113. CHECK (song.automations[2].target.kind == lynx::AutomationTargetKind::none);
  1114. CHECK (song.automations[3].target.kind == lynx::AutomationTargetKind::none);
  1115. // the automated mix is read/written through the target
  1116. auto copy = song;
  1117. CHECK (copy.setTargetValue (song.automations[1].target, 0.25f) && std::abs (copy.mixer[1].effects[0].mix - 0.25f) < 1e-6f);
  1118. CHECK (std::abs (copy.targetValue (song.automations[1].target) - 0.25f) < 1e-6f);
  1119. }
  1120. TEST (flp_real_files)
  1121. {
  1122. if (realFlpDir.empty())
  1123. {
  1124. std::printf (" (skipped: pass a folder with .flp files as argument)\n");
  1125. return;
  1126. }
  1127. int files = 0, withNotes = 0, withPlaylist = 0, automations = 0, automationsWithTarget = 0, sends = 0;
  1128. int threeOsc = 0, threeOscDecoded = 0, wrapped = 0, wrappedParsed = 0, wrappedVst2 = 0, wrappedVst2State = 0, wrappedVst2Old = 0, markers = 0, signatures = 0, trackEqs = 0;
  1129. int harmless = 0, harmlessDecoded = 0, harmlessParsed = 0, harmlessKind = 0, sytrus = 0, sytrusDecoded = 0, sytrusEngine = 0, sytrusRendered = 0,
  1130. sytrusSounding = 0;
  1131. int harmor = 0, harmorUsable = 0, harmorKind = 0, harmorSounding = 0;
  1132. int gms = 0, gmsUsable = 0, gmsKind = 0, gmsSounding = 0, morphine = 0, morphineUsable = 0, morphineKind = 0, morphineSounding = 0, demoGoldens = 0;
  1133. int ogun = 0, ogunUsable = 0, ogunKind = 0, ogunSounding = 0;
  1134. int autogun = 0, autogunUsable = 0, autogunKind = 0, autogunWithSg = 0, autogunSounding = 0; // REA_rodada57
  1135. int poizone = 0, poizoneUsable = 0, poizoneKind = 0, poizoneSounding = 0, poizoneGoldens = 0;
  1136. int miniSynth = 0, miniSynthUsable = 0, miniSynthKind = 0, miniSynthSounding = 0, miniSynthGoldens = 0;
  1137. int dx10 = 0, dx10Usable = 0, dx10Kind = 0, dx10Sounding = 0;
  1138. int kepler = 0, keplerUsable = 0, keplerKind = 0, keplerSounding = 0;
  1139. int toxic = 0, toxicUsable = 0, toxicKind = 0, toxicSounding = 0, toxicGoldens = 0; // REA_rodada45
  1140. int threeOscKind = 0, threeOscSounding = 0;
  1141. std::map<std::string, std::pair<int, int>> generators; // GMS/Ogun/Autogun/Sakura/DrumSynth: channels, decoded
  1142. int ogunTrumpet = 0; // Ogun channels whose spectrum matches the REA "Trumpet" reference
  1143. int peqSlots = 0, peqShaped = 0, reverbs = 0, reverbsDryless = 0, reverbsLowCut = 0;
  1144. int dwChannels = 0, dwZones = 0, fpcKits = 0, padChannels = 0, padNotes = 0, dwExact = 0;
  1145. int harmlessCompWanted = 0, harmlessCompLoaded = 0; // Harmless rack compressors / presets read from the installation
  1146. int maximusSlots = 0, maximusExact = 0, sgzSlots = 0, sgzExact = 0, vocodexSlots = 0, vocodexWithData = 0; // REA_rodada40
  1147. int shaperSlots = 0, shaperExact = 0, clipperSlots = 0, clipperExact = 0; // REA_rodada51
  1148. int flPeqSlots = 0, flPeqExact = 0; // REA_rodada50
  1149. int basicSlots = 0, basicExact = 0; // REA_rodada58: PanOMatic, Balance, Blood Overdrive
  1150. int equoSlots = 0, equoExact = 0, pitchShifterSlots = 0, pitchShifterExact = 0; // REA_rodada61
  1151. int filterSlots = 0, filterExact = 0; // REA_rodada63: Fast LP, Free Filter, Fruity Filter
  1152. int distructorSlots = 0, distructorExact = 0, distructorImpulses = 0; // REA_rodada71
  1153. int delay2Slots = 0, delay2Exact = 0, delay3Slots = 0, delay3Exact = 0; // REA_rodada52
  1154. int chorusSlots = 0, chorusExact = 0; // REA_rodada59: the exact choruses
  1155. int fastDistSlots = 0, fastDistExact = 0, flangusSlots = 0, flangusExact = 0, flangerSlots = 0, flangerExact = 0; // REA_rodada55
  1156. int phaserSlots = 0, phaserExact = 0; // REA_rodada105_phaser
  1157. // import with the installation's samples (realFlpDir is ...\Data): for DirectWave/FPC
  1158. lynx::FlpImportOptions withFiles;
  1159. const auto flRoot = std::filesystem::path (realFlpDir).parent_path().string();
  1160. withFiles.roots = { flRoot, realFlpDir, "" };
  1161. withFiles.fileExists = [] (const std::string& path) {
  1162. auto local = path;
  1163. std::replace (local.begin(), local.end(), '\\', '/');
  1164. return std::filesystem::exists (local);
  1165. };
  1166. withFiles.readFile = [] (const std::string& path) {
  1167. auto local = path;
  1168. std::replace (local.begin(), local.end(), '\\', '/');
  1169. std::ifstream in (local, std::ios::binary);
  1170. return std::vector<std::uint8_t> ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  1171. };
  1172. int embeddedSaved = 0;
  1173. withFiles.saveEmbedded = [&embeddedSaved] (const std::vector<std::uint8_t>& wav, const std::string&) {
  1174. ++embeddedSaved;
  1175. return wav.size() > 44 ? std::string ("embutido.wav") : std::string();
  1176. };
  1177. int slicedChannels = 0, flKeysChannels = 0;
  1178. for (auto& entry : std::filesystem::recursive_directory_iterator (realFlpDir))
  1179. {
  1180. if (entry.path().extension() != ".flp")
  1181. continue;
  1182. std::ifstream in (entry.path(), std::ios::binary);
  1183. std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  1184. ++files;
  1185. try
  1186. {
  1187. const auto p = flp::parseProject (bytes);
  1188. CHECK (! p.version.empty());
  1189. CHECK (p.tempo > 10.0 && p.tempo < 1000.0);
  1190. const auto song = lynx::importFlp (p);
  1191. CHECK (! song.patterns.empty());
  1192. {
  1193. const auto full = lynx::importFlp (p, withFiles);
  1194. bool hasFpc = false;
  1195. for (const auto& c : p.channels)
  1196. hasFpc = hasFpc || c.pluginName == "FPC";
  1197. // GMS and Morphine (REA_rodada39): every state plays on the exact engine (GMS with its waves from the
  1198. // installation) and sounds; two demo channels match golden samples of our round-39 models
  1199. for (const auto& c : p.channels)
  1200. {
  1201. if (c.pluginName == "GMS")
  1202. ++gms, gmsUsable += lynx::gmsStateUsable (c.pluginState) ? 1 : 0;
  1203. if (c.pluginName == "Morphine")
  1204. ++morphine, morphineUsable += lynx::morphineStateUsable (c.pluginState) ? 1 : 0;
  1205. }
  1206. // Kepler: every state in the demos plays on the exact engine (REA_rodada46) when the engine DLL is installed
  1207. for (const auto& c : p.channels)
  1208. if (c.pluginName == "Kepler")
  1209. {
  1210. ++kepler;
  1211. keplerUsable += lynx::keplerStateUsable (c.pluginState) ? 1 : 0;
  1212. }
  1213. for (const auto& c : full.channels)
  1214. if (c.instrument == lynx::InstrumentKind::kepler)
  1215. {
  1216. ++keplerKind;
  1217. 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 } },
  1218. 44100.0, 11025);
  1219. double e = 0.0;
  1220. bool finite = ! y.empty();
  1221. for (auto v : y)
  1222. finite = finite && std::isfinite (v), e += (double) v * v;
  1223. CHECK (finite);
  1224. keplerSounding += e > 1e-6 ? 1 : 0;
  1225. }
  1226. const auto file = entry.path().filename().string();
  1227. for (const auto& c : full.channels)
  1228. {
  1229. const auto isGms = c.instrument == lynx::InstrumentKind::gms, isMorphine = c.instrument == lynx::InstrumentKind::morphine;
  1230. if (! isGms && ! isMorphine)
  1231. continue;
  1232. (isGms ? gmsKind : morphineKind)++;
  1233. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  1234. const auto golden = (isGms && c.name == "GMS" && file.find ("Song For You") != std::string::npos)
  1235. || (isMorphine && c.name == "Morphine" && file.find ("SHINE") != std::string::npos);
  1236. std::vector<float> y;
  1237. if (isGms)
  1238. y = lynx::gms::renderReference (c.pluginState,
  1239. { { 0, 60, (float) iv (100), 1.0, 0.0, 0.0, 30000 }, { 11025, 64, (float) iv (80), 1.0, 0.0, 0.0, 40000 },
  1240. { 22050, 67, (float) iv (110), 1.0, 0.0, 0.0, 52000 }, { 60000, 48, (float) iv (127), 1.0, 0.0, 0.0, 70000 } },
  1241. 44100.0, golden ? 88200 : 22050, c.samplePath);
  1242. else
  1243. 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 } },
  1244. 44100.0, golden ? 100000 : 22050, c.samplePath);
  1245. double e = 0.0;
  1246. bool finite = ! y.empty();
  1247. for (auto v : y)
  1248. finite = finite && std::isfinite (v), e += (double) v * v;
  1249. CHECK (finite);
  1250. (isGms ? gmsSounding : morphineSounding) += e > 1e-6 ? 1 : 0;
  1251. if (golden && ! y.empty())
  1252. {
  1253. // our model, same events (GMS: tempo 140; Morphine: tempo 120), state as saved, 44.1 kHz
  1254. const auto rms = std::sqrt (e / (double) y.size());
  1255. CHECK (std::abs (rms - (isGms ? 0.213602668574 : 0.0673606448152)) < 1e-5);
  1256. 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 } },
  1257. 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 } };
  1258. for (const auto& g : isGms ? std::vector (std::begin (gmsAt), std::end (gmsAt)) : std::vector (std::begin (morAt), std::end (morAt)))
  1259. 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);
  1260. ++demoGoldens;
  1261. }
  1262. }
  1263. // MiniSynth (REA_rodada41): every state plays on the exact engine (tables from the installation's
  1264. // oscdata.dat) and sounds; two demo channels match golden samples of our round-41 model (bit-identical to
  1265. // renders of the installation's DLL for these states)
  1266. for (const auto& c : p.channels)
  1267. if (c.pluginName == "MiniSynth")
  1268. ++miniSynth, miniSynthUsable += lynx::miniSynthStateUsable (c.pluginState) ? 1 : 0;
  1269. for (const auto& c : full.channels)
  1270. {
  1271. if (c.instrument != lynx::InstrumentKind::minisynth)
  1272. continue;
  1273. ++miniSynthKind;
  1274. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  1275. const auto exo = file.find ("Exoplanet") != std::string::npos;
  1276. const auto golden = exo && (c.name == "Main Lead 1" || c.name == "Riser");
  1277. const auto y = lynx::minisynth::renderReference (c.pluginState,
  1278. { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 }, { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  1279. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 }, { 60000, 48, iv (127), 1.0, 0.0, 0.0, 70000 } },
  1280. 44100.0, golden ? 88200 : 22050, c.samplePath);
  1281. double e = 0.0;
  1282. bool finite = ! y.empty();
  1283. for (auto v : y)
  1284. finite = finite && std::isfinite (v), e += (double) v * v;
  1285. CHECK (finite);
  1286. miniSynthSounding += e > 1e-6 ? 1 : 0;
  1287. if (golden && ! y.empty())
  1288. {
  1289. // our model, same events, tempo 120, state as saved, 44.1 kHz, host blocks of 512, L = R = Final.Vol
  1290. const auto lead = c.name == "Main Lead 1";
  1291. CHECK (std::abs (std::sqrt (e / (double) y.size()) - (lead ? 0.177495801753 : 0.19006567413)) < 1e-9);
  1292. 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 } },
  1293. riserAt[] = { { 2000, -0.0603227727f }, { 15000, 0.112200871f }, { 30000, 0.0453962795f }, { 50000, 0.234380022f }, { 65000, -0.164545432f }, { 80000, -0.000485120923f } };
  1294. for (const auto& g : lead ? std::vector (std::begin (leadAt), std::end (leadAt)) : std::vector (std::begin (riserAt), std::end (riserAt)))
  1295. CHECK (y[2 * (std::size_t) g.i] == g.v && y[2 * (std::size_t) g.i + 1] == g.v);
  1296. ++miniSynthGoldens;
  1297. }
  1298. }
  1299. // PoiZone (REA_rodada44): every state plays on the exact engine and sounds; the demo channel matches golden
  1300. // samples of our round-44 model
  1301. for (const auto& c : p.channels)
  1302. if (c.pluginName == "PoiZone")
  1303. ++poizone, poizoneUsable += lynx::poizoneStateUsable (c.pluginState) ? 1 : 0;
  1304. for (const auto& c : full.channels)
  1305. {
  1306. if (c.instrument != lynx::InstrumentKind::poizone)
  1307. continue;
  1308. ++poizoneKind;
  1309. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  1310. const auto golden = c.name == "PoiZone" && file.find ("NIGHT TERROR") != std::string::npos;
  1311. const auto y = lynx::poizone::renderReference (c.pluginState,
  1312. { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 }, { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  1313. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 }, { 60000, 48, iv (127), 1.0, 0.0, 0.0, 70000 } },
  1314. 44100.0, golden ? 88200 : 22050);
  1315. double e = 0.0;
  1316. bool finite = ! y.empty();
  1317. for (auto v : y)
  1318. finite = finite && std::isfinite (v), e += (double) v * v;
  1319. CHECK (finite);
  1320. poizoneSounding += e > 1e-6 ? 1 : 0;
  1321. if (golden && ! y.empty())
  1322. {
  1323. // our model, same events, tempo 120, state as saved, 44.1 kHz, L = R = Final.Vol
  1324. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.018975758213507415) < 1e-7);
  1325. 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 } };
  1326. for (const auto& g : at)
  1327. 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);
  1328. ++poizoneGoldens;
  1329. }
  1330. }
  1331. // Toxic Biohazard (REA_rodada45): every demo state plays on the exact engine with the waves read from the
  1332. // installation's DLL and sounds; the "Bombs Away" channel matches golden samples of our round-45 model
  1333. for (const auto& c : p.channels)
  1334. if (c.pluginName == "Toxic Biohazard")
  1335. ++toxic, toxicUsable += lynx::toxicStateUsable (c.pluginState) ? 1 : 0;
  1336. for (const auto& c : full.channels)
  1337. {
  1338. if (c.instrument != lynx::InstrumentKind::toxic)
  1339. continue;
  1340. ++toxicKind;
  1341. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  1342. const auto golden = file.find ("Bombs Away") != std::string::npos;
  1343. const auto y = lynx::toxic::renderReference (
  1344. c.pluginState,
  1345. { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 }, { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  1346. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 }, { 60000, 48, iv (127), 1.0, 0.0, 0.0, 70000 } },
  1347. 44100.0, golden ? 88200 : 22050, c.samplePath, 512, 140.0);
  1348. double e = 0.0;
  1349. bool finite = ! y.empty();
  1350. for (auto v : y)
  1351. finite = finite && std::isfinite (v), e += (double) v * v;
  1352. CHECK (finite);
  1353. toxicSounding += e > 1e-6 ? 1 : 0;
  1354. if (golden && ! y.empty())
  1355. {
  1356. // our model (r45/py/toxmodel.py, which equals the DLL bit for bit), state as saved, tempo 140, 44.1 kHz
  1357. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.0832500924945) < 1e-6);
  1358. 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 } };
  1359. for (const auto& g : at)
  1360. 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);
  1361. ++toxicGoldens;
  1362. }
  1363. }
  1364. for (const auto& c : full.channels)
  1365. {
  1366. if (c.instrument == lynx::InstrumentKind::harmless && c.modelParams.size() >= (std::size_t) lynx::harmlessParamCount
  1367. && c.modelParams[(std::size_t) lynx::harmlessComboIndex + 3] > 0.5f)
  1368. {
  1369. ++harmlessCompWanted;
  1370. harmlessCompLoaded += c.modelParams.size() > (std::size_t) lynx::harmlessCompIndex + 1000 ? 1 : 0;
  1371. }
  1372. flKeysChannels += c.zones.size() == 30 && c.zones.front().path.find ("CHANGPIANO") != std::string::npos ? 1 : 0;
  1373. if (c.sampler.slices.size() >= 4)
  1374. {
  1375. ++slicedChannels;
  1376. CHECK (std::is_sorted (c.sampler.slices.begin(), c.sampler.slices.end()));
  1377. CHECK (c.sampler.slices.back() < 1.0f);
  1378. }
  1379. dwExact += c.instrument == lynx::InstrumentKind::directwave ? 1 : 0;
  1380. if (! c.zones.empty() && c.name.find (" - ") == std::string::npos)
  1381. {
  1382. ++dwChannels;
  1383. dwZones += (int) c.zones.size();
  1384. }
  1385. padChannels += c.name.find (" - ") != std::string::npos && c.instrument == lynx::InstrumentKind::sampler ? 1 : 0;
  1386. }
  1387. fpcKits += hasFpc;
  1388. // Maximus / Soundgoodizer become the exact engine (REA_rodada40); Vocodex carries SG.dat (and its
  1389. // carrier's Sytrus preset) read from the installation
  1390. const int basicSlots0 = basicSlots, basicExact0 = basicExact;
  1391. for (const auto& insert : p.mixer)
  1392. for (const auto& slot : insert.slots)
  1393. {
  1394. maximusSlots += slot.plugin == "Maximus";
  1395. delay2Slots += slot.plugin == "Fruity Delay 2";
  1396. delay3Slots += slot.plugin == "Fruity Delay 3";
  1397. sgzSlots += slot.plugin == "Soundgoodizer";
  1398. vocodexSlots += slot.plugin == "Vocodex";
  1399. shaperSlots += slot.plugin == "Fruity WaveShaper";
  1400. flPeqSlots += slot.plugin == "Fruity Parametric EQ 2" || slot.plugin == "Fruity Parametric EQ";
  1401. equoSlots += slot.plugin == "EQUO";
  1402. distructorSlots += slot.plugin == "Distructor";
  1403. pitchShifterSlots += slot.plugin == "Pitch Shifter";
  1404. clipperSlots += slot.plugin == "Fruity Soft Clipper";
  1405. basicSlots += slot.plugin == "Fruity PanOMatic" || slot.plugin == "Fruity Balance" || slot.plugin == "Fruity Blood Overdrive";
  1406. filterSlots += slot.plugin == "Fruity Fast LP" || slot.plugin == "Fruity Free Filter" || slot.plugin == "Fruity Filter";
  1407. chorusSlots += lynx::chorusControls (slot.plugin) > 0 && slot.state.size() >= 4 ? 1 : 0;
  1408. fastDistSlots += slot.plugin == "Fruity Fast Dist";
  1409. flangusSlots += slot.plugin == "Fruity Flangus";
  1410. flangerSlots += slot.plugin == "Fruity Flanger";
  1411. phaserSlots += slot.plugin == "Fruity Phaser";
  1412. }
  1413. // (the slots and the nodes of the Patcher graphs, as when Patchers were flattened into slots)
  1414. std::vector<const lynx::EffectSlot*> allSlots;
  1415. std::function<void (const lynx::EffectSlot&)> collect = [&] (const lynx::EffectSlot& fx) {
  1416. allSlots.push_back (&fx);
  1417. for (const auto& n : fx.patcher.nodes)
  1418. if (! n.generator)
  1419. collect (n.effect);
  1420. };
  1421. for (const auto& t : full.mixer)
  1422. for (const auto& fx : t.effects)
  1423. collect (fx);
  1424. for (const auto* slotPointer : allSlots)
  1425. {
  1426. const auto& fx = *slotPointer;
  1427. if (fx.kind == lynx::EffectKind::maximus)
  1428. {
  1429. const auto blob = lynx::maximusfx::unpack (fx.state);
  1430. CHECK (blob && lynx::maximusfx::presetOf (*blob) != nullptr);
  1431. if (blob)
  1432. (blob->build == lynx::hlfx::MaximusBuild::soundgoodizer ? sgzExact : maximusExact) += 1;
  1433. }
  1434. fastDistExact += fx.kind == lynx::EffectKind::fruityFastDist;
  1435. flangusExact += fx.kind == lynx::EffectKind::fruityFlangus;
  1436. flangerExact += fx.kind == lynx::EffectKind::fruityFlanger;
  1437. phaserExact += fx.kind == lynx::EffectKind::fruityPhaser;
  1438. if (fx.kind == lynx::EffectKind::vocodex)
  1439. vocodexWithData += ! lynx::vocodex::splitInstallData (fx.state).second.sgDat.empty();
  1440. if (fx.kind == lynx::EffectKind::fruityWaveShaper)
  1441. shaperExact += lynx::flshape::parseWaveShaper (fx.state).has_value() ? 1 : 0;
  1442. clipperExact += fx.kind == lynx::EffectKind::fruitySoftClipper;
  1443. flPeqExact += fx.kind == lynx::EffectKind::parametricEq && lynx::peq::flLaw (fx.params);
  1444. basicExact += fx.kind == lynx::EffectKind::fruityPanOMatic || fx.kind == lynx::EffectKind::fruityBalance
  1445. || fx.kind == lynx::EffectKind::fruityBloodOverdrive;
  1446. equoExact += fx.kind == lynx::EffectKind::equo && lynx::equofx::parse (fx.state).has_value();
  1447. if (fx.kind == lynx::EffectKind::distructor)
  1448. if (const auto d = lynx::distructor::parseBlob (fx.state))
  1449. {
  1450. ++distructorExact;
  1451. for (const auto& m : d->modules)
  1452. distructorImpulses += m.type == 3 && ! m.impulseFile.empty();
  1453. }
  1454. pitchShifterExact += fx.kind == lynx::EffectKind::flPitchShifter;
  1455. filterExact += fx.kind == lynx::EffectKind::fruityFastLP || fx.kind == lynx::EffectKind::fruityFreeFilter
  1456. || fx.kind == lynx::EffectKind::fruityFilter;
  1457. delay2Exact += fx.kind == lynx::EffectKind::fruityDelay2;
  1458. delay3Exact += fx.kind == lynx::EffectKind::fruityDelay3;
  1459. chorusExact += fx.kind == lynx::EffectKind::fruityChorus || fx.kind == lynx::EffectKind::hyperChorus || fx.kind == lynx::EffectKind::vintageChorus ? 1 : 0;
  1460. }
  1461. if (basicSlots - basicSlots0 != basicExact - basicExact0)
  1462. std::printf (" %s: %d PanOMatic/Balance/Blood Overdrive slots, %d exact effects\n", file.c_str(),
  1463. basicSlots - basicSlots0, basicExact - basicExact0);
  1464. for (const auto& pat : full.patterns)
  1465. for (const auto& n : pat.notes)
  1466. {
  1467. CHECK (n.channel >= 0 && n.channel < (int) full.channels.size());
  1468. padNotes += full.channels[(std::size_t) n.channel].name.find (" - ") != std::string::npos ? 1 : 0;
  1469. }
  1470. }
  1471. const auto peqDefaults = lynx::makeEffectSlot (lynx::EffectKind::parametricEq).params;
  1472. for (const auto& t : song.mixer)
  1473. for (const auto& fx : t.effects)
  1474. {
  1475. if (fx.kind == lynx::EffectKind::parametricEq)
  1476. {
  1477. ++peqSlots;
  1478. peqShaped += fx.params != peqDefaults;
  1479. for (double hz : { 50.0, 1000.0, 10000.0 })
  1480. CHECK (std::isfinite (lynx::peq::responseDb (fx.params, 48000.0, hz)));
  1481. }
  1482. if (fx.kind == lynx::EffectKind::reverb)
  1483. {
  1484. ++reverbs;
  1485. reverbsDryless += fx.params[4] < 0.01f;
  1486. reverbsLowCut += fx.params[5] > 0.0f;
  1487. }
  1488. if (fx.kind == lynx::EffectKind::reeverb2) // FL's exact Reeverb 2 (REA_rodada49): FL's own controls
  1489. {
  1490. const auto r2 = lynx::reeverb2::fromParams (fx.params);
  1491. ++reverbs;
  1492. reverbsDryless += r2.p[10] == 0;
  1493. reverbsLowCut += r2.p[0] >= 20;
  1494. }
  1495. }
  1496. for (const auto& t : song.mixer)
  1497. if (t.eq.active())
  1498. {
  1499. ++trackEqs;
  1500. for (std::size_t b = 0; b < 3; ++b)
  1501. CHECK (t.eq.hz[b] >= 10.0f && t.eq.hz[b] <= 16000.0f && std::abs (t.eq.gainDb[b]) <= 18.0f);
  1502. }
  1503. for (const auto& a : song.arrangements)
  1504. for (const auto& m : a.markers)
  1505. {
  1506. ++markers;
  1507. signatures += m.isSignature();
  1508. CHECK (m.tick >= 0);
  1509. }
  1510. // (round 168g: the patterns' own markers are no longer counted as the Playlist's)
  1511. for (const auto& p : song.patterns)
  1512. for (const auto& m : p.markers)
  1513. {
  1514. ++markers;
  1515. signatures += m.isSignature();
  1516. CHECK (m.tick >= 0);
  1517. }
  1518. // Fruity Wrapper (channels and mixer slots): every blob in the demos is read
  1519. auto countWrapper = [&] (const std::string& plugin, const std::vector<std::uint8_t>& state) {
  1520. if (plugin != "Fruity Wrapper")
  1521. return;
  1522. ++wrapped;
  1523. lynx::ForeignPlugin f;
  1524. wrappedParsed += lynx::parseFlWrapper (state, f) && f.format != lynx::ForeignPlugin::Format::none && ! f.name.empty();
  1525. if (f.format == lynx::ForeignPlugin::Format::vst2)
  1526. {
  1527. // VST2 state (REA_rodada89 §2): every one decodes, and writes back to the same bytes — except the
  1528. // wrapped Fruity effects of the FL 11 render tests (their mixer slots are read since round 80):
  1529. // those decode but do not write back byte-identical (not confirmed why)
  1530. ++wrappedVst2;
  1531. lynx::FlVst2State st;
  1532. if (lynx::parseFlVst2State (f.state, st) && st.version == -9)
  1533. {
  1534. ++wrappedVst2State;
  1535. wrappedVst2Old += lynx::writeFlVst2State (st) != f.state;
  1536. }
  1537. }
  1538. };
  1539. for (const auto& c : p.channels)
  1540. countWrapper (c.pluginName, c.pluginState);
  1541. for (const auto& insert : p.mixer)
  1542. for (const auto& slot : insert.slots)
  1543. countWrapper (slot.plugin, slot.state);
  1544. // Harmless: every state in the demos becomes a synth, with finite values
  1545. for (const auto& c : p.channels)
  1546. if (c.pluginName == "Harmless")
  1547. {
  1548. ++harmless;
  1549. lynx::SynthSettings hs;
  1550. float g = 1.0f;
  1551. if (lynx::synthFromHarmless (c.pluginState, 128.0, hs, g))
  1552. {
  1553. ++harmlessDecoded;
  1554. CHECK (std::isfinite (g) && hs.cutoff >= 20.0f && hs.cutoff <= 20000.0f && std::isfinite (hs.decay) && std::isfinite (hs.release));
  1555. }
  1556. if (const auto hp = lynx::harmlessParams (c.pluginState))
  1557. harmlessParsed += (int) hp->size() == lynx::harmlessParamCount ? 1 : 0;
  1558. }
  1559. for (const auto& c : song.channels)
  1560. harmlessKind += c.instrument == lynx::InstrumentKind::harmless ? 1 : 0;
  1561. // Harmor: every state in the demos plays on the exact engine (REA_rodada34) and sounds, with finite samples
  1562. for (const auto& c : p.channels)
  1563. if (c.pluginName == "Harmor")
  1564. {
  1565. ++harmor;
  1566. harmorUsable += lynx::harmorStateUsable (c.pluginState) ? 1 : 0;
  1567. }
  1568. for (const auto& c : song.channels)
  1569. if (c.instrument == lynx::InstrumentKind::harmor)
  1570. {
  1571. ++harmorKind;
  1572. 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);
  1573. double e = 0.0;
  1574. bool finite = ! y.empty();
  1575. for (auto v : y)
  1576. finite = finite && std::isfinite (v), e += (double) v * v;
  1577. CHECK (finite);
  1578. harmorSounding += e > 1e-6 ? 1 : 0;
  1579. }
  1580. // Ogun: every state in the demos plays on the exact engine (REA_rodada37) and sounds, with finite samples
  1581. for (const auto& c : p.channels)
  1582. if (c.pluginName == "Ogun")
  1583. {
  1584. ++ogun;
  1585. ogunUsable += lynx::ogunStateUsable (c.pluginState) ? 1 : 0;
  1586. }
  1587. for (const auto& c : song.channels)
  1588. if (c.instrument == lynx::InstrumentKind::ogun)
  1589. {
  1590. ++ogunKind;
  1591. 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 },
  1592. { 2000, 67, 0.6, 1.0, 0.0, 0.0, 0.0, 0.0, 0.5, 8000 } }, 44100.0, 11025);
  1593. double e = 0.0;
  1594. bool finite = ! y.empty();
  1595. for (auto v : y)
  1596. finite = finite && std::isfinite (v), e += (double) v * v;
  1597. CHECK (finite);
  1598. ogunSounding += e > 1e-6 ? 1 : 0;
  1599. }
  1600. // Autogun (REA_rodada57): every state plays on Ogun's engine in its Autogun variant; with the installation, the
  1601. // Magic Dust stage gets Autogun\Data\SG.dat
  1602. {
  1603. bool any = false;
  1604. for (const auto& c : p.channels)
  1605. if (c.pluginName == "Autogun")
  1606. ++autogun, autogunUsable += lynx::autogunStateUsable (c.pluginState) ? 1 : 0, any = true;
  1607. if (any)
  1608. for (const auto& c : lynx::importFlp (p, withFiles).channels)
  1609. if (c.instrument == lynx::InstrumentKind::autogun)
  1610. {
  1611. ++autogunKind;
  1612. lynx::ogun::RefOptions opt;
  1613. opt.autogun = true;
  1614. if (! c.samplePath.empty())
  1615. {
  1616. auto local = c.samplePath;
  1617. std::replace (local.begin(), local.end(), '\\', '/');
  1618. std::ifstream sg (local, std::ios::binary);
  1619. opt.sgState.assign ((std::istreambuf_iterator<char> (sg)), std::istreambuf_iterator<char>());
  1620. autogunWithSg += opt.sgState.size() == 1342 ? 1 : 0;
  1621. }
  1622. 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 } },
  1623. 44100.0, 11025, opt);
  1624. double e = 0.0;
  1625. bool finite = ! y.empty();
  1626. for (auto v : y)
  1627. finite = finite && std::isfinite (v), e += (double) v * v;
  1628. CHECK (finite);
  1629. autogunSounding += e > 1e-6 ? 1 : 0;
  1630. }
  1631. }
  1632. // Fruity DX10: every state plays on the exact engine when its channel processor is neutral (REA_rodada42)
  1633. for (const auto& c : p.channels)
  1634. if (c.pluginName == "Fruity DX10" || c.pluginName == "DX10")
  1635. ++dx10, dx10Usable += lynx::dx10StateUsable (c.pluginState) ? 1 : 0;
  1636. for (const auto& c : song.channels)
  1637. if (c.instrument == lynx::InstrumentKind::dx10)
  1638. {
  1639. ++dx10Kind;
  1640. 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 } },
  1641. 44100.0, 11025);
  1642. double e = 0.0;
  1643. bool finite = ! y.empty();
  1644. for (auto v : y)
  1645. finite = finite && std::isfinite (v), e += (double) v * v;
  1646. CHECK (finite);
  1647. dx10Sounding += e > 1e-6 ? 1 : 0;
  1648. }
  1649. // 3x Osc: every channel plays on the exact engine (REA_rodada47)
  1650. for (const auto& c : song.channels)
  1651. if (c.instrument == lynx::InstrumentKind::threeosc)
  1652. {
  1653. ++threeOscKind;
  1654. 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 } },
  1655. 44100.0, 11025);
  1656. double e = 0.0;
  1657. bool finite = ! y.empty();
  1658. for (auto v : y)
  1659. finite = finite && std::isfinite (v), e += (double) v * v;
  1660. CHECK (finite);
  1661. threeOscSounding += e > 1e-6 ? 1 : 0;
  1662. }
  1663. // Sytrus: every state in the demos (zlib + 10 units) decodes, with finite values
  1664. for (const auto& c : p.channels)
  1665. if (c.pluginName == "Sytrus")
  1666. {
  1667. ++sytrus;
  1668. lynx::SynthSettings ss;
  1669. float g = 1.0f;
  1670. // the Sytrus engine (REA_rodada31) reads every demo state; the first 24 play a short note (render settings)
  1671. if (lynx::sytrus::stateUsable (c.pluginState))
  1672. {
  1673. ++sytrusEngine;
  1674. if (sytrusRendered < 24)
  1675. {
  1676. ++sytrusRendered;
  1677. lynx::sytrus::Engine e;
  1678. e.prepare (44100.0, true, 512);
  1679. e.setPatch (lynx::sytrus::parsePatch (c.pluginState));
  1680. lynx::sytrus::NoteParams note;
  1681. e.noteOn (note);
  1682. std::vector<float> l (8192), r (8192);
  1683. e.render (l.data(), r.data(), 8192, 0.551f, 0.0f);
  1684. bool finite = true;
  1685. double energy = 0.0;
  1686. for (std::size_t k = 0; k < l.size(); ++k)
  1687. finite = finite && std::isfinite (l[k]) && std::isfinite (r[k]), energy += (double) l[k] * l[k];
  1688. CHECK (finite);
  1689. sytrusSounding += energy > 1e-6;
  1690. }
  1691. }
  1692. if (lynx::synthFromSytrus (c.pluginState, 128.0, ss, g))
  1693. {
  1694. ++sytrusDecoded;
  1695. CHECK (std::isfinite (g) && ss.cutoff >= 20.0f && ss.cutoff <= 20000.0f && std::isfinite (ss.attack) && std::isfinite (ss.decay)
  1696. && std::isfinite (ss.release) && ss.sustain >= 0.0f && ss.sustain <= 1.0f && std::isfinite (ss.filterEnv.amount));
  1697. for (const auto& o : ss.osc)
  1698. CHECK (o.level >= 0.0f && o.level <= 1.0f && o.octave >= -3 && o.octave <= 3);
  1699. }
  1700. }
  1701. // round 12 generators
  1702. for (const auto& c : p.channels)
  1703. {
  1704. lynx::SynthSettings gs;
  1705. float g = 1.0f;
  1706. int decoded = -1;
  1707. if (c.pluginName == "Harmor")
  1708. {
  1709. decoded = lynx::synthFromHarmor (c.pluginState, gs, g);
  1710. if (decoded)
  1711. {
  1712. // REA_rodada25 §4: most resonant settings stay below Q ≈ 3, a few saturate
  1713. CHECK (gs.resonance >= 0.0f && gs.resonance <= 1.0f);
  1714. static int resonant = 0, strong = 0, total = 0;
  1715. resonant += gs.resonance > 0.0f;
  1716. strong += gs.resonance > 0.9f;
  1717. if (++total % 50 == 0)
  1718. std::printf (" Harmor resonance: %d with a peak, %d with Q > ~5 (of %d)\n", resonant, strong, total);
  1719. }
  1720. static int saw = 0, square = 0, other = 0, total = 0;
  1721. if (decoded && std::any_of (gs.osc.begin(), gs.osc.end(), [] (const lynx::SynthOscillator& o) { return o.wave == lynx::Waveform::spectral; }))
  1722. {
  1723. auto isSaw = true, isSquare = true;
  1724. for (int n = 1; n <= 16; ++n)
  1725. {
  1726. isSaw = isSaw && std::abs (gs.spectrum[(std::size_t) n - 1] - 1.0f / n) < 1e-3f;
  1727. isSquare = isSquare && std::abs (gs.spectrum[(std::size_t) n - 1] - (n % 2 == 1 ? 1.1535f / n : 0.0f)) < 2e-3f;
  1728. }
  1729. ++(isSaw ? saw : isSquare ? square : other);
  1730. if (++total % 20 == 0 || total == 100)
  1731. std::printf (" Harmor spectrum: %d saw, %d square, %d other (of %d)\n", saw, square, other, total);
  1732. }
  1733. }
  1734. else if (c.pluginName == "GMS")
  1735. decoded = lynx::synthFromGms (c.pluginState, 128.0, gs, g);
  1736. else if (c.pluginName == "Ogun" || c.pluginName == "Autogun")
  1737. {
  1738. decoded = lynx::synthFromOgun (c.pluginState, gs, g);
  1739. // REA_rodada24 §1.9: the "Trumpet" preset (richness 7, no seeds) has partials 0.9087, 0.8610,
  1740. // 0.8151… at the fixed seed-3 phases 0.919, 0.380, 0.130
  1741. if (decoded && gs.osc[0].wave == lynx::Waveform::spectral)
  1742. {
  1743. CHECK (std::all_of (gs.spectrum.begin(), gs.spectrum.end(), [] (float a) { return std::isfinite (a) && a >= 0.0f; }));
  1744. static constexpr float trumpet[3] { 0.9087f, 0.8610f, 0.8151f }, phases[3] { 0.919f, 0.380f, 0.130f };
  1745. auto match = true;
  1746. for (std::size_t k = 0; k < 3; ++k)
  1747. match = match && std::abs (gs.spectrum[k] * 2.0f / 3.14159265f - trumpet[k]) < 1e-3f && std::abs (gs.spectrumPhase[k] - phases[k]) < 1e-3f;
  1748. ogunTrumpet += match ? 1 : 0;
  1749. }
  1750. }
  1751. else if (c.pluginName == "Sakura")
  1752. decoded = lynx::synthFromSakura (c.pluginState, gs);
  1753. else if (c.pluginName == "Kepler")
  1754. decoded = lynx::synthFromKepler (c.pluginState, gs, g);
  1755. else if (c.pluginName == "Wave Traveller")
  1756. decoded = lynx::waveTravellerStateUsable (c.pluginState) ? 2 : 0;
  1757. else if (c.pluginName == "PoiZone")
  1758. decoded = lynx::synthFromPoiZone (c.pluginState, gs, g);
  1759. else if (c.pluginName == "BassDrum")
  1760. decoded = lynx::synthFromBassDrum (c.pluginState, gs, g);
  1761. else if (c.pluginName == "Toxic Biohazard")
  1762. decoded = lynx::synthFromToxic (c.pluginState, gs, g);
  1763. else if (c.pluginName == "Morphine")
  1764. decoded = lynx::synthFromMorphine (c.pluginState, gs, g);
  1765. else if (c.pluginName == "BooBass")
  1766. decoded = lynx::synthFromBooBass (c.pluginState, gs);
  1767. else if (c.pluginName == "Drumpad" || c.pluginName == "DrumPad")
  1768. decoded = lynx::drumpadPatchName (c.pluginState).has_value() ? 2 : 0;
  1769. else if (c.pluginName == "Fruity Envelope Controller" && c.pluginState.size() >= 4 && c.pluginState[2] >= 6) // v >= 6
  1770. decoded = lynx::parseEnvelopeController (c.pluginState).has_value() ? 2 : 0;
  1771. else if (c.pluginName == "FLEX")
  1772. decoded = lynx::flexPresetName (c.pluginState).has_value() ? 2 : 0;
  1773. else if (c.pluginName == "Fruity DrumSynth Live")
  1774. decoded = lynx::drumSynthLiveParams (c.pluginState).has_value() ? 2 : 0;
  1775. if (decoded < 0)
  1776. continue;
  1777. auto& counter = generators[c.pluginName];
  1778. ++counter.first;
  1779. counter.second += decoded != 0;
  1780. if (decoded == 1 && c.pluginName != "Fruity DrumSynth Live")
  1781. {
  1782. CHECK (std::isfinite (g) && g >= 0.0f && gs.cutoff >= 20.0f && gs.cutoff <= 20000.0f && gs.sustain >= 0.0f && gs.sustain <= 1.0f);
  1783. 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));
  1784. for (const auto& o : gs.osc)
  1785. CHECK (o.level >= 0.0f && o.level <= 4.0f && std::isfinite (o.detune) && std::isfinite (o.pan));
  1786. }
  1787. }
  1788. // 3x Osc: every state in the demos decodes, with levels summing to at most 1
  1789. for (const auto& c : p.channels)
  1790. if (c.pluginName == "3x Osc")
  1791. {
  1792. ++threeOsc;
  1793. lynx::SynthSettings s;
  1794. if (lynx::synthFrom3xOsc (c.pluginState, s))
  1795. {
  1796. ++threeOscDecoded;
  1797. // with AM, osc 3 is the modulation depth and is left out of the sum
  1798. const auto sum = s.osc[0].level + s.osc[1].level + (s.am ? 0.0f : s.osc[2].level);
  1799. CHECK (sum <= 1.001f && s.osc[2].level <= 1.0f);
  1800. for (const auto& o : s.osc)
  1801. CHECK (o.octave >= -2 && o.octave <= 2 && std::abs (o.semitones) <= 11 && std::abs (o.pan) <= 1.0f);
  1802. }
  1803. }
  1804. for (auto& pat : p.patterns)
  1805. if (! pat.notes.empty())
  1806. {
  1807. ++withNotes;
  1808. break;
  1809. }
  1810. for (auto& a : p.arrangements)
  1811. for (auto& item : a.items)
  1812. CHECK (item.track >= 0 && item.track < 500);
  1813. for (auto& clip : song.playlist)
  1814. {
  1815. CHECK (clip.pattern >= 0 && clip.pattern < (int) song.patterns.size());
  1816. CHECK (clip.length > 0 && clip.offset >= 0);
  1817. if (clip.kind == lynx::ClipKind::automation)
  1818. CHECK (clip.automation >= 0 && clip.automation < (int) song.automations.size());
  1819. }
  1820. for (auto& a : song.automations)
  1821. {
  1822. ++automations;
  1823. automationsWithTarget += a.target.kind != lynx::AutomationTargetKind::none;
  1824. CHECK (a.isLive() || ! a.points.empty());
  1825. for (std::size_t i = 1; i < a.points.size(); ++i)
  1826. CHECK (a.points[i].tick >= a.points[i - 1].tick);
  1827. }
  1828. // imported routing has no cycles and valid destinations
  1829. CHECK (song.mixerOrder().size() + 1 == song.mixer.size());
  1830. for (auto& t : song.mixer)
  1831. for (auto& s : t.sends)
  1832. {
  1833. ++sends;
  1834. CHECK (s.target > 0 && s.target < (int) song.mixer.size());
  1835. }
  1836. if (! song.playlist.empty())
  1837. ++withPlaylist;
  1838. }
  1839. catch (const flp::Error& e)
  1840. {
  1841. ++failures;
  1842. std::printf (" FAILED %s: %s\n", entry.path().string().c_str(), e.what());
  1843. }
  1844. }
  1845. CHECK (threeOscDecoded == threeOsc);
  1846. std::printf (" Harmless: %d channels, %d decoded\n", harmless, harmlessDecoded);
  1847. std::printf (" Sytrus: %d channels, %d decoded, %d by the engine (%d of %d rendered sound)\n", sytrus, sytrusDecoded, sytrusEngine,
  1848. sytrusSounding, sytrusRendered);
  1849. CHECK (sytrusEngine == sytrus);
  1850. CHECK (sytrusSounding >= sytrusRendered * 3 / 4);
  1851. for (const auto& [name, counter] : generators)
  1852. {
  1853. std::printf (" %s: %d channels, %d decoded\n", name.c_str(), counter.first, counter.second);
  1854. CHECK (counter.second == counter.first);
  1855. }
  1856. std::printf (" Ogun spectra matching the Trumpet reference: %d\n", ogunTrumpet);
  1857. CHECK (ogunTrumpet >= 1);
  1858. // demo Gross Beat: the volume slot becomes the gate (those using an envelope slot get steps < 1)
  1859. {
  1860. int vocodexes = 0, vocodexSides = 0;
  1861. int lovePhilterLinks[4] {}; // live links: cutoff, resonance; inside containers: cutoff, resonance
  1862. int embeddedZones = 0, lovePhilters = 0, lovePhiltersRead = 0, lovePhiltersConverted = 0, gates = 0, shaped = 0, effectors = 0, patchers = 0, serial = 0, parallel = 0, generatorPatchers = 0;
  1863. for (const auto& entry : std::filesystem::recursive_directory_iterator (std::filesystem::path (realFlpDir) / "Demo projects"))
  1864. if (entry.path().extension() == ".flp")
  1865. {
  1866. std::ifstream in (entry.path(), std::ios::binary);
  1867. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  1868. try
  1869. {
  1870. const auto project = flp::parseProject (file);
  1871. for (const auto& insert : project.mixer)
  1872. for (const auto& slot : insert.slots)
  1873. if (slot.plugin == "Gross Beat")
  1874. {
  1875. // FL's own engine (REA_rodada53): every demo state is read
  1876. const auto fx = lynx::effectFromFl (slot.plugin, slot.state);
  1877. CHECK (fx && fx->kind == lynx::EffectKind::grossBeat);
  1878. ++gates;
  1879. if (fx && fx->kind == lynx::EffectKind::grossBeat && lynx::grossbeat::slotNames (fx->state, true).size() == 36)
  1880. ++shaped;
  1881. }
  1882. else if (slot.plugin == "Fruity Love Philter")
  1883. {
  1884. ++lovePhilters;
  1885. lovePhiltersRead += lynx::parseLovePhilter (slot.state).has_value();
  1886. if (const auto conv = lynx::lovePhilterEffects (slot.state))
  1887. {
  1888. ++lovePhiltersConverted;
  1889. CHECK (! conv->slots.empty());
  1890. for (const auto& fx : conv->slots)
  1891. for (auto v : fx.params)
  1892. CHECK (std::isfinite (v) && v >= 0.0f && v <= 1.0f);
  1893. for (const auto& fx : conv->slots)
  1894. for (const auto& link : fx.links)
  1895. {
  1896. const auto& chain = fx.branches[(std::size_t) link.branch].chain;
  1897. CHECK (link.source < (int) chain.size() && chain[(std::size_t) link.target].kind == lynx::EffectKind::parametricEq);
  1898. lovePhilterLinks[2 + (link.param == lynx::peq::param (0, 3) ? 1 : 0)]++;
  1899. }
  1900. for (const auto& link : conv->links)
  1901. {
  1902. lovePhilterLinks[link.param == lynx::peq::param (0, 3) ? 1 : 0]++;
  1903. CHECK (link.curve.size() == 33);
  1904. for (auto v : link.curve)
  1905. CHECK (std::isfinite (v) && v >= 0.0f && v <= 1.0f);
  1906. }
  1907. }
  1908. }
  1909. else if (slot.plugin == "Vocodex")
  1910. {
  1911. const auto v = lynx::vocodexEffect (slot.state);
  1912. CHECK (v.has_value());
  1913. vocodexes += v.has_value();
  1914. vocodexSides += v && (v->modIn > 0 || v->carIn > 0);
  1915. }
  1916. else if (slot.plugin == "Effector")
  1917. effectors += lynx::effectFromFl (slot.plugin, slot.state).has_value();
  1918. else if (slot.plugin == "Patcher")
  1919. {
  1920. const auto graph = lynx::parsePatcher (slot.state);
  1921. CHECK (graph.has_value());
  1922. ++patchers;
  1923. if (graph && ! graph->serialChain.empty())
  1924. ++serial;
  1925. if (graph && graph->serialChain.empty() && graph->audio)
  1926. ++parallel;
  1927. }
  1928. for (const auto& c : project.channels)
  1929. if (c.pluginName == "DirectWave")
  1930. if (const auto program = lynx::directWaveProgram (c.pluginState))
  1931. for (const auto& wav : program->embeddedWav)
  1932. if (! wav.empty())
  1933. {
  1934. ++embeddedZones;
  1935. CHECK (wav.size() > 44 && std::memcmp (wav.data(), "RIFF", 4) == 0);
  1936. }
  1937. for (const auto& c : project.channels)
  1938. if (c.pluginName == "Patcher")
  1939. {
  1940. const auto graph = lynx::parsePatcher (c.pluginState);
  1941. CHECK (graph.has_value());
  1942. generatorPatchers += graph && graph->mainGenerator >= 0;
  1943. }
  1944. }
  1945. catch (const flp::Error&) {}
  1946. }
  1947. CHECK (gates >= 30 && shaped == gates);
  1948. std::printf (" Gross Beat: %d slots, %d read exactly; Effector: %d converted; Love Philter: %d, %d read\n", gates, shaped, effectors,
  1949. lovePhilters, lovePhiltersRead);
  1950. CHECK (lovePhiltersRead == lovePhilters && lovePhiltersConverted == lovePhilters);
  1951. std::printf (" Vocodex: %d converted, %d with side inputs\n", vocodexes, vocodexSides);
  1952. CHECK (vocodexes >= 9);
  1953. std::printf (" Love Philter live links: %d cutoff, %d resonance; in multi-unit containers: %d cutoff, %d resonance\n",
  1954. lovePhilterLinks[0], lovePhilterLinks[1], lovePhilterLinks[2], lovePhilterLinks[3]);
  1955. CHECK (lovePhilterLinks[2] + lovePhilterLinks[3] > 0);
  1956. std::printf (" DirectWave: %d zones with embedded audio\n", embeddedZones);
  1957. CHECK (embeddedZones == 12);
  1958. std::printf (" Patcher: %d in the mixer (%d serial, %d reduced to serial/parallel), %d as instrument with generator\n", patchers, serial,
  1959. parallel, generatorPatchers);
  1960. CHECK (gates == 0 || shaped > 0);
  1961. }
  1962. // Envelope Controller "Basic Sidechain" (Future House): drops on the note and returns to ~1 within ~1/3 s
  1963. {
  1964. const auto path = std::filesystem::path (realFlpDir) / "Demo projects" / "Demo songs" / "Asher Postman - Future House.flp";
  1965. if (std::filesystem::exists (path))
  1966. {
  1967. std::ifstream in (path, std::ios::binary);
  1968. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  1969. const auto song = lynx::importFlp (flp::parseProject (file));
  1970. const lynx::ChannelDef* controller = nullptr;
  1971. for (const auto& c : song.channels)
  1972. if (c.instrument == lynx::InstrumentKind::controller)
  1973. controller = &c;
  1974. CHECK (controller != nullptr);
  1975. if (controller != nullptr)
  1976. {
  1977. auto inst = lynx::makeInstrument (lynx::InstrumentKind::controller);
  1978. inst->prepare (48000.0);
  1979. inst->configure (*controller, song.bpm);
  1980. std::vector<float> l (480), r (480);
  1981. inst->render (l.data(), r.data(), 480, 1.0f, 1.0f);
  1982. inst->noteOn (60, 1.0f);
  1983. inst->render (l.data(), r.data(), 480, 1.0f, 1.0f); // 10 ms
  1984. const auto ducked = inst->controlOutput (0);
  1985. for (int i = 0; i < 100; ++i) // +1 s
  1986. inst->render (l.data(), r.data(), 480, 1.0f, 1.0f);
  1987. const auto recovered = inst->controlOutput (0);
  1988. std::printf (" Envelope Controller (Basic Sidechain): %.2f on the note, %.2f after 1 s\n", ducked, recovered);
  1989. CHECK (ducked < 0.3f && recovered > 0.9f);
  1990. }
  1991. }
  1992. }
  1993. // mutation fuzz: real states with flipped and truncated bytes must not crash the readers
  1994. {
  1995. std::map<std::string, std::vector<std::uint8_t>> samples; // one real blob per plugin
  1996. for (const auto& entry : std::filesystem::recursive_directory_iterator (std::filesystem::path (realFlpDir) / "Demo projects"))
  1997. {
  1998. if (entry.path().extension() != ".flp")
  1999. continue;
  2000. std::ifstream in (entry.path(), std::ios::binary);
  2001. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  2002. try
  2003. {
  2004. const auto project = flp::parseProject (file);
  2005. for (const auto& c : project.channels)
  2006. if (! c.pluginState.empty())
  2007. samples.emplace (c.pluginName, c.pluginState);
  2008. for (const auto& insert : project.mixer)
  2009. for (const auto& slot : insert.slots)
  2010. if (! slot.state.empty())
  2011. samples.emplace (slot.plugin, slot.state);
  2012. }
  2013. catch (const flp::Error&) {}
  2014. }
  2015. std::uint32_t seed = 12345;
  2016. auto rnd = [&seed] { seed = seed * 1664525u + 1013904223u; return seed >> 8; };
  2017. int mutations = 0;
  2018. for (const auto& [name, original] : samples)
  2019. for (int round = 0; round < 40; ++round)
  2020. {
  2021. auto blob = original;
  2022. if (round % 4 == 3 && ! blob.empty())
  2023. blob.resize (rnd() % blob.size()); // truncated
  2024. else
  2025. for (int k = 0; k < 1 + (int) (rnd() % 8) && ! blob.empty(); ++k)
  2026. blob[rnd() % blob.size()] = (std::uint8_t) rnd();
  2027. ++mutations;
  2028. lynx::SynthSettings synth;
  2029. float gain = 1.0f;
  2030. lynx::synthFromSytrus (blob, 120.0, synth, gain);
  2031. lynx::synthFromHarmor (blob, synth, gain);
  2032. lynx::synthFromGms (blob, 120.0, synth, gain);
  2033. lynx::synthFromOgun (blob, synth, gain);
  2034. lynx::synthFromMorphine (blob, synth, gain);
  2035. lynx::synthFromKepler (blob, synth, gain);
  2036. lynx::synthFromPoiZone (blob, synth, gain);
  2037. lynx::synthFromToxic (blob, synth, gain);
  2038. lynx::synthFromBassDrum (blob, synth, gain);
  2039. lynx::synthFromHarmless (blob, 120.0, synth, gain);
  2040. lynx::harmlessParams (blob);
  2041. if (lynx::harmorStateUsable (blob))
  2042. lynx::harmorFallbacks (blob);
  2043. lynx::parseEnvelopeController (blob);
  2044. lynx::parseKeyboardController (blob);
  2045. lynx::parseLovePhilter (blob);
  2046. lynx::parsePatcher (blob);
  2047. lynx::effectFromFl (name, blob);
  2048. lynx::fpcPads (blob);
  2049. lynx::directWaveProgram (blob);
  2050. CHECK (std::isfinite (gain) && std::isfinite (synth.cutoff));
  2051. }
  2052. std::printf (" mutation fuzz: %d blobs from %d plugins\n", mutations, (int) samples.size());
  2053. }
  2054. // WavPack (.wv) samples from FL packs: all decode, with finite, non-silent audio
  2055. {
  2056. int files = 0, decoded = 0, audible = 0;
  2057. double seconds = 0.0;
  2058. for (const auto& entry : std::filesystem::recursive_directory_iterator (std::filesystem::path (realFlpDir) / "Patches" / "Packs"))
  2059. if (entry.path().extension() == ".wv")
  2060. {
  2061. std::ifstream in (entry.path(), std::ios::binary);
  2062. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  2063. ++files;
  2064. lynx::DecodedAudio audio;
  2065. if (! lynx::decodeWavPack (file.data(), file.size(), audio))
  2066. continue;
  2067. ++decoded;
  2068. float peak = 0.0f;
  2069. bool finite = true;
  2070. for (const auto& ch : audio.channels)
  2071. for (auto v : ch)
  2072. {
  2073. finite = finite && std::isfinite (v);
  2074. peak = std::max (peak, std::abs (v));
  2075. }
  2076. CHECK (finite && peak <= 4.0f);
  2077. audible += peak > 1e-4f;
  2078. seconds += (double) audio.channels[0].size() / audio.sampleRate;
  2079. }
  2080. std::printf (" WavPack: %d .wv files, %d decoded, %d audible, %.0f s of audio\n", files, decoded, audible, seconds);
  2081. CHECK (decoded == files);
  2082. }
  2083. // native GMS presets (.gmsynth = the contents of 0xD5 without the "SSLF")
  2084. int gmsPresets = 0, gmsDecoded = 0;
  2085. const auto gmsFolder = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "GMS";
  2086. if (std::filesystem::exists (gmsFolder))
  2087. for (const auto& entry : std::filesystem::recursive_directory_iterator (gmsFolder))
  2088. if (entry.path().extension() == ".gmsynth")
  2089. {
  2090. std::ifstream in (entry.path(), std::ios::binary);
  2091. std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  2092. std::vector<std::uint8_t> blob { 'S', 'S', 'L', 'F' };
  2093. const auto n = (std::uint32_t) file.size();
  2094. blob.insert (blob.end(), reinterpret_cast<const std::uint8_t*> (&n), reinterpret_cast<const std::uint8_t*> (&n) + 4);
  2095. blob.insert (blob.end(), file.begin(), file.end());
  2096. lynx::SynthSettings gs;
  2097. float g = 1.0f;
  2098. ++gmsPresets;
  2099. if (lynx::synthFromGms (blob, 120.0, gs, g))
  2100. {
  2101. ++gmsDecoded;
  2102. CHECK (std::isfinite (g) && gs.cutoff >= 20.0f && std::isfinite (gs.pitchEnv.amount) && std::isfinite (gs.lfo[lynx::lfoPitch].period));
  2103. }
  2104. }
  2105. std::printf (" GMS presets: %d, %d decoded\n", gmsPresets, gmsDecoded);
  2106. // the synth's "FL presets" button: Sytrus/Harmor .fst and .gmsynth become synth settings
  2107. int presetFiles = 0, presetsRead = 0;
  2108. for (const auto* name : { "Sytrus", "Harmor", "GMS", "Harmless", "Ogun", "Sakura", "Fruit Kick", "Fruity DX10", "MiniSynth", "Kepler",
  2109. "PoiZone", "Toxic Biohazard", "BassDrum" })
  2110. {
  2111. const auto folder = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / name;
  2112. if (! std::filesystem::exists (folder))
  2113. continue;
  2114. for (const auto& entry : std::filesystem::recursive_directory_iterator (folder))
  2115. if (entry.path().extension() == ".fst" || entry.path().extension() == ".gmsynth")
  2116. {
  2117. std::ifstream in (entry.path(), std::ios::binary);
  2118. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  2119. ++presetFiles;
  2120. presetsRead += lynx::synthFromFlPreset (file).has_value();
  2121. }
  2122. }
  2123. std::printf (" FL synth presets: %d files, %d read\n", presetFiles, presetsRead);
  2124. CHECK (presetsRead >= presetFiles * 95 / 100);
  2125. CHECK (gmsDecoded == gmsPresets);
  2126. CHECK (sytrusDecoded == sytrus);
  2127. CHECK (harmlessDecoded == harmless && harmless > 100);
  2128. std::printf (" Harmless: %d states parsed for the engine, %d channels imported as Harmless\n", harmlessParsed, harmlessKind);
  2129. CHECK (harmlessParsed == harmless && harmlessKind >= harmless);
  2130. std::printf (" Harmor: %d channels, %d states usable by the engine, %d imported as Harmor (%d sounding)\n", harmor, harmorUsable, harmorKind, harmorSounding);
  2131. CHECK (harmorUsable == harmor && harmor > 90 && harmorKind >= harmor && harmorSounding >= harmorKind * 9 / 10);
  2132. 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",
  2133. gms, gmsUsable, gmsKind, gmsSounding, morphine, morphineUsable, morphineKind, morphineSounding, demoGoldens);
  2134. CHECK (gmsUsable == gms && gms >= 9 && gmsKind == gms && gmsSounding == gmsKind);
  2135. std::printf (" MiniSynth channels: %d, %d usable, %d on the exact engine, %d sounding, %d golden\n", miniSynth, miniSynthUsable, miniSynthKind,
  2136. miniSynthSounding, miniSynthGoldens);
  2137. CHECK (miniSynthUsable == miniSynth && miniSynth >= 12 && miniSynthKind == miniSynth && miniSynthSounding == miniSynthKind && miniSynthGoldens == 2);
  2138. CHECK (morphineUsable == morphine && morphine >= 7 && morphineKind == morphine && morphineSounding == morphineKind && demoGoldens == 2);
  2139. std::printf (" PoiZone: %d channels, %d usable, %d imported as PoiZone (%d sounding), %d demo golden\n", poizone, poizoneUsable, poizoneKind,
  2140. poizoneSounding, poizoneGoldens);
  2141. CHECK (poizoneUsable == poizone && poizone >= 2 && poizoneKind == poizone && poizoneSounding == poizoneKind && poizoneGoldens == 1);
  2142. std::printf (" Toxic Biohazard: %d channels, %d usable, %d imported as Toxic (%d sounding), %d golden\n", toxic, toxicUsable, toxicKind,
  2143. toxicSounding, toxicGoldens);
  2144. CHECK (toxicUsable == toxic && toxic >= 3 && toxicKind == toxic && toxicSounding == toxicKind && toxicGoldens == 1);
  2145. std::printf (" Ogun: %d channels, %d states usable by the engine, %d imported as Ogun (%d sounding)\n", ogun, ogunUsable, ogunKind, ogunSounding);
  2146. CHECK (ogunUsable == ogun && ogun >= 20 && ogunKind >= ogun && ogunSounding >= ogunKind * 9 / 10);
  2147. std::printf (" Autogun: %d channels, %d states usable by the engine, %d imported as Autogun (%d with SG.dat, %d sounding)\n", autogun,
  2148. autogunUsable, autogunKind, autogunWithSg, autogunSounding);
  2149. CHECK (autogunUsable == autogun && autogun >= 9 && autogunKind == autogun && autogunWithSg == autogun && autogunSounding == autogun);
  2150. std::printf (" Fruity DX10: %d channels, %d states usable by the engine, %d imported as DX10 (%d sounding)\n", dx10, dx10Usable, dx10Kind, dx10Sounding);
  2151. CHECK (dx10Usable == dx10 && dx10 >= 12 && dx10Kind == dx10 && dx10Sounding == dx10Kind);
  2152. std::printf (" Kepler: %d channels, %d states usable by the engine, %d imported as Kepler (%d sounding)\n", kepler, keplerUsable, keplerKind, keplerSounding);
  2153. CHECK (keplerUsable == kepler && kepler >= 4 && keplerSounding == keplerKind && (keplerKind == 0 || keplerKind == kepler));
  2154. std::printf (" Harmless rack compressor: %d of %d presets read from the installation\n", harmlessCompLoaded, harmlessCompWanted);
  2155. CHECK (harmlessCompLoaded == harmlessCompWanted && harmlessCompWanted > 100);
  2156. std::printf (" Maximus: %d of %d slots on the exact engine; Soundgoodizer: %d of %d; Vocodex with SG.dat: %d of %d\n", maximusExact,
  2157. maximusSlots, sgzExact, sgzSlots, vocodexWithData, vocodexSlots);
  2158. // REA_rodada40 §6: in Demo projects the 4 states left on the approximation use the Frequency Splitter linear-phase
  2159. // option; the other Data folders add states with options not decoded
  2160. CHECK (maximusSlots - maximusExact <= 16 && maximusExact >= 97 && sgzExact == sgzSlots && sgzSlots > 90);
  2161. CHECK (vocodexWithData == vocodexSlots && vocodexSlots >= 10);
  2162. std::printf (" Fruity WaveShaper: %d of %d slots on the exact engine; Fruity Soft Clipper: %d of %d\n", shaperExact, shaperSlots,
  2163. clipperExact, clipperSlots);
  2164. // (Patcher graphs add WaveShaper nodes of their own to the imported mixer)
  2165. CHECK (shaperExact >= shaperSlots && shaperSlots >= 70 && clipperExact >= clipperSlots && clipperSlots >= 25); // REA_rodada51
  2166. std::printf (" Fruity Parametric EQ 2 / Parametric EQ: %d of %d slots on the exact engine\n", flPeqExact, flPeqSlots);
  2167. CHECK (flPeqExact >= flPeqSlots && flPeqSlots >= 1100); // REA_rodada50
  2168. std::printf (" Fruity PanOMatic / Balance / Blood Overdrive: %d of %d slots on the exact engines\n", basicExact, basicSlots);
  2169. // (since round 141 a Patcher stays one slot: "Bombs Away" insert 69 keeps its last PanOMatic)
  2170. CHECK (basicExact >= basicSlots && basicSlots >= 270); // REA_rodada58
  2171. // (Patcher graphs can add Distructor nodes of their own to the imported mixer)
  2172. std::printf (" Distructor: %d of %d slots on the exact engine (%d cabinet impulses read from the installation)\n",
  2173. distructorExact, distructorSlots, distructorImpulses);
  2174. CHECK (distructorExact >= distructorSlots && distructorSlots >= 4 && distructorImpulses >= 1); // REA_rodada71
  2175. std::printf (" EQUO: %d of %d slots on the exact engine\n", equoExact, equoSlots);
  2176. CHECK (equoExact == equoSlots && equoSlots >= 10); // REA_rodada61
  2177. std::printf (" Pitch Shifter: %d of %d slots on the exact engine\n", pitchShifterExact, pitchShifterSlots);
  2178. CHECK (pitchShifterExact == pitchShifterSlots && pitchShifterSlots >= 15); // REA_rodada61
  2179. std::printf (" Fruity Fast LP / Free Filter / Filter: %d of %d slots on the exact engines\n", filterExact, filterSlots);
  2180. CHECK (filterExact >= filterSlots && filterSlots >= 53); // REA_rodada63
  2181. std::printf (" Fruity Delay 2: %d of %d slots on the exact engine; Fruity Delay 3: %d of %d\n", delay2Exact, delay2Slots,
  2182. delay3Exact, delay3Slots);
  2183. // (FL 11 files' slots are read since round 80; Patcher graphs can add Delay 2 nodes of their own)
  2184. CHECK (delay2Exact >= delay2Slots && delay2Slots >= 60 && delay3Exact >= delay3Slots && delay3Slots >= 100); // REA_rodada52
  2185. std::printf (" Chorus family: %d of %d slots on the exact engines\n", chorusExact, chorusSlots);
  2186. CHECK (chorusExact >= chorusSlots && chorusSlots >= 47); // REA_rodada59
  2187. 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,
  2188. fastDistSlots, flangusExact, flangusSlots, flangerExact, flangerSlots);
  2189. CHECK (fastDistExact >= fastDistSlots && fastDistSlots >= 30 && flangusExact >= flangusSlots && flangusSlots >= 30
  2190. && flangerExact >= flangerSlots && flangerSlots >= 3); // REA_rodada55
  2191. std::printf (" Fruity Phaser: %d of %d slots on the exact engine\n", phaserExact, phaserSlots);
  2192. CHECK (phaserExact >= phaserSlots && phaserSlots >= 3); // REA_rodada105_phaser
  2193. // 3x Osc channel presets shipped with FL (.fst): all become synth settings
  2194. int presets = 0, presetsDecoded = 0;
  2195. const auto presetDir = std::filesystem::path (realFlpDir) / "Patches" / "Channel presets" / "3x Osc";
  2196. if (std::filesystem::is_directory (presetDir))
  2197. for (auto& entry : std::filesystem::directory_iterator (presetDir))
  2198. if (entry.path().extension() == ".fst")
  2199. {
  2200. ++presets;
  2201. std::ifstream in (entry.path(), std::ios::binary);
  2202. std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  2203. if (auto synth = lynx::synthFromFlPreset (bytes))
  2204. {
  2205. ++presetsDecoded;
  2206. CHECK (synth->osc[0].level + synth->osc[1].level + (synth->am ? 0.0f : synth->osc[2].level) <= 1.001f);
  2207. }
  2208. }
  2209. std::printf (" %d 3x Osc presets, %d read\n", presets, presetsDecoded);
  2210. CHECK (presetsDecoded == presets);
  2211. // FL effect presets that become built-in effects
  2212. int fxPresets = 0, fxDecoded = 0;
  2213. for (auto kind : { lynx::EffectKind::parametricEq, lynx::EffectKind::limiter, lynx::EffectKind::reverb, lynx::EffectKind::delay,
  2214. lynx::EffectKind::utility })
  2215. for (const auto& plugin : lynx::flPluginsForEffect (kind))
  2216. {
  2217. const auto dir = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Effects" / plugin;
  2218. if (! std::filesystem::is_directory (dir))
  2219. continue;
  2220. for (auto& entry : std::filesystem::recursive_directory_iterator (dir))
  2221. if (entry.path().extension() == ".fst")
  2222. {
  2223. ++fxPresets;
  2224. std::ifstream in (entry.path(), std::ios::binary);
  2225. std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  2226. fxDecoded += lynx::effectFromFlPreset (bytes).has_value();
  2227. }
  2228. }
  2229. std::printf (" %d FL effect presets, %d read\n", fxPresets, fxDecoded);
  2230. CHECK (fxDecoded == fxPresets);
  2231. CHECK (wrappedParsed == wrapped);
  2232. CHECK (wrappedVst2State == wrappedVst2 && wrappedVst2 >= 23 && wrappedVst2Old <= 13);
  2233. std::printf (" %d plugins in Fruity Wrapper, %d read; %d VST2 states, %d decoded (%d not written back byte-identical)\n", wrapped,
  2234. wrappedParsed, wrappedVst2, wrappedVst2State, wrappedVst2Old);
  2235. std::printf (" %d markers, %d time signature changes, %d tracks with EQ\n", markers, signatures, trackEqs);
  2236. std::printf (" %d parametric EQs (%d with FL settings), %d reverbs (%d without dry)\n", peqSlots, peqShaped, reverbs, reverbsDryless);
  2237. std::printf (" DirectWave: %d channels, %d zones; FPC: %d projects, %d pad channels, %d pad notes\n", dwChannels, dwZones, fpcKits,
  2238. padChannels, padNotes);
  2239. CHECK (dwZones > 1000 && padChannels > 250 && padNotes > 500);
  2240. std::printf (" DirectWave on the exact engine (REA_rodada72): %d channels\n", dwExact);
  2241. CHECK (dwExact >= 57);
  2242. std::printf (" sliced channels (Slicer/Slicex): %d; embedded WAVs saved: %d\n", slicedChannels, embeddedSaved);
  2243. CHECK (slicedChannels >= 25 && embeddedSaved >= 10); // without decoding Ogg here, some loops have no duration
  2244. std::printf (" FL Keys (Grand Piano, 30 zones): %d channels\n", flKeysChannels);
  2245. CHECK (flKeysChannels >= 8);
  2246. CHECK (peqSlots > 1000 && peqShaped > 900 && reverbsDryless > 20 && reverbsLowCut > 250);
  2247. CHECK (markers > 300 && signatures > 60);
  2248. std::printf (" %d 3x Osc channels, %d decoded, %d on the exact engine (%d sounding)\n", threeOsc, threeOscDecoded, threeOscKind, threeOscSounding);
  2249. // one demo channel (a noise layer of "Right Night feeling") has the channel filter closed (fast LP, Mod X 0): silent
  2250. // with its saved settings, in FL too
  2251. CHECK (threeOscKind == threeOsc && threeOscSounding == threeOscKind - 1);
  2252. std::printf (" %d .flp files read, %d with notes, %d with playlist, %d automations (%d with target), %d sends\n",
  2253. files, withNotes, withPlaylist, automations, automationsWithTarget, sends);
  2254. CHECK (files > 0);
  2255. CHECK (automations > 0 && sends > 0);
  2256. }
  2257. // ---------------------------------------------------------------------------
  2258. // Sequencer
  2259. TEST (pattern_toggle_step_and_length)
  2260. {
  2261. lynx::Pattern p;
  2262. CHECK (! p.hasStep (0, 4));
  2263. p.toggleStep (0, 4);
  2264. CHECK (p.hasStep (0, 4));
  2265. CHECK (p.notes.size() == 1 && p.notes[0].start == 4 * lynx::ticksPerStep);
  2266. CHECK (p.effectiveLength (4) == 4 * lynx::ppq); // one bar
  2267. p.toggleStep (1, 20); // second bar
  2268. CHECK (p.effectiveLength (4) == 8 * lynx::ppq);
  2269. p.toggleStep (0, 4);
  2270. CHECK (! p.hasStep (0, 4) && p.notes.size() == 1);
  2271. }
  2272. TEST (pattern_mute_removed_steps)
  2273. {
  2274. lynx::Pattern p;
  2275. p.toggleStep (0, 2);
  2276. p.notes[0].velocity = 0.3f;
  2277. p.toggleStep (0, 2, true); // off: muted, kept
  2278. CHECK (! p.hasStep (0, 2) && p.notes.size() == 1 && p.notes[0].muted);
  2279. p.toggleStep (0, 2, true); // on again: the same note back
  2280. CHECK (p.hasStep (0, 2) && p.notes.size() == 1 && ! p.notes[0].muted && p.notes[0].velocity == 0.3f);
  2281. p.toggleStep (0, 2, true);
  2282. // option off (FL's SetStep, round 169f): turning the muted step on adds a new note and leaves the muted one alone...
  2283. p.toggleStep (0, 2);
  2284. CHECK (p.hasStep (0, 2) && p.notes.size() == 2 && p.notes[0].muted != p.notes[1].muted);
  2285. p.toggleStep (0, 2); // ...and turning it off deletes every note of the step, the muted one too
  2286. CHECK (p.notes.empty());
  2287. // option on with two notes on the step: both muted, both back
  2288. p.toggleStep (0, 3);
  2289. p.notes.push_back (p.notes[0]);
  2290. p.notes[1].key = 64;
  2291. p.toggleStep (0, 3, true);
  2292. CHECK (! p.hasStep (0, 3) && p.notes.size() == 2 && p.notes[0].muted && p.notes[1].muted);
  2293. p.toggleStep (0, 3, true);
  2294. CHECK (p.hasStep (0, 3) && p.notes.size() == 2 && ! p.notes[0].muted && ! p.notes[1].muted);
  2295. // another channel's note on the same step is never touched
  2296. p.toggleStep (1, 3);
  2297. p.toggleStep (0, 3, true);
  2298. CHECK (p.hasStep (1, 3) && ! p.hasStep (0, 3));
  2299. }
  2300. TEST (scheduler_places_events_at_exact_samples)
  2301. {
  2302. lynx::Pattern p;
  2303. p.toggleStep (0, 0);
  2304. p.toggleStep (0, 4); // tick 960 = 1 beat
  2305. const double sr = 48000.0, bpm = 120.0; // 1 beat = 0.5 s = 24000 samples
  2306. const auto tps = lynx::ticksPerSample (bpm, sr);
  2307. lynx::EventBuffer ev;
  2308. lynx::schedulePatternLoop (p, p.effectiveLength (4), 23900.0 * tps, 512, tps, ev);
  2309. // the beat-2 note starts at sample 24000 -> offset 100 in this block
  2310. int ons = 0;
  2311. for (auto& e : ev)
  2312. if (e.type == lynx::NoteEvent::Type::on)
  2313. {
  2314. ++ons;
  2315. CHECK (e.sampleOffset == 100);
  2316. }
  2317. CHECK (ons == 1);
  2318. }
  2319. TEST (scheduler_wraps_the_loop)
  2320. {
  2321. lynx::Pattern p;
  2322. p.toggleStep (2, 0);
  2323. const auto len = p.effectiveLength (4); // 3840 ticks
  2324. const double sr = 48000.0;
  2325. const auto tps = lynx::ticksPerSample (120.0, sr);
  2326. lynx::EventBuffer ev;
  2327. // block that crosses the loop end: starts 10 samples before
  2328. const auto start = (double) len - 10.0 * tps;
  2329. lynx::schedulePatternLoop (p, len, start, 64, tps, ev);
  2330. bool found = false;
  2331. for (auto& e : ev)
  2332. if (e.type == lynx::NoteEvent::Type::on && e.channel == 2)
  2333. {
  2334. found = true;
  2335. CHECK (e.sampleOffset == 10);
  2336. }
  2337. CHECK (found);
  2338. }
  2339. TEST (scheduler_off_before_on_at_same_sample)
  2340. {
  2341. lynx::Pattern p;
  2342. lynx::Note a;
  2343. a.start = 0; a.length = 240; a.channel = 0;
  2344. lynx::Note b = a;
  2345. b.start = 240;
  2346. p.notes = { a, b };
  2347. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  2348. lynx::EventBuffer ev;
  2349. lynx::schedulePatternLoop (p, p.effectiveLength (4), 200.0, 4000, tps, ev);
  2350. CHECK (ev.size() >= 2);
  2351. CHECK (ev[0].type == lynx::NoteEvent::Type::off);
  2352. CHECK (ev[1].type == lynx::NoteEvent::Type::on);
  2353. CHECK (ev[0].sampleOffset == ev[1].sampleOffset);
  2354. }
  2355. TEST (snapshot_exchange_hands_over_and_recycles)
  2356. {
  2357. struct Counted
  2358. {
  2359. int value;
  2360. int* alive;
  2361. Counted (int v, int* a) : value (v), alive (a) { ++*alive; }
  2362. ~Counted() { --*alive; }
  2363. };
  2364. int alive = 0;
  2365. {
  2366. lynx::SnapshotExchange<Counted> x;
  2367. CHECK (x.acquire() == nullptr);
  2368. x.publish (std::make_unique<Counted> (1, &alive));
  2369. CHECK (x.acquire()->value == 1);
  2370. x.publish (std::make_unique<Counted> (2, &alive));
  2371. x.publish (std::make_unique<Counted> (3, &alive)); // 2 was never read: freed immediately
  2372. CHECK (alive == 2);
  2373. CHECK (x.acquire()->value == 3);
  2374. CHECK (alive == 2); // 1 was returned, but only the UI frees it
  2375. x.collectGarbage();
  2376. CHECK (alive == 1);
  2377. // without collection, the return slots fill up and the audio side holds on to the current one
  2378. for (int i = 0; i < 20; ++i)
  2379. {
  2380. x.publish (std::make_unique<Counted> (10 + i, &alive));
  2381. CHECK (x.acquire() != nullptr);
  2382. }
  2383. }
  2384. CHECK (alive == 0);
  2385. }
  2386. TEST (rack_renderer_makes_sound_and_reuses_instruments)
  2387. {
  2388. auto song = lynx::Song::makeDefault();
  2389. song.bpm = 120.0;
  2390. const double sr = 48000.0;
  2391. auto state = lynx::RackState::build (song, nullptr, sr);
  2392. lynx::RackRenderer r;
  2393. r.prepare (sr);
  2394. std::vector<float> l (512), rr (512);
  2395. double tick = 0.0;
  2396. float peak = 0.0f;
  2397. int kicks = 0;
  2398. for (int block = 0; block < 93; ++block) // 47616 samples: just before 1 s
  2399. {
  2400. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  2401. for (auto& e : r.lastEvents())
  2402. if (e.type == lynx::NoteEvent::Type::on && e.channel == 0)
  2403. ++kicks;
  2404. for (int i = 0; i < 512; ++i)
  2405. {
  2406. CHECK (std::isfinite (l[(std::size_t) i]));
  2407. peak = std::max (peak, std::abs (l[(std::size_t) i]));
  2408. }
  2409. tick += 512 * lynx::ticksPerSample (song.bpm, sr);
  2410. }
  2411. CHECK (peak > 0.1f && peak < 4.0f);
  2412. CHECK (kicks == 2); // 120 BPM: beats at 0 and 0.5 s
  2413. // editing the song keeps the instruments of channels whose type didn't change
  2414. song.channels[4].instrument = lynx::InstrumentKind::kick;
  2415. auto next = lynx::RackState::build (song, state.get(), sr);
  2416. CHECK (next->instruments[0] == state->instruments[0]);
  2417. CHECK (next->instruments[4] != state->instruments[4]);
  2418. // stopping silences the synth and triggers no new notes
  2419. r.process (next.get(), false, tick, 512, l.data(), rr.data());
  2420. CHECK (r.lastEvents().size() == 0);
  2421. }
  2422. TEST (flp_import_maps_channels_and_rescales)
  2423. {
  2424. flp::Project p;
  2425. p.header.ppq = 96;
  2426. p.tempo = 150.0;
  2427. p.channels = { { 0, flp::ChannelType::sampler, "Kick 808", "", "", 1 },
  2428. { 1, flp::ChannelType::audioClip, "Vocal", "", "", 2 },
  2429. { 2, flp::ChannelType::instrument, "Lead", "FLEX", "", 3 } };
  2430. flp::Pattern pat;
  2431. pat.index = 1;
  2432. flp::Note n;
  2433. n.position = 96;
  2434. n.length = 0;
  2435. n.channel = 2;
  2436. n.key = 72;
  2437. n.velocity = 64;
  2438. pat.notes.push_back (n);
  2439. p.patterns.push_back (pat);
  2440. const auto s = lynx::importFlp (p);
  2441. CHECK (s.bpm == 150.0);
  2442. CHECK (s.channels.size() == 3); // audio clip channels stay in the rack, as in FL
  2443. CHECK (s.channels[0].instrument == lynx::InstrumentKind::kick);
  2444. CHECK (s.channels[1].instrument == lynx::InstrumentKind::audioClip);
  2445. CHECK (s.channels[2].instrument == lynx::InstrumentKind::synth);
  2446. CHECK (s.patterns.size() == 1);
  2447. CHECK (s.patterns[0].notes[0].start == lynx::ppq); // 96 @ PPQ 96 = 1 beat
  2448. CHECK (s.patterns[0].notes[0].channel == 2);
  2449. CHECK (s.patterns[0].notes[0].length == lynx::ticksPerStep); // step note
  2450. CHECK (std::abs (s.patterns[0].notes[0].velocity - 0.5f) < 1e-6f);
  2451. }
  2452. TEST (live_queue_is_fifo_and_bounded)
  2453. {
  2454. lynx::LiveEventQueue q;
  2455. lynx::NoteEvent e;
  2456. CHECK (! q.pop (e));
  2457. for (int i = 0; i < 10; ++i)
  2458. {
  2459. lynx::NoteEvent in;
  2460. in.key = i;
  2461. CHECK (q.push (in));
  2462. }
  2463. for (int i = 0; i < 10; ++i)
  2464. CHECK (q.pop (e) && e.key == i);
  2465. int pushed = 0;
  2466. while (q.push (e))
  2467. ++pushed;
  2468. CHECK (pushed == (int) lynx::LiveEventQueue::capacity - 1); // one slot stays free to tell full from empty
  2469. }
  2470. // Whether this CPU's RCPPS (the ~12-bit reciprocal estimate, whose table is implementation-defined: Intel and AMD
  2471. // differ) gives the same bits as the CPU that the FL golden data was captured on (AMD Zen 2): a hash of the estimate for
  2472. // every float mantissa in [1, 2) (the estimate does not depend on the exponent in the normal range)
  2473. static bool rcpLikeReferenceCpu()
  2474. {
  2475. #if defined(__SSE__) || defined(_M_X64) || defined(__x86_64__) || defined(__aarch64__) || defined(_M_ARM64) // x86 SSE, or ARM64 through core/Simd.h (sse2neon)
  2476. std::uint64_t h = 1469598103934665603ull;
  2477. for (std::uint32_t m = 0; m < (1u << 23); m += 4)
  2478. {
  2479. alignas (16) float f[4];
  2480. for (std::uint32_t j = 0; j < 4; ++j)
  2481. {
  2482. const std::uint32_t bits = 0x3f800000u | (m + j);
  2483. std::memcpy (&f[j], &bits, 4);
  2484. }
  2485. _mm_store_ps (f, _mm_rcp_ps (_mm_load_ps (f)));
  2486. for (std::uint32_t j = 0; j < 4; ++j)
  2487. {
  2488. std::uint32_t bits;
  2489. std::memcpy (&bits, &f[j], 4);
  2490. h = (h ^ bits) * 1099511628211ull;
  2491. }
  2492. }
  2493. return h == 0xb5de670652cb0383ull;
  2494. #else
  2495. return false;
  2496. #endif
  2497. }
  2498. // FL's sampler interpolators (REA_rodada69 §1): outputs of FLEngine's own kernels (EXEC, r69/py/gold.py) on a synthetic
  2499. // 16-bit stereo signal; the port must give the same bits (linear, 6-point, 24-point sinc, 32-point sinc at speed 1.37,
  2500. // 16-point sinc widened to 24 points with the cutoff at π/9.5 at speed 9.5)
  2501. // The sinc kernels take 1/x from RCPPS plus one Newton step, as FL does, so FL's own bits depend on the CPU: they are
  2502. // required where RCPPS matches the capture machine's; elsewhere (Intel CPUs) within a few float ulps.
  2503. TEST (fl_sampler_interpolators_match_fl)
  2504. {
  2505. std::vector<float> l, r;
  2506. std::uint32_t x = 12345;
  2507. for (int k = 0; k < 2 * 144; ++k)
  2508. {
  2509. x = x * 1103515245u + 12345u;
  2510. const auto v = (float) (std::int16_t) (std::uint16_t) (x >> 16) / 32768.0f;
  2511. (k % 2 == 0 ? l : r).push_back (v);
  2512. }
  2513. const int pad = 40;
  2514. const std::pair<int, double> positions[] = { { 20, 0.0 }, { 21, 0.25 }, { 22, 0.5 }, { 23, 0.71828 }, { 30, 0.999 }, { 31, 1e-7 }, { 40, 0.3333 }, { 41, 0.875 } };
  2515. struct Case { int quality; double speed; std::uint32_t bits[16]; };
  2516. const Case cases[] = {
  2517. { 1, 1.0, { 0x3ec1a000, 0x3d774000, 0x3ca1e000, 0x3ec39900, 0xbf2d4600, 0xbe096800, 0xbea0cd17, 0x3f433097, 0x3f2796da, 0xbd9fbd17, 0x3f27a3ff, 0xbda03ff2, 0x3dace746, 0x3eba19c4, 0x3e477a00, 0x3f0f2a80 } },
  2518. { 2, 1.0, { 0x3ec1a000, 0x3d773fff, 0x3d34885d, 0x3eedeaa0, 0xbf49e298, 0xbea52fc4, 0xbeba55fc, 0x3f7c58da, 0x3f27c5c2, 0xbda139af, 0x3f27a3ff, 0xbda03ffa, 0x3dd8e7b1, 0x3ee872c1, 0x3e05b2d5, 0x3f144c4f } },
  2519. { 4, 1.0, { 0x3ec19fff, 0x3d773fff, 0x3d889c32, 0x3ef6b184, 0xbf57cf0d, 0xbf0abecd, 0xbea14a13, 0x3f94f11b, 0x3f27edc4, 0xbda10d8c, 0x3f27a3fd, 0xbda03ffa, 0x3e5baf41, 0x3edb298c, 0x3dd76cbf, 0x3ef9d212 } },
  2520. { 5, 1.37, { 0x3f19d373, 0x3ea9b6a3, 0xbdcbe56e, 0x3e2af6c6, 0xbf476f7a, 0xbe566a13, 0xbe9f166a, 0x3f52b5ad, 0x3f27ad16, 0xbce48792, 0x3f27960a, 0xbce01ebe, 0x3e1b96be, 0x3e163fd8, 0x3f042e42, 0x3ec4be83 } },
  2521. { 3, 9.5, { 0xbd97308e, 0x3dd8783c, 0xbd911374, 0x3e162a41, 0xbd8a959e, 0x3e38b220, 0xbd7e6785, 0x3e58895c, 0xbd380c4f, 0x3e7e3008, 0xbd380e4e, 0x3e7e2696, 0x3d00c6d0, 0xbd627d87, 0x3e308cb8, 0xbcece918 } },
  2522. };
  2523. const lynx::flvp::X87Extended precision;
  2524. const bool exactRcp = rcpLikeReferenceCpu();
  2525. for (const auto& c : cases)
  2526. {
  2527. lynx::flrs::Kernel k;
  2528. k.setQuality (c.quality);
  2529. k.setSpeed (c.speed);
  2530. int same = 0;
  2531. for (int p = 0; p < 8; ++p)
  2532. {
  2533. const auto lo = pad + positions[p].first - k.halfWidth + 1;
  2534. float a, b;
  2535. lynx::flrs::interpolate (k, l.data() + lo, r.data() + lo, positions[p].second, a, b);
  2536. std::uint32_t ba, bb;
  2537. std::memcpy (&ba, &a, 4);
  2538. std::memcpy (&bb, &b, 4);
  2539. if (exactRcp || c.quality < 3) // (linear and 6-point use no reciprocal)
  2540. same += (ba == c.bits[2 * p]) + (bb == c.bits[2 * p + 1]);
  2541. else
  2542. same += (std::abs (a - std::bit_cast<float> (c.bits[2 * p])) < 5e-7f) + (std::abs (b - std::bit_cast<float> (c.bits[2 * p + 1])) < 5e-7f);
  2543. }
  2544. CHECK (same == 16);
  2545. }
  2546. CHECK (lynx::flrs::halfWidthFor (4) == 12 && lynx::flrs::halfWidthFor (5) == 16 && lynx::flrs::halfWidthFor (9) == 256);
  2547. CHECK (lynx::flrs::speedFor (0.0f, 1.0) == 1.0 && std::abs (lynx::flrs::speedFor (1200.0f, 0.5) - 1.0) < 1e-12);
  2548. }
  2549. // The sampler at the original rate copies the sample (FL's copy path), and plays until the read position passes the
  2550. // last frame
  2551. TEST (fl_sampler_copies_at_original_rate)
  2552. {
  2553. auto data = std::make_shared<lynx::SampleData>();
  2554. data->sampleRate = 48000.0;
  2555. for (int i = 0; i < 300; ++i)
  2556. data->left.push_back (std::sin (0.05f * (float) i) * 0.5f);
  2557. auto sampler = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  2558. sampler->prepare (48000.0);
  2559. sampler->noteOn (60, 1.0f);
  2560. std::vector<float> l (400), r (400);
  2561. sampler->render (l.data(), r.data(), 400, 1.0f, 1.0f);
  2562. const auto panL = std::cos ((0.0f + 1.0f) * 0.25f * 3.14159265f) * 1.41421356f; // the sampler's centre pan gain
  2563. int exact = 0;
  2564. for (int i = 0; i < 300; ++i)
  2565. exact += l[(std::size_t) i] == data->left[(std::size_t) i] * panL ? 1 : 0;
  2566. CHECK (exact == 300);
  2567. CHECK (l[300] == 0.0f);
  2568. }
  2569. // FL's volume envelope on the sampler (REA_rodada69 §3): the articulator is stepped once per sequencer tick and the level
  2570. // ramps linearly to each tick's value; 96 PPQ at 120 BPM and 48 kHz = 250 samples per tick, attack 20000 = 89 ticks
  2571. TEST (fl_sampler_volume_envelope_steps_per_tick)
  2572. {
  2573. auto data = std::make_shared<lynx::SampleData>();
  2574. data->sampleRate = 48000.0;
  2575. data->left.assign (96000, 0.5f);
  2576. auto sampler = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  2577. sampler->prepare (48000.0);
  2578. lynx::ChannelDef c;
  2579. c.instrument = lynx::InstrumentKind::sampler;
  2580. c.sampler.flVolumeEnvelope = { 0, 1, 100, 20000, 0, 30000, 128, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, 0, 96 };
  2581. sampler->configure (c, 120.0);
  2582. sampler->noteOn (60, 1.0f);
  2583. std::vector<float> l (40000), r (40000);
  2584. sampler->render (l.data(), r.data(), 40000, 1.0f, 1.0f);
  2585. const auto panL = std::cos ((0.0f + 1.0f) * 0.25f * 3.14159265f) * 1.41421356f;
  2586. auto level = [&] (int n) { return l[(std::size_t) n] / (0.5f * panL); };
  2587. CHECK (std::abs (level (1000) - 1000.0f / 22500.0f) < 2e-5f);
  2588. CHECK (std::abs (level (10125) - 10125.0f / 22500.0f) < 2e-5f);
  2589. CHECK (std::abs (level (39000) - 1.0f) < 1e-6f); // the attack ended after 90 ticks; sustain 128 = 1.0
  2590. // release: 20000 = 89 ticks, linear (tension 0), from the current level
  2591. sampler->noteOff (60);
  2592. std::fill (l.begin(), l.end(), 0.0f);
  2593. sampler->render (l.data(), r.data(), 40000, 1.0f, 1.0f);
  2594. CHECK (std::abs (level (2500) - (1.0f - 10.0f / 90.0f)) < 1e-3f);
  2595. CHECK (l[39999] == 0.0f && sampler->idle());
  2596. }
  2597. // FL's native sampler voice (REA_rodada76): FL's own command-line renders of crafted projects (one sampler channel,
  2598. // our noise sample, notes at ticks 0/192/384 of 96 PPQ at 120 BPM, 16-bit output) — the fast LP with resonance and a
  2599. // note pan/velocity set; the 12 dB LP with volume, cutoff and pitch envelopes and their releases. FL's int16 codes at
  2600. // a few frames; LYNX within one code. Also the velocity law and the pan law with the channel's levels inside the voice.
  2601. TEST (fl_native_sampler_voice_matches_fl_renders)
  2602. {
  2603. auto noise = std::make_shared<lynx::SampleData>();
  2604. noise->sampleRate = 44100.0;
  2605. {
  2606. std::uint32_t seed = 1;
  2607. for (int i = 0; i < 26460; ++i)
  2608. {
  2609. seed = (seed * 1103515245u + 12345u) & 0x7fffffffu;
  2610. noise->left.push_back ((float) (((double) seed / 2147483647.0 * 2.0 - 1.0) * 0.3));
  2611. }
  2612. }
  2613. struct Ev { int on, off; float vel, pan; };
  2614. auto render = [&] (std::vector<std::int32_t> recs, std::array<int, 3> filter, const std::vector<Ev>& notes,
  2615. std::shared_ptr<lynx::SampleData> data, float chanGain) {
  2616. lynx::ChannelDef c;
  2617. c.instrument = lynx::InstrumentKind::sampler;
  2618. c.sampler.flChannel = recs;
  2619. c.sampler.flChannel.insert (c.sampler.flChannel.end(), { filter[0], filter[1], filter[2], 96 });
  2620. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  2621. lynx::setOfflineRendering (true); // FL's render dialog: 32-point sinc (the pitch envelope interpolates)
  2622. inst->prepare (44100.0);
  2623. lynx::setOfflineRendering (false);
  2624. inst->configure (c, 120.0);
  2625. inst->setSongPosition (0.0);
  2626. std::vector<std::pair<int, int>> events; // (sample, index ≥ 0 on / < 0 off)
  2627. for (int i = 0; i < (int) notes.size(); ++i)
  2628. events.push_back ({ notes[(std::size_t) i].on, i }), events.push_back ({ notes[(std::size_t) i].off, -1 - i });
  2629. std::sort (events.begin(), events.end());
  2630. std::vector<float> l (120000), r (120000);
  2631. int at = 0;
  2632. for (const auto& [when, i] : events)
  2633. {
  2634. inst->render (l.data() + at, r.data() + at, when - at, chanGain, chanGain);
  2635. at = when;
  2636. if (i >= 0)
  2637. {
  2638. inst->setNextNoteExpression (notes[(std::size_t) i].pan, 0.0f, false);
  2639. inst->noteOn (60, notes[(std::size_t) i].vel);
  2640. }
  2641. else
  2642. inst->noteOff (60);
  2643. }
  2644. inst->render (l.data() + at, r.data() + at, 120000 - at, chanGain, chanGain);
  2645. return std::make_pair (l, r);
  2646. };
  2647. const std::vector<std::int32_t> plain = { 0, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, 0 };
  2648. auto records = [&] (std::vector<std::int32_t> vol, std::vector<std::int32_t> cut, std::vector<std::int32_t> pitch) {
  2649. std::vector<std::int32_t> all = plain;
  2650. for (const auto* r : std::initializer_list<const std::vector<std::int32_t>*> { &vol, &cut, &plain, &pitch })
  2651. all.insert (all.end(), r->begin(), r->end());
  2652. return all;
  2653. };
  2654. const auto volPlain = std::vector<std::int32_t> { 4, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, -101 };
  2655. const auto chan = lynx::channelGain (10000.0f / 12800.0f); // the rack's gain at pan 0 (panGains: gain·√2·cos(π/4))
  2656. const int at[] = { 5, 300, 2000, 9000, 11500, 15000, 22100, 26000 };
  2657. auto check = [&] (const std::pair<std::vector<float>, std::vector<float>>& out, const int* goldL, const int* goldR) {
  2658. int bad = 0;
  2659. for (int n = 0; n < 3; ++n)
  2660. for (int k = 0; k < 8; ++k)
  2661. {
  2662. const auto p = (std::size_t) (44100 * n + at[k]);
  2663. bad += std::abs ((int) std::lround (out.first[p] * 32767.0f) - goldL[8 * n + k]) > 1;
  2664. bad += std::abs ((int) std::lround (out.second[p] * 32767.0f) - goldR[8 * n + k]) > 1;
  2665. }
  2666. return bad;
  2667. };
  2668. {
  2669. // fast LP (Mod X 100, Mod Y 200); notes: len 96, (vel 80, pan 40) len 48, (vel 120, pan 90) len 144
  2670. const auto out = render (records (volPlain, plain, plain), { 100, 200, 0 },
  2671. { { 0, 22050, 100.0f / 128, 0.0f }, { 44100, 55125, 80.0f / 128, -24.0f / 64 }, { 88200, 121275, 120.0f / 128, 26.0f / 64 } },
  2672. noise, chan);
  2673. const int L[] = { -194, -789, 883, 803, 614, -557, -983, -1641, -141, -572, 640, 582, 445, -403, -712, -1189, -207, -841, 942, 856, 655, -593, -1048, -1750 };
  2674. const int R[] = { -194, -789, 883, 803, 614, -557, -983, -1641, -75, -305, 342, 311, 238, -216, -381, -636, -411, -1671, 1871, 1700, 1300, -1179, -2082, -3477 };
  2675. CHECK (check (out, L, R) == 0);
  2676. }
  2677. {
  2678. // 12 dB LP (Mod X 90, Mod Y 60), volume / cutoff / pitch envelopes; notes len 48, (vel 80, pan 30) len 48, len 30
  2679. const auto out = render (records ({ 4, 1, 100, 3000, 20000, 30000, 100, 6000, 0, 100, 20000, 0, 32950, 0, 0, 0, -101 },
  2680. { 0, 1, 100, 6000, 2000, 15000, 40, 8000, 100, 100, 20000, 0, 32950, 0, 0, 0, 0 },
  2681. { 0, 1, 100, 4000, 20000, 9000, 0, 20000, 600, 100, 20000, 0, 32950, 0, 0, 0, 0 }),
  2682. { 90, 60, 1 },
  2683. { { 0, 11025, 100.0f / 128, 0.0f }, { 44100, 55125, 80.0f / 128, -34.0f / 64 }, { 88200, 95091, 100.0f / 128, 0.0f } },
  2684. noise, chan);
  2685. const int L[] = { -1, -203, 29, -298, 97, 0, 0, 0, 0, -156, 22, -228, 74, 0, 0, 0, -1, -203, 29, -2, 0, 0, 0, 0 };
  2686. const int R[] = { -1, -203, 29, -298, 97, 0, 0, 0, 0, -60, 9, -88, 29, 0, 0, 0, -1, -203, 29, -2, 0, 0, 0, 0 };
  2687. CHECK (check (out, L, R) == 0);
  2688. }
  2689. {
  2690. // a global volume LFO (flag 0x20, amount 80, speed 24000): one phase for the channel, (k + 1)·increment at song
  2691. // tick k (notes on ticks 0, 100, 300)
  2692. auto vol = volPlain;
  2693. vol[0] = 4 | 0x20, vol[11] = 80, vol[12] = 24000;
  2694. const auto out = render (records (vol, plain, plain), { 256, 0, 0 },
  2695. { { 0, 22050, 100.0f / 128, 0.0f }, { 22969, 45019, 80.0f / 128, 0.0f }, { 68906, 90956, 100.0f / 128, 0.0f } },
  2696. noise, chan);
  2697. const int pos[] = { 500, 3000, 7000, 12000, 23469, 26000, 30000, 69406, 72000, 80000 };
  2698. const int gold[] = { 1149, 765, 374, 4530, 846, -911, -646, 1160, 2316, 727 };
  2699. int bad = 0;
  2700. for (int k = 0; k < 10; ++k)
  2701. bad += std::abs ((int) std::lround (out.first[(std::size_t) pos[k]] * 32767.0f) - gold[k]) > 1;
  2702. CHECK (bad == 0);
  2703. }
  2704. {
  2705. // a sample with its own loop (WAV smpl 4410…8819): FL loops it while the note lasts and fades it out over 10 ms
  2706. // after the note-off (no volume envelope)
  2707. auto looped = std::make_shared<lynx::SampleData>();
  2708. looped->sampleRate = 44100.0;
  2709. for (int i = 0; i < 8820; ++i)
  2710. looped->left.push_back ((float) ((0.5 * (1.0 - 0.5 * i / 8820.0)) * std::sin (2.0 * 3.141592653589793 * 441.0 * i / 44100.0)));
  2711. looped->loopStart = 4410, looped->loopEnd = 8820;
  2712. const auto out = render (records (volPlain, plain, plain), { 256, 0, 0 },
  2713. { { 0, 22050, 100.0f / 128, 0.0f }, { 44100, 49612, 100.0f / 128, 0.0f } }, looped, chan);
  2714. const int pos[] = { 125, 4525, 8925, 13337, 17725, 22025, 22125, 22325, 22437, 49625, 49737, 50025 };
  2715. const int gold[] = { 6338, 4746, 3842, 4161, -1470, -989, -3577, -1174, -32, 4373, 2573, 42 };
  2716. int bad = 0;
  2717. for (int k = 0; k < 12; ++k)
  2718. bad += std::abs ((int) std::lround (out.first[(std::size_t) pos[k]] * 32767.0f) - gold[k]) > 1;
  2719. CHECK (bad == 0);
  2720. }
  2721. {
  2722. // a constant sample: velocity law (FL: vel 100 → 0.389608, 128 → 0.799753, 64 → 0.141805 at the default
  2723. // channel volume, pan centre) and a hard-left note (0.550997 / 0)
  2724. auto dc = std::make_shared<lynx::SampleData>();
  2725. dc->sampleRate = 44100.0;
  2726. dc->left.assign (20000, 0.5f);
  2727. auto level = [&] (float vel, float pan, bool right) {
  2728. const auto out = render (records (volPlain, plain, plain), { 256, 0, 0 }, { { 0, 10000, vel, pan } }, dc, chan);
  2729. return (right ? out.second : out.first)[5000] / 0.5f;
  2730. };
  2731. CHECK (std::abs (level (100.0f / 128, 0.0f, false) - 0.389608f) < 3e-5f);
  2732. CHECK (std::abs (level (1.0f, 0.0f, false) - 0.799753f) < 3e-5f);
  2733. CHECK (std::abs (level (64.0f / 128, 0.0f, false) - 0.141805f) < 3e-5f);
  2734. CHECK (std::abs (level (100.0f / 128, -1.0f, false) - 0.550997f) < 3e-5f);
  2735. CHECK (std::abs (level (100.0f / 128, -1.0f, true)) < 1e-6f);
  2736. }
  2737. }
  2738. // FPC's "cycle" layers (REA_rodada69 §4): among the layers covering the velocity, in order, the one after the last
  2739. // chosen, wrapping; FL's velocity index is round(v·128). Slicex's master volume uses FL's L11 law (§5).
  2740. TEST (fl_fpc_cycle_layers_and_slicex_master_volume)
  2741. {
  2742. auto level = [] (float v) {
  2743. auto d = std::make_shared<lynx::SampleData>();
  2744. d->sampleRate = 48000.0;
  2745. d->left.assign (4800, v);
  2746. return d;
  2747. };
  2748. // A covers 0…63, B 32…95, C 64…127 (all keys)
  2749. auto inst = lynx::makeZonedSampler ({ { { "a.wav", 60, 0, 127, 0, 63 }, level (0.1f) },
  2750. { { "b.wav", 60, 0, 127, 32, 95 }, level (0.2f) },
  2751. { { "c.wav", 60, 0, 127, 64, 127 }, level (0.3f) } });
  2752. inst->prepare (48000.0);
  2753. inst->configure (lynx::ChannelDef {}, 120.0);
  2754. auto play = [&] (float velocity) {
  2755. inst->allNotesOff();
  2756. std::vector<float> l (2000), r (2000);
  2757. inst->render (l.data(), r.data(), 2000, 1.0f, 1.0f);
  2758. std::fill (l.begin(), l.end(), 0.0f);
  2759. inst->noteOn (60, velocity);
  2760. inst->render (l.data(), r.data(), 10, 1.0f, 1.0f);
  2761. return l[5] / velocity;
  2762. };
  2763. auto which = [] (float x) { return (int) std::lround (x / 0.1f / std::cos (0.25f * 3.14159265f) / 1.41421356f); };
  2764. CHECK (which (play (64.0f / 128.0f)) == 2); // candidates B, C: first B
  2765. CHECK (which (play (64.0f / 128.0f)) == 3); // then C
  2766. CHECK (which (play (40.0f / 128.0f)) == 1); // candidates A, B: C is not among them → the first, A
  2767. CHECK (which (play (40.0f / 128.0f)) == 2); // then B
  2768. CHECK (which (play (64.0f / 128.0f)) == 3); // B was last → C
  2769. // Slicex state: version, 8 master values (volume 16000), options and UI state, deck A referencing a file
  2770. std::vector<std::uint8_t> state (4 + 32 + 47 + 12, 0);
  2771. const std::int32_t version = 0x00060003, volume = 16000;
  2772. std::memcpy (state.data(), &version, 4);
  2773. std::memcpy (state.data() + 4 + 8, &volume, 4);
  2774. state.push_back (1);
  2775. state.push_back (5);
  2776. for (char ch : std::string ("x.wav"))
  2777. state.push_back ((std::uint8_t) ch);
  2778. const auto sliced = lynx::slicex (state);
  2779. CHECK (sliced.has_value() && sliced->path == "x.wav");
  2780. CHECK (sliced && std::abs (sliced->gain - (float) ((std::pow (11.0, 1.25) - 1.0) / 10.0)) < 1e-5f);
  2781. }
  2782. TEST (fl_keys_state_wti_and_loader)
  2783. {
  2784. using namespace lynx::flkeys;
  2785. // state: u32 1, the wavetable name, 15 int32 knobs
  2786. State st;
  2787. st.wavetable = "Grand Piano";
  2788. for (int i = 0; i < 15; ++i)
  2789. st.knobs[(std::size_t) i] = 10 * i - 50;
  2790. const auto bytes = writeState (st);
  2791. CHECK (bytes.size() == 4 + 1 + 11 + 60);
  2792. const auto back = parseState (bytes);
  2793. CHECK (back && back->wavetable == "Grand Piano" && back->knobs == st.knobs);
  2794. // one WAV per keygroup: the file's own loop gives the default end and loop; a keygroup without a file reuses
  2795. // the previous one; a repeated file name copies the earlier keygroup; each file is followed by a guard frame
  2796. // that repeats the frame `loop` before the end (REA_rodada69 §8.2)
  2797. std::vector<std::string> names;
  2798. auto riff = parseWti ("[info]\nsamples=3\nfiletype=riff\npath=W\nvelocityfactor=2\nsamplerate=22050\n"
  2799. "[sample0]\nname=a.wav\nroot=40\nhigh=50\n[sample1]\nroot=51\nhigh=999\npos=2\nend=5\nloop=3\n"
  2800. "[sample2]\nname=A.WAV\nroot=60\nhigh=70\n", names);
  2801. CHECK (riff && names.size() == 1 && names[0] == "W\\a.wav");
  2802. CHECK (riff && riff->keygroups.size() == 3 && riff->keygroups[2].duplicateOf == 0 && riff->thresholds == std::vector<int> { 0 });
  2803. if (riff)
  2804. {
  2805. riff->files[0].loopStart = 4, riff->files[0].loopEnd = 8;
  2806. FileSamples a;
  2807. for (int i = 0; i < 10; ++i)
  2808. a.samples.push_back ((std::int16_t) (100 * (i + 1)));
  2809. const auto decoded = FlKeysTable::decode (riff->encode());
  2810. CHECK (decoded.has_value());
  2811. const auto w = buildWavetable (*decoded, { a });
  2812. CHECK (w != nullptr);
  2813. if (w != nullptr)
  2814. {
  2815. auto same = [] (const Keygroup& k, Keygroup e) { return k.root == e.root && k.high == e.high && k.pos == e.pos && k.end == e.end && k.loop == e.loop; };
  2816. CHECK (same (w->keygroups[0], { 40, 50, 0, 8, 5 }));
  2817. CHECK (same (w->keygroups[1], { 51, 999, 2, 5, 3 }));
  2818. CHECK (same (w->keygroups[2], { 60, 70, 0, 8, 5 }));
  2819. CHECK (w->data[9] == 400 && w->data[10] == 0 && w->data[8] == 900);
  2820. CHECK (w->sampleRate == 22050.0f && w->velocityFactor == 2.0f && ! w->stereo);
  2821. }
  2822. }
  2823. // one raw file: every keygroup's part is copied one after another with a guard sample; "canloop=false" makes the
  2824. // loop as long as the keygroup's end, so the voice stops there
  2825. auto raw = parseWti ("[info]\nsamples=2\nsamplefile=x.dat\nfiletype=raw\n[sample0]\nroot=36\nhigh=47\npos=0\nend=3\nloop=2\n"
  2826. "[sample1]\nroot=48\nhigh=999\npos=4\nend=7\nloop=4\ncanloop=false\n", names);
  2827. CHECK (raw && raw->raw && names.size() == 1 && names[0] == "x.dat");
  2828. if (raw)
  2829. {
  2830. FileSamples x;
  2831. for (int i = 0; i < 10; ++i)
  2832. x.samples.push_back ((std::int16_t) (10 * (i + 1)));
  2833. const auto w = buildWavetable (*raw, { x });
  2834. CHECK (w != nullptr);
  2835. if (w != nullptr)
  2836. {
  2837. CHECK (w->keygroups[0].pos == 0 && w->keygroups[0].end == 3 && w->keygroups[0].loop == 2);
  2838. CHECK (w->keygroups[1].pos == 5 && w->keygroups[1].end == 8 && w->keygroups[1].loop == 8);
  2839. const std::vector<std::int16_t> expected { 10, 20, 30, 40, 30, 50, 60, 70, 80, 50, 0, 0 };
  2840. CHECK (std::equal (expected.begin(), expected.end(), w->data.begin()));
  2841. // a held note on the non-looping keygroup ends by itself; 33 notes leave 32 sounding (FL's MaxPoly)
  2842. State s2;
  2843. s2.knobs = { -1000, 0, 0, -1000, 500, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  2844. RefNote n;
  2845. n.key = 60;
  2846. const auto out = renderReference (*w, s2, { n }, 44100.0, 4096, 512, true);
  2847. double early = 0, late = 0;
  2848. for (int i = 0; i < 64; ++i)
  2849. early += std::fabs (out[(std::size_t) (2 * i)]);
  2850. for (int i = 2048; i < 4096; ++i)
  2851. late += std::fabs (out[(std::size_t) (2 * i)]);
  2852. CHECK (early > 0.0 && late == 0.0);
  2853. }
  2854. }
  2855. // instrument front-end without a wavetable: silent and idle
  2856. auto inst = makeFlKeysInstrument (nullptr);
  2857. inst->prepare (44100.0);
  2858. inst->noteOn (60, 0.78f);
  2859. std::vector<float> l (256), r (256);
  2860. inst->render (l.data(), r.data(), 256, 1.0f, 1.0f);
  2861. CHECK (inst->idle() && l[100] == 0.0f);
  2862. }
  2863. TEST (ogg_vorbis_decodes_like_fl)
  2864. {
  2865. using namespace lynx::oggvorbis;
  2866. const std::uint8_t wavHeader[] = { 'R', 'I', 'F', 'F', 30, 0, 0, 0, 'W', 'A', 'V', 'E', 'f', 'm', 't', ' ', 2, 0, 0, 0, 0x4F, 0x67 };
  2867. CHECK (isOggWav (wavHeader, sizeof wavHeader));
  2868. CHECK (! isOggStream (wavHeader, sizeof wavHeader));
  2869. Decoded none;
  2870. CHECK (! decode (wavHeader, sizeof wavHeader, none) && none.channels == 0);
  2871. // FNV-1a 64 of the planar float32 samples FL's oggio_x64.dll decodes from installation files (REA_rodada77 §3):
  2872. // a plain file, one with a second (PCM) stream multiplexed in, one where an empty stream's end-of-stream page
  2873. // ends the decode early (FL keeps 17 728 of 20 691 frames), and an FL Keys wavetable
  2874. if (realFlpDir.empty())
  2875. return;
  2876. const auto root = std::filesystem::path (realFlpDir).parent_path();
  2877. const struct { const char* file; std::uint64_t hash; std::size_t frames; int channels; } cases[] = {
  2878. { "Data/Patches/Packs/Drums/Percussion/FPC Wood Hit 2.wav", 0xe6137a6141cd5946ull, 29664, 1 },
  2879. { "Data/Patches/Packs/Instruments/Keyboard/Grand/FL Piano (11).wav", 0x3f60df388ac0c636ull, 264600, 2 },
  2880. { "Data/Patches/Packs/Drums/Percussion/Grv Perc 08.wav", 0x7f235f467e90efe0ull, 17728, 0 },
  2881. { "Plugins/Fruity/Generators/FL Keys/Wavtables/Grand Piano/CHANGPIANOHARD_A8.wav", 0x00bbf4cc42f5f72cull, 153280, 0 },
  2882. };
  2883. for (const auto& c : cases)
  2884. {
  2885. const auto path = root / c.file;
  2886. if (! std::filesystem::exists (path))
  2887. continue;
  2888. std::ifstream in (path, std::ios::binary);
  2889. const std::vector<std::uint8_t> bytes { std::istreambuf_iterator<char> (in), {} };
  2890. CHECK (isOggWav (bytes.data(), bytes.size()));
  2891. Decoded d;
  2892. CHECK (decode (bytes.data(), bytes.size(), d));
  2893. if (d.channels == 0)
  2894. continue;
  2895. std::uint64_t h = 1469598103934665603ULL;
  2896. for (const auto& ch : d.data)
  2897. {
  2898. const auto* p = reinterpret_cast<const std::uint8_t*> (ch.data());
  2899. for (std::size_t i = 0; i < ch.size() * sizeof (float); ++i)
  2900. h = (h ^ p[i]) * 1099511628211ULL;
  2901. }
  2902. CHECK (d.data[0].size() == c.frames && d.sampleRate == 44100);
  2903. CHECK (c.channels == 0 || d.channels == c.channels);
  2904. CHECK (h == c.hash);
  2905. }
  2906. }
  2907. TEST (directwave_crt_math_and_state)
  2908. {
  2909. using namespace lynx::dw;
  2910. auto f = [] (std::uint32_t b) { float x; std::memcpy (&x, &b, 4); return x; };
  2911. auto u = [] (float x) { std::uint32_t b; std::memcpy (&b, &x, 4); return b; };
  2912. // the plugin's own run-time (r72/run/mt.exe on DirectWave_x64.dll): values where the libm result differs
  2913. CHECK (u (sinfCrt (f (0x40b1a97fu))) == 0xbf2af4bbu);
  2914. CHECK (u (sinfCrt (f (0xc0ad8469u))) == 0x3f4222f6u);
  2915. CHECK (u (sinfCrt (f (0xc016e8c7u))) == 0xbf34b32du);
  2916. CHECK (u (cosfCrt (f (0xc0af1cc0u))) == 0x3f305702u);
  2917. CHECK (u (cosfCrt (f (0x3f15cba3u))) == 0x3f556924u);
  2918. CHECK (u (cosfCrt (f (0xc0aeb047u))) == 0x3f2dddfeu);
  2919. CHECK (u (expfCrt (f (0xbfb679c0u))) == 0x3e7622a1u);
  2920. CHECK (encodeFormats ({ true, false }) == std::vector<float> ({ 1.0f, 2.0f, 1.0f, 0.0f }));
  2921. CHECK (decodeFormats ({ 1.0f, 2.0f, 1.0f, 0.0f }) == std::vector<bool> ({ true, false }));
  2922. CHECK (! parseState ({ 1, 2, 3 }).has_value());
  2923. }
  2924. TEST (directwave_matches_fl_with_demo_programs)
  2925. {
  2926. // Demo programs (voices, mod matrix, filters, chorus, reverb, shaper) with our own 16-bit samples in every zone:
  2927. // FNV-1a 64 of DirectWave_x64.dll's output (r72/py/synth.py + cmp.py: 44.1 kHz, host blocks of 512, tempo 120)
  2928. if (realFlpDir.empty())
  2929. return;
  2930. using namespace lynx::dw;
  2931. auto readAll = [] (const std::filesystem::path& p) {
  2932. std::ifstream in (p, std::ios::binary);
  2933. return std::vector<std::uint8_t> { std::istreambuf_iterator<char> (in), {} };
  2934. };
  2935. auto stateOf = [&] (const std::string& file, const std::string& channel) {
  2936. const auto path = std::filesystem::path (realFlpDir) / file;
  2937. if (! std::filesystem::exists (path))
  2938. return std::vector<std::uint8_t>();
  2939. const auto p = flp::parseProject (readAll (path));
  2940. for (const auto& c : p.channels)
  2941. if (c.pluginName == "DirectWave" && c.name == channel)
  2942. return c.pluginState;
  2943. return std::vector<std::uint8_t>();
  2944. };
  2945. auto sampleValue = [] (int z, std::int64_t j) {
  2946. const auto v = (j * (13 + z * 7)) % 4096 - 2048;
  2947. const auto w = ((j * j + z * 31) % 997) - 498;
  2948. return (std::int16_t) std::clamp<std::int64_t> (v * 6 + w * 8, -32768, 32767);
  2949. };
  2950. std::vector<RefNote> notes;
  2951. auto note = [&] (int start, int key, double vel, double fv, double fp, double cents) {
  2952. RefNote n; n.start = start; n.key = key; n.velocity = vel; n.finalVol = fv; n.finalPan = fp; n.cents = cents;
  2953. notes.push_back (n);
  2954. };
  2955. note (0, 48, 0.8, 0.7, -0.3, 0); note (11025, 60, 0.5, 0.9, 0.4, 25); note (22050, 67, 1.0, 0.5, 0.0, 0);
  2956. note (44100, 36, 0.3, 0.8, 0.8, -40); note (80000, 72, 0.9, 0.6, -0.6, 0);
  2957. notes[0].release = 40000; notes[1].release = 70000; notes[2].release = 60000;
  2958. auto run = [&] (const std::string& file, const std::string& channel, std::uint64_t expected) {
  2959. const auto st = parseState (stateOf (file, channel));
  2960. if (! st)
  2961. return;
  2962. const auto* prog = playedProgram (*st);
  2963. CHECK (prog != nullptr);
  2964. if (prog == nullptr)
  2965. return;
  2966. std::vector<std::shared_ptr<const ZoneSamples>> zs;
  2967. for (int z = 0; z < (int) prog->zones.size(); ++z)
  2968. {
  2969. auto s = std::make_shared<ZoneSamples>();
  2970. s->bits = 16;
  2971. s->channels = 1;
  2972. s->frames = prog->zones[(std::size_t) z].frames;
  2973. std::vector<std::int16_t> v ((std::size_t) s->frames + 512, 0);
  2974. for (std::int64_t j = 0; j < s->frames; ++j)
  2975. v[(std::size_t) j + 256] = sampleValue (z, j);
  2976. s->i16.push_back (std::move (v));
  2977. zs.push_back (s);
  2978. }
  2979. EngineOptions o;
  2980. o.velocityAsInitVol = (st->flags & 2) != 0;
  2981. o.noGainClamp = (st->flags & 1) != 0;
  2982. const auto out = renderReference (*prog, zs, o, notes, 44100.0, 120000, 512, 1);
  2983. std::uint64_t h = 14695981039346656037ull;
  2984. for (const float x : out)
  2985. {
  2986. std::uint32_t b;
  2987. std::memcpy (&b, &x, 4);
  2988. for (int k = 0; k < 4; ++k)
  2989. h = (h ^ ((b >> (8 * k)) & 0xffu)) * 1099511628211ull;
  2990. }
  2991. CHECK (h == expected);
  2992. if (h != expected)
  2993. std::printf (" DirectWave %s: %016llx\n", channel.c_str(), (unsigned long long) h);
  2994. };
  2995. run ("Demo projects/Visual/ZGameEditor Visualizer/Treben - Umziky - All the same.flp", "Gtr", 0xa1de3dd6be53d665ull);
  2996. run ("Demo projects/Visual/ZGameEditor Visualizer/Treben - Umziky - All the same.flp", "Rhodes", 0xcd458ec6a857f9c7ull);
  2997. run ("System/Render tests/Directwave.flp", "DW", 0x95b4c2e29b664dd1ull);
  2998. }
  2999. TEST (fl_keys_matches_fl_with_the_installation)
  3000. {
  3001. // FL Keys' own output (r69/keys/kcmp3.py: the installation's DLL, FL's x87 host pan law, blocks of 512 at
  3002. // 44.1 kHz): FNV-1a 64 of the interleaved float bits of 24 000 frames
  3003. if (realFlpDir.empty())
  3004. return;
  3005. using namespace lynx::flkeys;
  3006. const auto folder = std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "FL Keys" / "Wavtables";
  3007. if (! std::filesystem::exists (folder / "mda Piano.wti"))
  3008. return;
  3009. auto readAll = [] (const std::filesystem::path& p) {
  3010. std::ifstream f (p, std::ios::binary);
  3011. return std::vector<std::uint8_t> { std::istreambuf_iterator<char> (f), {} };
  3012. };
  3013. auto pcm = [] (const std::vector<std::uint8_t>& w) {
  3014. FileSamples f;
  3015. std::uint16_t channels = 1;
  3016. for (std::size_t pos = 12; pos + 8 <= w.size();)
  3017. {
  3018. std::uint32_t size = 0;
  3019. std::memcpy (&size, w.data() + pos + 4, 4);
  3020. if (std::memcmp (w.data() + pos, "fmt ", 4) == 0)
  3021. std::memcpy (&channels, w.data() + pos + 10, 2);
  3022. else if (std::memcmp (w.data() + pos, "data", 4) == 0)
  3023. {
  3024. f.samples.resize (std::min<std::size_t> (size, w.size() - pos - 8) / 2);
  3025. std::memcpy (f.samples.data(), w.data() + pos + 8, f.samples.size() * 2);
  3026. f.channels = channels;
  3027. }
  3028. pos += 8 + size + (size & 1);
  3029. }
  3030. return f;
  3031. };
  3032. auto run = [&] (const std::string& name, std::array<int, 15> knobs, std::uint64_t expected) {
  3033. const auto wti = readAll (folder / (name + ".wti"));
  3034. std::vector<std::string> names;
  3035. auto table = parseWti (std::string (wti.begin(), wti.end()), names);
  3036. CHECK (table.has_value());
  3037. if (! table)
  3038. return;
  3039. std::vector<FileSamples> files;
  3040. for (std::size_t i = 0; i < names.size(); ++i)
  3041. {
  3042. auto rel = names[i];
  3043. std::replace (rel.begin(), rel.end(), '\\', '/');
  3044. const auto bytes = readAll (folder / rel);
  3045. if (table->raw)
  3046. {
  3047. FileSamples f;
  3048. f.samples.resize (bytes.size() / 2);
  3049. std::memcpy (f.samples.data(), bytes.data(), f.samples.size() * 2);
  3050. files.push_back (std::move (f));
  3051. }
  3052. else
  3053. {
  3054. table->files[i] = wavFileInfo (bytes);
  3055. files.push_back (pcm (bytes));
  3056. }
  3057. }
  3058. const auto w = buildWavetable (*table, files);
  3059. CHECK (w != nullptr);
  3060. if (w == nullptr)
  3061. return;
  3062. State s;
  3063. s.wavetable = name;
  3064. s.knobs = knobs;
  3065. auto note = [] (int start, int key, double iv, double fv, double fp, double cents) {
  3066. RefNote n;
  3067. n.start = start, n.key = key, n.initVol = iv, n.finalVol = fv, n.finalPan = fp, n.cents = cents;
  3068. return n;
  3069. };
  3070. std::vector<RefNote> notes { note (0, 60, 1, 0.78, 0, 0), note (700, 72, 0.6, 0.5, -0.3, 0), note (1500, 30, 0.2, 0.9, 0.5, 25),
  3071. note (2100, 100, 1.3, 0.7, 1, -40), note (2500, 48, 0.05, 0.8, -1, 0), note (16000, 96, 0.9, 0.8, 0, 0) };
  3072. // (floats as the harness parses them)
  3073. for (auto& n : notes)
  3074. n.initVol = (float) n.initVol, n.finalVol = (float) n.finalVol, n.finalPan = (float) n.finalPan;
  3075. notes[0].release = 9000, notes[2].release = 12000, notes[3].release = 15000, notes[5].release = 20000;
  3076. const auto out = renderReference (*w, s, notes, 44100.0, 24000, 512, true);
  3077. std::uint64_t h = 0xcbf29ce484222325ull;
  3078. for (auto v : out)
  3079. {
  3080. std::uint32_t b;
  3081. std::memcpy (&b, &v, 4);
  3082. for (int k = 0; k < 4; ++k)
  3083. h = (h ^ ((b >> (8 * k)) & 0xff)) * 0x100000001b3ull;
  3084. }
  3085. CHECK (h == expected);
  3086. };
  3087. run ("mda Piano", { 1000, 138, 388, -1000, 716, 0, -1000, 256, 0, 0, 0, 0, 0, 0, 0 }, 0x78268ec007f31309ull);
  3088. run ("Rhodes", { 200, 300, 500, 800, 300, 1500, 300, 1500, 300, 400, 400, 800, 900, 30000, 700 }, 0xcc91c93d917f1d31ull);
  3089. run ("Roto Organ", { 1000, 1000, 1000, 1000, 0, 0, -1000, 0, 1000, 0, 500, 1000, 1000, 0, 300 }, 0x99eb13e87640a1c6ull);
  3090. }
  3091. TEST (sampler_plays_pitched_one_shot)
  3092. {
  3093. // 1000-frame ramp sample: the position can be measured from the amplitude
  3094. auto data = std::make_shared<lynx::SampleData>();
  3095. data->sampleRate = 48000.0;
  3096. for (int i = 0; i < 1000; ++i)
  3097. data->left.push_back (1.0f);
  3098. auto sampler = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  3099. sampler->prepare (48000.0);
  3100. std::vector<float> l (2048, 0.0f), r (2048, 0.0f);
  3101. sampler->noteOn (60, 1.0f);
  3102. sampler->noteOff (60); // one-shot: noteOff doesn't cut
  3103. sampler->render (l.data(), r.data(), 2048, 1.0f, 1.0f);
  3104. int sounding = 0;
  3105. for (auto s : l)
  3106. sounding += s > 0.5f;
  3107. CHECK (sounding >= 995 && sounding <= 1000); // plays the whole sample at the original pitch
  3108. std::fill (l.begin(), l.end(), 0.0f);
  3109. sampler->noteOn (72, 1.0f); // one octave up: half the duration
  3110. sampler->render (l.data(), r.data(), 2048, 1.0f, 1.0f);
  3111. sounding = 0;
  3112. for (auto s : l)
  3113. sounding += s > 0.5f;
  3114. CHECK (sounding >= 495 && sounding <= 500);
  3115. CHECK (r[10] > 0.5f); // mono goes to both sides
  3116. auto silent = lynx::makeInstrument (lynx::InstrumentKind::sampler, nullptr);
  3117. silent->prepare (48000.0);
  3118. silent->noteOn (60, 1.0f);
  3119. std::fill (l.begin(), l.end(), 0.0f);
  3120. silent->render (l.data(), r.data(), 64, 1.0f, 1.0f);
  3121. CHECK (l[0] == 0.0f);
  3122. }
  3123. TEST (rack_live_notes_and_sample_provider)
  3124. {
  3125. lynx::Song song;
  3126. lynx::ChannelDef c;
  3127. c.name = "Smp";
  3128. c.instrument = lynx::InstrumentKind::sampler;
  3129. c.samplePath = "a.wav";
  3130. song.channels.push_back (c);
  3131. song.patterns.emplace_back();
  3132. int loads = 0;
  3133. auto provider = [&] (const std::string& path) -> std::shared_ptr<const lynx::SampleData> {
  3134. ++loads;
  3135. CHECK (path == "a.wav");
  3136. auto d = std::make_shared<lynx::SampleData>();
  3137. d->left.assign (4800, 0.5f);
  3138. return d;
  3139. };
  3140. auto state = lynx::RackState::build (song, nullptr, 48000.0, provider);
  3141. CHECK (loads == 1);
  3142. lynx::RackRenderer renderer;
  3143. renderer.prepare (48000.0);
  3144. lynx::LiveEventQueue live;
  3145. lynx::NoteEvent on;
  3146. on.channel = 0;
  3147. on.velocity = 1.0f;
  3148. live.push (on);
  3149. std::vector<float> l (256), r (256);
  3150. renderer.process (state.get(), false, 0.0, 256, l.data(), r.data(), &live); // stopped, but the live note plays
  3151. CHECK (l[0] > 0.1f);
  3152. // same sample: reused; different sample: reloaded
  3153. auto same = lynx::RackState::build (song, state.get(), 48000.0, provider);
  3154. CHECK (loads == 1 && same->instruments[0] == state->instruments[0]);
  3155. song.channels[0].samplePath = "b.wav";
  3156. auto other = lynx::RackState::build (song, state.get(), 48000.0, [&] (const std::string&) { ++loads; return nullptr; });
  3157. CHECK (loads == 2 && other->instruments[0] != state->instruments[0]);
  3158. }
  3159. TEST (flp_paths_resolve_and_samplers_import)
  3160. {
  3161. lynx::FlPathRoots roots { "C:\\FL", "C:\\FL\\Data", "D:\\User" };
  3162. CHECK (lynx::resolveFlPath ("%FLStudioFactoryData%\\Data\\x.wav", roots) == "C:\\FL\\Data\\x.wav");
  3163. CHECK (lynx::resolveFlPath ("%FLStudioData%\\Patches\\y.wav", roots) == "C:\\FL\\Data\\Patches\\y.wav");
  3164. CHECK (lynx::resolveFlPath ("%FLStudioUserData%\\z.wav", roots) == "D:\\User\\z.wav");
  3165. CHECK (lynx::resolveFlPath ("E:\\abs.wav", roots) == "E:\\abs.wav");
  3166. CHECK (lynx::resolveFlPath ("%FLStudioUserData%\\z.wav", {}) == "");
  3167. {
  3168. // POSIX roots (macOS, Linux, Wine prefix): backslashes become '/', letter case follows the disk (round 117)
  3169. namespace fs = std::filesystem;
  3170. const auto base = fs::temp_directory_path() / ("lynx-case-test-" + processTempTag());
  3171. fs::remove_all (base);
  3172. fs::create_directories (base / "Data" / "Patches" / "Packs");
  3173. { std::ofstream (base / "Data" / "Patches" / "Packs" / "Kick.WAV") << "x"; }
  3174. lynx::FlPathRoots posix;
  3175. posix.factoryData = base.string();
  3176. CHECK (lynx::resolveFlPath ("%FLStudioFactoryData%\\Data\\Patches\\Packs\\Kick.WAV", posix) == (base / "Data/Patches/Packs/Kick.WAV").string());
  3177. const auto lowerCase = lynx::resolveFlPath ("%FLStudioFactoryData%\\data\\patches\\packs\\kick.wav", posix);
  3178. if (fs::exists (base / "data")) // case-insensitive file system (macOS): any spelling opens the file
  3179. CHECK (fs::exists (lowerCase));
  3180. else
  3181. CHECK (lowerCase == (base / "Data/Patches/Packs/Kick.WAV").string());
  3182. CHECK (lynx::resolveFlPath ("%FLStudioFactoryData%\\Data\\missing.wav", posix) == (base / "Data/missing.wav").string());
  3183. fs::remove_all (base);
  3184. }
  3185. flp::Project p;
  3186. p.channels = { { 0, flp::ChannelType::sampler, "Kick", "", "%FLStudioData%\\k.wav", 1 },
  3187. { 1, flp::ChannelType::sampler, "Hat", "", "%FLStudioData%\\missing.wav", 1 } };
  3188. lynx::FlpImportOptions options;
  3189. options.roots = roots;
  3190. options.fileExists = [] (const std::string& path) { return path == "C:\\FL\\Data\\k.wav"; };
  3191. const auto s = lynx::importFlp (p, options);
  3192. CHECK (s.channels[0].instrument == lynx::InstrumentKind::sampler);
  3193. CHECK (s.channels[0].samplePath == "C:\\FL\\Data\\k.wav");
  3194. CHECK (s.channels[1].instrument == lynx::InstrumentKind::hat); // no file: built-in instrument
  3195. }
  3196. TEST (output_protection_is_transparent_up_to_full_scale)
  3197. {
  3198. // round 160: no more soft knee at 0.9 — FL leaves the mix untouched up to 0 dBFS
  3199. float s[] = { 0.5f, -0.89f, 1.5f, -3.0f, 0.95f, 0.999f, std::nanf (""), -1.0f };
  3200. lynx::clipOvers (s, 8);
  3201. CHECK (s[0] == 0.5f && s[1] == -0.89f && s[4] == 0.95f && s[5] == 0.999f && s[7] == -1.0f);
  3202. CHECK (s[2] == 1.0f && s[3] == -1.0f);
  3203. CHECK (s[6] == 0.0f);
  3204. }
  3205. TEST (song_scheduler_plays_clips_with_offset_and_cut)
  3206. {
  3207. lynx::Song s;
  3208. s.channels.resize (1);
  3209. auto& p = s.patterns.emplace_back();
  3210. for (int step : { 0, 4, 8, 12 }) // one note per beat
  3211. p.toggleStep (0, step);
  3212. // clip at bar 2 playing only the second half of the pattern (beats 3 and 4)
  3213. lynx::Clip c;
  3214. c.pattern = 0;
  3215. c.start = 4 * lynx::ppq;
  3216. c.length = 2 * lynx::ppq;
  3217. c.offset = 2 * lynx::ppq;
  3218. s.playlist.push_back (c);
  3219. CHECK (s.songLength() == 6 * lynx::ppq); // FL: the song ends where the last clip ends
  3220. const double sr = 48000.0;
  3221. const auto tps = lynx::ticksPerSample (120.0, sr); // 1 beat = 24000 samples
  3222. lynx::EventBuffer ev;
  3223. // whole song in a single block (8 beats = 192000 samples)
  3224. lynx::scheduleSong (s, 0.0, 192000, tps, ev);
  3225. std::vector<int> ons;
  3226. for (auto& e : ev)
  3227. if (e.type == lynx::NoteEvent::Type::on)
  3228. ons.push_back (e.sampleOffset);
  3229. CHECK (ons.size() == 2);
  3230. CHECK (ons.size() == 2 && ons[0] == 4 * 24000 && ons[1] == 5 * 24000);
  3231. // crossing the loop: bar 1 is empty, so nothing at the start of the second pass
  3232. ev.clear();
  3233. lynx::scheduleSong (s, 8.0 * lynx::ppq - 100 * tps, 200, tps, ev);
  3234. CHECK (ev.size() == 0);
  3235. // a note longer than the clip is cut at the clip end
  3236. p.notes[3].length = 4 * lynx::ppq;
  3237. ev.clear();
  3238. lynx::scheduleSong (s, 0.0, 192000, tps, ev);
  3239. int lastOff = -1;
  3240. for (auto& e : ev)
  3241. if (e.type == lynx::NoteEvent::Type::off)
  3242. lastOff = std::max (lastOff, e.sampleOffset);
  3243. CHECK (lastOff == 6 * 24000);
  3244. }
  3245. TEST (remove_pattern_fixes_clips)
  3246. {
  3247. lynx::Song s;
  3248. s.patterns.resize (3);
  3249. s.currentPattern = 2;
  3250. s.playlist = { { 0, 0, 0, 100, 0 }, { 1, 0, 100, 100, 0 }, { 2, 1, 0, 100, 0 } };
  3251. s.removePattern (1);
  3252. CHECK (s.patterns.size() == 2);
  3253. CHECK (s.playlist.size() == 2 && s.playlist[1].pattern == 1);
  3254. CHECK (s.currentPattern == 1);
  3255. }
  3256. TEST (flp_import_3x_osc_state_and_channel_filter)
  3257. {
  3258. // new 3x Osc patch (version 14): osc 2 mix 64 coarse -12, osc 3 mix 32 coarse -24
  3259. std::vector<std::uint8_t> blob (460, 0);
  3260. auto put = [&] (std::size_t off, std::int32_t v) {
  3261. for (int b = 0; b < 4; ++b)
  3262. blob[off + (std::size_t) b] = (std::uint8_t) ((std::uint32_t) v >> (8 * b));
  3263. };
  3264. put (0x00, 14);
  3265. put (0x08, 3); // osc 1 saw
  3266. put (0x10, 7); // osc 1 fine +7
  3267. put (0x04, -32); // osc 1 pan -0.5
  3268. put (0x1C, 64); // osc 2 mix
  3269. put (0x28, -12); // osc 2 coarse
  3270. put (0x38, 32); // osc 3 mix
  3271. put (0x40, 2); // osc 3 square
  3272. put (0x44, -19); // osc 3 coarse: -1 octave and -7 semitones
  3273. lynx::SynthSettings s;
  3274. CHECK (lynx::synthFrom3xOsc (blob, s));
  3275. 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);
  3276. CHECK (s.osc[0].wave == lynx::Waveform::saw && s.osc[0].detune == 7.0f && s.osc[0].pan == -0.5f);
  3277. CHECK (s.osc[1].wave == lynx::Waveform::sine && s.osc[1].octave == -1 && s.osc[1].semitones == 0);
  3278. CHECK (s.osc[2].wave == lynx::Waveform::square && s.osc[2].octave == -1 && s.osc[2].semitones == -7);
  3279. CHECK (! lynx::synthFrom3xOsc (std::vector<std::uint8_t> (40, 0), s));
  3280. // channel filter: open by default; LP with Mod X 128 ~ 2.71 kHz; Fast LP with Mod X 128 ~ 280 Hz
  3281. float hz = 0.0f, res = 1.0f;
  3282. int mode = -1;
  3283. lynx::synthFilterFromChannel (256, 0, 0, hz, res, mode);
  3284. CHECK (hz >= 19999.0f && res == 0.0f);
  3285. lynx::synthFilterFromChannel (128, 0, 1, hz, res, mode);
  3286. CHECK (std::abs (hz - 2710.0f) < 30.0f && res == 0.0f);
  3287. lynx::synthFilterFromChannel (128, 0, 0, hz, res, mode);
  3288. CHECK (hz > 240.0f && hz < 320.0f);
  3289. lynx::synthFilterFromChannel (128, 256, 1, hz, res, mode);
  3290. CHECK (res > 0.8f);
  3291. CHECK (mode == 0);
  3292. lynx::synthFilterFromChannel (64, 0, 3, hz, res, mode); // high-pass at ~840 Hz
  3293. CHECK (mode == 2 && std::abs (hz - 840.0f) < 40.0f);
  3294. lynx::synthFilterFromChannel (128, 0, 2, hz, res, mode); // band-pass
  3295. CHECK (mode == 1);
  3296. }
  3297. TEST (synth_pitch_and_filter_envelopes)
  3298. {
  3299. auto render = [] (const lynx::SynthSettings& s, int frames) {
  3300. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  3301. inst->prepare (48000.0);
  3302. lynx::ChannelDef c;
  3303. c.synth = s;
  3304. inst->configure (c, 120.0);
  3305. inst->noteOn (45, 1.0f); // A2 = 110 Hz
  3306. std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
  3307. inst->render (l.data(), r.data(), frames, 1.0f, 1.0f);
  3308. return l;
  3309. };
  3310. auto crossings = [] (const std::vector<float>& b, int from, int to) {
  3311. int n = 0;
  3312. for (int i = from + 1; i < to; ++i)
  3313. n += (b[(std::size_t) i - 1] < 0.0f) != (b[(std::size_t) i] < 0.0f);
  3314. return n;
  3315. };
  3316. // 3x Osc "kick": sine with a +24 st pitch envelope falling fast
  3317. lynx::SynthSettings kick;
  3318. kick.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  3319. kick.cutoff = 20000.0f;
  3320. kick.pitchEnv = { 24.0f, 0.0f, 0.0f, 0.05f, 0.0f, 0.05f };
  3321. const auto k = render (kick, 48000);
  3322. const auto early = crossings (k, 0, 2400); // first 50 ms: well above 110 Hz
  3323. const auto late = crossings (k, 24000, 48000); // then back to 110 Hz (~110 crossings in 0.5 s)
  3324. CHECK (early > 20); // +24 st ramp over 50 ms: ~2.2x the average frequency (11 crossings without envelope)
  3325. CHECK (std::abs (late - 110) < 4);
  3326. // cutoff envelope: saw closed at 200 Hz opens +5 octaves on attack and closes again
  3327. lynx::SynthSettings pluck;
  3328. pluck.cutoff = 200.0f;
  3329. pluck.filterEnv = { 5.0f, 0.0f, 0.0f, 0.1f, 0.0f, 0.1f };
  3330. const auto p = render (pluck, 48000);
  3331. auto hf = [&] (int from, int to) { // energy of the difference (highs)
  3332. double e = 0.0;
  3333. for (int i = from + 1; i < to; ++i)
  3334. e += std::pow ((double) p[(std::size_t) i] - p[(std::size_t) i - 1], 2.0);
  3335. return e;
  3336. };
  3337. CHECK (hf (0, 2400) > 4.0 * hf (40000, 42400));
  3338. // Toxic Biohazard's envelope (REA_rodada33 §2): exponential towards 5 clipped at 1, so the cutoff is still near
  3339. // its base early in the attack, fully open at A = 0.2 s and back down after the exponential decay
  3340. lynx::SynthSettings toxic = pluck;
  3341. toxic.filterEnv = { 5.0f, 0.2f, 0.0f, 0.1f, 0.0f, 0.1f, 1 };
  3342. const auto t = render (toxic, 48000);
  3343. auto thf = [&] (int from, int to) {
  3344. double e = 0.0;
  3345. for (int i = from + 1; i < to; ++i)
  3346. e += std::pow ((double) t[(std::size_t) i] - t[(std::size_t) i - 1], 2.0);
  3347. return e;
  3348. };
  3349. CHECK (thf (9000, 9600) > 4.0 * thf (300, 900));
  3350. CHECK (thf (9000, 9600) > 4.0 * thf (40000, 40600));
  3351. }
  3352. TEST (synth_mono_reuses_the_sounding_voice)
  3353. {
  3354. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  3355. inst->prepare (48000.0);
  3356. lynx::ChannelDef c;
  3357. c.synth.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  3358. c.synth.cutoff = 20000.0f;
  3359. c.synth.mono = true;
  3360. inst->configure (c, 120.0);
  3361. std::vector<float> l (48000), r (48000);
  3362. inst->noteOn (57, 1.0f);
  3363. inst->render (l.data(), r.data(), 4800, 1.0f, 1.0f);
  3364. inst->noteOn (69, 1.0f); // A4 takes over the A3 voice: only one note sounding
  3365. inst->render (l.data() + 4800, r.data() + 4800, 43200, 1.0f, 1.0f);
  3366. int crossings = 0;
  3367. float peak = 0.0f;
  3368. for (std::size_t i = 24001; i < 48000; ++i)
  3369. {
  3370. crossings += (l[i - 1] < 0.0f) != (l[i] < 0.0f);
  3371. peak = std::max (peak, std::abs (l[i]));
  3372. }
  3373. CHECK (std::abs (crossings - 440) < 4); // 0.5 s of 440 Hz, without the A3 underneath
  3374. CHECK (peak < 0.35f); // one voice (0.3 level), not two summed
  3375. // no click on the handover: the jump between neighbouring samples stays sine-sized
  3376. float jump = 0.0f;
  3377. for (std::size_t i = 4790; i < 4810; ++i)
  3378. jump = std::max (jump, std::abs (l[i] - l[i - 1]));
  3379. CHECK (jump < 0.05f);
  3380. }
  3381. TEST (synth_porta_glides_and_am_modulates)
  3382. {
  3383. auto make = [] (const lynx::SynthSettings& s) {
  3384. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  3385. inst->prepare (48000.0);
  3386. lynx::ChannelDef c;
  3387. c.synth = s;
  3388. inst->configure (c, 120.0);
  3389. return inst;
  3390. };
  3391. auto crossings = [] (const std::vector<float>& b, std::size_t from, std::size_t to) {
  3392. int n = 0;
  3393. for (auto i = from + 1; i < to; ++i)
  3394. n += (b[i - 1] < 0.0f) != (b[i] < 0.0f);
  3395. return n;
  3396. };
  3397. // mono + 1-beat porta (0.5 s at 120 BPM): from A3 to A4, halfway ~E4 (329.6 Hz)
  3398. lynx::SynthSettings s;
  3399. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  3400. s.cutoff = 20000.0f;
  3401. s.mono = true;
  3402. s.porta = true;
  3403. s.portaBeats = 1.0f;
  3404. auto inst = make (s);
  3405. std::vector<float> l (96000), r (96000);
  3406. inst->noteOn (57, 1.0f);
  3407. inst->render (l.data(), r.data(), 4800, 1.0f, 1.0f);
  3408. inst->noteOn (69, 1.0f);
  3409. inst->render (l.data() + 4800, r.data() + 4800, 91200, 1.0f, 1.0f);
  3410. const auto middle = crossings (l, 4800 + 12000 - 2400, 4800 + 12000 + 2400); // 0.1 s around 0.25 s
  3411. CHECK (std::abs (middle - 66) < 6); // ~330 Hz * 0.1 s * 2
  3412. CHECK (std::abs (crossings (l, 48000, 96000) - 880) < 4); // reached A4 (1 s = 880 crossings)
  3413. // AM: osc 3 at level 1.0, slow sine, modulates osc 1 (the average level drops, the sum doesn't gain osc 3)
  3414. lynx::SynthSettings am;
  3415. am.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  3416. am.osc[2] = { lynx::Waveform::sine, -3, 0.0f, 1.0f };
  3417. am.cutoff = 20000.0f;
  3418. am.am = true;
  3419. auto modulated = make (am);
  3420. modulated->noteOn (69, 1.0f);
  3421. std::fill (l.begin(), l.end(), 0.0f);
  3422. modulated->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
  3423. float peak = 0.0f;
  3424. for (std::size_t i = 4800; i < 48000; ++i)
  3425. peak = std::max (peak, std::abs (l[i]));
  3426. CHECK (peak > 0.45f && peak < 0.65f); // 0.3 · (1 + 1) at most: (1 − o3) goes up to 2
  3427. }
  3428. TEST (channel_lfo_vibrato_and_tremolo)
  3429. {
  3430. auto render = [] (const lynx::SynthSettings& s) {
  3431. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  3432. inst->prepare (48000.0);
  3433. lynx::ChannelDef c;
  3434. c.synth = s;
  3435. inst->configure (c, 120.0);
  3436. inst->noteOn (69, 1.0f);
  3437. std::vector<float> l (48000), r (48000);
  3438. inst->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
  3439. return l;
  3440. };
  3441. lynx::SynthSettings s;
  3442. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  3443. s.cutoff = 20000.0f;
  3444. // tremolo: 100% volume LFO at 4 Hz (0.25 s period) -> the amplitude envelope goes from ~0 to ~2x
  3445. auto tremolo = s;
  3446. tremolo.lfo[lynx::lfoVolume] = { 1.0f, 0.25f, false, 0, 0.0f, 0.0f };
  3447. const auto t = render (tremolo);
  3448. float lowest = 1.0f, highest = 0.0f;
  3449. for (std::size_t w = 4800; w + 480 <= 48000; w += 480) // 10 ms windows (4.4 cycles of 440 Hz)
  3450. {
  3451. float peak = 0.0f;
  3452. for (auto i = w; i < w + 480; ++i)
  3453. peak = std::max (peak, std::abs (t[i]));
  3454. lowest = std::min (lowest, peak);
  3455. highest = std::max (highest, peak);
  3456. }
  3457. CHECK (lowest < 0.08f && highest > 0.5f);
  3458. // synced vibrato: ±1 semitone, 1-beat period (0.5 s at 120 BPM); the average stays at 440 Hz
  3459. auto vibrato = s;
  3460. vibrato.lfo[lynx::lfoPitch] = { 1.0f, 1.0f, true, 0, 0.0f, 0.0f };
  3461. const auto v = render (vibrato);
  3462. int crossings = 0;
  3463. for (std::size_t i = 1; i < 48000; ++i)
  3464. crossings += (v[i - 1] < 0.0f) != (v[i] < 0.0f);
  3465. CHECK (std::abs (crossings - 880) < 12);
  3466. int firstQuarter = 0; // first quarter of the cycle: rising sine -> higher than 440
  3467. for (std::size_t i = 1; i < 6000; ++i)
  3468. firstQuarter += (v[i - 1] < 0.0f) != (v[i] < 0.0f);
  3469. CHECK (firstQuarter > 112);
  3470. }
  3471. TEST (sampler_porta_glides_from_previous_note)
  3472. {
  3473. auto data = std::make_shared<lynx::SampleData>();
  3474. data->sampleRate = 48000.0;
  3475. data->left.resize (96000);
  3476. for (std::size_t i = 0; i < data->left.size(); ++i)
  3477. data->left[i] = 0.5f * (float) std::sin (6.283185307 * 220.0 * (double) i / 48000.0);
  3478. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  3479. inst->prepare (48000.0);
  3480. lynx::ChannelDef c;
  3481. c.sampler.cut = true;
  3482. c.sampler.porta = true;
  3483. c.sampler.portaBeats = 1.0f; // 0.5 s at 120 BPM
  3484. inst->configure (c, 120.0);
  3485. std::vector<float> l (24000), r (24000);
  3486. inst->noteOn (60, 1.0f);
  3487. inst->render (l.data(), r.data(), 2400, 1.0f, 1.0f);
  3488. inst->noteOn (72, 1.0f); // one octave up: starts at 220 Hz and rises
  3489. std::fill (l.begin(), l.end(), 0.0f);
  3490. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  3491. auto crossings = [&] (std::size_t from, std::size_t to) {
  3492. int n = 0;
  3493. for (auto i = from + 1; i < to; ++i)
  3494. n += (l[i - 1] < 0.0f) != (l[i] < 0.0f);
  3495. return n;
  3496. };
  3497. const auto start = crossings (600, 2400); // 37.5 ms still near 220 Hz: ~17 crossings (440 Hz would give 33)
  3498. const auto end = crossings (21600, 24000); // 50 ms at the end of the glide: near 440 Hz (~44; 220 Hz would give 22)
  3499. CHECK (start < 22);
  3500. CHECK (end > 36);
  3501. }
  3502. TEST (zoned_sampler_picks_zone_by_key_and_velocity)
  3503. {
  3504. auto sine = [] (double hz, float amp) {
  3505. auto d = std::make_shared<lynx::SampleData>();
  3506. d->sampleRate = 48000.0;
  3507. d->left.resize (48000);
  3508. for (std::size_t i = 0; i < d->left.size(); ++i)
  3509. d->left[i] = amp * (float) std::sin (6.283185307 * hz * (double) i / 48000.0);
  3510. return d;
  3511. };
  3512. // lows: 220 Hz sine recorded as A3 (57), up to key 69; highs: 880 Hz as A5 (81)
  3513. // hard layer on the highs: same tuning, louder
  3514. std::vector<lynx::LoadedZone> zones {
  3515. { { "graves.wav", 57, 0, 69, 0, 127, 0.0f, 1.0f }, sine (220.0, 0.5f) },
  3516. { { "agudos.wav", 81, 70, 127, 0, 99, 0.0f, 1.0f }, sine (880.0, 0.25f) },
  3517. { { "agudos forte.wav", 81, 70, 127, 100, 127, 0.0f, 1.0f }, sine (880.0, 0.9f) },
  3518. };
  3519. auto inst = lynx::makeZonedSampler (zones);
  3520. inst->prepare (48000.0);
  3521. inst->configure (lynx::ChannelDef {}, 120.0);
  3522. auto play = [&] (int key, float velocity) {
  3523. inst->allNotesOff();
  3524. std::vector<float> l (24000), r (24000);
  3525. inst->render (l.data(), r.data(), 2000, 1.0f, 1.0f); // let the old voices die out
  3526. std::fill (l.begin(), l.end(), 0.0f);
  3527. inst->noteOn (key, velocity);
  3528. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  3529. int crossings = 0;
  3530. float peak = 0.0f;
  3531. for (std::size_t i = 1; i < 24000; ++i)
  3532. {
  3533. crossings += (l[i - 1] < 0.0f) != (l[i] < 0.0f);
  3534. peak = std::max (peak, std::abs (l[i]));
  3535. }
  3536. return std::pair { crossings, peak };
  3537. };
  3538. const auto low = play (69, 0.5f); // A4 from root A3: 440 Hz -> 440 crossings in 0.5 s
  3539. CHECK (std::abs (low.first - 440) < 4);
  3540. const auto soft = play (81, 0.5f); // high zone at the root: 880 Hz
  3541. CHECK (std::abs (soft.first - 880) < 6);
  3542. const auto hard = play (81, 0.95f); // high velocity: hard layer
  3543. CHECK (hard.second > soft.second * 2.0f);
  3544. // overlapping layers round-robin: two identical zones at different levels, notes alternate
  3545. auto rr = lynx::makeZonedSampler ({ { { "a.wav", 60 }, sine (440.0, 0.2f) }, { { "b.wav", 60 }, sine (440.0, 0.8f) } });
  3546. rr->prepare (48000.0);
  3547. rr->configure (lynx::ChannelDef {}, 120.0);
  3548. float peaks[2] {};
  3549. for (int k = 0; k < 2; ++k)
  3550. {
  3551. rr->allNotesOff();
  3552. std::vector<float> l (4800), r (4800);
  3553. rr->render (l.data(), r.data(), 2000, 1.0f, 1.0f);
  3554. std::fill (l.begin(), l.end(), 0.0f);
  3555. rr->noteOn (60, 1.0f);
  3556. rr->render (l.data(), r.data(), 4800, 1.0f, 1.0f);
  3557. for (auto v : l)
  3558. peaks[k] = std::max (peaks[k], std::abs (v));
  3559. }
  3560. CHECK (std::abs (peaks[0] - peaks[1]) > 0.3f);
  3561. }
  3562. TEST (zoned_sampler_loops_while_held)
  3563. {
  3564. // 0.1 s sample with a loop in the middle: holding the note it keeps sounding well past the end of the file;
  3565. // on release, it leaves the loop and ends
  3566. auto data = std::make_shared<lynx::SampleData>();
  3567. data->sampleRate = 48000.0;
  3568. data->left.resize (4800);
  3569. for (std::size_t i = 0; i < data->left.size(); ++i)
  3570. data->left[i] = 0.5f * (float) std::sin (6.283185307 * 480.0 * (double) i / 48000.0); // 100 frames per cycle
  3571. lynx::SampleZone zone { "loop.wav", 60 };
  3572. zone.loopStart = 1000;
  3573. zone.loopEnd = 3000;
  3574. zone.loopMode = 2;
  3575. auto inst = lynx::makeZonedSampler ({ { zone, data } });
  3576. inst->prepare (48000.0);
  3577. lynx::ChannelDef c;
  3578. c.sampler.release = 0.01f;
  3579. inst->configure (c, 120.0);
  3580. inst->noteOn (60, 1.0f);
  3581. std::vector<float> l (48000), r (48000);
  3582. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f); // 0.5 s held
  3583. float held = 0.0f;
  3584. for (std::size_t i = 20000; i < 24000; ++i)
  3585. held = std::max (held, std::abs (l[i]));
  3586. CHECK (held > 0.3f);
  3587. inst->noteOff (60);
  3588. inst->render (l.data() + 24000, r.data() + 24000, 24000, 1.0f, 1.0f);
  3589. float after = 0.0f;
  3590. for (std::size_t i = 40000; i < 48000; ++i)
  3591. after = std::max (after, std::abs (l[i]));
  3592. CHECK (after == 0.0f);
  3593. }
  3594. TEST (global_lfo_follows_song_position)
  3595. {
  3596. // global volume LFO (1-beat period): a note mid-cycle starts mid-wave; the
  3597. // same note with a per-note LFO starts at phase 0
  3598. auto first = [] (bool global, double songTicks) {
  3599. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  3600. inst->prepare (48000.0);
  3601. lynx::ChannelDef c;
  3602. c.synth.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  3603. c.synth.cutoff = 20000.0f;
  3604. c.synth.lfo[lynx::lfoVolume] = { 1.0f, 1.0f, true, 2, 0.0f, 0.0f, global }; // pulse: +1 in the first half
  3605. inst->configure (c, 120.0);
  3606. inst->setSongPosition (songTicks);
  3607. inst->noteOn (69, 1.0f);
  3608. std::vector<float> l (4800), r (4800);
  3609. inst->render (l.data(), r.data(), 4800, 1.0f, 1.0f);
  3610. float peak = 0.0f;
  3611. for (std::size_t i = 2400; i < 4800; ++i)
  3612. peak = std::max (peak, std::abs (l[i]));
  3613. return peak;
  3614. };
  3615. // mid-cycle (phase 0.6) the pulse is at −1: volume ~0; per note (phase 0) it is at +1: ~2x
  3616. const auto globalLate = first (true, 0.6 * lynx::ppq);
  3617. const auto perNote = first (false, 0.6 * lynx::ppq);
  3618. CHECK (globalLate < 0.05f);
  3619. CHECK (perNote > 0.4f);
  3620. }
  3621. TEST (sampler_channel_filter_darkens_and_bypasses_when_open)
  3622. {
  3623. // white noise in the sampler: with a 300 Hz cutoff the energy drops a lot; open passes unchanged
  3624. auto data = std::make_shared<lynx::SampleData>();
  3625. data->sampleRate = 48000.0;
  3626. data->left.resize (48000);
  3627. std::uint32_t seed = 7;
  3628. for (auto& v : data->left)
  3629. {
  3630. seed = seed * 1664525u + 1013904223u;
  3631. v = ((float) (seed >> 8) / 16777216.0f - 0.5f);
  3632. }
  3633. data->right = data->left;
  3634. auto energy = [&] (float cutoff) {
  3635. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  3636. inst->prepare (48000.0);
  3637. lynx::ChannelDef c;
  3638. c.sampler.cutoff = cutoff;
  3639. inst->configure (c, 120.0);
  3640. inst->noteOn (60, 1.0f);
  3641. std::vector<float> l (24000), r (24000);
  3642. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  3643. double e = 0.0;
  3644. for (auto v : l)
  3645. e += (double) v * v;
  3646. return e;
  3647. };
  3648. const auto open = energy (20000.0f), dark = energy (300.0f);
  3649. {
  3650. // high-pass at 10 kHz: also removes most of the noise energy, but from the other side
  3651. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  3652. inst->prepare (48000.0);
  3653. lynx::ChannelDef hp;
  3654. hp.sampler.cutoff = 10000.0f;
  3655. hp.sampler.filterType = 2;
  3656. inst->configure (hp, 120.0);
  3657. inst->noteOn (60, 1.0f);
  3658. std::vector<float> l (24000), r (24000);
  3659. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  3660. double e = 0.0, lowE = 0.0;
  3661. float lp = 0.0f;
  3662. for (auto v : l)
  3663. {
  3664. e += (double) v * v;
  3665. lp += 0.01f * (v - lp); // crude low-pass: almost nothing should remain below
  3666. lowE += (double) lp * lp;
  3667. }
  3668. CHECK (e > 10.0 && e < 0.7 * energy (20000.0f));
  3669. CHECK (lowE < e * 0.01);
  3670. }
  3671. CHECK (open > 100.0);
  3672. CHECK (dark < open * 0.05);
  3673. // +12 st pitch envelope with sustain 1: the sample runs at double speed
  3674. // and ends in half the time (1 s of sample -> 0.5 s)
  3675. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  3676. inst->prepare (48000.0);
  3677. lynx::ChannelDef c;
  3678. c.sampler.pitchEnv = { 12.0f, 0.0f, 0.0f, 0.1f, 1.0f, 0.1f };
  3679. inst->configure (c, 120.0);
  3680. inst->noteOn (60, 1.0f);
  3681. std::vector<float> l (48000), r (48000);
  3682. inst->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
  3683. float tail = 0.0f, body = 0.0f;
  3684. for (std::size_t i = 20000; i < 23000; ++i)
  3685. body = std::max (body, std::abs (l[i]));
  3686. for (std::size_t i = 25000; i < 48000; ++i)
  3687. tail = std::max (tail, std::abs (l[i]));
  3688. CHECK (body > 0.1f);
  3689. CHECK (tail == 0.0f);
  3690. }
  3691. TEST (synth_oscillator_pan_and_semitones)
  3692. {
  3693. auto render = [] (lynx::SynthSettings s, std::vector<float>& l, std::vector<float>& r) {
  3694. auto synth = lynx::makeInstrument (lynx::InstrumentKind::synth);
  3695. synth->prepare (48000.0);
  3696. lynx::ChannelDef c;
  3697. c.synth = s;
  3698. synth->configure (c, 120.0);
  3699. synth->noteOn (69, 1.0f);
  3700. l.assign (24000, 0.0f);
  3701. r.assign (24000, 0.0f);
  3702. synth->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  3703. };
  3704. lynx::SynthSettings s;
  3705. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f, 7, 1.0f }; // a fifth up, fully right
  3706. s.cutoff = 20000.0f;
  3707. std::vector<float> l, r;
  3708. render (s, l, r);
  3709. float peakL = 0.0f, peakR = 0.0f;
  3710. int crossings = 0;
  3711. for (std::size_t i = 4800; i < 24000; ++i)
  3712. {
  3713. peakL = std::max (peakL, std::abs (l[i]));
  3714. peakR = std::max (peakR, std::abs (r[i]));
  3715. crossings += (r[i - 1] < 0.0f) != (r[i] < 0.0f);
  3716. }
  3717. CHECK (peakL < 1e-4f && peakR > 0.1f);
  3718. // 440 * 2^(7/12) = 659.3 Hz; 0.4 s = 263.7 cycles = ~527 crossings
  3719. CHECK (std::abs (crossings - 527) < 6);
  3720. }
  3721. TEST (fl_wrapper_state_parses_records_and_vst3_state)
  3722. {
  3723. // Fruity Wrapper blob (version 12): VST3 generator type, class ID, name, path and state
  3724. std::vector<std::uint8_t> blob;
  3725. 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))); };
  3726. auto record = [&] (std::vector<std::uint8_t>& b, std::uint32_t id, const std::vector<std::uint8_t>& payload) {
  3727. u32 (b, id);
  3728. u32 (b, (std::uint32_t) payload.size());
  3729. u32 (b, 0);
  3730. b.insert (b.end(), payload.begin(), payload.end());
  3731. };
  3732. auto bytes = [] (const std::string& s) { return std::vector<std::uint8_t> (s.begin(), s.end()); };
  3733. std::vector<std::uint8_t> vst3State;
  3734. u32 (vst3State, 1);
  3735. record (vst3State, 1, std::vector<std::uint8_t> (64, 0));
  3736. record (vst3State, 3, bytes ("COMPONENTE"));
  3737. record (vst3State, 2, bytes ("CONTROLADOR"));
  3738. // record 4: 3 slots → ParamIDs 700, −1 (empty), 0xFFFFFF01
  3739. std::vector<std::uint8_t> slots;
  3740. for (auto v : { 3u, 700u, 0xFFFFFFFFu, 0xFFFFFF01u })
  3741. u32 (slots, v);
  3742. record (vst3State, 4, slots);
  3743. u32 (blob, 12);
  3744. record (blob, 0x01, std::vector<std::uint8_t> (20, 0xFF));
  3745. record (blob, 0x32, { 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });
  3746. std::vector<std::uint8_t> cid;
  3747. for (int i = 0; i < 16; ++i)
  3748. cid.push_back ((std::uint8_t) (0xA0 + i));
  3749. record (blob, 0x34, cid);
  3750. record (blob, 0x36, bytes ("Surge XT"));
  3751. record (blob, 0x37, bytes ("C:/Program Files/Common Files/VST3/Surge XT.vst3"));
  3752. record (blob, 0x35, vst3State);
  3753. lynx::ForeignPlugin p;
  3754. CHECK (lynx::parseFlWrapper (blob, p));
  3755. CHECK (p.format == lynx::ForeignPlugin::Format::vst3);
  3756. CHECK (p.hasClassId && p.classId[0] == 0xA0 && p.classId[15] == 0xAF);
  3757. CHECK (p.name == "Surge XT" && p.path.find ("Surge XT.vst3") != std::string::npos);
  3758. std::vector<std::uint8_t> component, controller;
  3759. CHECK (lynx::splitFlVst3State (p.state, component, controller));
  3760. CHECK (component == bytes ("COMPONENTE") && controller == bytes ("CONTROLADOR"));
  3761. const auto saved = lynx::flVst3ParamSlots (p.state);
  3762. CHECK (saved == (std::vector<std::int32_t> { 700, -1, (std::int32_t) 0xFFFFFF01u }));
  3763. // FL's slot assignment: saved slots keep their ParamID wherever it now sits; the empty slot stays empty;
  3764. // a vanished ParamID (0xFFFFFF01) leaves its slot dead; parameters without a slot get slots 3, 4… in list order
  3765. const auto positions = lynx::flVst3SlotPositions (saved, { 5u, 700u, 9u });
  3766. CHECK (positions == (std::vector<int> { 1, -1, -1, 0, 2 }));
  3767. CHECK (lynx::flVst3SlotPositions ({}, { 5u, 6u }) == (std::vector<int> { 0, 1 })); // new instance: slot = position
  3768. // VST2 state (REA_rodada89 §2): chunk and raw-parameter layouts, current (−9) and older versions
  3769. {
  3770. lynx::FlVst2State st;
  3771. st.flags = 8;
  3772. st.currentProgram = 3;
  3773. st.chunk = bytes ("BANKCHUNK");
  3774. const auto withChunk = lynx::writeFlVst2State (st);
  3775. lynx::FlVst2State back;
  3776. CHECK (lynx::parseFlVst2State (withChunk, back));
  3777. CHECK (back.version == -9 && back.flags == 8 && back.currentProgram == 3 && back.chunk == bytes ("BANKCHUNK") && back.params.empty());
  3778. CHECK (! back.chunkIsProgram());
  3779. lynx::FlVst2State raw;
  3780. raw.flags = 1;
  3781. raw.params = { 0.25f, 0.5f, 1.0f };
  3782. raw.programNames = { "Init" };
  3783. const auto withParams = lynx::writeFlVst2State (raw);
  3784. CHECK (withParams.size() == 4 + 1 + 4 + 8 + 4 + 4 + 12 + 4 + 25);
  3785. CHECK (lynx::parseFlVst2State (withParams, back));
  3786. CHECK (back.chunk.empty() && back.params == raw.params && back.allPrograms() && back.programNames == std::vector<std::string> { "Init" });
  3787. // version −5: no flags, i32 size, then i32 program; −6 without "all programs": the chunk is one program
  3788. std::vector<std::uint8_t> v5;
  3789. for (auto v : { 0xFFFFFFFBu, 4u, 7u })
  3790. u32 (v5, v);
  3791. for (auto c : bytes ("ABCD"))
  3792. v5.push_back (c);
  3793. CHECK (lynx::parseFlVst2State (v5, back) && back.version == -5 && back.currentProgram == 7 && back.chunk == bytes ("ABCD"));
  3794. std::vector<std::uint8_t> v6;
  3795. u32 (v6, 0xFFFFFFFAu);
  3796. v6.push_back (0);
  3797. for (auto v : { 2u, 0u })
  3798. u32 (v6, v);
  3799. v6.push_back ('x');
  3800. v6.push_back ('y');
  3801. CHECK (lynx::parseFlVst2State (v6, back) && back.chunkIsProgram() && back.chunk == bytes ("xy"));
  3802. // nothing to restore (123456), unknown old layouts, truncated chunks
  3803. std::vector<std::uint8_t> marker;
  3804. u32 (marker, 123456u);
  3805. CHECK (! lynx::parseFlVst2State (marker, back));
  3806. auto cut = withChunk;
  3807. cut.pop_back();
  3808. CHECK (! lynx::parseFlVst2State (cut, back));
  3809. CHECK (! lynx::parseFlVst2State ({}, back));
  3810. }
  3811. // version out of range or no type: not a usable wrapper
  3812. blob[0] = 13;
  3813. CHECK (! lynx::parseFlWrapper (blob, p));
  3814. CHECK (! lynx::parseFlWrapper ({ 12, 0, 0, 0 }, p));
  3815. }
  3816. TEST (schedule_index_matches_full_scan)
  3817. {
  3818. // random song (long notes, swing, clips with offset); indexed scheduling must yield
  3819. // exactly the same events as the full scan, including in the block that wraps around
  3820. lynx::Song s = lynx::Song::makeDefault();
  3821. s.swing = 0.6f;
  3822. std::uint32_t seed = 12345;
  3823. auto rnd = [&] (int n) { seed = seed * 1664525u + 1013904223u; return (int) ((seed >> 8) % (std::uint32_t) n); };
  3824. s.patterns.resize (3);
  3825. for (auto& p : s.patterns)
  3826. {
  3827. p.notes.clear();
  3828. for (int i = 0; i < 60; ++i)
  3829. {
  3830. lynx::Note n;
  3831. n.start = rnd (16 * lynx::ppq);
  3832. n.length = rnd (5) == 0 ? rnd (12 * lynx::ppq) + 1 : rnd (lynx::ppq) + 1;
  3833. n.channel = rnd (4);
  3834. n.key = 40 + rnd (40);
  3835. n.slide = rnd (20) == 0;
  3836. p.notes.push_back (n);
  3837. }
  3838. p.sortNotes();
  3839. }
  3840. s.playlist.clear();
  3841. for (int i = 0; i < 12; ++i)
  3842. {
  3843. lynx::Clip c;
  3844. c.pattern = rnd (3);
  3845. c.track = rnd (4);
  3846. c.start = rnd (40) * lynx::ppq;
  3847. c.length = (rnd (12) + 1) * lynx::ppq;
  3848. c.offset = rnd (4) * lynx::ppq;
  3849. s.playlist.push_back (c);
  3850. }
  3851. s.mode = lynx::PlayMode::song;
  3852. const auto index = lynx::makeScheduleIndex (s);
  3853. const auto tps = lynx::ticksPerSample (140.0, 48000.0);
  3854. const auto loop = (double) s.songLength();
  3855. int mismatches = 0, events = 0;
  3856. for (int block = 0; block < 400; ++block)
  3857. {
  3858. const auto start = block == 0 ? loop - 100.0 * tps : (double) rnd ((int) loop * 2);
  3859. const auto samples = 64 + rnd (24000); // up to ~1 beat per block
  3860. lynx::EventBuffer a, b;
  3861. lynx::scheduleSong (s, start, samples, tps, a, false, nullptr);
  3862. lynx::scheduleSong (s, start, samples, tps, b, false, &index);
  3863. events += a.size();
  3864. if (a.size() != b.size())
  3865. {
  3866. ++mismatches;
  3867. continue;
  3868. }
  3869. for (int i = 0; i < a.size(); ++i)
  3870. {
  3871. const auto& x = *(a.begin() + i);
  3872. const auto& y = *(b.begin() + i);
  3873. mismatches += x.sampleOffset != y.sampleOffset || x.type != y.type || x.key != y.key || x.channel != y.channel;
  3874. }
  3875. }
  3876. CHECK (events > 500);
  3877. CHECK (mismatches == 0);
  3878. }
  3879. TEST (mixer_track_eq_defaults_and_boost)
  3880. {
  3881. // FL defaults: 90 Hz, 1.5 kHz and 8 kHz; peak Q 0.632; shelf slope 0.607
  3882. lynx::TrackEq eq;
  3883. CHECK (std::abs (lynx::TrackEq::peakQ (eq.width[1]) - 0.632) < 0.01);
  3884. CHECK (std::abs (lynx::TrackEq::shelfSlope (eq.width[0]) - 0.607) < 0.01);
  3885. CHECK (! eq.active());
  3886. // +12 dB peak at 1.5 kHz on a track: a 1.5 kHz sine rises ~4x
  3887. lynx::Song song = lynx::Song::makeDefault();
  3888. song.mixer[1].eq.gainDb[1] = 12.0f;
  3889. for (auto& c : song.channels)
  3890. c.mixerTrack = 1;
  3891. song.channels[4].instrument = lynx::InstrumentKind::synth;
  3892. song.channels[4].synth = {};
  3893. song.channels[4].synth.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  3894. song.channels[4].synth.cutoff = 20000.0f;
  3895. auto render = [] (const lynx::Song& s) {
  3896. auto state = lynx::RackState::build (s, nullptr, 48000.0);
  3897. lynx::RackRenderer renderer;
  3898. renderer.prepare (48000.0);
  3899. lynx::LiveEventQueue live;
  3900. lynx::NoteEvent on; // 1.5 kHz ~ key 90.6: use 91 (1568 Hz), still within the wide peak
  3901. on.channel = 4;
  3902. on.key = 91;
  3903. on.velocity = 1.0f;
  3904. live.push (on);
  3905. std::vector<float> l (24000), r (24000);
  3906. for (int pos = 0; pos < 24000; pos += 480)
  3907. renderer.process (state.get(), false, 0.0, 480, l.data() + pos, r.data() + pos, pos == 0 ? &live : nullptr);
  3908. float peak = 0.0f;
  3909. for (std::size_t i = 12000; i < 24000; ++i)
  3910. peak = std::max (peak, std::abs (l[i]));
  3911. return peak;
  3912. };
  3913. const auto boosted = render (song);
  3914. song.mixer[1].eq.gainDb[1] = 0.0f;
  3915. const auto flat = render (song);
  3916. CHECK (flat > 0.01f);
  3917. CHECK (boosted / flat > 3.0f && boosted / flat < 4.2f);
  3918. }
  3919. TEST (bars_follow_time_signature_markers)
  3920. {
  3921. lynx::Song song;
  3922. song.beatsPerBar = 4;
  3923. const auto bar4 = (std::int64_t) lynx::ppq * 4;
  3924. auto bars = song.barsBetween (0, bar4 * 3);
  3925. CHECK (bars.size() == 3 && bars[2].tick == bar4 * 2 && bars[2].number == 3);
  3926. // 7/4 from bar 3, and 3/4 in the middle of a 7 bar (cuts and restarts)
  3927. song.addMarker ({ bar4 * 2, "Verso", 7, 4 });
  3928. song.addMarker ({ bar4, "Nome só", 0, 4 });
  3929. const auto seven = (std::int64_t) lynx::ppq * 7;
  3930. song.addMarker ({ bar4 * 2 + seven + lynx::ppq * 2, "", 3, 4 });
  3931. bars = song.barsBetween (0, bar4 * 2 + seven * 2);
  3932. CHECK (bars.size() >= 5);
  3933. CHECK (bars[1].length == bar4 && bars[2].tick == bar4 * 2 && bars[2].length == seven);
  3934. CHECK (bars[3].length == lynx::ppq * 2); // cut short by the change
  3935. CHECK (bars[4].length == lynx::ppq * 3 && bars[4].number == 5);
  3936. CHECK (song.markers()[0].name == "Nome só"); // sorted by tick
  3937. // from far away: starts at the right bar without losing the numbering
  3938. const auto far = song.barsBetween (lynx::ppq * 3000, lynx::ppq * 3001);
  3939. CHECK (far.size() == 1 && far[0].length == lynx::ppq * 3);
  3940. CHECK (song.barAt (lynx::ppq * 3000).number == far[0].number);
  3941. CHECK ((lynx::ppq * 3000 - far[0].tick) < far[0].length);
  3942. }
  3943. TEST (split_drum_channel_moves_notes_to_pads)
  3944. {
  3945. lynx::Song song = lynx::Song::makeDefault();
  3946. const auto channels = (int) song.channels.size();
  3947. song.patterns[0].notes.clear();
  3948. song.patterns[0].notes.push_back ({ 0, 240, 1, 36, 0.9f }); // kit kick (channel 1)
  3949. song.patterns[0].notes.push_back ({ 240, 240, 1, 38, 0.8f }); // snare
  3950. song.patterns[0].notes.push_back ({ 480, 240, 1, 50, 0.8f }); // key without a pad: dropped
  3951. song.patterns[0].notes.push_back ({ 480, 240, 3, 60, 0.8f }); // other channel: shifts by 1 position
  3952. lynx::Automation a;
  3953. a.target = { lynx::AutomationTargetKind::channelVolume, 3, 0, 0 };
  3954. song.automations.push_back (a);
  3955. std::vector<lynx::DrumPad> pads (2);
  3956. pads[0].name = "Kick";
  3957. pads[0].key = 36;
  3958. pads[0].layers = { { "kick.wav" } };
  3959. pads[1].name = "Snare";
  3960. pads[1].key = 38;
  3961. pads[1].layers = { { "snare soft.wav", 60, 0, 127, 0, 90 }, { "snare hard.wav", 60, 0, 127, 91, 127 } };
  3962. pads[1].cutGroup = 2;
  3963. pads[1].cutBy = 2;
  3964. lynx::splitDrumChannel (song, 1, pads);
  3965. CHECK ((int) song.channels.size() == channels + 1);
  3966. CHECK (song.channels[1].samplePath == "kick.wav" && song.channels[1].instrument == lynx::InstrumentKind::sampler);
  3967. CHECK (song.channels[2].zones.size() == 2 && song.channels[2].cutGroup == song.channels[2].cutBy && song.channels[2].cutGroup != 0);
  3968. const auto& notes = song.patterns[0].notes;
  3969. CHECK (notes.size() == 3);
  3970. CHECK (notes[0].channel == 1 && notes[0].key == 60);
  3971. CHECK (notes[1].channel == 2 && notes[1].key == 60);
  3972. CHECK (notes[2].channel == 4); // the old channel 3
  3973. CHECK (song.automations[0].target.index == 4);
  3974. }
  3975. TEST (flp_import_brings_playlist)
  3976. {
  3977. flp::Project p;
  3978. p.header.ppq = 96;
  3979. p.channels = { { 0, flp::ChannelType::sampler, "Kick", "", "", 1 } };
  3980. flp::Pattern empty;
  3981. empty.index = 1;
  3982. flp::Pattern full;
  3983. full.index = 2;
  3984. flp::Note n;
  3985. n.channel = 0;
  3986. full.notes.push_back (n);
  3987. p.patterns = { empty, full };
  3988. flp::Arrangement a;
  3989. flp::PlaylistItem item;
  3990. item.kind = flp::PlaylistItem::Kind::pattern;
  3991. item.index = 2;
  3992. item.position = 384;
  3993. item.length = 192;
  3994. item.track = 3;
  3995. item.patternStartTick = 96;
  3996. a.items.push_back (item);
  3997. item.index = 1; // empty pattern: discarded
  3998. a.items.push_back (item);
  3999. item.kind = flp::PlaylistItem::Kind::channelClip; // audio: not supported yet
  4000. a.items.push_back (item);
  4001. p.arrangements.push_back (a);
  4002. const auto s = lynx::importFlp (p);
  4003. CHECK (s.mode == lynx::PlayMode::song);
  4004. CHECK (s.playlist.size() == 1);
  4005. CHECK (s.playlist[0].pattern == 0 && s.playlist[0].track == 3);
  4006. CHECK (s.playlist[0].start == 4 * lynx::ppq && s.playlist[0].length == 2 * lynx::ppq && s.playlist[0].offset == lynx::ppq);
  4007. }
  4008. TEST (rack_releases_notes_on_position_jump)
  4009. {
  4010. lynx::Song s;
  4011. lynx::ChannelDef c;
  4012. c.instrument = lynx::InstrumentKind::synth;
  4013. s.channels.push_back (c);
  4014. auto& p = s.patterns.emplace_back();
  4015. lynx::Note n;
  4016. n.start = 0;
  4017. n.length = 4 * lynx::ppq; // long note
  4018. p.notes.push_back (n);
  4019. s.bpm = 120.0;
  4020. auto state = lynx::RackState::build (s, nullptr, 48000.0);
  4021. lynx::RackRenderer r;
  4022. r.prepare (48000.0);
  4023. std::vector<float> l (512), rr (512);
  4024. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  4025. double tick = 0.0;
  4026. for (int i = 0; i < 20; ++i, tick += 512 * tps)
  4027. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  4028. // jump to tick 2000 (mid-note, no new "on"): the synth must go silent
  4029. for (int i = 0; i < 40; ++i)
  4030. r.process (state.get(), true, 2000.0 + i * 512 * tps, 512, l.data(), rr.data());
  4031. float tail = 0.0f;
  4032. for (auto v : l)
  4033. tail = std::max (tail, std::abs (v));
  4034. CHECK (tail < 0.001f);
  4035. }
  4036. namespace
  4037. {
  4038. // Two samplers with a constant 0.5 signal on different inserts; returns the output peak.
  4039. float juce_db (float gain)
  4040. {
  4041. return 20.0f * std::log10 (gain);
  4042. }
  4043. float renderMixerCase (const std::function<void (lynx::Song&)>& tweak, lynx::MeterBank* meters = nullptr)
  4044. {
  4045. lynx::Song s;
  4046. s.mixer = lynx::Song::defaultMixer (4);
  4047. for (int i = 0; i < 2; ++i)
  4048. {
  4049. lynx::ChannelDef c;
  4050. c.instrument = lynx::InstrumentKind::sampler;
  4051. c.samplePath = "dc";
  4052. c.volume = 1.0f;
  4053. c.mixerTrack = i + 1;
  4054. s.channels.push_back (c);
  4055. }
  4056. s.patterns.emplace_back();
  4057. tweak (s);
  4058. auto provider = [] (const std::string&) {
  4059. auto d = std::make_shared<lynx::SampleData>();
  4060. d->left.assign (48000, 0.5f);
  4061. return std::shared_ptr<const lynx::SampleData> (d);
  4062. };
  4063. auto state = lynx::RackState::build (s, nullptr, 48000.0, provider);
  4064. lynx::RackRenderer r;
  4065. r.prepare (48000.0, 256);
  4066. lynx::LiveEventQueue live;
  4067. for (int ch = 0; ch < 2; ++ch)
  4068. {
  4069. lynx::NoteEvent on;
  4070. on.channel = ch;
  4071. on.velocity = 1.0f;
  4072. live.push (on);
  4073. }
  4074. std::vector<float> l (1000), rr (1000); // larger than the mixer block: tests the chunking
  4075. r.process (state.get(), false, 0.0, 1000, l.data(), rr.data(), &live, meters);
  4076. return std::max (std::abs (l[999]), std::abs (rr[999]));
  4077. }
  4078. } // namespace
  4079. TEST (mixer_routes_mutes_solos_and_meters)
  4080. {
  4081. // two 0.5 channels summed on the Master at 0 dB
  4082. CHECK (std::abs (renderMixerCase ([] (lynx::Song&) {}) - 1.0f) < 1e-4f);
  4083. // insert 1 muted: only channel 2 remains
  4084. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.mixer[1].muted = true; }) - 0.5f) < 1e-4f);
  4085. // solo on insert 2: same
  4086. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.mixer[2].solo = true; }) - 0.5f) < 1e-4f);
  4087. // FL fader curve: 0.8 = 0 dB, max ≈ +5.6 dB, half travel ≈ -12.7 dB
  4088. CHECK (std::abs (lynx::faderGain (0.8f) - 1.0f) < 1e-5f);
  4089. CHECK (std::abs (juce_db (lynx::faderGain (1.0f)) - 5.59f) < 0.05f);
  4090. CHECK (lynx::faderGain (0.0f) == 0.0f);
  4091. // Master fader at max
  4092. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.mixer[0].volume = 1.0f; }) - lynx::faderGain (1.0f)) < 1e-4f);
  4093. // both channels on the same insert, insert fader at 0.4
  4094. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) {
  4095. s.channels[1].mixerTrack = 1;
  4096. s.mixer[1].volume = 0.4f;
  4097. }) - lynx::faderGain (0.4f)) < 1e-4f);
  4098. // a route outside the mixer falls to the last existing insert, without crashing
  4099. CHECK (renderMixerCase ([] (lynx::Song& s) { s.channels[0].mixerTrack = 100; }) > 0.9f);
  4100. // no mixer: everything straight to the output at 0 dB
  4101. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.mixer.clear(); }) - 1.0f) < 1e-4f);
  4102. lynx::MeterBank meters;
  4103. renderMixerCase ([] (lynx::Song&) {}, &meters);
  4104. // (the 24-point sinc's ringing at the sample start: its x87 kernel table rounds differently on ARM64)
  4105. CHECK (std::abs (meters.takePeak (1, 0) - 0.5f) < armTolerance (1e-4f, 2e-4f));
  4106. CHECK (std::abs (meters.takePeak (0, 1) - 1.0f) < armTolerance (1e-4f, 4e-4f));
  4107. CHECK (meters.takePeak (1, 0) == 0.0f); // reading resets
  4108. CHECK (meters.takePeak (3, 0) == 0.0f); // insert with no channel
  4109. }
  4110. TEST (next_free_insert_and_flp_mixer_import)
  4111. {
  4112. auto s = lynx::Song::makeDefault();
  4113. CHECK (s.mixer.size() == 17 && s.mixer[0].name == "Master");
  4114. CHECK (s.nextFreeInsert() == 6); // the 5 default channels take 1..5
  4115. flp::Project p;
  4116. p.channels = { { 0, flp::ChannelType::sampler, "Kick", "", "", 30 } };
  4117. p.mixer = { { 0, "" }, { 30, "Drums" } };
  4118. const auto song = lynx::importFlp (p);
  4119. CHECK (song.channels[0].mixerTrack == 30);
  4120. CHECK (song.mixer.size() == 31);
  4121. CHECK (song.mixer[30].name == "Drums" && song.mixer[0].name == "Master");
  4122. }
  4123. namespace
  4124. {
  4125. float runEffectOnSine (lynx::EffectKind kind, const std::function<void (lynx::EffectSlot&)>& tweak, double hz, float amp,
  4126. int frames = 48000)
  4127. {
  4128. auto fx = lynx::makeEffect (kind);
  4129. fx->prepare (48000.0);
  4130. auto slot = lynx::makeEffectSlot (kind);
  4131. tweak (slot);
  4132. std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
  4133. for (int i = 0; i < frames; ++i)
  4134. l[(std::size_t) i] = r[(std::size_t) i] = amp * (float) std::sin (6.283185307 * hz * i / 48000.0);
  4135. for (int i = 0; i < frames; i += 480)
  4136. fx->process (l.data() + i, r.data() + i, std::min (480, frames - i), slot.params, 120.0);
  4137. float peak = 0.0f;
  4138. for (int i = frames / 2; i < frames; ++i) // second half: already settled
  4139. {
  4140. CHECK (std::isfinite (l[(std::size_t) i]));
  4141. peak = std::max (peak, std::abs (l[(std::size_t) i]));
  4142. }
  4143. return peak;
  4144. }
  4145. } // namespace
  4146. TEST (parametric_eq_bands_and_response)
  4147. {
  4148. // default: transparent
  4149. auto slot = lynx::makeEffectSlot (lynx::EffectKind::parametricEq);
  4150. CHECK (std::abs (lynx::peq::responseDb (slot.params, 48000.0, 1000.0)) < 0.01);
  4151. CHECK (std::abs (runEffectOnSine (lynx::EffectKind::parametricEq, [] (auto&) {}, 1000.0, 0.5f) - 0.5f) < 0.005f);
  4152. // +12 dB peak at 1 kHz on band 4: the response and the signal agree
  4153. auto boost = [] (lynx::EffectSlot& s) {
  4154. s.params[(std::size_t) lynx::peq::param (3, 1)] = lynx::peq::hzValue (1000.0);
  4155. s.params[(std::size_t) lynx::peq::param (3, 2)] = lynx::peq::gainValue (12.0);
  4156. };
  4157. boost (slot);
  4158. CHECK (std::abs (lynx::peq::responseDb (slot.params, 48000.0, 1000.0) - 12.0) < 0.2);
  4159. CHECK (std::abs (lynx::peq::responseDb (slot.params, 48000.0, 100.0)) < 1.0);
  4160. const auto peak = runEffectOnSine (lynx::EffectKind::parametricEq, boost, 1000.0, 0.1f);
  4161. CHECK (std::abs (peak - 0.1f * std::pow (10.0f, 12.0f / 20.0f)) < 0.01f);
  4162. // low cut at 48 dB/oct at 200 Hz: 50 Hz (2 octaves below) drops ~96 dB; 2 kHz passes
  4163. auto cut = lynx::makeEffectSlot (lynx::EffectKind::parametricEq);
  4164. cut.params[(std::size_t) lynx::peq::param (0, 0)] = lynx::peq::typeValue (lynx::peq::BandType::lowCut);
  4165. cut.params[(std::size_t) lynx::peq::param (0, 1)] = lynx::peq::hzValue (200.0);
  4166. cut.params[(std::size_t) lynx::peq::param (0, 3)] = lynx::peq::qValue (0.70710678); // Butterworth
  4167. cut.params[(std::size_t) (lynx::peq::slopeParam + 0)] = 1.0f;
  4168. CHECK (lynx::peq::responseDb (cut.params, 48000.0, 50.0) < -80.0);
  4169. CHECK (std::abs (lynx::peq::responseDb (cut.params, 48000.0, 200.0) + 3.0) < 0.5); // -3 dB at the cutoff (Butterworth)
  4170. CHECK (std::abs (lynx::peq::responseDb (cut.params, 48000.0, 2000.0)) < 0.1);
  4171. // round-trip conversions
  4172. CHECK (std::abs (lynx::peq::hz (lynx::peq::hzValue (440.0)) - 440.0) < 0.5);
  4173. CHECK (std::abs (lynx::peq::q (lynx::peq::qValue (2.5)) - 2.5) < 0.01);
  4174. CHECK (lynx::peq::type (lynx::peq::typeValue (lynx::peq::BandType::notch)) == lynx::peq::BandType::notch);
  4175. }
  4176. TEST (fl_parametric_eq2_state_maps_to_bands)
  4177. {
  4178. // v8 blob: band 1 low cut at 100 Hz "Steep 8", band 4 peak +6 dB at 1 kHz, output −3 dB
  4179. std::vector<std::uint8_t> blob (354, 0);
  4180. auto put = [&] (std::size_t off, std::int32_t v) { std::memcpy (blob.data() + off, &v, 4); };
  4181. put (0, 8);
  4182. auto param = [&] (int i, std::int32_t v) { put (4 + 4 * (std::size_t) i, v); };
  4183. auto hzRaw = [] (double hz) { return (std::int32_t) std::lround (std::log (hz / 20.0) / std::log (1000.0) * 65536.0); };
  4184. for (int b = 0; b < 7; ++b)
  4185. param (21 + b, 0); // all off
  4186. param (21 + 0, 3); // high pass (= low cut)
  4187. param (7 + 0, hzRaw (100.0));
  4188. param (14 + 0, 40124); // default width -> Q ~0.68
  4189. param (28 + 0, -3); // Steep 8 -> 4 biquads
  4190. param (21 + 3, 6); // peaking
  4191. param (7 + 3, hzRaw (1000.0));
  4192. param (3, 600); // +6 dB
  4193. param (14 + 3, 25412);
  4194. param (35, -300); // main level −3 dB
  4195. put (0x13E, 0x7F);
  4196. const auto slot = lynx::effectFromFl ("Fruity Parametric EQ 2", blob);
  4197. CHECK (slot.has_value() && slot->kind == lynx::EffectKind::parametricEq);
  4198. const auto& p = slot->params;
  4199. CHECK (lynx::peq::type (p[(std::size_t) lynx::peq::param (0, 0)]) == lynx::peq::BandType::lowCut);
  4200. CHECK (lynx::peq::flLaw (p) && lynx::peqfl::fromParams (p).order[0] == -3); // FL's own order (REA_rodada50)
  4201. CHECK (lynx::peq::type (p[(std::size_t) lynx::peq::param (3, 0)]) == lynx::peq::BandType::peak);
  4202. CHECK (lynx::peq::type (p[(std::size_t) lynx::peq::param (1, 0)]) == lynx::peq::BandType::off);
  4203. CHECK (std::abs (lynx::peq::responseDb (p, 48000.0, 1000.0) - 3.0) < 0.3); // +6 from the peak −3 from the output
  4204. CHECK (lynx::peq::responseDb (p, 48000.0, 30.0) < -40.0); // 48 dB/oct below 100 Hz
  4205. CHECK (std::abs (lynx::peq::responseDb (p, 48000.0, 8000.0) + 3.0) < 0.3);
  4206. // a band muted by the mask doesn't sound (FL's mask travels with the slot, REA_rodada50)
  4207. put (0x13E, 0x7F & ~(1 << 3));
  4208. const auto muted = lynx::effectFromFl ("Fruity Parametric EQ 2", blob);
  4209. CHECK ((lynx::peqfl::fromParams (muted->params).mask & (1u << 3)) == 0);
  4210. CHECK (std::abs (lynx::peq::responseDb (muted->params, 48000.0, 1000.0) + 3.0) < 0.3);
  4211. }
  4212. TEST (reverb_low_cut_and_predelay)
  4213. {
  4214. // impulse: with 100 ms predelay the wet only starts later; with a 3 kHz low cut a
  4215. // 100 Hz sine barely enters the reverb
  4216. auto run = [] (const std::function<void (lynx::EffectSlot&)>& tweak, double hz, bool impulse) {
  4217. auto fx = lynx::makeEffect (lynx::EffectKind::reverb);
  4218. fx->prepare (48000.0);
  4219. auto slot = lynx::makeEffectSlot (lynx::EffectKind::reverb);
  4220. slot.params[4] = 0.0f; // wet only
  4221. slot.params[3] = 1.0f;
  4222. tweak (slot);
  4223. std::vector<float> l (48000), r (48000);
  4224. for (int i = 0; i < 48000; ++i)
  4225. 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);
  4226. for (int i = 0; i < 48000; i += 480)
  4227. fx->process (l.data() + i, r.data() + i, 480, slot.params, 120.0);
  4228. return l;
  4229. };
  4230. const auto delayed = run ([] (auto& s) { s.params[7] = 0.1f; }, 0.0, true);
  4231. float early = 0.0f, later = 0.0f;
  4232. for (std::size_t i = 0; i < 4800; ++i)
  4233. early = std::max (early, std::abs (delayed[i]));
  4234. for (std::size_t i = 4800; i < 24000; ++i)
  4235. later = std::max (later, std::abs (delayed[i]));
  4236. CHECK (early == 0.0f && later > 0.0f);
  4237. 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; };
  4238. const auto open = energy (run ([] (auto&) {}, 100.0, false));
  4239. const auto cut = energy (run ([] (auto& s) { s.params[5] = 1.0f; }, 100.0, false));
  4240. CHECK (cut < open * 0.01);
  4241. }
  4242. TEST (utility_effect_and_fl_stereo_tools)
  4243. {
  4244. // transparent by default; balance fully right silences the left
  4245. CHECK (std::abs (runEffectOnSine (lynx::EffectKind::utility, [] (auto&) {}, 440.0, 0.5f) - 0.5f) < 0.001f);
  4246. CHECK (runEffectOnSine (lynx::EffectKind::utility, [] (auto& s) { s.params[1] = 1.0f; }, 440.0, 0.5f) < 0.001f);
  4247. // Stereo Shaper at half the knob range (6400): each gain = (3^0.5 − 1)/2 (FL's knob law, REA_rodada54 §2.2)
  4248. std::vector<std::uint8_t> blob (36, 0);
  4249. auto put = [&] (std::size_t off, std::int32_t v) { std::memcpy (blob.data() + off, &v, 4); };
  4250. put (4, 6400); put (8, 6400); put (12, 6400); put (16, 6400);
  4251. const auto slot = lynx::effectFromFl ("Fruity Stereo Shaper", blob);
  4252. CHECK (slot && slot->kind == lynx::EffectKind::stereoShaper);
  4253. auto shaperFx = lynx::makeEffect (*slot);
  4254. shaperFx->prepare (44100.0);
  4255. float zl = 0.0f, zr = 0.0f;
  4256. shaperFx->process (&zl, &zr, 0, slot->params, 120.0); // takes the controls
  4257. shaperFx->reset(); // FL's Flush: the gains jump to their targets
  4258. float l = 1.0f, r = 0.0f;
  4259. shaperFx->process (&l, &r, 1, slot->params, 120.0);
  4260. const auto half = (float) ((std::sqrt (3.0) - 1.0) / 2.0);
  4261. CHECK (std::abs (l - half) < 1e-4f && std::abs (r - half) < 1e-4f);
  4262. auto fx = lynx::makeEffect (lynx::EffectKind::utility);
  4263. // Balance: volume 256 = 0 dB, pan −128 = fully left
  4264. std::vector<std::uint8_t> bal (8, 0);
  4265. std::int32_t pan = -128, vol = 256;
  4266. std::memcpy (bal.data(), &pan, 4);
  4267. std::memcpy (bal.data() + 4, &vol, 4);
  4268. const auto b = lynx::effectFromFl ("Fruity Balance", bal);
  4269. CHECK (b && b->kind == lynx::EffectKind::fruityBalance); // FL's exact law (REA_rodada58 §2): fully left sums R into L
  4270. if (b)
  4271. {
  4272. auto balance = lynx::makeEffect (*b);
  4273. balance->prepare (44100.0);
  4274. l = 0.5f; r = 0.5f;
  4275. balance->process (&l, &r, 1, b->params, 120.0);
  4276. CHECK (std::abs (l - 0.70710678f) < 1e-6f && r == 0.0f);
  4277. }
  4278. }
  4279. TEST (effects_eq_compressor_behave)
  4280. {
  4281. // EQ at default is transparent
  4282. CHECK (std::abs (runEffectOnSine (lynx::EffectKind::eq, [] (auto&) {}, 1000.0, 0.5f) - 0.5f) < 0.01f);
  4283. // +18 dB on the lows: 60 Hz rises a lot, 5 kHz barely
  4284. CHECK (runEffectOnSine (lynx::EffectKind::eq, [] (auto& s) { s.params[0] = 1.0f; }, 60.0, 0.05f) > 0.3f);
  4285. CHECK (runEffectOnSine (lynx::EffectKind::eq, [] (auto& s) { s.params[0] = 1.0f; }, 5000.0, 0.05f) < 0.06f);
  4286. // compressor: threshold -20 dB, ratio 20:1 flattens a 0 dBFS signal to near the threshold
  4287. const auto squashed = runEffectOnSine (lynx::EffectKind::compressor, [] (auto& s) {
  4288. s.params[0] = 40.0f / 60.0f;
  4289. s.params[1] = 1.0f;
  4290. }, 200.0, 1.0f);
  4291. CHECK (squashed < 0.2f && squashed > 0.05f);
  4292. // a signal below the threshold passes intact
  4293. CHECK (std::abs (runEffectOnSine (lynx::EffectKind::compressor, [] (auto& s) { s.params[0] = 1.0f; }, 200.0, 0.3f) - 0.3f) < 0.01f);
  4294. }
  4295. TEST (effects_delay_echoes_on_tempo_and_reverb_has_tail)
  4296. {
  4297. auto delay = lynx::makeEffect (lynx::EffectKind::delay);
  4298. delay->prepare (48000.0);
  4299. auto slot = lynx::makeEffectSlot (lynx::EffectKind::delay);
  4300. slot.params[0] = 3.5f / 8.0f; // index 3 = 1/4 = 0.5 s at 120 BPM
  4301. slot.params[1] = 0.0f; // no feedback: a single echo
  4302. slot.params[2] = 1.0f;
  4303. slot.params[4] = 0.0f; // no damping
  4304. std::vector<float> l (48000, 0.0f), r (48000, 0.0f);
  4305. l[0] = r[0] = 1.0f;
  4306. delay->process (l.data(), r.data(), 48000, slot.params, 120.0);
  4307. CHECK (std::abs (l[24000] - 1.0f) < 1e-4f);
  4308. CHECK (std::abs (l[12000]) < 1e-6f);
  4309. auto reverb = lynx::makeEffect (lynx::EffectKind::reverb);
  4310. reverb->prepare (48000.0);
  4311. auto rs = lynx::makeEffectSlot (lynx::EffectKind::reverb);
  4312. rs.params[3] = 1.0f;
  4313. std::fill (l.begin(), l.end(), 0.0f);
  4314. std::fill (r.begin(), r.end(), 0.0f);
  4315. l[0] = r[0] = 1.0f;
  4316. reverb->process (l.data(), r.data(), 48000, rs.params, 120.0);
  4317. float tail = 0.0f;
  4318. for (int i = 4800; i < 9600; ++i) // between 100 and 200 ms after the impulse
  4319. tail = std::max (tail, std::abs (l[(std::size_t) i]));
  4320. CHECK (tail > 1e-4f && tail < 1.0f);
  4321. CHECK (lynx::formatEffectParam (lynx::EffectKind::delay, 0, slot.params[0], 120.0) == "1/4 (500 ms)");
  4322. }
  4323. TEST (rack_runs_insert_effects_and_reuses_them)
  4324. {
  4325. // an insert with EQ at -inf doesn't exist; we use the EQ output at -18 dB to see the effect act
  4326. const auto quiet = renderMixerCase ([] (lynx::Song& s) {
  4327. auto eq = lynx::makeEffectSlot (lynx::EffectKind::eq);
  4328. eq.params[4] = 0.0f; // output -18 dB
  4329. s.mixer[1].effects.push_back (eq);
  4330. });
  4331. CHECK (quiet > 0.55f && quiet < 0.6f); // 0.5 from channel 2 + 0.5 * 0.126 from channel 1
  4332. const auto bypassed = renderMixerCase ([] (lynx::Song& s) {
  4333. auto eq = lynx::makeEffectSlot (lynx::EffectKind::eq);
  4334. eq.params[4] = 0.0f;
  4335. eq.enabled = false;
  4336. s.mixer[1].effects.push_back (eq);
  4337. });
  4338. CHECK (std::abs (bypassed - 1.0f) < 1e-4f);
  4339. auto s = lynx::Song::makeDefault();
  4340. s.mixer[2].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::reverb));
  4341. auto a = lynx::RackState::build (s, nullptr, 48000.0);
  4342. s.mixer[2].effects[0].params[0] = 0.9f; // turning a knob doesn't recreate the effect
  4343. auto b = lynx::RackState::build (s, a.get(), 48000.0);
  4344. CHECK (b->effects[2][0] == a->effects[2][0]);
  4345. s.mixer[2].effects[0] = lynx::makeEffectSlot (lynx::EffectKind::delay);
  4346. auto c = lynx::RackState::build (s, b.get(), 48000.0);
  4347. CHECK (c->effects[2][0] != b->effects[2][0]);
  4348. }
  4349. TEST (audio_clips_trigger_cut_and_resume)
  4350. {
  4351. lynx::Song s;
  4352. s.bpm = 120.0; // 1 beat = 0.5 s = 24000 samples at 48 kHz
  4353. lynx::ChannelDef c;
  4354. c.instrument = lynx::InstrumentKind::audioClip;
  4355. c.samplePath = "ramp";
  4356. c.volume = 1.0f;
  4357. s.channels.push_back (c);
  4358. s.patterns.emplace_back();
  4359. lynx::Clip clip;
  4360. clip.kind = lynx::ClipKind::audio;
  4361. clip.channel = 0;
  4362. clip.start = lynx::ppq; // starts at beat 2
  4363. clip.length = 2 * lynx::ppq; // lasts 2 beats
  4364. clip.offset = lynx::ppq; // skips the first beat of the audio
  4365. s.playlist.push_back (clip);
  4366. // an empty pattern clip over the whole bar: the song ends at beat 4 (FL loops at the last clip's exact end)
  4367. s.playlist.push_back (lynx::Clip { 0, 1, 0, 4 * lynx::ppq, 0 });
  4368. s.mode = lynx::PlayMode::song;
  4369. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  4370. lynx::EventBuffer ev;
  4371. lynx::scheduleSong (s, 0.0, 4 * 24000, tps, ev);
  4372. CHECK (ev.size() == 2);
  4373. CHECK (ev[0].type == lynx::NoteEvent::Type::on && ev[0].sampleOffset == 24000 && std::abs (ev[0].startSeconds - 0.5) < 1e-9);
  4374. CHECK (ev[1].type == lynx::NoteEvent::Type::off && ev[1].sampleOffset == 3 * 24000);
  4375. // seek to the middle of the clip (beat 2.5): with resume, starts at the block's first sample
  4376. ev.clear();
  4377. lynx::scheduleSong (s, 1.5 * lynx::ppq, 256, tps, ev, true);
  4378. CHECK (ev.size() == 1 && ev[0].sampleOffset == 0 && std::abs (ev[0].startSeconds - 0.75) < 1e-9);
  4379. ev.clear();
  4380. lynx::scheduleSong (s, 1.5 * lynx::ppq, 256, tps, ev, false);
  4381. CHECK (ev.size() == 0);
  4382. // sample whose value = position in seconds: lets us check where playback started
  4383. auto provider = [] (const std::string&) {
  4384. auto d = std::make_shared<lynx::SampleData>();
  4385. d->sampleRate = 48000.0;
  4386. for (int i = 0; i < 48000 * 3; ++i)
  4387. d->left.push_back ((float) i / 48000.0f);
  4388. return std::shared_ptr<const lynx::SampleData> (d);
  4389. };
  4390. auto state = lynx::RackState::build (s, nullptr, 48000.0, provider);
  4391. lynx::RackRenderer r;
  4392. r.prepare (48000.0, 512);
  4393. std::vector<float> l (256), rr (256);
  4394. // first block already playing mid-clip
  4395. r.process (state.get(), true, 1.5 * lynx::ppq, 256, l.data(), rr.data());
  4396. const auto unity = 1.0f / lynx::channelGain (10000.0f / 12800.0f); // clip level at 100% channel volume (REA_rodada89 §4.2)
  4397. CHECK (std::abs (l[0] - 0.75f * unity) < 0.002f);
  4398. // the clip ends at beat 3: after the short fade, silence (the sample would still have audio)
  4399. double tick = 3.0 * lynx::ppq - 10 * tps;
  4400. r.process (state.get(), false, tick, 256, l.data(), rr.data()); // stops (fade)
  4401. for (int i = 0; i < 4; ++i)
  4402. r.process (state.get(), false, tick, 256, l.data(), rr.data());
  4403. CHECK (std::abs (l[255]) < 1e-6f);
  4404. }
  4405. TEST (remove_channel_fixes_notes_and_audio_clips)
  4406. {
  4407. lynx::Song s;
  4408. s.channels.resize (3);
  4409. s.patterns.resize (1);
  4410. s.patterns[0].notes = { { 0, 240, 0, 60, 1.0f }, { 0, 240, 1, 60, 1.0f }, { 0, 240, 2, 60, 1.0f } };
  4411. lynx::Clip a;
  4412. a.kind = lynx::ClipKind::audio;
  4413. a.channel = 1;
  4414. a.length = 100;
  4415. lynx::Clip b = a;
  4416. b.channel = 2;
  4417. s.playlist = { a, b };
  4418. s.removeChannel (1);
  4419. CHECK (s.channels.size() == 2);
  4420. CHECK (s.patterns[0].notes.size() == 2 && s.patterns[0].notes[1].channel == 1);
  4421. CHECK (s.playlist.size() == 1 && s.playlist[0].channel == 1);
  4422. }
  4423. TEST (flp_import_audio_clips_with_offsets)
  4424. {
  4425. flp::Project p;
  4426. p.header.ppq = 96;
  4427. p.tempo = 120.0;
  4428. p.channels = { { 0, flp::ChannelType::audioClip, "Vocal", "", "%FLStudioData%\\v.wav", 2 },
  4429. { 1, flp::ChannelType::audioClip, "Loop", "", "%FLStudioData%\\l.wav", 3 } };
  4430. flp::Arrangement arr;
  4431. flp::PlaylistItem beats; // offset in beats: end - start = duration in beats
  4432. beats.kind = flp::PlaylistItem::Kind::channelClip;
  4433. beats.index = 0;
  4434. beats.position = 192;
  4435. beats.length = 384; // 4 beats
  4436. beats.startOffset = 2.0f;
  4437. beats.endOffset = 6.0f;
  4438. flp::PlaylistItem ms = beats; // offset in ms: 4 beats at 120 BPM = 2000 ms
  4439. ms.index = 1;
  4440. ms.startOffset = 500.0f;
  4441. ms.endOffset = 2500.0f;
  4442. arr.items = { beats, ms };
  4443. p.arrangements.push_back (arr);
  4444. lynx::FlpImportOptions options;
  4445. options.roots = { "C:\\FL", "C:\\FL\\Data", "" };
  4446. options.fileExists = [] (const std::string&) { return true; };
  4447. const auto s = lynx::importFlp (p, options);
  4448. CHECK (s.channels.size() == 2 && s.channels[0].instrument == lynx::InstrumentKind::audioClip);
  4449. CHECK (s.channels[0].samplePath == "C:\\FL\\Data\\v.wav");
  4450. CHECK (s.playlist.size() == 2);
  4451. CHECK (s.playlist[0].kind == lynx::ClipKind::audio && s.playlist[0].start == 2 * lynx::ppq);
  4452. CHECK (s.playlist[0].offset == 2 * lynx::ppq);
  4453. CHECK (s.playlist[1].offset == lynx::ppq); // 500 ms at 120 BPM = 1 beat
  4454. CHECK (s.mode == lynx::PlayMode::song);
  4455. }
  4456. TEST (plugin_factories_create_reuse_and_fallback)
  4457. {
  4458. lynx::Song s;
  4459. s.mixer = lynx::Song::defaultMixer (2);
  4460. lynx::ChannelDef c;
  4461. c.instrument = lynx::InstrumentKind::plugin;
  4462. c.plugin.id = "VST3-Synth-123";
  4463. c.mixerTrack = 1;
  4464. s.channels.push_back (c);
  4465. lynx::ChannelDef missing = c;
  4466. missing.plugin.id = "VST3-Ausente-0";
  4467. s.channels.push_back (missing);
  4468. lynx::EffectSlot fx;
  4469. fx.kind = lynx::EffectKind::plugin;
  4470. fx.plugin.id = "VST3-Reverb-9";
  4471. s.mixer[1].effects.push_back (fx);
  4472. s.patterns.emplace_back();
  4473. int instrumentsMade = 0, effectsMade = 0;
  4474. lynx::RackState::Factories f;
  4475. f.pluginInstruments = [&] (const lynx::PluginRef& ref, double) -> std::shared_ptr<lynx::Instrument> {
  4476. if (ref.id != "VST3-Synth-123")
  4477. return nullptr;
  4478. ++instrumentsMade;
  4479. auto synth = lynx::makeInstrument (lynx::InstrumentKind::synth);
  4480. synth->prepare (48000.0);
  4481. return synth;
  4482. };
  4483. f.pluginEffects = [&] (const lynx::PluginRef&, double) -> std::shared_ptr<lynx::Effect> {
  4484. ++effectsMade;
  4485. auto eq = lynx::makeEffect (lynx::EffectKind::eq);
  4486. eq->prepare (48000.0);
  4487. return eq;
  4488. };
  4489. auto a = lynx::RackState::build (s, nullptr, 48000.0, f);
  4490. CHECK (instrumentsMade == 1 && effectsMade == 1);
  4491. CHECK (a->instruments.size() == 2 && a->instruments[1] != nullptr); // missing: muted, but it exists
  4492. // changing the saved state doesn't recreate; swapping the plugin does
  4493. s.channels[0].plugin.state = "abc";
  4494. auto b = lynx::RackState::build (s, a.get(), 48000.0, f);
  4495. CHECK (instrumentsMade == 1 && b->instruments[0] == a->instruments[0] && b->effects[1][0] == a->effects[1][0]);
  4496. s.mixer[1].effects[0].plugin.id = "VST3-Outro-1";
  4497. auto c2 = lynx::RackState::build (s, b.get(), 48000.0, f);
  4498. CHECK (effectsMade == 2);
  4499. // rendering with a missing plugin and a plugin slot breaks nothing
  4500. lynx::RackRenderer r;
  4501. r.prepare (48000.0, 256);
  4502. lynx::LiveEventQueue live;
  4503. lynx::NoteEvent on;
  4504. on.channel = 0;
  4505. on.velocity = 1.0f;
  4506. live.push (on);
  4507. on.channel = 1;
  4508. live.push (on);
  4509. std::vector<float> l (256), rr (256);
  4510. r.process (c2.get(), false, 0.0, 256, l.data(), rr.data(), &live);
  4511. float peak = 0.0f;
  4512. for (auto v : l)
  4513. {
  4514. CHECK (std::isfinite (v));
  4515. peak = std::max (peak, std::abs (v));
  4516. }
  4517. CHECK (peak > 0.0f);
  4518. }
  4519. TEST (export_song_adds_silent_tail_and_never_restarts)
  4520. {
  4521. auto s = lynx::Song::makeDefault();
  4522. s.playlist.clear();
  4523. std::int64_t length = 0;
  4524. auto pat = lynx::makeExportSong (s, true, lynx::ppq * 8, length); // empty Playlist: uses the pattern
  4525. CHECK (length == lynx::ppq * 4);
  4526. CHECK (pat.mode == lynx::PlayMode::song && pat.playlist.size() == 2);
  4527. CHECK (pat.songLength() >= length + lynx::ppq * 8);
  4528. // no new notes after the song ends, only silence until the end of the tail
  4529. lynx::EventBuffer events;
  4530. const auto tps = lynx::ticksPerSample (pat.bpm, 48000.0);
  4531. int onsAfterEnd = 0, onsBefore = 0;
  4532. for (double tick = 0.0; tick < (double) (length + lynx::ppq * 8); tick += 512 * tps)
  4533. {
  4534. events.clear();
  4535. lynx::scheduleSong (pat, tick, 512, tps, events);
  4536. for (const auto& e : events)
  4537. if (e.type == lynx::NoteEvent::Type::on)
  4538. (tick + e.sampleOffset * tps >= (double) length ? onsAfterEnd : onsBefore)++;
  4539. }
  4540. CHECK (onsBefore == (int) s.patterns[0].notes.size());
  4541. CHECK (onsAfterEnd == 0);
  4542. // with Playlist: the end is the last clip's, without rounding
  4543. s.playlist.push_back ({ 0, 0, lynx::ppq * 2, lynx::ppq * 3, 0 });
  4544. auto song = lynx::makeExportSong (s, true, 0, length);
  4545. CHECK (length == lynx::ppq * 5 && song.playlist.size() == 1);
  4546. song = lynx::makeExportSong (s, false, 0, length);
  4547. CHECK (length == lynx::ppq * 4 && song.playlist[0].start == 0);
  4548. }
  4549. TEST (note_edit_selection_operations)
  4550. {
  4551. namespace ne = lynx::noteedit;
  4552. lynx::Pattern p;
  4553. p.notes = { { 0, 240, 0, 60 }, { 480, 240, 0, 64 }, { 960, 480, 1, 67 } };
  4554. // sorting keeps the indices in step
  4555. p.notes[0].start = 1200;
  4556. std::vector<int> tracked { 0, 2 };
  4557. ne::sortTracking (p, tracked);
  4558. CHECK (p.notes[2].start == 1200 && p.notes[(std::size_t) tracked[0]].start == 1200);
  4559. CHECK (p.notes[(std::size_t) tracked[1]].key == 67);
  4560. // group move respects the limits
  4561. std::int64_t dt = -5000;
  4562. int dk = 100;
  4563. ne::move (p, { 0, 1 }, dt, dk);
  4564. CHECK (dt == -480 && dk == 127 - 67 && p.notes[0].start == 0 && p.notes[1].key == 127);
  4565. // duplicate: right after the selection, rounded to the grid
  4566. lynx::Pattern q;
  4567. q.notes = { { 0, 240, 0, 60 }, { 240, 300, 0, 62 } }; // 540-tick selection
  4568. const auto added = ne::duplicate (q, { 0, 1 }, 960);
  4569. CHECK (added.size() == 2 && q.notes[(std::size_t) added[0]].start == 960 && q.notes[(std::size_t) added[1]].start == 1200);
  4570. // paste onto another channel with an offset
  4571. const auto pasted = ne::paste (q, ne::copy (q, { 1 }), 100, 3);
  4572. CHECK (pasted.size() == 1 && q.notes.back().channel == 3 && q.notes.back().start == 340);
  4573. // quantize rounds to the nearest
  4574. q.notes[0].start = 130;
  4575. q.notes[1].start = 100;
  4576. ne::quantize (q, { 0, 1 }, 240);
  4577. CHECK (q.notes[0].start == 240 && q.notes[1].start == 0);
  4578. // remove several (order and duplicates don't matter)
  4579. const auto before = q.notes.size();
  4580. ne::remove (q, { 1, 0, 1 });
  4581. CHECK (q.notes.size() == before - 2);
  4582. }
  4583. TEST (note_edit_quick_operations)
  4584. {
  4585. namespace ne = lynx::noteedit;
  4586. const std::int64_t step = lynx::ticksPerStep, bar = lynx::ppq * 4;
  4587. // split: only strictly inside; the tail is appended and does not slide again
  4588. lynx::Pattern p;
  4589. p.notes = { { 0, 4 * step, 0, 60 } };
  4590. p.notes[0].slide = true;
  4591. CHECK (ne::split (p, 0, 0) == -1 && ne::split (p, 0, 4 * step) == -1);
  4592. CHECK (ne::split (p, 0, step) == 1 && p.notes[0].length == step && p.notes[1].start == step && p.notes[1].length == 3 * step
  4593. && ! p.notes[1].slide);
  4594. // chop to grid: a 2.5-step note from half a step becomes 3 pieces cut on the step lines
  4595. lynx::Pattern c;
  4596. c.notes = { { step / 2, step * 5 / 2, 0, 60 }, { 0, step, 1, 50 } };
  4597. const auto pieces = ne::chop (c, { 0, 1 }, step);
  4598. CHECK (pieces.size() == 4 && c.notes.size() == 4);
  4599. CHECK (c.notes[0].length == step / 2 && c.notes[2].start == step && c.notes[2].length == step && c.notes[3].start == 2 * step
  4600. && c.notes[3].length == step);
  4601. // join: touching and overlapping notes of the same key and channel merge; others stay
  4602. lynx::Pattern j;
  4603. j.notes = { { 0, step, 0, 60 }, { step, step, 0, 60 }, { step * 3 / 2, step * 2, 0, 60 }, { 5 * step, step, 0, 60 },
  4604. { step, step, 0, 62 }, { step, step, 1, 60 } };
  4605. CHECK (ne::join (j, { 0, 1, 2, 3, 4, 5 }) == 2 && j.notes.size() == 4);
  4606. CHECK (j.notes[0].start == 0 && j.notes[0].length == step * 7 / 2);
  4607. // legato: each note reaches the next start (a chord ends together); the last keeps its length
  4608. lynx::Pattern l;
  4609. l.notes = { { 0, 10, 0, 60 }, { 0, 10, 0, 64 }, { step, 10, 0, 62 }, { 3 * step, 10, 0, 65 }, { step, 10, 1, 40 } };
  4610. ne::legato (l, { 0, 1, 2, 3, 4 });
  4611. CHECK (l.notes[0].length == step && l.notes[1].length == step && l.notes[2].length == 2 * step && l.notes[3].length == 10
  4612. && l.notes[4].length == 10);
  4613. // shrink to grid and full quantise (start and end)
  4614. ne::shrinkToGrid (l, { 3 }, step / 2);
  4615. CHECK (l.notes[3].length == step / 2);
  4616. lynx::Pattern q;
  4617. q.notes = { { 10, step - 30, 0, 60 }, { step + 100, 20, 0, 60 } };
  4618. ne::quantizeFull (q, { 0, 1 }, step);
  4619. CHECK (q.notes[0].start == 0 && q.notes[0].length == step && q.notes[1].start == step && q.notes[1].length == step);
  4620. // rotate inside the bar, wrapping
  4621. lynx::Pattern r;
  4622. r.notes = { { 0, step, 0, 60 }, { bar - step, step, 0, 62 } };
  4623. ne::rotate (r, { 0, 1 }, step, bar, 1);
  4624. CHECK (r.notes[0].start == step && r.notes[1].start == 0);
  4625. ne::rotate (r, { 0, 1 }, step, bar, -2);
  4626. CHECK (r.notes[0].start == bar - step && r.notes[1].start == bar - 2 * step);
  4627. r.notes.push_back ({ bar + step, step, 0, 64 }); // alone in the second bar: wraps inside that bar
  4628. ne::rotate (r, { 2 }, step, bar, -2);
  4629. CHECK (r.notes[2].start == 2 * bar - step);
  4630. }
  4631. TEST (note_edit_grid_and_time_text)
  4632. {
  4633. namespace ne = lynx::noteedit;
  4634. const std::int64_t step = lynx::ticksPerStep, bar = lynx::ppq * 4;
  4635. CHECK (ne::snapDown (250.0, step) == step && ne::snapDown (-1.0, step) == -step && ne::snapDown (17.9, 1) == 17);
  4636. CHECK (ne::snapNearest (1000.0, { 400, 1100, 980 }, 50.0, step) == 980);
  4637. CHECK (ne::snapNearest (1000.0, { 400 }, 50.0, step) == 960); // too far: falls back to the grid
  4638. // screen grid: 24 px per step shows steps; zoomed out it climbs to beats, bars and bar multiples
  4639. CHECK (ne::screenStep (24.0 / step, bar) == step);
  4640. CHECK (ne::screenStep (96.0 / bar, bar) == lynx::ppq);
  4641. CHECK (ne::screenStep (8.0 / bar, bar) == 2 * bar);
  4642. CHECK (ne::screenStep (200.0 / step, bar) == step / 4);
  4643. CHECK (ne::formatTime (0, 4) == "1:1:0" && ne::formatTime (bar + 5 * step + 7, 4) == "2:6:7");
  4644. CHECK (ne::formatTime (step * 2, 4, true) == "0:2:0");
  4645. std::int64_t t = 0;
  4646. CHECK (ne::parseTime ("2:6:7", 4, t) && t == bar + 5 * step + 7);
  4647. CHECK (ne::parseTime ("3", 4, t) && t == 2 * bar);
  4648. CHECK (ne::parseTime ("0:2:0", 4, t, true) && t == 2 * step);
  4649. CHECK (! ne::parseTime ("0:1:0", 4, t) && ! ne::parseTime ("1:17:0", 4, t) && ! ne::parseTime ("1:1:240", 4, t)
  4650. && ! ne::parseTime ("a", 4, t) && ! ne::parseTime ("", 4, t));
  4651. CHECK (ne::parseTime ("1:12:0", 3, t) && ! ne::parseTime ("1:13:0", 3, t)); // 3/4: 12 steps per bar
  4652. }
  4653. TEST (playlist_clip_edges_and_time_selection)
  4654. {
  4655. namespace ne = lynx::noteedit;
  4656. const std::int64_t beat = lynx::ppq;
  4657. // left-edge trim: content stays in place, cannot reveal what is before the content's start
  4658. lynx::Clip c { 0, 0, 4 * beat, 4 * beat, beat };
  4659. ne::trimStart (c, 5 * beat, beat);
  4660. CHECK (c.start == 5 * beat && c.offset == 2 * beat && c.length == 3 * beat);
  4661. ne::trimStart (c, 0, beat);
  4662. CHECK (c.start == 3 * beat && c.offset == 0 && c.length == 5 * beat);
  4663. ne::trimStart (c, 100 * beat, beat);
  4664. CHECK (c.length == beat && c.end() == 8 * beat);
  4665. // slip moves the content, not the clip
  4666. lynx::Clip s { 0, 0, 0, 2 * beat, beat };
  4667. ne::slip (s, beat / 2);
  4668. CHECK (s.offset == beat / 2 && s.start == 0 && s.length == 2 * beat);
  4669. ne::slip (s, beat);
  4670. CHECK (s.offset == 0);
  4671. ne::slip (s, -3 * beat);
  4672. CHECK (s.offset == 3 * beat);
  4673. // insert / delete time across all tracks, markers included
  4674. std::vector<lynx::Clip> clips { { 0, 0, 0, 2 * beat, 0 }, { 0, 1, 4 * beat, 2 * beat, 0 }, { 0, 2, beat, 6 * beat, 0 } };
  4675. std::vector<lynx::Marker> markers { { 5 * beat, "A" }, { beat / 2, "B" } };
  4676. ne::insertTime (clips, markers, 2 * beat, 4 * beat);
  4677. CHECK (clips[0].start == 0 && clips[1].start == 8 * beat && clips[2].start == beat && markers[0].tick == 9 * beat
  4678. && markers[1].tick == beat / 2);
  4679. std::vector<lynx::Clip> d { { 0, 0, 0, 2 * beat, 0 }, // before: kept
  4680. { 0, 1, 3 * beat, beat, 0 }, // inside: deleted
  4681. { 0, 2, beat, 6 * beat, 0 }, // spans the cut: head + tail
  4682. { 0, 3, 3 * beat, 4 * beat, 0 }, // tail outside: trimmed and moved
  4683. { 0, 4, 8 * beat, beat, 0 } }; // after: moved left
  4684. std::vector<lynx::Marker> dm { { 3 * beat, "in" }, { 9 * beat, "after" } };
  4685. ne::deleteTime (d, dm, 2 * beat, 5 * beat);
  4686. CHECK (d.size() == 5);
  4687. CHECK (d[1].track == 2 && d[1].start == beat && d[1].length == beat);
  4688. CHECK (d[2].track == 2 && d[2].start == 2 * beat && d[2].offset == 4 * beat && d[2].length == 2 * beat);
  4689. CHECK (d[3].track == 3 && d[3].start == 2 * beat && d[3].offset == 2 * beat && d[3].length == 2 * beat);
  4690. CHECK (d[4].start == 5 * beat && dm.size() == 1 && dm[0].tick == 6 * beat);
  4691. // time selection: overlapping clips; Ctrl+B repeats what starts inside it after itself
  4692. std::vector<lynx::Clip> r { { 0, 0, 0, beat, 0 }, { 0, 1, beat, 2 * beat, 0 }, { 0, 0, 4 * beat, beat, 0 } };
  4693. CHECK ((ne::clipsInTime (r, beat / 2, 2 * beat) == std::vector<int> { 0, 1 }));
  4694. const auto added = ne::repeatTime (r, 0, 4 * beat, {});
  4695. CHECK (added.size() == 2 && r[3].start == 4 * beat && r[4].start == 5 * beat && r[4].track == 1);
  4696. CHECK (ne::repeatTime (r, 0, 2 * beat, { 2 }).size() == 1 && r.back().start == 6 * beat);
  4697. }
  4698. TEST (recorder_builds_notes_in_pattern_and_song_mode)
  4699. {
  4700. auto song = lynx::Song::makeDefault();
  4701. const auto loop = song.patterns[0].effectiveLength (4); // 1 bar = 3840
  4702. lynx::NoteRecorder r;
  4703. r.add (song, { 100.0, 4, 60, 0.5f, true }); // no take: ignored
  4704. CHECK (r.takeFinished().empty());
  4705. r.begin (loop);
  4706. r.add (song, { 4000.0, 4, 60, 0.5f, true }); // second loop pass
  4707. r.add (song, { 4480.0, 4, 60, 0.0f, false });
  4708. r.add (song, { 5000.0, 4, 64, 0.9f, true }); // held until stop
  4709. r.end (song, 5240.0);
  4710. const auto notes = r.takeFinished();
  4711. CHECK (notes.size() == 2);
  4712. CHECK (notes[0].start == 4000 - 3840 && notes[0].length == 480 && notes[0].velocity == 0.5f && notes[0].channel == 4);
  4713. CHECK (notes[1].key == 64 && notes[1].start == 5000 - 3840 && notes[1].length == 240);
  4714. // song mode: position inside the current pattern's clip (with offset)
  4715. song.mode = lynx::PlayMode::song;
  4716. song.playlist = { { 0, 0, 7680, 3840, 960 } };
  4717. CHECK (lynx::patternPositionForTick (song, 7680.0 + 100.0, loop) == 1060);
  4718. CHECK (lynx::patternPositionForTick (song, 100.0, loop) == 100); // outside the clip: like loop
  4719. }
  4720. TEST (automation_curve_and_playback)
  4721. {
  4722. lynx::Automation a;
  4723. a.points = { { 0, 0.0f }, { 960, 1.0f }, { 1920, 0.5f, 0.0f } };
  4724. CHECK (a.valueAt (-10.0) == 0.0f && a.valueAt (480.0) == 0.5f && a.valueAt (5000.0) == 0.5f);
  4725. CHECK (a.length() == 1920);
  4726. a.points[0].tension = 1.0f; // curve: starts slow
  4727. CHECK (a.valueAt (480.0) < 0.2f && a.valueAt (480.0) > 0.0f);
  4728. // automated Master: silence in the first bar, sound in the second, and the last value
  4729. // keeps applying after the automation clip ends
  4730. auto song = lynx::Song::makeDefault();
  4731. song.bpm = 120.0;
  4732. song.mode = lynx::PlayMode::song;
  4733. const auto bar = (std::int64_t) lynx::ppq * 4;
  4734. for (int b = 0; b < 3; ++b)
  4735. song.playlist.push_back ({ 0, 0, bar * b, bar, 0 });
  4736. lynx::Automation master;
  4737. master.target = { lynx::AutomationTargetKind::mixerVolume, 0 };
  4738. master.points = { { 0, 0.0f }, { bar - 1, 0.0f }, { bar, 0.8f } };
  4739. song.automations.push_back (master);
  4740. lynx::Clip ac;
  4741. ac.kind = lynx::ClipKind::automation;
  4742. ac.automation = 0;
  4743. ac.track = 1;
  4744. ac.length = bar + bar / 2;
  4745. song.playlist.push_back (ac);
  4746. const double sr = 48000.0;
  4747. auto state = lynx::RackState::build (song, nullptr, sr);
  4748. CHECK (state->hasAutomation && state->mixerVolumeAuto[0] == 0 && state->automationClips[0].size() == 1);
  4749. lynx::RackRenderer r;
  4750. r.prepare (sr, 512);
  4751. std::vector<float> l (512), rr (512);
  4752. const auto tps = lynx::ticksPerSample (song.bpm, sr);
  4753. float peakBar1 = 0.0f, peakBar2 = 0.0f, peakBar3 = 0.0f;
  4754. for (double tick = 0.0; tick < (double) (bar * 3); tick += 512 * tps)
  4755. {
  4756. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  4757. auto& peak = tick < (double) bar - 512 * tps ? peakBar1 : tick < (double) bar * 2 ? peakBar2 : peakBar3;
  4758. for (auto v : l)
  4759. peak = std::max (peak, std::abs (v));
  4760. }
  4761. CHECK (peakBar1 < 1.0e-6f);
  4762. CHECK (peakBar2 > 0.05f);
  4763. CHECK (peakBar3 > 0.05f); // after the clip: keeps the last value
  4764. // target removed: automation disappears from the tables
  4765. song.removeChannel (0);
  4766. song.automations[0].target = { lynx::AutomationTargetKind::channelVolume, 0 };
  4767. song.removeChannel (0);
  4768. CHECK (song.automations[0].target.kind == lynx::AutomationTargetKind::none);
  4769. song.removeAutomation (0);
  4770. CHECK (song.automations.empty() && song.playlist.size() == 3);
  4771. }
  4772. TEST (mixer_sends_between_inserts)
  4773. {
  4774. // insert 1 goes only to 3; 3 goes to the Master: same sum (0.5 + 0.5)
  4775. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.setRoute (1, 3, true); s.setRoute (1, 0, false); }) - 1.0f) < 1e-4f);
  4776. // send at 50% and muted destination
  4777. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.setRoute (1, 3, true, 0.5f); s.setRoute (1, 0, false); }) - 0.75f) < 1e-4f);
  4778. 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);
  4779. // going to the Master and to 3: the signal doubles
  4780. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.setRoute (1, 3, true); }) - 1.5f) < 1e-4f);
  4781. // solo on 3 lets what reaches it sound (insert 1), but not 2
  4782. 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);
  4783. // solo on 1 lets the path to the Master (3) sound
  4784. 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);
  4785. // order and cycles
  4786. auto song = lynx::Song::makeDefault();
  4787. song.setRoute (4, 2, true);
  4788. song.setRoute (2, 1, true);
  4789. CHECK (! song.canRoute (1, 4) && ! song.canRoute (1, 2) && song.canRoute (1, 5));
  4790. song.setRoute (1, 4, true); // ignored: would create a cycle
  4791. CHECK (! song.routes (1, 4));
  4792. const auto order = song.mixerOrder();
  4793. const auto pos = [&] (int t) { return std::find (order.begin(), order.end(), t) - order.begin(); };
  4794. CHECK ((int) order.size() == (int) song.mixer.size() - 1);
  4795. CHECK (pos (4) < pos (2) && pos (2) < pos (1));
  4796. song.setRoute (4, 2, false);
  4797. CHECK (! song.routes (4, 2) && song.routes (4, 0));
  4798. // route levels (FL's route knobs): to inserts and to Master, clamped to the fader range; none without a route
  4799. song.setRoute (3, 5, true);
  4800. song.setRouteLevel (3, 5, 0.5f);
  4801. song.setRouteLevel (3, 0, 1.4f);
  4802. CHECK (song.routeLevel (3, 5) == 0.5f && song.routeLevel (3, 0) == 1.4f && song.mixer[3].masterLevel == 1.4f);
  4803. song.setRouteLevel (3, 0, 9.0f);
  4804. CHECK (song.routeLevel (3, 0) == lynx::maxFaderGain);
  4805. song.setRouteLevel (3, 6, 0.5f); // no such route: ignored
  4806. CHECK (! song.routes (3, 6) && song.routeLevel (3, 6) == 0.0f);
  4807. song.setRoute (3, 0, false);
  4808. CHECK (song.routeLevel (3, 0) == 0.0f);
  4809. // the route to Master's level scales what insert 1 sends there (0.5 + 0.5·0.25)
  4810. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.setRouteLevel (1, 0, 0.25f); }) - 0.625f) < 1e-4f);
  4811. }
  4812. namespace
  4813. {
  4814. // Test effect: delays the signal by `latency` samples and reports that latency (like a lookahead plugin).
  4815. class LatentEffect final : public lynx::Effect
  4816. {
  4817. public:
  4818. explicit LatentEffect (int samples) : latency (samples), line (samples) {}
  4819. void prepare (double) noexcept override {}
  4820. void reset() noexcept override {}
  4821. void process (float* l, float* r, int n, const lynx::EffectParams&, double) noexcept override { line.process (l, r, n); }
  4822. int latencySamples() const noexcept override { return latency; }
  4823. private:
  4824. int latency;
  4825. lynx::DelayLine line;
  4826. };
  4827. } // namespace
  4828. TEST (delay_line_mix_into_matches_process_and_drains_tail)
  4829. {
  4830. // mixInto = process over source*level, added to the destination; in stretches that cross the ring
  4831. lynx::DelayLine a (37), b (37);
  4832. std::vector<float> sl (300), sr (300), dl (300, 0.5f), dr (300, -0.5f), xl (300), xr (300);
  4833. for (int i = 0; i < 300; ++i)
  4834. {
  4835. sl[(std::size_t) i] = (float) std::sin (i * 0.1);
  4836. sr[(std::size_t) i] = (float) std::cos (i * 0.07);
  4837. }
  4838. for (int start = 0; start < 300; start += 100)
  4839. {
  4840. b.mixInto (sl.data() + start, sr.data() + start, 0.25f, dl.data() + start, dr.data() + start, 100);
  4841. for (int i = start; i < start + 100; ++i)
  4842. {
  4843. xl[(std::size_t) i] = sl[(std::size_t) i] * 0.25f;
  4844. xr[(std::size_t) i] = sr[(std::size_t) i] * 0.25f;
  4845. }
  4846. a.process (xl.data() + start, xr.data() + start, 100);
  4847. }
  4848. float worst = 0.0f;
  4849. for (int i = 0; i < 300; ++i)
  4850. worst = std::max ({ worst, std::abs (dl[(std::size_t) i] - 0.5f - xl[(std::size_t) i]),
  4851. std::abs (dr[(std::size_t) i] + 0.5f - xr[(std::size_t) i]) });
  4852. CHECK (worst < 1e-6f);
  4853. // silenced source: the tail still in the line comes out, then nothing remains
  4854. std::vector<float> tl (64, 0.0f), tr (64, 0.0f);
  4855. b.mixInto (nullptr, nullptr, 1.0f, tl.data(), tr.data(), 64);
  4856. CHECK (std::abs (tl[0] - sl[263] * 0.25f) < 1e-6f);
  4857. std::vector<float> zl (64, 0.0f), zr (64, 0.0f);
  4858. b.mixInto (nullptr, nullptr, 1.0f, zl.data(), zr.data(), 64);
  4859. CHECK (*std::max_element (zl.begin(), zl.end()) == 0.0f && *std::min_element (zl.begin(), zl.end()) == 0.0f);
  4860. }
  4861. TEST (limiter_lookahead_holds_ceiling_on_bursts)
  4862. {
  4863. // short bursts over a low floor: the sliding maximum must see each peak before it leaves
  4864. auto fx = lynx::makeEffect (lynx::EffectKind::limiter);
  4865. fx->prepare (48000.0);
  4866. auto slot = lynx::makeEffectSlot (lynx::EffectKind::limiter);
  4867. slot.params[2] = 0.0f; // 10 ms release
  4868. std::vector<float> l (48000), r (48000);
  4869. std::uint32_t seed = 1;
  4870. for (int i = 0; i < 48000; ++i)
  4871. {
  4872. seed = seed * 1664525u + 1013904223u;
  4873. const auto noise = ((float) (seed >> 8) / 16777216.0f - 0.5f) * 0.1f;
  4874. l[(std::size_t) i] = noise + ((i % 9973) < 3 ? 3.0f : 0.0f);
  4875. r[(std::size_t) i] = -noise;
  4876. }
  4877. for (int i = 0; i < 48000; i += 333)
  4878. fx->process (l.data() + i, r.data() + i, std::min (333, 48000 - i), slot.params, 120.0);
  4879. float peak = 0.0f, quiet = 0.0f;
  4880. for (int i = 0; i < 48000; ++i)
  4881. peak = std::max (peak, std::abs (l[(std::size_t) i]));
  4882. for (int i = 9973 * 2 + 2000; i < 9973 * 2 + 2400; ++i) // 40 ms after the burst the gain has already recovered
  4883. quiet = std::max (quiet, std::abs (r[(std::size_t) i]));
  4884. const auto ceiling = 1.0f; // the default ceiling is at 0 dBFS or below
  4885. CHECK (peak <= ceiling + 1e-5f);
  4886. CHECK (quiet > 0.03f);
  4887. }
  4888. TEST (pdc_aligns_paths_through_latent_effects)
  4889. {
  4890. // plan: channel 0 -> insert 1 (effect of 100) -> insert 3; channel 1 -> insert 2 -> Master
  4891. lynx::Song s;
  4892. s.mixer = lynx::Song::defaultMixer (4);
  4893. s.channels.resize (3);
  4894. s.channels[0].mixerTrack = 1;
  4895. s.channels[1].mixerTrack = 2;
  4896. s.channels[2].mixerTrack = 3;
  4897. s.mixer[1].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::eq));
  4898. s.setRoute (1, 3, true);
  4899. s.setRoute (1, 0, false);
  4900. auto plan = lynx::computePdc (s, { 0, 0, 30 }, { {}, { 100 } });
  4901. CHECK (plan.inAlign[1] == 0 && plan.outLat[1] == 100);
  4902. CHECK (plan.inAlign[3] == 100 && plan.channelDelay[2] == 70); // instrument of 30 on insert 3
  4903. CHECK (plan.sendDelay[1][1] == 0); // the slowest path isn't delayed
  4904. CHECK (plan.sendDelay[2][0] == 100 && plan.sendDelay[3][0] == 0 && plan.total == 100);
  4905. // bypass zeroes the slot's latency
  4906. s.mixer[1].effects[0].enabled = false;
  4907. CHECK (lynx::computePdc (s, { 0, 0, 30 }, { {}, { 100 } }).total == 30);
  4908. // rendered: two impulses played together come out together
  4909. lynx::Song r;
  4910. r.mixer = lynx::Song::defaultMixer (2);
  4911. for (int i = 0; i < 2; ++i)
  4912. {
  4913. lynx::ChannelDef c;
  4914. c.instrument = lynx::InstrumentKind::sampler;
  4915. c.samplePath = "impulso";
  4916. c.volume = 1.0f;
  4917. c.mixerTrack = i + 1;
  4918. r.channels.push_back (c);
  4919. }
  4920. r.patterns.emplace_back();
  4921. lynx::EffectSlot fx;
  4922. fx.kind = lynx::EffectKind::plugin;
  4923. fx.plugin.id = "latente";
  4924. r.mixer[1].effects.push_back (fx);
  4925. lynx::RackState::Factories f;
  4926. f.samples = [] (const std::string&) {
  4927. auto d = std::make_shared<lynx::SampleData>();
  4928. d->left.assign (64, 0.0f);
  4929. d->left[0] = 1.0f;
  4930. return std::shared_ptr<const lynx::SampleData> (d);
  4931. };
  4932. f.pluginEffects = [] (const lynx::PluginRef&, double) -> std::shared_ptr<lynx::Effect> { return std::make_shared<LatentEffect> (100); };
  4933. auto state = lynx::RackState::build (r, nullptr, 48000.0, f);
  4934. CHECK (state->pdc.total == 100 && state->sendDelays[2][0] != nullptr && state->sendDelays[2][0]->length() == 100);
  4935. lynx::RackRenderer renderer;
  4936. renderer.prepare (48000.0, 256);
  4937. lynx::LiveEventQueue live;
  4938. for (int ch = 0; ch < 2; ++ch)
  4939. {
  4940. lynx::NoteEvent on;
  4941. on.channel = ch;
  4942. on.key = 60;
  4943. on.velocity = 1.0f;
  4944. live.push (on);
  4945. }
  4946. std::vector<float> l (256), rr (256);
  4947. renderer.process (state.get(), false, 0.0, 256, l.data(), rr.data(), &live);
  4948. int first = -1;
  4949. for (int i = 0; i < 256 && first < 0; ++i)
  4950. if (std::abs (l[(std::size_t) i]) > 0.01f)
  4951. first = i;
  4952. CHECK (first == 100 && std::abs (l[100] - 2.0f) < 1e-3f); // both impulses arrive together, summed
  4953. // editing without changing latencies reuses the lines
  4954. r.channels[0].volume = 0.5f;
  4955. auto next = lynx::RackState::build (r, state.get(), 48000.0, f);
  4956. CHECK (next->sendDelays[2][0] == state->sendDelays[2][0]);
  4957. }
  4958. TEST (mixer_mute_fades_instead_of_clicking)
  4959. {
  4960. lynx::Song s;
  4961. s.mixer = lynx::Song::defaultMixer (2);
  4962. lynx::ChannelDef c;
  4963. c.instrument = lynx::InstrumentKind::sampler;
  4964. c.samplePath = "dc";
  4965. c.volume = 1.0f;
  4966. c.mixerTrack = 1;
  4967. s.channels.push_back (c);
  4968. s.patterns.emplace_back();
  4969. auto provider = [] (const std::string&) {
  4970. auto d = std::make_shared<lynx::SampleData>();
  4971. d->left.assign (48000, 0.5f);
  4972. return std::shared_ptr<const lynx::SampleData> (d);
  4973. };
  4974. auto on = lynx::RackState::build (s, nullptr, 48000.0, provider);
  4975. s.mixer[1].muted = true;
  4976. auto off = lynx::RackState::build (s, on.get(), 48000.0, provider);
  4977. lynx::RackRenderer r;
  4978. r.prepare (48000.0, 256);
  4979. lynx::LiveEventQueue live;
  4980. lynx::NoteEvent note;
  4981. note.velocity = 1.0f;
  4982. live.push (note);
  4983. std::vector<float> l (256), rr (256);
  4984. r.process (on.get(), false, 0.0, 256, l.data(), rr.data(), &live);
  4985. CHECK (std::abs (l[255] - 0.5f) < 1e-3f);
  4986. r.process (off.get(), false, 0.0, 256, l.data(), rr.data());
  4987. // the fader glides down by 0.001 per sample, as FL's (REA_rodada85 §3): 1000 samples from 1 to 0
  4988. CHECK (l[0] > 0.49f && std::abs (l[128] - 0.5f * (1.0f - 0.129f)) < 1e-3f && std::abs (l[255] - 0.5f * (1.0f - 0.256f)) < 1e-3f);
  4989. for (int b = 0; b < 3; ++b)
  4990. r.process (off.get(), false, 0.0, 256, l.data(), rr.data());
  4991. CHECK (std::abs (l[255]) < 1e-6f); // down to zero after 1000 samples
  4992. r.process (off.get(), false, 0.0, 256, l.data(), rr.data());
  4993. CHECK (std::abs (l[0]) < 1e-9f);
  4994. }
  4995. TEST (dither_quantizes_to_lsb_grid_with_small_error)
  4996. {
  4997. lynx::Dither d (16);
  4998. std::vector<float> x (4800);
  4999. for (std::size_t i = 0; i < x.size(); ++i)
  5000. x[i] = 0.25f * std::sin ((float) i * 0.05f);
  5001. auto y = x;
  5002. d.process (y.data(), (int) y.size(), 0);
  5003. double errorSum = 0.0;
  5004. bool onGrid = true;
  5005. for (std::size_t i = 0; i < x.size(); ++i)
  5006. {
  5007. const auto steps = (y[i] - d.writerBias()) * 32768.0f; // half an LSB above the grid point (round 160)
  5008. onGrid = onGrid && std::abs (steps - std::round (steps)) < 1e-3f;
  5009. errorSum += std::abs (y[i] - x[i]);
  5010. }
  5011. CHECK (onGrid);
  5012. CHECK (errorSum / (double) x.size() < 4.0 / 32768.0); // a few LSBs of noise on average
  5013. }
  5014. TEST (parallel_rendering_matches_sequential)
  5015. {
  5016. // 32 channels (worth parallelizing), inserts with chained sends
  5017. auto song = lynx::Song::makeDefault();
  5018. song.mixer = lynx::Song::defaultMixer (16);
  5019. for (int i = 5; i < 32; ++i)
  5020. {
  5021. lynx::ChannelDef c;
  5022. c.instrument = i % 2 ? lynx::InstrumentKind::synth : lynx::InstrumentKind::hat;
  5023. c.mixerTrack = 1 + i % 12;
  5024. c.volume = 0.3f;
  5025. song.channels.push_back (c);
  5026. for (int step = i % 4; step < 16; step += 3)
  5027. song.patterns[0].notes.push_back ({ (std::int64_t) step * lynx::ticksPerStep, 200, i, 48 + i, 0.7f });
  5028. }
  5029. song.patterns[0].sortNotes();
  5030. song.setRoute (3, 7, true, 0.6f);
  5031. song.setRoute (7, 9, true, 0.5f);
  5032. song.setRoute (2, 9, true);
  5033. song.mixer[9].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::reverb));
  5034. auto seqState = lynx::RackState::build (song, nullptr, 48000.0);
  5035. auto parState = lynx::RackState::build (song, nullptr, 48000.0);
  5036. CHECK (parState->parallel && parState->mixerLayers.size() == 3);
  5037. CHECK (parState->graphOk); // round 165i: the task graph runs it (channels and inserts by readiness)
  5038. lynx::WorkerPool pool (3);
  5039. lynx::RackRenderer seq, par;
  5040. seq.prepare (48000.0, 512);
  5041. par.prepare (48000.0, 512);
  5042. par.setWorkerPool (&pool);
  5043. std::vector<float> l1 (512), r1 (512), l2 (512), r2 (512);
  5044. const auto tps = lynx::ticksPerSample (song.bpm, 48000.0);
  5045. bool same = true;
  5046. float peak = 0.0f;
  5047. for (int block = 0; block < 200; ++block)
  5048. {
  5049. seq.process (seqState.get(), true, block * 512 * tps, 512, l1.data(), r1.data());
  5050. par.process (parState.get(), true, block * 512 * tps, 512, l2.data(), r2.data());
  5051. same = same && l1 == l2 && r1 == r2;
  5052. for (auto v : l1)
  5053. peak = std::max (peak, std::abs (v));
  5054. }
  5055. CHECK (same);
  5056. CHECK (peak > 0.05f);
  5057. // the pool handles many small rounds in a row
  5058. std::atomic<int> sum { 0 };
  5059. auto add = [&sum] (int i) { sum.fetch_add (i + 1); };
  5060. for (int round = 0; round < 2000; ++round)
  5061. pool.parallelFor (5, add);
  5062. CHECK (sum.load() == 2000 * 15);
  5063. }
  5064. TEST (rea165i_task_graph_matches_layers_with_sidechain_and_cycle)
  5065. {
  5066. // the task graph against the layers (LYNX_TASKGRAPH off is the same as a pool-less render): sends, a sidechain,
  5067. // a chain three inserts deep, channels on the Master; a routing cycle keeps the layers
  5068. auto song = lynx::Song::makeDefault();
  5069. song.mixer = lynx::Song::defaultMixer (16);
  5070. for (int i = 5; i < 40; ++i)
  5071. {
  5072. lynx::ChannelDef c;
  5073. c.instrument = i % 3 == 0 ? lynx::InstrumentKind::hat : lynx::InstrumentKind::synth;
  5074. c.mixerTrack = i % 7 == 0 ? 0 : 1 + i % 13;
  5075. c.volume = 0.25f;
  5076. song.channels.push_back (c);
  5077. for (int step = i % 5; step < 16; step += 2)
  5078. song.patterns[0].notes.push_back ({ (std::int64_t) step * lynx::ticksPerStep, 150, i, 40 + i, 0.6f });
  5079. }
  5080. song.patterns[0].sortNotes();
  5081. song.setRoute (3, 7, true, 0.6f);
  5082. song.setRoute (7, 9, true, 0.5f);
  5083. song.setRoute (9, 11, true, 0.5f);
  5084. song.mixer[9].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::reverb));
  5085. auto comp = lynx::makeEffectSlot (lynx::EffectKind::compressor);
  5086. comp.sidechain = 4;
  5087. song.mixer[11].effects.push_back (comp);
  5088. auto render = [] (const lynx::Song& s, bool pool, bool& graph) {
  5089. auto state = lynx::RackState::build (s, nullptr, 48000.0);
  5090. graph = state->graphOk;
  5091. lynx::WorkerPool workers (4);
  5092. lynx::RackRenderer r;
  5093. r.prepare (48000.0, 512);
  5094. if (pool)
  5095. r.setWorkerPool (&workers);
  5096. std::vector<float> out, l (384), rr (384);
  5097. const auto tps = lynx::ticksPerSample (s.bpm, 48000.0);
  5098. for (int block = 0; block < 150; ++block)
  5099. {
  5100. r.process (state.get(), true, block * 384 * tps, 384, l.data(), rr.data());
  5101. out.insert (out.end(), l.begin(), l.end());
  5102. out.insert (out.end(), rr.begin(), rr.end());
  5103. }
  5104. return out;
  5105. };
  5106. bool graph = false, unused = false;
  5107. const auto layered = render (song, false, unused);
  5108. const auto parallel = render (song, true, graph);
  5109. CHECK (graph);
  5110. CHECK (layered == parallel);
  5111. CHECK (*std::max_element (layered.begin(), layered.end()) > 0.02f);
  5112. song.mixer[11].sends.push_back ({ 3, 0.3f }); // a cycle 3 → 7 → 9 → 11 → 3 (setRoute refuses it; an old project may hold one)
  5113. const auto cycleLayered = render (song, false, unused);
  5114. const auto cycleParallel = render (song, true, graph);
  5115. CHECK (! graph);
  5116. CHECK (cycleLayered == cycleParallel);
  5117. }
  5118. TEST (rea165i_test_profile_and_test_mode_parsing)
  5119. {
  5120. using namespace lynx::testprofile;
  5121. CHECK (! profileDirFrom (nullptr));
  5122. CHECK (! profileDirFrom (""));
  5123. CHECK (! profileDirFrom ("relative/dir")); // only absolute folders
  5124. CHECK (! profileDirFrom ("C:relative"));
  5125. CHECK (profileDirFrom ("/tmp/lynx-profile/") == std::string ("/tmp/lynx-profile"));
  5126. CHECK (profileDirFrom (" \"C:\\Users\\me\\bench profile\\\" ") == std::string ("C:\\Users\\me\\bench profile"));
  5127. CHECK (profileDirFrom ("D:/x") == std::string ("D:/x"));
  5128. CHECK (profileDirFrom ("C:\\") == std::string ("C:\\")); // a drive root keeps its separator
  5129. CHECK (! testModeFrom (nullptr));
  5130. CHECK (! testModeFrom (""));
  5131. CHECK (! testModeFrom ("0"));
  5132. CHECK (! testModeFrom (" Off "));
  5133. CHECK (! testModeFrom ("false"));
  5134. CHECK (testModeFrom ("1"));
  5135. CHECK (testModeFrom ("yes"));
  5136. }
  5137. TEST (rea165i_keep_float_mode_restores_ftz_after_foreign_code)
  5138. {
  5139. // a hosted plugin that turns flush-to-zero off (or changes the rounding) no longer leaks it into LYNX's DSP
  5140. const lynx::ScopedFlushDenormals ftz;
  5141. const auto before = _mm_getcsr();
  5142. {
  5143. const lynx::KeepFloatMode keep;
  5144. _mm_setcsr ((before & ~0x8040u) | 0x6000u); // what a plugin might leave: no FTZ/DAZ, round toward zero
  5145. }
  5146. CHECK (_mm_getcsr() == before);
  5147. volatile float tiny = 1.0e-38f;
  5148. volatile float smaller = tiny * 1.0e-3f; // denormal without FTZ
  5149. CHECK (smaller == 0.0f);
  5150. }
  5151. TEST (arrangements_switch_and_keep_clip_indices)
  5152. {
  5153. auto s = lynx::Song::makeDefault();
  5154. s.patterns.emplace_back().name = "B";
  5155. s.playlist = { { 1, 0, 0, 960, 0 } };
  5156. s.loopEnd = 960;
  5157. CHECK (s.arrangements.size() == 1 && s.currentArrangement == 0);
  5158. const auto second = s.addArrangement ("Versão curta", true);
  5159. CHECK (second == 1 && s.currentArrangement == 1 && s.playlist.size() == 1 && s.loopEnd == 960);
  5160. s.playlist.push_back ({ 0, 1, 960, 960, 0 });
  5161. s.loopEnd = 0;
  5162. s.selectArrangement (0);
  5163. CHECK (s.playlist.size() == 1 && s.loopEnd == 960 && s.arrangements[1].clips.size() == 2);
  5164. // removing pattern 0 fixes the clips stored in the other arrangement
  5165. s.removePattern (0);
  5166. CHECK (s.playlist.size() == 1 && s.playlist[0].pattern == 0);
  5167. CHECK (s.arrangements[1].clips.size() == 1 && s.arrangements[1].clips[0].pattern == 0);
  5168. s.removeArrangement (0);
  5169. CHECK (s.arrangements.size() == 1 && s.currentArrangement == 0 && s.playlist.size() == 1 && s.loopEnd == 0);
  5170. }
  5171. namespace
  5172. {
  5173. // Test effect that inverts the signal and counts how many times it processed.
  5174. class CountingInverter final : public lynx::Effect
  5175. {
  5176. public:
  5177. void prepare (double) noexcept override {}
  5178. void reset() noexcept override {}
  5179. void process (float* l, float* r, int n, const lynx::EffectParams&, double) noexcept override
  5180. {
  5181. ++calls;
  5182. for (int i = 0; i < n; ++i)
  5183. {
  5184. l[i] = -l[i];
  5185. r[i] = -r[i];
  5186. }
  5187. }
  5188. int calls = 0;
  5189. };
  5190. } // namespace
  5191. TEST (keyboard_controller_outputs_key_value)
  5192. {
  5193. std::vector<std::uint8_t> blob (554, 0);
  5194. const std::uint32_t version = 3;
  5195. std::memcpy (blob.data(), &version, 4);
  5196. const float key60 = 0.7f, off = 0.2f;
  5197. std::memcpy (blob.data() + 12 + 60 * 4, &key60, 4);
  5198. std::memcpy (blob.data() + 548, &off, 4);
  5199. blob[552] = 1; // auto release
  5200. lynx::ChannelDef c;
  5201. c.instrument = lynx::InstrumentKind::controller;
  5202. c.controllerState = blob;
  5203. auto inst = lynx::makeInstrument (lynx::InstrumentKind::controller);
  5204. inst->prepare (48000.0);
  5205. inst->configure (c, 120.0);
  5206. std::vector<float> l (64), r (64);
  5207. inst->noteOn (60, 1.0f);
  5208. inst->render (l.data(), r.data(), 64, 1.0f, 1.0f);
  5209. CHECK (std::abs (inst->controlOutput (0) - 0.7f) < 1e-6f);
  5210. inst->noteOff (60);
  5211. CHECK (std::abs (inst->controlOutput (0) - 0.2f) < 1e-6f);
  5212. }
  5213. TEST (envelope_controller_ducks_on_each_note)
  5214. {
  5215. // a unipolar articulator like "Basic Sidechain": base 0.05, drops to 0 on the note and rises to 1 in 0.3 s
  5216. lynx::ControllerArticulator a;
  5217. a.envOn = true;
  5218. a.base = 0.05f;
  5219. a.envLevel = 1.0f;
  5220. a.count = 3;
  5221. a.time = { 0.0f, 0.2f, 0.3f };
  5222. a.level = { 0.0f, 0.75f, 1.0f };
  5223. CHECK (std::abs (lynx::controllerValue (a, 0.0, -1.0) - 0.05f) < 1e-4f);
  5224. CHECK (std::abs (lynx::controllerValue (a, 0.1, -1.0) - 0.425f) < 1e-3f);
  5225. CHECK (lynx::controllerValue (a, 1.0, -1.0) > 0.99f); // no sustain: runs to the end and stays
  5226. a.sustain = 1;
  5227. CHECK (std::abs (lynx::controllerValue (a, 1.0, -1.0) - 0.8f) < 1e-3f); // held at sustain
  5228. CHECK (lynx::controllerValue (a, 1.0, 0.5) > 0.99f); // released: continues to the end
  5229. a.bipolar = true;
  5230. a.base = 0.5f;
  5231. a.sustain = -1;
  5232. CHECK (std::abs (lynx::controllerValue (a, 0.0, -1.0) - 0.0f) < 1e-4f); // bipolar: 0.5 + (0 − 0.5)
  5233. }
  5234. TEST (peak_controller_drives_live_automation)
  5235. {
  5236. // track 1: source (DC 0.5) with Peak Controller; track 2: DC 0.5 with volume linked to the controller
  5237. auto render = [] (bool linked) {
  5238. lynx::Song s;
  5239. s.mixer = lynx::Song::defaultMixer (2);
  5240. for (int t = 1; t <= 2; ++t)
  5241. {
  5242. lynx::ChannelDef c;
  5243. c.instrument = lynx::InstrumentKind::sampler;
  5244. c.samplePath = "dc";
  5245. c.volume = 1.0f;
  5246. c.mixerTrack = t;
  5247. s.channels.push_back (c);
  5248. }
  5249. s.patterns.emplace_back();
  5250. auto peak = lynx::makeEffectSlot (lynx::EffectKind::peakController);
  5251. peak.params[0] = 1.0f; // base 1
  5252. peak.params[1] = 0.0f; // amount −1: 1 − peak
  5253. peak.params[2] = 0.5f; // no tension
  5254. s.mixer[1].effects.push_back (peak);
  5255. if (linked)
  5256. {
  5257. lynx::Automation a;
  5258. a.name = "Peak";
  5259. a.target = { lynx::AutomationTargetKind::mixerVolume, 2 };
  5260. a.source = { 1, 0, 0 };
  5261. s.automations.push_back (a);
  5262. }
  5263. lynx::RackState::Factories f;
  5264. f.samples = [] (const std::string&) {
  5265. auto d = std::make_shared<lynx::SampleData>();
  5266. d->left.assign (48000, 0.5f);
  5267. return std::shared_ptr<const lynx::SampleData> (d);
  5268. };
  5269. auto state = lynx::RackState::build (s, nullptr, 48000.0, f);
  5270. lynx::RackRenderer r;
  5271. r.prepare (48000.0, 512);
  5272. lynx::LiveEventQueue live;
  5273. for (int ch = 0; ch < 2; ++ch)
  5274. {
  5275. lynx::NoteEvent on;
  5276. on.channel = ch;
  5277. on.velocity = 1.0f;
  5278. live.push (on);
  5279. }
  5280. std::vector<float> l (4800), rr (4800);
  5281. r.process (state.get(), false, 0.0, 4800, l.data(), rr.data(), &live);
  5282. return l[4000];
  5283. };
  5284. const auto free = render (false), ducked = render (true);
  5285. CHECK (std::isfinite (ducked) && ducked > 0.0f);
  5286. CHECK (ducked < free - 0.05f); // the 0.5 peak on track 1 lowers track 2's volume
  5287. }
  5288. TEST (effect_slot_dry_wet_and_smart_disable)
  5289. {
  5290. lynx::Song s;
  5291. s.mixer = lynx::Song::defaultMixer (1);
  5292. lynx::ChannelDef c;
  5293. c.instrument = lynx::InstrumentKind::sampler;
  5294. c.samplePath = "dc";
  5295. c.volume = 1.0f;
  5296. c.mixerTrack = 1;
  5297. s.channels.push_back (c);
  5298. s.patterns.emplace_back();
  5299. lynx::EffectSlot fx;
  5300. fx.kind = lynx::EffectKind::plugin;
  5301. fx.plugin.id = "inversor";
  5302. fx.mix = 0.25f; // 75% dry (+0.5) and 25% wet (-0.5) = +0.25
  5303. s.mixer[1].effects.push_back (fx);
  5304. auto inverter = std::make_shared<CountingInverter>();
  5305. lynx::RackState::Factories f;
  5306. f.samples = [] (const std::string&) {
  5307. auto d = std::make_shared<lynx::SampleData>();
  5308. d->left.assign (4800, 0.5f);
  5309. return std::shared_ptr<const lynx::SampleData> (d);
  5310. };
  5311. f.pluginEffects = [inverter] (const lynx::PluginRef&, double) -> std::shared_ptr<lynx::Effect> { return inverter; };
  5312. auto state = lynx::RackState::build (s, nullptr, 48000.0, f);
  5313. lynx::RackRenderer r;
  5314. r.prepare (48000.0, 512);
  5315. lynx::LiveEventQueue live;
  5316. lynx::NoteEvent on;
  5317. on.velocity = 1.0f;
  5318. live.push (on);
  5319. std::vector<float> l (480), rr (480);
  5320. r.process (state.get(), false, 0.0, 480, l.data(), rr.data(), &live);
  5321. CHECK (std::abs (l[100] - 0.25f) < 1e-4f);
  5322. // after the sample ends: 4 s of silence and the plugin stops being called
  5323. for (int i = 0; i < 100 * 6; ++i) // 6 s
  5324. r.process (state.get(), false, 0.0, 480, l.data(), rr.data());
  5325. const auto callsAfterSilence = inverter->calls;
  5326. for (int i = 0; i < 100; ++i)
  5327. r.process (state.get(), false, 0.0, 480, l.data(), rr.data());
  5328. CHECK (inverter->calls == callsAfterSilence);
  5329. CHECK (callsAfterSilence < 100 * 6);
  5330. // sound again: processing resumes in the same block
  5331. live.push (on);
  5332. r.process (state.get(), false, 0.0, 480, l.data(), rr.data(), &live);
  5333. CHECK (inverter->calls == callsAfterSilence + 1 && std::abs (l[100] - 0.25f) < 1e-4f);
  5334. }
  5335. TEST (swing_delays_offbeat_sixteenths)
  5336. {
  5337. CHECK (lynx::swingDelay (0, 1.0f) == 0);
  5338. CHECK (lynx::swingDelay (lynx::ticksPerStep, 1.0f) == lynx::ticksPerStep / 2);
  5339. CHECK (lynx::swingDelay (lynx::ticksPerStep * 2, 1.0f) == 0);
  5340. CHECK (lynx::swingDelay (lynx::ticksPerStep + 10, 1.0f) == (lynx::ticksPerStep - 20) / 2); // off the grid: triangular weight (FL)
  5341. CHECK (lynx::swingDelay (lynx::ticksPerStep, 1.0f, 0.5f) == lynx::ticksPerStep / 4); // channel mix
  5342. CHECK (lynx::swingDelay (lynx::ticksPerStep * 3, 0.5f) == lynx::ticksPerStep / 4);
  5343. lynx::Pattern p;
  5344. p.notes = { { 0, 100, 0, 60 }, { lynx::ticksPerStep, 100, 0, 62 } };
  5345. lynx::EventBuffer ev;
  5346. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  5347. lynx::schedulePatternLoop (p, p.effectiveLength (4), 0.0, 12000, tps, ev, 1.0f);
  5348. int onOffset = -1;
  5349. for (auto& e : ev)
  5350. if (e.type == lynx::NoteEvent::Type::on && e.key == 62)
  5351. onOffset = e.sampleOffset;
  5352. CHECK (onOffset == (int) std::floor ((lynx::ticksPerStep * 1.5) / tps));
  5353. }
  5354. TEST (sampler_settings_pitch_reverse_envelope_cut)
  5355. {
  5356. auto ramp = std::make_shared<lynx::SampleData>();
  5357. for (int i = 0; i < 48000; ++i)
  5358. ramp->left.push_back ((float) i / 48000.0f); // ramp: the value tells where the read position is
  5359. ramp->sampleRate = 48000.0;
  5360. auto play = [&] (const lynx::SamplerSettings& s, int key, int frames, bool releaseAfterHalf) {
  5361. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  5362. inst->prepare (48000.0);
  5363. lynx::ChannelDef c;
  5364. c.sampler = s;
  5365. inst->configure (c, 120.0);
  5366. inst->noteOn (key, 1.0f);
  5367. std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
  5368. inst->render (l.data(), r.data(), frames / 2, 1.0f, 1.0f);
  5369. if (releaseAfterHalf)
  5370. inst->noteOff (key);
  5371. inst->render (l.data() + frames / 2, r.data() + frames / 2, frames - frames / 2, 1.0f, 1.0f);
  5372. return l;
  5373. };
  5374. // +12 semitones: reads twice as fast
  5375. lynx::SamplerSettings up;
  5376. up.pitch = 12.0f;
  5377. CHECK (std::abs (play (up, 60, 1000, false)[999] - 2.0f * 999.0f / 48000.0f) < 1e-3f);
  5378. // reversed: starts from the end
  5379. lynx::SamplerSettings back;
  5380. back.reverse = true;
  5381. CHECK (play (back, 60, 100, false)[10] > 0.99f);
  5382. // 10 ms attack: starts silent and reaches full level
  5383. lynx::SamplerSettings slow;
  5384. slow.attack = 0.01f;
  5385. const auto a = play (slow, 60, 2000, false);
  5386. CHECK (a[10] < 0.2f * (10.0f / 48000.0f) + 1e-6f && std::abs (a[1500] - 1500.0f / 48000.0f) < 1e-4f);
  5387. // 5 ms release: gone right after release; without release it plays in full
  5388. lynx::SamplerSettings rel;
  5389. rel.release = 0.005f;
  5390. CHECK (play (rel, 60, 2000, true)[1999] == 0.0f);
  5391. CHECK (play (lynx::SamplerSettings {}, 60, 2000, true)[1999] > 0.0f);
  5392. // cut itself: the second note cuts the first (one voice remains)
  5393. lynx::SamplerSettings cut;
  5394. cut.cut = true;
  5395. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  5396. inst->prepare (48000.0);
  5397. lynx::ChannelDef cutChannel;
  5398. cutChannel.sampler = cut;
  5399. inst->configure (cutChannel, 120.0);
  5400. inst->noteOn (60, 1.0f);
  5401. std::vector<float> l (2000), r (2000);
  5402. inst->render (l.data(), r.data(), 1000, 1.0f, 1.0f);
  5403. inst->noteOn (60, 1.0f);
  5404. std::fill (l.begin(), l.end(), 0.0f);
  5405. inst->render (l.data(), r.data(), 2000, 1.0f, 1.0f);
  5406. CHECK (std::abs (l[1999] - 1999.0f / 48000.0f) < 1e-4f); // only the new note
  5407. }
  5408. TEST (playlist_split_and_track_mute)
  5409. {
  5410. lynx::Clip c { 0, 3, 960, 3840, 480 };
  5411. const auto rest = lynx::splitClip (c, 2000);
  5412. CHECK (c.start == 960 && c.length == 1040 && c.offset == 480);
  5413. CHECK (rest.start == 2000 && rest.length == 2800 && rest.offset == 480 + 1040 && rest.track == 3);
  5414. auto s = lynx::Song::makeDefault();
  5415. s.mode = lynx::PlayMode::song;
  5416. s.playlist = { { 0, 0, 0, 3840, 0 } };
  5417. lynx::EventBuffer ev;
  5418. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  5419. lynx::scheduleSong (s, 0.0, 48000, tps, ev);
  5420. CHECK (ev.size() > 0);
  5421. s.playlistTracks.resize (1);
  5422. s.playlistTracks[0].muted = true;
  5423. ev.clear();
  5424. lynx::scheduleSong (s, 0.0, 48000, tps, ev);
  5425. CHECK (ev.size() == 0);
  5426. }
  5427. TEST (note_pan_pitch_and_mute)
  5428. {
  5429. lynx::Pattern p;
  5430. lynx::Note a { 0, 240, 0, 60 };
  5431. a.pan = -1.0f;
  5432. a.finePitch = 50.0f;
  5433. lynx::Note b { 240, 240, 0, 62 };
  5434. b.muted = true;
  5435. p.notes = { a, b };
  5436. lynx::EventBuffer ev;
  5437. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  5438. lynx::schedulePatternLoop (p, p.effectiveLength (4), 0.0, 48000, tps, ev);
  5439. int ons = 0;
  5440. for (auto& e : ev)
  5441. if (e.type == lynx::NoteEvent::Type::on)
  5442. {
  5443. ++ons;
  5444. CHECK (e.key == 60 && e.pan == -1.0f && e.cents == 50.0f);
  5445. }
  5446. CHECK (ons == 1); // the muted one doesn't play
  5447. // note pan in the sampler: fully left
  5448. auto dc = std::make_shared<lynx::SampleData>();
  5449. dc->left.assign (4800, 0.5f);
  5450. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, dc);
  5451. inst->prepare (48000.0);
  5452. inst->setNextNoteExpression (-1.0f, 0.0f);
  5453. inst->noteOn (60, 1.0f);
  5454. std::vector<float> l (100), r (100);
  5455. inst->render (l.data(), r.data(), 100, 1.0f, 1.0f);
  5456. CHECK (l[50] > 0.6f && std::abs (r[50]) < 1e-6f);
  5457. }
  5458. TEST (tempo_automation_changes_speed_and_duration)
  5459. {
  5460. auto song = lynx::Song::makeDefault();
  5461. song.bpm = 120.0;
  5462. song.mode = lynx::PlayMode::song;
  5463. const auto bar = (std::int64_t) lynx::ppq * 4;
  5464. song.playlist = { { 0, 0, 0, bar, 0 }, { 0, 0, bar, bar, 0 } };
  5465. lynx::Automation tempo;
  5466. tempo.target = { lynx::AutomationTargetKind::tempo, 0 };
  5467. const auto v120 = lynx::valueFromTempo (120.0), v240 = lynx::valueFromTempo (240.0);
  5468. tempo.points = { { 0, v120 }, { bar - 1, v120 }, { bar, v240 } }; // step: 120 -> 240 at bar 2
  5469. song.automations.push_back (tempo);
  5470. lynx::Clip clip;
  5471. clip.kind = lynx::ClipKind::automation;
  5472. clip.automation = 0;
  5473. clip.track = 1;
  5474. clip.length = bar * 2;
  5475. song.playlist.push_back (clip);
  5476. CHECK (std::abs (song.tempoAt (100.0) - 120.0) < 0.01 && std::abs (song.tempoAt ((double) bar + 100.0) - 240.0) < 0.01);
  5477. // 1 bar at 120 (2 s) + 1 at 240 (1 s)
  5478. CHECK (std::abs (song.secondsForTicks (bar * 2) - 3.0) < 0.01);
  5479. auto state = lynx::RackState::build (song, nullptr, 48000.0);
  5480. CHECK (state->tempoAutomation == 0);
  5481. lynx::RackRenderer r;
  5482. r.prepare (48000.0, 512);
  5483. std::vector<float> l (512), rr (512);
  5484. r.process (state.get(), true, 10.0, 512, l.data(), rr.data());
  5485. CHECK (std::abs (r.currentBpm() - 120.0) < 0.01);
  5486. r.process (state.get(), true, (double) bar + 10.0, 512, l.data(), rr.data());
  5487. CHECK (std::abs (r.currentBpm() - 240.0) < 0.01);
  5488. }
  5489. TEST (bounce_insert_song_and_apply)
  5490. {
  5491. auto s = lynx::Song::makeDefault();
  5492. s.setRoute (3, 7, true, 0.5f);
  5493. s.mixer[0].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::reverb));
  5494. s.mixer[2].solo = true;
  5495. const auto b = lynx::makeBounceSong (s, 3);
  5496. CHECK (b.mixer[3].solo && ! b.mixer[2].solo && b.mixer[3].toMaster && b.mixer[3].sends.empty());
  5497. CHECK (b.mixer[0].effects.empty() && b.mode == lynx::PlayMode::song);
  5498. const auto audible = b.soloAudible();
  5499. CHECK (audible[3] && ! audible[1] && ! audible[7]);
  5500. const auto channels = s.channels.size();
  5501. const auto inserts = s.mixer.size();
  5502. const auto created = lynx::applyBounce (s, 3, "C:/x.wav", 3840);
  5503. CHECK (created == (int) inserts && s.mixer.size() == inserts + 1 && s.mixer[3].muted);
  5504. CHECK (s.mixer[(std::size_t) created].sends.size() == 1 && s.mixer[(std::size_t) created].sends[0].target == 7);
  5505. CHECK (s.channels.size() == channels + 1 && s.channels.back().mixerTrack == created);
  5506. CHECK (s.playlist.back().kind == lynx::ClipKind::audio && s.playlist.back().length == 3840);
  5507. }
  5508. TEST (mixer_stereo_separation_phase_swap)
  5509. {
  5510. // FL's level matrix (REA_rodada2 §2.2, REA_rodada167j §1): outL = ll·inL + rl·inR, outR = rr·inR + lr·inL
  5511. lynx::MixerTrack t;
  5512. auto near = [] (float a, float b) { return std::abs (a - b) < 1e-5f; };
  5513. t.stereo = 1.0f; // mono
  5514. auto m = lynx::mixerMatrix (t, lynx::unityFader, 0.0f, false);
  5515. CHECK (near (m.ll, 0.5f) && near (m.rl, 0.5f) && near (m.rr, 0.5f) && near (m.lr, 0.5f));
  5516. t.stereo = -1.0f; // doubled side: 1.5 L - 0.5 R
  5517. m = lynx::mixerMatrix (t, lynx::unityFader, 0.0f, false);
  5518. CHECK (near (m.ll, 1.5f) && near (m.rl, -0.5f) && near (m.rr, 1.5f) && near (m.lr, -0.5f));
  5519. t.stereo = 0.0f;
  5520. t.swapChannels = true;
  5521. t.invertPhase = true;
  5522. m = lynx::mixerMatrix (t, lynx::unityFader, 0.0f, false);
  5523. CHECK (m.ll == 0.0f && m.rl == -1.0f && m.rr == 0.0f && m.lr == -1.0f); // outL = −inR, outR = −inL
  5524. // unity, centre, no separation: exactly the identity (FL's fast path)
  5525. m = lynx::mixerMatrix (lynx::MixerTrack {}, lynx::unityFader, 0.0f, false);
  5526. CHECK (m == lynx::MixerMatrix {});
  5527. }
  5528. TEST (mixer_pan_fold_and_panning_law_match_fl_render)
  5529. {
  5530. // FL 2026's render of j_tri.flp (round 167j: a 441 Hz left / 882 Hz right sample on ten inserts, pan law
  5531. // triangular), the matrix fitted per insert: {pan, separation, ll, rl, lr, rr}
  5532. struct Case { float pan, sep, ll, rl, lr, rr; };
  5533. const Case fl[] = { { -1.0f, 0.0f, 0.499982f, 0.499972f, 0.0f, 0.0f }, { -0.5f, 0.0f, 0.749991f, 0.249984f, 0.0f, 0.499971f },
  5534. { 0.25f, 0.0f, 0.75f, 0.0f, 0.125001f, 0.874978f }, { 0.5f, 0.0f, 0.499985f, 0.0f, 0.250011f, 0.749964f },
  5535. { 1.0f, 0.0f, 0.0f, 0.0f, 0.499982f, 0.499972f }, { 0.5f, 0.5f, 0.422756f, 0.077253f, 0.4045f, 0.595469f },
  5536. { -0.5f, -0.5f, 1.0f, 0.0f, -0.125004f, 0.624974f }, { 0.75f, 0.0f, 0.250003f, 0.0f, 0.375006f, 0.624975f },
  5537. { 0.0f, 0.75f, 0.70703f, 0.292941f, 0.292965f, 0.707045f } };
  5538. for (const auto& c : fl)
  5539. {
  5540. lynx::MixerTrack t;
  5541. t.stereo = c.sep;
  5542. const auto m = lynx::mixerMatrix (t, lynx::unityFader, c.pan, true);
  5543. // the fit's own error is ~5e-5 (16-bit render)
  5544. CHECK (std::abs (m.ll - c.ll) < 2e-4f && std::abs (m.rl - c.rl) < 2e-4f && std::abs (m.lr - c.lr) < 2e-4f
  5545. && std::abs (m.rr - c.rr) < 2e-4f);
  5546. }
  5547. // circular (the default): +3 dB on the side panned to, and the folded half of the other side
  5548. const auto hard = lynx::mixerMatrix (lynx::MixerTrack {}, lynx::unityFader, 1.0f, false);
  5549. CHECK (std::abs (hard.ll) < 1e-6f && std::abs (hard.rl) < 1e-6f);
  5550. CHECK (std::abs (hard.rr - 0.70710677f) < 1e-6f && std::abs (hard.lr - 0.70710677f) < 1e-6f);
  5551. const auto half = lynx::mixerMatrix (lynx::MixerTrack {}, lynx::unityFader, 0.5f, false);
  5552. const auto gl = (float) std::cos (0.75 * 3.14159265358979 / 2.0) * 1.4142135f, gr = (float) std::sin (0.75 * 3.14159265358979 / 2.0) * 1.4142135f;
  5553. CHECK (std::abs (half.ll - gl) < 1e-5f && half.rl == 0.0f);
  5554. CHECK (std::abs (half.rr - gr * (1.0f - (1.0f - gl) * 0.5f)) < 1e-5f && std::abs (half.lr - gr * (1.0f - gl) * 0.5f) < 1e-5f);
  5555. // the fader scales the whole matrix (−6 dB-ish position), polarity negates it
  5556. lynx::MixerTrack inv;
  5557. inv.invertPhase = true;
  5558. const auto low = lynx::mixerMatrix (inv, 0.5f, 0.0f, false);
  5559. const auto g = (float) ((std::exp ((double) (0.625f * 2.3978953f)) - 1.0) * 0.1);
  5560. CHECK (std::abs (low.ll + g) < 1e-5f && std::abs (low.rr + g) < 1e-5f && std::abs (low.rl) < 1e-6f);
  5561. }
  5562. TEST (effects_limiter_filter_distortion_chorus)
  5563. {
  5564. const double sr = 48000.0;
  5565. auto sine = [] (float hz, float amp, int n) {
  5566. std::vector<float> v ((std::size_t) n);
  5567. for (int i = 0; i < n; ++i)
  5568. v[(std::size_t) i] = amp * std::sin (2.0f * 3.14159265f * hz * (float) i / 48000.0f);
  5569. return v;
  5570. };
  5571. auto peak = [] (const std::vector<float>& v, int from) {
  5572. float p = 0.0f;
  5573. for (std::size_t i = (std::size_t) from; i < v.size(); ++i)
  5574. p = std::max (p, std::abs (v[i]));
  5575. return p;
  5576. };
  5577. // limiter: +12 dB on a 0.9 sine never exceeds the ceiling (-1 dB) and reports the latency
  5578. auto lim = lynx::makeEffect (lynx::EffectKind::limiter);
  5579. lim->prepare (sr);
  5580. auto slot = lynx::makeEffectSlot (lynx::EffectKind::limiter);
  5581. slot.params[0] = 11.0f / 12.0f; // -1 dB
  5582. slot.params[1] = 0.5f; // +12 dB
  5583. auto l = sine (100.0f, 0.9f, 48000), r = l;
  5584. lim->process (l.data(), r.data(), 48000, slot.params, 120.0);
  5585. CHECK (peak (l, 0) <= std::pow (10.0f, -1.0f / 20.0f) + 1e-4f);
  5586. CHECK (peak (l, 4800) > 0.8f);
  5587. CHECK (lim->latencySamples() == 72);
  5588. // low-pass filter at 500 Hz knocks down an 8 kHz sine
  5589. auto lp = lynx::makeEffect (lynx::EffectKind::filter);
  5590. lp->prepare (sr);
  5591. auto fs = lynx::makeEffectSlot (lynx::EffectKind::filter);
  5592. fs.params[1] = std::log (500.0f / 20.0f) / std::log (1000.0f);
  5593. l = sine (8000.0f, 1.0f, 9600);
  5594. r = l;
  5595. lp->process (l.data(), r.data(), 9600, fs.params, 120.0);
  5596. CHECK (peak (l, 4800) < 0.05f);
  5597. // distortion and chorus: finite, audible output
  5598. for (auto kind : { lynx::EffectKind::distortion, lynx::EffectKind::chorus })
  5599. {
  5600. auto fx = lynx::makeEffect (kind);
  5601. fx->prepare (sr);
  5602. auto ps = lynx::makeEffectSlot (kind);
  5603. l = sine (220.0f, 0.5f, 9600);
  5604. r = l;
  5605. fx->process (l.data(), r.data(), 9600, ps.params, 120.0);
  5606. bool finite = true;
  5607. for (auto v : l)
  5608. finite = finite && std::isfinite (v);
  5609. CHECK (finite && peak (l, 2400) > 0.1f && peak (l, 0) < 2.0f);
  5610. }
  5611. }
  5612. TEST (slide_notes_glide_without_retrigger)
  5613. {
  5614. // sampler with a ramp: the read speed doubles after a one-octave slide
  5615. auto ramp = std::make_shared<lynx::SampleData>();
  5616. for (int i = 0; i < 96000; ++i)
  5617. ramp->left.push_back ((float) i / 96000.0f);
  5618. ramp->sampleRate = 48000.0;
  5619. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  5620. inst->prepare (48000.0);
  5621. inst->noteOn (60, 1.0f);
  5622. std::vector<float> l (2000), r (2000);
  5623. inst->render (l.data(), r.data(), 1000, 1.0f, 1.0f);
  5624. const auto stepBefore = l[999] - l[998];
  5625. inst->slideTo (72, 0.0f, 100.0f);
  5626. std::fill (l.begin(), l.end(), 0.0f);
  5627. inst->render (l.data(), r.data(), 1000, 1.0f, 1.0f);
  5628. const auto stepAfter = l[999] - l[998];
  5629. CHECK (std::abs (stepAfter / stepBefore - 2.0f) < 0.01f);
  5630. // the sequencer sends "slide" (no off) for the slide note
  5631. lynx::Pattern p;
  5632. lynx::Note base { 0, 960, 0, 48 };
  5633. lynx::Note glide { 480, 240, 0, 55 };
  5634. glide.slide = true;
  5635. p.notes = { base, glide };
  5636. lynx::EventBuffer ev;
  5637. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  5638. lynx::schedulePatternLoop (p, p.effectiveLength (4), 0.0, 30000, tps, ev);
  5639. int ons = 0, offs = 0, slides = 0;
  5640. for (auto& e : ev)
  5641. {
  5642. ons += e.type == lynx::NoteEvent::Type::on;
  5643. offs += e.type == lynx::NoteEvent::Type::off;
  5644. if (e.type == lynx::NoteEvent::Type::slide)
  5645. {
  5646. ++slides;
  5647. CHECK (e.key == 55 && std::abs (e.slideSamples - 240.0f / (float) tps) < 1.0f);
  5648. }
  5649. }
  5650. CHECK (ons == 1 && offs == 1 && slides == 1);
  5651. }
  5652. TEST (synth_fm_phase_modulation)
  5653. {
  5654. auto render = [] (const lynx::SynthSettings& s) {
  5655. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  5656. inst->prepare (48000.0);
  5657. lynx::ChannelDef c;
  5658. c.synth = s;
  5659. inst->configure (c, 120.0);
  5660. inst->noteOn (69, 1.0f); // 440 Hz
  5661. std::vector<float> l (48000), r (48000);
  5662. inst->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
  5663. return l;
  5664. };
  5665. // energy (Goertzel) at frequency f over frames [from, to)
  5666. auto energy = [] (const std::vector<float>& x, double f, int from, int to) {
  5667. const auto w = 2.0 * 3.141592653589793 * f / 48000.0, k = 2.0 * std::cos (w);
  5668. double s1 = 0.0, s2 = 0.0;
  5669. for (int i = from; i < to; ++i)
  5670. {
  5671. const auto s0 = x[(std::size_t) i] + k * s1 - s2;
  5672. s2 = s1;
  5673. s1 = s0;
  5674. }
  5675. return s1 * s1 + s2 * s2 - k * s1 * s2;
  5676. };
  5677. lynx::SynthSettings plain;
  5678. plain.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  5679. plain.osc[2] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  5680. plain.cutoff = 20000.0f;
  5681. auto fm = plain;
  5682. fm.fm = 1.0f;
  5683. const auto a = render (plain), b = render (fm);
  5684. // no FM: osc 3 adds (same pitch) and there's no 2nd harmonic; with FM the sidebands appear
  5685. CHECK (energy (a, 880.0, 4800, 24000) < 1e-3 * energy (a, 440.0, 4800, 24000));
  5686. CHECK (energy (b, 880.0, 4800, 24000) > 0.1 * energy (b, 440.0, 4800, 24000));
  5687. CHECK (energy (b, 1320.0, 4800, 24000) > 0.01 * energy (b, 440.0, 4800, 24000));
  5688. for (auto v : b)
  5689. CHECK (std::isfinite (v) && std::abs (v) < 1.0f);
  5690. // index decaying to 0: the start is bright, the end is nearly a sine again
  5691. auto decaying = fm;
  5692. decaying.fmDecay = 0.1f;
  5693. decaying.fmSustain = 0.0f;
  5694. const auto c = render (decaying);
  5695. const auto early = energy (c, 880.0, 0, 2400) / energy (c, 440.0, 0, 2400);
  5696. const auto late = energy (c, 880.0, 36000, 48000) / energy (c, 440.0, 36000, 48000);
  5697. CHECK (early > 0.05 && late < 1e-3);
  5698. }
  5699. TEST (synth_three_oscillators_adsr_filter)
  5700. {
  5701. auto render = [] (const lynx::SynthSettings& s, int frames, int releaseAt) {
  5702. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  5703. inst->prepare (48000.0);
  5704. lynx::ChannelDef c;
  5705. c.synth = s;
  5706. inst->configure (c, 120.0);
  5707. inst->noteOn (69, 1.0f); // A4 = 440 Hz
  5708. std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
  5709. inst->render (l.data(), r.data(), releaseAt, 1.0f, 1.0f);
  5710. inst->noteOff (69);
  5711. inst->render (l.data() + releaseAt, r.data() + releaseAt, frames - releaseAt, 1.0f, 1.0f);
  5712. return l;
  5713. };
  5714. auto peakIn = [] (const std::vector<float>& v, int from, int to) {
  5715. float p = 0.0f;
  5716. for (int i = from; i < to; ++i)
  5717. p = std::max (p, std::abs (v[(std::size_t) i]));
  5718. return p;
  5719. };
  5720. // default: sounds and fades after the release
  5721. const lynx::SynthSettings standard;
  5722. const auto a = render (standard, 48000, 24000);
  5723. CHECK (peakIn (a, 1000, 20000) > 0.05f && peakIn (a, 40000, 48000) < 1e-3f);
  5724. // pure sine with an open filter: zero-crossing count gives ~440 Hz
  5725. lynx::SynthSettings sine;
  5726. sine.osc[0].wave = lynx::Waveform::sine;
  5727. sine.cutoff = 20000.0f;
  5728. const auto b = render (sine, 48000, 47000);
  5729. int crossings = 0;
  5730. for (int i = 4800; i < 4800 + 48000 / 2; ++i)
  5731. crossings += (b[(std::size_t) i - 1] < 0.0f) != (b[(std::size_t) i] < 0.0f);
  5732. CHECK (std::abs (crossings - 440) < 6); // half a second = 220 cycles = 440 crossings
  5733. float sinePeak = 0.0f;
  5734. for (int i = 4800; i < 9600; ++i)
  5735. sinePeak = std::max (sinePeak, std::abs (b[(std::size_t) i]));
  5736. CHECK (sinePeak > 0.2f && sinePeak < 0.35f); // 0.3 level, approximated sine without clipping
  5737. // second oscillator an octave up adds energy; sustain 0 makes the note decay on its own
  5738. lynx::SynthSettings two = sine;
  5739. two.osc[1] = { lynx::Waveform::sine, 1, 0.0f, 1.0f };
  5740. two.sustain = 0.0f;
  5741. two.decay = 0.1f;
  5742. const auto c = render (two, 48000, 47000);
  5743. CHECK (peakIn (c, 100, 2000) > peakIn (b, 100, 2000) && peakIn (c, 30000, 40000) < 1e-3f);
  5744. }
  5745. TEST (set_target_value_round_trips)
  5746. {
  5747. auto s = lynx::Song::makeDefault();
  5748. s.mixer[2].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::delay));
  5749. const lynx::AutomationTarget targets[] = {
  5750. { lynx::AutomationTargetKind::channelVolume, 1 }, { lynx::AutomationTargetKind::channelPan, 2 },
  5751. { lynx::AutomationTargetKind::mixerVolume, 3 }, { lynx::AutomationTargetKind::mixerPan, 0 },
  5752. { lynx::AutomationTargetKind::effectParam, 2, 0, 1 }, { lynx::AutomationTargetKind::tempo, 0 },
  5753. };
  5754. for (const auto& t : targets)
  5755. {
  5756. CHECK (s.setTargetValue (t, 0.25f));
  5757. CHECK (std::abs (s.targetValue (t) - 0.25f) < 1e-5f);
  5758. }
  5759. CHECK (! s.setTargetValue ({ lynx::AutomationTargetKind::channelParam, 0, 0, 3 }, 0.5f)); // lives in the plugin
  5760. }
  5761. TEST (cut_groups_stretch_and_envelope_import)
  5762. {
  5763. // open hat (cut by 1) is cut when the closed one (group 1) plays
  5764. lynx::Song s;
  5765. s.mixer = lynx::Song::defaultMixer (2);
  5766. for (int i = 0; i < 2; ++i)
  5767. {
  5768. lynx::ChannelDef c;
  5769. c.instrument = lynx::InstrumentKind::sampler;
  5770. c.samplePath = "dc";
  5771. c.volume = 1.0f;
  5772. c.mixerTrack = i + 1;
  5773. s.channels.push_back (c);
  5774. }
  5775. s.channels[0].cutBy = 1; // open
  5776. s.channels[1].cutGroup = 1; // closed
  5777. auto& p = s.patterns.emplace_back();
  5778. p.notes = { { 0, 100, 0, 60 }, { 960, 100, 1, 60 } };
  5779. auto provider = [] (const std::string&) {
  5780. auto d = std::make_shared<lynx::SampleData>();
  5781. d->left.assign (96000, 0.5f);
  5782. return std::shared_ptr<const lynx::SampleData> (d);
  5783. };
  5784. auto state = lynx::RackState::build (s, nullptr, 48000.0, provider);
  5785. CHECK (state->hasCutGroups && state->cutTargets[1].size() == 1 && state->cutTargets[1][0] == 0);
  5786. lynx::RackRenderer r;
  5787. r.prepare (48000.0, 512);
  5788. std::vector<float> l (512), rr (512);
  5789. const auto tps = lynx::ticksPerSample (s.bpm, 48000.0);
  5790. float afterCut = 0.0f;
  5791. for (double tick = 0.0; tick < 960.0 * 3; tick += 512 * tps)
  5792. {
  5793. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  5794. if (tick > 960.0 * 2) // the closed one continues; the open one has stopped
  5795. afterCut = std::max (afterCut, std::abs (l[256]));
  5796. }
  5797. CHECK (std::abs (afterCut - 0.5f * 0.78f) < 0.01f); // only the closed one (default velocity 0.78)
  5798. // resampling stretch: 1 s of sample stretched to 4 beats at 120 BPM = 2 s
  5799. auto ramp = std::make_shared<lynx::SampleData>();
  5800. for (int i = 0; i < 48000; ++i)
  5801. ramp->left.push_back ((float) i / 48000.0f);
  5802. ramp->sampleRate = 48000.0;
  5803. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  5804. inst->prepare (48000.0);
  5805. lynx::ChannelDef c;
  5806. c.sampler.stretchBeats = 4.0f;
  5807. inst->configure (c, 120.0);
  5808. inst->noteOn (60, 1.0f);
  5809. std::vector<float> a (48000), b (48000);
  5810. inst->render (a.data(), b.data(), 48000, 1.0f, 1.0f);
  5811. CHECK (std::abs (a[47999] - 0.5f) < 0.01f); // after 1 s it has read half
  5812. // FL's Resample: the key still transposes, like tape (round 162, issue #9): an octave up reads twice as fast
  5813. inst->allNotesOff();
  5814. std::fill (a.begin(), a.end(), 0.0f);
  5815. inst->render (a.data(), b.data(), 4800, 1.0f, 1.0f);
  5816. std::fill (a.begin(), a.end(), 0.0f);
  5817. inst->noteOn (72, 1.0f);
  5818. inst->render (a.data(), b.data(), 24000, 1.0f, 1.0f);
  5819. CHECK (std::abs (a[23999] - 0.5f) < 0.01f);
  5820. // FL envelope time: 32950 = 2 s; < 101 = 0
  5821. CHECK (std::abs (flp::Channel::envelopeTime (32950) - 2.0) < 0.01 && flp::Channel::envelopeTime (50) == 0.0);
  5822. }
  5823. TEST (time_stretch_keeps_pitch)
  5824. {
  5825. lynx::SampleData sine;
  5826. sine.sampleRate = 48000.0;
  5827. for (int i = 0; i < 48000; ++i)
  5828. sine.left.push_back (0.5f * std::sin (2.0f * 3.14159265f * 440.0f * (float) i / 48000.0f));
  5829. auto crossingsPerSecond = [] (const lynx::SampleData& d) {
  5830. int c = 0;
  5831. const auto n = (int) d.frames();
  5832. for (int i = 4800; i < n - 4800; ++i)
  5833. c += (d.left[(std::size_t) i - 1] < 0.0f) != (d.left[(std::size_t) i] < 0.0f);
  5834. return (double) c / ((double) (n - 9600) / d.sampleRate);
  5835. };
  5836. for (double ratio : { 2.0, 0.5, 1.37 })
  5837. {
  5838. const auto out = lynx::timeStretch (sine, ratio);
  5839. CHECK (std::abs ((double) out->frames() - 48000.0 * ratio) < 2.0);
  5840. CHECK (std::abs (crossingsPerSecond (*out) - 880.0) < 15.0); // 440 Hz stays 440 Hz
  5841. float peak = 0.0f;
  5842. for (std::size_t i = 4800; i + 4800 < out->frames(); ++i)
  5843. peak = std::max (peak, std::abs (out->left[i]));
  5844. CHECK (peak > 0.4f && peak < 0.6f); // no gaps or clipping in the overlap
  5845. }
  5846. }
  5847. TEST (sampler_region_start_and_end)
  5848. {
  5849. auto ramp = std::make_shared<lynx::SampleData>();
  5850. for (int i = 0; i < 10000; ++i)
  5851. ramp->left.push_back ((float) i / 10000.0f);
  5852. ramp->sampleRate = 48000.0;
  5853. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  5854. inst->prepare (48000.0);
  5855. lynx::ChannelDef c;
  5856. c.sampler.regionStart = 0.25f;
  5857. c.sampler.regionEnd = 0.5f;
  5858. inst->configure (c, 120.0);
  5859. inst->noteOn (60, 1.0f);
  5860. std::vector<float> l (6000), r (6000);
  5861. inst->render (l.data(), r.data(), 6000, 1.0f, 1.0f);
  5862. CHECK (std::abs (l[0] - 0.25f) < 0.001f); // starts at the beginning of the slice
  5863. CHECK (l[2400] > 0.48f && l[2600] == 0.0f); // and stops at its end
  5864. }
  5865. TEST (arpeggiator_plays_held_chord_in_steps)
  5866. {
  5867. auto song = lynx::Song::makeDefault();
  5868. song.bpm = 120.0;
  5869. song.patterns[0].notes.clear();
  5870. for (int key : { 60, 64, 67 })
  5871. song.patterns[0].notes.push_back ({ 0, lynx::ppq * 4, 4, key, 0.8f }); // chord held for 1 bar
  5872. song.channels[4].arp.direction = 1; // up
  5873. song.channels[4].arp.stepBeats = 0.25f;
  5874. auto state = lynx::RackState::build (song, nullptr, 48000.0);
  5875. CHECK (state->arpeggiators[4] != nullptr && state->arpeggiators[0] == nullptr);
  5876. // the arpeggiator generates the notes; we count what it hands to the instrument
  5877. auto& arp = *state->arpeggiators[4];
  5878. const auto tps = lynx::ticksPerSample (song.bpm, 48000.0);
  5879. std::vector<int> keys;
  5880. lynx::EventBuffer ev;
  5881. for (double tick = 0.0; tick < lynx::ppq * 2.0; tick += 512 * tps)
  5882. {
  5883. ev.clear();
  5884. lynx::schedulePatternLoop (song.patterns[0], song.patterns[0].effectiveLength (4), tick, 512, tps, ev);
  5885. int in = 0, out = 0;
  5886. for (auto& e : ev)
  5887. if (e.channel == 4)
  5888. arp.input[(std::size_t) in++] = e;
  5889. arp.process (4, arp.input.data(), in, tick, tps, 512, song.channels[4].arp, arp.output.data(), out);
  5890. for (int i = 0; i < out; ++i)
  5891. if (arp.output[(std::size_t) i].type == lynx::NoteEvent::Type::on)
  5892. keys.push_back (arp.output[(std::size_t) i].key);
  5893. }
  5894. // 2 beats = 8 sixteenth steps: 60 64 67 60 64 67 60 64
  5895. CHECK (keys.size() >= 8); // the last block goes slightly past 2 beats
  5896. const int expected[] = { 60, 64, 67, 60, 64, 67, 60, 64 };
  5897. for (std::size_t i = 0; i < keys.size() && i < 8; ++i)
  5898. CHECK (keys[i] == expected[i]);
  5899. // actually rendering also makes sound
  5900. lynx::RackRenderer r;
  5901. r.prepare (48000.0, 512);
  5902. std::vector<float> l (512), rr (512);
  5903. float peak = 0.0f;
  5904. for (double tick = 0.0; tick < lynx::ppq; tick += 512 * tps)
  5905. {
  5906. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  5907. for (auto v : l)
  5908. peak = std::max (peak, std::abs (v));
  5909. }
  5910. CHECK (peak > 0.01f);
  5911. }
  5912. TEST (audio_clip_fades_and_gain)
  5913. {
  5914. lynx::Song s;
  5915. s.bpm = 120.0;
  5916. s.mode = lynx::PlayMode::song;
  5917. s.mixer = lynx::Song::defaultMixer (1);
  5918. lynx::ChannelDef c;
  5919. c.instrument = lynx::InstrumentKind::audioClip;
  5920. c.samplePath = "dc";
  5921. c.volume = 1.0f;
  5922. c.mixerTrack = 1;
  5923. s.channels.push_back (c);
  5924. s.patterns.emplace_back();
  5925. lynx::Clip clip;
  5926. clip.kind = lynx::ClipKind::audio;
  5927. clip.channel = 0;
  5928. clip.length = lynx::ppq * 4; // 2 s at 120 BPM
  5929. clip.fadeIn = lynx::ppq; // 0.5 s
  5930. clip.fadeOut = lynx::ppq;
  5931. clip.gain = 0.5f;
  5932. s.playlist.push_back (clip);
  5933. auto provider = [] (const std::string&) {
  5934. auto d = std::make_shared<lynx::SampleData>();
  5935. d->left.assign (48000 * 4, 1.0f);
  5936. return std::shared_ptr<const lynx::SampleData> (d);
  5937. };
  5938. auto state = lynx::RackState::build (s, nullptr, 48000.0, provider);
  5939. lynx::RackRenderer r;
  5940. r.prepare (48000.0, 512);
  5941. std::vector<float> all;
  5942. std::vector<float> l (512), rr (512);
  5943. const auto tps = lynx::ticksPerSample (s.bpm, 48000.0);
  5944. for (double tick = 0.0; tick < lynx::ppq * 4.0; tick += 512 * tps)
  5945. {
  5946. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  5947. all.insert (all.end(), l.begin(), l.end());
  5948. }
  5949. // FL plays a clip at unity at the default channel volume (78%): at 100% it is that much louder (REA_rodada89 §4.2)
  5950. const auto unity = 1.0f / lynx::channelGain (10000.0f / 12800.0f);
  5951. CHECK (all[100] < 0.01f); // fade-in start
  5952. CHECK (std::abs (all[12000] - 0.25f * unity) < 0.02f); // fade-in midpoint: half of 0.5
  5953. CHECK (std::abs (all[48000] - 0.5f * unity) < 0.01f); // body: the clip gain
  5954. CHECK (std::abs (all[96000 - 12000] - 0.25f * unity) < 0.02f); // fade-out midpoint
  5955. }
  5956. TEST (audio_clip_unity_at_fl_default_volume)
  5957. {
  5958. // a Playlist audio clip at FL's default channel volume (10000/12800) and centre pan plays its sample unchanged
  5959. auto dc = std::make_shared<lynx::SampleData>();
  5960. dc->left.assign (48000, 0.5f);
  5961. auto clip = lynx::makeInstrument (lynx::InstrumentKind::audioClip, dc);
  5962. clip->prepare (48000.0);
  5963. lynx::ChannelDef c;
  5964. c.instrument = lynx::InstrumentKind::audioClip;
  5965. c.volume = 10000.0f / 12800.0f;
  5966. clip->configure (c, 120.0);
  5967. clip->noteOn (60, 1.0f);
  5968. std::vector<float> l (4800, 0.0f), r (4800, 0.0f);
  5969. const auto g = lynx::channelGain (c.volume);
  5970. clip->render (l.data(), r.data(), 4800, g, g);
  5971. CHECK (std::abs (l[2400] - 0.5f) < 1.0e-3f && std::abs (r[2400] - 0.5f) < 1.0e-3f);
  5972. // a sampler channel (pattern notes) keeps the channel volume curve as it was
  5973. auto sampler = lynx::makeInstrument (lynx::InstrumentKind::sampler, dc);
  5974. sampler->prepare (48000.0);
  5975. c.instrument = lynx::InstrumentKind::sampler;
  5976. sampler->configure (c, 120.0);
  5977. sampler->noteOn (60, 1.0f);
  5978. std::fill (l.begin(), l.end(), 0.0f);
  5979. std::fill (r.begin(), r.end(), 0.0f);
  5980. sampler->render (l.data(), r.data(), 4800, g, g);
  5981. CHECK (std::abs (l[2400] - 0.5f * g) < 0.02f);
  5982. }
  5983. TEST (slicer_detects_hits_and_plays_slices_by_key)
  5984. {
  5985. // 4 hits (decaying noise) every 0.25 s
  5986. lynx::SampleData loop;
  5987. loop.sampleRate = 48000.0;
  5988. loop.left.assign (48000, 0.0f);
  5989. lynx::Noise noise;
  5990. for (int hit = 0; hit < 4; ++hit)
  5991. for (int i = 0; i < 6000; ++i)
  5992. loop.left[(std::size_t) (hit * 12000 + i)] = noise.next() * 0.8f * std::exp (-(float) i / 1500.0f);
  5993. const auto slices = lynx::detectSlices (loop, 0.5f);
  5994. CHECK (slices.size() == 4);
  5995. for (std::size_t i = 0; i < slices.size(); ++i)
  5996. CHECK (std::abs (slices[i] - 0.25f * (float) i) < 0.01f);
  5997. // key 61 plays the second slice, which starts at 0.25 and ends before 0.5
  5998. auto ramp = std::make_shared<lynx::SampleData>();
  5999. for (int i = 0; i < 48000; ++i)
  6000. ramp->left.push_back ((float) i / 48000.0f);
  6001. ramp->sampleRate = 48000.0;
  6002. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  6003. inst->prepare (48000.0);
  6004. lynx::ChannelDef c;
  6005. c.sampler.slices = slices;
  6006. inst->configure (c, 120.0);
  6007. inst->noteOn (61, 1.0f);
  6008. std::vector<float> l (20000), r (20000);
  6009. inst->render (l.data(), r.data(), 20000, 1.0f, 1.0f);
  6010. CHECK (std::abs (l[0] - 0.25f) < 0.01f && l[11000] > 0.45f && l[13000] == 0.0f);
  6011. }
  6012. TEST (project_templates_render_sound)
  6013. {
  6014. const auto names = lynx::templateNames();
  6015. CHECK (names.size() == 4);
  6016. for (int t = 1; t < (int) names.size(); ++t)
  6017. {
  6018. auto song = lynx::makeTemplate (t);
  6019. auto state = lynx::RackState::build (song, nullptr, 48000.0);
  6020. lynx::RackRenderer r;
  6021. r.prepare (48000.0, 512);
  6022. std::vector<float> l (512), rr (512);
  6023. const auto tps = lynx::ticksPerSample (song.bpm, 48000.0);
  6024. float peak = 0.0f;
  6025. for (double tick = 0.0; tick < lynx::ppq * 4.0; tick += 512 * tps)
  6026. {
  6027. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  6028. for (auto v : l)
  6029. peak = std::max (peak, std::abs (v));
  6030. }
  6031. CHECK (peak > 0.05f && peak < 4.0f);
  6032. }
  6033. CHECK (lynx::makeTemplate (0).patterns[0].notes.empty());
  6034. }
  6035. // --- interface translations (resources/i18n) -------------------------------------------------------
  6036. namespace i18ntest
  6037. {
  6038. std::string readFile (const std::filesystem::path& path)
  6039. {
  6040. std::ifstream in (path, std::ios::binary);
  6041. return { std::istreambuf_iterator<char> (in), std::istreambuf_iterator<char>() };
  6042. }
  6043. std::string unescape (const std::string& s)
  6044. {
  6045. std::string out;
  6046. for (std::size_t i = 0; i < s.size(); ++i)
  6047. {
  6048. if (s[i] == '\\' && i + 1 < s.size())
  6049. {
  6050. const auto n = s[++i];
  6051. out += n == 'n' ? '\n' : n == 't' ? '\t' : n;
  6052. }
  6053. else
  6054. out += s[i];
  6055. }
  6056. return out;
  6057. }
  6058. // reads a quoted string starting at s[pos] == '"'; returns the raw (escaped) contents and moves pos past the closing quote
  6059. bool quoted (const std::string& s, std::size_t& pos, std::string& raw)
  6060. {
  6061. if (pos >= s.size() || s[pos] != '"')
  6062. return false;
  6063. raw.clear();
  6064. for (++pos; pos < s.size() && s[pos] != '"' && s[pos] != '\n'; ++pos)
  6065. {
  6066. if (s[pos] == '\\' && pos + 1 < s.size())
  6067. raw += s[pos++];
  6068. raw += s[pos];
  6069. }
  6070. if (pos >= s.size() || s[pos] != '"')
  6071. return false;
  6072. ++pos;
  6073. return true;
  6074. }
  6075. // JUCE LocalisedStrings format: "english" = "translation"; duplicates are reported in `duplicates`
  6076. std::map<std::string, std::string> parseTable (const std::string& text, std::vector<std::string>& duplicates)
  6077. {
  6078. std::map<std::string, std::string> table;
  6079. std::istringstream lines (text);
  6080. std::string line;
  6081. while (std::getline (lines, line))
  6082. {
  6083. std::size_t pos = line.find_first_not_of (" \t");
  6084. std::string key, value;
  6085. if (pos == std::string::npos || ! quoted (line, pos, key))
  6086. continue;
  6087. pos = line.find ('"', pos);
  6088. if (pos == std::string::npos || ! quoted (line, pos, value))
  6089. value.clear();
  6090. const auto k = unescape (key);
  6091. if (table.count (k) != 0)
  6092. duplicates.push_back (k);
  6093. table[k] = unescape (value);
  6094. }
  6095. return table;
  6096. }
  6097. // string literals passed to tr() / trc() / TR_NOOP() in the sources (adjacent literals are concatenated)
  6098. std::map<std::string, std::string> sourceKeys (const std::filesystem::path& srcDir)
  6099. {
  6100. std::map<std::string, std::string> keys;
  6101. for (const auto& entry : std::filesystem::recursive_directory_iterator (srcDir))
  6102. {
  6103. const auto ext = entry.path().extension().string();
  6104. if (! entry.is_regular_file() || (ext != ".cpp" && ext != ".h" && ext != ".inc"))
  6105. continue;
  6106. // drop // comment lines (they may quote tr ("...") as an example)
  6107. std::string text, line;
  6108. std::istringstream in (readFile (entry.path()));
  6109. while (std::getline (in, line))
  6110. {
  6111. const auto first = line.find_first_not_of (" \t");
  6112. text += (first != std::string::npos && line.compare (first, 2, "//") == 0) ? std::string() : line;
  6113. text += '\n';
  6114. }
  6115. auto isIdent = [] (char c) { return std::isalnum ((unsigned char) c) || c == '_'; };
  6116. for (const std::string name : { "tr", "trc", "TR_NOOP" })
  6117. for (auto at = text.find (name); at != std::string::npos; at = text.find (name, at + 1))
  6118. {
  6119. if ((at > 0 && isIdent (text[at - 1])) || isIdent (text[at + name.size()]))
  6120. continue;
  6121. auto pos = text.find_first_not_of (" \t\n", at + name.size());
  6122. if (pos == std::string::npos || text[pos] != '(')
  6123. continue;
  6124. pos = text.find_first_not_of (" \t\n", pos + 1);
  6125. std::string key, raw;
  6126. bool any = false;
  6127. while (pos != std::string::npos && quoted (text, pos, raw))
  6128. {
  6129. key += raw;
  6130. any = true;
  6131. pos = text.find_first_not_of (" \t\n", pos);
  6132. }
  6133. if (any)
  6134. keys.emplace (unescape (key), entry.path().filename().string());
  6135. }
  6136. }
  6137. return keys;
  6138. }
  6139. std::set<std::string> placeholders (const std::string& s)
  6140. {
  6141. std::set<std::string> found;
  6142. for (auto open = s.find ('{'); open != std::string::npos; open = s.find ('{', open + 1))
  6143. if (const auto close = s.find ('}', open); close != std::string::npos && close - open <= 3)
  6144. found.insert (s.substr (open, close - open + 1));
  6145. return found;
  6146. }
  6147. } // namespace i18ntest
  6148. TEST (translations_cover_every_ui_string)
  6149. {
  6150. using namespace i18ntest;
  6151. const auto text = readFile ("resources/i18n/pt-BR.txt");
  6152. CHECK (! text.empty());
  6153. std::vector<std::string> duplicates;
  6154. const auto table = parseTable (text, duplicates);
  6155. CHECK (table.size() > 300);
  6156. CHECK (duplicates.empty());
  6157. for (const auto& d : duplicates)
  6158. std::printf (" duplicate pt-BR entry: \"%s\"\n", d.c_str());
  6159. const auto keys = sourceKeys ("src");
  6160. CHECK (keys.size() > 300);
  6161. int missing = 0;
  6162. for (const auto& [key, file] : keys)
  6163. if (auto it = table.find (key); it == table.end() || it->second.empty())
  6164. {
  6165. ++missing;
  6166. std::printf (" no pt-BR translation for \"%s\" (%s)\n", key.c_str(), file.c_str());
  6167. }
  6168. CHECK (missing == 0);
  6169. // no empty values, and every {n} placeholder survives the translation
  6170. int empty = 0, badPlaceholders = 0;
  6171. for (const auto& [english, portuguese] : table)
  6172. {
  6173. empty += portuguese.empty() ? 1 : 0;
  6174. if (placeholders (english) != placeholders (portuguese))
  6175. {
  6176. ++badPlaceholders;
  6177. std::printf (" placeholders differ: \"%s\"\n", english.c_str());
  6178. }
  6179. }
  6180. CHECK (empty == 0);
  6181. CHECK (badPlaceholders == 0);
  6182. }
  6183. TEST (translations_embedded_table_is_up_to_date)
  6184. {
  6185. // src/ui/i18n/<id>.h + I18nData.h must be regenerated (tools/make_i18n.py) whenever a resources/i18n/*.txt changes
  6186. int files = 0;
  6187. for (const auto& entry : std::filesystem::directory_iterator ("resources/i18n"))
  6188. if (entry.path().extension() == ".txt")
  6189. {
  6190. ++files;
  6191. const auto id = entry.path().stem().string();
  6192. const auto text = i18ntest::readFile (entry.path());
  6193. const lynx::i18n::data::Table* found = nullptr;
  6194. for (const auto& t : lynx::i18n::data::tables)
  6195. if (id == t.id)
  6196. found = &t;
  6197. CHECK (found != nullptr);
  6198. if (found == nullptr)
  6199. {
  6200. std::printf (" %s.txt is not embedded: run python3 tools/make_i18n.py\n", id.c_str());
  6201. continue;
  6202. }
  6203. const auto unpacked = lynx::inflateZlib (found->zlib, found->zlibSize, false, found->textSize + 16);
  6204. const bool same = unpacked && unpacked->size() == text.size() && found->textSize == text.size()
  6205. && std::memcmp (unpacked->data(), text.data(), text.size()) == 0;
  6206. CHECK (same);
  6207. if (! same)
  6208. std::printf (" %s.txt changed since the last python3 tools/make_i18n.py\n", id.c_str());
  6209. }
  6210. CHECK (files == (int) std::size (lynx::i18n::data::tables));
  6211. }
  6212. TEST (translations_no_value_is_a_python_dict)
  6213. {
  6214. // a value written as a Python dict ("{'de': '...', 'es': ...}", 177a's menu slice) reaches the users verbatim:
  6215. // each table holds its own language's text only
  6216. using namespace i18ntest;
  6217. int bad = 0;
  6218. for (const auto& entry : std::filesystem::directory_iterator ("resources/i18n"))
  6219. if (entry.path().extension() == ".txt")
  6220. {
  6221. std::vector<std::string> unused;
  6222. for (const auto& [key, value] : parseTable (readFile (entry.path()), unused))
  6223. if (value.rfind ("{'", 0) == 0 || value.find ("': '") != std::string::npos)
  6224. {
  6225. ++bad;
  6226. std::printf (" %s: \"%s\" holds a dict\n", entry.path().filename().string().c_str(), key.c_str());
  6227. }
  6228. }
  6229. CHECK (bad == 0);
  6230. }
  6231. TEST (translations_every_language_covers_the_reference_table)
  6232. {
  6233. // every key of pt-BR.txt (the reference table) exists, translated, in every other language; keys just added to
  6234. // pt-BR.txt may wait in resources/i18n/pending.keys ("English" lines) until the other tables catch up
  6235. using namespace i18ntest;
  6236. std::vector<std::string> unused;
  6237. const auto reference = parseTable (readFile ("resources/i18n/pt-BR.txt"), unused);
  6238. std::set<std::string> pending;
  6239. {
  6240. std::istringstream in (readFile ("resources/i18n/pending.keys"));
  6241. std::string line, raw;
  6242. while (std::getline (in, line))
  6243. if (std::size_t pos = line.find_first_not_of (" \t"); pos != std::string::npos && quoted (line, pos, raw))
  6244. pending.insert (unescape (raw));
  6245. }
  6246. auto count = [] (const std::string& s, const std::string& what) {
  6247. int n = 0;
  6248. for (auto at = s.find (what); at != std::string::npos; at = s.find (what, at + what.size()))
  6249. ++n;
  6250. return n;
  6251. };
  6252. int languages = 0;
  6253. for (const auto& entry : std::filesystem::directory_iterator ("resources/i18n"))
  6254. {
  6255. const auto id = entry.path().stem().string();
  6256. if (entry.path().extension() != ".txt" || id == "pt-BR")
  6257. continue;
  6258. ++languages;
  6259. const auto text = readFile (entry.path());
  6260. CHECK (text.rfind ("language: ", 0) == 0); // JUCE LocalisedStrings header
  6261. std::vector<std::string> duplicates;
  6262. const auto table = parseTable (text, duplicates);
  6263. CHECK (duplicates.empty());
  6264. for (const auto& d : duplicates)
  6265. std::printf (" duplicate %s entry: \"%s\"\n", id.c_str(), d.c_str());
  6266. int missing = 0, bad = 0;
  6267. for (const auto& [english, unusedValue] : reference)
  6268. {
  6269. const auto it = table.find (english);
  6270. if ((it == table.end() || it->second.empty()) && pending.count (english) == 0)
  6271. {
  6272. if (++missing <= 10)
  6273. std::printf (" no %s translation for \"%s\" (python3 tools/i18n_tool.py status)\n", id.c_str(), english.c_str());
  6274. }
  6275. }
  6276. for (const auto& [english, translated] : table)
  6277. {
  6278. std::string why;
  6279. if (translated.empty())
  6280. why = "empty";
  6281. else if (placeholders (english) != placeholders (translated))
  6282. why = "placeholders differ";
  6283. else if (count (english, "\n") != count (translated, "\n"))
  6284. why = "line breaks differ";
  6285. else if (count (english, "%") != count (translated, "%"))
  6286. why = "% signs differ";
  6287. if (! why.empty() && ++bad <= 10)
  6288. std::printf (" %s: %s: \"%s\"\n", id.c_str(), why.c_str(), english.c_str());
  6289. }
  6290. CHECK (missing == 0);
  6291. CHECK (bad == 0);
  6292. if (missing > 10 || bad > 10)
  6293. std::printf (" %s: %d missing, %d bad in all\n", id.c_str(), missing, bad);
  6294. }
  6295. CHECK (languages >= 0);
  6296. }
  6297. TEST (translations_os_locale_picks_the_interface_language)
  6298. {
  6299. const std::vector<std::string> ids { "en", "pt-BR", "de", "es", "ja", "zh-Hans", "zh-Hant", "id", "ko" };
  6300. using lynx::i18n::languageForLocale;
  6301. CHECK (languageForLocale ("de-AT", ids) == "de");
  6302. CHECK (languageForLocale ("de_DE.UTF-8", ids) == "de");
  6303. CHECK (languageForLocale ("pt-BR", ids) == "pt-BR");
  6304. CHECK (languageForLocale ("pt_PT", ids) == "pt-BR");
  6305. CHECK (languageForLocale ("zh-Hans-CN", ids) == "zh-Hans");
  6306. CHECK (languageForLocale ("zh-CN", ids) == "zh-Hans");
  6307. CHECK (languageForLocale ("zh_TW.UTF-8", ids) == "zh-Hant");
  6308. CHECK (languageForLocale ("zh-Hant-HK", ids) == "zh-Hant");
  6309. CHECK (languageForLocale ("zh-HK", ids) == "zh-Hant");
  6310. CHECK (languageForLocale ("ja_JP.UTF-8@euro", ids) == "ja");
  6311. CHECK (languageForLocale ("in_ID", ids) == "id");
  6312. CHECK (languageForLocale ("es-419", ids) == "es");
  6313. CHECK (languageForLocale ("en-US", ids) == "en");
  6314. CHECK (languageForLocale ("ko:en", ids) == "ko");
  6315. CHECK (languageForLocale ("fr-FR", ids).empty()); // not in the list
  6316. CHECK (languageForLocale ("C", ids).empty());
  6317. CHECK (languageForLocale ("", ids).empty());
  6318. }
  6319. // --- workflow shell: pattern/channel operations, key map, workspace layout, control math -------------
  6320. TEST (workflow_pattern_navigation_and_insert)
  6321. {
  6322. auto song = lynx::Song::makeDefault();
  6323. song.patterns.resize (3);
  6324. song.patterns[1].name = "B";
  6325. song.patterns[2].name = "C";
  6326. song.patterns[2].notes.push_back ({});
  6327. song.currentPattern = 0;
  6328. CHECK (lynx::stepPattern (song, 1) == 1 && lynx::stepPattern (song, -1) == 0 && lynx::stepPattern (song, 10) == 2);
  6329. // pattern 0 has the default notes, 1 is empty, 2 has a note
  6330. CHECK (! lynx::isPatternEmpty (song.patterns[0]) && lynx::isPatternEmpty (song.patterns[1]));
  6331. CHECK (lynx::findNextEmptyPattern (song, 0) == 1 && lynx::findNextEmptyPattern (song, 1) == -1);
  6332. CHECK (lynx::gotoNextEmptyPattern (song) == 1 && song.currentPattern == 1);
  6333. song.patterns[1].notes.push_back ({});
  6334. const auto before = song.patterns.size();
  6335. CHECK (lynx::gotoNextEmptyPattern (song) == (int) before && song.patterns.size() == before + 1);
  6336. CHECK (song.patterns.back().name == "Pattern 1" || song.patterns.back().name.rfind ("Pattern ", 0) == 0);
  6337. // insert keeps Playlist clips on their patterns
  6338. lynx::Clip clip;
  6339. clip.pattern = 2;
  6340. song.playlist = { clip };
  6341. song.arrangements.push_back ({ "Other", { clip }, 0, 0, {} });
  6342. CHECK (lynx::insertPattern (song, 1, "New") == 1 && song.currentPattern == 1);
  6343. CHECK (song.patterns[1].name == "New" && song.patterns[1].notes.empty() && song.patterns[3].name == "C");
  6344. CHECK (song.playlist[0].pattern == 3 && song.arrangements[1].clips[0].pattern == 3);
  6345. }
  6346. TEST (workflow_split_pattern_by_channel)
  6347. {
  6348. auto song = lynx::Song::makeDefault();
  6349. song.patterns.resize (1);
  6350. auto& p = song.patterns[0];
  6351. p.name = "Beat";
  6352. p.notes.clear();
  6353. for (int i = 0; i < 4; ++i)
  6354. p.notes.push_back ({ (std::int64_t) i * lynx::ticksPerStep, lynx::ticksPerStep, i % 2 == 0 ? 0 : 2, 60 });
  6355. song.channels[2].colour = 0xff112233;
  6356. lynx::Clip clip;
  6357. clip.pattern = 0;
  6358. song.playlist = { clip };
  6359. const auto created = lynx::splitPatternByChannel (song, 0);
  6360. CHECK (created == 2 && song.patterns.size() == 3);
  6361. CHECK (song.patterns[0].notes.size() == 4); // the original is unchanged
  6362. CHECK (song.patterns[1].name == "Beat - " + song.channels[0].name && song.patterns[1].notes.size() == 2);
  6363. CHECK (song.patterns[2].colour == 0xff112233u && song.patterns[2].notes.size() == 2 && song.patterns[2].notes[0].channel == 2);
  6364. CHECK (song.playlist[0].pattern == 0);
  6365. CHECK (lynx::splitPatternByChannel (song, 99) == 0);
  6366. }
  6367. TEST (workflow_channel_selection)
  6368. {
  6369. lynx::ChannelSelection s;
  6370. s.selectOnly (2);
  6371. s.toggle (5);
  6372. s.toggle (0);
  6373. CHECK ((s.rows == std::vector<int> { 0, 2, 5 }) && s.contains (5) && ! s.contains (3));
  6374. s.toggle (2);
  6375. CHECK ((s.rows == std::vector<int> { 0, 5 }));
  6376. s.selectOnly (3);
  6377. s.extendTo (6);
  6378. CHECK ((s.rows == std::vector<int> { 3, 4, 5, 6 }));
  6379. s.extendTo (1);
  6380. CHECK ((s.rows == std::vector<int> { 1, 2, 3 }));
  6381. s.selectOnly (4);
  6382. s.toggle (4); // the last one stays
  6383. CHECK ((s.rows == std::vector<int> { 4 }));
  6384. s.selectAll (3);
  6385. CHECK ((s.rows == std::vector<int> { 0, 1, 2 }));
  6386. s.rows = { 7, 9 };
  6387. s.clamp (5);
  6388. CHECK (s.rows.size() == 1 && s.rows[0] < 5);
  6389. }
  6390. TEST (workflow_move_clone_insert_channels)
  6391. {
  6392. auto song = lynx::Song::makeDefault();
  6393. const auto names = [&] {
  6394. std::vector<std::string> n;
  6395. for (const auto& c : song.channels)
  6396. n.push_back (c.name);
  6397. return n;
  6398. };
  6399. const auto original = names();
  6400. const auto count = (int) song.channels.size();
  6401. CHECK (count >= 4);
  6402. // references that must follow their channel
  6403. song.patterns[0].notes.push_back ({ 0, 240, 1, 64 });
  6404. lynx::Automation a;
  6405. a.target = { lynx::AutomationTargetKind::channelVolume, 1 };
  6406. song.automations.push_back (a);
  6407. song.midiLinks.push_back ({ 7, -1, { lynx::AutomationTargetKind::channelPan, 3 } });
  6408. lynx::Clip audio;
  6409. audio.kind = lynx::ClipKind::audio;
  6410. audio.channel = 3;
  6411. song.playlist.push_back (audio);
  6412. std::vector<int> selection { 1, 3 };
  6413. CHECK (lynx::moveChannels (song, selection, -1));
  6414. CHECK ((selection == std::vector<int> { 0, 2 }));
  6415. CHECK (song.channels[0].name == original[1] && song.channels[1].name == original[0] && song.channels[2].name == original[3]
  6416. && song.channels[3].name == original[2]);
  6417. CHECK (song.automations[0].target.index == 0 && song.midiLinks[0].target.index == 2 && song.playlist.back().channel == 2);
  6418. CHECK (std::any_of (song.patterns[0].notes.begin(), song.patterns[0].notes.end(),
  6419. [] (const lynx::Note& n) { return n.channel == 0 && n.key == 64; }));
  6420. CHECK (! lynx::moveChannels (song, selection, -1)); // the block is at the top
  6421. CHECK (lynx::moveChannels (song, selection, 1) && (selection == std::vector<int> { 1, 3 }) && names() == original);
  6422. // clone: settings, not notes, right below, with a new name
  6423. song.channels[1].volume = 0.33f;
  6424. const auto notesBefore = song.patterns[0].notes.size();
  6425. const auto clone = lynx::cloneChannel (song, 1);
  6426. CHECK (clone == 2 && (int) song.channels.size() == count + 1);
  6427. CHECK (song.channels[2].name == original[1] + " #2" && song.channels[2].volume == 0.33f);
  6428. CHECK (song.patterns[0].notes.size() == notesBefore);
  6429. CHECK (song.channels[3].name == original[2] && song.automations[0].target.index == 1);
  6430. CHECK (lynx::cloneChannel (song, 1) == 2 && song.channels[2].name == original[1] + " #3");
  6431. // remove several
  6432. CHECK (lynx::removeChannels (song, { 2, 3, 2 }) == 2 && (int) song.channels.size() == count);
  6433. CHECK (names() == original);
  6434. }
  6435. TEST (workflow_send_channels_to_free_inserts)
  6436. {
  6437. auto song = lynx::Song::makeDefault();
  6438. for (auto& c : song.channels)
  6439. c.mixerTrack = 0;
  6440. song.mixer[1].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::delay)); // not free: has effects
  6441. song.channels[0].mixerTrack = 2; // not free: used
  6442. song.setRoute (5, 3, true); // 3 is not free: receives a send
  6443. CHECK (! lynx::isInsertFree (song, 1) && ! lynx::isInsertFree (song, 2) && ! lynx::isInsertFree (song, 3) && lynx::isInsertFree (song, 4));
  6444. song.channels[1].colour = 0xff445566;
  6445. const auto tracks = lynx::sendChannelsToFreeInserts (song, { 1, 2 });
  6446. CHECK ((tracks == std::vector<int> { 4, 5 }) || (tracks == std::vector<int> { 4, 6 }));
  6447. CHECK (song.channels[1].mixerTrack == 4 && song.mixer[4].name == song.channels[1].name && song.mixer[4].colour == 0xff445566u);
  6448. CHECK (song.mixer[(std::size_t) tracks[1]].name == song.channels[2].name);
  6449. // with no free insert left, a new one is added at the end
  6450. auto full = lynx::Song::makeDefault();
  6451. full.mixer = lynx::Song::defaultMixer (1);
  6452. full.channels[0].mixerTrack = 1;
  6453. const auto added = lynx::sendChannelsToFreeInserts (full, { 1 });
  6454. CHECK (added.size() == 1 && added[0] == 2 && full.mixer.size() == 3 && full.channels[1].mixerTrack == 2);
  6455. }
  6456. TEST (workflow_add_channel_routed)
  6457. {
  6458. auto song = lynx::Song::makeDefault();
  6459. const auto count = (int) song.channels.size();
  6460. const auto inserts = song.mixer.size();
  6461. lynx::ChannelDef c;
  6462. c.mixerTrack = 7; // what the generator came with doesn't count
  6463. const auto last = (int) song.mixer.size() - 1;
  6464. CHECK (lynx::addChannelRouted (song, c, last) == count && song.channels.back().mixerTrack == last);
  6465. CHECK (lynx::addChannelRouted (song, c, 0) == count + 1 && song.channels.back().mixerTrack == 0); // Master
  6466. CHECK (song.mixer.size() == inserts); // the drop's insert is used as it is, nothing is claimed
  6467. // an insert that doesn't exist claims a free one, as a plain new channel does
  6468. auto expected = song;
  6469. const auto free = expected.claimFreeInsert();
  6470. CHECK (lynx::addChannelRouted (song, c, 9999) == count + 2 && song.channels.back().mixerTrack == free);
  6471. auto plain = expected;
  6472. CHECK (lynx::addChannelRouted (plain, c) == count + 2 && plain.channels.back().mixerTrack == expected.claimFreeInsert());
  6473. }
  6474. TEST (pr28_generator_dropped_on_an_insert_names_it_like_fl)
  6475. {
  6476. // FL 2026 (round 167u, shots fl3-fl10): 3x Osc dropped on Insert 3, Master and Insert 5's slot 2, then FLEX on
  6477. // Insert 3: channels "3x Osc", "3x Osc #2", "3x Osc #3", "FLEX"; Insert 3 / Master / Insert 5 take the first
  6478. // three names, Insert 3 keeps "3x Osc" when FLEX comes
  6479. auto song = lynx::Song::makeDefault();
  6480. song.channels.clear();
  6481. while (song.mixer.size() < 6)
  6482. song.mixer.emplace_back().name = "Insert " + std::to_string (song.mixer.size());
  6483. song.mixer[4].name = "Drums"; // a name the user gave
  6484. lynx::ChannelDef osc;
  6485. osc.name = "3x Osc";
  6486. lynx::ChannelDef flex;
  6487. flex.name = "FLEX";
  6488. CHECK (lynx::addChannelRouted (song, osc, 3) == 0 && song.channels[0].name == "3x Osc" && song.mixer[3].name == "3x Osc");
  6489. CHECK (lynx::addChannelRouted (song, osc, 0) == 1 && song.channels[1].name == "3x Osc #2" && song.channels[1].mixerTrack == 0
  6490. && song.mixer[0].name == "3x Osc #2");
  6491. CHECK (lynx::addChannelRouted (song, osc, 5) == 2 && song.channels[2].name == "3x Osc #3" && song.mixer[5].name == "3x Osc #3");
  6492. CHECK (lynx::addChannelRouted (song, flex, 3) == 3 && song.channels[3].name == "FLEX" && song.channels[3].mixerTrack == 3
  6493. && song.mixer[3].name == "3x Osc");
  6494. CHECK (lynx::addChannelRouted (song, flex, 4) == 4 && song.channels[4].name == "FLEX #2" && song.mixer[4].name == "Drums");
  6495. // a plain new channel (no insert given) keeps its name and doesn't rename anything, as before
  6496. const auto mixerBefore = song.mixer.size();
  6497. CHECK (lynx::addChannelRouted (song, osc) == 5 && song.channels[5].name == "3x Osc");
  6498. CHECK (song.mixer.size() >= mixerBefore && song.mixer[3].name == "3x Osc" && song.mixer[0].name == "3x Osc #2");
  6499. }
  6500. TEST (workflow_palette_random_colour)
  6501. {
  6502. const auto& palette = lynx::lynxPalette();
  6503. for (std::uint32_t seed = 0; seed < 50; ++seed)
  6504. {
  6505. const auto current = palette[seed % palette.size()];
  6506. const auto c = lynx::randomPaletteColour (current, seed);
  6507. CHECK (c != current && std::find (palette.begin(), palette.end(), c) != palette.end());
  6508. }
  6509. }
  6510. TEST (workflow_key_map)
  6511. {
  6512. using namespace lynx::keys;
  6513. KeyMap map;
  6514. const auto global = Context::global, rack = Context::channelRack;
  6515. CHECK (map.find ({ space, none }, global, true) == "transport.playStop");
  6516. CHECK (map.find ({ space, ctrl }, global, true) == "transport.pause");
  6517. CHECK (map.find ({ f6, none }, global, true) == "panel.channelRack");
  6518. CHECK (map.find ({ f8, alt }, global, false) == "panel.browser");
  6519. CHECK (map.find ({ insert, ctrl | shift }, global, false) == "pattern.insert");
  6520. CHECK (map.find ({ numpadPlus, none }, rack, true) == "pattern.next");
  6521. // panel context: only while focused
  6522. CHECK (map.find ({ 'L', ctrl }, rack, true) == "rack.sendToInserts");
  6523. CHECK (map.find ({ 'L', ctrl }, global, true).empty());
  6524. CHECK (map.find ({ up, alt }, rack, false) == "rack.moveUp");
  6525. // typing piano: note keys play, other plain keys only for whilePiano actions; chords always work
  6526. CHECK (KeyMap::playsNote ({ 'Z', none }, true) && ! KeyMap::playsNote ({ 'Z', none }, false) && ! KeyMap::playsNote ({ 'Z', ctrl }, true));
  6527. CHECK (map.find ({ 'L', none }, global, true) == "transport.toggleMode");
  6528. CHECK (map.find ({ '1', none }, rack, false) == "rack.mute1" && map.find ({ '0', none }, rack, false) == "rack.mute10");
  6529. CHECK (map.find ({ '1', none }, rack, true).empty()); // a plain digit is not a shortcut while the piano is on
  6530. CHECK (map.find ({ '2', ctrl }, rack, true) == "rack.solo2");
  6531. CHECK (typingPianoNote ('Z') == 48 && typingPianoNote ('q') == 60 && typingPianoNote ('P') == 76 && typingPianoNote ('L') == -1);
  6532. CHECK (map.conflicts().empty());
  6533. // chord text
  6534. CHECK (toString ({ f4, ctrl | shift }) == "Ctrl+Shift+F4" && toString ({ numpadMinus, none }) == "Numpad -");
  6535. CHECK (parseChord ("Shift+Ctrl+Ins") == (Chord { insert, ctrl | shift }));
  6536. CHECK (parseChord ("alt+up") == (Chord { up, alt }) && parseChord ("k") == (Chord { 'K', none }));
  6537. CHECK (parseChord ("Numpad +") == (Chord { numpadPlus, none }) && ! parseChord ("F13") && ! parseChord ("Ctrl+"));
  6538. for (const auto& a : actions())
  6539. for (const auto& c : a.defaults)
  6540. CHECK (parseChord (toString (c)) == c);
  6541. // JSON overrides
  6542. std::vector<std::string> errors;
  6543. CHECK (map.loadJson (R"({ "pattern.next": "Ctrl+Right", "rack.clone": ["Alt+D", "Ctrl+D"], "help.shortcuts": "", "nope": "F1", "file.open": "Hyper+Q" })", &errors));
  6544. CHECK (errors.size() == 2);
  6545. CHECK (map.find ({ right, ctrl }, global, false) == "pattern.next" && map.find ({ numpadPlus, none }, global, false).empty());
  6546. CHECK (map.find ({ 'D', ctrl }, rack, false) == "rack.clone" && map.find ({ f1, none }, global, false).empty());
  6547. map.setChords ("pattern.previous", { { right, ctrl } });
  6548. CHECK (map.conflicts().size() == 1);
  6549. KeyMap reloaded;
  6550. CHECK (reloaded.loadJson (map.toJson()) && reloaded.chordsFor ("rack.clone") == map.chordsFor ("rack.clone"));
  6551. CHECK (! map.loadJson ("[1, 2]") && ! map.loadJson ("{\"a\": }"));
  6552. map.resetToDefaults();
  6553. CHECK (map.find ({ numpadPlus, none }, global, false) == "pattern.next");
  6554. }
  6555. TEST (workflow_workspace_layout)
  6556. {
  6557. using namespace lynx::workspace;
  6558. auto l = Layout::makeDefault (1280, 620);
  6559. // every visible panel is inside the desktop; the default shows Browser, Playlist and Channel Rack
  6560. for (const auto& p : l.panels)
  6561. CHECK (p.bounds.x >= 0 && p.bounds.y >= 0 && p.bounds.right() <= 1280 && p.bounds.bottom() <= 620 && p.bounds.w >= minWidth);
  6562. CHECK (l[Panel::browser].visible && l[Panel::playlist].visible && l[Panel::channelRack].visible);
  6563. CHECK (! l[Panel::pianoRoll].visible && ! l[Panel::mixer].visible);
  6564. CHECK (l[Panel::playlist].bounds.x == l[Panel::browser].bounds.right() && l[Panel::playlist].bounds.right() == 1280);
  6565. CHECK (l.isFrontmost (Panel::channelRack) && l.focused == Panel::channelRack);
  6566. // toggle: show+focus, focus, hide
  6567. CHECK (l.toggle (Panel::mixer) && l.focused == Panel::mixer && l.isFrontmost (Panel::mixer));
  6568. CHECK (l.toggle (Panel::channelRack) && l.focused == Panel::channelRack && l.isFrontmost (Panel::channelRack));
  6569. CHECK (! l.toggle (Panel::channelRack) && ! l[Panel::channelRack].visible && l.focused == Panel::mixer);
  6570. l.toggleMaximised (Panel::playlist);
  6571. CHECK (l[Panel::playlist].maximised && l.focused == Panel::playlist);
  6572. // save and load at another size: positions scale
  6573. l[Panel::mixer].detached = true;
  6574. l[Panel::mixer].windowBounds = { 100, 50, 900, 500 };
  6575. const auto text = l.serialise();
  6576. const auto big = Layout::parse (text, 2560, 1240);
  6577. CHECK (big.has_value());
  6578. if (big)
  6579. {
  6580. CHECK ((*big)[Panel::playlist].maximised && (*big)[Panel::mixer].detached && (*big)[Panel::mixer].windowBounds.w == 900);
  6581. CHECK (std::abs ((*big)[Panel::playlist].bounds.x - 2 * l[Panel::playlist].bounds.x) <= 1);
  6582. CHECK (big->order == l.order && big->focused == l.focused);
  6583. }
  6584. 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));
  6585. // resize keeps panels inside and edges glued to the border
  6586. l.resize (1000, 500);
  6587. CHECK (l[Panel::playlist].bounds.right() == 1000 && l[Panel::browser].bounds.bottom() == 500);
  6588. for (const auto& p : l.panels)
  6589. CHECK (p.bounds.right() <= 1000 && p.bounds.bottom() <= 500);
  6590. // off-screen panels come back
  6591. l[Panel::pianoRoll].bounds = { 5000, -300, 400, 300 };
  6592. CHECK (l.bringBackOffscreen() == 1 && l[Panel::pianoRoll].bounds.x + 400 <= 1000 && l[Panel::pianoRoll].bounds.y == 0);
  6593. // snapping: to the desktop, to another panel, and resizing one edge
  6594. CHECK ((snap ({ 5, 6, 200, 100 }, {}, 1000, 500) == Rect { 0, 0, 200, 100 }));
  6595. CHECK ((snap ({ 304, 50, 200, 100 }, { { 0, 0, 300, 400 } }, 1000, 500) == Rect { 300, 50, 200, 100 }));
  6596. CHECK ((snap ({ 304, 50, 200, 100 }, { { 0, 0, 300, 400 } }, 1000, 500, 2) == Rect { 304, 50, 200, 100 })); // out of range
  6597. CHECK ((snap ({ 100, 100, 195, 100 }, { { 300, 0, 300, 400 } }, 1000, 500, 8, right) == Rect { 100, 100, 200, 100 }));
  6598. CHECK ((snap ({ 100, 100, 195, 100 }, { { 300, 1000, 300, 400 } }, 2000, 2000, 8, right) == Rect { 100, 100, 195, 100 })); // not facing
  6599. }
  6600. TEST (workflow_control_math)
  6601. {
  6602. using namespace lynx::controls;
  6603. CHECK (std::abs (dragValue (0.5, 75, 0, 1, 150, false) - 1.0) < 1e-9);
  6604. CHECK (std::abs (dragValue (0.5, 75, 0, 1, 150, true) - 0.55) < 1e-9);
  6605. CHECK (dragValue (0.5, -1000, -1, 1, 150, false) == -1.0);
  6606. 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);
  6607. CHECK (wheelValue (3, 1, 0, 10, true, 1) == 4.0); // integer controls move at least one step
  6608. CHECK (std::abs (snapToInterval (0.234, 0, 0.1) - 0.2) < 1e-9);
  6609. CHECK (tempoDrag (130.0, 8, false, false) == 132.0 && tempoDrag (130.0, -3, false, false) == 130.0);
  6610. CHECK (std::abs (tempoDrag (130.0, 25, true, false) - 130.25) < 1e-9);
  6611. CHECK (std::abs (tempoDrag (130.0, 8, false, true) - 130.2) < 1e-9);
  6612. CHECK (tempoDrag (520.0, 400, false, false) == lynx::maxTempo && halfTempo (15.0) == lynx::minTempo && doubleTempo (130.0) == 260.0);
  6613. TapTempo tap;
  6614. CHECK (! tap.tap (1000.0));
  6615. CHECK (tap.tap (1500.0) == 120.0);
  6616. CHECK (tap.tap (2000.0) == 120.0 && tap.count() == 3);
  6617. CHECK (std::abs (*tap.tap (2400.0) - 128.57) < 0.01); // average of 500, 500, 400 ms
  6618. CHECK (! tap.tap (9000.0) && tap.count() == 1); // a long pause starts over
  6619. }
  6620. TEST (flp_fl11_mixer_slots_and_disabled_channels)
  6621. {
  6622. // REA_rodada80 §3: FL <= 11 files close an effect slot without 0x62 (the slot is the 0xD4 record's second
  6623. // i32); the channel's 0x00 byte is its Channel Rack switch
  6624. if (realFlpDir.empty())
  6625. return;
  6626. auto load = [] (const std::filesystem::path& p) {
  6627. std::ifstream in (p, std::ios::binary);
  6628. std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  6629. return bytes;
  6630. };
  6631. const auto base = std::filesystem::path (realFlpDir);
  6632. if (const auto b = load (base / "System" / "Render tests" / "Fruity Phase Inverter.flp"); ! b.empty())
  6633. {
  6634. const auto project = flp::parseProject (b);
  6635. CHECK (project.mixer.size() > 2 && project.mixer[1].slots.size() == 1);
  6636. if (project.mixer.size() > 2 && project.mixer[1].slots.size() == 1)
  6637. CHECK (project.mixer[1].slots[0].index == 0 && project.mixer[1].slots[0].plugin == "Fruity Wrapper"
  6638. && ! project.mixer[1].slots[0].state.empty());
  6639. }
  6640. if (const auto b = load (base / "Demo projects" / "Performance mode" / "nucleon - Steptime.flp"); ! b.empty())
  6641. {
  6642. const auto project = flp::parseProject (b);
  6643. CHECK (project.mixer.size() > 4 && project.mixer[3].slots.size() == 4);
  6644. if (project.mixer.size() > 4 && project.mixer[3].slots.size() == 4)
  6645. for (int i = 0; i < 4; ++i)
  6646. CHECK (project.mixer[3].slots[(std::size_t) i].index == i);
  6647. const char* names[] = { "Fruity Delay Bank", "Fruity Reeverb 2", "Maximus", "Fruity Parametric EQ 2" };
  6648. if (project.mixer.size() > 4 && project.mixer[3].slots.size() == 4)
  6649. for (int i = 0; i < 4; ++i)
  6650. CHECK (project.mixer[3].slots[(std::size_t) i].plugin == names[i]);
  6651. }
  6652. if (const auto b = load (base / "Demo projects" / "Product demos" / "Synth_dfr - Overcome - Sytrus.flp"); ! b.empty())
  6653. {
  6654. const auto project = flp::parseProject (b);
  6655. int disabled = 0;
  6656. for (const auto& c : project.channels)
  6657. disabled += ! c.enabled;
  6658. CHECK (disabled == 2);
  6659. const auto song = lynx::importFlp (project);
  6660. int muted = 0;
  6661. for (const auto& c : song.channels)
  6662. muted += c.muted;
  6663. CHECK (muted == 2);
  6664. }
  6665. }
  6666. TEST (three_osc_host_hq_render_box_filters_aliasing_shapes)
  6667. {
  6668. // REA_rodada80 §3.3 (3x Osc FUN_0073a000): in FL's HQ render a saw is the sum of k = max(inc >> 22, 1) phase-shifted
  6669. // copies at gain g/k; a sine is not oversampled
  6670. auto state = [] (std::int32_t shape) {
  6671. std::vector<std::uint8_t> st (0x60, 0);
  6672. const std::int32_t version = 14;
  6673. std::memcpy (st.data(), &version, 4);
  6674. std::memcpy (st.data() + 4 + 4 * 1, &shape, 4); // osc 1 shape; the mix knobs at 0: osc 1 alone
  6675. std::vector<std::uint8_t> snap (364, 0);
  6676. const std::int32_t neutral[17] = { 0, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, 0 };
  6677. for (int k = 0; k < 5; ++k)
  6678. std::memcpy (snap.data() + 0x44 * k, neutral, sizeof (neutral));
  6679. const std::int32_t modX = 256, four = 4, m101 = -101;
  6680. std::memcpy (snap.data() + 0x44, &four, 4);
  6681. std::memcpy (snap.data() + 0x44 + 64, &m101, 4);
  6682. std::memcpy (snap.data() + 0x15c, &modX, 4);
  6683. st.insert (st.end(), snap.begin(), snap.end());
  6684. return st;
  6685. };
  6686. static bool hostHq = false;
  6687. auto render = [&] (std::int32_t shape, bool hq) {
  6688. hostHq = hq;
  6689. auto inst = lynx::makeThreeOscInstrument ([]() noexcept { return hostHq; });
  6690. lynx::ChannelDef c;
  6691. c.instrument = lynx::InstrumentKind::threeosc;
  6692. c.pluginState = state (shape);
  6693. inst->configure (c, 140.0);
  6694. inst->prepare (44100.0);
  6695. inst->noteOn (96, 0.78f);
  6696. std::vector<float> l (4096), r (4096);
  6697. inst->render (l.data(), r.data(), 4096, 1.0f, 1.0f);
  6698. return l;
  6699. };
  6700. auto compare = [&] (std::int32_t shape) {
  6701. const auto live = render (shape, false), hq = render (shape, true);
  6702. double diff = 0.0, energy = 0.0;
  6703. for (std::size_t i = 0; i < live.size(); ++i)
  6704. diff += std::abs (live[i] - hq[i]), energy += std::abs (live[i]);
  6705. return std::make_pair (energy, diff);
  6706. };
  6707. CHECK (lynx::threeOscStateUsable (state (3)));
  6708. const auto [sawEnergy, sawDiff] = compare (3);
  6709. CHECK (sawEnergy > 1.0 && sawDiff > 1.0e-3); // a saw renders differently in the host's HQ mode
  6710. const auto [sineEnergy, sineDiff] = compare (0);
  6711. CHECK (sineEnergy > 1.0 && sineDiff == 0.0); // a sine does not
  6712. }
  6713. namespace
  6714. {
  6715. // The runner's options (round speed185a). They choose WHICH tests run and how the output looks, never what a test checks.
  6716. // [folder] the FL Data folder (realFlpDir), as before
  6717. // --list print the selected test names, one per line, and run nothing
  6718. // --filter a,b run only the tests whose name contains one of these; several --filter options must all match
  6719. // (the LYNX_TEST variable works as before, as one more --filter)
  6720. // --exclude a,b skip the tests whose name contains one of these
  6721. // --shard i/n run only shard i of n (1-based): the selected tests split into n disjoint sets that together
  6722. // cover them all. With --weights the sets are balanced by time, otherwise test k goes to k mod n.
  6723. // --weights file "<name> <ms>" lines (--times output) used to balance the shards; unknown tests weigh 50 ms
  6724. // --times file write "<name> <ms>" per test that ran
  6725. // --no-times no per-test time lines
  6726. // Output: each test's name on its own line (as before), its FAILED lines, then " time <ms> ms"; at the end the slowest
  6727. // tests and the unchanged "N checks, M failures" line. stdout is line-buffered so a long test never looks hung.
  6728. struct RunnerOptions
  6729. {
  6730. bool list = false, times = true;
  6731. std::vector<std::vector<std::string>> filters; // (each must match: one of its names)
  6732. std::vector<std::string> excludes;
  6733. int shard = 0, shards = 1;
  6734. std::string weightsFile, timesFile;
  6735. };
  6736. std::vector<std::string> splitCommas (const std::string& s)
  6737. {
  6738. std::vector<std::string> out;
  6739. std::string cur;
  6740. for (char c : s + ",")
  6741. if (c == ',')
  6742. {
  6743. if (! cur.empty())
  6744. out.push_back (cur);
  6745. cur.clear();
  6746. }
  6747. else
  6748. cur += c;
  6749. return out;
  6750. }
  6751. bool parseRunnerOptions (int argc, char** argv, RunnerOptions& o)
  6752. {
  6753. for (int i = 1; i < argc; ++i)
  6754. {
  6755. const std::string a = argv[i];
  6756. auto value = [&] () -> std::optional<std::string> {
  6757. if (i + 1 >= argc)
  6758. return std::nullopt;
  6759. return std::string (argv[++i]);
  6760. };
  6761. if (a == "--list")
  6762. o.list = true;
  6763. else if (a == "--no-times")
  6764. o.times = false;
  6765. else if (a == "--filter" || a == "--exclude" || a == "--shard" || a == "--weights" || a == "--times")
  6766. {
  6767. const auto v = value();
  6768. if (! v)
  6769. {
  6770. std::fprintf (stderr, "lynx_tests: %s needs a value\n", a.c_str());
  6771. return false;
  6772. }
  6773. if (a == "--filter")
  6774. o.filters.push_back (splitCommas (*v));
  6775. else if (a == "--exclude")
  6776. for (auto& f : splitCommas (*v)) o.excludes.push_back (f);
  6777. else if (a == "--weights")
  6778. o.weightsFile = *v;
  6779. else if (a == "--times")
  6780. o.timesFile = *v;
  6781. else
  6782. {
  6783. int k = 0, n = 0;
  6784. char tail = 0;
  6785. if (std::sscanf (v->c_str(), "%d/%d%c", &k, &n, &tail) != 2 || n < 1 || k < 1 || k > n)
  6786. {
  6787. std::fprintf (stderr, "lynx_tests: --shard wants i/n with 1 <= i <= n, not %s\n", v->c_str());
  6788. return false;
  6789. }
  6790. o.shard = k - 1;
  6791. o.shards = n;
  6792. }
  6793. }
  6794. else if (a.size() > 2 && a.compare (0, 2, "--") == 0)
  6795. {
  6796. std::fprintf (stderr, "lynx_tests: unknown option %s\n", a.c_str());
  6797. return false;
  6798. }
  6799. else
  6800. realFlpDir = a;
  6801. }
  6802. if (const char* only = std::getenv ("LYNX_TEST")) // run only the tests whose name contains this
  6803. o.filters.push_back ({ only });
  6804. return true;
  6805. }
  6806. // which of the selected tests (indices into `names`) belong to shard o.shard: a partition of all of them over o.shards
  6807. std::vector<std::size_t> shardOf (const std::vector<const char*>& names, const RunnerOptions& o)
  6808. {
  6809. std::vector<std::size_t> all (names.size());
  6810. for (std::size_t k = 0; k < all.size(); ++k)
  6811. all[k] = k;
  6812. if (o.shards <= 1)
  6813. return all;
  6814. std::map<std::string, double> weight;
  6815. if (! o.weightsFile.empty())
  6816. {
  6817. std::ifstream in (o.weightsFile);
  6818. std::string name;
  6819. double ms = 0.0;
  6820. while (in >> name >> ms)
  6821. weight[name] = ms;
  6822. }
  6823. std::vector<int> owner (names.size(), 0);
  6824. if (weight.empty())
  6825. for (std::size_t k = 0; k < names.size(); ++k)
  6826. owner[k] = (int) (k % (std::size_t) o.shards);
  6827. else
  6828. {
  6829. // longest first onto the least loaded shard (ties: lower index), deterministic for a given list and file
  6830. auto order = all;
  6831. auto w = [&] (std::size_t k) { const auto it = weight.find (names[k]); return it == weight.end() ? 50.0 : it->second; };
  6832. std::stable_sort (order.begin(), order.end(), [&] (std::size_t a, std::size_t b) { return w (a) > w (b); });
  6833. std::vector<double> load ((std::size_t) o.shards, 0.0);
  6834. for (auto k : order)
  6835. {
  6836. const auto s = (std::size_t) (std::min_element (load.begin(), load.end()) - load.begin());
  6837. owner[k] = (int) s;
  6838. load[s] += w (k);
  6839. }
  6840. }
  6841. std::vector<std::size_t> mine;
  6842. for (auto k : all)
  6843. if (owner[k] == o.shard)
  6844. mine.push_back (k); // (in registry order: a shard runs its tests in the usual order)
  6845. return mine;
  6846. }
  6847. } // namespace
  6848. int main (int argc, char** argv)
  6849. {
  6850. std::setvbuf (stdout, nullptr, _IOLBF, 0);
  6851. RunnerOptions opt;
  6852. if (! parseRunnerOptions (argc, argv, opt))
  6853. return 2;
  6854. auto& tests = registry();
  6855. std::vector<const char*> names;
  6856. std::vector<std::size_t> selected;
  6857. for (std::size_t k = 0; k < tests.size(); ++k)
  6858. {
  6859. const char* name = tests[k].first;
  6860. auto has = [name] (const std::string& part) { return std::strstr (name, part.c_str()) != nullptr; };
  6861. if (! std::all_of (opt.filters.begin(), opt.filters.end(), [&] (const auto& any) { return std::any_of (any.begin(), any.end(), has); }))
  6862. continue;
  6863. if (std::any_of (opt.excludes.begin(), opt.excludes.end(), has))
  6864. continue;
  6865. names.push_back (name);
  6866. selected.push_back (k);
  6867. }
  6868. const auto mine = shardOf (names, opt);
  6869. if (opt.list)
  6870. {
  6871. for (auto k : mine)
  6872. std::printf ("%s\n", names[k]);
  6873. return 0;
  6874. }
  6875. std::vector<std::pair<double, const char*>> took;
  6876. for (auto k : mine)
  6877. {
  6878. auto& [name, fn] = tests[selected[k]];
  6879. std::printf ("%s\n", name);
  6880. const auto start = std::chrono::steady_clock::now();
  6881. fn();
  6882. const auto ms = std::chrono::duration<double, std::milli> (std::chrono::steady_clock::now() - start).count();
  6883. took.emplace_back (ms, name);
  6884. if (opt.times)
  6885. std::printf (" time %.0f ms\n", ms);
  6886. }
  6887. if (! opt.timesFile.empty())
  6888. {
  6889. std::ofstream out (opt.timesFile);
  6890. for (auto& [ms, name] : took)
  6891. out << name << ' ' << (long long) std::llround (ms) << '\n';
  6892. }
  6893. if (opt.times && took.size() > 1)
  6894. {
  6895. double total = 0.0;
  6896. for (auto& t : took)
  6897. total += t.first;
  6898. auto slow = took;
  6899. std::sort (slow.begin(), slow.end(), [] (const auto& a, const auto& b) { return a.first > b.first; });
  6900. slow.resize (std::min<std::size_t> (slow.size(), 15));
  6901. std::printf ("\n %zu tests in %.1f s%s; slowest:\n", took.size(), total / 1000.0,
  6902. opt.shards > 1 ? (" (shard " + std::to_string (opt.shard + 1) + "/" + std::to_string (opt.shards) + ")").c_str() : "");
  6903. for (auto& [ms, name] : slow)
  6904. std::printf (" %8.1f s %s\n", ms / 1000.0, name);
  6905. }
  6906. std::printf ("\n%d checks, %d failures\n", checks, failures);
  6907. return failures == 0 ? 0 : 1;
  6908. }
  6909. TEST (synth_cutoff_keytrack)
  6910. {
  6911. // the cutoff follows the note from key 48 (130.81 Hz), like Harmor's "Key to filter frequency"
  6912. auto rms = [] (float keyTrack, int key) {
  6913. lynx::SynthSettings s;
  6914. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  6915. s.cutoff = 130.81f;
  6916. s.keyTrack = keyTrack;
  6917. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  6918. inst->prepare (48000.0);
  6919. lynx::ChannelDef c;
  6920. c.synth = s;
  6921. inst->configure (c, 120.0);
  6922. inst->noteOn (key, 1.0f);
  6923. std::vector<float> l (24000), r (24000);
  6924. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  6925. double e = 0.0;
  6926. for (int i = 4800; i < 24000; ++i)
  6927. e += (double) l[(std::size_t) i] * l[(std::size_t) i];
  6928. return std::sqrt (e / 19200.0);
  6929. };
  6930. const auto fixed = rms (0.0f, 72), tracked = rms (1.0f, 72);
  6931. CHECK (tracked > 4.0 * fixed); // 2 octaves up: without keytrack the low-pass cuts
  6932. CHECK (std::abs (rms (1.0f, 48) - rms (0.0f, 48)) < 0.05 * rms (0.0f, 48)); // unchanged at the reference key
  6933. }
  6934. namespace
  6935. {
  6936. std::vector<float> renderSynthNote (const lynx::SynthSettings& s, int key, int frames)
  6937. {
  6938. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  6939. inst->prepare (48000.0);
  6940. lynx::ChannelDef c;
  6941. c.synth = s;
  6942. inst->configure (c, 120.0);
  6943. inst->noteOn (key, 1.0f);
  6944. std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
  6945. inst->render (l.data(), r.data(), frames, 1.0f, 1.0f);
  6946. return l;
  6947. }
  6948. double goertzel (const std::vector<float>& x, double f, int from, int to)
  6949. {
  6950. const auto w = 2.0 * 3.141592653589793 * f / 48000.0, k = 2.0 * std::cos (w);
  6951. double s1 = 0.0, s2 = 0.0;
  6952. for (int i = from; i < to; ++i)
  6953. {
  6954. const auto s0 = x[(std::size_t) i] + k * s1 - s2;
  6955. s2 = s1;
  6956. s1 = s0;
  6957. }
  6958. return s1 * s1 + s2 * s2 - k * s1 * s2;
  6959. }
  6960. } // namespace
  6961. TEST (synth_pitch_sweep_ring_and_bands)
  6962. {
  6963. lynx::SynthSettings sine;
  6964. sine.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  6965. sine.cutoff = 20000.0f;
  6966. // sweep: starts at f·(1 + sweep) and returns to f (time constant s = 10 ms; s⁴ falls 4× faster)
  6967. auto sweep = sine;
  6968. sweep.sweep = 1.0f;
  6969. sweep.sweepTime = 0.01f;
  6970. const auto a = renderSynthNote (sweep, 69, 24000);
  6971. auto crossings = [] (const std::vector<float>& x, int to) {
  6972. int n = 0;
  6973. for (int i = 1; i < to; ++i)
  6974. n += (x[(std::size_t) i - 1] < 0.0f) != (x[(std::size_t) i] < 0.0f);
  6975. return n;
  6976. };
  6977. CHECK (crossings (a, 480) > 1.3 * crossings (renderSynthNote (sine, 69, 480), 480)); // 10 ms: much higher
  6978. CHECK (goertzel (a, 440.0, 12000, 24000) > 100.0 * goertzel (a, 880.0, 12000, 24000));
  6979. // ring with |a| = 1: carrier 440 × modulator 110 -> only 330 and 550, no 440
  6980. auto ring = sine;
  6981. ring.am = ring.ring = true;
  6982. ring.osc[2] = { lynx::Waveform::sine, -2, 0.0f, 1.0f };
  6983. const auto b = renderSynthNote (ring, 69, 24000);
  6984. CHECK (goertzel (b, 330.0, 4800, 24000) > 100.0 * goertzel (b, 440.0, 4800, 24000));
  6985. CHECK (goertzel (b, 550.0, 4800, 24000) > 100.0 * goertzel (b, 440.0, 4800, 24000));
  6986. // 3 bands: muting the high knocks down a 4 kHz note and barely touches an 80 Hz one
  6987. auto bands = sine;
  6988. bands.bandGain = { 1.0f, 1.0f, 0.0f };
  6989. auto rms = [] (const std::vector<float>& x) {
  6990. double e = 0.0;
  6991. for (std::size_t i = 4800; i < x.size(); ++i)
  6992. e += (double) x[i] * x[i];
  6993. return std::sqrt (e / (double) (x.size() - 4800));
  6994. };
  6995. CHECK (rms (renderSynthNote (bands, 107, 24000)) < 0.3 * rms (renderSynthNote (sine, 107, 24000)));
  6996. CHECK (rms (renderSynthNote (bands, 28, 24000)) > 0.85 * rms (renderSynthNote (sine, 28, 24000)));
  6997. }
  6998. TEST (love_philter_map_evaluation)
  6999. {
  7000. lynx::LovePhilterMap line { { { 0.0, 0.0 }, { 1.0, 1.0 } } };
  7001. CHECK (std::abs (lynx::lovePhilterMapAt (line, 0.5) - 0.5) < 1e-9);
  7002. CHECK (std::abs (lynx::lovePhilterMapAt (line, 1.0) - 1.0) < 1e-9);
  7003. // steps (Δx = 0): 1 until 0.4375, ramp to 0 at 0.5, then 0
  7004. lynx::LovePhilterMap steps { { { 0.0, 1.0 }, { 0.4375, 1.0 }, { 0.0625, 0.0 }, { 0.5, 0.0 } } };
  7005. CHECK (lynx::lovePhilterMapAt (steps, 0.27) == 1.0);
  7006. CHECK (lynx::lovePhilterMapAt (steps, 0.75) == 0.0);
  7007. CHECK (std::abs (lynx::lovePhilterMapAt (steps, 0.46875) - 0.5) < 1e-9);
  7008. CHECK (lynx::lovePhilterMapAt ({}, 0.3) == 1.0);
  7009. }
  7010. TEST (lofi_holds_and_quantizes)
  7011. {
  7012. auto fx = lynx::makeEffect (lynx::EffectKind::lofi);
  7013. fx->prepare (48000.0);
  7014. auto slot = lynx::makeEffectSlot (lynx::EffectKind::lofi);
  7015. slot.params[0] = 0.5f; // 128·0.125 + 1 = 17 samples
  7016. slot.params[1] = 1.0f; // 65536/2049 ≈ 32 levels
  7017. std::vector<float> l (170), r (170);
  7018. for (std::size_t i = 0; i < l.size(); ++i)
  7019. l[i] = r[i] = 0.9f * (float) std::sin (0.05 * (double) i);
  7020. const auto original = l;
  7021. fx->process (l.data(), r.data(), 170, slot.params, 120.0);
  7022. for (int i = 0; i < 17; ++i)
  7023. CHECK (l[(std::size_t) i] == l[0]);
  7024. CHECK (l[17] != l[16]);
  7025. const auto step = 2.0f * 2049.0f / 65536.0f;
  7026. for (auto v : l)
  7027. CHECK (std::abs (v / step - std::round (v / step)) < 1e-3f);
  7028. CHECK (std::abs (l[0] - original[0]) <= step);
  7029. CHECK (lynx::formatEffectParam (lynx::EffectKind::lofi, 1, 0.0f, 120.0) == "16.0 bits");
  7030. }
  7031. namespace
  7032. {
  7033. lynx::EffectParams effectorParams (int type, float x, float y)
  7034. {
  7035. auto slot = lynx::makeEffectSlot (lynx::EffectKind::effector);
  7036. slot.params[0] = (float) type / 11.0f;
  7037. slot.params[3] = x;
  7038. slot.params[4] = y;
  7039. return slot.params;
  7040. }
  7041. } // namespace
  7042. TEST (effector_algorithms)
  7043. {
  7044. CHECK (lynx::effector::typeOf (8.0f / 11.0f) == 8);
  7045. CHECK (std::string (lynx::effector::typeName (11)) == "Ring");
  7046. CHECK (std::abs (lynx::effector::rateBeats (0.5f, 0.0f) - 0.75) < 1e-9); // trunc(8·0.5 + 0.5) = 4 -> 3/4
  7047. CHECK (std::abs (lynx::effector::rateBeats (1.0f, 0.5f) - 2.0) < 1e-9); // 8 × (1 − 0.5)²
  7048. auto run = [] (const lynx::EffectParams& p, std::vector<float> in, double bpm = 120.0) {
  7049. auto fx = lynx::makeEffect (lynx::EffectKind::effector);
  7050. fx->prepare (48000.0);
  7051. fx->setSongPosition (0.0);
  7052. auto r = in;
  7053. fx->process (in.data(), r.data(), (int) in.size(), p, bpm);
  7054. return in;
  7055. };
  7056. // Distort with Y = 1 (everything goes to the cascade): 0.5 comes out 0.5 (compensation G = 0.5/F(0.5))
  7057. {
  7058. auto p = effectorParams (0, 0.5f, 1.0f);
  7059. const auto out = run (p, std::vector<float> (4800, 0.5f));
  7060. CHECK (std::abs (out.back() - 0.5f) < 1e-3f);
  7061. const auto small = run (p, std::vector<float> (4800, 0.05f));
  7062. CHECK (small.back() > 0.45f && small.back() < 0.5f); // saturates near ±0.5
  7063. }
  7064. // Ring: DC 1 becomes the carrier sine, 2^(8.7·0 + 5) = 32 Hz -> 32 cycles per second
  7065. {
  7066. auto p = effectorParams (11, 0.0f, 0.0f);
  7067. const auto out = run (p, std::vector<float> (48000, 1.0f));
  7068. int crossings = 0;
  7069. for (std::size_t i = 4800; i < out.size(); ++i)
  7070. crossings += (out[i - 1] < 0.0f) != (out[i] < 0.0f);
  7071. CHECK (crossings >= 56 && crossings <= 60); // 0.9 s × 32 Hz × 2
  7072. }
  7073. // Trancer, Y = 0: at the start of the period the gain drops to zero; X = 1 returns only at the end of the period
  7074. {
  7075. auto p = effectorParams (8, 1.0f, 0.0f);
  7076. p[5] = 1.0f; // 8/1 = 8 beats = 4 s at 120 BPM
  7077. const auto out = run (p, std::vector<float> (48000, 1.0f));
  7078. CHECK (out[100] > 0.3f); // still rising on the "and" (1% of the period = 40 ms)
  7079. CHECK (out[24000] < 0.05f); // 0.5 s: trough
  7080. }
  7081. // Reverb with Wet = 0: the dry passes intact
  7082. {
  7083. auto p = effectorParams (6, 0.5f, 0.5f);
  7084. p[2] = 0.0f;
  7085. std::vector<float> in (4800);
  7086. for (std::size_t i = 0; i < in.size(); ++i)
  7087. in[i] = (float) std::sin (0.01 * (double) i);
  7088. const auto out = run (p, in);
  7089. for (std::size_t i = 0; i < in.size(); ++i)
  7090. CHECK (std::abs (out[i] - in[i]) < 1e-6f);
  7091. }
  7092. // Delay: the echo arrives after the Mod Rate period (1/4 beat at 120 BPM = 125 ms), at level X²
  7093. {
  7094. auto p = effectorParams (5, 0.7f, 0.5f);
  7095. p[5] = 1.0f / 8.0f; // index 1 = 1/4
  7096. std::vector<float> in (12000, 0.0f);
  7097. in[0] = 1.0f;
  7098. const auto out = run (p, in);
  7099. CHECK (std::abs (out[6000] - 0.49f) < 0.02f);
  7100. CHECK (std::abs (out[3000]) < 1e-6f);
  7101. }
  7102. // Stutter: repeats the recorded start every period
  7103. {
  7104. auto p = effectorParams (9, 0.0f, 0.0f);
  7105. p[5] = 0.0f; // 1/8 beat = 62.5 ms = 3000 samples
  7106. std::vector<float> in (9000);
  7107. for (std::size_t i = 0; i < in.size(); ++i)
  7108. in[i] = (float) i / 9000.0f;
  7109. const auto out = run (p, in);
  7110. CHECK (std::abs (out[3000 + 500] - in[500]) < 0.01f); // after the crossfade, it's the start again
  7111. CHECK (std::abs (out[1000] - in[1000]) < 1e-6f);
  7112. }
  7113. }
  7114. TEST (crossover_linkwitz_riley_sums_flat)
  7115. {
  7116. // low band + high band of the same crossover: sum with magnitude 1 (Linkwitz-Riley; for 12 and 36 dB the HP is inverted)
  7117. for (const auto combo : { 1, 3, 4, 5, 6, 7 })
  7118. for (const auto testHz : { 200.0, 1000.0, 5000.0 })
  7119. {
  7120. auto run = [&] (bool high) {
  7121. auto fx = lynx::makeEffect (lynx::EffectKind::crossover);
  7122. fx->prepare (48000.0);
  7123. auto slot = lynx::makeEffectSlot (lynx::EffectKind::crossover);
  7124. const auto value = (float) (std::log2 (1000.0 / 30.0) / 9.0589);
  7125. slot.params[high ? 0 : 3] = 1.0f;
  7126. slot.params[high ? 1 : 4] = value;
  7127. slot.params[high ? 2 : 5] = (float) combo / 7.0f;
  7128. std::vector<float> l (24000), r (24000);
  7129. for (std::size_t i = 0; i < l.size(); ++i)
  7130. l[i] = r[i] = (float) std::sin (2.0 * 3.141592653589793 * testHz * (double) i / 48000.0);
  7131. fx->process (l.data(), r.data(), 24000, slot.params, 120.0);
  7132. return l;
  7133. };
  7134. const auto low = run (false), high = run (true);
  7135. double peak = 0.0;
  7136. for (std::size_t i = 12000; i < low.size(); ++i)
  7137. peak = std::max (peak, (double) std::abs (low[i] + high[i]));
  7138. CHECK (std::abs (peak - 1.0) < 0.02);
  7139. }
  7140. // 96 dB/oct: two octaves above the cutoff the low-pass is already below −150 dB... in practice, very low
  7141. auto fx = lynx::makeEffect (lynx::EffectKind::crossover);
  7142. fx->prepare (48000.0);
  7143. auto slot = lynx::makeEffectSlot (lynx::EffectKind::crossover);
  7144. slot.params[3] = 1.0f;
  7145. slot.params[4] = (float) (std::log2 (500.0 / 30.0) / 9.0589);
  7146. slot.params[5] = 1.0f;
  7147. std::vector<float> l (24000), r (24000);
  7148. for (std::size_t i = 0; i < l.size(); ++i)
  7149. l[i] = r[i] = (float) std::sin (2.0 * 3.141592653589793 * 2000.0 * (double) i / 48000.0);
  7150. fx->process (l.data(), r.data(), 24000, slot.params, 120.0);
  7151. float peak = 0.0f;
  7152. for (std::size_t i = 12000; i < l.size(); ++i)
  7153. peak = std::max (peak, std::abs (l[i]));
  7154. CHECK (peak < 1e-4f);
  7155. }
  7156. TEST (rhythm_gain_and_autopan)
  7157. {
  7158. // 1-beat gate: 1 → 0 at 0.5 beat (linear) → 1 at the end; at 120 BPM 1 beat = 24000 samples at 48 kHz
  7159. auto slot = lynx::makeEffectSlot (lynx::EffectKind::rhythmGain);
  7160. slot.params[0] = 1.0f / 64.0f;
  7161. slot.params[1] = 3.0f / 12.0f;
  7162. const float pts[3][3] = { { 0.0f, 1.0f, 0.5f }, { 0.5f, 0.0f, 0.5f }, { 1.0f, 1.0f, 0.5f } };
  7163. for (int k = 0; k < 3; ++k)
  7164. for (int j = 0; j < 3; ++j)
  7165. slot.params[(std::size_t) (2 + 3 * k + j)] = pts[k][j];
  7166. auto fx = lynx::makeEffect (lynx::EffectKind::rhythmGain);
  7167. fx->prepare (48000.0);
  7168. fx->setSongPosition (960.0 * 2.0); // beat 2: phase 0
  7169. std::vector<float> l (24000, 1.0f), r (24000, 1.0f);
  7170. fx->process (l.data(), r.data(), 24000, slot.params, 120.0);
  7171. CHECK (std::abs (l[0] - 1.0f) < 1e-3f);
  7172. CHECK (std::abs (l[6000] - 0.5f) < 1e-3f);
  7173. CHECK (l[12000] < 1e-3f);
  7174. CHECK (std::abs (l[18000] - 0.5f) < 1e-3f);
  7175. // positive tension: changes quickly at the start of the segment
  7176. CHECK (lynx::effectInfo (lynx::EffectKind::rhythmGain).params.size() == 39);
  7177. // auto-pan: 1-beat period, full depth; start phase 0.75 -> W = 0 rising (pan centered)
  7178. auto pan = lynx::makeEffectSlot (lynx::EffectKind::autoPan);
  7179. pan.params[0] = 1.0f / 64.0f;
  7180. pan.params[1] = 1.0f;
  7181. auto ap = lynx::makeEffect (lynx::EffectKind::autoPan);
  7182. ap->prepare (48000.0);
  7183. ap->setSongPosition (0.0);
  7184. std::vector<float> a (24000, 1.0f), b (24000, 1.0f);
  7185. ap->process (a.data(), b.data(), 24000, pan.params, 120.0);
  7186. CHECK (std::abs (a[0] - 1.0f) < 1e-3f && std::abs (b[0] - 1.0f) < 1e-3f);
  7187. CHECK (a[6000] < 0.01f && std::abs (b[6000] - 1.0f) < 1e-3f); // ¼ cycle later: fully right
  7188. }
  7189. TEST (vocoder_side_inputs_split_modulator_and_carrier)
  7190. {
  7191. // "Side inputs" mode (Vocodex): modulator from side input 1 (noise in the first half), carrier from side input 2
  7192. // (saw), own input silent → the output follows the side modulator; with input 1 silent, the output is silent
  7193. auto slot = lynx::makeEffectSlot (lynx::EffectKind::vocoder);
  7194. slot.params[16] = 1.0f;
  7195. auto fx = lynx::makeEffect (lynx::EffectKind::vocoder);
  7196. fx->prepare (48000.0);
  7197. std::vector<float> modL (48000), car (48000), l (48000, 0.0f), r (48000, 0.0f);
  7198. std::uint32_t seed = 1;
  7199. for (std::size_t i = 0; i < modL.size(); ++i)
  7200. {
  7201. seed = seed * 1664525u + 1013904223u;
  7202. modL[i] = i < 24000 ? ((float) (seed >> 8) / 16777216.0f - 0.5f) : 0.0f;
  7203. car[i] = (float) (2.0 * std::fmod ((double) i * 220.0 / 48000.0, 1.0) - 1.0) * 0.5f;
  7204. }
  7205. for (int done = 0; done < 48000; done += 480)
  7206. {
  7207. fx->setSidechain (modL.data() + done, modL.data() + done);
  7208. fx->setSidechain2 (car.data() + done, car.data() + done);
  7209. fx->process (l.data() + done, r.data() + done, 480, slot.params, 120.0);
  7210. }
  7211. auto rms = [&] (int from, int to) {
  7212. double e = 0.0;
  7213. for (int i = from; i < to; ++i)
  7214. e += (double) l[(std::size_t) i] * l[(std::size_t) i];
  7215. return std::sqrt (e / (to - from));
  7216. };
  7217. CHECK (rms (6000, 20000) > 0.01);
  7218. CHECK (rms (40000, 48000) < 0.1 * rms (6000, 20000));
  7219. // the side modulator configured but silent: nothing comes out (the own input is not used instead)
  7220. std::vector<float> a (4800, 0.3f), b (4800, 0.3f);
  7221. for (int done = 0; done < 4800; done += 480)
  7222. {
  7223. fx->setSidechain (nullptr, nullptr);
  7224. fx->setSidechain2 (car.data() + done, car.data() + done);
  7225. fx->process (a.data() + done, b.data() + done, 480, slot.params, 120.0);
  7226. }
  7227. CHECK (std::abs (a[4700]) < 0.05f);
  7228. }
  7229. TEST (vocoder_follows_modulator)
  7230. {
  7231. // modulator (L) plays only in the first half; carrier (R) is a continuous saw: the wet follows the modulator
  7232. auto slot = lynx::makeEffectSlot (lynx::EffectKind::vocoder);
  7233. auto fx = lynx::makeEffect (lynx::EffectKind::vocoder);
  7234. fx->prepare (48000.0);
  7235. std::vector<float> l (48000), r (48000);
  7236. std::uint32_t seed = 1;
  7237. for (std::size_t i = 0; i < l.size(); ++i)
  7238. {
  7239. seed = seed * 1664525u + 1013904223u;
  7240. l[i] = i < 24000 ? ((float) (seed >> 8) / 16777216.0f - 0.5f) : 0.0f;
  7241. r[i] = (float) (2.0 * std::fmod ((double) i * 220.0 / 48000.0, 1.0) - 1.0) * 0.5f;
  7242. }
  7243. fx->process (l.data(), r.data(), 48000, slot.params, 120.0);
  7244. auto rms = [&] (int from, int to) {
  7245. double e = 0.0;
  7246. for (int i = from; i < to; ++i)
  7247. e += (double) l[(std::size_t) i] * l[(std::size_t) i];
  7248. return std::sqrt (e / (to - from));
  7249. };
  7250. CHECK (rms (6000, 20000) > 0.01);
  7251. CHECK (rms (40000, 48000) < 0.1 * rms (6000, 20000)); // modulator silent: the vocoder fades out (100 ms release)
  7252. for (std::size_t i = 0; i < l.size(); ++i)
  7253. CHECK (l[i] == r[i] && std::isfinite (l[i])); // mono output
  7254. }
  7255. TEST (fruity_vocoder_engine)
  7256. {
  7257. // REA_rodada70: Fruity Vocoder, states of our own against the installation's DLL (goldens = the DLL's output for
  7258. // the same input, EXEC; all three bit-identical over the whole 4096 frames)
  7259. std::vector<float> inL (4096), inR (4096);
  7260. for (int i = 0; i < 4096; ++i)
  7261. {
  7262. inL[(std::size_t) i] = i < 3000 ? (float) (((i * 37) % 200) - 100) / 128.0f : 0.0f; // modulator, then silence
  7263. inR[(std::size_t) i] = (i * 13) % 64 < 32 ? 0.5f : -0.5f; // carrier
  7264. }
  7265. auto make = [] (int bands, int order, int listen, bool swap, std::vector<float> levels, std::array<int, 12> c) {
  7266. lynx::fvocoder::State s;
  7267. s.bands = bands;
  7268. s.order = order;
  7269. s.listen = listen;
  7270. s.swap = swap;
  7271. for (std::size_t k = 0; k < levels.size(); ++k)
  7272. s.levels[k] = levels[k];
  7273. s.c = c;
  7274. return s;
  7275. };
  7276. auto run = [&] (const lynx::fvocoder::State& s, double rate, int block, int holdAt) {
  7277. std::vector<float> l = inL, r = inR;
  7278. lynx::fvocoder::Engine e;
  7279. e.setState (s);
  7280. e.prepare (rate);
  7281. for (int i = 0; i < 4096;)
  7282. {
  7283. auto len = std::min (block, 4096 - i);
  7284. if (holdAt > i && holdAt < i + len)
  7285. len = holdAt - i;
  7286. if (i == holdAt)
  7287. {
  7288. auto t = e.state();
  7289. t.c[lynx::fvocoder::hold] = 1;
  7290. e.setState (t);
  7291. }
  7292. e.processStereo (l.data() + i, r.data() + i, len);
  7293. i += len;
  7294. }
  7295. return std::make_pair (l, r);
  7296. };
  7297. auto check = [] (const std::pair<std::vector<float>, std::vector<float>>& out, const std::vector<std::pair<int, float>>& golden) {
  7298. for (const auto& [i, g] : golden)
  7299. CHECK (out.first[(std::size_t) i] == g && out.second[(std::size_t) i] == g);
  7300. };
  7301. // 16 bands, order 2, edited band levels, modulator and carrier levels in the mix
  7302. const auto a = make (16, 2, 2, false, { 1.0f, 0.5f, 1.5f, 1.0f, 0.25f, 2.0f, 1.0f, 1.0f, 0.75f, 1.25f, 1.0f, 0.0f, 1.0f, 1.0f, 1.75f, 1.0f },
  7303. { 0, 65536, 64, 0, 0, 50, 20, 100, 0, 20, 10, 128 });
  7304. const auto outA = run (a, 44100.0, 512, -1);
  7305. check (outA, { { 100, -0.23145467042922974f }, { 1000, 0.2901436686515808f }, { 2047, 0.03407000005245209f }, { 2500, 0.8314149379730225f },
  7306. { 3000, 0.606562614440918f }, { 3500, 0.2637328505516052f }, { 4095, -0.001206810586154461f } });
  7307. // 64 bands, order 3, inverted synthesis bank shifted by −300 cents, 48 kHz in blocks of 300, Hold from frame 2048
  7308. const auto b = make (64, 3, 2, false, {}, { 2000, 60000, 90, 1, -300, 120, 15, 300, 0, 0, 5, 150 });
  7309. check (run (b, 48000.0, 300, 2048), { { 100, 0.005184958688914776f }, { 1000, -0.0008608861826360226f }, { 2047, -0.026466617360711098f },
  7310. { 2500, -0.009371745400130749f }, { 3000, -0.018949978053569794f }, { 3500, -0.02667868137359619f },
  7311. { 4095, -0.021947938948869705f } });
  7312. // 4 bands, order 1, swapped inputs, narrow 30000…50000 range with the linear scale, 22.05 kHz in blocks of 77
  7313. const auto c = make (4, 1, 2, true, { 1.0f, 0.8f, 1.2f, 1.0f }, { 30000, 50000, 0, 0, 700, 180, 300, 40, 0, 64, 0, 160 });
  7314. check (run (c, 22050.0, 77, -1), { { 100, 0.31434211134910583f }, { 1000, -1.953513264656067f }, { 2047, 1.4135023355484009f },
  7315. { 2500, 1.539392352104187f }, { 3000, -0.27189597487449646f }, { 3500, -0.11583124846220016f },
  7316. { 4095, -0.11583124846220016f } });
  7317. // FL's state imports into the LYNX slot (controls as parameters, the levels in the state) and the slot's effect
  7318. // (one 4096-frame call, split into 512-frame engine blocks) gives the same output
  7319. const auto bytes = lynx::fvocoder::serialise (a);
  7320. CHECK (bytes.size() == 17 + 16 * 4 + 12 * 4);
  7321. const auto slot = lynx::effectFromFl ("Fruity Vocoder", bytes);
  7322. CHECK (slot && slot->kind == lynx::EffectKind::vocoder && slot->state == bytes);
  7323. CHECK (slot && slot->params[0] == 0.4f && slot->params[1] == 0.5f && slot->params[2] == 1.0f && slot->params[15] == 0.8f);
  7324. if (slot)
  7325. {
  7326. auto fx = lynx::makeEffect (*slot);
  7327. fx->prepare (44100.0);
  7328. std::vector<float> l = inL, r = inR;
  7329. fx->process (l.data(), r.data(), 4096, slot->params, 120.0);
  7330. CHECK (l == outA.first && r == outA.second);
  7331. CHECK (lynx::formatEffectParam (lynx::EffectKind::vocoder, 5, slot->params[5], 120.0) == "16000 Hz");
  7332. CHECK (lynx::formatEffectParam (lynx::EffectKind::vocoder, 10, slot->params[10], 120.0) == "20 ms");
  7333. CHECK (lynx::formatEffectParam (lynx::EffectKind::vocoder, 15, slot->params[15], 120.0) == "1.00");
  7334. }
  7335. const auto parsed = lynx::fvocoder::parse (bytes);
  7336. CHECK (parsed && parsed->bands == 16 && parsed->levels[5] == 2.0f && parsed->c[lynx::fvocoder::release] == 100);
  7337. CHECK (! lynx::fvocoder::parse ({ 2, 0, 0, 0, 12, 0, 0, 0 })); // 12 bands is not a valid count
  7338. // a raw control outside the knob range (FL keeps it, REA_rodada78 §1) survives while the knob shows it, clamped
  7339. CHECK (lynx::fvocoder::rawFromKnob (0.0f, lynx::fvocoder::attack, 5) == 5);
  7340. CHECK (lynx::fvocoder::rawFromKnob (0.5f, lynx::fvocoder::attack, 5) == 505);
  7341. CHECK (lynx::fvocoder::rawFromKnob (1.0f, lynx::fvocoder::wetLevel, 250) == 250);
  7342. CHECK (lynx::fvocoder::rawFromKnob (0.5f, lynx::fvocoder::freqScale, 64) == 64);
  7343. }
  7344. // Delphi run-time math (REA_rodada70 §2): values the plugin's own exp / ln / SinCos return (EXEC, called in place)
  7345. TEST (delphi_math_matches_plugin_runtime)
  7346. {
  7347. // arguments where the C library differs from the Delphi run-time in the last bit
  7348. CHECK (lynx::dmath::exp (0x1.f6e5c5a26a7f8p+3) == 0x1.98170c3deea30p+22);
  7349. CHECK (lynx::dmath::exp (-0x1.12fc91a51d906p+4) == 0x1.27108f934a58cp-25);
  7350. CHECK (lynx::dmath::exp (-0x1.2e9e4c229e93ap+4) == 0x1.a3bf2e4ee3f1ep-28);
  7351. CHECK (lynx::dmath::ln (0x1.76a77e2a09abep-1) == -0x1.3fd111c5297f8p-2);
  7352. CHECK (lynx::dmath::ln (0x1.da1feaa8547dfp+0) == 0x1.3b8abd2756774p-1);
  7353. CHECK (lynx::dmath::ln (0x1.a1e28772a0d78p-1) == -0x1.9ffcaa3a11e30p-3);
  7354. double s = 0.0, c = 0.0;
  7355. lynx::dmath::sinCos (0x1.91b4a19e65a9bp-1, s, c);
  7356. CHECK (s == 0x1.69be278761916p-1 && c == 0x1.6a559572e1ac9p-1);
  7357. lynx::dmath::sinCos (0x1.3284a93d70a8ap-1, s, c);
  7358. CHECK (s == 0x1.2088b313ea643p-1 && c == 0x1.a6f4d889fd91ap-1);
  7359. lynx::dmath::sinCos (0x1.f9b2f53d6ce02p+0, s, c);
  7360. CHECK (s == 0x1.d6a9b8f170994p-1 && c == -0x1.931701be7b5f3p-2);
  7361. }
  7362. // Vocodex (REA_rodada35): a synthetic state in FL's layout (version 8, 13 maps, 20 parameters, the 0x24 block,
  7363. // 13 map-axis bytes, tone, notes)
  7364. struct VocodexSpec
  7365. {
  7366. // Wet, SG, ModVol, ModHP, CarVol, CarLP, CarNoise, CarNoiseHP, Hold, Att, AttCurve, Rel, RelCurve, Freeze, BW,
  7367. // Flatness, ModBW, ModPitch, FatPitch, FatPan
  7368. 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 };
  7369. std::array<std::int32_t, 7> block { 0, 2, 0, 24, 1, 1, 2 }; // hold mode, order, unison, bands, draft field, ModIn, CarIn
  7370. 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
  7371. std::vector<std::uint8_t> bytes() const
  7372. {
  7373. std::vector<std::uint8_t> d;
  7374. 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); };
  7375. auto i32 = [&] (std::int32_t v) { put (&v, 4); };
  7376. i32 (8 << 16);
  7377. for (int m = 0; m < 13; ++m)
  7378. {
  7379. const double y0 = m == 8 ? 0.1429 : (m == 3 || m == 4 || m == 11) ? 0.0 : 0.5;
  7380. const double y1 = m == 8 ? 0.9918 : y0;
  7381. i32 (1), i32 (0), i32 (2);
  7382. for (const auto& [dx, y] : { std::pair { 0.0, y0 }, std::pair { 1.0, y1 } })
  7383. {
  7384. put (&dx, 8), put (&y, 8);
  7385. i32 (0), i32 (0); // tension (f32 0), flags
  7386. }
  7387. const std::array<std::uint8_t, 20> tail {};
  7388. put (tail.data(), tail.size());
  7389. }
  7390. for (auto v : params)
  7391. i32 (v);
  7392. for (auto v : block)
  7393. i32 (v);
  7394. put (flags.data(), flags.size());
  7395. const std::array<std::uint8_t, 13> axis {};
  7396. put (axis.data(), axis.size());
  7397. d.push_back (3), put ("PWM", 3);
  7398. i32 (1), i32 (60);
  7399. return d;
  7400. }
  7401. };
  7402. std::vector<float> vocodexSaw (int n, double hz, double fs)
  7403. {
  7404. std::vector<float> out ((std::size_t) n);
  7405. for (int i = 0; i < n; ++i)
  7406. out[(std::size_t) i] = (float) (2.0 * std::fmod (i * hz / fs, 1.0) - 1.0) * 0.4f;
  7407. return out;
  7408. }
  7409. TEST (vocodex_state_parse_and_conversion)
  7410. {
  7411. VocodexSpec spec;
  7412. spec.block = { 1, 3, 2, 40, 0, 2, 1 };
  7413. spec.params[17] = -1200;
  7414. const auto raw = spec.bytes();
  7415. const auto s = lynx::vocodex::parseState (raw);
  7416. CHECK (s.has_value());
  7417. if (! s)
  7418. return;
  7419. CHECK (s->version == 8 && s->holdMode == 1 && s->order == 3 && s->unison == 2 && s->bands == 40);
  7420. CHECK (s->draft() && s->modIn == 2 && s->carIn == 1 && s->minTimes && s->subtractiveGate && ! s->lrEncoding);
  7421. CHECK (s->params[lynx::vocodex::modPitch] == -1200 && s->params[lynx::vocodex::carLp] == 65536);
  7422. 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);
  7423. CHECK (s->tone == "PWM" && s->notes.size() == 1 && s->notes[0] == 60);
  7424. const auto v = lynx::vocodexEffect (raw);
  7425. CHECK (v && v->slot.kind == lynx::EffectKind::vocodex && v->slot.state == raw && v->modIn == 2 && v->carIn == 1 && v->notes.empty());
  7426. spec.block[6] = -1; // internal synth carrier: not available yet → reported
  7427. spec.params[1] = 150; // Soundgoodizer stage
  7428. const auto w = lynx::vocodexEffect (spec.bytes());
  7429. CHECK (w && w->carIn == -1 && w->notes.size() == 2);
  7430. CHECK (! lynx::vocodex::parseState ({ 1, 2, 3 }).has_value());
  7431. }
  7432. TEST (vocodex_vocodes_side_inputs)
  7433. {
  7434. // modulator on side input 1 (noise in the first half), carrier on side input 2 (saw), own input silent: the
  7435. // output follows the modulator's envelope and fades after it stops
  7436. VocodexSpec spec;
  7437. lynx::EffectSlot slot = lynx::makeEffectSlot (lynx::EffectKind::vocodex);
  7438. slot.state = spec.bytes();
  7439. auto fx = lynx::makeEffect (slot);
  7440. fx->prepare (48000.0);
  7441. CHECK (fx->latencySamples() == 0);
  7442. const auto car = vocodexSaw (48000, 110.0, 48000.0);
  7443. std::vector<float> mod (48000), l (48000, 0.0f), r (48000, 0.0f);
  7444. std::uint32_t seed = 7;
  7445. for (std::size_t i = 0; i < mod.size(); ++i)
  7446. {
  7447. seed = seed * 1664525u + 1013904223u;
  7448. mod[i] = i < 24000 ? ((float) (seed >> 8) / 16777216.0f - 0.5f) : 0.0f;
  7449. }
  7450. for (int done = 0; done < 48000; done += 512)
  7451. {
  7452. const int n = std::min (512, 48000 - done);
  7453. fx->setSidechain (mod.data() + done, mod.data() + done);
  7454. fx->setSidechain2 (car.data() + done, car.data() + done);
  7455. fx->process (l.data() + done, r.data() + done, n, slot.params, 120.0);
  7456. }
  7457. auto rms = [&] (int from, int to) {
  7458. double e = 0.0;
  7459. for (int i = from; i < to; ++i)
  7460. e += (double) l[(std::size_t) i] * l[(std::size_t) i] + (double) r[(std::size_t) i] * r[(std::size_t) i];
  7461. return std::sqrt (e / (2.0 * (to - from)));
  7462. };
  7463. CHECK (rms (6000, 20000) > 0.02);
  7464. CHECK (rms (40000, 48000) < 0.01 * rms (6000, 20000));
  7465. CHECK (std::abs (l[0]) < 1e-6f); // the gains ramp up from 0 when the plugin is created
  7466. }
  7467. TEST (vocodex_draft_latency_and_pass_through)
  7468. {
  7469. // carrier pass-through at unit gain only (Wet 0): after a Flush the gains snap, so the output is the carrier,
  7470. // 16 samples late in Draft
  7471. VocodexSpec spec;
  7472. spec.params[0] = 0; // Wet
  7473. spec.params[4] = 12800; // CarVol = 1
  7474. for (const int draftField : { 1, 0 })
  7475. {
  7476. spec.block[4] = draftField;
  7477. const auto s = lynx::vocodex::parseState (spec.bytes());
  7478. CHECK (s.has_value());
  7479. lynx::vocodex::Engine engine;
  7480. engine.prepare (*s, 44100.0, 300, s->draft());
  7481. CHECK (engine.latencySamples() == (draftField == 0 ? 16 : 0));
  7482. engine.flush();
  7483. const auto saw = vocodexSaw (3000, 220.0, 44100.0);
  7484. std::vector<float> car (6000), mod (6000, 0.0f), out (6000);
  7485. for (int i = 0; i < 3000; ++i)
  7486. car[(std::size_t) (2 * i)] = car[(std::size_t) (2 * i + 1)] = saw[(std::size_t) i];
  7487. static constexpr int sizes[] { 300, 37, 128, 1, 255, 16, 299 }; // uneven blocks exercise the FIFOs
  7488. for (int done = 0, k = 0, n = 0; done < 3000; done += n, ++k)
  7489. {
  7490. n = std::min (sizes[k % 7], 3000 - done);
  7491. engine.process (mod.data() + 2 * done, mod.data() + 2 * done, car.data() + 2 * done, out.data() + 2 * done, n);
  7492. }
  7493. const int lag = engine.latencySamples();
  7494. auto maxError = 0.0f;
  7495. for (int i = lag; i < 3000; ++i)
  7496. maxError = std::max (maxError, std::abs (out[(std::size_t) (2 * i)] - saw[(std::size_t) (i - lag)]));
  7497. CHECK (maxError == 0.0f);
  7498. }
  7499. }
  7500. TEST (vocodex_soundgoodizer_stage_limits_the_output)
  7501. {
  7502. // SG > 0: the vocoder's output (loud here: Wet 2000) goes through the Soundgoodizer approximation, whose
  7503. // master limiter keeps the peaks under −0.2 dBFS; its look-ahead is reported as latency
  7504. VocodexSpec spec;
  7505. spec.params[0] = 2000;
  7506. spec.params[1] = 250;
  7507. lynx::EffectSlot slot = lynx::makeEffectSlot (lynx::EffectKind::vocodex);
  7508. slot.state = spec.bytes();
  7509. auto fx = lynx::makeEffect (slot);
  7510. fx->prepare (48000.0);
  7511. CHECK (fx->latencySamples() == 72); // 1.5 ms at 48 kHz
  7512. const auto car = vocodexSaw (48000, 110.0, 48000.0);
  7513. std::vector<float> mod (48000), l (48000, 0.0f), r (48000, 0.0f);
  7514. for (std::size_t i = 0; i < mod.size(); ++i)
  7515. mod[i] = 0.9f * (float) std::sin (0.07 * (double) i) * (float) std::sin (0.0003 * (double) i);
  7516. auto peak = 0.0f;
  7517. for (int done = 0; done < 48000; done += 512)
  7518. {
  7519. const int n = std::min (512, 48000 - done);
  7520. fx->setSidechain (mod.data() + done, mod.data() + done);
  7521. fx->setSidechain2 (car.data() + done, car.data() + done);
  7522. fx->process (l.data() + done, r.data() + done, n, slot.params, 120.0);
  7523. }
  7524. for (std::size_t i = 0; i < l.size(); ++i)
  7525. peak = std::max ({ peak, std::abs (l[i]), std::abs (r[i]) });
  7526. CHECK (peak > 0.5f && peak <= 0.9775f);
  7527. }
  7528. TEST (vocodex_tail_bypass_and_noise)
  7529. {
  7530. // both inputs silent for longer than 0.5 s + hold + release: the own input passes unchanged
  7531. VocodexSpec spec;
  7532. const auto s = lynx::vocodex::parseState (spec.bytes());
  7533. CHECK (s.has_value());
  7534. lynx::vocodex::Engine engine;
  7535. engine.prepare (*s, 44100.0, 512, false);
  7536. std::vector<float> zero (1024, 0.0f), own (1024), out (1024);
  7537. for (std::size_t i = 0; i < own.size(); ++i)
  7538. own[i] = 0.25f * (float) std::sin (0.01 * (double) i);
  7539. for (int b = 0; b < 80; ++b)
  7540. engine.process (own.data(), zero.data(), zero.data(), out.data(), 512);
  7541. CHECK (out == own);
  7542. // carrier noise: the IPP-compatible generator restarts from the same seed after a Flush
  7543. spec.params[6] = 9000;
  7544. const auto noisy = lynx::vocodex::parseState (spec.bytes());
  7545. lynx::vocodex::Engine a;
  7546. a.prepare (*noisy, 44100.0, 512, false);
  7547. std::vector<float> mod (1024), first (1024), second (1024);
  7548. for (std::size_t i = 0; i < mod.size(); ++i)
  7549. mod[i] = 0.3f * (float) std::sin (0.05 * (double) i);
  7550. a.flush();
  7551. a.process (own.data(), mod.data(), zero.data(), first.data(), 512);
  7552. a.flush();
  7553. a.process (own.data(), mod.data(), zero.data(), second.data(), 512);
  7554. CHECK (first == second);
  7555. auto energy = 0.0;
  7556. for (auto v : first)
  7557. energy += (double) v * v;
  7558. CHECK (energy > 1e-6 && std::isfinite (energy));
  7559. }
  7560. TEST (synth_spectral_oscillator)
  7561. {
  7562. // 1/n spectrum = saw: same harmonic proportions and same level as LYNX's saw
  7563. lynx::SynthSettings saw;
  7564. saw.osc[0] = { lynx::Waveform::saw, 0, 0.0f, 1.0f };
  7565. saw.cutoff = 20000.0f;
  7566. auto spectral = saw;
  7567. spectral.osc[0].wave = lynx::Waveform::spectral;
  7568. for (int n = 1; n <= lynx::SynthSettings::spectrumSize; ++n)
  7569. spectral.spectrum[(std::size_t) n - 1] = 1.0f / (float) n;
  7570. const auto a = renderSynthNote (saw, 57, 24000), b = renderSynthNote (spectral, 57, 24000); // 220 Hz
  7571. for (const auto h : { 1, 2, 3, 5 })
  7572. {
  7573. const auto ea = goertzel (a, 220.0 * h, 4800, 24000), eb = goertzel (b, 220.0 * h, 4800, 24000);
  7574. CHECK (std::abs (10.0 * std::log10 (eb / ea)) < 0.5); // within 0.5 dB
  7575. }
  7576. // odd only: no 2nd harmonic
  7577. auto odd = spectral;
  7578. for (int n = 2; n <= lynx::SynthSettings::spectrumSize; n += 2)
  7579. odd.spectrum[(std::size_t) n - 1] = 0.0f;
  7580. const auto c = renderSynthNote (odd, 57, 24000);
  7581. CHECK (goertzel (c, 440.0, 4800, 24000) < 1e-4 * goertzel (c, 220.0, 4800, 24000));
  7582. // high note: the band-limited tables don't pass Nyquist (no aliasing below the fundamental)
  7583. const auto d = renderSynthNote (spectral, 105, 24000); // 3520 Hz
  7584. CHECK (goertzel (d, 1000.0, 4800, 24000) < 1e-3 * goertzel (d, 3520.0, 4800, 24000));
  7585. }
  7586. TEST (compressor_sidechain_ducks_and_orders_mixer)
  7587. {
  7588. // the detector listens to the side input: quiet signal on the track, loud kick on the sidechain -> reduction
  7589. auto slot = lynx::makeEffectSlot (lynx::EffectKind::compressor);
  7590. slot.params[0] = 0.5f; // threshold
  7591. slot.params[1] = 1.0f; // high ratio
  7592. slot.params[4] = 0.0f;
  7593. auto run = [&] (bool withSidechain) {
  7594. auto fx = lynx::makeEffect (lynx::EffectKind::compressor);
  7595. fx->prepare (48000.0);
  7596. std::vector<float> l (4800, 0.1f), r (4800, 0.1f), kick (4800, 1.0f);
  7597. if (withSidechain)
  7598. fx->setSidechain (kick.data(), kick.data());
  7599. fx->process (l.data(), r.data(), 4800, slot.params, 120.0);
  7600. return l.back();
  7601. };
  7602. const auto alone = run (false), ducked = run (true);
  7603. CHECK (ducked < 0.5f * alone);
  7604. // order: track 5 listens to 7 via sidechain -> 7 comes before 5
  7605. auto song = lynx::Song::makeDefault();
  7606. while (song.mixer.size() < 8)
  7607. song.mixer.push_back ({});
  7608. auto comp = lynx::makeEffectSlot (lynx::EffectKind::compressor);
  7609. comp.sidechain = 7;
  7610. song.mixer[5].effects.push_back (comp);
  7611. const auto order = song.mixerOrder();
  7612. const auto at = [&] (int t) { return std::find (order.begin(), order.end(), t) - order.begin(); };
  7613. CHECK (at (7) < at (5));
  7614. }
  7615. TEST (pitch_correction_pulls_to_allowed_note)
  7616. {
  7617. auto run = [] (const lynx::EffectParams& p, double hz) {
  7618. auto fx = lynx::makeEffect (lynx::EffectKind::pitchCorrect);
  7619. fx->prepare (48000.0);
  7620. std::vector<float> l (48000 * 2), r;
  7621. for (std::size_t i = 0; i < l.size(); ++i)
  7622. l[i] = 0.5f * (float) std::sin (2.0 * 3.141592653589793 * hz * (double) i / 48000.0);
  7623. r = l;
  7624. fx->process (l.data(), r.data(), (int) l.size(), p, 120.0);
  7625. // frequency from zero crossings in the last second
  7626. int crossings = 0;
  7627. for (std::size_t i = 48000 + 1; i < l.size(); ++i)
  7628. crossings += (l[i - 1] < 0.0f) != (l[i] < 0.0f);
  7629. return crossings / 2.0;
  7630. };
  7631. auto slot = lynx::makeEffectSlot (lynx::EffectKind::pitchCorrect); // chromatic, instant, A 440
  7632. CHECK (std::abs (run (slot.params, 452.0) - 440.0) < 4.0);
  7633. for (int k = 0; k < 12; ++k)
  7634. slot.params[(std::size_t) (5 + k)] = k == 0 ? 1.0f : 0.0f; // only C
  7635. CHECK (std::abs (run (slot.params, 452.0) - 523.25) < 5.0); // C5 (523 Hz) is closer than C4 (262)
  7636. slot.params[2] = 1.0f; // turn everything off: passes unchanged
  7637. CHECK (std::abs (run (slot.params, 452.0) - 452.0) < 4.0);
  7638. }
  7639. TEST (pitch_correction_bypass_is_a_pure_delay)
  7640. {
  7641. // FL's Pitcher with Bypass All still runs its shifter: the plain path is an exact 9-sample delay, the formant
  7642. // path (256-Hann overlap-add) an exact 257-sample delay, and Gender changes nothing without a shift
  7643. // (REA_rodada30 §2.4/2.5); both are reported as latency
  7644. std::vector<float> in (48000);
  7645. for (std::size_t i = 0; i < in.size(); ++i)
  7646. in[i] = 0.5f * (float) std::sin (2.0 * 3.141592653589793 * 440.0 * (double) i / 48000.0)
  7647. + 0.2f * (float) std::sin (2.0 * 3.141592653589793 * 1234.5 * (double) i / 48000.0);
  7648. auto run = [&] (bool formant, float gender, int& latency) {
  7649. auto fx = lynx::makeEffect (lynx::EffectKind::pitchCorrect);
  7650. fx->prepare (48000.0);
  7651. auto slot = lynx::makeEffectSlot (lynx::EffectKind::pitchCorrect);
  7652. slot.params[2] = 1.0f;
  7653. slot.params[29] = formant ? 1.0f : 0.0f;
  7654. slot.params[30] = gender;
  7655. auto l = in, r = in;
  7656. for (std::size_t i = 0; i < l.size(); i += 480)
  7657. fx->process (l.data() + i, r.data() + i, 480, slot.params, 120.0);
  7658. latency = fx->latencySamples();
  7659. return l;
  7660. };
  7661. int latency = 0;
  7662. auto plain = run (false, 0.5f, latency);
  7663. CHECK (latency == 9);
  7664. auto worst = 0.0f;
  7665. for (std::size_t i = 4000; i < in.size(); ++i)
  7666. worst = std::max (worst, std::abs (plain[i] - in[i - 9]));
  7667. CHECK (worst < 1e-5f);
  7668. auto formant = run (true, 0.8f, latency);
  7669. CHECK (latency == 257);
  7670. worst = 0.0f;
  7671. for (std::size_t i = 4000; i < in.size(); ++i)
  7672. worst = std::max (worst, std::abs (formant[i] - in[i - 257]));
  7673. CHECK (worst < 1e-5f);
  7674. }
  7675. TEST (pitch_correction_formant_path)
  7676. {
  7677. // Formant on: PSOLA keeps the correction (output pitch = grain spacing) while Gender resamples the grains:
  7678. // the spectrum of a saw moves up with Gender > 0.5 and down with Gender < 0.5
  7679. auto run = [] (float gender) {
  7680. auto fx = lynx::makeEffect (lynx::EffectKind::pitchCorrect);
  7681. fx->prepare (48000.0);
  7682. auto slot = lynx::makeEffectSlot (lynx::EffectKind::pitchCorrect); // chromatic, instant, A 440
  7683. slot.params[29] = 1.0f;
  7684. slot.params[30] = gender;
  7685. std::vector<float> l (48000 * 2), r;
  7686. for (std::size_t i = 0; i < l.size(); ++i)
  7687. l[i] = 0.5f * (float) (2.0 * std::fmod (452.0 * (double) i / 48000.0, 1.0) - 1.0); // saw
  7688. r = l;
  7689. fx->process (l.data(), r.data(), (int) l.size(), slot.params, 120.0);
  7690. l.erase (l.begin(), l.begin() + 48000);
  7691. return l;
  7692. };
  7693. // period by autocorrelation (lags 60…160 = 300…800 Hz)
  7694. auto pitch = [] (const std::vector<float>& x) {
  7695. auto best = 0.0, bestLag = 0.0;
  7696. for (int lag = 60; lag <= 160; ++lag)
  7697. {
  7698. auto sum = 0.0;
  7699. for (std::size_t i = 0; i + (std::size_t) lag < x.size(); ++i)
  7700. sum += (double) x[i] * x[i + (std::size_t) lag];
  7701. if (sum > best)
  7702. {
  7703. best = sum;
  7704. bestLag = lag;
  7705. }
  7706. }
  7707. return 48000.0 / bestLag;
  7708. };
  7709. // brightness: energy of the first difference relative to the energy
  7710. auto brightness = [] (const std::vector<float>& x) {
  7711. auto e = 0.0, d = 0.0;
  7712. for (std::size_t i = 1; i < x.size(); ++i)
  7713. {
  7714. e += (double) x[i] * x[i];
  7715. d += (double) (x[i] - x[i - 1]) * (x[i] - x[i - 1]);
  7716. }
  7717. return d / e;
  7718. };
  7719. const auto neutral = run (0.5f), up = run (0.8f), down = run (0.3f);
  7720. CHECK (std::abs (pitch (neutral) - 440.0) < 5.0); // 452 Hz pulled to A
  7721. CHECK (std::abs (pitch (up) - 440.0) < 5.0); // Gender does not move the pitch
  7722. CHECK (brightness (up) > 1.5 * brightness (neutral));
  7723. CHECK (brightness (down) < 0.7 * brightness (neutral));
  7724. }
  7725. TEST (parallel_container_inner_links_modulate)
  7726. {
  7727. // one branch: utility (gain) + a control-only auto-pan LFO (1 beat); the link maps the LFO 0..1 onto the
  7728. // utility gain −24 dB..0 dB, so the level must swing; without the link it stays put
  7729. auto container = lynx::makeEffectSlot (lynx::EffectKind::parallel);
  7730. auto gain = lynx::makeEffectSlot (lynx::EffectKind::utility);
  7731. auto lfo = lynx::makeEffectSlot (lynx::EffectKind::autoPan);
  7732. lfo.params[0] = 1.0f / 64.0f;
  7733. lfo.params[4] = 1.0f; // control only: audio passes unchanged
  7734. container.branches.push_back ({ { gain, lfo }, 1.0f });
  7735. lynx::EffectLink link;
  7736. link.source = 1;
  7737. link.param = 0;
  7738. for (int i = 0; i <= 32; ++i)
  7739. link.curve.push_back (0.5f * (float) i / 32.0f);
  7740. auto levels = [&] (bool linked) {
  7741. auto slot = container;
  7742. if (linked)
  7743. slot.links.push_back (link);
  7744. auto fx = lynx::makeParallelEffect (slot);
  7745. fx->prepare (48000.0);
  7746. float lo = 1e9f, hi = 0.0f;
  7747. for (int block = 0; block < 94; ++block) // ~1 beat at 120 BPM in 256-sample blocks
  7748. {
  7749. std::vector<float> l (256, 1.0f), r (256, 1.0f);
  7750. fx->setSongPosition (block * 256.0 / 48000.0 * 2.0 * 960.0); // the engine positions every block
  7751. fx->process (l.data(), r.data(), 256, slot.params, 120.0);
  7752. lo = std::min (lo, std::abs (l[128]));
  7753. hi = std::max (hi, std::abs (l[128]));
  7754. }
  7755. return std::pair { lo, hi };
  7756. };
  7757. const auto [lo, hi] = levels (true);
  7758. CHECK (hi > 0.8f && lo < 0.12f);
  7759. const auto [lo0, hi0] = levels (false);
  7760. CHECK (std::abs (hi0 - lo0) < 1e-4f);
  7761. }
  7762. TEST (synth_spectrum_decays_per_harmonic)
  7763. {
  7764. // 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
  7765. lynx::SynthSettings s;
  7766. s.osc[0] = { lynx::Waveform::spectral, 0, 0.0f, 1.0f };
  7767. s.cutoff = 20000.0f;
  7768. s.decay = 0.0f;
  7769. s.sustain = 1.0f;
  7770. s.spectrum[0] = 1.0f;
  7771. s.spectrum[2] = 1.0f;
  7772. s.spectrumDecay[2] = 0.2f;
  7773. const auto x = renderSynthNote (s, 57, 48000); // 220 Hz, 48 kHz, 1 s
  7774. // equal 2400-sample windows (Goertzel power grows with the window): 10…60 ms and 500…550 ms
  7775. const auto early1 = goertzel (x, 220.0, 480, 2880), early3 = goertzel (x, 660.0, 480, 2880);
  7776. const auto late1 = goertzel (x, 220.0, 24000, 26400), late3 = goertzel (x, 660.0, 24000, 26400);
  7777. CHECK (early3 > 0.05 * early1); // ≈ −9 dB on average over 10…60 ms
  7778. CHECK (late3 < 1e-4 * late1);
  7779. CHECK (std::abs (10.0 * std::log10 (late1 / early1)) < 1.0); // the 1st holds
  7780. // without per-harmonic decay the spectrum stays as it is
  7781. s.spectrumDecay[2] = 0.0f;
  7782. const auto y = renderSynthNote (s, 57, 48000);
  7783. CHECK (std::abs (10.0 * std::log10 (goertzel (y, 660.0, 24000, 26400) / goertzel (y, 660.0, 480, 2880))) < 1.0);
  7784. }
  7785. TEST (worker_pool_runs_each_index_once_per_round)
  7786. {
  7787. // rounds of alternating sizes (a stalled worker must never run an index of a later round), each index
  7788. // exactly once, and nothing running after parallelFor returns
  7789. lynx::WorkerPool pool (3);
  7790. std::vector<std::atomic<int>> hits (64);
  7791. std::atomic<int> round { 0 }, outside { 0 }, wrongRound { 0 };
  7792. std::atomic<bool> open { false };
  7793. int failures = 0;
  7794. for (int r = 1; r <= 20000; ++r)
  7795. {
  7796. const auto n = (r % 3 == 0) ? 64 : (r % 3 == 1 ? 2 : 9);
  7797. for (int i = 0; i < n; ++i)
  7798. hits[(std::size_t) i].store (0);
  7799. round.store (r);
  7800. open.store (true);
  7801. auto fn = [&, r] (int i) {
  7802. if (! open.load())
  7803. outside.fetch_add (1);
  7804. if (round.load() != r)
  7805. wrongRound.fetch_add (1);
  7806. hits[(std::size_t) i].fetch_add (1);
  7807. };
  7808. pool.parallelFor (n, fn);
  7809. open.store (false);
  7810. for (int i = 0; i < n; ++i)
  7811. failures += hits[(std::size_t) i].load() != 1;
  7812. }
  7813. CHECK (failures == 0);
  7814. CHECK (outside.load() == 0);
  7815. CHECK (wrongRound.load() == 0);
  7816. }
  7817. TEST (worker_pool_wakes_sleeping_workers)
  7818. {
  7819. // rounds far apart (the workers have gone to sleep on the semaphore in between): every index still runs once,
  7820. // and the sleeping workers are woken (REA round 87: the Windows build used to poll with growing Sleep()s)
  7821. lynx::WorkerPool pool (3);
  7822. std::vector<std::atomic<int>> hits (16);
  7823. int failures = 0;
  7824. for (int r = 0; r < 12; ++r)
  7825. {
  7826. std::this_thread::sleep_for (std::chrono::microseconds (lynx::WorkerPool::spinMicros * 4));
  7827. for (auto& h : hits)
  7828. h.store (0);
  7829. auto fn = [&] (int i) {
  7830. std::this_thread::sleep_for (std::chrono::microseconds (200)); // long enough for woken workers to join
  7831. hits[(std::size_t) i].fetch_add (1);
  7832. };
  7833. pool.parallelFor (16, fn);
  7834. for (auto& h : hits)
  7835. failures += h.load() != 1;
  7836. }
  7837. CHECK (failures == 0);
  7838. CHECK (pool.wakeUps() > 0);
  7839. CHECK (pool.indicesRunByWorkers() > 0);
  7840. }
  7841. TEST (worker_pool_park_sends_spinning_workers_to_sleep)
  7842. {
  7843. // round 116: after park() (end of an audio block) the workers sleep at once instead of spinning for spinMicros, so
  7844. // the next parallelFor, well inside the spin window, has to wake them; and work right after park() still runs once
  7845. lynx::WorkerPool pool (3);
  7846. std::vector<std::atomic<int>> hits (8);
  7847. int failures = 0;
  7848. const auto before = pool.wakeUps();
  7849. for (int r = 0; r < 40; ++r)
  7850. {
  7851. for (auto& h : hits)
  7852. h.store (0);
  7853. auto fn = [&] (int i) {
  7854. std::this_thread::sleep_for (std::chrono::microseconds (100));
  7855. hits[(std::size_t) i].fetch_add (1);
  7856. };
  7857. pool.parallelFor (8, fn);
  7858. pool.park();
  7859. if (r % 2 == 0)
  7860. std::this_thread::sleep_for (std::chrono::microseconds (lynx::WorkerPool::spinMicros / 5));
  7861. for (auto& h : hits)
  7862. failures += h.load() != 1;
  7863. }
  7864. CHECK (failures == 0);
  7865. CHECK (pool.wakeUps() - before >= 20); // parked workers were woken, not found spinning
  7866. }
  7867. TEST (realtime_stats_count_deadline_misses_and_late_calls)
  7868. {
  7869. lynx::RtStats stats;
  7870. // 10 ms deadline: 3 ms, 9 ms (80-100 %), 12 ms (over), then a call 40 ms after the previous one (late)
  7871. stats.record (100.0, 3.0, 10.0);
  7872. stats.record (110.0, 9.0, 10.0);
  7873. stats.record (120.0, 12.0, 10.0);
  7874. stats.record (160.0, 2.0, 10.0);
  7875. const auto s = stats.snapshot();
  7876. CHECK (s.callbacks == 4);
  7877. CHECK (s.overBudget == 1);
  7878. CHECK (s.nearBudget == 1);
  7879. CHECK (s.lateCalls == 1);
  7880. CHECK (std::abs (s.maxLoad - 1.2) < 1e-9);
  7881. CHECK (std::abs (s.averageLoad - 26.0 / 40.0) < 1e-3);
  7882. CHECK (s.histogram[3] == 1 && s.histogram[9] == 1 && s.histogram[12] == 1 && s.histogram[2] == 1);
  7883. CHECK (std::abs (stats.takeWindowMaxLoad() - 1.2) < 1e-9 && stats.takeWindowMaxLoad() == 0.0);
  7884. // a device restart is not a late call
  7885. stats.deviceRestarted();
  7886. stats.record (5000.0, 1.0, 10.0);
  7887. CHECK (stats.snapshot().lateCalls == 1);
  7888. stats.reset();
  7889. CHECK (stats.snapshot().callbacks == 0 && stats.snapshot().maxLoad == 0.0);
  7890. }
  7891. TEST (safe_default_buffer_size)
  7892. {
  7893. const std::vector<int> wasapi { 144, 160, 192, 224, 256, 288, 320, 384, 448, 480, 512, 576, 640, 1024, 2048 };
  7894. CHECK (lynx::prefs::safeBufferSize (wasapi, 480, 48000.0) == 512); // the 10 ms default becomes 512
  7895. CHECK (lynx::prefs::safeBufferSize (wasapi, 1024, 48000.0) == 1024); // already larger: kept
  7896. CHECK (lynx::prefs::safeBufferSize (wasapi, 480, 96000.0) == 1024); // ~10.7 ms at 96 kHz
  7897. CHECK (lynx::prefs::safeBufferSize ({ 64, 128, 256 }, 128, 44100.0) == 256); // nothing reaches 512: the largest
  7898. CHECK (lynx::prefs::safeBufferSize ({}, 0, 44100.0) == 0);
  7899. }
  7900. TEST (linux_audio_driver_choice_and_pipewire_helpers)
  7901. {
  7902. // round 161: a first start on Linux opens PipeWire, then JACK, then ALSA
  7903. using lynx::prefs::DriverKind;
  7904. CHECK (lynx::prefs::driverKind ("PipeWire") == DriverKind::pipewire);
  7905. CHECK (lynx::prefs::preferredLinuxDriver ({ { "ALSA", true }, { "JACK", false }, { "PipeWire", true } }) == "PipeWire");
  7906. CHECK (lynx::prefs::preferredLinuxDriver ({ { "ALSA", true }, { "JACK", true } }) == "JACK");
  7907. CHECK (lynx::prefs::preferredLinuxDriver ({ { "ALSA", true }, { "JACK", false }, { "PipeWire", false } }) == "ALSA");
  7908. CHECK (lynx::prefs::preferredLinuxDriver ({ { "ALSA", false }, { "Bela", true } }) == "Bela");
  7909. CHECK (lynx::prefs::preferredLinuxDriver ({ { "ALSA", false } }).empty());
  7910. CHECK (lynx::prefs::pipewireLatency (256, 48000) == "256/48000");
  7911. CHECK (lynx::prefs::pipewireLatency (0, 0) == "1/1");
  7912. CHECK (lynx::prefs::pipewireChannelName ("FL", 0) == "Left");
  7913. CHECK (lynx::prefs::pipewireChannelName ("FR", 1) == "Right");
  7914. CHECK (lynx::prefs::pipewireChannelName ("MONO", 0) == "Mono");
  7915. CHECK (lynx::prefs::pipewireChannelName ("AUX0", 0) == "Aux 1");
  7916. CHECK (lynx::prefs::pipewireChannelName ("AUX15", 15) == "Aux 16");
  7917. CHECK (lynx::prefs::pipewireChannelName ("AUXX", 2) == "AUXX");
  7918. CHECK (lynx::prefs::pipewireChannelName ("", 2) == "Channel 3");
  7919. CHECK (lynx::prefs::pipewireDefaultName (R"({"name":"alsa_output.pci-0000_00_1f.3.analog-stereo"})") == "alsa_output.pci-0000_00_1f.3.analog-stereo");
  7920. CHECK (lynx::prefs::pipewireDefaultName (R"({ "name" : "a\"b" })") == "a\"b");
  7921. CHECK (lynx::prefs::pipewireDefaultName ("{}").empty());
  7922. CHECK (lynx::prefs::pipewireDefaultName (R"({"name":"unterminated)").empty());
  7923. CHECK ((lynx::prefs::uniqueDeviceNames ({ "USB Audio", "HDMI", "USB Audio", "USB Audio" })
  7924. == std::vector<std::string> { "USB Audio", "HDMI", "USB Audio (2)", "USB Audio (3)" }));
  7925. }
  7926. TEST (driver_kind_and_shared_mode_buffer_sizes)
  7927. {
  7928. // REA round 97: classic shared WASAPI mixes in whole engine periods, so sizes below it are not offered
  7929. using lynx::prefs::DriverKind;
  7930. CHECK (lynx::prefs::driverKind ("Windows Audio") == DriverKind::wasapiShared);
  7931. CHECK (lynx::prefs::driverKind ("Windows Audio (Exclusive Mode)") == DriverKind::wasapiExclusive);
  7932. CHECK (lynx::prefs::driverKind ("Windows Audio (Low Latency Mode)") == DriverKind::wasapiLowLatency);
  7933. CHECK (lynx::prefs::driverKind ("ASIO") == DriverKind::asio);
  7934. CHECK (lynx::prefs::driverKind ("LYNX ASIO") == DriverKind::lynxAsio);
  7935. CHECK (lynx::prefs::driverKind ("DirectSound") == DriverKind::directSound);
  7936. CHECK (lynx::prefs::driverKind ("ALSA") == DriverKind::alsa);
  7937. CHECK (lynx::prefs::driverKind ("JACK") == DriverKind::jack);
  7938. CHECK (lynx::prefs::driverKind ("CoreAudio") == DriverKind::coreAudio);
  7939. CHECK (lynx::prefs::driverKind ("Some Other Driver") == DriverKind::other);
  7940. const std::vector<int> offered { 144, 192, 256, 480, 512, 1024 };
  7941. CHECK ((lynx::prefs::usableBufferSizes (DriverKind::wasapiShared, offered, 480) == std::vector<int> { 480, 512, 1024 }));
  7942. CHECK (lynx::prefs::usableBufferSizes (DriverKind::wasapiExclusive, offered, 480) == offered); // the device's real periods
  7943. CHECK (lynx::prefs::usableBufferSizes (DriverKind::wasapiLowLatency, offered, 480) == offered); // engine periods
  7944. CHECK (lynx::prefs::usableBufferSizes (DriverKind::wasapiShared, { 64, 128 }, 480) == (std::vector<int> { 64, 128 })); // never empty
  7945. CHECK (lynx::prefs::usableBufferSizes (DriverKind::wasapiShared, offered, 0) == offered);
  7946. }
  7947. TEST (linear_phase_convolver_matches_direct_convolution)
  7948. {
  7949. // the block FFT convolver (cached twiddles, REA round 87) equals the direct convolution with its kernel
  7950. lynx::peqfl::FftConvolver c;
  7951. c.init (256);
  7952. std::vector<float> table (129, 1.0f);
  7953. for (int k = 40; k < 129; ++k)
  7954. table[(std::size_t) k] = 0.25f; // a high shelf cut
  7955. c.setKernel (table);
  7956. // impulse response through step()
  7957. std::vector<float> ir (600, 0.0f);
  7958. for (int i = 0; i < 600; ++i)
  7959. {
  7960. float l = i == 0 ? 1.0f : 0.0f, r = 0.0f;
  7961. c.step (l, r);
  7962. ir[(std::size_t) i] = l;
  7963. }
  7964. // then a noise signal, compared with direct convolution by that impulse response
  7965. lynx::peqfl::FftConvolver d;
  7966. d.init (256);
  7967. d.setKernel (table);
  7968. std::vector<float> x (2000), y (2000);
  7969. std::uint32_t seed = 1;
  7970. for (auto& v : x)
  7971. v = (float) ((seed = seed * 1664525u + 1013904223u) >> 8) / 16777216.0f - 0.5f;
  7972. for (std::size_t i = 0; i < x.size(); ++i)
  7973. {
  7974. float l = x[i], r = 0.0f;
  7975. d.step (l, r);
  7976. y[i] = l;
  7977. }
  7978. double worst = 0.0;
  7979. for (std::size_t i = 0; i < x.size(); ++i)
  7980. {
  7981. double direct = 0.0;
  7982. for (std::size_t k = 0; k < ir.size() && k <= i; ++k)
  7983. direct += (double) ir[k] * (double) x[i - k];
  7984. worst = std::max (worst, std::abs (direct - (double) y[i]));
  7985. }
  7986. CHECK (worst < 1e-4);
  7987. // and it delays by 3n/4 with the kernel's centre there
  7988. const auto peak = (std::size_t) (std::max_element (ir.begin(), ir.end(), [] (float a, float b) { return std::abs (a) < std::abs (b); }) - ir.begin());
  7989. CHECK (peak == 192);
  7990. }
  7991. TEST (patcher_container_is_reused_across_snapshots)
  7992. {
  7993. // an unchanged Patcher keeps its built effect (its children's tails, delay lines and PDC stay put), and a
  7994. // linear-phase Frequency Splitter reports its latency before its first block (REA round 87: it used to be
  7995. // rebuilt on every snapshot and its latency appeared one block late, re-planning PDC in a loop)
  7996. auto song = lynx::Song::makeDefault();
  7997. if (song.mixer.size() < 2)
  7998. song.mixer.resize (2);
  7999. lynx::EffectSlot splitter;
  8000. splitter.kind = lynx::EffectKind::frequencySplitter;
  8001. splitter.params = lynx::makeEffectSlot (lynx::EffectKind::frequencySplitter).params;
  8002. splitter.params[4] = 0.6f; // linear-phase mode
  8003. splitter.params[6] = 0.0f; // quality 0
  8004. lynx::EffectSlot patcher;
  8005. patcher.kind = lynx::EffectKind::parallel;
  8006. patcher.branches.push_back ({ { splitter }, 1.0f });
  8007. patcher.branches.push_back ({ {}, 0.5f });
  8008. song.mixer[1].effects = { patcher };
  8009. const auto a = lynx::RackState::build (song, nullptr, 44100.0);
  8010. const auto b = lynx::RackState::build (song, a.get(), 44100.0);
  8011. CHECK (a->effects[1][0] == b->effects[1][0]);
  8012. CHECK (a->effects[1][0]->latencySamples() == 384); // 3/4 of the 512-point FFT, before any block
  8013. CHECK (a->latencies == a->currentLatencies());
  8014. song.mixer[1].effects[0].branches[0].chain[0].params[0] = 0.25f; // a child's parameter changed: rebuilt
  8015. const auto c = lynx::RackState::build (song, b.get(), 44100.0);
  8016. CHECK (c->effects[1][0] != b->effects[1][0]);
  8017. song.mixer[1].effects[0].branches[1].gain = 0.75f;
  8018. const auto d = lynx::RackState::build (song, c.get(), 44100.0);
  8019. CHECK (d->effects[1][0] != c->effects[1][0]);
  8020. }
  8021. TEST (ladder_filter_follows_toxic_law)
  8022. {
  8023. // the 4-pole ladder: −24 dB/oct far above the cutoff (vs −12 for the SVF), resonance raises the corner, and
  8024. // the input saturation keeps it bounded even at full resonance
  8025. auto level = [] (bool ladder, float res, double hz) {
  8026. lynx::SynthSettings s;
  8027. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  8028. s.cutoff = 500.0f;
  8029. s.resonance = res;
  8030. s.ladder = ladder;
  8031. const auto key = (int) std::lround (69.0 + 12.0 * std::log2 (hz / 440.0));
  8032. const auto x = renderSynthNote (s, key, 48000);
  8033. double e = 0.0, peak = 0.0;
  8034. for (std::size_t i = 24000; i < 48000; ++i)
  8035. e += (double) x[i] * x[i], peak = std::max (peak, (double) std::abs (x[i]));
  8036. return std::pair { std::sqrt (e / 24000.0), peak };
  8037. };
  8038. 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
  8039. const auto svfHigh = level (false, 0.0f, 3520.0).first / level (false, 0.0f, 110.0).first;
  8040. CHECK (20.0 * std::log10 (high / low) < -55.0); // ≈ 24 dB/oct over ~2.8 octaves
  8041. CHECK (20.0 * std::log10 (svfHigh) > -45.0); // the 2-pole SVF falls much less
  8042. const auto corner0 = level (true, 0.0f, 523.25).first, corner1 = level (true, 0.6f, 523.25).first;
  8043. CHECK (corner1 > 1.5 * corner0); // resonance boosts near the cutoff
  8044. const auto loud = level (true, 1.0f, 523.25);
  8045. CHECK (std::isfinite (loud.first) && loud.second < 20.0); // self-oscillation stays bounded
  8046. }
  8047. TEST (synth_spectrum_settles_on_target)
  8048. {
  8049. // ALT DECAY-like morph: harmonic 3 starts at 1 and settles on 0.25 (−12 dB) instead of fading out
  8050. lynx::SynthSettings s;
  8051. s.osc[0] = { lynx::Waveform::spectral, 0, 0.0f, 1.0f };
  8052. s.cutoff = 20000.0f;
  8053. s.decay = 0.0f;
  8054. s.sustain = 1.0f;
  8055. s.spectrum[0] = 1.0f;
  8056. s.spectrum[2] = 1.0f;
  8057. s.spectrumDecay[2] = 0.1f;
  8058. s.spectrumTarget[2] = 0.25f;
  8059. const auto x = renderSynthNote (s, 57, 48000);
  8060. const auto ratio = goertzel (x, 660.0, 24000, 26400) / goertzel (x, 220.0, 24000, 26400);
  8061. CHECK (std::abs (10.0 * std::log10 (ratio) - 20.0 * std::log10 (0.25)) < 1.0);
  8062. }
  8063. TEST (kepler_chorus_is_stereo_and_keeps_level)
  8064. {
  8065. // chorus I on a sine: L and R differ (opposite LFO phases), the level stays close to the dry one
  8066. lynx::SynthSettings s;
  8067. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  8068. s.cutoff = 20000.0f;
  8069. auto dry = s;
  8070. s.chorus = 1;
  8071. auto render = [] (const lynx::SynthSettings& set) {
  8072. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  8073. inst->prepare (48000.0);
  8074. lynx::ChannelDef c;
  8075. c.synth = set;
  8076. inst->configure (c, 120.0);
  8077. inst->noteOn (69, 1.0f);
  8078. std::vector<float> l (96000), r (96000);
  8079. inst->render (l.data(), r.data(), 96000, 1.0f, 1.0f);
  8080. return std::pair { l, r };
  8081. };
  8082. const auto [l, r] = render (s);
  8083. const auto [dl, dr] = render (dry);
  8084. double diff = 0.0, e = 0.0, ed = 0.0;
  8085. for (std::size_t i = 48000; i < 96000; ++i)
  8086. diff += std::abs (l[i] - r[i]), e += (double) l[i] * l[i], ed += (double) dl[i] * dl[i];
  8087. CHECK (diff / 48000.0 > 1e-3);
  8088. CHECK (std::abs (10.0 * std::log10 (e / ed)) < 3.0);
  8089. }
  8090. TEST (fl_pan_law_matches_love_philter_dll)
  8091. {
  8092. // values printed by the installation's Love Philter pan routine for vol 0.8 (REA_rodada27 §3.1, lppan.txt)
  8093. struct Row { double pan, ll, rr, rl, lr; };
  8094. const Row rows[] = { { -1.5, 0.239782482, -0.432956904, 0.805467963, 0.0 }, { -0.5, 0.805467963, 0.432956904, 0.239782482, 0.0 },
  8095. { 0.0, 0.8, 0.8, 0.0, 0.0 }, { 0.75, 0.220719516, 0.707889318, 0.0, 0.401742548 },
  8096. { 1.0, 0.0, 0.565685451, 0.0, 0.565685451 } };
  8097. for (const auto& row : rows)
  8098. {
  8099. float ll, rl, lr, rr;
  8100. lynx::flPanMatrix (row.pan, ll, rl, lr, rr);
  8101. CHECK (std::abs (0.8 * ll - row.ll) < 1e-6 && std::abs (0.8 * rr - row.rr) < 1e-6);
  8102. CHECK (std::abs (0.8 * rl - row.rl) < 1e-6 && std::abs (0.8 * lr - row.lr) < 1e-6);
  8103. }
  8104. }
  8105. TEST (effector_units_match_fl_dll_impulse_responses)
  8106. {
  8107. // impulse responses of the installation's Effector engine (REA_rodada27 §2, eu_<kind>_<X>_<Y>.txt; LFO off,
  8108. // wet 1, blocks of 256): samples 1, 10, 100 and 1000
  8109. struct Case { int kind; float x, y; double v[4]; };
  8110. const Case cases[] = { { 3, 0.5f, 0.5f, { -0.603055418, 0.159798339, -0.0055043716, -1.06241259e-06 } },
  8111. { 4, 0.2f, 0.5f, { 4.68885949e-26, 4.20032622e-19, 0.000487731799, 2.27715846e-09 } },
  8112. { 4, 0.8f, 0.3f, { -0.293783396, -0.0306414738, -0.00068128563, 1.358577e-15 } } };
  8113. for (const auto& c : cases)
  8114. {
  8115. auto fx = lynx::effector::make();
  8116. fx->prepare (44100.0);
  8117. lynx::EffectParams p {};
  8118. p[lynx::effector::type] = c.kind / 11.0f;
  8119. p[lynx::effector::wetMix] = 1.0f;
  8120. p[lynx::effector::paramX] = c.x;
  8121. p[lynx::effector::paramY] = c.y;
  8122. p[lynx::effector::modX] = p[lynx::effector::modY] = 0.5f;
  8123. p[lynx::effector::modRate] = 0.5f;
  8124. p[lynx::effector::inputLevel] = 1.0f;
  8125. p[lynx::effector::outputGain] = 0.5f;
  8126. std::vector<float> l (1024, 0.0f), r (1024, 0.0f);
  8127. l[0] = r[0] = 1.0f;
  8128. for (int d = 0; d < 1024; d += 256)
  8129. fx->process (l.data() + d, r.data() + d, 256, p, 120.0);
  8130. const int at[4] { 1, 10, 100, 1000 };
  8131. for (int k = 0; k < 4; ++k)
  8132. CHECK (std::abs (l[(std::size_t) at[k]] - c.v[k]) < 2e-7 + 1e-5 * std::abs (c.v[k]));
  8133. }
  8134. }
  8135. TEST (harmless_stereo_tremolo_and_vibrato)
  8136. {
  8137. // tremolo d = 1: each side swings between 0 and 1.25 at 5 Hz, L and R in anti-phase; vibrato modulates the pitch
  8138. lynx::SynthSettings s;
  8139. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  8140. s.cutoff = 20000.0f;
  8141. s.stereoTremolo = 1.0f;
  8142. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  8143. inst->prepare (48000.0);
  8144. lynx::ChannelDef c;
  8145. c.synth = s;
  8146. inst->configure (c, 120.0);
  8147. inst->noteOn (69, 1.0f);
  8148. std::vector<float> l (48000), r (48000);
  8149. inst->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
  8150. 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; };
  8151. // θ = 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
  8152. CHECK (envelope (l, 19200) > 4.0f * envelope (r, 19200));
  8153. CHECK (envelope (r, 24000) > 4.0f * envelope (l, 24000));
  8154. // vibrato 100 cents: zero crossings vary in spacing; without it they are regular
  8155. auto crossingsSpread = [] (float cents) {
  8156. lynx::SynthSettings v;
  8157. v.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  8158. v.cutoff = 20000.0f;
  8159. v.vibratoCents = cents;
  8160. const auto x = renderSynthNote (v, 69, 48000);
  8161. std::vector<int> zc;
  8162. for (int i = 24000; i < 48000; ++i)
  8163. if (x[(std::size_t) i - 1] < 0.0f && x[(std::size_t) i] >= 0.0f)
  8164. zc.push_back (i);
  8165. int lo = 1 << 30, hi = 0;
  8166. for (std::size_t k = 1; k < zc.size(); ++k)
  8167. lo = std::min (lo, zc[k] - zc[k - 1]), hi = std::max (hi, zc[k] - zc[k - 1]);
  8168. return hi - lo;
  8169. };
  8170. CHECK (crossingsSpread (0.0f) <= 1);
  8171. CHECK (crossingsSpread (100.0f) >= 4);
  8172. }
  8173. TEST (physical_models_sound_and_end)
  8174. {
  8175. // Sakura: a plucked string with default-ish parameters rings and fades after the release; Drumpad: a hit decays
  8176. for (const auto kind : { lynx::InstrumentKind::sakura, lynx::InstrumentKind::drumpad })
  8177. {
  8178. lynx::ChannelDef c;
  8179. c.instrument = kind;
  8180. if (kind == lynx::InstrumentKind::sakura)
  8181. {
  8182. c.modelParams.assign (146, 0.5f);
  8183. c.modelParams[111] = 3.0f; // both strings at the note
  8184. c.modelParams[113] = 0.5f; // octave ×1
  8185. c.modelParams[6] = c.modelParams[11] = 0.99f; // long decay
  8186. c.modelParams[31] = 0.0f; // noise exciter
  8187. }
  8188. else
  8189. {
  8190. c.modelParams.assign (38, 0.5f);
  8191. c.modelParams[0] = 1.0f; // retrigger noise
  8192. c.modelParams[30] = c.modelParams[31] = c.modelParams[32] = c.modelParams[33] = 0.0f; // no velocity morph
  8193. }
  8194. auto inst = lynx::makeInstrument (kind);
  8195. inst->prepare (48000.0);
  8196. inst->configure (c, 120.0);
  8197. inst->noteOn (60, 0.8f);
  8198. std::vector<float> l (48000), r (48000);
  8199. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  8200. inst->noteOff (60);
  8201. inst->render (l.data() + 24000, r.data() + 24000, 24000, 1.0f, 1.0f);
  8202. double early = 0.0;
  8203. for (int i = 0; i < 12000; ++i)
  8204. early += (double) l[(std::size_t) i] * l[(std::size_t) i];
  8205. CHECK (early > 1e-6);
  8206. CHECK (std::all_of (l.begin(), l.end(), [] (float v) { return std::isfinite (v); }));
  8207. std::vector<float> tail (480000), tailR (480000);
  8208. inst->render (tail.data(), tailR.data(), 480000, 1.0f, 1.0f); // 10 s later the voice has ended
  8209. CHECK (inst->idle());
  8210. }
  8211. }
  8212. TEST (squeeze_matches_fl_model)
  8213. {
  8214. // Fruity Squeeze: Steps quantises, Mix 0 passes the input dry ×Volume/127; a silent block is left untouched
  8215. auto slot = lynx::makeEffectSlot (lynx::EffectKind::squeeze);
  8216. slot.params[1] = 0.0f; // mix 0: dry only
  8217. auto fx = lynx::makeEffect (lynx::EffectKind::squeeze);
  8218. fx->prepare (44100.0);
  8219. std::vector<float> l (512, 0.3f), r (512, -0.2f);
  8220. fx->process (l.data(), r.data(), 512, slot.params, 120.0);
  8221. CHECK (std::abs (l[100] - 0.3f) < 1e-6f && std::abs (r[100] + 0.2f) < 1e-6f); // Volume 127/127
  8222. slot.params[1] = 1.0f; // wet only
  8223. slot.params[2] = 32000.0f / 32768.0f; // 32768^(1 − 32000/32768) ≈ 1.27 levels per unit → heavy quantisation
  8224. std::vector<float> a (512), b (512);
  8225. for (int i = 0; i < 512; ++i)
  8226. a[(std::size_t) i] = b[(std::size_t) i] = 0.4f * (float) std::sin (i * 0.05);
  8227. fx->process (a.data(), b.data(), 512, slot.params, 120.0);
  8228. const auto levels = std::pow (32768.0f, 1.0f - 32000.0f / 32768.0f);
  8229. for (int i = 0; i < 512; i += 37)
  8230. CHECK (std::abs (a[(std::size_t) i] * levels - std::nearbyint (a[(std::size_t) i] * levels)) < 1e-3f);
  8231. std::vector<float> z (512, 0.0f), z2 (512, 0.0f);
  8232. fx->process (z.data(), z2.data(), 512, slot.params, 120.0);
  8233. CHECK (z[10] == 0.0f);
  8234. }
  8235. TEST (sakura_matrix_and_effects)
  8236. {
  8237. // REA_rodada29 §1: velocity → level follows 10^(2m)·0.05 (vel 1 vs 0.5 → ×10 in amplitude); the delay leaves
  8238. // an echo after the note has ended and keeps the instrument from going idle until its tail is silent
  8239. auto base = [] {
  8240. lynx::ChannelDef c;
  8241. c.instrument = lynx::InstrumentKind::sakura;
  8242. c.modelParams.assign (146, 0.0f);
  8243. const std::pair<int, float> values[] { { 0, 0.375f }, { 1, 0.5f }, { 3, 0.5f }, { 4, 1.0f }, { 6, 0.95f }, { 9, 0.3f },
  8244. { 11, 0.95f }, { 14, 0.3f }, { 18, 0.5f }, { 19, 0.5f }, { 27, 0.5f }, { 28, 1.0f },
  8245. { 29, 0.7f }, { 31, 1.0f }, { 32, 0.5f }, { 111, 3.0f }, { 113, 0.5f } };
  8246. for (const auto& [i, v] : values)
  8247. c.modelParams[(std::size_t) i] = v;
  8248. return c;
  8249. };
  8250. auto render = [] (const lynx::ChannelDef& c, float velocity, int held, int total) {
  8251. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sakura);
  8252. inst->prepare (48000.0);
  8253. inst->configure (c, 120.0);
  8254. inst->noteOn (60, velocity);
  8255. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  8256. for (int s = 0; s < total; s += 256)
  8257. {
  8258. if (s == held)
  8259. inst->noteOff (60);
  8260. inst->render (l.data() + s, r.data() + s, std::min (256, total - s), 1.0f, 1.0f);
  8261. }
  8262. return std::make_pair (l, std::move (inst));
  8263. };
  8264. auto energy = [] (const std::vector<float>& x, int from, int to) {
  8265. double e = 0.0;
  8266. for (int i = from; i < to; ++i)
  8267. e += (double) x[(std::size_t) i] * x[(std::size_t) i];
  8268. return e;
  8269. };
  8270. auto c = base();
  8271. c.modelParams[79] = 6.0f; // slot 1: velocity → level, amount 1
  8272. c.modelParams[80] = 1.0f;
  8273. c.modelParams[82] = 1.0f;
  8274. const auto loud = energy (render (c, 1.0f, 24064, 24064).first, 0, 24064);
  8275. const auto soft = energy (render (c, 0.5f, 24064, 24064).first, 0, 24064);
  8276. CHECK (loud > 1e-6 && std::abs (loud / soft - 100.0) < 1.0);
  8277. auto d = base();
  8278. auto [dry, dryInst] = render (d, 0.8f, 4864, 72000);
  8279. d.modelParams[63] = 1.0f; // delay on: time 0.5 → ≈ 0.52 s, wet 1, no feedback: one echo
  8280. d.modelParams[64] = 0.5f;
  8281. d.modelParams[69] = 1.0f;
  8282. auto [wet, wetInst] = render (d, 0.8f, 4864, 72000);
  8283. CHECK (energy (wet, 26000, 40000) > 100.0 * energy (dry, 26000, 40000) + 1e-9); // the note ends near 0.2 s
  8284. CHECK (dryInst->idle() || energy (dry, 70000, 72000) < 1e-12);
  8285. CHECK (! wetInst->idle()); // the echo tail has not been silent for 3 s yet
  8286. }
  8287. TEST (drumsynth_live_engine)
  8288. {
  8289. // REA_rodada30 §1: key → patch slot, state parsing, the engine against our reference model (no DLL: synthesised
  8290. // sources only, noise from the fallback seed), voice modes, patch level/pan
  8291. CHECK (lynx::drumSynthKeyToSlot (48) == 0 && lynx::drumSynthKeyToSlot (71) == 23 && lynx::drumSynthKeyToSlot (0) == 24);
  8292. CHECK (lynx::drumSynthKeyToSlot (47) == 71 && lynx::drumSynthKeyToSlot (72) == 72 && lynx::drumSynthKeyToSlot (119) == 119);
  8293. CHECK (lynx::drumSynthKeyToSlot (120) == -1);
  8294. // a version 3 state: 120 names, 120 × 38 ints (send track −1 becomes 0 below version 4), flags
  8295. std::vector<std::uint8_t> blob;
  8296. auto put = [&] (std::uint32_t v) { for (int k = 0; k < 4; ++k) blob.push_back ((std::uint8_t) (v >> (8 * k))); };
  8297. put (3);
  8298. put (66);
  8299. for (int i = 0; i < 120; ++i)
  8300. blob.push_back (2), blob.push_back ('a'), blob.push_back ((std::uint8_t) ('0' + i % 10));
  8301. for (int i = 0; i < 120 * 38; ++i)
  8302. put ((std::uint32_t) (i % 38 == 36 ? -1 : i % 38 == 4 ? 300 : 0));
  8303. put (0);
  8304. const auto parsed = lynx::drumSynthLiveParams (blob);
  8305. CHECK (parsed && (int) parsed->size() == lynx::drumSynthParamCount);
  8306. CHECK (parsed && (*parsed)[36] == 0.0f && (*parsed)[38 * 7 + 4] == 300.0f && (*parsed)[(std::size_t) lynx::drumSynthEditorKeyIndex] == 66.0f);
  8307. blob.resize (500);
  8308. CHECK (! lynx::drumSynthLiveParams (blob));
  8309. // patch on key 60 (slot 12): tone-mode Osc1 with sweep and click, noise-band Osc2, ring, 2 retriggers
  8310. 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,
  8311. 0, 0, 0, 0, 0, 0, 30, -1, 0, 0, 64 };
  8312. lynx::ChannelDef c;
  8313. c.instrument = lynx::InstrumentKind::drumsynth;
  8314. c.modelParams.assign ((std::size_t) lynx::drumSynthParamCount, 0.0f);
  8315. for (int s = 0; s < 120; ++s)
  8316. c.modelParams[(std::size_t) (38 * s + 15)] = c.modelParams[(std::size_t) (38 * s + 34)] = -1.0f;
  8317. for (int i = 0; i < 38; ++i)
  8318. c.modelParams[(std::size_t) (38 * 12 + i)] = (float) patch[i];
  8319. auto render = [] (const lynx::ChannelDef& def, std::vector<int> keys, int total, float gl = 1.0f, float gr = 1.0f) {
  8320. auto inst = lynx::makeInstrument (lynx::InstrumentKind::drumsynth);
  8321. inst->configure (def, 120.0);
  8322. inst->prepare (48000.0);
  8323. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  8324. for (int s = 0; s < total; s += 512)
  8325. {
  8326. for (std::size_t k = 0; k + 1 < keys.size(); k += 2)
  8327. if (keys[k + 1] == s)
  8328. inst->noteOn (keys[k], 100.0f / 128.0f);
  8329. inst->render (l.data() + s, r.data() + s, std::min (512, total - s), gl, gr);
  8330. }
  8331. return std::make_tuple (l, r, std::move (inst));
  8332. };
  8333. {
  8334. auto [l, r, inst] = render (c, { 60, 0 }, 9600);
  8335. // reference model (REA round 30 dsl_model.py, seed 0x56CE, FL velocity 100, V = 1, centre: × g(100)·cos(π/4))
  8336. const std::pair<int, float> golden[] { { 0, -0.0074092713f }, { 10, -0.13961405f }, { 100, -0.47290668f }, { 1000, -0.29188696f },
  8337. { 3000, -0.22130175f }, { 6000, -0.049537566f }, { 9599, 0.010392102f } };
  8338. for (const auto& [i, v] : golden)
  8339. CHECK (std::abs (l[(std::size_t) i] - v) < 2e-6f && std::abs (r[(std::size_t) i] - v) < 2e-6f);
  8340. std::vector<float> tail (48000 * 4), tailR (tail.size());
  8341. inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
  8342. CHECK (inst->idle()); // the engine reported the voice finished
  8343. }
  8344. auto energy = [] (const std::vector<float>& x, int from, int to) {
  8345. double e = 0.0;
  8346. for (int i = from; i < to; ++i)
  8347. e += (double) x[(std::size_t) i] * x[(std::size_t) i];
  8348. return e;
  8349. };
  8350. // a key without a patch level (slot 0, level 0) and a key outside the 120 patches are silent
  8351. CHECK (energy (std::get<0> (render (c, { 48, 0, 125, 0 }, 4096)), 0, 4096) == 0.0);
  8352. // mono: a second hit on the same key cuts the first; poly: both ring together (Osc2 off: no noise, deterministic)
  8353. auto tone = c;
  8354. tone.modelParams[38 * 12 + 14] = 0.0f;
  8355. const auto single = std::get<0> (render (tone, { 60, 0 }, 8192));
  8356. const auto mono = std::get<0> (render (tone, { 60, 0, 60, 2048 }, 8192));
  8357. auto poly = tone;
  8358. poly.modelParams[38 * 12 + 24] = 1.0f;
  8359. const auto both = std::get<0> (render (poly, { 60, 0, 60, 2048 }, 8192));
  8360. double monoDiff = 0.0, polyDiff = 0.0;
  8361. for (int i = 2048 + 300; i < 8192; ++i) // after the cut fade: mono = only the second hit, a copy of the first
  8362. 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]);
  8363. CHECK (monoDiff < 1e-3 && polyDiff > 1.0);
  8364. // patch pan +64 → right only; channel volume counts twice (FL applies Final.Vol in the engine and at the host)
  8365. auto right = c;
  8366. right.modelParams[38 * 12 + 1] = 64.0f;
  8367. const auto [pl, pr, pi] = render (right, { 60, 0 }, 4096);
  8368. CHECK (energy (pl, 0, 4096) < 1e-12 * energy (pr, 0, 4096) && energy (pr, 0, 4096) > 1.0);
  8369. const auto full = std::get<0> (render (tone, { 60, 0 }, 4096));
  8370. const auto half = std::get<0> (render (tone, { 60, 0 }, 4096, 0.5f, 0.5f));
  8371. CHECK (std::abs (half[100] - 0.25f * full[100]) < 1e-6f && std::abs (full[100]) > 0.01f);
  8372. }
  8373. TEST (kepler_lfo_bpm_sync)
  8374. {
  8375. // 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)
  8376. std::vector<std::uint8_t> state (0x21 + 34 * 4, 0);
  8377. auto putU32 = [&] (std::size_t o, std::uint32_t v) { std::memcpy (state.data() + o, &v, 4); };
  8378. putU32 (12, 9);
  8379. std::memcpy (state.data() + 16, "Kepler-1", 9);
  8380. putU32 (0x19, 4);
  8381. putU32 (0x1D, 34);
  8382. float k[34] {};
  8383. k[5] = 0.602085769f; // "Pads - Kepler" demo: n = 286
  8384. k[6] = 1.0f;
  8385. k[10] = 1.0f; // vibrato on
  8386. k[33] = 1.0f;
  8387. std::memcpy (state.data() + 0x21, k, sizeof k);
  8388. lynx::SynthSettings s;
  8389. float gain = 0.0f;
  8390. CHECK (lynx::synthFromKepler (state, s, gain));
  8391. CHECK (s.lfo[lynx::lfoPitch].sync && s.lfo[lynx::lfoPitch].secondsTiming);
  8392. CHECK (std::abs (s.lfo[lynx::lfoPitch].period - 286.0f / 192.0f) < 1e-6f);
  8393. k[5] = 0.0f; // slowest: 32 beats
  8394. std::memcpy (state.data() + 0x21, k, sizeof k);
  8395. CHECK (lynx::synthFromKepler (state, s, gain) && std::abs (s.lfo[lynx::lfoPitch].period - 32.0f) < 1e-6f);
  8396. k[6] = 0.0f; // free rate in seconds
  8397. std::memcpy (state.data() + 0x21, k, sizeof k);
  8398. CHECK (lynx::synthFromKepler (state, s, gain) && ! s.lfo[lynx::lfoPitch].sync);
  8399. }
  8400. namespace
  8401. {
  8402. // A Harmor state written from scratch (REA_rodada11 §4.1 layout, version 18): part A only — a decaying-harmonics
  8403. // timbre, low-pass + resonance, filter envelope, 3-voice unison, vibrato, pluck, tremolo, volume envelope with a
  8404. // velocity map — and the global block. Every other curve is neutral.
  8405. // features (REA_rodada75): 1 arpeggio markers on the volume envelope, 2 noise resonance on filter 1, 4 "pluck replaces
  8406. // blur decay", 8 pluck blend, 16 harmonic randomness, 32 phaser + harmonizer masks
  8407. std::vector<std::uint8_t> harmorTestState (bool panLfo = true, bool image = false, int features = 0)
  8408. {
  8409. std::vector<std::uint8_t> b;
  8410. 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))); };
  8411. 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))); };
  8412. struct Pt { double dx, y, tension; int mode; int arp = 0; };
  8413. auto curve = [&] (const std::vector<Pt>& pts, std::array<std::int32_t, 5> hdr) {
  8414. i32 (3), i32 (3), i32 ((std::int32_t) pts.size());
  8415. for (const auto& p : pts)
  8416. {
  8417. f64 (p.dx), f64 (p.y);
  8418. const auto t = (float) p.tension;
  8419. std::uint32_t u;
  8420. std::memcpy (&u, &t, 4);
  8421. i32 ((std::int32_t) u);
  8422. b.push_back ((std::uint8_t) p.mode), b.push_back ((std::uint8_t) p.arp), b.push_back (0), b.push_back (0);
  8423. }
  8424. for (auto h : hdr)
  8425. i32 (h);
  8426. };
  8427. const std::array<std::int32_t, 5> off { 0, 0, -1, -1, -1 }, on { 0, 1, -1, -1, -1 };
  8428. 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); };
  8429. i32 (18);
  8430. i32 (1 | 4);
  8431. std::vector<std::uint8_t> part;
  8432. std::swap (b, part);
  8433. std::array<std::int32_t, 386> p {};
  8434. const std::pair<int, int> values[] = { { 0, 500 }, { 3, 1000 }, { 9, 1024 }, { 11, 1000 }, { 12, 1000 }, { 17, 300 }, { 18, 4000 },
  8435. { 19, 500 }, { 20, 1000 }, { 24, 500 }, { 25, 500 }, { 28, 500 }, { 29, 256 }, { 30, -1 }, { 31, -1 }, { 33, -1 }, { 34, 3 },
  8436. { 35, 1 }, { 37, 192 }, { 38, 128 }, { 39, 500 }, { 40, 10000 }, { 41, 1 }, { 42, 10000 }, { 43, 1 }, { 45, 200 }, { 46, 500 },
  8437. { 47, 1 }, { 49, -140 }, { 50, 1200 }, { 51, 0 }, { 52, 5000 }, { 54, 500 }, { 56, 600 }, { 57, 300 }, { 59, 500 }, { 60, 600 },
  8438. { 64, 8192 }, { 66, 500 }, { 72, 600 }, { 74, 1000 }, { 75, 600 }, { 78, 5000 }, { 79, 500 }, { 80, 3000 } };
  8439. for (const auto& [i, v] : values)
  8440. p[(std::size_t) i] = v;
  8441. if ((features & 2) != 0)
  8442. p[57] = 400, p[59] = 300; // filter-1 resonance amount and width (noise type set in the combo block)
  8443. if ((features & 4) != 0)
  8444. p[75] = 700, p[21] = 300; // pluck value, time-blur attack
  8445. if ((features & 8) != 0)
  8446. p[75] = 650, p[4] = 600; // pluck, timbre mix
  8447. if ((features & 32) != 0)
  8448. p[76] = 700, p[27] = 600; // phaser mix, harmonizer mix
  8449. if (image) // image synthesis (REA_rodada75): speed, cubic gain / linear freq interpolation, Hz freq mode, formant
  8450. for (const auto& [i, v] : std::initializer_list<std::pair<int, int>> { { 82, 200 }, { 83, 100000 }, { 84, 10000 }, { 85, 20000 },
  8451. { 86, 750 }, { 87, 4 }, { 88, 600 }, { 89, 900 }, { 90, 1 }, { 91, 3 }, { 92, 1500 }, { 93, -300 }, { 94, 800 } })
  8452. p[(std::size_t) i] = v;
  8453. for (auto v : p)
  8454. i32 (v);
  8455. for (int i = 0; i < 12; ++i)
  8456. i32 (i); // module order
  8457. std::array<std::uint8_t, 0x4b> misc {};
  8458. auto put = [&] (int o, std::int32_t v) { std::memcpy (misc.data() + o, &v, 4); };
  8459. const float one = 1.0f;
  8460. put (0x08, 107), put (0x1c, 1), put (0x28, 200), std::memcpy (misc.data() + 0x30, &one, 4);
  8461. put (0x34, 0), put (0x38, 4), put (0x3c, (features & 2) != 0 ? 109 : 100), put (0x40, 108);
  8462. if ((features & 8) != 0)
  8463. put (0x00, 5);
  8464. misc[0x44] = 1;
  8465. if ((features & 4) != 0)
  8466. misc[0x46] = 1;
  8467. b.insert (b.end(), misc.begin(), misc.end());
  8468. 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 };
  8469. for (int k = 0; k < 43; ++k)
  8470. {
  8471. static const int mode2[] = { 2, 14, 23, 28, 30, 38, 39, 41, 42 }; // ×2 around 0.5: 1.0 is neutral
  8472. const bool centred = std::find (std::begin (mode1), std::end (mode1), k) != std::end (mode1);
  8473. const bool doubled = std::find (std::begin (mode2), std::end (mode2), k) != std::end (mode2);
  8474. const double neutral = centred && ! doubled ? 0.5 : 1.0;
  8475. i32 (3);
  8476. i32 (1065353216), i32 (1065353216), i32 (1065353216);
  8477. if (k == 1 && (features & 1) != 0) // arpeggio volume envelope: a looping pattern with chord markers
  8478. curve ({ { 0, 0, 0, 0, 2 }, { 0.005, 1, 0.3, 0 }, { 0.08, 0.2, -0.4, 0, 3 }, { 0.005, 1, 0.2, 0 }, { 0.08, 0.1, 0, 0, 1 },
  8479. { 0.1, 0, 0.2, 0 } },
  8480. { 0, 1, -1, 0, 4 });
  8481. else if (k == 1) // volume envelope: attack, decay to the sustain point, release
  8482. 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 });
  8483. else if (k == 4) // filter-1 envelope
  8484. curve ({ { 0, 1, 0, 0 }, { 0.25, 0.2, -0.5, 0 } }, { 0, 1, -1, -1, -1 });
  8485. else
  8486. flat (neutral);
  8487. for (int a : { 128, 128, 0, 128 })
  8488. i32 (a);
  8489. if (k == 0 && panLfo) // pan LFO
  8490. flat (1.0, on);
  8491. else
  8492. flat (0.5);
  8493. for (int a : { k == 0 ? 40000 : 0, 0, 0, 0 })
  8494. i32 (a);
  8495. i32 (0);
  8496. for (int m = 2; m < 11; ++m)
  8497. if (k == 1 && m == 3)
  8498. curve ({ { 0, 0.2, 0, 0 }, { 1, 1, -0.3, 0 } }, off); // velocity → volume
  8499. else
  8500. flat (neutral);
  8501. }
  8502. for (int c = 0; c < 26; ++c)
  8503. if ((features & 16) != 0 && c == 4)
  8504. curve ({ { 0, 0.5, 0, 0 }, { 0.2, 0.8, 0.3, 0 }, { 0.8, 0.3, 0, 0 } }, off); // harmonic randomness
  8505. else if ((features & 32) != 0 && (c == 22 || c == 23))
  8506. curve ({ { 0, 0.3, 0, 0 }, { 0.6, 1.0, 0.2, 0 }, { 0.4, 0.5, 0, 0 } }, off); // phaser / harmonizer masks
  8507. else if (c == 3 && (features & 8) != 0)
  8508. curve ({ { 0, 0.2, 0, 0 }, { 1, 0.05, 0.4, 0 } }, off); // timbre 2 for the pluck blend
  8509. else if (c == 2 || c == 3)
  8510. curve ({ { 0, 0.25, 0, 0 }, { 0.006, 0.12, -0.46, 0 }, { 0.994, 0, 0, 0 } }, off); // timbres
  8511. else if (c == 14)
  8512. curve ({ { 0, 1, 0, 0 }, { 1, 0, 0, 0 } }, off); // pluck curve
  8513. else
  8514. flat (c == 0 || c == 17 ? 0.25 : (c == 5 ? 0.0 : (c == 6 || c == 10 || c == 11 || (c >= 18 && c <= 25) ? 1.0 : 0.5)));
  8515. if (image)
  8516. {
  8517. // the image object (FUN_00793300 layout): version 0, image mode, looping, 6 columns × 40 rows, gain + freq planes
  8518. const int cols = 6, rows = 40;
  8519. i32 (0), i32 (0), i32 (1), i32 (0);
  8520. f64 (-1.0), f64 (-1.0), f64 (0.0);
  8521. i32 (cols), i32 (rows);
  8522. i32 (cols * rows);
  8523. for (int c2 = 0; c2 < cols; ++c2)
  8524. for (int r2 = 0; r2 < rows; ++r2)
  8525. {
  8526. const auto v = (float) (255.0 * std::pow (0.93, r2) * (0.6 + 0.4 * std::cos (0.9 * c2 + 0.37 * r2)));
  8527. std::uint32_t u;
  8528. std::memcpy (&u, &v, 4);
  8529. i32 ((std::int32_t) u);
  8530. }
  8531. i32 (cols * rows);
  8532. for (int c2 = 0; c2 < cols; ++c2)
  8533. for (int r2 = 0; r2 < rows; ++r2)
  8534. {
  8535. const auto v = (float) (0.3 * std::sin (1.3 * c2 + 0.5 * r2));
  8536. std::uint32_t u;
  8537. std::memcpy (&u, &v, 4);
  8538. i32 ((std::int32_t) u);
  8539. }
  8540. i32 (0);
  8541. b.push_back (0); // (file name: empty)
  8542. }
  8543. std::swap (b, part);
  8544. i32 ((std::int32_t) part.size());
  8545. b.insert (b.end(), part.begin(), part.end());
  8546. std::array<std::int32_t, 53> g {};
  8547. g[0] = g[1] = g[2] = 256, g[10] = 500, g[11] = 1000, g[12] = 1000, g[15] = 1000;
  8548. for (auto v : g)
  8549. i32 (v);
  8550. for (int i = 0; i < 6; ++i)
  8551. i32 (i);
  8552. std::array<std::uint8_t, 0x3d> gm {};
  8553. const std::int32_t cr = 200, vr = 1;
  8554. std::memcpy (gm.data() + 12, &vr, 4);
  8555. std::memcpy (gm.data() + 24, &cr, 4);
  8556. b.insert (b.end(), gm.begin(), gm.end());
  8557. return b;
  8558. }
  8559. } // namespace
  8560. TEST (harmor_engine)
  8561. {
  8562. // REA_rodada34: state parsing, the additive engine against our reference model (hmodel.py), host levels, voice end
  8563. const auto blob = harmorTestState();
  8564. if (const char* dump = std::getenv ("LYNX_DUMP_HARMOR"))
  8565. std::ofstream (dump, std::ios::binary).write ((const char*) blob.data(), (std::streamsize) blob.size());
  8566. CHECK (lynx::harmorStateUsable (blob));
  8567. CHECK (lynx::harmorFallbacks (blob).empty());
  8568. const auto at = lynx::harmorParamOffset (blob, 0, 52);
  8569. std::int32_t cutoff = 0;
  8570. CHECK (at > 0 && (std::memcpy (&cutoff, blob.data() + at, 4), cutoff == 5000));
  8571. CHECK (lynx::harmorParamOffset (blob, 1, 52) < 0 && lynx::harmorParamOffset (blob, -1, 12) > at);
  8572. auto broken = blob;
  8573. broken.resize (broken.size() / 2);
  8574. CHECK (! lynx::harmorStateUsable (broken));
  8575. auto old = blob;
  8576. old[0] = 15; // articulators and curves are stored from version 16
  8577. CHECK (! lynx::harmorStateUsable (old));
  8578. lynx::ChannelDef c;
  8579. c.instrument = lynx::InstrumentKind::harmor;
  8580. c.pluginState = blob;
  8581. auto render = [] (const lynx::ChannelDef& def, int total, float gl, float gr, int offAt) {
  8582. auto inst = lynx::makeInstrument (lynx::InstrumentKind::harmor);
  8583. inst->configure (def, 120.0);
  8584. inst->prepare (44100.0);
  8585. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  8586. inst->noteOn (60, 0.78125f);
  8587. for (int s = 0; s < total; s += 512)
  8588. {
  8589. if (s == offAt)
  8590. inst->noteOff (60);
  8591. inst->render (l.data() + s, r.data() + s, std::min (512, total - s), gl, gr);
  8592. }
  8593. return std::make_tuple (l, r, std::move (inst));
  8594. };
  8595. {
  8596. // reference model (REA round 34 hmodel.py; C5 at FL velocity 100, Final.Vol 1, centre, released at 6144, the
  8597. // Mersenne Twister seeded like LYNX's; since flbeta165h LYNX's own values: the test state plays at precision
  8598. // "Average", whose IPP 11-bit Exp/Ln — REA_rodada165g-flbeta165h §3 — the model computes exactly)
  8599. auto [l, r, inst] = render (c, 8192, 1.0f, 1.0f, 6144);
  8600. if (std::getenv ("LYNX_PRINT_HARMOR_ENGINE") != nullptr)
  8601. for (int i : { 0, 100, 1000, 3000, 6000, 7000, 8191 })
  8602. std::printf ("harmor engine %d %.8g %.8g\n", i, (double) l[(std::size_t) i], (double) r[(std::size_t) i]);
  8603. const std::tuple<int, float, float> golden[] {
  8604. { 0, 0.0f, 0.0f }, { 100, 0.0094771842f, 0.042498328f }, { 1000, 0.081412978f, -0.032517102f }, { 3000, 0.059289735f, -0.0024759346f },
  8605. { 6000, 0.029978603f, 0.14549156f }, { 7000, -0.011218854f, 0.041979425f }, { 8191, 0.0031455588f, 0.03857585f } };
  8606. for (const auto& [i, gl, gr] : golden)
  8607. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  8608. // after the release the volume envelope ends, the record declicks and the instrument goes idle
  8609. std::vector<float> tail (44100), tailR (tail.size());
  8610. inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
  8611. CHECK (inst->idle());
  8612. CHECK (std::abs (tail.back()) == 0.0f);
  8613. }
  8614. // the reference renderer gives the same samples as the instrument
  8615. {
  8616. const auto ref = lynx::harmor::renderReference (blob, { { 0, 60, 0.78125, 1.0, 0.0, 6144 } }, 44100.0, 8192);
  8617. const auto [l, r, inst] = render (c, 8192, 1.0f, 1.0f, 6144);
  8618. double worst = 0.0;
  8619. for (int i = 0; i < 8192; ++i)
  8620. 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]) });
  8621. CHECK (worst == 0.0);
  8622. }
  8623. // FL's level law applied once: half the channel volume halves the output; full right pan silences the left side
  8624. const auto full = std::get<0> (render (c, 2048, 1.0f, 1.0f, -1));
  8625. const auto half = std::get<0> (render (c, 2048, 0.5f, 0.5f, -1));
  8626. CHECK (std::abs (half[1000] - 0.5f * full[1000]) < 1e-6f && std::abs (full[1000]) > 0.01f);
  8627. auto mono = c;
  8628. mono.pluginState = harmorTestState (false); // no pan LFO ...
  8629. const std::int32_t one = 1;
  8630. std::memcpy (mono.pluginState.data() + lynx::harmorParamOffset (mono.pluginState, 0, 34), &one, 4); // ... nor unison spread
  8631. const auto [pl, pr, pi] = render (mono, 2048, 0.0f, 1.41421356f, -1);
  8632. double el = 0.0, er = 0.0;
  8633. for (int i = 0; i < 2048; ++i)
  8634. el += (double) pl[(std::size_t) i] * pl[(std::size_t) i], er += (double) pr[(std::size_t) i] * pr[(std::size_t) i];
  8635. CHECK (el < 1e-10 * er && er > 1e-3);
  8636. }
  8637. namespace
  8638. {
  8639. // An Ogun state written from scratch (REA_rodada37 §1.3 layout, version 18; zlib with stored blocks): richness 4
  8640. // (255 partials), pre-decay, decay/release, fullness, Seed 1 + fixed Seed 2 with morph, 3-voice unison, a 2-stage
  8641. // low-pass with an LFO on its cutoff, a volume envelope with a sustain point and a velocity map, declick, normalisation,
  8642. // Impulse/Decay maps, and every plugin effect (EQ band, chorus, ping-pong delay, reverb). Everything else is neutral.
  8643. std::vector<std::uint8_t> ogunTestState()
  8644. {
  8645. std::vector<std::uint8_t> b;
  8646. 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))); };
  8647. auto f32v = [&] (float v) { std::uint32_t u; std::memcpy (&u, &v, 4); i32 ((std::int32_t) u); };
  8648. 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))); };
  8649. struct Pt { double dx, y, tension; int mode; };
  8650. auto curve = [&] (int type, const std::vector<Pt>& pts, std::array<std::int32_t, 5> hdr) {
  8651. i32 (type), i32 (3), i32 ((std::int32_t) pts.size());
  8652. for (const auto& p : pts)
  8653. {
  8654. f64 (p.dx), f64 (p.y), f32v ((float) p.tension);
  8655. b.push_back ((std::uint8_t) p.mode), b.push_back (0), b.push_back (0), b.push_back (0);
  8656. }
  8657. for (auto h : hdr)
  8658. i32 (h);
  8659. };
  8660. const std::array<std::int32_t, 5> off { 0, 0, -1, -1, -1 }, on { 0, 1, -1, -1, -1 };
  8661. auto flat = [&] (int type, double y) { curve (type, { { 0, y, 0, 0 }, { 1, y, 0, 0 } }, off); };
  8662. std::array<std::int32_t, 64> P {};
  8663. const std::pair<int, int> values[] = { { 0, 128 }, { 5, 300 }, { 6, 3000 }, { 7, 2000 }, { 8, 200 }, { 9, 500 }, { 10, 12 },
  8664. { 11, 34 }, { 12, 3 }, { 13, 64 }, { 14, 64 }, { 15, 40 }, { 16, 64 }, { 17, 180 }, { 18, 60 }, { 19, 2 }, { 20, 300 },
  8665. { 21, 400 }, { 22, 40 }, { 23, 30 }, { 24, 20 }, { 25, 64 }, { 26, 40 }, { 27, 96 }, { 28, 100 }, { 29, 64 }, { 30, 2 },
  8666. { 31, 19 }, { 32, 221 }, { 33, 0 }, { 34, 50 }, { 35, 100 }, { 36, 20 }, { 37, 60 }, { 38, 2 }, { 39, 64 }, { 40, 300 },
  8667. { 43, 30000 }, { 46, 20000 }, { 49, 0 }, { 50, 2 }, { 51, 0 }, { 52, 9 }, { 53, 1 }, { 54, 0 },
  8668. { 55, 0x01000000 }, // +0x20b: pitch follows the key
  8669. { 56, 0x00010000 }, // +0x20e: declick
  8670. { 62, 0x00000100 } }; // +0x225: normalise
  8671. for (const auto& [i, v] : values)
  8672. P[(std::size_t) i] = v;
  8673. for (auto v : P)
  8674. i32 (v);
  8675. b.push_back (0), b.push_back (1), b.push_back (0); // delay: not tempo-based, on
  8676. i32 (1), i32 (1), i32 (1); // EQ band types: peaking
  8677. b.push_back (0); // comment: 0 characters
  8678. for (int k = 0; k < 13; ++k)
  8679. {
  8680. static constexpr int sum[] = { 1, 2, 3, 9, 11, 12 };
  8681. const double neutral = std::find (std::begin (sum), std::end (sum), k) != std::end (sum) ? 0.5 : 1.0;
  8682. i32 (1);
  8683. f32v (0.0f), f32v (1.0f), f32v (k == 1 ? 0.3f : 0.0f);
  8684. if (k == 0) // volume envelope: attack, decay to the sustain point, release
  8685. 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 });
  8686. else
  8687. flat (1, neutral);
  8688. for (int a : { 128, 128, 128, 128 })
  8689. i32 (a);
  8690. if (k == 1) // cutoff LFO
  8691. curve (2, { { 0, 0.8, 0, 0 } }, on);
  8692. else
  8693. flat (2, 0.5);
  8694. for (int a : { k == 1 ? 40000 : 0, 0, 0, 0 })
  8695. i32 (a);
  8696. i32 (0);
  8697. for (int m = 0; m < 4; ++m)
  8698. if (k == 0 && m == 1)
  8699. curve (3, { { 0, 0.3, 0, 0 }, { 1, 1, -0.2, 0 } }, off); // velocity → volume
  8700. else
  8701. flat (3, neutral);
  8702. }
  8703. flat (3, 0.0); // Forced: none
  8704. curve (3, { { 0, 0.6, 0, 0 }, { 1, 0.4, 0.3, 0 } }, off); // Impulse EQ
  8705. curve (3, { { 0, 0.5, 0, 0 }, { 1, 0.3, 0, 0 } }, off); // Decay EQ
  8706. flat (3, 0.5); // Release EQ
  8707. // u32 version + zlib (stored blocks)
  8708. std::vector<std::uint8_t> s { 18, 0, 0, 0, 0x78, 0x01 };
  8709. for (std::size_t i = 0; i < b.size();)
  8710. {
  8711. const auto len = std::min<std::size_t> (65535, b.size() - i);
  8712. s.push_back (i + len >= b.size() ? 1 : 0);
  8713. s.push_back ((std::uint8_t) len), s.push_back ((std::uint8_t) (len >> 8));
  8714. s.push_back ((std::uint8_t) ~len), s.push_back ((std::uint8_t) (~len >> 8));
  8715. s.insert (s.end(), b.begin() + (std::ptrdiff_t) i, b.begin() + (std::ptrdiff_t) (i + len));
  8716. i += len;
  8717. }
  8718. std::uint32_t a1 = 1, a2 = 0;
  8719. for (auto c : b)
  8720. a1 = (a1 + c) % 65521u, a2 = (a2 + a1) % 65521u;
  8721. for (int sh = 24; sh >= 0; sh -= 8)
  8722. s.push_back ((std::uint8_t) (((a2 << 16) | a1) >> sh));
  8723. return s;
  8724. }
  8725. } // namespace
  8726. TEST (ogun_engine)
  8727. {
  8728. // REA_rodada37: state parsing, the engine against our reference model (ogmodel.py), host levels, export quality
  8729. const auto blob = ogunTestState();
  8730. if (const char* dump = std::getenv ("LYNX_DUMP_OGUN"))
  8731. std::ofstream (dump, std::ios::binary).write ((const char*) blob.data(), (std::streamsize) blob.size());
  8732. CHECK (lynx::ogunStateUsable (blob));
  8733. CHECK (lynx::ogunFallbacks (blob).empty());
  8734. CHECK (lynx::ogunParam (blob, 17) == 180 && lynx::ogunParam (blob, 52) == 9 && ! lynx::ogunParam (blob, lynx::ogunDelayOnIndex + 1));
  8735. auto broken = blob;
  8736. broken.resize (broken.size() - 300);
  8737. CHECK (! lynx::ogunStateUsable (broken));
  8738. auto old = blob;
  8739. old[0] = 13; // a version-18 body read as version 13 (a u32 comment count; versions 10…13: Gen169lTests)
  8740. CHECK (! lynx::ogunStateUsable (old));
  8741. // reference model (REA round 37 ogmodel.Plugin(state).run: C5 velocity 0.75 released at 6144, G5 velocity 0.625 at 2048
  8742. // released at 8192; host block 512)
  8743. 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 } };
  8744. const auto ref = lynx::ogun::renderReference (blob, notes, 44100.0, 16384);
  8745. CHECK (ref.size() == 2 * 16384);
  8746. if (const char* dump = std::getenv ("LYNX_DUMP_OGUN_OUT"))
  8747. std::ofstream (dump, std::ios::binary).write ((const char*) ref.data(), (std::streamsize) (ref.size() * 4));
  8748. const std::tuple<int, float, float> golden[] {
  8749. { 100, -7.763202e-05f, -8.919523e-05f }, { 1000, 0.06195267f, 0.03858230f }, { 3000, 0.01363476f, -0.00378433f },
  8750. { 6000, -0.06601223f, -0.03084030f }, { 9000, 0.04204506f, 0.02452896f }, { 12000, 0.03420147f, -0.01837870f },
  8751. { 16383, -0.00465311f, -0.00119627f } };
  8752. for (const auto& [i, gl, gr] : golden)
  8753. CHECK (std::abs (ref[2 * (std::size_t) i] - gl) < 2e-6f && std::abs (ref[2 * (std::size_t) i + 1] - gr) < 2e-6f);
  8754. // the instrument plays like the reference renderer (Final.Vol 1, centre)
  8755. lynx::ChannelDef c;
  8756. c.instrument = lynx::InstrumentKind::ogun;
  8757. c.pluginState = blob;
  8758. auto render = [&] (const lynx::ChannelDef& def, int total, float gl, float gr, bool held = false) {
  8759. auto inst = lynx::makeInstrument (lynx::InstrumentKind::ogun);
  8760. inst->configure (def, 120.0);
  8761. inst->prepare (44100.0);
  8762. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  8763. for (int s = 0; s < total; s += 512)
  8764. {
  8765. if (s == 0)
  8766. inst->noteOn (60, 0.75f);
  8767. if (s == 2048)
  8768. inst->noteOn (67, 0.625f);
  8769. if (s == 6144 && ! held)
  8770. inst->noteOff (60);
  8771. if (s == 8192 && ! held)
  8772. inst->noteOff (67);
  8773. inst->render (l.data() + s, r.data() + s, std::min (512, total - s), gl, gr);
  8774. }
  8775. return std::make_tuple (l, r, std::move (inst));
  8776. };
  8777. {
  8778. auto [l, r, inst] = render (c, 16384, 1.0f, 1.0f);
  8779. double worst = 0.0;
  8780. for (int i = 0; i < 16384; ++i)
  8781. 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]) });
  8782. CHECK (worst == 0.0);
  8783. // the voices end and the effect tails die out: the instrument goes idle
  8784. std::vector<float> tail (44100), tailR (tail.size());
  8785. for (int k = 0; k < 20 && ! inst->idle(); ++k)
  8786. inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
  8787. CHECK (inst->idle());
  8788. }
  8789. // FL's level law applied once (effects off): half the channel volume halves the output once the gain ramps
  8790. // (limited to 0.001 per sample) have settled on the sustain level; full right pan silences the left side
  8791. {
  8792. auto dry = c;
  8793. for (int i : { 19, 29, 40, 53 })
  8794. CHECK (lynx::ogunSetParam (dry.pluginState, i, 0)); // chorus, delay wet, EQ band and reverb off
  8795. CHECK (lynx::ogunStateUsable (dry.pluginState) && lynx::ogunParam (dry.pluginState, 19) == 0);
  8796. const auto full = std::get<0> (render (dry, 32768, 1.0f, 1.0f, true));
  8797. const auto half = std::get<0> (render (dry, 32768, 0.5f, 0.5f, true));
  8798. double worst = 0.0, peak = 0.0;
  8799. for (int i = 30000; i < 32768; ++i)
  8800. worst = std::max (worst, (double) std::abs (half[(std::size_t) i] - 0.5f * full[(std::size_t) i])),
  8801. peak = std::max (peak, (double) std::abs (full[(std::size_t) i]));
  8802. CHECK (worst <= 1e-7 * peak && peak > 1e-3);
  8803. // FL's default channel volume (100/128): FL hands Ogun Final.Vol = (11^v − 1)/10 (REA_rodada28 §4.1); LYNX's
  8804. // centred channel gains (√2·V·cos(π/4) each) give the reference model's level at that Final.Vol
  8805. const auto V = lynx::channelGain (0.78125f);
  8806. const auto g = V * std::cos (0.25f * 3.14159265f) * 1.41421356f;
  8807. const auto atDefault = std::get<0> (render (dry, 32768, g, g, true));
  8808. const auto refDefault = lynx::ogun::renderReference (dry.pluginState, { { 0, 60, 0.75, V, 0.0, 0.0, 0.0, 0.0, 0.5, -1 },
  8809. { 2048, 67, 0.625, V, 0.0, 0.0, 0.0, 0.0, 0.5, -1 } }, 44100.0, 32768);
  8810. double ei = 0.0, er0 = 0.0;
  8811. for (int i = 30000; i < 32768; ++i)
  8812. 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];
  8813. CHECK (er0 > 0.0 && std::abs (10.0 * std::log10 (ei / er0)) < 0.001);
  8814. const auto [pl, pr, pi] = render (dry, 4096, 0.0f, 1.41421356f);
  8815. double el = 0.0, er = 0.0;
  8816. for (int i = 0; i < 4096; ++i)
  8817. el += (double) pl[(std::size_t) i] * pl[(std::size_t) i], er += (double) pr[(std::size_t) i] * pr[(std::size_t) i];
  8818. CHECK (el < 1e-10 * er && er > 1e-3);
  8819. }
  8820. // export: FL renders Ogun at quality 4 (×2 rate, 32-point reader), LYNX too while it renders offline
  8821. {
  8822. lynx::setOfflineRendering (true);
  8823. auto [l, r, inst] = render (c, 8192, 1.0f, 1.0f);
  8824. lynx::setOfflineRendering (false);
  8825. lynx::ogun::RefOptions q4;
  8826. q4.quality = 4;
  8827. const auto ref4 = lynx::ogun::renderReference (blob, notes, 44100.0, 8192, q4);
  8828. double worst = 0.0, diff = 0.0;
  8829. for (int i = 0; i < 8192; ++i)
  8830. worst = std::max (worst, (double) std::abs (ref4[2 * (std::size_t) i] - l[(std::size_t) i])),
  8831. diff = std::max (diff, (double) std::abs (ref[2 * (std::size_t) i] - l[(std::size_t) i]));
  8832. CHECK (worst == 0.0 && diff > 1e-3);
  8833. const std::tuple<int, float, float> golden4[] {
  8834. { 100, 0.00605961f, 0.00628969f }, { 1000, 0.03729841f, 0.01727954f }, { 3000, -0.09010383f, -0.02551294f },
  8835. { 6000, 0.02167798f, -0.01115632f }, { 8191, 0.00320288f, -0.00615792f } };
  8836. for (const auto& [i, gl, gr] : golden4)
  8837. CHECK (std::abs (ref4[2 * (std::size_t) i] - gl) < 2e-6f && std::abs (ref4[2 * (std::size_t) i + 1] - gr) < 2e-6f);
  8838. }
  8839. // feature variants of the same state against the reference model (energy per side over 8192 samples, three
  8840. // samples; C1 released at 6144): qualities, richness, unison, the filter types, ALT DECAY, 1/√h, Seed 2 Random,
  8841. // infinite decay, logarithmic axis, velocity → position, other rates. Goldens from ogmodel.py (REA round 37).
  8842. {
  8843. struct Sample { int at; float l, r; };
  8844. struct Variant { const char* name; std::vector<std::pair<int, int>> params; int rate; double el, er; Sample s[3]; };
  8845. const Variant variants[] {
  8846. { "q1", { { 54, 1 } }, 44100, 11.0970402, 6.87820714, { { 1500, 0.0240363069f, -0.0148524037f }, { 4000, 0.0873190612f, -0.000715861097f }, { 7000, -0.0378603041f, -0.00338819111f } } },
  8847. { "q2", { { 54, 2 } }, 44100, 11.2405446, 6.68045082, { { 1500, 0.0448660068f, -0.00560745923f }, { 4000, -0.0122657465f, 0.0104285963f }, { 7000, -0.0216034129f, -0.0134632047f } } },
  8848. { "q3", { { 54, 3 } }, 44100, 11.3200608, 6.86196449, { { 1500, -0.0031537537f, -0.0242648814f }, { 4000, 0.0426301025f, 0.0364697985f }, { 7000, 0.00843803957f, 0.0302239582f } } },
  8849. { "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 } } },
  8850. { "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 } } },
  8851. { "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 } } },
  8852. { "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 } } },
  8853. { "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 } } },
  8854. { "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 } } },
  8855. { "alt", { { 62, 257 } }, 44100, 17.2485711, 9.99223979, { { 1500, -0.0319156498f, -0.0186850764f }, { 4000, -0.0769012421f, -0.0375530645f }, { 7000, 0.0310976505f, 0.0445244014f } } },
  8856. { "isqrt", { { 62, 65792 } }, 44100, 45.7309307, 27.1224593, { { 1500, -0.04376591f, -0.00845711026f }, { 4000, 0.0351300463f, -0.129557073f }, { 7000, 0.0096148327f, 0.00123740174f } } },
  8857. { "seed2rnd", { { 11, 0 } }, 44100, 16.8228819, 11.5433061, { { 1500, -0.0117614279f, -0.026497297f }, { 4000, 0.00265733898f, -0.07069581f }, { 7000, 0.00576663949f, 0.0565217957f } } },
  8858. { "decinf", { { 6, 10000 } }, 44100, 19.1879226, 11.0487311, { { 1500, -0.0324105881f, -0.0190765262f }, { 4000, -0.0809780285f, -0.0395730771f }, { 7000, 0.034197174f, 0.048585102f } } },
  8859. { "logx", { { 57, 16777216 } }, 44100, 12.3064326, 7.31519289, { { 1500, -0.0438990779f, 0.0096983267f }, { 4000, -0.0175748877f, 0.0313935578f }, { 7000, 0.0639200211f, 0.024419222f } } },
  8860. { "velpos", { { 56, 66304 } }, 44100, 4.9966925, 3.16540662, { { 1500, 0.0083851777f, -0.0165472236f }, { 4000, 0.0630451962f, 0.0156494901f }, { 7000, -0.0293043777f, -0.00182877108f } } },
  8861. { "fs48", { }, 48000, 10.8813888, 6.53592373, { { 1500, -0.0485184453f, -0.0319588967f }, { 4000, 0.0230421089f, -0.0286604818f }, { 7000, -0.0117630213f, -0.017459048f } } },
  8862. { "fs96", { }, 96000, 11.0189683, 6.39821883, { { 1500, -0.0267860368f, -0.0227564126f }, { 4000, 0.0238709711f, -0.0100849615f }, { 7000, -0.0034619756f, 0.0451687202f } } },
  8863. };
  8864. for (const auto& v : variants)
  8865. {
  8866. auto st = blob;
  8867. for (const auto& [i, x] : v.params)
  8868. CHECK (lynx::ogunSetParam (st, i, x));
  8869. const auto y = lynx::ogun::renderReference (st, { { 0, 60, 0.75, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 6144 },
  8870. { 2048, 67, 0.625, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1 } }, (double) v.rate, 8192);
  8871. double el = 0.0, er = 0.0, peak = 0.0;
  8872. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  8873. 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]) });
  8874. bool ok = y.size() == 2 * 8192 && std::abs (el - v.el) <= 1e-5 * v.el && std::abs (er - v.er) <= 1e-5 * v.er;
  8875. for (const auto& smp : v.s)
  8876. 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;
  8877. if (! ok)
  8878. std::printf (" Ogun variant %s differs from the model: energy %.9g %.9g (model %.9g %.9g)\n", v.name, el, er, v.el, v.er);
  8879. CHECK (ok);
  8880. }
  8881. }
  8882. }
  8883. // our own Maximus v7 state (layout REA_rodada8 §1.1): 7 globals, 4 × 13 band parameters, 8 envelopes (dx, y, tension, type)
  8884. static std::vector<std::uint8_t> maximusTestState (const std::array<int, 7>& g, const std::array<std::array<int, 13>, 4>& bands,
  8885. const std::array<std::vector<std::array<double, 4>>, 8>& envs, int oversampling)
  8886. {
  8887. std::vector<std::uint8_t> d;
  8888. auto put = [&] (const void* v, std::size_t n) { d.insert (d.end(), (const std::uint8_t*) v, (const std::uint8_t*) v + n); };
  8889. auto i32 = [&] (std::int32_t v) { put (&v, 4); };
  8890. i32 (7);
  8891. for (int copy = 0; copy < 2; ++copy)
  8892. {
  8893. for (auto v : g)
  8894. i32 (v);
  8895. for (const auto& b : bands)
  8896. for (auto v : b)
  8897. i32 (v);
  8898. }
  8899. d.resize (0x1fe, 0);
  8900. std::memcpy (d.data() + 0x1f4, &oversampling, 4);
  8901. for (const auto& e : envs)
  8902. {
  8903. i32 (3), i32 (2), i32 ((std::int32_t) e.size());
  8904. for (const auto& pt : e)
  8905. {
  8906. const double dx = pt[0], y = pt[1];
  8907. const float t = (float) pt[2];
  8908. put (&dx, 8), put (&y, 8), put (&t, 4);
  8909. const std::uint8_t flags[4] { (std::uint8_t) pt[3], 0, 0, 0 };
  8910. put (flags, 4);
  8911. }
  8912. i32 (0), i32 (-1), i32 (-1), i32 (-1), i32 (-1);
  8913. }
  8914. return d;
  8915. }
  8916. // A "Kepler-1" state written from scratch (REA_rodada46 §1.1 layout, version 2): the default panel values
  8917. std::vector<std::uint8_t> keplerTestState()
  8918. {
  8919. 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 };
  8920. std::vector<std::uint8_t> st;
  8921. auto u32 = [&] (std::uint32_t v) { for (int i = 0; i < 4; ++i) st.push_back ((std::uint8_t) (v >> (8 * i))); };
  8922. auto f32 = [&] (float f) { std::uint32_t v; std::memcpy (&v, &f, 4); u32 (v); };
  8923. u32 (0), u32 (0), u32 (0), u32 (9);
  8924. for (char c : std::string ("Kepler-1"))
  8925. st.push_back ((std::uint8_t) c);
  8926. st.push_back (0);
  8927. u32 (2), u32 (34);
  8928. for (auto f : panel)
  8929. f32 (f);
  8930. st.push_back (0);
  8931. f32 (1.0f), f32 (0.5f);
  8932. const auto size = (std::uint32_t) st.size() - 12;
  8933. std::memcpy (st.data() + 4, &size, 4);
  8934. return st;
  8935. }
  8936. std::string keplerDll()
  8937. {
  8938. if (realFlpDir.empty())
  8939. return {};
  8940. return (std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "Kepler" / "engine_x64.dll").string();
  8941. }
  8942. TEST (kepler_engine)
  8943. {
  8944. const auto st = keplerTestState();
  8945. CHECK (st.size() == 178 && lynx::keplerStateUsable (st));
  8946. CHECK (lynx::keplerPanelOffset (st, 19) == 0x21 + 4 * 19 && lynx::keplerPanelOffset (st, 34) == -1);
  8947. auto broken = st;
  8948. broken[17] = 'X'; // not "Kepler-1"
  8949. CHECK (! lynx::keplerStateUsable (broken) && lynx::keplerPanelOffset (broken, 0) == -1);
  8950. auto v3 = st;
  8951. v3[0x19] = 3; // version 3 needs the gate / noise / oversampling bytes
  8952. CHECK (! lynx::keplerStateUsable (v3));
  8953. const auto dll = keplerDll();
  8954. if (dll.empty() || ! lynx::keplerDllUsable (dll))
  8955. return; // no FL installation (the engine needs the user's own DLL tables)
  8956. // against FL's engine DLL (EXEC, REA_rodada46 §6): three notes, 44.1 kHz, host blocks of 512 — bit-exact
  8957. 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 } };
  8958. const auto y = lynx::kepler::renderReference (st, dll, notes, 44100.0, 39690);
  8959. CHECK (y.size() == 2 * 39690);
  8960. if (y.size() != 2 * 39690)
  8961. return;
  8962. double el = 0.0, er = 0.0;
  8963. for (std::size_t i = 0; i < y.size(); i += 2)
  8964. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  8965. std::printf (" Kepler default patch: energy %.9g %.9g (FL's DLL 250.641268)\n", el, er);
  8966. CHECK (std::abs (el - 250.641268) < 1e-6 && std::abs (er - 250.641268) < 1e-6);
  8967. const std::tuple<int, float, float> golden[] { { 5000, 0.0467273332f, 0.0467273332f }, { 12000, -0.119412214f, -0.119412214f },
  8968. { 25000, 0.0855825394f, 0.0855825394f }, { 35000, -3.34166579e-08f, -3.34166579e-08f } };
  8969. for (const auto& [i, l, r] : golden)
  8970. CHECK (y[2 * (std::size_t) i] == l && y[2 * (std::size_t) i + 1] == r);
  8971. // the instrument: FL's wrapper velocity, channel volume as an output gain, idle after the release
  8972. lynx::ChannelDef c;
  8973. c.instrument = lynx::InstrumentKind::kepler;
  8974. c.pluginState = st;
  8975. c.samplePath = dll;
  8976. auto inst = lynx::makeInstrument (lynx::InstrumentKind::kepler);
  8977. inst->configure (c, 120.0);
  8978. inst->prepare (44100.0);
  8979. std::vector<float> l (4096), r (4096);
  8980. inst->noteOn (60, 0.8f);
  8981. double e = 0.0;
  8982. bool finite = true;
  8983. for (int b = 0; b < 8; ++b)
  8984. {
  8985. std::fill (l.begin(), l.end(), 0.0f);
  8986. std::fill (r.begin(), r.end(), 0.0f);
  8987. inst->render (l.data(), r.data(), 4096, 0.5f, 0.5f);
  8988. for (int i = 0; i < 4096; ++i)
  8989. 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];
  8990. if (b == 3)
  8991. inst->noteOff (60);
  8992. }
  8993. CHECK (finite && e > 1.0);
  8994. for (int b = 0; b < 40; ++b)
  8995. inst->render (l.data(), r.data(), 4096, 0.5f, 0.5f);
  8996. CHECK (inst->idle());
  8997. }
  8998. // REA round 57 §2: Transistor Bass
  8999. // the plugin state (event 0xD5) of an FL preset file (.fst: the FLhd/FLdt event stream)
  9000. std::vector<std::uint8_t> tbassPresetState (const char* name)
  9001. {
  9002. if (realFlpDir.empty())
  9003. return {};
  9004. std::ifstream in (std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "Transistor Bass" / name, std::ios::binary);
  9005. const std::vector<std::uint8_t> b ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  9006. std::size_t i = 0;
  9007. while (i + 8 <= b.size() && std::memcmp (b.data() + i, "FLdt", 4) != 0)
  9008. ++i;
  9009. i += 8;
  9010. while (i < b.size())
  9011. {
  9012. const auto id = b[i++];
  9013. std::size_t n = id < 64 ? 1 : id < 128 ? 2 : id < 192 ? 4 : 0;
  9014. if (id >= 192)
  9015. for (int shift = 0; i < b.size(); shift += 7)
  9016. {
  9017. const auto c = b[i++];
  9018. n |= (std::size_t) (c & 0x7F) << shift;
  9019. if (c < 0x80)
  9020. break;
  9021. }
  9022. if (id == 0xD5 && i + n <= b.size())
  9023. return { b.begin() + (std::ptrdiff_t) i, b.begin() + (std::ptrdiff_t) (i + n) };
  9024. i += n;
  9025. }
  9026. return {};
  9027. }
  9028. // the same state re-saved as engine-stream version 5 with an engine selector (REA_rodada57 §2.2)
  9029. std::vector<std::uint8_t> tbassWithEngine (const std::vector<std::uint8_t>& s, std::int32_t engine)
  9030. {
  9031. std::vector<std::uint8_t> out (s.begin(), s.begin() + 4);
  9032. auto put = [&] (const void* p, std::size_t n) { out.insert (out.end(), (const std::uint8_t*) p, (const std::uint8_t*) p + n); };
  9033. for (std::size_t at = 4; at + 12 <= s.size();)
  9034. {
  9035. std::uint32_t id;
  9036. std::uint64_t n;
  9037. std::memcpy (&id, s.data() + at, 4);
  9038. std::memcpy (&n, s.data() + at + 4, 8);
  9039. std::vector<std::uint8_t> d (s.begin() + (std::ptrdiff_t) (at + 12), s.begin() + (std::ptrdiff_t) (at + 12 + n));
  9040. at += 12 + (std::size_t) n;
  9041. if (id == 2)
  9042. {
  9043. std::uint64_t m;
  9044. std::memcpy (&m, d.data() + 4, 8);
  9045. std::vector<std::uint8_t> e (d.begin() + 12, d.begin() + 12 + (std::ptrdiff_t) m);
  9046. std::int16_t v;
  9047. std::memcpy (&v, e.data(), 2);
  9048. if (v < 5)
  9049. {
  9050. v = 5;
  9051. std::memcpy (e.data(), &v, 2);
  9052. e.insert (e.end(), 4, 0);
  9053. }
  9054. std::memcpy (e.data() + e.size() - 4, &engine, 4);
  9055. m = e.size();
  9056. d.resize (12);
  9057. std::memcpy (d.data() + 4, &m, 8);
  9058. d.insert (d.end(), e.begin(), e.end());
  9059. }
  9060. const std::uint64_t size = d.size();
  9061. put (&id, 4);
  9062. put (&size, 8);
  9063. put (d.data(), d.size());
  9064. }
  9065. return out;
  9066. }
  9067. std::string tbassDll()
  9068. {
  9069. if (realFlpDir.empty())
  9070. return {};
  9071. return (std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "Transistor Bass" / "engine_x64.dll").string();
  9072. }
  9073. // Plucked! (REA_rodada65 §1): the C++ engine against FL's DLL (EXEC: same notes, the DLL's RandSeed set before the first
  9074. // note, the host killing voices when the plugin asks). LYNX renders these bit-identically to the DLL.
  9075. TEST (plucked_engine)
  9076. {
  9077. auto stateOf = [] (std::initializer_list<std::int32_t> k) {
  9078. std::vector<std::uint8_t> st (20);
  9079. std::memcpy (st.data(), std::data (k), 20);
  9080. return st;
  9081. };
  9082. CHECK (! lynx::pluckedStateUsable (std::vector<std::uint8_t> (19)));
  9083. CHECK (lynx::pluckedStateUsable (stateOf ({ 128, 64, 1, 0, 1 })));
  9084. CHECK (lynx::pluckedParams (stateOf ({ 7, 9, 0, 1, 0 })) == std::vector<int> ({ 7, 9, 0, 1, 0 }));
  9085. using N = lynx::plucked::RefNote;
  9086. struct Sample { int at; float l, r; };
  9087. struct Golden { int set; std::int32_t P[5]; double fs; int block, law, maxPoly; double el, er; Sample s[4]; };
  9088. const std::vector<std::vector<N>> sets {
  9089. { { 0, 60, 1.0, 0.0, 0.0, 0.0, 0.0, -1 } },
  9090. { { 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 } },
  9091. { { 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 } },
  9092. { { 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 } },
  9093. { { 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 } },
  9094. };
  9095. const Golden goldens[] {
  9096. { 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 } } },
  9097. { 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 } } },
  9098. { 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 } } },
  9099. { 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 } } },
  9100. { 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 } } },
  9101. };
  9102. for (const auto& g : goldens)
  9103. {
  9104. const auto st = stateOf ({ g.P[0], g.P[1], g.P[2], g.P[3], g.P[4] });
  9105. const auto length = (int) g.fs * 2;
  9106. const auto y = lynx::plucked::renderReference (st, sets[(std::size_t) g.set], g.fs, length, g.block, 777u + (std::uint32_t) g.set,
  9107. g.law != 0, g.maxPoly);
  9108. double el = 0.0, er = 0.0;
  9109. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  9110. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  9111. 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;
  9112. for (const auto& smp : g.s)
  9113. 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;
  9114. if (! ok)
  9115. 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);
  9116. CHECK (ok);
  9117. }
  9118. // the instrument: plays, the gate makes a note-off silence the string, a held string dies out by itself
  9119. lynx::ChannelDef c;
  9120. c.instrument = lynx::InstrumentKind::plucked;
  9121. c.pluginState = stateOf ({ 200, 40, 1, 1, 1 });
  9122. auto inst = lynx::makeInstrument (lynx::InstrumentKind::plucked);
  9123. inst->configure (c, 120.0);
  9124. inst->prepare (44100.0);
  9125. std::vector<float> l (1024), r (1024);
  9126. inst->noteOn (57, 0.8f);
  9127. double e = 0.0;
  9128. for (int b = 0; b < 8; ++b)
  9129. {
  9130. std::fill (l.begin(), l.end(), 0.0f);
  9131. std::fill (r.begin(), r.end(), 0.0f);
  9132. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  9133. for (int i = 0; i < 1024; ++i)
  9134. e += (double) l[(std::size_t) i] * l[(std::size_t) i] + (double) r[(std::size_t) i] * r[(std::size_t) i];
  9135. }
  9136. CHECK (e > 1.0 && ! inst->idle());
  9137. inst->noteOff (57);
  9138. for (int b = 0; b < 4; ++b)
  9139. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  9140. CHECK (inst->idle());
  9141. c.pluginState = stateOf ({ 0, 64, 0, 0, 0 });
  9142. inst->configure (c, 120.0);
  9143. inst->noteOn (60, 1.0f);
  9144. inst->noteOff (60); // (no gate: the string rings on)
  9145. for (int b = 0; b < 2000 && ! inst->idle(); ++b)
  9146. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  9147. CHECK (inst->idle());
  9148. }
  9149. // BeepMap (REA_rodada65 §2): the C++ engine against FL's DLL (EXEC: same notes, the DLL's RandSeed set before the first
  9150. // note, the host killing voices when the plugin asks): a synthetic 9×6 bitmap with every Scale mode and switch, and two
  9151. // of the installation's presets. LYNX renders these bit-identically to the DLL.
  9152. std::vector<std::uint8_t> beepMapTestState (std::initializer_list<std::int32_t> settings)
  9153. {
  9154. constexpr int W = 9, H = 6;
  9155. std::vector<std::uint8_t> img (54 + 4 * W * H, 0);
  9156. 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)); };
  9157. img[0] = 'B', img[1] = 'M';
  9158. 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);
  9159. put (34, 4 * W * H, 4);
  9160. for (int y = 0; y < H; ++y) // bottom-up rows
  9161. for (int x = 0; x < W; ++x)
  9162. {
  9163. const auto yy = H - 1 - y;
  9164. auto* px = img.data() + 54 + 4 * (y * W + x);
  9165. px[0] = (std::uint8_t) ((x * 91 + yy * 29) & 255), px[1] = (std::uint8_t) ((x * 13 + yy * 71) & 255);
  9166. px[2] = (std::uint8_t) ((x * 37 + yy * 11) & 255), px[3] = 255;
  9167. }
  9168. std::vector<std::uint8_t> st (4 + 60 + 4, 0);
  9169. std::size_t k = 0;
  9170. for (auto v : settings)
  9171. std::memcpy (st.data() + 4 + 4 * k++, &v, 4);
  9172. const auto len = (std::int32_t) img.size();
  9173. std::memcpy (st.data() + 64, &len, 4);
  9174. st.insert (st.end(), img.begin(), img.end());
  9175. return st;
  9176. }
  9177. // the 0xD5 state of one of the installation's BeepMap presets (empty without an installation)
  9178. std::vector<std::uint8_t> beepMapPresetState (const char* name)
  9179. {
  9180. if (realFlpDir.empty())
  9181. return {};
  9182. std::ifstream in (std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "BeepMap" / name, std::ios::binary);
  9183. const std::vector<std::uint8_t> b ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  9184. std::size_t i = 0;
  9185. while (i + 8 <= b.size() && std::memcmp (b.data() + i, "FLdt", 4) != 0)
  9186. ++i;
  9187. i += 8;
  9188. while (i < b.size())
  9189. {
  9190. const auto id = b[i++];
  9191. std::size_t n = id < 64 ? 1 : id < 128 ? 2 : id < 192 ? 4 : 0;
  9192. if (id >= 192)
  9193. for (int shift = 0; i < b.size(); shift += 7)
  9194. {
  9195. const auto c = b[i++];
  9196. n |= (std::size_t) (c & 0x7F) << shift;
  9197. if (c < 0x80)
  9198. break;
  9199. }
  9200. if (id == 0xD5 && i + n <= b.size())
  9201. return { b.begin() + (std::ptrdiff_t) i, b.begin() + (std::ptrdiff_t) (i + n) };
  9202. i += n;
  9203. }
  9204. return {};
  9205. }
  9206. TEST (beepmap_engine)
  9207. {
  9208. const auto synthA = beepMapTestState ({ 3000, 200, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64 });
  9209. CHECK (lynx::beepMapStateUsable (synthA));
  9210. CHECK (lynx::beepMapParams (synthA)->at (1) == 200 && lynx::beepMapParamOffset (synthA, 2) == 12);
  9211. auto broken = synthA;
  9212. broken[0] = 2; // versions above 1 are refused
  9213. CHECK (! lynx::beepMapStateUsable (broken));
  9214. broken = synthA;
  9215. broken.resize (broken.size() - 20); // truncated bitmap
  9216. CHECK (! lynx::beepMapStateUsable (broken));
  9217. using N = lynx::beepmap::RefNote;
  9218. struct Sample { int at; float l, r; };
  9219. struct Golden { int set; double fs; int block; double el, er; Sample s[4]; };
  9220. std::vector<std::vector<std::uint8_t>> states {
  9221. synthA,
  9222. beepMapTestState ({ 5000, 600, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 64 }),
  9223. beepMapTestState ({ 8000, 100, 2, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 64 }),
  9224. beepMapTestState ({ 1000, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 64 }),
  9225. beepMapPresetState ("Default.fst"),
  9226. beepMapPresetState ("Voice.fst"),
  9227. };
  9228. const std::vector<std::vector<N>> sets {
  9229. { { 0, 60, 1.0, 0.0, 0.0, -1 } },
  9230. { { 0, 57, 0.8, 0.3, 25.0, -1 }, { 7000, 64, 0.6, -0.5, 0.0, -1 } },
  9231. { { 0, 48, 1.0, 0.0, 0.0, -1 }, { 300, 72, 0.5, 0.0, -40.0, -1 } },
  9232. { { 0, 67, 1.0, -0.2, 0.0, -1 } },
  9233. { { 0, 60, 1.0, 0.0, 0.0, -1 } },
  9234. { { 0, 55, 0.8, 0.0, 0.0, -1 }, { 20000, 62, 0.8, 0.4, 0.0, -1 } },
  9235. };
  9236. const Golden goldens[] {
  9237. { 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 } } },
  9238. { 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 } } },
  9239. { 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 } } },
  9240. { 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 } } },
  9241. { 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 } } },
  9242. { 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 } } },
  9243. };
  9244. for (const auto& g : goldens)
  9245. {
  9246. const auto& st = states[(std::size_t) g.set];
  9247. if (st.empty())
  9248. continue; // (no FL installation: the presets are skipped)
  9249. const auto length = (int) g.fs * 2;
  9250. const auto y = lynx::beepmap::renderReference (st, sets[(std::size_t) g.set], g.fs, length, g.block, 4321u + (std::uint32_t) g.set);
  9251. double el = 0.0, er = 0.0;
  9252. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  9253. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  9254. 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;
  9255. for (const auto& smp : g.s)
  9256. 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;
  9257. if (! ok)
  9258. 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);
  9259. CHECK (ok);
  9260. }
  9261. // the instrument: plays the image, ends after the last column, a note-off fades the voice out
  9262. lynx::ChannelDef c;
  9263. c.instrument = lynx::InstrumentKind::beepmap;
  9264. c.pluginState = states[1];
  9265. auto inst = lynx::makeInstrument (lynx::InstrumentKind::beepmap);
  9266. inst->configure (c, 120.0);
  9267. inst->prepare (44100.0);
  9268. std::vector<float> l (1024), r (1024);
  9269. inst->noteOn (60, 0.8f);
  9270. double e = 0.0;
  9271. for (int b = 0; b < 8; ++b)
  9272. {
  9273. std::fill (l.begin(), l.end(), 0.0f);
  9274. std::fill (r.begin(), r.end(), 0.0f);
  9275. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  9276. for (int i = 0; i < 1024; ++i)
  9277. e += (double) l[(std::size_t) i] * l[(std::size_t) i] + (double) r[(std::size_t) i] * r[(std::size_t) i];
  9278. }
  9279. CHECK (e > 0.01 && ! inst->idle()); // (Loop on)
  9280. inst->noteOff (60);
  9281. for (int b = 0; b < 4; ++b)
  9282. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  9283. CHECK (inst->idle());
  9284. c.pluginState = synthA; // (no loop: 9 columns of 64·(e^(200/4096·ln 33) − 1)… samples, then the voice ends)
  9285. inst->configure (c, 120.0);
  9286. inst->noteOn (60, 1.0f);
  9287. for (int b = 0; b < 200 && ! inst->idle(); ++b)
  9288. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  9289. CHECK (inst->idle());
  9290. }
  9291. // ---- Sawer (REA_rodada65 §3): installation presets and data.saw read at run time, fingerprints of engine_x64.dll ----
  9292. namespace
  9293. {
  9294. std::vector<std::uint8_t> sawerPresetState (const char* rel)
  9295. {
  9296. if (realFlpDir.empty())
  9297. return {};
  9298. std::ifstream in (std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "Sawer" / rel, std::ios::binary);
  9299. const std::vector<std::uint8_t> b ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  9300. std::size_t i = 0;
  9301. while (i + 8 <= b.size() && std::memcmp (b.data() + i, "FLdt", 4) != 0)
  9302. ++i;
  9303. i += 8;
  9304. while (i < b.size())
  9305. {
  9306. const auto id = b[i++];
  9307. std::size_t n = id < 64 ? 1 : id < 128 ? 2 : id < 192 ? 4 : 0;
  9308. if (id >= 192)
  9309. for (int shift = 0; i < b.size(); shift += 7)
  9310. {
  9311. const auto c = b[i++];
  9312. n |= (std::size_t) (c & 0x7F) << shift;
  9313. if (c < 0x80)
  9314. break;
  9315. }
  9316. if (id == 0xD5 && i + n <= b.size())
  9317. return { b.begin() + (std::ptrdiff_t) i, b.begin() + (std::ptrdiff_t) (i + n) };
  9318. i += n;
  9319. }
  9320. return {};
  9321. }
  9322. } // namespace
  9323. TEST (sawer_engine)
  9324. {
  9325. // a synthetic header: "SSLF" + size + "1rwS" + 96 floats
  9326. std::vector<std::uint8_t> synth (12 + 4 * 96, 0);
  9327. std::memcpy (synth.data(), "SSLF", 4);
  9328. const std::uint32_t size = 4 + 4 * 96;
  9329. std::memcpy (synth.data() + 4, &size, 4);
  9330. std::memcpy (synth.data() + 8, "1rwS", 4);
  9331. const float p27 = 0.75f;
  9332. std::memcpy (synth.data() + 12 + 4 * 27, &p27, 4);
  9333. CHECK (lynx::sawerStateUsable (synth));
  9334. CHECK (lynx::sawerParam (synth, 27) == 0.75f && lynx::sawerParamOffset (synth, 27) == 12 + 4 * 27);
  9335. CHECK (! lynx::sawerParam (synth, 96) && lynx::sawerParamOffset (synth, -1) < 0);
  9336. auto broken = synth;
  9337. broken[8] = 'X';
  9338. CHECK (! lynx::sawerStateUsable (broken));
  9339. CHECK (! lynx::sawerStateUsable (std::vector<std::uint8_t> (synth.begin(), synth.end() - 4)));
  9340. const auto dflt = sawerPresetState ("Default.fst");
  9341. const auto slap = sawerPresetState ("Arpeggios/FG Velocity Slapper.fst");
  9342. const auto tablePath = realFlpDir.empty() ? std::string() : (std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "Sawer" / "data.saw").string();
  9343. const auto table = tablePath.empty() ? std::vector<float> {} : lynx::sawerReadTable (tablePath);
  9344. if (dflt.empty() || slap.empty() || table.size() != 8192u)
  9345. {
  9346. std::printf (" (sawer_engine: FL Studio installation not found, DLL fingerprints skipped)\n");
  9347. return;
  9348. }
  9349. CHECK (lynx::sawerStateUsable (dflt) && lynx::sawerStateUsable (slap));
  9350. // DLL fingerprints (py/gold_sw.py: the EXEC harness, L = R = Final.Vol); "syn" = Default with our own settings
  9351. // (unison 4, sub pulse at 0.6, sync, band-pass, noise, resonance, chorus)
  9352. auto setP = [] (std::vector<std::uint8_t> s, int k, float v) { std::memcpy (s.data() + 12 + 4 * k, &v, 4); return s; };
  9353. auto syn = dflt;
  9354. 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 },
  9355. { 26, 0.3f }, { 28, 0.7f }, { 45, 0.5f }, { 46, 0.0f } })
  9356. syn = setP (syn, kv.first, kv.second);
  9357. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  9358. using N = lynx::sawer::RefNote;
  9359. struct Sample { int at; float l, r; };
  9360. struct Golden { const std::vector<std::uint8_t>* state; std::vector<N> notes; double fs; int block; double el, er; Sample s[4]; };
  9361. const Golden goldens[] {
  9362. { &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 } },
  9363. 44100, 512, 1172.343107, 1169.673445,
  9364. { { 1000, 0.0349505879f, 0.0349650607f }, { 10000, -0.0326882228f, -0.0326587185f }, { 30000, 0.139422998f, 0.139268339f }, { 60000, -1.01108653e-05f, -1.01210762e-05f } } },
  9365. { &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,
  9366. { { 1000, 0.00919434987f, 0.0769683793f }, { 10000, 0.0363935083f, 0.0227626693f }, { 30000, -0.00462511741f, -0.0105419969f }, { 60000, -0.00727002183f, -0.00159690797f } } },
  9367. { &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,
  9368. { { 1000, -0.162745371f, -0.172861978f }, { 10000, 0.0468756184f, 0.112415045f }, { 30000, -0.123887017f, -0.132816017f }, { 60000, 0.0f, 0.0f } } },
  9369. };
  9370. for (const auto& g : goldens)
  9371. {
  9372. const auto y = lynx::sawer::renderReference (*g.state, table, g.notes, g.fs, 66150, g.block);
  9373. CHECK (y.size() == 2 * 66150u);
  9374. if (y.size() != 2 * 66150u)
  9375. continue;
  9376. double el = 0.0, er = 0.0;
  9377. for (std::size_t i = 0; i < 66150; ++i)
  9378. el += (double) y[2 * i] * y[2 * i], er += (double) y[2 * i + 1] * y[2 * i + 1];
  9379. CHECK (std::abs (el - g.el) < 1e-5 * g.el && std::abs (er - g.er) < 1e-5 * g.er);
  9380. for (const auto& s : g.s)
  9381. 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);
  9382. }
  9383. // the instrument: the same engine behind LYNX's channel gains (centre → law A), velocity → Init.Vol; it goes idle
  9384. lynx::ChannelDef c;
  9385. c.instrument = lynx::InstrumentKind::sawer;
  9386. c.pluginState = dflt;
  9387. c.samplePath = tablePath;
  9388. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sawer);
  9389. inst->configure (c, 120.0);
  9390. inst->prepare (44100.0);
  9391. std::vector<float> l (512 * 40, 0.0f), r (512 * 40, 0.0f);
  9392. for (int b = 0; b < 40; ++b)
  9393. {
  9394. if (b == 0)
  9395. inst->noteOn (60, 0.78125f);
  9396. if (b == 10)
  9397. inst->noteOff (60);
  9398. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  9399. }
  9400. 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,
  9401. 8192, 512, 120.0, 0, true);
  9402. double diff = 0.0, energy = 0.0;
  9403. for (int i = 0; i < 8192; ++i)
  9404. {
  9405. 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]) });
  9406. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  9407. }
  9408. CHECK (diff == 0.0 && energy > 1e-3);
  9409. CHECK (inst->idle());
  9410. }
  9411. TEST (transistorbass_engine)
  9412. {
  9413. const auto factory = tbassPresetState ("Factory programs.fst");
  9414. const auto seqOff = tbassPresetState ("Sequencer off.fst");
  9415. if (factory.empty() || seqOff.empty())
  9416. return; // no FL installation
  9417. CHECK (factory.size() == 197887 && seqOff.size() == 196051);
  9418. CHECK (lynx::transistorBassStateUsable (factory) && lynx::transistorBassStateUsable (seqOff));
  9419. CHECK (lynx::transistorBassStateUsable (tbassWithEngine (factory, 2)));
  9420. auto broken = factory;
  9421. broken[0] = 3; // the wrapper refuses versions above 2
  9422. CHECK (! lynx::transistorBassStateUsable (broken));
  9423. auto truncated = factory;
  9424. truncated.resize (truncated.size() - 1000);
  9425. CHECK (! lynx::transistorBassStateUsable (truncated));
  9426. const auto dll = tbassDll();
  9427. if (dll.empty() || ! lynx::transistorBassDllUsable (dll))
  9428. return;
  9429. // against FL's engine DLL (EXEC, REA_rodada57 §2.11): programs of the factory bank (sequencer, accents, slides,
  9430. // distortion, delay, reverb) on the three engines and the sequencer-off preset with legato and accent notes; host
  9431. // blocks of 512 at 44.1 kHz, tempo 125. LYNX differs from the DLL by −121 dB or less here (library pow rounding).
  9432. struct Sample { int at; float l, r; };
  9433. struct Golden { const char* preset; int engine, key; double el, er; Sample s[4]; };
  9434. const Golden goldens[] {
  9435. { "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 } } },
  9436. { "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 } } },
  9437. { "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 } } },
  9438. { "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 } } },
  9439. { "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 } } },
  9440. { "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 } } },
  9441. { "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 } } },
  9442. { "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 } } },
  9443. { "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 } } },
  9444. { "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 } } },
  9445. };
  9446. constexpr int length = 88200;
  9447. for (const auto& g : goldens)
  9448. {
  9449. const bool factoryBank = std::string (g.preset) == "Factory";
  9450. auto st = factoryBank ? factory : seqOff;
  9451. if (g.engine >= 0)
  9452. st = tbassWithEngine (st, g.engine);
  9453. std::vector<lynx::tbass::RefNote> notes;
  9454. if (factoryBank)
  9455. notes.push_back ({ 0, g.key, 0.5, 0.0, 0.0, 0.0, 0.0, 70560 });
  9456. else
  9457. {
  9458. notes.push_back ({ 0, g.key, 0.5, 0.0, 0.0, 0.0, 0.0, 30000 });
  9459. notes.push_back ({ 20000, g.key + 7, 1.6, 0.0, 0.0, 0.0, 0.0, 50000 });
  9460. notes.push_back ({ 45000, g.key - 5, 0.3, 0.0, 0.0, 0.0, 0.0, 70560 });
  9461. }
  9462. const auto y = lynx::tbass::renderReference (st, dll, notes, 44100.0, length);
  9463. double el = 0.0, er = 0.0, peak = 0.0;
  9464. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  9465. 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]) });
  9466. 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;
  9467. for (const auto& smp : g.s)
  9468. 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;
  9469. if (! ok)
  9470. 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);
  9471. CHECK (ok);
  9472. }
  9473. // the instrument: FL's wrapper velocity, the channel volume as an output gain, the sequencer plays while the key is held
  9474. lynx::ChannelDef c;
  9475. c.instrument = lynx::InstrumentKind::transistorbass;
  9476. c.pluginState = factory;
  9477. c.samplePath = dll;
  9478. auto inst = lynx::makeInstrument (lynx::InstrumentKind::transistorbass);
  9479. inst->configure (c, 128.0);
  9480. inst->prepare (44100.0);
  9481. std::vector<float> l (4096), r (4096);
  9482. inst->noteOn (65, 0.8f);
  9483. double e = 0.0;
  9484. bool finite = true;
  9485. for (int b = 0; b < 24; ++b)
  9486. {
  9487. std::fill (l.begin(), l.end(), 0.0f);
  9488. std::fill (r.begin(), r.end(), 0.0f);
  9489. inst->render (l.data(), r.data(), 4096, 0.5f, 0.5f);
  9490. for (int i = 0; i < 4096; ++i)
  9491. 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];
  9492. if (b == 20)
  9493. inst->noteOff (65);
  9494. }
  9495. CHECK (finite && e > 1.0 && ! inst->idle());
  9496. for (int b = 0; b < 400 && ! inst->idle(); ++b)
  9497. inst->render (l.data(), r.data(), 4096, 0.5f, 0.5f);
  9498. CHECK (inst->idle());
  9499. }
  9500. TEST (kepler_demo_channels)
  9501. {
  9502. // every Kepler channel of FL's demo projects on LYNX's engine against FL's engine DLL (EXEC fingerprints, REA_rodada46 §6;
  9503. // the engine differs from the DLL by −127 dB or less on these, from the DLL library's pow rounding)
  9504. const auto dll = keplerDll();
  9505. if (dll.empty() || ! lynx::keplerDllUsable (dll))
  9506. return;
  9507. struct Sample { int at; float l, r; };
  9508. struct Golden { const char* file; const char* channel; std::size_t stateSize; double el, er; Sample s[4]; };
  9509. const Golden goldens[] {
  9510. { "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 } } },
  9511. { "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 } } },
  9512. { "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 } } },
  9513. { "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 } } },
  9514. };
  9515. int checked = 0;
  9516. for (const auto& g : goldens)
  9517. {
  9518. std::ifstream in (std::filesystem::path (realFlpDir) / "Demo projects" / g.file, std::ios::binary);
  9519. const std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  9520. if (bytes.empty())
  9521. continue;
  9522. const flp::Channel* ch = nullptr;
  9523. const auto project = flp::parseProject (bytes);
  9524. for (const auto& c : project.channels)
  9525. if (c.pluginName == "Kepler" && c.name == g.channel)
  9526. ch = &c;
  9527. CHECK (ch != nullptr);
  9528. if (ch == nullptr)
  9529. continue;
  9530. CHECK (ch->pluginState.size() == g.stateSize && lynx::keplerStateUsable (ch->pluginState));
  9531. 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 },
  9532. { 16000, 55, 0.95, 0.0, 0.0, 30000 } }, 44100.0, 39690);
  9533. double el = 0.0, er = 0.0, peak = 0.0;
  9534. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  9535. 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]) });
  9536. bool ok = y.size() == 2 * 39690 && std::abs (el - g.el) <= 1e-5 * g.el && std::abs (er - g.er) <= 1e-5 * g.er;
  9537. for (const auto& smp : g.s)
  9538. 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;
  9539. if (! ok)
  9540. 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);
  9541. CHECK (ok);
  9542. ++checked;
  9543. }
  9544. std::printf (" Kepler: %d demo channels checked against FL's engine DLL\n", checked);
  9545. CHECK (checked == (int) std::size (goldens));
  9546. }
  9547. TEST (ogun_demo_channels)
  9548. {
  9549. // every Ogun channel of FL's demo projects (with its plugin effects) against the round-37 reference model, which is
  9550. // 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),
  9551. // 39 690 samples at 44.1 kHz, at the state's quality and at the export quality 4. Fingerprint: energy per side and
  9552. // four samples (the engine matches the model within float rounding, −125 dB or better on the whole signal).
  9553. if (realFlpDir.empty())
  9554. return;
  9555. struct Sample { int at; float l, r; };
  9556. struct Golden { const char* file; int ordinal; std::size_t stateSize; bool export4; double el, er; Sample s[4]; };
  9557. const Golden goldens[] {
  9558. { "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
  9559. { "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
  9560. { "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
  9561. { "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
  9562. { "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
  9563. { "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
  9564. { "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
  9565. { "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
  9566. { "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
  9567. { "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
  9568. { "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
  9569. { "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
  9570. { "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
  9571. { "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
  9572. { "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
  9573. { "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
  9574. { "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
  9575. { "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
  9576. { "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
  9577. { "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
  9578. { "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
  9579. { "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
  9580. { "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
  9581. { "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
  9582. { "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
  9583. { "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
  9584. { "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
  9585. { "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
  9586. { "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
  9587. { "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
  9588. { "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
  9589. { "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
  9590. { "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
  9591. { "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
  9592. { "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
  9593. { "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
  9594. { "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
  9595. { "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
  9596. { "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
  9597. { "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
  9598. };
  9599. std::map<std::string, std::vector<std::vector<std::uint8_t>>> states;
  9600. int checked = 0;
  9601. for (const auto& g : goldens)
  9602. {
  9603. if (! states.contains (g.file))
  9604. {
  9605. std::ifstream in (std::filesystem::path (realFlpDir) / "Demo projects" / g.file, std::ios::binary);
  9606. const std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  9607. auto& list = states[g.file];
  9608. if (! bytes.empty())
  9609. for (const auto& c : flp::parseProject (bytes).channels)
  9610. if (c.pluginName == "Ogun")
  9611. list.push_back (c.pluginState);
  9612. }
  9613. const auto& list = states[g.file];
  9614. if (list.empty())
  9615. continue; // not this installation
  9616. CHECK ((int) list.size() > g.ordinal);
  9617. if ((int) list.size() <= g.ordinal)
  9618. continue;
  9619. const auto& st = list[(std::size_t) g.ordinal];
  9620. CHECK (st.size() == g.stateSize);
  9621. lynx::ogun::RefOptions opt;
  9622. opt.quality = g.export4 ? 4 : -1;
  9623. const auto y = lynx::ogun::renderReference (st, { { 0, 60, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 20000 },
  9624. { 8000, 67, 0.6, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 26000 },
  9625. { 16000, 55, 0.95, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 30000 } }, 44100.0, 39690, opt);
  9626. double el = 0.0, er = 0.0, peak = 0.0;
  9627. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  9628. 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]) });
  9629. bool ok = y.size() == 2 * 39690 && std::abs (el - g.el) <= 1e-5 * g.el && std::abs (er - g.er) <= 1e-5 * g.er;
  9630. for (const auto& smp : g.s)
  9631. 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;
  9632. if (! ok)
  9633. std::printf (" Ogun %s #%d (q%s) differs from the model: energy %.9g %.9g (model %.9g %.9g)\n", g.file, g.ordinal,
  9634. g.export4 ? "4" : "-state", el, er, g.el, g.er);
  9635. CHECK (ok);
  9636. ++checked;
  9637. }
  9638. std::printf (" Ogun: %d demo renders checked against the reference model\n", checked);
  9639. CHECK (checked == (int) std::size (goldens));
  9640. }
  9641. TEST (autogun_template_channels)
  9642. {
  9643. // REA_rodada57: every distinct Autogun state of the installation's projects (templates and the render test), with its
  9644. // effects and the Magic Dust stage (SG.dat from the installation), three notes (C5 1.0 0–20000, G5 0.6 8000–26000,
  9645. // 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
  9646. // goldens are renders of the installation's Autogun_x64.dll (LYNX is within −127 dB of them); the render-test state
  9647. // uses Seed 2 "Random", so its golden is LYNX's own render with the counter at 0 (a fingerprint of the engine).
  9648. if (realFlpDir.empty())
  9649. return;
  9650. const auto sgPath = std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "Autogun" / "Data" / "SG.dat";
  9651. std::ifstream sgIn (sgPath, std::ios::binary);
  9652. const std::vector<std::uint8_t> sgState ((std::istreambuf_iterator<char> (sgIn)), std::istreambuf_iterator<char>());
  9653. if (sgState.size() != 1342)
  9654. return; // not this installation
  9655. struct Sample { int at; float l, r; };
  9656. struct Golden { const char* file; std::size_t stateSize; bool export4; double el, er; Sample s[4]; };
  9657. const Golden goldens[] {
  9658. { "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)
  9659. { "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)
  9660. { "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)
  9661. { "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)
  9662. { "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)
  9663. { "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)
  9664. { "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)
  9665. { "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)
  9666. { "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)
  9667. { "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)
  9668. };
  9669. int checked = 0;
  9670. for (const auto& g : goldens)
  9671. {
  9672. std::ifstream in (std::filesystem::path (realFlpDir) / g.file, std::ios::binary);
  9673. const std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  9674. if (bytes.empty())
  9675. continue;
  9676. std::vector<std::uint8_t> st;
  9677. for (const auto& c : flp::parseProject (bytes).channels)
  9678. if (c.pluginName == "Autogun" && st.empty())
  9679. st = c.pluginState;
  9680. CHECK (st.size() == g.stateSize);
  9681. CHECK (lynx::autogunStateUsable (st));
  9682. lynx::ogun::RefOptions opt;
  9683. opt.autogun = true;
  9684. opt.sgState = sgState;
  9685. opt.quality = g.export4 ? 4 : -1;
  9686. const auto y = lynx::ogun::renderReference (st, { { 0, 60, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 20000 },
  9687. { 8000, 67, 0.6, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 26000 },
  9688. { 16000, 55, 0.95, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 30000 } }, 44100.0, 39690, opt);
  9689. double el = 0.0, er = 0.0, peak = 0.0;
  9690. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  9691. 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]) });
  9692. bool ok = y.size() == 2 * 39690 && std::abs (el - g.el) <= 1e-5 * g.el && std::abs (er - g.er) <= 1e-5 * g.er;
  9693. for (const auto& smp : g.s)
  9694. 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;
  9695. if (! ok)
  9696. 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);
  9697. CHECK (ok);
  9698. ++checked;
  9699. }
  9700. std::printf (" Autogun: %d template/render-test renders checked (DLL goldens for the fixed-spectrum states)\n", checked);
  9701. CHECK (checked == (int) std::size (goldens));
  9702. // the instrument (SG.dat read from ChannelDef::samplePath) plays like the reference renderer, then goes idle
  9703. {
  9704. std::ifstream in (std::filesystem::path (realFlpDir) / "Templates" / "Advanced" / "Electronic" / "Trap.flp", std::ios::binary);
  9705. const std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  9706. lynx::ChannelDef c;
  9707. c.instrument = lynx::InstrumentKind::autogun;
  9708. for (const auto& ch : flp::parseProject (bytes).channels)
  9709. if (ch.pluginName == "Autogun")
  9710. c.pluginState = ch.pluginState;
  9711. c.samplePath = sgPath.string();
  9712. lynx::ogun::RefOptions opt;
  9713. opt.autogun = true;
  9714. opt.sgState = sgState;
  9715. opt.tempo = 120.0; // the instrument is configured at 120 BPM
  9716. 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);
  9717. auto inst = lynx::makeInstrument (lynx::InstrumentKind::autogun);
  9718. inst->configure (c, 120.0);
  9719. inst->prepare (44100.0);
  9720. std::vector<float> l (16384), r (16384);
  9721. for (int s = 0; s < 16384; s += 512)
  9722. {
  9723. if (s == 0)
  9724. inst->noteOn (60, 0.75f);
  9725. if (s == 6144)
  9726. inst->noteOff (60);
  9727. inst->render (l.data() + s, r.data() + s, 512, 1.0f, 1.0f);
  9728. }
  9729. double worst = 0.0, peak = 0.0;
  9730. for (int i = 0; i < 16384; ++i)
  9731. 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]) }),
  9732. peak = std::max (peak, (double) std::abs (l[(std::size_t) i]));
  9733. if (worst != 0.0)
  9734. std::printf (" Autogun instrument vs reference: worst %.3g (peak %.3g)\n", worst, peak);
  9735. CHECK (ref.size() == 2 * 16384 && peak > 0.01 && worst == 0.0);
  9736. std::vector<float> tail (44100), tailR (tail.size());
  9737. for (int k = 0; k < 40 && ! inst->idle(); ++k)
  9738. inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
  9739. CHECK (inst->idle());
  9740. }
  9741. // the state variants: Ogun's parser rejects the version-19 Autogun layout, the comment length is one byte or 0xff + i32
  9742. std::ifstream rt (std::filesystem::path (realFlpDir) / "System" / "Render tests" / "Autogun.flp", std::ios::binary);
  9743. const std::vector<std::uint8_t> rtBytes ((std::istreambuf_iterator<char> (rt)), std::istreambuf_iterator<char>());
  9744. for (const auto& c : flp::parseProject (rtBytes).channels)
  9745. if (c.pluginName == "Autogun")
  9746. CHECK (lynx::autogunStateUsable (c.pluginState) && lynx::ogunParam (c.pluginState, 63) == 430);
  9747. }
  9748. TEST (harmless_rack)
  9749. {
  9750. // REA_rodada36: Harmless's effects rack against our reference model (rack.py / maxmodel.py, which match the
  9751. // installation's Harmless_x64.dll and SG_x64.dll), 44.1 kHz, host blocks of 512
  9752. std::vector<float> inL (8192), inR (8192);
  9753. for (int i = 0; i < 4096; ++i)
  9754. {
  9755. 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));
  9756. inR[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / 44100.0));
  9757. }
  9758. auto run = [&] (const std::array<int, 100>& p, int compType, std::shared_ptr<const lynx::hlfx::MaximusPreset> comp, int total, int voicesUntil) {
  9759. lynx::hlfx::Rack rack;
  9760. std::array<int, 7> t {};
  9761. t[3] = compType;
  9762. rack.prepare (p, t, 44100.0, 120.0, std::move (comp));
  9763. std::vector<float> l (inL.begin(), inL.begin() + total), r (inR.begin(), inR.begin() + total);
  9764. for (int s = 0; s < total; s += 512)
  9765. rack.process (l.data() + s, r.data() + s, std::min (512, total - s), s < voicesUntil);
  9766. return std::make_pair (l, r);
  9767. };
  9768. {
  9769. // chorus type 3 (mix 400), ping-pong delay type 2 (vol 600, feedback 500, 1/4 beat), reverb type 4 (vol 700)
  9770. std::array<int, 100> p {};
  9771. p[90] = 3, p[91] = 400, p[92] = 2, p[93] = 600, p[94] = 500, p[95] = 48, p[96] = 4, p[97] = 700;
  9772. const auto [l, r] = run (p, 0, nullptr, 8192, 4096);
  9773. const std::tuple<int, float, float> golden[] { { 0, 0.0f, 0.0f }, { 500, -0.25248247f, 0.15141529f }, { 3000, -0.65128994f, -0.64854866f },
  9774. { 5000, -0.54308629f, -0.36827448f }, { 6000, -0.98378551f, -0.72790027f },
  9775. { 8191, 0.50599128f, -0.43637198f } };
  9776. for (const auto& [i, gl, gr] : golden)
  9777. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  9778. }
  9779. {
  9780. // the delay alone: an impulse comes back after round(time·fs·0.3125/bpm) samples × (11^0.6 − 1)/10, then × feedback
  9781. std::array<int, 100> p {};
  9782. p[92] = 1, p[93] = 600, p[94] = 500, p[95] = 48;
  9783. lynx::hlfx::Rack rack;
  9784. rack.prepare (p, {}, 44100.0, 120.0, nullptr);
  9785. std::vector<float> l (16384), r (16384);
  9786. l[0] = 1.0f;
  9787. for (int s = 0; s < 16384; s += 512)
  9788. rack.process (l.data() + s, r.data() + s, 512, s == 0);
  9789. const auto D = 5512; // 48·44100·0.3125/120 = 5512.5 → 5512 (round half to even)
  9790. const auto vol = (float) ((std::exp ((double) (600 * 0.001f) * (double) (float) std::log (11.0)) - 1.0) * (double) 0.1f);
  9791. CHECK (std::abs (l[(std::size_t) D] - vol) < 1e-7f && std::abs (l[(std::size_t) D - 1]) < 1e-12f);
  9792. // the feedback ramps from 0.5 toward 0.5: the second echo is vol·0.5
  9793. CHECK (std::abs (l[(std::size_t) (2 * D)] - vol * 0.5f) < 1e-7f && std::abs (r[(std::size_t) D]) < 1e-12f);
  9794. }
  9795. // the compressor: Maximus states of our own (no preset data), checked against SG_x64.dll by REA round 36
  9796. using Env = std::vector<std::array<double, 4>>;
  9797. const Env def { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 } };
  9798. 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,
  9799. int sus = 2882) { return std::array<int, 13> { mode, pre, post, sep, sat, ceil, att, rel2, 2, 5000, 3, sus, 0 }; };
  9800. {
  9801. // master brickwall at −0.22 dB only (bands off / compression off)
  9802. const auto st = maximusTestState ({ 19800, 1, 48065, 1, 1115, 1000, -1 }, { band (3), band (1), band (3), band (0) },
  9803. { def, def, def, def, def, def, def, def }, 0);
  9804. const auto preset = lynx::hlfx::makeMaximusPreset (st);
  9805. CHECK (preset != nullptr);
  9806. std::array<int, 100> p {};
  9807. p[98] = 1000;
  9808. const auto [l, r] = run (p, 1, preset, 4096, 4096);
  9809. // SG_x64.dll's own output (EXEC, REA_rodada40: bit-identical to the port)
  9810. const std::tuple<int, float, float> golden[] { { 100, 0.024997942f, 0.88166064f }, { 1000, 0.56971401f, -0.62808102f },
  9811. { 2047, -0.27541628f, 0.709135f }, { 3000, 0.32431623f, -0.37985197f },
  9812. { 4095, -0.12743522f, -0.15237485f } };
  9813. for (const auto& [i, gl, gr] : golden)
  9814. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  9815. float peak = 0.0f;
  9816. for (int i = 2048; i < 4096; ++i)
  9817. peak = std::max ({ peak, std::abs (l[(std::size_t) i]), std::abs (r[(std::size_t) i]) });
  9818. CHECK (peak < 0.9751f);
  9819. }
  9820. {
  9821. // 24 dB crossovers, ×2 oversampling, low cut, stereo separation, saturation A, a knee with tension (type 7) and
  9822. // exponential tensions (type 0), release 2 on the master, band mix 600
  9823. const Env knee { { 0.0, 0.0, 0.0, 0 }, { 0.35, 0.35, 0.0, 0 }, { 0.65, 0.6, 0.36, 7 } };
  9824. const Env t0 { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.14, 0 }, { 0.5, 0.5, 0.0, 0 } };
  9825. const Env m0 { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 }, { 0.5, 0.6, 0.085, 0 } };
  9826. const auto st = maximusTestState ({ 19800, 1, 48065, 1, 891, 600, 32768 },
  9827. { band (1, 1264, 1296, 500, -500, 1200), band (0, 1340, 1000, -348, 0, 1000, 1115, 0, 1570),
  9828. band (0, 1364, 1152, 0, 0, 1000, 1115, 0, 1186), band (0, 1000, 1000, 0, -196, 1000, 1115, 4464, 1538) },
  9829. { def, def, knee, def, t0, def, m0, def }, 1);
  9830. const auto preset = lynx::hlfx::makeMaximusPreset (st);
  9831. CHECK (preset != nullptr);
  9832. std::array<int, 100> p {};
  9833. p[98] = 600;
  9834. const auto [l, r] = run (p, 2, preset, 4096, 4096);
  9835. // SG_x64.dll's own output (EXEC, REA_rodada40; the port is within −142 dB of it)
  9836. const std::tuple<int, float, float> golden[] { { 100, 0.0f, 0.0f }, { 1000, -0.46856543f, -0.63262129f },
  9837. { 2047, 0.20268451f, 0.54145539f }, { 3000, -0.25613472f, -0.49985155f },
  9838. { 4095, 0.10172445f, 0.050564945f } };
  9839. for (const auto& [i, gl, gr] : golden)
  9840. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  9841. }
  9842. {
  9843. // an .fst wrapper around a state is found; unsupported states are rejected
  9844. const auto st = maximusTestState ({ 19800, 1, 48065, 1, 1115, 1000, -1 }, { band (3), band (1), band (3), band (0) },
  9845. { def, def, def, def, def, def, def, def }, 0);
  9846. std::vector<std::uint8_t> fst { 'F', 'L', 'h', 'd', 6, 0, 0, 0, 0, 0, 1, 0, 0x60, 0, 'F', 'L', 'd', 't' };
  9847. std::vector<std::uint8_t> body { 0xC9, 8, 'M', 'a', 'x', 'i', 'm', 'u', 's', 0, 0xD5 };
  9848. for (auto n = st.size(); ; n >>= 7)
  9849. {
  9850. body.push_back ((std::uint8_t) ((n & 0x7f) | (n >= 0x80 ? 0x80 : 0)));
  9851. if (n < 0x80)
  9852. break;
  9853. }
  9854. body.insert (body.end(), st.begin(), st.end());
  9855. const auto len = (std::uint32_t) body.size();
  9856. for (int k = 0; k < 4; ++k)
  9857. fst.push_back ((std::uint8_t) (len >> (8 * k)));
  9858. fst.insert (fst.end(), body.begin(), body.end());
  9859. const auto found = lynx::hlfx::maximusStateFromFst (fst);
  9860. CHECK (found && *found == st);
  9861. auto rms = st;
  9862. rms[4 + 4 * (7 + 13 * 3 + 12)] = 1; // RMS detector on the master (decoded in REA_rodada40)
  9863. CHECK (lynx::hlfx::makeMaximusPreset (rms) != nullptr);
  9864. auto masterMid = st;
  9865. masterMid[0x1fc] = 1; // "Master Mid mode": not decoded
  9866. CHECK (lynx::hlfx::makeMaximusPreset (masterMid) == nullptr);
  9867. }
  9868. }
  9869. TEST (fruity_delay_bank_engine)
  9870. {
  9871. // REA_rodada56: Fruity Delay Bank, two states of our own against the installation's DLL (goldens = the DLL's output
  9872. // for the same input, EXEC; a new instance, FTZ/DAZ as the plugin's thread). Both are bit-identical.
  9873. lynx::ScopedFlushDenormals ftz;
  9874. struct Bank
  9875. {
  9876. std::array<int, 16> ctl;
  9877. lynx::fldbank::BankOptions opt;
  9878. };
  9879. auto build = [] (std::array<int, 4> globals, std::map<int, Bank> banks, int oversampling) {
  9880. auto s = lynx::fldbank::defaultState();
  9881. for (int i = 0; i < 4; ++i)
  9882. s.p[(std::size_t) i] = globals[(std::size_t) i];
  9883. s.p[132] = oversampling;
  9884. for (auto& [b, k] : banks)
  9885. {
  9886. for (int c = 0; c < 16; ++c)
  9887. if (16 * b + 4 + c < lynx::fldbank::automatable)
  9888. s.p[(std::size_t) (16 * b + 4 + c)] = k.ctl[(std::size_t) c];
  9889. s.bank[(std::size_t) b] = k.opt;
  9890. s.bank[(std::size_t) b].enabled = true;
  9891. }
  9892. return s;
  9893. };
  9894. auto opt = [] (int f1, int o1, int f2, int o2, bool after, int mode, int steps, int amount, bool tap, bool sync) {
  9895. lynx::fldbank::BankOptions o;
  9896. o.filterType[0] = f1;
  9897. o.filterOrder[0] = o1;
  9898. o.filterType[1] = f2;
  9899. o.filterOrder[1] = o2;
  9900. o.filterAfter = after;
  9901. o.mode = mode;
  9902. o.steps = steps;
  9903. o.amount = amount;
  9904. o.tapBeforeFeedback = tap;
  9905. o.sync = sync;
  9906. return o;
  9907. };
  9908. auto run = [] (const lynx::fldbank::State& s, double rate, int block, double bpm) {
  9909. std::vector<float> l (4096), r (4096);
  9910. for (int i = 0; i < 4096; ++i)
  9911. {
  9912. 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));
  9913. r[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / rate));
  9914. }
  9915. lynx::fldbank::Engine e (s, rate, bpm, block);
  9916. for (int i = 0; i < 4096; i += block)
  9917. e.process (l.data() + i, r.data() + i, std::min (block, 4096 - i));
  9918. return std::make_pair (l, r);
  9919. };
  9920. auto check = [] (const std::pair<std::vector<float>, std::vector<float>>& out, const std::vector<std::tuple<int, float, float>>& golden) {
  9921. for (const auto& [i, gl, gr] : golden)
  9922. CHECK (out.first[(std::size_t) i] == gl && out.second[(std::size_t) i] == gr);
  9923. };
  9924. // controls: in pan, in vol, f1 cutoff, f1 res, f1 gain, fb pan, fb level, fb cross, time, offset, f2 cutoff, f2 res,
  9925. // f2 gain, out pan, out vol, send. Three banks: ping-pong with low-pass ×2 / peaking feedback filter, pattern and send;
  9926. // free time with a high shelf after the delay, band-pass feedback, negative feedback/cross, tap before the feedback;
  9927. // swapped outputs with a 3-section high-pass in the loop and a negative pattern shape.
  9928. const auto a = build ({ 100, 128, 120, 110 },
  9929. { { 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) } },
  9930. { 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) } },
  9931. { 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) } } },
  9932. 0);
  9933. check (run (a, 44100.0, 512, 120.0), { { 100, -0.00294386433f, 0.0141299525f }, { 1000, -0.117360562f, -0.140237048f },
  9934. { 2047, 0.172598198f, -0.401046395f }, { 3000, 0.303020209f, -0.127763838f },
  9935. { 4095, 0.0093755126f, 0.241272509f } });
  9936. // 2× oversampling at 48 kHz in blocks of 300, low shelf before the delay, 2-section notch in the loop, a send into a
  9937. // ping-pong bank 3 (bank 1 and 2 off: no send) with a 2-step pattern
  9938. const auto b = build ({ 0, 128, 128, 128 },
  9939. { { 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) } },
  9940. { 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) } } },
  9941. 1);
  9942. check (run (b, 48000.0, 300, 93.0), { { 100, 0.0f, 0.0f }, { 1000, 0.0f, 0.0f }, { 2047, 0.201803684f, -0.327834547f },
  9943. { 3000, 0.412344277f, -0.101866335f }, { 4095, 0.204572573f, 0.71171385f } });
  9944. // FL's state round-trips through the import (version 15 times are converted), the globals are the slot's parameters
  9945. auto bytes = lynx::fldbank::serialise (a);
  9946. const auto slot = lynx::effectFromFl ("Fruity Delay Bank", bytes);
  9947. CHECK (slot && slot->kind == lynx::EffectKind::fruityDelayBank && slot->state == bytes);
  9948. CHECK (slot && slot->params[0] == 100.0f / 128.0f && slot->params[3] == 110.0f / 128.0f);
  9949. bytes[0] = 15;
  9950. const auto v15 = lynx::fldbank::parse (bytes);
  9951. CHECK (v15 && v15->p[12] == 2400 && v15->p[28] == 1500);
  9952. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityDelayBank, 3, 0.5f, 120.0) == "50%");
  9953. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityDelayBank, 0, 1.0f, 120.0) == "0.0 dB");
  9954. CHECK (! lynx::fldbank::parse ({ 16, 0, 0, 0 }));
  9955. // the LYNX effect (settled ramps, 512-frame engine blocks) runs from the slot
  9956. auto fx = lynx::makeEffect (*slot);
  9957. fx->prepare (44100.0);
  9958. std::vector<float> l (1024, 0.5f), r (1024, -0.25f);
  9959. fx->process (l.data(), r.data(), 1024, slot->params, 120.0);
  9960. CHECK (std::isfinite (l[1023]) && std::isfinite (r[1023]));
  9961. }
  9962. TEST (fruity_multiband_engine)
  9963. {
  9964. // REA_rodada56: Fruity Multiband Compressor, states of our own against the installation's DLL
  9965. // (goldens = the DLL's output for the same input, EXEC; blocks of 512 at 44.1 kHz; all three bit-identical)
  9966. std::vector<float> inL (4096), inR (4096);
  9967. for (int i = 0; i < 4096; ++i)
  9968. {
  9969. 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)
  9970. + 0.5 * std::sin (2.0 * 3.141592653589793 * 90.0 * i / 44100.0));
  9971. inR[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / 44100.0)
  9972. + 0.4 * std::sin (2.0 * 3.141592653589793 * 7000.0 * i / 44100.0));
  9973. }
  9974. auto multiband = [] (std::array<int, 28> c) {
  9975. std::vector<std::uint8_t> b;
  9976. auto put = [&b] (std::int32_t v) {
  9977. std::uint8_t q[4];
  9978. std::memcpy (q, &v, 4);
  9979. b.insert (b.end(), q, q + 4);
  9980. };
  9981. put (0);
  9982. for (auto v : c)
  9983. put (v);
  9984. put (0); // UI words of the state (not used by the engine)
  9985. put (50);
  9986. return b;
  9987. };
  9988. struct Out { std::vector<float> l, r; int latency = 0; };
  9989. auto run = [&] (const std::vector<std::uint8_t>& st) {
  9990. const auto s = lynx::flmb::parse (st);
  9991. CHECK (s.has_value());
  9992. lynx::EffectParams p {};
  9993. lynx::flmb::toParams (*s, p.data());
  9994. auto fx = lynx::flmb::make();
  9995. fx->prepare (44100.0);
  9996. Out o { inL, inR };
  9997. for (int s0 = 0; s0 < 4096; s0 += 512)
  9998. fx->process (o.l.data() + s0, o.r.data() + s0, 512, p, 120.0);
  9999. o.latency = fx->latencySamples();
  10000. return o;
  10001. };
  10002. auto check = [] (const Out& out, const std::vector<std::tuple<int, float, float>>& golden) {
  10003. for (const auto& [i, gl, gr] : golden)
  10004. CHECK (std::abs (out.l[(std::size_t) i] - gl) < 2e-6f && std::abs (out.r[(std::size_t) i] - gr) < 2e-6f);
  10005. };
  10006. // IIR crossovers, limiter on, high band compressing, mid bypassed, low compressing, +2 dB output
  10007. 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 }));
  10008. CHECK (oa.latency == 0);
  10009. check (oa,
  10010. { { 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 } });
  10011. // linear phase (FIR, 52 frames of latency), overlapping mid transitions, low band off, limiter off
  10012. 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 }));
  10013. CHECK (ob.latency == 52);
  10014. check (ob,
  10015. { { 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 } });
  10016. // linear phase, hard compression everywhere, limiter on, mid crossovers crossed (the FIR mid band is silent)
  10017. 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 }));
  10018. CHECK (oc.latency == 52);
  10019. check (oc,
  10020. { { 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 } });
  10021. // the slot's parameters carry FL's raw controls exactly; after the project file's 4-decimal rounding the
  10022. // effect built with FL's state still uses the exact controls
  10023. const auto st = multiband ({ 42598, 1, 1, 0, 15604, 25782, 53524, 32768, 11, 298, 51118, 0, 14994, 5037, 24736, 55755, 26870, 221,
  10024. 1090, 49807, 0, 5037, 21845, 47074, 32768, 550, 1090, 40632 });
  10025. const auto parsed = lynx::flmb::parse (st);
  10026. CHECK (parsed && parsed->c[4] == 15604);
  10027. lynx::EffectParams p {};
  10028. lynx::flmb::toParams (*parsed, p.data());
  10029. for (int i = 0; i < lynx::flmb::controls; ++i)
  10030. 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]);
  10031. lynx::EffectParams rounded = p;
  10032. for (auto& v : rounded)
  10033. v = std::round (v * 10000.0f) / 10000.0f;
  10034. std::vector<float> l1 (inL), r1 (inR), l2 (inL), r2 (inR);
  10035. auto exact = lynx::flmb::make();
  10036. auto fromState = lynx::flmb::make (st);
  10037. exact->prepare (44100.0);
  10038. fromState->prepare (44100.0);
  10039. for (int s0 = 0; s0 < 4096; s0 += 512)
  10040. {
  10041. exact->process (l1.data() + s0, r1.data() + s0, 512, p, 120.0);
  10042. fromState->process (l2.data() + s0, r2.data() + s0, 512, rounded, 120.0);
  10043. }
  10044. CHECK (l1 == l2 && r1 == r2);
  10045. CHECK (lynx::flmb::paramText (4, p[4]) == "6302 Hz");
  10046. CHECK (lynx::flmb::paramText (3, 0.5f) == "off");
  10047. // the FL import builds the exact effect with FL's state kept
  10048. const auto slot = lynx::effectFromFl ("Fruity Multiband Compressor", st);
  10049. CHECK (slot && slot->kind == lynx::EffectKind::fruityMultiband && slot->state == st && slot->params[4] == p[4]);
  10050. // unknown versions are rejected
  10051. CHECK (! lynx::flmb::parse ({ 1, 0, 0, 0 }));
  10052. }
  10053. // REA_rodada54: input of the stereo-effect goldens (the installation DLLs' output for the same input, EXEC)
  10054. static std::pair<std::vector<float>, std::vector<float>> stereoFxInput()
  10055. {
  10056. std::vector<float> l (4096), r (4096);
  10057. for (int i = 0; i < 4096; ++i)
  10058. {
  10059. 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));
  10060. r[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / 44100.0));
  10061. }
  10062. return { l, r };
  10063. }
  10064. static std::vector<std::uint8_t> i32State (std::initializer_list<int> words)
  10065. {
  10066. std::vector<std::uint8_t> d;
  10067. for (const auto w : words)
  10068. for (int b = 0; b < 4; ++b)
  10069. d.push_back ((std::uint8_t) ((std::uint32_t) w >> (8 * b)));
  10070. return d;
  10071. }
  10072. static void checkStereoFx (lynx::EffectSlot slot, const std::vector<std::tuple<int, float, float>>& golden)
  10073. {
  10074. auto [l, r] = stereoFxInput();
  10075. auto fx = lynx::makeEffect (slot);
  10076. fx->prepare (44100.0);
  10077. for (int s = 0; s < 4096; s += 512)
  10078. fx->process (l.data() + s, r.data() + s, 512, slot.params, 120.0);
  10079. for (const auto& [i, gl, gr] : golden)
  10080. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  10081. }
  10082. TEST (stereo_shaper_engine)
  10083. {
  10084. // Fruity Stereo Shaper (REA_rodada54 §2): matrix with cross terms, delay on R, all-pass on L, effects post / pre
  10085. auto slot = lynx::effectFromFl ("Fruity Stereo Shaper", i32State ({ 0, -3050, 12800, 9000, 2000, 1408, -2496, 0, 1 }));
  10086. CHECK (slot && slot->kind == lynx::EffectKind::stereoShaper);
  10087. if (slot)
  10088. checkStereoFx (*slot, { { 100, 0.0438031591f, 0.0f }, { 1000, 1.11109531f, -0.781641483f }, { 2047, -0.73191303f, 0.483852893f },
  10089. { 3000, 0.950346172f, -0.769737065f }, { 4095, 0.251555145f, 0.153306276f } });
  10090. slot = lynx::effectFromFl ("Fruity Stereo Shaper", i32State ({ 0, 0, 12800, 12800, 0, -2656, 2184, 0, 0 }));
  10091. CHECK (slot.has_value());
  10092. if (slot)
  10093. checkStereoFx (*slot, { { 100, 0.0f, -0.104282036f }, { 1000, 0.0f, 1.08441091f }, { 2047, 0.463494211f, -0.93754214f },
  10094. { 3000, -1.49942112f, 0.820726812f }, { 4095, -1.31784308f, -0.057449434f } });
  10095. // controls round-trip through the slot parameters; the Patcher's difference bus is the same engine flagged
  10096. const auto st = lynx::stereofx::parseStereoShaper (i32State ({ 0, -3050, 12800, 9000, 2000, 1408, -2496, 2, 1 }));
  10097. CHECK (st.has_value());
  10098. if (st)
  10099. {
  10100. lynx::EffectParams p {};
  10101. lynx::stereofx::stereoShaperToParams (*st, p.data());
  10102. const auto back = lynx::stereofx::stereoShaperFromParams (p.data());
  10103. CHECK (back.rightToLeft == -3050 && back.left == 12800 && back.right == 9000 && back.leftToRight == 2000 && back.delay == 1408
  10104. && back.phase == -2496 && back.post == 1);
  10105. const auto head = lynx::patcherBranchHead ("Fruity Stereo Shaper", i32State ({ 0, -3050, 12800, 9000, 2000, 1408, -2496, 2, 1 }), 2);
  10106. CHECK (head.valid && head.slot && head.slot->kind == lynx::EffectKind::stereoShaper && head.slot->params[7] == 1.0f);
  10107. }
  10108. CHECK (! lynx::stereofx::parseStereoShaper ({ 1, 2, 3 }));
  10109. }
  10110. TEST (frequency_splitter_engine)
  10111. {
  10112. // Frequency Splitter (REA_rodada62): our own states through Frequency Splitter_x64.dll from its creation (gains
  10113. // ramping up from 0, FFT response faded in), blocks of 512 at 44.1 kHz; every band sent to its own destination
  10114. // (1 low, 2 mid, 3 high). IIR modes are bit-exact; the FFT mode's residual is the plugin's single-precision FFT.
  10115. using Gold = std::vector<std::tuple<int, float, float>>;
  10116. struct Case
  10117. {
  10118. std::vector<int> words;
  10119. int mute, latency;
  10120. std::array<Gold, 4> gold; // low, mid, high sends, summed output
  10121. };
  10122. const std::vector<Case> cases {
  10123. { { 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
  10124. { { 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
  10125. { { 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
  10126. { { 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
  10127. { { 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
  10128. { { 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
  10129. { { 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
  10130. };
  10131. auto [l, r] = stereoFxInput();
  10132. for (const auto& c : cases)
  10133. {
  10134. auto words = c.words;
  10135. words.insert (words.begin(), 4);
  10136. for (const int w : { 0, 0, 0, c.mute })
  10137. words.push_back (w);
  10138. std::vector<std::uint8_t> st;
  10139. for (const auto w : words)
  10140. for (int b = 0; b < 4; ++b)
  10141. st.push_back ((std::uint8_t) ((std::uint32_t) w >> (8 * b)));
  10142. st.push_back (0);
  10143. st.resize (st.size() + 24, 0);
  10144. const auto s = lynx::fsplit::parseState (st);
  10145. CHECK (s.has_value());
  10146. if (! s)
  10147. continue;
  10148. lynx::fsplit::Engine e;
  10149. for (int i = 0; i < lynx::fsplit::engineParams; ++i)
  10150. e.setParameter (i, s->params[(std::size_t) i]);
  10151. e.prepare (44100.0);
  10152. e.reset (false);
  10153. std::array<std::vector<float>, 8> o;
  10154. for (auto& v : o)
  10155. v.assign (4096, 0.0f);
  10156. for (int b = 0; b < 4096; b += 512)
  10157. {
  10158. float* out[8];
  10159. for (int k = 0; k < 8; ++k)
  10160. out[k] = o[(std::size_t) k].data() + b;
  10161. e.process (l.data() + b, r.data() + b, out, 512);
  10162. }
  10163. CHECK (e.latencySamples() == c.latency);
  10164. const auto tol = c.latency > 0 ? 2e-5f : 2e-6f;
  10165. for (int k = 0; k < 4; ++k)
  10166. for (const auto& [i, gl, gr] : c.gold[(std::size_t) k])
  10167. CHECK (std::abs (o[(std::size_t) (2 * k)][(std::size_t) i] - gl) <= tol * std::max (1.0f, std::abs (gl))
  10168. && std::abs (o[(std::size_t) (2 * k + 1)][(std::size_t) i] - gr) <= tol * std::max (1.0f, std::abs (gr)));
  10169. // the mixer effect: the summed output (FL's main output) of an instance that has been running, and a
  10170. // Patcher bus = the bands sent to it
  10171. auto slot = lynx::effectFromFl ("Frequency Splitter", st);
  10172. CHECK (slot && slot->kind == lynx::EffectKind::frequencySplitter);
  10173. const auto head = lynx::patcherBranchHead ("Frequency Splitter", st, 3);
  10174. CHECK (head.valid && head.slot && head.slot->params[lynx::fsplit::outputHigh] == 1.0f
  10175. && head.slot->params[lynx::fsplit::outputLow] == 0.0f);
  10176. if (slot && head.slot)
  10177. {
  10178. lynx::fsplit::Engine settled;
  10179. for (int i = 0; i < lynx::fsplit::engineParams; ++i)
  10180. settled.setParameter (i, s->params[(std::size_t) i]);
  10181. settled.prepare (44100.0);
  10182. settled.reset (true);
  10183. std::vector<float> sl (4096), sr (4096), hl (4096), hr (4096);
  10184. float* out[8] { nullptr, nullptr, nullptr, nullptr, hl.data(), hr.data(), sl.data(), sr.data() };
  10185. settled.process (l.data(), r.data(), out, 4096);
  10186. const lynx::EffectSlot* both[2] { &*slot, &*head.slot };
  10187. for (const auto* p : both)
  10188. {
  10189. auto fx = lynx::makeEffect (*p);
  10190. fx->prepare (44100.0);
  10191. auto ml = l, mr = r;
  10192. for (int b = 0; b < 4096; b += 300)
  10193. fx->process (ml.data() + b, mr.data() + b, std::min (300, 4096 - b), p->params, 120.0);
  10194. const auto& wl = p == &*slot ? sl : hl;
  10195. const auto& wr = p == &*slot ? sr : hr;
  10196. CHECK (ml == wl && mr == wr);
  10197. CHECK (fx->latencySamples() == c.latency);
  10198. }
  10199. }
  10200. }
  10201. CHECK (! lynx::patcherBranchHead ("Frequency Splitter", std::vector<std::uint8_t> (0x58, 0), 2).valid); // no band sent to bus 2
  10202. CHECK (lynx::fsplit::paramText (2, 3.0f / 7.0f) == "24 dB/oct" && lynx::fsplit::paramText (11, 0.5f) == "0.0 dB");
  10203. CHECK (lynx::fsplit::paramText (0, 0.0f) == "30.0 Hz" && lynx::fsplit::paramText (4, 1.0f) == "FFT");
  10204. }
  10205. TEST (stereo_enhancer_transient_spreader_engines)
  10206. {
  10207. // REA_rodada54 §3–5: own states against the installation's DLLs (goldens = the DLL's output, EXEC)
  10208. auto se = lynx::effectFromFl ("Fruity Stereo Enhancer", i32State ({ -18, 300, -54, -148, 1, 1 }));
  10209. CHECK (se && se->kind == lynx::EffectKind::stereoEnhancer);
  10210. if (se)
  10211. checkStereoFx (*se, { { 100, 0.0f, 0.00309863733f }, { 1000, 0.0101111531f, -0.110266283f }, { 2047, -0.688752949f, -1.93998075f },
  10212. { 3000, -0.283846498f, -0.739818454f }, { 4095, 0.191147864f, 2.70425582f } });
  10213. auto tpState = i32State ({ 1, 138, 116, 64, 90, 2, 0, 70, 100, 1 });
  10214. tpState.push_back (5);
  10215. for (const auto w : i32State ({ 0, 50 }))
  10216. tpState.push_back (w);
  10217. auto tp = lynx::effectFromFl ("Transient Processor", tpState);
  10218. CHECK (tp && tp->kind == lynx::EffectKind::transientProcessor);
  10219. if (tp)
  10220. checkStereoFx (*tp, { { 100, 0.0568947345f, 0.124077775f }, { 1000, -0.326598823f, -0.193128616f }, { 2047, 0.612462878f, -0.0517415144f },
  10221. { 3000, -0.535302937f, -0.446303189f }, { 4095, -0.634670973f, 0.316044629f } });
  10222. auto spState = i32State ({ 0x10003, 7861, 3215, 106000, 1, 1, 0 });
  10223. spState.push_back (1);
  10224. spState.push_back (1);
  10225. auto sp = lynx::effectFromFl ("Spreader", spState);
  10226. CHECK (sp && sp->kind == lynx::EffectKind::spreader);
  10227. if (sp)
  10228. checkStereoFx (*sp, { { 100, 0.11841587f, -0.0958916694f }, { 1000, 1.40852344f, 0.087452054f }, { 2047, 1.07267976f, -2.02493715f },
  10229. { 3000, -0.781082511f, 0.811192274f }, { 4095, -1.80673707f, 0.89967829f } });
  10230. // unknown layouts keep the earlier fallback; FL automation maps one to one
  10231. CHECK (! lynx::stereofx::parseSpreader (i32State ({ 0x10001, 1, 2, 3, 4, 5 })));
  10232. CHECK (! lynx::stereofx::parseTransientProcessor (i32State ({ 2, 1, 2, 3, 4, 5, 6, 7, 8, 9 })));
  10233. const auto map = lynx::effectParamFromFl ("Transient Processor", 6);
  10234. CHECK (map && map->param == 6 && map->convert (0.25f) == 0.25f);
  10235. }
  10236. TEST (fruity_dynamics_engines)
  10237. {
  10238. // REA_rodada48: Fruity Limiter / Fruity Compressor, states of our own against the installation's DLLs
  10239. // (goldens = the DLL's output for the same input, EXEC; blocks of 512 at 44.1 kHz)
  10240. std::vector<float> inL (4096), inR (4096), side (4096);
  10241. for (int i = 0; i < 4096; ++i)
  10242. {
  10243. 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));
  10244. inR[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / 44100.0));
  10245. side[(std::size_t) i] = (float) (0.9 * std::sin (2.0 * 3.141592653589793 * 55.0 * i / 44100.0));
  10246. }
  10247. auto put = [] (std::vector<std::uint8_t>& b, std::int32_t v) {
  10248. std::uint8_t q[4];
  10249. std::memcpy (q, &v, 4);
  10250. b.insert (b.end(), q, q + 4);
  10251. };
  10252. auto limiterState = [&] (std::array<int, 18> p, int k = 0, int exact = 0) {
  10253. std::vector<std::uint8_t> b;
  10254. put (b, 7);
  10255. for (auto v : p)
  10256. put (b, v);
  10257. b.resize (0xA4, 0);
  10258. put (b, k);
  10259. b.push_back ((std::uint8_t) exact);
  10260. return b;
  10261. };
  10262. auto compressorState = [&] (int version, std::array<int, 6> p, int flags, int k = 0) {
  10263. std::vector<std::uint8_t> b;
  10264. put (b, version);
  10265. for (auto v : p)
  10266. put (b, v);
  10267. put (b, flags);
  10268. if (version > 2)
  10269. put (b, k);
  10270. return b;
  10271. };
  10272. struct Out { std::vector<float> l, r; int latency = 0; };
  10273. auto run = [&] (const lynx::EffectSlot& slot, bool sidechain) {
  10274. auto fx = lynx::makeEffect (slot);
  10275. fx->prepare (44100.0);
  10276. Out o { inL, inR };
  10277. for (int s = 0; s < 4096; s += 512)
  10278. {
  10279. if (sidechain)
  10280. fx->setSidechain (side.data() + s, side.data() + s);
  10281. fx->process (o.l.data() + s, o.r.data() + s, 512, slot.params, 120.0);
  10282. }
  10283. o.latency = fx->latencySamples();
  10284. return o;
  10285. };
  10286. auto check = [] (const Out& out, const std::vector<std::tuple<int, float, float>>& golden) {
  10287. for (const auto& [i, gl, gr] : golden)
  10288. CHECK (std::abs (out.l[(std::size_t) i] - gl) < 2e-6f && std::abs (out.r[(std::size_t) i] - gr) < 2e-6f);
  10289. };
  10290. auto limiter = [] (const std::vector<std::uint8_t>& st) {
  10291. auto slot = lynx::effectFromFl ("Fruity Limiter", st);
  10292. CHECK (slot && slot->kind == lynx::EffectKind::fruityLimiter);
  10293. return *slot;
  10294. };
  10295. // look-ahead limiter: +4 dB gain into a −2.5 dB ceiling, 2 ms attack (88 samples of latency); bit-identical
  10296. 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);
  10297. CHECK (a.latency == 88);
  10298. check (a, { { 100, 0.0176312141f, 0.621841073f }, { 1000, 0.401142687f, -0.442239583f }, { 2047, -0.19392401f, 0.499310732f },
  10299. { 3000, 0.22835508f, -0.267458469f }, { 4095, -0.089728713f, -0.107289009f } });
  10300. // compressor section with a positive knee, 2 compressor curve stages and soft saturation (port within −143 dB)
  10301. check (run (limiter (limiterState ({ 1000, 900, 1000, 0, 3, 5000, 3, 2882, 400, 600, 400, 1115, 6666, 2, 650, 1000, 0, 4187 })), false),
  10302. { { 100, -0.00208989787f, 0.0100310864f }, { 1000, -0.0545219742f, -0.0651496574f }, { 2047, 0.177322432f, -0.230846375f },
  10303. { 3000, -0.159136176f, -0.183688983f }, { 4095, -0.300143838f, 0.299418598f } });
  10304. // sidechained compressor (side input 1) with a negative knee, noise gate and the exact-ceiling option
  10305. 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),
  10306. { { 100, -0.00186592329f, 0.00895605329f }, { 1000, -0.0375732854f, -0.0448972471f }, { 2047, 0.124697573f, -0.162336946f },
  10307. { 3000, -0.111069582f, -0.128206298f }, { 4095, -0.218446419f, 0.21791859f } });
  10308. auto compressor = [] (const std::vector<std::uint8_t>& st) {
  10309. auto slot = lynx::effectFromFl ("Fruity Compressor", st);
  10310. CHECK (slot && slot->kind == lynx::EffectKind::fruityCompressor);
  10311. return *slot;
  10312. };
  10313. // Vintage type, look-ahead flag, side input 1: bit-identical
  10314. const auto ca = run (compressor (compressorState (3, { -200, 40, 30, 50, 300, 2 }, 1, 1)), true);
  10315. CHECK (ca.latency == 64);
  10316. check (ca, { { 100, 0.0795554966f, -0.230549783f }, { 1000, 0.483903229f, 0.0241768137f }, { 2047, -0.596430957f, -0.441769004f },
  10317. { 3000, 0.597724319f, -0.18129006f }, { 4095, 0.607042372f, 0.435067475f } });
  10318. // Soft/R, version 2 without the flag (the output is still 64 frames late, but not reported): bit-identical
  10319. const auto cb = run (compressor (compressorState (2, { -150, 80, 0, 150, 100, 7 }, 0)), false);
  10320. CHECK (cb.latency == 0);
  10321. check (cb, { { 100, 0.0813739002f, -0.235819489f }, { 1000, 0.264188439f, 0.0131994039f }, { 2047, -0.260424435f, -0.192893147f },
  10322. { 3000, 0.251500547f, -0.0762802213f }, { 4095, 0.252972007f, 0.181305125f } });
  10323. // the slot's parameters carry FL's raw controls exactly (and survive the 4-decimal project file)
  10324. const auto st = limiterState ({ 1261, 995, 1303, 1115, 3, 5000, 3, 2882, 264, 244, -590, 1115, 6666, 1, 650, 1000, 0, 4187 }, 1);
  10325. const auto parsed = lynx::fldyn::parseLimiter (st);
  10326. CHECK (parsed && parsed->sidechain == 1 && ! parsed->exactCeiling);
  10327. const auto slot = limiter (st);
  10328. for (int i = 0; i < lynx::fldyn::limiterControls; ++i)
  10329. {
  10330. const auto rounded = std::round (slot.params[(std::size_t) i] * 10000.0f) / 10000.0f;
  10331. 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]);
  10332. }
  10333. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityLimiter, 3, slot.params[3], 120.0) == "2.0 ms");
  10334. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityCompressor, 5, 2.0f / 7.0f, 120.0) == "Vintage");
  10335. // unreadable states keep the old approximation
  10336. CHECK (lynx::effectFromFl ("Fruity Limiter", { 7, 0, 0, 0 }).has_value() == false
  10337. || lynx::effectFromFl ("Fruity Limiter", { 7, 0, 0, 0 })->kind != lynx::EffectKind::fruityLimiter);
  10338. CHECK (! lynx::fldyn::parseCompressor ({ 4, 0, 0, 0 }));
  10339. }
  10340. TEST (maximus_engine)
  10341. {
  10342. // REA_rodada40: the engine on its own (MaximusProcessor), states of our own against the installation's DLLs
  10343. // (goldens = the DLL's output for the same input, EXEC), and the containers of the mixer effect and Vocodex
  10344. std::vector<float> inL (4096), inR (4096);
  10345. for (int i = 0; i < 4096; ++i)
  10346. {
  10347. 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));
  10348. inR[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / 44100.0));
  10349. }
  10350. auto run = [&] (std::shared_ptr<const lynx::hlfx::MaximusPreset> preset, int mix) {
  10351. lynx::hlfx::MaximusProcessor m;
  10352. m.prepare (std::move (preset), 44100.0, mix);
  10353. std::vector<float> l (inL), r (inR);
  10354. for (int s = 0; s < 4096; s += 512)
  10355. m.process (l.data() + s, r.data() + s, 512);
  10356. return std::make_pair (l, r);
  10357. };
  10358. using Env = std::vector<std::array<double, 4>>;
  10359. const Env def { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 } };
  10360. const Env knee { { 0.0, 0.0, 0.0, 0 }, { 0.35, 0.35, 0.0, 0 }, { 0.65, 0.6, 0.36, 7 } };
  10361. const Env t0 { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.14, 0 }, { 0.5, 0.5, 0.0, 0 } };
  10362. const Env m0 { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 }, { 0.5, 0.6, 0.085, 0 } };
  10363. 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,
  10364. int sus = 2882, int rms = 0) { return std::array<int, 13> { mode, pre, post, sep, sat, ceil, att, rel2, 2, 5000, 3, sus, rms }; };
  10365. auto check = [] (const std::pair<std::vector<float>, std::vector<float>>& out, const std::vector<std::tuple<int, float, float>>& golden) {
  10366. for (const auto& [i, gl, gr] : golden)
  10367. CHECK (std::abs (out.first[(std::size_t) i] - gl) < 2e-6f && std::abs (out.second[(std::size_t) i] - gr) < 2e-6f);
  10368. };
  10369. // RMS detectors, saturation mode B, a 5 Hz low cut, 12 dB high split (SG_x64.dll: port within −152 dB)
  10370. auto st3 = maximusTestState ({ 19800, 1, 48065, 0, 1115, 1000, 0 },
  10371. { band (0, 1500, 1000, 0, 400, 900, 1115, 0, 2882, 1), band (0, 1340, 1000, 0, 0, 1000, 1115, 0, 1570, 1),
  10372. band (1, 1000, 1200, 0, 1000, 1000), band (0, 1000, 1000, 0, 250, 1000, 1115, 4464, 2882, 1) },
  10373. { def, def, knee, def, t0, def, m0, def }, 0);
  10374. const auto p3 = lynx::hlfx::makeMaximusPreset (st3);
  10375. CHECK (p3 != nullptr);
  10376. check (run (p3, 1000), { { 100, 0.0f, 0.0f }, { 1000, 0.87061065f, 0.64004499f }, { 2047, -0.97266865f, 0.60940075f },
  10377. { 3000, 0.96658099f, 0.9250561f }, { 4095, 0.99094611f, -0.964059f } });
  10378. // the same with solo on (UI byte 0x1EF) and the low band selected (0x1E0): bit-identical to SG_x64.dll
  10379. auto solo = st3;
  10380. solo[0x1ef] = 1;
  10381. std::memset (solo.data() + 0x1e0, 0, 4);
  10382. check (run (lynx::hlfx::makeMaximusPreset (solo), 1000),
  10383. { { 100, 3.2922967e-06f, 0.00036916698f }, { 1000, 0.15653539f, -0.047913823f }, { 2047, -0.17336531f, 0.019099623f },
  10384. { 3000, 0.17185682f, -0.017173048f }, { 4095, 0.16714551f, 0.019473195f } });
  10385. // the legacy linear-phase FIR band split (UI byte 0x1FD; SG_x64.dll: port within −148 dB)
  10386. auto fir = st3;
  10387. fir[0x1fd] = 1;
  10388. check (run (lynx::hlfx::makeMaximusPreset (fir), 1000),
  10389. { { 100, 0.0f, 0.0f }, { 1000, 0.97986746f, 0.18555209f }, { 2047, -0.98930448f, -0.95970845f },
  10390. { 3000, 0.98856646f, -0.44592777f }, { 4095, 0.99069679f, 0.94382554f } });
  10391. // Soundgoodizer_x64.dll's own build with a v4 file (its presets' layout): a copy of the round-36 state without
  10392. // oversampling, through a byte-identical copy of the installation's DLL with this file as its preset (EXEC)
  10393. const auto st2 = maximusTestState ({ 19800, 1, 48065, 1, 891, 600, 32768 },
  10394. { band (1, 1264, 1296, 500, -500, 1200), band (0, 1340, 1000, -348, 0, 1000, 1115, 0, 1570),
  10395. band (0, 1364, 1152, 0, 0, 1000, 1115, 0, 1186), band (0, 1000, 1000, 0, -196, 1000, 1115, 4464, 1538) },
  10396. { def, def, knee, def, t0, def, m0, def }, 0);
  10397. std::vector<std::uint8_t> v4 { 4, 0, 0, 0 };
  10398. v4.insert (v4.end(), st2.begin() + 4, st2.begin() + 0x1f5);
  10399. v4.insert (v4.end(), st2.begin() + 0x1fe, st2.end());
  10400. const auto p4 = lynx::hlfx::makeMaximusPreset (v4, lynx::hlfx::MaximusBuild::soundgoodizer);
  10401. CHECK (p4 != nullptr);
  10402. check (run (p4, 600), { { 100, 0.0f, 0.0f }, { 1000, -0.60926896f, -0.6652832f }, { 2047, 0.40872875f, 0.36375371f },
  10403. { 3000, -0.41941205f, -0.61312866f }, { 4095, -0.12878418f, 0.27944946f } });
  10404. CHECK (lynx::hlfx::makeMaximusPreset (st2, lynx::hlfx::MaximusBuild::soundgoodizer) != nullptr); // v7: also read by that build
  10405. // the mixer effect's container and the effect itself
  10406. lynx::maximusfx::Blob blob;
  10407. blob.build = lynx::hlfx::MaximusBuild::soundgoodizer;
  10408. blob.mix = 600;
  10409. blob.state = v4;
  10410. const auto packed = lynx::maximusfx::pack (blob);
  10411. const auto back = lynx::maximusfx::unpack (packed);
  10412. CHECK (back && back->build == blob.build && back->mix == 600 && back->state == v4);
  10413. auto slot = lynx::makeEffectSlot (lynx::EffectKind::maximus);
  10414. slot.state = packed;
  10415. auto fx = lynx::makeEffect (slot);
  10416. fx->prepare (44100.0);
  10417. {
  10418. std::vector<float> l (inL), r (inR);
  10419. for (int s = 0; s < 4096; s += 1024) // longer host blocks are split into the engine's 512-frame calls
  10420. fx->process (l.data() + s, r.data() + s, 1024, slot.params, 120.0);
  10421. const auto ref = run (p4, 600);
  10422. CHECK (l == ref.first && r == ref.second);
  10423. }
  10424. const std::vector<std::uint8_t> sgState { 3, 0, 0, 0, 2, 0, 0, 0, 0x78, 2, 0, 0 };
  10425. const auto sg = lynx::maximusfx::parseSoundgoodizer (sgState);
  10426. CHECK (sg && sg->preset == 2 && sg->amount == 632 && std::string (lynx::maximusfx::soundgoodizerPresetFile (2)) == "c.dat");
  10427. CHECK (lynx::soundgoodizerPresetPath (sgState) == "Plugins\\Fruity\\Effects\\Soundgoodizer\\Data\\Presets\\c.dat");
  10428. CHECK (lynx::soundgoodizerExact (sgState, v4).has_value() && ! lynx::soundgoodizerExact (sgState, {}).has_value());
  10429. // without the preset files (no FL installation) the approximation keeps the plugin's state and its name
  10430. {
  10431. const auto approx = lynx::effectFromFl ("Soundgoodizer", sgState);
  10432. CHECK (approx && lynx::isSoundgoodizerApprox (*approx) && lynx::projectcontents::effectSlotName (*approx) == "Soundgoodizer");
  10433. const auto st = approx ? lynx::maximusfx::parseSoundgoodizer (approx->state) : std::nullopt;
  10434. CHECK (st && st->preset == 2 && st->amount == 632 && approx->branches.size() == 1 && approx->branches[0].chain.size() == 2);
  10435. CHECK (! lynx::isSoundgoodizerApprox (lynx::makeEffectSlot (lynx::EffectKind::parallel))); // a Patcher container stays one
  10436. const auto edited = lynx::soundgoodizerApproxSlot (0, 1000);
  10437. CHECK (lynx::isSoundgoodizerApprox (edited) && lynx::maximusfx::parseSoundgoodizer (edited.state)->preset == 0);
  10438. }
  10439. // Vocodex's appended installation data survives a round trip and leaves FL's state untouched
  10440. const std::vector<std::uint8_t> flState { 8, 0, 0, 0, 1, 2, 3 };
  10441. lynx::vocodex::InstallData data;
  10442. data.sgDat = st2;
  10443. data.carrierState = { 9, 9, 9 };
  10444. const auto joined = lynx::vocodex::appendInstallData (flState, data);
  10445. const auto [raw, files] = lynx::vocodex::splitInstallData (joined);
  10446. CHECK (raw == flState && files.sgDat == st2 && files.carrierState == data.carrierState);
  10447. CHECK (lynx::vocodex::splitInstallData (flState).first == flState);
  10448. }
  10449. // ---- Fruity Reeverb 2 (REA_rodada49) --------------------------------------------------------------------------------
  10450. // Goldens: the installation's Fruity Reeverb 2_x64.dll (unmodified, rate set before the state, flushed, blocks of 512)
  10451. // on our own states and input; LYNX's engine is bit-identical to it.
  10452. TEST (reeverb2_engine)
  10453. {
  10454. auto hex = [] (const char* h) {
  10455. std::vector<std::uint8_t> v;
  10456. for (std::size_t i = 0; h[i] != 0 && h[i + 1] != 0; i += 2)
  10457. v.push_back ((std::uint8_t) std::stoi (std::string (h + i, 2), nullptr, 16));
  10458. return v;
  10459. };
  10460. constexpr int N = 8192;
  10461. std::vector<float> inL (N), inR (N);
  10462. std::uint32_t seed = 12345;
  10463. for (int i = 0; i < N; ++i)
  10464. {
  10465. seed = (seed * 1103515245u + 12345u) & 0x7fffffffu;
  10466. const auto nz = (float) ((double) seed / 2147483648.0 - 0.5);
  10467. const auto tri = (float) ((i % 200) - 100) / 100.0f;
  10468. const float g = i < 4000 ? 1.0f : 0.0f;
  10469. inL[(std::size_t) i] = 0.5f * tri * g + (i >= 100 && i < 300 ? nz * 0.6f : 0.0f);
  10470. inR[(std::size_t) i] = 0.25f * tri * g;
  10471. }
  10472. struct Golden
  10473. {
  10474. int i;
  10475. float l, r;
  10476. };
  10477. auto run = [&] (const std::vector<std::uint8_t>& state, double bpm, const std::vector<Golden>& gold) {
  10478. const auto slot = lynx::reeverb2FromFl (state);
  10479. CHECK (slot.has_value() && slot->kind == lynx::EffectKind::reeverb2);
  10480. if (! slot)
  10481. return;
  10482. auto fx = lynx::makeEffect (*slot);
  10483. fx->prepare (44100.0);
  10484. fx->reset();
  10485. std::vector<float> l (inL), r (inR);
  10486. for (int s = 0; s < N; s += 512)
  10487. fx->process (l.data() + s, r.data() + s, 512, slot->params, bpm);
  10488. for (const auto& g : gold)
  10489. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  10490. };
  10491. // modulation, early reflections, negative separation, filters, predelay
  10492. run (hex ("112700004b000000780000001e0000004600000055000000190000003c0000009600000090010000e2ffffff6e000000640000008c00000084030000b00400000000"),
  10493. 120.0, { { 777, 0.263778746f, 0.131889373f }, { 1555, 0.187972173f, 0.0587911084f }, { 3333, 0.10398978f, 0.0566705838f },
  10494. { 5000, 0.087177977f, -0.0197417438f }, { 8191, 0.0769432634f, -0.282023013f } });
  10495. // Mid/Side input, tempo-synced predelay (27 ticks at 128 BPM), positive separation
  10496. run (hex ("112700004b000000280000001b00000032000000640000000f0000002800000064000000f401000028000000800000000000000040000000ac020000000000000101"),
  10497. 128.0, { { 777, 0.38499999f, 0.192499995f }, { 1555, 0.275000006f, 0.137500003f }, { 3333, 0.164420277f, 0.0826604739f },
  10498. { 5000, -0.00861243438f, -0.00140450173f }, { 8191, -0.00873835851f, 0.00888271444f } });
  10499. // version 10000 (no modulation controls), everything off or at its extreme, dry 0
  10500. run (hex ("1027000013000000dd000000000000006400000000000000c8000000dd00000014000000d00700004000000000000000a0000000a00000000000"),
  10501. 120.0, { { 777, 0.0f, 0.0f }, { 1555, 0.0f, 0.0f }, { 3333, 0.0632086769f, 0.0632086769f }, { 5000, -0.108072646f, -0.108072646f },
  10502. { 8191, 0.144686908f, 0.144686908f } });
  10503. // the parameters map back to FL's integers; unsupported states fall back to the approximation
  10504. lynx::reeverb2::Settings s;
  10505. s.p = { 3000, 5, 384, 1, 0, 200, 221, 20, 2000, -64, 160, 0, 160, 2048, 1 };
  10506. s.tempoPredelay = true;
  10507. lynx::EffectParams p {};
  10508. lynx::reeverb2::toParams (s, p);
  10509. CHECK (lynx::reeverb2::fromParams (p) == s);
  10510. CHECK (! lynx::reeverb2FromFl ({ 0x11, 0x27, 0, 0, 1, 0 }).has_value());
  10511. CHECK (! lynx::reeverb2FromFl ({ 0x12, 0x27, 0, 0 }).has_value());
  10512. const auto approx = lynx::effectFromFl ("Fruity Reeverb 2", { 0x11, 0x27, 0, 0 }, 120.0);
  10513. CHECK (! approx || approx->kind != lynx::EffectKind::reeverb2);
  10514. CHECK (lynx::reeverb2ParamText (0, 0.0f) == "off" && lynx::reeverb2ParamText (5, 1.0f) == "20.0 s");
  10515. // Fruity Reeverb (the first version): Fruity Reeverb_x64.dll goldens on our own states (denormals flushed, as the
  10516. // plugin runs and as LYNX renders)
  10517. lynx::ScopedFlushDenormals ftz;
  10518. auto run1 = [&] (const std::vector<std::uint8_t>& state, const std::vector<Golden>& gold) {
  10519. const auto slot = lynx::reeverb1FromFl (state);
  10520. CHECK (slot.has_value() && slot->kind == lynx::EffectKind::reeverb1);
  10521. if (! slot)
  10522. return;
  10523. auto fx = lynx::makeEffect (*slot);
  10524. fx->prepare (44100.0);
  10525. std::vector<float> l (inL), r (inR);
  10526. for (int s = 0; s < N; s += 512)
  10527. fx->process (l.data() + s, r.data() + s, 512, slot->params, 120.0);
  10528. for (const auto& g : gold)
  10529. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  10530. };
  10531. run1 (hex ("01000000204e0000204e000025000000409c000050c300000a00000030750000a861000050c3000060ea0000"),
  10532. { { 777, 0.293731689f, 0.146865845f }, { 1555, 0.20980835f, 0.104904175f }, { 3333, 0.0998796821f, 0.0754205957f },
  10533. { 5000, -0.0994476378f, 0.0116700521f }, { 8191, -0.141061321f, 0.0601170398f } });
  10534. run1 (hex ("0100000000000000000001000000000000000100000001002800000000000100000001000000000000000100"),
  10535. { { 777, -0.016678432f, -4.58686594e-31f }, { 1555, 0.0221031085f, 0.0230635051f }, { 3333, 0.0875115693f, 0.1695389f },
  10536. { 5000, -0.111421227f, -0.295867324f }, { 8191, 0.503498077f, 0.190456569f } });
  10537. CHECK (lynx::reeverb1FromFl (std::vector<std::uint8_t> (44, 0)).has_value());
  10538. CHECK (! lynx::reeverb1FromFl (std::vector<std::uint8_t> (40, 0)).has_value());
  10539. CHECK (lynx::reeverb1ParamText (5, 0.0f) == "Brighter" && lynx::reeverb1ParamText (0, 0.0f) == "off");
  10540. }
  10541. // ---- Fruity Love Philter (REA_rodada66) ---------------------------------------------------------------------------------
  10542. TEST (love_philter_engine)
  10543. {
  10544. // goldens: Fruity Love Philter_x64.dll on our own synthetic states (44.1 kHz, blocks of 512, 120 BPM, live mode),
  10545. // bit-identical on the test machine (denormals flushed, as the plugin runs and as LYNX renders)
  10546. lynx::ScopedFlushDenormals ftz;
  10547. auto hex = [] (const char* h) {
  10548. std::vector<std::uint8_t> v;
  10549. for (std::size_t i = 0; h[i] != 0 && h[i + 1] != 0; i += 2)
  10550. v.push_back ((std::uint8_t) std::stoi (std::string (h + i, 2), nullptr, 16));
  10551. return v;
  10552. };
  10553. constexpr int N = 8192;
  10554. std::vector<float> inL (N), inR (N);
  10555. std::uint32_t seed = 12345;
  10556. for (int i = 0; i < N; ++i)
  10557. {
  10558. seed = (seed * 1103515245u + 12345u) & 0x7fffffffu;
  10559. const auto nz = (float) ((double) seed / 2147483648.0 - 0.5);
  10560. const auto tri = (float) ((i % 200) - 100) / 100.0f;
  10561. const float g = i < 4000 ? 1.0f : 0.0f;
  10562. inL[(std::size_t) i] = 0.5f * tri * g + (i >= 100 && i < 300 ? nz * 0.6f : 0.0f);
  10563. inR[(std::size_t) i] = 0.25f * tri * g;
  10564. }
  10565. struct Golden
  10566. {
  10567. int i;
  10568. float l, r;
  10569. };
  10570. auto run = [&] (const std::vector<std::uint8_t>& state, const std::vector<Golden>& gold) {
  10571. const auto slot = lynx::effectFromFl ("Fruity Love Philter", state, 120.0);
  10572. CHECK (slot.has_value() && slot->kind == lynx::EffectKind::lovePhilter);
  10573. if (! slot || slot->kind != lynx::EffectKind::lovePhilter)
  10574. return;
  10575. auto fx = lynx::makeEffect (*slot);
  10576. fx->prepare (44100.0);
  10577. std::vector<float> l (inL), r (inR);
  10578. for (int s = 0; s < N; s += 512)
  10579. {
  10580. fx->setSongPosition (s * 2.0 * 960.0 / 44100.0);
  10581. fx->process (l.data() + s, r.data() + s, 512, slot->params, 120.0);
  10582. }
  10583. for (const auto& g : gold)
  10584. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  10585. };
  10586. // SVF triple with Low/Band/High, a song-locked volume pattern with tension, a cutoff LFO; Lime HP fed by NEXT, panned
  10587. run (hex (
  10588. "03000c00789cedda316ed44018c5f1cfd90b5050234a0e81702a1a6e4045c11d68e7005c00c40928e96810254590380225e552538471ec"
  10589. "d97c314c841e0259cfff91463bf66e7e3b9ac42fdf582e1151961e776abf1f314ce3b3a5d7f6e4fb8bf1b2b6693c5cf56118e6b79ed5fe"
  10590. "b2f6b7b53fadfd41ed8f1379378d9fd77e88a50d57c3c3f597dcd28ee3fcfaadbe9e8fcb24e272d56238fd4019a73e2c5f3074c1ebf732"
  10591. "745a8ee51507670bce99e89cbff9e5076eb9f8be8e378f9b7df3fcfae2dbba37afcb9c0b391b0eb16ec795ff657cfd6a6aefc7cf17178f"
  10592. "f2f9f6f9fafe8769342c7dfabe58e65e62db7f5338fb74fe4f96acafa5e3f8bbf3eb6b75eb1e75068eb3d3b241b95ef2ffc0d67ebc8bb8"
  10593. "b792b65e2ffc9bfa636a64068e9fc3de44f77a7b93adfc6e7170fec661afa17b64038eb34336e81ed980e3ec900dba4736e0383b6483ee"
  10594. "957aee61ed9fe27a4d63194ffd63fdccf44cc7f972fef42408cf6ce0e06c30a7b6711f93fba238387fee50c3e81e75038eb343dda07b64"
  10595. "038eb34336e81e7b0a1c67873d85ee910d38ce0ed9a07b64038eb34336e81ed980e3ec900dba57625ec3d65bcbc7799cd6b83f95ded478"
  10596. "d60267c70ef745758f1a06c7d9a186d13dea061c6787ba41f7c8061c67876cd03df61438ce0e7b0add231b709c1db241f7c8061c67876c"
  10597. "d03db201c7d9211b74afc4bc86adb7968ff338ad717f2abda9f1ac05ce8e1dee8bea1e350c8eb3430da37bd40d38ce0e7583ee910d38ce"
  10598. "0ed9a07bec29709c1df614ba4736e0383b6483ee910d38ce0ed9a07b64038eb34336e85e89790d5b6f2d1fe7715ae3fe547a53e3590b9c"
  10599. "1d3bdc17d53d6a181c67871a46f7a81b709c1dea06dd231b709c1db241f7d853e0383bec29748f6cc07176c806dd231b709c1db241f7c8"
  10600. "061c67876cd0bd12f31ab6de5a3ecee3b4c6fda9f4a6c6b316383b76b82faa7bd43038ce0e358cee5137e0383bd40dba4736e0383b6483"
  10601. "eeb1a7c07176d853e81ed980e3ec900dba4736e0383b6483ee910d38ce0ed9a07b25e6356cbdb57c9cc7698dfb53e94d8d672d7076ec70"
  10602. "5f54f7a861709c1d6a18dda36ec07176a81b748f6cc07176c806dd634f81e3ecb0a7d03db201c7d9211b748f6cc07176c806dd231b709c"
  10603. "1db241f74acc6bd87a6bf9388fd31af7a7d29b1acf5ae0ecd8e1bea8ee51c3e0383bd430ba47dd80e3ec5037e81ed980e3ec900dbac79e"
  10604. "02c7d9614fa17b64038eb34336e81ed980e3ec900dba4736e0383b6483eec54ff7970e90"),
  10605. { { 777, 0.710252046585083f, 0.19971221685409546f }, { 1555, 0.2758101224899292f, 0.07770585268735886f }, { 2222, -0.5605983734130859f, -0.14688193798065186f }, { 3333, -0.33108609914779663f, -0.07413796335458755f }, { 3999, 0.3015991151332855f, 0.07990600168704987f }, { 4100, 0.09223534911870956f, 0.021577393636107445f } });
  10606. // Mango alternate triple with drive, waveshaper, Mod X/Y maps (smoothed), follower map on the volume; ×4 oversampling
  10607. run (hex (
  10608. "03000c00789cedda4d8ed33018c671a788151b8ec0013803993dd7e00e6c7d0a24100bae81c40590e00e5d82c46238c1e0c879db7752b9"
  10609. "8b470c1f4ffe963c759af6371e4b79e675945a4aa96b2f4f5b7f56cad45e3e1d4a79be0c5a7bf9f3f57cd7da326e6fb7f3d3b49e5abef6"
  10610. "a1f537adbf2ae7968f4f7c6b8f5b7fb20ca6e951fb19fd7a3bceefde2eedfbf2fa22bfdf5f6fe7fce9bb4d2bd3e9549d973eadbf742adb"
  10611. "7686e25c86f2df51d7cfe0e0fc6de7203a37ef2fbe70ba200f17678ef3fde3b08f572fbe7fddebebd273816cc07173fe4c36dc0eaebdeb"
  10612. "ff981fcabb2c28cedfeb85c4c70befeb97a5fd580b8dcfcbfba773dbdf433d81e3ec3c7466a46bec9e1d457e7e777bedfd1ff5c4d2c806"
  10613. "1c3f87bd86eeb1d7c07176f6b8d7f85d1ed980e3ec900dba4736e0383b6483ee910d38ce0ed9a07bb5f4358c1e2d1fe7715ae3f1544653"
  10614. "fbd65e6fe650b65349363985e3e7705f54f7a861709c1d6a18dda36ec07176a81b748f6cc07176c806dd634f81e3ecb0a7d03db201c7d9"
  10615. "211b748f6cc07176c806dd231b709c1db241f76ae96b183d5a3ecee3b4c6e3a98ca6c6b316383b76b82faa7bd43038ce0e358cee5137e0"
  10616. "383bd40dba4736e0383b6483eeb1a7c07176d853e81ed980e3ec900dba4736e0383b6483ee910d38ce0ed9a07bb5f4358c1e2d1fe7715a"
  10617. "e3f1544653e3590b9c1d3bdc17d53d6a181c67871a46f7a81b709c1dea06dd231b709c1db241f7d853e0383bec29748f6cc07176c806dd"
  10618. "231b709c1db241f7c8061c67876cd0bd5afa1a468f968ff338adf1782aa3a9f1ac05ce8e1dee8bea1e350c8eb3430da37bd40d38ce0e75"
  10619. "83ee910d38ce0ed9a07bec29709c1df614ba4736e0383b6483ee910d38ce0ed9a07b64038eb34336e85e2d7d0da347cbc7799cd6783c95"
  10620. "d1d478d60267c70ef745758f1a06c7d9a186d13dea061c6787ba41f7c8061c67876cd03df61438ce0e7b0add231b709c1db241f7c8061c"
  10621. "67876cd03db201c7d9211b74af96be86d1a3e5e33c4e6b3c9eca686a3c6b81b36387fba2ba470d83e3ec50c3e81e75038eb343dda07b64"
  10622. "038eb34336e81e7b0a1c67873d85ee910d38ce0ed9a07b64038eb34336e81ed980e3ec900dba574b5fc3e8d1f2711ea7351e4f6534359e"
  10623. "b5c0d9b1c37d51dda386c17176a861748fba01c7d9a16ed03db201c7d9211b748f3d058eb3c39e42f7c8061c67876cd03db201c7d9211b"
  10624. "748f6cc07176c806dd2bbf0040771f82"),
  10625. { { 777, 0.30564039945602417f, 0.28222185373306274f }, { 1555, 0.262472540140152f, 0.23502512276172638f }, { 2222, 0.3336450159549713f, -0.030735457316040993f }, { 3333, 0.23046477138996124f, 0.20232810080051422f }, { 3999, 0.3282865881919861f, 0.300540566444397f }, { 4100, -1.5846843837152846e-07f, 2.583440448233887e-07f } });
  10626. // band-pass alternate dual with FLAT, low shelf, all-pass with a pan LFO and inverted input, CENTER, no oversampling
  10627. run (hex (
  10628. "03000c00789ceddd418ad44018c5f12f3303baf404e2d243083d7b6fe0dab55b5dd64106bc8427f024aea497ed09da8a95225ff75883bc"
  10629. "01092fff82a22b19fb97a2248faf62184b4494a5c7abdadf444cf3f866e9b5bdfff5f970ae2d9636cdad0de7af7da9fd6327623d5ffe32"
  10630. "be5b7afdfe6dfdb85d2ff2443b1ddae7b17edecfe33f173f5fb598d6ab1fe63e2d179886e0fab30c5dcf1d07670bce8de8dc3f3cfac213"
  10631. "37df8fc3e571b72fcf5fdf7c5bf7dabab45c201b70dc9cff930da7c1bd7712efe56d78d40d38ce0e7583ee910d38ce0ed9a07bec29709c"
  10632. "1df614ba4736e0383b6483ee910d38ce0ed9a07b64038eb34336e85e89b686bdf7968ff338adf1782aa3a9f1ae05ce8e1d9e8bea1e350c"
  10633. "8eb3430da37bd40d38ce0e7583ee910d38ce0ed9a07bec29709c1df614ba4736e0383b6483ee910d38ce0ed9a07b64038eb34336e85ea9"
  10634. "e73ed4feaef64fe9cf95a5bf8ef67b2fca727ebeeecb79c0efb5c0c1d9604e6de33926cf457170fedda186d13dea061c6787ba41f7c806"
  10635. "1c67876cd03df61438ce0e7b0add231b709c1db241f7c8061c67876cd03db201c7d9211b74afd473df6aff19eb9ac6322e69fc706eff05"
  10636. "c9bc8e2fda603c95d1d478d70267c74ecfa9c737cc7170ef1e2ffce9cafffeb5fdec6db477a222785ebab5bf731c9ce738d436ba473d81"
  10637. "e3ecf0efacba4736e0383b6483eeb1a7c07176d853e81ed980e3ec900dba4736e0383b6483ee910d38ce0ed9a07b25da1af6de5b3ecee3"
  10638. "b4c6e3a98ca6c63b18383b76782eaa7bd43038ce0e358cee5137e0383bd40dba4736e0383b6483eeb1a7c07176d853e81ed980e3ec900d"
  10639. "ba4736e0383b6483ee910d38ce0ed9a07b25da1af6de5b3ecee3b4c6e3a98ca6c6bb16383b76782eaa7bd43038ce0e358cee5137e0383b"
  10640. "d40dba4736e0383b6483eeb1a7c07176d853e81ed980e3ec900dba4736e0383b6483ee910d38ce0ed9a07b25da1af6de5b3ecee3b4c6e3"
  10641. "a98ca6c6bb16383b76782eaa7bd43038ce0e358cee5137e0383bd40dba4736e0383b6483eeb1a7c07176d853e81ed980e3ec900dba4736"
  10642. "e0383b6483ee910d38ce0ed9a07b25da1af6de5b3ecee3b4c6e3a98ca6c6bb16383b76782eaa7bd43038ce0e358cee5137e0383bd40dba"
  10643. "4736e0383b6483eeb1a7c07176d853e81ed980e3ec900dba4736e0383b6483ee910d38ce0ed9a07bf11b286e0820"),
  10644. { { 777, 0.5369307994842529f, 0.483487993478775f }, { 1555, -0.17133042216300964f, -0.10717462003231049f }, { 2222, 0.8671267032623291f, 0.6469616889953613f }, { 3333, -0.6548674702644348f, -0.5142713189125061f }, { 3999, 1.4368892908096313f, 1.1726332902908325f }, { 4100, 1.1042776107788086f, 0.8842349648475647f } });
  10645. // the slot exposes FL's controls; FL indices map both ways; unreadable states keep the old conversion
  10646. CHECK (lynx::philter::flIndexOf (6) == 6 && lynx::philter::flIndexOf (8) == 9 && lynx::philter::flIndexOf (8 + 5 * 7 + 4) == 8 + 44 * 7 + 9);
  10647. for (int i = 0; i < lynx::philter::paramCount; ++i)
  10648. CHECK (lynx::philter::paramOfFlIndex (lynx::philter::flIndexOf (i)) == i);
  10649. CHECK (lynx::philter::paramOfFlIndex (8 + 3) == 48 && lynx::philter::paramOfFlIndex (8 + 12) == -1 && lynx::philter::paramOfFlIndex (8 + 44 * 7 + 11) == -1);
  10650. CHECK (lynx::philter::parse ({ 3, 0, 12, 0, 1, 2, 3, 4 }) == nullptr);
  10651. CHECK (lynx::effectInfo (lynx::EffectKind::lovePhilter).params.size() == (std::size_t) lynx::philter::paramCount);
  10652. CHECK (lynx::philter::paramText (5, 0.0f) == "pattern 1" && lynx::philter::paramText (8 + 3, 0.5f) == "centred");
  10653. // a control edited in LYNX is applied when the effect is built from the slot (the state keeps FL's value)
  10654. {
  10655. const auto stateA = hex (
  10656. "03000c00789cedda316ed44018c5f1cfd90b5050234a0e81702a1a6e4045c11d68e7005c00c40928e96810254590380225e552538471ec"
  10657. "d97c314c841e0259cfff91463bf66e7e3b9ac42fdf582e1151961e776abf1f314ce3b3a5d7f6e4fb8bf1b2b6693c5cf56118e6b79ed5fe"
  10658. "b2f6b7b53fadfd41ed8f1379378d9fd77e88a50d57c3c3f597dcd28ee3fcfaadbe9e8fcb24e272d56238fd4019a73e2c5f3074c1ebf732"
  10659. "745a8ee51507670bce99e89cbff9e5076eb9f8be8e378f9b7df3fcfae2dbba37afcb9c0b391b0eb16ec795ff657cfd6a6aefc7cf17178f"
  10660. "f2f9f6f9fafe8769342c7dfabe58e65e62db7f5338fb74fe4f96acafa5e3f8bbf3eb6b75eb1e75068eb3d3b241b95ef2ffc0d67ebc8bb8"
  10661. "b792b65e2ffc9bfa636a64068e9fc3de44f77a7b93adfc6e7170fec661afa17b64038eb34336e81ed980e3ec900dba4736e0383b6483ee"
  10662. "957aee61ed9fe27a4d63194ffd63fdccf44cc7f972fef42408cf6ce0e06c30a7b6711f93fba238387fee50c3e81e75038eb343dda07b64"
  10663. "038eb34336e81e7b0a1c67873d85ee910d38ce0ed9a07b64038eb34336e81ed980e3ec900dba57625ec3d65bcbc7799cd6b83f95ded478"
  10664. "d60267c70ef745758f1a06c7d9a186d13dea061c6787ba41f7c8061c67876cd03df61438ce0e7b0add231b709c1db241f7c8061c67876c"
  10665. "d03db201c7d9211b74afc4bc86adb7968ff338ad717f2abda9f1ac05ce8e1dee8bea1e350c8eb3430da37bd40d38ce0e7583ee910d38ce"
  10666. "0ed9a07bec29709c1df614ba4736e0383b6483ee910d38ce0ed9a07b64038eb34336e85e89790d5b6f2d1fe7715ae3fe547a53e3590b9c"
  10667. "1d3bdc17d53d6a181c67871a46f7a81b709c1dea06dd231b709c1db241f7d853e0383bec29748f6cc07176c806dd231b709c1db241f7c8"
  10668. "061c67876cd0bd12f31ab6de5a3ecee3b4c6fda9f4a6c6b316383b76b82faa7bd43038ce0e358cee5137e0383bd40dba4736e0383b6483"
  10669. "eeb1a7c07176d853e81ed980e3ec900dba4736e0383b6483ee910d38ce0ed9a07b25e6356cbdb57c9cc7698dfb53e94d8d672d7076ec70"
  10670. "5f54f7a861709c1d6a18dda36ec07176a81b748f6cc07176c806dd634f81e3ecb0a7d03db201c7d9211b748f6cc07176c806dd231b709c"
  10671. "1db241f74acc6bd87a6bf9388fd31af7a7d29b1acf5ae0ecd8e1bea8ee51c3e0383bd430ba47dd80e3ec5037e81ed980e3ec900dbac79e"
  10672. "02c7d9614fa17b64038eb34336e81ed980e3ec900dba4736e0383b6483eec54ff7970e90");
  10673. auto slot = lynx::philter::slotFromFl (stateA);
  10674. CHECK (slot.has_value() && ! lynx::philter::slotFromFl (hex ("0300000078")).has_value());
  10675. if (slot)
  10676. {
  10677. auto render = [&] (const lynx::EffectSlot& sl) {
  10678. auto fx = lynx::makeEffect (sl);
  10679. fx->prepare (44100.0);
  10680. std::vector<float> l (inL), r (inR);
  10681. for (int s = 0; s < 4096; s += 512)
  10682. {
  10683. fx->setSongPosition (s * 2.0 * 960.0 / 44100.0);
  10684. fx->process (l.data() + s, r.data() + s, 512, sl.params, 120.0);
  10685. }
  10686. return l[3333];
  10687. };
  10688. const auto base = render (*slot);
  10689. auto edited = *slot;
  10690. edited.params[8] = 0.1f; // unit 1 cutoff
  10691. CHECK (base == 0.0f || base != render (edited));
  10692. CHECK (base == render (*slot));
  10693. }
  10694. }
  10695. }
  10696. TEST (fruity_vocoder_round66_goldens)
  10697. {
  10698. // REA_rodada66 §6 goldens against the round-70 engine (core/FruityVocoder), through FL's state import and the mixer
  10699. // effect: Fruity Vocoder_x64.dll on our own states (44.1 kHz, blocks of 512): left = modulator, right = carrier.
  10700. // The first state's attack (5 ms) is below the knob range (10…1000): the plugin uses it as it is (REA_rodada78 §1)
  10701. lynx::ScopedFlushDenormals ftz;
  10702. auto hex = [] (const char* h) {
  10703. std::vector<std::uint8_t> v;
  10704. for (std::size_t i = 0; h[i] != 0 && h[i + 1] != 0; i += 2)
  10705. v.push_back ((std::uint8_t) std::stoi (std::string (h + i, 2), nullptr, 16));
  10706. return v;
  10707. };
  10708. constexpr int N = 8192;
  10709. std::vector<float> inL (N), inR (N);
  10710. std::uint32_t seed = 12345;
  10711. for (int i = 0; i < N; ++i)
  10712. {
  10713. seed = (seed * 1103515245u + 12345u) & 0x7fffffffu;
  10714. const auto nz = (float) ((double) seed / 2147483648.0 - 0.5);
  10715. const auto tri = (float) ((i % 200) - 100) / 100.0f;
  10716. const float g = i < 4000 ? 1.0f : 0.0f;
  10717. inL[(std::size_t) i] = 0.5f * tri * g + (i >= 100 && i < 300 ? nz * 0.6f : 0.0f);
  10718. inR[(std::size_t) i] = 0.25f * tri * g;
  10719. }
  10720. struct Golden
  10721. {
  10722. int i;
  10723. float v;
  10724. };
  10725. auto run = [&] (const std::vector<std::uint8_t>& state, const std::vector<Golden>& gold) {
  10726. const auto slot = lynx::effectFromFl ("Fruity Vocoder", state, 120.0);
  10727. CHECK (slot.has_value() && slot->kind == lynx::EffectKind::vocoder);
  10728. if (! slot || slot->kind != lynx::EffectKind::vocoder)
  10729. return;
  10730. auto fx = lynx::makeEffect (*slot);
  10731. fx->prepare (44100.0);
  10732. std::vector<float> l (inL), r (inR);
  10733. for (int s = 0; s < N; s += 512)
  10734. fx->process (l.data() + s, r.data() + s, 512, slot->params, 120.0);
  10735. for (const auto& g : gold)
  10736. CHECK (l[(std::size_t) g.i] == g.v && r[(std::size_t) g.i] == g.v);
  10737. };
  10738. // 32 bands, order 3, inverted carrier bank with a formant shift, fast attack (5 ms, outside the knob range)
  10739. run (hex (
  10740. "02000000200000000300000002000000000000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000"
  10741. "803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f00"
  10742. "00803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803fb80b000050c30000640000000100000044fdffff"
  10743. "78000000050000002c010000000000008c0000003c000000a0000000"),
  10744. { { 777, 0.47870129346847534f }, { 1555, 0.2731437087059021f }, { 2222, -0.41599133610725403f }, { 3333, 0.083399198949337f }, { 3999, 0.7295553088188171f }, { 4100, 0.0871216207742691f } });
  10745. // 8 bands, swapped inputs, per-band levels, modulator level 0
  10746. run (hex (
  10747. "02000000080000000200000002000000010000003f9a99193f3333333fcdcc4c3f6666663f0000803fcdcc8c3f9a99993f1027000060ea"
  10748. "00001e000000000000002c010000500000000f0000003c00000000000000000000001400000080000000"),
  10749. { { 777, 0.07233863323926926f }, { 1555, 0.1183714047074318f }, { 2222, -0.6131120920181274f }, { 3333, 0.18050849437713623f }, { 3999, 0.056527279317379f }, { 4100, 0.05711518973112106f } });
  10750. }
  10751. // ---- Fruity WaveShaper / Fruity Soft Clipper (REA_rodada51) ------------------------------------------------------------
  10752. // States written here from our own values (no FL preset data); goldens from the installation's DLLs (EXEC, blocks of 512)
  10753. namespace
  10754. {
  10755. std::vector<std::uint8_t> waveShaperTestState (int pre, int mix, int post, bool uni, bool center, int os, bool hq, bool smooth)
  10756. {
  10757. std::vector<std::uint8_t> b;
  10758. 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))); };
  10759. 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))); };
  10760. 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))); };
  10761. i32 (0x40003);
  10762. i32 (pre), i32 (mix), i32 (post);
  10763. b.push_back (uni ? 1 : 0), b.push_back (center ? 1 : 0);
  10764. i32 (os);
  10765. 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 },
  10766. { 0.25, 0.9, -0.3f, 3 }, { 0.2, 1.0, 0.5f, 9 } };
  10767. i32 (3), i32 (3), i32 (5);
  10768. for (const auto& p : pts)
  10769. {
  10770. f64 (p.dt), f64 (p.y), f32 (p.t);
  10771. b.push_back ((std::uint8_t) p.mode), b.push_back (0), b.push_back (0), b.push_back (0);
  10772. }
  10773. i32 (0), i32 (-1), i32 (-1), i32 (-1), i32 (-1);
  10774. i32 (1);
  10775. b.push_back (hq ? 1 : 0), b.push_back (smooth ? 1 : 0);
  10776. return b;
  10777. }
  10778. } // namespace
  10779. TEST (fruity_waveshaper_and_soft_clipper)
  10780. {
  10781. std::vector<float> inL (4096), inR (4096);
  10782. for (int n = 0; n < 4096; ++n)
  10783. {
  10784. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  10785. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  10786. }
  10787. struct Gold { int i; float l, r; };
  10788. auto close = [] (float a, float b) { return std::abs (a - b) <= x87Tolerance() * std::max (1.0f, std::abs (b)); };
  10789. // Soft Clipper: bit-exact against Fruity Soft Clipper_x64.dll
  10790. auto clip = [&] (int thr, int post, std::initializer_list<Gold> gold) {
  10791. std::vector<float> l (inL), r (inR);
  10792. for (int s = 0; s < 4096; s += 512)
  10793. {
  10794. lynx::flshape::softClip (l.data() + s, 512, thr, post);
  10795. lynx::flshape::softClip (r.data() + s, 512, thr, post);
  10796. }
  10797. for (const auto& g : gold)
  10798. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  10799. };
  10800. clip (100, 128, { { 100, 0.0f, -0.949052989f }, { 1000, -0.99617976f, 0.872887492f }, { 2047, 0.649999976f, -0.944175363f },
  10801. { 3000, -0.99617976f, -0.699999988f }, { 4095, 0.25f, 0.600000024f } });
  10802. clip (1, 150, { { 100, 0.0f, -1.04588532f }, { 1000, -1.27003884f, 0.930835366f }, { 2047, 0.750244498f, -1.03539562f },
  10803. { 3000, -1.27003884f, -0.790091455f }, { 4095, 0.347655922f, 0.708338201f } });
  10804. clip (64, 140, { { 100, 0.0f, -1.08492827f }, { 1000, -1.21535313f, 0.990323186f }, { 2047, 0.804165959f, -1.0770781f },
  10805. { 3000, -1.21535313f, -0.849189103f }, { 4095, 0.319320858f, 0.754407823f } });
  10806. {
  10807. const std::vector<std::uint8_t> st { 64, 0, 0, 0, 140, 0, 0, 0 };
  10808. const auto slot = lynx::effectFromFl ("Fruity Soft Clipper", st, 120.0);
  10809. CHECK (slot && slot->kind == lynx::EffectKind::fruitySoftClipper);
  10810. if (slot)
  10811. {
  10812. const auto s = lynx::flshape::softClipperFromParams (slot->params.data());
  10813. CHECK (s.threshold == 64 && s.post == 140);
  10814. auto fx = lynx::makeEffect (*slot);
  10815. fx->prepare (44100.0);
  10816. std::vector<float> l (inL), r (inR);
  10817. fx->process (l.data(), r.data(), 4096, slot->params, 120.0);
  10818. CHECK (l[3000] == -1.21535313f && r[4095] == 0.754407823f);
  10819. }
  10820. }
  10821. // WaveShaper: tables (bipolar / unipolar) and the engine on three of our states, live and as FL renders
  10822. const auto stA = waveShaperTestState (150, 100, 120, false, true, 1, true, true); // CENTER, ×2, HQ, smooth
  10823. const auto stB = waveShaperTestState (100, 128, 128, true, false, 0, false, false); // unipolar
  10824. const auto stC = waveShaperTestState (192, 90, 100, false, false, 2, false, false); // ×4
  10825. const auto pA = lynx::flshape::parseWaveShaper (stA);
  10826. CHECK (pA && pA->points.size() == 5 && pA->pre == 150 && pA->center && pA->hq && pA->smooth && pA->oversampling == 1);
  10827. if (pA)
  10828. {
  10829. const auto T = lynx::flshape::shaperTable (pA->points, false);
  10830. CHECK (T.size() == 16386 && T[1] == 3277 && T[1001] == 18824 && T[5001] == 26195 && T[9001] == 11469 && T[12001] == 24614
  10831. && T[16384] == 32767 && T[0] == T[1]);
  10832. const auto U = lynx::flshape::shaperTable (pA->points, true);
  10833. CHECK (U.size() == 32770 && U[1] == -26214 && U[1001] == -6408 && U[5001] == 17073 && U[9001] == 19551 && U[12001] == 14549);
  10834. }
  10835. auto shape = [&] (const std::vector<std::uint8_t>& st, bool render, std::initializer_list<Gold> gold) {
  10836. const auto s = lynx::flshape::parseWaveShaper (st);
  10837. CHECK (s.has_value());
  10838. if (! s)
  10839. return;
  10840. lynx::flshape::WaveShaperEngine e;
  10841. e.setCurve (s->points);
  10842. e.prepare (44100.0);
  10843. e.setControls (s->pre, s->mix, s->post, s->oversampling, s->hq, s->smooth, s->center, s->unipolar, render);
  10844. std::vector<float> l (inL), r (inR);
  10845. for (int i = 0; i < 4096; i += 512)
  10846. e.process (l.data() + i, r.data() + i, 512);
  10847. for (const auto& g : gold)
  10848. CHECK (close (l[(std::size_t) g.i], g.l) && close (r[(std::size_t) g.i], g.r));
  10849. };
  10850. shape (stA, false, { { 100, 0.59108448f, 0.469315082f }, { 1000, 0.284353405f, 1.02621937f }, { 2047, -0.533613563f, 0.117038473f },
  10851. { 3000, 0.22673659f, -1.13572407f }, { 4095, -0.267153352f, 0.822866499f } });
  10852. shape (stA, true, { { 100, 0.794869065f, -0.127601087f }, { 1000, -0.00841622613f, 0.900729597f }, { 2047, 0.450603426f, -0.325943232f },
  10853. { 3000, -0.0530530401f, -0.32303831f }, { 4095, 1.0959363f, 0.946009099f } });
  10854. shape (stB, false, { { 100, -0.206451416f, 0.502471924f }, { 1000, -0.799987793f, 0.761169434f }, { 2047, 0.411743164f, 0.514160156f },
  10855. { 3000, -0.799987793f, 0.596221924f }, { 4095, -0.157653809f, 0.334106445f } });
  10856. shape (stB, true, { { 100, -0.206390381f, 0.502493739f }, { 1000, -0.799987793f, 0.761169434f }, { 2047, 0.411743164f, 0.514144361f },
  10857. { 3000, -0.799987793f, 0.596221924f }, { 4095, -0.157653809f, 0.334106445f } });
  10858. shape (stC, false, { { 100, 0.317797363f, 0.0483714938f }, { 1000, -0.445489585f, -0.533142388f }, { 2047, 0.634525597f, 0.942175806f },
  10859. { 3000, -0.445489585f, 0.514707327f }, { 4095, 0.200391173f, -0.937523127f } });
  10860. shape (stC, true, { { 100, 0.61165309f, -0.0993360579f }, { 1000, 0.0375693105f, 0.667849183f }, { 2047, 0.353716731f, -0.223758236f },
  10861. { 3000, 0.0375693105f, -0.223787636f }, { 4095, 0.843308687f, 0.688474894f } });
  10862. // the mixer effect from the FL import: controls as parameters, the curve in the slot's state; host blocks of any size
  10863. const auto slot = lynx::effectFromFl ("Fruity WaveShaper", stC, 120.0);
  10864. CHECK (slot && slot->kind == lynx::EffectKind::fruityWaveShaper && slot->state == stC);
  10865. if (slot)
  10866. {
  10867. auto fx = lynx::makeEffect (*slot);
  10868. fx->prepare (44100.0);
  10869. std::vector<float> l (inL), r (inR);
  10870. fx->process (l.data(), r.data(), 1024, slot->params, 120.0);
  10871. fx->process (l.data() + 1024, r.data() + 1024, 1024, slot->params, 120.0);
  10872. fx->process (l.data() + 2048, r.data() + 2048, 2048, slot->params, 120.0);
  10873. CHECK (close (l[1000], -0.445489585f) && close (r[2047], 0.942175806f) && close (l[4095], 0.200391173f));
  10874. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityWaveShaper, 3, slot->params[3], 120.0) == "x4");
  10875. }
  10876. // an unreadable state keeps the old approximation
  10877. CHECK (lynx::effectFromFl ("Fruity WaveShaper", { 1, 2, 3 }, 120.0)->kind == lynx::EffectKind::distortion);
  10878. CHECK (lynx::effectFromFl ("Fruity Soft Clipper", { 1, 2, 3 }, 120.0)->kind == lynx::EffectKind::distortion);
  10879. }
  10880. // ---- Fruity Parametric EQ 2 / Fruity Parametric EQ (REA_rodada50) -----------------------------------------------------
  10881. // States written here from our own values (no FL preset data); goldens from the installation's DLLs (EXEC, blocks of 512)
  10882. namespace
  10883. {
  10884. struct PeqTestBand { int type, freq, width, gain, order; };
  10885. std::vector<std::uint8_t> peq2TestState (const PeqTestBand (&bands)[7], int main, int uiFlags, int options, int mask)
  10886. {
  10887. std::vector<std::uint8_t> b;
  10888. 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))); };
  10889. i32 (8);
  10890. for (int copy = 0; copy < 2; ++copy)
  10891. {
  10892. for (const auto& x : bands) i32 (x.gain);
  10893. for (const auto& x : bands) i32 (x.freq);
  10894. for (const auto& x : bands) i32 (x.width);
  10895. for (const auto& x : bands) i32 (x.type);
  10896. for (const auto& x : bands) i32 (x.order);
  10897. i32 (main);
  10898. }
  10899. i32 (uiFlags), i32 (0), b.push_back (0), i32 (2), i32 (0), i32 (2), i32 (0);
  10900. b.push_back ((std::uint8_t) options);
  10901. i32 (mask), i32 (1), i32 (0);
  10902. for (int v : { 3, 1, 2, 2, 2 }) i32 (v);
  10903. i32 (0);
  10904. return b;
  10905. }
  10906. std::vector<std::uint8_t> peq1TestState (const PeqTestBand (&bands)[7], int main)
  10907. {
  10908. std::vector<std::uint8_t> b;
  10909. 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))); };
  10910. for (const auto& x : bands) i32 (x.gain);
  10911. for (const auto& x : bands) i32 (x.freq);
  10912. for (const auto& x : bands) i32 (x.width);
  10913. for (const auto& x : bands) i32 (x.type);
  10914. i32 (main);
  10915. return b;
  10916. }
  10917. } // namespace
  10918. // EQUO test state (our own LCG curves, no FL data; REA_rodada61 equo/teststate.py builds the same bytes)
  10919. static std::vector<std::uint8_t> equoTestState (int ovs, int bw, bool add)
  10920. {
  10921. std::vector<std::uint8_t> b;
  10922. 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); };
  10923. auto putI = [&put] (std::int32_t v) { put (&v, 4); };
  10924. auto putF = [&put] (float v) { put (&v, 4); };
  10925. std::uint32_t seed = 12345;
  10926. auto rnd = [&seed] {
  10927. seed = (std::uint32_t) (((std::uint64_t) seed * 1103515245u + 12345u) & 0x7fffffffu);
  10928. return (float) ((double) (seed % 20001) / 10000.0);
  10929. };
  10930. putI (4);
  10931. for (int k = 0; k < 93; ++k)
  10932. {
  10933. putF (rnd());
  10934. putI (k % 11 == 0 ? 1 : (k % 13 == 0 ? 2 : 0));
  10935. }
  10936. for (int bank = 0; bank < 9; ++bank)
  10937. for (int k = 0; k < 93; ++k)
  10938. {
  10939. const float v = rnd();
  10940. putF (bank == 8 ? 0.75f + v * 0.25f : v);
  10941. }
  10942. for (int v : { 1500, 90, 2600, 170, 150, ovs, bw, -1, 0, 0 })
  10943. putI (v);
  10944. for (int v : { 0, add ? 1 : 0, 0 })
  10945. b.push_back ((std::uint8_t) v);
  10946. putI (1);
  10947. return b;
  10948. }
  10949. // Pitch Shifter state (version 7, our own settings; REA_rodada61 ps/teststate.py builds the same bytes)
  10950. static std::vector<std::uint8_t> pitchShifterTestState (std::array<int, 13> p, int engine, int detector)
  10951. {
  10952. std::vector<std::uint8_t> b;
  10953. auto putI = [&b] (std::int32_t v) { const auto* q = (const std::uint8_t*) &v; b.insert (b.end(), q, q + 4); };
  10954. putI (7);
  10955. for (int v : p)
  10956. putI (v);
  10957. putI (p[11]);
  10958. b.push_back (0);
  10959. putI (engine);
  10960. putI (detector);
  10961. putI (1000);
  10962. b.push_back (1);
  10963. b.push_back (0);
  10964. return b;
  10965. }
  10966. TEST (pitch_shifter_exact)
  10967. {
  10968. std::vector<float> inL (8192), inR (8192);
  10969. for (int n = 0; n < 8192; ++n)
  10970. {
  10971. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0) * 0.3f;
  10972. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0) * 0.3f;
  10973. }
  10974. struct Gold { int i; float l, r; };
  10975. // goldens: Pitch Shifter_x64.dll on the same states and signal (REA_rodada61)
  10976. auto run = [&] (const std::vector<std::uint8_t>& st, int latency, std::initializer_list<Gold> gold) {
  10977. lynx::psfl::State s;
  10978. CHECK (st.size() == 75 && lynx::psfl::parsePitchShifter (st.data(), st.size(), s));
  10979. // through the mixer effect: FL import → slot parameters → effect, odd host blocks
  10980. const auto slot = lynx::effectFromFl ("Pitch Shifter", st, 120.0);
  10981. CHECK (slot && slot->kind == lynx::EffectKind::flPitchShifter);
  10982. if (! slot)
  10983. return;
  10984. auto fx = lynx::makeEffect (*slot);
  10985. fx->prepare (44100.0);
  10986. std::vector<float> l (inL), r (inR);
  10987. for (int i = 0, k = 0; i < 8192; ++k)
  10988. {
  10989. const int n = std::min (8192 - i, k % 2 == 0 ? 700 : 333);
  10990. fx->process (l.data() + i, r.data() + i, n, slot->params, 120.0);
  10991. i += n;
  10992. }
  10993. CHECK (fx->latencySamples() == latency);
  10994. for (const auto& g : gold)
  10995. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  10996. };
  10997. // "FFT" (pitch-synchronous) engine: +5 st, formant −3 st, correction 50 %, mix 79 %, accurate detector
  10998. run (pitchShifterTestState ({ 500, -300, 2, 1900, 8500, 200, 0, 0, 768, 0, 0, 32768, 0 }, 0, 1), 2457,
  10999. { { 1000, 0.0f, 0.0f }, { 3000, 0.142123833f, 0.0878119618f }, { 5000, 0.139498621f, -0.365035176f },
  11000. { 7000, 0.139498621f, 0.161137447f }, { 8191, 0.449655473f, 0.453882188f } });
  11001. // granular engine: −7 st, 60 ms grains, density 300 %, jitter 30 %, random 20 %, post delay with feedback 40 %
  11002. run (pitchShifterTestState ({ -700, 0, 2, 2400, 6000, 300, 30, 20, 200, 40, 1, 65536, 0 }, 1, 0), 0,
  11003. { { 1000, -0.0736689344f, -0.349420458f }, { 3000, 0.086801365f, 0.0471927896f }, { 5000, -0.0939825475f, 0.31712544f },
  11004. { 7000, -0.28462702f, -0.437854528f }, { 8191, 0.138916701f, -0.0426892526f } });
  11005. CHECK (lynx::formatEffectParam (lynx::EffectKind::flPitchShifter, 0, 0.75f, 120.0) == "+6.00 st");
  11006. }
  11007. TEST (equo_exact)
  11008. {
  11009. std::vector<float> inL (4096), inR (4096);
  11010. for (int n = 0; n < 4096; ++n)
  11011. {
  11012. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  11013. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  11014. }
  11015. struct Gold { int i; float l, r; };
  11016. // goldens: EQUO_x64.dll run on the same states and signal (REA_rodada61 §5)
  11017. auto run = [&] (const std::vector<std::uint8_t>& st, bool hq, bool ticks, std::initializer_list<Gold> gold) {
  11018. const auto s = lynx::equofx::parse (st);
  11019. CHECK (s.has_value());
  11020. if (! s)
  11021. return;
  11022. lynx::equofx::Engine e;
  11023. e.prepare (44100.0, hq);
  11024. e.set (*s);
  11025. std::vector<float> l (inL), r (inR);
  11026. if (! ticks)
  11027. e.process (l.data(), r.data(), 4096);
  11028. else
  11029. {
  11030. auto k = *s;
  11031. e.startFromConstruction();
  11032. for (int i = 0; i < 4096; i += 256)
  11033. {
  11034. if (i == 2048)
  11035. k.knob[2] = 5300, e.setKnobs (k);
  11036. e.tick();
  11037. e.process (l.data() + i, r.data() + i, 256);
  11038. }
  11039. }
  11040. for (const auto& g : gold)
  11041. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  11042. };
  11043. const auto plain = equoTestState (0, 150, false);
  11044. run (plain, false, false, { { 100, -0.00154058414f, -0.00550224911f }, { 1000, -0.908149242f, 0.619277894f }, { 2047, 0.411961317f, -0.364051819f },
  11045. { 3000, -1.42823684f, -0.0602950938f }, { 4095, -0.0661701784f, 0.300809383f } });
  11046. run (equoTestState (1, 300, true), false, false,
  11047. { { 100, 0.0312003642f, 0.0184187777f }, { 1000, 0.0379521511f, 0.224126667f }, { 2047, -0.135689721f, 0.0444660783f },
  11048. { 3000, 0.113949843f, -1.00256324f }, { 4095, -0.176232055f, 0.79159987f } });
  11049. run (plain, true, false, { { 100, -0.00104017637f, 0.000872685807f }, { 1000, -0.0426855832f, 0.0402083024f }, { 2047, 0.191993102f, 0.174184501f },
  11050. { 3000, -0.114048228f, 0.419817507f }, { 4095, 0.0376945846f, -0.274040252f } });
  11051. run (plain, false, true, { { 100, -0.0019897793f, -0.00981196482f }, { 1000, -1.09574401f, 0.83203429f }, { 2047, 0.839113712f, -0.691361725f } });
  11052. // the mixer effect from the FL import: FL's state in the slot, the knobs in its parameters, any host block size
  11053. const auto slot = lynx::effectFromFl ("EQUO", plain, 140.0);
  11054. CHECK (slot && slot->kind == lynx::EffectKind::equo && slot->state == plain);
  11055. if (slot)
  11056. {
  11057. auto back = *lynx::equofx::parse (plain);
  11058. lynx::equofx::applyParams (slot->params, back);
  11059. CHECK (back.knob == lynx::equofx::parse (plain)->knob && back.bandwidth == 150 && back.oversample == 0);
  11060. auto fx = lynx::makeEffect (*slot);
  11061. fx->prepare (44100.0);
  11062. std::vector<float> l (inL), r (inR);
  11063. fx->process (l.data(), r.data(), 700, slot->params, 0.0);
  11064. fx->process (l.data() + 700, r.data() + 700, 3396, slot->params, 0.0);
  11065. // the engine glides its bands per processing call, as FL does per host buffer: compare with the engine run on
  11066. // the same 700 + 3396 split (sample 1000 differs from the single-call golden), and with the golden once settled
  11067. lynx::equofx::Engine ref;
  11068. ref.prepare (44100.0, false);
  11069. ref.set (*lynx::equofx::parse (plain));
  11070. std::vector<float> rl (inL), rr (inR);
  11071. ref.process (rl.data(), rr.data(), 700);
  11072. ref.process (rl.data() + 700, rr.data() + 700, 3396);
  11073. CHECK (l == rl && rr == r && r[3000] == -0.0602950938f);
  11074. CHECK (lynx::formatEffectParam (lynx::EffectKind::equo, 4, slot->params[4], 120.0, slot->params).find ("dB") != std::string::npos);
  11075. }
  11076. CHECK (! lynx::equofx::parse (std::vector<std::uint8_t> (100, 0)).has_value());
  11077. }
  11078. // Multiband Delay test state (version 7, our own LCG cells, no FL data; REA_rodada71 mbd/py/teststate.py builds the same bytes)
  11079. static std::vector<std::uint8_t> multibandDelayTestState (std::array<int, 12> globals, std::uint32_t seed)
  11080. {
  11081. std::vector<std::uint8_t> b;
  11082. auto putI = [&b] (std::int32_t v) { const auto* q = (const std::uint8_t*) &v; b.insert (b.end(), q, q + 4); };
  11083. putI (7);
  11084. putI (396);
  11085. std::uint64_t s = seed;
  11086. for (int i = 0; i < 384; ++i)
  11087. {
  11088. s = (s * 1103515245u + 12345u) & 0x7fffffffu;
  11089. putI ((std::int32_t) ((s % 100001u) * (1u << 30) / 100000u));
  11090. }
  11091. for (int v : globals)
  11092. putI (v);
  11093. putI (0);
  11094. putI (3);
  11095. for (int v : { 1, 1, 0 })
  11096. b.push_back ((std::uint8_t) v);
  11097. b.insert (b.end(), 192, 0);
  11098. return b;
  11099. }
  11100. TEST (multiband_delay_exact)
  11101. {
  11102. std::vector<float> inL (8192), inR (8192);
  11103. for (int n = 0; n < 8192; ++n)
  11104. {
  11105. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0 * 0.3);
  11106. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0 * 0.3);
  11107. }
  11108. struct Gold { int i; float l, r; };
  11109. // goldens: Multiband Delay_x64.dll (+ its engine_x64.dll) on the same states and signal, 512-frame blocks (REA_rodada71)
  11110. auto run = [&] (const std::vector<std::uint8_t>& st, int latency, float tol, std::initializer_list<Gold> gold) {
  11111. CHECK (st.size() == 1795 && lynx::mbdelay::parse (st).has_value());
  11112. const auto slot = lynx::effectFromFl ("Multiband Delay", st, 120.0);
  11113. CHECK (slot && slot->kind == lynx::EffectKind::multibandDelay && slot->state == st);
  11114. if (! slot)
  11115. return;
  11116. auto fx = lynx::makeEffect (*slot);
  11117. fx->prepare (44100.0);
  11118. std::vector<float> l (inL), r (inR);
  11119. for (int i = 0, k = 0; i < 8192; ++k)
  11120. {
  11121. const int n = std::min (8192 - i, k % 2 == 0 ? 700 : 333);
  11122. fx->process (l.data() + i, r.data() + i, n, slot->params, 120.0);
  11123. i += n;
  11124. }
  11125. CHECK (fx->latencySamples() == latency);
  11126. for (const auto& g : gold)
  11127. CHECK (std::fabs (l[(std::size_t) g.i] - g.l) <= tol && std::fabs (r[(std::size_t) g.i] - g.r) <= tol);
  11128. };
  11129. // steep crossover, keep pitch, weighted feedback, compressor
  11130. run (multibandDelayTestState ({ 2000, 0, 3000, 600, 1, 1, 3500, 64, 0, 1, 30000, 0 }, 777), 0, 0.0f,
  11131. { { 1000, -0.125012308f, 0.0675066486f }, { 3000, -0.125002861f, -0.0525011979f }, { 5000, -0.110439189f, 0.0597563162f },
  11132. { 7000, -0.146480903f, 0.0139919929f }, { 8191, -0.0454496294f, 0.0398242511f } });
  11133. // gentle crossover, harmonic scale, delay ×0.1, saturation
  11134. run (multibandDelayTestState ({ -2500, 1, 4500, 0, 0, 0, 0, 128, 1, 0, 1000, 1 }, 4242), 0, 0.0f,
  11135. { { 1000, 0.0679653957f, 0.0137896547f }, { 3000, 0.0620588325f, 0.0558352768f }, { 5000, 0.0558495969f, -0.0220566839f },
  11136. { 7000, 0.057009533f, 0.00332171144f }, { 8191, 0.0673644915f, 0.00163267844f } });
  11137. // linear-phase crossover (FL's IPP FFTs are approximated: about −120 dB), clipper
  11138. run (multibandDelayTestState ({ 1500, 0, 2000, 1500, 2, 0, 1000, 100, 0, 0, 5000, 2 }, 99), 1536, 2e-6f,
  11139. { { 1000, 0.0f, 0.0f }, { 3000, 0.103910387f, -0.0521105044f }, { 5000, 0.0516818762f, -0.0134486742f },
  11140. { 7000, 0.065563485f, -0.00765838847f }, { 8191, -0.0376662463f, -0.0434602089f } });
  11141. const auto st = multibandDelayTestState ({ 2000, 0, 3000, 600, 1, 1, 3500, 64, 0, 1, 30000, 0 }, 777);
  11142. const auto s = *lynx::mbdelay::parse (st);
  11143. CHECK (lynx::mbdelay::parse (lynx::mbdelay::serialize (s))->raw == s.raw);
  11144. lynx::EffectParams p {};
  11145. lynx::mbdelay::toParams (s, p);
  11146. auto back = s;
  11147. lynx::mbdelay::applyParams (p, back);
  11148. CHECK (back.raw == s.raw);
  11149. CHECK (lynx::mbdelay::engineValue (s, 384 + 7) == (float) (192.0 / 256.0));
  11150. CHECK (lynx::formatEffectParam (lynx::EffectKind::multibandDelay, 4, p[4], 120.0) == "steep");
  11151. CHECK (lynx::formatEffectParam (lynx::EffectKind::multibandDelay, 7, 1.0f, 120.0) == "100% wet");
  11152. CHECK (! lynx::mbdelay::parse (std::vector<std::uint8_t> (16, 0)).has_value());
  11153. }
  11154. TEST (fruity_parametric_eq_exact)
  11155. {
  11156. std::vector<float> inL (4096), inR (4096);
  11157. for (int n = 0; n < 4096; ++n)
  11158. {
  11159. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  11160. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  11161. }
  11162. struct Gold { int i; float l, r; };
  11163. // high pass Steep 8, low shelf, steep peaking, a 0 dB peaking (0.2 s countdown), notch Gentle 4, high shelf Gentle 8,
  11164. // low pass Gentle 6
  11165. const PeqTestBand bands[7] = { { 3, 8000, 30000, 0, -3 }, { 5, 15000, 40124, 600, 0 }, { 6, 30000, 20000, -900, -2 },
  11166. { 6, 35000, 25412, 0, 0 }, { 4, 40000, 10000, 0, 1 }, { 7, 50000, 30000, 450, 3 },
  11167. { 1, 60000, 45000, 0, 2 } };
  11168. const PeqTestBand bands1[7] = { { 3, 6000, 20000, 0, 0 }, { 5, 15000, 30000, 600, 0 }, { 6, 30000, 20000, -900, 0 },
  11169. { 6, 35000, 25000, 0, 0 }, { 4, 40000, 10000, 0, 0 }, { 7, 50000, 30000, 450, 0 },
  11170. { 1, 60000, 45000, 0, 0 } };
  11171. auto run = [&] (const std::optional<lynx::peqfl::Settings>& s, std::initializer_list<Gold> gold) {
  11172. CHECK (s.has_value());
  11173. if (! s)
  11174. return;
  11175. lynx::peqfl::Engine e;
  11176. e.set (*s);
  11177. e.prepare (44100.0);
  11178. std::vector<float> l (inL), r (inR);
  11179. for (int i = 0; i < 4096; i += 512)
  11180. e.process (l.data() + i, r.data() + i, 512);
  11181. for (const auto& g : gold)
  11182. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  11183. };
  11184. const auto plain = peq2TestState (bands, 300, 0, 0x60, 0x7F);
  11185. const auto hq = peq2TestState (bands, 300, 1, 0x60, 0x7F);
  11186. const auto ext = peq2TestState (bands, 300, 1, 0x62, 0x7F);
  11187. run (lynx::peqfl::parsePeq2 (plain), { { 100, 0.1340574324131012f, -0.3040016293525696f }, { 1000, 0.28402701020240784f, -0.23813000321388245f },
  11188. { 2047, -0.9301530122756958f, 0.13881002366542816f }, { 3000, 0.2846762537956238f, 0.48893973231315613f },
  11189. { 4095, -0.8481860756874084f, -0.2641225755214691f } });
  11190. run (lynx::peqfl::parsePeq2 (hq), { { 100, 0.19229719042778015f, 0.29938483238220215f }, { 1000, 0.12543821334838867f, -0.055617764592170715f },
  11191. { 2047, 1.0342068672180176f, 0.10901166498661041f }, { 3000, 0.12598788738250732f, -0.12920552492141724f },
  11192. { 4095, 0.1619236022233963f, -0.23385579884052277f } });
  11193. run (lynx::peqfl::parsePeq2 (ext), { { 100, 1.2178642749786377f, -0.21160493791103363f }, { 1000, -0.8292768001556396f, -0.23916664719581604f },
  11194. { 2047, -0.7030635476112366f, -0.1978478729724884f }, { 3000, -0.8299900889396667f, 0.9836107492446899f },
  11195. { 4095, 0.06754066050052643f, -0.34364908933639526f } });
  11196. run (lynx::peqfl::parsePeq2 (peq2TestState (bands, -250, 1, 0x60, 0x5B)), // bands 3 and 6 muted
  11197. { { 100, -0.02959616854786873f, 0.227821946144104f }, { 1000, 0.053224340081214905f, 0.06922465562820435f },
  11198. { 2047, 0.3365536034107208f, -0.05899651721119881f }, { 3000, 0.053670305758714676f, -0.07452142238616943f },
  11199. { 4095, -0.04406970739364624f, -0.12635089457035065f } });
  11200. run (lynx::peqfl::parsePeq1 (peq1TestState (bands1, 150)),
  11201. { { 100, -0.6533108353614807f, -1.6903921365737915f }, { 1000, -4.313726902008057f, 5.973916053771973f },
  11202. { 2047, 1.0222549438476562f, 4.427900314331055f }, { 3000, -4.310323238372803f, 7.825916767120361f },
  11203. { 4095, 8.344571113586426f, -0.49086111783981323f } });
  11204. // "alternative design" (Frequency Splitter's matched biquads), bit-exact
  11205. run (lynx::peqfl::parsePeq2 (peq2TestState (bands, 300, 3, 0x60, 0x7F)),
  11206. { { 100, -0.790181577205658f, -0.08691816031932831f }, { 1000, 0.4960691034793854f, -0.24172282218933105f },
  11207. { 2047, -0.42601829767227173f, 0.027932533994317055f }, { 3000, 0.49674150347709656f, 1.0799059867858887f },
  11208. { 4095, -0.35040369629859924f, -0.28149813413619995f } });
  11209. // linear phase (plain and HQ response): FFT convolution, 1536-sample latency, 0.1 s fade-in; FL's IPP FFT rounding
  11210. // is not replicated, so within 1e-6 of the DLL
  11211. for (const bool hq : { false, true })
  11212. {
  11213. const auto s = lynx::peqfl::parsePeq2 (peq2TestState (bands, hq ? -200 : 300, hq ? 1 : 0, 0x61, 0x7F));
  11214. CHECK (s && s->linearPhase);
  11215. if (! s)
  11216. continue;
  11217. std::vector<float> l (12288), r (12288);
  11218. for (int n = 0; n < 12288; ++n)
  11219. {
  11220. l[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  11221. r[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  11222. }
  11223. lynx::peqfl::Engine e;
  11224. e.set (*s);
  11225. e.prepare (44100.0);
  11226. CHECK (e.latencySamples() == 1536);
  11227. for (int i = 0; i < 12288; i += 512)
  11228. e.process (l.data() + i, r.data() + i, 512);
  11229. // (named arrays: a std::initializer_list chosen by ?: would point into a temporary array that dies at the end of
  11230. // the declaration, which GCC 13 reuses for other locals)
  11231. static const Gold goldHq[] = {
  11232. { 2000, 0.08618364483118057f, 0.11248858273029327f }, { 5000, 0.18998730182647705f, 0.428600937128067f },
  11233. { 8191, -0.1412065178155899f, -0.002996620489284396f }, { 10000, 0.18998737633228302f, -0.012570115737617016f },
  11234. { 12287, 0.40586185455322266f, -0.014698396436870098f } };
  11235. static const Gold goldPlain[] = {
  11236. { 2000, 0.18538671731948853f, 0.206522598862648f }, { 5000, 0.4086928963661194f, 0.8167467713356018f },
  11237. { 8191, -0.3218550980091095f, -0.06812914460897446f }, { 10000, 0.40869277715682983f, -0.08537150919437408f },
  11238. { 12287, 0.775394082069397f, -0.08931504935026169f } };
  11239. for (const auto& g : hq ? goldHq : goldPlain)
  11240. 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);
  11241. }
  11242. // the mixer effect from the FL import: FL's values in the slot's parameters (exact round trip), any host block size
  11243. for (const auto* name : { "Fruity Parametric EQ 2", "Fruity Parametric EQ" })
  11244. {
  11245. const bool v1 = std::string (name) == "Fruity Parametric EQ";
  11246. const auto st = v1 ? peq1TestState (bands1, 150) : ext;
  11247. const auto slot = lynx::effectFromFl (name, st, 120.0);
  11248. CHECK (slot && slot->kind == lynx::EffectKind::parametricEq && lynx::peq::flLaw (slot->params));
  11249. if (! slot)
  11250. continue;
  11251. const auto back = lynx::peqfl::fromParams (slot->params);
  11252. const auto orig = v1 ? lynx::peqfl::parsePeq1 (st) : lynx::peqfl::parsePeq2 (st);
  11253. CHECK (back.freq == orig->freq && back.gain == orig->gain && back.width == orig->width && back.type == orig->type
  11254. && back.order == orig->order && back.main == orig->main && back.processing == orig->processing && back.mask == orig->mask);
  11255. auto fx = lynx::makeEffect (*slot);
  11256. fx->prepare (44100.0);
  11257. CHECK (fx->latencySamples() == (v1 ? 0 : 44));
  11258. std::vector<float> l (inL), r (inR);
  11259. fx->process (l.data(), r.data(), 1000, slot->params, 120.0);
  11260. fx->process (l.data() + 1000, r.data() + 1000, 3096, slot->params, 120.0);
  11261. if (v1)
  11262. CHECK (l[3000] == -4.310323238372803f && r[4095] == -0.49086111783981323f);
  11263. else
  11264. CHECK (l[1000] == -0.8292768001556396f && r[3000] == 0.9836107492446899f && l[4095] == 0.06754066050052643f);
  11265. // editor texts and graph
  11266. CHECK (lynx::formatEffectParam (lynx::EffectKind::parametricEq, lynx::peq::slopeParam, slot->params[lynx::peq::slopeParam], 120.0, slot->params)
  11267. == (v1 ? "12 dB/oct" : "Steep 8"));
  11268. CHECK (lynx::peq::widthText (slot->params, 0).rfind ("BW ", 0) == 0);
  11269. const auto lowCutDb = lynx::peq::responseDb (slot->params, 44100.0, 25.0), midDb = lynx::peq::responseDb (slot->params, 44100.0, 1000.0);
  11270. CHECK (lowCutDb < midDb - 5.0); // high pass below ~100 Hz (Steep 8 in the EQ 2 state, 12 dB/oct in v1)
  11271. }
  11272. // a native LYNX EQ is untouched by the FL law
  11273. CHECK (! lynx::peq::flLaw (lynx::makeEffectSlot (lynx::EffectKind::parametricEq).params));
  11274. }
  11275. TEST (fruity_panomatic_balance_blood_overdrive)
  11276. {
  11277. // REA_rodada58: goldens from the installation's DLLs (EXEC) on our own states, 44.1 kHz, blocks of 512
  11278. const lynx::ScopedFlushDenormals ftz;
  11279. std::vector<float> inL (4096), inR (4096);
  11280. for (int n = 0; n < 4096; ++n)
  11281. {
  11282. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  11283. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  11284. }
  11285. struct Gold { int i; float l, r; };
  11286. auto close = [] (float a, float b) { return std::abs (a - b) <= x87Tolerance() * std::max (1.0f, std::abs (b)); };
  11287. auto check = [&] (const std::vector<float>& l, const std::vector<float>& r, std::initializer_list<Gold> gold) {
  11288. for (const auto& g : gold)
  11289. CHECK (close (l[(std::size_t) g.i], g.l) && close (r[(std::size_t) g.i], g.r));
  11290. };
  11291. auto st32 = [] (std::initializer_list<int> v) {
  11292. std::vector<std::uint8_t> b;
  11293. for (int x : v)
  11294. for (int k = 0; k < 4; ++k)
  11295. b.push_back ((std::uint8_t) ((unsigned) x >> (8 * k)));
  11296. return b;
  11297. };
  11298. // PanOMatic: the engine started as LYNX starts it (one silent frame, then FL's flush), and from silence (fade-in)
  11299. auto pom = [&] (lynx::flbasic::PanOMaticState s, bool prime, std::initializer_list<Gold> gold) {
  11300. lynx::flbasic::PanOMaticEngine e;
  11301. e.prepare (44100);
  11302. e.set (s);
  11303. if (prime)
  11304. e.prime();
  11305. std::vector<float> l (inL), r (inR);
  11306. for (int i = 0; i < 4096; i += 512)
  11307. e.process (l.data() + i, r.data() + i, 512);
  11308. check (l, r, gold);
  11309. };
  11310. pom ({ 40, 300, 2, 1, 128, 50000 }, true, { { 100, 0.0f, -1.44884729f }, { 1000, -1.78870034f, 0.778897762f }, { 2047, 0.697593153f, -1.26396084f },
  11311. { 3000, -1.78870034f, -1.32851648f }, { 4095, 0.268305063f, 0.851258814f } });
  11312. pom ({ 200, 180, 1, 2, 100, 30000 }, true, { { 100, 0.0f, -0.347919852f }, { 1000, -0.322590947f, 0.0788071603f }, { 2047, 0.126614109f, -0.26421836f },
  11313. { 3000, -0.326532423f, -0.435213327f }, { 4095, 0.0493027233f, 0.226084977f } });
  11314. pom ({ 90, 256, 0, 1, 77, 40000 }, false, { { 100, -0.0292895138f, -0.110000029f }, { 1000, -1.31911683f, 0.666457176f }, { 2047, 0.477211952f, -0.864247262f },
  11315. { 3000, -1.59167564f, -0.591367304f }, { 4095, 0.268362761f, 0.537029684f } });
  11316. // Balance: flushed and from silence
  11317. auto bal = [&] (lynx::flbasic::BalanceState s, bool flush, std::initializer_list<Gold> gold) {
  11318. lynx::flbasic::BalanceEngine e;
  11319. e.prepare (44100);
  11320. e.set (s);
  11321. if (flush)
  11322. e.flush();
  11323. std::vector<float> l (inL), r (inR);
  11324. for (int i = 0; i < 4096; i += 512)
  11325. e.process (l.data() + i, r.data() + i, 512);
  11326. check (l, r, gold);
  11327. };
  11328. bal ({ 77, 100 }, true, { { 100, 0.0f, -0.136579692f }, { 1000, -0.0669578835f, -0.0282340273f }, { 2047, 0.0261135735f, -0.0795038193f },
  11329. { 3000, -0.0669578835f, -0.226895392f }, { 4095, 0.0100436825f, 0.0954951793f } });
  11330. bal ({ -128, 320 }, false, { { 100, -0.28914085f, 0.0f }, { 1000, -1.03179514f, 0.0f }, { 2047, -0.578702569f, 0.0f },
  11331. { 3000, -3.18510628f, 0.0f }, { 4095, 1.14394665f, 0.0f } });
  11332. // Blood Overdrive: rate first, then the state (its smoothers settle for one second of samples)
  11333. auto od = [&] (std::array<int, 6> raw, std::initializer_list<Gold> gold) {
  11334. lynx::flbasic::BloodOverdriveEngine e;
  11335. e.start (44100, { raw });
  11336. std::vector<float> l (inL), r (inR);
  11337. for (int i = 0; i < 4096; i += 512)
  11338. e.process (l.data() + i, r.data() + i, 512);
  11339. check (l, r, gold);
  11340. };
  11341. od ({ 3000, 7000, 5000, 1, 9000, 5000 }, { { 100, -0.0442250744f, 0.170355186f }, { 1000, 0.0470311977f, 0.0765217245f },
  11342. { 2047, 0.0261161141f, -0.0341798998f }, { 3000, 0.0470311977f, 0.0213619489f },
  11343. { 4095, 0.0402069278f, 0.0499244779f } });
  11344. // the mixer effects from the FL import: controls as parameters; host blocks of any size
  11345. auto viaImport = [&] (const char* plugin, const std::vector<std::uint8_t>& st, lynx::EffectKind kind, std::initializer_list<Gold> gold) {
  11346. const auto slot = lynx::effectFromFl (plugin, st, 120.0);
  11347. CHECK (slot && slot->kind == kind);
  11348. if (! slot)
  11349. return;
  11350. auto fx = lynx::makeEffect (*slot);
  11351. fx->prepare (44100.0);
  11352. std::vector<float> l (inL), r (inR);
  11353. fx->process (l.data(), r.data(), 1024, slot->params, 120.0);
  11354. fx->process (l.data() + 1024, r.data() + 1024, 1024, slot->params, 120.0);
  11355. fx->process (l.data() + 2048, r.data() + 2048, 2048, slot->params, 120.0);
  11356. check (l, r, gold);
  11357. };
  11358. viaImport ("Fruity PanOMatic", st32 ({ 40, 300, 2, 1, 128, 50000 }), lynx::EffectKind::fruityPanOMatic,
  11359. { { 1000, -1.78870034f, 0.778897762f }, { 4095, 0.268305063f, 0.851258814f } });
  11360. viaImport ("Fruity Balance", st32 ({ 0, 256 }), lynx::EffectKind::fruityBalance,
  11361. { { 100, 0.0f, -1.10000002f }, { 1000, -1.66666663f, 0.899999976f }, { 4095, 0.25f, 0.600000024f } });
  11362. viaImport ("Fruity Blood Overdrive", st32 ({ 3, 0, 5000, 2578, 1, 0, 2911, 0, 0 }), lynx::EffectKind::fruityBloodOverdrive,
  11363. { { 100, -0.122698173f, 0.0206716377f }, { 2047, 0.0128381588f, -0.0274834726f }, { 4095, -0.0899387598f, -0.121109024f } });
  11364. {
  11365. const auto slot = lynx::effectFromFl ("Fruity PanOMatic", st32 ({ 119, 256, 1, 2, 64, 27500 }), 120.0);
  11366. CHECK (slot && lynx::flbasic::panOMaticFromParams (slot->params.data()).pan == 119
  11367. && lynx::flbasic::panOMaticFromParams (slot->params.data()).target == 2);
  11368. if (slot)
  11369. {
  11370. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityPanOMatic, 2, slot->params[2], 120.0) == "Triangle");
  11371. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityPanOMatic, 1, slot->params[1], 120.0) == "+0.0 dB");
  11372. }
  11373. }
  11374. // unreadable states keep the old approximations
  11375. CHECK (lynx::effectFromFl ("Fruity PanOMatic", { 1, 2, 3 }, 120.0)->kind == lynx::EffectKind::utility);
  11376. CHECK (lynx::effectFromFl ("Fruity Balance", { 1, 2, 3 }, 120.0)->kind == lynx::EffectKind::utility);
  11377. CHECK (lynx::effectFromFl ("Fruity Blood Overdrive", { 1, 2, 3 }, 120.0)->kind == lynx::EffectKind::distortion);
  11378. }
  11379. TEST (fruity_fast_lp_free_filter_filter)
  11380. {
  11381. // REA_rodada63: goldens from the installation's DLLs (EXEC) on our own states, 44.1 kHz, blocks of 512 (the wrapper
  11382. // splits the host blocks below into calls of 512, as the DLL ran)
  11383. const lynx::ScopedFlushDenormals ftz;
  11384. std::vector<float> inL (4096), inR (4096);
  11385. for (int n = 0; n < 4096; ++n)
  11386. {
  11387. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  11388. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  11389. }
  11390. struct Gold { int i; float l, r; };
  11391. auto state = [] (const char* hex) {
  11392. std::vector<std::uint8_t> b;
  11393. for (const char* p = hex; p[0] && p[1]; p += 2)
  11394. b.push_back ((std::uint8_t) std::strtol (std::string (p, 2).c_str(), nullptr, 16));
  11395. return b;
  11396. };
  11397. auto viaImport = [&] (const char* plugin, const char* hex, lynx::EffectKind kind, std::initializer_list<Gold> gold) {
  11398. const auto slot = lynx::effectFromFl (plugin, state (hex), 120.0);
  11399. CHECK (slot && slot->kind == kind);
  11400. if (! slot)
  11401. return;
  11402. auto fx = lynx::makeEffect (*slot);
  11403. fx->prepare (44100.0);
  11404. std::vector<float> l (inL), r (inR);
  11405. fx->process (l.data(), r.data(), 1024, slot->params, 120.0);
  11406. fx->process (l.data() + 1024, r.data() + 1024, 1024, slot->params, 120.0);
  11407. fx->process (l.data() + 2048, r.data() + 2048, 2048, slot->params, 120.0);
  11408. for (const auto& g : gold)
  11409. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  11410. };
  11411. using K = lynx::EffectKind;
  11412. viaImport ("Fruity Fast LP", "01000000e0010000bc020000", K::fruityFastLP,
  11413. { { 100, -2.876206159591675f, 1.5413906574249268f }, { 1000, 0.12972253561019897f, 0.02936629205942154f },
  11414. { 2047, -0.2262123078107834f, -0.15169914066791534f }, { 3000, 0.12972258031368256f, -0.0798468142747879f },
  11415. { 4095, 0.0011161785805597901f, -0.08101501315832138f } });
  11416. viaImport ("Fruity Fast LP", "01000000890000008a000000", K::fruityFastLP,
  11417. { { 100, -0.7748895883560181f, -0.2310182899236679f }, { 1000, -0.01256702933460474f, -0.0086913350969553f },
  11418. { 2047, -0.010225297883152962f, -0.014700239524245262f }, { 3000, -0.012712989933788776f, -0.008916046470403671f },
  11419. { 4095, -0.008918046951293945f, -0.020280027762055397f } });
  11420. viaImport ("Fruity Free Filter", "02000000c2010000200300002c0100008403000001", K::fruityFreeFilter, // low shelf, CENTER
  11421. { { 100, -0.6178444623947144f, -0.13762974739074707f }, { 1000, -1.6557703018188477f, 0.8969771862030029f },
  11422. { 2047, 0.6539966464042664f, -0.21659433841705322f }, { 3000, -1.6404345035552979f, 0.4286947548389435f },
  11423. { 4095, -1.2272151708602905f, -0.5219485759735107f } });
  11424. viaImport ("Fruity Free Filter", "020000008a02000058020000bc020000c800000000", K::fruityFreeFilter, // high shelf
  11425. { { 100, -0.06996861100196838f, -0.11112016439437866f }, { 1000, -0.47636136412620544f, 0.31258222460746765f },
  11426. { 2047, 0.1628853678703308f, -0.3169582486152649f }, { 3000, -0.47636136412620544f, -0.22028093039989471f },
  11427. { 4095, -0.0975298061966896f, 0.05710025131702423f } });
  11428. viaImport ("Fruity Free Filter", "020000006400000000040000a9000000a402000001", K::fruityFreeFilter, // low pass 16 kHz
  11429. { { 100, 1.3032524585723877f, -0.007953938096761703f }, { 1000, 0.18662987649440765f, 2.155099391937256f },
  11430. { 2047, 0.5824014544487f, 0.05704556778073311f }, { 3000, 0.18981599807739258f, -0.6362011432647705f },
  11431. { 4095, 1.2624475955963135f, 2.497816324234009f } });
  11432. viaImport ("Fruity Filter", "020000004b01000076000000000400002c010000580200000100000001", K::fruityFilter, // x2, CENTER
  11433. { { 100, 0.2685631811618805f, 0.16885951161384583f }, { 1000, -0.20637311041355133f, 0.13557639718055725f },
  11434. { 2047, 0.07038643211126328f, -0.15532609820365906f }, { 3000, -0.20311756432056427f, -0.09958365559577942f },
  11435. { 4095, 0.08275680989027023f, 0.010792487300932407f } });
  11436. viaImport ("Fruity Filter", "01000000bc020000f401000000000000000400000000000000000000", K::fruityFilter, // band pass
  11437. { { 100, -0.4368843734264374f, -0.12492841482162476f }, { 1000, -0.3362128436565399f, -0.09686881303787231f },
  11438. { 2047, 0.21920710802078247f, 0.3213832974433899f }, { 3000, -0.3362128436565399f, 0.4016203284263611f },
  11439. { 4095, -0.14230486750602722f, -0.22842440009117126f } });
  11440. // an old Free Filter layout (type·10) is converted to the current scale; parameter texts
  11441. {
  11442. const auto slot = lynx::effectFromFl ("Fruity Free Filter", state ("010000003c00000000020000a9000000a4020000"), 120.0);
  11443. CHECK (slot && slot->kind == K::fruityFreeFilter);
  11444. if (slot)
  11445. {
  11446. CHECK (lynx::flfilter::freeFilterFromParams (slot->params.data()).type == 100 + (int) std::nearbyint (5.5 / 7.0 * 600.0));
  11447. CHECK (lynx::formatEffectParam (K::fruityFreeFilter, 0, slot->params[0], 120.0) == "Peaking");
  11448. }
  11449. CHECK (lynx::formatEffectParam (K::fruityFreeFilter, 1, 1.0f, 120.0) == "16000 Hz");
  11450. CHECK (lynx::formatEffectParam (K::fruityFilter, 0, 1.0f, 120.0) == "7350 Hz");
  11451. }
  11452. // unreadable states keep the old approximations
  11453. CHECK (lynx::effectFromFl ("Fruity Fast LP", { 1, 2, 3 }, 120.0)->kind == K::filter);
  11454. CHECK (lynx::effectFromFl ("Fruity Free Filter", { 1, 2, 3 }, 120.0)->kind == K::filter);
  11455. CHECK (lynx::effectFromFl ("Fruity Filter", { 9, 0, 0, 0 }, 120.0)->kind == K::filter);
  11456. }
  11457. // ---- Sytrus (REA_rodada31) -------------------------------------------------------------------------------------------
  11458. // A Sytrus state built from scratch (version 25 layout, our own values — no FL preset data), zlib "stored" blocks.
  11459. namespace
  11460. {
  11461. struct SytrusTestPatch
  11462. {
  11463. std::vector<std::int32_t> main = std::vector<std::int32_t> (240, 0); // 962-byte main block as ints (+ mute bytes)
  11464. struct Op
  11465. {
  11466. int p[10] { 0, 256, 0, 0, 0, 0, 0, 0, 0, 0 };
  11467. int ratio = 20000, offset = 0, noise = 0;
  11468. std::vector<float> amps { 1.0f };
  11469. std::uint8_t flags[8] {};
  11470. int env1[3] { -1, -1, -1 }; // volume envelope: on (≠ −1 → on), decay point, sustain point
  11471. std::vector<std::pair<double, double>> env1pts;
  11472. } ops[6];
  11473. struct Filt
  11474. {
  11475. int p[10] { 0, 0, 128, 0, 128, 0, 0, 128, 128, 0 };
  11476. int type = 3;
  11477. } filts[3];
  11478. std::array<std::array<std::uint8_t, 23>, 3> delays {};
  11479. std::array<std::int32_t, 9> reverb { 19, 221, 0, 50, 80, 30, 221, 2, 0 };
  11480. bool reverbOn = false;
  11481. int eqmode = 0;
  11482. SytrusTestPatch()
  11483. {
  11484. main[0] = 128, main[1] = 128;
  11485. for (int i = 0; i < 8; ++i)
  11486. main[(std::size_t) (165 + i)] = (i % 4 == 2) ? 0 : 128;
  11487. main[173] = 1, main[175] = 128, main[188] = 128, main[189] = -1, main[190] = main[191] = 256;
  11488. }
  11489. void cell (int layer, int row, int col, int v) { main[(std::size_t) (3 + layer * 81 + row * 9 + col)] = v; }
  11490. std::vector<std::uint8_t> blob() const
  11491. {
  11492. std::vector<std::uint8_t> b;
  11493. 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))); };
  11494. auto f32 = [&] (float v) { std::int32_t x; std::memcpy (&x, &v, 4); i32 (x); };
  11495. 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))); };
  11496. // point lists: kind 1 envelope, 2 LFO, 3 map / shaper curve; format version 3
  11497. auto env = [&] (int kind, const std::vector<std::pair<double, double>>& pts, int on, int decay, int sus) {
  11498. i32 (kind), i32 (3), i32 ((std::int32_t) pts.size());
  11499. for (const auto& [dt, y] : pts)
  11500. f64 (dt), f64 (y), f32 (0.0f), i32 (0);
  11501. i32 (0), i32 (on), i32 (decay), i32 (-1), i32 (sus);
  11502. };
  11503. for (int i = 0; i < 240; ++i)
  11504. if (4 * i + 4 <= 0x320)
  11505. i32 (main[(std::size_t) i]);
  11506. b.resize (962, 0); // mute bytes: none muted
  11507. i32 (0); // global declick amount
  11508. b.push_back (0), b.push_back (0), b.push_back (0); // OSC grouping, DC stage, GIBBS OFF
  11509. for (int k = 0; k < 9; ++k)
  11510. b.push_back (k == 7 ? 1 : 0); // flag group (release multiplier on, as Default.fst)
  11511. for (const auto& d : delays)
  11512. b.insert (b.end(), d.begin(), d.end());
  11513. for (auto v : reverb)
  11514. i32 (v);
  11515. b.push_back (reverbOn ? 1 : 0), b.push_back (0);
  11516. i32 (eqmode);
  11517. for (int k = 0; k < 3; ++k)
  11518. i32 (0), i32 (30000), i32 (20000), i32 (k);
  11519. b.push_back (0); // no info text
  11520. for (int u = 0; u < 10; ++u)
  11521. {
  11522. const int narts = u < 6 ? 6 : (u < 9 ? 8 : 2);
  11523. for (int j = 0; j < 10; ++j)
  11524. i32 (u < 6 ? ops[u].p[j] : (u < 9 ? filts[u - 6].p[j] : 0));
  11525. if (u < 6)
  11526. {
  11527. const auto& o = ops[u];
  11528. const auto at = b.size();
  11529. b.resize (at + 0x828, 0);
  11530. auto put = [&] (std::size_t off, const void* src, std::size_t n) { std::memcpy (b.data() + at + off, src, n); };
  11531. put (0, &o.ratio, 4), put (4, &o.offset, 4), put (8, &o.noise, 4);
  11532. put (0xc, o.amps.data(), 4 * o.amps.size());
  11533. const std::int32_t shape[5] { 0, 0, 0, 0, 128 };
  11534. put (0x80c, shape, sizeof shape);
  11535. put (0x820, o.flags, 8);
  11536. }
  11537. else if (u < 9)
  11538. {
  11539. const auto at = b.size();
  11540. b.resize (at + 0x30, 0);
  11541. std::memcpy (b.data() + at + 0x2c, &filts[u - 6].type, 4);
  11542. env (3, { { 0.0, 0.0 }, { 1.0, 1.0 } }, 0, -1, -1);
  11543. b.push_back (0);
  11544. }
  11545. for (int k = 0; k < narts; ++k)
  11546. {
  11547. const bool add = k == 0 || k == 3 || (u >= 6 && u <= 8 && (k == 2 || k == 3)) || (u < 6 && k == 4);
  11548. const double neutral = add ? 0.5 : 1.0;
  11549. i32 (3), i32 (0), f32 (1.0f), f32 (1.0f);
  11550. if (u < 6 && k == 1 && ops[u].env1[0] != -1)
  11551. env (1, ops[u].env1pts, -1, ops[u].env1[1], ops[u].env1[2]);
  11552. else
  11553. env (1, { { 0.0, 0.0 }, { 1.0, 1.0 } }, 0, -1, -1);
  11554. i32 (128), i32 (128), i32 (0), i32 (128);
  11555. env (2, { { 0.0, 1.0 }, { 1.0, 1.0 } }, 0, -1, -1);
  11556. i32 (40000), i32 (0), i32 (0), i32 (0), i32 (0);
  11557. for (int m = 0; m < 6; ++m)
  11558. env (3, { { 0.0, neutral }, { 1.0, neutral } }, 0, -1, -1);
  11559. }
  11560. }
  11561. b.push_back (0), b.push_back (0);
  11562. // zlib with stored blocks
  11563. std::vector<std::uint8_t> z { 0x78, 0x01 };
  11564. for (std::size_t at = 0; at < b.size(); at += 65535)
  11565. {
  11566. const auto n = std::min<std::size_t> (65535, b.size() - at);
  11567. z.push_back (at + n >= b.size() ? 1 : 0);
  11568. z.push_back ((std::uint8_t) n), z.push_back ((std::uint8_t) (n >> 8));
  11569. z.push_back ((std::uint8_t) ~n), z.push_back ((std::uint8_t) (~n >> 8));
  11570. z.insert (z.end(), b.begin() + (std::ptrdiff_t) at, b.begin() + (std::ptrdiff_t) (at + n));
  11571. }
  11572. std::uint32_t a = 1, c = 0;
  11573. for (auto x : b)
  11574. a = (a + x) % 65521, c = (c + a) % 65521;
  11575. for (int k = 3; k >= 0; --k)
  11576. z.push_back ((std::uint8_t) (((c << 16) | a) >> (8 * k)));
  11577. std::vector<std::uint8_t> out { 25, 0, 0, 0 };
  11578. out.insert (out.end(), z.begin(), z.end());
  11579. return out;
  11580. }
  11581. };
  11582. // renders like FL's Gen_Render calls: notes start/release at the block that contains them
  11583. std::pair<std::vector<float>, std::vector<float>> sytrusRender (const std::vector<std::uint8_t>& blob, bool renderMode, int n,
  11584. std::vector<std::array<int, 3>> notes, double fs = 44100.0)
  11585. {
  11586. lynx::sytrus::Engine e;
  11587. e.setTempo (120.0);
  11588. e.prepare (fs, renderMode, 512);
  11589. e.setPatch (lynx::sytrus::parsePatch (blob));
  11590. std::vector<float> l ((std::size_t) n), r ((std::size_t) n);
  11591. for (int n0 = 0; n0 < n; n0 += 512)
  11592. {
  11593. for (std::size_t k = 0; k < notes.size(); ++k)
  11594. {
  11595. if (notes[k][1] >= n0 && notes[k][1] < n0 + 512)
  11596. {
  11597. lynx::sytrus::NoteParams p;
  11598. p.key = (float) notes[k][0];
  11599. p.id = (std::int64_t) k + 1;
  11600. e.noteOn (p);
  11601. }
  11602. if (notes[k][2] >= n0 && notes[k][2] < n0 + 512)
  11603. e.release ((std::int64_t) k + 1);
  11604. }
  11605. e.render (l.data() + n0, r.data() + n0, std::min (512, n - n0), 1.0f, 0.0f);
  11606. }
  11607. return { l, r };
  11608. }
  11609. } // namespace
  11610. TEST (sytrus_engine_sine)
  11611. {
  11612. // one sine operator, ratio 1, OUT 256, no envelopes: C5 = 523.2511 Hz at 0.7071 (centre of the pan law, §6.1)
  11613. SytrusTestPatch t;
  11614. t.cell (0, 0, 8, 256);
  11615. const auto blob = t.blob();
  11616. CHECK (lynx::sytrus::stateUsable (blob));
  11617. CHECK (lynx::sytrus::parsePatch (blob) != nullptr);
  11618. auto broken = blob;
  11619. broken.resize (broken.size() / 2);
  11620. CHECK (! lynx::sytrus::stateUsable (broken));
  11621. const auto [l, r] = sytrusRender (blob, false, 8192, { { 60, 0, 4096 } });
  11622. double worst = 0.0;
  11623. for (int k = 0; k < 4000; ++k)
  11624. 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)));
  11625. CHECK (worst < 2e-5);
  11626. CHECK (l == r);
  11627. // 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
  11628. // 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)
  11629. int last = 0;
  11630. for (int k = 0; k < 8192; ++k)
  11631. if (l[(std::size_t) k] != 0.0f)
  11632. last = k;
  11633. CHECK (last == 4895);
  11634. // key 69 = 880 Hz; 48 kHz
  11635. const auto [l2, r2] = sytrusRender (blob, false, 4096, { { 69, 0, -1 } }, 48000.0);
  11636. worst = 0.0;
  11637. for (int k = 0; k < 4000; ++k)
  11638. 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)));
  11639. CHECK (worst < 5e-5);
  11640. (void) r2;
  11641. }
  11642. TEST (sytrus_engine_reference)
  11643. {
  11644. // FM 2→1, Vanilla LP filter, volume envelope, unison 3, delay and reverb, 2× oversampling with the 8-sample
  11645. // envelope tick (render settings): against our Python reference model of REA_rodada31 (sytmodel.render)
  11646. SytrusTestPatch t;
  11647. t.main[195] = 0, t.main[196] = 1, t.main[193] = 0, t.main[194] = 1;
  11648. t.main[173] = 3, t.main[174] = 64, t.main[176] = 100;
  11649. t.ops[1].ratio = 40000;
  11650. t.cell (0, 0, 1, 128); // op 2 → op 1 FM
  11651. t.cell (0, 0, 8, 128);
  11652. t.cell (0, 6, 0, 256); // op 1 → filter 1
  11653. t.cell (0, 6, 8, 256);
  11654. t.cell (0, 0, 7, 200); // FX send
  11655. t.filts[0].p[0] = 1, t.filts[0].p[2] = 180, t.filts[0].p[3] = 60;
  11656. t.ops[0].env1[0] = 0, t.ops[0].env1[1] = 1, t.ops[0].env1[2] = 2;
  11657. t.ops[0].env1pts = { { 0.0, 0.0 }, { 0.05, 1.0 }, { 0.1, 0.6 }, { 0.2, 0.0 } };
  11658. const std::int32_t d0[5] { 40, 100, 50, 100, 0 };
  11659. std::memcpy (t.delays[0].data(), d0, 20);
  11660. t.delays[0][22] = 1;
  11661. t.reverb = { 19, 221, 10, 50, 80, 30, 100, 2, 100 };
  11662. t.reverbOn = true;
  11663. const auto blob = t.blob();
  11664. const auto [l, r] = sytrusRender (blob, true, 12000, { { 60, 0, 6000 } });
  11665. // reference values (model render_mode=True, 44.1 kHz, block 512, key 60, velocity 0.78125, release at 6000); the same
  11666. // state rendered by FL's own Sytrus DLL in render mode differs from this by −136 dB (EXEC, REA round 31 harness)
  11667. const std::array<std::pair<int, float>, 8> golden { { { 100, -0.00235529384f }, { 777, -0.175126165f }, { 2000, 0.30976975f }, { 4321, -0.0213534199f },
  11668. { 6100, 0.202372357f }, { 7000, 0.249530748f }, { 9000, 0.0307101421f }, { 11999, 0.176390275f } } };
  11669. for (const auto& [i, v] : golden)
  11670. CHECK (std::abs (l[(std::size_t) i] - v) < 1e-6f);
  11671. const std::array<std::pair<int, float>, 3> goldenR { { { 777, -0.166890964f }, { 4321, -0.212358952f }, { 9000, -0.0251909085f } } };
  11672. for (const auto& [i, v] : goldenR)
  11673. CHECK (std::abs (r[(std::size_t) i] - v) < 1e-6f);
  11674. }
  11675. TEST (sytrus_instrument)
  11676. {
  11677. // the LYNX instrument: state in ChannelDef::pluginState, channel volume/pan recovered from the rack gains
  11678. SytrusTestPatch t;
  11679. t.cell (0, 0, 8, 256);
  11680. lynx::ChannelDef c;
  11681. c.instrument = lynx::InstrumentKind::sytrus;
  11682. c.pluginState = t.blob();
  11683. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sytrus);
  11684. inst->prepare (44100.0);
  11685. inst->configure (c, 120.0);
  11686. CHECK (inst->idle());
  11687. std::vector<float> l (4096), r (4096);
  11688. inst->noteOn (60, 100.0f / 128.0f);
  11689. // 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
  11690. inst->render (l.data(), r.data(), 4096, 0.5f, 0.5f);
  11691. double peak = 0.0;
  11692. for (int k = 1000; k < 4096; ++k)
  11693. peak = std::max (peak, (double) std::abs (l[(std::size_t) k]));
  11694. CHECK (std::abs (peak - 0.5 * 0.70710678) < 1e-3); // Final.Vol 0.5 × the plugin's centre pan law
  11695. CHECK (! inst->idle());
  11696. inst->noteOff (60);
  11697. std::vector<float> tail (44100 * 3), tailR (tail.size());
  11698. inst->render (tail.data(), tailR.data(), (int) tail.size(), 0.5f, 0.5f);
  11699. CHECK (inst->idle());
  11700. CHECK (tail.back() == 0.0f);
  11701. }
  11702. TEST (harmor_installation_presets)
  11703. {
  11704. // REA_rodada165g-flbeta165s: every Harmor preset of the installation plays on the exact engine — the 88 old
  11705. // (version 11…15) ones through the upgrade to the current layout, which renders exactly like the old bytes and
  11706. // keeps the parameter offsets editable; small images (Labyrinth's 16 × 12) are played, not skipped
  11707. if (realFlpDir.empty())
  11708. return;
  11709. const auto folder = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "Harmor";
  11710. if (! std::filesystem::exists (folder))
  11711. return;
  11712. int files = 0, usable = 0, old = 0, upgraded = 0, same = 0;
  11713. const std::vector<lynx::harmor::RefNote> notes { { 0, 60, 0.8, 1.0, 0.0, 3000 } };
  11714. for (const auto& entry : std::filesystem::recursive_directory_iterator (folder))
  11715. {
  11716. if (entry.path().extension() != ".fst")
  11717. continue;
  11718. std::ifstream in (entry.path(), std::ios::binary);
  11719. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  11720. std::vector<std::uint8_t> st;
  11721. try
  11722. {
  11723. for (const auto& c : flp::parseProject (file, true).channels)
  11724. if (! c.pluginState.empty())
  11725. st = c.pluginState;
  11726. }
  11727. catch (const flp::Error&)
  11728. {
  11729. }
  11730. ++files;
  11731. usable += lynx::harmorStateUsable (st);
  11732. std::uint32_t v = 0;
  11733. if (st.size() >= 4)
  11734. std::memcpy (&v, st.data(), 4);
  11735. if ((v > 0xffff ? v >> 16 : v) >= 16)
  11736. continue;
  11737. ++old;
  11738. const auto up = lynx::harmorUpgradeState (st);
  11739. if (! up)
  11740. continue;
  11741. std::uint32_t uv = 0;
  11742. std::memcpy (&uv, up->data(), 4);
  11743. upgraded += uv == 0x130003u && lynx::harmorParamOffset (*up, 0, 34) > 0 && lynx::harmorStateUsable (*up);
  11744. if (old <= 6) // (a few: rendering is the slow part)
  11745. same += lynx::harmor::renderReference (st, notes, 44100.0, 4096) == lynx::harmor::renderReference (*up, notes, 44100.0, 4096);
  11746. else
  11747. ++same;
  11748. }
  11749. std::printf (" Harmor installation presets: %d files, %d usable, %d old, %d upgraded\n", files, usable, old, upgraded);
  11750. CHECK (files >= 300 && usable == files && old >= 80 && upgraded == old && same == old);
  11751. std::ifstream in (folder / "Bell" / "Labyrinth.fst", std::ios::binary);
  11752. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  11753. if (file.empty())
  11754. return;
  11755. auto st = flp::parseProject (file, true).channels.at (0).pluginState;
  11756. const auto a = lynx::harmor::renderReference (st, notes, 44100.0, 4096);
  11757. const std::int32_t zero = 0;
  11758. for (int part = 0; part < 2; ++part)
  11759. std::memcpy (st.data() + lynx::harmorParamOffset (st, part, 92), &zero, 4); // image frequency scale 0
  11760. const auto b = lynx::harmor::renderReference (st, notes, 44100.0, 4096);
  11761. CHECK (a.size() == b.size() && a != b);
  11762. }
  11763. TEST (harmor_image)
  11764. {
  11765. // REA_rodada75: image synthesis — a looping 6-column image (gain + frequency planes) read at a speed, cubic gain and
  11766. // linear frequency interpolation, Hz frequency mode, formant shift — against our reference model (r75 imgm.py);
  11767. // since flbeta165h LYNX's own values: the test state plays at precision "Average" (global misc +28 = 0), whose
  11768. // IPP 11-bit Exp/Ln (REA_rodada165g-flbeta165h §3) the model computes exactly
  11769. const auto blob = harmorTestState (false, true);
  11770. if (const char* dump = std::getenv ("LYNX_DUMP_HARMOR_IMAGE"))
  11771. std::ofstream (dump, std::ios::binary).write ((const char*) blob.data(), (std::streamsize) blob.size());
  11772. CHECK (lynx::harmorStateUsable (blob));
  11773. CHECK (lynx::harmorFallbacks (blob).empty()); // (the image is modelled)
  11774. const auto ref = lynx::harmor::renderReference (blob, { { 0, 60, 0.78125, 1.0, 0.0, 6144 }, { 2000, 64, 0.6, 1.0, 0.0, 7000 } }, 44100.0, 8192);
  11775. CHECK (ref.size() == 2 * 8192);
  11776. if (std::getenv ("LYNX_PRINT_HARMOR_IMAGE") != nullptr)
  11777. for (int i : { 100, 1000, 2500, 3000, 5000, 6500, 8191 })
  11778. std::printf ("harmor image %d %.8g %.8g\n", i, (double) ref[2 * (std::size_t) i], (double) ref[2 * (std::size_t) i + 1]);
  11779. const std::tuple<int, float, float> golden[] {
  11780. { 100, 0.0022266121f, 0.018016478f }, { 1000, 0.013964245f, -0.028604792f }, { 2500, 0.016782919f, -0.028934296f },
  11781. { 3000, -0.010533399f, -0.0061015566f }, { 5000, -0.017427528f, 0.016475817f }, { 6500, 0.16895676f, 0.078423217f },
  11782. { 8191, -0.045406573f, -0.049047086f } };
  11783. for (const auto& [i, gl, gr] : golden)
  11784. CHECK (std::abs (ref[2 * (std::size_t) i] - gl) < 2e-6f && std::abs (ref[2 * (std::size_t) i + 1] - gr) < 2e-6f);
  11785. // the image changes the sound: without it (gain mix 0, frequency scale 0) the same notes render differently
  11786. auto plain = blob;
  11787. const std::int32_t zero = 0;
  11788. std::memcpy (plain.data() + lynx::harmorParamOffset (plain, 0, 89), &zero, 4);
  11789. std::memcpy (plain.data() + lynx::harmorParamOffset (plain, 0, 92), &zero, 4);
  11790. std::memcpy (plain.data() + lynx::harmorParamOffset (plain, 0, 94), &zero, 4);
  11791. const auto ref0 = lynx::harmor::renderReference (plain, { { 0, 60, 0.78125, 1.0, 0.0, 6144 }, { 2000, 64, 0.6, 1.0, 0.0, 7000 } }, 44100.0, 8192);
  11792. double diff = 0.0, energy = 0.0;
  11793. for (std::size_t i = 0; i < ref.size(); ++i)
  11794. diff += (double) (ref[i] - ref0[i]) * (ref[i] - ref0[i]), energy += (double) ref[i] * ref[i];
  11795. CHECK (energy > 1.0 && diff > 0.01 * energy);
  11796. // a resynthesis-mode image (a sample analysed at load time) is not modelled: reported, the part plays its timbres
  11797. auto resyn = blob;
  11798. {
  11799. // the image object follows the 26 curves: find its header (version 0, mode 0, loop 1) from the end of part A
  11800. const std::uint8_t hdr[] = { 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xf0, 0xbf };
  11801. const auto it = std::search (resyn.begin(), resyn.end(), std::begin (hdr), std::end (hdr));
  11802. CHECK (it != resyn.end());
  11803. if (it != resyn.end())
  11804. *(it + 4) = 1;
  11805. }
  11806. const auto fb = lynx::harmorFallbacks (resyn);
  11807. CHECK (std::any_of (fb.begin(), fb.end(), [] (const std::string& f) { return f.find ("resynthesis") != std::string::npos; }));
  11808. }
  11809. TEST (harmor_round75_features)
  11810. {
  11811. // REA_rodada75: arpeggio markers, noise resonance, pluck blur mode, pluck blend, harmonic randomness, phaser and
  11812. // harmonizer masks — LYNX's engine against our reference model (r75 hmodel.py, the Mersenne Twister seeded like LYNX's);
  11813. // since flbeta165h LYNX's own values: the test state plays at precision "Average", whose IPP 11-bit Exp/Ln
  11814. // (REA_rodada165g-flbeta165h §3) the model computes exactly
  11815. const std::vector<lynx::harmor::RefNote> chord { { 0, 60, 0.8, 1.0, 0.0, 9000 }, { 1500, 64, 0.7, 1.0, 0.0, 9500 }, { 3000, 67, 0.9, 1.0, 0.0, 10000 } };
  11816. struct Case { int features; std::array<std::tuple<int, float, float>, 4> golden; };
  11817. const Case cases[] {
  11818. { 1, { { { 2000, 0.0032625794f, -0.0014364491f }, { 4500, 0.10464316f, 0.026205163f }, { 7000, -0.0024230368f, 0.0036458215f }, { 11000, -0.0029985344f, -0.0030873856f } } } },
  11819. { 2, { { { 2000, 0.29411051f, -0.22980602f }, { 4500, -0.004929353f, -0.0013274907f }, { 7000, 0.013239861f, 0.0054404787f }, { 11000, -0.0010576053f, -0.0021088289f } } } },
  11820. { 4, { { { 2000, 0.072069392f, -0.029683406f }, { 4500, -0.10765476f, 0.11332012f }, { 7000, -0.22984716f, -0.10102195f }, { 11000, -0.051904719f, -0.066571109f } } } },
  11821. { 8, { { { 2000, 0.072010428f, -0.029725688f }, { 4500, -0.10748001f, 0.1136639f }, { 7000, -0.22849904f, -0.15257275f }, { 11000, -0.041248851f, -0.050450616f } } } },
  11822. { 16, { { { 2000, 0.063442282f, -0.030946331f }, { 4500, -0.11211193f, 0.11279358f }, { 7000, -0.23168804f, -0.1670858f }, { 11000, -0.041186355f, -0.050383076f } } } },
  11823. { 32, { { { 2000, 0.086303107f, -0.035885293f }, { 4500, 0.069407858f, 0.14133474f }, { 7000, -0.15907738f, -0.10380846f }, { 11000, -0.029428774f, -0.041782599f } } } },
  11824. { 63, { { { 2000, 0.017201126f, -0.010683246f }, { 4500, 0.55659908f, 0.08110752f }, { 7000, -0.0025542902f, 0.020610567f }, { 11000, -0.0031871174f, 0.00024673535f } } } } };
  11825. for (const auto& c : cases)
  11826. {
  11827. const auto blob = harmorTestState (false, false, c.features);
  11828. if (const char* dump = std::getenv ("LYNX_DUMP_HARMOR_R75"))
  11829. std::ofstream (std::string (dump) + std::to_string (c.features) + ".bin", std::ios::binary).write ((const char*) blob.data(), (std::streamsize) blob.size());
  11830. CHECK (lynx::harmorStateUsable (blob));
  11831. CHECK (lynx::harmorFallbacks (blob).empty());
  11832. const auto ref = lynx::harmor::renderReference (blob, chord, 44100.0, 12000);
  11833. if (std::getenv ("LYNX_PRINT_HARMOR_R75") != nullptr)
  11834. for (int i : { 2000, 4500, 7000, 11000 })
  11835. std::printf ("harmor r75 %d %d %.8g %.8g\n", c.features, i, (double) ref[2 * (std::size_t) i], (double) ref[2 * (std::size_t) i + 1]);
  11836. for (const auto& [i, gl, gr] : c.golden)
  11837. CHECK (std::abs (ref[2 * (std::size_t) i] - gl) < 2e-6f && std::abs (ref[2 * (std::size_t) i + 1] - gr) < 2e-6f);
  11838. }
  11839. }
  11840. TEST (sytrus_main_settings)
  11841. {
  11842. // the channel settings panel's controls (REA_rodada74 §3): FL's "Main settings" ints inside the zlib state
  11843. SytrusTestPatch t;
  11844. t.cell (0, 0, 8, 256);
  11845. t.main[173] = 3;
  11846. const auto blob = t.blob();
  11847. const auto& S = lynx::sytrus::settings();
  11848. auto find = [&] (const char* name) {
  11849. for (int i = 0; i < (int) S.size(); ++i)
  11850. if (std::string (S[(std::size_t) i].name) == name)
  11851. return i;
  11852. return -1;
  11853. };
  11854. const int master = find ("Master level"), unison = find ("Unison"), porta = find ("Porta"), live = find ("Live oversampling");
  11855. // 25 main controls, then the matrix (round 172a): FM 9 × 9 (sources, PAN, FX, OUT) and RM 9 × 6
  11856. CHECK (master >= 0 && unison >= 0 && porta >= 0 && live >= 0 && S.size() == 25u + 81u + 54u);
  11857. const int out1 = find ("Op 1 out"), fm21 = find ("FM op 2 to op 1"), rm3f2 = find ("RM op 3 to filter 2");
  11858. CHECK (out1 >= 0 && fm21 >= 0 && rm3f2 >= 0 && find ("RM op 1 pan") < 0);
  11859. CHECK (out1 >= 0 && S[(std::size_t) out1].index == 3 + 8 && S[(std::size_t) rm3f2].index == 3 + 81 + 9 * 7 + 2);
  11860. CHECK (lynx::sytrus::settingValue (blob, out1) == 256 && lynx::sytrus::settingValue (blob, fm21) == 0);
  11861. CHECK (lynx::sytrus::settingValue (blob, master) == 128 && lynx::sytrus::settingValue (blob, unison) == 3);
  11862. CHECK (lynx::sytrus::settingValue (blob, porta) == 0 && lynx::sytrus::settingValue (blob, find ("Mod X")) == 256);
  11863. CHECK (! lynx::sytrus::settingValue (blob, -1) && ! lynx::sytrus::settingValue (blob, (int) S.size()));
  11864. auto broken = blob;
  11865. broken.resize (broken.size() / 2);
  11866. CHECK (! lynx::sytrus::settingValue (broken, master) && ! lynx::sytrus::setSetting (broken, master, 64));
  11867. // master level 64: gain (11^(64/128) − 1)/10 instead of 1 (REA_rodada11 §1.3); the state stays readable
  11868. auto quiet = blob;
  11869. CHECK (lynx::sytrus::setSetting (quiet, master, 64) && lynx::sytrus::stateUsable (quiet));
  11870. CHECK (lynx::sytrus::settingValue (quiet, master) == 64 && lynx::sytrus::settingValue (quiet, unison) == 3);
  11871. CHECK (quiet[0] == blob[0] && quiet[1] == blob[1]); // version word kept
  11872. const auto [l1, r1] = sytrusRender (blob, false, 4096, { { 60, 0, -1 } });
  11873. const auto [l2, r2] = sytrusRender (quiet, false, 4096, { { 60, 0, -1 } });
  11874. const auto g = (std::sqrt (11.0) - 1.0) / 10.0;
  11875. double worst = 0.0;
  11876. for (int k = 0; k < 4096; ++k)
  11877. worst = std::max (worst, std::abs ((double) l2[(std::size_t) k] - g * l1[(std::size_t) k]));
  11878. CHECK (worst < 1e-5);
  11879. (void) r1, (void) r2;
  11880. // clamped to the control's range; PORTA is the flag byte the engine reads
  11881. auto edited = blob;
  11882. CHECK (lynx::sytrus::setSetting (edited, unison, 42) && lynx::sytrus::settingValue (edited, unison) == 9);
  11883. CHECK (lynx::sytrus::setSetting (edited, porta, 1) && lynx::sytrus::settingValue (edited, porta) == 1);
  11884. CHECK (lynx::sytrus::setSetting (edited, live, 2) && lynx::sytrus::settingValue (edited, live) == 2);
  11885. CHECK (lynx::sytrus::parsePatch (edited) != nullptr);
  11886. // a matrix cell: op 2 modulates op 1 (FM), clamped to ±256; the patch stays playable
  11887. CHECK (lynx::sytrus::setSetting (edited, fm21, 999) && lynx::sytrus::settingValue (edited, fm21) == 256);
  11888. CHECK (lynx::sytrus::settingValue (edited, out1) == 256 && lynx::sytrus::parsePatch (edited) != nullptr);
  11889. }
  11890. TEST (harmor_effects_rack)
  11891. {
  11892. // REA_rodada38: the rack runs in the state's unit order and the records' FX send joins it at unit 4 — with the FX
  11893. // amount at 1000 every record output bypasses a reverb placed before unit 4 (EXEC: −600 dB vs the rack off)
  11894. auto blob = harmorTestState (false);
  11895. auto set = [&] (std::vector<std::uint8_t>& st, int part, int index, std::int32_t v) {
  11896. std::memcpy (st.data() + lynx::harmorParamOffset (st, part, index), &v, 4);
  11897. };
  11898. set (blob, 0, 10, 1000); // all to the FX send
  11899. auto render = [] (const std::vector<std::uint8_t>& st) {
  11900. return lynx::harmor::renderReference (st, { { 0, 60, 0.8, 1.0, 0.0, 6000 } }, 44100.0, 12000);
  11901. };
  11902. const auto dry = render (blob);
  11903. auto rev = blob;
  11904. set (rev, -1, 22, 1); // reverb on: room 50, diffusion 100, decay 2^3·0.1 s, high cut / damping off, wet 0.8
  11905. 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);
  11906. set (rev, -1, 30, 2), set (rev, -1, 31, 800);
  11907. const auto bypassed = render (rev);
  11908. double eb = 0.0, db = 0.0;
  11909. for (std::size_t i = 0; i < dry.size(); ++i)
  11910. eb += (double) (bypassed[i] - dry[i]) * (bypassed[i] - dry[i]), db += (double) dry[i] * dry[i];
  11911. CHECK (eb < 1e-12 * db && db > 1e-3); // (float rounding of the rack's buffers only)
  11912. auto after = rev;
  11913. const std::int32_t order[6] { 0, 1, 2, 4, 3, 5 }; // the join before the reverb
  11914. for (int k = 0; k < 6; ++k)
  11915. set (after, -1, 53 + k, order[k]);
  11916. const auto wet = render (after);
  11917. double e = 0.0, d = 0.0;
  11918. bool finite = true;
  11919. for (std::size_t i = 0; i < wet.size(); ++i)
  11920. e += (double) (wet[i] - dry[i]) * (wet[i] - dry[i]), d += (double) dry[i] * dry[i], finite = finite && std::isfinite (wet[i]);
  11921. CHECK (finite && e > 1e-4 * d && d > 1e-3);
  11922. // a distortion (type 6) and a delay on the dry path change the sound, stay finite, and the instrument ends quiet
  11923. auto dist = harmorTestState (false);
  11924. {
  11925. const auto at = lynx::harmorParamOffset (dist, -1, 0) + 0xd4 + 0x18 + 48; // global misc +48: distortion type
  11926. const std::int32_t six = 6;
  11927. std::memcpy (dist.data() + at, &six, 4);
  11928. }
  11929. set (dist, -1, 17, 600), set (dist, -1, 19, 1000), set (dist, -1, 21, 1100);
  11930. 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);
  11931. set (dist, -1, 47, 1050);
  11932. lynx::ChannelDef c;
  11933. c.instrument = lynx::InstrumentKind::harmor;
  11934. c.pluginState = dist;
  11935. auto inst = lynx::makeInstrument (lynx::InstrumentKind::harmor);
  11936. inst->configure (c, 120.0);
  11937. inst->prepare (44100.0);
  11938. std::vector<float> l (44100 * 4), r (l.size());
  11939. inst->noteOn (60, 0.8f);
  11940. double energy = 0.0;
  11941. for (int s = 0; s < (int) l.size(); s += 512)
  11942. {
  11943. if (s == 8192)
  11944. inst->noteOff (60);
  11945. inst->render (l.data() + s, r.data() + s, std::min (512, (int) l.size() - s), 1.0f, 1.0f);
  11946. }
  11947. for (std::size_t i = 0; i < l.size(); ++i)
  11948. finite = finite && std::isfinite (l[i]) && std::isfinite (r[i]), energy += (double) l[i] * l[i];
  11949. CHECK (finite && energy > 1.0);
  11950. std::vector<float> tail (44100 * 6), tailR (tail.size());
  11951. for (int s = 0; s < (int) tail.size(); s += 512)
  11952. inst->render (tail.data() + s, tailR.data() + s, std::min (512, (int) tail.size() - s), 1.0f, 1.0f);
  11953. CHECK (inst->idle());
  11954. }
  11955. TEST (harmless_engine)
  11956. {
  11957. // REA_rodada32: state parsing, the additive engine against our reference model (hlmodel.py), host levels, voice end
  11958. std::vector<std::uint8_t> blob;
  11959. 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))); };
  11960. std::array<std::int32_t, 100> p {}; // the reference's clean patch: Saw, open filter, no modulation
  11961. for (int i = 11; i < 23; ++i)
  11962. p[(std::size_t) i] = 1000;
  11963. 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;
  11964. 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;
  11965. // + resonance, pluck, 3-voice unison, tremolo, LFO on pitch
  11966. 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;
  11967. put (12);
  11968. for (auto v : p)
  11969. put (v);
  11970. for (int i = 0; i < 7; ++i)
  11971. put (0);
  11972. put (524); // Custom colour table: 524 × (amplitude, phase)
  11973. for (int i = 0; i < 1048; ++i)
  11974. {
  11975. const auto f = i < 524 ? 1.0f / (float) (i + 1) : 0.25f;
  11976. std::uint32_t bits = 0;
  11977. std::memcpy (&bits, &f, 4);
  11978. put ((std::int32_t) bits);
  11979. }
  11980. const auto parsed = lynx::harmlessParams (blob);
  11981. CHECK (parsed && (int) parsed->size() == lynx::harmlessParamCount);
  11982. CHECK (parsed && (*parsed)[54] == 3500.0f && (*parsed)[99] == 0.0f && (*parsed)[lynx::harmlessCustomFlagIndex] == 1.0f);
  11983. CHECK (parsed && (*parsed)[lynx::harmlessCustomIndex + 1] == 0.5f && (*parsed)[lynx::harmlessCustomIndex + 524] == 0.25f);
  11984. auto old = blob;
  11985. old[0] = 3; // versions below 4 are not Harmless states we know
  11986. CHECK (! lynx::harmlessParams (old));
  11987. old = blob;
  11988. old.resize (300);
  11989. CHECK (! lynx::harmlessParams (old));
  11990. lynx::ChannelDef c;
  11991. c.instrument = lynx::InstrumentKind::harmless;
  11992. c.modelParams = *parsed;
  11993. c.modelParams[lynx::harmlessCustomFlagIndex] = 0.0f; // colour 0 (Saw): the custom table is not read
  11994. auto render = [] (const lynx::ChannelDef& def, int total, float gl, float gr, int offAt) {
  11995. auto inst = lynx::makeInstrument (lynx::InstrumentKind::harmless);
  11996. inst->configure (def, 120.0);
  11997. inst->prepare (44100.0);
  11998. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  11999. inst->noteOn (60, 0.78125f);
  12000. for (int s = 0; s < total; s += 512)
  12001. {
  12002. if (s == offAt)
  12003. inst->noteOff (60);
  12004. inst->render (l.data() + s, r.data() + s, std::min (512, total - s), gl, gr);
  12005. }
  12006. return std::make_tuple (l, r, std::move (inst));
  12007. };
  12008. {
  12009. // reference model (REA round 32 hlmodel.py, C5 at FL velocity 100, channel volume 1, centre, released at 6144)
  12010. auto [l, r, inst] = render (c, 8192, 1.0f, 1.0f, 6144);
  12011. const std::tuple<int, float, float> golden[] {
  12012. { 0, 0.0f, 0.0f }, { 10, -0.02220994f, -0.06232700f }, { 100, 0.40623373f, 0.17308202f }, { 1000, -0.04091963f, -0.26821208f },
  12013. { 3000, 0.14519004f, 0.11027778f }, { 6000, 0.50411958f, 0.27901855f }, { 8191, 0.02117692f, -0.01466602f } };
  12014. for (const auto& [i, gl, gr] : golden)
  12015. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  12016. // after the release the record fades out (below 1e-4, then 30 ms) and the instrument goes idle
  12017. std::vector<float> tail (44100), tailR (tail.size());
  12018. inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
  12019. CHECK (inst->idle());
  12020. }
  12021. // FL's level law applied once: half the channel volume halves the output; full right pan silences the left side
  12022. const auto full = std::get<0> (render (c, 2048, 1.0f, 1.0f, -1));
  12023. const auto half = std::get<0> (render (c, 2048, 0.5f, 0.5f, -1));
  12024. CHECK (std::abs (half[100] - 0.5f * full[100]) < 1e-6f && std::abs (full[100]) > 0.1f);
  12025. auto mono = c;
  12026. mono.modelParams[25] = 0.0f; // no tremolo, no unison pan spread
  12027. mono.modelParams[65] = 1.0f;
  12028. const auto [pl, pr, pi] = render (mono, 2048, 0.0f, 1.41421356f, -1);
  12029. double el = 0.0, er = 0.0;
  12030. for (int i = 0; i < 2048; ++i)
  12031. el += (double) pl[(std::size_t) i] * pl[(std::size_t) i], er += (double) pr[(std::size_t) i] * pr[(std::size_t) i];
  12032. CHECK (el < 1e-10 * er && er > 1.0);
  12033. }
  12034. TEST (startup_sound)
  12035. {
  12036. // the splash's sound: the right length, finite, at its peak level, quiet before the whoosh swells,
  12037. // a landing "pop" at impactTime and a chime ringing after it
  12038. using namespace lynx::startup;
  12039. const auto sound = makeSound (48000.0);
  12040. CHECK (sound.left.size() == (std::size_t) (soundLength * 48000.0) && sound.right.size() == sound.left.size());
  12041. float peak = 0.0f;
  12042. bool finite = true;
  12043. for (std::size_t i = 0; i < sound.left.size(); ++i)
  12044. {
  12045. finite = finite && std::isfinite (sound.left[i]) && std::isfinite (sound.right[i]);
  12046. peak = std::max ({ peak, std::abs (sound.left[i]), std::abs (sound.right[i]) });
  12047. }
  12048. CHECK (finite && std::abs (peak - peakLevel) < 1.0e-4f);
  12049. auto rms = [&sound] (double from, double to) {
  12050. double e = 0.0;
  12051. const auto a = (std::size_t) (from * 48000.0), b = (std::size_t) (to * 48000.0);
  12052. for (auto i = a; i < b; ++i)
  12053. e += (double) sound.left[i] * sound.left[i];
  12054. return std::sqrt (e / (double) (b - a));
  12055. };
  12056. CHECK (rms (0.0, 0.05) < 0.2 * rms (impactTime - 0.05, impactTime)); // the whoosh swells
  12057. CHECK (rms (impactTime, impactTime + 0.05) > rms (impactTime - 0.05, impactTime)); // the landing
  12058. CHECK (rms (0.8, 1.0) > 0.005 && rms (soundLength - 0.02, soundLength) < rms (0.8, 1.0)); // chime rings, tail fades
  12059. CHECK (makeSound (48000.0).left == sound.left); // the same sound on every start
  12060. }
  12061. namespace
  12062. {
  12063. // little-endian byte writer for the state builders below
  12064. struct Bytes
  12065. {
  12066. std::vector<std::uint8_t> b;
  12067. void raw (const char* s, std::size_t n) { b.insert (b.end(), s, s + n); }
  12068. 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); }
  12069. void f32 (float v) { b.insert (b.end(), reinterpret_cast<const std::uint8_t*> (&v), reinterpret_cast<const std::uint8_t*> (&v) + 4); }
  12070. void zeros (std::size_t n) { b.insert (b.end(), n, 0); }
  12071. void bytes (const std::vector<std::uint8_t>& o) { b.insert (b.end(), o.begin(), o.end()); }
  12072. };
  12073. // A GMS state written from scratch (REA_rodada12 §1.1 layout, "Syn4"): oscillator waves that don't exist (silent
  12074. // tables, so the result does not depend on an FL installation), noise through the ladder with envelope and LFO,
  12075. // unison, slide, distortion, delay, reverb and the output EQ.
  12076. std::vector<std::uint8_t> gmsTestState()
  12077. {
  12078. std::array<float, 212> P {};
  12079. for (int i : { 1, 2, 44, 55, 56, 57, 58, 59, 60, 207, 208, 209, 210, 211 })
  12080. P[(std::size_t) i] = 0.5f;
  12081. P[0] = 0.75f, P[4] = 0.1f, P[5] = 0.4f, P[6] = 0.6f, P[7] = 0.3f, P[8] = 0.8f;
  12082. 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;
  12083. 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;
  12084. P[147] = 1.0f, P[117] = 0.3f, P[127] = 0.6f; // distortion
  12085. P[153] = 1.0f, P[123] = 0.5f, P[133] = 0.4f, P[143] = 0.4f; // delay (1/2 beat)
  12086. P[154] = 1.0f, P[124] = 0.6f, P[134] = 0.5f; // reverb
  12087. P[206] = 1.0f, P[207] = 0.7f, P[211] = 0.3f; // output EQ
  12088. Bytes preset;
  12089. preset.raw ("4nyS", 4);
  12090. for (const char* path : { "NoSuchDir\\NoWave A.wav", "NoSuchDir\\NoWave B.wav", "NoSuchDir\\NoWave C.wav", "", "", "", "" })
  12091. {
  12092. const auto n = std::strlen (path);
  12093. preset.raw (path, n);
  12094. preset.zeros (512 - n);
  12095. }
  12096. preset.i32 (0), preset.i32 (0), preset.i32 (0);
  12097. for (auto v : P)
  12098. preset.f32 (v);
  12099. preset.raw ("LYNX test", 9);
  12100. preset.zeros (512 - 9);
  12101. Bytes s;
  12102. s.raw ("SSLF", 4);
  12103. s.i32 ((std::int32_t) preset.b.size());
  12104. s.bytes (preset.b);
  12105. return s.b;
  12106. }
  12107. // A Morphine state written from scratch (REA_rodada16 §3 / REA_rodada39 §2.2 layout): generators A and B with a few
  12108. // frames each (A loops, PWM on A), C and D silent, a morph line A→B, four envelopes (Eg1 → pitch and velocity →
  12109. // amplitude through the matrix), the master ADSR and every plugin effect on.
  12110. std::vector<std::uint8_t> morphineTestState()
  12111. {
  12112. auto mbp = [] (int seed) {
  12113. Bytes b;
  12114. b.raw ("0PBM", 4);
  12115. for (int a = 0; a < 3; ++a)
  12116. for (int byte = 0; byte < 16; ++byte)
  12117. {
  12118. b.b.push_back (0xff);
  12119. for (int bit = 0; bit < 8; ++bit)
  12120. {
  12121. const auto k = byte * 8 + bit;
  12122. // (cbrtf is not correctly rounded before glibc 2.43 and differs between libms; the cube root in double
  12123. // rounded to float gives the same (correctly rounded) bits with old and new glibc)
  12124. const float v = a == 0 ? (float) std::cbrt ((double) (1.0f / (float) (k + 1 + seed * (k % 3)))) * (k < 40 ? 1.0f : 0.2f)
  12125. : a == 1 ? 0.1f * (float) ((k * 7 + seed) % 11 - 5) / 5.0f
  12126. : 0.02f * (float) ((k * 5 + seed) % 7 - 3);
  12127. b.f32 (v);
  12128. }
  12129. }
  12130. return b.b;
  12131. };
  12132. auto msp = [&] (int frames, int seed, int loopMode) {
  12133. Bytes b;
  12134. b.raw ("0PSM", 4);
  12135. b.i32 (frames);
  12136. for (int f = 0; f < frames; ++f)
  12137. {
  12138. const auto m = mbp (seed + f);
  12139. b.i32 (20 + 7 * f), b.f32 (1.0f - 0.2f * (float) f), b.i32 ((std::int32_t) m.size());
  12140. b.bytes (m);
  12141. }
  12142. b.i32 (0x424);
  12143. b.i32 (10), b.i32 (40), b.i32 (0), b.i32 (loopMode);
  12144. b.f32 (0.45f), b.f32 (0.6f), b.f32 (0.5f), b.i32 (0);
  12145. for (int k = 0; k < 128; ++k)
  12146. b.f32 (0.3f);
  12147. for (int k = 0; k < 128; ++k)
  12148. b.f32 (1.0f);
  12149. return b.b;
  12150. };
  12151. auto mvc = [&] (int frames, int seed, int pwmMode) {
  12152. Bytes z;
  12153. z.raw ("0HZM", 4);
  12154. z.i32 (1);
  12155. const auto s = msp (frames, seed, 1);
  12156. z.i32 (128), z.f32 (frames > 1 ? 0.9f : 0.0f), z.i32 ((std::int32_t) s.size());
  12157. z.bytes (s);
  12158. Bytes b;
  12159. b.raw ("0CVM", 4);
  12160. b.i32 ((std::int32_t) z.b.size());
  12161. b.bytes (z.b);
  12162. 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);
  12163. return b.b;
  12164. };
  12165. Bytes p;
  12166. p.raw ("0roM", 4);
  12167. p.raw ("0RPM", 4);
  12168. for (const auto& g : { mvc (3, 1, 2), mvc (2, 7, 0), mvc (1, 3, 0), mvc (1, 5, 0) })
  12169. {
  12170. p.i32 ((std::int32_t) g.size());
  12171. p.bytes (g);
  12172. }
  12173. {
  12174. Bytes m;
  12175. m.raw ("0LMM", 4);
  12176. m.i32 (2), m.i32 (12);
  12177. m.f32 (0.0f), m.f32 (1.0f), m.i32 (60);
  12178. m.f32 (1.0f), m.f32 (1.0f), m.i32 (60);
  12179. m.i32 (0x54), m.i32 (0), m.i32 (0), m.i32 (0), m.i32 (0);
  12180. for (float v : { 1.0f, 0.8f, 0.0f, 0.0f, 0.5f, 0.4f, 0.5f, 0.5f })
  12181. m.f32 (v);
  12182. for (int k = 0; k < 4; ++k)
  12183. m.i32 (0);
  12184. m.f32 (0.5f), m.f32 (0.5f), m.f32 (0.5f), m.i32 (0);
  12185. p.i32 ((std::int32_t) m.b.size());
  12186. p.bytes (m.b);
  12187. }
  12188. {
  12189. Bytes d;
  12190. d.raw ("0DMM", 4);
  12191. d.i32 (4);
  12192. for (int e = 0; e < 4; ++e)
  12193. {
  12194. Bytes g;
  12195. g.raw ("0GEM", 4);
  12196. g.i32 (3), g.i32 (8);
  12197. g.f32 (0.0f), g.i32 (10), g.f32 (1.0f), g.i32 (40 + 10 * e), g.f32 (0.3f), g.i32 (50);
  12198. 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),
  12199. g.i32 (e == 0 ? 1 : 0);
  12200. d.i32 ((std::int32_t) g.b.size());
  12201. d.bytes (g.b);
  12202. }
  12203. d.i32 (0x94);
  12204. const int src[12] = { 5, 2 }, dst[12] = { 3, 1 };
  12205. const float amt[12] = { 0.56f, 0.7f };
  12206. for (int k = 0; k < 12; ++k)
  12207. d.i32 (src[k]);
  12208. for (int k = 0; k < 12; ++k)
  12209. d.i32 (dst[k]);
  12210. for (int k = 0; k < 12; ++k)
  12211. d.f32 (amt[k]);
  12212. p.i32 ((std::int32_t) d.b.size());
  12213. p.bytes (d.b);
  12214. }
  12215. {
  12216. 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
  12217. 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 …
  12218. 0.4f, 0.3f, 1.0f, 0.5f, 0.5f, 0.4f, 1.0f, 0.6f, 0.4f, 0.5f, // … delay, reverb, EQ …
  12219. 0.5f, 0.5f, 0.55f, 0.5f, 0.45f, 1.0f, 0.0f, 0.5f, 1.0f, 0.0f }; // EQ on, noise (muted)
  12220. Bytes g;
  12221. g.i32 (0x5c4);
  12222. for (auto v : A)
  12223. g.f32 (v);
  12224. g.zeros (0x5c4 - g.b.size());
  12225. p.bytes (g.b);
  12226. }
  12227. Bytes s;
  12228. s.raw ("SSLF", 4);
  12229. s.i32 ((std::int32_t) p.b.size());
  12230. s.bytes (p.b);
  12231. return s.b;
  12232. }
  12233. void dumpIfAsked (const char* var, const std::vector<std::uint8_t>& blob)
  12234. {
  12235. if (const char* path = std::getenv (var))
  12236. {
  12237. std::ofstream out (path, std::ios::binary);
  12238. out.write (reinterpret_cast<const char*> (blob.data()), (std::streamsize) blob.size());
  12239. }
  12240. }
  12241. } // namespace
  12242. TEST (dx10_engine)
  12243. {
  12244. // REA_rodada42: a Fruity DX10 state written from scratch (version 1: 22 raw parameters) against our reference model
  12245. // (dxmodel.py + dxproc.py, bit-identical to the installation's DLL), the channel processor, host levels and the
  12246. // instrument front-end
  12247. const std::int32_t P[22] = { 1000, 40000, 30000, 30000, 40000, 30000, 35000, 8000, 40000, 50000, 20000,
  12248. 25000, 15000, 30000, 20000, 10000, 20000, 30000, 5000, 30000, 20000, -1 };
  12249. std::vector<std::uint8_t> blob (4 + 22 * 4, 0);
  12250. blob[0] = 1;
  12251. std::memcpy (blob.data() + 4, P, sizeof (P));
  12252. CHECK (lynx::dx10StateUsable (blob) && lynx::dx10ProcessorNeutral (blob));
  12253. CHECK (lynx::dx10Param (blob, 21) == -1 && lynx::dx10Param (blob, 3) == 30000 && ! lynx::dx10Param (blob, 22));
  12254. CHECK (! lynx::dx10StateUsable (std::vector<std::uint8_t> (40, 0)));
  12255. {
  12256. auto bad = blob;
  12257. bad[0] = 3; // versions ≥ 3 are ignored by the plugin
  12258. CHECK (! lynx::dx10StateUsable (bad));
  12259. }
  12260. using N = lynx::dx10::RefNote;
  12261. 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 } };
  12262. struct Golden { std::vector<std::uint8_t> state; lynx::dx10::RefOptions opt; int rate; double el, er; std::tuple<int, float, float> at[6]; };
  12263. auto check = [&] (const Golden& g) {
  12264. const auto y = lynx::dx10::renderReference (g.state, notes, (double) g.rate, 16384, g.opt);
  12265. CHECK (y.size() == 2 * 16384);
  12266. if (y.size() != 2 * 16384)
  12267. return y;
  12268. double el = 0.0, er = 0.0;
  12269. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  12270. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  12271. CHECK (std::abs (el - g.el) <= 1e-7 * g.el && std::abs (er - g.er) <= 1e-7 * g.er);
  12272. for (const auto& [i, l, r] : g.at)
  12273. CHECK (goldenSampleClose (y[2 * (std::size_t) i], l, libmTolerance (1e-7f))
  12274. && goldenSampleClose (y[2 * (std::size_t) i + 1], r, libmTolerance (1e-7f)));
  12275. return y;
  12276. };
  12277. lynx::dx10::RefOptions plain, odd;
  12278. odd.block = 333, odd.maxPoly = 1;
  12279. check ({ blob, plain, 44100, 672.293115, 778.393071,
  12280. { { 100, -0.0943799689f, -0.154014125f }, { 1000, -0.0814286768f, -0.132879525f }, { 2500, 0.499493301f, 0.312498778f },
  12281. { 5000, -0.184045464f, 0.171641544f }, { 9000, -0.153369367f, -0.20805186f }, { 16383, -0.00441304967f, 0.200126961f } } });
  12282. // 48 kHz, odd host blocks, max polyphony 1 (the last note steals); the hard-right note keeps FL's x87 left gain
  12283. check ({ blob, odd, 48000, 122.232761, 493.320297,
  12284. { { 100, 0.0236493405f, 0.0385922194f }, { 1000, 0.189806491f, 0.309736073f }, { 2500, -0.425576478f, -0.176279545f },
  12285. { 5000, -3.54030076e-18f, -0.0578194074f }, { 9000, 1.37052479e-17f, 0.223831087f }, { 16383, 9.248742e-18f, 0.151048407f } } });
  12286. // the channel processor (§3): our own settings snapshot — pan LFO (triangle, delayed, faded in), volume envelope
  12287. // with tensions and LFO, cutoff envelope + square LFO, resonance LFO, pitch envelope, SVF LP ×2 (type 7)
  12288. auto record = [] (std::vector<std::uint8_t>& s, int k, std::initializer_list<std::pair<int, int>> fields) {
  12289. const std::int32_t neutral[17] = { 0, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, 0 };
  12290. std::memcpy (s.data() + 0x44 * k, neutral, sizeof (neutral));
  12291. for (const auto& [i, v] : fields)
  12292. std::memcpy (s.data() + 0x44 * k + 4 * i, &v, 4);
  12293. };
  12294. auto settings = [] (std::vector<std::uint8_t>& s, std::int32_t modX, std::int32_t modY, std::int32_t type) {
  12295. std::memcpy (s.data() + 0x15c, &modX, 4);
  12296. std::memcpy (s.data() + 0x160, &modY, 4);
  12297. std::memcpy (s.data() + 0x168, &type, 4);
  12298. };
  12299. std::vector<std::uint8_t> snap (364, 0);
  12300. record (snap, 0, { { 9, 5000 }, { 10, 9000 }, { 11, -40 }, { 12, 15000 }, { 13, 1 } });
  12301. 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 } });
  12302. record (snap, 2, { { 1, 1 }, { 3, 6000 }, { 4, 100 }, { 5, 16000 }, { 6, 20 }, { 8, -60 }, { 11, 20 }, { 12, 18000 }, { 13, 2 } });
  12303. record (snap, 3, { { 11, 40 }, { 12, 17000 } });
  12304. record (snap, 4, { { 1, 1 }, { 3, 5000 }, { 4, 100 }, { 5, 14000 }, { 6, 30 }, { 8, -700 } });
  12305. settings (snap, 140, 120, 7);
  12306. const auto withSnap = lynx::dx10StateWithSnapshot (blob, snap);
  12307. CHECK (withSnap.size() == 460 && withSnap[0] == 2 && ! lynx::dx10ProcessorNeutral (withSnap));
  12308. CHECK (lynx::dx10StateWithSnapshot (withSnap, std::vector<std::uint8_t> (364, 0)) == withSnap); // a v2 state keeps its own
  12309. lynx::dx10::RefOptions ticks;
  12310. ticks.tick = 230, ticks.ppq = 96, ticks.tempo = 120.0;
  12311. check ({ withSnap, ticks, 44100, 346.852062, 554.699375,
  12312. { { 100, -0.00162654708f, -0.00265428401f }, { 1000, 0.0310285408f, 0.0506339073f }, { 2500, -0.118068494f, -0.0915558636f },
  12313. { 5000, -0.297166437f, -0.256465197f }, { 9000, 0.199509233f, 0.166108996f }, { 16383, -0.00392446062f, 0.0410436988f } } });
  12314. // a volume envelope decaying to zero: the 10 ms end fade, then every voice is gone (silence from 14950 on)
  12315. std::vector<std::uint8_t> decay (364, 0);
  12316. for (int k = 0; k < 5; ++k)
  12317. record (decay, k, {});
  12318. record (decay, 1, { { 0, 4 }, { 1, 1 }, { 3, 9000 }, { 4, 100 }, { 5, 12000 }, { 6, 0 }, { 15, 50 }, { 16, -101 } });
  12319. settings (decay, 256, 0, 0);
  12320. auto ticks48 = ticks;
  12321. ticks48.block = 333;
  12322. const auto y = check ({ lynx::dx10StateWithSnapshot (blob, decay), ticks48, 48000, 338.361707, 350.520208,
  12323. { { 100, 0.000514116196f, 0.000838961045f }, { 1000, 0.0412623473f, 0.0673340634f }, { 2500, -0.141532451f, -0.156581759f },
  12324. { 5000, 0.382550895f, 0.23462519f }, { 7000, -0.189829409f, 0.144132584f }, { 9000, -0.13638559f, 0.142912149f } } });
  12325. bool silentTail = y.size() == 2 * 16384 && (y[2 * 14949] != 0.0f || y[2 * 14949 + 1] != 0.0f);
  12326. for (std::size_t i = 2 * 14950; i < y.size(); ++i)
  12327. silentTail = silentTail && y[i] == 0.0f;
  12328. CHECK (silentTail);
  12329. // the instrument: LYNX velocity v → Init.Vol = (11^(1.28·v) − 1)/10 and Final.Vol = channel volume × Init.Vol; channel
  12330. // gains √2·V·(cos, sin)((1 + p)π/4) → V, p; notes on block boundaries play like the reference renderer (neutral
  12331. // channel settings: the instrument's sequencer ticks change nothing; vibrato off, since the plugin-wide vibrato
  12332. // oscillator is stepped by each voice in turn, so its interleaving follows the host block layout, as in FL)
  12333. auto noVib = blob;
  12334. CHECK (lynx::dx10SetParam (noVib, 10, 0));
  12335. auto initVol = [] (double v) { return (double) (float) ((std::pow (11.0, 1.28 * v) - 1.0) / 10.0); }; // Init.Vol is a float
  12336. lynx::ChannelDef c;
  12337. c.instrument = lynx::InstrumentKind::dx10;
  12338. c.pluginState = noVib;
  12339. auto inst = lynx::makeInstrument (lynx::InstrumentKind::dx10);
  12340. inst->configure (c, 120.0);
  12341. inst->prepare (44100.0);
  12342. const float V = 0.6f, pan = 0.25f;
  12343. const auto gl = (float) (std::sqrt (2.0) * V * std::cos ((1.0 + pan) * 3.141592653589793 / 4.0));
  12344. const auto gr = (float) (std::sqrt (2.0) * V * std::sin ((1.0 + pan) * 3.141592653589793 / 4.0));
  12345. std::vector<float> l (16384), r (16384);
  12346. for (int s = 0; s < 16384; s += 512)
  12347. {
  12348. if (s == 0)
  12349. inst->noteOn (60, 0.78125f);
  12350. if (s == 2048)
  12351. {
  12352. inst->setNextNoteExpression (-0.5f, 25.0f);
  12353. inst->noteOn (67, 0.5f);
  12354. }
  12355. if (s == 6144)
  12356. inst->noteOff (60);
  12357. if (s == 8192)
  12358. inst->noteOff (67);
  12359. inst->render (l.data() + s, r.data() + s, 512, gl, gr);
  12360. }
  12361. // the channel level reaches the engine as FL's (recovered to float precision)
  12362. const auto Vr = std::sqrt (((double) gl * gl + (double) gr * gr) * 0.5);
  12363. const auto pr = std::atan2 ((double) gr, (double) gl) * (4.0 / 3.141592653589793) - 1.0;
  12364. const auto ref = lynx::dx10::renderReference (noVib, { { 0, 60, initVol (0.78125), Vr * initVol (0.78125), pr, 0.0, 6144 },
  12365. { 2048, 67, initVol (0.5), Vr * initVol (0.5), std::clamp (-0.5 + pr, -1.0, 1.0), 25.0, 8192 } },
  12366. 44100.0, 16384);
  12367. double worst = 0.0, peak = 0.0;
  12368. for (int i = 0; i < 16384; ++i)
  12369. 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]) }),
  12370. peak = std::max (peak, (double) std::abs (l[(std::size_t) i]));
  12371. CHECK (worst == 0.0 && peak > 0.05);
  12372. // the released voices end (DX10's own release) and the instrument goes idle
  12373. std::vector<float> tail (44100), tailR (tail.size());
  12374. for (int k = 0; k < 20 && ! inst->idle(); ++k)
  12375. inst->render (tail.data(), tailR.data(), (int) tail.size(), gl, gr);
  12376. CHECK (inst->idle());
  12377. // with the channel settings above the instrument follows FL's ticks: the cutoff envelope darkens the note
  12378. {
  12379. lynx::ChannelDef cs = c;
  12380. cs.pluginState = withSnap;
  12381. cs.modelParams = { 96.0f };
  12382. auto si = lynx::makeInstrument (lynx::InstrumentKind::dx10);
  12383. si->configure (cs, 120.0);
  12384. si->prepare (44100.0);
  12385. std::vector<float> sl (32768), sr (32768);
  12386. si->noteOn (48, 0.78125f);
  12387. for (int s = 0; s < 32768; s += 512)
  12388. si->render (sl.data() + s, sr.data() + s, 512, 1.0f, 1.0f);
  12389. double e1 = 0.0, e2 = 0.0;
  12390. bool finite = true;
  12391. for (int i = 0; i < 32768; ++i)
  12392. finite = finite && std::isfinite (sl[(std::size_t) i]), (i < 4096 ? e1 : e2) += (double) sl[(std::size_t) i] * sl[(std::size_t) i];
  12393. CHECK (finite && e1 > 1e-3 && e2 > 1e-3);
  12394. }
  12395. // parameter edits (the channel settings panel) write the raw integers, clamped to the knob ranges
  12396. auto edited = blob;
  12397. CHECK (lynx::dx10SetParam (edited, 21, 7) && lynx::dx10Param (edited, 21) == 3);
  12398. CHECK (lynx::dx10SetParam (edited, 12, 70000) && lynx::dx10Param (edited, 12) == 65536);
  12399. }
  12400. TEST (threeosc_engine)
  12401. {
  12402. // REA_rodada47: a 3x Osc state written from scratch (version 14) against our reference model (osc3model.py,
  12403. // bit-identical to the installation's DLL), the plugin voice processor with its release path, the instrument
  12404. 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 };
  12405. 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) {
  12406. std::vector<std::uint8_t> st (0x60, 0);
  12407. const std::int32_t version = 14;
  12408. std::memcpy (st.data(), &version, 4);
  12409. std::memcpy (st.data() + 4, p, 21 * 4);
  12410. st[0x58] = inv[0], st[0x59] = inv[1], st[0x5a] = inv[2], st[0x5b] = am;
  12411. st.insert (st.end(), snap.begin(), snap.end());
  12412. return st;
  12413. };
  12414. auto record = [] (std::vector<std::uint8_t>& s, int k, std::initializer_list<std::pair<int, int>> fields) {
  12415. const std::int32_t neutral[17] = { 0, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, 0 };
  12416. std::memcpy (s.data() + 0x44 * k, neutral, sizeof (neutral));
  12417. for (const auto& [i, v] : fields)
  12418. std::memcpy (s.data() + 0x44 * k + 4 * i, &v, 4);
  12419. };
  12420. auto settings = [] (std::vector<std::uint8_t>& s, std::int32_t modX, std::int32_t modY, std::int32_t type) {
  12421. std::memcpy (s.data() + 0x15c, &modX, 4);
  12422. std::memcpy (s.data() + 0x160, &modY, 4);
  12423. std::memcpy (s.data() + 0x168, &type, 4);
  12424. };
  12425. std::vector<std::uint8_t> neutral (364, 0);
  12426. for (int k = 0; k < 5; ++k)
  12427. record (neutral, k, k == 1 ? std::initializer_list<std::pair<int, int>> { { 0, 4 }, { 16, -101 } } : std::initializer_list<std::pair<int, int>> {});
  12428. settings (neutral, 256, 0, 0);
  12429. const auto blob = make (P, { 0, 0, 0 }, 0, neutral);
  12430. CHECK (lynx::threeOscStateUsable (blob) && lynx::threeOscNormalisedState (blob, {}) == blob);
  12431. CHECK (lynx::threeOscParam (blob, 9) == -12 && lynx::threeOscParam (blob, 20) == 16 && lynx::threeOscParam (blob, 24) == 0 && ! lynx::threeOscParam (blob, 26));
  12432. // older versions are normalised like the plugin reads them: version 12 (no snapshot → the plugin's defaults),
  12433. // version 10 (20 parameters, no invert/AM bytes)
  12434. {
  12435. std::vector<std::uint8_t> v12 (blob.begin(), blob.begin() + 92);
  12436. v12[0] = 12;
  12437. v12[0x58] = 1;
  12438. const auto n12 = lynx::threeOscNormalisedState (v12, {});
  12439. CHECK (n12.size() == 460 && n12[0] == 14 && lynx::threeOscParam (n12, 21) == 1 && lynx::threeOscParam (n12, 20) == 16
  12440. && std::equal (n12.begin() + 0x60, n12.end(), neutral.begin()));
  12441. std::vector<std::uint8_t> v10 (blob.begin(), blob.begin() + 84);
  12442. v10[0] = 10;
  12443. const auto n10 = lynx::threeOscNormalisedState (v10, {});
  12444. CHECK (lynx::threeOscParam (n10, 20) == 0 && lynx::threeOscParam (n10, 13) == 40 && lynx::threeOscParam (n10, 21) == 0);
  12445. }
  12446. using N = lynx::threeosc::RefNote;
  12447. 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 } };
  12448. struct Golden { std::vector<std::uint8_t> state; lynx::threeosc::RefOptions opt; int rate; double el, er; std::tuple<int, float, float> at[6]; };
  12449. auto check = [&] (const Golden& g) {
  12450. const auto y = lynx::threeosc::renderReference (g.state, notes, (double) g.rate, 16384, g.opt);
  12451. CHECK (y.size() == 2 * 16384);
  12452. if (y.size() != 2 * 16384)
  12453. return y;
  12454. double el = 0.0, er = 0.0;
  12455. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  12456. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  12457. CHECK (std::abs (el - g.el) <= 1e-7 * g.el && std::abs (er - g.er) <= 1e-7 * g.er);
  12458. for (const auto& [i, l, r] : g.at)
  12459. CHECK (goldenSampleClose (y[2 * (std::size_t) i], l, libmTolerance (1e-7f))
  12460. && goldenSampleClose (y[2 * (std::size_t) i + 1], r, libmTolerance (1e-7f)));
  12461. return y;
  12462. };
  12463. // saw / square / triangle-ish mix with pans, stereo detune and phase, random start phases (seed 12345)
  12464. lynx::threeosc::RefOptions plain;
  12465. plain.seed = 12345;
  12466. check ({ blob, plain, 44100, 681.6491972612481, 1509.7245664533937,
  12467. { { 100, 0.0674247071146965f, -0.13440212607383728f }, { 1000, 0.12890009582042694f, 0.446039617061615f },
  12468. { 2500, -0.007686644792556763f, 0.3574123680591583f }, { 5000, 0.08792756497859955f, 0.4948147237300873f },
  12469. { 9000, 1.2136793475613885e-17f, 0.3116455078125f }, { 16383, 5.7305530145831346e-18f, 0.4134995639324188f } } });
  12470. // AM (osc 3 noise modulating osc 1 + 2), inverted oscillators, 48 kHz, odd blocks, max polyphony 1
  12471. 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 };
  12472. lynx::threeosc::RefOptions odd;
  12473. odd.block = 333, odd.maxPoly = 1, odd.seed = 777;
  12474. check ({ make (Pam, { 1, 0, 1 }, 1, neutral), odd, 48000, 453.27241060751771, 1996.4548848690094,
  12475. { { 100, -0.2589033246040344f, -0.310184121131897f }, { 1000, 0.22021546959877014f, 0.3591070771217346f },
  12476. { 2500, -0.5378497242927551f, -0.15602676570415497f }, { 5000, 1.2423200626807308e-17f, 0.3293072581291199f },
  12477. { 9000, 2.5857780099332177e-17f, 0.42474493384361267f }, { 16383, 2.941168384618868e-17f, 0.3569665551185608f } } });
  12478. // the channel processor with its release path: pan LFO, volume envelope released from its current value, cutoff
  12479. // envelope + square LFO, resonance LFO, pitch envelope, SVF LP ×2 (type 7)
  12480. std::vector<std::uint8_t> snap (364, 0);
  12481. record (snap, 0, { { 9, 5000 }, { 10, 9000 }, { 11, -40 }, { 12, 15000 }, { 13, 1 } });
  12482. 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 } });
  12483. record (snap, 2, { { 1, 1 }, { 3, 6000 }, { 4, 100 }, { 5, 16000 }, { 6, 20 }, { 7, 9000 }, { 8, -60 }, { 11, 20 }, { 12, 18000 }, { 13, 2 } });
  12484. record (snap, 3, { { 11, 40 }, { 12, 17000 } });
  12485. record (snap, 4, { { 1, 1 }, { 3, 5000 }, { 4, 100 }, { 5, 14000 }, { 6, 30 }, { 7, 12000 }, { 8, -700 } });
  12486. settings (snap, 140, 120, 7);
  12487. lynx::threeosc::RefOptions ticks;
  12488. ticks.tick = 230, ticks.ppq = 96, ticks.tempo = 120.0, ticks.seed = 99;
  12489. check ({ make (P, { 0, 0, 0 }, 0, snap), ticks, 44100, 977.9928196811321, 2072.965349921462,
  12490. { { 100, -0.001351095037534833f, 0.0029758031014353037f }, { 1000, 0.002833257894963026f, 0.10575854033231735f },
  12491. { 2500, 0.020360738039016724f, 0.3181821405887604f }, { 5000, 0.3340669274330139f, 0.4912331700325012f },
  12492. { 9000, 0.26940545439720154f, 0.27708977460861206f }, { 16383, 0.16570518910884857f, -0.10794822871685028f } } });
  12493. auto ticks48 = ticks;
  12494. ticks48.block = 333, ticks48.tick = 250, ticks48.seed = 5;
  12495. // (at 48 kHz the library's rate ratio 44100/fs scales the filter coefficients and the ramp limit)
  12496. check ({ make (P, { 0, 1, 0 }, 0, snap), ticks48, 48000, 864.9859578029492, 1895.6281818565287,
  12497. { { 100, 0.0031638722866773605f, -0.001987025374546647f }, { 1000, 0.049239177256822586f, 0.08794696629047394f },
  12498. { 2500, 0.13807585835456848f, 0.3554498553276062f }, { 5000, -0.08793184161186218f, -0.1596519947052002f },
  12499. { 9000, 0.08728396147489548f, 0.540290117263794f }, { 16383, 0.039887525141239166f, 0.21270982921123505f } } });
  12500. // HQ oscillators (engine_x64.dll: band-limited tables built with an FFT; FL's single-precision FFT and LYNX's double
  12501. // one agree to float rounding, so the golden values hold to 1e-5)
  12502. {
  12503. 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 };
  12504. auto hq = make (Phq, { 0, 0, 0 }, 0, neutral);
  12505. hq[0x5c] = 1;
  12506. CHECK (lynx::threeOscParam (hq, 25) == 1);
  12507. lynx::threeosc::RefOptions o;
  12508. o.seed = 3;
  12509. const auto y = lynx::threeosc::renderReference (hq, notes, 44100.0, 16384, o);
  12510. CHECK (y.size() == 2 * 16384);
  12511. if (y.size() == 2 * 16384)
  12512. {
  12513. double el = 0.0, er = 0.0;
  12514. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  12515. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  12516. CHECK (std::abs (el - 862.7912087773848) < 1e-5 * 862.8 && std::abs (er - 1232.8046929286115) < 1e-5 * 1232.8);
  12517. const std::tuple<int, float, float> at[] = { { 100, -0.019788391888141632f, -0.19209949672222137f }, { 1000, 0.12019827216863632f, 0.03852222114801407f },
  12518. { 2500, 0.3239150643348694f, 0.07325324416160583f }, { 5000, -0.2809634208679199f, -0.5693862438201904f },
  12519. { 16383, -1.1937656360586644e-17f, -0.061853960156440735f } };
  12520. for (const auto& [i, l, r] : at)
  12521. CHECK (std::abs (y[2 * (std::size_t) i] - l) < 1e-5f && std::abs (y[2 * (std::size_t) i + 1] - r) < 1e-5f);
  12522. }
  12523. }
  12524. // the instrument: Final.Vol = channel volume × Init.Vol, channel gains → FL's volume/pan; notes on block boundaries
  12525. // play like the reference renderer (seed 0, neutral channel settings: ticks change nothing)
  12526. auto initVol = [] (double v) { return (double) (float) ((std::pow (11.0, 1.28 * v) - 1.0) / 10.0); };
  12527. lynx::ChannelDef c;
  12528. c.instrument = lynx::InstrumentKind::threeosc;
  12529. c.pluginState = blob;
  12530. auto inst = lynx::makeInstrument (lynx::InstrumentKind::threeosc);
  12531. inst->configure (c, 120.0);
  12532. inst->prepare (44100.0);
  12533. const float V = 0.6f, pan = 0.25f;
  12534. const auto gl = (float) (std::sqrt (2.0) * V * std::cos ((1.0 + pan) * 3.141592653589793 / 4.0));
  12535. const auto gr = (float) (std::sqrt (2.0) * V * std::sin ((1.0 + pan) * 3.141592653589793 / 4.0));
  12536. std::vector<float> l (16384), r (16384);
  12537. for (int s = 0; s < 16384; s += 512)
  12538. {
  12539. if (s == 0)
  12540. inst->noteOn (60, 0.78125f);
  12541. if (s == 2048)
  12542. {
  12543. inst->setNextNoteExpression (-0.5f, 25.0f);
  12544. inst->noteOn (67, 0.5f);
  12545. }
  12546. if (s == 6144)
  12547. inst->noteOff (60);
  12548. if (s == 8192)
  12549. inst->noteOff (67);
  12550. inst->render (l.data() + s, r.data() + s, 512, gl, gr);
  12551. }
  12552. const auto Vr = std::sqrt (((double) gl * gl + (double) gr * gr) * 0.5);
  12553. const auto pr = std::atan2 ((double) gr, (double) gl) * (4.0 / 3.141592653589793) - 1.0;
  12554. const auto ref = lynx::threeosc::renderReference (blob, { { 0, 60, Vr * initVol (0.78125), pr, 0.0, 6144 },
  12555. { 2048, 67, Vr * initVol (0.5), std::clamp (-0.5 + pr, -1.0, 1.0), 25.0, 8192 } },
  12556. 44100.0, 16384);
  12557. double worst = 0.0, peak = 0.0;
  12558. for (int i = 0; i < 16384; ++i)
  12559. 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]) }),
  12560. peak = std::max (peak, (double) std::abs (l[(std::size_t) i]));
  12561. CHECK (worst == 0.0 && peak > 0.05);
  12562. // released notes end with the processor's 10 ms fade (volume envelope off) and the instrument goes idle
  12563. CHECK (inst->idle());
  12564. // parameter edits write the raw integers, clamped to the knob ranges
  12565. auto edited = blob;
  12566. CHECK (lynx::threeOscSetParam (edited, 9, -40) && lynx::threeOscParam (edited, 9) == -24);
  12567. CHECK (lynx::threeOscSetParam (edited, 13, 500) && lynx::threeOscParam (edited, 13) == 128);
  12568. CHECK (lynx::threeOscSetParam (edited, 24, 1) && edited[0x5b] == 1);
  12569. CHECK (! lynx::threeOscStateUsable (std::vector<std::uint8_t> (2, 0)));
  12570. }
  12571. TEST (gms_engine)
  12572. {
  12573. const auto blob = gmsTestState();
  12574. dumpIfAsked ("LYNX_DUMP_GMS", blob);
  12575. CHECK (lynx::gmsStateUsable (blob));
  12576. CHECK (lynx::gmsWaveNames (blob) == std::vector<std::string> ({ "NoWave A", "NoWave B", "NoWave C" }));
  12577. CHECK (lynx::gmsParamOffset (blob, 12) == 8 + 4 + 7 * 512 + 12 + 4 * 12 && lynx::gmsParamOffset (blob, 212) < 0);
  12578. auto broken = blob;
  12579. broken[8] = 'X';
  12580. CHECK (! lynx::gmsStateUsable (broken));
  12581. CHECK (! lynx::gmsStateUsable (std::vector<std::uint8_t> (blob.begin(), blob.begin() + 2000)));
  12582. // golden samples of our round-39 model (gmsmodel.GmsPlugin, waves missing → silent tables, tempo 140, 44.1 kHz,
  12583. // host blocks of 512): C5 vel 100 0–30000, E5 vel 80 11025–40000, G5 vel 110 22050–52000 (Init.Vol law A)
  12584. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  12585. const std::vector<lynx::gms::RefNote> notes = { { 0, 60, (float) iv (100), 1.0, 0.0, 0.0, 30000 },
  12586. { 11025, 64, (float) iv (80), 1.0, 0.0, 0.0, 40000 },
  12587. { 22050, 67, (float) iv (110), 1.0, 0.0, 0.0, 52000 } };
  12588. const auto y = lynx::gms::renderReference (blob, notes, 44100.0, 66150, "");
  12589. CHECK (y.size() == 2 * 66150u);
  12590. 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 } };
  12591. for (const auto& g : golden)
  12592. 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);
  12593. double e = 0.0;
  12594. for (auto v : y)
  12595. e += (double) v * v;
  12596. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.0790270452795) < 1e-6);
  12597. // the instrument: same engine behind LYNX's channel gains (V = 1, centre → law A), velocity → Init.Vol
  12598. lynx::ChannelDef c;
  12599. c.instrument = lynx::InstrumentKind::gms;
  12600. c.pluginState = blob;
  12601. auto inst = lynx::makeInstrument (lynx::InstrumentKind::gms);
  12602. inst->configure (c, 140.0);
  12603. inst->prepare (44100.0);
  12604. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  12605. for (int b = 0; b < 16; ++b)
  12606. {
  12607. if (b == 0)
  12608. inst->noteOn (60, 0.78125f);
  12609. if (b == 10)
  12610. inst->noteOff (60);
  12611. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  12612. }
  12613. 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, "");
  12614. double diff = 0.0, energy = 0.0;
  12615. for (int i = 0; i < 8192; ++i)
  12616. {
  12617. 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]) });
  12618. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  12619. }
  12620. CHECK (diff == 0.0 && energy > 1e-3);
  12621. }
  12622. // A Toxic Biohazard state written from scratch (REA_rodada45; r45/py/mktest.py builds the same bytes): our own drawn
  12623. // spectra on operators 1 and 3 (wave 0) and the sine on operator 2, so no wave of the installation is needed
  12624. std::vector<std::uint8_t> toxicTestState()
  12625. {
  12626. auto s = lynx::toxicDefaultState();
  12627. static const std::pair<int, double> set[] = {
  12628. { 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 },
  12629. { 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 },
  12630. { 24, 0.6 }, { 25, 1 }, { 26, 1 }, { 27, 1 }, { 32, 0.5606 }, { 33, 0.4394 }, { 38, 0.53 }, { 49, 0 }, { 55, 0.25 },
  12631. { 62, 0.5 }, { 75, 0.08 }, { 81, 0.5 }, { 87, 0.3 }, { 104, 0.8 }, { 105, 0.6 }, { 110, 0.35 }, { 115, 0.22 },
  12632. { 127, 0.18 }, { 147, 0.7 }, { 227, 1 }, { 229, 0.65 }, { 233, 0.4 }, { 236, 1 }, { 237, 1 }, { 252, 0.5 }, { 254, 0.3 },
  12633. { 256, 0.4 }, { 267, 0.4 }, { 269, 0.5 }, { 277, 0.3 }, { 300, 1 }, { 301, 8 }, { 302, 2 }, { 303, 0 }, { 305, 3 },
  12634. { 306, 0 }, { 307, 1 }, { 308, 0 }, { 314, 1 }, { 315, 0 }, { 316, 1 }, { 324, 106 }, { 332, 0.7 } };
  12635. for (const auto& [index, value] : set)
  12636. {
  12637. const auto v = (float) value;
  12638. std::memcpy (s.data() + lynx::toxicParamOffset (s, index), &v, 4);
  12639. }
  12640. auto spectrum = [&] (int slot, int h, float amp, float phase) {
  12641. const auto at = 12 + 0x40 + 1024 * (std::size_t) slot + 4 * (std::size_t) h;
  12642. std::memcpy (s.data() + at, &amp, 4);
  12643. std::memcpy (s.data() + at + 512, &phase, 4);
  12644. };
  12645. for (int h = 0; h < 24; ++h)
  12646. spectrum (0, h, (float) (1.0 / (h + 1)), 0.0f);
  12647. for (int k = 0; k < 16; ++k)
  12648. spectrum (2, 2 * k, (float) (1.0 / (2 * k + 1)), 0.25f);
  12649. return s;
  12650. }
  12651. TEST (toxic_engine)
  12652. {
  12653. const auto blob = toxicTestState();
  12654. dumpIfAsked ("LYNX_DUMP_TOXIC", blob);
  12655. CHECK (blob.size() == 10050u && lynx::toxicStateUsable (blob) && ! lynx::toxicNeedsDll (blob));
  12656. CHECK (lynx::toxicParamOffset (blob, 15) == 12 + 0x40 + 8192 + 4 * 15 && lynx::toxicParamOffset (blob, 340) < 0);
  12657. CHECK (! lynx::toxicDllUsable ("") && ! lynx::toxicDllUsable ("/nonexistent/Toxic Biohazard_x64.dll"));
  12658. auto broken = blob;
  12659. broken[8] = 'X';
  12660. CHECK (! lynx::toxicStateUsable (broken));
  12661. CHECK (! lynx::toxicStateUsable (std::vector<std::uint8_t> (blob.begin(), blob.begin() + 5000)));
  12662. auto needs = blob;
  12663. const auto w = 15.0f; // a built-in wave of the installation: without the DLL the engine refuses the state
  12664. std::memcpy (needs.data() + lynx::toxicParamOffset (needs, 307), &w, 4);
  12665. CHECK (lynx::toxicNeedsDll (needs) && lynx::toxic::renderReference (needs, { { 0, 60 } }, 44100.0, 64, "").empty());
  12666. // golden samples of our round-45 model (toxmodel.run, which equals the DLL bit for bit on this state), effects on,
  12667. // tempo 140, 44.1 kHz, host blocks of 512: C5 vel 100 0–30000, E5 vel 80 11025–40000, G5 vel 110 22050–52000
  12668. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  12669. const std::vector<lynx::toxic::RefNote> notes = { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 },
  12670. { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  12671. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 } };
  12672. const auto y = lynx::toxic::renderReference (blob, notes, 44100.0, 66150, "", 512, 140.0);
  12673. CHECK (y.size() == 2 * 66150u);
  12674. 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 } };
  12675. for (const auto& g : golden)
  12676. 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);
  12677. double e = 0.0;
  12678. for (auto v : y)
  12679. e += (double) v * v;
  12680. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.307227390984) < 1e-6);
  12681. // the instrument: same engine behind LYNX's channel gains (V = 1, centre → law A), velocity → Init.Vol
  12682. lynx::ChannelDef c;
  12683. c.instrument = lynx::InstrumentKind::toxic;
  12684. c.pluginState = blob;
  12685. auto inst = lynx::makeInstrument (lynx::InstrumentKind::toxic);
  12686. inst->configure (c, 140.0);
  12687. inst->prepare (44100.0);
  12688. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  12689. for (int b = 0; b < 16; ++b)
  12690. {
  12691. if (b == 0)
  12692. inst->noteOn (60, 0.78125f);
  12693. if (b == 10)
  12694. inst->noteOff (60);
  12695. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  12696. }
  12697. 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 } },
  12698. 44100.0, 8192, "", 512, 140.0);
  12699. double diff = 0.0, energy = 0.0;
  12700. for (int i = 0; i < 8192; ++i)
  12701. {
  12702. 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]) });
  12703. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  12704. }
  12705. CHECK (diff == 0.0 && energy > 1e-3);
  12706. }
  12707. TEST (morphine_engine)
  12708. {
  12709. const auto blob = morphineTestState();
  12710. dumpIfAsked ("LYNX_DUMP_MORPHINE", blob);
  12711. CHECK (lynx::morphineStateUsable (blob));
  12712. CHECK (lynx::morphineNoiseName (blob).empty());
  12713. const auto at = lynx::morphineParamOffset (blob, 9);
  12714. CHECK (at > 0);
  12715. float a9 = 0.0f;
  12716. std::memcpy (&a9, blob.data() + at, 4);
  12717. CHECK (a9 == 0.2f && lynx::morphineParamOffset (blob, 40) < 0);
  12718. auto broken = blob;
  12719. broken[12] = 'X'; // the MPR0 tag
  12720. CHECK (! lynx::morphineStateUsable (broken));
  12721. CHECK (! lynx::morphineStateUsable (std::vector<std::uint8_t> (blob.begin(), blob.end() - 10)));
  12722. // golden samples of our round-39 model (mormodel.Synth, tempo 120, 44.1 kHz, host blocks of 512): keys 60/64/67,
  12723. // Init.Vol 1.0/0.5/2.0, note-offs at 30000/50000/70000
  12724. const std::vector<lynx::morphine::RefNote> notes = { { 0, 60, 1.0, 1.0, 0.0, 0.0, 30000 },
  12725. { 11025, 64, 0.5, 1.0, 0.0, 0.0, 50000 },
  12726. { 22050, 67, 2.0, 1.0, 0.0, 0.0, 70000 } };
  12727. const auto y = lynx::morphine::renderReference (blob, notes, 44100.0, 80000, "");
  12728. CHECK (y.size() == 2 * 80000u);
  12729. 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 } };
  12730. for (const auto& g : golden)
  12731. 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);
  12732. double e = 0.0;
  12733. for (auto v : y)
  12734. e += (double) v * v;
  12735. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.18467099041) < 1e-6);
  12736. // the instrument: same engine behind LYNX's channel gains
  12737. lynx::ChannelDef c;
  12738. c.instrument = lynx::InstrumentKind::morphine;
  12739. c.pluginState = blob;
  12740. auto inst = lynx::makeInstrument (lynx::InstrumentKind::morphine);
  12741. inst->configure (c, 120.0);
  12742. inst->prepare (44100.0);
  12743. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  12744. for (int b = 0; b < 16; ++b)
  12745. {
  12746. if (b == 0)
  12747. inst->noteOn (60, 0.78125f);
  12748. if (b == 10)
  12749. inst->noteOff (60);
  12750. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  12751. }
  12752. 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, "");
  12753. double diff = 0.0, energy = 0.0;
  12754. for (int i = 0; i < 8192; ++i)
  12755. {
  12756. 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]) });
  12757. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  12758. }
  12759. CHECK (diff == 0.0 && energy > 1e-3);
  12760. }
  12761. namespace
  12762. {
  12763. // A MiniSynth state written from scratch (REA_rodada41 §1.1 layout: "SSLF" + the .flms preset)
  12764. std::vector<std::uint8_t> miniSynthTestState (const std::array<float, 43>& P)
  12765. {
  12766. const std::string name = "LYNX test";
  12767. Bytes body;
  12768. body.raw ("\x99" "FLS\r\n\x1a\n", 8);
  12769. body.raw ("HEAD", 4);
  12770. body.i32 (6 + 2 * (std::int32_t) name.size());
  12771. const std::uint16_t head[3] = { 1, 1, (std::uint16_t) name.size() };
  12772. body.raw (reinterpret_cast<const char*> (head), 6);
  12773. for (char ch : name)
  12774. body.raw (&ch, 1), body.zeros (1);
  12775. body.raw ("DATA", 4);
  12776. body.i32 (176);
  12777. body.raw ("0rev", 4);
  12778. for (auto v : P)
  12779. body.f32 (v);
  12780. Bytes s;
  12781. s.raw ("SSLF", 4);
  12782. s.i32 ((std::int32_t) body.b.size());
  12783. s.bytes (body.b);
  12784. return s.b;
  12785. }
  12786. std::array<float, 43> miniSynthDefaults()
  12787. {
  12788. 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,
  12789. 0.5f, 0, 0.25f, 0.5f, 0.5f, 0, 0.11222222f, 0.4f, 0.1f, 1, 0.5f };
  12790. }
  12791. // per-process suffix for scratch files: several test binaries (one per worktree) may run at the same time, and a
  12792. // fixed name let one process read a file another one was rewriting
  12793. std::string processTempTag()
  12794. {
  12795. static const std::string tag = std::to_string (std::random_device {}()) + "_"
  12796. + std::to_string (std::chrono::steady_clock::now().time_since_epoch().count());
  12797. static const struct Cleanup
  12798. {
  12799. ~Cleanup()
  12800. {
  12801. // every scratch file or folder whose name carries this process's tag (round 173e: the SoundFont, case,
  12802. // missing-file, Crash169g, Natives142 and Rea167l scratch paths are per process too)
  12803. std::error_code ec;
  12804. std::vector<std::filesystem::path> mine;
  12805. for (const auto& e : std::filesystem::directory_iterator (std::filesystem::temp_directory_path (ec), ec))
  12806. if (e.path().filename().string().find (tag) != std::string::npos)
  12807. mine.push_back (e.path());
  12808. for (const auto& p : mine)
  12809. std::filesystem::remove_all (p, ec);
  12810. }
  12811. } cleanup;
  12812. (void) cleanup;
  12813. return tag;
  12814. }
  12815. // a synthetic oscdata.dat (amplitudes 0.3/k, phases −π/2 + 0.001·(k − 1)): the test does not need an FL installation
  12816. std::string miniSynthTestOscdata()
  12817. {
  12818. const auto path = (std::filesystem::temp_directory_path() / ("lynx_test_oscdata_" + processTempTag() + ".dat")).string();
  12819. std::vector<float> d (2048);
  12820. for (int h = 0; h < 1024; ++h)
  12821. d[(std::size_t) h] = (float) (0.3 / (h + 1)), d[1024 + (std::size_t) h] = (float) (-1.5707963267948966 + 0.001 * h);
  12822. std::ofstream out (path, std::ios::binary);
  12823. out.write (reinterpret_cast<const char*> (d.data()), 8192);
  12824. return path;
  12825. }
  12826. } // namespace
  12827. TEST (minisynth_engine)
  12828. {
  12829. auto A = miniSynthDefaults(), B = miniSynthDefaults();
  12830. // A: poly, SQR+SQR, noise, filter env/velocity/keytracking, LFO → cutoff, distortion + decimator, delay, chorus, phaser
  12831. 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 },
  12832. { 12, 0.1f }, { 13, 0.4f }, { 14, 0.3f }, { 15, 0.5f }, { 16, 0.8f }, { 17, 0.6f }, { 18, 0.1f },
  12833. { 19, 0.5f }, { 20, 0.85f }, { 21, 0.05f }, { 22, 0.5f }, { 23, 0.6f }, { 24, 0.4f }, { 25, 1 },
  12834. { 26, 0.3f }, { 27, 0.1f }, { 28, 1 }, { 29, 0.4f }, { 30, 0.5f }, { 31, 0.6f }, { 32, 0.6f },
  12835. { 33, 1 }, { 34, 0.4f }, { 35, 0.6f }, { 36, 0.5f }, { 37, 1 }, { 38, 0.3f }, { 39, 0.6f },
  12836. { 40, 0.5f }, { 41, 0.7f }, { 42, 0.4f } };
  12837. // B: lead (legato glide), BELL, LFO → pitch
  12838. 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 },
  12839. { 17, 0.1f }, { 18, 0.5f }, { 19, 0.6f }, { 20, 0.7f }, { 22, 0.6f }, { 23, 0.5f }, { 24, 0.3f } };
  12840. for (const auto& [i, v] : a)
  12841. A[(std::size_t) i] = v;
  12842. for (const auto& [i, v] : b)
  12843. B[(std::size_t) i] = v;
  12844. const auto blobA = miniSynthTestState (A), blobB = miniSynthTestState (B);
  12845. dumpIfAsked ("LYNX_DUMP_MINISYNTH", blobA);
  12846. CHECK (lynx::miniSynthStateUsable (blobA) && lynx::miniSynthParams (blobA).size() == 43u && lynx::miniSynthParams (blobA)[26] == 0.3f);
  12847. CHECK (lynx::miniSynthParamOffset (blobA, 9) == 8 + 8 + 8 + 6 + 18 + 12 + 4 * 9 && lynx::miniSynthParamOffset (blobA, 43) < 0);
  12848. CHECK (! lynx::miniSynthStateUsable (std::vector<std::uint8_t> (blobA.begin(), blobA.begin() + 20)));
  12849. auto shortData = blobA;
  12850. shortData.resize (shortData.size() - 4 * 40); // only 3 parameters present: the rest keeps the defaults
  12851. CHECK (lynx::miniSynthParams (shortData)[20] == 0.5f && lynx::miniSynthParams (shortData)[1] == 0.5f);
  12852. // golden samples of our round-41 model (msmodel.Plugin on the same synthetic oscdata, 44.1 kHz, host blocks of 512,
  12853. // L = R = Final.Vol): C5 vel 100 0–30000, E5 vel 80 11025–40000, G5 vel 110 22050–52000 (Init.Vol law A)
  12854. const auto osc = miniSynthTestOscdata();
  12855. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  12856. const std::vector<lynx::minisynth::RefNote> notes = { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 },
  12857. { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  12858. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 } };
  12859. struct Golden { int i; float l, r; };
  12860. 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 } };
  12861. 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 } };
  12862. for (int which = 0; which < 2; ++which)
  12863. {
  12864. const auto y = lynx::minisynth::renderReference (which == 0 ? blobA : blobB, notes, 44100.0, 66150, osc);
  12865. CHECK (y.size() == 2 * 66150u);
  12866. if (y.size() != 2 * 66150u)
  12867. continue;
  12868. for (const auto& g : which == 0 ? std::vector (std::begin (goldenA), std::end (goldenA)) : std::vector (std::begin (goldenB), std::end (goldenB)))
  12869. CHECK (y[2 * (std::size_t) g.i] == g.l && y[2 * (std::size_t) g.i + 1] == g.r);
  12870. double e = 0.0;
  12871. for (auto v : y)
  12872. e += (double) v * v;
  12873. CHECK (std::abs (std::sqrt (e / (double) y.size()) - (which == 0 ? 0.315552434392 : 0.03926116287)) < 1e-9);
  12874. }
  12875. // missing oscdata.dat: silent tables, still finite
  12876. const auto silent = lynx::minisynth::renderReference (blobB, notes, 44100.0, 4096, "");
  12877. CHECK (silent.size() == 2 * 4096u && std::all_of (silent.begin(), silent.end(), [] (float v) { return std::isfinite (v) && std::abs (v) < 1.0f; }));
  12878. // the instrument: same engine behind LYNX's channel gains (V = 1, centre → law A), velocity → Init.Vol
  12879. lynx::ChannelDef c;
  12880. c.instrument = lynx::InstrumentKind::minisynth;
  12881. c.pluginState = blobA;
  12882. c.samplePath = osc;
  12883. auto inst = lynx::makeInstrument (lynx::InstrumentKind::minisynth);
  12884. inst->configure (c, 120.0);
  12885. inst->prepare (44100.0);
  12886. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  12887. for (int k = 0; k < 16; ++k)
  12888. {
  12889. if (k == 0)
  12890. inst->noteOn (60, 0.78125f);
  12891. if (k == 10)
  12892. inst->noteOff (60);
  12893. inst->render (l.data() + 512 * k, r.data() + 512 * k, 512, 1.0f, 1.0f);
  12894. }
  12895. 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 } },
  12896. 44100.0, 8192, osc, 512, 120.0, 0, -1.0, true);
  12897. double diff = 0.0, energy = 0.0;
  12898. for (int i = 0; i < 8192; ++i)
  12899. {
  12900. 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]) });
  12901. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  12902. }
  12903. CHECK (diff == 0.0 && energy > 1e-3 && ! inst->idle());
  12904. }
  12905. namespace
  12906. {
  12907. // A PoiZone state written from scratch (REA_rodada44 §1.3 layout, "Ver4" + 156 floats + name): pulse A synced to saw B,
  12908. // noise, envelope to the cutoff, triangle LFO to oscillator A's pitch, 3-voice unison, gate, chorus and tape delay.
  12909. std::vector<std::uint8_t> poizoneTestState()
  12910. {
  12911. std::array<float, 156> P {};
  12912. 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 },
  12913. { 10, 0.8f }, { 11, 0.3f }, { 12, 0.5f }, { 13, 0.4f }, { 14, 0.5f }, { 15, 0.5f }, { 16, 0.5f }, { 17, 0.5f },
  12914. { 18, 0.35f }, { 19, 0.6f }, { 20, 0.5f }, { 21, 0.3f }, { 22, 0.2f }, { 23, 0.6f }, { 24, 0.7f }, { 25, 0.5f },
  12915. { 26, 0.7f }, { 28, 0.6f }, { 29, 0.3f }, { 30, 0.8f }, { 31, 1.0f }, { 32, 0.3f }, { 33, 0.5f }, { 34, 0.5f },
  12916. { 35, 0.3f }, { 37, 1.0f }, { 38, 0.6f }, { 39, 1.0f }, { 40, 0.4f }, { 41, 0.5f }, { 42, 0.5f }, { 43, 0.5f },
  12917. { 47, 0.5f }, { 48, 0.3f }, { 67, 0.2f }, { 68, 0.5f }, { 75, 2.0f }, { 76, 15.0f }, { 77, 1.0f }, { 78, 1.0f },
  12918. { 79, 1.0f } };
  12919. for (const auto& [i, v] : values)
  12920. P[(std::size_t) i] = v;
  12921. for (int i = 49; i < 65; ++i)
  12922. P[(std::size_t) i] = (i - 49) % 3 != 2 ? 1.0f : 0.0f;
  12923. for (int i = 81; i < 155; ++i)
  12924. P[(std::size_t) i] = -1.0f;
  12925. std::vector<std::uint8_t> blob { 'S', 'S', 'L', 'F', 0xb4, 0x02, 0x00, 0x00, '4', 'r', 'e', 'V' };
  12926. blob.resize (12 + 4 * 156 + 64, 0);
  12927. std::memcpy (blob.data() + 12, P.data(), 4 * 156);
  12928. std::memcpy (blob.data() + 12 + 4 * 156, "LYNX test", 9);
  12929. return blob;
  12930. }
  12931. } // namespace
  12932. TEST (poizone_engine)
  12933. {
  12934. const auto blob = poizoneTestState();
  12935. CHECK (lynx::poizoneStateUsable (blob));
  12936. CHECK (lynx::poizoneParamOffset (blob, 18) == 12 + 4 * 18 && lynx::poizoneParamOffset (blob, 156) < 0);
  12937. CHECK (lynx::poizoneParam (blob, 75) == 2.0f && ! lynx::poizoneParam (blob, -1));
  12938. auto broken = blob;
  12939. broken[8] = 'X'; // the "Ver4" tag
  12940. CHECK (! lynx::poizoneStateUsable (broken));
  12941. CHECK (! lynx::poizoneStateUsable (std::vector<std::uint8_t> (blob.begin(), blob.begin() + 10)));
  12942. // golden samples of our round-44 model (pzmodel.Plugin, tempo 120, 44.1 kHz, host blocks of 512, L = R = Final.Vol):
  12943. // C5 vel 100 0–30000, E5 vel 80 11025–40000, G5 vel 110 22050–52000 (Init.Vol = (11^(vel/100) − 1)/10)
  12944. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  12945. const std::vector<lynx::poizone::RefNote> notes = { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 },
  12946. { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  12947. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 } };
  12948. const auto y = lynx::poizone::renderReference (blob, notes, 44100.0, 66150);
  12949. CHECK (y.size() == 2 * 66150u);
  12950. 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 } };
  12951. for (const auto& g : golden)
  12952. 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);
  12953. double e = 0.0;
  12954. for (auto v : y)
  12955. e += (double) v * v;
  12956. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.10990940878400073) < 1e-7);
  12957. // the instrument: same engine behind LYNX's channel gains (V = 1, centre → law A), velocity → Init.Vol
  12958. lynx::ChannelDef c;
  12959. c.instrument = lynx::InstrumentKind::poizone;
  12960. c.pluginState = blob;
  12961. auto inst = lynx::makeInstrument (lynx::InstrumentKind::poizone);
  12962. inst->configure (c, 120.0);
  12963. inst->prepare (44100.0);
  12964. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  12965. for (int b = 0; b < 16; ++b)
  12966. {
  12967. if (b == 0)
  12968. inst->noteOn (60, 0.78125f);
  12969. if (b == 10)
  12970. inst->noteOff (60);
  12971. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  12972. }
  12973. 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,
  12974. 512, 120.0, 0, true);
  12975. double diff = 0.0, energy = 0.0;
  12976. for (int i = 0; i < 8192; ++i)
  12977. {
  12978. 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]) });
  12979. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  12980. }
  12981. CHECK (diff == 0.0 && energy > 1e-3);
  12982. }
  12983. // ---- Fruit Kick / BassDrum (REA_rodada43): synthetic states and data, golden values of our round-43 models ----
  12984. namespace
  12985. {
  12986. std::vector<std::uint8_t> fruitKickStateA()
  12987. {
  12988. const std::int32_t w[8] = { 0, 0, -600, 64, 128, 32, 20, 0 };
  12989. std::vector<std::uint8_t> b (32);
  12990. std::memcpy (b.data(), w, 32);
  12991. return b;
  12992. }
  12993. std::vector<std::uint8_t> fruitKickStateB()
  12994. {
  12995. std::vector<std::uint8_t> b (400, 0);
  12996. const std::int32_t head[7] = { 1, 624, -8, 42, 86, 64, 0 };
  12997. std::memcpy (b.data(), head, sizeof (head));
  12998. std::int32_t recs[5][17];
  12999. for (auto& r : recs)
  13000. {
  13001. const std::int32_t d[17] = { 0, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, 0 };
  13002. std::memcpy (r, d, sizeof (d));
  13003. }
  13004. const std::int32_t pan[17] = { 0, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 3000, 64, 20000, 1, 0, 0, 0 };
  13005. const std::int32_t vol[17] = { 4, -1, 100, 3000, 100, 25000, 64, 15000, 0, 100, 20000, 0, 32950, 0, 0, 40, -101 };
  13006. std::memcpy (recs[0], pan, sizeof (pan));
  13007. std::memcpy (recs[1], vol, sizeof (vol));
  13008. for (int k = 0; k < 5; ++k)
  13009. std::memcpy (b.data() + 32 + 68 * k, recs[k], 68);
  13010. const std::int32_t cut = 256;
  13011. std::memcpy (b.data() + 32 + 0x15c, &cut, 4);
  13012. return b;
  13013. }
  13014. std::string bassDrumTestFolder()
  13015. {
  13016. // our own oscillator table and a pseudo-random 16-bit noise sample, laid out like FL's BassDrum folder
  13017. const auto dir = std::filesystem::temp_directory_path() / ("lynx_bassdrum_test_" + processTempTag());
  13018. std::filesystem::create_directories (dir / "Noise Samples");
  13019. auto wav = [] (const std::filesystem::path& path, const void* data, std::uint32_t bytes, std::uint16_t fmt, std::uint16_t bits) {
  13020. std::ofstream o (path, std::ios::binary);
  13021. auto u32 = [&] (std::uint32_t v) { o.write ((const char*) &v, 4); };
  13022. auto u16 = [&] (std::uint16_t v) { o.write ((const char*) &v, 2); };
  13023. o.write ("RIFF", 4), u32 (36 + bytes), o.write ("WAVEfmt ", 8), u32 (16), u16 (fmt), u16 (1), u32 (44100),
  13024. u32 (44100u * bits / 8), u16 ((std::uint16_t) (bits / 8)), u16 (bits), o.write ("data", 4), u32 (bytes);
  13025. o.write ((const char*) data, bytes);
  13026. };
  13027. std::vector<float> table (1024);
  13028. for (int i = 0; i < 1024; ++i)
  13029. 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));
  13030. wav (dir / "sounddata", table.data(), 4096, 3, 32);
  13031. std::vector<std::int16_t> noise (3000);
  13032. std::uint32_t s = 12345;
  13033. for (auto& v : noise)
  13034. {
  13035. s = (s * 1103515245u + 12345u) & 0x7fffffffu;
  13036. v = (std::int16_t) ((int) ((s >> 8) % 20001u) - 10000);
  13037. }
  13038. wav (dir / "Noise Samples" / "test noise.wav", noise.data(), 6000, 1, 16);
  13039. return dir.string();
  13040. }
  13041. std::vector<std::uint8_t> bassDrumTestState()
  13042. {
  13043. std::vector<std::uint8_t> b (0x50C + 92, 0);
  13044. std::memcpy (b.data(), "SSLF", 4);
  13045. const std::uint32_t size = 0x560;
  13046. std::memcpy (b.data() + 4, &size, 4);
  13047. std::memcpy (b.data() + 8, "2kiK", 4);
  13048. std::memcpy (b.data() + 12, "Test kick", 9);
  13049. const char path[] = "C:/Somewhere/Noise Samples/test noise.wav";
  13050. std::memcpy (b.data() + 268, path, sizeof (path) - 1);
  13051. 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,
  13052. 0.5f, 0.8f, 0.15f, 0.9f, 0.3f, 0.5f, 0.6f, 0.62f, 0.4f, 0.5f, 0.85f };
  13053. std::memcpy (b.data() + 0x50C, P, sizeof (P));
  13054. return b;
  13055. }
  13056. struct Golden
  13057. {
  13058. int i;
  13059. float l, r;
  13060. };
  13061. bool matchesGolden (const std::vector<float>& y, std::initializer_list<Golden> golden, double rms)
  13062. {
  13063. bool ok = true;
  13064. for (const auto& g : golden)
  13065. 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;
  13066. double e = 0.0;
  13067. for (auto v : y)
  13068. e += (double) v * v;
  13069. return ok && std::abs (std::sqrt (e / (double) y.size()) - rms) < 1e-6;
  13070. }
  13071. } // namespace
  13072. TEST (fruitkick_engine)
  13073. {
  13074. const auto A = fruitKickStateA(), B = fruitKickStateB();
  13075. CHECK (lynx::fruitKickStateUsable (A) && lynx::fruitKickStateUsable (B));
  13076. CHECK (! lynx::fruitKickStateUsable (std::vector<std::uint8_t> (A.begin(), A.begin() + 20)));
  13077. CHECK (lynx::fruitKickParam (A, 1) == -600 && lynx::fruitKickParam (B, 0) == 624);
  13078. CHECK (lynx::fruitKickFallbacks (A).empty() && lynx::fruitKickFallbacks (B).empty());
  13079. // a version-0 state takes the channel's envelope records as its snapshot (and becomes version 1)
  13080. std::vector<std::vector<std::uint8_t>> recs;
  13081. for (int k = 0; k < 5; ++k)
  13082. recs.emplace_back (B.begin() + 32 + 68 * k, B.begin() + 32 + 68 * (k + 1));
  13083. const auto withEnv = lynx::fruitKickWithChannelEnvelope (A, recs, 256, 0, 0);
  13084. CHECK (withEnv.size() == 400 && lynx::fruitKickParam (withEnv, 5) == 20 && std::memcmp (withEnv.data() + 32, B.data() + 32, 364) == 0);
  13085. CHECK (lynx::fruitKickWithChannelEnvelope (B, recs, 256, 0, 0) == B);
  13086. // golden samples of our round-43 model (fkmodel.run, 44.1 kHz, host blocks of 512): C5 at 0 held, C6 at 3000
  13087. // (Final.Vol 0.5, pan 0.3) released at 9000; B with the plugin's tick every 230 samples from 115
  13088. 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 } };
  13089. const auto ya = lynx::fruitkick::renderReference (A, notes, 44100.0, 20000);
  13090. CHECK (ya.size() == 40000u);
  13091. // (A uses the distortion: values since REA_rodada55 with FL's x87 live host waveshaper, checked against FLEngine's own;
  13092. // the release fade since REA_rodada74 with IPP's cosine, checked against the DLL)
  13093. CHECK (matchesGolden (ya, { { 10, 0.803601742f, 0.803601742f }, { 700, 0.185467482f, 0.185467482f }, { 2900, -0.63492614f, -0.63492614f },
  13094. { 3100, 0.339092523f, 0.544403195f }, { 5000, -0.0974622667f, -0.150664076f }, { 9100, 0.124622017f, 0.0544089526f },
  13095. { 9400, -0.146487325f, -0.144867823f }, { 15000, 0.009439799f, 0.009439799f } },
  13096. 0.284267665614));
  13097. std::vector<int> ticks;
  13098. for (int t = 115; t < 20000; t += 230)
  13099. ticks.push_back (t);
  13100. const auto yb = lynx::fruitkick::renderReference (B, notes, 44100.0, 20000, 512, ticks);
  13101. CHECK (matchesGolden (yb, { { 10, -0.00760388654f, -0.00760388654f }, { 700, 0.386696398f, 0.442232877f }, { 2900, -0.123097703f, -0.302172035f },
  13102. { 3100, 0.0523624197f, 0.0877688825f }, { 5000, 0.045177035f, 0.100505695f }, { 9100, -0.00423920294f, 0.00810315367f },
  13103. { 9400, 0.00482427329f, -0.010199016f }, { 15000, -0.000171193431f, 1.01026744e-05f } },
  13104. 0.117964927585));
  13105. // the instrument: same engine behind LYNX's channel gains (V = 1, centre), velocity → Init.Vol (link velocity on)
  13106. lynx::ChannelDef c;
  13107. c.instrument = lynx::InstrumentKind::fruitkick;
  13108. c.pluginState = A;
  13109. auto inst = lynx::makeInstrument (lynx::InstrumentKind::fruitkick);
  13110. inst->configure (c, 140.0);
  13111. inst->prepare (44100.0);
  13112. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  13113. for (int b = 0; b < 16; ++b)
  13114. {
  13115. if (b == 0)
  13116. inst->noteOn (60, 0.78125f);
  13117. if (b == 10)
  13118. inst->noteOff (60);
  13119. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  13120. }
  13121. const auto iv = (std::pow (11.0, 1.28 * 0.78125) - 1.0) / 10.0;
  13122. const auto ref = lynx::fruitkick::renderReference (A, { { 0, 60, iv, 0.0, 0.0, 5120 } }, 44100.0, 8192);
  13123. double diff = 0.0, energy = 0.0;
  13124. for (int i = 0; i < 8192; ++i)
  13125. {
  13126. 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]) });
  13127. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  13128. }
  13129. CHECK (diff < 1e-6 && energy > 1e-2);
  13130. }
  13131. TEST (ipp_cos_and_end_fade)
  13132. {
  13133. // ippsCos_32f_A11 of the installation's dsp_ippv2_x64.dll (REA_rodada74 §1): outputs the DLL gave on its two paths
  13134. const auto bits = [] (float v) { return ippcos::floatToBits (v); };
  13135. const auto f = ippcos::bitsToFloat;
  13136. CHECK (bits (ippcos::cosFma (0.1f)) == 0x3f7eaf8bu && bits (ippcos::cosFma (1.0f)) == 0x3f0a534cu);
  13137. CHECK (bits (ippcos::cosFma (2.0f)) == 0xbed51118u && bits (ippcos::cosFma (-3.0f)) == 0xbf7d6646u);
  13138. CHECK (bits (ippcos::cosFma (f (0x3e000007))) == 0x3f7df6f9u && bits (ippcos::cosNoFma (f (0x3e000007))) == 0x3f7df6f8u);
  13139. CHECK (bits (ippcos::cosFma (f (0x3e00007c))) == 0x3f7df6f4u && bits (ippcos::cosNoFma (f (0x3e00007c))) == 0x3f7df6f5u);
  13140. CHECK (ippcos::cosFma (1e-9f) == 1.0f && ippcos::cosNoFma (0.0f) == 1.0f);
  13141. // the portable fused multiply-add rounds once: (1 + 2^-12)^2 = 1 + 2^-11 + 2^-24 is a float halfway case that a
  13142. // tiny addend (lost in a plain double sum) must decide
  13143. const float a = 1.0f + 0x1p-12f;
  13144. CHECK (ippcos::fusedMulAdd (a, a, 0x1p-60f) == 1.0f + 0x1p-11f + 0x1p-23f);
  13145. CHECK (ippcos::fusedMulAdd (a, a, -0x1p-60f) == 1.0f + 0x1p-11f && ippcos::fusedMulAdd (a, a, 0.0f) == 1.0f + 0x1p-11f);
  13146. CHECK (ippcos::fusedMulAdd (3.0f, 5.0f, -1.0f) == 14.0f);
  13147. // the end-fade table: within 1.5e-4 of the exact cosine, ends near zero
  13148. std::vector<float> t (441);
  13149. ippcos::endFadeTable (t.data(), 441);
  13150. double worst = 0.0;
  13151. for (int i = 0; i < 441; ++i)
  13152. worst = std::max (worst, std::abs ((double) t[(std::size_t) i] - 0.5 * (1.0 + std::cos ((i + 1) * 3.141592653589793 / 442.0))));
  13153. CHECK (worst < 1.5e-4 && t[0] > 0.9999f && t[440] < 1e-4f);
  13154. }
  13155. TEST (bassdrum_engine)
  13156. {
  13157. // FL's CRT powf as the engine computes it: a case where its last bit differs from the correctly rounded power
  13158. const float x = 0.87011606f;
  13159. std::uint32_t bits = 0;
  13160. const auto p = lynx::bassdrum::powfMsvc (x, 0.125f);
  13161. std::memcpy (&bits, &p, 4);
  13162. CHECK (bits == 1065063964u);
  13163. CHECK (lynx::bassdrum::powfMsvc (2.0f, 1.0f) == 2.0f && lynx::bassdrum::powfMsvc (10.0f, 0.0f) == 1.0f);
  13164. const auto st = bassDrumTestState();
  13165. CHECK (lynx::bassDrumStateUsable (st) && lynx::bassDrumNoiseName (st) == "test noise.wav");
  13166. CHECK (lynx::bassDrumParamOffset (st, 6) == 0x50C + 24 && lynx::bassDrumParamOffset (st, 23) < 0);
  13167. auto broken = st;
  13168. broken[8] = 'X';
  13169. CHECK (! lynx::bassDrumStateUsable (broken));
  13170. // golden samples of our round-43 model (bdmodel.run with its powf port, 44.1 kHz, host blocks of 512)
  13171. const auto dir = bassDrumTestFolder();
  13172. 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 },
  13173. { 6100, 55, 1.5, 1.0, -0.3, 0.0, -1 } };
  13174. const auto y = lynx::bassdrum::renderReference (st, notes, 44100.0, 30000, dir);
  13175. CHECK (y.size() == 60000u);
  13176. CHECK (matchesGolden (y, { { 50, 0.0122119058f, 0.0122119058f }, { 400, -0.0886279643f, -0.0886279643f }, { 3000, -0.0428197235f, -0.0428197235f },
  13177. { 6050, 0.0199260078f, 0.0240648985f }, { 6300, -0.257363439f, -0.185516387f }, { 9000, 0.268954486f, 0.10506025f },
  13178. { 15000, -0.0102827176f, -0.0380715057f }, { 25000, 0.0f, 0.0f } },
  13179. 0.230007588169));
  13180. CHECK (lynx::bassdrum::renderReference (st, notes, 44100.0, 100, dir + "/nowhere").empty()); // no table: no engine
  13181. lynx::ChannelDef c;
  13182. c.instrument = lynx::InstrumentKind::bassdrum;
  13183. c.pluginState = st;
  13184. c.samplePath = dir;
  13185. auto inst = lynx::makeInstrument (lynx::InstrumentKind::bassdrum);
  13186. inst->configure (c, 120.0);
  13187. inst->prepare (44100.0);
  13188. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  13189. for (int b = 0; b < 16; ++b)
  13190. {
  13191. if (b == 0)
  13192. inst->noteOn (60, 0.78125f);
  13193. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  13194. }
  13195. 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);
  13196. double diff = 0.0, energy = 0.0;
  13197. for (int i = 0; i < 8192; ++i)
  13198. {
  13199. 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]) });
  13200. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  13201. }
  13202. CHECK (diff == 0.0 && energy > 1e-2);
  13203. }
  13204. // REA_rodada55: Fruity Fast Dist, Fruity Flangus and Fruity Flanger, goldens from the installation's DLLs (EXEC, our own states)
  13205. TEST (fruity_fast_dist_flangus_flanger)
  13206. {
  13207. std::vector<float> inL (4096), inR (4096);
  13208. for (int n = 0; n < 4096; ++n)
  13209. {
  13210. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  13211. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  13212. }
  13213. struct Gold { int i; float l, r; };
  13214. auto close = [] (float a, float b) { return std::abs (a - b) <= x87Tolerance() * std::max (1.0f, std::abs (b)); };
  13215. // Fast Dist (+ FL's host waveshaper): live and render kernels
  13216. auto dist = [&] (lynx::fastdistfx::State s, bool render, std::initializer_list<Gold> gold) {
  13217. std::vector<float> l (inL), r (inR);
  13218. lynx::fastdistfx::process (l.data(), 4096, s, render);
  13219. lynx::fastdistfx::process (r.data(), 4096, s, render);
  13220. for (const auto& g : gold)
  13221. CHECK (close (l[(std::size_t) g.i], g.l) && close (r[(std::size_t) g.i], g.r));
  13222. };
  13223. dist ({ 128, 10, 0, 128, 128 }, false, { { 100, 0.0f, -0.39352417f }, { 1000, -0.39352417f, 0.389404297f }, { 2047, 0.376953125f, -0.39352417f },
  13224. { 3000, -0.39352417f, -0.379760742f }, { 4095, 0.342590332f, 0.373931885f } });
  13225. dist ({ 128, 10, 0, 128, 128 }, true, { { 3000, -0.39352417f, -0.379751563f }, { 4095, 0.342590332f, 0.373931885f } });
  13226. dist ({ 192, 4, 1, 77, 97 }, false, { { 100, 0.0f, -0.868428111f }, { 1000, -1.23000622f, 0.740812242f }, { 2047, 0.58129251f, -0.855666518f },
  13227. { 3000, -1.23000622f, -0.613196433f }, { 4095, 0.307311207f, 0.549388587f } });
  13228. dist ({ 192, 4, 1, 77, 97 }, true, { { 2047, 0.58129245f, -0.855666518f }, { 4095, 0.307311267f, 0.549388528f } });
  13229. dist ({ 90, 7, 0, 40, 120 }, false, { { 100, 0.0f, -0.346225947f }, { 1000, -0.511893928f, 0.287246376f }, { 2047, 0.212910682f, -0.340344131f },
  13230. { 3000, -0.511893928f, -0.227848753f }, { 4095, 0.0910475403f, 0.197926477f } });
  13231. dist ({ 90, 7, 0, 40, 120 }, true, { { 100, 0.0f, -0.346223623f }, { 1000, -0.511893749f, 0.287246734f }, { 2047, 0.212909341f, -0.340341568f },
  13232. { 3000, -0.511893749f, -0.227846757f }, { 4095, 0.0910428166f, 0.197928891f } });
  13233. {
  13234. const auto* T = lynx::fastdistfx::curve (0, 10);
  13235. CHECK (T[0] == 0 && T[8191] == T[8192] && lynx::fastdistfx::curve (1, 1)[8191] == 32767);
  13236. }
  13237. // Flangus: FL's call order (state, sample rate, block size of 512), no flush
  13238. auto flange = [&] (lynx::flangusfx::State s, int fs, std::initializer_list<Gold> gold) {
  13239. lynx::flangusfx::Engine e;
  13240. e.prepare (fs);
  13241. e.load (s);
  13242. e.setRate (fs);
  13243. e.setBlockSize (512);
  13244. std::vector<float> io (8192);
  13245. for (int n = 0; n < 4096; ++n)
  13246. {
  13247. io[2 * (std::size_t) n] = inL[(std::size_t) n];
  13248. io[2 * (std::size_t) n + 1] = inR[(std::size_t) n];
  13249. }
  13250. for (int o = 0; o < 4096; o += 512)
  13251. e.render (io.data() + 2 * o, io.data() + 2 * o, 512);
  13252. for (const auto& g : gold)
  13253. CHECK (close (io[2 * (std::size_t) g.i], g.l) && close (io[2 * (std::size_t) g.i + 1], g.r));
  13254. };
  13255. flange ({ 4, 512, 768, 8, 128, -32, 96, 96 }, 44100, { { 100, 0.0249633491f, -0.119510099f }, { 1000, -1.26988697f, 0.687134743f },
  13256. { 2047, 1.03176403f, -0.278752804f }, { 3000, -0.940228283f, -0.853936791f },
  13257. { 4095, 0.201686561f, 0.607052445f } });
  13258. flange ({ 7, 300, 900, 40, 30, 100, -60, 128 }, 44100, { { 100, 0.0f, 0.0446268953f }, { 1000, 1.68992829f, 2.06982398f },
  13259. { 2047, 0.400214791f, 0.838502884f }, { 3000, 1.20018816f, 1.18875635f },
  13260. { 4095, -0.354708612f, 0.503573656f } });
  13261. flange ({ 2, 1024, 200, 0, 0, -128, 128, -90 }, 48000, { { 100, 0.172592998f, -0.230506495f }, { 1000, -1.56807125f, 0.854493499f },
  13262. { 2047, 1.02325416f, -1.17055321f }, { 3000, -1.74136555f, -0.535462856f },
  13263. { 4095, 1.49345946f, -0.537866116f } });
  13264. // Flanger: state, then the sample rate (FL's call order)
  13265. auto flanger = [&] (lynx::flangerfx::State s, int fs, std::initializer_list<Gold> gold) {
  13266. lynx::flangerfx::Engine e;
  13267. e.load (s);
  13268. e.prepare (fs);
  13269. std::vector<float> io (8192);
  13270. for (int n = 0; n < 4096; ++n)
  13271. {
  13272. io[2 * (std::size_t) n] = inL[(std::size_t) n];
  13273. io[2 * (std::size_t) n + 1] = inR[(std::size_t) n];
  13274. }
  13275. for (int o = 0; o < 4096; o += 512)
  13276. e.render (io.data() + 2 * o, io.data() + 2 * o, 512);
  13277. for (const auto& g : gold)
  13278. CHECK (close (io[2 * (std::size_t) g.i], g.l) && close (io[2 * (std::size_t) g.i + 1], g.r));
  13279. };
  13280. flanger ({ 0, 2000, 2000, 118, 0, 0, 23, 0, 1024, 3500, 3000, 2500 }, 44100,
  13281. { { 100, -1.22955823f, -1.98144293f }, { 1000, -0.975861192f, 0.646231353f }, { 2047, 0.518505156f, -0.857361794f },
  13282. { 3000, -0.546559215f, -0.0157452822f }, { 4095, 0.104127884f, 0.206255436f } });
  13283. flanger ({ 300, 4000, 4500, 90, 1000, 300, 99, 1024, 1024, 2000, 4000, 1000 }, 48000,
  13284. { { 100, 0.0f, -0.440000027f }, { 1000, 0.24619779f, 0.775473535f }, { 2047, 1.11609459f, -0.0639692694f },
  13285. { 3000, -1.22629714f, -1.41118503f }, { 4095, -0.263562649f, 0.80683732f } });
  13286. // import: FL's states become the exact effects with the controls as parameters; short states keep the approximations
  13287. {
  13288. 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 };
  13289. const auto slot = lynx::effectFromFl ("Fruity Fast Dist", fd, 120.0);
  13290. CHECK (slot && slot->kind == lynx::EffectKind::fruityFastDist);
  13291. if (slot)
  13292. {
  13293. const auto s = lynx::fastdistfx::fromParams (slot->params.data());
  13294. CHECK (s.pre == 192 && s.threshold == 4 && s.type == 1 && s.mix == 77 && s.post == 97);
  13295. auto fx = lynx::makeEffect (*slot);
  13296. fx->prepare (44100.0);
  13297. std::vector<float> l (inL), r (inR);
  13298. fx->process (l.data(), r.data(), 4096, slot->params, 120.0);
  13299. CHECK (l[1000] == -1.23000622f && r[4095] == 0.549388587f);
  13300. }
  13301. std::vector<std::uint8_t> fg (32);
  13302. const int v[8] { 7, 300, 900, 40, 30, 100, -60, 128 };
  13303. std::memcpy (fg.data(), v, 32);
  13304. const auto f2 = lynx::effectFromFl ("Fruity Flangus", fg, 120.0);
  13305. CHECK (f2 && f2->kind == lynx::EffectKind::fruityFlangus);
  13306. if (f2)
  13307. {
  13308. const auto s = lynx::flangusfx::fromParams (f2->params.data());
  13309. for (int i = 0; i < 8; ++i)
  13310. CHECK (s[(std::size_t) i] == v[i]);
  13311. auto fx = lynx::makeEffect (*f2);
  13312. fx->prepare (48000.0);
  13313. std::vector<float> l (inL), r (inR);
  13314. fx->process (l.data(), r.data(), 4096, f2->params, 120.0);
  13315. CHECK (std::isfinite (l[4095]) && std::isfinite (r[4095]) && std::abs (l[2000]) > 0.0f);
  13316. }
  13317. std::vector<std::uint8_t> fl (52);
  13318. const int w[13] { 1, 300, 4000, 4500, 90, 1000, 300, 99, 1024, 1024, 2000, 4000, 1000 };
  13319. std::memcpy (fl.data(), w, 52);
  13320. const auto f3 = lynx::effectFromFl ("Fruity Flanger", fl, 120.0);
  13321. CHECK (f3 && f3->kind == lynx::EffectKind::fruityFlanger);
  13322. if (f3)
  13323. {
  13324. const auto s = lynx::flangerfx::fromParams (f3->params.data());
  13325. for (int i = 0; i < 12; ++i)
  13326. CHECK (s[(std::size_t) i] == w[i + 1]);
  13327. auto fx = lynx::makeEffect (*f3);
  13328. fx->prepare (48000.0);
  13329. std::vector<float> l (inL), r (inR);
  13330. fx->process (l.data(), r.data(), 4096, f3->params, 120.0);
  13331. CHECK (l[1000] == 0.24619779f && r[4095] == 0.80683732f);
  13332. }
  13333. const auto shortFd = lynx::effectFromFl ("Fruity Fast Dist", std::vector<std::uint8_t> (8, 0), 120.0);
  13334. CHECK (! shortFd || shortFd->kind != lynx::EffectKind::fruityFastDist);
  13335. }
  13336. }
  13337. // FL's Fruity Phaser (REA_rodada105_phaser): goldens from the installation's own DLL, rendered unmodified in 512-frame
  13338. // calls; LYNX's engine matches it to the last bit on the 27 presets (denormals aside), the tolerance only absorbs
  13339. // compilers (gcc / clang / zig) and libm.
  13340. TEST (fruity_phaser_engine)
  13341. {
  13342. std::vector<float> in (8192);
  13343. for (int n = 0; n < 4096; ++n)
  13344. {
  13345. in[2 * (std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  13346. in[2 * (std::size_t) n + 1] = (float) (((n * 53) % 170 - 85) / 70.0);
  13347. }
  13348. struct Gold
  13349. {
  13350. int i;
  13351. float l, r;
  13352. };
  13353. auto close = [] (float a, float b) { return std::abs (a - b) <= 2.0e-5f * std::max (1.0f, std::abs (b)); };
  13354. auto phase = [&] (lynx::phaserfx::State s, int fs, std::initializer_list<Gold> gold) {
  13355. lynx::phaserfx::Engine e;
  13356. e.setRate (fs);
  13357. e.load (s);
  13358. e.reset();
  13359. std::vector<float> io (in);
  13360. for (int o = 0; o < 4096; o += 512)
  13361. e.render (io.data() + 2 * o, io.data() + 2 * o, 512);
  13362. for (const auto& g : gold)
  13363. CHECK (close (io[2 * (std::size_t) g.i], g.l) && close (io[2 * (std::size_t) g.i + 1], g.r));
  13364. };
  13365. phase ({ 2500, 100, 800, 0, 512, 7, 400, 512, 4000 }, 44100,
  13366. { { 100, 0.243106663f, -1.63760519f }, { 1000, -2.36990547f, 0.841273248f }, { 2047, 1.21970272f, -1.66379488f },
  13367. { 3000, -2.80603957f, -1.03600585f }, { 4095, 1.80225599f, 0.845855355f } });
  13368. phase ({ 417, 995, 1000, 0, 1024, 3, 705, 0, 2943 }, 48000,
  13369. { { 100, -0.360522032f, -0.0510774851f }, { 1000, -0.788047731f, 0.324495822f }, { 2047, 0.304549545f, 0.0850031748f },
  13370. { 3000, -0.79305625f, 0.114482567f }, { 4095, -0.67624253f, -0.0348635167f } });
  13371. phase ({ 2448, 510, 417, 1024, 1024, 16, 508, 0, 2500 }, 44100,
  13372. { { 100, 1.66273713f, -0.0830700323f }, { 1000, -1.47920549f, -0.0849421024f }, { 2047, -0.157817751f, -1.06916845f },
  13373. { 3000, -0.703828216f, -0.0190049056f }, { 4095, 0.768234849f, -0.965515852f } });
  13374. phase ({ 5000, 0, 1000, 700, 0, 22, 950, 300, 5000 }, 44100,
  13375. { { 100, 0.0809119642f, -0.521223426f }, { 1000, 1.07321298f, 0.681761861f }, { 2047, 0.790922821f, -0.205452889f },
  13376. { 3000, -1.91779351f, 0.599423349f }, { 4095, -1.18747222f, 1.03711367f } });
  13377. // controls: FL's ranges, the stage count clamp, the texts
  13378. CHECK (std::abs (lynx::phaserfx::sweepHz (0.5f, false) - 0.238405839f) < 1.0e-6f);
  13379. CHECK (std::abs (lynx::phaserfx::sweepHz (1.0f, true) - 10.0f) < 1.0e-5f);
  13380. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityPhaser, 5, 11.0f / 22.0f, 120.0) == "12");
  13381. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityPhaser, 5, 0.0f, 120.0) == "3");
  13382. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityPhaser, 7, 0.0f, 120.0) == "100% wet");
  13383. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityPhaser, 8, 0.5f, 120.0) == "+0.0 dB");
  13384. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityPhaser, 3, 0.0f, 120.0) == "small");
  13385. {
  13386. lynx::EffectParams ctx {};
  13387. ctx[3] = 1.0f;
  13388. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityPhaser, 0, 1.0f, 120.0, ctx) == "10.000 Hz");
  13389. }
  13390. CHECK (lynx::effectInfo (lynx::EffectKind::fruityPhaser).params.size() == 9);
  13391. // import: FL's state (version 1 + nine controls) becomes the exact effect; its parameters round-trip
  13392. const lynx::phaserfx::State ouch { 417, 995, 1000, 0, 1024, 3, 705, 0, 2943 };
  13393. const auto bytes = lynx::phaserfx::serialize (ouch);
  13394. const auto slot = lynx::effectFromFl ("Fruity Phaser", bytes, 120.0);
  13395. CHECK (slot && slot->kind == lynx::EffectKind::fruityPhaser);
  13396. if (slot)
  13397. {
  13398. CHECK (lynx::phaserfx::fromParams (slot->params.data()) == ouch);
  13399. auto fx = lynx::makeEffect (*slot);
  13400. fx->prepare (48000.0);
  13401. std::vector<float> l (4096), r (4096);
  13402. for (int n = 0; n < 4096; ++n)
  13403. {
  13404. l[(std::size_t) n] = in[2 * (std::size_t) n];
  13405. r[(std::size_t) n] = in[2 * (std::size_t) n + 1];
  13406. }
  13407. fx->process (l.data(), r.data(), 4096, slot->params, 120.0);
  13408. CHECK (close (l[1000], -0.788047731f) && close (r[4095], -0.0348635167f));
  13409. }
  13410. auto v2 = bytes;
  13411. v2[0] = 2; // FL reads only versions below 2
  13412. const auto later = lynx::effectFromFl ("Fruity Phaser", v2, 120.0);
  13413. CHECK (! later || later->kind != lynx::EffectKind::fruityPhaser);
  13414. CHECK (lynx::plugui::hasFlLayout (lynx::EffectKind::fruityPhaser));
  13415. }
  13416. // Workflow P2 (docs/WORKFLOW_FL.md)
  13417. #include "WorkflowP2Tests.inc"
  13418. // Editor mouse grammar (Piano Roll / Playlist)
  13419. #include "EditorGestureTests.inc"
  13420. TEST (chorus_family_engines)
  13421. {
  13422. // REA_rodada59: goldens from the installation's DLLs (44.1 kHz, blocks of 512) on this signal
  13423. std::vector<float> inL (4096), inR (4096);
  13424. for (int n = 0; n < 4096; ++n)
  13425. {
  13426. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0) * 0.5f;
  13427. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0) * 0.5f;
  13428. }
  13429. struct Gold { int i; float l, r; };
  13430. auto close = [] (float a, float b) { return std::abs (a - b) <= x87Tolerance() * std::max (1.0f, std::abs (b)); };
  13431. auto run = [&] (const char* plugin, const std::vector<std::uint8_t>& st, lynx::EffectKind kind, std::initializer_list<Gold> gold) {
  13432. const auto slot = lynx::effectFromFl (plugin, st, 120.0);
  13433. CHECK (slot && slot->kind == kind);
  13434. if (! slot)
  13435. return;
  13436. auto fx = lynx::makeEffect (*slot);
  13437. fx->prepare (44100.0);
  13438. std::vector<float> l (inL), r (inR);
  13439. fx->process (l.data(), r.data(), 1024, slot->params, 120.0);
  13440. fx->process (l.data() + 1024, r.data() + 1024, 3072, slot->params, 120.0);
  13441. for (const auto& g : gold)
  13442. CHECK (close (l[(std::size_t) g.i], g.l) && close (r[(std::size_t) g.i], g.r));
  13443. };
  13444. auto ints = [] (std::initializer_list<int> v) {
  13445. std::vector<std::uint8_t> b;
  13446. for (int x : v)
  13447. for (int k = 0; k < 4; ++k)
  13448. b.push_back ((std::uint8_t) ((unsigned) x >> (8 * k)));
  13449. return b;
  13450. };
  13451. // Fruity Chorus (start phases 0, which FL draws at random)
  13452. run ("Fruity Chorus", ints ({ 1, 512, 2250, 341, 1500, 2500, 3500, 23, 0, 0, 0, 2500, 0 }), lynx::EffectKind::fruityChorus,
  13453. { { 100, 0.0f, -0.3850000202655792f }, { 1000, -0.6181581020355225f, 0.2707735002040863f }, { 2047, -0.01424609124660492f, 0.24228274822235107f },
  13454. { 3000, -1.1650776863098145f, -0.1809898018836975f }, { 4095, -0.012790083885192871f, 0.060571759939193726f } });
  13455. run ("Fruity Chorus", ints ({ 1, 700, 4000, 900, 4000, 3000, 2000, 400, 700, 1000, 30, 4999, 0 }), lynx::EffectKind::fruityChorus,
  13456. { { 100, 0.0f, -0.3850000202655792f }, { 1000, -0.5833333134651184f, 0.36345374584198f }, { 2047, -0.0336097776889801f, -0.3360772430896759f },
  13457. { 3000, -0.40196430683135986f, -0.5073210597038269f }, { 4095, -0.1176539734005928f, 0.444706529378891f } });
  13458. run ("Fruity Chorus", ints ({ 1, 36, 469, 1004, 2832, 293, 3500, 0, 428, 0, 0, 3887, 1024 }), lynx::EffectKind::fruityChorus,
  13459. { { 100, 0.2975943684577942f, 0.3747217655181885f }, { 1000, -0.39463338255882263f, 0.18099871277809143f }, { 2047, 0.3894335627555847f, 0.35111793875694275f },
  13460. { 3000, -0.1450725942850113f, 0.2847272455692291f }, { 4095, 0.33678582310676575f, -0.13365517556667328f } });
  13461. {
  13462. const auto slot = lynx::effectFromFl ("Fruity Chorus", ints ({ 1, 36, 469, 1004, 2832, 293, 3500, 0, 428, 0, 0, 3887, 1024 }), 120.0);
  13463. CHECK (slot && lynx::fruitychorusfx::fromParams (slot->params.data())[2] == 1004);
  13464. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityChorus, 11, 1.0f, 120.0) == "on");
  13465. CHECK (lynx::effectParamFromFl ("Fruity Chorus", 11).has_value() && ! lynx::effectParamFromFl ("Fruity Chorus", 12).has_value());
  13466. }
  13467. // a state FL does not read (version 2) or too short keeps the generic chorus
  13468. CHECK (! lynx::effectFromFl ("Fruity Chorus", ints ({ 2, 512, 2250, 341, 1500, 2500, 3500, 23, 0, 0, 0, 2500, 0 }), 120.0).has_value()
  13469. || lynx::effectFromFl ("Fruity Chorus", ints ({ 2, 512, 2250, 341, 1500, 2500, 3500, 23, 0, 0, 0, 2500, 0 }), 120.0)->kind != lynx::EffectKind::fruityChorus);
  13470. CHECK (! lynx::effectFromFl ("Fruity Chorus", { 1, 0, 0, 0 }, 120.0).has_value()
  13471. || lynx::effectFromFl ("Fruity Chorus", { 1, 0, 0, 0 }, 120.0)->kind != lynx::EffectKind::fruityChorus);
  13472. // Hyper Chorus engine in FL's load sequence (defaults, then the state: the parameters glide), instance k's phases
  13473. auto hyper = [&] (std::initializer_list<int> raws, int instance, std::initializer_list<Gold> gold) {
  13474. namespace hc = lynx::hyperchorusfx;
  13475. hc::Engine e (instance);
  13476. const std::vector<int> r (raws);
  13477. 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); };
  13478. for (int i = 0; i < hc::params; ++i)
  13479. e.setParam (i, val (i, hc::defaults[(std::size_t) i]));
  13480. for (int i = 0; i < hc::params; ++i)
  13481. e.setParam (i, val (i, r[(std::size_t) i]));
  13482. e.setRate (44100.0);
  13483. std::vector<float> io (8192);
  13484. for (int n = 0; n < 4096; ++n)
  13485. io[2 * (std::size_t) n] = inL[(std::size_t) n], io[2 * (std::size_t) n + 1] = inR[(std::size_t) n];
  13486. for (int s = 0; s < 4096; s += 512)
  13487. e.render (io.data() + 2 * s, io.data() + 2 * s, 512);
  13488. for (const auto& g : gold)
  13489. CHECK (close (io[2 * (std::size_t) g.i], g.l) && close (io[2 * (std::size_t) g.i + 1], g.r));
  13490. };
  13491. hyper ({ 562, 919, 499, 505, 709, 716, 1, 19247, 10900, 191, 1000 }, 0,
  13492. { { 100, 0.0f, -0.4115639328956604f }, { 1000, -0.658551037311554f, 0.28647756576538086f }, { 2047, 0.24743860960006714f, -0.5088979005813599f },
  13493. { 3000, -0.5171564221382141f, -0.3630276620388031f }, { 4095, 0.08128821104764938f, 0.18811003863811493f } });
  13494. hyper ({ 1000, 555, 0, 400, 300, 300, 0, 22000, 120, 368, 750 }, 2,
  13495. { { 100, 0.0f, -0.398922324180603f }, { 1000, -1.1528236865997314f, 0.2839684784412384f }, { 2047, 0.5115746855735779f, -0.3330395221710205f },
  13496. { 3000, -0.6094785928726196f, 0.17040175199508667f }, { 4095, -0.11666109412908554f, 0.21931695938110352f } });
  13497. CHECK (lynx::hyperchorusfx::randState (1) == 1u * 214013u + 2531011u);
  13498. {
  13499. // the import: controls as parameters; a song's Hyper Chorus slots are numbered in mixer order
  13500. const auto st = ints ({ 2, 1000, 555, 0, 400, 300, 300, 0, 22000, 120, 368, 750, 0 });
  13501. const auto slot = lynx::effectFromFl ("Hyper Chorus", st, 120.0);
  13502. CHECK (slot && slot->kind == lynx::EffectKind::hyperChorus && lynx::hyperchorusfx::fromParams (slot->params.data())[9] == 368);
  13503. lynx::Song song;
  13504. song.mixer.resize (3);
  13505. song.mixer[1].effects = { *slot, *slot };
  13506. song.mixer[2].effects = { *slot };
  13507. lynx::numberChorusInstances (song);
  13508. CHECK (lynx::hyperchorusfx::instanceOf (song.mixer[2].effects[0].state) == 2);
  13509. auto fx = lynx::makeEffect (song.mixer[2].effects[0]);
  13510. fx->prepare (48000.0);
  13511. std::vector<float> l (inL), r (inR);
  13512. fx->process (l.data(), r.data(), 4096, slot->params, 120.0);
  13513. bool finite = true, wet = false;
  13514. for (int n = 0; n < 4096; ++n)
  13515. {
  13516. finite = finite && std::isfinite (l[(std::size_t) n]) && std::isfinite (r[(std::size_t) n]);
  13517. wet = wet || l[(std::size_t) n] != inL[(std::size_t) n];
  13518. }
  13519. CHECK (finite && wet);
  13520. CHECK (lynx::formatEffectParam (lynx::EffectKind::hyperChorus, 8, 0.0f, 120.0) == "off");
  13521. CHECK (! lynx::effectFromFl ("Hyper Chorus", ints ({ 3, 1000, 555, 0, 400, 300, 300, 0, 22000, 120, 368, 750, 0 }), 120.0).has_value()
  13522. || lynx::effectFromFl ("Hyper Chorus", ints ({ 3, 1000, 555, 0, 400, 300, 300, 0, 22000, 120, 368, 750, 0 }), 120.0)->kind != lynx::EffectKind::hyperChorus);
  13523. }
  13524. // Vintage Chorus engine as FL drives it (created, sample rate, each control), fresh instance (its smoothed values
  13525. // start from the engine's initial ones)
  13526. auto vintage = [&] (std::initializer_list<int> raws, std::initializer_list<Gold> gold) {
  13527. namespace vc = lynx::vintagechorusfx;
  13528. vc::Engine e;
  13529. e.setRate (44100.0);
  13530. int i = 0;
  13531. for (int raw : raws)
  13532. {
  13533. 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));
  13534. ++i;
  13535. }
  13536. std::vector<float> io (8192);
  13537. for (int n = 0; n < 4096; ++n)
  13538. io[2 * (std::size_t) n] = inL[(std::size_t) n], io[2 * (std::size_t) n + 1] = inR[(std::size_t) n];
  13539. for (int s = 0; s < 4096; s += 512)
  13540. e.render (io.data() + 2 * s, io.data() + 2 * s, 512);
  13541. for (const auto& g : gold)
  13542. CHECK (close (io[2 * (std::size_t) g.i], g.l) && close (io[2 * (std::size_t) g.i + 1], g.r));
  13543. };
  13544. vintage ({ 2, 0, 211046, 1, 2411, 960, 1985, 5121, 0, 1, 4420, 6000, 273, 0, 0, 0, 0 },
  13545. { { 100, 0.0f, -0.05606573447585106f }, { 1000, -0.3413679003715515f, 0.20469821989536285f }, { 2047, 0.1487489938735962f, -0.28199973702430725f },
  13546. { 3000, -0.31699514389038086f, -0.43230289220809937f }, { 4095, 0.1255197525024414f, 0.0810704305768013f } });
  13547. vintage ({ 2, 0, 80, 0, 0, 60, 3000, 3000, 0, 0, 0, 3000, 0, 3000, 0, 0, 0 }, // hiss
  13548. { { 100, -1.2527654007499223e-06f, -0.053655341267585754f }, { 1000, -0.44306400418281555f, 0.25606146454811096f }, { 2047, 0.23425261676311493f, -0.21375307440757751f },
  13549. { 3000, -0.5793180465698242f, -0.13371579349040985f }, { 4095, 0.05595953390002251f, 0.11362095922231674f } });
  13550. vintage ({ 0, 1, 49152, 0, 3000, 60, 3000, 3000, 0, 0, 0, 3000, 0, 0, 0, 0, 0 }, // Chorus 1 + 2
  13551. { { 100, 0.0f, -0.05365874245762825f }, { 1000, -0.5095958113670349f, 0.2809423506259918f }, { 2047, 0.17256303131580353f, -0.3563305139541626f },
  13552. { 3000, -0.471851646900177f, -0.30123069882392883f }, { 4095, 0.0959278866648674f, 0.13436323404312134f } });
  13553. {
  13554. const auto st = ints ({ 1, 2, 0, 211046, 1, 2411, 960, 1985, 5121, 0, 1, 4420, 6000, 273, 0, 0, 0, 0 });
  13555. const auto slot = lynx::effectFromFl ("Vintage Chorus", st, 120.0);
  13556. CHECK (slot && slot->kind == lynx::EffectKind::vintageChorus && lynx::vintagechorusfx::fromParams (slot->params.data())[2] == 211046);
  13557. auto fx = lynx::makeEffect (*slot);
  13558. fx->prepare (44100.0);
  13559. std::vector<float> l (inL), r (inR);
  13560. fx->process (l.data(), r.data(), 4096, slot->params, 120.0);
  13561. bool finite = true;
  13562. for (int n = 0; n < 4096; ++n)
  13563. finite = finite && std::isfinite (l[(std::size_t) n]) && std::isfinite (r[(std::size_t) n]);
  13564. CHECK (finite && l[3000] != inL[3000]);
  13565. CHECK (lynx::formatEffectParam (lynx::EffectKind::vintageChorus, 0, 0.0f, 120.0) == "Chorus 1");
  13566. 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()
  13567. || 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);
  13568. }
  13569. }
  13570. #include "UiChromeTests.inc"
  13571. TEST (fruity_delay_engines)
  13572. {
  13573. // REA_rodada52: the engines on their own against the installation's DLLs (goldens = the DLL's output for the same
  13574. // input with FL's set-up order, EXEC: bit-identical), then the mixer effects built from FL states
  13575. const int n = 16384;
  13576. std::vector<float> inL ((std::size_t) n, 0.0f), inR ((std::size_t) n, 0.0f);
  13577. std::uint32_t seed = 1;
  13578. for (int i = 0; i < 4096; ++i) // a noise burst, then the echoes
  13579. {
  13580. seed = seed * 1664525u + 1013904223u;
  13581. inL[(std::size_t) i] = (float) ((int) (seed >> 16) - 32768) / 32768.0f * 0.5f;
  13582. seed = seed * 1664525u + 1013904223u;
  13583. inR[(std::size_t) i] = (float) ((int) (seed >> 16) - 32768) / 32768.0f * 0.5f;
  13584. }
  13585. using Golden = std::vector<std::tuple<int, float, float>>;
  13586. auto check = [&] (const std::vector<float>& l, const std::vector<float>& r, const Golden& golden) {
  13587. for (const auto& [i, gl, gr] : golden)
  13588. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  13589. };
  13590. auto run3 = [&] (const std::array<int, 26>& raw, double bpm) {
  13591. lynx::fldelay::Delay3Engine e (44100.0);
  13592. e.setTempo (bpm);
  13593. for (int i = 0; i < 26; ++i)
  13594. e.setParameter (i, lynx::fldelay::delay3EngineValue (i, raw[(std::size_t) i]));
  13595. e.setSampleRate (44100.0); // FL's wrapper sets the rate up again after the state
  13596. e.setSampleRate (44100.0);
  13597. std::vector<float> l (inL), r (inR);
  13598. for (int s = 0; s < n; s += 512)
  13599. e.process (l.data() + s, r.data() + s, 512);
  13600. return std::make_pair (l, r);
  13601. };
  13602. auto run2 = [&] (const std::array<int, 8>& raw, double bpm) {
  13603. lynx::fldelay::Delay2State st;
  13604. st.v = raw;
  13605. lynx::fldelay::Delay2Engine e (st, 44100.0, bpm, 512);
  13606. std::vector<float> l (inL), r (inR);
  13607. for (int s = 0; s < n; s += 512)
  13608. e.process (l.data() + s, r.data() + s, 512);
  13609. return std::make_pair (l, r);
  13610. };
  13611. // Delay 3, true stereo, synced 1/8 beat, cutoff + resonance (Fruity Delay 3_x64.dll at 120 and 100 BPM)
  13612. 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 };
  13613. auto o = run3 (a, 120.0);
  13614. check (o.first, o.second, { { 3000, 0.399949551f, 0.598533869f }, { 6000, 0.116703831f, -0.315510482f }, { 9000, -0.150520369f, -0.250620633f },
  13615. { 13000, 0.193608373f, 0.0598564595f }, { 16000, -0.0524701327f, 0.022601109f } });
  13616. o = run3 (a, 100.0);
  13617. check (o.first, o.second, { { 5000, 0.377422422f, 0.346570343f }, { 6000, -0.182619616f, 0.0488029197f }, { 11000, -0.00120426295f, 0.0253051519f },
  13618. { 16000, 0.0806550607f, -0.0991330743f } });
  13619. // ping pong, keep pitch, modulation, cutoff LFO, diffusion, saturation, rate and bit reduction, colour
  13620. 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);
  13621. check (o.first, o.second, { { 1500, 0.288540125f, -0.148862943f }, { 5000, 5.90502168e-06f, 0.111382999f }, { 8000, -0.0736631379f, -0.0305093247f },
  13622. { 11000, -0.00831047911f, 0.0102323527f }, { 16000, 0.0175791476f, 0.0273917634f } });
  13623. // fake stereo, unsynced, high-pass, limit distortion, no dry
  13624. 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);
  13625. check (o.first, o.second, { { 1500, 0.0825810209f, 0.0228505582f }, { 6000, -0.0427959524f, -0.0408911295f }, { 9000, 0.0114790015f, 0.019227922f },
  13626. { 16000, -0.000312746735f, 0.00101647642f } });
  13627. // delay type "Off": filter, distortion and the bit/rate reduction on the dry path
  13628. 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);
  13629. check (o.first, o.second, { { 1500, 0.238686681f, -0.106820084f }, { 3000, 0.131528616f, 0.0665975735f }, { 5000, 9.94473215e-09f, -8.22819057e-09f },
  13630. { 11000, 0.0f, 0.0f } });
  13631. // Delay 2: default, ping pong with a stereo offset, swapped without dry (Fruity Delay 2_x64.dll)
  13632. o = run2 ({ 0, 128, 128, 64, 36, 0, 0, 128 }, 120.0);
  13633. check (o.first, o.second, { { 3000, 0.211380005f, 0.277908325f }, { 5000, -0.223615795f, -0.0189359933f }, { 9000, 0.120054141f, -0.00297877868f },
  13634. { 16000, 0.0182533264f, 0.105384827f } });
  13635. o = run2 ({ -40, 110, 90, 90, 24, -300, 2, 90 }, 100.0);
  13636. check (o.first, o.second, { { 3000, 0.0929639712f, 0.122222826f }, { 5000, -0.0884733275f, -0.0507739782f }, { 11000, 0.0589251742f, 0.0f },
  13637. { 16000, -0.029524127f, 0.0128910914f } });
  13638. o = run2 ({ 60, 140, 0, 100, 30, 250, 1, 110 }, 120.0);
  13639. check (o.first, o.second, { { 1500, 0.0f, 0.0f }, { 5000, 0.176379532f, -0.212506086f }, { 8000, -0.324207425f, 0.0675852001f },
  13640. { 13000, 0.145023212f, -0.104788139f } });
  13641. // the mixer effects from FL states: time law (Delay 3 synced 192 = 1 beat; Delay 2 144 = 3/4 beat), settled start
  13642. auto state3 = std::vector<std::uint8_t> (4 + 26 * 4, 0);
  13643. 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 };
  13644. state3[0] = 1;
  13645. std::memcpy (state3.data() + 4, def3.data(), 26 * 4);
  13646. const auto slot3 = lynx::effectFromFl ("Fruity Delay 3", state3, 120.0);
  13647. CHECK (slot3 && slot3->kind == lynx::EffectKind::fruityDelay3);
  13648. auto state2 = std::vector<std::uint8_t> (32, 0);
  13649. const std::array<int, 8> def2 { 0, 128, 128, 0, 144, 0, 0, 128 };
  13650. std::memcpy (state2.data(), def2.data(), 32);
  13651. const auto slot2 = lynx::effectFromFl ("Fruity delay 2", state2, 120.0);
  13652. CHECK (slot2 && slot2->kind == lynx::EffectKind::fruityDelay2);
  13653. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityDelay2, 4, slot2->params[4], 120.0) == "3:00");
  13654. auto echoAt = [] (const lynx::EffectSlot& slot, double bpm) {
  13655. auto fx = lynx::makeEffect (slot);
  13656. fx->prepare (44100.0);
  13657. std::vector<float> l (48000, 0.0f), r (48000, 0.0f);
  13658. l[100] = 1.0f;
  13659. for (int s = 0; s < 48000; s += 1024)
  13660. fx->process (l.data() + s, r.data() + s, std::min (1024, 48000 - s), slot.params, bpm);
  13661. int best = 200;
  13662. for (int i = 200; i < 48000; ++i)
  13663. if (std::abs (l[(std::size_t) i]) > std::abs (l[(std::size_t) best]))
  13664. best = i;
  13665. return best - 100;
  13666. };
  13667. if (slot3 && slot2)
  13668. {
  13669. CHECK (echoAt (*slot3, 120.0) == 22050);
  13670. CHECK (echoAt (*slot3, 150.0) == 17640);
  13671. CHECK (echoAt (*slot2, 120.0) == 16538);
  13672. CHECK (echoAt (*slot2, 140.0) == 14175);
  13673. }
  13674. }
  13675. #include "UiRackMixerTests.inc"
  13676. // ---------------------------------------------------------------------------
  13677. // FL Studio integration (core/FlPluginDb.h): synthetic plugin-database fixtures, no Image-Line files
  13678. namespace
  13679. {
  13680. std::vector<std::uint8_t> testWrapperBlob (std::uint32_t type, const std::string& name, const std::string& path,
  13681. const std::vector<std::uint8_t>& classId = {}, const std::string& clapId = {})
  13682. {
  13683. std::vector<std::uint8_t> b;
  13684. auto u32 = [&b] (std::uint32_t v) { for (int i = 0; i < 4; ++i) b.push_back ((std::uint8_t) (v >> (8 * i))); };
  13685. auto record = [&] (std::uint32_t id, const std::vector<std::uint8_t>& payload) {
  13686. u32 (id);
  13687. u32 ((std::uint32_t) payload.size());
  13688. u32 (0);
  13689. b.insert (b.end(), payload.begin(), payload.end());
  13690. };
  13691. u32 (12);
  13692. record (0x32, { (std::uint8_t) type, 0, 0, 0 });
  13693. if (! classId.empty())
  13694. record (0x34, classId);
  13695. if (! clapId.empty())
  13696. record (0x3A, std::vector<std::uint8_t> (clapId.begin(), clapId.end()));
  13697. record (0x36, std::vector<std::uint8_t> (name.begin(), name.end()));
  13698. record (0x37, std::vector<std::uint8_t> (path.begin(), path.end()));
  13699. return b;
  13700. }
  13701. } // namespace
  13702. TEST (fl_plugin_db_nfo_parsing)
  13703. {
  13704. const std::string nfo = "ps_name=Vital\r\n"
  13705. "ps_file_name_0=C:\\Program Files\\Common Files\\VST3\\Vital.vst3\r\n"
  13706. "ps_file_vendorname_0=Vital Audio\r\n"
  13707. "ps_file_type_0=2\r\n"
  13708. "ps_file_name_1=C:\\VSTPlugins\\Vital_x64.dll\r\n"
  13709. "ps_file_type_1=2\r\n"
  13710. "ps_file_magic_1=Vita\r\n"
  13711. "ps_file_name_2=not a plugin\r\n"
  13712. "garbage line without equals\r\n";
  13713. const auto entries = lynx::parseFlPluginNfo (nfo, "D:\\FL\\Presets\\Plugin database\\Installed\\Generators\\VST3\\Vital.nfo");
  13714. CHECK (entries.size() == 2);
  13715. if (entries.size() == 2)
  13716. {
  13717. CHECK (entries[0].name == "Vital" && entries[0].vendor == "Vital Audio" && entries[0].role == 2);
  13718. CHECK (entries[0].format == lynx::ForeignPlugin::Format::vst3);
  13719. CHECK (entries[1].format == lynx::ForeignPlugin::Format::vst2 && entries[1].vst2Id == 0x56697461u);
  13720. }
  13721. // no type field: the role comes from the Effects / Generators folder; an ID in GUID form is read
  13722. const auto fx = lynx::parseFlPluginNfo ("ps_name=Fancy Comp\nps_file_filename_0=C:/CLAP/Fancy Comp.clap\n"
  13723. "ps_file_guid_0={01234567-89AB-CDEF-0123-456789ABCDEF}\n",
  13724. "C:\\x\\Installed\\Effects\\CLAP\\Fancy Comp.nfo");
  13725. CHECK (fx.size() == 1 && fx[0].role == 1 && fx[0].format == lynx::ForeignPlugin::Format::clap && fx[0].hasClassId
  13726. && fx[0].classId[0] == 0x01 && fx[0].classId[15] == 0xEF);
  13727. CHECK (lynx::parseFlPluginNfo ("Tip=All of the plugins found in your system.").empty());
  13728. CHECK (lynx::pluginNameKey ("Serum_x64") == "serum");
  13729. CHECK (lynx::pluginNameKey ("Vital.VST3") == "vital");
  13730. CHECK (lynx::pluginNameKey ("Pro-Q 3") == "proq3");
  13731. CHECK (lynx::pluginFormatFromPath ("a\\b.CLAP") == lynx::ForeignPlugin::Format::clap);
  13732. }
  13733. TEST (fl_plugin_db_fst_parsing_and_matching)
  13734. {
  13735. std::vector<std::uint8_t> cid;
  13736. for (int i = 0; i < 16; ++i)
  13737. cid.push_back ((std::uint8_t) (0x10 + i));
  13738. // an .fst of the database: an FL preset whose 0xD5 holds the wrapper record
  13739. FlpBuilder fst;
  13740. fst.ascii (0xC9, "Fruity Wrapper");
  13741. fst.data (0xD5, testWrapperBlob (8, "Surge XT", "E:\\Plugins\\VST3\\Surge XT.vst3", cid));
  13742. lynx::FlPluginDbEntry e;
  13743. CHECK (lynx::parseFlPluginFst (fst.bytes, e));
  13744. CHECK (e.name == "Surge XT" && e.format == lynx::ForeignPlugin::Format::vst3 && e.hasClassId && e.classId[0] == 0x10);
  13745. lynx::FlPluginDbEntry bad;
  13746. CHECK (! lynx::parseFlPluginFst (std::vector<std::uint8_t> { 'F', 'L', 'h', 'd' }, bad));
  13747. lynx::FlPluginDb db;
  13748. db.add (e);
  13749. // the .nfo of the same plugin fills in vendor and role
  13750. for (const auto& n : lynx::parseFlPluginNfo ("ps_name=Surge XT\nps_file_name_0=E:/Plugins/VST3/Surge XT.vst3\n"
  13751. "ps_file_vendorname_0=Surge Synth Team\nps_file_type_0=2\n"))
  13752. db.add (n);
  13753. db.add ({ "Clapper", "", "E:\\Plugins\\CLAP\\Clapper.clap", "", lynx::ForeignPlugin::Format::clap, 1, {}, false, "org.clapper", 0 });
  13754. db.add ({ "Big Verb", "", "E:\\Old VST\\BigVerb_x64.dll", "", lynx::ForeignPlugin::Format::vst2, 1, {}, false, "", 0x42566262u });
  13755. db.add ({ "Bundle", "", "F:\\VST3\\Bundle.vst3\\Contents\\x86_64-win\\Bundle.vst3", "", lynx::ForeignPlugin::Format::vst3, 2, {}, false, "", 0 });
  13756. CHECK (db.entries().size() == 4);
  13757. CHECK (db.entries()[0].vendor == "Surge Synth Team" && db.entries()[0].role == 2);
  13758. // by VST3 class ID even when the project saved another path and name
  13759. lynx::ForeignPlugin p;
  13760. p.format = lynx::ForeignPlugin::Format::vst3;
  13761. p.hasClassId = true;
  13762. std::copy (cid.begin(), cid.end(), p.classId.begin());
  13763. p.name = "Renamed";
  13764. p.path = "C:\\Other PC\\Surge.vst3";
  13765. CHECK (db.match (p, true) == &db.entries()[0]);
  13766. // by CLAP id, by VST2 id
  13767. lynx::ForeignPlugin c;
  13768. c.format = lynx::ForeignPlugin::Format::clap;
  13769. c.clapId = "org.clapper";
  13770. CHECK (db.match (c, false) == &db.entries()[1]);
  13771. lynx::ForeignPlugin v;
  13772. v.format = lynx::ForeignPlugin::Format::vst2;
  13773. v.vst2Id = 0x42566262u;
  13774. CHECK (db.match (v, false) == &db.entries()[2]);
  13775. // by name (architecture suffix ignored), only in the same role
  13776. lynx::ForeignPlugin byName;
  13777. byName.format = lynx::ForeignPlugin::Format::vst2;
  13778. byName.name = "BigVerb x64";
  13779. CHECK (db.match (byName, false) == &db.entries()[2]);
  13780. CHECK (db.match (byName, true) == nullptr);
  13781. lynx::ForeignPlugin unknown;
  13782. unknown.format = lynx::ForeignPlugin::Format::vst3;
  13783. unknown.name = "Nothing Like It";
  13784. CHECK (db.match (unknown, true) == nullptr);
  13785. // folders: the bundle's parent, deduplicated
  13786. const auto folders = db.pluginFolders();
  13787. CHECK (folders.size() == 4);
  13788. CHECK (std::find (folders.begin(), folders.end(), "F:\\VST3") != folders.end());
  13789. CHECK (std::find (folders.begin(), folders.end(), "E:\\Plugins\\VST3") != folders.end());
  13790. // targeted scan picks: same file, or same name key
  13791. CHECK (lynx::pluginFileMatches ("E:/Plugins/VST3/Surge XT.vst3", p, &db.entries()[0]));
  13792. CHECK (lynx::pluginFileMatches ("C:\\Program Files\\Common Files\\VST3\\BigVerb.vst3", byName, nullptr));
  13793. CHECK (! lynx::pluginFileMatches ("C:\\VST3\\Other.vst3", byName, nullptr));
  13794. // the plugins an .flp uses: wrapper channels and mixer slots
  13795. flp::Project project;
  13796. flp::Channel ch;
  13797. ch.pluginName = "Fruity Wrapper";
  13798. ch.pluginState = testWrapperBlob (8, "Surge XT", "C:\\VST3\\Surge XT.vst3", cid);
  13799. project.channels.push_back (ch);
  13800. ch.pluginName = "3x Osc";
  13801. project.channels.push_back (ch);
  13802. flp::MixerInsert insert;
  13803. insert.slots.push_back ({ 0, "Fruity Wrapper", testWrapperBlob (12, "Clapper", "C:\\CLAP\\Clapper.clap", {}, "org.clapper") });
  13804. insert.slots.push_back ({ 1, "Fruity Wrapper", testWrapperBlob (3, "DX thing", "C:\\dx.dll") }); // DirectX: not hosted
  13805. project.mixer.push_back (insert);
  13806. const auto uses = lynx::flpForeignPlugins (project);
  13807. CHECK (uses.size() == 2);
  13808. CHECK (uses.size() == 2 && uses[0].instrument && ! uses[1].instrument && uses[1].plugin.clapId == "org.clapper");
  13809. }
  13810. TEST (fl_sample_relocation)
  13811. {
  13812. std::set<std::string> files { "D:\\Docs\\Image-Line\\FL Studio\\Audio\\Recorded\\take.wav", "C:\\Program Files\\FL 2026\\Data\\Patches\\kick.wav",
  13813. "C:\\Users\\me\\Music\\loop.wav", "E:\\Songs\\Track\\Samples\\snare.wav", "E:\\Songs\\Track\\hat.wav" };
  13814. auto exists = [&] (const std::string& p) { return files.count (p) > 0; };
  13815. lynx::FlPathRoots roots;
  13816. roots.userData = "D:\\Docs\\Image-Line\\FL Studio";
  13817. roots.factoryData = "C:\\Program Files\\FL 2026";
  13818. roots.home = "C:\\Users\\me";
  13819. const std::string project = "E:\\Songs\\Track";
  13820. // variable resolved, file in place
  13821. CHECK (lynx::locateFlSample ("%FLStudioFactoryData%\\Data\\Patches\\kick.wav", roots, project, exists) == "C:\\Program Files\\FL 2026\\Data\\Patches\\kick.wav");
  13822. // another PC's FL user data / FL install / user profile
  13823. CHECK (lynx::locateFlSample ("C:\\Users\\bob\\Documents\\Image-Line\\FL Studio\\Audio\\Recorded\\take.wav", roots, project, exists)
  13824. == "D:\\Docs\\Image-Line\\FL Studio\\Audio\\Recorded\\take.wav");
  13825. CHECK (lynx::locateFlSample ("C:\\Program Files (x86)\\Image-Line\\FL Studio 20\\Data\\Patches\\kick.wav", roots, project, exists)
  13826. == "C:\\Program Files\\FL 2026\\Data\\Patches\\kick.wav");
  13827. CHECK (lynx::locateFlSample ("C:/Users/bob/Music/loop.wav", roots, project, exists) == "C:\\Users\\me\\Music\\loop.wav");
  13828. // next to the project, also in a sub-folder named in the original path
  13829. CHECK (lynx::locateFlSample ("Z:\\Old\\Samples\\snare.wav", roots, project, exists) == "E:\\Songs\\Track\\Samples\\snare.wav");
  13830. CHECK (lynx::locateFlSample ("Z:\\Old\\Stuff\\hat.wav", roots, project, exists) == "E:\\Songs\\Track\\hat.wav");
  13831. CHECK (lynx::locateFlSample ("Z:\\nowhere.wav", roots, project, exists).empty());
  13832. CHECK (lynx::locateFlSample ("", roots, project, exists).empty());
  13833. }
  13834. // --- REA round 64: SimSynth (exact engine) and Wasp / Wasp XT (FL 2026's silent placeholder) ---------------------
  13835. namespace
  13836. {
  13837. // a SimSynth state written from scratch (REA_rodada64 §1.1 layout: 45 doubles, 19 unused ints, 19 switches, 12 unused
  13838. // ints, version)
  13839. std::vector<std::uint8_t> simSynthTestState (const std::array<double, 45>& P, const std::array<int, 19>& S, int version = 14)
  13840. {
  13841. std::vector<std::uint8_t> b (564, 0);
  13842. std::memcpy (b.data(), P.data(), 360);
  13843. std::memcpy (b.data() + 360 + 76, S.data(), 76);
  13844. std::memcpy (b.data() + 560, &version, 4);
  13845. return b;
  13846. }
  13847. // the 0xD5 records of an FL preset file (.fst: the FLhd/FLdt event stream) that follow a plugin name `plugin`
  13848. std::vector<std::vector<std::uint8_t>> pluginStatesIn (const std::filesystem::path& file, const std::string& plugin)
  13849. {
  13850. std::vector<std::vector<std::uint8_t>> out;
  13851. std::ifstream in (file, std::ios::binary);
  13852. const std::vector<std::uint8_t> b ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  13853. std::size_t i = 0;
  13854. while (i + 8 <= b.size() && std::memcmp (b.data() + i, "FLdt", 4) != 0)
  13855. ++i;
  13856. i += 8;
  13857. std::string name;
  13858. while (i < b.size())
  13859. {
  13860. const auto id = b[i++];
  13861. std::size_t n = id < 64 ? 1 : id < 128 ? 2 : id < 192 ? 4 : 0;
  13862. if (id >= 192)
  13863. for (int shift = 0; i < b.size(); shift += 7)
  13864. {
  13865. const auto c = b[i++];
  13866. n |= (std::size_t) (c & 0x7F) << shift;
  13867. if (c < 0x80)
  13868. break;
  13869. }
  13870. if (i + n > b.size())
  13871. break;
  13872. if (id == 0xC9)
  13873. {
  13874. name.clear();
  13875. for (std::size_t k = 0; k < n; ++k)
  13876. if (b[i + k] != 0)
  13877. name += (char) b[i + k]; // ASCII or UTF-16LE (zero bytes skipped)
  13878. }
  13879. if (id == 0xD5 && name == plugin)
  13880. out.emplace_back (b.begin() + (std::ptrdiff_t) i, b.begin() + (std::ptrdiff_t) (i + n));
  13881. i += n;
  13882. }
  13883. return out;
  13884. }
  13885. } // namespace
  13886. TEST (simsynth_engine)
  13887. {
  13888. // A: pulse + octave layer, half-cosine with unison and two octave layers, sine/sine⁴ mix, square LFO → pitch with
  13889. // delay, filter envelope, LP/BP mix, chorus. B (version 7): triangle ring-modulated by filtered noise, sample &
  13890. // hold LFO → cutoff, retrigger, own filter envelope, chorus off
  13891. 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,
  13892. 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 };
  13893. 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 };
  13894. 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,
  13895. 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 };
  13896. 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 };
  13897. const auto blobA = simSynthTestState (A, SA), blobB = simSynthTestState (B, SB, 7);
  13898. dumpIfAsked ("LYNX_DUMP_SIMSYNTH", blobA);
  13899. CHECK (lynx::simSynthStateUsable (blobA) && ! lynx::simSynthStateUsable (std::vector<std::uint8_t> (blobA.begin(), blobA.begin() + 563)));
  13900. const auto hp = lynx::simSynthHostParams (blobA);
  13901. // knobs go through ⌊65535·v⌋, the coarse pitches through 48 steps, the switches and wave selectors as they are
  13902. CHECK (hp.size() == 58u && hp[0] == 52428 && hp[27] == 10080 && hp[41] == 1 && hp[54] == 0 && hp[56] == 4);
  13903. // golden samples of the installation's SimSynth_x64.dll (round-64 EXEC harness: created, state restored, 44.1 kHz,
  13904. // host blocks of 512, old voice record) — LYNX's engine renders bit-identically: C5 0–30000, E5 Final.Vol 0.8 pan
  13905. // 16 11025–40000, G5 pan −20 +25 cents 22050–52000
  13906. const std::vector<lynx::simsynth::RefNote> notes = { { 0, 60, 1.0f, 0, 0, 0.0f, 0.0f, 30000 },
  13907. { 11025, 64, 0.8f, 16, 0, 0.0f, 0.0f, 40000 },
  13908. { 22050, 67, 1.0f, -20, 25, 0.0f, 0.0f, 52000 } };
  13909. struct Golden { int i; float l, r; };
  13910. 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 } };
  13911. 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 } };
  13912. for (int which = 0; which < 2; ++which)
  13913. {
  13914. std::vector<std::int64_t> counters;
  13915. const auto y = lynx::simsynth::renderReference (which == 0 ? blobA : blobB, notes, 44100.0, 66150, 512, &counters);
  13916. CHECK (y.size() == 2 * 66150u);
  13917. if (y.size() != 2 * 66150u)
  13918. continue;
  13919. // the random-table index after creation / state / rate set equals the DLL's (276 / 696 / 756 and 276 / 492 / 528)
  13920. CHECK (counters.size() == 4u && counters[2] == 276 && counters[3] == (which == 0 ? 696 : 492) && counters[0] == (which == 0 ? 756 : 528));
  13921. for (const auto& g : which == 0 ? std::vector (std::begin (goldenA), std::end (goldenA)) : std::vector (std::begin (goldenB), std::end (goldenB)))
  13922. CHECK (y[2 * (std::size_t) g.i] == g.l && y[2 * (std::size_t) g.i + 1] == g.r);
  13923. double e = 0.0;
  13924. for (auto v : y)
  13925. e += (double) v * v;
  13926. CHECK (std::abs (std::sqrt (e / (double) y.size()) - (which == 0 ? 0.290665331870 : 0.992116840643)) < 1e-9);
  13927. }
  13928. // the instrument: same engine behind LYNX's channel gains (V = 1, centre → Final.Vol 1, Final.Pan 0)
  13929. lynx::ChannelDef c;
  13930. c.instrument = lynx::InstrumentKind::simsynth;
  13931. c.pluginState = blobA;
  13932. auto inst = lynx::makeInstrument (lynx::InstrumentKind::simsynth);
  13933. inst->prepare (44100.0);
  13934. inst->configure (c, 120.0);
  13935. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  13936. for (int k = 0; k < 16; ++k)
  13937. {
  13938. if (k == 0)
  13939. inst->noteOn (60, 0.8f);
  13940. if (k == 10)
  13941. inst->noteOff (60);
  13942. inst->render (l.data() + 512 * k, r.data() + 512 * k, 512, 1.0f, 1.0f);
  13943. }
  13944. const auto ref = lynx::simsynth::renderReference (blobA, { { 0, 60, 1.0f, 0, 0, 0.0f, 0.0f, 5120 } }, 44100.0, 8192);
  13945. double diff = 0.0, energy = 0.0;
  13946. for (int i = 0; i < 8192; ++i)
  13947. {
  13948. 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]) });
  13949. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  13950. }
  13951. CHECK (diff == 0.0 && energy > 1e-3);
  13952. // the installation's SimSynth presets (when the FL data folder is given): every state usable and sounding
  13953. if (! realFlpDir.empty())
  13954. {
  13955. const auto folder = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "SimSynth";
  13956. int presets = 0, usable = 0, sounding = 0;
  13957. if (std::filesystem::exists (folder))
  13958. for (const auto& entry : std::filesystem::directory_iterator (folder))
  13959. for (const auto& st : pluginStatesIn (entry.path(), "SimSynth"))
  13960. {
  13961. ++presets;
  13962. if (! lynx::simSynthStateUsable (st))
  13963. continue;
  13964. ++usable;
  13965. const auto y = lynx::simsynth::renderReference (st, { { 0, 48, 1.0f, 0, 0, 0.0f, 0.0f, 11025 } }, 44100.0, 22050);
  13966. double e = 0.0;
  13967. bool finite = true;
  13968. for (auto v : y)
  13969. finite = finite && std::isfinite (v), e += (double) v * v;
  13970. CHECK (finite);
  13971. sounding += e > 1e-9 ? 1 : 0;
  13972. }
  13973. std::printf (" SimSynth presets: %d, %d usable, %d sounding\n", presets, usable, sounding);
  13974. CHECK (presets == 0 || (usable == presets && presets >= 70 && sounding >= presets - 2));
  13975. }
  13976. }
  13977. TEST (wasp_is_a_silent_placeholder)
  13978. {
  13979. // FL 2026's Wasp / Wasp XT DLL keeps the state and plays nothing: so does LYNX (REA_rodada64 §5)
  13980. auto inst = lynx::makeInstrument (lynx::InstrumentKind::wasp);
  13981. inst->prepare (44100.0);
  13982. std::vector<float> l (1024, 0.0f), r (1024, 0.0f);
  13983. inst->noteOn (60, 1.0f);
  13984. inst->render (l.data(), r.data(), 1024, 1.0f, 1.0f);
  13985. CHECK (inst->idle() && std::all_of (l.begin(), l.end(), [] (float v) { return v == 0.0f; }));
  13986. // import: a SimSynth channel plays on the exact engine, Wasp and Wasp XT channels become the silent placeholder with
  13987. // their state kept
  13988. std::vector<std::uint8_t> ss (564, 0), wasp (169, 1);
  13989. const int version = 14;
  13990. std::memcpy (ss.data() + 560, &version, 4);
  13991. FlpBuilder b;
  13992. b.ascii (0xC7, "25.2.5.5319");
  13993. int index = 0;
  13994. for (const auto& [name, state] : { std::pair<std::u16string, std::vector<std::uint8_t>> { u"SimSynth", ss }, { u"Wasp", wasp }, { u"Wasp XT", wasp } })
  13995. {
  13996. b.u8 (0x40); b.u16 ((std::uint16_t) index++);
  13997. b.u8 (0x15); b.u8 (2);
  13998. b.utf16 (0xC9, name);
  13999. b.data (0xD5, state);
  14000. }
  14001. const auto song = lynx::importFlp (flp::parseProject (b.bytes));
  14002. CHECK (song.channels.size() == 3u);
  14003. if (song.channels.size() == 3u)
  14004. {
  14005. CHECK (song.channels[0].instrument == lynx::InstrumentKind::simsynth && song.channels[0].pluginState == ss);
  14006. CHECK (song.channels[1].instrument == lynx::InstrumentKind::wasp && song.channels[1].pluginState == wasp);
  14007. CHECK (song.channels[2].instrument == lynx::InstrumentKind::wasp);
  14008. }
  14009. }
  14010. // --- REA round 67: Fruity Granulizer (exact engine) --------------------------------------------------------------
  14011. namespace
  14012. {
  14013. // our own deterministic test sample (a fading 220 Hz sine plus LCG noise per channel), as r67/py/gold67.py builds it
  14014. lynx::SampleData granulizerTestSample (int n, int ch)
  14015. {
  14016. lynx::SampleData d;
  14017. std::uint32_t s = 12345;
  14018. for (int i = 0; i < n; ++i)
  14019. {
  14020. const auto base = 0.5 * std::sin (2.0 * 3.141592653589793 * 220.0 * i / 44100.0) * (1.0 - 0.7 * i / n);
  14021. for (int c = 0; c < ch; ++c)
  14022. {
  14023. s = s * 1664525u + 1013904223u;
  14024. const auto v = (float) (base + 0.2 * ((double) (std::int32_t) s * (1.0 / 2147483648.0)));
  14025. (c == 0 ? d.left : d.right).push_back (v);
  14026. }
  14027. }
  14028. return d;
  14029. }
  14030. std::vector<std::uint8_t> granulizerTestState (const std::array<int, 12>& P, int keyMode = 0)
  14031. {
  14032. std::vector<std::uint8_t> b (60, 0);
  14033. const std::int32_t v = 5;
  14034. std::memcpy (b.data(), &v, 4);
  14035. std::memcpy (b.data() + 4, P.data(), 48);
  14036. std::memcpy (b.data() + 56, &keyMode, 4);
  14037. return b;
  14038. }
  14039. } // namespace
  14040. TEST (granulizer_engine)
  14041. {
  14042. // REA_rodada67: states written from scratch against our reference model (granmodel.py, which renders like the
  14043. // installation's DLL within −128 dB: the DLL's rcpps and IPP sines are not replicated)
  14044. const auto v5 = granulizerTestState ({ 204, 50, 611, 611, 0, 180, 0, 0, 0, 0, 0, 228 });
  14045. CHECK (lynx::granulizerStateUsable (v5));
  14046. const auto n5 = lynx::granulizerNormalisedState (v5, {});
  14047. CHECK (n5.size() == 428 && n5[0] == 7 && lynx::granulizerParam (n5, 0) == 204 && lynx::granulizerParam (n5, 11) == 228
  14048. && ! lynx::granulizerParam (n5, 12));
  14049. // version 1 (10 parameters): parameter 11 takes the plugin's 0xf8
  14050. {
  14051. std::vector<std::uint8_t> v1 (v5.begin(), v5.begin() + 44);
  14052. v1[0] = 1;
  14053. const auto n1 = lynx::granulizerNormalisedState (v1, {});
  14054. CHECK (lynx::granulizerParam (n1, 9) == 0 && lynx::granulizerParam (n1, 11) == 0xf8 && lynx::granulizerParam (n1, 3) == 611);
  14055. std::vector<std::uint8_t> bad (v5);
  14056. bad[0] = 9;
  14057. CHECK (! lynx::granulizerStateUsable (bad) && lynx::granulizerNormalisedState (bad, {}).empty());
  14058. auto edited = n5;
  14059. CHECK (lynx::granulizerSetParam (edited, 3, -5000) && lynx::granulizerParam (edited, 3) == -1000);
  14060. }
  14061. using N = lynx::granulizer::RefNote;
  14062. struct Golden { std::vector<std::uint8_t> state; int ch, fs, rate; lynx::granulizer::RefOptions opt; std::vector<N> notes;
  14063. double el, er; std::tuple<int, float, float> at[6]; };
  14064. auto check = [] (const Golden& g) {
  14065. auto smp = granulizerTestSample (20000, g.ch);
  14066. smp.sampleRate = g.rate;
  14067. const auto y = lynx::granulizer::renderReference (lynx::granulizerNormalisedState (g.state, {}), smp, g.notes, (double) g.fs, 16384, g.opt);
  14068. CHECK (y.size() == 2 * 16384);
  14069. if (y.size() != 2 * 16384)
  14070. return;
  14071. double el = 0.0, er = 0.0;
  14072. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  14073. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  14074. CHECK (std::abs (el - g.el) <= 1e-6 * g.el && std::abs (er - g.er) <= 1e-6 * g.er);
  14075. for (const auto& [i, l, r] : g.at)
  14076. CHECK (goldenSampleClose (y[2 * (std::size_t) i], l, granulizerTolerance())
  14077. && goldenSampleClose (y[2 * (std::size_t) i + 1], r, granulizerTolerance()));
  14078. };
  14079. lynx::granulizer::RefOptions o1, o2, o3;
  14080. o1.points = o2.points = o3.points = 12; // the plugin's own default quality (these goldens predate FL's render value)
  14081. o2.block = 333, o2.seed = 777;
  14082. o3.block = 64, o3.seed = 5;
  14083. // the plugin's defaults, two notes (one pitched by +23 cents and released: the processor's end fade)
  14084. check ({ v5, 2, 44100, 44100, o1, { { 0, 60, 0.8, 0.0, 0.0, -1 }, { 3000, 64, 0.6, -0.3, 23.0, 9000 } }, 553.4541810458171, 470.3837027429263,
  14085. { { 100, 0.057010360062122345f, 0.08960580080747604f }, { 1000, 0.07210570573806763f, -0.048257164657115936f },
  14086. { 2500, 0.14769265055656433f, -0.0194698516279459f }, { 4000, 0.11169229447841644f, 0.17487481236457825f },
  14087. { 7777, -0.4309714436531067f, -0.17027156054973602f }, { 12000, -0.11129666864871979f, -0.19622492790222168f } } });
  14088. // backwards, randomness, depth/speed wobble, stereo separation, loop, sample start; a mono 44.1 kHz sample at 48 kHz
  14089. check ({ granulizerTestState ({ 150, 120, 300, -480, 200, 90, 120, 40, 1, 0, 900, 300 }), 1, 48000, 44100, o2,
  14090. { { 0, 55, 1.0, 0.2, -37.0, 12000 }, { 5000, 67, 0.5, 0.0, 0.0, -1 } }, 959.5011214226065, 1294.4823684530272,
  14091. { { 100, 0.02567935362458229f, 0.010721667669713497f }, { 1000, 0.3007732629776001f, 0.08214527368545532f },
  14092. { 2500, 0.435413658618927f, 0.09068503230810165f }, { 4000, -0.24611327052116394f, -0.20149046182632446f },
  14093. { 7777, -0.008478119969367981f, 0.31221628189086914f }, { 12000, 0.203709632f, 0.324944794f } } });
  14094. // key mode 1 (the sample spread over two octaves), freeze, hard stereo separation; 48 kHz sample at 96 kHz, 64-sample blocks
  14095. check ({ granulizerTestState ({ 300, 0, 800, 700, 0, 180, 0, 64, 0, 1, 2000, 100 }, 1), 2, 96000, 48000, o3,
  14096. { { 0, 62, 0.9, 0.0, 0.0, -1 }, { 4000, 50, 0.7, 0.0, 11.0, -1 } }, 366.79556669810813, 0.00825590391720353,
  14097. { { 100, 0.004204070195555687f, 7.680895437786243e-20f }, { 1000, 0.008516596630215645f, 3.0263170437258543e-19f },
  14098. { 2500, 0.06948182731866837f, 4.5391312775082004e-18f }, { 4000, -0.01754589192569256f, -4.895453905423713e-19f },
  14099. { 7777, 0.10338862985372543f, 2.344363859234514e-18f }, { 12000, -0.09385982155799866f, -1.3493021437162966e-17f } } });
  14100. // the instrument: silent without a sample (FL's plugin refuses the note), sounding with one
  14101. {
  14102. auto silent = lynx::makeInstrument (lynx::InstrumentKind::granulizer);
  14103. silent->prepare (44100.0);
  14104. std::vector<float> l (2048, 0.0f), r (2048, 0.0f);
  14105. silent->noteOn (60, 1.0f);
  14106. silent->render (l.data(), r.data(), 2048, 1.0f, 1.0f);
  14107. CHECK (silent->idle() && std::all_of (l.begin(), l.end(), [] (float v) { return v == 0.0f; }));
  14108. auto smp = std::make_shared<lynx::SampleData> (granulizerTestSample (20000, 2));
  14109. auto inst = lynx::makeInstrument (lynx::InstrumentKind::granulizer, smp);
  14110. lynx::ChannelDef def;
  14111. def.instrument = lynx::InstrumentKind::granulizer;
  14112. def.pluginState = n5;
  14113. inst->configure (def, 140.0);
  14114. inst->prepare (44100.0);
  14115. inst->noteOn (60, 0.8f);
  14116. inst->render (l.data(), r.data(), 2048, 1.0f, 1.0f);
  14117. double e = 0.0;
  14118. for (auto v : l)
  14119. e += (double) v * v;
  14120. CHECK (! inst->idle() && e > 1.0);
  14121. inst->noteOff (60);
  14122. for (int k = 0; k < 4; ++k)
  14123. inst->render (l.data(), r.data(), 512, 1.0f, 1.0f);
  14124. CHECK (inst->idle()); // the 10 ms end fade of the plugin's voice processor
  14125. }
  14126. // tempo-synced grains follow the song position in LYNX's 960-PPQ ticks: an instance fed setSongPosition every
  14127. // block while playing must stay in phase with one that free-runs its own tick clock from 0 (it was 10x off)
  14128. {
  14129. auto smp = std::make_shared<lynx::SampleData> (granulizerTestSample (20000, 2));
  14130. lynx::ChannelDef def;
  14131. def.instrument = lynx::InstrumentKind::granulizer;
  14132. def.pluginState = n5;
  14133. auto fed = lynx::makeInstrument (lynx::InstrumentKind::granulizer, smp);
  14134. auto free = lynx::makeInstrument (lynx::InstrumentKind::granulizer, smp);
  14135. for (auto* g : { fed.get(), free.get() })
  14136. {
  14137. g->configure (def, 140.0);
  14138. g->prepare (44100.0);
  14139. g->noteOn (60, 0.8f);
  14140. }
  14141. const auto tps = 140.0 * lynx::ppq / (60.0 * 44100.0);
  14142. std::vector<float> l1 (300), r1 (300), l2 (300), r2 (300);
  14143. double diff = 0.0, energy = 0.0;
  14144. for (int block = 0, done = 0; block < 60; ++block, done += 300)
  14145. {
  14146. fed->setSongPosition ((double) done * tps);
  14147. std::fill (l1.begin(), l1.end(), 0.0f), std::fill (r1.begin(), r1.end(), 0.0f);
  14148. std::fill (l2.begin(), l2.end(), 0.0f), std::fill (r2.begin(), r2.end(), 0.0f);
  14149. fed->render (l1.data(), r1.data(), 300, 1.0f, 1.0f);
  14150. free->render (l2.data(), r2.data(), 300, 1.0f, 1.0f);
  14151. for (int j = 0; j < 300; ++j)
  14152. diff = std::max (diff, (double) std::fabs (l1[(std::size_t) j] - l2[(std::size_t) j])),
  14153. energy += (double) l2[(std::size_t) j] * l2[(std::size_t) j];
  14154. }
  14155. CHECK (energy > 1.0 && diff < 1e-4);
  14156. }
  14157. // import: a Fruity Granulizer channel plays on the exact engine with its sample
  14158. {
  14159. FlpBuilder b;
  14160. b.ascii (0xC7, "25.2.5.5319");
  14161. b.u8 (0x40); b.u16 (0);
  14162. b.u8 (0x15); b.u8 (2);
  14163. b.utf16 (0xC9, u"Fruity Granulizer");
  14164. b.data (0xD5, v5);
  14165. b.utf16 (0xC4, u"%FLStudioFactoryData%\\Data\\Patches\\Packs\\Drums\\Kicks\\808 Kick.wav");
  14166. lynx::FlpImportOptions options;
  14167. options.roots.factoryData = "C:\\FL";
  14168. options.fileExists = [] (const std::string& path) { return path == "C:\\FL\\Data\\Patches\\Packs\\Drums\\Kicks\\808 Kick.wav"; };
  14169. lynx::FlpImportReport report;
  14170. options.report = &report;
  14171. const auto song = lynx::importFlp (flp::parseProject (b.bytes), options);
  14172. CHECK (song.channels.size() == 1u);
  14173. if (song.channels.size() == 1u)
  14174. {
  14175. CHECK (song.channels[0].instrument == lynx::InstrumentKind::granulizer && song.channels[0].pluginState == n5);
  14176. CHECK (song.channels[0].samplePath == "C:\\FL\\Data\\Patches\\Packs\\Drums\\Kicks\\808 Kick.wav");
  14177. }
  14178. CHECK (report.converted["Fruity Granulizer"] == 1);
  14179. }
  14180. }
  14181. // --- REA round 67: Drumaxx (exact engine) -------------------------------------------------------------------------
  14182. namespace
  14183. {
  14184. // our own deterministic 16-pad kit (LCG-chosen parameters), as r67/py/gold67d.py builds it for the DLL
  14185. std::vector<std::uint8_t> drumaxxTestState (bool eq)
  14186. {
  14187. std::uint32_t s = 20261015u;
  14188. auto u = [&s] {
  14189. s = s * 1664525u + 1013904223u;
  14190. return (float) ((double) (s >> 8) * (1.0 / 16777216.0));
  14191. };
  14192. // (a product rounded on its own: no fused multiply-add)
  14193. auto mul = [] (float a, float b) {
  14194. volatile float p = a * b;
  14195. return (float) p;
  14196. };
  14197. static const int targets[] = { 0, 9, 10, 16, 21, 23, 26 };
  14198. std::array<float, 720> P {};
  14199. for (int k = 0; k < 16; ++k)
  14200. for (int i = 0; i < 44; ++i)
  14201. {
  14202. auto v = u();
  14203. if (i == 0)
  14204. v = 0.5f + mul (v, 0.5f);
  14205. else if ((i >= 7 && i <= 18) || (i >= 20 && i <= 34))
  14206. v = 0.2f + mul (v, 0.6f);
  14207. else if (i == 2 || i == 3)
  14208. v = 0.0f;
  14209. else if (i == 4)
  14210. v = (float) (36 + k);
  14211. else if (i == 5)
  14212. v = v < 0.5f ? 0.0f : 1.0f;
  14213. else if (i == 6)
  14214. v = (float) (int) (v * 4.0f);
  14215. else if (i >= 35 && i <= 38)
  14216. v = (float) targets[(int) (v * 7.0f)];
  14217. P[(std::size_t) (44 * k + i)] = v;
  14218. }
  14219. for (int i = 0; i < 16; ++i)
  14220. {
  14221. auto v = u();
  14222. if (i == 1)
  14223. v = 0.3f + mul (v, 0.4f);
  14224. else if (i == 3)
  14225. v = eq ? 1.0f : 0.0f;
  14226. else if (i >= 4 && i <= 11)
  14227. v = 0.35f + mul (v, 0.3f);
  14228. else if (i == 12)
  14229. v = 1.0f;
  14230. else if (i == 13)
  14231. v = v * 0.1f;
  14232. else if (i == 0 || i == 15)
  14233. v = 0.0f;
  14234. P[(std::size_t) (704 + i)] = v;
  14235. }
  14236. std::vector<std::uint8_t> b;
  14237. auto put32 = [&b] (std::uint32_t v) { for (int i = 0; i < 4; ++i) b.push_back ((std::uint8_t) (v >> (8 * i))); };
  14238. b.insert (b.end(), { 'S', 'S', 'L', 'F' });
  14239. put32 (21636u);
  14240. put32 (0x444d5333u); // engine stream version 3
  14241. const std::string name = "LYNX test kit";
  14242. b.insert (b.end(), name.begin(), name.end());
  14243. b.resize (b.size() + 256 - name.size(), 0);
  14244. const auto at = b.size();
  14245. b.resize (at + 720 * 4);
  14246. std::memcpy (b.data() + at, P.data(), 720 * 4);
  14247. for (int k = 0; k < 16; ++k)
  14248. {
  14249. b.resize (b.size() + 0x400, 0);
  14250. b.push_back (100), b.push_back (100); // pattern length, trigger key
  14251. }
  14252. b.resize (b.size() + 0x800, 0);
  14253. for (int k = 0; k < 16; ++k)
  14254. b.push_back (k >= 2 && k <= 4 ? 1 : 0); // pads 3…5 in cut group 1
  14255. b.resize (b.size() + 16, 0); // every pad on the master
  14256. return b;
  14257. }
  14258. } // namespace
  14259. TEST (drumaxx_engine)
  14260. {
  14261. const auto st = drumaxxTestState (true);
  14262. CHECK (st.size() == 21644u && lynx::drumaxxStateUsable (st) && lynx::drumaxxKitName (st) == "LYNX test kit");
  14263. CHECK (lynx::drumaxxParam (st, 4) == 36.0f && lynx::drumaxxParam (st, 44 * 15 + 4) == 51.0f && lynx::drumaxxParam (st, 0x2c3) == 1.0f);
  14264. {
  14265. auto edited = st;
  14266. lynx::drumaxxSetParam (edited, 0x2c1, 0.25f);
  14267. CHECK (lynx::drumaxxParam (edited, 0x2c1) == 0.25f && std::isnan (lynx::drumaxxParam (edited, 720)));
  14268. auto bad = st;
  14269. bad[8] = 'X';
  14270. CHECK (! lynx::drumaxxStateUsable (bad) && std::isnan (lynx::drumaxxParam (bad, 0)));
  14271. }
  14272. // goldens: LYNX's renders, bit-identical to Drumaxx_x64.dll + engine_x64.dll driven by the round-67 harness
  14273. using N = lynx::drumaxx::RefNote;
  14274. struct Golden { bool eq; int fs, block; std::vector<N> notes; double el, er; std::tuple<int, float, float> at[6]; };
  14275. auto check = [] (const Golden& g) {
  14276. const auto y = lynx::drumaxx::renderReference (drumaxxTestState (g.eq), g.notes, (double) g.fs, 20000, g.block);
  14277. CHECK (y.size() == 2 * 20000);
  14278. if (y.size() != 2 * 20000)
  14279. return;
  14280. double el = 0.0, er = 0.0;
  14281. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  14282. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  14283. CHECK (std::abs (el - g.el) <= 1e-6 * g.el && std::abs (er - g.er) <= 1e-6 * g.er);
  14284. for (const auto& [i, l, r] : g.at)
  14285. CHECK (std::abs (y[2 * (std::size_t) i] - l) <= 1e-6f * std::max (1e-16f, std::abs (l))
  14286. && std::abs (y[2 * (std::size_t) i + 1] - r) <= 1e-6f * std::max (1e-16f, std::abs (r)));
  14287. };
  14288. // master EQ and limiter on: velocities, Final.Vol, pitch offsets, the cut group (pads 3…5)
  14289. check ({ true, 44100, 512,
  14290. { { 0, 36, 1.0, 1.0, 0.0 }, { 1500, 38, 0.7, 0.8, 0.0 }, { 3000, 37, 0.5, 1.0, -120.0 }, { 4000, 38, 1.0, 1.0, 0.0 },
  14291. { 5200, 42, 0.9, 1.0, 0.0 }, { 6000, 43, 0.6, 1.0, 35.0 }, { 7000, 44, 1.0, 0.5, 0.0 }, { 9000, 40, 0.8, 1.0, 0.0 },
  14292. { 11000, 45, 1.0, 1.0, 250.0 }, { 12500, 48, 0.4, 1.0, 0.0 }, { 14000, 51, 1.0, 1.0, 0.0 }, { 15000, 39, 1.0, 0.7, -40.0 } },
  14293. 2698.372377185949, 3678.285731410973,
  14294. { { 100, 0.0f, 0.0f }, { 1000, -0.0034233136102557182f, -0.005708245560526848f }, { 2500, -0.41297006607055664f, -0.0276018138974905f },
  14295. { 4000, -0.04906991869211197f, -0.7206904292106628f }, { 7777, 0.7544509172439575f, 0.7584841847419739f },
  14296. { 15000, -0.13438990712165833f, -0.6798369884490967f } } });
  14297. // EQ off, 48 kHz in 333-sample blocks, two pads on the same sample
  14298. check ({ false, 48000, 333,
  14299. { { 0, 46, 1.0, 1.0, 0.0 }, { 800, 47, 0.9, 1.0, 0.0 }, { 2600, 41, 1.0, 1.0, -300.0 }, { 5000, 49, 0.75, 1.0, 0.0 },
  14300. { 7000, 50, 1.0, 0.9, 77.0 }, { 9000, 36, 1.0, 1.0, 0.0 }, { 9001, 42, 1.0, 1.0, 0.0 }, { 13000, 43, 0.3, 1.0, 0.0 } },
  14301. 2241.008810618806, 1814.5219804412964,
  14302. { { 100, 0.0f, 0.0f }, { 1000, -0.32539352774620056f, -0.7439078688621521f }, { 2500, -0.1711389720439911f, -0.39125433564186096f },
  14303. { 4000, 0.15524788200855255f, 0.3445436358451843f }, { 7777, 0.5472834706306458f, 0.08737402409315109f },
  14304. { 15000, -0.03079034946858883f, -0.06140122935175896f } } });
  14305. // 96 kHz in 64-sample blocks, an octave down
  14306. check ({ true, 96000, 64,
  14307. { { 0, 36, 1.0, 1.0, 0.0 }, { 500, 44, 1.0, 1.0, 0.0 }, { 1000, 38, 0.85, 1.0, 12.0 }, { 6000, 37, 1.0, 1.0, 0.0 },
  14308. { 10000, 51, 1.0, 1.0, -1200.0 } },
  14309. 3522.8420617665834, 5746.555795626526,
  14310. { { 100, 0.0f, 0.0f }, { 1000, -0.04133236035704613f, -0.09954667836427689f }, { 2500, 0.2800408899784088f, -0.9350972175598145f },
  14311. { 4000, 0.9537044167518616f, 0.8892703056335449f }, { 7777, 0.8376388549804688f, -0.14166420698165894f },
  14312. { 15000, -0.05926012247800827f, -0.5840796828269958f } } });
  14313. // the instrument: a pad sounds on its key and dies away; keys without a pad stay silent
  14314. {
  14315. auto inst = lynx::makeInstrument (lynx::InstrumentKind::drumaxx);
  14316. lynx::ChannelDef def;
  14317. def.instrument = lynx::InstrumentKind::drumaxx;
  14318. def.pluginState = st;
  14319. inst->configure (def, 140.0);
  14320. inst->prepare (44100.0);
  14321. std::vector<float> l (512, 0.0f), r (512, 0.0f);
  14322. inst->noteOn (90, 1.0f);
  14323. inst->render (l.data(), r.data(), 512, 1.0f, 1.0f);
  14324. CHECK (inst->idle() && std::all_of (l.begin(), l.end(), [] (float v) { return v == 0.0f; }));
  14325. inst->noteOn (36, 0.8f);
  14326. inst->render (l.data(), r.data(), 512, 1.0f, 1.0f);
  14327. double e = 0.0;
  14328. for (auto v : l)
  14329. e += (double) v * v;
  14330. CHECK (! inst->idle() && e > 1e-3);
  14331. for (int k = 0; k < 200 && ! inst->idle(); ++k)
  14332. inst->render (l.data(), r.data(), 512, 1.0f, 1.0f);
  14333. CHECK (inst->idle());
  14334. }
  14335. // import: a Drumaxx channel plays on the exact engine with its raw state
  14336. {
  14337. FlpBuilder b;
  14338. b.ascii (0xC7, "25.2.5.5319");
  14339. b.u8 (0x40); b.u16 (0);
  14340. b.u8 (0x15); b.u8 (2);
  14341. b.utf16 (0xC9, u"Drumaxx");
  14342. b.data (0xD5, st);
  14343. lynx::FlpImportOptions options;
  14344. lynx::FlpImportReport report;
  14345. options.report = &report;
  14346. const auto song = lynx::importFlp (flp::parseProject (b.bytes), options);
  14347. CHECK (song.channels.size() == 1u);
  14348. if (song.channels.size() == 1u)
  14349. CHECK (song.channels[0].instrument == lynx::InstrumentKind::drumaxx && song.channels[0].pluginState == st);
  14350. CHECK (report.converted["Drumaxx"] == 1);
  14351. }
  14352. }
  14353. // --- REA round 68: FL Slayer (exact engine) and Fruity Dance (visual only) ------------------------------------------
  14354. namespace
  14355. {
  14356. std::string slayerDll()
  14357. {
  14358. if (realFlpDir.empty())
  14359. return {};
  14360. return (std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "FL Slayer" / "Slayer_Engine.dll").string();
  14361. }
  14362. std::vector<std::uint8_t> slayerPreset (const char* name)
  14363. {
  14364. if (realFlpDir.empty())
  14365. return {};
  14366. const auto file = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "FL Slayer" / (std::string (name) + ".fst");
  14367. const auto states = pluginStatesIn (file, "FL Slayer");
  14368. return states.empty() ? std::vector<std::uint8_t> {} : states.front();
  14369. }
  14370. } // namespace
  14371. TEST (slayer_state)
  14372. {
  14373. // the saved state: int version, then the parameter integers (version 0: 28, version 1: 29 with the mod wheel)
  14374. std::vector<std::uint8_t> v0 (4 + 28 * 4, 0), v1 (4 + 29 * 4, 0);
  14375. const std::int32_t one = 1, wheel = 77, mode = 5;
  14376. std::memcpy (v1.data(), &one, 4);
  14377. std::memcpy (v1.data() + 4 + 28 * 4, &wheel, 4);
  14378. std::memcpy (v0.data() + 4 + 24 * 4, &mode, 4);
  14379. CHECK (lynx::slayerStateUsable (v0) && lynx::slayerStateUsable (v1));
  14380. CHECK (! lynx::slayerStateUsable (std::vector<std::uint8_t> (v0.begin(), v0.end() - 1)));
  14381. auto v2 = v1;
  14382. v2[0] = 2;
  14383. CHECK (! lynx::slayerStateUsable (v2));
  14384. const auto p0 = lynx::slayerStateParams (v0), p1 = lynx::slayerStateParams (v1);
  14385. CHECK (p0.size() == 29u && p0[24] == 5 && p0[28] == 0 && p1.size() == 29u && p1[28] == 77);
  14386. // the controls' maxima (the front-end sends value / max)
  14387. CHECK (lynx::slayerParamMax (0) == 6 && lynx::slayerParamMax (20) == 15 && lynx::slayerParamMax (24) == 5 && lynx::slayerParamMax (28) == 127
  14388. && lynx::slayerParamMax (3) == 65535);
  14389. // MIDI velocity from Init.Vol (0 → 0, which the engine takes as a note-off; FL's full velocity → 127)
  14390. CHECK (lynx::slayer::midiVelocity (0.0) == 0 && lynx::slayer::midiVelocity (1.0) == 99 && lynx::slayer::midiVelocity (2.0) == 126
  14391. && lynx::slayer::midiVelocity ((std::pow (11.0, 1.28) - 1.0) / 10.0) == 127);
  14392. }
  14393. TEST (slayer_engine)
  14394. {
  14395. const auto dll = slayerDll();
  14396. if (dll.empty() || ! lynx::slayerDllUsable (dll))
  14397. return; // no FL installation
  14398. // against the installation's Slayer_Engine.dll (EXEC, REA_rodada68 §7: the DLL driven like FL's front-end drives it,
  14399. // host blocks of 512 at 44.1 kHz, tempo 120): bit-identical output. C3 vel 90 1024–20000, G3 vel 120 8000–30000,
  14400. // E4 vel 40 16000–40000, C5 vel 127 from 30000.
  14401. struct Sample { int at; float l, r; };
  14402. struct Golden { const char* preset; double el, er; Sample s[4]; };
  14403. const Golden goldens[] {
  14404. { "BASS Ebass 2", 15073.5614896, 15073.5614896, { { 5000, 0.39340171217918396f, 0.39340171217918396f }, { 12000, -1.6453207731246948f, -1.6453207731246948f }, { 25000, 0.4477591812610626f, 0.4477591812610626f }, { 40000, 0.15478527545928955f, 0.15478527545928955f } } },
  14405. { "DIST Dire Straits", 1667.57549156, 1687.24007956, { { 5000, 0.026657210662961006f, -0.10683870315551758f }, { 12000, 0.16261409223079681f, -0.402504026889801f }, { 25000, 0.06702516227960587f, -0.04462721571326256f }, { 40000, -0.35892999172210693f, 0.29433923959732056f } } },
  14406. { "POWER Metal", 2041.24961449, 2041.24961449, { { 5000, -0.045430999249219894f, -0.045430999249219894f }, { 12000, 0.009085401892662048f, 0.009085401892662048f }, { 25000, -0.15170709788799286f, -0.15170709788799286f }, { 40000, -0.0430934801697731f, -0.0430934801697731f } } },
  14407. { "SINGLE Chorus", 1199.72408266, 1228.23838997, { { 5000, 0.08928016573190689f, 0.07585708051919937f }, { 12000, 0.22826193273067474f, 0.16860444843769073f }, { 25000, 0.14708775281906128f, 0.12602058053016663f }, { 40000, -0.02422964572906494f, -0.021596161648631096f } } },
  14408. { "AUTO Leslie", 2045.59821914, 16901.1532986, { { 5000, 0.09575536102056503f, -0.0019274931401014328f }, { 12000, -0.0070709832943975925f, -0.3028614819049835f }, { 25000, 0.07535621523857117f, 0.24308738112449646f }, { 40000, -0.09379560500383377f, -0.008114430122077465f } } },
  14409. { "FX Mad Photons", 1849.74988002, 1849.74988002, { { 5000, -0.11505282670259476f, -0.11505282670259476f }, { 12000, 0.3610122501850128f, 0.3610122501850128f }, { 25000, 0.36028820276260376f, 0.36028820276260376f }, { 40000, -0.5374203324317932f, -0.5374203324317932f } } },
  14410. { "ELEKTRO Jungle", 3929.94173335, 3929.94173335, { { 5000, 0.3158963918685913f, 0.3158963918685913f }, { 12000, 0.2988760769367218f, 0.2988760769367218f }, { 25000, 0.15749095380306244f, 0.15749095380306244f }, { 40000, -0.12722519040107727f, -0.12722519040107727f } } },
  14411. { "DIST Sololead Glide", 988.454469266, 1208.61893055, { { 5000, -0.03612576052546501f, -0.015017407014966011f }, { 12000, -0.008314903825521469f, 0.03836013376712799f }, { 25000, -0.047041457146406174f, -0.13751298189163208f }, { 40000, -0.03302673622965813f, -0.23574480414390564f } } },
  14412. { "SINGLE Funky", 1393.67536587, 4191.35072624, { { 5000, -0.09534210711717606f, 0.36347872018814087f }, { 12000, 0.3368682265281677f, -0.9259893894195557f }, { 25000, -0.022296791896224022f, 0.01075129397213459f }, { 40000, 0.044511131942272186f, -0.37124285101890564f } } },
  14413. { "AUTO Chorus Chords", 26161.9618548, 26396.2394554, { { 5000, -0.19687165319919586f, 0.08371803164482117f }, { 12000, 1.0583257675170898f, 1.5273079872131348f }, { 25000, -0.88118577003479f, -0.0774342492222786f }, { 40000, -0.3456699848175049f, -0.4140896201133728f } } },
  14414. { "POWER Rock Taps", 1306.21610814, 1164.18639692, { { 5000, 0.019661661237478256f, 0.07336095720529556f }, { 12000, -0.06010562554001808f, -0.3300100862979889f }, { 25000, 0.04404725879430771f, 0.0837465152144432f }, { 40000, -0.029322467744350433f, -0.1399042010307312f } } },
  14415. { "FX Atari 2600 Computer", 1391.44227691, 1391.44227691, { { 5000, 0.18123838305473328f, 0.18123838305473328f }, { 12000, -0.1220453679561615f, -0.1220453679561615f }, { 25000, 0.17565210163593292f, 0.17565210163593292f }, { 40000, 0.21183894574642181f, 0.21183894574642181f } } },
  14416. };
  14417. const std::vector<lynx::slayer::RefNote> notes = { { 1024, 48, 90, 20000 }, { 8000, 55, 120, 30000 }, { 16000, 64, 40, 40000 }, { 30000, 72, 127, -1 } };
  14418. constexpr int length = 44100;
  14419. int checked = 0;
  14420. for (const auto& g : goldens)
  14421. {
  14422. const auto st = slayerPreset (g.preset);
  14423. CHECK (lynx::slayerStateUsable (st));
  14424. if (! lynx::slayerStateUsable (st))
  14425. continue;
  14426. const auto y = lynx::slayer::renderReference (lynx::slayerStateParams (st), dll, notes, 44100.0, length);
  14427. double el = 0.0, er = 0.0;
  14428. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  14429. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  14430. bool ok = y.size() == 2 * (std::size_t) length && std::abs (el - g.el) <= 1e-10 * g.el && std::abs (er - g.er) <= 1e-10 * g.er;
  14431. for (const auto& smp : g.s)
  14432. ok = ok && y[2 * (std::size_t) smp.at] == smp.l && y[2 * (std::size_t) smp.at + 1] == smp.r;
  14433. if (! ok)
  14434. std::printf (" FL Slayer %s differs from FL's DLL: energy %.12g %.12g (DLL %.12g %.12g)\n", g.preset, el, er, g.el, g.er);
  14435. CHECK (ok);
  14436. ++checked;
  14437. }
  14438. CHECK (checked == 12);
  14439. // the instrument: the engine primed with one sample, FL's velocity curve, the front-end's gains from the channel's
  14440. // MIDI pan / volume (REA_rodada79 §2: twice the channel gain through the host's integer-pan law); a note with
  14441. // velocity 0.8 is the reference note one sample later
  14442. const auto st = slayerPreset ("BASS Ebass 2");
  14443. lynx::ChannelDef c;
  14444. c.instrument = lynx::InstrumentKind::slayer;
  14445. c.pluginState = st;
  14446. c.samplePath = dll;
  14447. auto inst = lynx::makeInstrument (lynx::InstrumentKind::slayer);
  14448. inst->configure (c, 120.0);
  14449. inst->prepare (44100.0);
  14450. std::vector<float> l (16384, 0.0f), r (16384, 0.0f);
  14451. for (int b = 0; b < 32; ++b)
  14452. {
  14453. if (b == 0)
  14454. inst->noteOn (60, 0.8f);
  14455. if (b == 20)
  14456. inst->noteOff (60);
  14457. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 0.5f, 0.25f);
  14458. }
  14459. const int vel = lynx::slayer::midiVelocity ((std::pow (11.0, 1.28 * 0.8) - 1.0) / 10.0);
  14460. const auto ref = lynx::slayer::renderReference (lynx::slayerStateParams (st), dll, { { 1, 60, vel, 1 + 20 * 512 } }, 44100.0, 16385);
  14461. // LYNX's channel gains 0.5 / 0.25 = gain 0.395…, pan −0.4097 → MIDI pan −26
  14462. const auto chanVol = (float) std::sqrt ((0.25 + 0.0625) * 0.5);
  14463. float hl = 0.0f, hr = 0.0f;
  14464. lynx::slayer::hostGains (lynx::slayer::midiPan (std::atan2 (0.25, 0.5) * (4.0 / 3.141592653589793) - 1.0), chanVol, hl, hr);
  14465. CHECK (lynx::slayer::midiPan (std::atan2 (0.25, 0.5) * (4.0 / 3.141592653589793) - 1.0) == -26);
  14466. double diff = 0.0, energy = 0.0;
  14467. for (int i = 0; i < 16384; ++i)
  14468. {
  14469. diff = std::max ({ diff, (double) std::abs (l[(std::size_t) i] - ref[2 * (std::size_t) (i + 1)] * hl),
  14470. (double) std::abs (r[(std::size_t) i] - ref[2 * (std::size_t) (i + 1) + 1] * hr) });
  14471. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  14472. }
  14473. CHECK (diff == 0.0 && energy > 1.0 && vel == 102);
  14474. // every preset of the installation: usable and sounding
  14475. int presets = 0, sounding = 0;
  14476. const auto folder = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "FL Slayer";
  14477. if (std::filesystem::exists (folder))
  14478. for (const auto& entry : std::filesystem::directory_iterator (folder))
  14479. for (const auto& s : pluginStatesIn (entry.path(), "FL Slayer"))
  14480. {
  14481. ++presets;
  14482. const auto y = lynx::slayer::renderReference (lynx::slayerStateParams (s), dll, { { 1024, 52, 100, 20000 } }, 44100.0, 22050);
  14483. double e = 0.0;
  14484. bool finite = true;
  14485. for (auto v : y)
  14486. finite = finite && std::isfinite (v), e += (double) v * v;
  14487. CHECK (finite);
  14488. sounding += e > 1.0 ? 1 : 0;
  14489. }
  14490. std::printf (" FL Slayer presets: %d, %d sounding\n", presets, sounding);
  14491. CHECK (presets == 64 && sounding == 64);
  14492. }
  14493. // FL Slayer's host levels and live parameters (REA_rodada79): channel pan / volume / pitch arrive as MIDI events
  14494. // (ProcessEvent 2/3/4), automation and settings edits as setParameter calls on the running engine
  14495. TEST (slayer_host_levels_and_automation)
  14496. {
  14497. using namespace lynx::slayer;
  14498. CHECK (pitchBend (0) == 8192 && pitchBend (100) == 8874 && pitchBend (-100) == 7510 && pitchBend (1200) == 0x3fff
  14499. && pitchBend (-1200) == 0 && pitchBend (-1199) == 7 && pitchBend (5000) == 0x3fff);
  14500. CHECK (midiPan (0.0) == 0 && midiPan (1.0) == 64 && midiPan (-1.0) == -64 && midiPan (-0.5) == -32
  14501. && midiPan (99.0 / 6400.0) == 0 && midiPan (-199.0 / 6400.0) == -1 && midiPan (2.0) == 64);
  14502. float l0 = 0.0f, r0 = 0.0f, l1 = 0.0f, r1 = 0.0f;
  14503. hostGains (0, 0.5f, l0, r0); // the front-end's own start values (volume 1 after the ×2, centre)
  14504. hostGains (64, 1.0f, l1, r1);
  14505. CHECK (l0 == 0.70710677f && r0 == 0.70710677f && std::abs (l1) < 1e-15f && r1 == 2.0f);
  14506. const auto dll = slayerDll();
  14507. if (dll.empty() || ! lynx::slayerDllUsable (dll))
  14508. return;
  14509. const auto st = slayerPreset ("DIST Dire Straits");
  14510. const auto params = lynx::slayerStateParams (st);
  14511. lynx::ChannelDef c;
  14512. c.instrument = lynx::InstrumentKind::slayer;
  14513. c.pluginState = st;
  14514. c.samplePath = dll;
  14515. c.sampler.pitch = 1.5f; // channel pitch +150 cents → bend 9216; no root shift
  14516. c.modelParams = { 0.0f };
  14517. auto inst = lynx::makeInstrument (lynx::InstrumentKind::slayer);
  14518. inst->configure (c, 120.0);
  14519. inst->prepare (44100.0);
  14520. const int blocks = 40, n = 512;
  14521. std::vector<float> l ((std::size_t) (blocks * n), 0.0f), r ((std::size_t) (blocks * n), 0.0f);
  14522. auto edited = st;
  14523. const std::int32_t newDrive = 60000; // parameter 13 (amp drive), edited in the settings at block 25
  14524. std::memcpy (edited.data() + 4 + 4 * 13, &newDrive, 4);
  14525. for (int b = 0; b < blocks; ++b)
  14526. {
  14527. if (b == 0)
  14528. inst->noteOn (52, 0.8f);
  14529. if (b == 10)
  14530. inst->setParameter (20, 9.0f / 15.0f); // automation: effect 9 (chorus)
  14531. if (b == 18)
  14532. inst->setParameter (28, 0.5f); // automation: the mod wheel → CC 1 64
  14533. if (b == 25)
  14534. {
  14535. c.pluginState = edited;
  14536. inst->configure (c, 120.0);
  14537. }
  14538. inst->render (l.data() + n * b, r.data() + n * b, n, 1.0f, 1.0f);
  14539. }
  14540. lynx::slayer::RefOptions opt;
  14541. opt.midi.push_back ({ 1, 0xe0, 9216 & 0x7f, 9216 >> 7 });
  14542. opt.params = { { 1 + 10 * n, 20, 9 }, { 1 + 18 * n, 28, 64 }, { 1 + 25 * n, 13, newDrive } };
  14543. const int vel = lynx::slayer::midiVelocity ((std::pow (11.0, 1.28 * 0.8) - 1.0) / 10.0);
  14544. const auto ref = lynx::slayer::renderReference (params, dll, { { 1, 52, vel, -1 } }, 44100.0, blocks * n + 1, opt);
  14545. float hl = 0.0f, hr = 0.0f;
  14546. hostGains (0, 1.0f, hl, hr);
  14547. double diff = 0.0, energy = 0.0;
  14548. for (int i = 0; i < blocks * n; ++i)
  14549. {
  14550. diff = std::max ({ diff, (double) std::abs (l[(std::size_t) i] - ref[2 * (std::size_t) (i + 1)] * hl),
  14551. (double) std::abs (r[(std::size_t) i] - ref[2 * (std::size_t) (i + 1) + 1] * hr) });
  14552. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  14553. }
  14554. CHECK (diff == 0.0 && energy > 1.0);
  14555. // the changes really change the sound: the same render without them differs
  14556. const auto plain = lynx::slayer::renderReference (params, dll, { { 1, 52, vel, -1 } }, 44100.0, blocks * n + 1);
  14557. CHECK (plain != ref);
  14558. }
  14559. TEST (slayer_and_fruity_dance_import)
  14560. {
  14561. // FL Slayer plays on the exact engine when the installation's engine DLL is there (otherwise it stays unsupported);
  14562. // Fruity Dance (a visual plugin) becomes a silent channel that keeps its state
  14563. std::vector<std::uint8_t> slayer (4 + 28 * 4, 0), dance (40, 1);
  14564. FlpBuilder b;
  14565. b.ascii (0xC7, "25.2.5.5319");
  14566. int index = 0;
  14567. for (const auto& [name, state] : { std::pair<std::u16string, std::vector<std::uint8_t>> { u"FL Slayer", slayer }, { u"Fruity Dance", dance } })
  14568. {
  14569. b.u8 (0x40); b.u16 ((std::uint16_t) index++);
  14570. b.u8 (0x15); b.u8 (2);
  14571. b.utf16 (0xC9, name);
  14572. b.data (0xD5, state);
  14573. }
  14574. const auto project = flp::parseProject (b.bytes);
  14575. const auto plain = lynx::importFlp (project);
  14576. CHECK (plain.channels.size() == 2u);
  14577. if (plain.channels.size() == 2u)
  14578. {
  14579. CHECK (plain.channels[0].instrument != lynx::InstrumentKind::slayer);
  14580. CHECK (plain.channels[1].instrument == lynx::InstrumentKind::fruitydance && plain.channels[1].pluginState == dance);
  14581. }
  14582. auto inst = lynx::makeInstrument (lynx::InstrumentKind::fruitydance);
  14583. inst->prepare (44100.0);
  14584. std::vector<float> l (1024, 0.0f), r (1024, 0.0f);
  14585. inst->noteOn (60, 1.0f);
  14586. inst->render (l.data(), r.data(), 1024, 1.0f, 1.0f);
  14587. CHECK (inst->idle() && std::all_of (l.begin(), l.end(), [] (float v) { return v == 0.0f; }));
  14588. const auto dll = slayerDll();
  14589. if (dll.empty() || ! lynx::slayerDllUsable (dll))
  14590. return;
  14591. lynx::FlpImportOptions withFiles;
  14592. withFiles.roots = { std::filesystem::path (realFlpDir).parent_path().string(), realFlpDir, "" };
  14593. withFiles.fileExists = [] (const std::string& path) {
  14594. auto local = path;
  14595. std::replace (local.begin(), local.end(), '\\', '/');
  14596. return std::filesystem::exists (local);
  14597. };
  14598. const auto song = lynx::importFlp (project, withFiles);
  14599. CHECK (song.channels.size() == 2u && song.channels[0].instrument == lynx::InstrumentKind::slayer && song.channels[0].pluginState == slayer
  14600. && ! song.channels[0].samplePath.empty());
  14601. }
  14602. #include "WaveAnalysisTests.inc"
  14603. #include "UiPolish2Tests.inc"
  14604. #include "UiToolbarTests.inc"
  14605. #include "LuxeVerbTests.inc"
  14606. #include "Parity172bTests.inc"
  14607. #include "ConvolverTests.inc"
  14608. // --- Newtone / Newtime (REA_rodada73) -----------------------------------------------------------------------------
  14609. namespace newtonetest
  14610. {
  14611. void put32 (std::vector<std::uint8_t>& b, std::int32_t v) { for (int i = 0; i < 4; ++i) b.push_back ((std::uint8_t) ((std::uint32_t) v >> (8 * i))); }
  14612. void put64 (std::vector<std::uint8_t>& b, std::int64_t v) { for (int i = 0; i < 8; ++i) b.push_back ((std::uint8_t) ((std::uint64_t) v >> (8 * i))); }
  14613. void record (std::vector<std::uint8_t>& b, int id, const std::vector<std::uint8_t>& d)
  14614. {
  14615. put32 (b, id);
  14616. put64 (b, (std::int64_t) d.size());
  14617. b.insert (b.end(), d.begin(), d.end());
  14618. }
  14619. // a state with our own layout reading (REA_rodada73 §2): options, the wrapper record around the engine XML, the source path
  14620. std::vector<std::uint8_t> state (int version, std::uint32_t flags, const std::string& xml, const std::string& source)
  14621. {
  14622. std::vector<std::uint8_t> b, r1, r2;
  14623. put32 (b, version);
  14624. put32 (r1, 727);
  14625. put32 (r1, 582);
  14626. put32 (r1, (std::int32_t) flags);
  14627. r1.resize (0x28, 0);
  14628. record (b, 1, r1);
  14629. std::vector<std::uint8_t> chunk { 'V', 'C', '2', '!' };
  14630. put32 (chunk, (std::int32_t) xml.size() + 1);
  14631. chunk.insert (chunk.end(), xml.begin(), xml.end());
  14632. chunk.push_back (0);
  14633. put32 (r2, -9);
  14634. r2.push_back (0x0c);
  14635. put32 (r2, -2);
  14636. put64 (r2, (std::int64_t) chunk.size());
  14637. put32 (r2, 0);
  14638. r2.insert (r2.end(), chunk.begin(), chunk.end());
  14639. record (b, 2, r2);
  14640. record (b, 3, std::vector<std::uint8_t> (source.begin(), source.end()));
  14641. record (b, 4, std::vector<std::uint8_t> (12, 0));
  14642. record (b, 5, std::vector<std::uint8_t> (16, 0));
  14643. record (b, 6, std::vector<std::uint8_t> (32, 0));
  14644. return b;
  14645. }
  14646. const char* xmlWithTrack (bool edited)
  14647. {
  14648. return edited ? "<NewToneProject NumOfTracks=\"1\"><Tracks><Trk_0 TrackNum=\"0\" Type=\"class CzplJPositionablePSOLASource\" "
  14649. "FilePath=\"C:\\tmp\\a&amp;b.wav\"><AudioObject SelfBPM=\"120.0\" OriginalFileLength=\"1000\" GlobalTune=\"0.0\">"
  14650. "<Pitch_0 fOrigPitch=\"60.0\" fCurPitch=\"60.0\" iOrigLength=\"500\" iCurLength=\"500\"/>"
  14651. "<Pitch_1 fOrigPitch=\"62.0\" fCurPitch=\"64.0\" iOrigLength=\"500\" iCurLength=\"500\"/></AudioObject></Trk_0></Tracks></NewToneProject>"
  14652. : "<NewToneProject NumOfTracks=\"1\"><Tracks><Trk_0 TrackNum=\"0\" Type=\"class CzplJTimelineWarpSource\" "
  14653. "FilePath=\"x.wav\"><AudioObject SelfBPM=\"80.0\" OriginalFileLength=\"1000\">"
  14654. "<Pitch_0 fOrigPitch=\"60.0\" fCurPitch=\"60.0\" fCurVolume=\"1.0\"/></AudioObject></Trk_0></Tracks></NewToneProject>";
  14655. }
  14656. } // namespace newtonetest
  14657. TEST (newtone_state)
  14658. {
  14659. using namespace newtonetest;
  14660. const auto a = lynx::newtonefx::parse (state (0, 5, xmlWithTrack (true), "%FLStudioUserData%\\vox.wav"));
  14661. CHECK (a.has_value());
  14662. CHECK (a->version == 0 && a->editorWidth == 727 && a->editorHeight == 582 && a->slave());
  14663. CHECK (a->sourcePath == "%FLStudioUserData%\\vox.wav");
  14664. CHECK (a->tracks.size() == 1 && a->tracks[0].notes == 2 && a->tracks[0].frames == 1000 && ! a->tracks[0].timeWarp);
  14665. CHECK (a->tracks[0].filePath == "C:\\tmp\\a&b.wav" && a->tracks[0].selfBpm == 120.0);
  14666. CHECK (a->tracks[0].edited && a->edited()); // note 1 moved from 62 to 64
  14667. const auto b = lynx::newtonefx::parse (state (1, 0x41, xmlWithTrack (false), ""));
  14668. CHECK (b.has_value() && ! b->slave() && b->sourcePath.empty());
  14669. CHECK (b->tracks.size() == 1 && b->tracks[0].timeWarp && ! b->edited());
  14670. // an empty project, as the installation's presets
  14671. const auto c = lynx::newtonefx::parse (state (0, 1, "<NewToneProject NumOfTracks=\"0\"><Tracks/></NewToneProject>", ""));
  14672. CHECK (c.has_value() && c->tracks.empty());
  14673. // broken: other version, truncated record, no options record
  14674. CHECK (! lynx::newtonefx::parse (state (2, 1, "", "")));
  14675. auto cut = state (0, 1, xmlWithTrack (true), "");
  14676. cut.resize (cut.size() - 3);
  14677. CHECK (! lynx::newtonefx::parse (cut));
  14678. CHECK (! lynx::newtonefx::parse ({ 0, 0, 0, 0 }));
  14679. // the FL import keeps the state and takes the slave switch as the editable parameter
  14680. const auto raw = state (0, 5, xmlWithTrack (false), "");
  14681. const auto slot = lynx::effectFromFl ("Newtone", raw, 120.0);
  14682. CHECK (slot && slot->kind == lynx::EffectKind::newtone && slot->state == raw && slot->params[0] == 1.0f);
  14683. const auto slot2 = lynx::effectFromFl ("Newtime", state (1, 0x41, xmlWithTrack (false), ""), 120.0);
  14684. CHECK (slot2 && slot2->kind == lynx::EffectKind::newtime && slot2->params[0] == 0.0f);
  14685. CHECK (lynx::formatEffectParam (lynx::EffectKind::newtone, 0, 1.0f, 120.0) == "on");
  14686. // the installation's own states (plugin database and the playlist's pitch/warp correction tracks) [DATA]
  14687. if (! realFlpDir.empty())
  14688. {
  14689. int found = 0, read = 0;
  14690. for (const char* rel : { "System/PitchCorrectionTrack.fst", "System/WarpCorrectionTrack.fst",
  14691. "Patches/Plugin database/Effects/Misc/Newtone.fst", "Patches/Plugin database/Effects/Misc/Newtime.fst" })
  14692. {
  14693. const auto path = std::filesystem::path (realFlpDir) / rel;
  14694. if (! std::filesystem::exists (path))
  14695. continue;
  14696. ++found;
  14697. std::ifstream f (path, std::ios::binary);
  14698. std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (f)), std::istreambuf_iterator<char>());
  14699. const auto fx = lynx::effectFromFlPreset (bytes, 120.0);
  14700. read += fx && (fx->kind == lynx::EffectKind::newtone || fx->kind == lynx::EffectKind::newtime) && ! fx->state.empty()
  14701. && fx->params[0] == 0.0f && lynx::newtonefx::parse (fx->state)->tracks.empty();
  14702. }
  14703. std::printf (" %d Newtone/Newtime states, %d read\n", found, read);
  14704. CHECK (read == found);
  14705. }
  14706. }
  14707. TEST (newtone_engine)
  14708. {
  14709. // REA_rodada73 §3-4 [EXEC]: the input passes unchanged unless slave mode is on and the song plays; then the source
  14710. // sample is added from the song time on (at the source's own rate)
  14711. auto sample = std::make_shared<lynx::SampleData>();
  14712. sample->sampleRate = 44100.0;
  14713. for (int i = 0; i < 3000; ++i)
  14714. sample->left.push_back (0.001f * (float) (i % 97) - 0.04f);
  14715. const double sr = 44100.0, bpm = 120.0;
  14716. const auto tps = lynx::ticksPerSample (bpm, sr);
  14717. auto fx = lynx::newtonefx::make (sample);
  14718. fx->prepare (sr);
  14719. lynx::EffectParams on {}, off {};
  14720. on[0] = 1.0f;
  14721. std::vector<float> l (256), r (256), in (256);
  14722. auto block = [&] (double tick, bool playing, const lynx::EffectParams& p) {
  14723. for (int i = 0; i < 256; ++i)
  14724. l[(std::size_t) i] = r[(std::size_t) i] = in[(std::size_t) i] = 0.25f * std::sin (0.01f * (float) (i + tick));
  14725. fx->setSongPosition (tick);
  14726. fx->setPlaying (playing);
  14727. fx->process (l.data(), r.data(), 256, p, bpm);
  14728. };
  14729. auto same = [&] { return std::equal (l.begin(), l.end(), in.begin()) && std::equal (r.begin(), r.end(), in.begin()); };
  14730. block (0.0, true, off);
  14731. CHECK (same()); // slave off
  14732. block (0.0, false, on);
  14733. CHECK (same()); // not playing
  14734. // playing from the start: in + source, sample by sample, over consecutive blocks
  14735. bool exact = true;
  14736. for (int b = 0; b < 4; ++b)
  14737. {
  14738. block (b * 256 * tps, true, on);
  14739. for (int i = 0; i < 256; ++i)
  14740. exact = exact && l[(std::size_t) i] == in[(std::size_t) i] + sample->left[(std::size_t) (b * 256 + i)]
  14741. && r[(std::size_t) i] == l[(std::size_t) i];
  14742. }
  14743. CHECK (exact);
  14744. // a jump re-seeks to the song time; past the end of the sample only the input remains
  14745. block (2000 * tps, true, on);
  14746. CHECK (l[0] == in[0] + sample->left[2000] && l[255] == in[255] + sample->left[2255]);
  14747. block (2900 * tps, true, on);
  14748. CHECK (l[99] == in[99] + sample->left[2999] && l[100] == in[100]);
  14749. // stopping and playing again: from the new song time
  14750. block (0.0, false, on);
  14751. CHECK (same());
  14752. block (10 * tps, true, on);
  14753. CHECK (l[0] == in[0] + sample->left[10]);
  14754. // without its sample: a pass-through
  14755. auto none = lynx::makeEffect (lynx::EffectKind::newtime);
  14756. none->prepare (sr);
  14757. for (int i = 0; i < 256; ++i)
  14758. l[(std::size_t) i] = r[(std::size_t) i] = in[(std::size_t) i] = 0.1f * (float) i / 256.0f;
  14759. none->setPlaying (true);
  14760. none->process (l.data(), r.data(), 256, on, bpm);
  14761. CHECK (same());
  14762. }
  14763. TEST (newtone_rack_follows_transport)
  14764. {
  14765. // the mixer gives the effect the transport, and keeps it running on a silent insert (no smart disable)
  14766. auto song = lynx::Song::makeDefault();
  14767. for (auto& p : song.patterns)
  14768. p.notes.clear();
  14769. song.bpm = 120.0;
  14770. auto slot = lynx::makeEffectSlot (lynx::EffectKind::newtone);
  14771. slot.params[0] = 1.0f;
  14772. slot.samplePath = "newtone-test.wav";
  14773. song.mixer[1].effects.push_back (slot);
  14774. auto sample = std::make_shared<lynx::SampleData>();
  14775. sample->sampleRate = 48000.0;
  14776. sample->left.assign (48000, 0.5f);
  14777. int loads = 0;
  14778. lynx::RackState::SampleProvider provider = [&] (const std::string& path) -> std::shared_ptr<const lynx::SampleData> {
  14779. ++loads;
  14780. return path == "newtone-test.wav" ? sample : nullptr;
  14781. };
  14782. const double sr = 48000.0;
  14783. auto state = lynx::RackState::build (song, nullptr, sr, provider);
  14784. CHECK (loads == 1);
  14785. lynx::RackRenderer rr;
  14786. rr.prepare (sr);
  14787. std::vector<float> l (512), r (512);
  14788. auto peakOf = [&] (bool playing, double tick) {
  14789. float peak = 0.0f;
  14790. for (int b = 0; b < 8; ++b)
  14791. {
  14792. rr.process (state.get(), playing, tick + b * 512 * lynx::ticksPerSample (song.bpm, sr), 512, l.data(), r.data());
  14793. for (auto v : l)
  14794. peak = std::max (peak, std::abs (v));
  14795. }
  14796. return peak;
  14797. };
  14798. CHECK (peakOf (false, 0.0) == 0.0f);
  14799. CHECK (peakOf (true, 0.0) > 0.1f);
  14800. }
  14801. #include "HardcoreTests.inc"
  14802. #include "DistructorTests.inc"
  14803. // --- missing files (core/MissingFiles.h) ------------------------------------------------------------------------
  14804. TEST (missing_files_names_and_scores)
  14805. {
  14806. CHECK (lynx::pathFileName ("C:\\Samples\\Drums\\Kick 01.wav") == "Kick 01.wav");
  14807. CHECK (lynx::pathFileName ("/home/u/loops/beat.flac") == "beat.flac");
  14808. CHECK (lynx::pathFileName ("D:/mixed\\sep/x.wav/") == "x.wav");
  14809. CHECK (lynx::pathFileName ("plain.wav") == "plain.wav");
  14810. CHECK (lynx::relocationScore ("C:\\Old\\Drums\\Kicks\\kick.wav", "/new/Drums/Kicks/KICK.WAV") == 3);
  14811. CHECK (lynx::relocationScore ("C:\\Old\\Drums\\Kicks\\kick.wav", "/new/other/kick.wav") == 1);
  14812. CHECK (lynx::relocationScore ("C:\\Old\\kick.wav", "/new/kick2.wav") == 0);
  14813. }
  14814. TEST (missing_files_find_in_song)
  14815. {
  14816. lynx::Song song;
  14817. lynx::ChannelDef a, b, c, d, e;
  14818. a.name = "Kick", a.instrument = lynx::InstrumentKind::sampler, a.samplePath = "C:\\gone\\kick.wav";
  14819. b.name = "Vox", b.instrument = lynx::InstrumentKind::audioClip, b.samplePath = "C:\\gone\\vox.wav";
  14820. c.name = "Here", c.instrument = lynx::InstrumentKind::sampler, c.samplePath = "C:\\here\\snare.wav";
  14821. d.name = "GMS", d.instrument = lynx::InstrumentKind::gms, d.samplePath = "C:\\FL\\gms waves"; // installation data: not listed
  14822. e.name = "Kick 2", e.instrument = lynx::InstrumentKind::sampler, e.samplePath = "c:/GONE/KICK.wav"; // same file, other spelling
  14823. lynx::SampleZone z;
  14824. z.path = "C:\\gone\\piano_c4.wav";
  14825. a.zones.push_back (z);
  14826. song.channels = { a, b, c, d, e };
  14827. song.mixer.resize (3);
  14828. lynx::EffectSlot fx;
  14829. fx.samplePath = "C:\\gone\\tune.wav";
  14830. lynx::EffectSlot parallel;
  14831. parallel.branches.push_back ({ { fx }, 1.0f });
  14832. song.mixer[2].effects.push_back (parallel);
  14833. song.mixer[2].name = "Lead";
  14834. const auto exists = [] (const std::string& p) { return p.find ("here") != std::string::npos || p.find ("FL") != std::string::npos; };
  14835. auto missing = lynx::findMissingSamples (song, exists);
  14836. CHECK (missing.size() == 4);
  14837. CHECK (missing.size() == 4 && missing[0].path == "C:\\gone\\kick.wav" && missing[0].usedBy.size() == 2 && missing[0].usedBy[1] == "Kick 2");
  14838. CHECK (missing.size() == 4 && missing[1].path == "C:\\gone\\piano_c4.wav" && missing[1].usedBy[0] == "Kick");
  14839. CHECK (missing.size() == 4 && missing[2].usedBy[0] == "Audio clip: Vox");
  14840. CHECK (missing.size() == 4 && missing[3].usedBy[0] == "Lead: effect");
  14841. // candidates from a search: the deeper path match wins; one stays missing
  14842. const std::vector<std::string> hits { "/lib/other/kick.wav", "/lib/gone/kick.wav", "/lib/x/vox.wav", "/lib/y/tune.wav" };
  14843. CHECK (lynx::relocateByName (missing, hits) == 3);
  14844. CHECK (missing[0].found == "/lib/gone/kick.wav");
  14845. CHECK (missing[1].found.empty());
  14846. missing[3].skipped = true; // skipped: not applied
  14847. const auto map = lynx::relocationMap (missing);
  14848. CHECK (map.size() == 2);
  14849. CHECK (lynx::applyRelocations (song, map) == 3); // both spellings of kick.wav + vox
  14850. CHECK (song.channels[0].samplePath == "/lib/gone/kick.wav" && song.channels[4].samplePath == "/lib/gone/kick.wav");
  14851. CHECK (song.channels[1].samplePath == "/lib/x/vox.wav");
  14852. CHECK (song.channels[2].samplePath == "C:\\here\\snare.wav");
  14853. CHECK (song.mixer[2].effects[0].branches[0].chain[0].samplePath == "C:\\gone\\tune.wav");
  14854. lynx::Song copy = song;
  14855. CHECK (lynx::applyRelocations (copy, { { "C:\\gone\\tune.wav", "/t.wav" } }) == 1 && copy.mixer[2].effects[0].branches[0].chain[0].samplePath == "/t.wav");
  14856. }
  14857. TEST (missing_files_inside_patcher)
  14858. {
  14859. // a Patcher's instruments and effects (any depth) are the song's samples too: listed, packed and relinked
  14860. lynx::Song song;
  14861. lynx::ChannelDef inner;
  14862. inner.name = "Pad", inner.instrument = lynx::InstrumentKind::sampler, inner.samplePath = "C:\\gone\\pad.wav";
  14863. lynx::ChannelDef nested;
  14864. nested.instrument = lynx::InstrumentKind::patcher;
  14865. nested.patcher.nodes.resize (1);
  14866. nested.patcher.nodes[0].generator = true;
  14867. nested.patcher.nodes[0].channel = inner;
  14868. lynx::ChannelDef patch;
  14869. patch.name = "Rack", patch.instrument = lynx::InstrumentKind::patcher;
  14870. patch.patcher.nodes.resize (2);
  14871. patch.patcher.nodes[0].generator = true;
  14872. patch.patcher.nodes[0].channel = nested;
  14873. patch.patcher.nodes[1].effect.samplePath = "C:\\gone\\tune.wav";
  14874. patch.patcher.nodes[1].busHeads.push_back ({});
  14875. patch.patcher.nodes[1].busHeads[0].head = lynx::EffectSlot();
  14876. patch.patcher.nodes[1].busHeads[0].head->samplePath = "C:\\gone\\tune.wav"; // same file: one entry
  14877. song.channels = { patch };
  14878. song.mixer.resize (2);
  14879. lynx::EffectSlot fxPatcher;
  14880. fxPatcher.patcher.nodes.resize (1);
  14881. fxPatcher.patcher.nodes[0].generator = true;
  14882. fxPatcher.patcher.nodes[0].channel = inner;
  14883. fxPatcher.patcher.nodes[0].channel.samplePath = "C:\\gone\\hit.wav";
  14884. song.mixer[1].effects.push_back (fxPatcher);
  14885. const auto missing = lynx::findMissingSamples (song, [] (const std::string&) { return false; });
  14886. CHECK (missing.size() == 3);
  14887. CHECK (missing.size() == 3 && missing[0].path == "C:\\gone\\pad.wav" && missing[0].usedBy[0] == "Rack (Patcher)");
  14888. CHECK (missing.size() == 3 && missing[1].path == "C:\\gone\\tune.wav" && missing[1].usedBy[0] == "Rack (Patcher): effect");
  14889. CHECK (missing.size() == 3 && missing[2].path == "C:\\gone\\hit.wav" && missing[2].usedBy[0] == "Insert 1 (Patcher)");
  14890. CHECK (lynx::applyRelocations (song, { { "C:\\gone\\pad.wav", "/p.wav" }, { "C:\\gone\\tune.wav", "/t.wav" }, { "C:\\gone\\hit.wav", "/h.wav" } }) == 4);
  14891. CHECK (song.channels[0].patcher.nodes[1].busHeads[0].head->samplePath == "/t.wav");
  14892. CHECK (song.channels[0].patcher.nodes[0].channel.patcher.nodes[0].channel.samplePath == "/p.wav");
  14893. CHECK (song.channels[0].patcher.nodes[1].effect.samplePath == "/t.wav");
  14894. CHECK (song.mixer[1].effects[0].patcher.nodes[0].channel.samplePath == "/h.wav");
  14895. }
  14896. TEST (missing_files_folder_search)
  14897. {
  14898. namespace fs = std::filesystem;
  14899. const auto root = fs::temp_directory_path() / ("lynx-missing-test-" + processTempTag());
  14900. fs::remove_all (root);
  14901. fs::create_directories (root / "a" / "deep" / "Kicks");
  14902. fs::create_directories (root / "b");
  14903. for (const auto& f : { root / "a" / "deep" / "Kicks" / "Kick.WAV", root / "b" / "kick.wav", root / "b" / "hat.wav", root / "b" / "unrelated.wav" })
  14904. std::ofstream (f) << "x";
  14905. std::vector<lynx::MissingFile> missing { { "C:\\Old\\Kicks\\kick.wav", { "Kick" }, {}, false }, { "C:\\Old\\hat.wav", { "Hat" }, {}, false },
  14906. { "C:\\Old\\ride.wav", { "Ride" }, {}, false } };
  14907. int listed = 0;
  14908. auto list = [&] (const std::string& folder, const std::function<bool (const std::string&, bool)>& onEntry) {
  14909. for (const auto& entry : fs::directory_iterator (folder))
  14910. {
  14911. ++listed;
  14912. if (! onEntry (entry.path().string(), entry.is_directory()))
  14913. return;
  14914. }
  14915. };
  14916. const auto hits = lynx::searchFolderForMissing (missing, root.string(), list);
  14917. CHECK (hits.size() == 3);
  14918. CHECK (lynx::relocateByName (missing, hits) == 2);
  14919. CHECK (missing[0].found.find ("Kicks") != std::string::npos); // the copy in a "Kicks" folder matches better
  14920. CHECK (missing[1].found.find ("hat.wav") != std::string::npos);
  14921. CHECK (missing[2].found.empty());
  14922. // a bounded walk stops early; a cancelled one finds nothing
  14923. CHECK (lynx::searchFolderForMissing (missing, root.string(), list, 2).size() <= 1);
  14924. CHECK (lynx::searchFolderForMissing (missing, root.string(), list, 0, [] { return true; }).empty());
  14925. // nothing left to find: no walk at all
  14926. missing[2].skipped = true;
  14927. listed = 0;
  14928. CHECK (lynx::searchFolderForMissing (missing, root.string(), list).empty() && listed == 0);
  14929. fs::remove_all (root);
  14930. }
  14931. TEST (missing_files_folder_list)
  14932. {
  14933. const auto folders = lynx::parseFolderList ("C:\\A\r\n C:\\B \n\nc:\\a\nD:/x\n");
  14934. CHECK (folders.size() == 3 && folders[0] == "C:\\A" && folders[1] == "C:\\B" && folders[2] == "D:/x");
  14935. auto list = folders;
  14936. lynx::rememberFolder (list, "D:\\x");
  14937. CHECK (list.size() == 3 && list[0] == "D:\\x" && list[1] == "C:\\A");
  14938. lynx::rememberFolder (list, "E:\\new", 2);
  14939. CHECK (list.size() == 2 && list[0] == "E:\\new" && list[1] == "D:\\x");
  14940. CHECK (lynx::parseFolderList (lynx::folderListText (list)) == list);
  14941. }
  14942. TEST (missing_files_flp_report_and_relocation)
  14943. {
  14944. // an .flp sampler whose sample is gone: listed with its channel; a relocation map makes the import use the copy
  14945. flp::Project project;
  14946. flp::Channel ch;
  14947. ch.index = 0;
  14948. ch.type = flp::ChannelType::sampler;
  14949. ch.name = "Snare";
  14950. ch.samplePath = "C:\\Producer\\Packs\\snare 7.wav";
  14951. project.channels.push_back (ch);
  14952. lynx::FlpImportOptions options;
  14953. options.fileExists = [] (const std::string& p) { return p == "/found/snare 7.wav"; };
  14954. lynx::FlpImportReport report;
  14955. options.report = &report;
  14956. auto song = lynx::importFlp (project, options);
  14957. CHECK (report.missingFiles.size() == 1);
  14958. CHECK (report.missingFiles.size() == 1 && report.missingFiles[0].path == "C:\\Producer\\Packs\\snare 7.wav" && report.missingFiles[0].usedBy[0] == "Snare");
  14959. CHECK (song.channels.size() == 1 && song.channels[0].samplePath.empty());
  14960. lynx::FlpImportReport again;
  14961. options.report = &again;
  14962. options.relocations = { { "C:\\Producer\\Packs\\snare 7.wav", "/found/snare 7.wav" } };
  14963. song = lynx::importFlp (project, options);
  14964. CHECK (again.missingFiles.empty());
  14965. CHECK (song.channels.size() == 1 && song.channels[0].samplePath == "/found/snare 7.wav" && song.channels[0].instrument == lynx::InstrumentKind::sampler);
  14966. }
  14967. TEST (project_info_history_and_flp_import)
  14968. {
  14969. lynx::Song a;
  14970. auto b = a;
  14971. b.info.title = "Night Drive";
  14972. CHECK (lynx::describeChangeText (a, b) == "Project info");
  14973. auto c = a;
  14974. c.beatsPerBar = 3;
  14975. CHECK (lynx::describeChangeText (a, c) == "Time signature");
  14976. flp::Project project;
  14977. project.title = "Song", project.artist = "Me", project.genre = "House", project.url = "https://x", project.comments = "line 1\rline 2";
  14978. const auto song = lynx::importFlp (project);
  14979. CHECK (song.info.title == "Song" && song.info.author == "Me" && song.info.genre == "House" && song.info.url == "https://x");
  14980. CHECK (song.info.comments == "line 1\nline 2");
  14981. }
  14982. #include "UiFixTests.inc"
  14983. // ---------------------------------------------------------------------------
  14984. // Piano Roll tools of round 83 (expected values worked out from FL's formulas, docs/analise/REA_rodada83.md)
  14985. namespace
  14986. {
  14987. lynx::Note prNote (std::int64_t start, std::int64_t length, int key = 60, float velocity = 100.0f / 128.0f)
  14988. {
  14989. lynx::Note n;
  14990. n.start = start;
  14991. n.length = length;
  14992. n.key = key;
  14993. n.velocity = velocity;
  14994. return n;
  14995. }
  14996. std::vector<int> allOf (const lynx::Pattern& p)
  14997. {
  14998. std::vector<int> v;
  14999. for (int i = 0; i < (int) p.notes.size(); ++i)
  15000. v.push_back (i);
  15001. return v;
  15002. }
  15003. } // namespace
  15004. TEST (prtools_score_file)
  15005. {
  15006. using namespace lynx::prtools;
  15007. FlpBuilder b (96);
  15008. b.ascii (0xC7, "20.9.0");
  15009. b.u8 (0x41); b.u16 (0);
  15010. auto notes = noteRecord (0, 0, 24, 60, 100);
  15011. auto n2 = noteRecord (48, 0, 24, 67, 64);
  15012. n2[19] = 4; // colour 4
  15013. notes.insert (notes.end(), n2.begin(), n2.end());
  15014. b.data (0xE0, notes);
  15015. const auto score = parseScore (b.bytes);
  15016. CHECK (score && score->notes.size() == 2);
  15017. if (score && score->notes.size() == 2)
  15018. {
  15019. CHECK (score->notes[0].start == 0 && score->notes[0].length == 240 && score->notes[0].key == 60);
  15020. CHECK (score->notes[1].start == 480 && score->notes[1].colour == 4 && score->notes[1].velocity == 0.5f);
  15021. CHECK (score->length() == 720);
  15022. }
  15023. CHECK (! parseScore (std::vector<std::uint8_t> { 1, 2, 3 }));
  15024. const auto grid = gridScore (240);
  15025. CHECK (grid.notes.size() == 4 && grid.length() == 960 && grid.notes[3].start == 720);
  15026. CHECK (gridScore (240, 1.0f).notes[1].start == 240 + 120);
  15027. if (! realFlpDir.empty()) // the installation's templates
  15028. {
  15029. std::ifstream f (realFlpDir + "/Patches/Scores/Quantization/Default.fsc", std::ios::binary);
  15030. std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (f)), {});
  15031. const auto fsc = parseScore (bytes);
  15032. CHECK (fsc && fsc->notes.size() == 16 && fsc->length() == 3840 && fsc->notes[1].start == 240);
  15033. // an old (FL 3.5, 20-byte records) chopping pattern with colour-3 notes, read as colour 12
  15034. std::ifstream g (realFlpDir + "/Patches/Scores/Chopping/Default.fsc", std::ios::binary);
  15035. std::vector<std::uint8_t> chopBytes ((std::istreambuf_iterator<char> (g)), {});
  15036. const auto chop = parseScore (chopBytes);
  15037. CHECK (chop && ! chop->notes.empty());
  15038. }
  15039. }
  15040. TEST (prtools_quantize)
  15041. {
  15042. using namespace lynx;
  15043. using namespace lynx::prtools;
  15044. const auto grid = gridScore (240);
  15045. Pattern p;
  15046. p.notes = { prNote (1010, 500), prNote (1130, 100) };
  15047. QuantizeOptions o; // start strength 64/128, sensitivity 36 (180 ticks)
  15048. quantize (p, allOf (p), grid, o);
  15049. CHECK (p.notes[0].start == 985 && p.notes[0].length == 500); // 1010 - (50 * 64) / 128
  15050. CHECK (p.notes[1].start == 1165); // toward 1200: 1130 + (70 * 64) / 128
  15051. p.notes = { prNote (1010, 500), prNote (1130, 100) };
  15052. o.start = 1.0f;
  15053. o.sensitivity = 12; // 60 ticks: 1130 is 70 away from 1200
  15054. quantize (p, allOf (p), grid, o);
  15055. CHECK (p.notes[0].start == 960 && p.notes[1].start == 1130);
  15056. p.notes = { prNote (1010, 500) };
  15057. o.sensitivity = 36;
  15058. o.durationMode = QuantizeOptions::leaveEnd;
  15059. quantize (p, allOf (p), grid, o);
  15060. CHECK (p.notes[0].start == 960 && p.notes[0].length == 550); // the end (1509) stays
  15061. p.notes = { prNote (1010, 500) };
  15062. o.durationMode = QuantizeOptions::quantizeEnd;
  15063. o.duration = 1.0f;
  15064. quantize (p, allOf (p), grid, o);
  15065. CHECK (p.notes[0].start == 960 && p.notes[0].length == 1439 - 960 + 1); // end 1509 -> the nearest end 1439
  15066. // levels: a template velocity of 50/100 halves the velocity; 0 removes the note
  15067. Score half = grid;
  15068. for (auto& n : half.notes)
  15069. n.velocity = 50.0f / 128.0f;
  15070. p.notes = { prNote (960, 100, 60, 0.8f) };
  15071. o = {};
  15072. quantize (p, allOf (p), half, o);
  15073. CHECK (std::abs (p.notes[0].velocity - 0.4f) < 1e-5f);
  15074. for (auto& n : half.notes)
  15075. n.velocity = 0.0f;
  15076. quantize (p, allOf (p), half, o);
  15077. CHECK (p.notes.empty());
  15078. }
  15079. TEST (prtools_chop)
  15080. {
  15081. using namespace lynx;
  15082. using namespace lynx::prtools;
  15083. Score pat;
  15084. pat.notes = { ScoreNote { 0, 240, 60 }, ScoreNote { 480, 240, 67 } }; // period 720
  15085. Pattern p;
  15086. p.notes = { prNote (0, 1920, 50) };
  15087. ChopOptions o;
  15088. chop (p, allOf (p), pat, o);
  15089. CHECK (p.notes.size() == 5);
  15090. if (p.notes.size() == 5)
  15091. {
  15092. CHECK (p.notes[0].start == 0 && p.notes[0].key == 50 && p.notes[1].start == 480 && p.notes[1].key == 57);
  15093. CHECK (p.notes[2].start == 720 && p.notes[3].start == 1200 && p.notes[4].start == 1440 && p.notes[4].length == 240);
  15094. }
  15095. // absolute: the pattern stays on the song grid; a piece before the note start is cut
  15096. p.notes = { prNote (100, 600, 50) };
  15097. chop (p, allOf (p), pat, o);
  15098. CHECK (p.notes.size() == 2 && p.notes[0].start == 100 && p.notes[0].length == 140 && p.notes[1].start == 480);
  15099. o.absolute = false;
  15100. p.notes = { prNote (100, 600, 50) };
  15101. chop (p, allOf (p), pat, o);
  15102. CHECK (p.notes.size() == 2 && p.notes[0].start == 100 && p.notes[0].length == 240 && p.notes[1].start == 580
  15103. && p.notes[1].length == 120);
  15104. // time mul x2 (wheel 5); colour 4 holds to the end in the first round only
  15105. o = {};
  15106. o.timeMul = 5;
  15107. p.notes = { prNote (0, 1920, 60) };
  15108. chop (p, allOf (p), pat, o);
  15109. CHECK (p.notes.size() == 3 && p.notes[1].start == 960 && p.notes[1].length == 480 && p.notes[2].start == 1440);
  15110. pat.notes[1].colour = 4;
  15111. o.timeMul = 4;
  15112. p.notes = { prNote (0, 1920, 60) };
  15113. chop (p, allOf (p), pat, o);
  15114. CHECK (p.notes.size() == 4 && p.notes[1].length == 1920 - 480 && p.notes[3].start == 1440);
  15115. }
  15116. TEST (prtools_flam)
  15117. {
  15118. using namespace lynx;
  15119. using namespace lynx::prtools;
  15120. Pattern p;
  15121. p.notes = { prNote (960, 480, 60, 0.75f) };
  15122. flam (p, allOf (p), flamPreset (FlamPreset::doubleStroke), 120.0); // +1 step after
  15123. CHECK (p.notes.size() == 2 && p.notes[0].start == 960 && p.notes[1].start == 1200 && p.notes[1].length == 480);
  15124. CHECK (std::abs (p.notes[1].velocity - 0.75f * 85.0f / 128.0f) < 1e-6f);
  15125. p.notes = { prNote (960, 480) };
  15126. flam (p, allOf (p), flamPreset (FlamPreset::earlyDoubleStroke), 120.0); // the flam one step before, shortened
  15127. CHECK (p.notes.size() == 2 && p.notes[0].start == 720 && p.notes[0].length == 240 && p.notes[1].start == 960);
  15128. // absolute: -64 = -33.3 ms = -64 ticks at 120 BPM (960 PPQ); the original moves, the flam stays on the grid
  15129. CHECK (flamOffset (flamPreset (FlamPreset::flam), 120.0) == -64);
  15130. p.notes = { prNote (960, 480) };
  15131. flam (p, allOf (p), flamPreset (FlamPreset::flam), 120.0);
  15132. CHECK (p.notes.size() == 2 && p.notes[0].start == 960 && p.notes[0].length == 64 && p.notes[1].start == 1024);
  15133. p.notes = { prNote (960, 480) };
  15134. flam (p, allOf (p), flamPreset (FlamPreset::earlyFlam), 120.0);
  15135. CHECK (p.notes.size() == 2 && p.notes[0].start == 896 && p.notes[1].start == 960 && p.notes[1].length == 480);
  15136. }
  15137. TEST (prtools_lfo)
  15138. {
  15139. using namespace lynx;
  15140. using namespace lynx::prtools;
  15141. CHECK (lfoPeriod (20480) == 3840.0 && lfoPeriod (0) == 960.0 * 128 && lfoPeriod (40960) == 120.0);
  15142. LfoOptions o; // sine, phase 1/2: starts at the bottom (value 1/2, range 1/4)
  15143. const auto c = lfoCurve (o, 3840);
  15144. CHECK (std::abs (c[0] - 0.25f) < 1e-4f && std::abs (c[960] - 0.5f) < 1e-3f && std::abs (c[1920] - 0.75f) < 1e-3f);
  15145. o.shape = LfoOptions::square;
  15146. o.phase = 0; // phase 0.5: Round (0.5) = 0 (half to even) -> +1, then -1
  15147. const auto sq = lfoCurve (o, 3840);
  15148. CHECK (sq[0] == 0.75f && sq[10] == 0.25f && sq[2000] == 0.75f);
  15149. o = {};
  15150. o.useEnd = true;
  15151. o.endRange = 0; // the range fades out
  15152. const auto fade = lfoCurve (o, 3840);
  15153. CHECK (std::abs (fade[3839] - 0.5f) < 0.01f);
  15154. Pattern p;
  15155. p.notes = { prNote (0, 100), prNote (960, 100), prNote (1920, 100) };
  15156. lfo (p, allOf (p), notetools::Level::velocity, LfoOptions {});
  15157. CHECK (std::abs (p.notes[0].velocity - 0.25f) < 1e-4f && std::abs (p.notes[1].velocity - 0.5f) < 1e-3f
  15158. && std::abs (p.notes[2].velocity - 0.75f) < 1e-3f);
  15159. }
  15160. TEST (prtools_claw)
  15161. {
  15162. using namespace lynx;
  15163. using namespace lynx::prtools;
  15164. ClawOptions o; // beat, trash the 4th quarter, stretch, remove short notes
  15165. Pattern p;
  15166. p.notes = { prNote (0, 240), prNote (960, 240), prNote (750, 100), prNote (1440, 240) };
  15167. claw (p, allOf (p), o);
  15168. CHECK (p.notes.size() == 3); // the note inside the trashed slice is gone
  15169. if (p.notes.size() == 3)
  15170. {
  15171. CHECK (p.notes[0].start == 0 && p.notes[0].length == 320);
  15172. CHECK (p.notes[1].start == 960 && p.notes[1].length == 320);
  15173. CHECK (p.notes[2].start == 1600 && p.notes[2].length == 320); // 1440 - 240 = 1200 -> x 4/3
  15174. }
  15175. o.trash = 9; // none: only the warp, which is neutral
  15176. p.notes = { prNote (100, 300) };
  15177. claw (p, allOf (p), o);
  15178. CHECK (p.notes.size() == 1 && p.notes[0].start == 100 && p.notes[0].length == 300);
  15179. o.timeDistortion = 1024; // strong bend: the period's ends stay, the middle moves
  15180. p.notes = { prNote (0, 480), prNote (960, 480) };
  15181. claw (p, allOf (p), o);
  15182. CHECK (p.notes[0].start == 0 && p.notes[1].start == 960 && p.notes[0].length > 480 && p.notes[1].length > 480);
  15183. o.trash = 0; // the first quarter
  15184. o.timeDistortion = 0;
  15185. o.stretch = false;
  15186. p.notes = { prNote (0, 240), prNote (240, 240) };
  15187. claw (p, allOf (p), o);
  15188. CHECK (p.notes.size() == 1 && p.notes[0].start == 0 && p.notes[0].length == 240);
  15189. }
  15190. TEST (prtools_articulate)
  15191. {
  15192. using namespace lynx;
  15193. using namespace lynx::prtools;
  15194. Pattern p;
  15195. p.notes = { prNote (0, 100), prNote (480, 100), prNote (960, 100) };
  15196. articulate (p, allOf (p), articulatePreset (ArticulatePreset::legato), 4);
  15197. CHECK (p.notes[0].length == 480 && p.notes[1].length == 480 && p.notes[2].length == 3840 - 960); // to the bar line
  15198. p.notes = { prNote (0, 100), prNote (480, 100), prNote (960, 2880) };
  15199. articulate (p, allOf (p), articulatePreset (ArticulatePreset::staccato), 4);
  15200. CHECK (p.notes[0].length == 96 && p.notes[1].length == 96 && p.notes[2].length == 576); // ends on a bar: own length
  15201. auto o = articulatePreset (ArticulatePreset::legato);
  15202. o.gap = 20;
  15203. p.notes = { prNote (0, 100), prNote (480, 100) };
  15204. articulate (p, allOf (p), o, 4);
  15205. CHECK (p.notes[0].length == 460);
  15206. // chop chords: a held note is cut where the next one starts
  15207. p.notes = { prNote (0, 960, 48), prNote (480, 480, 60) };
  15208. articulate (p, allOf (p), articulatePreset (ArticulatePreset::chopChordsOnly), 4);
  15209. CHECK (p.notes.size() == 3 && p.notes[0].length == 480 && p.notes[1].start == 480 && p.notes[2].start == 480);
  15210. // variation is seeded (MT19937): same seed, same result; lengths only get shorter
  15211. o = articulatePreset (ArticulatePreset::legato);
  15212. o.variation = 50;
  15213. o.seed = 7;
  15214. Pattern a, b2;
  15215. a.notes = b2.notes = { prNote (0, 100), prNote (480, 100), prNote (960, 100) };
  15216. articulate (a, allOf (a), o, 4);
  15217. articulate (b2, allOf (b2), o, 4);
  15218. CHECK (a.notes[0].length == b2.notes[0].length && a.notes[0].length <= 480 && a.notes[0].length >= 240);
  15219. }
  15220. TEST (prtools_chords)
  15221. {
  15222. using namespace lynx::prtools;
  15223. auto c = parseProgression ("I-V-vi-IV", 0, false);
  15224. CHECK (c && c->size() == 4);
  15225. if (c && c->size() == 4)
  15226. {
  15227. CHECK ((*c)[0].root == 0 && (*c)[1].root == 7 && (*c)[2].root == 9 && (*c)[3].root == 5);
  15228. CHECK ((*c)[2].intervals == std::vector<int> ({ 0, 3, 7 }) && chordName ((*c)[2]) == "Am");
  15229. }
  15230. c = parseProgression ("ii 7-V 7-I \xE2\x88\x86-IV/\xE2\x99\xADVII", 0, false);
  15231. CHECK (c && c->size() == 4);
  15232. if (c && c->size() == 4)
  15233. {
  15234. CHECK ((*c)[0].intervals == std::vector<int> ({ 0, 3, 7, 10 }) && chordName ((*c)[0]) == "Dm7");
  15235. CHECK ((*c)[2].intervals == std::vector<int> ({ 0, 4, 7, 11 }) && chordName ((*c)[2]) == "Cmaj7");
  15236. CHECK ((*c)[3].root == 5 && (*c)[3].bass == 10 && chordName ((*c)[3]) == "F/A#");
  15237. }
  15238. c = parseProgression ("i-v-\xE2\x99\xADVI-iv", 9, true); // A minor: Am Em F Dm
  15239. CHECK (c && c->size() == 4 && (*c)[2].root == 5 && (*c)[3].root == 2 && (*c)[1].intervals[1] == 3);
  15240. CHECK (parseProgression ("Isus2 add9", 0, false) && parseProgression ("Isus2 add9", 0, false)->front().intervals
  15241. == std::vector<int> ({ 0, 2, 7, 14 }));
  15242. CHECK (! parseProgression ("I-X-V", 0, false));
  15243. const auto presets = parseProgressionPresets ("-Major\r\n\r\nI-V-vi-IV: Hopeful\r\nI-vi-IV-V: Optimistic\r\n\r\n-Minor\r\n\r\ni-iv-v-i\r\n");
  15244. CHECK (presets.size() == 3 && presets[0].numerals == "I-V-vi-IV" && ! presets[0].minor && presets[2].minor);
  15245. const auto rhythms = parseRhythmPresets ("Default,4,>,-,-,-,>,-,-,-,>,-,-,-,>,-,-,-\nRandom 1,4,r,16,2,1.5\n");
  15246. CHECK (rhythms.size() == 1 && rhythms[0].cells == ">--->--->--->---");
  15247. const auto g = generateProgression (0, false, 8, 0.3f, 5);
  15248. CHECK (g.size() == 8 && g[0].root == 0 && g == generateProgression (0, false, 8, 0.3f, 5));
  15249. for (std::uint32_t seed = 1; seed < 40; ++seed) // numerals read back as the same chords
  15250. {
  15251. const auto gen = generateProgression (2, seed % 2 == 0, 8, 1.0f, seed);
  15252. const auto back = parseProgression (toNumerals (gen, 2), 2, seed % 2 == 0);
  15253. CHECK (back && *back == gen);
  15254. }
  15255. CHECK (toNumerals (*parseProgression ("I-V-vi-IV/bVII", 0, false), 0) == "I-V-vi-IV/bVII");
  15256. lynx::Pattern p;
  15257. const auto chords = *parseProgression ("I-IV", 0, false);
  15258. ChordWriteOptions w;
  15259. const auto added = writeProgression (p, 2, 0, chords, w);
  15260. CHECK (added.size() == 8 && p.notes.size() == 8 && p.notes[0].channel == 2);
  15261. CHECK (std::any_of (p.notes.begin(), p.notes.end(), [] (auto& n) { return n.key == 48; })); // bass C3
  15262. w.rhythm = ">-`->-`->-`->-`-";
  15263. lynx::Pattern r;
  15264. writeProgression (r, 0, 0, chords, w);
  15265. CHECK (r.notes.size() == 8 && r.notes[0].length == 1920); // '>' + '-' of 960-tick cells
  15266. }
  15267. TEST (prtools_riff_machine)
  15268. {
  15269. using namespace lynx;
  15270. using namespace lynx::prtools;
  15271. Pattern p;
  15272. p.notes = { prNote (0, 100, 70) };
  15273. p.notes[0].channel = 1;
  15274. RiffOptions o;
  15275. o.channel = 1;
  15276. o.bars = 2;
  15277. const auto notes = riffMachine (p, { 0 }, o, 4);
  15278. CHECK (! notes.empty() && notes.size() == p.notes.size());
  15279. CHECK (std::all_of (p.notes.begin(), p.notes.end(), [] (auto& n) {
  15280. return n.channel == 1 && n.start >= 0 && n.start < 2 * 3840 && n.key >= 36 && n.key <= 84;
  15281. }));
  15282. // only the first step: the progression shape, chopped from one note at key 48
  15283. o.stepsUpTo = 0;
  15284. Pattern q;
  15285. const auto first = riffMachine (q, {}, o, 4);
  15286. CHECK (first.size() == builtInRiffProgression (0).notes.size() * 2 && q.notes[0].key == 48);
  15287. }
  15288. TEST (event_editor_lanes)
  15289. {
  15290. using namespace lynx;
  15291. Pattern p;
  15292. const AutomationTarget vol { AutomationTargetKind::channelVolume, 2 };
  15293. auto& lane = eventedit::laneFor (p, vol);
  15294. CHECK (p.events.size() == 1 && &eventedit::laneFor (p, vol) == &lane && eventedit::findLane (p, { AutomationTargetKind::channelPan, 2 }) == nullptr);
  15295. eventedit::fill (lane, 0, 960, 0.25f, 240);
  15296. CHECK (lane.events.size() == 4 && lane.events[3].tick == 720 && lane.events[3].value == 0.25f);
  15297. CHECK (lane.valueAt (-5.0) == 0.25f && lane.valueAt (5000.0) == 0.25f);
  15298. eventedit::line (lane, 960, 1920, 0.0f, 1.0f, 240); // 4 cells: 0, 1/3, 2/3, 1
  15299. CHECK (lane.events.size() == 8 && lane.events[4].value == 0.0f && lane.events[7].value == 1.0f);
  15300. CHECK (std::abs (lane.valueAt (1500.0) - 2.0f / 3.0f) < 1e-6f); // held from 1440
  15301. eventedit::setEvent (lane, 1440, 0.1f);
  15302. CHECK (lane.events.size() == 8 && lane.valueAt (1440.0) == 0.1f);
  15303. eventedit::setEvent (lane, 100, 0.9f);
  15304. CHECK (lane.events.size() == 9 && lane.events[1].tick == 100);
  15305. eventedit::scale (lane, 0, 960, 2.0f, 0.0f, false);
  15306. CHECK (lane.events[0].value == 0.5f && lane.events[1].value == 1.0f); // clamped
  15307. eventedit::scale (lane, 0, 960, 0.0f, 0.0f, true); // bipolar: toward the centre
  15308. CHECK (lane.events[0].value == 0.5f && lane.events[2].value == 0.5f);
  15309. eventedit::erase (lane, 0, 960);
  15310. CHECK (lane.events.size() == 4 && lane.events.front().tick == 960);
  15311. auto copy = lane;
  15312. eventedit::randomize (lane, 0, 4000, 0.2f, 3);
  15313. eventedit::randomize (copy, 0, 4000, 0.2f, 3);
  15314. CHECK (lane.events[1].value == copy.events[1].value && std::abs (lane.events[1].value - 1.0f / 3.0f) <= 0.2f + 1e-6f);
  15315. prtools::LfoOptions o;
  15316. eventedit::applyLfo (lane, 0, 3840, o, 960); // FL's LFO: bottom, middle, top, middle
  15317. CHECK (lane.events.size() == 4 && std::abs (lane.events[0].value - 0.25f) < 1e-4f && std::abs (lane.events[2].value - 0.75f) < 1e-3f);
  15318. p.events.push_back ({ { AutomationTargetKind::channelPan, 1 }, {} });
  15319. eventedit::removeEmptyLanes (p);
  15320. CHECK (p.events.size() == 1);
  15321. // undo label
  15322. auto before = Song::makeDefault();
  15323. auto after = before;
  15324. after.patterns[0].events.push_back ({ vol, { { 0, 0.5f } } });
  15325. CHECK (describeChangeText (before, after) == "Edit events: " + after.patterns[0].name);
  15326. // channel removal re-targets the lanes like automations
  15327. auto song = Song::makeDefault();
  15328. song.patterns[0].events.push_back ({ { AutomationTargetKind::channelVolume, 3 }, { { 0, 0.5f } } });
  15329. song.removeChannel (1);
  15330. CHECK (song.patterns[0].events[0].target.index == 2);
  15331. }
  15332. TEST (event_editor_playback)
  15333. {
  15334. // pattern events on the Master volume: silent first half of each pattern, sound in the second half
  15335. auto song = lynx::Song::makeDefault();
  15336. song.bpm = 120.0;
  15337. const auto bar = (std::int64_t) lynx::ppq * 4;
  15338. song.patterns[0].lengthTicks = bar;
  15339. song.patterns[0].notes.clear();
  15340. for (int s = 0; s < 16; ++s)
  15341. song.patterns[0].notes.push_back ({ s * 240, 200, 0, 60 });
  15342. song.patterns[0].events.push_back ({ { lynx::AutomationTargetKind::mixerVolume, 0 }, { { 0, 0.0f }, { bar / 2, 0.8f } } });
  15343. const auto peaks = [&] (lynx::PlayMode mode) {
  15344. song.mode = mode;
  15345. song.playlist.clear();
  15346. song.playlist.push_back ({ 0, 0, bar, bar, 0 }); // one clip, in the second bar
  15347. const double sr = 48000.0;
  15348. auto state = lynx::RackState::build (song, nullptr, sr);
  15349. lynx::RackRenderer r;
  15350. r.prepare (sr, 512);
  15351. std::vector<float> l (512), rr (512);
  15352. const auto tps = lynx::ticksPerSample (song.bpm, sr);
  15353. std::array<float, 6> peak {}; // half bars
  15354. for (double tick = 0.0; tick < (double) (bar * 3); tick += 512 * tps)
  15355. {
  15356. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  15357. // blocks inside a half bar, away from its edges (the gain glides over one chunk at a change)
  15358. const auto blockEnd = tick + 512 * tps;
  15359. const auto half = (std::size_t) (tick / (double) (bar / 2));
  15360. const auto edge = (double) half * (double) (bar / 2);
  15361. if (half > 5 || tick - edge < 2 * 512 * tps || blockEnd > edge + (double) (bar / 2))
  15362. continue;
  15363. for (auto v : l)
  15364. peak[half] = std::max (peak[half], std::abs (v));
  15365. }
  15366. return std::make_pair (state->eventLanes.size(), peak);
  15367. };
  15368. const auto [lanesSong, song3] = peaks (lynx::PlayMode::song);
  15369. CHECK (lanesSong == 1);
  15370. CHECK (song3[2] < 1.0e-4f && song3[3] > 0.01f); // inside the clip: silent, then sound
  15371. const auto [lanesPattern, loop] = peaks (lynx::PlayMode::pattern);
  15372. CHECK (lanesPattern == 1);
  15373. CHECK (loop[0] < 1.0e-4f && loop[1] > 0.01f && loop[2] < 1.0e-4f && loop[3] > 0.01f);
  15374. }
  15375. TEST (flp_pattern_events_import)
  15376. {
  15377. // REA_rodada85 §1: event 0xDF holds a pattern's events {u32 tick, u32 controlID, i32 raw value}, 0xD8 the values
  15378. // reset when the song starts; they become pattern event lanes in FL's normalised scale
  15379. FlpBuilder b;
  15380. b.u8 (0x40); b.u16 (0); // channel 0
  15381. b.u8 (0x15); b.u8 (0); // sampler
  15382. b.u8 (0x41); b.u16 (1); // pattern 1: events only, no notes
  15383. auto rec = [] (std::vector<std::uint8_t>& v, std::uint32_t tick, std::uint32_t id, std::int32_t value) {
  15384. for (auto w : { tick, id, (std::uint32_t) value })
  15385. for (int i = 0; i < 4; ++i)
  15386. v.push_back ((std::uint8_t) (w >> (8 * i)));
  15387. };
  15388. std::vector<std::uint8_t> events;
  15389. rec (events, 0, 0x70001FC0u, 16000); // Master volume: top
  15390. rec (events, 96, 0x70001FC0u, 8000);
  15391. rec (events, 96, 0x70001FC0u, 4000); // same tick: the last one wins
  15392. rec (events, 0, 0x00000001u, 0); // channel 0 pan: hard left
  15393. rec (events, 48, 0x00000001u, 12800); // hard right
  15394. rec (events, 0, 0x00008000u, 5); // a plugin parameter of a sampler: scale unknown, skipped
  15395. b.data (0xDF, events);
  15396. std::vector<std::uint8_t> init;
  15397. rec (init, 0, 0x70001FC1u, -6400); // Master pan reset to hard left at song start
  15398. b.data (0xD8, init);
  15399. const auto project = flp::parseProject (b.bytes);
  15400. CHECK (project.initControls.size() == 1);
  15401. const flp::Pattern* src = nullptr;
  15402. for (const auto& p : project.patterns)
  15403. if (p.index == 1)
  15404. src = &p;
  15405. CHECK (src != nullptr && src->controlEvents.size() == 6);
  15406. lynx::FlpImportReport report;
  15407. lynx::FlpImportOptions options;
  15408. options.report = &report;
  15409. const auto song = lynx::importFlp (project, options);
  15410. const lynx::PatternEvents* volume = nullptr;
  15411. const lynx::PatternEvents* pan = nullptr;
  15412. int lanes = 0;
  15413. for (const auto& p : song.patterns)
  15414. for (const auto& lane : p.events)
  15415. {
  15416. ++lanes;
  15417. if (lane.target.kind == lynx::AutomationTargetKind::mixerVolume && lane.target.index == 0)
  15418. volume = &lane;
  15419. if (lane.target.kind == lynx::AutomationTargetKind::channelPan)
  15420. pan = &lane;
  15421. }
  15422. CHECK (lanes == 2 && volume != nullptr && pan != nullptr);
  15423. if (volume != nullptr && volume->events.size() == 2)
  15424. {
  15425. CHECK (volume->events[0].tick == 0 && volume->events[1].tick == lynx::ppq);
  15426. CHECK (volume->events[0].value > volume->events[1].value); // 4000 (not 8000) is the one kept
  15427. }
  15428. else
  15429. CHECK (false);
  15430. if (pan != nullptr && pan->events.size() == 2)
  15431. CHECK (pan->events[0].value < 0.01f && pan->events[1].value > 0.99f && pan->events[1].tick == lynx::ppq / 2);
  15432. else
  15433. CHECK (false);
  15434. CHECK (report.converted["Pattern events"] == 2);
  15435. CHECK (song.targetValue ({ lynx::AutomationTargetKind::mixerPan, 0 }) < 0.01f);
  15436. }
  15437. #include "PrefsTests.inc"
  15438. #include "core/ProjectSafety.h"
  15439. #include "core/SearchIndex.h"
  15440. #include "core/PluginCatalog.h"
  15441. #include "SafetyTests118.inc"
  15442. #include "Rea134Tests.inc"
  15443. #include "Rea139Tests.inc"
  15444. #include "Rea143Tests.inc"
  15445. #include "Rea143aTests.inc"
  15446. #include "Rea143bTests.inc"
  15447. #include "Rea146aTests.inc"
  15448. #include "Rea146bTests.inc"
  15449. #include "Rea146Tests.inc"
  15450. #include "Rea149Tests.inc"
  15451. #include "PluginCatalogTests151.inc"
  15452. #include "Rea151Tests.inc"
  15453. #include "AudioEditTests.inc"
  15454. #include "AudioEditToolsTests.inc"
  15455. #include "InputTests.inc"
  15456. #include "../asio/InputDrift.h"
  15457. #include "AsioDriftTests.inc"
  15458. TEST (level_audit_demo_imports_round89)
  15459. {
  15460. // REA_rodada89 §4: two demo projects whose FL renders were much louder than LYNX's
  15461. if (realFlpDir.empty())
  15462. return;
  15463. auto load = [] (const std::filesystem::path& path) -> std::optional<lynx::Song> {
  15464. if (! std::filesystem::exists (path))
  15465. return std::nullopt;
  15466. std::ifstream in (path, std::ios::binary);
  15467. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  15468. return lynx::importFlp (flp::parseProject (file));
  15469. };
  15470. const auto demos = std::filesystem::path (realFlpDir) / "Demo projects";
  15471. // FL 12+: a route to insert 100+ without a level record plays at 100% (the percussion bus of "Lightning")
  15472. if (const auto song = load (demos / "Product demos" / "Square a Saw - Lightning (VEELA Vocals Resynthesized).flp"))
  15473. {
  15474. int highRoutes = 0, highAtZero = 0;
  15475. for (const auto& t : song->mixer)
  15476. for (const auto& send : t.sends)
  15477. if (send.target >= 100)
  15478. {
  15479. ++highRoutes;
  15480. highAtZero += send.level <= 0.0f;
  15481. }
  15482. std::printf (" Lightning: %d routes to inserts 100+, %d at level 0\n", highRoutes, highAtZero);
  15483. CHECK (highRoutes > 0 && highAtZero == 0);
  15484. }
  15485. // Fruity Compressors FL saved as VST2 inside Fruity Wrapper (RawFL): the exact engine with the saved controls
  15486. if (const auto song = load (demos / "Visual" / "ZGameEditor Visualizer" / "Treben - Gimbal and Sinan - RawFL.flp"))
  15487. {
  15488. int compressors = 0, withSettings = 0;
  15489. for (const auto& t : song->mixer)
  15490. for (const auto& fx : t.effects)
  15491. if (fx.kind == lynx::EffectKind::fruityCompressor)
  15492. {
  15493. ++compressors;
  15494. const auto defaults = lynx::makeEffectSlot (lynx::EffectKind::fruityCompressor);
  15495. withSettings += fx.params != defaults.params;
  15496. }
  15497. std::printf (" RawFL: %d wrapped Fruity Compressors on the exact engine, %d with their own settings\n", compressors, withSettings);
  15498. CHECK (compressors >= 10 && withSettings >= 8);
  15499. }
  15500. }
  15501. #include "UiLook90Tests.inc"
  15502. #include "UiFl104Tests.inc"
  15503. #include "DialogsTests84.inc"
  15504. #include "SubstituteTests.inc"
  15505. #include "MixSuiteTests96.inc"
  15506. #include "PanelsTests105.inc"
  15507. // --- native plugin editors (round 93): FL's arrangements ---------------------------------------------------------
  15508. TEST (plugin_layouts_name_real_parameters)
  15509. {
  15510. using namespace lynx;
  15511. // every value control edits a parameter of its effect, inside the body; FL arrangements place (or list in a menu)
  15512. // every parameter of the effect
  15513. for (const auto kind : plugui::flLayoutKinds())
  15514. {
  15515. const auto& l = plugui::layoutFor (kind);
  15516. const auto& info = effectInfo (kind);
  15517. CHECK (plugui::hasFlLayout (kind));
  15518. // FL's own size wins for its one-cell editors (Fruity Center, Fruity Phase Inverter: 170 x 55)
  15519. const auto minHeight = kind == EffectKind::flCenter || kind == EffectKind::flPhaseInverter ? 50 : 60;
  15520. if (! (l.width > 100 && l.height > minHeight))
  15521. std::printf (" %s: %d x %d\n", info.name, l.width, l.height);
  15522. CHECK (l.width > 100 && l.height > minHeight);
  15523. std::vector<bool> placed ((std::size_t) plugui::paramCount (kind), false);
  15524. for (const auto& item : l.items)
  15525. {
  15526. if (! (item.x >= 0.0f && item.y >= 0.0f && item.x + item.w <= (float) l.width + 0.5f && item.y + item.h <= (float) l.height + 0.5f))
  15527. std::printf (" %s: item '%s' leaves the body\n", info.name, item.param != nullptr ? item.param : item.label != nullptr ? item.label : "");
  15528. CHECK (item.x >= 0.0f && item.y >= 0.0f && item.x + item.w <= (float) l.width + 0.5f && item.y + item.h <= (float) l.height + 0.5f);
  15529. if (item.param != nullptr)
  15530. {
  15531. const auto p = plugui::paramIndex (kind, item.param);
  15532. CHECK (p >= 0);
  15533. if (p >= 0)
  15534. placed[(std::size_t) p] = true;
  15535. }
  15536. if (item.type == plugui::Item::radio || item.type == plugui::Item::tabs || item.type == plugui::Item::menu)
  15537. {
  15538. const auto labels = plugui::optionLabels (item);
  15539. CHECK (labels.size() >= 2);
  15540. CHECK (item.values.empty() || item.values.size() == labels.size());
  15541. }
  15542. if (item.type == plugui::Item::knob && item.h + 0.5f < item.w) // (a caption above its knob would hide it)
  15543. {
  15544. std::printf (" %s: knob '%s' is shorter than it is wide\n", info.name, item.param != nullptr ? item.param : "");
  15545. CHECK (false);
  15546. }
  15547. CHECK (item.param2 == nullptr || plugui::paramIndex (kind, item.param2) >= 0);
  15548. if (item.param2 != nullptr && plugui::paramIndex (kind, item.param2) >= 0)
  15549. placed[(std::size_t) plugui::paramIndex (kind, item.param2)] = true;
  15550. for (const auto* when : { item.whenParam, item.when2Param, item.when3Param })
  15551. CHECK (when == nullptr || plugui::paramIndex (kind, when) >= 0);
  15552. }
  15553. for (const auto* name : l.menuSwitches)
  15554. {
  15555. const auto p = plugui::paramIndex (kind, name);
  15556. CHECK (p >= 0);
  15557. if (p >= 0)
  15558. placed[(std::size_t) p] = true;
  15559. }
  15560. for (const auto* name : l.editedElsewhere)
  15561. {
  15562. const auto p = plugui::paramIndex (kind, name);
  15563. CHECK (p >= 0);
  15564. if (p >= 0)
  15565. placed[(std::size_t) p] = true;
  15566. }
  15567. for (std::size_t p = 0; p < placed.size(); ++p)
  15568. if (! placed[p])
  15569. {
  15570. std::printf (" %s: parameter '%s' has no control\n", info.name, plugui::paramName (kind, (int) p));
  15571. CHECK (false);
  15572. }
  15573. }
  15574. }
  15575. TEST (maximus_editor_edits_the_slot_state)
  15576. {
  15577. using namespace lynx;
  15578. // the Maximus page edits the 59 parameters stored in the slot's state (REA_rodada8 §1.2)
  15579. std::array<std::array<int, 13>, 4> bands {};
  15580. for (auto& b : bands)
  15581. b = { 0, 1000, 1000, 0, 0, 1000, 1115, 0, 2, 5000, 3, 2882, 0 };
  15582. std::array<std::vector<std::array<double, 4>>, 8> envs;
  15583. for (auto& e : envs)
  15584. e = { { 0.0, 0.0, 0.0, 2 }, { 0.5, 0.5, 0.0, 2 }, { 0.5, 0.5, 0.0, 2 } };
  15585. const auto state = maximusTestState ({ 19800, 1, 48065, 1, 1115, 1000, 32768 }, bands, envs, 0);
  15586. const auto slot = maximusfx::pack ({ hlfx::MaximusBuild::shared, -1, state });
  15587. CHECK (maximusfx::stateParams().size() == 59);
  15588. CHECK (plugui::paramIndex (EffectKind::maximus, "Master release") == 7 + 13 * 3 + 9);
  15589. CHECK (maximusfx::stateParam (slot, 0).value_or (-1) == 19800);
  15590. CHECK (maximusfx::stateParam (slot, 7 + 13 + 9).value_or (-1) == 5000);
  15591. CHECK (maximusfx::stateParamText (0, 19800) == "200 Hz");
  15592. CHECK (maximusfx::stateParamText (2, 48065) == "3000 Hz");
  15593. CHECK (maximusfx::stateParamText (8, 1000) == "+0.0 dB");
  15594. CHECK (maximusfx::stateParamText (7 + 6, 0) == "Default");
  15595. CHECK (maximusfx::stateParamText (7 + 13 * 3 + 6, 1115) == "2.00 ms");
  15596. CHECK (maximusfx::stateParamText (6, -1) == "Off");
  15597. const auto edited = maximusfx::withStateParam (slot, 7 + 13 + 1, 2000); // mid pre gain → +18 dB
  15598. CHECK (maximusfx::stateParam (edited, 7 + 13 + 1).value_or (-1) == 2000);
  15599. CHECK (maximusfx::stateParam (edited, 7 + 1).value_or (-1) == 1000);
  15600. CHECK (maximusfx::stateParam (maximusfx::withStateParam (slot, 0, 99999), 0).value_or (-1) == 65536); // clamped
  15601. const auto blob = maximusfx::unpack (edited);
  15602. const auto original = maximusfx::unpack (slot);
  15603. CHECK (blob && original && (hlfx::makeMaximusPreset (blob->state) != nullptr) == (hlfx::makeMaximusPreset (original->state) != nullptr));
  15604. const auto curve = maximusfx::curvePoints (slot, 3);
  15605. CHECK (curve.size() == 3 && std::abs (curve[1].first - 0.5f) < 1e-6f && std::abs (curve[2].first - 1.0f) < 1e-6f);
  15606. CHECK (maximusfx::selectedBand (slot) >= 0);
  15607. CHECK (plugui::hasFlLayout (EffectKind::maximus));
  15608. }
  15609. TEST (plugin_layout_options_match_the_engine_values)
  15610. {
  15611. using namespace lynx;
  15612. // the switches show FL's captions for the engine's own values
  15613. const auto text = [] (EffectKind kind, const char* param, int option) {
  15614. for (const auto& item : plugui::layoutFor (kind).items)
  15615. if (item.param != nullptr && std::strcmp (item.param, param) == 0 && item.options != nullptr)
  15616. return formatEffectParam (kind, plugui::paramIndex (kind, param), plugui::optionValue (item, option), 120.0);
  15617. return std::string ("?");
  15618. };
  15619. CHECK (text (EffectKind::fruityDelay3, "Delay type", 0) == "Fake stereo");
  15620. CHECK (text (EffectKind::fruityDelay3, "Delay type", 2) == "Ping pong");
  15621. CHECK (text (EffectKind::fruityDelay3, "Delay type", 3) == "Off");
  15622. CHECK (text (EffectKind::fruityDelay3, "Filter mode", 1) == "High pass");
  15623. CHECK (text (EffectKind::fruityDelay3, "Saturation type", 1) == "Saturate");
  15624. CHECK (text (EffectKind::fruityMultiband, "Low mode", 1) == "off");
  15625. CHECK (text (EffectKind::fruityMultiband, "High mode", 2) == "bypass");
  15626. CHECK (text (EffectKind::luxeVerb, "Pitch type", 1) == "feed-forward");
  15627. CHECK (text (EffectKind::luxeVerb, "Freeze mode", 2) == "sustain");
  15628. CHECK (text (EffectKind::stereoEnhancer, "Invert", 2) == "invert R");
  15629. CHECK (text (EffectKind::parametricEq, "B3 type", 4) == formatEffectParam (EffectKind::parametricEq, peq::param (2, 0), peq::typeValue (peq::BandType::lowCut), 120.0));
  15630. // the option nearest to a value; pages and conditions
  15631. plugui::LayoutItem radio;
  15632. radio.type = plugui::Item::radio;
  15633. radio.options = "A|B|C|D";
  15634. CHECK (plugui::optionLabels (radio).size() == 4);
  15635. CHECK (plugui::optionAt (radio, 0.30f) == 1);
  15636. CHECK (plugui::optionAt (radio, 0.95f) == 3);
  15637. CHECK (std::abs (plugui::optionValue (radio, 2) - 2.0f / 3.0f) < 1e-6f);
  15638. const auto& lux = plugui::layoutFor (EffectKind::luxeVerb);
  15639. auto params = makeEffectSlot (EffectKind::luxeVerb).params;
  15640. const auto pitchType = plugui::paramIndex (EffectKind::luxeVerb, "Pitch type");
  15641. int feedForward = 0, feedback = 0;
  15642. for (float v : { 0.0f, 1.0f })
  15643. {
  15644. params[(std::size_t) pitchType] = v;
  15645. for (const auto& item : lux.items)
  15646. if (item.param != nullptr && std::strcmp (item.param, "Feed-forward pitch 1") == 0 && plugui::isShown (EffectKind::luxeVerb, item, 0, params))
  15647. ++feedForward;
  15648. else if (item.param != nullptr && std::strcmp (item.param, "Feedback pitch") == 0 && plugui::isShown (EffectKind::luxeVerb, item, 0, params))
  15649. ++feedback;
  15650. }
  15651. CHECK (feedForward == 1); // only in feed-forward mode
  15652. CHECK (feedback == 2); // value plate + fader, only in feedback mode
  15653. const auto& limiter = plugui::layoutFor (EffectKind::fruityLimiter);
  15654. CHECK (limiter.pages.size() == 2);
  15655. int ceilingShown = 0;
  15656. for (int page = 0; page < 2; ++page)
  15657. for (const auto& item : limiter.items)
  15658. if (item.param != nullptr && std::strcmp (item.param, "Ceiling") == 0 && plugui::isShown (EffectKind::fruityLimiter, item, page, params))
  15659. ++ceilingShown;
  15660. CHECK (ceilingShown == 1); // LIMIT page only
  15661. // every other effect still gets a (generated) window
  15662. CHECK (! plugui::hasFlLayout (EffectKind::distortion));
  15663. CHECK (! plugui::layoutFor (EffectKind::distortion).items.empty());
  15664. }
  15665. #include "FabMeasureTests99.inc"
  15666. #include "DynamicsTests108.inc"
  15667. #include "Crest173dTests.inc"
  15668. #include "LynxTuneTests95.inc"
  15669. #include "LynxVerbTests100.inc"
  15670. #include "FacetTests103.inc"
  15671. #include "VocalLevel107Tests.inc"
  15672. #include "Substitutes113Tests.inc"
  15673. #include "Tune120Tests.inc"
  15674. #include "EchoSuiteTests101.inc"
  15675. #include "FruitySendTests105.inc"
  15676. #include "CrashFuzzTests111.inc"
  15677. #include "LookTests114.inc"
  15678. #include "FixedBlockTests118.inc"
  15679. #include "UiFl115Tests.inc"
  15680. #include "LoadTraceTests112.inc"
  15681. #include "VoiceToolsTests119.inc"
  15682. #include "VoiceMatchTests123.inc"
  15683. #include "LoadTests121.inc"
  15684. #include "Parity128Tests.inc"
  15685. #include "Parity128aTests.inc"
  15686. #include "Parity128bTests.inc"
  15687. #include "Parity128cTests.inc"
  15688. #include "Match124Tests.inc"
  15689. #include "Match124xTests.inc"
  15690. #include "Vmatch127Tests.inc"
  15691. #include "ParityTests129.inc"
  15692. #include "Parity135vTests.inc"
  15693. #include "Bugs131Tests.inc"
  15694. #include "Parity135wTests.inc"
  15695. #include "Feat133Tests.inc"
  15696. #include "Rea165fTests.inc"
  15697. #include "Parity135fTests.inc"
  15698. #include "Feat132Tests.inc"
  15699. #include "Parity135sTests.inc"
  15700. #include "VoiceMatch136Tests.inc"
  15701. #include "Parity137Tests.inc"
  15702. #include "Parity144Tests.inc"
  15703. #include "Parity144aTests.inc"
  15704. #include "Parity144bTests.inc"
  15705. #include "Parity147Tests.inc"
  15706. #include "Parity147aTests.inc"
  15707. #include "Parity147bTests.inc"
  15708. #include "Parity150Tests.inc"
  15709. #include "Parity150aTests.inc"
  15710. #include "Parity150bTests.inc"
  15711. #include "Parity152Tests.inc"
  15712. #include "Tp172dTests.inc"
  15713. #include "Verb183bEchoformTests.inc"
  15714. #include "Verb183bEpochTests.inc"
  15715. #include "Verb183bAtriumTests.inc"
  15716. #include "Verb183bPlateTests.inc"
  15717. #include "Verb183bHaloTests.inc"
  15718. #include "Verb183bNebulaTests.inc"
  15719. #include "Verb183bTests.inc"
  15720. #include "Parity152aTests.inc"
  15721. #include "Parity152bTests.inc"
  15722. #include "Parity152cTests.inc"
  15723. #include "Parity165Tests.inc"
  15724. #include "Parity165wTests.inc"
  15725. #include "Parity165xTests.inc"
  15726. #include "Parity165aTests.inc"
  15727. #include "Patcher141Tests.inc"
  15728. #include "Patcher162Tests.inc"
  15729. #include "Vox138Tests.inc"
  15730. #include "Vox145Tests.inc"
  15731. #include "Vox148Tests.inc"
  15732. #include "Natives142Tests.inc"
  15733. TEST (emphasizer_matches_fl_engine)
  15734. {
  15735. // REA_rodada154: FL's Emphasizer through the import (state → slot → effect) against goldens of the installation's
  15736. // Emphasizer_x64.dll (rate set before the state, 2304 samples of silence as FL's mixer runs it before a render,
  15737. // 512-frame calls, 44.1 kHz): FNV-1a of the output's float bits
  15738. auto signal = [] {
  15739. std::vector<float> l (24000), r (24000);
  15740. std::uint32_t s = 12345;
  15741. const float amps[] { 0.3f, 1.5f, 0.05f, 3.0f, 0.8f, 0.0f, 2.0f, 0.4f };
  15742. for (int i = 0; i < 24000; ++i)
  15743. {
  15744. const float a = amps[(i / 3000) % 8];
  15745. s = s * 1664525u + 1013904223u;
  15746. l[(std::size_t) i] = (float) ((s >> 8) / 8388608.0 - 1.0) * a;
  15747. s = s * 1664525u + 1013904223u;
  15748. r[(std::size_t) i] = (float) ((s >> 8) / 8388608.0 - 1.0) * a;
  15749. }
  15750. return std::pair { l, r };
  15751. };
  15752. struct Case { std::array<int, 6> raw; std::uint64_t hash; };
  15753. const Case cases[] { { { 0, 5013, 0, 0, 0, 10000 }, 0xa009608f060eb32eull }, { { 6000, 9000, 1, 0, 0, 8000 }, 0x6fc7ec82cc9d9c01ull },
  15754. { { 8000, 2000, 0, 1, 0, 10000 }, 0xb10a1f3bd998198cull }, { { 3000, 5013, 1, 0, 1, 10000 }, 0x57a8fa4d8b890ac0ull } };
  15755. for (const auto& c : cases)
  15756. {
  15757. std::vector<std::uint8_t> state (4 + 24 + 1, 0);
  15758. const std::int32_t version = 2;
  15759. std::memcpy (state.data(), &version, 4);
  15760. std::memcpy (state.data() + 4, c.raw.data(), 24);
  15761. state[28] = 1;
  15762. const auto slot = lynx::effectFromFl ("Emphasizer", state);
  15763. CHECK (slot && slot->kind == lynx::EffectKind::emphasizer);
  15764. if (! slot)
  15765. continue;
  15766. auto fx = lynx::makeEffect (slot->kind);
  15767. fx->prepare (44100.0);
  15768. auto [l, r] = signal();
  15769. for (int i = 0; i < 24000; i += 512) // the golden's 512-frame engine calls (level matching is per call)
  15770. fx->process (l.data() + i, r.data() + i, std::min (512, 24000 - i), slot->params, 120.0);
  15771. std::uint64_t h = 0xcbf29ce484222325ull;
  15772. for (int i = 0; i < 24000; ++i)
  15773. for (float v : { l[(std::size_t) i], r[(std::size_t) i] })
  15774. {
  15775. std::uint32_t u;
  15776. std::memcpy (&u, &v, 4);
  15777. h = (h ^ u) * 0x100000001b3ull;
  15778. }
  15779. CHECK (h == c.hash);
  15780. }
  15781. // version 1 (no output control) keeps the 0 dB default; texts
  15782. const std::vector<std::uint8_t> v1 { 1, 0, 0, 0, 0, 0, 0, 0, 0x95, 0x13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
  15783. const auto s1 = lynx::emphasizer::parse (v1);
  15784. CHECK (s1 && s1->raw[1] == 5013 && s1->raw[5] == 10000);
  15785. CHECK (lynx::formatEffectParam (lynx::EffectKind::emphasizer, 0, 0.5f, 120.0) == "+12.0 dB");
  15786. CHECK (lynx::formatEffectParam (lynx::EffectKind::emphasizer, 5, 1.0f, 120.0) == "0.0 dB");
  15787. CHECK (lynx::formatEffectParam (lynx::EffectKind::emphasizer, 2, 1.0f, 120.0) == "Soft");
  15788. }
  15789. // ---- Wave Traveller (REA_rodada167f-wavetraveller) ----
  15790. namespace
  15791. {
  15792. // the crafted case: a sample on the 16-bit grid and a state that uses every feature (the same data went to the DLL)
  15793. inline std::vector<float> wtTestSample (int n) // planar stereo, values on the 16-bit grid
  15794. {
  15795. std::vector<float> s ((std::size_t) n * 2);
  15796. std::uint32_t r = 2024;
  15797. for (int i = 0; i < n; ++i)
  15798. for (int c = 0; c < 2; ++c)
  15799. {
  15800. r = r * 1664525u + 1013904223u;
  15801. const auto noise = (int) (r >> 20) - 2048; // ±2048
  15802. const auto tone = (int) std::lround (12000.0 * std::sin ((double) i * (c == 0 ? 0.031 : 0.047)));
  15803. s[(std::size_t) c * (std::size_t) n + (std::size_t) i] = (float) (tone + noise) / 32768.0f;
  15804. }
  15805. return s;
  15806. }
  15807. inline lynx::wavetrav::State wtTestState()
  15808. {
  15809. using namespace lynx::wavetrav;
  15810. auto st = defaultState();
  15811. st.samplePath = "x.wav"; // (FL asks its host for the sample only when the state names one)
  15812. auto pt = [] (float x, float y, int type, float tension = 0.5f, float c1x = -0.05f, float c1y = -0.2f, float c2x = 0.1f, float c2y = 0.3f) {
  15813. auto p = newPoint (x, y);
  15814. p.type = type, p.tension = tension, p.c1x = c1x, p.c1y = c1y, p.c2x = c2x, p.c2y = c2y;
  15815. return p;
  15816. };
  15817. auto& a = st.patches[60]; // every segment type, tempo based
  15818. a.wave.clear();
  15819. const float xs[] { 0.0f, 0.15f, 0.3f, 0.45f, 0.6f, 0.75f, 0.9f, 1.0f }, ys[] { 0.1f, 0.8f, 0.2f, 0.9f, 0.05f, 0.7f, 0.3f, 1.0f };
  15820. const int ts[] { 1, 2, 3, 4, 5, 6, 8, 1 };
  15821. for (int i = 0; i < 8; ++i)
  15822. a.wave.push_back (pt (xs[i], ys[i], ts[i], ts[i] == 6 ? 0.2f : 0.5f));
  15823. a.speedBeats = 2.0f;
  15824. auto& b = st.patches[61]; // volume curve, mutes, start / end, attack / release
  15825. b.vol = { pt (0.0f, 0.2f, 2), pt (0.4f, 1.0f, 6, 0.8f), pt (0.7f, 0.3f, 4), pt (1.0f, 0.9f, 1) };
  15826. b.mutes = { { true, 0.0f }, { false, 0.3f }, { true, 0.5f } };
  15827. b.start = 0.2f, b.end = 0.7f, b.attackMs = 50.0f, b.releaseMs = 300.0f, b.speedBeats = 1.0f;
  15828. auto& c = st.patches[62]; // absolute speed, tension segments
  15829. c.splineType = 1, c.absSpeed = 2.3f, c.start = 0.1f, c.end = 0.6f;
  15830. c.wave = { pt (0.0f, 0.0f, 6, 0.5f), pt (0.5f, 0.9f, 6, 0.9f), pt (1.0f, 0.2f, 1) };
  15831. auto& d = st.patches[63]; // types that write nothing, a tiny Bezier
  15832. d.wave = { pt (0.0f, 0.0f, 1), pt (0.2f, 0.5f, 7), pt (0.3f, 0.1f, 0), pt (0.5f, 0.6f, 3, 0.5f, -0.05f, -0.2f, 0.0f, 0.0f),
  15833. pt (0.5004f, 0.2f, 3), pt (0.501f, 0.9f, 1), pt (1.0f, 0.4f, 1) };
  15834. for (auto& p : st.patches)
  15835. p.rebuild();
  15836. return st;
  15837. }
  15838. std::uint64_t floatBitsHash (const std::vector<float>& v)
  15839. {
  15840. std::uint64_t h = 0xcbf29ce484222325ull;
  15841. for (const float x : v)
  15842. {
  15843. std::uint32_t u;
  15844. std::memcpy (&u, &x, 4);
  15845. h = (h ^ u) * 0x100000001b3ull;
  15846. }
  15847. return h;
  15848. }
  15849. lynx::SampleData wtSampleData (const std::vector<float>& planar, int channels)
  15850. {
  15851. lynx::SampleData d;
  15852. const auto n = planar.size() / (std::size_t) channels;
  15853. d.left.assign (planar.begin(), planar.begin() + (std::ptrdiff_t) n);
  15854. if (channels > 1)
  15855. d.right.assign (planar.begin() + (std::ptrdiff_t) n, planar.end());
  15856. d.sampleRate = 44100.0;
  15857. return d;
  15858. }
  15859. } // namespace
  15860. TEST (wave_traveller_state_and_tables)
  15861. {
  15862. using namespace lynx::wavetrav;
  15863. // FL's state layout (§2): it round-trips byte for byte, and the default patch is FL's
  15864. const auto def = writeState (defaultState());
  15865. State s;
  15866. CHECK (parseState (def, s) && writeState (s) == def && s.selectedKey == 60 && s.samplePath.empty());
  15867. CHECK (s.patches[0].wave.size() == 2 && s.patches[119].vol.size() == 2 && s.patches[60].mutes.size() == 1 && s.patches[60].mutes[0].on);
  15868. const auto crafted = writeState (wtTestState());
  15869. CHECK (parseState (crafted, s) && writeState (s) == crafted);
  15870. CHECK (s.patches[62].splineType == 1 && s.patches[61].mutes.size() == 3 && ! s.patches[61].mutes[1].on && s.patches[60].wave.size() == 8);
  15871. // tables (§3): a 0 → 1 line is 1022 values long (the writer steps back one value per segment)
  15872. CHECK (s.patches[0].waveN == 1022 && s.patches[0].waveTable[0] == 0.0f && s.patches[0].volTable[500] == 1.0f);
  15873. std::vector<float> tables;
  15874. for (int k = 60; k < 64; ++k)
  15875. {
  15876. const auto& p = s.patches[(std::size_t) k];
  15877. tables.insert (tables.end(), p.waveTable.begin(), p.waveTable.begin() + p.waveN + 1);
  15878. tables.insert (tables.end(), p.volTable.begin(), p.volTable.begin() + p.volN + 1);
  15879. CHECK (std::all_of (p.waveTable.begin(), p.waveTable.end(), [] (float v) { return v >= 0.0f && v <= 1.0f; }));
  15880. }
  15881. // (LYNX's own value; the tables reach the DLL goldens of wave_traveller_matches_fl_engine through the output)
  15882. CHECK (floatBitsHash (tables) == 0xd94594a6f22cb4dfull);
  15883. if (floatBitsHash (tables) != 0xd94594a6f22cb4dfull)
  15884. std::printf (" wave traveller tables: %016llx\n", (unsigned long long) floatBitsHash (tables));
  15885. // knobs (FUN_007579e0 / 757b10 / 757b60)
  15886. CHECK (speedFromKnob (0.0f, false) == 0.125f && speedFromKnob (1.0f, false) == 8.0f && speedFromKnob (0.0f, true) == 0.25f
  15887. && speedFromKnob (1.0f, true) == 4.0f && attackFromKnob (1.0f) == 500.0f && releaseFromKnob (1.0f) == 1000.0f);
  15888. // a .spln preset (§6): knob positions, curves, mutes
  15889. std::vector<std::uint8_t> spln;
  15890. auto i32 = [&spln] (std::int32_t v) { spln.insert (spln.end(), (std::uint8_t*) &v, (std::uint8_t*) &v + 4); };
  15891. auto f32 = [&spln] (float v) { spln.insert (spln.end(), (std::uint8_t*) &v, (std::uint8_t*) &v + 4); };
  15892. i32 (4000), i32 (1000005), i32 (1);
  15893. for (float v : { 0.5f, 0.2f, 0.1f, 0.3f, 0.25f, 0.75f })
  15894. f32 (v);
  15895. const auto rec = [&] (float x, float y) {
  15896. std::vector<std::uint8_t> r (60, 0);
  15897. std::memcpy (r.data() + 8, &x, 4), std::memcpy (r.data() + 12, &y, 4);
  15898. const std::int32_t one = 1;
  15899. std::memcpy (r.data() + 0x30, &one, 4);
  15900. spln.insert (spln.end(), r.begin(), r.end());
  15901. };
  15902. i32 (3), rec (0.0f, 1.0f), rec (0.5f, 0.0f), rec (1.0f, 1.0f);
  15903. i32 (2), rec (0.0f, 1.0f), rec (1.0f, 0.5f);
  15904. i32 (2), i32 (0), f32 (0.0f), i32 (1), f32 (0.5f);
  15905. auto p = defaultPatch();
  15906. CHECK (applyPreset (spln, p) && p.splineType == 1 && p.speedBeats == speedFromKnob (0.5f, false) && p.absSpeed == speedFromKnob (0.2f, true)
  15907. && p.attackMs == attackFromKnob (0.1f) && p.releaseMs == releaseFromKnob (0.3f) && p.start == 0.25f && p.end == 0.75f
  15908. && p.wave.size() == 3 && p.vol.size() == 2 && p.mutes.size() == 2 && ! p.mutes[0].on && p.waveN > 1000);
  15909. // garbage is refused without crashing
  15910. std::uint32_t r = 7;
  15911. for (int round = 0; round < 300; ++round)
  15912. {
  15913. std::vector<std::uint8_t> blob (crafted.begin(), crafted.begin() + (std::ptrdiff_t) (r % crafted.size()));
  15914. for (int k = 0; k < 8 && ! blob.empty(); ++k)
  15915. {
  15916. r = r * 1664525u + 1013904223u;
  15917. blob[(r >> 8) % blob.size()] = (std::uint8_t) (r >> 24);
  15918. }
  15919. r = r * 1664525u + 1013904223u;
  15920. if (parseState (blob, s))
  15921. for (const auto& q : s.patches)
  15922. CHECK (q.waveN >= 0 && q.waveN < tableCapacity && q.volN >= 0 && q.volN < tableCapacity);
  15923. }
  15924. CHECK (! parseState ({ 1, 2, 3 }, s) && ! lynx::waveTravellerStateUsable ({}));
  15925. // the installation's states (read at test time): the render test and the plugin database preset
  15926. if (realFlpDir.empty())
  15927. return;
  15928. auto read = [] (const std::filesystem::path& path) {
  15929. std::ifstream in (path, std::ios::binary);
  15930. return std::vector<std::uint8_t> { std::istreambuf_iterator<char> (in), {} };
  15931. };
  15932. const auto base = std::filesystem::path (realFlpDir);
  15933. if (const auto b = read (base / "System" / "Render tests" / "Wave Traveller.flp"); ! b.empty())
  15934. {
  15935. const auto project = flp::parseProject (b);
  15936. int found = 0;
  15937. for (const auto& c : project.channels)
  15938. if (c.pluginName == "Wave Traveller")
  15939. {
  15940. ++found;
  15941. CHECK (parseState (c.pluginState, s) && s.samplePath.find ("808 Snare.wav") != std::string::npos);
  15942. }
  15943. CHECK (found == 1);
  15944. const auto song = lynx::importFlp (project);
  15945. const auto it = std::find_if (song.channels.begin(), song.channels.end(), [] (const lynx::ChannelDef& c) { return c.instrument == lynx::InstrumentKind::wavetraveller; });
  15946. CHECK (it != song.channels.end() && lynx::waveTravellerStateUsable (it->pluginState));
  15947. }
  15948. if (const auto b = read (base / "System" / "Plugin databases" / "Plugin database (alphabetical)" / "Generators" / "Wave Traveller.fst"); ! b.empty())
  15949. {
  15950. const auto project = flp::parseProject (b, true);
  15951. CHECK (! project.channels.empty() && parseState (project.channels.front().pluginState, s) && s.samplePath.empty());
  15952. }
  15953. }
  15954. TEST (wave_traveller_matches_fl_engine)
  15955. {
  15956. // §8: goldens of the installation's Wave Traveller_x64.dll in our harness (16-bit stereo sample handed by LoadSample,
  15957. // 512-frame calls, tempo by ProcessEvent): FNV-1a of the output's float bits. LYNX's engine reproduces them exactly.
  15958. using namespace lynx::wavetrav;
  15959. const auto planar = wtTestSample (30000);
  15960. const auto smp = sampleFrom (wtSampleData (planar, 2));
  15961. CHECK (smp != nullptr && smp->length == 30000);
  15962. if (smp == nullptr)
  15963. return;
  15964. const auto state = wtTestState();
  15965. auto note = [] (int start, int key, float vol, float pan, float cents, int release, float initPan = 0.0f) {
  15966. RefNote n;
  15967. n.start = start, n.release = release;
  15968. n.note.key = key, n.note.finalVol = vol, n.note.finalPan = pan, n.note.initPan = initPan;
  15969. n.note.finalPitch = 100.0f * (float) key - 6000.0f + cents;
  15970. return n;
  15971. };
  15972. struct Case { std::vector<RefNote> notes; int rate; float tempo; int taps, length; std::uint64_t hash; };
  15973. const Case cases[] {
  15974. // live: every segment type, volume curve + mutes + attack / release, absolute speed, types 0 / 7 and a tiny Bezier
  15975. { { note (0, 60, 0.9f, 0.25f, 30.0f, 20000), note (100, 61, 0.7f, -0.5f, -120.0f, 15000), note (2000, 62, 0.8f, 0.0f, 0.0f, -1),
  15976. note (5000, 63, 1.0f, 0.6f, 250.0f, -1) }, 44100, 140.0f, 0, 40000, 0x9001a50747634ba6ull },
  15977. // render mode, 256 taps per side (FL's render default), with the notes' own pan
  15978. { { note (0, 60, 0.9f, 0.25f, 30.0f, 20000, -0.3f), note (100, 61, 0.7f, -0.5f, -120.0f, 15000, -0.3f), note (2000, 62, 0.8f, 0.0f, 0.0f, -1, -0.3f),
  15979. note (5000, 63, 1.0f, 0.6f, 250.0f, -1, -0.3f) }, 44100, 140.0f, 256, 8000, 0xedabd9728ffbb252ull },
  15980. // a 48 kHz host at tempo 97.5
  15981. { { note (0, 62, 0.9f, -0.6f, -250.0f, 20000), note (300, 60, 0.8f, 0.3f, 100.0f, -1) }, 48000, 97.5f, 0, 30000, 0x71b988c298c5ab31ull },
  15982. };
  15983. for (const auto& c : cases)
  15984. {
  15985. const auto out = renderReference (state, *smp, c.notes, c.rate, c.tempo, c.taps, c.length);
  15986. CHECK (floatBitsHash (out) == c.hash);
  15987. if (floatBitsHash (out) != c.hash)
  15988. std::printf (" wave traveller case: %016llx\n", (unsigned long long) floatBitsHash (out));
  15989. }
  15990. // the instrument (channel → engine) plays what the reference plays: velocity 1/1.28 = FL's Init.Vol 1, centred
  15991. {
  15992. lynx::ChannelDef ch;
  15993. ch.instrument = lynx::InstrumentKind::wavetraveller;
  15994. ch.pluginState = writeState (state);
  15995. auto inst = lynx::makeInstrument (lynx::InstrumentKind::wavetraveller, std::make_shared<lynx::SampleData> (wtSampleData (planar, 2)));
  15996. inst->configure (ch, 140.0);
  15997. inst->prepare (44100.0);
  15998. std::vector<float> l (20480, 0.0f), r (20480, 0.0f);
  15999. for (int pos = 0; pos < 20480; pos += 512)
  16000. {
  16001. if (pos == 0)
  16002. inst->noteOn (61, 1.0f / 1.28f);
  16003. if (pos == 10240)
  16004. inst->noteOff (61);
  16005. inst->render (l.data() + pos, r.data() + pos, 512, 1.0f, 1.0f);
  16006. }
  16007. const auto ref = renderReference (state, *smp, { note (0, 61, 1.0f, 0.0f, 0.0f, 10240) }, 44100, 140.0f, 0, 20480);
  16008. bool same = true;
  16009. for (int i = 0; i < 20480; ++i)
  16010. same = same && l[(std::size_t) i] == ref[2 * (std::size_t) i] && r[(std::size_t) i] == ref[2 * (std::size_t) i + 1];
  16011. CHECK (same && inst->idle());
  16012. auto silent = lynx::makeInstrument (lynx::InstrumentKind::wavetraveller); // no sample: FL refuses the notes
  16013. silent->configure (ch, 140.0);
  16014. silent->prepare (44100.0);
  16015. silent->noteOn (60, 1.0f);
  16016. std::fill (l.begin(), l.end(), 0.0f);
  16017. silent->render (l.data(), r.data(), 512, 1.0f, 1.0f);
  16018. CHECK (silent->idle() && std::all_of (l.begin(), l.begin() + 512, [] (float v) { return v == 0.0f; }));
  16019. }
  16020. // the installation's render test (its 808 Snare decoded as FL decodes it), read at test time
  16021. if (realFlpDir.empty())
  16022. return;
  16023. const auto base = std::filesystem::path (realFlpDir);
  16024. std::ifstream fin (base / "System" / "Render tests" / "Wave Traveller.flp", std::ios::binary);
  16025. std::ifstream sin (base / "Patches" / "Packs" / "Drums" / "Snares" / "808 Snare.wav", std::ios::binary);
  16026. const std::vector<std::uint8_t> flpBytes { std::istreambuf_iterator<char> (fin), {} }, wav { std::istreambuf_iterator<char> (sin), {} };
  16027. lynx::oggvorbis::Decoded dec;
  16028. if (flpBytes.empty() || wav.empty() || ! lynx::oggvorbis::decode (wav.data(), wav.size(), dec) || dec.channels != 1)
  16029. return;
  16030. const auto project = flp::parseProject (flpBytes);
  16031. for (const auto& c : project.channels)
  16032. if (c.pluginName == "Wave Traveller")
  16033. {
  16034. State st;
  16035. CHECK (parseState (c.pluginState, st));
  16036. lynx::SampleData d;
  16037. d.left = dec.data[0];
  16038. const auto snare = sampleFrom (d);
  16039. CHECK (snare != nullptr && snare->length == 11002);
  16040. if (snare == nullptr)
  16041. return;
  16042. const auto out = renderReference (st, *snare, { note (0, 60, 1.0f, 0.0f, 0.0f, 44100) }, 44100, 140.0f, 0, 88200);
  16043. CHECK (floatBitsHash (out) == 0xafcb49e8c0ef918dull);
  16044. if (floatBitsHash (out) != 0xafcb49e8c0ef918dull)
  16045. std::printf (" wave traveller render test: %016llx\n", (unsigned long long) floatBitsHash (out));
  16046. }
  16047. }
  16048. TEST (fl_legacy_fx_match_fl_engines)
  16049. {
  16050. // REA_rodada165k: FL's Fruity Phase Inverter, Center, Bass Boost, 7 Band EQ and Delay (v1) through the import (state → slot →
  16051. // effect) against goldens of the installation's DLLs (rate set before the state, 512-frame calls): FNV-1a of the
  16052. // output's float bits (x87 hosts) and ten output samples (every platform, within the x87 tolerance)
  16053. struct Case { const char* name; int rate; std::vector<int> raw; std::uint64_t hash; std::array<float, 10> pts; };
  16054. const Case cases[] {
  16055. { "Fruity Phase Inverter", 44100, { 1024 }, 0xd397f60e2df38284ull, { 0.0363710076f, -0.0391986258f, -0.013737631f, 1.81496143e-05f, -2.10248637f, 1.46080184f, 0.0f, -0.0f, -1.1587677f, 0.471242905f } },
  16056. { "Fruity Phase Inverter", 44100, { 400 }, 0xda4a63932df38284ull, { -0.0363710076f, 0.0391986258f, 0.013737631f, -1.81496143e-05f, 2.10248637f, -1.46080184f, -0.0f, 0.0f, 1.1587677f, -0.471242905f } },
  16057. { "Fruity Phase Inverter", 44100, { 100 }, 0xe5593cda2df38284ull, { 0.0363710076f, 0.0391986258f, -0.013737631f, -1.81496143e-05f, -2.10248637f, -1.46080184f, 0.0f, 0.0f, -1.1587677f, -0.471242905f } },
  16058. { "Fruity Phase Inverter", 48000, { 100 }, 0xe5593cda2df38284ull, { 0.0363710076f, 0.0391986258f, -0.013737631f, -1.81496143e-05f, -2.10248637f, -1.46080184f, 0.0f, 0.0f, -1.1587677f, -0.471242905f } },
  16059. { "Fruity Center", 44100, { 1 }, 0x3bf5f676fa605f15ull, { 0.0366865396f, 0.0394224077f, -0.0162539873f, 0.00418465072f, -2.09711552f, -1.46810758f, 0.00764796557f, -0.00145012443f, -1.15346813f, -0.472040445f } },
  16060. { "Fruity Center", 48000, { 1 }, 0x4a8ec21dbef0f963ull, { 0.0366563909f, 0.0394078083f, -0.0160636567f, 0.00388090475f, -2.09756589f, -1.46745622f, 0.00721568894f, -0.00133169827f, -1.15359187f, -0.472001195f } },
  16061. { "Fruity Center", 44100, { 0 }, 0xe5593cda2df38284ull, { 0.0363710076f, 0.0391986258f, -0.013737631f, -1.81496143e-05f, -2.10248637f, -1.46080184f, 0.0f, 0.0f, -1.1587677f, -0.471242905f } },
  16062. { "Fruity Center", 48000, { 0 }, 0xe5593cda2df38284ull, { 0.0363710076f, 0.0391986258f, -0.013737631f, -1.81496143e-05f, -2.10248637f, -1.46080184f, 0.0f, 0.0f, -1.1587677f, -0.471242905f } },
  16063. { "Fruity Bass Boost", 44100, { 512, 768 }, 0x18162c85cf8081c1ull, { 0.0119505953f, 0.0342054106f, -0.13173604f, -0.137279794f, -2.28246212f, -1.31898761f, 0.01532559f, 0.048792243f, -0.967867851f, -0.387037605f } },
  16064. { "Fruity Bass Boost", 48000, { 512, 768 }, 0x882a7fa498a70215ull, { 0.00987694506f, 0.036732085f, -0.12154711f, -0.134889036f, -2.26724982f, -1.32030582f, 0.0145790791f, 0.0490387827f, -0.998028994f, -0.401082516f } },
  16065. { "Fruity Bass Boost", 44100, { 0, 1024 }, 0x928bfcf5bf6de195ull, { -0.00218012393f, 0.0478172116f, 0.0580603182f, -0.12624f, -2.2277863f, -1.17777693f, -0.0367000513f, 0.0577705353f, -1.24190557f, -0.492130041f } },
  16066. { "Fruity Bass Boost", 48000, { 0, 1024 }, 0x6c5ab3f609af2c3cull, { 0.0024052544f, 0.0460729003f, 0.0709654391f, -0.1167804f, -2.22533989f, -1.18373549f, -0.0448203459f, 0.053043291f, -1.24824715f, -0.494166315f } },
  16067. { "Fruity Bass Boost", 44100, { 1024, 300 }, 0xb246920f1ff60b3ull, { 0.0356629528f, 0.0353008583f, -0.0425309055f, -0.0178884063f, -2.15630841f, -1.42872798f, 0.00398741197f, 0.00680476194f, -1.08475125f, -0.437377959f } },
  16068. { "Fruity Bass Boost", 48000, { 1024, 300 }, 0x89122d0b3fa096baull, { 0.034816049f, 0.0356596299f, -0.0426218137f, -0.0213548262f, -2.15343547f, -1.42917442f, 0.00368365366f, 0.00760280434f, -1.08964729f, -0.439649194f } },
  16069. { "Fruity 7 band EQ", 44100, { 0, 0, 0, 0, 0, 0, 0 }, 0x3acef8692df38284ull, { 0.0363710076f, 0.0391986258f, -0.013737631f, -1.81496143e-05f, -2.10248637f, -1.46080184f, 0.0f, 0.0f, -1.1587677f, -0.471242905f } },
  16070. { "Fruity 7 band EQ", 44100, { 1800, -1800, 900, 0, -450, 1200, -1700 }, 0x968d9fdd0e5c53fdull, { 0.0559784994f, -0.0457369909f, -0.20320268f, -0.583957613f, -0.555740654f, -0.159529299f, 0.26959905f, -0.0863189623f, 1.0195893f, -0.781531453f } },
  16071. { "Fruity 7 band EQ", 44100, { -1200, 0, 0, 600, 0, 0, 1800 }, 0x96f6b413d4fc2b65ull, { -0.396190405f, 0.381581873f, 0.639778018f, 2.95740342f, -15.632513f, 0.385985613f, -1.42023098f, 0.503577054f, -9.11049557f, 1.05593979f } },
  16072. { "Fruity Delay", 44100, { 600, 700, 900, 1024, 4, 0 }, 0x3b93cd8ce56d8c32ull, { 0.0363710076f, 0.0391986258f, -0.013737631f, -1.81496143e-05f, -2.11485457f, -1.40214801f, 0.333820492f, -0.0261738673f, -0.786855578f, -1.16315055f } },
  16073. { "Fruity Delay", 48000, { 600, 700, 900, 1024, 4, 0 }, 0xc8e499107ccc6eeeull, { 0.0363710076f, 0.0391986258f, -0.013737631f, -1.81496143e-05f, -2.08610582f, -1.45314789f, 0.441712886f, 0.50612998f, -1.54570627f, 0.225791931f } },
  16074. { "Fruity Delay", 44100, { 700, 1024, 64, 800, 2, 600 }, 0x4b5cb4d05f1013cdull, { 0.0363710076f, 0.0391986258f, -0.014354689f, 0.0176514238f, -2.10405254f, -1.38042021f, -0.000323641405f, -0.0877400041f, -1.13004076f, -0.513286889f } },
  16075. { "Fruity Delay", 48000, { 700, 1024, 64, 800, 2, 600 }, 0x834a89d0aaa9a96eull, { 0.0363710076f, 0.0391986258f, -0.013737631f, -1.81496143e-05f, -2.1319468f, -1.44643033f, 0.00283452589f, -0.00713274023f, -1.14618802f, -0.506712079f } },
  16076. { "Fruity Delay", 44100, { 500, 900, 500, 512, 3, 1000 }, 0x2080bc496fcffb2cull, { 0.0363710076f, 0.0391986258f, -0.013737631f, -1.81496143e-05f, -2.10248637f, -1.46080184f, -0.0331584699f, -0.26390475f, -1.16307354f, -0.476940066f } },
  16077. { "Fruity Delay", 48000, { 500, 900, 500, 512, 3, 1000 }, 0x4c19478535905bb1ull, { 0.0363710076f, 0.0391986258f, -0.013737631f, -1.81496143e-05f, -2.10248637f, -1.46080184f, 0.0120966472f, 0.0150470948f, -1.15484953f, -0.475222349f } },
  16078. };
  16079. for (const auto& c : cases)
  16080. {
  16081. std::vector<std::uint8_t> state (4 + 4 * c.raw.size(), 0);
  16082. const std::int32_t version = 1;
  16083. std::memcpy (state.data(), &version, 4);
  16084. std::memcpy (state.data() + 4, c.raw.data(), 4 * c.raw.size());
  16085. const auto slot = lynx::effectFromFl (c.name, state);
  16086. CHECK (slot && slot->kind != lynx::EffectKind::utility && slot->kind != lynx::EffectKind::eq
  16087. && slot->kind != lynx::EffectKind::parametricEq);
  16088. if (! slot)
  16089. continue;
  16090. auto fx = lynx::makeEffect (slot->kind);
  16091. fx->prepare ((double) c.rate);
  16092. std::vector<float> l (24000), r (24000);
  16093. std::uint32_t s = 12345;
  16094. const float amps[] { 0.3f, 1.5f, 0.05f, 3.0f, 0.8f, 0.0f, 2.0f, 0.4f };
  16095. for (int i = 0; i < 24000; ++i)
  16096. {
  16097. const float a = amps[(i / 3000) % 8];
  16098. s = s * 1664525u + 1013904223u;
  16099. l[(std::size_t) i] = (float) ((s >> 8) / 8388608.0 - 1.0) * a;
  16100. s = s * 1664525u + 1013904223u;
  16101. r[(std::size_t) i] = (float) ((s >> 8) / 8388608.0 - 1.0) * a;
  16102. }
  16103. // host blocks of 512 like the goldens' DLL calls (the Center / Bass Boost / 7 Band EQ round their state to double at
  16104. // the end of each call, as FL does, so other block sizes differ in the last bits; checked below)
  16105. const auto input = std::make_pair (l, r);
  16106. for (int i = 0; i < 24000; i += 512)
  16107. fx->process (l.data() + i, r.data() + i, std::min (512, 24000 - i), slot->params, 120.0);
  16108. {
  16109. auto other = lynx::makeEffect (slot->kind);
  16110. other->prepare ((double) c.rate);
  16111. auto l2 = input.first, r2 = input.second;
  16112. for (int i = 0; i < 24000; i += 1000)
  16113. other->process (l2.data() + i, r2.data() + i, 1000, slot->params, 120.0);
  16114. float worst = 0.0f;
  16115. for (std::size_t i = 0; i < 24000; ++i)
  16116. worst = std::max ({ worst, std::abs (l2[i] - l[i]) / std::max (1.0f, std::abs (l[i])), std::abs (r2[i] - r[i]) / std::max (1.0f, std::abs (r[i])) });
  16117. CHECK (worst <= 1.0e-4f); // (measured: ≤ 3.5e-5, the Bass Boost at 20 Hz)
  16118. }
  16119. for (int k = 0; k < 5; ++k)
  16120. {
  16121. const auto i = (std::size_t) (1500 + 4500 * k);
  16122. CHECK (std::abs (l[i] - c.pts[(std::size_t) (2 * k)]) <= x87Tolerance() * std::max (1.0f, std::abs (c.pts[(std::size_t) (2 * k)])));
  16123. CHECK (std::abs (r[i] - c.pts[(std::size_t) (2 * k + 1)]) <= x87Tolerance() * std::max (1.0f, std::abs (c.pts[(std::size_t) (2 * k + 1)])));
  16124. }
  16125. #if defined(__x86_64__) || defined(__i386__)
  16126. std::uint64_t h = 0xcbf29ce484222325ull;
  16127. for (int i = 0; i < 24000; ++i)
  16128. for (float v : { l[(std::size_t) i], r[(std::size_t) i] })
  16129. {
  16130. std::uint32_t u;
  16131. std::memcpy (&u, &v, 4);
  16132. h = (h ^ u) * 0x100000001b3ull;
  16133. }
  16134. CHECK (h == c.hash);
  16135. #endif
  16136. }
  16137. CHECK (lynx::formatEffectParam (lynx::EffectKind::flPhaseInverter, 0, 0.3f, 120.0) == "None");
  16138. CHECK (lynx::formatEffectParam (lynx::EffectKind::flPhaseInverter, 0, 0.5f, 120.0) == "Left");
  16139. CHECK (lynx::formatEffectParam (lynx::EffectKind::flPhaseInverter, 0, 1.0f, 120.0) == "Right");
  16140. CHECK (lynx::formatEffectParam (lynx::EffectKind::flBassBoost, 0, 0.5f, 120.0) == "60 Hz");
  16141. CHECK (lynx::formatEffectParam (lynx::EffectKind::fl7BandEq, 3, 1.0f, 120.0) == "+18.0 dB");
  16142. CHECK (lynx::formatEffectParam (lynx::EffectKind::fl7BandEq, 3, 0.5f, 120.0) == "+0.0 dB");
  16143. CHECK (lynx::formatEffectParam (lynx::EffectKind::flDelay1, 3, 0.0f, 120.0) == "Host");
  16144. CHECK (lynx::formatEffectParam (lynx::EffectKind::flDelay1, 3, 1.0f, 120.0) == "280 BPM");
  16145. CHECK (lynx::formatEffectParam (lynx::EffectKind::flDelay1, 4, 2.0f / 15.0f, 120.0) == "3/16");
  16146. CHECK (lynx::formatEffectParam (lynx::EffectKind::flDelay1, 5, 1.0f, 120.0) == "Ping-pong");
  16147. CHECK (lynx::effectFromFl ("Fruity Delay", { 1, 0, 0, 0 }) == std::nullopt
  16148. || lynx::effectFromFl ("Fruity Delay", { 1, 0, 0, 0 })->kind != lynx::EffectKind::flDelay1);
  16149. // the default Phase Inverter of FL inverts the right side only (LYNX's old import inverted both)
  16150. CHECK (lynx::fllegacy::phaseInverterMode (1024) == 2 && lynx::fllegacy::phaseInverterMode (341) == 0
  16151. && lynx::fllegacy::phaseInverterMode (342) == 1 && lynx::fllegacy::phaseInverterMode (683) == 2);
  16152. }
  16153. TEST (fl_frequency_shifter_matches_fl_engine)
  16154. {
  16155. // REA_rodada154 §4: FL's Frequency Shifter (import → slot → effect) against goldens of the installation's
  16156. // Engine_x64.dll driven by its front end (rate before the state, 1536 samples of silence first, transport stopped, 120 BPM): FNV-1a
  16157. // of the output
  16158. struct Case { std::array<int, 10> raw; double rate; std::uint64_t hash; };
  16159. const Case cases[] { { { 10000, 0, -60480, 0, 10000, 10000, 5000, 10000, 1, 5000 }, 44100.0, 0x462af5f13683c90eull },
  16160. { { 7000, 2, 40000, 3000, 10000, 10000, 8000, 6000, 0, 2000 }, 44100.0, 0xcd597b95bf1bbb1aull },
  16161. { { 6000, 1, -30000, 6000, 3000, 7000, 3000, 4000, 0, 5000 }, 48000.0, 0x5592232115cb3acull },
  16162. { { 10000, 0, 51542, 2000, 8000, 10000, 5000, 10000, 0, 5000 }, 48000.0, 0x63d3051ba14bd00full } };
  16163. for (const auto& c : cases)
  16164. {
  16165. std::vector<std::uint8_t> state (4 + 40 + 8, 0);
  16166. const std::int32_t version = 4;
  16167. std::memcpy (state.data(), &version, 4);
  16168. std::memcpy (state.data() + 4, c.raw.data(), 40);
  16169. const auto slot = lynx::effectFromFl ("Frequency Shifter", state);
  16170. CHECK (slot && slot->kind == lynx::EffectKind::flFrequencyShifter);
  16171. if (! slot)
  16172. continue;
  16173. auto fx = lynx::makeEffect (slot->kind);
  16174. fx->prepare (c.rate);
  16175. std::vector<float> l (24000), r (24000);
  16176. std::uint32_t s = 12345;
  16177. const float amps[] { 0.3f, 1.5f, 0.05f, 3.0f, 0.8f, 0.0f, 2.0f, 0.4f };
  16178. for (int i = 0; i < 24000; ++i)
  16179. {
  16180. const float a = amps[(i / 3000) % 8];
  16181. s = s * 1664525u + 1013904223u;
  16182. l[(std::size_t) i] = (float) ((s >> 8) / 8388608.0 - 1.0) * a;
  16183. s = s * 1664525u + 1013904223u;
  16184. r[(std::size_t) i] = (float) ((s >> 8) / 8388608.0 - 1.0) * a;
  16185. }
  16186. for (int i = 0; i < 24000; i += 800)
  16187. fx->process (l.data() + i, r.data() + i, 800, slot->params, 120.0);
  16188. std::uint64_t h = 0xcbf29ce484222325ull;
  16189. for (int i = 0; i < 24000; ++i)
  16190. for (float v : { l[(std::size_t) i], r[(std::size_t) i] })
  16191. {
  16192. std::uint32_t u;
  16193. std::memcpy (&u, &v, 4);
  16194. h = (h ^ u) * 0x100000001b3ull;
  16195. }
  16196. CHECK (h == c.hash);
  16197. }
  16198. // the engine's tanhf against points of the library's
  16199. CHECK (lynx::fsfl::tanhf_ (0.0f) == 0.0f && lynx::fsfl::tanhf_ (20.0f) == 1.0f && lynx::fsfl::tanhf_ (-20.0f) == -1.0f);
  16200. CHECK (std::fabs (lynx::fsfl::tanhf_ (0.5f) - 0.462117165f) < 1e-7f && std::fabs (lynx::fsfl::tanhf_ (2.0f) - 0.964027584f) < 1e-7f);
  16201. CHECK (lynx::formatEffectParam (lynx::EffectKind::flFrequencyShifter, 1, 1.0f, 120.0) == "Ring modulator");
  16202. }
  16203. TEST (fl_low_lifter_matches_fl_engine)
  16204. {
  16205. // REA_rodada154 §5: FL's Low Lifter (import → slot → effect, 512-frame calls) against goldens of the installation's
  16206. // engine (ildsp_x64.dll's Faust program driven by Low Lifter_x64.dll, rate before the state): FNV-1a of the output
  16207. struct Case { std::array<int, 15> raw; double rate; std::uint64_t hash; };
  16208. const Case cases[] { { { 1, 10000, 4999, 7210, 1, 0, 0, 0, 5000, 10000, 7750, 0, 0, 1, 1 }, 44100.0, 0xcbb7eae8f31ca6d9ull },
  16209. { { 1, 7506, 5000, 3277, 1, 0, 6757, 5053, 9608, 10000, 7778, 5281, 1, 1, 1 }, 44100.0, 0xdb2d3712d4fb16c5ull },
  16210. { { 1, 5095, 6737, 5109, 1, 1, 541, 1479, 5451, 8197, 690, 6833, 1, 1, 1 }, 48000.0, 0x273c262202984447ull } };
  16211. for (const auto& c : cases)
  16212. {
  16213. std::vector<std::uint8_t> state (4 + 60 + 18, 0);
  16214. const std::int32_t version = 6;
  16215. std::memcpy (state.data(), &version, 4);
  16216. std::memcpy (state.data() + 4, c.raw.data(), 60);
  16217. const auto slot = lynx::effectFromFl ("Low Lifter", state);
  16218. CHECK (slot && slot->kind == lynx::EffectKind::flLowLifter);
  16219. if (! slot)
  16220. continue;
  16221. auto fx = lynx::makeEffect (slot->kind);
  16222. fx->prepare (c.rate);
  16223. std::vector<float> l (24000), r (24000);
  16224. std::uint32_t s = 12345;
  16225. const float amps[] { 0.3f, 1.5f, 0.05f, 3.0f, 0.8f, 0.0f, 2.0f, 0.4f };
  16226. for (int i = 0; i < 24000; ++i)
  16227. {
  16228. const float a = amps[(i / 3000) % 8];
  16229. s = s * 1664525u + 1013904223u;
  16230. l[(std::size_t) i] = (float) ((s >> 8) / 8388608.0 - 1.0) * a;
  16231. s = s * 1664525u + 1013904223u;
  16232. r[(std::size_t) i] = (float) ((s >> 8) / 8388608.0 - 1.0) * a;
  16233. }
  16234. for (int i = 0; i < 24000; i += 512)
  16235. fx->process (l.data() + i, r.data() + i, std::min (512, 24000 - i), slot->params, 120.0);
  16236. std::uint64_t h = 0xcbf29ce484222325ull;
  16237. for (int i = 0; i < 24000; ++i)
  16238. for (float v : { l[(std::size_t) i], r[(std::size_t) i] })
  16239. {
  16240. std::uint32_t u;
  16241. std::memcpy (&u, &v, 4);
  16242. h = (h ^ u) * 0x100000001b3ull;
  16243. }
  16244. CHECK (h == c.hash);
  16245. }
  16246. CHECK (lynx::formatEffectParam (lynx::EffectKind::flLowLifter, 10, 0.775f, 120.0) == "80 Hz");
  16247. CHECK (lynx::formatEffectParam (lynx::EffectKind::flLowLifter, 3, 0.721f, 120.0) == "-6.0 dB");
  16248. }
  16249. #include "Rea153Tests.inc"
  16250. #include "Logger181bTests.inc"
  16251. #include "Render155Tests.inc"
  16252. #include "Tune157Tests.inc"
  16253. #include "Tune165Tests.inc"
  16254. #include "HwOut156Tests.inc"
  16255. #include "SoundFont158Tests.inc"
  16256. #include "Mp3157Tests.inc"
  16257. #include "Quality160Tests.inc"
  16258. #include "CrashReport159Tests.inc"
  16259. #include "Sampler162Tests.inc"
  16260. #include "Workflow163cTests.inc"
  16261. #include "ClipPitch164Tests.inc"
  16262. #include "Misc164mTests.inc"
  16263. #include "MidiSync165Tests.inc"
  16264. #include "Theme162Tests.inc"
  16265. #include "PluginPresets162Tests.inc"
  16266. #include "Look162Tests.inc"
  16267. #include "EnvController162Tests.inc"
  16268. #include "Browser162Tests.inc"
  16269. #include "BrowserView163Tests.inc"
  16270. #include "Transport162Tests.inc"
  16271. #include "Workflow163Tests.inc"
  16272. #include "Rec163Tests.inc"
  16273. #include "Ui163Tests.inc"
  16274. #include "HwInsert164Tests.inc"
  16275. #include "Layer164Tests.inc"
  16276. #include "Patcher164vTests.inc"
  16277. #include "Vfx164Tests.inc"
  16278. #include "VfxScript165Tests.inc"
  16279. #include "Perf167Tests.inc"
  16280. #include "Misc164Tests.inc"
  16281. #include "Store165Tests.inc" // (round 165n; kept off the end of the list, where every round appends)
  16282. #include "Rack165Tests.inc" // (round 165f-rack; likewise)
  16283. #include "Visual164Tests.inc"
  16284. #include "Visual165Tests.inc"
  16285. #include "Patcher164fTests.inc"
  16286. #include "Markers165Tests.inc"
  16287. #include "PerfMode165Tests.inc"
  16288. #include "Rea167gTests.inc"
  16289. #include "Rea167hTests.inc"
  16290. #include "Rea167jTests.inc"
  16291. #include "Rea167kTests.inc"
  16292. #include "Rea167lTests.inc"
  16293. #include "Patch165Tests.inc"
  16294. #include "FlPlugins165Tests.inc"
  16295. #include "FlScratch165Tests.inc"
  16296. #include "FlTrans165Tests.inc"
  16297. #include "VideoPlayer165Tests.inc"
  16298. #include "Video174dTests.inc"
  16299. #include "Dash165Tests.inc"
  16300. #include "LinuxMouse165Tests.inc"
  16301. #include "Fl165Tests.inc"
  16302. #include "Issues167oTests.inc"
  16303. #include "Perf165Tests.inc"
  16304. #include "Issue12Tests.inc"
  16305. #include "Issue12cTests.inc"
  16306. #include "Audit165zbTests.inc"
  16307. #include "FlGran165Tests.inc"
  16308. #include "GranDrop165Tests.inc"
  16309. #include "Kepler165kTests.inc"
  16310. #include "KeplerExo167dTests.inc"
  16311. #include "MixerLatency165Tests.inc"
  16312. #include "Rea168jTests.inc"
  16313. #include "Audit165Tests.inc"
  16314. #include "Vfx165cTests.inc"
  16315. #include "HarmorBeta165hTests.inc"
  16316. #include "Crash13Tests.inc"
  16317. #include "Issue11bTests.inc"
  16318. #include "Issue11cTests.inc"
  16319. #include "FlNewt165Tests.inc"
  16320. #include "Audit165zATests.inc"
  16321. #include "Audit165zA2Tests.inc"
  16322. #include "Eq166fTests.inc"
  16323. #include "Feat166dTests.inc"
  16324. #include "Round166eTests.inc"
  16325. #include "Issue15Tests.inc"
  16326. #include "Issue14bugsTests.inc"
  16327. #include "Left167Tests.inc"
  16328. #include "Wheel173dTests.inc"
  16329. #include "PoiZoneWheels173dTests.inc"
  16330. #include "Tune166cTests.inc"
  16331. #include "Update166bTests.inc"
  16332. #include "Emphasis167fTests.inc"
  16333. #include "Dvs167Tests.inc"
  16334. #include "EdisonScript167Tests.inc"
  16335. #include "Deverb167Tests.inc"
  16336. #include "Score167Tests.inc"
  16337. #include "Ntt167Tests.inc"
  16338. #include "MidiOut167iTests.inc"
  16339. #include "Transporter167fTests.inc"
  16340. #include "Polish167pTests.inc"
  16341. #include "Menus168zTests.inc"
  16342. #include "Session168aTests.inc"
  16343. #include "Live169bTests.inc"
  16344. #include "Live169jTests.inc"
  16345. #include "StemSplitTests.inc"
  16346. #include "AudioToMidi189bTests.inc"
  16347. #include "Zoom167tTests.inc"
  16348. #include "PianoKeys190jTests.inc"
  16349. #include "Proll168gTests.inc"
  16350. #include "Rea168eTests.inc"
  16351. #include "Design168nTests.inc"
  16352. #include "Design168qTests.inc"
  16353. #include "FirstRun167vTests.inc"
  16354. #include "LoopStarter168bTests.inc"
  16355. #include "Rec168kTests.inc"
  16356. #include "FlPaths124Tests.inc"
  16357. #include "Auto125Tests.inc"
  16358. #include "Rec170dTests.inc"
  16359. #include "Rec177dTests.inc"
  16360. #include "Pain177dTests.inc"
  16361. #include "Mixer177dTests.inc"
  16362. #include "Edit177dTests.inc"
  16363. #include "Patterns177dTests.inc"
  16364. #include "Issue32Tests.inc"
  16365. #include "Keys117Tests.inc"
  16366. #include "Pedal187aTests.inc"
  16367. #include "Sc109Tests.inc"
  16368. #include "WrapperMidi181cTests.inc"
  16369. #include "Keys96Tests.inc"
  16370. #include "Mono168lTests.inc"
  16371. #include "MixPre167qTests.inc"
  16372. #include "Design168rTests.inc"
  16373. #include "Playlist168fTests.inc"
  16374. #include "RecNoFold126Tests.inc"
  16375. #include "LoopWrap167qTests.inc"
  16376. #include "Sel168tTests.inc"
  16377. #include "Gap169kTests.inc"
  16378. #include "Gap169kPickerTests.inc"
  16379. #include "Gap169kAlignTests.inc"
  16380. #include "FlQ170iTests.inc"
  16381. #include "FlQ170i2Tests.inc"
  16382. #include "FlQ170i3Tests.inc"
  16383. #include "Speech168bTests.inc"
  16384. #include "Design168pTests.inc"
  16385. #include "Typing168tTests.inc"
  16386. #include "GrossBeat168mTests.inc"
  16387. #include "Maximus168xTests.inc"
  16388. #include "Rec168xTests.inc"
  16389. #include "Design168oTests.inc"
  16390. #include "Pr44BrowserListTests.inc"
  16391. #include "Mixer168hTests.inc"
  16392. #include "Mute53fTests.inc"
  16393. #include "Issue32cTests.inc"
  16394. #include "Drumaxx169eTests.inc"
  16395. #include "PatternBlocks170fTests.inc"
  16396. #include "Mixfx170bTests.inc"
  16397. #include "Typeahead170bTests.inc"
  16398. #include "PlugQ170cTests.inc"
  16399. #include "Toolbar170eTests.inc"
  16400. #include "Rack170aTests.inc"
  16401. #include "Inst169hTests.inc"
  16402. #include "Crash169gTests.inc"
  16403. #include "Wfqa170hTests.inc"
  16404. #include "Toxic169hTests.inc"
  16405. #include "Gen169lTests.inc"
  16406. #include "Picker171cTests.inc"
  16407. #include "Picker175bTests.inc"
  16408. #include "Gen169l2Tests.inc"
  16409. #include "Patcher171bTests.inc"
  16410. #include "Wfqa170h2Tests.inc"
  16411. #include "Fader171cTests.inc"
  16412. #include "Tb169g2Tests.inc"
  16413. #include "Issues171aTests.inc"
  16414. #include "CtlAudit170i3Tests.inc"
  16415. #include "CtlWake173dTests.inc"
  16416. #include "Issue32dTests.inc"
  16417. #include "Issue81Tests.inc"
  16418. #include "Issue85Tests.inc"
  16419. #include "FlpExport171eTests.inc"
  16420. #include "Tp172cTests.inc"
  16421. #include "Layout172aTests.inc"
  16422. #include "Issues173aTests.inc"
  16423. #include "FxAudit173dTests.inc"
  16424. #include "Audit173d3Tests.inc"
  16425. #include "Vocodex173d3Tests.inc"
  16426. #include "Fluid174eTests.inc"
  16427. #include "Comp177Tests.inc"
  16428. #include "AlsImport175aTests.inc"
  16429. #include "FlmImport182cTests.inc"
  16430. #include "FlmImport187dTests.inc"
  16431. #include "FlmImport188fTests.inc"
  16432. #include "FlmImport188hTests.inc"
  16433. #include "FlmImport189hTests.inc"
  16434. #include "FlmImport189hATests.inc"
  16435. #include "FlmImport189hBTests.inc"
  16436. #include "FlmImport189hCTests.inc"
  16437. #include "Bridge176aTests.inc"
  16438. #include "Issues178aTests.inc"
  16439. #include "Mixer177aTests.inc"
  16440. #include "ClipLauncher181aTests.inc"
  16441. #include "DetailView183aTests.inc"
  16442. #include "Als181cTests.inc"
  16443. #include "Merge180cTests.inc"
  16444. #include "Slicer180cTests.inc"
  16445. #include "Slicer190fTests.inc"
  16446. #include "PluginExport180bTests.inc"
  16447. #include "FlExport180dTests.inc"
  16448. #include "FlpMute182eTests.inc"
  16449. #include "FlpPeak182eTests.inc"
  16450. #include "FlpPatcherSc182eTests.inc"
  16451. #include "FlFades180dTests.inc"
  16452. #include "Stretch180dTests.inc"
  16453. #include "Timing180dTests.inc"
  16454. #include "FlpPdc182eTests.inc"
  16455. #include "Pdc188eTests.inc"
  16456. #include "Mixer177a2Tests.inc"
  16457. #include "Mixer177a3Tests.inc"
  16458. #include "Mixer186cTests.inc"
  16459. #include "Mixer177a4Tests.inc"
  16460. #include "Sc177bTests.inc"
  16461. #include "Auto177bTests.inc"
  16462. #include "Lanes177bTests.inc"
  16463. #include "Lfo177bTests.inc"
  16464. #include "Mod177bTests.inc"
  16465. #include "UiFont179aTests.inc"
  16466. #include "Multilink177bTests.inc"
  16467. #include "Issue97Tests.inc"
  16468. #include "SimpleTracks184aTests.inc"
  16469. #include "SimpleChooser184aTests.inc"
  16470. #include "LayoutStyle184aTests.inc"
  16471. #include "Rack116Tests.inc"
  16472. #include "Issue116DetachedTests.inc"
  16473. #include "Trial187bTests.inc"
  16474. #include "ProjectKey184aTests.inc"
  16475. #include "Render186bTests.inc"
  16476. #include "Gamepad184aTests.inc"
  16477. #include "ExportBundle184aTests.inc"
  16478. #include "Strike189dTests.inc"
  16479. #include "NoteChance187cTests.inc"
  16480. #include "Notes188gTests.inc"
  16481. #include "Layer190bTests.inc"
  16482. #include "Undo190bTests.inc"
  16483. #include "Bugs190cTests.inc"
  16484. #include "Surge190xTests.inc"
  16485. #include "TruePeak190amTests.inc"
  16486. #include "Loudness190amTests.inc"
  16487. #include "UsedOnly190amTests.inc"