tests.cpp 950 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145
  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. #include "core/FlTrackMenus.h" // round 190dx
  24. #include "core/OneClickRec.h"
  25. #include "core/TakeRecording.h"
  26. #if defined(__SSE__) || defined(_M_X64) || defined(__x86_64__) || defined(__aarch64__) || defined(_M_ARM64) // x86 SSE, or ARM64 through core/Simd.h (sse2neon)
  27. #include "core/FlControlMenus.h"
  28. #include "core/LinkFormula.h"
  29. #include "core/ModulatorExport.h"
  30. #include "core/Multilink.h"
  31. #include "core/Simd.h"
  32. #endif
  33. #include "core/AutomationDisplay.h"
  34. #include "core/ShownEffectSlot.h"
  35. #include "core/Dither.h"
  36. #include "core/DrumSynthLive.h"
  37. #include "core/Effects.h"
  38. #include "core/MixSuite.h"
  39. #include "core/StrikeShaper.h"
  40. #include "core/EmberClassic.h"
  41. #include "core/Effector.h"
  42. #include "core/FruityDelayBank.h"
  43. #include "core/FruityMultiband.h"
  44. #include "core/Denormals.h"
  45. #include "core/TestProfile.h"
  46. #include "core/FlCurve.h"
  47. #include "core/FlpImport.h"
  48. #include "core/MidiControls.h"
  49. #include "core/MidiImport.h"
  50. #include "core/ChannelMiscFx.h"
  51. #include "core/FlEffects.h"
  52. #include "core/ProjectContents.h"
  53. #include "core/FlPatcher.h"
  54. #include "core/Patcher.h"
  55. #include "core/Rack.h"
  56. #include "core/Layer.h"
  57. #include "core/XyController.h"
  58. #include "core/ControlSurface.h"
  59. #include "core/VoltageController.h"
  60. #include "core/FlSamplers.h"
  61. #include "core/Fpc.h"
  62. #include "core/FpcInstrument.h"
  63. #include "core/FpcEnvelope.h"
  64. #include "core/FlSynths.h"
  65. #include "core/Inflate.h"
  66. #include "core/WavPack.h"
  67. #include "core/SampleFiles.h"
  68. #include "core/FlWrapper.h"
  69. #include "core/FlPluginDb.h"
  70. #include "core/MissingFiles.h"
  71. #include "core/PianoKeys.h"
  72. #include "core/PianoRollTools.h"
  73. #include "core/EventEdit.h"
  74. #include "core/Preferences.h"
  75. #include "core/MidiSync.h"
  76. #include "core/ControllerSession.h"
  77. #include "core/History.h"
  78. #include "core/Gms.h"
  79. #include "core/GmsMobile.h"
  80. #include "core/PoiZone.h"
  81. #include "core/MiniSynth.h"
  82. #include "core/SimSynth.h"
  83. #include "core/Granulizer.h"
  84. #include "core/WaveTraveller.h"
  85. #include "core/Drumaxx.h"
  86. #include "core/Wasp.h"
  87. #include "core/PluginLayout.h"
  88. #include "core/Plucked.h"
  89. #include "core/BeepMap.h"
  90. #include "core/Sawer.h"
  91. #include "core/Slayer.h"
  92. #include "core/Toxic.h"
  93. #include "core/Harmless.h"
  94. #include "core/Harmor.h"
  95. #include "core/Morphine.h"
  96. #include "core/Ogun.h"
  97. #include "core/BassDrum.h"
  98. #include "core/FruitKick.h"
  99. #include "core/IppCos.h"
  100. #include "core/Dx10.h"
  101. #include "core/Kepler.h"
  102. #include "core/ThreeOsc.h"
  103. #include "core/FlResampler.h"
  104. #include "core/FlKeys.h"
  105. #include "core/DirectWave.h"
  106. #include "core/OggVorbis.h"
  107. #include "core/FlVoiceProcessor.h"
  108. #include "core/TransistorBass.h"
  109. #include "core/HarmlessFx.h"
  110. #include "core/MaximusFx.h"
  111. #include "core/EmphasisFx.h"
  112. #include "core/VisualFx.h"
  113. #include "core/EmphasizerFx.h"
  114. #include "core/FlLegacyFx.h"
  115. #include "core/FreqShifterFl.h"
  116. #include "core/LowLifterFl.h"
  117. #include "core/Reeverb2.h"
  118. #include "core/LuxeVerb.h"
  119. #include "core/Denormals.h"
  120. #include "core/FruityDynamics.h"
  121. #include "core/FruityShapers.h"
  122. #include "core/ParametricEqFl.h"
  123. #include "core/FruityBasicFx.h"
  124. #include "core/EquoFx.h"
  125. #include "core/DistructorFx.h"
  126. #include "core/MultibandDelayFx.h"
  127. #include "core/PitchShifterFl.h"
  128. #include "core/FruityFilters.h"
  129. #include "core/NewtoneFx.h"
  130. #include "core/EdisonFx.h"
  131. #include "core/FruityDelay.h"
  132. #include "core/StereoFx.h"
  133. #include "core/FrequencySplitter.h"
  134. #include "core/ChorusFx.h"
  135. #include "core/GrossBeat.h"
  136. #include "core/LovePhilter.h"
  137. #include "core/FlangusFastDist.h"
  138. #include "core/FruityPhaser.h"
  139. #include "core/FruityVocoder.h"
  140. #include "core/DelphiMath.h"
  141. #include "core/LiveEventQueue.h"
  142. #include "core/RackRenderer.h"
  143. #include "core/PerfProjects.h"
  144. #include "core/TempoMap.h"
  145. #include "core/ActivityLight.h" // round 190ih
  146. #include "core/LiveNote.h"
  147. #include "core/LiveKeys.h"
  148. #include "core/PresetMix.h"
  149. #include "core/PinOnTop.h"
  150. #include "core/MetronomeVoice.h"
  151. #include "engine/Transport.h"
  152. #include "engine/RemoteHistory.h"
  153. #include "core/Patcherize.h"
  154. #include "core/Performance.h"
  155. #include "core/Scheduler.h"
  156. #include "core/MarkerPlan.h"
  157. #include "core/SnapshotExchange.h"
  158. #include "core/SoftClip.h"
  159. #include "core/Slicer.h"
  160. #include "core/SytrusEngine.h"
  161. #include "core/Templates.h"
  162. #include "core/Vocodex.h"
  163. #include "core/TimeStretch.h"
  164. #include "core/WorkerPool.h"
  165. #include "core/RtStats.h"
  166. #include "core/FixedBlock.h"
  167. #include "core/ParametricEqLinear.h"
  168. #include "core/Song.h"
  169. #include "core/Slicex.h"
  170. #include "core/NoteEdit.h"
  171. #include "core/ScaleSnap.h"
  172. #include "core/LiveRecordView.h"
  173. #include "core/Recorder.h"
  174. #include "core/KeyMap.h"
  175. #include "core/EventPaneSplit.h" // round 190fz
  176. #include "core/UndoKeys.h"
  177. #include "core/SortOnce.h"
  178. #include "core/RackOps.h"
  179. #include "core/PlaylistTrackOps.h"
  180. #include "core/ChannelLoops.h"
  181. #include "core/ChannelBurn.h"
  182. #include "core/AutoSampler.h"
  183. #include "core/ValueMath.h"
  184. #include "core/Workspace.h"
  185. #include "core/StartupSound.h"
  186. #include "core/ExportOptions.h"
  187. #include "core/Mp3Encoder.h"
  188. #include "core/PluginMonitor.h"
  189. #include "core/PluginSubstitutes.h"
  190. #include "core/ClipOps.h"
  191. #include "core/PlaylistOps.h"
  192. #include "core/PluginDrop.h"
  193. #include "core/QuickRender.h"
  194. #include "core/TouchKeyboard.h"
  195. #include "core/HeldNotes.h"
  196. #include "core/Templates.h"
  197. #include "flp/Flp.h"
  198. #include "ui/I18nData.h"
  199. #include "ui/I18nEnglish.h"
  200. #include "ui/I18nLocale.h"
  201. #include "core/CrashReport.h"
  202. #include "core/RenderAhead.h"
  203. #include "core/WorkerPool.h"
  204. namespace
  205. {
  206. int failures = 0;
  207. int checks = 0;
  208. std::string processTempTag(); // per-process suffix for scratch files (defined further down)
  209. std::vector<std::pair<const char*, std::function<void()>>>& registry()
  210. {
  211. static std::vector<std::pair<const char*, std::function<void()>>> r;
  212. return r;
  213. }
  214. struct Register
  215. {
  216. Register (const char* name, std::function<void()> fn) { registry().emplace_back (name, std::move (fn)); }
  217. };
  218. #define TEST(name) \
  219. static void name(); \
  220. static Register reg_##name (#name, name); \
  221. static void name()
  222. #define CHECK(cond) \
  223. do \
  224. { \
  225. ++checks; \
  226. if (! (cond)) \
  227. { \
  228. ++failures; \
  229. std::printf (" FAILED %s:%d: %s\n", __FILE__, __LINE__, #cond); \
  230. } \
  231. } while (false)
  232. // Engines that compute in x87 80-bit long double (FL's own arithmetic) match their goldens to the last bit on x86. On
  233. // ARM64 (Apple Silicon) long double is not the x87 format, so there they hold to a looser bound (round 117).
  234. // LYNX_X87_TOL overrides it (measuring).
  235. inline float armTolerance (float x86, float arm)
  236. {
  237. #if defined(__x86_64__) || defined(__i386__) || defined(_M_X64) || defined(_M_IX86)
  238. (void) arm;
  239. return x86;
  240. #else
  241. if (const char* t = std::getenv ("LYNX_X87_TOL"))
  242. return (float) std::atof (t);
  243. return arm;
  244. #endif
  245. }
  246. inline float x87Tolerance() { return armTolerance (2.0e-7f, 2.0e-4f); }
  247. inline float granulizerTolerance()
  248. {
  249. #if defined(__APPLE__)
  250. return 2.0e-4f;
  251. #else
  252. return armTolerance (1.0e-6f, 1.0e-5f);
  253. #endif
  254. }
  255. // Engines whose goldens depend on the C library's sin/cos/exp/pow: Windows (mingw), glibc and musl hold the 1e-7
  256. // goldens, Apple's libm does not (round 117), so on macOS the per-sample bound is looser.
  257. inline float libmTolerance (float strict)
  258. {
  259. #if defined(__APPLE__)
  260. (void) strict;
  261. return 2.0e-4f;
  262. #else
  263. return strict;
  264. #endif
  265. }
  266. // One golden sample: relative bound (with a 1e-3 floor on macOS, where near-zero samples carry libm noise); prints the
  267. // measured error when it fails.
  268. inline bool goldenSampleClose (float y, float golden, float tol)
  269. {
  270. #if defined(__APPLE__)
  271. const float scale = std::max (1.0e-3f, std::abs (golden));
  272. #else
  273. const float scale = std::max (1.0e-16f, std::abs (golden));
  274. #endif
  275. if (std::abs (y - golden) <= tol * scale)
  276. return true;
  277. std::fprintf (stderr, " golden sample %.9g vs %.9g (relative error %.3g, bound %.3g)\n", (double) y, (double) golden,
  278. (double) (std::abs (y - golden) / scale), (double) tol);
  279. return false;
  280. }
  281. std::string realFlpDir;
  282. // --- helpers for building synthetic .flp files -----------------------------
  283. struct FlpBuilder
  284. {
  285. std::vector<std::uint8_t> bytes;
  286. FlpBuilder (std::uint16_t ppq = 96)
  287. {
  288. put ("FLhd");
  289. u32 (6);
  290. u16 (0);
  291. u16 (1);
  292. u16 (ppq);
  293. put ("FLdt");
  294. u32 (0); // size is not used by the reader
  295. }
  296. void put (const char* s) { bytes.insert (bytes.end(), s, s + std::strlen (s)); }
  297. void u8 (std::uint8_t v) { bytes.push_back (v); }
  298. void u16 (std::uint16_t v) { u8 ((std::uint8_t) v); u8 ((std::uint8_t) (v >> 8)); }
  299. void u32 (std::uint32_t v) { u16 ((std::uint16_t) v); u16 ((std::uint16_t) (v >> 16)); }
  300. void varint (std::size_t n)
  301. {
  302. do
  303. {
  304. auto b = (std::uint8_t) (n & 0x7F);
  305. n >>= 7;
  306. u8 (n ? (std::uint8_t) (b | 0x80) : b);
  307. } while (n);
  308. }
  309. void data (std::uint8_t id, const std::vector<std::uint8_t>& payload)
  310. {
  311. u8 (id);
  312. varint (payload.size());
  313. bytes.insert (bytes.end(), payload.begin(), payload.end());
  314. }
  315. void ascii (std::uint8_t id, const std::string& s)
  316. {
  317. std::vector<std::uint8_t> p (s.begin(), s.end());
  318. p.push_back (0);
  319. data (id, p);
  320. }
  321. void utf16 (std::uint8_t id, const std::u16string& s)
  322. {
  323. std::vector<std::uint8_t> p;
  324. for (auto c : s)
  325. {
  326. p.push_back ((std::uint8_t) c);
  327. p.push_back ((std::uint8_t) (c >> 8));
  328. }
  329. p.push_back (0);
  330. p.push_back (0);
  331. data (id, p);
  332. }
  333. };
  334. std::vector<std::uint8_t> noteRecord (std::uint32_t pos, std::uint16_t channel, std::uint32_t len, std::uint16_t key,
  335. std::uint8_t velocity)
  336. {
  337. std::vector<std::uint8_t> r (24, 0);
  338. std::memcpy (&r[0], &pos, 4);
  339. std::memcpy (&r[6], &channel, 2);
  340. std::memcpy (&r[8], &len, 4);
  341. std::memcpy (&r[12], &key, 2);
  342. r[16] = 120;
  343. r[20] = 64;
  344. r[21] = velocity;
  345. return r;
  346. }
  347. } // namespace
  348. // ---------------------------------------------------------------------------
  349. // FLP
  350. TEST (flp_rejects_garbage)
  351. {
  352. std::vector<std::uint8_t> junk (64, 0x41);
  353. bool threw = false;
  354. try { flp::EventReader r (junk); } catch (const flp::Error&) { threw = true; }
  355. CHECK (threw);
  356. }
  357. TEST (flp_reads_size_classes_varint_and_extended_ids)
  358. {
  359. FlpBuilder b;
  360. b.u8 (0x15); b.u8 (2); // u8
  361. b.u8 (0x40); b.u16 (513); // u16
  362. b.u8 (0x9C); b.u32 (128500); // u32
  363. b.data (0xD5, std::vector<std::uint8_t> (300, 7)); // 2-byte varint
  364. b.u8 (0xAC); b.u32 (0xC0000100); b.varint (3); b.u8 (1); b.u8 (2); b.u8 (3); // extended ID
  365. flp::EventReader r (b.bytes);
  366. flp::Event e;
  367. CHECK (r.header().ppq == 96);
  368. CHECK (r.next (e) && e.id == 0x15 && e.value == 2);
  369. CHECK (r.next (e) && e.id == 0x40 && e.value == 513);
  370. CHECK (r.next (e) && e.id == 0x9C && e.value == 128500);
  371. CHECK (r.next (e) && e.id == 0xD5 && e.data.size() == 300 && e.data[299] == 7);
  372. CHECK (r.next (e) && e.id == 0xC0000100 && e.isData() && e.data.size() == 3 && e.data[2] == 3);
  373. CHECK (! r.next (e));
  374. }
  375. TEST (flp_truncated_file_throws)
  376. {
  377. FlpBuilder b;
  378. b.u8 (0x9C); b.u8 (1); // u32 with only 1 byte
  379. flp::EventReader r (b.bytes);
  380. flp::Event e;
  381. bool threw = false;
  382. try { r.next (e); } catch (const flp::Error&) { threw = true; }
  383. CHECK (threw);
  384. }
  385. TEST (flp_version_numbers)
  386. {
  387. CHECK (flp::parseVersionNumber ("25.2.5.5319") == 25002005u);
  388. CHECK (flp::parseVersionNumber ("20.99.3000.3031") == 20102000u);
  389. CHECK (flp::parseVersionNumber ("11.0.3") == 11000003u);
  390. }
  391. TEST (flp_playlist_record_size_by_version)
  392. {
  393. CHECK (flp::playlistRecordSize (12009003, 64) == 32);
  394. CHECK (flp::playlistRecordSize (21000000, 120) == 60);
  395. CHECK (flp::playlistRecordSize (25002005, 160) == 80);
  396. CHECK (flp::playlistRecordSize (25002005, 60) == 60); // size doesn't match the version -> tries the others
  397. CHECK (flp::playlistRecordSize (25002005, 7) == 0);
  398. }
  399. TEST (flp_playlist_record_size_by_file_flags)
  400. {
  401. // REA_rodada2 §4.2: 0xA9 bit 1 -> 80 B, bits 1 and 3 -> the native 88 B record (FL 25.2.99 / 26.x)
  402. CHECK (flp::playlistRecordSize (26001003, 88 * 4, 15) == 88); // 352 is also a multiple of 32
  403. CHECK (flp::playlistRecordSize (26001003, 88 * 10, 15) == 88); // ... and of 80
  404. CHECK (flp::playlistRecordSize (25002005, 160, 7) == 80);
  405. CHECK (flp::playlistRecordSize (21000000, 120, 1) == 60);
  406. CHECK (flp::playlistRecordSize (26001003, 63712) == 88); // no flags: the only size that fits besides 32
  407. }
  408. TEST (flp_native_88_byte_playlist_items_with_clip_gain_and_fades)
  409. {
  410. // an FL 26.1 file: 0xA9 = 15, four 88 B items (352 B: the old 32 B guess read garbage from it)
  411. FlpBuilder b (96);
  412. b.ascii (0xC7, "26.1.3.5570");
  413. b.u8 (0xA9); b.u32 (15);
  414. b.u8 (0x9C); b.u32 (130000);
  415. b.u8 (0x40); b.u16 (0);
  416. b.u8 (0x15); b.u8 (4); // audio clip channel
  417. b.utf16 (0xC4, u"C:\\nowhere\\clip.wav");
  418. std::vector<std::uint8_t> items;
  419. auto add = [&] (std::uint32_t pos, std::uint32_t len, std::uint16_t track, float fadeIn, float fadeOut, float gain) {
  420. std::vector<std::uint8_t> r (88, 0);
  421. const std::uint16_t base = 20480, idx = 0, rv = (std::uint16_t) (499 - track);
  422. const float offs[] = { -1.0f, -1.0f };
  423. std::memcpy (&r[0], &pos, 4);
  424. std::memcpy (&r[4], &base, 2);
  425. std::memcpy (&r[6], &idx, 2);
  426. std::memcpy (&r[8], &len, 4);
  427. std::memcpy (&r[12], &rv, 2);
  428. std::memcpy (&r[0x18], offs, 8);
  429. std::memcpy (&r[0x24], &fadeIn, 4);
  430. std::memcpy (&r[0x2C], &fadeOut, 4);
  431. std::memcpy (&r[0x34], &gain, 4);
  432. r[0x38] = (std::uint8_t) ((fadeIn > 0.0f ? 1 : 0) | (fadeOut > 0.0f ? 2 : 0)); // manual fades (round 180d)
  433. const double stretch = 1.0;
  434. const std::int32_t minusOne = -1;
  435. std::memcpy (&r[0x40], &stretch, 8);
  436. std::memcpy (&r[0x4C], &minusOne, 4);
  437. items.insert (items.end(), r.begin(), r.end());
  438. };
  439. add (0, 384, 0, 0.0f, 0.0f, 1.0f);
  440. add (384, 384, 1, 923.0769f, 461.5385f, 1.65f); // 2 beats in, 1 beat out at 130 BPM
  441. add (768, 96, 2, 0.0f, 0.0f, 0.0f);
  442. add (1536, 192, 3, 0.0f, 5000.0f, 0.5f); // fade longer than the clip
  443. b.data (0xE9, items);
  444. const auto p = flp::parseProject (b.bytes);
  445. CHECK (p.flagsA9 == 15);
  446. CHECK (p.arrangements.size() == 1 && p.arrangements[0].items.size() == 4);
  447. CHECK (p.warnings.empty());
  448. if (p.arrangements.size() == 1 && p.arrangements[0].items.size() == 4)
  449. {
  450. const auto& it = p.arrangements[0].items;
  451. CHECK (it[1].position == 384 && it[1].length == 384 && it[1].track == 1 && it[1].kind == flp::PlaylistItem::Kind::channelClip);
  452. CHECK (it[3].position == 1536 && it[3].track == 3);
  453. CHECK (std::abs (it[1].gain - 1.65f) < 1e-6f && std::abs (it[1].fadeInMs - 923.0769f) < 1e-3f);
  454. const auto song = lynx::importFlp (p);
  455. CHECK (song.playlist.size() == 4);
  456. if (song.playlist.size() == 4)
  457. {
  458. const auto beat = (std::int64_t) lynx::ppq;
  459. CHECK (song.playlist[0].gain == 1.0f && song.playlist[0].fadeIn == 0 && song.playlist[0].fadeOut == 0);
  460. CHECK (std::abs (song.playlist[1].gain - 1.65f) < 1e-6f);
  461. CHECK (std::llabs (song.playlist[1].fadeIn - 2 * beat) <= 1 && std::llabs (song.playlist[1].fadeOut - beat) <= 1);
  462. CHECK (song.playlist[2].gain == 0.0f);
  463. CHECK (song.playlist[3].fadeOut == song.playlist[3].length); // clamped
  464. }
  465. }
  466. }
  467. TEST (flp_channel_level_adjustments_and_stretched_clip_start)
  468. {
  469. // FL 26.1 audio clip channel (gf92 follow-up): 0xE5 level adjustments and a time-stretched clip whose item
  470. // start/end are in the sample's milliseconds ("Liwn Passar voz" ch5: 788 ms into a clip stretched by 1.0569)
  471. FlpBuilder b (96);
  472. b.ascii (0xC7, "26.1.3.5570");
  473. b.u8 (0xA9); b.u32 (15);
  474. b.u8 (0x9C); b.u32 (130000);
  475. b.u8 (0x40); b.u16 (0);
  476. b.u8 (0x15); b.u8 (4); // audio clip channel
  477. b.utf16 (0xC4, u"C:\\nowhere\\clip.wav");
  478. {
  479. std::vector<std::uint8_t> levels (24, 0);
  480. const std::int32_t pan = 6400, vol = 10000;
  481. std::memcpy (&levels[0], &pan, 4);
  482. std::memcpy (&levels[4], &vol, 4);
  483. b.data (0xDB, levels);
  484. std::vector<std::uint8_t> adjust (20, 0);
  485. const std::int32_t panAdj = 3200, volAdj = 7286;
  486. std::memcpy (&adjust[0], &panAdj, 4);
  487. std::memcpy (&adjust[4], &volAdj, 4);
  488. b.data (0xE5, adjust);
  489. std::vector<std::uint8_t> params (0x98, 0);
  490. const float time = 64.0f * 192.0f; // stretched to 64 beats (float: 0xA9 bit 0)
  491. const double end = 1.0;
  492. std::memcpy (&params[0x60], &time, 4);
  493. std::memcpy (&params[0x88], &end, 8);
  494. b.data (0xD7, params);
  495. }
  496. {
  497. std::vector<std::uint8_t> r (88, 0);
  498. const std::uint32_t pos = 0, len = 6144; // 64 beats
  499. const std::uint16_t base = 20480, idx = 0, rv = 499;
  500. const float offs[] = { 788.4615f, 788.4615f + 31218.92f }; // sample ms: 64 beats at 130 BPM x 1.056892
  501. const float gain = 1.0f;
  502. std::memcpy (&r[0], &pos, 4);
  503. std::memcpy (&r[4], &base, 2);
  504. std::memcpy (&r[6], &idx, 2);
  505. std::memcpy (&r[8], &len, 4);
  506. std::memcpy (&r[12], &rv, 2);
  507. std::memcpy (&r[0x18], offs, 8);
  508. std::memcpy (&r[0x34], &gain, 4);
  509. b.data (0xE9, r);
  510. }
  511. const auto p = flp::parseProject (b.bytes);
  512. CHECK (p.channels.size() == 1);
  513. if (p.channels.size() != 1)
  514. return;
  515. CHECK (p.channels[0].hasLevelAdjust && p.channels[0].panAdj == 3200 && p.channels[0].volAdj == 7286);
  516. const auto song = lynx::importFlp (p);
  517. CHECK (song.channels.size() == 1 && song.playlist.size() == 1);
  518. if (song.channels.size() != 1 || song.playlist.size() != 1)
  519. return;
  520. // gain = f(volume)·f(volAdj), pan = pan + panAdj (round 190dz: the adjustments are the channel's own Levels
  521. // adjustment, the knobs read as in FL)
  522. const auto expected = lynx::channelGain (10000.0f / 12800.0f) * lynx::channelGain (7286.0f / 12800.0f);
  523. CHECK (std::abs (lynx::channelGain (song.channels[0].volume) * lynx::levelAdjustGain (song.channels[0].misc.levels.volume) - expected) < 1e-5f);
  524. CHECK (std::abs (song.channels[0].volume - 10000.0f / 12800.0f) < 1e-6f);
  525. CHECK (std::abs (lynx::adjustedPan (song.channels[0].pan, song.channels[0].misc.levels.pan) - 0.5f) < 1e-6f);
  526. // 788.46 sample ms / 1.056892 = 746.0 ms = 1.6164 beats into the clip
  527. CHECK (song.channels[0].sampler.stretchBeats > 0.0f);
  528. CHECK (std::llabs (song.playlist[0].offset - (std::int64_t) std::llround (1.61638 * lynx::ppq)) <= 1);
  529. // REA_rodada110 §1: the sub-tick rest is kept (offset + fraction = the exact position, to the sample)
  530. 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;
  531. CHECK (std::abs ((double) song.playlist[0].offset + (double) song.playlist[0].offsetFraction - exact) < 1e-3);
  532. CHECK (std::abs (song.playlist[0].offsetFraction) <= 0.5f);
  533. }
  534. TEST (flp_parses_project_model)
  535. {
  536. FlpBuilder b (96);
  537. b.ascii (0xC7, "25.2.5.5319");
  538. b.u8 (0x9C); b.u32 (174500);
  539. b.utf16 (0xC2, u"Título");
  540. b.u8 (0x41); b.u16 (3);
  541. b.u8 (0x96); b.u32 (0x00A14547); // BGR in the file
  542. auto notes = noteRecord (96, 1, 48, 64, 100);
  543. auto n2 = noteRecord (0, 0, 0, 60, 128);
  544. notes.insert (notes.end(), n2.begin(), n2.end());
  545. b.data (0xE0, notes);
  546. b.u8 (0x40); b.u16 (0);
  547. b.u8 (0x15); b.u8 (0);
  548. b.utf16 (0xCB, u"Kick");
  549. {
  550. std::vector<std::uint8_t> levels (24, 0);
  551. const std::int32_t lv[] = { 3200, 12800, -150 };
  552. std::memcpy (levels.data(), lv, sizeof (lv));
  553. b.data (0xDB, levels);
  554. }
  555. b.u8 (0x68); b.u16 (5);
  556. b.u8 (0x40); b.u16 (1);
  557. b.u8 (0x15); b.u8 (2);
  558. b.utf16 (0xC9, u"FLEX");
  559. b.utf16 (0xCB, u"Lead");
  560. b.u8 (0x63); b.u16 (0);
  561. std::vector<std::uint8_t> item (80, 0);
  562. std::uint32_t pos = 384, len = 768;
  563. std::uint16_t base = 20480, idx = 20480 + 3, rv = 499 - 2;
  564. std::memcpy (&item[0], &pos, 4);
  565. std::memcpy (&item[4], &base, 2);
  566. std::memcpy (&item[6], &idx, 2);
  567. std::memcpy (&item[8], &len, 4);
  568. std::memcpy (&item[12], &rv, 2);
  569. b.data (0xE9, item);
  570. b.utf16 (0xCC, u"Drums");
  571. b.data (0xEC, std::vector<std::uint8_t> (12, 0));
  572. b.data (0xEC, std::vector<std::uint8_t> (12, 0));
  573. const auto p = flp::parseProject (b.bytes);
  574. CHECK (p.version == "25.2.5.5319");
  575. CHECK (std::abs (p.tempo - 174.5) < 1e-9);
  576. CHECK (p.title == "Título");
  577. CHECK (p.channels.size() == 2);
  578. CHECK (p.channels[0].name == "Kick" && p.channels[0].mixerTrack == 5);
  579. CHECK (p.channels[0].hasLevels && p.channels[0].pan == 3200 && p.channels[0].volume == 12800 && p.channels[0].pitchCents == -150);
  580. CHECK (p.channels[1].type == flp::ChannelType::instrument && p.channels[1].pluginName == "FLEX");
  581. CHECK (p.patterns.size() == 1 && p.patterns[0].index == 3);
  582. CHECK (p.patterns[0].colour && *p.patterns[0].colour == 0x4745A1);
  583. CHECK (p.patterns[0].notes.size() == 2 && p.patterns[0].notes[0].position == 0); // sorted
  584. CHECK (p.patterns[0].notes[1].key == 64 && p.patterns[0].notes[1].length == 48);
  585. CHECK (p.arrangements.size() == 1 && p.arrangements[0].items.size() == 1);
  586. const auto& it = p.arrangements[0].items[0];
  587. CHECK (it.kind == flp::PlaylistItem::Kind::pattern && it.index == 3 && it.track == 2 && it.position == 384);
  588. CHECK (p.mixer.size() == 2 && p.mixer[0].name == "Drums" && p.mixer[1].name.empty());
  589. CHECK (p.warnings.empty());
  590. }
  591. TEST (flp_fuzz_corrupted_files_do_not_crash)
  592. {
  593. if (realFlpDir.empty())
  594. return;
  595. // some real .flp files, corrupted in various ways: the parser may reject them (flp::Error), but
  596. // must never hang, read past the buffer or produce a Song with invalid indices
  597. std::vector<std::vector<std::uint8_t>> sources;
  598. for (auto& entry : std::filesystem::recursive_directory_iterator (realFlpDir))
  599. if (entry.path().extension() == ".flp" && sources.size() < 6 && std::filesystem::file_size (entry.path()) < 3'000'000)
  600. {
  601. std::ifstream in (entry.path(), std::ios::binary);
  602. sources.emplace_back ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  603. }
  604. std::uint32_t seed = 2024;
  605. auto rnd = [&] (std::uint32_t n) { seed = seed * 1664525u + 1013904223u; return n == 0 ? 0u : (seed >> 8) % n; };
  606. int parsed = 0, rejected = 0;
  607. for (int round = 0; round < 240 && ! sources.empty(); ++round)
  608. {
  609. auto bytes = sources[(std::size_t) rnd ((std::uint32_t) sources.size())];
  610. switch (round % 4)
  611. {
  612. case 0: bytes.resize (rnd ((std::uint32_t) bytes.size())); break; // truncated
  613. 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;
  614. case 2: // absurd event size
  615. for (int k = 0; k < 5; ++k)
  616. bytes[(std::size_t) rnd ((std::uint32_t) bytes.size())] = 0xFF;
  617. break;
  618. default: // shuffled block
  619. {
  620. const auto a = rnd ((std::uint32_t) bytes.size()), b = rnd ((std::uint32_t) bytes.size());
  621. std::reverse (bytes.begin() + std::min (a, b), bytes.begin() + std::max (a, b));
  622. break;
  623. }
  624. }
  625. try
  626. {
  627. const auto project = flp::parseProject (bytes);
  628. const auto song = lynx::importFlp (project);
  629. ++parsed;
  630. for (const auto& p : song.patterns)
  631. for (const auto& n : p.notes)
  632. CHECK (n.channel >= 0 && n.channel < (int) song.channels.size());
  633. for (const auto& c : song.playlist)
  634. CHECK (c.kind != lynx::ClipKind::pattern || (c.pattern >= 0 && c.pattern < (int) song.patterns.size()));
  635. }
  636. catch (const flp::Error&)
  637. {
  638. ++rejected;
  639. }
  640. }
  641. std::printf (" fuzz: %d imported, %d rejected\n", parsed, rejected);
  642. CHECK (parsed + rejected == 240);
  643. }
  644. TEST (plugin_state_decoders_survive_garbage)
  645. {
  646. // random blobs (varied sizes, including valid headers followed by garbage)
  647. std::uint32_t seed = 77;
  648. auto rnd = [&] { seed = seed * 1664525u + 1013904223u; return seed >> 8; };
  649. const char* plugins[] = { "Fruity Parametric EQ 2", "Fruity Limiter", "Fruity Reeverb 2", "Fruity Delay 2", "Fruity Delay 3",
  650. "Fruity Compressor", "Fruity Balance", "Fruity PanOMatic", "Fruity Stereo Shaper", "Fruity Phase Inverter" };
  651. int decoded = 0;
  652. for (int round = 0; round < 3000; ++round)
  653. {
  654. std::vector<std::uint8_t> blob ((std::size_t) (rnd() % 600));
  655. for (auto& b : blob)
  656. b = (std::uint8_t) rnd();
  657. if (blob.size() >= 4 && round % 3 == 0) // versions the readers accept
  658. {
  659. const std::uint32_t versions[] = { 8, 10001, 10000, 12, 14, 6, 1 };
  660. const auto v = versions[rnd() % 7];
  661. std::memcpy (blob.data(), &v, 4);
  662. }
  663. if (auto fx = lynx::effectFromFl (plugins[rnd() % 10], blob))
  664. {
  665. ++decoded;
  666. for (auto v : fx->params)
  667. CHECK (std::isfinite (v) && v >= 0.0f && v <= 1.0f);
  668. }
  669. lynx::SynthSettings synth;
  670. if (lynx::synthFrom3xOsc (blob, synth))
  671. for (const auto& o : synth.osc)
  672. CHECK (std::isfinite (o.level) && o.level >= 0.0f && o.level <= 1.0f);
  673. float harmlessGain = 1.0f;
  674. if (lynx::synthFromHarmless (blob, 120.0, synth, harmlessGain))
  675. CHECK (std::isfinite (harmlessGain) && std::isfinite (synth.cutoff) && std::isfinite (synth.attack));
  676. if (lynx::synthFromFruitKick (blob, synth))
  677. CHECK (std::isfinite (synth.decay) && std::isfinite (synth.pitchEnv.amount));
  678. if (lynx::synthFromGms (blob, 120.0, synth, harmlessGain) || lynx::synthFromHarmor (blob, synth, harmlessGain) || lynx::synthFromOgun (blob, synth, harmlessGain)
  679. || lynx::synthFromSakura (blob, synth) || lynx::synthFromKepler (blob, synth, harmlessGain))
  680. CHECK (std::isfinite (synth.cutoff) && std::isfinite (synth.attack) && std::isfinite (harmlessGain));
  681. if (auto sound = lynx::drumSynthLiveSound (blob))
  682. CHECK (sound->size() < 256);
  683. if (lynx::wavetrav::State wt; lynx::wavetrav::parseState (blob, wt))
  684. CHECK (wt.patches[60].waveN >= 0 && wt.patches[60].waveN < lynx::wavetrav::tableCapacity);
  685. lynx::synthFromBooBass (blob, synth);
  686. lynx::gmsStateUsable (blob); // (the exact engines' parsers reject garbage without crashing)
  687. lynx::morphineStateUsable (blob);
  688. lynx::poizoneStateUsable (blob);
  689. if (lynx::synthFromPoiZone (blob, synth, harmlessGain) || lynx::synthFromBassDrum (blob, synth, harmlessGain) || lynx::synthFromToxic (blob, synth, harmlessGain)
  690. || lynx::synthFromMorphine (blob, synth, harmlessGain))
  691. CHECK (std::isfinite (synth.cutoff) && std::isfinite (harmlessGain));
  692. if (auto patch = lynx::drumpadPatchName (blob))
  693. CHECK (patch->size() <= 0x30 && std::isfinite (lynx::drumToneForName (*patch).decay));
  694. if (auto flex = lynx::flexPresetName (blob))
  695. CHECK ((! flex->first.empty() || ! flex->second.empty()) && flex->second.size() <= 1024);
  696. if (lynx::synthFromSytrus (blob, 120.0, synth, harmlessGain))
  697. CHECK (std::isfinite (synth.cutoff) && std::isfinite (synth.attack));
  698. if (lynx::synthFromDX10 (blob, synth))
  699. CHECK (std::isfinite (synth.decay) && std::isfinite (synth.release) && std::isfinite (synth.osc[1].level));
  700. if (blob.size() >= 4 && round % 5 == 0) // MiniSynth header followed by garbage
  701. {
  702. std::memcpy (blob.data(), "SSLF", 4);
  703. if (blob.size() > 220)
  704. {
  705. std::memcpy (blob.data() + 40, "DATA", 4);
  706. std::memcpy (blob.data() + 48, "0rev", 4);
  707. }
  708. }
  709. if (lynx::synthFromMiniSynth (blob, 120.0, synth))
  710. CHECK (std::isfinite (synth.cutoff) && std::isfinite (synth.decay) && std::isfinite (synth.attack));
  711. if (lynx::miniSynthStateUsable (blob)) // the exact engine's parser and a short render with silent tables
  712. {
  713. const auto params = lynx::miniSynthParams (blob);
  714. CHECK (params.size() == 43u && std::all_of (params.begin(), params.end(), [] (float v) { return v >= 0.0f && v <= 1.0f; }));
  715. const auto y = lynx::minisynth::renderReference (blob, { { 0, 60, 1.0, 1.0, 0.0, 0.0, 64 } }, 44100.0, 128, "");
  716. CHECK (y.size() == 256u && std::all_of (y.begin(), y.end(), [] (float v) { return std::isfinite (v); }));
  717. }
  718. lynx::ForeignPlugin foreign;
  719. lynx::parseFlWrapper (blob, foreign);
  720. std::vector<std::uint8_t> component, controller;
  721. lynx::splitFlVst3State (blob, component, controller);
  722. }
  723. CHECK (decoded == 3000); // every known name yields an effect (default when the blob is unusable)
  724. }
  725. TEST (inflate_zlib)
  726. {
  727. // fixed Huffman with back-references
  728. const std::uint8_t fixed[] = { 0x78, 0xda, 0x4b, 0x4c, 0x4a, 0x4e, 0xc4, 0x86, 0x22, 0x00, 0x7c, 0x69, 0x09, 0x89 };
  729. const auto a = lynx::inflateZlib (fixed, sizeof (fixed));
  730. CHECK (a && std::string (a->begin(), a->end()) == "abcabcabcabcabcabcabcabcX");
  731. // stored (uncompressed) block
  732. const std::uint8_t stored[] = { 0x78, 0x01, 0x01, 0x03, 0x00, 0xfc, 0xff, 'L', 'Y', 'N', 0x00, 0x00, 0x00, 0x00 };
  733. const auto b = lynx::inflateZlib (stored, sizeof (stored));
  734. CHECK (b && std::string (b->begin(), b->end()) == "LYN");
  735. // truncated, bad header and size limit
  736. CHECK (! lynx::inflateZlib (fixed, 6));
  737. const std::uint8_t bad[] = { 0x12, 0x34, 0x00 };
  738. CHECK (! lynx::inflateZlib (bad, sizeof (bad)));
  739. CHECK (! lynx::inflateZlib (fixed, sizeof (fixed), false, 10));
  740. }
  741. TEST (fl_round15_effects_convert_known_values)
  742. {
  743. auto blob = [] (std::vector<std::int32_t> words) {
  744. std::vector<std::uint8_t> b (words.size() * 4);
  745. std::memcpy (b.data(), words.data(), b.size());
  746. return b;
  747. };
  748. // Frequency Shifter "+800 Hz": frequency 51542 in the 20 kHz range, 100% wet — FL's own engine since round 154
  749. // (its controls as (raw − min) / range); unreadable states keep the generic shifter
  750. const auto fs = lynx::effectFromFl ("Frequency Shifter", blob ({ 4, 10000, 0, 51542, 0, 10000, 10000, 5000, 10000, 0, 5000, 0, 0 }));
  751. CHECK (fs && fs->kind == lynx::EffectKind::flFrequencyShifter);
  752. if (fs)
  753. {
  754. CHECK (std::abs (fs->params[0] - 1.0f) < 1e-6f && fs->params[1] == 0.0f);
  755. CHECK (std::abs (fs->params[2] - (51542.0f + 70000.0f) / 140000.0f) < 1e-6f);
  756. }
  757. const auto fsOld = lynx::effectFromFl ("Frequency Shifter", blob ({ 4, 10000, 0 }));
  758. CHECK (fsOld && fsOld->kind == lynx::EffectKind::freqShift);
  759. // Emphasis (Bien Duro's Master): FL's own engine since round 167f, its controls as (raw − min) / range: input 625
  760. // (+1.5 dB), ceiling 9308 (−1.60 dB), envelope order 2, true peak; an unreadable state keeps the limiter
  761. const auto em = lynx::effectFromFl ("Emphasis", blob ({ 8, 0, 0, 2, 1, 0, 0, 625, 10000, 10000, 9308, 0, 2, 7, 0 }));
  762. CHECK (em && em->kind == lynx::EffectKind::flEmphasis && em->enabled);
  763. if (em)
  764. {
  765. CHECK (std::abs (em->params[6] - 0.0625f) < 1e-6f && std::abs (em->params[9] - 0.9308f) < 1e-6f);
  766. 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);
  767. CHECK (lynx::formatEffectParam (lynx::EffectKind::flEmphasis, 6, em->params[6], 120.0) == "+1.5 dB");
  768. CHECK (lynx::formatEffectParam (lynx::EffectKind::flEmphasis, 9, em->params[9], 120.0) == "-1.60 dB");
  769. CHECK (em->params[14] == 0.0f && em->params[15] == 0.0f); // a version-8 state has no option bytes: off
  770. }
  771. const auto emOld = lynx::effectFromFl ("Emphasis", blob ({ 8, 0, 0, 2 }));
  772. CHECK (emOld && emOld->kind == lynx::EffectKind::limiter);
  773. // round 167f: FL's legacy dB Meter / Spectroman keep their slot as LYNX's analyser in the matching view
  774. const auto dbm = lynx::effectFromFl ("Fruity dB Meter", {});
  775. CHECK (dbm && dbm->kind == lynx::EffectKind::waveCandy
  776. && dbm->params[0] == lynx::visualfx::wavecandy::modeValue (lynx::visualfx::wavecandy::Mode::peakMeter));
  777. const auto spm = lynx::effectFromFl ("Fruity Spectroman", {});
  778. CHECK (spm && spm->kind == lynx::EffectKind::waveCandy
  779. && spm->params[0] == lynx::visualfx::wavecandy::modeValue (lynx::visualfx::wavecandy::Mode::spectrum));
  780. // Pitch Shifter +12 st, mix 100%
  781. 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 }));
  782. CHECK (ps && ps->kind == lynx::EffectKind::flPitchShifter && ps->params[0] == 1.0f && ps->params[3] == 1.0f); // REA_rodada61
  783. }
  784. TEST (frequency_shifter_moves_spectrum_by_hz)
  785. {
  786. auto fx = lynx::makeEffect (lynx::EffectKind::freqShift);
  787. fx->prepare (48000.0);
  788. auto slot = lynx::makeEffectSlot (lynx::EffectKind::freqShift);
  789. // +200 Hz: x·|x|·2000 = 200 -> x = sqrt(0.1)
  790. slot.params[0] = (float) (0.5 + std::sqrt (0.1) / 2.0);
  791. std::vector<float> l (48000), r (48000);
  792. for (int i = 0; i < 48000; ++i)
  793. l[(std::size_t) i] = r[(std::size_t) i] = (float) std::sin (2.0 * 3.141592653589793 * 1000.0 * i / 48000.0);
  794. for (int done = 0; done < 48000; done += 480)
  795. fx->process (l.data() + done, r.data() + done, 480, slot.params, 120.0);
  796. auto energy = [&] (double f) {
  797. const auto w = 2.0 * 3.141592653589793 * f / 48000.0, k = 2.0 * std::cos (w);
  798. double s1 = 0.0, s2 = 0.0;
  799. for (int i = 9600; i < 48000; ++i)
  800. {
  801. const auto s0 = l[(std::size_t) i] + k * s1 - s2;
  802. s2 = s1;
  803. s1 = s0;
  804. }
  805. return s1 * s1 + s2 * s2 - k * s1 * s2;
  806. };
  807. // the energy moves to 1200 Hz; little remains at 1000 and almost nothing at the image (800)
  808. CHECK (energy (1200.0) > 50.0 * energy (1000.0));
  809. CHECK (energy (1200.0) > 50.0 * energy (800.0));
  810. }
  811. TEST (pitch_shift_moves_frequency_keeps_length)
  812. {
  813. auto fx = lynx::makeEffect (lynx::EffectKind::pitchShift);
  814. fx->prepare (48000.0);
  815. auto slot = lynx::makeEffectSlot (lynx::EffectKind::pitchShift);
  816. auto crossingsAfter = [&] (float semitoneParam) {
  817. fx->reset();
  818. slot.params[0] = semitoneParam;
  819. std::vector<float> l (48000), r (48000);
  820. for (int i = 0; i < 48000; ++i)
  821. l[(std::size_t) i] = r[(std::size_t) i] = (float) std::sin (2.0 * 3.141592653589793 * 220.0 * i / 48000.0);
  822. for (int done = 0; done < 48000; done += 512)
  823. fx->process (l.data() + done, r.data() + done, std::min (512, 48000 - done), slot.params, 120.0);
  824. int crossings = 0;
  825. for (int i = 9600; i < 48000; ++i)
  826. crossings += (l[(std::size_t) i - 1] < 0.0f) != (l[(std::size_t) i] < 0.0f);
  827. for (auto v : l)
  828. CHECK (std::isfinite (v) && std::abs (v) < 1.6f);
  829. return crossings / 0.8 / 2.0; // cycles per second
  830. };
  831. CHECK (std::abs (crossingsAfter (0.5f) - 220.0) < 8.0); // 0 st
  832. CHECK (std::abs (crossingsAfter (0.5f + 12.0f / 48.0f) - 440.0) < 25.0); // +12 st
  833. CHECK (std::abs (crossingsAfter (0.5f - 12.0f / 48.0f) - 110.0) < 12.0); // −12 st
  834. }
  835. // A Gross Beat state in FL's layout (REA_rodada53 §1), built here: version 8, parameters, options, 72 slots with a name,
  836. // sync bytes and a map envelope (kind 3, version 3, points: Δx in beats, level, tension, curve type)
  837. namespace
  838. {
  839. struct GbPoint
  840. {
  841. double dx, y;
  842. std::uint8_t type;
  843. };
  844. std::vector<std::uint8_t> grossBeatState (const std::array<int, 9>& params, const std::array<std::uint8_t, 9>& options,
  845. const std::map<int, std::vector<GbPoint>>& slots)
  846. {
  847. std::vector<std::uint8_t> b;
  848. 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); };
  849. auto i32 = [&put] (std::int32_t v) { put (&v, 4); };
  850. i32 (0x00080003);
  851. for (auto v : params)
  852. i32 (v);
  853. put (options.data(), options.size());
  854. for (int k = 0; k < 72; ++k)
  855. {
  856. b.push_back (0); // empty name
  857. i32 (0), i32 (5), i32 (0); // trigger sync off, position sync 4 beats, no hold
  858. b.push_back (0), b.push_back (0), b.push_back (0);
  859. const auto it = slots.find (k);
  860. const std::vector<GbPoint> flat { { 0.0, 1.0, 0 }, { 4.0, 1.0, 0 } };
  861. const auto& pts = it != slots.end() ? it->second : flat;
  862. i32 (3), i32 (3), i32 ((std::int32_t) pts.size());
  863. for (const auto& p : pts)
  864. {
  865. put (&p.dx, 8), put (&p.y, 8);
  866. const float tension = 0.0f;
  867. put (&tension, 4);
  868. b.push_back (p.type), b.push_back (0), b.push_back (0), b.push_back (2);
  869. }
  870. i32 (0), i32 (-1), i32 (-1), i32 (-1), i32 (-1);
  871. }
  872. return b;
  873. }
  874. std::vector<float> renderGrossBeat (const lynx::EffectSlot& slot, const std::vector<float>& in, double rate, double bpm)
  875. {
  876. auto fx = lynx::makeEffect (slot);
  877. fx->prepare (rate);
  878. auto l = in, r = in;
  879. const auto tps = bpm / 60.0 * lynx::ppq / rate;
  880. for (std::size_t done = 0; done < l.size(); done += 512)
  881. {
  882. const auto n = (int) std::min<std::size_t> (512, l.size() - done);
  883. fx->setSongPosition ((double) done * tps);
  884. fx->process (l.data() + done, r.data() + done, n, slot.params, bpm);
  885. }
  886. return l;
  887. }
  888. } // namespace
  889. TEST (gross_beat_state_container_and_parameters)
  890. {
  891. const auto state = grossBeatState ({ 3, 40, 128, 256, 1765, 5000, -77, 0, 0x10000 }, { 0, 0, 0, 0, 0, 0, 0, 1, 1 }, {});
  892. const auto parsed = lynx::grossbeat::parseState (state.data(), state.size());
  893. CHECK (parsed.has_value());
  894. CHECK (parsed && parsed->version == 8 && parsed->params[6] == -77 && parsed->slots[0].flat && parsed->slots[71].flat);
  895. const auto slot = lynx::grossbeat::slotFromFl (state, 960);
  896. CHECK (slot && slot->kind == lynx::EffectKind::grossBeat);
  897. std::vector<std::uint8_t> fl;
  898. int ppq = 0;
  899. CHECK (slot && lynx::grossbeat::unpack (slot->state, fl, ppq) && fl == state && ppq == 960);
  900. // every raw FL value survives the 0..1 parameter
  901. const int lo[8] { 0, 0, 0, 0, 0, 0, -256, 0 }, hi[8] { 35, 35, 256, 256, 10000, 10000, 256, 1 };
  902. bool roundTrip = true;
  903. for (int i = 0; i < 8; ++i)
  904. for (int raw = lo[i]; raw <= hi[i]; ++raw)
  905. roundTrip = roundTrip && lynx::grossbeat::rawFromParam (i, lynx::grossbeat::paramFromRaw (i, raw)) == raw;
  906. CHECK (roundTrip);
  907. CHECK (slot && slot->params[1] == lynx::grossbeat::paramFromRaw (1, 35)); // the volume slot 40 is clamped to 35
  908. CHECK (! lynx::grossbeat::parseState (state.data(), 100).has_value());
  909. }
  910. TEST (gross_beat_volume_slot_gates_the_bar)
  911. {
  912. // volume slot 1: open for two beats, closed for two (hold segments); no smoothing; dry path 15 samples late
  913. 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 } };
  914. const auto state = grossBeatState ({ 0, 1, 256, 256, 0, 0, 0, 0, 0x10000 }, { 0, 0, 0, 0, 0, 0, 0, 1, 1 }, { { 37, gate } });
  915. const auto slot = lynx::grossbeat::slotFromFl (state);
  916. CHECK (slot.has_value());
  917. if (! slot)
  918. return;
  919. const auto out = renderGrossBeat (*slot, std::vector<float> (200000, 1.0f), 48000.0, 120.0); // a beat = 24000 samples
  920. CHECK (out[14] == 0.0f && out[15] == 1.0f);
  921. CHECK (out[30000] == 1.0f && out[47990] == 1.0f);
  922. CHECK (out[48010] == 0.0f && out[95990] == 0.0f);
  923. CHECK (out[96010] == 1.0f && out[150000] == 0.0f);
  924. }
  925. TEST (gross_beat_time_slot_reads_the_past_bar)
  926. {
  927. // time slot 1 at level 0.5: the buffer is read one bar back (two bars = level 0), plus the 15-sample latency
  928. const std::vector<GbPoint> half { { 0.0, 0.5, 0 }, { 4.0, 0.5, 2 } };
  929. const auto state = grossBeatState ({ 1, 0, 256, 256, 0, 0, 0, 0, 0x10000 }, { 0, 0, 0, 0, 0, 0, 0, 1, 1 }, { { 1, half } });
  930. const auto slot = lynx::grossbeat::slotFromFl (state);
  931. CHECK (slot.has_value());
  932. if (! slot)
  933. return;
  934. std::vector<float> in (150000, 0.0f);
  935. in[1000] = 1.0f;
  936. const auto out = renderGrossBeat (*slot, in, 48000.0, 120.0); // a bar = 96000 samples
  937. std::size_t peak = 0;
  938. for (std::size_t i = 0; i < out.size(); ++i)
  939. if (std::abs (out[i]) > std::abs (out[peak]))
  940. peak = i;
  941. CHECK (peak == 1000 + 96000 + 15);
  942. CHECK (std::abs (out[peak] - 1.0f) < 1e-6f);
  943. float elsewhere = 0.0f;
  944. for (std::size_t i = 0; i < out.size(); ++i)
  945. if (i + 20 < peak || i > peak + 20)
  946. elsewhere = std::max (elsewhere, std::abs (out[i]));
  947. CHECK (elsewhere == 0.0f);
  948. }
  949. TEST (volume_gate_follows_bar_position)
  950. {
  951. auto fx = lynx::makeEffect (lynx::EffectKind::volumeGate);
  952. fx->prepare (48000.0);
  953. auto slot = lynx::makeEffectSlot (lynx::EffectKind::volumeGate);
  954. for (int i = 0; i < 32; ++i)
  955. slot.params[(std::size_t) i] = i < 16 ? 1.0f : 0.0f; // first two beats open, two closed
  956. slot.params[33] = slot.params[34] = 0.0f; // no smoothing
  957. // 120 BPM: one beat = 0.5 s = 24000 samples; the 4-beat bar starts at tick 0
  958. std::vector<float> l (96000, 1.0f), r (96000, 1.0f);
  959. fx->setSongPosition (0.0);
  960. for (int done = 0; done < 96000; done += 512)
  961. fx->process (l.data() + done, r.data() + done, std::min (512, 96000 - done), slot.params, 120.0);
  962. CHECK (l[10000] > 0.99f && l[40000] > 0.99f); // beats 1 and 2
  963. CHECK (l[60000] < 0.01f && l[90000] < 0.01f); // beats 3 and 4
  964. // starting mid-bar (beat 3): already closed
  965. std::vector<float> l2 (4800, 1.0f), r2 (4800, 1.0f);
  966. fx->reset();
  967. fx->setSongPosition (2.0 * lynx::ppq);
  968. fx->process (l2.data(), r2.data(), 4800, slot.params, 120.0);
  969. CHECK (l2[4000] < 0.01f);
  970. // mix 0: transparent
  971. slot.params[32] = 0.0f;
  972. std::vector<float> l3 (4800, 1.0f), r3 (4800, 1.0f);
  973. fx->setSongPosition (2.0 * lynx::ppq);
  974. fx->process (l3.data(), r3.data(), 4800, slot.params, 120.0);
  975. CHECK (l3[4000] > 0.99f);
  976. }
  977. TEST (patcher_branch_heads_follow_rea)
  978. {
  979. auto words = [] (std::vector<std::int32_t> v) {
  980. std::vector<std::uint8_t> b (v.size() * 4);
  981. std::memcpy (b.data(), v.data(), b.size());
  982. return b;
  983. };
  984. // Frequency Splitter, 2 bands, destinations 1/2/3, slope combo 3 (24 dB/oct), high send −6 dB
  985. std::vector<std::int32_t> fs (18, 0);
  986. fs[0] = 4;
  987. fs[1] = 32768;
  988. fs[3] = 3;
  989. fs[6] = 0;
  990. fs[11] = -600;
  991. fs[15] = 1;
  992. fs[16] = 2;
  993. fs[17] = 3;
  994. const auto split = words (fs);
  995. const auto low = lynx::patcherBranchHead ("Frequency Splitter", split, 1);
  996. CHECK (low.valid && low.slot && low.note.empty());
  997. if (low.slot)
  998. {
  999. // the exact engine (REA_rodada62): the low band's send, 24 dB/oct
  1000. CHECK (low.slot->kind == lynx::EffectKind::frequencySplitter && low.slot->params[26] == 1.0f && low.slot->params[27] == 0.0f);
  1001. CHECK (low.slot->params[2] == 3.0f / 7.0f);
  1002. }
  1003. const auto mid = lynx::patcherBranchHead ("Frequency Splitter", split, 2);
  1004. 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
  1005. const auto high = lynx::patcherBranchHead ("Frequency Splitter", split, 3);
  1006. CHECK (high.valid && high.slot && high.slot->params[28] == 1.0f && high.slot->params[10] == (float) (1200.0 / 3600.0));
  1007. CHECK (! lynx::patcherBranchHead ("Frequency Splitter", split, 7).valid);
  1008. // Stereo Shaper: the difference destination (4) is the exact engine with its difference output (REA_rodada54)
  1009. const auto shaper = words ({ 3, 0, 12800, 12800, 0, 0, 0, 4, 0 });
  1010. const auto diff = lynx::patcherBranchHead ("Fruity Stereo Shaper", shaper, 4);
  1011. CHECK (diff.valid && diff.slot && diff.slot->kind == lynx::EffectKind::stereoShaper && diff.slot->params[7] == 1.0f);
  1012. CHECK (! lynx::patcherBranchHead ("Fruity Stereo Shaper", shaper, 2).valid);
  1013. }
  1014. TEST (parallel_container_sums_branches)
  1015. {
  1016. // two branches: dry with gain 0.5 and a utility that flips phase (matrix −1) with gain 0.5 -> silence
  1017. auto slot = lynx::makeEffectSlot (lynx::EffectKind::parallel);
  1018. lynx::EffectBranch dry;
  1019. dry.gain = 0.5f;
  1020. lynx::EffectBranch inverted;
  1021. inverted.gain = 0.5f;
  1022. auto flip = lynx::makeEffectSlot (lynx::EffectKind::utility);
  1023. flip.params[2] = flip.params[5] = 0.25f; // (−1 + 2)/4
  1024. inverted.chain.push_back (flip);
  1025. slot.branches = { dry, inverted };
  1026. auto fx = lynx::makeParallelEffect (slot);
  1027. fx->prepare (48000.0);
  1028. std::vector<float> l (9000, 0.8f), r (9000, -0.4f);
  1029. fx->process (l.data(), r.data(), 9000, slot.params, 120.0);
  1030. CHECK (std::abs (l[100]) < 1e-5f && std::abs (r[8999]) < 1e-5f);
  1031. // only the dry branch with gain 2: doubles
  1032. slot.branches = { lynx::EffectBranch { {}, 2.0f } };
  1033. auto doubler = lynx::makeParallelEffect (slot);
  1034. doubler->prepare (48000.0);
  1035. std::fill (l.begin(), l.end(), 0.25f);
  1036. doubler->process (l.data(), r.data(), 9000, slot.params, 120.0);
  1037. CHECK (std::abs (l[5000] - 0.5f) < 1e-6f);
  1038. }
  1039. TEST (fl_controller_constant_outputs)
  1040. {
  1041. std::vector<std::int32_t> xy { 2, 32768, 0, 65536, 0, 16384, 0, 65536, 0, 1024, 256 };
  1042. std::vector<std::uint8_t> blob (xy.size() * 4 + 8, 0);
  1043. std::memcpy (blob.data(), xy.data(), xy.size() * 4);
  1044. CHECK (std::abs (lynx::controllerConstantOutput ("Fruity X-Y Controller", blob, 0).value_or (-1.0f) - 0.5f) < 1e-4f);
  1045. CHECK (std::abs (lynx::controllerConstantOutput ("Fruity X-Y Controller", blob, 1).value_or (-1.0f) - 0.25f) < 1e-4f);
  1046. // Control Surface: version + group { 0x836: value 0.75, -, output 2 }
  1047. std::vector<std::uint8_t> cs (4, 0);
  1048. cs[0] = 1;
  1049. auto chunk = [] (std::vector<std::uint8_t>& out, std::uint32_t id, const std::vector<std::uint8_t>& body) {
  1050. const auto size = (std::uint64_t) body.size();
  1051. out.insert (out.end(), reinterpret_cast<const std::uint8_t*> (&id), reinterpret_cast<const std::uint8_t*> (&id) + 4);
  1052. out.insert (out.end(), reinterpret_cast<const std::uint8_t*> (&size), reinterpret_cast<const std::uint8_t*> (&size) + 8);
  1053. out.insert (out.end(), body.begin(), body.end());
  1054. };
  1055. std::vector<std::uint8_t> value (12, 0);
  1056. const float v = 0.75f;
  1057. const std::int32_t out = 2;
  1058. std::memcpy (value.data(), &v, 4);
  1059. std::memcpy (value.data() + 8, &out, 4);
  1060. std::vector<std::uint8_t> group;
  1061. chunk (group, 0x836, value);
  1062. chunk (cs, 0, group);
  1063. CHECK (std::abs (lynx::controllerConstantOutput ("Control Surface", cs, 2).value_or (-1.0f) - 0.75f) < 1e-6f);
  1064. CHECK (! lynx::controllerConstantOutput ("Control Surface", cs, 0).has_value());
  1065. }
  1066. TEST (flp_midi_out_notes_reach_wrapper_on_same_port)
  1067. {
  1068. auto record = [] (std::vector<std::uint8_t>& out, std::uint32_t id, std::vector<std::uint8_t> body) {
  1069. const auto size = (std::uint64_t) body.size();
  1070. out.insert (out.end(), reinterpret_cast<const std::uint8_t*> (&id), reinterpret_cast<const std::uint8_t*> (&id) + 4);
  1071. out.insert (out.end(), reinterpret_cast<const std::uint8_t*> (&size), reinterpret_cast<const std::uint8_t*> (&size) + 8);
  1072. out.insert (out.end(), body.begin(), body.end());
  1073. };
  1074. auto i32s = [] (std::vector<std::int32_t> v) {
  1075. std::vector<std::uint8_t> b (v.size() * 4);
  1076. std::memcpy (b.data(), v.data(), b.size());
  1077. return b;
  1078. };
  1079. flp::Project p;
  1080. flp::Channel midiOut;
  1081. midiOut.index = 0;
  1082. midiOut.type = flp::ChannelType::native;
  1083. midiOut.pluginName = "MIDI Out";
  1084. midiOut.pluginState = i32s ({ 7, 0, -1, -1, -1, 0, 3, 0 }); // channel 1, port 3 (@24)
  1085. flp::Channel wrapper;
  1086. wrapper.index = 1;
  1087. wrapper.type = flp::ChannelType::native;
  1088. wrapper.pluginName = "Fruity Wrapper";
  1089. wrapper.pluginState = i32s ({ 9 });
  1090. record (wrapper.pluginState, 0x32, i32s ({ 7 })); // VST3
  1091. record (wrapper.pluginState, 0x01, i32s ({ 3, -1, 0, 0, 0 })); // input port 3
  1092. p.channels = { midiOut, wrapper };
  1093. flp::Pattern pattern;
  1094. pattern.index = 1;
  1095. flp::Note note;
  1096. note.channel = 0;
  1097. note.key = 64;
  1098. note.length = 96;
  1099. pattern.notes.push_back (note);
  1100. p.patterns.push_back (pattern);
  1101. const auto song = lynx::importFlp (p);
  1102. // round 133: the MIDI Out channel stays a live route (its notes are sent at play time, with its MIDI channel)
  1103. CHECK (song.channels.size() == 2 && song.channels[0].instrument == lynx::InstrumentKind::midiOut && ! song.channels[0].muted);
  1104. CHECK (song.channels[1].plugin.foreign.midiInPort == 3 && song.channels[1].plugin.midiInPort == 3);
  1105. CHECK (lynx::midictl::parseMidiOut (song.channels[0].pluginState).port == 3);
  1106. int toWrapper = 0;
  1107. for (const auto& pat : song.patterns)
  1108. for (const auto& n : pat.notes)
  1109. toWrapper += n.channel == 1 && n.key == 64;
  1110. CHECK (toWrapper == 0);
  1111. }
  1112. TEST (flp_effect_automation_follows_slots_and_params)
  1113. {
  1114. // track 1: slot 0 with an effect that has no equivalent (dropped), slot 2 with Parametric EQ 2
  1115. flp::Project p;
  1116. flp::MixerInsert insert;
  1117. insert.index = 1;
  1118. insert.slots.push_back ({ 0, "Some Unknown FX", {} });
  1119. insert.slots.push_back ({ 2, "Fruity Parametric EQ 2", {} });
  1120. p.mixer.push_back (insert);
  1121. auto clip = [&] (int index, std::uint32_t target) {
  1122. flp::Channel c;
  1123. c.index = index;
  1124. c.type = flp::ChannelType::automationClip;
  1125. c.automation = { { 0.0, 0.0 }, { 96.0, 1.0 } };
  1126. p.channels.push_back (c);
  1127. p.automationLinks.push_back ({ index, { target } });
  1128. };
  1129. 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; };
  1130. clip (0, mixer (1, 2, 0x8000 + 7)); // EQ band 1 frequency
  1131. clip (1, mixer (1, 2, 0x1F01)); // EQ slot mix
  1132. clip (2, mixer (1, 0, 0x8000)); // parameter of the dropped effect
  1133. clip (3, mixer (1, 2, 0x8000 + 21)); // band type: no equivalent
  1134. const auto song = lynx::importFlp (p);
  1135. CHECK (song.automations.size() == 4);
  1136. CHECK (song.mixer[1].effects.size() == 1 && song.mixer[1].effects[0].kind == lynx::EffectKind::parametricEq);
  1137. const auto& freq = song.automations[0];
  1138. CHECK (freq.target.kind == lynx::AutomationTargetKind::effectParam && freq.target.index == 1 && freq.target.slot == 0);
  1139. CHECK (freq.target.param == lynx::peq::param (0, 1));
  1140. // FL's 0..1 (20 Hz..20 kHz) becomes our frequency scale
  1141. CHECK (std::abs (freq.points.front().value - lynx::peq::hzValue (20.0)) < 1e-4f);
  1142. CHECK (std::abs (freq.points.back().value - lynx::peq::hzValue (20000.0)) < 1e-4f);
  1143. CHECK (song.automations[1].target.kind == lynx::AutomationTargetKind::effectMix && song.automations[1].target.slot == 0);
  1144. CHECK (song.automations[2].target.kind == lynx::AutomationTargetKind::none);
  1145. CHECK (song.automations[3].target.kind == lynx::AutomationTargetKind::none);
  1146. // the automated mix is read/written through the target
  1147. auto copy = song;
  1148. CHECK (copy.setTargetValue (song.automations[1].target, 0.25f) && std::abs (copy.mixer[1].effects[0].mix - 0.25f) < 1e-6f);
  1149. CHECK (std::abs (copy.targetValue (song.automations[1].target) - 0.25f) < 1e-6f);
  1150. }
  1151. TEST (flp_real_files)
  1152. {
  1153. if (realFlpDir.empty())
  1154. {
  1155. std::printf (" (skipped: pass a folder with .flp files as argument)\n");
  1156. return;
  1157. }
  1158. int files = 0, withNotes = 0, withPlaylist = 0, automations = 0, automationsWithTarget = 0, sends = 0;
  1159. int threeOsc = 0, threeOscDecoded = 0, wrapped = 0, wrappedParsed = 0, wrappedVst2 = 0, wrappedVst2State = 0, wrappedVst2Old = 0, markers = 0, signatures = 0, trackEqs = 0;
  1160. int harmless = 0, harmlessDecoded = 0, harmlessParsed = 0, harmlessKind = 0, sytrus = 0, sytrusDecoded = 0, sytrusEngine = 0, sytrusRendered = 0,
  1161. sytrusSounding = 0;
  1162. int harmor = 0, harmorUsable = 0, harmorKind = 0, harmorSounding = 0;
  1163. int gms = 0, gmsUsable = 0, gmsKind = 0, gmsSounding = 0, morphine = 0, morphineUsable = 0, morphineKind = 0, morphineSounding = 0, demoGoldens = 0;
  1164. int ogun = 0, ogunUsable = 0, ogunKind = 0, ogunSounding = 0;
  1165. int autogun = 0, autogunUsable = 0, autogunKind = 0, autogunWithSg = 0, autogunSounding = 0; // REA_rodada57
  1166. int poizone = 0, poizoneUsable = 0, poizoneKind = 0, poizoneSounding = 0, poizoneGoldens = 0;
  1167. int miniSynth = 0, miniSynthUsable = 0, miniSynthKind = 0, miniSynthSounding = 0, miniSynthGoldens = 0;
  1168. int dx10 = 0, dx10Usable = 0, dx10Kind = 0, dx10Sounding = 0;
  1169. int kepler = 0, keplerUsable = 0, keplerKind = 0, keplerSounding = 0;
  1170. int toxic = 0, toxicUsable = 0, toxicKind = 0, toxicSounding = 0, toxicGoldens = 0; // REA_rodada45
  1171. int threeOscKind = 0, threeOscSounding = 0;
  1172. std::map<std::string, std::pair<int, int>> generators; // GMS/Ogun/Autogun/Sakura/DrumSynth: channels, decoded
  1173. int ogunTrumpet = 0; // Ogun channels whose spectrum matches the REA "Trumpet" reference
  1174. int peqSlots = 0, peqShaped = 0, reverbs = 0, reverbsDryless = 0, reverbsLowCut = 0;
  1175. int dwChannels = 0, dwZones = 0, fpcKits = 0, padChannels = 0, padNotes = 0, dwExact = 0;
  1176. int harmlessCompWanted = 0, harmlessCompLoaded = 0; // Harmless rack compressors / presets read from the installation
  1177. int maximusSlots = 0, maximusExact = 0, sgzSlots = 0, sgzExact = 0, vocodexSlots = 0, vocodexWithData = 0; // REA_rodada40
  1178. int shaperSlots = 0, shaperExact = 0, clipperSlots = 0, clipperExact = 0; // REA_rodada51
  1179. int flPeqSlots = 0, flPeqExact = 0; // REA_rodada50
  1180. int basicSlots = 0, basicExact = 0; // REA_rodada58: PanOMatic, Balance, Blood Overdrive
  1181. int equoSlots = 0, equoExact = 0, pitchShifterSlots = 0, pitchShifterExact = 0; // REA_rodada61
  1182. int filterSlots = 0, filterExact = 0; // REA_rodada63: Fast LP, Free Filter, Fruity Filter
  1183. int distructorSlots = 0, distructorExact = 0, distructorImpulses = 0; // REA_rodada71
  1184. int delay2Slots = 0, delay2Exact = 0, delay3Slots = 0, delay3Exact = 0; // REA_rodada52
  1185. int chorusSlots = 0, chorusExact = 0; // REA_rodada59: the exact choruses
  1186. int fastDistSlots = 0, fastDistExact = 0, flangusSlots = 0, flangusExact = 0, flangerSlots = 0, flangerExact = 0; // REA_rodada55
  1187. int phaserSlots = 0, phaserExact = 0; // REA_rodada105_phaser
  1188. // import with the installation's samples (realFlpDir is ...\Data): for DirectWave/FPC
  1189. lynx::FlpImportOptions withFiles;
  1190. const auto flRoot = std::filesystem::path (realFlpDir).parent_path().string();
  1191. withFiles.roots = { flRoot, realFlpDir, "" };
  1192. withFiles.fileExists = [] (const std::string& path) {
  1193. auto local = path;
  1194. std::replace (local.begin(), local.end(), '\\', '/');
  1195. return std::filesystem::exists (local);
  1196. };
  1197. withFiles.readFile = [] (const std::string& path) {
  1198. auto local = path;
  1199. std::replace (local.begin(), local.end(), '\\', '/');
  1200. std::ifstream in (local, std::ios::binary);
  1201. return std::vector<std::uint8_t> ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  1202. };
  1203. int embeddedSaved = 0;
  1204. withFiles.saveEmbedded = [&embeddedSaved] (const std::vector<std::uint8_t>& wav, const std::string&) {
  1205. ++embeddedSaved;
  1206. return wav.size() > 44 ? std::string ("embutido.wav") : std::string();
  1207. };
  1208. int slicedChannels = 0, flKeysChannels = 0;
  1209. for (auto& entry : std::filesystem::recursive_directory_iterator (realFlpDir))
  1210. {
  1211. if (entry.path().extension() != ".flp")
  1212. continue;
  1213. std::ifstream in (entry.path(), std::ios::binary);
  1214. std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  1215. ++files;
  1216. try
  1217. {
  1218. const auto p = flp::parseProject (bytes);
  1219. CHECK (! p.version.empty());
  1220. CHECK (p.tempo > 10.0 && p.tempo < 1000.0);
  1221. const auto song = lynx::importFlp (p);
  1222. CHECK (! song.patterns.empty());
  1223. {
  1224. const auto full = lynx::importFlp (p, withFiles);
  1225. bool hasFpc = false;
  1226. for (const auto& c : p.channels)
  1227. hasFpc = hasFpc || c.pluginName == "FPC";
  1228. // GMS and Morphine (REA_rodada39): every state plays on the exact engine (GMS with its waves from the
  1229. // installation) and sounds; two demo channels match golden samples of our round-39 models
  1230. for (const auto& c : p.channels)
  1231. {
  1232. if (c.pluginName == "GMS")
  1233. ++gms, gmsUsable += lynx::gmsStateUsable (c.pluginState) ? 1 : 0;
  1234. if (c.pluginName == "Morphine")
  1235. ++morphine, morphineUsable += lynx::morphineStateUsable (c.pluginState) ? 1 : 0;
  1236. }
  1237. // Kepler: every state in the demos plays on the exact engine (REA_rodada46) when the engine DLL is installed
  1238. for (const auto& c : p.channels)
  1239. if (c.pluginName == "Kepler")
  1240. {
  1241. ++kepler;
  1242. keplerUsable += lynx::keplerStateUsable (c.pluginState) ? 1 : 0;
  1243. }
  1244. for (const auto& c : full.channels)
  1245. if (c.instrument == lynx::InstrumentKind::kepler)
  1246. {
  1247. ++keplerKind;
  1248. 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 } },
  1249. 44100.0, 11025);
  1250. double e = 0.0;
  1251. bool finite = ! y.empty();
  1252. for (auto v : y)
  1253. finite = finite && std::isfinite (v), e += (double) v * v;
  1254. CHECK (finite);
  1255. keplerSounding += e > 1e-6 ? 1 : 0;
  1256. }
  1257. const auto file = entry.path().filename().string();
  1258. for (const auto& c : full.channels)
  1259. {
  1260. const auto isGms = c.instrument == lynx::InstrumentKind::gms, isMorphine = c.instrument == lynx::InstrumentKind::morphine;
  1261. if (! isGms && ! isMorphine)
  1262. continue;
  1263. (isGms ? gmsKind : morphineKind)++;
  1264. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  1265. const auto golden = (isGms && c.name == "GMS" && file.find ("Song For You") != std::string::npos)
  1266. || (isMorphine && c.name == "Morphine" && file.find ("SHINE") != std::string::npos);
  1267. std::vector<float> y;
  1268. if (isGms)
  1269. y = lynx::gms::renderReference (c.pluginState,
  1270. { { 0, 60, (float) iv (100), 1.0, 0.0, 0.0, 30000 }, { 11025, 64, (float) iv (80), 1.0, 0.0, 0.0, 40000 },
  1271. { 22050, 67, (float) iv (110), 1.0, 0.0, 0.0, 52000 }, { 60000, 48, (float) iv (127), 1.0, 0.0, 0.0, 70000 } },
  1272. 44100.0, golden ? 88200 : 22050, c.samplePath);
  1273. else
  1274. 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 } },
  1275. 44100.0, golden ? 100000 : 22050, c.samplePath);
  1276. double e = 0.0;
  1277. bool finite = ! y.empty();
  1278. for (auto v : y)
  1279. finite = finite && std::isfinite (v), e += (double) v * v;
  1280. CHECK (finite);
  1281. (isGms ? gmsSounding : morphineSounding) += e > 1e-6 ? 1 : 0;
  1282. if (golden && ! y.empty())
  1283. {
  1284. // our model, same events (GMS: tempo 140; Morphine: tempo 120), state as saved, 44.1 kHz
  1285. const auto rms = std::sqrt (e / (double) y.size());
  1286. CHECK (std::abs (rms - (isGms ? 0.213602668574 : 0.0673606448152)) < 1e-5);
  1287. 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 } },
  1288. 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 } };
  1289. for (const auto& g : isGms ? std::vector (std::begin (gmsAt), std::end (gmsAt)) : std::vector (std::begin (morAt), std::end (morAt)))
  1290. 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);
  1291. ++demoGoldens;
  1292. }
  1293. }
  1294. // MiniSynth (REA_rodada41): every state plays on the exact engine (tables from the installation's
  1295. // oscdata.dat) and sounds; two demo channels match golden samples of our round-41 model (bit-identical to
  1296. // renders of the installation's DLL for these states)
  1297. for (const auto& c : p.channels)
  1298. if (c.pluginName == "MiniSynth")
  1299. ++miniSynth, miniSynthUsable += lynx::miniSynthStateUsable (c.pluginState) ? 1 : 0;
  1300. for (const auto& c : full.channels)
  1301. {
  1302. if (c.instrument != lynx::InstrumentKind::minisynth)
  1303. continue;
  1304. ++miniSynthKind;
  1305. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  1306. const auto exo = file.find ("Exoplanet") != std::string::npos;
  1307. const auto golden = exo && (c.name == "Main Lead 1" || c.name == "Riser");
  1308. const auto y = lynx::minisynth::renderReference (c.pluginState,
  1309. { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 }, { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  1310. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 }, { 60000, 48, iv (127), 1.0, 0.0, 0.0, 70000 } },
  1311. 44100.0, golden ? 88200 : 22050, c.samplePath);
  1312. double e = 0.0;
  1313. bool finite = ! y.empty();
  1314. for (auto v : y)
  1315. finite = finite && std::isfinite (v), e += (double) v * v;
  1316. CHECK (finite);
  1317. miniSynthSounding += e > 1e-6 ? 1 : 0;
  1318. if (golden && ! y.empty())
  1319. {
  1320. // our model, same events, tempo 120, state as saved, 44.1 kHz, host blocks of 512, L = R = Final.Vol
  1321. const auto lead = c.name == "Main Lead 1";
  1322. CHECK (std::abs (std::sqrt (e / (double) y.size()) - (lead ? 0.177495801753 : 0.19006567413)) < 1e-9);
  1323. 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 } },
  1324. riserAt[] = { { 2000, -0.0603227727f }, { 15000, 0.112200871f }, { 30000, 0.0453962795f }, { 50000, 0.234380022f }, { 65000, -0.164545432f }, { 80000, -0.000485120923f } };
  1325. for (const auto& g : lead ? std::vector (std::begin (leadAt), std::end (leadAt)) : std::vector (std::begin (riserAt), std::end (riserAt)))
  1326. CHECK (y[2 * (std::size_t) g.i] == g.v && y[2 * (std::size_t) g.i + 1] == g.v);
  1327. ++miniSynthGoldens;
  1328. }
  1329. }
  1330. // PoiZone (REA_rodada44): every state plays on the exact engine and sounds; the demo channel matches golden
  1331. // samples of our round-44 model
  1332. for (const auto& c : p.channels)
  1333. if (c.pluginName == "PoiZone")
  1334. ++poizone, poizoneUsable += lynx::poizoneStateUsable (c.pluginState) ? 1 : 0;
  1335. for (const auto& c : full.channels)
  1336. {
  1337. if (c.instrument != lynx::InstrumentKind::poizone)
  1338. continue;
  1339. ++poizoneKind;
  1340. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  1341. const auto golden = c.name == "PoiZone" && file.find ("NIGHT TERROR") != std::string::npos;
  1342. const auto y = lynx::poizone::renderReference (c.pluginState,
  1343. { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 }, { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  1344. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 }, { 60000, 48, iv (127), 1.0, 0.0, 0.0, 70000 } },
  1345. 44100.0, golden ? 88200 : 22050);
  1346. double e = 0.0;
  1347. bool finite = ! y.empty();
  1348. for (auto v : y)
  1349. finite = finite && std::isfinite (v), e += (double) v * v;
  1350. CHECK (finite);
  1351. poizoneSounding += e > 1e-6 ? 1 : 0;
  1352. if (golden && ! y.empty())
  1353. {
  1354. // our model, same events, tempo 120, state as saved, 44.1 kHz, L = R = Final.Vol
  1355. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.018975758213507415) < 1e-7);
  1356. 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 } };
  1357. for (const auto& g : at)
  1358. 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);
  1359. ++poizoneGoldens;
  1360. }
  1361. }
  1362. // Toxic Biohazard (REA_rodada45): every demo state plays on the exact engine with the waves read from the
  1363. // installation's DLL and sounds; the "Bombs Away" channel matches golden samples of our round-45 model
  1364. for (const auto& c : p.channels)
  1365. if (c.pluginName == "Toxic Biohazard")
  1366. ++toxic, toxicUsable += lynx::toxicStateUsable (c.pluginState) ? 1 : 0;
  1367. for (const auto& c : full.channels)
  1368. {
  1369. if (c.instrument != lynx::InstrumentKind::toxic)
  1370. continue;
  1371. ++toxicKind;
  1372. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  1373. const auto golden = file.find ("Bombs Away") != std::string::npos;
  1374. const auto y = lynx::toxic::renderReference (
  1375. c.pluginState,
  1376. { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 }, { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  1377. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 }, { 60000, 48, iv (127), 1.0, 0.0, 0.0, 70000 } },
  1378. 44100.0, golden ? 88200 : 22050, c.samplePath, 512, 140.0);
  1379. double e = 0.0;
  1380. bool finite = ! y.empty();
  1381. for (auto v : y)
  1382. finite = finite && std::isfinite (v), e += (double) v * v;
  1383. CHECK (finite);
  1384. toxicSounding += e > 1e-6 ? 1 : 0;
  1385. if (golden && ! y.empty())
  1386. {
  1387. // our model (r45/py/toxmodel.py, which equals the DLL bit for bit), state as saved, tempo 140, 44.1 kHz
  1388. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.0832500924945) < 1e-6);
  1389. 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 } };
  1390. for (const auto& g : at)
  1391. 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);
  1392. ++toxicGoldens;
  1393. }
  1394. }
  1395. for (const auto& c : full.channels)
  1396. {
  1397. if (c.instrument == lynx::InstrumentKind::harmless && c.modelParams.size() >= (std::size_t) lynx::harmlessParamCount
  1398. && c.modelParams[(std::size_t) lynx::harmlessComboIndex + 3] > 0.5f)
  1399. {
  1400. ++harmlessCompWanted;
  1401. harmlessCompLoaded += c.modelParams.size() > (std::size_t) lynx::harmlessCompIndex + 1000 ? 1 : 0;
  1402. }
  1403. flKeysChannels += c.zones.size() == 30 && c.zones.front().path.find ("CHANGPIANO") != std::string::npos ? 1 : 0;
  1404. // a Slicex channel is its own instrument since round 190l (no sampler slices): its regions are the
  1405. // region records of its decks, counted here so the bar below still means "sliced channels import"
  1406. std::size_t slicexRegions = 0;
  1407. if (c.instrument == lynx::InstrumentKind::slicex)
  1408. if (const auto state = lynx::slicex::parse (c.pluginState))
  1409. for (const auto& deck : state->decks)
  1410. slicexRegions += deck.empty() ? 0 : deck.regions.size();
  1411. if (c.sampler.slices.size() >= 4 || slicexRegions >= 4)
  1412. ++slicedChannels;
  1413. if (c.sampler.slices.size() >= 4)
  1414. {
  1415. CHECK (std::is_sorted (c.sampler.slices.begin(), c.sampler.slices.end()));
  1416. CHECK (c.sampler.slices.back() < 1.0f);
  1417. }
  1418. dwExact += c.instrument == lynx::InstrumentKind::directwave ? 1 : 0;
  1419. if (! c.zones.empty() && c.name.find (" - ") == std::string::npos && c.instrument != lynx::InstrumentKind::fpc)
  1420. {
  1421. ++dwChannels;
  1422. dwZones += (int) c.zones.size();
  1423. }
  1424. padChannels += c.name.find (" - ") != std::string::npos && c.instrument == lynx::InstrumentKind::sampler ? 1 : 0;
  1425. if (c.instrument == lynx::InstrumentKind::fpc) // (round 190q: a kit on one channel; its pads that play)
  1426. padChannels += (int) lynx::fpcChannelPads (c).size();
  1427. }
  1428. fpcKits += hasFpc;
  1429. // Maximus / Soundgoodizer become the exact engine (REA_rodada40); Vocodex carries SG.dat (and its
  1430. // carrier's Sytrus preset) read from the installation
  1431. const int basicSlots0 = basicSlots, basicExact0 = basicExact;
  1432. for (const auto& insert : p.mixer)
  1433. for (const auto& slot : insert.slots)
  1434. {
  1435. maximusSlots += slot.plugin == "Maximus";
  1436. delay2Slots += slot.plugin == "Fruity Delay 2";
  1437. delay3Slots += slot.plugin == "Fruity Delay 3";
  1438. sgzSlots += slot.plugin == "Soundgoodizer";
  1439. vocodexSlots += slot.plugin == "Vocodex";
  1440. shaperSlots += slot.plugin == "Fruity WaveShaper";
  1441. flPeqSlots += slot.plugin == "Fruity Parametric EQ 2" || slot.plugin == "Fruity Parametric EQ";
  1442. equoSlots += slot.plugin == "EQUO";
  1443. distructorSlots += slot.plugin == "Distructor";
  1444. pitchShifterSlots += slot.plugin == "Pitch Shifter";
  1445. clipperSlots += slot.plugin == "Fruity Soft Clipper";
  1446. basicSlots += slot.plugin == "Fruity PanOMatic" || slot.plugin == "Fruity Balance" || slot.plugin == "Fruity Blood Overdrive";
  1447. filterSlots += slot.plugin == "Fruity Fast LP" || slot.plugin == "Fruity Free Filter" || slot.plugin == "Fruity Filter";
  1448. chorusSlots += lynx::chorusControls (slot.plugin) > 0 && slot.state.size() >= 4 ? 1 : 0;
  1449. fastDistSlots += slot.plugin == "Fruity Fast Dist";
  1450. flangusSlots += slot.plugin == "Fruity Flangus";
  1451. flangerSlots += slot.plugin == "Fruity Flanger";
  1452. phaserSlots += slot.plugin == "Fruity Phaser";
  1453. }
  1454. // (the slots and the nodes of the Patcher graphs, as when Patchers were flattened into slots)
  1455. std::vector<const lynx::EffectSlot*> allSlots;
  1456. std::function<void (const lynx::EffectSlot&)> collect = [&] (const lynx::EffectSlot& fx) {
  1457. allSlots.push_back (&fx);
  1458. for (const auto& n : fx.patcher.nodes)
  1459. if (! n.generator)
  1460. collect (n.effect);
  1461. };
  1462. for (const auto& t : full.mixer)
  1463. for (const auto& fx : t.effects)
  1464. collect (fx);
  1465. for (const auto* slotPointer : allSlots)
  1466. {
  1467. const auto& fx = *slotPointer;
  1468. if (fx.kind == lynx::EffectKind::maximus)
  1469. {
  1470. const auto blob = lynx::maximusfx::unpack (fx.state);
  1471. CHECK (blob && lynx::maximusfx::presetOf (*blob) != nullptr);
  1472. if (blob)
  1473. (blob->build == lynx::hlfx::MaximusBuild::soundgoodizer ? sgzExact : maximusExact) += 1;
  1474. }
  1475. fastDistExact += fx.kind == lynx::EffectKind::fruityFastDist;
  1476. flangusExact += fx.kind == lynx::EffectKind::fruityFlangus;
  1477. flangerExact += fx.kind == lynx::EffectKind::fruityFlanger;
  1478. phaserExact += fx.kind == lynx::EffectKind::fruityPhaser;
  1479. if (fx.kind == lynx::EffectKind::vocodex)
  1480. vocodexWithData += ! lynx::vocodex::splitInstallData (fx.state).second.sgDat.empty();
  1481. if (fx.kind == lynx::EffectKind::fruityWaveShaper)
  1482. shaperExact += lynx::flshape::parseWaveShaper (fx.state).has_value() ? 1 : 0;
  1483. clipperExact += fx.kind == lynx::EffectKind::fruitySoftClipper;
  1484. flPeqExact += fx.kind == lynx::EffectKind::parametricEq && lynx::peq::flLaw (fx.params);
  1485. basicExact += fx.kind == lynx::EffectKind::fruityPanOMatic || fx.kind == lynx::EffectKind::fruityBalance
  1486. || fx.kind == lynx::EffectKind::fruityBloodOverdrive;
  1487. equoExact += fx.kind == lynx::EffectKind::equo && lynx::equofx::parse (fx.state).has_value();
  1488. if (fx.kind == lynx::EffectKind::distructor)
  1489. if (const auto d = lynx::distructor::parseBlob (fx.state))
  1490. {
  1491. ++distructorExact;
  1492. for (const auto& m : d->modules)
  1493. distructorImpulses += m.type == 3 && ! m.impulseFile.empty();
  1494. }
  1495. pitchShifterExact += fx.kind == lynx::EffectKind::flPitchShifter;
  1496. filterExact += fx.kind == lynx::EffectKind::fruityFastLP || fx.kind == lynx::EffectKind::fruityFreeFilter
  1497. || fx.kind == lynx::EffectKind::fruityFilter;
  1498. delay2Exact += fx.kind == lynx::EffectKind::fruityDelay2;
  1499. delay3Exact += fx.kind == lynx::EffectKind::fruityDelay3;
  1500. chorusExact += fx.kind == lynx::EffectKind::fruityChorus || fx.kind == lynx::EffectKind::hyperChorus || fx.kind == lynx::EffectKind::vintageChorus ? 1 : 0;
  1501. }
  1502. if (basicSlots - basicSlots0 != basicExact - basicExact0)
  1503. std::printf (" %s: %d PanOMatic/Balance/Blood Overdrive slots, %d exact effects\n", file.c_str(),
  1504. basicSlots - basicSlots0, basicExact - basicExact0);
  1505. for (const auto& pat : full.patterns)
  1506. for (const auto& n : pat.notes)
  1507. {
  1508. CHECK (n.channel >= 0 && n.channel < (int) full.channels.size());
  1509. padNotes += full.channels[(std::size_t) n.channel].name.find (" - ") != std::string::npos
  1510. || full.channels[(std::size_t) n.channel].instrument == lynx::InstrumentKind::fpc ? 1 : 0;
  1511. }
  1512. }
  1513. const auto peqDefaults = lynx::makeEffectSlot (lynx::EffectKind::parametricEq).params;
  1514. for (const auto& t : song.mixer)
  1515. for (const auto& fx : t.effects)
  1516. {
  1517. if (fx.kind == lynx::EffectKind::parametricEq)
  1518. {
  1519. ++peqSlots;
  1520. peqShaped += fx.params != peqDefaults;
  1521. for (double hz : { 50.0, 1000.0, 10000.0 })
  1522. CHECK (std::isfinite (lynx::peq::responseDb (fx.params, 48000.0, hz)));
  1523. }
  1524. if (fx.kind == lynx::EffectKind::reverb)
  1525. {
  1526. ++reverbs;
  1527. reverbsDryless += fx.params[4] < 0.01f;
  1528. reverbsLowCut += fx.params[5] > 0.0f;
  1529. }
  1530. if (fx.kind == lynx::EffectKind::reeverb2) // FL's exact Reeverb 2 (REA_rodada49): FL's own controls
  1531. {
  1532. const auto r2 = lynx::reeverb2::fromParams (fx.params);
  1533. ++reverbs;
  1534. reverbsDryless += r2.p[10] == 0;
  1535. reverbsLowCut += r2.p[0] >= 20;
  1536. }
  1537. }
  1538. for (const auto& t : song.mixer)
  1539. if (t.eq.active())
  1540. {
  1541. ++trackEqs;
  1542. for (std::size_t b = 0; b < 3; ++b)
  1543. CHECK (t.eq.hz[b] >= 10.0f && t.eq.hz[b] <= 16000.0f && std::abs (t.eq.gainDb[b]) <= 18.0f);
  1544. }
  1545. for (const auto& a : song.arrangements)
  1546. for (const auto& m : a.markers)
  1547. {
  1548. ++markers;
  1549. signatures += m.isSignature();
  1550. CHECK (m.tick >= 0);
  1551. }
  1552. // (round 168g: the patterns' own markers are no longer counted as the Playlist's)
  1553. for (const auto& p : song.patterns)
  1554. for (const auto& m : p.markers)
  1555. {
  1556. ++markers;
  1557. signatures += m.isSignature();
  1558. CHECK (m.tick >= 0);
  1559. }
  1560. // Fruity Wrapper (channels and mixer slots): every blob in the demos is read
  1561. auto countWrapper = [&] (const std::string& plugin, const std::vector<std::uint8_t>& state) {
  1562. if (plugin != "Fruity Wrapper")
  1563. return;
  1564. ++wrapped;
  1565. lynx::ForeignPlugin f;
  1566. wrappedParsed += lynx::parseFlWrapper (state, f) && f.format != lynx::ForeignPlugin::Format::none && ! f.name.empty();
  1567. if (f.format == lynx::ForeignPlugin::Format::vst2)
  1568. {
  1569. // VST2 state (REA_rodada89 §2): every one decodes, and writes back to the same bytes — except the
  1570. // wrapped Fruity effects of the FL 11 render tests (their mixer slots are read since round 80):
  1571. // those decode but do not write back byte-identical (not confirmed why)
  1572. ++wrappedVst2;
  1573. lynx::FlVst2State st;
  1574. if (lynx::parseFlVst2State (f.state, st) && st.version == -9)
  1575. {
  1576. ++wrappedVst2State;
  1577. wrappedVst2Old += lynx::writeFlVst2State (st) != f.state;
  1578. }
  1579. }
  1580. };
  1581. for (const auto& c : p.channels)
  1582. countWrapper (c.pluginName, c.pluginState);
  1583. for (const auto& insert : p.mixer)
  1584. for (const auto& slot : insert.slots)
  1585. countWrapper (slot.plugin, slot.state);
  1586. // Harmless: every state in the demos becomes a synth, with finite values
  1587. for (const auto& c : p.channels)
  1588. if (c.pluginName == "Harmless")
  1589. {
  1590. ++harmless;
  1591. lynx::SynthSettings hs;
  1592. float g = 1.0f;
  1593. if (lynx::synthFromHarmless (c.pluginState, 128.0, hs, g))
  1594. {
  1595. ++harmlessDecoded;
  1596. CHECK (std::isfinite (g) && hs.cutoff >= 20.0f && hs.cutoff <= 20000.0f && std::isfinite (hs.decay) && std::isfinite (hs.release));
  1597. }
  1598. if (const auto hp = lynx::harmlessParams (c.pluginState))
  1599. harmlessParsed += (int) hp->size() == lynx::harmlessParamCount ? 1 : 0;
  1600. }
  1601. for (const auto& c : song.channels)
  1602. harmlessKind += c.instrument == lynx::InstrumentKind::harmless ? 1 : 0;
  1603. // Harmor: every state in the demos plays on the exact engine (REA_rodada34) and sounds, with finite samples
  1604. for (const auto& c : p.channels)
  1605. if (c.pluginName == "Harmor")
  1606. {
  1607. ++harmor;
  1608. harmorUsable += lynx::harmorStateUsable (c.pluginState) ? 1 : 0;
  1609. }
  1610. for (const auto& c : song.channels)
  1611. if (c.instrument == lynx::InstrumentKind::harmor)
  1612. {
  1613. ++harmorKind;
  1614. 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);
  1615. double e = 0.0;
  1616. bool finite = ! y.empty();
  1617. for (auto v : y)
  1618. finite = finite && std::isfinite (v), e += (double) v * v;
  1619. CHECK (finite);
  1620. harmorSounding += e > 1e-6 ? 1 : 0;
  1621. }
  1622. // Ogun: every state in the demos plays on the exact engine (REA_rodada37) and sounds, with finite samples
  1623. for (const auto& c : p.channels)
  1624. if (c.pluginName == "Ogun")
  1625. {
  1626. ++ogun;
  1627. ogunUsable += lynx::ogunStateUsable (c.pluginState) ? 1 : 0;
  1628. }
  1629. for (const auto& c : song.channels)
  1630. if (c.instrument == lynx::InstrumentKind::ogun)
  1631. {
  1632. ++ogunKind;
  1633. 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 },
  1634. { 2000, 67, 0.6, 1.0, 0.0, 0.0, 0.0, 0.0, 0.5, 8000 } }, 44100.0, 11025);
  1635. double e = 0.0;
  1636. bool finite = ! y.empty();
  1637. for (auto v : y)
  1638. finite = finite && std::isfinite (v), e += (double) v * v;
  1639. CHECK (finite);
  1640. ogunSounding += e > 1e-6 ? 1 : 0;
  1641. }
  1642. // Autogun (REA_rodada57): every state plays on Ogun's engine in its Autogun variant; with the installation, the
  1643. // Magic Dust stage gets Autogun\Data\SG.dat
  1644. {
  1645. bool any = false;
  1646. for (const auto& c : p.channels)
  1647. if (c.pluginName == "Autogun")
  1648. ++autogun, autogunUsable += lynx::autogunStateUsable (c.pluginState) ? 1 : 0, any = true;
  1649. if (any)
  1650. for (const auto& c : lynx::importFlp (p, withFiles).channels)
  1651. if (c.instrument == lynx::InstrumentKind::autogun)
  1652. {
  1653. ++autogunKind;
  1654. lynx::ogun::RefOptions opt;
  1655. opt.autogun = true;
  1656. if (! c.samplePath.empty())
  1657. {
  1658. auto local = c.samplePath;
  1659. std::replace (local.begin(), local.end(), '\\', '/');
  1660. std::ifstream sg (local, std::ios::binary);
  1661. opt.sgState.assign ((std::istreambuf_iterator<char> (sg)), std::istreambuf_iterator<char>());
  1662. autogunWithSg += opt.sgState.size() == 1342 ? 1 : 0;
  1663. }
  1664. 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 } },
  1665. 44100.0, 11025, opt);
  1666. double e = 0.0;
  1667. bool finite = ! y.empty();
  1668. for (auto v : y)
  1669. finite = finite && std::isfinite (v), e += (double) v * v;
  1670. CHECK (finite);
  1671. autogunSounding += e > 1e-6 ? 1 : 0;
  1672. }
  1673. }
  1674. // Fruity DX10: every state plays on the exact engine when its channel processor is neutral (REA_rodada42)
  1675. for (const auto& c : p.channels)
  1676. if (c.pluginName == "Fruity DX10" || c.pluginName == "DX10")
  1677. ++dx10, dx10Usable += lynx::dx10StateUsable (c.pluginState) ? 1 : 0;
  1678. for (const auto& c : song.channels)
  1679. if (c.instrument == lynx::InstrumentKind::dx10)
  1680. {
  1681. ++dx10Kind;
  1682. 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 } },
  1683. 44100.0, 11025);
  1684. double e = 0.0;
  1685. bool finite = ! y.empty();
  1686. for (auto v : y)
  1687. finite = finite && std::isfinite (v), e += (double) v * v;
  1688. CHECK (finite);
  1689. dx10Sounding += e > 1e-6 ? 1 : 0;
  1690. }
  1691. // 3x Osc: every channel plays on the exact engine (REA_rodada47)
  1692. for (const auto& c : song.channels)
  1693. if (c.instrument == lynx::InstrumentKind::threeosc)
  1694. {
  1695. ++threeOscKind;
  1696. 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 } },
  1697. 44100.0, 11025);
  1698. double e = 0.0;
  1699. bool finite = ! y.empty();
  1700. for (auto v : y)
  1701. finite = finite && std::isfinite (v), e += (double) v * v;
  1702. CHECK (finite);
  1703. threeOscSounding += e > 1e-6 ? 1 : 0;
  1704. }
  1705. // Sytrus: every state in the demos (zlib + 10 units) decodes, with finite values
  1706. for (const auto& c : p.channels)
  1707. if (c.pluginName == "Sytrus")
  1708. {
  1709. ++sytrus;
  1710. lynx::SynthSettings ss;
  1711. float g = 1.0f;
  1712. // the Sytrus engine (REA_rodada31) reads every demo state; the first 24 play a short note (render settings)
  1713. if (lynx::sytrus::stateUsable (c.pluginState))
  1714. {
  1715. ++sytrusEngine;
  1716. if (sytrusRendered < 24)
  1717. {
  1718. ++sytrusRendered;
  1719. lynx::sytrus::Engine e;
  1720. e.prepare (44100.0, true, 512);
  1721. e.setPatch (lynx::sytrus::parsePatch (c.pluginState));
  1722. lynx::sytrus::NoteParams note;
  1723. e.noteOn (note);
  1724. std::vector<float> l (8192), r (8192);
  1725. e.render (l.data(), r.data(), 8192, 0.551f, 0.0f);
  1726. bool finite = true;
  1727. double energy = 0.0;
  1728. for (std::size_t k = 0; k < l.size(); ++k)
  1729. finite = finite && std::isfinite (l[k]) && std::isfinite (r[k]), energy += (double) l[k] * l[k];
  1730. CHECK (finite);
  1731. sytrusSounding += energy > 1e-6;
  1732. }
  1733. }
  1734. if (lynx::synthFromSytrus (c.pluginState, 128.0, ss, g))
  1735. {
  1736. ++sytrusDecoded;
  1737. CHECK (std::isfinite (g) && ss.cutoff >= 20.0f && ss.cutoff <= 20000.0f && std::isfinite (ss.attack) && std::isfinite (ss.decay)
  1738. && std::isfinite (ss.release) && ss.sustain >= 0.0f && ss.sustain <= 1.0f && std::isfinite (ss.filterEnv.amount));
  1739. for (const auto& o : ss.osc)
  1740. CHECK (o.level >= 0.0f && o.level <= 1.0f && o.octave >= -3 && o.octave <= 3);
  1741. }
  1742. }
  1743. // round 12 generators
  1744. for (const auto& c : p.channels)
  1745. {
  1746. lynx::SynthSettings gs;
  1747. float g = 1.0f;
  1748. int decoded = -1;
  1749. if (c.pluginName == "Harmor")
  1750. {
  1751. decoded = lynx::synthFromHarmor (c.pluginState, gs, g);
  1752. if (decoded)
  1753. {
  1754. // REA_rodada25 §4: most resonant settings stay below Q ≈ 3, a few saturate
  1755. CHECK (gs.resonance >= 0.0f && gs.resonance <= 1.0f);
  1756. static int resonant = 0, strong = 0, total = 0;
  1757. resonant += gs.resonance > 0.0f;
  1758. strong += gs.resonance > 0.9f;
  1759. if (++total % 50 == 0)
  1760. std::printf (" Harmor resonance: %d with a peak, %d with Q > ~5 (of %d)\n", resonant, strong, total);
  1761. }
  1762. static int saw = 0, square = 0, other = 0, total = 0;
  1763. if (decoded && std::any_of (gs.osc.begin(), gs.osc.end(), [] (const lynx::SynthOscillator& o) { return o.wave == lynx::Waveform::spectral; }))
  1764. {
  1765. auto isSaw = true, isSquare = true;
  1766. for (int n = 1; n <= 16; ++n)
  1767. {
  1768. isSaw = isSaw && std::abs (gs.spectrum[(std::size_t) n - 1] - 1.0f / n) < 1e-3f;
  1769. isSquare = isSquare && std::abs (gs.spectrum[(std::size_t) n - 1] - (n % 2 == 1 ? 1.1535f / n : 0.0f)) < 2e-3f;
  1770. }
  1771. ++(isSaw ? saw : isSquare ? square : other);
  1772. if (++total % 20 == 0 || total == 100)
  1773. std::printf (" Harmor spectrum: %d saw, %d square, %d other (of %d)\n", saw, square, other, total);
  1774. }
  1775. }
  1776. else if (c.pluginName == "GMS")
  1777. decoded = lynx::synthFromGms (c.pluginState, 128.0, gs, g);
  1778. else if (c.pluginName == "Ogun" || c.pluginName == "Autogun")
  1779. {
  1780. decoded = lynx::synthFromOgun (c.pluginState, gs, g);
  1781. // REA_rodada24 §1.9: the "Trumpet" preset (richness 7, no seeds) has partials 0.9087, 0.8610,
  1782. // 0.8151… at the fixed seed-3 phases 0.919, 0.380, 0.130
  1783. if (decoded && gs.osc[0].wave == lynx::Waveform::spectral)
  1784. {
  1785. CHECK (std::all_of (gs.spectrum.begin(), gs.spectrum.end(), [] (float a) { return std::isfinite (a) && a >= 0.0f; }));
  1786. static constexpr float trumpet[3] { 0.9087f, 0.8610f, 0.8151f }, phases[3] { 0.919f, 0.380f, 0.130f };
  1787. auto match = true;
  1788. for (std::size_t k = 0; k < 3; ++k)
  1789. match = match && std::abs (gs.spectrum[k] * 2.0f / 3.14159265f - trumpet[k]) < 1e-3f && std::abs (gs.spectrumPhase[k] - phases[k]) < 1e-3f;
  1790. ogunTrumpet += match ? 1 : 0;
  1791. }
  1792. }
  1793. else if (c.pluginName == "Sakura")
  1794. decoded = lynx::synthFromSakura (c.pluginState, gs);
  1795. else if (c.pluginName == "Kepler")
  1796. decoded = lynx::synthFromKepler (c.pluginState, gs, g);
  1797. else if (c.pluginName == "Wave Traveller")
  1798. decoded = lynx::waveTravellerStateUsable (c.pluginState) ? 2 : 0;
  1799. else if (c.pluginName == "PoiZone")
  1800. decoded = lynx::synthFromPoiZone (c.pluginState, gs, g);
  1801. else if (c.pluginName == "BassDrum")
  1802. decoded = lynx::synthFromBassDrum (c.pluginState, gs, g);
  1803. else if (c.pluginName == "Toxic Biohazard")
  1804. decoded = lynx::synthFromToxic (c.pluginState, gs, g);
  1805. else if (c.pluginName == "Morphine")
  1806. decoded = lynx::synthFromMorphine (c.pluginState, gs, g);
  1807. else if (c.pluginName == "BooBass")
  1808. decoded = lynx::synthFromBooBass (c.pluginState, gs);
  1809. else if (c.pluginName == "Drumpad" || c.pluginName == "DrumPad")
  1810. decoded = lynx::drumpadPatchName (c.pluginState).has_value() ? 2 : 0;
  1811. else if (c.pluginName == "Fruity Envelope Controller" && c.pluginState.size() >= 4 && c.pluginState[2] >= 6) // v >= 6
  1812. decoded = lynx::parseEnvelopeController (c.pluginState).has_value() ? 2 : 0;
  1813. else if (c.pluginName == "FLEX")
  1814. decoded = lynx::flexPresetName (c.pluginState).has_value() ? 2 : 0;
  1815. else if (c.pluginName == "Fruity DrumSynth Live")
  1816. decoded = lynx::drumSynthLiveParams (c.pluginState).has_value() ? 2 : 0;
  1817. if (decoded < 0)
  1818. continue;
  1819. auto& counter = generators[c.pluginName];
  1820. ++counter.first;
  1821. counter.second += decoded != 0;
  1822. if (decoded == 1 && c.pluginName != "Fruity DrumSynth Live")
  1823. {
  1824. CHECK (std::isfinite (g) && g >= 0.0f && gs.cutoff >= 20.0f && gs.cutoff <= 20000.0f && gs.sustain >= 0.0f && gs.sustain <= 1.0f);
  1825. 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));
  1826. for (const auto& o : gs.osc)
  1827. CHECK (o.level >= 0.0f && o.level <= 4.0f && std::isfinite (o.detune) && std::isfinite (o.pan));
  1828. }
  1829. }
  1830. // 3x Osc: every state in the demos decodes, with levels summing to at most 1
  1831. for (const auto& c : p.channels)
  1832. if (c.pluginName == "3x Osc")
  1833. {
  1834. ++threeOsc;
  1835. lynx::SynthSettings s;
  1836. if (lynx::synthFrom3xOsc (c.pluginState, s))
  1837. {
  1838. ++threeOscDecoded;
  1839. // with AM, osc 3 is the modulation depth and is left out of the sum
  1840. const auto sum = s.osc[0].level + s.osc[1].level + (s.am ? 0.0f : s.osc[2].level);
  1841. CHECK (sum <= 1.001f && s.osc[2].level <= 1.0f);
  1842. for (const auto& o : s.osc)
  1843. CHECK (o.octave >= -2 && o.octave <= 2 && std::abs (o.semitones) <= 11 && std::abs (o.pan) <= 1.0f);
  1844. }
  1845. }
  1846. for (auto& pat : p.patterns)
  1847. if (! pat.notes.empty())
  1848. {
  1849. ++withNotes;
  1850. break;
  1851. }
  1852. for (auto& a : p.arrangements)
  1853. for (auto& item : a.items)
  1854. CHECK (item.track >= 0 && item.track < 500);
  1855. for (auto& clip : song.playlist)
  1856. {
  1857. CHECK (clip.pattern >= 0 && clip.pattern < (int) song.patterns.size());
  1858. CHECK (clip.length > 0 && clip.offset >= 0);
  1859. if (clip.kind == lynx::ClipKind::automation)
  1860. CHECK (clip.automation >= 0 && clip.automation < (int) song.automations.size());
  1861. }
  1862. for (auto& a : song.automations)
  1863. {
  1864. ++automations;
  1865. automationsWithTarget += a.target.kind != lynx::AutomationTargetKind::none;
  1866. CHECK (a.isLive() || ! a.points.empty());
  1867. for (std::size_t i = 1; i < a.points.size(); ++i)
  1868. CHECK (a.points[i].tick >= a.points[i - 1].tick);
  1869. }
  1870. // imported routing has no cycles and valid destinations
  1871. CHECK (song.mixerOrder().size() + 1 == song.mixer.size());
  1872. for (auto& t : song.mixer)
  1873. for (auto& s : t.sends)
  1874. {
  1875. ++sends;
  1876. CHECK (s.target > 0 && s.target < (int) song.mixer.size());
  1877. }
  1878. if (! song.playlist.empty())
  1879. ++withPlaylist;
  1880. }
  1881. catch (const flp::Error& e)
  1882. {
  1883. ++failures;
  1884. std::printf (" FAILED %s: %s\n", entry.path().string().c_str(), e.what());
  1885. }
  1886. }
  1887. CHECK (threeOscDecoded == threeOsc);
  1888. std::printf (" Harmless: %d channels, %d decoded\n", harmless, harmlessDecoded);
  1889. std::printf (" Sytrus: %d channels, %d decoded, %d by the engine (%d of %d rendered sound)\n", sytrus, sytrusDecoded, sytrusEngine,
  1890. sytrusSounding, sytrusRendered);
  1891. CHECK (sytrusEngine == sytrus);
  1892. CHECK (sytrusSounding >= sytrusRendered * 3 / 4);
  1893. for (const auto& [name, counter] : generators)
  1894. {
  1895. std::printf (" %s: %d channels, %d decoded\n", name.c_str(), counter.first, counter.second);
  1896. CHECK (counter.second == counter.first);
  1897. }
  1898. std::printf (" Ogun spectra matching the Trumpet reference: %d\n", ogunTrumpet);
  1899. CHECK (ogunTrumpet >= 1);
  1900. // demo Gross Beat: the volume slot becomes the gate (those using an envelope slot get steps < 1)
  1901. {
  1902. int vocodexes = 0, vocodexSides = 0;
  1903. int lovePhilterLinks[4] {}; // live links: cutoff, resonance; inside containers: cutoff, resonance
  1904. int embeddedZones = 0, lovePhilters = 0, lovePhiltersRead = 0, lovePhiltersConverted = 0, gates = 0, shaped = 0, effectors = 0, patchers = 0, serial = 0, parallel = 0, generatorPatchers = 0;
  1905. for (const auto& entry : std::filesystem::recursive_directory_iterator (std::filesystem::path (realFlpDir) / "Demo projects"))
  1906. if (entry.path().extension() == ".flp")
  1907. {
  1908. std::ifstream in (entry.path(), std::ios::binary);
  1909. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  1910. try
  1911. {
  1912. const auto project = flp::parseProject (file);
  1913. for (const auto& insert : project.mixer)
  1914. for (const auto& slot : insert.slots)
  1915. if (slot.plugin == "Gross Beat")
  1916. {
  1917. // FL's own engine (REA_rodada53): every demo state is read
  1918. const auto fx = lynx::effectFromFl (slot.plugin, slot.state);
  1919. CHECK (fx && fx->kind == lynx::EffectKind::grossBeat);
  1920. ++gates;
  1921. if (fx && fx->kind == lynx::EffectKind::grossBeat && lynx::grossbeat::slotNames (fx->state, true).size() == 36)
  1922. ++shaped;
  1923. }
  1924. else if (slot.plugin == "Fruity Love Philter")
  1925. {
  1926. ++lovePhilters;
  1927. lovePhiltersRead += lynx::parseLovePhilter (slot.state).has_value();
  1928. if (const auto conv = lynx::lovePhilterEffects (slot.state))
  1929. {
  1930. ++lovePhiltersConverted;
  1931. CHECK (! conv->slots.empty());
  1932. for (const auto& fx : conv->slots)
  1933. for (auto v : fx.params)
  1934. CHECK (std::isfinite (v) && v >= 0.0f && v <= 1.0f);
  1935. for (const auto& fx : conv->slots)
  1936. for (const auto& link : fx.links)
  1937. {
  1938. const auto& chain = fx.branches[(std::size_t) link.branch].chain;
  1939. CHECK (link.source < (int) chain.size() && chain[(std::size_t) link.target].kind == lynx::EffectKind::parametricEq);
  1940. lovePhilterLinks[2 + (link.param == lynx::peq::param (0, 3) ? 1 : 0)]++;
  1941. }
  1942. for (const auto& link : conv->links)
  1943. {
  1944. lovePhilterLinks[link.param == lynx::peq::param (0, 3) ? 1 : 0]++;
  1945. CHECK (link.curve.size() == 33);
  1946. for (auto v : link.curve)
  1947. CHECK (std::isfinite (v) && v >= 0.0f && v <= 1.0f);
  1948. }
  1949. }
  1950. }
  1951. else if (slot.plugin == "Vocodex")
  1952. {
  1953. const auto v = lynx::vocodexEffect (slot.state);
  1954. CHECK (v.has_value());
  1955. vocodexes += v.has_value();
  1956. vocodexSides += v && (v->modIn > 0 || v->carIn > 0);
  1957. }
  1958. else if (slot.plugin == "Effector")
  1959. effectors += lynx::effectFromFl (slot.plugin, slot.state).has_value();
  1960. else if (slot.plugin == "Patcher")
  1961. {
  1962. const auto graph = lynx::parsePatcher (slot.state);
  1963. CHECK (graph.has_value());
  1964. ++patchers;
  1965. if (graph && ! graph->serialChain.empty())
  1966. ++serial;
  1967. if (graph && graph->serialChain.empty() && graph->audio)
  1968. ++parallel;
  1969. }
  1970. for (const auto& c : project.channels)
  1971. if (c.pluginName == "DirectWave")
  1972. if (const auto program = lynx::directWaveProgram (c.pluginState))
  1973. for (const auto& wav : program->embeddedWav)
  1974. if (! wav.empty())
  1975. {
  1976. ++embeddedZones;
  1977. CHECK (wav.size() > 44 && std::memcmp (wav.data(), "RIFF", 4) == 0);
  1978. }
  1979. for (const auto& c : project.channels)
  1980. if (c.pluginName == "Patcher")
  1981. {
  1982. const auto graph = lynx::parsePatcher (c.pluginState);
  1983. CHECK (graph.has_value());
  1984. generatorPatchers += graph && graph->mainGenerator >= 0;
  1985. }
  1986. }
  1987. catch (const flp::Error&) {}
  1988. }
  1989. CHECK (gates >= 30 && shaped == gates);
  1990. std::printf (" Gross Beat: %d slots, %d read exactly; Effector: %d converted; Love Philter: %d, %d read\n", gates, shaped, effectors,
  1991. lovePhilters, lovePhiltersRead);
  1992. CHECK (lovePhiltersRead == lovePhilters && lovePhiltersConverted == lovePhilters);
  1993. std::printf (" Vocodex: %d converted, %d with side inputs\n", vocodexes, vocodexSides);
  1994. CHECK (vocodexes >= 9);
  1995. std::printf (" Love Philter live links: %d cutoff, %d resonance; in multi-unit containers: %d cutoff, %d resonance\n",
  1996. lovePhilterLinks[0], lovePhilterLinks[1], lovePhilterLinks[2], lovePhilterLinks[3]);
  1997. CHECK (lovePhilterLinks[2] + lovePhilterLinks[3] > 0);
  1998. std::printf (" DirectWave: %d zones with embedded audio\n", embeddedZones);
  1999. CHECK (embeddedZones == 12);
  2000. std::printf (" Patcher: %d in the mixer (%d serial, %d reduced to serial/parallel), %d as instrument with generator\n", patchers, serial,
  2001. parallel, generatorPatchers);
  2002. CHECK (gates == 0 || shaped > 0);
  2003. }
  2004. // Envelope Controller "Basic Sidechain" (Future House): drops on the note and returns to ~1 within ~1/3 s
  2005. {
  2006. const auto path = std::filesystem::path (realFlpDir) / "Demo projects" / "Demo songs" / "Asher Postman - Future House.flp";
  2007. if (std::filesystem::exists (path))
  2008. {
  2009. std::ifstream in (path, std::ios::binary);
  2010. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  2011. const auto song = lynx::importFlp (flp::parseProject (file));
  2012. const lynx::ChannelDef* controller = nullptr;
  2013. for (const auto& c : song.channels)
  2014. if (c.instrument == lynx::InstrumentKind::controller)
  2015. controller = &c;
  2016. CHECK (controller != nullptr);
  2017. if (controller != nullptr)
  2018. {
  2019. auto inst = lynx::makeInstrument (lynx::InstrumentKind::controller);
  2020. inst->prepare (48000.0);
  2021. inst->configure (*controller, song.bpm);
  2022. std::vector<float> l (480), r (480);
  2023. inst->render (l.data(), r.data(), 480, 1.0f, 1.0f);
  2024. inst->noteOn (60, 1.0f);
  2025. inst->render (l.data(), r.data(), 480, 1.0f, 1.0f); // 10 ms
  2026. const auto ducked = inst->controlOutput (0);
  2027. for (int i = 0; i < 100; ++i) // +1 s
  2028. inst->render (l.data(), r.data(), 480, 1.0f, 1.0f);
  2029. const auto recovered = inst->controlOutput (0);
  2030. std::printf (" Envelope Controller (Basic Sidechain): %.2f on the note, %.2f after 1 s\n", ducked, recovered);
  2031. CHECK (ducked < 0.3f && recovered > 0.9f);
  2032. }
  2033. }
  2034. }
  2035. // mutation fuzz: real states with flipped and truncated bytes must not crash the readers
  2036. {
  2037. std::map<std::string, std::vector<std::uint8_t>> samples; // one real blob per plugin
  2038. for (const auto& entry : std::filesystem::recursive_directory_iterator (std::filesystem::path (realFlpDir) / "Demo projects"))
  2039. {
  2040. if (entry.path().extension() != ".flp")
  2041. continue;
  2042. std::ifstream in (entry.path(), std::ios::binary);
  2043. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  2044. try
  2045. {
  2046. const auto project = flp::parseProject (file);
  2047. for (const auto& c : project.channels)
  2048. if (! c.pluginState.empty())
  2049. samples.emplace (c.pluginName, c.pluginState);
  2050. for (const auto& insert : project.mixer)
  2051. for (const auto& slot : insert.slots)
  2052. if (! slot.state.empty())
  2053. samples.emplace (slot.plugin, slot.state);
  2054. }
  2055. catch (const flp::Error&) {}
  2056. }
  2057. std::uint32_t seed = 12345;
  2058. auto rnd = [&seed] { seed = seed * 1664525u + 1013904223u; return seed >> 8; };
  2059. int mutations = 0;
  2060. for (const auto& [name, original] : samples)
  2061. for (int round = 0; round < 40; ++round)
  2062. {
  2063. auto blob = original;
  2064. if (round % 4 == 3 && ! blob.empty())
  2065. blob.resize (rnd() % blob.size()); // truncated
  2066. else
  2067. for (int k = 0; k < 1 + (int) (rnd() % 8) && ! blob.empty(); ++k)
  2068. blob[rnd() % blob.size()] = (std::uint8_t) rnd();
  2069. ++mutations;
  2070. lynx::SynthSettings synth;
  2071. float gain = 1.0f;
  2072. lynx::synthFromSytrus (blob, 120.0, synth, gain);
  2073. lynx::synthFromHarmor (blob, synth, gain);
  2074. lynx::synthFromGms (blob, 120.0, synth, gain);
  2075. lynx::synthFromOgun (blob, synth, gain);
  2076. lynx::synthFromMorphine (blob, synth, gain);
  2077. lynx::synthFromKepler (blob, synth, gain);
  2078. lynx::synthFromPoiZone (blob, synth, gain);
  2079. lynx::synthFromToxic (blob, synth, gain);
  2080. lynx::synthFromBassDrum (blob, synth, gain);
  2081. lynx::synthFromHarmless (blob, 120.0, synth, gain);
  2082. lynx::harmlessParams (blob);
  2083. if (lynx::harmorStateUsable (blob))
  2084. lynx::harmorFallbacks (blob);
  2085. lynx::parseEnvelopeController (blob);
  2086. lynx::parseKeyboardController (blob);
  2087. lynx::parseLovePhilter (blob);
  2088. lynx::parsePatcher (blob);
  2089. lynx::effectFromFl (name, blob);
  2090. lynx::fpcPads (blob);
  2091. lynx::directWaveProgram (blob);
  2092. CHECK (std::isfinite (gain) && std::isfinite (synth.cutoff));
  2093. }
  2094. std::printf (" mutation fuzz: %d blobs from %d plugins\n", mutations, (int) samples.size());
  2095. }
  2096. // WavPack (.wv) samples from FL packs: all decode, with finite, non-silent audio
  2097. {
  2098. int files = 0, decoded = 0, audible = 0;
  2099. double seconds = 0.0;
  2100. for (const auto& entry : std::filesystem::recursive_directory_iterator (std::filesystem::path (realFlpDir) / "Patches" / "Packs"))
  2101. if (entry.path().extension() == ".wv")
  2102. {
  2103. std::ifstream in (entry.path(), std::ios::binary);
  2104. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  2105. ++files;
  2106. lynx::DecodedAudio audio;
  2107. if (! lynx::decodeWavPack (file.data(), file.size(), audio))
  2108. continue;
  2109. ++decoded;
  2110. float peak = 0.0f;
  2111. bool finite = true;
  2112. for (const auto& ch : audio.channels)
  2113. for (auto v : ch)
  2114. {
  2115. finite = finite && std::isfinite (v);
  2116. peak = std::max (peak, std::abs (v));
  2117. }
  2118. CHECK (finite && peak <= 4.0f);
  2119. audible += peak > 1e-4f;
  2120. seconds += (double) audio.channels[0].size() / audio.sampleRate;
  2121. }
  2122. std::printf (" WavPack: %d .wv files, %d decoded, %d audible, %.0f s of audio\n", files, decoded, audible, seconds);
  2123. CHECK (decoded == files);
  2124. }
  2125. // native GMS presets (.gmsynth = the contents of 0xD5 without the "SSLF")
  2126. int gmsPresets = 0, gmsDecoded = 0;
  2127. const auto gmsFolder = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "GMS";
  2128. if (std::filesystem::exists (gmsFolder))
  2129. for (const auto& entry : std::filesystem::recursive_directory_iterator (gmsFolder))
  2130. if (entry.path().extension() == ".gmsynth")
  2131. {
  2132. std::ifstream in (entry.path(), std::ios::binary);
  2133. std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  2134. std::vector<std::uint8_t> blob { 'S', 'S', 'L', 'F' };
  2135. const auto n = (std::uint32_t) file.size();
  2136. blob.insert (blob.end(), reinterpret_cast<const std::uint8_t*> (&n), reinterpret_cast<const std::uint8_t*> (&n) + 4);
  2137. blob.insert (blob.end(), file.begin(), file.end());
  2138. lynx::SynthSettings gs;
  2139. float g = 1.0f;
  2140. ++gmsPresets;
  2141. if (lynx::synthFromGms (blob, 120.0, gs, g))
  2142. {
  2143. ++gmsDecoded;
  2144. CHECK (std::isfinite (g) && gs.cutoff >= 20.0f && std::isfinite (gs.pitchEnv.amount) && std::isfinite (gs.lfo[lynx::lfoPitch].period));
  2145. }
  2146. }
  2147. std::printf (" GMS presets: %d, %d decoded\n", gmsPresets, gmsDecoded);
  2148. // the synth's "FL presets" button: Sytrus/Harmor .fst and .gmsynth become synth settings
  2149. int presetFiles = 0, presetsRead = 0;
  2150. for (const auto* name : { "Sytrus", "Harmor", "GMS", "Harmless", "Ogun", "Sakura", "Fruit Kick", "Fruity DX10", "MiniSynth", "Kepler",
  2151. "PoiZone", "Toxic Biohazard", "BassDrum" })
  2152. {
  2153. const auto folder = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / name;
  2154. if (! std::filesystem::exists (folder))
  2155. continue;
  2156. for (const auto& entry : std::filesystem::recursive_directory_iterator (folder))
  2157. if (entry.path().extension() == ".fst" || entry.path().extension() == ".gmsynth")
  2158. {
  2159. std::ifstream in (entry.path(), std::ios::binary);
  2160. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  2161. ++presetFiles;
  2162. presetsRead += lynx::synthFromFlPreset (file).has_value();
  2163. }
  2164. }
  2165. std::printf (" FL synth presets: %d files, %d read\n", presetFiles, presetsRead);
  2166. CHECK (presetsRead >= presetFiles * 95 / 100);
  2167. CHECK (gmsDecoded == gmsPresets);
  2168. CHECK (sytrusDecoded == sytrus);
  2169. CHECK (harmlessDecoded == harmless && harmless > 100);
  2170. std::printf (" Harmless: %d states parsed for the engine, %d channels imported as Harmless\n", harmlessParsed, harmlessKind);
  2171. CHECK (harmlessParsed == harmless && harmlessKind >= harmless);
  2172. std::printf (" Harmor: %d channels, %d states usable by the engine, %d imported as Harmor (%d sounding)\n", harmor, harmorUsable, harmorKind, harmorSounding);
  2173. CHECK (harmorUsable == harmor && harmor > 90 && harmorKind >= harmor && harmorSounding >= harmorKind * 9 / 10);
  2174. 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",
  2175. gms, gmsUsable, gmsKind, gmsSounding, morphine, morphineUsable, morphineKind, morphineSounding, demoGoldens);
  2176. CHECK (gmsUsable == gms && gms >= 9 && gmsKind == gms && gmsSounding == gmsKind);
  2177. std::printf (" MiniSynth channels: %d, %d usable, %d on the exact engine, %d sounding, %d golden\n", miniSynth, miniSynthUsable, miniSynthKind,
  2178. miniSynthSounding, miniSynthGoldens);
  2179. CHECK (miniSynthUsable == miniSynth && miniSynth >= 12 && miniSynthKind == miniSynth && miniSynthSounding == miniSynthKind && miniSynthGoldens == 2);
  2180. CHECK (morphineUsable == morphine && morphine >= 7 && morphineKind == morphine && morphineSounding == morphineKind && demoGoldens == 2);
  2181. std::printf (" PoiZone: %d channels, %d usable, %d imported as PoiZone (%d sounding), %d demo golden\n", poizone, poizoneUsable, poizoneKind,
  2182. poizoneSounding, poizoneGoldens);
  2183. CHECK (poizoneUsable == poizone && poizone >= 2 && poizoneKind == poizone && poizoneSounding == poizoneKind && poizoneGoldens == 1);
  2184. std::printf (" Toxic Biohazard: %d channels, %d usable, %d imported as Toxic (%d sounding), %d golden\n", toxic, toxicUsable, toxicKind,
  2185. toxicSounding, toxicGoldens);
  2186. CHECK (toxicUsable == toxic && toxic >= 3 && toxicKind == toxic && toxicSounding == toxicKind && toxicGoldens == 1);
  2187. std::printf (" Ogun: %d channels, %d states usable by the engine, %d imported as Ogun (%d sounding)\n", ogun, ogunUsable, ogunKind, ogunSounding);
  2188. CHECK (ogunUsable == ogun && ogun >= 20 && ogunKind >= ogun && ogunSounding >= ogunKind * 9 / 10);
  2189. std::printf (" Autogun: %d channels, %d states usable by the engine, %d imported as Autogun (%d with SG.dat, %d sounding)\n", autogun,
  2190. autogunUsable, autogunKind, autogunWithSg, autogunSounding);
  2191. CHECK (autogunUsable == autogun && autogun >= 9 && autogunKind == autogun && autogunWithSg == autogun && autogunSounding == autogun);
  2192. std::printf (" Fruity DX10: %d channels, %d states usable by the engine, %d imported as DX10 (%d sounding)\n", dx10, dx10Usable, dx10Kind, dx10Sounding);
  2193. CHECK (dx10Usable == dx10 && dx10 >= 12 && dx10Kind == dx10 && dx10Sounding == dx10Kind);
  2194. std::printf (" Kepler: %d channels, %d states usable by the engine, %d imported as Kepler (%d sounding)\n", kepler, keplerUsable, keplerKind, keplerSounding);
  2195. CHECK (keplerUsable == kepler && kepler >= 4 && keplerSounding == keplerKind && (keplerKind == 0 || keplerKind == kepler));
  2196. std::printf (" Harmless rack compressor: %d of %d presets read from the installation\n", harmlessCompLoaded, harmlessCompWanted);
  2197. CHECK (harmlessCompLoaded == harmlessCompWanted && harmlessCompWanted > 100);
  2198. std::printf (" Maximus: %d of %d slots on the exact engine; Soundgoodizer: %d of %d; Vocodex with SG.dat: %d of %d\n", maximusExact,
  2199. maximusSlots, sgzExact, sgzSlots, vocodexWithData, vocodexSlots);
  2200. // REA_rodada40 §6: in Demo projects the 4 states left on the approximation use the Frequency Splitter linear-phase
  2201. // option; the other Data folders add states with options not decoded
  2202. CHECK (maximusSlots - maximusExact <= 16 && maximusExact >= 97 && sgzExact == sgzSlots && sgzSlots > 90);
  2203. CHECK (vocodexWithData == vocodexSlots && vocodexSlots >= 10);
  2204. std::printf (" Fruity WaveShaper: %d of %d slots on the exact engine; Fruity Soft Clipper: %d of %d\n", shaperExact, shaperSlots,
  2205. clipperExact, clipperSlots);
  2206. // (Patcher graphs add WaveShaper nodes of their own to the imported mixer)
  2207. CHECK (shaperExact >= shaperSlots && shaperSlots >= 70 && clipperExact >= clipperSlots && clipperSlots >= 25); // REA_rodada51
  2208. std::printf (" Fruity Parametric EQ 2 / Parametric EQ: %d of %d slots on the exact engine\n", flPeqExact, flPeqSlots);
  2209. CHECK (flPeqExact >= flPeqSlots && flPeqSlots >= 1100); // REA_rodada50
  2210. std::printf (" Fruity PanOMatic / Balance / Blood Overdrive: %d of %d slots on the exact engines\n", basicExact, basicSlots);
  2211. // (since round 141 a Patcher stays one slot: "Bombs Away" insert 69 keeps its last PanOMatic)
  2212. CHECK (basicExact >= basicSlots && basicSlots >= 270); // REA_rodada58
  2213. // (Patcher graphs can add Distructor nodes of their own to the imported mixer)
  2214. std::printf (" Distructor: %d of %d slots on the exact engine (%d cabinet impulses read from the installation)\n",
  2215. distructorExact, distructorSlots, distructorImpulses);
  2216. CHECK (distructorExact >= distructorSlots && distructorSlots >= 4 && distructorImpulses >= 1); // REA_rodada71
  2217. std::printf (" EQUO: %d of %d slots on the exact engine\n", equoExact, equoSlots);
  2218. CHECK (equoExact == equoSlots && equoSlots >= 10); // REA_rodada61
  2219. std::printf (" Pitch Shifter: %d of %d slots on the exact engine\n", pitchShifterExact, pitchShifterSlots);
  2220. CHECK (pitchShifterExact == pitchShifterSlots && pitchShifterSlots >= 15); // REA_rodada61
  2221. std::printf (" Fruity Fast LP / Free Filter / Filter: %d of %d slots on the exact engines\n", filterExact, filterSlots);
  2222. CHECK (filterExact >= filterSlots && filterSlots >= 53); // REA_rodada63
  2223. std::printf (" Fruity Delay 2: %d of %d slots on the exact engine; Fruity Delay 3: %d of %d\n", delay2Exact, delay2Slots,
  2224. delay3Exact, delay3Slots);
  2225. // (FL 11 files' slots are read since round 80; Patcher graphs can add Delay 2 nodes of their own)
  2226. CHECK (delay2Exact >= delay2Slots && delay2Slots >= 60 && delay3Exact >= delay3Slots && delay3Slots >= 100); // REA_rodada52
  2227. std::printf (" Chorus family: %d of %d slots on the exact engines\n", chorusExact, chorusSlots);
  2228. CHECK (chorusExact >= chorusSlots && chorusSlots >= 47); // REA_rodada59
  2229. 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,
  2230. fastDistSlots, flangusExact, flangusSlots, flangerExact, flangerSlots);
  2231. CHECK (fastDistExact >= fastDistSlots && fastDistSlots >= 30 && flangusExact >= flangusSlots && flangusSlots >= 30
  2232. && flangerExact >= flangerSlots && flangerSlots >= 3); // REA_rodada55
  2233. std::printf (" Fruity Phaser: %d of %d slots on the exact engine\n", phaserExact, phaserSlots);
  2234. CHECK (phaserExact >= phaserSlots && phaserSlots >= 3); // REA_rodada105_phaser
  2235. // 3x Osc channel presets shipped with FL (.fst): all become synth settings
  2236. int presets = 0, presetsDecoded = 0;
  2237. const auto presetDir = std::filesystem::path (realFlpDir) / "Patches" / "Channel presets" / "3x Osc";
  2238. if (std::filesystem::is_directory (presetDir))
  2239. for (auto& entry : std::filesystem::directory_iterator (presetDir))
  2240. if (entry.path().extension() == ".fst")
  2241. {
  2242. ++presets;
  2243. std::ifstream in (entry.path(), std::ios::binary);
  2244. std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  2245. if (auto synth = lynx::synthFromFlPreset (bytes))
  2246. {
  2247. ++presetsDecoded;
  2248. CHECK (synth->osc[0].level + synth->osc[1].level + (synth->am ? 0.0f : synth->osc[2].level) <= 1.001f);
  2249. }
  2250. }
  2251. std::printf (" %d 3x Osc presets, %d read\n", presets, presetsDecoded);
  2252. CHECK (presetsDecoded == presets);
  2253. // FL effect presets that become built-in effects
  2254. int fxPresets = 0, fxDecoded = 0;
  2255. for (auto kind : { lynx::EffectKind::parametricEq, lynx::EffectKind::limiter, lynx::EffectKind::reverb, lynx::EffectKind::delay,
  2256. lynx::EffectKind::utility })
  2257. for (const auto& plugin : lynx::flPluginsForEffect (kind))
  2258. {
  2259. const auto dir = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Effects" / plugin;
  2260. if (! std::filesystem::is_directory (dir))
  2261. continue;
  2262. for (auto& entry : std::filesystem::recursive_directory_iterator (dir))
  2263. if (entry.path().extension() == ".fst")
  2264. {
  2265. ++fxPresets;
  2266. std::ifstream in (entry.path(), std::ios::binary);
  2267. std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  2268. fxDecoded += lynx::effectFromFlPreset (bytes).has_value();
  2269. }
  2270. }
  2271. std::printf (" %d FL effect presets, %d read\n", fxPresets, fxDecoded);
  2272. CHECK (fxDecoded == fxPresets);
  2273. CHECK (wrappedParsed == wrapped);
  2274. CHECK (wrappedVst2State == wrappedVst2 && wrappedVst2 >= 23 && wrappedVst2Old <= 13);
  2275. std::printf (" %d plugins in Fruity Wrapper, %d read; %d VST2 states, %d decoded (%d not written back byte-identical)\n", wrapped,
  2276. wrappedParsed, wrappedVst2, wrappedVst2State, wrappedVst2Old);
  2277. std::printf (" %d markers, %d time signature changes, %d tracks with EQ\n", markers, signatures, trackEqs);
  2278. std::printf (" %d parametric EQs (%d with FL settings), %d reverbs (%d without dry)\n", peqSlots, peqShaped, reverbs, reverbsDryless);
  2279. std::printf (" DirectWave: %d channels, %d zones; FPC: %d projects, %d pad channels, %d pad notes\n", dwChannels, dwZones, fpcKits,
  2280. padChannels, padNotes);
  2281. CHECK (dwZones > 1000 && padChannels > 250 && padNotes > 500);
  2282. std::printf (" DirectWave on the exact engine (REA_rodada72): %d channels\n", dwExact);
  2283. CHECK (dwExact >= 57);
  2284. std::printf (" sliced channels (Slicer/Slicex): %d; embedded WAVs saved: %d\n", slicedChannels, embeddedSaved);
  2285. CHECK (slicedChannels >= 25 && embeddedSaved >= 10); // without decoding Ogg here, some loops have no duration
  2286. std::printf (" FL Keys (Grand Piano, 30 zones): %d channels\n", flKeysChannels);
  2287. CHECK (flKeysChannels >= 8);
  2288. CHECK (peqSlots > 1000 && peqShaped > 900 && reverbsDryless > 20 && reverbsLowCut > 250);
  2289. CHECK (markers > 300 && signatures > 60);
  2290. std::printf (" %d 3x Osc channels, %d decoded, %d on the exact engine (%d sounding)\n", threeOsc, threeOscDecoded, threeOscKind, threeOscSounding);
  2291. // one demo channel (a noise layer of "Right Night feeling") has the channel filter closed (fast LP, Mod X 0): silent
  2292. // with its saved settings, in FL too
  2293. CHECK (threeOscKind == threeOsc && threeOscSounding == threeOscKind - 1);
  2294. std::printf (" %d .flp files read, %d with notes, %d with playlist, %d automations (%d with target), %d sends\n",
  2295. files, withNotes, withPlaylist, automations, automationsWithTarget, sends);
  2296. CHECK (files > 0);
  2297. CHECK (automations > 0 && sends > 0);
  2298. }
  2299. // ---------------------------------------------------------------------------
  2300. // Sequencer
  2301. TEST (pattern_toggle_step_and_length)
  2302. {
  2303. lynx::Pattern p;
  2304. CHECK (! p.hasStep (0, 4));
  2305. p.toggleStep (0, 4);
  2306. CHECK (p.hasStep (0, 4));
  2307. CHECK (p.notes.size() == 1 && p.notes[0].start == 4 * lynx::ticksPerStep);
  2308. CHECK (p.effectiveLength (lynx::Meter (4, 4)) == 4 * lynx::ppq); // one bar
  2309. p.toggleStep (1, 20); // second bar
  2310. CHECK (p.effectiveLength (lynx::Meter (4, 4)) == 8 * lynx::ppq);
  2311. p.toggleStep (0, 4);
  2312. CHECK (! p.hasStep (0, 4) && p.notes.size() == 1);
  2313. }
  2314. TEST (pattern_mute_removed_steps)
  2315. {
  2316. lynx::Pattern p;
  2317. p.toggleStep (0, 2);
  2318. p.notes[0].velocity = 0.3f;
  2319. p.toggleStep (0, 2, true); // off: muted, kept
  2320. CHECK (! p.hasStep (0, 2) && p.notes.size() == 1 && p.notes[0].muted);
  2321. p.toggleStep (0, 2, true); // on again: the same note back
  2322. CHECK (p.hasStep (0, 2) && p.notes.size() == 1 && ! p.notes[0].muted && p.notes[0].velocity == 0.3f);
  2323. p.toggleStep (0, 2, true);
  2324. // option off (FL's SetStep, round 169f): turning the muted step on adds a new note and leaves the muted one alone...
  2325. p.toggleStep (0, 2);
  2326. CHECK (p.hasStep (0, 2) && p.notes.size() == 2 && p.notes[0].muted != p.notes[1].muted);
  2327. p.toggleStep (0, 2); // ...and turning it off deletes every note of the step, the muted one too
  2328. CHECK (p.notes.empty());
  2329. // option on with two notes on the step: both muted, both back
  2330. p.toggleStep (0, 3);
  2331. p.notes.push_back (p.notes[0]);
  2332. p.notes[1].key = 64;
  2333. p.toggleStep (0, 3, true);
  2334. CHECK (! p.hasStep (0, 3) && p.notes.size() == 2 && p.notes[0].muted && p.notes[1].muted);
  2335. p.toggleStep (0, 3, true);
  2336. CHECK (p.hasStep (0, 3) && p.notes.size() == 2 && ! p.notes[0].muted && ! p.notes[1].muted);
  2337. // another channel's note on the same step is never touched
  2338. p.toggleStep (1, 3);
  2339. p.toggleStep (0, 3, true);
  2340. CHECK (p.hasStep (1, 3) && ! p.hasStep (0, 3));
  2341. }
  2342. TEST (scheduler_places_events_at_exact_samples)
  2343. {
  2344. lynx::Pattern p;
  2345. p.toggleStep (0, 0);
  2346. p.toggleStep (0, 4); // tick 960 = 1 beat
  2347. const double sr = 48000.0, bpm = 120.0; // 1 beat = 0.5 s = 24000 samples
  2348. const auto tps = lynx::ticksPerSample (bpm, sr);
  2349. lynx::EventBuffer ev;
  2350. lynx::schedulePatternLoop (p, p.effectiveLength (lynx::Meter (4, 4)), 23900.0 * tps, 512, tps, ev);
  2351. // the beat-2 note starts at sample 24000 -> offset 100 in this block
  2352. int ons = 0;
  2353. for (auto& e : ev)
  2354. if (e.type == lynx::NoteEvent::Type::on)
  2355. {
  2356. ++ons;
  2357. CHECK (e.sampleOffset == 100);
  2358. }
  2359. CHECK (ons == 1);
  2360. }
  2361. TEST (scheduler_wraps_the_loop)
  2362. {
  2363. lynx::Pattern p;
  2364. p.toggleStep (2, 0);
  2365. const auto len = p.effectiveLength (lynx::Meter (4, 4)); // 3840 ticks
  2366. const double sr = 48000.0;
  2367. const auto tps = lynx::ticksPerSample (120.0, sr);
  2368. lynx::EventBuffer ev;
  2369. // block that crosses the loop end: starts 10 samples before
  2370. const auto start = (double) len - 10.0 * tps;
  2371. lynx::schedulePatternLoop (p, len, start, 64, tps, ev);
  2372. bool found = false;
  2373. for (auto& e : ev)
  2374. if (e.type == lynx::NoteEvent::Type::on && e.channel == 2)
  2375. {
  2376. found = true;
  2377. CHECK (e.sampleOffset == 10);
  2378. }
  2379. CHECK (found);
  2380. }
  2381. TEST (scheduler_off_before_on_at_same_sample)
  2382. {
  2383. lynx::Pattern p;
  2384. lynx::Note a;
  2385. a.start = 0; a.length = 240; a.channel = 0;
  2386. lynx::Note b = a;
  2387. b.start = 240;
  2388. p.notes = { a, b };
  2389. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  2390. lynx::EventBuffer ev;
  2391. lynx::schedulePatternLoop (p, p.effectiveLength (lynx::Meter (4, 4)), 200.0, 4000, tps, ev);
  2392. CHECK (ev.size() >= 2);
  2393. CHECK (ev[0].type == lynx::NoteEvent::Type::off);
  2394. CHECK (ev[1].type == lynx::NoteEvent::Type::on);
  2395. CHECK (ev[0].sampleOffset == ev[1].sampleOffset);
  2396. }
  2397. TEST (snapshot_exchange_hands_over_and_recycles)
  2398. {
  2399. struct Counted
  2400. {
  2401. int value;
  2402. int* alive;
  2403. Counted (int v, int* a) : value (v), alive (a) { ++*alive; }
  2404. ~Counted() { --*alive; }
  2405. };
  2406. int alive = 0;
  2407. {
  2408. lynx::SnapshotExchange<Counted> x;
  2409. CHECK (x.acquire() == nullptr);
  2410. x.publish (std::make_unique<Counted> (1, &alive));
  2411. CHECK (x.acquire()->value == 1);
  2412. x.publish (std::make_unique<Counted> (2, &alive));
  2413. x.publish (std::make_unique<Counted> (3, &alive)); // 2 was never read: freed immediately
  2414. CHECK (alive == 2);
  2415. CHECK (x.acquire()->value == 3);
  2416. CHECK (alive == 2); // 1 was returned, but only the UI frees it
  2417. x.collectGarbage();
  2418. CHECK (alive == 1);
  2419. // without collection, the return slots fill up and the audio side holds on to the current one
  2420. for (int i = 0; i < 20; ++i)
  2421. {
  2422. x.publish (std::make_unique<Counted> (10 + i, &alive));
  2423. CHECK (x.acquire() != nullptr);
  2424. }
  2425. }
  2426. CHECK (alive == 0);
  2427. }
  2428. TEST (rack_renderer_makes_sound_and_reuses_instruments)
  2429. {
  2430. auto song = lynx::Song::makeDefault();
  2431. song.bpm = 120.0;
  2432. const double sr = 48000.0;
  2433. auto state = lynx::RackState::build (song, nullptr, sr);
  2434. lynx::RackRenderer r;
  2435. r.prepare (sr);
  2436. std::vector<float> l (512), rr (512);
  2437. double tick = 0.0;
  2438. float peak = 0.0f;
  2439. int kicks = 0;
  2440. for (int block = 0; block < 93; ++block) // 47616 samples: just before 1 s
  2441. {
  2442. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  2443. for (auto& e : r.lastEvents())
  2444. if (e.type == lynx::NoteEvent::Type::on && e.channel == 0)
  2445. ++kicks;
  2446. for (int i = 0; i < 512; ++i)
  2447. {
  2448. CHECK (std::isfinite (l[(std::size_t) i]));
  2449. peak = std::max (peak, std::abs (l[(std::size_t) i]));
  2450. }
  2451. tick += 512 * lynx::ticksPerSample (song.bpm, sr);
  2452. }
  2453. CHECK (peak > 0.1f && peak < 4.0f);
  2454. CHECK (kicks == 2); // 120 BPM: beats at 0 and 0.5 s
  2455. // editing the song keeps the instruments of channels whose type didn't change
  2456. song.channels[4].instrument = lynx::InstrumentKind::kick;
  2457. auto next = lynx::RackState::build (song, state.get(), sr);
  2458. CHECK (next->instruments[0] == state->instruments[0]);
  2459. CHECK (next->instruments[4] != state->instruments[4]);
  2460. // stopping silences the synth and triggers no new notes
  2461. r.process (next.get(), false, tick, 512, l.data(), rr.data());
  2462. CHECK (r.lastEvents().size() == 0);
  2463. }
  2464. TEST (flp_import_maps_channels_and_rescales)
  2465. {
  2466. flp::Project p;
  2467. p.header.ppq = 96;
  2468. p.tempo = 150.0;
  2469. p.channels = { { 0, flp::ChannelType::sampler, "Kick 808", "", "", 1 },
  2470. { 1, flp::ChannelType::audioClip, "Vocal", "", "", 2 },
  2471. { 2, flp::ChannelType::instrument, "Lead", "FLEX", "", 3 } };
  2472. flp::Pattern pat;
  2473. pat.index = 1;
  2474. flp::Note n;
  2475. n.position = 96;
  2476. n.length = 0;
  2477. n.channel = 2;
  2478. n.key = 72;
  2479. n.velocity = 64;
  2480. pat.notes.push_back (n);
  2481. p.patterns.push_back (pat);
  2482. const auto s = lynx::importFlp (p);
  2483. CHECK (s.bpm == 150.0);
  2484. CHECK (s.channels.size() == 3); // audio clip channels stay in the rack, as in FL
  2485. CHECK (s.channels[0].instrument == lynx::InstrumentKind::kick);
  2486. CHECK (s.channels[1].instrument == lynx::InstrumentKind::audioClip);
  2487. CHECK (s.channels[2].instrument == lynx::InstrumentKind::synth);
  2488. CHECK (s.patterns.size() == 1);
  2489. CHECK (s.patterns[0].notes[0].start == lynx::ppq); // 96 @ PPQ 96 = 1 beat
  2490. CHECK (s.patterns[0].notes[0].channel == 2);
  2491. CHECK (s.patterns[0].notes[0].length == lynx::ticksPerStep); // step note
  2492. CHECK (std::abs (s.patterns[0].notes[0].velocity - 0.5f) < 1e-6f);
  2493. }
  2494. TEST (live_queue_is_fifo_and_bounded)
  2495. {
  2496. lynx::LiveEventQueue q;
  2497. lynx::NoteEvent e;
  2498. CHECK (! q.pop (e));
  2499. for (int i = 0; i < 10; ++i)
  2500. {
  2501. lynx::NoteEvent in;
  2502. in.key = i;
  2503. CHECK (q.push (in));
  2504. }
  2505. for (int i = 0; i < 10; ++i)
  2506. CHECK (q.pop (e) && e.key == i);
  2507. int pushed = 0;
  2508. while (q.push (e))
  2509. ++pushed;
  2510. CHECK (pushed == (int) lynx::LiveEventQueue::capacity - 1); // one slot stays free to tell full from empty
  2511. }
  2512. // Whether this CPU's RCPPS (the ~12-bit reciprocal estimate, whose table is implementation-defined: Intel and AMD
  2513. // 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
  2514. // every float mantissa in [1, 2) (the estimate does not depend on the exponent in the normal range)
  2515. static bool rcpLikeReferenceCpu()
  2516. {
  2517. #if defined(__SSE__) || defined(_M_X64) || defined(__x86_64__) || defined(__aarch64__) || defined(_M_ARM64) // x86 SSE, or ARM64 through core/Simd.h (sse2neon)
  2518. std::uint64_t h = 1469598103934665603ull;
  2519. for (std::uint32_t m = 0; m < (1u << 23); m += 4)
  2520. {
  2521. alignas (16) float f[4];
  2522. for (std::uint32_t j = 0; j < 4; ++j)
  2523. {
  2524. const std::uint32_t bits = 0x3f800000u | (m + j);
  2525. std::memcpy (&f[j], &bits, 4);
  2526. }
  2527. _mm_store_ps (f, _mm_rcp_ps (_mm_load_ps (f)));
  2528. for (std::uint32_t j = 0; j < 4; ++j)
  2529. {
  2530. std::uint32_t bits;
  2531. std::memcpy (&bits, &f[j], 4);
  2532. h = (h ^ bits) * 1099511628211ull;
  2533. }
  2534. }
  2535. return h == 0xb5de670652cb0383ull;
  2536. #else
  2537. return false;
  2538. #endif
  2539. }
  2540. // FL's sampler interpolators (REA_rodada69 §1): outputs of FLEngine's own kernels (EXEC, r69/py/gold.py) on a synthetic
  2541. // 16-bit stereo signal; the port must give the same bits (linear, 6-point, 24-point sinc, 32-point sinc at speed 1.37,
  2542. // 16-point sinc widened to 24 points with the cutoff at π/9.5 at speed 9.5)
  2543. // 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
  2544. // required where RCPPS matches the capture machine's; elsewhere (Intel CPUs) within a few float ulps.
  2545. TEST (fl_sampler_interpolators_match_fl)
  2546. {
  2547. std::vector<float> l, r;
  2548. std::uint32_t x = 12345;
  2549. for (int k = 0; k < 2 * 144; ++k)
  2550. {
  2551. x = x * 1103515245u + 12345u;
  2552. const auto v = (float) (std::int16_t) (std::uint16_t) (x >> 16) / 32768.0f;
  2553. (k % 2 == 0 ? l : r).push_back (v);
  2554. }
  2555. const int pad = 40;
  2556. 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 } };
  2557. struct Case { int quality; double speed; std::uint32_t bits[16]; };
  2558. const Case cases[] = {
  2559. { 1, 1.0, { 0x3ec1a000, 0x3d774000, 0x3ca1e000, 0x3ec39900, 0xbf2d4600, 0xbe096800, 0xbea0cd17, 0x3f433097, 0x3f2796da, 0xbd9fbd17, 0x3f27a3ff, 0xbda03ff2, 0x3dace746, 0x3eba19c4, 0x3e477a00, 0x3f0f2a80 } },
  2560. { 2, 1.0, { 0x3ec1a000, 0x3d773fff, 0x3d34885d, 0x3eedeaa0, 0xbf49e298, 0xbea52fc4, 0xbeba55fc, 0x3f7c58da, 0x3f27c5c2, 0xbda139af, 0x3f27a3ff, 0xbda03ffa, 0x3dd8e7b1, 0x3ee872c1, 0x3e05b2d5, 0x3f144c4f } },
  2561. { 4, 1.0, { 0x3ec19fff, 0x3d773fff, 0x3d889c32, 0x3ef6b184, 0xbf57cf0d, 0xbf0abecd, 0xbea14a13, 0x3f94f11b, 0x3f27edc4, 0xbda10d8c, 0x3f27a3fd, 0xbda03ffa, 0x3e5baf41, 0x3edb298c, 0x3dd76cbf, 0x3ef9d212 } },
  2562. { 5, 1.37, { 0x3f19d373, 0x3ea9b6a3, 0xbdcbe56e, 0x3e2af6c6, 0xbf476f7a, 0xbe566a13, 0xbe9f166a, 0x3f52b5ad, 0x3f27ad16, 0xbce48792, 0x3f27960a, 0xbce01ebe, 0x3e1b96be, 0x3e163fd8, 0x3f042e42, 0x3ec4be83 } },
  2563. { 3, 9.5, { 0xbd97308e, 0x3dd8783c, 0xbd911374, 0x3e162a41, 0xbd8a959e, 0x3e38b220, 0xbd7e6785, 0x3e58895c, 0xbd380c4f, 0x3e7e3008, 0xbd380e4e, 0x3e7e2696, 0x3d00c6d0, 0xbd627d87, 0x3e308cb8, 0xbcece918 } },
  2564. };
  2565. const lynx::flvp::X87Extended precision;
  2566. const bool exactRcp = rcpLikeReferenceCpu();
  2567. for (const auto& c : cases)
  2568. {
  2569. lynx::flrs::Kernel k;
  2570. k.setQuality (c.quality);
  2571. k.setSpeed (c.speed);
  2572. int same = 0;
  2573. for (int p = 0; p < 8; ++p)
  2574. {
  2575. const auto lo = pad + positions[p].first - k.halfWidth + 1;
  2576. float a, b;
  2577. lynx::flrs::interpolate (k, l.data() + lo, r.data() + lo, positions[p].second, a, b);
  2578. std::uint32_t ba, bb;
  2579. std::memcpy (&ba, &a, 4);
  2580. std::memcpy (&bb, &b, 4);
  2581. if (exactRcp || c.quality < 3) // (linear and 6-point use no reciprocal)
  2582. same += (ba == c.bits[2 * p]) + (bb == c.bits[2 * p + 1]);
  2583. else
  2584. 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);
  2585. }
  2586. CHECK (same == 16);
  2587. }
  2588. CHECK (lynx::flrs::halfWidthFor (4) == 12 && lynx::flrs::halfWidthFor (5) == 16 && lynx::flrs::halfWidthFor (9) == 256);
  2589. CHECK (lynx::flrs::speedFor (0.0f, 1.0) == 1.0 && std::abs (lynx::flrs::speedFor (1200.0f, 0.5) - 1.0) < 1e-12);
  2590. }
  2591. // The sampler at the original rate copies the sample (FL's copy path), and plays until the read position passes the
  2592. // last frame
  2593. TEST (fl_sampler_copies_at_original_rate)
  2594. {
  2595. auto data = std::make_shared<lynx::SampleData>();
  2596. data->sampleRate = 48000.0;
  2597. for (int i = 0; i < 300; ++i)
  2598. data->left.push_back (std::sin (0.05f * (float) i) * 0.5f);
  2599. auto sampler = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  2600. sampler->prepare (48000.0);
  2601. sampler->noteOn (60, 1.0f);
  2602. std::vector<float> l (400), r (400);
  2603. sampler->render (l.data(), r.data(), 400, 1.0f, 1.0f);
  2604. const auto panL = std::cos ((0.0f + 1.0f) * 0.25f * 3.14159265f) * 1.41421356f; // the sampler's centre pan gain
  2605. int exact = 0;
  2606. for (int i = 0; i < 300; ++i)
  2607. exact += l[(std::size_t) i] == data->left[(std::size_t) i] * panL ? 1 : 0;
  2608. CHECK (exact == 300);
  2609. CHECK (l[300] == 0.0f);
  2610. }
  2611. // FL's volume envelope on the sampler (REA_rodada69 §3): the articulator is stepped once per sequencer tick and the level
  2612. // ramps linearly to each tick's value; 96 PPQ at 120 BPM and 48 kHz = 250 samples per tick, attack 20000 = 89 ticks
  2613. TEST (fl_sampler_volume_envelope_steps_per_tick)
  2614. {
  2615. auto data = std::make_shared<lynx::SampleData>();
  2616. data->sampleRate = 48000.0;
  2617. data->left.assign (96000, 0.5f);
  2618. auto sampler = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  2619. sampler->prepare (48000.0);
  2620. lynx::ChannelDef c;
  2621. c.instrument = lynx::InstrumentKind::sampler;
  2622. c.sampler.flVolumeEnvelope = { 0, 1, 100, 20000, 0, 30000, 128, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, 0, 96 };
  2623. sampler->configure (c, 120.0);
  2624. sampler->noteOn (60, 1.0f);
  2625. std::vector<float> l (40000), r (40000);
  2626. sampler->render (l.data(), r.data(), 40000, 1.0f, 1.0f);
  2627. const auto panL = std::cos ((0.0f + 1.0f) * 0.25f * 3.14159265f) * 1.41421356f;
  2628. auto level = [&] (int n) { return l[(std::size_t) n] / (0.5f * panL); };
  2629. CHECK (std::abs (level (1000) - 1000.0f / 22500.0f) < 2e-5f);
  2630. CHECK (std::abs (level (10125) - 10125.0f / 22500.0f) < 2e-5f);
  2631. CHECK (std::abs (level (39000) - 1.0f) < 1e-6f); // the attack ended after 90 ticks; sustain 128 = 1.0
  2632. // release: 20000 = 89 ticks, linear (tension 0), from the current level
  2633. sampler->noteOff (60);
  2634. std::fill (l.begin(), l.end(), 0.0f);
  2635. sampler->render (l.data(), r.data(), 40000, 1.0f, 1.0f);
  2636. CHECK (std::abs (level (2500) - (1.0f - 10.0f / 90.0f)) < 1e-3f);
  2637. CHECK (l[39999] == 0.0f && sampler->idle());
  2638. }
  2639. // FL's native sampler voice (REA_rodada76): FL's own command-line renders of crafted projects (one sampler channel,
  2640. // 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
  2641. // note pan/velocity set; the 12 dB LP with volume, cutoff and pitch envelopes and their releases. FL's int16 codes at
  2642. // a few frames; LYNX within one code. Also the velocity law and the pan law with the channel's levels inside the voice.
  2643. TEST (fl_native_sampler_voice_matches_fl_renders)
  2644. {
  2645. auto noise = std::make_shared<lynx::SampleData>();
  2646. noise->sampleRate = 44100.0;
  2647. {
  2648. std::uint32_t seed = 1;
  2649. for (int i = 0; i < 26460; ++i)
  2650. {
  2651. seed = (seed * 1103515245u + 12345u) & 0x7fffffffu;
  2652. noise->left.push_back ((float) (((double) seed / 2147483647.0 * 2.0 - 1.0) * 0.3));
  2653. }
  2654. }
  2655. struct Ev { int on, off; float vel, pan; };
  2656. auto render = [&] (std::vector<std::int32_t> recs, std::array<int, 3> filter, const std::vector<Ev>& notes,
  2657. std::shared_ptr<lynx::SampleData> data, float chanGain) {
  2658. lynx::ChannelDef c;
  2659. c.instrument = lynx::InstrumentKind::sampler;
  2660. c.sampler.flChannel = recs;
  2661. c.sampler.flChannel.insert (c.sampler.flChannel.end(), { filter[0], filter[1], filter[2], 96 });
  2662. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  2663. lynx::setOfflineRendering (true); // FL's render dialog: 32-point sinc (the pitch envelope interpolates)
  2664. inst->prepare (44100.0);
  2665. lynx::setOfflineRendering (false);
  2666. inst->configure (c, 120.0);
  2667. inst->setSongPosition (0.0);
  2668. std::vector<std::pair<int, int>> events; // (sample, index ≥ 0 on / < 0 off)
  2669. for (int i = 0; i < (int) notes.size(); ++i)
  2670. events.push_back ({ notes[(std::size_t) i].on, i }), events.push_back ({ notes[(std::size_t) i].off, -1 - i });
  2671. std::sort (events.begin(), events.end());
  2672. std::vector<float> l (120000), r (120000);
  2673. int at = 0;
  2674. for (const auto& [when, i] : events)
  2675. {
  2676. inst->render (l.data() + at, r.data() + at, when - at, chanGain, chanGain);
  2677. at = when;
  2678. if (i >= 0)
  2679. {
  2680. inst->setNextNoteExpression (notes[(std::size_t) i].pan, 0.0f, false);
  2681. inst->noteOn (60, notes[(std::size_t) i].vel);
  2682. }
  2683. else
  2684. inst->noteOff (60);
  2685. }
  2686. inst->render (l.data() + at, r.data() + at, 120000 - at, chanGain, chanGain);
  2687. return std::make_pair (l, r);
  2688. };
  2689. const std::vector<std::int32_t> plain = { 0, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, 0 };
  2690. auto records = [&] (std::vector<std::int32_t> vol, std::vector<std::int32_t> cut, std::vector<std::int32_t> pitch) {
  2691. std::vector<std::int32_t> all = plain;
  2692. for (const auto* r : std::initializer_list<const std::vector<std::int32_t>*> { &vol, &cut, &plain, &pitch })
  2693. all.insert (all.end(), r->begin(), r->end());
  2694. return all;
  2695. };
  2696. 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 };
  2697. const auto chan = lynx::channelGain (10000.0f / 12800.0f); // the rack's gain at pan 0 (panGains: gain·√2·cos(π/4))
  2698. const int at[] = { 5, 300, 2000, 9000, 11500, 15000, 22100, 26000 };
  2699. auto check = [&] (const std::pair<std::vector<float>, std::vector<float>>& out, const int* goldL, const int* goldR) {
  2700. int bad = 0;
  2701. for (int n = 0; n < 3; ++n)
  2702. for (int k = 0; k < 8; ++k)
  2703. {
  2704. const auto p = (std::size_t) (44100 * n + at[k]);
  2705. bad += std::abs ((int) std::lround (out.first[p] * 32767.0f) - goldL[8 * n + k]) > 1;
  2706. bad += std::abs ((int) std::lround (out.second[p] * 32767.0f) - goldR[8 * n + k]) > 1;
  2707. }
  2708. return bad;
  2709. };
  2710. {
  2711. // fast LP (Mod X 100, Mod Y 200); notes: len 96, (vel 80, pan 40) len 48, (vel 120, pan 90) len 144
  2712. const auto out = render (records (volPlain, plain, plain), { 100, 200, 0 },
  2713. { { 0, 22050, 100.0f / 128, 0.0f }, { 44100, 55125, 80.0f / 128, -24.0f / 64 }, { 88200, 121275, 120.0f / 128, 26.0f / 64 } },
  2714. noise, chan);
  2715. 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 };
  2716. 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 };
  2717. CHECK (check (out, L, R) == 0);
  2718. }
  2719. {
  2720. // 12 dB LP (Mod X 90, Mod Y 60), volume / cutoff / pitch envelopes; notes len 48, (vel 80, pan 30) len 48, len 30
  2721. const auto out = render (records ({ 4, 1, 100, 3000, 20000, 30000, 100, 6000, 0, 100, 20000, 0, 32950, 0, 0, 0, -101 },
  2722. { 0, 1, 100, 6000, 2000, 15000, 40, 8000, 100, 100, 20000, 0, 32950, 0, 0, 0, 0 },
  2723. { 0, 1, 100, 4000, 20000, 9000, 0, 20000, 600, 100, 20000, 0, 32950, 0, 0, 0, 0 }),
  2724. { 90, 60, 1 },
  2725. { { 0, 11025, 100.0f / 128, 0.0f }, { 44100, 55125, 80.0f / 128, -34.0f / 64 }, { 88200, 95091, 100.0f / 128, 0.0f } },
  2726. noise, chan);
  2727. 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 };
  2728. 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 };
  2729. CHECK (check (out, L, R) == 0);
  2730. }
  2731. {
  2732. // a global volume LFO (flag 0x20, amount 80, speed 24000): one phase for the channel, (k + 1)·increment at song
  2733. // tick k (notes on ticks 0, 100, 300)
  2734. auto vol = volPlain;
  2735. vol[0] = 4 | 0x20, vol[11] = 80, vol[12] = 24000;
  2736. const auto out = render (records (vol, plain, plain), { 256, 0, 0 },
  2737. { { 0, 22050, 100.0f / 128, 0.0f }, { 22969, 45019, 80.0f / 128, 0.0f }, { 68906, 90956, 100.0f / 128, 0.0f } },
  2738. noise, chan);
  2739. const int pos[] = { 500, 3000, 7000, 12000, 23469, 26000, 30000, 69406, 72000, 80000 };
  2740. const int gold[] = { 1149, 765, 374, 4530, 846, -911, -646, 1160, 2316, 727 };
  2741. int bad = 0;
  2742. for (int k = 0; k < 10; ++k)
  2743. bad += std::abs ((int) std::lround (out.first[(std::size_t) pos[k]] * 32767.0f) - gold[k]) > 1;
  2744. CHECK (bad == 0);
  2745. }
  2746. {
  2747. // a sample with its own loop (WAV smpl 4410…8819): FL loops it while the note lasts and fades it out over 10 ms
  2748. // after the note-off (no volume envelope)
  2749. auto looped = std::make_shared<lynx::SampleData>();
  2750. looped->sampleRate = 44100.0;
  2751. for (int i = 0; i < 8820; ++i)
  2752. 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)));
  2753. looped->loopStart = 4410, looped->loopEnd = 8820;
  2754. const auto out = render (records (volPlain, plain, plain), { 256, 0, 0 },
  2755. { { 0, 22050, 100.0f / 128, 0.0f }, { 44100, 49612, 100.0f / 128, 0.0f } }, looped, chan);
  2756. const int pos[] = { 125, 4525, 8925, 13337, 17725, 22025, 22125, 22325, 22437, 49625, 49737, 50025 };
  2757. const int gold[] = { 6338, 4746, 3842, 4161, -1470, -989, -3577, -1174, -32, 4373, 2573, 42 };
  2758. int bad = 0;
  2759. for (int k = 0; k < 12; ++k)
  2760. bad += std::abs ((int) std::lround (out.first[(std::size_t) pos[k]] * 32767.0f) - gold[k]) > 1;
  2761. CHECK (bad == 0);
  2762. }
  2763. {
  2764. // a constant sample: velocity law (FL: vel 100 → 0.389608, 128 → 0.799753, 64 → 0.141805 at the default
  2765. // channel volume, pan centre) and a hard-left note (0.550997 / 0)
  2766. auto dc = std::make_shared<lynx::SampleData>();
  2767. dc->sampleRate = 44100.0;
  2768. dc->left.assign (20000, 0.5f);
  2769. auto level = [&] (float vel, float pan, bool right) {
  2770. const auto out = render (records (volPlain, plain, plain), { 256, 0, 0 }, { { 0, 10000, vel, pan } }, dc, chan);
  2771. return (right ? out.second : out.first)[5000] / 0.5f;
  2772. };
  2773. CHECK (std::abs (level (100.0f / 128, 0.0f, false) - 0.389608f) < 3e-5f);
  2774. CHECK (std::abs (level (1.0f, 0.0f, false) - 0.799753f) < 3e-5f);
  2775. CHECK (std::abs (level (64.0f / 128, 0.0f, false) - 0.141805f) < 3e-5f);
  2776. CHECK (std::abs (level (100.0f / 128, -1.0f, false) - 0.550997f) < 3e-5f);
  2777. CHECK (std::abs (level (100.0f / 128, -1.0f, true)) < 1e-6f);
  2778. }
  2779. }
  2780. // FPC's "cycle" layers (REA_rodada69 §4): among the layers covering the velocity, in order, the one after the last
  2781. // chosen, wrapping; FL's velocity index is round(v·128). Slicex's master volume uses FL's L11 law (§5).
  2782. TEST (fl_fpc_cycle_layers_and_slicex_master_volume)
  2783. {
  2784. auto level = [] (float v) {
  2785. auto d = std::make_shared<lynx::SampleData>();
  2786. d->sampleRate = 48000.0;
  2787. d->left.assign (4800, v);
  2788. return d;
  2789. };
  2790. // A covers 0…63, B 32…95, C 64…127 (all keys)
  2791. auto inst = lynx::makeZonedSampler ({ { { "a.wav", 60, 0, 127, 0, 63 }, level (0.1f) },
  2792. { { "b.wav", 60, 0, 127, 32, 95 }, level (0.2f) },
  2793. { { "c.wav", 60, 0, 127, 64, 127 }, level (0.3f) } });
  2794. inst->prepare (48000.0);
  2795. inst->configure (lynx::ChannelDef {}, 120.0);
  2796. auto play = [&] (float velocity) {
  2797. inst->allNotesOff();
  2798. std::vector<float> l (2000), r (2000);
  2799. inst->render (l.data(), r.data(), 2000, 1.0f, 1.0f);
  2800. std::fill (l.begin(), l.end(), 0.0f);
  2801. inst->noteOn (60, velocity);
  2802. inst->render (l.data(), r.data(), 10, 1.0f, 1.0f);
  2803. return l[5] / velocity;
  2804. };
  2805. auto which = [] (float x) { return (int) std::lround (x / 0.1f / std::cos (0.25f * 3.14159265f) / 1.41421356f); };
  2806. CHECK (which (play (64.0f / 128.0f)) == 2); // candidates B, C: first B
  2807. CHECK (which (play (64.0f / 128.0f)) == 3); // then C
  2808. CHECK (which (play (40.0f / 128.0f)) == 1); // candidates A, B: C is not among them → the first, A
  2809. CHECK (which (play (40.0f / 128.0f)) == 2); // then B
  2810. CHECK (which (play (64.0f / 128.0f)) == 3); // B was last → C
  2811. // Slicex state: version, 8 master values (volume 16000), options and UI state, deck A referencing a file
  2812. std::vector<std::uint8_t> state (4 + 32 + 47 + 12, 0);
  2813. const std::int32_t version = 0x00060003, volume = 16000;
  2814. std::memcpy (state.data(), &version, 4);
  2815. std::memcpy (state.data() + 4 + 8, &volume, 4);
  2816. state.push_back (1);
  2817. state.push_back (5);
  2818. for (char ch : std::string ("x.wav"))
  2819. state.push_back ((std::uint8_t) ch);
  2820. const auto sliced = lynx::slicexSample (state);
  2821. CHECK (sliced.has_value() && sliced->path == "x.wav");
  2822. CHECK (sliced && std::abs (sliced->gain - (float) ((std::pow (11.0, 1.25) - 1.0) / 10.0)) < 1e-5f);
  2823. }
  2824. TEST (fl_keys_state_wti_and_loader)
  2825. {
  2826. using namespace lynx::flkeys;
  2827. // state: u32 1, the wavetable name, 15 int32 knobs
  2828. State st;
  2829. st.wavetable = "Grand Piano";
  2830. for (int i = 0; i < 15; ++i)
  2831. st.knobs[(std::size_t) i] = 10 * i - 50;
  2832. const auto bytes = writeState (st);
  2833. CHECK (bytes.size() == 4 + 1 + 11 + 60);
  2834. const auto back = parseState (bytes);
  2835. CHECK (back && back->wavetable == "Grand Piano" && back->knobs == st.knobs);
  2836. // one WAV per keygroup: the file's own loop gives the default end and loop; a keygroup without a file reuses
  2837. // the previous one; a repeated file name copies the earlier keygroup; each file is followed by a guard frame
  2838. // that repeats the frame `loop` before the end (REA_rodada69 §8.2)
  2839. std::vector<std::string> names;
  2840. auto riff = parseWti ("[info]\nsamples=3\nfiletype=riff\npath=W\nvelocityfactor=2\nsamplerate=22050\n"
  2841. "[sample0]\nname=a.wav\nroot=40\nhigh=50\n[sample1]\nroot=51\nhigh=999\npos=2\nend=5\nloop=3\n"
  2842. "[sample2]\nname=A.WAV\nroot=60\nhigh=70\n", names);
  2843. CHECK (riff && names.size() == 1 && names[0] == "W\\a.wav");
  2844. CHECK (riff && riff->keygroups.size() == 3 && riff->keygroups[2].duplicateOf == 0 && riff->thresholds == std::vector<int> { 0 });
  2845. if (riff)
  2846. {
  2847. riff->files[0].loopStart = 4, riff->files[0].loopEnd = 8;
  2848. FileSamples a;
  2849. for (int i = 0; i < 10; ++i)
  2850. a.samples.push_back ((std::int16_t) (100 * (i + 1)));
  2851. const auto decoded = FlKeysTable::decode (riff->encode());
  2852. CHECK (decoded.has_value());
  2853. const auto w = buildWavetable (*decoded, { a });
  2854. CHECK (w != nullptr);
  2855. if (w != nullptr)
  2856. {
  2857. 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; };
  2858. CHECK (same (w->keygroups[0], { 40, 50, 0, 8, 5 }));
  2859. CHECK (same (w->keygroups[1], { 51, 999, 2, 5, 3 }));
  2860. CHECK (same (w->keygroups[2], { 60, 70, 0, 8, 5 }));
  2861. CHECK (w->data[9] == 400 && w->data[10] == 0 && w->data[8] == 900);
  2862. CHECK (w->sampleRate == 22050.0f && w->velocityFactor == 2.0f && ! w->stereo);
  2863. }
  2864. }
  2865. // one raw file: every keygroup's part is copied one after another with a guard sample; "canloop=false" makes the
  2866. // loop as long as the keygroup's end, so the voice stops there
  2867. auto raw = parseWti ("[info]\nsamples=2\nsamplefile=x.dat\nfiletype=raw\n[sample0]\nroot=36\nhigh=47\npos=0\nend=3\nloop=2\n"
  2868. "[sample1]\nroot=48\nhigh=999\npos=4\nend=7\nloop=4\ncanloop=false\n", names);
  2869. CHECK (raw && raw->raw && names.size() == 1 && names[0] == "x.dat");
  2870. if (raw)
  2871. {
  2872. FileSamples x;
  2873. for (int i = 0; i < 10; ++i)
  2874. x.samples.push_back ((std::int16_t) (10 * (i + 1)));
  2875. const auto w = buildWavetable (*raw, { x });
  2876. CHECK (w != nullptr);
  2877. if (w != nullptr)
  2878. {
  2879. CHECK (w->keygroups[0].pos == 0 && w->keygroups[0].end == 3 && w->keygroups[0].loop == 2);
  2880. CHECK (w->keygroups[1].pos == 5 && w->keygroups[1].end == 8 && w->keygroups[1].loop == 8);
  2881. const std::vector<std::int16_t> expected { 10, 20, 30, 40, 30, 50, 60, 70, 80, 50, 0, 0 };
  2882. CHECK (std::equal (expected.begin(), expected.end(), w->data.begin()));
  2883. // a held note on the non-looping keygroup ends by itself; 33 notes leave 32 sounding (FL's MaxPoly)
  2884. State s2;
  2885. s2.knobs = { -1000, 0, 0, -1000, 500, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  2886. RefNote n;
  2887. n.key = 60;
  2888. const auto out = renderReference (*w, s2, { n }, 44100.0, 4096, 512, true);
  2889. double early = 0, late = 0;
  2890. for (int i = 0; i < 64; ++i)
  2891. early += std::fabs (out[(std::size_t) (2 * i)]);
  2892. for (int i = 2048; i < 4096; ++i)
  2893. late += std::fabs (out[(std::size_t) (2 * i)]);
  2894. CHECK (early > 0.0 && late == 0.0);
  2895. }
  2896. }
  2897. // instrument front-end without a wavetable: silent and idle
  2898. auto inst = makeFlKeysInstrument (nullptr);
  2899. inst->prepare (44100.0);
  2900. inst->noteOn (60, 0.78f);
  2901. std::vector<float> l (256), r (256);
  2902. inst->render (l.data(), r.data(), 256, 1.0f, 1.0f);
  2903. CHECK (inst->idle() && l[100] == 0.0f);
  2904. }
  2905. TEST (ogg_vorbis_decodes_like_fl)
  2906. {
  2907. using namespace lynx::oggvorbis;
  2908. 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 };
  2909. CHECK (isOggWav (wavHeader, sizeof wavHeader));
  2910. CHECK (! isOggStream (wavHeader, sizeof wavHeader));
  2911. Decoded none;
  2912. CHECK (! decode (wavHeader, sizeof wavHeader, none) && none.channels == 0);
  2913. // FNV-1a 64 of the planar float32 samples FL's oggio_x64.dll decodes from installation files (REA_rodada77 §3):
  2914. // a plain file, one with a second (PCM) stream multiplexed in, one where an empty stream's end-of-stream page
  2915. // ends the decode early (FL keeps 17 728 of 20 691 frames), and an FL Keys wavetable
  2916. if (realFlpDir.empty())
  2917. return;
  2918. const auto root = std::filesystem::path (realFlpDir).parent_path();
  2919. const struct { const char* file; std::uint64_t hash; std::size_t frames; int channels; } cases[] = {
  2920. { "Data/Patches/Packs/Drums/Percussion/FPC Wood Hit 2.wav", 0xe6137a6141cd5946ull, 29664, 1 },
  2921. { "Data/Patches/Packs/Instruments/Keyboard/Grand/FL Piano (11).wav", 0x3f60df388ac0c636ull, 264600, 2 },
  2922. { "Data/Patches/Packs/Drums/Percussion/Grv Perc 08.wav", 0x7f235f467e90efe0ull, 17728, 0 },
  2923. { "Plugins/Fruity/Generators/FL Keys/Wavtables/Grand Piano/CHANGPIANOHARD_A8.wav", 0x00bbf4cc42f5f72cull, 153280, 0 },
  2924. };
  2925. for (const auto& c : cases)
  2926. {
  2927. const auto path = root / c.file;
  2928. if (! std::filesystem::exists (path))
  2929. continue;
  2930. std::ifstream in (path, std::ios::binary);
  2931. const std::vector<std::uint8_t> bytes { std::istreambuf_iterator<char> (in), {} };
  2932. CHECK (isOggWav (bytes.data(), bytes.size()));
  2933. Decoded d;
  2934. CHECK (decode (bytes.data(), bytes.size(), d));
  2935. if (d.channels == 0)
  2936. continue;
  2937. std::uint64_t h = 1469598103934665603ULL;
  2938. for (const auto& ch : d.data)
  2939. {
  2940. const auto* p = reinterpret_cast<const std::uint8_t*> (ch.data());
  2941. for (std::size_t i = 0; i < ch.size() * sizeof (float); ++i)
  2942. h = (h ^ p[i]) * 1099511628211ULL;
  2943. }
  2944. CHECK (d.data[0].size() == c.frames && d.sampleRate == 44100);
  2945. CHECK (c.channels == 0 || d.channels == c.channels);
  2946. CHECK (h == c.hash);
  2947. }
  2948. }
  2949. TEST (directwave_crt_math_and_state)
  2950. {
  2951. using namespace lynx::dw;
  2952. auto f = [] (std::uint32_t b) { float x; std::memcpy (&x, &b, 4); return x; };
  2953. auto u = [] (float x) { std::uint32_t b; std::memcpy (&b, &x, 4); return b; };
  2954. // the plugin's own run-time (r72/run/mt.exe on DirectWave_x64.dll): values where the libm result differs
  2955. CHECK (u (sinfCrt (f (0x40b1a97fu))) == 0xbf2af4bbu);
  2956. CHECK (u (sinfCrt (f (0xc0ad8469u))) == 0x3f4222f6u);
  2957. CHECK (u (sinfCrt (f (0xc016e8c7u))) == 0xbf34b32du);
  2958. CHECK (u (cosfCrt (f (0xc0af1cc0u))) == 0x3f305702u);
  2959. CHECK (u (cosfCrt (f (0x3f15cba3u))) == 0x3f556924u);
  2960. CHECK (u (cosfCrt (f (0xc0aeb047u))) == 0x3f2dddfeu);
  2961. CHECK (u (expfCrt (f (0xbfb679c0u))) == 0x3e7622a1u);
  2962. CHECK (encodeFormats ({ true, false }) == std::vector<float> ({ 1.0f, 2.0f, 1.0f, 0.0f }));
  2963. CHECK (decodeFormats ({ 1.0f, 2.0f, 1.0f, 0.0f }) == std::vector<bool> ({ true, false }));
  2964. CHECK (! parseState ({ 1, 2, 3 }).has_value());
  2965. }
  2966. TEST (directwave_matches_fl_with_demo_programs)
  2967. {
  2968. // Demo programs (voices, mod matrix, filters, chorus, reverb, shaper) with our own 16-bit samples in every zone:
  2969. // FNV-1a 64 of DirectWave_x64.dll's output (r72/py/synth.py + cmp.py: 44.1 kHz, host blocks of 512, tempo 120)
  2970. if (realFlpDir.empty())
  2971. return;
  2972. using namespace lynx::dw;
  2973. auto readAll = [] (const std::filesystem::path& p) {
  2974. std::ifstream in (p, std::ios::binary);
  2975. return std::vector<std::uint8_t> { std::istreambuf_iterator<char> (in), {} };
  2976. };
  2977. auto stateOf = [&] (const std::string& file, const std::string& channel) {
  2978. const auto path = std::filesystem::path (realFlpDir) / file;
  2979. if (! std::filesystem::exists (path))
  2980. return std::vector<std::uint8_t>();
  2981. const auto p = flp::parseProject (readAll (path));
  2982. for (const auto& c : p.channels)
  2983. if (c.pluginName == "DirectWave" && c.name == channel)
  2984. return c.pluginState;
  2985. return std::vector<std::uint8_t>();
  2986. };
  2987. auto sampleValue = [] (int z, std::int64_t j) {
  2988. const auto v = (j * (13 + z * 7)) % 4096 - 2048;
  2989. const auto w = ((j * j + z * 31) % 997) - 498;
  2990. return (std::int16_t) std::clamp<std::int64_t> (v * 6 + w * 8, -32768, 32767);
  2991. };
  2992. std::vector<RefNote> notes;
  2993. auto note = [&] (int start, int key, double vel, double fv, double fp, double cents) {
  2994. RefNote n; n.start = start; n.key = key; n.velocity = vel; n.finalVol = fv; n.finalPan = fp; n.cents = cents;
  2995. notes.push_back (n);
  2996. };
  2997. 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);
  2998. note (44100, 36, 0.3, 0.8, 0.8, -40); note (80000, 72, 0.9, 0.6, -0.6, 0);
  2999. notes[0].release = 40000; notes[1].release = 70000; notes[2].release = 60000;
  3000. auto run = [&] (const std::string& file, const std::string& channel, std::uint64_t expected) {
  3001. const auto st = parseState (stateOf (file, channel));
  3002. if (! st)
  3003. return;
  3004. const auto* prog = playedProgram (*st);
  3005. CHECK (prog != nullptr);
  3006. if (prog == nullptr)
  3007. return;
  3008. std::vector<std::shared_ptr<const ZoneSamples>> zs;
  3009. for (int z = 0; z < (int) prog->zones.size(); ++z)
  3010. {
  3011. auto s = std::make_shared<ZoneSamples>();
  3012. s->bits = 16;
  3013. s->channels = 1;
  3014. s->frames = prog->zones[(std::size_t) z].frames;
  3015. std::vector<std::int16_t> v ((std::size_t) s->frames + 512, 0);
  3016. for (std::int64_t j = 0; j < s->frames; ++j)
  3017. v[(std::size_t) j + 256] = sampleValue (z, j);
  3018. s->i16.push_back (std::move (v));
  3019. zs.push_back (s);
  3020. }
  3021. EngineOptions o;
  3022. o.velocityAsInitVol = (st->flags & 2) != 0;
  3023. o.noGainClamp = (st->flags & 1) != 0;
  3024. const auto out = renderReference (*prog, zs, o, notes, 44100.0, 120000, 512, 1);
  3025. std::uint64_t h = 14695981039346656037ull;
  3026. for (const float x : out)
  3027. {
  3028. std::uint32_t b;
  3029. std::memcpy (&b, &x, 4);
  3030. for (int k = 0; k < 4; ++k)
  3031. h = (h ^ ((b >> (8 * k)) & 0xffu)) * 1099511628211ull;
  3032. }
  3033. CHECK (h == expected);
  3034. if (h != expected)
  3035. std::printf (" DirectWave %s: %016llx\n", channel.c_str(), (unsigned long long) h);
  3036. };
  3037. run ("Demo projects/Visual/ZGameEditor Visualizer/Treben - Umziky - All the same.flp", "Gtr", 0xa1de3dd6be53d665ull);
  3038. run ("Demo projects/Visual/ZGameEditor Visualizer/Treben - Umziky - All the same.flp", "Rhodes", 0xcd458ec6a857f9c7ull);
  3039. run ("System/Render tests/Directwave.flp", "DW", 0x95b4c2e29b664dd1ull);
  3040. }
  3041. TEST (fl_keys_matches_fl_with_the_installation)
  3042. {
  3043. // FL Keys' own output (r69/keys/kcmp3.py: the installation's DLL, FL's x87 host pan law, blocks of 512 at
  3044. // 44.1 kHz): FNV-1a 64 of the interleaved float bits of 24 000 frames
  3045. if (realFlpDir.empty())
  3046. return;
  3047. using namespace lynx::flkeys;
  3048. const auto folder = std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "FL Keys" / "Wavtables";
  3049. if (! std::filesystem::exists (folder / "mda Piano.wti"))
  3050. return;
  3051. auto readAll = [] (const std::filesystem::path& p) {
  3052. std::ifstream f (p, std::ios::binary);
  3053. return std::vector<std::uint8_t> { std::istreambuf_iterator<char> (f), {} };
  3054. };
  3055. auto pcm = [] (const std::vector<std::uint8_t>& w) {
  3056. FileSamples f;
  3057. std::uint16_t channels = 1;
  3058. for (std::size_t pos = 12; pos + 8 <= w.size();)
  3059. {
  3060. std::uint32_t size = 0;
  3061. std::memcpy (&size, w.data() + pos + 4, 4);
  3062. if (std::memcmp (w.data() + pos, "fmt ", 4) == 0)
  3063. std::memcpy (&channels, w.data() + pos + 10, 2);
  3064. else if (std::memcmp (w.data() + pos, "data", 4) == 0)
  3065. {
  3066. f.samples.resize (std::min<std::size_t> (size, w.size() - pos - 8) / 2);
  3067. std::memcpy (f.samples.data(), w.data() + pos + 8, f.samples.size() * 2);
  3068. f.channels = channels;
  3069. }
  3070. pos += 8 + size + (size & 1);
  3071. }
  3072. return f;
  3073. };
  3074. auto run = [&] (const std::string& name, std::array<int, 15> knobs, std::uint64_t expected) {
  3075. const auto wti = readAll (folder / (name + ".wti"));
  3076. std::vector<std::string> names;
  3077. auto table = parseWti (std::string (wti.begin(), wti.end()), names);
  3078. CHECK (table.has_value());
  3079. if (! table)
  3080. return;
  3081. std::vector<FileSamples> files;
  3082. for (std::size_t i = 0; i < names.size(); ++i)
  3083. {
  3084. auto rel = names[i];
  3085. std::replace (rel.begin(), rel.end(), '\\', '/');
  3086. const auto bytes = readAll (folder / rel);
  3087. if (table->raw)
  3088. {
  3089. FileSamples f;
  3090. f.samples.resize (bytes.size() / 2);
  3091. std::memcpy (f.samples.data(), bytes.data(), f.samples.size() * 2);
  3092. files.push_back (std::move (f));
  3093. }
  3094. else
  3095. {
  3096. table->files[i] = wavFileInfo (bytes);
  3097. files.push_back (pcm (bytes));
  3098. }
  3099. }
  3100. const auto w = buildWavetable (*table, files);
  3101. CHECK (w != nullptr);
  3102. if (w == nullptr)
  3103. return;
  3104. State s;
  3105. s.wavetable = name;
  3106. s.knobs = knobs;
  3107. auto note = [] (int start, int key, double iv, double fv, double fp, double cents) {
  3108. RefNote n;
  3109. n.start = start, n.key = key, n.initVol = iv, n.finalVol = fv, n.finalPan = fp, n.cents = cents;
  3110. return n;
  3111. };
  3112. 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),
  3113. 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) };
  3114. // (floats as the harness parses them)
  3115. for (auto& n : notes)
  3116. n.initVol = (float) n.initVol, n.finalVol = (float) n.finalVol, n.finalPan = (float) n.finalPan;
  3117. notes[0].release = 9000, notes[2].release = 12000, notes[3].release = 15000, notes[5].release = 20000;
  3118. const auto out = renderReference (*w, s, notes, 44100.0, 24000, 512, true);
  3119. std::uint64_t h = 0xcbf29ce484222325ull;
  3120. for (auto v : out)
  3121. {
  3122. std::uint32_t b;
  3123. std::memcpy (&b, &v, 4);
  3124. for (int k = 0; k < 4; ++k)
  3125. h = (h ^ ((b >> (8 * k)) & 0xff)) * 0x100000001b3ull;
  3126. }
  3127. CHECK (h == expected);
  3128. };
  3129. run ("mda Piano", { 1000, 138, 388, -1000, 716, 0, -1000, 256, 0, 0, 0, 0, 0, 0, 0 }, 0x78268ec007f31309ull);
  3130. run ("Rhodes", { 200, 300, 500, 800, 300, 1500, 300, 1500, 300, 400, 400, 800, 900, 30000, 700 }, 0xcc91c93d917f1d31ull);
  3131. run ("Roto Organ", { 1000, 1000, 1000, 1000, 0, 0, -1000, 0, 1000, 0, 500, 1000, 1000, 0, 300 }, 0x99eb13e87640a1c6ull);
  3132. }
  3133. TEST (sampler_plays_pitched_one_shot)
  3134. {
  3135. // 1000-frame ramp sample: the position can be measured from the amplitude
  3136. auto data = std::make_shared<lynx::SampleData>();
  3137. data->sampleRate = 48000.0;
  3138. for (int i = 0; i < 1000; ++i)
  3139. data->left.push_back (1.0f);
  3140. auto sampler = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  3141. sampler->prepare (48000.0);
  3142. std::vector<float> l (2048, 0.0f), r (2048, 0.0f);
  3143. sampler->noteOn (60, 1.0f);
  3144. sampler->noteOff (60); // one-shot: noteOff doesn't cut
  3145. sampler->render (l.data(), r.data(), 2048, 1.0f, 1.0f);
  3146. int sounding = 0;
  3147. for (auto s : l)
  3148. sounding += s > 0.5f;
  3149. CHECK (sounding >= 995 && sounding <= 1000); // plays the whole sample at the original pitch
  3150. std::fill (l.begin(), l.end(), 0.0f);
  3151. sampler->noteOn (72, 1.0f); // one octave up: half the duration
  3152. sampler->render (l.data(), r.data(), 2048, 1.0f, 1.0f);
  3153. sounding = 0;
  3154. for (auto s : l)
  3155. sounding += s > 0.5f;
  3156. CHECK (sounding >= 495 && sounding <= 500);
  3157. CHECK (r[10] > 0.5f); // mono goes to both sides
  3158. auto silent = lynx::makeInstrument (lynx::InstrumentKind::sampler, nullptr);
  3159. silent->prepare (48000.0);
  3160. silent->noteOn (60, 1.0f);
  3161. std::fill (l.begin(), l.end(), 0.0f);
  3162. silent->render (l.data(), r.data(), 64, 1.0f, 1.0f);
  3163. CHECK (l[0] == 0.0f);
  3164. }
  3165. TEST (rack_live_notes_and_sample_provider)
  3166. {
  3167. lynx::Song song;
  3168. lynx::ChannelDef c;
  3169. c.name = "Smp";
  3170. c.instrument = lynx::InstrumentKind::sampler;
  3171. c.samplePath = "a.wav";
  3172. song.channels.push_back (c);
  3173. song.patterns.emplace_back();
  3174. int loads = 0;
  3175. auto provider = [&] (const std::string& path) -> std::shared_ptr<const lynx::SampleData> {
  3176. ++loads;
  3177. CHECK (path == "a.wav");
  3178. auto d = std::make_shared<lynx::SampleData>();
  3179. d->left.assign (4800, 0.5f);
  3180. return d;
  3181. };
  3182. auto state = lynx::RackState::build (song, nullptr, 48000.0, provider);
  3183. CHECK (loads == 1);
  3184. lynx::RackRenderer renderer;
  3185. renderer.prepare (48000.0);
  3186. lynx::LiveEventQueue live;
  3187. lynx::NoteEvent on;
  3188. on.channel = 0;
  3189. on.velocity = 1.0f;
  3190. live.push (on);
  3191. std::vector<float> l (256), r (256);
  3192. renderer.process (state.get(), false, 0.0, 256, l.data(), r.data(), &live); // stopped, but the live note plays
  3193. CHECK (l[0] > 0.1f);
  3194. // same sample: reused; different sample: reloaded
  3195. auto same = lynx::RackState::build (song, state.get(), 48000.0, provider);
  3196. CHECK (loads == 1 && same->instruments[0] == state->instruments[0]);
  3197. song.channels[0].samplePath = "b.wav";
  3198. auto other = lynx::RackState::build (song, state.get(), 48000.0, [&] (const std::string&) { ++loads; return nullptr; });
  3199. CHECK (loads == 2 && other->instruments[0] != state->instruments[0]);
  3200. }
  3201. TEST (flp_paths_resolve_and_samplers_import)
  3202. {
  3203. lynx::FlPathRoots roots { "C:\\FL", "C:\\FL\\Data", "D:\\User" };
  3204. CHECK (lynx::resolveFlPath ("%FLStudioFactoryData%\\Data\\x.wav", roots) == "C:\\FL\\Data\\x.wav");
  3205. CHECK (lynx::resolveFlPath ("%FLStudioData%\\Patches\\y.wav", roots) == "C:\\FL\\Data\\Patches\\y.wav");
  3206. CHECK (lynx::resolveFlPath ("%FLStudioUserData%\\z.wav", roots) == "D:\\User\\z.wav");
  3207. CHECK (lynx::resolveFlPath ("E:\\abs.wav", roots) == "E:\\abs.wav");
  3208. CHECK (lynx::resolveFlPath ("%FLStudioUserData%\\z.wav", {}) == "");
  3209. {
  3210. // POSIX roots (macOS, Linux, Wine prefix): backslashes become '/', letter case follows the disk (round 117)
  3211. namespace fs = std::filesystem;
  3212. const auto base = fs::temp_directory_path() / ("lynx-case-test-" + processTempTag());
  3213. fs::remove_all (base);
  3214. fs::create_directories (base / "Data" / "Patches" / "Packs");
  3215. { std::ofstream (base / "Data" / "Patches" / "Packs" / "Kick.WAV") << "x"; }
  3216. lynx::FlPathRoots posix;
  3217. posix.factoryData = base.string();
  3218. CHECK (lynx::resolveFlPath ("%FLStudioFactoryData%\\Data\\Patches\\Packs\\Kick.WAV", posix) == (base / "Data/Patches/Packs/Kick.WAV").string());
  3219. const auto lowerCase = lynx::resolveFlPath ("%FLStudioFactoryData%\\data\\patches\\packs\\kick.wav", posix);
  3220. if (fs::exists (base / "data")) // case-insensitive file system (macOS): any spelling opens the file
  3221. CHECK (fs::exists (lowerCase));
  3222. else
  3223. CHECK (lowerCase == (base / "Data/Patches/Packs/Kick.WAV").string());
  3224. CHECK (lynx::resolveFlPath ("%FLStudioFactoryData%\\Data\\missing.wav", posix) == (base / "Data/missing.wav").string());
  3225. fs::remove_all (base);
  3226. }
  3227. flp::Project p;
  3228. p.channels = { { 0, flp::ChannelType::sampler, "Kick", "", "%FLStudioData%\\k.wav", 1 },
  3229. { 1, flp::ChannelType::sampler, "Hat", "", "%FLStudioData%\\missing.wav", 1 } };
  3230. lynx::FlpImportOptions options;
  3231. options.roots = roots;
  3232. options.fileExists = [] (const std::string& path) { return path == "C:\\FL\\Data\\k.wav"; };
  3233. const auto s = lynx::importFlp (p, options);
  3234. CHECK (s.channels[0].instrument == lynx::InstrumentKind::sampler);
  3235. CHECK (s.channels[0].samplePath == "C:\\FL\\Data\\k.wav");
  3236. CHECK (s.channels[1].instrument == lynx::InstrumentKind::hat); // no file: built-in instrument
  3237. }
  3238. TEST (output_protection_is_transparent_up_to_full_scale)
  3239. {
  3240. // round 160: no more soft knee at 0.9 — FL leaves the mix untouched up to 0 dBFS
  3241. float s[] = { 0.5f, -0.89f, 1.5f, -3.0f, 0.95f, 0.999f, std::nanf (""), -1.0f };
  3242. lynx::clipOvers (s, 8);
  3243. CHECK (s[0] == 0.5f && s[1] == -0.89f && s[4] == 0.95f && s[5] == 0.999f && s[7] == -1.0f);
  3244. CHECK (s[2] == 1.0f && s[3] == -1.0f);
  3245. CHECK (s[6] == 0.0f);
  3246. }
  3247. TEST (song_scheduler_plays_clips_with_offset_and_cut)
  3248. {
  3249. lynx::Song s;
  3250. s.channels.resize (1);
  3251. auto& p = s.patterns.emplace_back();
  3252. for (int step : { 0, 4, 8, 12 }) // one note per beat
  3253. p.toggleStep (0, step);
  3254. // clip at bar 2 playing only the second half of the pattern (beats 3 and 4)
  3255. lynx::Clip c;
  3256. c.pattern = 0;
  3257. c.start = 4 * lynx::ppq;
  3258. c.length = 2 * lynx::ppq;
  3259. c.offset = 2 * lynx::ppq;
  3260. s.playlist.push_back (c);
  3261. CHECK (s.songLength() == 6 * lynx::ppq); // FL: the song ends where the last clip ends
  3262. const double sr = 48000.0;
  3263. const auto tps = lynx::ticksPerSample (120.0, sr); // 1 beat = 24000 samples
  3264. lynx::EventBuffer ev;
  3265. // whole song in a single block (8 beats = 192000 samples)
  3266. lynx::scheduleSong (s, 0.0, 192000, tps, ev);
  3267. std::vector<int> ons;
  3268. for (auto& e : ev)
  3269. if (e.type == lynx::NoteEvent::Type::on)
  3270. ons.push_back (e.sampleOffset);
  3271. CHECK (ons.size() == 2);
  3272. CHECK (ons.size() == 2 && ons[0] == 4 * 24000 && ons[1] == 5 * 24000);
  3273. // crossing the loop: bar 1 is empty, so nothing at the start of the second pass
  3274. ev.clear();
  3275. lynx::scheduleSong (s, 8.0 * lynx::ppq - 100 * tps, 200, tps, ev);
  3276. CHECK (ev.size() == 0);
  3277. // a note longer than the clip is cut at the clip end
  3278. p.notes[3].length = 4 * lynx::ppq;
  3279. ev.clear();
  3280. lynx::scheduleSong (s, 0.0, 192000, tps, ev);
  3281. int lastOff = -1;
  3282. for (auto& e : ev)
  3283. if (e.type == lynx::NoteEvent::Type::off)
  3284. lastOff = std::max (lastOff, e.sampleOffset);
  3285. CHECK (lastOff == 6 * 24000);
  3286. }
  3287. TEST (remove_pattern_fixes_clips)
  3288. {
  3289. lynx::Song s;
  3290. s.patterns.resize (3);
  3291. s.currentPattern = 2;
  3292. s.playlist = { { 0, 0, 0, 100, 0 }, { 1, 0, 100, 100, 0 }, { 2, 1, 0, 100, 0 } };
  3293. s.removePattern (1);
  3294. CHECK (s.patterns.size() == 2);
  3295. CHECK (s.playlist.size() == 2 && s.playlist[1].pattern == 1);
  3296. CHECK (s.currentPattern == 1);
  3297. }
  3298. TEST (flp_import_3x_osc_state_and_channel_filter)
  3299. {
  3300. // new 3x Osc patch (version 14): osc 2 mix 64 coarse -12, osc 3 mix 32 coarse -24
  3301. std::vector<std::uint8_t> blob (460, 0);
  3302. auto put = [&] (std::size_t off, std::int32_t v) {
  3303. for (int b = 0; b < 4; ++b)
  3304. blob[off + (std::size_t) b] = (std::uint8_t) ((std::uint32_t) v >> (8 * b));
  3305. };
  3306. put (0x00, 14);
  3307. put (0x08, 3); // osc 1 saw
  3308. put (0x10, 7); // osc 1 fine +7
  3309. put (0x04, -32); // osc 1 pan -0.5
  3310. put (0x1C, 64); // osc 2 mix
  3311. put (0x28, -12); // osc 2 coarse
  3312. put (0x38, 32); // osc 3 mix
  3313. put (0x40, 2); // osc 3 square
  3314. put (0x44, -19); // osc 3 coarse: -1 octave and -7 semitones
  3315. lynx::SynthSettings s;
  3316. CHECK (lynx::synthFrom3xOsc (blob, s));
  3317. 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);
  3318. CHECK (s.osc[0].wave == lynx::Waveform::saw && s.osc[0].detune == 7.0f && s.osc[0].pan == -0.5f);
  3319. CHECK (s.osc[1].wave == lynx::Waveform::sine && s.osc[1].octave == -1 && s.osc[1].semitones == 0);
  3320. CHECK (s.osc[2].wave == lynx::Waveform::square && s.osc[2].octave == -1 && s.osc[2].semitones == -7);
  3321. CHECK (! lynx::synthFrom3xOsc (std::vector<std::uint8_t> (40, 0), s));
  3322. // channel filter: open by default; LP with Mod X 128 ~ 2.71 kHz; Fast LP with Mod X 128 ~ 280 Hz
  3323. float hz = 0.0f, res = 1.0f;
  3324. int mode = -1;
  3325. lynx::synthFilterFromChannel (256, 0, 0, hz, res, mode);
  3326. CHECK (hz >= 19999.0f && res == 0.0f);
  3327. lynx::synthFilterFromChannel (128, 0, 1, hz, res, mode);
  3328. CHECK (std::abs (hz - 2710.0f) < 30.0f && res == 0.0f);
  3329. lynx::synthFilterFromChannel (128, 0, 0, hz, res, mode);
  3330. CHECK (hz > 240.0f && hz < 320.0f);
  3331. lynx::synthFilterFromChannel (128, 256, 1, hz, res, mode);
  3332. CHECK (res > 0.8f);
  3333. CHECK (mode == 0);
  3334. lynx::synthFilterFromChannel (64, 0, 3, hz, res, mode); // high-pass at ~840 Hz
  3335. CHECK (mode == 2 && std::abs (hz - 840.0f) < 40.0f);
  3336. lynx::synthFilterFromChannel (128, 0, 2, hz, res, mode); // band-pass
  3337. CHECK (mode == 1);
  3338. }
  3339. TEST (synth_pitch_and_filter_envelopes)
  3340. {
  3341. auto render = [] (const lynx::SynthSettings& s, int frames) {
  3342. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  3343. inst->prepare (48000.0);
  3344. lynx::ChannelDef c;
  3345. c.synth = s;
  3346. inst->configure (c, 120.0);
  3347. inst->noteOn (45, 1.0f); // A2 = 110 Hz
  3348. std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
  3349. inst->render (l.data(), r.data(), frames, 1.0f, 1.0f);
  3350. return l;
  3351. };
  3352. auto crossings = [] (const std::vector<float>& b, int from, int to) {
  3353. int n = 0;
  3354. for (int i = from + 1; i < to; ++i)
  3355. n += (b[(std::size_t) i - 1] < 0.0f) != (b[(std::size_t) i] < 0.0f);
  3356. return n;
  3357. };
  3358. // 3x Osc "kick": sine with a +24 st pitch envelope falling fast
  3359. lynx::SynthSettings kick;
  3360. kick.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  3361. kick.cutoff = 20000.0f;
  3362. kick.pitchEnv = { 24.0f, 0.0f, 0.0f, 0.05f, 0.0f, 0.05f };
  3363. const auto k = render (kick, 48000);
  3364. const auto early = crossings (k, 0, 2400); // first 50 ms: well above 110 Hz
  3365. const auto late = crossings (k, 24000, 48000); // then back to 110 Hz (~110 crossings in 0.5 s)
  3366. CHECK (early > 20); // +24 st ramp over 50 ms: ~2.2x the average frequency (11 crossings without envelope)
  3367. CHECK (std::abs (late - 110) < 4);
  3368. // cutoff envelope: saw closed at 200 Hz opens +5 octaves on attack and closes again
  3369. lynx::SynthSettings pluck;
  3370. pluck.cutoff = 200.0f;
  3371. pluck.filterEnv = { 5.0f, 0.0f, 0.0f, 0.1f, 0.0f, 0.1f };
  3372. const auto p = render (pluck, 48000);
  3373. auto hf = [&] (int from, int to) { // energy of the difference (highs)
  3374. double e = 0.0;
  3375. for (int i = from + 1; i < to; ++i)
  3376. e += std::pow ((double) p[(std::size_t) i] - p[(std::size_t) i - 1], 2.0);
  3377. return e;
  3378. };
  3379. CHECK (hf (0, 2400) > 4.0 * hf (40000, 42400));
  3380. // Toxic Biohazard's envelope (REA_rodada33 §2): exponential towards 5 clipped at 1, so the cutoff is still near
  3381. // its base early in the attack, fully open at A = 0.2 s and back down after the exponential decay
  3382. lynx::SynthSettings toxic = pluck;
  3383. toxic.filterEnv = { 5.0f, 0.2f, 0.0f, 0.1f, 0.0f, 0.1f, 1 };
  3384. const auto t = render (toxic, 48000);
  3385. auto thf = [&] (int from, int to) {
  3386. double e = 0.0;
  3387. for (int i = from + 1; i < to; ++i)
  3388. e += std::pow ((double) t[(std::size_t) i] - t[(std::size_t) i - 1], 2.0);
  3389. return e;
  3390. };
  3391. CHECK (thf (9000, 9600) > 4.0 * thf (300, 900));
  3392. CHECK (thf (9000, 9600) > 4.0 * thf (40000, 40600));
  3393. }
  3394. TEST (synth_mono_reuses_the_sounding_voice)
  3395. {
  3396. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  3397. inst->prepare (48000.0);
  3398. lynx::ChannelDef c;
  3399. c.synth.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  3400. c.synth.cutoff = 20000.0f;
  3401. c.synth.mono = true;
  3402. inst->configure (c, 120.0);
  3403. std::vector<float> l (48000), r (48000);
  3404. inst->noteOn (57, 1.0f);
  3405. inst->render (l.data(), r.data(), 4800, 1.0f, 1.0f);
  3406. inst->noteOn (69, 1.0f); // A4 takes over the A3 voice: only one note sounding
  3407. inst->render (l.data() + 4800, r.data() + 4800, 43200, 1.0f, 1.0f);
  3408. int crossings = 0;
  3409. float peak = 0.0f;
  3410. for (std::size_t i = 24001; i < 48000; ++i)
  3411. {
  3412. crossings += (l[i - 1] < 0.0f) != (l[i] < 0.0f);
  3413. peak = std::max (peak, std::abs (l[i]));
  3414. }
  3415. CHECK (std::abs (crossings - 440) < 4); // 0.5 s of 440 Hz, without the A3 underneath
  3416. CHECK (peak < 0.35f); // one voice (0.3 level), not two summed
  3417. // no click on the handover: the jump between neighbouring samples stays sine-sized
  3418. float jump = 0.0f;
  3419. for (std::size_t i = 4790; i < 4810; ++i)
  3420. jump = std::max (jump, std::abs (l[i] - l[i - 1]));
  3421. CHECK (jump < 0.05f);
  3422. }
  3423. TEST (synth_porta_glides_and_am_modulates)
  3424. {
  3425. auto make = [] (const lynx::SynthSettings& s) {
  3426. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  3427. inst->prepare (48000.0);
  3428. lynx::ChannelDef c;
  3429. c.synth = s;
  3430. inst->configure (c, 120.0);
  3431. return inst;
  3432. };
  3433. auto crossings = [] (const std::vector<float>& b, std::size_t from, std::size_t to) {
  3434. int n = 0;
  3435. for (auto i = from + 1; i < to; ++i)
  3436. n += (b[i - 1] < 0.0f) != (b[i] < 0.0f);
  3437. return n;
  3438. };
  3439. // mono + 1-beat porta (0.5 s at 120 BPM): from A3 to A4, halfway ~E4 (329.6 Hz)
  3440. lynx::SynthSettings s;
  3441. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  3442. s.cutoff = 20000.0f;
  3443. s.mono = true;
  3444. s.porta = true;
  3445. s.portaBeats = 1.0f;
  3446. auto inst = make (s);
  3447. std::vector<float> l (96000), r (96000);
  3448. inst->noteOn (57, 1.0f);
  3449. inst->render (l.data(), r.data(), 4800, 1.0f, 1.0f);
  3450. inst->noteOn (69, 1.0f);
  3451. inst->render (l.data() + 4800, r.data() + 4800, 91200, 1.0f, 1.0f);
  3452. const auto middle = crossings (l, 4800 + 12000 - 2400, 4800 + 12000 + 2400); // 0.1 s around 0.25 s
  3453. CHECK (std::abs (middle - 66) < 6); // ~330 Hz * 0.1 s * 2
  3454. CHECK (std::abs (crossings (l, 48000, 96000) - 880) < 4); // reached A4 (1 s = 880 crossings)
  3455. // AM: osc 3 at level 1.0, slow sine, modulates osc 1 (the average level drops, the sum doesn't gain osc 3)
  3456. lynx::SynthSettings am;
  3457. am.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  3458. am.osc[2] = { lynx::Waveform::sine, -3, 0.0f, 1.0f };
  3459. am.cutoff = 20000.0f;
  3460. am.am = true;
  3461. auto modulated = make (am);
  3462. modulated->noteOn (69, 1.0f);
  3463. std::fill (l.begin(), l.end(), 0.0f);
  3464. modulated->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
  3465. float peak = 0.0f;
  3466. for (std::size_t i = 4800; i < 48000; ++i)
  3467. peak = std::max (peak, std::abs (l[i]));
  3468. CHECK (peak > 0.45f && peak < 0.65f); // 0.3 · (1 + 1) at most: (1 − o3) goes up to 2
  3469. }
  3470. TEST (channel_lfo_vibrato_and_tremolo)
  3471. {
  3472. auto render = [] (const lynx::SynthSettings& s) {
  3473. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  3474. inst->prepare (48000.0);
  3475. lynx::ChannelDef c;
  3476. c.synth = s;
  3477. inst->configure (c, 120.0);
  3478. inst->noteOn (69, 1.0f);
  3479. std::vector<float> l (48000), r (48000);
  3480. inst->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
  3481. return l;
  3482. };
  3483. lynx::SynthSettings s;
  3484. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  3485. s.cutoff = 20000.0f;
  3486. // tremolo: 100% volume LFO at 4 Hz (0.25 s period) -> the amplitude envelope goes from ~0 to ~2x
  3487. auto tremolo = s;
  3488. tremolo.lfo[lynx::lfoVolume] = { 1.0f, 0.25f, false, 0, 0.0f, 0.0f };
  3489. const auto t = render (tremolo);
  3490. float lowest = 1.0f, highest = 0.0f;
  3491. for (std::size_t w = 4800; w + 480 <= 48000; w += 480) // 10 ms windows (4.4 cycles of 440 Hz)
  3492. {
  3493. float peak = 0.0f;
  3494. for (auto i = w; i < w + 480; ++i)
  3495. peak = std::max (peak, std::abs (t[i]));
  3496. lowest = std::min (lowest, peak);
  3497. highest = std::max (highest, peak);
  3498. }
  3499. CHECK (lowest < 0.08f && highest > 0.5f);
  3500. // synced vibrato: ±1 semitone, 1-beat period (0.5 s at 120 BPM); the average stays at 440 Hz
  3501. auto vibrato = s;
  3502. vibrato.lfo[lynx::lfoPitch] = { 1.0f, 1.0f, true, 0, 0.0f, 0.0f };
  3503. const auto v = render (vibrato);
  3504. int crossings = 0;
  3505. for (std::size_t i = 1; i < 48000; ++i)
  3506. crossings += (v[i - 1] < 0.0f) != (v[i] < 0.0f);
  3507. CHECK (std::abs (crossings - 880) < 12);
  3508. int firstQuarter = 0; // first quarter of the cycle: rising sine -> higher than 440
  3509. for (std::size_t i = 1; i < 6000; ++i)
  3510. firstQuarter += (v[i - 1] < 0.0f) != (v[i] < 0.0f);
  3511. CHECK (firstQuarter > 112);
  3512. }
  3513. TEST (sampler_porta_glides_from_previous_note)
  3514. {
  3515. auto data = std::make_shared<lynx::SampleData>();
  3516. data->sampleRate = 48000.0;
  3517. data->left.resize (96000);
  3518. for (std::size_t i = 0; i < data->left.size(); ++i)
  3519. data->left[i] = 0.5f * (float) std::sin (6.283185307 * 220.0 * (double) i / 48000.0);
  3520. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  3521. inst->prepare (48000.0);
  3522. lynx::ChannelDef c;
  3523. c.sampler.cut = true;
  3524. c.sampler.porta = true;
  3525. c.sampler.portaBeats = 1.0f; // 0.5 s at 120 BPM
  3526. inst->configure (c, 120.0);
  3527. std::vector<float> l (24000), r (24000);
  3528. inst->noteOn (60, 1.0f);
  3529. inst->render (l.data(), r.data(), 2400, 1.0f, 1.0f);
  3530. inst->noteOn (72, 1.0f); // one octave up: starts at 220 Hz and rises
  3531. std::fill (l.begin(), l.end(), 0.0f);
  3532. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  3533. auto crossings = [&] (std::size_t from, std::size_t to) {
  3534. int n = 0;
  3535. for (auto i = from + 1; i < to; ++i)
  3536. n += (l[i - 1] < 0.0f) != (l[i] < 0.0f);
  3537. return n;
  3538. };
  3539. const auto start = crossings (600, 2400); // 37.5 ms still near 220 Hz: ~17 crossings (440 Hz would give 33)
  3540. const auto end = crossings (21600, 24000); // 50 ms at the end of the glide: near 440 Hz (~44; 220 Hz would give 22)
  3541. CHECK (start < 22);
  3542. CHECK (end > 36);
  3543. }
  3544. TEST (zoned_sampler_picks_zone_by_key_and_velocity)
  3545. {
  3546. auto sine = [] (double hz, float amp) {
  3547. auto d = std::make_shared<lynx::SampleData>();
  3548. d->sampleRate = 48000.0;
  3549. d->left.resize (48000);
  3550. for (std::size_t i = 0; i < d->left.size(); ++i)
  3551. d->left[i] = amp * (float) std::sin (6.283185307 * hz * (double) i / 48000.0);
  3552. return d;
  3553. };
  3554. // lows: 220 Hz sine recorded as A3 (57), up to key 69; highs: 880 Hz as A5 (81)
  3555. // hard layer on the highs: same tuning, louder
  3556. std::vector<lynx::LoadedZone> zones {
  3557. { { "graves.wav", 57, 0, 69, 0, 127, 0.0f, 1.0f }, sine (220.0, 0.5f) },
  3558. { { "agudos.wav", 81, 70, 127, 0, 99, 0.0f, 1.0f }, sine (880.0, 0.25f) },
  3559. { { "agudos forte.wav", 81, 70, 127, 100, 127, 0.0f, 1.0f }, sine (880.0, 0.9f) },
  3560. };
  3561. auto inst = lynx::makeZonedSampler (zones);
  3562. inst->prepare (48000.0);
  3563. inst->configure (lynx::ChannelDef {}, 120.0);
  3564. auto play = [&] (int key, float velocity) {
  3565. inst->allNotesOff();
  3566. std::vector<float> l (24000), r (24000);
  3567. inst->render (l.data(), r.data(), 2000, 1.0f, 1.0f); // let the old voices die out
  3568. std::fill (l.begin(), l.end(), 0.0f);
  3569. inst->noteOn (key, velocity);
  3570. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  3571. int crossings = 0;
  3572. float peak = 0.0f;
  3573. for (std::size_t i = 1; i < 24000; ++i)
  3574. {
  3575. crossings += (l[i - 1] < 0.0f) != (l[i] < 0.0f);
  3576. peak = std::max (peak, std::abs (l[i]));
  3577. }
  3578. return std::pair { crossings, peak };
  3579. };
  3580. const auto low = play (69, 0.5f); // A4 from root A3: 440 Hz -> 440 crossings in 0.5 s
  3581. CHECK (std::abs (low.first - 440) < 4);
  3582. const auto soft = play (81, 0.5f); // high zone at the root: 880 Hz
  3583. CHECK (std::abs (soft.first - 880) < 6);
  3584. const auto hard = play (81, 0.95f); // high velocity: hard layer
  3585. CHECK (hard.second > soft.second * 2.0f);
  3586. // overlapping layers round-robin: two identical zones at different levels, notes alternate
  3587. auto rr = lynx::makeZonedSampler ({ { { "a.wav", 60 }, sine (440.0, 0.2f) }, { { "b.wav", 60 }, sine (440.0, 0.8f) } });
  3588. rr->prepare (48000.0);
  3589. rr->configure (lynx::ChannelDef {}, 120.0);
  3590. float peaks[2] {};
  3591. for (int k = 0; k < 2; ++k)
  3592. {
  3593. rr->allNotesOff();
  3594. std::vector<float> l (4800), r (4800);
  3595. rr->render (l.data(), r.data(), 2000, 1.0f, 1.0f);
  3596. std::fill (l.begin(), l.end(), 0.0f);
  3597. rr->noteOn (60, 1.0f);
  3598. rr->render (l.data(), r.data(), 4800, 1.0f, 1.0f);
  3599. for (auto v : l)
  3600. peaks[k] = std::max (peaks[k], std::abs (v));
  3601. }
  3602. CHECK (std::abs (peaks[0] - peaks[1]) > 0.3f);
  3603. }
  3604. TEST (zoned_sampler_loops_while_held)
  3605. {
  3606. // 0.1 s sample with a loop in the middle: holding the note it keeps sounding well past the end of the file;
  3607. // on release, it leaves the loop and ends
  3608. auto data = std::make_shared<lynx::SampleData>();
  3609. data->sampleRate = 48000.0;
  3610. data->left.resize (4800);
  3611. for (std::size_t i = 0; i < data->left.size(); ++i)
  3612. data->left[i] = 0.5f * (float) std::sin (6.283185307 * 480.0 * (double) i / 48000.0); // 100 frames per cycle
  3613. lynx::SampleZone zone { "loop.wav", 60 };
  3614. zone.loopStart = 1000;
  3615. zone.loopEnd = 3000;
  3616. zone.loopMode = 2;
  3617. auto inst = lynx::makeZonedSampler ({ { zone, data } });
  3618. inst->prepare (48000.0);
  3619. lynx::ChannelDef c;
  3620. c.sampler.release = 0.01f;
  3621. inst->configure (c, 120.0);
  3622. inst->noteOn (60, 1.0f);
  3623. std::vector<float> l (48000), r (48000);
  3624. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f); // 0.5 s held
  3625. float held = 0.0f;
  3626. for (std::size_t i = 20000; i < 24000; ++i)
  3627. held = std::max (held, std::abs (l[i]));
  3628. CHECK (held > 0.3f);
  3629. inst->noteOff (60);
  3630. inst->render (l.data() + 24000, r.data() + 24000, 24000, 1.0f, 1.0f);
  3631. float after = 0.0f;
  3632. for (std::size_t i = 40000; i < 48000; ++i)
  3633. after = std::max (after, std::abs (l[i]));
  3634. CHECK (after == 0.0f);
  3635. }
  3636. TEST (global_lfo_follows_song_position)
  3637. {
  3638. // global volume LFO (1-beat period): a note mid-cycle starts mid-wave; the
  3639. // same note with a per-note LFO starts at phase 0
  3640. auto first = [] (bool global, double songTicks) {
  3641. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  3642. inst->prepare (48000.0);
  3643. lynx::ChannelDef c;
  3644. c.synth.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  3645. c.synth.cutoff = 20000.0f;
  3646. c.synth.lfo[lynx::lfoVolume] = { 1.0f, 1.0f, true, 2, 0.0f, 0.0f, global }; // pulse: +1 in the first half
  3647. inst->configure (c, 120.0);
  3648. inst->setSongPosition (songTicks);
  3649. inst->noteOn (69, 1.0f);
  3650. std::vector<float> l (4800), r (4800);
  3651. inst->render (l.data(), r.data(), 4800, 1.0f, 1.0f);
  3652. float peak = 0.0f;
  3653. for (std::size_t i = 2400; i < 4800; ++i)
  3654. peak = std::max (peak, std::abs (l[i]));
  3655. return peak;
  3656. };
  3657. // mid-cycle (phase 0.6) the pulse is at −1: volume ~0; per note (phase 0) it is at +1: ~2x
  3658. const auto globalLate = first (true, 0.6 * lynx::ppq);
  3659. const auto perNote = first (false, 0.6 * lynx::ppq);
  3660. CHECK (globalLate < 0.05f);
  3661. CHECK (perNote > 0.4f);
  3662. }
  3663. TEST (sampler_channel_filter_darkens_and_bypasses_when_open)
  3664. {
  3665. // white noise in the sampler: with a 300 Hz cutoff the energy drops a lot; open passes unchanged
  3666. auto data = std::make_shared<lynx::SampleData>();
  3667. data->sampleRate = 48000.0;
  3668. data->left.resize (48000);
  3669. std::uint32_t seed = 7;
  3670. for (auto& v : data->left)
  3671. {
  3672. seed = seed * 1664525u + 1013904223u;
  3673. v = ((float) (seed >> 8) / 16777216.0f - 0.5f);
  3674. }
  3675. data->right = data->left;
  3676. auto energy = [&] (float cutoff) {
  3677. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  3678. inst->prepare (48000.0);
  3679. lynx::ChannelDef c;
  3680. c.sampler.cutoff = cutoff;
  3681. inst->configure (c, 120.0);
  3682. inst->noteOn (60, 1.0f);
  3683. std::vector<float> l (24000), r (24000);
  3684. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  3685. double e = 0.0;
  3686. for (auto v : l)
  3687. e += (double) v * v;
  3688. return e;
  3689. };
  3690. const auto open = energy (20000.0f), dark = energy (300.0f);
  3691. {
  3692. // high-pass at 10 kHz: also removes most of the noise energy, but from the other side
  3693. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  3694. inst->prepare (48000.0);
  3695. lynx::ChannelDef hp;
  3696. hp.sampler.cutoff = 10000.0f;
  3697. hp.sampler.filterType = 2;
  3698. inst->configure (hp, 120.0);
  3699. inst->noteOn (60, 1.0f);
  3700. std::vector<float> l (24000), r (24000);
  3701. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  3702. double e = 0.0, lowE = 0.0;
  3703. float lp = 0.0f;
  3704. for (auto v : l)
  3705. {
  3706. e += (double) v * v;
  3707. lp += 0.01f * (v - lp); // crude low-pass: almost nothing should remain below
  3708. lowE += (double) lp * lp;
  3709. }
  3710. CHECK (e > 10.0 && e < 0.7 * energy (20000.0f));
  3711. CHECK (lowE < e * 0.01);
  3712. }
  3713. CHECK (open > 100.0);
  3714. CHECK (dark < open * 0.05);
  3715. // +12 st pitch envelope with sustain 1: the sample runs at double speed
  3716. // and ends in half the time (1 s of sample -> 0.5 s)
  3717. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  3718. inst->prepare (48000.0);
  3719. lynx::ChannelDef c;
  3720. c.sampler.pitchEnv = { 12.0f, 0.0f, 0.0f, 0.1f, 1.0f, 0.1f };
  3721. inst->configure (c, 120.0);
  3722. inst->noteOn (60, 1.0f);
  3723. std::vector<float> l (48000), r (48000);
  3724. inst->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
  3725. float tail = 0.0f, body = 0.0f;
  3726. for (std::size_t i = 20000; i < 23000; ++i)
  3727. body = std::max (body, std::abs (l[i]));
  3728. for (std::size_t i = 25000; i < 48000; ++i)
  3729. tail = std::max (tail, std::abs (l[i]));
  3730. CHECK (body > 0.1f);
  3731. CHECK (tail == 0.0f);
  3732. }
  3733. TEST (synth_oscillator_pan_and_semitones)
  3734. {
  3735. auto render = [] (lynx::SynthSettings s, std::vector<float>& l, std::vector<float>& r) {
  3736. auto synth = lynx::makeInstrument (lynx::InstrumentKind::synth);
  3737. synth->prepare (48000.0);
  3738. lynx::ChannelDef c;
  3739. c.synth = s;
  3740. synth->configure (c, 120.0);
  3741. synth->noteOn (69, 1.0f);
  3742. l.assign (24000, 0.0f);
  3743. r.assign (24000, 0.0f);
  3744. synth->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  3745. };
  3746. lynx::SynthSettings s;
  3747. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f, 7, 1.0f }; // a fifth up, fully right
  3748. s.cutoff = 20000.0f;
  3749. std::vector<float> l, r;
  3750. render (s, l, r);
  3751. float peakL = 0.0f, peakR = 0.0f;
  3752. int crossings = 0;
  3753. for (std::size_t i = 4800; i < 24000; ++i)
  3754. {
  3755. peakL = std::max (peakL, std::abs (l[i]));
  3756. peakR = std::max (peakR, std::abs (r[i]));
  3757. crossings += (r[i - 1] < 0.0f) != (r[i] < 0.0f);
  3758. }
  3759. CHECK (peakL < 1e-4f && peakR > 0.1f);
  3760. // 440 * 2^(7/12) = 659.3 Hz; 0.4 s = 263.7 cycles = ~527 crossings
  3761. CHECK (std::abs (crossings - 527) < 6);
  3762. }
  3763. TEST (fl_wrapper_state_parses_records_and_vst3_state)
  3764. {
  3765. // Fruity Wrapper blob (version 12): VST3 generator type, class ID, name, path and state
  3766. std::vector<std::uint8_t> blob;
  3767. 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))); };
  3768. auto record = [&] (std::vector<std::uint8_t>& b, std::uint32_t id, const std::vector<std::uint8_t>& payload) {
  3769. u32 (b, id);
  3770. u32 (b, (std::uint32_t) payload.size());
  3771. u32 (b, 0);
  3772. b.insert (b.end(), payload.begin(), payload.end());
  3773. };
  3774. auto bytes = [] (const std::string& s) { return std::vector<std::uint8_t> (s.begin(), s.end()); };
  3775. std::vector<std::uint8_t> vst3State;
  3776. u32 (vst3State, 1);
  3777. record (vst3State, 1, std::vector<std::uint8_t> (64, 0));
  3778. record (vst3State, 3, bytes ("COMPONENTE"));
  3779. record (vst3State, 2, bytes ("CONTROLADOR"));
  3780. // record 4: 3 slots → ParamIDs 700, −1 (empty), 0xFFFFFF01
  3781. std::vector<std::uint8_t> slots;
  3782. for (auto v : { 3u, 700u, 0xFFFFFFFFu, 0xFFFFFF01u })
  3783. u32 (slots, v);
  3784. record (vst3State, 4, slots);
  3785. u32 (blob, 12);
  3786. record (blob, 0x01, std::vector<std::uint8_t> (20, 0xFF));
  3787. record (blob, 0x32, { 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });
  3788. std::vector<std::uint8_t> cid;
  3789. for (int i = 0; i < 16; ++i)
  3790. cid.push_back ((std::uint8_t) (0xA0 + i));
  3791. record (blob, 0x34, cid);
  3792. record (blob, 0x36, bytes ("Surge XT"));
  3793. record (blob, 0x37, bytes ("C:/Program Files/Common Files/VST3/Surge XT.vst3"));
  3794. record (blob, 0x35, vst3State);
  3795. lynx::ForeignPlugin p;
  3796. CHECK (lynx::parseFlWrapper (blob, p));
  3797. CHECK (p.format == lynx::ForeignPlugin::Format::vst3);
  3798. CHECK (p.hasClassId && p.classId[0] == 0xA0 && p.classId[15] == 0xAF);
  3799. CHECK (p.name == "Surge XT" && p.path.find ("Surge XT.vst3") != std::string::npos);
  3800. std::vector<std::uint8_t> component, controller;
  3801. CHECK (lynx::splitFlVst3State (p.state, component, controller));
  3802. CHECK (component == bytes ("COMPONENTE") && controller == bytes ("CONTROLADOR"));
  3803. const auto saved = lynx::flVst3ParamSlots (p.state);
  3804. CHECK (saved == (std::vector<std::int32_t> { 700, -1, (std::int32_t) 0xFFFFFF01u }));
  3805. // FL's slot assignment: saved slots keep their ParamID wherever it now sits; the empty slot stays empty;
  3806. // a vanished ParamID (0xFFFFFF01) leaves its slot dead; parameters without a slot get slots 3, 4… in list order
  3807. const auto positions = lynx::flVst3SlotPositions (saved, { 5u, 700u, 9u });
  3808. CHECK (positions == (std::vector<int> { 1, -1, -1, 0, 2 }));
  3809. CHECK (lynx::flVst3SlotPositions ({}, { 5u, 6u }) == (std::vector<int> { 0, 1 })); // new instance: slot = position
  3810. // VST2 state (REA_rodada89 §2): chunk and raw-parameter layouts, current (−9) and older versions
  3811. {
  3812. lynx::FlVst2State st;
  3813. st.flags = 8;
  3814. st.currentProgram = 3;
  3815. st.chunk = bytes ("BANKCHUNK");
  3816. const auto withChunk = lynx::writeFlVst2State (st);
  3817. lynx::FlVst2State back;
  3818. CHECK (lynx::parseFlVst2State (withChunk, back));
  3819. CHECK (back.version == -9 && back.flags == 8 && back.currentProgram == 3 && back.chunk == bytes ("BANKCHUNK") && back.params.empty());
  3820. CHECK (! back.chunkIsProgram());
  3821. lynx::FlVst2State raw;
  3822. raw.flags = 1;
  3823. raw.params = { 0.25f, 0.5f, 1.0f };
  3824. raw.programNames = { "Init" };
  3825. const auto withParams = lynx::writeFlVst2State (raw);
  3826. CHECK (withParams.size() == 4 + 1 + 4 + 8 + 4 + 4 + 12 + 4 + 25);
  3827. CHECK (lynx::parseFlVst2State (withParams, back));
  3828. CHECK (back.chunk.empty() && back.params == raw.params && back.allPrograms() && back.programNames == std::vector<std::string> { "Init" });
  3829. // version −5: no flags, i32 size, then i32 program; −6 without "all programs": the chunk is one program
  3830. std::vector<std::uint8_t> v5;
  3831. for (auto v : { 0xFFFFFFFBu, 4u, 7u })
  3832. u32 (v5, v);
  3833. for (auto c : bytes ("ABCD"))
  3834. v5.push_back (c);
  3835. CHECK (lynx::parseFlVst2State (v5, back) && back.version == -5 && back.currentProgram == 7 && back.chunk == bytes ("ABCD"));
  3836. std::vector<std::uint8_t> v6;
  3837. u32 (v6, 0xFFFFFFFAu);
  3838. v6.push_back (0);
  3839. for (auto v : { 2u, 0u })
  3840. u32 (v6, v);
  3841. v6.push_back ('x');
  3842. v6.push_back ('y');
  3843. CHECK (lynx::parseFlVst2State (v6, back) && back.chunkIsProgram() && back.chunk == bytes ("xy"));
  3844. // nothing to restore (123456), unknown old layouts, truncated chunks
  3845. std::vector<std::uint8_t> marker;
  3846. u32 (marker, 123456u);
  3847. CHECK (! lynx::parseFlVst2State (marker, back));
  3848. auto cut = withChunk;
  3849. cut.pop_back();
  3850. CHECK (! lynx::parseFlVst2State (cut, back));
  3851. CHECK (! lynx::parseFlVst2State ({}, back));
  3852. }
  3853. // version out of range or no type: not a usable wrapper
  3854. blob[0] = 13;
  3855. CHECK (! lynx::parseFlWrapper (blob, p));
  3856. CHECK (! lynx::parseFlWrapper ({ 12, 0, 0, 0 }, p));
  3857. }
  3858. TEST (schedule_index_matches_full_scan)
  3859. {
  3860. // random song (long notes, swing, clips with offset); indexed scheduling must yield
  3861. // exactly the same events as the full scan, including in the block that wraps around
  3862. lynx::Song s = lynx::Song::makeDefault();
  3863. s.swing = 0.6f;
  3864. std::uint32_t seed = 12345;
  3865. auto rnd = [&] (int n) { seed = seed * 1664525u + 1013904223u; return (int) ((seed >> 8) % (std::uint32_t) n); };
  3866. s.patterns.resize (3);
  3867. for (auto& p : s.patterns)
  3868. {
  3869. p.notes.clear();
  3870. for (int i = 0; i < 60; ++i)
  3871. {
  3872. lynx::Note n;
  3873. n.start = rnd (16 * lynx::ppq);
  3874. n.length = rnd (5) == 0 ? rnd (12 * lynx::ppq) + 1 : rnd (lynx::ppq) + 1;
  3875. n.channel = rnd (4);
  3876. n.key = 40 + rnd (40);
  3877. n.slide = rnd (20) == 0;
  3878. p.notes.push_back (n);
  3879. }
  3880. p.sortNotes();
  3881. }
  3882. s.playlist.clear();
  3883. for (int i = 0; i < 12; ++i)
  3884. {
  3885. lynx::Clip c;
  3886. c.pattern = rnd (3);
  3887. c.track = rnd (4);
  3888. c.start = rnd (40) * lynx::ppq;
  3889. c.length = (rnd (12) + 1) * lynx::ppq;
  3890. c.offset = rnd (4) * lynx::ppq;
  3891. s.playlist.push_back (c);
  3892. }
  3893. s.mode = lynx::PlayMode::song;
  3894. const auto index = lynx::makeScheduleIndex (s);
  3895. const auto tps = lynx::ticksPerSample (140.0, 48000.0);
  3896. const auto loop = (double) s.songLength();
  3897. int mismatches = 0, events = 0;
  3898. for (int block = 0; block < 400; ++block)
  3899. {
  3900. const auto start = block == 0 ? loop - 100.0 * tps : (double) rnd ((int) loop * 2);
  3901. const auto samples = 64 + rnd (24000); // up to ~1 beat per block
  3902. lynx::EventBuffer a, b;
  3903. lynx::scheduleSong (s, start, samples, tps, a, false, nullptr);
  3904. lynx::scheduleSong (s, start, samples, tps, b, false, &index);
  3905. events += a.size();
  3906. if (a.size() != b.size())
  3907. {
  3908. ++mismatches;
  3909. continue;
  3910. }
  3911. for (int i = 0; i < a.size(); ++i)
  3912. {
  3913. const auto& x = *(a.begin() + i);
  3914. const auto& y = *(b.begin() + i);
  3915. mismatches += x.sampleOffset != y.sampleOffset || x.type != y.type || x.key != y.key || x.channel != y.channel;
  3916. }
  3917. }
  3918. CHECK (events > 500);
  3919. CHECK (mismatches == 0);
  3920. }
  3921. TEST (mixer_track_eq_defaults_and_boost)
  3922. {
  3923. // FL defaults: 90 Hz, 1.5 kHz and 8 kHz; peak Q 0.632; shelf slope 0.607
  3924. lynx::TrackEq eq;
  3925. CHECK (std::abs (lynx::TrackEq::peakQ (eq.width[1]) - 0.632) < 0.01);
  3926. CHECK (std::abs (lynx::TrackEq::shelfSlope (eq.width[0]) - 0.607) < 0.01);
  3927. CHECK (! eq.active());
  3928. // +12 dB peak at 1.5 kHz on a track: a 1.5 kHz sine rises ~4x
  3929. lynx::Song song = lynx::Song::makeDefault();
  3930. song.mixer[1].eq.gainDb[1] = 12.0f;
  3931. for (auto& c : song.channels)
  3932. c.mixerTrack = 1;
  3933. song.channels[4].instrument = lynx::InstrumentKind::synth;
  3934. song.channels[4].synth = {};
  3935. song.channels[4].synth.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  3936. song.channels[4].synth.cutoff = 20000.0f;
  3937. auto render = [] (const lynx::Song& s) {
  3938. auto state = lynx::RackState::build (s, nullptr, 48000.0);
  3939. lynx::RackRenderer renderer;
  3940. renderer.prepare (48000.0);
  3941. lynx::LiveEventQueue live;
  3942. lynx::NoteEvent on; // 1.5 kHz ~ key 90.6: use 91 (1568 Hz), still within the wide peak
  3943. on.channel = 4;
  3944. on.key = 91;
  3945. on.velocity = 1.0f;
  3946. live.push (on);
  3947. std::vector<float> l (24000), r (24000);
  3948. for (int pos = 0; pos < 24000; pos += 480)
  3949. renderer.process (state.get(), false, 0.0, 480, l.data() + pos, r.data() + pos, pos == 0 ? &live : nullptr);
  3950. float peak = 0.0f;
  3951. for (std::size_t i = 12000; i < 24000; ++i)
  3952. peak = std::max (peak, std::abs (l[i]));
  3953. return peak;
  3954. };
  3955. const auto boosted = render (song);
  3956. song.mixer[1].eq.gainDb[1] = 0.0f;
  3957. const auto flat = render (song);
  3958. CHECK (flat > 0.01f);
  3959. CHECK (boosted / flat > 3.0f && boosted / flat < 4.2f);
  3960. }
  3961. TEST (bars_follow_time_signature_markers)
  3962. {
  3963. lynx::Song song;
  3964. song.beatsPerBar = 4;
  3965. const auto bar4 = (std::int64_t) lynx::ppq * 4;
  3966. auto bars = song.barsBetween (0, bar4 * 3);
  3967. CHECK (bars.size() == 3 && bars[2].tick == bar4 * 2 && bars[2].number == 3);
  3968. // 7/4 from bar 3, and 3/4 in the middle of a 7 bar (cuts and restarts)
  3969. song.addMarker ({ bar4 * 2, "Verso", 7, 4 });
  3970. song.addMarker ({ bar4, "Nome só", 0, 4 });
  3971. const auto seven = (std::int64_t) lynx::ppq * 7;
  3972. song.addMarker ({ bar4 * 2 + seven + lynx::ppq * 2, "", 3, 4 });
  3973. bars = song.barsBetween (0, bar4 * 2 + seven * 2);
  3974. CHECK (bars.size() >= 5);
  3975. CHECK (bars[1].length == bar4 && bars[2].tick == bar4 * 2 && bars[2].length == seven);
  3976. CHECK (bars[3].length == lynx::ppq * 2); // cut short by the change
  3977. CHECK (bars[4].length == lynx::ppq * 3 && bars[4].number == 5);
  3978. CHECK (song.markers()[0].name == "Nome só"); // sorted by tick
  3979. // from far away: starts at the right bar without losing the numbering
  3980. const auto far = song.barsBetween (lynx::ppq * 3000, lynx::ppq * 3001);
  3981. CHECK (far.size() == 1 && far[0].length == lynx::ppq * 3);
  3982. CHECK (song.barAt (lynx::ppq * 3000).number == far[0].number);
  3983. CHECK ((lynx::ppq * 3000 - far[0].tick) < far[0].length);
  3984. }
  3985. TEST (split_drum_channel_moves_notes_to_pads)
  3986. {
  3987. lynx::Song song = lynx::Song::makeDefault();
  3988. const auto channels = (int) song.channels.size();
  3989. song.patterns[0].notes.clear();
  3990. song.patterns[0].notes.push_back ({ 0, 240, 1, 36, 0.9f }); // kit kick (channel 1)
  3991. song.patterns[0].notes.push_back ({ 240, 240, 1, 38, 0.8f }); // snare
  3992. song.patterns[0].notes.push_back ({ 480, 240, 1, 50, 0.8f }); // key without a pad: dropped
  3993. song.patterns[0].notes.push_back ({ 480, 240, 3, 60, 0.8f }); // other channel: shifts by 1 position
  3994. lynx::Automation a;
  3995. a.target = { lynx::AutomationTargetKind::channelVolume, 3, 0, 0 };
  3996. song.automations.push_back (a);
  3997. std::vector<lynx::DrumPad> pads (2);
  3998. pads[0].name = "Kick";
  3999. pads[0].key = 36;
  4000. pads[0].layers = { { "kick.wav" } };
  4001. pads[1].name = "Snare";
  4002. pads[1].key = 38;
  4003. pads[1].layers = { { "snare soft.wav", 60, 0, 127, 0, 90 }, { "snare hard.wav", 60, 0, 127, 91, 127 } };
  4004. pads[1].cutGroup = 2;
  4005. pads[1].cutBy = 2;
  4006. lynx::splitDrumChannel (song, 1, pads);
  4007. CHECK ((int) song.channels.size() == channels + 1);
  4008. CHECK (song.channels[1].samplePath == "kick.wav" && song.channels[1].instrument == lynx::InstrumentKind::sampler);
  4009. CHECK (song.channels[2].zones.size() == 2 && song.channels[2].cutGroup == song.channels[2].cutBy && song.channels[2].cutGroup != 0);
  4010. const auto& notes = song.patterns[0].notes;
  4011. CHECK (notes.size() == 3);
  4012. CHECK (notes[0].channel == 1 && notes[0].key == 60);
  4013. CHECK (notes[1].channel == 2 && notes[1].key == 60);
  4014. CHECK (notes[2].channel == 4); // the old channel 3
  4015. CHECK (song.automations[0].target.index == 4);
  4016. }
  4017. TEST (flp_import_brings_playlist)
  4018. {
  4019. flp::Project p;
  4020. p.header.ppq = 96;
  4021. p.channels = { { 0, flp::ChannelType::sampler, "Kick", "", "", 1 } };
  4022. flp::Pattern empty;
  4023. empty.index = 1;
  4024. flp::Pattern full;
  4025. full.index = 2;
  4026. flp::Note n;
  4027. n.channel = 0;
  4028. full.notes.push_back (n);
  4029. p.patterns = { empty, full };
  4030. flp::Arrangement a;
  4031. flp::PlaylistItem item;
  4032. item.kind = flp::PlaylistItem::Kind::pattern;
  4033. item.index = 2;
  4034. item.position = 384;
  4035. item.length = 192;
  4036. item.track = 3;
  4037. item.patternStartTick = 96;
  4038. a.items.push_back (item);
  4039. item.index = 1; // empty pattern: discarded
  4040. a.items.push_back (item);
  4041. item.kind = flp::PlaylistItem::Kind::channelClip; // audio: not supported yet
  4042. a.items.push_back (item);
  4043. p.arrangements.push_back (a);
  4044. const auto s = lynx::importFlp (p);
  4045. CHECK (s.mode == lynx::PlayMode::song);
  4046. CHECK (s.playlist.size() == 1);
  4047. CHECK (s.playlist[0].pattern == 0 && s.playlist[0].track == 3);
  4048. CHECK (s.playlist[0].start == 4 * lynx::ppq && s.playlist[0].length == 2 * lynx::ppq && s.playlist[0].offset == lynx::ppq);
  4049. }
  4050. TEST (rack_releases_notes_on_position_jump)
  4051. {
  4052. lynx::Song s;
  4053. lynx::ChannelDef c;
  4054. c.instrument = lynx::InstrumentKind::synth;
  4055. s.channels.push_back (c);
  4056. auto& p = s.patterns.emplace_back();
  4057. lynx::Note n;
  4058. n.start = 0;
  4059. n.length = 4 * lynx::ppq; // long note
  4060. p.notes.push_back (n);
  4061. s.bpm = 120.0;
  4062. auto state = lynx::RackState::build (s, nullptr, 48000.0);
  4063. lynx::RackRenderer r;
  4064. r.prepare (48000.0);
  4065. std::vector<float> l (512), rr (512);
  4066. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  4067. double tick = 0.0;
  4068. for (int i = 0; i < 20; ++i, tick += 512 * tps)
  4069. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  4070. // jump to tick 2000 (mid-note, no new "on"): the synth must go silent
  4071. for (int i = 0; i < 40; ++i)
  4072. r.process (state.get(), true, 2000.0 + i * 512 * tps, 512, l.data(), rr.data());
  4073. float tail = 0.0f;
  4074. for (auto v : l)
  4075. tail = std::max (tail, std::abs (v));
  4076. CHECK (tail < 0.001f);
  4077. }
  4078. namespace
  4079. {
  4080. // Two samplers with a constant 0.5 signal on different inserts; returns the output peak.
  4081. float juce_db (float gain)
  4082. {
  4083. return 20.0f * std::log10 (gain);
  4084. }
  4085. float renderMixerCase (const std::function<void (lynx::Song&)>& tweak, lynx::MeterBank* meters = nullptr)
  4086. {
  4087. lynx::Song s;
  4088. s.mixer = lynx::Song::defaultMixer (4);
  4089. for (int i = 0; i < 2; ++i)
  4090. {
  4091. lynx::ChannelDef c;
  4092. c.instrument = lynx::InstrumentKind::sampler;
  4093. c.samplePath = "dc";
  4094. c.volume = 1.0f;
  4095. c.mixerTrack = i + 1;
  4096. s.channels.push_back (c);
  4097. }
  4098. s.patterns.emplace_back();
  4099. tweak (s);
  4100. auto provider = [] (const std::string&) {
  4101. auto d = std::make_shared<lynx::SampleData>();
  4102. d->left.assign (48000, 0.5f);
  4103. return std::shared_ptr<const lynx::SampleData> (d);
  4104. };
  4105. auto state = lynx::RackState::build (s, nullptr, 48000.0, provider);
  4106. lynx::RackRenderer r;
  4107. r.prepare (48000.0, 256);
  4108. lynx::LiveEventQueue live;
  4109. for (int ch = 0; ch < 2; ++ch)
  4110. {
  4111. lynx::NoteEvent on;
  4112. on.channel = ch;
  4113. on.velocity = 1.0f;
  4114. live.push (on);
  4115. }
  4116. std::vector<float> l (1000), rr (1000); // larger than the mixer block: tests the chunking
  4117. r.process (state.get(), false, 0.0, 1000, l.data(), rr.data(), &live, meters);
  4118. return std::max (std::abs (l[999]), std::abs (rr[999]));
  4119. }
  4120. } // namespace
  4121. TEST (mixer_routes_mutes_solos_and_meters)
  4122. {
  4123. // two 0.5 channels summed on the Master at 0 dB
  4124. CHECK (std::abs (renderMixerCase ([] (lynx::Song&) {}) - 1.0f) < 1e-4f);
  4125. // insert 1 muted: only channel 2 remains
  4126. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.mixer[1].muted = true; }) - 0.5f) < 1e-4f);
  4127. // solo on insert 2: same
  4128. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.mixer[2].solo = true; }) - 0.5f) < 1e-4f);
  4129. // FL fader curve: 0.8 = 0 dB, max ≈ +5.6 dB, half travel ≈ -12.7 dB
  4130. CHECK (std::abs (lynx::faderGain (0.8f) - 1.0f) < 1e-5f);
  4131. CHECK (std::abs (juce_db (lynx::faderGain (1.0f)) - 5.59f) < 0.05f);
  4132. CHECK (lynx::faderGain (0.0f) == 0.0f);
  4133. // Master fader at max
  4134. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.mixer[0].volume = 1.0f; }) - lynx::faderGain (1.0f)) < 1e-4f);
  4135. // both channels on the same insert, insert fader at 0.4
  4136. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) {
  4137. s.channels[1].mixerTrack = 1;
  4138. s.mixer[1].volume = 0.4f;
  4139. }) - lynx::faderGain (0.4f)) < 1e-4f);
  4140. // a route outside the mixer falls to the last existing insert, without crashing
  4141. CHECK (renderMixerCase ([] (lynx::Song& s) { s.channels[0].mixerTrack = 100; }) > 0.9f);
  4142. // no mixer: everything straight to the output at 0 dB
  4143. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.mixer.clear(); }) - 1.0f) < 1e-4f);
  4144. lynx::MeterBank meters;
  4145. renderMixerCase ([] (lynx::Song&) {}, &meters);
  4146. // (the 24-point sinc's ringing at the sample start: its x87 kernel table rounds differently on ARM64)
  4147. CHECK (std::abs (meters.takePeak (1, 0) - 0.5f) < armTolerance (1e-4f, 2e-4f));
  4148. CHECK (std::abs (meters.takePeak (0, 1) - 1.0f) < armTolerance (1e-4f, 4e-4f));
  4149. CHECK (meters.takePeak (1, 0) == 0.0f); // reading resets
  4150. CHECK (meters.takePeak (3, 0) == 0.0f); // insert with no channel
  4151. }
  4152. TEST (next_free_insert_and_flp_mixer_import)
  4153. {
  4154. auto s = lynx::Song::makeDefault();
  4155. CHECK (s.mixer.size() == 17 && s.mixer[0].name == "Master");
  4156. CHECK (s.nextFreeInsert() == 6); // the 5 default channels take 1..5
  4157. flp::Project p;
  4158. p.channels = { { 0, flp::ChannelType::sampler, "Kick", "", "", 30 } };
  4159. p.mixer = { { 0, "" }, { 30, "Drums" } };
  4160. const auto song = lynx::importFlp (p);
  4161. CHECK (song.channels[0].mixerTrack == 30);
  4162. CHECK (song.mixer.size() == 31);
  4163. CHECK (song.mixer[30].name == "Drums" && song.mixer[0].name == "Master");
  4164. }
  4165. namespace
  4166. {
  4167. float runEffectOnSine (lynx::EffectKind kind, const std::function<void (lynx::EffectSlot&)>& tweak, double hz, float amp,
  4168. int frames = 48000)
  4169. {
  4170. auto fx = lynx::makeEffect (kind);
  4171. fx->prepare (48000.0);
  4172. auto slot = lynx::makeEffectSlot (kind);
  4173. tweak (slot);
  4174. std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
  4175. for (int i = 0; i < frames; ++i)
  4176. l[(std::size_t) i] = r[(std::size_t) i] = amp * (float) std::sin (6.283185307 * hz * i / 48000.0);
  4177. for (int i = 0; i < frames; i += 480)
  4178. fx->process (l.data() + i, r.data() + i, std::min (480, frames - i), slot.params, 120.0);
  4179. float peak = 0.0f;
  4180. for (int i = frames / 2; i < frames; ++i) // second half: already settled
  4181. {
  4182. CHECK (std::isfinite (l[(std::size_t) i]));
  4183. peak = std::max (peak, std::abs (l[(std::size_t) i]));
  4184. }
  4185. return peak;
  4186. }
  4187. } // namespace
  4188. TEST (parametric_eq_bands_and_response)
  4189. {
  4190. // default: transparent
  4191. auto slot = lynx::makeEffectSlot (lynx::EffectKind::parametricEq);
  4192. CHECK (std::abs (lynx::peq::responseDb (slot.params, 48000.0, 1000.0)) < 0.01);
  4193. CHECK (std::abs (runEffectOnSine (lynx::EffectKind::parametricEq, [] (auto&) {}, 1000.0, 0.5f) - 0.5f) < 0.005f);
  4194. // +12 dB peak at 1 kHz on band 4: the response and the signal agree
  4195. auto boost = [] (lynx::EffectSlot& s) {
  4196. s.params[(std::size_t) lynx::peq::param (3, 1)] = lynx::peq::hzValue (1000.0);
  4197. s.params[(std::size_t) lynx::peq::param (3, 2)] = lynx::peq::gainValue (12.0);
  4198. };
  4199. boost (slot);
  4200. CHECK (std::abs (lynx::peq::responseDb (slot.params, 48000.0, 1000.0) - 12.0) < 0.2);
  4201. CHECK (std::abs (lynx::peq::responseDb (slot.params, 48000.0, 100.0)) < 1.0);
  4202. const auto peak = runEffectOnSine (lynx::EffectKind::parametricEq, boost, 1000.0, 0.1f);
  4203. CHECK (std::abs (peak - 0.1f * std::pow (10.0f, 12.0f / 20.0f)) < 0.01f);
  4204. // low cut at 48 dB/oct at 200 Hz: 50 Hz (2 octaves below) drops ~96 dB; 2 kHz passes
  4205. auto cut = lynx::makeEffectSlot (lynx::EffectKind::parametricEq);
  4206. cut.params[(std::size_t) lynx::peq::param (0, 0)] = lynx::peq::typeValue (lynx::peq::BandType::lowCut);
  4207. cut.params[(std::size_t) lynx::peq::param (0, 1)] = lynx::peq::hzValue (200.0);
  4208. cut.params[(std::size_t) lynx::peq::param (0, 3)] = lynx::peq::qValue (0.70710678); // Butterworth
  4209. cut.params[(std::size_t) (lynx::peq::slopeParam + 0)] = 1.0f;
  4210. CHECK (lynx::peq::responseDb (cut.params, 48000.0, 50.0) < -80.0);
  4211. CHECK (std::abs (lynx::peq::responseDb (cut.params, 48000.0, 200.0) + 3.0) < 0.5); // -3 dB at the cutoff (Butterworth)
  4212. CHECK (std::abs (lynx::peq::responseDb (cut.params, 48000.0, 2000.0)) < 0.1);
  4213. // round-trip conversions
  4214. CHECK (std::abs (lynx::peq::hz (lynx::peq::hzValue (440.0)) - 440.0) < 0.5);
  4215. CHECK (std::abs (lynx::peq::q (lynx::peq::qValue (2.5)) - 2.5) < 0.01);
  4216. CHECK (lynx::peq::type (lynx::peq::typeValue (lynx::peq::BandType::notch)) == lynx::peq::BandType::notch);
  4217. }
  4218. TEST (fl_parametric_eq2_state_maps_to_bands)
  4219. {
  4220. // v8 blob: band 1 low cut at 100 Hz "Steep 8", band 4 peak +6 dB at 1 kHz, output −3 dB
  4221. std::vector<std::uint8_t> blob (354, 0);
  4222. auto put = [&] (std::size_t off, std::int32_t v) { std::memcpy (blob.data() + off, &v, 4); };
  4223. put (0, 8);
  4224. auto param = [&] (int i, std::int32_t v) { put (4 + 4 * (std::size_t) i, v); };
  4225. auto hzRaw = [] (double hz) { return (std::int32_t) std::lround (std::log (hz / 20.0) / std::log (1000.0) * 65536.0); };
  4226. for (int b = 0; b < 7; ++b)
  4227. param (21 + b, 0); // all off
  4228. param (21 + 0, 3); // high pass (= low cut)
  4229. param (7 + 0, hzRaw (100.0));
  4230. param (14 + 0, 40124); // default width -> Q ~0.68
  4231. param (28 + 0, -3); // Steep 8 -> 4 biquads
  4232. param (21 + 3, 6); // peaking
  4233. param (7 + 3, hzRaw (1000.0));
  4234. param (3, 600); // +6 dB
  4235. param (14 + 3, 25412);
  4236. param (35, -300); // main level −3 dB
  4237. put (0x13E, 0x7F);
  4238. const auto slot = lynx::effectFromFl ("Fruity Parametric EQ 2", blob);
  4239. CHECK (slot.has_value() && slot->kind == lynx::EffectKind::parametricEq);
  4240. const auto& p = slot->params;
  4241. CHECK (lynx::peq::type (p[(std::size_t) lynx::peq::param (0, 0)]) == lynx::peq::BandType::lowCut);
  4242. CHECK (lynx::peq::flLaw (p) && lynx::peqfl::fromParams (p).order[0] == -3); // FL's own order (REA_rodada50)
  4243. CHECK (lynx::peq::type (p[(std::size_t) lynx::peq::param (3, 0)]) == lynx::peq::BandType::peak);
  4244. CHECK (lynx::peq::type (p[(std::size_t) lynx::peq::param (1, 0)]) == lynx::peq::BandType::off);
  4245. CHECK (std::abs (lynx::peq::responseDb (p, 48000.0, 1000.0) - 3.0) < 0.3); // +6 from the peak −3 from the output
  4246. CHECK (lynx::peq::responseDb (p, 48000.0, 30.0) < -40.0); // 48 dB/oct below 100 Hz
  4247. CHECK (std::abs (lynx::peq::responseDb (p, 48000.0, 8000.0) + 3.0) < 0.3);
  4248. // a band muted by the mask doesn't sound (FL's mask travels with the slot, REA_rodada50)
  4249. put (0x13E, 0x7F & ~(1 << 3));
  4250. const auto muted = lynx::effectFromFl ("Fruity Parametric EQ 2", blob);
  4251. CHECK ((lynx::peqfl::fromParams (muted->params).mask & (1u << 3)) == 0);
  4252. CHECK (std::abs (lynx::peq::responseDb (muted->params, 48000.0, 1000.0) + 3.0) < 0.3);
  4253. }
  4254. TEST (reverb_low_cut_and_predelay)
  4255. {
  4256. // impulse: with 100 ms predelay the wet only starts later; with a 3 kHz low cut a
  4257. // 100 Hz sine barely enters the reverb
  4258. auto run = [] (const std::function<void (lynx::EffectSlot&)>& tweak, double hz, bool impulse) {
  4259. auto fx = lynx::makeEffect (lynx::EffectKind::reverb);
  4260. fx->prepare (48000.0);
  4261. auto slot = lynx::makeEffectSlot (lynx::EffectKind::reverb);
  4262. slot.params[4] = 0.0f; // wet only
  4263. slot.params[3] = 1.0f;
  4264. tweak (slot);
  4265. std::vector<float> l (48000), r (48000);
  4266. for (int i = 0; i < 48000; ++i)
  4267. 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);
  4268. for (int i = 0; i < 48000; i += 480)
  4269. fx->process (l.data() + i, r.data() + i, 480, slot.params, 120.0);
  4270. return l;
  4271. };
  4272. const auto delayed = run ([] (auto& s) { s.params[7] = 0.1f; }, 0.0, true);
  4273. float early = 0.0f, later = 0.0f;
  4274. for (std::size_t i = 0; i < 4800; ++i)
  4275. early = std::max (early, std::abs (delayed[i]));
  4276. for (std::size_t i = 4800; i < 24000; ++i)
  4277. later = std::max (later, std::abs (delayed[i]));
  4278. CHECK (early == 0.0f && later > 0.0f);
  4279. 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; };
  4280. const auto open = energy (run ([] (auto&) {}, 100.0, false));
  4281. const auto cut = energy (run ([] (auto& s) { s.params[5] = 1.0f; }, 100.0, false));
  4282. CHECK (cut < open * 0.01);
  4283. }
  4284. TEST (utility_effect_and_fl_stereo_tools)
  4285. {
  4286. // transparent by default; balance fully right silences the left
  4287. CHECK (std::abs (runEffectOnSine (lynx::EffectKind::utility, [] (auto&) {}, 440.0, 0.5f) - 0.5f) < 0.001f);
  4288. CHECK (runEffectOnSine (lynx::EffectKind::utility, [] (auto& s) { s.params[1] = 1.0f; }, 440.0, 0.5f) < 0.001f);
  4289. // Stereo Shaper at half the knob range (6400): each gain = (3^0.5 − 1)/2 (FL's knob law, REA_rodada54 §2.2)
  4290. std::vector<std::uint8_t> blob (36, 0);
  4291. auto put = [&] (std::size_t off, std::int32_t v) { std::memcpy (blob.data() + off, &v, 4); };
  4292. put (4, 6400); put (8, 6400); put (12, 6400); put (16, 6400);
  4293. const auto slot = lynx::effectFromFl ("Fruity Stereo Shaper", blob);
  4294. CHECK (slot && slot->kind == lynx::EffectKind::stereoShaper);
  4295. auto shaperFx = lynx::makeEffect (*slot);
  4296. shaperFx->prepare (44100.0);
  4297. float zl = 0.0f, zr = 0.0f;
  4298. shaperFx->process (&zl, &zr, 0, slot->params, 120.0); // takes the controls
  4299. shaperFx->reset(); // FL's Flush: the gains jump to their targets
  4300. float l = 1.0f, r = 0.0f;
  4301. shaperFx->process (&l, &r, 1, slot->params, 120.0);
  4302. const auto half = (float) ((std::sqrt (3.0) - 1.0) / 2.0);
  4303. CHECK (std::abs (l - half) < 1e-4f && std::abs (r - half) < 1e-4f);
  4304. auto fx = lynx::makeEffect (lynx::EffectKind::utility);
  4305. // Balance: volume 256 = 0 dB, pan −128 = fully left
  4306. std::vector<std::uint8_t> bal (8, 0);
  4307. std::int32_t pan = -128, vol = 256;
  4308. std::memcpy (bal.data(), &pan, 4);
  4309. std::memcpy (bal.data() + 4, &vol, 4);
  4310. const auto b = lynx::effectFromFl ("Fruity Balance", bal);
  4311. CHECK (b && b->kind == lynx::EffectKind::fruityBalance); // FL's exact law (REA_rodada58 §2): fully left sums R into L
  4312. if (b)
  4313. {
  4314. auto balance = lynx::makeEffect (*b);
  4315. balance->prepare (44100.0);
  4316. l = 0.5f; r = 0.5f;
  4317. balance->process (&l, &r, 1, b->params, 120.0);
  4318. CHECK (std::abs (l - 0.70710678f) < 1e-6f && r == 0.0f);
  4319. }
  4320. }
  4321. TEST (effects_eq_compressor_behave)
  4322. {
  4323. // EQ at default is transparent
  4324. CHECK (std::abs (runEffectOnSine (lynx::EffectKind::eq, [] (auto&) {}, 1000.0, 0.5f) - 0.5f) < 0.01f);
  4325. // +18 dB on the lows: 60 Hz rises a lot, 5 kHz barely
  4326. CHECK (runEffectOnSine (lynx::EffectKind::eq, [] (auto& s) { s.params[0] = 1.0f; }, 60.0, 0.05f) > 0.3f);
  4327. CHECK (runEffectOnSine (lynx::EffectKind::eq, [] (auto& s) { s.params[0] = 1.0f; }, 5000.0, 0.05f) < 0.06f);
  4328. // compressor: threshold -20 dB, ratio 20:1 flattens a 0 dBFS signal to near the threshold
  4329. const auto squashed = runEffectOnSine (lynx::EffectKind::compressor, [] (auto& s) {
  4330. s.params[0] = 40.0f / 60.0f;
  4331. s.params[1] = 1.0f;
  4332. }, 200.0, 1.0f);
  4333. CHECK (squashed < 0.2f && squashed > 0.05f);
  4334. // a signal below the threshold passes intact
  4335. CHECK (std::abs (runEffectOnSine (lynx::EffectKind::compressor, [] (auto& s) { s.params[0] = 1.0f; }, 200.0, 0.3f) - 0.3f) < 0.01f);
  4336. }
  4337. TEST (effects_delay_echoes_on_tempo_and_reverb_has_tail)
  4338. {
  4339. auto delay = lynx::makeEffect (lynx::EffectKind::delay);
  4340. delay->prepare (48000.0);
  4341. auto slot = lynx::makeEffectSlot (lynx::EffectKind::delay);
  4342. slot.params[0] = 3.5f / 8.0f; // index 3 = 1/4 = 0.5 s at 120 BPM
  4343. slot.params[1] = 0.0f; // no feedback: a single echo
  4344. slot.params[2] = 1.0f;
  4345. slot.params[4] = 0.0f; // no damping
  4346. std::vector<float> l (48000, 0.0f), r (48000, 0.0f);
  4347. l[0] = r[0] = 1.0f;
  4348. delay->process (l.data(), r.data(), 48000, slot.params, 120.0);
  4349. CHECK (std::abs (l[24000] - 1.0f) < 1e-4f);
  4350. CHECK (std::abs (l[12000]) < 1e-6f);
  4351. auto reverb = lynx::makeEffect (lynx::EffectKind::reverb);
  4352. reverb->prepare (48000.0);
  4353. auto rs = lynx::makeEffectSlot (lynx::EffectKind::reverb);
  4354. rs.params[3] = 1.0f;
  4355. std::fill (l.begin(), l.end(), 0.0f);
  4356. std::fill (r.begin(), r.end(), 0.0f);
  4357. l[0] = r[0] = 1.0f;
  4358. reverb->process (l.data(), r.data(), 48000, rs.params, 120.0);
  4359. float tail = 0.0f;
  4360. for (int i = 4800; i < 9600; ++i) // between 100 and 200 ms after the impulse
  4361. tail = std::max (tail, std::abs (l[(std::size_t) i]));
  4362. CHECK (tail > 1e-4f && tail < 1.0f);
  4363. CHECK (lynx::formatEffectParam (lynx::EffectKind::delay, 0, slot.params[0], 120.0) == "1/4 (500 ms)");
  4364. }
  4365. TEST (rack_runs_insert_effects_and_reuses_them)
  4366. {
  4367. // an insert with EQ at -inf doesn't exist; we use the EQ output at -18 dB to see the effect act
  4368. const auto quiet = renderMixerCase ([] (lynx::Song& s) {
  4369. auto eq = lynx::makeEffectSlot (lynx::EffectKind::eq);
  4370. eq.params[4] = 0.0f; // output -18 dB
  4371. s.mixer[1].effects.push_back (eq);
  4372. });
  4373. CHECK (quiet > 0.55f && quiet < 0.6f); // 0.5 from channel 2 + 0.5 * 0.126 from channel 1
  4374. const auto bypassed = renderMixerCase ([] (lynx::Song& s) {
  4375. auto eq = lynx::makeEffectSlot (lynx::EffectKind::eq);
  4376. eq.params[4] = 0.0f;
  4377. eq.enabled = false;
  4378. s.mixer[1].effects.push_back (eq);
  4379. });
  4380. CHECK (std::abs (bypassed - 1.0f) < 1e-4f);
  4381. auto s = lynx::Song::makeDefault();
  4382. s.mixer[2].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::reverb));
  4383. auto a = lynx::RackState::build (s, nullptr, 48000.0);
  4384. s.mixer[2].effects[0].params[0] = 0.9f; // turning a knob doesn't recreate the effect
  4385. auto b = lynx::RackState::build (s, a.get(), 48000.0);
  4386. CHECK (b->effects[2][0] == a->effects[2][0]);
  4387. s.mixer[2].effects[0] = lynx::makeEffectSlot (lynx::EffectKind::delay);
  4388. auto c = lynx::RackState::build (s, b.get(), 48000.0);
  4389. CHECK (c->effects[2][0] != b->effects[2][0]);
  4390. }
  4391. TEST (audio_clips_trigger_cut_and_resume)
  4392. {
  4393. lynx::Song s;
  4394. s.bpm = 120.0; // 1 beat = 0.5 s = 24000 samples at 48 kHz
  4395. lynx::ChannelDef c;
  4396. c.instrument = lynx::InstrumentKind::audioClip;
  4397. c.samplePath = "ramp";
  4398. c.volume = 1.0f;
  4399. s.channels.push_back (c);
  4400. s.patterns.emplace_back();
  4401. lynx::Clip clip;
  4402. clip.kind = lynx::ClipKind::audio;
  4403. clip.channel = 0;
  4404. clip.start = lynx::ppq; // starts at beat 2
  4405. clip.length = 2 * lynx::ppq; // lasts 2 beats
  4406. clip.offset = lynx::ppq; // skips the first beat of the audio
  4407. s.playlist.push_back (clip);
  4408. // an empty pattern clip over the whole bar: the song ends at beat 4 (FL loops at the last clip's exact end)
  4409. s.playlist.push_back (lynx::Clip { 0, 1, 0, 4 * lynx::ppq, 0 });
  4410. s.mode = lynx::PlayMode::song;
  4411. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  4412. lynx::EventBuffer ev;
  4413. lynx::scheduleSong (s, 0.0, 4 * 24000, tps, ev);
  4414. CHECK (ev.size() == 2);
  4415. CHECK (ev[0].type == lynx::NoteEvent::Type::on && ev[0].sampleOffset == 24000 && std::abs (ev[0].startSeconds - 0.5) < 1e-9);
  4416. CHECK (ev[1].type == lynx::NoteEvent::Type::off && ev[1].sampleOffset == 3 * 24000);
  4417. // seek to the middle of the clip (beat 2.5): with resume, starts at the block's first sample
  4418. ev.clear();
  4419. lynx::scheduleSong (s, 1.5 * lynx::ppq, 256, tps, ev, true);
  4420. CHECK (ev.size() == 1 && ev[0].sampleOffset == 0 && std::abs (ev[0].startSeconds - 0.75) < 1e-9);
  4421. ev.clear();
  4422. lynx::scheduleSong (s, 1.5 * lynx::ppq, 256, tps, ev, false);
  4423. CHECK (ev.size() == 0);
  4424. // sample whose value = position in seconds: lets us check where playback started
  4425. auto provider = [] (const std::string&) {
  4426. auto d = std::make_shared<lynx::SampleData>();
  4427. d->sampleRate = 48000.0;
  4428. for (int i = 0; i < 48000 * 3; ++i)
  4429. d->left.push_back ((float) i / 48000.0f);
  4430. return std::shared_ptr<const lynx::SampleData> (d);
  4431. };
  4432. auto state = lynx::RackState::build (s, nullptr, 48000.0, provider);
  4433. lynx::RackRenderer r;
  4434. r.prepare (48000.0, 512);
  4435. std::vector<float> l (256), rr (256);
  4436. // first block already playing mid-clip
  4437. r.process (state.get(), true, 1.5 * lynx::ppq, 256, l.data(), rr.data());
  4438. const auto unity = 1.0f / lynx::channelGain (10000.0f / 12800.0f); // clip level at 100% channel volume (REA_rodada89 §4.2)
  4439. CHECK (std::abs (l[0] - 0.75f * unity) < 0.002f);
  4440. // the clip ends at beat 3: after the short fade, silence (the sample would still have audio)
  4441. double tick = 3.0 * lynx::ppq - 10 * tps;
  4442. r.process (state.get(), false, tick, 256, l.data(), rr.data()); // stops (fade)
  4443. for (int i = 0; i < 4; ++i)
  4444. r.process (state.get(), false, tick, 256, l.data(), rr.data());
  4445. CHECK (std::abs (l[255]) < 1e-6f);
  4446. }
  4447. TEST (remove_channel_fixes_notes_and_audio_clips)
  4448. {
  4449. lynx::Song s;
  4450. s.channels.resize (3);
  4451. s.patterns.resize (1);
  4452. s.patterns[0].notes = { { 0, 240, 0, 60, 1.0f }, { 0, 240, 1, 60, 1.0f }, { 0, 240, 2, 60, 1.0f } };
  4453. lynx::Clip a;
  4454. a.kind = lynx::ClipKind::audio;
  4455. a.channel = 1;
  4456. a.length = 100;
  4457. lynx::Clip b = a;
  4458. b.channel = 2;
  4459. s.playlist = { a, b };
  4460. s.removeChannel (1);
  4461. CHECK (s.channels.size() == 2);
  4462. CHECK (s.patterns[0].notes.size() == 2 && s.patterns[0].notes[1].channel == 1);
  4463. CHECK (s.playlist.size() == 1 && s.playlist[0].channel == 1);
  4464. }
  4465. TEST (flp_import_audio_clips_with_offsets)
  4466. {
  4467. flp::Project p;
  4468. p.header.ppq = 96;
  4469. p.tempo = 120.0;
  4470. p.channels = { { 0, flp::ChannelType::audioClip, "Vocal", "", "%FLStudioData%\\v.wav", 2 },
  4471. { 1, flp::ChannelType::audioClip, "Loop", "", "%FLStudioData%\\l.wav", 3 } };
  4472. flp::Arrangement arr;
  4473. flp::PlaylistItem beats; // offset in beats: end - start = duration in beats
  4474. beats.kind = flp::PlaylistItem::Kind::channelClip;
  4475. beats.index = 0;
  4476. beats.position = 192;
  4477. beats.length = 384; // 4 beats
  4478. beats.startOffset = 2.0f;
  4479. beats.endOffset = 6.0f;
  4480. flp::PlaylistItem ms = beats; // offset in ms: 4 beats at 120 BPM = 2000 ms
  4481. ms.index = 1;
  4482. ms.startOffset = 500.0f;
  4483. ms.endOffset = 2500.0f;
  4484. arr.items = { beats, ms };
  4485. p.arrangements.push_back (arr);
  4486. lynx::FlpImportOptions options;
  4487. options.roots = { "C:\\FL", "C:\\FL\\Data", "" };
  4488. options.fileExists = [] (const std::string&) { return true; };
  4489. const auto s = lynx::importFlp (p, options);
  4490. CHECK (s.channels.size() == 2 && s.channels[0].instrument == lynx::InstrumentKind::audioClip);
  4491. CHECK (s.channels[0].samplePath == "C:\\FL\\Data\\v.wav");
  4492. CHECK (s.playlist.size() == 2);
  4493. CHECK (s.playlist[0].kind == lynx::ClipKind::audio && s.playlist[0].start == 2 * lynx::ppq);
  4494. CHECK (s.playlist[0].offset == 2 * lynx::ppq);
  4495. CHECK (s.playlist[1].offset == lynx::ppq); // 500 ms at 120 BPM = 1 beat
  4496. CHECK (s.mode == lynx::PlayMode::song);
  4497. }
  4498. TEST (plugin_factories_create_reuse_and_fallback)
  4499. {
  4500. lynx::Song s;
  4501. s.mixer = lynx::Song::defaultMixer (2);
  4502. lynx::ChannelDef c;
  4503. c.instrument = lynx::InstrumentKind::plugin;
  4504. c.plugin.id = "VST3-Synth-123";
  4505. c.mixerTrack = 1;
  4506. s.channels.push_back (c);
  4507. lynx::ChannelDef missing = c;
  4508. missing.plugin.id = "VST3-Ausente-0";
  4509. s.channels.push_back (missing);
  4510. lynx::EffectSlot fx;
  4511. fx.kind = lynx::EffectKind::plugin;
  4512. fx.plugin.id = "VST3-Reverb-9";
  4513. s.mixer[1].effects.push_back (fx);
  4514. s.patterns.emplace_back();
  4515. int instrumentsMade = 0, effectsMade = 0;
  4516. lynx::RackState::Factories f;
  4517. f.pluginInstruments = [&] (const lynx::PluginRef& ref, double) -> std::shared_ptr<lynx::Instrument> {
  4518. if (ref.id != "VST3-Synth-123")
  4519. return nullptr;
  4520. ++instrumentsMade;
  4521. auto synth = lynx::makeInstrument (lynx::InstrumentKind::synth);
  4522. synth->prepare (48000.0);
  4523. return synth;
  4524. };
  4525. f.pluginEffects = [&] (const lynx::PluginRef&, double) -> std::shared_ptr<lynx::Effect> {
  4526. ++effectsMade;
  4527. auto eq = lynx::makeEffect (lynx::EffectKind::eq);
  4528. eq->prepare (48000.0);
  4529. return eq;
  4530. };
  4531. auto a = lynx::RackState::build (s, nullptr, 48000.0, f);
  4532. CHECK (instrumentsMade == 1 && effectsMade == 1);
  4533. CHECK (a->instruments.size() == 2 && a->instruments[1] != nullptr); // missing: muted, but it exists
  4534. // Default startup keeps the established reuse contract: saving/capturing
  4535. // different serialized bytes by itself must not recreate hosted instances.
  4536. auto storedOnly = s;
  4537. storedOnly.channels[0].plugin.state = "captured instrument without opt-in";
  4538. storedOnly.mixer[1].effects[0].plugin.state = "captured effect without opt-in";
  4539. auto ordinary = lynx::RackState::build (storedOnly, a.get(), 48000.0, f);
  4540. CHECK (instrumentsMade == 1 && effectsMade == 1);
  4541. CHECK (ordinary->instruments[0] == a->instruments[0] && ordinary->effects[1][0] == a->effects[1][0]);
  4542. // Serialized state changes and undo preserve running instances. Remote control
  4543. // applies deliberate state edits in place through the engine, outside this factory.
  4544. s.channels[0].plugin.state = "abc";
  4545. auto b = lynx::RackState::build (s, a.get(), 48000.0, f);
  4546. CHECK (instrumentsMade == 1 && b->instruments[0] == a->instruments[0] && b->effects[1][0] == a->effects[1][0]);
  4547. s.mixer[1].effects[0].plugin.state = "new effect state";
  4548. auto changed = lynx::RackState::build (s, b.get(), 48000.0, f);
  4549. CHECK (instrumentsMade == 1 && effectsMade == 1 && changed->effects[1][0] == b->effects[1][0]);
  4550. s.channels[0].plugin.state.clear();
  4551. s.mixer[1].effects[0].plugin.state.clear();
  4552. auto undone = lynx::RackState::build (s, changed.get(), 48000.0, f);
  4553. CHECK (instrumentsMade == 1 && effectsMade == 1);
  4554. CHECK (undone->instruments[0] == changed->instruments[0] && undone->effects[1][0] == changed->effects[1][0]);
  4555. auto capturedShape = *undone;
  4556. capturedShape.song.channels[0].plugin.state = s.channels[0].plugin.state = "captured instrument";
  4557. capturedShape.song.mixer[1].effects[0].plugin.state = s.mixer[1].effects[0].plugin.state = "captured effect";
  4558. auto captured = lynx::RackState::build (s, &capturedShape, 48000.0, f);
  4559. CHECK (instrumentsMade == 1 && effectsMade == 1);
  4560. CHECK (captured->instruments[0] == undone->instruments[0] && captured->effects[1][0] == undone->effects[1][0]);
  4561. s.mixer[1].effects[0].plugin.id = "VST3-Outro-1";
  4562. auto c2 = lynx::RackState::build (s, captured.get(), 48000.0, f);
  4563. CHECK (effectsMade == 2);
  4564. // rendering with a missing plugin and a plugin slot breaks nothing
  4565. lynx::RackRenderer r;
  4566. r.prepare (48000.0, 256);
  4567. lynx::LiveEventQueue live;
  4568. lynx::NoteEvent on;
  4569. on.channel = 0;
  4570. on.velocity = 1.0f;
  4571. live.push (on);
  4572. on.channel = 1;
  4573. live.push (on);
  4574. std::vector<float> l (256), rr (256);
  4575. r.process (c2.get(), false, 0.0, 256, l.data(), rr.data(), &live);
  4576. float peak = 0.0f;
  4577. for (auto v : l)
  4578. {
  4579. CHECK (std::isfinite (v));
  4580. peak = std::max (peak, std::abs (v));
  4581. }
  4582. CHECK (peak > 0.0f);
  4583. }
  4584. TEST (remote_reset_replaces_only_requested_instances_and_default_reuse_stays_unchanged)
  4585. {
  4586. lynx::Song source;
  4587. source.mixer = lynx::Song::defaultMixer (2);
  4588. for (const auto* id : { "Synth A", "Synth B" })
  4589. {
  4590. lynx::ChannelDef channel;
  4591. channel.instrument = lynx::InstrumentKind::plugin;
  4592. channel.plugin.id = id;
  4593. channel.plugin.state = std::string ("saved ") + id;
  4594. source.channels.push_back (std::move (channel));
  4595. }
  4596. for (const auto* id : { "Effect A", "Effect B" })
  4597. {
  4598. lynx::EffectSlot effect;
  4599. effect.kind = lynx::EffectKind::plugin;
  4600. effect.plugin.id = id;
  4601. effect.plugin.state = std::string ("saved ") + id;
  4602. source.mixer[1].effects.push_back (std::move (effect));
  4603. }
  4604. int instrumentsMade = 0, effectsMade = 0;
  4605. std::string lastInstrumentState, lastEffectState;
  4606. lynx::RackState::Factories normal;
  4607. normal.pluginInstruments = [&] (const lynx::PluginRef& ref, double) -> std::shared_ptr<lynx::Instrument> {
  4608. ++instrumentsMade;
  4609. lastInstrumentState = ref.state;
  4610. return lynx::makeInstrument (lynx::InstrumentKind::synth);
  4611. };
  4612. normal.pluginEffects = [&] (const lynx::PluginRef& ref, double) -> std::shared_ptr<lynx::Effect> {
  4613. ++effectsMade;
  4614. lastEffectState = ref.state;
  4615. return lynx::makeEffect (lynx::EffectKind::eq);
  4616. };
  4617. const auto before = lynx::RackState::build (source, nullptr, 48000.0, normal);
  4618. CHECK (instrumentsMade == 2 && effectsMade == 2);
  4619. // Round 190az (24047e7a0): an ordinary build keeps a running instance for its own channel / slot (the instance
  4620. // tag) or for one saved with the same settings. A renewed tag with a cleared state is another device: it is
  4621. // created anew, with the default state, as a remote reset asks.
  4622. auto reset = source;
  4623. reset.channels[0].instance.renew();
  4624. reset.channels[0].plugin.state.clear();
  4625. reset.mixer[1].effects[0].instance.renew();
  4626. reset.mixer[1].effects[0].plugin.state.clear();
  4627. const auto ordinary = lynx::RackState::build (reset, before.get(), 48000.0, normal);
  4628. CHECK (instrumentsMade == 3 && effectsMade == 3);
  4629. CHECK (lastInstrumentState.empty() && lastEffectState.empty()); // factories received actual default-state requests
  4630. CHECK (ordinary->instruments[0] != before->instruments[0] && ordinary->effects[1][0] != before->effects[1][0]);
  4631. CHECK (ordinary->instruments[1] == before->instruments[1] && ordinary->effects[1][1] == before->effects[1][1]);
  4632. // With the same saved bytes the positional instance stays in an ordinary build; freshInstances replaces exactly
  4633. // the devices it names.
  4634. auto renewed = source;
  4635. renewed.channels[0].instance.renew();
  4636. renewed.mixer[1].effects[0].instance.renew();
  4637. const auto kept = lynx::RackState::build (renewed, before.get(), 48000.0, normal);
  4638. CHECK (instrumentsMade == 3 && effectsMade == 3);
  4639. CHECK (kept->instruments[0] == before->instruments[0] && kept->effects[1][0] == before->effects[1][0]);
  4640. auto requested = normal;
  4641. requested.freshInstances = { renewed.channels[0].instance.id, renewed.mixer[1].effects[0].instance.id };
  4642. const auto replaced = lynx::RackState::build (renewed, before.get(), 48000.0, requested);
  4643. CHECK (instrumentsMade == 4 && effectsMade == 4);
  4644. CHECK (lastInstrumentState == "saved Synth A" && lastEffectState == "saved Effect A");
  4645. CHECK (replaced->instruments[0] != before->instruments[0] && replaced->effects[1][0] != before->effects[1][0]);
  4646. CHECK (replaced->instruments[1] == before->instruments[1] && replaced->effects[1][1] == before->effects[1][1]);
  4647. // The policy belongs to that build only: later ordinary edits reuse the newly created instances.
  4648. const auto later = lynx::RackState::build (renewed, replaced.get(), 48000.0, normal);
  4649. CHECK (instrumentsMade == 4 && effectsMade == 4);
  4650. CHECK (later->instruments[0] == replaced->instruments[0] && later->effects[1][0] == replaced->effects[1][0]);
  4651. }
  4652. TEST (remote_history_normalized_restores_preserve_lineage_without_crossing_producer_edits)
  4653. {
  4654. struct Entry { std::string song; bool remote = false; std::string deviceHash; };
  4655. std::vector<Entry> undo { { "A", true, "B" }, { "B", true, "C" } }, redo;
  4656. std::string live = "C";
  4657. const auto restore = [&] (auto& from, auto& to, const std::string& normalized) {
  4658. CHECK (! from.empty() && from.back().deviceHash == live);
  4659. const auto savedTarget = from.back().song;
  4660. // A real engine inverse stores captured live bytes, even when its raw model blob was older.
  4661. to.push_back ({ live, true, {} });
  4662. from.pop_back();
  4663. live = normalized;
  4664. lynx::remote_history::acceptRestoredState (to.back(), from.empty() ? nullptr : &from.back(), savedTarget, live);
  4665. };
  4666. restore (undo, redo, "B-normalized");
  4667. CHECK (undo.back().deviceHash == live && redo.back().deviceHash == live);
  4668. CHECK (redo.back().song == "C");
  4669. restore (undo, redo, "A-normalized");
  4670. CHECK (undo.empty() && redo.back().song == "B-normalized");
  4671. restore (redo, undo, "B-normalized-again");
  4672. CHECK (redo.back().deviceHash == live); // the next redo adopts normalization through the same proven lineage
  4673. restore (redo, undo, "C-normalized");
  4674. CHECK (redo.empty() && undo.size() == 2);
  4675. // A producer changed B to D before the following remote edit. Undoing that edit may normalize D, but cannot
  4676. // promote the older B fence: doing so would silently authorize undo across the producer's opaque edit.
  4677. undo = { { "A", true, "B" }, { "D", true, "E" } };
  4678. redo.clear();
  4679. live = "E";
  4680. restore (undo, redo, "D-normalized");
  4681. CHECK (redo.back().deviceHash == live && undo.back().deviceHash == "B");
  4682. CHECK (undo.back().deviceHash != live); // the next remote-owned undo remains refused
  4683. // An ordinary producer entry never acquires a remote fence even if its bytes happen to match the target.
  4684. Entry ordinary { "A", false, "B" }, inverse { "C", true, {} };
  4685. lynx::remote_history::acceptRestoredState (inverse, &ordinary, std::string ("B"), std::string ("B-normalized"));
  4686. CHECK (ordinary.deviceHash == "B" && inverse.deviceHash == "B-normalized");
  4687. }
  4688. TEST (remote_capture_mirror_preserves_projected_shape_and_updates_only_matching_hosted_bytes)
  4689. {
  4690. lynx::Song captured;
  4691. lynx::ChannelDef hosted;
  4692. hosted.instrument = lynx::InstrumentKind::plugin;
  4693. hosted.plugin.id = "Synth";
  4694. hosted.plugin.state = "saved";
  4695. captured.channels = { hosted, hosted };
  4696. captured.channels[1].instance.renew();
  4697. lynx::ChannelDef patcher;
  4698. patcher.instrument = lynx::InstrumentKind::patcher;
  4699. lynx::PatchNode nested;
  4700. nested.generator = true;
  4701. nested.channel = hosted;
  4702. patcher.patcher.nodes.push_back (nested);
  4703. captured.channels.push_back (patcher);
  4704. captured.mixer = lynx::Song::defaultMixer (2);
  4705. lynx::EffectSlot effect;
  4706. effect.kind = lynx::EffectKind::plugin;
  4707. effect.plugin.id = "Effect";
  4708. effect.plugin.state = "saved";
  4709. captured.mixer[1].effects.push_back (effect);
  4710. auto published = captured;
  4711. published.channels[0].muted = true; // an insert stand-in changes routing/muting in the published projection
  4712. published.channels[1].instrument = lynx::InstrumentKind::sampler;
  4713. published.channels[1].samplePath = "stem.wav";
  4714. published.channels.push_back (lynx::ChannelDef()); // generated stem helper channels must survive
  4715. published.mixer[1].effects[0].enabled = false;
  4716. captured.channels[0].plugin.state = "captured synth";
  4717. captured.channels[1].plugin.state = "unavailable synth";
  4718. captured.channels[2].patcher.nodes[0].channel.plugin.state = "captured nested synth";
  4719. captured.mixer[1].effects[0].plugin.state = "captured effect";
  4720. lynx::remote_history::mirrorCapturedHostedStates (captured, published);
  4721. CHECK (published.channels.size() == 4 && published.channels[0].muted);
  4722. CHECK (published.channels[0].plugin.state == "captured synth");
  4723. CHECK (published.channels[1].instrument == lynx::InstrumentKind::sampler && published.channels[1].samplePath == "stem.wav");
  4724. CHECK (published.channels[1].plugin.state == "saved"); // a projected replacement is not the captured hosted device
  4725. CHECK (published.channels[2].patcher.nodes[0].channel.plugin.state == "captured nested synth");
  4726. CHECK (! published.mixer[1].effects[0].enabled && published.mixer[1].effects[0].plugin.state == "captured effect");
  4727. published.channels[0].instance.renew();
  4728. captured.channels[0].plugin.state = "different device";
  4729. lynx::remote_history::mirrorCapturedHostedStates (captured, published);
  4730. CHECK (published.channels[0].plugin.state == "captured synth");
  4731. }
  4732. TEST (export_song_adds_silent_tail_and_never_restarts)
  4733. {
  4734. auto s = lynx::Song::makeDefault();
  4735. s.playlist.clear();
  4736. std::int64_t length = 0;
  4737. auto pat = lynx::makeExportSong (s, true, lynx::ppq * 8, length); // empty Playlist: uses the pattern
  4738. CHECK (length == lynx::ppq * 4);
  4739. CHECK (pat.mode == lynx::PlayMode::song && pat.playlist.size() == 2);
  4740. CHECK (pat.songLength() >= length + lynx::ppq * 8);
  4741. // no new notes after the song ends, only silence until the end of the tail
  4742. lynx::EventBuffer events;
  4743. const auto tps = lynx::ticksPerSample (pat.bpm, 48000.0);
  4744. int onsAfterEnd = 0, onsBefore = 0;
  4745. for (double tick = 0.0; tick < (double) (length + lynx::ppq * 8); tick += 512 * tps)
  4746. {
  4747. events.clear();
  4748. lynx::scheduleSong (pat, tick, 512, tps, events);
  4749. for (const auto& e : events)
  4750. if (e.type == lynx::NoteEvent::Type::on)
  4751. (tick + e.sampleOffset * tps >= (double) length ? onsAfterEnd : onsBefore)++;
  4752. }
  4753. CHECK (onsBefore == (int) s.patterns[0].notes.size());
  4754. CHECK (onsAfterEnd == 0);
  4755. // with Playlist: the end is the last clip's, without rounding
  4756. s.playlist.push_back ({ 0, 0, lynx::ppq * 2, lynx::ppq * 3, 0 });
  4757. auto song = lynx::makeExportSong (s, true, 0, length);
  4758. CHECK (length == lynx::ppq * 5 && song.playlist.size() == 1);
  4759. song = lynx::makeExportSong (s, false, 0, length);
  4760. CHECK (length == lynx::ppq * 4 && song.playlist[0].start == 0);
  4761. }
  4762. TEST (note_edit_selection_operations)
  4763. {
  4764. namespace ne = lynx::noteedit;
  4765. lynx::Pattern p;
  4766. p.notes = { { 0, 240, 0, 60 }, { 480, 240, 0, 64 }, { 960, 480, 1, 67 } };
  4767. // sorting keeps the indices in step
  4768. p.notes[0].start = 1200;
  4769. std::vector<int> tracked { 0, 2 };
  4770. ne::sortTracking (p, tracked);
  4771. CHECK (p.notes[2].start == 1200 && p.notes[(std::size_t) tracked[0]].start == 1200);
  4772. CHECK (p.notes[(std::size_t) tracked[1]].key == 67);
  4773. // group move respects the limits
  4774. std::int64_t dt = -5000;
  4775. int dk = 100;
  4776. ne::move (p, { 0, 1 }, dt, dk);
  4777. CHECK (dt == -480 && dk == 127 - 67 && p.notes[0].start == 0 && p.notes[1].key == 127);
  4778. // duplicate: right after the selection, rounded to the grid
  4779. lynx::Pattern q;
  4780. q.notes = { { 0, 240, 0, 60 }, { 240, 300, 0, 62 } }; // 540-tick selection
  4781. const auto added = ne::duplicate (q, { 0, 1 }, 960);
  4782. CHECK (added.size() == 2 && q.notes[(std::size_t) added[0]].start == 960 && q.notes[(std::size_t) added[1]].start == 1200);
  4783. // paste onto another channel with an offset
  4784. const auto pasted = ne::paste (q, ne::copy (q, { 1 }), 100, 3);
  4785. CHECK (pasted.size() == 1 && q.notes.back().channel == 3 && q.notes.back().start == 340);
  4786. // quantize rounds to the nearest
  4787. q.notes[0].start = 130;
  4788. q.notes[1].start = 100;
  4789. ne::quantize (q, { 0, 1 }, 240);
  4790. CHECK (q.notes[0].start == 240 && q.notes[1].start == 0);
  4791. // remove several (order and duplicates don't matter)
  4792. const auto before = q.notes.size();
  4793. ne::remove (q, { 1, 0, 1 });
  4794. CHECK (q.notes.size() == before - 2);
  4795. }
  4796. TEST (note_edit_quick_operations)
  4797. {
  4798. namespace ne = lynx::noteedit;
  4799. const std::int64_t step = lynx::ticksPerStep, bar = lynx::ppq * 4;
  4800. // split: only strictly inside; the tail is appended and does not slide again
  4801. lynx::Pattern p;
  4802. p.notes = { { 0, 4 * step, 0, 60 } };
  4803. p.notes[0].slide = true;
  4804. CHECK (ne::split (p, 0, 0) == -1 && ne::split (p, 0, 4 * step) == -1);
  4805. CHECK (ne::split (p, 0, step) == 1 && p.notes[0].length == step && p.notes[1].start == step && p.notes[1].length == 3 * step
  4806. && ! p.notes[1].slide);
  4807. // chop to grid: a 2.5-step note from half a step becomes 3 pieces cut on the step lines
  4808. lynx::Pattern c;
  4809. c.notes = { { step / 2, step * 5 / 2, 0, 60 }, { 0, step, 1, 50 } };
  4810. const auto pieces = ne::chop (c, { 0, 1 }, step);
  4811. CHECK (pieces.size() == 4 && c.notes.size() == 4);
  4812. CHECK (c.notes[0].length == step / 2 && c.notes[2].start == step && c.notes[2].length == step && c.notes[3].start == 2 * step
  4813. && c.notes[3].length == step);
  4814. // join: touching and overlapping notes of the same key and channel merge; others stay
  4815. lynx::Pattern j;
  4816. j.notes = { { 0, step, 0, 60 }, { step, step, 0, 60 }, { step * 3 / 2, step * 2, 0, 60 }, { 5 * step, step, 0, 60 },
  4817. { step, step, 0, 62 }, { step, step, 1, 60 } };
  4818. CHECK (ne::join (j, { 0, 1, 2, 3, 4, 5 }) == 2 && j.notes.size() == 4);
  4819. CHECK (j.notes[0].start == 0 && j.notes[0].length == step * 7 / 2);
  4820. // legato: each note reaches the next start (a chord ends together); the last keeps its length
  4821. lynx::Pattern l;
  4822. l.notes = { { 0, 10, 0, 60 }, { 0, 10, 0, 64 }, { step, 10, 0, 62 }, { 3 * step, 10, 0, 65 }, { step, 10, 1, 40 } };
  4823. ne::legato (l, { 0, 1, 2, 3, 4 });
  4824. CHECK (l.notes[0].length == step && l.notes[1].length == step && l.notes[2].length == 2 * step && l.notes[3].length == 10
  4825. && l.notes[4].length == 10);
  4826. // shrink to grid and full quantise (start and end)
  4827. ne::shrinkToGrid (l, { 3 }, step / 2);
  4828. CHECK (l.notes[3].length == step / 2);
  4829. lynx::Pattern q;
  4830. q.notes = { { 10, step - 30, 0, 60 }, { step + 100, 20, 0, 60 } };
  4831. ne::quantizeFull (q, { 0, 1 }, step);
  4832. CHECK (q.notes[0].start == 0 && q.notes[0].length == step && q.notes[1].start == step && q.notes[1].length == step);
  4833. // rotate inside the bar, wrapping
  4834. lynx::Pattern r;
  4835. r.notes = { { 0, step, 0, 60 }, { bar - step, step, 0, 62 } };
  4836. ne::rotate (r, { 0, 1 }, step, bar, 1);
  4837. CHECK (r.notes[0].start == step && r.notes[1].start == 0);
  4838. ne::rotate (r, { 0, 1 }, step, bar, -2);
  4839. CHECK (r.notes[0].start == bar - step && r.notes[1].start == bar - 2 * step);
  4840. r.notes.push_back ({ bar + step, step, 0, 64 }); // alone in the second bar: wraps inside that bar
  4841. ne::rotate (r, { 2 }, step, bar, -2);
  4842. CHECK (r.notes[2].start == 2 * bar - step);
  4843. }
  4844. TEST (note_edit_grid_and_time_text)
  4845. {
  4846. namespace ne = lynx::noteedit;
  4847. const std::int64_t step = lynx::ticksPerStep, bar = lynx::ppq * 4;
  4848. CHECK (ne::snapDown (250.0, step) == step && ne::snapDown (-1.0, step) == -step && ne::snapDown (17.9, 1) == 17);
  4849. CHECK (ne::snapNearest (1000.0, { 400, 1100, 980 }, 50.0, step) == 980);
  4850. CHECK (ne::snapNearest (1000.0, { 400 }, 50.0, step) == 960); // too far: falls back to the grid
  4851. // screen grid: 24 px per step shows steps; zoomed out it climbs to beats, bars and bar multiples
  4852. CHECK (ne::screenStep (24.0 / step, bar) == step);
  4853. CHECK (ne::screenStep (96.0 / bar, bar) == lynx::ppq);
  4854. CHECK (ne::screenStep (8.0 / bar, bar) == 2 * bar);
  4855. CHECK (ne::screenStep (200.0 / step, bar) == step / 4);
  4856. CHECK (ne::formatTime (0, lynx::Meter (4, 4)) == "1:1:0" && ne::formatTime (bar + 5 * step + 7, lynx::Meter (4, 4)) == "2:6:7");
  4857. CHECK (ne::formatTime (step * 2, lynx::Meter (4, 4), true) == "0:2:0");
  4858. std::int64_t t = 0;
  4859. CHECK (ne::parseTime ("2:6:7", lynx::Meter (4, 4), t) && t == bar + 5 * step + 7);
  4860. CHECK (ne::parseTime ("3", lynx::Meter (4, 4), t) && t == 2 * bar);
  4861. CHECK (ne::parseTime ("0:2:0", lynx::Meter (4, 4), t, true) && t == 2 * step);
  4862. CHECK (! ne::parseTime ("0:1:0", lynx::Meter (4, 4), t) && ! ne::parseTime ("1:17:0", lynx::Meter (4, 4), t) && ! ne::parseTime ("1:1:240", lynx::Meter (4, 4), t)
  4863. && ! ne::parseTime ("a", lynx::Meter (4, 4), t) && ! ne::parseTime ("", lynx::Meter (4, 4), t));
  4864. CHECK (ne::parseTime ("1:12:0", lynx::Meter (3, 4), t) && ! ne::parseTime ("1:13:0", lynx::Meter (3, 4), t)); // 3/4: 12 steps per bar
  4865. }
  4866. TEST (playlist_clip_edges_and_time_selection)
  4867. {
  4868. namespace ne = lynx::noteedit;
  4869. const std::int64_t beat = lynx::ppq;
  4870. // left-edge trim: content stays in place, cannot reveal what is before the content's start
  4871. lynx::Clip c { 0, 0, 4 * beat, 4 * beat, beat };
  4872. ne::trimStart (c, 5 * beat, beat);
  4873. CHECK (c.start == 5 * beat && c.offset == 2 * beat && c.length == 3 * beat);
  4874. ne::trimStart (c, 0, beat);
  4875. CHECK (c.start == 3 * beat && c.offset == 0 && c.length == 5 * beat);
  4876. ne::trimStart (c, 100 * beat, beat);
  4877. CHECK (c.length == beat && c.end() == 8 * beat);
  4878. // slip moves the content, not the clip
  4879. lynx::Clip s { 0, 0, 0, 2 * beat, beat };
  4880. ne::slip (s, beat / 2);
  4881. CHECK (s.offset == beat / 2 && s.start == 0 && s.length == 2 * beat);
  4882. ne::slip (s, beat);
  4883. CHECK (s.offset == 0);
  4884. ne::slip (s, -3 * beat);
  4885. CHECK (s.offset == 3 * beat);
  4886. // insert / delete time across all tracks, markers included
  4887. std::vector<lynx::Clip> clips { { 0, 0, 0, 2 * beat, 0 }, { 0, 1, 4 * beat, 2 * beat, 0 }, { 0, 2, beat, 6 * beat, 0 } };
  4888. std::vector<lynx::Marker> markers { { 5 * beat, "A" }, { beat / 2, "B" } };
  4889. ne::insertTime (clips, markers, 2 * beat, 4 * beat);
  4890. CHECK (clips[0].start == 0 && clips[1].start == 8 * beat && clips[2].start == beat && markers[0].tick == 9 * beat
  4891. && markers[1].tick == beat / 2);
  4892. std::vector<lynx::Clip> d { { 0, 0, 0, 2 * beat, 0 }, // before: kept
  4893. { 0, 1, 3 * beat, beat, 0 }, // inside: deleted
  4894. { 0, 2, beat, 6 * beat, 0 }, // spans the cut: head + tail
  4895. { 0, 3, 3 * beat, 4 * beat, 0 }, // tail outside: trimmed and moved
  4896. { 0, 4, 8 * beat, beat, 0 } }; // after: moved left
  4897. std::vector<lynx::Marker> dm { { 3 * beat, "in" }, { 9 * beat, "after" } };
  4898. ne::deleteTime (d, dm, 2 * beat, 5 * beat);
  4899. CHECK (d.size() == 5);
  4900. CHECK (d[1].track == 2 && d[1].start == beat && d[1].length == beat);
  4901. CHECK (d[2].track == 2 && d[2].start == 2 * beat && d[2].offset == 4 * beat && d[2].length == 2 * beat);
  4902. CHECK (d[3].track == 3 && d[3].start == 2 * beat && d[3].offset == 2 * beat && d[3].length == 2 * beat);
  4903. CHECK (d[4].start == 5 * beat && dm.size() == 1 && dm[0].tick == 6 * beat);
  4904. // time selection: overlapping clips; Ctrl+B repeats what starts inside it after itself
  4905. std::vector<lynx::Clip> r { { 0, 0, 0, beat, 0 }, { 0, 1, beat, 2 * beat, 0 }, { 0, 0, 4 * beat, beat, 0 } };
  4906. CHECK ((ne::clipsInTime (r, beat / 2, 2 * beat) == std::vector<int> { 0, 1 }));
  4907. const auto added = ne::repeatTime (r, 0, 4 * beat, {});
  4908. CHECK (added.size() == 2 && r[3].start == 4 * beat && r[4].start == 5 * beat && r[4].track == 1);
  4909. CHECK (ne::repeatTime (r, 0, 2 * beat, { 2 }).size() == 1 && r.back().start == 6 * beat);
  4910. }
  4911. TEST (recorder_builds_notes_in_pattern_and_song_mode)
  4912. {
  4913. auto song = lynx::Song::makeDefault();
  4914. const auto loop = song.patterns[0].effectiveLength (lynx::Meter (4, 4)); // 1 bar = 3840
  4915. lynx::NoteRecorder r;
  4916. r.add (song, { 100.0, 4, 60, 0.5f, true }); // no take: ignored
  4917. CHECK (r.takeFinished().empty());
  4918. r.begin (loop);
  4919. r.add (song, { 4000.0, 4, 60, 0.5f, true }); // second loop pass
  4920. r.add (song, { 4480.0, 4, 60, 0.0f, false });
  4921. r.add (song, { 5000.0, 4, 64, 0.9f, true }); // held until stop
  4922. r.end (song, 5240.0);
  4923. const auto notes = r.takeFinished();
  4924. CHECK (notes.size() == 2);
  4925. CHECK (notes[0].start == 4000 - 3840 && notes[0].length == 480 && notes[0].velocity == 0.5f && notes[0].channel == 4);
  4926. CHECK (notes[1].key == 64 && notes[1].start == 5000 - 3840 && notes[1].length == 240);
  4927. // song mode: position inside the current pattern's clip (with offset)
  4928. song.mode = lynx::PlayMode::song;
  4929. song.playlist = { { 0, 0, 7680, 3840, 960 } };
  4930. CHECK (lynx::patternPositionForTick (song, 7680.0 + 100.0, loop) == 1060);
  4931. CHECK (lynx::patternPositionForTick (song, 100.0, loop) == 100); // outside the clip: like loop
  4932. }
  4933. TEST (automation_curve_and_playback)
  4934. {
  4935. lynx::Automation a;
  4936. a.points = { { 0, 0.0f }, { 960, 1.0f }, { 1920, 0.5f, 0.0f } };
  4937. CHECK (a.valueAt (-10.0) == 0.0f && a.valueAt (480.0) == 0.5f && a.valueAt (5000.0) == 0.5f);
  4938. CHECK (a.length() == 1920);
  4939. a.points[0].tension = 1.0f; // curve: starts slow
  4940. CHECK (a.valueAt (480.0) < 0.2f && a.valueAt (480.0) > 0.0f);
  4941. // automated Master: silence in the first bar, sound in the second, and the last value
  4942. // keeps applying after the automation clip ends
  4943. auto song = lynx::Song::makeDefault();
  4944. song.bpm = 120.0;
  4945. song.mode = lynx::PlayMode::song;
  4946. const auto bar = (std::int64_t) lynx::ppq * 4;
  4947. for (int b = 0; b < 3; ++b)
  4948. song.playlist.push_back ({ 0, 0, bar * b, bar, 0 });
  4949. lynx::Automation master;
  4950. master.target = { lynx::AutomationTargetKind::mixerVolume, 0 };
  4951. master.points = { { 0, 0.0f }, { bar - 1, 0.0f }, { bar, 0.8f } };
  4952. song.automations.push_back (master);
  4953. lynx::Clip ac;
  4954. ac.kind = lynx::ClipKind::automation;
  4955. ac.automation = 0;
  4956. ac.track = 1;
  4957. ac.length = bar + bar / 2;
  4958. song.playlist.push_back (ac);
  4959. const double sr = 48000.0;
  4960. auto state = lynx::RackState::build (song, nullptr, sr);
  4961. CHECK (state->hasAutomation && state->mixerVolumeAuto[0] == 0 && state->automationClips[0].size() == 1);
  4962. lynx::RackRenderer r;
  4963. r.prepare (sr, 512);
  4964. std::vector<float> l (512), rr (512);
  4965. const auto tps = lynx::ticksPerSample (song.bpm, sr);
  4966. float peakBar1 = 0.0f, peakBar2 = 0.0f, peakBar3 = 0.0f;
  4967. for (double tick = 0.0; tick < (double) (bar * 3); tick += 512 * tps)
  4968. {
  4969. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  4970. auto& peak = tick < (double) bar - 512 * tps ? peakBar1 : tick < (double) bar * 2 ? peakBar2 : peakBar3;
  4971. for (auto v : l)
  4972. peak = std::max (peak, std::abs (v));
  4973. }
  4974. CHECK (peakBar1 < 1.0e-6f);
  4975. CHECK (peakBar2 > 0.05f);
  4976. CHECK (peakBar3 > 0.05f); // after the clip: keeps the last value
  4977. // target removed: automation disappears from the tables
  4978. song.removeChannel (0);
  4979. song.automations[0].target = { lynx::AutomationTargetKind::channelVolume, 0 };
  4980. song.removeChannel (0);
  4981. CHECK (song.automations[0].target.kind == lynx::AutomationTargetKind::none);
  4982. song.removeAutomation (0);
  4983. CHECK (song.automations.empty() && song.playlist.size() == 3);
  4984. }
  4985. TEST (mixer_sends_between_inserts)
  4986. {
  4987. // insert 1 goes only to 3; 3 goes to the Master: same sum (0.5 + 0.5)
  4988. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.setRoute (1, 3, true); s.setRoute (1, 0, false); }) - 1.0f) < 1e-4f);
  4989. // send at 50% and muted destination
  4990. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.setRoute (1, 3, true, 0.5f); s.setRoute (1, 0, false); }) - 0.75f) < 1e-4f);
  4991. 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);
  4992. // going to the Master and to 3: the signal doubles
  4993. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.setRoute (1, 3, true); }) - 1.5f) < 1e-4f);
  4994. // solo on 3 lets what reaches it sound (insert 1), but not 2
  4995. 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);
  4996. // solo on 1 lets the path to the Master (3) sound
  4997. 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);
  4998. // order and cycles
  4999. auto song = lynx::Song::makeDefault();
  5000. song.setRoute (4, 2, true);
  5001. song.setRoute (2, 1, true);
  5002. CHECK (! song.canRoute (1, 4) && ! song.canRoute (1, 2) && song.canRoute (1, 5));
  5003. song.setRoute (1, 4, true); // ignored: would create a cycle
  5004. CHECK (! song.routes (1, 4));
  5005. const auto order = song.mixerOrder();
  5006. const auto pos = [&] (int t) { return std::find (order.begin(), order.end(), t) - order.begin(); };
  5007. CHECK ((int) order.size() == (int) song.mixer.size() - 1);
  5008. CHECK (pos (4) < pos (2) && pos (2) < pos (1));
  5009. song.setRoute (4, 2, false);
  5010. CHECK (! song.routes (4, 2) && song.routes (4, 0));
  5011. // route levels (FL's route knobs): to inserts and to Master, clamped to the fader range; none without a route
  5012. song.setRoute (3, 5, true);
  5013. song.setRouteLevel (3, 5, 0.5f);
  5014. song.setRouteLevel (3, 0, 1.4f);
  5015. CHECK (song.routeLevel (3, 5) == 0.5f && song.routeLevel (3, 0) == 1.4f && song.mixer[3].masterLevel == 1.4f);
  5016. song.setRouteLevel (3, 0, 9.0f);
  5017. CHECK (song.routeLevel (3, 0) == lynx::maxFaderGain);
  5018. song.setRouteLevel (3, 6, 0.5f); // no such route: ignored
  5019. CHECK (! song.routes (3, 6) && song.routeLevel (3, 6) == 0.0f);
  5020. song.setRoute (3, 0, false);
  5021. CHECK (song.routeLevel (3, 0) == 0.0f);
  5022. // the route to Master's level scales what insert 1 sends there (0.5 + 0.5·0.25)
  5023. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.setRouteLevel (1, 0, 0.25f); }) - 0.625f) < 1e-4f);
  5024. }
  5025. namespace
  5026. {
  5027. // Test effect: delays the signal by `latency` samples and reports that latency (like a lookahead plugin).
  5028. class LatentEffect final : public lynx::Effect
  5029. {
  5030. public:
  5031. explicit LatentEffect (int samples) : latency (samples), line (samples) {}
  5032. void prepare (double) noexcept override {}
  5033. void reset() noexcept override {}
  5034. void process (float* l, float* r, int n, const lynx::EffectParams&, double) noexcept override { line.process (l, r, n); }
  5035. int latencySamples() const noexcept override { return latency; }
  5036. private:
  5037. int latency;
  5038. lynx::DelayLine line;
  5039. };
  5040. } // namespace
  5041. TEST (delay_line_mix_into_matches_process_and_drains_tail)
  5042. {
  5043. // mixInto = process over source*level, added to the destination; in stretches that cross the ring
  5044. lynx::DelayLine a (37), b (37);
  5045. std::vector<float> sl (300), sr (300), dl (300, 0.5f), dr (300, -0.5f), xl (300), xr (300);
  5046. for (int i = 0; i < 300; ++i)
  5047. {
  5048. sl[(std::size_t) i] = (float) std::sin (i * 0.1);
  5049. sr[(std::size_t) i] = (float) std::cos (i * 0.07);
  5050. }
  5051. for (int start = 0; start < 300; start += 100)
  5052. {
  5053. b.mixInto (sl.data() + start, sr.data() + start, 0.25f, dl.data() + start, dr.data() + start, 100);
  5054. for (int i = start; i < start + 100; ++i)
  5055. {
  5056. xl[(std::size_t) i] = sl[(std::size_t) i] * 0.25f;
  5057. xr[(std::size_t) i] = sr[(std::size_t) i] * 0.25f;
  5058. }
  5059. a.process (xl.data() + start, xr.data() + start, 100);
  5060. }
  5061. float worst = 0.0f;
  5062. for (int i = 0; i < 300; ++i)
  5063. worst = std::max ({ worst, std::abs (dl[(std::size_t) i] - 0.5f - xl[(std::size_t) i]),
  5064. std::abs (dr[(std::size_t) i] + 0.5f - xr[(std::size_t) i]) });
  5065. CHECK (worst < 1e-6f);
  5066. // silenced source: the tail still in the line comes out, then nothing remains
  5067. std::vector<float> tl (64, 0.0f), tr (64, 0.0f);
  5068. b.mixInto (nullptr, nullptr, 1.0f, tl.data(), tr.data(), 64);
  5069. CHECK (std::abs (tl[0] - sl[263] * 0.25f) < 1e-6f);
  5070. std::vector<float> zl (64, 0.0f), zr (64, 0.0f);
  5071. b.mixInto (nullptr, nullptr, 1.0f, zl.data(), zr.data(), 64);
  5072. CHECK (*std::max_element (zl.begin(), zl.end()) == 0.0f && *std::min_element (zl.begin(), zl.end()) == 0.0f);
  5073. }
  5074. TEST (limiter_lookahead_holds_ceiling_on_bursts)
  5075. {
  5076. // short bursts over a low floor: the sliding maximum must see each peak before it leaves
  5077. auto fx = lynx::makeEffect (lynx::EffectKind::limiter);
  5078. fx->prepare (48000.0);
  5079. auto slot = lynx::makeEffectSlot (lynx::EffectKind::limiter);
  5080. slot.params[2] = 0.0f; // 10 ms release
  5081. std::vector<float> l (48000), r (48000);
  5082. std::uint32_t seed = 1;
  5083. for (int i = 0; i < 48000; ++i)
  5084. {
  5085. seed = seed * 1664525u + 1013904223u;
  5086. const auto noise = ((float) (seed >> 8) / 16777216.0f - 0.5f) * 0.1f;
  5087. l[(std::size_t) i] = noise + ((i % 9973) < 3 ? 3.0f : 0.0f);
  5088. r[(std::size_t) i] = -noise;
  5089. }
  5090. for (int i = 0; i < 48000; i += 333)
  5091. fx->process (l.data() + i, r.data() + i, std::min (333, 48000 - i), slot.params, 120.0);
  5092. float peak = 0.0f, quiet = 0.0f;
  5093. for (int i = 0; i < 48000; ++i)
  5094. peak = std::max (peak, std::abs (l[(std::size_t) i]));
  5095. for (int i = 9973 * 2 + 2000; i < 9973 * 2 + 2400; ++i) // 40 ms after the burst the gain has already recovered
  5096. quiet = std::max (quiet, std::abs (r[(std::size_t) i]));
  5097. const auto ceiling = 1.0f; // the default ceiling is at 0 dBFS or below
  5098. CHECK (peak <= ceiling + 1e-5f);
  5099. CHECK (quiet > 0.03f);
  5100. }
  5101. TEST (pdc_aligns_paths_through_latent_effects)
  5102. {
  5103. // plan: channel 0 -> insert 1 (effect of 100) -> insert 3; channel 1 -> insert 2 -> Master
  5104. lynx::Song s;
  5105. s.mixer = lynx::Song::defaultMixer (4);
  5106. s.channels.resize (3);
  5107. s.channels[0].mixerTrack = 1;
  5108. s.channels[1].mixerTrack = 2;
  5109. s.channels[2].mixerTrack = 3;
  5110. s.mixer[1].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::eq));
  5111. s.setRoute (1, 3, true);
  5112. s.setRoute (1, 0, false);
  5113. auto plan = lynx::computePdc (s, { 0, 0, 30 }, { {}, { 100 } });
  5114. CHECK (plan.inAlign[1] == 0 && plan.outLat[1] == 100);
  5115. CHECK (plan.inAlign[3] == 100 && plan.channelDelay[2] == 70); // instrument of 30 on insert 3
  5116. CHECK (plan.sendDelay[1][1] == 0); // the slowest path isn't delayed
  5117. CHECK (plan.sendDelay[2][0] == 100 && plan.sendDelay[3][0] == 0 && plan.total == 100);
  5118. // bypass zeroes the slot's latency
  5119. s.mixer[1].effects[0].enabled = false;
  5120. CHECK (lynx::computePdc (s, { 0, 0, 30 }, { {}, { 100 } }).total == 30);
  5121. // rendered: two impulses played together come out together
  5122. lynx::Song r;
  5123. r.mixer = lynx::Song::defaultMixer (2);
  5124. for (int i = 0; i < 2; ++i)
  5125. {
  5126. lynx::ChannelDef c;
  5127. c.instrument = lynx::InstrumentKind::sampler;
  5128. c.samplePath = "impulso";
  5129. c.volume = 1.0f;
  5130. c.mixerTrack = i + 1;
  5131. r.channels.push_back (c);
  5132. }
  5133. r.patterns.emplace_back();
  5134. lynx::EffectSlot fx;
  5135. fx.kind = lynx::EffectKind::plugin;
  5136. fx.plugin.id = "latente";
  5137. r.mixer[1].effects.push_back (fx);
  5138. lynx::RackState::Factories f;
  5139. f.samples = [] (const std::string&) {
  5140. auto d = std::make_shared<lynx::SampleData>();
  5141. d->left.assign (64, 0.0f);
  5142. d->left[0] = 1.0f;
  5143. return std::shared_ptr<const lynx::SampleData> (d);
  5144. };
  5145. f.pluginEffects = [] (const lynx::PluginRef&, double) -> std::shared_ptr<lynx::Effect> { return std::make_shared<LatentEffect> (100); };
  5146. auto state = lynx::RackState::build (r, nullptr, 48000.0, f);
  5147. CHECK (state->pdc.total == 100 && state->sendDelays[2][0] != nullptr && state->sendDelays[2][0]->length() == 100);
  5148. lynx::RackRenderer renderer;
  5149. renderer.prepare (48000.0, 256);
  5150. lynx::LiveEventQueue live;
  5151. for (int ch = 0; ch < 2; ++ch)
  5152. {
  5153. lynx::NoteEvent on;
  5154. on.channel = ch;
  5155. on.key = 60;
  5156. on.velocity = 1.0f;
  5157. live.push (on);
  5158. }
  5159. std::vector<float> l (256), rr (256);
  5160. renderer.process (state.get(), false, 0.0, 256, l.data(), rr.data(), &live);
  5161. int first = -1;
  5162. for (int i = 0; i < 256 && first < 0; ++i)
  5163. if (std::abs (l[(std::size_t) i]) > 0.01f)
  5164. first = i;
  5165. CHECK (first == 100 && std::abs (l[100] - 2.0f) < 1e-3f); // both impulses arrive together, summed
  5166. // editing without changing latencies reuses the lines
  5167. r.channels[0].volume = 0.5f;
  5168. auto next = lynx::RackState::build (r, state.get(), 48000.0, f);
  5169. CHECK (next->sendDelays[2][0] == state->sendDelays[2][0]);
  5170. }
  5171. TEST (mixer_mute_fades_instead_of_clicking)
  5172. {
  5173. lynx::Song s;
  5174. s.mixer = lynx::Song::defaultMixer (2);
  5175. lynx::ChannelDef c;
  5176. c.instrument = lynx::InstrumentKind::sampler;
  5177. c.samplePath = "dc";
  5178. c.volume = 1.0f;
  5179. c.mixerTrack = 1;
  5180. s.channels.push_back (c);
  5181. s.patterns.emplace_back();
  5182. auto provider = [] (const std::string&) {
  5183. auto d = std::make_shared<lynx::SampleData>();
  5184. d->left.assign (48000, 0.5f);
  5185. return std::shared_ptr<const lynx::SampleData> (d);
  5186. };
  5187. auto on = lynx::RackState::build (s, nullptr, 48000.0, provider);
  5188. s.mixer[1].muted = true;
  5189. auto off = lynx::RackState::build (s, on.get(), 48000.0, provider);
  5190. lynx::RackRenderer r;
  5191. r.prepare (48000.0, 256);
  5192. lynx::LiveEventQueue live;
  5193. lynx::NoteEvent note;
  5194. note.velocity = 1.0f;
  5195. live.push (note);
  5196. std::vector<float> l (256), rr (256);
  5197. r.process (on.get(), false, 0.0, 256, l.data(), rr.data(), &live);
  5198. CHECK (std::abs (l[255] - 0.5f) < 1e-3f);
  5199. r.process (off.get(), false, 0.0, 256, l.data(), rr.data());
  5200. // the fader glides down by 0.001 per sample, as FL's (REA_rodada85 §3): 1000 samples from 1 to 0
  5201. 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);
  5202. for (int b = 0; b < 3; ++b)
  5203. r.process (off.get(), false, 0.0, 256, l.data(), rr.data());
  5204. CHECK (std::abs (l[255]) < 1e-6f); // down to zero after 1000 samples
  5205. r.process (off.get(), false, 0.0, 256, l.data(), rr.data());
  5206. CHECK (std::abs (l[0]) < 1e-9f);
  5207. }
  5208. TEST (dither_quantizes_to_lsb_grid_with_small_error)
  5209. {
  5210. lynx::Dither d (16);
  5211. std::vector<float> x (4800);
  5212. for (std::size_t i = 0; i < x.size(); ++i)
  5213. x[i] = 0.25f * std::sin ((float) i * 0.05f);
  5214. auto y = x;
  5215. d.process (y.data(), (int) y.size(), 0);
  5216. double errorSum = 0.0;
  5217. bool onGrid = true;
  5218. for (std::size_t i = 0; i < x.size(); ++i)
  5219. {
  5220. const auto steps = (y[i] - d.writerBias()) * 32768.0f; // half an LSB above the grid point (round 160)
  5221. onGrid = onGrid && std::abs (steps - std::round (steps)) < 1e-3f;
  5222. errorSum += std::abs (y[i] - x[i]);
  5223. }
  5224. CHECK (onGrid);
  5225. CHECK (errorSum / (double) x.size() < 4.0 / 32768.0); // a few LSBs of noise on average
  5226. }
  5227. TEST (parallel_rendering_matches_sequential)
  5228. {
  5229. // 32 channels (worth parallelizing), inserts with chained sends
  5230. auto song = lynx::Song::makeDefault();
  5231. song.mixer = lynx::Song::defaultMixer (16);
  5232. for (int i = 5; i < 32; ++i)
  5233. {
  5234. lynx::ChannelDef c;
  5235. c.instrument = i % 2 ? lynx::InstrumentKind::synth : lynx::InstrumentKind::hat;
  5236. c.mixerTrack = 1 + i % 12;
  5237. c.volume = 0.3f;
  5238. song.channels.push_back (c);
  5239. for (int step = i % 4; step < 16; step += 3)
  5240. song.patterns[0].notes.push_back ({ (std::int64_t) step * lynx::ticksPerStep, 200, i, 48 + i, 0.7f });
  5241. }
  5242. song.patterns[0].sortNotes();
  5243. song.setRoute (3, 7, true, 0.6f);
  5244. song.setRoute (7, 9, true, 0.5f);
  5245. song.setRoute (2, 9, true);
  5246. song.mixer[9].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::reverb));
  5247. auto seqState = lynx::RackState::build (song, nullptr, 48000.0);
  5248. auto parState = lynx::RackState::build (song, nullptr, 48000.0);
  5249. CHECK (parState->parallel && parState->mixerLayers.size() == 3);
  5250. CHECK (parState->graphOk); // round 165i: the task graph runs it (channels and inserts by readiness)
  5251. lynx::WorkerPool pool (3);
  5252. lynx::RackRenderer seq, par;
  5253. seq.prepare (48000.0, 512);
  5254. par.prepare (48000.0, 512);
  5255. par.setWorkerPool (&pool);
  5256. std::vector<float> l1 (512), r1 (512), l2 (512), r2 (512);
  5257. const auto tps = lynx::ticksPerSample (song.bpm, 48000.0);
  5258. bool same = true;
  5259. float peak = 0.0f;
  5260. for (int block = 0; block < 200; ++block)
  5261. {
  5262. seq.process (seqState.get(), true, block * 512 * tps, 512, l1.data(), r1.data());
  5263. par.process (parState.get(), true, block * 512 * tps, 512, l2.data(), r2.data());
  5264. same = same && l1 == l2 && r1 == r2;
  5265. for (auto v : l1)
  5266. peak = std::max (peak, std::abs (v));
  5267. }
  5268. CHECK (same);
  5269. CHECK (peak > 0.05f);
  5270. // the pool handles many small rounds in a row
  5271. std::atomic<int> sum { 0 };
  5272. auto add = [&sum] (int i) { sum.fetch_add (i + 1); };
  5273. for (int round = 0; round < 2000; ++round)
  5274. pool.parallelFor (5, add);
  5275. CHECK (sum.load() == 2000 * 15);
  5276. }
  5277. TEST (rea165i_task_graph_matches_layers_with_sidechain_and_cycle)
  5278. {
  5279. // the task graph against the layers (LYNX_TASKGRAPH off is the same as a pool-less render): sends, a sidechain,
  5280. // a chain three inserts deep, channels on the Master; a routing cycle keeps the layers
  5281. auto song = lynx::Song::makeDefault();
  5282. song.mixer = lynx::Song::defaultMixer (16);
  5283. for (int i = 5; i < 40; ++i)
  5284. {
  5285. lynx::ChannelDef c;
  5286. c.instrument = i % 3 == 0 ? lynx::InstrumentKind::hat : lynx::InstrumentKind::synth;
  5287. c.mixerTrack = i % 7 == 0 ? 0 : 1 + i % 13;
  5288. c.volume = 0.25f;
  5289. song.channels.push_back (c);
  5290. for (int step = i % 5; step < 16; step += 2)
  5291. song.patterns[0].notes.push_back ({ (std::int64_t) step * lynx::ticksPerStep, 150, i, 40 + i, 0.6f });
  5292. }
  5293. song.patterns[0].sortNotes();
  5294. song.setRoute (3, 7, true, 0.6f);
  5295. song.setRoute (7, 9, true, 0.5f);
  5296. song.setRoute (9, 11, true, 0.5f);
  5297. song.mixer[9].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::reverb));
  5298. auto comp = lynx::makeEffectSlot (lynx::EffectKind::compressor);
  5299. comp.sidechain = 4;
  5300. song.mixer[11].effects.push_back (comp);
  5301. auto render = [] (const lynx::Song& s, bool pool, bool& graph) {
  5302. auto state = lynx::RackState::build (s, nullptr, 48000.0);
  5303. graph = state->graphOk;
  5304. lynx::WorkerPool workers (4);
  5305. lynx::RackRenderer r;
  5306. r.prepare (48000.0, 512);
  5307. if (pool)
  5308. r.setWorkerPool (&workers);
  5309. std::vector<float> out, l (384), rr (384);
  5310. const auto tps = lynx::ticksPerSample (s.bpm, 48000.0);
  5311. for (int block = 0; block < 150; ++block)
  5312. {
  5313. r.process (state.get(), true, block * 384 * tps, 384, l.data(), rr.data());
  5314. out.insert (out.end(), l.begin(), l.end());
  5315. out.insert (out.end(), rr.begin(), rr.end());
  5316. }
  5317. return out;
  5318. };
  5319. bool graph = false, unused = false;
  5320. const auto layered = render (song, false, unused);
  5321. const auto parallel = render (song, true, graph);
  5322. CHECK (graph);
  5323. CHECK (layered == parallel);
  5324. CHECK (*std::max_element (layered.begin(), layered.end()) > 0.02f);
  5325. 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)
  5326. const auto cycleLayered = render (song, false, unused);
  5327. const auto cycleParallel = render (song, true, graph);
  5328. CHECK (! graph);
  5329. CHECK (cycleLayered == cycleParallel);
  5330. }
  5331. TEST (rea165i_test_profile_and_test_mode_parsing)
  5332. {
  5333. using namespace lynx::testprofile;
  5334. CHECK (! profileDirFrom (nullptr));
  5335. CHECK (! profileDirFrom (""));
  5336. CHECK (! profileDirFrom ("relative/dir")); // only absolute folders
  5337. CHECK (! profileDirFrom ("C:relative"));
  5338. CHECK (profileDirFrom ("/tmp/lynx-profile/") == std::string ("/tmp/lynx-profile"));
  5339. CHECK (profileDirFrom (" \"C:\\Users\\me\\bench profile\\\" ") == std::string ("C:\\Users\\me\\bench profile"));
  5340. CHECK (profileDirFrom ("D:/x") == std::string ("D:/x"));
  5341. CHECK (profileDirFrom ("C:\\") == std::string ("C:\\")); // a drive root keeps its separator
  5342. CHECK (! testModeFrom (nullptr));
  5343. CHECK (! testModeFrom (""));
  5344. CHECK (! testModeFrom ("0"));
  5345. CHECK (! testModeFrom (" Off "));
  5346. CHECK (! testModeFrom ("false"));
  5347. CHECK (testModeFrom ("1"));
  5348. CHECK (testModeFrom ("yes"));
  5349. }
  5350. TEST (rea165i_keep_float_mode_restores_ftz_after_foreign_code)
  5351. {
  5352. // a hosted plugin that turns flush-to-zero off (or changes the rounding) no longer leaks it into LYNX's DSP
  5353. const lynx::ScopedFlushDenormals ftz;
  5354. const auto before = _mm_getcsr();
  5355. {
  5356. const lynx::KeepFloatMode keep;
  5357. _mm_setcsr ((before & ~0x8040u) | 0x6000u); // what a plugin might leave: no FTZ/DAZ, round toward zero
  5358. }
  5359. CHECK (_mm_getcsr() == before);
  5360. volatile float tiny = 1.0e-38f;
  5361. volatile float smaller = tiny * 1.0e-3f; // denormal without FTZ
  5362. CHECK (smaller == 0.0f);
  5363. }
  5364. TEST (arrangements_switch_and_keep_clip_indices)
  5365. {
  5366. auto s = lynx::Song::makeDefault();
  5367. s.patterns.emplace_back().name = "B";
  5368. s.playlist = { { 1, 0, 0, 960, 0 } };
  5369. s.loopEnd = 960;
  5370. CHECK (s.arrangements.size() == 1 && s.currentArrangement == 0);
  5371. const auto second = s.addArrangement ("Versão curta", true);
  5372. CHECK (second == 1 && s.currentArrangement == 1 && s.playlist.size() == 1 && s.loopEnd == 960);
  5373. s.playlist.push_back ({ 0, 1, 960, 960, 0 });
  5374. s.loopEnd = 0;
  5375. s.selectArrangement (0);
  5376. CHECK (s.playlist.size() == 1 && s.loopEnd == 960 && s.arrangements[1].clips.size() == 2);
  5377. // removing pattern 0 fixes the clips stored in the other arrangement
  5378. s.removePattern (0);
  5379. CHECK (s.playlist.size() == 1 && s.playlist[0].pattern == 0);
  5380. CHECK (s.arrangements[1].clips.size() == 1 && s.arrangements[1].clips[0].pattern == 0);
  5381. s.removeArrangement (0);
  5382. CHECK (s.arrangements.size() == 1 && s.currentArrangement == 0 && s.playlist.size() == 1 && s.loopEnd == 0);
  5383. }
  5384. namespace
  5385. {
  5386. // Test effect that inverts the signal and counts how many times it processed.
  5387. class CountingInverter final : public lynx::Effect
  5388. {
  5389. public:
  5390. void prepare (double) noexcept override {}
  5391. void reset() noexcept override {}
  5392. void process (float* l, float* r, int n, const lynx::EffectParams&, double) noexcept override
  5393. {
  5394. ++calls;
  5395. for (int i = 0; i < n; ++i)
  5396. {
  5397. l[i] = -l[i];
  5398. r[i] = -r[i];
  5399. }
  5400. }
  5401. int calls = 0;
  5402. };
  5403. } // namespace
  5404. TEST (keyboard_controller_outputs_key_value)
  5405. {
  5406. std::vector<std::uint8_t> blob (554, 0);
  5407. const std::uint32_t version = 3;
  5408. std::memcpy (blob.data(), &version, 4);
  5409. const float key60 = 0.7f, off = 0.2f;
  5410. std::memcpy (blob.data() + 12 + 60 * 4, &key60, 4);
  5411. std::memcpy (blob.data() + 548, &off, 4);
  5412. blob[552] = 1; // auto release
  5413. lynx::ChannelDef c;
  5414. c.instrument = lynx::InstrumentKind::controller;
  5415. c.controllerState = blob;
  5416. auto inst = lynx::makeInstrument (lynx::InstrumentKind::controller);
  5417. inst->prepare (48000.0);
  5418. inst->configure (c, 120.0);
  5419. std::vector<float> l (64), r (64);
  5420. inst->noteOn (60, 1.0f);
  5421. inst->render (l.data(), r.data(), 64, 1.0f, 1.0f);
  5422. CHECK (std::abs (inst->controlOutput (0) - 0.7f) < 1e-6f);
  5423. inst->noteOff (60);
  5424. CHECK (std::abs (inst->controlOutput (0) - 0.2f) < 1e-6f);
  5425. }
  5426. TEST (envelope_controller_ducks_on_each_note)
  5427. {
  5428. // a unipolar articulator like "Basic Sidechain": base 0.05, drops to 0 on the note and rises to 1 in 0.3 s
  5429. lynx::ControllerArticulator a;
  5430. a.envOn = true;
  5431. a.base = 0.05f;
  5432. a.envLevel = 1.0f;
  5433. a.count = 3;
  5434. a.time = { 0.0f, 0.2f, 0.3f };
  5435. a.level = { 0.0f, 0.75f, 1.0f };
  5436. CHECK (std::abs (lynx::controllerValue (a, 0.0, -1.0) - 0.05f) < 1e-4f);
  5437. CHECK (std::abs (lynx::controllerValue (a, 0.1, -1.0) - 0.425f) < 1e-3f);
  5438. CHECK (lynx::controllerValue (a, 1.0, -1.0) > 0.99f); // no sustain: runs to the end and stays
  5439. a.sustain = 1;
  5440. CHECK (std::abs (lynx::controllerValue (a, 1.0, -1.0) - 0.8f) < 1e-3f); // held at sustain
  5441. CHECK (lynx::controllerValue (a, 1.0, 0.5) > 0.99f); // released: continues to the end
  5442. a.bipolar = true;
  5443. a.base = 0.5f;
  5444. a.sustain = -1;
  5445. CHECK (std::abs (lynx::controllerValue (a, 0.0, -1.0) - 0.0f) < 1e-4f); // bipolar: 0.5 + (0 − 0.5)
  5446. }
  5447. TEST (peak_controller_drives_live_automation)
  5448. {
  5449. // track 1: source (DC 0.5) with Peak Controller; track 2: DC 0.5 with volume linked to the controller
  5450. auto render = [] (bool linked) {
  5451. lynx::Song s;
  5452. s.mixer = lynx::Song::defaultMixer (2);
  5453. for (int t = 1; t <= 2; ++t)
  5454. {
  5455. lynx::ChannelDef c;
  5456. c.instrument = lynx::InstrumentKind::sampler;
  5457. c.samplePath = "dc";
  5458. c.volume = 1.0f;
  5459. c.mixerTrack = t;
  5460. s.channels.push_back (c);
  5461. }
  5462. s.patterns.emplace_back();
  5463. auto peak = lynx::makeEffectSlot (lynx::EffectKind::peakController);
  5464. peak.params[0] = 1.0f; // base 1
  5465. peak.params[1] = 0.0f; // amount −1: 1 − peak
  5466. peak.params[2] = 0.5f; // no tension
  5467. s.mixer[1].effects.push_back (peak);
  5468. if (linked)
  5469. {
  5470. lynx::Automation a;
  5471. a.name = "Peak";
  5472. a.target = { lynx::AutomationTargetKind::mixerVolume, 2 };
  5473. a.source = { 1, 0, 0 };
  5474. s.automations.push_back (a);
  5475. }
  5476. lynx::RackState::Factories f;
  5477. f.samples = [] (const std::string&) {
  5478. auto d = std::make_shared<lynx::SampleData>();
  5479. d->left.assign (48000, 0.5f);
  5480. return std::shared_ptr<const lynx::SampleData> (d);
  5481. };
  5482. auto state = lynx::RackState::build (s, nullptr, 48000.0, f);
  5483. lynx::RackRenderer r;
  5484. r.prepare (48000.0, 512);
  5485. lynx::LiveEventQueue live;
  5486. for (int ch = 0; ch < 2; ++ch)
  5487. {
  5488. lynx::NoteEvent on;
  5489. on.channel = ch;
  5490. on.velocity = 1.0f;
  5491. live.push (on);
  5492. }
  5493. std::vector<float> l (4800), rr (4800);
  5494. r.process (state.get(), false, 0.0, 4800, l.data(), rr.data(), &live);
  5495. return l[4000];
  5496. };
  5497. const auto free = render (false), ducked = render (true);
  5498. CHECK (std::isfinite (ducked) && ducked > 0.0f);
  5499. CHECK (ducked < free - 0.05f); // the 0.5 peak on track 1 lowers track 2's volume
  5500. }
  5501. TEST (effect_slot_dry_wet_and_smart_disable)
  5502. {
  5503. lynx::Song s;
  5504. s.mixer = lynx::Song::defaultMixer (1);
  5505. lynx::ChannelDef c;
  5506. c.instrument = lynx::InstrumentKind::sampler;
  5507. c.samplePath = "dc";
  5508. c.volume = 1.0f;
  5509. c.mixerTrack = 1;
  5510. s.channels.push_back (c);
  5511. s.patterns.emplace_back();
  5512. lynx::EffectSlot fx;
  5513. fx.kind = lynx::EffectKind::plugin;
  5514. fx.plugin.id = "inversor";
  5515. fx.mix = 0.25f; // 75% dry (+0.5) and 25% wet (-0.5) = +0.25
  5516. s.mixer[1].effects.push_back (fx);
  5517. auto inverter = std::make_shared<CountingInverter>();
  5518. lynx::RackState::Factories f;
  5519. f.samples = [] (const std::string&) {
  5520. auto d = std::make_shared<lynx::SampleData>();
  5521. d->left.assign (4800, 0.5f);
  5522. return std::shared_ptr<const lynx::SampleData> (d);
  5523. };
  5524. f.pluginEffects = [inverter] (const lynx::PluginRef&, double) -> std::shared_ptr<lynx::Effect> { return inverter; };
  5525. auto state = lynx::RackState::build (s, nullptr, 48000.0, f);
  5526. lynx::RackRenderer r;
  5527. r.prepare (48000.0, 512);
  5528. lynx::LiveEventQueue live;
  5529. lynx::NoteEvent on;
  5530. on.velocity = 1.0f;
  5531. live.push (on);
  5532. std::vector<float> l (480), rr (480);
  5533. r.process (state.get(), false, 0.0, 480, l.data(), rr.data(), &live);
  5534. CHECK (std::abs (l[100] - 0.25f) < 1e-4f);
  5535. // after the sample ends: 4 s of silence and the plugin stops being called
  5536. for (int i = 0; i < 100 * 6; ++i) // 6 s
  5537. r.process (state.get(), false, 0.0, 480, l.data(), rr.data());
  5538. const auto callsAfterSilence = inverter->calls;
  5539. for (int i = 0; i < 100; ++i)
  5540. r.process (state.get(), false, 0.0, 480, l.data(), rr.data());
  5541. CHECK (inverter->calls == callsAfterSilence);
  5542. CHECK (callsAfterSilence < 100 * 6);
  5543. // sound again: processing resumes in the same block
  5544. live.push (on);
  5545. r.process (state.get(), false, 0.0, 480, l.data(), rr.data(), &live);
  5546. CHECK (inverter->calls == callsAfterSilence + 1 && std::abs (l[100] - 0.25f) < 1e-4f);
  5547. }
  5548. TEST (swing_delays_offbeat_sixteenths)
  5549. {
  5550. CHECK (lynx::swingDelay (0, 1.0f) == 0);
  5551. CHECK (lynx::swingDelay (lynx::ticksPerStep, 1.0f) == lynx::ticksPerStep / 2);
  5552. CHECK (lynx::swingDelay (lynx::ticksPerStep * 2, 1.0f) == 0);
  5553. CHECK (lynx::swingDelay (lynx::ticksPerStep + 10, 1.0f) == (lynx::ticksPerStep - 20) / 2); // off the grid: triangular weight (FL)
  5554. CHECK (lynx::swingDelay (lynx::ticksPerStep, 1.0f, 0.5f) == lynx::ticksPerStep / 4); // channel mix
  5555. CHECK (lynx::swingDelay (lynx::ticksPerStep * 3, 0.5f) == lynx::ticksPerStep / 4);
  5556. lynx::Pattern p;
  5557. p.notes = { { 0, 100, 0, 60 }, { lynx::ticksPerStep, 100, 0, 62 } };
  5558. lynx::EventBuffer ev;
  5559. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  5560. lynx::schedulePatternLoop (p, p.effectiveLength (lynx::Meter (4, 4)), 0.0, 12000, tps, ev, 1.0f);
  5561. int onOffset = -1;
  5562. for (auto& e : ev)
  5563. if (e.type == lynx::NoteEvent::Type::on && e.key == 62)
  5564. onOffset = e.sampleOffset;
  5565. CHECK (onOffset == (int) std::floor ((lynx::ticksPerStep * 1.5) / tps));
  5566. }
  5567. TEST (sampler_settings_pitch_reverse_envelope_cut)
  5568. {
  5569. auto ramp = std::make_shared<lynx::SampleData>();
  5570. for (int i = 0; i < 48000; ++i)
  5571. ramp->left.push_back ((float) i / 48000.0f); // ramp: the value tells where the read position is
  5572. ramp->sampleRate = 48000.0;
  5573. auto play = [&] (const lynx::SamplerSettings& s, int key, int frames, bool releaseAfterHalf) {
  5574. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  5575. inst->prepare (48000.0);
  5576. lynx::ChannelDef c;
  5577. c.sampler = s;
  5578. inst->configure (c, 120.0);
  5579. inst->noteOn (key, 1.0f);
  5580. std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
  5581. inst->render (l.data(), r.data(), frames / 2, 1.0f, 1.0f);
  5582. if (releaseAfterHalf)
  5583. inst->noteOff (key);
  5584. inst->render (l.data() + frames / 2, r.data() + frames / 2, frames - frames / 2, 1.0f, 1.0f);
  5585. return l;
  5586. };
  5587. // +12 semitones: reads twice as fast
  5588. lynx::SamplerSettings up;
  5589. up.pitch = 12.0f;
  5590. CHECK (std::abs (play (up, 60, 1000, false)[999] - 2.0f * 999.0f / 48000.0f) < 1e-3f);
  5591. // reversed: starts from the end
  5592. lynx::SamplerSettings back;
  5593. back.reverse = true;
  5594. CHECK (play (back, 60, 100, false)[10] > 0.99f);
  5595. // 10 ms attack: starts silent and reaches full level
  5596. lynx::SamplerSettings slow;
  5597. slow.attack = 0.01f;
  5598. const auto a = play (slow, 60, 2000, false);
  5599. CHECK (a[10] < 0.2f * (10.0f / 48000.0f) + 1e-6f && std::abs (a[1500] - 1500.0f / 48000.0f) < 1e-4f);
  5600. // 5 ms release: gone right after release; without release it plays in full
  5601. lynx::SamplerSettings rel;
  5602. rel.release = 0.005f;
  5603. CHECK (play (rel, 60, 2000, true)[1999] == 0.0f);
  5604. CHECK (play (lynx::SamplerSettings {}, 60, 2000, true)[1999] > 0.0f);
  5605. // cut itself: the second note cuts the first (one voice remains)
  5606. lynx::SamplerSettings cut;
  5607. cut.cut = true;
  5608. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  5609. inst->prepare (48000.0);
  5610. lynx::ChannelDef cutChannel;
  5611. cutChannel.sampler = cut;
  5612. inst->configure (cutChannel, 120.0);
  5613. inst->noteOn (60, 1.0f);
  5614. std::vector<float> l (2000), r (2000);
  5615. inst->render (l.data(), r.data(), 1000, 1.0f, 1.0f);
  5616. inst->noteOn (60, 1.0f);
  5617. std::fill (l.begin(), l.end(), 0.0f);
  5618. inst->render (l.data(), r.data(), 2000, 1.0f, 1.0f);
  5619. CHECK (std::abs (l[1999] - 1999.0f / 48000.0f) < 1e-4f); // only the new note
  5620. }
  5621. TEST (playlist_split_and_track_mute)
  5622. {
  5623. lynx::Clip c { 0, 3, 960, 3840, 480 };
  5624. const auto rest = lynx::splitClip (c, 2000);
  5625. CHECK (c.start == 960 && c.length == 1040 && c.offset == 480);
  5626. CHECK (rest.start == 2000 && rest.length == 2800 && rest.offset == 480 + 1040 && rest.track == 3);
  5627. auto s = lynx::Song::makeDefault();
  5628. s.mode = lynx::PlayMode::song;
  5629. s.playlist = { { 0, 0, 0, 3840, 0 } };
  5630. lynx::EventBuffer ev;
  5631. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  5632. lynx::scheduleSong (s, 0.0, 48000, tps, ev);
  5633. CHECK (ev.size() > 0);
  5634. s.playlistTracks.resize (1);
  5635. s.playlistTracks[0].muted = true;
  5636. ev.clear();
  5637. lynx::scheduleSong (s, 0.0, 48000, tps, ev);
  5638. CHECK (ev.size() == 0);
  5639. }
  5640. TEST (note_pan_pitch_and_mute)
  5641. {
  5642. lynx::Pattern p;
  5643. lynx::Note a { 0, 240, 0, 60 };
  5644. a.pan = -1.0f;
  5645. a.finePitch = 50.0f;
  5646. lynx::Note b { 240, 240, 0, 62 };
  5647. b.muted = true;
  5648. p.notes = { a, b };
  5649. lynx::EventBuffer ev;
  5650. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  5651. lynx::schedulePatternLoop (p, p.effectiveLength (lynx::Meter (4, 4)), 0.0, 48000, tps, ev);
  5652. int ons = 0;
  5653. for (auto& e : ev)
  5654. if (e.type == lynx::NoteEvent::Type::on)
  5655. {
  5656. ++ons;
  5657. CHECK (e.key == 60 && e.pan == -1.0f && e.cents == 50.0f);
  5658. }
  5659. CHECK (ons == 1); // the muted one doesn't play
  5660. // note pan in the sampler: fully left
  5661. auto dc = std::make_shared<lynx::SampleData>();
  5662. dc->left.assign (4800, 0.5f);
  5663. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, dc);
  5664. inst->prepare (48000.0);
  5665. inst->setNextNoteExpression (-1.0f, 0.0f);
  5666. inst->noteOn (60, 1.0f);
  5667. std::vector<float> l (100), r (100);
  5668. inst->render (l.data(), r.data(), 100, 1.0f, 1.0f);
  5669. CHECK (l[50] > 0.6f && std::abs (r[50]) < 1e-6f);
  5670. }
  5671. TEST (tempo_automation_changes_speed_and_duration)
  5672. {
  5673. auto song = lynx::Song::makeDefault();
  5674. song.bpm = 120.0;
  5675. song.mode = lynx::PlayMode::song;
  5676. const auto bar = (std::int64_t) lynx::ppq * 4;
  5677. song.playlist = { { 0, 0, 0, bar, 0 }, { 0, 0, bar, bar, 0 } };
  5678. lynx::Automation tempo;
  5679. tempo.target = { lynx::AutomationTargetKind::tempo, 0 };
  5680. const auto v120 = lynx::valueFromTempo (120.0), v240 = lynx::valueFromTempo (240.0);
  5681. tempo.points = { { 0, v120 }, { bar - 1, v120 }, { bar, v240 } }; // step: 120 -> 240 at bar 2
  5682. song.automations.push_back (tempo);
  5683. lynx::Clip clip;
  5684. clip.kind = lynx::ClipKind::automation;
  5685. clip.automation = 0;
  5686. clip.track = 1;
  5687. clip.length = bar * 2;
  5688. song.playlist.push_back (clip);
  5689. CHECK (std::abs (song.tempoAt (100.0) - 120.0) < 0.01 && std::abs (song.tempoAt ((double) bar + 100.0) - 240.0) < 0.01);
  5690. // 1 bar at 120 (2 s) + 1 at 240 (1 s)
  5691. CHECK (std::abs (song.secondsForTicks (bar * 2) - 3.0) < 0.01);
  5692. auto state = lynx::RackState::build (song, nullptr, 48000.0);
  5693. CHECK (state->tempoAutomation == 0);
  5694. lynx::RackRenderer r;
  5695. r.prepare (48000.0, 512);
  5696. std::vector<float> l (512), rr (512);
  5697. r.process (state.get(), true, 10.0, 512, l.data(), rr.data());
  5698. CHECK (std::abs (r.currentBpm() - 120.0) < 0.01);
  5699. r.process (state.get(), true, (double) bar + 10.0, 512, l.data(), rr.data());
  5700. CHECK (std::abs (r.currentBpm() - 240.0) < 0.01);
  5701. }
  5702. TEST (bounce_insert_song_and_apply)
  5703. {
  5704. auto s = lynx::Song::makeDefault();
  5705. s.setRoute (3, 7, true, 0.5f);
  5706. s.mixer[0].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::reverb));
  5707. s.mixer[2].solo = true;
  5708. const auto b = lynx::makeBounceSong (s, 3);
  5709. CHECK (b.mixer[3].solo && ! b.mixer[2].solo && b.mixer[3].toMaster && b.mixer[3].sends.empty());
  5710. CHECK (b.mixer[0].effects.empty() && b.mode == lynx::PlayMode::song);
  5711. const auto audible = b.soloAudible();
  5712. CHECK (audible[3] && ! audible[1] && ! audible[7]);
  5713. const auto channels = s.channels.size();
  5714. const auto inserts = s.mixer.size();
  5715. const auto created = lynx::applyBounce (s, 3, "C:/x.wav", 3840);
  5716. CHECK (created == (int) inserts && s.mixer.size() == inserts + 1 && s.mixer[3].muted);
  5717. CHECK (s.mixer[(std::size_t) created].sends.size() == 1 && s.mixer[(std::size_t) created].sends[0].target == 7);
  5718. CHECK (s.channels.size() == channels + 1 && s.channels.back().mixerTrack == created);
  5719. CHECK (s.playlist.back().kind == lynx::ClipKind::audio && s.playlist.back().length == 3840);
  5720. }
  5721. TEST (mixer_stereo_separation_phase_swap)
  5722. {
  5723. // FL's level matrix (REA_rodada2 §2.2, REA_rodada167j §1): outL = ll·inL + rl·inR, outR = rr·inR + lr·inL
  5724. lynx::MixerTrack t;
  5725. auto near = [] (float a, float b) { return std::abs (a - b) < 1e-5f; };
  5726. t.stereo = 1.0f; // mono
  5727. auto m = lynx::mixerMatrix (t, lynx::unityFader, 0.0f, false);
  5728. CHECK (near (m.ll, 0.5f) && near (m.rl, 0.5f) && near (m.rr, 0.5f) && near (m.lr, 0.5f));
  5729. t.stereo = -1.0f; // doubled side: 1.5 L - 0.5 R
  5730. m = lynx::mixerMatrix (t, lynx::unityFader, 0.0f, false);
  5731. CHECK (near (m.ll, 1.5f) && near (m.rl, -0.5f) && near (m.rr, 1.5f) && near (m.lr, -0.5f));
  5732. t.stereo = 0.0f;
  5733. t.swapChannels = true;
  5734. t.invertPhase = true;
  5735. m = lynx::mixerMatrix (t, lynx::unityFader, 0.0f, false);
  5736. CHECK (m.ll == 0.0f && m.rl == -1.0f && m.rr == 0.0f && m.lr == -1.0f); // outL = −inR, outR = −inL
  5737. // unity, centre, no separation: exactly the identity (FL's fast path)
  5738. m = lynx::mixerMatrix (lynx::MixerTrack {}, lynx::unityFader, 0.0f, false);
  5739. CHECK (m == lynx::MixerMatrix {});
  5740. }
  5741. TEST (mixer_pan_fold_and_panning_law_match_fl_render)
  5742. {
  5743. // FL 2026's render of j_tri.flp (round 167j: a 441 Hz left / 882 Hz right sample on ten inserts, pan law
  5744. // triangular), the matrix fitted per insert: {pan, separation, ll, rl, lr, rr}
  5745. struct Case { float pan, sep, ll, rl, lr, rr; };
  5746. 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 },
  5747. { 0.25f, 0.0f, 0.75f, 0.0f, 0.125001f, 0.874978f }, { 0.5f, 0.0f, 0.499985f, 0.0f, 0.250011f, 0.749964f },
  5748. { 1.0f, 0.0f, 0.0f, 0.0f, 0.499982f, 0.499972f }, { 0.5f, 0.5f, 0.422756f, 0.077253f, 0.4045f, 0.595469f },
  5749. { -0.5f, -0.5f, 1.0f, 0.0f, -0.125004f, 0.624974f }, { 0.75f, 0.0f, 0.250003f, 0.0f, 0.375006f, 0.624975f },
  5750. { 0.0f, 0.75f, 0.70703f, 0.292941f, 0.292965f, 0.707045f } };
  5751. for (const auto& c : fl)
  5752. {
  5753. lynx::MixerTrack t;
  5754. t.stereo = c.sep;
  5755. const auto m = lynx::mixerMatrix (t, lynx::unityFader, c.pan, true);
  5756. // the fit's own error is ~5e-5 (16-bit render)
  5757. 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
  5758. && std::abs (m.rr - c.rr) < 2e-4f);
  5759. }
  5760. // circular (the default): +3 dB on the side panned to, and the folded half of the other side
  5761. const auto hard = lynx::mixerMatrix (lynx::MixerTrack {}, lynx::unityFader, 1.0f, false);
  5762. CHECK (std::abs (hard.ll) < 1e-6f && std::abs (hard.rl) < 1e-6f);
  5763. CHECK (std::abs (hard.rr - 0.70710677f) < 1e-6f && std::abs (hard.lr - 0.70710677f) < 1e-6f);
  5764. const auto half = lynx::mixerMatrix (lynx::MixerTrack {}, lynx::unityFader, 0.5f, false);
  5765. 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;
  5766. CHECK (std::abs (half.ll - gl) < 1e-5f && half.rl == 0.0f);
  5767. 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);
  5768. // the fader scales the whole matrix (−6 dB-ish position), polarity negates it
  5769. lynx::MixerTrack inv;
  5770. inv.invertPhase = true;
  5771. const auto low = lynx::mixerMatrix (inv, 0.5f, 0.0f, false);
  5772. const auto g = (float) ((std::exp ((double) (0.625f * 2.3978953f)) - 1.0) * 0.1);
  5773. CHECK (std::abs (low.ll + g) < 1e-5f && std::abs (low.rr + g) < 1e-5f && std::abs (low.rl) < 1e-6f);
  5774. }
  5775. TEST (effects_limiter_filter_distortion_chorus)
  5776. {
  5777. const double sr = 48000.0;
  5778. auto sine = [] (float hz, float amp, int n) {
  5779. std::vector<float> v ((std::size_t) n);
  5780. for (int i = 0; i < n; ++i)
  5781. v[(std::size_t) i] = amp * std::sin (2.0f * 3.14159265f * hz * (float) i / 48000.0f);
  5782. return v;
  5783. };
  5784. auto peak = [] (const std::vector<float>& v, int from) {
  5785. float p = 0.0f;
  5786. for (std::size_t i = (std::size_t) from; i < v.size(); ++i)
  5787. p = std::max (p, std::abs (v[i]));
  5788. return p;
  5789. };
  5790. // limiter: +12 dB on a 0.9 sine never exceeds the ceiling (-1 dB) and reports the latency
  5791. auto lim = lynx::makeEffect (lynx::EffectKind::limiter);
  5792. lim->prepare (sr);
  5793. auto slot = lynx::makeEffectSlot (lynx::EffectKind::limiter);
  5794. slot.params[0] = 11.0f / 12.0f; // -1 dB
  5795. slot.params[1] = 0.5f; // +12 dB
  5796. auto l = sine (100.0f, 0.9f, 48000), r = l;
  5797. lim->process (l.data(), r.data(), 48000, slot.params, 120.0);
  5798. CHECK (peak (l, 0) <= std::pow (10.0f, -1.0f / 20.0f) + 1e-4f);
  5799. CHECK (peak (l, 4800) > 0.8f);
  5800. CHECK (lim->latencySamples() == 72);
  5801. // low-pass filter at 500 Hz knocks down an 8 kHz sine
  5802. auto lp = lynx::makeEffect (lynx::EffectKind::filter);
  5803. lp->prepare (sr);
  5804. auto fs = lynx::makeEffectSlot (lynx::EffectKind::filter);
  5805. fs.params[1] = std::log (500.0f / 20.0f) / std::log (1000.0f);
  5806. l = sine (8000.0f, 1.0f, 9600);
  5807. r = l;
  5808. lp->process (l.data(), r.data(), 9600, fs.params, 120.0);
  5809. CHECK (peak (l, 4800) < 0.05f);
  5810. // distortion and chorus: finite, audible output
  5811. for (auto kind : { lynx::EffectKind::distortion, lynx::EffectKind::chorus })
  5812. {
  5813. auto fx = lynx::makeEffect (kind);
  5814. fx->prepare (sr);
  5815. auto ps = lynx::makeEffectSlot (kind);
  5816. l = sine (220.0f, 0.5f, 9600);
  5817. r = l;
  5818. fx->process (l.data(), r.data(), 9600, ps.params, 120.0);
  5819. bool finite = true;
  5820. for (auto v : l)
  5821. finite = finite && std::isfinite (v);
  5822. CHECK (finite && peak (l, 2400) > 0.1f && peak (l, 0) < 2.0f);
  5823. }
  5824. }
  5825. TEST (slide_notes_glide_without_retrigger)
  5826. {
  5827. // sampler with a ramp: the read speed doubles after a one-octave slide
  5828. auto ramp = std::make_shared<lynx::SampleData>();
  5829. for (int i = 0; i < 96000; ++i)
  5830. ramp->left.push_back ((float) i / 96000.0f);
  5831. ramp->sampleRate = 48000.0;
  5832. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  5833. inst->prepare (48000.0);
  5834. inst->noteOn (60, 1.0f);
  5835. std::vector<float> l (2000), r (2000);
  5836. inst->render (l.data(), r.data(), 1000, 1.0f, 1.0f);
  5837. const auto stepBefore = l[999] - l[998];
  5838. inst->slideTo (72, 0.0f, 100.0f);
  5839. std::fill (l.begin(), l.end(), 0.0f);
  5840. inst->render (l.data(), r.data(), 1000, 1.0f, 1.0f);
  5841. const auto stepAfter = l[999] - l[998];
  5842. CHECK (std::abs (stepAfter / stepBefore - 2.0f) < 0.01f);
  5843. // the sequencer sends "slide" (no off) for the slide note
  5844. lynx::Pattern p;
  5845. lynx::Note base { 0, 960, 0, 48 };
  5846. lynx::Note glide { 480, 240, 0, 55 };
  5847. glide.slide = true;
  5848. p.notes = { base, glide };
  5849. lynx::EventBuffer ev;
  5850. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  5851. lynx::schedulePatternLoop (p, p.effectiveLength (lynx::Meter (4, 4)), 0.0, 30000, tps, ev);
  5852. int ons = 0, offs = 0, slides = 0;
  5853. for (auto& e : ev)
  5854. {
  5855. ons += e.type == lynx::NoteEvent::Type::on;
  5856. offs += e.type == lynx::NoteEvent::Type::off;
  5857. if (e.type == lynx::NoteEvent::Type::slide)
  5858. {
  5859. ++slides;
  5860. CHECK (e.key == 55 && std::abs (e.slideSamples - 240.0f / (float) tps) < 1.0f);
  5861. }
  5862. }
  5863. CHECK (ons == 1 && offs == 1 && slides == 1);
  5864. }
  5865. TEST (synth_fm_phase_modulation)
  5866. {
  5867. auto render = [] (const lynx::SynthSettings& s) {
  5868. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  5869. inst->prepare (48000.0);
  5870. lynx::ChannelDef c;
  5871. c.synth = s;
  5872. inst->configure (c, 120.0);
  5873. inst->noteOn (69, 1.0f); // 440 Hz
  5874. std::vector<float> l (48000), r (48000);
  5875. inst->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
  5876. return l;
  5877. };
  5878. // energy (Goertzel) at frequency f over frames [from, to)
  5879. auto energy = [] (const std::vector<float>& x, double f, int from, int to) {
  5880. const auto w = 2.0 * 3.141592653589793 * f / 48000.0, k = 2.0 * std::cos (w);
  5881. double s1 = 0.0, s2 = 0.0;
  5882. for (int i = from; i < to; ++i)
  5883. {
  5884. const auto s0 = x[(std::size_t) i] + k * s1 - s2;
  5885. s2 = s1;
  5886. s1 = s0;
  5887. }
  5888. return s1 * s1 + s2 * s2 - k * s1 * s2;
  5889. };
  5890. lynx::SynthSettings plain;
  5891. plain.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  5892. plain.osc[2] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  5893. plain.cutoff = 20000.0f;
  5894. auto fm = plain;
  5895. fm.fm = 1.0f;
  5896. const auto a = render (plain), b = render (fm);
  5897. // no FM: osc 3 adds (same pitch) and there's no 2nd harmonic; with FM the sidebands appear
  5898. CHECK (energy (a, 880.0, 4800, 24000) < 1e-3 * energy (a, 440.0, 4800, 24000));
  5899. CHECK (energy (b, 880.0, 4800, 24000) > 0.1 * energy (b, 440.0, 4800, 24000));
  5900. CHECK (energy (b, 1320.0, 4800, 24000) > 0.01 * energy (b, 440.0, 4800, 24000));
  5901. for (auto v : b)
  5902. CHECK (std::isfinite (v) && std::abs (v) < 1.0f);
  5903. // index decaying to 0: the start is bright, the end is nearly a sine again
  5904. auto decaying = fm;
  5905. decaying.fmDecay = 0.1f;
  5906. decaying.fmSustain = 0.0f;
  5907. const auto c = render (decaying);
  5908. const auto early = energy (c, 880.0, 0, 2400) / energy (c, 440.0, 0, 2400);
  5909. const auto late = energy (c, 880.0, 36000, 48000) / energy (c, 440.0, 36000, 48000);
  5910. CHECK (early > 0.05 && late < 1e-3);
  5911. }
  5912. TEST (synth_three_oscillators_adsr_filter)
  5913. {
  5914. auto render = [] (const lynx::SynthSettings& s, int frames, int releaseAt) {
  5915. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  5916. inst->prepare (48000.0);
  5917. lynx::ChannelDef c;
  5918. c.synth = s;
  5919. inst->configure (c, 120.0);
  5920. inst->noteOn (69, 1.0f); // A4 = 440 Hz
  5921. std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
  5922. inst->render (l.data(), r.data(), releaseAt, 1.0f, 1.0f);
  5923. inst->noteOff (69);
  5924. inst->render (l.data() + releaseAt, r.data() + releaseAt, frames - releaseAt, 1.0f, 1.0f);
  5925. return l;
  5926. };
  5927. auto peakIn = [] (const std::vector<float>& v, int from, int to) {
  5928. float p = 0.0f;
  5929. for (int i = from; i < to; ++i)
  5930. p = std::max (p, std::abs (v[(std::size_t) i]));
  5931. return p;
  5932. };
  5933. // default: sounds and fades after the release
  5934. const lynx::SynthSettings standard;
  5935. const auto a = render (standard, 48000, 24000);
  5936. CHECK (peakIn (a, 1000, 20000) > 0.05f && peakIn (a, 40000, 48000) < 1e-3f);
  5937. // pure sine with an open filter: zero-crossing count gives ~440 Hz
  5938. lynx::SynthSettings sine;
  5939. sine.osc[0].wave = lynx::Waveform::sine;
  5940. sine.cutoff = 20000.0f;
  5941. const auto b = render (sine, 48000, 47000);
  5942. int crossings = 0;
  5943. for (int i = 4800; i < 4800 + 48000 / 2; ++i)
  5944. crossings += (b[(std::size_t) i - 1] < 0.0f) != (b[(std::size_t) i] < 0.0f);
  5945. CHECK (std::abs (crossings - 440) < 6); // half a second = 220 cycles = 440 crossings
  5946. float sinePeak = 0.0f;
  5947. for (int i = 4800; i < 9600; ++i)
  5948. sinePeak = std::max (sinePeak, std::abs (b[(std::size_t) i]));
  5949. CHECK (sinePeak > 0.2f && sinePeak < 0.35f); // 0.3 level, approximated sine without clipping
  5950. // second oscillator an octave up adds energy; sustain 0 makes the note decay on its own
  5951. lynx::SynthSettings two = sine;
  5952. two.osc[1] = { lynx::Waveform::sine, 1, 0.0f, 1.0f };
  5953. two.sustain = 0.0f;
  5954. two.decay = 0.1f;
  5955. const auto c = render (two, 48000, 47000);
  5956. CHECK (peakIn (c, 100, 2000) > peakIn (b, 100, 2000) && peakIn (c, 30000, 40000) < 1e-3f);
  5957. }
  5958. TEST (set_target_value_round_trips)
  5959. {
  5960. auto s = lynx::Song::makeDefault();
  5961. s.mixer[2].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::delay));
  5962. const lynx::AutomationTarget targets[] = {
  5963. { lynx::AutomationTargetKind::channelVolume, 1 }, { lynx::AutomationTargetKind::channelPan, 2 },
  5964. { lynx::AutomationTargetKind::mixerVolume, 3 }, { lynx::AutomationTargetKind::mixerPan, 0 },
  5965. { lynx::AutomationTargetKind::effectParam, 2, 0, 1 }, { lynx::AutomationTargetKind::tempo, 0 },
  5966. };
  5967. for (const auto& t : targets)
  5968. {
  5969. CHECK (s.setTargetValue (t, 0.25f));
  5970. CHECK (std::abs (s.targetValue (t) - 0.25f) < 1e-5f);
  5971. }
  5972. CHECK (! s.setTargetValue ({ lynx::AutomationTargetKind::channelParam, 0, 0, 3 }, 0.5f)); // lives in the plugin
  5973. }
  5974. TEST (cut_groups_stretch_and_envelope_import)
  5975. {
  5976. // open hat (cut by 1) is cut when the closed one (group 1) plays
  5977. lynx::Song s;
  5978. s.mixer = lynx::Song::defaultMixer (2);
  5979. for (int i = 0; i < 2; ++i)
  5980. {
  5981. lynx::ChannelDef c;
  5982. c.instrument = lynx::InstrumentKind::sampler;
  5983. c.samplePath = "dc";
  5984. c.volume = 1.0f;
  5985. c.mixerTrack = i + 1;
  5986. s.channels.push_back (c);
  5987. }
  5988. s.channels[0].cutBy = 1; // open
  5989. s.channels[1].cutGroup = 1; // closed
  5990. auto& p = s.patterns.emplace_back();
  5991. p.notes = { { 0, 100, 0, 60 }, { 960, 100, 1, 60 } };
  5992. auto provider = [] (const std::string&) {
  5993. auto d = std::make_shared<lynx::SampleData>();
  5994. d->left.assign (96000, 0.5f);
  5995. return std::shared_ptr<const lynx::SampleData> (d);
  5996. };
  5997. auto state = lynx::RackState::build (s, nullptr, 48000.0, provider);
  5998. CHECK (state->hasCutGroups && state->cutTargets[1].size() == 1 && state->cutTargets[1][0] == 0);
  5999. lynx::RackRenderer r;
  6000. r.prepare (48000.0, 512);
  6001. std::vector<float> l (512), rr (512);
  6002. const auto tps = lynx::ticksPerSample (s.bpm, 48000.0);
  6003. float afterCut = 0.0f;
  6004. for (double tick = 0.0; tick < 960.0 * 3; tick += 512 * tps)
  6005. {
  6006. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  6007. if (tick > 960.0 * 2) // the closed one continues; the open one has stopped
  6008. afterCut = std::max (afterCut, std::abs (l[256]));
  6009. }
  6010. CHECK (std::abs (afterCut - 0.5f * 0.78f) < 0.01f); // only the closed one (default velocity 0.78)
  6011. // resampling stretch: 1 s of sample stretched to 4 beats at 120 BPM = 2 s
  6012. auto ramp = std::make_shared<lynx::SampleData>();
  6013. for (int i = 0; i < 48000; ++i)
  6014. ramp->left.push_back ((float) i / 48000.0f);
  6015. ramp->sampleRate = 48000.0;
  6016. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  6017. inst->prepare (48000.0);
  6018. lynx::ChannelDef c;
  6019. c.sampler.stretchBeats = 4.0f;
  6020. inst->configure (c, 120.0);
  6021. inst->noteOn (60, 1.0f);
  6022. std::vector<float> a (48000), b (48000);
  6023. inst->render (a.data(), b.data(), 48000, 1.0f, 1.0f);
  6024. CHECK (std::abs (a[47999] - 0.5f) < 0.01f); // after 1 s it has read half
  6025. // FL's Resample: the key still transposes, like tape (round 162, issue #9): an octave up reads twice as fast
  6026. inst->allNotesOff();
  6027. std::fill (a.begin(), a.end(), 0.0f);
  6028. inst->render (a.data(), b.data(), 4800, 1.0f, 1.0f);
  6029. std::fill (a.begin(), a.end(), 0.0f);
  6030. inst->noteOn (72, 1.0f);
  6031. inst->render (a.data(), b.data(), 24000, 1.0f, 1.0f);
  6032. CHECK (std::abs (a[23999] - 0.5f) < 0.01f);
  6033. // FL envelope time: 32950 = 2 s; < 101 = 0
  6034. CHECK (std::abs (flp::Channel::envelopeTime (32950) - 2.0) < 0.01 && flp::Channel::envelopeTime (50) == 0.0);
  6035. }
  6036. TEST (time_stretch_keeps_pitch)
  6037. {
  6038. lynx::SampleData sine;
  6039. sine.sampleRate = 48000.0;
  6040. for (int i = 0; i < 48000; ++i)
  6041. sine.left.push_back (0.5f * std::sin (2.0f * 3.14159265f * 440.0f * (float) i / 48000.0f));
  6042. auto crossingsPerSecond = [] (const lynx::SampleData& d) {
  6043. int c = 0;
  6044. const auto n = (int) d.frames();
  6045. for (int i = 4800; i < n - 4800; ++i)
  6046. c += (d.left[(std::size_t) i - 1] < 0.0f) != (d.left[(std::size_t) i] < 0.0f);
  6047. return (double) c / ((double) (n - 9600) / d.sampleRate);
  6048. };
  6049. for (double ratio : { 2.0, 0.5, 1.37 })
  6050. {
  6051. const auto out = lynx::timeStretch (sine, ratio);
  6052. CHECK (std::abs ((double) out->frames() - 48000.0 * ratio) < 2.0);
  6053. CHECK (std::abs (crossingsPerSecond (*out) - 880.0) < 15.0); // 440 Hz stays 440 Hz
  6054. float peak = 0.0f;
  6055. for (std::size_t i = 4800; i + 4800 < out->frames(); ++i)
  6056. peak = std::max (peak, std::abs (out->left[i]));
  6057. CHECK (peak > 0.4f && peak < 0.6f); // no gaps or clipping in the overlap
  6058. }
  6059. }
  6060. TEST (sampler_region_start_and_end)
  6061. {
  6062. auto ramp = std::make_shared<lynx::SampleData>();
  6063. for (int i = 0; i < 10000; ++i)
  6064. ramp->left.push_back ((float) i / 10000.0f);
  6065. ramp->sampleRate = 48000.0;
  6066. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  6067. inst->prepare (48000.0);
  6068. lynx::ChannelDef c;
  6069. c.sampler.regionStart = 0.25f;
  6070. c.sampler.regionEnd = 0.5f;
  6071. inst->configure (c, 120.0);
  6072. inst->noteOn (60, 1.0f);
  6073. std::vector<float> l (6000), r (6000);
  6074. inst->render (l.data(), r.data(), 6000, 1.0f, 1.0f);
  6075. CHECK (std::abs (l[0] - 0.25f) < 0.001f); // starts at the beginning of the slice
  6076. CHECK (l[2400] > 0.48f && l[2600] == 0.0f); // and stops at its end
  6077. }
  6078. TEST (arpeggiator_plays_held_chord_in_steps)
  6079. {
  6080. auto song = lynx::Song::makeDefault();
  6081. song.bpm = 120.0;
  6082. song.patterns[0].notes.clear();
  6083. for (int key : { 60, 64, 67 })
  6084. song.patterns[0].notes.push_back ({ 0, lynx::ppq * 4, 4, key, 0.8f }); // chord held for 1 bar
  6085. song.channels[4].arp.direction = 1; // up
  6086. song.channels[4].arp.stepBeats = 0.25f;
  6087. auto state = lynx::RackState::build (song, nullptr, 48000.0);
  6088. CHECK (state->arpeggiators[4] != nullptr && state->arpeggiators[0] == nullptr);
  6089. // the arpeggiator generates the notes; we count what it hands to the instrument
  6090. auto& arp = *state->arpeggiators[4];
  6091. const auto tps = lynx::ticksPerSample (song.bpm, 48000.0);
  6092. std::vector<int> keys;
  6093. lynx::EventBuffer ev;
  6094. for (double tick = 0.0; tick < lynx::ppq * 2.0; tick += 512 * tps)
  6095. {
  6096. ev.clear();
  6097. lynx::schedulePatternLoop (song.patterns[0], song.patterns[0].effectiveLength (lynx::Meter (4, 4)), tick, 512, tps, ev);
  6098. int in = 0, out = 0;
  6099. for (auto& e : ev)
  6100. if (e.channel == 4)
  6101. arp.input[(std::size_t) in++] = e;
  6102. arp.process (4, arp.input.data(), in, tick, tps, 512, song.channels[4].arp, arp.output.data(), out);
  6103. for (int i = 0; i < out; ++i)
  6104. if (arp.output[(std::size_t) i].type == lynx::NoteEvent::Type::on)
  6105. keys.push_back (arp.output[(std::size_t) i].key);
  6106. }
  6107. // 2 beats = 8 sixteenth steps: 60 64 67 60 64 67 60 64
  6108. CHECK (keys.size() >= 8); // the last block goes slightly past 2 beats
  6109. const int expected[] = { 60, 64, 67, 60, 64, 67, 60, 64 };
  6110. for (std::size_t i = 0; i < keys.size() && i < 8; ++i)
  6111. CHECK (keys[i] == expected[i]);
  6112. // actually rendering also makes sound
  6113. lynx::RackRenderer r;
  6114. r.prepare (48000.0, 512);
  6115. std::vector<float> l (512), rr (512);
  6116. float peak = 0.0f;
  6117. for (double tick = 0.0; tick < lynx::ppq; tick += 512 * tps)
  6118. {
  6119. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  6120. for (auto v : l)
  6121. peak = std::max (peak, std::abs (v));
  6122. }
  6123. CHECK (peak > 0.01f);
  6124. }
  6125. TEST (audio_clip_fades_and_gain)
  6126. {
  6127. lynx::Song s;
  6128. s.bpm = 120.0;
  6129. s.mode = lynx::PlayMode::song;
  6130. s.mixer = lynx::Song::defaultMixer (1);
  6131. lynx::ChannelDef c;
  6132. c.instrument = lynx::InstrumentKind::audioClip;
  6133. c.samplePath = "dc";
  6134. c.volume = 1.0f;
  6135. c.mixerTrack = 1;
  6136. s.channels.push_back (c);
  6137. s.patterns.emplace_back();
  6138. lynx::Clip clip;
  6139. clip.kind = lynx::ClipKind::audio;
  6140. clip.channel = 0;
  6141. clip.length = lynx::ppq * 4; // 2 s at 120 BPM
  6142. clip.fadeIn = lynx::ppq; // 0.5 s
  6143. clip.fadeOut = lynx::ppq;
  6144. clip.gain = 0.5f;
  6145. s.playlist.push_back (clip);
  6146. auto provider = [] (const std::string&) {
  6147. auto d = std::make_shared<lynx::SampleData>();
  6148. d->left.assign (48000 * 4, 1.0f);
  6149. return std::shared_ptr<const lynx::SampleData> (d);
  6150. };
  6151. auto state = lynx::RackState::build (s, nullptr, 48000.0, provider);
  6152. lynx::RackRenderer r;
  6153. r.prepare (48000.0, 512);
  6154. std::vector<float> all;
  6155. std::vector<float> l (512), rr (512);
  6156. const auto tps = lynx::ticksPerSample (s.bpm, 48000.0);
  6157. for (double tick = 0.0; tick < lynx::ppq * 4.0; tick += 512 * tps)
  6158. {
  6159. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  6160. all.insert (all.end(), l.begin(), l.end());
  6161. }
  6162. // FL plays a clip at unity at the default channel volume (78%): at 100% it is that much louder (REA_rodada89 §4.2)
  6163. const auto unity = 1.0f / lynx::channelGain (10000.0f / 12800.0f);
  6164. CHECK (all[100] < 0.01f); // fade-in start
  6165. CHECK (std::abs (all[12000] - 0.25f * unity) < 0.02f); // fade-in midpoint: half of 0.5
  6166. CHECK (std::abs (all[48000] - 0.5f * unity) < 0.01f); // body: the clip gain
  6167. CHECK (std::abs (all[96000 - 12000] - 0.25f * unity) < 0.02f); // fade-out midpoint
  6168. }
  6169. TEST (audio_clip_unity_at_fl_default_volume)
  6170. {
  6171. // a Playlist audio clip at FL's default channel volume (10000/12800) and centre pan plays its sample unchanged
  6172. auto dc = std::make_shared<lynx::SampleData>();
  6173. dc->left.assign (48000, 0.5f);
  6174. auto clip = lynx::makeInstrument (lynx::InstrumentKind::audioClip, dc);
  6175. clip->prepare (48000.0);
  6176. lynx::ChannelDef c;
  6177. c.instrument = lynx::InstrumentKind::audioClip;
  6178. c.volume = 10000.0f / 12800.0f;
  6179. clip->configure (c, 120.0);
  6180. // (a Playlist clip's own key: since round 190lq a key below audioClipKeyBase is a NOTE of the channel, which plays as a
  6181. // Sampler note — tests/Issue204Tests.inc)
  6182. clip->noteOn (lynx::audioClipKey (0), 1.0f);
  6183. std::vector<float> l (4800, 0.0f), r (4800, 0.0f);
  6184. const auto g = lynx::channelGain (c.volume);
  6185. clip->render (l.data(), r.data(), 4800, g, g);
  6186. CHECK (std::abs (l[2400] - 0.5f) < 1.0e-3f && std::abs (r[2400] - 0.5f) < 1.0e-3f);
  6187. // a sampler channel (pattern notes) keeps the channel volume curve as it was
  6188. auto sampler = lynx::makeInstrument (lynx::InstrumentKind::sampler, dc);
  6189. sampler->prepare (48000.0);
  6190. c.instrument = lynx::InstrumentKind::sampler;
  6191. sampler->configure (c, 120.0);
  6192. sampler->noteOn (60, 1.0f);
  6193. std::fill (l.begin(), l.end(), 0.0f);
  6194. std::fill (r.begin(), r.end(), 0.0f);
  6195. sampler->render (l.data(), r.data(), 4800, g, g);
  6196. CHECK (std::abs (l[2400] - 0.5f * g) < 0.02f);
  6197. }
  6198. TEST (slicer_detects_hits_and_plays_slices_by_key)
  6199. {
  6200. // 4 hits (decaying noise) every 0.25 s
  6201. lynx::SampleData loop;
  6202. loop.sampleRate = 48000.0;
  6203. loop.left.assign (48000, 0.0f);
  6204. lynx::Noise noise;
  6205. for (int hit = 0; hit < 4; ++hit)
  6206. for (int i = 0; i < 6000; ++i)
  6207. loop.left[(std::size_t) (hit * 12000 + i)] = noise.next() * 0.8f * std::exp (-(float) i / 1500.0f);
  6208. const auto slices = lynx::detectSlices (loop, 0.5f);
  6209. CHECK (slices.size() == 4);
  6210. for (std::size_t i = 0; i < slices.size(); ++i)
  6211. CHECK (std::abs (slices[i] - 0.25f * (float) i) < 0.01f);
  6212. // key 61 plays the second slice, which starts at 0.25 and ends before 0.5
  6213. auto ramp = std::make_shared<lynx::SampleData>();
  6214. for (int i = 0; i < 48000; ++i)
  6215. ramp->left.push_back ((float) i / 48000.0f);
  6216. ramp->sampleRate = 48000.0;
  6217. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  6218. inst->prepare (48000.0);
  6219. lynx::ChannelDef c;
  6220. c.sampler.slices = slices;
  6221. inst->configure (c, 120.0);
  6222. inst->noteOn (61, 1.0f);
  6223. std::vector<float> l (20000), r (20000);
  6224. inst->render (l.data(), r.data(), 20000, 1.0f, 1.0f);
  6225. CHECK (std::abs (l[0] - 0.25f) < 0.01f && l[11000] > 0.45f && l[13000] == 0.0f);
  6226. }
  6227. TEST (project_templates_render_sound)
  6228. {
  6229. const auto names = lynx::templateNames();
  6230. CHECK (names.size() == 4);
  6231. for (int t = 1; t < (int) names.size(); ++t)
  6232. {
  6233. auto song = lynx::makeTemplate (t);
  6234. auto state = lynx::RackState::build (song, nullptr, 48000.0);
  6235. lynx::RackRenderer r;
  6236. r.prepare (48000.0, 512);
  6237. std::vector<float> l (512), rr (512);
  6238. const auto tps = lynx::ticksPerSample (song.bpm, 48000.0);
  6239. float peak = 0.0f;
  6240. for (double tick = 0.0; tick < lynx::ppq * 4.0; tick += 512 * tps)
  6241. {
  6242. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  6243. for (auto v : l)
  6244. peak = std::max (peak, std::abs (v));
  6245. }
  6246. CHECK (peak > 0.05f && peak < 4.0f);
  6247. }
  6248. CHECK (lynx::makeTemplate (0).patterns[0].notes.empty());
  6249. }
  6250. // --- interface translations (resources/i18n) -------------------------------------------------------
  6251. namespace i18ntest
  6252. {
  6253. std::string readFile (const std::filesystem::path& path)
  6254. {
  6255. std::ifstream in (path, std::ios::binary);
  6256. return { std::istreambuf_iterator<char> (in), std::istreambuf_iterator<char>() };
  6257. }
  6258. std::string unescape (const std::string& s)
  6259. {
  6260. std::string out;
  6261. for (std::size_t i = 0; i < s.size(); ++i)
  6262. {
  6263. if (s[i] == '\\' && i + 1 < s.size())
  6264. {
  6265. const auto n = s[++i];
  6266. out += n == 'n' ? '\n' : n == 't' ? '\t' : n;
  6267. }
  6268. else
  6269. out += s[i];
  6270. }
  6271. return out;
  6272. }
  6273. // reads a quoted string starting at s[pos] == '"'; returns the raw (escaped) contents and moves pos past the closing quote
  6274. bool quoted (const std::string& s, std::size_t& pos, std::string& raw)
  6275. {
  6276. if (pos >= s.size() || s[pos] != '"')
  6277. return false;
  6278. raw.clear();
  6279. for (++pos; pos < s.size() && s[pos] != '"' && s[pos] != '\n'; ++pos)
  6280. {
  6281. if (s[pos] == '\\' && pos + 1 < s.size())
  6282. raw += s[pos++];
  6283. raw += s[pos];
  6284. }
  6285. if (pos >= s.size() || s[pos] != '"')
  6286. return false;
  6287. ++pos;
  6288. return true;
  6289. }
  6290. // JUCE LocalisedStrings format: "english" = "translation"; duplicates are reported in `duplicates`
  6291. std::map<std::string, std::string> parseTable (const std::string& text, std::vector<std::string>& duplicates)
  6292. {
  6293. std::map<std::string, std::string> table;
  6294. std::istringstream lines (text);
  6295. std::string line;
  6296. while (std::getline (lines, line))
  6297. {
  6298. std::size_t pos = line.find_first_not_of (" \t");
  6299. std::string key, value;
  6300. if (pos == std::string::npos || ! quoted (line, pos, key))
  6301. continue;
  6302. pos = line.find ('"', pos);
  6303. if (pos == std::string::npos || ! quoted (line, pos, value))
  6304. value.clear();
  6305. const auto k = unescape (key);
  6306. if (table.count (k) != 0)
  6307. duplicates.push_back (k);
  6308. table[k] = unescape (value);
  6309. }
  6310. return table;
  6311. }
  6312. // string literals passed to tr() / trc() / TR_NOOP() in the sources (adjacent literals are concatenated)
  6313. std::map<std::string, std::string> sourceKeys (const std::filesystem::path& srcDir)
  6314. {
  6315. std::map<std::string, std::string> keys;
  6316. for (const auto& entry : std::filesystem::recursive_directory_iterator (srcDir))
  6317. {
  6318. const auto ext = entry.path().extension().string();
  6319. if (! entry.is_regular_file() || (ext != ".cpp" && ext != ".h" && ext != ".inc"))
  6320. continue;
  6321. // drop // comment lines (they may quote tr ("...") as an example)
  6322. std::string text, line;
  6323. std::istringstream in (readFile (entry.path()));
  6324. while (std::getline (in, line))
  6325. {
  6326. const auto first = line.find_first_not_of (" \t");
  6327. text += (first != std::string::npos && line.compare (first, 2, "//") == 0) ? std::string() : line;
  6328. text += '\n';
  6329. }
  6330. auto isIdent = [] (char c) { return std::isalnum ((unsigned char) c) || c == '_'; };
  6331. for (const std::string name : { "tr", "trc", "TR_NOOP" })
  6332. for (auto at = text.find (name); at != std::string::npos; at = text.find (name, at + 1))
  6333. {
  6334. if ((at > 0 && isIdent (text[at - 1])) || isIdent (text[at + name.size()]))
  6335. continue;
  6336. auto pos = text.find_first_not_of (" \t\n", at + name.size());
  6337. if (pos == std::string::npos || text[pos] != '(')
  6338. continue;
  6339. pos = text.find_first_not_of (" \t\n", pos + 1);
  6340. std::string key, raw;
  6341. bool any = false;
  6342. while (pos != std::string::npos && quoted (text, pos, raw))
  6343. {
  6344. key += raw;
  6345. any = true;
  6346. pos = text.find_first_not_of (" \t\n", pos);
  6347. }
  6348. if (any)
  6349. keys.emplace (unescape (key), entry.path().filename().string());
  6350. }
  6351. }
  6352. return keys;
  6353. }
  6354. std::set<std::string> placeholders (const std::string& s)
  6355. {
  6356. std::set<std::string> found;
  6357. for (auto open = s.find ('{'); open != std::string::npos; open = s.find ('{', open + 1))
  6358. if (const auto close = s.find ('}', open); close != std::string::npos && close - open <= 3)
  6359. found.insert (s.substr (open, close - open + 1));
  6360. return found;
  6361. }
  6362. } // namespace i18ntest
  6363. TEST (translations_cover_every_ui_string)
  6364. {
  6365. using namespace i18ntest;
  6366. const auto text = readFile ("resources/i18n/pt-BR.txt");
  6367. CHECK (! text.empty());
  6368. std::vector<std::string> duplicates;
  6369. const auto table = parseTable (text, duplicates);
  6370. CHECK (table.size() > 300);
  6371. CHECK (duplicates.empty());
  6372. for (const auto& d : duplicates)
  6373. std::printf (" duplicate pt-BR entry: \"%s\"\n", d.c_str());
  6374. const auto keys = sourceKeys ("src");
  6375. CHECK (keys.size() > 300);
  6376. int missing = 0;
  6377. for (const auto& [key, file] : keys)
  6378. if (auto it = table.find (key); it == table.end() || it->second.empty())
  6379. {
  6380. ++missing;
  6381. std::printf (" no pt-BR translation for \"%s\" (%s)\n", key.c_str(), file.c_str());
  6382. }
  6383. CHECK (missing == 0);
  6384. // no empty values, and every {n} placeholder survives the translation
  6385. int empty = 0, badPlaceholders = 0;
  6386. for (const auto& [english, portuguese] : table)
  6387. {
  6388. empty += portuguese.empty() ? 1 : 0;
  6389. if (placeholders (english) != placeholders (portuguese))
  6390. {
  6391. ++badPlaceholders;
  6392. std::printf (" placeholders differ: \"%s\"\n", english.c_str());
  6393. }
  6394. }
  6395. CHECK (empty == 0);
  6396. CHECK (badPlaceholders == 0);
  6397. }
  6398. TEST (translations_embedded_table_is_up_to_date)
  6399. {
  6400. // src/ui/i18n/<id>.h + I18nData.h must be regenerated (tools/make_i18n.py) whenever a resources/i18n/*.txt changes
  6401. int files = 0;
  6402. for (const auto& entry : std::filesystem::directory_iterator ("resources/i18n"))
  6403. if (entry.path().extension() == ".txt")
  6404. {
  6405. ++files;
  6406. const auto id = entry.path().stem().string();
  6407. const auto text = i18ntest::readFile (entry.path());
  6408. const lynx::i18n::data::Table* found = nullptr;
  6409. for (const auto& t : lynx::i18n::data::tables)
  6410. if (id == t.id)
  6411. found = &t;
  6412. CHECK (found != nullptr);
  6413. if (found == nullptr)
  6414. {
  6415. std::printf (" %s.txt is not embedded: run python3 tools/make_i18n.py\n", id.c_str());
  6416. continue;
  6417. }
  6418. const auto unpacked = lynx::inflateZlib (found->zlib, found->zlibSize, false, found->textSize + 16);
  6419. const bool same = unpacked && unpacked->size() == text.size() && found->textSize == text.size()
  6420. && std::memcmp (unpacked->data(), text.data(), text.size()) == 0;
  6421. CHECK (same);
  6422. if (! same)
  6423. std::printf (" %s.txt changed since the last python3 tools/make_i18n.py\n", id.c_str());
  6424. }
  6425. CHECK (files == (int) std::size (lynx::i18n::data::tables));
  6426. }
  6427. TEST (translations_english_display_table)
  6428. {
  6429. // rollup 0.1.31 step 3 (round 190ny): English shows the three styles of View › Layout style as layouts through its
  6430. // display table (resources/i18n/en/display.txt, embedded as ui/I18nEnglish.h); the tr() keys stay, so every key of
  6431. // the table is a key of the translation tables, keeps its placeholders, and the embedded copy is up to date
  6432. using namespace i18ntest;
  6433. const auto text = readFile ("resources/i18n/en/display.txt");
  6434. CHECK (text == std::string (lynx::i18n::data::englishDisplay));
  6435. if (text != std::string (lynx::i18n::data::englishDisplay))
  6436. std::printf (" resources/i18n/en/display.txt changed since the last python3 tools/make_i18n.py\n");
  6437. std::vector<std::string> duplicates, unused;
  6438. const auto table = parseTable (text, duplicates);
  6439. const auto reference = parseTable (readFile ("resources/i18n/pt-BR.txt"), unused);
  6440. CHECK (duplicates.empty());
  6441. CHECK (table.size() >= 15);
  6442. const auto placeholders = [] (const std::string& s) {
  6443. std::multiset<std::string> out;
  6444. for (auto at = s.find ('{'); at != std::string::npos; at = s.find ('{', at + 1))
  6445. if (const auto end = s.find ('}', at); end != std::string::npos)
  6446. out.insert (s.substr (at, end - at + 1));
  6447. return out;
  6448. };
  6449. int notKeys = 0, workflow = 0;
  6450. for (const auto& [key, value] : table)
  6451. {
  6452. if (reference.count (key) == 0)
  6453. {
  6454. ++notKeys;
  6455. std::printf (" English display key \"%s\" is not a key of the translation tables\n", key.c_str());
  6456. }
  6457. CHECK (placeholders (key) == placeholders (value));
  6458. workflow += value.find ("orkflow") != std::string::npos ? 1 : 0;
  6459. }
  6460. CHECK (notKeys == 0);
  6461. CHECK (workflow == 0);
  6462. CHECK (table.count ("Pattern workflow (default)") == 1 && table.at ("Pattern workflow (default)") == "Pattern layout (default)");
  6463. CHECK (table.count ("Clip-launch workflow") == 1 && table.at ("Clip-launch workflow") == "Clip-launch layout");
  6464. CHECK (table.count ("Simple workflow") == 1 && table.at ("Simple workflow") == "Simple layout");
  6465. // every key with "workflow" that English shows is in it
  6466. int missing = 0;
  6467. for (const auto& [key, value] : reference)
  6468. if (key.find ("orkflow") != std::string::npos && table.count (key) == 0)
  6469. {
  6470. ++missing;
  6471. std::printf (" \"%s\" still shows \"workflow\" in English\n", key.c_str());
  6472. }
  6473. CHECK (missing == 0);
  6474. }
  6475. TEST (translations_no_value_is_a_python_dict)
  6476. {
  6477. // a value written as a Python dict ("{'de': '...', 'es': ...}", 177a's menu slice) reaches the users verbatim:
  6478. // each table holds its own language's text only
  6479. using namespace i18ntest;
  6480. int bad = 0;
  6481. for (const auto& entry : std::filesystem::directory_iterator ("resources/i18n"))
  6482. if (entry.path().extension() == ".txt")
  6483. {
  6484. std::vector<std::string> unused;
  6485. for (const auto& [key, value] : parseTable (readFile (entry.path()), unused))
  6486. if (value.rfind ("{'", 0) == 0 || value.find ("': '") != std::string::npos)
  6487. {
  6488. ++bad;
  6489. std::printf (" %s: \"%s\" holds a dict\n", entry.path().filename().string().c_str(), key.c_str());
  6490. }
  6491. }
  6492. CHECK (bad == 0);
  6493. }
  6494. TEST (translations_every_language_covers_the_reference_table)
  6495. {
  6496. // every key of pt-BR.txt (the reference table) exists, translated, in every other language; keys just added to
  6497. // pt-BR.txt may wait in resources/i18n/pending.keys ("English" lines) until the other tables catch up
  6498. using namespace i18ntest;
  6499. std::vector<std::string> unused;
  6500. const auto reference = parseTable (readFile ("resources/i18n/pt-BR.txt"), unused);
  6501. std::set<std::string> pending;
  6502. {
  6503. std::istringstream in (readFile ("resources/i18n/pending.keys"));
  6504. std::string line, raw;
  6505. while (std::getline (in, line))
  6506. if (std::size_t pos = line.find_first_not_of (" \t"); pos != std::string::npos && quoted (line, pos, raw))
  6507. pending.insert (unescape (raw));
  6508. }
  6509. auto count = [] (const std::string& s, const std::string& what) {
  6510. int n = 0;
  6511. for (auto at = s.find (what); at != std::string::npos; at = s.find (what, at + what.size()))
  6512. ++n;
  6513. return n;
  6514. };
  6515. int languages = 0;
  6516. for (const auto& entry : std::filesystem::directory_iterator ("resources/i18n"))
  6517. {
  6518. const auto id = entry.path().stem().string();
  6519. if (entry.path().extension() != ".txt" || id == "pt-BR")
  6520. continue;
  6521. ++languages;
  6522. const auto text = readFile (entry.path());
  6523. CHECK (text.rfind ("language: ", 0) == 0); // JUCE LocalisedStrings header
  6524. std::vector<std::string> duplicates;
  6525. const auto table = parseTable (text, duplicates);
  6526. CHECK (duplicates.empty());
  6527. for (const auto& d : duplicates)
  6528. std::printf (" duplicate %s entry: \"%s\"\n", id.c_str(), d.c_str());
  6529. int missing = 0, bad = 0;
  6530. for (const auto& [english, unusedValue] : reference)
  6531. {
  6532. const auto it = table.find (english);
  6533. if ((it == table.end() || it->second.empty()) && pending.count (english) == 0)
  6534. {
  6535. if (++missing <= 10)
  6536. std::printf (" no %s translation for \"%s\" (python3 tools/i18n_tool.py status)\n", id.c_str(), english.c_str());
  6537. }
  6538. }
  6539. for (const auto& [english, translated] : table)
  6540. {
  6541. std::string why;
  6542. if (translated.empty())
  6543. why = "empty";
  6544. else if (placeholders (english) != placeholders (translated))
  6545. why = "placeholders differ";
  6546. else if (count (english, "\n") != count (translated, "\n"))
  6547. why = "line breaks differ";
  6548. else if (count (english, "%") != count (translated, "%"))
  6549. why = "% signs differ";
  6550. if (! why.empty() && ++bad <= 10)
  6551. std::printf (" %s: %s: \"%s\"\n", id.c_str(), why.c_str(), english.c_str());
  6552. }
  6553. CHECK (missing == 0);
  6554. CHECK (bad == 0);
  6555. if (missing > 10 || bad > 10)
  6556. std::printf (" %s: %d missing, %d bad in all\n", id.c_str(), missing, bad);
  6557. }
  6558. CHECK (languages >= 0);
  6559. }
  6560. TEST (translations_os_locale_picks_the_interface_language)
  6561. {
  6562. const std::vector<std::string> ids { "en", "pt-BR", "de", "es", "ja", "zh-Hans", "zh-Hant", "id", "ko" };
  6563. using lynx::i18n::languageForLocale;
  6564. CHECK (languageForLocale ("de-AT", ids) == "de");
  6565. CHECK (languageForLocale ("de_DE.UTF-8", ids) == "de");
  6566. CHECK (languageForLocale ("pt-BR", ids) == "pt-BR");
  6567. CHECK (languageForLocale ("pt_PT", ids) == "pt-BR");
  6568. CHECK (languageForLocale ("zh-Hans-CN", ids) == "zh-Hans");
  6569. CHECK (languageForLocale ("zh-CN", ids) == "zh-Hans");
  6570. CHECK (languageForLocale ("zh_TW.UTF-8", ids) == "zh-Hant");
  6571. CHECK (languageForLocale ("zh-Hant-HK", ids) == "zh-Hant");
  6572. CHECK (languageForLocale ("zh-HK", ids) == "zh-Hant");
  6573. CHECK (languageForLocale ("ja_JP.UTF-8@euro", ids) == "ja");
  6574. CHECK (languageForLocale ("in_ID", ids) == "id");
  6575. CHECK (languageForLocale ("es-419", ids) == "es");
  6576. CHECK (languageForLocale ("en-US", ids) == "en");
  6577. CHECK (languageForLocale ("ko:en", ids) == "ko");
  6578. CHECK (languageForLocale ("fr-FR", ids).empty()); // not in the list
  6579. CHECK (languageForLocale ("C", ids).empty());
  6580. CHECK (languageForLocale ("", ids).empty());
  6581. }
  6582. // --- workflow shell: pattern/channel operations, key map, workspace layout, control math -------------
  6583. TEST (workflow_pattern_navigation_and_insert)
  6584. {
  6585. auto song = lynx::Song::makeDefault();
  6586. song.patterns.resize (3);
  6587. song.patterns[1].name = "B";
  6588. song.patterns[2].name = "C";
  6589. song.patterns[2].notes.push_back ({});
  6590. song.currentPattern = 0;
  6591. CHECK (lynx::stepPattern (song, 1) == 1 && lynx::stepPattern (song, -1) == 0 && lynx::stepPattern (song, 10) == 2);
  6592. // pattern 0 has the default notes, 1 is empty, 2 has a note
  6593. CHECK (! lynx::isPatternEmpty (song.patterns[0]) && lynx::isPatternEmpty (song.patterns[1]));
  6594. CHECK (lynx::findNextEmptyPattern (song, 0) == 1 && lynx::findNextEmptyPattern (song, 1) == -1);
  6595. CHECK (lynx::gotoNextEmptyPattern (song) == 1 && song.currentPattern == 1);
  6596. song.patterns[1].notes.push_back ({});
  6597. const auto before = song.patterns.size();
  6598. CHECK (lynx::gotoNextEmptyPattern (song) == (int) before && song.patterns.size() == before + 1);
  6599. CHECK (song.patterns.back().name == "Pattern 1" || song.patterns.back().name.rfind ("Pattern ", 0) == 0);
  6600. // insert keeps Playlist clips on their patterns
  6601. lynx::Clip clip;
  6602. clip.pattern = 2;
  6603. song.playlist = { clip };
  6604. song.arrangements.push_back ({ "Other", { clip }, 0, 0, {} });
  6605. CHECK (lynx::insertPattern (song, 1, "New") == 1 && song.currentPattern == 1);
  6606. CHECK (song.patterns[1].name == "New" && song.patterns[1].notes.empty() && song.patterns[3].name == "C");
  6607. CHECK (song.playlist[0].pattern == 3 && song.arrangements[1].clips[0].pattern == 3);
  6608. }
  6609. TEST (workflow_split_pattern_by_channel)
  6610. {
  6611. auto song = lynx::Song::makeDefault();
  6612. song.patterns.resize (1);
  6613. auto& p = song.patterns[0];
  6614. p.name = "Beat";
  6615. p.notes.clear();
  6616. for (int i = 0; i < 4; ++i)
  6617. p.notes.push_back ({ (std::int64_t) i * lynx::ticksPerStep, lynx::ticksPerStep, i % 2 == 0 ? 0 : 2, 60 });
  6618. song.channels[2].colour = 0xff112233;
  6619. lynx::Clip clip;
  6620. clip.pattern = 0;
  6621. song.playlist = { clip };
  6622. // round 190qu: FL's Split by channel (checked live in FL 2026): the pattern keeps its first channel, named after it
  6623. const auto created = lynx::splitPatternByChannel (song, 0);
  6624. CHECK (created == 1 && song.patterns.size() == 2);
  6625. CHECK (song.patterns[0].name == song.channels[0].name && song.patterns[0].notes.size() == 2 && song.patterns[0].notes[0].channel == 0);
  6626. CHECK (song.patterns[1].name == song.channels[2].name && song.patterns[1].notes.size() == 2 && song.patterns[1].notes[0].channel == 2);
  6627. CHECK (song.playlist[0].pattern == 0);
  6628. // LYNX's Convert to Track-only keeps the pattern's name in the parts
  6629. auto other = lynx::Song::makeDefault();
  6630. other.patterns.resize (1);
  6631. other.patterns[0].name = "Beat";
  6632. other.patterns[0].notes = { { 0, lynx::ticksPerStep, 0, 60 }, { lynx::ticksPerStep, lynx::ticksPerStep, 2, 60 } };
  6633. CHECK (lynx::splitPatternByChannel (other, 0, true) == 1 && other.patterns[0].name == "Beat - " + other.channels[0].name);
  6634. CHECK (lynx::splitPatternByChannel (song, 99) == 0);
  6635. }
  6636. TEST (workflow_channel_selection)
  6637. {
  6638. lynx::ChannelSelection s;
  6639. s.selectOnly (2);
  6640. s.toggle (5);
  6641. s.toggle (0);
  6642. CHECK ((s.rows == std::vector<int> { 0, 2, 5 }) && s.contains (5) && ! s.contains (3));
  6643. s.toggle (2);
  6644. CHECK ((s.rows == std::vector<int> { 0, 5 }));
  6645. s.selectOnly (3);
  6646. s.extendTo (6);
  6647. CHECK ((s.rows == std::vector<int> { 3, 4, 5, 6 }));
  6648. s.extendTo (1);
  6649. CHECK ((s.rows == std::vector<int> { 1, 2, 3 }));
  6650. s.selectOnly (4);
  6651. s.toggle (4); // the last one stays
  6652. CHECK ((s.rows == std::vector<int> { 4 }));
  6653. s.selectAll (3);
  6654. CHECK ((s.rows == std::vector<int> { 0, 1, 2 }));
  6655. s.rows = { 7, 9 };
  6656. s.clamp (5);
  6657. CHECK (s.rows.size() == 1 && s.rows[0] < 5);
  6658. }
  6659. TEST (workflow_move_clone_insert_channels)
  6660. {
  6661. auto song = lynx::Song::makeDefault();
  6662. const auto names = [&] {
  6663. std::vector<std::string> n;
  6664. for (const auto& c : song.channels)
  6665. n.push_back (c.name);
  6666. return n;
  6667. };
  6668. const auto original = names();
  6669. const auto count = (int) song.channels.size();
  6670. CHECK (count >= 4);
  6671. // references that must follow their channel
  6672. song.patterns[0].notes.push_back ({ 0, 240, 1, 64 });
  6673. lynx::Automation a;
  6674. a.target = { lynx::AutomationTargetKind::channelVolume, 1 };
  6675. song.automations.push_back (a);
  6676. song.midiLinks.push_back ({ 7, -1, { lynx::AutomationTargetKind::channelPan, 3 } });
  6677. lynx::Clip audio;
  6678. audio.kind = lynx::ClipKind::audio;
  6679. audio.channel = 3;
  6680. song.playlist.push_back (audio);
  6681. std::vector<int> selection { 1, 3 };
  6682. CHECK (lynx::moveChannels (song, selection, -1));
  6683. CHECK ((selection == std::vector<int> { 0, 2 }));
  6684. CHECK (song.channels[0].name == original[1] && song.channels[1].name == original[0] && song.channels[2].name == original[3]
  6685. && song.channels[3].name == original[2]);
  6686. CHECK (song.automations[0].target.index == 0 && song.midiLinks[0].target.index == 2 && song.playlist.back().channel == 2);
  6687. CHECK (std::any_of (song.patterns[0].notes.begin(), song.patterns[0].notes.end(),
  6688. [] (const lynx::Note& n) { return n.channel == 0 && n.key == 64; }));
  6689. CHECK (! lynx::moveChannels (song, selection, -1)); // the block is at the top
  6690. CHECK (lynx::moveChannels (song, selection, 1) && (selection == std::vector<int> { 1, 3 }) && names() == original);
  6691. // clone: settings, not notes, right below, with a new name
  6692. song.channels[1].volume = 0.33f;
  6693. const auto notesBefore = song.patterns[0].notes.size();
  6694. const auto clone = lynx::cloneChannel (song, 1);
  6695. CHECK (clone == 2 && (int) song.channels.size() == count + 1);
  6696. CHECK (song.channels[2].name == original[1] + " #2" && song.channels[2].volume == 0.33f);
  6697. CHECK (song.patterns[0].notes.size() == notesBefore);
  6698. CHECK (song.channels[3].name == original[2] && song.automations[0].target.index == 1);
  6699. CHECK (lynx::cloneChannel (song, 1) == 2 && song.channels[2].name == original[1] + " #3");
  6700. // remove several
  6701. CHECK (lynx::removeChannels (song, { 2, 3, 2 }) == 2 && (int) song.channels.size() == count);
  6702. CHECK (names() == original);
  6703. }
  6704. TEST (workflow_send_channels_to_free_inserts)
  6705. {
  6706. auto song = lynx::Song::makeDefault();
  6707. for (auto& c : song.channels)
  6708. c.mixerTrack = 0;
  6709. song.mixer[1].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::delay)); // not free: has effects
  6710. song.channels[0].mixerTrack = 2; // not free: used
  6711. song.setRoute (5, 3, true); // 3 is not free: receives a send
  6712. CHECK (! lynx::isInsertFree (song, 1) && ! lynx::isInsertFree (song, 2) && ! lynx::isInsertFree (song, 3) && lynx::isInsertFree (song, 4));
  6713. song.channels[1].colour = 0xff445566;
  6714. const auto tracks = lynx::sendChannelsToFreeInserts (song, { 1, 2 });
  6715. CHECK ((tracks == std::vector<int> { 4, 5 }) || (tracks == std::vector<int> { 4, 6 }));
  6716. CHECK (song.channels[1].mixerTrack == 4 && song.mixer[4].name == song.channels[1].name && song.mixer[4].colour == 0xff445566u);
  6717. CHECK (song.mixer[(std::size_t) tracks[1]].name == song.channels[2].name);
  6718. // with no free insert left, a new one is added at the end
  6719. auto full = lynx::Song::makeDefault();
  6720. full.mixer = lynx::Song::defaultMixer (1);
  6721. full.channels[0].mixerTrack = 1;
  6722. const auto added = lynx::sendChannelsToFreeInserts (full, { 1 });
  6723. CHECK (added.size() == 1 && added[0] == 2 && full.mixer.size() == 3 && full.channels[1].mixerTrack == 2);
  6724. }
  6725. TEST (workflow_add_channel_routed)
  6726. {
  6727. auto song = lynx::Song::makeDefault();
  6728. const auto count = (int) song.channels.size();
  6729. const auto inserts = song.mixer.size();
  6730. lynx::ChannelDef c;
  6731. c.mixerTrack = 7; // what the generator came with doesn't count
  6732. const auto last = (int) song.mixer.size() - 1;
  6733. CHECK (lynx::addChannelRouted (song, c, last) == count && song.channels.back().mixerTrack == last);
  6734. CHECK (lynx::addChannelRouted (song, c, 0) == count + 1 && song.channels.back().mixerTrack == 0); // Master
  6735. CHECK (song.mixer.size() == inserts); // the drop's insert is used as it is, nothing is claimed
  6736. // an insert that doesn't exist claims a free one, as a plain new channel does
  6737. auto expected = song;
  6738. const auto free = expected.claimFreeInsert();
  6739. CHECK (lynx::addChannelRouted (song, c, 9999) == count + 2 && song.channels.back().mixerTrack == free);
  6740. auto plain = expected;
  6741. CHECK (lynx::addChannelRouted (plain, c) == count + 2 && plain.channels.back().mixerTrack == expected.claimFreeInsert());
  6742. }
  6743. TEST (pr28_generator_dropped_on_an_insert_names_it_like_fl)
  6744. {
  6745. // FL 2026 (round 167u, shots fl3-fl10): 3x Osc dropped on Insert 3, Master and Insert 5's slot 2, then FLEX on
  6746. // Insert 3: channels "3x Osc", "3x Osc #2", "3x Osc #3", "FLEX"; Insert 3 / Master / Insert 5 take the first
  6747. // three names, Insert 3 keeps "3x Osc" when FLEX comes
  6748. auto song = lynx::Song::makeDefault();
  6749. song.channels.clear();
  6750. while (song.mixer.size() < 6)
  6751. song.mixer.emplace_back().name = "Insert " + std::to_string (song.mixer.size());
  6752. song.mixer[4].name = "Drums"; // a name the user gave
  6753. lynx::ChannelDef osc;
  6754. osc.name = "3x Osc";
  6755. lynx::ChannelDef flex;
  6756. flex.name = "FLEX";
  6757. CHECK (lynx::addChannelRouted (song, osc, 3) == 0 && song.channels[0].name == "3x Osc" && song.mixer[3].name == "3x Osc");
  6758. CHECK (lynx::addChannelRouted (song, osc, 0) == 1 && song.channels[1].name == "3x Osc #2" && song.channels[1].mixerTrack == 0
  6759. && song.mixer[0].name == "3x Osc #2");
  6760. CHECK (lynx::addChannelRouted (song, osc, 5) == 2 && song.channels[2].name == "3x Osc #3" && song.mixer[5].name == "3x Osc #3");
  6761. CHECK (lynx::addChannelRouted (song, flex, 3) == 3 && song.channels[3].name == "FLEX" && song.channels[3].mixerTrack == 3
  6762. && song.mixer[3].name == "3x Osc");
  6763. CHECK (lynx::addChannelRouted (song, flex, 4) == 4 && song.channels[4].name == "FLEX #2" && song.mixer[4].name == "Drums");
  6764. // a plain new channel (no insert given) keeps its name and doesn't rename anything, as before
  6765. const auto mixerBefore = song.mixer.size();
  6766. CHECK (lynx::addChannelRouted (song, osc) == 5 && song.channels[5].name == "3x Osc");
  6767. CHECK (song.mixer.size() >= mixerBefore && song.mixer[3].name == "3x Osc" && song.mixer[0].name == "3x Osc #2");
  6768. }
  6769. TEST (workflow_palette_random_colour)
  6770. {
  6771. const auto& palette = lynx::lynxPalette();
  6772. for (std::uint32_t seed = 0; seed < 50; ++seed)
  6773. {
  6774. const auto current = palette[seed % palette.size()];
  6775. const auto c = lynx::randomPaletteColour (current, seed);
  6776. CHECK (c != current && std::find (palette.begin(), palette.end(), c) != palette.end());
  6777. }
  6778. }
  6779. TEST (workflow_key_map)
  6780. {
  6781. using namespace lynx::keys;
  6782. KeyMap map;
  6783. const auto global = Context::global, rack = Context::channelRack;
  6784. CHECK (map.find ({ space, none }, global, true) == "transport.playStop");
  6785. CHECK (map.find ({ space, ctrl }, global, true) == "transport.pause");
  6786. CHECK (map.find ({ f6, none }, global, true) == "panel.channelRack");
  6787. CHECK (map.find ({ f8, alt }, global, false) == "panel.browser");
  6788. CHECK (map.find ({ insert, ctrl | shift }, global, false) == "pattern.insert");
  6789. CHECK (map.find ({ numpadPlus, none }, rack, true) == "pattern.next");
  6790. // panel context: only while focused
  6791. CHECK (map.find ({ 'L', ctrl }, rack, true) == "rack.sendToInserts");
  6792. CHECK (map.find ({ 'L', ctrl }, global, true).empty());
  6793. CHECK (map.find ({ up, alt }, rack, false) == "rack.moveUp");
  6794. // typing piano: note keys play, other plain keys only for whilePiano actions; chords always work
  6795. CHECK (KeyMap::playsNote ({ 'Z', none }, true) && ! KeyMap::playsNote ({ 'Z', none }, false) && ! KeyMap::playsNote ({ 'Z', ctrl }, true));
  6796. CHECK (map.find ({ 'L', none }, global, true) == "transport.toggleMode");
  6797. CHECK (map.find ({ '1', none }, rack, false) == "rack.mute1" && map.find ({ '0', none }, rack, false) == "rack.mute10");
  6798. CHECK (map.find ({ '1', none }, rack, true).empty()); // a plain digit is not a shortcut while the piano is on
  6799. CHECK (map.find ({ '2', ctrl }, rack, true) == "rack.solo2");
  6800. CHECK (typingPianoNote ('Z') == 48 && typingPianoNote ('q') == 60 && typingPianoNote ('P') == 76 && typingPianoNote ('L') == -1);
  6801. CHECK (map.conflicts().empty());
  6802. // chord text
  6803. CHECK (toString ({ f4, ctrl | shift }) == "Ctrl+Shift+F4" && toString ({ numpadMinus, none }) == "Numpad -");
  6804. CHECK (parseChord ("Shift+Ctrl+Ins") == (Chord { insert, ctrl | shift }));
  6805. CHECK (parseChord ("alt+up") == (Chord { up, alt }) && parseChord ("k") == (Chord { 'K', none }));
  6806. CHECK (parseChord ("Numpad +") == (Chord { numpadPlus, none }) && ! parseChord ("F13") && ! parseChord ("Ctrl+"));
  6807. for (const auto& a : actions())
  6808. for (const auto& c : a.defaults)
  6809. CHECK (parseChord (toString (c)) == c);
  6810. // JSON overrides
  6811. std::vector<std::string> errors;
  6812. CHECK (map.loadJson (R"({ "pattern.next": "Ctrl+Right", "rack.clone": ["Alt+D", "Ctrl+D"], "help.shortcuts": "", "nope": "F1", "file.open": "Hyper+Q" })", &errors));
  6813. CHECK (errors.size() == 2);
  6814. CHECK (map.find ({ right, ctrl }, global, false) == "pattern.next" && map.find ({ numpadPlus, none }, global, false).empty());
  6815. CHECK (map.find ({ 'D', ctrl }, rack, false) == "rack.clone" && map.find ({ f1, none }, global, false).empty());
  6816. map.setChords ("pattern.previous", { { right, ctrl } });
  6817. CHECK (map.conflicts().size() == 1);
  6818. KeyMap reloaded;
  6819. CHECK (reloaded.loadJson (map.toJson()) && reloaded.chordsFor ("rack.clone") == map.chordsFor ("rack.clone"));
  6820. CHECK (! map.loadJson ("[1, 2]") && ! map.loadJson ("{\"a\": }"));
  6821. map.resetToDefaults();
  6822. CHECK (map.find ({ numpadPlus, none }, global, false) == "pattern.next");
  6823. }
  6824. TEST (workflow_workspace_layout)
  6825. {
  6826. using namespace lynx::workspace;
  6827. auto l = Layout::makeDefault (1280, 620);
  6828. // every visible panel is inside the desktop; the default shows Browser, Playlist and Channel Rack
  6829. for (const auto& p : l.panels)
  6830. CHECK (p.bounds.x >= 0 && p.bounds.y >= 0 && p.bounds.right() <= 1280 && p.bounds.bottom() <= 620 && p.bounds.w >= minWidth);
  6831. CHECK (l[Panel::browser].visible && l[Panel::playlist].visible && l[Panel::channelRack].visible);
  6832. CHECK (! l[Panel::pianoRoll].visible && ! l[Panel::mixer].visible);
  6833. CHECK (l[Panel::playlist].bounds.x == l[Panel::browser].bounds.right() && l[Panel::playlist].bounds.right() == 1280);
  6834. CHECK (l.isFrontmost (Panel::channelRack) && l.focused == Panel::channelRack);
  6835. // toggle: show+focus, focus, hide
  6836. CHECK (l.toggle (Panel::mixer) && l.focused == Panel::mixer && l.isFrontmost (Panel::mixer));
  6837. CHECK (l.toggle (Panel::channelRack) && l.focused == Panel::channelRack && l.isFrontmost (Panel::channelRack));
  6838. CHECK (! l.toggle (Panel::channelRack) && ! l[Panel::channelRack].visible && l.focused == Panel::mixer);
  6839. l.toggleMaximised (Panel::playlist);
  6840. CHECK (l[Panel::playlist].maximised && l.focused == Panel::playlist);
  6841. // save and load at another size: positions scale
  6842. l[Panel::mixer].detached = true;
  6843. l[Panel::mixer].windowBounds = { 100, 50, 900, 500 };
  6844. const auto text = l.serialise();
  6845. const auto big = Layout::parse (text, 2560, 1240);
  6846. CHECK (big.has_value());
  6847. if (big)
  6848. {
  6849. CHECK ((*big)[Panel::playlist].maximised && (*big)[Panel::mixer].detached && (*big)[Panel::mixer].windowBounds.w == 900);
  6850. CHECK (std::abs ((*big)[Panel::playlist].bounds.x - 2 * l[Panel::playlist].bounds.x) <= 1);
  6851. CHECK (big->order == l.order && big->focused == l.focused);
  6852. }
  6853. 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));
  6854. // resize keeps panels inside and edges glued to the border
  6855. l.resize (1000, 500);
  6856. CHECK (l[Panel::playlist].bounds.right() == 1000 && l[Panel::browser].bounds.bottom() == 500);
  6857. for (const auto& p : l.panels)
  6858. CHECK (p.bounds.right() <= 1000 && p.bounds.bottom() <= 500);
  6859. // off-screen panels come back
  6860. l[Panel::pianoRoll].bounds = { 5000, -300, 400, 300 };
  6861. CHECK (l.bringBackOffscreen() == 1 && l[Panel::pianoRoll].bounds.x + 400 <= 1000 && l[Panel::pianoRoll].bounds.y == 0);
  6862. // snapping: to the desktop, to another panel, and resizing one edge
  6863. CHECK ((snap ({ 5, 6, 200, 100 }, {}, 1000, 500) == Rect { 0, 0, 200, 100 }));
  6864. CHECK ((snap ({ 304, 50, 200, 100 }, { { 0, 0, 300, 400 } }, 1000, 500) == Rect { 300, 50, 200, 100 }));
  6865. CHECK ((snap ({ 304, 50, 200, 100 }, { { 0, 0, 300, 400 } }, 1000, 500, 2) == Rect { 304, 50, 200, 100 })); // out of range
  6866. CHECK ((snap ({ 100, 100, 195, 100 }, { { 300, 0, 300, 400 } }, 1000, 500, 8, right) == Rect { 100, 100, 200, 100 }));
  6867. CHECK ((snap ({ 100, 100, 195, 100 }, { { 300, 1000, 300, 400 } }, 2000, 2000, 8, right) == Rect { 100, 100, 195, 100 })); // not facing
  6868. }
  6869. TEST (workflow_control_math)
  6870. {
  6871. using namespace lynx::controls;
  6872. CHECK (std::abs (dragValue (0.5, 75, 0, 1, 150, false) - 1.0) < 1e-9);
  6873. CHECK (std::abs (dragValue (0.5, 75, 0, 1, 150, true) - 0.55) < 1e-9);
  6874. CHECK (dragValue (0.5, -1000, -1, 1, 150, false) == -1.0);
  6875. 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);
  6876. CHECK (wheelValue (3, 1, 0, 10, true, 1) == 4.0); // integer controls move at least one step
  6877. CHECK (std::abs (snapToInterval (0.234, 0, 0.1) - 0.2) < 1e-9);
  6878. CHECK (tempoDrag (130.0, 8, false, false) == 132.0 && tempoDrag (130.0, -3, false, false) == 130.0);
  6879. CHECK (std::abs (tempoDrag (130.0, 25, true, false) - 130.25) < 1e-9);
  6880. CHECK (std::abs (tempoDrag (130.0, 8, false, true) - 130.2) < 1e-9);
  6881. CHECK (tempoDrag (520.0, 400, false, false) == lynx::maxTempo && halfTempo (15.0) == lynx::minTempo && doubleTempo (130.0) == 260.0);
  6882. TapTempo tap;
  6883. CHECK (! tap.tap (1000.0));
  6884. CHECK (tap.tap (1500.0) == 120.0);
  6885. CHECK (tap.tap (2000.0) == 120.0 && tap.count() == 3);
  6886. CHECK (std::abs (*tap.tap (2400.0) - 128.57) < 0.01); // average of 500, 500, 400 ms
  6887. CHECK (! tap.tap (9000.0) && tap.count() == 1); // a long pause starts over
  6888. }
  6889. TEST (flp_fl11_mixer_slots_and_disabled_channels)
  6890. {
  6891. // REA_rodada80 §3: FL <= 11 files close an effect slot without 0x62 (the slot is the 0xD4 record's second
  6892. // i32); the channel's 0x00 byte is its Channel Rack switch
  6893. if (realFlpDir.empty())
  6894. return;
  6895. auto load = [] (const std::filesystem::path& p) {
  6896. std::ifstream in (p, std::ios::binary);
  6897. std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  6898. return bytes;
  6899. };
  6900. const auto base = std::filesystem::path (realFlpDir);
  6901. if (const auto b = load (base / "System" / "Render tests" / "Fruity Phase Inverter.flp"); ! b.empty())
  6902. {
  6903. const auto project = flp::parseProject (b);
  6904. CHECK (project.mixer.size() > 2 && project.mixer[1].slots.size() == 1);
  6905. if (project.mixer.size() > 2 && project.mixer[1].slots.size() == 1)
  6906. CHECK (project.mixer[1].slots[0].index == 0 && project.mixer[1].slots[0].plugin == "Fruity Wrapper"
  6907. && ! project.mixer[1].slots[0].state.empty());
  6908. }
  6909. if (const auto b = load (base / "Demo projects" / "Performance mode" / "nucleon - Steptime.flp"); ! b.empty())
  6910. {
  6911. const auto project = flp::parseProject (b);
  6912. CHECK (project.mixer.size() > 4 && project.mixer[3].slots.size() == 4);
  6913. if (project.mixer.size() > 4 && project.mixer[3].slots.size() == 4)
  6914. for (int i = 0; i < 4; ++i)
  6915. CHECK (project.mixer[3].slots[(std::size_t) i].index == i);
  6916. const char* names[] = { "Fruity Delay Bank", "Fruity Reeverb 2", "Maximus", "Fruity Parametric EQ 2" };
  6917. if (project.mixer.size() > 4 && project.mixer[3].slots.size() == 4)
  6918. for (int i = 0; i < 4; ++i)
  6919. CHECK (project.mixer[3].slots[(std::size_t) i].plugin == names[i]);
  6920. }
  6921. if (const auto b = load (base / "Demo projects" / "Product demos" / "Synth_dfr - Overcome - Sytrus.flp"); ! b.empty())
  6922. {
  6923. const auto project = flp::parseProject (b);
  6924. int disabled = 0;
  6925. for (const auto& c : project.channels)
  6926. disabled += ! c.enabled;
  6927. CHECK (disabled == 2);
  6928. const auto song = lynx::importFlp (project);
  6929. int muted = 0;
  6930. for (const auto& c : song.channels)
  6931. muted += c.muted;
  6932. CHECK (muted == 2);
  6933. }
  6934. }
  6935. TEST (three_osc_host_hq_render_box_filters_aliasing_shapes)
  6936. {
  6937. // 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
  6938. // copies at gain g/k; a sine is not oversampled
  6939. auto state = [] (std::int32_t shape) {
  6940. std::vector<std::uint8_t> st (0x60, 0);
  6941. const std::int32_t version = 14;
  6942. std::memcpy (st.data(), &version, 4);
  6943. std::memcpy (st.data() + 4 + 4 * 1, &shape, 4); // osc 1 shape; the mix knobs at 0: osc 1 alone
  6944. std::vector<std::uint8_t> snap (364, 0);
  6945. const std::int32_t neutral[17] = { 0, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, 0 };
  6946. for (int k = 0; k < 5; ++k)
  6947. std::memcpy (snap.data() + 0x44 * k, neutral, sizeof (neutral));
  6948. const std::int32_t modX = 256, four = 4, m101 = -101;
  6949. std::memcpy (snap.data() + 0x44, &four, 4);
  6950. std::memcpy (snap.data() + 0x44 + 64, &m101, 4);
  6951. std::memcpy (snap.data() + 0x15c, &modX, 4);
  6952. st.insert (st.end(), snap.begin(), snap.end());
  6953. return st;
  6954. };
  6955. static bool hostHq = false;
  6956. auto render = [&] (std::int32_t shape, bool hq) {
  6957. hostHq = hq;
  6958. auto inst = lynx::makeThreeOscInstrument ([]() noexcept { return hostHq; });
  6959. lynx::ChannelDef c;
  6960. c.instrument = lynx::InstrumentKind::threeosc;
  6961. c.pluginState = state (shape);
  6962. inst->configure (c, 140.0);
  6963. inst->prepare (44100.0);
  6964. inst->noteOn (96, 0.78f);
  6965. std::vector<float> l (4096), r (4096);
  6966. inst->render (l.data(), r.data(), 4096, 1.0f, 1.0f);
  6967. return l;
  6968. };
  6969. auto compare = [&] (std::int32_t shape) {
  6970. const auto live = render (shape, false), hq = render (shape, true);
  6971. double diff = 0.0, energy = 0.0;
  6972. for (std::size_t i = 0; i < live.size(); ++i)
  6973. diff += std::abs (live[i] - hq[i]), energy += std::abs (live[i]);
  6974. return std::make_pair (energy, diff);
  6975. };
  6976. CHECK (lynx::threeOscStateUsable (state (3)));
  6977. const auto [sawEnergy, sawDiff] = compare (3);
  6978. CHECK (sawEnergy > 1.0 && sawDiff > 1.0e-3); // a saw renders differently in the host's HQ mode
  6979. const auto [sineEnergy, sineDiff] = compare (0);
  6980. CHECK (sineEnergy > 1.0 && sineDiff == 0.0); // a sine does not
  6981. }
  6982. namespace
  6983. {
  6984. // The runner's options (round speed185a). They choose WHICH tests run and how the output looks, never what a test checks.
  6985. // [folder] the FL Data folder (realFlpDir), as before
  6986. // --list print the selected test names, one per line, and run nothing
  6987. // --filter a,b run only the tests whose name contains one of these; several --filter options must all match
  6988. // (the LYNX_TEST variable works as before, as one more --filter)
  6989. // --exclude a,b skip the tests whose name contains one of these
  6990. // --shard i/n run only shard i of n (1-based): the selected tests split into n disjoint sets that together
  6991. // cover them all. With --weights the sets are balanced by time, otherwise test k goes to k mod n.
  6992. // --weights file "<name> <ms>" lines (--times output) used to balance the shards; unknown tests weigh 50 ms
  6993. // --times file write "<name> <ms>" per test that ran
  6994. // --no-times no per-test time lines
  6995. // Output: each test's name on its own line (as before), its FAILED lines, then " time <ms> ms"; at the end the slowest
  6996. // tests and the unchanged "N checks, M failures" line. stdout is line-buffered so a long test never looks hung.
  6997. struct RunnerOptions
  6998. {
  6999. bool list = false, times = true;
  7000. std::vector<std::vector<std::string>> filters; // (each must match: one of its names)
  7001. std::vector<std::string> excludes;
  7002. int shard = 0, shards = 1;
  7003. std::string weightsFile, timesFile;
  7004. };
  7005. std::vector<std::string> splitCommas (const std::string& s)
  7006. {
  7007. std::vector<std::string> out;
  7008. std::string cur;
  7009. for (char c : s + ",")
  7010. if (c == ',')
  7011. {
  7012. if (! cur.empty())
  7013. out.push_back (cur);
  7014. cur.clear();
  7015. }
  7016. else
  7017. cur += c;
  7018. return out;
  7019. }
  7020. bool parseRunnerOptions (int argc, char** argv, RunnerOptions& o)
  7021. {
  7022. for (int i = 1; i < argc; ++i)
  7023. {
  7024. const std::string a = argv[i];
  7025. auto value = [&] () -> std::optional<std::string> {
  7026. if (i + 1 >= argc)
  7027. return std::nullopt;
  7028. return std::string (argv[++i]);
  7029. };
  7030. if (a == "--list")
  7031. o.list = true;
  7032. else if (a == "--no-times")
  7033. o.times = false;
  7034. else if (a == "--filter" || a == "--exclude" || a == "--shard" || a == "--weights" || a == "--times")
  7035. {
  7036. const auto v = value();
  7037. if (! v)
  7038. {
  7039. std::fprintf (stderr, "lynx_tests: %s needs a value\n", a.c_str());
  7040. return false;
  7041. }
  7042. if (a == "--filter")
  7043. o.filters.push_back (splitCommas (*v));
  7044. else if (a == "--exclude")
  7045. for (auto& f : splitCommas (*v)) o.excludes.push_back (f);
  7046. else if (a == "--weights")
  7047. o.weightsFile = *v;
  7048. else if (a == "--times")
  7049. o.timesFile = *v;
  7050. else
  7051. {
  7052. int k = 0, n = 0;
  7053. char tail = 0;
  7054. if (std::sscanf (v->c_str(), "%d/%d%c", &k, &n, &tail) != 2 || n < 1 || k < 1 || k > n)
  7055. {
  7056. std::fprintf (stderr, "lynx_tests: --shard wants i/n with 1 <= i <= n, not %s\n", v->c_str());
  7057. return false;
  7058. }
  7059. o.shard = k - 1;
  7060. o.shards = n;
  7061. }
  7062. }
  7063. else if (a.size() > 2 && a.compare (0, 2, "--") == 0)
  7064. {
  7065. std::fprintf (stderr, "lynx_tests: unknown option %s\n", a.c_str());
  7066. return false;
  7067. }
  7068. else
  7069. realFlpDir = a;
  7070. }
  7071. if (const char* only = std::getenv ("LYNX_TEST")) // run only the tests whose name contains this
  7072. o.filters.push_back ({ only });
  7073. return true;
  7074. }
  7075. // which of the selected tests (indices into `names`) belong to shard o.shard: a partition of all of them over o.shards
  7076. std::vector<std::size_t> shardOf (const std::vector<const char*>& names, const RunnerOptions& o)
  7077. {
  7078. std::vector<std::size_t> all (names.size());
  7079. for (std::size_t k = 0; k < all.size(); ++k)
  7080. all[k] = k;
  7081. if (o.shards <= 1)
  7082. return all;
  7083. std::map<std::string, double> weight;
  7084. if (! o.weightsFile.empty())
  7085. {
  7086. std::ifstream in (o.weightsFile);
  7087. std::string name;
  7088. double ms = 0.0;
  7089. while (in >> name >> ms)
  7090. weight[name] = ms;
  7091. }
  7092. std::vector<int> owner (names.size(), 0);
  7093. if (weight.empty())
  7094. for (std::size_t k = 0; k < names.size(); ++k)
  7095. owner[k] = (int) (k % (std::size_t) o.shards);
  7096. else
  7097. {
  7098. // longest first onto the least loaded shard (ties: lower index), deterministic for a given list and file
  7099. auto order = all;
  7100. auto w = [&] (std::size_t k) { const auto it = weight.find (names[k]); return it == weight.end() ? 50.0 : it->second; };
  7101. std::stable_sort (order.begin(), order.end(), [&] (std::size_t a, std::size_t b) { return w (a) > w (b); });
  7102. std::vector<double> load ((std::size_t) o.shards, 0.0);
  7103. for (auto k : order)
  7104. {
  7105. const auto s = (std::size_t) (std::min_element (load.begin(), load.end()) - load.begin());
  7106. owner[k] = (int) s;
  7107. load[s] += w (k);
  7108. }
  7109. }
  7110. std::vector<std::size_t> mine;
  7111. for (auto k : all)
  7112. if (owner[k] == o.shard)
  7113. mine.push_back (k); // (in registry order: a shard runs its tests in the usual order)
  7114. return mine;
  7115. }
  7116. } // namespace
  7117. int main (int argc, char** argv)
  7118. {
  7119. std::setvbuf (stdout, nullptr, _IOLBF, 0);
  7120. RunnerOptions opt;
  7121. if (! parseRunnerOptions (argc, argv, opt))
  7122. return 2;
  7123. auto& tests = registry();
  7124. std::vector<const char*> names;
  7125. std::vector<std::size_t> selected;
  7126. for (std::size_t k = 0; k < tests.size(); ++k)
  7127. {
  7128. const char* name = tests[k].first;
  7129. auto has = [name] (const std::string& part) { return std::strstr (name, part.c_str()) != nullptr; };
  7130. if (! std::all_of (opt.filters.begin(), opt.filters.end(), [&] (const auto& any) { return std::any_of (any.begin(), any.end(), has); }))
  7131. continue;
  7132. if (std::any_of (opt.excludes.begin(), opt.excludes.end(), has))
  7133. continue;
  7134. names.push_back (name);
  7135. selected.push_back (k);
  7136. }
  7137. const auto mine = shardOf (names, opt);
  7138. if (opt.list)
  7139. {
  7140. for (auto k : mine)
  7141. std::printf ("%s\n", names[k]);
  7142. return 0;
  7143. }
  7144. std::vector<std::pair<double, const char*>> took;
  7145. for (auto k : mine)
  7146. {
  7147. auto& [name, fn] = tests[selected[k]];
  7148. std::printf ("%s\n", name);
  7149. const auto start = std::chrono::steady_clock::now();
  7150. fn();
  7151. const auto ms = std::chrono::duration<double, std::milli> (std::chrono::steady_clock::now() - start).count();
  7152. took.emplace_back (ms, name);
  7153. if (opt.times)
  7154. std::printf (" time %.0f ms\n", ms);
  7155. }
  7156. if (! opt.timesFile.empty())
  7157. {
  7158. std::ofstream out (opt.timesFile);
  7159. for (auto& [ms, name] : took)
  7160. out << name << ' ' << (long long) std::llround (ms) << '\n';
  7161. }
  7162. if (opt.times && took.size() > 1)
  7163. {
  7164. double total = 0.0;
  7165. for (auto& t : took)
  7166. total += t.first;
  7167. auto slow = took;
  7168. std::sort (slow.begin(), slow.end(), [] (const auto& a, const auto& b) { return a.first > b.first; });
  7169. slow.resize (std::min<std::size_t> (slow.size(), 15));
  7170. std::printf ("\n %zu tests in %.1f s%s; slowest:\n", took.size(), total / 1000.0,
  7171. opt.shards > 1 ? (" (shard " + std::to_string (opt.shard + 1) + "/" + std::to_string (opt.shards) + ")").c_str() : "");
  7172. for (auto& [ms, name] : slow)
  7173. std::printf (" %8.1f s %s\n", ms / 1000.0, name);
  7174. }
  7175. std::printf ("\n%d checks, %d failures\n", checks, failures);
  7176. return failures == 0 ? 0 : 1;
  7177. }
  7178. TEST (synth_cutoff_keytrack)
  7179. {
  7180. // the cutoff follows the note from key 48 (130.81 Hz), like Harmor's "Key to filter frequency"
  7181. auto rms = [] (float keyTrack, int key) {
  7182. lynx::SynthSettings s;
  7183. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  7184. s.cutoff = 130.81f;
  7185. s.keyTrack = keyTrack;
  7186. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  7187. inst->prepare (48000.0);
  7188. lynx::ChannelDef c;
  7189. c.synth = s;
  7190. inst->configure (c, 120.0);
  7191. inst->noteOn (key, 1.0f);
  7192. std::vector<float> l (24000), r (24000);
  7193. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  7194. double e = 0.0;
  7195. for (int i = 4800; i < 24000; ++i)
  7196. e += (double) l[(std::size_t) i] * l[(std::size_t) i];
  7197. return std::sqrt (e / 19200.0);
  7198. };
  7199. const auto fixed = rms (0.0f, 72), tracked = rms (1.0f, 72);
  7200. CHECK (tracked > 4.0 * fixed); // 2 octaves up: without keytrack the low-pass cuts
  7201. CHECK (std::abs (rms (1.0f, 48) - rms (0.0f, 48)) < 0.05 * rms (0.0f, 48)); // unchanged at the reference key
  7202. }
  7203. namespace
  7204. {
  7205. std::vector<float> renderSynthNote (const lynx::SynthSettings& s, int key, int frames)
  7206. {
  7207. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  7208. inst->prepare (48000.0);
  7209. lynx::ChannelDef c;
  7210. c.synth = s;
  7211. inst->configure (c, 120.0);
  7212. inst->noteOn (key, 1.0f);
  7213. std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
  7214. inst->render (l.data(), r.data(), frames, 1.0f, 1.0f);
  7215. return l;
  7216. }
  7217. double goertzel (const std::vector<float>& x, double f, int from, int to)
  7218. {
  7219. const auto w = 2.0 * 3.141592653589793 * f / 48000.0, k = 2.0 * std::cos (w);
  7220. double s1 = 0.0, s2 = 0.0;
  7221. for (int i = from; i < to; ++i)
  7222. {
  7223. const auto s0 = x[(std::size_t) i] + k * s1 - s2;
  7224. s2 = s1;
  7225. s1 = s0;
  7226. }
  7227. return s1 * s1 + s2 * s2 - k * s1 * s2;
  7228. }
  7229. } // namespace
  7230. TEST (synth_pitch_sweep_ring_and_bands)
  7231. {
  7232. lynx::SynthSettings sine;
  7233. sine.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  7234. sine.cutoff = 20000.0f;
  7235. // sweep: starts at f·(1 + sweep) and returns to f (time constant s = 10 ms; s⁴ falls 4× faster)
  7236. auto sweep = sine;
  7237. sweep.sweep = 1.0f;
  7238. sweep.sweepTime = 0.01f;
  7239. const auto a = renderSynthNote (sweep, 69, 24000);
  7240. auto crossings = [] (const std::vector<float>& x, int to) {
  7241. int n = 0;
  7242. for (int i = 1; i < to; ++i)
  7243. n += (x[(std::size_t) i - 1] < 0.0f) != (x[(std::size_t) i] < 0.0f);
  7244. return n;
  7245. };
  7246. CHECK (crossings (a, 480) > 1.3 * crossings (renderSynthNote (sine, 69, 480), 480)); // 10 ms: much higher
  7247. CHECK (goertzel (a, 440.0, 12000, 24000) > 100.0 * goertzel (a, 880.0, 12000, 24000));
  7248. // ring with |a| = 1: carrier 440 × modulator 110 -> only 330 and 550, no 440
  7249. auto ring = sine;
  7250. ring.am = ring.ring = true;
  7251. ring.osc[2] = { lynx::Waveform::sine, -2, 0.0f, 1.0f };
  7252. const auto b = renderSynthNote (ring, 69, 24000);
  7253. CHECK (goertzel (b, 330.0, 4800, 24000) > 100.0 * goertzel (b, 440.0, 4800, 24000));
  7254. CHECK (goertzel (b, 550.0, 4800, 24000) > 100.0 * goertzel (b, 440.0, 4800, 24000));
  7255. // 3 bands: muting the high knocks down a 4 kHz note and barely touches an 80 Hz one
  7256. auto bands = sine;
  7257. bands.bandGain = { 1.0f, 1.0f, 0.0f };
  7258. auto rms = [] (const std::vector<float>& x) {
  7259. double e = 0.0;
  7260. for (std::size_t i = 4800; i < x.size(); ++i)
  7261. e += (double) x[i] * x[i];
  7262. return std::sqrt (e / (double) (x.size() - 4800));
  7263. };
  7264. CHECK (rms (renderSynthNote (bands, 107, 24000)) < 0.3 * rms (renderSynthNote (sine, 107, 24000)));
  7265. CHECK (rms (renderSynthNote (bands, 28, 24000)) > 0.85 * rms (renderSynthNote (sine, 28, 24000)));
  7266. }
  7267. TEST (love_philter_map_evaluation)
  7268. {
  7269. lynx::LovePhilterMap line { { { 0.0, 0.0 }, { 1.0, 1.0 } } };
  7270. CHECK (std::abs (lynx::lovePhilterMapAt (line, 0.5) - 0.5) < 1e-9);
  7271. CHECK (std::abs (lynx::lovePhilterMapAt (line, 1.0) - 1.0) < 1e-9);
  7272. // steps (Δx = 0): 1 until 0.4375, ramp to 0 at 0.5, then 0
  7273. lynx::LovePhilterMap steps { { { 0.0, 1.0 }, { 0.4375, 1.0 }, { 0.0625, 0.0 }, { 0.5, 0.0 } } };
  7274. CHECK (lynx::lovePhilterMapAt (steps, 0.27) == 1.0);
  7275. CHECK (lynx::lovePhilterMapAt (steps, 0.75) == 0.0);
  7276. CHECK (std::abs (lynx::lovePhilterMapAt (steps, 0.46875) - 0.5) < 1e-9);
  7277. CHECK (lynx::lovePhilterMapAt ({}, 0.3) == 1.0);
  7278. }
  7279. TEST (lofi_holds_and_quantizes)
  7280. {
  7281. auto fx = lynx::makeEffect (lynx::EffectKind::lofi);
  7282. fx->prepare (48000.0);
  7283. auto slot = lynx::makeEffectSlot (lynx::EffectKind::lofi);
  7284. slot.params[0] = 0.5f; // 128·0.125 + 1 = 17 samples
  7285. slot.params[1] = 1.0f; // 65536/2049 ≈ 32 levels
  7286. std::vector<float> l (170), r (170);
  7287. for (std::size_t i = 0; i < l.size(); ++i)
  7288. l[i] = r[i] = 0.9f * (float) std::sin (0.05 * (double) i);
  7289. const auto original = l;
  7290. fx->process (l.data(), r.data(), 170, slot.params, 120.0);
  7291. for (int i = 0; i < 17; ++i)
  7292. CHECK (l[(std::size_t) i] == l[0]);
  7293. CHECK (l[17] != l[16]);
  7294. const auto step = 2.0f * 2049.0f / 65536.0f;
  7295. for (auto v : l)
  7296. CHECK (std::abs (v / step - std::round (v / step)) < 1e-3f);
  7297. CHECK (std::abs (l[0] - original[0]) <= step);
  7298. CHECK (lynx::formatEffectParam (lynx::EffectKind::lofi, 1, 0.0f, 120.0) == "16.0 bits");
  7299. }
  7300. namespace
  7301. {
  7302. lynx::EffectParams effectorParams (int type, float x, float y)
  7303. {
  7304. auto slot = lynx::makeEffectSlot (lynx::EffectKind::effector);
  7305. slot.params[0] = (float) type / 11.0f;
  7306. slot.params[3] = x;
  7307. slot.params[4] = y;
  7308. return slot.params;
  7309. }
  7310. } // namespace
  7311. TEST (effector_algorithms)
  7312. {
  7313. CHECK (lynx::effector::typeOf (8.0f / 11.0f) == 8);
  7314. CHECK (std::string (lynx::effector::typeName (11)) == "Ring");
  7315. CHECK (std::abs (lynx::effector::rateBeats (0.5f, 0.0f) - 0.75) < 1e-9); // trunc(8·0.5 + 0.5) = 4 -> 3/4
  7316. CHECK (std::abs (lynx::effector::rateBeats (1.0f, 0.5f) - 2.0) < 1e-9); // 8 × (1 − 0.5)²
  7317. auto run = [] (const lynx::EffectParams& p, std::vector<float> in, double bpm = 120.0) {
  7318. auto fx = lynx::makeEffect (lynx::EffectKind::effector);
  7319. fx->prepare (48000.0);
  7320. fx->setSongPosition (0.0);
  7321. auto r = in;
  7322. fx->process (in.data(), r.data(), (int) in.size(), p, bpm);
  7323. return in;
  7324. };
  7325. // Distort with Y = 1 (everything goes to the cascade): 0.5 comes out 0.5 (compensation G = 0.5/F(0.5))
  7326. {
  7327. auto p = effectorParams (0, 0.5f, 1.0f);
  7328. const auto out = run (p, std::vector<float> (4800, 0.5f));
  7329. CHECK (std::abs (out.back() - 0.5f) < 1e-3f);
  7330. const auto small = run (p, std::vector<float> (4800, 0.05f));
  7331. CHECK (small.back() > 0.45f && small.back() < 0.5f); // saturates near ±0.5
  7332. }
  7333. // Ring: DC 1 becomes the carrier sine, 2^(8.7·0 + 5) = 32 Hz -> 32 cycles per second
  7334. {
  7335. auto p = effectorParams (11, 0.0f, 0.0f);
  7336. const auto out = run (p, std::vector<float> (48000, 1.0f));
  7337. int crossings = 0;
  7338. for (std::size_t i = 4800; i < out.size(); ++i)
  7339. crossings += (out[i - 1] < 0.0f) != (out[i] < 0.0f);
  7340. CHECK (crossings >= 56 && crossings <= 60); // 0.9 s × 32 Hz × 2
  7341. }
  7342. // Trancer, Y = 0: at the start of the period the gain drops to zero; X = 1 returns only at the end of the period
  7343. {
  7344. auto p = effectorParams (8, 1.0f, 0.0f);
  7345. p[5] = 1.0f; // 8/1 = 8 beats = 4 s at 120 BPM
  7346. const auto out = run (p, std::vector<float> (48000, 1.0f));
  7347. CHECK (out[100] > 0.3f); // still rising on the "and" (1% of the period = 40 ms)
  7348. CHECK (out[24000] < 0.05f); // 0.5 s: trough
  7349. }
  7350. // Reverb with Wet = 0: the dry passes intact
  7351. {
  7352. auto p = effectorParams (6, 0.5f, 0.5f);
  7353. p[2] = 0.0f;
  7354. std::vector<float> in (4800);
  7355. for (std::size_t i = 0; i < in.size(); ++i)
  7356. in[i] = (float) std::sin (0.01 * (double) i);
  7357. const auto out = run (p, in);
  7358. for (std::size_t i = 0; i < in.size(); ++i)
  7359. CHECK (std::abs (out[i] - in[i]) < 1e-6f);
  7360. }
  7361. // Delay: the echo arrives after the Mod Rate period (1/4 beat at 120 BPM = 125 ms), at level X²
  7362. {
  7363. auto p = effectorParams (5, 0.7f, 0.5f);
  7364. p[5] = 1.0f / 8.0f; // index 1 = 1/4
  7365. std::vector<float> in (12000, 0.0f);
  7366. in[0] = 1.0f;
  7367. const auto out = run (p, in);
  7368. CHECK (std::abs (out[6000] - 0.49f) < 0.02f);
  7369. CHECK (std::abs (out[3000]) < 1e-6f);
  7370. }
  7371. // Stutter: repeats the recorded start every period
  7372. {
  7373. auto p = effectorParams (9, 0.0f, 0.0f);
  7374. p[5] = 0.0f; // 1/8 beat = 62.5 ms = 3000 samples
  7375. std::vector<float> in (9000);
  7376. for (std::size_t i = 0; i < in.size(); ++i)
  7377. in[i] = (float) i / 9000.0f;
  7378. const auto out = run (p, in);
  7379. CHECK (std::abs (out[3000 + 500] - in[500]) < 0.01f); // after the crossfade, it's the start again
  7380. CHECK (std::abs (out[1000] - in[1000]) < 1e-6f);
  7381. }
  7382. }
  7383. TEST (crossover_linkwitz_riley_sums_flat)
  7384. {
  7385. // low band + high band of the same crossover: sum with magnitude 1 (Linkwitz-Riley; for 12 and 36 dB the HP is inverted)
  7386. for (const auto combo : { 1, 3, 4, 5, 6, 7 })
  7387. for (const auto testHz : { 200.0, 1000.0, 5000.0 })
  7388. {
  7389. auto run = [&] (bool high) {
  7390. auto fx = lynx::makeEffect (lynx::EffectKind::crossover);
  7391. fx->prepare (48000.0);
  7392. auto slot = lynx::makeEffectSlot (lynx::EffectKind::crossover);
  7393. const auto value = (float) (std::log2 (1000.0 / 30.0) / 9.0589);
  7394. slot.params[high ? 0 : 3] = 1.0f;
  7395. slot.params[high ? 1 : 4] = value;
  7396. slot.params[high ? 2 : 5] = (float) combo / 7.0f;
  7397. std::vector<float> l (24000), r (24000);
  7398. for (std::size_t i = 0; i < l.size(); ++i)
  7399. l[i] = r[i] = (float) std::sin (2.0 * 3.141592653589793 * testHz * (double) i / 48000.0);
  7400. fx->process (l.data(), r.data(), 24000, slot.params, 120.0);
  7401. return l;
  7402. };
  7403. const auto low = run (false), high = run (true);
  7404. double peak = 0.0;
  7405. for (std::size_t i = 12000; i < low.size(); ++i)
  7406. peak = std::max (peak, (double) std::abs (low[i] + high[i]));
  7407. CHECK (std::abs (peak - 1.0) < 0.02);
  7408. }
  7409. // 96 dB/oct: two octaves above the cutoff the low-pass is already below −150 dB... in practice, very low
  7410. auto fx = lynx::makeEffect (lynx::EffectKind::crossover);
  7411. fx->prepare (48000.0);
  7412. auto slot = lynx::makeEffectSlot (lynx::EffectKind::crossover);
  7413. slot.params[3] = 1.0f;
  7414. slot.params[4] = (float) (std::log2 (500.0 / 30.0) / 9.0589);
  7415. slot.params[5] = 1.0f;
  7416. std::vector<float> l (24000), r (24000);
  7417. for (std::size_t i = 0; i < l.size(); ++i)
  7418. l[i] = r[i] = (float) std::sin (2.0 * 3.141592653589793 * 2000.0 * (double) i / 48000.0);
  7419. fx->process (l.data(), r.data(), 24000, slot.params, 120.0);
  7420. float peak = 0.0f;
  7421. for (std::size_t i = 12000; i < l.size(); ++i)
  7422. peak = std::max (peak, std::abs (l[i]));
  7423. CHECK (peak < 1e-4f);
  7424. }
  7425. TEST (rhythm_gain_and_autopan)
  7426. {
  7427. // 1-beat gate: 1 → 0 at 0.5 beat (linear) → 1 at the end; at 120 BPM 1 beat = 24000 samples at 48 kHz
  7428. auto slot = lynx::makeEffectSlot (lynx::EffectKind::rhythmGain);
  7429. slot.params[0] = 1.0f / 64.0f;
  7430. slot.params[1] = 3.0f / 12.0f;
  7431. const float pts[3][3] = { { 0.0f, 1.0f, 0.5f }, { 0.5f, 0.0f, 0.5f }, { 1.0f, 1.0f, 0.5f } };
  7432. for (int k = 0; k < 3; ++k)
  7433. for (int j = 0; j < 3; ++j)
  7434. slot.params[(std::size_t) (2 + 3 * k + j)] = pts[k][j];
  7435. auto fx = lynx::makeEffect (lynx::EffectKind::rhythmGain);
  7436. fx->prepare (48000.0);
  7437. fx->setSongPosition (960.0 * 2.0); // beat 2: phase 0
  7438. std::vector<float> l (24000, 1.0f), r (24000, 1.0f);
  7439. fx->process (l.data(), r.data(), 24000, slot.params, 120.0);
  7440. CHECK (std::abs (l[0] - 1.0f) < 1e-3f);
  7441. CHECK (std::abs (l[6000] - 0.5f) < 1e-3f);
  7442. CHECK (l[12000] < 1e-3f);
  7443. CHECK (std::abs (l[18000] - 0.5f) < 1e-3f);
  7444. // positive tension: changes quickly at the start of the segment
  7445. CHECK (lynx::effectInfo (lynx::EffectKind::rhythmGain).params.size() == 39);
  7446. // auto-pan: 1-beat period, full depth; start phase 0.75 -> W = 0 rising (pan centered)
  7447. auto pan = lynx::makeEffectSlot (lynx::EffectKind::autoPan);
  7448. pan.params[0] = 1.0f / 64.0f;
  7449. pan.params[1] = 1.0f;
  7450. auto ap = lynx::makeEffect (lynx::EffectKind::autoPan);
  7451. ap->prepare (48000.0);
  7452. ap->setSongPosition (0.0);
  7453. std::vector<float> a (24000, 1.0f), b (24000, 1.0f);
  7454. ap->process (a.data(), b.data(), 24000, pan.params, 120.0);
  7455. CHECK (std::abs (a[0] - 1.0f) < 1e-3f && std::abs (b[0] - 1.0f) < 1e-3f);
  7456. CHECK (a[6000] < 0.01f && std::abs (b[6000] - 1.0f) < 1e-3f); // ¼ cycle later: fully right
  7457. }
  7458. TEST (vocoder_side_inputs_split_modulator_and_carrier)
  7459. {
  7460. // "Side inputs" mode (Vocodex): modulator from side input 1 (noise in the first half), carrier from side input 2
  7461. // (saw), own input silent → the output follows the side modulator; with input 1 silent, the output is silent
  7462. auto slot = lynx::makeEffectSlot (lynx::EffectKind::vocoder);
  7463. slot.params[16] = 1.0f;
  7464. auto fx = lynx::makeEffect (lynx::EffectKind::vocoder);
  7465. fx->prepare (48000.0);
  7466. std::vector<float> modL (48000), car (48000), l (48000, 0.0f), r (48000, 0.0f);
  7467. std::uint32_t seed = 1;
  7468. for (std::size_t i = 0; i < modL.size(); ++i)
  7469. {
  7470. seed = seed * 1664525u + 1013904223u;
  7471. modL[i] = i < 24000 ? ((float) (seed >> 8) / 16777216.0f - 0.5f) : 0.0f;
  7472. car[i] = (float) (2.0 * std::fmod ((double) i * 220.0 / 48000.0, 1.0) - 1.0) * 0.5f;
  7473. }
  7474. for (int done = 0; done < 48000; done += 480)
  7475. {
  7476. fx->setSidechain (modL.data() + done, modL.data() + done);
  7477. fx->setSidechain2 (car.data() + done, car.data() + done);
  7478. fx->process (l.data() + done, r.data() + done, 480, slot.params, 120.0);
  7479. }
  7480. auto rms = [&] (int from, int to) {
  7481. double e = 0.0;
  7482. for (int i = from; i < to; ++i)
  7483. e += (double) l[(std::size_t) i] * l[(std::size_t) i];
  7484. return std::sqrt (e / (to - from));
  7485. };
  7486. CHECK (rms (6000, 20000) > 0.01);
  7487. CHECK (rms (40000, 48000) < 0.1 * rms (6000, 20000));
  7488. // the side modulator configured but silent: nothing comes out (the own input is not used instead)
  7489. std::vector<float> a (4800, 0.3f), b (4800, 0.3f);
  7490. for (int done = 0; done < 4800; done += 480)
  7491. {
  7492. fx->setSidechain (nullptr, nullptr);
  7493. fx->setSidechain2 (car.data() + done, car.data() + done);
  7494. fx->process (a.data() + done, b.data() + done, 480, slot.params, 120.0);
  7495. }
  7496. CHECK (std::abs (a[4700]) < 0.05f);
  7497. }
  7498. TEST (vocoder_follows_modulator)
  7499. {
  7500. // modulator (L) plays only in the first half; carrier (R) is a continuous saw: the wet follows the modulator
  7501. auto slot = lynx::makeEffectSlot (lynx::EffectKind::vocoder);
  7502. auto fx = lynx::makeEffect (lynx::EffectKind::vocoder);
  7503. fx->prepare (48000.0);
  7504. std::vector<float> l (48000), r (48000);
  7505. std::uint32_t seed = 1;
  7506. for (std::size_t i = 0; i < l.size(); ++i)
  7507. {
  7508. seed = seed * 1664525u + 1013904223u;
  7509. l[i] = i < 24000 ? ((float) (seed >> 8) / 16777216.0f - 0.5f) : 0.0f;
  7510. r[i] = (float) (2.0 * std::fmod ((double) i * 220.0 / 48000.0, 1.0) - 1.0) * 0.5f;
  7511. }
  7512. fx->process (l.data(), r.data(), 48000, slot.params, 120.0);
  7513. auto rms = [&] (int from, int to) {
  7514. double e = 0.0;
  7515. for (int i = from; i < to; ++i)
  7516. e += (double) l[(std::size_t) i] * l[(std::size_t) i];
  7517. return std::sqrt (e / (to - from));
  7518. };
  7519. CHECK (rms (6000, 20000) > 0.01);
  7520. CHECK (rms (40000, 48000) < 0.1 * rms (6000, 20000)); // modulator silent: the vocoder fades out (100 ms release)
  7521. for (std::size_t i = 0; i < l.size(); ++i)
  7522. CHECK (l[i] == r[i] && std::isfinite (l[i])); // mono output
  7523. }
  7524. TEST (fruity_vocoder_engine)
  7525. {
  7526. // REA_rodada70: Fruity Vocoder, states of our own against the installation's DLL (goldens = the DLL's output for
  7527. // the same input, EXEC; all three bit-identical over the whole 4096 frames)
  7528. std::vector<float> inL (4096), inR (4096);
  7529. for (int i = 0; i < 4096; ++i)
  7530. {
  7531. inL[(std::size_t) i] = i < 3000 ? (float) (((i * 37) % 200) - 100) / 128.0f : 0.0f; // modulator, then silence
  7532. inR[(std::size_t) i] = (i * 13) % 64 < 32 ? 0.5f : -0.5f; // carrier
  7533. }
  7534. auto make = [] (int bands, int order, int listen, bool swap, std::vector<float> levels, std::array<int, 12> c) {
  7535. lynx::fvocoder::State s;
  7536. s.bands = bands;
  7537. s.order = order;
  7538. s.listen = listen;
  7539. s.swap = swap;
  7540. for (std::size_t k = 0; k < levels.size(); ++k)
  7541. s.levels[k] = levels[k];
  7542. s.c = c;
  7543. return s;
  7544. };
  7545. auto run = [&] (const lynx::fvocoder::State& s, double rate, int block, int holdAt) {
  7546. std::vector<float> l = inL, r = inR;
  7547. lynx::fvocoder::Engine e;
  7548. e.setState (s);
  7549. e.prepare (rate);
  7550. for (int i = 0; i < 4096;)
  7551. {
  7552. auto len = std::min (block, 4096 - i);
  7553. if (holdAt > i && holdAt < i + len)
  7554. len = holdAt - i;
  7555. if (i == holdAt)
  7556. {
  7557. auto t = e.state();
  7558. t.c[lynx::fvocoder::hold] = 1;
  7559. e.setState (t);
  7560. }
  7561. e.processStereo (l.data() + i, r.data() + i, len);
  7562. i += len;
  7563. }
  7564. return std::make_pair (l, r);
  7565. };
  7566. auto check = [] (const std::pair<std::vector<float>, std::vector<float>>& out, const std::vector<std::pair<int, float>>& golden) {
  7567. for (const auto& [i, g] : golden)
  7568. CHECK (out.first[(std::size_t) i] == g && out.second[(std::size_t) i] == g);
  7569. };
  7570. // 16 bands, order 2, edited band levels, modulator and carrier levels in the mix
  7571. 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 },
  7572. { 0, 65536, 64, 0, 0, 50, 20, 100, 0, 20, 10, 128 });
  7573. const auto outA = run (a, 44100.0, 512, -1);
  7574. check (outA, { { 100, -0.23145467042922974f }, { 1000, 0.2901436686515808f }, { 2047, 0.03407000005245209f }, { 2500, 0.8314149379730225f },
  7575. { 3000, 0.606562614440918f }, { 3500, 0.2637328505516052f }, { 4095, -0.001206810586154461f } });
  7576. // 64 bands, order 3, inverted synthesis bank shifted by −300 cents, 48 kHz in blocks of 300, Hold from frame 2048
  7577. const auto b = make (64, 3, 2, false, {}, { 2000, 60000, 90, 1, -300, 120, 15, 300, 0, 0, 5, 150 });
  7578. check (run (b, 48000.0, 300, 2048), { { 100, 0.005184958688914776f }, { 1000, -0.0008608861826360226f }, { 2047, -0.026466617360711098f },
  7579. { 2500, -0.009371745400130749f }, { 3000, -0.018949978053569794f }, { 3500, -0.02667868137359619f },
  7580. { 4095, -0.021947938948869705f } });
  7581. // 4 bands, order 1, swapped inputs, narrow 30000…50000 range with the linear scale, 22.05 kHz in blocks of 77
  7582. 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 });
  7583. check (run (c, 22050.0, 77, -1), { { 100, 0.31434211134910583f }, { 1000, -1.953513264656067f }, { 2047, 1.4135023355484009f },
  7584. { 2500, 1.539392352104187f }, { 3000, -0.27189597487449646f }, { 3500, -0.11583124846220016f },
  7585. { 4095, -0.11583124846220016f } });
  7586. // FL's state imports into the LYNX slot (controls as parameters, the levels in the state) and the slot's effect
  7587. // (one 4096-frame call, split into 512-frame engine blocks) gives the same output
  7588. const auto bytes = lynx::fvocoder::serialise (a);
  7589. CHECK (bytes.size() == 17 + 16 * 4 + 12 * 4);
  7590. const auto slot = lynx::effectFromFl ("Fruity Vocoder", bytes);
  7591. CHECK (slot && slot->kind == lynx::EffectKind::vocoder && slot->state == bytes);
  7592. CHECK (slot && slot->params[0] == 0.4f && slot->params[1] == 0.5f && slot->params[2] == 1.0f && slot->params[15] == 0.8f);
  7593. if (slot)
  7594. {
  7595. auto fx = lynx::makeEffect (*slot);
  7596. fx->prepare (44100.0);
  7597. std::vector<float> l = inL, r = inR;
  7598. fx->process (l.data(), r.data(), 4096, slot->params, 120.0);
  7599. CHECK (l == outA.first && r == outA.second);
  7600. CHECK (lynx::formatEffectParam (lynx::EffectKind::vocoder, 5, slot->params[5], 120.0) == "16000 Hz");
  7601. CHECK (lynx::formatEffectParam (lynx::EffectKind::vocoder, 10, slot->params[10], 120.0) == "20 ms");
  7602. CHECK (lynx::formatEffectParam (lynx::EffectKind::vocoder, 15, slot->params[15], 120.0) == "1.00");
  7603. }
  7604. const auto parsed = lynx::fvocoder::parse (bytes);
  7605. CHECK (parsed && parsed->bands == 16 && parsed->levels[5] == 2.0f && parsed->c[lynx::fvocoder::release] == 100);
  7606. CHECK (! lynx::fvocoder::parse ({ 2, 0, 0, 0, 12, 0, 0, 0 })); // 12 bands is not a valid count
  7607. // a raw control outside the knob range (FL keeps it, REA_rodada78 §1) survives while the knob shows it, clamped
  7608. CHECK (lynx::fvocoder::rawFromKnob (0.0f, lynx::fvocoder::attack, 5) == 5);
  7609. CHECK (lynx::fvocoder::rawFromKnob (0.5f, lynx::fvocoder::attack, 5) == 505);
  7610. CHECK (lynx::fvocoder::rawFromKnob (1.0f, lynx::fvocoder::wetLevel, 250) == 250);
  7611. CHECK (lynx::fvocoder::rawFromKnob (0.5f, lynx::fvocoder::freqScale, 64) == 64);
  7612. }
  7613. // Delphi run-time math (REA_rodada70 §2): values the plugin's own exp / ln / SinCos return (EXEC, called in place)
  7614. TEST (delphi_math_matches_plugin_runtime)
  7615. {
  7616. // arguments where the C library differs from the Delphi run-time in the last bit
  7617. CHECK (lynx::dmath::exp (0x1.f6e5c5a26a7f8p+3) == 0x1.98170c3deea30p+22);
  7618. CHECK (lynx::dmath::exp (-0x1.12fc91a51d906p+4) == 0x1.27108f934a58cp-25);
  7619. CHECK (lynx::dmath::exp (-0x1.2e9e4c229e93ap+4) == 0x1.a3bf2e4ee3f1ep-28);
  7620. CHECK (lynx::dmath::ln (0x1.76a77e2a09abep-1) == -0x1.3fd111c5297f8p-2);
  7621. CHECK (lynx::dmath::ln (0x1.da1feaa8547dfp+0) == 0x1.3b8abd2756774p-1);
  7622. CHECK (lynx::dmath::ln (0x1.a1e28772a0d78p-1) == -0x1.9ffcaa3a11e30p-3);
  7623. double s = 0.0, c = 0.0;
  7624. lynx::dmath::sinCos (0x1.91b4a19e65a9bp-1, s, c);
  7625. CHECK (s == 0x1.69be278761916p-1 && c == 0x1.6a559572e1ac9p-1);
  7626. lynx::dmath::sinCos (0x1.3284a93d70a8ap-1, s, c);
  7627. CHECK (s == 0x1.2088b313ea643p-1 && c == 0x1.a6f4d889fd91ap-1);
  7628. lynx::dmath::sinCos (0x1.f9b2f53d6ce02p+0, s, c);
  7629. CHECK (s == 0x1.d6a9b8f170994p-1 && c == -0x1.931701be7b5f3p-2);
  7630. }
  7631. // Vocodex (REA_rodada35): a synthetic state in FL's layout (version 8, 13 maps, 20 parameters, the 0x24 block,
  7632. // 13 map-axis bytes, tone, notes)
  7633. struct VocodexSpec
  7634. {
  7635. // Wet, SG, ModVol, ModHP, CarVol, CarLP, CarNoise, CarNoiseHP, Hold, Att, AttCurve, Rel, RelCurve, Freeze, BW,
  7636. // Flatness, ModBW, ModPitch, FatPitch, FatPan
  7637. 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 };
  7638. std::array<std::int32_t, 7> block { 0, 2, 0, 24, 1, 1, 2 }; // hold mode, order, unison, bands, draft field, ModIn, CarIn
  7639. 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
  7640. std::vector<std::uint8_t> bytes() const
  7641. {
  7642. std::vector<std::uint8_t> d;
  7643. 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); };
  7644. auto i32 = [&] (std::int32_t v) { put (&v, 4); };
  7645. i32 (8 << 16);
  7646. for (int m = 0; m < 13; ++m)
  7647. {
  7648. const double y0 = m == 8 ? 0.1429 : (m == 3 || m == 4 || m == 11) ? 0.0 : 0.5;
  7649. const double y1 = m == 8 ? 0.9918 : y0;
  7650. i32 (1), i32 (0), i32 (2);
  7651. for (const auto& [dx, y] : { std::pair { 0.0, y0 }, std::pair { 1.0, y1 } })
  7652. {
  7653. put (&dx, 8), put (&y, 8);
  7654. i32 (0), i32 (0); // tension (f32 0), flags
  7655. }
  7656. const std::array<std::uint8_t, 20> tail {};
  7657. put (tail.data(), tail.size());
  7658. }
  7659. for (auto v : params)
  7660. i32 (v);
  7661. for (auto v : block)
  7662. i32 (v);
  7663. put (flags.data(), flags.size());
  7664. const std::array<std::uint8_t, 13> axis {};
  7665. put (axis.data(), axis.size());
  7666. d.push_back (3), put ("PWM", 3);
  7667. i32 (1), i32 (60);
  7668. return d;
  7669. }
  7670. };
  7671. std::vector<float> vocodexSaw (int n, double hz, double fs)
  7672. {
  7673. std::vector<float> out ((std::size_t) n);
  7674. for (int i = 0; i < n; ++i)
  7675. out[(std::size_t) i] = (float) (2.0 * std::fmod (i * hz / fs, 1.0) - 1.0) * 0.4f;
  7676. return out;
  7677. }
  7678. TEST (vocodex_state_parse_and_conversion)
  7679. {
  7680. VocodexSpec spec;
  7681. spec.block = { 1, 3, 2, 40, 0, 2, 1 };
  7682. spec.params[17] = -1200;
  7683. const auto raw = spec.bytes();
  7684. const auto s = lynx::vocodex::parseState (raw);
  7685. CHECK (s.has_value());
  7686. if (! s)
  7687. return;
  7688. CHECK (s->version == 8 && s->holdMode == 1 && s->order == 3 && s->unison == 2 && s->bands == 40);
  7689. CHECK (s->draft() && s->modIn == 2 && s->carIn == 1 && s->minTimes && s->subtractiveGate && ! s->lrEncoding);
  7690. CHECK (s->params[lynx::vocodex::modPitch] == -1200 && s->params[lynx::vocodex::carLp] == 65536);
  7691. 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);
  7692. CHECK (s->tone == "PWM" && s->notes.size() == 1 && s->notes[0] == 60);
  7693. const auto v = lynx::vocodexEffect (raw);
  7694. CHECK (v && v->slot.kind == lynx::EffectKind::vocodex && v->slot.state == raw && v->modIn == 2 && v->carIn == 1 && v->notes.empty());
  7695. spec.block[6] = -1; // internal synth carrier: not available yet → reported
  7696. spec.params[1] = 150; // Soundgoodizer stage
  7697. const auto w = lynx::vocodexEffect (spec.bytes());
  7698. CHECK (w && w->carIn == -1 && w->notes.size() == 2);
  7699. CHECK (! lynx::vocodex::parseState ({ 1, 2, 3 }).has_value());
  7700. }
  7701. TEST (vocodex_vocodes_side_inputs)
  7702. {
  7703. // modulator on side input 1 (noise in the first half), carrier on side input 2 (saw), own input silent: the
  7704. // output follows the modulator's envelope and fades after it stops
  7705. VocodexSpec spec;
  7706. lynx::EffectSlot slot = lynx::makeEffectSlot (lynx::EffectKind::vocodex);
  7707. slot.state = spec.bytes();
  7708. auto fx = lynx::makeEffect (slot);
  7709. fx->prepare (48000.0);
  7710. CHECK (fx->latencySamples() == 0);
  7711. const auto car = vocodexSaw (48000, 110.0, 48000.0);
  7712. std::vector<float> mod (48000), l (48000, 0.0f), r (48000, 0.0f);
  7713. std::uint32_t seed = 7;
  7714. for (std::size_t i = 0; i < mod.size(); ++i)
  7715. {
  7716. seed = seed * 1664525u + 1013904223u;
  7717. mod[i] = i < 24000 ? ((float) (seed >> 8) / 16777216.0f - 0.5f) : 0.0f;
  7718. }
  7719. for (int done = 0; done < 48000; done += 512)
  7720. {
  7721. const int n = std::min (512, 48000 - done);
  7722. fx->setSidechain (mod.data() + done, mod.data() + done);
  7723. fx->setSidechain2 (car.data() + done, car.data() + done);
  7724. fx->process (l.data() + done, r.data() + done, n, slot.params, 120.0);
  7725. }
  7726. auto rms = [&] (int from, int to) {
  7727. double e = 0.0;
  7728. for (int i = from; i < to; ++i)
  7729. e += (double) l[(std::size_t) i] * l[(std::size_t) i] + (double) r[(std::size_t) i] * r[(std::size_t) i];
  7730. return std::sqrt (e / (2.0 * (to - from)));
  7731. };
  7732. CHECK (rms (6000, 20000) > 0.02);
  7733. CHECK (rms (40000, 48000) < 0.01 * rms (6000, 20000));
  7734. CHECK (std::abs (l[0]) < 1e-6f); // the gains ramp up from 0 when the plugin is created
  7735. }
  7736. TEST (vocodex_draft_latency_and_pass_through)
  7737. {
  7738. // carrier pass-through at unit gain only (Wet 0): after a Flush the gains snap, so the output is the carrier,
  7739. // 16 samples late in Draft
  7740. VocodexSpec spec;
  7741. spec.params[0] = 0; // Wet
  7742. spec.params[4] = 12800; // CarVol = 1
  7743. for (const int draftField : { 1, 0 })
  7744. {
  7745. spec.block[4] = draftField;
  7746. const auto s = lynx::vocodex::parseState (spec.bytes());
  7747. CHECK (s.has_value());
  7748. lynx::vocodex::Engine engine;
  7749. engine.prepare (*s, 44100.0, 300, s->draft());
  7750. CHECK (engine.latencySamples() == (draftField == 0 ? 16 : 0));
  7751. engine.flush();
  7752. const auto saw = vocodexSaw (3000, 220.0, 44100.0);
  7753. std::vector<float> car (6000), mod (6000, 0.0f), out (6000);
  7754. for (int i = 0; i < 3000; ++i)
  7755. car[(std::size_t) (2 * i)] = car[(std::size_t) (2 * i + 1)] = saw[(std::size_t) i];
  7756. static constexpr int sizes[] { 300, 37, 128, 1, 255, 16, 299 }; // uneven blocks exercise the FIFOs
  7757. for (int done = 0, k = 0, n = 0; done < 3000; done += n, ++k)
  7758. {
  7759. n = std::min (sizes[k % 7], 3000 - done);
  7760. engine.process (mod.data() + 2 * done, mod.data() + 2 * done, car.data() + 2 * done, out.data() + 2 * done, n);
  7761. }
  7762. const int lag = engine.latencySamples();
  7763. auto maxError = 0.0f;
  7764. for (int i = lag; i < 3000; ++i)
  7765. maxError = std::max (maxError, std::abs (out[(std::size_t) (2 * i)] - saw[(std::size_t) (i - lag)]));
  7766. CHECK (maxError == 0.0f);
  7767. }
  7768. }
  7769. TEST (vocodex_soundgoodizer_stage_limits_the_output)
  7770. {
  7771. // SG > 0: the vocoder's output (loud here: Wet 2000) goes through the Soundgoodizer approximation, whose
  7772. // master limiter keeps the peaks under −0.2 dBFS; its look-ahead is reported as latency
  7773. VocodexSpec spec;
  7774. spec.params[0] = 2000;
  7775. spec.params[1] = 250;
  7776. lynx::EffectSlot slot = lynx::makeEffectSlot (lynx::EffectKind::vocodex);
  7777. slot.state = spec.bytes();
  7778. auto fx = lynx::makeEffect (slot);
  7779. fx->prepare (48000.0);
  7780. CHECK (fx->latencySamples() == 72); // 1.5 ms at 48 kHz
  7781. const auto car = vocodexSaw (48000, 110.0, 48000.0);
  7782. std::vector<float> mod (48000), l (48000, 0.0f), r (48000, 0.0f);
  7783. for (std::size_t i = 0; i < mod.size(); ++i)
  7784. mod[i] = 0.9f * (float) std::sin (0.07 * (double) i) * (float) std::sin (0.0003 * (double) i);
  7785. auto peak = 0.0f;
  7786. for (int done = 0; done < 48000; done += 512)
  7787. {
  7788. const int n = std::min (512, 48000 - done);
  7789. fx->setSidechain (mod.data() + done, mod.data() + done);
  7790. fx->setSidechain2 (car.data() + done, car.data() + done);
  7791. fx->process (l.data() + done, r.data() + done, n, slot.params, 120.0);
  7792. }
  7793. for (std::size_t i = 0; i < l.size(); ++i)
  7794. peak = std::max ({ peak, std::abs (l[i]), std::abs (r[i]) });
  7795. CHECK (peak > 0.5f && peak <= 0.9775f);
  7796. }
  7797. TEST (vocodex_tail_bypass_and_noise)
  7798. {
  7799. // both inputs silent for longer than 0.5 s + hold + release: the own input passes unchanged
  7800. VocodexSpec spec;
  7801. const auto s = lynx::vocodex::parseState (spec.bytes());
  7802. CHECK (s.has_value());
  7803. lynx::vocodex::Engine engine;
  7804. engine.prepare (*s, 44100.0, 512, false);
  7805. std::vector<float> zero (1024, 0.0f), own (1024), out (1024);
  7806. for (std::size_t i = 0; i < own.size(); ++i)
  7807. own[i] = 0.25f * (float) std::sin (0.01 * (double) i);
  7808. for (int b = 0; b < 80; ++b)
  7809. engine.process (own.data(), zero.data(), zero.data(), out.data(), 512);
  7810. CHECK (out == own);
  7811. // carrier noise: the IPP-compatible generator restarts from the same seed after a Flush
  7812. spec.params[6] = 9000;
  7813. const auto noisy = lynx::vocodex::parseState (spec.bytes());
  7814. lynx::vocodex::Engine a;
  7815. a.prepare (*noisy, 44100.0, 512, false);
  7816. std::vector<float> mod (1024), first (1024), second (1024);
  7817. for (std::size_t i = 0; i < mod.size(); ++i)
  7818. mod[i] = 0.3f * (float) std::sin (0.05 * (double) i);
  7819. a.flush();
  7820. a.process (own.data(), mod.data(), zero.data(), first.data(), 512);
  7821. a.flush();
  7822. a.process (own.data(), mod.data(), zero.data(), second.data(), 512);
  7823. CHECK (first == second);
  7824. auto energy = 0.0;
  7825. for (auto v : first)
  7826. energy += (double) v * v;
  7827. CHECK (energy > 1e-6 && std::isfinite (energy));
  7828. }
  7829. TEST (synth_spectral_oscillator)
  7830. {
  7831. // 1/n spectrum = saw: same harmonic proportions and same level as LYNX's saw
  7832. lynx::SynthSettings saw;
  7833. saw.osc[0] = { lynx::Waveform::saw, 0, 0.0f, 1.0f };
  7834. saw.cutoff = 20000.0f;
  7835. auto spectral = saw;
  7836. spectral.osc[0].wave = lynx::Waveform::spectral;
  7837. for (int n = 1; n <= lynx::SynthSettings::spectrumSize; ++n)
  7838. spectral.spectrum[(std::size_t) n - 1] = 1.0f / (float) n;
  7839. const auto a = renderSynthNote (saw, 57, 24000), b = renderSynthNote (spectral, 57, 24000); // 220 Hz
  7840. for (const auto h : { 1, 2, 3, 5 })
  7841. {
  7842. const auto ea = goertzel (a, 220.0 * h, 4800, 24000), eb = goertzel (b, 220.0 * h, 4800, 24000);
  7843. CHECK (std::abs (10.0 * std::log10 (eb / ea)) < 0.5); // within 0.5 dB
  7844. }
  7845. // odd only: no 2nd harmonic
  7846. auto odd = spectral;
  7847. for (int n = 2; n <= lynx::SynthSettings::spectrumSize; n += 2)
  7848. odd.spectrum[(std::size_t) n - 1] = 0.0f;
  7849. const auto c = renderSynthNote (odd, 57, 24000);
  7850. CHECK (goertzel (c, 440.0, 4800, 24000) < 1e-4 * goertzel (c, 220.0, 4800, 24000));
  7851. // high note: the band-limited tables don't pass Nyquist (no aliasing below the fundamental)
  7852. const auto d = renderSynthNote (spectral, 105, 24000); // 3520 Hz
  7853. CHECK (goertzel (d, 1000.0, 4800, 24000) < 1e-3 * goertzel (d, 3520.0, 4800, 24000));
  7854. }
  7855. TEST (compressor_sidechain_ducks_and_orders_mixer)
  7856. {
  7857. // the detector listens to the side input: quiet signal on the track, loud kick on the sidechain -> reduction
  7858. auto slot = lynx::makeEffectSlot (lynx::EffectKind::compressor);
  7859. slot.params[0] = 0.5f; // threshold
  7860. slot.params[1] = 1.0f; // high ratio
  7861. slot.params[4] = 0.0f;
  7862. auto run = [&] (bool withSidechain) {
  7863. auto fx = lynx::makeEffect (lynx::EffectKind::compressor);
  7864. fx->prepare (48000.0);
  7865. std::vector<float> l (4800, 0.1f), r (4800, 0.1f), kick (4800, 1.0f);
  7866. if (withSidechain)
  7867. fx->setSidechain (kick.data(), kick.data());
  7868. fx->process (l.data(), r.data(), 4800, slot.params, 120.0);
  7869. return l.back();
  7870. };
  7871. const auto alone = run (false), ducked = run (true);
  7872. CHECK (ducked < 0.5f * alone);
  7873. // order: track 5 listens to 7 via sidechain -> 7 comes before 5
  7874. auto song = lynx::Song::makeDefault();
  7875. while (song.mixer.size() < 8)
  7876. song.mixer.push_back ({});
  7877. auto comp = lynx::makeEffectSlot (lynx::EffectKind::compressor);
  7878. comp.sidechain = 7;
  7879. song.mixer[5].effects.push_back (comp);
  7880. const auto order = song.mixerOrder();
  7881. const auto at = [&] (int t) { return std::find (order.begin(), order.end(), t) - order.begin(); };
  7882. CHECK (at (7) < at (5));
  7883. }
  7884. TEST (pitch_correction_pulls_to_allowed_note)
  7885. {
  7886. auto run = [] (const lynx::EffectParams& p, double hz) {
  7887. auto fx = lynx::makeEffect (lynx::EffectKind::pitchCorrect);
  7888. fx->prepare (48000.0);
  7889. std::vector<float> l (48000 * 2), r;
  7890. for (std::size_t i = 0; i < l.size(); ++i)
  7891. l[i] = 0.5f * (float) std::sin (2.0 * 3.141592653589793 * hz * (double) i / 48000.0);
  7892. r = l;
  7893. fx->process (l.data(), r.data(), (int) l.size(), p, 120.0);
  7894. // frequency from zero crossings in the last second
  7895. int crossings = 0;
  7896. for (std::size_t i = 48000 + 1; i < l.size(); ++i)
  7897. crossings += (l[i - 1] < 0.0f) != (l[i] < 0.0f);
  7898. return crossings / 2.0;
  7899. };
  7900. auto slot = lynx::makeEffectSlot (lynx::EffectKind::pitchCorrect); // chromatic, instant, A 440
  7901. CHECK (std::abs (run (slot.params, 452.0) - 440.0) < 4.0);
  7902. for (int k = 0; k < 12; ++k)
  7903. slot.params[(std::size_t) (5 + k)] = k == 0 ? 1.0f : 0.0f; // only C
  7904. CHECK (std::abs (run (slot.params, 452.0) - 523.25) < 5.0); // C5 (523 Hz) is closer than C4 (262)
  7905. slot.params[2] = 1.0f; // turn everything off: passes unchanged
  7906. CHECK (std::abs (run (slot.params, 452.0) - 452.0) < 4.0);
  7907. }
  7908. TEST (pitch_correction_bypass_is_a_pure_delay)
  7909. {
  7910. // FL's Pitcher with Bypass All still runs its shifter: the plain path is an exact 9-sample delay, the formant
  7911. // path (256-Hann overlap-add) an exact 257-sample delay, and Gender changes nothing without a shift
  7912. // (REA_rodada30 §2.4/2.5); both are reported as latency
  7913. std::vector<float> in (48000);
  7914. for (std::size_t i = 0; i < in.size(); ++i)
  7915. in[i] = 0.5f * (float) std::sin (2.0 * 3.141592653589793 * 440.0 * (double) i / 48000.0)
  7916. + 0.2f * (float) std::sin (2.0 * 3.141592653589793 * 1234.5 * (double) i / 48000.0);
  7917. auto run = [&] (bool formant, float gender, int& latency) {
  7918. auto fx = lynx::makeEffect (lynx::EffectKind::pitchCorrect);
  7919. fx->prepare (48000.0);
  7920. auto slot = lynx::makeEffectSlot (lynx::EffectKind::pitchCorrect);
  7921. slot.params[2] = 1.0f;
  7922. slot.params[29] = formant ? 1.0f : 0.0f;
  7923. slot.params[30] = gender;
  7924. auto l = in, r = in;
  7925. for (std::size_t i = 0; i < l.size(); i += 480)
  7926. fx->process (l.data() + i, r.data() + i, 480, slot.params, 120.0);
  7927. latency = fx->latencySamples();
  7928. return l;
  7929. };
  7930. int latency = 0;
  7931. auto plain = run (false, 0.5f, latency);
  7932. CHECK (latency == 9);
  7933. auto worst = 0.0f;
  7934. for (std::size_t i = 4000; i < in.size(); ++i)
  7935. worst = std::max (worst, std::abs (plain[i] - in[i - 9]));
  7936. CHECK (worst < 1e-5f);
  7937. auto formant = run (true, 0.8f, latency);
  7938. CHECK (latency == 257);
  7939. worst = 0.0f;
  7940. for (std::size_t i = 4000; i < in.size(); ++i)
  7941. worst = std::max (worst, std::abs (formant[i] - in[i - 257]));
  7942. CHECK (worst < 1e-5f);
  7943. }
  7944. TEST (pitch_correction_formant_path)
  7945. {
  7946. // Formant on: PSOLA keeps the correction (output pitch = grain spacing) while Gender resamples the grains:
  7947. // the spectrum of a saw moves up with Gender > 0.5 and down with Gender < 0.5
  7948. auto run = [] (float gender) {
  7949. auto fx = lynx::makeEffect (lynx::EffectKind::pitchCorrect);
  7950. fx->prepare (48000.0);
  7951. auto slot = lynx::makeEffectSlot (lynx::EffectKind::pitchCorrect); // chromatic, instant, A 440
  7952. slot.params[29] = 1.0f;
  7953. slot.params[30] = gender;
  7954. std::vector<float> l (48000 * 2), r;
  7955. for (std::size_t i = 0; i < l.size(); ++i)
  7956. l[i] = 0.5f * (float) (2.0 * std::fmod (452.0 * (double) i / 48000.0, 1.0) - 1.0); // saw
  7957. r = l;
  7958. fx->process (l.data(), r.data(), (int) l.size(), slot.params, 120.0);
  7959. l.erase (l.begin(), l.begin() + 48000);
  7960. return l;
  7961. };
  7962. // period by autocorrelation (lags 60…160 = 300…800 Hz)
  7963. auto pitch = [] (const std::vector<float>& x) {
  7964. auto best = 0.0, bestLag = 0.0;
  7965. for (int lag = 60; lag <= 160; ++lag)
  7966. {
  7967. auto sum = 0.0;
  7968. for (std::size_t i = 0; i + (std::size_t) lag < x.size(); ++i)
  7969. sum += (double) x[i] * x[i + (std::size_t) lag];
  7970. if (sum > best)
  7971. {
  7972. best = sum;
  7973. bestLag = lag;
  7974. }
  7975. }
  7976. return 48000.0 / bestLag;
  7977. };
  7978. // brightness: energy of the first difference relative to the energy
  7979. auto brightness = [] (const std::vector<float>& x) {
  7980. auto e = 0.0, d = 0.0;
  7981. for (std::size_t i = 1; i < x.size(); ++i)
  7982. {
  7983. e += (double) x[i] * x[i];
  7984. d += (double) (x[i] - x[i - 1]) * (x[i] - x[i - 1]);
  7985. }
  7986. return d / e;
  7987. };
  7988. const auto neutral = run (0.5f), up = run (0.8f), down = run (0.3f);
  7989. CHECK (std::abs (pitch (neutral) - 440.0) < 5.0); // 452 Hz pulled to A
  7990. CHECK (std::abs (pitch (up) - 440.0) < 5.0); // Gender does not move the pitch
  7991. CHECK (brightness (up) > 1.5 * brightness (neutral));
  7992. CHECK (brightness (down) < 0.7 * brightness (neutral));
  7993. }
  7994. TEST (parallel_container_inner_links_modulate)
  7995. {
  7996. // one branch: utility (gain) + a control-only auto-pan LFO (1 beat); the link maps the LFO 0..1 onto the
  7997. // utility gain −24 dB..0 dB, so the level must swing; without the link it stays put
  7998. auto container = lynx::makeEffectSlot (lynx::EffectKind::parallel);
  7999. auto gain = lynx::makeEffectSlot (lynx::EffectKind::utility);
  8000. auto lfo = lynx::makeEffectSlot (lynx::EffectKind::autoPan);
  8001. lfo.params[0] = 1.0f / 64.0f;
  8002. lfo.params[4] = 1.0f; // control only: audio passes unchanged
  8003. container.branches.push_back ({ { gain, lfo }, 1.0f });
  8004. lynx::EffectLink link;
  8005. link.source = 1;
  8006. link.param = 0;
  8007. for (int i = 0; i <= 32; ++i)
  8008. link.curve.push_back (0.5f * (float) i / 32.0f);
  8009. auto levels = [&] (bool linked) {
  8010. auto slot = container;
  8011. if (linked)
  8012. slot.links.push_back (link);
  8013. auto fx = lynx::makeParallelEffect (slot);
  8014. fx->prepare (48000.0);
  8015. float lo = 1e9f, hi = 0.0f;
  8016. for (int block = 0; block < 94; ++block) // ~1 beat at 120 BPM in 256-sample blocks
  8017. {
  8018. std::vector<float> l (256, 1.0f), r (256, 1.0f);
  8019. fx->setSongPosition (block * 256.0 / 48000.0 * 2.0 * 960.0); // the engine positions every block
  8020. fx->process (l.data(), r.data(), 256, slot.params, 120.0);
  8021. lo = std::min (lo, std::abs (l[128]));
  8022. hi = std::max (hi, std::abs (l[128]));
  8023. }
  8024. return std::pair { lo, hi };
  8025. };
  8026. const auto [lo, hi] = levels (true);
  8027. CHECK (hi > 0.8f && lo < 0.12f);
  8028. const auto [lo0, hi0] = levels (false);
  8029. CHECK (std::abs (hi0 - lo0) < 1e-4f);
  8030. }
  8031. TEST (synth_spectrum_decays_per_harmonic)
  8032. {
  8033. // 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
  8034. lynx::SynthSettings s;
  8035. s.osc[0] = { lynx::Waveform::spectral, 0, 0.0f, 1.0f };
  8036. s.cutoff = 20000.0f;
  8037. s.decay = 0.0f;
  8038. s.sustain = 1.0f;
  8039. s.spectrum[0] = 1.0f;
  8040. s.spectrum[2] = 1.0f;
  8041. s.spectrumDecay[2] = 0.2f;
  8042. const auto x = renderSynthNote (s, 57, 48000); // 220 Hz, 48 kHz, 1 s
  8043. // equal 2400-sample windows (Goertzel power grows with the window): 10…60 ms and 500…550 ms
  8044. const auto early1 = goertzel (x, 220.0, 480, 2880), early3 = goertzel (x, 660.0, 480, 2880);
  8045. const auto late1 = goertzel (x, 220.0, 24000, 26400), late3 = goertzel (x, 660.0, 24000, 26400);
  8046. CHECK (early3 > 0.05 * early1); // ≈ −9 dB on average over 10…60 ms
  8047. CHECK (late3 < 1e-4 * late1);
  8048. CHECK (std::abs (10.0 * std::log10 (late1 / early1)) < 1.0); // the 1st holds
  8049. // without per-harmonic decay the spectrum stays as it is
  8050. s.spectrumDecay[2] = 0.0f;
  8051. const auto y = renderSynthNote (s, 57, 48000);
  8052. CHECK (std::abs (10.0 * std::log10 (goertzel (y, 660.0, 24000, 26400) / goertzel (y, 660.0, 480, 2880))) < 1.0);
  8053. }
  8054. TEST (worker_pool_runs_each_index_once_per_round)
  8055. {
  8056. // rounds of alternating sizes (a stalled worker must never run an index of a later round), each index
  8057. // exactly once, and nothing running after parallelFor returns
  8058. lynx::WorkerPool pool (3);
  8059. std::vector<std::atomic<int>> hits (64);
  8060. std::atomic<int> round { 0 }, outside { 0 }, wrongRound { 0 };
  8061. std::atomic<bool> open { false };
  8062. int failures = 0;
  8063. for (int r = 1; r <= 20000; ++r)
  8064. {
  8065. const auto n = (r % 3 == 0) ? 64 : (r % 3 == 1 ? 2 : 9);
  8066. for (int i = 0; i < n; ++i)
  8067. hits[(std::size_t) i].store (0);
  8068. round.store (r);
  8069. open.store (true);
  8070. auto fn = [&, r] (int i) {
  8071. if (! open.load())
  8072. outside.fetch_add (1);
  8073. if (round.load() != r)
  8074. wrongRound.fetch_add (1);
  8075. hits[(std::size_t) i].fetch_add (1);
  8076. };
  8077. pool.parallelFor (n, fn);
  8078. open.store (false);
  8079. for (int i = 0; i < n; ++i)
  8080. failures += hits[(std::size_t) i].load() != 1;
  8081. }
  8082. CHECK (failures == 0);
  8083. CHECK (outside.load() == 0);
  8084. CHECK (wrongRound.load() == 0);
  8085. }
  8086. TEST (worker_pool_wakes_sleeping_workers)
  8087. {
  8088. // rounds far apart (the workers have gone to sleep on the semaphore in between): every index still runs once,
  8089. // and the sleeping workers are woken (REA round 87: the Windows build used to poll with growing Sleep()s)
  8090. lynx::WorkerPool pool (3);
  8091. std::vector<std::atomic<int>> hits (16);
  8092. int failures = 0;
  8093. for (int r = 0; r < 12; ++r)
  8094. {
  8095. std::this_thread::sleep_for (std::chrono::microseconds (lynx::WorkerPool::spinMicros * 4));
  8096. for (auto& h : hits)
  8097. h.store (0);
  8098. auto fn = [&] (int i) {
  8099. std::this_thread::sleep_for (std::chrono::microseconds (200)); // long enough for woken workers to join
  8100. hits[(std::size_t) i].fetch_add (1);
  8101. };
  8102. pool.parallelFor (16, fn);
  8103. for (auto& h : hits)
  8104. failures += h.load() != 1;
  8105. }
  8106. CHECK (failures == 0);
  8107. CHECK (pool.wakeUps() > 0);
  8108. CHECK (pool.indicesRunByWorkers() > 0);
  8109. }
  8110. TEST (worker_pool_park_sends_spinning_workers_to_sleep)
  8111. {
  8112. // round 116: after park() (end of an audio block) the workers sleep at once instead of spinning for spinMicros, so
  8113. // the next parallelFor, well inside the spin window, has to wake them; and work right after park() still runs once
  8114. lynx::WorkerPool pool (3);
  8115. std::vector<std::atomic<int>> hits (8);
  8116. int failures = 0;
  8117. const auto before = pool.wakeUps();
  8118. for (int r = 0; r < 40; ++r)
  8119. {
  8120. for (auto& h : hits)
  8121. h.store (0);
  8122. auto fn = [&] (int i) {
  8123. std::this_thread::sleep_for (std::chrono::microseconds (100));
  8124. hits[(std::size_t) i].fetch_add (1);
  8125. };
  8126. pool.parallelFor (8, fn);
  8127. pool.park();
  8128. if (r % 2 == 0)
  8129. std::this_thread::sleep_for (std::chrono::microseconds (lynx::WorkerPool::spinMicros / 5));
  8130. for (auto& h : hits)
  8131. failures += h.load() != 1;
  8132. }
  8133. CHECK (failures == 0);
  8134. CHECK (pool.wakeUps() - before >= 20); // parked workers were woken, not found spinning
  8135. }
  8136. TEST (realtime_stats_count_deadline_misses_and_late_calls)
  8137. {
  8138. lynx::RtStats stats;
  8139. // 10 ms deadline: 3 ms, 9 ms (80-100 %), 12 ms (over), then a call 40 ms after the previous one (late)
  8140. stats.record (100.0, 3.0, 10.0);
  8141. stats.record (110.0, 9.0, 10.0);
  8142. stats.record (120.0, 12.0, 10.0);
  8143. stats.record (160.0, 2.0, 10.0);
  8144. const auto s = stats.snapshot();
  8145. CHECK (s.callbacks == 4);
  8146. CHECK (s.overBudget == 1);
  8147. CHECK (s.nearBudget == 1);
  8148. CHECK (s.lateCalls == 1);
  8149. CHECK (std::abs (s.maxLoad - 1.2) < 1e-9);
  8150. CHECK (std::abs (s.averageLoad - 26.0 / 40.0) < 1e-3);
  8151. CHECK (s.histogram[3] == 1 && s.histogram[9] == 1 && s.histogram[12] == 1 && s.histogram[2] == 1);
  8152. CHECK (std::abs (stats.takeWindowMaxLoad() - 1.2) < 1e-9 && stats.takeWindowMaxLoad() == 0.0);
  8153. // a device restart is not a late call
  8154. stats.deviceRestarted();
  8155. stats.record (5000.0, 1.0, 10.0);
  8156. CHECK (stats.snapshot().lateCalls == 1);
  8157. stats.reset();
  8158. CHECK (stats.snapshot().callbacks == 0 && stats.snapshot().maxLoad == 0.0);
  8159. }
  8160. TEST (safe_default_buffer_size)
  8161. {
  8162. const std::vector<int> wasapi { 144, 160, 192, 224, 256, 288, 320, 384, 448, 480, 512, 576, 640, 1024, 2048 };
  8163. CHECK (lynx::prefs::safeBufferSize (wasapi, 480, 48000.0) == 512); // the 10 ms default becomes 512
  8164. CHECK (lynx::prefs::safeBufferSize (wasapi, 1024, 48000.0) == 1024); // already larger: kept
  8165. CHECK (lynx::prefs::safeBufferSize (wasapi, 480, 96000.0) == 1024); // ~10.7 ms at 96 kHz
  8166. CHECK (lynx::prefs::safeBufferSize ({ 64, 128, 256 }, 128, 44100.0) == 256); // nothing reaches 512: the largest
  8167. CHECK (lynx::prefs::safeBufferSize ({}, 0, 44100.0) == 0);
  8168. }
  8169. TEST (linux_audio_driver_choice_and_pipewire_helpers)
  8170. {
  8171. // round 161: a first start on Linux opens PipeWire, then JACK, then ALSA
  8172. using lynx::prefs::DriverKind;
  8173. CHECK (lynx::prefs::driverKind ("PipeWire") == DriverKind::pipewire);
  8174. CHECK (lynx::prefs::preferredLinuxDriver ({ { "ALSA", true }, { "JACK", false }, { "PipeWire", true } }) == "PipeWire");
  8175. CHECK (lynx::prefs::preferredLinuxDriver ({ { "ALSA", true }, { "JACK", true } }) == "JACK");
  8176. CHECK (lynx::prefs::preferredLinuxDriver ({ { "ALSA", true }, { "JACK", false }, { "PipeWire", false } }) == "ALSA");
  8177. CHECK (lynx::prefs::preferredLinuxDriver ({ { "ALSA", false }, { "Bela", true } }) == "Bela");
  8178. CHECK (lynx::prefs::preferredLinuxDriver ({ { "ALSA", false } }).empty());
  8179. CHECK (lynx::prefs::pipewireLatency (256, 48000) == "256/48000");
  8180. CHECK (lynx::prefs::pipewireLatency (0, 0) == "1/1");
  8181. CHECK (lynx::prefs::pipewireChannelName ("FL", 0) == "Left");
  8182. CHECK (lynx::prefs::pipewireChannelName ("FR", 1) == "Right");
  8183. CHECK (lynx::prefs::pipewireChannelName ("MONO", 0) == "Mono");
  8184. CHECK (lynx::prefs::pipewireChannelName ("AUX0", 0) == "Aux 1");
  8185. CHECK (lynx::prefs::pipewireChannelName ("AUX15", 15) == "Aux 16");
  8186. CHECK (lynx::prefs::pipewireChannelName ("AUXX", 2) == "AUXX");
  8187. CHECK (lynx::prefs::pipewireChannelName ("", 2) == "Channel 3");
  8188. CHECK (lynx::prefs::pipewireDefaultName (R"({"name":"alsa_output.pci-0000_00_1f.3.analog-stereo"})") == "alsa_output.pci-0000_00_1f.3.analog-stereo");
  8189. CHECK (lynx::prefs::pipewireDefaultName (R"({ "name" : "a\"b" })") == "a\"b");
  8190. CHECK (lynx::prefs::pipewireDefaultName ("{}").empty());
  8191. CHECK (lynx::prefs::pipewireDefaultName (R"({"name":"unterminated)").empty());
  8192. CHECK ((lynx::prefs::uniqueDeviceNames ({ "USB Audio", "HDMI", "USB Audio", "USB Audio" })
  8193. == std::vector<std::string> { "USB Audio", "HDMI", "USB Audio (2)", "USB Audio (3)" }));
  8194. }
  8195. TEST (driver_kind_and_shared_mode_buffer_sizes)
  8196. {
  8197. // REA round 97: classic shared WASAPI mixes in whole engine periods, so sizes below it are not offered
  8198. using lynx::prefs::DriverKind;
  8199. CHECK (lynx::prefs::driverKind ("Windows Audio") == DriverKind::wasapiShared);
  8200. CHECK (lynx::prefs::driverKind ("Windows Audio (Exclusive Mode)") == DriverKind::wasapiExclusive);
  8201. CHECK (lynx::prefs::driverKind ("Windows Audio (Low Latency Mode)") == DriverKind::wasapiLowLatency);
  8202. CHECK (lynx::prefs::driverKind ("ASIO") == DriverKind::asio);
  8203. CHECK (lynx::prefs::driverKind ("LYNX ASIO") == DriverKind::lynxAsio);
  8204. CHECK (lynx::prefs::driverKind ("DirectSound") == DriverKind::directSound);
  8205. CHECK (lynx::prefs::driverKind ("ALSA") == DriverKind::alsa);
  8206. CHECK (lynx::prefs::driverKind ("JACK") == DriverKind::jack);
  8207. CHECK (lynx::prefs::driverKind ("CoreAudio") == DriverKind::coreAudio);
  8208. CHECK (lynx::prefs::driverKind ("Some Other Driver") == DriverKind::other);
  8209. const std::vector<int> offered { 144, 192, 256, 480, 512, 1024 };
  8210. CHECK ((lynx::prefs::usableBufferSizes (DriverKind::wasapiShared, offered, 480) == std::vector<int> { 480, 512, 1024 }));
  8211. CHECK (lynx::prefs::usableBufferSizes (DriverKind::wasapiExclusive, offered, 480) == offered); // the device's real periods
  8212. CHECK (lynx::prefs::usableBufferSizes (DriverKind::wasapiLowLatency, offered, 480) == offered); // engine periods
  8213. CHECK (lynx::prefs::usableBufferSizes (DriverKind::wasapiShared, { 64, 128 }, 480) == (std::vector<int> { 64, 128 })); // never empty
  8214. CHECK (lynx::prefs::usableBufferSizes (DriverKind::wasapiShared, offered, 0) == offered);
  8215. }
  8216. TEST (linear_phase_convolver_matches_direct_convolution)
  8217. {
  8218. // the block FFT convolver (cached twiddles, REA round 87) equals the direct convolution with its kernel
  8219. lynx::peqfl::FftConvolver c;
  8220. c.init (256);
  8221. std::vector<float> table (129, 1.0f);
  8222. for (int k = 40; k < 129; ++k)
  8223. table[(std::size_t) k] = 0.25f; // a high shelf cut
  8224. c.setKernel (table);
  8225. // impulse response through step()
  8226. std::vector<float> ir (600, 0.0f);
  8227. for (int i = 0; i < 600; ++i)
  8228. {
  8229. float l = i == 0 ? 1.0f : 0.0f, r = 0.0f;
  8230. c.step (l, r);
  8231. ir[(std::size_t) i] = l;
  8232. }
  8233. // then a noise signal, compared with direct convolution by that impulse response
  8234. lynx::peqfl::FftConvolver d;
  8235. d.init (256);
  8236. d.setKernel (table);
  8237. std::vector<float> x (2000), y (2000);
  8238. std::uint32_t seed = 1;
  8239. for (auto& v : x)
  8240. v = (float) ((seed = seed * 1664525u + 1013904223u) >> 8) / 16777216.0f - 0.5f;
  8241. for (std::size_t i = 0; i < x.size(); ++i)
  8242. {
  8243. float l = x[i], r = 0.0f;
  8244. d.step (l, r);
  8245. y[i] = l;
  8246. }
  8247. double worst = 0.0;
  8248. for (std::size_t i = 0; i < x.size(); ++i)
  8249. {
  8250. double direct = 0.0;
  8251. for (std::size_t k = 0; k < ir.size() && k <= i; ++k)
  8252. direct += (double) ir[k] * (double) x[i - k];
  8253. worst = std::max (worst, std::abs (direct - (double) y[i]));
  8254. }
  8255. CHECK (worst < 1e-4);
  8256. // and it delays by 3n/4 with the kernel's centre there
  8257. 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());
  8258. CHECK (peak == 192);
  8259. }
  8260. TEST (patcher_container_is_reused_across_snapshots)
  8261. {
  8262. // an unchanged Patcher keeps its built effect (its children's tails, delay lines and PDC stay put), and a
  8263. // linear-phase Frequency Splitter reports its latency before its first block (REA round 87: it used to be
  8264. // rebuilt on every snapshot and its latency appeared one block late, re-planning PDC in a loop)
  8265. auto song = lynx::Song::makeDefault();
  8266. if (song.mixer.size() < 2)
  8267. song.mixer.resize (2);
  8268. lynx::EffectSlot splitter;
  8269. splitter.kind = lynx::EffectKind::frequencySplitter;
  8270. splitter.params = lynx::makeEffectSlot (lynx::EffectKind::frequencySplitter).params;
  8271. splitter.params[4] = 0.6f; // linear-phase mode
  8272. splitter.params[6] = 0.0f; // quality 0
  8273. lynx::EffectSlot patcher;
  8274. patcher.kind = lynx::EffectKind::parallel;
  8275. patcher.branches.push_back ({ { splitter }, 1.0f });
  8276. patcher.branches.push_back ({ {}, 0.5f });
  8277. song.mixer[1].effects = { patcher };
  8278. const auto a = lynx::RackState::build (song, nullptr, 44100.0);
  8279. const auto b = lynx::RackState::build (song, a.get(), 44100.0);
  8280. CHECK (a->effects[1][0] == b->effects[1][0]);
  8281. CHECK (a->effects[1][0]->latencySamples() == 384); // 3/4 of the 512-point FFT, before any block
  8282. CHECK (a->latencies == a->currentLatencies());
  8283. song.mixer[1].effects[0].branches[0].chain[0].params[0] = 0.25f; // a child's parameter changed: rebuilt
  8284. const auto c = lynx::RackState::build (song, b.get(), 44100.0);
  8285. CHECK (c->effects[1][0] != b->effects[1][0]);
  8286. song.mixer[1].effects[0].branches[1].gain = 0.75f;
  8287. const auto d = lynx::RackState::build (song, c.get(), 44100.0);
  8288. CHECK (d->effects[1][0] != c->effects[1][0]);
  8289. }
  8290. TEST (ladder_filter_follows_toxic_law)
  8291. {
  8292. // the 4-pole ladder: −24 dB/oct far above the cutoff (vs −12 for the SVF), resonance raises the corner, and
  8293. // the input saturation keeps it bounded even at full resonance
  8294. auto level = [] (bool ladder, float res, double hz) {
  8295. lynx::SynthSettings s;
  8296. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  8297. s.cutoff = 500.0f;
  8298. s.resonance = res;
  8299. s.ladder = ladder;
  8300. const auto key = (int) std::lround (69.0 + 12.0 * std::log2 (hz / 440.0));
  8301. const auto x = renderSynthNote (s, key, 48000);
  8302. double e = 0.0, peak = 0.0;
  8303. for (std::size_t i = 24000; i < 48000; ++i)
  8304. e += (double) x[i] * x[i], peak = std::max (peak, (double) std::abs (x[i]));
  8305. return std::pair { std::sqrt (e / 24000.0), peak };
  8306. };
  8307. 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
  8308. const auto svfHigh = level (false, 0.0f, 3520.0).first / level (false, 0.0f, 110.0).first;
  8309. CHECK (20.0 * std::log10 (high / low) < -55.0); // ≈ 24 dB/oct over ~2.8 octaves
  8310. CHECK (20.0 * std::log10 (svfHigh) > -45.0); // the 2-pole SVF falls much less
  8311. const auto corner0 = level (true, 0.0f, 523.25).first, corner1 = level (true, 0.6f, 523.25).first;
  8312. CHECK (corner1 > 1.5 * corner0); // resonance boosts near the cutoff
  8313. const auto loud = level (true, 1.0f, 523.25);
  8314. CHECK (std::isfinite (loud.first) && loud.second < 20.0); // self-oscillation stays bounded
  8315. }
  8316. TEST (synth_spectrum_settles_on_target)
  8317. {
  8318. // ALT DECAY-like morph: harmonic 3 starts at 1 and settles on 0.25 (−12 dB) instead of fading out
  8319. lynx::SynthSettings s;
  8320. s.osc[0] = { lynx::Waveform::spectral, 0, 0.0f, 1.0f };
  8321. s.cutoff = 20000.0f;
  8322. s.decay = 0.0f;
  8323. s.sustain = 1.0f;
  8324. s.spectrum[0] = 1.0f;
  8325. s.spectrum[2] = 1.0f;
  8326. s.spectrumDecay[2] = 0.1f;
  8327. s.spectrumTarget[2] = 0.25f;
  8328. const auto x = renderSynthNote (s, 57, 48000);
  8329. const auto ratio = goertzel (x, 660.0, 24000, 26400) / goertzel (x, 220.0, 24000, 26400);
  8330. CHECK (std::abs (10.0 * std::log10 (ratio) - 20.0 * std::log10 (0.25)) < 1.0);
  8331. }
  8332. TEST (kepler_chorus_is_stereo_and_keeps_level)
  8333. {
  8334. // chorus I on a sine: L and R differ (opposite LFO phases), the level stays close to the dry one
  8335. lynx::SynthSettings s;
  8336. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  8337. s.cutoff = 20000.0f;
  8338. auto dry = s;
  8339. s.chorus = 1;
  8340. auto render = [] (const lynx::SynthSettings& set) {
  8341. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  8342. inst->prepare (48000.0);
  8343. lynx::ChannelDef c;
  8344. c.synth = set;
  8345. inst->configure (c, 120.0);
  8346. inst->noteOn (69, 1.0f);
  8347. std::vector<float> l (96000), r (96000);
  8348. inst->render (l.data(), r.data(), 96000, 1.0f, 1.0f);
  8349. return std::pair { l, r };
  8350. };
  8351. const auto [l, r] = render (s);
  8352. const auto [dl, dr] = render (dry);
  8353. double diff = 0.0, e = 0.0, ed = 0.0;
  8354. for (std::size_t i = 48000; i < 96000; ++i)
  8355. diff += std::abs (l[i] - r[i]), e += (double) l[i] * l[i], ed += (double) dl[i] * dl[i];
  8356. CHECK (diff / 48000.0 > 1e-3);
  8357. CHECK (std::abs (10.0 * std::log10 (e / ed)) < 3.0);
  8358. }
  8359. TEST (fl_pan_law_matches_love_philter_dll)
  8360. {
  8361. // values printed by the installation's Love Philter pan routine for vol 0.8 (REA_rodada27 §3.1, lppan.txt)
  8362. struct Row { double pan, ll, rr, rl, lr; };
  8363. const Row rows[] = { { -1.5, 0.239782482, -0.432956904, 0.805467963, 0.0 }, { -0.5, 0.805467963, 0.432956904, 0.239782482, 0.0 },
  8364. { 0.0, 0.8, 0.8, 0.0, 0.0 }, { 0.75, 0.220719516, 0.707889318, 0.0, 0.401742548 },
  8365. { 1.0, 0.0, 0.565685451, 0.0, 0.565685451 } };
  8366. for (const auto& row : rows)
  8367. {
  8368. float ll, rl, lr, rr;
  8369. lynx::flPanMatrix (row.pan, ll, rl, lr, rr);
  8370. CHECK (std::abs (0.8 * ll - row.ll) < 1e-6 && std::abs (0.8 * rr - row.rr) < 1e-6);
  8371. CHECK (std::abs (0.8 * rl - row.rl) < 1e-6 && std::abs (0.8 * lr - row.lr) < 1e-6);
  8372. }
  8373. }
  8374. TEST (effector_units_match_fl_dll_impulse_responses)
  8375. {
  8376. // impulse responses of the installation's Effector engine (REA_rodada27 §2, eu_<kind>_<X>_<Y>.txt; LFO off,
  8377. // wet 1, blocks of 256): samples 1, 10, 100 and 1000
  8378. struct Case { int kind; float x, y; double v[4]; };
  8379. const Case cases[] = { { 3, 0.5f, 0.5f, { -0.603055418, 0.159798339, -0.0055043716, -1.06241259e-06 } },
  8380. { 4, 0.2f, 0.5f, { 4.68885949e-26, 4.20032622e-19, 0.000487731799, 2.27715846e-09 } },
  8381. { 4, 0.8f, 0.3f, { -0.293783396, -0.0306414738, -0.00068128563, 1.358577e-15 } } };
  8382. for (const auto& c : cases)
  8383. {
  8384. auto fx = lynx::effector::make();
  8385. fx->prepare (44100.0);
  8386. lynx::EffectParams p {};
  8387. p[lynx::effector::type] = c.kind / 11.0f;
  8388. p[lynx::effector::wetMix] = 1.0f;
  8389. p[lynx::effector::paramX] = c.x;
  8390. p[lynx::effector::paramY] = c.y;
  8391. p[lynx::effector::modX] = p[lynx::effector::modY] = 0.5f;
  8392. p[lynx::effector::modRate] = 0.5f;
  8393. p[lynx::effector::inputLevel] = 1.0f;
  8394. p[lynx::effector::outputGain] = 0.5f;
  8395. std::vector<float> l (1024, 0.0f), r (1024, 0.0f);
  8396. l[0] = r[0] = 1.0f;
  8397. for (int d = 0; d < 1024; d += 256)
  8398. fx->process (l.data() + d, r.data() + d, 256, p, 120.0);
  8399. const int at[4] { 1, 10, 100, 1000 };
  8400. for (int k = 0; k < 4; ++k)
  8401. CHECK (std::abs (l[(std::size_t) at[k]] - c.v[k]) < 2e-7 + 1e-5 * std::abs (c.v[k]));
  8402. }
  8403. }
  8404. TEST (harmless_stereo_tremolo_and_vibrato)
  8405. {
  8406. // tremolo d = 1: each side swings between 0 and 1.25 at 5 Hz, L and R in anti-phase; vibrato modulates the pitch
  8407. lynx::SynthSettings s;
  8408. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  8409. s.cutoff = 20000.0f;
  8410. s.stereoTremolo = 1.0f;
  8411. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  8412. inst->prepare (48000.0);
  8413. lynx::ChannelDef c;
  8414. c.synth = s;
  8415. inst->configure (c, 120.0);
  8416. inst->noteOn (69, 1.0f);
  8417. std::vector<float> l (48000), r (48000);
  8418. inst->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
  8419. 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; };
  8420. // θ = 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
  8421. CHECK (envelope (l, 19200) > 4.0f * envelope (r, 19200));
  8422. CHECK (envelope (r, 24000) > 4.0f * envelope (l, 24000));
  8423. // vibrato 100 cents: zero crossings vary in spacing; without it they are regular
  8424. auto crossingsSpread = [] (float cents) {
  8425. lynx::SynthSettings v;
  8426. v.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  8427. v.cutoff = 20000.0f;
  8428. v.vibratoCents = cents;
  8429. const auto x = renderSynthNote (v, 69, 48000);
  8430. std::vector<int> zc;
  8431. for (int i = 24000; i < 48000; ++i)
  8432. if (x[(std::size_t) i - 1] < 0.0f && x[(std::size_t) i] >= 0.0f)
  8433. zc.push_back (i);
  8434. int lo = 1 << 30, hi = 0;
  8435. for (std::size_t k = 1; k < zc.size(); ++k)
  8436. lo = std::min (lo, zc[k] - zc[k - 1]), hi = std::max (hi, zc[k] - zc[k - 1]);
  8437. return hi - lo;
  8438. };
  8439. CHECK (crossingsSpread (0.0f) <= 1);
  8440. CHECK (crossingsSpread (100.0f) >= 4);
  8441. }
  8442. TEST (physical_models_sound_and_end)
  8443. {
  8444. // Sakura: a plucked string with default-ish parameters rings and fades after the release; Drumpad: a hit decays
  8445. for (const auto kind : { lynx::InstrumentKind::sakura, lynx::InstrumentKind::drumpad })
  8446. {
  8447. lynx::ChannelDef c;
  8448. c.instrument = kind;
  8449. if (kind == lynx::InstrumentKind::sakura)
  8450. {
  8451. c.modelParams.assign (146, 0.5f);
  8452. c.modelParams[111] = 3.0f; // both strings at the note
  8453. c.modelParams[113] = 0.5f; // octave ×1
  8454. c.modelParams[6] = c.modelParams[11] = 0.99f; // long decay
  8455. c.modelParams[31] = 0.0f; // noise exciter
  8456. }
  8457. else
  8458. {
  8459. c.modelParams.assign (38, 0.5f);
  8460. c.modelParams[0] = 1.0f; // retrigger noise
  8461. c.modelParams[30] = c.modelParams[31] = c.modelParams[32] = c.modelParams[33] = 0.0f; // no velocity morph
  8462. }
  8463. auto inst = lynx::makeInstrument (kind);
  8464. inst->prepare (48000.0);
  8465. inst->configure (c, 120.0);
  8466. inst->noteOn (60, 0.8f);
  8467. std::vector<float> l (48000), r (48000);
  8468. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  8469. inst->noteOff (60);
  8470. inst->render (l.data() + 24000, r.data() + 24000, 24000, 1.0f, 1.0f);
  8471. double early = 0.0;
  8472. for (int i = 0; i < 12000; ++i)
  8473. early += (double) l[(std::size_t) i] * l[(std::size_t) i];
  8474. CHECK (early > 1e-6);
  8475. CHECK (std::all_of (l.begin(), l.end(), [] (float v) { return std::isfinite (v); }));
  8476. std::vector<float> tail (480000), tailR (480000);
  8477. inst->render (tail.data(), tailR.data(), 480000, 1.0f, 1.0f); // 10 s later the voice has ended
  8478. CHECK (inst->idle());
  8479. }
  8480. }
  8481. TEST (squeeze_matches_fl_model)
  8482. {
  8483. // Fruity Squeeze: Steps quantises, Mix 0 passes the input dry ×Volume/127; a silent block is left untouched
  8484. auto slot = lynx::makeEffectSlot (lynx::EffectKind::squeeze);
  8485. slot.params[1] = 0.0f; // mix 0: dry only
  8486. auto fx = lynx::makeEffect (lynx::EffectKind::squeeze);
  8487. fx->prepare (44100.0);
  8488. std::vector<float> l (512, 0.3f), r (512, -0.2f);
  8489. fx->process (l.data(), r.data(), 512, slot.params, 120.0);
  8490. CHECK (std::abs (l[100] - 0.3f) < 1e-6f && std::abs (r[100] + 0.2f) < 1e-6f); // Volume 127/127
  8491. slot.params[1] = 1.0f; // wet only
  8492. slot.params[2] = 32000.0f / 32768.0f; // 32768^(1 − 32000/32768) ≈ 1.27 levels per unit → heavy quantisation
  8493. std::vector<float> a (512), b (512);
  8494. for (int i = 0; i < 512; ++i)
  8495. a[(std::size_t) i] = b[(std::size_t) i] = 0.4f * (float) std::sin (i * 0.05);
  8496. fx->process (a.data(), b.data(), 512, slot.params, 120.0);
  8497. const auto levels = std::pow (32768.0f, 1.0f - 32000.0f / 32768.0f);
  8498. for (int i = 0; i < 512; i += 37)
  8499. CHECK (std::abs (a[(std::size_t) i] * levels - std::nearbyint (a[(std::size_t) i] * levels)) < 1e-3f);
  8500. std::vector<float> z (512, 0.0f), z2 (512, 0.0f);
  8501. fx->process (z.data(), z2.data(), 512, slot.params, 120.0);
  8502. CHECK (z[10] == 0.0f);
  8503. }
  8504. TEST (sakura_matrix_and_effects)
  8505. {
  8506. // REA_rodada29 §1: velocity → level follows 10^(2m)·0.05 (vel 1 vs 0.5 → ×10 in amplitude); the delay leaves
  8507. // an echo after the note has ended and keeps the instrument from going idle until its tail is silent
  8508. auto base = [] {
  8509. lynx::ChannelDef c;
  8510. c.instrument = lynx::InstrumentKind::sakura;
  8511. c.modelParams.assign (146, 0.0f);
  8512. const std::pair<int, float> values[] { { 0, 0.375f }, { 1, 0.5f }, { 3, 0.5f }, { 4, 1.0f }, { 6, 0.95f }, { 9, 0.3f },
  8513. { 11, 0.95f }, { 14, 0.3f }, { 18, 0.5f }, { 19, 0.5f }, { 27, 0.5f }, { 28, 1.0f },
  8514. { 29, 0.7f }, { 31, 1.0f }, { 32, 0.5f }, { 111, 3.0f }, { 113, 0.5f } };
  8515. for (const auto& [i, v] : values)
  8516. c.modelParams[(std::size_t) i] = v;
  8517. return c;
  8518. };
  8519. auto render = [] (const lynx::ChannelDef& c, float velocity, int held, int total) {
  8520. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sakura);
  8521. inst->prepare (48000.0);
  8522. inst->configure (c, 120.0);
  8523. inst->noteOn (60, velocity);
  8524. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  8525. for (int s = 0; s < total; s += 256)
  8526. {
  8527. if (s == held)
  8528. inst->noteOff (60);
  8529. inst->render (l.data() + s, r.data() + s, std::min (256, total - s), 1.0f, 1.0f);
  8530. }
  8531. return std::make_pair (l, std::move (inst));
  8532. };
  8533. auto energy = [] (const std::vector<float>& x, int from, int to) {
  8534. double e = 0.0;
  8535. for (int i = from; i < to; ++i)
  8536. e += (double) x[(std::size_t) i] * x[(std::size_t) i];
  8537. return e;
  8538. };
  8539. auto c = base();
  8540. c.modelParams[79] = 6.0f; // slot 1: velocity → level, amount 1
  8541. c.modelParams[80] = 1.0f;
  8542. c.modelParams[82] = 1.0f;
  8543. const auto loud = energy (render (c, 1.0f, 24064, 24064).first, 0, 24064);
  8544. const auto soft = energy (render (c, 0.5f, 24064, 24064).first, 0, 24064);
  8545. CHECK (loud > 1e-6 && std::abs (loud / soft - 100.0) < 1.0);
  8546. auto d = base();
  8547. auto [dry, dryInst] = render (d, 0.8f, 4864, 72000);
  8548. d.modelParams[63] = 1.0f; // delay on: time 0.5 → ≈ 0.52 s, wet 1, no feedback: one echo
  8549. d.modelParams[64] = 0.5f;
  8550. d.modelParams[69] = 1.0f;
  8551. auto [wet, wetInst] = render (d, 0.8f, 4864, 72000);
  8552. CHECK (energy (wet, 26000, 40000) > 100.0 * energy (dry, 26000, 40000) + 1e-9); // the note ends near 0.2 s
  8553. CHECK (dryInst->idle() || energy (dry, 70000, 72000) < 1e-12);
  8554. CHECK (! wetInst->idle()); // the echo tail has not been silent for 3 s yet
  8555. }
  8556. TEST (drumsynth_live_engine)
  8557. {
  8558. // REA_rodada30 §1: key → patch slot, state parsing, the engine against our reference model (no DLL: synthesised
  8559. // sources only, noise from the fallback seed), voice modes, patch level/pan
  8560. CHECK (lynx::drumSynthKeyToSlot (48) == 0 && lynx::drumSynthKeyToSlot (71) == 23 && lynx::drumSynthKeyToSlot (0) == 24);
  8561. CHECK (lynx::drumSynthKeyToSlot (47) == 71 && lynx::drumSynthKeyToSlot (72) == 72 && lynx::drumSynthKeyToSlot (119) == 119);
  8562. CHECK (lynx::drumSynthKeyToSlot (120) == -1);
  8563. // a version 3 state: 120 names, 120 × 38 ints (send track −1 becomes 0 below version 4), flags
  8564. std::vector<std::uint8_t> blob;
  8565. auto put = [&] (std::uint32_t v) { for (int k = 0; k < 4; ++k) blob.push_back ((std::uint8_t) (v >> (8 * k))); };
  8566. put (3);
  8567. put (66);
  8568. for (int i = 0; i < 120; ++i)
  8569. blob.push_back (2), blob.push_back ('a'), blob.push_back ((std::uint8_t) ('0' + i % 10));
  8570. for (int i = 0; i < 120 * 38; ++i)
  8571. put ((std::uint32_t) (i % 38 == 36 ? -1 : i % 38 == 4 ? 300 : 0));
  8572. put (0);
  8573. const auto parsed = lynx::drumSynthLiveParams (blob);
  8574. CHECK (parsed && (int) parsed->size() == lynx::drumSynthParamCount);
  8575. CHECK (parsed && (*parsed)[36] == 0.0f && (*parsed)[38 * 7 + 4] == 300.0f && (*parsed)[(std::size_t) lynx::drumSynthEditorKeyIndex] == 66.0f);
  8576. blob.resize (500);
  8577. CHECK (! lynx::drumSynthLiveParams (blob));
  8578. // patch on key 60 (slot 12): tone-mode Osc1 with sweep and click, noise-band Osc2, ring, 2 retriggers
  8579. 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,
  8580. 0, 0, 0, 0, 0, 0, 30, -1, 0, 0, 64 };
  8581. lynx::ChannelDef c;
  8582. c.instrument = lynx::InstrumentKind::drumsynth;
  8583. c.modelParams.assign ((std::size_t) lynx::drumSynthParamCount, 0.0f);
  8584. for (int s = 0; s < 120; ++s)
  8585. c.modelParams[(std::size_t) (38 * s + 15)] = c.modelParams[(std::size_t) (38 * s + 34)] = -1.0f;
  8586. for (int i = 0; i < 38; ++i)
  8587. c.modelParams[(std::size_t) (38 * 12 + i)] = (float) patch[i];
  8588. auto render = [] (const lynx::ChannelDef& def, std::vector<int> keys, int total, float gl = 1.0f, float gr = 1.0f) {
  8589. auto inst = lynx::makeInstrument (lynx::InstrumentKind::drumsynth);
  8590. inst->configure (def, 120.0);
  8591. inst->prepare (48000.0);
  8592. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  8593. for (int s = 0; s < total; s += 512)
  8594. {
  8595. for (std::size_t k = 0; k + 1 < keys.size(); k += 2)
  8596. if (keys[k + 1] == s)
  8597. inst->noteOn (keys[k], 100.0f / 128.0f);
  8598. inst->render (l.data() + s, r.data() + s, std::min (512, total - s), gl, gr);
  8599. }
  8600. return std::make_tuple (l, r, std::move (inst));
  8601. };
  8602. {
  8603. auto [l, r, inst] = render (c, { 60, 0 }, 9600);
  8604. // reference model (REA round 30 dsl_model.py, seed 0x56CE, FL velocity 100, V = 1, centre: × g(100)·cos(π/4))
  8605. const std::pair<int, float> golden[] { { 0, -0.0074092713f }, { 10, -0.13961405f }, { 100, -0.47290668f }, { 1000, -0.29188696f },
  8606. { 3000, -0.22130175f }, { 6000, -0.049537566f }, { 9599, 0.010392102f } };
  8607. for (const auto& [i, v] : golden)
  8608. CHECK (std::abs (l[(std::size_t) i] - v) < 2e-6f && std::abs (r[(std::size_t) i] - v) < 2e-6f);
  8609. std::vector<float> tail (48000 * 4), tailR (tail.size());
  8610. inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
  8611. CHECK (inst->idle()); // the engine reported the voice finished
  8612. }
  8613. auto energy = [] (const std::vector<float>& x, int from, int to) {
  8614. double e = 0.0;
  8615. for (int i = from; i < to; ++i)
  8616. e += (double) x[(std::size_t) i] * x[(std::size_t) i];
  8617. return e;
  8618. };
  8619. // a key without a patch level (slot 0, level 0) and a key outside the 120 patches are silent
  8620. CHECK (energy (std::get<0> (render (c, { 48, 0, 125, 0 }, 4096)), 0, 4096) == 0.0);
  8621. // mono: a second hit on the same key cuts the first; poly: both ring together (Osc2 off: no noise, deterministic)
  8622. auto tone = c;
  8623. tone.modelParams[38 * 12 + 14] = 0.0f;
  8624. const auto single = std::get<0> (render (tone, { 60, 0 }, 8192));
  8625. const auto mono = std::get<0> (render (tone, { 60, 0, 60, 2048 }, 8192));
  8626. auto poly = tone;
  8627. poly.modelParams[38 * 12 + 24] = 1.0f;
  8628. const auto both = std::get<0> (render (poly, { 60, 0, 60, 2048 }, 8192));
  8629. double monoDiff = 0.0, polyDiff = 0.0;
  8630. for (int i = 2048 + 300; i < 8192; ++i) // after the cut fade: mono = only the second hit, a copy of the first
  8631. 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]);
  8632. CHECK (monoDiff < 1e-3 && polyDiff > 1.0);
  8633. // patch pan +64 → right only; channel volume counts twice (FL applies Final.Vol in the engine and at the host)
  8634. auto right = c;
  8635. right.modelParams[38 * 12 + 1] = 64.0f;
  8636. const auto [pl, pr, pi] = render (right, { 60, 0 }, 4096);
  8637. CHECK (energy (pl, 0, 4096) < 1e-12 * energy (pr, 0, 4096) && energy (pr, 0, 4096) > 1.0);
  8638. const auto full = std::get<0> (render (tone, { 60, 0 }, 4096));
  8639. const auto half = std::get<0> (render (tone, { 60, 0 }, 4096, 0.5f, 0.5f));
  8640. CHECK (std::abs (half[100] - 0.25f * full[100]) < 1e-6f && std::abs (full[100]) > 0.01f);
  8641. }
  8642. TEST (kepler_lfo_bpm_sync)
  8643. {
  8644. // 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)
  8645. std::vector<std::uint8_t> state (0x21 + 34 * 4, 0);
  8646. auto putU32 = [&] (std::size_t o, std::uint32_t v) { std::memcpy (state.data() + o, &v, 4); };
  8647. putU32 (12, 9);
  8648. std::memcpy (state.data() + 16, "Kepler-1", 9);
  8649. putU32 (0x19, 4);
  8650. putU32 (0x1D, 34);
  8651. float k[34] {};
  8652. k[5] = 0.602085769f; // "Pads - Kepler" demo: n = 286
  8653. k[6] = 1.0f;
  8654. k[10] = 1.0f; // vibrato on
  8655. k[33] = 1.0f;
  8656. std::memcpy (state.data() + 0x21, k, sizeof k);
  8657. lynx::SynthSettings s;
  8658. float gain = 0.0f;
  8659. CHECK (lynx::synthFromKepler (state, s, gain));
  8660. CHECK (s.lfo[lynx::lfoPitch].sync && s.lfo[lynx::lfoPitch].secondsTiming);
  8661. CHECK (std::abs (s.lfo[lynx::lfoPitch].period - 286.0f / 192.0f) < 1e-6f);
  8662. k[5] = 0.0f; // slowest: 32 beats
  8663. std::memcpy (state.data() + 0x21, k, sizeof k);
  8664. CHECK (lynx::synthFromKepler (state, s, gain) && std::abs (s.lfo[lynx::lfoPitch].period - 32.0f) < 1e-6f);
  8665. k[6] = 0.0f; // free rate in seconds
  8666. std::memcpy (state.data() + 0x21, k, sizeof k);
  8667. CHECK (lynx::synthFromKepler (state, s, gain) && ! s.lfo[lynx::lfoPitch].sync);
  8668. }
  8669. namespace
  8670. {
  8671. // A Harmor state written from scratch (REA_rodada11 §4.1 layout, version 18): part A only — a decaying-harmonics
  8672. // timbre, low-pass + resonance, filter envelope, 3-voice unison, vibrato, pluck, tremolo, volume envelope with a
  8673. // velocity map — and the global block. Every other curve is neutral.
  8674. // features (REA_rodada75): 1 arpeggio markers on the volume envelope, 2 noise resonance on filter 1, 4 "pluck replaces
  8675. // blur decay", 8 pluck blend, 16 harmonic randomness, 32 phaser + harmonizer masks
  8676. std::vector<std::uint8_t> harmorTestState (bool panLfo = true, bool image = false, int features = 0, const std::string& resynthPath = {})
  8677. {
  8678. std::vector<std::uint8_t> b;
  8679. 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))); };
  8680. 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))); };
  8681. struct Pt { double dx, y, tension; int mode; int arp = 0; };
  8682. auto curve = [&] (const std::vector<Pt>& pts, std::array<std::int32_t, 5> hdr) {
  8683. i32 (3), i32 (3), i32 ((std::int32_t) pts.size());
  8684. for (const auto& p : pts)
  8685. {
  8686. f64 (p.dx), f64 (p.y);
  8687. const auto t = (float) p.tension;
  8688. std::uint32_t u;
  8689. std::memcpy (&u, &t, 4);
  8690. i32 ((std::int32_t) u);
  8691. b.push_back ((std::uint8_t) p.mode), b.push_back ((std::uint8_t) p.arp), b.push_back (0), b.push_back (0);
  8692. }
  8693. for (auto h : hdr)
  8694. i32 (h);
  8695. };
  8696. const std::array<std::int32_t, 5> off { 0, 0, -1, -1, -1 }, on { 0, 1, -1, -1, -1 };
  8697. 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); };
  8698. i32 (18);
  8699. i32 (1 | 4);
  8700. std::vector<std::uint8_t> part;
  8701. std::swap (b, part);
  8702. std::array<std::int32_t, 386> p {};
  8703. const std::pair<int, int> values[] = { { 0, 500 }, { 3, 1000 }, { 9, 1024 }, { 11, 1000 }, { 12, 1000 }, { 17, 300 }, { 18, 4000 },
  8704. { 19, 500 }, { 20, 1000 }, { 24, 500 }, { 25, 500 }, { 28, 500 }, { 29, 256 }, { 30, -1 }, { 31, -1 }, { 33, -1 }, { 34, 3 },
  8705. { 35, 1 }, { 37, 192 }, { 38, 128 }, { 39, 500 }, { 40, 10000 }, { 41, 1 }, { 42, 10000 }, { 43, 1 }, { 45, 200 }, { 46, 500 },
  8706. { 47, 1 }, { 49, -140 }, { 50, 1200 }, { 51, 0 }, { 52, 5000 }, { 54, 500 }, { 56, 600 }, { 57, 300 }, { 59, 500 }, { 60, 600 },
  8707. { 64, 8192 }, { 66, 500 }, { 72, 600 }, { 74, 1000 }, { 75, 600 }, { 78, 5000 }, { 79, 500 }, { 80, 3000 } };
  8708. for (const auto& [i, v] : values)
  8709. p[(std::size_t) i] = v;
  8710. if ((features & 2) != 0)
  8711. p[57] = 400, p[59] = 300; // filter-1 resonance amount and width (noise type set in the combo block)
  8712. if ((features & 4) != 0)
  8713. p[75] = 700, p[21] = 300; // pluck value, time-blur attack
  8714. if ((features & 8) != 0)
  8715. p[75] = 650, p[4] = 600; // pluck, timbre mix
  8716. if ((features & 32) != 0)
  8717. p[76] = 700, p[27] = 600; // phaser mix, harmonizer mix
  8718. if (image) // image synthesis (REA_rodada75): speed, cubic gain / linear freq interpolation, Hz freq mode, formant
  8719. for (const auto& [i, v] : std::initializer_list<std::pair<int, int>> { { 82, 200 }, { 83, 100000 }, { 84, 10000 }, { 85, 20000 },
  8720. { 86, 750 }, { 87, 4 }, { 88, 600 }, { 89, 900 }, { 90, 1 }, { 91, 3 }, { 92, 1500 }, { 93, -300 }, { 94, 800 } })
  8721. p[(std::size_t) i] = v;
  8722. if (! resynthPath.empty()) // resynthesis: speed 1, linear interpolation, Hz frequency mode, sharpening 750
  8723. for (const auto& [i, v] : std::initializer_list<std::pair<int, int>> { { 82, 500 }, { 84, 10000 }, { 86, 750 }, { 87, 3 },
  8724. { 88, 500 }, { 89, 1000 }, { 90, 1 }, { 91, 3 }, { 92, 1000 } })
  8725. p[(std::size_t) i] = v;
  8726. for (auto v : p)
  8727. i32 (v);
  8728. for (int i = 0; i < 12; ++i)
  8729. i32 (i); // module order
  8730. std::array<std::uint8_t, 0x4b> misc {};
  8731. auto put = [&] (int o, std::int32_t v) { std::memcpy (misc.data() + o, &v, 4); };
  8732. const float one = 1.0f;
  8733. put (0x08, 107), put (0x1c, 1), put (0x28, 200), std::memcpy (misc.data() + 0x30, &one, 4);
  8734. put (0x34, 0), put (0x38, 4), put (0x3c, (features & 2) != 0 ? 109 : 100), put (0x40, 108);
  8735. if ((features & 8) != 0)
  8736. put (0x00, 5);
  8737. misc[0x44] = 1;
  8738. if (! resynthPath.empty())
  8739. put (0x24, 250); // resynthesis soft knee (part +0x244)
  8740. if ((features & 4) != 0)
  8741. misc[0x46] = 1;
  8742. b.insert (b.end(), misc.begin(), misc.end());
  8743. 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 };
  8744. for (int k = 0; k < 43; ++k)
  8745. {
  8746. static const int mode2[] = { 2, 14, 23, 28, 30, 38, 39, 41, 42 }; // ×2 around 0.5: 1.0 is neutral
  8747. const bool centred = std::find (std::begin (mode1), std::end (mode1), k) != std::end (mode1);
  8748. const bool doubled = std::find (std::begin (mode2), std::end (mode2), k) != std::end (mode2);
  8749. const double neutral = centred && ! doubled ? 0.5 : 1.0;
  8750. i32 (3);
  8751. i32 (1065353216), i32 (1065353216), i32 (1065353216);
  8752. if (k == 1 && (features & 1) != 0) // arpeggio volume envelope: a looping pattern with chord markers
  8753. 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 },
  8754. { 0.1, 0, 0.2, 0 } },
  8755. { 0, 1, -1, 0, 4 });
  8756. else if (k == 1) // volume envelope: attack, decay to the sustain point, release
  8757. 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 });
  8758. else if (k == 4) // filter-1 envelope
  8759. curve ({ { 0, 1, 0, 0 }, { 0.25, 0.2, -0.5, 0 } }, { 0, 1, -1, -1, -1 });
  8760. else
  8761. flat (neutral);
  8762. for (int a : { 128, 128, 0, 128 })
  8763. i32 (a);
  8764. if (k == 0 && panLfo) // pan LFO
  8765. flat (1.0, on);
  8766. else
  8767. flat (0.5);
  8768. for (int a : { k == 0 ? 40000 : 0, 0, 0, 0 })
  8769. i32 (a);
  8770. i32 (0);
  8771. for (int m = 2; m < 11; ++m)
  8772. if (k == 1 && m == 3)
  8773. curve ({ { 0, 0.2, 0, 0 }, { 1, 1, -0.3, 0 } }, off); // velocity → volume
  8774. else
  8775. flat (neutral);
  8776. }
  8777. for (int c = 0; c < 26; ++c)
  8778. if ((features & 16) != 0 && c == 4)
  8779. curve ({ { 0, 0.5, 0, 0 }, { 0.2, 0.8, 0.3, 0 }, { 0.8, 0.3, 0, 0 } }, off); // harmonic randomness
  8780. else if ((features & 32) != 0 && (c == 22 || c == 23))
  8781. curve ({ { 0, 0.3, 0, 0 }, { 0.6, 1.0, 0.2, 0 }, { 0.4, 0.5, 0, 0 } }, off); // phaser / harmonizer masks
  8782. else if (c == 3 && (features & 8) != 0)
  8783. curve ({ { 0, 0.2, 0, 0 }, { 1, 0.05, 0.4, 0 } }, off); // timbre 2 for the pluck blend
  8784. else if (c == 2 || c == 3)
  8785. curve ({ { 0, 0.25, 0, 0 }, { 0.006, 0.12, -0.46, 0 }, { 0.994, 0, 0, 0 } }, off); // timbres
  8786. else if (c == 14)
  8787. curve ({ { 0, 1, 0, 0 }, { 1, 0, 0, 0 } }, off); // pluck curve
  8788. else
  8789. 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)));
  8790. if (image)
  8791. {
  8792. // the image object (FUN_00793300 layout): version 0, image mode, looping, 6 columns × 40 rows, gain + freq planes
  8793. const int cols = 6, rows = 40;
  8794. i32 (0), i32 (0), i32 (1), i32 (0);
  8795. f64 (-1.0), f64 (-1.0), f64 (0.0);
  8796. i32 (cols), i32 (rows);
  8797. i32 (cols * rows);
  8798. for (int c2 = 0; c2 < cols; ++c2)
  8799. for (int r2 = 0; r2 < rows; ++r2)
  8800. {
  8801. const auto v = (float) (255.0 * std::pow (0.93, r2) * (0.6 + 0.4 * std::cos (0.9 * c2 + 0.37 * r2)));
  8802. std::uint32_t u;
  8803. std::memcpy (&u, &v, 4);
  8804. i32 ((std::int32_t) u);
  8805. }
  8806. i32 (cols * rows);
  8807. for (int c2 = 0; c2 < cols; ++c2)
  8808. for (int r2 = 0; r2 < rows; ++r2)
  8809. {
  8810. const auto v = (float) (0.3 * std::sin (1.3 * c2 + 0.5 * r2));
  8811. std::uint32_t u;
  8812. std::memcpy (&u, &v, 4);
  8813. i32 ((std::int32_t) u);
  8814. }
  8815. i32 (0);
  8816. b.push_back (0); // (file name: empty)
  8817. }
  8818. if (! image && ! resynthPath.empty())
  8819. {
  8820. // a resynthesis image object (round natives190kj): mode 1, ping-pong, no planes, then the sample's path
  8821. i32 (0), i32 (1), i32 (2), i32 (0);
  8822. f64 (-1.0), f64 (-1.0), f64 (0.0);
  8823. i32 (0), i32 (0);
  8824. b.push_back ((std::uint8_t) resynthPath.size());
  8825. b.insert (b.end(), resynthPath.begin(), resynthPath.end());
  8826. }
  8827. std::swap (b, part);
  8828. i32 ((std::int32_t) part.size());
  8829. b.insert (b.end(), part.begin(), part.end());
  8830. std::array<std::int32_t, 53> g {};
  8831. g[0] = g[1] = g[2] = 256, g[10] = 500, g[11] = 1000, g[12] = 1000, g[15] = 1000;
  8832. for (auto v : g)
  8833. i32 (v);
  8834. for (int i = 0; i < 6; ++i)
  8835. i32 (i);
  8836. std::array<std::uint8_t, 0x3d> gm {};
  8837. const std::int32_t cr = 200, vr = 1;
  8838. std::memcpy (gm.data() + 12, &vr, 4);
  8839. std::memcpy (gm.data() + 24, &cr, 4);
  8840. b.insert (b.end(), gm.begin(), gm.end());
  8841. return b;
  8842. }
  8843. } // namespace
  8844. TEST (harmor_engine)
  8845. {
  8846. // REA_rodada34: state parsing, the additive engine against our reference model (hmodel.py), host levels, voice end
  8847. const auto blob = harmorTestState();
  8848. if (const char* dump = std::getenv ("LYNX_DUMP_HARMOR"))
  8849. std::ofstream (dump, std::ios::binary).write ((const char*) blob.data(), (std::streamsize) blob.size());
  8850. CHECK (lynx::harmorStateUsable (blob));
  8851. CHECK (lynx::harmorFallbacks (blob).empty());
  8852. const auto at = lynx::harmorParamOffset (blob, 0, 52);
  8853. std::int32_t cutoff = 0;
  8854. CHECK (at > 0 && (std::memcpy (&cutoff, blob.data() + at, 4), cutoff == 5000));
  8855. CHECK (lynx::harmorParamOffset (blob, 1, 52) < 0 && lynx::harmorParamOffset (blob, -1, 12) > at);
  8856. auto broken = blob;
  8857. broken.resize (broken.size() / 2);
  8858. CHECK (! lynx::harmorStateUsable (broken));
  8859. auto old = blob;
  8860. old[0] = 15; // articulators and curves are stored from version 16
  8861. CHECK (! lynx::harmorStateUsable (old));
  8862. lynx::ChannelDef c;
  8863. c.instrument = lynx::InstrumentKind::harmor;
  8864. c.pluginState = blob;
  8865. auto render = [] (const lynx::ChannelDef& def, int total, float gl, float gr, int offAt) {
  8866. auto inst = lynx::makeInstrument (lynx::InstrumentKind::harmor);
  8867. inst->configure (def, 120.0);
  8868. inst->prepare (44100.0);
  8869. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  8870. inst->noteOn (60, 0.78125f);
  8871. for (int s = 0; s < total; s += 512)
  8872. {
  8873. if (s == offAt)
  8874. inst->noteOff (60);
  8875. inst->render (l.data() + s, r.data() + s, std::min (512, total - s), gl, gr);
  8876. }
  8877. return std::make_tuple (l, r, std::move (inst));
  8878. };
  8879. {
  8880. // reference model (REA round 34 hmodel.py; C5 at FL velocity 100, Final.Vol 1, centre, released at 6144, the
  8881. // Mersenne Twister seeded like LYNX's; since flbeta165h LYNX's own values: the test state plays at precision
  8882. // "Average", whose IPP 11-bit Exp/Ln — REA_rodada165g-flbeta165h §3 — the model computes exactly)
  8883. auto [l, r, inst] = render (c, 8192, 1.0f, 1.0f, 6144);
  8884. if (std::getenv ("LYNX_PRINT_HARMOR_ENGINE") != nullptr)
  8885. for (int i : { 0, 100, 1000, 3000, 6000, 7000, 8191 })
  8886. std::printf ("harmor engine %d %.8g %.8g\n", i, (double) l[(std::size_t) i], (double) r[(std::size_t) i]);
  8887. const std::tuple<int, float, float> golden[] {
  8888. { 0, 0.0f, 0.0f }, { 100, 0.0094771842f, 0.042498328f }, { 1000, 0.081412978f, -0.032517102f }, { 3000, 0.059289735f, -0.0024759346f },
  8889. { 6000, 0.029978603f, 0.14549156f }, { 7000, -0.011218854f, 0.041979425f }, { 8191, 0.0031455588f, 0.03857585f } };
  8890. for (const auto& [i, gl, gr] : golden)
  8891. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  8892. // after the release the volume envelope ends, the record declicks and the instrument goes idle
  8893. std::vector<float> tail (44100), tailR (tail.size());
  8894. inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
  8895. CHECK (inst->idle());
  8896. CHECK (std::abs (tail.back()) == 0.0f);
  8897. }
  8898. // the reference renderer gives the same samples as the instrument
  8899. {
  8900. const auto ref = lynx::harmor::renderReference (blob, { { 0, 60, 0.78125, 1.0, 0.0, 6144 } }, 44100.0, 8192);
  8901. const auto [l, r, inst] = render (c, 8192, 1.0f, 1.0f, 6144);
  8902. double worst = 0.0;
  8903. for (int i = 0; i < 8192; ++i)
  8904. 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]) });
  8905. CHECK (worst == 0.0);
  8906. }
  8907. // FL's level law applied once: half the channel volume halves the output; full right pan silences the left side
  8908. const auto full = std::get<0> (render (c, 2048, 1.0f, 1.0f, -1));
  8909. const auto half = std::get<0> (render (c, 2048, 0.5f, 0.5f, -1));
  8910. CHECK (std::abs (half[1000] - 0.5f * full[1000]) < 1e-6f && std::abs (full[1000]) > 0.01f);
  8911. auto mono = c;
  8912. mono.pluginState = harmorTestState (false); // no pan LFO ...
  8913. const std::int32_t one = 1;
  8914. std::memcpy (mono.pluginState.data() + lynx::harmorParamOffset (mono.pluginState, 0, 34), &one, 4); // ... nor unison spread
  8915. const auto [pl, pr, pi] = render (mono, 2048, 0.0f, 1.41421356f, -1);
  8916. double el = 0.0, er = 0.0;
  8917. for (int i = 0; i < 2048; ++i)
  8918. el += (double) pl[(std::size_t) i] * pl[(std::size_t) i], er += (double) pr[(std::size_t) i] * pr[(std::size_t) i];
  8919. CHECK (el < 1e-10 * er && er > 1e-3);
  8920. }
  8921. namespace
  8922. {
  8923. // An Ogun state written from scratch (REA_rodada37 §1.3 layout, version 18; zlib with stored blocks): richness 4
  8924. // (255 partials), pre-decay, decay/release, fullness, Seed 1 + fixed Seed 2 with morph, 3-voice unison, a 2-stage
  8925. // low-pass with an LFO on its cutoff, a volume envelope with a sustain point and a velocity map, declick, normalisation,
  8926. // Impulse/Decay maps, and every plugin effect (EQ band, chorus, ping-pong delay, reverb). Everything else is neutral.
  8927. std::vector<std::uint8_t> ogunTestState()
  8928. {
  8929. std::vector<std::uint8_t> b;
  8930. 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))); };
  8931. auto f32v = [&] (float v) { std::uint32_t u; std::memcpy (&u, &v, 4); i32 ((std::int32_t) u); };
  8932. 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))); };
  8933. struct Pt { double dx, y, tension; int mode; };
  8934. auto curve = [&] (int type, const std::vector<Pt>& pts, std::array<std::int32_t, 5> hdr) {
  8935. i32 (type), i32 (3), i32 ((std::int32_t) pts.size());
  8936. for (const auto& p : pts)
  8937. {
  8938. f64 (p.dx), f64 (p.y), f32v ((float) p.tension);
  8939. b.push_back ((std::uint8_t) p.mode), b.push_back (0), b.push_back (0), b.push_back (0);
  8940. }
  8941. for (auto h : hdr)
  8942. i32 (h);
  8943. };
  8944. const std::array<std::int32_t, 5> off { 0, 0, -1, -1, -1 }, on { 0, 1, -1, -1, -1 };
  8945. auto flat = [&] (int type, double y) { curve (type, { { 0, y, 0, 0 }, { 1, y, 0, 0 } }, off); };
  8946. std::array<std::int32_t, 64> P {};
  8947. const std::pair<int, int> values[] = { { 0, 128 }, { 5, 300 }, { 6, 3000 }, { 7, 2000 }, { 8, 200 }, { 9, 500 }, { 10, 12 },
  8948. { 11, 34 }, { 12, 3 }, { 13, 64 }, { 14, 64 }, { 15, 40 }, { 16, 64 }, { 17, 180 }, { 18, 60 }, { 19, 2 }, { 20, 300 },
  8949. { 21, 400 }, { 22, 40 }, { 23, 30 }, { 24, 20 }, { 25, 64 }, { 26, 40 }, { 27, 96 }, { 28, 100 }, { 29, 64 }, { 30, 2 },
  8950. { 31, 19 }, { 32, 221 }, { 33, 0 }, { 34, 50 }, { 35, 100 }, { 36, 20 }, { 37, 60 }, { 38, 2 }, { 39, 64 }, { 40, 300 },
  8951. { 43, 30000 }, { 46, 20000 }, { 49, 0 }, { 50, 2 }, { 51, 0 }, { 52, 9 }, { 53, 1 }, { 54, 0 },
  8952. { 55, 0x01000000 }, // +0x20b: pitch follows the key
  8953. { 56, 0x00010000 }, // +0x20e: declick
  8954. { 62, 0x00000100 } }; // +0x225: normalise
  8955. for (const auto& [i, v] : values)
  8956. P[(std::size_t) i] = v;
  8957. for (auto v : P)
  8958. i32 (v);
  8959. b.push_back (0), b.push_back (1), b.push_back (0); // delay: not tempo-based, on
  8960. i32 (1), i32 (1), i32 (1); // EQ band types: peaking
  8961. b.push_back (0); // comment: 0 characters
  8962. for (int k = 0; k < 13; ++k)
  8963. {
  8964. static constexpr int sum[] = { 1, 2, 3, 9, 11, 12 };
  8965. const double neutral = std::find (std::begin (sum), std::end (sum), k) != std::end (sum) ? 0.5 : 1.0;
  8966. i32 (1);
  8967. f32v (0.0f), f32v (1.0f), f32v (k == 1 ? 0.3f : 0.0f);
  8968. if (k == 0) // volume envelope: attack, decay to the sustain point, release
  8969. 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 });
  8970. else
  8971. flat (1, neutral);
  8972. for (int a : { 128, 128, 128, 128 })
  8973. i32 (a);
  8974. if (k == 1) // cutoff LFO
  8975. curve (2, { { 0, 0.8, 0, 0 } }, on);
  8976. else
  8977. flat (2, 0.5);
  8978. for (int a : { k == 1 ? 40000 : 0, 0, 0, 0 })
  8979. i32 (a);
  8980. i32 (0);
  8981. for (int m = 0; m < 4; ++m)
  8982. if (k == 0 && m == 1)
  8983. curve (3, { { 0, 0.3, 0, 0 }, { 1, 1, -0.2, 0 } }, off); // velocity → volume
  8984. else
  8985. flat (3, neutral);
  8986. }
  8987. flat (3, 0.0); // Forced: none
  8988. curve (3, { { 0, 0.6, 0, 0 }, { 1, 0.4, 0.3, 0 } }, off); // Impulse EQ
  8989. curve (3, { { 0, 0.5, 0, 0 }, { 1, 0.3, 0, 0 } }, off); // Decay EQ
  8990. flat (3, 0.5); // Release EQ
  8991. // u32 version + zlib (stored blocks)
  8992. std::vector<std::uint8_t> s { 18, 0, 0, 0, 0x78, 0x01 };
  8993. for (std::size_t i = 0; i < b.size();)
  8994. {
  8995. const auto len = std::min<std::size_t> (65535, b.size() - i);
  8996. s.push_back (i + len >= b.size() ? 1 : 0);
  8997. s.push_back ((std::uint8_t) len), s.push_back ((std::uint8_t) (len >> 8));
  8998. s.push_back ((std::uint8_t) ~len), s.push_back ((std::uint8_t) (~len >> 8));
  8999. s.insert (s.end(), b.begin() + (std::ptrdiff_t) i, b.begin() + (std::ptrdiff_t) (i + len));
  9000. i += len;
  9001. }
  9002. std::uint32_t a1 = 1, a2 = 0;
  9003. for (auto c : b)
  9004. a1 = (a1 + c) % 65521u, a2 = (a2 + a1) % 65521u;
  9005. for (int sh = 24; sh >= 0; sh -= 8)
  9006. s.push_back ((std::uint8_t) (((a2 << 16) | a1) >> sh));
  9007. return s;
  9008. }
  9009. } // namespace
  9010. TEST (ogun_engine)
  9011. {
  9012. // REA_rodada37: state parsing, the engine against our reference model (ogmodel.py), host levels, export quality
  9013. const auto blob = ogunTestState();
  9014. if (const char* dump = std::getenv ("LYNX_DUMP_OGUN"))
  9015. std::ofstream (dump, std::ios::binary).write ((const char*) blob.data(), (std::streamsize) blob.size());
  9016. CHECK (lynx::ogunStateUsable (blob));
  9017. CHECK (lynx::ogunFallbacks (blob).empty());
  9018. CHECK (lynx::ogunParam (blob, 17) == 180 && lynx::ogunParam (blob, 52) == 9 && ! lynx::ogunParam (blob, lynx::ogunDelayOnIndex + 1));
  9019. auto broken = blob;
  9020. broken.resize (broken.size() - 300);
  9021. CHECK (! lynx::ogunStateUsable (broken));
  9022. auto old = blob;
  9023. old[0] = 13; // a version-18 body read as version 13 (a u32 comment count; versions 10…13: Gen169lTests)
  9024. CHECK (! lynx::ogunStateUsable (old));
  9025. // reference model (REA round 37 ogmodel.Plugin(state).run: C5 velocity 0.75 released at 6144, G5 velocity 0.625 at 2048
  9026. // released at 8192; host block 512)
  9027. 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 } };
  9028. const auto ref = lynx::ogun::renderReference (blob, notes, 44100.0, 16384);
  9029. CHECK (ref.size() == 2 * 16384);
  9030. if (const char* dump = std::getenv ("LYNX_DUMP_OGUN_OUT"))
  9031. std::ofstream (dump, std::ios::binary).write ((const char*) ref.data(), (std::streamsize) (ref.size() * 4));
  9032. const std::tuple<int, float, float> golden[] {
  9033. { 100, -7.763202e-05f, -8.919523e-05f }, { 1000, 0.06195267f, 0.03858230f }, { 3000, 0.01363476f, -0.00378433f },
  9034. { 6000, -0.06601223f, -0.03084030f }, { 9000, 0.04204506f, 0.02452896f }, { 12000, 0.03420147f, -0.01837870f },
  9035. { 16383, -0.00465311f, -0.00119627f } };
  9036. for (const auto& [i, gl, gr] : golden)
  9037. CHECK (std::abs (ref[2 * (std::size_t) i] - gl) < 2e-6f && std::abs (ref[2 * (std::size_t) i + 1] - gr) < 2e-6f);
  9038. // the instrument plays like the reference renderer (Final.Vol 1, centre)
  9039. lynx::ChannelDef c;
  9040. c.instrument = lynx::InstrumentKind::ogun;
  9041. c.pluginState = blob;
  9042. auto render = [&] (const lynx::ChannelDef& def, int total, float gl, float gr, bool held = false) {
  9043. auto inst = lynx::makeInstrument (lynx::InstrumentKind::ogun);
  9044. inst->configure (def, 120.0);
  9045. inst->prepare (44100.0);
  9046. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  9047. for (int s = 0; s < total; s += 512)
  9048. {
  9049. if (s == 0)
  9050. inst->noteOn (60, 0.75f);
  9051. if (s == 2048)
  9052. inst->noteOn (67, 0.625f);
  9053. if (s == 6144 && ! held)
  9054. inst->noteOff (60);
  9055. if (s == 8192 && ! held)
  9056. inst->noteOff (67);
  9057. inst->render (l.data() + s, r.data() + s, std::min (512, total - s), gl, gr);
  9058. }
  9059. return std::make_tuple (l, r, std::move (inst));
  9060. };
  9061. {
  9062. auto [l, r, inst] = render (c, 16384, 1.0f, 1.0f);
  9063. double worst = 0.0;
  9064. for (int i = 0; i < 16384; ++i)
  9065. 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]) });
  9066. CHECK (worst == 0.0);
  9067. // the voices end and the effect tails die out: the instrument goes idle
  9068. std::vector<float> tail (44100), tailR (tail.size());
  9069. for (int k = 0; k < 20 && ! inst->idle(); ++k)
  9070. inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
  9071. CHECK (inst->idle());
  9072. }
  9073. // FL's level law applied once (effects off): half the channel volume halves the output once the gain ramps
  9074. // (limited to 0.001 per sample) have settled on the sustain level; full right pan silences the left side
  9075. {
  9076. auto dry = c;
  9077. for (int i : { 19, 29, 40, 53 })
  9078. CHECK (lynx::ogunSetParam (dry.pluginState, i, 0)); // chorus, delay wet, EQ band and reverb off
  9079. CHECK (lynx::ogunStateUsable (dry.pluginState) && lynx::ogunParam (dry.pluginState, 19) == 0);
  9080. const auto full = std::get<0> (render (dry, 32768, 1.0f, 1.0f, true));
  9081. const auto half = std::get<0> (render (dry, 32768, 0.5f, 0.5f, true));
  9082. double worst = 0.0, peak = 0.0;
  9083. for (int i = 30000; i < 32768; ++i)
  9084. worst = std::max (worst, (double) std::abs (half[(std::size_t) i] - 0.5f * full[(std::size_t) i])),
  9085. peak = std::max (peak, (double) std::abs (full[(std::size_t) i]));
  9086. CHECK (worst <= 1e-7 * peak && peak > 1e-3);
  9087. // FL's default channel volume (100/128): FL hands Ogun Final.Vol = (11^v − 1)/10 (REA_rodada28 §4.1); LYNX's
  9088. // centred channel gains (√2·V·cos(π/4) each) give the reference model's level at that Final.Vol
  9089. const auto V = lynx::channelGain (0.78125f);
  9090. const auto g = V * std::cos (0.25f * 3.14159265f) * 1.41421356f;
  9091. const auto atDefault = std::get<0> (render (dry, 32768, g, g, true));
  9092. const auto refDefault = lynx::ogun::renderReference (dry.pluginState, { { 0, 60, 0.75, V, 0.0, 0.0, 0.0, 0.0, 0.5, -1 },
  9093. { 2048, 67, 0.625, V, 0.0, 0.0, 0.0, 0.0, 0.5, -1 } }, 44100.0, 32768);
  9094. double ei = 0.0, er0 = 0.0;
  9095. for (int i = 30000; i < 32768; ++i)
  9096. 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];
  9097. CHECK (er0 > 0.0 && std::abs (10.0 * std::log10 (ei / er0)) < 0.001);
  9098. const auto [pl, pr, pi] = render (dry, 4096, 0.0f, 1.41421356f);
  9099. double el = 0.0, er = 0.0;
  9100. for (int i = 0; i < 4096; ++i)
  9101. el += (double) pl[(std::size_t) i] * pl[(std::size_t) i], er += (double) pr[(std::size_t) i] * pr[(std::size_t) i];
  9102. CHECK (el < 1e-10 * er && er > 1e-3);
  9103. }
  9104. // export: FL renders Ogun at quality 4 (×2 rate, 32-point reader), LYNX too while it renders offline
  9105. {
  9106. lynx::setOfflineRendering (true);
  9107. auto [l, r, inst] = render (c, 8192, 1.0f, 1.0f);
  9108. lynx::setOfflineRendering (false);
  9109. lynx::ogun::RefOptions q4;
  9110. q4.quality = 4;
  9111. const auto ref4 = lynx::ogun::renderReference (blob, notes, 44100.0, 8192, q4);
  9112. double worst = 0.0, diff = 0.0;
  9113. for (int i = 0; i < 8192; ++i)
  9114. worst = std::max (worst, (double) std::abs (ref4[2 * (std::size_t) i] - l[(std::size_t) i])),
  9115. diff = std::max (diff, (double) std::abs (ref[2 * (std::size_t) i] - l[(std::size_t) i]));
  9116. CHECK (worst == 0.0 && diff > 1e-3);
  9117. const std::tuple<int, float, float> golden4[] {
  9118. { 100, 0.00605961f, 0.00628969f }, { 1000, 0.03729841f, 0.01727954f }, { 3000, -0.09010383f, -0.02551294f },
  9119. { 6000, 0.02167798f, -0.01115632f }, { 8191, 0.00320288f, -0.00615792f } };
  9120. for (const auto& [i, gl, gr] : golden4)
  9121. CHECK (std::abs (ref4[2 * (std::size_t) i] - gl) < 2e-6f && std::abs (ref4[2 * (std::size_t) i + 1] - gr) < 2e-6f);
  9122. }
  9123. // feature variants of the same state against the reference model (energy per side over 8192 samples, three
  9124. // samples; C1 released at 6144): qualities, richness, unison, the filter types, ALT DECAY, 1/√h, Seed 2 Random,
  9125. // infinite decay, logarithmic axis, velocity → position, other rates. Goldens from ogmodel.py (REA round 37).
  9126. {
  9127. struct Sample { int at; float l, r; };
  9128. struct Variant { const char* name; std::vector<std::pair<int, int>> params; int rate; double el, er; Sample s[3]; };
  9129. const Variant variants[] {
  9130. { "q1", { { 54, 1 } }, 44100, 11.0970402, 6.87820714, { { 1500, 0.0240363069f, -0.0148524037f }, { 4000, 0.0873190612f, -0.000715861097f }, { 7000, -0.0378603041f, -0.00338819111f } } },
  9131. { "q2", { { 54, 2 } }, 44100, 11.2405446, 6.68045082, { { 1500, 0.0448660068f, -0.00560745923f }, { 4000, -0.0122657465f, 0.0104285963f }, { 7000, -0.0216034129f, -0.0134632047f } } },
  9132. { "q3", { { 54, 3 } }, 44100, 11.3200608, 6.86196449, { { 1500, -0.0031537537f, -0.0242648814f }, { 4000, 0.0426301025f, 0.0364697985f }, { 7000, 0.00843803957f, 0.0302239582f } } },
  9133. { "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 } } },
  9134. { "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 } } },
  9135. { "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 } } },
  9136. { "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 } } },
  9137. { "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 } } },
  9138. { "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 } } },
  9139. { "alt", { { 62, 257 } }, 44100, 17.2485711, 9.99223979, { { 1500, -0.0319156498f, -0.0186850764f }, { 4000, -0.0769012421f, -0.0375530645f }, { 7000, 0.0310976505f, 0.0445244014f } } },
  9140. { "isqrt", { { 62, 65792 } }, 44100, 45.7309307, 27.1224593, { { 1500, -0.04376591f, -0.00845711026f }, { 4000, 0.0351300463f, -0.129557073f }, { 7000, 0.0096148327f, 0.00123740174f } } },
  9141. { "seed2rnd", { { 11, 0 } }, 44100, 16.8228819, 11.5433061, { { 1500, -0.0117614279f, -0.026497297f }, { 4000, 0.00265733898f, -0.07069581f }, { 7000, 0.00576663949f, 0.0565217957f } } },
  9142. { "decinf", { { 6, 10000 } }, 44100, 19.1879226, 11.0487311, { { 1500, -0.0324105881f, -0.0190765262f }, { 4000, -0.0809780285f, -0.0395730771f }, { 7000, 0.034197174f, 0.048585102f } } },
  9143. { "logx", { { 57, 16777216 } }, 44100, 12.3064326, 7.31519289, { { 1500, -0.0438990779f, 0.0096983267f }, { 4000, -0.0175748877f, 0.0313935578f }, { 7000, 0.0639200211f, 0.024419222f } } },
  9144. { "velpos", { { 56, 66304 } }, 44100, 4.9966925, 3.16540662, { { 1500, 0.0083851777f, -0.0165472236f }, { 4000, 0.0630451962f, 0.0156494901f }, { 7000, -0.0293043777f, -0.00182877108f } } },
  9145. { "fs48", { }, 48000, 10.8813888, 6.53592373, { { 1500, -0.0485184453f, -0.0319588967f }, { 4000, 0.0230421089f, -0.0286604818f }, { 7000, -0.0117630213f, -0.017459048f } } },
  9146. { "fs96", { }, 96000, 11.0189683, 6.39821883, { { 1500, -0.0267860368f, -0.0227564126f }, { 4000, 0.0238709711f, -0.0100849615f }, { 7000, -0.0034619756f, 0.0451687202f } } },
  9147. };
  9148. for (const auto& v : variants)
  9149. {
  9150. auto st = blob;
  9151. for (const auto& [i, x] : v.params)
  9152. CHECK (lynx::ogunSetParam (st, i, x));
  9153. const auto y = lynx::ogun::renderReference (st, { { 0, 60, 0.75, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 6144 },
  9154. { 2048, 67, 0.625, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1 } }, (double) v.rate, 8192);
  9155. double el = 0.0, er = 0.0, peak = 0.0;
  9156. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  9157. 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]) });
  9158. bool ok = y.size() == 2 * 8192 && std::abs (el - v.el) <= 1e-5 * v.el && std::abs (er - v.er) <= 1e-5 * v.er;
  9159. for (const auto& smp : v.s)
  9160. 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;
  9161. if (! ok)
  9162. std::printf (" Ogun variant %s differs from the model: energy %.9g %.9g (model %.9g %.9g)\n", v.name, el, er, v.el, v.er);
  9163. CHECK (ok);
  9164. }
  9165. }
  9166. }
  9167. // our own Maximus v7 state (layout REA_rodada8 §1.1): 7 globals, 4 × 13 band parameters, 8 envelopes (dx, y, tension, type)
  9168. static std::vector<std::uint8_t> maximusTestState (const std::array<int, 7>& g, const std::array<std::array<int, 13>, 4>& bands,
  9169. const std::array<std::vector<std::array<double, 4>>, 8>& envs, int oversampling)
  9170. {
  9171. std::vector<std::uint8_t> d;
  9172. auto put = [&] (const void* v, std::size_t n) { d.insert (d.end(), (const std::uint8_t*) v, (const std::uint8_t*) v + n); };
  9173. auto i32 = [&] (std::int32_t v) { put (&v, 4); };
  9174. i32 (7);
  9175. for (int copy = 0; copy < 2; ++copy)
  9176. {
  9177. for (auto v : g)
  9178. i32 (v);
  9179. for (const auto& b : bands)
  9180. for (auto v : b)
  9181. i32 (v);
  9182. }
  9183. d.resize (0x1fe, 0);
  9184. std::memcpy (d.data() + 0x1f4, &oversampling, 4);
  9185. for (const auto& e : envs)
  9186. {
  9187. i32 (3), i32 (2), i32 ((std::int32_t) e.size());
  9188. for (const auto& pt : e)
  9189. {
  9190. const double dx = pt[0], y = pt[1];
  9191. const float t = (float) pt[2];
  9192. put (&dx, 8), put (&y, 8), put (&t, 4);
  9193. const std::uint8_t flags[4] { (std::uint8_t) pt[3], 0, 0, 0 };
  9194. put (flags, 4);
  9195. }
  9196. i32 (0), i32 (-1), i32 (-1), i32 (-1), i32 (-1);
  9197. }
  9198. return d;
  9199. }
  9200. // A "Kepler-1" state written from scratch (REA_rodada46 §1.1 layout, version 2): the default panel values
  9201. std::vector<std::uint8_t> keplerTestState()
  9202. {
  9203. 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 };
  9204. std::vector<std::uint8_t> st;
  9205. auto u32 = [&] (std::uint32_t v) { for (int i = 0; i < 4; ++i) st.push_back ((std::uint8_t) (v >> (8 * i))); };
  9206. auto f32 = [&] (float f) { std::uint32_t v; std::memcpy (&v, &f, 4); u32 (v); };
  9207. u32 (0), u32 (0), u32 (0), u32 (9);
  9208. for (char c : std::string ("Kepler-1"))
  9209. st.push_back ((std::uint8_t) c);
  9210. st.push_back (0);
  9211. u32 (2), u32 (34);
  9212. for (auto f : panel)
  9213. f32 (f);
  9214. st.push_back (0);
  9215. f32 (1.0f), f32 (0.5f);
  9216. const auto size = (std::uint32_t) st.size() - 12;
  9217. std::memcpy (st.data() + 4, &size, 4);
  9218. return st;
  9219. }
  9220. std::string keplerDll()
  9221. {
  9222. if (realFlpDir.empty())
  9223. return {};
  9224. return (std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "Kepler" / "engine_x64.dll").string();
  9225. }
  9226. TEST (kepler_engine)
  9227. {
  9228. const auto st = keplerTestState();
  9229. CHECK (st.size() == 178 && lynx::keplerStateUsable (st));
  9230. CHECK (lynx::keplerPanelOffset (st, 19) == 0x21 + 4 * 19 && lynx::keplerPanelOffset (st, 34) == -1);
  9231. auto broken = st;
  9232. broken[17] = 'X'; // not "Kepler-1"
  9233. CHECK (! lynx::keplerStateUsable (broken) && lynx::keplerPanelOffset (broken, 0) == -1);
  9234. auto v3 = st;
  9235. v3[0x19] = 3; // version 3 needs the gate / noise / oversampling bytes
  9236. CHECK (! lynx::keplerStateUsable (v3));
  9237. const auto dll = keplerDll();
  9238. if (dll.empty() || ! lynx::keplerDllUsable (dll))
  9239. return; // no FL installation (the engine needs the user's own DLL tables)
  9240. // against FL's engine DLL (EXEC, REA_rodada46 §6): three notes, 44.1 kHz, host blocks of 512 — bit-exact
  9241. 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 } };
  9242. const auto y = lynx::kepler::renderReference (st, dll, notes, 44100.0, 39690);
  9243. CHECK (y.size() == 2 * 39690);
  9244. if (y.size() != 2 * 39690)
  9245. return;
  9246. double el = 0.0, er = 0.0;
  9247. for (std::size_t i = 0; i < y.size(); i += 2)
  9248. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  9249. std::printf (" Kepler default patch: energy %.9g %.9g (FL's DLL 250.641268)\n", el, er);
  9250. CHECK (std::abs (el - 250.641268) < 1e-6 && std::abs (er - 250.641268) < 1e-6);
  9251. const std::tuple<int, float, float> golden[] { { 5000, 0.0467273332f, 0.0467273332f }, { 12000, -0.119412214f, -0.119412214f },
  9252. { 25000, 0.0855825394f, 0.0855825394f }, { 35000, -3.34166579e-08f, -3.34166579e-08f } };
  9253. for (const auto& [i, l, r] : golden)
  9254. CHECK (y[2 * (std::size_t) i] == l && y[2 * (std::size_t) i + 1] == r);
  9255. // the instrument: FL's wrapper velocity, channel volume as an output gain, idle after the release
  9256. lynx::ChannelDef c;
  9257. c.instrument = lynx::InstrumentKind::kepler;
  9258. c.pluginState = st;
  9259. c.samplePath = dll;
  9260. auto inst = lynx::makeInstrument (lynx::InstrumentKind::kepler);
  9261. inst->configure (c, 120.0);
  9262. inst->prepare (44100.0);
  9263. std::vector<float> l (4096), r (4096);
  9264. inst->noteOn (60, 0.8f);
  9265. double e = 0.0;
  9266. bool finite = true;
  9267. for (int b = 0; b < 8; ++b)
  9268. {
  9269. std::fill (l.begin(), l.end(), 0.0f);
  9270. std::fill (r.begin(), r.end(), 0.0f);
  9271. inst->render (l.data(), r.data(), 4096, 0.5f, 0.5f);
  9272. for (int i = 0; i < 4096; ++i)
  9273. 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];
  9274. if (b == 3)
  9275. inst->noteOff (60);
  9276. }
  9277. CHECK (finite && e > 1.0);
  9278. for (int b = 0; b < 40; ++b)
  9279. inst->render (l.data(), r.data(), 4096, 0.5f, 0.5f);
  9280. CHECK (inst->idle());
  9281. }
  9282. // REA round 57 §2: Transistor Bass
  9283. // the plugin state (event 0xD5) of an FL preset file (.fst: the FLhd/FLdt event stream)
  9284. std::vector<std::uint8_t> tbassPresetState (const char* name)
  9285. {
  9286. if (realFlpDir.empty())
  9287. return {};
  9288. std::ifstream in (std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "Transistor Bass" / name, std::ios::binary);
  9289. const std::vector<std::uint8_t> b ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  9290. std::size_t i = 0;
  9291. while (i + 8 <= b.size() && std::memcmp (b.data() + i, "FLdt", 4) != 0)
  9292. ++i;
  9293. i += 8;
  9294. while (i < b.size())
  9295. {
  9296. const auto id = b[i++];
  9297. std::size_t n = id < 64 ? 1 : id < 128 ? 2 : id < 192 ? 4 : 0;
  9298. if (id >= 192)
  9299. for (int shift = 0; i < b.size(); shift += 7)
  9300. {
  9301. const auto c = b[i++];
  9302. n |= (std::size_t) (c & 0x7F) << shift;
  9303. if (c < 0x80)
  9304. break;
  9305. }
  9306. if (id == 0xD5 && i + n <= b.size())
  9307. return { b.begin() + (std::ptrdiff_t) i, b.begin() + (std::ptrdiff_t) (i + n) };
  9308. i += n;
  9309. }
  9310. return {};
  9311. }
  9312. // the same state re-saved as engine-stream version 5 with an engine selector (REA_rodada57 §2.2)
  9313. std::vector<std::uint8_t> tbassWithEngine (const std::vector<std::uint8_t>& s, std::int32_t engine)
  9314. {
  9315. std::vector<std::uint8_t> out (s.begin(), s.begin() + 4);
  9316. auto put = [&] (const void* p, std::size_t n) { out.insert (out.end(), (const std::uint8_t*) p, (const std::uint8_t*) p + n); };
  9317. for (std::size_t at = 4; at + 12 <= s.size();)
  9318. {
  9319. std::uint32_t id;
  9320. std::uint64_t n;
  9321. std::memcpy (&id, s.data() + at, 4);
  9322. std::memcpy (&n, s.data() + at + 4, 8);
  9323. std::vector<std::uint8_t> d (s.begin() + (std::ptrdiff_t) (at + 12), s.begin() + (std::ptrdiff_t) (at + 12 + n));
  9324. at += 12 + (std::size_t) n;
  9325. if (id == 2)
  9326. {
  9327. std::uint64_t m;
  9328. std::memcpy (&m, d.data() + 4, 8);
  9329. std::vector<std::uint8_t> e (d.begin() + 12, d.begin() + 12 + (std::ptrdiff_t) m);
  9330. std::int16_t v;
  9331. std::memcpy (&v, e.data(), 2);
  9332. if (v < 5)
  9333. {
  9334. v = 5;
  9335. std::memcpy (e.data(), &v, 2);
  9336. e.insert (e.end(), 4, 0);
  9337. }
  9338. std::memcpy (e.data() + e.size() - 4, &engine, 4);
  9339. m = e.size();
  9340. d.resize (12);
  9341. std::memcpy (d.data() + 4, &m, 8);
  9342. d.insert (d.end(), e.begin(), e.end());
  9343. }
  9344. const std::uint64_t size = d.size();
  9345. put (&id, 4);
  9346. put (&size, 8);
  9347. put (d.data(), d.size());
  9348. }
  9349. return out;
  9350. }
  9351. std::string tbassDll()
  9352. {
  9353. if (realFlpDir.empty())
  9354. return {};
  9355. return (std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "Transistor Bass" / "engine_x64.dll").string();
  9356. }
  9357. // Plucked! (REA_rodada65 §1): the C++ engine against FL's DLL (EXEC: same notes, the DLL's RandSeed set before the first
  9358. // note, the host killing voices when the plugin asks). LYNX renders these bit-identically to the DLL.
  9359. TEST (plucked_engine)
  9360. {
  9361. auto stateOf = [] (std::initializer_list<std::int32_t> k) {
  9362. std::vector<std::uint8_t> st (20);
  9363. std::memcpy (st.data(), std::data (k), 20);
  9364. return st;
  9365. };
  9366. CHECK (! lynx::pluckedStateUsable (std::vector<std::uint8_t> (19)));
  9367. CHECK (lynx::pluckedStateUsable (stateOf ({ 128, 64, 1, 0, 1 })));
  9368. CHECK (lynx::pluckedParams (stateOf ({ 7, 9, 0, 1, 0 })) == std::vector<int> ({ 7, 9, 0, 1, 0 }));
  9369. using N = lynx::plucked::RefNote;
  9370. struct Sample { int at; float l, r; };
  9371. struct Golden { int set; std::int32_t P[5]; double fs; int block, law, maxPoly; double el, er; Sample s[4]; };
  9372. const std::vector<std::vector<N>> sets {
  9373. { { 0, 60, 1.0, 0.0, 0.0, 0.0, 0.0, -1 } },
  9374. { { 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 } },
  9375. { { 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 } },
  9376. { { 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 } },
  9377. { { 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 } },
  9378. };
  9379. const Golden goldens[] {
  9380. { 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 } } },
  9381. { 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 } } },
  9382. { 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 } } },
  9383. { 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 } } },
  9384. { 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 } } },
  9385. };
  9386. for (const auto& g : goldens)
  9387. {
  9388. const auto st = stateOf ({ g.P[0], g.P[1], g.P[2], g.P[3], g.P[4] });
  9389. const auto length = (int) g.fs * 2;
  9390. const auto y = lynx::plucked::renderReference (st, sets[(std::size_t) g.set], g.fs, length, g.block, 777u + (std::uint32_t) g.set,
  9391. g.law != 0, g.maxPoly);
  9392. double el = 0.0, er = 0.0;
  9393. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  9394. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  9395. 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;
  9396. for (const auto& smp : g.s)
  9397. 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;
  9398. if (! ok)
  9399. 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);
  9400. CHECK (ok);
  9401. }
  9402. // the instrument: plays, the gate makes a note-off silence the string, a held string dies out by itself
  9403. lynx::ChannelDef c;
  9404. c.instrument = lynx::InstrumentKind::plucked;
  9405. c.pluginState = stateOf ({ 200, 40, 1, 1, 1 });
  9406. auto inst = lynx::makeInstrument (lynx::InstrumentKind::plucked);
  9407. inst->configure (c, 120.0);
  9408. inst->prepare (44100.0);
  9409. std::vector<float> l (1024), r (1024);
  9410. inst->noteOn (57, 0.8f);
  9411. double e = 0.0;
  9412. for (int b = 0; b < 8; ++b)
  9413. {
  9414. std::fill (l.begin(), l.end(), 0.0f);
  9415. std::fill (r.begin(), r.end(), 0.0f);
  9416. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  9417. for (int i = 0; i < 1024; ++i)
  9418. e += (double) l[(std::size_t) i] * l[(std::size_t) i] + (double) r[(std::size_t) i] * r[(std::size_t) i];
  9419. }
  9420. CHECK (e > 1.0 && ! inst->idle());
  9421. inst->noteOff (57);
  9422. for (int b = 0; b < 4; ++b)
  9423. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  9424. CHECK (inst->idle());
  9425. c.pluginState = stateOf ({ 0, 64, 0, 0, 0 });
  9426. inst->configure (c, 120.0);
  9427. inst->noteOn (60, 1.0f);
  9428. inst->noteOff (60); // (no gate: the string rings on)
  9429. for (int b = 0; b < 2000 && ! inst->idle(); ++b)
  9430. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  9431. CHECK (inst->idle());
  9432. }
  9433. // BeepMap (REA_rodada65 §2): the C++ engine against FL's DLL (EXEC: same notes, the DLL's RandSeed set before the first
  9434. // note, the host killing voices when the plugin asks): a synthetic 9×6 bitmap with every Scale mode and switch, and two
  9435. // of the installation's presets. LYNX renders these bit-identically to the DLL.
  9436. std::vector<std::uint8_t> beepMapTestState (std::initializer_list<std::int32_t> settings)
  9437. {
  9438. constexpr int W = 9, H = 6;
  9439. std::vector<std::uint8_t> img (54 + 4 * W * H, 0);
  9440. 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)); };
  9441. img[0] = 'B', img[1] = 'M';
  9442. 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);
  9443. put (34, 4 * W * H, 4);
  9444. for (int y = 0; y < H; ++y) // bottom-up rows
  9445. for (int x = 0; x < W; ++x)
  9446. {
  9447. const auto yy = H - 1 - y;
  9448. auto* px = img.data() + 54 + 4 * (y * W + x);
  9449. px[0] = (std::uint8_t) ((x * 91 + yy * 29) & 255), px[1] = (std::uint8_t) ((x * 13 + yy * 71) & 255);
  9450. px[2] = (std::uint8_t) ((x * 37 + yy * 11) & 255), px[3] = 255;
  9451. }
  9452. std::vector<std::uint8_t> st (4 + 60 + 4, 0);
  9453. std::size_t k = 0;
  9454. for (auto v : settings)
  9455. std::memcpy (st.data() + 4 + 4 * k++, &v, 4);
  9456. const auto len = (std::int32_t) img.size();
  9457. std::memcpy (st.data() + 64, &len, 4);
  9458. st.insert (st.end(), img.begin(), img.end());
  9459. return st;
  9460. }
  9461. // the 0xD5 state of one of the installation's BeepMap presets (empty without an installation)
  9462. std::vector<std::uint8_t> beepMapPresetState (const char* name)
  9463. {
  9464. if (realFlpDir.empty())
  9465. return {};
  9466. std::ifstream in (std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "BeepMap" / name, std::ios::binary);
  9467. const std::vector<std::uint8_t> b ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  9468. std::size_t i = 0;
  9469. while (i + 8 <= b.size() && std::memcmp (b.data() + i, "FLdt", 4) != 0)
  9470. ++i;
  9471. i += 8;
  9472. while (i < b.size())
  9473. {
  9474. const auto id = b[i++];
  9475. std::size_t n = id < 64 ? 1 : id < 128 ? 2 : id < 192 ? 4 : 0;
  9476. if (id >= 192)
  9477. for (int shift = 0; i < b.size(); shift += 7)
  9478. {
  9479. const auto c = b[i++];
  9480. n |= (std::size_t) (c & 0x7F) << shift;
  9481. if (c < 0x80)
  9482. break;
  9483. }
  9484. if (id == 0xD5 && i + n <= b.size())
  9485. return { b.begin() + (std::ptrdiff_t) i, b.begin() + (std::ptrdiff_t) (i + n) };
  9486. i += n;
  9487. }
  9488. return {};
  9489. }
  9490. TEST (beepmap_engine)
  9491. {
  9492. const auto synthA = beepMapTestState ({ 3000, 200, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64 });
  9493. CHECK (lynx::beepMapStateUsable (synthA));
  9494. CHECK (lynx::beepMapParams (synthA)->at (1) == 200 && lynx::beepMapParamOffset (synthA, 2) == 12);
  9495. auto broken = synthA;
  9496. broken[0] = 2; // versions above 1 are refused
  9497. CHECK (! lynx::beepMapStateUsable (broken));
  9498. broken = synthA;
  9499. broken.resize (broken.size() - 20); // truncated bitmap
  9500. CHECK (! lynx::beepMapStateUsable (broken));
  9501. using N = lynx::beepmap::RefNote;
  9502. struct Sample { int at; float l, r; };
  9503. struct Golden { int set; double fs; int block; double el, er; Sample s[4]; };
  9504. std::vector<std::vector<std::uint8_t>> states {
  9505. synthA,
  9506. beepMapTestState ({ 5000, 600, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 64 }),
  9507. beepMapTestState ({ 8000, 100, 2, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 64 }),
  9508. beepMapTestState ({ 1000, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 64 }),
  9509. beepMapPresetState ("Default.fst"),
  9510. beepMapPresetState ("Voice.fst"),
  9511. };
  9512. const std::vector<std::vector<N>> sets {
  9513. { { 0, 60, 1.0, 0.0, 0.0, -1 } },
  9514. { { 0, 57, 0.8, 0.3, 25.0, -1 }, { 7000, 64, 0.6, -0.5, 0.0, -1 } },
  9515. { { 0, 48, 1.0, 0.0, 0.0, -1 }, { 300, 72, 0.5, 0.0, -40.0, -1 } },
  9516. { { 0, 67, 1.0, -0.2, 0.0, -1 } },
  9517. { { 0, 60, 1.0, 0.0, 0.0, -1 } },
  9518. { { 0, 55, 0.8, 0.0, 0.0, -1 }, { 20000, 62, 0.8, 0.4, 0.0, -1 } },
  9519. };
  9520. const Golden goldens[] {
  9521. { 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 } } },
  9522. { 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 } } },
  9523. { 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 } } },
  9524. { 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 } } },
  9525. { 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 } } },
  9526. { 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 } } },
  9527. };
  9528. for (const auto& g : goldens)
  9529. {
  9530. const auto& st = states[(std::size_t) g.set];
  9531. if (st.empty())
  9532. continue; // (no FL installation: the presets are skipped)
  9533. const auto length = (int) g.fs * 2;
  9534. const auto y = lynx::beepmap::renderReference (st, sets[(std::size_t) g.set], g.fs, length, g.block, 4321u + (std::uint32_t) g.set);
  9535. double el = 0.0, er = 0.0;
  9536. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  9537. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  9538. 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;
  9539. for (const auto& smp : g.s)
  9540. 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;
  9541. if (! ok)
  9542. 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);
  9543. CHECK (ok);
  9544. }
  9545. // the instrument: plays the image, ends after the last column, a note-off fades the voice out
  9546. lynx::ChannelDef c;
  9547. c.instrument = lynx::InstrumentKind::beepmap;
  9548. c.pluginState = states[1];
  9549. auto inst = lynx::makeInstrument (lynx::InstrumentKind::beepmap);
  9550. inst->configure (c, 120.0);
  9551. inst->prepare (44100.0);
  9552. std::vector<float> l (1024), r (1024);
  9553. inst->noteOn (60, 0.8f);
  9554. double e = 0.0;
  9555. for (int b = 0; b < 8; ++b)
  9556. {
  9557. std::fill (l.begin(), l.end(), 0.0f);
  9558. std::fill (r.begin(), r.end(), 0.0f);
  9559. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  9560. for (int i = 0; i < 1024; ++i)
  9561. e += (double) l[(std::size_t) i] * l[(std::size_t) i] + (double) r[(std::size_t) i] * r[(std::size_t) i];
  9562. }
  9563. CHECK (e > 0.01 && ! inst->idle()); // (Loop on)
  9564. inst->noteOff (60);
  9565. for (int b = 0; b < 4; ++b)
  9566. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  9567. CHECK (inst->idle());
  9568. c.pluginState = synthA; // (no loop: 9 columns of 64·(e^(200/4096·ln 33) − 1)… samples, then the voice ends)
  9569. inst->configure (c, 120.0);
  9570. inst->noteOn (60, 1.0f);
  9571. for (int b = 0; b < 200 && ! inst->idle(); ++b)
  9572. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  9573. CHECK (inst->idle());
  9574. }
  9575. // ---- Sawer (REA_rodada65 §3): installation presets and data.saw read at run time, fingerprints of engine_x64.dll ----
  9576. namespace
  9577. {
  9578. std::vector<std::uint8_t> sawerPresetState (const char* rel)
  9579. {
  9580. if (realFlpDir.empty())
  9581. return {};
  9582. std::ifstream in (std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "Sawer" / rel, std::ios::binary);
  9583. const std::vector<std::uint8_t> b ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  9584. std::size_t i = 0;
  9585. while (i + 8 <= b.size() && std::memcmp (b.data() + i, "FLdt", 4) != 0)
  9586. ++i;
  9587. i += 8;
  9588. while (i < b.size())
  9589. {
  9590. const auto id = b[i++];
  9591. std::size_t n = id < 64 ? 1 : id < 128 ? 2 : id < 192 ? 4 : 0;
  9592. if (id >= 192)
  9593. for (int shift = 0; i < b.size(); shift += 7)
  9594. {
  9595. const auto c = b[i++];
  9596. n |= (std::size_t) (c & 0x7F) << shift;
  9597. if (c < 0x80)
  9598. break;
  9599. }
  9600. if (id == 0xD5 && i + n <= b.size())
  9601. return { b.begin() + (std::ptrdiff_t) i, b.begin() + (std::ptrdiff_t) (i + n) };
  9602. i += n;
  9603. }
  9604. return {};
  9605. }
  9606. } // namespace
  9607. TEST (sawer_engine)
  9608. {
  9609. // a synthetic header: "SSLF" + size + "1rwS" + 96 floats
  9610. std::vector<std::uint8_t> synth (12 + 4 * 96, 0);
  9611. std::memcpy (synth.data(), "SSLF", 4);
  9612. const std::uint32_t size = 4 + 4 * 96;
  9613. std::memcpy (synth.data() + 4, &size, 4);
  9614. std::memcpy (synth.data() + 8, "1rwS", 4);
  9615. const float p27 = 0.75f;
  9616. std::memcpy (synth.data() + 12 + 4 * 27, &p27, 4);
  9617. CHECK (lynx::sawerStateUsable (synth));
  9618. CHECK (lynx::sawerParam (synth, 27) == 0.75f && lynx::sawerParamOffset (synth, 27) == 12 + 4 * 27);
  9619. CHECK (! lynx::sawerParam (synth, 96) && lynx::sawerParamOffset (synth, -1) < 0);
  9620. auto broken = synth;
  9621. broken[8] = 'X';
  9622. CHECK (! lynx::sawerStateUsable (broken));
  9623. CHECK (! lynx::sawerStateUsable (std::vector<std::uint8_t> (synth.begin(), synth.end() - 4)));
  9624. const auto dflt = sawerPresetState ("Default.fst");
  9625. const auto slap = sawerPresetState ("Arpeggios/FG Velocity Slapper.fst");
  9626. const auto tablePath = realFlpDir.empty() ? std::string() : (std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "Sawer" / "data.saw").string();
  9627. const auto table = tablePath.empty() ? std::vector<float> {} : lynx::sawerReadTable (tablePath);
  9628. if (dflt.empty() || slap.empty() || table.size() != 8192u)
  9629. {
  9630. std::printf (" (sawer_engine: FL Studio installation not found, DLL fingerprints skipped)\n");
  9631. return;
  9632. }
  9633. CHECK (lynx::sawerStateUsable (dflt) && lynx::sawerStateUsable (slap));
  9634. // DLL fingerprints (py/gold_sw.py: the EXEC harness, L = R = Final.Vol); "syn" = Default with our own settings
  9635. // (unison 4, sub pulse at 0.6, sync, band-pass, noise, resonance, chorus)
  9636. auto setP = [] (std::vector<std::uint8_t> s, int k, float v) { std::memcpy (s.data() + 12 + 4 * k, &v, 4); return s; };
  9637. auto syn = dflt;
  9638. 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 },
  9639. { 26, 0.3f }, { 28, 0.7f }, { 45, 0.5f }, { 46, 0.0f } })
  9640. syn = setP (syn, kv.first, kv.second);
  9641. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  9642. using N = lynx::sawer::RefNote;
  9643. struct Sample { int at; float l, r; };
  9644. struct Golden { const std::vector<std::uint8_t>* state; std::vector<N> notes; double fs; int block; double el, er; Sample s[4]; };
  9645. const Golden goldens[] {
  9646. { &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 } },
  9647. 44100, 512, 1172.343107, 1169.673445,
  9648. { { 1000, 0.0349505879f, 0.0349650607f }, { 10000, -0.0326882228f, -0.0326587185f }, { 30000, 0.139422998f, 0.139268339f }, { 60000, -1.01108653e-05f, -1.01210762e-05f } } },
  9649. { &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,
  9650. { { 1000, 0.00919434987f, 0.0769683793f }, { 10000, 0.0363935083f, 0.0227626693f }, { 30000, -0.00462511741f, -0.0105419969f }, { 60000, -0.00727002183f, -0.00159690797f } } },
  9651. { &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,
  9652. { { 1000, -0.162745371f, -0.172861978f }, { 10000, 0.0468756184f, 0.112415045f }, { 30000, -0.123887017f, -0.132816017f }, { 60000, 0.0f, 0.0f } } },
  9653. };
  9654. for (const auto& g : goldens)
  9655. {
  9656. const auto y = lynx::sawer::renderReference (*g.state, table, g.notes, g.fs, 66150, g.block);
  9657. CHECK (y.size() == 2 * 66150u);
  9658. if (y.size() != 2 * 66150u)
  9659. continue;
  9660. double el = 0.0, er = 0.0;
  9661. for (std::size_t i = 0; i < 66150; ++i)
  9662. el += (double) y[2 * i] * y[2 * i], er += (double) y[2 * i + 1] * y[2 * i + 1];
  9663. CHECK (std::abs (el - g.el) < 1e-5 * g.el && std::abs (er - g.er) < 1e-5 * g.er);
  9664. for (const auto& s : g.s)
  9665. 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);
  9666. }
  9667. // the instrument: the same engine behind LYNX's channel gains (centre → law A), velocity → Init.Vol; it goes idle
  9668. lynx::ChannelDef c;
  9669. c.instrument = lynx::InstrumentKind::sawer;
  9670. c.pluginState = dflt;
  9671. c.samplePath = tablePath;
  9672. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sawer);
  9673. inst->configure (c, 120.0);
  9674. inst->prepare (44100.0);
  9675. std::vector<float> l (512 * 40, 0.0f), r (512 * 40, 0.0f);
  9676. for (int b = 0; b < 40; ++b)
  9677. {
  9678. if (b == 0)
  9679. inst->noteOn (60, 0.78125f);
  9680. if (b == 10)
  9681. inst->noteOff (60);
  9682. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  9683. }
  9684. 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,
  9685. 8192, 512, 120.0, 0, true);
  9686. double diff = 0.0, energy = 0.0;
  9687. for (int i = 0; i < 8192; ++i)
  9688. {
  9689. 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]) });
  9690. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  9691. }
  9692. CHECK (diff == 0.0 && energy > 1e-3);
  9693. CHECK (inst->idle());
  9694. }
  9695. TEST (transistorbass_engine)
  9696. {
  9697. const auto factory = tbassPresetState ("Factory programs.fst");
  9698. const auto seqOff = tbassPresetState ("Sequencer off.fst");
  9699. if (factory.empty() || seqOff.empty())
  9700. return; // no FL installation
  9701. CHECK (factory.size() == 197887 && seqOff.size() == 196051);
  9702. CHECK (lynx::transistorBassStateUsable (factory) && lynx::transistorBassStateUsable (seqOff));
  9703. CHECK (lynx::transistorBassStateUsable (tbassWithEngine (factory, 2)));
  9704. auto broken = factory;
  9705. broken[0] = 3; // the wrapper refuses versions above 2
  9706. CHECK (! lynx::transistorBassStateUsable (broken));
  9707. auto truncated = factory;
  9708. truncated.resize (truncated.size() - 1000);
  9709. CHECK (! lynx::transistorBassStateUsable (truncated));
  9710. const auto dll = tbassDll();
  9711. if (dll.empty() || ! lynx::transistorBassDllUsable (dll))
  9712. return;
  9713. // against FL's engine DLL (EXEC, REA_rodada57 §2.11): programs of the factory bank (sequencer, accents, slides,
  9714. // distortion, delay, reverb) on the three engines and the sequencer-off preset with legato and accent notes; host
  9715. // blocks of 512 at 44.1 kHz, tempo 125. LYNX differs from the DLL by −121 dB or less here (library pow rounding).
  9716. struct Sample { int at; float l, r; };
  9717. struct Golden { const char* preset; int engine, key; double el, er; Sample s[4]; };
  9718. const Golden goldens[] {
  9719. { "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 } } },
  9720. { "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 } } },
  9721. { "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 } } },
  9722. { "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 } } },
  9723. { "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 } } },
  9724. { "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 } } },
  9725. { "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 } } },
  9726. { "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 } } },
  9727. { "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 } } },
  9728. { "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 } } },
  9729. };
  9730. constexpr int length = 88200;
  9731. for (const auto& g : goldens)
  9732. {
  9733. const bool factoryBank = std::string (g.preset) == "Factory";
  9734. auto st = factoryBank ? factory : seqOff;
  9735. if (g.engine >= 0)
  9736. st = tbassWithEngine (st, g.engine);
  9737. std::vector<lynx::tbass::RefNote> notes;
  9738. if (factoryBank)
  9739. notes.push_back ({ 0, g.key, 0.5, 0.0, 0.0, 0.0, 0.0, 70560 });
  9740. else
  9741. {
  9742. notes.push_back ({ 0, g.key, 0.5, 0.0, 0.0, 0.0, 0.0, 30000 });
  9743. notes.push_back ({ 20000, g.key + 7, 1.6, 0.0, 0.0, 0.0, 0.0, 50000 });
  9744. notes.push_back ({ 45000, g.key - 5, 0.3, 0.0, 0.0, 0.0, 0.0, 70560 });
  9745. }
  9746. const auto y = lynx::tbass::renderReference (st, dll, notes, 44100.0, length);
  9747. double el = 0.0, er = 0.0, peak = 0.0;
  9748. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  9749. 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]) });
  9750. 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;
  9751. for (const auto& smp : g.s)
  9752. 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;
  9753. if (! ok)
  9754. 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);
  9755. CHECK (ok);
  9756. }
  9757. // the instrument: FL's wrapper velocity, the channel volume as an output gain, the sequencer plays while the key is held
  9758. lynx::ChannelDef c;
  9759. c.instrument = lynx::InstrumentKind::transistorbass;
  9760. c.pluginState = factory;
  9761. c.samplePath = dll;
  9762. auto inst = lynx::makeInstrument (lynx::InstrumentKind::transistorbass);
  9763. inst->configure (c, 128.0);
  9764. inst->prepare (44100.0);
  9765. std::vector<float> l (4096), r (4096);
  9766. inst->noteOn (65, 0.8f);
  9767. double e = 0.0;
  9768. bool finite = true;
  9769. for (int b = 0; b < 24; ++b)
  9770. {
  9771. std::fill (l.begin(), l.end(), 0.0f);
  9772. std::fill (r.begin(), r.end(), 0.0f);
  9773. inst->render (l.data(), r.data(), 4096, 0.5f, 0.5f);
  9774. for (int i = 0; i < 4096; ++i)
  9775. 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];
  9776. if (b == 20)
  9777. inst->noteOff (65);
  9778. }
  9779. CHECK (finite && e > 1.0 && ! inst->idle());
  9780. for (int b = 0; b < 400 && ! inst->idle(); ++b)
  9781. inst->render (l.data(), r.data(), 4096, 0.5f, 0.5f);
  9782. CHECK (inst->idle());
  9783. }
  9784. TEST (kepler_demo_channels)
  9785. {
  9786. // every Kepler channel of FL's demo projects on LYNX's engine against FL's engine DLL (EXEC fingerprints, REA_rodada46 §6;
  9787. // the engine differs from the DLL by −127 dB or less on these, from the DLL library's pow rounding)
  9788. const auto dll = keplerDll();
  9789. if (dll.empty() || ! lynx::keplerDllUsable (dll))
  9790. return;
  9791. struct Sample { int at; float l, r; };
  9792. struct Golden { const char* file; const char* channel; std::size_t stateSize; double el, er; Sample s[4]; };
  9793. const Golden goldens[] {
  9794. { "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 } } },
  9795. { "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 } } },
  9796. { "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 } } },
  9797. { "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 } } },
  9798. };
  9799. int checked = 0;
  9800. for (const auto& g : goldens)
  9801. {
  9802. std::ifstream in (std::filesystem::path (realFlpDir) / "Demo projects" / g.file, std::ios::binary);
  9803. const std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  9804. if (bytes.empty())
  9805. continue;
  9806. const flp::Channel* ch = nullptr;
  9807. const auto project = flp::parseProject (bytes);
  9808. for (const auto& c : project.channels)
  9809. if (c.pluginName == "Kepler" && c.name == g.channel)
  9810. ch = &c;
  9811. CHECK (ch != nullptr);
  9812. if (ch == nullptr)
  9813. continue;
  9814. CHECK (ch->pluginState.size() == g.stateSize && lynx::keplerStateUsable (ch->pluginState));
  9815. 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 },
  9816. { 16000, 55, 0.95, 0.0, 0.0, 30000 } }, 44100.0, 39690);
  9817. double el = 0.0, er = 0.0, peak = 0.0;
  9818. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  9819. 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]) });
  9820. bool ok = y.size() == 2 * 39690 && std::abs (el - g.el) <= 1e-5 * g.el && std::abs (er - g.er) <= 1e-5 * g.er;
  9821. for (const auto& smp : g.s)
  9822. 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;
  9823. if (! ok)
  9824. 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);
  9825. CHECK (ok);
  9826. ++checked;
  9827. }
  9828. std::printf (" Kepler: %d demo channels checked against FL's engine DLL\n", checked);
  9829. CHECK (checked == (int) std::size (goldens));
  9830. }
  9831. TEST (ogun_demo_channels)
  9832. {
  9833. // every Ogun channel of FL's demo projects (with its plugin effects) against the round-37 reference model, which is
  9834. // 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),
  9835. // 39 690 samples at 44.1 kHz, at the state's quality and at the export quality 4. Fingerprint: energy per side and
  9836. // four samples (the engine matches the model within float rounding, −125 dB or better on the whole signal).
  9837. if (realFlpDir.empty())
  9838. return;
  9839. struct Sample { int at; float l, r; };
  9840. struct Golden { const char* file; int ordinal; std::size_t stateSize; bool export4; double el, er; Sample s[4]; };
  9841. const Golden goldens[] {
  9842. { "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
  9843. { "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
  9844. { "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
  9845. { "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
  9846. { "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
  9847. { "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
  9848. { "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
  9849. { "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
  9850. { "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
  9851. { "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
  9852. { "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
  9853. { "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
  9854. { "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
  9855. { "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
  9856. { "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
  9857. { "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
  9858. { "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
  9859. { "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
  9860. { "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
  9861. { "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
  9862. { "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
  9863. { "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
  9864. { "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
  9865. { "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
  9866. { "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
  9867. { "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
  9868. { "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
  9869. { "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
  9870. { "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
  9871. { "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
  9872. { "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
  9873. { "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
  9874. { "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
  9875. { "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
  9876. { "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
  9877. { "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
  9878. { "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
  9879. { "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
  9880. { "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
  9881. { "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
  9882. };
  9883. std::map<std::string, std::vector<std::vector<std::uint8_t>>> states;
  9884. int checked = 0;
  9885. for (const auto& g : goldens)
  9886. {
  9887. if (! states.contains (g.file))
  9888. {
  9889. std::ifstream in (std::filesystem::path (realFlpDir) / "Demo projects" / g.file, std::ios::binary);
  9890. const std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  9891. auto& list = states[g.file];
  9892. if (! bytes.empty())
  9893. for (const auto& c : flp::parseProject (bytes).channels)
  9894. if (c.pluginName == "Ogun")
  9895. list.push_back (c.pluginState);
  9896. }
  9897. const auto& list = states[g.file];
  9898. if (list.empty())
  9899. continue; // not this installation
  9900. CHECK ((int) list.size() > g.ordinal);
  9901. if ((int) list.size() <= g.ordinal)
  9902. continue;
  9903. const auto& st = list[(std::size_t) g.ordinal];
  9904. CHECK (st.size() == g.stateSize);
  9905. lynx::ogun::RefOptions opt;
  9906. opt.quality = g.export4 ? 4 : -1;
  9907. const auto y = lynx::ogun::renderReference (st, { { 0, 60, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 20000 },
  9908. { 8000, 67, 0.6, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 26000 },
  9909. { 16000, 55, 0.95, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 30000 } }, 44100.0, 39690, opt);
  9910. double el = 0.0, er = 0.0, peak = 0.0;
  9911. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  9912. 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]) });
  9913. bool ok = y.size() == 2 * 39690 && std::abs (el - g.el) <= 1e-5 * g.el && std::abs (er - g.er) <= 1e-5 * g.er;
  9914. for (const auto& smp : g.s)
  9915. 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;
  9916. if (! ok)
  9917. std::printf (" Ogun %s #%d (q%s) differs from the model: energy %.9g %.9g (model %.9g %.9g)\n", g.file, g.ordinal,
  9918. g.export4 ? "4" : "-state", el, er, g.el, g.er);
  9919. CHECK (ok);
  9920. ++checked;
  9921. }
  9922. std::printf (" Ogun: %d demo renders checked against the reference model\n", checked);
  9923. CHECK (checked == (int) std::size (goldens));
  9924. }
  9925. TEST (autogun_template_channels)
  9926. {
  9927. // REA_rodada57: every distinct Autogun state of the installation's projects (templates and the render test), with its
  9928. // effects and the Magic Dust stage (SG.dat from the installation), three notes (C5 1.0 0–20000, G5 0.6 8000–26000,
  9929. // 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
  9930. // goldens are renders of the installation's Autogun_x64.dll (LYNX is within −127 dB of them); the render-test state
  9931. // uses Seed 2 "Random", so its golden is LYNX's own render with the counter at 0 (a fingerprint of the engine).
  9932. if (realFlpDir.empty())
  9933. return;
  9934. const auto sgPath = std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "Autogun" / "Data" / "SG.dat";
  9935. std::ifstream sgIn (sgPath, std::ios::binary);
  9936. const std::vector<std::uint8_t> sgState ((std::istreambuf_iterator<char> (sgIn)), std::istreambuf_iterator<char>());
  9937. if (sgState.size() != 1342)
  9938. return; // not this installation
  9939. struct Sample { int at; float l, r; };
  9940. struct Golden { const char* file; std::size_t stateSize; bool export4; double el, er; Sample s[4]; };
  9941. const Golden goldens[] {
  9942. { "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)
  9943. { "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)
  9944. { "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)
  9945. { "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)
  9946. { "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)
  9947. { "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)
  9948. { "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)
  9949. { "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)
  9950. { "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)
  9951. { "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)
  9952. };
  9953. int checked = 0;
  9954. for (const auto& g : goldens)
  9955. {
  9956. std::ifstream in (std::filesystem::path (realFlpDir) / g.file, std::ios::binary);
  9957. const std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  9958. if (bytes.empty())
  9959. continue;
  9960. std::vector<std::uint8_t> st;
  9961. for (const auto& c : flp::parseProject (bytes).channels)
  9962. if (c.pluginName == "Autogun" && st.empty())
  9963. st = c.pluginState;
  9964. CHECK (st.size() == g.stateSize);
  9965. CHECK (lynx::autogunStateUsable (st));
  9966. lynx::ogun::RefOptions opt;
  9967. opt.autogun = true;
  9968. opt.sgState = sgState;
  9969. opt.quality = g.export4 ? 4 : -1;
  9970. const auto y = lynx::ogun::renderReference (st, { { 0, 60, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 20000 },
  9971. { 8000, 67, 0.6, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 26000 },
  9972. { 16000, 55, 0.95, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 30000 } }, 44100.0, 39690, opt);
  9973. double el = 0.0, er = 0.0, peak = 0.0;
  9974. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  9975. 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]) });
  9976. bool ok = y.size() == 2 * 39690 && std::abs (el - g.el) <= 1e-5 * g.el && std::abs (er - g.er) <= 1e-5 * g.er;
  9977. for (const auto& smp : g.s)
  9978. 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;
  9979. if (! ok)
  9980. 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);
  9981. CHECK (ok);
  9982. ++checked;
  9983. }
  9984. std::printf (" Autogun: %d template/render-test renders checked (DLL goldens for the fixed-spectrum states)\n", checked);
  9985. CHECK (checked == (int) std::size (goldens));
  9986. // the instrument (SG.dat read from ChannelDef::samplePath) plays like the reference renderer, then goes idle
  9987. {
  9988. std::ifstream in (std::filesystem::path (realFlpDir) / "Templates" / "Advanced" / "Electronic" / "Trap.flp", std::ios::binary);
  9989. const std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  9990. lynx::ChannelDef c;
  9991. c.instrument = lynx::InstrumentKind::autogun;
  9992. for (const auto& ch : flp::parseProject (bytes).channels)
  9993. if (ch.pluginName == "Autogun")
  9994. c.pluginState = ch.pluginState;
  9995. c.samplePath = sgPath.string();
  9996. lynx::ogun::RefOptions opt;
  9997. opt.autogun = true;
  9998. opt.sgState = sgState;
  9999. opt.tempo = 120.0; // the instrument is configured at 120 BPM
  10000. // (round 190pi: FL's host hands a velocity-linked voice Final.Vol = Init.Vol, (11^(1.28·v) − 1)/10 — Autogun's
  10001. // default patch scales with the velocity in FL 2026's renders, REA_rodada190pi §3; this state links it)
  10002. const auto finalVol = (std::pow (11.0, 1.28 * 0.75) - 1.0) / 10.0;
  10003. const auto ref = lynx::ogun::renderReference (c.pluginState, { { 0, 60, 0.75, finalVol, 0.0, 0.0, 0.0, 0.0, 0.5, 6144 } }, 44100.0, 16384, opt);
  10004. auto inst = lynx::makeInstrument (lynx::InstrumentKind::autogun);
  10005. inst->configure (c, 120.0);
  10006. inst->prepare (44100.0);
  10007. std::vector<float> l (16384), r (16384);
  10008. for (int s = 0; s < 16384; s += 512)
  10009. {
  10010. if (s == 0)
  10011. inst->noteOn (60, 0.75f);
  10012. if (s == 6144)
  10013. inst->noteOff (60);
  10014. inst->render (l.data() + s, r.data() + s, 512, 1.0f, 1.0f);
  10015. }
  10016. double worst = 0.0, peak = 0.0;
  10017. for (int i = 0; i < 16384; ++i)
  10018. 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]) }),
  10019. peak = std::max (peak, (double) std::abs (l[(std::size_t) i]));
  10020. if (worst != 0.0)
  10021. std::printf (" Autogun instrument vs reference: worst %.3g (peak %.3g)\n", worst, peak);
  10022. CHECK (ref.size() == 2 * 16384 && peak > 0.01 && worst == 0.0);
  10023. std::vector<float> tail (44100), tailR (tail.size());
  10024. for (int k = 0; k < 40 && ! inst->idle(); ++k)
  10025. inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
  10026. CHECK (inst->idle());
  10027. }
  10028. // the state variants: Ogun's parser rejects the version-19 Autogun layout, the comment length is one byte or 0xff + i32
  10029. std::ifstream rt (std::filesystem::path (realFlpDir) / "System" / "Render tests" / "Autogun.flp", std::ios::binary);
  10030. const std::vector<std::uint8_t> rtBytes ((std::istreambuf_iterator<char> (rt)), std::istreambuf_iterator<char>());
  10031. for (const auto& c : flp::parseProject (rtBytes).channels)
  10032. if (c.pluginName == "Autogun")
  10033. CHECK (lynx::autogunStateUsable (c.pluginState) && lynx::ogunParam (c.pluginState, 63) == 430);
  10034. }
  10035. TEST (harmless_rack)
  10036. {
  10037. // REA_rodada36: Harmless's effects rack against our reference model (rack.py / maxmodel.py, which match the
  10038. // installation's Harmless_x64.dll and SG_x64.dll), 44.1 kHz, host blocks of 512
  10039. std::vector<float> inL (8192), inR (8192);
  10040. for (int i = 0; i < 4096; ++i)
  10041. {
  10042. 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));
  10043. inR[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / 44100.0));
  10044. }
  10045. auto run = [&] (const std::array<int, 100>& p, int compType, std::shared_ptr<const lynx::hlfx::MaximusPreset> comp, int total, int voicesUntil) {
  10046. lynx::hlfx::Rack rack;
  10047. std::array<int, 7> t {};
  10048. t[3] = compType;
  10049. rack.prepare (p, t, 44100.0, 120.0, std::move (comp));
  10050. std::vector<float> l (inL.begin(), inL.begin() + total), r (inR.begin(), inR.begin() + total);
  10051. for (int s = 0; s < total; s += 512)
  10052. rack.process (l.data() + s, r.data() + s, std::min (512, total - s), s < voicesUntil);
  10053. return std::make_pair (l, r);
  10054. };
  10055. {
  10056. // chorus type 3 (mix 400), ping-pong delay type 2 (vol 600, feedback 500, 1/4 beat), reverb type 4 (vol 700)
  10057. std::array<int, 100> p {};
  10058. p[90] = 3, p[91] = 400, p[92] = 2, p[93] = 600, p[94] = 500, p[95] = 48, p[96] = 4, p[97] = 700;
  10059. const auto [l, r] = run (p, 0, nullptr, 8192, 4096);
  10060. const std::tuple<int, float, float> golden[] { { 0, 0.0f, 0.0f }, { 500, -0.25248247f, 0.15141529f }, { 3000, -0.65128994f, -0.64854866f },
  10061. { 5000, -0.54308629f, -0.36827448f }, { 6000, -0.98378551f, -0.72790027f },
  10062. { 8191, 0.50599128f, -0.43637198f } };
  10063. for (const auto& [i, gl, gr] : golden)
  10064. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  10065. }
  10066. {
  10067. // the delay alone: an impulse comes back after round(time·fs·0.3125/bpm) samples × (11^0.6 − 1)/10, then × feedback
  10068. std::array<int, 100> p {};
  10069. p[92] = 1, p[93] = 600, p[94] = 500, p[95] = 48;
  10070. lynx::hlfx::Rack rack;
  10071. rack.prepare (p, {}, 44100.0, 120.0, nullptr);
  10072. std::vector<float> l (16384), r (16384);
  10073. l[0] = 1.0f;
  10074. for (int s = 0; s < 16384; s += 512)
  10075. rack.process (l.data() + s, r.data() + s, 512, s == 0);
  10076. const auto D = 5512; // 48·44100·0.3125/120 = 5512.5 → 5512 (round half to even)
  10077. const auto vol = (float) ((std::exp ((double) (600 * 0.001f) * (double) (float) std::log (11.0)) - 1.0) * (double) 0.1f);
  10078. CHECK (std::abs (l[(std::size_t) D] - vol) < 1e-7f && std::abs (l[(std::size_t) D - 1]) < 1e-12f);
  10079. // the feedback ramps from 0.5 toward 0.5: the second echo is vol·0.5
  10080. CHECK (std::abs (l[(std::size_t) (2 * D)] - vol * 0.5f) < 1e-7f && std::abs (r[(std::size_t) D]) < 1e-12f);
  10081. }
  10082. // the compressor: Maximus states of our own (no preset data), checked against SG_x64.dll by REA round 36
  10083. using Env = std::vector<std::array<double, 4>>;
  10084. const Env def { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 } };
  10085. 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,
  10086. int sus = 2882) { return std::array<int, 13> { mode, pre, post, sep, sat, ceil, att, rel2, 2, 5000, 3, sus, 0 }; };
  10087. {
  10088. // master brickwall at −0.22 dB only (bands off / compression off)
  10089. const auto st = maximusTestState ({ 19800, 1, 48065, 1, 1115, 1000, -1 }, { band (3), band (1), band (3), band (0) },
  10090. { def, def, def, def, def, def, def, def }, 0);
  10091. const auto preset = lynx::hlfx::makeMaximusPreset (st);
  10092. CHECK (preset != nullptr);
  10093. std::array<int, 100> p {};
  10094. p[98] = 1000;
  10095. const auto [l, r] = run (p, 1, preset, 4096, 4096);
  10096. // SG_x64.dll's own output (EXEC, REA_rodada40: bit-identical to the port)
  10097. const std::tuple<int, float, float> golden[] { { 100, 0.024997942f, 0.88166064f }, { 1000, 0.56971401f, -0.62808102f },
  10098. { 2047, -0.27541628f, 0.709135f }, { 3000, 0.32431623f, -0.37985197f },
  10099. { 4095, -0.12743522f, -0.15237485f } };
  10100. for (const auto& [i, gl, gr] : golden)
  10101. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  10102. float peak = 0.0f;
  10103. for (int i = 2048; i < 4096; ++i)
  10104. peak = std::max ({ peak, std::abs (l[(std::size_t) i]), std::abs (r[(std::size_t) i]) });
  10105. CHECK (peak < 0.9751f);
  10106. }
  10107. {
  10108. // 24 dB crossovers, ×2 oversampling, low cut, stereo separation, saturation A, a knee with tension (type 7) and
  10109. // exponential tensions (type 0), release 2 on the master, band mix 600
  10110. const Env knee { { 0.0, 0.0, 0.0, 0 }, { 0.35, 0.35, 0.0, 0 }, { 0.65, 0.6, 0.36, 7 } };
  10111. const Env t0 { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.14, 0 }, { 0.5, 0.5, 0.0, 0 } };
  10112. const Env m0 { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 }, { 0.5, 0.6, 0.085, 0 } };
  10113. const auto st = maximusTestState ({ 19800, 1, 48065, 1, 891, 600, 32768 },
  10114. { band (1, 1264, 1296, 500, -500, 1200), band (0, 1340, 1000, -348, 0, 1000, 1115, 0, 1570),
  10115. band (0, 1364, 1152, 0, 0, 1000, 1115, 0, 1186), band (0, 1000, 1000, 0, -196, 1000, 1115, 4464, 1538) },
  10116. { def, def, knee, def, t0, def, m0, def }, 1);
  10117. const auto preset = lynx::hlfx::makeMaximusPreset (st);
  10118. CHECK (preset != nullptr);
  10119. std::array<int, 100> p {};
  10120. p[98] = 600;
  10121. const auto [l, r] = run (p, 2, preset, 4096, 4096);
  10122. // SG_x64.dll's own output (EXEC, REA_rodada40; the port is within −142 dB of it)
  10123. const std::tuple<int, float, float> golden[] { { 100, 0.0f, 0.0f }, { 1000, -0.46856543f, -0.63262129f },
  10124. { 2047, 0.20268451f, 0.54145539f }, { 3000, -0.25613472f, -0.49985155f },
  10125. { 4095, 0.10172445f, 0.050564945f } };
  10126. for (const auto& [i, gl, gr] : golden)
  10127. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  10128. }
  10129. {
  10130. // an .fst wrapper around a state is found; unsupported states are rejected
  10131. const auto st = maximusTestState ({ 19800, 1, 48065, 1, 1115, 1000, -1 }, { band (3), band (1), band (3), band (0) },
  10132. { def, def, def, def, def, def, def, def }, 0);
  10133. std::vector<std::uint8_t> fst { 'F', 'L', 'h', 'd', 6, 0, 0, 0, 0, 0, 1, 0, 0x60, 0, 'F', 'L', 'd', 't' };
  10134. std::vector<std::uint8_t> body { 0xC9, 8, 'M', 'a', 'x', 'i', 'm', 'u', 's', 0, 0xD5 };
  10135. for (auto n = st.size(); ; n >>= 7)
  10136. {
  10137. body.push_back ((std::uint8_t) ((n & 0x7f) | (n >= 0x80 ? 0x80 : 0)));
  10138. if (n < 0x80)
  10139. break;
  10140. }
  10141. body.insert (body.end(), st.begin(), st.end());
  10142. const auto len = (std::uint32_t) body.size();
  10143. for (int k = 0; k < 4; ++k)
  10144. fst.push_back ((std::uint8_t) (len >> (8 * k)));
  10145. fst.insert (fst.end(), body.begin(), body.end());
  10146. const auto found = lynx::hlfx::maximusStateFromFst (fst);
  10147. CHECK (found && *found == st);
  10148. auto rms = st;
  10149. rms[4 + 4 * (7 + 13 * 3 + 12)] = 1; // RMS detector on the master (decoded in REA_rodada40)
  10150. CHECK (lynx::hlfx::makeMaximusPreset (rms) != nullptr);
  10151. auto masterMid = st;
  10152. masterMid[0x1fc] = 1; // "Master Mid mode": not decoded
  10153. CHECK (lynx::hlfx::makeMaximusPreset (masterMid) == nullptr);
  10154. }
  10155. }
  10156. TEST (fruity_delay_bank_engine)
  10157. {
  10158. // REA_rodada56: Fruity Delay Bank, two states of our own against the installation's DLL (goldens = the DLL's output
  10159. // for the same input, EXEC; a new instance, FTZ/DAZ as the plugin's thread). Both are bit-identical.
  10160. lynx::ScopedFlushDenormals ftz;
  10161. struct Bank
  10162. {
  10163. std::array<int, 16> ctl;
  10164. lynx::fldbank::BankOptions opt;
  10165. };
  10166. auto build = [] (std::array<int, 4> globals, std::map<int, Bank> banks, int oversampling) {
  10167. auto s = lynx::fldbank::defaultState();
  10168. for (int i = 0; i < 4; ++i)
  10169. s.p[(std::size_t) i] = globals[(std::size_t) i];
  10170. s.p[132] = oversampling;
  10171. for (auto& [b, k] : banks)
  10172. {
  10173. for (int c = 0; c < 16; ++c)
  10174. if (16 * b + 4 + c < lynx::fldbank::automatable)
  10175. s.p[(std::size_t) (16 * b + 4 + c)] = k.ctl[(std::size_t) c];
  10176. s.bank[(std::size_t) b] = k.opt;
  10177. s.bank[(std::size_t) b].enabled = true;
  10178. }
  10179. return s;
  10180. };
  10181. auto opt = [] (int f1, int o1, int f2, int o2, bool after, int mode, int steps, int amount, bool tap, bool sync) {
  10182. lynx::fldbank::BankOptions o;
  10183. o.filterType[0] = f1;
  10184. o.filterOrder[0] = o1;
  10185. o.filterType[1] = f2;
  10186. o.filterOrder[1] = o2;
  10187. o.filterAfter = after;
  10188. o.mode = mode;
  10189. o.steps = steps;
  10190. o.amount = amount;
  10191. o.tapBeforeFeedback = tap;
  10192. o.sync = sync;
  10193. return o;
  10194. };
  10195. auto run = [] (const lynx::fldbank::State& s, double rate, int block, double bpm) {
  10196. std::vector<float> l (4096), r (4096);
  10197. for (int i = 0; i < 4096; ++i)
  10198. {
  10199. 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));
  10200. r[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / rate));
  10201. }
  10202. lynx::fldbank::Engine e (s, rate, bpm, block);
  10203. for (int i = 0; i < 4096; i += block)
  10204. e.process (l.data() + i, r.data() + i, std::min (block, 4096 - i));
  10205. return std::make_pair (l, r);
  10206. };
  10207. auto check = [] (const std::pair<std::vector<float>, std::vector<float>>& out, const std::vector<std::tuple<int, float, float>>& golden) {
  10208. for (const auto& [i, gl, gr] : golden)
  10209. CHECK (out.first[(std::size_t) i] == gl && out.second[(std::size_t) i] == gr);
  10210. };
  10211. // controls: in pan, in vol, f1 cutoff, f1 res, f1 gain, fb pan, fb level, fb cross, time, offset, f2 cutoff, f2 res,
  10212. // f2 gain, out pan, out vol, send. Three banks: ping-pong with low-pass ×2 / peaking feedback filter, pattern and send;
  10213. // free time with a high shelf after the delay, band-pass feedback, negative feedback/cross, tap before the feedback;
  10214. // swapped outputs with a 3-section high-pass in the loop and a negative pattern shape.
  10215. const auto a = build ({ 100, 128, 120, 110 },
  10216. { { 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) } },
  10217. { 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) } },
  10218. { 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) } } },
  10219. 0);
  10220. check (run (a, 44100.0, 512, 120.0), { { 100, -0.00294386433f, 0.0141299525f }, { 1000, -0.117360562f, -0.140237048f },
  10221. { 2047, 0.172598198f, -0.401046395f }, { 3000, 0.303020209f, -0.127763838f },
  10222. { 4095, 0.0093755126f, 0.241272509f } });
  10223. // 2× oversampling at 48 kHz in blocks of 300, low shelf before the delay, 2-section notch in the loop, a send into a
  10224. // ping-pong bank 3 (bank 1 and 2 off: no send) with a 2-step pattern
  10225. const auto b = build ({ 0, 128, 128, 128 },
  10226. { { 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) } },
  10227. { 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) } } },
  10228. 1);
  10229. check (run (b, 48000.0, 300, 93.0), { { 100, 0.0f, 0.0f }, { 1000, 0.0f, 0.0f }, { 2047, 0.201803684f, -0.327834547f },
  10230. { 3000, 0.412344277f, -0.101866335f }, { 4095, 0.204572573f, 0.71171385f } });
  10231. // FL's state round-trips through the import (version 15 times are converted), the globals are the slot's parameters
  10232. auto bytes = lynx::fldbank::serialise (a);
  10233. const auto slot = lynx::effectFromFl ("Fruity Delay Bank", bytes);
  10234. CHECK (slot && slot->kind == lynx::EffectKind::fruityDelayBank && slot->state == bytes);
  10235. CHECK (slot && slot->params[0] == 100.0f / 128.0f && slot->params[3] == 110.0f / 128.0f);
  10236. bytes[0] = 15;
  10237. const auto v15 = lynx::fldbank::parse (bytes);
  10238. CHECK (v15 && v15->p[12] == 2400 && v15->p[28] == 1500);
  10239. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityDelayBank, 3, 0.5f, 120.0) == "50%");
  10240. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityDelayBank, 0, 1.0f, 120.0) == "0.0 dB");
  10241. CHECK (! lynx::fldbank::parse ({ 16, 0, 0, 0 }));
  10242. // the LYNX effect (settled ramps, 512-frame engine blocks) runs from the slot
  10243. auto fx = lynx::makeEffect (*slot);
  10244. fx->prepare (44100.0);
  10245. std::vector<float> l (1024, 0.5f), r (1024, -0.25f);
  10246. fx->process (l.data(), r.data(), 1024, slot->params, 120.0);
  10247. CHECK (std::isfinite (l[1023]) && std::isfinite (r[1023]));
  10248. }
  10249. TEST (fruity_multiband_engine)
  10250. {
  10251. // REA_rodada56: Fruity Multiband Compressor, states of our own against the installation's DLL
  10252. // (goldens = the DLL's output for the same input, EXEC; blocks of 512 at 44.1 kHz; all three bit-identical)
  10253. std::vector<float> inL (4096), inR (4096);
  10254. for (int i = 0; i < 4096; ++i)
  10255. {
  10256. 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)
  10257. + 0.5 * std::sin (2.0 * 3.141592653589793 * 90.0 * i / 44100.0));
  10258. inR[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / 44100.0)
  10259. + 0.4 * std::sin (2.0 * 3.141592653589793 * 7000.0 * i / 44100.0));
  10260. }
  10261. auto multiband = [] (std::array<int, 28> c) {
  10262. std::vector<std::uint8_t> b;
  10263. auto put = [&b] (std::int32_t v) {
  10264. std::uint8_t q[4];
  10265. std::memcpy (q, &v, 4);
  10266. b.insert (b.end(), q, q + 4);
  10267. };
  10268. put (0);
  10269. for (auto v : c)
  10270. put (v);
  10271. put (0); // UI words of the state (not used by the engine)
  10272. put (50);
  10273. return b;
  10274. };
  10275. struct Out { std::vector<float> l, r; int latency = 0; };
  10276. auto run = [&] (const std::vector<std::uint8_t>& st) {
  10277. const auto s = lynx::flmb::parse (st);
  10278. CHECK (s.has_value());
  10279. lynx::EffectParams p {};
  10280. lynx::flmb::toParams (*s, p.data());
  10281. auto fx = lynx::flmb::make();
  10282. fx->prepare (44100.0);
  10283. Out o { inL, inR };
  10284. for (int s0 = 0; s0 < 4096; s0 += 512)
  10285. fx->process (o.l.data() + s0, o.r.data() + s0, 512, p, 120.0);
  10286. o.latency = fx->latencySamples();
  10287. return o;
  10288. };
  10289. auto check = [] (const Out& out, const std::vector<std::tuple<int, float, float>>& golden) {
  10290. for (const auto& [i, gl, gr] : golden)
  10291. CHECK (std::abs (out.l[(std::size_t) i] - gl) < 2e-6f && std::abs (out.r[(std::size_t) i] - gr) < 2e-6f);
  10292. };
  10293. // IIR crossovers, limiter on, high band compressing, mid bypassed, low compressing, +2 dB output
  10294. 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 }));
  10295. CHECK (oa.latency == 0);
  10296. check (oa,
  10297. { { 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 } });
  10298. // linear phase (FIR, 52 frames of latency), overlapping mid transitions, low band off, limiter off
  10299. 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 }));
  10300. CHECK (ob.latency == 52);
  10301. check (ob,
  10302. { { 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 } });
  10303. // linear phase, hard compression everywhere, limiter on, mid crossovers crossed (the FIR mid band is silent)
  10304. 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 }));
  10305. CHECK (oc.latency == 52);
  10306. check (oc,
  10307. { { 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 } });
  10308. // the slot's parameters carry FL's raw controls exactly; after the project file's 4-decimal rounding the
  10309. // effect built with FL's state still uses the exact controls
  10310. const auto st = multiband ({ 42598, 1, 1, 0, 15604, 25782, 53524, 32768, 11, 298, 51118, 0, 14994, 5037, 24736, 55755, 26870, 221,
  10311. 1090, 49807, 0, 5037, 21845, 47074, 32768, 550, 1090, 40632 });
  10312. const auto parsed = lynx::flmb::parse (st);
  10313. CHECK (parsed && parsed->c[4] == 15604);
  10314. lynx::EffectParams p {};
  10315. lynx::flmb::toParams (*parsed, p.data());
  10316. for (int i = 0; i < lynx::flmb::controls; ++i)
  10317. 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]);
  10318. lynx::EffectParams rounded = p;
  10319. for (auto& v : rounded)
  10320. v = std::round (v * 10000.0f) / 10000.0f;
  10321. std::vector<float> l1 (inL), r1 (inR), l2 (inL), r2 (inR);
  10322. auto exact = lynx::flmb::make();
  10323. auto fromState = lynx::flmb::make (st);
  10324. exact->prepare (44100.0);
  10325. fromState->prepare (44100.0);
  10326. for (int s0 = 0; s0 < 4096; s0 += 512)
  10327. {
  10328. exact->process (l1.data() + s0, r1.data() + s0, 512, p, 120.0);
  10329. fromState->process (l2.data() + s0, r2.data() + s0, 512, rounded, 120.0);
  10330. }
  10331. CHECK (l1 == l2 && r1 == r2);
  10332. CHECK (lynx::flmb::paramText (4, p[4]) == "6302 Hz");
  10333. CHECK (lynx::flmb::paramText (3, 0.5f) == "off");
  10334. // the FL import builds the exact effect with FL's state kept
  10335. const auto slot = lynx::effectFromFl ("Fruity Multiband Compressor", st);
  10336. CHECK (slot && slot->kind == lynx::EffectKind::fruityMultiband && slot->state == st && slot->params[4] == p[4]);
  10337. // unknown versions are rejected
  10338. CHECK (! lynx::flmb::parse ({ 1, 0, 0, 0 }));
  10339. }
  10340. // REA_rodada54: input of the stereo-effect goldens (the installation DLLs' output for the same input, EXEC)
  10341. static std::pair<std::vector<float>, std::vector<float>> stereoFxInput()
  10342. {
  10343. std::vector<float> l (4096), r (4096);
  10344. for (int i = 0; i < 4096; ++i)
  10345. {
  10346. 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));
  10347. r[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / 44100.0));
  10348. }
  10349. return { l, r };
  10350. }
  10351. static std::vector<std::uint8_t> i32State (std::initializer_list<int> words)
  10352. {
  10353. std::vector<std::uint8_t> d;
  10354. for (const auto w : words)
  10355. for (int b = 0; b < 4; ++b)
  10356. d.push_back ((std::uint8_t) ((std::uint32_t) w >> (8 * b)));
  10357. return d;
  10358. }
  10359. static void checkStereoFx (lynx::EffectSlot slot, const std::vector<std::tuple<int, float, float>>& golden)
  10360. {
  10361. auto [l, r] = stereoFxInput();
  10362. auto fx = lynx::makeEffect (slot);
  10363. fx->prepare (44100.0);
  10364. for (int s = 0; s < 4096; s += 512)
  10365. fx->process (l.data() + s, r.data() + s, 512, slot.params, 120.0);
  10366. for (const auto& [i, gl, gr] : golden)
  10367. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  10368. }
  10369. TEST (stereo_shaper_engine)
  10370. {
  10371. // Fruity Stereo Shaper (REA_rodada54 §2): matrix with cross terms, delay on R, all-pass on L, effects post / pre
  10372. auto slot = lynx::effectFromFl ("Fruity Stereo Shaper", i32State ({ 0, -3050, 12800, 9000, 2000, 1408, -2496, 0, 1 }));
  10373. CHECK (slot && slot->kind == lynx::EffectKind::stereoShaper);
  10374. if (slot)
  10375. checkStereoFx (*slot, { { 100, 0.0438031591f, 0.0f }, { 1000, 1.11109531f, -0.781641483f }, { 2047, -0.73191303f, 0.483852893f },
  10376. { 3000, 0.950346172f, -0.769737065f }, { 4095, 0.251555145f, 0.153306276f } });
  10377. slot = lynx::effectFromFl ("Fruity Stereo Shaper", i32State ({ 0, 0, 12800, 12800, 0, -2656, 2184, 0, 0 }));
  10378. CHECK (slot.has_value());
  10379. if (slot)
  10380. checkStereoFx (*slot, { { 100, 0.0f, -0.104282036f }, { 1000, 0.0f, 1.08441091f }, { 2047, 0.463494211f, -0.93754214f },
  10381. { 3000, -1.49942112f, 0.820726812f }, { 4095, -1.31784308f, -0.057449434f } });
  10382. // controls round-trip through the slot parameters; the Patcher's difference bus is the same engine flagged
  10383. const auto st = lynx::stereofx::parseStereoShaper (i32State ({ 0, -3050, 12800, 9000, 2000, 1408, -2496, 2, 1 }));
  10384. CHECK (st.has_value());
  10385. if (st)
  10386. {
  10387. lynx::EffectParams p {};
  10388. lynx::stereofx::stereoShaperToParams (*st, p.data());
  10389. const auto back = lynx::stereofx::stereoShaperFromParams (p.data());
  10390. CHECK (back.rightToLeft == -3050 && back.left == 12800 && back.right == 9000 && back.leftToRight == 2000 && back.delay == 1408
  10391. && back.phase == -2496 && back.post == 1);
  10392. const auto head = lynx::patcherBranchHead ("Fruity Stereo Shaper", i32State ({ 0, -3050, 12800, 9000, 2000, 1408, -2496, 2, 1 }), 2);
  10393. CHECK (head.valid && head.slot && head.slot->kind == lynx::EffectKind::stereoShaper && head.slot->params[7] == 1.0f);
  10394. }
  10395. CHECK (! lynx::stereofx::parseStereoShaper ({ 1, 2, 3 }));
  10396. }
  10397. TEST (frequency_splitter_engine)
  10398. {
  10399. // Frequency Splitter (REA_rodada62): our own states through Frequency Splitter_x64.dll from its creation (gains
  10400. // ramping up from 0, FFT response faded in), blocks of 512 at 44.1 kHz; every band sent to its own destination
  10401. // (1 low, 2 mid, 3 high). IIR modes are bit-exact; the FFT mode's residual is the plugin's single-precision FFT.
  10402. using Gold = std::vector<std::tuple<int, float, float>>;
  10403. struct Case
  10404. {
  10405. std::vector<int> words;
  10406. int mute, latency;
  10407. std::array<Gold, 4> gold; // low, mid, high sends, summed output
  10408. };
  10409. const std::vector<Case> cases {
  10410. { { 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
  10411. { { 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
  10412. { { 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
  10413. { { 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
  10414. { { 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
  10415. { { 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
  10416. { { 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
  10417. };
  10418. auto [l, r] = stereoFxInput();
  10419. for (const auto& c : cases)
  10420. {
  10421. auto words = c.words;
  10422. words.insert (words.begin(), 4);
  10423. for (const int w : { 0, 0, 0, c.mute })
  10424. words.push_back (w);
  10425. std::vector<std::uint8_t> st;
  10426. for (const auto w : words)
  10427. for (int b = 0; b < 4; ++b)
  10428. st.push_back ((std::uint8_t) ((std::uint32_t) w >> (8 * b)));
  10429. st.push_back (0);
  10430. st.resize (st.size() + 24, 0);
  10431. const auto s = lynx::fsplit::parseState (st);
  10432. CHECK (s.has_value());
  10433. if (! s)
  10434. continue;
  10435. lynx::fsplit::Engine e;
  10436. for (int i = 0; i < lynx::fsplit::engineParams; ++i)
  10437. e.setParameter (i, s->params[(std::size_t) i]);
  10438. e.prepare (44100.0);
  10439. e.reset (false);
  10440. std::array<std::vector<float>, 8> o;
  10441. for (auto& v : o)
  10442. v.assign (4096, 0.0f);
  10443. for (int b = 0; b < 4096; b += 512)
  10444. {
  10445. float* out[8];
  10446. for (int k = 0; k < 8; ++k)
  10447. out[k] = o[(std::size_t) k].data() + b;
  10448. e.process (l.data() + b, r.data() + b, out, 512);
  10449. }
  10450. CHECK (e.latencySamples() == c.latency);
  10451. const auto tol = c.latency > 0 ? 2e-5f : 2e-6f;
  10452. for (int k = 0; k < 4; ++k)
  10453. for (const auto& [i, gl, gr] : c.gold[(std::size_t) k])
  10454. CHECK (std::abs (o[(std::size_t) (2 * k)][(std::size_t) i] - gl) <= tol * std::max (1.0f, std::abs (gl))
  10455. && std::abs (o[(std::size_t) (2 * k + 1)][(std::size_t) i] - gr) <= tol * std::max (1.0f, std::abs (gr)));
  10456. // the mixer effect: the summed output (FL's main output) of an instance that has been running, and a
  10457. // Patcher bus = the bands sent to it
  10458. auto slot = lynx::effectFromFl ("Frequency Splitter", st);
  10459. CHECK (slot && slot->kind == lynx::EffectKind::frequencySplitter);
  10460. const auto head = lynx::patcherBranchHead ("Frequency Splitter", st, 3);
  10461. CHECK (head.valid && head.slot && head.slot->params[lynx::fsplit::outputHigh] == 1.0f
  10462. && head.slot->params[lynx::fsplit::outputLow] == 0.0f);
  10463. if (slot && head.slot)
  10464. {
  10465. lynx::fsplit::Engine settled;
  10466. for (int i = 0; i < lynx::fsplit::engineParams; ++i)
  10467. settled.setParameter (i, s->params[(std::size_t) i]);
  10468. settled.prepare (44100.0);
  10469. settled.reset (true);
  10470. std::vector<float> sl (4096), sr (4096), hl (4096), hr (4096);
  10471. float* out[8] { nullptr, nullptr, nullptr, nullptr, hl.data(), hr.data(), sl.data(), sr.data() };
  10472. settled.process (l.data(), r.data(), out, 4096);
  10473. const lynx::EffectSlot* both[2] { &*slot, &*head.slot };
  10474. for (const auto* p : both)
  10475. {
  10476. auto fx = lynx::makeEffect (*p);
  10477. fx->prepare (44100.0);
  10478. auto ml = l, mr = r;
  10479. for (int b = 0; b < 4096; b += 300)
  10480. fx->process (ml.data() + b, mr.data() + b, std::min (300, 4096 - b), p->params, 120.0);
  10481. const auto& wl = p == &*slot ? sl : hl;
  10482. const auto& wr = p == &*slot ? sr : hr;
  10483. CHECK (ml == wl && mr == wr);
  10484. CHECK (fx->latencySamples() == c.latency);
  10485. }
  10486. }
  10487. }
  10488. CHECK (! lynx::patcherBranchHead ("Frequency Splitter", std::vector<std::uint8_t> (0x58, 0), 2).valid); // no band sent to bus 2
  10489. CHECK (lynx::fsplit::paramText (2, 3.0f / 7.0f) == "24 dB/oct" && lynx::fsplit::paramText (11, 0.5f) == "0.0 dB");
  10490. CHECK (lynx::fsplit::paramText (0, 0.0f) == "30.0 Hz" && lynx::fsplit::paramText (4, 1.0f) == "FFT");
  10491. }
  10492. TEST (stereo_enhancer_transient_spreader_engines)
  10493. {
  10494. // REA_rodada54 §3–5: own states against the installation's DLLs (goldens = the DLL's output, EXEC)
  10495. auto se = lynx::effectFromFl ("Fruity Stereo Enhancer", i32State ({ -18, 300, -54, -148, 1, 1 }));
  10496. CHECK (se && se->kind == lynx::EffectKind::stereoEnhancer);
  10497. if (se)
  10498. checkStereoFx (*se, { { 100, 0.0f, 0.00309863733f }, { 1000, 0.0101111531f, -0.110266283f }, { 2047, -0.688752949f, -1.93998075f },
  10499. { 3000, -0.283846498f, -0.739818454f }, { 4095, 0.191147864f, 2.70425582f } });
  10500. auto tpState = i32State ({ 1, 138, 116, 64, 90, 2, 0, 70, 100, 1 });
  10501. tpState.push_back (5);
  10502. for (const auto w : i32State ({ 0, 50 }))
  10503. tpState.push_back (w);
  10504. auto tp = lynx::effectFromFl ("Transient Processor", tpState);
  10505. CHECK (tp && tp->kind == lynx::EffectKind::transientProcessor);
  10506. if (tp)
  10507. checkStereoFx (*tp, { { 100, 0.0568947345f, 0.124077775f }, { 1000, -0.326598823f, -0.193128616f }, { 2047, 0.612462878f, -0.0517415144f },
  10508. { 3000, -0.535302937f, -0.446303189f }, { 4095, -0.634670973f, 0.316044629f } });
  10509. auto spState = i32State ({ 0x10003, 7861, 3215, 106000, 1, 1, 0 });
  10510. spState.push_back (1);
  10511. spState.push_back (1);
  10512. auto sp = lynx::effectFromFl ("Spreader", spState);
  10513. CHECK (sp && sp->kind == lynx::EffectKind::spreader);
  10514. if (sp)
  10515. checkStereoFx (*sp, { { 100, 0.11841587f, -0.0958916694f }, { 1000, 1.40852344f, 0.087452054f }, { 2047, 1.07267976f, -2.02493715f },
  10516. { 3000, -0.781082511f, 0.811192274f }, { 4095, -1.80673707f, 0.89967829f } });
  10517. // unknown layouts keep the earlier fallback; FL automation maps one to one
  10518. CHECK (! lynx::stereofx::parseSpreader (i32State ({ 0x10001, 1, 2, 3, 4, 5 })));
  10519. CHECK (! lynx::stereofx::parseTransientProcessor (i32State ({ 2, 1, 2, 3, 4, 5, 6, 7, 8, 9 })));
  10520. const auto map = lynx::effectParamFromFl ("Transient Processor", 6);
  10521. CHECK (map && map->param == 6 && map->convert (0.25f) == 0.25f);
  10522. }
  10523. TEST (fruity_dynamics_engines)
  10524. {
  10525. // REA_rodada48: Fruity Limiter / Fruity Compressor, states of our own against the installation's DLLs
  10526. // (goldens = the DLL's output for the same input, EXEC; blocks of 512 at 44.1 kHz)
  10527. std::vector<float> inL (4096), inR (4096), side (4096);
  10528. for (int i = 0; i < 4096; ++i)
  10529. {
  10530. 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));
  10531. inR[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / 44100.0));
  10532. side[(std::size_t) i] = (float) (0.9 * std::sin (2.0 * 3.141592653589793 * 55.0 * i / 44100.0));
  10533. }
  10534. auto put = [] (std::vector<std::uint8_t>& b, std::int32_t v) {
  10535. std::uint8_t q[4];
  10536. std::memcpy (q, &v, 4);
  10537. b.insert (b.end(), q, q + 4);
  10538. };
  10539. auto limiterState = [&] (std::array<int, 18> p, int k = 0, int exact = 0) {
  10540. std::vector<std::uint8_t> b;
  10541. put (b, 7);
  10542. for (auto v : p)
  10543. put (b, v);
  10544. b.resize (0xA4, 0);
  10545. put (b, k);
  10546. b.push_back ((std::uint8_t) exact);
  10547. return b;
  10548. };
  10549. auto compressorState = [&] (int version, std::array<int, 6> p, int flags, int k = 0) {
  10550. std::vector<std::uint8_t> b;
  10551. put (b, version);
  10552. for (auto v : p)
  10553. put (b, v);
  10554. put (b, flags);
  10555. if (version > 2)
  10556. put (b, k);
  10557. return b;
  10558. };
  10559. struct Out { std::vector<float> l, r; int latency = 0; };
  10560. auto run = [&] (const lynx::EffectSlot& slot, bool sidechain) {
  10561. auto fx = lynx::makeEffect (slot);
  10562. fx->prepare (44100.0);
  10563. Out o { inL, inR };
  10564. for (int s = 0; s < 4096; s += 512)
  10565. {
  10566. if (sidechain)
  10567. fx->setSidechain (side.data() + s, side.data() + s);
  10568. fx->process (o.l.data() + s, o.r.data() + s, 512, slot.params, 120.0);
  10569. }
  10570. o.latency = fx->latencySamples();
  10571. return o;
  10572. };
  10573. auto check = [] (const Out& out, const std::vector<std::tuple<int, float, float>>& golden) {
  10574. for (const auto& [i, gl, gr] : golden)
  10575. CHECK (std::abs (out.l[(std::size_t) i] - gl) < 2e-6f && std::abs (out.r[(std::size_t) i] - gr) < 2e-6f);
  10576. };
  10577. auto limiter = [] (const std::vector<std::uint8_t>& st) {
  10578. auto slot = lynx::effectFromFl ("Fruity Limiter", st);
  10579. CHECK (slot && slot->kind == lynx::EffectKind::fruityLimiter);
  10580. return *slot;
  10581. };
  10582. // look-ahead limiter: +4 dB gain into a −2.5 dB ceiling, 2 ms attack (88 samples of latency); bit-identical
  10583. 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);
  10584. CHECK (a.latency == 88);
  10585. check (a, { { 100, 0.0176312141f, 0.621841073f }, { 1000, 0.401142687f, -0.442239583f }, { 2047, -0.19392401f, 0.499310732f },
  10586. { 3000, 0.22835508f, -0.267458469f }, { 4095, -0.089728713f, -0.107289009f } });
  10587. // compressor section with a positive knee, 2 compressor curve stages and soft saturation (port within −143 dB)
  10588. check (run (limiter (limiterState ({ 1000, 900, 1000, 0, 3, 5000, 3, 2882, 400, 600, 400, 1115, 6666, 2, 650, 1000, 0, 4187 })), false),
  10589. { { 100, -0.00208989787f, 0.0100310864f }, { 1000, -0.0545219742f, -0.0651496574f }, { 2047, 0.177322432f, -0.230846375f },
  10590. { 3000, -0.159136176f, -0.183688983f }, { 4095, -0.300143838f, 0.299418598f } });
  10591. // sidechained compressor (side input 1) with a negative knee, noise gate and the exact-ceiling option
  10592. 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),
  10593. { { 100, -0.00186592329f, 0.00895605329f }, { 1000, -0.0375732854f, -0.0448972471f }, { 2047, 0.124697573f, -0.162336946f },
  10594. { 3000, -0.111069582f, -0.128206298f }, { 4095, -0.218446419f, 0.21791859f } });
  10595. auto compressor = [] (const std::vector<std::uint8_t>& st) {
  10596. auto slot = lynx::effectFromFl ("Fruity Compressor", st);
  10597. CHECK (slot && slot->kind == lynx::EffectKind::fruityCompressor);
  10598. return *slot;
  10599. };
  10600. // Vintage type, look-ahead flag, side input 1: bit-identical
  10601. const auto ca = run (compressor (compressorState (3, { -200, 40, 30, 50, 300, 2 }, 1, 1)), true);
  10602. CHECK (ca.latency == 64);
  10603. check (ca, { { 100, 0.0795554966f, -0.230549783f }, { 1000, 0.483903229f, 0.0241768137f }, { 2047, -0.596430957f, -0.441769004f },
  10604. { 3000, 0.597724319f, -0.18129006f }, { 4095, 0.607042372f, 0.435067475f } });
  10605. // Soft/R, version 2 without the flag (the output is still 64 frames late, but not reported): bit-identical
  10606. const auto cb = run (compressor (compressorState (2, { -150, 80, 0, 150, 100, 7 }, 0)), false);
  10607. CHECK (cb.latency == 0);
  10608. check (cb, { { 100, 0.0813739002f, -0.235819489f }, { 1000, 0.264188439f, 0.0131994039f }, { 2047, -0.260424435f, -0.192893147f },
  10609. { 3000, 0.251500547f, -0.0762802213f }, { 4095, 0.252972007f, 0.181305125f } });
  10610. // the slot's parameters carry FL's raw controls exactly (and survive the 4-decimal project file)
  10611. const auto st = limiterState ({ 1261, 995, 1303, 1115, 3, 5000, 3, 2882, 264, 244, -590, 1115, 6666, 1, 650, 1000, 0, 4187 }, 1);
  10612. const auto parsed = lynx::fldyn::parseLimiter (st);
  10613. CHECK (parsed && parsed->sidechain == 1 && ! parsed->exactCeiling);
  10614. const auto slot = limiter (st);
  10615. for (int i = 0; i < lynx::fldyn::limiterControls; ++i)
  10616. {
  10617. const auto rounded = std::round (slot.params[(std::size_t) i] * 10000.0f) / 10000.0f;
  10618. 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]);
  10619. }
  10620. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityLimiter, 3, slot.params[3], 120.0) == "2.0 ms");
  10621. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityCompressor, 5, 2.0f / 7.0f, 120.0) == "Vintage");
  10622. // unreadable states keep the old approximation
  10623. CHECK (lynx::effectFromFl ("Fruity Limiter", { 7, 0, 0, 0 }).has_value() == false
  10624. || lynx::effectFromFl ("Fruity Limiter", { 7, 0, 0, 0 })->kind != lynx::EffectKind::fruityLimiter);
  10625. CHECK (! lynx::fldyn::parseCompressor ({ 4, 0, 0, 0 }));
  10626. }
  10627. TEST (maximus_engine)
  10628. {
  10629. // REA_rodada40: the engine on its own (MaximusProcessor), states of our own against the installation's DLLs
  10630. // (goldens = the DLL's output for the same input, EXEC), and the containers of the mixer effect and Vocodex
  10631. std::vector<float> inL (4096), inR (4096);
  10632. for (int i = 0; i < 4096; ++i)
  10633. {
  10634. 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));
  10635. inR[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / 44100.0));
  10636. }
  10637. auto run = [&] (std::shared_ptr<const lynx::hlfx::MaximusPreset> preset, int mix) {
  10638. lynx::hlfx::MaximusProcessor m;
  10639. m.prepare (std::move (preset), 44100.0, mix);
  10640. std::vector<float> l (inL), r (inR);
  10641. for (int s = 0; s < 4096; s += 512)
  10642. m.process (l.data() + s, r.data() + s, 512);
  10643. return std::make_pair (l, r);
  10644. };
  10645. using Env = std::vector<std::array<double, 4>>;
  10646. const Env def { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 } };
  10647. const Env knee { { 0.0, 0.0, 0.0, 0 }, { 0.35, 0.35, 0.0, 0 }, { 0.65, 0.6, 0.36, 7 } };
  10648. const Env t0 { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.14, 0 }, { 0.5, 0.5, 0.0, 0 } };
  10649. const Env m0 { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 }, { 0.5, 0.6, 0.085, 0 } };
  10650. 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,
  10651. int sus = 2882, int rms = 0) { return std::array<int, 13> { mode, pre, post, sep, sat, ceil, att, rel2, 2, 5000, 3, sus, rms }; };
  10652. auto check = [] (const std::pair<std::vector<float>, std::vector<float>>& out, const std::vector<std::tuple<int, float, float>>& golden) {
  10653. for (const auto& [i, gl, gr] : golden)
  10654. CHECK (std::abs (out.first[(std::size_t) i] - gl) < 2e-6f && std::abs (out.second[(std::size_t) i] - gr) < 2e-6f);
  10655. };
  10656. // RMS detectors, saturation mode B, a 5 Hz low cut, 12 dB high split (SG_x64.dll: port within −152 dB)
  10657. auto st3 = maximusTestState ({ 19800, 1, 48065, 0, 1115, 1000, 0 },
  10658. { band (0, 1500, 1000, 0, 400, 900, 1115, 0, 2882, 1), band (0, 1340, 1000, 0, 0, 1000, 1115, 0, 1570, 1),
  10659. band (1, 1000, 1200, 0, 1000, 1000), band (0, 1000, 1000, 0, 250, 1000, 1115, 4464, 2882, 1) },
  10660. { def, def, knee, def, t0, def, m0, def }, 0);
  10661. const auto p3 = lynx::hlfx::makeMaximusPreset (st3);
  10662. CHECK (p3 != nullptr);
  10663. check (run (p3, 1000), { { 100, 0.0f, 0.0f }, { 1000, 0.87061065f, 0.64004499f }, { 2047, -0.97266865f, 0.60940075f },
  10664. { 3000, 0.96658099f, 0.9250561f }, { 4095, 0.99094611f, -0.964059f } });
  10665. // the same with solo on (UI byte 0x1EF) and the low band selected (0x1E0): bit-identical to SG_x64.dll
  10666. auto solo = st3;
  10667. solo[0x1ef] = 1;
  10668. std::memset (solo.data() + 0x1e0, 0, 4);
  10669. check (run (lynx::hlfx::makeMaximusPreset (solo), 1000),
  10670. { { 100, 3.2922967e-06f, 0.00036916698f }, { 1000, 0.15653539f, -0.047913823f }, { 2047, -0.17336531f, 0.019099623f },
  10671. { 3000, 0.17185682f, -0.017173048f }, { 4095, 0.16714551f, 0.019473195f } });
  10672. // the legacy linear-phase FIR band split (UI byte 0x1FD; SG_x64.dll: port within −148 dB)
  10673. auto fir = st3;
  10674. fir[0x1fd] = 1;
  10675. check (run (lynx::hlfx::makeMaximusPreset (fir), 1000),
  10676. { { 100, 0.0f, 0.0f }, { 1000, 0.97986746f, 0.18555209f }, { 2047, -0.98930448f, -0.95970845f },
  10677. { 3000, 0.98856646f, -0.44592777f }, { 4095, 0.99069679f, 0.94382554f } });
  10678. // Soundgoodizer_x64.dll's own build with a v4 file (its presets' layout): a copy of the round-36 state without
  10679. // oversampling, through a byte-identical copy of the installation's DLL with this file as its preset (EXEC)
  10680. const auto st2 = maximusTestState ({ 19800, 1, 48065, 1, 891, 600, 32768 },
  10681. { band (1, 1264, 1296, 500, -500, 1200), band (0, 1340, 1000, -348, 0, 1000, 1115, 0, 1570),
  10682. band (0, 1364, 1152, 0, 0, 1000, 1115, 0, 1186), band (0, 1000, 1000, 0, -196, 1000, 1115, 4464, 1538) },
  10683. { def, def, knee, def, t0, def, m0, def }, 0);
  10684. std::vector<std::uint8_t> v4 { 4, 0, 0, 0 };
  10685. v4.insert (v4.end(), st2.begin() + 4, st2.begin() + 0x1f5);
  10686. v4.insert (v4.end(), st2.begin() + 0x1fe, st2.end());
  10687. const auto p4 = lynx::hlfx::makeMaximusPreset (v4, lynx::hlfx::MaximusBuild::soundgoodizer);
  10688. CHECK (p4 != nullptr);
  10689. check (run (p4, 600), { { 100, 0.0f, 0.0f }, { 1000, -0.60926896f, -0.6652832f }, { 2047, 0.40872875f, 0.36375371f },
  10690. { 3000, -0.41941205f, -0.61312866f }, { 4095, -0.12878418f, 0.27944946f } });
  10691. CHECK (lynx::hlfx::makeMaximusPreset (st2, lynx::hlfx::MaximusBuild::soundgoodizer) != nullptr); // v7: also read by that build
  10692. // the mixer effect's container and the effect itself
  10693. lynx::maximusfx::Blob blob;
  10694. blob.build = lynx::hlfx::MaximusBuild::soundgoodizer;
  10695. blob.mix = 600;
  10696. blob.state = v4;
  10697. const auto packed = lynx::maximusfx::pack (blob);
  10698. const auto back = lynx::maximusfx::unpack (packed);
  10699. CHECK (back && back->build == blob.build && back->mix == 600 && back->state == v4);
  10700. auto slot = lynx::makeEffectSlot (lynx::EffectKind::maximus);
  10701. slot.state = packed;
  10702. auto fx = lynx::makeEffect (slot);
  10703. fx->prepare (44100.0);
  10704. {
  10705. std::vector<float> l (inL), r (inR);
  10706. for (int s = 0; s < 4096; s += 1024) // longer host blocks are split into the engine's 512-frame calls
  10707. fx->process (l.data() + s, r.data() + s, 1024, slot.params, 120.0);
  10708. const auto ref = run (p4, 600);
  10709. CHECK (l == ref.first && r == ref.second);
  10710. }
  10711. const std::vector<std::uint8_t> sgState { 3, 0, 0, 0, 2, 0, 0, 0, 0x78, 2, 0, 0 };
  10712. const auto sg = lynx::maximusfx::parseSoundgoodizer (sgState);
  10713. CHECK (sg && sg->preset == 2 && sg->amount == 632 && std::string (lynx::maximusfx::soundgoodizerPresetFile (2)) == "c.dat");
  10714. CHECK (lynx::soundgoodizerPresetPath (sgState) == "Plugins\\Fruity\\Effects\\Soundgoodizer\\Data\\Presets\\c.dat");
  10715. CHECK (lynx::soundgoodizerExact (sgState, v4).has_value() && ! lynx::soundgoodizerExact (sgState, {}).has_value());
  10716. // without the preset files (no FL installation) the approximation keeps the plugin's state and its name
  10717. {
  10718. const auto approx = lynx::effectFromFl ("Soundgoodizer", sgState);
  10719. CHECK (approx && lynx::isSoundgoodizerApprox (*approx) && lynx::projectcontents::effectSlotName (*approx) == "Soundgoodizer");
  10720. const auto st = approx ? lynx::maximusfx::parseSoundgoodizer (approx->state) : std::nullopt;
  10721. CHECK (st && st->preset == 2 && st->amount == 632 && approx->branches.size() == 1 && approx->branches[0].chain.size() == 2);
  10722. CHECK (! lynx::isSoundgoodizerApprox (lynx::makeEffectSlot (lynx::EffectKind::parallel))); // a Patcher container stays one
  10723. const auto edited = lynx::soundgoodizerApproxSlot (0, 1000);
  10724. CHECK (lynx::isSoundgoodizerApprox (edited) && lynx::maximusfx::parseSoundgoodizer (edited.state)->preset == 0);
  10725. }
  10726. // Vocodex's appended installation data survives a round trip and leaves FL's state untouched
  10727. const std::vector<std::uint8_t> flState { 8, 0, 0, 0, 1, 2, 3 };
  10728. lynx::vocodex::InstallData data;
  10729. data.sgDat = st2;
  10730. data.carrierState = { 9, 9, 9 };
  10731. const auto joined = lynx::vocodex::appendInstallData (flState, data);
  10732. const auto [raw, files] = lynx::vocodex::splitInstallData (joined);
  10733. CHECK (raw == flState && files.sgDat == st2 && files.carrierState == data.carrierState);
  10734. CHECK (lynx::vocodex::splitInstallData (flState).first == flState);
  10735. }
  10736. // ---- Fruity Reeverb 2 (REA_rodada49) --------------------------------------------------------------------------------
  10737. // Goldens: the installation's Fruity Reeverb 2_x64.dll (unmodified, rate set before the state, flushed, blocks of 512)
  10738. // on our own states and input; LYNX's engine is bit-identical to it.
  10739. TEST (reeverb2_engine)
  10740. {
  10741. auto hex = [] (const char* h) {
  10742. std::vector<std::uint8_t> v;
  10743. for (std::size_t i = 0; h[i] != 0 && h[i + 1] != 0; i += 2)
  10744. v.push_back ((std::uint8_t) std::stoi (std::string (h + i, 2), nullptr, 16));
  10745. return v;
  10746. };
  10747. constexpr int N = 8192;
  10748. std::vector<float> inL (N), inR (N);
  10749. std::uint32_t seed = 12345;
  10750. for (int i = 0; i < N; ++i)
  10751. {
  10752. seed = (seed * 1103515245u + 12345u) & 0x7fffffffu;
  10753. const auto nz = (float) ((double) seed / 2147483648.0 - 0.5);
  10754. const auto tri = (float) ((i % 200) - 100) / 100.0f;
  10755. const float g = i < 4000 ? 1.0f : 0.0f;
  10756. inL[(std::size_t) i] = 0.5f * tri * g + (i >= 100 && i < 300 ? nz * 0.6f : 0.0f);
  10757. inR[(std::size_t) i] = 0.25f * tri * g;
  10758. }
  10759. struct Golden
  10760. {
  10761. int i;
  10762. float l, r;
  10763. };
  10764. auto run = [&] (const std::vector<std::uint8_t>& state, double bpm, const std::vector<Golden>& gold) {
  10765. const auto slot = lynx::reeverb2FromFl (state);
  10766. CHECK (slot.has_value() && slot->kind == lynx::EffectKind::reeverb2);
  10767. if (! slot)
  10768. return;
  10769. auto fx = lynx::makeEffect (*slot);
  10770. fx->prepare (44100.0);
  10771. fx->reset();
  10772. std::vector<float> l (inL), r (inR);
  10773. for (int s = 0; s < N; s += 512)
  10774. fx->process (l.data() + s, r.data() + s, 512, slot->params, bpm);
  10775. for (const auto& g : gold)
  10776. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  10777. };
  10778. // modulation, early reflections, negative separation, filters, predelay
  10779. run (hex ("112700004b000000780000001e0000004600000055000000190000003c0000009600000090010000e2ffffff6e000000640000008c00000084030000b00400000000"),
  10780. 120.0, { { 777, 0.263778746f, 0.131889373f }, { 1555, 0.187972173f, 0.0587911084f }, { 3333, 0.10398978f, 0.0566705838f },
  10781. { 5000, 0.087177977f, -0.0197417438f }, { 8191, 0.0769432634f, -0.282023013f } });
  10782. // Mid/Side input, tempo-synced predelay (27 ticks at 128 BPM), positive separation
  10783. run (hex ("112700004b000000280000001b00000032000000640000000f0000002800000064000000f401000028000000800000000000000040000000ac020000000000000101"),
  10784. 128.0, { { 777, 0.38499999f, 0.192499995f }, { 1555, 0.275000006f, 0.137500003f }, { 3333, 0.164420277f, 0.0826604739f },
  10785. { 5000, -0.00861243438f, -0.00140450173f }, { 8191, -0.00873835851f, 0.00888271444f } });
  10786. // version 10000 (no modulation controls), everything off or at its extreme, dry 0
  10787. run (hex ("1027000013000000dd000000000000006400000000000000c8000000dd00000014000000d00700004000000000000000a0000000a00000000000"),
  10788. 120.0, { { 777, 0.0f, 0.0f }, { 1555, 0.0f, 0.0f }, { 3333, 0.0632086769f, 0.0632086769f }, { 5000, -0.108072646f, -0.108072646f },
  10789. { 8191, 0.144686908f, 0.144686908f } });
  10790. // the parameters map back to FL's integers; unsupported states fall back to the approximation
  10791. lynx::reeverb2::Settings s;
  10792. s.p = { 3000, 5, 384, 1, 0, 200, 221, 20, 2000, -64, 160, 0, 160, 2048, 1 };
  10793. s.tempoPredelay = true;
  10794. lynx::EffectParams p {};
  10795. lynx::reeverb2::toParams (s, p);
  10796. CHECK (lynx::reeverb2::fromParams (p) == s);
  10797. CHECK (! lynx::reeverb2FromFl ({ 0x11, 0x27, 0, 0, 1, 0 }).has_value());
  10798. CHECK (! lynx::reeverb2FromFl ({ 0x12, 0x27, 0, 0 }).has_value());
  10799. const auto approx = lynx::effectFromFl ("Fruity Reeverb 2", { 0x11, 0x27, 0, 0 }, 120.0);
  10800. CHECK (! approx || approx->kind != lynx::EffectKind::reeverb2);
  10801. CHECK (lynx::reeverb2ParamText (0, 0.0f) == "off" && lynx::reeverb2ParamText (5, 1.0f) == "20.0 s");
  10802. // Fruity Reeverb (the first version): Fruity Reeverb_x64.dll goldens on our own states (denormals flushed, as the
  10803. // plugin runs and as LYNX renders)
  10804. lynx::ScopedFlushDenormals ftz;
  10805. auto run1 = [&] (const std::vector<std::uint8_t>& state, const std::vector<Golden>& gold) {
  10806. const auto slot = lynx::reeverb1FromFl (state);
  10807. CHECK (slot.has_value() && slot->kind == lynx::EffectKind::reeverb1);
  10808. if (! slot)
  10809. return;
  10810. auto fx = lynx::makeEffect (*slot);
  10811. fx->prepare (44100.0);
  10812. std::vector<float> l (inL), r (inR);
  10813. for (int s = 0; s < N; s += 512)
  10814. fx->process (l.data() + s, r.data() + s, 512, slot->params, 120.0);
  10815. for (const auto& g : gold)
  10816. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  10817. };
  10818. run1 (hex ("01000000204e0000204e000025000000409c000050c300000a00000030750000a861000050c3000060ea0000"),
  10819. { { 777, 0.293731689f, 0.146865845f }, { 1555, 0.20980835f, 0.104904175f }, { 3333, 0.0998796821f, 0.0754205957f },
  10820. { 5000, -0.0994476378f, 0.0116700521f }, { 8191, -0.141061321f, 0.0601170398f } });
  10821. run1 (hex ("0100000000000000000001000000000000000100000001002800000000000100000001000000000000000100"),
  10822. { { 777, -0.016678432f, -4.58686594e-31f }, { 1555, 0.0221031085f, 0.0230635051f }, { 3333, 0.0875115693f, 0.1695389f },
  10823. { 5000, -0.111421227f, -0.295867324f }, { 8191, 0.503498077f, 0.190456569f } });
  10824. CHECK (lynx::reeverb1FromFl (std::vector<std::uint8_t> (44, 0)).has_value());
  10825. CHECK (! lynx::reeverb1FromFl (std::vector<std::uint8_t> (40, 0)).has_value());
  10826. CHECK (lynx::reeverb1ParamText (5, 0.0f) == "Brighter" && lynx::reeverb1ParamText (0, 0.0f) == "off");
  10827. }
  10828. // ---- Fruity Love Philter (REA_rodada66) ---------------------------------------------------------------------------------
  10829. TEST (love_philter_engine)
  10830. {
  10831. // goldens: Fruity Love Philter_x64.dll on our own synthetic states (44.1 kHz, blocks of 512, 120 BPM, live mode),
  10832. // bit-identical on the test machine (denormals flushed, as the plugin runs and as LYNX renders)
  10833. lynx::ScopedFlushDenormals ftz;
  10834. auto hex = [] (const char* h) {
  10835. std::vector<std::uint8_t> v;
  10836. for (std::size_t i = 0; h[i] != 0 && h[i + 1] != 0; i += 2)
  10837. v.push_back ((std::uint8_t) std::stoi (std::string (h + i, 2), nullptr, 16));
  10838. return v;
  10839. };
  10840. constexpr int N = 8192;
  10841. std::vector<float> inL (N), inR (N);
  10842. std::uint32_t seed = 12345;
  10843. for (int i = 0; i < N; ++i)
  10844. {
  10845. seed = (seed * 1103515245u + 12345u) & 0x7fffffffu;
  10846. const auto nz = (float) ((double) seed / 2147483648.0 - 0.5);
  10847. const auto tri = (float) ((i % 200) - 100) / 100.0f;
  10848. const float g = i < 4000 ? 1.0f : 0.0f;
  10849. inL[(std::size_t) i] = 0.5f * tri * g + (i >= 100 && i < 300 ? nz * 0.6f : 0.0f);
  10850. inR[(std::size_t) i] = 0.25f * tri * g;
  10851. }
  10852. struct Golden
  10853. {
  10854. int i;
  10855. float l, r;
  10856. };
  10857. auto run = [&] (const std::vector<std::uint8_t>& state, const std::vector<Golden>& gold) {
  10858. const auto slot = lynx::effectFromFl ("Fruity Love Philter", state, 120.0);
  10859. CHECK (slot.has_value() && slot->kind == lynx::EffectKind::lovePhilter);
  10860. if (! slot || slot->kind != lynx::EffectKind::lovePhilter)
  10861. return;
  10862. auto fx = lynx::makeEffect (*slot);
  10863. fx->prepare (44100.0);
  10864. std::vector<float> l (inL), r (inR);
  10865. for (int s = 0; s < N; s += 512)
  10866. {
  10867. fx->setSongPosition (s * 2.0 * 960.0 / 44100.0);
  10868. fx->process (l.data() + s, r.data() + s, 512, slot->params, 120.0);
  10869. }
  10870. for (const auto& g : gold)
  10871. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  10872. };
  10873. // SVF triple with Low/Band/High, a song-locked volume pattern with tension, a cutoff LFO; Lime HP fed by NEXT, panned
  10874. run (hex (
  10875. "03000c00789cedda316ed44018c5f1cfd90b5050234a0e81702a1a6e4045c11d68e7005c00c40928e96810254590380225e552538471ec"
  10876. "d97c314c841e0259cfff91463bf66e7e3b9ac42fdf582e1151961e776abf1f314ce3b3a5d7f6e4fb8bf1b2b6693c5cf56118e6b79ed5fe"
  10877. "b2f6b7b53fadfd41ed8f1379378d9fd77e88a50d57c3c3f597dcd28ee3fcfaadbe9e8fcb24e272d56238fd4019a73e2c5f3074c1ebf732"
  10878. "745a8ee51507670bce99e89cbff9e5076eb9f8be8e378f9b7df3fcfae2dbba37afcb9c0b391b0eb16ec795ff657cfd6a6aefc7cf17178f"
  10879. "f2f9f6f9fafe8769342c7dfabe58e65e62db7f5338fb74fe4f96acafa5e3f8bbf3eb6b75eb1e75068eb3d3b241b95ef2ffc0d67ebc8bb8"
  10880. "b792b65e2ffc9bfa636a64068e9fc3de44f77a7b93adfc6e7170fec661afa17b64038eb34336e81ed980e3ec900dba4736e0383b6483ee"
  10881. "957aee61ed9fe27a4d63194ffd63fdccf44cc7f972fef42408cf6ce0e06c30a7b6711f93fba238387fee50c3e81e75038eb343dda07b64"
  10882. "038eb34336e81e7b0a1c67873d85ee910d38ce0ed9a07b64038eb34336e81ed980e3ec900dba57625ec3d65bcbc7799cd6b83f95ded478"
  10883. "d60267c70ef745758f1a06c7d9a186d13dea061c6787ba41f7c8061c67876cd03df61438ce0e7b0add231b709c1db241f7c8061c67876c"
  10884. "d03db201c7d9211b74afc4bc86adb7968ff338ad717f2abda9f1ac05ce8e1dee8bea1e350c8eb3430da37bd40d38ce0e7583ee910d38ce"
  10885. "0ed9a07bec29709c1df614ba4736e0383b6483ee910d38ce0ed9a07b64038eb34336e85e89790d5b6f2d1fe7715ae3fe547a53e3590b9c"
  10886. "1d3bdc17d53d6a181c67871a46f7a81b709c1dea06dd231b709c1db241f7d853e0383bec29748f6cc07176c806dd231b709c1db241f7c8"
  10887. "061c67876cd0bd12f31ab6de5a3ecee3b4c6fda9f4a6c6b316383b76b82faa7bd43038ce0e358cee5137e0383bd40dba4736e0383b6483"
  10888. "eeb1a7c07176d853e81ed980e3ec900dba4736e0383b6483ee910d38ce0ed9a07b25e6356cbdb57c9cc7698dfb53e94d8d672d7076ec70"
  10889. "5f54f7a861709c1d6a18dda36ec07176a81b748f6cc07176c806dd634f81e3ecb0a7d03db201c7d9211b748f6cc07176c806dd231b709c"
  10890. "1db241f74acc6bd87a6bf9388fd31af7a7d29b1acf5ae0ecd8e1bea8ee51c3e0383bd430ba47dd80e3ec5037e81ed980e3ec900dbac79e"
  10891. "02c7d9614fa17b64038eb34336e81ed980e3ec900dba4736e0383b6483eec54ff7970e90"),
  10892. { { 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 } });
  10893. // Mango alternate triple with drive, waveshaper, Mod X/Y maps (smoothed), follower map on the volume; ×4 oversampling
  10894. run (hex (
  10895. "03000c00789cedda4d8ed33018c671a788151b8ec0013803993dd7e00e6c7d0a24100bae81c40590e00e5d82c46238c1e0c879db7752b9"
  10896. "8b470c1f4ffe963c759af6371e4b79e675945a4aa96b2f4f5b7f56cad45e3e1d4a79be0c5a7bf9f3f57cd7da326e6fb7f3d3b49e5abef6"
  10897. "a1f537adbf2ae7968f4f7c6b8f5b7fb20ca6e951fb19fd7a3bceefde2eedfbf2fa22bfdf5f6fe7fce9bb4d2bd3e9549d973eadbf742adb"
  10898. "7686e25c86f2df51d7cfe0e0fc6de7203a37ef2fbe70ba200f17678ef3fde3b08f572fbe7fddebebd273816cc07173fe4c36dc0eaebdeb"
  10899. "ff981fcabb2c28cedfeb85c4c70befeb97a5fd580b8dcfcbfba773dbdf433d81e3ec3c7466a46bec9e1d457e7e777bedfd1ff5c4d2c806"
  10900. "1c3f87bd86eeb1d7c07176f6b8d7f85d1ed980e3ec900dba4736e0383b6483ee910d38ce0ed9a07bb5f4358c1e2d1fe7715ae3f1544653"
  10901. "fbd65e6fe650b65349363985e3e7705f54f7a861709c1d6a18dda36ec07176a81b748f6cc07176c806dd634f81e3ecb0a7d03db201c7d9"
  10902. "211b748f6cc07176c806dd231b709c1db241f76ae96b183d5a3ecee3b4c6e3a98ca6c6b316383b76b82faa7bd43038ce0e358cee5137e0"
  10903. "383bd40dba4736e0383b6483eeb1a7c07176d853e81ed980e3ec900dba4736e0383b6483ee910d38ce0ed9a07bb5f4358c1e2d1fe7715a"
  10904. "e3f1544653e3590b9c1d3bdc17d53d6a181c67871a46f7a81b709c1dea06dd231b709c1db241f7d853e0383bec29748f6cc07176c806dd"
  10905. "231b709c1db241f7c8061c67876cd0bd5afa1a468f968ff338adf1782aa3a9f1ac05ce8e1dee8bea1e350c8eb3430da37bd40d38ce0e75"
  10906. "83ee910d38ce0ed9a07bec29709c1df614ba4736e0383b6483ee910d38ce0ed9a07b64038eb34336e85e2d7d0da347cbc7799cd6783c95"
  10907. "d1d478d60267c70ef745758f1a06c7d9a186d13dea061c6787ba41f7c8061c67876cd03df61438ce0e7b0add231b709c1db241f7c8061c"
  10908. "67876cd03db201c7d9211b74af96be86d1a3e5e33c4e6b3c9eca686a3c6b81b36387fba2ba470d83e3ec50c3e81e75038eb343dda07b64"
  10909. "038eb34336e81e7b0a1c67873d85ee910d38ce0ed9a07b64038eb34336e81ed980e3ec900dba574b5fc3e8d1f2711ea7351e4f6534359e"
  10910. "b5c0d9b1c37d51dda386c17176a861748fba01c7d9a16ed03db201c7d9211b748f3d058eb3c39e42f7c8061c67876cd03db201c7d9211b"
  10911. "748f6cc07176c806dd2bbf0040771f82"),
  10912. { { 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 } });
  10913. // band-pass alternate dual with FLAT, low shelf, all-pass with a pan LFO and inverted input, CENTER, no oversampling
  10914. run (hex (
  10915. "03000c00789ceddd418ad44018c5f12f3303baf404e2d243083d7b6fe0dab55b5dd64106bc8427f024aea497ed09da8a95225ff75883bc"
  10916. "01092fff82a22b19fb97a2248faf62184b4494a5c7abdadf444cf3f866e9b5bdfff5f970ae2d9636cdad0de7af7da9fd6327623d5ffe32"
  10917. "be5b7afdfe6dfdb85d2ff2443b1ddae7b17edecfe33f173f5fb598d6ab1fe63e2d179886e0fab30c5dcf1d07670bce8de8dc3f3cfac213"
  10918. "37df8fc3e571b72fcf5fdf7c5bf7dabab45c201b70dc9cff930da7c1bd7712efe56d78d40d38ce0e7583ee910d38ce0ed9a07bec29709c"
  10919. "1df614ba4736e0383b6483ee910d38ce0ed9a07b64038eb34336e85e89b686bdf7968ff338adf1782aa3a9f1ae05ce8e1d9e8bea1e350c"
  10920. "8eb3430da37bd40d38ce0e7583ee910d38ce0ed9a07bec29709c1df614ba4736e0383b6483ee910d38ce0ed9a07b64038eb34336e85ea9"
  10921. "e73ed4feaef64fe9cf95a5bf8ef67b2fca727ebeeecb79c0efb5c0c1d9604e6de33926cf457170fedda186d13dea061c6787ba41f7c806"
  10922. "1c67876cd03df61438ce0e7b0add231b709c1db241f7c8061c67876cd03db201c7d9211b74afd473df6aff19eb9ac6322e69fc706eff05"
  10923. "c9bc8e2fda603c95d1d478d70267c74ecfa9c737cc7170ef1e2ffce9cafffeb5fdec6db477a222785ebab5bf731c9ce738d436ba473d81"
  10924. "e3ecf0efacba4736e0383b6483eeb1a7c07176d853e81ed980e3ec900dba4736e0383b6483ee910d38ce0ed9a07b25da1af6de5b3ecee3"
  10925. "b4c6e3a98ca6c63b18383b76782eaa7bd43038ce0e358cee5137e0383bd40dba4736e0383b6483eeb1a7c07176d853e81ed980e3ec900d"
  10926. "ba4736e0383b6483ee910d38ce0ed9a07b25da1af6de5b3ecee3b4c6e3a98ca6c6bb16383b76782eaa7bd43038ce0e358cee5137e0383b"
  10927. "d40dba4736e0383b6483eeb1a7c07176d853e81ed980e3ec900dba4736e0383b6483ee910d38ce0ed9a07b25da1af6de5b3ecee3b4c6e3"
  10928. "a98ca6c6bb16383b76782eaa7bd43038ce0e358cee5137e0383bd40dba4736e0383b6483eeb1a7c07176d853e81ed980e3ec900dba4736"
  10929. "e0383b6483ee910d38ce0ed9a07b25da1af6de5b3ecee3b4c6e3a98ca6c6bb16383b76782eaa7bd43038ce0e358cee5137e0383bd40dba"
  10930. "4736e0383b6483eeb1a7c07176d853e81ed980e3ec900dba4736e0383b6483ee910d38ce0ed9a07bf11b286e0820"),
  10931. { { 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 } });
  10932. // the slot exposes FL's controls; FL indices map both ways; unreadable states keep the old conversion
  10933. CHECK (lynx::philter::flIndexOf (6) == 6 && lynx::philter::flIndexOf (8) == 9 && lynx::philter::flIndexOf (8 + 5 * 7 + 4) == 8 + 44 * 7 + 9);
  10934. for (int i = 0; i < lynx::philter::paramCount; ++i)
  10935. CHECK (lynx::philter::paramOfFlIndex (lynx::philter::flIndexOf (i)) == i);
  10936. CHECK (lynx::philter::paramOfFlIndex (8 + 3) == 48 && lynx::philter::paramOfFlIndex (8 + 12) == -1 && lynx::philter::paramOfFlIndex (8 + 44 * 7 + 11) == -1);
  10937. CHECK (lynx::philter::parse ({ 3, 0, 12, 0, 1, 2, 3, 4 }) == nullptr);
  10938. CHECK (lynx::effectInfo (lynx::EffectKind::lovePhilter).params.size() == (std::size_t) lynx::philter::paramCount);
  10939. CHECK (lynx::philter::paramText (5, 0.0f) == "pattern 1" && lynx::philter::paramText (8 + 3, 0.5f) == "centred");
  10940. // a control edited in LYNX is applied when the effect is built from the slot (the state keeps FL's value)
  10941. {
  10942. const auto stateA = hex (
  10943. "03000c00789cedda316ed44018c5f1cfd90b5050234a0e81702a1a6e4045c11d68e7005c00c40928e96810254590380225e552538471ec"
  10944. "d97c314c841e0259cfff91463bf66e7e3b9ac42fdf582e1151961e776abf1f314ce3b3a5d7f6e4fb8bf1b2b6693c5cf56118e6b79ed5fe"
  10945. "b2f6b7b53fadfd41ed8f1379378d9fd77e88a50d57c3c3f597dcd28ee3fcfaadbe9e8fcb24e272d56238fd4019a73e2c5f3074c1ebf732"
  10946. "745a8ee51507670bce99e89cbff9e5076eb9f8be8e378f9b7df3fcfae2dbba37afcb9c0b391b0eb16ec795ff657cfd6a6aefc7cf17178f"
  10947. "f2f9f6f9fafe8769342c7dfabe58e65e62db7f5338fb74fe4f96acafa5e3f8bbf3eb6b75eb1e75068eb3d3b241b95ef2ffc0d67ebc8bb8"
  10948. "b792b65e2ffc9bfa636a64068e9fc3de44f77a7b93adfc6e7170fec661afa17b64038eb34336e81ed980e3ec900dba4736e0383b6483ee"
  10949. "957aee61ed9fe27a4d63194ffd63fdccf44cc7f972fef42408cf6ce0e06c30a7b6711f93fba238387fee50c3e81e75038eb343dda07b64"
  10950. "038eb34336e81e7b0a1c67873d85ee910d38ce0ed9a07b64038eb34336e81ed980e3ec900dba57625ec3d65bcbc7799cd6b83f95ded478"
  10951. "d60267c70ef745758f1a06c7d9a186d13dea061c6787ba41f7c8061c67876cd03df61438ce0e7b0add231b709c1db241f7c8061c67876c"
  10952. "d03db201c7d9211b74afc4bc86adb7968ff338ad717f2abda9f1ac05ce8e1dee8bea1e350c8eb3430da37bd40d38ce0e7583ee910d38ce"
  10953. "0ed9a07bec29709c1df614ba4736e0383b6483ee910d38ce0ed9a07b64038eb34336e85e89790d5b6f2d1fe7715ae3fe547a53e3590b9c"
  10954. "1d3bdc17d53d6a181c67871a46f7a81b709c1dea06dd231b709c1db241f7d853e0383bec29748f6cc07176c806dd231b709c1db241f7c8"
  10955. "061c67876cd0bd12f31ab6de5a3ecee3b4c6fda9f4a6c6b316383b76b82faa7bd43038ce0e358cee5137e0383bd40dba4736e0383b6483"
  10956. "eeb1a7c07176d853e81ed980e3ec900dba4736e0383b6483ee910d38ce0ed9a07b25e6356cbdb57c9cc7698dfb53e94d8d672d7076ec70"
  10957. "5f54f7a861709c1d6a18dda36ec07176a81b748f6cc07176c806dd634f81e3ecb0a7d03db201c7d9211b748f6cc07176c806dd231b709c"
  10958. "1db241f74acc6bd87a6bf9388fd31af7a7d29b1acf5ae0ecd8e1bea8ee51c3e0383bd430ba47dd80e3ec5037e81ed980e3ec900dbac79e"
  10959. "02c7d9614fa17b64038eb34336e81ed980e3ec900dba4736e0383b6483eec54ff7970e90");
  10960. auto slot = lynx::philter::slotFromFl (stateA);
  10961. CHECK (slot.has_value() && ! lynx::philter::slotFromFl (hex ("0300000078")).has_value());
  10962. if (slot)
  10963. {
  10964. auto render = [&] (const lynx::EffectSlot& sl) {
  10965. auto fx = lynx::makeEffect (sl);
  10966. fx->prepare (44100.0);
  10967. std::vector<float> l (inL), r (inR);
  10968. for (int s = 0; s < 4096; s += 512)
  10969. {
  10970. fx->setSongPosition (s * 2.0 * 960.0 / 44100.0);
  10971. fx->process (l.data() + s, r.data() + s, 512, sl.params, 120.0);
  10972. }
  10973. return l[3333];
  10974. };
  10975. const auto base = render (*slot);
  10976. auto edited = *slot;
  10977. edited.params[8] = 0.1f; // unit 1 cutoff
  10978. CHECK (base == 0.0f || base != render (edited));
  10979. CHECK (base == render (*slot));
  10980. }
  10981. }
  10982. }
  10983. TEST (fruity_vocoder_round66_goldens)
  10984. {
  10985. // REA_rodada66 §6 goldens against the round-70 engine (core/FruityVocoder), through FL's state import and the mixer
  10986. // effect: Fruity Vocoder_x64.dll on our own states (44.1 kHz, blocks of 512): left = modulator, right = carrier.
  10987. // The first state's attack (5 ms) is below the knob range (10…1000): the plugin uses it as it is (REA_rodada78 §1)
  10988. lynx::ScopedFlushDenormals ftz;
  10989. auto hex = [] (const char* h) {
  10990. std::vector<std::uint8_t> v;
  10991. for (std::size_t i = 0; h[i] != 0 && h[i + 1] != 0; i += 2)
  10992. v.push_back ((std::uint8_t) std::stoi (std::string (h + i, 2), nullptr, 16));
  10993. return v;
  10994. };
  10995. constexpr int N = 8192;
  10996. std::vector<float> inL (N), inR (N);
  10997. std::uint32_t seed = 12345;
  10998. for (int i = 0; i < N; ++i)
  10999. {
  11000. seed = (seed * 1103515245u + 12345u) & 0x7fffffffu;
  11001. const auto nz = (float) ((double) seed / 2147483648.0 - 0.5);
  11002. const auto tri = (float) ((i % 200) - 100) / 100.0f;
  11003. const float g = i < 4000 ? 1.0f : 0.0f;
  11004. inL[(std::size_t) i] = 0.5f * tri * g + (i >= 100 && i < 300 ? nz * 0.6f : 0.0f);
  11005. inR[(std::size_t) i] = 0.25f * tri * g;
  11006. }
  11007. struct Golden
  11008. {
  11009. int i;
  11010. float v;
  11011. };
  11012. auto run = [&] (const std::vector<std::uint8_t>& state, const std::vector<Golden>& gold) {
  11013. const auto slot = lynx::effectFromFl ("Fruity Vocoder", state, 120.0);
  11014. CHECK (slot.has_value() && slot->kind == lynx::EffectKind::vocoder);
  11015. if (! slot || slot->kind != lynx::EffectKind::vocoder)
  11016. return;
  11017. auto fx = lynx::makeEffect (*slot);
  11018. fx->prepare (44100.0);
  11019. std::vector<float> l (inL), r (inR);
  11020. for (int s = 0; s < N; s += 512)
  11021. fx->process (l.data() + s, r.data() + s, 512, slot->params, 120.0);
  11022. for (const auto& g : gold)
  11023. CHECK (l[(std::size_t) g.i] == g.v && r[(std::size_t) g.i] == g.v);
  11024. };
  11025. // 32 bands, order 3, inverted carrier bank with a formant shift, fast attack (5 ms, outside the knob range)
  11026. run (hex (
  11027. "02000000200000000300000002000000000000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000"
  11028. "803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f00"
  11029. "00803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803fb80b000050c30000640000000100000044fdffff"
  11030. "78000000050000002c010000000000008c0000003c000000a0000000"),
  11031. { { 777, 0.47870129346847534f }, { 1555, 0.2731437087059021f }, { 2222, -0.41599133610725403f }, { 3333, 0.083399198949337f }, { 3999, 0.7295553088188171f }, { 4100, 0.0871216207742691f } });
  11032. // 8 bands, swapped inputs, per-band levels, modulator level 0
  11033. run (hex (
  11034. "02000000080000000200000002000000010000003f9a99193f3333333fcdcc4c3f6666663f0000803fcdcc8c3f9a99993f1027000060ea"
  11035. "00001e000000000000002c010000500000000f0000003c00000000000000000000001400000080000000"),
  11036. { { 777, 0.07233863323926926f }, { 1555, 0.1183714047074318f }, { 2222, -0.6131120920181274f }, { 3333, 0.18050849437713623f }, { 3999, 0.056527279317379f }, { 4100, 0.05711518973112106f } });
  11037. }
  11038. // ---- Fruity WaveShaper / Fruity Soft Clipper (REA_rodada51) ------------------------------------------------------------
  11039. // States written here from our own values (no FL preset data); goldens from the installation's DLLs (EXEC, blocks of 512)
  11040. namespace
  11041. {
  11042. std::vector<std::uint8_t> waveShaperTestState (int pre, int mix, int post, bool uni, bool center, int os, bool hq, bool smooth)
  11043. {
  11044. std::vector<std::uint8_t> b;
  11045. 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))); };
  11046. 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))); };
  11047. 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))); };
  11048. i32 (0x40003);
  11049. i32 (pre), i32 (mix), i32 (post);
  11050. b.push_back (uni ? 1 : 0), b.push_back (center ? 1 : 0);
  11051. i32 (os);
  11052. 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 },
  11053. { 0.25, 0.9, -0.3f, 3 }, { 0.2, 1.0, 0.5f, 9 } };
  11054. i32 (3), i32 (3), i32 (5);
  11055. for (const auto& p : pts)
  11056. {
  11057. f64 (p.dt), f64 (p.y), f32 (p.t);
  11058. b.push_back ((std::uint8_t) p.mode), b.push_back (0), b.push_back (0), b.push_back (0);
  11059. }
  11060. i32 (0), i32 (-1), i32 (-1), i32 (-1), i32 (-1);
  11061. i32 (1);
  11062. b.push_back (hq ? 1 : 0), b.push_back (smooth ? 1 : 0);
  11063. return b;
  11064. }
  11065. } // namespace
  11066. TEST (fruity_waveshaper_and_soft_clipper)
  11067. {
  11068. std::vector<float> inL (4096), inR (4096);
  11069. for (int n = 0; n < 4096; ++n)
  11070. {
  11071. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  11072. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  11073. }
  11074. struct Gold { int i; float l, r; };
  11075. auto close = [] (float a, float b) { return std::abs (a - b) <= x87Tolerance() * std::max (1.0f, std::abs (b)); };
  11076. // Soft Clipper: bit-exact against Fruity Soft Clipper_x64.dll
  11077. auto clip = [&] (int thr, int post, std::initializer_list<Gold> gold) {
  11078. std::vector<float> l (inL), r (inR);
  11079. for (int s = 0; s < 4096; s += 512)
  11080. {
  11081. lynx::flshape::softClip (l.data() + s, 512, thr, post);
  11082. lynx::flshape::softClip (r.data() + s, 512, thr, post);
  11083. }
  11084. for (const auto& g : gold)
  11085. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  11086. };
  11087. clip (100, 128, { { 100, 0.0f, -0.949052989f }, { 1000, -0.99617976f, 0.872887492f }, { 2047, 0.649999976f, -0.944175363f },
  11088. { 3000, -0.99617976f, -0.699999988f }, { 4095, 0.25f, 0.600000024f } });
  11089. clip (1, 150, { { 100, 0.0f, -1.04588532f }, { 1000, -1.27003884f, 0.930835366f }, { 2047, 0.750244498f, -1.03539562f },
  11090. { 3000, -1.27003884f, -0.790091455f }, { 4095, 0.347655922f, 0.708338201f } });
  11091. clip (64, 140, { { 100, 0.0f, -1.08492827f }, { 1000, -1.21535313f, 0.990323186f }, { 2047, 0.804165959f, -1.0770781f },
  11092. { 3000, -1.21535313f, -0.849189103f }, { 4095, 0.319320858f, 0.754407823f } });
  11093. {
  11094. const std::vector<std::uint8_t> st { 64, 0, 0, 0, 140, 0, 0, 0 };
  11095. const auto slot = lynx::effectFromFl ("Fruity Soft Clipper", st, 120.0);
  11096. CHECK (slot && slot->kind == lynx::EffectKind::fruitySoftClipper);
  11097. if (slot)
  11098. {
  11099. const auto s = lynx::flshape::softClipperFromParams (slot->params.data());
  11100. CHECK (s.threshold == 64 && s.post == 140);
  11101. auto fx = lynx::makeEffect (*slot);
  11102. fx->prepare (44100.0);
  11103. std::vector<float> l (inL), r (inR);
  11104. fx->process (l.data(), r.data(), 4096, slot->params, 120.0);
  11105. CHECK (l[3000] == -1.21535313f && r[4095] == 0.754407823f);
  11106. }
  11107. }
  11108. // WaveShaper: tables (bipolar / unipolar) and the engine on three of our states, live and as FL renders
  11109. const auto stA = waveShaperTestState (150, 100, 120, false, true, 1, true, true); // CENTER, ×2, HQ, smooth
  11110. const auto stB = waveShaperTestState (100, 128, 128, true, false, 0, false, false); // unipolar
  11111. const auto stC = waveShaperTestState (192, 90, 100, false, false, 2, false, false); // ×4
  11112. const auto pA = lynx::flshape::parseWaveShaper (stA);
  11113. CHECK (pA && pA->points.size() == 5 && pA->pre == 150 && pA->center && pA->hq && pA->smooth && pA->oversampling == 1);
  11114. if (pA)
  11115. {
  11116. const auto T = lynx::flshape::shaperTable (pA->points, false);
  11117. CHECK (T.size() == 16386 && T[1] == 3277 && T[1001] == 18824 && T[5001] == 26195 && T[9001] == 11469 && T[12001] == 24614
  11118. && T[16384] == 32767 && T[0] == T[1]);
  11119. const auto U = lynx::flshape::shaperTable (pA->points, true);
  11120. CHECK (U.size() == 32770 && U[1] == -26214 && U[1001] == -6408 && U[5001] == 17073 && U[9001] == 19551 && U[12001] == 14549);
  11121. }
  11122. auto shape = [&] (const std::vector<std::uint8_t>& st, bool render, std::initializer_list<Gold> gold) {
  11123. const auto s = lynx::flshape::parseWaveShaper (st);
  11124. CHECK (s.has_value());
  11125. if (! s)
  11126. return;
  11127. lynx::flshape::WaveShaperEngine e;
  11128. e.setCurve (s->points);
  11129. e.prepare (44100.0);
  11130. e.setControls (s->pre, s->mix, s->post, s->oversampling, s->hq, s->smooth, s->center, s->unipolar, render);
  11131. std::vector<float> l (inL), r (inR);
  11132. for (int i = 0; i < 4096; i += 512)
  11133. e.process (l.data() + i, r.data() + i, 512);
  11134. for (const auto& g : gold)
  11135. CHECK (close (l[(std::size_t) g.i], g.l) && close (r[(std::size_t) g.i], g.r));
  11136. };
  11137. shape (stA, false, { { 100, 0.59108448f, 0.469315082f }, { 1000, 0.284353405f, 1.02621937f }, { 2047, -0.533613563f, 0.117038473f },
  11138. { 3000, 0.22673659f, -1.13572407f }, { 4095, -0.267153352f, 0.822866499f } });
  11139. shape (stA, true, { { 100, 0.794869065f, -0.127601087f }, { 1000, -0.00841622613f, 0.900729597f }, { 2047, 0.450603426f, -0.325943232f },
  11140. { 3000, -0.0530530401f, -0.32303831f }, { 4095, 1.0959363f, 0.946009099f } });
  11141. shape (stB, false, { { 100, -0.206451416f, 0.502471924f }, { 1000, -0.799987793f, 0.761169434f }, { 2047, 0.411743164f, 0.514160156f },
  11142. { 3000, -0.799987793f, 0.596221924f }, { 4095, -0.157653809f, 0.334106445f } });
  11143. shape (stB, true, { { 100, -0.206390381f, 0.502493739f }, { 1000, -0.799987793f, 0.761169434f }, { 2047, 0.411743164f, 0.514144361f },
  11144. { 3000, -0.799987793f, 0.596221924f }, { 4095, -0.157653809f, 0.334106445f } });
  11145. shape (stC, false, { { 100, 0.317797363f, 0.0483714938f }, { 1000, -0.445489585f, -0.533142388f }, { 2047, 0.634525597f, 0.942175806f },
  11146. { 3000, -0.445489585f, 0.514707327f }, { 4095, 0.200391173f, -0.937523127f } });
  11147. shape (stC, true, { { 100, 0.61165309f, -0.0993360579f }, { 1000, 0.0375693105f, 0.667849183f }, { 2047, 0.353716731f, -0.223758236f },
  11148. { 3000, 0.0375693105f, -0.223787636f }, { 4095, 0.843308687f, 0.688474894f } });
  11149. // the mixer effect from the FL import: controls as parameters, the curve in the slot's state; host blocks of any size
  11150. const auto slot = lynx::effectFromFl ("Fruity WaveShaper", stC, 120.0);
  11151. CHECK (slot && slot->kind == lynx::EffectKind::fruityWaveShaper && slot->state == stC);
  11152. if (slot)
  11153. {
  11154. auto fx = lynx::makeEffect (*slot);
  11155. fx->prepare (44100.0);
  11156. std::vector<float> l (inL), r (inR);
  11157. fx->process (l.data(), r.data(), 1024, slot->params, 120.0);
  11158. fx->process (l.data() + 1024, r.data() + 1024, 1024, slot->params, 120.0);
  11159. fx->process (l.data() + 2048, r.data() + 2048, 2048, slot->params, 120.0);
  11160. CHECK (close (l[1000], -0.445489585f) && close (r[2047], 0.942175806f) && close (l[4095], 0.200391173f));
  11161. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityWaveShaper, 3, slot->params[3], 120.0) == "x4");
  11162. }
  11163. // an unreadable state keeps the old approximation
  11164. CHECK (lynx::effectFromFl ("Fruity WaveShaper", { 1, 2, 3 }, 120.0)->kind == lynx::EffectKind::distortion);
  11165. CHECK (lynx::effectFromFl ("Fruity Soft Clipper", { 1, 2, 3 }, 120.0)->kind == lynx::EffectKind::distortion);
  11166. }
  11167. // ---- Fruity Parametric EQ 2 / Fruity Parametric EQ (REA_rodada50) -----------------------------------------------------
  11168. // States written here from our own values (no FL preset data); goldens from the installation's DLLs (EXEC, blocks of 512)
  11169. namespace
  11170. {
  11171. struct PeqTestBand { int type, freq, width, gain, order; };
  11172. std::vector<std::uint8_t> peq2TestState (const PeqTestBand (&bands)[7], int main, int uiFlags, int options, int mask)
  11173. {
  11174. std::vector<std::uint8_t> b;
  11175. 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))); };
  11176. i32 (8);
  11177. for (int copy = 0; copy < 2; ++copy)
  11178. {
  11179. for (const auto& x : bands) i32 (x.gain);
  11180. for (const auto& x : bands) i32 (x.freq);
  11181. for (const auto& x : bands) i32 (x.width);
  11182. for (const auto& x : bands) i32 (x.type);
  11183. for (const auto& x : bands) i32 (x.order);
  11184. i32 (main);
  11185. }
  11186. i32 (uiFlags), i32 (0), b.push_back (0), i32 (2), i32 (0), i32 (2), i32 (0);
  11187. b.push_back ((std::uint8_t) options);
  11188. i32 (mask), i32 (1), i32 (0);
  11189. for (int v : { 3, 1, 2, 2, 2 }) i32 (v);
  11190. i32 (0);
  11191. return b;
  11192. }
  11193. std::vector<std::uint8_t> peq1TestState (const PeqTestBand (&bands)[7], int main)
  11194. {
  11195. std::vector<std::uint8_t> b;
  11196. 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))); };
  11197. for (const auto& x : bands) i32 (x.gain);
  11198. for (const auto& x : bands) i32 (x.freq);
  11199. for (const auto& x : bands) i32 (x.width);
  11200. for (const auto& x : bands) i32 (x.type);
  11201. i32 (main);
  11202. return b;
  11203. }
  11204. } // namespace
  11205. // EQUO test state (our own LCG curves, no FL data; REA_rodada61 equo/teststate.py builds the same bytes)
  11206. static std::vector<std::uint8_t> equoTestState (int ovs, int bw, bool add)
  11207. {
  11208. std::vector<std::uint8_t> b;
  11209. 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); };
  11210. auto putI = [&put] (std::int32_t v) { put (&v, 4); };
  11211. auto putF = [&put] (float v) { put (&v, 4); };
  11212. std::uint32_t seed = 12345;
  11213. auto rnd = [&seed] {
  11214. seed = (std::uint32_t) (((std::uint64_t) seed * 1103515245u + 12345u) & 0x7fffffffu);
  11215. return (float) ((double) (seed % 20001) / 10000.0);
  11216. };
  11217. putI (4);
  11218. for (int k = 0; k < 93; ++k)
  11219. {
  11220. putF (rnd());
  11221. putI (k % 11 == 0 ? 1 : (k % 13 == 0 ? 2 : 0));
  11222. }
  11223. for (int bank = 0; bank < 9; ++bank)
  11224. for (int k = 0; k < 93; ++k)
  11225. {
  11226. const float v = rnd();
  11227. putF (bank == 8 ? 0.75f + v * 0.25f : v);
  11228. }
  11229. for (int v : { 1500, 90, 2600, 170, 150, ovs, bw, -1, 0, 0 })
  11230. putI (v);
  11231. for (int v : { 0, add ? 1 : 0, 0 })
  11232. b.push_back ((std::uint8_t) v);
  11233. putI (1);
  11234. return b;
  11235. }
  11236. // Pitch Shifter state (version 7, our own settings; REA_rodada61 ps/teststate.py builds the same bytes)
  11237. static std::vector<std::uint8_t> pitchShifterTestState (std::array<int, 13> p, int engine, int detector)
  11238. {
  11239. std::vector<std::uint8_t> b;
  11240. auto putI = [&b] (std::int32_t v) { const auto* q = (const std::uint8_t*) &v; b.insert (b.end(), q, q + 4); };
  11241. putI (7);
  11242. for (int v : p)
  11243. putI (v);
  11244. putI (p[11]);
  11245. b.push_back (0);
  11246. putI (engine);
  11247. putI (detector);
  11248. putI (1000);
  11249. b.push_back (1);
  11250. b.push_back (0);
  11251. return b;
  11252. }
  11253. TEST (pitch_shifter_exact)
  11254. {
  11255. std::vector<float> inL (8192), inR (8192);
  11256. for (int n = 0; n < 8192; ++n)
  11257. {
  11258. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0) * 0.3f;
  11259. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0) * 0.3f;
  11260. }
  11261. struct Gold { int i; float l, r; };
  11262. // goldens: Pitch Shifter_x64.dll on the same states and signal (REA_rodada61)
  11263. auto run = [&] (const std::vector<std::uint8_t>& st, int latency, std::initializer_list<Gold> gold) {
  11264. lynx::psfl::State s;
  11265. CHECK (st.size() == 75 && lynx::psfl::parsePitchShifter (st.data(), st.size(), s));
  11266. // through the mixer effect: FL import → slot parameters → effect, odd host blocks
  11267. const auto slot = lynx::effectFromFl ("Pitch Shifter", st, 120.0);
  11268. CHECK (slot && slot->kind == lynx::EffectKind::flPitchShifter);
  11269. if (! slot)
  11270. return;
  11271. auto fx = lynx::makeEffect (*slot);
  11272. fx->prepare (44100.0);
  11273. std::vector<float> l (inL), r (inR);
  11274. for (int i = 0, k = 0; i < 8192; ++k)
  11275. {
  11276. const int n = std::min (8192 - i, k % 2 == 0 ? 700 : 333);
  11277. fx->process (l.data() + i, r.data() + i, n, slot->params, 120.0);
  11278. i += n;
  11279. }
  11280. CHECK (fx->latencySamples() == latency);
  11281. for (const auto& g : gold)
  11282. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  11283. };
  11284. // "FFT" (pitch-synchronous) engine: +5 st, formant −3 st, correction 50 %, mix 79 %, accurate detector
  11285. run (pitchShifterTestState ({ 500, -300, 2, 1900, 8500, 200, 0, 0, 768, 0, 0, 32768, 0 }, 0, 1), 2457,
  11286. { { 1000, 0.0f, 0.0f }, { 3000, 0.142123833f, 0.0878119618f }, { 5000, 0.139498621f, -0.365035176f },
  11287. { 7000, 0.139498621f, 0.161137447f }, { 8191, 0.449655473f, 0.453882188f } });
  11288. // granular engine: −7 st, 60 ms grains, density 300 %, jitter 30 %, random 20 %, post delay with feedback 40 %
  11289. run (pitchShifterTestState ({ -700, 0, 2, 2400, 6000, 300, 30, 20, 200, 40, 1, 65536, 0 }, 1, 0), 0,
  11290. { { 1000, -0.0736689344f, -0.349420458f }, { 3000, 0.086801365f, 0.0471927896f }, { 5000, -0.0939825475f, 0.31712544f },
  11291. { 7000, -0.28462702f, -0.437854528f }, { 8191, 0.138916701f, -0.0426892526f } });
  11292. CHECK (lynx::formatEffectParam (lynx::EffectKind::flPitchShifter, 0, 0.75f, 120.0) == "+6.00 st");
  11293. }
  11294. TEST (equo_exact)
  11295. {
  11296. std::vector<float> inL (4096), inR (4096);
  11297. for (int n = 0; n < 4096; ++n)
  11298. {
  11299. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  11300. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  11301. }
  11302. struct Gold { int i; float l, r; };
  11303. // goldens: EQUO_x64.dll run on the same states and signal (REA_rodada61 §5)
  11304. auto run = [&] (const std::vector<std::uint8_t>& st, bool hq, bool ticks, std::initializer_list<Gold> gold) {
  11305. const auto s = lynx::equofx::parse (st);
  11306. CHECK (s.has_value());
  11307. if (! s)
  11308. return;
  11309. lynx::equofx::Engine e;
  11310. e.prepare (44100.0, hq);
  11311. e.set (*s);
  11312. std::vector<float> l (inL), r (inR);
  11313. if (! ticks)
  11314. e.process (l.data(), r.data(), 4096);
  11315. else
  11316. {
  11317. auto k = *s;
  11318. e.startFromConstruction();
  11319. for (int i = 0; i < 4096; i += 256)
  11320. {
  11321. if (i == 2048)
  11322. k.knob[2] = 5300, e.setKnobs (k);
  11323. e.tick();
  11324. e.process (l.data() + i, r.data() + i, 256);
  11325. }
  11326. }
  11327. for (const auto& g : gold)
  11328. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  11329. };
  11330. const auto plain = equoTestState (0, 150, false);
  11331. run (plain, false, false, { { 100, -0.00154058414f, -0.00550224911f }, { 1000, -0.908149242f, 0.619277894f }, { 2047, 0.411961317f, -0.364051819f },
  11332. { 3000, -1.42823684f, -0.0602950938f }, { 4095, -0.0661701784f, 0.300809383f } });
  11333. run (equoTestState (1, 300, true), false, false,
  11334. { { 100, 0.0312003642f, 0.0184187777f }, { 1000, 0.0379521511f, 0.224126667f }, { 2047, -0.135689721f, 0.0444660783f },
  11335. { 3000, 0.113949843f, -1.00256324f }, { 4095, -0.176232055f, 0.79159987f } });
  11336. run (plain, true, false, { { 100, -0.00104017637f, 0.000872685807f }, { 1000, -0.0426855832f, 0.0402083024f }, { 2047, 0.191993102f, 0.174184501f },
  11337. { 3000, -0.114048228f, 0.419817507f }, { 4095, 0.0376945846f, -0.274040252f } });
  11338. run (plain, false, true, { { 100, -0.0019897793f, -0.00981196482f }, { 1000, -1.09574401f, 0.83203429f }, { 2047, 0.839113712f, -0.691361725f } });
  11339. // the mixer effect from the FL import: FL's state in the slot, the knobs in its parameters, any host block size
  11340. const auto slot = lynx::effectFromFl ("EQUO", plain, 140.0);
  11341. CHECK (slot && slot->kind == lynx::EffectKind::equo && slot->state == plain);
  11342. if (slot)
  11343. {
  11344. auto back = *lynx::equofx::parse (plain);
  11345. lynx::equofx::applyParams (slot->params, back);
  11346. CHECK (back.knob == lynx::equofx::parse (plain)->knob && back.bandwidth == 150 && back.oversample == 0);
  11347. auto fx = lynx::makeEffect (*slot);
  11348. fx->prepare (44100.0);
  11349. std::vector<float> l (inL), r (inR);
  11350. fx->process (l.data(), r.data(), 700, slot->params, 0.0);
  11351. fx->process (l.data() + 700, r.data() + 700, 3396, slot->params, 0.0);
  11352. // the engine glides its bands per processing call, as FL does per host buffer: compare with the engine run on
  11353. // the same 700 + 3396 split (sample 1000 differs from the single-call golden), and with the golden once settled
  11354. lynx::equofx::Engine ref;
  11355. ref.prepare (44100.0, false);
  11356. ref.set (*lynx::equofx::parse (plain));
  11357. std::vector<float> rl (inL), rr (inR);
  11358. ref.process (rl.data(), rr.data(), 700);
  11359. ref.process (rl.data() + 700, rr.data() + 700, 3396);
  11360. CHECK (l == rl && rr == r && r[3000] == -0.0602950938f);
  11361. CHECK (lynx::formatEffectParam (lynx::EffectKind::equo, 4, slot->params[4], 120.0, slot->params).find ("dB") != std::string::npos);
  11362. }
  11363. CHECK (! lynx::equofx::parse (std::vector<std::uint8_t> (100, 0)).has_value());
  11364. }
  11365. // Multiband Delay test state (version 7, our own LCG cells, no FL data; REA_rodada71 mbd/py/teststate.py builds the same bytes)
  11366. static std::vector<std::uint8_t> multibandDelayTestState (std::array<int, 12> globals, std::uint32_t seed)
  11367. {
  11368. std::vector<std::uint8_t> b;
  11369. auto putI = [&b] (std::int32_t v) { const auto* q = (const std::uint8_t*) &v; b.insert (b.end(), q, q + 4); };
  11370. putI (7);
  11371. putI (396);
  11372. std::uint64_t s = seed;
  11373. for (int i = 0; i < 384; ++i)
  11374. {
  11375. s = (s * 1103515245u + 12345u) & 0x7fffffffu;
  11376. putI ((std::int32_t) ((s % 100001u) * (1u << 30) / 100000u));
  11377. }
  11378. for (int v : globals)
  11379. putI (v);
  11380. putI (0);
  11381. putI (3);
  11382. for (int v : { 1, 1, 0 })
  11383. b.push_back ((std::uint8_t) v);
  11384. b.insert (b.end(), 192, 0);
  11385. return b;
  11386. }
  11387. TEST (multiband_delay_exact)
  11388. {
  11389. std::vector<float> inL (8192), inR (8192);
  11390. for (int n = 0; n < 8192; ++n)
  11391. {
  11392. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0 * 0.3);
  11393. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0 * 0.3);
  11394. }
  11395. struct Gold { int i; float l, r; };
  11396. // goldens: Multiband Delay_x64.dll (+ its engine_x64.dll) on the same states and signal, 512-frame blocks (REA_rodada71)
  11397. auto run = [&] (const std::vector<std::uint8_t>& st, int latency, float tol, std::initializer_list<Gold> gold) {
  11398. CHECK (st.size() == 1795 && lynx::mbdelay::parse (st).has_value());
  11399. const auto slot = lynx::effectFromFl ("Multiband Delay", st, 120.0);
  11400. CHECK (slot && slot->kind == lynx::EffectKind::multibandDelay && slot->state == st);
  11401. if (! slot)
  11402. return;
  11403. auto fx = lynx::makeEffect (*slot);
  11404. fx->prepare (44100.0);
  11405. std::vector<float> l (inL), r (inR);
  11406. for (int i = 0, k = 0; i < 8192; ++k)
  11407. {
  11408. const int n = std::min (8192 - i, k % 2 == 0 ? 700 : 333);
  11409. fx->process (l.data() + i, r.data() + i, n, slot->params, 120.0);
  11410. i += n;
  11411. }
  11412. CHECK (fx->latencySamples() == latency);
  11413. for (const auto& g : gold)
  11414. CHECK (std::fabs (l[(std::size_t) g.i] - g.l) <= tol && std::fabs (r[(std::size_t) g.i] - g.r) <= tol);
  11415. };
  11416. // steep crossover, keep pitch, weighted feedback, compressor
  11417. run (multibandDelayTestState ({ 2000, 0, 3000, 600, 1, 1, 3500, 64, 0, 1, 30000, 0 }, 777), 0, 0.0f,
  11418. { { 1000, -0.125012308f, 0.0675066486f }, { 3000, -0.125002861f, -0.0525011979f }, { 5000, -0.110439189f, 0.0597563162f },
  11419. { 7000, -0.146480903f, 0.0139919929f }, { 8191, -0.0454496294f, 0.0398242511f } });
  11420. // gentle crossover, harmonic scale, delay ×0.1, saturation
  11421. run (multibandDelayTestState ({ -2500, 1, 4500, 0, 0, 0, 0, 128, 1, 0, 1000, 1 }, 4242), 0, 0.0f,
  11422. { { 1000, 0.0679653957f, 0.0137896547f }, { 3000, 0.0620588325f, 0.0558352768f }, { 5000, 0.0558495969f, -0.0220566839f },
  11423. { 7000, 0.057009533f, 0.00332171144f }, { 8191, 0.0673644915f, 0.00163267844f } });
  11424. // linear-phase crossover (FL's IPP FFTs are approximated: about −120 dB), clipper
  11425. run (multibandDelayTestState ({ 1500, 0, 2000, 1500, 2, 0, 1000, 100, 0, 0, 5000, 2 }, 99), 1536, 2e-6f,
  11426. { { 1000, 0.0f, 0.0f }, { 3000, 0.103910387f, -0.0521105044f }, { 5000, 0.0516818762f, -0.0134486742f },
  11427. { 7000, 0.065563485f, -0.00765838847f }, { 8191, -0.0376662463f, -0.0434602089f } });
  11428. const auto st = multibandDelayTestState ({ 2000, 0, 3000, 600, 1, 1, 3500, 64, 0, 1, 30000, 0 }, 777);
  11429. const auto s = *lynx::mbdelay::parse (st);
  11430. CHECK (lynx::mbdelay::parse (lynx::mbdelay::serialize (s))->raw == s.raw);
  11431. lynx::EffectParams p {};
  11432. lynx::mbdelay::toParams (s, p);
  11433. auto back = s;
  11434. lynx::mbdelay::applyParams (p, back);
  11435. CHECK (back.raw == s.raw);
  11436. CHECK (lynx::mbdelay::engineValue (s, 384 + 7) == (float) (192.0 / 256.0));
  11437. CHECK (lynx::formatEffectParam (lynx::EffectKind::multibandDelay, 4, p[4], 120.0) == "steep");
  11438. CHECK (lynx::formatEffectParam (lynx::EffectKind::multibandDelay, 7, 1.0f, 120.0) == "100% wet");
  11439. CHECK (! lynx::mbdelay::parse (std::vector<std::uint8_t> (16, 0)).has_value());
  11440. }
  11441. TEST (fruity_parametric_eq_exact)
  11442. {
  11443. std::vector<float> inL (4096), inR (4096);
  11444. for (int n = 0; n < 4096; ++n)
  11445. {
  11446. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  11447. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  11448. }
  11449. struct Gold { int i; float l, r; };
  11450. // high pass Steep 8, low shelf, steep peaking, a 0 dB peaking (0.2 s countdown), notch Gentle 4, high shelf Gentle 8,
  11451. // low pass Gentle 6
  11452. const PeqTestBand bands[7] = { { 3, 8000, 30000, 0, -3 }, { 5, 15000, 40124, 600, 0 }, { 6, 30000, 20000, -900, -2 },
  11453. { 6, 35000, 25412, 0, 0 }, { 4, 40000, 10000, 0, 1 }, { 7, 50000, 30000, 450, 3 },
  11454. { 1, 60000, 45000, 0, 2 } };
  11455. const PeqTestBand bands1[7] = { { 3, 6000, 20000, 0, 0 }, { 5, 15000, 30000, 600, 0 }, { 6, 30000, 20000, -900, 0 },
  11456. { 6, 35000, 25000, 0, 0 }, { 4, 40000, 10000, 0, 0 }, { 7, 50000, 30000, 450, 0 },
  11457. { 1, 60000, 45000, 0, 0 } };
  11458. auto run = [&] (const std::optional<lynx::peqfl::Settings>& s, std::initializer_list<Gold> gold) {
  11459. CHECK (s.has_value());
  11460. if (! s)
  11461. return;
  11462. lynx::peqfl::Engine e;
  11463. e.set (*s);
  11464. e.prepare (44100.0);
  11465. std::vector<float> l (inL), r (inR);
  11466. for (int i = 0; i < 4096; i += 512)
  11467. e.process (l.data() + i, r.data() + i, 512);
  11468. for (const auto& g : gold)
  11469. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  11470. };
  11471. const auto plain = peq2TestState (bands, 300, 0, 0x60, 0x7F);
  11472. const auto hq = peq2TestState (bands, 300, 1, 0x60, 0x7F);
  11473. const auto ext = peq2TestState (bands, 300, 1, 0x62, 0x7F);
  11474. run (lynx::peqfl::parsePeq2 (plain), { { 100, 0.1340574324131012f, -0.3040016293525696f }, { 1000, 0.28402701020240784f, -0.23813000321388245f },
  11475. { 2047, -0.9301530122756958f, 0.13881002366542816f }, { 3000, 0.2846762537956238f, 0.48893973231315613f },
  11476. { 4095, -0.8481860756874084f, -0.2641225755214691f } });
  11477. run (lynx::peqfl::parsePeq2 (hq), { { 100, 0.19229719042778015f, 0.29938483238220215f }, { 1000, 0.12543821334838867f, -0.055617764592170715f },
  11478. { 2047, 1.0342068672180176f, 0.10901166498661041f }, { 3000, 0.12598788738250732f, -0.12920552492141724f },
  11479. { 4095, 0.1619236022233963f, -0.23385579884052277f } });
  11480. run (lynx::peqfl::parsePeq2 (ext), { { 100, 1.2178642749786377f, -0.21160493791103363f }, { 1000, -0.8292768001556396f, -0.23916664719581604f },
  11481. { 2047, -0.7030635476112366f, -0.1978478729724884f }, { 3000, -0.8299900889396667f, 0.9836107492446899f },
  11482. { 4095, 0.06754066050052643f, -0.34364908933639526f } });
  11483. run (lynx::peqfl::parsePeq2 (peq2TestState (bands, -250, 1, 0x60, 0x5B)), // bands 3 and 6 muted
  11484. { { 100, -0.02959616854786873f, 0.227821946144104f }, { 1000, 0.053224340081214905f, 0.06922465562820435f },
  11485. { 2047, 0.3365536034107208f, -0.05899651721119881f }, { 3000, 0.053670305758714676f, -0.07452142238616943f },
  11486. { 4095, -0.04406970739364624f, -0.12635089457035065f } });
  11487. run (lynx::peqfl::parsePeq1 (peq1TestState (bands1, 150)),
  11488. { { 100, -0.6533108353614807f, -1.6903921365737915f }, { 1000, -4.313726902008057f, 5.973916053771973f },
  11489. { 2047, 1.0222549438476562f, 4.427900314331055f }, { 3000, -4.310323238372803f, 7.825916767120361f },
  11490. { 4095, 8.344571113586426f, -0.49086111783981323f } });
  11491. // "alternative design" (Frequency Splitter's matched biquads), bit-exact
  11492. run (lynx::peqfl::parsePeq2 (peq2TestState (bands, 300, 3, 0x60, 0x7F)),
  11493. { { 100, -0.790181577205658f, -0.08691816031932831f }, { 1000, 0.4960691034793854f, -0.24172282218933105f },
  11494. { 2047, -0.42601829767227173f, 0.027932533994317055f }, { 3000, 0.49674150347709656f, 1.0799059867858887f },
  11495. { 4095, -0.35040369629859924f, -0.28149813413619995f } });
  11496. // linear phase (plain and HQ response): FFT convolution, 1536-sample latency, 0.1 s fade-in; FL's IPP FFT rounding
  11497. // is not replicated, so within 1e-6 of the DLL
  11498. for (const bool hq : { false, true })
  11499. {
  11500. const auto s = lynx::peqfl::parsePeq2 (peq2TestState (bands, hq ? -200 : 300, hq ? 1 : 0, 0x61, 0x7F));
  11501. CHECK (s && s->linearPhase);
  11502. if (! s)
  11503. continue;
  11504. std::vector<float> l (12288), r (12288);
  11505. for (int n = 0; n < 12288; ++n)
  11506. {
  11507. l[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  11508. r[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  11509. }
  11510. lynx::peqfl::Engine e;
  11511. e.set (*s);
  11512. e.prepare (44100.0);
  11513. CHECK (e.latencySamples() == 1536);
  11514. for (int i = 0; i < 12288; i += 512)
  11515. e.process (l.data() + i, r.data() + i, 512);
  11516. // (named arrays: a std::initializer_list chosen by ?: would point into a temporary array that dies at the end of
  11517. // the declaration, which GCC 13 reuses for other locals)
  11518. static const Gold goldHq[] = {
  11519. { 2000, 0.08618364483118057f, 0.11248858273029327f }, { 5000, 0.18998730182647705f, 0.428600937128067f },
  11520. { 8191, -0.1412065178155899f, -0.002996620489284396f }, { 10000, 0.18998737633228302f, -0.012570115737617016f },
  11521. { 12287, 0.40586185455322266f, -0.014698396436870098f } };
  11522. static const Gold goldPlain[] = {
  11523. { 2000, 0.18538671731948853f, 0.206522598862648f }, { 5000, 0.4086928963661194f, 0.8167467713356018f },
  11524. { 8191, -0.3218550980091095f, -0.06812914460897446f }, { 10000, 0.40869277715682983f, -0.08537150919437408f },
  11525. { 12287, 0.775394082069397f, -0.08931504935026169f } };
  11526. for (const auto& g : hq ? goldHq : goldPlain)
  11527. 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);
  11528. }
  11529. // the mixer effect from the FL import: FL's values in the slot's parameters (exact round trip), any host block size
  11530. for (const auto* name : { "Fruity Parametric EQ 2", "Fruity Parametric EQ" })
  11531. {
  11532. const bool v1 = std::string (name) == "Fruity Parametric EQ";
  11533. const auto st = v1 ? peq1TestState (bands1, 150) : ext;
  11534. const auto slot = lynx::effectFromFl (name, st, 120.0);
  11535. CHECK (slot && slot->kind == lynx::EffectKind::parametricEq && lynx::peq::flLaw (slot->params));
  11536. if (! slot)
  11537. continue;
  11538. const auto back = lynx::peqfl::fromParams (slot->params);
  11539. const auto orig = v1 ? lynx::peqfl::parsePeq1 (st) : lynx::peqfl::parsePeq2 (st);
  11540. CHECK (back.freq == orig->freq && back.gain == orig->gain && back.width == orig->width && back.type == orig->type
  11541. && back.order == orig->order && back.main == orig->main && back.processing == orig->processing && back.mask == orig->mask);
  11542. auto fx = lynx::makeEffect (*slot);
  11543. fx->prepare (44100.0);
  11544. CHECK (fx->latencySamples() == (v1 ? 0 : 44));
  11545. std::vector<float> l (inL), r (inR);
  11546. fx->process (l.data(), r.data(), 1000, slot->params, 120.0);
  11547. fx->process (l.data() + 1000, r.data() + 1000, 3096, slot->params, 120.0);
  11548. if (v1)
  11549. CHECK (l[3000] == -4.310323238372803f && r[4095] == -0.49086111783981323f);
  11550. else
  11551. CHECK (l[1000] == -0.8292768001556396f && r[3000] == 0.9836107492446899f && l[4095] == 0.06754066050052643f);
  11552. // editor texts and graph
  11553. CHECK (lynx::formatEffectParam (lynx::EffectKind::parametricEq, lynx::peq::slopeParam, slot->params[lynx::peq::slopeParam], 120.0, slot->params)
  11554. == (v1 ? "12 dB/oct" : "Steep 8"));
  11555. CHECK (lynx::peq::widthText (slot->params, 0).rfind ("BW ", 0) == 0);
  11556. const auto lowCutDb = lynx::peq::responseDb (slot->params, 44100.0, 25.0), midDb = lynx::peq::responseDb (slot->params, 44100.0, 1000.0);
  11557. CHECK (lowCutDb < midDb - 5.0); // high pass below ~100 Hz (Steep 8 in the EQ 2 state, 12 dB/oct in v1)
  11558. }
  11559. // a native LYNX EQ is untouched by the FL law
  11560. CHECK (! lynx::peq::flLaw (lynx::makeEffectSlot (lynx::EffectKind::parametricEq).params));
  11561. }
  11562. TEST (fruity_panomatic_balance_blood_overdrive)
  11563. {
  11564. // REA_rodada58: goldens from the installation's DLLs (EXEC) on our own states, 44.1 kHz, blocks of 512
  11565. const lynx::ScopedFlushDenormals ftz;
  11566. std::vector<float> inL (4096), inR (4096);
  11567. for (int n = 0; n < 4096; ++n)
  11568. {
  11569. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  11570. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  11571. }
  11572. struct Gold { int i; float l, r; };
  11573. auto close = [] (float a, float b) { return std::abs (a - b) <= x87Tolerance() * std::max (1.0f, std::abs (b)); };
  11574. auto check = [&] (const std::vector<float>& l, const std::vector<float>& r, std::initializer_list<Gold> gold) {
  11575. for (const auto& g : gold)
  11576. CHECK (close (l[(std::size_t) g.i], g.l) && close (r[(std::size_t) g.i], g.r));
  11577. };
  11578. auto st32 = [] (std::initializer_list<int> v) {
  11579. std::vector<std::uint8_t> b;
  11580. for (int x : v)
  11581. for (int k = 0; k < 4; ++k)
  11582. b.push_back ((std::uint8_t) ((unsigned) x >> (8 * k)));
  11583. return b;
  11584. };
  11585. // PanOMatic: the engine started as LYNX starts it (one silent frame, then FL's flush), and from silence (fade-in)
  11586. auto pom = [&] (lynx::flbasic::PanOMaticState s, bool prime, std::initializer_list<Gold> gold) {
  11587. lynx::flbasic::PanOMaticEngine e;
  11588. e.prepare (44100);
  11589. e.set (s);
  11590. if (prime)
  11591. e.prime();
  11592. std::vector<float> l (inL), r (inR);
  11593. for (int i = 0; i < 4096; i += 512)
  11594. e.process (l.data() + i, r.data() + i, 512);
  11595. check (l, r, gold);
  11596. };
  11597. pom ({ 40, 300, 2, 1, 128, 50000 }, true, { { 100, 0.0f, -1.44884729f }, { 1000, -1.78870034f, 0.778897762f }, { 2047, 0.697593153f, -1.26396084f },
  11598. { 3000, -1.78870034f, -1.32851648f }, { 4095, 0.268305063f, 0.851258814f } });
  11599. pom ({ 200, 180, 1, 2, 100, 30000 }, true, { { 100, 0.0f, -0.347919852f }, { 1000, -0.322590947f, 0.0788071603f }, { 2047, 0.126614109f, -0.26421836f },
  11600. { 3000, -0.326532423f, -0.435213327f }, { 4095, 0.0493027233f, 0.226084977f } });
  11601. pom ({ 90, 256, 0, 1, 77, 40000 }, false, { { 100, -0.0292895138f, -0.110000029f }, { 1000, -1.31911683f, 0.666457176f }, { 2047, 0.477211952f, -0.864247262f },
  11602. { 3000, -1.59167564f, -0.591367304f }, { 4095, 0.268362761f, 0.537029684f } });
  11603. // Balance: flushed and from silence
  11604. auto bal = [&] (lynx::flbasic::BalanceState s, bool flush, std::initializer_list<Gold> gold) {
  11605. lynx::flbasic::BalanceEngine e;
  11606. e.prepare (44100);
  11607. e.set (s);
  11608. if (flush)
  11609. e.flush();
  11610. std::vector<float> l (inL), r (inR);
  11611. for (int i = 0; i < 4096; i += 512)
  11612. e.process (l.data() + i, r.data() + i, 512);
  11613. check (l, r, gold);
  11614. };
  11615. bal ({ 77, 100 }, true, { { 100, 0.0f, -0.136579692f }, { 1000, -0.0669578835f, -0.0282340273f }, { 2047, 0.0261135735f, -0.0795038193f },
  11616. { 3000, -0.0669578835f, -0.226895392f }, { 4095, 0.0100436825f, 0.0954951793f } });
  11617. bal ({ -128, 320 }, false, { { 100, -0.28914085f, 0.0f }, { 1000, -1.03179514f, 0.0f }, { 2047, -0.578702569f, 0.0f },
  11618. { 3000, -3.18510628f, 0.0f }, { 4095, 1.14394665f, 0.0f } });
  11619. // Blood Overdrive: rate first, then the state (its smoothers settle for one second of samples)
  11620. auto od = [&] (std::array<int, 6> raw, std::initializer_list<Gold> gold) {
  11621. lynx::flbasic::BloodOverdriveEngine e;
  11622. e.start (44100, { raw });
  11623. std::vector<float> l (inL), r (inR);
  11624. for (int i = 0; i < 4096; i += 512)
  11625. e.process (l.data() + i, r.data() + i, 512);
  11626. check (l, r, gold);
  11627. };
  11628. od ({ 3000, 7000, 5000, 1, 9000, 5000 }, { { 100, -0.0442250744f, 0.170355186f }, { 1000, 0.0470311977f, 0.0765217245f },
  11629. { 2047, 0.0261161141f, -0.0341798998f }, { 3000, 0.0470311977f, 0.0213619489f },
  11630. { 4095, 0.0402069278f, 0.0499244779f } });
  11631. // the mixer effects from the FL import: controls as parameters; host blocks of any size
  11632. auto viaImport = [&] (const char* plugin, const std::vector<std::uint8_t>& st, lynx::EffectKind kind, std::initializer_list<Gold> gold) {
  11633. const auto slot = lynx::effectFromFl (plugin, st, 120.0);
  11634. CHECK (slot && slot->kind == kind);
  11635. if (! slot)
  11636. return;
  11637. auto fx = lynx::makeEffect (*slot);
  11638. fx->prepare (44100.0);
  11639. std::vector<float> l (inL), r (inR);
  11640. fx->process (l.data(), r.data(), 1024, slot->params, 120.0);
  11641. fx->process (l.data() + 1024, r.data() + 1024, 1024, slot->params, 120.0);
  11642. fx->process (l.data() + 2048, r.data() + 2048, 2048, slot->params, 120.0);
  11643. check (l, r, gold);
  11644. };
  11645. viaImport ("Fruity PanOMatic", st32 ({ 40, 300, 2, 1, 128, 50000 }), lynx::EffectKind::fruityPanOMatic,
  11646. { { 1000, -1.78870034f, 0.778897762f }, { 4095, 0.268305063f, 0.851258814f } });
  11647. viaImport ("Fruity Balance", st32 ({ 0, 256 }), lynx::EffectKind::fruityBalance,
  11648. { { 100, 0.0f, -1.10000002f }, { 1000, -1.66666663f, 0.899999976f }, { 4095, 0.25f, 0.600000024f } });
  11649. viaImport ("Fruity Blood Overdrive", st32 ({ 3, 0, 5000, 2578, 1, 0, 2911, 0, 0 }), lynx::EffectKind::fruityBloodOverdrive,
  11650. { { 100, -0.122698173f, 0.0206716377f }, { 2047, 0.0128381588f, -0.0274834726f }, { 4095, -0.0899387598f, -0.121109024f } });
  11651. {
  11652. const auto slot = lynx::effectFromFl ("Fruity PanOMatic", st32 ({ 119, 256, 1, 2, 64, 27500 }), 120.0);
  11653. CHECK (slot && lynx::flbasic::panOMaticFromParams (slot->params.data()).pan == 119
  11654. && lynx::flbasic::panOMaticFromParams (slot->params.data()).target == 2);
  11655. if (slot)
  11656. {
  11657. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityPanOMatic, 2, slot->params[2], 120.0) == "Triangle");
  11658. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityPanOMatic, 1, slot->params[1], 120.0) == "+0.0 dB");
  11659. }
  11660. }
  11661. // unreadable states keep the old approximations
  11662. CHECK (lynx::effectFromFl ("Fruity PanOMatic", { 1, 2, 3 }, 120.0)->kind == lynx::EffectKind::utility);
  11663. CHECK (lynx::effectFromFl ("Fruity Balance", { 1, 2, 3 }, 120.0)->kind == lynx::EffectKind::utility);
  11664. CHECK (lynx::effectFromFl ("Fruity Blood Overdrive", { 1, 2, 3 }, 120.0)->kind == lynx::EffectKind::distortion);
  11665. }
  11666. TEST (fruity_fast_lp_free_filter_filter)
  11667. {
  11668. // REA_rodada63: goldens from the installation's DLLs (EXEC) on our own states, 44.1 kHz, blocks of 512 (the wrapper
  11669. // splits the host blocks below into calls of 512, as the DLL ran)
  11670. const lynx::ScopedFlushDenormals ftz;
  11671. std::vector<float> inL (4096), inR (4096);
  11672. for (int n = 0; n < 4096; ++n)
  11673. {
  11674. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  11675. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  11676. }
  11677. struct Gold { int i; float l, r; };
  11678. auto state = [] (const char* hex) {
  11679. std::vector<std::uint8_t> b;
  11680. for (const char* p = hex; p[0] && p[1]; p += 2)
  11681. b.push_back ((std::uint8_t) std::strtol (std::string (p, 2).c_str(), nullptr, 16));
  11682. return b;
  11683. };
  11684. auto viaImport = [&] (const char* plugin, const char* hex, lynx::EffectKind kind, std::initializer_list<Gold> gold) {
  11685. const auto slot = lynx::effectFromFl (plugin, state (hex), 120.0);
  11686. CHECK (slot && slot->kind == kind);
  11687. if (! slot)
  11688. return;
  11689. auto fx = lynx::makeEffect (*slot);
  11690. fx->prepare (44100.0);
  11691. std::vector<float> l (inL), r (inR);
  11692. fx->process (l.data(), r.data(), 1024, slot->params, 120.0);
  11693. fx->process (l.data() + 1024, r.data() + 1024, 1024, slot->params, 120.0);
  11694. fx->process (l.data() + 2048, r.data() + 2048, 2048, slot->params, 120.0);
  11695. for (const auto& g : gold)
  11696. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  11697. };
  11698. using K = lynx::EffectKind;
  11699. viaImport ("Fruity Fast LP", "01000000e0010000bc020000", K::fruityFastLP,
  11700. { { 100, -2.876206159591675f, 1.5413906574249268f }, { 1000, 0.12972253561019897f, 0.02936629205942154f },
  11701. { 2047, -0.2262123078107834f, -0.15169914066791534f }, { 3000, 0.12972258031368256f, -0.0798468142747879f },
  11702. { 4095, 0.0011161785805597901f, -0.08101501315832138f } });
  11703. viaImport ("Fruity Fast LP", "01000000890000008a000000", K::fruityFastLP,
  11704. { { 100, -0.7748895883560181f, -0.2310182899236679f }, { 1000, -0.01256702933460474f, -0.0086913350969553f },
  11705. { 2047, -0.010225297883152962f, -0.014700239524245262f }, { 3000, -0.012712989933788776f, -0.008916046470403671f },
  11706. { 4095, -0.008918046951293945f, -0.020280027762055397f } });
  11707. viaImport ("Fruity Free Filter", "02000000c2010000200300002c0100008403000001", K::fruityFreeFilter, // low shelf, CENTER
  11708. { { 100, -0.6178444623947144f, -0.13762974739074707f }, { 1000, -1.6557703018188477f, 0.8969771862030029f },
  11709. { 2047, 0.6539966464042664f, -0.21659433841705322f }, { 3000, -1.6404345035552979f, 0.4286947548389435f },
  11710. { 4095, -1.2272151708602905f, -0.5219485759735107f } });
  11711. viaImport ("Fruity Free Filter", "020000008a02000058020000bc020000c800000000", K::fruityFreeFilter, // high shelf
  11712. { { 100, -0.06996861100196838f, -0.11112016439437866f }, { 1000, -0.47636136412620544f, 0.31258222460746765f },
  11713. { 2047, 0.1628853678703308f, -0.3169582486152649f }, { 3000, -0.47636136412620544f, -0.22028093039989471f },
  11714. { 4095, -0.0975298061966896f, 0.05710025131702423f } });
  11715. viaImport ("Fruity Free Filter", "020000006400000000040000a9000000a402000001", K::fruityFreeFilter, // low pass 16 kHz
  11716. { { 100, 1.3032524585723877f, -0.007953938096761703f }, { 1000, 0.18662987649440765f, 2.155099391937256f },
  11717. { 2047, 0.5824014544487f, 0.05704556778073311f }, { 3000, 0.18981599807739258f, -0.6362011432647705f },
  11718. { 4095, 1.2624475955963135f, 2.497816324234009f } });
  11719. viaImport ("Fruity Filter", "020000004b01000076000000000400002c010000580200000100000001", K::fruityFilter, // x2, CENTER
  11720. { { 100, 0.2685631811618805f, 0.16885951161384583f }, { 1000, -0.20637311041355133f, 0.13557639718055725f },
  11721. { 2047, 0.07038643211126328f, -0.15532609820365906f }, { 3000, -0.20311756432056427f, -0.09958365559577942f },
  11722. { 4095, 0.08275680989027023f, 0.010792487300932407f } });
  11723. viaImport ("Fruity Filter", "01000000bc020000f401000000000000000400000000000000000000", K::fruityFilter, // band pass
  11724. { { 100, -0.4368843734264374f, -0.12492841482162476f }, { 1000, -0.3362128436565399f, -0.09686881303787231f },
  11725. { 2047, 0.21920710802078247f, 0.3213832974433899f }, { 3000, -0.3362128436565399f, 0.4016203284263611f },
  11726. { 4095, -0.14230486750602722f, -0.22842440009117126f } });
  11727. // an old Free Filter layout (type·10) is converted to the current scale; parameter texts
  11728. {
  11729. const auto slot = lynx::effectFromFl ("Fruity Free Filter", state ("010000003c00000000020000a9000000a4020000"), 120.0);
  11730. CHECK (slot && slot->kind == K::fruityFreeFilter);
  11731. if (slot)
  11732. {
  11733. CHECK (lynx::flfilter::freeFilterFromParams (slot->params.data()).type == 100 + (int) std::nearbyint (5.5 / 7.0 * 600.0));
  11734. CHECK (lynx::formatEffectParam (K::fruityFreeFilter, 0, slot->params[0], 120.0) == "Peaking");
  11735. }
  11736. CHECK (lynx::formatEffectParam (K::fruityFreeFilter, 1, 1.0f, 120.0) == "16000 Hz");
  11737. CHECK (lynx::formatEffectParam (K::fruityFilter, 0, 1.0f, 120.0) == "7350 Hz");
  11738. }
  11739. // unreadable states keep the old approximations
  11740. CHECK (lynx::effectFromFl ("Fruity Fast LP", { 1, 2, 3 }, 120.0)->kind == K::filter);
  11741. CHECK (lynx::effectFromFl ("Fruity Free Filter", { 1, 2, 3 }, 120.0)->kind == K::filter);
  11742. CHECK (lynx::effectFromFl ("Fruity Filter", { 9, 0, 0, 0 }, 120.0)->kind == K::filter);
  11743. }
  11744. // ---- Sytrus (REA_rodada31) -------------------------------------------------------------------------------------------
  11745. // A Sytrus state built from scratch (version 25 layout, our own values — no FL preset data), zlib "stored" blocks.
  11746. namespace
  11747. {
  11748. struct SytrusTestPatch
  11749. {
  11750. std::vector<std::int32_t> main = std::vector<std::int32_t> (240, 0); // 962-byte main block as ints (+ mute bytes)
  11751. struct Op
  11752. {
  11753. int p[10] { 0, 256, 0, 0, 0, 0, 0, 0, 0, 0 };
  11754. int ratio = 20000, offset = 0, noise = 0;
  11755. std::vector<float> amps { 1.0f };
  11756. std::uint8_t flags[8] {};
  11757. int env1[3] { -1, -1, -1 }; // volume envelope: on (≠ −1 → on), decay point, sustain point
  11758. std::vector<std::pair<double, double>> env1pts;
  11759. } ops[6];
  11760. struct Filt
  11761. {
  11762. int p[10] { 0, 0, 128, 0, 128, 0, 0, 128, 128, 0 };
  11763. int type = 3;
  11764. } filts[3];
  11765. std::array<std::array<std::uint8_t, 23>, 3> delays {};
  11766. std::array<std::int32_t, 9> reverb { 19, 221, 0, 50, 80, 30, 221, 2, 0 };
  11767. bool reverbOn = false;
  11768. int eqmode = 0;
  11769. SytrusTestPatch()
  11770. {
  11771. main[0] = 128, main[1] = 128;
  11772. for (int i = 0; i < 8; ++i)
  11773. main[(std::size_t) (165 + i)] = (i % 4 == 2) ? 0 : 128;
  11774. main[173] = 1, main[175] = 128, main[188] = 128, main[189] = -1, main[190] = main[191] = 256;
  11775. }
  11776. void cell (int layer, int row, int col, int v) { main[(std::size_t) (3 + layer * 81 + row * 9 + col)] = v; }
  11777. std::vector<std::uint8_t> blob() const
  11778. {
  11779. std::vector<std::uint8_t> b;
  11780. 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))); };
  11781. auto f32 = [&] (float v) { std::int32_t x; std::memcpy (&x, &v, 4); i32 (x); };
  11782. 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))); };
  11783. // point lists: kind 1 envelope, 2 LFO, 3 map / shaper curve; format version 3
  11784. auto env = [&] (int kind, const std::vector<std::pair<double, double>>& pts, int on, int decay, int sus) {
  11785. i32 (kind), i32 (3), i32 ((std::int32_t) pts.size());
  11786. for (const auto& [dt, y] : pts)
  11787. f64 (dt), f64 (y), f32 (0.0f), i32 (0);
  11788. i32 (0), i32 (on), i32 (decay), i32 (-1), i32 (sus);
  11789. };
  11790. for (int i = 0; i < 240; ++i)
  11791. if (4 * i + 4 <= 0x320)
  11792. i32 (main[(std::size_t) i]);
  11793. b.resize (962, 0); // mute bytes: none muted
  11794. i32 (0); // global declick amount
  11795. b.push_back (0), b.push_back (0), b.push_back (0); // OSC grouping, DC stage, GIBBS OFF
  11796. for (int k = 0; k < 9; ++k)
  11797. b.push_back (k == 7 ? 1 : 0); // flag group (release multiplier on, as Default.fst)
  11798. for (const auto& d : delays)
  11799. b.insert (b.end(), d.begin(), d.end());
  11800. for (auto v : reverb)
  11801. i32 (v);
  11802. b.push_back (reverbOn ? 1 : 0), b.push_back (0);
  11803. i32 (eqmode);
  11804. for (int k = 0; k < 3; ++k)
  11805. i32 (0), i32 (30000), i32 (20000), i32 (k);
  11806. b.push_back (0); // no info text
  11807. for (int u = 0; u < 10; ++u)
  11808. {
  11809. const int narts = u < 6 ? 6 : (u < 9 ? 8 : 2);
  11810. for (int j = 0; j < 10; ++j)
  11811. i32 (u < 6 ? ops[u].p[j] : (u < 9 ? filts[u - 6].p[j] : 0));
  11812. if (u < 6)
  11813. {
  11814. const auto& o = ops[u];
  11815. const auto at = b.size();
  11816. b.resize (at + 0x828, 0);
  11817. auto put = [&] (std::size_t off, const void* src, std::size_t n) { std::memcpy (b.data() + at + off, src, n); };
  11818. put (0, &o.ratio, 4), put (4, &o.offset, 4), put (8, &o.noise, 4);
  11819. put (0xc, o.amps.data(), 4 * o.amps.size());
  11820. const std::int32_t shape[5] { 0, 0, 0, 0, 128 };
  11821. put (0x80c, shape, sizeof shape);
  11822. put (0x820, o.flags, 8);
  11823. }
  11824. else if (u < 9)
  11825. {
  11826. const auto at = b.size();
  11827. b.resize (at + 0x30, 0);
  11828. std::memcpy (b.data() + at + 0x2c, &filts[u - 6].type, 4);
  11829. env (3, { { 0.0, 0.0 }, { 1.0, 1.0 } }, 0, -1, -1);
  11830. b.push_back (0);
  11831. }
  11832. for (int k = 0; k < narts; ++k)
  11833. {
  11834. const bool add = k == 0 || k == 3 || (u >= 6 && u <= 8 && (k == 2 || k == 3)) || (u < 6 && k == 4);
  11835. const double neutral = add ? 0.5 : 1.0;
  11836. i32 (3), i32 (0), f32 (1.0f), f32 (1.0f);
  11837. if (u < 6 && k == 1 && ops[u].env1[0] != -1)
  11838. env (1, ops[u].env1pts, -1, ops[u].env1[1], ops[u].env1[2]);
  11839. else
  11840. env (1, { { 0.0, 0.0 }, { 1.0, 1.0 } }, 0, -1, -1);
  11841. i32 (128), i32 (128), i32 (0), i32 (128);
  11842. env (2, { { 0.0, 1.0 }, { 1.0, 1.0 } }, 0, -1, -1);
  11843. i32 (40000), i32 (0), i32 (0), i32 (0), i32 (0);
  11844. for (int m = 0; m < 6; ++m)
  11845. env (3, { { 0.0, neutral }, { 1.0, neutral } }, 0, -1, -1);
  11846. }
  11847. }
  11848. b.push_back (0), b.push_back (0);
  11849. // zlib with stored blocks
  11850. std::vector<std::uint8_t> z { 0x78, 0x01 };
  11851. for (std::size_t at = 0; at < b.size(); at += 65535)
  11852. {
  11853. const auto n = std::min<std::size_t> (65535, b.size() - at);
  11854. z.push_back (at + n >= b.size() ? 1 : 0);
  11855. z.push_back ((std::uint8_t) n), z.push_back ((std::uint8_t) (n >> 8));
  11856. z.push_back ((std::uint8_t) ~n), z.push_back ((std::uint8_t) (~n >> 8));
  11857. z.insert (z.end(), b.begin() + (std::ptrdiff_t) at, b.begin() + (std::ptrdiff_t) (at + n));
  11858. }
  11859. std::uint32_t a = 1, c = 0;
  11860. for (auto x : b)
  11861. a = (a + x) % 65521, c = (c + a) % 65521;
  11862. for (int k = 3; k >= 0; --k)
  11863. z.push_back ((std::uint8_t) (((c << 16) | a) >> (8 * k)));
  11864. std::vector<std::uint8_t> out { 25, 0, 0, 0 };
  11865. out.insert (out.end(), z.begin(), z.end());
  11866. return out;
  11867. }
  11868. };
  11869. // renders like FL's Gen_Render calls: notes start/release at the block that contains them
  11870. std::pair<std::vector<float>, std::vector<float>> sytrusRender (const std::vector<std::uint8_t>& blob, bool renderMode, int n,
  11871. std::vector<std::array<int, 3>> notes, double fs = 44100.0)
  11872. {
  11873. lynx::sytrus::Engine e;
  11874. e.setTempo (120.0);
  11875. e.prepare (fs, renderMode, 512);
  11876. e.setPatch (lynx::sytrus::parsePatch (blob));
  11877. std::vector<float> l ((std::size_t) n), r ((std::size_t) n);
  11878. for (int n0 = 0; n0 < n; n0 += 512)
  11879. {
  11880. for (std::size_t k = 0; k < notes.size(); ++k)
  11881. {
  11882. if (notes[k][1] >= n0 && notes[k][1] < n0 + 512)
  11883. {
  11884. lynx::sytrus::NoteParams p;
  11885. p.key = (float) notes[k][0];
  11886. p.id = (std::int64_t) k + 1;
  11887. e.noteOn (p);
  11888. }
  11889. if (notes[k][2] >= n0 && notes[k][2] < n0 + 512)
  11890. e.release ((std::int64_t) k + 1);
  11891. }
  11892. e.render (l.data() + n0, r.data() + n0, std::min (512, n - n0), 1.0f, 0.0f);
  11893. }
  11894. return { l, r };
  11895. }
  11896. } // namespace
  11897. TEST (sytrus_engine_sine)
  11898. {
  11899. // one sine operator, ratio 1, OUT 256, no envelopes: C5 = 523.2511 Hz at 0.7071 (centre of the pan law, §6.1)
  11900. SytrusTestPatch t;
  11901. t.cell (0, 0, 8, 256);
  11902. const auto blob = t.blob();
  11903. CHECK (lynx::sytrus::stateUsable (blob));
  11904. CHECK (lynx::sytrus::parsePatch (blob) != nullptr);
  11905. auto broken = blob;
  11906. broken.resize (broken.size() / 2);
  11907. CHECK (! lynx::sytrus::stateUsable (broken));
  11908. const auto [l, r] = sytrusRender (blob, false, 8192, { { 60, 0, 4096 } });
  11909. double worst = 0.0;
  11910. for (int k = 0; k < 4000; ++k)
  11911. 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)));
  11912. CHECK (worst < 2e-5);
  11913. CHECK (l == r);
  11914. // 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
  11915. // 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)
  11916. int last = 0;
  11917. for (int k = 0; k < 8192; ++k)
  11918. if (l[(std::size_t) k] != 0.0f)
  11919. last = k;
  11920. CHECK (last == 4895);
  11921. // key 69 = 880 Hz; 48 kHz
  11922. const auto [l2, r2] = sytrusRender (blob, false, 4096, { { 69, 0, -1 } }, 48000.0);
  11923. worst = 0.0;
  11924. for (int k = 0; k < 4000; ++k)
  11925. 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)));
  11926. CHECK (worst < 5e-5);
  11927. (void) r2;
  11928. }
  11929. TEST (sytrus_engine_reference)
  11930. {
  11931. // FM 2→1, Vanilla LP filter, volume envelope, unison 3, delay and reverb, 2× oversampling with the 8-sample
  11932. // envelope tick (render settings): against our Python reference model of REA_rodada31 (sytmodel.render)
  11933. SytrusTestPatch t;
  11934. t.main[195] = 0, t.main[196] = 1, t.main[193] = 0, t.main[194] = 1;
  11935. t.main[173] = 3, t.main[174] = 64, t.main[176] = 100;
  11936. t.ops[1].ratio = 40000;
  11937. t.cell (0, 0, 1, 128); // op 2 → op 1 FM
  11938. t.cell (0, 0, 8, 128);
  11939. t.cell (0, 6, 0, 256); // op 1 → filter 1
  11940. t.cell (0, 6, 8, 256);
  11941. t.cell (0, 0, 7, 200); // FX send
  11942. t.filts[0].p[0] = 1, t.filts[0].p[2] = 180, t.filts[0].p[3] = 60;
  11943. t.ops[0].env1[0] = 0, t.ops[0].env1[1] = 1, t.ops[0].env1[2] = 2;
  11944. t.ops[0].env1pts = { { 0.0, 0.0 }, { 0.05, 1.0 }, { 0.1, 0.6 }, { 0.2, 0.0 } };
  11945. const std::int32_t d0[5] { 40, 100, 50, 100, 0 };
  11946. std::memcpy (t.delays[0].data(), d0, 20);
  11947. t.delays[0][22] = 1;
  11948. t.reverb = { 19, 221, 10, 50, 80, 30, 100, 2, 100 };
  11949. t.reverbOn = true;
  11950. const auto blob = t.blob();
  11951. const auto [l, r] = sytrusRender (blob, true, 12000, { { 60, 0, 6000 } });
  11952. // reference values (model render_mode=True, 44.1 kHz, block 512, key 60, velocity 0.78125, release at 6000); the same
  11953. // state rendered by FL's own Sytrus DLL in render mode differs from this by −136 dB (EXEC, REA round 31 harness)
  11954. const std::array<std::pair<int, float>, 8> golden { { { 100, -0.00235529384f }, { 777, -0.175126165f }, { 2000, 0.30976975f }, { 4321, -0.0213534199f },
  11955. { 6100, 0.202372357f }, { 7000, 0.249530748f }, { 9000, 0.0307101421f }, { 11999, 0.176390275f } } };
  11956. for (const auto& [i, v] : golden)
  11957. CHECK (std::abs (l[(std::size_t) i] - v) < 1e-6f);
  11958. const std::array<std::pair<int, float>, 3> goldenR { { { 777, -0.166890964f }, { 4321, -0.212358952f }, { 9000, -0.0251909085f } } };
  11959. for (const auto& [i, v] : goldenR)
  11960. CHECK (std::abs (r[(std::size_t) i] - v) < 1e-6f);
  11961. }
  11962. TEST (sytrus_instrument)
  11963. {
  11964. // the LYNX instrument: state in ChannelDef::pluginState, channel volume/pan recovered from the rack gains
  11965. SytrusTestPatch t;
  11966. t.cell (0, 0, 8, 256);
  11967. lynx::ChannelDef c;
  11968. c.instrument = lynx::InstrumentKind::sytrus;
  11969. c.pluginState = t.blob();
  11970. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sytrus);
  11971. inst->prepare (44100.0);
  11972. inst->configure (c, 120.0);
  11973. CHECK (inst->idle());
  11974. std::vector<float> l (4096), r (4096);
  11975. inst->noteOn (60, 100.0f / 128.0f);
  11976. // 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
  11977. inst->render (l.data(), r.data(), 4096, 0.5f, 0.5f);
  11978. double peak = 0.0;
  11979. for (int k = 1000; k < 4096; ++k)
  11980. peak = std::max (peak, (double) std::abs (l[(std::size_t) k]));
  11981. CHECK (std::abs (peak - 0.5 * 0.70710678) < 1e-3); // Final.Vol 0.5 × the plugin's centre pan law
  11982. CHECK (! inst->idle());
  11983. inst->noteOff (60);
  11984. std::vector<float> tail (44100 * 3), tailR (tail.size());
  11985. inst->render (tail.data(), tailR.data(), (int) tail.size(), 0.5f, 0.5f);
  11986. CHECK (inst->idle());
  11987. CHECK (tail.back() == 0.0f);
  11988. }
  11989. TEST (harmor_installation_presets)
  11990. {
  11991. // REA_rodada165g-flbeta165s: every Harmor preset of the installation plays on the exact engine — the 88 old
  11992. // (version 11…15) ones through the upgrade to the current layout, which renders exactly like the old bytes and
  11993. // keeps the parameter offsets editable; small images (Labyrinth's 16 × 12) are played, not skipped
  11994. if (realFlpDir.empty())
  11995. return;
  11996. const auto folder = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "Harmor";
  11997. if (! std::filesystem::exists (folder))
  11998. return;
  11999. int files = 0, usable = 0, old = 0, upgraded = 0, same = 0;
  12000. const std::vector<lynx::harmor::RefNote> notes { { 0, 60, 0.8, 1.0, 0.0, 3000 } };
  12001. for (const auto& entry : std::filesystem::recursive_directory_iterator (folder))
  12002. {
  12003. if (entry.path().extension() != ".fst")
  12004. continue;
  12005. std::ifstream in (entry.path(), std::ios::binary);
  12006. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  12007. std::vector<std::uint8_t> st;
  12008. try
  12009. {
  12010. for (const auto& c : flp::parseProject (file, true).channels)
  12011. if (! c.pluginState.empty())
  12012. st = c.pluginState;
  12013. }
  12014. catch (const flp::Error&)
  12015. {
  12016. }
  12017. ++files;
  12018. usable += lynx::harmorStateUsable (st);
  12019. std::uint32_t v = 0;
  12020. if (st.size() >= 4)
  12021. std::memcpy (&v, st.data(), 4);
  12022. if ((v > 0xffff ? v >> 16 : v) >= 16)
  12023. continue;
  12024. ++old;
  12025. const auto up = lynx::harmorUpgradeState (st);
  12026. if (! up)
  12027. continue;
  12028. std::uint32_t uv = 0;
  12029. std::memcpy (&uv, up->data(), 4);
  12030. upgraded += uv == 0x130003u && lynx::harmorParamOffset (*up, 0, 34) > 0 && lynx::harmorStateUsable (*up);
  12031. if (old <= 6) // (a few: rendering is the slow part)
  12032. same += lynx::harmor::renderReference (st, notes, 44100.0, 4096) == lynx::harmor::renderReference (*up, notes, 44100.0, 4096);
  12033. else
  12034. ++same;
  12035. }
  12036. std::printf (" Harmor installation presets: %d files, %d usable, %d old, %d upgraded\n", files, usable, old, upgraded);
  12037. CHECK (files >= 300 && usable == files && old >= 80 && upgraded == old && same == old);
  12038. std::ifstream in (folder / "Bell" / "Labyrinth.fst", std::ios::binary);
  12039. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  12040. if (file.empty())
  12041. return;
  12042. auto st = flp::parseProject (file, true).channels.at (0).pluginState;
  12043. const auto a = lynx::harmor::renderReference (st, notes, 44100.0, 4096);
  12044. const std::int32_t zero = 0;
  12045. for (int part = 0; part < 2; ++part)
  12046. std::memcpy (st.data() + lynx::harmorParamOffset (st, part, 92), &zero, 4); // image frequency scale 0
  12047. const auto b = lynx::harmor::renderReference (st, notes, 44100.0, 4096);
  12048. CHECK (a.size() == b.size() && a != b);
  12049. }
  12050. TEST (harmor_image)
  12051. {
  12052. // REA_rodada75: image synthesis — a looping 6-column image (gain + frequency planes) read at a speed, cubic gain and
  12053. // linear frequency interpolation, Hz frequency mode, formant shift — against our reference model (r75 imgm.py);
  12054. // since flbeta165h LYNX's own values: the test state plays at precision "Average" (global misc +28 = 0), whose
  12055. // IPP 11-bit Exp/Ln (REA_rodada165g-flbeta165h §3) the model computes exactly
  12056. const auto blob = harmorTestState (false, true);
  12057. if (const char* dump = std::getenv ("LYNX_DUMP_HARMOR_IMAGE"))
  12058. std::ofstream (dump, std::ios::binary).write ((const char*) blob.data(), (std::streamsize) blob.size());
  12059. CHECK (lynx::harmorStateUsable (blob));
  12060. CHECK (lynx::harmorFallbacks (blob).empty()); // (the image is modelled)
  12061. 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);
  12062. CHECK (ref.size() == 2 * 8192);
  12063. if (std::getenv ("LYNX_PRINT_HARMOR_IMAGE") != nullptr)
  12064. for (int i : { 100, 1000, 2500, 3000, 5000, 6500, 8191 })
  12065. std::printf ("harmor image %d %.8g %.8g\n", i, (double) ref[2 * (std::size_t) i], (double) ref[2 * (std::size_t) i + 1]);
  12066. const std::tuple<int, float, float> golden[] {
  12067. { 100, 0.0022266121f, 0.018016478f }, { 1000, 0.013964245f, -0.028604792f }, { 2500, 0.016782919f, -0.028934296f },
  12068. { 3000, -0.010533399f, -0.0061015566f }, { 5000, -0.017427528f, 0.016475817f }, { 6500, 0.16895676f, 0.078423217f },
  12069. { 8191, -0.045406573f, -0.049047086f } };
  12070. for (const auto& [i, gl, gr] : golden)
  12071. CHECK (std::abs (ref[2 * (std::size_t) i] - gl) < 2e-6f && std::abs (ref[2 * (std::size_t) i + 1] - gr) < 2e-6f);
  12072. // the image changes the sound: without it (gain mix 0, frequency scale 0) the same notes render differently
  12073. auto plain = blob;
  12074. const std::int32_t zero = 0;
  12075. std::memcpy (plain.data() + lynx::harmorParamOffset (plain, 0, 89), &zero, 4);
  12076. std::memcpy (plain.data() + lynx::harmorParamOffset (plain, 0, 92), &zero, 4);
  12077. std::memcpy (plain.data() + lynx::harmorParamOffset (plain, 0, 94), &zero, 4);
  12078. 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);
  12079. double diff = 0.0, energy = 0.0;
  12080. for (std::size_t i = 0; i < ref.size(); ++i)
  12081. diff += (double) (ref[i] - ref0[i]) * (ref[i] - ref0[i]), energy += (double) ref[i] * ref[i];
  12082. CHECK (energy > 1.0 && diff > 0.01 * energy);
  12083. // a resynthesis-mode image (a sample analysed at load time) is not modelled: reported, the part plays its timbres
  12084. auto resyn = blob;
  12085. {
  12086. // the image object follows the 26 curves: find its header (version 0, mode 0, loop 1) from the end of part A
  12087. 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 };
  12088. const auto it = std::search (resyn.begin(), resyn.end(), std::begin (hdr), std::end (hdr));
  12089. CHECK (it != resyn.end());
  12090. if (it != resyn.end())
  12091. *(it + 4) = 1;
  12092. }
  12093. const auto fb = lynx::harmorFallbacks (resyn);
  12094. CHECK (std::any_of (fb.begin(), fb.end(), [] (const std::string& f) { return f.find ("resynthesis") != std::string::npos; }));
  12095. }
  12096. TEST (harmor_round75_features)
  12097. {
  12098. // REA_rodada75: arpeggio markers, noise resonance, pluck blur mode, pluck blend, harmonic randomness, phaser and
  12099. // harmonizer masks — LYNX's engine against our reference model (r75 hmodel.py, the Mersenne Twister seeded like LYNX's);
  12100. // since flbeta165h LYNX's own values: the test state plays at precision "Average", whose IPP 11-bit Exp/Ln
  12101. // (REA_rodada165g-flbeta165h §3) the model computes exactly
  12102. 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 } };
  12103. struct Case { int features; std::array<std::tuple<int, float, float>, 4> golden; };
  12104. const Case cases[] {
  12105. { 1, { { { 2000, 0.0032625794f, -0.0014364491f }, { 4500, 0.10464316f, 0.026205163f }, { 7000, -0.0024230368f, 0.0036458215f }, { 11000, -0.0029985344f, -0.0030873856f } } } },
  12106. { 2, { { { 2000, 0.29411051f, -0.22980602f }, { 4500, -0.004929353f, -0.0013274907f }, { 7000, 0.013239861f, 0.0054404787f }, { 11000, -0.0010576053f, -0.0021088289f } } } },
  12107. { 4, { { { 2000, 0.072069392f, -0.029683406f }, { 4500, -0.10765476f, 0.11332012f }, { 7000, -0.22984716f, -0.10102195f }, { 11000, -0.051904719f, -0.066571109f } } } },
  12108. { 8, { { { 2000, 0.072010428f, -0.029725688f }, { 4500, -0.10748001f, 0.1136639f }, { 7000, -0.22849904f, -0.15257275f }, { 11000, -0.041248851f, -0.050450616f } } } },
  12109. { 16, { { { 2000, 0.063442282f, -0.030946331f }, { 4500, -0.11211193f, 0.11279358f }, { 7000, -0.23168804f, -0.1670858f }, { 11000, -0.041186355f, -0.050383076f } } } },
  12110. { 32, { { { 2000, 0.086303107f, -0.035885293f }, { 4500, 0.069407858f, 0.14133474f }, { 7000, -0.15907738f, -0.10380846f }, { 11000, -0.029428774f, -0.041782599f } } } },
  12111. { 63, { { { 2000, 0.017201126f, -0.010683246f }, { 4500, 0.55659908f, 0.08110752f }, { 7000, -0.0025542902f, 0.020610567f }, { 11000, -0.0031871174f, 0.00024673535f } } } } };
  12112. for (const auto& c : cases)
  12113. {
  12114. const auto blob = harmorTestState (false, false, c.features);
  12115. if (const char* dump = std::getenv ("LYNX_DUMP_HARMOR_R75"))
  12116. std::ofstream (std::string (dump) + std::to_string (c.features) + ".bin", std::ios::binary).write ((const char*) blob.data(), (std::streamsize) blob.size());
  12117. CHECK (lynx::harmorStateUsable (blob));
  12118. CHECK (lynx::harmorFallbacks (blob).empty());
  12119. const auto ref = lynx::harmor::renderReference (blob, chord, 44100.0, 12000);
  12120. if (std::getenv ("LYNX_PRINT_HARMOR_R75") != nullptr)
  12121. for (int i : { 2000, 4500, 7000, 11000 })
  12122. 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]);
  12123. for (const auto& [i, gl, gr] : c.golden)
  12124. CHECK (std::abs (ref[2 * (std::size_t) i] - gl) < 2e-6f && std::abs (ref[2 * (std::size_t) i + 1] - gr) < 2e-6f);
  12125. }
  12126. }
  12127. TEST (sytrus_main_settings)
  12128. {
  12129. // the channel settings panel's controls (REA_rodada74 §3): FL's "Main settings" ints inside the zlib state
  12130. SytrusTestPatch t;
  12131. t.cell (0, 0, 8, 256);
  12132. t.main[173] = 3;
  12133. const auto blob = t.blob();
  12134. const auto& S = lynx::sytrus::settings();
  12135. auto find = [&] (const char* name) {
  12136. for (int i = 0; i < (int) S.size(); ++i)
  12137. if (std::string (S[(std::size_t) i].name) == name)
  12138. return i;
  12139. return -1;
  12140. };
  12141. const int master = find ("Master level"), unison = find ("Unison"), porta = find ("Porta"), live = find ("Live oversampling");
  12142. // 25 main controls, then the matrix (round 172a): FM 9 × 9 (sources, PAN, FX, OUT) and RM 9 × 6
  12143. CHECK (master >= 0 && unison >= 0 && porta >= 0 && live >= 0 && S.size() == 25u + 81u + 54u);
  12144. const int out1 = find ("Op 1 out"), fm21 = find ("FM op 2 to op 1"), rm3f2 = find ("RM op 3 to filter 2");
  12145. CHECK (out1 >= 0 && fm21 >= 0 && rm3f2 >= 0 && find ("RM op 1 pan") < 0);
  12146. CHECK (out1 >= 0 && S[(std::size_t) out1].index == 3 + 8 && S[(std::size_t) rm3f2].index == 3 + 81 + 9 * 7 + 2);
  12147. CHECK (lynx::sytrus::settingValue (blob, out1) == 256 && lynx::sytrus::settingValue (blob, fm21) == 0);
  12148. CHECK (lynx::sytrus::settingValue (blob, master) == 128 && lynx::sytrus::settingValue (blob, unison) == 3);
  12149. CHECK (lynx::sytrus::settingValue (blob, porta) == 0 && lynx::sytrus::settingValue (blob, find ("Mod X")) == 256);
  12150. CHECK (! lynx::sytrus::settingValue (blob, -1) && ! lynx::sytrus::settingValue (blob, (int) S.size()));
  12151. auto broken = blob;
  12152. broken.resize (broken.size() / 2);
  12153. CHECK (! lynx::sytrus::settingValue (broken, master) && ! lynx::sytrus::setSetting (broken, master, 64));
  12154. // master level 64: gain (11^(64/128) − 1)/10 instead of 1 (REA_rodada11 §1.3); the state stays readable
  12155. auto quiet = blob;
  12156. CHECK (lynx::sytrus::setSetting (quiet, master, 64) && lynx::sytrus::stateUsable (quiet));
  12157. CHECK (lynx::sytrus::settingValue (quiet, master) == 64 && lynx::sytrus::settingValue (quiet, unison) == 3);
  12158. CHECK (quiet[0] == blob[0] && quiet[1] == blob[1]); // version word kept
  12159. const auto [l1, r1] = sytrusRender (blob, false, 4096, { { 60, 0, -1 } });
  12160. const auto [l2, r2] = sytrusRender (quiet, false, 4096, { { 60, 0, -1 } });
  12161. const auto g = (std::sqrt (11.0) - 1.0) / 10.0;
  12162. double worst = 0.0;
  12163. for (int k = 0; k < 4096; ++k)
  12164. worst = std::max (worst, std::abs ((double) l2[(std::size_t) k] - g * l1[(std::size_t) k]));
  12165. CHECK (worst < 1e-5);
  12166. (void) r1, (void) r2;
  12167. // clamped to the control's range; PORTA is the flag byte the engine reads
  12168. auto edited = blob;
  12169. CHECK (lynx::sytrus::setSetting (edited, unison, 42) && lynx::sytrus::settingValue (edited, unison) == 9);
  12170. CHECK (lynx::sytrus::setSetting (edited, porta, 1) && lynx::sytrus::settingValue (edited, porta) == 1);
  12171. CHECK (lynx::sytrus::setSetting (edited, live, 2) && lynx::sytrus::settingValue (edited, live) == 2);
  12172. CHECK (lynx::sytrus::parsePatch (edited) != nullptr);
  12173. // a matrix cell: op 2 modulates op 1 (FM), clamped to ±256; the patch stays playable
  12174. CHECK (lynx::sytrus::setSetting (edited, fm21, 999) && lynx::sytrus::settingValue (edited, fm21) == 256);
  12175. CHECK (lynx::sytrus::settingValue (edited, out1) == 256 && lynx::sytrus::parsePatch (edited) != nullptr);
  12176. }
  12177. TEST (harmor_effects_rack)
  12178. {
  12179. // REA_rodada38: the rack runs in the state's unit order and the records' FX send joins it at unit 4 — with the FX
  12180. // amount at 1000 every record output bypasses a reverb placed before unit 4 (EXEC: −600 dB vs the rack off)
  12181. auto blob = harmorTestState (false);
  12182. auto set = [&] (std::vector<std::uint8_t>& st, int part, int index, std::int32_t v) {
  12183. std::memcpy (st.data() + lynx::harmorParamOffset (st, part, index), &v, 4);
  12184. };
  12185. set (blob, 0, 10, 1000); // all to the FX send
  12186. auto render = [] (const std::vector<std::uint8_t>& st) {
  12187. return lynx::harmor::renderReference (st, { { 0, 60, 0.8, 1.0, 0.0, 6000 } }, 44100.0, 12000);
  12188. };
  12189. const auto dry = render (blob);
  12190. auto rev = blob;
  12191. set (rev, -1, 22, 1); // reverb on: room 50, diffusion 100, decay 2^3·0.1 s, high cut / damping off, wet 0.8
  12192. 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);
  12193. set (rev, -1, 30, 2), set (rev, -1, 31, 800);
  12194. const auto bypassed = render (rev);
  12195. double eb = 0.0, db = 0.0;
  12196. for (std::size_t i = 0; i < dry.size(); ++i)
  12197. eb += (double) (bypassed[i] - dry[i]) * (bypassed[i] - dry[i]), db += (double) dry[i] * dry[i];
  12198. CHECK (eb < 1e-12 * db && db > 1e-3); // (float rounding of the rack's buffers only)
  12199. auto after = rev;
  12200. const std::int32_t order[6] { 0, 1, 2, 4, 3, 5 }; // the join before the reverb
  12201. for (int k = 0; k < 6; ++k)
  12202. set (after, -1, 53 + k, order[k]);
  12203. const auto wet = render (after);
  12204. double e = 0.0, d = 0.0;
  12205. bool finite = true;
  12206. for (std::size_t i = 0; i < wet.size(); ++i)
  12207. e += (double) (wet[i] - dry[i]) * (wet[i] - dry[i]), d += (double) dry[i] * dry[i], finite = finite && std::isfinite (wet[i]);
  12208. CHECK (finite && e > 1e-4 * d && d > 1e-3);
  12209. // a distortion (type 6) and a delay on the dry path change the sound, stay finite, and the instrument ends quiet
  12210. auto dist = harmorTestState (false);
  12211. {
  12212. const auto at = lynx::harmorParamOffset (dist, -1, 0) + 0xd4 + 0x18 + 48; // global misc +48: distortion type
  12213. const std::int32_t six = 6;
  12214. std::memcpy (dist.data() + at, &six, 4);
  12215. }
  12216. set (dist, -1, 17, 600), set (dist, -1, 19, 1000), set (dist, -1, 21, 1100);
  12217. 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);
  12218. set (dist, -1, 47, 1050);
  12219. lynx::ChannelDef c;
  12220. c.instrument = lynx::InstrumentKind::harmor;
  12221. c.pluginState = dist;
  12222. auto inst = lynx::makeInstrument (lynx::InstrumentKind::harmor);
  12223. inst->configure (c, 120.0);
  12224. inst->prepare (44100.0);
  12225. std::vector<float> l (44100 * 4), r (l.size());
  12226. inst->noteOn (60, 0.8f);
  12227. double energy = 0.0;
  12228. for (int s = 0; s < (int) l.size(); s += 512)
  12229. {
  12230. if (s == 8192)
  12231. inst->noteOff (60);
  12232. inst->render (l.data() + s, r.data() + s, std::min (512, (int) l.size() - s), 1.0f, 1.0f);
  12233. }
  12234. for (std::size_t i = 0; i < l.size(); ++i)
  12235. finite = finite && std::isfinite (l[i]) && std::isfinite (r[i]), energy += (double) l[i] * l[i];
  12236. CHECK (finite && energy > 1.0);
  12237. std::vector<float> tail (44100 * 6), tailR (tail.size());
  12238. for (int s = 0; s < (int) tail.size(); s += 512)
  12239. inst->render (tail.data() + s, tailR.data() + s, std::min (512, (int) tail.size() - s), 1.0f, 1.0f);
  12240. CHECK (inst->idle());
  12241. }
  12242. TEST (harmless_engine)
  12243. {
  12244. // REA_rodada32: state parsing, the additive engine against our reference model (hlmodel.py), host levels, voice end
  12245. std::vector<std::uint8_t> blob;
  12246. 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))); };
  12247. std::array<std::int32_t, 100> p {}; // the reference's clean patch: Saw, open filter, no modulation
  12248. for (int i = 11; i < 23; ++i)
  12249. p[(std::size_t) i] = 1000;
  12250. 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;
  12251. 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;
  12252. // + resonance, pluck, 3-voice unison, tremolo, LFO on pitch
  12253. 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;
  12254. put (12);
  12255. for (auto v : p)
  12256. put (v);
  12257. for (int i = 0; i < 7; ++i)
  12258. put (0);
  12259. put (524); // Custom colour table: 524 × (amplitude, phase)
  12260. for (int i = 0; i < 1048; ++i)
  12261. {
  12262. const auto f = i < 524 ? 1.0f / (float) (i + 1) : 0.25f;
  12263. std::uint32_t bits = 0;
  12264. std::memcpy (&bits, &f, 4);
  12265. put ((std::int32_t) bits);
  12266. }
  12267. const auto parsed = lynx::harmlessParams (blob);
  12268. CHECK (parsed && (int) parsed->size() == lynx::harmlessParamCount);
  12269. CHECK (parsed && (*parsed)[54] == 3500.0f && (*parsed)[99] == 0.0f && (*parsed)[lynx::harmlessCustomFlagIndex] == 1.0f);
  12270. CHECK (parsed && (*parsed)[lynx::harmlessCustomIndex + 1] == 0.5f && (*parsed)[lynx::harmlessCustomIndex + 524] == 0.25f);
  12271. auto old = blob;
  12272. old[0] = 3; // versions below 4 are not Harmless states we know
  12273. CHECK (! lynx::harmlessParams (old));
  12274. old = blob;
  12275. old.resize (300);
  12276. CHECK (! lynx::harmlessParams (old));
  12277. lynx::ChannelDef c;
  12278. c.instrument = lynx::InstrumentKind::harmless;
  12279. c.modelParams = *parsed;
  12280. c.modelParams[lynx::harmlessCustomFlagIndex] = 0.0f; // colour 0 (Saw): the custom table is not read
  12281. auto render = [] (const lynx::ChannelDef& def, int total, float gl, float gr, int offAt) {
  12282. auto inst = lynx::makeInstrument (lynx::InstrumentKind::harmless);
  12283. inst->configure (def, 120.0);
  12284. inst->prepare (44100.0);
  12285. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  12286. inst->noteOn (60, 0.78125f);
  12287. for (int s = 0; s < total; s += 512)
  12288. {
  12289. if (s == offAt)
  12290. inst->noteOff (60);
  12291. inst->render (l.data() + s, r.data() + s, std::min (512, total - s), gl, gr);
  12292. }
  12293. return std::make_tuple (l, r, std::move (inst));
  12294. };
  12295. {
  12296. // reference model (REA round 32 hlmodel.py, C5 at FL velocity 100, channel volume 1, centre, released at 6144)
  12297. auto [l, r, inst] = render (c, 8192, 1.0f, 1.0f, 6144);
  12298. const std::tuple<int, float, float> golden[] {
  12299. { 0, 0.0f, 0.0f }, { 10, -0.02220994f, -0.06232700f }, { 100, 0.40623373f, 0.17308202f }, { 1000, -0.04091963f, -0.26821208f },
  12300. { 3000, 0.14519004f, 0.11027778f }, { 6000, 0.50411958f, 0.27901855f }, { 8191, 0.02117692f, -0.01466602f } };
  12301. for (const auto& [i, gl, gr] : golden)
  12302. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  12303. // after the release the record fades out (below 1e-4, then 30 ms) and the instrument goes idle
  12304. std::vector<float> tail (44100), tailR (tail.size());
  12305. inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
  12306. CHECK (inst->idle());
  12307. }
  12308. // FL's level law applied once: half the channel volume halves the output; full right pan silences the left side
  12309. const auto full = std::get<0> (render (c, 2048, 1.0f, 1.0f, -1));
  12310. const auto half = std::get<0> (render (c, 2048, 0.5f, 0.5f, -1));
  12311. CHECK (std::abs (half[100] - 0.5f * full[100]) < 1e-6f && std::abs (full[100]) > 0.1f);
  12312. auto mono = c;
  12313. mono.modelParams[25] = 0.0f; // no tremolo, no unison pan spread
  12314. mono.modelParams[65] = 1.0f;
  12315. const auto [pl, pr, pi] = render (mono, 2048, 0.0f, 1.41421356f, -1);
  12316. double el = 0.0, er = 0.0;
  12317. for (int i = 0; i < 2048; ++i)
  12318. el += (double) pl[(std::size_t) i] * pl[(std::size_t) i], er += (double) pr[(std::size_t) i] * pr[(std::size_t) i];
  12319. CHECK (el < 1e-10 * er && er > 1.0);
  12320. }
  12321. TEST (startup_sound)
  12322. {
  12323. // the splash's sound: the right length, finite, at its peak level, quiet before the whoosh swells,
  12324. // a landing "pop" at impactTime and a chime ringing after it
  12325. using namespace lynx::startup;
  12326. const auto sound = makeSound (48000.0);
  12327. CHECK (sound.left.size() == (std::size_t) (soundLength * 48000.0) && sound.right.size() == sound.left.size());
  12328. float peak = 0.0f;
  12329. bool finite = true;
  12330. for (std::size_t i = 0; i < sound.left.size(); ++i)
  12331. {
  12332. finite = finite && std::isfinite (sound.left[i]) && std::isfinite (sound.right[i]);
  12333. peak = std::max ({ peak, std::abs (sound.left[i]), std::abs (sound.right[i]) });
  12334. }
  12335. CHECK (finite && std::abs (peak - peakLevel) < 1.0e-4f);
  12336. auto rms = [&sound] (double from, double to) {
  12337. double e = 0.0;
  12338. const auto a = (std::size_t) (from * 48000.0), b = (std::size_t) (to * 48000.0);
  12339. for (auto i = a; i < b; ++i)
  12340. e += (double) sound.left[i] * sound.left[i];
  12341. return std::sqrt (e / (double) (b - a));
  12342. };
  12343. CHECK (rms (0.0, 0.05) < 0.2 * rms (impactTime - 0.05, impactTime)); // the whoosh swells
  12344. CHECK (rms (impactTime, impactTime + 0.05) > rms (impactTime - 0.05, impactTime)); // the landing
  12345. CHECK (rms (0.8, 1.0) > 0.005 && rms (soundLength - 0.02, soundLength) < rms (0.8, 1.0)); // chime rings, tail fades
  12346. CHECK (makeSound (48000.0).left == sound.left); // the same sound on every start
  12347. }
  12348. namespace
  12349. {
  12350. // little-endian byte writer for the state builders below
  12351. struct Bytes
  12352. {
  12353. std::vector<std::uint8_t> b;
  12354. void raw (const char* s, std::size_t n) { b.insert (b.end(), s, s + n); }
  12355. 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); }
  12356. void f32 (float v) { b.insert (b.end(), reinterpret_cast<const std::uint8_t*> (&v), reinterpret_cast<const std::uint8_t*> (&v) + 4); }
  12357. void zeros (std::size_t n) { b.insert (b.end(), n, 0); }
  12358. void bytes (const std::vector<std::uint8_t>& o) { b.insert (b.end(), o.begin(), o.end()); }
  12359. };
  12360. // A GMS state written from scratch (REA_rodada12 §1.1 layout, "Syn4"): oscillator waves that don't exist (silent
  12361. // tables, so the result does not depend on an FL installation), noise through the ladder with envelope and LFO,
  12362. // unison, slide, distortion, delay, reverb and the output EQ.
  12363. std::vector<std::uint8_t> gmsTestState()
  12364. {
  12365. std::array<float, 212> P {};
  12366. for (int i : { 1, 2, 44, 55, 56, 57, 58, 59, 60, 207, 208, 209, 210, 211 })
  12367. P[(std::size_t) i] = 0.5f;
  12368. P[0] = 0.75f, P[4] = 0.1f, P[5] = 0.4f, P[6] = 0.6f, P[7] = 0.3f, P[8] = 0.8f;
  12369. 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;
  12370. 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;
  12371. P[147] = 1.0f, P[117] = 0.3f, P[127] = 0.6f; // distortion
  12372. P[153] = 1.0f, P[123] = 0.5f, P[133] = 0.4f, P[143] = 0.4f; // delay (1/2 beat)
  12373. P[154] = 1.0f, P[124] = 0.6f, P[134] = 0.5f; // reverb
  12374. P[206] = 1.0f, P[207] = 0.7f, P[211] = 0.3f; // output EQ
  12375. Bytes preset;
  12376. preset.raw ("4nyS", 4);
  12377. for (const char* path : { "NoSuchDir\\NoWave A.wav", "NoSuchDir\\NoWave B.wav", "NoSuchDir\\NoWave C.wav", "", "", "", "" })
  12378. {
  12379. const auto n = std::strlen (path);
  12380. preset.raw (path, n);
  12381. preset.zeros (512 - n);
  12382. }
  12383. preset.i32 (0), preset.i32 (0), preset.i32 (0);
  12384. for (auto v : P)
  12385. preset.f32 (v);
  12386. preset.raw ("LYNX test", 9);
  12387. preset.zeros (512 - 9);
  12388. Bytes s;
  12389. s.raw ("SSLF", 4);
  12390. s.i32 ((std::int32_t) preset.b.size());
  12391. s.bytes (preset.b);
  12392. return s.b;
  12393. }
  12394. // A Morphine state written from scratch (REA_rodada16 §3 / REA_rodada39 §2.2 layout): generators A and B with a few
  12395. // frames each (A loops, PWM on A), C and D silent, a morph line A→B, four envelopes (Eg1 → pitch and velocity →
  12396. // amplitude through the matrix), the master ADSR and every plugin effect on.
  12397. std::vector<std::uint8_t> morphineTestState()
  12398. {
  12399. auto mbp = [] (int seed) {
  12400. Bytes b;
  12401. b.raw ("0PBM", 4);
  12402. for (int a = 0; a < 3; ++a)
  12403. for (int byte = 0; byte < 16; ++byte)
  12404. {
  12405. b.b.push_back (0xff);
  12406. for (int bit = 0; bit < 8; ++bit)
  12407. {
  12408. const auto k = byte * 8 + bit;
  12409. // (cbrtf is not correctly rounded before glibc 2.43 and differs between libms; the cube root in double
  12410. // rounded to float gives the same (correctly rounded) bits with old and new glibc)
  12411. const float v = a == 0 ? (float) std::cbrt ((double) (1.0f / (float) (k + 1 + seed * (k % 3)))) * (k < 40 ? 1.0f : 0.2f)
  12412. : a == 1 ? 0.1f * (float) ((k * 7 + seed) % 11 - 5) / 5.0f
  12413. : 0.02f * (float) ((k * 5 + seed) % 7 - 3);
  12414. b.f32 (v);
  12415. }
  12416. }
  12417. return b.b;
  12418. };
  12419. auto msp = [&] (int frames, int seed, int loopMode) {
  12420. Bytes b;
  12421. b.raw ("0PSM", 4);
  12422. b.i32 (frames);
  12423. for (int f = 0; f < frames; ++f)
  12424. {
  12425. const auto m = mbp (seed + f);
  12426. b.i32 (20 + 7 * f), b.f32 (1.0f - 0.2f * (float) f), b.i32 ((std::int32_t) m.size());
  12427. b.bytes (m);
  12428. }
  12429. b.i32 (0x424);
  12430. b.i32 (10), b.i32 (40), b.i32 (0), b.i32 (loopMode);
  12431. b.f32 (0.45f), b.f32 (0.6f), b.f32 (0.5f), b.i32 (0);
  12432. for (int k = 0; k < 128; ++k)
  12433. b.f32 (0.3f);
  12434. for (int k = 0; k < 128; ++k)
  12435. b.f32 (1.0f);
  12436. return b.b;
  12437. };
  12438. auto mvc = [&] (int frames, int seed, int pwmMode) {
  12439. Bytes z;
  12440. z.raw ("0HZM", 4);
  12441. z.i32 (1);
  12442. const auto s = msp (frames, seed, 1);
  12443. z.i32 (128), z.f32 (frames > 1 ? 0.9f : 0.0f), z.i32 ((std::int32_t) s.size());
  12444. z.bytes (s);
  12445. Bytes b;
  12446. b.raw ("0CVM", 4);
  12447. b.i32 ((std::int32_t) z.b.size());
  12448. b.bytes (z.b);
  12449. 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);
  12450. return b.b;
  12451. };
  12452. Bytes p;
  12453. p.raw ("0roM", 4);
  12454. p.raw ("0RPM", 4);
  12455. for (const auto& g : { mvc (3, 1, 2), mvc (2, 7, 0), mvc (1, 3, 0), mvc (1, 5, 0) })
  12456. {
  12457. p.i32 ((std::int32_t) g.size());
  12458. p.bytes (g);
  12459. }
  12460. {
  12461. Bytes m;
  12462. m.raw ("0LMM", 4);
  12463. m.i32 (2), m.i32 (12);
  12464. m.f32 (0.0f), m.f32 (1.0f), m.i32 (60);
  12465. m.f32 (1.0f), m.f32 (1.0f), m.i32 (60);
  12466. m.i32 (0x54), m.i32 (0), m.i32 (0), m.i32 (0), m.i32 (0);
  12467. for (float v : { 1.0f, 0.8f, 0.0f, 0.0f, 0.5f, 0.4f, 0.5f, 0.5f })
  12468. m.f32 (v);
  12469. for (int k = 0; k < 4; ++k)
  12470. m.i32 (0);
  12471. m.f32 (0.5f), m.f32 (0.5f), m.f32 (0.5f), m.i32 (0);
  12472. p.i32 ((std::int32_t) m.b.size());
  12473. p.bytes (m.b);
  12474. }
  12475. {
  12476. Bytes d;
  12477. d.raw ("0DMM", 4);
  12478. d.i32 (4);
  12479. for (int e = 0; e < 4; ++e)
  12480. {
  12481. Bytes g;
  12482. g.raw ("0GEM", 4);
  12483. g.i32 (3), g.i32 (8);
  12484. g.f32 (0.0f), g.i32 (10), g.f32 (1.0f), g.i32 (40 + 10 * e), g.f32 (0.3f), g.i32 (50);
  12485. 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),
  12486. g.i32 (e == 0 ? 1 : 0);
  12487. d.i32 ((std::int32_t) g.b.size());
  12488. d.bytes (g.b);
  12489. }
  12490. d.i32 (0x94);
  12491. const int src[12] = { 5, 2 }, dst[12] = { 3, 1 };
  12492. const float amt[12] = { 0.56f, 0.7f };
  12493. for (int k = 0; k < 12; ++k)
  12494. d.i32 (src[k]);
  12495. for (int k = 0; k < 12; ++k)
  12496. d.i32 (dst[k]);
  12497. for (int k = 0; k < 12; ++k)
  12498. d.f32 (amt[k]);
  12499. p.i32 ((std::int32_t) d.b.size());
  12500. p.bytes (d.b);
  12501. }
  12502. {
  12503. 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
  12504. 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 …
  12505. 0.4f, 0.3f, 1.0f, 0.5f, 0.5f, 0.4f, 1.0f, 0.6f, 0.4f, 0.5f, // … delay, reverb, EQ …
  12506. 0.5f, 0.5f, 0.55f, 0.5f, 0.45f, 1.0f, 0.0f, 0.5f, 1.0f, 0.0f }; // EQ on, noise (muted)
  12507. Bytes g;
  12508. g.i32 (0x5c4);
  12509. for (auto v : A)
  12510. g.f32 (v);
  12511. g.zeros (0x5c4 - g.b.size());
  12512. p.bytes (g.b);
  12513. }
  12514. Bytes s;
  12515. s.raw ("SSLF", 4);
  12516. s.i32 ((std::int32_t) p.b.size());
  12517. s.bytes (p.b);
  12518. return s.b;
  12519. }
  12520. void dumpIfAsked (const char* var, const std::vector<std::uint8_t>& blob)
  12521. {
  12522. if (const char* path = std::getenv (var))
  12523. {
  12524. std::ofstream out (path, std::ios::binary);
  12525. out.write (reinterpret_cast<const char*> (blob.data()), (std::streamsize) blob.size());
  12526. }
  12527. }
  12528. } // namespace
  12529. TEST (dx10_engine)
  12530. {
  12531. // REA_rodada42: a Fruity DX10 state written from scratch (version 1: 22 raw parameters) against our reference model
  12532. // (dxmodel.py + dxproc.py, bit-identical to the installation's DLL), the channel processor, host levels and the
  12533. // instrument front-end
  12534. const std::int32_t P[22] = { 1000, 40000, 30000, 30000, 40000, 30000, 35000, 8000, 40000, 50000, 20000,
  12535. 25000, 15000, 30000, 20000, 10000, 20000, 30000, 5000, 30000, 20000, -1 };
  12536. std::vector<std::uint8_t> blob (4 + 22 * 4, 0);
  12537. blob[0] = 1;
  12538. std::memcpy (blob.data() + 4, P, sizeof (P));
  12539. CHECK (lynx::dx10StateUsable (blob) && lynx::dx10ProcessorNeutral (blob));
  12540. CHECK (lynx::dx10Param (blob, 21) == -1 && lynx::dx10Param (blob, 3) == 30000 && ! lynx::dx10Param (blob, 22));
  12541. CHECK (! lynx::dx10StateUsable (std::vector<std::uint8_t> (40, 0)));
  12542. {
  12543. auto bad = blob;
  12544. bad[0] = 3; // versions ≥ 3 are ignored by the plugin
  12545. CHECK (! lynx::dx10StateUsable (bad));
  12546. }
  12547. using N = lynx::dx10::RefNote;
  12548. 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 } };
  12549. struct Golden { std::vector<std::uint8_t> state; lynx::dx10::RefOptions opt; int rate; double el, er; std::tuple<int, float, float> at[6]; };
  12550. auto check = [&] (const Golden& g) {
  12551. const auto y = lynx::dx10::renderReference (g.state, notes, (double) g.rate, 16384, g.opt);
  12552. CHECK (y.size() == 2 * 16384);
  12553. if (y.size() != 2 * 16384)
  12554. return y;
  12555. double el = 0.0, er = 0.0;
  12556. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  12557. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  12558. CHECK (std::abs (el - g.el) <= 1e-7 * g.el && std::abs (er - g.er) <= 1e-7 * g.er);
  12559. for (const auto& [i, l, r] : g.at)
  12560. CHECK (goldenSampleClose (y[2 * (std::size_t) i], l, libmTolerance (1e-7f))
  12561. && goldenSampleClose (y[2 * (std::size_t) i + 1], r, libmTolerance (1e-7f)));
  12562. return y;
  12563. };
  12564. lynx::dx10::RefOptions plain, odd;
  12565. odd.block = 333, odd.maxPoly = 1;
  12566. check ({ blob, plain, 44100, 672.293115, 778.393071,
  12567. { { 100, -0.0943799689f, -0.154014125f }, { 1000, -0.0814286768f, -0.132879525f }, { 2500, 0.499493301f, 0.312498778f },
  12568. { 5000, -0.184045464f, 0.171641544f }, { 9000, -0.153369367f, -0.20805186f }, { 16383, -0.00441304967f, 0.200126961f } } });
  12569. // 48 kHz, odd host blocks, max polyphony 1 (the last note steals); the hard-right note keeps FL's x87 left gain
  12570. check ({ blob, odd, 48000, 122.232761, 493.320297,
  12571. { { 100, 0.0236493405f, 0.0385922194f }, { 1000, 0.189806491f, 0.309736073f }, { 2500, -0.425576478f, -0.176279545f },
  12572. { 5000, -3.54030076e-18f, -0.0578194074f }, { 9000, 1.37052479e-17f, 0.223831087f }, { 16383, 9.248742e-18f, 0.151048407f } } });
  12573. // the channel processor (§3): our own settings snapshot — pan LFO (triangle, delayed, faded in), volume envelope
  12574. // with tensions and LFO, cutoff envelope + square LFO, resonance LFO, pitch envelope, SVF LP ×2 (type 7)
  12575. auto record = [] (std::vector<std::uint8_t>& s, int k, std::initializer_list<std::pair<int, int>> fields) {
  12576. const std::int32_t neutral[17] = { 0, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, 0 };
  12577. std::memcpy (s.data() + 0x44 * k, neutral, sizeof (neutral));
  12578. for (const auto& [i, v] : fields)
  12579. std::memcpy (s.data() + 0x44 * k + 4 * i, &v, 4);
  12580. };
  12581. auto settings = [] (std::vector<std::uint8_t>& s, std::int32_t modX, std::int32_t modY, std::int32_t type) {
  12582. std::memcpy (s.data() + 0x15c, &modX, 4);
  12583. std::memcpy (s.data() + 0x160, &modY, 4);
  12584. std::memcpy (s.data() + 0x168, &type, 4);
  12585. };
  12586. std::vector<std::uint8_t> snap (364, 0);
  12587. record (snap, 0, { { 9, 5000 }, { 10, 9000 }, { 11, -40 }, { 12, 15000 }, { 13, 1 } });
  12588. 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 } });
  12589. record (snap, 2, { { 1, 1 }, { 3, 6000 }, { 4, 100 }, { 5, 16000 }, { 6, 20 }, { 8, -60 }, { 11, 20 }, { 12, 18000 }, { 13, 2 } });
  12590. record (snap, 3, { { 11, 40 }, { 12, 17000 } });
  12591. record (snap, 4, { { 1, 1 }, { 3, 5000 }, { 4, 100 }, { 5, 14000 }, { 6, 30 }, { 8, -700 } });
  12592. settings (snap, 140, 120, 7);
  12593. const auto withSnap = lynx::dx10StateWithSnapshot (blob, snap);
  12594. CHECK (withSnap.size() == 460 && withSnap[0] == 2 && ! lynx::dx10ProcessorNeutral (withSnap));
  12595. CHECK (lynx::dx10StateWithSnapshot (withSnap, std::vector<std::uint8_t> (364, 0)) == withSnap); // a v2 state keeps its own
  12596. lynx::dx10::RefOptions ticks;
  12597. ticks.tick = 230, ticks.ppq = 96, ticks.tempo = 120.0;
  12598. check ({ withSnap, ticks, 44100, 346.852062, 554.699375,
  12599. { { 100, -0.00162654708f, -0.00265428401f }, { 1000, 0.0310285408f, 0.0506339073f }, { 2500, -0.118068494f, -0.0915558636f },
  12600. { 5000, -0.297166437f, -0.256465197f }, { 9000, 0.199509233f, 0.166108996f }, { 16383, -0.00392446062f, 0.0410436988f } } });
  12601. // a volume envelope decaying to zero: the 10 ms end fade, then every voice is gone (silence from 14950 on)
  12602. std::vector<std::uint8_t> decay (364, 0);
  12603. for (int k = 0; k < 5; ++k)
  12604. record (decay, k, {});
  12605. record (decay, 1, { { 0, 4 }, { 1, 1 }, { 3, 9000 }, { 4, 100 }, { 5, 12000 }, { 6, 0 }, { 15, 50 }, { 16, -101 } });
  12606. settings (decay, 256, 0, 0);
  12607. auto ticks48 = ticks;
  12608. ticks48.block = 333;
  12609. const auto y = check ({ lynx::dx10StateWithSnapshot (blob, decay), ticks48, 48000, 338.361707, 350.520208,
  12610. { { 100, 0.000514116196f, 0.000838961045f }, { 1000, 0.0412623473f, 0.0673340634f }, { 2500, -0.141532451f, -0.156581759f },
  12611. { 5000, 0.382550895f, 0.23462519f }, { 7000, -0.189829409f, 0.144132584f }, { 9000, -0.13638559f, 0.142912149f } } });
  12612. bool silentTail = y.size() == 2 * 16384 && (y[2 * 14949] != 0.0f || y[2 * 14949 + 1] != 0.0f);
  12613. for (std::size_t i = 2 * 14950; i < y.size(); ++i)
  12614. silentTail = silentTail && y[i] == 0.0f;
  12615. CHECK (silentTail);
  12616. // the instrument: LYNX velocity v → Init.Vol = (11^(1.28·v) − 1)/10 and Final.Vol = channel volume × Init.Vol; channel
  12617. // gains √2·V·(cos, sin)((1 + p)π/4) → V, p; notes on block boundaries play like the reference renderer (neutral
  12618. // channel settings: the instrument's sequencer ticks change nothing; vibrato off, since the plugin-wide vibrato
  12619. // oscillator is stepped by each voice in turn, so its interleaving follows the host block layout, as in FL)
  12620. auto noVib = blob;
  12621. CHECK (lynx::dx10SetParam (noVib, 10, 0));
  12622. auto initVol = [] (double v) { return (double) (float) ((std::pow (11.0, 1.28 * v) - 1.0) / 10.0); }; // Init.Vol is a float
  12623. lynx::ChannelDef c;
  12624. c.instrument = lynx::InstrumentKind::dx10;
  12625. c.pluginState = noVib;
  12626. auto inst = lynx::makeInstrument (lynx::InstrumentKind::dx10);
  12627. inst->configure (c, 120.0);
  12628. inst->prepare (44100.0);
  12629. const float V = 0.6f, pan = 0.25f;
  12630. const auto gl = (float) (std::sqrt (2.0) * V * std::cos ((1.0 + pan) * 3.141592653589793 / 4.0));
  12631. const auto gr = (float) (std::sqrt (2.0) * V * std::sin ((1.0 + pan) * 3.141592653589793 / 4.0));
  12632. std::vector<float> l (16384), r (16384);
  12633. for (int s = 0; s < 16384; s += 512)
  12634. {
  12635. if (s == 0)
  12636. inst->noteOn (60, 0.78125f);
  12637. if (s == 2048)
  12638. {
  12639. inst->setNextNoteExpression (-0.5f, 25.0f);
  12640. inst->noteOn (67, 0.5f);
  12641. }
  12642. if (s == 6144)
  12643. inst->noteOff (60);
  12644. if (s == 8192)
  12645. inst->noteOff (67);
  12646. inst->render (l.data() + s, r.data() + s, 512, gl, gr);
  12647. }
  12648. // the channel level reaches the engine as FL's (recovered to float precision)
  12649. const auto Vr = std::sqrt (((double) gl * gl + (double) gr * gr) * 0.5);
  12650. const auto pr = std::atan2 ((double) gr, (double) gl) * (4.0 / 3.141592653589793) - 1.0;
  12651. const auto ref = lynx::dx10::renderReference (noVib, { { 0, 60, initVol (0.78125), Vr * initVol (0.78125), pr, 0.0, 6144 },
  12652. { 2048, 67, initVol (0.5), Vr * initVol (0.5), std::clamp (-0.5 + pr, -1.0, 1.0), 25.0, 8192 } },
  12653. 44100.0, 16384);
  12654. double worst = 0.0, peak = 0.0;
  12655. for (int i = 0; i < 16384; ++i)
  12656. 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]) }),
  12657. peak = std::max (peak, (double) std::abs (l[(std::size_t) i]));
  12658. CHECK (worst == 0.0 && peak > 0.05);
  12659. // the released voices end (DX10's own release) and the instrument goes idle
  12660. std::vector<float> tail (44100), tailR (tail.size());
  12661. for (int k = 0; k < 20 && ! inst->idle(); ++k)
  12662. inst->render (tail.data(), tailR.data(), (int) tail.size(), gl, gr);
  12663. CHECK (inst->idle());
  12664. // with the channel settings above the instrument follows FL's ticks: the cutoff envelope darkens the note
  12665. {
  12666. lynx::ChannelDef cs = c;
  12667. cs.pluginState = withSnap;
  12668. cs.modelParams = { 96.0f };
  12669. auto si = lynx::makeInstrument (lynx::InstrumentKind::dx10);
  12670. si->configure (cs, 120.0);
  12671. si->prepare (44100.0);
  12672. std::vector<float> sl (32768), sr (32768);
  12673. si->noteOn (48, 0.78125f);
  12674. for (int s = 0; s < 32768; s += 512)
  12675. si->render (sl.data() + s, sr.data() + s, 512, 1.0f, 1.0f);
  12676. double e1 = 0.0, e2 = 0.0;
  12677. bool finite = true;
  12678. for (int i = 0; i < 32768; ++i)
  12679. finite = finite && std::isfinite (sl[(std::size_t) i]), (i < 4096 ? e1 : e2) += (double) sl[(std::size_t) i] * sl[(std::size_t) i];
  12680. CHECK (finite && e1 > 1e-3 && e2 > 1e-3);
  12681. }
  12682. // parameter edits (the channel settings panel) write the raw integers, clamped to the knob ranges
  12683. auto edited = blob;
  12684. CHECK (lynx::dx10SetParam (edited, 21, 7) && lynx::dx10Param (edited, 21) == 3);
  12685. CHECK (lynx::dx10SetParam (edited, 12, 70000) && lynx::dx10Param (edited, 12) == 65536);
  12686. }
  12687. TEST (threeosc_engine)
  12688. {
  12689. // REA_rodada47: a 3x Osc state written from scratch (version 14) against our reference model (osc3model.py,
  12690. // bit-identical to the installation's DLL), the plugin voice processor with its release path, the instrument
  12691. 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 };
  12692. 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) {
  12693. std::vector<std::uint8_t> st (0x60, 0);
  12694. const std::int32_t version = 14;
  12695. std::memcpy (st.data(), &version, 4);
  12696. std::memcpy (st.data() + 4, p, 21 * 4);
  12697. st[0x58] = inv[0], st[0x59] = inv[1], st[0x5a] = inv[2], st[0x5b] = am;
  12698. st.insert (st.end(), snap.begin(), snap.end());
  12699. return st;
  12700. };
  12701. auto record = [] (std::vector<std::uint8_t>& s, int k, std::initializer_list<std::pair<int, int>> fields) {
  12702. const std::int32_t neutral[17] = { 0, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, 0 };
  12703. std::memcpy (s.data() + 0x44 * k, neutral, sizeof (neutral));
  12704. for (const auto& [i, v] : fields)
  12705. std::memcpy (s.data() + 0x44 * k + 4 * i, &v, 4);
  12706. };
  12707. auto settings = [] (std::vector<std::uint8_t>& s, std::int32_t modX, std::int32_t modY, std::int32_t type) {
  12708. std::memcpy (s.data() + 0x15c, &modX, 4);
  12709. std::memcpy (s.data() + 0x160, &modY, 4);
  12710. std::memcpy (s.data() + 0x168, &type, 4);
  12711. };
  12712. std::vector<std::uint8_t> neutral (364, 0);
  12713. for (int k = 0; k < 5; ++k)
  12714. record (neutral, k, k == 1 ? std::initializer_list<std::pair<int, int>> { { 0, 4 }, { 16, -101 } } : std::initializer_list<std::pair<int, int>> {});
  12715. settings (neutral, 256, 0, 0);
  12716. const auto blob = make (P, { 0, 0, 0 }, 0, neutral);
  12717. CHECK (lynx::threeOscStateUsable (blob) && lynx::threeOscNormalisedState (blob, {}) == blob);
  12718. CHECK (lynx::threeOscParam (blob, 9) == -12 && lynx::threeOscParam (blob, 20) == 16 && lynx::threeOscParam (blob, 24) == 0 && ! lynx::threeOscParam (blob, 26));
  12719. // older versions are normalised like the plugin reads them: version 12 (no snapshot → the plugin's defaults),
  12720. // version 10 (20 parameters, no invert/AM bytes)
  12721. {
  12722. std::vector<std::uint8_t> v12 (blob.begin(), blob.begin() + 92);
  12723. v12[0] = 12;
  12724. v12[0x58] = 1;
  12725. const auto n12 = lynx::threeOscNormalisedState (v12, {});
  12726. CHECK (n12.size() == 460 && n12[0] == 14 && lynx::threeOscParam (n12, 21) == 1 && lynx::threeOscParam (n12, 20) == 16
  12727. && std::equal (n12.begin() + 0x60, n12.end(), neutral.begin()));
  12728. std::vector<std::uint8_t> v10 (blob.begin(), blob.begin() + 84);
  12729. v10[0] = 10;
  12730. const auto n10 = lynx::threeOscNormalisedState (v10, {});
  12731. CHECK (lynx::threeOscParam (n10, 20) == 0 && lynx::threeOscParam (n10, 13) == 40 && lynx::threeOscParam (n10, 21) == 0);
  12732. }
  12733. using N = lynx::threeosc::RefNote;
  12734. 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 } };
  12735. struct Golden { std::vector<std::uint8_t> state; lynx::threeosc::RefOptions opt; int rate; double el, er; std::tuple<int, float, float> at[6]; };
  12736. auto check = [&] (const Golden& g) {
  12737. const auto y = lynx::threeosc::renderReference (g.state, notes, (double) g.rate, 16384, g.opt);
  12738. CHECK (y.size() == 2 * 16384);
  12739. if (y.size() != 2 * 16384)
  12740. return y;
  12741. double el = 0.0, er = 0.0;
  12742. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  12743. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  12744. CHECK (std::abs (el - g.el) <= 1e-7 * g.el && std::abs (er - g.er) <= 1e-7 * g.er);
  12745. for (const auto& [i, l, r] : g.at)
  12746. CHECK (goldenSampleClose (y[2 * (std::size_t) i], l, libmTolerance (1e-7f))
  12747. && goldenSampleClose (y[2 * (std::size_t) i + 1], r, libmTolerance (1e-7f)));
  12748. return y;
  12749. };
  12750. // saw / square / triangle-ish mix with pans, stereo detune and phase, random start phases (seed 12345)
  12751. lynx::threeosc::RefOptions plain;
  12752. plain.seed = 12345;
  12753. check ({ blob, plain, 44100, 681.6491972612481, 1509.7245664533937,
  12754. { { 100, 0.0674247071146965f, -0.13440212607383728f }, { 1000, 0.12890009582042694f, 0.446039617061615f },
  12755. { 2500, -0.007686644792556763f, 0.3574123680591583f }, { 5000, 0.08792756497859955f, 0.4948147237300873f },
  12756. { 9000, 1.2136793475613885e-17f, 0.3116455078125f }, { 16383, 5.7305530145831346e-18f, 0.4134995639324188f } } });
  12757. // AM (osc 3 noise modulating osc 1 + 2), inverted oscillators, 48 kHz, odd blocks, max polyphony 1
  12758. 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 };
  12759. lynx::threeosc::RefOptions odd;
  12760. odd.block = 333, odd.maxPoly = 1, odd.seed = 777;
  12761. check ({ make (Pam, { 1, 0, 1 }, 1, neutral), odd, 48000, 453.27241060751771, 1996.4548848690094,
  12762. { { 100, -0.2589033246040344f, -0.310184121131897f }, { 1000, 0.22021546959877014f, 0.3591070771217346f },
  12763. { 2500, -0.5378497242927551f, -0.15602676570415497f }, { 5000, 1.2423200626807308e-17f, 0.3293072581291199f },
  12764. { 9000, 2.5857780099332177e-17f, 0.42474493384361267f }, { 16383, 2.941168384618868e-17f, 0.3569665551185608f } } });
  12765. // the channel processor with its release path: pan LFO, volume envelope released from its current value, cutoff
  12766. // envelope + square LFO, resonance LFO, pitch envelope, SVF LP ×2 (type 7)
  12767. std::vector<std::uint8_t> snap (364, 0);
  12768. record (snap, 0, { { 9, 5000 }, { 10, 9000 }, { 11, -40 }, { 12, 15000 }, { 13, 1 } });
  12769. 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 } });
  12770. record (snap, 2, { { 1, 1 }, { 3, 6000 }, { 4, 100 }, { 5, 16000 }, { 6, 20 }, { 7, 9000 }, { 8, -60 }, { 11, 20 }, { 12, 18000 }, { 13, 2 } });
  12771. record (snap, 3, { { 11, 40 }, { 12, 17000 } });
  12772. record (snap, 4, { { 1, 1 }, { 3, 5000 }, { 4, 100 }, { 5, 14000 }, { 6, 30 }, { 7, 12000 }, { 8, -700 } });
  12773. settings (snap, 140, 120, 7);
  12774. lynx::threeosc::RefOptions ticks;
  12775. ticks.tick = 230, ticks.ppq = 96, ticks.tempo = 120.0, ticks.seed = 99;
  12776. check ({ make (P, { 0, 0, 0 }, 0, snap), ticks, 44100, 977.9928196811321, 2072.965349921462,
  12777. { { 100, -0.001351095037534833f, 0.0029758031014353037f }, { 1000, 0.002833257894963026f, 0.10575854033231735f },
  12778. { 2500, 0.020360738039016724f, 0.3181821405887604f }, { 5000, 0.3340669274330139f, 0.4912331700325012f },
  12779. { 9000, 0.26940545439720154f, 0.27708977460861206f }, { 16383, 0.16570518910884857f, -0.10794822871685028f } } });
  12780. auto ticks48 = ticks;
  12781. ticks48.block = 333, ticks48.tick = 250, ticks48.seed = 5;
  12782. // (at 48 kHz the library's rate ratio 44100/fs scales the filter coefficients and the ramp limit)
  12783. check ({ make (P, { 0, 1, 0 }, 0, snap), ticks48, 48000, 864.9859578029492, 1895.6281818565287,
  12784. { { 100, 0.0031638722866773605f, -0.001987025374546647f }, { 1000, 0.049239177256822586f, 0.08794696629047394f },
  12785. { 2500, 0.13807585835456848f, 0.3554498553276062f }, { 5000, -0.08793184161186218f, -0.1596519947052002f },
  12786. { 9000, 0.08728396147489548f, 0.540290117263794f }, { 16383, 0.039887525141239166f, 0.21270982921123505f } } });
  12787. // HQ oscillators (engine_x64.dll: band-limited tables built with an FFT; FL's single-precision FFT and LYNX's double
  12788. // one agree to float rounding, so the golden values hold to 1e-5)
  12789. {
  12790. 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 };
  12791. auto hq = make (Phq, { 0, 0, 0 }, 0, neutral);
  12792. hq[0x5c] = 1;
  12793. CHECK (lynx::threeOscParam (hq, 25) == 1);
  12794. lynx::threeosc::RefOptions o;
  12795. o.seed = 3;
  12796. const auto y = lynx::threeosc::renderReference (hq, notes, 44100.0, 16384, o);
  12797. CHECK (y.size() == 2 * 16384);
  12798. if (y.size() == 2 * 16384)
  12799. {
  12800. double el = 0.0, er = 0.0;
  12801. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  12802. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  12803. CHECK (std::abs (el - 862.7912087773848) < 1e-5 * 862.8 && std::abs (er - 1232.8046929286115) < 1e-5 * 1232.8);
  12804. const std::tuple<int, float, float> at[] = { { 100, -0.019788391888141632f, -0.19209949672222137f }, { 1000, 0.12019827216863632f, 0.03852222114801407f },
  12805. { 2500, 0.3239150643348694f, 0.07325324416160583f }, { 5000, -0.2809634208679199f, -0.5693862438201904f },
  12806. { 16383, -1.1937656360586644e-17f, -0.061853960156440735f } };
  12807. for (const auto& [i, l, r] : at)
  12808. CHECK (std::abs (y[2 * (std::size_t) i] - l) < 1e-5f && std::abs (y[2 * (std::size_t) i + 1] - r) < 1e-5f);
  12809. }
  12810. }
  12811. // the instrument: Final.Vol = channel volume × Init.Vol, channel gains → FL's volume/pan; notes on block boundaries
  12812. // play like the reference renderer (seed 0, neutral channel settings: ticks change nothing)
  12813. auto initVol = [] (double v) { return (double) (float) ((std::pow (11.0, 1.28 * v) - 1.0) / 10.0); };
  12814. lynx::ChannelDef c;
  12815. c.instrument = lynx::InstrumentKind::threeosc;
  12816. c.pluginState = blob;
  12817. auto inst = lynx::makeInstrument (lynx::InstrumentKind::threeosc);
  12818. inst->configure (c, 120.0);
  12819. inst->prepare (44100.0);
  12820. const float V = 0.6f, pan = 0.25f;
  12821. const auto gl = (float) (std::sqrt (2.0) * V * std::cos ((1.0 + pan) * 3.141592653589793 / 4.0));
  12822. const auto gr = (float) (std::sqrt (2.0) * V * std::sin ((1.0 + pan) * 3.141592653589793 / 4.0));
  12823. std::vector<float> l (16384), r (16384);
  12824. for (int s = 0; s < 16384; s += 512)
  12825. {
  12826. if (s == 0)
  12827. inst->noteOn (60, 0.78125f);
  12828. if (s == 2048)
  12829. {
  12830. inst->setNextNoteExpression (-0.5f, 25.0f);
  12831. inst->noteOn (67, 0.5f);
  12832. }
  12833. if (s == 6144)
  12834. inst->noteOff (60);
  12835. if (s == 8192)
  12836. inst->noteOff (67);
  12837. inst->render (l.data() + s, r.data() + s, 512, gl, gr);
  12838. }
  12839. const auto Vr = std::sqrt (((double) gl * gl + (double) gr * gr) * 0.5);
  12840. const auto pr = std::atan2 ((double) gr, (double) gl) * (4.0 / 3.141592653589793) - 1.0;
  12841. const auto ref = lynx::threeosc::renderReference (blob, { { 0, 60, Vr * initVol (0.78125), pr, 0.0, 6144 },
  12842. { 2048, 67, Vr * initVol (0.5), std::clamp (-0.5 + pr, -1.0, 1.0), 25.0, 8192 } },
  12843. 44100.0, 16384);
  12844. double worst = 0.0, peak = 0.0;
  12845. for (int i = 0; i < 16384; ++i)
  12846. 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]) }),
  12847. peak = std::max (peak, (double) std::abs (l[(std::size_t) i]));
  12848. CHECK (worst == 0.0 && peak > 0.05);
  12849. // released notes end with the processor's 10 ms fade (volume envelope off) and the instrument goes idle
  12850. CHECK (inst->idle());
  12851. // parameter edits write the raw integers, clamped to the knob ranges
  12852. auto edited = blob;
  12853. CHECK (lynx::threeOscSetParam (edited, 9, -40) && lynx::threeOscParam (edited, 9) == -24);
  12854. CHECK (lynx::threeOscSetParam (edited, 13, 500) && lynx::threeOscParam (edited, 13) == 128);
  12855. CHECK (lynx::threeOscSetParam (edited, 24, 1) && edited[0x5b] == 1);
  12856. CHECK (! lynx::threeOscStateUsable (std::vector<std::uint8_t> (2, 0)));
  12857. }
  12858. TEST (gms_engine)
  12859. {
  12860. const auto blob = gmsTestState();
  12861. dumpIfAsked ("LYNX_DUMP_GMS", blob);
  12862. CHECK (lynx::gmsStateUsable (blob));
  12863. CHECK (lynx::gmsWaveNames (blob) == std::vector<std::string> ({ "NoWave A", "NoWave B", "NoWave C" }));
  12864. CHECK (lynx::gmsParamOffset (blob, 12) == 8 + 4 + 7 * 512 + 12 + 4 * 12 && lynx::gmsParamOffset (blob, 212) < 0);
  12865. auto broken = blob;
  12866. broken[8] = 'X';
  12867. CHECK (! lynx::gmsStateUsable (broken));
  12868. CHECK (! lynx::gmsStateUsable (std::vector<std::uint8_t> (blob.begin(), blob.begin() + 2000)));
  12869. // golden samples of our round-39 model (gmsmodel.GmsPlugin, waves missing → silent tables, tempo 140, 44.1 kHz,
  12870. // host blocks of 512): C5 vel 100 0–30000, E5 vel 80 11025–40000, G5 vel 110 22050–52000 (Init.Vol law A)
  12871. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  12872. const std::vector<lynx::gms::RefNote> notes = { { 0, 60, (float) iv (100), 1.0, 0.0, 0.0, 30000 },
  12873. { 11025, 64, (float) iv (80), 1.0, 0.0, 0.0, 40000 },
  12874. { 22050, 67, (float) iv (110), 1.0, 0.0, 0.0, 52000 } };
  12875. const auto y = lynx::gms::renderReference (blob, notes, 44100.0, 66150, "");
  12876. CHECK (y.size() == 2 * 66150u);
  12877. 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 } };
  12878. for (const auto& g : golden)
  12879. 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);
  12880. double e = 0.0;
  12881. for (auto v : y)
  12882. e += (double) v * v;
  12883. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.0790270452795) < 1e-6);
  12884. // the instrument: same engine behind LYNX's channel gains (V = 1, centre → law A), velocity → Init.Vol
  12885. lynx::ChannelDef c;
  12886. c.instrument = lynx::InstrumentKind::gms;
  12887. c.pluginState = blob;
  12888. auto inst = lynx::makeInstrument (lynx::InstrumentKind::gms);
  12889. inst->configure (c, 140.0);
  12890. inst->prepare (44100.0);
  12891. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  12892. for (int b = 0; b < 16; ++b)
  12893. {
  12894. if (b == 0)
  12895. inst->noteOn (60, 0.78125f);
  12896. if (b == 10)
  12897. inst->noteOff (60);
  12898. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  12899. }
  12900. 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, "");
  12901. double diff = 0.0, energy = 0.0;
  12902. for (int i = 0; i < 8192; ++i)
  12903. {
  12904. 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]) });
  12905. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  12906. }
  12907. CHECK (diff == 0.0 && energy > 1e-3);
  12908. }
  12909. // A Toxic Biohazard state written from scratch (REA_rodada45; r45/py/mktest.py builds the same bytes): our own drawn
  12910. // spectra on operators 1 and 3 (wave 0) and the sine on operator 2, so no wave of the installation is needed
  12911. std::vector<std::uint8_t> toxicTestState()
  12912. {
  12913. auto s = lynx::toxicDefaultState();
  12914. static const std::pair<int, double> set[] = {
  12915. { 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 },
  12916. { 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 },
  12917. { 24, 0.6 }, { 25, 1 }, { 26, 1 }, { 27, 1 }, { 32, 0.5606 }, { 33, 0.4394 }, { 38, 0.53 }, { 49, 0 }, { 55, 0.25 },
  12918. { 62, 0.5 }, { 75, 0.08 }, { 81, 0.5 }, { 87, 0.3 }, { 104, 0.8 }, { 105, 0.6 }, { 110, 0.35 }, { 115, 0.22 },
  12919. { 127, 0.18 }, { 147, 0.7 }, { 227, 1 }, { 229, 0.65 }, { 233, 0.4 }, { 236, 1 }, { 237, 1 }, { 252, 0.5 }, { 254, 0.3 },
  12920. { 256, 0.4 }, { 267, 0.4 }, { 269, 0.5 }, { 277, 0.3 }, { 300, 1 }, { 301, 8 }, { 302, 2 }, { 303, 0 }, { 305, 3 },
  12921. { 306, 0 }, { 307, 1 }, { 308, 0 }, { 314, 1 }, { 315, 0 }, { 316, 1 }, { 324, 106 }, { 332, 0.7 } };
  12922. for (const auto& [index, value] : set)
  12923. {
  12924. const auto v = (float) value;
  12925. std::memcpy (s.data() + lynx::toxicParamOffset (s, index), &v, 4);
  12926. }
  12927. auto spectrum = [&] (int slot, int h, float amp, float phase) {
  12928. const auto at = 12 + 0x40 + 1024 * (std::size_t) slot + 4 * (std::size_t) h;
  12929. std::memcpy (s.data() + at, &amp, 4);
  12930. std::memcpy (s.data() + at + 512, &phase, 4);
  12931. };
  12932. for (int h = 0; h < 24; ++h)
  12933. spectrum (0, h, (float) (1.0 / (h + 1)), 0.0f);
  12934. for (int k = 0; k < 16; ++k)
  12935. spectrum (2, 2 * k, (float) (1.0 / (2 * k + 1)), 0.25f);
  12936. return s;
  12937. }
  12938. TEST (toxic_engine)
  12939. {
  12940. const auto blob = toxicTestState();
  12941. dumpIfAsked ("LYNX_DUMP_TOXIC", blob);
  12942. CHECK (blob.size() == 10050u && lynx::toxicStateUsable (blob) && ! lynx::toxicNeedsDll (blob));
  12943. CHECK (lynx::toxicParamOffset (blob, 15) == 12 + 0x40 + 8192 + 4 * 15 && lynx::toxicParamOffset (blob, 340) < 0);
  12944. CHECK (! lynx::toxicDllUsable ("") && ! lynx::toxicDllUsable ("/nonexistent/Toxic Biohazard_x64.dll"));
  12945. auto broken = blob;
  12946. broken[8] = 'X';
  12947. CHECK (! lynx::toxicStateUsable (broken));
  12948. CHECK (! lynx::toxicStateUsable (std::vector<std::uint8_t> (blob.begin(), blob.begin() + 5000)));
  12949. auto needs = blob;
  12950. const auto w = 15.0f; // a built-in wave of the installation: without the DLL the engine refuses the state
  12951. std::memcpy (needs.data() + lynx::toxicParamOffset (needs, 307), &w, 4);
  12952. CHECK (lynx::toxicNeedsDll (needs) && lynx::toxic::renderReference (needs, { { 0, 60 } }, 44100.0, 64, "").empty());
  12953. // golden samples of our round-45 model (toxmodel.run, which equals the DLL bit for bit on this state), effects on,
  12954. // tempo 140, 44.1 kHz, host blocks of 512: C5 vel 100 0–30000, E5 vel 80 11025–40000, G5 vel 110 22050–52000
  12955. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  12956. const std::vector<lynx::toxic::RefNote> notes = { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 },
  12957. { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  12958. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 } };
  12959. const auto y = lynx::toxic::renderReference (blob, notes, 44100.0, 66150, "", 512, 140.0);
  12960. CHECK (y.size() == 2 * 66150u);
  12961. 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 } };
  12962. for (const auto& g : golden)
  12963. 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);
  12964. double e = 0.0;
  12965. for (auto v : y)
  12966. e += (double) v * v;
  12967. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.307227390984) < 1e-6);
  12968. // the instrument: same engine behind LYNX's channel gains (V = 1, centre → law A), velocity → Init.Vol
  12969. lynx::ChannelDef c;
  12970. c.instrument = lynx::InstrumentKind::toxic;
  12971. c.pluginState = blob;
  12972. auto inst = lynx::makeInstrument (lynx::InstrumentKind::toxic);
  12973. inst->configure (c, 140.0);
  12974. inst->prepare (44100.0);
  12975. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  12976. for (int b = 0; b < 16; ++b)
  12977. {
  12978. if (b == 0)
  12979. inst->noteOn (60, 0.78125f);
  12980. if (b == 10)
  12981. inst->noteOff (60);
  12982. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  12983. }
  12984. 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 } },
  12985. 44100.0, 8192, "", 512, 140.0);
  12986. double diff = 0.0, energy = 0.0;
  12987. for (int i = 0; i < 8192; ++i)
  12988. {
  12989. 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]) });
  12990. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  12991. }
  12992. CHECK (diff == 0.0 && energy > 1e-3);
  12993. }
  12994. TEST (morphine_engine)
  12995. {
  12996. const auto blob = morphineTestState();
  12997. dumpIfAsked ("LYNX_DUMP_MORPHINE", blob);
  12998. CHECK (lynx::morphineStateUsable (blob));
  12999. CHECK (lynx::morphineNoiseName (blob).empty());
  13000. const auto at = lynx::morphineParamOffset (blob, 9);
  13001. CHECK (at > 0);
  13002. float a9 = 0.0f;
  13003. std::memcpy (&a9, blob.data() + at, 4);
  13004. CHECK (a9 == 0.2f && lynx::morphineParamOffset (blob, 40) < 0);
  13005. auto broken = blob;
  13006. broken[12] = 'X'; // the MPR0 tag
  13007. CHECK (! lynx::morphineStateUsable (broken));
  13008. CHECK (! lynx::morphineStateUsable (std::vector<std::uint8_t> (blob.begin(), blob.end() - 10)));
  13009. // golden samples of our round-39 model (mormodel.Synth, tempo 120, 44.1 kHz, host blocks of 512): keys 60/64/67,
  13010. // Init.Vol 1.0/0.5/2.0, note-offs at 30000/50000/70000
  13011. const std::vector<lynx::morphine::RefNote> notes = { { 0, 60, 1.0, 1.0, 0.0, 0.0, 30000 },
  13012. { 11025, 64, 0.5, 1.0, 0.0, 0.0, 50000 },
  13013. { 22050, 67, 2.0, 1.0, 0.0, 0.0, 70000 } };
  13014. const auto y = lynx::morphine::renderReference (blob, notes, 44100.0, 80000, "");
  13015. CHECK (y.size() == 2 * 80000u);
  13016. 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 } };
  13017. for (const auto& g : golden)
  13018. 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);
  13019. double e = 0.0;
  13020. for (auto v : y)
  13021. e += (double) v * v;
  13022. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.18467099041) < 1e-6);
  13023. // the instrument: same engine behind LYNX's channel gains
  13024. lynx::ChannelDef c;
  13025. c.instrument = lynx::InstrumentKind::morphine;
  13026. c.pluginState = blob;
  13027. auto inst = lynx::makeInstrument (lynx::InstrumentKind::morphine);
  13028. inst->configure (c, 120.0);
  13029. inst->prepare (44100.0);
  13030. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  13031. for (int b = 0; b < 16; ++b)
  13032. {
  13033. if (b == 0)
  13034. inst->noteOn (60, 0.78125f);
  13035. if (b == 10)
  13036. inst->noteOff (60);
  13037. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  13038. }
  13039. 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, "");
  13040. double diff = 0.0, energy = 0.0;
  13041. for (int i = 0; i < 8192; ++i)
  13042. {
  13043. 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]) });
  13044. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  13045. }
  13046. CHECK (diff == 0.0 && energy > 1e-3);
  13047. }
  13048. namespace
  13049. {
  13050. // A MiniSynth state written from scratch (REA_rodada41 §1.1 layout: "SSLF" + the .flms preset)
  13051. std::vector<std::uint8_t> miniSynthTestState (const std::array<float, 43>& P)
  13052. {
  13053. const std::string name = "LYNX test";
  13054. Bytes body;
  13055. body.raw ("\x99" "FLS\r\n\x1a\n", 8);
  13056. body.raw ("HEAD", 4);
  13057. body.i32 (6 + 2 * (std::int32_t) name.size());
  13058. const std::uint16_t head[3] = { 1, 1, (std::uint16_t) name.size() };
  13059. body.raw (reinterpret_cast<const char*> (head), 6);
  13060. for (char ch : name)
  13061. body.raw (&ch, 1), body.zeros (1);
  13062. body.raw ("DATA", 4);
  13063. body.i32 (176);
  13064. body.raw ("0rev", 4);
  13065. for (auto v : P)
  13066. body.f32 (v);
  13067. Bytes s;
  13068. s.raw ("SSLF", 4);
  13069. s.i32 ((std::int32_t) body.b.size());
  13070. s.bytes (body.b);
  13071. return s.b;
  13072. }
  13073. std::array<float, 43> miniSynthDefaults()
  13074. {
  13075. 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,
  13076. 0.5f, 0, 0.25f, 0.5f, 0.5f, 0, 0.11222222f, 0.4f, 0.1f, 1, 0.5f };
  13077. }
  13078. // per-process suffix for scratch files: several test binaries (one per worktree) may run at the same time, and a
  13079. // fixed name let one process read a file another one was rewriting
  13080. std::string processTempTag()
  13081. {
  13082. static const std::string tag = std::to_string (std::random_device {}()) + "_"
  13083. + std::to_string (std::chrono::steady_clock::now().time_since_epoch().count());
  13084. static const struct Cleanup
  13085. {
  13086. ~Cleanup()
  13087. {
  13088. // every scratch file or folder whose name carries this process's tag (round 173e: the SoundFont, case,
  13089. // missing-file, Crash169g, Natives142 and Rea167l scratch paths are per process too)
  13090. std::error_code ec;
  13091. std::vector<std::filesystem::path> mine;
  13092. for (const auto& e : std::filesystem::directory_iterator (std::filesystem::temp_directory_path (ec), ec))
  13093. if (e.path().filename().string().find (tag) != std::string::npos)
  13094. mine.push_back (e.path());
  13095. for (const auto& p : mine)
  13096. std::filesystem::remove_all (p, ec);
  13097. }
  13098. } cleanup;
  13099. (void) cleanup;
  13100. return tag;
  13101. }
  13102. // a synthetic oscdata.dat (amplitudes 0.3/k, phases −π/2 + 0.001·(k − 1)): the test does not need an FL installation
  13103. std::string miniSynthTestOscdata()
  13104. {
  13105. const auto path = (std::filesystem::temp_directory_path() / ("lynx_test_oscdata_" + processTempTag() + ".dat")).string();
  13106. std::vector<float> d (2048);
  13107. for (int h = 0; h < 1024; ++h)
  13108. d[(std::size_t) h] = (float) (0.3 / (h + 1)), d[1024 + (std::size_t) h] = (float) (-1.5707963267948966 + 0.001 * h);
  13109. std::ofstream out (path, std::ios::binary);
  13110. out.write (reinterpret_cast<const char*> (d.data()), 8192);
  13111. return path;
  13112. }
  13113. } // namespace
  13114. TEST (minisynth_engine)
  13115. {
  13116. auto A = miniSynthDefaults(), B = miniSynthDefaults();
  13117. // A: poly, SQR+SQR, noise, filter env/velocity/keytracking, LFO → cutoff, distortion + decimator, delay, chorus, phaser
  13118. 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 },
  13119. { 12, 0.1f }, { 13, 0.4f }, { 14, 0.3f }, { 15, 0.5f }, { 16, 0.8f }, { 17, 0.6f }, { 18, 0.1f },
  13120. { 19, 0.5f }, { 20, 0.85f }, { 21, 0.05f }, { 22, 0.5f }, { 23, 0.6f }, { 24, 0.4f }, { 25, 1 },
  13121. { 26, 0.3f }, { 27, 0.1f }, { 28, 1 }, { 29, 0.4f }, { 30, 0.5f }, { 31, 0.6f }, { 32, 0.6f },
  13122. { 33, 1 }, { 34, 0.4f }, { 35, 0.6f }, { 36, 0.5f }, { 37, 1 }, { 38, 0.3f }, { 39, 0.6f },
  13123. { 40, 0.5f }, { 41, 0.7f }, { 42, 0.4f } };
  13124. // B: lead (legato glide), BELL, LFO → pitch
  13125. 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 },
  13126. { 17, 0.1f }, { 18, 0.5f }, { 19, 0.6f }, { 20, 0.7f }, { 22, 0.6f }, { 23, 0.5f }, { 24, 0.3f } };
  13127. for (const auto& [i, v] : a)
  13128. A[(std::size_t) i] = v;
  13129. for (const auto& [i, v] : b)
  13130. B[(std::size_t) i] = v;
  13131. const auto blobA = miniSynthTestState (A), blobB = miniSynthTestState (B);
  13132. dumpIfAsked ("LYNX_DUMP_MINISYNTH", blobA);
  13133. CHECK (lynx::miniSynthStateUsable (blobA) && lynx::miniSynthParams (blobA).size() == 43u && lynx::miniSynthParams (blobA)[26] == 0.3f);
  13134. CHECK (lynx::miniSynthParamOffset (blobA, 9) == 8 + 8 + 8 + 6 + 18 + 12 + 4 * 9 && lynx::miniSynthParamOffset (blobA, 43) < 0);
  13135. CHECK (! lynx::miniSynthStateUsable (std::vector<std::uint8_t> (blobA.begin(), blobA.begin() + 20)));
  13136. auto shortData = blobA;
  13137. shortData.resize (shortData.size() - 4 * 40); // only 3 parameters present: the rest keeps the defaults
  13138. CHECK (lynx::miniSynthParams (shortData)[20] == 0.5f && lynx::miniSynthParams (shortData)[1] == 0.5f);
  13139. // golden samples of our round-41 model (msmodel.Plugin on the same synthetic oscdata, 44.1 kHz, host blocks of 512,
  13140. // L = R = Final.Vol): C5 vel 100 0–30000, E5 vel 80 11025–40000, G5 vel 110 22050–52000 (Init.Vol law A)
  13141. const auto osc = miniSynthTestOscdata();
  13142. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  13143. const std::vector<lynx::minisynth::RefNote> notes = { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 },
  13144. { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  13145. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 } };
  13146. struct Golden { int i; float l, r; };
  13147. 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 } };
  13148. 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 } };
  13149. for (int which = 0; which < 2; ++which)
  13150. {
  13151. const auto y = lynx::minisynth::renderReference (which == 0 ? blobA : blobB, notes, 44100.0, 66150, osc);
  13152. CHECK (y.size() == 2 * 66150u);
  13153. if (y.size() != 2 * 66150u)
  13154. continue;
  13155. for (const auto& g : which == 0 ? std::vector (std::begin (goldenA), std::end (goldenA)) : std::vector (std::begin (goldenB), std::end (goldenB)))
  13156. CHECK (y[2 * (std::size_t) g.i] == g.l && y[2 * (std::size_t) g.i + 1] == g.r);
  13157. double e = 0.0;
  13158. for (auto v : y)
  13159. e += (double) v * v;
  13160. CHECK (std::abs (std::sqrt (e / (double) y.size()) - (which == 0 ? 0.315552434392 : 0.03926116287)) < 1e-9);
  13161. }
  13162. // missing oscdata.dat: silent tables, still finite
  13163. const auto silent = lynx::minisynth::renderReference (blobB, notes, 44100.0, 4096, "");
  13164. CHECK (silent.size() == 2 * 4096u && std::all_of (silent.begin(), silent.end(), [] (float v) { return std::isfinite (v) && std::abs (v) < 1.0f; }));
  13165. // the instrument: same engine behind LYNX's channel gains (V = 1, centre → law A), velocity → Init.Vol
  13166. lynx::ChannelDef c;
  13167. c.instrument = lynx::InstrumentKind::minisynth;
  13168. c.pluginState = blobA;
  13169. c.samplePath = osc;
  13170. auto inst = lynx::makeInstrument (lynx::InstrumentKind::minisynth);
  13171. inst->configure (c, 120.0);
  13172. inst->prepare (44100.0);
  13173. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  13174. for (int k = 0; k < 16; ++k)
  13175. {
  13176. if (k == 0)
  13177. inst->noteOn (60, 0.78125f);
  13178. if (k == 10)
  13179. inst->noteOff (60);
  13180. inst->render (l.data() + 512 * k, r.data() + 512 * k, 512, 1.0f, 1.0f);
  13181. }
  13182. 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 } },
  13183. 44100.0, 8192, osc, 512, 120.0, 0, -1.0, true);
  13184. double diff = 0.0, energy = 0.0;
  13185. for (int i = 0; i < 8192; ++i)
  13186. {
  13187. 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]) });
  13188. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  13189. }
  13190. CHECK (diff == 0.0 && energy > 1e-3 && ! inst->idle());
  13191. }
  13192. namespace
  13193. {
  13194. // A PoiZone state written from scratch (REA_rodada44 §1.3 layout, "Ver4" + 156 floats + name): pulse A synced to saw B,
  13195. // noise, envelope to the cutoff, triangle LFO to oscillator A's pitch, 3-voice unison, gate, chorus and tape delay.
  13196. std::vector<std::uint8_t> poizoneTestState()
  13197. {
  13198. std::array<float, 156> P {};
  13199. 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 },
  13200. { 10, 0.8f }, { 11, 0.3f }, { 12, 0.5f }, { 13, 0.4f }, { 14, 0.5f }, { 15, 0.5f }, { 16, 0.5f }, { 17, 0.5f },
  13201. { 18, 0.35f }, { 19, 0.6f }, { 20, 0.5f }, { 21, 0.3f }, { 22, 0.2f }, { 23, 0.6f }, { 24, 0.7f }, { 25, 0.5f },
  13202. { 26, 0.7f }, { 28, 0.6f }, { 29, 0.3f }, { 30, 0.8f }, { 31, 1.0f }, { 32, 0.3f }, { 33, 0.5f }, { 34, 0.5f },
  13203. { 35, 0.3f }, { 37, 1.0f }, { 38, 0.6f }, { 39, 1.0f }, { 40, 0.4f }, { 41, 0.5f }, { 42, 0.5f }, { 43, 0.5f },
  13204. { 47, 0.5f }, { 48, 0.3f }, { 67, 0.2f }, { 68, 0.5f }, { 75, 2.0f }, { 76, 15.0f }, { 77, 1.0f }, { 78, 1.0f },
  13205. { 79, 1.0f } };
  13206. for (const auto& [i, v] : values)
  13207. P[(std::size_t) i] = v;
  13208. for (int i = 49; i < 65; ++i)
  13209. P[(std::size_t) i] = (i - 49) % 3 != 2 ? 1.0f : 0.0f;
  13210. for (int i = 81; i < 155; ++i)
  13211. P[(std::size_t) i] = -1.0f;
  13212. std::vector<std::uint8_t> blob { 'S', 'S', 'L', 'F', 0xb4, 0x02, 0x00, 0x00, '4', 'r', 'e', 'V' };
  13213. blob.resize (12 + 4 * 156 + 64, 0);
  13214. std::memcpy (blob.data() + 12, P.data(), 4 * 156);
  13215. std::memcpy (blob.data() + 12 + 4 * 156, "LYNX test", 9);
  13216. return blob;
  13217. }
  13218. } // namespace
  13219. TEST (poizone_engine)
  13220. {
  13221. const auto blob = poizoneTestState();
  13222. CHECK (lynx::poizoneStateUsable (blob));
  13223. CHECK (lynx::poizoneParamOffset (blob, 18) == 12 + 4 * 18 && lynx::poizoneParamOffset (blob, 156) < 0);
  13224. CHECK (lynx::poizoneParam (blob, 75) == 2.0f && ! lynx::poizoneParam (blob, -1));
  13225. auto broken = blob;
  13226. broken[8] = 'X'; // the "Ver4" tag
  13227. CHECK (! lynx::poizoneStateUsable (broken));
  13228. CHECK (! lynx::poizoneStateUsable (std::vector<std::uint8_t> (blob.begin(), blob.begin() + 10)));
  13229. // golden samples of our round-44 model (pzmodel.Plugin, tempo 120, 44.1 kHz, host blocks of 512, L = R = Final.Vol):
  13230. // C5 vel 100 0–30000, E5 vel 80 11025–40000, G5 vel 110 22050–52000 (Init.Vol = (11^(vel/100) − 1)/10)
  13231. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  13232. const std::vector<lynx::poizone::RefNote> notes = { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 },
  13233. { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  13234. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 } };
  13235. const auto y = lynx::poizone::renderReference (blob, notes, 44100.0, 66150);
  13236. CHECK (y.size() == 2 * 66150u);
  13237. 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 } };
  13238. for (const auto& g : golden)
  13239. 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);
  13240. double e = 0.0;
  13241. for (auto v : y)
  13242. e += (double) v * v;
  13243. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.10990940878400073) < 1e-7);
  13244. // the instrument: same engine behind LYNX's channel gains (V = 1, centre → law A), velocity → Init.Vol
  13245. lynx::ChannelDef c;
  13246. c.instrument = lynx::InstrumentKind::poizone;
  13247. c.pluginState = blob;
  13248. auto inst = lynx::makeInstrument (lynx::InstrumentKind::poizone);
  13249. inst->configure (c, 120.0);
  13250. inst->prepare (44100.0);
  13251. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  13252. for (int b = 0; b < 16; ++b)
  13253. {
  13254. if (b == 0)
  13255. inst->noteOn (60, 0.78125f);
  13256. if (b == 10)
  13257. inst->noteOff (60);
  13258. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  13259. }
  13260. 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,
  13261. 512, 120.0, 0, true);
  13262. double diff = 0.0, energy = 0.0;
  13263. for (int i = 0; i < 8192; ++i)
  13264. {
  13265. 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]) });
  13266. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  13267. }
  13268. CHECK (diff == 0.0 && energy > 1e-3);
  13269. }
  13270. // ---- Fruit Kick / BassDrum (REA_rodada43): synthetic states and data, golden values of our round-43 models ----
  13271. namespace
  13272. {
  13273. std::vector<std::uint8_t> fruitKickStateA()
  13274. {
  13275. const std::int32_t w[8] = { 0, 0, -600, 64, 128, 32, 20, 0 };
  13276. std::vector<std::uint8_t> b (32);
  13277. std::memcpy (b.data(), w, 32);
  13278. return b;
  13279. }
  13280. std::vector<std::uint8_t> fruitKickStateB()
  13281. {
  13282. std::vector<std::uint8_t> b (400, 0);
  13283. const std::int32_t head[7] = { 1, 624, -8, 42, 86, 64, 0 };
  13284. std::memcpy (b.data(), head, sizeof (head));
  13285. std::int32_t recs[5][17];
  13286. for (auto& r : recs)
  13287. {
  13288. const std::int32_t d[17] = { 0, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, 0 };
  13289. std::memcpy (r, d, sizeof (d));
  13290. }
  13291. const std::int32_t pan[17] = { 0, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 3000, 64, 20000, 1, 0, 0, 0 };
  13292. const std::int32_t vol[17] = { 4, -1, 100, 3000, 100, 25000, 64, 15000, 0, 100, 20000, 0, 32950, 0, 0, 40, -101 };
  13293. std::memcpy (recs[0], pan, sizeof (pan));
  13294. std::memcpy (recs[1], vol, sizeof (vol));
  13295. for (int k = 0; k < 5; ++k)
  13296. std::memcpy (b.data() + 32 + 68 * k, recs[k], 68);
  13297. const std::int32_t cut = 256;
  13298. std::memcpy (b.data() + 32 + 0x15c, &cut, 4);
  13299. return b;
  13300. }
  13301. std::string bassDrumTestFolder()
  13302. {
  13303. // our own oscillator table and a pseudo-random 16-bit noise sample, laid out like FL's BassDrum folder
  13304. const auto dir = std::filesystem::temp_directory_path() / ("lynx_bassdrum_test_" + processTempTag());
  13305. std::filesystem::create_directories (dir / "Noise Samples");
  13306. auto wav = [] (const std::filesystem::path& path, const void* data, std::uint32_t bytes, std::uint16_t fmt, std::uint16_t bits) {
  13307. std::ofstream o (path, std::ios::binary);
  13308. auto u32 = [&] (std::uint32_t v) { o.write ((const char*) &v, 4); };
  13309. auto u16 = [&] (std::uint16_t v) { o.write ((const char*) &v, 2); };
  13310. o.write ("RIFF", 4), u32 (36 + bytes), o.write ("WAVEfmt ", 8), u32 (16), u16 (fmt), u16 (1), u32 (44100),
  13311. u32 (44100u * bits / 8), u16 ((std::uint16_t) (bits / 8)), u16 (bits), o.write ("data", 4), u32 (bytes);
  13312. o.write ((const char*) data, bytes);
  13313. };
  13314. std::vector<float> table (1024);
  13315. for (int i = 0; i < 1024; ++i)
  13316. 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));
  13317. wav (dir / "sounddata", table.data(), 4096, 3, 32);
  13318. std::vector<std::int16_t> noise (3000);
  13319. std::uint32_t s = 12345;
  13320. for (auto& v : noise)
  13321. {
  13322. s = (s * 1103515245u + 12345u) & 0x7fffffffu;
  13323. v = (std::int16_t) ((int) ((s >> 8) % 20001u) - 10000);
  13324. }
  13325. wav (dir / "Noise Samples" / "test noise.wav", noise.data(), 6000, 1, 16);
  13326. return dir.string();
  13327. }
  13328. std::vector<std::uint8_t> bassDrumTestState()
  13329. {
  13330. std::vector<std::uint8_t> b (0x50C + 92, 0);
  13331. std::memcpy (b.data(), "SSLF", 4);
  13332. const std::uint32_t size = 0x560;
  13333. std::memcpy (b.data() + 4, &size, 4);
  13334. std::memcpy (b.data() + 8, "2kiK", 4);
  13335. std::memcpy (b.data() + 12, "Test kick", 9);
  13336. const char path[] = "C:/Somewhere/Noise Samples/test noise.wav";
  13337. std::memcpy (b.data() + 268, path, sizeof (path) - 1);
  13338. 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,
  13339. 0.5f, 0.8f, 0.15f, 0.9f, 0.3f, 0.5f, 0.6f, 0.62f, 0.4f, 0.5f, 0.85f };
  13340. std::memcpy (b.data() + 0x50C, P, sizeof (P));
  13341. return b;
  13342. }
  13343. struct Golden
  13344. {
  13345. int i;
  13346. float l, r;
  13347. };
  13348. bool matchesGolden (const std::vector<float>& y, std::initializer_list<Golden> golden, double rms)
  13349. {
  13350. bool ok = true;
  13351. for (const auto& g : golden)
  13352. 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;
  13353. double e = 0.0;
  13354. for (auto v : y)
  13355. e += (double) v * v;
  13356. return ok && std::abs (std::sqrt (e / (double) y.size()) - rms) < 1e-6;
  13357. }
  13358. } // namespace
  13359. TEST (fruitkick_engine)
  13360. {
  13361. const auto A = fruitKickStateA(), B = fruitKickStateB();
  13362. CHECK (lynx::fruitKickStateUsable (A) && lynx::fruitKickStateUsable (B));
  13363. CHECK (! lynx::fruitKickStateUsable (std::vector<std::uint8_t> (A.begin(), A.begin() + 20)));
  13364. CHECK (lynx::fruitKickParam (A, 1) == -600 && lynx::fruitKickParam (B, 0) == 624);
  13365. CHECK (lynx::fruitKickFallbacks (A).empty() && lynx::fruitKickFallbacks (B).empty());
  13366. // a version-0 state takes the channel's envelope records as its snapshot (and becomes version 1)
  13367. std::vector<std::vector<std::uint8_t>> recs;
  13368. for (int k = 0; k < 5; ++k)
  13369. recs.emplace_back (B.begin() + 32 + 68 * k, B.begin() + 32 + 68 * (k + 1));
  13370. const auto withEnv = lynx::fruitKickWithChannelEnvelope (A, recs, 256, 0, 0);
  13371. CHECK (withEnv.size() == 400 && lynx::fruitKickParam (withEnv, 5) == 20 && std::memcmp (withEnv.data() + 32, B.data() + 32, 364) == 0);
  13372. CHECK (lynx::fruitKickWithChannelEnvelope (B, recs, 256, 0, 0) == B);
  13373. // golden samples of our round-43 model (fkmodel.run, 44.1 kHz, host blocks of 512): C5 at 0 held, C6 at 3000
  13374. // (Final.Vol 0.5, pan 0.3) released at 9000; B with the plugin's tick every 230 samples from 115
  13375. 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 } };
  13376. const auto ya = lynx::fruitkick::renderReference (A, notes, 44100.0, 20000);
  13377. CHECK (ya.size() == 40000u);
  13378. // (A uses the distortion: values since REA_rodada55 with FL's x87 live host waveshaper, checked against FLEngine's own;
  13379. // the release fade since REA_rodada74 with IPP's cosine, checked against the DLL)
  13380. CHECK (matchesGolden (ya, { { 10, 0.803601742f, 0.803601742f }, { 700, 0.185467482f, 0.185467482f }, { 2900, -0.63492614f, -0.63492614f },
  13381. { 3100, 0.339092523f, 0.544403195f }, { 5000, -0.0974622667f, -0.150664076f }, { 9100, 0.124622017f, 0.0544089526f },
  13382. { 9400, -0.146487325f, -0.144867823f }, { 15000, 0.009439799f, 0.009439799f } },
  13383. 0.284267665614));
  13384. std::vector<int> ticks;
  13385. for (int t = 115; t < 20000; t += 230)
  13386. ticks.push_back (t);
  13387. const auto yb = lynx::fruitkick::renderReference (B, notes, 44100.0, 20000, 512, ticks);
  13388. CHECK (matchesGolden (yb, { { 10, -0.00760388654f, -0.00760388654f }, { 700, 0.386696398f, 0.442232877f }, { 2900, -0.123097703f, -0.302172035f },
  13389. { 3100, 0.0523624197f, 0.0877688825f }, { 5000, 0.045177035f, 0.100505695f }, { 9100, -0.00423920294f, 0.00810315367f },
  13390. { 9400, 0.00482427329f, -0.010199016f }, { 15000, -0.000171193431f, 1.01026744e-05f } },
  13391. 0.117964927585));
  13392. // the instrument: same engine behind LYNX's channel gains (V = 1, centre), velocity → Init.Vol (link velocity on)
  13393. lynx::ChannelDef c;
  13394. c.instrument = lynx::InstrumentKind::fruitkick;
  13395. c.pluginState = A;
  13396. auto inst = lynx::makeInstrument (lynx::InstrumentKind::fruitkick);
  13397. inst->configure (c, 140.0);
  13398. inst->prepare (44100.0);
  13399. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  13400. for (int b = 0; b < 16; ++b)
  13401. {
  13402. if (b == 0)
  13403. inst->noteOn (60, 0.78125f);
  13404. if (b == 10)
  13405. inst->noteOff (60);
  13406. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  13407. }
  13408. const auto iv = (std::pow (11.0, 1.28 * 0.78125) - 1.0) / 10.0;
  13409. const auto ref = lynx::fruitkick::renderReference (A, { { 0, 60, iv, 0.0, 0.0, 5120 } }, 44100.0, 8192);
  13410. double diff = 0.0, energy = 0.0;
  13411. for (int i = 0; i < 8192; ++i)
  13412. {
  13413. 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]) });
  13414. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  13415. }
  13416. CHECK (diff < 1e-6 && energy > 1e-2);
  13417. }
  13418. TEST (ipp_cos_and_end_fade)
  13419. {
  13420. // ippsCos_32f_A11 of the installation's dsp_ippv2_x64.dll (REA_rodada74 §1): outputs the DLL gave on its two paths
  13421. const auto bits = [] (float v) { return ippcos::floatToBits (v); };
  13422. const auto f = ippcos::bitsToFloat;
  13423. CHECK (bits (ippcos::cosFma (0.1f)) == 0x3f7eaf8bu && bits (ippcos::cosFma (1.0f)) == 0x3f0a534cu);
  13424. CHECK (bits (ippcos::cosFma (2.0f)) == 0xbed51118u && bits (ippcos::cosFma (-3.0f)) == 0xbf7d6646u);
  13425. CHECK (bits (ippcos::cosFma (f (0x3e000007))) == 0x3f7df6f9u && bits (ippcos::cosNoFma (f (0x3e000007))) == 0x3f7df6f8u);
  13426. CHECK (bits (ippcos::cosFma (f (0x3e00007c))) == 0x3f7df6f4u && bits (ippcos::cosNoFma (f (0x3e00007c))) == 0x3f7df6f5u);
  13427. CHECK (ippcos::cosFma (1e-9f) == 1.0f && ippcos::cosNoFma (0.0f) == 1.0f);
  13428. // the portable fused multiply-add rounds once: (1 + 2^-12)^2 = 1 + 2^-11 + 2^-24 is a float halfway case that a
  13429. // tiny addend (lost in a plain double sum) must decide
  13430. const float a = 1.0f + 0x1p-12f;
  13431. CHECK (ippcos::fusedMulAdd (a, a, 0x1p-60f) == 1.0f + 0x1p-11f + 0x1p-23f);
  13432. CHECK (ippcos::fusedMulAdd (a, a, -0x1p-60f) == 1.0f + 0x1p-11f && ippcos::fusedMulAdd (a, a, 0.0f) == 1.0f + 0x1p-11f);
  13433. CHECK (ippcos::fusedMulAdd (3.0f, 5.0f, -1.0f) == 14.0f);
  13434. // the end-fade table: within 1.5e-4 of the exact cosine, ends near zero
  13435. std::vector<float> t (441);
  13436. ippcos::endFadeTable (t.data(), 441);
  13437. double worst = 0.0;
  13438. for (int i = 0; i < 441; ++i)
  13439. worst = std::max (worst, std::abs ((double) t[(std::size_t) i] - 0.5 * (1.0 + std::cos ((i + 1) * 3.141592653589793 / 442.0))));
  13440. CHECK (worst < 1.5e-4 && t[0] > 0.9999f && t[440] < 1e-4f);
  13441. }
  13442. TEST (bassdrum_engine)
  13443. {
  13444. // FL's CRT powf as the engine computes it: a case where its last bit differs from the correctly rounded power
  13445. const float x = 0.87011606f;
  13446. std::uint32_t bits = 0;
  13447. const auto p = lynx::bassdrum::powfMsvc (x, 0.125f);
  13448. std::memcpy (&bits, &p, 4);
  13449. CHECK (bits == 1065063964u);
  13450. CHECK (lynx::bassdrum::powfMsvc (2.0f, 1.0f) == 2.0f && lynx::bassdrum::powfMsvc (10.0f, 0.0f) == 1.0f);
  13451. const auto st = bassDrumTestState();
  13452. CHECK (lynx::bassDrumStateUsable (st) && lynx::bassDrumNoiseName (st) == "test noise.wav");
  13453. CHECK (lynx::bassDrumParamOffset (st, 6) == 0x50C + 24 && lynx::bassDrumParamOffset (st, 23) < 0);
  13454. auto broken = st;
  13455. broken[8] = 'X';
  13456. CHECK (! lynx::bassDrumStateUsable (broken));
  13457. // golden samples of our round-43 model (bdmodel.run with its powf port, 44.1 kHz, host blocks of 512)
  13458. const auto dir = bassDrumTestFolder();
  13459. 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 },
  13460. { 6100, 55, 1.5, 1.0, -0.3, 0.0, -1 } };
  13461. const auto y = lynx::bassdrum::renderReference (st, notes, 44100.0, 30000, dir);
  13462. CHECK (y.size() == 60000u);
  13463. CHECK (matchesGolden (y, { { 50, 0.0122119058f, 0.0122119058f }, { 400, -0.0886279643f, -0.0886279643f }, { 3000, -0.0428197235f, -0.0428197235f },
  13464. { 6050, 0.0199260078f, 0.0240648985f }, { 6300, -0.257363439f, -0.185516387f }, { 9000, 0.268954486f, 0.10506025f },
  13465. { 15000, -0.0102827176f, -0.0380715057f }, { 25000, 0.0f, 0.0f } },
  13466. 0.230007588169));
  13467. CHECK (lynx::bassdrum::renderReference (st, notes, 44100.0, 100, dir + "/nowhere").empty()); // no table: no engine
  13468. lynx::ChannelDef c;
  13469. c.instrument = lynx::InstrumentKind::bassdrum;
  13470. c.pluginState = st;
  13471. c.samplePath = dir;
  13472. auto inst = lynx::makeInstrument (lynx::InstrumentKind::bassdrum);
  13473. inst->configure (c, 120.0);
  13474. inst->prepare (44100.0);
  13475. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  13476. for (int b = 0; b < 16; ++b)
  13477. {
  13478. if (b == 0)
  13479. inst->noteOn (60, 0.78125f);
  13480. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  13481. }
  13482. 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);
  13483. double diff = 0.0, energy = 0.0;
  13484. for (int i = 0; i < 8192; ++i)
  13485. {
  13486. 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]) });
  13487. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  13488. }
  13489. CHECK (diff == 0.0 && energy > 1e-2);
  13490. }
  13491. // REA_rodada55: Fruity Fast Dist, Fruity Flangus and Fruity Flanger, goldens from the installation's DLLs (EXEC, our own states)
  13492. TEST (fruity_fast_dist_flangus_flanger)
  13493. {
  13494. std::vector<float> inL (4096), inR (4096);
  13495. for (int n = 0; n < 4096; ++n)
  13496. {
  13497. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  13498. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  13499. }
  13500. struct Gold { int i; float l, r; };
  13501. auto close = [] (float a, float b) { return std::abs (a - b) <= x87Tolerance() * std::max (1.0f, std::abs (b)); };
  13502. // Fast Dist (+ FL's host waveshaper): live and render kernels
  13503. auto dist = [&] (lynx::fastdistfx::State s, bool render, std::initializer_list<Gold> gold) {
  13504. std::vector<float> l (inL), r (inR);
  13505. lynx::fastdistfx::process (l.data(), 4096, s, render);
  13506. lynx::fastdistfx::process (r.data(), 4096, s, render);
  13507. for (const auto& g : gold)
  13508. CHECK (close (l[(std::size_t) g.i], g.l) && close (r[(std::size_t) g.i], g.r));
  13509. };
  13510. dist ({ 128, 10, 0, 128, 128 }, false, { { 100, 0.0f, -0.39352417f }, { 1000, -0.39352417f, 0.389404297f }, { 2047, 0.376953125f, -0.39352417f },
  13511. { 3000, -0.39352417f, -0.379760742f }, { 4095, 0.342590332f, 0.373931885f } });
  13512. dist ({ 128, 10, 0, 128, 128 }, true, { { 3000, -0.39352417f, -0.379751563f }, { 4095, 0.342590332f, 0.373931885f } });
  13513. dist ({ 192, 4, 1, 77, 97 }, false, { { 100, 0.0f, -0.868428111f }, { 1000, -1.23000622f, 0.740812242f }, { 2047, 0.58129251f, -0.855666518f },
  13514. { 3000, -1.23000622f, -0.613196433f }, { 4095, 0.307311207f, 0.549388587f } });
  13515. dist ({ 192, 4, 1, 77, 97 }, true, { { 2047, 0.58129245f, -0.855666518f }, { 4095, 0.307311267f, 0.549388528f } });
  13516. dist ({ 90, 7, 0, 40, 120 }, false, { { 100, 0.0f, -0.346225947f }, { 1000, -0.511893928f, 0.287246376f }, { 2047, 0.212910682f, -0.340344131f },
  13517. { 3000, -0.511893928f, -0.227848753f }, { 4095, 0.0910475403f, 0.197926477f } });
  13518. dist ({ 90, 7, 0, 40, 120 }, true, { { 100, 0.0f, -0.346223623f }, { 1000, -0.511893749f, 0.287246734f }, { 2047, 0.212909341f, -0.340341568f },
  13519. { 3000, -0.511893749f, -0.227846757f }, { 4095, 0.0910428166f, 0.197928891f } });
  13520. {
  13521. const auto* T = lynx::fastdistfx::curve (0, 10);
  13522. CHECK (T[0] == 0 && T[8191] == T[8192] && lynx::fastdistfx::curve (1, 1)[8191] == 32767);
  13523. }
  13524. // Flangus: FL's call order (state, sample rate, block size of 512), no flush
  13525. auto flange = [&] (lynx::flangusfx::State s, int fs, std::initializer_list<Gold> gold) {
  13526. lynx::flangusfx::Engine e;
  13527. e.prepare (fs);
  13528. e.load (s);
  13529. e.setRate (fs);
  13530. e.setBlockSize (512);
  13531. std::vector<float> io (8192);
  13532. for (int n = 0; n < 4096; ++n)
  13533. {
  13534. io[2 * (std::size_t) n] = inL[(std::size_t) n];
  13535. io[2 * (std::size_t) n + 1] = inR[(std::size_t) n];
  13536. }
  13537. for (int o = 0; o < 4096; o += 512)
  13538. e.render (io.data() + 2 * o, io.data() + 2 * o, 512);
  13539. for (const auto& g : gold)
  13540. CHECK (close (io[2 * (std::size_t) g.i], g.l) && close (io[2 * (std::size_t) g.i + 1], g.r));
  13541. };
  13542. flange ({ 4, 512, 768, 8, 128, -32, 96, 96 }, 44100, { { 100, 0.0249633491f, -0.119510099f }, { 1000, -1.26988697f, 0.687134743f },
  13543. { 2047, 1.03176403f, -0.278752804f }, { 3000, -0.940228283f, -0.853936791f },
  13544. { 4095, 0.201686561f, 0.607052445f } });
  13545. flange ({ 7, 300, 900, 40, 30, 100, -60, 128 }, 44100, { { 100, 0.0f, 0.0446268953f }, { 1000, 1.68992829f, 2.06982398f },
  13546. { 2047, 0.400214791f, 0.838502884f }, { 3000, 1.20018816f, 1.18875635f },
  13547. { 4095, -0.354708612f, 0.503573656f } });
  13548. flange ({ 2, 1024, 200, 0, 0, -128, 128, -90 }, 48000, { { 100, 0.172592998f, -0.230506495f }, { 1000, -1.56807125f, 0.854493499f },
  13549. { 2047, 1.02325416f, -1.17055321f }, { 3000, -1.74136555f, -0.535462856f },
  13550. { 4095, 1.49345946f, -0.537866116f } });
  13551. // Flanger: state, then the sample rate (FL's call order)
  13552. auto flanger = [&] (lynx::flangerfx::State s, int fs, std::initializer_list<Gold> gold) {
  13553. lynx::flangerfx::Engine e;
  13554. e.load (s);
  13555. e.prepare (fs);
  13556. std::vector<float> io (8192);
  13557. for (int n = 0; n < 4096; ++n)
  13558. {
  13559. io[2 * (std::size_t) n] = inL[(std::size_t) n];
  13560. io[2 * (std::size_t) n + 1] = inR[(std::size_t) n];
  13561. }
  13562. for (int o = 0; o < 4096; o += 512)
  13563. e.render (io.data() + 2 * o, io.data() + 2 * o, 512);
  13564. for (const auto& g : gold)
  13565. CHECK (close (io[2 * (std::size_t) g.i], g.l) && close (io[2 * (std::size_t) g.i + 1], g.r));
  13566. };
  13567. flanger ({ 0, 2000, 2000, 118, 0, 0, 23, 0, 1024, 3500, 3000, 2500 }, 44100,
  13568. { { 100, -1.22955823f, -1.98144293f }, { 1000, -0.975861192f, 0.646231353f }, { 2047, 0.518505156f, -0.857361794f },
  13569. { 3000, -0.546559215f, -0.0157452822f }, { 4095, 0.104127884f, 0.206255436f } });
  13570. flanger ({ 300, 4000, 4500, 90, 1000, 300, 99, 1024, 1024, 2000, 4000, 1000 }, 48000,
  13571. { { 100, 0.0f, -0.440000027f }, { 1000, 0.24619779f, 0.775473535f }, { 2047, 1.11609459f, -0.0639692694f },
  13572. { 3000, -1.22629714f, -1.41118503f }, { 4095, -0.263562649f, 0.80683732f } });
  13573. // import: FL's states become the exact effects with the controls as parameters; short states keep the approximations
  13574. {
  13575. 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 };
  13576. const auto slot = lynx::effectFromFl ("Fruity Fast Dist", fd, 120.0);
  13577. CHECK (slot && slot->kind == lynx::EffectKind::fruityFastDist);
  13578. if (slot)
  13579. {
  13580. const auto s = lynx::fastdistfx::fromParams (slot->params.data());
  13581. CHECK (s.pre == 192 && s.threshold == 4 && s.type == 1 && s.mix == 77 && s.post == 97);
  13582. auto fx = lynx::makeEffect (*slot);
  13583. fx->prepare (44100.0);
  13584. std::vector<float> l (inL), r (inR);
  13585. fx->process (l.data(), r.data(), 4096, slot->params, 120.0);
  13586. CHECK (l[1000] == -1.23000622f && r[4095] == 0.549388587f);
  13587. }
  13588. std::vector<std::uint8_t> fg (32);
  13589. const int v[8] { 7, 300, 900, 40, 30, 100, -60, 128 };
  13590. std::memcpy (fg.data(), v, 32);
  13591. const auto f2 = lynx::effectFromFl ("Fruity Flangus", fg, 120.0);
  13592. CHECK (f2 && f2->kind == lynx::EffectKind::fruityFlangus);
  13593. if (f2)
  13594. {
  13595. const auto s = lynx::flangusfx::fromParams (f2->params.data());
  13596. for (int i = 0; i < 8; ++i)
  13597. CHECK (s[(std::size_t) i] == v[i]);
  13598. auto fx = lynx::makeEffect (*f2);
  13599. fx->prepare (48000.0);
  13600. std::vector<float> l (inL), r (inR);
  13601. fx->process (l.data(), r.data(), 4096, f2->params, 120.0);
  13602. CHECK (std::isfinite (l[4095]) && std::isfinite (r[4095]) && std::abs (l[2000]) > 0.0f);
  13603. }
  13604. std::vector<std::uint8_t> fl (52);
  13605. const int w[13] { 1, 300, 4000, 4500, 90, 1000, 300, 99, 1024, 1024, 2000, 4000, 1000 };
  13606. std::memcpy (fl.data(), w, 52);
  13607. const auto f3 = lynx::effectFromFl ("Fruity Flanger", fl, 120.0);
  13608. CHECK (f3 && f3->kind == lynx::EffectKind::fruityFlanger);
  13609. if (f3)
  13610. {
  13611. const auto s = lynx::flangerfx::fromParams (f3->params.data());
  13612. for (int i = 0; i < 12; ++i)
  13613. CHECK (s[(std::size_t) i] == w[i + 1]);
  13614. auto fx = lynx::makeEffect (*f3);
  13615. fx->prepare (48000.0);
  13616. std::vector<float> l (inL), r (inR);
  13617. fx->process (l.data(), r.data(), 4096, f3->params, 120.0);
  13618. CHECK (l[1000] == 0.24619779f && r[4095] == 0.80683732f);
  13619. }
  13620. const auto shortFd = lynx::effectFromFl ("Fruity Fast Dist", std::vector<std::uint8_t> (8, 0), 120.0);
  13621. CHECK (! shortFd || shortFd->kind != lynx::EffectKind::fruityFastDist);
  13622. }
  13623. }
  13624. // FL's Fruity Phaser (REA_rodada105_phaser): goldens from the installation's own DLL, rendered unmodified in 512-frame
  13625. // calls; LYNX's engine matches it to the last bit on the 27 presets (denormals aside), the tolerance only absorbs
  13626. // compilers (gcc / clang / zig) and libm.
  13627. TEST (fruity_phaser_engine)
  13628. {
  13629. std::vector<float> in (8192);
  13630. for (int n = 0; n < 4096; ++n)
  13631. {
  13632. in[2 * (std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  13633. in[2 * (std::size_t) n + 1] = (float) (((n * 53) % 170 - 85) / 70.0);
  13634. }
  13635. struct Gold
  13636. {
  13637. int i;
  13638. float l, r;
  13639. };
  13640. auto close = [] (float a, float b) { return std::abs (a - b) <= 2.0e-5f * std::max (1.0f, std::abs (b)); };
  13641. auto phase = [&] (lynx::phaserfx::State s, int fs, std::initializer_list<Gold> gold) {
  13642. lynx::phaserfx::Engine e;
  13643. e.setRate (fs);
  13644. e.load (s);
  13645. e.reset();
  13646. std::vector<float> io (in);
  13647. for (int o = 0; o < 4096; o += 512)
  13648. e.render (io.data() + 2 * o, io.data() + 2 * o, 512);
  13649. for (const auto& g : gold)
  13650. CHECK (close (io[2 * (std::size_t) g.i], g.l) && close (io[2 * (std::size_t) g.i + 1], g.r));
  13651. };
  13652. phase ({ 2500, 100, 800, 0, 512, 7, 400, 512, 4000 }, 44100,
  13653. { { 100, 0.243106663f, -1.63760519f }, { 1000, -2.36990547f, 0.841273248f }, { 2047, 1.21970272f, -1.66379488f },
  13654. { 3000, -2.80603957f, -1.03600585f }, { 4095, 1.80225599f, 0.845855355f } });
  13655. phase ({ 417, 995, 1000, 0, 1024, 3, 705, 0, 2943 }, 48000,
  13656. { { 100, -0.360522032f, -0.0510774851f }, { 1000, -0.788047731f, 0.324495822f }, { 2047, 0.304549545f, 0.0850031748f },
  13657. { 3000, -0.79305625f, 0.114482567f }, { 4095, -0.67624253f, -0.0348635167f } });
  13658. phase ({ 2448, 510, 417, 1024, 1024, 16, 508, 0, 2500 }, 44100,
  13659. { { 100, 1.66273713f, -0.0830700323f }, { 1000, -1.47920549f, -0.0849421024f }, { 2047, -0.157817751f, -1.06916845f },
  13660. { 3000, -0.703828216f, -0.0190049056f }, { 4095, 0.768234849f, -0.965515852f } });
  13661. phase ({ 5000, 0, 1000, 700, 0, 22, 950, 300, 5000 }, 44100,
  13662. { { 100, 0.0809119642f, -0.521223426f }, { 1000, 1.07321298f, 0.681761861f }, { 2047, 0.790922821f, -0.205452889f },
  13663. { 3000, -1.91779351f, 0.599423349f }, { 4095, -1.18747222f, 1.03711367f } });
  13664. // controls: FL's ranges, the stage count clamp, the texts
  13665. CHECK (std::abs (lynx::phaserfx::sweepHz (0.5f, false) - 0.238405839f) < 1.0e-6f);
  13666. CHECK (std::abs (lynx::phaserfx::sweepHz (1.0f, true) - 10.0f) < 1.0e-5f);
  13667. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityPhaser, 5, 11.0f / 22.0f, 120.0) == "12");
  13668. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityPhaser, 5, 0.0f, 120.0) == "3");
  13669. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityPhaser, 7, 0.0f, 120.0) == "100% wet");
  13670. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityPhaser, 8, 0.5f, 120.0) == "+0.0 dB");
  13671. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityPhaser, 3, 0.0f, 120.0) == "small");
  13672. {
  13673. lynx::EffectParams ctx {};
  13674. ctx[3] = 1.0f;
  13675. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityPhaser, 0, 1.0f, 120.0, ctx) == "10.000 Hz");
  13676. }
  13677. CHECK (lynx::effectInfo (lynx::EffectKind::fruityPhaser).params.size() == 9);
  13678. // import: FL's state (version 1 + nine controls) becomes the exact effect; its parameters round-trip
  13679. const lynx::phaserfx::State ouch { 417, 995, 1000, 0, 1024, 3, 705, 0, 2943 };
  13680. const auto bytes = lynx::phaserfx::serialize (ouch);
  13681. const auto slot = lynx::effectFromFl ("Fruity Phaser", bytes, 120.0);
  13682. CHECK (slot && slot->kind == lynx::EffectKind::fruityPhaser);
  13683. if (slot)
  13684. {
  13685. CHECK (lynx::phaserfx::fromParams (slot->params.data()) == ouch);
  13686. auto fx = lynx::makeEffect (*slot);
  13687. fx->prepare (48000.0);
  13688. std::vector<float> l (4096), r (4096);
  13689. for (int n = 0; n < 4096; ++n)
  13690. {
  13691. l[(std::size_t) n] = in[2 * (std::size_t) n];
  13692. r[(std::size_t) n] = in[2 * (std::size_t) n + 1];
  13693. }
  13694. fx->process (l.data(), r.data(), 4096, slot->params, 120.0);
  13695. CHECK (close (l[1000], -0.788047731f) && close (r[4095], -0.0348635167f));
  13696. }
  13697. auto v2 = bytes;
  13698. v2[0] = 2; // FL reads only versions below 2
  13699. const auto later = lynx::effectFromFl ("Fruity Phaser", v2, 120.0);
  13700. CHECK (! later || later->kind != lynx::EffectKind::fruityPhaser);
  13701. CHECK (lynx::plugui::hasFlLayout (lynx::EffectKind::fruityPhaser));
  13702. }
  13703. // Workflow P2 (docs/WORKFLOW_FL.md)
  13704. #include "WorkflowP2Tests.inc"
  13705. // Editor mouse grammar (Piano Roll / Playlist)
  13706. #include "EditorGestureTests.inc"
  13707. TEST (chorus_family_engines)
  13708. {
  13709. // REA_rodada59: goldens from the installation's DLLs (44.1 kHz, blocks of 512) on this signal
  13710. std::vector<float> inL (4096), inR (4096);
  13711. for (int n = 0; n < 4096; ++n)
  13712. {
  13713. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0) * 0.5f;
  13714. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0) * 0.5f;
  13715. }
  13716. struct Gold { int i; float l, r; };
  13717. auto close = [] (float a, float b) { return std::abs (a - b) <= x87Tolerance() * std::max (1.0f, std::abs (b)); };
  13718. auto run = [&] (const char* plugin, const std::vector<std::uint8_t>& st, lynx::EffectKind kind, std::initializer_list<Gold> gold) {
  13719. const auto slot = lynx::effectFromFl (plugin, st, 120.0);
  13720. CHECK (slot && slot->kind == kind);
  13721. if (! slot)
  13722. return;
  13723. auto fx = lynx::makeEffect (*slot);
  13724. fx->prepare (44100.0);
  13725. std::vector<float> l (inL), r (inR);
  13726. fx->process (l.data(), r.data(), 1024, slot->params, 120.0);
  13727. fx->process (l.data() + 1024, r.data() + 1024, 3072, slot->params, 120.0);
  13728. for (const auto& g : gold)
  13729. CHECK (close (l[(std::size_t) g.i], g.l) && close (r[(std::size_t) g.i], g.r));
  13730. };
  13731. auto ints = [] (std::initializer_list<int> v) {
  13732. std::vector<std::uint8_t> b;
  13733. for (int x : v)
  13734. for (int k = 0; k < 4; ++k)
  13735. b.push_back ((std::uint8_t) ((unsigned) x >> (8 * k)));
  13736. return b;
  13737. };
  13738. // Fruity Chorus (start phases 0, which FL draws at random)
  13739. run ("Fruity Chorus", ints ({ 1, 512, 2250, 341, 1500, 2500, 3500, 23, 0, 0, 0, 2500, 0 }), lynx::EffectKind::fruityChorus,
  13740. { { 100, 0.0f, -0.3850000202655792f }, { 1000, -0.6181581020355225f, 0.2707735002040863f }, { 2047, -0.01424609124660492f, 0.24228274822235107f },
  13741. { 3000, -1.1650776863098145f, -0.1809898018836975f }, { 4095, -0.012790083885192871f, 0.060571759939193726f } });
  13742. run ("Fruity Chorus", ints ({ 1, 700, 4000, 900, 4000, 3000, 2000, 400, 700, 1000, 30, 4999, 0 }), lynx::EffectKind::fruityChorus,
  13743. { { 100, 0.0f, -0.3850000202655792f }, { 1000, -0.5833333134651184f, 0.36345374584198f }, { 2047, -0.0336097776889801f, -0.3360772430896759f },
  13744. { 3000, -0.40196430683135986f, -0.5073210597038269f }, { 4095, -0.1176539734005928f, 0.444706529378891f } });
  13745. run ("Fruity Chorus", ints ({ 1, 36, 469, 1004, 2832, 293, 3500, 0, 428, 0, 0, 3887, 1024 }), lynx::EffectKind::fruityChorus,
  13746. { { 100, 0.2975943684577942f, 0.3747217655181885f }, { 1000, -0.39463338255882263f, 0.18099871277809143f }, { 2047, 0.3894335627555847f, 0.35111793875694275f },
  13747. { 3000, -0.1450725942850113f, 0.2847272455692291f }, { 4095, 0.33678582310676575f, -0.13365517556667328f } });
  13748. {
  13749. const auto slot = lynx::effectFromFl ("Fruity Chorus", ints ({ 1, 36, 469, 1004, 2832, 293, 3500, 0, 428, 0, 0, 3887, 1024 }), 120.0);
  13750. CHECK (slot && lynx::fruitychorusfx::fromParams (slot->params.data())[2] == 1004);
  13751. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityChorus, 11, 1.0f, 120.0) == "on");
  13752. CHECK (lynx::effectParamFromFl ("Fruity Chorus", 11).has_value() && ! lynx::effectParamFromFl ("Fruity Chorus", 12).has_value());
  13753. }
  13754. // a state FL does not read (version 2) or too short keeps the generic chorus
  13755. CHECK (! lynx::effectFromFl ("Fruity Chorus", ints ({ 2, 512, 2250, 341, 1500, 2500, 3500, 23, 0, 0, 0, 2500, 0 }), 120.0).has_value()
  13756. || lynx::effectFromFl ("Fruity Chorus", ints ({ 2, 512, 2250, 341, 1500, 2500, 3500, 23, 0, 0, 0, 2500, 0 }), 120.0)->kind != lynx::EffectKind::fruityChorus);
  13757. CHECK (! lynx::effectFromFl ("Fruity Chorus", { 1, 0, 0, 0 }, 120.0).has_value()
  13758. || lynx::effectFromFl ("Fruity Chorus", { 1, 0, 0, 0 }, 120.0)->kind != lynx::EffectKind::fruityChorus);
  13759. // Hyper Chorus engine in FL's load sequence (defaults, then the state: the parameters glide), instance k's phases
  13760. auto hyper = [&] (std::initializer_list<int> raws, int instance, std::initializer_list<Gold> gold) {
  13761. namespace hc = lynx::hyperchorusfx;
  13762. hc::Engine e (instance);
  13763. const std::vector<int> r (raws);
  13764. 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); };
  13765. for (int i = 0; i < hc::params; ++i)
  13766. e.setParam (i, val (i, hc::defaults[(std::size_t) i]));
  13767. for (int i = 0; i < hc::params; ++i)
  13768. e.setParam (i, val (i, r[(std::size_t) i]));
  13769. e.setRate (44100.0);
  13770. std::vector<float> io (8192);
  13771. for (int n = 0; n < 4096; ++n)
  13772. io[2 * (std::size_t) n] = inL[(std::size_t) n], io[2 * (std::size_t) n + 1] = inR[(std::size_t) n];
  13773. for (int s = 0; s < 4096; s += 512)
  13774. e.render (io.data() + 2 * s, io.data() + 2 * s, 512);
  13775. for (const auto& g : gold)
  13776. CHECK (close (io[2 * (std::size_t) g.i], g.l) && close (io[2 * (std::size_t) g.i + 1], g.r));
  13777. };
  13778. hyper ({ 562, 919, 499, 505, 709, 716, 1, 19247, 10900, 191, 1000 }, 0,
  13779. { { 100, 0.0f, -0.4115639328956604f }, { 1000, -0.658551037311554f, 0.28647756576538086f }, { 2047, 0.24743860960006714f, -0.5088979005813599f },
  13780. { 3000, -0.5171564221382141f, -0.3630276620388031f }, { 4095, 0.08128821104764938f, 0.18811003863811493f } });
  13781. hyper ({ 1000, 555, 0, 400, 300, 300, 0, 22000, 120, 368, 750 }, 2,
  13782. { { 100, 0.0f, -0.398922324180603f }, { 1000, -1.1528236865997314f, 0.2839684784412384f }, { 2047, 0.5115746855735779f, -0.3330395221710205f },
  13783. { 3000, -0.6094785928726196f, 0.17040175199508667f }, { 4095, -0.11666109412908554f, 0.21931695938110352f } });
  13784. CHECK (lynx::hyperchorusfx::randState (1) == 1u * 214013u + 2531011u);
  13785. {
  13786. // the import: controls as parameters; a song's Hyper Chorus slots are numbered in mixer order
  13787. const auto st = ints ({ 2, 1000, 555, 0, 400, 300, 300, 0, 22000, 120, 368, 750, 0 });
  13788. const auto slot = lynx::effectFromFl ("Hyper Chorus", st, 120.0);
  13789. CHECK (slot && slot->kind == lynx::EffectKind::hyperChorus && lynx::hyperchorusfx::fromParams (slot->params.data())[9] == 368);
  13790. lynx::Song song;
  13791. song.mixer.resize (3);
  13792. song.mixer[1].effects = { *slot, *slot };
  13793. song.mixer[2].effects = { *slot };
  13794. lynx::numberChorusInstances (song);
  13795. CHECK (lynx::hyperchorusfx::instanceOf (song.mixer[2].effects[0].state) == 2);
  13796. auto fx = lynx::makeEffect (song.mixer[2].effects[0]);
  13797. fx->prepare (48000.0);
  13798. std::vector<float> l (inL), r (inR);
  13799. fx->process (l.data(), r.data(), 4096, slot->params, 120.0);
  13800. bool finite = true, wet = false;
  13801. for (int n = 0; n < 4096; ++n)
  13802. {
  13803. finite = finite && std::isfinite (l[(std::size_t) n]) && std::isfinite (r[(std::size_t) n]);
  13804. wet = wet || l[(std::size_t) n] != inL[(std::size_t) n];
  13805. }
  13806. CHECK (finite && wet);
  13807. CHECK (lynx::formatEffectParam (lynx::EffectKind::hyperChorus, 8, 0.0f, 120.0) == "off");
  13808. CHECK (! lynx::effectFromFl ("Hyper Chorus", ints ({ 3, 1000, 555, 0, 400, 300, 300, 0, 22000, 120, 368, 750, 0 }), 120.0).has_value()
  13809. || lynx::effectFromFl ("Hyper Chorus", ints ({ 3, 1000, 555, 0, 400, 300, 300, 0, 22000, 120, 368, 750, 0 }), 120.0)->kind != lynx::EffectKind::hyperChorus);
  13810. }
  13811. // Vintage Chorus engine as FL drives it (created, sample rate, each control), fresh instance (its smoothed values
  13812. // start from the engine's initial ones)
  13813. auto vintage = [&] (std::initializer_list<int> raws, std::initializer_list<Gold> gold) {
  13814. namespace vc = lynx::vintagechorusfx;
  13815. vc::Engine e;
  13816. e.setRate (44100.0);
  13817. int i = 0;
  13818. for (int raw : raws)
  13819. {
  13820. 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));
  13821. ++i;
  13822. }
  13823. std::vector<float> io (8192);
  13824. for (int n = 0; n < 4096; ++n)
  13825. io[2 * (std::size_t) n] = inL[(std::size_t) n], io[2 * (std::size_t) n + 1] = inR[(std::size_t) n];
  13826. for (int s = 0; s < 4096; s += 512)
  13827. e.render (io.data() + 2 * s, io.data() + 2 * s, 512);
  13828. for (const auto& g : gold)
  13829. CHECK (close (io[2 * (std::size_t) g.i], g.l) && close (io[2 * (std::size_t) g.i + 1], g.r));
  13830. };
  13831. vintage ({ 2, 0, 211046, 1, 2411, 960, 1985, 5121, 0, 1, 4420, 6000, 273, 0, 0, 0, 0 },
  13832. { { 100, 0.0f, -0.05606573447585106f }, { 1000, -0.3413679003715515f, 0.20469821989536285f }, { 2047, 0.1487489938735962f, -0.28199973702430725f },
  13833. { 3000, -0.31699514389038086f, -0.43230289220809937f }, { 4095, 0.1255197525024414f, 0.0810704305768013f } });
  13834. vintage ({ 2, 0, 80, 0, 0, 60, 3000, 3000, 0, 0, 0, 3000, 0, 3000, 0, 0, 0 }, // hiss
  13835. { { 100, -1.2527654007499223e-06f, -0.053655341267585754f }, { 1000, -0.44306400418281555f, 0.25606146454811096f }, { 2047, 0.23425261676311493f, -0.21375307440757751f },
  13836. { 3000, -0.5793180465698242f, -0.13371579349040985f }, { 4095, 0.05595953390002251f, 0.11362095922231674f } });
  13837. vintage ({ 0, 1, 49152, 0, 3000, 60, 3000, 3000, 0, 0, 0, 3000, 0, 0, 0, 0, 0 }, // Chorus 1 + 2
  13838. { { 100, 0.0f, -0.05365874245762825f }, { 1000, -0.5095958113670349f, 0.2809423506259918f }, { 2047, 0.17256303131580353f, -0.3563305139541626f },
  13839. { 3000, -0.471851646900177f, -0.30123069882392883f }, { 4095, 0.0959278866648674f, 0.13436323404312134f } });
  13840. {
  13841. const auto st = ints ({ 1, 2, 0, 211046, 1, 2411, 960, 1985, 5121, 0, 1, 4420, 6000, 273, 0, 0, 0, 0 });
  13842. const auto slot = lynx::effectFromFl ("Vintage Chorus", st, 120.0);
  13843. CHECK (slot && slot->kind == lynx::EffectKind::vintageChorus && lynx::vintagechorusfx::fromParams (slot->params.data())[2] == 211046);
  13844. auto fx = lynx::makeEffect (*slot);
  13845. fx->prepare (44100.0);
  13846. std::vector<float> l (inL), r (inR);
  13847. fx->process (l.data(), r.data(), 4096, slot->params, 120.0);
  13848. bool finite = true;
  13849. for (int n = 0; n < 4096; ++n)
  13850. finite = finite && std::isfinite (l[(std::size_t) n]) && std::isfinite (r[(std::size_t) n]);
  13851. CHECK (finite && l[3000] != inL[3000]);
  13852. CHECK (lynx::formatEffectParam (lynx::EffectKind::vintageChorus, 0, 0.0f, 120.0) == "Chorus 1");
  13853. 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()
  13854. || 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);
  13855. }
  13856. }
  13857. #include "UiChromeTests.inc"
  13858. TEST (fruity_delay_engines)
  13859. {
  13860. // REA_rodada52: the engines on their own against the installation's DLLs (goldens = the DLL's output for the same
  13861. // input with FL's set-up order, EXEC: bit-identical), then the mixer effects built from FL states
  13862. const int n = 16384;
  13863. std::vector<float> inL ((std::size_t) n, 0.0f), inR ((std::size_t) n, 0.0f);
  13864. std::uint32_t seed = 1;
  13865. for (int i = 0; i < 4096; ++i) // a noise burst, then the echoes
  13866. {
  13867. seed = seed * 1664525u + 1013904223u;
  13868. inL[(std::size_t) i] = (float) ((int) (seed >> 16) - 32768) / 32768.0f * 0.5f;
  13869. seed = seed * 1664525u + 1013904223u;
  13870. inR[(std::size_t) i] = (float) ((int) (seed >> 16) - 32768) / 32768.0f * 0.5f;
  13871. }
  13872. using Golden = std::vector<std::tuple<int, float, float>>;
  13873. auto check = [&] (const std::vector<float>& l, const std::vector<float>& r, const Golden& golden) {
  13874. for (const auto& [i, gl, gr] : golden)
  13875. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  13876. };
  13877. auto run3 = [&] (const std::array<int, 26>& raw, double bpm) {
  13878. lynx::fldelay::Delay3Engine e (44100.0);
  13879. e.setTempo (bpm);
  13880. for (int i = 0; i < 26; ++i)
  13881. e.setParameter (i, lynx::fldelay::delay3EngineValue (i, raw[(std::size_t) i]));
  13882. e.setSampleRate (44100.0); // FL's wrapper sets the rate up again after the state
  13883. e.setSampleRate (44100.0);
  13884. std::vector<float> l (inL), r (inR);
  13885. for (int s = 0; s < n; s += 512)
  13886. e.process (l.data() + s, r.data() + s, 512);
  13887. return std::make_pair (l, r);
  13888. };
  13889. auto run2 = [&] (const std::array<int, 8>& raw, double bpm) {
  13890. lynx::fldelay::Delay2State st;
  13891. st.v = raw;
  13892. lynx::fldelay::Delay2Engine e (st, 44100.0, bpm, 512);
  13893. std::vector<float> l (inL), r (inR);
  13894. for (int s = 0; s < n; s += 512)
  13895. e.process (l.data() + s, r.data() + s, 512);
  13896. return std::make_pair (l, r);
  13897. };
  13898. // Delay 3, true stereo, synced 1/8 beat, cutoff + resonance (Fruity Delay 3_x64.dll at 120 and 100 BPM)
  13899. 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 };
  13900. auto o = run3 (a, 120.0);
  13901. check (o.first, o.second, { { 3000, 0.399949551f, 0.598533869f }, { 6000, 0.116703831f, -0.315510482f }, { 9000, -0.150520369f, -0.250620633f },
  13902. { 13000, 0.193608373f, 0.0598564595f }, { 16000, -0.0524701327f, 0.022601109f } });
  13903. o = run3 (a, 100.0);
  13904. check (o.first, o.second, { { 5000, 0.377422422f, 0.346570343f }, { 6000, -0.182619616f, 0.0488029197f }, { 11000, -0.00120426295f, 0.0253051519f },
  13905. { 16000, 0.0806550607f, -0.0991330743f } });
  13906. // ping pong, keep pitch, modulation, cutoff LFO, diffusion, saturation, rate and bit reduction, colour
  13907. 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);
  13908. check (o.first, o.second, { { 1500, 0.288540125f, -0.148862943f }, { 5000, 5.90502168e-06f, 0.111382999f }, { 8000, -0.0736631379f, -0.0305093247f },
  13909. { 11000, -0.00831047911f, 0.0102323527f }, { 16000, 0.0175791476f, 0.0273917634f } });
  13910. // fake stereo, unsynced, high-pass, limit distortion, no dry
  13911. 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);
  13912. check (o.first, o.second, { { 1500, 0.0825810209f, 0.0228505582f }, { 6000, -0.0427959524f, -0.0408911295f }, { 9000, 0.0114790015f, 0.019227922f },
  13913. { 16000, -0.000312746735f, 0.00101647642f } });
  13914. // delay type "Off": filter, distortion and the bit/rate reduction on the dry path
  13915. 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);
  13916. check (o.first, o.second, { { 1500, 0.238686681f, -0.106820084f }, { 3000, 0.131528616f, 0.0665975735f }, { 5000, 9.94473215e-09f, -8.22819057e-09f },
  13917. { 11000, 0.0f, 0.0f } });
  13918. // Delay 2: default, ping pong with a stereo offset, swapped without dry (Fruity Delay 2_x64.dll)
  13919. o = run2 ({ 0, 128, 128, 64, 36, 0, 0, 128 }, 120.0);
  13920. check (o.first, o.second, { { 3000, 0.211380005f, 0.277908325f }, { 5000, -0.223615795f, -0.0189359933f }, { 9000, 0.120054141f, -0.00297877868f },
  13921. { 16000, 0.0182533264f, 0.105384827f } });
  13922. o = run2 ({ -40, 110, 90, 90, 24, -300, 2, 90 }, 100.0);
  13923. check (o.first, o.second, { { 3000, 0.0929639712f, 0.122222826f }, { 5000, -0.0884733275f, -0.0507739782f }, { 11000, 0.0589251742f, 0.0f },
  13924. { 16000, -0.029524127f, 0.0128910914f } });
  13925. o = run2 ({ 60, 140, 0, 100, 30, 250, 1, 110 }, 120.0);
  13926. check (o.first, o.second, { { 1500, 0.0f, 0.0f }, { 5000, 0.176379532f, -0.212506086f }, { 8000, -0.324207425f, 0.0675852001f },
  13927. { 13000, 0.145023212f, -0.104788139f } });
  13928. // the mixer effects from FL states: time law (Delay 3 synced 192 = 1 beat; Delay 2 144 = 3/4 beat), settled start
  13929. auto state3 = std::vector<std::uint8_t> (4 + 26 * 4, 0);
  13930. 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 };
  13931. state3[0] = 1;
  13932. std::memcpy (state3.data() + 4, def3.data(), 26 * 4);
  13933. const auto slot3 = lynx::effectFromFl ("Fruity Delay 3", state3, 120.0);
  13934. CHECK (slot3 && slot3->kind == lynx::EffectKind::fruityDelay3);
  13935. auto state2 = std::vector<std::uint8_t> (32, 0);
  13936. const std::array<int, 8> def2 { 0, 128, 128, 0, 144, 0, 0, 128 };
  13937. std::memcpy (state2.data(), def2.data(), 32);
  13938. const auto slot2 = lynx::effectFromFl ("Fruity delay 2", state2, 120.0);
  13939. CHECK (slot2 && slot2->kind == lynx::EffectKind::fruityDelay2);
  13940. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityDelay2, 4, slot2->params[4], 120.0) == "3:00");
  13941. auto echoAt = [] (const lynx::EffectSlot& slot, double bpm) {
  13942. auto fx = lynx::makeEffect (slot);
  13943. fx->prepare (44100.0);
  13944. std::vector<float> l (48000, 0.0f), r (48000, 0.0f);
  13945. l[100] = 1.0f;
  13946. for (int s = 0; s < 48000; s += 1024)
  13947. fx->process (l.data() + s, r.data() + s, std::min (1024, 48000 - s), slot.params, bpm);
  13948. int best = 200;
  13949. for (int i = 200; i < 48000; ++i)
  13950. if (std::abs (l[(std::size_t) i]) > std::abs (l[(std::size_t) best]))
  13951. best = i;
  13952. return best - 100;
  13953. };
  13954. if (slot3 && slot2)
  13955. {
  13956. CHECK (echoAt (*slot3, 120.0) == 22050);
  13957. CHECK (echoAt (*slot3, 150.0) == 17640);
  13958. CHECK (echoAt (*slot2, 120.0) == 16538);
  13959. CHECK (echoAt (*slot2, 140.0) == 14175);
  13960. }
  13961. }
  13962. #include "UiRackMixerTests.inc"
  13963. // ---------------------------------------------------------------------------
  13964. // FL Studio integration (core/FlPluginDb.h): synthetic plugin-database fixtures, no Image-Line files
  13965. namespace
  13966. {
  13967. std::vector<std::uint8_t> testWrapperBlob (std::uint32_t type, const std::string& name, const std::string& path,
  13968. const std::vector<std::uint8_t>& classId = {}, const std::string& clapId = {})
  13969. {
  13970. std::vector<std::uint8_t> b;
  13971. auto u32 = [&b] (std::uint32_t v) { for (int i = 0; i < 4; ++i) b.push_back ((std::uint8_t) (v >> (8 * i))); };
  13972. auto record = [&] (std::uint32_t id, const std::vector<std::uint8_t>& payload) {
  13973. u32 (id);
  13974. u32 ((std::uint32_t) payload.size());
  13975. u32 (0);
  13976. b.insert (b.end(), payload.begin(), payload.end());
  13977. };
  13978. u32 (12);
  13979. record (0x32, { (std::uint8_t) type, 0, 0, 0 });
  13980. if (! classId.empty())
  13981. record (0x34, classId);
  13982. if (! clapId.empty())
  13983. record (0x3A, std::vector<std::uint8_t> (clapId.begin(), clapId.end()));
  13984. record (0x36, std::vector<std::uint8_t> (name.begin(), name.end()));
  13985. record (0x37, std::vector<std::uint8_t> (path.begin(), path.end()));
  13986. return b;
  13987. }
  13988. } // namespace
  13989. TEST (fl_plugin_db_nfo_parsing)
  13990. {
  13991. const std::string nfo = "ps_name=Vital\r\n"
  13992. "ps_file_name_0=C:\\Program Files\\Common Files\\VST3\\Vital.vst3\r\n"
  13993. "ps_file_vendorname_0=Vital Audio\r\n"
  13994. "ps_file_type_0=2\r\n"
  13995. "ps_file_name_1=C:\\VSTPlugins\\Vital_x64.dll\r\n"
  13996. "ps_file_type_1=2\r\n"
  13997. "ps_file_magic_1=Vita\r\n"
  13998. "ps_file_name_2=not a plugin\r\n"
  13999. "garbage line without equals\r\n";
  14000. const auto entries = lynx::parseFlPluginNfo (nfo, "D:\\FL\\Presets\\Plugin database\\Installed\\Generators\\VST3\\Vital.nfo");
  14001. CHECK (entries.size() == 2);
  14002. if (entries.size() == 2)
  14003. {
  14004. CHECK (entries[0].name == "Vital" && entries[0].vendor == "Vital Audio" && entries[0].role == 2);
  14005. CHECK (entries[0].format == lynx::ForeignPlugin::Format::vst3);
  14006. CHECK (entries[1].format == lynx::ForeignPlugin::Format::vst2 && entries[1].vst2Id == 0x56697461u);
  14007. }
  14008. // no type field: the role comes from the Effects / Generators folder; an ID in GUID form is read
  14009. const auto fx = lynx::parseFlPluginNfo ("ps_name=Fancy Comp\nps_file_filename_0=C:/CLAP/Fancy Comp.clap\n"
  14010. "ps_file_guid_0={01234567-89AB-CDEF-0123-456789ABCDEF}\n",
  14011. "C:\\x\\Installed\\Effects\\CLAP\\Fancy Comp.nfo");
  14012. CHECK (fx.size() == 1 && fx[0].role == 1 && fx[0].format == lynx::ForeignPlugin::Format::clap && fx[0].hasClassId
  14013. && fx[0].classId[0] == 0x01 && fx[0].classId[15] == 0xEF);
  14014. CHECK (lynx::parseFlPluginNfo ("Tip=All of the plugins found in your system.").empty());
  14015. CHECK (lynx::pluginNameKey ("Serum_x64") == "serum");
  14016. CHECK (lynx::pluginNameKey ("Vital.VST3") == "vital");
  14017. CHECK (lynx::pluginNameKey ("Pro-Q 3") == "proq3");
  14018. CHECK (lynx::pluginFormatFromPath ("a\\b.CLAP") == lynx::ForeignPlugin::Format::clap);
  14019. }
  14020. TEST (fl_plugin_db_fst_parsing_and_matching)
  14021. {
  14022. std::vector<std::uint8_t> cid;
  14023. for (int i = 0; i < 16; ++i)
  14024. cid.push_back ((std::uint8_t) (0x10 + i));
  14025. // an .fst of the database: an FL preset whose 0xD5 holds the wrapper record
  14026. FlpBuilder fst;
  14027. fst.ascii (0xC9, "Fruity Wrapper");
  14028. fst.data (0xD5, testWrapperBlob (8, "Surge XT", "E:\\Plugins\\VST3\\Surge XT.vst3", cid));
  14029. lynx::FlPluginDbEntry e;
  14030. CHECK (lynx::parseFlPluginFst (fst.bytes, e));
  14031. CHECK (e.name == "Surge XT" && e.format == lynx::ForeignPlugin::Format::vst3 && e.hasClassId && e.classId[0] == 0x10);
  14032. lynx::FlPluginDbEntry bad;
  14033. CHECK (! lynx::parseFlPluginFst (std::vector<std::uint8_t> { 'F', 'L', 'h', 'd' }, bad));
  14034. lynx::FlPluginDb db;
  14035. db.add (e);
  14036. // the .nfo of the same plugin fills in vendor and role
  14037. for (const auto& n : lynx::parseFlPluginNfo ("ps_name=Surge XT\nps_file_name_0=E:/Plugins/VST3/Surge XT.vst3\n"
  14038. "ps_file_vendorname_0=Surge Synth Team\nps_file_type_0=2\n"))
  14039. db.add (n);
  14040. db.add ({ "Clapper", "", "E:\\Plugins\\CLAP\\Clapper.clap", "", lynx::ForeignPlugin::Format::clap, 1, {}, false, "org.clapper", 0 });
  14041. db.add ({ "Big Verb", "", "E:\\Old VST\\BigVerb_x64.dll", "", lynx::ForeignPlugin::Format::vst2, 1, {}, false, "", 0x42566262u });
  14042. db.add ({ "Bundle", "", "F:\\VST3\\Bundle.vst3\\Contents\\x86_64-win\\Bundle.vst3", "", lynx::ForeignPlugin::Format::vst3, 2, {}, false, "", 0 });
  14043. CHECK (db.entries().size() == 4);
  14044. CHECK (db.entries()[0].vendor == "Surge Synth Team" && db.entries()[0].role == 2);
  14045. // by VST3 class ID even when the project saved another path and name
  14046. lynx::ForeignPlugin p;
  14047. p.format = lynx::ForeignPlugin::Format::vst3;
  14048. p.hasClassId = true;
  14049. std::copy (cid.begin(), cid.end(), p.classId.begin());
  14050. p.name = "Renamed";
  14051. p.path = "C:\\Other PC\\Surge.vst3";
  14052. CHECK (db.match (p, true) == &db.entries()[0]);
  14053. // by CLAP id, by VST2 id
  14054. lynx::ForeignPlugin c;
  14055. c.format = lynx::ForeignPlugin::Format::clap;
  14056. c.clapId = "org.clapper";
  14057. CHECK (db.match (c, false) == &db.entries()[1]);
  14058. lynx::ForeignPlugin v;
  14059. v.format = lynx::ForeignPlugin::Format::vst2;
  14060. v.vst2Id = 0x42566262u;
  14061. CHECK (db.match (v, false) == &db.entries()[2]);
  14062. // by name (architecture suffix ignored), only in the same role
  14063. lynx::ForeignPlugin byName;
  14064. byName.format = lynx::ForeignPlugin::Format::vst2;
  14065. byName.name = "BigVerb x64";
  14066. CHECK (db.match (byName, false) == &db.entries()[2]);
  14067. CHECK (db.match (byName, true) == nullptr);
  14068. lynx::ForeignPlugin unknown;
  14069. unknown.format = lynx::ForeignPlugin::Format::vst3;
  14070. unknown.name = "Nothing Like It";
  14071. CHECK (db.match (unknown, true) == nullptr);
  14072. // folders: the bundle's parent, deduplicated
  14073. const auto folders = db.pluginFolders();
  14074. CHECK (folders.size() == 4);
  14075. CHECK (std::find (folders.begin(), folders.end(), "F:\\VST3") != folders.end());
  14076. CHECK (std::find (folders.begin(), folders.end(), "E:\\Plugins\\VST3") != folders.end());
  14077. // targeted scan picks: same file, or same name key
  14078. CHECK (lynx::pluginFileMatches ("E:/Plugins/VST3/Surge XT.vst3", p, &db.entries()[0]));
  14079. CHECK (lynx::pluginFileMatches ("C:\\Program Files\\Common Files\\VST3\\BigVerb.vst3", byName, nullptr));
  14080. CHECK (! lynx::pluginFileMatches ("C:\\VST3\\Other.vst3", byName, nullptr));
  14081. // the plugins an .flp uses: wrapper channels and mixer slots
  14082. flp::Project project;
  14083. flp::Channel ch;
  14084. ch.pluginName = "Fruity Wrapper";
  14085. ch.pluginState = testWrapperBlob (8, "Surge XT", "C:\\VST3\\Surge XT.vst3", cid);
  14086. project.channels.push_back (ch);
  14087. ch.pluginName = "3x Osc";
  14088. project.channels.push_back (ch);
  14089. flp::MixerInsert insert;
  14090. insert.slots.push_back ({ 0, "Fruity Wrapper", testWrapperBlob (12, "Clapper", "C:\\CLAP\\Clapper.clap", {}, "org.clapper") });
  14091. insert.slots.push_back ({ 1, "Fruity Wrapper", testWrapperBlob (3, "DX thing", "C:\\dx.dll") }); // DirectX: not hosted
  14092. project.mixer.push_back (insert);
  14093. const auto uses = lynx::flpForeignPlugins (project);
  14094. CHECK (uses.size() == 2);
  14095. CHECK (uses.size() == 2 && uses[0].instrument && ! uses[1].instrument && uses[1].plugin.clapId == "org.clapper");
  14096. }
  14097. TEST (fl_sample_relocation)
  14098. {
  14099. std::set<std::string> files { "D:\\Docs\\Image-Line\\FL Studio\\Audio\\Recorded\\take.wav", "C:\\Program Files\\FL 2026\\Data\\Patches\\kick.wav",
  14100. "C:\\Users\\me\\Music\\loop.wav", "E:\\Songs\\Track\\Samples\\snare.wav", "E:\\Songs\\Track\\hat.wav" };
  14101. auto exists = [&] (const std::string& p) { return files.count (p) > 0; };
  14102. lynx::FlPathRoots roots;
  14103. roots.userData = "D:\\Docs\\Image-Line\\FL Studio";
  14104. roots.factoryData = "C:\\Program Files\\FL 2026";
  14105. roots.home = "C:\\Users\\me";
  14106. const std::string project = "E:\\Songs\\Track";
  14107. // variable resolved, file in place
  14108. CHECK (lynx::locateFlSample ("%FLStudioFactoryData%\\Data\\Patches\\kick.wav", roots, project, exists) == "C:\\Program Files\\FL 2026\\Data\\Patches\\kick.wav");
  14109. // another PC's FL user data / FL install / user profile
  14110. CHECK (lynx::locateFlSample ("C:\\Users\\bob\\Documents\\Image-Line\\FL Studio\\Audio\\Recorded\\take.wav", roots, project, exists)
  14111. == "D:\\Docs\\Image-Line\\FL Studio\\Audio\\Recorded\\take.wav");
  14112. CHECK (lynx::locateFlSample ("C:\\Program Files (x86)\\Image-Line\\FL Studio 20\\Data\\Patches\\kick.wav", roots, project, exists)
  14113. == "C:\\Program Files\\FL 2026\\Data\\Patches\\kick.wav");
  14114. CHECK (lynx::locateFlSample ("C:/Users/bob/Music/loop.wav", roots, project, exists) == "C:\\Users\\me\\Music\\loop.wav");
  14115. // next to the project, also in a sub-folder named in the original path
  14116. CHECK (lynx::locateFlSample ("Z:\\Old\\Samples\\snare.wav", roots, project, exists) == "E:\\Songs\\Track\\Samples\\snare.wav");
  14117. CHECK (lynx::locateFlSample ("Z:\\Old\\Stuff\\hat.wav", roots, project, exists) == "E:\\Songs\\Track\\hat.wav");
  14118. CHECK (lynx::locateFlSample ("Z:\\nowhere.wav", roots, project, exists).empty());
  14119. CHECK (lynx::locateFlSample ("", roots, project, exists).empty());
  14120. }
  14121. // --- REA round 64: SimSynth (exact engine) and Wasp / Wasp XT (FL 2026's silent placeholder) ---------------------
  14122. namespace
  14123. {
  14124. // a SimSynth state written from scratch (REA_rodada64 §1.1 layout: 45 doubles, 19 unused ints, 19 switches, 12 unused
  14125. // ints, version)
  14126. std::vector<std::uint8_t> simSynthTestState (const std::array<double, 45>& P, const std::array<int, 19>& S, int version = 14)
  14127. {
  14128. std::vector<std::uint8_t> b (564, 0);
  14129. std::memcpy (b.data(), P.data(), 360);
  14130. std::memcpy (b.data() + 360 + 76, S.data(), 76);
  14131. std::memcpy (b.data() + 560, &version, 4);
  14132. return b;
  14133. }
  14134. // the 0xD5 records of an FL preset file (.fst: the FLhd/FLdt event stream) that follow a plugin name `plugin`
  14135. std::vector<std::vector<std::uint8_t>> pluginStatesIn (const std::filesystem::path& file, const std::string& plugin)
  14136. {
  14137. std::vector<std::vector<std::uint8_t>> out;
  14138. std::ifstream in (file, std::ios::binary);
  14139. const std::vector<std::uint8_t> b ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  14140. std::size_t i = 0;
  14141. while (i + 8 <= b.size() && std::memcmp (b.data() + i, "FLdt", 4) != 0)
  14142. ++i;
  14143. i += 8;
  14144. std::string name;
  14145. while (i < b.size())
  14146. {
  14147. const auto id = b[i++];
  14148. std::size_t n = id < 64 ? 1 : id < 128 ? 2 : id < 192 ? 4 : 0;
  14149. if (id >= 192)
  14150. for (int shift = 0; i < b.size(); shift += 7)
  14151. {
  14152. const auto c = b[i++];
  14153. n |= (std::size_t) (c & 0x7F) << shift;
  14154. if (c < 0x80)
  14155. break;
  14156. }
  14157. if (i + n > b.size())
  14158. break;
  14159. if (id == 0xC9)
  14160. {
  14161. name.clear();
  14162. for (std::size_t k = 0; k < n; ++k)
  14163. if (b[i + k] != 0)
  14164. name += (char) b[i + k]; // ASCII or UTF-16LE (zero bytes skipped)
  14165. }
  14166. if (id == 0xD5 && name == plugin)
  14167. out.emplace_back (b.begin() + (std::ptrdiff_t) i, b.begin() + (std::ptrdiff_t) (i + n));
  14168. i += n;
  14169. }
  14170. return out;
  14171. }
  14172. } // namespace
  14173. TEST (simsynth_engine)
  14174. {
  14175. // A: pulse + octave layer, half-cosine with unison and two octave layers, sine/sine⁴ mix, square LFO → pitch with
  14176. // delay, filter envelope, LP/BP mix, chorus. B (version 7): triangle ring-modulated by filtered noise, sample &
  14177. // hold LFO → cutoff, retrigger, own filter envelope, chorus off
  14178. 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,
  14179. 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 };
  14180. 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 };
  14181. 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,
  14182. 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 };
  14183. 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 };
  14184. const auto blobA = simSynthTestState (A, SA), blobB = simSynthTestState (B, SB, 7);
  14185. dumpIfAsked ("LYNX_DUMP_SIMSYNTH", blobA);
  14186. CHECK (lynx::simSynthStateUsable (blobA) && ! lynx::simSynthStateUsable (std::vector<std::uint8_t> (blobA.begin(), blobA.begin() + 563)));
  14187. const auto hp = lynx::simSynthHostParams (blobA);
  14188. // knobs go through ⌊65535·v⌋, the coarse pitches through 48 steps, the switches and wave selectors as they are
  14189. CHECK (hp.size() == 58u && hp[0] == 52428 && hp[27] == 10080 && hp[41] == 1 && hp[54] == 0 && hp[56] == 4);
  14190. // golden samples of the installation's SimSynth_x64.dll (round-64 EXEC harness: created, state restored, 44.1 kHz,
  14191. // host blocks of 512, old voice record) — LYNX's engine renders bit-identically: C5 0–30000, E5 Final.Vol 0.8 pan
  14192. // 16 11025–40000, G5 pan −20 +25 cents 22050–52000
  14193. const std::vector<lynx::simsynth::RefNote> notes = { { 0, 60, 1.0f, 0, 0, 0.0f, 0.0f, 30000 },
  14194. { 11025, 64, 0.8f, 16, 0, 0.0f, 0.0f, 40000 },
  14195. { 22050, 67, 1.0f, -20, 25, 0.0f, 0.0f, 52000 } };
  14196. struct Golden { int i; float l, r; };
  14197. 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 } };
  14198. 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 } };
  14199. for (int which = 0; which < 2; ++which)
  14200. {
  14201. std::vector<std::int64_t> counters;
  14202. const auto y = lynx::simsynth::renderReference (which == 0 ? blobA : blobB, notes, 44100.0, 66150, 512, &counters);
  14203. CHECK (y.size() == 2 * 66150u);
  14204. if (y.size() != 2 * 66150u)
  14205. continue;
  14206. // the random-table index after creation / state / rate set equals the DLL's (276 / 696 / 756 and 276 / 492 / 528)
  14207. CHECK (counters.size() == 4u && counters[2] == 276 && counters[3] == (which == 0 ? 696 : 492) && counters[0] == (which == 0 ? 756 : 528));
  14208. for (const auto& g : which == 0 ? std::vector (std::begin (goldenA), std::end (goldenA)) : std::vector (std::begin (goldenB), std::end (goldenB)))
  14209. CHECK (y[2 * (std::size_t) g.i] == g.l && y[2 * (std::size_t) g.i + 1] == g.r);
  14210. double e = 0.0;
  14211. for (auto v : y)
  14212. e += (double) v * v;
  14213. CHECK (std::abs (std::sqrt (e / (double) y.size()) - (which == 0 ? 0.290665331870 : 0.992116840643)) < 1e-9);
  14214. }
  14215. // the instrument: same engine behind LYNX's channel gains (V = 1, centre → Final.Vol 1, Final.Pan 0)
  14216. lynx::ChannelDef c;
  14217. c.instrument = lynx::InstrumentKind::simsynth;
  14218. c.pluginState = blobA;
  14219. c.engineRev = 1; // (round 190om: the revision before the velocity law, every note at Final.Vol 1)
  14220. auto inst = lynx::makeInstrument (lynx::InstrumentKind::simsynth);
  14221. inst->prepare (44100.0);
  14222. inst->configure (c, 120.0);
  14223. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  14224. for (int k = 0; k < 16; ++k)
  14225. {
  14226. if (k == 0)
  14227. inst->noteOn (60, 0.8f);
  14228. if (k == 10)
  14229. inst->noteOff (60);
  14230. inst->render (l.data() + 512 * k, r.data() + 512 * k, 512, 1.0f, 1.0f);
  14231. }
  14232. const auto ref = lynx::simsynth::renderReference (blobA, { { 0, 60, 1.0f, 0, 0, 0.0f, 0.0f, 5120 } }, 44100.0, 8192);
  14233. double diff = 0.0, energy = 0.0;
  14234. for (int i = 0; i < 8192; ++i)
  14235. {
  14236. 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]) });
  14237. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  14238. }
  14239. CHECK (diff == 0.0 && energy > 1e-3);
  14240. // the installation's SimSynth presets (when the FL data folder is given): every state usable and sounding
  14241. if (! realFlpDir.empty())
  14242. {
  14243. const auto folder = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "SimSynth";
  14244. int presets = 0, usable = 0, sounding = 0;
  14245. if (std::filesystem::exists (folder))
  14246. for (const auto& entry : std::filesystem::directory_iterator (folder))
  14247. for (const auto& st : pluginStatesIn (entry.path(), "SimSynth"))
  14248. {
  14249. ++presets;
  14250. if (! lynx::simSynthStateUsable (st))
  14251. continue;
  14252. ++usable;
  14253. const auto y = lynx::simsynth::renderReference (st, { { 0, 48, 1.0f, 0, 0, 0.0f, 0.0f, 11025 } }, 44100.0, 22050);
  14254. double e = 0.0;
  14255. bool finite = true;
  14256. for (auto v : y)
  14257. finite = finite && std::isfinite (v), e += (double) v * v;
  14258. CHECK (finite);
  14259. sounding += e > 1e-9 ? 1 : 0;
  14260. }
  14261. std::printf (" SimSynth presets: %d, %d usable, %d sounding\n", presets, usable, sounding);
  14262. CHECK (presets == 0 || (usable == presets && presets >= 70 && sounding >= presets - 2));
  14263. }
  14264. }
  14265. TEST (wasp_is_a_silent_placeholder)
  14266. {
  14267. // FL 2026's Wasp / Wasp XT DLL keeps the state and plays nothing: so does LYNX (REA_rodada64 §5)
  14268. auto inst = lynx::makeInstrument (lynx::InstrumentKind::wasp);
  14269. inst->prepare (44100.0);
  14270. std::vector<float> l (1024, 0.0f), r (1024, 0.0f);
  14271. inst->noteOn (60, 1.0f);
  14272. inst->render (l.data(), r.data(), 1024, 1.0f, 1.0f);
  14273. CHECK (inst->idle() && std::all_of (l.begin(), l.end(), [] (float v) { return v == 0.0f; }));
  14274. // import: a SimSynth channel plays on the exact engine, Wasp and Wasp XT channels become the silent placeholder with
  14275. // their state kept
  14276. std::vector<std::uint8_t> ss (564, 0), wasp (169, 1);
  14277. const int version = 14;
  14278. std::memcpy (ss.data() + 560, &version, 4);
  14279. FlpBuilder b;
  14280. b.ascii (0xC7, "25.2.5.5319");
  14281. int index = 0;
  14282. for (const auto& [name, state] : { std::pair<std::u16string, std::vector<std::uint8_t>> { u"SimSynth", ss }, { u"Wasp", wasp }, { u"Wasp XT", wasp } })
  14283. {
  14284. b.u8 (0x40); b.u16 ((std::uint16_t) index++);
  14285. b.u8 (0x15); b.u8 (2);
  14286. b.utf16 (0xC9, name);
  14287. b.data (0xD5, state);
  14288. }
  14289. const auto song = lynx::importFlp (flp::parseProject (b.bytes));
  14290. CHECK (song.channels.size() == 3u);
  14291. if (song.channels.size() == 3u)
  14292. {
  14293. CHECK (song.channels[0].instrument == lynx::InstrumentKind::simsynth && song.channels[0].pluginState == ss);
  14294. CHECK (song.channels[1].instrument == lynx::InstrumentKind::wasp && song.channels[1].pluginState == wasp);
  14295. CHECK (song.channels[2].instrument == lynx::InstrumentKind::wasp);
  14296. }
  14297. }
  14298. // --- REA round 67: Fruity Granulizer (exact engine) --------------------------------------------------------------
  14299. namespace
  14300. {
  14301. // our own deterministic test sample (a fading 220 Hz sine plus LCG noise per channel), as r67/py/gold67.py builds it
  14302. lynx::SampleData granulizerTestSample (int n, int ch)
  14303. {
  14304. lynx::SampleData d;
  14305. std::uint32_t s = 12345;
  14306. for (int i = 0; i < n; ++i)
  14307. {
  14308. const auto base = 0.5 * std::sin (2.0 * 3.141592653589793 * 220.0 * i / 44100.0) * (1.0 - 0.7 * i / n);
  14309. for (int c = 0; c < ch; ++c)
  14310. {
  14311. s = s * 1664525u + 1013904223u;
  14312. const auto v = (float) (base + 0.2 * ((double) (std::int32_t) s * (1.0 / 2147483648.0)));
  14313. (c == 0 ? d.left : d.right).push_back (v);
  14314. }
  14315. }
  14316. return d;
  14317. }
  14318. std::vector<std::uint8_t> granulizerTestState (const std::array<int, 12>& P, int keyMode = 0)
  14319. {
  14320. std::vector<std::uint8_t> b (60, 0);
  14321. const std::int32_t v = 5;
  14322. std::memcpy (b.data(), &v, 4);
  14323. std::memcpy (b.data() + 4, P.data(), 48);
  14324. std::memcpy (b.data() + 56, &keyMode, 4);
  14325. return b;
  14326. }
  14327. } // namespace
  14328. TEST (granulizer_engine)
  14329. {
  14330. // REA_rodada67: states written from scratch against our reference model (granmodel.py, which renders like the
  14331. // installation's DLL within −128 dB: the DLL's rcpps and IPP sines are not replicated)
  14332. const auto v5 = granulizerTestState ({ 204, 50, 611, 611, 0, 180, 0, 0, 0, 0, 0, 228 });
  14333. CHECK (lynx::granulizerStateUsable (v5));
  14334. const auto n5 = lynx::granulizerNormalisedState (v5, {});
  14335. CHECK (n5.size() == 428 && n5[0] == 7 && lynx::granulizerParam (n5, 0) == 204 && lynx::granulizerParam (n5, 11) == 228
  14336. && ! lynx::granulizerParam (n5, 12));
  14337. // version 1 (10 parameters): parameter 11 takes the plugin's 0xf8
  14338. {
  14339. std::vector<std::uint8_t> v1 (v5.begin(), v5.begin() + 44);
  14340. v1[0] = 1;
  14341. const auto n1 = lynx::granulizerNormalisedState (v1, {});
  14342. CHECK (lynx::granulizerParam (n1, 9) == 0 && lynx::granulizerParam (n1, 11) == 0xf8 && lynx::granulizerParam (n1, 3) == 611);
  14343. std::vector<std::uint8_t> bad (v5);
  14344. bad[0] = 9;
  14345. CHECK (! lynx::granulizerStateUsable (bad) && lynx::granulizerNormalisedState (bad, {}).empty());
  14346. auto edited = n5;
  14347. CHECK (lynx::granulizerSetParam (edited, 3, -5000) && lynx::granulizerParam (edited, 3) == -1000);
  14348. }
  14349. using N = lynx::granulizer::RefNote;
  14350. struct Golden { std::vector<std::uint8_t> state; int ch, fs, rate; lynx::granulizer::RefOptions opt; std::vector<N> notes;
  14351. double el, er; std::tuple<int, float, float> at[6]; };
  14352. auto check = [] (const Golden& g) {
  14353. auto smp = granulizerTestSample (20000, g.ch);
  14354. smp.sampleRate = g.rate;
  14355. const auto y = lynx::granulizer::renderReference (lynx::granulizerNormalisedState (g.state, {}), smp, g.notes, (double) g.fs, 16384, g.opt);
  14356. CHECK (y.size() == 2 * 16384);
  14357. if (y.size() != 2 * 16384)
  14358. return;
  14359. double el = 0.0, er = 0.0;
  14360. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  14361. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  14362. CHECK (std::abs (el - g.el) <= 1e-6 * g.el && std::abs (er - g.er) <= 1e-6 * g.er);
  14363. for (const auto& [i, l, r] : g.at)
  14364. CHECK (goldenSampleClose (y[2 * (std::size_t) i], l, granulizerTolerance())
  14365. && goldenSampleClose (y[2 * (std::size_t) i + 1], r, granulizerTolerance()));
  14366. };
  14367. lynx::granulizer::RefOptions o1, o2, o3;
  14368. o1.points = o2.points = o3.points = 12; // the plugin's own default quality (these goldens predate FL's render value)
  14369. o2.block = 333, o2.seed = 777;
  14370. o3.block = 64, o3.seed = 5;
  14371. // the plugin's defaults, two notes (one pitched by +23 cents and released: the processor's end fade)
  14372. 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,
  14373. { { 100, 0.057010360062122345f, 0.08960580080747604f }, { 1000, 0.07210570573806763f, -0.048257164657115936f },
  14374. { 2500, 0.14769265055656433f, -0.0194698516279459f }, { 4000, 0.11169229447841644f, 0.17487481236457825f },
  14375. { 7777, -0.4309714436531067f, -0.17027156054973602f }, { 12000, -0.11129666864871979f, -0.19622492790222168f } } });
  14376. // backwards, randomness, depth/speed wobble, stereo separation, loop, sample start; a mono 44.1 kHz sample at 48 kHz
  14377. check ({ granulizerTestState ({ 150, 120, 300, -480, 200, 90, 120, 40, 1, 0, 900, 300 }), 1, 48000, 44100, o2,
  14378. { { 0, 55, 1.0, 0.2, -37.0, 12000 }, { 5000, 67, 0.5, 0.0, 0.0, -1 } }, 959.5011214226065, 1294.4823684530272,
  14379. { { 100, 0.02567935362458229f, 0.010721667669713497f }, { 1000, 0.3007732629776001f, 0.08214527368545532f },
  14380. { 2500, 0.435413658618927f, 0.09068503230810165f }, { 4000, -0.24611327052116394f, -0.20149046182632446f },
  14381. { 7777, -0.008478119969367981f, 0.31221628189086914f }, { 12000, 0.203709632f, 0.324944794f } } });
  14382. // key mode 1 (the sample spread over two octaves), freeze, hard stereo separation; 48 kHz sample at 96 kHz, 64-sample blocks
  14383. check ({ granulizerTestState ({ 300, 0, 800, 700, 0, 180, 0, 64, 0, 1, 2000, 100 }, 1), 2, 96000, 48000, o3,
  14384. { { 0, 62, 0.9, 0.0, 0.0, -1 }, { 4000, 50, 0.7, 0.0, 11.0, -1 } }, 366.79556669810813, 0.00825590391720353,
  14385. { { 100, 0.004204070195555687f, 7.680895437786243e-20f }, { 1000, 0.008516596630215645f, 3.0263170437258543e-19f },
  14386. { 2500, 0.06948182731866837f, 4.5391312775082004e-18f }, { 4000, -0.01754589192569256f, -4.895453905423713e-19f },
  14387. { 7777, 0.10338862985372543f, 2.344363859234514e-18f }, { 12000, -0.09385982155799866f, -1.3493021437162966e-17f } } });
  14388. // the instrument: silent without a sample (FL's plugin refuses the note), sounding with one
  14389. {
  14390. auto silent = lynx::makeInstrument (lynx::InstrumentKind::granulizer);
  14391. silent->prepare (44100.0);
  14392. std::vector<float> l (2048, 0.0f), r (2048, 0.0f);
  14393. silent->noteOn (60, 1.0f);
  14394. silent->render (l.data(), r.data(), 2048, 1.0f, 1.0f);
  14395. CHECK (silent->idle() && std::all_of (l.begin(), l.end(), [] (float v) { return v == 0.0f; }));
  14396. auto smp = std::make_shared<lynx::SampleData> (granulizerTestSample (20000, 2));
  14397. auto inst = lynx::makeInstrument (lynx::InstrumentKind::granulizer, smp);
  14398. lynx::ChannelDef def;
  14399. def.instrument = lynx::InstrumentKind::granulizer;
  14400. def.pluginState = n5;
  14401. inst->configure (def, 140.0);
  14402. inst->prepare (44100.0);
  14403. inst->noteOn (60, 0.8f);
  14404. inst->render (l.data(), r.data(), 2048, 1.0f, 1.0f);
  14405. double e = 0.0;
  14406. for (auto v : l)
  14407. e += (double) v * v;
  14408. CHECK (! inst->idle() && e > 1.0);
  14409. inst->noteOff (60);
  14410. for (int k = 0; k < 4; ++k)
  14411. inst->render (l.data(), r.data(), 512, 1.0f, 1.0f);
  14412. CHECK (inst->idle()); // the 10 ms end fade of the plugin's voice processor
  14413. }
  14414. // tempo-synced grains follow the song position in LYNX's 960-PPQ ticks: an instance fed setSongPosition every
  14415. // block while playing must stay in phase with one that free-runs its own tick clock from 0 (it was 10x off)
  14416. {
  14417. auto smp = std::make_shared<lynx::SampleData> (granulizerTestSample (20000, 2));
  14418. lynx::ChannelDef def;
  14419. def.instrument = lynx::InstrumentKind::granulizer;
  14420. def.pluginState = n5;
  14421. auto fed = lynx::makeInstrument (lynx::InstrumentKind::granulizer, smp);
  14422. auto free = lynx::makeInstrument (lynx::InstrumentKind::granulizer, smp);
  14423. for (auto* g : { fed.get(), free.get() })
  14424. {
  14425. g->configure (def, 140.0);
  14426. g->prepare (44100.0);
  14427. g->noteOn (60, 0.8f);
  14428. }
  14429. const auto tps = 140.0 * lynx::ppq / (60.0 * 44100.0);
  14430. std::vector<float> l1 (300), r1 (300), l2 (300), r2 (300);
  14431. double diff = 0.0, energy = 0.0;
  14432. for (int block = 0, done = 0; block < 60; ++block, done += 300)
  14433. {
  14434. fed->setSongPosition ((double) done * tps);
  14435. std::fill (l1.begin(), l1.end(), 0.0f), std::fill (r1.begin(), r1.end(), 0.0f);
  14436. std::fill (l2.begin(), l2.end(), 0.0f), std::fill (r2.begin(), r2.end(), 0.0f);
  14437. fed->render (l1.data(), r1.data(), 300, 1.0f, 1.0f);
  14438. free->render (l2.data(), r2.data(), 300, 1.0f, 1.0f);
  14439. for (int j = 0; j < 300; ++j)
  14440. diff = std::max (diff, (double) std::fabs (l1[(std::size_t) j] - l2[(std::size_t) j])),
  14441. energy += (double) l2[(std::size_t) j] * l2[(std::size_t) j];
  14442. }
  14443. CHECK (energy > 1.0 && diff < 1e-4);
  14444. }
  14445. // import: a Fruity Granulizer channel plays on the exact engine with its sample
  14446. {
  14447. FlpBuilder b;
  14448. b.ascii (0xC7, "25.2.5.5319");
  14449. b.u8 (0x40); b.u16 (0);
  14450. b.u8 (0x15); b.u8 (2);
  14451. b.utf16 (0xC9, u"Fruity Granulizer");
  14452. b.data (0xD5, v5);
  14453. b.utf16 (0xC4, u"%FLStudioFactoryData%\\Data\\Patches\\Packs\\Drums\\Kicks\\808 Kick.wav");
  14454. lynx::FlpImportOptions options;
  14455. options.roots.factoryData = "C:\\FL";
  14456. options.fileExists = [] (const std::string& path) { return path == "C:\\FL\\Data\\Patches\\Packs\\Drums\\Kicks\\808 Kick.wav"; };
  14457. lynx::FlpImportReport report;
  14458. options.report = &report;
  14459. const auto song = lynx::importFlp (flp::parseProject (b.bytes), options);
  14460. CHECK (song.channels.size() == 1u);
  14461. if (song.channels.size() == 1u)
  14462. {
  14463. CHECK (song.channels[0].instrument == lynx::InstrumentKind::granulizer && song.channels[0].pluginState == n5);
  14464. CHECK (song.channels[0].samplePath == "C:\\FL\\Data\\Patches\\Packs\\Drums\\Kicks\\808 Kick.wav");
  14465. }
  14466. CHECK (report.converted["Fruity Granulizer"] == 1);
  14467. }
  14468. }
  14469. // --- REA round 67: Drumaxx (exact engine) -------------------------------------------------------------------------
  14470. namespace
  14471. {
  14472. // our own deterministic 16-pad kit (LCG-chosen parameters), as r67/py/gold67d.py builds it for the DLL
  14473. std::vector<std::uint8_t> drumaxxTestState (bool eq)
  14474. {
  14475. std::uint32_t s = 20261015u;
  14476. auto u = [&s] {
  14477. s = s * 1664525u + 1013904223u;
  14478. return (float) ((double) (s >> 8) * (1.0 / 16777216.0));
  14479. };
  14480. // (a product rounded on its own: no fused multiply-add)
  14481. auto mul = [] (float a, float b) {
  14482. volatile float p = a * b;
  14483. return (float) p;
  14484. };
  14485. static const int targets[] = { 0, 9, 10, 16, 21, 23, 26 };
  14486. std::array<float, 720> P {};
  14487. for (int k = 0; k < 16; ++k)
  14488. for (int i = 0; i < 44; ++i)
  14489. {
  14490. auto v = u();
  14491. if (i == 0)
  14492. v = 0.5f + mul (v, 0.5f);
  14493. else if ((i >= 7 && i <= 18) || (i >= 20 && i <= 34))
  14494. v = 0.2f + mul (v, 0.6f);
  14495. else if (i == 2 || i == 3)
  14496. v = 0.0f;
  14497. else if (i == 4)
  14498. v = (float) (36 + k);
  14499. else if (i == 5)
  14500. v = v < 0.5f ? 0.0f : 1.0f;
  14501. else if (i == 6)
  14502. v = (float) (int) (v * 4.0f);
  14503. else if (i >= 35 && i <= 38)
  14504. v = (float) targets[(int) (v * 7.0f)];
  14505. P[(std::size_t) (44 * k + i)] = v;
  14506. }
  14507. for (int i = 0; i < 16; ++i)
  14508. {
  14509. auto v = u();
  14510. if (i == 1)
  14511. v = 0.3f + mul (v, 0.4f);
  14512. else if (i == 3)
  14513. v = eq ? 1.0f : 0.0f;
  14514. else if (i >= 4 && i <= 11)
  14515. v = 0.35f + mul (v, 0.3f);
  14516. else if (i == 12)
  14517. v = 1.0f;
  14518. else if (i == 13)
  14519. v = v * 0.1f;
  14520. else if (i == 0 || i == 15)
  14521. v = 0.0f;
  14522. P[(std::size_t) (704 + i)] = v;
  14523. }
  14524. std::vector<std::uint8_t> b;
  14525. auto put32 = [&b] (std::uint32_t v) { for (int i = 0; i < 4; ++i) b.push_back ((std::uint8_t) (v >> (8 * i))); };
  14526. b.insert (b.end(), { 'S', 'S', 'L', 'F' });
  14527. put32 (21636u);
  14528. put32 (0x444d5333u); // engine stream version 3
  14529. const std::string name = "LYNX test kit";
  14530. b.insert (b.end(), name.begin(), name.end());
  14531. b.resize (b.size() + 256 - name.size(), 0);
  14532. const auto at = b.size();
  14533. b.resize (at + 720 * 4);
  14534. std::memcpy (b.data() + at, P.data(), 720 * 4);
  14535. for (int k = 0; k < 16; ++k)
  14536. {
  14537. b.resize (b.size() + 0x400, 0);
  14538. b.push_back (100), b.push_back (100); // pattern length, trigger key
  14539. }
  14540. b.resize (b.size() + 0x800, 0);
  14541. for (int k = 0; k < 16; ++k)
  14542. b.push_back (k >= 2 && k <= 4 ? 1 : 0); // pads 3…5 in cut group 1
  14543. b.resize (b.size() + 16, 0); // every pad on the master
  14544. return b;
  14545. }
  14546. } // namespace
  14547. TEST (drumaxx_engine)
  14548. {
  14549. const auto st = drumaxxTestState (true);
  14550. CHECK (st.size() == 21644u && lynx::drumaxxStateUsable (st) && lynx::drumaxxKitName (st) == "LYNX test kit");
  14551. CHECK (lynx::drumaxxParam (st, 4) == 36.0f && lynx::drumaxxParam (st, 44 * 15 + 4) == 51.0f && lynx::drumaxxParam (st, 0x2c3) == 1.0f);
  14552. {
  14553. auto edited = st;
  14554. lynx::drumaxxSetParam (edited, 0x2c1, 0.25f);
  14555. CHECK (lynx::drumaxxParam (edited, 0x2c1) == 0.25f && std::isnan (lynx::drumaxxParam (edited, 720)));
  14556. auto bad = st;
  14557. bad[8] = 'X';
  14558. CHECK (! lynx::drumaxxStateUsable (bad) && std::isnan (lynx::drumaxxParam (bad, 0)));
  14559. }
  14560. // goldens: LYNX's renders, bit-identical to Drumaxx_x64.dll + engine_x64.dll driven by the round-67 harness
  14561. using N = lynx::drumaxx::RefNote;
  14562. struct Golden { bool eq; int fs, block; std::vector<N> notes; double el, er; std::tuple<int, float, float> at[6]; };
  14563. auto check = [] (const Golden& g) {
  14564. const auto y = lynx::drumaxx::renderReference (drumaxxTestState (g.eq), g.notes, (double) g.fs, 20000, g.block);
  14565. CHECK (y.size() == 2 * 20000);
  14566. if (y.size() != 2 * 20000)
  14567. return;
  14568. double el = 0.0, er = 0.0;
  14569. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  14570. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  14571. CHECK (std::abs (el - g.el) <= 1e-6 * g.el && std::abs (er - g.er) <= 1e-6 * g.er);
  14572. for (const auto& [i, l, r] : g.at)
  14573. CHECK (std::abs (y[2 * (std::size_t) i] - l) <= 1e-6f * std::max (1e-16f, std::abs (l))
  14574. && std::abs (y[2 * (std::size_t) i + 1] - r) <= 1e-6f * std::max (1e-16f, std::abs (r)));
  14575. };
  14576. // master EQ and limiter on: velocities, Final.Vol, pitch offsets, the cut group (pads 3…5)
  14577. check ({ true, 44100, 512,
  14578. { { 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 },
  14579. { 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 },
  14580. { 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 } },
  14581. 2698.372377185949, 3678.285731410973,
  14582. { { 100, 0.0f, 0.0f }, { 1000, -0.0034233136102557182f, -0.005708245560526848f }, { 2500, -0.41297006607055664f, -0.0276018138974905f },
  14583. { 4000, -0.04906991869211197f, -0.7206904292106628f }, { 7777, 0.7544509172439575f, 0.7584841847419739f },
  14584. { 15000, -0.13438990712165833f, -0.6798369884490967f } } });
  14585. // EQ off, 48 kHz in 333-sample blocks, two pads on the same sample
  14586. check ({ false, 48000, 333,
  14587. { { 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 },
  14588. { 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 } },
  14589. 2241.008810618806, 1814.5219804412964,
  14590. { { 100, 0.0f, 0.0f }, { 1000, -0.32539352774620056f, -0.7439078688621521f }, { 2500, -0.1711389720439911f, -0.39125433564186096f },
  14591. { 4000, 0.15524788200855255f, 0.3445436358451843f }, { 7777, 0.5472834706306458f, 0.08737402409315109f },
  14592. { 15000, -0.03079034946858883f, -0.06140122935175896f } } });
  14593. // 96 kHz in 64-sample blocks, an octave down
  14594. check ({ true, 96000, 64,
  14595. { { 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 },
  14596. { 10000, 51, 1.0, 1.0, -1200.0 } },
  14597. 3522.8420617665834, 5746.555795626526,
  14598. { { 100, 0.0f, 0.0f }, { 1000, -0.04133236035704613f, -0.09954667836427689f }, { 2500, 0.2800408899784088f, -0.9350972175598145f },
  14599. { 4000, 0.9537044167518616f, 0.8892703056335449f }, { 7777, 0.8376388549804688f, -0.14166420698165894f },
  14600. { 15000, -0.05926012247800827f, -0.5840796828269958f } } });
  14601. // the instrument: a pad sounds on its key and dies away; keys without a pad stay silent
  14602. {
  14603. auto inst = lynx::makeInstrument (lynx::InstrumentKind::drumaxx);
  14604. lynx::ChannelDef def;
  14605. def.instrument = lynx::InstrumentKind::drumaxx;
  14606. def.pluginState = st;
  14607. inst->configure (def, 140.0);
  14608. inst->prepare (44100.0);
  14609. std::vector<float> l (512, 0.0f), r (512, 0.0f);
  14610. inst->noteOn (90, 1.0f);
  14611. inst->render (l.data(), r.data(), 512, 1.0f, 1.0f);
  14612. CHECK (inst->idle() && std::all_of (l.begin(), l.end(), [] (float v) { return v == 0.0f; }));
  14613. inst->noteOn (36, 0.8f);
  14614. inst->render (l.data(), r.data(), 512, 1.0f, 1.0f);
  14615. double e = 0.0;
  14616. for (auto v : l)
  14617. e += (double) v * v;
  14618. CHECK (! inst->idle() && e > 1e-3);
  14619. for (int k = 0; k < 200 && ! inst->idle(); ++k)
  14620. inst->render (l.data(), r.data(), 512, 1.0f, 1.0f);
  14621. CHECK (inst->idle());
  14622. }
  14623. // import: a Drumaxx channel plays on the exact engine with its raw state
  14624. {
  14625. FlpBuilder b;
  14626. b.ascii (0xC7, "25.2.5.5319");
  14627. b.u8 (0x40); b.u16 (0);
  14628. b.u8 (0x15); b.u8 (2);
  14629. b.utf16 (0xC9, u"Drumaxx");
  14630. b.data (0xD5, st);
  14631. lynx::FlpImportOptions options;
  14632. lynx::FlpImportReport report;
  14633. options.report = &report;
  14634. const auto song = lynx::importFlp (flp::parseProject (b.bytes), options);
  14635. CHECK (song.channels.size() == 1u);
  14636. if (song.channels.size() == 1u)
  14637. CHECK (song.channels[0].instrument == lynx::InstrumentKind::drumaxx && song.channels[0].pluginState == st);
  14638. CHECK (report.converted["Drumaxx"] == 1);
  14639. }
  14640. }
  14641. // --- REA round 68: FL Slayer (exact engine) and Fruity Dance (visual only) ------------------------------------------
  14642. namespace
  14643. {
  14644. std::string slayerDll()
  14645. {
  14646. if (realFlpDir.empty())
  14647. return {};
  14648. return (std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "FL Slayer" / "Slayer_Engine.dll").string();
  14649. }
  14650. std::vector<std::uint8_t> slayerPreset (const char* name)
  14651. {
  14652. if (realFlpDir.empty())
  14653. return {};
  14654. const auto file = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "FL Slayer" / (std::string (name) + ".fst");
  14655. const auto states = pluginStatesIn (file, "FL Slayer");
  14656. return states.empty() ? std::vector<std::uint8_t> {} : states.front();
  14657. }
  14658. } // namespace
  14659. TEST (slayer_state)
  14660. {
  14661. // the saved state: int version, then the parameter integers (version 0: 28, version 1: 29 with the mod wheel)
  14662. std::vector<std::uint8_t> v0 (4 + 28 * 4, 0), v1 (4 + 29 * 4, 0);
  14663. const std::int32_t one = 1, wheel = 77, mode = 5;
  14664. std::memcpy (v1.data(), &one, 4);
  14665. std::memcpy (v1.data() + 4 + 28 * 4, &wheel, 4);
  14666. std::memcpy (v0.data() + 4 + 24 * 4, &mode, 4);
  14667. CHECK (lynx::slayerStateUsable (v0) && lynx::slayerStateUsable (v1));
  14668. CHECK (! lynx::slayerStateUsable (std::vector<std::uint8_t> (v0.begin(), v0.end() - 1)));
  14669. auto v2 = v1;
  14670. v2[0] = 2;
  14671. CHECK (! lynx::slayerStateUsable (v2));
  14672. const auto p0 = lynx::slayerStateParams (v0), p1 = lynx::slayerStateParams (v1);
  14673. CHECK (p0.size() == 29u && p0[24] == 5 && p0[28] == 0 && p1.size() == 29u && p1[28] == 77);
  14674. // the controls' maxima (the front-end sends value / max)
  14675. CHECK (lynx::slayerParamMax (0) == 6 && lynx::slayerParamMax (20) == 15 && lynx::slayerParamMax (24) == 5 && lynx::slayerParamMax (28) == 127
  14676. && lynx::slayerParamMax (3) == 65535);
  14677. // MIDI velocity from Init.Vol (0 → 0, which the engine takes as a note-off; FL's full velocity → 127)
  14678. CHECK (lynx::slayer::midiVelocity (0.0) == 0 && lynx::slayer::midiVelocity (1.0) == 99 && lynx::slayer::midiVelocity (2.0) == 126
  14679. && lynx::slayer::midiVelocity ((std::pow (11.0, 1.28) - 1.0) / 10.0) == 127);
  14680. }
  14681. TEST (slayer_engine)
  14682. {
  14683. const auto dll = slayerDll();
  14684. if (dll.empty() || ! lynx::slayerDllUsable (dll))
  14685. return; // no FL installation
  14686. // against the installation's Slayer_Engine.dll (EXEC, REA_rodada68 §7: the DLL driven like FL's front-end drives it,
  14687. // host blocks of 512 at 44.1 kHz, tempo 120): bit-identical output. C3 vel 90 1024–20000, G3 vel 120 8000–30000,
  14688. // E4 vel 40 16000–40000, C5 vel 127 from 30000.
  14689. struct Sample { int at; float l, r; };
  14690. struct Golden { const char* preset; double el, er; Sample s[4]; };
  14691. const Golden goldens[] {
  14692. { "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 } } },
  14693. { "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 } } },
  14694. { "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 } } },
  14695. { "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 } } },
  14696. { "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 } } },
  14697. { "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 } } },
  14698. { "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 } } },
  14699. { "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 } } },
  14700. { "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 } } },
  14701. { "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 } } },
  14702. { "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 } } },
  14703. { "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 } } },
  14704. };
  14705. const std::vector<lynx::slayer::RefNote> notes = { { 1024, 48, 90, 20000 }, { 8000, 55, 120, 30000 }, { 16000, 64, 40, 40000 }, { 30000, 72, 127, -1 } };
  14706. constexpr int length = 44100;
  14707. int checked = 0;
  14708. for (const auto& g : goldens)
  14709. {
  14710. const auto st = slayerPreset (g.preset);
  14711. CHECK (lynx::slayerStateUsable (st));
  14712. if (! lynx::slayerStateUsable (st))
  14713. continue;
  14714. const auto y = lynx::slayer::renderReference (lynx::slayerStateParams (st), dll, notes, 44100.0, length);
  14715. double el = 0.0, er = 0.0;
  14716. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  14717. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  14718. 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;
  14719. for (const auto& smp : g.s)
  14720. ok = ok && y[2 * (std::size_t) smp.at] == smp.l && y[2 * (std::size_t) smp.at + 1] == smp.r;
  14721. if (! ok)
  14722. 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);
  14723. CHECK (ok);
  14724. ++checked;
  14725. }
  14726. CHECK (checked == 12);
  14727. // the instrument: the engine primed with one sample, FL's velocity curve, the front-end's gains from the channel's
  14728. // MIDI pan / volume (REA_rodada79 §2: twice the channel gain through the host's integer-pan law); a note with
  14729. // velocity 0.8 is the reference note one sample later
  14730. const auto st = slayerPreset ("BASS Ebass 2");
  14731. lynx::ChannelDef c;
  14732. c.instrument = lynx::InstrumentKind::slayer;
  14733. c.pluginState = st;
  14734. c.samplePath = dll;
  14735. auto inst = lynx::makeInstrument (lynx::InstrumentKind::slayer);
  14736. inst->configure (c, 120.0);
  14737. inst->prepare (44100.0);
  14738. std::vector<float> l (16384, 0.0f), r (16384, 0.0f);
  14739. for (int b = 0; b < 32; ++b)
  14740. {
  14741. if (b == 0)
  14742. inst->noteOn (60, 0.8f);
  14743. if (b == 20)
  14744. inst->noteOff (60);
  14745. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 0.5f, 0.25f);
  14746. }
  14747. const int vel = lynx::slayer::midiVelocity ((std::pow (11.0, 1.28 * 0.8) - 1.0) / 10.0);
  14748. const auto ref = lynx::slayer::renderReference (lynx::slayerStateParams (st), dll, { { 1, 60, vel, 1 + 20 * 512 } }, 44100.0, 16385);
  14749. // LYNX's channel gains 0.5 / 0.25 = gain 0.395…, pan −0.4097 → MIDI pan −26
  14750. const auto chanVol = (float) std::sqrt ((0.25 + 0.0625) * 0.5);
  14751. float hl = 0.0f, hr = 0.0f;
  14752. lynx::slayer::hostGains (lynx::slayer::midiPan (std::atan2 (0.25, 0.5) * (4.0 / 3.141592653589793) - 1.0), chanVol, hl, hr);
  14753. CHECK (lynx::slayer::midiPan (std::atan2 (0.25, 0.5) * (4.0 / 3.141592653589793) - 1.0) == -26);
  14754. double diff = 0.0, energy = 0.0;
  14755. for (int i = 0; i < 16384; ++i)
  14756. {
  14757. diff = std::max ({ diff, (double) std::abs (l[(std::size_t) i] - ref[2 * (std::size_t) (i + 1)] * hl),
  14758. (double) std::abs (r[(std::size_t) i] - ref[2 * (std::size_t) (i + 1) + 1] * hr) });
  14759. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  14760. }
  14761. CHECK (diff == 0.0 && energy > 1.0 && vel == 102);
  14762. // every preset of the installation: usable and sounding
  14763. int presets = 0, sounding = 0;
  14764. const auto folder = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "FL Slayer";
  14765. if (std::filesystem::exists (folder))
  14766. for (const auto& entry : std::filesystem::directory_iterator (folder))
  14767. for (const auto& s : pluginStatesIn (entry.path(), "FL Slayer"))
  14768. {
  14769. ++presets;
  14770. const auto y = lynx::slayer::renderReference (lynx::slayerStateParams (s), dll, { { 1024, 52, 100, 20000 } }, 44100.0, 22050);
  14771. double e = 0.0;
  14772. bool finite = true;
  14773. for (auto v : y)
  14774. finite = finite && std::isfinite (v), e += (double) v * v;
  14775. CHECK (finite);
  14776. sounding += e > 1.0 ? 1 : 0;
  14777. }
  14778. std::printf (" FL Slayer presets: %d, %d sounding\n", presets, sounding);
  14779. CHECK (presets == 64 && sounding == 64);
  14780. }
  14781. // FL Slayer's host levels and live parameters (REA_rodada79): channel pan / volume / pitch arrive as MIDI events
  14782. // (ProcessEvent 2/3/4), automation and settings edits as setParameter calls on the running engine
  14783. TEST (slayer_host_levels_and_automation)
  14784. {
  14785. using namespace lynx::slayer;
  14786. CHECK (pitchBend (0) == 8192 && pitchBend (100) == 8874 && pitchBend (-100) == 7510 && pitchBend (1200) == 0x3fff
  14787. && pitchBend (-1200) == 0 && pitchBend (-1199) == 7 && pitchBend (5000) == 0x3fff);
  14788. CHECK (midiPan (0.0) == 0 && midiPan (1.0) == 64 && midiPan (-1.0) == -64 && midiPan (-0.5) == -32
  14789. && midiPan (99.0 / 6400.0) == 0 && midiPan (-199.0 / 6400.0) == -1 && midiPan (2.0) == 64);
  14790. float l0 = 0.0f, r0 = 0.0f, l1 = 0.0f, r1 = 0.0f;
  14791. hostGains (0, 0.5f, l0, r0); // the front-end's own start values (volume 1 after the ×2, centre)
  14792. hostGains (64, 1.0f, l1, r1);
  14793. CHECK (l0 == 0.70710677f && r0 == 0.70710677f && std::abs (l1) < 1e-15f && r1 == 2.0f);
  14794. const auto dll = slayerDll();
  14795. if (dll.empty() || ! lynx::slayerDllUsable (dll))
  14796. return;
  14797. const auto st = slayerPreset ("DIST Dire Straits");
  14798. const auto params = lynx::slayerStateParams (st);
  14799. lynx::ChannelDef c;
  14800. c.instrument = lynx::InstrumentKind::slayer;
  14801. c.pluginState = st;
  14802. c.samplePath = dll;
  14803. c.sampler.pitch = 1.5f; // channel pitch +150 cents → bend 9216; no root shift
  14804. c.modelParams = { 0.0f };
  14805. auto inst = lynx::makeInstrument (lynx::InstrumentKind::slayer);
  14806. inst->configure (c, 120.0);
  14807. inst->prepare (44100.0);
  14808. const int blocks = 40, n = 512;
  14809. std::vector<float> l ((std::size_t) (blocks * n), 0.0f), r ((std::size_t) (blocks * n), 0.0f);
  14810. auto edited = st;
  14811. const std::int32_t newDrive = 60000; // parameter 13 (amp drive), edited in the settings at block 25
  14812. std::memcpy (edited.data() + 4 + 4 * 13, &newDrive, 4);
  14813. for (int b = 0; b < blocks; ++b)
  14814. {
  14815. if (b == 0)
  14816. inst->noteOn (52, 0.8f);
  14817. if (b == 10)
  14818. inst->setParameter (20, 9.0f / 15.0f); // automation: effect 9 (chorus)
  14819. if (b == 18)
  14820. inst->setParameter (28, 0.5f); // automation: the mod wheel → CC 1 64
  14821. if (b == 25)
  14822. {
  14823. c.pluginState = edited;
  14824. inst->configure (c, 120.0);
  14825. }
  14826. inst->render (l.data() + n * b, r.data() + n * b, n, 1.0f, 1.0f);
  14827. }
  14828. lynx::slayer::RefOptions opt;
  14829. opt.midi.push_back ({ 1, 0xe0, 9216 & 0x7f, 9216 >> 7 });
  14830. opt.params = { { 1 + 10 * n, 20, 9 }, { 1 + 18 * n, 28, 64 }, { 1 + 25 * n, 13, newDrive } };
  14831. const int vel = lynx::slayer::midiVelocity ((std::pow (11.0, 1.28 * 0.8) - 1.0) / 10.0);
  14832. const auto ref = lynx::slayer::renderReference (params, dll, { { 1, 52, vel, -1 } }, 44100.0, blocks * n + 1, opt);
  14833. float hl = 0.0f, hr = 0.0f;
  14834. hostGains (0, 1.0f, hl, hr);
  14835. double diff = 0.0, energy = 0.0;
  14836. for (int i = 0; i < blocks * n; ++i)
  14837. {
  14838. diff = std::max ({ diff, (double) std::abs (l[(std::size_t) i] - ref[2 * (std::size_t) (i + 1)] * hl),
  14839. (double) std::abs (r[(std::size_t) i] - ref[2 * (std::size_t) (i + 1) + 1] * hr) });
  14840. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  14841. }
  14842. CHECK (diff == 0.0 && energy > 1.0);
  14843. // the changes really change the sound: the same render without them differs
  14844. const auto plain = lynx::slayer::renderReference (params, dll, { { 1, 52, vel, -1 } }, 44100.0, blocks * n + 1);
  14845. CHECK (plain != ref);
  14846. }
  14847. TEST (slayer_and_fruity_dance_import)
  14848. {
  14849. // FL Slayer plays on the exact engine when the installation's engine DLL is there (otherwise it stays unsupported);
  14850. // Fruity Dance (a visual plugin) becomes a silent channel that keeps its state
  14851. std::vector<std::uint8_t> slayer (4 + 28 * 4, 0), dance (40, 1);
  14852. FlpBuilder b;
  14853. b.ascii (0xC7, "25.2.5.5319");
  14854. int index = 0;
  14855. for (const auto& [name, state] : { std::pair<std::u16string, std::vector<std::uint8_t>> { u"FL Slayer", slayer }, { u"Fruity Dance", dance } })
  14856. {
  14857. b.u8 (0x40); b.u16 ((std::uint16_t) index++);
  14858. b.u8 (0x15); b.u8 (2);
  14859. b.utf16 (0xC9, name);
  14860. b.data (0xD5, state);
  14861. }
  14862. const auto project = flp::parseProject (b.bytes);
  14863. const auto plain = lynx::importFlp (project);
  14864. CHECK (plain.channels.size() == 2u);
  14865. if (plain.channels.size() == 2u)
  14866. {
  14867. CHECK (plain.channels[0].instrument != lynx::InstrumentKind::slayer);
  14868. CHECK (plain.channels[1].instrument == lynx::InstrumentKind::fruitydance && plain.channels[1].pluginState == dance);
  14869. }
  14870. auto inst = lynx::makeInstrument (lynx::InstrumentKind::fruitydance);
  14871. inst->prepare (44100.0);
  14872. std::vector<float> l (1024, 0.0f), r (1024, 0.0f);
  14873. inst->noteOn (60, 1.0f);
  14874. inst->render (l.data(), r.data(), 1024, 1.0f, 1.0f);
  14875. CHECK (inst->idle() && std::all_of (l.begin(), l.end(), [] (float v) { return v == 0.0f; }));
  14876. const auto dll = slayerDll();
  14877. if (dll.empty() || ! lynx::slayerDllUsable (dll))
  14878. return;
  14879. lynx::FlpImportOptions withFiles;
  14880. withFiles.roots = { std::filesystem::path (realFlpDir).parent_path().string(), realFlpDir, "" };
  14881. withFiles.fileExists = [] (const std::string& path) {
  14882. auto local = path;
  14883. std::replace (local.begin(), local.end(), '\\', '/');
  14884. return std::filesystem::exists (local);
  14885. };
  14886. const auto song = lynx::importFlp (project, withFiles);
  14887. CHECK (song.channels.size() == 2u && song.channels[0].instrument == lynx::InstrumentKind::slayer && song.channels[0].pluginState == slayer
  14888. && ! song.channels[0].samplePath.empty());
  14889. }
  14890. #include "WaveAnalysisTests.inc"
  14891. #include "UiPolish2Tests.inc"
  14892. #include "UiToolbarTests.inc"
  14893. #include "LuxeVerbTests.inc"
  14894. #include "Parity172bTests.inc"
  14895. #include "ConvolverTests.inc"
  14896. // --- Newtone / Newtime (REA_rodada73) -----------------------------------------------------------------------------
  14897. namespace newtonetest
  14898. {
  14899. 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))); }
  14900. 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))); }
  14901. void record (std::vector<std::uint8_t>& b, int id, const std::vector<std::uint8_t>& d)
  14902. {
  14903. put32 (b, id);
  14904. put64 (b, (std::int64_t) d.size());
  14905. b.insert (b.end(), d.begin(), d.end());
  14906. }
  14907. // a state with our own layout reading (REA_rodada73 §2): options, the wrapper record around the engine XML, the source path
  14908. std::vector<std::uint8_t> state (int version, std::uint32_t flags, const std::string& xml, const std::string& source)
  14909. {
  14910. std::vector<std::uint8_t> b, r1, r2;
  14911. put32 (b, version);
  14912. put32 (r1, 727);
  14913. put32 (r1, 582);
  14914. put32 (r1, (std::int32_t) flags);
  14915. r1.resize (0x28, 0);
  14916. record (b, 1, r1);
  14917. std::vector<std::uint8_t> chunk { 'V', 'C', '2', '!' };
  14918. put32 (chunk, (std::int32_t) xml.size() + 1);
  14919. chunk.insert (chunk.end(), xml.begin(), xml.end());
  14920. chunk.push_back (0);
  14921. put32 (r2, -9);
  14922. r2.push_back (0x0c);
  14923. put32 (r2, -2);
  14924. put64 (r2, (std::int64_t) chunk.size());
  14925. put32 (r2, 0);
  14926. r2.insert (r2.end(), chunk.begin(), chunk.end());
  14927. record (b, 2, r2);
  14928. record (b, 3, std::vector<std::uint8_t> (source.begin(), source.end()));
  14929. record (b, 4, std::vector<std::uint8_t> (12, 0));
  14930. record (b, 5, std::vector<std::uint8_t> (16, 0));
  14931. record (b, 6, std::vector<std::uint8_t> (32, 0));
  14932. return b;
  14933. }
  14934. const char* xmlWithTrack (bool edited)
  14935. {
  14936. return edited ? "<NewToneProject NumOfTracks=\"1\"><Tracks><Trk_0 TrackNum=\"0\" Type=\"class CzplJPositionablePSOLASource\" "
  14937. "FilePath=\"C:\\tmp\\a&amp;b.wav\"><AudioObject SelfBPM=\"120.0\" OriginalFileLength=\"1000\" GlobalTune=\"0.0\">"
  14938. "<Pitch_0 fOrigPitch=\"60.0\" fCurPitch=\"60.0\" iOrigLength=\"500\" iCurLength=\"500\"/>"
  14939. "<Pitch_1 fOrigPitch=\"62.0\" fCurPitch=\"64.0\" iOrigLength=\"500\" iCurLength=\"500\"/></AudioObject></Trk_0></Tracks></NewToneProject>"
  14940. : "<NewToneProject NumOfTracks=\"1\"><Tracks><Trk_0 TrackNum=\"0\" Type=\"class CzplJTimelineWarpSource\" "
  14941. "FilePath=\"x.wav\"><AudioObject SelfBPM=\"80.0\" OriginalFileLength=\"1000\">"
  14942. "<Pitch_0 fOrigPitch=\"60.0\" fCurPitch=\"60.0\" fCurVolume=\"1.0\"/></AudioObject></Trk_0></Tracks></NewToneProject>";
  14943. }
  14944. } // namespace newtonetest
  14945. TEST (newtone_state)
  14946. {
  14947. using namespace newtonetest;
  14948. const auto a = lynx::newtonefx::parse (state (0, 5, xmlWithTrack (true), "%FLStudioUserData%\\vox.wav"));
  14949. CHECK (a.has_value());
  14950. CHECK (a->version == 0 && a->editorWidth == 727 && a->editorHeight == 582 && a->slave());
  14951. CHECK (a->sourcePath == "%FLStudioUserData%\\vox.wav");
  14952. CHECK (a->tracks.size() == 1 && a->tracks[0].notes == 2 && a->tracks[0].frames == 1000 && ! a->tracks[0].timeWarp);
  14953. CHECK (a->tracks[0].filePath == "C:\\tmp\\a&b.wav" && a->tracks[0].selfBpm == 120.0);
  14954. CHECK (a->tracks[0].edited && a->edited()); // note 1 moved from 62 to 64
  14955. const auto b = lynx::newtonefx::parse (state (1, 0x41, xmlWithTrack (false), ""));
  14956. CHECK (b.has_value() && ! b->slave() && b->sourcePath.empty());
  14957. CHECK (b->tracks.size() == 1 && b->tracks[0].timeWarp && ! b->edited());
  14958. // an empty project, as the installation's presets
  14959. const auto c = lynx::newtonefx::parse (state (0, 1, "<NewToneProject NumOfTracks=\"0\"><Tracks/></NewToneProject>", ""));
  14960. CHECK (c.has_value() && c->tracks.empty());
  14961. // broken: other version, truncated record, no options record
  14962. CHECK (! lynx::newtonefx::parse (state (2, 1, "", "")));
  14963. auto cut = state (0, 1, xmlWithTrack (true), "");
  14964. cut.resize (cut.size() - 3);
  14965. CHECK (! lynx::newtonefx::parse (cut));
  14966. CHECK (! lynx::newtonefx::parse ({ 0, 0, 0, 0 }));
  14967. // the FL import keeps the state and takes the slave switch as the editable parameter
  14968. const auto raw = state (0, 5, xmlWithTrack (false), "");
  14969. const auto slot = lynx::effectFromFl ("Newtone", raw, 120.0);
  14970. CHECK (slot && slot->kind == lynx::EffectKind::newtone && slot->state == raw && slot->params[0] == 1.0f);
  14971. const auto slot2 = lynx::effectFromFl ("Newtime", state (1, 0x41, xmlWithTrack (false), ""), 120.0);
  14972. CHECK (slot2 && slot2->kind == lynx::EffectKind::newtime && slot2->params[0] == 0.0f);
  14973. CHECK (lynx::formatEffectParam (lynx::EffectKind::newtone, 0, 1.0f, 120.0) == "on");
  14974. // the installation's own states (plugin database and the playlist's pitch/warp correction tracks) [DATA]
  14975. if (! realFlpDir.empty())
  14976. {
  14977. int found = 0, read = 0;
  14978. for (const char* rel : { "System/PitchCorrectionTrack.fst", "System/WarpCorrectionTrack.fst",
  14979. "Patches/Plugin database/Effects/Misc/Newtone.fst", "Patches/Plugin database/Effects/Misc/Newtime.fst" })
  14980. {
  14981. const auto path = std::filesystem::path (realFlpDir) / rel;
  14982. if (! std::filesystem::exists (path))
  14983. continue;
  14984. ++found;
  14985. std::ifstream f (path, std::ios::binary);
  14986. std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (f)), std::istreambuf_iterator<char>());
  14987. const auto fx = lynx::effectFromFlPreset (bytes, 120.0);
  14988. read += fx && (fx->kind == lynx::EffectKind::newtone || fx->kind == lynx::EffectKind::newtime) && ! fx->state.empty()
  14989. && fx->params[0] == 0.0f && lynx::newtonefx::parse (fx->state)->tracks.empty();
  14990. }
  14991. std::printf (" %d Newtone/Newtime states, %d read\n", found, read);
  14992. CHECK (read == found);
  14993. }
  14994. }
  14995. TEST (newtone_engine)
  14996. {
  14997. // REA_rodada73 §3-4 [EXEC]: the input passes unchanged unless slave mode is on and the song plays; then the source
  14998. // sample is added from the song time on (at the source's own rate)
  14999. auto sample = std::make_shared<lynx::SampleData>();
  15000. sample->sampleRate = 44100.0;
  15001. for (int i = 0; i < 3000; ++i)
  15002. sample->left.push_back (0.001f * (float) (i % 97) - 0.04f);
  15003. const double sr = 44100.0, bpm = 120.0;
  15004. const auto tps = lynx::ticksPerSample (bpm, sr);
  15005. auto fx = lynx::newtonefx::make (sample);
  15006. fx->prepare (sr);
  15007. lynx::EffectParams on {}, off {};
  15008. on[0] = 1.0f;
  15009. std::vector<float> l (256), r (256), in (256);
  15010. auto block = [&] (double tick, bool playing, const lynx::EffectParams& p) {
  15011. for (int i = 0; i < 256; ++i)
  15012. l[(std::size_t) i] = r[(std::size_t) i] = in[(std::size_t) i] = 0.25f * std::sin (0.01f * (float) (i + tick));
  15013. fx->setSongPosition (tick);
  15014. fx->setPlaying (playing);
  15015. fx->process (l.data(), r.data(), 256, p, bpm);
  15016. };
  15017. auto same = [&] { return std::equal (l.begin(), l.end(), in.begin()) && std::equal (r.begin(), r.end(), in.begin()); };
  15018. block (0.0, true, off);
  15019. CHECK (same()); // slave off
  15020. block (0.0, false, on);
  15021. CHECK (same()); // not playing
  15022. // playing from the start: in + source, sample by sample, over consecutive blocks
  15023. bool exact = true;
  15024. for (int b = 0; b < 4; ++b)
  15025. {
  15026. block (b * 256 * tps, true, on);
  15027. for (int i = 0; i < 256; ++i)
  15028. exact = exact && l[(std::size_t) i] == in[(std::size_t) i] + sample->left[(std::size_t) (b * 256 + i)]
  15029. && r[(std::size_t) i] == l[(std::size_t) i];
  15030. }
  15031. CHECK (exact);
  15032. // a jump re-seeks to the song time; past the end of the sample only the input remains
  15033. block (2000 * tps, true, on);
  15034. CHECK (l[0] == in[0] + sample->left[2000] && l[255] == in[255] + sample->left[2255]);
  15035. block (2900 * tps, true, on);
  15036. CHECK (l[99] == in[99] + sample->left[2999] && l[100] == in[100]);
  15037. // stopping and playing again: from the new song time
  15038. block (0.0, false, on);
  15039. CHECK (same());
  15040. block (10 * tps, true, on);
  15041. CHECK (l[0] == in[0] + sample->left[10]);
  15042. // without its sample: a pass-through
  15043. auto none = lynx::makeEffect (lynx::EffectKind::newtime);
  15044. none->prepare (sr);
  15045. for (int i = 0; i < 256; ++i)
  15046. l[(std::size_t) i] = r[(std::size_t) i] = in[(std::size_t) i] = 0.1f * (float) i / 256.0f;
  15047. none->setPlaying (true);
  15048. none->process (l.data(), r.data(), 256, on, bpm);
  15049. CHECK (same());
  15050. }
  15051. TEST (newtone_rack_follows_transport)
  15052. {
  15053. // the mixer gives the effect the transport, and keeps it running on a silent insert (no smart disable)
  15054. auto song = lynx::Song::makeDefault();
  15055. for (auto& p : song.patterns)
  15056. p.notes.clear();
  15057. song.bpm = 120.0;
  15058. auto slot = lynx::makeEffectSlot (lynx::EffectKind::newtone);
  15059. slot.params[0] = 1.0f;
  15060. slot.samplePath = "newtone-test.wav";
  15061. song.mixer[1].effects.push_back (slot);
  15062. auto sample = std::make_shared<lynx::SampleData>();
  15063. sample->sampleRate = 48000.0;
  15064. sample->left.assign (48000, 0.5f);
  15065. int loads = 0;
  15066. lynx::RackState::SampleProvider provider = [&] (const std::string& path) -> std::shared_ptr<const lynx::SampleData> {
  15067. ++loads;
  15068. return path == "newtone-test.wav" ? sample : nullptr;
  15069. };
  15070. const double sr = 48000.0;
  15071. auto state = lynx::RackState::build (song, nullptr, sr, provider);
  15072. CHECK (loads == 1);
  15073. lynx::RackRenderer rr;
  15074. rr.prepare (sr);
  15075. std::vector<float> l (512), r (512);
  15076. auto peakOf = [&] (bool playing, double tick) {
  15077. float peak = 0.0f;
  15078. for (int b = 0; b < 8; ++b)
  15079. {
  15080. rr.process (state.get(), playing, tick + b * 512 * lynx::ticksPerSample (song.bpm, sr), 512, l.data(), r.data());
  15081. for (auto v : l)
  15082. peak = std::max (peak, std::abs (v));
  15083. }
  15084. return peak;
  15085. };
  15086. CHECK (peakOf (false, 0.0) == 0.0f);
  15087. CHECK (peakOf (true, 0.0) > 0.1f);
  15088. }
  15089. #include "HardcoreTests.inc"
  15090. #include "DistructorTests.inc"
  15091. // --- missing files (core/MissingFiles.h) ------------------------------------------------------------------------
  15092. TEST (missing_files_names_and_scores)
  15093. {
  15094. CHECK (lynx::pathFileName ("C:\\Samples\\Drums\\Kick 01.wav") == "Kick 01.wav");
  15095. CHECK (lynx::pathFileName ("/home/u/loops/beat.flac") == "beat.flac");
  15096. CHECK (lynx::pathFileName ("D:/mixed\\sep/x.wav/") == "x.wav");
  15097. CHECK (lynx::pathFileName ("plain.wav") == "plain.wav");
  15098. CHECK (lynx::relocationScore ("C:\\Old\\Drums\\Kicks\\kick.wav", "/new/Drums/Kicks/KICK.WAV") == 3);
  15099. CHECK (lynx::relocationScore ("C:\\Old\\Drums\\Kicks\\kick.wav", "/new/other/kick.wav") == 1);
  15100. CHECK (lynx::relocationScore ("C:\\Old\\kick.wav", "/new/kick2.wav") == 0);
  15101. }
  15102. TEST (missing_files_find_in_song)
  15103. {
  15104. lynx::Song song;
  15105. lynx::ChannelDef a, b, c, d, e;
  15106. a.name = "Kick", a.instrument = lynx::InstrumentKind::sampler, a.samplePath = "C:\\gone\\kick.wav";
  15107. b.name = "Vox", b.instrument = lynx::InstrumentKind::audioClip, b.samplePath = "C:\\gone\\vox.wav";
  15108. c.name = "Here", c.instrument = lynx::InstrumentKind::sampler, c.samplePath = "C:\\here\\snare.wav";
  15109. d.name = "GMS", d.instrument = lynx::InstrumentKind::gms, d.samplePath = "C:\\FL\\gms waves"; // installation data: not listed
  15110. e.name = "Kick 2", e.instrument = lynx::InstrumentKind::sampler, e.samplePath = "c:/GONE/KICK.wav"; // same file, other spelling
  15111. lynx::SampleZone z;
  15112. z.path = "C:\\gone\\piano_c4.wav";
  15113. a.zones.push_back (z);
  15114. song.channels = { a, b, c, d, e };
  15115. song.mixer.resize (3);
  15116. lynx::EffectSlot fx;
  15117. fx.samplePath = "C:\\gone\\tune.wav";
  15118. lynx::EffectSlot parallel;
  15119. parallel.branches.push_back ({ { fx }, 1.0f });
  15120. song.mixer[2].effects.push_back (parallel);
  15121. song.mixer[2].name = "Lead";
  15122. const auto exists = [] (const std::string& p) { return p.find ("here") != std::string::npos || p.find ("FL") != std::string::npos; };
  15123. auto missing = lynx::findMissingSamples (song, exists);
  15124. CHECK (missing.size() == 4);
  15125. CHECK (missing.size() == 4 && missing[0].path == "C:\\gone\\kick.wav" && missing[0].usedBy.size() == 2 && missing[0].usedBy[1] == "Kick 2");
  15126. CHECK (missing.size() == 4 && missing[1].path == "C:\\gone\\piano_c4.wav" && missing[1].usedBy[0] == "Kick");
  15127. CHECK (missing.size() == 4 && missing[2].usedBy[0] == "Audio clip: Vox");
  15128. CHECK (missing.size() == 4 && missing[3].usedBy[0] == "Lead: effect");
  15129. // candidates from a search: the deeper path match wins; one stays missing
  15130. const std::vector<std::string> hits { "/lib/other/kick.wav", "/lib/gone/kick.wav", "/lib/x/vox.wav", "/lib/y/tune.wav" };
  15131. CHECK (lynx::relocateByName (missing, hits) == 3);
  15132. CHECK (missing[0].found == "/lib/gone/kick.wav");
  15133. CHECK (missing[1].found.empty());
  15134. missing[3].skipped = true; // skipped: not applied
  15135. const auto map = lynx::relocationMap (missing);
  15136. CHECK (map.size() == 2);
  15137. CHECK (lynx::applyRelocations (song, map) == 3); // both spellings of kick.wav + vox
  15138. CHECK (song.channels[0].samplePath == "/lib/gone/kick.wav" && song.channels[4].samplePath == "/lib/gone/kick.wav");
  15139. CHECK (song.channels[1].samplePath == "/lib/x/vox.wav");
  15140. CHECK (song.channels[2].samplePath == "C:\\here\\snare.wav");
  15141. CHECK (song.mixer[2].effects[0].branches[0].chain[0].samplePath == "C:\\gone\\tune.wav");
  15142. lynx::Song copy = song;
  15143. CHECK (lynx::applyRelocations (copy, { { "C:\\gone\\tune.wav", "/t.wav" } }) == 1 && copy.mixer[2].effects[0].branches[0].chain[0].samplePath == "/t.wav");
  15144. }
  15145. TEST (missing_files_inside_patcher)
  15146. {
  15147. // a Patcher's instruments and effects (any depth) are the song's samples too: listed, packed and relinked
  15148. lynx::Song song;
  15149. lynx::ChannelDef inner;
  15150. inner.name = "Pad", inner.instrument = lynx::InstrumentKind::sampler, inner.samplePath = "C:\\gone\\pad.wav";
  15151. lynx::ChannelDef nested;
  15152. nested.instrument = lynx::InstrumentKind::patcher;
  15153. nested.patcher.nodes.resize (1);
  15154. nested.patcher.nodes[0].generator = true;
  15155. nested.patcher.nodes[0].channel = inner;
  15156. lynx::ChannelDef patch;
  15157. patch.name = "Rack", patch.instrument = lynx::InstrumentKind::patcher;
  15158. patch.patcher.nodes.resize (2);
  15159. patch.patcher.nodes[0].generator = true;
  15160. patch.patcher.nodes[0].channel = nested;
  15161. patch.patcher.nodes[1].effect.samplePath = "C:\\gone\\tune.wav";
  15162. patch.patcher.nodes[1].busHeads.push_back ({});
  15163. patch.patcher.nodes[1].busHeads[0].head = lynx::EffectSlot();
  15164. patch.patcher.nodes[1].busHeads[0].head->samplePath = "C:\\gone\\tune.wav"; // same file: one entry
  15165. song.channels = { patch };
  15166. song.mixer.resize (2);
  15167. lynx::EffectSlot fxPatcher;
  15168. fxPatcher.patcher.nodes.resize (1);
  15169. fxPatcher.patcher.nodes[0].generator = true;
  15170. fxPatcher.patcher.nodes[0].channel = inner;
  15171. fxPatcher.patcher.nodes[0].channel.samplePath = "C:\\gone\\hit.wav";
  15172. song.mixer[1].effects.push_back (fxPatcher);
  15173. const auto missing = lynx::findMissingSamples (song, [] (const std::string&) { return false; });
  15174. CHECK (missing.size() == 3);
  15175. CHECK (missing.size() == 3 && missing[0].path == "C:\\gone\\pad.wav" && missing[0].usedBy[0] == "Rack (Patcher)");
  15176. CHECK (missing.size() == 3 && missing[1].path == "C:\\gone\\tune.wav" && missing[1].usedBy[0] == "Rack (Patcher): effect");
  15177. CHECK (missing.size() == 3 && missing[2].path == "C:\\gone\\hit.wav" && missing[2].usedBy[0] == "Insert 1 (Patcher)");
  15178. CHECK (lynx::applyRelocations (song, { { "C:\\gone\\pad.wav", "/p.wav" }, { "C:\\gone\\tune.wav", "/t.wav" }, { "C:\\gone\\hit.wav", "/h.wav" } }) == 4);
  15179. CHECK (song.channels[0].patcher.nodes[1].busHeads[0].head->samplePath == "/t.wav");
  15180. CHECK (song.channels[0].patcher.nodes[0].channel.patcher.nodes[0].channel.samplePath == "/p.wav");
  15181. CHECK (song.channels[0].patcher.nodes[1].effect.samplePath == "/t.wav");
  15182. CHECK (song.mixer[1].effects[0].patcher.nodes[0].channel.samplePath == "/h.wav");
  15183. }
  15184. TEST (missing_files_folder_search)
  15185. {
  15186. namespace fs = std::filesystem;
  15187. const auto root = fs::temp_directory_path() / ("lynx-missing-test-" + processTempTag());
  15188. fs::remove_all (root);
  15189. fs::create_directories (root / "a" / "deep" / "Kicks");
  15190. fs::create_directories (root / "b");
  15191. for (const auto& f : { root / "a" / "deep" / "Kicks" / "Kick.WAV", root / "b" / "kick.wav", root / "b" / "hat.wav", root / "b" / "unrelated.wav" })
  15192. std::ofstream (f) << "x";
  15193. std::vector<lynx::MissingFile> missing { { "C:\\Old\\Kicks\\kick.wav", { "Kick" }, {}, false }, { "C:\\Old\\hat.wav", { "Hat" }, {}, false },
  15194. { "C:\\Old\\ride.wav", { "Ride" }, {}, false } };
  15195. int listed = 0;
  15196. auto list = [&] (const std::string& folder, const std::function<bool (const std::string&, bool)>& onEntry) {
  15197. for (const auto& entry : fs::directory_iterator (folder))
  15198. {
  15199. ++listed;
  15200. if (! onEntry (entry.path().string(), entry.is_directory()))
  15201. return;
  15202. }
  15203. };
  15204. const auto hits = lynx::searchFolderForMissing (missing, root.string(), list);
  15205. CHECK (hits.size() == 3);
  15206. CHECK (lynx::relocateByName (missing, hits) == 2);
  15207. CHECK (missing[0].found.find ("Kicks") != std::string::npos); // the copy in a "Kicks" folder matches better
  15208. CHECK (missing[1].found.find ("hat.wav") != std::string::npos);
  15209. CHECK (missing[2].found.empty());
  15210. // a bounded walk stops early; a cancelled one finds nothing
  15211. CHECK (lynx::searchFolderForMissing (missing, root.string(), list, 2).size() <= 1);
  15212. CHECK (lynx::searchFolderForMissing (missing, root.string(), list, 0, [] { return true; }).empty());
  15213. // nothing left to find: no walk at all
  15214. missing[2].skipped = true;
  15215. listed = 0;
  15216. CHECK (lynx::searchFolderForMissing (missing, root.string(), list).empty() && listed == 0);
  15217. fs::remove_all (root);
  15218. }
  15219. TEST (missing_files_folder_list)
  15220. {
  15221. const auto folders = lynx::parseFolderList ("C:\\A\r\n C:\\B \n\nc:\\a\nD:/x\n");
  15222. CHECK (folders.size() == 3 && folders[0] == "C:\\A" && folders[1] == "C:\\B" && folders[2] == "D:/x");
  15223. auto list = folders;
  15224. lynx::rememberFolder (list, "D:\\x");
  15225. CHECK (list.size() == 3 && list[0] == "D:\\x" && list[1] == "C:\\A");
  15226. lynx::rememberFolder (list, "E:\\new", 2);
  15227. CHECK (list.size() == 2 && list[0] == "E:\\new" && list[1] == "D:\\x");
  15228. CHECK (lynx::parseFolderList (lynx::folderListText (list)) == list);
  15229. }
  15230. TEST (missing_files_flp_report_and_relocation)
  15231. {
  15232. // an .flp sampler whose sample is gone: listed with its channel; a relocation map makes the import use the copy
  15233. flp::Project project;
  15234. flp::Channel ch;
  15235. ch.index = 0;
  15236. ch.type = flp::ChannelType::sampler;
  15237. ch.name = "Snare";
  15238. ch.samplePath = "C:\\Producer\\Packs\\snare 7.wav";
  15239. project.channels.push_back (ch);
  15240. lynx::FlpImportOptions options;
  15241. options.fileExists = [] (const std::string& p) { return p == "/found/snare 7.wav"; };
  15242. lynx::FlpImportReport report;
  15243. options.report = &report;
  15244. auto song = lynx::importFlp (project, options);
  15245. CHECK (report.missingFiles.size() == 1);
  15246. CHECK (report.missingFiles.size() == 1 && report.missingFiles[0].path == "C:\\Producer\\Packs\\snare 7.wav" && report.missingFiles[0].usedBy[0] == "Snare");
  15247. CHECK (song.channels.size() == 1 && song.channels[0].samplePath.empty());
  15248. lynx::FlpImportReport again;
  15249. options.report = &again;
  15250. options.relocations = { { "C:\\Producer\\Packs\\snare 7.wav", "/found/snare 7.wav" } };
  15251. song = lynx::importFlp (project, options);
  15252. CHECK (again.missingFiles.empty());
  15253. CHECK (song.channels.size() == 1 && song.channels[0].samplePath == "/found/snare 7.wav" && song.channels[0].instrument == lynx::InstrumentKind::sampler);
  15254. }
  15255. TEST (project_info_history_and_flp_import)
  15256. {
  15257. lynx::Song a;
  15258. auto b = a;
  15259. b.info.title = "Night Drive";
  15260. CHECK (lynx::describeChangeText (a, b) == "Project info");
  15261. auto c = a;
  15262. c.beatsPerBar = 3;
  15263. CHECK (lynx::describeChangeText (a, c) == "Time signature");
  15264. flp::Project project;
  15265. project.title = "Song", project.artist = "Me", project.genre = "House", project.url = "https://x", project.comments = "line 1\rline 2";
  15266. const auto song = lynx::importFlp (project);
  15267. CHECK (song.info.title == "Song" && song.info.author == "Me" && song.info.genre == "House" && song.info.url == "https://x");
  15268. CHECK (song.info.comments == "line 1\nline 2");
  15269. }
  15270. #include "UiFixTests.inc"
  15271. // ---------------------------------------------------------------------------
  15272. // Piano Roll tools of round 83 (expected values worked out from FL's formulas, docs/analise/REA_rodada83.md)
  15273. namespace
  15274. {
  15275. lynx::Note prNote (std::int64_t start, std::int64_t length, int key = 60, float velocity = 100.0f / 128.0f)
  15276. {
  15277. lynx::Note n;
  15278. n.start = start;
  15279. n.length = length;
  15280. n.key = key;
  15281. n.velocity = velocity;
  15282. return n;
  15283. }
  15284. std::vector<int> allOf (const lynx::Pattern& p)
  15285. {
  15286. std::vector<int> v;
  15287. for (int i = 0; i < (int) p.notes.size(); ++i)
  15288. v.push_back (i);
  15289. return v;
  15290. }
  15291. } // namespace
  15292. TEST (prtools_score_file)
  15293. {
  15294. using namespace lynx::prtools;
  15295. FlpBuilder b (96);
  15296. b.ascii (0xC7, "20.9.0");
  15297. b.u8 (0x41); b.u16 (0);
  15298. auto notes = noteRecord (0, 0, 24, 60, 100);
  15299. auto n2 = noteRecord (48, 0, 24, 67, 64);
  15300. n2[19] = 4; // colour 4
  15301. notes.insert (notes.end(), n2.begin(), n2.end());
  15302. b.data (0xE0, notes);
  15303. const auto score = parseScore (b.bytes);
  15304. CHECK (score && score->notes.size() == 2);
  15305. if (score && score->notes.size() == 2)
  15306. {
  15307. CHECK (score->notes[0].start == 0 && score->notes[0].length == 240 && score->notes[0].key == 60);
  15308. CHECK (score->notes[1].start == 480 && score->notes[1].colour == 4 && score->notes[1].velocity == 0.5f);
  15309. CHECK (score->length() == 720);
  15310. }
  15311. CHECK (! parseScore (std::vector<std::uint8_t> { 1, 2, 3 }));
  15312. const auto grid = gridScore (240);
  15313. CHECK (grid.notes.size() == 4 && grid.length() == 960 && grid.notes[3].start == 720);
  15314. CHECK (gridScore (240, 1.0f).notes[1].start == 240 + 120);
  15315. if (! realFlpDir.empty()) // the installation's templates
  15316. {
  15317. std::ifstream f (realFlpDir + "/Patches/Scores/Quantization/Default.fsc", std::ios::binary);
  15318. std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (f)), {});
  15319. const auto fsc = parseScore (bytes);
  15320. CHECK (fsc && fsc->notes.size() == 16 && fsc->length() == 3840 && fsc->notes[1].start == 240);
  15321. // an old (FL 3.5, 20-byte records) chopping pattern with colour-3 notes, read as colour 12
  15322. std::ifstream g (realFlpDir + "/Patches/Scores/Chopping/Default.fsc", std::ios::binary);
  15323. std::vector<std::uint8_t> chopBytes ((std::istreambuf_iterator<char> (g)), {});
  15324. const auto chop = parseScore (chopBytes);
  15325. CHECK (chop && ! chop->notes.empty());
  15326. }
  15327. }
  15328. TEST (prtools_quantize)
  15329. {
  15330. using namespace lynx;
  15331. using namespace lynx::prtools;
  15332. const auto grid = gridScore (240);
  15333. Pattern p;
  15334. p.notes = { prNote (1010, 500), prNote (1130, 100) };
  15335. QuantizeOptions o; // start strength 64/128, sensitivity 36 (180 ticks)
  15336. quantize (p, allOf (p), grid, o);
  15337. CHECK (p.notes[0].start == 985 && p.notes[0].length == 500); // 1010 - (50 * 64) / 128
  15338. CHECK (p.notes[1].start == 1165); // toward 1200: 1130 + (70 * 64) / 128
  15339. p.notes = { prNote (1010, 500), prNote (1130, 100) };
  15340. o.start = 1.0f;
  15341. o.sensitivity = 12; // 60 ticks: 1130 is 70 away from 1200
  15342. quantize (p, allOf (p), grid, o);
  15343. CHECK (p.notes[0].start == 960 && p.notes[1].start == 1130);
  15344. p.notes = { prNote (1010, 500) };
  15345. o.sensitivity = 36;
  15346. o.durationMode = QuantizeOptions::leaveEnd;
  15347. quantize (p, allOf (p), grid, o);
  15348. CHECK (p.notes[0].start == 960 && p.notes[0].length == 550); // the end (1509) stays
  15349. p.notes = { prNote (1010, 500) };
  15350. o.durationMode = QuantizeOptions::quantizeEnd;
  15351. o.duration = 1.0f;
  15352. quantize (p, allOf (p), grid, o);
  15353. CHECK (p.notes[0].start == 960 && p.notes[0].length == 1439 - 960 + 1); // end 1509 -> the nearest end 1439
  15354. // levels: a template velocity of 50/100 halves the velocity; 0 removes the note
  15355. Score half = grid;
  15356. for (auto& n : half.notes)
  15357. n.velocity = 50.0f / 128.0f;
  15358. p.notes = { prNote (960, 100, 60, 0.8f) };
  15359. o = {};
  15360. quantize (p, allOf (p), half, o);
  15361. CHECK (std::abs (p.notes[0].velocity - 0.4f) < 1e-5f);
  15362. for (auto& n : half.notes)
  15363. n.velocity = 0.0f;
  15364. quantize (p, allOf (p), half, o);
  15365. CHECK (p.notes.empty());
  15366. }
  15367. TEST (prtools_chop)
  15368. {
  15369. using namespace lynx;
  15370. using namespace lynx::prtools;
  15371. Score pat;
  15372. pat.notes = { ScoreNote { 0, 240, 60 }, ScoreNote { 480, 240, 67 } }; // period 720
  15373. Pattern p;
  15374. p.notes = { prNote (0, 1920, 50) };
  15375. ChopOptions o;
  15376. chop (p, allOf (p), pat, o);
  15377. CHECK (p.notes.size() == 5);
  15378. if (p.notes.size() == 5)
  15379. {
  15380. CHECK (p.notes[0].start == 0 && p.notes[0].key == 50 && p.notes[1].start == 480 && p.notes[1].key == 57);
  15381. CHECK (p.notes[2].start == 720 && p.notes[3].start == 1200 && p.notes[4].start == 1440 && p.notes[4].length == 240);
  15382. }
  15383. // absolute: the pattern stays on the song grid; a piece before the note start is cut
  15384. p.notes = { prNote (100, 600, 50) };
  15385. chop (p, allOf (p), pat, o);
  15386. CHECK (p.notes.size() == 2 && p.notes[0].start == 100 && p.notes[0].length == 140 && p.notes[1].start == 480);
  15387. o.absolute = false;
  15388. p.notes = { prNote (100, 600, 50) };
  15389. chop (p, allOf (p), pat, o);
  15390. CHECK (p.notes.size() == 2 && p.notes[0].start == 100 && p.notes[0].length == 240 && p.notes[1].start == 580
  15391. && p.notes[1].length == 120);
  15392. // time mul x2 (wheel 5); colour 4 holds to the end in the first round only
  15393. o = {};
  15394. o.timeMul = 5;
  15395. p.notes = { prNote (0, 1920, 60) };
  15396. chop (p, allOf (p), pat, o);
  15397. CHECK (p.notes.size() == 3 && p.notes[1].start == 960 && p.notes[1].length == 480 && p.notes[2].start == 1440);
  15398. pat.notes[1].colour = 4;
  15399. o.timeMul = 4;
  15400. p.notes = { prNote (0, 1920, 60) };
  15401. chop (p, allOf (p), pat, o);
  15402. CHECK (p.notes.size() == 4 && p.notes[1].length == 1920 - 480 && p.notes[3].start == 1440);
  15403. }
  15404. TEST (prtools_flam)
  15405. {
  15406. using namespace lynx;
  15407. using namespace lynx::prtools;
  15408. Pattern p;
  15409. p.notes = { prNote (960, 480, 60, 0.75f) };
  15410. flam (p, allOf (p), flamPreset (FlamPreset::doubleStroke), 120.0); // +1 step after
  15411. CHECK (p.notes.size() == 2 && p.notes[0].start == 960 && p.notes[1].start == 1200 && p.notes[1].length == 480);
  15412. CHECK (std::abs (p.notes[1].velocity - 0.75f * 85.0f / 128.0f) < 1e-6f);
  15413. p.notes = { prNote (960, 480) };
  15414. flam (p, allOf (p), flamPreset (FlamPreset::earlyDoubleStroke), 120.0); // the flam one step before, shortened
  15415. CHECK (p.notes.size() == 2 && p.notes[0].start == 720 && p.notes[0].length == 240 && p.notes[1].start == 960);
  15416. // absolute: -64 = -33.3 ms = -64 ticks at 120 BPM (960 PPQ); the original moves, the flam stays on the grid
  15417. CHECK (flamOffset (flamPreset (FlamPreset::flam), 120.0) == -64);
  15418. p.notes = { prNote (960, 480) };
  15419. flam (p, allOf (p), flamPreset (FlamPreset::flam), 120.0);
  15420. CHECK (p.notes.size() == 2 && p.notes[0].start == 960 && p.notes[0].length == 64 && p.notes[1].start == 1024);
  15421. p.notes = { prNote (960, 480) };
  15422. flam (p, allOf (p), flamPreset (FlamPreset::earlyFlam), 120.0);
  15423. CHECK (p.notes.size() == 2 && p.notes[0].start == 896 && p.notes[1].start == 960 && p.notes[1].length == 480);
  15424. }
  15425. TEST (prtools_lfo)
  15426. {
  15427. using namespace lynx;
  15428. using namespace lynx::prtools;
  15429. CHECK (lfoPeriod (20480) == 3840.0 && lfoPeriod (0) == 960.0 * 128 && lfoPeriod (40960) == 120.0);
  15430. LfoOptions o; // sine, phase 1/2: starts at the bottom (value 1/2, range 1/4)
  15431. const auto c = lfoCurve (o, 3840);
  15432. 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);
  15433. o.shape = LfoOptions::square;
  15434. o.phase = 0; // phase 0.5: Round (0.5) = 0 (half to even) -> +1, then -1
  15435. const auto sq = lfoCurve (o, 3840);
  15436. CHECK (sq[0] == 0.75f && sq[10] == 0.25f && sq[2000] == 0.75f);
  15437. o = {};
  15438. o.useEnd = true;
  15439. o.endRange = 0; // the range fades out
  15440. const auto fade = lfoCurve (o, 3840);
  15441. CHECK (std::abs (fade[3839] - 0.5f) < 0.01f);
  15442. Pattern p;
  15443. p.notes = { prNote (0, 100), prNote (960, 100), prNote (1920, 100) };
  15444. lfo (p, allOf (p), notetools::Level::velocity, LfoOptions {});
  15445. CHECK (std::abs (p.notes[0].velocity - 0.25f) < 1e-4f && std::abs (p.notes[1].velocity - 0.5f) < 1e-3f
  15446. && std::abs (p.notes[2].velocity - 0.75f) < 1e-3f);
  15447. }
  15448. TEST (prtools_claw)
  15449. {
  15450. using namespace lynx;
  15451. using namespace lynx::prtools;
  15452. ClawOptions o; // beat, trash the 4th quarter, stretch, remove short notes
  15453. Pattern p;
  15454. p.notes = { prNote (0, 240), prNote (960, 240), prNote (750, 100), prNote (1440, 240) };
  15455. claw (p, allOf (p), o);
  15456. CHECK (p.notes.size() == 3); // the note inside the trashed slice is gone
  15457. if (p.notes.size() == 3)
  15458. {
  15459. CHECK (p.notes[0].start == 0 && p.notes[0].length == 320);
  15460. CHECK (p.notes[1].start == 960 && p.notes[1].length == 320);
  15461. CHECK (p.notes[2].start == 1600 && p.notes[2].length == 320); // 1440 - 240 = 1200 -> x 4/3
  15462. }
  15463. o.trash = 9; // none: only the warp, which is neutral
  15464. p.notes = { prNote (100, 300) };
  15465. claw (p, allOf (p), o);
  15466. CHECK (p.notes.size() == 1 && p.notes[0].start == 100 && p.notes[0].length == 300);
  15467. o.timeDistortion = 1024; // strong bend: the period's ends stay, the middle moves
  15468. p.notes = { prNote (0, 480), prNote (960, 480) };
  15469. claw (p, allOf (p), o);
  15470. CHECK (p.notes[0].start == 0 && p.notes[1].start == 960 && p.notes[0].length > 480 && p.notes[1].length > 480);
  15471. o.trash = 0; // the first quarter
  15472. o.timeDistortion = 0;
  15473. o.stretch = false;
  15474. p.notes = { prNote (0, 240), prNote (240, 240) };
  15475. claw (p, allOf (p), o);
  15476. CHECK (p.notes.size() == 1 && p.notes[0].start == 0 && p.notes[0].length == 240);
  15477. }
  15478. TEST (prtools_articulate)
  15479. {
  15480. using namespace lynx;
  15481. using namespace lynx::prtools;
  15482. Pattern p;
  15483. p.notes = { prNote (0, 100), prNote (480, 100), prNote (960, 100) };
  15484. articulate (p, allOf (p), articulatePreset (ArticulatePreset::legato), lynx::Meter (4, 4));
  15485. CHECK (p.notes[0].length == 480 && p.notes[1].length == 480 && p.notes[2].length == 3840 - 960); // to the bar line
  15486. p.notes = { prNote (0, 100), prNote (480, 100), prNote (960, 2880) };
  15487. articulate (p, allOf (p), articulatePreset (ArticulatePreset::staccato), lynx::Meter (4, 4));
  15488. CHECK (p.notes[0].length == 96 && p.notes[1].length == 96 && p.notes[2].length == 576); // ends on a bar: own length
  15489. auto o = articulatePreset (ArticulatePreset::legato);
  15490. o.gap = 20;
  15491. p.notes = { prNote (0, 100), prNote (480, 100) };
  15492. articulate (p, allOf (p), o, lynx::Meter (4, 4));
  15493. CHECK (p.notes[0].length == 460);
  15494. // chop chords: a held note is cut where the next one starts
  15495. p.notes = { prNote (0, 960, 48), prNote (480, 480, 60) };
  15496. articulate (p, allOf (p), articulatePreset (ArticulatePreset::chopChordsOnly), lynx::Meter (4, 4));
  15497. CHECK (p.notes.size() == 3 && p.notes[0].length == 480 && p.notes[1].start == 480 && p.notes[2].start == 480);
  15498. // variation is seeded (MT19937): same seed, same result; lengths only get shorter
  15499. o = articulatePreset (ArticulatePreset::legato);
  15500. o.variation = 50;
  15501. o.seed = 7;
  15502. Pattern a, b2;
  15503. a.notes = b2.notes = { prNote (0, 100), prNote (480, 100), prNote (960, 100) };
  15504. articulate (a, allOf (a), o, lynx::Meter (4, 4));
  15505. articulate (b2, allOf (b2), o, lynx::Meter (4, 4));
  15506. CHECK (a.notes[0].length == b2.notes[0].length && a.notes[0].length <= 480 && a.notes[0].length >= 240);
  15507. }
  15508. TEST (prtools_chords)
  15509. {
  15510. using namespace lynx::prtools;
  15511. auto c = parseProgression ("I-V-vi-IV", 0, false);
  15512. CHECK (c && c->size() == 4);
  15513. if (c && c->size() == 4)
  15514. {
  15515. CHECK ((*c)[0].root == 0 && (*c)[1].root == 7 && (*c)[2].root == 9 && (*c)[3].root == 5);
  15516. CHECK ((*c)[2].intervals == std::vector<int> ({ 0, 3, 7 }) && chordName ((*c)[2]) == "Am");
  15517. }
  15518. c = parseProgression ("ii 7-V 7-I \xE2\x88\x86-IV/\xE2\x99\xADVII", 0, false);
  15519. CHECK (c && c->size() == 4);
  15520. if (c && c->size() == 4)
  15521. {
  15522. CHECK ((*c)[0].intervals == std::vector<int> ({ 0, 3, 7, 10 }) && chordName ((*c)[0]) == "Dm7");
  15523. CHECK ((*c)[2].intervals == std::vector<int> ({ 0, 4, 7, 11 }) && chordName ((*c)[2]) == "Cmaj7");
  15524. CHECK ((*c)[3].root == 5 && (*c)[3].bass == 10 && chordName ((*c)[3]) == "F/A#");
  15525. }
  15526. c = parseProgression ("i-v-\xE2\x99\xADVI-iv", 9, true); // A minor: Am Em F Dm
  15527. CHECK (c && c->size() == 4 && (*c)[2].root == 5 && (*c)[3].root == 2 && (*c)[1].intervals[1] == 3);
  15528. CHECK (parseProgression ("Isus2 add9", 0, false) && parseProgression ("Isus2 add9", 0, false)->front().intervals
  15529. == std::vector<int> ({ 0, 2, 7, 14 }));
  15530. CHECK (! parseProgression ("I-X-V", 0, false));
  15531. 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");
  15532. CHECK (presets.size() == 3 && presets[0].numerals == "I-V-vi-IV" && ! presets[0].minor && presets[2].minor);
  15533. const auto rhythms = parseRhythmPresets ("Default,4,>,-,-,-,>,-,-,-,>,-,-,-,>,-,-,-\nRandom 1,4,r,16,2,1.5\n");
  15534. CHECK (rhythms.size() == 1 && rhythms[0].cells == ">--->--->--->---");
  15535. const auto g = generateProgression (0, false, 8, 0.3f, 5);
  15536. CHECK (g.size() == 8 && g[0].root == 0 && g == generateProgression (0, false, 8, 0.3f, 5));
  15537. for (std::uint32_t seed = 1; seed < 40; ++seed) // numerals read back as the same chords
  15538. {
  15539. const auto gen = generateProgression (2, seed % 2 == 0, 8, 1.0f, seed);
  15540. const auto back = parseProgression (toNumerals (gen, 2), 2, seed % 2 == 0);
  15541. CHECK (back && *back == gen);
  15542. }
  15543. CHECK (toNumerals (*parseProgression ("I-V-vi-IV/bVII", 0, false), 0) == "I-V-vi-IV/bVII");
  15544. lynx::Pattern p;
  15545. const auto chords = *parseProgression ("I-IV", 0, false);
  15546. ChordWriteOptions w;
  15547. const auto added = writeProgression (p, 2, 0, chords, w);
  15548. CHECK (added.size() == 8 && p.notes.size() == 8 && p.notes[0].channel == 2);
  15549. CHECK (std::any_of (p.notes.begin(), p.notes.end(), [] (auto& n) { return n.key == 48; })); // bass C3
  15550. w.rhythm = ">-`->-`->-`->-`-";
  15551. lynx::Pattern r;
  15552. writeProgression (r, 0, 0, chords, w);
  15553. CHECK (r.notes.size() == 8 && r.notes[0].length == 1920); // '>' + '-' of 960-tick cells
  15554. }
  15555. TEST (prtools_riff_machine)
  15556. {
  15557. using namespace lynx;
  15558. using namespace lynx::prtools;
  15559. Pattern p;
  15560. p.notes = { prNote (0, 100, 70) };
  15561. p.notes[0].channel = 1;
  15562. RiffOptions o;
  15563. o.channel = 1;
  15564. o.bars = 2;
  15565. const auto notes = riffMachine (p, { 0 }, o, lynx::Meter (4, 4));
  15566. CHECK (! notes.empty() && notes.size() == p.notes.size());
  15567. CHECK (std::all_of (p.notes.begin(), p.notes.end(), [] (auto& n) {
  15568. return n.channel == 1 && n.start >= 0 && n.start < 2 * 3840 && n.key >= 36 && n.key <= 84;
  15569. }));
  15570. // only the first step: the progression shape, chopped from one note at key 48
  15571. o.stepsUpTo = 0;
  15572. Pattern q;
  15573. const auto first = riffMachine (q, {}, o, lynx::Meter (4, 4));
  15574. CHECK (first.size() == builtInRiffProgression (0).notes.size() * 2 && q.notes[0].key == 48);
  15575. }
  15576. TEST (event_editor_lanes)
  15577. {
  15578. using namespace lynx;
  15579. Pattern p;
  15580. const AutomationTarget vol { AutomationTargetKind::channelVolume, 2 };
  15581. auto& lane = eventedit::laneFor (p, vol);
  15582. CHECK (p.events.size() == 1 && &eventedit::laneFor (p, vol) == &lane && eventedit::findLane (p, { AutomationTargetKind::channelPan, 2 }) == nullptr);
  15583. eventedit::fill (lane, 0, 960, 0.25f, 240);
  15584. CHECK (lane.events.size() == 4 && lane.events[3].tick == 720 && lane.events[3].value == 0.25f);
  15585. CHECK (lane.valueAt (-5.0) == 0.25f && lane.valueAt (5000.0) == 0.25f);
  15586. eventedit::line (lane, 960, 1920, 0.0f, 1.0f, 240); // 4 cells: 0, 1/3, 2/3, 1
  15587. CHECK (lane.events.size() == 8 && lane.events[4].value == 0.0f && lane.events[7].value == 1.0f);
  15588. CHECK (std::abs (lane.valueAt (1500.0) - 2.0f / 3.0f) < 1e-6f); // held from 1440
  15589. eventedit::setEvent (lane, 1440, 0.1f);
  15590. CHECK (lane.events.size() == 8 && lane.valueAt (1440.0) == 0.1f);
  15591. eventedit::setEvent (lane, 100, 0.9f);
  15592. CHECK (lane.events.size() == 9 && lane.events[1].tick == 100);
  15593. eventedit::scale (lane, 0, 960, 2.0f, 0.0f, false);
  15594. CHECK (lane.events[0].value == 0.5f && lane.events[1].value == 1.0f); // clamped
  15595. eventedit::scale (lane, 0, 960, 0.0f, 0.0f, true); // bipolar: toward the centre
  15596. CHECK (lane.events[0].value == 0.5f && lane.events[2].value == 0.5f);
  15597. eventedit::erase (lane, 0, 960);
  15598. CHECK (lane.events.size() == 4 && lane.events.front().tick == 960);
  15599. auto copy = lane;
  15600. eventedit::randomize (lane, 0, 4000, 0.2f, 3);
  15601. eventedit::randomize (copy, 0, 4000, 0.2f, 3);
  15602. CHECK (lane.events[1].value == copy.events[1].value && std::abs (lane.events[1].value - 1.0f / 3.0f) <= 0.2f + 1e-6f);
  15603. prtools::LfoOptions o;
  15604. eventedit::applyLfo (lane, 0, 3840, o, 960); // FL's LFO: bottom, middle, top, middle
  15605. 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);
  15606. p.events.push_back ({ { AutomationTargetKind::channelPan, 1 }, {} });
  15607. eventedit::removeEmptyLanes (p);
  15608. CHECK (p.events.size() == 1);
  15609. // undo label
  15610. auto before = Song::makeDefault();
  15611. auto after = before;
  15612. after.patterns[0].events.push_back ({ vol, { { 0, 0.5f } } });
  15613. CHECK (describeChangeText (before, after) == "Edit events: " + after.patterns[0].name);
  15614. // channel removal re-targets the lanes like automations
  15615. auto song = Song::makeDefault();
  15616. song.patterns[0].events.push_back ({ { AutomationTargetKind::channelVolume, 3 }, { { 0, 0.5f } } });
  15617. song.removeChannel (1);
  15618. CHECK (song.patterns[0].events[0].target.index == 2);
  15619. }
  15620. TEST (event_editor_playback)
  15621. {
  15622. // pattern events on the Master volume: silent first half of each pattern, sound in the second half
  15623. auto song = lynx::Song::makeDefault();
  15624. song.bpm = 120.0;
  15625. const auto bar = (std::int64_t) lynx::ppq * 4;
  15626. song.patterns[0].lengthTicks = bar;
  15627. song.patterns[0].notes.clear();
  15628. for (int s = 0; s < 16; ++s)
  15629. song.patterns[0].notes.push_back ({ s * 240, 200, 0, 60 });
  15630. song.patterns[0].events.push_back ({ { lynx::AutomationTargetKind::mixerVolume, 0 }, { { 0, 0.0f }, { bar / 2, 0.8f } } });
  15631. const auto peaks = [&] (lynx::PlayMode mode) {
  15632. song.mode = mode;
  15633. song.playlist.clear();
  15634. song.playlist.push_back ({ 0, 0, bar, bar, 0 }); // one clip, in the second bar
  15635. const double sr = 48000.0;
  15636. auto state = lynx::RackState::build (song, nullptr, sr);
  15637. lynx::RackRenderer r;
  15638. r.prepare (sr, 512);
  15639. std::vector<float> l (512), rr (512);
  15640. const auto tps = lynx::ticksPerSample (song.bpm, sr);
  15641. std::array<float, 6> peak {}; // half bars
  15642. for (double tick = 0.0; tick < (double) (bar * 3); tick += 512 * tps)
  15643. {
  15644. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  15645. // blocks inside a half bar, away from its edges (the gain glides over one chunk at a change)
  15646. const auto blockEnd = tick + 512 * tps;
  15647. const auto half = (std::size_t) (tick / (double) (bar / 2));
  15648. const auto edge = (double) half * (double) (bar / 2);
  15649. if (half > 5 || tick - edge < 2 * 512 * tps || blockEnd > edge + (double) (bar / 2))
  15650. continue;
  15651. for (auto v : l)
  15652. peak[half] = std::max (peak[half], std::abs (v));
  15653. }
  15654. return std::make_pair (state->eventLanes.size(), peak);
  15655. };
  15656. const auto [lanesSong, song3] = peaks (lynx::PlayMode::song);
  15657. CHECK (lanesSong == 1);
  15658. CHECK (song3[2] < 1.0e-4f && song3[3] > 0.01f); // inside the clip: silent, then sound
  15659. const auto [lanesPattern, loop] = peaks (lynx::PlayMode::pattern);
  15660. CHECK (lanesPattern == 1);
  15661. CHECK (loop[0] < 1.0e-4f && loop[1] > 0.01f && loop[2] < 1.0e-4f && loop[3] > 0.01f);
  15662. }
  15663. TEST (flp_pattern_events_import)
  15664. {
  15665. // REA_rodada85 §1: event 0xDF holds a pattern's events {u32 tick, u32 controlID, i32 raw value}, 0xD8 the values
  15666. // reset when the song starts; they become pattern event lanes in FL's normalised scale
  15667. FlpBuilder b;
  15668. b.u8 (0x40); b.u16 (0); // channel 0
  15669. b.u8 (0x15); b.u8 (0); // sampler
  15670. b.u8 (0x41); b.u16 (1); // pattern 1: events only, no notes
  15671. auto rec = [] (std::vector<std::uint8_t>& v, std::uint32_t tick, std::uint32_t id, std::int32_t value) {
  15672. for (auto w : { tick, id, (std::uint32_t) value })
  15673. for (int i = 0; i < 4; ++i)
  15674. v.push_back ((std::uint8_t) (w >> (8 * i)));
  15675. };
  15676. std::vector<std::uint8_t> events;
  15677. rec (events, 0, 0x70001FC0u, 16000); // Master volume: top
  15678. rec (events, 96, 0x70001FC0u, 8000);
  15679. rec (events, 96, 0x70001FC0u, 4000); // same tick: the last one wins
  15680. rec (events, 0, 0x00000001u, 0); // channel 0 pan: hard left
  15681. rec (events, 48, 0x00000001u, 12800); // hard right
  15682. rec (events, 0, 0x00008000u, 5); // a plugin parameter of a sampler: scale unknown, skipped
  15683. b.data (0xDF, events);
  15684. std::vector<std::uint8_t> init;
  15685. rec (init, 0, 0x70001FC1u, -6400); // Master pan reset to hard left at song start
  15686. b.data (0xD8, init);
  15687. const auto project = flp::parseProject (b.bytes);
  15688. CHECK (project.initControls.size() == 1);
  15689. const flp::Pattern* src = nullptr;
  15690. for (const auto& p : project.patterns)
  15691. if (p.index == 1)
  15692. src = &p;
  15693. CHECK (src != nullptr && src->controlEvents.size() == 6);
  15694. lynx::FlpImportReport report;
  15695. lynx::FlpImportOptions options;
  15696. options.report = &report;
  15697. const auto song = lynx::importFlp (project, options);
  15698. const lynx::PatternEvents* volume = nullptr;
  15699. const lynx::PatternEvents* pan = nullptr;
  15700. int lanes = 0;
  15701. for (const auto& p : song.patterns)
  15702. for (const auto& lane : p.events)
  15703. {
  15704. ++lanes;
  15705. if (lane.target.kind == lynx::AutomationTargetKind::mixerVolume && lane.target.index == 0)
  15706. volume = &lane;
  15707. if (lane.target.kind == lynx::AutomationTargetKind::channelPan)
  15708. pan = &lane;
  15709. }
  15710. CHECK (lanes == 2 && volume != nullptr && pan != nullptr);
  15711. if (volume != nullptr && volume->events.size() == 2)
  15712. {
  15713. CHECK (volume->events[0].tick == 0 && volume->events[1].tick == lynx::ppq);
  15714. CHECK (volume->events[0].value > volume->events[1].value); // 4000 (not 8000) is the one kept
  15715. }
  15716. else
  15717. CHECK (false);
  15718. if (pan != nullptr && pan->events.size() == 2)
  15719. CHECK (pan->events[0].value < 0.01f && pan->events[1].value > 0.99f && pan->events[1].tick == lynx::ppq / 2);
  15720. else
  15721. CHECK (false);
  15722. CHECK (report.converted["Pattern events"] == 2);
  15723. CHECK (song.targetValue ({ lynx::AutomationTargetKind::mixerPan, 0 }) < 0.01f);
  15724. }
  15725. #include "PrefsTests.inc"
  15726. #include "core/ProjectSafety.h"
  15727. #include "core/SearchIndex.h"
  15728. #include "core/PluginCatalog.h"
  15729. #include "SafetyTests118.inc"
  15730. #include "Rea134Tests.inc"
  15731. #include "Rea139Tests.inc"
  15732. #include "Rea143Tests.inc"
  15733. #include "Rea143aTests.inc"
  15734. #include "Rea143bTests.inc"
  15735. #include "Rea146aTests.inc"
  15736. #include "Rea146bTests.inc"
  15737. #include "Rea146Tests.inc"
  15738. #include "Rea149Tests.inc"
  15739. #include "PluginCatalogTests151.inc"
  15740. #include "Rea151Tests.inc"
  15741. #include "AudioEditTests.inc"
  15742. #include "AudioEditToolsTests.inc"
  15743. #include "InputTests.inc"
  15744. #include "../asio/InputDrift.h"
  15745. #include "AsioDriftTests.inc"
  15746. TEST (level_audit_demo_imports_round89)
  15747. {
  15748. // REA_rodada89 §4: two demo projects whose FL renders were much louder than LYNX's
  15749. if (realFlpDir.empty())
  15750. return;
  15751. auto load = [] (const std::filesystem::path& path) -> std::optional<lynx::Song> {
  15752. if (! std::filesystem::exists (path))
  15753. return std::nullopt;
  15754. std::ifstream in (path, std::ios::binary);
  15755. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  15756. return lynx::importFlp (flp::parseProject (file));
  15757. };
  15758. const auto demos = std::filesystem::path (realFlpDir) / "Demo projects";
  15759. // FL 12+: a route to insert 100+ without a level record plays at 100% (the percussion bus of "Lightning")
  15760. if (const auto song = load (demos / "Product demos" / "Square a Saw - Lightning (VEELA Vocals Resynthesized).flp"))
  15761. {
  15762. int highRoutes = 0, highAtZero = 0;
  15763. for (const auto& t : song->mixer)
  15764. for (const auto& send : t.sends)
  15765. if (send.target >= 100)
  15766. {
  15767. ++highRoutes;
  15768. highAtZero += send.level <= 0.0f;
  15769. }
  15770. std::printf (" Lightning: %d routes to inserts 100+, %d at level 0\n", highRoutes, highAtZero);
  15771. CHECK (highRoutes > 0 && highAtZero == 0);
  15772. }
  15773. // Fruity Compressors FL saved as VST2 inside Fruity Wrapper (RawFL): the exact engine with the saved controls
  15774. if (const auto song = load (demos / "Visual" / "ZGameEditor Visualizer" / "Treben - Gimbal and Sinan - RawFL.flp"))
  15775. {
  15776. int compressors = 0, withSettings = 0;
  15777. for (const auto& t : song->mixer)
  15778. for (const auto& fx : t.effects)
  15779. if (fx.kind == lynx::EffectKind::fruityCompressor)
  15780. {
  15781. ++compressors;
  15782. const auto defaults = lynx::makeEffectSlot (lynx::EffectKind::fruityCompressor);
  15783. withSettings += fx.params != defaults.params;
  15784. }
  15785. std::printf (" RawFL: %d wrapped Fruity Compressors on the exact engine, %d with their own settings\n", compressors, withSettings);
  15786. CHECK (compressors >= 10 && withSettings >= 8);
  15787. }
  15788. }
  15789. #include "UiLook90Tests.inc"
  15790. #include "UiFl104Tests.inc"
  15791. #include "DialogsTests84.inc"
  15792. #include "DockTests188.inc"
  15793. #include "SubstituteTests.inc"
  15794. #include "MixSuiteTests96.inc"
  15795. #include "PanelsTests105.inc"
  15796. // --- native plugin editors (round 93): FL's arrangements ---------------------------------------------------------
  15797. TEST (plugin_layouts_name_real_parameters)
  15798. {
  15799. using namespace lynx;
  15800. // every value control edits a parameter of its effect, inside the body; FL arrangements place (or list in a menu)
  15801. // every parameter of the effect
  15802. for (const auto kind : plugui::flLayoutKinds())
  15803. {
  15804. const auto& l = plugui::layoutFor (kind);
  15805. const auto& info = effectInfo (kind);
  15806. CHECK (plugui::hasFlLayout (kind));
  15807. // FL's own size wins for its one-cell editors (Fruity Center, Fruity Phase Inverter: 170 x 55)
  15808. const auto minHeight = kind == EffectKind::flCenter || kind == EffectKind::flPhaseInverter ? 50 : 60;
  15809. if (! (l.width > 100 && l.height > minHeight))
  15810. std::printf (" %s: %d x %d\n", info.name, l.width, l.height);
  15811. CHECK (l.width > 100 && l.height > minHeight);
  15812. std::vector<bool> placed ((std::size_t) plugui::paramCount (kind), false);
  15813. for (const auto& item : l.items)
  15814. {
  15815. 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))
  15816. std::printf (" %s: item '%s' leaves the body\n", info.name, item.param != nullptr ? item.param : item.label != nullptr ? item.label : "");
  15817. 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);
  15818. if (item.param != nullptr)
  15819. {
  15820. const auto p = plugui::paramIndex (kind, item.param);
  15821. CHECK (p >= 0);
  15822. if (p >= 0)
  15823. placed[(std::size_t) p] = true;
  15824. }
  15825. if (item.type == plugui::Item::radio || item.type == plugui::Item::tabs || item.type == plugui::Item::menu)
  15826. {
  15827. const auto labels = plugui::optionLabels (item);
  15828. CHECK (labels.size() >= 2);
  15829. CHECK (item.values.empty() || item.values.size() == labels.size());
  15830. }
  15831. if (item.type == plugui::Item::knob && item.h + 0.5f < item.w) // (a caption above its knob would hide it)
  15832. {
  15833. std::printf (" %s: knob '%s' is shorter than it is wide\n", info.name, item.param != nullptr ? item.param : "");
  15834. CHECK (false);
  15835. }
  15836. CHECK (item.param2 == nullptr || plugui::paramIndex (kind, item.param2) >= 0);
  15837. if (item.param2 != nullptr && plugui::paramIndex (kind, item.param2) >= 0)
  15838. placed[(std::size_t) plugui::paramIndex (kind, item.param2)] = true;
  15839. for (const auto* when : { item.whenParam, item.when2Param, item.when3Param })
  15840. CHECK (when == nullptr || plugui::paramIndex (kind, when) >= 0);
  15841. }
  15842. for (const auto* name : l.menuSwitches)
  15843. {
  15844. const auto p = plugui::paramIndex (kind, name);
  15845. CHECK (p >= 0);
  15846. if (p >= 0)
  15847. placed[(std::size_t) p] = true;
  15848. }
  15849. for (const auto* name : l.editedElsewhere)
  15850. {
  15851. const auto p = plugui::paramIndex (kind, name);
  15852. CHECK (p >= 0);
  15853. if (p >= 0)
  15854. placed[(std::size_t) p] = true;
  15855. }
  15856. for (std::size_t p = 0; p < placed.size(); ++p)
  15857. if (! placed[p])
  15858. {
  15859. std::printf (" %s: parameter '%s' has no control\n", info.name, plugui::paramName (kind, (int) p));
  15860. CHECK (false);
  15861. }
  15862. }
  15863. }
  15864. TEST (maximus_editor_edits_the_slot_state)
  15865. {
  15866. using namespace lynx;
  15867. // the Maximus page edits the 59 parameters stored in the slot's state (REA_rodada8 §1.2)
  15868. std::array<std::array<int, 13>, 4> bands {};
  15869. for (auto& b : bands)
  15870. b = { 0, 1000, 1000, 0, 0, 1000, 1115, 0, 2, 5000, 3, 2882, 0 };
  15871. std::array<std::vector<std::array<double, 4>>, 8> envs;
  15872. for (auto& e : envs)
  15873. e = { { 0.0, 0.0, 0.0, 2 }, { 0.5, 0.5, 0.0, 2 }, { 0.5, 0.5, 0.0, 2 } };
  15874. const auto state = maximusTestState ({ 19800, 1, 48065, 1, 1115, 1000, 32768 }, bands, envs, 0);
  15875. const auto slot = maximusfx::pack ({ hlfx::MaximusBuild::shared, -1, state });
  15876. CHECK (maximusfx::stateParams().size() == 59);
  15877. CHECK (plugui::paramIndex (EffectKind::maximus, "Master release") == 7 + 13 * 3 + 9);
  15878. CHECK (maximusfx::stateParam (slot, 0).value_or (-1) == 19800);
  15879. CHECK (maximusfx::stateParam (slot, 7 + 13 + 9).value_or (-1) == 5000);
  15880. CHECK (maximusfx::stateParamText (0, 19800) == "200 Hz");
  15881. CHECK (maximusfx::stateParamText (2, 48065) == "3000 Hz");
  15882. CHECK (maximusfx::stateParamText (8, 1000) == "+0.0 dB");
  15883. CHECK (maximusfx::stateParamText (7 + 6, 0) == "Default");
  15884. CHECK (maximusfx::stateParamText (7 + 13 * 3 + 6, 1115) == "2.00 ms");
  15885. CHECK (maximusfx::stateParamText (6, -1) == "Off");
  15886. const auto edited = maximusfx::withStateParam (slot, 7 + 13 + 1, 2000); // mid pre gain → +18 dB
  15887. CHECK (maximusfx::stateParam (edited, 7 + 13 + 1).value_or (-1) == 2000);
  15888. CHECK (maximusfx::stateParam (edited, 7 + 1).value_or (-1) == 1000);
  15889. CHECK (maximusfx::stateParam (maximusfx::withStateParam (slot, 0, 99999), 0).value_or (-1) == 65536); // clamped
  15890. const auto blob = maximusfx::unpack (edited);
  15891. const auto original = maximusfx::unpack (slot);
  15892. CHECK (blob && original && (hlfx::makeMaximusPreset (blob->state) != nullptr) == (hlfx::makeMaximusPreset (original->state) != nullptr));
  15893. const auto curve = maximusfx::curvePoints (slot, 3);
  15894. CHECK (curve.size() == 3 && std::abs (curve[1].first - 0.5f) < 1e-6f && std::abs (curve[2].first - 1.0f) < 1e-6f);
  15895. CHECK (maximusfx::selectedBand (slot) >= 0);
  15896. CHECK (plugui::hasFlLayout (EffectKind::maximus));
  15897. }
  15898. TEST (plugin_layout_options_match_the_engine_values)
  15899. {
  15900. using namespace lynx;
  15901. // the switches show FL's captions for the engine's own values
  15902. const auto text = [] (EffectKind kind, const char* param, int option) {
  15903. for (const auto& item : plugui::layoutFor (kind).items)
  15904. if (item.param != nullptr && std::strcmp (item.param, param) == 0 && item.options != nullptr)
  15905. return formatEffectParam (kind, plugui::paramIndex (kind, param), plugui::optionValue (item, option), 120.0);
  15906. return std::string ("?");
  15907. };
  15908. CHECK (text (EffectKind::fruityDelay3, "Delay type", 0) == "Fake stereo");
  15909. CHECK (text (EffectKind::fruityDelay3, "Delay type", 2) == "Ping pong");
  15910. CHECK (text (EffectKind::fruityDelay3, "Delay type", 3) == "Off");
  15911. CHECK (text (EffectKind::fruityDelay3, "Filter mode", 1) == "High pass");
  15912. CHECK (text (EffectKind::fruityDelay3, "Saturation type", 1) == "Saturate");
  15913. CHECK (text (EffectKind::fruityMultiband, "Low mode", 1) == "off");
  15914. CHECK (text (EffectKind::fruityMultiband, "High mode", 2) == "bypass");
  15915. CHECK (text (EffectKind::luxeVerb, "Pitch type", 1) == "feed-forward");
  15916. CHECK (text (EffectKind::luxeVerb, "Freeze mode", 2) == "sustain");
  15917. CHECK (text (EffectKind::stereoEnhancer, "Invert", 2) == "invert R");
  15918. CHECK (text (EffectKind::parametricEq, "B3 type", 4) == formatEffectParam (EffectKind::parametricEq, peq::param (2, 0), peq::typeValue (peq::BandType::lowCut), 120.0));
  15919. // the option nearest to a value; pages and conditions
  15920. plugui::LayoutItem radio;
  15921. radio.type = plugui::Item::radio;
  15922. radio.options = "A|B|C|D";
  15923. CHECK (plugui::optionLabels (radio).size() == 4);
  15924. CHECK (plugui::optionAt (radio, 0.30f) == 1);
  15925. CHECK (plugui::optionAt (radio, 0.95f) == 3);
  15926. CHECK (std::abs (plugui::optionValue (radio, 2) - 2.0f / 3.0f) < 1e-6f);
  15927. const auto& lux = plugui::layoutFor (EffectKind::luxeVerb);
  15928. auto params = makeEffectSlot (EffectKind::luxeVerb).params;
  15929. const auto pitchType = plugui::paramIndex (EffectKind::luxeVerb, "Pitch type");
  15930. int feedForward = 0, feedback = 0;
  15931. for (float v : { 0.0f, 1.0f })
  15932. {
  15933. params[(std::size_t) pitchType] = v;
  15934. for (const auto& item : lux.items)
  15935. if (item.param != nullptr && std::strcmp (item.param, "Feed-forward pitch 1") == 0 && plugui::isShown (EffectKind::luxeVerb, item, 0, params))
  15936. ++feedForward;
  15937. else if (item.param != nullptr && std::strcmp (item.param, "Feedback pitch") == 0 && plugui::isShown (EffectKind::luxeVerb, item, 0, params))
  15938. ++feedback;
  15939. }
  15940. CHECK (feedForward == 1); // only in feed-forward mode
  15941. CHECK (feedback == 2); // value plate + fader, only in feedback mode
  15942. const auto& limiter = plugui::layoutFor (EffectKind::fruityLimiter);
  15943. CHECK (limiter.pages.size() == 2);
  15944. int ceilingShown = 0;
  15945. for (int page = 0; page < 2; ++page)
  15946. for (const auto& item : limiter.items)
  15947. if (item.param != nullptr && std::strcmp (item.param, "Ceiling") == 0 && plugui::isShown (EffectKind::fruityLimiter, item, page, params))
  15948. ++ceilingShown;
  15949. CHECK (ceilingShown == 1); // LIMIT page only
  15950. // every other effect still gets a (generated) window
  15951. CHECK (! plugui::hasFlLayout (EffectKind::distortion));
  15952. CHECK (! plugui::layoutFor (EffectKind::distortion).items.empty());
  15953. }
  15954. #include "FabMeasureTests99.inc"
  15955. #include "DynamicsTests108.inc"
  15956. #include "Crest173dTests.inc"
  15957. #include "LynxTuneTests95.inc"
  15958. #include "LynxVerbTests100.inc"
  15959. #include "FacetTests103.inc"
  15960. #include "VocalLevel107Tests.inc"
  15961. #include "Substitutes113Tests.inc"
  15962. #include "Tune120Tests.inc"
  15963. #include "EchoSuiteTests101.inc"
  15964. #include "FruitySendTests105.inc"
  15965. #include "CrashFuzzTests111.inc"
  15966. #include "LookTests114.inc"
  15967. #include "FixedBlockTests118.inc"
  15968. #include "UiFl115Tests.inc"
  15969. #include "LoadTraceTests112.inc"
  15970. #include "VoiceToolsTests119.inc"
  15971. #include "VoiceMatchTests123.inc"
  15972. #include "LoadTests121.inc"
  15973. #include "Parity128Tests.inc"
  15974. #include "Parity128aTests.inc"
  15975. #include "Parity128bTests.inc"
  15976. #include "Parity128cTests.inc"
  15977. #include "Match124Tests.inc"
  15978. #include "Match124xTests.inc"
  15979. #include "Vmatch127Tests.inc"
  15980. #include "ParityTests129.inc"
  15981. #include "Parity135vTests.inc"
  15982. #include "Bugs131Tests.inc"
  15983. #include "Parity135wTests.inc"
  15984. #include "Feat133Tests.inc"
  15985. #include "Rea165fTests.inc"
  15986. #include "Parity135fTests.inc"
  15987. #include "Feat132Tests.inc"
  15988. #include "Parity135sTests.inc"
  15989. #include "VoiceMatch136Tests.inc"
  15990. #include "Parity137Tests.inc"
  15991. #include "Parity144Tests.inc"
  15992. #include "Parity144aTests.inc"
  15993. #include "Parity144bTests.inc"
  15994. #include "Parity147Tests.inc"
  15995. #include "Parity147aTests.inc"
  15996. #include "Parity147bTests.inc"
  15997. #include "Parity150Tests.inc"
  15998. #include "Parity150aTests.inc"
  15999. #include "Parity150bTests.inc"
  16000. #include "Parity152Tests.inc"
  16001. #include "Tp172dTests.inc"
  16002. #include "Verb183bEchoformTests.inc"
  16003. #include "Verb183bEpochTests.inc"
  16004. #include "Verb183bAtriumTests.inc"
  16005. #include "Verb183bPlateTests.inc"
  16006. #include "Verb183bHaloTests.inc"
  16007. #include "Verb183bNebulaTests.inc"
  16008. #include "Verb183bTests.inc"
  16009. #include "Parity152aTests.inc"
  16010. #include "Parity152bTests.inc"
  16011. #include "Parity152cTests.inc"
  16012. #include "Parity165Tests.inc"
  16013. #include "Parity165wTests.inc"
  16014. #include "Parity165xTests.inc"
  16015. #include "Parity165aTests.inc"
  16016. #include "Patcher141Tests.inc"
  16017. #include "Patcher162Tests.inc"
  16018. #include "Vox138Tests.inc"
  16019. #include "Vox145Tests.inc"
  16020. #include "Vox148Tests.inc"
  16021. #include "Natives142Tests.inc"
  16022. TEST (emphasizer_matches_fl_engine)
  16023. {
  16024. // REA_rodada154: FL's Emphasizer through the import (state → slot → effect) against goldens of the installation's
  16025. // Emphasizer_x64.dll (rate set before the state, 2304 samples of silence as FL's mixer runs it before a render,
  16026. // 512-frame calls, 44.1 kHz): FNV-1a of the output's float bits
  16027. auto signal = [] {
  16028. std::vector<float> l (24000), r (24000);
  16029. std::uint32_t s = 12345;
  16030. const float amps[] { 0.3f, 1.5f, 0.05f, 3.0f, 0.8f, 0.0f, 2.0f, 0.4f };
  16031. for (int i = 0; i < 24000; ++i)
  16032. {
  16033. const float a = amps[(i / 3000) % 8];
  16034. s = s * 1664525u + 1013904223u;
  16035. l[(std::size_t) i] = (float) ((s >> 8) / 8388608.0 - 1.0) * a;
  16036. s = s * 1664525u + 1013904223u;
  16037. r[(std::size_t) i] = (float) ((s >> 8) / 8388608.0 - 1.0) * a;
  16038. }
  16039. return std::pair { l, r };
  16040. };
  16041. struct Case { std::array<int, 6> raw; std::uint64_t hash; };
  16042. const Case cases[] { { { 0, 5013, 0, 0, 0, 10000 }, 0xa009608f060eb32eull }, { { 6000, 9000, 1, 0, 0, 8000 }, 0x6fc7ec82cc9d9c01ull },
  16043. { { 8000, 2000, 0, 1, 0, 10000 }, 0xb10a1f3bd998198cull }, { { 3000, 5013, 1, 0, 1, 10000 }, 0x57a8fa4d8b890ac0ull } };
  16044. for (const auto& c : cases)
  16045. {
  16046. std::vector<std::uint8_t> state (4 + 24 + 1, 0);
  16047. const std::int32_t version = 2;
  16048. std::memcpy (state.data(), &version, 4);
  16049. std::memcpy (state.data() + 4, c.raw.data(), 24);
  16050. state[28] = 1;
  16051. const auto slot = lynx::effectFromFl ("Emphasizer", state);
  16052. CHECK (slot && slot->kind == lynx::EffectKind::emphasizer);
  16053. if (! slot)
  16054. continue;
  16055. auto fx = lynx::makeEffect (slot->kind);
  16056. fx->prepare (44100.0);
  16057. auto [l, r] = signal();
  16058. for (int i = 0; i < 24000; i += 512) // the golden's 512-frame engine calls (level matching is per call)
  16059. fx->process (l.data() + i, r.data() + i, std::min (512, 24000 - i), slot->params, 120.0);
  16060. std::uint64_t h = 0xcbf29ce484222325ull;
  16061. for (int i = 0; i < 24000; ++i)
  16062. for (float v : { l[(std::size_t) i], r[(std::size_t) i] })
  16063. {
  16064. std::uint32_t u;
  16065. std::memcpy (&u, &v, 4);
  16066. h = (h ^ u) * 0x100000001b3ull;
  16067. }
  16068. CHECK (h == c.hash);
  16069. }
  16070. // version 1 (no output control) keeps the 0 dB default; texts
  16071. 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 };
  16072. const auto s1 = lynx::emphasizer::parse (v1);
  16073. CHECK (s1 && s1->raw[1] == 5013 && s1->raw[5] == 10000);
  16074. CHECK (lynx::formatEffectParam (lynx::EffectKind::emphasizer, 0, 0.5f, 120.0) == "+12.0 dB");
  16075. CHECK (lynx::formatEffectParam (lynx::EffectKind::emphasizer, 5, 1.0f, 120.0) == "0.0 dB");
  16076. CHECK (lynx::formatEffectParam (lynx::EffectKind::emphasizer, 2, 1.0f, 120.0) == "Soft");
  16077. }
  16078. // ---- Wave Traveller (REA_rodada167f-wavetraveller) ----
  16079. namespace
  16080. {
  16081. // the crafted case: a sample on the 16-bit grid and a state that uses every feature (the same data went to the DLL)
  16082. inline std::vector<float> wtTestSample (int n) // planar stereo, values on the 16-bit grid
  16083. {
  16084. std::vector<float> s ((std::size_t) n * 2);
  16085. std::uint32_t r = 2024;
  16086. for (int i = 0; i < n; ++i)
  16087. for (int c = 0; c < 2; ++c)
  16088. {
  16089. r = r * 1664525u + 1013904223u;
  16090. const auto noise = (int) (r >> 20) - 2048; // ±2048
  16091. const auto tone = (int) std::lround (12000.0 * std::sin ((double) i * (c == 0 ? 0.031 : 0.047)));
  16092. s[(std::size_t) c * (std::size_t) n + (std::size_t) i] = (float) (tone + noise) / 32768.0f;
  16093. }
  16094. return s;
  16095. }
  16096. inline lynx::wavetrav::State wtTestState()
  16097. {
  16098. using namespace lynx::wavetrav;
  16099. auto st = defaultState();
  16100. st.samplePath = "x.wav"; // (FL asks its host for the sample only when the state names one)
  16101. 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) {
  16102. auto p = newPoint (x, y);
  16103. p.type = type, p.tension = tension, p.c1x = c1x, p.c1y = c1y, p.c2x = c2x, p.c2y = c2y;
  16104. return p;
  16105. };
  16106. auto& a = st.patches[60]; // every segment type, tempo based
  16107. a.wave.clear();
  16108. 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 };
  16109. const int ts[] { 1, 2, 3, 4, 5, 6, 8, 1 };
  16110. for (int i = 0; i < 8; ++i)
  16111. a.wave.push_back (pt (xs[i], ys[i], ts[i], ts[i] == 6 ? 0.2f : 0.5f));
  16112. a.speedBeats = 2.0f;
  16113. auto& b = st.patches[61]; // volume curve, mutes, start / end, attack / release
  16114. 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) };
  16115. b.mutes = { { true, 0.0f }, { false, 0.3f }, { true, 0.5f } };
  16116. b.start = 0.2f, b.end = 0.7f, b.attackMs = 50.0f, b.releaseMs = 300.0f, b.speedBeats = 1.0f;
  16117. auto& c = st.patches[62]; // absolute speed, tension segments
  16118. c.splineType = 1, c.absSpeed = 2.3f, c.start = 0.1f, c.end = 0.6f;
  16119. c.wave = { pt (0.0f, 0.0f, 6, 0.5f), pt (0.5f, 0.9f, 6, 0.9f), pt (1.0f, 0.2f, 1) };
  16120. auto& d = st.patches[63]; // types that write nothing, a tiny Bezier
  16121. 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),
  16122. pt (0.5004f, 0.2f, 3), pt (0.501f, 0.9f, 1), pt (1.0f, 0.4f, 1) };
  16123. for (auto& p : st.patches)
  16124. p.rebuild();
  16125. return st;
  16126. }
  16127. std::uint64_t floatBitsHash (const std::vector<float>& v)
  16128. {
  16129. std::uint64_t h = 0xcbf29ce484222325ull;
  16130. for (const float x : v)
  16131. {
  16132. std::uint32_t u;
  16133. std::memcpy (&u, &x, 4);
  16134. h = (h ^ u) * 0x100000001b3ull;
  16135. }
  16136. return h;
  16137. }
  16138. lynx::SampleData wtSampleData (const std::vector<float>& planar, int channels)
  16139. {
  16140. lynx::SampleData d;
  16141. const auto n = planar.size() / (std::size_t) channels;
  16142. d.left.assign (planar.begin(), planar.begin() + (std::ptrdiff_t) n);
  16143. if (channels > 1)
  16144. d.right.assign (planar.begin() + (std::ptrdiff_t) n, planar.end());
  16145. d.sampleRate = 44100.0;
  16146. return d;
  16147. }
  16148. } // namespace
  16149. TEST (wave_traveller_state_and_tables)
  16150. {
  16151. using namespace lynx::wavetrav;
  16152. // FL's state layout (§2): it round-trips byte for byte, and the default patch is FL's
  16153. const auto def = writeState (defaultState());
  16154. State s;
  16155. CHECK (parseState (def, s) && writeState (s) == def && s.selectedKey == 60 && s.samplePath.empty());
  16156. 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);
  16157. const auto crafted = writeState (wtTestState());
  16158. CHECK (parseState (crafted, s) && writeState (s) == crafted);
  16159. CHECK (s.patches[62].splineType == 1 && s.patches[61].mutes.size() == 3 && ! s.patches[61].mutes[1].on && s.patches[60].wave.size() == 8);
  16160. // tables (§3): a 0 → 1 line is 1022 values long (the writer steps back one value per segment)
  16161. CHECK (s.patches[0].waveN == 1022 && s.patches[0].waveTable[0] == 0.0f && s.patches[0].volTable[500] == 1.0f);
  16162. std::vector<float> tables;
  16163. for (int k = 60; k < 64; ++k)
  16164. {
  16165. const auto& p = s.patches[(std::size_t) k];
  16166. tables.insert (tables.end(), p.waveTable.begin(), p.waveTable.begin() + p.waveN + 1);
  16167. tables.insert (tables.end(), p.volTable.begin(), p.volTable.begin() + p.volN + 1);
  16168. CHECK (std::all_of (p.waveTable.begin(), p.waveTable.end(), [] (float v) { return v >= 0.0f && v <= 1.0f; }));
  16169. }
  16170. // (LYNX's own value; the tables reach the DLL goldens of wave_traveller_matches_fl_engine through the output)
  16171. CHECK (floatBitsHash (tables) == 0xd94594a6f22cb4dfull);
  16172. if (floatBitsHash (tables) != 0xd94594a6f22cb4dfull)
  16173. std::printf (" wave traveller tables: %016llx\n", (unsigned long long) floatBitsHash (tables));
  16174. // knobs (FUN_007579e0 / 757b10 / 757b60)
  16175. CHECK (speedFromKnob (0.0f, false) == 0.125f && speedFromKnob (1.0f, false) == 8.0f && speedFromKnob (0.0f, true) == 0.25f
  16176. && speedFromKnob (1.0f, true) == 4.0f && attackFromKnob (1.0f) == 500.0f && releaseFromKnob (1.0f) == 1000.0f);
  16177. // a .spln preset (§6): knob positions, curves, mutes
  16178. std::vector<std::uint8_t> spln;
  16179. auto i32 = [&spln] (std::int32_t v) { spln.insert (spln.end(), (std::uint8_t*) &v, (std::uint8_t*) &v + 4); };
  16180. auto f32 = [&spln] (float v) { spln.insert (spln.end(), (std::uint8_t*) &v, (std::uint8_t*) &v + 4); };
  16181. i32 (4000), i32 (1000005), i32 (1);
  16182. for (float v : { 0.5f, 0.2f, 0.1f, 0.3f, 0.25f, 0.75f })
  16183. f32 (v);
  16184. const auto rec = [&] (float x, float y) {
  16185. std::vector<std::uint8_t> r (60, 0);
  16186. std::memcpy (r.data() + 8, &x, 4), std::memcpy (r.data() + 12, &y, 4);
  16187. const std::int32_t one = 1;
  16188. std::memcpy (r.data() + 0x30, &one, 4);
  16189. spln.insert (spln.end(), r.begin(), r.end());
  16190. };
  16191. i32 (3), rec (0.0f, 1.0f), rec (0.5f, 0.0f), rec (1.0f, 1.0f);
  16192. i32 (2), rec (0.0f, 1.0f), rec (1.0f, 0.5f);
  16193. i32 (2), i32 (0), f32 (0.0f), i32 (1), f32 (0.5f);
  16194. auto p = defaultPatch();
  16195. CHECK (applyPreset (spln, p) && p.splineType == 1 && p.speedBeats == speedFromKnob (0.5f, false) && p.absSpeed == speedFromKnob (0.2f, true)
  16196. && p.attackMs == attackFromKnob (0.1f) && p.releaseMs == releaseFromKnob (0.3f) && p.start == 0.25f && p.end == 0.75f
  16197. && p.wave.size() == 3 && p.vol.size() == 2 && p.mutes.size() == 2 && ! p.mutes[0].on && p.waveN > 1000);
  16198. // garbage is refused without crashing
  16199. std::uint32_t r = 7;
  16200. for (int round = 0; round < 300; ++round)
  16201. {
  16202. std::vector<std::uint8_t> blob (crafted.begin(), crafted.begin() + (std::ptrdiff_t) (r % crafted.size()));
  16203. for (int k = 0; k < 8 && ! blob.empty(); ++k)
  16204. {
  16205. r = r * 1664525u + 1013904223u;
  16206. blob[(r >> 8) % blob.size()] = (std::uint8_t) (r >> 24);
  16207. }
  16208. r = r * 1664525u + 1013904223u;
  16209. if (parseState (blob, s))
  16210. for (const auto& q : s.patches)
  16211. CHECK (q.waveN >= 0 && q.waveN < tableCapacity && q.volN >= 0 && q.volN < tableCapacity);
  16212. }
  16213. CHECK (! parseState ({ 1, 2, 3 }, s) && ! lynx::waveTravellerStateUsable ({}));
  16214. // the installation's states (read at test time): the render test and the plugin database preset
  16215. if (realFlpDir.empty())
  16216. return;
  16217. auto read = [] (const std::filesystem::path& path) {
  16218. std::ifstream in (path, std::ios::binary);
  16219. return std::vector<std::uint8_t> { std::istreambuf_iterator<char> (in), {} };
  16220. };
  16221. const auto base = std::filesystem::path (realFlpDir);
  16222. if (const auto b = read (base / "System" / "Render tests" / "Wave Traveller.flp"); ! b.empty())
  16223. {
  16224. const auto project = flp::parseProject (b);
  16225. int found = 0;
  16226. for (const auto& c : project.channels)
  16227. if (c.pluginName == "Wave Traveller")
  16228. {
  16229. ++found;
  16230. CHECK (parseState (c.pluginState, s) && s.samplePath.find ("808 Snare.wav") != std::string::npos);
  16231. }
  16232. CHECK (found == 1);
  16233. const auto song = lynx::importFlp (project);
  16234. const auto it = std::find_if (song.channels.begin(), song.channels.end(), [] (const lynx::ChannelDef& c) { return c.instrument == lynx::InstrumentKind::wavetraveller; });
  16235. CHECK (it != song.channels.end() && lynx::waveTravellerStateUsable (it->pluginState));
  16236. }
  16237. if (const auto b = read (base / "System" / "Plugin databases" / "Plugin database (alphabetical)" / "Generators" / "Wave Traveller.fst"); ! b.empty())
  16238. {
  16239. const auto project = flp::parseProject (b, true);
  16240. CHECK (! project.channels.empty() && parseState (project.channels.front().pluginState, s) && s.samplePath.empty());
  16241. }
  16242. }
  16243. TEST (wave_traveller_matches_fl_engine)
  16244. {
  16245. // §8: goldens of the installation's Wave Traveller_x64.dll in our harness (16-bit stereo sample handed by LoadSample,
  16246. // 512-frame calls, tempo by ProcessEvent): FNV-1a of the output's float bits. LYNX's engine reproduces them exactly.
  16247. using namespace lynx::wavetrav;
  16248. const auto planar = wtTestSample (30000);
  16249. const auto smp = sampleFrom (wtSampleData (planar, 2));
  16250. CHECK (smp != nullptr && smp->length == 30000);
  16251. if (smp == nullptr)
  16252. return;
  16253. const auto state = wtTestState();
  16254. auto note = [] (int start, int key, float vol, float pan, float cents, int release, float initPan = 0.0f) {
  16255. RefNote n;
  16256. n.start = start, n.release = release;
  16257. n.note.key = key, n.note.finalVol = vol, n.note.finalPan = pan, n.note.initPan = initPan;
  16258. n.note.finalPitch = 100.0f * (float) key - 6000.0f + cents;
  16259. return n;
  16260. };
  16261. struct Case { std::vector<RefNote> notes; int rate; float tempo; int taps, length; std::uint64_t hash; };
  16262. const Case cases[] {
  16263. // live: every segment type, volume curve + mutes + attack / release, absolute speed, types 0 / 7 and a tiny Bezier
  16264. { { 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),
  16265. note (5000, 63, 1.0f, 0.6f, 250.0f, -1) }, 44100, 140.0f, 0, 40000, 0x9001a50747634ba6ull },
  16266. // render mode, 256 taps per side (FL's render default), with the notes' own pan
  16267. { { 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),
  16268. note (5000, 63, 1.0f, 0.6f, 250.0f, -1, -0.3f) }, 44100, 140.0f, 256, 8000, 0xedabd9728ffbb252ull },
  16269. // a 48 kHz host at tempo 97.5
  16270. { { 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 },
  16271. };
  16272. for (const auto& c : cases)
  16273. {
  16274. const auto out = renderReference (state, *smp, c.notes, c.rate, c.tempo, c.taps, c.length);
  16275. CHECK (floatBitsHash (out) == c.hash);
  16276. if (floatBitsHash (out) != c.hash)
  16277. std::printf (" wave traveller case: %016llx\n", (unsigned long long) floatBitsHash (out));
  16278. }
  16279. // the instrument (channel → engine) plays what the reference plays: velocity 1/1.28 = FL's Init.Vol 1, centred
  16280. {
  16281. lynx::ChannelDef ch;
  16282. ch.instrument = lynx::InstrumentKind::wavetraveller;
  16283. ch.pluginState = writeState (state);
  16284. auto inst = lynx::makeInstrument (lynx::InstrumentKind::wavetraveller, std::make_shared<lynx::SampleData> (wtSampleData (planar, 2)));
  16285. inst->configure (ch, 140.0);
  16286. inst->prepare (44100.0);
  16287. std::vector<float> l (20480, 0.0f), r (20480, 0.0f);
  16288. for (int pos = 0; pos < 20480; pos += 512)
  16289. {
  16290. if (pos == 0)
  16291. inst->noteOn (61, 1.0f / 1.28f);
  16292. if (pos == 10240)
  16293. inst->noteOff (61);
  16294. inst->render (l.data() + pos, r.data() + pos, 512, 1.0f, 1.0f);
  16295. }
  16296. const auto ref = renderReference (state, *smp, { note (0, 61, 1.0f, 0.0f, 0.0f, 10240) }, 44100, 140.0f, 0, 20480);
  16297. bool same = true;
  16298. for (int i = 0; i < 20480; ++i)
  16299. 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];
  16300. CHECK (same && inst->idle());
  16301. auto silent = lynx::makeInstrument (lynx::InstrumentKind::wavetraveller); // no sample: FL refuses the notes
  16302. silent->configure (ch, 140.0);
  16303. silent->prepare (44100.0);
  16304. silent->noteOn (60, 1.0f);
  16305. std::fill (l.begin(), l.end(), 0.0f);
  16306. silent->render (l.data(), r.data(), 512, 1.0f, 1.0f);
  16307. CHECK (silent->idle() && std::all_of (l.begin(), l.begin() + 512, [] (float v) { return v == 0.0f; }));
  16308. }
  16309. // the installation's render test (its 808 Snare decoded as FL decodes it), read at test time
  16310. if (realFlpDir.empty())
  16311. return;
  16312. const auto base = std::filesystem::path (realFlpDir);
  16313. std::ifstream fin (base / "System" / "Render tests" / "Wave Traveller.flp", std::ios::binary);
  16314. std::ifstream sin (base / "Patches" / "Packs" / "Drums" / "Snares" / "808 Snare.wav", std::ios::binary);
  16315. const std::vector<std::uint8_t> flpBytes { std::istreambuf_iterator<char> (fin), {} }, wav { std::istreambuf_iterator<char> (sin), {} };
  16316. lynx::oggvorbis::Decoded dec;
  16317. if (flpBytes.empty() || wav.empty() || ! lynx::oggvorbis::decode (wav.data(), wav.size(), dec) || dec.channels != 1)
  16318. return;
  16319. const auto project = flp::parseProject (flpBytes);
  16320. for (const auto& c : project.channels)
  16321. if (c.pluginName == "Wave Traveller")
  16322. {
  16323. State st;
  16324. CHECK (parseState (c.pluginState, st));
  16325. lynx::SampleData d;
  16326. d.left = dec.data[0];
  16327. const auto snare = sampleFrom (d);
  16328. CHECK (snare != nullptr && snare->length == 11002);
  16329. if (snare == nullptr)
  16330. return;
  16331. const auto out = renderReference (st, *snare, { note (0, 60, 1.0f, 0.0f, 0.0f, 44100) }, 44100, 140.0f, 0, 88200);
  16332. CHECK (floatBitsHash (out) == 0xafcb49e8c0ef918dull);
  16333. if (floatBitsHash (out) != 0xafcb49e8c0ef918dull)
  16334. std::printf (" wave traveller render test: %016llx\n", (unsigned long long) floatBitsHash (out));
  16335. }
  16336. }
  16337. TEST (fl_legacy_fx_match_fl_engines)
  16338. {
  16339. // REA_rodada165k: FL's Fruity Phase Inverter, Center, Bass Boost, 7 Band EQ and Delay (v1) through the import (state → slot →
  16340. // effect) against goldens of the installation's DLLs (rate set before the state, 512-frame calls): FNV-1a of the
  16341. // output's float bits (x87 hosts) and ten output samples (every platform, within the x87 tolerance)
  16342. struct Case { const char* name; int rate; std::vector<int> raw; std::uint64_t hash; std::array<float, 10> pts; };
  16343. const Case cases[] {
  16344. { "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 } },
  16345. { "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 } },
  16346. { "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 } },
  16347. { "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 } },
  16348. { "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 } },
  16349. { "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 } },
  16350. { "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 } },
  16351. { "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 } },
  16352. { "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 } },
  16353. { "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 } },
  16354. { "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 } },
  16355. { "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 } },
  16356. { "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 } },
  16357. { "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 } },
  16358. { "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 } },
  16359. { "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 } },
  16360. { "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 } },
  16361. { "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 } },
  16362. { "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 } },
  16363. { "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 } },
  16364. { "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 } },
  16365. { "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 } },
  16366. { "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 } },
  16367. };
  16368. for (const auto& c : cases)
  16369. {
  16370. std::vector<std::uint8_t> state (4 + 4 * c.raw.size(), 0);
  16371. const std::int32_t version = 1;
  16372. std::memcpy (state.data(), &version, 4);
  16373. std::memcpy (state.data() + 4, c.raw.data(), 4 * c.raw.size());
  16374. const auto slot = lynx::effectFromFl (c.name, state);
  16375. CHECK (slot && slot->kind != lynx::EffectKind::utility && slot->kind != lynx::EffectKind::eq
  16376. && slot->kind != lynx::EffectKind::parametricEq);
  16377. if (! slot)
  16378. continue;
  16379. auto fx = lynx::makeEffect (slot->kind);
  16380. fx->prepare ((double) c.rate);
  16381. std::vector<float> l (24000), r (24000);
  16382. std::uint32_t s = 12345;
  16383. const float amps[] { 0.3f, 1.5f, 0.05f, 3.0f, 0.8f, 0.0f, 2.0f, 0.4f };
  16384. for (int i = 0; i < 24000; ++i)
  16385. {
  16386. const float a = amps[(i / 3000) % 8];
  16387. s = s * 1664525u + 1013904223u;
  16388. l[(std::size_t) i] = (float) ((s >> 8) / 8388608.0 - 1.0) * a;
  16389. s = s * 1664525u + 1013904223u;
  16390. r[(std::size_t) i] = (float) ((s >> 8) / 8388608.0 - 1.0) * a;
  16391. }
  16392. // host blocks of 512 like the goldens' DLL calls (the Center / Bass Boost / 7 Band EQ round their state to double at
  16393. // the end of each call, as FL does, so other block sizes differ in the last bits; checked below)
  16394. const auto input = std::make_pair (l, r);
  16395. for (int i = 0; i < 24000; i += 512)
  16396. fx->process (l.data() + i, r.data() + i, std::min (512, 24000 - i), slot->params, 120.0);
  16397. {
  16398. auto other = lynx::makeEffect (slot->kind);
  16399. other->prepare ((double) c.rate);
  16400. auto l2 = input.first, r2 = input.second;
  16401. for (int i = 0; i < 24000; i += 1000)
  16402. other->process (l2.data() + i, r2.data() + i, 1000, slot->params, 120.0);
  16403. float worst = 0.0f;
  16404. for (std::size_t i = 0; i < 24000; ++i)
  16405. 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])) });
  16406. CHECK (worst <= 1.0e-4f); // (measured: ≤ 3.5e-5, the Bass Boost at 20 Hz)
  16407. }
  16408. for (int k = 0; k < 5; ++k)
  16409. {
  16410. const auto i = (std::size_t) (1500 + 4500 * k);
  16411. 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)])));
  16412. 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)])));
  16413. }
  16414. #if defined(__x86_64__) || defined(__i386__)
  16415. std::uint64_t h = 0xcbf29ce484222325ull;
  16416. for (int i = 0; i < 24000; ++i)
  16417. for (float v : { l[(std::size_t) i], r[(std::size_t) i] })
  16418. {
  16419. std::uint32_t u;
  16420. std::memcpy (&u, &v, 4);
  16421. h = (h ^ u) * 0x100000001b3ull;
  16422. }
  16423. CHECK (h == c.hash);
  16424. #endif
  16425. }
  16426. CHECK (lynx::formatEffectParam (lynx::EffectKind::flPhaseInverter, 0, 0.3f, 120.0) == "None");
  16427. CHECK (lynx::formatEffectParam (lynx::EffectKind::flPhaseInverter, 0, 0.5f, 120.0) == "Left");
  16428. CHECK (lynx::formatEffectParam (lynx::EffectKind::flPhaseInverter, 0, 1.0f, 120.0) == "Right");
  16429. CHECK (lynx::formatEffectParam (lynx::EffectKind::flBassBoost, 0, 0.5f, 120.0) == "60 Hz");
  16430. CHECK (lynx::formatEffectParam (lynx::EffectKind::fl7BandEq, 3, 1.0f, 120.0) == "+18.0 dB");
  16431. CHECK (lynx::formatEffectParam (lynx::EffectKind::fl7BandEq, 3, 0.5f, 120.0) == "+0.0 dB");
  16432. CHECK (lynx::formatEffectParam (lynx::EffectKind::flDelay1, 3, 0.0f, 120.0) == "Host");
  16433. CHECK (lynx::formatEffectParam (lynx::EffectKind::flDelay1, 3, 1.0f, 120.0) == "280 BPM");
  16434. CHECK (lynx::formatEffectParam (lynx::EffectKind::flDelay1, 4, 2.0f / 15.0f, 120.0) == "3/16");
  16435. CHECK (lynx::formatEffectParam (lynx::EffectKind::flDelay1, 5, 1.0f, 120.0) == "Ping-pong");
  16436. CHECK (lynx::effectFromFl ("Fruity Delay", { 1, 0, 0, 0 }) == std::nullopt
  16437. || lynx::effectFromFl ("Fruity Delay", { 1, 0, 0, 0 })->kind != lynx::EffectKind::flDelay1);
  16438. // the default Phase Inverter of FL inverts the right side only (LYNX's old import inverted both)
  16439. CHECK (lynx::fllegacy::phaseInverterMode (1024) == 2 && lynx::fllegacy::phaseInverterMode (341) == 0
  16440. && lynx::fllegacy::phaseInverterMode (342) == 1 && lynx::fllegacy::phaseInverterMode (683) == 2);
  16441. }
  16442. TEST (fl_frequency_shifter_matches_fl_engine)
  16443. {
  16444. // REA_rodada154 §4: FL's Frequency Shifter (import → slot → effect) against goldens of the installation's
  16445. // Engine_x64.dll driven by its front end (rate before the state, 1536 samples of silence first, transport stopped, 120 BPM): FNV-1a
  16446. // of the output
  16447. struct Case { std::array<int, 10> raw; double rate; std::uint64_t hash; };
  16448. const Case cases[] { { { 10000, 0, -60480, 0, 10000, 10000, 5000, 10000, 1, 5000 }, 44100.0, 0x462af5f13683c90eull },
  16449. { { 7000, 2, 40000, 3000, 10000, 10000, 8000, 6000, 0, 2000 }, 44100.0, 0xcd597b95bf1bbb1aull },
  16450. { { 6000, 1, -30000, 6000, 3000, 7000, 3000, 4000, 0, 5000 }, 48000.0, 0x5592232115cb3acull },
  16451. { { 10000, 0, 51542, 2000, 8000, 10000, 5000, 10000, 0, 5000 }, 48000.0, 0x63d3051ba14bd00full } };
  16452. for (const auto& c : cases)
  16453. {
  16454. std::vector<std::uint8_t> state (4 + 40 + 8, 0);
  16455. const std::int32_t version = 4;
  16456. std::memcpy (state.data(), &version, 4);
  16457. std::memcpy (state.data() + 4, c.raw.data(), 40);
  16458. const auto slot = lynx::effectFromFl ("Frequency Shifter", state);
  16459. CHECK (slot && slot->kind == lynx::EffectKind::flFrequencyShifter);
  16460. if (! slot)
  16461. continue;
  16462. auto fx = lynx::makeEffect (slot->kind);
  16463. fx->prepare (c.rate);
  16464. std::vector<float> l (24000), r (24000);
  16465. std::uint32_t s = 12345;
  16466. const float amps[] { 0.3f, 1.5f, 0.05f, 3.0f, 0.8f, 0.0f, 2.0f, 0.4f };
  16467. for (int i = 0; i < 24000; ++i)
  16468. {
  16469. const float a = amps[(i / 3000) % 8];
  16470. s = s * 1664525u + 1013904223u;
  16471. l[(std::size_t) i] = (float) ((s >> 8) / 8388608.0 - 1.0) * a;
  16472. s = s * 1664525u + 1013904223u;
  16473. r[(std::size_t) i] = (float) ((s >> 8) / 8388608.0 - 1.0) * a;
  16474. }
  16475. for (int i = 0; i < 24000; i += 800)
  16476. fx->process (l.data() + i, r.data() + i, 800, slot->params, 120.0);
  16477. std::uint64_t h = 0xcbf29ce484222325ull;
  16478. for (int i = 0; i < 24000; ++i)
  16479. for (float v : { l[(std::size_t) i], r[(std::size_t) i] })
  16480. {
  16481. std::uint32_t u;
  16482. std::memcpy (&u, &v, 4);
  16483. h = (h ^ u) * 0x100000001b3ull;
  16484. }
  16485. CHECK (h == c.hash);
  16486. }
  16487. // the engine's tanhf against points of the library's
  16488. CHECK (lynx::fsfl::tanhf_ (0.0f) == 0.0f && lynx::fsfl::tanhf_ (20.0f) == 1.0f && lynx::fsfl::tanhf_ (-20.0f) == -1.0f);
  16489. CHECK (std::fabs (lynx::fsfl::tanhf_ (0.5f) - 0.462117165f) < 1e-7f && std::fabs (lynx::fsfl::tanhf_ (2.0f) - 0.964027584f) < 1e-7f);
  16490. CHECK (lynx::formatEffectParam (lynx::EffectKind::flFrequencyShifter, 1, 1.0f, 120.0) == "Ring modulator");
  16491. }
  16492. TEST (fl_low_lifter_matches_fl_engine)
  16493. {
  16494. // REA_rodada154 §5: FL's Low Lifter (import → slot → effect, 512-frame calls) against goldens of the installation's
  16495. // engine (ildsp_x64.dll's Faust program driven by Low Lifter_x64.dll, rate before the state): FNV-1a of the output
  16496. struct Case { std::array<int, 15> raw; double rate; std::uint64_t hash; };
  16497. const Case cases[] { { { 1, 10000, 4999, 7210, 1, 0, 0, 0, 5000, 10000, 7750, 0, 0, 1, 1 }, 44100.0, 0xcbb7eae8f31ca6d9ull },
  16498. { { 1, 7506, 5000, 3277, 1, 0, 6757, 5053, 9608, 10000, 7778, 5281, 1, 1, 1 }, 44100.0, 0xdb2d3712d4fb16c5ull },
  16499. { { 1, 5095, 6737, 5109, 1, 1, 541, 1479, 5451, 8197, 690, 6833, 1, 1, 1 }, 48000.0, 0x273c262202984447ull } };
  16500. for (const auto& c : cases)
  16501. {
  16502. std::vector<std::uint8_t> state (4 + 60 + 18, 0);
  16503. const std::int32_t version = 6;
  16504. std::memcpy (state.data(), &version, 4);
  16505. std::memcpy (state.data() + 4, c.raw.data(), 60);
  16506. const auto slot = lynx::effectFromFl ("Low Lifter", state);
  16507. CHECK (slot && slot->kind == lynx::EffectKind::flLowLifter);
  16508. if (! slot)
  16509. continue;
  16510. auto fx = lynx::makeEffect (slot->kind);
  16511. fx->prepare (c.rate);
  16512. std::vector<float> l (24000), r (24000);
  16513. std::uint32_t s = 12345;
  16514. const float amps[] { 0.3f, 1.5f, 0.05f, 3.0f, 0.8f, 0.0f, 2.0f, 0.4f };
  16515. for (int i = 0; i < 24000; ++i)
  16516. {
  16517. const float a = amps[(i / 3000) % 8];
  16518. s = s * 1664525u + 1013904223u;
  16519. l[(std::size_t) i] = (float) ((s >> 8) / 8388608.0 - 1.0) * a;
  16520. s = s * 1664525u + 1013904223u;
  16521. r[(std::size_t) i] = (float) ((s >> 8) / 8388608.0 - 1.0) * a;
  16522. }
  16523. for (int i = 0; i < 24000; i += 512)
  16524. fx->process (l.data() + i, r.data() + i, std::min (512, 24000 - i), slot->params, 120.0);
  16525. std::uint64_t h = 0xcbf29ce484222325ull;
  16526. for (int i = 0; i < 24000; ++i)
  16527. for (float v : { l[(std::size_t) i], r[(std::size_t) i] })
  16528. {
  16529. std::uint32_t u;
  16530. std::memcpy (&u, &v, 4);
  16531. h = (h ^ u) * 0x100000001b3ull;
  16532. }
  16533. CHECK (h == c.hash);
  16534. }
  16535. CHECK (lynx::formatEffectParam (lynx::EffectKind::flLowLifter, 10, 0.775f, 120.0) == "80 Hz");
  16536. CHECK (lynx::formatEffectParam (lynx::EffectKind::flLowLifter, 3, 0.721f, 120.0) == "-6.0 dB");
  16537. }
  16538. #include "Rea153Tests.inc"
  16539. #include "Logger181bTests.inc"
  16540. #include "Render155Tests.inc"
  16541. #include "Tune157Tests.inc"
  16542. #include "Tune165Tests.inc"
  16543. #include "Tune190liTests.inc" // round 190li: the vocal pitch toolkit's CPU pass
  16544. #include "HwOut156Tests.inc"
  16545. #include "SoundFont158Tests.inc"
  16546. #include "Mp3157Tests.inc"
  16547. #include "Quality160Tests.inc"
  16548. #include "CrashReport159Tests.inc"
  16549. #include "Sampler162Tests.inc"
  16550. #include "Workflow163cTests.inc"
  16551. #include "ClipPitch164Tests.inc"
  16552. #include "Misc164mTests.inc"
  16553. #include "MidiSync165Tests.inc"
  16554. #include "Theme162Tests.inc"
  16555. #include "PluginPresets162Tests.inc"
  16556. #include "Look162Tests.inc"
  16557. #include "EnvController162Tests.inc"
  16558. #include "Browser162Tests.inc"
  16559. #include "BrowserView163Tests.inc"
  16560. #include "Transport162Tests.inc"
  16561. #include "Workflow163Tests.inc"
  16562. #include "Rec163Tests.inc"
  16563. #include "Ui163Tests.inc"
  16564. #include "HwInsert164Tests.inc"
  16565. #include "Layer164Tests.inc"
  16566. #include "Patcher164vTests.inc"
  16567. #include "Vfx164Tests.inc"
  16568. #include "VfxScript165Tests.inc"
  16569. #include "Perf167Tests.inc"
  16570. #include "Misc164Tests.inc"
  16571. #include "Store165Tests.inc" // (round 165n; kept off the end of the list, where every round appends)
  16572. #include "Rack165Tests.inc" // (round 165f-rack; likewise)
  16573. #include "Visual164Tests.inc"
  16574. #include "Visual165Tests.inc"
  16575. #include "Dance190baTests.inc"
  16576. #include "Patcher164fTests.inc"
  16577. #include "Markers165Tests.inc"
  16578. #include "PerfMode165Tests.inc"
  16579. #include "Rea167gTests.inc"
  16580. #include "Rea167hTests.inc"
  16581. #include "Rea167jTests.inc"
  16582. #include "Rea167kTests.inc"
  16583. #include "Rea167lTests.inc"
  16584. #include "Patch165Tests.inc"
  16585. #include "FlPlugins165Tests.inc"
  16586. #include "FlScratch165Tests.inc"
  16587. #include "FlTrans165Tests.inc"
  16588. #include "VideoPlayer165Tests.inc"
  16589. #include "Video174dTests.inc"
  16590. #include "Dash165Tests.inc"
  16591. #include "LinuxMouse165Tests.inc"
  16592. #include "Fl165Tests.inc"
  16593. #include "Issues167oTests.inc"
  16594. #include "Perf165Tests.inc"
  16595. #include "Issue12Tests.inc"
  16596. #include "Issue12cTests.inc"
  16597. #include "Audit165zbTests.inc"
  16598. #include "FlGran165Tests.inc"
  16599. #include "GranDrop165Tests.inc"
  16600. #include "Kepler165kTests.inc"
  16601. #include "KeplerExo167dTests.inc"
  16602. #include "MixerLatency165Tests.inc"
  16603. #include "Rea168jTests.inc"
  16604. #include "Audit165Tests.inc"
  16605. #include "Vfx165cTests.inc"
  16606. #include "HarmorBeta165hTests.inc"
  16607. #include "Crash13Tests.inc"
  16608. #include "Issue11bTests.inc"
  16609. #include "Issue11cTests.inc"
  16610. #include "FlNewt165Tests.inc"
  16611. #include "Audit165zATests.inc"
  16612. #include "Audit165zA2Tests.inc"
  16613. #include "Eq166fTests.inc"
  16614. #include "Feat166dTests.inc"
  16615. #include "Round166eTests.inc"
  16616. #include "Issue15Tests.inc"
  16617. #include "Issue14bugsTests.inc"
  16618. #include "Mute190yTests.inc"
  16619. #include "Left167Tests.inc"
  16620. #include "Wheel173dTests.inc"
  16621. #include "PoiZoneWheels173dTests.inc"
  16622. #include "Tune166cTests.inc"
  16623. #include "TuneBreath190bbTests.inc"
  16624. #include "Update166bTests.inc"
  16625. #include "Emphasis167fTests.inc"
  16626. #include "Dvs167Tests.inc"
  16627. #include "EdisonScript167Tests.inc"
  16628. #include "PianoRollScript190Tests.inc"
  16629. #include "Deverb167Tests.inc"
  16630. #include "Score167Tests.inc"
  16631. #include "Ntt167Tests.inc"
  16632. #include "MidiOut167iTests.inc"
  16633. #include "Transporter167fTests.inc"
  16634. #include "Polish167pTests.inc"
  16635. #include "Menus168zTests.inc"
  16636. #include "Session168aTests.inc"
  16637. #include "Live169bTests.inc"
  16638. #include "Live169jTests.inc"
  16639. #include "Live190ahTests.inc"
  16640. #include "Remote190aiTests.inc"
  16641. #include "StemSplitTests.inc"
  16642. #include "AudioToMidi189bTests.inc"
  16643. #include "Poly2Midi190ljTests.inc"
  16644. #include "PolyEdit190lkTests.inc"
  16645. #include "PolyEdit190loTests.inc"
  16646. #include "PolyEdit190loUiTests.inc" // (round 190lo, UI half)
  16647. #include "Zoom167tTests.inc"
  16648. #include "PianoKeys190jTests.inc"
  16649. #include "Proll168gTests.inc"
  16650. #include "Rea168eTests.inc"
  16651. #include "Design168nTests.inc"
  16652. #include "Design168qTests.inc"
  16653. #include "FirstRun167vTests.inc"
  16654. #include "LoopStarter168bTests.inc"
  16655. #include "Rec168kTests.inc"
  16656. #include "FlPaths124Tests.inc"
  16657. #include "Auto125Tests.inc"
  16658. #include "Rec170dTests.inc"
  16659. #include "Rec177dTests.inc"
  16660. #include "Pain177dTests.inc"
  16661. #include "Mixer177dTests.inc"
  16662. #include "Edit177dTests.inc"
  16663. #include "Patterns177dTests.inc"
  16664. #include "Issue32Tests.inc"
  16665. #include "Keys117Tests.inc"
  16666. #include "Pedal187aTests.inc"
  16667. #include "Sc109Tests.inc"
  16668. #include "WrapperMidi181cTests.inc"
  16669. #include "Keys96Tests.inc"
  16670. #include "Mono168lTests.inc"
  16671. #include "MixPre167qTests.inc"
  16672. #include "Design168rTests.inc"
  16673. #include "Playlist168fTests.inc"
  16674. #include "RecNoFold126Tests.inc"
  16675. #include "LoopWrap167qTests.inc"
  16676. #include "Sel168tTests.inc"
  16677. #include "Gap169kTests.inc"
  16678. #include "Gap169kPickerTests.inc"
  16679. #include "Gap169kAlignTests.inc"
  16680. #include "FlQ170iTests.inc"
  16681. #include "FlQ170i2Tests.inc"
  16682. #include "FlQ170i3Tests.inc"
  16683. #include "Speech168bTests.inc"
  16684. #include "Design168pTests.inc"
  16685. #include "Typing168tTests.inc"
  16686. #include "TypingPlace190bmTests.inc"
  16687. #include "GrossBeat168mTests.inc"
  16688. #include "Maximus168xTests.inc"
  16689. #include "Rec168xTests.inc"
  16690. #include "Design168oTests.inc"
  16691. #include "Pr44BrowserListTests.inc"
  16692. #include "Mixer168hTests.inc"
  16693. #include "Mute53fTests.inc"
  16694. #include "Issue32cTests.inc"
  16695. #include "Drumaxx169eTests.inc"
  16696. #include "PatternBlocks170fTests.inc"
  16697. #include "Mixfx170bTests.inc"
  16698. #include "Typeahead170bTests.inc"
  16699. #include "PlugQ170cTests.inc"
  16700. #include "Toolbar170eTests.inc"
  16701. #include "Rack170aTests.inc"
  16702. #include "Inst169hTests.inc"
  16703. #include "Crash169gTests.inc"
  16704. #include "Wfqa170hTests.inc"
  16705. #include "Toxic169hTests.inc"
  16706. #include "Gen169lTests.inc"
  16707. #include "Picker171cTests.inc"
  16708. #include "Picker175bTests.inc"
  16709. #include "Gen169l2Tests.inc"
  16710. #include "Patcher171bTests.inc"
  16711. #include "Wfqa170h2Tests.inc"
  16712. #include "Fader171cTests.inc"
  16713. #include "Tb169g2Tests.inc"
  16714. #include "Issues171aTests.inc"
  16715. #include "CtlAudit170i3Tests.inc"
  16716. #include "CtlWake173dTests.inc"
  16717. #include "Issue32dTests.inc"
  16718. #include "Issue81Tests.inc"
  16719. #include "Issue85Tests.inc"
  16720. #include "FlpExport171eTests.inc"
  16721. #include "Tp172cTests.inc"
  16722. #include "Layout172aTests.inc"
  16723. #include "Issues173aTests.inc"
  16724. #include "FxAudit173dTests.inc"
  16725. #include "Audit173d3Tests.inc"
  16726. #include "Vocodex173d3Tests.inc"
  16727. #include "Fluid174eTests.inc"
  16728. #include "Comp177Tests.inc"
  16729. #include "AlsImport175aTests.inc"
  16730. #include "Als190kaTests.inc" // round als190ka (issue #193): .als automation laws, sends, device on / off, Main devices, video clips
  16731. #include "AlsColours190igTests.inc" // round 190ig (CL-6): Live's colour table for .als imports in the Clip-launch layout
  16732. #include "FlmImport182cTests.inc"
  16733. #include "FlmEngines190uTests.inc"
  16734. #include "FlmNotes190abTests.inc"
  16735. #include "FlmGmsynth190abTests.inc"
  16736. #include "PocketSuperSaw190byTests.inc" // round 190by
  16737. #include "FlmImport187dTests.inc"
  16738. #include "FlmImport188fTests.inc"
  16739. #include "FlmImport188hTests.inc"
  16740. #include "FlmImport189hTests.inc"
  16741. #include "FlmImport189hATests.inc"
  16742. #include "FlmImport189hBTests.inc"
  16743. #include "BandImport190icTests.inc"
  16744. #include "BandImport190jmTests.inc" // round 190jm: issue #184 project (GarageBand for iOS 2.3+ events, Sampler)
  16745. #include "Band184_190mxTests.inc" // round 190mx: issue #184 (GarageBand for iOS 2.3+ signature)
  16746. #include "Issue198_190mxTests.inc" // round 190mx: GitHub #198 follow-up (plugin-window knobs recorded as automation)
  16747. #include "Blx190jrTests.inc"
  16748. #include "Stems190jrTests.inc"
  16749. #include "SimpleGb190keTests.inc" // round 190ke: GarageBand / BandLab ideas in Simple (fade-out, Controls, tuner, factory tags)
  16750. #include "FlmImport189hCTests.inc"
  16751. #include "FlmImport190bsTests.inc"
  16752. #include "PocketFx190byTests.inc" // round 190by
  16753. #include "FlmImport190deTests.inc" // round 190de
  16754. #include "PocketEq190dwTests.inc" // round 190dw
  16755. #include "PocketReverb190dwTests.inc" // round 190dw
  16756. #include "FlmEffects190dwTests.inc" // round 190dw: the .flm effect laws against FLM's renders
  16757. #include "FlmImport190dwInstTests.inc" // round 190dw item 4
  16758. #include "FlmImport190feTests.inc" // round 190fe: cut groups, 3x Osc lanes, effect Mix, rack parts
  16759. #include "FlmImport190fnTests.inc" // round 190fn: FLM resources folders (O1), FL Studio Mobile Rack in an .flp, META, short SEND
  16760. #include "Bridge176aTests.inc"
  16761. #include "Issues178aTests.inc"
  16762. #include "Mixer177aTests.inc"
  16763. #include "ClipLauncher181aTests.inc"
  16764. #include "Session190bvTests.inc"
  16765. #include "DetailView183aTests.inc"
  16766. #include "Als181cTests.inc"
  16767. #include "DetailView190hrTests.inc"
  16768. #include "Groove190fmTests.inc"
  16769. #include "AlsDevices190bqTests.inc"
  16770. #include "AlsDevices190cfTests.inc"
  16771. #include "AlsDevices190edTests.inc"
  16772. #include "LiveDev190kkTests.inc" // round 190kk: Erosion → Corrode, Vinyl → Platter, Corpus → Soundboard, Looper, MIDI Effect Racks (uses als190cf::)
  16773. #include "Meld190koTests.inc" // round 190ko: Meld → Braid, Spectral Time → Timeprism, Spectral Resonator → Harmonia (uses als190cf::)
  16774. #include "AlsDevices190ekTests.inc"
  16775. #include "Tetra190hsTests.inc" // round 190hs
  16776. #include "LynxModel190niTests.inc" // round 190ni
  16777. #include "LynxModel190nlTests.inc" // round 190nl
  16778. #include "LynxModel190nmTests.inc" // round 190nm
  16779. #include "LynxModel190nnTests.inc" // round 190nn
  16780. #include "Tetra190ixTests.inc" // round 190ix
  16781. #include "Helix190jaTests.inc" // round 190ja
  16782. #include "Helix190miTests.inc" // round 190mi
  16783. #include "Helix190mlTests.inc" // round 190ml
  16784. #include "Helix190mtTests.inc" // round 190mt
  16785. #include "Helix190ocTests.inc" // round 190oc: Helix round 4 (grain / spectral unison, reverse grains)
  16786. #include "AlsDevices190fmTests.inc"
  16787. #include "Merge180cTests.inc"
  16788. #include "Slicer180cTests.inc"
  16789. #include "Slicer190fTests.inc"
  16790. #include "Issue160Tests.inc"
  16791. #include "Issue159Tests.inc"
  16792. #include "Issue165Tests.inc"
  16793. #include "Bugs190gyTests.inc"
  16794. #include "Fpc190mTests.inc"
  16795. #include "Fpc190qTests.inc"
  16796. #include "Fpc190zTests.inc"
  16797. #include "PluginExport180bTests.inc"
  16798. #include "FlExport180dTests.inc"
  16799. #include "FlpMute182eTests.inc"
  16800. #include "FlpPeak182eTests.inc"
  16801. #include "FlpPatcherSc182eTests.inc"
  16802. #include "FlFades180dTests.inc"
  16803. #include "Stretch180dTests.inc"
  16804. #include "Timing180dTests.inc"
  16805. #include "FlpPdc182eTests.inc"
  16806. #include "Pdc188eTests.inc"
  16807. #include "Mixer177a2Tests.inc"
  16808. #include "Mixer177a3Tests.inc"
  16809. #include "Mixer186cTests.inc"
  16810. #include "Mixer177a4Tests.inc"
  16811. #include "Sc177bTests.inc"
  16812. #include "Auto177bTests.inc"
  16813. #include "Lanes177bTests.inc"
  16814. #include "Lfo177bTests.inc"
  16815. #include "Curves190bwTests.inc"
  16816. #include "Mod177bTests.inc"
  16817. #include "Mod190oxTests.inc" // round 190ox: modulators stage 2
  16818. #include "UiFont179aTests.inc"
  16819. #include "Multilink177bTests.inc"
  16820. #include "Issue97Tests.inc"
  16821. #include "SimpleTracks184aTests.inc"
  16822. #include "SimpleChooser184aTests.inc"
  16823. #include "FlStateWrite184aTests.inc"
  16824. #include "MasterLooks189kTests.inc"
  16825. #include "LayoutStyle184aTests.inc"
  16826. #include "Rack116Tests.inc"
  16827. #include "Issue116DetachedTests.inc"
  16828. #include "Issue282TrialMarkTests.inc" // round 190qs: issue #282, FL's trial mark and the FL generators' round trip
  16829. #include "Trial187bTests.inc"
  16830. #include "ProjectKey184aTests.inc"
  16831. #include "KeyGuess184aTests.inc"
  16832. #include "Render186bTests.inc"
  16833. #include "Playlist189eTests.inc"
  16834. #include "Playlist189gTests.inc"
  16835. #include "Playlist189iTests.inc"
  16836. #include "Playlist189lTests.inc"
  16837. #include "Playlist189nTests.inc"
  16838. #include "PlaylistKeys190agTests.inc"
  16839. #include "Rack183cTests.inc"
  16840. #include "DrumRack190ecTests.inc"
  16841. #include "DrumRackSlice190ecTests.inc"
  16842. #include "DrumRack190elTests.inc"
  16843. #include "Gamepad184aTests.inc"
  16844. #include "ExportBundle184aTests.inc"
  16845. #include "MultiOut188dTests.inc"
  16846. #include "FxConn189jTests.inc"
  16847. #include "Strike189dTests.inc"
  16848. #include "NoteChance187cTests.inc"
  16849. #include "MidiTools190oTests.inc"
  16850. #include "MidiTools190sTests.inc"
  16851. #include "MidiTools190aaTests.inc"
  16852. #include "MidiTools190aa2Tests.inc"
  16853. #include "MidiGenerate190fmTests.inc"
  16854. #include "Notes188gTests.inc"
  16855. #include "Layer190bTests.inc"
  16856. #include "PlugLevel190adTests.inc"
  16857. #include "Undo190bTests.inc"
  16858. #include "Undo190mzTests.inc"
  16859. #include "TrackMeters190wTests.inc"
  16860. #include "Locator190wTests.inc"
  16861. #include "Bugs190cTests.inc"
  16862. #include "Parity190vTests.inc"
  16863. #include "Parity190arFetTests.inc"
  16864. #include "Parity190arApexTests.inc"
  16865. #include "Slicex190lTests.inc"
  16866. #include "Parity190alTests.inc"
  16867. #include "Slicex190hTests.inc"
  16868. #include "AudioX190aqTests.inc"
  16869. #include "Parity190arTranslateTests.inc"
  16870. #include "Surge190xTests.inc"
  16871. #include "Issue114Tests.inc"
  16872. #include "Slicex190rTests.inc"
  16873. #include "Slicex190acTests.inc"
  16874. #include "Slicex190ajTests.inc"
  16875. #include "Slicex190hgTests.inc"
  16876. #include "Slicex190hzTests.inc"
  16877. #include "ScaleSnap190anTests.inc"
  16878. #include "TruePeak190amTests.inc"
  16879. #include "Loudness190amTests.inc"
  16880. #include "UsedOnly190amTests.inc"
  16881. #include "Extras190amTests.inc"
  16882. #include "Extras190qkTests.inc" // round 190qk: the extras on by default, the user-set list
  16883. #include "Workflow190iuTests.inc" // round 190iu: issues #186 and #191
  16884. #include "Reload190aoTests.inc"
  16885. #include "Pin190aoTests.inc"
  16886. #include "Velocity190aoTests.inc"
  16887. #include "Audition190aoTests.inc"
  16888. #include "Versions190apTests.inc"
  16889. #include "core/PluginHold.h"
  16890. #include "PluginsOff190apTests.inc"
  16891. #include "ExportSections190apTests.inc"
  16892. #include "DirectWave190ajTests.inc"
  16893. #include "core/DirectWaveState.h"
  16894. #include "DirectWave190hzTests.inc"
  16895. #include "Similar190asTests.inc"
  16896. #include "Controller190awTests.inc"
  16897. #include "ControllerProfiles190awTests.inc"
  16898. #include "Bugs190axInsert6Tests.inc"
  16899. #include "Snap190axTests.inc"
  16900. #include "PluginState190azTests.inc"
  16901. #include "Parity190arTests.inc"
  16902. #include "Maps190caTests.inc"
  16903. #include "Mpe190bbTests.inc"
  16904. #include "Mpe190bbLiveTests.inc"
  16905. #include "Mpe190hyTests.inc"
  16906. #include "Mpe190hyAlsTests.inc"
  16907. #include "PluginDb190baTests.inc"
  16908. #include "PerfWin190gdTests.inc"
  16909. #include "Ram190gnTests.inc"
  16910. #include "Startup190goTests.inc"
  16911. #include "Startup190hdTests.inc"
  16912. #include "Freeze190ayTests.inc"
  16913. #include "LiveFreeze190ffTests.inc"
  16914. #include "QaFix190bjTests.inc"
  16915. #include "Fl190bfTests.inc"
  16916. #include "ShotScale190biTests.inc"
  16917. #include "QaFix190bjATests.inc"
  16918. #include "Demo190bmTests.inc"
  16919. #include "Nebula190caTests.inc"
  16920. #include "Settings190bxTests.inc"
  16921. #include "Link190btTests.inc"
  16922. #include "LiveBrowser190ceTests.inc"
  16923. #include "LoopBrowser190kfTests.inc" // round gaps190kf: the Loop Browser's keywords and filters
  16924. #include "Browser190izTests.inc" // round 190iz: Browser tags, FL tabs, Live sidebar, send to selected, 5-s preview
  16925. #include "SimpleFx190buTests.inc"
  16926. #include "SimpleGate190dgTests.inc"
  16927. #include "SimpleNext190dsTests.inc"
  16928. #include "FirstSongTour190dvTests.inc"
  16929. #include "Onboarding190npTests.inc" // round 190np: first-run onboarding (core/Onboarding.h)
  16930. #include "Offline190nxTests.inc" // round 190nx: offline mode (core/Offline.h)
  16931. #include "SimplePads190foTests.inc"
  16932. #include "Rollup027Tests.inc"
  16933. #include "Edison190cdTests.inc"
  16934. #include "Fled190eeTests.inc"
  16935. #include "Warp190cgTests.inc"
  16936. #include "LiveScr190drTests.inc"
  16937. #include "LiveWf190efMapTests.inc"
  16938. #include "LiveWf190efDrawTests.inc"
  16939. #include "LiveWf190efScenesTests.inc"
  16940. #include "LiveWf190emTests.inc"
  16941. #include "BandlabExtras190chTests.inc"
  16942. #include "DanceFix190cjTests.inc"
  16943. #include "Reverb190bzTests.inc"
  16944. #include "Dyn190dmTests.inc"
  16945. #include "ParityDyn190fxTests.inc"
  16946. #include "Echo190dlTests.inc"
  16947. #include "Vqe190dpTests.inc"
  16948. #include "Ember190fjTests.inc"
  16949. #include "ApexFix190coTests.inc"
  16950. #include "Link190csTests.inc"
  16951. #include "TrackLinkTests.inc"
  16952. #include "TrackAuto190gzTests.inc"
  16953. #include "Issues190htTests.inc"
  16954. #include "Warp190dbTests.inc"
  16955. #include "WarpModes190fmTests.inc" // round 190fm
  16956. #include "WarpLive190gxTests.inc" // round 190gx
  16957. #include "ClipEnv190giTests.inc" // round 190gi
  16958. #include "LiveFacts190gxTests.inc" // round 190gx
  16959. #include "BeatsLaw190gxTests.inc" // round 190gx
  16960. #include "FlCtl190dfTests.inc"
  16961. #include "FlFix190eyTests.inc"
  16962. #include "FlPl190dxTests.inc"
  16963. #include "FlClip190dzTests.inc"
  16964. #include "Iss152Tests.inc"
  16965. #include "Win190evTests.inc"
  16966. #include "U190heTests.inc"
  16967. #include "U190hxTests.inc"
  16968. #include "U190klTests.inc"
  16969. #include "D190jyTests.inc"
  16970. #include "Mod190doTests.inc"
  16971. #include "Parity190fyTests.inc"
  16972. #include "Parity190hoTests.inc"
  16973. #include "Parity190iqTests.inc"
  16974. #include "Parity190itTests.inc"
  16975. #include "Parity190lnVrTests.inc" // round 190ln: Foil Plate on ValhallaPlate's own EQ / bass-decay laws (after Parity190it: its gold helpers)
  16976. #include "Parity190lsTests.inc" // round 190ls: Atrium on a dense late tank for the ValhallaRoom role (after Parity190it: its gold helpers)
  16977. #include "Parity190lyTests.inc" // round 190ly: Atrium's dense tank only for a translated ValhallaRoom; Valhalla stand-ins part 2
  16978. #include "Parity190mbTests.inc" // round 190mb: Valhalla stand-ins part 3 (Supermassive tone, FutureVerb, SpaceModulator modes; after Parity190ly: its helpers)
  16979. #include "Parity190mjTests.inc" // round 190mj: Orbit = ValhallaSpaceModulator's own voices (hidden voiceLaw; after Parity190mb)
  16980. #include "Parity190mjHzTests.inc" // round 190mj: FutureVerb → Horizon part 2 (echo modes, Room / Chamber over Size; after Parity190mb)
  16981. #include "Parity190jhTests.inc" // round 190jh: Facet's automatic threshold, Pro-Q 4 dynamic bands, Waves L2 / SuperTap / SSLComp / CLA-76
  16982. #include "Parity190jsTests.inc" // round 190js: the Waves reverbs on Reeverb 2, Hush's Single Vocal revision, Facet's auto-threshold refit
  16983. #include "Parity190ktTests.inc" // round 190kt: REA of the Waves SSLComp DSP, Crest's console law, SSLComp on Crest
  16984. #include "Parity190lbTests.inc" // round 190lb: black-box study (no decryption); SSL 4K G Pink EQ + output ceiling
  16985. #include "Harrison190leTests.inc" // round 190le: Ridge Strip's 32C voicing (Harrison 32Classic EQ law)
  16986. #include "Parity190kyTests.inc" // round 190ky: Crest's FET law (REA of the Waves CLA-76 DSP), CLA-76 on it
  16987. #include "Parity190lgQ4Tests.inc" // round 190lg: Facet's automatic threshold on Pro-Q 4's structure (revision 1; after Parity190jh / 190js)
  16988. #include "Parity190kwTests.inc" // round 190kw: static REA — Crest's Pro-C 3 V1 / V2 style laws (hidden revision)
  16989. #include "Parity190lgVersTests.inc" // round 190lg: Crest Allround = Pro-C 3 Versatile (revision 1; after Parity190kw: its helpers)
  16990. #include "Parity190lpCrestTests.inc" // round 190lp: Crest Smooth / Op-El = Pro-C 3's own graphs (revision 1; after Parity190kw: its helpers)
  16991. #include "Parity190mbCrestTests.inc" // round 190mb: Crest Upward / Vari-mu = Pro-C 3's own Upward graph and Vari-Mu tube node (revision 1; after Parity190kw: its helpers)
  16992. #include "Parity190mjCrestTests.inc" // round 190mj: Crest Two-way = Pro-C 3's own TTM band processors (revision 1; after Parity190kw: its helpers)
  16993. #include "Parity190lgMbTests.inc" // round 190lg: Prism revision 1 on Pro-MB's own band compander graph (after DynamicsTests108: its helpers)
  16994. #include "Parity190lpLgTests.inc" // round 190lp: Apex Limiter / Sentry Gate revision 1, black-box refit on Pro-L 2 / Pro-G (after DynamicsTests108: its helpers)
  16995. #include "Parity190kwTl3Tests.inc" // round 190kw: Timeless 3 states on Echoform (the hidden line law)
  16996. #include "Parity190kwVvTests.inc" // round 190kw: Epoch Verb's level on ValhallaVintageVerb's loop law (after Parity190it: its gold helpers)
  16997. #include "Parity190kwSatTests.inc" // round 190kw: Saturn 2 -> Ember, the styles' own laws (hidden revision)
  16998. #include "Parity190lfTests.inc" // round 190lf: the Limiter's L2 law (REA of the Waves L2 DSP), L2 on it; PuigTec EQP1A on Facet EQ
  16999. #include "Parity190lfDynTests.inc" // round 190lf (dyn): Glow Leveler's cell law (REA of the Waves CLA-2A), CLA-2A on it
  17000. #include "Parity190lfVerbTests.inc" // round 190lf (verb): H-Delay's measured loop (Echoform hybrid law 1), TrueVerb's direct sound
  17001. #include "Parity190lfNs1Tests.inc" // round 190lf (ns1): Quell's zero law (REA of the Waves NS1: 0 % = its bare all-pass pair), NS1 on it
  17002. #include "Parity190lnTests.inc" // round 190ln: Eiosis AirEQ / Slate Infinity EQ translated onto Facet EQ in physical units
  17003. #include "Parity190lnKhsTests.inc" // round 190ln: Kilohearts states (kHs snapins, Faturator, Disperser) on LYNX stand-ins
  17004. #include "Parity190nbKhsTests.inc" // round 190nb: Kilohearts laws on Effector (Ring Mod, Reverser, Resonator, Phase Distortion, Shaper) + measured maps
  17005. #include "Parity190nhKhsTests.inc" // round 190nh: Kilohearts part 2 (Comb Filter, Chorus, Trance Gate, Phaser, Distortion, Bitcrush, Frequency Shifter, Ensemble)
  17006. #include "Parity190ocKhsTests.inc" // round 190oc: Kilohearts Nonlinear Filter
  17007. #include "Parity190pyKhsTests.inc" // round 190py: Kilohearts Flanger and Dual Delay on Effector
  17008. #include "Parity190qzKhsTests.inc" // round 190qz: Kilohearts Ring Mod 2× path, Formant Filter, Delay, Clipper on Effector
  17009. #include "Parity190kwR2Tests.inc" // round 190kw: Pro-R 2 → LuxeVerb Space, the Space engine's revision 1 (recovered laws)
  17010. #include "Parity190lpR2Tests.inc" // round 190lp: Pro-R 2 → LuxeVerb Space revision 1, the reference's fixed early taps (after Parity190kwR2: its helpers)
  17011. #include "Parity190mgC4Tests.inc" // round 190mg: Prism's hidden C4 law (REA of the Waves C4), C4 on it
  17012. #include "Parity190mgRenTests.inc" // round 190mg (ren): Compressor's Renaissance law (REA of the Waves Renaissance Compressor), RComp on it
  17013. #include "Parity190mgNs1Tests.inc" // round 190mg (ns1): Quell's NS1 law (REA of the Waves NS1: its own structure for 20 … 100 %), NS1 on it
  17014. #include "Parity190mgVocTests.inc" // round 190mg (voc): the Waves Doubler DSP's splice law on Gemini, the Waves Tune RT law on LYNX Tune
  17015. #include "Parity190mgDynTests.inc" // round 190mg (dyn): Facet's PuigTec law, L2's IDR on the Limiter (REA of the Waves PuigTec / L2 DSP)
  17016. #include "Parity190mmTests.inc" // round 190mm: Facet's Waves design law (REQ, Q10), API-550 / 560 on Facet, SSL G-Channel on Ridge
  17017. #include "Parity190mmSchepsTests.inc" // round 190mm (scheps): the Scheps Omni Channel on Ridge Strip's hidden omni law (five modules, order)
  17018. #include "Parity190mmDynTests.inc" // round 190mm (dyn): Waves RVox / RDeEsser / C6 / API-2500 / MaxxBass / RBass / Vitamin stand-ins on hidden laws
  17019. #include "Parity190mtTests.inc" // round 190mt: SSL G EQ law (Ridge), Q10 exact bell, API-560 law (Facet, 65-sample latency), RChannel RVox type
  17020. #include "Sampler190dhTests.inc"
  17021. #include "Fix190eaTests.inc"
  17022. #include "Iss158Tests.inc"
  17023. #include "Voices190ejTests.inc"
  17024. #include "Flfix190ezTests.inc"
  17025. #include "Reafl190flTests.inc"
  17026. #include "FlTails190gkTests.inc"
  17027. #include "Rcfix190fcTests.inc"
  17028. #include "Follow190fgTests.inc"
  17029. #include "Relfix190fpTests.inc"
  17030. #include "Anvil190fkTests.inc"
  17031. #include "AnvilBus190gxTests.inc"
  17032. #include "Donate190fqTests.inc"
  17033. #include "GenPreset190fsTests.inc"
  17034. #include "Issues190ftTests.inc"
  17035. #include "FlFacts190gwTests.inc"
  17036. #include "Flvsti190fwTests.inc"
  17037. #include "Stability190ggTests.inc"
  17038. #include "StemPass190gjTests.inc"
  17039. #include "Small190ghTests.inc"
  17040. #include "GoBack190hlTests.inc"
  17041. #include "Link190glTests.inc"
  17042. #include "SynthLanes190gpTests.inc"
  17043. #include "SchedIndex190gpTests.inc"
  17044. #include "SmartTails190gpTests.inc"
  17045. #include "ApexMeter190gpTests.inc"
  17046. #include "Toolchain190gtTests.inc"
  17047. #include "RenderAhead190gqTests.inc"
  17048. #include "EvPane190fzTests.inc"
  17049. #include "U190heLinkTests.inc" // round 190he (issue #152 item 2: linkable LYNX instruments, knobs that follow links)
  17050. #include "PluginDrop190hfTests.inc" // issue #152 point 6: plugins dropped on the tracks (FL and Live layouts)
  17051. #include "PickerDrop190hmTests.inc" // issue #177: Picker panel entries dragged onto the Playlist
  17052. #include "Community190hpTests.inc" // round 190hp: global MIDI mappings, mix snapshots, split at silence (0.1.28 pain report B9/B16/B18)
  17053. #include "Rollup028Tests.inc" // round 190fv: semantic interactions of the 0.1.28 rollup
  17054. #include "Design190igTests.inc" // round 190ig: automation clip colours (design190ia slice 3)
  17055. #include "Pdc190hqTests.inc" // round 190hq B13: side inputs, level automation and render-ahead under PDC
  17056. #include "TempoMarkers190hqTests.inc" // round 190hq B11: tempo markers, resampled audio under them, scene tempo hold
  17057. #include "UserFix190ibTests.inc" // round 190ib: issues #165 (SoundFont same-step slide), #170, #181, #182, #183
  17058. #include "LiveRack190hrTests.inc" // round 190hr: Live Racks finish (Group / Ungroup of an instrument, .adg presets)
  17059. #include "Formats190iiTests.inc" // round 190ii (issue #149): .dwp / .dwb / .gig / NCW / Kontakt detection
  17060. #include "Simple190jcTests.inc" // round 190jc: the Simple layout's BandLab / FLM look (design190ia slices 5, 14, 15, 22, 24)
  17061. #include "Rollup030Tests.inc" // round 190il: semantic interactions of the 0.1.30 rollup (keep last)
  17062. #include "Bug190ieTests.inc" // round 190ie (issue #185: tempo automation through the basic Delay)
  17063. #include "Perf190kgTests.inc" // round 190kg: the basic Delay's faster loops render round 190ie's samples bit for bit
  17064. #include "Harm190krTests.inc" // round beatfl190kr: Harmless's vector sine bank, Vocodex's sliding max: the old bits
  17065. #include "Shift190krTests.inc" // round 190kr: Signalsmith Stretch's faster loops render the base commit's pitch shifts bit for bit
  17066. #include "Perf190krTests.inc" // round 190kr: the FL-native mixer effects' faster kernels render the plain loops' samples bit for bit
  17067. #include "PerfGate190idTests.inc" // round perfgate190id: the performance gate's synthetic projects (community slice B12)
  17068. #include "Details190ihTests.inc" // round 190ih: DETAILS slices R2, R3, R4, R7, R9, R12, R14
  17069. #include "Rackmix190ioTests.inc" // round 190io: FL rack + Mixer landmarks, Mixer details, issue #154 part 2
  17070. #include "Bugs190irTests.inc" // round 190ir: issue #189 text size, takes on the tracks FL picks (Discord report)
  17071. #include "Parity190isTests.inc" // round 190is: Echoform voicings revision 1, the Soundtoys echoes' own defaults
  17072. #include "Warp190ikTests.inc" // round 190ik: Complex Pro, granular Texture, the pitch-aware Beats detector
  17073. #include "Details190imTests.inc" // round 190im: activity meter reader, deactivated grid clips, Live keys 0 / Ctrl+E
  17074. #include "Toolbar190ivTests.inc" // round 190iv: the toolbar per layout style (Simple capsules, Live control bar)
  17075. #include "Toolbar190qgTests.inc" // round 190qg: nothing hidden silently, FL's single line, the Grouped look
  17076. #include "MidiOutHw190jlTests.inc" // round 190jl: MIDI Out channels to hardware MIDI outputs (Discord #help)
  17077. #include "Hosting190jgTests.inc" // round 190jg: sample-accurate automation for hosted plugins (BUILD-LIST 18)
  17078. #include "G1menus190jeTests.inc" // round 190je: contrast floors, one state language, grid intensity, FL menus (design190ia G1, FW-S3)
  17079. #include "Capfix190jjTests.inc" // round 190jj: capture190ji changes (transport states per look, the master bar meter)
  17080. #include "Livefix190jdTests.inc" // round 190jd: live check 190iw — previews, automation clip colours, count-in, Gross Beat locator, .flp audio clip step
  17081. #include "Livefix190jdATests.inc" // round 190jd: DirectWave zone map, Slicex deck Snap menu and play line
  17082. #include "Livefix190jdBTests.inc" // round 190jd: the Clip View MPE tab as Live 12.4
  17083. #include "Livefix190jdCTests.inc" // round 190jd: Mixer peak hold and Extra large list, Limiter / Maximus history
  17084. #include "Folders190jfTests.inc" // round 190jf: B17 Playlist folders (nested groups, FL group mute / solo, Alt+drag)
  17085. #include "DawProject190jfTests.inc" // round 190jf B22: DAWproject import / export (core/DawProject.h, core/ZipLite.h)
  17086. #include "Iox190rmTests.inc" // round 190rm: the 0.1.32 import / export sweep (DAWproject audio formats)
  17087. #include "Polish190jpTests.inc" // round 190jp: Theme Adjustments and colour dots, bold interface text, Simple follow-ups
  17088. #include "LiveLook190jnTests.inc" // round 190jn: the Clip-launch layout in Live's look (the Info View key)
  17089. #include "Playlist190jqTests.inc" // round 190jq: Playlist header width (#164), pointer click (#191), automation clip lanes (#191.3)
  17090. #include "Slides190jvTests.inc" // round 190jv: FL's stepped slide notes on its native generators
  17091. #include "Trim190jvTests.inc" // round 190jv: Mixer › Trim unused tracks (FL's Remove unused tracks)
  17092. #include "Pr190jvTests.inc" // round 190jv: Piano roll Scale highlighting › Automatic (FL's key guess)
  17093. #include "PrArp190jvTests.inc" // round 190jv: FL's Piano roll arpeggiator patterns (TPRArpForm law)
  17094. #include "Flm190jvTests.inc" // round 190jv: FL Studio Mobile racks with several instruments / an effect first (BUILD-LIST 19)
  17095. #include "FlmExport190ogTests.inc" // round 190og: Save a copy as FL Studio Mobile project (.flm), the writer, the LYNX chunk
  17096. #include "Spin190kbTests.inc" // round 190kb: the worker pool's waiting (statistics, tuning, bounded task-graph waits, adaptive spin)
  17097. #include "Beat190krTests.inc" // round 190kr: the worker pool wakes only the workers a chunk's work needs; the same samples
  17098. #include "Lighter190laTests.inc" // round 190la: timed waits without the sub-millisecond spin (platform/CvWait.h)
  17099. #include "Sched190kzTests.inc" // round 190kz: one worker-pool round per device block cut in pieces; the same samples
  17100. #include "Sched190lvTests.inc" // round 190lv: batched blocks with live controllers (two rounds: their sources, then the rest); the same samples
  17101. #include "Export190kaTests.inc" // round als190ka-export (owner decision D5): AIFF and M4A (AAC) export
  17102. #include "Issues190jzTests.inc" // round 190jz: issues #192 (docked Browser), #194 (clip fade handles), #195 (Home, W)
  17103. #include "PrChord190kdTests.inc" // round 190kd: FL's chord tool strip (Generate modes, Analyze, Learn from MIDI input, note options)
  17104. #include "ClipLaunch190kcTests.inc" // round 190kc: Live return tracks, crossfader, relative session mappings, audio grooves
  17105. #include "Gaps190kfTests.inc" // round gaps190kf: Live Transpose ±48 in real time, the Clip View warp time line
  17106. #include "Gaps190kfNativesTests.inc" // round gaps190kf: FL Edison's Sample properties (form, Accept, WAV chunks)
  17107. #include "Natives190kjNewtoneTests.inc" // round natives190kj: Newtone note analysis as FL's (tiling, whole-sample pitch, fades), editor commands
  17108. #include "Natives190kjEdisonTests.inc" // round natives190kj: FL Edison Dual view, grid step, pitch-period snap, grid-slicing, scrub, pitch regions
  17109. #include "Natives190kjHarmorTests.inc" // round natives190kj: Harmor resynthesis (analysis, voice, engine revision)
  17110. #include "Qa190kpTests.inc" // round qa190kp: Follow pause after a layout switch during playback
  17111. #include "QaFix190kqTests.inc" // round qafix190kq: QA-1/2/6/9/10 release blockers of 0.1.30
  17112. #include "QaFix190kqReportTests.inc" // round qafix190kq: the import report's "not reproduced" list apart from the synth-preset instruments
  17113. #include "Details190knTests.inc" // round details190kn: FL mute-switch modifiers (Unmute All, Lock state, slot solo), BandLab chain-preset stepper
  17114. #include "Own190ksTests.inc" // round 190ks: GitHub #198, the notes a plugin plays from its own window are recorded
  17115. #include "Stoys190kvTests.inc" // round 190kv: the Soundtoys factory presets (fingerprint of the encoded payload -> their own settings)
  17116. #include "Details190ldTests.inc" // round details2_190ld: FL slot and Playlist track lock state, Live Shift+Space continue, FL tool names
  17117. #include "Details190ldPluginTests.inc" // round details2_190ld: Vocoder band levels published while watched, Patcher activity measured only while watched
  17118. #include "Ara190kxTests.inc" // round 190kx: ARA 2 on audio clips (the clips as playback regions, ids, tempo, the clip menu)
  17119. #include "LoopAnalysis190kxTests.inc" // round 190kx: loop analysis (key and tempo of the user's own loops; core/LoopAnalysis.h)
  17120. #include "Vocal190liTests.inc" // round 190li: Newtone natural engine (engine 1), modulation / drift split, sung centre, analysis octave guard
  17121. #include "Newtone190lmTests.inc" // rollup 0.1.30 step 8: a new Newtone slot (natural engine) analysed with "Separate close notes"
  17122. #include "Rollup190luTests.inc" // rollup 0.1.30 step 9: per-block batching vs plugins' own notes (190kz x 190ks), LYNX Tune's held notes
  17123. #include "Rollup190meTests.inc" // rollup 0.1.30 step 10: round 190lv's fix, a block of too many pieces still plays (190kz batching)
  17124. #include "Codegen190kzTests.inc" // round 190kz codegen: runtime CPU dispatch (AVX2 paths render the SSE2 bits)
  17125. #include "Dsp190kzTests.inc" // round 190kz: exact Reeverb 2 LFO sine and sinc seeds, Maximus revision 1
  17126. #include "Issue204Tests.inc" // round 190lq: issue #204, an audio clip channel played from notes (whole clip, layered, key pitch)
  17127. #include "Issues190llTests.inc" // round 190ll: issues #199 (hosted synth from the piano roll), #200, #201, #203
  17128. #include "Details190lrTests.inc" // round details3_190lr: the Simple Drum Machine (BandLab steps), Live scene follow actions
  17129. #include "Issues190lwTests.inc" // round 190lw: issues #205 / #206 / #207 (row bands, second click, automation point editing)
  17130. #include "Details190lxTests.inc" // round details4_190lx: Live slot-block clipboard, separate Punch-In / Punch-Out
  17131. #include "Issue208Tests.inc" // round 190ma: issue #208, the graph editor's Note pitch, Shift and Repeat (FL note repeat)
  17132. #include "Details190mfTests.inc" // round details5_190mf: FL normal meter law (Pattern default), Channel rack "+" menu cache, details pass 5
  17133. #include "Modal190mcTests.inc" // round 190mc: the Piano roll's Chord helper (discussion #105) and Transistor Bass's program row (discussion #202)
  17134. #include "KeyRegions190nuTests.inc" // round chord2_190nu: key regions (FL key markers), the Scale degree chord, pinned Chord helper relationships (discussion #105)
  17135. #include "Details190mkTests.inc" // round details6_190mk: Live insert marker, rack zip, details pass 6
  17136. #include "Details190mpTests.inc" // round details7_190mp: FL Half speed, Auto name clips, Restretch all, details pass 7
  17137. #include "Issues190mqTests.inc" // round 190mq: issues #211 / #212 / #213 (paste per layout, key-row shading, ruler double click)
  17138. #include "core/Details190mr.h"
  17139. #include "Details190mrTests.inc" // round details8_190mr: click-and-hold, rack Auto Select, undo / warning / layout options, details pass 8
  17140. #include "core/Details190mv.h"
  17141. #include "core/PluginHelp.h" // (round 190oa)
  17142. #include "Details190mvTests.inc" // round details9_190mv: undo knob tweaks and the rest of details pass 9
  17143. #include "Details190myTests.inc" // round details10_190my: the Current track follow-ups and details pass 10
  17144. #include "Details190oaTests.inc" // round details11_190oa: details pass 11 (the audit's last open rows)
  17145. #include "Mixer221_190ngTests.inc" // round 190ng: issue #221 (Del with only the used inserts shown)
  17146. #include "Issues190ngTests.inc" // round 190ng: issues #215, #217, #219, #220, #222
  17147. #include "Rack190ncTests.inc" // round 190nc (0.1.31): Live's Solo / Cue (DL-110) and the Macro Map pick (DL-95)
  17148. #include "TrackClips190neTests.inc" // round 190ne (0.1.31): track clips, "Clips own their notes" (core/TrackClips.h)
  17149. #include "Leftovers190nrTests.inc" // round 190nr: leftovers of the 0.1.30 release
  17150. #include "Bugs190nqTests.inc" // round 190nq: issues #229, #193, #223, #226 (bugs)
  17151. #include "Issues190nqTests.inc" // round 190nq: issues #225, #227, #228, #230
  17152. #include "Issues190nsTests.inc" // round 190ns: issues #231, #233, #234, #235
  17153. #include "Tracker190nwTests.inc" // round 190nw (0.1.31): the built-in tracker, stage 1 (core/TrackerGrid.h, core/ModuleImport.h)
  17154. #include "CrossPaste190nzTests.inc" // round 190nz: issue #232 stage 2 (copy and paste between windows)
  17155. #include "Issues190odTests.inc" // round 190od: issues #236, #238, #239, #240
  17156. #include "Issues190oqTests.inc" // round 190oq: issues #243 (ARA insert, tempo map), #252 (start page)
  17157. #include "Perf190ofTests.inc" // round 190of: the monitor's Longest / Spikes, the split CPU meter (P24), latency check
  17158. #include "Tracker190obTests.inc" // round 190ob (0.1.31): tracker follow-ups (module volume automation, the default key)
  17159. #include "Gap190ogTests.inc" // round gap190og (0.1.31): Live Duplicate Loop, Crop Clip, velocity Randomize, mappable transport, Edit menu on devices
  17160. #include "Host190ohTests.inc" // round 190oh: plugin host review, FL's wrapper Settings › GUI (P3)
  17161. #include "Sections190olTests.inc" // round 190ol (0.1.31): the Sections lane (P1) and Simple time signature changes (P6)
  17162. #include "ChordLane190pfTests.inc" // round 190pf (0.1.32): the Chord lane (P13) and Smart Tempo (P12)
  17163. #include "Research190onTests.inc" // round 190on: command palette (P2), Tab to transient (P8), stacked lanes (P10), removing a guest
  17164. #include "Perf190okTests.inc" // round 190ok: safe overloads (P24), generator smart disable (D2), the smart-disable tip (D1), fast maths (D3)
  17165. #include "Design190ooTests.inc" // round 190oo (0.1.31): design review 2, the owner's options with rules in core
  17166. #include "Lanes190ooTests.inc" // round 190oo (design review 2, lanes): Live envelope lanes (CS-19), lanes folded on open (CS O-3), Live scale control (O-5)
  17167. #include "SimpleFl190ooTests.inc" // round 190oo (design review 2, CS-24): S / volume / pan on an FL project's tracks in Simple
  17168. #include "Pat6_190ooTests.inc" // round 190oo (design review 2): PAT-6, the Playlist row from FL's vertical zoom (event 0xF2)
  17169. #include "DialogRules190ooTests.inc" // round 190oo: the one dialog system's button order and pixel bands (core/DialogRules.h)
  17170. #include "RackAutomation190ooTests.inc" // round 190oo (design review 2, PAT-7): FL's automation clip channels in the Channel rack
  17171. #include "RackAutomation190owTests.inc" // round 190ow (PAT-7 rest, DE-123): triggered automation clips, MIN / MAX, mute, rack order export
  17172. #include "TextFit190ooTests.inc" // round 190oo (design review 2, fit): translated menu titles give up width before toolbar panels drop, knob captions
  17173. #include "OldSteps190ozTests.inc" // round 190oz: FL <= 11 step grids (0x41 + 0x5B / 0x48 ... in a channel) become step notes
  17174. #include "HeldSteps190ozTests.inc" // round 190oz: FL step notes on generators are held (no note-off)
  17175. #include "FlParity190piTests.inc" // round 190pi: Sytrus slide velocity, arpeggio glides, chord slides, Ogun link velocity, FL 11 parameters
  17176. #include "FlParity190prTests.inc" // round 190pr: Harmor automation, FL song-start values of Harmor / Sytrus / FL effects
  17177. #include "PatcherXyz190ptTests.inc" // round 190pt: Patcher Control Surface / X-Y-Z links, Vocodex controls under automation
  17178. #include "Parity190pyTests.inc" // round 190py: channel PITCH automation, Gross Beat automation, Love Philter / Harmless scales, 3x Osc automation
  17179. #include "Parity190qzTests.inc" // round 190qz: Peak / Envelope Controller links before the controller's first value
  17180. #include "Perf190osTests.inc" // round 190os: the safe-overload guard lets go at the stop and within 1 s of the overload's end
  17181. #include "Perf190pxTests.inc" // round 190px: AVX half-band, kept minimum-phase poles, Ogg samples without spare room, render-ahead paced
  17182. #include "NativePerf190pzTests.inc" // round 190pz: recording path (no yield while takes record, durable sync every 5 s, two-piece copies)
  17183. #include "Tracker190omTests.inc" // round 190om (0.1.31): tracker stage 2 (pattern events, pitch curves, Bxx / Vxx), SimSynth velocity
  17184. #include "Rollup031s6Tests.inc" // rollup 0.1.31 step 6: gold sweep for 190om decisions 4-5 (LYNX_SWEEP_DIR)
  17185. #include "TrackStates190opTests.inc" // round 190op (0.1.31): Hide / Deactivate tracks (P4), Capture automation (P7)
  17186. #include "SimpleDuck190otTests.inc" // round 190ot (0.1.31): Simple "Duck under..." (sidechain in one pick) and the shared Echo send (P5)
  17187. #include "HWheel254Tests.inc" // round 190ou: issue #254, the horizontal wheel
  17188. #include "AutoLinks190ovTests.inc" // round 190ov (0.1.31): FL automation clips linked to several controls
  17189. #include "PluginPack190ovTests.inc" // round 190ov (0.1.31): the later LYNX-own engines in the plugin pack
  17190. #include "AutoModes190paTests.inc" // round 190pa (0.1.31): automation write modes (P21), range edits on chosen tracks (P14)
  17191. #include "Tracks190peTests.inc" // round 190pe (0.1.31): track versions (P20) and the import of tracks from another project (P11)
  17192. #include "Jsfx190pjTests.inc" // round 190pj (0.1.32): JSFX support stages 1-2 (issue #241)
  17193. #include "Issue258_190pvTests.inc" // round 190pv: issue #258, a cloned channel's own automation drives its sound
  17194. #include "Issues190qbTests.inc" // round 190qb: issue follow-ups (#215 folders in the export name)
  17195. #include "Repeats190pmTests.inc" // round 190pm (0.1.32): Note Repeats and Conditions (research190oe P19)
  17196. #include "MixerQol190qhTests.inc" // round 190qh (0.1.32 R5): Mixer QoL, FL's PDC rule for a switched-off slot (D2-MX9)
  17197. #include "Hotfix190qpTests.inc" // round 190qp (0.1.31.1): imported FL volume / send / filter automation, linked clips shown
  17198. #include "Hotfix190raTests.inc" // round 190ra (0.1.31.2): a set pattern length grows with the notes drawn past it
  17199. #include "Issue269_190qmTests.inc" // round 190qm: issue #269, the hint bar's right-click crash
  17200. #include "Hotfix190qpWfTests.inc" // round 190qp: 0.1.31.1 hotfix (track clips open their notes)
  17201. #include "Issues274_190qrTests.inc" // round 190qr: issues #274-#281 (markers delete, AppImage manager entries, zoom, rack meter...)
  17202. #include "Feel190qeTests.inc" // round 190qe (0.1.32): edge scroll (FL law), view stack, zoom glide and anchor, selection history, keep labels
  17203. #include "Picker190qiTests.inc" // round 190qi (0.1.32 R6): Recent / Hidden / one row per plugin, MIDI preview, file types, Tab completion, tag tree
  17204. #include "Issues190qjTests.inc" // round 190qj (0.1.32): issue #265 (a linked track's M and clips on other inserts), PRs #261-#266, #238
  17205. #include "Pr268_190qmTests.inc" // round 190qm: PR #268, a switched-off Playlist track (FL's greyed clips + the LYNX row veil)
  17206. #include "Qa190rbTests.inc" // round 190rb (0.1.32 quality): crash / memory fixes from the crash reports, ASan / UBSan and the UI monkey
  17207. #include "Issues190qoTests.inc" // round 190qo: issues #270 (typing keyboard layouts), #271 (SoundFont slide velocity)
  17208. #include "Discord1124_190quTests.inc" // round 190qu (0.1.32): Discord 11-24 (automation lanes, Split by channel, Track-only, Simple)
  17209. #include "Hotfix190rcFlpDataTests.inc" // round 190rc: the .flp export writes the project data folder only when it is on this computer (FL 2026: "Invalid project data folder")
  17210. #include "Hotfix190rcPresetsTests.inc" // round 190rc: issue #288, FL presets of the native generators open the generator itself
  17211. #include "TrialMark190qtTests.inc" // round 190qt: a project from an FL trial save goes back to FL as one (option A)
  17212. #include "Backlog190rcTests.inc" // round 190rc (0.1.32): #285 split at the song position, #286 the project folder, ghosts in Track-only / Simple
  17213. #include "Sweep190reTests.inc" // round 190re (0.1.32 regression sweep): the defects it found and fixed