tests.cpp 888 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126
  1. // Core tests (no JUCE). Usage: lynx_tests [folder-with-flp-files]
  2. #include <optional>
  3. #include <atomic>
  4. #include <chrono>
  5. #include <thread>
  6. #include <random>
  7. #include <cctype>
  8. #include <bit>
  9. #include <cmath>
  10. #include <cstdio>
  11. #include <cstring>
  12. #include <filesystem>
  13. #include <map>
  14. #include <set>
  15. #include <sstream>
  16. #include <fstream>
  17. #include <functional>
  18. #include <iterator>
  19. #include <string>
  20. #include <tuple>
  21. #include <vector>
  22. #if defined(__SSE__) || defined(_M_X64) || defined(__x86_64__) || defined(__aarch64__) || defined(_M_ARM64) // x86 SSE, or ARM64 through core/Simd.h (sse2neon)
  23. #include "core/Simd.h"
  24. #endif
  25. #include "core/AutomationDisplay.h"
  26. #include "core/ShownEffectSlot.h"
  27. #include "core/Dither.h"
  28. #include "core/DrumSynthLive.h"
  29. #include "core/Effects.h"
  30. #include "core/MixSuite.h"
  31. #include "core/EmberClassic.h"
  32. #include "core/Effector.h"
  33. #include "core/FruityDelayBank.h"
  34. #include "core/FruityMultiband.h"
  35. #include "core/Denormals.h"
  36. #include "core/TestProfile.h"
  37. #include "core/FlpImport.h"
  38. #include "core/MidiControls.h"
  39. #include "core/MidiImport.h"
  40. #include "core/ChannelMiscFx.h"
  41. #include "core/FlEffects.h"
  42. #include "core/ProjectContents.h"
  43. #include "core/FlPatcher.h"
  44. #include "core/Patcher.h"
  45. #include "core/Layer.h"
  46. #include "core/XyController.h"
  47. #include "core/ControlSurface.h"
  48. #include "core/VoltageController.h"
  49. #include "core/FlSamplers.h"
  50. #include "core/FlSynths.h"
  51. #include "core/Inflate.h"
  52. #include "core/WavPack.h"
  53. #include "core/SampleFiles.h"
  54. #include "core/FlWrapper.h"
  55. #include "core/FlPluginDb.h"
  56. #include "core/MissingFiles.h"
  57. #include "core/PianoRollTools.h"
  58. #include "core/EventEdit.h"
  59. #include "core/Preferences.h"
  60. #include "core/MidiSync.h"
  61. #include "core/History.h"
  62. #include "core/Gms.h"
  63. #include "core/PoiZone.h"
  64. #include "core/MiniSynth.h"
  65. #include "core/SimSynth.h"
  66. #include "core/Granulizer.h"
  67. #include "core/WaveTraveller.h"
  68. #include "core/Drumaxx.h"
  69. #include "core/Wasp.h"
  70. #include "core/PluginLayout.h"
  71. #include "core/Plucked.h"
  72. #include "core/BeepMap.h"
  73. #include "core/Sawer.h"
  74. #include "core/Slayer.h"
  75. #include "core/Toxic.h"
  76. #include "core/Harmless.h"
  77. #include "core/Harmor.h"
  78. #include "core/Morphine.h"
  79. #include "core/Ogun.h"
  80. #include "core/BassDrum.h"
  81. #include "core/FruitKick.h"
  82. #include "core/IppCos.h"
  83. #include "core/Dx10.h"
  84. #include "core/Kepler.h"
  85. #include "core/ThreeOsc.h"
  86. #include "core/FlResampler.h"
  87. #include "core/FlKeys.h"
  88. #include "core/DirectWave.h"
  89. #include "core/OggVorbis.h"
  90. #include "core/FlVoiceProcessor.h"
  91. #include "core/TransistorBass.h"
  92. #include "core/HarmlessFx.h"
  93. #include "core/MaximusFx.h"
  94. #include "core/EmphasisFx.h"
  95. #include "core/VisualFx.h"
  96. #include "core/EmphasizerFx.h"
  97. #include "core/FlLegacyFx.h"
  98. #include "core/FreqShifterFl.h"
  99. #include "core/LowLifterFl.h"
  100. #include "core/Reeverb2.h"
  101. #include "core/LuxeVerb.h"
  102. #include "core/Denormals.h"
  103. #include "core/FruityDynamics.h"
  104. #include "core/FruityShapers.h"
  105. #include "core/ParametricEqFl.h"
  106. #include "core/FruityBasicFx.h"
  107. #include "core/EquoFx.h"
  108. #include "core/DistructorFx.h"
  109. #include "core/MultibandDelayFx.h"
  110. #include "core/PitchShifterFl.h"
  111. #include "core/FruityFilters.h"
  112. #include "core/NewtoneFx.h"
  113. #include "core/FruityDelay.h"
  114. #include "core/StereoFx.h"
  115. #include "core/FrequencySplitter.h"
  116. #include "core/ChorusFx.h"
  117. #include "core/GrossBeat.h"
  118. #include "core/LovePhilter.h"
  119. #include "core/FlangusFastDist.h"
  120. #include "core/FruityPhaser.h"
  121. #include "core/FruityVocoder.h"
  122. #include "core/DelphiMath.h"
  123. #include "core/LiveEventQueue.h"
  124. #include "core/RackRenderer.h"
  125. #include "core/MetronomeVoice.h"
  126. #include "engine/Transport.h"
  127. #include "core/Patcherize.h"
  128. #include "core/Performance.h"
  129. #include "core/Scheduler.h"
  130. #include "core/MarkerPlan.h"
  131. #include "core/SnapshotExchange.h"
  132. #include "core/SoftClip.h"
  133. #include "core/Slicer.h"
  134. #include "core/SytrusEngine.h"
  135. #include "core/Templates.h"
  136. #include "core/Vocodex.h"
  137. #include "core/TimeStretch.h"
  138. #include "core/WorkerPool.h"
  139. #include "core/RtStats.h"
  140. #include "core/FixedBlock.h"
  141. #include "core/ParametricEqLinear.h"
  142. #include "core/Song.h"
  143. #include "core/NoteEdit.h"
  144. #include "core/LiveRecordView.h"
  145. #include "core/Recorder.h"
  146. #include "core/KeyMap.h"
  147. #include "core/SortOnce.h"
  148. #include "core/RackOps.h"
  149. #include "core/PlaylistTrackOps.h"
  150. #include "core/ChannelLoops.h"
  151. #include "core/ChannelBurn.h"
  152. #include "core/AutoSampler.h"
  153. #include "core/ValueMath.h"
  154. #include "core/Workspace.h"
  155. #include "core/StartupSound.h"
  156. #include "core/ExportOptions.h"
  157. #include "core/Mp3Encoder.h"
  158. #include "core/PluginMonitor.h"
  159. #include "core/PluginSubstitutes.h"
  160. #include "core/ClipOps.h"
  161. #include "core/PlaylistOps.h"
  162. #include "core/QuickRender.h"
  163. #include "core/TouchKeyboard.h"
  164. #include "core/HeldNotes.h"
  165. #include "core/Templates.h"
  166. #include "flp/Flp.h"
  167. #include "ui/I18nData.h"
  168. #include "ui/I18nLocale.h"
  169. #include "core/CrashReport.h"
  170. namespace
  171. {
  172. int failures = 0;
  173. int checks = 0;
  174. std::string processTempTag(); // per-process suffix for scratch files (defined further down)
  175. std::vector<std::pair<const char*, std::function<void()>>>& registry()
  176. {
  177. static std::vector<std::pair<const char*, std::function<void()>>> r;
  178. return r;
  179. }
  180. struct Register
  181. {
  182. Register (const char* name, std::function<void()> fn) { registry().emplace_back (name, std::move (fn)); }
  183. };
  184. #define TEST(name) \
  185. static void name(); \
  186. static Register reg_##name (#name, name); \
  187. static void name()
  188. #define CHECK(cond) \
  189. do \
  190. { \
  191. ++checks; \
  192. if (! (cond)) \
  193. { \
  194. ++failures; \
  195. std::printf (" FAILED %s:%d: %s\n", __FILE__, __LINE__, #cond); \
  196. } \
  197. } while (false)
  198. // Engines that compute in x87 80-bit long double (FL's own arithmetic) match their goldens to the last bit on x86. On
  199. // ARM64 (Apple Silicon) long double is not the x87 format, so there they hold to a looser bound (round 117).
  200. // LYNX_X87_TOL overrides it (measuring).
  201. inline float armTolerance (float x86, float arm)
  202. {
  203. #if defined(__x86_64__) || defined(__i386__) || defined(_M_X64) || defined(_M_IX86)
  204. (void) arm;
  205. return x86;
  206. #else
  207. if (const char* t = std::getenv ("LYNX_X87_TOL"))
  208. return (float) std::atof (t);
  209. return arm;
  210. #endif
  211. }
  212. inline float x87Tolerance() { return armTolerance (2.0e-7f, 2.0e-4f); }
  213. inline float granulizerTolerance()
  214. {
  215. #if defined(__APPLE__)
  216. return 2.0e-4f;
  217. #else
  218. return armTolerance (1.0e-6f, 1.0e-5f);
  219. #endif
  220. }
  221. // Engines whose goldens depend on the C library's sin/cos/exp/pow: Windows (mingw), glibc and musl hold the 1e-7
  222. // goldens, Apple's libm does not (round 117), so on macOS the per-sample bound is looser.
  223. inline float libmTolerance (float strict)
  224. {
  225. #if defined(__APPLE__)
  226. (void) strict;
  227. return 2.0e-4f;
  228. #else
  229. return strict;
  230. #endif
  231. }
  232. // One golden sample: relative bound (with a 1e-3 floor on macOS, where near-zero samples carry libm noise); prints the
  233. // measured error when it fails.
  234. inline bool goldenSampleClose (float y, float golden, float tol)
  235. {
  236. #if defined(__APPLE__)
  237. const float scale = std::max (1.0e-3f, std::abs (golden));
  238. #else
  239. const float scale = std::max (1.0e-16f, std::abs (golden));
  240. #endif
  241. if (std::abs (y - golden) <= tol * scale)
  242. return true;
  243. std::fprintf (stderr, " golden sample %.9g vs %.9g (relative error %.3g, bound %.3g)\n", (double) y, (double) golden,
  244. (double) (std::abs (y - golden) / scale), (double) tol);
  245. return false;
  246. }
  247. std::string realFlpDir;
  248. // --- helpers for building synthetic .flp files -----------------------------
  249. struct FlpBuilder
  250. {
  251. std::vector<std::uint8_t> bytes;
  252. FlpBuilder (std::uint16_t ppq = 96)
  253. {
  254. put ("FLhd");
  255. u32 (6);
  256. u16 (0);
  257. u16 (1);
  258. u16 (ppq);
  259. put ("FLdt");
  260. u32 (0); // size is not used by the reader
  261. }
  262. void put (const char* s) { bytes.insert (bytes.end(), s, s + std::strlen (s)); }
  263. void u8 (std::uint8_t v) { bytes.push_back (v); }
  264. void u16 (std::uint16_t v) { u8 ((std::uint8_t) v); u8 ((std::uint8_t) (v >> 8)); }
  265. void u32 (std::uint32_t v) { u16 ((std::uint16_t) v); u16 ((std::uint16_t) (v >> 16)); }
  266. void varint (std::size_t n)
  267. {
  268. do
  269. {
  270. auto b = (std::uint8_t) (n & 0x7F);
  271. n >>= 7;
  272. u8 (n ? (std::uint8_t) (b | 0x80) : b);
  273. } while (n);
  274. }
  275. void data (std::uint8_t id, const std::vector<std::uint8_t>& payload)
  276. {
  277. u8 (id);
  278. varint (payload.size());
  279. bytes.insert (bytes.end(), payload.begin(), payload.end());
  280. }
  281. void ascii (std::uint8_t id, const std::string& s)
  282. {
  283. std::vector<std::uint8_t> p (s.begin(), s.end());
  284. p.push_back (0);
  285. data (id, p);
  286. }
  287. void utf16 (std::uint8_t id, const std::u16string& s)
  288. {
  289. std::vector<std::uint8_t> p;
  290. for (auto c : s)
  291. {
  292. p.push_back ((std::uint8_t) c);
  293. p.push_back ((std::uint8_t) (c >> 8));
  294. }
  295. p.push_back (0);
  296. p.push_back (0);
  297. data (id, p);
  298. }
  299. };
  300. std::vector<std::uint8_t> noteRecord (std::uint32_t pos, std::uint16_t channel, std::uint32_t len, std::uint16_t key,
  301. std::uint8_t velocity)
  302. {
  303. std::vector<std::uint8_t> r (24, 0);
  304. std::memcpy (&r[0], &pos, 4);
  305. std::memcpy (&r[6], &channel, 2);
  306. std::memcpy (&r[8], &len, 4);
  307. std::memcpy (&r[12], &key, 2);
  308. r[16] = 120;
  309. r[20] = 64;
  310. r[21] = velocity;
  311. return r;
  312. }
  313. } // namespace
  314. // ---------------------------------------------------------------------------
  315. // FLP
  316. TEST (flp_rejects_garbage)
  317. {
  318. std::vector<std::uint8_t> junk (64, 0x41);
  319. bool threw = false;
  320. try { flp::EventReader r (junk); } catch (const flp::Error&) { threw = true; }
  321. CHECK (threw);
  322. }
  323. TEST (flp_reads_size_classes_varint_and_extended_ids)
  324. {
  325. FlpBuilder b;
  326. b.u8 (0x15); b.u8 (2); // u8
  327. b.u8 (0x40); b.u16 (513); // u16
  328. b.u8 (0x9C); b.u32 (128500); // u32
  329. b.data (0xD5, std::vector<std::uint8_t> (300, 7)); // 2-byte varint
  330. b.u8 (0xAC); b.u32 (0xC0000100); b.varint (3); b.u8 (1); b.u8 (2); b.u8 (3); // extended ID
  331. flp::EventReader r (b.bytes);
  332. flp::Event e;
  333. CHECK (r.header().ppq == 96);
  334. CHECK (r.next (e) && e.id == 0x15 && e.value == 2);
  335. CHECK (r.next (e) && e.id == 0x40 && e.value == 513);
  336. CHECK (r.next (e) && e.id == 0x9C && e.value == 128500);
  337. CHECK (r.next (e) && e.id == 0xD5 && e.data.size() == 300 && e.data[299] == 7);
  338. CHECK (r.next (e) && e.id == 0xC0000100 && e.isData() && e.data.size() == 3 && e.data[2] == 3);
  339. CHECK (! r.next (e));
  340. }
  341. TEST (flp_truncated_file_throws)
  342. {
  343. FlpBuilder b;
  344. b.u8 (0x9C); b.u8 (1); // u32 with only 1 byte
  345. flp::EventReader r (b.bytes);
  346. flp::Event e;
  347. bool threw = false;
  348. try { r.next (e); } catch (const flp::Error&) { threw = true; }
  349. CHECK (threw);
  350. }
  351. TEST (flp_version_numbers)
  352. {
  353. CHECK (flp::parseVersionNumber ("25.2.5.5319") == 25002005u);
  354. CHECK (flp::parseVersionNumber ("20.99.3000.3031") == 20102000u);
  355. CHECK (flp::parseVersionNumber ("11.0.3") == 11000003u);
  356. }
  357. TEST (flp_playlist_record_size_by_version)
  358. {
  359. CHECK (flp::playlistRecordSize (12009003, 64) == 32);
  360. CHECK (flp::playlistRecordSize (21000000, 120) == 60);
  361. CHECK (flp::playlistRecordSize (25002005, 160) == 80);
  362. CHECK (flp::playlistRecordSize (25002005, 60) == 60); // size doesn't match the version -> tries the others
  363. CHECK (flp::playlistRecordSize (25002005, 7) == 0);
  364. }
  365. TEST (flp_playlist_record_size_by_file_flags)
  366. {
  367. // REA_rodada2 §4.2: 0xA9 bit 1 -> 80 B, bits 1 and 3 -> the native 88 B record (FL 25.2.99 / 26.x)
  368. CHECK (flp::playlistRecordSize (26001003, 88 * 4, 15) == 88); // 352 is also a multiple of 32
  369. CHECK (flp::playlistRecordSize (26001003, 88 * 10, 15) == 88); // ... and of 80
  370. CHECK (flp::playlistRecordSize (25002005, 160, 7) == 80);
  371. CHECK (flp::playlistRecordSize (21000000, 120, 1) == 60);
  372. CHECK (flp::playlistRecordSize (26001003, 63712) == 88); // no flags: the only size that fits besides 32
  373. }
  374. TEST (flp_native_88_byte_playlist_items_with_clip_gain_and_fades)
  375. {
  376. // an FL 26.1 file: 0xA9 = 15, four 88 B items (352 B: the old 32 B guess read garbage from it)
  377. FlpBuilder b (96);
  378. b.ascii (0xC7, "26.1.3.5570");
  379. b.u8 (0xA9); b.u32 (15);
  380. b.u8 (0x9C); b.u32 (130000);
  381. b.u8 (0x40); b.u16 (0);
  382. b.u8 (0x15); b.u8 (4); // audio clip channel
  383. b.utf16 (0xC4, u"C:\\nowhere\\clip.wav");
  384. std::vector<std::uint8_t> items;
  385. auto add = [&] (std::uint32_t pos, std::uint32_t len, std::uint16_t track, float fadeIn, float fadeOut, float gain) {
  386. std::vector<std::uint8_t> r (88, 0);
  387. const std::uint16_t base = 20480, idx = 0, rv = (std::uint16_t) (499 - track);
  388. const float offs[] = { -1.0f, -1.0f };
  389. std::memcpy (&r[0], &pos, 4);
  390. std::memcpy (&r[4], &base, 2);
  391. std::memcpy (&r[6], &idx, 2);
  392. std::memcpy (&r[8], &len, 4);
  393. std::memcpy (&r[12], &rv, 2);
  394. std::memcpy (&r[0x18], offs, 8);
  395. std::memcpy (&r[0x24], &fadeIn, 4);
  396. std::memcpy (&r[0x2C], &fadeOut, 4);
  397. std::memcpy (&r[0x34], &gain, 4);
  398. const double stretch = 1.0;
  399. const std::int32_t minusOne = -1;
  400. std::memcpy (&r[0x40], &stretch, 8);
  401. std::memcpy (&r[0x4C], &minusOne, 4);
  402. items.insert (items.end(), r.begin(), r.end());
  403. };
  404. add (0, 384, 0, 0.0f, 0.0f, 1.0f);
  405. add (384, 384, 1, 923.0769f, 461.5385f, 1.65f); // 2 beats in, 1 beat out at 130 BPM
  406. add (768, 96, 2, 0.0f, 0.0f, 0.0f);
  407. add (1536, 192, 3, 0.0f, 5000.0f, 0.5f); // fade longer than the clip
  408. b.data (0xE9, items);
  409. const auto p = flp::parseProject (b.bytes);
  410. CHECK (p.flagsA9 == 15);
  411. CHECK (p.arrangements.size() == 1 && p.arrangements[0].items.size() == 4);
  412. CHECK (p.warnings.empty());
  413. if (p.arrangements.size() == 1 && p.arrangements[0].items.size() == 4)
  414. {
  415. const auto& it = p.arrangements[0].items;
  416. CHECK (it[1].position == 384 && it[1].length == 384 && it[1].track == 1 && it[1].kind == flp::PlaylistItem::Kind::channelClip);
  417. CHECK (it[3].position == 1536 && it[3].track == 3);
  418. CHECK (std::abs (it[1].gain - 1.65f) < 1e-6f && std::abs (it[1].fadeInMs - 923.0769f) < 1e-3f);
  419. const auto song = lynx::importFlp (p);
  420. CHECK (song.playlist.size() == 4);
  421. if (song.playlist.size() == 4)
  422. {
  423. const auto beat = (std::int64_t) lynx::ppq;
  424. CHECK (song.playlist[0].gain == 1.0f && song.playlist[0].fadeIn == 0 && song.playlist[0].fadeOut == 0);
  425. CHECK (std::abs (song.playlist[1].gain - 1.65f) < 1e-6f);
  426. CHECK (std::llabs (song.playlist[1].fadeIn - 2 * beat) <= 1 && std::llabs (song.playlist[1].fadeOut - beat) <= 1);
  427. CHECK (song.playlist[2].gain == 0.0f);
  428. CHECK (song.playlist[3].fadeOut == song.playlist[3].length); // clamped
  429. }
  430. }
  431. }
  432. TEST (flp_channel_level_adjustments_and_stretched_clip_start)
  433. {
  434. // FL 26.1 audio clip channel (gf92 follow-up): 0xE5 level adjustments and a time-stretched clip whose item
  435. // start/end are in the sample's milliseconds ("Liwn Passar voz" ch5: 788 ms into a clip stretched by 1.0569)
  436. FlpBuilder b (96);
  437. b.ascii (0xC7, "26.1.3.5570");
  438. b.u8 (0xA9); b.u32 (15);
  439. b.u8 (0x9C); b.u32 (130000);
  440. b.u8 (0x40); b.u16 (0);
  441. b.u8 (0x15); b.u8 (4); // audio clip channel
  442. b.utf16 (0xC4, u"C:\\nowhere\\clip.wav");
  443. {
  444. std::vector<std::uint8_t> levels (24, 0);
  445. const std::int32_t pan = 6400, vol = 10000;
  446. std::memcpy (&levels[0], &pan, 4);
  447. std::memcpy (&levels[4], &vol, 4);
  448. b.data (0xDB, levels);
  449. std::vector<std::uint8_t> adjust (20, 0);
  450. const std::int32_t panAdj = 3200, volAdj = 7286;
  451. std::memcpy (&adjust[0], &panAdj, 4);
  452. std::memcpy (&adjust[4], &volAdj, 4);
  453. b.data (0xE5, adjust);
  454. std::vector<std::uint8_t> params (0x98, 0);
  455. const float time = 64.0f * 192.0f; // stretched to 64 beats (float: 0xA9 bit 0)
  456. const double end = 1.0;
  457. std::memcpy (&params[0x60], &time, 4);
  458. std::memcpy (&params[0x88], &end, 8);
  459. b.data (0xD7, params);
  460. }
  461. {
  462. std::vector<std::uint8_t> r (88, 0);
  463. const std::uint32_t pos = 0, len = 6144; // 64 beats
  464. const std::uint16_t base = 20480, idx = 0, rv = 499;
  465. const float offs[] = { 788.4615f, 788.4615f + 31218.92f }; // sample ms: 64 beats at 130 BPM x 1.056892
  466. const float gain = 1.0f;
  467. std::memcpy (&r[0], &pos, 4);
  468. std::memcpy (&r[4], &base, 2);
  469. std::memcpy (&r[6], &idx, 2);
  470. std::memcpy (&r[8], &len, 4);
  471. std::memcpy (&r[12], &rv, 2);
  472. std::memcpy (&r[0x18], offs, 8);
  473. std::memcpy (&r[0x34], &gain, 4);
  474. b.data (0xE9, r);
  475. }
  476. const auto p = flp::parseProject (b.bytes);
  477. CHECK (p.channels.size() == 1);
  478. if (p.channels.size() != 1)
  479. return;
  480. CHECK (p.channels[0].hasLevelAdjust && p.channels[0].panAdj == 3200 && p.channels[0].volAdj == 7286);
  481. const auto song = lynx::importFlp (p);
  482. CHECK (song.channels.size() == 1 && song.playlist.size() == 1);
  483. if (song.channels.size() != 1 || song.playlist.size() != 1)
  484. return;
  485. // gain = f(volume)·f(volAdj), pan = pan + panAdj
  486. const auto expected = lynx::channelGain (10000.0f / 12800.0f) * lynx::channelGain (7286.0f / 12800.0f);
  487. CHECK (std::abs (lynx::channelGain (song.channels[0].volume) - expected) < 1e-5f);
  488. CHECK (std::abs (song.channels[0].pan - 0.5f) < 1e-6f);
  489. // 788.46 sample ms / 1.056892 = 746.0 ms = 1.6164 beats into the clip
  490. CHECK (song.channels[0].sampler.stretchBeats > 0.0f);
  491. CHECK (std::llabs (song.playlist[0].offset - (std::int64_t) std::llround (1.61638 * lynx::ppq)) <= 1);
  492. // REA_rodada110 §1: the sub-tick rest is kept (offset + fraction = the exact position, to the sample)
  493. 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;
  494. CHECK (std::abs ((double) song.playlist[0].offset + (double) song.playlist[0].offsetFraction - exact) < 1e-3);
  495. CHECK (std::abs (song.playlist[0].offsetFraction) <= 0.5f);
  496. }
  497. TEST (flp_parses_project_model)
  498. {
  499. FlpBuilder b (96);
  500. b.ascii (0xC7, "25.2.5.5319");
  501. b.u8 (0x9C); b.u32 (174500);
  502. b.utf16 (0xC2, u"Título");
  503. b.u8 (0x41); b.u16 (3);
  504. b.u8 (0x96); b.u32 (0x00A14547); // BGR in the file
  505. auto notes = noteRecord (96, 1, 48, 64, 100);
  506. auto n2 = noteRecord (0, 0, 0, 60, 128);
  507. notes.insert (notes.end(), n2.begin(), n2.end());
  508. b.data (0xE0, notes);
  509. b.u8 (0x40); b.u16 (0);
  510. b.u8 (0x15); b.u8 (0);
  511. b.utf16 (0xCB, u"Kick");
  512. {
  513. std::vector<std::uint8_t> levels (24, 0);
  514. const std::int32_t lv[] = { 3200, 12800, -150 };
  515. std::memcpy (levels.data(), lv, sizeof (lv));
  516. b.data (0xDB, levels);
  517. }
  518. b.u8 (0x68); b.u16 (5);
  519. b.u8 (0x40); b.u16 (1);
  520. b.u8 (0x15); b.u8 (2);
  521. b.utf16 (0xC9, u"FLEX");
  522. b.utf16 (0xCB, u"Lead");
  523. b.u8 (0x63); b.u16 (0);
  524. std::vector<std::uint8_t> item (80, 0);
  525. std::uint32_t pos = 384, len = 768;
  526. std::uint16_t base = 20480, idx = 20480 + 3, rv = 499 - 2;
  527. std::memcpy (&item[0], &pos, 4);
  528. std::memcpy (&item[4], &base, 2);
  529. std::memcpy (&item[6], &idx, 2);
  530. std::memcpy (&item[8], &len, 4);
  531. std::memcpy (&item[12], &rv, 2);
  532. b.data (0xE9, item);
  533. b.utf16 (0xCC, u"Drums");
  534. b.data (0xEC, std::vector<std::uint8_t> (12, 0));
  535. b.data (0xEC, std::vector<std::uint8_t> (12, 0));
  536. const auto p = flp::parseProject (b.bytes);
  537. CHECK (p.version == "25.2.5.5319");
  538. CHECK (std::abs (p.tempo - 174.5) < 1e-9);
  539. CHECK (p.title == "Título");
  540. CHECK (p.channels.size() == 2);
  541. CHECK (p.channels[0].name == "Kick" && p.channels[0].mixerTrack == 5);
  542. CHECK (p.channels[0].hasLevels && p.channels[0].pan == 3200 && p.channels[0].volume == 12800 && p.channels[0].pitchCents == -150);
  543. CHECK (p.channels[1].type == flp::ChannelType::instrument && p.channels[1].pluginName == "FLEX");
  544. CHECK (p.patterns.size() == 1 && p.patterns[0].index == 3);
  545. CHECK (p.patterns[0].colour && *p.patterns[0].colour == 0x4745A1);
  546. CHECK (p.patterns[0].notes.size() == 2 && p.patterns[0].notes[0].position == 0); // sorted
  547. CHECK (p.patterns[0].notes[1].key == 64 && p.patterns[0].notes[1].length == 48);
  548. CHECK (p.arrangements.size() == 1 && p.arrangements[0].items.size() == 1);
  549. const auto& it = p.arrangements[0].items[0];
  550. CHECK (it.kind == flp::PlaylistItem::Kind::pattern && it.index == 3 && it.track == 2 && it.position == 384);
  551. CHECK (p.mixer.size() == 2 && p.mixer[0].name == "Drums" && p.mixer[1].name.empty());
  552. CHECK (p.warnings.empty());
  553. }
  554. TEST (flp_fuzz_corrupted_files_do_not_crash)
  555. {
  556. if (realFlpDir.empty())
  557. return;
  558. // some real .flp files, corrupted in various ways: the parser may reject them (flp::Error), but
  559. // must never hang, read past the buffer or produce a Song with invalid indices
  560. std::vector<std::vector<std::uint8_t>> sources;
  561. for (auto& entry : std::filesystem::recursive_directory_iterator (realFlpDir))
  562. if (entry.path().extension() == ".flp" && sources.size() < 6 && std::filesystem::file_size (entry.path()) < 3'000'000)
  563. {
  564. std::ifstream in (entry.path(), std::ios::binary);
  565. sources.emplace_back ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  566. }
  567. std::uint32_t seed = 2024;
  568. auto rnd = [&] (std::uint32_t n) { seed = seed * 1664525u + 1013904223u; return n == 0 ? 0u : (seed >> 8) % n; };
  569. int parsed = 0, rejected = 0;
  570. for (int round = 0; round < 240 && ! sources.empty(); ++round)
  571. {
  572. auto bytes = sources[(std::size_t) rnd ((std::uint32_t) sources.size())];
  573. switch (round % 4)
  574. {
  575. case 0: bytes.resize (rnd ((std::uint32_t) bytes.size())); break; // truncated
  576. 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;
  577. case 2: // absurd event size
  578. for (int k = 0; k < 5; ++k)
  579. bytes[(std::size_t) rnd ((std::uint32_t) bytes.size())] = 0xFF;
  580. break;
  581. default: // shuffled block
  582. {
  583. const auto a = rnd ((std::uint32_t) bytes.size()), b = rnd ((std::uint32_t) bytes.size());
  584. std::reverse (bytes.begin() + std::min (a, b), bytes.begin() + std::max (a, b));
  585. break;
  586. }
  587. }
  588. try
  589. {
  590. const auto project = flp::parseProject (bytes);
  591. const auto song = lynx::importFlp (project);
  592. ++parsed;
  593. for (const auto& p : song.patterns)
  594. for (const auto& n : p.notes)
  595. CHECK (n.channel >= 0 && n.channel < (int) song.channels.size());
  596. for (const auto& c : song.playlist)
  597. CHECK (c.kind != lynx::ClipKind::pattern || (c.pattern >= 0 && c.pattern < (int) song.patterns.size()));
  598. }
  599. catch (const flp::Error&)
  600. {
  601. ++rejected;
  602. }
  603. }
  604. std::printf (" fuzz: %d imported, %d rejected\n", parsed, rejected);
  605. CHECK (parsed + rejected == 240);
  606. }
  607. TEST (plugin_state_decoders_survive_garbage)
  608. {
  609. // random blobs (varied sizes, including valid headers followed by garbage)
  610. std::uint32_t seed = 77;
  611. auto rnd = [&] { seed = seed * 1664525u + 1013904223u; return seed >> 8; };
  612. const char* plugins[] = { "Fruity Parametric EQ 2", "Fruity Limiter", "Fruity Reeverb 2", "Fruity Delay 2", "Fruity Delay 3",
  613. "Fruity Compressor", "Fruity Balance", "Fruity PanOMatic", "Fruity Stereo Shaper", "Fruity Phase Inverter" };
  614. int decoded = 0;
  615. for (int round = 0; round < 3000; ++round)
  616. {
  617. std::vector<std::uint8_t> blob ((std::size_t) (rnd() % 600));
  618. for (auto& b : blob)
  619. b = (std::uint8_t) rnd();
  620. if (blob.size() >= 4 && round % 3 == 0) // versions the readers accept
  621. {
  622. const std::uint32_t versions[] = { 8, 10001, 10000, 12, 14, 6, 1 };
  623. const auto v = versions[rnd() % 7];
  624. std::memcpy (blob.data(), &v, 4);
  625. }
  626. if (auto fx = lynx::effectFromFl (plugins[rnd() % 10], blob))
  627. {
  628. ++decoded;
  629. for (auto v : fx->params)
  630. CHECK (std::isfinite (v) && v >= 0.0f && v <= 1.0f);
  631. }
  632. lynx::SynthSettings synth;
  633. if (lynx::synthFrom3xOsc (blob, synth))
  634. for (const auto& o : synth.osc)
  635. CHECK (std::isfinite (o.level) && o.level >= 0.0f && o.level <= 1.0f);
  636. float harmlessGain = 1.0f;
  637. if (lynx::synthFromHarmless (blob, 120.0, synth, harmlessGain))
  638. CHECK (std::isfinite (harmlessGain) && std::isfinite (synth.cutoff) && std::isfinite (synth.attack));
  639. if (lynx::synthFromFruitKick (blob, synth))
  640. CHECK (std::isfinite (synth.decay) && std::isfinite (synth.pitchEnv.amount));
  641. if (lynx::synthFromGms (blob, 120.0, synth, harmlessGain) || lynx::synthFromHarmor (blob, synth, harmlessGain) || lynx::synthFromOgun (blob, synth, harmlessGain)
  642. || lynx::synthFromSakura (blob, synth) || lynx::synthFromKepler (blob, synth, harmlessGain))
  643. CHECK (std::isfinite (synth.cutoff) && std::isfinite (synth.attack) && std::isfinite (harmlessGain));
  644. if (auto sound = lynx::drumSynthLiveSound (blob))
  645. CHECK (sound->size() < 256);
  646. if (lynx::wavetrav::State wt; lynx::wavetrav::parseState (blob, wt))
  647. CHECK (wt.patches[60].waveN >= 0 && wt.patches[60].waveN < lynx::wavetrav::tableCapacity);
  648. lynx::synthFromBooBass (blob, synth);
  649. lynx::gmsStateUsable (blob); // (the exact engines' parsers reject garbage without crashing)
  650. lynx::morphineStateUsable (blob);
  651. lynx::poizoneStateUsable (blob);
  652. if (lynx::synthFromPoiZone (blob, synth, harmlessGain) || lynx::synthFromBassDrum (blob, synth, harmlessGain) || lynx::synthFromToxic (blob, synth, harmlessGain)
  653. || lynx::synthFromMorphine (blob, synth, harmlessGain))
  654. CHECK (std::isfinite (synth.cutoff) && std::isfinite (harmlessGain));
  655. if (auto patch = lynx::drumpadPatchName (blob))
  656. CHECK (patch->size() <= 0x30 && std::isfinite (lynx::drumToneForName (*patch).decay));
  657. if (auto flex = lynx::flexPresetName (blob))
  658. CHECK ((! flex->first.empty() || ! flex->second.empty()) && flex->second.size() <= 1024);
  659. if (lynx::synthFromSytrus (blob, 120.0, synth, harmlessGain))
  660. CHECK (std::isfinite (synth.cutoff) && std::isfinite (synth.attack));
  661. if (lynx::synthFromDX10 (blob, synth))
  662. CHECK (std::isfinite (synth.decay) && std::isfinite (synth.release) && std::isfinite (synth.osc[1].level));
  663. if (blob.size() >= 4 && round % 5 == 0) // MiniSynth header followed by garbage
  664. {
  665. std::memcpy (blob.data(), "SSLF", 4);
  666. if (blob.size() > 220)
  667. {
  668. std::memcpy (blob.data() + 40, "DATA", 4);
  669. std::memcpy (blob.data() + 48, "0rev", 4);
  670. }
  671. }
  672. if (lynx::synthFromMiniSynth (blob, 120.0, synth))
  673. CHECK (std::isfinite (synth.cutoff) && std::isfinite (synth.decay) && std::isfinite (synth.attack));
  674. if (lynx::miniSynthStateUsable (blob)) // the exact engine's parser and a short render with silent tables
  675. {
  676. const auto params = lynx::miniSynthParams (blob);
  677. CHECK (params.size() == 43u && std::all_of (params.begin(), params.end(), [] (float v) { return v >= 0.0f && v <= 1.0f; }));
  678. const auto y = lynx::minisynth::renderReference (blob, { { 0, 60, 1.0, 1.0, 0.0, 0.0, 64 } }, 44100.0, 128, "");
  679. CHECK (y.size() == 256u && std::all_of (y.begin(), y.end(), [] (float v) { return std::isfinite (v); }));
  680. }
  681. lynx::ForeignPlugin foreign;
  682. lynx::parseFlWrapper (blob, foreign);
  683. std::vector<std::uint8_t> component, controller;
  684. lynx::splitFlVst3State (blob, component, controller);
  685. }
  686. CHECK (decoded == 3000); // every known name yields an effect (default when the blob is unusable)
  687. }
  688. TEST (inflate_zlib)
  689. {
  690. // fixed Huffman with back-references
  691. const std::uint8_t fixed[] = { 0x78, 0xda, 0x4b, 0x4c, 0x4a, 0x4e, 0xc4, 0x86, 0x22, 0x00, 0x7c, 0x69, 0x09, 0x89 };
  692. const auto a = lynx::inflateZlib (fixed, sizeof (fixed));
  693. CHECK (a && std::string (a->begin(), a->end()) == "abcabcabcabcabcabcabcabcX");
  694. // stored (uncompressed) block
  695. const std::uint8_t stored[] = { 0x78, 0x01, 0x01, 0x03, 0x00, 0xfc, 0xff, 'L', 'Y', 'N', 0x00, 0x00, 0x00, 0x00 };
  696. const auto b = lynx::inflateZlib (stored, sizeof (stored));
  697. CHECK (b && std::string (b->begin(), b->end()) == "LYN");
  698. // truncated, bad header and size limit
  699. CHECK (! lynx::inflateZlib (fixed, 6));
  700. const std::uint8_t bad[] = { 0x12, 0x34, 0x00 };
  701. CHECK (! lynx::inflateZlib (bad, sizeof (bad)));
  702. CHECK (! lynx::inflateZlib (fixed, sizeof (fixed), false, 10));
  703. }
  704. TEST (fl_round15_effects_convert_known_values)
  705. {
  706. auto blob = [] (std::vector<std::int32_t> words) {
  707. std::vector<std::uint8_t> b (words.size() * 4);
  708. std::memcpy (b.data(), words.data(), b.size());
  709. return b;
  710. };
  711. // Frequency Shifter "+800 Hz": frequency 51542 in the 20 kHz range, 100% wet — FL's own engine since round 154
  712. // (its controls as (raw − min) / range); unreadable states keep the generic shifter
  713. const auto fs = lynx::effectFromFl ("Frequency Shifter", blob ({ 4, 10000, 0, 51542, 0, 10000, 10000, 5000, 10000, 0, 5000, 0, 0 }));
  714. CHECK (fs && fs->kind == lynx::EffectKind::flFrequencyShifter);
  715. if (fs)
  716. {
  717. CHECK (std::abs (fs->params[0] - 1.0f) < 1e-6f && fs->params[1] == 0.0f);
  718. CHECK (std::abs (fs->params[2] - (51542.0f + 70000.0f) / 140000.0f) < 1e-6f);
  719. }
  720. const auto fsOld = lynx::effectFromFl ("Frequency Shifter", blob ({ 4, 10000, 0 }));
  721. CHECK (fsOld && fsOld->kind == lynx::EffectKind::freqShift);
  722. // Emphasis (Bien Duro's Master): FL's own engine since round 167f, its controls as (raw − min) / range: input 625
  723. // (+1.5 dB), ceiling 9308 (−1.60 dB), envelope order 2, true peak; an unreadable state keeps the limiter
  724. const auto em = lynx::effectFromFl ("Emphasis", blob ({ 8, 0, 0, 2, 1, 0, 0, 625, 10000, 10000, 9308, 0, 2, 7, 0 }));
  725. CHECK (em && em->kind == lynx::EffectKind::flEmphasis && em->enabled);
  726. if (em)
  727. {
  728. CHECK (std::abs (em->params[6] - 0.0625f) < 1e-6f && std::abs (em->params[9] - 0.9308f) < 1e-6f);
  729. 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);
  730. CHECK (lynx::formatEffectParam (lynx::EffectKind::flEmphasis, 6, em->params[6], 120.0) == "+1.5 dB");
  731. CHECK (lynx::formatEffectParam (lynx::EffectKind::flEmphasis, 9, em->params[9], 120.0) == "-1.60 dB");
  732. CHECK (em->params[14] == 0.0f && em->params[15] == 0.0f); // a version-8 state has no option bytes: off
  733. }
  734. const auto emOld = lynx::effectFromFl ("Emphasis", blob ({ 8, 0, 0, 2 }));
  735. CHECK (emOld && emOld->kind == lynx::EffectKind::limiter);
  736. // round 167f: FL's legacy dB Meter / Spectroman keep their slot as LYNX's analyser in the matching view
  737. const auto dbm = lynx::effectFromFl ("Fruity dB Meter", {});
  738. CHECK (dbm && dbm->kind == lynx::EffectKind::waveCandy
  739. && dbm->params[0] == lynx::visualfx::wavecandy::modeValue (lynx::visualfx::wavecandy::Mode::peakMeter));
  740. const auto spm = lynx::effectFromFl ("Fruity Spectroman", {});
  741. CHECK (spm && spm->kind == lynx::EffectKind::waveCandy
  742. && spm->params[0] == lynx::visualfx::wavecandy::modeValue (lynx::visualfx::wavecandy::Mode::spectrum));
  743. // Pitch Shifter +12 st, mix 100%
  744. 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 }));
  745. CHECK (ps && ps->kind == lynx::EffectKind::flPitchShifter && ps->params[0] == 1.0f && ps->params[3] == 1.0f); // REA_rodada61
  746. }
  747. TEST (frequency_shifter_moves_spectrum_by_hz)
  748. {
  749. auto fx = lynx::makeEffect (lynx::EffectKind::freqShift);
  750. fx->prepare (48000.0);
  751. auto slot = lynx::makeEffectSlot (lynx::EffectKind::freqShift);
  752. // +200 Hz: x·|x|·2000 = 200 -> x = sqrt(0.1)
  753. slot.params[0] = (float) (0.5 + std::sqrt (0.1) / 2.0);
  754. std::vector<float> l (48000), r (48000);
  755. for (int i = 0; i < 48000; ++i)
  756. l[(std::size_t) i] = r[(std::size_t) i] = (float) std::sin (2.0 * 3.141592653589793 * 1000.0 * i / 48000.0);
  757. for (int done = 0; done < 48000; done += 480)
  758. fx->process (l.data() + done, r.data() + done, 480, slot.params, 120.0);
  759. auto energy = [&] (double f) {
  760. const auto w = 2.0 * 3.141592653589793 * f / 48000.0, k = 2.0 * std::cos (w);
  761. double s1 = 0.0, s2 = 0.0;
  762. for (int i = 9600; i < 48000; ++i)
  763. {
  764. const auto s0 = l[(std::size_t) i] + k * s1 - s2;
  765. s2 = s1;
  766. s1 = s0;
  767. }
  768. return s1 * s1 + s2 * s2 - k * s1 * s2;
  769. };
  770. // the energy moves to 1200 Hz; little remains at 1000 and almost nothing at the image (800)
  771. CHECK (energy (1200.0) > 50.0 * energy (1000.0));
  772. CHECK (energy (1200.0) > 50.0 * energy (800.0));
  773. }
  774. TEST (pitch_shift_moves_frequency_keeps_length)
  775. {
  776. auto fx = lynx::makeEffect (lynx::EffectKind::pitchShift);
  777. fx->prepare (48000.0);
  778. auto slot = lynx::makeEffectSlot (lynx::EffectKind::pitchShift);
  779. auto crossingsAfter = [&] (float semitoneParam) {
  780. fx->reset();
  781. slot.params[0] = semitoneParam;
  782. std::vector<float> l (48000), r (48000);
  783. for (int i = 0; i < 48000; ++i)
  784. l[(std::size_t) i] = r[(std::size_t) i] = (float) std::sin (2.0 * 3.141592653589793 * 220.0 * i / 48000.0);
  785. for (int done = 0; done < 48000; done += 512)
  786. fx->process (l.data() + done, r.data() + done, std::min (512, 48000 - done), slot.params, 120.0);
  787. int crossings = 0;
  788. for (int i = 9600; i < 48000; ++i)
  789. crossings += (l[(std::size_t) i - 1] < 0.0f) != (l[(std::size_t) i] < 0.0f);
  790. for (auto v : l)
  791. CHECK (std::isfinite (v) && std::abs (v) < 1.6f);
  792. return crossings / 0.8 / 2.0; // cycles per second
  793. };
  794. CHECK (std::abs (crossingsAfter (0.5f) - 220.0) < 8.0); // 0 st
  795. CHECK (std::abs (crossingsAfter (0.5f + 12.0f / 48.0f) - 440.0) < 25.0); // +12 st
  796. CHECK (std::abs (crossingsAfter (0.5f - 12.0f / 48.0f) - 110.0) < 12.0); // −12 st
  797. }
  798. // A Gross Beat state in FL's layout (REA_rodada53 §1), built here: version 8, parameters, options, 72 slots with a name,
  799. // sync bytes and a map envelope (kind 3, version 3, points: Δx in beats, level, tension, curve type)
  800. namespace
  801. {
  802. struct GbPoint
  803. {
  804. double dx, y;
  805. std::uint8_t type;
  806. };
  807. std::vector<std::uint8_t> grossBeatState (const std::array<int, 9>& params, const std::array<std::uint8_t, 9>& options,
  808. const std::map<int, std::vector<GbPoint>>& slots)
  809. {
  810. std::vector<std::uint8_t> b;
  811. 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); };
  812. auto i32 = [&put] (std::int32_t v) { put (&v, 4); };
  813. i32 (0x00080003);
  814. for (auto v : params)
  815. i32 (v);
  816. put (options.data(), options.size());
  817. for (int k = 0; k < 72; ++k)
  818. {
  819. b.push_back (0); // empty name
  820. i32 (0), i32 (5), i32 (0); // trigger sync off, position sync 4 beats, no hold
  821. b.push_back (0), b.push_back (0), b.push_back (0);
  822. const auto it = slots.find (k);
  823. const std::vector<GbPoint> flat { { 0.0, 1.0, 0 }, { 4.0, 1.0, 0 } };
  824. const auto& pts = it != slots.end() ? it->second : flat;
  825. i32 (3), i32 (3), i32 ((std::int32_t) pts.size());
  826. for (const auto& p : pts)
  827. {
  828. put (&p.dx, 8), put (&p.y, 8);
  829. const float tension = 0.0f;
  830. put (&tension, 4);
  831. b.push_back (p.type), b.push_back (0), b.push_back (0), b.push_back (2);
  832. }
  833. i32 (0), i32 (-1), i32 (-1), i32 (-1), i32 (-1);
  834. }
  835. return b;
  836. }
  837. std::vector<float> renderGrossBeat (const lynx::EffectSlot& slot, const std::vector<float>& in, double rate, double bpm)
  838. {
  839. auto fx = lynx::makeEffect (slot);
  840. fx->prepare (rate);
  841. auto l = in, r = in;
  842. const auto tps = bpm / 60.0 * lynx::ppq / rate;
  843. for (std::size_t done = 0; done < l.size(); done += 512)
  844. {
  845. const auto n = (int) std::min<std::size_t> (512, l.size() - done);
  846. fx->setSongPosition ((double) done * tps);
  847. fx->process (l.data() + done, r.data() + done, n, slot.params, bpm);
  848. }
  849. return l;
  850. }
  851. } // namespace
  852. TEST (gross_beat_state_container_and_parameters)
  853. {
  854. const auto state = grossBeatState ({ 3, 40, 128, 256, 1765, 5000, -77, 0, 0x10000 }, { 0, 0, 0, 0, 0, 0, 0, 1, 1 }, {});
  855. const auto parsed = lynx::grossbeat::parseState (state.data(), state.size());
  856. CHECK (parsed.has_value());
  857. CHECK (parsed && parsed->version == 8 && parsed->params[6] == -77 && parsed->slots[0].flat && parsed->slots[71].flat);
  858. const auto slot = lynx::grossbeat::slotFromFl (state, 960);
  859. CHECK (slot && slot->kind == lynx::EffectKind::grossBeat);
  860. std::vector<std::uint8_t> fl;
  861. int ppq = 0;
  862. CHECK (slot && lynx::grossbeat::unpack (slot->state, fl, ppq) && fl == state && ppq == 960);
  863. // every raw FL value survives the 0..1 parameter
  864. const int lo[8] { 0, 0, 0, 0, 0, 0, -256, 0 }, hi[8] { 35, 35, 256, 256, 10000, 10000, 256, 1 };
  865. bool roundTrip = true;
  866. for (int i = 0; i < 8; ++i)
  867. for (int raw = lo[i]; raw <= hi[i]; ++raw)
  868. roundTrip = roundTrip && lynx::grossbeat::rawFromParam (i, lynx::grossbeat::paramFromRaw (i, raw)) == raw;
  869. CHECK (roundTrip);
  870. CHECK (slot && slot->params[1] == lynx::grossbeat::paramFromRaw (1, 35)); // the volume slot 40 is clamped to 35
  871. CHECK (! lynx::grossbeat::parseState (state.data(), 100).has_value());
  872. }
  873. TEST (gross_beat_volume_slot_gates_the_bar)
  874. {
  875. // volume slot 1: open for two beats, closed for two (hold segments); no smoothing; dry path 15 samples late
  876. 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 } };
  877. const auto state = grossBeatState ({ 0, 1, 256, 256, 0, 0, 0, 0, 0x10000 }, { 0, 0, 0, 0, 0, 0, 0, 1, 1 }, { { 37, gate } });
  878. const auto slot = lynx::grossbeat::slotFromFl (state);
  879. CHECK (slot.has_value());
  880. if (! slot)
  881. return;
  882. const auto out = renderGrossBeat (*slot, std::vector<float> (200000, 1.0f), 48000.0, 120.0); // a beat = 24000 samples
  883. CHECK (out[14] == 0.0f && out[15] == 1.0f);
  884. CHECK (out[30000] == 1.0f && out[47990] == 1.0f);
  885. CHECK (out[48010] == 0.0f && out[95990] == 0.0f);
  886. CHECK (out[96010] == 1.0f && out[150000] == 0.0f);
  887. }
  888. TEST (gross_beat_time_slot_reads_the_past_bar)
  889. {
  890. // time slot 1 at level 0.5: the buffer is read one bar back (two bars = level 0), plus the 15-sample latency
  891. const std::vector<GbPoint> half { { 0.0, 0.5, 0 }, { 4.0, 0.5, 2 } };
  892. const auto state = grossBeatState ({ 1, 0, 256, 256, 0, 0, 0, 0, 0x10000 }, { 0, 0, 0, 0, 0, 0, 0, 1, 1 }, { { 1, half } });
  893. const auto slot = lynx::grossbeat::slotFromFl (state);
  894. CHECK (slot.has_value());
  895. if (! slot)
  896. return;
  897. std::vector<float> in (150000, 0.0f);
  898. in[1000] = 1.0f;
  899. const auto out = renderGrossBeat (*slot, in, 48000.0, 120.0); // a bar = 96000 samples
  900. std::size_t peak = 0;
  901. for (std::size_t i = 0; i < out.size(); ++i)
  902. if (std::abs (out[i]) > std::abs (out[peak]))
  903. peak = i;
  904. CHECK (peak == 1000 + 96000 + 15);
  905. CHECK (std::abs (out[peak] - 1.0f) < 1e-6f);
  906. float elsewhere = 0.0f;
  907. for (std::size_t i = 0; i < out.size(); ++i)
  908. if (i + 20 < peak || i > peak + 20)
  909. elsewhere = std::max (elsewhere, std::abs (out[i]));
  910. CHECK (elsewhere == 0.0f);
  911. }
  912. TEST (volume_gate_follows_bar_position)
  913. {
  914. auto fx = lynx::makeEffect (lynx::EffectKind::volumeGate);
  915. fx->prepare (48000.0);
  916. auto slot = lynx::makeEffectSlot (lynx::EffectKind::volumeGate);
  917. for (int i = 0; i < 32; ++i)
  918. slot.params[(std::size_t) i] = i < 16 ? 1.0f : 0.0f; // first two beats open, two closed
  919. slot.params[33] = slot.params[34] = 0.0f; // no smoothing
  920. // 120 BPM: one beat = 0.5 s = 24000 samples; the 4-beat bar starts at tick 0
  921. std::vector<float> l (96000, 1.0f), r (96000, 1.0f);
  922. fx->setSongPosition (0.0);
  923. for (int done = 0; done < 96000; done += 512)
  924. fx->process (l.data() + done, r.data() + done, std::min (512, 96000 - done), slot.params, 120.0);
  925. CHECK (l[10000] > 0.99f && l[40000] > 0.99f); // beats 1 and 2
  926. CHECK (l[60000] < 0.01f && l[90000] < 0.01f); // beats 3 and 4
  927. // starting mid-bar (beat 3): already closed
  928. std::vector<float> l2 (4800, 1.0f), r2 (4800, 1.0f);
  929. fx->reset();
  930. fx->setSongPosition (2.0 * lynx::ppq);
  931. fx->process (l2.data(), r2.data(), 4800, slot.params, 120.0);
  932. CHECK (l2[4000] < 0.01f);
  933. // mix 0: transparent
  934. slot.params[32] = 0.0f;
  935. std::vector<float> l3 (4800, 1.0f), r3 (4800, 1.0f);
  936. fx->setSongPosition (2.0 * lynx::ppq);
  937. fx->process (l3.data(), r3.data(), 4800, slot.params, 120.0);
  938. CHECK (l3[4000] > 0.99f);
  939. }
  940. TEST (patcher_branch_heads_follow_rea)
  941. {
  942. auto words = [] (std::vector<std::int32_t> v) {
  943. std::vector<std::uint8_t> b (v.size() * 4);
  944. std::memcpy (b.data(), v.data(), b.size());
  945. return b;
  946. };
  947. // Frequency Splitter, 2 bands, destinations 1/2/3, slope combo 3 (24 dB/oct), high send −6 dB
  948. std::vector<std::int32_t> fs (18, 0);
  949. fs[0] = 4;
  950. fs[1] = 32768;
  951. fs[3] = 3;
  952. fs[6] = 0;
  953. fs[11] = -600;
  954. fs[15] = 1;
  955. fs[16] = 2;
  956. fs[17] = 3;
  957. const auto split = words (fs);
  958. const auto low = lynx::patcherBranchHead ("Frequency Splitter", split, 1);
  959. CHECK (low.valid && low.slot && low.note.empty());
  960. if (low.slot)
  961. {
  962. // the exact engine (REA_rodada62): the low band's send, 24 dB/oct
  963. CHECK (low.slot->kind == lynx::EffectKind::frequencySplitter && low.slot->params[26] == 1.0f && low.slot->params[27] == 0.0f);
  964. CHECK (low.slot->params[2] == 3.0f / 7.0f);
  965. }
  966. const auto mid = lynx::patcherBranchHead ("Frequency Splitter", split, 2);
  967. 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
  968. const auto high = lynx::patcherBranchHead ("Frequency Splitter", split, 3);
  969. CHECK (high.valid && high.slot && high.slot->params[28] == 1.0f && high.slot->params[10] == (float) (1200.0 / 3600.0));
  970. CHECK (! lynx::patcherBranchHead ("Frequency Splitter", split, 7).valid);
  971. // Stereo Shaper: the difference destination (4) is the exact engine with its difference output (REA_rodada54)
  972. const auto shaper = words ({ 3, 0, 12800, 12800, 0, 0, 0, 4, 0 });
  973. const auto diff = lynx::patcherBranchHead ("Fruity Stereo Shaper", shaper, 4);
  974. CHECK (diff.valid && diff.slot && diff.slot->kind == lynx::EffectKind::stereoShaper && diff.slot->params[7] == 1.0f);
  975. CHECK (! lynx::patcherBranchHead ("Fruity Stereo Shaper", shaper, 2).valid);
  976. }
  977. TEST (parallel_container_sums_branches)
  978. {
  979. // two branches: dry with gain 0.5 and a utility that flips phase (matrix −1) with gain 0.5 -> silence
  980. auto slot = lynx::makeEffectSlot (lynx::EffectKind::parallel);
  981. lynx::EffectBranch dry;
  982. dry.gain = 0.5f;
  983. lynx::EffectBranch inverted;
  984. inverted.gain = 0.5f;
  985. auto flip = lynx::makeEffectSlot (lynx::EffectKind::utility);
  986. flip.params[2] = flip.params[5] = 0.25f; // (−1 + 2)/4
  987. inverted.chain.push_back (flip);
  988. slot.branches = { dry, inverted };
  989. auto fx = lynx::makeParallelEffect (slot);
  990. fx->prepare (48000.0);
  991. std::vector<float> l (9000, 0.8f), r (9000, -0.4f);
  992. fx->process (l.data(), r.data(), 9000, slot.params, 120.0);
  993. CHECK (std::abs (l[100]) < 1e-5f && std::abs (r[8999]) < 1e-5f);
  994. // only the dry branch with gain 2: doubles
  995. slot.branches = { lynx::EffectBranch { {}, 2.0f } };
  996. auto doubler = lynx::makeParallelEffect (slot);
  997. doubler->prepare (48000.0);
  998. std::fill (l.begin(), l.end(), 0.25f);
  999. doubler->process (l.data(), r.data(), 9000, slot.params, 120.0);
  1000. CHECK (std::abs (l[5000] - 0.5f) < 1e-6f);
  1001. }
  1002. TEST (fl_controller_constant_outputs)
  1003. {
  1004. std::vector<std::int32_t> xy { 2, 32768, 0, 65536, 0, 16384, 0, 65536, 0, 1024, 256 };
  1005. std::vector<std::uint8_t> blob (xy.size() * 4 + 8, 0);
  1006. std::memcpy (blob.data(), xy.data(), xy.size() * 4);
  1007. CHECK (std::abs (lynx::controllerConstantOutput ("Fruity X-Y Controller", blob, 0).value_or (-1.0f) - 0.5f) < 1e-4f);
  1008. CHECK (std::abs (lynx::controllerConstantOutput ("Fruity X-Y Controller", blob, 1).value_or (-1.0f) - 0.25f) < 1e-4f);
  1009. // Control Surface: version + group { 0x836: value 0.75, -, output 2 }
  1010. std::vector<std::uint8_t> cs (4, 0);
  1011. cs[0] = 1;
  1012. auto chunk = [] (std::vector<std::uint8_t>& out, std::uint32_t id, const std::vector<std::uint8_t>& body) {
  1013. const auto size = (std::uint64_t) body.size();
  1014. out.insert (out.end(), reinterpret_cast<const std::uint8_t*> (&id), reinterpret_cast<const std::uint8_t*> (&id) + 4);
  1015. out.insert (out.end(), reinterpret_cast<const std::uint8_t*> (&size), reinterpret_cast<const std::uint8_t*> (&size) + 8);
  1016. out.insert (out.end(), body.begin(), body.end());
  1017. };
  1018. std::vector<std::uint8_t> value (12, 0);
  1019. const float v = 0.75f;
  1020. const std::int32_t out = 2;
  1021. std::memcpy (value.data(), &v, 4);
  1022. std::memcpy (value.data() + 8, &out, 4);
  1023. std::vector<std::uint8_t> group;
  1024. chunk (group, 0x836, value);
  1025. chunk (cs, 0, group);
  1026. CHECK (std::abs (lynx::controllerConstantOutput ("Control Surface", cs, 2).value_or (-1.0f) - 0.75f) < 1e-6f);
  1027. CHECK (! lynx::controllerConstantOutput ("Control Surface", cs, 0).has_value());
  1028. }
  1029. TEST (flp_midi_out_notes_reach_wrapper_on_same_port)
  1030. {
  1031. auto record = [] (std::vector<std::uint8_t>& out, std::uint32_t id, std::vector<std::uint8_t> body) {
  1032. const auto size = (std::uint64_t) body.size();
  1033. out.insert (out.end(), reinterpret_cast<const std::uint8_t*> (&id), reinterpret_cast<const std::uint8_t*> (&id) + 4);
  1034. out.insert (out.end(), reinterpret_cast<const std::uint8_t*> (&size), reinterpret_cast<const std::uint8_t*> (&size) + 8);
  1035. out.insert (out.end(), body.begin(), body.end());
  1036. };
  1037. auto i32s = [] (std::vector<std::int32_t> v) {
  1038. std::vector<std::uint8_t> b (v.size() * 4);
  1039. std::memcpy (b.data(), v.data(), b.size());
  1040. return b;
  1041. };
  1042. flp::Project p;
  1043. flp::Channel midiOut;
  1044. midiOut.index = 0;
  1045. midiOut.type = flp::ChannelType::native;
  1046. midiOut.pluginName = "MIDI Out";
  1047. midiOut.pluginState = i32s ({ 7, 0, -1, -1, -1, 0, 3, 0 }); // channel 1, port 3 (@24)
  1048. flp::Channel wrapper;
  1049. wrapper.index = 1;
  1050. wrapper.type = flp::ChannelType::native;
  1051. wrapper.pluginName = "Fruity Wrapper";
  1052. wrapper.pluginState = i32s ({ 9 });
  1053. record (wrapper.pluginState, 0x32, i32s ({ 7 })); // VST3
  1054. record (wrapper.pluginState, 0x01, i32s ({ 3, -1, 0, 0, 0 })); // input port 3
  1055. p.channels = { midiOut, wrapper };
  1056. flp::Pattern pattern;
  1057. pattern.index = 1;
  1058. flp::Note note;
  1059. note.channel = 0;
  1060. note.key = 64;
  1061. note.length = 96;
  1062. pattern.notes.push_back (note);
  1063. p.patterns.push_back (pattern);
  1064. const auto song = lynx::importFlp (p);
  1065. // round 133: the MIDI Out channel stays a live route (its notes are sent at play time, with its MIDI channel)
  1066. CHECK (song.channels.size() == 2 && song.channels[0].instrument == lynx::InstrumentKind::midiOut && ! song.channels[0].muted);
  1067. CHECK (song.channels[1].plugin.foreign.midiInPort == 3 && song.channels[1].plugin.midiInPort == 3);
  1068. CHECK (lynx::midictl::parseMidiOut (song.channels[0].pluginState).port == 3);
  1069. int toWrapper = 0;
  1070. for (const auto& pat : song.patterns)
  1071. for (const auto& n : pat.notes)
  1072. toWrapper += n.channel == 1 && n.key == 64;
  1073. CHECK (toWrapper == 0);
  1074. }
  1075. TEST (flp_effect_automation_follows_slots_and_params)
  1076. {
  1077. // track 1: slot 0 with an effect that has no equivalent (dropped), slot 2 with Parametric EQ 2
  1078. flp::Project p;
  1079. flp::MixerInsert insert;
  1080. insert.index = 1;
  1081. insert.slots.push_back ({ 0, "Some Unknown FX", {} });
  1082. insert.slots.push_back ({ 2, "Fruity Parametric EQ 2", {} });
  1083. p.mixer.push_back (insert);
  1084. auto clip = [&] (int index, std::uint32_t target) {
  1085. flp::Channel c;
  1086. c.index = index;
  1087. c.type = flp::ChannelType::automationClip;
  1088. c.automation = { { 0.0, 0.0 }, { 96.0, 1.0 } };
  1089. p.channels.push_back (c);
  1090. p.automationLinks.push_back ({ index, { target } });
  1091. };
  1092. 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; };
  1093. clip (0, mixer (1, 2, 0x8000 + 7)); // EQ band 1 frequency
  1094. clip (1, mixer (1, 2, 0x1F01)); // EQ slot mix
  1095. clip (2, mixer (1, 0, 0x8000)); // parameter of the dropped effect
  1096. clip (3, mixer (1, 2, 0x8000 + 21)); // band type: no equivalent
  1097. const auto song = lynx::importFlp (p);
  1098. CHECK (song.automations.size() == 4);
  1099. CHECK (song.mixer[1].effects.size() == 1 && song.mixer[1].effects[0].kind == lynx::EffectKind::parametricEq);
  1100. const auto& freq = song.automations[0];
  1101. CHECK (freq.target.kind == lynx::AutomationTargetKind::effectParam && freq.target.index == 1 && freq.target.slot == 0);
  1102. CHECK (freq.target.param == lynx::peq::param (0, 1));
  1103. // FL's 0..1 (20 Hz..20 kHz) becomes our frequency scale
  1104. CHECK (std::abs (freq.points.front().value - lynx::peq::hzValue (20.0)) < 1e-4f);
  1105. CHECK (std::abs (freq.points.back().value - lynx::peq::hzValue (20000.0)) < 1e-4f);
  1106. CHECK (song.automations[1].target.kind == lynx::AutomationTargetKind::effectMix && song.automations[1].target.slot == 0);
  1107. CHECK (song.automations[2].target.kind == lynx::AutomationTargetKind::none);
  1108. CHECK (song.automations[3].target.kind == lynx::AutomationTargetKind::none);
  1109. // the automated mix is read/written through the target
  1110. auto copy = song;
  1111. CHECK (copy.setTargetValue (song.automations[1].target, 0.25f) && std::abs (copy.mixer[1].effects[0].mix - 0.25f) < 1e-6f);
  1112. CHECK (std::abs (copy.targetValue (song.automations[1].target) - 0.25f) < 1e-6f);
  1113. }
  1114. TEST (flp_real_files)
  1115. {
  1116. if (realFlpDir.empty())
  1117. {
  1118. std::printf (" (skipped: pass a folder with .flp files as argument)\n");
  1119. return;
  1120. }
  1121. int files = 0, withNotes = 0, withPlaylist = 0, automations = 0, automationsWithTarget = 0, sends = 0;
  1122. int threeOsc = 0, threeOscDecoded = 0, wrapped = 0, wrappedParsed = 0, wrappedVst2 = 0, wrappedVst2State = 0, wrappedVst2Old = 0, markers = 0, signatures = 0, trackEqs = 0;
  1123. int harmless = 0, harmlessDecoded = 0, harmlessParsed = 0, harmlessKind = 0, sytrus = 0, sytrusDecoded = 0, sytrusEngine = 0, sytrusRendered = 0,
  1124. sytrusSounding = 0;
  1125. int harmor = 0, harmorUsable = 0, harmorKind = 0, harmorSounding = 0;
  1126. int gms = 0, gmsUsable = 0, gmsKind = 0, gmsSounding = 0, morphine = 0, morphineUsable = 0, morphineKind = 0, morphineSounding = 0, demoGoldens = 0;
  1127. int ogun = 0, ogunUsable = 0, ogunKind = 0, ogunSounding = 0;
  1128. int autogun = 0, autogunUsable = 0, autogunKind = 0, autogunWithSg = 0, autogunSounding = 0; // REA_rodada57
  1129. int poizone = 0, poizoneUsable = 0, poizoneKind = 0, poizoneSounding = 0, poizoneGoldens = 0;
  1130. int miniSynth = 0, miniSynthUsable = 0, miniSynthKind = 0, miniSynthSounding = 0, miniSynthGoldens = 0;
  1131. int dx10 = 0, dx10Usable = 0, dx10Kind = 0, dx10Sounding = 0;
  1132. int kepler = 0, keplerUsable = 0, keplerKind = 0, keplerSounding = 0;
  1133. int toxic = 0, toxicUsable = 0, toxicKind = 0, toxicSounding = 0, toxicGoldens = 0; // REA_rodada45
  1134. int threeOscKind = 0, threeOscSounding = 0;
  1135. std::map<std::string, std::pair<int, int>> generators; // GMS/Ogun/Autogun/Sakura/DrumSynth: channels, decoded
  1136. int ogunTrumpet = 0; // Ogun channels whose spectrum matches the REA "Trumpet" reference
  1137. int peqSlots = 0, peqShaped = 0, reverbs = 0, reverbsDryless = 0, reverbsLowCut = 0;
  1138. int dwChannels = 0, dwZones = 0, fpcKits = 0, padChannels = 0, padNotes = 0, dwExact = 0;
  1139. int harmlessCompWanted = 0, harmlessCompLoaded = 0; // Harmless rack compressors / presets read from the installation
  1140. int maximusSlots = 0, maximusExact = 0, sgzSlots = 0, sgzExact = 0, vocodexSlots = 0, vocodexWithData = 0; // REA_rodada40
  1141. int shaperSlots = 0, shaperExact = 0, clipperSlots = 0, clipperExact = 0; // REA_rodada51
  1142. int flPeqSlots = 0, flPeqExact = 0; // REA_rodada50
  1143. int basicSlots = 0, basicExact = 0; // REA_rodada58: PanOMatic, Balance, Blood Overdrive
  1144. int equoSlots = 0, equoExact = 0, pitchShifterSlots = 0, pitchShifterExact = 0; // REA_rodada61
  1145. int filterSlots = 0, filterExact = 0; // REA_rodada63: Fast LP, Free Filter, Fruity Filter
  1146. int distructorSlots = 0, distructorExact = 0, distructorImpulses = 0; // REA_rodada71
  1147. int delay2Slots = 0, delay2Exact = 0, delay3Slots = 0, delay3Exact = 0; // REA_rodada52
  1148. int chorusSlots = 0, chorusExact = 0; // REA_rodada59: the exact choruses
  1149. int fastDistSlots = 0, fastDistExact = 0, flangusSlots = 0, flangusExact = 0, flangerSlots = 0, flangerExact = 0; // REA_rodada55
  1150. int phaserSlots = 0, phaserExact = 0; // REA_rodada105_phaser
  1151. // import with the installation's samples (realFlpDir is ...\Data): for DirectWave/FPC
  1152. lynx::FlpImportOptions withFiles;
  1153. const auto flRoot = std::filesystem::path (realFlpDir).parent_path().string();
  1154. withFiles.roots = { flRoot, realFlpDir, "" };
  1155. withFiles.fileExists = [] (const std::string& path) {
  1156. auto local = path;
  1157. std::replace (local.begin(), local.end(), '\\', '/');
  1158. return std::filesystem::exists (local);
  1159. };
  1160. withFiles.readFile = [] (const std::string& path) {
  1161. auto local = path;
  1162. std::replace (local.begin(), local.end(), '\\', '/');
  1163. std::ifstream in (local, std::ios::binary);
  1164. return std::vector<std::uint8_t> ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  1165. };
  1166. int embeddedSaved = 0;
  1167. withFiles.saveEmbedded = [&embeddedSaved] (const std::vector<std::uint8_t>& wav, const std::string&) {
  1168. ++embeddedSaved;
  1169. return wav.size() > 44 ? std::string ("embutido.wav") : std::string();
  1170. };
  1171. int slicedChannels = 0, flKeysChannels = 0;
  1172. for (auto& entry : std::filesystem::recursive_directory_iterator (realFlpDir))
  1173. {
  1174. if (entry.path().extension() != ".flp")
  1175. continue;
  1176. std::ifstream in (entry.path(), std::ios::binary);
  1177. std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  1178. ++files;
  1179. try
  1180. {
  1181. const auto p = flp::parseProject (bytes);
  1182. CHECK (! p.version.empty());
  1183. CHECK (p.tempo > 10.0 && p.tempo < 1000.0);
  1184. const auto song = lynx::importFlp (p);
  1185. CHECK (! song.patterns.empty());
  1186. {
  1187. const auto full = lynx::importFlp (p, withFiles);
  1188. bool hasFpc = false;
  1189. for (const auto& c : p.channels)
  1190. hasFpc = hasFpc || c.pluginName == "FPC";
  1191. // GMS and Morphine (REA_rodada39): every state plays on the exact engine (GMS with its waves from the
  1192. // installation) and sounds; two demo channels match golden samples of our round-39 models
  1193. for (const auto& c : p.channels)
  1194. {
  1195. if (c.pluginName == "GMS")
  1196. ++gms, gmsUsable += lynx::gmsStateUsable (c.pluginState) ? 1 : 0;
  1197. if (c.pluginName == "Morphine")
  1198. ++morphine, morphineUsable += lynx::morphineStateUsable (c.pluginState) ? 1 : 0;
  1199. }
  1200. // Kepler: every state in the demos plays on the exact engine (REA_rodada46) when the engine DLL is installed
  1201. for (const auto& c : p.channels)
  1202. if (c.pluginName == "Kepler")
  1203. {
  1204. ++kepler;
  1205. keplerUsable += lynx::keplerStateUsable (c.pluginState) ? 1 : 0;
  1206. }
  1207. for (const auto& c : full.channels)
  1208. if (c.instrument == lynx::InstrumentKind::kepler)
  1209. {
  1210. ++keplerKind;
  1211. 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 } },
  1212. 44100.0, 11025);
  1213. double e = 0.0;
  1214. bool finite = ! y.empty();
  1215. for (auto v : y)
  1216. finite = finite && std::isfinite (v), e += (double) v * v;
  1217. CHECK (finite);
  1218. keplerSounding += e > 1e-6 ? 1 : 0;
  1219. }
  1220. const auto file = entry.path().filename().string();
  1221. for (const auto& c : full.channels)
  1222. {
  1223. const auto isGms = c.instrument == lynx::InstrumentKind::gms, isMorphine = c.instrument == lynx::InstrumentKind::morphine;
  1224. if (! isGms && ! isMorphine)
  1225. continue;
  1226. (isGms ? gmsKind : morphineKind)++;
  1227. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  1228. const auto golden = (isGms && c.name == "GMS" && file.find ("Song For You") != std::string::npos)
  1229. || (isMorphine && c.name == "Morphine" && file.find ("SHINE") != std::string::npos);
  1230. std::vector<float> y;
  1231. if (isGms)
  1232. y = lynx::gms::renderReference (c.pluginState,
  1233. { { 0, 60, (float) iv (100), 1.0, 0.0, 0.0, 30000 }, { 11025, 64, (float) iv (80), 1.0, 0.0, 0.0, 40000 },
  1234. { 22050, 67, (float) iv (110), 1.0, 0.0, 0.0, 52000 }, { 60000, 48, (float) iv (127), 1.0, 0.0, 0.0, 70000 } },
  1235. 44100.0, golden ? 88200 : 22050, c.samplePath);
  1236. else
  1237. 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 } },
  1238. 44100.0, golden ? 100000 : 22050, c.samplePath);
  1239. double e = 0.0;
  1240. bool finite = ! y.empty();
  1241. for (auto v : y)
  1242. finite = finite && std::isfinite (v), e += (double) v * v;
  1243. CHECK (finite);
  1244. (isGms ? gmsSounding : morphineSounding) += e > 1e-6 ? 1 : 0;
  1245. if (golden && ! y.empty())
  1246. {
  1247. // our model, same events (GMS: tempo 140; Morphine: tempo 120), state as saved, 44.1 kHz
  1248. const auto rms = std::sqrt (e / (double) y.size());
  1249. CHECK (std::abs (rms - (isGms ? 0.213602668574 : 0.0673606448152)) < 1e-5);
  1250. 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 } },
  1251. 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 } };
  1252. for (const auto& g : isGms ? std::vector (std::begin (gmsAt), std::end (gmsAt)) : std::vector (std::begin (morAt), std::end (morAt)))
  1253. 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);
  1254. ++demoGoldens;
  1255. }
  1256. }
  1257. // MiniSynth (REA_rodada41): every state plays on the exact engine (tables from the installation's
  1258. // oscdata.dat) and sounds; two demo channels match golden samples of our round-41 model (bit-identical to
  1259. // renders of the installation's DLL for these states)
  1260. for (const auto& c : p.channels)
  1261. if (c.pluginName == "MiniSynth")
  1262. ++miniSynth, miniSynthUsable += lynx::miniSynthStateUsable (c.pluginState) ? 1 : 0;
  1263. for (const auto& c : full.channels)
  1264. {
  1265. if (c.instrument != lynx::InstrumentKind::minisynth)
  1266. continue;
  1267. ++miniSynthKind;
  1268. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  1269. const auto exo = file.find ("Exoplanet") != std::string::npos;
  1270. const auto golden = exo && (c.name == "Main Lead 1" || c.name == "Riser");
  1271. const auto y = lynx::minisynth::renderReference (c.pluginState,
  1272. { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 }, { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  1273. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 }, { 60000, 48, iv (127), 1.0, 0.0, 0.0, 70000 } },
  1274. 44100.0, golden ? 88200 : 22050, c.samplePath);
  1275. double e = 0.0;
  1276. bool finite = ! y.empty();
  1277. for (auto v : y)
  1278. finite = finite && std::isfinite (v), e += (double) v * v;
  1279. CHECK (finite);
  1280. miniSynthSounding += e > 1e-6 ? 1 : 0;
  1281. if (golden && ! y.empty())
  1282. {
  1283. // our model, same events, tempo 120, state as saved, 44.1 kHz, host blocks of 512, L = R = Final.Vol
  1284. const auto lead = c.name == "Main Lead 1";
  1285. CHECK (std::abs (std::sqrt (e / (double) y.size()) - (lead ? 0.177495801753 : 0.19006567413)) < 1e-9);
  1286. 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 } },
  1287. riserAt[] = { { 2000, -0.0603227727f }, { 15000, 0.112200871f }, { 30000, 0.0453962795f }, { 50000, 0.234380022f }, { 65000, -0.164545432f }, { 80000, -0.000485120923f } };
  1288. for (const auto& g : lead ? std::vector (std::begin (leadAt), std::end (leadAt)) : std::vector (std::begin (riserAt), std::end (riserAt)))
  1289. CHECK (y[2 * (std::size_t) g.i] == g.v && y[2 * (std::size_t) g.i + 1] == g.v);
  1290. ++miniSynthGoldens;
  1291. }
  1292. }
  1293. // PoiZone (REA_rodada44): every state plays on the exact engine and sounds; the demo channel matches golden
  1294. // samples of our round-44 model
  1295. for (const auto& c : p.channels)
  1296. if (c.pluginName == "PoiZone")
  1297. ++poizone, poizoneUsable += lynx::poizoneStateUsable (c.pluginState) ? 1 : 0;
  1298. for (const auto& c : full.channels)
  1299. {
  1300. if (c.instrument != lynx::InstrumentKind::poizone)
  1301. continue;
  1302. ++poizoneKind;
  1303. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  1304. const auto golden = c.name == "PoiZone" && file.find ("NIGHT TERROR") != std::string::npos;
  1305. const auto y = lynx::poizone::renderReference (c.pluginState,
  1306. { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 }, { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  1307. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 }, { 60000, 48, iv (127), 1.0, 0.0, 0.0, 70000 } },
  1308. 44100.0, golden ? 88200 : 22050);
  1309. double e = 0.0;
  1310. bool finite = ! y.empty();
  1311. for (auto v : y)
  1312. finite = finite && std::isfinite (v), e += (double) v * v;
  1313. CHECK (finite);
  1314. poizoneSounding += e > 1e-6 ? 1 : 0;
  1315. if (golden && ! y.empty())
  1316. {
  1317. // our model, same events, tempo 120, state as saved, 44.1 kHz, L = R = Final.Vol
  1318. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.018975758213507415) < 1e-7);
  1319. 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 } };
  1320. for (const auto& g : at)
  1321. 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);
  1322. ++poizoneGoldens;
  1323. }
  1324. }
  1325. // Toxic Biohazard (REA_rodada45): every demo state plays on the exact engine with the waves read from the
  1326. // installation's DLL and sounds; the "Bombs Away" channel matches golden samples of our round-45 model
  1327. for (const auto& c : p.channels)
  1328. if (c.pluginName == "Toxic Biohazard")
  1329. ++toxic, toxicUsable += lynx::toxicStateUsable (c.pluginState) ? 1 : 0;
  1330. for (const auto& c : full.channels)
  1331. {
  1332. if (c.instrument != lynx::InstrumentKind::toxic)
  1333. continue;
  1334. ++toxicKind;
  1335. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  1336. const auto golden = file.find ("Bombs Away") != std::string::npos;
  1337. const auto y = lynx::toxic::renderReference (
  1338. c.pluginState,
  1339. { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 }, { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  1340. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 }, { 60000, 48, iv (127), 1.0, 0.0, 0.0, 70000 } },
  1341. 44100.0, golden ? 88200 : 22050, c.samplePath, 512, 140.0);
  1342. double e = 0.0;
  1343. bool finite = ! y.empty();
  1344. for (auto v : y)
  1345. finite = finite && std::isfinite (v), e += (double) v * v;
  1346. CHECK (finite);
  1347. toxicSounding += e > 1e-6 ? 1 : 0;
  1348. if (golden && ! y.empty())
  1349. {
  1350. // our model (r45/py/toxmodel.py, which equals the DLL bit for bit), state as saved, tempo 140, 44.1 kHz
  1351. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.0832500924945) < 1e-6);
  1352. 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 } };
  1353. for (const auto& g : at)
  1354. 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);
  1355. ++toxicGoldens;
  1356. }
  1357. }
  1358. for (const auto& c : full.channels)
  1359. {
  1360. if (c.instrument == lynx::InstrumentKind::harmless && c.modelParams.size() >= (std::size_t) lynx::harmlessParamCount
  1361. && c.modelParams[(std::size_t) lynx::harmlessComboIndex + 3] > 0.5f)
  1362. {
  1363. ++harmlessCompWanted;
  1364. harmlessCompLoaded += c.modelParams.size() > (std::size_t) lynx::harmlessCompIndex + 1000 ? 1 : 0;
  1365. }
  1366. flKeysChannels += c.zones.size() == 30 && c.zones.front().path.find ("CHANGPIANO") != std::string::npos ? 1 : 0;
  1367. if (c.sampler.slices.size() >= 4)
  1368. {
  1369. ++slicedChannels;
  1370. CHECK (std::is_sorted (c.sampler.slices.begin(), c.sampler.slices.end()));
  1371. CHECK (c.sampler.slices.back() < 1.0f);
  1372. }
  1373. dwExact += c.instrument == lynx::InstrumentKind::directwave ? 1 : 0;
  1374. if (! c.zones.empty() && c.name.find (" - ") == std::string::npos)
  1375. {
  1376. ++dwChannels;
  1377. dwZones += (int) c.zones.size();
  1378. }
  1379. padChannels += c.name.find (" - ") != std::string::npos && c.instrument == lynx::InstrumentKind::sampler ? 1 : 0;
  1380. }
  1381. fpcKits += hasFpc;
  1382. // Maximus / Soundgoodizer become the exact engine (REA_rodada40); Vocodex carries SG.dat (and its
  1383. // carrier's Sytrus preset) read from the installation
  1384. const int basicSlots0 = basicSlots, basicExact0 = basicExact;
  1385. for (const auto& insert : p.mixer)
  1386. for (const auto& slot : insert.slots)
  1387. {
  1388. maximusSlots += slot.plugin == "Maximus";
  1389. delay2Slots += slot.plugin == "Fruity Delay 2";
  1390. delay3Slots += slot.plugin == "Fruity Delay 3";
  1391. sgzSlots += slot.plugin == "Soundgoodizer";
  1392. vocodexSlots += slot.plugin == "Vocodex";
  1393. shaperSlots += slot.plugin == "Fruity WaveShaper";
  1394. flPeqSlots += slot.plugin == "Fruity Parametric EQ 2" || slot.plugin == "Fruity Parametric EQ";
  1395. equoSlots += slot.plugin == "EQUO";
  1396. distructorSlots += slot.plugin == "Distructor";
  1397. pitchShifterSlots += slot.plugin == "Pitch Shifter";
  1398. clipperSlots += slot.plugin == "Fruity Soft Clipper";
  1399. basicSlots += slot.plugin == "Fruity PanOMatic" || slot.plugin == "Fruity Balance" || slot.plugin == "Fruity Blood Overdrive";
  1400. filterSlots += slot.plugin == "Fruity Fast LP" || slot.plugin == "Fruity Free Filter" || slot.plugin == "Fruity Filter";
  1401. chorusSlots += lynx::chorusControls (slot.plugin) > 0 && slot.state.size() >= 4 ? 1 : 0;
  1402. fastDistSlots += slot.plugin == "Fruity Fast Dist";
  1403. flangusSlots += slot.plugin == "Fruity Flangus";
  1404. flangerSlots += slot.plugin == "Fruity Flanger";
  1405. phaserSlots += slot.plugin == "Fruity Phaser";
  1406. }
  1407. // (the slots and the nodes of the Patcher graphs, as when Patchers were flattened into slots)
  1408. std::vector<const lynx::EffectSlot*> allSlots;
  1409. std::function<void (const lynx::EffectSlot&)> collect = [&] (const lynx::EffectSlot& fx) {
  1410. allSlots.push_back (&fx);
  1411. for (const auto& n : fx.patcher.nodes)
  1412. if (! n.generator)
  1413. collect (n.effect);
  1414. };
  1415. for (const auto& t : full.mixer)
  1416. for (const auto& fx : t.effects)
  1417. collect (fx);
  1418. for (const auto* slotPointer : allSlots)
  1419. {
  1420. const auto& fx = *slotPointer;
  1421. if (fx.kind == lynx::EffectKind::maximus)
  1422. {
  1423. const auto blob = lynx::maximusfx::unpack (fx.state);
  1424. CHECK (blob && lynx::maximusfx::presetOf (*blob) != nullptr);
  1425. if (blob)
  1426. (blob->build == lynx::hlfx::MaximusBuild::soundgoodizer ? sgzExact : maximusExact) += 1;
  1427. }
  1428. fastDistExact += fx.kind == lynx::EffectKind::fruityFastDist;
  1429. flangusExact += fx.kind == lynx::EffectKind::fruityFlangus;
  1430. flangerExact += fx.kind == lynx::EffectKind::fruityFlanger;
  1431. phaserExact += fx.kind == lynx::EffectKind::fruityPhaser;
  1432. if (fx.kind == lynx::EffectKind::vocodex)
  1433. vocodexWithData += ! lynx::vocodex::splitInstallData (fx.state).second.sgDat.empty();
  1434. if (fx.kind == lynx::EffectKind::fruityWaveShaper)
  1435. shaperExact += lynx::flshape::parseWaveShaper (fx.state).has_value() ? 1 : 0;
  1436. clipperExact += fx.kind == lynx::EffectKind::fruitySoftClipper;
  1437. flPeqExact += fx.kind == lynx::EffectKind::parametricEq && lynx::peq::flLaw (fx.params);
  1438. basicExact += fx.kind == lynx::EffectKind::fruityPanOMatic || fx.kind == lynx::EffectKind::fruityBalance
  1439. || fx.kind == lynx::EffectKind::fruityBloodOverdrive;
  1440. equoExact += fx.kind == lynx::EffectKind::equo && lynx::equofx::parse (fx.state).has_value();
  1441. if (fx.kind == lynx::EffectKind::distructor)
  1442. if (const auto d = lynx::distructor::parseBlob (fx.state))
  1443. {
  1444. ++distructorExact;
  1445. for (const auto& m : d->modules)
  1446. distructorImpulses += m.type == 3 && ! m.impulseFile.empty();
  1447. }
  1448. pitchShifterExact += fx.kind == lynx::EffectKind::flPitchShifter;
  1449. filterExact += fx.kind == lynx::EffectKind::fruityFastLP || fx.kind == lynx::EffectKind::fruityFreeFilter
  1450. || fx.kind == lynx::EffectKind::fruityFilter;
  1451. delay2Exact += fx.kind == lynx::EffectKind::fruityDelay2;
  1452. delay3Exact += fx.kind == lynx::EffectKind::fruityDelay3;
  1453. chorusExact += fx.kind == lynx::EffectKind::fruityChorus || fx.kind == lynx::EffectKind::hyperChorus || fx.kind == lynx::EffectKind::vintageChorus ? 1 : 0;
  1454. }
  1455. if (basicSlots - basicSlots0 != basicExact - basicExact0)
  1456. std::printf (" %s: %d PanOMatic/Balance/Blood Overdrive slots, %d exact effects\n", file.c_str(),
  1457. basicSlots - basicSlots0, basicExact - basicExact0);
  1458. for (const auto& pat : full.patterns)
  1459. for (const auto& n : pat.notes)
  1460. {
  1461. CHECK (n.channel >= 0 && n.channel < (int) full.channels.size());
  1462. padNotes += full.channels[(std::size_t) n.channel].name.find (" - ") != std::string::npos ? 1 : 0;
  1463. }
  1464. }
  1465. const auto peqDefaults = lynx::makeEffectSlot (lynx::EffectKind::parametricEq).params;
  1466. for (const auto& t : song.mixer)
  1467. for (const auto& fx : t.effects)
  1468. {
  1469. if (fx.kind == lynx::EffectKind::parametricEq)
  1470. {
  1471. ++peqSlots;
  1472. peqShaped += fx.params != peqDefaults;
  1473. for (double hz : { 50.0, 1000.0, 10000.0 })
  1474. CHECK (std::isfinite (lynx::peq::responseDb (fx.params, 48000.0, hz)));
  1475. }
  1476. if (fx.kind == lynx::EffectKind::reverb)
  1477. {
  1478. ++reverbs;
  1479. reverbsDryless += fx.params[4] < 0.01f;
  1480. reverbsLowCut += fx.params[5] > 0.0f;
  1481. }
  1482. if (fx.kind == lynx::EffectKind::reeverb2) // FL's exact Reeverb 2 (REA_rodada49): FL's own controls
  1483. {
  1484. const auto r2 = lynx::reeverb2::fromParams (fx.params);
  1485. ++reverbs;
  1486. reverbsDryless += r2.p[10] == 0;
  1487. reverbsLowCut += r2.p[0] >= 20;
  1488. }
  1489. }
  1490. for (const auto& t : song.mixer)
  1491. if (t.eq.active())
  1492. {
  1493. ++trackEqs;
  1494. for (std::size_t b = 0; b < 3; ++b)
  1495. CHECK (t.eq.hz[b] >= 10.0f && t.eq.hz[b] <= 16000.0f && std::abs (t.eq.gainDb[b]) <= 18.0f);
  1496. }
  1497. for (const auto& a : song.arrangements)
  1498. for (const auto& m : a.markers)
  1499. {
  1500. ++markers;
  1501. signatures += m.isSignature();
  1502. CHECK (m.tick >= 0);
  1503. }
  1504. // (round 168g: the patterns' own markers are no longer counted as the Playlist's)
  1505. for (const auto& p : song.patterns)
  1506. for (const auto& m : p.markers)
  1507. {
  1508. ++markers;
  1509. signatures += m.isSignature();
  1510. CHECK (m.tick >= 0);
  1511. }
  1512. // Fruity Wrapper (channels and mixer slots): every blob in the demos is read
  1513. auto countWrapper = [&] (const std::string& plugin, const std::vector<std::uint8_t>& state) {
  1514. if (plugin != "Fruity Wrapper")
  1515. return;
  1516. ++wrapped;
  1517. lynx::ForeignPlugin f;
  1518. wrappedParsed += lynx::parseFlWrapper (state, f) && f.format != lynx::ForeignPlugin::Format::none && ! f.name.empty();
  1519. if (f.format == lynx::ForeignPlugin::Format::vst2)
  1520. {
  1521. // VST2 state (REA_rodada89 §2): every one decodes, and writes back to the same bytes — except the
  1522. // wrapped Fruity effects of the FL 11 render tests (their mixer slots are read since round 80):
  1523. // those decode but do not write back byte-identical (not confirmed why)
  1524. ++wrappedVst2;
  1525. lynx::FlVst2State st;
  1526. if (lynx::parseFlVst2State (f.state, st) && st.version == -9)
  1527. {
  1528. ++wrappedVst2State;
  1529. wrappedVst2Old += lynx::writeFlVst2State (st) != f.state;
  1530. }
  1531. }
  1532. };
  1533. for (const auto& c : p.channels)
  1534. countWrapper (c.pluginName, c.pluginState);
  1535. for (const auto& insert : p.mixer)
  1536. for (const auto& slot : insert.slots)
  1537. countWrapper (slot.plugin, slot.state);
  1538. // Harmless: every state in the demos becomes a synth, with finite values
  1539. for (const auto& c : p.channels)
  1540. if (c.pluginName == "Harmless")
  1541. {
  1542. ++harmless;
  1543. lynx::SynthSettings hs;
  1544. float g = 1.0f;
  1545. if (lynx::synthFromHarmless (c.pluginState, 128.0, hs, g))
  1546. {
  1547. ++harmlessDecoded;
  1548. CHECK (std::isfinite (g) && hs.cutoff >= 20.0f && hs.cutoff <= 20000.0f && std::isfinite (hs.decay) && std::isfinite (hs.release));
  1549. }
  1550. if (const auto hp = lynx::harmlessParams (c.pluginState))
  1551. harmlessParsed += (int) hp->size() == lynx::harmlessParamCount ? 1 : 0;
  1552. }
  1553. for (const auto& c : song.channels)
  1554. harmlessKind += c.instrument == lynx::InstrumentKind::harmless ? 1 : 0;
  1555. // Harmor: every state in the demos plays on the exact engine (REA_rodada34) and sounds, with finite samples
  1556. for (const auto& c : p.channels)
  1557. if (c.pluginName == "Harmor")
  1558. {
  1559. ++harmor;
  1560. harmorUsable += lynx::harmorStateUsable (c.pluginState) ? 1 : 0;
  1561. }
  1562. for (const auto& c : song.channels)
  1563. if (c.instrument == lynx::InstrumentKind::harmor)
  1564. {
  1565. ++harmorKind;
  1566. 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);
  1567. double e = 0.0;
  1568. bool finite = ! y.empty();
  1569. for (auto v : y)
  1570. finite = finite && std::isfinite (v), e += (double) v * v;
  1571. CHECK (finite);
  1572. harmorSounding += e > 1e-6 ? 1 : 0;
  1573. }
  1574. // Ogun: every state in the demos plays on the exact engine (REA_rodada37) and sounds, with finite samples
  1575. for (const auto& c : p.channels)
  1576. if (c.pluginName == "Ogun")
  1577. {
  1578. ++ogun;
  1579. ogunUsable += lynx::ogunStateUsable (c.pluginState) ? 1 : 0;
  1580. }
  1581. for (const auto& c : song.channels)
  1582. if (c.instrument == lynx::InstrumentKind::ogun)
  1583. {
  1584. ++ogunKind;
  1585. 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 },
  1586. { 2000, 67, 0.6, 1.0, 0.0, 0.0, 0.0, 0.0, 0.5, 8000 } }, 44100.0, 11025);
  1587. double e = 0.0;
  1588. bool finite = ! y.empty();
  1589. for (auto v : y)
  1590. finite = finite && std::isfinite (v), e += (double) v * v;
  1591. CHECK (finite);
  1592. ogunSounding += e > 1e-6 ? 1 : 0;
  1593. }
  1594. // Autogun (REA_rodada57): every state plays on Ogun's engine in its Autogun variant; with the installation, the
  1595. // Magic Dust stage gets Autogun\Data\SG.dat
  1596. {
  1597. bool any = false;
  1598. for (const auto& c : p.channels)
  1599. if (c.pluginName == "Autogun")
  1600. ++autogun, autogunUsable += lynx::autogunStateUsable (c.pluginState) ? 1 : 0, any = true;
  1601. if (any)
  1602. for (const auto& c : lynx::importFlp (p, withFiles).channels)
  1603. if (c.instrument == lynx::InstrumentKind::autogun)
  1604. {
  1605. ++autogunKind;
  1606. lynx::ogun::RefOptions opt;
  1607. opt.autogun = true;
  1608. if (! c.samplePath.empty())
  1609. {
  1610. auto local = c.samplePath;
  1611. std::replace (local.begin(), local.end(), '\\', '/');
  1612. std::ifstream sg (local, std::ios::binary);
  1613. opt.sgState.assign ((std::istreambuf_iterator<char> (sg)), std::istreambuf_iterator<char>());
  1614. autogunWithSg += opt.sgState.size() == 1342 ? 1 : 0;
  1615. }
  1616. 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 } },
  1617. 44100.0, 11025, opt);
  1618. double e = 0.0;
  1619. bool finite = ! y.empty();
  1620. for (auto v : y)
  1621. finite = finite && std::isfinite (v), e += (double) v * v;
  1622. CHECK (finite);
  1623. autogunSounding += e > 1e-6 ? 1 : 0;
  1624. }
  1625. }
  1626. // Fruity DX10: every state plays on the exact engine when its channel processor is neutral (REA_rodada42)
  1627. for (const auto& c : p.channels)
  1628. if (c.pluginName == "Fruity DX10" || c.pluginName == "DX10")
  1629. ++dx10, dx10Usable += lynx::dx10StateUsable (c.pluginState) ? 1 : 0;
  1630. for (const auto& c : song.channels)
  1631. if (c.instrument == lynx::InstrumentKind::dx10)
  1632. {
  1633. ++dx10Kind;
  1634. 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 } },
  1635. 44100.0, 11025);
  1636. double e = 0.0;
  1637. bool finite = ! y.empty();
  1638. for (auto v : y)
  1639. finite = finite && std::isfinite (v), e += (double) v * v;
  1640. CHECK (finite);
  1641. dx10Sounding += e > 1e-6 ? 1 : 0;
  1642. }
  1643. // 3x Osc: every channel plays on the exact engine (REA_rodada47)
  1644. for (const auto& c : song.channels)
  1645. if (c.instrument == lynx::InstrumentKind::threeosc)
  1646. {
  1647. ++threeOscKind;
  1648. 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 } },
  1649. 44100.0, 11025);
  1650. double e = 0.0;
  1651. bool finite = ! y.empty();
  1652. for (auto v : y)
  1653. finite = finite && std::isfinite (v), e += (double) v * v;
  1654. CHECK (finite);
  1655. threeOscSounding += e > 1e-6 ? 1 : 0;
  1656. }
  1657. // Sytrus: every state in the demos (zlib + 10 units) decodes, with finite values
  1658. for (const auto& c : p.channels)
  1659. if (c.pluginName == "Sytrus")
  1660. {
  1661. ++sytrus;
  1662. lynx::SynthSettings ss;
  1663. float g = 1.0f;
  1664. // the Sytrus engine (REA_rodada31) reads every demo state; the first 24 play a short note (render settings)
  1665. if (lynx::sytrus::stateUsable (c.pluginState))
  1666. {
  1667. ++sytrusEngine;
  1668. if (sytrusRendered < 24)
  1669. {
  1670. ++sytrusRendered;
  1671. lynx::sytrus::Engine e;
  1672. e.prepare (44100.0, true, 512);
  1673. e.setPatch (lynx::sytrus::parsePatch (c.pluginState));
  1674. lynx::sytrus::NoteParams note;
  1675. e.noteOn (note);
  1676. std::vector<float> l (8192), r (8192);
  1677. e.render (l.data(), r.data(), 8192, 0.551f, 0.0f);
  1678. bool finite = true;
  1679. double energy = 0.0;
  1680. for (std::size_t k = 0; k < l.size(); ++k)
  1681. finite = finite && std::isfinite (l[k]) && std::isfinite (r[k]), energy += (double) l[k] * l[k];
  1682. CHECK (finite);
  1683. sytrusSounding += energy > 1e-6;
  1684. }
  1685. }
  1686. if (lynx::synthFromSytrus (c.pluginState, 128.0, ss, g))
  1687. {
  1688. ++sytrusDecoded;
  1689. CHECK (std::isfinite (g) && ss.cutoff >= 20.0f && ss.cutoff <= 20000.0f && std::isfinite (ss.attack) && std::isfinite (ss.decay)
  1690. && std::isfinite (ss.release) && ss.sustain >= 0.0f && ss.sustain <= 1.0f && std::isfinite (ss.filterEnv.amount));
  1691. for (const auto& o : ss.osc)
  1692. CHECK (o.level >= 0.0f && o.level <= 1.0f && o.octave >= -3 && o.octave <= 3);
  1693. }
  1694. }
  1695. // round 12 generators
  1696. for (const auto& c : p.channels)
  1697. {
  1698. lynx::SynthSettings gs;
  1699. float g = 1.0f;
  1700. int decoded = -1;
  1701. if (c.pluginName == "Harmor")
  1702. {
  1703. decoded = lynx::synthFromHarmor (c.pluginState, gs, g);
  1704. if (decoded)
  1705. {
  1706. // REA_rodada25 §4: most resonant settings stay below Q ≈ 3, a few saturate
  1707. CHECK (gs.resonance >= 0.0f && gs.resonance <= 1.0f);
  1708. static int resonant = 0, strong = 0, total = 0;
  1709. resonant += gs.resonance > 0.0f;
  1710. strong += gs.resonance > 0.9f;
  1711. if (++total % 50 == 0)
  1712. std::printf (" Harmor resonance: %d with a peak, %d with Q > ~5 (of %d)\n", resonant, strong, total);
  1713. }
  1714. static int saw = 0, square = 0, other = 0, total = 0;
  1715. if (decoded && std::any_of (gs.osc.begin(), gs.osc.end(), [] (const lynx::SynthOscillator& o) { return o.wave == lynx::Waveform::spectral; }))
  1716. {
  1717. auto isSaw = true, isSquare = true;
  1718. for (int n = 1; n <= 16; ++n)
  1719. {
  1720. isSaw = isSaw && std::abs (gs.spectrum[(std::size_t) n - 1] - 1.0f / n) < 1e-3f;
  1721. isSquare = isSquare && std::abs (gs.spectrum[(std::size_t) n - 1] - (n % 2 == 1 ? 1.1535f / n : 0.0f)) < 2e-3f;
  1722. }
  1723. ++(isSaw ? saw : isSquare ? square : other);
  1724. if (++total % 20 == 0 || total == 100)
  1725. std::printf (" Harmor spectrum: %d saw, %d square, %d other (of %d)\n", saw, square, other, total);
  1726. }
  1727. }
  1728. else if (c.pluginName == "GMS")
  1729. decoded = lynx::synthFromGms (c.pluginState, 128.0, gs, g);
  1730. else if (c.pluginName == "Ogun" || c.pluginName == "Autogun")
  1731. {
  1732. decoded = lynx::synthFromOgun (c.pluginState, gs, g);
  1733. // REA_rodada24 §1.9: the "Trumpet" preset (richness 7, no seeds) has partials 0.9087, 0.8610,
  1734. // 0.8151… at the fixed seed-3 phases 0.919, 0.380, 0.130
  1735. if (decoded && gs.osc[0].wave == lynx::Waveform::spectral)
  1736. {
  1737. CHECK (std::all_of (gs.spectrum.begin(), gs.spectrum.end(), [] (float a) { return std::isfinite (a) && a >= 0.0f; }));
  1738. static constexpr float trumpet[3] { 0.9087f, 0.8610f, 0.8151f }, phases[3] { 0.919f, 0.380f, 0.130f };
  1739. auto match = true;
  1740. for (std::size_t k = 0; k < 3; ++k)
  1741. match = match && std::abs (gs.spectrum[k] * 2.0f / 3.14159265f - trumpet[k]) < 1e-3f && std::abs (gs.spectrumPhase[k] - phases[k]) < 1e-3f;
  1742. ogunTrumpet += match ? 1 : 0;
  1743. }
  1744. }
  1745. else if (c.pluginName == "Sakura")
  1746. decoded = lynx::synthFromSakura (c.pluginState, gs);
  1747. else if (c.pluginName == "Kepler")
  1748. decoded = lynx::synthFromKepler (c.pluginState, gs, g);
  1749. else if (c.pluginName == "Wave Traveller")
  1750. decoded = lynx::waveTravellerStateUsable (c.pluginState) ? 2 : 0;
  1751. else if (c.pluginName == "PoiZone")
  1752. decoded = lynx::synthFromPoiZone (c.pluginState, gs, g);
  1753. else if (c.pluginName == "BassDrum")
  1754. decoded = lynx::synthFromBassDrum (c.pluginState, gs, g);
  1755. else if (c.pluginName == "Toxic Biohazard")
  1756. decoded = lynx::synthFromToxic (c.pluginState, gs, g);
  1757. else if (c.pluginName == "Morphine")
  1758. decoded = lynx::synthFromMorphine (c.pluginState, gs, g);
  1759. else if (c.pluginName == "BooBass")
  1760. decoded = lynx::synthFromBooBass (c.pluginState, gs);
  1761. else if (c.pluginName == "Drumpad" || c.pluginName == "DrumPad")
  1762. decoded = lynx::drumpadPatchName (c.pluginState).has_value() ? 2 : 0;
  1763. else if (c.pluginName == "Fruity Envelope Controller" && c.pluginState.size() >= 4 && c.pluginState[2] >= 6) // v >= 6
  1764. decoded = lynx::parseEnvelopeController (c.pluginState).has_value() ? 2 : 0;
  1765. else if (c.pluginName == "FLEX")
  1766. decoded = lynx::flexPresetName (c.pluginState).has_value() ? 2 : 0;
  1767. else if (c.pluginName == "Fruity DrumSynth Live")
  1768. decoded = lynx::drumSynthLiveParams (c.pluginState).has_value() ? 2 : 0;
  1769. if (decoded < 0)
  1770. continue;
  1771. auto& counter = generators[c.pluginName];
  1772. ++counter.first;
  1773. counter.second += decoded != 0;
  1774. if (decoded == 1 && c.pluginName != "Fruity DrumSynth Live")
  1775. {
  1776. CHECK (std::isfinite (g) && g >= 0.0f && gs.cutoff >= 20.0f && gs.cutoff <= 20000.0f && gs.sustain >= 0.0f && gs.sustain <= 1.0f);
  1777. 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));
  1778. for (const auto& o : gs.osc)
  1779. CHECK (o.level >= 0.0f && o.level <= 4.0f && std::isfinite (o.detune) && std::isfinite (o.pan));
  1780. }
  1781. }
  1782. // 3x Osc: every state in the demos decodes, with levels summing to at most 1
  1783. for (const auto& c : p.channels)
  1784. if (c.pluginName == "3x Osc")
  1785. {
  1786. ++threeOsc;
  1787. lynx::SynthSettings s;
  1788. if (lynx::synthFrom3xOsc (c.pluginState, s))
  1789. {
  1790. ++threeOscDecoded;
  1791. // with AM, osc 3 is the modulation depth and is left out of the sum
  1792. const auto sum = s.osc[0].level + s.osc[1].level + (s.am ? 0.0f : s.osc[2].level);
  1793. CHECK (sum <= 1.001f && s.osc[2].level <= 1.0f);
  1794. for (const auto& o : s.osc)
  1795. CHECK (o.octave >= -2 && o.octave <= 2 && std::abs (o.semitones) <= 11 && std::abs (o.pan) <= 1.0f);
  1796. }
  1797. }
  1798. for (auto& pat : p.patterns)
  1799. if (! pat.notes.empty())
  1800. {
  1801. ++withNotes;
  1802. break;
  1803. }
  1804. for (auto& a : p.arrangements)
  1805. for (auto& item : a.items)
  1806. CHECK (item.track >= 0 && item.track < 500);
  1807. for (auto& clip : song.playlist)
  1808. {
  1809. CHECK (clip.pattern >= 0 && clip.pattern < (int) song.patterns.size());
  1810. CHECK (clip.length > 0 && clip.offset >= 0);
  1811. if (clip.kind == lynx::ClipKind::automation)
  1812. CHECK (clip.automation >= 0 && clip.automation < (int) song.automations.size());
  1813. }
  1814. for (auto& a : song.automations)
  1815. {
  1816. ++automations;
  1817. automationsWithTarget += a.target.kind != lynx::AutomationTargetKind::none;
  1818. CHECK (a.isLive() || ! a.points.empty());
  1819. for (std::size_t i = 1; i < a.points.size(); ++i)
  1820. CHECK (a.points[i].tick >= a.points[i - 1].tick);
  1821. }
  1822. // imported routing has no cycles and valid destinations
  1823. CHECK (song.mixerOrder().size() + 1 == song.mixer.size());
  1824. for (auto& t : song.mixer)
  1825. for (auto& s : t.sends)
  1826. {
  1827. ++sends;
  1828. CHECK (s.target > 0 && s.target < (int) song.mixer.size());
  1829. }
  1830. if (! song.playlist.empty())
  1831. ++withPlaylist;
  1832. }
  1833. catch (const flp::Error& e)
  1834. {
  1835. ++failures;
  1836. std::printf (" FAILED %s: %s\n", entry.path().string().c_str(), e.what());
  1837. }
  1838. }
  1839. CHECK (threeOscDecoded == threeOsc);
  1840. std::printf (" Harmless: %d channels, %d decoded\n", harmless, harmlessDecoded);
  1841. std::printf (" Sytrus: %d channels, %d decoded, %d by the engine (%d of %d rendered sound)\n", sytrus, sytrusDecoded, sytrusEngine,
  1842. sytrusSounding, sytrusRendered);
  1843. CHECK (sytrusEngine == sytrus);
  1844. CHECK (sytrusSounding >= sytrusRendered * 3 / 4);
  1845. for (const auto& [name, counter] : generators)
  1846. {
  1847. std::printf (" %s: %d channels, %d decoded\n", name.c_str(), counter.first, counter.second);
  1848. CHECK (counter.second == counter.first);
  1849. }
  1850. std::printf (" Ogun spectra matching the Trumpet reference: %d\n", ogunTrumpet);
  1851. CHECK (ogunTrumpet >= 1);
  1852. // demo Gross Beat: the volume slot becomes the gate (those using an envelope slot get steps < 1)
  1853. {
  1854. int vocodexes = 0, vocodexSides = 0;
  1855. int lovePhilterLinks[4] {}; // live links: cutoff, resonance; inside containers: cutoff, resonance
  1856. int embeddedZones = 0, lovePhilters = 0, lovePhiltersRead = 0, lovePhiltersConverted = 0, gates = 0, shaped = 0, effectors = 0, patchers = 0, serial = 0, parallel = 0, generatorPatchers = 0;
  1857. for (const auto& entry : std::filesystem::recursive_directory_iterator (std::filesystem::path (realFlpDir) / "Demo projects"))
  1858. if (entry.path().extension() == ".flp")
  1859. {
  1860. std::ifstream in (entry.path(), std::ios::binary);
  1861. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  1862. try
  1863. {
  1864. const auto project = flp::parseProject (file);
  1865. for (const auto& insert : project.mixer)
  1866. for (const auto& slot : insert.slots)
  1867. if (slot.plugin == "Gross Beat")
  1868. {
  1869. // FL's own engine (REA_rodada53): every demo state is read
  1870. const auto fx = lynx::effectFromFl (slot.plugin, slot.state);
  1871. CHECK (fx && fx->kind == lynx::EffectKind::grossBeat);
  1872. ++gates;
  1873. if (fx && fx->kind == lynx::EffectKind::grossBeat && lynx::grossbeat::slotNames (fx->state, true).size() == 36)
  1874. ++shaped;
  1875. }
  1876. else if (slot.plugin == "Fruity Love Philter")
  1877. {
  1878. ++lovePhilters;
  1879. lovePhiltersRead += lynx::parseLovePhilter (slot.state).has_value();
  1880. if (const auto conv = lynx::lovePhilterEffects (slot.state))
  1881. {
  1882. ++lovePhiltersConverted;
  1883. CHECK (! conv->slots.empty());
  1884. for (const auto& fx : conv->slots)
  1885. for (auto v : fx.params)
  1886. CHECK (std::isfinite (v) && v >= 0.0f && v <= 1.0f);
  1887. for (const auto& fx : conv->slots)
  1888. for (const auto& link : fx.links)
  1889. {
  1890. const auto& chain = fx.branches[(std::size_t) link.branch].chain;
  1891. CHECK (link.source < (int) chain.size() && chain[(std::size_t) link.target].kind == lynx::EffectKind::parametricEq);
  1892. lovePhilterLinks[2 + (link.param == lynx::peq::param (0, 3) ? 1 : 0)]++;
  1893. }
  1894. for (const auto& link : conv->links)
  1895. {
  1896. lovePhilterLinks[link.param == lynx::peq::param (0, 3) ? 1 : 0]++;
  1897. CHECK (link.curve.size() == 33);
  1898. for (auto v : link.curve)
  1899. CHECK (std::isfinite (v) && v >= 0.0f && v <= 1.0f);
  1900. }
  1901. }
  1902. }
  1903. else if (slot.plugin == "Vocodex")
  1904. {
  1905. const auto v = lynx::vocodexEffect (slot.state);
  1906. CHECK (v.has_value());
  1907. vocodexes += v.has_value();
  1908. vocodexSides += v && (v->modIn > 0 || v->carIn > 0);
  1909. }
  1910. else if (slot.plugin == "Effector")
  1911. effectors += lynx::effectFromFl (slot.plugin, slot.state).has_value();
  1912. else if (slot.plugin == "Patcher")
  1913. {
  1914. const auto graph = lynx::parsePatcher (slot.state);
  1915. CHECK (graph.has_value());
  1916. ++patchers;
  1917. if (graph && ! graph->serialChain.empty())
  1918. ++serial;
  1919. if (graph && graph->serialChain.empty() && graph->audio)
  1920. ++parallel;
  1921. }
  1922. for (const auto& c : project.channels)
  1923. if (c.pluginName == "DirectWave")
  1924. if (const auto program = lynx::directWaveProgram (c.pluginState))
  1925. for (const auto& wav : program->embeddedWav)
  1926. if (! wav.empty())
  1927. {
  1928. ++embeddedZones;
  1929. CHECK (wav.size() > 44 && std::memcmp (wav.data(), "RIFF", 4) == 0);
  1930. }
  1931. for (const auto& c : project.channels)
  1932. if (c.pluginName == "Patcher")
  1933. {
  1934. const auto graph = lynx::parsePatcher (c.pluginState);
  1935. CHECK (graph.has_value());
  1936. generatorPatchers += graph && graph->mainGenerator >= 0;
  1937. }
  1938. }
  1939. catch (const flp::Error&) {}
  1940. }
  1941. CHECK (gates >= 30 && shaped == gates);
  1942. std::printf (" Gross Beat: %d slots, %d read exactly; Effector: %d converted; Love Philter: %d, %d read\n", gates, shaped, effectors,
  1943. lovePhilters, lovePhiltersRead);
  1944. CHECK (lovePhiltersRead == lovePhilters && lovePhiltersConverted == lovePhilters);
  1945. std::printf (" Vocodex: %d converted, %d with side inputs\n", vocodexes, vocodexSides);
  1946. CHECK (vocodexes >= 9);
  1947. std::printf (" Love Philter live links: %d cutoff, %d resonance; in multi-unit containers: %d cutoff, %d resonance\n",
  1948. lovePhilterLinks[0], lovePhilterLinks[1], lovePhilterLinks[2], lovePhilterLinks[3]);
  1949. CHECK (lovePhilterLinks[2] + lovePhilterLinks[3] > 0);
  1950. std::printf (" DirectWave: %d zones with embedded audio\n", embeddedZones);
  1951. CHECK (embeddedZones == 12);
  1952. std::printf (" Patcher: %d in the mixer (%d serial, %d reduced to serial/parallel), %d as instrument with generator\n", patchers, serial,
  1953. parallel, generatorPatchers);
  1954. CHECK (gates == 0 || shaped > 0);
  1955. }
  1956. // Envelope Controller "Basic Sidechain" (Future House): drops on the note and returns to ~1 within ~1/3 s
  1957. {
  1958. const auto path = std::filesystem::path (realFlpDir) / "Demo projects" / "Demo songs" / "Asher Postman - Future House.flp";
  1959. if (std::filesystem::exists (path))
  1960. {
  1961. std::ifstream in (path, std::ios::binary);
  1962. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  1963. const auto song = lynx::importFlp (flp::parseProject (file));
  1964. const lynx::ChannelDef* controller = nullptr;
  1965. for (const auto& c : song.channels)
  1966. if (c.instrument == lynx::InstrumentKind::controller)
  1967. controller = &c;
  1968. CHECK (controller != nullptr);
  1969. if (controller != nullptr)
  1970. {
  1971. auto inst = lynx::makeInstrument (lynx::InstrumentKind::controller);
  1972. inst->prepare (48000.0);
  1973. inst->configure (*controller, song.bpm);
  1974. std::vector<float> l (480), r (480);
  1975. inst->render (l.data(), r.data(), 480, 1.0f, 1.0f);
  1976. inst->noteOn (60, 1.0f);
  1977. inst->render (l.data(), r.data(), 480, 1.0f, 1.0f); // 10 ms
  1978. const auto ducked = inst->controlOutput (0);
  1979. for (int i = 0; i < 100; ++i) // +1 s
  1980. inst->render (l.data(), r.data(), 480, 1.0f, 1.0f);
  1981. const auto recovered = inst->controlOutput (0);
  1982. std::printf (" Envelope Controller (Basic Sidechain): %.2f on the note, %.2f after 1 s\n", ducked, recovered);
  1983. CHECK (ducked < 0.3f && recovered > 0.9f);
  1984. }
  1985. }
  1986. }
  1987. // mutation fuzz: real states with flipped and truncated bytes must not crash the readers
  1988. {
  1989. std::map<std::string, std::vector<std::uint8_t>> samples; // one real blob per plugin
  1990. for (const auto& entry : std::filesystem::recursive_directory_iterator (std::filesystem::path (realFlpDir) / "Demo projects"))
  1991. {
  1992. if (entry.path().extension() != ".flp")
  1993. continue;
  1994. std::ifstream in (entry.path(), std::ios::binary);
  1995. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  1996. try
  1997. {
  1998. const auto project = flp::parseProject (file);
  1999. for (const auto& c : project.channels)
  2000. if (! c.pluginState.empty())
  2001. samples.emplace (c.pluginName, c.pluginState);
  2002. for (const auto& insert : project.mixer)
  2003. for (const auto& slot : insert.slots)
  2004. if (! slot.state.empty())
  2005. samples.emplace (slot.plugin, slot.state);
  2006. }
  2007. catch (const flp::Error&) {}
  2008. }
  2009. std::uint32_t seed = 12345;
  2010. auto rnd = [&seed] { seed = seed * 1664525u + 1013904223u; return seed >> 8; };
  2011. int mutations = 0;
  2012. for (const auto& [name, original] : samples)
  2013. for (int round = 0; round < 40; ++round)
  2014. {
  2015. auto blob = original;
  2016. if (round % 4 == 3 && ! blob.empty())
  2017. blob.resize (rnd() % blob.size()); // truncated
  2018. else
  2019. for (int k = 0; k < 1 + (int) (rnd() % 8) && ! blob.empty(); ++k)
  2020. blob[rnd() % blob.size()] = (std::uint8_t) rnd();
  2021. ++mutations;
  2022. lynx::SynthSettings synth;
  2023. float gain = 1.0f;
  2024. lynx::synthFromSytrus (blob, 120.0, synth, gain);
  2025. lynx::synthFromHarmor (blob, synth, gain);
  2026. lynx::synthFromGms (blob, 120.0, synth, gain);
  2027. lynx::synthFromOgun (blob, synth, gain);
  2028. lynx::synthFromMorphine (blob, synth, gain);
  2029. lynx::synthFromKepler (blob, synth, gain);
  2030. lynx::synthFromPoiZone (blob, synth, gain);
  2031. lynx::synthFromToxic (blob, synth, gain);
  2032. lynx::synthFromBassDrum (blob, synth, gain);
  2033. lynx::synthFromHarmless (blob, 120.0, synth, gain);
  2034. lynx::harmlessParams (blob);
  2035. if (lynx::harmorStateUsable (blob))
  2036. lynx::harmorFallbacks (blob);
  2037. lynx::parseEnvelopeController (blob);
  2038. lynx::parseKeyboardController (blob);
  2039. lynx::parseLovePhilter (blob);
  2040. lynx::parsePatcher (blob);
  2041. lynx::effectFromFl (name, blob);
  2042. lynx::fpcPads (blob);
  2043. lynx::directWaveProgram (blob);
  2044. CHECK (std::isfinite (gain) && std::isfinite (synth.cutoff));
  2045. }
  2046. std::printf (" mutation fuzz: %d blobs from %d plugins\n", mutations, (int) samples.size());
  2047. }
  2048. // WavPack (.wv) samples from FL packs: all decode, with finite, non-silent audio
  2049. {
  2050. int files = 0, decoded = 0, audible = 0;
  2051. double seconds = 0.0;
  2052. for (const auto& entry : std::filesystem::recursive_directory_iterator (std::filesystem::path (realFlpDir) / "Patches" / "Packs"))
  2053. if (entry.path().extension() == ".wv")
  2054. {
  2055. std::ifstream in (entry.path(), std::ios::binary);
  2056. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  2057. ++files;
  2058. lynx::DecodedAudio audio;
  2059. if (! lynx::decodeWavPack (file.data(), file.size(), audio))
  2060. continue;
  2061. ++decoded;
  2062. float peak = 0.0f;
  2063. bool finite = true;
  2064. for (const auto& ch : audio.channels)
  2065. for (auto v : ch)
  2066. {
  2067. finite = finite && std::isfinite (v);
  2068. peak = std::max (peak, std::abs (v));
  2069. }
  2070. CHECK (finite && peak <= 4.0f);
  2071. audible += peak > 1e-4f;
  2072. seconds += (double) audio.channels[0].size() / audio.sampleRate;
  2073. }
  2074. std::printf (" WavPack: %d .wv files, %d decoded, %d audible, %.0f s of audio\n", files, decoded, audible, seconds);
  2075. CHECK (decoded == files);
  2076. }
  2077. // native GMS presets (.gmsynth = the contents of 0xD5 without the "SSLF")
  2078. int gmsPresets = 0, gmsDecoded = 0;
  2079. const auto gmsFolder = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "GMS";
  2080. if (std::filesystem::exists (gmsFolder))
  2081. for (const auto& entry : std::filesystem::recursive_directory_iterator (gmsFolder))
  2082. if (entry.path().extension() == ".gmsynth")
  2083. {
  2084. std::ifstream in (entry.path(), std::ios::binary);
  2085. std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  2086. std::vector<std::uint8_t> blob { 'S', 'S', 'L', 'F' };
  2087. const auto n = (std::uint32_t) file.size();
  2088. blob.insert (blob.end(), reinterpret_cast<const std::uint8_t*> (&n), reinterpret_cast<const std::uint8_t*> (&n) + 4);
  2089. blob.insert (blob.end(), file.begin(), file.end());
  2090. lynx::SynthSettings gs;
  2091. float g = 1.0f;
  2092. ++gmsPresets;
  2093. if (lynx::synthFromGms (blob, 120.0, gs, g))
  2094. {
  2095. ++gmsDecoded;
  2096. CHECK (std::isfinite (g) && gs.cutoff >= 20.0f && std::isfinite (gs.pitchEnv.amount) && std::isfinite (gs.lfo[lynx::lfoPitch].period));
  2097. }
  2098. }
  2099. std::printf (" GMS presets: %d, %d decoded\n", gmsPresets, gmsDecoded);
  2100. // the synth's "FL presets" button: Sytrus/Harmor .fst and .gmsynth become synth settings
  2101. int presetFiles = 0, presetsRead = 0;
  2102. for (const auto* name : { "Sytrus", "Harmor", "GMS", "Harmless", "Ogun", "Sakura", "Fruit Kick", "Fruity DX10", "MiniSynth", "Kepler",
  2103. "PoiZone", "Toxic Biohazard", "BassDrum" })
  2104. {
  2105. const auto folder = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / name;
  2106. if (! std::filesystem::exists (folder))
  2107. continue;
  2108. for (const auto& entry : std::filesystem::recursive_directory_iterator (folder))
  2109. if (entry.path().extension() == ".fst" || entry.path().extension() == ".gmsynth")
  2110. {
  2111. std::ifstream in (entry.path(), std::ios::binary);
  2112. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  2113. ++presetFiles;
  2114. presetsRead += lynx::synthFromFlPreset (file).has_value();
  2115. }
  2116. }
  2117. std::printf (" FL synth presets: %d files, %d read\n", presetFiles, presetsRead);
  2118. CHECK (presetsRead >= presetFiles * 95 / 100);
  2119. CHECK (gmsDecoded == gmsPresets);
  2120. CHECK (sytrusDecoded == sytrus);
  2121. CHECK (harmlessDecoded == harmless && harmless > 100);
  2122. std::printf (" Harmless: %d states parsed for the engine, %d channels imported as Harmless\n", harmlessParsed, harmlessKind);
  2123. CHECK (harmlessParsed == harmless && harmlessKind >= harmless);
  2124. std::printf (" Harmor: %d channels, %d states usable by the engine, %d imported as Harmor (%d sounding)\n", harmor, harmorUsable, harmorKind, harmorSounding);
  2125. CHECK (harmorUsable == harmor && harmor > 90 && harmorKind >= harmor && harmorSounding >= harmorKind * 9 / 10);
  2126. 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",
  2127. gms, gmsUsable, gmsKind, gmsSounding, morphine, morphineUsable, morphineKind, morphineSounding, demoGoldens);
  2128. CHECK (gmsUsable == gms && gms >= 9 && gmsKind == gms && gmsSounding == gmsKind);
  2129. std::printf (" MiniSynth channels: %d, %d usable, %d on the exact engine, %d sounding, %d golden\n", miniSynth, miniSynthUsable, miniSynthKind,
  2130. miniSynthSounding, miniSynthGoldens);
  2131. CHECK (miniSynthUsable == miniSynth && miniSynth >= 12 && miniSynthKind == miniSynth && miniSynthSounding == miniSynthKind && miniSynthGoldens == 2);
  2132. CHECK (morphineUsable == morphine && morphine >= 7 && morphineKind == morphine && morphineSounding == morphineKind && demoGoldens == 2);
  2133. std::printf (" PoiZone: %d channels, %d usable, %d imported as PoiZone (%d sounding), %d demo golden\n", poizone, poizoneUsable, poizoneKind,
  2134. poizoneSounding, poizoneGoldens);
  2135. CHECK (poizoneUsable == poizone && poizone >= 2 && poizoneKind == poizone && poizoneSounding == poizoneKind && poizoneGoldens == 1);
  2136. std::printf (" Toxic Biohazard: %d channels, %d usable, %d imported as Toxic (%d sounding), %d golden\n", toxic, toxicUsable, toxicKind,
  2137. toxicSounding, toxicGoldens);
  2138. CHECK (toxicUsable == toxic && toxic >= 3 && toxicKind == toxic && toxicSounding == toxicKind && toxicGoldens == 1);
  2139. std::printf (" Ogun: %d channels, %d states usable by the engine, %d imported as Ogun (%d sounding)\n", ogun, ogunUsable, ogunKind, ogunSounding);
  2140. CHECK (ogunUsable == ogun && ogun >= 20 && ogunKind >= ogun && ogunSounding >= ogunKind * 9 / 10);
  2141. std::printf (" Autogun: %d channels, %d states usable by the engine, %d imported as Autogun (%d with SG.dat, %d sounding)\n", autogun,
  2142. autogunUsable, autogunKind, autogunWithSg, autogunSounding);
  2143. CHECK (autogunUsable == autogun && autogun >= 9 && autogunKind == autogun && autogunWithSg == autogun && autogunSounding == autogun);
  2144. std::printf (" Fruity DX10: %d channels, %d states usable by the engine, %d imported as DX10 (%d sounding)\n", dx10, dx10Usable, dx10Kind, dx10Sounding);
  2145. CHECK (dx10Usable == dx10 && dx10 >= 12 && dx10Kind == dx10 && dx10Sounding == dx10Kind);
  2146. std::printf (" Kepler: %d channels, %d states usable by the engine, %d imported as Kepler (%d sounding)\n", kepler, keplerUsable, keplerKind, keplerSounding);
  2147. CHECK (keplerUsable == kepler && kepler >= 4 && keplerSounding == keplerKind && (keplerKind == 0 || keplerKind == kepler));
  2148. std::printf (" Harmless rack compressor: %d of %d presets read from the installation\n", harmlessCompLoaded, harmlessCompWanted);
  2149. CHECK (harmlessCompLoaded == harmlessCompWanted && harmlessCompWanted > 100);
  2150. std::printf (" Maximus: %d of %d slots on the exact engine; Soundgoodizer: %d of %d; Vocodex with SG.dat: %d of %d\n", maximusExact,
  2151. maximusSlots, sgzExact, sgzSlots, vocodexWithData, vocodexSlots);
  2152. // REA_rodada40 §6: in Demo projects the 4 states left on the approximation use the Frequency Splitter linear-phase
  2153. // option; the other Data folders add states with options not decoded
  2154. CHECK (maximusSlots - maximusExact <= 16 && maximusExact >= 97 && sgzExact == sgzSlots && sgzSlots > 90);
  2155. CHECK (vocodexWithData == vocodexSlots && vocodexSlots >= 10);
  2156. std::printf (" Fruity WaveShaper: %d of %d slots on the exact engine; Fruity Soft Clipper: %d of %d\n", shaperExact, shaperSlots,
  2157. clipperExact, clipperSlots);
  2158. // (Patcher graphs add WaveShaper nodes of their own to the imported mixer)
  2159. CHECK (shaperExact >= shaperSlots && shaperSlots >= 70 && clipperExact >= clipperSlots && clipperSlots >= 25); // REA_rodada51
  2160. std::printf (" Fruity Parametric EQ 2 / Parametric EQ: %d of %d slots on the exact engine\n", flPeqExact, flPeqSlots);
  2161. CHECK (flPeqExact >= flPeqSlots && flPeqSlots >= 1100); // REA_rodada50
  2162. std::printf (" Fruity PanOMatic / Balance / Blood Overdrive: %d of %d slots on the exact engines\n", basicExact, basicSlots);
  2163. // (since round 141 a Patcher stays one slot: "Bombs Away" insert 69 keeps its last PanOMatic)
  2164. CHECK (basicExact >= basicSlots && basicSlots >= 270); // REA_rodada58
  2165. // (Patcher graphs can add Distructor nodes of their own to the imported mixer)
  2166. std::printf (" Distructor: %d of %d slots on the exact engine (%d cabinet impulses read from the installation)\n",
  2167. distructorExact, distructorSlots, distructorImpulses);
  2168. CHECK (distructorExact >= distructorSlots && distructorSlots >= 4 && distructorImpulses >= 1); // REA_rodada71
  2169. std::printf (" EQUO: %d of %d slots on the exact engine\n", equoExact, equoSlots);
  2170. CHECK (equoExact == equoSlots && equoSlots >= 10); // REA_rodada61
  2171. std::printf (" Pitch Shifter: %d of %d slots on the exact engine\n", pitchShifterExact, pitchShifterSlots);
  2172. CHECK (pitchShifterExact == pitchShifterSlots && pitchShifterSlots >= 15); // REA_rodada61
  2173. std::printf (" Fruity Fast LP / Free Filter / Filter: %d of %d slots on the exact engines\n", filterExact, filterSlots);
  2174. CHECK (filterExact >= filterSlots && filterSlots >= 53); // REA_rodada63
  2175. std::printf (" Fruity Delay 2: %d of %d slots on the exact engine; Fruity Delay 3: %d of %d\n", delay2Exact, delay2Slots,
  2176. delay3Exact, delay3Slots);
  2177. // (FL 11 files' slots are read since round 80; Patcher graphs can add Delay 2 nodes of their own)
  2178. CHECK (delay2Exact >= delay2Slots && delay2Slots >= 60 && delay3Exact >= delay3Slots && delay3Slots >= 100); // REA_rodada52
  2179. std::printf (" Chorus family: %d of %d slots on the exact engines\n", chorusExact, chorusSlots);
  2180. CHECK (chorusExact >= chorusSlots && chorusSlots >= 47); // REA_rodada59
  2181. 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,
  2182. fastDistSlots, flangusExact, flangusSlots, flangerExact, flangerSlots);
  2183. CHECK (fastDistExact >= fastDistSlots && fastDistSlots >= 30 && flangusExact >= flangusSlots && flangusSlots >= 30
  2184. && flangerExact >= flangerSlots && flangerSlots >= 3); // REA_rodada55
  2185. std::printf (" Fruity Phaser: %d of %d slots on the exact engine\n", phaserExact, phaserSlots);
  2186. CHECK (phaserExact >= phaserSlots && phaserSlots >= 3); // REA_rodada105_phaser
  2187. // 3x Osc channel presets shipped with FL (.fst): all become synth settings
  2188. int presets = 0, presetsDecoded = 0;
  2189. const auto presetDir = std::filesystem::path (realFlpDir) / "Patches" / "Channel presets" / "3x Osc";
  2190. if (std::filesystem::is_directory (presetDir))
  2191. for (auto& entry : std::filesystem::directory_iterator (presetDir))
  2192. if (entry.path().extension() == ".fst")
  2193. {
  2194. ++presets;
  2195. std::ifstream in (entry.path(), std::ios::binary);
  2196. std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  2197. if (auto synth = lynx::synthFromFlPreset (bytes))
  2198. {
  2199. ++presetsDecoded;
  2200. CHECK (synth->osc[0].level + synth->osc[1].level + (synth->am ? 0.0f : synth->osc[2].level) <= 1.001f);
  2201. }
  2202. }
  2203. std::printf (" %d 3x Osc presets, %d read\n", presets, presetsDecoded);
  2204. CHECK (presetsDecoded == presets);
  2205. // FL effect presets that become built-in effects
  2206. int fxPresets = 0, fxDecoded = 0;
  2207. for (auto kind : { lynx::EffectKind::parametricEq, lynx::EffectKind::limiter, lynx::EffectKind::reverb, lynx::EffectKind::delay,
  2208. lynx::EffectKind::utility })
  2209. for (const auto& plugin : lynx::flPluginsForEffect (kind))
  2210. {
  2211. const auto dir = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Effects" / plugin;
  2212. if (! std::filesystem::is_directory (dir))
  2213. continue;
  2214. for (auto& entry : std::filesystem::recursive_directory_iterator (dir))
  2215. if (entry.path().extension() == ".fst")
  2216. {
  2217. ++fxPresets;
  2218. std::ifstream in (entry.path(), std::ios::binary);
  2219. std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  2220. fxDecoded += lynx::effectFromFlPreset (bytes).has_value();
  2221. }
  2222. }
  2223. std::printf (" %d FL effect presets, %d read\n", fxPresets, fxDecoded);
  2224. CHECK (fxDecoded == fxPresets);
  2225. CHECK (wrappedParsed == wrapped);
  2226. CHECK (wrappedVst2State == wrappedVst2 && wrappedVst2 >= 23 && wrappedVst2Old <= 13);
  2227. std::printf (" %d plugins in Fruity Wrapper, %d read; %d VST2 states, %d decoded (%d not written back byte-identical)\n", wrapped,
  2228. wrappedParsed, wrappedVst2, wrappedVst2State, wrappedVst2Old);
  2229. std::printf (" %d markers, %d time signature changes, %d tracks with EQ\n", markers, signatures, trackEqs);
  2230. std::printf (" %d parametric EQs (%d with FL settings), %d reverbs (%d without dry)\n", peqSlots, peqShaped, reverbs, reverbsDryless);
  2231. std::printf (" DirectWave: %d channels, %d zones; FPC: %d projects, %d pad channels, %d pad notes\n", dwChannels, dwZones, fpcKits,
  2232. padChannels, padNotes);
  2233. CHECK (dwZones > 1000 && padChannels > 250 && padNotes > 500);
  2234. std::printf (" DirectWave on the exact engine (REA_rodada72): %d channels\n", dwExact);
  2235. CHECK (dwExact >= 57);
  2236. std::printf (" sliced channels (Slicer/Slicex): %d; embedded WAVs saved: %d\n", slicedChannels, embeddedSaved);
  2237. CHECK (slicedChannels >= 25 && embeddedSaved >= 10); // without decoding Ogg here, some loops have no duration
  2238. std::printf (" FL Keys (Grand Piano, 30 zones): %d channels\n", flKeysChannels);
  2239. CHECK (flKeysChannels >= 8);
  2240. CHECK (peqSlots > 1000 && peqShaped > 900 && reverbsDryless > 20 && reverbsLowCut > 250);
  2241. CHECK (markers > 300 && signatures > 60);
  2242. std::printf (" %d 3x Osc channels, %d decoded, %d on the exact engine (%d sounding)\n", threeOsc, threeOscDecoded, threeOscKind, threeOscSounding);
  2243. // one demo channel (a noise layer of "Right Night feeling") has the channel filter closed (fast LP, Mod X 0): silent
  2244. // with its saved settings, in FL too
  2245. CHECK (threeOscKind == threeOsc && threeOscSounding == threeOscKind - 1);
  2246. std::printf (" %d .flp files read, %d with notes, %d with playlist, %d automations (%d with target), %d sends\n",
  2247. files, withNotes, withPlaylist, automations, automationsWithTarget, sends);
  2248. CHECK (files > 0);
  2249. CHECK (automations > 0 && sends > 0);
  2250. }
  2251. // ---------------------------------------------------------------------------
  2252. // Sequencer
  2253. TEST (pattern_toggle_step_and_length)
  2254. {
  2255. lynx::Pattern p;
  2256. CHECK (! p.hasStep (0, 4));
  2257. p.toggleStep (0, 4);
  2258. CHECK (p.hasStep (0, 4));
  2259. CHECK (p.notes.size() == 1 && p.notes[0].start == 4 * lynx::ticksPerStep);
  2260. CHECK (p.effectiveLength (4) == 4 * lynx::ppq); // one bar
  2261. p.toggleStep (1, 20); // second bar
  2262. CHECK (p.effectiveLength (4) == 8 * lynx::ppq);
  2263. p.toggleStep (0, 4);
  2264. CHECK (! p.hasStep (0, 4) && p.notes.size() == 1);
  2265. }
  2266. TEST (pattern_mute_removed_steps)
  2267. {
  2268. lynx::Pattern p;
  2269. p.toggleStep (0, 2);
  2270. p.notes[0].velocity = 0.3f;
  2271. p.toggleStep (0, 2, true); // off: muted, kept
  2272. CHECK (! p.hasStep (0, 2) && p.notes.size() == 1 && p.notes[0].muted);
  2273. p.toggleStep (0, 2, true); // on again: the same note back
  2274. CHECK (p.hasStep (0, 2) && p.notes.size() == 1 && ! p.notes[0].muted && p.notes[0].velocity == 0.3f);
  2275. p.toggleStep (0, 2, true);
  2276. // option off (FL's SetStep, round 169f): turning the muted step on adds a new note and leaves the muted one alone...
  2277. p.toggleStep (0, 2);
  2278. CHECK (p.hasStep (0, 2) && p.notes.size() == 2 && p.notes[0].muted != p.notes[1].muted);
  2279. p.toggleStep (0, 2); // ...and turning it off deletes every note of the step, the muted one too
  2280. CHECK (p.notes.empty());
  2281. // option on with two notes on the step: both muted, both back
  2282. p.toggleStep (0, 3);
  2283. p.notes.push_back (p.notes[0]);
  2284. p.notes[1].key = 64;
  2285. p.toggleStep (0, 3, true);
  2286. CHECK (! p.hasStep (0, 3) && p.notes.size() == 2 && p.notes[0].muted && p.notes[1].muted);
  2287. p.toggleStep (0, 3, true);
  2288. CHECK (p.hasStep (0, 3) && p.notes.size() == 2 && ! p.notes[0].muted && ! p.notes[1].muted);
  2289. // another channel's note on the same step is never touched
  2290. p.toggleStep (1, 3);
  2291. p.toggleStep (0, 3, true);
  2292. CHECK (p.hasStep (1, 3) && ! p.hasStep (0, 3));
  2293. }
  2294. TEST (scheduler_places_events_at_exact_samples)
  2295. {
  2296. lynx::Pattern p;
  2297. p.toggleStep (0, 0);
  2298. p.toggleStep (0, 4); // tick 960 = 1 beat
  2299. const double sr = 48000.0, bpm = 120.0; // 1 beat = 0.5 s = 24000 samples
  2300. const auto tps = lynx::ticksPerSample (bpm, sr);
  2301. lynx::EventBuffer ev;
  2302. lynx::schedulePatternLoop (p, p.effectiveLength (4), 23900.0 * tps, 512, tps, ev);
  2303. // the beat-2 note starts at sample 24000 -> offset 100 in this block
  2304. int ons = 0;
  2305. for (auto& e : ev)
  2306. if (e.type == lynx::NoteEvent::Type::on)
  2307. {
  2308. ++ons;
  2309. CHECK (e.sampleOffset == 100);
  2310. }
  2311. CHECK (ons == 1);
  2312. }
  2313. TEST (scheduler_wraps_the_loop)
  2314. {
  2315. lynx::Pattern p;
  2316. p.toggleStep (2, 0);
  2317. const auto len = p.effectiveLength (4); // 3840 ticks
  2318. const double sr = 48000.0;
  2319. const auto tps = lynx::ticksPerSample (120.0, sr);
  2320. lynx::EventBuffer ev;
  2321. // block that crosses the loop end: starts 10 samples before
  2322. const auto start = (double) len - 10.0 * tps;
  2323. lynx::schedulePatternLoop (p, len, start, 64, tps, ev);
  2324. bool found = false;
  2325. for (auto& e : ev)
  2326. if (e.type == lynx::NoteEvent::Type::on && e.channel == 2)
  2327. {
  2328. found = true;
  2329. CHECK (e.sampleOffset == 10);
  2330. }
  2331. CHECK (found);
  2332. }
  2333. TEST (scheduler_off_before_on_at_same_sample)
  2334. {
  2335. lynx::Pattern p;
  2336. lynx::Note a;
  2337. a.start = 0; a.length = 240; a.channel = 0;
  2338. lynx::Note b = a;
  2339. b.start = 240;
  2340. p.notes = { a, b };
  2341. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  2342. lynx::EventBuffer ev;
  2343. lynx::schedulePatternLoop (p, p.effectiveLength (4), 200.0, 4000, tps, ev);
  2344. CHECK (ev.size() >= 2);
  2345. CHECK (ev[0].type == lynx::NoteEvent::Type::off);
  2346. CHECK (ev[1].type == lynx::NoteEvent::Type::on);
  2347. CHECK (ev[0].sampleOffset == ev[1].sampleOffset);
  2348. }
  2349. TEST (snapshot_exchange_hands_over_and_recycles)
  2350. {
  2351. struct Counted
  2352. {
  2353. int value;
  2354. int* alive;
  2355. Counted (int v, int* a) : value (v), alive (a) { ++*alive; }
  2356. ~Counted() { --*alive; }
  2357. };
  2358. int alive = 0;
  2359. {
  2360. lynx::SnapshotExchange<Counted> x;
  2361. CHECK (x.acquire() == nullptr);
  2362. x.publish (std::make_unique<Counted> (1, &alive));
  2363. CHECK (x.acquire()->value == 1);
  2364. x.publish (std::make_unique<Counted> (2, &alive));
  2365. x.publish (std::make_unique<Counted> (3, &alive)); // 2 was never read: freed immediately
  2366. CHECK (alive == 2);
  2367. CHECK (x.acquire()->value == 3);
  2368. CHECK (alive == 2); // 1 was returned, but only the UI frees it
  2369. x.collectGarbage();
  2370. CHECK (alive == 1);
  2371. // without collection, the return slots fill up and the audio side holds on to the current one
  2372. for (int i = 0; i < 20; ++i)
  2373. {
  2374. x.publish (std::make_unique<Counted> (10 + i, &alive));
  2375. CHECK (x.acquire() != nullptr);
  2376. }
  2377. }
  2378. CHECK (alive == 0);
  2379. }
  2380. TEST (rack_renderer_makes_sound_and_reuses_instruments)
  2381. {
  2382. auto song = lynx::Song::makeDefault();
  2383. song.bpm = 120.0;
  2384. const double sr = 48000.0;
  2385. auto state = lynx::RackState::build (song, nullptr, sr);
  2386. lynx::RackRenderer r;
  2387. r.prepare (sr);
  2388. std::vector<float> l (512), rr (512);
  2389. double tick = 0.0;
  2390. float peak = 0.0f;
  2391. int kicks = 0;
  2392. for (int block = 0; block < 93; ++block) // 47616 samples: just before 1 s
  2393. {
  2394. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  2395. for (auto& e : r.lastEvents())
  2396. if (e.type == lynx::NoteEvent::Type::on && e.channel == 0)
  2397. ++kicks;
  2398. for (int i = 0; i < 512; ++i)
  2399. {
  2400. CHECK (std::isfinite (l[(std::size_t) i]));
  2401. peak = std::max (peak, std::abs (l[(std::size_t) i]));
  2402. }
  2403. tick += 512 * lynx::ticksPerSample (song.bpm, sr);
  2404. }
  2405. CHECK (peak > 0.1f && peak < 4.0f);
  2406. CHECK (kicks == 2); // 120 BPM: beats at 0 and 0.5 s
  2407. // editing the song keeps the instruments of channels whose type didn't change
  2408. song.channels[4].instrument = lynx::InstrumentKind::kick;
  2409. auto next = lynx::RackState::build (song, state.get(), sr);
  2410. CHECK (next->instruments[0] == state->instruments[0]);
  2411. CHECK (next->instruments[4] != state->instruments[4]);
  2412. // stopping silences the synth and triggers no new notes
  2413. r.process (next.get(), false, tick, 512, l.data(), rr.data());
  2414. CHECK (r.lastEvents().size() == 0);
  2415. }
  2416. TEST (flp_import_maps_channels_and_rescales)
  2417. {
  2418. flp::Project p;
  2419. p.header.ppq = 96;
  2420. p.tempo = 150.0;
  2421. p.channels = { { 0, flp::ChannelType::sampler, "Kick 808", "", "", 1 },
  2422. { 1, flp::ChannelType::audioClip, "Vocal", "", "", 2 },
  2423. { 2, flp::ChannelType::instrument, "Lead", "FLEX", "", 3 } };
  2424. flp::Pattern pat;
  2425. pat.index = 1;
  2426. flp::Note n;
  2427. n.position = 96;
  2428. n.length = 0;
  2429. n.channel = 2;
  2430. n.key = 72;
  2431. n.velocity = 64;
  2432. pat.notes.push_back (n);
  2433. p.patterns.push_back (pat);
  2434. const auto s = lynx::importFlp (p);
  2435. CHECK (s.bpm == 150.0);
  2436. CHECK (s.channels.size() == 3); // audio clip channels stay in the rack, as in FL
  2437. CHECK (s.channels[0].instrument == lynx::InstrumentKind::kick);
  2438. CHECK (s.channels[1].instrument == lynx::InstrumentKind::audioClip);
  2439. CHECK (s.channels[2].instrument == lynx::InstrumentKind::synth);
  2440. CHECK (s.patterns.size() == 1);
  2441. CHECK (s.patterns[0].notes[0].start == lynx::ppq); // 96 @ PPQ 96 = 1 beat
  2442. CHECK (s.patterns[0].notes[0].channel == 2);
  2443. CHECK (s.patterns[0].notes[0].length == lynx::ticksPerStep); // step note
  2444. CHECK (std::abs (s.patterns[0].notes[0].velocity - 0.5f) < 1e-6f);
  2445. }
  2446. TEST (live_queue_is_fifo_and_bounded)
  2447. {
  2448. lynx::LiveEventQueue q;
  2449. lynx::NoteEvent e;
  2450. CHECK (! q.pop (e));
  2451. for (int i = 0; i < 10; ++i)
  2452. {
  2453. lynx::NoteEvent in;
  2454. in.key = i;
  2455. CHECK (q.push (in));
  2456. }
  2457. for (int i = 0; i < 10; ++i)
  2458. CHECK (q.pop (e) && e.key == i);
  2459. int pushed = 0;
  2460. while (q.push (e))
  2461. ++pushed;
  2462. CHECK (pushed == (int) lynx::LiveEventQueue::capacity - 1); // one slot stays free to tell full from empty
  2463. }
  2464. // Whether this CPU's RCPPS (the ~12-bit reciprocal estimate, whose table is implementation-defined: Intel and AMD
  2465. // 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
  2466. // every float mantissa in [1, 2) (the estimate does not depend on the exponent in the normal range)
  2467. static bool rcpLikeReferenceCpu()
  2468. {
  2469. #if defined(__SSE__) || defined(_M_X64) || defined(__x86_64__) || defined(__aarch64__) || defined(_M_ARM64) // x86 SSE, or ARM64 through core/Simd.h (sse2neon)
  2470. std::uint64_t h = 1469598103934665603ull;
  2471. for (std::uint32_t m = 0; m < (1u << 23); m += 4)
  2472. {
  2473. alignas (16) float f[4];
  2474. for (std::uint32_t j = 0; j < 4; ++j)
  2475. {
  2476. const std::uint32_t bits = 0x3f800000u | (m + j);
  2477. std::memcpy (&f[j], &bits, 4);
  2478. }
  2479. _mm_store_ps (f, _mm_rcp_ps (_mm_load_ps (f)));
  2480. for (std::uint32_t j = 0; j < 4; ++j)
  2481. {
  2482. std::uint32_t bits;
  2483. std::memcpy (&bits, &f[j], 4);
  2484. h = (h ^ bits) * 1099511628211ull;
  2485. }
  2486. }
  2487. return h == 0xb5de670652cb0383ull;
  2488. #else
  2489. return false;
  2490. #endif
  2491. }
  2492. // FL's sampler interpolators (REA_rodada69 §1): outputs of FLEngine's own kernels (EXEC, r69/py/gold.py) on a synthetic
  2493. // 16-bit stereo signal; the port must give the same bits (linear, 6-point, 24-point sinc, 32-point sinc at speed 1.37,
  2494. // 16-point sinc widened to 24 points with the cutoff at π/9.5 at speed 9.5)
  2495. // 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
  2496. // required where RCPPS matches the capture machine's; elsewhere (Intel CPUs) within a few float ulps.
  2497. TEST (fl_sampler_interpolators_match_fl)
  2498. {
  2499. std::vector<float> l, r;
  2500. std::uint32_t x = 12345;
  2501. for (int k = 0; k < 2 * 144; ++k)
  2502. {
  2503. x = x * 1103515245u + 12345u;
  2504. const auto v = (float) (std::int16_t) (std::uint16_t) (x >> 16) / 32768.0f;
  2505. (k % 2 == 0 ? l : r).push_back (v);
  2506. }
  2507. const int pad = 40;
  2508. 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 } };
  2509. struct Case { int quality; double speed; std::uint32_t bits[16]; };
  2510. const Case cases[] = {
  2511. { 1, 1.0, { 0x3ec1a000, 0x3d774000, 0x3ca1e000, 0x3ec39900, 0xbf2d4600, 0xbe096800, 0xbea0cd17, 0x3f433097, 0x3f2796da, 0xbd9fbd17, 0x3f27a3ff, 0xbda03ff2, 0x3dace746, 0x3eba19c4, 0x3e477a00, 0x3f0f2a80 } },
  2512. { 2, 1.0, { 0x3ec1a000, 0x3d773fff, 0x3d34885d, 0x3eedeaa0, 0xbf49e298, 0xbea52fc4, 0xbeba55fc, 0x3f7c58da, 0x3f27c5c2, 0xbda139af, 0x3f27a3ff, 0xbda03ffa, 0x3dd8e7b1, 0x3ee872c1, 0x3e05b2d5, 0x3f144c4f } },
  2513. { 4, 1.0, { 0x3ec19fff, 0x3d773fff, 0x3d889c32, 0x3ef6b184, 0xbf57cf0d, 0xbf0abecd, 0xbea14a13, 0x3f94f11b, 0x3f27edc4, 0xbda10d8c, 0x3f27a3fd, 0xbda03ffa, 0x3e5baf41, 0x3edb298c, 0x3dd76cbf, 0x3ef9d212 } },
  2514. { 5, 1.37, { 0x3f19d373, 0x3ea9b6a3, 0xbdcbe56e, 0x3e2af6c6, 0xbf476f7a, 0xbe566a13, 0xbe9f166a, 0x3f52b5ad, 0x3f27ad16, 0xbce48792, 0x3f27960a, 0xbce01ebe, 0x3e1b96be, 0x3e163fd8, 0x3f042e42, 0x3ec4be83 } },
  2515. { 3, 9.5, { 0xbd97308e, 0x3dd8783c, 0xbd911374, 0x3e162a41, 0xbd8a959e, 0x3e38b220, 0xbd7e6785, 0x3e58895c, 0xbd380c4f, 0x3e7e3008, 0xbd380e4e, 0x3e7e2696, 0x3d00c6d0, 0xbd627d87, 0x3e308cb8, 0xbcece918 } },
  2516. };
  2517. const lynx::flvp::X87Extended precision;
  2518. const bool exactRcp = rcpLikeReferenceCpu();
  2519. for (const auto& c : cases)
  2520. {
  2521. lynx::flrs::Kernel k;
  2522. k.setQuality (c.quality);
  2523. k.setSpeed (c.speed);
  2524. int same = 0;
  2525. for (int p = 0; p < 8; ++p)
  2526. {
  2527. const auto lo = pad + positions[p].first - k.halfWidth + 1;
  2528. float a, b;
  2529. lynx::flrs::interpolate (k, l.data() + lo, r.data() + lo, positions[p].second, a, b);
  2530. std::uint32_t ba, bb;
  2531. std::memcpy (&ba, &a, 4);
  2532. std::memcpy (&bb, &b, 4);
  2533. if (exactRcp || c.quality < 3) // (linear and 6-point use no reciprocal)
  2534. same += (ba == c.bits[2 * p]) + (bb == c.bits[2 * p + 1]);
  2535. else
  2536. 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);
  2537. }
  2538. CHECK (same == 16);
  2539. }
  2540. CHECK (lynx::flrs::halfWidthFor (4) == 12 && lynx::flrs::halfWidthFor (5) == 16 && lynx::flrs::halfWidthFor (9) == 256);
  2541. CHECK (lynx::flrs::speedFor (0.0f, 1.0) == 1.0 && std::abs (lynx::flrs::speedFor (1200.0f, 0.5) - 1.0) < 1e-12);
  2542. }
  2543. // The sampler at the original rate copies the sample (FL's copy path), and plays until the read position passes the
  2544. // last frame
  2545. TEST (fl_sampler_copies_at_original_rate)
  2546. {
  2547. auto data = std::make_shared<lynx::SampleData>();
  2548. data->sampleRate = 48000.0;
  2549. for (int i = 0; i < 300; ++i)
  2550. data->left.push_back (std::sin (0.05f * (float) i) * 0.5f);
  2551. auto sampler = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  2552. sampler->prepare (48000.0);
  2553. sampler->noteOn (60, 1.0f);
  2554. std::vector<float> l (400), r (400);
  2555. sampler->render (l.data(), r.data(), 400, 1.0f, 1.0f);
  2556. const auto panL = std::cos ((0.0f + 1.0f) * 0.25f * 3.14159265f) * 1.41421356f; // the sampler's centre pan gain
  2557. int exact = 0;
  2558. for (int i = 0; i < 300; ++i)
  2559. exact += l[(std::size_t) i] == data->left[(std::size_t) i] * panL ? 1 : 0;
  2560. CHECK (exact == 300);
  2561. CHECK (l[300] == 0.0f);
  2562. }
  2563. // FL's volume envelope on the sampler (REA_rodada69 §3): the articulator is stepped once per sequencer tick and the level
  2564. // ramps linearly to each tick's value; 96 PPQ at 120 BPM and 48 kHz = 250 samples per tick, attack 20000 = 89 ticks
  2565. TEST (fl_sampler_volume_envelope_steps_per_tick)
  2566. {
  2567. auto data = std::make_shared<lynx::SampleData>();
  2568. data->sampleRate = 48000.0;
  2569. data->left.assign (96000, 0.5f);
  2570. auto sampler = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  2571. sampler->prepare (48000.0);
  2572. lynx::ChannelDef c;
  2573. c.instrument = lynx::InstrumentKind::sampler;
  2574. c.sampler.flVolumeEnvelope = { 0, 1, 100, 20000, 0, 30000, 128, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, 0, 96 };
  2575. sampler->configure (c, 120.0);
  2576. sampler->noteOn (60, 1.0f);
  2577. std::vector<float> l (40000), r (40000);
  2578. sampler->render (l.data(), r.data(), 40000, 1.0f, 1.0f);
  2579. const auto panL = std::cos ((0.0f + 1.0f) * 0.25f * 3.14159265f) * 1.41421356f;
  2580. auto level = [&] (int n) { return l[(std::size_t) n] / (0.5f * panL); };
  2581. CHECK (std::abs (level (1000) - 1000.0f / 22500.0f) < 2e-5f);
  2582. CHECK (std::abs (level (10125) - 10125.0f / 22500.0f) < 2e-5f);
  2583. CHECK (std::abs (level (39000) - 1.0f) < 1e-6f); // the attack ended after 90 ticks; sustain 128 = 1.0
  2584. // release: 20000 = 89 ticks, linear (tension 0), from the current level
  2585. sampler->noteOff (60);
  2586. std::fill (l.begin(), l.end(), 0.0f);
  2587. sampler->render (l.data(), r.data(), 40000, 1.0f, 1.0f);
  2588. CHECK (std::abs (level (2500) - (1.0f - 10.0f / 90.0f)) < 1e-3f);
  2589. CHECK (l[39999] == 0.0f && sampler->idle());
  2590. }
  2591. // FL's native sampler voice (REA_rodada76): FL's own command-line renders of crafted projects (one sampler channel,
  2592. // 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
  2593. // note pan/velocity set; the 12 dB LP with volume, cutoff and pitch envelopes and their releases. FL's int16 codes at
  2594. // a few frames; LYNX within one code. Also the velocity law and the pan law with the channel's levels inside the voice.
  2595. TEST (fl_native_sampler_voice_matches_fl_renders)
  2596. {
  2597. auto noise = std::make_shared<lynx::SampleData>();
  2598. noise->sampleRate = 44100.0;
  2599. {
  2600. std::uint32_t seed = 1;
  2601. for (int i = 0; i < 26460; ++i)
  2602. {
  2603. seed = (seed * 1103515245u + 12345u) & 0x7fffffffu;
  2604. noise->left.push_back ((float) (((double) seed / 2147483647.0 * 2.0 - 1.0) * 0.3));
  2605. }
  2606. }
  2607. struct Ev { int on, off; float vel, pan; };
  2608. auto render = [&] (std::vector<std::int32_t> recs, std::array<int, 3> filter, const std::vector<Ev>& notes,
  2609. std::shared_ptr<lynx::SampleData> data, float chanGain) {
  2610. lynx::ChannelDef c;
  2611. c.instrument = lynx::InstrumentKind::sampler;
  2612. c.sampler.flChannel = recs;
  2613. c.sampler.flChannel.insert (c.sampler.flChannel.end(), { filter[0], filter[1], filter[2], 96 });
  2614. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  2615. lynx::setOfflineRendering (true); // FL's render dialog: 32-point sinc (the pitch envelope interpolates)
  2616. inst->prepare (44100.0);
  2617. lynx::setOfflineRendering (false);
  2618. inst->configure (c, 120.0);
  2619. inst->setSongPosition (0.0);
  2620. std::vector<std::pair<int, int>> events; // (sample, index ≥ 0 on / < 0 off)
  2621. for (int i = 0; i < (int) notes.size(); ++i)
  2622. events.push_back ({ notes[(std::size_t) i].on, i }), events.push_back ({ notes[(std::size_t) i].off, -1 - i });
  2623. std::sort (events.begin(), events.end());
  2624. std::vector<float> l (120000), r (120000);
  2625. int at = 0;
  2626. for (const auto& [when, i] : events)
  2627. {
  2628. inst->render (l.data() + at, r.data() + at, when - at, chanGain, chanGain);
  2629. at = when;
  2630. if (i >= 0)
  2631. {
  2632. inst->setNextNoteExpression (notes[(std::size_t) i].pan, 0.0f, false);
  2633. inst->noteOn (60, notes[(std::size_t) i].vel);
  2634. }
  2635. else
  2636. inst->noteOff (60);
  2637. }
  2638. inst->render (l.data() + at, r.data() + at, 120000 - at, chanGain, chanGain);
  2639. return std::make_pair (l, r);
  2640. };
  2641. const std::vector<std::int32_t> plain = { 0, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, 0 };
  2642. auto records = [&] (std::vector<std::int32_t> vol, std::vector<std::int32_t> cut, std::vector<std::int32_t> pitch) {
  2643. std::vector<std::int32_t> all = plain;
  2644. for (const auto* r : std::initializer_list<const std::vector<std::int32_t>*> { &vol, &cut, &plain, &pitch })
  2645. all.insert (all.end(), r->begin(), r->end());
  2646. return all;
  2647. };
  2648. 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 };
  2649. const auto chan = lynx::channelGain (10000.0f / 12800.0f); // the rack's gain at pan 0 (panGains: gain·√2·cos(π/4))
  2650. const int at[] = { 5, 300, 2000, 9000, 11500, 15000, 22100, 26000 };
  2651. auto check = [&] (const std::pair<std::vector<float>, std::vector<float>>& out, const int* goldL, const int* goldR) {
  2652. int bad = 0;
  2653. for (int n = 0; n < 3; ++n)
  2654. for (int k = 0; k < 8; ++k)
  2655. {
  2656. const auto p = (std::size_t) (44100 * n + at[k]);
  2657. bad += std::abs ((int) std::lround (out.first[p] * 32767.0f) - goldL[8 * n + k]) > 1;
  2658. bad += std::abs ((int) std::lround (out.second[p] * 32767.0f) - goldR[8 * n + k]) > 1;
  2659. }
  2660. return bad;
  2661. };
  2662. {
  2663. // fast LP (Mod X 100, Mod Y 200); notes: len 96, (vel 80, pan 40) len 48, (vel 120, pan 90) len 144
  2664. const auto out = render (records (volPlain, plain, plain), { 100, 200, 0 },
  2665. { { 0, 22050, 100.0f / 128, 0.0f }, { 44100, 55125, 80.0f / 128, -24.0f / 64 }, { 88200, 121275, 120.0f / 128, 26.0f / 64 } },
  2666. noise, chan);
  2667. 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 };
  2668. 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 };
  2669. CHECK (check (out, L, R) == 0);
  2670. }
  2671. {
  2672. // 12 dB LP (Mod X 90, Mod Y 60), volume / cutoff / pitch envelopes; notes len 48, (vel 80, pan 30) len 48, len 30
  2673. const auto out = render (records ({ 4, 1, 100, 3000, 20000, 30000, 100, 6000, 0, 100, 20000, 0, 32950, 0, 0, 0, -101 },
  2674. { 0, 1, 100, 6000, 2000, 15000, 40, 8000, 100, 100, 20000, 0, 32950, 0, 0, 0, 0 },
  2675. { 0, 1, 100, 4000, 20000, 9000, 0, 20000, 600, 100, 20000, 0, 32950, 0, 0, 0, 0 }),
  2676. { 90, 60, 1 },
  2677. { { 0, 11025, 100.0f / 128, 0.0f }, { 44100, 55125, 80.0f / 128, -34.0f / 64 }, { 88200, 95091, 100.0f / 128, 0.0f } },
  2678. noise, chan);
  2679. 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 };
  2680. 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 };
  2681. CHECK (check (out, L, R) == 0);
  2682. }
  2683. {
  2684. // a global volume LFO (flag 0x20, amount 80, speed 24000): one phase for the channel, (k + 1)·increment at song
  2685. // tick k (notes on ticks 0, 100, 300)
  2686. auto vol = volPlain;
  2687. vol[0] = 4 | 0x20, vol[11] = 80, vol[12] = 24000;
  2688. const auto out = render (records (vol, plain, plain), { 256, 0, 0 },
  2689. { { 0, 22050, 100.0f / 128, 0.0f }, { 22969, 45019, 80.0f / 128, 0.0f }, { 68906, 90956, 100.0f / 128, 0.0f } },
  2690. noise, chan);
  2691. const int pos[] = { 500, 3000, 7000, 12000, 23469, 26000, 30000, 69406, 72000, 80000 };
  2692. const int gold[] = { 1149, 765, 374, 4530, 846, -911, -646, 1160, 2316, 727 };
  2693. int bad = 0;
  2694. for (int k = 0; k < 10; ++k)
  2695. bad += std::abs ((int) std::lround (out.first[(std::size_t) pos[k]] * 32767.0f) - gold[k]) > 1;
  2696. CHECK (bad == 0);
  2697. }
  2698. {
  2699. // a sample with its own loop (WAV smpl 4410…8819): FL loops it while the note lasts and fades it out over 10 ms
  2700. // after the note-off (no volume envelope)
  2701. auto looped = std::make_shared<lynx::SampleData>();
  2702. looped->sampleRate = 44100.0;
  2703. for (int i = 0; i < 8820; ++i)
  2704. 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)));
  2705. looped->loopStart = 4410, looped->loopEnd = 8820;
  2706. const auto out = render (records (volPlain, plain, plain), { 256, 0, 0 },
  2707. { { 0, 22050, 100.0f / 128, 0.0f }, { 44100, 49612, 100.0f / 128, 0.0f } }, looped, chan);
  2708. const int pos[] = { 125, 4525, 8925, 13337, 17725, 22025, 22125, 22325, 22437, 49625, 49737, 50025 };
  2709. const int gold[] = { 6338, 4746, 3842, 4161, -1470, -989, -3577, -1174, -32, 4373, 2573, 42 };
  2710. int bad = 0;
  2711. for (int k = 0; k < 12; ++k)
  2712. bad += std::abs ((int) std::lround (out.first[(std::size_t) pos[k]] * 32767.0f) - gold[k]) > 1;
  2713. CHECK (bad == 0);
  2714. }
  2715. {
  2716. // a constant sample: velocity law (FL: vel 100 → 0.389608, 128 → 0.799753, 64 → 0.141805 at the default
  2717. // channel volume, pan centre) and a hard-left note (0.550997 / 0)
  2718. auto dc = std::make_shared<lynx::SampleData>();
  2719. dc->sampleRate = 44100.0;
  2720. dc->left.assign (20000, 0.5f);
  2721. auto level = [&] (float vel, float pan, bool right) {
  2722. const auto out = render (records (volPlain, plain, plain), { 256, 0, 0 }, { { 0, 10000, vel, pan } }, dc, chan);
  2723. return (right ? out.second : out.first)[5000] / 0.5f;
  2724. };
  2725. CHECK (std::abs (level (100.0f / 128, 0.0f, false) - 0.389608f) < 3e-5f);
  2726. CHECK (std::abs (level (1.0f, 0.0f, false) - 0.799753f) < 3e-5f);
  2727. CHECK (std::abs (level (64.0f / 128, 0.0f, false) - 0.141805f) < 3e-5f);
  2728. CHECK (std::abs (level (100.0f / 128, -1.0f, false) - 0.550997f) < 3e-5f);
  2729. CHECK (std::abs (level (100.0f / 128, -1.0f, true)) < 1e-6f);
  2730. }
  2731. }
  2732. // FPC's "cycle" layers (REA_rodada69 §4): among the layers covering the velocity, in order, the one after the last
  2733. // chosen, wrapping; FL's velocity index is round(v·128). Slicex's master volume uses FL's L11 law (§5).
  2734. TEST (fl_fpc_cycle_layers_and_slicex_master_volume)
  2735. {
  2736. auto level = [] (float v) {
  2737. auto d = std::make_shared<lynx::SampleData>();
  2738. d->sampleRate = 48000.0;
  2739. d->left.assign (4800, v);
  2740. return d;
  2741. };
  2742. // A covers 0…63, B 32…95, C 64…127 (all keys)
  2743. auto inst = lynx::makeZonedSampler ({ { { "a.wav", 60, 0, 127, 0, 63 }, level (0.1f) },
  2744. { { "b.wav", 60, 0, 127, 32, 95 }, level (0.2f) },
  2745. { { "c.wav", 60, 0, 127, 64, 127 }, level (0.3f) } });
  2746. inst->prepare (48000.0);
  2747. inst->configure (lynx::ChannelDef {}, 120.0);
  2748. auto play = [&] (float velocity) {
  2749. inst->allNotesOff();
  2750. std::vector<float> l (2000), r (2000);
  2751. inst->render (l.data(), r.data(), 2000, 1.0f, 1.0f);
  2752. std::fill (l.begin(), l.end(), 0.0f);
  2753. inst->noteOn (60, velocity);
  2754. inst->render (l.data(), r.data(), 10, 1.0f, 1.0f);
  2755. return l[5] / velocity;
  2756. };
  2757. auto which = [] (float x) { return (int) std::lround (x / 0.1f / std::cos (0.25f * 3.14159265f) / 1.41421356f); };
  2758. CHECK (which (play (64.0f / 128.0f)) == 2); // candidates B, C: first B
  2759. CHECK (which (play (64.0f / 128.0f)) == 3); // then C
  2760. CHECK (which (play (40.0f / 128.0f)) == 1); // candidates A, B: C is not among them → the first, A
  2761. CHECK (which (play (40.0f / 128.0f)) == 2); // then B
  2762. CHECK (which (play (64.0f / 128.0f)) == 3); // B was last → C
  2763. // Slicex state: version, 8 master values (volume 16000), options and UI state, deck A referencing a file
  2764. std::vector<std::uint8_t> state (4 + 32 + 47 + 12, 0);
  2765. const std::int32_t version = 0x00060003, volume = 16000;
  2766. std::memcpy (state.data(), &version, 4);
  2767. std::memcpy (state.data() + 4 + 8, &volume, 4);
  2768. state.push_back (1);
  2769. state.push_back (5);
  2770. for (char ch : std::string ("x.wav"))
  2771. state.push_back ((std::uint8_t) ch);
  2772. const auto sliced = lynx::slicex (state);
  2773. CHECK (sliced.has_value() && sliced->path == "x.wav");
  2774. CHECK (sliced && std::abs (sliced->gain - (float) ((std::pow (11.0, 1.25) - 1.0) / 10.0)) < 1e-5f);
  2775. }
  2776. TEST (fl_keys_state_wti_and_loader)
  2777. {
  2778. using namespace lynx::flkeys;
  2779. // state: u32 1, the wavetable name, 15 int32 knobs
  2780. State st;
  2781. st.wavetable = "Grand Piano";
  2782. for (int i = 0; i < 15; ++i)
  2783. st.knobs[(std::size_t) i] = 10 * i - 50;
  2784. const auto bytes = writeState (st);
  2785. CHECK (bytes.size() == 4 + 1 + 11 + 60);
  2786. const auto back = parseState (bytes);
  2787. CHECK (back && back->wavetable == "Grand Piano" && back->knobs == st.knobs);
  2788. // one WAV per keygroup: the file's own loop gives the default end and loop; a keygroup without a file reuses
  2789. // the previous one; a repeated file name copies the earlier keygroup; each file is followed by a guard frame
  2790. // that repeats the frame `loop` before the end (REA_rodada69 §8.2)
  2791. std::vector<std::string> names;
  2792. auto riff = parseWti ("[info]\nsamples=3\nfiletype=riff\npath=W\nvelocityfactor=2\nsamplerate=22050\n"
  2793. "[sample0]\nname=a.wav\nroot=40\nhigh=50\n[sample1]\nroot=51\nhigh=999\npos=2\nend=5\nloop=3\n"
  2794. "[sample2]\nname=A.WAV\nroot=60\nhigh=70\n", names);
  2795. CHECK (riff && names.size() == 1 && names[0] == "W\\a.wav");
  2796. CHECK (riff && riff->keygroups.size() == 3 && riff->keygroups[2].duplicateOf == 0 && riff->thresholds == std::vector<int> { 0 });
  2797. if (riff)
  2798. {
  2799. riff->files[0].loopStart = 4, riff->files[0].loopEnd = 8;
  2800. FileSamples a;
  2801. for (int i = 0; i < 10; ++i)
  2802. a.samples.push_back ((std::int16_t) (100 * (i + 1)));
  2803. const auto decoded = FlKeysTable::decode (riff->encode());
  2804. CHECK (decoded.has_value());
  2805. const auto w = buildWavetable (*decoded, { a });
  2806. CHECK (w != nullptr);
  2807. if (w != nullptr)
  2808. {
  2809. 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; };
  2810. CHECK (same (w->keygroups[0], { 40, 50, 0, 8, 5 }));
  2811. CHECK (same (w->keygroups[1], { 51, 999, 2, 5, 3 }));
  2812. CHECK (same (w->keygroups[2], { 60, 70, 0, 8, 5 }));
  2813. CHECK (w->data[9] == 400 && w->data[10] == 0 && w->data[8] == 900);
  2814. CHECK (w->sampleRate == 22050.0f && w->velocityFactor == 2.0f && ! w->stereo);
  2815. }
  2816. }
  2817. // one raw file: every keygroup's part is copied one after another with a guard sample; "canloop=false" makes the
  2818. // loop as long as the keygroup's end, so the voice stops there
  2819. auto raw = parseWti ("[info]\nsamples=2\nsamplefile=x.dat\nfiletype=raw\n[sample0]\nroot=36\nhigh=47\npos=0\nend=3\nloop=2\n"
  2820. "[sample1]\nroot=48\nhigh=999\npos=4\nend=7\nloop=4\ncanloop=false\n", names);
  2821. CHECK (raw && raw->raw && names.size() == 1 && names[0] == "x.dat");
  2822. if (raw)
  2823. {
  2824. FileSamples x;
  2825. for (int i = 0; i < 10; ++i)
  2826. x.samples.push_back ((std::int16_t) (10 * (i + 1)));
  2827. const auto w = buildWavetable (*raw, { x });
  2828. CHECK (w != nullptr);
  2829. if (w != nullptr)
  2830. {
  2831. CHECK (w->keygroups[0].pos == 0 && w->keygroups[0].end == 3 && w->keygroups[0].loop == 2);
  2832. CHECK (w->keygroups[1].pos == 5 && w->keygroups[1].end == 8 && w->keygroups[1].loop == 8);
  2833. const std::vector<std::int16_t> expected { 10, 20, 30, 40, 30, 50, 60, 70, 80, 50, 0, 0 };
  2834. CHECK (std::equal (expected.begin(), expected.end(), w->data.begin()));
  2835. // a held note on the non-looping keygroup ends by itself; 33 notes leave 32 sounding (FL's MaxPoly)
  2836. State s2;
  2837. s2.knobs = { -1000, 0, 0, -1000, 500, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  2838. RefNote n;
  2839. n.key = 60;
  2840. const auto out = renderReference (*w, s2, { n }, 44100.0, 4096, 512, true);
  2841. double early = 0, late = 0;
  2842. for (int i = 0; i < 64; ++i)
  2843. early += std::fabs (out[(std::size_t) (2 * i)]);
  2844. for (int i = 2048; i < 4096; ++i)
  2845. late += std::fabs (out[(std::size_t) (2 * i)]);
  2846. CHECK (early > 0.0 && late == 0.0);
  2847. }
  2848. }
  2849. // instrument front-end without a wavetable: silent and idle
  2850. auto inst = makeFlKeysInstrument (nullptr);
  2851. inst->prepare (44100.0);
  2852. inst->noteOn (60, 0.78f);
  2853. std::vector<float> l (256), r (256);
  2854. inst->render (l.data(), r.data(), 256, 1.0f, 1.0f);
  2855. CHECK (inst->idle() && l[100] == 0.0f);
  2856. }
  2857. TEST (ogg_vorbis_decodes_like_fl)
  2858. {
  2859. using namespace lynx::oggvorbis;
  2860. 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 };
  2861. CHECK (isOggWav (wavHeader, sizeof wavHeader));
  2862. CHECK (! isOggStream (wavHeader, sizeof wavHeader));
  2863. Decoded none;
  2864. CHECK (! decode (wavHeader, sizeof wavHeader, none) && none.channels == 0);
  2865. // FNV-1a 64 of the planar float32 samples FL's oggio_x64.dll decodes from installation files (REA_rodada77 §3):
  2866. // a plain file, one with a second (PCM) stream multiplexed in, one where an empty stream's end-of-stream page
  2867. // ends the decode early (FL keeps 17 728 of 20 691 frames), and an FL Keys wavetable
  2868. if (realFlpDir.empty())
  2869. return;
  2870. const auto root = std::filesystem::path (realFlpDir).parent_path();
  2871. const struct { const char* file; std::uint64_t hash; std::size_t frames; int channels; } cases[] = {
  2872. { "Data/Patches/Packs/Drums/Percussion/FPC Wood Hit 2.wav", 0xe6137a6141cd5946ull, 29664, 1 },
  2873. { "Data/Patches/Packs/Instruments/Keyboard/Grand/FL Piano (11).wav", 0x3f60df388ac0c636ull, 264600, 2 },
  2874. { "Data/Patches/Packs/Drums/Percussion/Grv Perc 08.wav", 0x7f235f467e90efe0ull, 17728, 0 },
  2875. { "Plugins/Fruity/Generators/FL Keys/Wavtables/Grand Piano/CHANGPIANOHARD_A8.wav", 0x00bbf4cc42f5f72cull, 153280, 0 },
  2876. };
  2877. for (const auto& c : cases)
  2878. {
  2879. const auto path = root / c.file;
  2880. if (! std::filesystem::exists (path))
  2881. continue;
  2882. std::ifstream in (path, std::ios::binary);
  2883. const std::vector<std::uint8_t> bytes { std::istreambuf_iterator<char> (in), {} };
  2884. CHECK (isOggWav (bytes.data(), bytes.size()));
  2885. Decoded d;
  2886. CHECK (decode (bytes.data(), bytes.size(), d));
  2887. if (d.channels == 0)
  2888. continue;
  2889. std::uint64_t h = 1469598103934665603ULL;
  2890. for (const auto& ch : d.data)
  2891. {
  2892. const auto* p = reinterpret_cast<const std::uint8_t*> (ch.data());
  2893. for (std::size_t i = 0; i < ch.size() * sizeof (float); ++i)
  2894. h = (h ^ p[i]) * 1099511628211ULL;
  2895. }
  2896. CHECK (d.data[0].size() == c.frames && d.sampleRate == 44100);
  2897. CHECK (c.channels == 0 || d.channels == c.channels);
  2898. CHECK (h == c.hash);
  2899. }
  2900. }
  2901. TEST (directwave_crt_math_and_state)
  2902. {
  2903. using namespace lynx::dw;
  2904. auto f = [] (std::uint32_t b) { float x; std::memcpy (&x, &b, 4); return x; };
  2905. auto u = [] (float x) { std::uint32_t b; std::memcpy (&b, &x, 4); return b; };
  2906. // the plugin's own run-time (r72/run/mt.exe on DirectWave_x64.dll): values where the libm result differs
  2907. CHECK (u (sinfCrt (f (0x40b1a97fu))) == 0xbf2af4bbu);
  2908. CHECK (u (sinfCrt (f (0xc0ad8469u))) == 0x3f4222f6u);
  2909. CHECK (u (sinfCrt (f (0xc016e8c7u))) == 0xbf34b32du);
  2910. CHECK (u (cosfCrt (f (0xc0af1cc0u))) == 0x3f305702u);
  2911. CHECK (u (cosfCrt (f (0x3f15cba3u))) == 0x3f556924u);
  2912. CHECK (u (cosfCrt (f (0xc0aeb047u))) == 0x3f2dddfeu);
  2913. CHECK (u (expfCrt (f (0xbfb679c0u))) == 0x3e7622a1u);
  2914. CHECK (encodeFormats ({ true, false }) == std::vector<float> ({ 1.0f, 2.0f, 1.0f, 0.0f }));
  2915. CHECK (decodeFormats ({ 1.0f, 2.0f, 1.0f, 0.0f }) == std::vector<bool> ({ true, false }));
  2916. CHECK (! parseState ({ 1, 2, 3 }).has_value());
  2917. }
  2918. TEST (directwave_matches_fl_with_demo_programs)
  2919. {
  2920. // Demo programs (voices, mod matrix, filters, chorus, reverb, shaper) with our own 16-bit samples in every zone:
  2921. // FNV-1a 64 of DirectWave_x64.dll's output (r72/py/synth.py + cmp.py: 44.1 kHz, host blocks of 512, tempo 120)
  2922. if (realFlpDir.empty())
  2923. return;
  2924. using namespace lynx::dw;
  2925. auto readAll = [] (const std::filesystem::path& p) {
  2926. std::ifstream in (p, std::ios::binary);
  2927. return std::vector<std::uint8_t> { std::istreambuf_iterator<char> (in), {} };
  2928. };
  2929. auto stateOf = [&] (const std::string& file, const std::string& channel) {
  2930. const auto path = std::filesystem::path (realFlpDir) / file;
  2931. if (! std::filesystem::exists (path))
  2932. return std::vector<std::uint8_t>();
  2933. const auto p = flp::parseProject (readAll (path));
  2934. for (const auto& c : p.channels)
  2935. if (c.pluginName == "DirectWave" && c.name == channel)
  2936. return c.pluginState;
  2937. return std::vector<std::uint8_t>();
  2938. };
  2939. auto sampleValue = [] (int z, std::int64_t j) {
  2940. const auto v = (j * (13 + z * 7)) % 4096 - 2048;
  2941. const auto w = ((j * j + z * 31) % 997) - 498;
  2942. return (std::int16_t) std::clamp<std::int64_t> (v * 6 + w * 8, -32768, 32767);
  2943. };
  2944. std::vector<RefNote> notes;
  2945. auto note = [&] (int start, int key, double vel, double fv, double fp, double cents) {
  2946. RefNote n; n.start = start; n.key = key; n.velocity = vel; n.finalVol = fv; n.finalPan = fp; n.cents = cents;
  2947. notes.push_back (n);
  2948. };
  2949. 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);
  2950. note (44100, 36, 0.3, 0.8, 0.8, -40); note (80000, 72, 0.9, 0.6, -0.6, 0);
  2951. notes[0].release = 40000; notes[1].release = 70000; notes[2].release = 60000;
  2952. auto run = [&] (const std::string& file, const std::string& channel, std::uint64_t expected) {
  2953. const auto st = parseState (stateOf (file, channel));
  2954. if (! st)
  2955. return;
  2956. const auto* prog = playedProgram (*st);
  2957. CHECK (prog != nullptr);
  2958. if (prog == nullptr)
  2959. return;
  2960. std::vector<std::shared_ptr<const ZoneSamples>> zs;
  2961. for (int z = 0; z < (int) prog->zones.size(); ++z)
  2962. {
  2963. auto s = std::make_shared<ZoneSamples>();
  2964. s->bits = 16;
  2965. s->channels = 1;
  2966. s->frames = prog->zones[(std::size_t) z].frames;
  2967. std::vector<std::int16_t> v ((std::size_t) s->frames + 512, 0);
  2968. for (std::int64_t j = 0; j < s->frames; ++j)
  2969. v[(std::size_t) j + 256] = sampleValue (z, j);
  2970. s->i16.push_back (std::move (v));
  2971. zs.push_back (s);
  2972. }
  2973. EngineOptions o;
  2974. o.velocityAsInitVol = (st->flags & 2) != 0;
  2975. o.noGainClamp = (st->flags & 1) != 0;
  2976. const auto out = renderReference (*prog, zs, o, notes, 44100.0, 120000, 512, 1);
  2977. std::uint64_t h = 14695981039346656037ull;
  2978. for (const float x : out)
  2979. {
  2980. std::uint32_t b;
  2981. std::memcpy (&b, &x, 4);
  2982. for (int k = 0; k < 4; ++k)
  2983. h = (h ^ ((b >> (8 * k)) & 0xffu)) * 1099511628211ull;
  2984. }
  2985. CHECK (h == expected);
  2986. if (h != expected)
  2987. std::printf (" DirectWave %s: %016llx\n", channel.c_str(), (unsigned long long) h);
  2988. };
  2989. run ("Demo projects/Visual/ZGameEditor Visualizer/Treben - Umziky - All the same.flp", "Gtr", 0xa1de3dd6be53d665ull);
  2990. run ("Demo projects/Visual/ZGameEditor Visualizer/Treben - Umziky - All the same.flp", "Rhodes", 0xcd458ec6a857f9c7ull);
  2991. run ("System/Render tests/Directwave.flp", "DW", 0x95b4c2e29b664dd1ull);
  2992. }
  2993. TEST (fl_keys_matches_fl_with_the_installation)
  2994. {
  2995. // FL Keys' own output (r69/keys/kcmp3.py: the installation's DLL, FL's x87 host pan law, blocks of 512 at
  2996. // 44.1 kHz): FNV-1a 64 of the interleaved float bits of 24 000 frames
  2997. if (realFlpDir.empty())
  2998. return;
  2999. using namespace lynx::flkeys;
  3000. const auto folder = std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "FL Keys" / "Wavtables";
  3001. if (! std::filesystem::exists (folder / "mda Piano.wti"))
  3002. return;
  3003. auto readAll = [] (const std::filesystem::path& p) {
  3004. std::ifstream f (p, std::ios::binary);
  3005. return std::vector<std::uint8_t> { std::istreambuf_iterator<char> (f), {} };
  3006. };
  3007. auto pcm = [] (const std::vector<std::uint8_t>& w) {
  3008. FileSamples f;
  3009. std::uint16_t channels = 1;
  3010. for (std::size_t pos = 12; pos + 8 <= w.size();)
  3011. {
  3012. std::uint32_t size = 0;
  3013. std::memcpy (&size, w.data() + pos + 4, 4);
  3014. if (std::memcmp (w.data() + pos, "fmt ", 4) == 0)
  3015. std::memcpy (&channels, w.data() + pos + 10, 2);
  3016. else if (std::memcmp (w.data() + pos, "data", 4) == 0)
  3017. {
  3018. f.samples.resize (std::min<std::size_t> (size, w.size() - pos - 8) / 2);
  3019. std::memcpy (f.samples.data(), w.data() + pos + 8, f.samples.size() * 2);
  3020. f.channels = channels;
  3021. }
  3022. pos += 8 + size + (size & 1);
  3023. }
  3024. return f;
  3025. };
  3026. auto run = [&] (const std::string& name, std::array<int, 15> knobs, std::uint64_t expected) {
  3027. const auto wti = readAll (folder / (name + ".wti"));
  3028. std::vector<std::string> names;
  3029. auto table = parseWti (std::string (wti.begin(), wti.end()), names);
  3030. CHECK (table.has_value());
  3031. if (! table)
  3032. return;
  3033. std::vector<FileSamples> files;
  3034. for (std::size_t i = 0; i < names.size(); ++i)
  3035. {
  3036. auto rel = names[i];
  3037. std::replace (rel.begin(), rel.end(), '\\', '/');
  3038. const auto bytes = readAll (folder / rel);
  3039. if (table->raw)
  3040. {
  3041. FileSamples f;
  3042. f.samples.resize (bytes.size() / 2);
  3043. std::memcpy (f.samples.data(), bytes.data(), f.samples.size() * 2);
  3044. files.push_back (std::move (f));
  3045. }
  3046. else
  3047. {
  3048. table->files[i] = wavFileInfo (bytes);
  3049. files.push_back (pcm (bytes));
  3050. }
  3051. }
  3052. const auto w = buildWavetable (*table, files);
  3053. CHECK (w != nullptr);
  3054. if (w == nullptr)
  3055. return;
  3056. State s;
  3057. s.wavetable = name;
  3058. s.knobs = knobs;
  3059. auto note = [] (int start, int key, double iv, double fv, double fp, double cents) {
  3060. RefNote n;
  3061. n.start = start, n.key = key, n.initVol = iv, n.finalVol = fv, n.finalPan = fp, n.cents = cents;
  3062. return n;
  3063. };
  3064. 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),
  3065. 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) };
  3066. // (floats as the harness parses them)
  3067. for (auto& n : notes)
  3068. n.initVol = (float) n.initVol, n.finalVol = (float) n.finalVol, n.finalPan = (float) n.finalPan;
  3069. notes[0].release = 9000, notes[2].release = 12000, notes[3].release = 15000, notes[5].release = 20000;
  3070. const auto out = renderReference (*w, s, notes, 44100.0, 24000, 512, true);
  3071. std::uint64_t h = 0xcbf29ce484222325ull;
  3072. for (auto v : out)
  3073. {
  3074. std::uint32_t b;
  3075. std::memcpy (&b, &v, 4);
  3076. for (int k = 0; k < 4; ++k)
  3077. h = (h ^ ((b >> (8 * k)) & 0xff)) * 0x100000001b3ull;
  3078. }
  3079. CHECK (h == expected);
  3080. };
  3081. run ("mda Piano", { 1000, 138, 388, -1000, 716, 0, -1000, 256, 0, 0, 0, 0, 0, 0, 0 }, 0x78268ec007f31309ull);
  3082. run ("Rhodes", { 200, 300, 500, 800, 300, 1500, 300, 1500, 300, 400, 400, 800, 900, 30000, 700 }, 0xcc91c93d917f1d31ull);
  3083. run ("Roto Organ", { 1000, 1000, 1000, 1000, 0, 0, -1000, 0, 1000, 0, 500, 1000, 1000, 0, 300 }, 0x99eb13e87640a1c6ull);
  3084. }
  3085. TEST (sampler_plays_pitched_one_shot)
  3086. {
  3087. // 1000-frame ramp sample: the position can be measured from the amplitude
  3088. auto data = std::make_shared<lynx::SampleData>();
  3089. data->sampleRate = 48000.0;
  3090. for (int i = 0; i < 1000; ++i)
  3091. data->left.push_back (1.0f);
  3092. auto sampler = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  3093. sampler->prepare (48000.0);
  3094. std::vector<float> l (2048, 0.0f), r (2048, 0.0f);
  3095. sampler->noteOn (60, 1.0f);
  3096. sampler->noteOff (60); // one-shot: noteOff doesn't cut
  3097. sampler->render (l.data(), r.data(), 2048, 1.0f, 1.0f);
  3098. int sounding = 0;
  3099. for (auto s : l)
  3100. sounding += s > 0.5f;
  3101. CHECK (sounding >= 995 && sounding <= 1000); // plays the whole sample at the original pitch
  3102. std::fill (l.begin(), l.end(), 0.0f);
  3103. sampler->noteOn (72, 1.0f); // one octave up: half the duration
  3104. sampler->render (l.data(), r.data(), 2048, 1.0f, 1.0f);
  3105. sounding = 0;
  3106. for (auto s : l)
  3107. sounding += s > 0.5f;
  3108. CHECK (sounding >= 495 && sounding <= 500);
  3109. CHECK (r[10] > 0.5f); // mono goes to both sides
  3110. auto silent = lynx::makeInstrument (lynx::InstrumentKind::sampler, nullptr);
  3111. silent->prepare (48000.0);
  3112. silent->noteOn (60, 1.0f);
  3113. std::fill (l.begin(), l.end(), 0.0f);
  3114. silent->render (l.data(), r.data(), 64, 1.0f, 1.0f);
  3115. CHECK (l[0] == 0.0f);
  3116. }
  3117. TEST (rack_live_notes_and_sample_provider)
  3118. {
  3119. lynx::Song song;
  3120. lynx::ChannelDef c;
  3121. c.name = "Smp";
  3122. c.instrument = lynx::InstrumentKind::sampler;
  3123. c.samplePath = "a.wav";
  3124. song.channels.push_back (c);
  3125. song.patterns.emplace_back();
  3126. int loads = 0;
  3127. auto provider = [&] (const std::string& path) -> std::shared_ptr<const lynx::SampleData> {
  3128. ++loads;
  3129. CHECK (path == "a.wav");
  3130. auto d = std::make_shared<lynx::SampleData>();
  3131. d->left.assign (4800, 0.5f);
  3132. return d;
  3133. };
  3134. auto state = lynx::RackState::build (song, nullptr, 48000.0, provider);
  3135. CHECK (loads == 1);
  3136. lynx::RackRenderer renderer;
  3137. renderer.prepare (48000.0);
  3138. lynx::LiveEventQueue live;
  3139. lynx::NoteEvent on;
  3140. on.channel = 0;
  3141. on.velocity = 1.0f;
  3142. live.push (on);
  3143. std::vector<float> l (256), r (256);
  3144. renderer.process (state.get(), false, 0.0, 256, l.data(), r.data(), &live); // stopped, but the live note plays
  3145. CHECK (l[0] > 0.1f);
  3146. // same sample: reused; different sample: reloaded
  3147. auto same = lynx::RackState::build (song, state.get(), 48000.0, provider);
  3148. CHECK (loads == 1 && same->instruments[0] == state->instruments[0]);
  3149. song.channels[0].samplePath = "b.wav";
  3150. auto other = lynx::RackState::build (song, state.get(), 48000.0, [&] (const std::string&) { ++loads; return nullptr; });
  3151. CHECK (loads == 2 && other->instruments[0] != state->instruments[0]);
  3152. }
  3153. TEST (flp_paths_resolve_and_samplers_import)
  3154. {
  3155. lynx::FlPathRoots roots { "C:\\FL", "C:\\FL\\Data", "D:\\User" };
  3156. CHECK (lynx::resolveFlPath ("%FLStudioFactoryData%\\Data\\x.wav", roots) == "C:\\FL\\Data\\x.wav");
  3157. CHECK (lynx::resolveFlPath ("%FLStudioData%\\Patches\\y.wav", roots) == "C:\\FL\\Data\\Patches\\y.wav");
  3158. CHECK (lynx::resolveFlPath ("%FLStudioUserData%\\z.wav", roots) == "D:\\User\\z.wav");
  3159. CHECK (lynx::resolveFlPath ("E:\\abs.wav", roots) == "E:\\abs.wav");
  3160. CHECK (lynx::resolveFlPath ("%FLStudioUserData%\\z.wav", {}) == "");
  3161. {
  3162. // POSIX roots (macOS, Linux, Wine prefix): backslashes become '/', letter case follows the disk (round 117)
  3163. namespace fs = std::filesystem;
  3164. const auto base = fs::temp_directory_path() / ("lynx-case-test-" + processTempTag());
  3165. fs::remove_all (base);
  3166. fs::create_directories (base / "Data" / "Patches" / "Packs");
  3167. { std::ofstream (base / "Data" / "Patches" / "Packs" / "Kick.WAV") << "x"; }
  3168. lynx::FlPathRoots posix;
  3169. posix.factoryData = base.string();
  3170. CHECK (lynx::resolveFlPath ("%FLStudioFactoryData%\\Data\\Patches\\Packs\\Kick.WAV", posix) == (base / "Data/Patches/Packs/Kick.WAV").string());
  3171. const auto lowerCase = lynx::resolveFlPath ("%FLStudioFactoryData%\\data\\patches\\packs\\kick.wav", posix);
  3172. if (fs::exists (base / "data")) // case-insensitive file system (macOS): any spelling opens the file
  3173. CHECK (fs::exists (lowerCase));
  3174. else
  3175. CHECK (lowerCase == (base / "Data/Patches/Packs/Kick.WAV").string());
  3176. CHECK (lynx::resolveFlPath ("%FLStudioFactoryData%\\Data\\missing.wav", posix) == (base / "Data/missing.wav").string());
  3177. fs::remove_all (base);
  3178. }
  3179. flp::Project p;
  3180. p.channels = { { 0, flp::ChannelType::sampler, "Kick", "", "%FLStudioData%\\k.wav", 1 },
  3181. { 1, flp::ChannelType::sampler, "Hat", "", "%FLStudioData%\\missing.wav", 1 } };
  3182. lynx::FlpImportOptions options;
  3183. options.roots = roots;
  3184. options.fileExists = [] (const std::string& path) { return path == "C:\\FL\\Data\\k.wav"; };
  3185. const auto s = lynx::importFlp (p, options);
  3186. CHECK (s.channels[0].instrument == lynx::InstrumentKind::sampler);
  3187. CHECK (s.channels[0].samplePath == "C:\\FL\\Data\\k.wav");
  3188. CHECK (s.channels[1].instrument == lynx::InstrumentKind::hat); // no file: built-in instrument
  3189. }
  3190. TEST (output_protection_is_transparent_up_to_full_scale)
  3191. {
  3192. // round 160: no more soft knee at 0.9 — FL leaves the mix untouched up to 0 dBFS
  3193. float s[] = { 0.5f, -0.89f, 1.5f, -3.0f, 0.95f, 0.999f, std::nanf (""), -1.0f };
  3194. lynx::clipOvers (s, 8);
  3195. CHECK (s[0] == 0.5f && s[1] == -0.89f && s[4] == 0.95f && s[5] == 0.999f && s[7] == -1.0f);
  3196. CHECK (s[2] == 1.0f && s[3] == -1.0f);
  3197. CHECK (s[6] == 0.0f);
  3198. }
  3199. TEST (song_scheduler_plays_clips_with_offset_and_cut)
  3200. {
  3201. lynx::Song s;
  3202. s.channels.resize (1);
  3203. auto& p = s.patterns.emplace_back();
  3204. for (int step : { 0, 4, 8, 12 }) // one note per beat
  3205. p.toggleStep (0, step);
  3206. // clip at bar 2 playing only the second half of the pattern (beats 3 and 4)
  3207. lynx::Clip c;
  3208. c.pattern = 0;
  3209. c.start = 4 * lynx::ppq;
  3210. c.length = 2 * lynx::ppq;
  3211. c.offset = 2 * lynx::ppq;
  3212. s.playlist.push_back (c);
  3213. CHECK (s.songLength() == 6 * lynx::ppq); // FL: the song ends where the last clip ends
  3214. const double sr = 48000.0;
  3215. const auto tps = lynx::ticksPerSample (120.0, sr); // 1 beat = 24000 samples
  3216. lynx::EventBuffer ev;
  3217. // whole song in a single block (8 beats = 192000 samples)
  3218. lynx::scheduleSong (s, 0.0, 192000, tps, ev);
  3219. std::vector<int> ons;
  3220. for (auto& e : ev)
  3221. if (e.type == lynx::NoteEvent::Type::on)
  3222. ons.push_back (e.sampleOffset);
  3223. CHECK (ons.size() == 2);
  3224. CHECK (ons.size() == 2 && ons[0] == 4 * 24000 && ons[1] == 5 * 24000);
  3225. // crossing the loop: bar 1 is empty, so nothing at the start of the second pass
  3226. ev.clear();
  3227. lynx::scheduleSong (s, 8.0 * lynx::ppq - 100 * tps, 200, tps, ev);
  3228. CHECK (ev.size() == 0);
  3229. // a note longer than the clip is cut at the clip end
  3230. p.notes[3].length = 4 * lynx::ppq;
  3231. ev.clear();
  3232. lynx::scheduleSong (s, 0.0, 192000, tps, ev);
  3233. int lastOff = -1;
  3234. for (auto& e : ev)
  3235. if (e.type == lynx::NoteEvent::Type::off)
  3236. lastOff = std::max (lastOff, e.sampleOffset);
  3237. CHECK (lastOff == 6 * 24000);
  3238. }
  3239. TEST (remove_pattern_fixes_clips)
  3240. {
  3241. lynx::Song s;
  3242. s.patterns.resize (3);
  3243. s.currentPattern = 2;
  3244. s.playlist = { { 0, 0, 0, 100, 0 }, { 1, 0, 100, 100, 0 }, { 2, 1, 0, 100, 0 } };
  3245. s.removePattern (1);
  3246. CHECK (s.patterns.size() == 2);
  3247. CHECK (s.playlist.size() == 2 && s.playlist[1].pattern == 1);
  3248. CHECK (s.currentPattern == 1);
  3249. }
  3250. TEST (flp_import_3x_osc_state_and_channel_filter)
  3251. {
  3252. // new 3x Osc patch (version 14): osc 2 mix 64 coarse -12, osc 3 mix 32 coarse -24
  3253. std::vector<std::uint8_t> blob (460, 0);
  3254. auto put = [&] (std::size_t off, std::int32_t v) {
  3255. for (int b = 0; b < 4; ++b)
  3256. blob[off + (std::size_t) b] = (std::uint8_t) ((std::uint32_t) v >> (8 * b));
  3257. };
  3258. put (0x00, 14);
  3259. put (0x08, 3); // osc 1 saw
  3260. put (0x10, 7); // osc 1 fine +7
  3261. put (0x04, -32); // osc 1 pan -0.5
  3262. put (0x1C, 64); // osc 2 mix
  3263. put (0x28, -12); // osc 2 coarse
  3264. put (0x38, 32); // osc 3 mix
  3265. put (0x40, 2); // osc 3 square
  3266. put (0x44, -19); // osc 3 coarse: -1 octave and -7 semitones
  3267. lynx::SynthSettings s;
  3268. CHECK (lynx::synthFrom3xOsc (blob, s));
  3269. 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);
  3270. CHECK (s.osc[0].wave == lynx::Waveform::saw && s.osc[0].detune == 7.0f && s.osc[0].pan == -0.5f);
  3271. CHECK (s.osc[1].wave == lynx::Waveform::sine && s.osc[1].octave == -1 && s.osc[1].semitones == 0);
  3272. CHECK (s.osc[2].wave == lynx::Waveform::square && s.osc[2].octave == -1 && s.osc[2].semitones == -7);
  3273. CHECK (! lynx::synthFrom3xOsc (std::vector<std::uint8_t> (40, 0), s));
  3274. // channel filter: open by default; LP with Mod X 128 ~ 2.71 kHz; Fast LP with Mod X 128 ~ 280 Hz
  3275. float hz = 0.0f, res = 1.0f;
  3276. int mode = -1;
  3277. lynx::synthFilterFromChannel (256, 0, 0, hz, res, mode);
  3278. CHECK (hz >= 19999.0f && res == 0.0f);
  3279. lynx::synthFilterFromChannel (128, 0, 1, hz, res, mode);
  3280. CHECK (std::abs (hz - 2710.0f) < 30.0f && res == 0.0f);
  3281. lynx::synthFilterFromChannel (128, 0, 0, hz, res, mode);
  3282. CHECK (hz > 240.0f && hz < 320.0f);
  3283. lynx::synthFilterFromChannel (128, 256, 1, hz, res, mode);
  3284. CHECK (res > 0.8f);
  3285. CHECK (mode == 0);
  3286. lynx::synthFilterFromChannel (64, 0, 3, hz, res, mode); // high-pass at ~840 Hz
  3287. CHECK (mode == 2 && std::abs (hz - 840.0f) < 40.0f);
  3288. lynx::synthFilterFromChannel (128, 0, 2, hz, res, mode); // band-pass
  3289. CHECK (mode == 1);
  3290. }
  3291. TEST (synth_pitch_and_filter_envelopes)
  3292. {
  3293. auto render = [] (const lynx::SynthSettings& s, int frames) {
  3294. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  3295. inst->prepare (48000.0);
  3296. lynx::ChannelDef c;
  3297. c.synth = s;
  3298. inst->configure (c, 120.0);
  3299. inst->noteOn (45, 1.0f); // A2 = 110 Hz
  3300. std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
  3301. inst->render (l.data(), r.data(), frames, 1.0f, 1.0f);
  3302. return l;
  3303. };
  3304. auto crossings = [] (const std::vector<float>& b, int from, int to) {
  3305. int n = 0;
  3306. for (int i = from + 1; i < to; ++i)
  3307. n += (b[(std::size_t) i - 1] < 0.0f) != (b[(std::size_t) i] < 0.0f);
  3308. return n;
  3309. };
  3310. // 3x Osc "kick": sine with a +24 st pitch envelope falling fast
  3311. lynx::SynthSettings kick;
  3312. kick.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  3313. kick.cutoff = 20000.0f;
  3314. kick.pitchEnv = { 24.0f, 0.0f, 0.0f, 0.05f, 0.0f, 0.05f };
  3315. const auto k = render (kick, 48000);
  3316. const auto early = crossings (k, 0, 2400); // first 50 ms: well above 110 Hz
  3317. const auto late = crossings (k, 24000, 48000); // then back to 110 Hz (~110 crossings in 0.5 s)
  3318. CHECK (early > 20); // +24 st ramp over 50 ms: ~2.2x the average frequency (11 crossings without envelope)
  3319. CHECK (std::abs (late - 110) < 4);
  3320. // cutoff envelope: saw closed at 200 Hz opens +5 octaves on attack and closes again
  3321. lynx::SynthSettings pluck;
  3322. pluck.cutoff = 200.0f;
  3323. pluck.filterEnv = { 5.0f, 0.0f, 0.0f, 0.1f, 0.0f, 0.1f };
  3324. const auto p = render (pluck, 48000);
  3325. auto hf = [&] (int from, int to) { // energy of the difference (highs)
  3326. double e = 0.0;
  3327. for (int i = from + 1; i < to; ++i)
  3328. e += std::pow ((double) p[(std::size_t) i] - p[(std::size_t) i - 1], 2.0);
  3329. return e;
  3330. };
  3331. CHECK (hf (0, 2400) > 4.0 * hf (40000, 42400));
  3332. // Toxic Biohazard's envelope (REA_rodada33 §2): exponential towards 5 clipped at 1, so the cutoff is still near
  3333. // its base early in the attack, fully open at A = 0.2 s and back down after the exponential decay
  3334. lynx::SynthSettings toxic = pluck;
  3335. toxic.filterEnv = { 5.0f, 0.2f, 0.0f, 0.1f, 0.0f, 0.1f, 1 };
  3336. const auto t = render (toxic, 48000);
  3337. auto thf = [&] (int from, int to) {
  3338. double e = 0.0;
  3339. for (int i = from + 1; i < to; ++i)
  3340. e += std::pow ((double) t[(std::size_t) i] - t[(std::size_t) i - 1], 2.0);
  3341. return e;
  3342. };
  3343. CHECK (thf (9000, 9600) > 4.0 * thf (300, 900));
  3344. CHECK (thf (9000, 9600) > 4.0 * thf (40000, 40600));
  3345. }
  3346. TEST (synth_mono_reuses_the_sounding_voice)
  3347. {
  3348. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  3349. inst->prepare (48000.0);
  3350. lynx::ChannelDef c;
  3351. c.synth.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  3352. c.synth.cutoff = 20000.0f;
  3353. c.synth.mono = true;
  3354. inst->configure (c, 120.0);
  3355. std::vector<float> l (48000), r (48000);
  3356. inst->noteOn (57, 1.0f);
  3357. inst->render (l.data(), r.data(), 4800, 1.0f, 1.0f);
  3358. inst->noteOn (69, 1.0f); // A4 takes over the A3 voice: only one note sounding
  3359. inst->render (l.data() + 4800, r.data() + 4800, 43200, 1.0f, 1.0f);
  3360. int crossings = 0;
  3361. float peak = 0.0f;
  3362. for (std::size_t i = 24001; i < 48000; ++i)
  3363. {
  3364. crossings += (l[i - 1] < 0.0f) != (l[i] < 0.0f);
  3365. peak = std::max (peak, std::abs (l[i]));
  3366. }
  3367. CHECK (std::abs (crossings - 440) < 4); // 0.5 s of 440 Hz, without the A3 underneath
  3368. CHECK (peak < 0.35f); // one voice (0.3 level), not two summed
  3369. // no click on the handover: the jump between neighbouring samples stays sine-sized
  3370. float jump = 0.0f;
  3371. for (std::size_t i = 4790; i < 4810; ++i)
  3372. jump = std::max (jump, std::abs (l[i] - l[i - 1]));
  3373. CHECK (jump < 0.05f);
  3374. }
  3375. TEST (synth_porta_glides_and_am_modulates)
  3376. {
  3377. auto make = [] (const lynx::SynthSettings& s) {
  3378. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  3379. inst->prepare (48000.0);
  3380. lynx::ChannelDef c;
  3381. c.synth = s;
  3382. inst->configure (c, 120.0);
  3383. return inst;
  3384. };
  3385. auto crossings = [] (const std::vector<float>& b, std::size_t from, std::size_t to) {
  3386. int n = 0;
  3387. for (auto i = from + 1; i < to; ++i)
  3388. n += (b[i - 1] < 0.0f) != (b[i] < 0.0f);
  3389. return n;
  3390. };
  3391. // mono + 1-beat porta (0.5 s at 120 BPM): from A3 to A4, halfway ~E4 (329.6 Hz)
  3392. lynx::SynthSettings s;
  3393. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  3394. s.cutoff = 20000.0f;
  3395. s.mono = true;
  3396. s.porta = true;
  3397. s.portaBeats = 1.0f;
  3398. auto inst = make (s);
  3399. std::vector<float> l (96000), r (96000);
  3400. inst->noteOn (57, 1.0f);
  3401. inst->render (l.data(), r.data(), 4800, 1.0f, 1.0f);
  3402. inst->noteOn (69, 1.0f);
  3403. inst->render (l.data() + 4800, r.data() + 4800, 91200, 1.0f, 1.0f);
  3404. const auto middle = crossings (l, 4800 + 12000 - 2400, 4800 + 12000 + 2400); // 0.1 s around 0.25 s
  3405. CHECK (std::abs (middle - 66) < 6); // ~330 Hz * 0.1 s * 2
  3406. CHECK (std::abs (crossings (l, 48000, 96000) - 880) < 4); // reached A4 (1 s = 880 crossings)
  3407. // AM: osc 3 at level 1.0, slow sine, modulates osc 1 (the average level drops, the sum doesn't gain osc 3)
  3408. lynx::SynthSettings am;
  3409. am.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  3410. am.osc[2] = { lynx::Waveform::sine, -3, 0.0f, 1.0f };
  3411. am.cutoff = 20000.0f;
  3412. am.am = true;
  3413. auto modulated = make (am);
  3414. modulated->noteOn (69, 1.0f);
  3415. std::fill (l.begin(), l.end(), 0.0f);
  3416. modulated->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
  3417. float peak = 0.0f;
  3418. for (std::size_t i = 4800; i < 48000; ++i)
  3419. peak = std::max (peak, std::abs (l[i]));
  3420. CHECK (peak > 0.45f && peak < 0.65f); // 0.3 · (1 + 1) at most: (1 − o3) goes up to 2
  3421. }
  3422. TEST (channel_lfo_vibrato_and_tremolo)
  3423. {
  3424. auto render = [] (const lynx::SynthSettings& s) {
  3425. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  3426. inst->prepare (48000.0);
  3427. lynx::ChannelDef c;
  3428. c.synth = s;
  3429. inst->configure (c, 120.0);
  3430. inst->noteOn (69, 1.0f);
  3431. std::vector<float> l (48000), r (48000);
  3432. inst->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
  3433. return l;
  3434. };
  3435. lynx::SynthSettings s;
  3436. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  3437. s.cutoff = 20000.0f;
  3438. // tremolo: 100% volume LFO at 4 Hz (0.25 s period) -> the amplitude envelope goes from ~0 to ~2x
  3439. auto tremolo = s;
  3440. tremolo.lfo[lynx::lfoVolume] = { 1.0f, 0.25f, false, 0, 0.0f, 0.0f };
  3441. const auto t = render (tremolo);
  3442. float lowest = 1.0f, highest = 0.0f;
  3443. for (std::size_t w = 4800; w + 480 <= 48000; w += 480) // 10 ms windows (4.4 cycles of 440 Hz)
  3444. {
  3445. float peak = 0.0f;
  3446. for (auto i = w; i < w + 480; ++i)
  3447. peak = std::max (peak, std::abs (t[i]));
  3448. lowest = std::min (lowest, peak);
  3449. highest = std::max (highest, peak);
  3450. }
  3451. CHECK (lowest < 0.08f && highest > 0.5f);
  3452. // synced vibrato: ±1 semitone, 1-beat period (0.5 s at 120 BPM); the average stays at 440 Hz
  3453. auto vibrato = s;
  3454. vibrato.lfo[lynx::lfoPitch] = { 1.0f, 1.0f, true, 0, 0.0f, 0.0f };
  3455. const auto v = render (vibrato);
  3456. int crossings = 0;
  3457. for (std::size_t i = 1; i < 48000; ++i)
  3458. crossings += (v[i - 1] < 0.0f) != (v[i] < 0.0f);
  3459. CHECK (std::abs (crossings - 880) < 12);
  3460. int firstQuarter = 0; // first quarter of the cycle: rising sine -> higher than 440
  3461. for (std::size_t i = 1; i < 6000; ++i)
  3462. firstQuarter += (v[i - 1] < 0.0f) != (v[i] < 0.0f);
  3463. CHECK (firstQuarter > 112);
  3464. }
  3465. TEST (sampler_porta_glides_from_previous_note)
  3466. {
  3467. auto data = std::make_shared<lynx::SampleData>();
  3468. data->sampleRate = 48000.0;
  3469. data->left.resize (96000);
  3470. for (std::size_t i = 0; i < data->left.size(); ++i)
  3471. data->left[i] = 0.5f * (float) std::sin (6.283185307 * 220.0 * (double) i / 48000.0);
  3472. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  3473. inst->prepare (48000.0);
  3474. lynx::ChannelDef c;
  3475. c.sampler.cut = true;
  3476. c.sampler.porta = true;
  3477. c.sampler.portaBeats = 1.0f; // 0.5 s at 120 BPM
  3478. inst->configure (c, 120.0);
  3479. std::vector<float> l (24000), r (24000);
  3480. inst->noteOn (60, 1.0f);
  3481. inst->render (l.data(), r.data(), 2400, 1.0f, 1.0f);
  3482. inst->noteOn (72, 1.0f); // one octave up: starts at 220 Hz and rises
  3483. std::fill (l.begin(), l.end(), 0.0f);
  3484. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  3485. auto crossings = [&] (std::size_t from, std::size_t to) {
  3486. int n = 0;
  3487. for (auto i = from + 1; i < to; ++i)
  3488. n += (l[i - 1] < 0.0f) != (l[i] < 0.0f);
  3489. return n;
  3490. };
  3491. const auto start = crossings (600, 2400); // 37.5 ms still near 220 Hz: ~17 crossings (440 Hz would give 33)
  3492. const auto end = crossings (21600, 24000); // 50 ms at the end of the glide: near 440 Hz (~44; 220 Hz would give 22)
  3493. CHECK (start < 22);
  3494. CHECK (end > 36);
  3495. }
  3496. TEST (zoned_sampler_picks_zone_by_key_and_velocity)
  3497. {
  3498. auto sine = [] (double hz, float amp) {
  3499. auto d = std::make_shared<lynx::SampleData>();
  3500. d->sampleRate = 48000.0;
  3501. d->left.resize (48000);
  3502. for (std::size_t i = 0; i < d->left.size(); ++i)
  3503. d->left[i] = amp * (float) std::sin (6.283185307 * hz * (double) i / 48000.0);
  3504. return d;
  3505. };
  3506. // lows: 220 Hz sine recorded as A3 (57), up to key 69; highs: 880 Hz as A5 (81)
  3507. // hard layer on the highs: same tuning, louder
  3508. std::vector<lynx::LoadedZone> zones {
  3509. { { "graves.wav", 57, 0, 69, 0, 127, 0.0f, 1.0f }, sine (220.0, 0.5f) },
  3510. { { "agudos.wav", 81, 70, 127, 0, 99, 0.0f, 1.0f }, sine (880.0, 0.25f) },
  3511. { { "agudos forte.wav", 81, 70, 127, 100, 127, 0.0f, 1.0f }, sine (880.0, 0.9f) },
  3512. };
  3513. auto inst = lynx::makeZonedSampler (zones);
  3514. inst->prepare (48000.0);
  3515. inst->configure (lynx::ChannelDef {}, 120.0);
  3516. auto play = [&] (int key, float velocity) {
  3517. inst->allNotesOff();
  3518. std::vector<float> l (24000), r (24000);
  3519. inst->render (l.data(), r.data(), 2000, 1.0f, 1.0f); // let the old voices die out
  3520. std::fill (l.begin(), l.end(), 0.0f);
  3521. inst->noteOn (key, velocity);
  3522. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  3523. int crossings = 0;
  3524. float peak = 0.0f;
  3525. for (std::size_t i = 1; i < 24000; ++i)
  3526. {
  3527. crossings += (l[i - 1] < 0.0f) != (l[i] < 0.0f);
  3528. peak = std::max (peak, std::abs (l[i]));
  3529. }
  3530. return std::pair { crossings, peak };
  3531. };
  3532. const auto low = play (69, 0.5f); // A4 from root A3: 440 Hz -> 440 crossings in 0.5 s
  3533. CHECK (std::abs (low.first - 440) < 4);
  3534. const auto soft = play (81, 0.5f); // high zone at the root: 880 Hz
  3535. CHECK (std::abs (soft.first - 880) < 6);
  3536. const auto hard = play (81, 0.95f); // high velocity: hard layer
  3537. CHECK (hard.second > soft.second * 2.0f);
  3538. // overlapping layers round-robin: two identical zones at different levels, notes alternate
  3539. auto rr = lynx::makeZonedSampler ({ { { "a.wav", 60 }, sine (440.0, 0.2f) }, { { "b.wav", 60 }, sine (440.0, 0.8f) } });
  3540. rr->prepare (48000.0);
  3541. rr->configure (lynx::ChannelDef {}, 120.0);
  3542. float peaks[2] {};
  3543. for (int k = 0; k < 2; ++k)
  3544. {
  3545. rr->allNotesOff();
  3546. std::vector<float> l (4800), r (4800);
  3547. rr->render (l.data(), r.data(), 2000, 1.0f, 1.0f);
  3548. std::fill (l.begin(), l.end(), 0.0f);
  3549. rr->noteOn (60, 1.0f);
  3550. rr->render (l.data(), r.data(), 4800, 1.0f, 1.0f);
  3551. for (auto v : l)
  3552. peaks[k] = std::max (peaks[k], std::abs (v));
  3553. }
  3554. CHECK (std::abs (peaks[0] - peaks[1]) > 0.3f);
  3555. }
  3556. TEST (zoned_sampler_loops_while_held)
  3557. {
  3558. // 0.1 s sample with a loop in the middle: holding the note it keeps sounding well past the end of the file;
  3559. // on release, it leaves the loop and ends
  3560. auto data = std::make_shared<lynx::SampleData>();
  3561. data->sampleRate = 48000.0;
  3562. data->left.resize (4800);
  3563. for (std::size_t i = 0; i < data->left.size(); ++i)
  3564. data->left[i] = 0.5f * (float) std::sin (6.283185307 * 480.0 * (double) i / 48000.0); // 100 frames per cycle
  3565. lynx::SampleZone zone { "loop.wav", 60 };
  3566. zone.loopStart = 1000;
  3567. zone.loopEnd = 3000;
  3568. zone.loopMode = 2;
  3569. auto inst = lynx::makeZonedSampler ({ { zone, data } });
  3570. inst->prepare (48000.0);
  3571. lynx::ChannelDef c;
  3572. c.sampler.release = 0.01f;
  3573. inst->configure (c, 120.0);
  3574. inst->noteOn (60, 1.0f);
  3575. std::vector<float> l (48000), r (48000);
  3576. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f); // 0.5 s held
  3577. float held = 0.0f;
  3578. for (std::size_t i = 20000; i < 24000; ++i)
  3579. held = std::max (held, std::abs (l[i]));
  3580. CHECK (held > 0.3f);
  3581. inst->noteOff (60);
  3582. inst->render (l.data() + 24000, r.data() + 24000, 24000, 1.0f, 1.0f);
  3583. float after = 0.0f;
  3584. for (std::size_t i = 40000; i < 48000; ++i)
  3585. after = std::max (after, std::abs (l[i]));
  3586. CHECK (after == 0.0f);
  3587. }
  3588. TEST (global_lfo_follows_song_position)
  3589. {
  3590. // global volume LFO (1-beat period): a note mid-cycle starts mid-wave; the
  3591. // same note with a per-note LFO starts at phase 0
  3592. auto first = [] (bool global, double songTicks) {
  3593. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  3594. inst->prepare (48000.0);
  3595. lynx::ChannelDef c;
  3596. c.synth.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  3597. c.synth.cutoff = 20000.0f;
  3598. c.synth.lfo[lynx::lfoVolume] = { 1.0f, 1.0f, true, 2, 0.0f, 0.0f, global }; // pulse: +1 in the first half
  3599. inst->configure (c, 120.0);
  3600. inst->setSongPosition (songTicks);
  3601. inst->noteOn (69, 1.0f);
  3602. std::vector<float> l (4800), r (4800);
  3603. inst->render (l.data(), r.data(), 4800, 1.0f, 1.0f);
  3604. float peak = 0.0f;
  3605. for (std::size_t i = 2400; i < 4800; ++i)
  3606. peak = std::max (peak, std::abs (l[i]));
  3607. return peak;
  3608. };
  3609. // mid-cycle (phase 0.6) the pulse is at −1: volume ~0; per note (phase 0) it is at +1: ~2x
  3610. const auto globalLate = first (true, 0.6 * lynx::ppq);
  3611. const auto perNote = first (false, 0.6 * lynx::ppq);
  3612. CHECK (globalLate < 0.05f);
  3613. CHECK (perNote > 0.4f);
  3614. }
  3615. TEST (sampler_channel_filter_darkens_and_bypasses_when_open)
  3616. {
  3617. // white noise in the sampler: with a 300 Hz cutoff the energy drops a lot; open passes unchanged
  3618. auto data = std::make_shared<lynx::SampleData>();
  3619. data->sampleRate = 48000.0;
  3620. data->left.resize (48000);
  3621. std::uint32_t seed = 7;
  3622. for (auto& v : data->left)
  3623. {
  3624. seed = seed * 1664525u + 1013904223u;
  3625. v = ((float) (seed >> 8) / 16777216.0f - 0.5f);
  3626. }
  3627. data->right = data->left;
  3628. auto energy = [&] (float cutoff) {
  3629. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  3630. inst->prepare (48000.0);
  3631. lynx::ChannelDef c;
  3632. c.sampler.cutoff = cutoff;
  3633. inst->configure (c, 120.0);
  3634. inst->noteOn (60, 1.0f);
  3635. std::vector<float> l (24000), r (24000);
  3636. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  3637. double e = 0.0;
  3638. for (auto v : l)
  3639. e += (double) v * v;
  3640. return e;
  3641. };
  3642. const auto open = energy (20000.0f), dark = energy (300.0f);
  3643. {
  3644. // high-pass at 10 kHz: also removes most of the noise energy, but from the other side
  3645. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  3646. inst->prepare (48000.0);
  3647. lynx::ChannelDef hp;
  3648. hp.sampler.cutoff = 10000.0f;
  3649. hp.sampler.filterType = 2;
  3650. inst->configure (hp, 120.0);
  3651. inst->noteOn (60, 1.0f);
  3652. std::vector<float> l (24000), r (24000);
  3653. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  3654. double e = 0.0, lowE = 0.0;
  3655. float lp = 0.0f;
  3656. for (auto v : l)
  3657. {
  3658. e += (double) v * v;
  3659. lp += 0.01f * (v - lp); // crude low-pass: almost nothing should remain below
  3660. lowE += (double) lp * lp;
  3661. }
  3662. CHECK (e > 10.0 && e < 0.7 * energy (20000.0f));
  3663. CHECK (lowE < e * 0.01);
  3664. }
  3665. CHECK (open > 100.0);
  3666. CHECK (dark < open * 0.05);
  3667. // +12 st pitch envelope with sustain 1: the sample runs at double speed
  3668. // and ends in half the time (1 s of sample -> 0.5 s)
  3669. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  3670. inst->prepare (48000.0);
  3671. lynx::ChannelDef c;
  3672. c.sampler.pitchEnv = { 12.0f, 0.0f, 0.0f, 0.1f, 1.0f, 0.1f };
  3673. inst->configure (c, 120.0);
  3674. inst->noteOn (60, 1.0f);
  3675. std::vector<float> l (48000), r (48000);
  3676. inst->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
  3677. float tail = 0.0f, body = 0.0f;
  3678. for (std::size_t i = 20000; i < 23000; ++i)
  3679. body = std::max (body, std::abs (l[i]));
  3680. for (std::size_t i = 25000; i < 48000; ++i)
  3681. tail = std::max (tail, std::abs (l[i]));
  3682. CHECK (body > 0.1f);
  3683. CHECK (tail == 0.0f);
  3684. }
  3685. TEST (synth_oscillator_pan_and_semitones)
  3686. {
  3687. auto render = [] (lynx::SynthSettings s, std::vector<float>& l, std::vector<float>& r) {
  3688. auto synth = lynx::makeInstrument (lynx::InstrumentKind::synth);
  3689. synth->prepare (48000.0);
  3690. lynx::ChannelDef c;
  3691. c.synth = s;
  3692. synth->configure (c, 120.0);
  3693. synth->noteOn (69, 1.0f);
  3694. l.assign (24000, 0.0f);
  3695. r.assign (24000, 0.0f);
  3696. synth->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  3697. };
  3698. lynx::SynthSettings s;
  3699. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f, 7, 1.0f }; // a fifth up, fully right
  3700. s.cutoff = 20000.0f;
  3701. std::vector<float> l, r;
  3702. render (s, l, r);
  3703. float peakL = 0.0f, peakR = 0.0f;
  3704. int crossings = 0;
  3705. for (std::size_t i = 4800; i < 24000; ++i)
  3706. {
  3707. peakL = std::max (peakL, std::abs (l[i]));
  3708. peakR = std::max (peakR, std::abs (r[i]));
  3709. crossings += (r[i - 1] < 0.0f) != (r[i] < 0.0f);
  3710. }
  3711. CHECK (peakL < 1e-4f && peakR > 0.1f);
  3712. // 440 * 2^(7/12) = 659.3 Hz; 0.4 s = 263.7 cycles = ~527 crossings
  3713. CHECK (std::abs (crossings - 527) < 6);
  3714. }
  3715. TEST (fl_wrapper_state_parses_records_and_vst3_state)
  3716. {
  3717. // Fruity Wrapper blob (version 12): VST3 generator type, class ID, name, path and state
  3718. std::vector<std::uint8_t> blob;
  3719. 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))); };
  3720. auto record = [&] (std::vector<std::uint8_t>& b, std::uint32_t id, const std::vector<std::uint8_t>& payload) {
  3721. u32 (b, id);
  3722. u32 (b, (std::uint32_t) payload.size());
  3723. u32 (b, 0);
  3724. b.insert (b.end(), payload.begin(), payload.end());
  3725. };
  3726. auto bytes = [] (const std::string& s) { return std::vector<std::uint8_t> (s.begin(), s.end()); };
  3727. std::vector<std::uint8_t> vst3State;
  3728. u32 (vst3State, 1);
  3729. record (vst3State, 1, std::vector<std::uint8_t> (64, 0));
  3730. record (vst3State, 3, bytes ("COMPONENTE"));
  3731. record (vst3State, 2, bytes ("CONTROLADOR"));
  3732. // record 4: 3 slots → ParamIDs 700, −1 (empty), 0xFFFFFF01
  3733. std::vector<std::uint8_t> slots;
  3734. for (auto v : { 3u, 700u, 0xFFFFFFFFu, 0xFFFFFF01u })
  3735. u32 (slots, v);
  3736. record (vst3State, 4, slots);
  3737. u32 (blob, 12);
  3738. record (blob, 0x01, std::vector<std::uint8_t> (20, 0xFF));
  3739. record (blob, 0x32, { 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });
  3740. std::vector<std::uint8_t> cid;
  3741. for (int i = 0; i < 16; ++i)
  3742. cid.push_back ((std::uint8_t) (0xA0 + i));
  3743. record (blob, 0x34, cid);
  3744. record (blob, 0x36, bytes ("Surge XT"));
  3745. record (blob, 0x37, bytes ("C:/Program Files/Common Files/VST3/Surge XT.vst3"));
  3746. record (blob, 0x35, vst3State);
  3747. lynx::ForeignPlugin p;
  3748. CHECK (lynx::parseFlWrapper (blob, p));
  3749. CHECK (p.format == lynx::ForeignPlugin::Format::vst3);
  3750. CHECK (p.hasClassId && p.classId[0] == 0xA0 && p.classId[15] == 0xAF);
  3751. CHECK (p.name == "Surge XT" && p.path.find ("Surge XT.vst3") != std::string::npos);
  3752. std::vector<std::uint8_t> component, controller;
  3753. CHECK (lynx::splitFlVst3State (p.state, component, controller));
  3754. CHECK (component == bytes ("COMPONENTE") && controller == bytes ("CONTROLADOR"));
  3755. const auto saved = lynx::flVst3ParamSlots (p.state);
  3756. CHECK (saved == (std::vector<std::int32_t> { 700, -1, (std::int32_t) 0xFFFFFF01u }));
  3757. // FL's slot assignment: saved slots keep their ParamID wherever it now sits; the empty slot stays empty;
  3758. // a vanished ParamID (0xFFFFFF01) leaves its slot dead; parameters without a slot get slots 3, 4… in list order
  3759. const auto positions = lynx::flVst3SlotPositions (saved, { 5u, 700u, 9u });
  3760. CHECK (positions == (std::vector<int> { 1, -1, -1, 0, 2 }));
  3761. CHECK (lynx::flVst3SlotPositions ({}, { 5u, 6u }) == (std::vector<int> { 0, 1 })); // new instance: slot = position
  3762. // VST2 state (REA_rodada89 §2): chunk and raw-parameter layouts, current (−9) and older versions
  3763. {
  3764. lynx::FlVst2State st;
  3765. st.flags = 8;
  3766. st.currentProgram = 3;
  3767. st.chunk = bytes ("BANKCHUNK");
  3768. const auto withChunk = lynx::writeFlVst2State (st);
  3769. lynx::FlVst2State back;
  3770. CHECK (lynx::parseFlVst2State (withChunk, back));
  3771. CHECK (back.version == -9 && back.flags == 8 && back.currentProgram == 3 && back.chunk == bytes ("BANKCHUNK") && back.params.empty());
  3772. CHECK (! back.chunkIsProgram());
  3773. lynx::FlVst2State raw;
  3774. raw.flags = 1;
  3775. raw.params = { 0.25f, 0.5f, 1.0f };
  3776. raw.programNames = { "Init" };
  3777. const auto withParams = lynx::writeFlVst2State (raw);
  3778. CHECK (withParams.size() == 4 + 1 + 4 + 8 + 4 + 4 + 12 + 4 + 25);
  3779. CHECK (lynx::parseFlVst2State (withParams, back));
  3780. CHECK (back.chunk.empty() && back.params == raw.params && back.allPrograms() && back.programNames == std::vector<std::string> { "Init" });
  3781. // version −5: no flags, i32 size, then i32 program; −6 without "all programs": the chunk is one program
  3782. std::vector<std::uint8_t> v5;
  3783. for (auto v : { 0xFFFFFFFBu, 4u, 7u })
  3784. u32 (v5, v);
  3785. for (auto c : bytes ("ABCD"))
  3786. v5.push_back (c);
  3787. CHECK (lynx::parseFlVst2State (v5, back) && back.version == -5 && back.currentProgram == 7 && back.chunk == bytes ("ABCD"));
  3788. std::vector<std::uint8_t> v6;
  3789. u32 (v6, 0xFFFFFFFAu);
  3790. v6.push_back (0);
  3791. for (auto v : { 2u, 0u })
  3792. u32 (v6, v);
  3793. v6.push_back ('x');
  3794. v6.push_back ('y');
  3795. CHECK (lynx::parseFlVst2State (v6, back) && back.chunkIsProgram() && back.chunk == bytes ("xy"));
  3796. // nothing to restore (123456), unknown old layouts, truncated chunks
  3797. std::vector<std::uint8_t> marker;
  3798. u32 (marker, 123456u);
  3799. CHECK (! lynx::parseFlVst2State (marker, back));
  3800. auto cut = withChunk;
  3801. cut.pop_back();
  3802. CHECK (! lynx::parseFlVst2State (cut, back));
  3803. CHECK (! lynx::parseFlVst2State ({}, back));
  3804. }
  3805. // version out of range or no type: not a usable wrapper
  3806. blob[0] = 13;
  3807. CHECK (! lynx::parseFlWrapper (blob, p));
  3808. CHECK (! lynx::parseFlWrapper ({ 12, 0, 0, 0 }, p));
  3809. }
  3810. TEST (schedule_index_matches_full_scan)
  3811. {
  3812. // random song (long notes, swing, clips with offset); indexed scheduling must yield
  3813. // exactly the same events as the full scan, including in the block that wraps around
  3814. lynx::Song s = lynx::Song::makeDefault();
  3815. s.swing = 0.6f;
  3816. std::uint32_t seed = 12345;
  3817. auto rnd = [&] (int n) { seed = seed * 1664525u + 1013904223u; return (int) ((seed >> 8) % (std::uint32_t) n); };
  3818. s.patterns.resize (3);
  3819. for (auto& p : s.patterns)
  3820. {
  3821. p.notes.clear();
  3822. for (int i = 0; i < 60; ++i)
  3823. {
  3824. lynx::Note n;
  3825. n.start = rnd (16 * lynx::ppq);
  3826. n.length = rnd (5) == 0 ? rnd (12 * lynx::ppq) + 1 : rnd (lynx::ppq) + 1;
  3827. n.channel = rnd (4);
  3828. n.key = 40 + rnd (40);
  3829. n.slide = rnd (20) == 0;
  3830. p.notes.push_back (n);
  3831. }
  3832. p.sortNotes();
  3833. }
  3834. s.playlist.clear();
  3835. for (int i = 0; i < 12; ++i)
  3836. {
  3837. lynx::Clip c;
  3838. c.pattern = rnd (3);
  3839. c.track = rnd (4);
  3840. c.start = rnd (40) * lynx::ppq;
  3841. c.length = (rnd (12) + 1) * lynx::ppq;
  3842. c.offset = rnd (4) * lynx::ppq;
  3843. s.playlist.push_back (c);
  3844. }
  3845. s.mode = lynx::PlayMode::song;
  3846. const auto index = lynx::makeScheduleIndex (s);
  3847. const auto tps = lynx::ticksPerSample (140.0, 48000.0);
  3848. const auto loop = (double) s.songLength();
  3849. int mismatches = 0, events = 0;
  3850. for (int block = 0; block < 400; ++block)
  3851. {
  3852. const auto start = block == 0 ? loop - 100.0 * tps : (double) rnd ((int) loop * 2);
  3853. const auto samples = 64 + rnd (24000); // up to ~1 beat per block
  3854. lynx::EventBuffer a, b;
  3855. lynx::scheduleSong (s, start, samples, tps, a, false, nullptr);
  3856. lynx::scheduleSong (s, start, samples, tps, b, false, &index);
  3857. events += a.size();
  3858. if (a.size() != b.size())
  3859. {
  3860. ++mismatches;
  3861. continue;
  3862. }
  3863. for (int i = 0; i < a.size(); ++i)
  3864. {
  3865. const auto& x = *(a.begin() + i);
  3866. const auto& y = *(b.begin() + i);
  3867. mismatches += x.sampleOffset != y.sampleOffset || x.type != y.type || x.key != y.key || x.channel != y.channel;
  3868. }
  3869. }
  3870. CHECK (events > 500);
  3871. CHECK (mismatches == 0);
  3872. }
  3873. TEST (mixer_track_eq_defaults_and_boost)
  3874. {
  3875. // FL defaults: 90 Hz, 1.5 kHz and 8 kHz; peak Q 0.632; shelf slope 0.607
  3876. lynx::TrackEq eq;
  3877. CHECK (std::abs (lynx::TrackEq::peakQ (eq.width[1]) - 0.632) < 0.01);
  3878. CHECK (std::abs (lynx::TrackEq::shelfSlope (eq.width[0]) - 0.607) < 0.01);
  3879. CHECK (! eq.active());
  3880. // +12 dB peak at 1.5 kHz on a track: a 1.5 kHz sine rises ~4x
  3881. lynx::Song song = lynx::Song::makeDefault();
  3882. song.mixer[1].eq.gainDb[1] = 12.0f;
  3883. for (auto& c : song.channels)
  3884. c.mixerTrack = 1;
  3885. song.channels[4].instrument = lynx::InstrumentKind::synth;
  3886. song.channels[4].synth = {};
  3887. song.channels[4].synth.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  3888. song.channels[4].synth.cutoff = 20000.0f;
  3889. auto render = [] (const lynx::Song& s) {
  3890. auto state = lynx::RackState::build (s, nullptr, 48000.0);
  3891. lynx::RackRenderer renderer;
  3892. renderer.prepare (48000.0);
  3893. lynx::LiveEventQueue live;
  3894. lynx::NoteEvent on; // 1.5 kHz ~ key 90.6: use 91 (1568 Hz), still within the wide peak
  3895. on.channel = 4;
  3896. on.key = 91;
  3897. on.velocity = 1.0f;
  3898. live.push (on);
  3899. std::vector<float> l (24000), r (24000);
  3900. for (int pos = 0; pos < 24000; pos += 480)
  3901. renderer.process (state.get(), false, 0.0, 480, l.data() + pos, r.data() + pos, pos == 0 ? &live : nullptr);
  3902. float peak = 0.0f;
  3903. for (std::size_t i = 12000; i < 24000; ++i)
  3904. peak = std::max (peak, std::abs (l[i]));
  3905. return peak;
  3906. };
  3907. const auto boosted = render (song);
  3908. song.mixer[1].eq.gainDb[1] = 0.0f;
  3909. const auto flat = render (song);
  3910. CHECK (flat > 0.01f);
  3911. CHECK (boosted / flat > 3.0f && boosted / flat < 4.2f);
  3912. }
  3913. TEST (bars_follow_time_signature_markers)
  3914. {
  3915. lynx::Song song;
  3916. song.beatsPerBar = 4;
  3917. const auto bar4 = (std::int64_t) lynx::ppq * 4;
  3918. auto bars = song.barsBetween (0, bar4 * 3);
  3919. CHECK (bars.size() == 3 && bars[2].tick == bar4 * 2 && bars[2].number == 3);
  3920. // 7/4 from bar 3, and 3/4 in the middle of a 7 bar (cuts and restarts)
  3921. song.addMarker ({ bar4 * 2, "Verso", 7, 4 });
  3922. song.addMarker ({ bar4, "Nome só", 0, 4 });
  3923. const auto seven = (std::int64_t) lynx::ppq * 7;
  3924. song.addMarker ({ bar4 * 2 + seven + lynx::ppq * 2, "", 3, 4 });
  3925. bars = song.barsBetween (0, bar4 * 2 + seven * 2);
  3926. CHECK (bars.size() >= 5);
  3927. CHECK (bars[1].length == bar4 && bars[2].tick == bar4 * 2 && bars[2].length == seven);
  3928. CHECK (bars[3].length == lynx::ppq * 2); // cut short by the change
  3929. CHECK (bars[4].length == lynx::ppq * 3 && bars[4].number == 5);
  3930. CHECK (song.markers()[0].name == "Nome só"); // sorted by tick
  3931. // from far away: starts at the right bar without losing the numbering
  3932. const auto far = song.barsBetween (lynx::ppq * 3000, lynx::ppq * 3001);
  3933. CHECK (far.size() == 1 && far[0].length == lynx::ppq * 3);
  3934. CHECK (song.barAt (lynx::ppq * 3000).number == far[0].number);
  3935. CHECK ((lynx::ppq * 3000 - far[0].tick) < far[0].length);
  3936. }
  3937. TEST (split_drum_channel_moves_notes_to_pads)
  3938. {
  3939. lynx::Song song = lynx::Song::makeDefault();
  3940. const auto channels = (int) song.channels.size();
  3941. song.patterns[0].notes.clear();
  3942. song.patterns[0].notes.push_back ({ 0, 240, 1, 36, 0.9f }); // kit kick (channel 1)
  3943. song.patterns[0].notes.push_back ({ 240, 240, 1, 38, 0.8f }); // snare
  3944. song.patterns[0].notes.push_back ({ 480, 240, 1, 50, 0.8f }); // key without a pad: dropped
  3945. song.patterns[0].notes.push_back ({ 480, 240, 3, 60, 0.8f }); // other channel: shifts by 1 position
  3946. lynx::Automation a;
  3947. a.target = { lynx::AutomationTargetKind::channelVolume, 3, 0, 0 };
  3948. song.automations.push_back (a);
  3949. std::vector<lynx::DrumPad> pads (2);
  3950. pads[0].name = "Kick";
  3951. pads[0].key = 36;
  3952. pads[0].layers = { { "kick.wav" } };
  3953. pads[1].name = "Snare";
  3954. pads[1].key = 38;
  3955. pads[1].layers = { { "snare soft.wav", 60, 0, 127, 0, 90 }, { "snare hard.wav", 60, 0, 127, 91, 127 } };
  3956. pads[1].cutGroup = 2;
  3957. pads[1].cutBy = 2;
  3958. lynx::splitDrumChannel (song, 1, pads);
  3959. CHECK ((int) song.channels.size() == channels + 1);
  3960. CHECK (song.channels[1].samplePath == "kick.wav" && song.channels[1].instrument == lynx::InstrumentKind::sampler);
  3961. CHECK (song.channels[2].zones.size() == 2 && song.channels[2].cutGroup == song.channels[2].cutBy && song.channels[2].cutGroup != 0);
  3962. const auto& notes = song.patterns[0].notes;
  3963. CHECK (notes.size() == 3);
  3964. CHECK (notes[0].channel == 1 && notes[0].key == 60);
  3965. CHECK (notes[1].channel == 2 && notes[1].key == 60);
  3966. CHECK (notes[2].channel == 4); // the old channel 3
  3967. CHECK (song.automations[0].target.index == 4);
  3968. }
  3969. TEST (flp_import_brings_playlist)
  3970. {
  3971. flp::Project p;
  3972. p.header.ppq = 96;
  3973. p.channels = { { 0, flp::ChannelType::sampler, "Kick", "", "", 1 } };
  3974. flp::Pattern empty;
  3975. empty.index = 1;
  3976. flp::Pattern full;
  3977. full.index = 2;
  3978. flp::Note n;
  3979. n.channel = 0;
  3980. full.notes.push_back (n);
  3981. p.patterns = { empty, full };
  3982. flp::Arrangement a;
  3983. flp::PlaylistItem item;
  3984. item.kind = flp::PlaylistItem::Kind::pattern;
  3985. item.index = 2;
  3986. item.position = 384;
  3987. item.length = 192;
  3988. item.track = 3;
  3989. item.patternStartTick = 96;
  3990. a.items.push_back (item);
  3991. item.index = 1; // empty pattern: discarded
  3992. a.items.push_back (item);
  3993. item.kind = flp::PlaylistItem::Kind::channelClip; // audio: not supported yet
  3994. a.items.push_back (item);
  3995. p.arrangements.push_back (a);
  3996. const auto s = lynx::importFlp (p);
  3997. CHECK (s.mode == lynx::PlayMode::song);
  3998. CHECK (s.playlist.size() == 1);
  3999. CHECK (s.playlist[0].pattern == 0 && s.playlist[0].track == 3);
  4000. CHECK (s.playlist[0].start == 4 * lynx::ppq && s.playlist[0].length == 2 * lynx::ppq && s.playlist[0].offset == lynx::ppq);
  4001. }
  4002. TEST (rack_releases_notes_on_position_jump)
  4003. {
  4004. lynx::Song s;
  4005. lynx::ChannelDef c;
  4006. c.instrument = lynx::InstrumentKind::synth;
  4007. s.channels.push_back (c);
  4008. auto& p = s.patterns.emplace_back();
  4009. lynx::Note n;
  4010. n.start = 0;
  4011. n.length = 4 * lynx::ppq; // long note
  4012. p.notes.push_back (n);
  4013. s.bpm = 120.0;
  4014. auto state = lynx::RackState::build (s, nullptr, 48000.0);
  4015. lynx::RackRenderer r;
  4016. r.prepare (48000.0);
  4017. std::vector<float> l (512), rr (512);
  4018. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  4019. double tick = 0.0;
  4020. for (int i = 0; i < 20; ++i, tick += 512 * tps)
  4021. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  4022. // jump to tick 2000 (mid-note, no new "on"): the synth must go silent
  4023. for (int i = 0; i < 40; ++i)
  4024. r.process (state.get(), true, 2000.0 + i * 512 * tps, 512, l.data(), rr.data());
  4025. float tail = 0.0f;
  4026. for (auto v : l)
  4027. tail = std::max (tail, std::abs (v));
  4028. CHECK (tail < 0.001f);
  4029. }
  4030. namespace
  4031. {
  4032. // Two samplers with a constant 0.5 signal on different inserts; returns the output peak.
  4033. float juce_db (float gain)
  4034. {
  4035. return 20.0f * std::log10 (gain);
  4036. }
  4037. float renderMixerCase (const std::function<void (lynx::Song&)>& tweak, lynx::MeterBank* meters = nullptr)
  4038. {
  4039. lynx::Song s;
  4040. s.mixer = lynx::Song::defaultMixer (4);
  4041. for (int i = 0; i < 2; ++i)
  4042. {
  4043. lynx::ChannelDef c;
  4044. c.instrument = lynx::InstrumentKind::sampler;
  4045. c.samplePath = "dc";
  4046. c.volume = 1.0f;
  4047. c.mixerTrack = i + 1;
  4048. s.channels.push_back (c);
  4049. }
  4050. s.patterns.emplace_back();
  4051. tweak (s);
  4052. auto provider = [] (const std::string&) {
  4053. auto d = std::make_shared<lynx::SampleData>();
  4054. d->left.assign (48000, 0.5f);
  4055. return std::shared_ptr<const lynx::SampleData> (d);
  4056. };
  4057. auto state = lynx::RackState::build (s, nullptr, 48000.0, provider);
  4058. lynx::RackRenderer r;
  4059. r.prepare (48000.0, 256);
  4060. lynx::LiveEventQueue live;
  4061. for (int ch = 0; ch < 2; ++ch)
  4062. {
  4063. lynx::NoteEvent on;
  4064. on.channel = ch;
  4065. on.velocity = 1.0f;
  4066. live.push (on);
  4067. }
  4068. std::vector<float> l (1000), rr (1000); // larger than the mixer block: tests the chunking
  4069. r.process (state.get(), false, 0.0, 1000, l.data(), rr.data(), &live, meters);
  4070. return std::max (std::abs (l[999]), std::abs (rr[999]));
  4071. }
  4072. } // namespace
  4073. TEST (mixer_routes_mutes_solos_and_meters)
  4074. {
  4075. // two 0.5 channels summed on the Master at 0 dB
  4076. CHECK (std::abs (renderMixerCase ([] (lynx::Song&) {}) - 1.0f) < 1e-4f);
  4077. // insert 1 muted: only channel 2 remains
  4078. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.mixer[1].muted = true; }) - 0.5f) < 1e-4f);
  4079. // solo on insert 2: same
  4080. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.mixer[2].solo = true; }) - 0.5f) < 1e-4f);
  4081. // FL fader curve: 0.8 = 0 dB, max ≈ +5.6 dB, half travel ≈ -12.7 dB
  4082. CHECK (std::abs (lynx::faderGain (0.8f) - 1.0f) < 1e-5f);
  4083. CHECK (std::abs (juce_db (lynx::faderGain (1.0f)) - 5.59f) < 0.05f);
  4084. CHECK (lynx::faderGain (0.0f) == 0.0f);
  4085. // Master fader at max
  4086. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.mixer[0].volume = 1.0f; }) - lynx::faderGain (1.0f)) < 1e-4f);
  4087. // both channels on the same insert, insert fader at 0.4
  4088. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) {
  4089. s.channels[1].mixerTrack = 1;
  4090. s.mixer[1].volume = 0.4f;
  4091. }) - lynx::faderGain (0.4f)) < 1e-4f);
  4092. // a route outside the mixer falls to the last existing insert, without crashing
  4093. CHECK (renderMixerCase ([] (lynx::Song& s) { s.channels[0].mixerTrack = 100; }) > 0.9f);
  4094. // no mixer: everything straight to the output at 0 dB
  4095. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.mixer.clear(); }) - 1.0f) < 1e-4f);
  4096. lynx::MeterBank meters;
  4097. renderMixerCase ([] (lynx::Song&) {}, &meters);
  4098. // (the 24-point sinc's ringing at the sample start: its x87 kernel table rounds differently on ARM64)
  4099. CHECK (std::abs (meters.takePeak (1, 0) - 0.5f) < armTolerance (1e-4f, 2e-4f));
  4100. CHECK (std::abs (meters.takePeak (0, 1) - 1.0f) < armTolerance (1e-4f, 4e-4f));
  4101. CHECK (meters.takePeak (1, 0) == 0.0f); // reading resets
  4102. CHECK (meters.takePeak (3, 0) == 0.0f); // insert with no channel
  4103. }
  4104. TEST (next_free_insert_and_flp_mixer_import)
  4105. {
  4106. auto s = lynx::Song::makeDefault();
  4107. CHECK (s.mixer.size() == 17 && s.mixer[0].name == "Master");
  4108. CHECK (s.nextFreeInsert() == 6); // the 5 default channels take 1..5
  4109. flp::Project p;
  4110. p.channels = { { 0, flp::ChannelType::sampler, "Kick", "", "", 30 } };
  4111. p.mixer = { { 0, "" }, { 30, "Drums" } };
  4112. const auto song = lynx::importFlp (p);
  4113. CHECK (song.channels[0].mixerTrack == 30);
  4114. CHECK (song.mixer.size() == 31);
  4115. CHECK (song.mixer[30].name == "Drums" && song.mixer[0].name == "Master");
  4116. }
  4117. namespace
  4118. {
  4119. float runEffectOnSine (lynx::EffectKind kind, const std::function<void (lynx::EffectSlot&)>& tweak, double hz, float amp,
  4120. int frames = 48000)
  4121. {
  4122. auto fx = lynx::makeEffect (kind);
  4123. fx->prepare (48000.0);
  4124. auto slot = lynx::makeEffectSlot (kind);
  4125. tweak (slot);
  4126. std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
  4127. for (int i = 0; i < frames; ++i)
  4128. l[(std::size_t) i] = r[(std::size_t) i] = amp * (float) std::sin (6.283185307 * hz * i / 48000.0);
  4129. for (int i = 0; i < frames; i += 480)
  4130. fx->process (l.data() + i, r.data() + i, std::min (480, frames - i), slot.params, 120.0);
  4131. float peak = 0.0f;
  4132. for (int i = frames / 2; i < frames; ++i) // second half: already settled
  4133. {
  4134. CHECK (std::isfinite (l[(std::size_t) i]));
  4135. peak = std::max (peak, std::abs (l[(std::size_t) i]));
  4136. }
  4137. return peak;
  4138. }
  4139. } // namespace
  4140. TEST (parametric_eq_bands_and_response)
  4141. {
  4142. // default: transparent
  4143. auto slot = lynx::makeEffectSlot (lynx::EffectKind::parametricEq);
  4144. CHECK (std::abs (lynx::peq::responseDb (slot.params, 48000.0, 1000.0)) < 0.01);
  4145. CHECK (std::abs (runEffectOnSine (lynx::EffectKind::parametricEq, [] (auto&) {}, 1000.0, 0.5f) - 0.5f) < 0.005f);
  4146. // +12 dB peak at 1 kHz on band 4: the response and the signal agree
  4147. auto boost = [] (lynx::EffectSlot& s) {
  4148. s.params[(std::size_t) lynx::peq::param (3, 1)] = lynx::peq::hzValue (1000.0);
  4149. s.params[(std::size_t) lynx::peq::param (3, 2)] = lynx::peq::gainValue (12.0);
  4150. };
  4151. boost (slot);
  4152. CHECK (std::abs (lynx::peq::responseDb (slot.params, 48000.0, 1000.0) - 12.0) < 0.2);
  4153. CHECK (std::abs (lynx::peq::responseDb (slot.params, 48000.0, 100.0)) < 1.0);
  4154. const auto peak = runEffectOnSine (lynx::EffectKind::parametricEq, boost, 1000.0, 0.1f);
  4155. CHECK (std::abs (peak - 0.1f * std::pow (10.0f, 12.0f / 20.0f)) < 0.01f);
  4156. // low cut at 48 dB/oct at 200 Hz: 50 Hz (2 octaves below) drops ~96 dB; 2 kHz passes
  4157. auto cut = lynx::makeEffectSlot (lynx::EffectKind::parametricEq);
  4158. cut.params[(std::size_t) lynx::peq::param (0, 0)] = lynx::peq::typeValue (lynx::peq::BandType::lowCut);
  4159. cut.params[(std::size_t) lynx::peq::param (0, 1)] = lynx::peq::hzValue (200.0);
  4160. cut.params[(std::size_t) lynx::peq::param (0, 3)] = lynx::peq::qValue (0.70710678); // Butterworth
  4161. cut.params[(std::size_t) (lynx::peq::slopeParam + 0)] = 1.0f;
  4162. CHECK (lynx::peq::responseDb (cut.params, 48000.0, 50.0) < -80.0);
  4163. CHECK (std::abs (lynx::peq::responseDb (cut.params, 48000.0, 200.0) + 3.0) < 0.5); // -3 dB at the cutoff (Butterworth)
  4164. CHECK (std::abs (lynx::peq::responseDb (cut.params, 48000.0, 2000.0)) < 0.1);
  4165. // round-trip conversions
  4166. CHECK (std::abs (lynx::peq::hz (lynx::peq::hzValue (440.0)) - 440.0) < 0.5);
  4167. CHECK (std::abs (lynx::peq::q (lynx::peq::qValue (2.5)) - 2.5) < 0.01);
  4168. CHECK (lynx::peq::type (lynx::peq::typeValue (lynx::peq::BandType::notch)) == lynx::peq::BandType::notch);
  4169. }
  4170. TEST (fl_parametric_eq2_state_maps_to_bands)
  4171. {
  4172. // v8 blob: band 1 low cut at 100 Hz "Steep 8", band 4 peak +6 dB at 1 kHz, output −3 dB
  4173. std::vector<std::uint8_t> blob (354, 0);
  4174. auto put = [&] (std::size_t off, std::int32_t v) { std::memcpy (blob.data() + off, &v, 4); };
  4175. put (0, 8);
  4176. auto param = [&] (int i, std::int32_t v) { put (4 + 4 * (std::size_t) i, v); };
  4177. auto hzRaw = [] (double hz) { return (std::int32_t) std::lround (std::log (hz / 20.0) / std::log (1000.0) * 65536.0); };
  4178. for (int b = 0; b < 7; ++b)
  4179. param (21 + b, 0); // all off
  4180. param (21 + 0, 3); // high pass (= low cut)
  4181. param (7 + 0, hzRaw (100.0));
  4182. param (14 + 0, 40124); // default width -> Q ~0.68
  4183. param (28 + 0, -3); // Steep 8 -> 4 biquads
  4184. param (21 + 3, 6); // peaking
  4185. param (7 + 3, hzRaw (1000.0));
  4186. param (3, 600); // +6 dB
  4187. param (14 + 3, 25412);
  4188. param (35, -300); // main level −3 dB
  4189. put (0x13E, 0x7F);
  4190. const auto slot = lynx::effectFromFl ("Fruity Parametric EQ 2", blob);
  4191. CHECK (slot.has_value() && slot->kind == lynx::EffectKind::parametricEq);
  4192. const auto& p = slot->params;
  4193. CHECK (lynx::peq::type (p[(std::size_t) lynx::peq::param (0, 0)]) == lynx::peq::BandType::lowCut);
  4194. CHECK (lynx::peq::flLaw (p) && lynx::peqfl::fromParams (p).order[0] == -3); // FL's own order (REA_rodada50)
  4195. CHECK (lynx::peq::type (p[(std::size_t) lynx::peq::param (3, 0)]) == lynx::peq::BandType::peak);
  4196. CHECK (lynx::peq::type (p[(std::size_t) lynx::peq::param (1, 0)]) == lynx::peq::BandType::off);
  4197. CHECK (std::abs (lynx::peq::responseDb (p, 48000.0, 1000.0) - 3.0) < 0.3); // +6 from the peak −3 from the output
  4198. CHECK (lynx::peq::responseDb (p, 48000.0, 30.0) < -40.0); // 48 dB/oct below 100 Hz
  4199. CHECK (std::abs (lynx::peq::responseDb (p, 48000.0, 8000.0) + 3.0) < 0.3);
  4200. // a band muted by the mask doesn't sound (FL's mask travels with the slot, REA_rodada50)
  4201. put (0x13E, 0x7F & ~(1 << 3));
  4202. const auto muted = lynx::effectFromFl ("Fruity Parametric EQ 2", blob);
  4203. CHECK ((lynx::peqfl::fromParams (muted->params).mask & (1u << 3)) == 0);
  4204. CHECK (std::abs (lynx::peq::responseDb (muted->params, 48000.0, 1000.0) + 3.0) < 0.3);
  4205. }
  4206. TEST (reverb_low_cut_and_predelay)
  4207. {
  4208. // impulse: with 100 ms predelay the wet only starts later; with a 3 kHz low cut a
  4209. // 100 Hz sine barely enters the reverb
  4210. auto run = [] (const std::function<void (lynx::EffectSlot&)>& tweak, double hz, bool impulse) {
  4211. auto fx = lynx::makeEffect (lynx::EffectKind::reverb);
  4212. fx->prepare (48000.0);
  4213. auto slot = lynx::makeEffectSlot (lynx::EffectKind::reverb);
  4214. slot.params[4] = 0.0f; // wet only
  4215. slot.params[3] = 1.0f;
  4216. tweak (slot);
  4217. std::vector<float> l (48000), r (48000);
  4218. for (int i = 0; i < 48000; ++i)
  4219. 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);
  4220. for (int i = 0; i < 48000; i += 480)
  4221. fx->process (l.data() + i, r.data() + i, 480, slot.params, 120.0);
  4222. return l;
  4223. };
  4224. const auto delayed = run ([] (auto& s) { s.params[7] = 0.1f; }, 0.0, true);
  4225. float early = 0.0f, later = 0.0f;
  4226. for (std::size_t i = 0; i < 4800; ++i)
  4227. early = std::max (early, std::abs (delayed[i]));
  4228. for (std::size_t i = 4800; i < 24000; ++i)
  4229. later = std::max (later, std::abs (delayed[i]));
  4230. CHECK (early == 0.0f && later > 0.0f);
  4231. 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; };
  4232. const auto open = energy (run ([] (auto&) {}, 100.0, false));
  4233. const auto cut = energy (run ([] (auto& s) { s.params[5] = 1.0f; }, 100.0, false));
  4234. CHECK (cut < open * 0.01);
  4235. }
  4236. TEST (utility_effect_and_fl_stereo_tools)
  4237. {
  4238. // transparent by default; balance fully right silences the left
  4239. CHECK (std::abs (runEffectOnSine (lynx::EffectKind::utility, [] (auto&) {}, 440.0, 0.5f) - 0.5f) < 0.001f);
  4240. CHECK (runEffectOnSine (lynx::EffectKind::utility, [] (auto& s) { s.params[1] = 1.0f; }, 440.0, 0.5f) < 0.001f);
  4241. // Stereo Shaper at half the knob range (6400): each gain = (3^0.5 − 1)/2 (FL's knob law, REA_rodada54 §2.2)
  4242. std::vector<std::uint8_t> blob (36, 0);
  4243. auto put = [&] (std::size_t off, std::int32_t v) { std::memcpy (blob.data() + off, &v, 4); };
  4244. put (4, 6400); put (8, 6400); put (12, 6400); put (16, 6400);
  4245. const auto slot = lynx::effectFromFl ("Fruity Stereo Shaper", blob);
  4246. CHECK (slot && slot->kind == lynx::EffectKind::stereoShaper);
  4247. auto shaperFx = lynx::makeEffect (*slot);
  4248. shaperFx->prepare (44100.0);
  4249. float zl = 0.0f, zr = 0.0f;
  4250. shaperFx->process (&zl, &zr, 0, slot->params, 120.0); // takes the controls
  4251. shaperFx->reset(); // FL's Flush: the gains jump to their targets
  4252. float l = 1.0f, r = 0.0f;
  4253. shaperFx->process (&l, &r, 1, slot->params, 120.0);
  4254. const auto half = (float) ((std::sqrt (3.0) - 1.0) / 2.0);
  4255. CHECK (std::abs (l - half) < 1e-4f && std::abs (r - half) < 1e-4f);
  4256. auto fx = lynx::makeEffect (lynx::EffectKind::utility);
  4257. // Balance: volume 256 = 0 dB, pan −128 = fully left
  4258. std::vector<std::uint8_t> bal (8, 0);
  4259. std::int32_t pan = -128, vol = 256;
  4260. std::memcpy (bal.data(), &pan, 4);
  4261. std::memcpy (bal.data() + 4, &vol, 4);
  4262. const auto b = lynx::effectFromFl ("Fruity Balance", bal);
  4263. CHECK (b && b->kind == lynx::EffectKind::fruityBalance); // FL's exact law (REA_rodada58 §2): fully left sums R into L
  4264. if (b)
  4265. {
  4266. auto balance = lynx::makeEffect (*b);
  4267. balance->prepare (44100.0);
  4268. l = 0.5f; r = 0.5f;
  4269. balance->process (&l, &r, 1, b->params, 120.0);
  4270. CHECK (std::abs (l - 0.70710678f) < 1e-6f && r == 0.0f);
  4271. }
  4272. }
  4273. TEST (effects_eq_compressor_behave)
  4274. {
  4275. // EQ at default is transparent
  4276. CHECK (std::abs (runEffectOnSine (lynx::EffectKind::eq, [] (auto&) {}, 1000.0, 0.5f) - 0.5f) < 0.01f);
  4277. // +18 dB on the lows: 60 Hz rises a lot, 5 kHz barely
  4278. CHECK (runEffectOnSine (lynx::EffectKind::eq, [] (auto& s) { s.params[0] = 1.0f; }, 60.0, 0.05f) > 0.3f);
  4279. CHECK (runEffectOnSine (lynx::EffectKind::eq, [] (auto& s) { s.params[0] = 1.0f; }, 5000.0, 0.05f) < 0.06f);
  4280. // compressor: threshold -20 dB, ratio 20:1 flattens a 0 dBFS signal to near the threshold
  4281. const auto squashed = runEffectOnSine (lynx::EffectKind::compressor, [] (auto& s) {
  4282. s.params[0] = 40.0f / 60.0f;
  4283. s.params[1] = 1.0f;
  4284. }, 200.0, 1.0f);
  4285. CHECK (squashed < 0.2f && squashed > 0.05f);
  4286. // a signal below the threshold passes intact
  4287. CHECK (std::abs (runEffectOnSine (lynx::EffectKind::compressor, [] (auto& s) { s.params[0] = 1.0f; }, 200.0, 0.3f) - 0.3f) < 0.01f);
  4288. }
  4289. TEST (effects_delay_echoes_on_tempo_and_reverb_has_tail)
  4290. {
  4291. auto delay = lynx::makeEffect (lynx::EffectKind::delay);
  4292. delay->prepare (48000.0);
  4293. auto slot = lynx::makeEffectSlot (lynx::EffectKind::delay);
  4294. slot.params[0] = 3.5f / 8.0f; // index 3 = 1/4 = 0.5 s at 120 BPM
  4295. slot.params[1] = 0.0f; // no feedback: a single echo
  4296. slot.params[2] = 1.0f;
  4297. slot.params[4] = 0.0f; // no damping
  4298. std::vector<float> l (48000, 0.0f), r (48000, 0.0f);
  4299. l[0] = r[0] = 1.0f;
  4300. delay->process (l.data(), r.data(), 48000, slot.params, 120.0);
  4301. CHECK (std::abs (l[24000] - 1.0f) < 1e-4f);
  4302. CHECK (std::abs (l[12000]) < 1e-6f);
  4303. auto reverb = lynx::makeEffect (lynx::EffectKind::reverb);
  4304. reverb->prepare (48000.0);
  4305. auto rs = lynx::makeEffectSlot (lynx::EffectKind::reverb);
  4306. rs.params[3] = 1.0f;
  4307. std::fill (l.begin(), l.end(), 0.0f);
  4308. std::fill (r.begin(), r.end(), 0.0f);
  4309. l[0] = r[0] = 1.0f;
  4310. reverb->process (l.data(), r.data(), 48000, rs.params, 120.0);
  4311. float tail = 0.0f;
  4312. for (int i = 4800; i < 9600; ++i) // between 100 and 200 ms after the impulse
  4313. tail = std::max (tail, std::abs (l[(std::size_t) i]));
  4314. CHECK (tail > 1e-4f && tail < 1.0f);
  4315. CHECK (lynx::formatEffectParam (lynx::EffectKind::delay, 0, slot.params[0], 120.0) == "1/4 (500 ms)");
  4316. }
  4317. TEST (rack_runs_insert_effects_and_reuses_them)
  4318. {
  4319. // an insert with EQ at -inf doesn't exist; we use the EQ output at -18 dB to see the effect act
  4320. const auto quiet = renderMixerCase ([] (lynx::Song& s) {
  4321. auto eq = lynx::makeEffectSlot (lynx::EffectKind::eq);
  4322. eq.params[4] = 0.0f; // output -18 dB
  4323. s.mixer[1].effects.push_back (eq);
  4324. });
  4325. CHECK (quiet > 0.55f && quiet < 0.6f); // 0.5 from channel 2 + 0.5 * 0.126 from channel 1
  4326. const auto bypassed = renderMixerCase ([] (lynx::Song& s) {
  4327. auto eq = lynx::makeEffectSlot (lynx::EffectKind::eq);
  4328. eq.params[4] = 0.0f;
  4329. eq.enabled = false;
  4330. s.mixer[1].effects.push_back (eq);
  4331. });
  4332. CHECK (std::abs (bypassed - 1.0f) < 1e-4f);
  4333. auto s = lynx::Song::makeDefault();
  4334. s.mixer[2].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::reverb));
  4335. auto a = lynx::RackState::build (s, nullptr, 48000.0);
  4336. s.mixer[2].effects[0].params[0] = 0.9f; // turning a knob doesn't recreate the effect
  4337. auto b = lynx::RackState::build (s, a.get(), 48000.0);
  4338. CHECK (b->effects[2][0] == a->effects[2][0]);
  4339. s.mixer[2].effects[0] = lynx::makeEffectSlot (lynx::EffectKind::delay);
  4340. auto c = lynx::RackState::build (s, b.get(), 48000.0);
  4341. CHECK (c->effects[2][0] != b->effects[2][0]);
  4342. }
  4343. TEST (audio_clips_trigger_cut_and_resume)
  4344. {
  4345. lynx::Song s;
  4346. s.bpm = 120.0; // 1 beat = 0.5 s = 24000 samples at 48 kHz
  4347. lynx::ChannelDef c;
  4348. c.instrument = lynx::InstrumentKind::audioClip;
  4349. c.samplePath = "ramp";
  4350. c.volume = 1.0f;
  4351. s.channels.push_back (c);
  4352. s.patterns.emplace_back();
  4353. lynx::Clip clip;
  4354. clip.kind = lynx::ClipKind::audio;
  4355. clip.channel = 0;
  4356. clip.start = lynx::ppq; // starts at beat 2
  4357. clip.length = 2 * lynx::ppq; // lasts 2 beats
  4358. clip.offset = lynx::ppq; // skips the first beat of the audio
  4359. s.playlist.push_back (clip);
  4360. // an empty pattern clip over the whole bar: the song ends at beat 4 (FL loops at the last clip's exact end)
  4361. s.playlist.push_back (lynx::Clip { 0, 1, 0, 4 * lynx::ppq, 0 });
  4362. s.mode = lynx::PlayMode::song;
  4363. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  4364. lynx::EventBuffer ev;
  4365. lynx::scheduleSong (s, 0.0, 4 * 24000, tps, ev);
  4366. CHECK (ev.size() == 2);
  4367. CHECK (ev[0].type == lynx::NoteEvent::Type::on && ev[0].sampleOffset == 24000 && std::abs (ev[0].startSeconds - 0.5) < 1e-9);
  4368. CHECK (ev[1].type == lynx::NoteEvent::Type::off && ev[1].sampleOffset == 3 * 24000);
  4369. // seek to the middle of the clip (beat 2.5): with resume, starts at the block's first sample
  4370. ev.clear();
  4371. lynx::scheduleSong (s, 1.5 * lynx::ppq, 256, tps, ev, true);
  4372. CHECK (ev.size() == 1 && ev[0].sampleOffset == 0 && std::abs (ev[0].startSeconds - 0.75) < 1e-9);
  4373. ev.clear();
  4374. lynx::scheduleSong (s, 1.5 * lynx::ppq, 256, tps, ev, false);
  4375. CHECK (ev.size() == 0);
  4376. // sample whose value = position in seconds: lets us check where playback started
  4377. auto provider = [] (const std::string&) {
  4378. auto d = std::make_shared<lynx::SampleData>();
  4379. d->sampleRate = 48000.0;
  4380. for (int i = 0; i < 48000 * 3; ++i)
  4381. d->left.push_back ((float) i / 48000.0f);
  4382. return std::shared_ptr<const lynx::SampleData> (d);
  4383. };
  4384. auto state = lynx::RackState::build (s, nullptr, 48000.0, provider);
  4385. lynx::RackRenderer r;
  4386. r.prepare (48000.0, 512);
  4387. std::vector<float> l (256), rr (256);
  4388. // first block already playing mid-clip
  4389. r.process (state.get(), true, 1.5 * lynx::ppq, 256, l.data(), rr.data());
  4390. const auto unity = 1.0f / lynx::channelGain (10000.0f / 12800.0f); // clip level at 100% channel volume (REA_rodada89 §4.2)
  4391. CHECK (std::abs (l[0] - 0.75f * unity) < 0.002f);
  4392. // the clip ends at beat 3: after the short fade, silence (the sample would still have audio)
  4393. double tick = 3.0 * lynx::ppq - 10 * tps;
  4394. r.process (state.get(), false, tick, 256, l.data(), rr.data()); // stops (fade)
  4395. for (int i = 0; i < 4; ++i)
  4396. r.process (state.get(), false, tick, 256, l.data(), rr.data());
  4397. CHECK (std::abs (l[255]) < 1e-6f);
  4398. }
  4399. TEST (remove_channel_fixes_notes_and_audio_clips)
  4400. {
  4401. lynx::Song s;
  4402. s.channels.resize (3);
  4403. s.patterns.resize (1);
  4404. s.patterns[0].notes = { { 0, 240, 0, 60, 1.0f }, { 0, 240, 1, 60, 1.0f }, { 0, 240, 2, 60, 1.0f } };
  4405. lynx::Clip a;
  4406. a.kind = lynx::ClipKind::audio;
  4407. a.channel = 1;
  4408. a.length = 100;
  4409. lynx::Clip b = a;
  4410. b.channel = 2;
  4411. s.playlist = { a, b };
  4412. s.removeChannel (1);
  4413. CHECK (s.channels.size() == 2);
  4414. CHECK (s.patterns[0].notes.size() == 2 && s.patterns[0].notes[1].channel == 1);
  4415. CHECK (s.playlist.size() == 1 && s.playlist[0].channel == 1);
  4416. }
  4417. TEST (flp_import_audio_clips_with_offsets)
  4418. {
  4419. flp::Project p;
  4420. p.header.ppq = 96;
  4421. p.tempo = 120.0;
  4422. p.channels = { { 0, flp::ChannelType::audioClip, "Vocal", "", "%FLStudioData%\\v.wav", 2 },
  4423. { 1, flp::ChannelType::audioClip, "Loop", "", "%FLStudioData%\\l.wav", 3 } };
  4424. flp::Arrangement arr;
  4425. flp::PlaylistItem beats; // offset in beats: end - start = duration in beats
  4426. beats.kind = flp::PlaylistItem::Kind::channelClip;
  4427. beats.index = 0;
  4428. beats.position = 192;
  4429. beats.length = 384; // 4 beats
  4430. beats.startOffset = 2.0f;
  4431. beats.endOffset = 6.0f;
  4432. flp::PlaylistItem ms = beats; // offset in ms: 4 beats at 120 BPM = 2000 ms
  4433. ms.index = 1;
  4434. ms.startOffset = 500.0f;
  4435. ms.endOffset = 2500.0f;
  4436. arr.items = { beats, ms };
  4437. p.arrangements.push_back (arr);
  4438. lynx::FlpImportOptions options;
  4439. options.roots = { "C:\\FL", "C:\\FL\\Data", "" };
  4440. options.fileExists = [] (const std::string&) { return true; };
  4441. const auto s = lynx::importFlp (p, options);
  4442. CHECK (s.channels.size() == 2 && s.channels[0].instrument == lynx::InstrumentKind::audioClip);
  4443. CHECK (s.channels[0].samplePath == "C:\\FL\\Data\\v.wav");
  4444. CHECK (s.playlist.size() == 2);
  4445. CHECK (s.playlist[0].kind == lynx::ClipKind::audio && s.playlist[0].start == 2 * lynx::ppq);
  4446. CHECK (s.playlist[0].offset == 2 * lynx::ppq);
  4447. CHECK (s.playlist[1].offset == lynx::ppq); // 500 ms at 120 BPM = 1 beat
  4448. CHECK (s.mode == lynx::PlayMode::song);
  4449. }
  4450. TEST (plugin_factories_create_reuse_and_fallback)
  4451. {
  4452. lynx::Song s;
  4453. s.mixer = lynx::Song::defaultMixer (2);
  4454. lynx::ChannelDef c;
  4455. c.instrument = lynx::InstrumentKind::plugin;
  4456. c.plugin.id = "VST3-Synth-123";
  4457. c.mixerTrack = 1;
  4458. s.channels.push_back (c);
  4459. lynx::ChannelDef missing = c;
  4460. missing.plugin.id = "VST3-Ausente-0";
  4461. s.channels.push_back (missing);
  4462. lynx::EffectSlot fx;
  4463. fx.kind = lynx::EffectKind::plugin;
  4464. fx.plugin.id = "VST3-Reverb-9";
  4465. s.mixer[1].effects.push_back (fx);
  4466. s.patterns.emplace_back();
  4467. int instrumentsMade = 0, effectsMade = 0;
  4468. lynx::RackState::Factories f;
  4469. f.pluginInstruments = [&] (const lynx::PluginRef& ref, double) -> std::shared_ptr<lynx::Instrument> {
  4470. if (ref.id != "VST3-Synth-123")
  4471. return nullptr;
  4472. ++instrumentsMade;
  4473. auto synth = lynx::makeInstrument (lynx::InstrumentKind::synth);
  4474. synth->prepare (48000.0);
  4475. return synth;
  4476. };
  4477. f.pluginEffects = [&] (const lynx::PluginRef&, double) -> std::shared_ptr<lynx::Effect> {
  4478. ++effectsMade;
  4479. auto eq = lynx::makeEffect (lynx::EffectKind::eq);
  4480. eq->prepare (48000.0);
  4481. return eq;
  4482. };
  4483. auto a = lynx::RackState::build (s, nullptr, 48000.0, f);
  4484. CHECK (instrumentsMade == 1 && effectsMade == 1);
  4485. CHECK (a->instruments.size() == 2 && a->instruments[1] != nullptr); // missing: muted, but it exists
  4486. // changing the saved state doesn't recreate; swapping the plugin does
  4487. s.channels[0].plugin.state = "abc";
  4488. auto b = lynx::RackState::build (s, a.get(), 48000.0, f);
  4489. CHECK (instrumentsMade == 1 && b->instruments[0] == a->instruments[0] && b->effects[1][0] == a->effects[1][0]);
  4490. s.mixer[1].effects[0].plugin.id = "VST3-Outro-1";
  4491. auto c2 = lynx::RackState::build (s, b.get(), 48000.0, f);
  4492. CHECK (effectsMade == 2);
  4493. // rendering with a missing plugin and a plugin slot breaks nothing
  4494. lynx::RackRenderer r;
  4495. r.prepare (48000.0, 256);
  4496. lynx::LiveEventQueue live;
  4497. lynx::NoteEvent on;
  4498. on.channel = 0;
  4499. on.velocity = 1.0f;
  4500. live.push (on);
  4501. on.channel = 1;
  4502. live.push (on);
  4503. std::vector<float> l (256), rr (256);
  4504. r.process (c2.get(), false, 0.0, 256, l.data(), rr.data(), &live);
  4505. float peak = 0.0f;
  4506. for (auto v : l)
  4507. {
  4508. CHECK (std::isfinite (v));
  4509. peak = std::max (peak, std::abs (v));
  4510. }
  4511. CHECK (peak > 0.0f);
  4512. }
  4513. TEST (export_song_adds_silent_tail_and_never_restarts)
  4514. {
  4515. auto s = lynx::Song::makeDefault();
  4516. s.playlist.clear();
  4517. std::int64_t length = 0;
  4518. auto pat = lynx::makeExportSong (s, true, lynx::ppq * 8, length); // empty Playlist: uses the pattern
  4519. CHECK (length == lynx::ppq * 4);
  4520. CHECK (pat.mode == lynx::PlayMode::song && pat.playlist.size() == 2);
  4521. CHECK (pat.songLength() >= length + lynx::ppq * 8);
  4522. // no new notes after the song ends, only silence until the end of the tail
  4523. lynx::EventBuffer events;
  4524. const auto tps = lynx::ticksPerSample (pat.bpm, 48000.0);
  4525. int onsAfterEnd = 0, onsBefore = 0;
  4526. for (double tick = 0.0; tick < (double) (length + lynx::ppq * 8); tick += 512 * tps)
  4527. {
  4528. events.clear();
  4529. lynx::scheduleSong (pat, tick, 512, tps, events);
  4530. for (const auto& e : events)
  4531. if (e.type == lynx::NoteEvent::Type::on)
  4532. (tick + e.sampleOffset * tps >= (double) length ? onsAfterEnd : onsBefore)++;
  4533. }
  4534. CHECK (onsBefore == (int) s.patterns[0].notes.size());
  4535. CHECK (onsAfterEnd == 0);
  4536. // with Playlist: the end is the last clip's, without rounding
  4537. s.playlist.push_back ({ 0, 0, lynx::ppq * 2, lynx::ppq * 3, 0 });
  4538. auto song = lynx::makeExportSong (s, true, 0, length);
  4539. CHECK (length == lynx::ppq * 5 && song.playlist.size() == 1);
  4540. song = lynx::makeExportSong (s, false, 0, length);
  4541. CHECK (length == lynx::ppq * 4 && song.playlist[0].start == 0);
  4542. }
  4543. TEST (note_edit_selection_operations)
  4544. {
  4545. namespace ne = lynx::noteedit;
  4546. lynx::Pattern p;
  4547. p.notes = { { 0, 240, 0, 60 }, { 480, 240, 0, 64 }, { 960, 480, 1, 67 } };
  4548. // sorting keeps the indices in step
  4549. p.notes[0].start = 1200;
  4550. std::vector<int> tracked { 0, 2 };
  4551. ne::sortTracking (p, tracked);
  4552. CHECK (p.notes[2].start == 1200 && p.notes[(std::size_t) tracked[0]].start == 1200);
  4553. CHECK (p.notes[(std::size_t) tracked[1]].key == 67);
  4554. // group move respects the limits
  4555. std::int64_t dt = -5000;
  4556. int dk = 100;
  4557. ne::move (p, { 0, 1 }, dt, dk);
  4558. CHECK (dt == -480 && dk == 127 - 67 && p.notes[0].start == 0 && p.notes[1].key == 127);
  4559. // duplicate: right after the selection, rounded to the grid
  4560. lynx::Pattern q;
  4561. q.notes = { { 0, 240, 0, 60 }, { 240, 300, 0, 62 } }; // 540-tick selection
  4562. const auto added = ne::duplicate (q, { 0, 1 }, 960);
  4563. CHECK (added.size() == 2 && q.notes[(std::size_t) added[0]].start == 960 && q.notes[(std::size_t) added[1]].start == 1200);
  4564. // paste onto another channel with an offset
  4565. const auto pasted = ne::paste (q, ne::copy (q, { 1 }), 100, 3);
  4566. CHECK (pasted.size() == 1 && q.notes.back().channel == 3 && q.notes.back().start == 340);
  4567. // quantize rounds to the nearest
  4568. q.notes[0].start = 130;
  4569. q.notes[1].start = 100;
  4570. ne::quantize (q, { 0, 1 }, 240);
  4571. CHECK (q.notes[0].start == 240 && q.notes[1].start == 0);
  4572. // remove several (order and duplicates don't matter)
  4573. const auto before = q.notes.size();
  4574. ne::remove (q, { 1, 0, 1 });
  4575. CHECK (q.notes.size() == before - 2);
  4576. }
  4577. TEST (note_edit_quick_operations)
  4578. {
  4579. namespace ne = lynx::noteedit;
  4580. const std::int64_t step = lynx::ticksPerStep, bar = lynx::ppq * 4;
  4581. // split: only strictly inside; the tail is appended and does not slide again
  4582. lynx::Pattern p;
  4583. p.notes = { { 0, 4 * step, 0, 60 } };
  4584. p.notes[0].slide = true;
  4585. CHECK (ne::split (p, 0, 0) == -1 && ne::split (p, 0, 4 * step) == -1);
  4586. CHECK (ne::split (p, 0, step) == 1 && p.notes[0].length == step && p.notes[1].start == step && p.notes[1].length == 3 * step
  4587. && ! p.notes[1].slide);
  4588. // chop to grid: a 2.5-step note from half a step becomes 3 pieces cut on the step lines
  4589. lynx::Pattern c;
  4590. c.notes = { { step / 2, step * 5 / 2, 0, 60 }, { 0, step, 1, 50 } };
  4591. const auto pieces = ne::chop (c, { 0, 1 }, step);
  4592. CHECK (pieces.size() == 4 && c.notes.size() == 4);
  4593. CHECK (c.notes[0].length == step / 2 && c.notes[2].start == step && c.notes[2].length == step && c.notes[3].start == 2 * step
  4594. && c.notes[3].length == step);
  4595. // join: touching and overlapping notes of the same key and channel merge; others stay
  4596. lynx::Pattern j;
  4597. j.notes = { { 0, step, 0, 60 }, { step, step, 0, 60 }, { step * 3 / 2, step * 2, 0, 60 }, { 5 * step, step, 0, 60 },
  4598. { step, step, 0, 62 }, { step, step, 1, 60 } };
  4599. CHECK (ne::join (j, { 0, 1, 2, 3, 4, 5 }) == 2 && j.notes.size() == 4);
  4600. CHECK (j.notes[0].start == 0 && j.notes[0].length == step * 7 / 2);
  4601. // legato: each note reaches the next start (a chord ends together); the last keeps its length
  4602. lynx::Pattern l;
  4603. l.notes = { { 0, 10, 0, 60 }, { 0, 10, 0, 64 }, { step, 10, 0, 62 }, { 3 * step, 10, 0, 65 }, { step, 10, 1, 40 } };
  4604. ne::legato (l, { 0, 1, 2, 3, 4 });
  4605. CHECK (l.notes[0].length == step && l.notes[1].length == step && l.notes[2].length == 2 * step && l.notes[3].length == 10
  4606. && l.notes[4].length == 10);
  4607. // shrink to grid and full quantise (start and end)
  4608. ne::shrinkToGrid (l, { 3 }, step / 2);
  4609. CHECK (l.notes[3].length == step / 2);
  4610. lynx::Pattern q;
  4611. q.notes = { { 10, step - 30, 0, 60 }, { step + 100, 20, 0, 60 } };
  4612. ne::quantizeFull (q, { 0, 1 }, step);
  4613. CHECK (q.notes[0].start == 0 && q.notes[0].length == step && q.notes[1].start == step && q.notes[1].length == step);
  4614. // rotate inside the bar, wrapping
  4615. lynx::Pattern r;
  4616. r.notes = { { 0, step, 0, 60 }, { bar - step, step, 0, 62 } };
  4617. ne::rotate (r, { 0, 1 }, step, bar, 1);
  4618. CHECK (r.notes[0].start == step && r.notes[1].start == 0);
  4619. ne::rotate (r, { 0, 1 }, step, bar, -2);
  4620. CHECK (r.notes[0].start == bar - step && r.notes[1].start == bar - 2 * step);
  4621. r.notes.push_back ({ bar + step, step, 0, 64 }); // alone in the second bar: wraps inside that bar
  4622. ne::rotate (r, { 2 }, step, bar, -2);
  4623. CHECK (r.notes[2].start == 2 * bar - step);
  4624. }
  4625. TEST (note_edit_grid_and_time_text)
  4626. {
  4627. namespace ne = lynx::noteedit;
  4628. const std::int64_t step = lynx::ticksPerStep, bar = lynx::ppq * 4;
  4629. CHECK (ne::snapDown (250.0, step) == step && ne::snapDown (-1.0, step) == -step && ne::snapDown (17.9, 1) == 17);
  4630. CHECK (ne::snapNearest (1000.0, { 400, 1100, 980 }, 50.0, step) == 980);
  4631. CHECK (ne::snapNearest (1000.0, { 400 }, 50.0, step) == 960); // too far: falls back to the grid
  4632. // screen grid: 24 px per step shows steps; zoomed out it climbs to beats, bars and bar multiples
  4633. CHECK (ne::screenStep (24.0 / step, bar) == step);
  4634. CHECK (ne::screenStep (96.0 / bar, bar) == lynx::ppq);
  4635. CHECK (ne::screenStep (8.0 / bar, bar) == 2 * bar);
  4636. CHECK (ne::screenStep (200.0 / step, bar) == step / 4);
  4637. CHECK (ne::formatTime (0, 4) == "1:1:0" && ne::formatTime (bar + 5 * step + 7, 4) == "2:6:7");
  4638. CHECK (ne::formatTime (step * 2, 4, true) == "0:2:0");
  4639. std::int64_t t = 0;
  4640. CHECK (ne::parseTime ("2:6:7", 4, t) && t == bar + 5 * step + 7);
  4641. CHECK (ne::parseTime ("3", 4, t) && t == 2 * bar);
  4642. CHECK (ne::parseTime ("0:2:0", 4, t, true) && t == 2 * step);
  4643. CHECK (! ne::parseTime ("0:1:0", 4, t) && ! ne::parseTime ("1:17:0", 4, t) && ! ne::parseTime ("1:1:240", 4, t)
  4644. && ! ne::parseTime ("a", 4, t) && ! ne::parseTime ("", 4, t));
  4645. CHECK (ne::parseTime ("1:12:0", 3, t) && ! ne::parseTime ("1:13:0", 3, t)); // 3/4: 12 steps per bar
  4646. }
  4647. TEST (playlist_clip_edges_and_time_selection)
  4648. {
  4649. namespace ne = lynx::noteedit;
  4650. const std::int64_t beat = lynx::ppq;
  4651. // left-edge trim: content stays in place, cannot reveal what is before the content's start
  4652. lynx::Clip c { 0, 0, 4 * beat, 4 * beat, beat };
  4653. ne::trimStart (c, 5 * beat, beat);
  4654. CHECK (c.start == 5 * beat && c.offset == 2 * beat && c.length == 3 * beat);
  4655. ne::trimStart (c, 0, beat);
  4656. CHECK (c.start == 3 * beat && c.offset == 0 && c.length == 5 * beat);
  4657. ne::trimStart (c, 100 * beat, beat);
  4658. CHECK (c.length == beat && c.end() == 8 * beat);
  4659. // slip moves the content, not the clip
  4660. lynx::Clip s { 0, 0, 0, 2 * beat, beat };
  4661. ne::slip (s, beat / 2);
  4662. CHECK (s.offset == beat / 2 && s.start == 0 && s.length == 2 * beat);
  4663. ne::slip (s, beat);
  4664. CHECK (s.offset == 0);
  4665. ne::slip (s, -3 * beat);
  4666. CHECK (s.offset == 3 * beat);
  4667. // insert / delete time across all tracks, markers included
  4668. std::vector<lynx::Clip> clips { { 0, 0, 0, 2 * beat, 0 }, { 0, 1, 4 * beat, 2 * beat, 0 }, { 0, 2, beat, 6 * beat, 0 } };
  4669. std::vector<lynx::Marker> markers { { 5 * beat, "A" }, { beat / 2, "B" } };
  4670. ne::insertTime (clips, markers, 2 * beat, 4 * beat);
  4671. CHECK (clips[0].start == 0 && clips[1].start == 8 * beat && clips[2].start == beat && markers[0].tick == 9 * beat
  4672. && markers[1].tick == beat / 2);
  4673. std::vector<lynx::Clip> d { { 0, 0, 0, 2 * beat, 0 }, // before: kept
  4674. { 0, 1, 3 * beat, beat, 0 }, // inside: deleted
  4675. { 0, 2, beat, 6 * beat, 0 }, // spans the cut: head + tail
  4676. { 0, 3, 3 * beat, 4 * beat, 0 }, // tail outside: trimmed and moved
  4677. { 0, 4, 8 * beat, beat, 0 } }; // after: moved left
  4678. std::vector<lynx::Marker> dm { { 3 * beat, "in" }, { 9 * beat, "after" } };
  4679. ne::deleteTime (d, dm, 2 * beat, 5 * beat);
  4680. CHECK (d.size() == 5);
  4681. CHECK (d[1].track == 2 && d[1].start == beat && d[1].length == beat);
  4682. CHECK (d[2].track == 2 && d[2].start == 2 * beat && d[2].offset == 4 * beat && d[2].length == 2 * beat);
  4683. CHECK (d[3].track == 3 && d[3].start == 2 * beat && d[3].offset == 2 * beat && d[3].length == 2 * beat);
  4684. CHECK (d[4].start == 5 * beat && dm.size() == 1 && dm[0].tick == 6 * beat);
  4685. // time selection: overlapping clips; Ctrl+B repeats what starts inside it after itself
  4686. std::vector<lynx::Clip> r { { 0, 0, 0, beat, 0 }, { 0, 1, beat, 2 * beat, 0 }, { 0, 0, 4 * beat, beat, 0 } };
  4687. CHECK ((ne::clipsInTime (r, beat / 2, 2 * beat) == std::vector<int> { 0, 1 }));
  4688. const auto added = ne::repeatTime (r, 0, 4 * beat, {});
  4689. CHECK (added.size() == 2 && r[3].start == 4 * beat && r[4].start == 5 * beat && r[4].track == 1);
  4690. CHECK (ne::repeatTime (r, 0, 2 * beat, { 2 }).size() == 1 && r.back().start == 6 * beat);
  4691. }
  4692. TEST (recorder_builds_notes_in_pattern_and_song_mode)
  4693. {
  4694. auto song = lynx::Song::makeDefault();
  4695. const auto loop = song.patterns[0].effectiveLength (4); // 1 bar = 3840
  4696. lynx::NoteRecorder r;
  4697. r.add (song, { 100.0, 4, 60, 0.5f, true }); // no take: ignored
  4698. CHECK (r.takeFinished().empty());
  4699. r.begin (loop);
  4700. r.add (song, { 4000.0, 4, 60, 0.5f, true }); // second loop pass
  4701. r.add (song, { 4480.0, 4, 60, 0.0f, false });
  4702. r.add (song, { 5000.0, 4, 64, 0.9f, true }); // held until stop
  4703. r.end (song, 5240.0);
  4704. const auto notes = r.takeFinished();
  4705. CHECK (notes.size() == 2);
  4706. CHECK (notes[0].start == 4000 - 3840 && notes[0].length == 480 && notes[0].velocity == 0.5f && notes[0].channel == 4);
  4707. CHECK (notes[1].key == 64 && notes[1].start == 5000 - 3840 && notes[1].length == 240);
  4708. // song mode: position inside the current pattern's clip (with offset)
  4709. song.mode = lynx::PlayMode::song;
  4710. song.playlist = { { 0, 0, 7680, 3840, 960 } };
  4711. CHECK (lynx::patternPositionForTick (song, 7680.0 + 100.0, loop) == 1060);
  4712. CHECK (lynx::patternPositionForTick (song, 100.0, loop) == 100); // outside the clip: like loop
  4713. }
  4714. TEST (automation_curve_and_playback)
  4715. {
  4716. lynx::Automation a;
  4717. a.points = { { 0, 0.0f }, { 960, 1.0f }, { 1920, 0.5f, 0.0f } };
  4718. CHECK (a.valueAt (-10.0) == 0.0f && a.valueAt (480.0) == 0.5f && a.valueAt (5000.0) == 0.5f);
  4719. CHECK (a.length() == 1920);
  4720. a.points[0].tension = 1.0f; // curve: starts slow
  4721. CHECK (a.valueAt (480.0) < 0.2f && a.valueAt (480.0) > 0.0f);
  4722. // automated Master: silence in the first bar, sound in the second, and the last value
  4723. // keeps applying after the automation clip ends
  4724. auto song = lynx::Song::makeDefault();
  4725. song.bpm = 120.0;
  4726. song.mode = lynx::PlayMode::song;
  4727. const auto bar = (std::int64_t) lynx::ppq * 4;
  4728. for (int b = 0; b < 3; ++b)
  4729. song.playlist.push_back ({ 0, 0, bar * b, bar, 0 });
  4730. lynx::Automation master;
  4731. master.target = { lynx::AutomationTargetKind::mixerVolume, 0 };
  4732. master.points = { { 0, 0.0f }, { bar - 1, 0.0f }, { bar, 0.8f } };
  4733. song.automations.push_back (master);
  4734. lynx::Clip ac;
  4735. ac.kind = lynx::ClipKind::automation;
  4736. ac.automation = 0;
  4737. ac.track = 1;
  4738. ac.length = bar + bar / 2;
  4739. song.playlist.push_back (ac);
  4740. const double sr = 48000.0;
  4741. auto state = lynx::RackState::build (song, nullptr, sr);
  4742. CHECK (state->hasAutomation && state->mixerVolumeAuto[0] == 0 && state->automationClips[0].size() == 1);
  4743. lynx::RackRenderer r;
  4744. r.prepare (sr, 512);
  4745. std::vector<float> l (512), rr (512);
  4746. const auto tps = lynx::ticksPerSample (song.bpm, sr);
  4747. float peakBar1 = 0.0f, peakBar2 = 0.0f, peakBar3 = 0.0f;
  4748. for (double tick = 0.0; tick < (double) (bar * 3); tick += 512 * tps)
  4749. {
  4750. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  4751. auto& peak = tick < (double) bar - 512 * tps ? peakBar1 : tick < (double) bar * 2 ? peakBar2 : peakBar3;
  4752. for (auto v : l)
  4753. peak = std::max (peak, std::abs (v));
  4754. }
  4755. CHECK (peakBar1 < 1.0e-6f);
  4756. CHECK (peakBar2 > 0.05f);
  4757. CHECK (peakBar3 > 0.05f); // after the clip: keeps the last value
  4758. // target removed: automation disappears from the tables
  4759. song.removeChannel (0);
  4760. song.automations[0].target = { lynx::AutomationTargetKind::channelVolume, 0 };
  4761. song.removeChannel (0);
  4762. CHECK (song.automations[0].target.kind == lynx::AutomationTargetKind::none);
  4763. song.removeAutomation (0);
  4764. CHECK (song.automations.empty() && song.playlist.size() == 3);
  4765. }
  4766. TEST (mixer_sends_between_inserts)
  4767. {
  4768. // insert 1 goes only to 3; 3 goes to the Master: same sum (0.5 + 0.5)
  4769. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.setRoute (1, 3, true); s.setRoute (1, 0, false); }) - 1.0f) < 1e-4f);
  4770. // send at 50% and muted destination
  4771. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.setRoute (1, 3, true, 0.5f); s.setRoute (1, 0, false); }) - 0.75f) < 1e-4f);
  4772. 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);
  4773. // going to the Master and to 3: the signal doubles
  4774. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.setRoute (1, 3, true); }) - 1.5f) < 1e-4f);
  4775. // solo on 3 lets what reaches it sound (insert 1), but not 2
  4776. 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);
  4777. // solo on 1 lets the path to the Master (3) sound
  4778. 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);
  4779. // order and cycles
  4780. auto song = lynx::Song::makeDefault();
  4781. song.setRoute (4, 2, true);
  4782. song.setRoute (2, 1, true);
  4783. CHECK (! song.canRoute (1, 4) && ! song.canRoute (1, 2) && song.canRoute (1, 5));
  4784. song.setRoute (1, 4, true); // ignored: would create a cycle
  4785. CHECK (! song.routes (1, 4));
  4786. const auto order = song.mixerOrder();
  4787. const auto pos = [&] (int t) { return std::find (order.begin(), order.end(), t) - order.begin(); };
  4788. CHECK ((int) order.size() == (int) song.mixer.size() - 1);
  4789. CHECK (pos (4) < pos (2) && pos (2) < pos (1));
  4790. song.setRoute (4, 2, false);
  4791. CHECK (! song.routes (4, 2) && song.routes (4, 0));
  4792. // route levels (FL's route knobs): to inserts and to Master, clamped to the fader range; none without a route
  4793. song.setRoute (3, 5, true);
  4794. song.setRouteLevel (3, 5, 0.5f);
  4795. song.setRouteLevel (3, 0, 1.4f);
  4796. CHECK (song.routeLevel (3, 5) == 0.5f && song.routeLevel (3, 0) == 1.4f && song.mixer[3].masterLevel == 1.4f);
  4797. song.setRouteLevel (3, 0, 9.0f);
  4798. CHECK (song.routeLevel (3, 0) == lynx::maxFaderGain);
  4799. song.setRouteLevel (3, 6, 0.5f); // no such route: ignored
  4800. CHECK (! song.routes (3, 6) && song.routeLevel (3, 6) == 0.0f);
  4801. song.setRoute (3, 0, false);
  4802. CHECK (song.routeLevel (3, 0) == 0.0f);
  4803. // the route to Master's level scales what insert 1 sends there (0.5 + 0.5·0.25)
  4804. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.setRouteLevel (1, 0, 0.25f); }) - 0.625f) < 1e-4f);
  4805. }
  4806. namespace
  4807. {
  4808. // Test effect: delays the signal by `latency` samples and reports that latency (like a lookahead plugin).
  4809. class LatentEffect final : public lynx::Effect
  4810. {
  4811. public:
  4812. explicit LatentEffect (int samples) : latency (samples), line (samples) {}
  4813. void prepare (double) noexcept override {}
  4814. void reset() noexcept override {}
  4815. void process (float* l, float* r, int n, const lynx::EffectParams&, double) noexcept override { line.process (l, r, n); }
  4816. int latencySamples() const noexcept override { return latency; }
  4817. private:
  4818. int latency;
  4819. lynx::DelayLine line;
  4820. };
  4821. } // namespace
  4822. TEST (delay_line_mix_into_matches_process_and_drains_tail)
  4823. {
  4824. // mixInto = process over source*level, added to the destination; in stretches that cross the ring
  4825. lynx::DelayLine a (37), b (37);
  4826. std::vector<float> sl (300), sr (300), dl (300, 0.5f), dr (300, -0.5f), xl (300), xr (300);
  4827. for (int i = 0; i < 300; ++i)
  4828. {
  4829. sl[(std::size_t) i] = (float) std::sin (i * 0.1);
  4830. sr[(std::size_t) i] = (float) std::cos (i * 0.07);
  4831. }
  4832. for (int start = 0; start < 300; start += 100)
  4833. {
  4834. b.mixInto (sl.data() + start, sr.data() + start, 0.25f, dl.data() + start, dr.data() + start, 100);
  4835. for (int i = start; i < start + 100; ++i)
  4836. {
  4837. xl[(std::size_t) i] = sl[(std::size_t) i] * 0.25f;
  4838. xr[(std::size_t) i] = sr[(std::size_t) i] * 0.25f;
  4839. }
  4840. a.process (xl.data() + start, xr.data() + start, 100);
  4841. }
  4842. float worst = 0.0f;
  4843. for (int i = 0; i < 300; ++i)
  4844. worst = std::max ({ worst, std::abs (dl[(std::size_t) i] - 0.5f - xl[(std::size_t) i]),
  4845. std::abs (dr[(std::size_t) i] + 0.5f - xr[(std::size_t) i]) });
  4846. CHECK (worst < 1e-6f);
  4847. // silenced source: the tail still in the line comes out, then nothing remains
  4848. std::vector<float> tl (64, 0.0f), tr (64, 0.0f);
  4849. b.mixInto (nullptr, nullptr, 1.0f, tl.data(), tr.data(), 64);
  4850. CHECK (std::abs (tl[0] - sl[263] * 0.25f) < 1e-6f);
  4851. std::vector<float> zl (64, 0.0f), zr (64, 0.0f);
  4852. b.mixInto (nullptr, nullptr, 1.0f, zl.data(), zr.data(), 64);
  4853. CHECK (*std::max_element (zl.begin(), zl.end()) == 0.0f && *std::min_element (zl.begin(), zl.end()) == 0.0f);
  4854. }
  4855. TEST (limiter_lookahead_holds_ceiling_on_bursts)
  4856. {
  4857. // short bursts over a low floor: the sliding maximum must see each peak before it leaves
  4858. auto fx = lynx::makeEffect (lynx::EffectKind::limiter);
  4859. fx->prepare (48000.0);
  4860. auto slot = lynx::makeEffectSlot (lynx::EffectKind::limiter);
  4861. slot.params[2] = 0.0f; // 10 ms release
  4862. std::vector<float> l (48000), r (48000);
  4863. std::uint32_t seed = 1;
  4864. for (int i = 0; i < 48000; ++i)
  4865. {
  4866. seed = seed * 1664525u + 1013904223u;
  4867. const auto noise = ((float) (seed >> 8) / 16777216.0f - 0.5f) * 0.1f;
  4868. l[(std::size_t) i] = noise + ((i % 9973) < 3 ? 3.0f : 0.0f);
  4869. r[(std::size_t) i] = -noise;
  4870. }
  4871. for (int i = 0; i < 48000; i += 333)
  4872. fx->process (l.data() + i, r.data() + i, std::min (333, 48000 - i), slot.params, 120.0);
  4873. float peak = 0.0f, quiet = 0.0f;
  4874. for (int i = 0; i < 48000; ++i)
  4875. peak = std::max (peak, std::abs (l[(std::size_t) i]));
  4876. for (int i = 9973 * 2 + 2000; i < 9973 * 2 + 2400; ++i) // 40 ms after the burst the gain has already recovered
  4877. quiet = std::max (quiet, std::abs (r[(std::size_t) i]));
  4878. const auto ceiling = 1.0f; // the default ceiling is at 0 dBFS or below
  4879. CHECK (peak <= ceiling + 1e-5f);
  4880. CHECK (quiet > 0.03f);
  4881. }
  4882. TEST (pdc_aligns_paths_through_latent_effects)
  4883. {
  4884. // plan: channel 0 -> insert 1 (effect of 100) -> insert 3; channel 1 -> insert 2 -> Master
  4885. lynx::Song s;
  4886. s.mixer = lynx::Song::defaultMixer (4);
  4887. s.channels.resize (3);
  4888. s.channels[0].mixerTrack = 1;
  4889. s.channels[1].mixerTrack = 2;
  4890. s.channels[2].mixerTrack = 3;
  4891. s.mixer[1].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::eq));
  4892. s.setRoute (1, 3, true);
  4893. s.setRoute (1, 0, false);
  4894. auto plan = lynx::computePdc (s, { 0, 0, 30 }, { {}, { 100 } });
  4895. CHECK (plan.inAlign[1] == 0 && plan.outLat[1] == 100);
  4896. CHECK (plan.inAlign[3] == 100 && plan.channelDelay[2] == 70); // instrument of 30 on insert 3
  4897. CHECK (plan.sendDelay[1][1] == 0); // the slowest path isn't delayed
  4898. CHECK (plan.sendDelay[2][0] == 100 && plan.sendDelay[3][0] == 0 && plan.total == 100);
  4899. // bypass zeroes the slot's latency
  4900. s.mixer[1].effects[0].enabled = false;
  4901. CHECK (lynx::computePdc (s, { 0, 0, 30 }, { {}, { 100 } }).total == 30);
  4902. // rendered: two impulses played together come out together
  4903. lynx::Song r;
  4904. r.mixer = lynx::Song::defaultMixer (2);
  4905. for (int i = 0; i < 2; ++i)
  4906. {
  4907. lynx::ChannelDef c;
  4908. c.instrument = lynx::InstrumentKind::sampler;
  4909. c.samplePath = "impulso";
  4910. c.volume = 1.0f;
  4911. c.mixerTrack = i + 1;
  4912. r.channels.push_back (c);
  4913. }
  4914. r.patterns.emplace_back();
  4915. lynx::EffectSlot fx;
  4916. fx.kind = lynx::EffectKind::plugin;
  4917. fx.plugin.id = "latente";
  4918. r.mixer[1].effects.push_back (fx);
  4919. lynx::RackState::Factories f;
  4920. f.samples = [] (const std::string&) {
  4921. auto d = std::make_shared<lynx::SampleData>();
  4922. d->left.assign (64, 0.0f);
  4923. d->left[0] = 1.0f;
  4924. return std::shared_ptr<const lynx::SampleData> (d);
  4925. };
  4926. f.pluginEffects = [] (const lynx::PluginRef&, double) -> std::shared_ptr<lynx::Effect> { return std::make_shared<LatentEffect> (100); };
  4927. auto state = lynx::RackState::build (r, nullptr, 48000.0, f);
  4928. CHECK (state->pdc.total == 100 && state->sendDelays[2][0] != nullptr && state->sendDelays[2][0]->length() == 100);
  4929. lynx::RackRenderer renderer;
  4930. renderer.prepare (48000.0, 256);
  4931. lynx::LiveEventQueue live;
  4932. for (int ch = 0; ch < 2; ++ch)
  4933. {
  4934. lynx::NoteEvent on;
  4935. on.channel = ch;
  4936. on.key = 60;
  4937. on.velocity = 1.0f;
  4938. live.push (on);
  4939. }
  4940. std::vector<float> l (256), rr (256);
  4941. renderer.process (state.get(), false, 0.0, 256, l.data(), rr.data(), &live);
  4942. int first = -1;
  4943. for (int i = 0; i < 256 && first < 0; ++i)
  4944. if (std::abs (l[(std::size_t) i]) > 0.01f)
  4945. first = i;
  4946. CHECK (first == 100 && std::abs (l[100] - 2.0f) < 1e-3f); // both impulses arrive together, summed
  4947. // editing without changing latencies reuses the lines
  4948. r.channels[0].volume = 0.5f;
  4949. auto next = lynx::RackState::build (r, state.get(), 48000.0, f);
  4950. CHECK (next->sendDelays[2][0] == state->sendDelays[2][0]);
  4951. }
  4952. TEST (mixer_mute_fades_instead_of_clicking)
  4953. {
  4954. lynx::Song s;
  4955. s.mixer = lynx::Song::defaultMixer (2);
  4956. lynx::ChannelDef c;
  4957. c.instrument = lynx::InstrumentKind::sampler;
  4958. c.samplePath = "dc";
  4959. c.volume = 1.0f;
  4960. c.mixerTrack = 1;
  4961. s.channels.push_back (c);
  4962. s.patterns.emplace_back();
  4963. auto provider = [] (const std::string&) {
  4964. auto d = std::make_shared<lynx::SampleData>();
  4965. d->left.assign (48000, 0.5f);
  4966. return std::shared_ptr<const lynx::SampleData> (d);
  4967. };
  4968. auto on = lynx::RackState::build (s, nullptr, 48000.0, provider);
  4969. s.mixer[1].muted = true;
  4970. auto off = lynx::RackState::build (s, on.get(), 48000.0, provider);
  4971. lynx::RackRenderer r;
  4972. r.prepare (48000.0, 256);
  4973. lynx::LiveEventQueue live;
  4974. lynx::NoteEvent note;
  4975. note.velocity = 1.0f;
  4976. live.push (note);
  4977. std::vector<float> l (256), rr (256);
  4978. r.process (on.get(), false, 0.0, 256, l.data(), rr.data(), &live);
  4979. CHECK (std::abs (l[255] - 0.5f) < 1e-3f);
  4980. r.process (off.get(), false, 0.0, 256, l.data(), rr.data());
  4981. // the fader glides down by 0.001 per sample, as FL's (REA_rodada85 §3): 1000 samples from 1 to 0
  4982. 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);
  4983. for (int b = 0; b < 3; ++b)
  4984. r.process (off.get(), false, 0.0, 256, l.data(), rr.data());
  4985. CHECK (std::abs (l[255]) < 1e-6f); // down to zero after 1000 samples
  4986. r.process (off.get(), false, 0.0, 256, l.data(), rr.data());
  4987. CHECK (std::abs (l[0]) < 1e-9f);
  4988. }
  4989. TEST (dither_quantizes_to_lsb_grid_with_small_error)
  4990. {
  4991. lynx::Dither d (16);
  4992. std::vector<float> x (4800);
  4993. for (std::size_t i = 0; i < x.size(); ++i)
  4994. x[i] = 0.25f * std::sin ((float) i * 0.05f);
  4995. auto y = x;
  4996. d.process (y.data(), (int) y.size(), 0);
  4997. double errorSum = 0.0;
  4998. bool onGrid = true;
  4999. for (std::size_t i = 0; i < x.size(); ++i)
  5000. {
  5001. const auto steps = (y[i] - d.writerBias()) * 32768.0f; // half an LSB above the grid point (round 160)
  5002. onGrid = onGrid && std::abs (steps - std::round (steps)) < 1e-3f;
  5003. errorSum += std::abs (y[i] - x[i]);
  5004. }
  5005. CHECK (onGrid);
  5006. CHECK (errorSum / (double) x.size() < 4.0 / 32768.0); // a few LSBs of noise on average
  5007. }
  5008. TEST (parallel_rendering_matches_sequential)
  5009. {
  5010. // 32 channels (worth parallelizing), inserts with chained sends
  5011. auto song = lynx::Song::makeDefault();
  5012. song.mixer = lynx::Song::defaultMixer (16);
  5013. for (int i = 5; i < 32; ++i)
  5014. {
  5015. lynx::ChannelDef c;
  5016. c.instrument = i % 2 ? lynx::InstrumentKind::synth : lynx::InstrumentKind::hat;
  5017. c.mixerTrack = 1 + i % 12;
  5018. c.volume = 0.3f;
  5019. song.channels.push_back (c);
  5020. for (int step = i % 4; step < 16; step += 3)
  5021. song.patterns[0].notes.push_back ({ (std::int64_t) step * lynx::ticksPerStep, 200, i, 48 + i, 0.7f });
  5022. }
  5023. song.patterns[0].sortNotes();
  5024. song.setRoute (3, 7, true, 0.6f);
  5025. song.setRoute (7, 9, true, 0.5f);
  5026. song.setRoute (2, 9, true);
  5027. song.mixer[9].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::reverb));
  5028. auto seqState = lynx::RackState::build (song, nullptr, 48000.0);
  5029. auto parState = lynx::RackState::build (song, nullptr, 48000.0);
  5030. CHECK (parState->parallel && parState->mixerLayers.size() == 3);
  5031. CHECK (parState->graphOk); // round 165i: the task graph runs it (channels and inserts by readiness)
  5032. lynx::WorkerPool pool (3);
  5033. lynx::RackRenderer seq, par;
  5034. seq.prepare (48000.0, 512);
  5035. par.prepare (48000.0, 512);
  5036. par.setWorkerPool (&pool);
  5037. std::vector<float> l1 (512), r1 (512), l2 (512), r2 (512);
  5038. const auto tps = lynx::ticksPerSample (song.bpm, 48000.0);
  5039. bool same = true;
  5040. float peak = 0.0f;
  5041. for (int block = 0; block < 200; ++block)
  5042. {
  5043. seq.process (seqState.get(), true, block * 512 * tps, 512, l1.data(), r1.data());
  5044. par.process (parState.get(), true, block * 512 * tps, 512, l2.data(), r2.data());
  5045. same = same && l1 == l2 && r1 == r2;
  5046. for (auto v : l1)
  5047. peak = std::max (peak, std::abs (v));
  5048. }
  5049. CHECK (same);
  5050. CHECK (peak > 0.05f);
  5051. // the pool handles many small rounds in a row
  5052. std::atomic<int> sum { 0 };
  5053. auto add = [&sum] (int i) { sum.fetch_add (i + 1); };
  5054. for (int round = 0; round < 2000; ++round)
  5055. pool.parallelFor (5, add);
  5056. CHECK (sum.load() == 2000 * 15);
  5057. }
  5058. TEST (rea165i_task_graph_matches_layers_with_sidechain_and_cycle)
  5059. {
  5060. // the task graph against the layers (LYNX_TASKGRAPH off is the same as a pool-less render): sends, a sidechain,
  5061. // a chain three inserts deep, channels on the Master; a routing cycle keeps the layers
  5062. auto song = lynx::Song::makeDefault();
  5063. song.mixer = lynx::Song::defaultMixer (16);
  5064. for (int i = 5; i < 40; ++i)
  5065. {
  5066. lynx::ChannelDef c;
  5067. c.instrument = i % 3 == 0 ? lynx::InstrumentKind::hat : lynx::InstrumentKind::synth;
  5068. c.mixerTrack = i % 7 == 0 ? 0 : 1 + i % 13;
  5069. c.volume = 0.25f;
  5070. song.channels.push_back (c);
  5071. for (int step = i % 5; step < 16; step += 2)
  5072. song.patterns[0].notes.push_back ({ (std::int64_t) step * lynx::ticksPerStep, 150, i, 40 + i, 0.6f });
  5073. }
  5074. song.patterns[0].sortNotes();
  5075. song.setRoute (3, 7, true, 0.6f);
  5076. song.setRoute (7, 9, true, 0.5f);
  5077. song.setRoute (9, 11, true, 0.5f);
  5078. song.mixer[9].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::reverb));
  5079. auto comp = lynx::makeEffectSlot (lynx::EffectKind::compressor);
  5080. comp.sidechain = 4;
  5081. song.mixer[11].effects.push_back (comp);
  5082. auto render = [] (const lynx::Song& s, bool pool, bool& graph) {
  5083. auto state = lynx::RackState::build (s, nullptr, 48000.0);
  5084. graph = state->graphOk;
  5085. lynx::WorkerPool workers (4);
  5086. lynx::RackRenderer r;
  5087. r.prepare (48000.0, 512);
  5088. if (pool)
  5089. r.setWorkerPool (&workers);
  5090. std::vector<float> out, l (384), rr (384);
  5091. const auto tps = lynx::ticksPerSample (s.bpm, 48000.0);
  5092. for (int block = 0; block < 150; ++block)
  5093. {
  5094. r.process (state.get(), true, block * 384 * tps, 384, l.data(), rr.data());
  5095. out.insert (out.end(), l.begin(), l.end());
  5096. out.insert (out.end(), rr.begin(), rr.end());
  5097. }
  5098. return out;
  5099. };
  5100. bool graph = false, unused = false;
  5101. const auto layered = render (song, false, unused);
  5102. const auto parallel = render (song, true, graph);
  5103. CHECK (graph);
  5104. CHECK (layered == parallel);
  5105. CHECK (*std::max_element (layered.begin(), layered.end()) > 0.02f);
  5106. 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)
  5107. const auto cycleLayered = render (song, false, unused);
  5108. const auto cycleParallel = render (song, true, graph);
  5109. CHECK (! graph);
  5110. CHECK (cycleLayered == cycleParallel);
  5111. }
  5112. TEST (rea165i_test_profile_and_test_mode_parsing)
  5113. {
  5114. using namespace lynx::testprofile;
  5115. CHECK (! profileDirFrom (nullptr));
  5116. CHECK (! profileDirFrom (""));
  5117. CHECK (! profileDirFrom ("relative/dir")); // only absolute folders
  5118. CHECK (! profileDirFrom ("C:relative"));
  5119. CHECK (profileDirFrom ("/tmp/lynx-profile/") == std::string ("/tmp/lynx-profile"));
  5120. CHECK (profileDirFrom (" \"C:\\Users\\me\\bench profile\\\" ") == std::string ("C:\\Users\\me\\bench profile"));
  5121. CHECK (profileDirFrom ("D:/x") == std::string ("D:/x"));
  5122. CHECK (profileDirFrom ("C:\\") == std::string ("C:\\")); // a drive root keeps its separator
  5123. CHECK (! testModeFrom (nullptr));
  5124. CHECK (! testModeFrom (""));
  5125. CHECK (! testModeFrom ("0"));
  5126. CHECK (! testModeFrom (" Off "));
  5127. CHECK (! testModeFrom ("false"));
  5128. CHECK (testModeFrom ("1"));
  5129. CHECK (testModeFrom ("yes"));
  5130. }
  5131. TEST (rea165i_keep_float_mode_restores_ftz_after_foreign_code)
  5132. {
  5133. // a hosted plugin that turns flush-to-zero off (or changes the rounding) no longer leaks it into LYNX's DSP
  5134. const lynx::ScopedFlushDenormals ftz;
  5135. const auto before = _mm_getcsr();
  5136. {
  5137. const lynx::KeepFloatMode keep;
  5138. _mm_setcsr ((before & ~0x8040u) | 0x6000u); // what a plugin might leave: no FTZ/DAZ, round toward zero
  5139. }
  5140. CHECK (_mm_getcsr() == before);
  5141. volatile float tiny = 1.0e-38f;
  5142. volatile float smaller = tiny * 1.0e-3f; // denormal without FTZ
  5143. CHECK (smaller == 0.0f);
  5144. }
  5145. TEST (arrangements_switch_and_keep_clip_indices)
  5146. {
  5147. auto s = lynx::Song::makeDefault();
  5148. s.patterns.emplace_back().name = "B";
  5149. s.playlist = { { 1, 0, 0, 960, 0 } };
  5150. s.loopEnd = 960;
  5151. CHECK (s.arrangements.size() == 1 && s.currentArrangement == 0);
  5152. const auto second = s.addArrangement ("Versão curta", true);
  5153. CHECK (second == 1 && s.currentArrangement == 1 && s.playlist.size() == 1 && s.loopEnd == 960);
  5154. s.playlist.push_back ({ 0, 1, 960, 960, 0 });
  5155. s.loopEnd = 0;
  5156. s.selectArrangement (0);
  5157. CHECK (s.playlist.size() == 1 && s.loopEnd == 960 && s.arrangements[1].clips.size() == 2);
  5158. // removing pattern 0 fixes the clips stored in the other arrangement
  5159. s.removePattern (0);
  5160. CHECK (s.playlist.size() == 1 && s.playlist[0].pattern == 0);
  5161. CHECK (s.arrangements[1].clips.size() == 1 && s.arrangements[1].clips[0].pattern == 0);
  5162. s.removeArrangement (0);
  5163. CHECK (s.arrangements.size() == 1 && s.currentArrangement == 0 && s.playlist.size() == 1 && s.loopEnd == 0);
  5164. }
  5165. namespace
  5166. {
  5167. // Test effect that inverts the signal and counts how many times it processed.
  5168. class CountingInverter final : public lynx::Effect
  5169. {
  5170. public:
  5171. void prepare (double) noexcept override {}
  5172. void reset() noexcept override {}
  5173. void process (float* l, float* r, int n, const lynx::EffectParams&, double) noexcept override
  5174. {
  5175. ++calls;
  5176. for (int i = 0; i < n; ++i)
  5177. {
  5178. l[i] = -l[i];
  5179. r[i] = -r[i];
  5180. }
  5181. }
  5182. int calls = 0;
  5183. };
  5184. } // namespace
  5185. TEST (keyboard_controller_outputs_key_value)
  5186. {
  5187. std::vector<std::uint8_t> blob (554, 0);
  5188. const std::uint32_t version = 3;
  5189. std::memcpy (blob.data(), &version, 4);
  5190. const float key60 = 0.7f, off = 0.2f;
  5191. std::memcpy (blob.data() + 12 + 60 * 4, &key60, 4);
  5192. std::memcpy (blob.data() + 548, &off, 4);
  5193. blob[552] = 1; // auto release
  5194. lynx::ChannelDef c;
  5195. c.instrument = lynx::InstrumentKind::controller;
  5196. c.controllerState = blob;
  5197. auto inst = lynx::makeInstrument (lynx::InstrumentKind::controller);
  5198. inst->prepare (48000.0);
  5199. inst->configure (c, 120.0);
  5200. std::vector<float> l (64), r (64);
  5201. inst->noteOn (60, 1.0f);
  5202. inst->render (l.data(), r.data(), 64, 1.0f, 1.0f);
  5203. CHECK (std::abs (inst->controlOutput (0) - 0.7f) < 1e-6f);
  5204. inst->noteOff (60);
  5205. CHECK (std::abs (inst->controlOutput (0) - 0.2f) < 1e-6f);
  5206. }
  5207. TEST (envelope_controller_ducks_on_each_note)
  5208. {
  5209. // a unipolar articulator like "Basic Sidechain": base 0.05, drops to 0 on the note and rises to 1 in 0.3 s
  5210. lynx::ControllerArticulator a;
  5211. a.envOn = true;
  5212. a.base = 0.05f;
  5213. a.envLevel = 1.0f;
  5214. a.count = 3;
  5215. a.time = { 0.0f, 0.2f, 0.3f };
  5216. a.level = { 0.0f, 0.75f, 1.0f };
  5217. CHECK (std::abs (lynx::controllerValue (a, 0.0, -1.0) - 0.05f) < 1e-4f);
  5218. CHECK (std::abs (lynx::controllerValue (a, 0.1, -1.0) - 0.425f) < 1e-3f);
  5219. CHECK (lynx::controllerValue (a, 1.0, -1.0) > 0.99f); // no sustain: runs to the end and stays
  5220. a.sustain = 1;
  5221. CHECK (std::abs (lynx::controllerValue (a, 1.0, -1.0) - 0.8f) < 1e-3f); // held at sustain
  5222. CHECK (lynx::controllerValue (a, 1.0, 0.5) > 0.99f); // released: continues to the end
  5223. a.bipolar = true;
  5224. a.base = 0.5f;
  5225. a.sustain = -1;
  5226. CHECK (std::abs (lynx::controllerValue (a, 0.0, -1.0) - 0.0f) < 1e-4f); // bipolar: 0.5 + (0 − 0.5)
  5227. }
  5228. TEST (peak_controller_drives_live_automation)
  5229. {
  5230. // track 1: source (DC 0.5) with Peak Controller; track 2: DC 0.5 with volume linked to the controller
  5231. auto render = [] (bool linked) {
  5232. lynx::Song s;
  5233. s.mixer = lynx::Song::defaultMixer (2);
  5234. for (int t = 1; t <= 2; ++t)
  5235. {
  5236. lynx::ChannelDef c;
  5237. c.instrument = lynx::InstrumentKind::sampler;
  5238. c.samplePath = "dc";
  5239. c.volume = 1.0f;
  5240. c.mixerTrack = t;
  5241. s.channels.push_back (c);
  5242. }
  5243. s.patterns.emplace_back();
  5244. auto peak = lynx::makeEffectSlot (lynx::EffectKind::peakController);
  5245. peak.params[0] = 1.0f; // base 1
  5246. peak.params[1] = 0.0f; // amount −1: 1 − peak
  5247. peak.params[2] = 0.5f; // no tension
  5248. s.mixer[1].effects.push_back (peak);
  5249. if (linked)
  5250. {
  5251. lynx::Automation a;
  5252. a.name = "Peak";
  5253. a.target = { lynx::AutomationTargetKind::mixerVolume, 2 };
  5254. a.source = { 1, 0, 0 };
  5255. s.automations.push_back (a);
  5256. }
  5257. lynx::RackState::Factories f;
  5258. f.samples = [] (const std::string&) {
  5259. auto d = std::make_shared<lynx::SampleData>();
  5260. d->left.assign (48000, 0.5f);
  5261. return std::shared_ptr<const lynx::SampleData> (d);
  5262. };
  5263. auto state = lynx::RackState::build (s, nullptr, 48000.0, f);
  5264. lynx::RackRenderer r;
  5265. r.prepare (48000.0, 512);
  5266. lynx::LiveEventQueue live;
  5267. for (int ch = 0; ch < 2; ++ch)
  5268. {
  5269. lynx::NoteEvent on;
  5270. on.channel = ch;
  5271. on.velocity = 1.0f;
  5272. live.push (on);
  5273. }
  5274. std::vector<float> l (4800), rr (4800);
  5275. r.process (state.get(), false, 0.0, 4800, l.data(), rr.data(), &live);
  5276. return l[4000];
  5277. };
  5278. const auto free = render (false), ducked = render (true);
  5279. CHECK (std::isfinite (ducked) && ducked > 0.0f);
  5280. CHECK (ducked < free - 0.05f); // the 0.5 peak on track 1 lowers track 2's volume
  5281. }
  5282. TEST (effect_slot_dry_wet_and_smart_disable)
  5283. {
  5284. lynx::Song s;
  5285. s.mixer = lynx::Song::defaultMixer (1);
  5286. lynx::ChannelDef c;
  5287. c.instrument = lynx::InstrumentKind::sampler;
  5288. c.samplePath = "dc";
  5289. c.volume = 1.0f;
  5290. c.mixerTrack = 1;
  5291. s.channels.push_back (c);
  5292. s.patterns.emplace_back();
  5293. lynx::EffectSlot fx;
  5294. fx.kind = lynx::EffectKind::plugin;
  5295. fx.plugin.id = "inversor";
  5296. fx.mix = 0.25f; // 75% dry (+0.5) and 25% wet (-0.5) = +0.25
  5297. s.mixer[1].effects.push_back (fx);
  5298. auto inverter = std::make_shared<CountingInverter>();
  5299. lynx::RackState::Factories f;
  5300. f.samples = [] (const std::string&) {
  5301. auto d = std::make_shared<lynx::SampleData>();
  5302. d->left.assign (4800, 0.5f);
  5303. return std::shared_ptr<const lynx::SampleData> (d);
  5304. };
  5305. f.pluginEffects = [inverter] (const lynx::PluginRef&, double) -> std::shared_ptr<lynx::Effect> { return inverter; };
  5306. auto state = lynx::RackState::build (s, nullptr, 48000.0, f);
  5307. lynx::RackRenderer r;
  5308. r.prepare (48000.0, 512);
  5309. lynx::LiveEventQueue live;
  5310. lynx::NoteEvent on;
  5311. on.velocity = 1.0f;
  5312. live.push (on);
  5313. std::vector<float> l (480), rr (480);
  5314. r.process (state.get(), false, 0.0, 480, l.data(), rr.data(), &live);
  5315. CHECK (std::abs (l[100] - 0.25f) < 1e-4f);
  5316. // after the sample ends: 4 s of silence and the plugin stops being called
  5317. for (int i = 0; i < 100 * 6; ++i) // 6 s
  5318. r.process (state.get(), false, 0.0, 480, l.data(), rr.data());
  5319. const auto callsAfterSilence = inverter->calls;
  5320. for (int i = 0; i < 100; ++i)
  5321. r.process (state.get(), false, 0.0, 480, l.data(), rr.data());
  5322. CHECK (inverter->calls == callsAfterSilence);
  5323. CHECK (callsAfterSilence < 100 * 6);
  5324. // sound again: processing resumes in the same block
  5325. live.push (on);
  5326. r.process (state.get(), false, 0.0, 480, l.data(), rr.data(), &live);
  5327. CHECK (inverter->calls == callsAfterSilence + 1 && std::abs (l[100] - 0.25f) < 1e-4f);
  5328. }
  5329. TEST (swing_delays_offbeat_sixteenths)
  5330. {
  5331. CHECK (lynx::swingDelay (0, 1.0f) == 0);
  5332. CHECK (lynx::swingDelay (lynx::ticksPerStep, 1.0f) == lynx::ticksPerStep / 2);
  5333. CHECK (lynx::swingDelay (lynx::ticksPerStep * 2, 1.0f) == 0);
  5334. CHECK (lynx::swingDelay (lynx::ticksPerStep + 10, 1.0f) == (lynx::ticksPerStep - 20) / 2); // off the grid: triangular weight (FL)
  5335. CHECK (lynx::swingDelay (lynx::ticksPerStep, 1.0f, 0.5f) == lynx::ticksPerStep / 4); // channel mix
  5336. CHECK (lynx::swingDelay (lynx::ticksPerStep * 3, 0.5f) == lynx::ticksPerStep / 4);
  5337. lynx::Pattern p;
  5338. p.notes = { { 0, 100, 0, 60 }, { lynx::ticksPerStep, 100, 0, 62 } };
  5339. lynx::EventBuffer ev;
  5340. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  5341. lynx::schedulePatternLoop (p, p.effectiveLength (4), 0.0, 12000, tps, ev, 1.0f);
  5342. int onOffset = -1;
  5343. for (auto& e : ev)
  5344. if (e.type == lynx::NoteEvent::Type::on && e.key == 62)
  5345. onOffset = e.sampleOffset;
  5346. CHECK (onOffset == (int) std::floor ((lynx::ticksPerStep * 1.5) / tps));
  5347. }
  5348. TEST (sampler_settings_pitch_reverse_envelope_cut)
  5349. {
  5350. auto ramp = std::make_shared<lynx::SampleData>();
  5351. for (int i = 0; i < 48000; ++i)
  5352. ramp->left.push_back ((float) i / 48000.0f); // ramp: the value tells where the read position is
  5353. ramp->sampleRate = 48000.0;
  5354. auto play = [&] (const lynx::SamplerSettings& s, int key, int frames, bool releaseAfterHalf) {
  5355. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  5356. inst->prepare (48000.0);
  5357. lynx::ChannelDef c;
  5358. c.sampler = s;
  5359. inst->configure (c, 120.0);
  5360. inst->noteOn (key, 1.0f);
  5361. std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
  5362. inst->render (l.data(), r.data(), frames / 2, 1.0f, 1.0f);
  5363. if (releaseAfterHalf)
  5364. inst->noteOff (key);
  5365. inst->render (l.data() + frames / 2, r.data() + frames / 2, frames - frames / 2, 1.0f, 1.0f);
  5366. return l;
  5367. };
  5368. // +12 semitones: reads twice as fast
  5369. lynx::SamplerSettings up;
  5370. up.pitch = 12.0f;
  5371. CHECK (std::abs (play (up, 60, 1000, false)[999] - 2.0f * 999.0f / 48000.0f) < 1e-3f);
  5372. // reversed: starts from the end
  5373. lynx::SamplerSettings back;
  5374. back.reverse = true;
  5375. CHECK (play (back, 60, 100, false)[10] > 0.99f);
  5376. // 10 ms attack: starts silent and reaches full level
  5377. lynx::SamplerSettings slow;
  5378. slow.attack = 0.01f;
  5379. const auto a = play (slow, 60, 2000, false);
  5380. CHECK (a[10] < 0.2f * (10.0f / 48000.0f) + 1e-6f && std::abs (a[1500] - 1500.0f / 48000.0f) < 1e-4f);
  5381. // 5 ms release: gone right after release; without release it plays in full
  5382. lynx::SamplerSettings rel;
  5383. rel.release = 0.005f;
  5384. CHECK (play (rel, 60, 2000, true)[1999] == 0.0f);
  5385. CHECK (play (lynx::SamplerSettings {}, 60, 2000, true)[1999] > 0.0f);
  5386. // cut itself: the second note cuts the first (one voice remains)
  5387. lynx::SamplerSettings cut;
  5388. cut.cut = true;
  5389. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  5390. inst->prepare (48000.0);
  5391. lynx::ChannelDef cutChannel;
  5392. cutChannel.sampler = cut;
  5393. inst->configure (cutChannel, 120.0);
  5394. inst->noteOn (60, 1.0f);
  5395. std::vector<float> l (2000), r (2000);
  5396. inst->render (l.data(), r.data(), 1000, 1.0f, 1.0f);
  5397. inst->noteOn (60, 1.0f);
  5398. std::fill (l.begin(), l.end(), 0.0f);
  5399. inst->render (l.data(), r.data(), 2000, 1.0f, 1.0f);
  5400. CHECK (std::abs (l[1999] - 1999.0f / 48000.0f) < 1e-4f); // only the new note
  5401. }
  5402. TEST (playlist_split_and_track_mute)
  5403. {
  5404. lynx::Clip c { 0, 3, 960, 3840, 480 };
  5405. const auto rest = lynx::splitClip (c, 2000);
  5406. CHECK (c.start == 960 && c.length == 1040 && c.offset == 480);
  5407. CHECK (rest.start == 2000 && rest.length == 2800 && rest.offset == 480 + 1040 && rest.track == 3);
  5408. auto s = lynx::Song::makeDefault();
  5409. s.mode = lynx::PlayMode::song;
  5410. s.playlist = { { 0, 0, 0, 3840, 0 } };
  5411. lynx::EventBuffer ev;
  5412. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  5413. lynx::scheduleSong (s, 0.0, 48000, tps, ev);
  5414. CHECK (ev.size() > 0);
  5415. s.playlistTracks.resize (1);
  5416. s.playlistTracks[0].muted = true;
  5417. ev.clear();
  5418. lynx::scheduleSong (s, 0.0, 48000, tps, ev);
  5419. CHECK (ev.size() == 0);
  5420. }
  5421. TEST (note_pan_pitch_and_mute)
  5422. {
  5423. lynx::Pattern p;
  5424. lynx::Note a { 0, 240, 0, 60 };
  5425. a.pan = -1.0f;
  5426. a.finePitch = 50.0f;
  5427. lynx::Note b { 240, 240, 0, 62 };
  5428. b.muted = true;
  5429. p.notes = { a, b };
  5430. lynx::EventBuffer ev;
  5431. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  5432. lynx::schedulePatternLoop (p, p.effectiveLength (4), 0.0, 48000, tps, ev);
  5433. int ons = 0;
  5434. for (auto& e : ev)
  5435. if (e.type == lynx::NoteEvent::Type::on)
  5436. {
  5437. ++ons;
  5438. CHECK (e.key == 60 && e.pan == -1.0f && e.cents == 50.0f);
  5439. }
  5440. CHECK (ons == 1); // the muted one doesn't play
  5441. // note pan in the sampler: fully left
  5442. auto dc = std::make_shared<lynx::SampleData>();
  5443. dc->left.assign (4800, 0.5f);
  5444. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, dc);
  5445. inst->prepare (48000.0);
  5446. inst->setNextNoteExpression (-1.0f, 0.0f);
  5447. inst->noteOn (60, 1.0f);
  5448. std::vector<float> l (100), r (100);
  5449. inst->render (l.data(), r.data(), 100, 1.0f, 1.0f);
  5450. CHECK (l[50] > 0.6f && std::abs (r[50]) < 1e-6f);
  5451. }
  5452. TEST (tempo_automation_changes_speed_and_duration)
  5453. {
  5454. auto song = lynx::Song::makeDefault();
  5455. song.bpm = 120.0;
  5456. song.mode = lynx::PlayMode::song;
  5457. const auto bar = (std::int64_t) lynx::ppq * 4;
  5458. song.playlist = { { 0, 0, 0, bar, 0 }, { 0, 0, bar, bar, 0 } };
  5459. lynx::Automation tempo;
  5460. tempo.target = { lynx::AutomationTargetKind::tempo, 0 };
  5461. const auto v120 = lynx::valueFromTempo (120.0), v240 = lynx::valueFromTempo (240.0);
  5462. tempo.points = { { 0, v120 }, { bar - 1, v120 }, { bar, v240 } }; // step: 120 -> 240 at bar 2
  5463. song.automations.push_back (tempo);
  5464. lynx::Clip clip;
  5465. clip.kind = lynx::ClipKind::automation;
  5466. clip.automation = 0;
  5467. clip.track = 1;
  5468. clip.length = bar * 2;
  5469. song.playlist.push_back (clip);
  5470. CHECK (std::abs (song.tempoAt (100.0) - 120.0) < 0.01 && std::abs (song.tempoAt ((double) bar + 100.0) - 240.0) < 0.01);
  5471. // 1 bar at 120 (2 s) + 1 at 240 (1 s)
  5472. CHECK (std::abs (song.secondsForTicks (bar * 2) - 3.0) < 0.01);
  5473. auto state = lynx::RackState::build (song, nullptr, 48000.0);
  5474. CHECK (state->tempoAutomation == 0);
  5475. lynx::RackRenderer r;
  5476. r.prepare (48000.0, 512);
  5477. std::vector<float> l (512), rr (512);
  5478. r.process (state.get(), true, 10.0, 512, l.data(), rr.data());
  5479. CHECK (std::abs (r.currentBpm() - 120.0) < 0.01);
  5480. r.process (state.get(), true, (double) bar + 10.0, 512, l.data(), rr.data());
  5481. CHECK (std::abs (r.currentBpm() - 240.0) < 0.01);
  5482. }
  5483. TEST (bounce_insert_song_and_apply)
  5484. {
  5485. auto s = lynx::Song::makeDefault();
  5486. s.setRoute (3, 7, true, 0.5f);
  5487. s.mixer[0].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::reverb));
  5488. s.mixer[2].solo = true;
  5489. const auto b = lynx::makeBounceSong (s, 3);
  5490. CHECK (b.mixer[3].solo && ! b.mixer[2].solo && b.mixer[3].toMaster && b.mixer[3].sends.empty());
  5491. CHECK (b.mixer[0].effects.empty() && b.mode == lynx::PlayMode::song);
  5492. const auto audible = b.soloAudible();
  5493. CHECK (audible[3] && ! audible[1] && ! audible[7]);
  5494. const auto channels = s.channels.size();
  5495. const auto inserts = s.mixer.size();
  5496. const auto created = lynx::applyBounce (s, 3, "C:/x.wav", 3840);
  5497. CHECK (created == (int) inserts && s.mixer.size() == inserts + 1 && s.mixer[3].muted);
  5498. CHECK (s.mixer[(std::size_t) created].sends.size() == 1 && s.mixer[(std::size_t) created].sends[0].target == 7);
  5499. CHECK (s.channels.size() == channels + 1 && s.channels.back().mixerTrack == created);
  5500. CHECK (s.playlist.back().kind == lynx::ClipKind::audio && s.playlist.back().length == 3840);
  5501. }
  5502. TEST (mixer_stereo_separation_phase_swap)
  5503. {
  5504. // FL's level matrix (REA_rodada2 §2.2, REA_rodada167j §1): outL = ll·inL + rl·inR, outR = rr·inR + lr·inL
  5505. lynx::MixerTrack t;
  5506. auto near = [] (float a, float b) { return std::abs (a - b) < 1e-5f; };
  5507. t.stereo = 1.0f; // mono
  5508. auto m = lynx::mixerMatrix (t, lynx::unityFader, 0.0f, false);
  5509. CHECK (near (m.ll, 0.5f) && near (m.rl, 0.5f) && near (m.rr, 0.5f) && near (m.lr, 0.5f));
  5510. t.stereo = -1.0f; // doubled side: 1.5 L - 0.5 R
  5511. m = lynx::mixerMatrix (t, lynx::unityFader, 0.0f, false);
  5512. CHECK (near (m.ll, 1.5f) && near (m.rl, -0.5f) && near (m.rr, 1.5f) && near (m.lr, -0.5f));
  5513. t.stereo = 0.0f;
  5514. t.swapChannels = true;
  5515. t.invertPhase = true;
  5516. m = lynx::mixerMatrix (t, lynx::unityFader, 0.0f, false);
  5517. CHECK (m.ll == 0.0f && m.rl == -1.0f && m.rr == 0.0f && m.lr == -1.0f); // outL = −inR, outR = −inL
  5518. // unity, centre, no separation: exactly the identity (FL's fast path)
  5519. m = lynx::mixerMatrix (lynx::MixerTrack {}, lynx::unityFader, 0.0f, false);
  5520. CHECK (m == lynx::MixerMatrix {});
  5521. }
  5522. TEST (mixer_pan_fold_and_panning_law_match_fl_render)
  5523. {
  5524. // FL 2026's render of j_tri.flp (round 167j: a 441 Hz left / 882 Hz right sample on ten inserts, pan law
  5525. // triangular), the matrix fitted per insert: {pan, separation, ll, rl, lr, rr}
  5526. struct Case { float pan, sep, ll, rl, lr, rr; };
  5527. 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 },
  5528. { 0.25f, 0.0f, 0.75f, 0.0f, 0.125001f, 0.874978f }, { 0.5f, 0.0f, 0.499985f, 0.0f, 0.250011f, 0.749964f },
  5529. { 1.0f, 0.0f, 0.0f, 0.0f, 0.499982f, 0.499972f }, { 0.5f, 0.5f, 0.422756f, 0.077253f, 0.4045f, 0.595469f },
  5530. { -0.5f, -0.5f, 1.0f, 0.0f, -0.125004f, 0.624974f }, { 0.75f, 0.0f, 0.250003f, 0.0f, 0.375006f, 0.624975f },
  5531. { 0.0f, 0.75f, 0.70703f, 0.292941f, 0.292965f, 0.707045f } };
  5532. for (const auto& c : fl)
  5533. {
  5534. lynx::MixerTrack t;
  5535. t.stereo = c.sep;
  5536. const auto m = lynx::mixerMatrix (t, lynx::unityFader, c.pan, true);
  5537. // the fit's own error is ~5e-5 (16-bit render)
  5538. 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
  5539. && std::abs (m.rr - c.rr) < 2e-4f);
  5540. }
  5541. // circular (the default): +3 dB on the side panned to, and the folded half of the other side
  5542. const auto hard = lynx::mixerMatrix (lynx::MixerTrack {}, lynx::unityFader, 1.0f, false);
  5543. CHECK (std::abs (hard.ll) < 1e-6f && std::abs (hard.rl) < 1e-6f);
  5544. CHECK (std::abs (hard.rr - 0.70710677f) < 1e-6f && std::abs (hard.lr - 0.70710677f) < 1e-6f);
  5545. const auto half = lynx::mixerMatrix (lynx::MixerTrack {}, lynx::unityFader, 0.5f, false);
  5546. 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;
  5547. CHECK (std::abs (half.ll - gl) < 1e-5f && half.rl == 0.0f);
  5548. 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);
  5549. // the fader scales the whole matrix (−6 dB-ish position), polarity negates it
  5550. lynx::MixerTrack inv;
  5551. inv.invertPhase = true;
  5552. const auto low = lynx::mixerMatrix (inv, 0.5f, 0.0f, false);
  5553. const auto g = (float) ((std::exp ((double) (0.625f * 2.3978953f)) - 1.0) * 0.1);
  5554. CHECK (std::abs (low.ll + g) < 1e-5f && std::abs (low.rr + g) < 1e-5f && std::abs (low.rl) < 1e-6f);
  5555. }
  5556. TEST (effects_limiter_filter_distortion_chorus)
  5557. {
  5558. const double sr = 48000.0;
  5559. auto sine = [] (float hz, float amp, int n) {
  5560. std::vector<float> v ((std::size_t) n);
  5561. for (int i = 0; i < n; ++i)
  5562. v[(std::size_t) i] = amp * std::sin (2.0f * 3.14159265f * hz * (float) i / 48000.0f);
  5563. return v;
  5564. };
  5565. auto peak = [] (const std::vector<float>& v, int from) {
  5566. float p = 0.0f;
  5567. for (std::size_t i = (std::size_t) from; i < v.size(); ++i)
  5568. p = std::max (p, std::abs (v[i]));
  5569. return p;
  5570. };
  5571. // limiter: +12 dB on a 0.9 sine never exceeds the ceiling (-1 dB) and reports the latency
  5572. auto lim = lynx::makeEffect (lynx::EffectKind::limiter);
  5573. lim->prepare (sr);
  5574. auto slot = lynx::makeEffectSlot (lynx::EffectKind::limiter);
  5575. slot.params[0] = 11.0f / 12.0f; // -1 dB
  5576. slot.params[1] = 0.5f; // +12 dB
  5577. auto l = sine (100.0f, 0.9f, 48000), r = l;
  5578. lim->process (l.data(), r.data(), 48000, slot.params, 120.0);
  5579. CHECK (peak (l, 0) <= std::pow (10.0f, -1.0f / 20.0f) + 1e-4f);
  5580. CHECK (peak (l, 4800) > 0.8f);
  5581. CHECK (lim->latencySamples() == 72);
  5582. // low-pass filter at 500 Hz knocks down an 8 kHz sine
  5583. auto lp = lynx::makeEffect (lynx::EffectKind::filter);
  5584. lp->prepare (sr);
  5585. auto fs = lynx::makeEffectSlot (lynx::EffectKind::filter);
  5586. fs.params[1] = std::log (500.0f / 20.0f) / std::log (1000.0f);
  5587. l = sine (8000.0f, 1.0f, 9600);
  5588. r = l;
  5589. lp->process (l.data(), r.data(), 9600, fs.params, 120.0);
  5590. CHECK (peak (l, 4800) < 0.05f);
  5591. // distortion and chorus: finite, audible output
  5592. for (auto kind : { lynx::EffectKind::distortion, lynx::EffectKind::chorus })
  5593. {
  5594. auto fx = lynx::makeEffect (kind);
  5595. fx->prepare (sr);
  5596. auto ps = lynx::makeEffectSlot (kind);
  5597. l = sine (220.0f, 0.5f, 9600);
  5598. r = l;
  5599. fx->process (l.data(), r.data(), 9600, ps.params, 120.0);
  5600. bool finite = true;
  5601. for (auto v : l)
  5602. finite = finite && std::isfinite (v);
  5603. CHECK (finite && peak (l, 2400) > 0.1f && peak (l, 0) < 2.0f);
  5604. }
  5605. }
  5606. TEST (slide_notes_glide_without_retrigger)
  5607. {
  5608. // sampler with a ramp: the read speed doubles after a one-octave slide
  5609. auto ramp = std::make_shared<lynx::SampleData>();
  5610. for (int i = 0; i < 96000; ++i)
  5611. ramp->left.push_back ((float) i / 96000.0f);
  5612. ramp->sampleRate = 48000.0;
  5613. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  5614. inst->prepare (48000.0);
  5615. inst->noteOn (60, 1.0f);
  5616. std::vector<float> l (2000), r (2000);
  5617. inst->render (l.data(), r.data(), 1000, 1.0f, 1.0f);
  5618. const auto stepBefore = l[999] - l[998];
  5619. inst->slideTo (72, 0.0f, 100.0f);
  5620. std::fill (l.begin(), l.end(), 0.0f);
  5621. inst->render (l.data(), r.data(), 1000, 1.0f, 1.0f);
  5622. const auto stepAfter = l[999] - l[998];
  5623. CHECK (std::abs (stepAfter / stepBefore - 2.0f) < 0.01f);
  5624. // the sequencer sends "slide" (no off) for the slide note
  5625. lynx::Pattern p;
  5626. lynx::Note base { 0, 960, 0, 48 };
  5627. lynx::Note glide { 480, 240, 0, 55 };
  5628. glide.slide = true;
  5629. p.notes = { base, glide };
  5630. lynx::EventBuffer ev;
  5631. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  5632. lynx::schedulePatternLoop (p, p.effectiveLength (4), 0.0, 30000, tps, ev);
  5633. int ons = 0, offs = 0, slides = 0;
  5634. for (auto& e : ev)
  5635. {
  5636. ons += e.type == lynx::NoteEvent::Type::on;
  5637. offs += e.type == lynx::NoteEvent::Type::off;
  5638. if (e.type == lynx::NoteEvent::Type::slide)
  5639. {
  5640. ++slides;
  5641. CHECK (e.key == 55 && std::abs (e.slideSamples - 240.0f / (float) tps) < 1.0f);
  5642. }
  5643. }
  5644. CHECK (ons == 1 && offs == 1 && slides == 1);
  5645. }
  5646. TEST (synth_fm_phase_modulation)
  5647. {
  5648. auto render = [] (const lynx::SynthSettings& s) {
  5649. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  5650. inst->prepare (48000.0);
  5651. lynx::ChannelDef c;
  5652. c.synth = s;
  5653. inst->configure (c, 120.0);
  5654. inst->noteOn (69, 1.0f); // 440 Hz
  5655. std::vector<float> l (48000), r (48000);
  5656. inst->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
  5657. return l;
  5658. };
  5659. // energy (Goertzel) at frequency f over frames [from, to)
  5660. auto energy = [] (const std::vector<float>& x, double f, int from, int to) {
  5661. const auto w = 2.0 * 3.141592653589793 * f / 48000.0, k = 2.0 * std::cos (w);
  5662. double s1 = 0.0, s2 = 0.0;
  5663. for (int i = from; i < to; ++i)
  5664. {
  5665. const auto s0 = x[(std::size_t) i] + k * s1 - s2;
  5666. s2 = s1;
  5667. s1 = s0;
  5668. }
  5669. return s1 * s1 + s2 * s2 - k * s1 * s2;
  5670. };
  5671. lynx::SynthSettings plain;
  5672. plain.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  5673. plain.osc[2] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  5674. plain.cutoff = 20000.0f;
  5675. auto fm = plain;
  5676. fm.fm = 1.0f;
  5677. const auto a = render (plain), b = render (fm);
  5678. // no FM: osc 3 adds (same pitch) and there's no 2nd harmonic; with FM the sidebands appear
  5679. CHECK (energy (a, 880.0, 4800, 24000) < 1e-3 * energy (a, 440.0, 4800, 24000));
  5680. CHECK (energy (b, 880.0, 4800, 24000) > 0.1 * energy (b, 440.0, 4800, 24000));
  5681. CHECK (energy (b, 1320.0, 4800, 24000) > 0.01 * energy (b, 440.0, 4800, 24000));
  5682. for (auto v : b)
  5683. CHECK (std::isfinite (v) && std::abs (v) < 1.0f);
  5684. // index decaying to 0: the start is bright, the end is nearly a sine again
  5685. auto decaying = fm;
  5686. decaying.fmDecay = 0.1f;
  5687. decaying.fmSustain = 0.0f;
  5688. const auto c = render (decaying);
  5689. const auto early = energy (c, 880.0, 0, 2400) / energy (c, 440.0, 0, 2400);
  5690. const auto late = energy (c, 880.0, 36000, 48000) / energy (c, 440.0, 36000, 48000);
  5691. CHECK (early > 0.05 && late < 1e-3);
  5692. }
  5693. TEST (synth_three_oscillators_adsr_filter)
  5694. {
  5695. auto render = [] (const lynx::SynthSettings& s, int frames, int releaseAt) {
  5696. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  5697. inst->prepare (48000.0);
  5698. lynx::ChannelDef c;
  5699. c.synth = s;
  5700. inst->configure (c, 120.0);
  5701. inst->noteOn (69, 1.0f); // A4 = 440 Hz
  5702. std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
  5703. inst->render (l.data(), r.data(), releaseAt, 1.0f, 1.0f);
  5704. inst->noteOff (69);
  5705. inst->render (l.data() + releaseAt, r.data() + releaseAt, frames - releaseAt, 1.0f, 1.0f);
  5706. return l;
  5707. };
  5708. auto peakIn = [] (const std::vector<float>& v, int from, int to) {
  5709. float p = 0.0f;
  5710. for (int i = from; i < to; ++i)
  5711. p = std::max (p, std::abs (v[(std::size_t) i]));
  5712. return p;
  5713. };
  5714. // default: sounds and fades after the release
  5715. const lynx::SynthSettings standard;
  5716. const auto a = render (standard, 48000, 24000);
  5717. CHECK (peakIn (a, 1000, 20000) > 0.05f && peakIn (a, 40000, 48000) < 1e-3f);
  5718. // pure sine with an open filter: zero-crossing count gives ~440 Hz
  5719. lynx::SynthSettings sine;
  5720. sine.osc[0].wave = lynx::Waveform::sine;
  5721. sine.cutoff = 20000.0f;
  5722. const auto b = render (sine, 48000, 47000);
  5723. int crossings = 0;
  5724. for (int i = 4800; i < 4800 + 48000 / 2; ++i)
  5725. crossings += (b[(std::size_t) i - 1] < 0.0f) != (b[(std::size_t) i] < 0.0f);
  5726. CHECK (std::abs (crossings - 440) < 6); // half a second = 220 cycles = 440 crossings
  5727. float sinePeak = 0.0f;
  5728. for (int i = 4800; i < 9600; ++i)
  5729. sinePeak = std::max (sinePeak, std::abs (b[(std::size_t) i]));
  5730. CHECK (sinePeak > 0.2f && sinePeak < 0.35f); // 0.3 level, approximated sine without clipping
  5731. // second oscillator an octave up adds energy; sustain 0 makes the note decay on its own
  5732. lynx::SynthSettings two = sine;
  5733. two.osc[1] = { lynx::Waveform::sine, 1, 0.0f, 1.0f };
  5734. two.sustain = 0.0f;
  5735. two.decay = 0.1f;
  5736. const auto c = render (two, 48000, 47000);
  5737. CHECK (peakIn (c, 100, 2000) > peakIn (b, 100, 2000) && peakIn (c, 30000, 40000) < 1e-3f);
  5738. }
  5739. TEST (set_target_value_round_trips)
  5740. {
  5741. auto s = lynx::Song::makeDefault();
  5742. s.mixer[2].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::delay));
  5743. const lynx::AutomationTarget targets[] = {
  5744. { lynx::AutomationTargetKind::channelVolume, 1 }, { lynx::AutomationTargetKind::channelPan, 2 },
  5745. { lynx::AutomationTargetKind::mixerVolume, 3 }, { lynx::AutomationTargetKind::mixerPan, 0 },
  5746. { lynx::AutomationTargetKind::effectParam, 2, 0, 1 }, { lynx::AutomationTargetKind::tempo, 0 },
  5747. };
  5748. for (const auto& t : targets)
  5749. {
  5750. CHECK (s.setTargetValue (t, 0.25f));
  5751. CHECK (std::abs (s.targetValue (t) - 0.25f) < 1e-5f);
  5752. }
  5753. CHECK (! s.setTargetValue ({ lynx::AutomationTargetKind::channelParam, 0, 0, 3 }, 0.5f)); // lives in the plugin
  5754. }
  5755. TEST (cut_groups_stretch_and_envelope_import)
  5756. {
  5757. // open hat (cut by 1) is cut when the closed one (group 1) plays
  5758. lynx::Song s;
  5759. s.mixer = lynx::Song::defaultMixer (2);
  5760. for (int i = 0; i < 2; ++i)
  5761. {
  5762. lynx::ChannelDef c;
  5763. c.instrument = lynx::InstrumentKind::sampler;
  5764. c.samplePath = "dc";
  5765. c.volume = 1.0f;
  5766. c.mixerTrack = i + 1;
  5767. s.channels.push_back (c);
  5768. }
  5769. s.channels[0].cutBy = 1; // open
  5770. s.channels[1].cutGroup = 1; // closed
  5771. auto& p = s.patterns.emplace_back();
  5772. p.notes = { { 0, 100, 0, 60 }, { 960, 100, 1, 60 } };
  5773. auto provider = [] (const std::string&) {
  5774. auto d = std::make_shared<lynx::SampleData>();
  5775. d->left.assign (96000, 0.5f);
  5776. return std::shared_ptr<const lynx::SampleData> (d);
  5777. };
  5778. auto state = lynx::RackState::build (s, nullptr, 48000.0, provider);
  5779. CHECK (state->hasCutGroups && state->cutTargets[1].size() == 1 && state->cutTargets[1][0] == 0);
  5780. lynx::RackRenderer r;
  5781. r.prepare (48000.0, 512);
  5782. std::vector<float> l (512), rr (512);
  5783. const auto tps = lynx::ticksPerSample (s.bpm, 48000.0);
  5784. float afterCut = 0.0f;
  5785. for (double tick = 0.0; tick < 960.0 * 3; tick += 512 * tps)
  5786. {
  5787. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  5788. if (tick > 960.0 * 2) // the closed one continues; the open one has stopped
  5789. afterCut = std::max (afterCut, std::abs (l[256]));
  5790. }
  5791. CHECK (std::abs (afterCut - 0.5f * 0.78f) < 0.01f); // only the closed one (default velocity 0.78)
  5792. // resampling stretch: 1 s of sample stretched to 4 beats at 120 BPM = 2 s
  5793. auto ramp = std::make_shared<lynx::SampleData>();
  5794. for (int i = 0; i < 48000; ++i)
  5795. ramp->left.push_back ((float) i / 48000.0f);
  5796. ramp->sampleRate = 48000.0;
  5797. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  5798. inst->prepare (48000.0);
  5799. lynx::ChannelDef c;
  5800. c.sampler.stretchBeats = 4.0f;
  5801. inst->configure (c, 120.0);
  5802. inst->noteOn (60, 1.0f);
  5803. std::vector<float> a (48000), b (48000);
  5804. inst->render (a.data(), b.data(), 48000, 1.0f, 1.0f);
  5805. CHECK (std::abs (a[47999] - 0.5f) < 0.01f); // after 1 s it has read half
  5806. // FL's Resample: the key still transposes, like tape (round 162, issue #9): an octave up reads twice as fast
  5807. inst->allNotesOff();
  5808. std::fill (a.begin(), a.end(), 0.0f);
  5809. inst->render (a.data(), b.data(), 4800, 1.0f, 1.0f);
  5810. std::fill (a.begin(), a.end(), 0.0f);
  5811. inst->noteOn (72, 1.0f);
  5812. inst->render (a.data(), b.data(), 24000, 1.0f, 1.0f);
  5813. CHECK (std::abs (a[23999] - 0.5f) < 0.01f);
  5814. // FL envelope time: 32950 = 2 s; < 101 = 0
  5815. CHECK (std::abs (flp::Channel::envelopeTime (32950) - 2.0) < 0.01 && flp::Channel::envelopeTime (50) == 0.0);
  5816. }
  5817. TEST (time_stretch_keeps_pitch)
  5818. {
  5819. lynx::SampleData sine;
  5820. sine.sampleRate = 48000.0;
  5821. for (int i = 0; i < 48000; ++i)
  5822. sine.left.push_back (0.5f * std::sin (2.0f * 3.14159265f * 440.0f * (float) i / 48000.0f));
  5823. auto crossingsPerSecond = [] (const lynx::SampleData& d) {
  5824. int c = 0;
  5825. const auto n = (int) d.frames();
  5826. for (int i = 4800; i < n - 4800; ++i)
  5827. c += (d.left[(std::size_t) i - 1] < 0.0f) != (d.left[(std::size_t) i] < 0.0f);
  5828. return (double) c / ((double) (n - 9600) / d.sampleRate);
  5829. };
  5830. for (double ratio : { 2.0, 0.5, 1.37 })
  5831. {
  5832. const auto out = lynx::timeStretch (sine, ratio);
  5833. CHECK (std::abs ((double) out->frames() - 48000.0 * ratio) < 2.0);
  5834. CHECK (std::abs (crossingsPerSecond (*out) - 880.0) < 15.0); // 440 Hz stays 440 Hz
  5835. float peak = 0.0f;
  5836. for (std::size_t i = 4800; i + 4800 < out->frames(); ++i)
  5837. peak = std::max (peak, std::abs (out->left[i]));
  5838. CHECK (peak > 0.4f && peak < 0.6f); // no gaps or clipping in the overlap
  5839. }
  5840. }
  5841. TEST (sampler_region_start_and_end)
  5842. {
  5843. auto ramp = std::make_shared<lynx::SampleData>();
  5844. for (int i = 0; i < 10000; ++i)
  5845. ramp->left.push_back ((float) i / 10000.0f);
  5846. ramp->sampleRate = 48000.0;
  5847. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  5848. inst->prepare (48000.0);
  5849. lynx::ChannelDef c;
  5850. c.sampler.regionStart = 0.25f;
  5851. c.sampler.regionEnd = 0.5f;
  5852. inst->configure (c, 120.0);
  5853. inst->noteOn (60, 1.0f);
  5854. std::vector<float> l (6000), r (6000);
  5855. inst->render (l.data(), r.data(), 6000, 1.0f, 1.0f);
  5856. CHECK (std::abs (l[0] - 0.25f) < 0.001f); // starts at the beginning of the slice
  5857. CHECK (l[2400] > 0.48f && l[2600] == 0.0f); // and stops at its end
  5858. }
  5859. TEST (arpeggiator_plays_held_chord_in_steps)
  5860. {
  5861. auto song = lynx::Song::makeDefault();
  5862. song.bpm = 120.0;
  5863. song.patterns[0].notes.clear();
  5864. for (int key : { 60, 64, 67 })
  5865. song.patterns[0].notes.push_back ({ 0, lynx::ppq * 4, 4, key, 0.8f }); // chord held for 1 bar
  5866. song.channels[4].arp.direction = 1; // up
  5867. song.channels[4].arp.stepBeats = 0.25f;
  5868. auto state = lynx::RackState::build (song, nullptr, 48000.0);
  5869. CHECK (state->arpeggiators[4] != nullptr && state->arpeggiators[0] == nullptr);
  5870. // the arpeggiator generates the notes; we count what it hands to the instrument
  5871. auto& arp = *state->arpeggiators[4];
  5872. const auto tps = lynx::ticksPerSample (song.bpm, 48000.0);
  5873. std::vector<int> keys;
  5874. lynx::EventBuffer ev;
  5875. for (double tick = 0.0; tick < lynx::ppq * 2.0; tick += 512 * tps)
  5876. {
  5877. ev.clear();
  5878. lynx::schedulePatternLoop (song.patterns[0], song.patterns[0].effectiveLength (4), tick, 512, tps, ev);
  5879. int in = 0, out = 0;
  5880. for (auto& e : ev)
  5881. if (e.channel == 4)
  5882. arp.input[(std::size_t) in++] = e;
  5883. arp.process (4, arp.input.data(), in, tick, tps, 512, song.channels[4].arp, arp.output.data(), out);
  5884. for (int i = 0; i < out; ++i)
  5885. if (arp.output[(std::size_t) i].type == lynx::NoteEvent::Type::on)
  5886. keys.push_back (arp.output[(std::size_t) i].key);
  5887. }
  5888. // 2 beats = 8 sixteenth steps: 60 64 67 60 64 67 60 64
  5889. CHECK (keys.size() >= 8); // the last block goes slightly past 2 beats
  5890. const int expected[] = { 60, 64, 67, 60, 64, 67, 60, 64 };
  5891. for (std::size_t i = 0; i < keys.size() && i < 8; ++i)
  5892. CHECK (keys[i] == expected[i]);
  5893. // actually rendering also makes sound
  5894. lynx::RackRenderer r;
  5895. r.prepare (48000.0, 512);
  5896. std::vector<float> l (512), rr (512);
  5897. float peak = 0.0f;
  5898. for (double tick = 0.0; tick < lynx::ppq; tick += 512 * tps)
  5899. {
  5900. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  5901. for (auto v : l)
  5902. peak = std::max (peak, std::abs (v));
  5903. }
  5904. CHECK (peak > 0.01f);
  5905. }
  5906. TEST (audio_clip_fades_and_gain)
  5907. {
  5908. lynx::Song s;
  5909. s.bpm = 120.0;
  5910. s.mode = lynx::PlayMode::song;
  5911. s.mixer = lynx::Song::defaultMixer (1);
  5912. lynx::ChannelDef c;
  5913. c.instrument = lynx::InstrumentKind::audioClip;
  5914. c.samplePath = "dc";
  5915. c.volume = 1.0f;
  5916. c.mixerTrack = 1;
  5917. s.channels.push_back (c);
  5918. s.patterns.emplace_back();
  5919. lynx::Clip clip;
  5920. clip.kind = lynx::ClipKind::audio;
  5921. clip.channel = 0;
  5922. clip.length = lynx::ppq * 4; // 2 s at 120 BPM
  5923. clip.fadeIn = lynx::ppq; // 0.5 s
  5924. clip.fadeOut = lynx::ppq;
  5925. clip.gain = 0.5f;
  5926. s.playlist.push_back (clip);
  5927. auto provider = [] (const std::string&) {
  5928. auto d = std::make_shared<lynx::SampleData>();
  5929. d->left.assign (48000 * 4, 1.0f);
  5930. return std::shared_ptr<const lynx::SampleData> (d);
  5931. };
  5932. auto state = lynx::RackState::build (s, nullptr, 48000.0, provider);
  5933. lynx::RackRenderer r;
  5934. r.prepare (48000.0, 512);
  5935. std::vector<float> all;
  5936. std::vector<float> l (512), rr (512);
  5937. const auto tps = lynx::ticksPerSample (s.bpm, 48000.0);
  5938. for (double tick = 0.0; tick < lynx::ppq * 4.0; tick += 512 * tps)
  5939. {
  5940. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  5941. all.insert (all.end(), l.begin(), l.end());
  5942. }
  5943. // FL plays a clip at unity at the default channel volume (78%): at 100% it is that much louder (REA_rodada89 §4.2)
  5944. const auto unity = 1.0f / lynx::channelGain (10000.0f / 12800.0f);
  5945. CHECK (all[100] < 0.01f); // fade-in start
  5946. CHECK (std::abs (all[12000] - 0.25f * unity) < 0.02f); // fade-in midpoint: half of 0.5
  5947. CHECK (std::abs (all[48000] - 0.5f * unity) < 0.01f); // body: the clip gain
  5948. CHECK (std::abs (all[96000 - 12000] - 0.25f * unity) < 0.02f); // fade-out midpoint
  5949. }
  5950. TEST (audio_clip_unity_at_fl_default_volume)
  5951. {
  5952. // a Playlist audio clip at FL's default channel volume (10000/12800) and centre pan plays its sample unchanged
  5953. auto dc = std::make_shared<lynx::SampleData>();
  5954. dc->left.assign (48000, 0.5f);
  5955. auto clip = lynx::makeInstrument (lynx::InstrumentKind::audioClip, dc);
  5956. clip->prepare (48000.0);
  5957. lynx::ChannelDef c;
  5958. c.instrument = lynx::InstrumentKind::audioClip;
  5959. c.volume = 10000.0f / 12800.0f;
  5960. clip->configure (c, 120.0);
  5961. clip->noteOn (60, 1.0f);
  5962. std::vector<float> l (4800, 0.0f), r (4800, 0.0f);
  5963. const auto g = lynx::channelGain (c.volume);
  5964. clip->render (l.data(), r.data(), 4800, g, g);
  5965. CHECK (std::abs (l[2400] - 0.5f) < 1.0e-3f && std::abs (r[2400] - 0.5f) < 1.0e-3f);
  5966. // a sampler channel (pattern notes) keeps the channel volume curve as it was
  5967. auto sampler = lynx::makeInstrument (lynx::InstrumentKind::sampler, dc);
  5968. sampler->prepare (48000.0);
  5969. c.instrument = lynx::InstrumentKind::sampler;
  5970. sampler->configure (c, 120.0);
  5971. sampler->noteOn (60, 1.0f);
  5972. std::fill (l.begin(), l.end(), 0.0f);
  5973. std::fill (r.begin(), r.end(), 0.0f);
  5974. sampler->render (l.data(), r.data(), 4800, g, g);
  5975. CHECK (std::abs (l[2400] - 0.5f * g) < 0.02f);
  5976. }
  5977. TEST (slicer_detects_hits_and_plays_slices_by_key)
  5978. {
  5979. // 4 hits (decaying noise) every 0.25 s
  5980. lynx::SampleData loop;
  5981. loop.sampleRate = 48000.0;
  5982. loop.left.assign (48000, 0.0f);
  5983. lynx::Noise noise;
  5984. for (int hit = 0; hit < 4; ++hit)
  5985. for (int i = 0; i < 6000; ++i)
  5986. loop.left[(std::size_t) (hit * 12000 + i)] = noise.next() * 0.8f * std::exp (-(float) i / 1500.0f);
  5987. const auto slices = lynx::detectSlices (loop, 0.5f);
  5988. CHECK (slices.size() == 4);
  5989. for (std::size_t i = 0; i < slices.size(); ++i)
  5990. CHECK (std::abs (slices[i] - 0.25f * (float) i) < 0.01f);
  5991. // key 61 plays the second slice, which starts at 0.25 and ends before 0.5
  5992. auto ramp = std::make_shared<lynx::SampleData>();
  5993. for (int i = 0; i < 48000; ++i)
  5994. ramp->left.push_back ((float) i / 48000.0f);
  5995. ramp->sampleRate = 48000.0;
  5996. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  5997. inst->prepare (48000.0);
  5998. lynx::ChannelDef c;
  5999. c.sampler.slices = slices;
  6000. inst->configure (c, 120.0);
  6001. inst->noteOn (61, 1.0f);
  6002. std::vector<float> l (20000), r (20000);
  6003. inst->render (l.data(), r.data(), 20000, 1.0f, 1.0f);
  6004. CHECK (std::abs (l[0] - 0.25f) < 0.01f && l[11000] > 0.45f && l[13000] == 0.0f);
  6005. }
  6006. TEST (project_templates_render_sound)
  6007. {
  6008. const auto names = lynx::templateNames();
  6009. CHECK (names.size() == 4);
  6010. for (int t = 1; t < (int) names.size(); ++t)
  6011. {
  6012. auto song = lynx::makeTemplate (t);
  6013. auto state = lynx::RackState::build (song, nullptr, 48000.0);
  6014. lynx::RackRenderer r;
  6015. r.prepare (48000.0, 512);
  6016. std::vector<float> l (512), rr (512);
  6017. const auto tps = lynx::ticksPerSample (song.bpm, 48000.0);
  6018. float peak = 0.0f;
  6019. for (double tick = 0.0; tick < lynx::ppq * 4.0; tick += 512 * tps)
  6020. {
  6021. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  6022. for (auto v : l)
  6023. peak = std::max (peak, std::abs (v));
  6024. }
  6025. CHECK (peak > 0.05f && peak < 4.0f);
  6026. }
  6027. CHECK (lynx::makeTemplate (0).patterns[0].notes.empty());
  6028. }
  6029. // --- interface translations (resources/i18n) -------------------------------------------------------
  6030. namespace i18ntest
  6031. {
  6032. std::string readFile (const std::filesystem::path& path)
  6033. {
  6034. std::ifstream in (path, std::ios::binary);
  6035. return { std::istreambuf_iterator<char> (in), std::istreambuf_iterator<char>() };
  6036. }
  6037. std::string unescape (const std::string& s)
  6038. {
  6039. std::string out;
  6040. for (std::size_t i = 0; i < s.size(); ++i)
  6041. {
  6042. if (s[i] == '\\' && i + 1 < s.size())
  6043. {
  6044. const auto n = s[++i];
  6045. out += n == 'n' ? '\n' : n == 't' ? '\t' : n;
  6046. }
  6047. else
  6048. out += s[i];
  6049. }
  6050. return out;
  6051. }
  6052. // reads a quoted string starting at s[pos] == '"'; returns the raw (escaped) contents and moves pos past the closing quote
  6053. bool quoted (const std::string& s, std::size_t& pos, std::string& raw)
  6054. {
  6055. if (pos >= s.size() || s[pos] != '"')
  6056. return false;
  6057. raw.clear();
  6058. for (++pos; pos < s.size() && s[pos] != '"' && s[pos] != '\n'; ++pos)
  6059. {
  6060. if (s[pos] == '\\' && pos + 1 < s.size())
  6061. raw += s[pos++];
  6062. raw += s[pos];
  6063. }
  6064. if (pos >= s.size() || s[pos] != '"')
  6065. return false;
  6066. ++pos;
  6067. return true;
  6068. }
  6069. // JUCE LocalisedStrings format: "english" = "translation"; duplicates are reported in `duplicates`
  6070. std::map<std::string, std::string> parseTable (const std::string& text, std::vector<std::string>& duplicates)
  6071. {
  6072. std::map<std::string, std::string> table;
  6073. std::istringstream lines (text);
  6074. std::string line;
  6075. while (std::getline (lines, line))
  6076. {
  6077. std::size_t pos = line.find_first_not_of (" \t");
  6078. std::string key, value;
  6079. if (pos == std::string::npos || ! quoted (line, pos, key))
  6080. continue;
  6081. pos = line.find ('"', pos);
  6082. if (pos == std::string::npos || ! quoted (line, pos, value))
  6083. value.clear();
  6084. const auto k = unescape (key);
  6085. if (table.count (k) != 0)
  6086. duplicates.push_back (k);
  6087. table[k] = unescape (value);
  6088. }
  6089. return table;
  6090. }
  6091. // string literals passed to tr() / trc() / TR_NOOP() in the sources (adjacent literals are concatenated)
  6092. std::map<std::string, std::string> sourceKeys (const std::filesystem::path& srcDir)
  6093. {
  6094. std::map<std::string, std::string> keys;
  6095. for (const auto& entry : std::filesystem::recursive_directory_iterator (srcDir))
  6096. {
  6097. const auto ext = entry.path().extension().string();
  6098. if (! entry.is_regular_file() || (ext != ".cpp" && ext != ".h" && ext != ".inc"))
  6099. continue;
  6100. // drop // comment lines (they may quote tr ("...") as an example)
  6101. std::string text, line;
  6102. std::istringstream in (readFile (entry.path()));
  6103. while (std::getline (in, line))
  6104. {
  6105. const auto first = line.find_first_not_of (" \t");
  6106. text += (first != std::string::npos && line.compare (first, 2, "//") == 0) ? std::string() : line;
  6107. text += '\n';
  6108. }
  6109. auto isIdent = [] (char c) { return std::isalnum ((unsigned char) c) || c == '_'; };
  6110. for (const std::string name : { "tr", "trc", "TR_NOOP" })
  6111. for (auto at = text.find (name); at != std::string::npos; at = text.find (name, at + 1))
  6112. {
  6113. if ((at > 0 && isIdent (text[at - 1])) || isIdent (text[at + name.size()]))
  6114. continue;
  6115. auto pos = text.find_first_not_of (" \t\n", at + name.size());
  6116. if (pos == std::string::npos || text[pos] != '(')
  6117. continue;
  6118. pos = text.find_first_not_of (" \t\n", pos + 1);
  6119. std::string key, raw;
  6120. bool any = false;
  6121. while (pos != std::string::npos && quoted (text, pos, raw))
  6122. {
  6123. key += raw;
  6124. any = true;
  6125. pos = text.find_first_not_of (" \t\n", pos);
  6126. }
  6127. if (any)
  6128. keys.emplace (unescape (key), entry.path().filename().string());
  6129. }
  6130. }
  6131. return keys;
  6132. }
  6133. std::set<std::string> placeholders (const std::string& s)
  6134. {
  6135. std::set<std::string> found;
  6136. for (auto open = s.find ('{'); open != std::string::npos; open = s.find ('{', open + 1))
  6137. if (const auto close = s.find ('}', open); close != std::string::npos && close - open <= 3)
  6138. found.insert (s.substr (open, close - open + 1));
  6139. return found;
  6140. }
  6141. } // namespace i18ntest
  6142. TEST (translations_cover_every_ui_string)
  6143. {
  6144. using namespace i18ntest;
  6145. const auto text = readFile ("resources/i18n/pt-BR.txt");
  6146. CHECK (! text.empty());
  6147. std::vector<std::string> duplicates;
  6148. const auto table = parseTable (text, duplicates);
  6149. CHECK (table.size() > 300);
  6150. CHECK (duplicates.empty());
  6151. for (const auto& d : duplicates)
  6152. std::printf (" duplicate pt-BR entry: \"%s\"\n", d.c_str());
  6153. const auto keys = sourceKeys ("src");
  6154. CHECK (keys.size() > 300);
  6155. int missing = 0;
  6156. for (const auto& [key, file] : keys)
  6157. if (auto it = table.find (key); it == table.end() || it->second.empty())
  6158. {
  6159. ++missing;
  6160. std::printf (" no pt-BR translation for \"%s\" (%s)\n", key.c_str(), file.c_str());
  6161. }
  6162. CHECK (missing == 0);
  6163. // no empty values, and every {n} placeholder survives the translation
  6164. int empty = 0, badPlaceholders = 0;
  6165. for (const auto& [english, portuguese] : table)
  6166. {
  6167. empty += portuguese.empty() ? 1 : 0;
  6168. if (placeholders (english) != placeholders (portuguese))
  6169. {
  6170. ++badPlaceholders;
  6171. std::printf (" placeholders differ: \"%s\"\n", english.c_str());
  6172. }
  6173. }
  6174. CHECK (empty == 0);
  6175. CHECK (badPlaceholders == 0);
  6176. }
  6177. TEST (translations_embedded_table_is_up_to_date)
  6178. {
  6179. // src/ui/i18n/<id>.h + I18nData.h must be regenerated (tools/make_i18n.py) whenever a resources/i18n/*.txt changes
  6180. int files = 0;
  6181. for (const auto& entry : std::filesystem::directory_iterator ("resources/i18n"))
  6182. if (entry.path().extension() == ".txt")
  6183. {
  6184. ++files;
  6185. const auto id = entry.path().stem().string();
  6186. const auto text = i18ntest::readFile (entry.path());
  6187. const lynx::i18n::data::Table* found = nullptr;
  6188. for (const auto& t : lynx::i18n::data::tables)
  6189. if (id == t.id)
  6190. found = &t;
  6191. CHECK (found != nullptr);
  6192. if (found == nullptr)
  6193. {
  6194. std::printf (" %s.txt is not embedded: run python3 tools/make_i18n.py\n", id.c_str());
  6195. continue;
  6196. }
  6197. const auto unpacked = lynx::inflateZlib (found->zlib, found->zlibSize, false, found->textSize + 16);
  6198. const bool same = unpacked && unpacked->size() == text.size() && found->textSize == text.size()
  6199. && std::memcmp (unpacked->data(), text.data(), text.size()) == 0;
  6200. CHECK (same);
  6201. if (! same)
  6202. std::printf (" %s.txt changed since the last python3 tools/make_i18n.py\n", id.c_str());
  6203. }
  6204. CHECK (files == (int) std::size (lynx::i18n::data::tables));
  6205. }
  6206. TEST (translations_every_language_covers_the_reference_table)
  6207. {
  6208. // every key of pt-BR.txt (the reference table) exists, translated, in every other language; keys just added to
  6209. // pt-BR.txt may wait in resources/i18n/pending.keys ("English" lines) until the other tables catch up
  6210. using namespace i18ntest;
  6211. std::vector<std::string> unused;
  6212. const auto reference = parseTable (readFile ("resources/i18n/pt-BR.txt"), unused);
  6213. std::set<std::string> pending;
  6214. {
  6215. std::istringstream in (readFile ("resources/i18n/pending.keys"));
  6216. std::string line, raw;
  6217. while (std::getline (in, line))
  6218. if (std::size_t pos = line.find_first_not_of (" \t"); pos != std::string::npos && quoted (line, pos, raw))
  6219. pending.insert (unescape (raw));
  6220. }
  6221. auto count = [] (const std::string& s, const std::string& what) {
  6222. int n = 0;
  6223. for (auto at = s.find (what); at != std::string::npos; at = s.find (what, at + what.size()))
  6224. ++n;
  6225. return n;
  6226. };
  6227. int languages = 0;
  6228. for (const auto& entry : std::filesystem::directory_iterator ("resources/i18n"))
  6229. {
  6230. const auto id = entry.path().stem().string();
  6231. if (entry.path().extension() != ".txt" || id == "pt-BR")
  6232. continue;
  6233. ++languages;
  6234. const auto text = readFile (entry.path());
  6235. CHECK (text.rfind ("language: ", 0) == 0); // JUCE LocalisedStrings header
  6236. std::vector<std::string> duplicates;
  6237. const auto table = parseTable (text, duplicates);
  6238. CHECK (duplicates.empty());
  6239. for (const auto& d : duplicates)
  6240. std::printf (" duplicate %s entry: \"%s\"\n", id.c_str(), d.c_str());
  6241. int missing = 0, bad = 0;
  6242. for (const auto& [english, unusedValue] : reference)
  6243. {
  6244. const auto it = table.find (english);
  6245. if ((it == table.end() || it->second.empty()) && pending.count (english) == 0)
  6246. {
  6247. if (++missing <= 10)
  6248. std::printf (" no %s translation for \"%s\" (python3 tools/i18n_tool.py status)\n", id.c_str(), english.c_str());
  6249. }
  6250. }
  6251. for (const auto& [english, translated] : table)
  6252. {
  6253. std::string why;
  6254. if (translated.empty())
  6255. why = "empty";
  6256. else if (placeholders (english) != placeholders (translated))
  6257. why = "placeholders differ";
  6258. else if (count (english, "\n") != count (translated, "\n"))
  6259. why = "line breaks differ";
  6260. else if (count (english, "%") != count (translated, "%"))
  6261. why = "% signs differ";
  6262. if (! why.empty() && ++bad <= 10)
  6263. std::printf (" %s: %s: \"%s\"\n", id.c_str(), why.c_str(), english.c_str());
  6264. }
  6265. CHECK (missing == 0);
  6266. CHECK (bad == 0);
  6267. if (missing > 10 || bad > 10)
  6268. std::printf (" %s: %d missing, %d bad in all\n", id.c_str(), missing, bad);
  6269. }
  6270. CHECK (languages >= 0);
  6271. }
  6272. TEST (translations_os_locale_picks_the_interface_language)
  6273. {
  6274. const std::vector<std::string> ids { "en", "pt-BR", "de", "es", "ja", "zh-Hans", "zh-Hant", "id", "ko" };
  6275. using lynx::i18n::languageForLocale;
  6276. CHECK (languageForLocale ("de-AT", ids) == "de");
  6277. CHECK (languageForLocale ("de_DE.UTF-8", ids) == "de");
  6278. CHECK (languageForLocale ("pt-BR", ids) == "pt-BR");
  6279. CHECK (languageForLocale ("pt_PT", ids) == "pt-BR");
  6280. CHECK (languageForLocale ("zh-Hans-CN", ids) == "zh-Hans");
  6281. CHECK (languageForLocale ("zh-CN", ids) == "zh-Hans");
  6282. CHECK (languageForLocale ("zh_TW.UTF-8", ids) == "zh-Hant");
  6283. CHECK (languageForLocale ("zh-Hant-HK", ids) == "zh-Hant");
  6284. CHECK (languageForLocale ("zh-HK", ids) == "zh-Hant");
  6285. CHECK (languageForLocale ("ja_JP.UTF-8@euro", ids) == "ja");
  6286. CHECK (languageForLocale ("in_ID", ids) == "id");
  6287. CHECK (languageForLocale ("es-419", ids) == "es");
  6288. CHECK (languageForLocale ("en-US", ids) == "en");
  6289. CHECK (languageForLocale ("ko:en", ids) == "ko");
  6290. CHECK (languageForLocale ("fr-FR", ids).empty()); // not in the list
  6291. CHECK (languageForLocale ("C", ids).empty());
  6292. CHECK (languageForLocale ("", ids).empty());
  6293. }
  6294. // --- workflow shell: pattern/channel operations, key map, workspace layout, control math -------------
  6295. TEST (workflow_pattern_navigation_and_insert)
  6296. {
  6297. auto song = lynx::Song::makeDefault();
  6298. song.patterns.resize (3);
  6299. song.patterns[1].name = "B";
  6300. song.patterns[2].name = "C";
  6301. song.patterns[2].notes.push_back ({});
  6302. song.currentPattern = 0;
  6303. CHECK (lynx::stepPattern (song, 1) == 1 && lynx::stepPattern (song, -1) == 0 && lynx::stepPattern (song, 10) == 2);
  6304. // pattern 0 has the default notes, 1 is empty, 2 has a note
  6305. CHECK (! lynx::isPatternEmpty (song.patterns[0]) && lynx::isPatternEmpty (song.patterns[1]));
  6306. CHECK (lynx::findNextEmptyPattern (song, 0) == 1 && lynx::findNextEmptyPattern (song, 1) == -1);
  6307. CHECK (lynx::gotoNextEmptyPattern (song) == 1 && song.currentPattern == 1);
  6308. song.patterns[1].notes.push_back ({});
  6309. const auto before = song.patterns.size();
  6310. CHECK (lynx::gotoNextEmptyPattern (song) == (int) before && song.patterns.size() == before + 1);
  6311. CHECK (song.patterns.back().name == "Pattern 1" || song.patterns.back().name.rfind ("Pattern ", 0) == 0);
  6312. // insert keeps Playlist clips on their patterns
  6313. lynx::Clip clip;
  6314. clip.pattern = 2;
  6315. song.playlist = { clip };
  6316. song.arrangements.push_back ({ "Other", { clip }, 0, 0, {} });
  6317. CHECK (lynx::insertPattern (song, 1, "New") == 1 && song.currentPattern == 1);
  6318. CHECK (song.patterns[1].name == "New" && song.patterns[1].notes.empty() && song.patterns[3].name == "C");
  6319. CHECK (song.playlist[0].pattern == 3 && song.arrangements[1].clips[0].pattern == 3);
  6320. }
  6321. TEST (workflow_split_pattern_by_channel)
  6322. {
  6323. auto song = lynx::Song::makeDefault();
  6324. song.patterns.resize (1);
  6325. auto& p = song.patterns[0];
  6326. p.name = "Beat";
  6327. p.notes.clear();
  6328. for (int i = 0; i < 4; ++i)
  6329. p.notes.push_back ({ (std::int64_t) i * lynx::ticksPerStep, lynx::ticksPerStep, i % 2 == 0 ? 0 : 2, 60 });
  6330. song.channels[2].colour = 0xff112233;
  6331. lynx::Clip clip;
  6332. clip.pattern = 0;
  6333. song.playlist = { clip };
  6334. const auto created = lynx::splitPatternByChannel (song, 0);
  6335. CHECK (created == 2 && song.patterns.size() == 3);
  6336. CHECK (song.patterns[0].notes.size() == 4); // the original is unchanged
  6337. CHECK (song.patterns[1].name == "Beat - " + song.channels[0].name && song.patterns[1].notes.size() == 2);
  6338. CHECK (song.patterns[2].colour == 0xff112233u && song.patterns[2].notes.size() == 2 && song.patterns[2].notes[0].channel == 2);
  6339. CHECK (song.playlist[0].pattern == 0);
  6340. CHECK (lynx::splitPatternByChannel (song, 99) == 0);
  6341. }
  6342. TEST (workflow_channel_selection)
  6343. {
  6344. lynx::ChannelSelection s;
  6345. s.selectOnly (2);
  6346. s.toggle (5);
  6347. s.toggle (0);
  6348. CHECK ((s.rows == std::vector<int> { 0, 2, 5 }) && s.contains (5) && ! s.contains (3));
  6349. s.toggle (2);
  6350. CHECK ((s.rows == std::vector<int> { 0, 5 }));
  6351. s.selectOnly (3);
  6352. s.extendTo (6);
  6353. CHECK ((s.rows == std::vector<int> { 3, 4, 5, 6 }));
  6354. s.extendTo (1);
  6355. CHECK ((s.rows == std::vector<int> { 1, 2, 3 }));
  6356. s.selectOnly (4);
  6357. s.toggle (4); // the last one stays
  6358. CHECK ((s.rows == std::vector<int> { 4 }));
  6359. s.selectAll (3);
  6360. CHECK ((s.rows == std::vector<int> { 0, 1, 2 }));
  6361. s.rows = { 7, 9 };
  6362. s.clamp (5);
  6363. CHECK (s.rows.size() == 1 && s.rows[0] < 5);
  6364. }
  6365. TEST (workflow_move_clone_insert_channels)
  6366. {
  6367. auto song = lynx::Song::makeDefault();
  6368. const auto names = [&] {
  6369. std::vector<std::string> n;
  6370. for (const auto& c : song.channels)
  6371. n.push_back (c.name);
  6372. return n;
  6373. };
  6374. const auto original = names();
  6375. const auto count = (int) song.channels.size();
  6376. CHECK (count >= 4);
  6377. // references that must follow their channel
  6378. song.patterns[0].notes.push_back ({ 0, 240, 1, 64 });
  6379. lynx::Automation a;
  6380. a.target = { lynx::AutomationTargetKind::channelVolume, 1 };
  6381. song.automations.push_back (a);
  6382. song.midiLinks.push_back ({ 7, -1, { lynx::AutomationTargetKind::channelPan, 3 } });
  6383. lynx::Clip audio;
  6384. audio.kind = lynx::ClipKind::audio;
  6385. audio.channel = 3;
  6386. song.playlist.push_back (audio);
  6387. std::vector<int> selection { 1, 3 };
  6388. CHECK (lynx::moveChannels (song, selection, -1));
  6389. CHECK ((selection == std::vector<int> { 0, 2 }));
  6390. CHECK (song.channels[0].name == original[1] && song.channels[1].name == original[0] && song.channels[2].name == original[3]
  6391. && song.channels[3].name == original[2]);
  6392. CHECK (song.automations[0].target.index == 0 && song.midiLinks[0].target.index == 2 && song.playlist.back().channel == 2);
  6393. CHECK (std::any_of (song.patterns[0].notes.begin(), song.patterns[0].notes.end(),
  6394. [] (const lynx::Note& n) { return n.channel == 0 && n.key == 64; }));
  6395. CHECK (! lynx::moveChannels (song, selection, -1)); // the block is at the top
  6396. CHECK (lynx::moveChannels (song, selection, 1) && (selection == std::vector<int> { 1, 3 }) && names() == original);
  6397. // clone: settings, not notes, right below, with a new name
  6398. song.channels[1].volume = 0.33f;
  6399. const auto notesBefore = song.patterns[0].notes.size();
  6400. const auto clone = lynx::cloneChannel (song, 1);
  6401. CHECK (clone == 2 && (int) song.channels.size() == count + 1);
  6402. CHECK (song.channels[2].name == original[1] + " #2" && song.channels[2].volume == 0.33f);
  6403. CHECK (song.patterns[0].notes.size() == notesBefore);
  6404. CHECK (song.channels[3].name == original[2] && song.automations[0].target.index == 1);
  6405. CHECK (lynx::cloneChannel (song, 1) == 2 && song.channels[2].name == original[1] + " #3");
  6406. // remove several
  6407. CHECK (lynx::removeChannels (song, { 2, 3, 2 }) == 2 && (int) song.channels.size() == count);
  6408. CHECK (names() == original);
  6409. }
  6410. TEST (workflow_send_channels_to_free_inserts)
  6411. {
  6412. auto song = lynx::Song::makeDefault();
  6413. for (auto& c : song.channels)
  6414. c.mixerTrack = 0;
  6415. song.mixer[1].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::delay)); // not free: has effects
  6416. song.channels[0].mixerTrack = 2; // not free: used
  6417. song.setRoute (5, 3, true); // 3 is not free: receives a send
  6418. CHECK (! lynx::isInsertFree (song, 1) && ! lynx::isInsertFree (song, 2) && ! lynx::isInsertFree (song, 3) && lynx::isInsertFree (song, 4));
  6419. song.channels[1].colour = 0xff445566;
  6420. const auto tracks = lynx::sendChannelsToFreeInserts (song, { 1, 2 });
  6421. CHECK ((tracks == std::vector<int> { 4, 5 }) || (tracks == std::vector<int> { 4, 6 }));
  6422. CHECK (song.channels[1].mixerTrack == 4 && song.mixer[4].name == song.channels[1].name && song.mixer[4].colour == 0xff445566u);
  6423. CHECK (song.mixer[(std::size_t) tracks[1]].name == song.channels[2].name);
  6424. // with no free insert left, a new one is added at the end
  6425. auto full = lynx::Song::makeDefault();
  6426. full.mixer = lynx::Song::defaultMixer (1);
  6427. full.channels[0].mixerTrack = 1;
  6428. const auto added = lynx::sendChannelsToFreeInserts (full, { 1 });
  6429. CHECK (added.size() == 1 && added[0] == 2 && full.mixer.size() == 3 && full.channels[1].mixerTrack == 2);
  6430. }
  6431. TEST (workflow_add_channel_routed)
  6432. {
  6433. auto song = lynx::Song::makeDefault();
  6434. const auto count = (int) song.channels.size();
  6435. const auto inserts = song.mixer.size();
  6436. lynx::ChannelDef c;
  6437. c.mixerTrack = 7; // what the generator came with doesn't count
  6438. const auto last = (int) song.mixer.size() - 1;
  6439. CHECK (lynx::addChannelRouted (song, c, last) == count && song.channels.back().mixerTrack == last);
  6440. CHECK (lynx::addChannelRouted (song, c, 0) == count + 1 && song.channels.back().mixerTrack == 0); // Master
  6441. CHECK (song.mixer.size() == inserts); // the drop's insert is used as it is, nothing is claimed
  6442. // an insert that doesn't exist claims a free one, as a plain new channel does
  6443. auto expected = song;
  6444. const auto free = expected.claimFreeInsert();
  6445. CHECK (lynx::addChannelRouted (song, c, 9999) == count + 2 && song.channels.back().mixerTrack == free);
  6446. auto plain = expected;
  6447. CHECK (lynx::addChannelRouted (plain, c) == count + 2 && plain.channels.back().mixerTrack == expected.claimFreeInsert());
  6448. }
  6449. TEST (pr28_generator_dropped_on_an_insert_names_it_like_fl)
  6450. {
  6451. // FL 2026 (round 167u, shots fl3-fl10): 3x Osc dropped on Insert 3, Master and Insert 5's slot 2, then FLEX on
  6452. // Insert 3: channels "3x Osc", "3x Osc #2", "3x Osc #3", "FLEX"; Insert 3 / Master / Insert 5 take the first
  6453. // three names, Insert 3 keeps "3x Osc" when FLEX comes
  6454. auto song = lynx::Song::makeDefault();
  6455. song.channels.clear();
  6456. while (song.mixer.size() < 6)
  6457. song.mixer.emplace_back().name = "Insert " + std::to_string (song.mixer.size());
  6458. song.mixer[4].name = "Drums"; // a name the user gave
  6459. lynx::ChannelDef osc;
  6460. osc.name = "3x Osc";
  6461. lynx::ChannelDef flex;
  6462. flex.name = "FLEX";
  6463. CHECK (lynx::addChannelRouted (song, osc, 3) == 0 && song.channels[0].name == "3x Osc" && song.mixer[3].name == "3x Osc");
  6464. CHECK (lynx::addChannelRouted (song, osc, 0) == 1 && song.channels[1].name == "3x Osc #2" && song.channels[1].mixerTrack == 0
  6465. && song.mixer[0].name == "3x Osc #2");
  6466. CHECK (lynx::addChannelRouted (song, osc, 5) == 2 && song.channels[2].name == "3x Osc #3" && song.mixer[5].name == "3x Osc #3");
  6467. CHECK (lynx::addChannelRouted (song, flex, 3) == 3 && song.channels[3].name == "FLEX" && song.channels[3].mixerTrack == 3
  6468. && song.mixer[3].name == "3x Osc");
  6469. CHECK (lynx::addChannelRouted (song, flex, 4) == 4 && song.channels[4].name == "FLEX #2" && song.mixer[4].name == "Drums");
  6470. // a plain new channel (no insert given) keeps its name and doesn't rename anything, as before
  6471. const auto mixerBefore = song.mixer.size();
  6472. CHECK (lynx::addChannelRouted (song, osc) == 5 && song.channels[5].name == "3x Osc");
  6473. CHECK (song.mixer.size() >= mixerBefore && song.mixer[3].name == "3x Osc" && song.mixer[0].name == "3x Osc #2");
  6474. }
  6475. TEST (workflow_palette_random_colour)
  6476. {
  6477. const auto& palette = lynx::lynxPalette();
  6478. for (std::uint32_t seed = 0; seed < 50; ++seed)
  6479. {
  6480. const auto current = palette[seed % palette.size()];
  6481. const auto c = lynx::randomPaletteColour (current, seed);
  6482. CHECK (c != current && std::find (palette.begin(), palette.end(), c) != palette.end());
  6483. }
  6484. }
  6485. TEST (workflow_key_map)
  6486. {
  6487. using namespace lynx::keys;
  6488. KeyMap map;
  6489. const auto global = Context::global, rack = Context::channelRack;
  6490. CHECK (map.find ({ space, none }, global, true) == "transport.playStop");
  6491. CHECK (map.find ({ space, ctrl }, global, true) == "transport.pause");
  6492. CHECK (map.find ({ f6, none }, global, true) == "panel.channelRack");
  6493. CHECK (map.find ({ f8, alt }, global, false) == "panel.browser");
  6494. CHECK (map.find ({ insert, ctrl | shift }, global, false) == "pattern.insert");
  6495. CHECK (map.find ({ numpadPlus, none }, rack, true) == "pattern.next");
  6496. // panel context: only while focused
  6497. CHECK (map.find ({ 'L', ctrl }, rack, true) == "rack.sendToInserts");
  6498. CHECK (map.find ({ 'L', ctrl }, global, true).empty());
  6499. CHECK (map.find ({ up, alt }, rack, false) == "rack.moveUp");
  6500. // typing piano: note keys play, other plain keys only for whilePiano actions; chords always work
  6501. CHECK (KeyMap::playsNote ({ 'Z', none }, true) && ! KeyMap::playsNote ({ 'Z', none }, false) && ! KeyMap::playsNote ({ 'Z', ctrl }, true));
  6502. CHECK (map.find ({ 'L', none }, global, true) == "transport.toggleMode");
  6503. CHECK (map.find ({ '1', none }, rack, false) == "rack.mute1" && map.find ({ '0', none }, rack, false) == "rack.mute10");
  6504. CHECK (map.find ({ '1', none }, rack, true).empty()); // a plain digit is not a shortcut while the piano is on
  6505. CHECK (map.find ({ '2', ctrl }, rack, true) == "rack.solo2");
  6506. CHECK (typingPianoNote ('Z') == 48 && typingPianoNote ('q') == 60 && typingPianoNote ('P') == 76 && typingPianoNote ('L') == -1);
  6507. CHECK (map.conflicts().empty());
  6508. // chord text
  6509. CHECK (toString ({ f4, ctrl | shift }) == "Ctrl+Shift+F4" && toString ({ numpadMinus, none }) == "Numpad -");
  6510. CHECK (parseChord ("Shift+Ctrl+Ins") == (Chord { insert, ctrl | shift }));
  6511. CHECK (parseChord ("alt+up") == (Chord { up, alt }) && parseChord ("k") == (Chord { 'K', none }));
  6512. CHECK (parseChord ("Numpad +") == (Chord { numpadPlus, none }) && ! parseChord ("F13") && ! parseChord ("Ctrl+"));
  6513. for (const auto& a : actions())
  6514. for (const auto& c : a.defaults)
  6515. CHECK (parseChord (toString (c)) == c);
  6516. // JSON overrides
  6517. std::vector<std::string> errors;
  6518. CHECK (map.loadJson (R"({ "pattern.next": "Ctrl+Right", "rack.clone": ["Alt+D", "Ctrl+D"], "help.shortcuts": "", "nope": "F1", "file.open": "Hyper+Q" })", &errors));
  6519. CHECK (errors.size() == 2);
  6520. CHECK (map.find ({ right, ctrl }, global, false) == "pattern.next" && map.find ({ numpadPlus, none }, global, false).empty());
  6521. CHECK (map.find ({ 'D', ctrl }, rack, false) == "rack.clone" && map.find ({ f1, none }, global, false).empty());
  6522. map.setChords ("pattern.previous", { { right, ctrl } });
  6523. CHECK (map.conflicts().size() == 1);
  6524. KeyMap reloaded;
  6525. CHECK (reloaded.loadJson (map.toJson()) && reloaded.chordsFor ("rack.clone") == map.chordsFor ("rack.clone"));
  6526. CHECK (! map.loadJson ("[1, 2]") && ! map.loadJson ("{\"a\": }"));
  6527. map.resetToDefaults();
  6528. CHECK (map.find ({ numpadPlus, none }, global, false) == "pattern.next");
  6529. }
  6530. TEST (workflow_workspace_layout)
  6531. {
  6532. using namespace lynx::workspace;
  6533. auto l = Layout::makeDefault (1280, 620);
  6534. // every visible panel is inside the desktop; the default shows Browser, Playlist and Channel Rack
  6535. for (const auto& p : l.panels)
  6536. CHECK (p.bounds.x >= 0 && p.bounds.y >= 0 && p.bounds.right() <= 1280 && p.bounds.bottom() <= 620 && p.bounds.w >= minWidth);
  6537. CHECK (l[Panel::browser].visible && l[Panel::playlist].visible && l[Panel::channelRack].visible);
  6538. CHECK (! l[Panel::pianoRoll].visible && ! l[Panel::mixer].visible);
  6539. CHECK (l[Panel::playlist].bounds.x == l[Panel::browser].bounds.right() && l[Panel::playlist].bounds.right() == 1280);
  6540. CHECK (l.isFrontmost (Panel::channelRack) && l.focused == Panel::channelRack);
  6541. // toggle: show+focus, focus, hide
  6542. CHECK (l.toggle (Panel::mixer) && l.focused == Panel::mixer && l.isFrontmost (Panel::mixer));
  6543. CHECK (l.toggle (Panel::channelRack) && l.focused == Panel::channelRack && l.isFrontmost (Panel::channelRack));
  6544. CHECK (! l.toggle (Panel::channelRack) && ! l[Panel::channelRack].visible && l.focused == Panel::mixer);
  6545. l.toggleMaximised (Panel::playlist);
  6546. CHECK (l[Panel::playlist].maximised && l.focused == Panel::playlist);
  6547. // save and load at another size: positions scale
  6548. l[Panel::mixer].detached = true;
  6549. l[Panel::mixer].windowBounds = { 100, 50, 900, 500 };
  6550. const auto text = l.serialise();
  6551. const auto big = Layout::parse (text, 2560, 1240);
  6552. CHECK (big.has_value());
  6553. if (big)
  6554. {
  6555. CHECK ((*big)[Panel::playlist].maximised && (*big)[Panel::mixer].detached && (*big)[Panel::mixer].windowBounds.w == 900);
  6556. CHECK (std::abs ((*big)[Panel::playlist].bounds.x - 2 * l[Panel::playlist].bounds.x) <= 1);
  6557. CHECK (big->order == l.order && big->focused == l.focused);
  6558. }
  6559. 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));
  6560. // resize keeps panels inside and edges glued to the border
  6561. l.resize (1000, 500);
  6562. CHECK (l[Panel::playlist].bounds.right() == 1000 && l[Panel::browser].bounds.bottom() == 500);
  6563. for (const auto& p : l.panels)
  6564. CHECK (p.bounds.right() <= 1000 && p.bounds.bottom() <= 500);
  6565. // off-screen panels come back
  6566. l[Panel::pianoRoll].bounds = { 5000, -300, 400, 300 };
  6567. CHECK (l.bringBackOffscreen() == 1 && l[Panel::pianoRoll].bounds.x + 400 <= 1000 && l[Panel::pianoRoll].bounds.y == 0);
  6568. // snapping: to the desktop, to another panel, and resizing one edge
  6569. CHECK ((snap ({ 5, 6, 200, 100 }, {}, 1000, 500) == Rect { 0, 0, 200, 100 }));
  6570. CHECK ((snap ({ 304, 50, 200, 100 }, { { 0, 0, 300, 400 } }, 1000, 500) == Rect { 300, 50, 200, 100 }));
  6571. CHECK ((snap ({ 304, 50, 200, 100 }, { { 0, 0, 300, 400 } }, 1000, 500, 2) == Rect { 304, 50, 200, 100 })); // out of range
  6572. CHECK ((snap ({ 100, 100, 195, 100 }, { { 300, 0, 300, 400 } }, 1000, 500, 8, right) == Rect { 100, 100, 200, 100 }));
  6573. CHECK ((snap ({ 100, 100, 195, 100 }, { { 300, 1000, 300, 400 } }, 2000, 2000, 8, right) == Rect { 100, 100, 195, 100 })); // not facing
  6574. }
  6575. TEST (workflow_control_math)
  6576. {
  6577. using namespace lynx::controls;
  6578. CHECK (std::abs (dragValue (0.5, 75, 0, 1, 150, false) - 1.0) < 1e-9);
  6579. CHECK (std::abs (dragValue (0.5, 75, 0, 1, 150, true) - 0.55) < 1e-9);
  6580. CHECK (dragValue (0.5, -1000, -1, 1, 150, false) == -1.0);
  6581. 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);
  6582. CHECK (wheelValue (3, 1, 0, 10, true, 1) == 4.0); // integer controls move at least one step
  6583. CHECK (std::abs (snapToInterval (0.234, 0, 0.1) - 0.2) < 1e-9);
  6584. CHECK (tempoDrag (130.0, 8, false, false) == 132.0 && tempoDrag (130.0, -3, false, false) == 130.0);
  6585. CHECK (std::abs (tempoDrag (130.0, 25, true, false) - 130.25) < 1e-9);
  6586. CHECK (std::abs (tempoDrag (130.0, 8, false, true) - 130.2) < 1e-9);
  6587. CHECK (tempoDrag (520.0, 400, false, false) == lynx::maxTempo && halfTempo (15.0) == lynx::minTempo && doubleTempo (130.0) == 260.0);
  6588. TapTempo tap;
  6589. CHECK (! tap.tap (1000.0));
  6590. CHECK (tap.tap (1500.0) == 120.0);
  6591. CHECK (tap.tap (2000.0) == 120.0 && tap.count() == 3);
  6592. CHECK (std::abs (*tap.tap (2400.0) - 128.57) < 0.01); // average of 500, 500, 400 ms
  6593. CHECK (! tap.tap (9000.0) && tap.count() == 1); // a long pause starts over
  6594. }
  6595. TEST (flp_fl11_mixer_slots_and_disabled_channels)
  6596. {
  6597. // REA_rodada80 §3: FL <= 11 files close an effect slot without 0x62 (the slot is the 0xD4 record's second
  6598. // i32); the channel's 0x00 byte is its Channel Rack switch
  6599. if (realFlpDir.empty())
  6600. return;
  6601. auto load = [] (const std::filesystem::path& p) {
  6602. std::ifstream in (p, std::ios::binary);
  6603. std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  6604. return bytes;
  6605. };
  6606. const auto base = std::filesystem::path (realFlpDir);
  6607. if (const auto b = load (base / "System" / "Render tests" / "Fruity Phase Inverter.flp"); ! b.empty())
  6608. {
  6609. const auto project = flp::parseProject (b);
  6610. CHECK (project.mixer.size() > 2 && project.mixer[1].slots.size() == 1);
  6611. if (project.mixer.size() > 2 && project.mixer[1].slots.size() == 1)
  6612. CHECK (project.mixer[1].slots[0].index == 0 && project.mixer[1].slots[0].plugin == "Fruity Wrapper"
  6613. && ! project.mixer[1].slots[0].state.empty());
  6614. }
  6615. if (const auto b = load (base / "Demo projects" / "Performance mode" / "nucleon - Steptime.flp"); ! b.empty())
  6616. {
  6617. const auto project = flp::parseProject (b);
  6618. CHECK (project.mixer.size() > 4 && project.mixer[3].slots.size() == 4);
  6619. if (project.mixer.size() > 4 && project.mixer[3].slots.size() == 4)
  6620. for (int i = 0; i < 4; ++i)
  6621. CHECK (project.mixer[3].slots[(std::size_t) i].index == i);
  6622. const char* names[] = { "Fruity Delay Bank", "Fruity Reeverb 2", "Maximus", "Fruity Parametric EQ 2" };
  6623. if (project.mixer.size() > 4 && project.mixer[3].slots.size() == 4)
  6624. for (int i = 0; i < 4; ++i)
  6625. CHECK (project.mixer[3].slots[(std::size_t) i].plugin == names[i]);
  6626. }
  6627. if (const auto b = load (base / "Demo projects" / "Product demos" / "Synth_dfr - Overcome - Sytrus.flp"); ! b.empty())
  6628. {
  6629. const auto project = flp::parseProject (b);
  6630. int disabled = 0;
  6631. for (const auto& c : project.channels)
  6632. disabled += ! c.enabled;
  6633. CHECK (disabled == 2);
  6634. const auto song = lynx::importFlp (project);
  6635. int muted = 0;
  6636. for (const auto& c : song.channels)
  6637. muted += c.muted;
  6638. CHECK (muted == 2);
  6639. }
  6640. }
  6641. TEST (three_osc_host_hq_render_box_filters_aliasing_shapes)
  6642. {
  6643. // 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
  6644. // copies at gain g/k; a sine is not oversampled
  6645. auto state = [] (std::int32_t shape) {
  6646. std::vector<std::uint8_t> st (0x60, 0);
  6647. const std::int32_t version = 14;
  6648. std::memcpy (st.data(), &version, 4);
  6649. std::memcpy (st.data() + 4 + 4 * 1, &shape, 4); // osc 1 shape; the mix knobs at 0: osc 1 alone
  6650. std::vector<std::uint8_t> snap (364, 0);
  6651. const std::int32_t neutral[17] = { 0, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, 0 };
  6652. for (int k = 0; k < 5; ++k)
  6653. std::memcpy (snap.data() + 0x44 * k, neutral, sizeof (neutral));
  6654. const std::int32_t modX = 256, four = 4, m101 = -101;
  6655. std::memcpy (snap.data() + 0x44, &four, 4);
  6656. std::memcpy (snap.data() + 0x44 + 64, &m101, 4);
  6657. std::memcpy (snap.data() + 0x15c, &modX, 4);
  6658. st.insert (st.end(), snap.begin(), snap.end());
  6659. return st;
  6660. };
  6661. static bool hostHq = false;
  6662. auto render = [&] (std::int32_t shape, bool hq) {
  6663. hostHq = hq;
  6664. auto inst = lynx::makeThreeOscInstrument ([]() noexcept { return hostHq; });
  6665. lynx::ChannelDef c;
  6666. c.instrument = lynx::InstrumentKind::threeosc;
  6667. c.pluginState = state (shape);
  6668. inst->configure (c, 140.0);
  6669. inst->prepare (44100.0);
  6670. inst->noteOn (96, 0.78f);
  6671. std::vector<float> l (4096), r (4096);
  6672. inst->render (l.data(), r.data(), 4096, 1.0f, 1.0f);
  6673. return l;
  6674. };
  6675. auto compare = [&] (std::int32_t shape) {
  6676. const auto live = render (shape, false), hq = render (shape, true);
  6677. double diff = 0.0, energy = 0.0;
  6678. for (std::size_t i = 0; i < live.size(); ++i)
  6679. diff += std::abs (live[i] - hq[i]), energy += std::abs (live[i]);
  6680. return std::make_pair (energy, diff);
  6681. };
  6682. CHECK (lynx::threeOscStateUsable (state (3)));
  6683. const auto [sawEnergy, sawDiff] = compare (3);
  6684. CHECK (sawEnergy > 1.0 && sawDiff > 1.0e-3); // a saw renders differently in the host's HQ mode
  6685. const auto [sineEnergy, sineDiff] = compare (0);
  6686. CHECK (sineEnergy > 1.0 && sineDiff == 0.0); // a sine does not
  6687. }
  6688. int main (int argc, char** argv)
  6689. {
  6690. if (argc > 1)
  6691. realFlpDir = argv[1];
  6692. const char* only = std::getenv ("LYNX_TEST"); // run only the tests whose name contains this
  6693. for (auto& [name, fn] : registry())
  6694. {
  6695. if (only != nullptr && std::strstr (name, only) == nullptr)
  6696. continue;
  6697. std::printf ("%s\n", name);
  6698. fn();
  6699. }
  6700. std::printf ("\n%d checks, %d failures\n", checks, failures);
  6701. return failures == 0 ? 0 : 1;
  6702. }
  6703. TEST (synth_cutoff_keytrack)
  6704. {
  6705. // the cutoff follows the note from key 48 (130.81 Hz), like Harmor's "Key to filter frequency"
  6706. auto rms = [] (float keyTrack, int key) {
  6707. lynx::SynthSettings s;
  6708. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  6709. s.cutoff = 130.81f;
  6710. s.keyTrack = keyTrack;
  6711. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  6712. inst->prepare (48000.0);
  6713. lynx::ChannelDef c;
  6714. c.synth = s;
  6715. inst->configure (c, 120.0);
  6716. inst->noteOn (key, 1.0f);
  6717. std::vector<float> l (24000), r (24000);
  6718. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  6719. double e = 0.0;
  6720. for (int i = 4800; i < 24000; ++i)
  6721. e += (double) l[(std::size_t) i] * l[(std::size_t) i];
  6722. return std::sqrt (e / 19200.0);
  6723. };
  6724. const auto fixed = rms (0.0f, 72), tracked = rms (1.0f, 72);
  6725. CHECK (tracked > 4.0 * fixed); // 2 octaves up: without keytrack the low-pass cuts
  6726. CHECK (std::abs (rms (1.0f, 48) - rms (0.0f, 48)) < 0.05 * rms (0.0f, 48)); // unchanged at the reference key
  6727. }
  6728. namespace
  6729. {
  6730. std::vector<float> renderSynthNote (const lynx::SynthSettings& s, int key, int frames)
  6731. {
  6732. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  6733. inst->prepare (48000.0);
  6734. lynx::ChannelDef c;
  6735. c.synth = s;
  6736. inst->configure (c, 120.0);
  6737. inst->noteOn (key, 1.0f);
  6738. std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
  6739. inst->render (l.data(), r.data(), frames, 1.0f, 1.0f);
  6740. return l;
  6741. }
  6742. double goertzel (const std::vector<float>& x, double f, int from, int to)
  6743. {
  6744. const auto w = 2.0 * 3.141592653589793 * f / 48000.0, k = 2.0 * std::cos (w);
  6745. double s1 = 0.0, s2 = 0.0;
  6746. for (int i = from; i < to; ++i)
  6747. {
  6748. const auto s0 = x[(std::size_t) i] + k * s1 - s2;
  6749. s2 = s1;
  6750. s1 = s0;
  6751. }
  6752. return s1 * s1 + s2 * s2 - k * s1 * s2;
  6753. }
  6754. } // namespace
  6755. TEST (synth_pitch_sweep_ring_and_bands)
  6756. {
  6757. lynx::SynthSettings sine;
  6758. sine.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  6759. sine.cutoff = 20000.0f;
  6760. // sweep: starts at f·(1 + sweep) and returns to f (time constant s = 10 ms; s⁴ falls 4× faster)
  6761. auto sweep = sine;
  6762. sweep.sweep = 1.0f;
  6763. sweep.sweepTime = 0.01f;
  6764. const auto a = renderSynthNote (sweep, 69, 24000);
  6765. auto crossings = [] (const std::vector<float>& x, int to) {
  6766. int n = 0;
  6767. for (int i = 1; i < to; ++i)
  6768. n += (x[(std::size_t) i - 1] < 0.0f) != (x[(std::size_t) i] < 0.0f);
  6769. return n;
  6770. };
  6771. CHECK (crossings (a, 480) > 1.3 * crossings (renderSynthNote (sine, 69, 480), 480)); // 10 ms: much higher
  6772. CHECK (goertzel (a, 440.0, 12000, 24000) > 100.0 * goertzel (a, 880.0, 12000, 24000));
  6773. // ring with |a| = 1: carrier 440 × modulator 110 -> only 330 and 550, no 440
  6774. auto ring = sine;
  6775. ring.am = ring.ring = true;
  6776. ring.osc[2] = { lynx::Waveform::sine, -2, 0.0f, 1.0f };
  6777. const auto b = renderSynthNote (ring, 69, 24000);
  6778. CHECK (goertzel (b, 330.0, 4800, 24000) > 100.0 * goertzel (b, 440.0, 4800, 24000));
  6779. CHECK (goertzel (b, 550.0, 4800, 24000) > 100.0 * goertzel (b, 440.0, 4800, 24000));
  6780. // 3 bands: muting the high knocks down a 4 kHz note and barely touches an 80 Hz one
  6781. auto bands = sine;
  6782. bands.bandGain = { 1.0f, 1.0f, 0.0f };
  6783. auto rms = [] (const std::vector<float>& x) {
  6784. double e = 0.0;
  6785. for (std::size_t i = 4800; i < x.size(); ++i)
  6786. e += (double) x[i] * x[i];
  6787. return std::sqrt (e / (double) (x.size() - 4800));
  6788. };
  6789. CHECK (rms (renderSynthNote (bands, 107, 24000)) < 0.3 * rms (renderSynthNote (sine, 107, 24000)));
  6790. CHECK (rms (renderSynthNote (bands, 28, 24000)) > 0.85 * rms (renderSynthNote (sine, 28, 24000)));
  6791. }
  6792. TEST (love_philter_map_evaluation)
  6793. {
  6794. lynx::LovePhilterMap line { { { 0.0, 0.0 }, { 1.0, 1.0 } } };
  6795. CHECK (std::abs (lynx::lovePhilterMapAt (line, 0.5) - 0.5) < 1e-9);
  6796. CHECK (std::abs (lynx::lovePhilterMapAt (line, 1.0) - 1.0) < 1e-9);
  6797. // steps (Δx = 0): 1 until 0.4375, ramp to 0 at 0.5, then 0
  6798. lynx::LovePhilterMap steps { { { 0.0, 1.0 }, { 0.4375, 1.0 }, { 0.0625, 0.0 }, { 0.5, 0.0 } } };
  6799. CHECK (lynx::lovePhilterMapAt (steps, 0.27) == 1.0);
  6800. CHECK (lynx::lovePhilterMapAt (steps, 0.75) == 0.0);
  6801. CHECK (std::abs (lynx::lovePhilterMapAt (steps, 0.46875) - 0.5) < 1e-9);
  6802. CHECK (lynx::lovePhilterMapAt ({}, 0.3) == 1.0);
  6803. }
  6804. TEST (lofi_holds_and_quantizes)
  6805. {
  6806. auto fx = lynx::makeEffect (lynx::EffectKind::lofi);
  6807. fx->prepare (48000.0);
  6808. auto slot = lynx::makeEffectSlot (lynx::EffectKind::lofi);
  6809. slot.params[0] = 0.5f; // 128·0.125 + 1 = 17 samples
  6810. slot.params[1] = 1.0f; // 65536/2049 ≈ 32 levels
  6811. std::vector<float> l (170), r (170);
  6812. for (std::size_t i = 0; i < l.size(); ++i)
  6813. l[i] = r[i] = 0.9f * (float) std::sin (0.05 * (double) i);
  6814. const auto original = l;
  6815. fx->process (l.data(), r.data(), 170, slot.params, 120.0);
  6816. for (int i = 0; i < 17; ++i)
  6817. CHECK (l[(std::size_t) i] == l[0]);
  6818. CHECK (l[17] != l[16]);
  6819. const auto step = 2.0f * 2049.0f / 65536.0f;
  6820. for (auto v : l)
  6821. CHECK (std::abs (v / step - std::round (v / step)) < 1e-3f);
  6822. CHECK (std::abs (l[0] - original[0]) <= step);
  6823. CHECK (lynx::formatEffectParam (lynx::EffectKind::lofi, 1, 0.0f, 120.0) == "16.0 bits");
  6824. }
  6825. namespace
  6826. {
  6827. lynx::EffectParams effectorParams (int type, float x, float y)
  6828. {
  6829. auto slot = lynx::makeEffectSlot (lynx::EffectKind::effector);
  6830. slot.params[0] = (float) type / 11.0f;
  6831. slot.params[3] = x;
  6832. slot.params[4] = y;
  6833. return slot.params;
  6834. }
  6835. } // namespace
  6836. TEST (effector_algorithms)
  6837. {
  6838. CHECK (lynx::effector::typeOf (8.0f / 11.0f) == 8);
  6839. CHECK (std::string (lynx::effector::typeName (11)) == "Ring");
  6840. CHECK (std::abs (lynx::effector::rateBeats (0.5f, 0.0f) - 0.75) < 1e-9); // trunc(8·0.5 + 0.5) = 4 -> 3/4
  6841. CHECK (std::abs (lynx::effector::rateBeats (1.0f, 0.5f) - 2.0) < 1e-9); // 8 × (1 − 0.5)²
  6842. auto run = [] (const lynx::EffectParams& p, std::vector<float> in, double bpm = 120.0) {
  6843. auto fx = lynx::makeEffect (lynx::EffectKind::effector);
  6844. fx->prepare (48000.0);
  6845. fx->setSongPosition (0.0);
  6846. auto r = in;
  6847. fx->process (in.data(), r.data(), (int) in.size(), p, bpm);
  6848. return in;
  6849. };
  6850. // Distort with Y = 1 (everything goes to the cascade): 0.5 comes out 0.5 (compensation G = 0.5/F(0.5))
  6851. {
  6852. auto p = effectorParams (0, 0.5f, 1.0f);
  6853. const auto out = run (p, std::vector<float> (4800, 0.5f));
  6854. CHECK (std::abs (out.back() - 0.5f) < 1e-3f);
  6855. const auto small = run (p, std::vector<float> (4800, 0.05f));
  6856. CHECK (small.back() > 0.45f && small.back() < 0.5f); // saturates near ±0.5
  6857. }
  6858. // Ring: DC 1 becomes the carrier sine, 2^(8.7·0 + 5) = 32 Hz -> 32 cycles per second
  6859. {
  6860. auto p = effectorParams (11, 0.0f, 0.0f);
  6861. const auto out = run (p, std::vector<float> (48000, 1.0f));
  6862. int crossings = 0;
  6863. for (std::size_t i = 4800; i < out.size(); ++i)
  6864. crossings += (out[i - 1] < 0.0f) != (out[i] < 0.0f);
  6865. CHECK (crossings >= 56 && crossings <= 60); // 0.9 s × 32 Hz × 2
  6866. }
  6867. // Trancer, Y = 0: at the start of the period the gain drops to zero; X = 1 returns only at the end of the period
  6868. {
  6869. auto p = effectorParams (8, 1.0f, 0.0f);
  6870. p[5] = 1.0f; // 8/1 = 8 beats = 4 s at 120 BPM
  6871. const auto out = run (p, std::vector<float> (48000, 1.0f));
  6872. CHECK (out[100] > 0.3f); // still rising on the "and" (1% of the period = 40 ms)
  6873. CHECK (out[24000] < 0.05f); // 0.5 s: trough
  6874. }
  6875. // Reverb with Wet = 0: the dry passes intact
  6876. {
  6877. auto p = effectorParams (6, 0.5f, 0.5f);
  6878. p[2] = 0.0f;
  6879. std::vector<float> in (4800);
  6880. for (std::size_t i = 0; i < in.size(); ++i)
  6881. in[i] = (float) std::sin (0.01 * (double) i);
  6882. const auto out = run (p, in);
  6883. for (std::size_t i = 0; i < in.size(); ++i)
  6884. CHECK (std::abs (out[i] - in[i]) < 1e-6f);
  6885. }
  6886. // Delay: the echo arrives after the Mod Rate period (1/4 beat at 120 BPM = 125 ms), at level X²
  6887. {
  6888. auto p = effectorParams (5, 0.7f, 0.5f);
  6889. p[5] = 1.0f / 8.0f; // index 1 = 1/4
  6890. std::vector<float> in (12000, 0.0f);
  6891. in[0] = 1.0f;
  6892. const auto out = run (p, in);
  6893. CHECK (std::abs (out[6000] - 0.49f) < 0.02f);
  6894. CHECK (std::abs (out[3000]) < 1e-6f);
  6895. }
  6896. // Stutter: repeats the recorded start every period
  6897. {
  6898. auto p = effectorParams (9, 0.0f, 0.0f);
  6899. p[5] = 0.0f; // 1/8 beat = 62.5 ms = 3000 samples
  6900. std::vector<float> in (9000);
  6901. for (std::size_t i = 0; i < in.size(); ++i)
  6902. in[i] = (float) i / 9000.0f;
  6903. const auto out = run (p, in);
  6904. CHECK (std::abs (out[3000 + 500] - in[500]) < 0.01f); // after the crossfade, it's the start again
  6905. CHECK (std::abs (out[1000] - in[1000]) < 1e-6f);
  6906. }
  6907. }
  6908. TEST (crossover_linkwitz_riley_sums_flat)
  6909. {
  6910. // low band + high band of the same crossover: sum with magnitude 1 (Linkwitz-Riley; for 12 and 36 dB the HP is inverted)
  6911. for (const auto combo : { 1, 3, 4, 5, 6, 7 })
  6912. for (const auto testHz : { 200.0, 1000.0, 5000.0 })
  6913. {
  6914. auto run = [&] (bool high) {
  6915. auto fx = lynx::makeEffect (lynx::EffectKind::crossover);
  6916. fx->prepare (48000.0);
  6917. auto slot = lynx::makeEffectSlot (lynx::EffectKind::crossover);
  6918. const auto value = (float) (std::log2 (1000.0 / 30.0) / 9.0589);
  6919. slot.params[high ? 0 : 3] = 1.0f;
  6920. slot.params[high ? 1 : 4] = value;
  6921. slot.params[high ? 2 : 5] = (float) combo / 7.0f;
  6922. std::vector<float> l (24000), r (24000);
  6923. for (std::size_t i = 0; i < l.size(); ++i)
  6924. l[i] = r[i] = (float) std::sin (2.0 * 3.141592653589793 * testHz * (double) i / 48000.0);
  6925. fx->process (l.data(), r.data(), 24000, slot.params, 120.0);
  6926. return l;
  6927. };
  6928. const auto low = run (false), high = run (true);
  6929. double peak = 0.0;
  6930. for (std::size_t i = 12000; i < low.size(); ++i)
  6931. peak = std::max (peak, (double) std::abs (low[i] + high[i]));
  6932. CHECK (std::abs (peak - 1.0) < 0.02);
  6933. }
  6934. // 96 dB/oct: two octaves above the cutoff the low-pass is already below −150 dB... in practice, very low
  6935. auto fx = lynx::makeEffect (lynx::EffectKind::crossover);
  6936. fx->prepare (48000.0);
  6937. auto slot = lynx::makeEffectSlot (lynx::EffectKind::crossover);
  6938. slot.params[3] = 1.0f;
  6939. slot.params[4] = (float) (std::log2 (500.0 / 30.0) / 9.0589);
  6940. slot.params[5] = 1.0f;
  6941. std::vector<float> l (24000), r (24000);
  6942. for (std::size_t i = 0; i < l.size(); ++i)
  6943. l[i] = r[i] = (float) std::sin (2.0 * 3.141592653589793 * 2000.0 * (double) i / 48000.0);
  6944. fx->process (l.data(), r.data(), 24000, slot.params, 120.0);
  6945. float peak = 0.0f;
  6946. for (std::size_t i = 12000; i < l.size(); ++i)
  6947. peak = std::max (peak, std::abs (l[i]));
  6948. CHECK (peak < 1e-4f);
  6949. }
  6950. TEST (rhythm_gain_and_autopan)
  6951. {
  6952. // 1-beat gate: 1 → 0 at 0.5 beat (linear) → 1 at the end; at 120 BPM 1 beat = 24000 samples at 48 kHz
  6953. auto slot = lynx::makeEffectSlot (lynx::EffectKind::rhythmGain);
  6954. slot.params[0] = 1.0f / 64.0f;
  6955. slot.params[1] = 3.0f / 12.0f;
  6956. const float pts[3][3] = { { 0.0f, 1.0f, 0.5f }, { 0.5f, 0.0f, 0.5f }, { 1.0f, 1.0f, 0.5f } };
  6957. for (int k = 0; k < 3; ++k)
  6958. for (int j = 0; j < 3; ++j)
  6959. slot.params[(std::size_t) (2 + 3 * k + j)] = pts[k][j];
  6960. auto fx = lynx::makeEffect (lynx::EffectKind::rhythmGain);
  6961. fx->prepare (48000.0);
  6962. fx->setSongPosition (960.0 * 2.0); // beat 2: phase 0
  6963. std::vector<float> l (24000, 1.0f), r (24000, 1.0f);
  6964. fx->process (l.data(), r.data(), 24000, slot.params, 120.0);
  6965. CHECK (std::abs (l[0] - 1.0f) < 1e-3f);
  6966. CHECK (std::abs (l[6000] - 0.5f) < 1e-3f);
  6967. CHECK (l[12000] < 1e-3f);
  6968. CHECK (std::abs (l[18000] - 0.5f) < 1e-3f);
  6969. // positive tension: changes quickly at the start of the segment
  6970. CHECK (lynx::effectInfo (lynx::EffectKind::rhythmGain).params.size() == 39);
  6971. // auto-pan: 1-beat period, full depth; start phase 0.75 -> W = 0 rising (pan centered)
  6972. auto pan = lynx::makeEffectSlot (lynx::EffectKind::autoPan);
  6973. pan.params[0] = 1.0f / 64.0f;
  6974. pan.params[1] = 1.0f;
  6975. auto ap = lynx::makeEffect (lynx::EffectKind::autoPan);
  6976. ap->prepare (48000.0);
  6977. ap->setSongPosition (0.0);
  6978. std::vector<float> a (24000, 1.0f), b (24000, 1.0f);
  6979. ap->process (a.data(), b.data(), 24000, pan.params, 120.0);
  6980. CHECK (std::abs (a[0] - 1.0f) < 1e-3f && std::abs (b[0] - 1.0f) < 1e-3f);
  6981. CHECK (a[6000] < 0.01f && std::abs (b[6000] - 1.0f) < 1e-3f); // ¼ cycle later: fully right
  6982. }
  6983. TEST (vocoder_side_inputs_split_modulator_and_carrier)
  6984. {
  6985. // "Side inputs" mode (Vocodex): modulator from side input 1 (noise in the first half), carrier from side input 2
  6986. // (saw), own input silent → the output follows the side modulator; with input 1 silent, the output is silent
  6987. auto slot = lynx::makeEffectSlot (lynx::EffectKind::vocoder);
  6988. slot.params[16] = 1.0f;
  6989. auto fx = lynx::makeEffect (lynx::EffectKind::vocoder);
  6990. fx->prepare (48000.0);
  6991. std::vector<float> modL (48000), car (48000), l (48000, 0.0f), r (48000, 0.0f);
  6992. std::uint32_t seed = 1;
  6993. for (std::size_t i = 0; i < modL.size(); ++i)
  6994. {
  6995. seed = seed * 1664525u + 1013904223u;
  6996. modL[i] = i < 24000 ? ((float) (seed >> 8) / 16777216.0f - 0.5f) : 0.0f;
  6997. car[i] = (float) (2.0 * std::fmod ((double) i * 220.0 / 48000.0, 1.0) - 1.0) * 0.5f;
  6998. }
  6999. for (int done = 0; done < 48000; done += 480)
  7000. {
  7001. fx->setSidechain (modL.data() + done, modL.data() + done);
  7002. fx->setSidechain2 (car.data() + done, car.data() + done);
  7003. fx->process (l.data() + done, r.data() + done, 480, slot.params, 120.0);
  7004. }
  7005. auto rms = [&] (int from, int to) {
  7006. double e = 0.0;
  7007. for (int i = from; i < to; ++i)
  7008. e += (double) l[(std::size_t) i] * l[(std::size_t) i];
  7009. return std::sqrt (e / (to - from));
  7010. };
  7011. CHECK (rms (6000, 20000) > 0.01);
  7012. CHECK (rms (40000, 48000) < 0.1 * rms (6000, 20000));
  7013. // the side modulator configured but silent: nothing comes out (the own input is not used instead)
  7014. std::vector<float> a (4800, 0.3f), b (4800, 0.3f);
  7015. for (int done = 0; done < 4800; done += 480)
  7016. {
  7017. fx->setSidechain (nullptr, nullptr);
  7018. fx->setSidechain2 (car.data() + done, car.data() + done);
  7019. fx->process (a.data() + done, b.data() + done, 480, slot.params, 120.0);
  7020. }
  7021. CHECK (std::abs (a[4700]) < 0.05f);
  7022. }
  7023. TEST (vocoder_follows_modulator)
  7024. {
  7025. // modulator (L) plays only in the first half; carrier (R) is a continuous saw: the wet follows the modulator
  7026. auto slot = lynx::makeEffectSlot (lynx::EffectKind::vocoder);
  7027. auto fx = lynx::makeEffect (lynx::EffectKind::vocoder);
  7028. fx->prepare (48000.0);
  7029. std::vector<float> l (48000), r (48000);
  7030. std::uint32_t seed = 1;
  7031. for (std::size_t i = 0; i < l.size(); ++i)
  7032. {
  7033. seed = seed * 1664525u + 1013904223u;
  7034. l[i] = i < 24000 ? ((float) (seed >> 8) / 16777216.0f - 0.5f) : 0.0f;
  7035. r[i] = (float) (2.0 * std::fmod ((double) i * 220.0 / 48000.0, 1.0) - 1.0) * 0.5f;
  7036. }
  7037. fx->process (l.data(), r.data(), 48000, slot.params, 120.0);
  7038. auto rms = [&] (int from, int to) {
  7039. double e = 0.0;
  7040. for (int i = from; i < to; ++i)
  7041. e += (double) l[(std::size_t) i] * l[(std::size_t) i];
  7042. return std::sqrt (e / (to - from));
  7043. };
  7044. CHECK (rms (6000, 20000) > 0.01);
  7045. CHECK (rms (40000, 48000) < 0.1 * rms (6000, 20000)); // modulator silent: the vocoder fades out (100 ms release)
  7046. for (std::size_t i = 0; i < l.size(); ++i)
  7047. CHECK (l[i] == r[i] && std::isfinite (l[i])); // mono output
  7048. }
  7049. TEST (fruity_vocoder_engine)
  7050. {
  7051. // REA_rodada70: Fruity Vocoder, states of our own against the installation's DLL (goldens = the DLL's output for
  7052. // the same input, EXEC; all three bit-identical over the whole 4096 frames)
  7053. std::vector<float> inL (4096), inR (4096);
  7054. for (int i = 0; i < 4096; ++i)
  7055. {
  7056. inL[(std::size_t) i] = i < 3000 ? (float) (((i * 37) % 200) - 100) / 128.0f : 0.0f; // modulator, then silence
  7057. inR[(std::size_t) i] = (i * 13) % 64 < 32 ? 0.5f : -0.5f; // carrier
  7058. }
  7059. auto make = [] (int bands, int order, int listen, bool swap, std::vector<float> levels, std::array<int, 12> c) {
  7060. lynx::fvocoder::State s;
  7061. s.bands = bands;
  7062. s.order = order;
  7063. s.listen = listen;
  7064. s.swap = swap;
  7065. for (std::size_t k = 0; k < levels.size(); ++k)
  7066. s.levels[k] = levels[k];
  7067. s.c = c;
  7068. return s;
  7069. };
  7070. auto run = [&] (const lynx::fvocoder::State& s, double rate, int block, int holdAt) {
  7071. std::vector<float> l = inL, r = inR;
  7072. lynx::fvocoder::Engine e;
  7073. e.setState (s);
  7074. e.prepare (rate);
  7075. for (int i = 0; i < 4096;)
  7076. {
  7077. auto len = std::min (block, 4096 - i);
  7078. if (holdAt > i && holdAt < i + len)
  7079. len = holdAt - i;
  7080. if (i == holdAt)
  7081. {
  7082. auto t = e.state();
  7083. t.c[lynx::fvocoder::hold] = 1;
  7084. e.setState (t);
  7085. }
  7086. e.processStereo (l.data() + i, r.data() + i, len);
  7087. i += len;
  7088. }
  7089. return std::make_pair (l, r);
  7090. };
  7091. auto check = [] (const std::pair<std::vector<float>, std::vector<float>>& out, const std::vector<std::pair<int, float>>& golden) {
  7092. for (const auto& [i, g] : golden)
  7093. CHECK (out.first[(std::size_t) i] == g && out.second[(std::size_t) i] == g);
  7094. };
  7095. // 16 bands, order 2, edited band levels, modulator and carrier levels in the mix
  7096. 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 },
  7097. { 0, 65536, 64, 0, 0, 50, 20, 100, 0, 20, 10, 128 });
  7098. const auto outA = run (a, 44100.0, 512, -1);
  7099. check (outA, { { 100, -0.23145467042922974f }, { 1000, 0.2901436686515808f }, { 2047, 0.03407000005245209f }, { 2500, 0.8314149379730225f },
  7100. { 3000, 0.606562614440918f }, { 3500, 0.2637328505516052f }, { 4095, -0.001206810586154461f } });
  7101. // 64 bands, order 3, inverted synthesis bank shifted by −300 cents, 48 kHz in blocks of 300, Hold from frame 2048
  7102. const auto b = make (64, 3, 2, false, {}, { 2000, 60000, 90, 1, -300, 120, 15, 300, 0, 0, 5, 150 });
  7103. check (run (b, 48000.0, 300, 2048), { { 100, 0.005184958688914776f }, { 1000, -0.0008608861826360226f }, { 2047, -0.026466617360711098f },
  7104. { 2500, -0.009371745400130749f }, { 3000, -0.018949978053569794f }, { 3500, -0.02667868137359619f },
  7105. { 4095, -0.021947938948869705f } });
  7106. // 4 bands, order 1, swapped inputs, narrow 30000…50000 range with the linear scale, 22.05 kHz in blocks of 77
  7107. 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 });
  7108. check (run (c, 22050.0, 77, -1), { { 100, 0.31434211134910583f }, { 1000, -1.953513264656067f }, { 2047, 1.4135023355484009f },
  7109. { 2500, 1.539392352104187f }, { 3000, -0.27189597487449646f }, { 3500, -0.11583124846220016f },
  7110. { 4095, -0.11583124846220016f } });
  7111. // FL's state imports into the LYNX slot (controls as parameters, the levels in the state) and the slot's effect
  7112. // (one 4096-frame call, split into 512-frame engine blocks) gives the same output
  7113. const auto bytes = lynx::fvocoder::serialise (a);
  7114. CHECK (bytes.size() == 17 + 16 * 4 + 12 * 4);
  7115. const auto slot = lynx::effectFromFl ("Fruity Vocoder", bytes);
  7116. CHECK (slot && slot->kind == lynx::EffectKind::vocoder && slot->state == bytes);
  7117. CHECK (slot && slot->params[0] == 0.4f && slot->params[1] == 0.5f && slot->params[2] == 1.0f && slot->params[15] == 0.8f);
  7118. if (slot)
  7119. {
  7120. auto fx = lynx::makeEffect (*slot);
  7121. fx->prepare (44100.0);
  7122. std::vector<float> l = inL, r = inR;
  7123. fx->process (l.data(), r.data(), 4096, slot->params, 120.0);
  7124. CHECK (l == outA.first && r == outA.second);
  7125. CHECK (lynx::formatEffectParam (lynx::EffectKind::vocoder, 5, slot->params[5], 120.0) == "16000 Hz");
  7126. CHECK (lynx::formatEffectParam (lynx::EffectKind::vocoder, 10, slot->params[10], 120.0) == "20 ms");
  7127. CHECK (lynx::formatEffectParam (lynx::EffectKind::vocoder, 15, slot->params[15], 120.0) == "1.00");
  7128. }
  7129. const auto parsed = lynx::fvocoder::parse (bytes);
  7130. CHECK (parsed && parsed->bands == 16 && parsed->levels[5] == 2.0f && parsed->c[lynx::fvocoder::release] == 100);
  7131. CHECK (! lynx::fvocoder::parse ({ 2, 0, 0, 0, 12, 0, 0, 0 })); // 12 bands is not a valid count
  7132. // a raw control outside the knob range (FL keeps it, REA_rodada78 §1) survives while the knob shows it, clamped
  7133. CHECK (lynx::fvocoder::rawFromKnob (0.0f, lynx::fvocoder::attack, 5) == 5);
  7134. CHECK (lynx::fvocoder::rawFromKnob (0.5f, lynx::fvocoder::attack, 5) == 505);
  7135. CHECK (lynx::fvocoder::rawFromKnob (1.0f, lynx::fvocoder::wetLevel, 250) == 250);
  7136. CHECK (lynx::fvocoder::rawFromKnob (0.5f, lynx::fvocoder::freqScale, 64) == 64);
  7137. }
  7138. // Delphi run-time math (REA_rodada70 §2): values the plugin's own exp / ln / SinCos return (EXEC, called in place)
  7139. TEST (delphi_math_matches_plugin_runtime)
  7140. {
  7141. // arguments where the C library differs from the Delphi run-time in the last bit
  7142. CHECK (lynx::dmath::exp (0x1.f6e5c5a26a7f8p+3) == 0x1.98170c3deea30p+22);
  7143. CHECK (lynx::dmath::exp (-0x1.12fc91a51d906p+4) == 0x1.27108f934a58cp-25);
  7144. CHECK (lynx::dmath::exp (-0x1.2e9e4c229e93ap+4) == 0x1.a3bf2e4ee3f1ep-28);
  7145. CHECK (lynx::dmath::ln (0x1.76a77e2a09abep-1) == -0x1.3fd111c5297f8p-2);
  7146. CHECK (lynx::dmath::ln (0x1.da1feaa8547dfp+0) == 0x1.3b8abd2756774p-1);
  7147. CHECK (lynx::dmath::ln (0x1.a1e28772a0d78p-1) == -0x1.9ffcaa3a11e30p-3);
  7148. double s = 0.0, c = 0.0;
  7149. lynx::dmath::sinCos (0x1.91b4a19e65a9bp-1, s, c);
  7150. CHECK (s == 0x1.69be278761916p-1 && c == 0x1.6a559572e1ac9p-1);
  7151. lynx::dmath::sinCos (0x1.3284a93d70a8ap-1, s, c);
  7152. CHECK (s == 0x1.2088b313ea643p-1 && c == 0x1.a6f4d889fd91ap-1);
  7153. lynx::dmath::sinCos (0x1.f9b2f53d6ce02p+0, s, c);
  7154. CHECK (s == 0x1.d6a9b8f170994p-1 && c == -0x1.931701be7b5f3p-2);
  7155. }
  7156. // Vocodex (REA_rodada35): a synthetic state in FL's layout (version 8, 13 maps, 20 parameters, the 0x24 block,
  7157. // 13 map-axis bytes, tone, notes)
  7158. struct VocodexSpec
  7159. {
  7160. // Wet, SG, ModVol, ModHP, CarVol, CarLP, CarNoise, CarNoiseHP, Hold, Att, AttCurve, Rel, RelCurve, Freeze, BW,
  7161. // Flatness, ModBW, ModPitch, FatPitch, FatPan
  7162. 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 };
  7163. std::array<std::int32_t, 7> block { 0, 2, 0, 24, 1, 1, 2 }; // hold mode, order, unison, bands, draft field, ModIn, CarIn
  7164. 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
  7165. std::vector<std::uint8_t> bytes() const
  7166. {
  7167. std::vector<std::uint8_t> d;
  7168. 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); };
  7169. auto i32 = [&] (std::int32_t v) { put (&v, 4); };
  7170. i32 (8 << 16);
  7171. for (int m = 0; m < 13; ++m)
  7172. {
  7173. const double y0 = m == 8 ? 0.1429 : (m == 3 || m == 4 || m == 11) ? 0.0 : 0.5;
  7174. const double y1 = m == 8 ? 0.9918 : y0;
  7175. i32 (1), i32 (0), i32 (2);
  7176. for (const auto& [dx, y] : { std::pair { 0.0, y0 }, std::pair { 1.0, y1 } })
  7177. {
  7178. put (&dx, 8), put (&y, 8);
  7179. i32 (0), i32 (0); // tension (f32 0), flags
  7180. }
  7181. const std::array<std::uint8_t, 20> tail {};
  7182. put (tail.data(), tail.size());
  7183. }
  7184. for (auto v : params)
  7185. i32 (v);
  7186. for (auto v : block)
  7187. i32 (v);
  7188. put (flags.data(), flags.size());
  7189. const std::array<std::uint8_t, 13> axis {};
  7190. put (axis.data(), axis.size());
  7191. d.push_back (3), put ("PWM", 3);
  7192. i32 (1), i32 (60);
  7193. return d;
  7194. }
  7195. };
  7196. std::vector<float> vocodexSaw (int n, double hz, double fs)
  7197. {
  7198. std::vector<float> out ((std::size_t) n);
  7199. for (int i = 0; i < n; ++i)
  7200. out[(std::size_t) i] = (float) (2.0 * std::fmod (i * hz / fs, 1.0) - 1.0) * 0.4f;
  7201. return out;
  7202. }
  7203. TEST (vocodex_state_parse_and_conversion)
  7204. {
  7205. VocodexSpec spec;
  7206. spec.block = { 1, 3, 2, 40, 0, 2, 1 };
  7207. spec.params[17] = -1200;
  7208. const auto raw = spec.bytes();
  7209. const auto s = lynx::vocodex::parseState (raw);
  7210. CHECK (s.has_value());
  7211. if (! s)
  7212. return;
  7213. CHECK (s->version == 8 && s->holdMode == 1 && s->order == 3 && s->unison == 2 && s->bands == 40);
  7214. CHECK (s->draft() && s->modIn == 2 && s->carIn == 1 && s->minTimes && s->subtractiveGate && ! s->lrEncoding);
  7215. CHECK (s->params[lynx::vocodex::modPitch] == -1200 && s->params[lynx::vocodex::carLp] == 65536);
  7216. 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);
  7217. CHECK (s->tone == "PWM" && s->notes.size() == 1 && s->notes[0] == 60);
  7218. const auto v = lynx::vocodexEffect (raw);
  7219. CHECK (v && v->slot.kind == lynx::EffectKind::vocodex && v->slot.state == raw && v->modIn == 2 && v->carIn == 1 && v->notes.empty());
  7220. spec.block[6] = -1; // internal synth carrier: not available yet → reported
  7221. spec.params[1] = 150; // Soundgoodizer stage
  7222. const auto w = lynx::vocodexEffect (spec.bytes());
  7223. CHECK (w && w->carIn == -1 && w->notes.size() == 2);
  7224. CHECK (! lynx::vocodex::parseState ({ 1, 2, 3 }).has_value());
  7225. }
  7226. TEST (vocodex_vocodes_side_inputs)
  7227. {
  7228. // modulator on side input 1 (noise in the first half), carrier on side input 2 (saw), own input silent: the
  7229. // output follows the modulator's envelope and fades after it stops
  7230. VocodexSpec spec;
  7231. lynx::EffectSlot slot = lynx::makeEffectSlot (lynx::EffectKind::vocodex);
  7232. slot.state = spec.bytes();
  7233. auto fx = lynx::makeEffect (slot);
  7234. fx->prepare (48000.0);
  7235. CHECK (fx->latencySamples() == 0);
  7236. const auto car = vocodexSaw (48000, 110.0, 48000.0);
  7237. std::vector<float> mod (48000), l (48000, 0.0f), r (48000, 0.0f);
  7238. std::uint32_t seed = 7;
  7239. for (std::size_t i = 0; i < mod.size(); ++i)
  7240. {
  7241. seed = seed * 1664525u + 1013904223u;
  7242. mod[i] = i < 24000 ? ((float) (seed >> 8) / 16777216.0f - 0.5f) : 0.0f;
  7243. }
  7244. for (int done = 0; done < 48000; done += 512)
  7245. {
  7246. const int n = std::min (512, 48000 - done);
  7247. fx->setSidechain (mod.data() + done, mod.data() + done);
  7248. fx->setSidechain2 (car.data() + done, car.data() + done);
  7249. fx->process (l.data() + done, r.data() + done, n, slot.params, 120.0);
  7250. }
  7251. auto rms = [&] (int from, int to) {
  7252. double e = 0.0;
  7253. for (int i = from; i < to; ++i)
  7254. e += (double) l[(std::size_t) i] * l[(std::size_t) i] + (double) r[(std::size_t) i] * r[(std::size_t) i];
  7255. return std::sqrt (e / (2.0 * (to - from)));
  7256. };
  7257. CHECK (rms (6000, 20000) > 0.02);
  7258. CHECK (rms (40000, 48000) < 0.01 * rms (6000, 20000));
  7259. CHECK (std::abs (l[0]) < 1e-6f); // the gains ramp up from 0 when the plugin is created
  7260. }
  7261. TEST (vocodex_draft_latency_and_pass_through)
  7262. {
  7263. // carrier pass-through at unit gain only (Wet 0): after a Flush the gains snap, so the output is the carrier,
  7264. // 16 samples late in Draft
  7265. VocodexSpec spec;
  7266. spec.params[0] = 0; // Wet
  7267. spec.params[4] = 12800; // CarVol = 1
  7268. for (const int draftField : { 1, 0 })
  7269. {
  7270. spec.block[4] = draftField;
  7271. const auto s = lynx::vocodex::parseState (spec.bytes());
  7272. CHECK (s.has_value());
  7273. lynx::vocodex::Engine engine;
  7274. engine.prepare (*s, 44100.0, 300, s->draft());
  7275. CHECK (engine.latencySamples() == (draftField == 0 ? 16 : 0));
  7276. engine.flush();
  7277. const auto saw = vocodexSaw (3000, 220.0, 44100.0);
  7278. std::vector<float> car (6000), mod (6000, 0.0f), out (6000);
  7279. for (int i = 0; i < 3000; ++i)
  7280. car[(std::size_t) (2 * i)] = car[(std::size_t) (2 * i + 1)] = saw[(std::size_t) i];
  7281. static constexpr int sizes[] { 300, 37, 128, 1, 255, 16, 299 }; // uneven blocks exercise the FIFOs
  7282. for (int done = 0, k = 0, n = 0; done < 3000; done += n, ++k)
  7283. {
  7284. n = std::min (sizes[k % 7], 3000 - done);
  7285. engine.process (mod.data() + 2 * done, mod.data() + 2 * done, car.data() + 2 * done, out.data() + 2 * done, n);
  7286. }
  7287. const int lag = engine.latencySamples();
  7288. auto maxError = 0.0f;
  7289. for (int i = lag; i < 3000; ++i)
  7290. maxError = std::max (maxError, std::abs (out[(std::size_t) (2 * i)] - saw[(std::size_t) (i - lag)]));
  7291. CHECK (maxError == 0.0f);
  7292. }
  7293. }
  7294. TEST (vocodex_soundgoodizer_stage_limits_the_output)
  7295. {
  7296. // SG > 0: the vocoder's output (loud here: Wet 2000) goes through the Soundgoodizer approximation, whose
  7297. // master limiter keeps the peaks under −0.2 dBFS; its look-ahead is reported as latency
  7298. VocodexSpec spec;
  7299. spec.params[0] = 2000;
  7300. spec.params[1] = 250;
  7301. lynx::EffectSlot slot = lynx::makeEffectSlot (lynx::EffectKind::vocodex);
  7302. slot.state = spec.bytes();
  7303. auto fx = lynx::makeEffect (slot);
  7304. fx->prepare (48000.0);
  7305. CHECK (fx->latencySamples() == 72); // 1.5 ms at 48 kHz
  7306. const auto car = vocodexSaw (48000, 110.0, 48000.0);
  7307. std::vector<float> mod (48000), l (48000, 0.0f), r (48000, 0.0f);
  7308. for (std::size_t i = 0; i < mod.size(); ++i)
  7309. mod[i] = 0.9f * (float) std::sin (0.07 * (double) i) * (float) std::sin (0.0003 * (double) i);
  7310. auto peak = 0.0f;
  7311. for (int done = 0; done < 48000; done += 512)
  7312. {
  7313. const int n = std::min (512, 48000 - done);
  7314. fx->setSidechain (mod.data() + done, mod.data() + done);
  7315. fx->setSidechain2 (car.data() + done, car.data() + done);
  7316. fx->process (l.data() + done, r.data() + done, n, slot.params, 120.0);
  7317. }
  7318. for (std::size_t i = 0; i < l.size(); ++i)
  7319. peak = std::max ({ peak, std::abs (l[i]), std::abs (r[i]) });
  7320. CHECK (peak > 0.5f && peak <= 0.9775f);
  7321. }
  7322. TEST (vocodex_tail_bypass_and_noise)
  7323. {
  7324. // both inputs silent for longer than 0.5 s + hold + release: the own input passes unchanged
  7325. VocodexSpec spec;
  7326. const auto s = lynx::vocodex::parseState (spec.bytes());
  7327. CHECK (s.has_value());
  7328. lynx::vocodex::Engine engine;
  7329. engine.prepare (*s, 44100.0, 512, false);
  7330. std::vector<float> zero (1024, 0.0f), own (1024), out (1024);
  7331. for (std::size_t i = 0; i < own.size(); ++i)
  7332. own[i] = 0.25f * (float) std::sin (0.01 * (double) i);
  7333. for (int b = 0; b < 80; ++b)
  7334. engine.process (own.data(), zero.data(), zero.data(), out.data(), 512);
  7335. CHECK (out == own);
  7336. // carrier noise: the IPP-compatible generator restarts from the same seed after a Flush
  7337. spec.params[6] = 9000;
  7338. const auto noisy = lynx::vocodex::parseState (spec.bytes());
  7339. lynx::vocodex::Engine a;
  7340. a.prepare (*noisy, 44100.0, 512, false);
  7341. std::vector<float> mod (1024), first (1024), second (1024);
  7342. for (std::size_t i = 0; i < mod.size(); ++i)
  7343. mod[i] = 0.3f * (float) std::sin (0.05 * (double) i);
  7344. a.flush();
  7345. a.process (own.data(), mod.data(), zero.data(), first.data(), 512);
  7346. a.flush();
  7347. a.process (own.data(), mod.data(), zero.data(), second.data(), 512);
  7348. CHECK (first == second);
  7349. auto energy = 0.0;
  7350. for (auto v : first)
  7351. energy += (double) v * v;
  7352. CHECK (energy > 1e-6 && std::isfinite (energy));
  7353. }
  7354. TEST (synth_spectral_oscillator)
  7355. {
  7356. // 1/n spectrum = saw: same harmonic proportions and same level as LYNX's saw
  7357. lynx::SynthSettings saw;
  7358. saw.osc[0] = { lynx::Waveform::saw, 0, 0.0f, 1.0f };
  7359. saw.cutoff = 20000.0f;
  7360. auto spectral = saw;
  7361. spectral.osc[0].wave = lynx::Waveform::spectral;
  7362. for (int n = 1; n <= lynx::SynthSettings::spectrumSize; ++n)
  7363. spectral.spectrum[(std::size_t) n - 1] = 1.0f / (float) n;
  7364. const auto a = renderSynthNote (saw, 57, 24000), b = renderSynthNote (spectral, 57, 24000); // 220 Hz
  7365. for (const auto h : { 1, 2, 3, 5 })
  7366. {
  7367. const auto ea = goertzel (a, 220.0 * h, 4800, 24000), eb = goertzel (b, 220.0 * h, 4800, 24000);
  7368. CHECK (std::abs (10.0 * std::log10 (eb / ea)) < 0.5); // within 0.5 dB
  7369. }
  7370. // odd only: no 2nd harmonic
  7371. auto odd = spectral;
  7372. for (int n = 2; n <= lynx::SynthSettings::spectrumSize; n += 2)
  7373. odd.spectrum[(std::size_t) n - 1] = 0.0f;
  7374. const auto c = renderSynthNote (odd, 57, 24000);
  7375. CHECK (goertzel (c, 440.0, 4800, 24000) < 1e-4 * goertzel (c, 220.0, 4800, 24000));
  7376. // high note: the band-limited tables don't pass Nyquist (no aliasing below the fundamental)
  7377. const auto d = renderSynthNote (spectral, 105, 24000); // 3520 Hz
  7378. CHECK (goertzel (d, 1000.0, 4800, 24000) < 1e-3 * goertzel (d, 3520.0, 4800, 24000));
  7379. }
  7380. TEST (compressor_sidechain_ducks_and_orders_mixer)
  7381. {
  7382. // the detector listens to the side input: quiet signal on the track, loud kick on the sidechain -> reduction
  7383. auto slot = lynx::makeEffectSlot (lynx::EffectKind::compressor);
  7384. slot.params[0] = 0.5f; // threshold
  7385. slot.params[1] = 1.0f; // high ratio
  7386. slot.params[4] = 0.0f;
  7387. auto run = [&] (bool withSidechain) {
  7388. auto fx = lynx::makeEffect (lynx::EffectKind::compressor);
  7389. fx->prepare (48000.0);
  7390. std::vector<float> l (4800, 0.1f), r (4800, 0.1f), kick (4800, 1.0f);
  7391. if (withSidechain)
  7392. fx->setSidechain (kick.data(), kick.data());
  7393. fx->process (l.data(), r.data(), 4800, slot.params, 120.0);
  7394. return l.back();
  7395. };
  7396. const auto alone = run (false), ducked = run (true);
  7397. CHECK (ducked < 0.5f * alone);
  7398. // order: track 5 listens to 7 via sidechain -> 7 comes before 5
  7399. auto song = lynx::Song::makeDefault();
  7400. while (song.mixer.size() < 8)
  7401. song.mixer.push_back ({});
  7402. auto comp = lynx::makeEffectSlot (lynx::EffectKind::compressor);
  7403. comp.sidechain = 7;
  7404. song.mixer[5].effects.push_back (comp);
  7405. const auto order = song.mixerOrder();
  7406. const auto at = [&] (int t) { return std::find (order.begin(), order.end(), t) - order.begin(); };
  7407. CHECK (at (7) < at (5));
  7408. }
  7409. TEST (pitch_correction_pulls_to_allowed_note)
  7410. {
  7411. auto run = [] (const lynx::EffectParams& p, double hz) {
  7412. auto fx = lynx::makeEffect (lynx::EffectKind::pitchCorrect);
  7413. fx->prepare (48000.0);
  7414. std::vector<float> l (48000 * 2), r;
  7415. for (std::size_t i = 0; i < l.size(); ++i)
  7416. l[i] = 0.5f * (float) std::sin (2.0 * 3.141592653589793 * hz * (double) i / 48000.0);
  7417. r = l;
  7418. fx->process (l.data(), r.data(), (int) l.size(), p, 120.0);
  7419. // frequency from zero crossings in the last second
  7420. int crossings = 0;
  7421. for (std::size_t i = 48000 + 1; i < l.size(); ++i)
  7422. crossings += (l[i - 1] < 0.0f) != (l[i] < 0.0f);
  7423. return crossings / 2.0;
  7424. };
  7425. auto slot = lynx::makeEffectSlot (lynx::EffectKind::pitchCorrect); // chromatic, instant, A 440
  7426. CHECK (std::abs (run (slot.params, 452.0) - 440.0) < 4.0);
  7427. for (int k = 0; k < 12; ++k)
  7428. slot.params[(std::size_t) (5 + k)] = k == 0 ? 1.0f : 0.0f; // only C
  7429. CHECK (std::abs (run (slot.params, 452.0) - 523.25) < 5.0); // C5 (523 Hz) is closer than C4 (262)
  7430. slot.params[2] = 1.0f; // turn everything off: passes unchanged
  7431. CHECK (std::abs (run (slot.params, 452.0) - 452.0) < 4.0);
  7432. }
  7433. TEST (pitch_correction_bypass_is_a_pure_delay)
  7434. {
  7435. // FL's Pitcher with Bypass All still runs its shifter: the plain path is an exact 9-sample delay, the formant
  7436. // path (256-Hann overlap-add) an exact 257-sample delay, and Gender changes nothing without a shift
  7437. // (REA_rodada30 §2.4/2.5); both are reported as latency
  7438. std::vector<float> in (48000);
  7439. for (std::size_t i = 0; i < in.size(); ++i)
  7440. in[i] = 0.5f * (float) std::sin (2.0 * 3.141592653589793 * 440.0 * (double) i / 48000.0)
  7441. + 0.2f * (float) std::sin (2.0 * 3.141592653589793 * 1234.5 * (double) i / 48000.0);
  7442. auto run = [&] (bool formant, float gender, int& latency) {
  7443. auto fx = lynx::makeEffect (lynx::EffectKind::pitchCorrect);
  7444. fx->prepare (48000.0);
  7445. auto slot = lynx::makeEffectSlot (lynx::EffectKind::pitchCorrect);
  7446. slot.params[2] = 1.0f;
  7447. slot.params[29] = formant ? 1.0f : 0.0f;
  7448. slot.params[30] = gender;
  7449. auto l = in, r = in;
  7450. for (std::size_t i = 0; i < l.size(); i += 480)
  7451. fx->process (l.data() + i, r.data() + i, 480, slot.params, 120.0);
  7452. latency = fx->latencySamples();
  7453. return l;
  7454. };
  7455. int latency = 0;
  7456. auto plain = run (false, 0.5f, latency);
  7457. CHECK (latency == 9);
  7458. auto worst = 0.0f;
  7459. for (std::size_t i = 4000; i < in.size(); ++i)
  7460. worst = std::max (worst, std::abs (plain[i] - in[i - 9]));
  7461. CHECK (worst < 1e-5f);
  7462. auto formant = run (true, 0.8f, latency);
  7463. CHECK (latency == 257);
  7464. worst = 0.0f;
  7465. for (std::size_t i = 4000; i < in.size(); ++i)
  7466. worst = std::max (worst, std::abs (formant[i] - in[i - 257]));
  7467. CHECK (worst < 1e-5f);
  7468. }
  7469. TEST (pitch_correction_formant_path)
  7470. {
  7471. // Formant on: PSOLA keeps the correction (output pitch = grain spacing) while Gender resamples the grains:
  7472. // the spectrum of a saw moves up with Gender > 0.5 and down with Gender < 0.5
  7473. auto run = [] (float gender) {
  7474. auto fx = lynx::makeEffect (lynx::EffectKind::pitchCorrect);
  7475. fx->prepare (48000.0);
  7476. auto slot = lynx::makeEffectSlot (lynx::EffectKind::pitchCorrect); // chromatic, instant, A 440
  7477. slot.params[29] = 1.0f;
  7478. slot.params[30] = gender;
  7479. std::vector<float> l (48000 * 2), r;
  7480. for (std::size_t i = 0; i < l.size(); ++i)
  7481. l[i] = 0.5f * (float) (2.0 * std::fmod (452.0 * (double) i / 48000.0, 1.0) - 1.0); // saw
  7482. r = l;
  7483. fx->process (l.data(), r.data(), (int) l.size(), slot.params, 120.0);
  7484. l.erase (l.begin(), l.begin() + 48000);
  7485. return l;
  7486. };
  7487. // period by autocorrelation (lags 60…160 = 300…800 Hz)
  7488. auto pitch = [] (const std::vector<float>& x) {
  7489. auto best = 0.0, bestLag = 0.0;
  7490. for (int lag = 60; lag <= 160; ++lag)
  7491. {
  7492. auto sum = 0.0;
  7493. for (std::size_t i = 0; i + (std::size_t) lag < x.size(); ++i)
  7494. sum += (double) x[i] * x[i + (std::size_t) lag];
  7495. if (sum > best)
  7496. {
  7497. best = sum;
  7498. bestLag = lag;
  7499. }
  7500. }
  7501. return 48000.0 / bestLag;
  7502. };
  7503. // brightness: energy of the first difference relative to the energy
  7504. auto brightness = [] (const std::vector<float>& x) {
  7505. auto e = 0.0, d = 0.0;
  7506. for (std::size_t i = 1; i < x.size(); ++i)
  7507. {
  7508. e += (double) x[i] * x[i];
  7509. d += (double) (x[i] - x[i - 1]) * (x[i] - x[i - 1]);
  7510. }
  7511. return d / e;
  7512. };
  7513. const auto neutral = run (0.5f), up = run (0.8f), down = run (0.3f);
  7514. CHECK (std::abs (pitch (neutral) - 440.0) < 5.0); // 452 Hz pulled to A
  7515. CHECK (std::abs (pitch (up) - 440.0) < 5.0); // Gender does not move the pitch
  7516. CHECK (brightness (up) > 1.5 * brightness (neutral));
  7517. CHECK (brightness (down) < 0.7 * brightness (neutral));
  7518. }
  7519. TEST (parallel_container_inner_links_modulate)
  7520. {
  7521. // one branch: utility (gain) + a control-only auto-pan LFO (1 beat); the link maps the LFO 0..1 onto the
  7522. // utility gain −24 dB..0 dB, so the level must swing; without the link it stays put
  7523. auto container = lynx::makeEffectSlot (lynx::EffectKind::parallel);
  7524. auto gain = lynx::makeEffectSlot (lynx::EffectKind::utility);
  7525. auto lfo = lynx::makeEffectSlot (lynx::EffectKind::autoPan);
  7526. lfo.params[0] = 1.0f / 64.0f;
  7527. lfo.params[4] = 1.0f; // control only: audio passes unchanged
  7528. container.branches.push_back ({ { gain, lfo }, 1.0f });
  7529. lynx::EffectLink link;
  7530. link.source = 1;
  7531. link.param = 0;
  7532. for (int i = 0; i <= 32; ++i)
  7533. link.curve.push_back (0.5f * (float) i / 32.0f);
  7534. auto levels = [&] (bool linked) {
  7535. auto slot = container;
  7536. if (linked)
  7537. slot.links.push_back (link);
  7538. auto fx = lynx::makeParallelEffect (slot);
  7539. fx->prepare (48000.0);
  7540. float lo = 1e9f, hi = 0.0f;
  7541. for (int block = 0; block < 94; ++block) // ~1 beat at 120 BPM in 256-sample blocks
  7542. {
  7543. std::vector<float> l (256, 1.0f), r (256, 1.0f);
  7544. fx->setSongPosition (block * 256.0 / 48000.0 * 2.0 * 960.0); // the engine positions every block
  7545. fx->process (l.data(), r.data(), 256, slot.params, 120.0);
  7546. lo = std::min (lo, std::abs (l[128]));
  7547. hi = std::max (hi, std::abs (l[128]));
  7548. }
  7549. return std::pair { lo, hi };
  7550. };
  7551. const auto [lo, hi] = levels (true);
  7552. CHECK (hi > 0.8f && lo < 0.12f);
  7553. const auto [lo0, hi0] = levels (false);
  7554. CHECK (std::abs (hi0 - lo0) < 1e-4f);
  7555. }
  7556. TEST (synth_spectrum_decays_per_harmonic)
  7557. {
  7558. // 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
  7559. lynx::SynthSettings s;
  7560. s.osc[0] = { lynx::Waveform::spectral, 0, 0.0f, 1.0f };
  7561. s.cutoff = 20000.0f;
  7562. s.decay = 0.0f;
  7563. s.sustain = 1.0f;
  7564. s.spectrum[0] = 1.0f;
  7565. s.spectrum[2] = 1.0f;
  7566. s.spectrumDecay[2] = 0.2f;
  7567. const auto x = renderSynthNote (s, 57, 48000); // 220 Hz, 48 kHz, 1 s
  7568. // equal 2400-sample windows (Goertzel power grows with the window): 10…60 ms and 500…550 ms
  7569. const auto early1 = goertzel (x, 220.0, 480, 2880), early3 = goertzel (x, 660.0, 480, 2880);
  7570. const auto late1 = goertzel (x, 220.0, 24000, 26400), late3 = goertzel (x, 660.0, 24000, 26400);
  7571. CHECK (early3 > 0.05 * early1); // ≈ −9 dB on average over 10…60 ms
  7572. CHECK (late3 < 1e-4 * late1);
  7573. CHECK (std::abs (10.0 * std::log10 (late1 / early1)) < 1.0); // the 1st holds
  7574. // without per-harmonic decay the spectrum stays as it is
  7575. s.spectrumDecay[2] = 0.0f;
  7576. const auto y = renderSynthNote (s, 57, 48000);
  7577. CHECK (std::abs (10.0 * std::log10 (goertzel (y, 660.0, 24000, 26400) / goertzel (y, 660.0, 480, 2880))) < 1.0);
  7578. }
  7579. TEST (worker_pool_runs_each_index_once_per_round)
  7580. {
  7581. // rounds of alternating sizes (a stalled worker must never run an index of a later round), each index
  7582. // exactly once, and nothing running after parallelFor returns
  7583. lynx::WorkerPool pool (3);
  7584. std::vector<std::atomic<int>> hits (64);
  7585. std::atomic<int> round { 0 }, outside { 0 }, wrongRound { 0 };
  7586. std::atomic<bool> open { false };
  7587. int failures = 0;
  7588. for (int r = 1; r <= 20000; ++r)
  7589. {
  7590. const auto n = (r % 3 == 0) ? 64 : (r % 3 == 1 ? 2 : 9);
  7591. for (int i = 0; i < n; ++i)
  7592. hits[(std::size_t) i].store (0);
  7593. round.store (r);
  7594. open.store (true);
  7595. auto fn = [&, r] (int i) {
  7596. if (! open.load())
  7597. outside.fetch_add (1);
  7598. if (round.load() != r)
  7599. wrongRound.fetch_add (1);
  7600. hits[(std::size_t) i].fetch_add (1);
  7601. };
  7602. pool.parallelFor (n, fn);
  7603. open.store (false);
  7604. for (int i = 0; i < n; ++i)
  7605. failures += hits[(std::size_t) i].load() != 1;
  7606. }
  7607. CHECK (failures == 0);
  7608. CHECK (outside.load() == 0);
  7609. CHECK (wrongRound.load() == 0);
  7610. }
  7611. TEST (worker_pool_wakes_sleeping_workers)
  7612. {
  7613. // rounds far apart (the workers have gone to sleep on the semaphore in between): every index still runs once,
  7614. // and the sleeping workers are woken (REA round 87: the Windows build used to poll with growing Sleep()s)
  7615. lynx::WorkerPool pool (3);
  7616. std::vector<std::atomic<int>> hits (16);
  7617. int failures = 0;
  7618. for (int r = 0; r < 12; ++r)
  7619. {
  7620. std::this_thread::sleep_for (std::chrono::microseconds (lynx::WorkerPool::spinMicros * 4));
  7621. for (auto& h : hits)
  7622. h.store (0);
  7623. auto fn = [&] (int i) {
  7624. std::this_thread::sleep_for (std::chrono::microseconds (200)); // long enough for woken workers to join
  7625. hits[(std::size_t) i].fetch_add (1);
  7626. };
  7627. pool.parallelFor (16, fn);
  7628. for (auto& h : hits)
  7629. failures += h.load() != 1;
  7630. }
  7631. CHECK (failures == 0);
  7632. CHECK (pool.wakeUps() > 0);
  7633. CHECK (pool.indicesRunByWorkers() > 0);
  7634. }
  7635. TEST (worker_pool_park_sends_spinning_workers_to_sleep)
  7636. {
  7637. // round 116: after park() (end of an audio block) the workers sleep at once instead of spinning for spinMicros, so
  7638. // the next parallelFor, well inside the spin window, has to wake them; and work right after park() still runs once
  7639. lynx::WorkerPool pool (3);
  7640. std::vector<std::atomic<int>> hits (8);
  7641. int failures = 0;
  7642. const auto before = pool.wakeUps();
  7643. for (int r = 0; r < 40; ++r)
  7644. {
  7645. for (auto& h : hits)
  7646. h.store (0);
  7647. auto fn = [&] (int i) {
  7648. std::this_thread::sleep_for (std::chrono::microseconds (100));
  7649. hits[(std::size_t) i].fetch_add (1);
  7650. };
  7651. pool.parallelFor (8, fn);
  7652. pool.park();
  7653. if (r % 2 == 0)
  7654. std::this_thread::sleep_for (std::chrono::microseconds (lynx::WorkerPool::spinMicros / 5));
  7655. for (auto& h : hits)
  7656. failures += h.load() != 1;
  7657. }
  7658. CHECK (failures == 0);
  7659. CHECK (pool.wakeUps() - before >= 20); // parked workers were woken, not found spinning
  7660. }
  7661. TEST (realtime_stats_count_deadline_misses_and_late_calls)
  7662. {
  7663. lynx::RtStats stats;
  7664. // 10 ms deadline: 3 ms, 9 ms (80-100 %), 12 ms (over), then a call 40 ms after the previous one (late)
  7665. stats.record (100.0, 3.0, 10.0);
  7666. stats.record (110.0, 9.0, 10.0);
  7667. stats.record (120.0, 12.0, 10.0);
  7668. stats.record (160.0, 2.0, 10.0);
  7669. const auto s = stats.snapshot();
  7670. CHECK (s.callbacks == 4);
  7671. CHECK (s.overBudget == 1);
  7672. CHECK (s.nearBudget == 1);
  7673. CHECK (s.lateCalls == 1);
  7674. CHECK (std::abs (s.maxLoad - 1.2) < 1e-9);
  7675. CHECK (std::abs (s.averageLoad - 26.0 / 40.0) < 1e-3);
  7676. CHECK (s.histogram[3] == 1 && s.histogram[9] == 1 && s.histogram[12] == 1 && s.histogram[2] == 1);
  7677. CHECK (std::abs (stats.takeWindowMaxLoad() - 1.2) < 1e-9 && stats.takeWindowMaxLoad() == 0.0);
  7678. // a device restart is not a late call
  7679. stats.deviceRestarted();
  7680. stats.record (5000.0, 1.0, 10.0);
  7681. CHECK (stats.snapshot().lateCalls == 1);
  7682. stats.reset();
  7683. CHECK (stats.snapshot().callbacks == 0 && stats.snapshot().maxLoad == 0.0);
  7684. }
  7685. TEST (safe_default_buffer_size)
  7686. {
  7687. const std::vector<int> wasapi { 144, 160, 192, 224, 256, 288, 320, 384, 448, 480, 512, 576, 640, 1024, 2048 };
  7688. CHECK (lynx::prefs::safeBufferSize (wasapi, 480, 48000.0) == 512); // the 10 ms default becomes 512
  7689. CHECK (lynx::prefs::safeBufferSize (wasapi, 1024, 48000.0) == 1024); // already larger: kept
  7690. CHECK (lynx::prefs::safeBufferSize (wasapi, 480, 96000.0) == 1024); // ~10.7 ms at 96 kHz
  7691. CHECK (lynx::prefs::safeBufferSize ({ 64, 128, 256 }, 128, 44100.0) == 256); // nothing reaches 512: the largest
  7692. CHECK (lynx::prefs::safeBufferSize ({}, 0, 44100.0) == 0);
  7693. }
  7694. TEST (linux_audio_driver_choice_and_pipewire_helpers)
  7695. {
  7696. // round 161: a first start on Linux opens PipeWire, then JACK, then ALSA
  7697. using lynx::prefs::DriverKind;
  7698. CHECK (lynx::prefs::driverKind ("PipeWire") == DriverKind::pipewire);
  7699. CHECK (lynx::prefs::preferredLinuxDriver ({ { "ALSA", true }, { "JACK", false }, { "PipeWire", true } }) == "PipeWire");
  7700. CHECK (lynx::prefs::preferredLinuxDriver ({ { "ALSA", true }, { "JACK", true } }) == "JACK");
  7701. CHECK (lynx::prefs::preferredLinuxDriver ({ { "ALSA", true }, { "JACK", false }, { "PipeWire", false } }) == "ALSA");
  7702. CHECK (lynx::prefs::preferredLinuxDriver ({ { "ALSA", false }, { "Bela", true } }) == "Bela");
  7703. CHECK (lynx::prefs::preferredLinuxDriver ({ { "ALSA", false } }).empty());
  7704. CHECK (lynx::prefs::pipewireLatency (256, 48000) == "256/48000");
  7705. CHECK (lynx::prefs::pipewireLatency (0, 0) == "1/1");
  7706. CHECK (lynx::prefs::pipewireChannelName ("FL", 0) == "Left");
  7707. CHECK (lynx::prefs::pipewireChannelName ("FR", 1) == "Right");
  7708. CHECK (lynx::prefs::pipewireChannelName ("MONO", 0) == "Mono");
  7709. CHECK (lynx::prefs::pipewireChannelName ("AUX0", 0) == "Aux 1");
  7710. CHECK (lynx::prefs::pipewireChannelName ("AUX15", 15) == "Aux 16");
  7711. CHECK (lynx::prefs::pipewireChannelName ("AUXX", 2) == "AUXX");
  7712. CHECK (lynx::prefs::pipewireChannelName ("", 2) == "Channel 3");
  7713. CHECK (lynx::prefs::pipewireDefaultName (R"({"name":"alsa_output.pci-0000_00_1f.3.analog-stereo"})") == "alsa_output.pci-0000_00_1f.3.analog-stereo");
  7714. CHECK (lynx::prefs::pipewireDefaultName (R"({ "name" : "a\"b" })") == "a\"b");
  7715. CHECK (lynx::prefs::pipewireDefaultName ("{}").empty());
  7716. CHECK (lynx::prefs::pipewireDefaultName (R"({"name":"unterminated)").empty());
  7717. CHECK ((lynx::prefs::uniqueDeviceNames ({ "USB Audio", "HDMI", "USB Audio", "USB Audio" })
  7718. == std::vector<std::string> { "USB Audio", "HDMI", "USB Audio (2)", "USB Audio (3)" }));
  7719. }
  7720. TEST (driver_kind_and_shared_mode_buffer_sizes)
  7721. {
  7722. // REA round 97: classic shared WASAPI mixes in whole engine periods, so sizes below it are not offered
  7723. using lynx::prefs::DriverKind;
  7724. CHECK (lynx::prefs::driverKind ("Windows Audio") == DriverKind::wasapiShared);
  7725. CHECK (lynx::prefs::driverKind ("Windows Audio (Exclusive Mode)") == DriverKind::wasapiExclusive);
  7726. CHECK (lynx::prefs::driverKind ("Windows Audio (Low Latency Mode)") == DriverKind::wasapiLowLatency);
  7727. CHECK (lynx::prefs::driverKind ("ASIO") == DriverKind::asio);
  7728. CHECK (lynx::prefs::driverKind ("LYNX ASIO") == DriverKind::lynxAsio);
  7729. CHECK (lynx::prefs::driverKind ("DirectSound") == DriverKind::directSound);
  7730. CHECK (lynx::prefs::driverKind ("ALSA") == DriverKind::alsa);
  7731. CHECK (lynx::prefs::driverKind ("JACK") == DriverKind::jack);
  7732. CHECK (lynx::prefs::driverKind ("CoreAudio") == DriverKind::coreAudio);
  7733. CHECK (lynx::prefs::driverKind ("Some Other Driver") == DriverKind::other);
  7734. const std::vector<int> offered { 144, 192, 256, 480, 512, 1024 };
  7735. CHECK ((lynx::prefs::usableBufferSizes (DriverKind::wasapiShared, offered, 480) == std::vector<int> { 480, 512, 1024 }));
  7736. CHECK (lynx::prefs::usableBufferSizes (DriverKind::wasapiExclusive, offered, 480) == offered); // the device's real periods
  7737. CHECK (lynx::prefs::usableBufferSizes (DriverKind::wasapiLowLatency, offered, 480) == offered); // engine periods
  7738. CHECK (lynx::prefs::usableBufferSizes (DriverKind::wasapiShared, { 64, 128 }, 480) == (std::vector<int> { 64, 128 })); // never empty
  7739. CHECK (lynx::prefs::usableBufferSizes (DriverKind::wasapiShared, offered, 0) == offered);
  7740. }
  7741. TEST (linear_phase_convolver_matches_direct_convolution)
  7742. {
  7743. // the block FFT convolver (cached twiddles, REA round 87) equals the direct convolution with its kernel
  7744. lynx::peqfl::FftConvolver c;
  7745. c.init (256);
  7746. std::vector<float> table (129, 1.0f);
  7747. for (int k = 40; k < 129; ++k)
  7748. table[(std::size_t) k] = 0.25f; // a high shelf cut
  7749. c.setKernel (table);
  7750. // impulse response through step()
  7751. std::vector<float> ir (600, 0.0f);
  7752. for (int i = 0; i < 600; ++i)
  7753. {
  7754. float l = i == 0 ? 1.0f : 0.0f, r = 0.0f;
  7755. c.step (l, r);
  7756. ir[(std::size_t) i] = l;
  7757. }
  7758. // then a noise signal, compared with direct convolution by that impulse response
  7759. lynx::peqfl::FftConvolver d;
  7760. d.init (256);
  7761. d.setKernel (table);
  7762. std::vector<float> x (2000), y (2000);
  7763. std::uint32_t seed = 1;
  7764. for (auto& v : x)
  7765. v = (float) ((seed = seed * 1664525u + 1013904223u) >> 8) / 16777216.0f - 0.5f;
  7766. for (std::size_t i = 0; i < x.size(); ++i)
  7767. {
  7768. float l = x[i], r = 0.0f;
  7769. d.step (l, r);
  7770. y[i] = l;
  7771. }
  7772. double worst = 0.0;
  7773. for (std::size_t i = 0; i < x.size(); ++i)
  7774. {
  7775. double direct = 0.0;
  7776. for (std::size_t k = 0; k < ir.size() && k <= i; ++k)
  7777. direct += (double) ir[k] * (double) x[i - k];
  7778. worst = std::max (worst, std::abs (direct - (double) y[i]));
  7779. }
  7780. CHECK (worst < 1e-4);
  7781. // and it delays by 3n/4 with the kernel's centre there
  7782. 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());
  7783. CHECK (peak == 192);
  7784. }
  7785. TEST (patcher_container_is_reused_across_snapshots)
  7786. {
  7787. // an unchanged Patcher keeps its built effect (its children's tails, delay lines and PDC stay put), and a
  7788. // linear-phase Frequency Splitter reports its latency before its first block (REA round 87: it used to be
  7789. // rebuilt on every snapshot and its latency appeared one block late, re-planning PDC in a loop)
  7790. auto song = lynx::Song::makeDefault();
  7791. if (song.mixer.size() < 2)
  7792. song.mixer.resize (2);
  7793. lynx::EffectSlot splitter;
  7794. splitter.kind = lynx::EffectKind::frequencySplitter;
  7795. splitter.params = lynx::makeEffectSlot (lynx::EffectKind::frequencySplitter).params;
  7796. splitter.params[4] = 0.6f; // linear-phase mode
  7797. splitter.params[6] = 0.0f; // quality 0
  7798. lynx::EffectSlot patcher;
  7799. patcher.kind = lynx::EffectKind::parallel;
  7800. patcher.branches.push_back ({ { splitter }, 1.0f });
  7801. patcher.branches.push_back ({ {}, 0.5f });
  7802. song.mixer[1].effects = { patcher };
  7803. const auto a = lynx::RackState::build (song, nullptr, 44100.0);
  7804. const auto b = lynx::RackState::build (song, a.get(), 44100.0);
  7805. CHECK (a->effects[1][0] == b->effects[1][0]);
  7806. CHECK (a->effects[1][0]->latencySamples() == 384); // 3/4 of the 512-point FFT, before any block
  7807. CHECK (a->latencies == a->currentLatencies());
  7808. song.mixer[1].effects[0].branches[0].chain[0].params[0] = 0.25f; // a child's parameter changed: rebuilt
  7809. const auto c = lynx::RackState::build (song, b.get(), 44100.0);
  7810. CHECK (c->effects[1][0] != b->effects[1][0]);
  7811. song.mixer[1].effects[0].branches[1].gain = 0.75f;
  7812. const auto d = lynx::RackState::build (song, c.get(), 44100.0);
  7813. CHECK (d->effects[1][0] != c->effects[1][0]);
  7814. }
  7815. TEST (ladder_filter_follows_toxic_law)
  7816. {
  7817. // the 4-pole ladder: −24 dB/oct far above the cutoff (vs −12 for the SVF), resonance raises the corner, and
  7818. // the input saturation keeps it bounded even at full resonance
  7819. auto level = [] (bool ladder, float res, double hz) {
  7820. lynx::SynthSettings s;
  7821. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  7822. s.cutoff = 500.0f;
  7823. s.resonance = res;
  7824. s.ladder = ladder;
  7825. const auto key = (int) std::lround (69.0 + 12.0 * std::log2 (hz / 440.0));
  7826. const auto x = renderSynthNote (s, key, 48000);
  7827. double e = 0.0, peak = 0.0;
  7828. for (std::size_t i = 24000; i < 48000; ++i)
  7829. e += (double) x[i] * x[i], peak = std::max (peak, (double) std::abs (x[i]));
  7830. return std::pair { std::sqrt (e / 24000.0), peak };
  7831. };
  7832. 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
  7833. const auto svfHigh = level (false, 0.0f, 3520.0).first / level (false, 0.0f, 110.0).first;
  7834. CHECK (20.0 * std::log10 (high / low) < -55.0); // ≈ 24 dB/oct over ~2.8 octaves
  7835. CHECK (20.0 * std::log10 (svfHigh) > -45.0); // the 2-pole SVF falls much less
  7836. const auto corner0 = level (true, 0.0f, 523.25).first, corner1 = level (true, 0.6f, 523.25).first;
  7837. CHECK (corner1 > 1.5 * corner0); // resonance boosts near the cutoff
  7838. const auto loud = level (true, 1.0f, 523.25);
  7839. CHECK (std::isfinite (loud.first) && loud.second < 20.0); // self-oscillation stays bounded
  7840. }
  7841. TEST (synth_spectrum_settles_on_target)
  7842. {
  7843. // ALT DECAY-like morph: harmonic 3 starts at 1 and settles on 0.25 (−12 dB) instead of fading out
  7844. lynx::SynthSettings s;
  7845. s.osc[0] = { lynx::Waveform::spectral, 0, 0.0f, 1.0f };
  7846. s.cutoff = 20000.0f;
  7847. s.decay = 0.0f;
  7848. s.sustain = 1.0f;
  7849. s.spectrum[0] = 1.0f;
  7850. s.spectrum[2] = 1.0f;
  7851. s.spectrumDecay[2] = 0.1f;
  7852. s.spectrumTarget[2] = 0.25f;
  7853. const auto x = renderSynthNote (s, 57, 48000);
  7854. const auto ratio = goertzel (x, 660.0, 24000, 26400) / goertzel (x, 220.0, 24000, 26400);
  7855. CHECK (std::abs (10.0 * std::log10 (ratio) - 20.0 * std::log10 (0.25)) < 1.0);
  7856. }
  7857. TEST (kepler_chorus_is_stereo_and_keeps_level)
  7858. {
  7859. // chorus I on a sine: L and R differ (opposite LFO phases), the level stays close to the dry one
  7860. lynx::SynthSettings s;
  7861. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  7862. s.cutoff = 20000.0f;
  7863. auto dry = s;
  7864. s.chorus = 1;
  7865. auto render = [] (const lynx::SynthSettings& set) {
  7866. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  7867. inst->prepare (48000.0);
  7868. lynx::ChannelDef c;
  7869. c.synth = set;
  7870. inst->configure (c, 120.0);
  7871. inst->noteOn (69, 1.0f);
  7872. std::vector<float> l (96000), r (96000);
  7873. inst->render (l.data(), r.data(), 96000, 1.0f, 1.0f);
  7874. return std::pair { l, r };
  7875. };
  7876. const auto [l, r] = render (s);
  7877. const auto [dl, dr] = render (dry);
  7878. double diff = 0.0, e = 0.0, ed = 0.0;
  7879. for (std::size_t i = 48000; i < 96000; ++i)
  7880. diff += std::abs (l[i] - r[i]), e += (double) l[i] * l[i], ed += (double) dl[i] * dl[i];
  7881. CHECK (diff / 48000.0 > 1e-3);
  7882. CHECK (std::abs (10.0 * std::log10 (e / ed)) < 3.0);
  7883. }
  7884. TEST (fl_pan_law_matches_love_philter_dll)
  7885. {
  7886. // values printed by the installation's Love Philter pan routine for vol 0.8 (REA_rodada27 §3.1, lppan.txt)
  7887. struct Row { double pan, ll, rr, rl, lr; };
  7888. const Row rows[] = { { -1.5, 0.239782482, -0.432956904, 0.805467963, 0.0 }, { -0.5, 0.805467963, 0.432956904, 0.239782482, 0.0 },
  7889. { 0.0, 0.8, 0.8, 0.0, 0.0 }, { 0.75, 0.220719516, 0.707889318, 0.0, 0.401742548 },
  7890. { 1.0, 0.0, 0.565685451, 0.0, 0.565685451 } };
  7891. for (const auto& row : rows)
  7892. {
  7893. float ll, rl, lr, rr;
  7894. lynx::flPanMatrix (row.pan, ll, rl, lr, rr);
  7895. CHECK (std::abs (0.8 * ll - row.ll) < 1e-6 && std::abs (0.8 * rr - row.rr) < 1e-6);
  7896. CHECK (std::abs (0.8 * rl - row.rl) < 1e-6 && std::abs (0.8 * lr - row.lr) < 1e-6);
  7897. }
  7898. }
  7899. TEST (effector_units_match_fl_dll_impulse_responses)
  7900. {
  7901. // impulse responses of the installation's Effector engine (REA_rodada27 §2, eu_<kind>_<X>_<Y>.txt; LFO off,
  7902. // wet 1, blocks of 256): samples 1, 10, 100 and 1000
  7903. struct Case { int kind; float x, y; double v[4]; };
  7904. const Case cases[] = { { 3, 0.5f, 0.5f, { -0.603055418, 0.159798339, -0.0055043716, -1.06241259e-06 } },
  7905. { 4, 0.2f, 0.5f, { 4.68885949e-26, 4.20032622e-19, 0.000487731799, 2.27715846e-09 } },
  7906. { 4, 0.8f, 0.3f, { -0.293783396, -0.0306414738, -0.00068128563, 1.358577e-15 } } };
  7907. for (const auto& c : cases)
  7908. {
  7909. auto fx = lynx::effector::make();
  7910. fx->prepare (44100.0);
  7911. lynx::EffectParams p {};
  7912. p[lynx::effector::type] = c.kind / 11.0f;
  7913. p[lynx::effector::wetMix] = 1.0f;
  7914. p[lynx::effector::paramX] = c.x;
  7915. p[lynx::effector::paramY] = c.y;
  7916. p[lynx::effector::modX] = p[lynx::effector::modY] = 0.5f;
  7917. p[lynx::effector::modRate] = 0.5f;
  7918. p[lynx::effector::inputLevel] = 1.0f;
  7919. p[lynx::effector::outputGain] = 0.5f;
  7920. std::vector<float> l (1024, 0.0f), r (1024, 0.0f);
  7921. l[0] = r[0] = 1.0f;
  7922. for (int d = 0; d < 1024; d += 256)
  7923. fx->process (l.data() + d, r.data() + d, 256, p, 120.0);
  7924. const int at[4] { 1, 10, 100, 1000 };
  7925. for (int k = 0; k < 4; ++k)
  7926. CHECK (std::abs (l[(std::size_t) at[k]] - c.v[k]) < 2e-7 + 1e-5 * std::abs (c.v[k]));
  7927. }
  7928. }
  7929. TEST (harmless_stereo_tremolo_and_vibrato)
  7930. {
  7931. // tremolo d = 1: each side swings between 0 and 1.25 at 5 Hz, L and R in anti-phase; vibrato modulates the pitch
  7932. lynx::SynthSettings s;
  7933. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  7934. s.cutoff = 20000.0f;
  7935. s.stereoTremolo = 1.0f;
  7936. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  7937. inst->prepare (48000.0);
  7938. lynx::ChannelDef c;
  7939. c.synth = s;
  7940. inst->configure (c, 120.0);
  7941. inst->noteOn (69, 1.0f);
  7942. std::vector<float> l (48000), r (48000);
  7943. inst->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
  7944. 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; };
  7945. // θ = 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
  7946. CHECK (envelope (l, 19200) > 4.0f * envelope (r, 19200));
  7947. CHECK (envelope (r, 24000) > 4.0f * envelope (l, 24000));
  7948. // vibrato 100 cents: zero crossings vary in spacing; without it they are regular
  7949. auto crossingsSpread = [] (float cents) {
  7950. lynx::SynthSettings v;
  7951. v.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  7952. v.cutoff = 20000.0f;
  7953. v.vibratoCents = cents;
  7954. const auto x = renderSynthNote (v, 69, 48000);
  7955. std::vector<int> zc;
  7956. for (int i = 24000; i < 48000; ++i)
  7957. if (x[(std::size_t) i - 1] < 0.0f && x[(std::size_t) i] >= 0.0f)
  7958. zc.push_back (i);
  7959. int lo = 1 << 30, hi = 0;
  7960. for (std::size_t k = 1; k < zc.size(); ++k)
  7961. lo = std::min (lo, zc[k] - zc[k - 1]), hi = std::max (hi, zc[k] - zc[k - 1]);
  7962. return hi - lo;
  7963. };
  7964. CHECK (crossingsSpread (0.0f) <= 1);
  7965. CHECK (crossingsSpread (100.0f) >= 4);
  7966. }
  7967. TEST (physical_models_sound_and_end)
  7968. {
  7969. // Sakura: a plucked string with default-ish parameters rings and fades after the release; Drumpad: a hit decays
  7970. for (const auto kind : { lynx::InstrumentKind::sakura, lynx::InstrumentKind::drumpad })
  7971. {
  7972. lynx::ChannelDef c;
  7973. c.instrument = kind;
  7974. if (kind == lynx::InstrumentKind::sakura)
  7975. {
  7976. c.modelParams.assign (146, 0.5f);
  7977. c.modelParams[111] = 3.0f; // both strings at the note
  7978. c.modelParams[113] = 0.5f; // octave ×1
  7979. c.modelParams[6] = c.modelParams[11] = 0.99f; // long decay
  7980. c.modelParams[31] = 0.0f; // noise exciter
  7981. }
  7982. else
  7983. {
  7984. c.modelParams.assign (38, 0.5f);
  7985. c.modelParams[0] = 1.0f; // retrigger noise
  7986. c.modelParams[30] = c.modelParams[31] = c.modelParams[32] = c.modelParams[33] = 0.0f; // no velocity morph
  7987. }
  7988. auto inst = lynx::makeInstrument (kind);
  7989. inst->prepare (48000.0);
  7990. inst->configure (c, 120.0);
  7991. inst->noteOn (60, 0.8f);
  7992. std::vector<float> l (48000), r (48000);
  7993. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  7994. inst->noteOff (60);
  7995. inst->render (l.data() + 24000, r.data() + 24000, 24000, 1.0f, 1.0f);
  7996. double early = 0.0;
  7997. for (int i = 0; i < 12000; ++i)
  7998. early += (double) l[(std::size_t) i] * l[(std::size_t) i];
  7999. CHECK (early > 1e-6);
  8000. CHECK (std::all_of (l.begin(), l.end(), [] (float v) { return std::isfinite (v); }));
  8001. std::vector<float> tail (480000), tailR (480000);
  8002. inst->render (tail.data(), tailR.data(), 480000, 1.0f, 1.0f); // 10 s later the voice has ended
  8003. CHECK (inst->idle());
  8004. }
  8005. }
  8006. TEST (squeeze_matches_fl_model)
  8007. {
  8008. // Fruity Squeeze: Steps quantises, Mix 0 passes the input dry ×Volume/127; a silent block is left untouched
  8009. auto slot = lynx::makeEffectSlot (lynx::EffectKind::squeeze);
  8010. slot.params[1] = 0.0f; // mix 0: dry only
  8011. auto fx = lynx::makeEffect (lynx::EffectKind::squeeze);
  8012. fx->prepare (44100.0);
  8013. std::vector<float> l (512, 0.3f), r (512, -0.2f);
  8014. fx->process (l.data(), r.data(), 512, slot.params, 120.0);
  8015. CHECK (std::abs (l[100] - 0.3f) < 1e-6f && std::abs (r[100] + 0.2f) < 1e-6f); // Volume 127/127
  8016. slot.params[1] = 1.0f; // wet only
  8017. slot.params[2] = 32000.0f / 32768.0f; // 32768^(1 − 32000/32768) ≈ 1.27 levels per unit → heavy quantisation
  8018. std::vector<float> a (512), b (512);
  8019. for (int i = 0; i < 512; ++i)
  8020. a[(std::size_t) i] = b[(std::size_t) i] = 0.4f * (float) std::sin (i * 0.05);
  8021. fx->process (a.data(), b.data(), 512, slot.params, 120.0);
  8022. const auto levels = std::pow (32768.0f, 1.0f - 32000.0f / 32768.0f);
  8023. for (int i = 0; i < 512; i += 37)
  8024. CHECK (std::abs (a[(std::size_t) i] * levels - std::nearbyint (a[(std::size_t) i] * levels)) < 1e-3f);
  8025. std::vector<float> z (512, 0.0f), z2 (512, 0.0f);
  8026. fx->process (z.data(), z2.data(), 512, slot.params, 120.0);
  8027. CHECK (z[10] == 0.0f);
  8028. }
  8029. TEST (sakura_matrix_and_effects)
  8030. {
  8031. // REA_rodada29 §1: velocity → level follows 10^(2m)·0.05 (vel 1 vs 0.5 → ×10 in amplitude); the delay leaves
  8032. // an echo after the note has ended and keeps the instrument from going idle until its tail is silent
  8033. auto base = [] {
  8034. lynx::ChannelDef c;
  8035. c.instrument = lynx::InstrumentKind::sakura;
  8036. c.modelParams.assign (146, 0.0f);
  8037. const std::pair<int, float> values[] { { 0, 0.375f }, { 1, 0.5f }, { 3, 0.5f }, { 4, 1.0f }, { 6, 0.95f }, { 9, 0.3f },
  8038. { 11, 0.95f }, { 14, 0.3f }, { 18, 0.5f }, { 19, 0.5f }, { 27, 0.5f }, { 28, 1.0f },
  8039. { 29, 0.7f }, { 31, 1.0f }, { 32, 0.5f }, { 111, 3.0f }, { 113, 0.5f } };
  8040. for (const auto& [i, v] : values)
  8041. c.modelParams[(std::size_t) i] = v;
  8042. return c;
  8043. };
  8044. auto render = [] (const lynx::ChannelDef& c, float velocity, int held, int total) {
  8045. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sakura);
  8046. inst->prepare (48000.0);
  8047. inst->configure (c, 120.0);
  8048. inst->noteOn (60, velocity);
  8049. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  8050. for (int s = 0; s < total; s += 256)
  8051. {
  8052. if (s == held)
  8053. inst->noteOff (60);
  8054. inst->render (l.data() + s, r.data() + s, std::min (256, total - s), 1.0f, 1.0f);
  8055. }
  8056. return std::make_pair (l, std::move (inst));
  8057. };
  8058. auto energy = [] (const std::vector<float>& x, int from, int to) {
  8059. double e = 0.0;
  8060. for (int i = from; i < to; ++i)
  8061. e += (double) x[(std::size_t) i] * x[(std::size_t) i];
  8062. return e;
  8063. };
  8064. auto c = base();
  8065. c.modelParams[79] = 6.0f; // slot 1: velocity → level, amount 1
  8066. c.modelParams[80] = 1.0f;
  8067. c.modelParams[82] = 1.0f;
  8068. const auto loud = energy (render (c, 1.0f, 24064, 24064).first, 0, 24064);
  8069. const auto soft = energy (render (c, 0.5f, 24064, 24064).first, 0, 24064);
  8070. CHECK (loud > 1e-6 && std::abs (loud / soft - 100.0) < 1.0);
  8071. auto d = base();
  8072. auto [dry, dryInst] = render (d, 0.8f, 4864, 72000);
  8073. d.modelParams[63] = 1.0f; // delay on: time 0.5 → ≈ 0.52 s, wet 1, no feedback: one echo
  8074. d.modelParams[64] = 0.5f;
  8075. d.modelParams[69] = 1.0f;
  8076. auto [wet, wetInst] = render (d, 0.8f, 4864, 72000);
  8077. CHECK (energy (wet, 26000, 40000) > 100.0 * energy (dry, 26000, 40000) + 1e-9); // the note ends near 0.2 s
  8078. CHECK (dryInst->idle() || energy (dry, 70000, 72000) < 1e-12);
  8079. CHECK (! wetInst->idle()); // the echo tail has not been silent for 3 s yet
  8080. }
  8081. TEST (drumsynth_live_engine)
  8082. {
  8083. // REA_rodada30 §1: key → patch slot, state parsing, the engine against our reference model (no DLL: synthesised
  8084. // sources only, noise from the fallback seed), voice modes, patch level/pan
  8085. CHECK (lynx::drumSynthKeyToSlot (48) == 0 && lynx::drumSynthKeyToSlot (71) == 23 && lynx::drumSynthKeyToSlot (0) == 24);
  8086. CHECK (lynx::drumSynthKeyToSlot (47) == 71 && lynx::drumSynthKeyToSlot (72) == 72 && lynx::drumSynthKeyToSlot (119) == 119);
  8087. CHECK (lynx::drumSynthKeyToSlot (120) == -1);
  8088. // a version 3 state: 120 names, 120 × 38 ints (send track −1 becomes 0 below version 4), flags
  8089. std::vector<std::uint8_t> blob;
  8090. auto put = [&] (std::uint32_t v) { for (int k = 0; k < 4; ++k) blob.push_back ((std::uint8_t) (v >> (8 * k))); };
  8091. put (3);
  8092. put (66);
  8093. for (int i = 0; i < 120; ++i)
  8094. blob.push_back (2), blob.push_back ('a'), blob.push_back ((std::uint8_t) ('0' + i % 10));
  8095. for (int i = 0; i < 120 * 38; ++i)
  8096. put ((std::uint32_t) (i % 38 == 36 ? -1 : i % 38 == 4 ? 300 : 0));
  8097. put (0);
  8098. const auto parsed = lynx::drumSynthLiveParams (blob);
  8099. CHECK (parsed && (int) parsed->size() == lynx::drumSynthParamCount);
  8100. CHECK (parsed && (*parsed)[36] == 0.0f && (*parsed)[38 * 7 + 4] == 300.0f && (*parsed)[(std::size_t) lynx::drumSynthEditorKeyIndex] == 66.0f);
  8101. blob.resize (500);
  8102. CHECK (! lynx::drumSynthLiveParams (blob));
  8103. // patch on key 60 (slot 12): tone-mode Osc1 with sweep and click, noise-band Osc2, ring, 2 retriggers
  8104. 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,
  8105. 0, 0, 0, 0, 0, 0, 30, -1, 0, 0, 64 };
  8106. lynx::ChannelDef c;
  8107. c.instrument = lynx::InstrumentKind::drumsynth;
  8108. c.modelParams.assign ((std::size_t) lynx::drumSynthParamCount, 0.0f);
  8109. for (int s = 0; s < 120; ++s)
  8110. c.modelParams[(std::size_t) (38 * s + 15)] = c.modelParams[(std::size_t) (38 * s + 34)] = -1.0f;
  8111. for (int i = 0; i < 38; ++i)
  8112. c.modelParams[(std::size_t) (38 * 12 + i)] = (float) patch[i];
  8113. auto render = [] (const lynx::ChannelDef& def, std::vector<int> keys, int total, float gl = 1.0f, float gr = 1.0f) {
  8114. auto inst = lynx::makeInstrument (lynx::InstrumentKind::drumsynth);
  8115. inst->configure (def, 120.0);
  8116. inst->prepare (48000.0);
  8117. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  8118. for (int s = 0; s < total; s += 512)
  8119. {
  8120. for (std::size_t k = 0; k + 1 < keys.size(); k += 2)
  8121. if (keys[k + 1] == s)
  8122. inst->noteOn (keys[k], 100.0f / 128.0f);
  8123. inst->render (l.data() + s, r.data() + s, std::min (512, total - s), gl, gr);
  8124. }
  8125. return std::make_tuple (l, r, std::move (inst));
  8126. };
  8127. {
  8128. auto [l, r, inst] = render (c, { 60, 0 }, 9600);
  8129. // reference model (REA round 30 dsl_model.py, seed 0x56CE, FL velocity 100, V = 1, centre: × g(100)·cos(π/4))
  8130. const std::pair<int, float> golden[] { { 0, -0.0074092713f }, { 10, -0.13961405f }, { 100, -0.47290668f }, { 1000, -0.29188696f },
  8131. { 3000, -0.22130175f }, { 6000, -0.049537566f }, { 9599, 0.010392102f } };
  8132. for (const auto& [i, v] : golden)
  8133. CHECK (std::abs (l[(std::size_t) i] - v) < 2e-6f && std::abs (r[(std::size_t) i] - v) < 2e-6f);
  8134. std::vector<float> tail (48000 * 4), tailR (tail.size());
  8135. inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
  8136. CHECK (inst->idle()); // the engine reported the voice finished
  8137. }
  8138. auto energy = [] (const std::vector<float>& x, int from, int to) {
  8139. double e = 0.0;
  8140. for (int i = from; i < to; ++i)
  8141. e += (double) x[(std::size_t) i] * x[(std::size_t) i];
  8142. return e;
  8143. };
  8144. // a key without a patch level (slot 0, level 0) and a key outside the 120 patches are silent
  8145. CHECK (energy (std::get<0> (render (c, { 48, 0, 125, 0 }, 4096)), 0, 4096) == 0.0);
  8146. // mono: a second hit on the same key cuts the first; poly: both ring together (Osc2 off: no noise, deterministic)
  8147. auto tone = c;
  8148. tone.modelParams[38 * 12 + 14] = 0.0f;
  8149. const auto single = std::get<0> (render (tone, { 60, 0 }, 8192));
  8150. const auto mono = std::get<0> (render (tone, { 60, 0, 60, 2048 }, 8192));
  8151. auto poly = tone;
  8152. poly.modelParams[38 * 12 + 24] = 1.0f;
  8153. const auto both = std::get<0> (render (poly, { 60, 0, 60, 2048 }, 8192));
  8154. double monoDiff = 0.0, polyDiff = 0.0;
  8155. for (int i = 2048 + 300; i < 8192; ++i) // after the cut fade: mono = only the second hit, a copy of the first
  8156. 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]);
  8157. CHECK (monoDiff < 1e-3 && polyDiff > 1.0);
  8158. // patch pan +64 → right only; channel volume counts twice (FL applies Final.Vol in the engine and at the host)
  8159. auto right = c;
  8160. right.modelParams[38 * 12 + 1] = 64.0f;
  8161. const auto [pl, pr, pi] = render (right, { 60, 0 }, 4096);
  8162. CHECK (energy (pl, 0, 4096) < 1e-12 * energy (pr, 0, 4096) && energy (pr, 0, 4096) > 1.0);
  8163. const auto full = std::get<0> (render (tone, { 60, 0 }, 4096));
  8164. const auto half = std::get<0> (render (tone, { 60, 0 }, 4096, 0.5f, 0.5f));
  8165. CHECK (std::abs (half[100] - 0.25f * full[100]) < 1e-6f && std::abs (full[100]) > 0.01f);
  8166. }
  8167. TEST (kepler_lfo_bpm_sync)
  8168. {
  8169. // 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)
  8170. std::vector<std::uint8_t> state (0x21 + 34 * 4, 0);
  8171. auto putU32 = [&] (std::size_t o, std::uint32_t v) { std::memcpy (state.data() + o, &v, 4); };
  8172. putU32 (12, 9);
  8173. std::memcpy (state.data() + 16, "Kepler-1", 9);
  8174. putU32 (0x19, 4);
  8175. putU32 (0x1D, 34);
  8176. float k[34] {};
  8177. k[5] = 0.602085769f; // "Pads - Kepler" demo: n = 286
  8178. k[6] = 1.0f;
  8179. k[10] = 1.0f; // vibrato on
  8180. k[33] = 1.0f;
  8181. std::memcpy (state.data() + 0x21, k, sizeof k);
  8182. lynx::SynthSettings s;
  8183. float gain = 0.0f;
  8184. CHECK (lynx::synthFromKepler (state, s, gain));
  8185. CHECK (s.lfo[lynx::lfoPitch].sync && s.lfo[lynx::lfoPitch].secondsTiming);
  8186. CHECK (std::abs (s.lfo[lynx::lfoPitch].period - 286.0f / 192.0f) < 1e-6f);
  8187. k[5] = 0.0f; // slowest: 32 beats
  8188. std::memcpy (state.data() + 0x21, k, sizeof k);
  8189. CHECK (lynx::synthFromKepler (state, s, gain) && std::abs (s.lfo[lynx::lfoPitch].period - 32.0f) < 1e-6f);
  8190. k[6] = 0.0f; // free rate in seconds
  8191. std::memcpy (state.data() + 0x21, k, sizeof k);
  8192. CHECK (lynx::synthFromKepler (state, s, gain) && ! s.lfo[lynx::lfoPitch].sync);
  8193. }
  8194. namespace
  8195. {
  8196. // A Harmor state written from scratch (REA_rodada11 §4.1 layout, version 18): part A only — a decaying-harmonics
  8197. // timbre, low-pass + resonance, filter envelope, 3-voice unison, vibrato, pluck, tremolo, volume envelope with a
  8198. // velocity map — and the global block. Every other curve is neutral.
  8199. // features (REA_rodada75): 1 arpeggio markers on the volume envelope, 2 noise resonance on filter 1, 4 "pluck replaces
  8200. // blur decay", 8 pluck blend, 16 harmonic randomness, 32 phaser + harmonizer masks
  8201. std::vector<std::uint8_t> harmorTestState (bool panLfo = true, bool image = false, int features = 0)
  8202. {
  8203. std::vector<std::uint8_t> b;
  8204. 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))); };
  8205. 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))); };
  8206. struct Pt { double dx, y, tension; int mode; int arp = 0; };
  8207. auto curve = [&] (const std::vector<Pt>& pts, std::array<std::int32_t, 5> hdr) {
  8208. i32 (3), i32 (3), i32 ((std::int32_t) pts.size());
  8209. for (const auto& p : pts)
  8210. {
  8211. f64 (p.dx), f64 (p.y);
  8212. const auto t = (float) p.tension;
  8213. std::uint32_t u;
  8214. std::memcpy (&u, &t, 4);
  8215. i32 ((std::int32_t) u);
  8216. b.push_back ((std::uint8_t) p.mode), b.push_back ((std::uint8_t) p.arp), b.push_back (0), b.push_back (0);
  8217. }
  8218. for (auto h : hdr)
  8219. i32 (h);
  8220. };
  8221. const std::array<std::int32_t, 5> off { 0, 0, -1, -1, -1 }, on { 0, 1, -1, -1, -1 };
  8222. 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); };
  8223. i32 (18);
  8224. i32 (1 | 4);
  8225. std::vector<std::uint8_t> part;
  8226. std::swap (b, part);
  8227. std::array<std::int32_t, 386> p {};
  8228. const std::pair<int, int> values[] = { { 0, 500 }, { 3, 1000 }, { 9, 1024 }, { 11, 1000 }, { 12, 1000 }, { 17, 300 }, { 18, 4000 },
  8229. { 19, 500 }, { 20, 1000 }, { 24, 500 }, { 25, 500 }, { 28, 500 }, { 29, 256 }, { 30, -1 }, { 31, -1 }, { 33, -1 }, { 34, 3 },
  8230. { 35, 1 }, { 37, 192 }, { 38, 128 }, { 39, 500 }, { 40, 10000 }, { 41, 1 }, { 42, 10000 }, { 43, 1 }, { 45, 200 }, { 46, 500 },
  8231. { 47, 1 }, { 49, -140 }, { 50, 1200 }, { 51, 0 }, { 52, 5000 }, { 54, 500 }, { 56, 600 }, { 57, 300 }, { 59, 500 }, { 60, 600 },
  8232. { 64, 8192 }, { 66, 500 }, { 72, 600 }, { 74, 1000 }, { 75, 600 }, { 78, 5000 }, { 79, 500 }, { 80, 3000 } };
  8233. for (const auto& [i, v] : values)
  8234. p[(std::size_t) i] = v;
  8235. if ((features & 2) != 0)
  8236. p[57] = 400, p[59] = 300; // filter-1 resonance amount and width (noise type set in the combo block)
  8237. if ((features & 4) != 0)
  8238. p[75] = 700, p[21] = 300; // pluck value, time-blur attack
  8239. if ((features & 8) != 0)
  8240. p[75] = 650, p[4] = 600; // pluck, timbre mix
  8241. if ((features & 32) != 0)
  8242. p[76] = 700, p[27] = 600; // phaser mix, harmonizer mix
  8243. if (image) // image synthesis (REA_rodada75): speed, cubic gain / linear freq interpolation, Hz freq mode, formant
  8244. for (const auto& [i, v] : std::initializer_list<std::pair<int, int>> { { 82, 200 }, { 83, 100000 }, { 84, 10000 }, { 85, 20000 },
  8245. { 86, 750 }, { 87, 4 }, { 88, 600 }, { 89, 900 }, { 90, 1 }, { 91, 3 }, { 92, 1500 }, { 93, -300 }, { 94, 800 } })
  8246. p[(std::size_t) i] = v;
  8247. for (auto v : p)
  8248. i32 (v);
  8249. for (int i = 0; i < 12; ++i)
  8250. i32 (i); // module order
  8251. std::array<std::uint8_t, 0x4b> misc {};
  8252. auto put = [&] (int o, std::int32_t v) { std::memcpy (misc.data() + o, &v, 4); };
  8253. const float one = 1.0f;
  8254. put (0x08, 107), put (0x1c, 1), put (0x28, 200), std::memcpy (misc.data() + 0x30, &one, 4);
  8255. put (0x34, 0), put (0x38, 4), put (0x3c, (features & 2) != 0 ? 109 : 100), put (0x40, 108);
  8256. if ((features & 8) != 0)
  8257. put (0x00, 5);
  8258. misc[0x44] = 1;
  8259. if ((features & 4) != 0)
  8260. misc[0x46] = 1;
  8261. b.insert (b.end(), misc.begin(), misc.end());
  8262. 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 };
  8263. for (int k = 0; k < 43; ++k)
  8264. {
  8265. static const int mode2[] = { 2, 14, 23, 28, 30, 38, 39, 41, 42 }; // ×2 around 0.5: 1.0 is neutral
  8266. const bool centred = std::find (std::begin (mode1), std::end (mode1), k) != std::end (mode1);
  8267. const bool doubled = std::find (std::begin (mode2), std::end (mode2), k) != std::end (mode2);
  8268. const double neutral = centred && ! doubled ? 0.5 : 1.0;
  8269. i32 (3);
  8270. i32 (1065353216), i32 (1065353216), i32 (1065353216);
  8271. if (k == 1 && (features & 1) != 0) // arpeggio volume envelope: a looping pattern with chord markers
  8272. 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 },
  8273. { 0.1, 0, 0.2, 0 } },
  8274. { 0, 1, -1, 0, 4 });
  8275. else if (k == 1) // volume envelope: attack, decay to the sustain point, release
  8276. 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 });
  8277. else if (k == 4) // filter-1 envelope
  8278. curve ({ { 0, 1, 0, 0 }, { 0.25, 0.2, -0.5, 0 } }, { 0, 1, -1, -1, -1 });
  8279. else
  8280. flat (neutral);
  8281. for (int a : { 128, 128, 0, 128 })
  8282. i32 (a);
  8283. if (k == 0 && panLfo) // pan LFO
  8284. flat (1.0, on);
  8285. else
  8286. flat (0.5);
  8287. for (int a : { k == 0 ? 40000 : 0, 0, 0, 0 })
  8288. i32 (a);
  8289. i32 (0);
  8290. for (int m = 2; m < 11; ++m)
  8291. if (k == 1 && m == 3)
  8292. curve ({ { 0, 0.2, 0, 0 }, { 1, 1, -0.3, 0 } }, off); // velocity → volume
  8293. else
  8294. flat (neutral);
  8295. }
  8296. for (int c = 0; c < 26; ++c)
  8297. if ((features & 16) != 0 && c == 4)
  8298. curve ({ { 0, 0.5, 0, 0 }, { 0.2, 0.8, 0.3, 0 }, { 0.8, 0.3, 0, 0 } }, off); // harmonic randomness
  8299. else if ((features & 32) != 0 && (c == 22 || c == 23))
  8300. curve ({ { 0, 0.3, 0, 0 }, { 0.6, 1.0, 0.2, 0 }, { 0.4, 0.5, 0, 0 } }, off); // phaser / harmonizer masks
  8301. else if (c == 3 && (features & 8) != 0)
  8302. curve ({ { 0, 0.2, 0, 0 }, { 1, 0.05, 0.4, 0 } }, off); // timbre 2 for the pluck blend
  8303. else if (c == 2 || c == 3)
  8304. curve ({ { 0, 0.25, 0, 0 }, { 0.006, 0.12, -0.46, 0 }, { 0.994, 0, 0, 0 } }, off); // timbres
  8305. else if (c == 14)
  8306. curve ({ { 0, 1, 0, 0 }, { 1, 0, 0, 0 } }, off); // pluck curve
  8307. else
  8308. 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)));
  8309. if (image)
  8310. {
  8311. // the image object (FUN_00793300 layout): version 0, image mode, looping, 6 columns × 40 rows, gain + freq planes
  8312. const int cols = 6, rows = 40;
  8313. i32 (0), i32 (0), i32 (1), i32 (0);
  8314. f64 (-1.0), f64 (-1.0), f64 (0.0);
  8315. i32 (cols), i32 (rows);
  8316. i32 (cols * rows);
  8317. for (int c2 = 0; c2 < cols; ++c2)
  8318. for (int r2 = 0; r2 < rows; ++r2)
  8319. {
  8320. const auto v = (float) (255.0 * std::pow (0.93, r2) * (0.6 + 0.4 * std::cos (0.9 * c2 + 0.37 * r2)));
  8321. std::uint32_t u;
  8322. std::memcpy (&u, &v, 4);
  8323. i32 ((std::int32_t) u);
  8324. }
  8325. i32 (cols * rows);
  8326. for (int c2 = 0; c2 < cols; ++c2)
  8327. for (int r2 = 0; r2 < rows; ++r2)
  8328. {
  8329. const auto v = (float) (0.3 * std::sin (1.3 * c2 + 0.5 * r2));
  8330. std::uint32_t u;
  8331. std::memcpy (&u, &v, 4);
  8332. i32 ((std::int32_t) u);
  8333. }
  8334. i32 (0);
  8335. b.push_back (0); // (file name: empty)
  8336. }
  8337. std::swap (b, part);
  8338. i32 ((std::int32_t) part.size());
  8339. b.insert (b.end(), part.begin(), part.end());
  8340. std::array<std::int32_t, 53> g {};
  8341. g[0] = g[1] = g[2] = 256, g[10] = 500, g[11] = 1000, g[12] = 1000, g[15] = 1000;
  8342. for (auto v : g)
  8343. i32 (v);
  8344. for (int i = 0; i < 6; ++i)
  8345. i32 (i);
  8346. std::array<std::uint8_t, 0x3d> gm {};
  8347. const std::int32_t cr = 200, vr = 1;
  8348. std::memcpy (gm.data() + 12, &vr, 4);
  8349. std::memcpy (gm.data() + 24, &cr, 4);
  8350. b.insert (b.end(), gm.begin(), gm.end());
  8351. return b;
  8352. }
  8353. } // namespace
  8354. TEST (harmor_engine)
  8355. {
  8356. // REA_rodada34: state parsing, the additive engine against our reference model (hmodel.py), host levels, voice end
  8357. const auto blob = harmorTestState();
  8358. if (const char* dump = std::getenv ("LYNX_DUMP_HARMOR"))
  8359. std::ofstream (dump, std::ios::binary).write ((const char*) blob.data(), (std::streamsize) blob.size());
  8360. CHECK (lynx::harmorStateUsable (blob));
  8361. CHECK (lynx::harmorFallbacks (blob).empty());
  8362. const auto at = lynx::harmorParamOffset (blob, 0, 52);
  8363. std::int32_t cutoff = 0;
  8364. CHECK (at > 0 && (std::memcpy (&cutoff, blob.data() + at, 4), cutoff == 5000));
  8365. CHECK (lynx::harmorParamOffset (blob, 1, 52) < 0 && lynx::harmorParamOffset (blob, -1, 12) > at);
  8366. auto broken = blob;
  8367. broken.resize (broken.size() / 2);
  8368. CHECK (! lynx::harmorStateUsable (broken));
  8369. auto old = blob;
  8370. old[0] = 15; // articulators and curves are stored from version 16
  8371. CHECK (! lynx::harmorStateUsable (old));
  8372. lynx::ChannelDef c;
  8373. c.instrument = lynx::InstrumentKind::harmor;
  8374. c.pluginState = blob;
  8375. auto render = [] (const lynx::ChannelDef& def, int total, float gl, float gr, int offAt) {
  8376. auto inst = lynx::makeInstrument (lynx::InstrumentKind::harmor);
  8377. inst->configure (def, 120.0);
  8378. inst->prepare (44100.0);
  8379. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  8380. inst->noteOn (60, 0.78125f);
  8381. for (int s = 0; s < total; s += 512)
  8382. {
  8383. if (s == offAt)
  8384. inst->noteOff (60);
  8385. inst->render (l.data() + s, r.data() + s, std::min (512, total - s), gl, gr);
  8386. }
  8387. return std::make_tuple (l, r, std::move (inst));
  8388. };
  8389. {
  8390. // reference model (REA round 34 hmodel.py; C5 at FL velocity 100, Final.Vol 1, centre, released at 6144, the
  8391. // Mersenne Twister seeded like LYNX's; since flbeta165h LYNX's own values: the test state plays at precision
  8392. // "Average", whose IPP 11-bit Exp/Ln — REA_rodada165g-flbeta165h §3 — the model computes exactly)
  8393. auto [l, r, inst] = render (c, 8192, 1.0f, 1.0f, 6144);
  8394. if (std::getenv ("LYNX_PRINT_HARMOR_ENGINE") != nullptr)
  8395. for (int i : { 0, 100, 1000, 3000, 6000, 7000, 8191 })
  8396. std::printf ("harmor engine %d %.8g %.8g\n", i, (double) l[(std::size_t) i], (double) r[(std::size_t) i]);
  8397. const std::tuple<int, float, float> golden[] {
  8398. { 0, 0.0f, 0.0f }, { 100, 0.0094771842f, 0.042498328f }, { 1000, 0.081412978f, -0.032517102f }, { 3000, 0.059289735f, -0.0024759346f },
  8399. { 6000, 0.029978603f, 0.14549156f }, { 7000, -0.011218854f, 0.041979425f }, { 8191, 0.0031455588f, 0.03857585f } };
  8400. for (const auto& [i, gl, gr] : golden)
  8401. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  8402. // after the release the volume envelope ends, the record declicks and the instrument goes idle
  8403. std::vector<float> tail (44100), tailR (tail.size());
  8404. inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
  8405. CHECK (inst->idle());
  8406. CHECK (std::abs (tail.back()) == 0.0f);
  8407. }
  8408. // the reference renderer gives the same samples as the instrument
  8409. {
  8410. const auto ref = lynx::harmor::renderReference (blob, { { 0, 60, 0.78125, 1.0, 0.0, 6144 } }, 44100.0, 8192);
  8411. const auto [l, r, inst] = render (c, 8192, 1.0f, 1.0f, 6144);
  8412. double worst = 0.0;
  8413. for (int i = 0; i < 8192; ++i)
  8414. 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]) });
  8415. CHECK (worst == 0.0);
  8416. }
  8417. // FL's level law applied once: half the channel volume halves the output; full right pan silences the left side
  8418. const auto full = std::get<0> (render (c, 2048, 1.0f, 1.0f, -1));
  8419. const auto half = std::get<0> (render (c, 2048, 0.5f, 0.5f, -1));
  8420. CHECK (std::abs (half[1000] - 0.5f * full[1000]) < 1e-6f && std::abs (full[1000]) > 0.01f);
  8421. auto mono = c;
  8422. mono.pluginState = harmorTestState (false); // no pan LFO ...
  8423. const std::int32_t one = 1;
  8424. std::memcpy (mono.pluginState.data() + lynx::harmorParamOffset (mono.pluginState, 0, 34), &one, 4); // ... nor unison spread
  8425. const auto [pl, pr, pi] = render (mono, 2048, 0.0f, 1.41421356f, -1);
  8426. double el = 0.0, er = 0.0;
  8427. for (int i = 0; i < 2048; ++i)
  8428. el += (double) pl[(std::size_t) i] * pl[(std::size_t) i], er += (double) pr[(std::size_t) i] * pr[(std::size_t) i];
  8429. CHECK (el < 1e-10 * er && er > 1e-3);
  8430. }
  8431. namespace
  8432. {
  8433. // An Ogun state written from scratch (REA_rodada37 §1.3 layout, version 18; zlib with stored blocks): richness 4
  8434. // (255 partials), pre-decay, decay/release, fullness, Seed 1 + fixed Seed 2 with morph, 3-voice unison, a 2-stage
  8435. // low-pass with an LFO on its cutoff, a volume envelope with a sustain point and a velocity map, declick, normalisation,
  8436. // Impulse/Decay maps, and every plugin effect (EQ band, chorus, ping-pong delay, reverb). Everything else is neutral.
  8437. std::vector<std::uint8_t> ogunTestState()
  8438. {
  8439. std::vector<std::uint8_t> b;
  8440. 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))); };
  8441. auto f32v = [&] (float v) { std::uint32_t u; std::memcpy (&u, &v, 4); i32 ((std::int32_t) u); };
  8442. 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))); };
  8443. struct Pt { double dx, y, tension; int mode; };
  8444. auto curve = [&] (int type, const std::vector<Pt>& pts, std::array<std::int32_t, 5> hdr) {
  8445. i32 (type), i32 (3), i32 ((std::int32_t) pts.size());
  8446. for (const auto& p : pts)
  8447. {
  8448. f64 (p.dx), f64 (p.y), f32v ((float) p.tension);
  8449. b.push_back ((std::uint8_t) p.mode), b.push_back (0), b.push_back (0), b.push_back (0);
  8450. }
  8451. for (auto h : hdr)
  8452. i32 (h);
  8453. };
  8454. const std::array<std::int32_t, 5> off { 0, 0, -1, -1, -1 }, on { 0, 1, -1, -1, -1 };
  8455. auto flat = [&] (int type, double y) { curve (type, { { 0, y, 0, 0 }, { 1, y, 0, 0 } }, off); };
  8456. std::array<std::int32_t, 64> P {};
  8457. const std::pair<int, int> values[] = { { 0, 128 }, { 5, 300 }, { 6, 3000 }, { 7, 2000 }, { 8, 200 }, { 9, 500 }, { 10, 12 },
  8458. { 11, 34 }, { 12, 3 }, { 13, 64 }, { 14, 64 }, { 15, 40 }, { 16, 64 }, { 17, 180 }, { 18, 60 }, { 19, 2 }, { 20, 300 },
  8459. { 21, 400 }, { 22, 40 }, { 23, 30 }, { 24, 20 }, { 25, 64 }, { 26, 40 }, { 27, 96 }, { 28, 100 }, { 29, 64 }, { 30, 2 },
  8460. { 31, 19 }, { 32, 221 }, { 33, 0 }, { 34, 50 }, { 35, 100 }, { 36, 20 }, { 37, 60 }, { 38, 2 }, { 39, 64 }, { 40, 300 },
  8461. { 43, 30000 }, { 46, 20000 }, { 49, 0 }, { 50, 2 }, { 51, 0 }, { 52, 9 }, { 53, 1 }, { 54, 0 },
  8462. { 55, 0x01000000 }, // +0x20b: pitch follows the key
  8463. { 56, 0x00010000 }, // +0x20e: declick
  8464. { 62, 0x00000100 } }; // +0x225: normalise
  8465. for (const auto& [i, v] : values)
  8466. P[(std::size_t) i] = v;
  8467. for (auto v : P)
  8468. i32 (v);
  8469. b.push_back (0), b.push_back (1), b.push_back (0); // delay: not tempo-based, on
  8470. i32 (1), i32 (1), i32 (1); // EQ band types: peaking
  8471. b.push_back (0); // comment: 0 characters
  8472. for (int k = 0; k < 13; ++k)
  8473. {
  8474. static constexpr int sum[] = { 1, 2, 3, 9, 11, 12 };
  8475. const double neutral = std::find (std::begin (sum), std::end (sum), k) != std::end (sum) ? 0.5 : 1.0;
  8476. i32 (1);
  8477. f32v (0.0f), f32v (1.0f), f32v (k == 1 ? 0.3f : 0.0f);
  8478. if (k == 0) // volume envelope: attack, decay to the sustain point, release
  8479. 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 });
  8480. else
  8481. flat (1, neutral);
  8482. for (int a : { 128, 128, 128, 128 })
  8483. i32 (a);
  8484. if (k == 1) // cutoff LFO
  8485. curve (2, { { 0, 0.8, 0, 0 } }, on);
  8486. else
  8487. flat (2, 0.5);
  8488. for (int a : { k == 1 ? 40000 : 0, 0, 0, 0 })
  8489. i32 (a);
  8490. i32 (0);
  8491. for (int m = 0; m < 4; ++m)
  8492. if (k == 0 && m == 1)
  8493. curve (3, { { 0, 0.3, 0, 0 }, { 1, 1, -0.2, 0 } }, off); // velocity → volume
  8494. else
  8495. flat (3, neutral);
  8496. }
  8497. flat (3, 0.0); // Forced: none
  8498. curve (3, { { 0, 0.6, 0, 0 }, { 1, 0.4, 0.3, 0 } }, off); // Impulse EQ
  8499. curve (3, { { 0, 0.5, 0, 0 }, { 1, 0.3, 0, 0 } }, off); // Decay EQ
  8500. flat (3, 0.5); // Release EQ
  8501. // u32 version + zlib (stored blocks)
  8502. std::vector<std::uint8_t> s { 18, 0, 0, 0, 0x78, 0x01 };
  8503. for (std::size_t i = 0; i < b.size();)
  8504. {
  8505. const auto len = std::min<std::size_t> (65535, b.size() - i);
  8506. s.push_back (i + len >= b.size() ? 1 : 0);
  8507. s.push_back ((std::uint8_t) len), s.push_back ((std::uint8_t) (len >> 8));
  8508. s.push_back ((std::uint8_t) ~len), s.push_back ((std::uint8_t) (~len >> 8));
  8509. s.insert (s.end(), b.begin() + (std::ptrdiff_t) i, b.begin() + (std::ptrdiff_t) (i + len));
  8510. i += len;
  8511. }
  8512. std::uint32_t a1 = 1, a2 = 0;
  8513. for (auto c : b)
  8514. a1 = (a1 + c) % 65521u, a2 = (a2 + a1) % 65521u;
  8515. for (int sh = 24; sh >= 0; sh -= 8)
  8516. s.push_back ((std::uint8_t) (((a2 << 16) | a1) >> sh));
  8517. return s;
  8518. }
  8519. } // namespace
  8520. TEST (ogun_engine)
  8521. {
  8522. // REA_rodada37: state parsing, the engine against our reference model (ogmodel.py), host levels, export quality
  8523. const auto blob = ogunTestState();
  8524. if (const char* dump = std::getenv ("LYNX_DUMP_OGUN"))
  8525. std::ofstream (dump, std::ios::binary).write ((const char*) blob.data(), (std::streamsize) blob.size());
  8526. CHECK (lynx::ogunStateUsable (blob));
  8527. CHECK (lynx::ogunFallbacks (blob).empty());
  8528. CHECK (lynx::ogunParam (blob, 17) == 180 && lynx::ogunParam (blob, 52) == 9 && ! lynx::ogunParam (blob, lynx::ogunDelayOnIndex + 1));
  8529. auto broken = blob;
  8530. broken.resize (broken.size() - 300);
  8531. CHECK (! lynx::ogunStateUsable (broken));
  8532. auto old = blob;
  8533. old[0] = 13; // a version-18 body read as version 13 (a u32 comment count; versions 10…13: Gen169lTests)
  8534. CHECK (! lynx::ogunStateUsable (old));
  8535. // reference model (REA round 37 ogmodel.Plugin(state).run: C5 velocity 0.75 released at 6144, G5 velocity 0.625 at 2048
  8536. // released at 8192; host block 512)
  8537. 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 } };
  8538. const auto ref = lynx::ogun::renderReference (blob, notes, 44100.0, 16384);
  8539. CHECK (ref.size() == 2 * 16384);
  8540. if (const char* dump = std::getenv ("LYNX_DUMP_OGUN_OUT"))
  8541. std::ofstream (dump, std::ios::binary).write ((const char*) ref.data(), (std::streamsize) (ref.size() * 4));
  8542. const std::tuple<int, float, float> golden[] {
  8543. { 100, -7.763202e-05f, -8.919523e-05f }, { 1000, 0.06195267f, 0.03858230f }, { 3000, 0.01363476f, -0.00378433f },
  8544. { 6000, -0.06601223f, -0.03084030f }, { 9000, 0.04204506f, 0.02452896f }, { 12000, 0.03420147f, -0.01837870f },
  8545. { 16383, -0.00465311f, -0.00119627f } };
  8546. for (const auto& [i, gl, gr] : golden)
  8547. CHECK (std::abs (ref[2 * (std::size_t) i] - gl) < 2e-6f && std::abs (ref[2 * (std::size_t) i + 1] - gr) < 2e-6f);
  8548. // the instrument plays like the reference renderer (Final.Vol 1, centre)
  8549. lynx::ChannelDef c;
  8550. c.instrument = lynx::InstrumentKind::ogun;
  8551. c.pluginState = blob;
  8552. auto render = [&] (const lynx::ChannelDef& def, int total, float gl, float gr, bool held = false) {
  8553. auto inst = lynx::makeInstrument (lynx::InstrumentKind::ogun);
  8554. inst->configure (def, 120.0);
  8555. inst->prepare (44100.0);
  8556. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  8557. for (int s = 0; s < total; s += 512)
  8558. {
  8559. if (s == 0)
  8560. inst->noteOn (60, 0.75f);
  8561. if (s == 2048)
  8562. inst->noteOn (67, 0.625f);
  8563. if (s == 6144 && ! held)
  8564. inst->noteOff (60);
  8565. if (s == 8192 && ! held)
  8566. inst->noteOff (67);
  8567. inst->render (l.data() + s, r.data() + s, std::min (512, total - s), gl, gr);
  8568. }
  8569. return std::make_tuple (l, r, std::move (inst));
  8570. };
  8571. {
  8572. auto [l, r, inst] = render (c, 16384, 1.0f, 1.0f);
  8573. double worst = 0.0;
  8574. for (int i = 0; i < 16384; ++i)
  8575. 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]) });
  8576. CHECK (worst == 0.0);
  8577. // the voices end and the effect tails die out: the instrument goes idle
  8578. std::vector<float> tail (44100), tailR (tail.size());
  8579. for (int k = 0; k < 20 && ! inst->idle(); ++k)
  8580. inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
  8581. CHECK (inst->idle());
  8582. }
  8583. // FL's level law applied once (effects off): half the channel volume halves the output once the gain ramps
  8584. // (limited to 0.001 per sample) have settled on the sustain level; full right pan silences the left side
  8585. {
  8586. auto dry = c;
  8587. for (int i : { 19, 29, 40, 53 })
  8588. CHECK (lynx::ogunSetParam (dry.pluginState, i, 0)); // chorus, delay wet, EQ band and reverb off
  8589. CHECK (lynx::ogunStateUsable (dry.pluginState) && lynx::ogunParam (dry.pluginState, 19) == 0);
  8590. const auto full = std::get<0> (render (dry, 32768, 1.0f, 1.0f, true));
  8591. const auto half = std::get<0> (render (dry, 32768, 0.5f, 0.5f, true));
  8592. double worst = 0.0, peak = 0.0;
  8593. for (int i = 30000; i < 32768; ++i)
  8594. worst = std::max (worst, (double) std::abs (half[(std::size_t) i] - 0.5f * full[(std::size_t) i])),
  8595. peak = std::max (peak, (double) std::abs (full[(std::size_t) i]));
  8596. CHECK (worst <= 1e-7 * peak && peak > 1e-3);
  8597. // FL's default channel volume (100/128): FL hands Ogun Final.Vol = (11^v − 1)/10 (REA_rodada28 §4.1); LYNX's
  8598. // centred channel gains (√2·V·cos(π/4) each) give the reference model's level at that Final.Vol
  8599. const auto V = lynx::channelGain (0.78125f);
  8600. const auto g = V * std::cos (0.25f * 3.14159265f) * 1.41421356f;
  8601. const auto atDefault = std::get<0> (render (dry, 32768, g, g, true));
  8602. const auto refDefault = lynx::ogun::renderReference (dry.pluginState, { { 0, 60, 0.75, V, 0.0, 0.0, 0.0, 0.0, 0.5, -1 },
  8603. { 2048, 67, 0.625, V, 0.0, 0.0, 0.0, 0.0, 0.5, -1 } }, 44100.0, 32768);
  8604. double ei = 0.0, er0 = 0.0;
  8605. for (int i = 30000; i < 32768; ++i)
  8606. 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];
  8607. CHECK (er0 > 0.0 && std::abs (10.0 * std::log10 (ei / er0)) < 0.001);
  8608. const auto [pl, pr, pi] = render (dry, 4096, 0.0f, 1.41421356f);
  8609. double el = 0.0, er = 0.0;
  8610. for (int i = 0; i < 4096; ++i)
  8611. el += (double) pl[(std::size_t) i] * pl[(std::size_t) i], er += (double) pr[(std::size_t) i] * pr[(std::size_t) i];
  8612. CHECK (el < 1e-10 * er && er > 1e-3);
  8613. }
  8614. // export: FL renders Ogun at quality 4 (×2 rate, 32-point reader), LYNX too while it renders offline
  8615. {
  8616. lynx::setOfflineRendering (true);
  8617. auto [l, r, inst] = render (c, 8192, 1.0f, 1.0f);
  8618. lynx::setOfflineRendering (false);
  8619. lynx::ogun::RefOptions q4;
  8620. q4.quality = 4;
  8621. const auto ref4 = lynx::ogun::renderReference (blob, notes, 44100.0, 8192, q4);
  8622. double worst = 0.0, diff = 0.0;
  8623. for (int i = 0; i < 8192; ++i)
  8624. worst = std::max (worst, (double) std::abs (ref4[2 * (std::size_t) i] - l[(std::size_t) i])),
  8625. diff = std::max (diff, (double) std::abs (ref[2 * (std::size_t) i] - l[(std::size_t) i]));
  8626. CHECK (worst == 0.0 && diff > 1e-3);
  8627. const std::tuple<int, float, float> golden4[] {
  8628. { 100, 0.00605961f, 0.00628969f }, { 1000, 0.03729841f, 0.01727954f }, { 3000, -0.09010383f, -0.02551294f },
  8629. { 6000, 0.02167798f, -0.01115632f }, { 8191, 0.00320288f, -0.00615792f } };
  8630. for (const auto& [i, gl, gr] : golden4)
  8631. CHECK (std::abs (ref4[2 * (std::size_t) i] - gl) < 2e-6f && std::abs (ref4[2 * (std::size_t) i + 1] - gr) < 2e-6f);
  8632. }
  8633. // feature variants of the same state against the reference model (energy per side over 8192 samples, three
  8634. // samples; C1 released at 6144): qualities, richness, unison, the filter types, ALT DECAY, 1/√h, Seed 2 Random,
  8635. // infinite decay, logarithmic axis, velocity → position, other rates. Goldens from ogmodel.py (REA round 37).
  8636. {
  8637. struct Sample { int at; float l, r; };
  8638. struct Variant { const char* name; std::vector<std::pair<int, int>> params; int rate; double el, er; Sample s[3]; };
  8639. const Variant variants[] {
  8640. { "q1", { { 54, 1 } }, 44100, 11.0970402, 6.87820714, { { 1500, 0.0240363069f, -0.0148524037f }, { 4000, 0.0873190612f, -0.000715861097f }, { 7000, -0.0378603041f, -0.00338819111f } } },
  8641. { "q2", { { 54, 2 } }, 44100, 11.2405446, 6.68045082, { { 1500, 0.0448660068f, -0.00560745923f }, { 4000, -0.0122657465f, 0.0104285963f }, { 7000, -0.0216034129f, -0.0134632047f } } },
  8642. { "q3", { { 54, 3 } }, 44100, 11.3200608, 6.86196449, { { 1500, -0.0031537537f, -0.0242648814f }, { 4000, 0.0426301025f, 0.0364697985f }, { 7000, 0.00843803957f, 0.0302239582f } } },
  8643. { "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 } } },
  8644. { "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 } } },
  8645. { "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 } } },
  8646. { "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 } } },
  8647. { "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 } } },
  8648. { "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 } } },
  8649. { "alt", { { 62, 257 } }, 44100, 17.2485711, 9.99223979, { { 1500, -0.0319156498f, -0.0186850764f }, { 4000, -0.0769012421f, -0.0375530645f }, { 7000, 0.0310976505f, 0.0445244014f } } },
  8650. { "isqrt", { { 62, 65792 } }, 44100, 45.7309307, 27.1224593, { { 1500, -0.04376591f, -0.00845711026f }, { 4000, 0.0351300463f, -0.129557073f }, { 7000, 0.0096148327f, 0.00123740174f } } },
  8651. { "seed2rnd", { { 11, 0 } }, 44100, 16.8228819, 11.5433061, { { 1500, -0.0117614279f, -0.026497297f }, { 4000, 0.00265733898f, -0.07069581f }, { 7000, 0.00576663949f, 0.0565217957f } } },
  8652. { "decinf", { { 6, 10000 } }, 44100, 19.1879226, 11.0487311, { { 1500, -0.0324105881f, -0.0190765262f }, { 4000, -0.0809780285f, -0.0395730771f }, { 7000, 0.034197174f, 0.048585102f } } },
  8653. { "logx", { { 57, 16777216 } }, 44100, 12.3064326, 7.31519289, { { 1500, -0.0438990779f, 0.0096983267f }, { 4000, -0.0175748877f, 0.0313935578f }, { 7000, 0.0639200211f, 0.024419222f } } },
  8654. { "velpos", { { 56, 66304 } }, 44100, 4.9966925, 3.16540662, { { 1500, 0.0083851777f, -0.0165472236f }, { 4000, 0.0630451962f, 0.0156494901f }, { 7000, -0.0293043777f, -0.00182877108f } } },
  8655. { "fs48", { }, 48000, 10.8813888, 6.53592373, { { 1500, -0.0485184453f, -0.0319588967f }, { 4000, 0.0230421089f, -0.0286604818f }, { 7000, -0.0117630213f, -0.017459048f } } },
  8656. { "fs96", { }, 96000, 11.0189683, 6.39821883, { { 1500, -0.0267860368f, -0.0227564126f }, { 4000, 0.0238709711f, -0.0100849615f }, { 7000, -0.0034619756f, 0.0451687202f } } },
  8657. };
  8658. for (const auto& v : variants)
  8659. {
  8660. auto st = blob;
  8661. for (const auto& [i, x] : v.params)
  8662. CHECK (lynx::ogunSetParam (st, i, x));
  8663. const auto y = lynx::ogun::renderReference (st, { { 0, 60, 0.75, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 6144 },
  8664. { 2048, 67, 0.625, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1 } }, (double) v.rate, 8192);
  8665. double el = 0.0, er = 0.0, peak = 0.0;
  8666. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  8667. 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]) });
  8668. bool ok = y.size() == 2 * 8192 && std::abs (el - v.el) <= 1e-5 * v.el && std::abs (er - v.er) <= 1e-5 * v.er;
  8669. for (const auto& smp : v.s)
  8670. 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;
  8671. if (! ok)
  8672. std::printf (" Ogun variant %s differs from the model: energy %.9g %.9g (model %.9g %.9g)\n", v.name, el, er, v.el, v.er);
  8673. CHECK (ok);
  8674. }
  8675. }
  8676. }
  8677. // our own Maximus v7 state (layout REA_rodada8 §1.1): 7 globals, 4 × 13 band parameters, 8 envelopes (dx, y, tension, type)
  8678. static std::vector<std::uint8_t> maximusTestState (const std::array<int, 7>& g, const std::array<std::array<int, 13>, 4>& bands,
  8679. const std::array<std::vector<std::array<double, 4>>, 8>& envs, int oversampling)
  8680. {
  8681. std::vector<std::uint8_t> d;
  8682. auto put = [&] (const void* v, std::size_t n) { d.insert (d.end(), (const std::uint8_t*) v, (const std::uint8_t*) v + n); };
  8683. auto i32 = [&] (std::int32_t v) { put (&v, 4); };
  8684. i32 (7);
  8685. for (int copy = 0; copy < 2; ++copy)
  8686. {
  8687. for (auto v : g)
  8688. i32 (v);
  8689. for (const auto& b : bands)
  8690. for (auto v : b)
  8691. i32 (v);
  8692. }
  8693. d.resize (0x1fe, 0);
  8694. std::memcpy (d.data() + 0x1f4, &oversampling, 4);
  8695. for (const auto& e : envs)
  8696. {
  8697. i32 (3), i32 (2), i32 ((std::int32_t) e.size());
  8698. for (const auto& pt : e)
  8699. {
  8700. const double dx = pt[0], y = pt[1];
  8701. const float t = (float) pt[2];
  8702. put (&dx, 8), put (&y, 8), put (&t, 4);
  8703. const std::uint8_t flags[4] { (std::uint8_t) pt[3], 0, 0, 0 };
  8704. put (flags, 4);
  8705. }
  8706. i32 (0), i32 (-1), i32 (-1), i32 (-1), i32 (-1);
  8707. }
  8708. return d;
  8709. }
  8710. // A "Kepler-1" state written from scratch (REA_rodada46 §1.1 layout, version 2): the default panel values
  8711. std::vector<std::uint8_t> keplerTestState()
  8712. {
  8713. 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 };
  8714. std::vector<std::uint8_t> st;
  8715. auto u32 = [&] (std::uint32_t v) { for (int i = 0; i < 4; ++i) st.push_back ((std::uint8_t) (v >> (8 * i))); };
  8716. auto f32 = [&] (float f) { std::uint32_t v; std::memcpy (&v, &f, 4); u32 (v); };
  8717. u32 (0), u32 (0), u32 (0), u32 (9);
  8718. for (char c : std::string ("Kepler-1"))
  8719. st.push_back ((std::uint8_t) c);
  8720. st.push_back (0);
  8721. u32 (2), u32 (34);
  8722. for (auto f : panel)
  8723. f32 (f);
  8724. st.push_back (0);
  8725. f32 (1.0f), f32 (0.5f);
  8726. const auto size = (std::uint32_t) st.size() - 12;
  8727. std::memcpy (st.data() + 4, &size, 4);
  8728. return st;
  8729. }
  8730. std::string keplerDll()
  8731. {
  8732. if (realFlpDir.empty())
  8733. return {};
  8734. return (std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "Kepler" / "engine_x64.dll").string();
  8735. }
  8736. TEST (kepler_engine)
  8737. {
  8738. const auto st = keplerTestState();
  8739. CHECK (st.size() == 178 && lynx::keplerStateUsable (st));
  8740. CHECK (lynx::keplerPanelOffset (st, 19) == 0x21 + 4 * 19 && lynx::keplerPanelOffset (st, 34) == -1);
  8741. auto broken = st;
  8742. broken[17] = 'X'; // not "Kepler-1"
  8743. CHECK (! lynx::keplerStateUsable (broken) && lynx::keplerPanelOffset (broken, 0) == -1);
  8744. auto v3 = st;
  8745. v3[0x19] = 3; // version 3 needs the gate / noise / oversampling bytes
  8746. CHECK (! lynx::keplerStateUsable (v3));
  8747. const auto dll = keplerDll();
  8748. if (dll.empty() || ! lynx::keplerDllUsable (dll))
  8749. return; // no FL installation (the engine needs the user's own DLL tables)
  8750. // against FL's engine DLL (EXEC, REA_rodada46 §6): three notes, 44.1 kHz, host blocks of 512 — bit-exact
  8751. 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 } };
  8752. const auto y = lynx::kepler::renderReference (st, dll, notes, 44100.0, 39690);
  8753. CHECK (y.size() == 2 * 39690);
  8754. if (y.size() != 2 * 39690)
  8755. return;
  8756. double el = 0.0, er = 0.0;
  8757. for (std::size_t i = 0; i < y.size(); i += 2)
  8758. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  8759. std::printf (" Kepler default patch: energy %.9g %.9g (FL's DLL 250.641268)\n", el, er);
  8760. CHECK (std::abs (el - 250.641268) < 1e-6 && std::abs (er - 250.641268) < 1e-6);
  8761. const std::tuple<int, float, float> golden[] { { 5000, 0.0467273332f, 0.0467273332f }, { 12000, -0.119412214f, -0.119412214f },
  8762. { 25000, 0.0855825394f, 0.0855825394f }, { 35000, -3.34166579e-08f, -3.34166579e-08f } };
  8763. for (const auto& [i, l, r] : golden)
  8764. CHECK (y[2 * (std::size_t) i] == l && y[2 * (std::size_t) i + 1] == r);
  8765. // the instrument: FL's wrapper velocity, channel volume as an output gain, idle after the release
  8766. lynx::ChannelDef c;
  8767. c.instrument = lynx::InstrumentKind::kepler;
  8768. c.pluginState = st;
  8769. c.samplePath = dll;
  8770. auto inst = lynx::makeInstrument (lynx::InstrumentKind::kepler);
  8771. inst->configure (c, 120.0);
  8772. inst->prepare (44100.0);
  8773. std::vector<float> l (4096), r (4096);
  8774. inst->noteOn (60, 0.8f);
  8775. double e = 0.0;
  8776. bool finite = true;
  8777. for (int b = 0; b < 8; ++b)
  8778. {
  8779. std::fill (l.begin(), l.end(), 0.0f);
  8780. std::fill (r.begin(), r.end(), 0.0f);
  8781. inst->render (l.data(), r.data(), 4096, 0.5f, 0.5f);
  8782. for (int i = 0; i < 4096; ++i)
  8783. 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];
  8784. if (b == 3)
  8785. inst->noteOff (60);
  8786. }
  8787. CHECK (finite && e > 1.0);
  8788. for (int b = 0; b < 40; ++b)
  8789. inst->render (l.data(), r.data(), 4096, 0.5f, 0.5f);
  8790. CHECK (inst->idle());
  8791. }
  8792. // REA round 57 §2: Transistor Bass
  8793. // the plugin state (event 0xD5) of an FL preset file (.fst: the FLhd/FLdt event stream)
  8794. std::vector<std::uint8_t> tbassPresetState (const char* name)
  8795. {
  8796. if (realFlpDir.empty())
  8797. return {};
  8798. std::ifstream in (std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "Transistor Bass" / name, std::ios::binary);
  8799. const std::vector<std::uint8_t> b ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  8800. std::size_t i = 0;
  8801. while (i + 8 <= b.size() && std::memcmp (b.data() + i, "FLdt", 4) != 0)
  8802. ++i;
  8803. i += 8;
  8804. while (i < b.size())
  8805. {
  8806. const auto id = b[i++];
  8807. std::size_t n = id < 64 ? 1 : id < 128 ? 2 : id < 192 ? 4 : 0;
  8808. if (id >= 192)
  8809. for (int shift = 0; i < b.size(); shift += 7)
  8810. {
  8811. const auto c = b[i++];
  8812. n |= (std::size_t) (c & 0x7F) << shift;
  8813. if (c < 0x80)
  8814. break;
  8815. }
  8816. if (id == 0xD5 && i + n <= b.size())
  8817. return { b.begin() + (std::ptrdiff_t) i, b.begin() + (std::ptrdiff_t) (i + n) };
  8818. i += n;
  8819. }
  8820. return {};
  8821. }
  8822. // the same state re-saved as engine-stream version 5 with an engine selector (REA_rodada57 §2.2)
  8823. std::vector<std::uint8_t> tbassWithEngine (const std::vector<std::uint8_t>& s, std::int32_t engine)
  8824. {
  8825. std::vector<std::uint8_t> out (s.begin(), s.begin() + 4);
  8826. auto put = [&] (const void* p, std::size_t n) { out.insert (out.end(), (const std::uint8_t*) p, (const std::uint8_t*) p + n); };
  8827. for (std::size_t at = 4; at + 12 <= s.size();)
  8828. {
  8829. std::uint32_t id;
  8830. std::uint64_t n;
  8831. std::memcpy (&id, s.data() + at, 4);
  8832. std::memcpy (&n, s.data() + at + 4, 8);
  8833. std::vector<std::uint8_t> d (s.begin() + (std::ptrdiff_t) (at + 12), s.begin() + (std::ptrdiff_t) (at + 12 + n));
  8834. at += 12 + (std::size_t) n;
  8835. if (id == 2)
  8836. {
  8837. std::uint64_t m;
  8838. std::memcpy (&m, d.data() + 4, 8);
  8839. std::vector<std::uint8_t> e (d.begin() + 12, d.begin() + 12 + (std::ptrdiff_t) m);
  8840. std::int16_t v;
  8841. std::memcpy (&v, e.data(), 2);
  8842. if (v < 5)
  8843. {
  8844. v = 5;
  8845. std::memcpy (e.data(), &v, 2);
  8846. e.insert (e.end(), 4, 0);
  8847. }
  8848. std::memcpy (e.data() + e.size() - 4, &engine, 4);
  8849. m = e.size();
  8850. d.resize (12);
  8851. std::memcpy (d.data() + 4, &m, 8);
  8852. d.insert (d.end(), e.begin(), e.end());
  8853. }
  8854. const std::uint64_t size = d.size();
  8855. put (&id, 4);
  8856. put (&size, 8);
  8857. put (d.data(), d.size());
  8858. }
  8859. return out;
  8860. }
  8861. std::string tbassDll()
  8862. {
  8863. if (realFlpDir.empty())
  8864. return {};
  8865. return (std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "Transistor Bass" / "engine_x64.dll").string();
  8866. }
  8867. // Plucked! (REA_rodada65 §1): the C++ engine against FL's DLL (EXEC: same notes, the DLL's RandSeed set before the first
  8868. // note, the host killing voices when the plugin asks). LYNX renders these bit-identically to the DLL.
  8869. TEST (plucked_engine)
  8870. {
  8871. auto stateOf = [] (std::initializer_list<std::int32_t> k) {
  8872. std::vector<std::uint8_t> st (20);
  8873. std::memcpy (st.data(), std::data (k), 20);
  8874. return st;
  8875. };
  8876. CHECK (! lynx::pluckedStateUsable (std::vector<std::uint8_t> (19)));
  8877. CHECK (lynx::pluckedStateUsable (stateOf ({ 128, 64, 1, 0, 1 })));
  8878. CHECK (lynx::pluckedParams (stateOf ({ 7, 9, 0, 1, 0 })) == std::vector<int> ({ 7, 9, 0, 1, 0 }));
  8879. using N = lynx::plucked::RefNote;
  8880. struct Sample { int at; float l, r; };
  8881. struct Golden { int set; std::int32_t P[5]; double fs; int block, law, maxPoly; double el, er; Sample s[4]; };
  8882. const std::vector<std::vector<N>> sets {
  8883. { { 0, 60, 1.0, 0.0, 0.0, 0.0, 0.0, -1 } },
  8884. { { 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 } },
  8885. { { 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 } },
  8886. { { 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 } },
  8887. { { 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 } },
  8888. };
  8889. const Golden goldens[] {
  8890. { 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 } } },
  8891. { 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 } } },
  8892. { 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 } } },
  8893. { 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 } } },
  8894. { 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 } } },
  8895. };
  8896. for (const auto& g : goldens)
  8897. {
  8898. const auto st = stateOf ({ g.P[0], g.P[1], g.P[2], g.P[3], g.P[4] });
  8899. const auto length = (int) g.fs * 2;
  8900. const auto y = lynx::plucked::renderReference (st, sets[(std::size_t) g.set], g.fs, length, g.block, 777u + (std::uint32_t) g.set,
  8901. g.law != 0, g.maxPoly);
  8902. double el = 0.0, er = 0.0;
  8903. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  8904. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  8905. 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;
  8906. for (const auto& smp : g.s)
  8907. 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;
  8908. if (! ok)
  8909. 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);
  8910. CHECK (ok);
  8911. }
  8912. // the instrument: plays, the gate makes a note-off silence the string, a held string dies out by itself
  8913. lynx::ChannelDef c;
  8914. c.instrument = lynx::InstrumentKind::plucked;
  8915. c.pluginState = stateOf ({ 200, 40, 1, 1, 1 });
  8916. auto inst = lynx::makeInstrument (lynx::InstrumentKind::plucked);
  8917. inst->configure (c, 120.0);
  8918. inst->prepare (44100.0);
  8919. std::vector<float> l (1024), r (1024);
  8920. inst->noteOn (57, 0.8f);
  8921. double e = 0.0;
  8922. for (int b = 0; b < 8; ++b)
  8923. {
  8924. std::fill (l.begin(), l.end(), 0.0f);
  8925. std::fill (r.begin(), r.end(), 0.0f);
  8926. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  8927. for (int i = 0; i < 1024; ++i)
  8928. e += (double) l[(std::size_t) i] * l[(std::size_t) i] + (double) r[(std::size_t) i] * r[(std::size_t) i];
  8929. }
  8930. CHECK (e > 1.0 && ! inst->idle());
  8931. inst->noteOff (57);
  8932. for (int b = 0; b < 4; ++b)
  8933. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  8934. CHECK (inst->idle());
  8935. c.pluginState = stateOf ({ 0, 64, 0, 0, 0 });
  8936. inst->configure (c, 120.0);
  8937. inst->noteOn (60, 1.0f);
  8938. inst->noteOff (60); // (no gate: the string rings on)
  8939. for (int b = 0; b < 2000 && ! inst->idle(); ++b)
  8940. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  8941. CHECK (inst->idle());
  8942. }
  8943. // BeepMap (REA_rodada65 §2): the C++ engine against FL's DLL (EXEC: same notes, the DLL's RandSeed set before the first
  8944. // note, the host killing voices when the plugin asks): a synthetic 9×6 bitmap with every Scale mode and switch, and two
  8945. // of the installation's presets. LYNX renders these bit-identically to the DLL.
  8946. std::vector<std::uint8_t> beepMapTestState (std::initializer_list<std::int32_t> settings)
  8947. {
  8948. constexpr int W = 9, H = 6;
  8949. std::vector<std::uint8_t> img (54 + 4 * W * H, 0);
  8950. 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)); };
  8951. img[0] = 'B', img[1] = 'M';
  8952. 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);
  8953. put (34, 4 * W * H, 4);
  8954. for (int y = 0; y < H; ++y) // bottom-up rows
  8955. for (int x = 0; x < W; ++x)
  8956. {
  8957. const auto yy = H - 1 - y;
  8958. auto* px = img.data() + 54 + 4 * (y * W + x);
  8959. px[0] = (std::uint8_t) ((x * 91 + yy * 29) & 255), px[1] = (std::uint8_t) ((x * 13 + yy * 71) & 255);
  8960. px[2] = (std::uint8_t) ((x * 37 + yy * 11) & 255), px[3] = 255;
  8961. }
  8962. std::vector<std::uint8_t> st (4 + 60 + 4, 0);
  8963. std::size_t k = 0;
  8964. for (auto v : settings)
  8965. std::memcpy (st.data() + 4 + 4 * k++, &v, 4);
  8966. const auto len = (std::int32_t) img.size();
  8967. std::memcpy (st.data() + 64, &len, 4);
  8968. st.insert (st.end(), img.begin(), img.end());
  8969. return st;
  8970. }
  8971. // the 0xD5 state of one of the installation's BeepMap presets (empty without an installation)
  8972. std::vector<std::uint8_t> beepMapPresetState (const char* name)
  8973. {
  8974. if (realFlpDir.empty())
  8975. return {};
  8976. std::ifstream in (std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "BeepMap" / name, std::ios::binary);
  8977. const std::vector<std::uint8_t> b ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  8978. std::size_t i = 0;
  8979. while (i + 8 <= b.size() && std::memcmp (b.data() + i, "FLdt", 4) != 0)
  8980. ++i;
  8981. i += 8;
  8982. while (i < b.size())
  8983. {
  8984. const auto id = b[i++];
  8985. std::size_t n = id < 64 ? 1 : id < 128 ? 2 : id < 192 ? 4 : 0;
  8986. if (id >= 192)
  8987. for (int shift = 0; i < b.size(); shift += 7)
  8988. {
  8989. const auto c = b[i++];
  8990. n |= (std::size_t) (c & 0x7F) << shift;
  8991. if (c < 0x80)
  8992. break;
  8993. }
  8994. if (id == 0xD5 && i + n <= b.size())
  8995. return { b.begin() + (std::ptrdiff_t) i, b.begin() + (std::ptrdiff_t) (i + n) };
  8996. i += n;
  8997. }
  8998. return {};
  8999. }
  9000. TEST (beepmap_engine)
  9001. {
  9002. const auto synthA = beepMapTestState ({ 3000, 200, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64 });
  9003. CHECK (lynx::beepMapStateUsable (synthA));
  9004. CHECK (lynx::beepMapParams (synthA)->at (1) == 200 && lynx::beepMapParamOffset (synthA, 2) == 12);
  9005. auto broken = synthA;
  9006. broken[0] = 2; // versions above 1 are refused
  9007. CHECK (! lynx::beepMapStateUsable (broken));
  9008. broken = synthA;
  9009. broken.resize (broken.size() - 20); // truncated bitmap
  9010. CHECK (! lynx::beepMapStateUsable (broken));
  9011. using N = lynx::beepmap::RefNote;
  9012. struct Sample { int at; float l, r; };
  9013. struct Golden { int set; double fs; int block; double el, er; Sample s[4]; };
  9014. std::vector<std::vector<std::uint8_t>> states {
  9015. synthA,
  9016. beepMapTestState ({ 5000, 600, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 64 }),
  9017. beepMapTestState ({ 8000, 100, 2, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 64 }),
  9018. beepMapTestState ({ 1000, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 64 }),
  9019. beepMapPresetState ("Default.fst"),
  9020. beepMapPresetState ("Voice.fst"),
  9021. };
  9022. const std::vector<std::vector<N>> sets {
  9023. { { 0, 60, 1.0, 0.0, 0.0, -1 } },
  9024. { { 0, 57, 0.8, 0.3, 25.0, -1 }, { 7000, 64, 0.6, -0.5, 0.0, -1 } },
  9025. { { 0, 48, 1.0, 0.0, 0.0, -1 }, { 300, 72, 0.5, 0.0, -40.0, -1 } },
  9026. { { 0, 67, 1.0, -0.2, 0.0, -1 } },
  9027. { { 0, 60, 1.0, 0.0, 0.0, -1 } },
  9028. { { 0, 55, 0.8, 0.0, 0.0, -1 }, { 20000, 62, 0.8, 0.4, 0.0, -1 } },
  9029. };
  9030. const Golden goldens[] {
  9031. { 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 } } },
  9032. { 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 } } },
  9033. { 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 } } },
  9034. { 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 } } },
  9035. { 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 } } },
  9036. { 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 } } },
  9037. };
  9038. for (const auto& g : goldens)
  9039. {
  9040. const auto& st = states[(std::size_t) g.set];
  9041. if (st.empty())
  9042. continue; // (no FL installation: the presets are skipped)
  9043. const auto length = (int) g.fs * 2;
  9044. const auto y = lynx::beepmap::renderReference (st, sets[(std::size_t) g.set], g.fs, length, g.block, 4321u + (std::uint32_t) g.set);
  9045. double el = 0.0, er = 0.0;
  9046. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  9047. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  9048. 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;
  9049. for (const auto& smp : g.s)
  9050. 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;
  9051. if (! ok)
  9052. 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);
  9053. CHECK (ok);
  9054. }
  9055. // the instrument: plays the image, ends after the last column, a note-off fades the voice out
  9056. lynx::ChannelDef c;
  9057. c.instrument = lynx::InstrumentKind::beepmap;
  9058. c.pluginState = states[1];
  9059. auto inst = lynx::makeInstrument (lynx::InstrumentKind::beepmap);
  9060. inst->configure (c, 120.0);
  9061. inst->prepare (44100.0);
  9062. std::vector<float> l (1024), r (1024);
  9063. inst->noteOn (60, 0.8f);
  9064. double e = 0.0;
  9065. for (int b = 0; b < 8; ++b)
  9066. {
  9067. std::fill (l.begin(), l.end(), 0.0f);
  9068. std::fill (r.begin(), r.end(), 0.0f);
  9069. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  9070. for (int i = 0; i < 1024; ++i)
  9071. e += (double) l[(std::size_t) i] * l[(std::size_t) i] + (double) r[(std::size_t) i] * r[(std::size_t) i];
  9072. }
  9073. CHECK (e > 0.01 && ! inst->idle()); // (Loop on)
  9074. inst->noteOff (60);
  9075. for (int b = 0; b < 4; ++b)
  9076. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  9077. CHECK (inst->idle());
  9078. c.pluginState = synthA; // (no loop: 9 columns of 64·(e^(200/4096·ln 33) − 1)… samples, then the voice ends)
  9079. inst->configure (c, 120.0);
  9080. inst->noteOn (60, 1.0f);
  9081. for (int b = 0; b < 200 && ! inst->idle(); ++b)
  9082. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  9083. CHECK (inst->idle());
  9084. }
  9085. // ---- Sawer (REA_rodada65 §3): installation presets and data.saw read at run time, fingerprints of engine_x64.dll ----
  9086. namespace
  9087. {
  9088. std::vector<std::uint8_t> sawerPresetState (const char* rel)
  9089. {
  9090. if (realFlpDir.empty())
  9091. return {};
  9092. std::ifstream in (std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "Sawer" / rel, std::ios::binary);
  9093. const std::vector<std::uint8_t> b ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  9094. std::size_t i = 0;
  9095. while (i + 8 <= b.size() && std::memcmp (b.data() + i, "FLdt", 4) != 0)
  9096. ++i;
  9097. i += 8;
  9098. while (i < b.size())
  9099. {
  9100. const auto id = b[i++];
  9101. std::size_t n = id < 64 ? 1 : id < 128 ? 2 : id < 192 ? 4 : 0;
  9102. if (id >= 192)
  9103. for (int shift = 0; i < b.size(); shift += 7)
  9104. {
  9105. const auto c = b[i++];
  9106. n |= (std::size_t) (c & 0x7F) << shift;
  9107. if (c < 0x80)
  9108. break;
  9109. }
  9110. if (id == 0xD5 && i + n <= b.size())
  9111. return { b.begin() + (std::ptrdiff_t) i, b.begin() + (std::ptrdiff_t) (i + n) };
  9112. i += n;
  9113. }
  9114. return {};
  9115. }
  9116. } // namespace
  9117. TEST (sawer_engine)
  9118. {
  9119. // a synthetic header: "SSLF" + size + "1rwS" + 96 floats
  9120. std::vector<std::uint8_t> synth (12 + 4 * 96, 0);
  9121. std::memcpy (synth.data(), "SSLF", 4);
  9122. const std::uint32_t size = 4 + 4 * 96;
  9123. std::memcpy (synth.data() + 4, &size, 4);
  9124. std::memcpy (synth.data() + 8, "1rwS", 4);
  9125. const float p27 = 0.75f;
  9126. std::memcpy (synth.data() + 12 + 4 * 27, &p27, 4);
  9127. CHECK (lynx::sawerStateUsable (synth));
  9128. CHECK (lynx::sawerParam (synth, 27) == 0.75f && lynx::sawerParamOffset (synth, 27) == 12 + 4 * 27);
  9129. CHECK (! lynx::sawerParam (synth, 96) && lynx::sawerParamOffset (synth, -1) < 0);
  9130. auto broken = synth;
  9131. broken[8] = 'X';
  9132. CHECK (! lynx::sawerStateUsable (broken));
  9133. CHECK (! lynx::sawerStateUsable (std::vector<std::uint8_t> (synth.begin(), synth.end() - 4)));
  9134. const auto dflt = sawerPresetState ("Default.fst");
  9135. const auto slap = sawerPresetState ("Arpeggios/FG Velocity Slapper.fst");
  9136. const auto tablePath = realFlpDir.empty() ? std::string() : (std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "Sawer" / "data.saw").string();
  9137. const auto table = tablePath.empty() ? std::vector<float> {} : lynx::sawerReadTable (tablePath);
  9138. if (dflt.empty() || slap.empty() || table.size() != 8192u)
  9139. {
  9140. std::printf (" (sawer_engine: FL Studio installation not found, DLL fingerprints skipped)\n");
  9141. return;
  9142. }
  9143. CHECK (lynx::sawerStateUsable (dflt) && lynx::sawerStateUsable (slap));
  9144. // DLL fingerprints (py/gold_sw.py: the EXEC harness, L = R = Final.Vol); "syn" = Default with our own settings
  9145. // (unison 4, sub pulse at 0.6, sync, band-pass, noise, resonance, chorus)
  9146. auto setP = [] (std::vector<std::uint8_t> s, int k, float v) { std::memcpy (s.data() + 12 + 4 * k, &v, 4); return s; };
  9147. auto syn = dflt;
  9148. 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 },
  9149. { 26, 0.3f }, { 28, 0.7f }, { 45, 0.5f }, { 46, 0.0f } })
  9150. syn = setP (syn, kv.first, kv.second);
  9151. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  9152. using N = lynx::sawer::RefNote;
  9153. struct Sample { int at; float l, r; };
  9154. struct Golden { const std::vector<std::uint8_t>* state; std::vector<N> notes; double fs; int block; double el, er; Sample s[4]; };
  9155. const Golden goldens[] {
  9156. { &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 } },
  9157. 44100, 512, 1172.343107, 1169.673445,
  9158. { { 1000, 0.0349505879f, 0.0349650607f }, { 10000, -0.0326882228f, -0.0326587185f }, { 30000, 0.139422998f, 0.139268339f }, { 60000, -1.01108653e-05f, -1.01210762e-05f } } },
  9159. { &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,
  9160. { { 1000, 0.00919434987f, 0.0769683793f }, { 10000, 0.0363935083f, 0.0227626693f }, { 30000, -0.00462511741f, -0.0105419969f }, { 60000, -0.00727002183f, -0.00159690797f } } },
  9161. { &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,
  9162. { { 1000, -0.162745371f, -0.172861978f }, { 10000, 0.0468756184f, 0.112415045f }, { 30000, -0.123887017f, -0.132816017f }, { 60000, 0.0f, 0.0f } } },
  9163. };
  9164. for (const auto& g : goldens)
  9165. {
  9166. const auto y = lynx::sawer::renderReference (*g.state, table, g.notes, g.fs, 66150, g.block);
  9167. CHECK (y.size() == 2 * 66150u);
  9168. if (y.size() != 2 * 66150u)
  9169. continue;
  9170. double el = 0.0, er = 0.0;
  9171. for (std::size_t i = 0; i < 66150; ++i)
  9172. el += (double) y[2 * i] * y[2 * i], er += (double) y[2 * i + 1] * y[2 * i + 1];
  9173. CHECK (std::abs (el - g.el) < 1e-5 * g.el && std::abs (er - g.er) < 1e-5 * g.er);
  9174. for (const auto& s : g.s)
  9175. 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);
  9176. }
  9177. // the instrument: the same engine behind LYNX's channel gains (centre → law A), velocity → Init.Vol; it goes idle
  9178. lynx::ChannelDef c;
  9179. c.instrument = lynx::InstrumentKind::sawer;
  9180. c.pluginState = dflt;
  9181. c.samplePath = tablePath;
  9182. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sawer);
  9183. inst->configure (c, 120.0);
  9184. inst->prepare (44100.0);
  9185. std::vector<float> l (512 * 40, 0.0f), r (512 * 40, 0.0f);
  9186. for (int b = 0; b < 40; ++b)
  9187. {
  9188. if (b == 0)
  9189. inst->noteOn (60, 0.78125f);
  9190. if (b == 10)
  9191. inst->noteOff (60);
  9192. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  9193. }
  9194. 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,
  9195. 8192, 512, 120.0, 0, true);
  9196. double diff = 0.0, energy = 0.0;
  9197. for (int i = 0; i < 8192; ++i)
  9198. {
  9199. 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]) });
  9200. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  9201. }
  9202. CHECK (diff == 0.0 && energy > 1e-3);
  9203. CHECK (inst->idle());
  9204. }
  9205. TEST (transistorbass_engine)
  9206. {
  9207. const auto factory = tbassPresetState ("Factory programs.fst");
  9208. const auto seqOff = tbassPresetState ("Sequencer off.fst");
  9209. if (factory.empty() || seqOff.empty())
  9210. return; // no FL installation
  9211. CHECK (factory.size() == 197887 && seqOff.size() == 196051);
  9212. CHECK (lynx::transistorBassStateUsable (factory) && lynx::transistorBassStateUsable (seqOff));
  9213. CHECK (lynx::transistorBassStateUsable (tbassWithEngine (factory, 2)));
  9214. auto broken = factory;
  9215. broken[0] = 3; // the wrapper refuses versions above 2
  9216. CHECK (! lynx::transistorBassStateUsable (broken));
  9217. auto truncated = factory;
  9218. truncated.resize (truncated.size() - 1000);
  9219. CHECK (! lynx::transistorBassStateUsable (truncated));
  9220. const auto dll = tbassDll();
  9221. if (dll.empty() || ! lynx::transistorBassDllUsable (dll))
  9222. return;
  9223. // against FL's engine DLL (EXEC, REA_rodada57 §2.11): programs of the factory bank (sequencer, accents, slides,
  9224. // distortion, delay, reverb) on the three engines and the sequencer-off preset with legato and accent notes; host
  9225. // blocks of 512 at 44.1 kHz, tempo 125. LYNX differs from the DLL by −121 dB or less here (library pow rounding).
  9226. struct Sample { int at; float l, r; };
  9227. struct Golden { const char* preset; int engine, key; double el, er; Sample s[4]; };
  9228. const Golden goldens[] {
  9229. { "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 } } },
  9230. { "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 } } },
  9231. { "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 } } },
  9232. { "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 } } },
  9233. { "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 } } },
  9234. { "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 } } },
  9235. { "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 } } },
  9236. { "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 } } },
  9237. { "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 } } },
  9238. { "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 } } },
  9239. };
  9240. constexpr int length = 88200;
  9241. for (const auto& g : goldens)
  9242. {
  9243. const bool factoryBank = std::string (g.preset) == "Factory";
  9244. auto st = factoryBank ? factory : seqOff;
  9245. if (g.engine >= 0)
  9246. st = tbassWithEngine (st, g.engine);
  9247. std::vector<lynx::tbass::RefNote> notes;
  9248. if (factoryBank)
  9249. notes.push_back ({ 0, g.key, 0.5, 0.0, 0.0, 0.0, 0.0, 70560 });
  9250. else
  9251. {
  9252. notes.push_back ({ 0, g.key, 0.5, 0.0, 0.0, 0.0, 0.0, 30000 });
  9253. notes.push_back ({ 20000, g.key + 7, 1.6, 0.0, 0.0, 0.0, 0.0, 50000 });
  9254. notes.push_back ({ 45000, g.key - 5, 0.3, 0.0, 0.0, 0.0, 0.0, 70560 });
  9255. }
  9256. const auto y = lynx::tbass::renderReference (st, dll, notes, 44100.0, length);
  9257. double el = 0.0, er = 0.0, peak = 0.0;
  9258. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  9259. 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]) });
  9260. 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;
  9261. for (const auto& smp : g.s)
  9262. 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;
  9263. if (! ok)
  9264. 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);
  9265. CHECK (ok);
  9266. }
  9267. // the instrument: FL's wrapper velocity, the channel volume as an output gain, the sequencer plays while the key is held
  9268. lynx::ChannelDef c;
  9269. c.instrument = lynx::InstrumentKind::transistorbass;
  9270. c.pluginState = factory;
  9271. c.samplePath = dll;
  9272. auto inst = lynx::makeInstrument (lynx::InstrumentKind::transistorbass);
  9273. inst->configure (c, 128.0);
  9274. inst->prepare (44100.0);
  9275. std::vector<float> l (4096), r (4096);
  9276. inst->noteOn (65, 0.8f);
  9277. double e = 0.0;
  9278. bool finite = true;
  9279. for (int b = 0; b < 24; ++b)
  9280. {
  9281. std::fill (l.begin(), l.end(), 0.0f);
  9282. std::fill (r.begin(), r.end(), 0.0f);
  9283. inst->render (l.data(), r.data(), 4096, 0.5f, 0.5f);
  9284. for (int i = 0; i < 4096; ++i)
  9285. 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];
  9286. if (b == 20)
  9287. inst->noteOff (65);
  9288. }
  9289. CHECK (finite && e > 1.0 && ! inst->idle());
  9290. for (int b = 0; b < 400 && ! inst->idle(); ++b)
  9291. inst->render (l.data(), r.data(), 4096, 0.5f, 0.5f);
  9292. CHECK (inst->idle());
  9293. }
  9294. TEST (kepler_demo_channels)
  9295. {
  9296. // every Kepler channel of FL's demo projects on LYNX's engine against FL's engine DLL (EXEC fingerprints, REA_rodada46 §6;
  9297. // the engine differs from the DLL by −127 dB or less on these, from the DLL library's pow rounding)
  9298. const auto dll = keplerDll();
  9299. if (dll.empty() || ! lynx::keplerDllUsable (dll))
  9300. return;
  9301. struct Sample { int at; float l, r; };
  9302. struct Golden { const char* file; const char* channel; std::size_t stateSize; double el, er; Sample s[4]; };
  9303. const Golden goldens[] {
  9304. { "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 } } },
  9305. { "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 } } },
  9306. { "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 } } },
  9307. { "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 } } },
  9308. };
  9309. int checked = 0;
  9310. for (const auto& g : goldens)
  9311. {
  9312. std::ifstream in (std::filesystem::path (realFlpDir) / "Demo projects" / g.file, std::ios::binary);
  9313. const std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  9314. if (bytes.empty())
  9315. continue;
  9316. const flp::Channel* ch = nullptr;
  9317. const auto project = flp::parseProject (bytes);
  9318. for (const auto& c : project.channels)
  9319. if (c.pluginName == "Kepler" && c.name == g.channel)
  9320. ch = &c;
  9321. CHECK (ch != nullptr);
  9322. if (ch == nullptr)
  9323. continue;
  9324. CHECK (ch->pluginState.size() == g.stateSize && lynx::keplerStateUsable (ch->pluginState));
  9325. 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 },
  9326. { 16000, 55, 0.95, 0.0, 0.0, 30000 } }, 44100.0, 39690);
  9327. double el = 0.0, er = 0.0, peak = 0.0;
  9328. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  9329. 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]) });
  9330. bool ok = y.size() == 2 * 39690 && std::abs (el - g.el) <= 1e-5 * g.el && std::abs (er - g.er) <= 1e-5 * g.er;
  9331. for (const auto& smp : g.s)
  9332. 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;
  9333. if (! ok)
  9334. 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);
  9335. CHECK (ok);
  9336. ++checked;
  9337. }
  9338. std::printf (" Kepler: %d demo channels checked against FL's engine DLL\n", checked);
  9339. CHECK (checked == (int) std::size (goldens));
  9340. }
  9341. TEST (ogun_demo_channels)
  9342. {
  9343. // every Ogun channel of FL's demo projects (with its plugin effects) against the round-37 reference model, which is
  9344. // 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),
  9345. // 39 690 samples at 44.1 kHz, at the state's quality and at the export quality 4. Fingerprint: energy per side and
  9346. // four samples (the engine matches the model within float rounding, −125 dB or better on the whole signal).
  9347. if (realFlpDir.empty())
  9348. return;
  9349. struct Sample { int at; float l, r; };
  9350. struct Golden { const char* file; int ordinal; std::size_t stateSize; bool export4; double el, er; Sample s[4]; };
  9351. const Golden goldens[] {
  9352. { "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
  9353. { "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
  9354. { "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
  9355. { "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
  9356. { "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
  9357. { "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
  9358. { "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
  9359. { "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
  9360. { "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
  9361. { "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
  9362. { "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
  9363. { "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
  9364. { "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
  9365. { "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
  9366. { "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
  9367. { "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
  9368. { "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
  9369. { "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
  9370. { "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
  9371. { "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
  9372. { "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
  9373. { "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
  9374. { "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
  9375. { "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
  9376. { "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
  9377. { "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
  9378. { "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
  9379. { "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
  9380. { "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
  9381. { "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
  9382. { "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
  9383. { "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
  9384. { "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
  9385. { "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
  9386. { "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
  9387. { "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
  9388. { "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
  9389. { "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
  9390. { "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
  9391. { "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
  9392. };
  9393. std::map<std::string, std::vector<std::vector<std::uint8_t>>> states;
  9394. int checked = 0;
  9395. for (const auto& g : goldens)
  9396. {
  9397. if (! states.contains (g.file))
  9398. {
  9399. std::ifstream in (std::filesystem::path (realFlpDir) / "Demo projects" / g.file, std::ios::binary);
  9400. const std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  9401. auto& list = states[g.file];
  9402. if (! bytes.empty())
  9403. for (const auto& c : flp::parseProject (bytes).channels)
  9404. if (c.pluginName == "Ogun")
  9405. list.push_back (c.pluginState);
  9406. }
  9407. const auto& list = states[g.file];
  9408. if (list.empty())
  9409. continue; // not this installation
  9410. CHECK ((int) list.size() > g.ordinal);
  9411. if ((int) list.size() <= g.ordinal)
  9412. continue;
  9413. const auto& st = list[(std::size_t) g.ordinal];
  9414. CHECK (st.size() == g.stateSize);
  9415. lynx::ogun::RefOptions opt;
  9416. opt.quality = g.export4 ? 4 : -1;
  9417. const auto y = lynx::ogun::renderReference (st, { { 0, 60, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 20000 },
  9418. { 8000, 67, 0.6, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 26000 },
  9419. { 16000, 55, 0.95, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 30000 } }, 44100.0, 39690, opt);
  9420. double el = 0.0, er = 0.0, peak = 0.0;
  9421. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  9422. 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]) });
  9423. bool ok = y.size() == 2 * 39690 && std::abs (el - g.el) <= 1e-5 * g.el && std::abs (er - g.er) <= 1e-5 * g.er;
  9424. for (const auto& smp : g.s)
  9425. 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;
  9426. if (! ok)
  9427. std::printf (" Ogun %s #%d (q%s) differs from the model: energy %.9g %.9g (model %.9g %.9g)\n", g.file, g.ordinal,
  9428. g.export4 ? "4" : "-state", el, er, g.el, g.er);
  9429. CHECK (ok);
  9430. ++checked;
  9431. }
  9432. std::printf (" Ogun: %d demo renders checked against the reference model\n", checked);
  9433. CHECK (checked == (int) std::size (goldens));
  9434. }
  9435. TEST (autogun_template_channels)
  9436. {
  9437. // REA_rodada57: every distinct Autogun state of the installation's projects (templates and the render test), with its
  9438. // effects and the Magic Dust stage (SG.dat from the installation), three notes (C5 1.0 0–20000, G5 0.6 8000–26000,
  9439. // 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
  9440. // goldens are renders of the installation's Autogun_x64.dll (LYNX is within −127 dB of them); the render-test state
  9441. // uses Seed 2 "Random", so its golden is LYNX's own render with the counter at 0 (a fingerprint of the engine).
  9442. if (realFlpDir.empty())
  9443. return;
  9444. const auto sgPath = std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "Autogun" / "Data" / "SG.dat";
  9445. std::ifstream sgIn (sgPath, std::ios::binary);
  9446. const std::vector<std::uint8_t> sgState ((std::istreambuf_iterator<char> (sgIn)), std::istreambuf_iterator<char>());
  9447. if (sgState.size() != 1342)
  9448. return; // not this installation
  9449. struct Sample { int at; float l, r; };
  9450. struct Golden { const char* file; std::size_t stateSize; bool export4; double el, er; Sample s[4]; };
  9451. const Golden goldens[] {
  9452. { "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)
  9453. { "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)
  9454. { "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)
  9455. { "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)
  9456. { "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)
  9457. { "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)
  9458. { "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)
  9459. { "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)
  9460. { "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)
  9461. { "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)
  9462. };
  9463. int checked = 0;
  9464. for (const auto& g : goldens)
  9465. {
  9466. std::ifstream in (std::filesystem::path (realFlpDir) / g.file, std::ios::binary);
  9467. const std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  9468. if (bytes.empty())
  9469. continue;
  9470. std::vector<std::uint8_t> st;
  9471. for (const auto& c : flp::parseProject (bytes).channels)
  9472. if (c.pluginName == "Autogun" && st.empty())
  9473. st = c.pluginState;
  9474. CHECK (st.size() == g.stateSize);
  9475. CHECK (lynx::autogunStateUsable (st));
  9476. lynx::ogun::RefOptions opt;
  9477. opt.autogun = true;
  9478. opt.sgState = sgState;
  9479. opt.quality = g.export4 ? 4 : -1;
  9480. const auto y = lynx::ogun::renderReference (st, { { 0, 60, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 20000 },
  9481. { 8000, 67, 0.6, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 26000 },
  9482. { 16000, 55, 0.95, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 30000 } }, 44100.0, 39690, opt);
  9483. double el = 0.0, er = 0.0, peak = 0.0;
  9484. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  9485. 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]) });
  9486. bool ok = y.size() == 2 * 39690 && std::abs (el - g.el) <= 1e-5 * g.el && std::abs (er - g.er) <= 1e-5 * g.er;
  9487. for (const auto& smp : g.s)
  9488. 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;
  9489. if (! ok)
  9490. 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);
  9491. CHECK (ok);
  9492. ++checked;
  9493. }
  9494. std::printf (" Autogun: %d template/render-test renders checked (DLL goldens for the fixed-spectrum states)\n", checked);
  9495. CHECK (checked == (int) std::size (goldens));
  9496. // the instrument (SG.dat read from ChannelDef::samplePath) plays like the reference renderer, then goes idle
  9497. {
  9498. std::ifstream in (std::filesystem::path (realFlpDir) / "Templates" / "Advanced" / "Electronic" / "Trap.flp", std::ios::binary);
  9499. const std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  9500. lynx::ChannelDef c;
  9501. c.instrument = lynx::InstrumentKind::autogun;
  9502. for (const auto& ch : flp::parseProject (bytes).channels)
  9503. if (ch.pluginName == "Autogun")
  9504. c.pluginState = ch.pluginState;
  9505. c.samplePath = sgPath.string();
  9506. lynx::ogun::RefOptions opt;
  9507. opt.autogun = true;
  9508. opt.sgState = sgState;
  9509. opt.tempo = 120.0; // the instrument is configured at 120 BPM
  9510. const auto ref = lynx::ogun::renderReference (c.pluginState, { { 0, 60, 0.75, 1.0, 0.0, 0.0, 0.0, 0.0, 0.5, 6144 } }, 44100.0, 16384, opt);
  9511. auto inst = lynx::makeInstrument (lynx::InstrumentKind::autogun);
  9512. inst->configure (c, 120.0);
  9513. inst->prepare (44100.0);
  9514. std::vector<float> l (16384), r (16384);
  9515. for (int s = 0; s < 16384; s += 512)
  9516. {
  9517. if (s == 0)
  9518. inst->noteOn (60, 0.75f);
  9519. if (s == 6144)
  9520. inst->noteOff (60);
  9521. inst->render (l.data() + s, r.data() + s, 512, 1.0f, 1.0f);
  9522. }
  9523. double worst = 0.0, peak = 0.0;
  9524. for (int i = 0; i < 16384; ++i)
  9525. 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]) }),
  9526. peak = std::max (peak, (double) std::abs (l[(std::size_t) i]));
  9527. if (worst != 0.0)
  9528. std::printf (" Autogun instrument vs reference: worst %.3g (peak %.3g)\n", worst, peak);
  9529. CHECK (ref.size() == 2 * 16384 && peak > 0.01 && worst == 0.0);
  9530. std::vector<float> tail (44100), tailR (tail.size());
  9531. for (int k = 0; k < 40 && ! inst->idle(); ++k)
  9532. inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
  9533. CHECK (inst->idle());
  9534. }
  9535. // the state variants: Ogun's parser rejects the version-19 Autogun layout, the comment length is one byte or 0xff + i32
  9536. std::ifstream rt (std::filesystem::path (realFlpDir) / "System" / "Render tests" / "Autogun.flp", std::ios::binary);
  9537. const std::vector<std::uint8_t> rtBytes ((std::istreambuf_iterator<char> (rt)), std::istreambuf_iterator<char>());
  9538. for (const auto& c : flp::parseProject (rtBytes).channels)
  9539. if (c.pluginName == "Autogun")
  9540. CHECK (lynx::autogunStateUsable (c.pluginState) && lynx::ogunParam (c.pluginState, 63) == 430);
  9541. }
  9542. TEST (harmless_rack)
  9543. {
  9544. // REA_rodada36: Harmless's effects rack against our reference model (rack.py / maxmodel.py, which match the
  9545. // installation's Harmless_x64.dll and SG_x64.dll), 44.1 kHz, host blocks of 512
  9546. std::vector<float> inL (8192), inR (8192);
  9547. for (int i = 0; i < 4096; ++i)
  9548. {
  9549. 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));
  9550. inR[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / 44100.0));
  9551. }
  9552. auto run = [&] (const std::array<int, 100>& p, int compType, std::shared_ptr<const lynx::hlfx::MaximusPreset> comp, int total, int voicesUntil) {
  9553. lynx::hlfx::Rack rack;
  9554. std::array<int, 7> t {};
  9555. t[3] = compType;
  9556. rack.prepare (p, t, 44100.0, 120.0, std::move (comp));
  9557. std::vector<float> l (inL.begin(), inL.begin() + total), r (inR.begin(), inR.begin() + total);
  9558. for (int s = 0; s < total; s += 512)
  9559. rack.process (l.data() + s, r.data() + s, std::min (512, total - s), s < voicesUntil);
  9560. return std::make_pair (l, r);
  9561. };
  9562. {
  9563. // chorus type 3 (mix 400), ping-pong delay type 2 (vol 600, feedback 500, 1/4 beat), reverb type 4 (vol 700)
  9564. std::array<int, 100> p {};
  9565. p[90] = 3, p[91] = 400, p[92] = 2, p[93] = 600, p[94] = 500, p[95] = 48, p[96] = 4, p[97] = 700;
  9566. const auto [l, r] = run (p, 0, nullptr, 8192, 4096);
  9567. const std::tuple<int, float, float> golden[] { { 0, 0.0f, 0.0f }, { 500, -0.25248247f, 0.15141529f }, { 3000, -0.65128994f, -0.64854866f },
  9568. { 5000, -0.54308629f, -0.36827448f }, { 6000, -0.98378551f, -0.72790027f },
  9569. { 8191, 0.50599128f, -0.43637198f } };
  9570. for (const auto& [i, gl, gr] : golden)
  9571. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  9572. }
  9573. {
  9574. // the delay alone: an impulse comes back after round(time·fs·0.3125/bpm) samples × (11^0.6 − 1)/10, then × feedback
  9575. std::array<int, 100> p {};
  9576. p[92] = 1, p[93] = 600, p[94] = 500, p[95] = 48;
  9577. lynx::hlfx::Rack rack;
  9578. rack.prepare (p, {}, 44100.0, 120.0, nullptr);
  9579. std::vector<float> l (16384), r (16384);
  9580. l[0] = 1.0f;
  9581. for (int s = 0; s < 16384; s += 512)
  9582. rack.process (l.data() + s, r.data() + s, 512, s == 0);
  9583. const auto D = 5512; // 48·44100·0.3125/120 = 5512.5 → 5512 (round half to even)
  9584. const auto vol = (float) ((std::exp ((double) (600 * 0.001f) * (double) (float) std::log (11.0)) - 1.0) * (double) 0.1f);
  9585. CHECK (std::abs (l[(std::size_t) D] - vol) < 1e-7f && std::abs (l[(std::size_t) D - 1]) < 1e-12f);
  9586. // the feedback ramps from 0.5 toward 0.5: the second echo is vol·0.5
  9587. CHECK (std::abs (l[(std::size_t) (2 * D)] - vol * 0.5f) < 1e-7f && std::abs (r[(std::size_t) D]) < 1e-12f);
  9588. }
  9589. // the compressor: Maximus states of our own (no preset data), checked against SG_x64.dll by REA round 36
  9590. using Env = std::vector<std::array<double, 4>>;
  9591. const Env def { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 } };
  9592. 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,
  9593. int sus = 2882) { return std::array<int, 13> { mode, pre, post, sep, sat, ceil, att, rel2, 2, 5000, 3, sus, 0 }; };
  9594. {
  9595. // master brickwall at −0.22 dB only (bands off / compression off)
  9596. const auto st = maximusTestState ({ 19800, 1, 48065, 1, 1115, 1000, -1 }, { band (3), band (1), band (3), band (0) },
  9597. { def, def, def, def, def, def, def, def }, 0);
  9598. const auto preset = lynx::hlfx::makeMaximusPreset (st);
  9599. CHECK (preset != nullptr);
  9600. std::array<int, 100> p {};
  9601. p[98] = 1000;
  9602. const auto [l, r] = run (p, 1, preset, 4096, 4096);
  9603. // SG_x64.dll's own output (EXEC, REA_rodada40: bit-identical to the port)
  9604. const std::tuple<int, float, float> golden[] { { 100, 0.024997942f, 0.88166064f }, { 1000, 0.56971401f, -0.62808102f },
  9605. { 2047, -0.27541628f, 0.709135f }, { 3000, 0.32431623f, -0.37985197f },
  9606. { 4095, -0.12743522f, -0.15237485f } };
  9607. for (const auto& [i, gl, gr] : golden)
  9608. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  9609. float peak = 0.0f;
  9610. for (int i = 2048; i < 4096; ++i)
  9611. peak = std::max ({ peak, std::abs (l[(std::size_t) i]), std::abs (r[(std::size_t) i]) });
  9612. CHECK (peak < 0.9751f);
  9613. }
  9614. {
  9615. // 24 dB crossovers, ×2 oversampling, low cut, stereo separation, saturation A, a knee with tension (type 7) and
  9616. // exponential tensions (type 0), release 2 on the master, band mix 600
  9617. const Env knee { { 0.0, 0.0, 0.0, 0 }, { 0.35, 0.35, 0.0, 0 }, { 0.65, 0.6, 0.36, 7 } };
  9618. const Env t0 { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.14, 0 }, { 0.5, 0.5, 0.0, 0 } };
  9619. const Env m0 { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 }, { 0.5, 0.6, 0.085, 0 } };
  9620. const auto st = maximusTestState ({ 19800, 1, 48065, 1, 891, 600, 32768 },
  9621. { band (1, 1264, 1296, 500, -500, 1200), band (0, 1340, 1000, -348, 0, 1000, 1115, 0, 1570),
  9622. band (0, 1364, 1152, 0, 0, 1000, 1115, 0, 1186), band (0, 1000, 1000, 0, -196, 1000, 1115, 4464, 1538) },
  9623. { def, def, knee, def, t0, def, m0, def }, 1);
  9624. const auto preset = lynx::hlfx::makeMaximusPreset (st);
  9625. CHECK (preset != nullptr);
  9626. std::array<int, 100> p {};
  9627. p[98] = 600;
  9628. const auto [l, r] = run (p, 2, preset, 4096, 4096);
  9629. // SG_x64.dll's own output (EXEC, REA_rodada40; the port is within −142 dB of it)
  9630. const std::tuple<int, float, float> golden[] { { 100, 0.0f, 0.0f }, { 1000, -0.46856543f, -0.63262129f },
  9631. { 2047, 0.20268451f, 0.54145539f }, { 3000, -0.25613472f, -0.49985155f },
  9632. { 4095, 0.10172445f, 0.050564945f } };
  9633. for (const auto& [i, gl, gr] : golden)
  9634. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  9635. }
  9636. {
  9637. // an .fst wrapper around a state is found; unsupported states are rejected
  9638. const auto st = maximusTestState ({ 19800, 1, 48065, 1, 1115, 1000, -1 }, { band (3), band (1), band (3), band (0) },
  9639. { def, def, def, def, def, def, def, def }, 0);
  9640. std::vector<std::uint8_t> fst { 'F', 'L', 'h', 'd', 6, 0, 0, 0, 0, 0, 1, 0, 0x60, 0, 'F', 'L', 'd', 't' };
  9641. std::vector<std::uint8_t> body { 0xC9, 8, 'M', 'a', 'x', 'i', 'm', 'u', 's', 0, 0xD5 };
  9642. for (auto n = st.size(); ; n >>= 7)
  9643. {
  9644. body.push_back ((std::uint8_t) ((n & 0x7f) | (n >= 0x80 ? 0x80 : 0)));
  9645. if (n < 0x80)
  9646. break;
  9647. }
  9648. body.insert (body.end(), st.begin(), st.end());
  9649. const auto len = (std::uint32_t) body.size();
  9650. for (int k = 0; k < 4; ++k)
  9651. fst.push_back ((std::uint8_t) (len >> (8 * k)));
  9652. fst.insert (fst.end(), body.begin(), body.end());
  9653. const auto found = lynx::hlfx::maximusStateFromFst (fst);
  9654. CHECK (found && *found == st);
  9655. auto rms = st;
  9656. rms[4 + 4 * (7 + 13 * 3 + 12)] = 1; // RMS detector on the master (decoded in REA_rodada40)
  9657. CHECK (lynx::hlfx::makeMaximusPreset (rms) != nullptr);
  9658. auto masterMid = st;
  9659. masterMid[0x1fc] = 1; // "Master Mid mode": not decoded
  9660. CHECK (lynx::hlfx::makeMaximusPreset (masterMid) == nullptr);
  9661. }
  9662. }
  9663. TEST (fruity_delay_bank_engine)
  9664. {
  9665. // REA_rodada56: Fruity Delay Bank, two states of our own against the installation's DLL (goldens = the DLL's output
  9666. // for the same input, EXEC; a new instance, FTZ/DAZ as the plugin's thread). Both are bit-identical.
  9667. lynx::ScopedFlushDenormals ftz;
  9668. struct Bank
  9669. {
  9670. std::array<int, 16> ctl;
  9671. lynx::fldbank::BankOptions opt;
  9672. };
  9673. auto build = [] (std::array<int, 4> globals, std::map<int, Bank> banks, int oversampling) {
  9674. auto s = lynx::fldbank::defaultState();
  9675. for (int i = 0; i < 4; ++i)
  9676. s.p[(std::size_t) i] = globals[(std::size_t) i];
  9677. s.p[132] = oversampling;
  9678. for (auto& [b, k] : banks)
  9679. {
  9680. for (int c = 0; c < 16; ++c)
  9681. if (16 * b + 4 + c < lynx::fldbank::automatable)
  9682. s.p[(std::size_t) (16 * b + 4 + c)] = k.ctl[(std::size_t) c];
  9683. s.bank[(std::size_t) b] = k.opt;
  9684. s.bank[(std::size_t) b].enabled = true;
  9685. }
  9686. return s;
  9687. };
  9688. auto opt = [] (int f1, int o1, int f2, int o2, bool after, int mode, int steps, int amount, bool tap, bool sync) {
  9689. lynx::fldbank::BankOptions o;
  9690. o.filterType[0] = f1;
  9691. o.filterOrder[0] = o1;
  9692. o.filterType[1] = f2;
  9693. o.filterOrder[1] = o2;
  9694. o.filterAfter = after;
  9695. o.mode = mode;
  9696. o.steps = steps;
  9697. o.amount = amount;
  9698. o.tapBeforeFeedback = tap;
  9699. o.sync = sync;
  9700. return o;
  9701. };
  9702. auto run = [] (const lynx::fldbank::State& s, double rate, int block, double bpm) {
  9703. std::vector<float> l (4096), r (4096);
  9704. for (int i = 0; i < 4096; ++i)
  9705. {
  9706. 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));
  9707. r[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / rate));
  9708. }
  9709. lynx::fldbank::Engine e (s, rate, bpm, block);
  9710. for (int i = 0; i < 4096; i += block)
  9711. e.process (l.data() + i, r.data() + i, std::min (block, 4096 - i));
  9712. return std::make_pair (l, r);
  9713. };
  9714. auto check = [] (const std::pair<std::vector<float>, std::vector<float>>& out, const std::vector<std::tuple<int, float, float>>& golden) {
  9715. for (const auto& [i, gl, gr] : golden)
  9716. CHECK (out.first[(std::size_t) i] == gl && out.second[(std::size_t) i] == gr);
  9717. };
  9718. // controls: in pan, in vol, f1 cutoff, f1 res, f1 gain, fb pan, fb level, fb cross, time, offset, f2 cutoff, f2 res,
  9719. // f2 gain, out pan, out vol, send. Three banks: ping-pong with low-pass ×2 / peaking feedback filter, pattern and send;
  9720. // free time with a high shelf after the delay, band-pass feedback, negative feedback/cross, tap before the feedback;
  9721. // swapped outputs with a 3-section high-pass in the loop and a negative pattern shape.
  9722. const auto a = build ({ 100, 128, 120, 110 },
  9723. { { 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) } },
  9724. { 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) } },
  9725. { 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) } } },
  9726. 0);
  9727. check (run (a, 44100.0, 512, 120.0), { { 100, -0.00294386433f, 0.0141299525f }, { 1000, -0.117360562f, -0.140237048f },
  9728. { 2047, 0.172598198f, -0.401046395f }, { 3000, 0.303020209f, -0.127763838f },
  9729. { 4095, 0.0093755126f, 0.241272509f } });
  9730. // 2× oversampling at 48 kHz in blocks of 300, low shelf before the delay, 2-section notch in the loop, a send into a
  9731. // ping-pong bank 3 (bank 1 and 2 off: no send) with a 2-step pattern
  9732. const auto b = build ({ 0, 128, 128, 128 },
  9733. { { 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) } },
  9734. { 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) } } },
  9735. 1);
  9736. check (run (b, 48000.0, 300, 93.0), { { 100, 0.0f, 0.0f }, { 1000, 0.0f, 0.0f }, { 2047, 0.201803684f, -0.327834547f },
  9737. { 3000, 0.412344277f, -0.101866335f }, { 4095, 0.204572573f, 0.71171385f } });
  9738. // FL's state round-trips through the import (version 15 times are converted), the globals are the slot's parameters
  9739. auto bytes = lynx::fldbank::serialise (a);
  9740. const auto slot = lynx::effectFromFl ("Fruity Delay Bank", bytes);
  9741. CHECK (slot && slot->kind == lynx::EffectKind::fruityDelayBank && slot->state == bytes);
  9742. CHECK (slot && slot->params[0] == 100.0f / 128.0f && slot->params[3] == 110.0f / 128.0f);
  9743. bytes[0] = 15;
  9744. const auto v15 = lynx::fldbank::parse (bytes);
  9745. CHECK (v15 && v15->p[12] == 2400 && v15->p[28] == 1500);
  9746. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityDelayBank, 3, 0.5f, 120.0) == "50%");
  9747. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityDelayBank, 0, 1.0f, 120.0) == "0.0 dB");
  9748. CHECK (! lynx::fldbank::parse ({ 16, 0, 0, 0 }));
  9749. // the LYNX effect (settled ramps, 512-frame engine blocks) runs from the slot
  9750. auto fx = lynx::makeEffect (*slot);
  9751. fx->prepare (44100.0);
  9752. std::vector<float> l (1024, 0.5f), r (1024, -0.25f);
  9753. fx->process (l.data(), r.data(), 1024, slot->params, 120.0);
  9754. CHECK (std::isfinite (l[1023]) && std::isfinite (r[1023]));
  9755. }
  9756. TEST (fruity_multiband_engine)
  9757. {
  9758. // REA_rodada56: Fruity Multiband Compressor, states of our own against the installation's DLL
  9759. // (goldens = the DLL's output for the same input, EXEC; blocks of 512 at 44.1 kHz; all three bit-identical)
  9760. std::vector<float> inL (4096), inR (4096);
  9761. for (int i = 0; i < 4096; ++i)
  9762. {
  9763. 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)
  9764. + 0.5 * std::sin (2.0 * 3.141592653589793 * 90.0 * i / 44100.0));
  9765. inR[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / 44100.0)
  9766. + 0.4 * std::sin (2.0 * 3.141592653589793 * 7000.0 * i / 44100.0));
  9767. }
  9768. auto multiband = [] (std::array<int, 28> c) {
  9769. std::vector<std::uint8_t> b;
  9770. auto put = [&b] (std::int32_t v) {
  9771. std::uint8_t q[4];
  9772. std::memcpy (q, &v, 4);
  9773. b.insert (b.end(), q, q + 4);
  9774. };
  9775. put (0);
  9776. for (auto v : c)
  9777. put (v);
  9778. put (0); // UI words of the state (not used by the engine)
  9779. put (50);
  9780. return b;
  9781. };
  9782. struct Out { std::vector<float> l, r; int latency = 0; };
  9783. auto run = [&] (const std::vector<std::uint8_t>& st) {
  9784. const auto s = lynx::flmb::parse (st);
  9785. CHECK (s.has_value());
  9786. lynx::EffectParams p {};
  9787. lynx::flmb::toParams (*s, p.data());
  9788. auto fx = lynx::flmb::make();
  9789. fx->prepare (44100.0);
  9790. Out o { inL, inR };
  9791. for (int s0 = 0; s0 < 4096; s0 += 512)
  9792. fx->process (o.l.data() + s0, o.r.data() + s0, 512, p, 120.0);
  9793. o.latency = fx->latencySamples();
  9794. return o;
  9795. };
  9796. auto check = [] (const Out& out, const std::vector<std::tuple<int, float, float>>& golden) {
  9797. for (const auto& [i, gl, gr] : golden)
  9798. CHECK (std::abs (out.l[(std::size_t) i] - gl) < 2e-6f && std::abs (out.r[(std::size_t) i] - gr) < 2e-6f);
  9799. };
  9800. // IIR crossovers, limiter on, high band compressing, mid bypassed, low compressing, +2 dB output
  9801. 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 }));
  9802. CHECK (oa.latency == 0);
  9803. check (oa,
  9804. { { 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 } });
  9805. // linear phase (FIR, 52 frames of latency), overlapping mid transitions, low band off, limiter off
  9806. 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 }));
  9807. CHECK (ob.latency == 52);
  9808. check (ob,
  9809. { { 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 } });
  9810. // linear phase, hard compression everywhere, limiter on, mid crossovers crossed (the FIR mid band is silent)
  9811. 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 }));
  9812. CHECK (oc.latency == 52);
  9813. check (oc,
  9814. { { 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 } });
  9815. // the slot's parameters carry FL's raw controls exactly; after the project file's 4-decimal rounding the
  9816. // effect built with FL's state still uses the exact controls
  9817. const auto st = multiband ({ 42598, 1, 1, 0, 15604, 25782, 53524, 32768, 11, 298, 51118, 0, 14994, 5037, 24736, 55755, 26870, 221,
  9818. 1090, 49807, 0, 5037, 21845, 47074, 32768, 550, 1090, 40632 });
  9819. const auto parsed = lynx::flmb::parse (st);
  9820. CHECK (parsed && parsed->c[4] == 15604);
  9821. lynx::EffectParams p {};
  9822. lynx::flmb::toParams (*parsed, p.data());
  9823. for (int i = 0; i < lynx::flmb::controls; ++i)
  9824. 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]);
  9825. lynx::EffectParams rounded = p;
  9826. for (auto& v : rounded)
  9827. v = std::round (v * 10000.0f) / 10000.0f;
  9828. std::vector<float> l1 (inL), r1 (inR), l2 (inL), r2 (inR);
  9829. auto exact = lynx::flmb::make();
  9830. auto fromState = lynx::flmb::make (st);
  9831. exact->prepare (44100.0);
  9832. fromState->prepare (44100.0);
  9833. for (int s0 = 0; s0 < 4096; s0 += 512)
  9834. {
  9835. exact->process (l1.data() + s0, r1.data() + s0, 512, p, 120.0);
  9836. fromState->process (l2.data() + s0, r2.data() + s0, 512, rounded, 120.0);
  9837. }
  9838. CHECK (l1 == l2 && r1 == r2);
  9839. CHECK (lynx::flmb::paramText (4, p[4]) == "6302 Hz");
  9840. CHECK (lynx::flmb::paramText (3, 0.5f) == "off");
  9841. // the FL import builds the exact effect with FL's state kept
  9842. const auto slot = lynx::effectFromFl ("Fruity Multiband Compressor", st);
  9843. CHECK (slot && slot->kind == lynx::EffectKind::fruityMultiband && slot->state == st && slot->params[4] == p[4]);
  9844. // unknown versions are rejected
  9845. CHECK (! lynx::flmb::parse ({ 1, 0, 0, 0 }));
  9846. }
  9847. // REA_rodada54: input of the stereo-effect goldens (the installation DLLs' output for the same input, EXEC)
  9848. static std::pair<std::vector<float>, std::vector<float>> stereoFxInput()
  9849. {
  9850. std::vector<float> l (4096), r (4096);
  9851. for (int i = 0; i < 4096; ++i)
  9852. {
  9853. 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));
  9854. r[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / 44100.0));
  9855. }
  9856. return { l, r };
  9857. }
  9858. static std::vector<std::uint8_t> i32State (std::initializer_list<int> words)
  9859. {
  9860. std::vector<std::uint8_t> d;
  9861. for (const auto w : words)
  9862. for (int b = 0; b < 4; ++b)
  9863. d.push_back ((std::uint8_t) ((std::uint32_t) w >> (8 * b)));
  9864. return d;
  9865. }
  9866. static void checkStereoFx (lynx::EffectSlot slot, const std::vector<std::tuple<int, float, float>>& golden)
  9867. {
  9868. auto [l, r] = stereoFxInput();
  9869. auto fx = lynx::makeEffect (slot);
  9870. fx->prepare (44100.0);
  9871. for (int s = 0; s < 4096; s += 512)
  9872. fx->process (l.data() + s, r.data() + s, 512, slot.params, 120.0);
  9873. for (const auto& [i, gl, gr] : golden)
  9874. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  9875. }
  9876. TEST (stereo_shaper_engine)
  9877. {
  9878. // Fruity Stereo Shaper (REA_rodada54 §2): matrix with cross terms, delay on R, all-pass on L, effects post / pre
  9879. auto slot = lynx::effectFromFl ("Fruity Stereo Shaper", i32State ({ 0, -3050, 12800, 9000, 2000, 1408, -2496, 0, 1 }));
  9880. CHECK (slot && slot->kind == lynx::EffectKind::stereoShaper);
  9881. if (slot)
  9882. checkStereoFx (*slot, { { 100, 0.0438031591f, 0.0f }, { 1000, 1.11109531f, -0.781641483f }, { 2047, -0.73191303f, 0.483852893f },
  9883. { 3000, 0.950346172f, -0.769737065f }, { 4095, 0.251555145f, 0.153306276f } });
  9884. slot = lynx::effectFromFl ("Fruity Stereo Shaper", i32State ({ 0, 0, 12800, 12800, 0, -2656, 2184, 0, 0 }));
  9885. CHECK (slot.has_value());
  9886. if (slot)
  9887. checkStereoFx (*slot, { { 100, 0.0f, -0.104282036f }, { 1000, 0.0f, 1.08441091f }, { 2047, 0.463494211f, -0.93754214f },
  9888. { 3000, -1.49942112f, 0.820726812f }, { 4095, -1.31784308f, -0.057449434f } });
  9889. // controls round-trip through the slot parameters; the Patcher's difference bus is the same engine flagged
  9890. const auto st = lynx::stereofx::parseStereoShaper (i32State ({ 0, -3050, 12800, 9000, 2000, 1408, -2496, 2, 1 }));
  9891. CHECK (st.has_value());
  9892. if (st)
  9893. {
  9894. lynx::EffectParams p {};
  9895. lynx::stereofx::stereoShaperToParams (*st, p.data());
  9896. const auto back = lynx::stereofx::stereoShaperFromParams (p.data());
  9897. CHECK (back.rightToLeft == -3050 && back.left == 12800 && back.right == 9000 && back.leftToRight == 2000 && back.delay == 1408
  9898. && back.phase == -2496 && back.post == 1);
  9899. const auto head = lynx::patcherBranchHead ("Fruity Stereo Shaper", i32State ({ 0, -3050, 12800, 9000, 2000, 1408, -2496, 2, 1 }), 2);
  9900. CHECK (head.valid && head.slot && head.slot->kind == lynx::EffectKind::stereoShaper && head.slot->params[7] == 1.0f);
  9901. }
  9902. CHECK (! lynx::stereofx::parseStereoShaper ({ 1, 2, 3 }));
  9903. }
  9904. TEST (frequency_splitter_engine)
  9905. {
  9906. // Frequency Splitter (REA_rodada62): our own states through Frequency Splitter_x64.dll from its creation (gains
  9907. // ramping up from 0, FFT response faded in), blocks of 512 at 44.1 kHz; every band sent to its own destination
  9908. // (1 low, 2 mid, 3 high). IIR modes are bit-exact; the FFT mode's residual is the plugin's single-precision FFT.
  9909. using Gold = std::vector<std::tuple<int, float, float>>;
  9910. struct Case
  9911. {
  9912. std::vector<int> words;
  9913. int mute, latency;
  9914. std::array<Gold, 4> gold; // low, mid, high sends, summed output
  9915. };
  9916. const std::vector<Case> cases {
  9917. { { 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
  9918. { { 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
  9919. { { 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
  9920. { { 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
  9921. { { 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
  9922. { { 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
  9923. { { 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
  9924. };
  9925. auto [l, r] = stereoFxInput();
  9926. for (const auto& c : cases)
  9927. {
  9928. auto words = c.words;
  9929. words.insert (words.begin(), 4);
  9930. for (const int w : { 0, 0, 0, c.mute })
  9931. words.push_back (w);
  9932. std::vector<std::uint8_t> st;
  9933. for (const auto w : words)
  9934. for (int b = 0; b < 4; ++b)
  9935. st.push_back ((std::uint8_t) ((std::uint32_t) w >> (8 * b)));
  9936. st.push_back (0);
  9937. st.resize (st.size() + 24, 0);
  9938. const auto s = lynx::fsplit::parseState (st);
  9939. CHECK (s.has_value());
  9940. if (! s)
  9941. continue;
  9942. lynx::fsplit::Engine e;
  9943. for (int i = 0; i < lynx::fsplit::engineParams; ++i)
  9944. e.setParameter (i, s->params[(std::size_t) i]);
  9945. e.prepare (44100.0);
  9946. e.reset (false);
  9947. std::array<std::vector<float>, 8> o;
  9948. for (auto& v : o)
  9949. v.assign (4096, 0.0f);
  9950. for (int b = 0; b < 4096; b += 512)
  9951. {
  9952. float* out[8];
  9953. for (int k = 0; k < 8; ++k)
  9954. out[k] = o[(std::size_t) k].data() + b;
  9955. e.process (l.data() + b, r.data() + b, out, 512);
  9956. }
  9957. CHECK (e.latencySamples() == c.latency);
  9958. const auto tol = c.latency > 0 ? 2e-5f : 2e-6f;
  9959. for (int k = 0; k < 4; ++k)
  9960. for (const auto& [i, gl, gr] : c.gold[(std::size_t) k])
  9961. CHECK (std::abs (o[(std::size_t) (2 * k)][(std::size_t) i] - gl) <= tol * std::max (1.0f, std::abs (gl))
  9962. && std::abs (o[(std::size_t) (2 * k + 1)][(std::size_t) i] - gr) <= tol * std::max (1.0f, std::abs (gr)));
  9963. // the mixer effect: the summed output (FL's main output) of an instance that has been running, and a
  9964. // Patcher bus = the bands sent to it
  9965. auto slot = lynx::effectFromFl ("Frequency Splitter", st);
  9966. CHECK (slot && slot->kind == lynx::EffectKind::frequencySplitter);
  9967. const auto head = lynx::patcherBranchHead ("Frequency Splitter", st, 3);
  9968. CHECK (head.valid && head.slot && head.slot->params[lynx::fsplit::outputHigh] == 1.0f
  9969. && head.slot->params[lynx::fsplit::outputLow] == 0.0f);
  9970. if (slot && head.slot)
  9971. {
  9972. lynx::fsplit::Engine settled;
  9973. for (int i = 0; i < lynx::fsplit::engineParams; ++i)
  9974. settled.setParameter (i, s->params[(std::size_t) i]);
  9975. settled.prepare (44100.0);
  9976. settled.reset (true);
  9977. std::vector<float> sl (4096), sr (4096), hl (4096), hr (4096);
  9978. float* out[8] { nullptr, nullptr, nullptr, nullptr, hl.data(), hr.data(), sl.data(), sr.data() };
  9979. settled.process (l.data(), r.data(), out, 4096);
  9980. const lynx::EffectSlot* both[2] { &*slot, &*head.slot };
  9981. for (const auto* p : both)
  9982. {
  9983. auto fx = lynx::makeEffect (*p);
  9984. fx->prepare (44100.0);
  9985. auto ml = l, mr = r;
  9986. for (int b = 0; b < 4096; b += 300)
  9987. fx->process (ml.data() + b, mr.data() + b, std::min (300, 4096 - b), p->params, 120.0);
  9988. const auto& wl = p == &*slot ? sl : hl;
  9989. const auto& wr = p == &*slot ? sr : hr;
  9990. CHECK (ml == wl && mr == wr);
  9991. CHECK (fx->latencySamples() == c.latency);
  9992. }
  9993. }
  9994. }
  9995. CHECK (! lynx::patcherBranchHead ("Frequency Splitter", std::vector<std::uint8_t> (0x58, 0), 2).valid); // no band sent to bus 2
  9996. CHECK (lynx::fsplit::paramText (2, 3.0f / 7.0f) == "24 dB/oct" && lynx::fsplit::paramText (11, 0.5f) == "0.0 dB");
  9997. CHECK (lynx::fsplit::paramText (0, 0.0f) == "30.0 Hz" && lynx::fsplit::paramText (4, 1.0f) == "FFT");
  9998. }
  9999. TEST (stereo_enhancer_transient_spreader_engines)
  10000. {
  10001. // REA_rodada54 §3–5: own states against the installation's DLLs (goldens = the DLL's output, EXEC)
  10002. auto se = lynx::effectFromFl ("Fruity Stereo Enhancer", i32State ({ -18, 300, -54, -148, 1, 1 }));
  10003. CHECK (se && se->kind == lynx::EffectKind::stereoEnhancer);
  10004. if (se)
  10005. checkStereoFx (*se, { { 100, 0.0f, 0.00309863733f }, { 1000, 0.0101111531f, -0.110266283f }, { 2047, -0.688752949f, -1.93998075f },
  10006. { 3000, -0.283846498f, -0.739818454f }, { 4095, 0.191147864f, 2.70425582f } });
  10007. auto tpState = i32State ({ 1, 138, 116, 64, 90, 2, 0, 70, 100, 1 });
  10008. tpState.push_back (5);
  10009. for (const auto w : i32State ({ 0, 50 }))
  10010. tpState.push_back (w);
  10011. auto tp = lynx::effectFromFl ("Transient Processor", tpState);
  10012. CHECK (tp && tp->kind == lynx::EffectKind::transientProcessor);
  10013. if (tp)
  10014. checkStereoFx (*tp, { { 100, 0.0568947345f, 0.124077775f }, { 1000, -0.326598823f, -0.193128616f }, { 2047, 0.612462878f, -0.0517415144f },
  10015. { 3000, -0.535302937f, -0.446303189f }, { 4095, -0.634670973f, 0.316044629f } });
  10016. auto spState = i32State ({ 0x10003, 7861, 3215, 106000, 1, 1, 0 });
  10017. spState.push_back (1);
  10018. spState.push_back (1);
  10019. auto sp = lynx::effectFromFl ("Spreader", spState);
  10020. CHECK (sp && sp->kind == lynx::EffectKind::spreader);
  10021. if (sp)
  10022. checkStereoFx (*sp, { { 100, 0.11841587f, -0.0958916694f }, { 1000, 1.40852344f, 0.087452054f }, { 2047, 1.07267976f, -2.02493715f },
  10023. { 3000, -0.781082511f, 0.811192274f }, { 4095, -1.80673707f, 0.89967829f } });
  10024. // unknown layouts keep the earlier fallback; FL automation maps one to one
  10025. CHECK (! lynx::stereofx::parseSpreader (i32State ({ 0x10001, 1, 2, 3, 4, 5 })));
  10026. CHECK (! lynx::stereofx::parseTransientProcessor (i32State ({ 2, 1, 2, 3, 4, 5, 6, 7, 8, 9 })));
  10027. const auto map = lynx::effectParamFromFl ("Transient Processor", 6);
  10028. CHECK (map && map->param == 6 && map->convert (0.25f) == 0.25f);
  10029. }
  10030. TEST (fruity_dynamics_engines)
  10031. {
  10032. // REA_rodada48: Fruity Limiter / Fruity Compressor, states of our own against the installation's DLLs
  10033. // (goldens = the DLL's output for the same input, EXEC; blocks of 512 at 44.1 kHz)
  10034. std::vector<float> inL (4096), inR (4096), side (4096);
  10035. for (int i = 0; i < 4096; ++i)
  10036. {
  10037. 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));
  10038. inR[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / 44100.0));
  10039. side[(std::size_t) i] = (float) (0.9 * std::sin (2.0 * 3.141592653589793 * 55.0 * i / 44100.0));
  10040. }
  10041. auto put = [] (std::vector<std::uint8_t>& b, std::int32_t v) {
  10042. std::uint8_t q[4];
  10043. std::memcpy (q, &v, 4);
  10044. b.insert (b.end(), q, q + 4);
  10045. };
  10046. auto limiterState = [&] (std::array<int, 18> p, int k = 0, int exact = 0) {
  10047. std::vector<std::uint8_t> b;
  10048. put (b, 7);
  10049. for (auto v : p)
  10050. put (b, v);
  10051. b.resize (0xA4, 0);
  10052. put (b, k);
  10053. b.push_back ((std::uint8_t) exact);
  10054. return b;
  10055. };
  10056. auto compressorState = [&] (int version, std::array<int, 6> p, int flags, int k = 0) {
  10057. std::vector<std::uint8_t> b;
  10058. put (b, version);
  10059. for (auto v : p)
  10060. put (b, v);
  10061. put (b, flags);
  10062. if (version > 2)
  10063. put (b, k);
  10064. return b;
  10065. };
  10066. struct Out { std::vector<float> l, r; int latency = 0; };
  10067. auto run = [&] (const lynx::EffectSlot& slot, bool sidechain) {
  10068. auto fx = lynx::makeEffect (slot);
  10069. fx->prepare (44100.0);
  10070. Out o { inL, inR };
  10071. for (int s = 0; s < 4096; s += 512)
  10072. {
  10073. if (sidechain)
  10074. fx->setSidechain (side.data() + s, side.data() + s);
  10075. fx->process (o.l.data() + s, o.r.data() + s, 512, slot.params, 120.0);
  10076. }
  10077. o.latency = fx->latencySamples();
  10078. return o;
  10079. };
  10080. auto check = [] (const Out& out, const std::vector<std::tuple<int, float, float>>& golden) {
  10081. for (const auto& [i, gl, gr] : golden)
  10082. CHECK (std::abs (out.l[(std::size_t) i] - gl) < 2e-6f && std::abs (out.r[(std::size_t) i] - gr) < 2e-6f);
  10083. };
  10084. auto limiter = [] (const std::vector<std::uint8_t>& st) {
  10085. auto slot = lynx::effectFromFl ("Fruity Limiter", st);
  10086. CHECK (slot && slot->kind == lynx::EffectKind::fruityLimiter);
  10087. return *slot;
  10088. };
  10089. // look-ahead limiter: +4 dB gain into a −2.5 dB ceiling, 2 ms attack (88 samples of latency); bit-identical
  10090. 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);
  10091. CHECK (a.latency == 88);
  10092. check (a, { { 100, 0.0176312141f, 0.621841073f }, { 1000, 0.401142687f, -0.442239583f }, { 2047, -0.19392401f, 0.499310732f },
  10093. { 3000, 0.22835508f, -0.267458469f }, { 4095, -0.089728713f, -0.107289009f } });
  10094. // compressor section with a positive knee, 2 compressor curve stages and soft saturation (port within −143 dB)
  10095. check (run (limiter (limiterState ({ 1000, 900, 1000, 0, 3, 5000, 3, 2882, 400, 600, 400, 1115, 6666, 2, 650, 1000, 0, 4187 })), false),
  10096. { { 100, -0.00208989787f, 0.0100310864f }, { 1000, -0.0545219742f, -0.0651496574f }, { 2047, 0.177322432f, -0.230846375f },
  10097. { 3000, -0.159136176f, -0.183688983f }, { 4095, -0.300143838f, 0.299418598f } });
  10098. // sidechained compressor (side input 1) with a negative knee, noise gate and the exact-ceiling option
  10099. 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),
  10100. { { 100, -0.00186592329f, 0.00895605329f }, { 1000, -0.0375732854f, -0.0448972471f }, { 2047, 0.124697573f, -0.162336946f },
  10101. { 3000, -0.111069582f, -0.128206298f }, { 4095, -0.218446419f, 0.21791859f } });
  10102. auto compressor = [] (const std::vector<std::uint8_t>& st) {
  10103. auto slot = lynx::effectFromFl ("Fruity Compressor", st);
  10104. CHECK (slot && slot->kind == lynx::EffectKind::fruityCompressor);
  10105. return *slot;
  10106. };
  10107. // Vintage type, look-ahead flag, side input 1: bit-identical
  10108. const auto ca = run (compressor (compressorState (3, { -200, 40, 30, 50, 300, 2 }, 1, 1)), true);
  10109. CHECK (ca.latency == 64);
  10110. check (ca, { { 100, 0.0795554966f, -0.230549783f }, { 1000, 0.483903229f, 0.0241768137f }, { 2047, -0.596430957f, -0.441769004f },
  10111. { 3000, 0.597724319f, -0.18129006f }, { 4095, 0.607042372f, 0.435067475f } });
  10112. // Soft/R, version 2 without the flag (the output is still 64 frames late, but not reported): bit-identical
  10113. const auto cb = run (compressor (compressorState (2, { -150, 80, 0, 150, 100, 7 }, 0)), false);
  10114. CHECK (cb.latency == 0);
  10115. check (cb, { { 100, 0.0813739002f, -0.235819489f }, { 1000, 0.264188439f, 0.0131994039f }, { 2047, -0.260424435f, -0.192893147f },
  10116. { 3000, 0.251500547f, -0.0762802213f }, { 4095, 0.252972007f, 0.181305125f } });
  10117. // the slot's parameters carry FL's raw controls exactly (and survive the 4-decimal project file)
  10118. const auto st = limiterState ({ 1261, 995, 1303, 1115, 3, 5000, 3, 2882, 264, 244, -590, 1115, 6666, 1, 650, 1000, 0, 4187 }, 1);
  10119. const auto parsed = lynx::fldyn::parseLimiter (st);
  10120. CHECK (parsed && parsed->sidechain == 1 && ! parsed->exactCeiling);
  10121. const auto slot = limiter (st);
  10122. for (int i = 0; i < lynx::fldyn::limiterControls; ++i)
  10123. {
  10124. const auto rounded = std::round (slot.params[(std::size_t) i] * 10000.0f) / 10000.0f;
  10125. 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]);
  10126. }
  10127. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityLimiter, 3, slot.params[3], 120.0) == "2.0 ms");
  10128. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityCompressor, 5, 2.0f / 7.0f, 120.0) == "Vintage");
  10129. // unreadable states keep the old approximation
  10130. CHECK (lynx::effectFromFl ("Fruity Limiter", { 7, 0, 0, 0 }).has_value() == false
  10131. || lynx::effectFromFl ("Fruity Limiter", { 7, 0, 0, 0 })->kind != lynx::EffectKind::fruityLimiter);
  10132. CHECK (! lynx::fldyn::parseCompressor ({ 4, 0, 0, 0 }));
  10133. }
  10134. TEST (maximus_engine)
  10135. {
  10136. // REA_rodada40: the engine on its own (MaximusProcessor), states of our own against the installation's DLLs
  10137. // (goldens = the DLL's output for the same input, EXEC), and the containers of the mixer effect and Vocodex
  10138. std::vector<float> inL (4096), inR (4096);
  10139. for (int i = 0; i < 4096; ++i)
  10140. {
  10141. 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));
  10142. inR[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / 44100.0));
  10143. }
  10144. auto run = [&] (std::shared_ptr<const lynx::hlfx::MaximusPreset> preset, int mix) {
  10145. lynx::hlfx::MaximusProcessor m;
  10146. m.prepare (std::move (preset), 44100.0, mix);
  10147. std::vector<float> l (inL), r (inR);
  10148. for (int s = 0; s < 4096; s += 512)
  10149. m.process (l.data() + s, r.data() + s, 512);
  10150. return std::make_pair (l, r);
  10151. };
  10152. using Env = std::vector<std::array<double, 4>>;
  10153. const Env def { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 } };
  10154. const Env knee { { 0.0, 0.0, 0.0, 0 }, { 0.35, 0.35, 0.0, 0 }, { 0.65, 0.6, 0.36, 7 } };
  10155. const Env t0 { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.14, 0 }, { 0.5, 0.5, 0.0, 0 } };
  10156. const Env m0 { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 }, { 0.5, 0.6, 0.085, 0 } };
  10157. 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,
  10158. int sus = 2882, int rms = 0) { return std::array<int, 13> { mode, pre, post, sep, sat, ceil, att, rel2, 2, 5000, 3, sus, rms }; };
  10159. auto check = [] (const std::pair<std::vector<float>, std::vector<float>>& out, const std::vector<std::tuple<int, float, float>>& golden) {
  10160. for (const auto& [i, gl, gr] : golden)
  10161. CHECK (std::abs (out.first[(std::size_t) i] - gl) < 2e-6f && std::abs (out.second[(std::size_t) i] - gr) < 2e-6f);
  10162. };
  10163. // RMS detectors, saturation mode B, a 5 Hz low cut, 12 dB high split (SG_x64.dll: port within −152 dB)
  10164. auto st3 = maximusTestState ({ 19800, 1, 48065, 0, 1115, 1000, 0 },
  10165. { band (0, 1500, 1000, 0, 400, 900, 1115, 0, 2882, 1), band (0, 1340, 1000, 0, 0, 1000, 1115, 0, 1570, 1),
  10166. band (1, 1000, 1200, 0, 1000, 1000), band (0, 1000, 1000, 0, 250, 1000, 1115, 4464, 2882, 1) },
  10167. { def, def, knee, def, t0, def, m0, def }, 0);
  10168. const auto p3 = lynx::hlfx::makeMaximusPreset (st3);
  10169. CHECK (p3 != nullptr);
  10170. check (run (p3, 1000), { { 100, 0.0f, 0.0f }, { 1000, 0.87061065f, 0.64004499f }, { 2047, -0.97266865f, 0.60940075f },
  10171. { 3000, 0.96658099f, 0.9250561f }, { 4095, 0.99094611f, -0.964059f } });
  10172. // the same with solo on (UI byte 0x1EF) and the low band selected (0x1E0): bit-identical to SG_x64.dll
  10173. auto solo = st3;
  10174. solo[0x1ef] = 1;
  10175. std::memset (solo.data() + 0x1e0, 0, 4);
  10176. check (run (lynx::hlfx::makeMaximusPreset (solo), 1000),
  10177. { { 100, 3.2922967e-06f, 0.00036916698f }, { 1000, 0.15653539f, -0.047913823f }, { 2047, -0.17336531f, 0.019099623f },
  10178. { 3000, 0.17185682f, -0.017173048f }, { 4095, 0.16714551f, 0.019473195f } });
  10179. // the legacy linear-phase FIR band split (UI byte 0x1FD; SG_x64.dll: port within −148 dB)
  10180. auto fir = st3;
  10181. fir[0x1fd] = 1;
  10182. check (run (lynx::hlfx::makeMaximusPreset (fir), 1000),
  10183. { { 100, 0.0f, 0.0f }, { 1000, 0.97986746f, 0.18555209f }, { 2047, -0.98930448f, -0.95970845f },
  10184. { 3000, 0.98856646f, -0.44592777f }, { 4095, 0.99069679f, 0.94382554f } });
  10185. // Soundgoodizer_x64.dll's own build with a v4 file (its presets' layout): a copy of the round-36 state without
  10186. // oversampling, through a byte-identical copy of the installation's DLL with this file as its preset (EXEC)
  10187. const auto st2 = maximusTestState ({ 19800, 1, 48065, 1, 891, 600, 32768 },
  10188. { band (1, 1264, 1296, 500, -500, 1200), band (0, 1340, 1000, -348, 0, 1000, 1115, 0, 1570),
  10189. band (0, 1364, 1152, 0, 0, 1000, 1115, 0, 1186), band (0, 1000, 1000, 0, -196, 1000, 1115, 4464, 1538) },
  10190. { def, def, knee, def, t0, def, m0, def }, 0);
  10191. std::vector<std::uint8_t> v4 { 4, 0, 0, 0 };
  10192. v4.insert (v4.end(), st2.begin() + 4, st2.begin() + 0x1f5);
  10193. v4.insert (v4.end(), st2.begin() + 0x1fe, st2.end());
  10194. const auto p4 = lynx::hlfx::makeMaximusPreset (v4, lynx::hlfx::MaximusBuild::soundgoodizer);
  10195. CHECK (p4 != nullptr);
  10196. check (run (p4, 600), { { 100, 0.0f, 0.0f }, { 1000, -0.60926896f, -0.6652832f }, { 2047, 0.40872875f, 0.36375371f },
  10197. { 3000, -0.41941205f, -0.61312866f }, { 4095, -0.12878418f, 0.27944946f } });
  10198. CHECK (lynx::hlfx::makeMaximusPreset (st2, lynx::hlfx::MaximusBuild::soundgoodizer) != nullptr); // v7: also read by that build
  10199. // the mixer effect's container and the effect itself
  10200. lynx::maximusfx::Blob blob;
  10201. blob.build = lynx::hlfx::MaximusBuild::soundgoodizer;
  10202. blob.mix = 600;
  10203. blob.state = v4;
  10204. const auto packed = lynx::maximusfx::pack (blob);
  10205. const auto back = lynx::maximusfx::unpack (packed);
  10206. CHECK (back && back->build == blob.build && back->mix == 600 && back->state == v4);
  10207. auto slot = lynx::makeEffectSlot (lynx::EffectKind::maximus);
  10208. slot.state = packed;
  10209. auto fx = lynx::makeEffect (slot);
  10210. fx->prepare (44100.0);
  10211. {
  10212. std::vector<float> l (inL), r (inR);
  10213. for (int s = 0; s < 4096; s += 1024) // longer host blocks are split into the engine's 512-frame calls
  10214. fx->process (l.data() + s, r.data() + s, 1024, slot.params, 120.0);
  10215. const auto ref = run (p4, 600);
  10216. CHECK (l == ref.first && r == ref.second);
  10217. }
  10218. const std::vector<std::uint8_t> sgState { 3, 0, 0, 0, 2, 0, 0, 0, 0x78, 2, 0, 0 };
  10219. const auto sg = lynx::maximusfx::parseSoundgoodizer (sgState);
  10220. CHECK (sg && sg->preset == 2 && sg->amount == 632 && std::string (lynx::maximusfx::soundgoodizerPresetFile (2)) == "c.dat");
  10221. CHECK (lynx::soundgoodizerPresetPath (sgState) == "Plugins\\Fruity\\Effects\\Soundgoodizer\\Data\\Presets\\c.dat");
  10222. CHECK (lynx::soundgoodizerExact (sgState, v4).has_value() && ! lynx::soundgoodizerExact (sgState, {}).has_value());
  10223. // without the preset files (no FL installation) the approximation keeps the plugin's state and its name
  10224. {
  10225. const auto approx = lynx::effectFromFl ("Soundgoodizer", sgState);
  10226. CHECK (approx && lynx::isSoundgoodizerApprox (*approx) && lynx::projectcontents::effectSlotName (*approx) == "Soundgoodizer");
  10227. const auto st = approx ? lynx::maximusfx::parseSoundgoodizer (approx->state) : std::nullopt;
  10228. CHECK (st && st->preset == 2 && st->amount == 632 && approx->branches.size() == 1 && approx->branches[0].chain.size() == 2);
  10229. CHECK (! lynx::isSoundgoodizerApprox (lynx::makeEffectSlot (lynx::EffectKind::parallel))); // a Patcher container stays one
  10230. const auto edited = lynx::soundgoodizerApproxSlot (0, 1000);
  10231. CHECK (lynx::isSoundgoodizerApprox (edited) && lynx::maximusfx::parseSoundgoodizer (edited.state)->preset == 0);
  10232. }
  10233. // Vocodex's appended installation data survives a round trip and leaves FL's state untouched
  10234. const std::vector<std::uint8_t> flState { 8, 0, 0, 0, 1, 2, 3 };
  10235. lynx::vocodex::InstallData data;
  10236. data.sgDat = st2;
  10237. data.carrierState = { 9, 9, 9 };
  10238. const auto joined = lynx::vocodex::appendInstallData (flState, data);
  10239. const auto [raw, files] = lynx::vocodex::splitInstallData (joined);
  10240. CHECK (raw == flState && files.sgDat == st2 && files.carrierState == data.carrierState);
  10241. CHECK (lynx::vocodex::splitInstallData (flState).first == flState);
  10242. }
  10243. // ---- Fruity Reeverb 2 (REA_rodada49) --------------------------------------------------------------------------------
  10244. // Goldens: the installation's Fruity Reeverb 2_x64.dll (unmodified, rate set before the state, flushed, blocks of 512)
  10245. // on our own states and input; LYNX's engine is bit-identical to it.
  10246. TEST (reeverb2_engine)
  10247. {
  10248. auto hex = [] (const char* h) {
  10249. std::vector<std::uint8_t> v;
  10250. for (std::size_t i = 0; h[i] != 0 && h[i + 1] != 0; i += 2)
  10251. v.push_back ((std::uint8_t) std::stoi (std::string (h + i, 2), nullptr, 16));
  10252. return v;
  10253. };
  10254. constexpr int N = 8192;
  10255. std::vector<float> inL (N), inR (N);
  10256. std::uint32_t seed = 12345;
  10257. for (int i = 0; i < N; ++i)
  10258. {
  10259. seed = (seed * 1103515245u + 12345u) & 0x7fffffffu;
  10260. const auto nz = (float) ((double) seed / 2147483648.0 - 0.5);
  10261. const auto tri = (float) ((i % 200) - 100) / 100.0f;
  10262. const float g = i < 4000 ? 1.0f : 0.0f;
  10263. inL[(std::size_t) i] = 0.5f * tri * g + (i >= 100 && i < 300 ? nz * 0.6f : 0.0f);
  10264. inR[(std::size_t) i] = 0.25f * tri * g;
  10265. }
  10266. struct Golden
  10267. {
  10268. int i;
  10269. float l, r;
  10270. };
  10271. auto run = [&] (const std::vector<std::uint8_t>& state, double bpm, const std::vector<Golden>& gold) {
  10272. const auto slot = lynx::reeverb2FromFl (state);
  10273. CHECK (slot.has_value() && slot->kind == lynx::EffectKind::reeverb2);
  10274. if (! slot)
  10275. return;
  10276. auto fx = lynx::makeEffect (*slot);
  10277. fx->prepare (44100.0);
  10278. fx->reset();
  10279. std::vector<float> l (inL), r (inR);
  10280. for (int s = 0; s < N; s += 512)
  10281. fx->process (l.data() + s, r.data() + s, 512, slot->params, bpm);
  10282. for (const auto& g : gold)
  10283. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  10284. };
  10285. // modulation, early reflections, negative separation, filters, predelay
  10286. run (hex ("112700004b000000780000001e0000004600000055000000190000003c0000009600000090010000e2ffffff6e000000640000008c00000084030000b00400000000"),
  10287. 120.0, { { 777, 0.263778746f, 0.131889373f }, { 1555, 0.187972173f, 0.0587911084f }, { 3333, 0.10398978f, 0.0566705838f },
  10288. { 5000, 0.087177977f, -0.0197417438f }, { 8191, 0.0769432634f, -0.282023013f } });
  10289. // Mid/Side input, tempo-synced predelay (27 ticks at 128 BPM), positive separation
  10290. run (hex ("112700004b000000280000001b00000032000000640000000f0000002800000064000000f401000028000000800000000000000040000000ac020000000000000101"),
  10291. 128.0, { { 777, 0.38499999f, 0.192499995f }, { 1555, 0.275000006f, 0.137500003f }, { 3333, 0.164420277f, 0.0826604739f },
  10292. { 5000, -0.00861243438f, -0.00140450173f }, { 8191, -0.00873835851f, 0.00888271444f } });
  10293. // version 10000 (no modulation controls), everything off or at its extreme, dry 0
  10294. run (hex ("1027000013000000dd000000000000006400000000000000c8000000dd00000014000000d00700004000000000000000a0000000a00000000000"),
  10295. 120.0, { { 777, 0.0f, 0.0f }, { 1555, 0.0f, 0.0f }, { 3333, 0.0632086769f, 0.0632086769f }, { 5000, -0.108072646f, -0.108072646f },
  10296. { 8191, 0.144686908f, 0.144686908f } });
  10297. // the parameters map back to FL's integers; unsupported states fall back to the approximation
  10298. lynx::reeverb2::Settings s;
  10299. s.p = { 3000, 5, 384, 1, 0, 200, 221, 20, 2000, -64, 160, 0, 160, 2048, 1 };
  10300. s.tempoPredelay = true;
  10301. lynx::EffectParams p {};
  10302. lynx::reeverb2::toParams (s, p);
  10303. CHECK (lynx::reeverb2::fromParams (p) == s);
  10304. CHECK (! lynx::reeverb2FromFl ({ 0x11, 0x27, 0, 0, 1, 0 }).has_value());
  10305. CHECK (! lynx::reeverb2FromFl ({ 0x12, 0x27, 0, 0 }).has_value());
  10306. const auto approx = lynx::effectFromFl ("Fruity Reeverb 2", { 0x11, 0x27, 0, 0 }, 120.0);
  10307. CHECK (! approx || approx->kind != lynx::EffectKind::reeverb2);
  10308. CHECK (lynx::reeverb2ParamText (0, 0.0f) == "off" && lynx::reeverb2ParamText (5, 1.0f) == "20.0 s");
  10309. // Fruity Reeverb (the first version): Fruity Reeverb_x64.dll goldens on our own states (denormals flushed, as the
  10310. // plugin runs and as LYNX renders)
  10311. lynx::ScopedFlushDenormals ftz;
  10312. auto run1 = [&] (const std::vector<std::uint8_t>& state, const std::vector<Golden>& gold) {
  10313. const auto slot = lynx::reeverb1FromFl (state);
  10314. CHECK (slot.has_value() && slot->kind == lynx::EffectKind::reeverb1);
  10315. if (! slot)
  10316. return;
  10317. auto fx = lynx::makeEffect (*slot);
  10318. fx->prepare (44100.0);
  10319. std::vector<float> l (inL), r (inR);
  10320. for (int s = 0; s < N; s += 512)
  10321. fx->process (l.data() + s, r.data() + s, 512, slot->params, 120.0);
  10322. for (const auto& g : gold)
  10323. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  10324. };
  10325. run1 (hex ("01000000204e0000204e000025000000409c000050c300000a00000030750000a861000050c3000060ea0000"),
  10326. { { 777, 0.293731689f, 0.146865845f }, { 1555, 0.20980835f, 0.104904175f }, { 3333, 0.0998796821f, 0.0754205957f },
  10327. { 5000, -0.0994476378f, 0.0116700521f }, { 8191, -0.141061321f, 0.0601170398f } });
  10328. run1 (hex ("0100000000000000000001000000000000000100000001002800000000000100000001000000000000000100"),
  10329. { { 777, -0.016678432f, -4.58686594e-31f }, { 1555, 0.0221031085f, 0.0230635051f }, { 3333, 0.0875115693f, 0.1695389f },
  10330. { 5000, -0.111421227f, -0.295867324f }, { 8191, 0.503498077f, 0.190456569f } });
  10331. CHECK (lynx::reeverb1FromFl (std::vector<std::uint8_t> (44, 0)).has_value());
  10332. CHECK (! lynx::reeverb1FromFl (std::vector<std::uint8_t> (40, 0)).has_value());
  10333. CHECK (lynx::reeverb1ParamText (5, 0.0f) == "Brighter" && lynx::reeverb1ParamText (0, 0.0f) == "off");
  10334. }
  10335. // ---- Fruity Love Philter (REA_rodada66) ---------------------------------------------------------------------------------
  10336. TEST (love_philter_engine)
  10337. {
  10338. // goldens: Fruity Love Philter_x64.dll on our own synthetic states (44.1 kHz, blocks of 512, 120 BPM, live mode),
  10339. // bit-identical on the test machine (denormals flushed, as the plugin runs and as LYNX renders)
  10340. lynx::ScopedFlushDenormals ftz;
  10341. auto hex = [] (const char* h) {
  10342. std::vector<std::uint8_t> v;
  10343. for (std::size_t i = 0; h[i] != 0 && h[i + 1] != 0; i += 2)
  10344. v.push_back ((std::uint8_t) std::stoi (std::string (h + i, 2), nullptr, 16));
  10345. return v;
  10346. };
  10347. constexpr int N = 8192;
  10348. std::vector<float> inL (N), inR (N);
  10349. std::uint32_t seed = 12345;
  10350. for (int i = 0; i < N; ++i)
  10351. {
  10352. seed = (seed * 1103515245u + 12345u) & 0x7fffffffu;
  10353. const auto nz = (float) ((double) seed / 2147483648.0 - 0.5);
  10354. const auto tri = (float) ((i % 200) - 100) / 100.0f;
  10355. const float g = i < 4000 ? 1.0f : 0.0f;
  10356. inL[(std::size_t) i] = 0.5f * tri * g + (i >= 100 && i < 300 ? nz * 0.6f : 0.0f);
  10357. inR[(std::size_t) i] = 0.25f * tri * g;
  10358. }
  10359. struct Golden
  10360. {
  10361. int i;
  10362. float l, r;
  10363. };
  10364. auto run = [&] (const std::vector<std::uint8_t>& state, const std::vector<Golden>& gold) {
  10365. const auto slot = lynx::effectFromFl ("Fruity Love Philter", state, 120.0);
  10366. CHECK (slot.has_value() && slot->kind == lynx::EffectKind::lovePhilter);
  10367. if (! slot || slot->kind != lynx::EffectKind::lovePhilter)
  10368. return;
  10369. auto fx = lynx::makeEffect (*slot);
  10370. fx->prepare (44100.0);
  10371. std::vector<float> l (inL), r (inR);
  10372. for (int s = 0; s < N; s += 512)
  10373. {
  10374. fx->setSongPosition (s * 2.0 * 960.0 / 44100.0);
  10375. fx->process (l.data() + s, r.data() + s, 512, slot->params, 120.0);
  10376. }
  10377. for (const auto& g : gold)
  10378. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  10379. };
  10380. // SVF triple with Low/Band/High, a song-locked volume pattern with tension, a cutoff LFO; Lime HP fed by NEXT, panned
  10381. run (hex (
  10382. "03000c00789cedda316ed44018c5f1cfd90b5050234a0e81702a1a6e4045c11d68e7005c00c40928e96810254590380225e552538471ec"
  10383. "d97c314c841e0259cfff91463bf66e7e3b9ac42fdf582e1151961e776abf1f314ce3b3a5d7f6e4fb8bf1b2b6693c5cf56118e6b79ed5fe"
  10384. "b2f6b7b53fadfd41ed8f1379378d9fd77e88a50d57c3c3f597dcd28ee3fcfaadbe9e8fcb24e272d56238fd4019a73e2c5f3074c1ebf732"
  10385. "745a8ee51507670bce99e89cbff9e5076eb9f8be8e378f9b7df3fcfae2dbba37afcb9c0b391b0eb16ec795ff657cfd6a6aefc7cf17178f"
  10386. "f2f9f6f9fafe8769342c7dfabe58e65e62db7f5338fb74fe4f96acafa5e3f8bbf3eb6b75eb1e75068eb3d3b241b95ef2ffc0d67ebc8bb8"
  10387. "b792b65e2ffc9bfa636a64068e9fc3de44f77a7b93adfc6e7170fec661afa17b64038eb34336e81ed980e3ec900dba4736e0383b6483ee"
  10388. "957aee61ed9fe27a4d63194ffd63fdccf44cc7f972fef42408cf6ce0e06c30a7b6711f93fba238387fee50c3e81e75038eb343dda07b64"
  10389. "038eb34336e81e7b0a1c67873d85ee910d38ce0ed9a07b64038eb34336e81ed980e3ec900dba57625ec3d65bcbc7799cd6b83f95ded478"
  10390. "d60267c70ef745758f1a06c7d9a186d13dea061c6787ba41f7c8061c67876cd03df61438ce0e7b0add231b709c1db241f7c8061c67876c"
  10391. "d03db201c7d9211b74afc4bc86adb7968ff338ad717f2abda9f1ac05ce8e1dee8bea1e350c8eb3430da37bd40d38ce0e7583ee910d38ce"
  10392. "0ed9a07bec29709c1df614ba4736e0383b6483ee910d38ce0ed9a07b64038eb34336e85e89790d5b6f2d1fe7715ae3fe547a53e3590b9c"
  10393. "1d3bdc17d53d6a181c67871a46f7a81b709c1dea06dd231b709c1db241f7d853e0383bec29748f6cc07176c806dd231b709c1db241f7c8"
  10394. "061c67876cd0bd12f31ab6de5a3ecee3b4c6fda9f4a6c6b316383b76b82faa7bd43038ce0e358cee5137e0383bd40dba4736e0383b6483"
  10395. "eeb1a7c07176d853e81ed980e3ec900dba4736e0383b6483ee910d38ce0ed9a07b25e6356cbdb57c9cc7698dfb53e94d8d672d7076ec70"
  10396. "5f54f7a861709c1d6a18dda36ec07176a81b748f6cc07176c806dd634f81e3ecb0a7d03db201c7d9211b748f6cc07176c806dd231b709c"
  10397. "1db241f74acc6bd87a6bf9388fd31af7a7d29b1acf5ae0ecd8e1bea8ee51c3e0383bd430ba47dd80e3ec5037e81ed980e3ec900dbac79e"
  10398. "02c7d9614fa17b64038eb34336e81ed980e3ec900dba4736e0383b6483eec54ff7970e90"),
  10399. { { 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 } });
  10400. // Mango alternate triple with drive, waveshaper, Mod X/Y maps (smoothed), follower map on the volume; ×4 oversampling
  10401. run (hex (
  10402. "03000c00789cedda4d8ed33018c671a788151b8ec0013803993dd7e00e6c7d0a24100bae81c40590e00e5d82c46238c1e0c879db7752b9"
  10403. "8b470c1f4ffe963c759af6371e4b79e675945a4aa96b2f4f5b7f56cad45e3e1d4a79be0c5a7bf9f3f57cd7da326e6fb7f3d3b49e5abef6"
  10404. "a1f537adbf2ae7968f4f7c6b8f5b7fb20ca6e951fb19fd7a3bceefde2eedfbf2fa22bfdf5f6fe7fce9bb4d2bd3e9549d973eadbf742adb"
  10405. "7686e25c86f2df51d7cfe0e0fc6de7203a37ef2fbe70ba200f17678ef3fde3b08f572fbe7fddebebd273816cc07173fe4c36dc0eaebdeb"
  10406. "ff981fcabb2c28cedfeb85c4c70befeb97a5fd580b8dcfcbfba773dbdf433d81e3ec3c7466a46bec9e1d457e7e777bedfd1ff5c4d2c806"
  10407. "1c3f87bd86eeb1d7c07176f6b8d7f85d1ed980e3ec900dba4736e0383b6483ee910d38ce0ed9a07bb5f4358c1e2d1fe7715ae3f1544653"
  10408. "fbd65e6fe650b65349363985e3e7705f54f7a861709c1d6a18dda36ec07176a81b748f6cc07176c806dd634f81e3ecb0a7d03db201c7d9"
  10409. "211b748f6cc07176c806dd231b709c1db241f76ae96b183d5a3ecee3b4c6e3a98ca6c6b316383b76b82faa7bd43038ce0e358cee5137e0"
  10410. "383bd40dba4736e0383b6483eeb1a7c07176d853e81ed980e3ec900dba4736e0383b6483ee910d38ce0ed9a07bb5f4358c1e2d1fe7715a"
  10411. "e3f1544653e3590b9c1d3bdc17d53d6a181c67871a46f7a81b709c1dea06dd231b709c1db241f7d853e0383bec29748f6cc07176c806dd"
  10412. "231b709c1db241f7c8061c67876cd0bd5afa1a468f968ff338adf1782aa3a9f1ac05ce8e1dee8bea1e350c8eb3430da37bd40d38ce0e75"
  10413. "83ee910d38ce0ed9a07bec29709c1df614ba4736e0383b6483ee910d38ce0ed9a07b64038eb34336e85e2d7d0da347cbc7799cd6783c95"
  10414. "d1d478d60267c70ef745758f1a06c7d9a186d13dea061c6787ba41f7c8061c67876cd03df61438ce0e7b0add231b709c1db241f7c8061c"
  10415. "67876cd03db201c7d9211b74af96be86d1a3e5e33c4e6b3c9eca686a3c6b81b36387fba2ba470d83e3ec50c3e81e75038eb343dda07b64"
  10416. "038eb34336e81e7b0a1c67873d85ee910d38ce0ed9a07b64038eb34336e81ed980e3ec900dba574b5fc3e8d1f2711ea7351e4f6534359e"
  10417. "b5c0d9b1c37d51dda386c17176a861748fba01c7d9a16ed03db201c7d9211b748f3d058eb3c39e42f7c8061c67876cd03db201c7d9211b"
  10418. "748f6cc07176c806dd2bbf0040771f82"),
  10419. { { 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 } });
  10420. // band-pass alternate dual with FLAT, low shelf, all-pass with a pan LFO and inverted input, CENTER, no oversampling
  10421. run (hex (
  10422. "03000c00789ceddd418ad44018c5f12f3303baf404e2d243083d7b6fe0dab55b5dd64106bc8427f024aea497ed09da8a95225ff75883bc"
  10423. "01092fff82a22b19fb97a2248faf62184b4494a5c7abdadf444cf3f866e9b5bdfff5f970ae2d9636cdad0de7af7da9fd6327623d5ffe32"
  10424. "be5b7afdfe6dfdb85d2ff2443b1ddae7b17edecfe33f173f5fb598d6ab1fe63e2d179886e0fab30c5dcf1d07670bce8de8dc3f3cfac213"
  10425. "37df8fc3e571b72fcf5fdf7c5bf7dabab45c201b70dc9cff930da7c1bd7712efe56d78d40d38ce0e7583ee910d38ce0ed9a07bec29709c"
  10426. "1df614ba4736e0383b6483ee910d38ce0ed9a07b64038eb34336e85e89b686bdf7968ff338adf1782aa3a9f1ae05ce8e1d9e8bea1e350c"
  10427. "8eb3430da37bd40d38ce0e7583ee910d38ce0ed9a07bec29709c1df614ba4736e0383b6483ee910d38ce0ed9a07b64038eb34336e85ea9"
  10428. "e73ed4feaef64fe9cf95a5bf8ef67b2fca727ebeeecb79c0efb5c0c1d9604e6de33926cf457170fedda186d13dea061c6787ba41f7c806"
  10429. "1c67876cd03df61438ce0e7b0add231b709c1db241f7c8061c67876cd03db201c7d9211b74afd473df6aff19eb9ac6322e69fc706eff05"
  10430. "c9bc8e2fda603c95d1d478d70267c74ecfa9c737cc7170ef1e2ffce9cafffeb5fdec6db477a222785ebab5bf731c9ce738d436ba473d81"
  10431. "e3ecf0efacba4736e0383b6483eeb1a7c07176d853e81ed980e3ec900dba4736e0383b6483ee910d38ce0ed9a07b25da1af6de5b3ecee3"
  10432. "b4c6e3a98ca6c63b18383b76782eaa7bd43038ce0e358cee5137e0383bd40dba4736e0383b6483eeb1a7c07176d853e81ed980e3ec900d"
  10433. "ba4736e0383b6483ee910d38ce0ed9a07b25da1af6de5b3ecee3b4c6e3a98ca6c6bb16383b76782eaa7bd43038ce0e358cee5137e0383b"
  10434. "d40dba4736e0383b6483eeb1a7c07176d853e81ed980e3ec900dba4736e0383b6483ee910d38ce0ed9a07b25da1af6de5b3ecee3b4c6e3"
  10435. "a98ca6c6bb16383b76782eaa7bd43038ce0e358cee5137e0383bd40dba4736e0383b6483eeb1a7c07176d853e81ed980e3ec900dba4736"
  10436. "e0383b6483ee910d38ce0ed9a07b25da1af6de5b3ecee3b4c6e3a98ca6c6bb16383b76782eaa7bd43038ce0e358cee5137e0383bd40dba"
  10437. "4736e0383b6483eeb1a7c07176d853e81ed980e3ec900dba4736e0383b6483ee910d38ce0ed9a07bf11b286e0820"),
  10438. { { 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 } });
  10439. // the slot exposes FL's controls; FL indices map both ways; unreadable states keep the old conversion
  10440. CHECK (lynx::philter::flIndexOf (6) == 6 && lynx::philter::flIndexOf (8) == 9 && lynx::philter::flIndexOf (8 + 5 * 7 + 4) == 8 + 44 * 7 + 9);
  10441. for (int i = 0; i < lynx::philter::paramCount; ++i)
  10442. CHECK (lynx::philter::paramOfFlIndex (lynx::philter::flIndexOf (i)) == i);
  10443. CHECK (lynx::philter::paramOfFlIndex (8 + 3) == 48 && lynx::philter::paramOfFlIndex (8 + 12) == -1 && lynx::philter::paramOfFlIndex (8 + 44 * 7 + 11) == -1);
  10444. CHECK (lynx::philter::parse ({ 3, 0, 12, 0, 1, 2, 3, 4 }) == nullptr);
  10445. CHECK (lynx::effectInfo (lynx::EffectKind::lovePhilter).params.size() == (std::size_t) lynx::philter::paramCount);
  10446. CHECK (lynx::philter::paramText (5, 0.0f) == "pattern 1" && lynx::philter::paramText (8 + 3, 0.5f) == "centred");
  10447. // a control edited in LYNX is applied when the effect is built from the slot (the state keeps FL's value)
  10448. {
  10449. const auto stateA = hex (
  10450. "03000c00789cedda316ed44018c5f1cfd90b5050234a0e81702a1a6e4045c11d68e7005c00c40928e96810254590380225e552538471ec"
  10451. "d97c314c841e0259cfff91463bf66e7e3b9ac42fdf582e1151961e776abf1f314ce3b3a5d7f6e4fb8bf1b2b6693c5cf56118e6b79ed5fe"
  10452. "b2f6b7b53fadfd41ed8f1379378d9fd77e88a50d57c3c3f597dcd28ee3fcfaadbe9e8fcb24e272d56238fd4019a73e2c5f3074c1ebf732"
  10453. "745a8ee51507670bce99e89cbff9e5076eb9f8be8e378f9b7df3fcfae2dbba37afcb9c0b391b0eb16ec795ff657cfd6a6aefc7cf17178f"
  10454. "f2f9f6f9fafe8769342c7dfabe58e65e62db7f5338fb74fe4f96acafa5e3f8bbf3eb6b75eb1e75068eb3d3b241b95ef2ffc0d67ebc8bb8"
  10455. "b792b65e2ffc9bfa636a64068e9fc3de44f77a7b93adfc6e7170fec661afa17b64038eb34336e81ed980e3ec900dba4736e0383b6483ee"
  10456. "957aee61ed9fe27a4d63194ffd63fdccf44cc7f972fef42408cf6ce0e06c30a7b6711f93fba238387fee50c3e81e75038eb343dda07b64"
  10457. "038eb34336e81e7b0a1c67873d85ee910d38ce0ed9a07b64038eb34336e81ed980e3ec900dba57625ec3d65bcbc7799cd6b83f95ded478"
  10458. "d60267c70ef745758f1a06c7d9a186d13dea061c6787ba41f7c8061c67876cd03df61438ce0e7b0add231b709c1db241f7c8061c67876c"
  10459. "d03db201c7d9211b74afc4bc86adb7968ff338ad717f2abda9f1ac05ce8e1dee8bea1e350c8eb3430da37bd40d38ce0e7583ee910d38ce"
  10460. "0ed9a07bec29709c1df614ba4736e0383b6483ee910d38ce0ed9a07b64038eb34336e85e89790d5b6f2d1fe7715ae3fe547a53e3590b9c"
  10461. "1d3bdc17d53d6a181c67871a46f7a81b709c1dea06dd231b709c1db241f7d853e0383bec29748f6cc07176c806dd231b709c1db241f7c8"
  10462. "061c67876cd0bd12f31ab6de5a3ecee3b4c6fda9f4a6c6b316383b76b82faa7bd43038ce0e358cee5137e0383bd40dba4736e0383b6483"
  10463. "eeb1a7c07176d853e81ed980e3ec900dba4736e0383b6483ee910d38ce0ed9a07b25e6356cbdb57c9cc7698dfb53e94d8d672d7076ec70"
  10464. "5f54f7a861709c1d6a18dda36ec07176a81b748f6cc07176c806dd634f81e3ecb0a7d03db201c7d9211b748f6cc07176c806dd231b709c"
  10465. "1db241f74acc6bd87a6bf9388fd31af7a7d29b1acf5ae0ecd8e1bea8ee51c3e0383bd430ba47dd80e3ec5037e81ed980e3ec900dbac79e"
  10466. "02c7d9614fa17b64038eb34336e81ed980e3ec900dba4736e0383b6483eec54ff7970e90");
  10467. auto slot = lynx::philter::slotFromFl (stateA);
  10468. CHECK (slot.has_value() && ! lynx::philter::slotFromFl (hex ("0300000078")).has_value());
  10469. if (slot)
  10470. {
  10471. auto render = [&] (const lynx::EffectSlot& sl) {
  10472. auto fx = lynx::makeEffect (sl);
  10473. fx->prepare (44100.0);
  10474. std::vector<float> l (inL), r (inR);
  10475. for (int s = 0; s < 4096; s += 512)
  10476. {
  10477. fx->setSongPosition (s * 2.0 * 960.0 / 44100.0);
  10478. fx->process (l.data() + s, r.data() + s, 512, sl.params, 120.0);
  10479. }
  10480. return l[3333];
  10481. };
  10482. const auto base = render (*slot);
  10483. auto edited = *slot;
  10484. edited.params[8] = 0.1f; // unit 1 cutoff
  10485. CHECK (base == 0.0f || base != render (edited));
  10486. CHECK (base == render (*slot));
  10487. }
  10488. }
  10489. }
  10490. TEST (fruity_vocoder_round66_goldens)
  10491. {
  10492. // REA_rodada66 §6 goldens against the round-70 engine (core/FruityVocoder), through FL's state import and the mixer
  10493. // effect: Fruity Vocoder_x64.dll on our own states (44.1 kHz, blocks of 512): left = modulator, right = carrier.
  10494. // The first state's attack (5 ms) is below the knob range (10…1000): the plugin uses it as it is (REA_rodada78 §1)
  10495. lynx::ScopedFlushDenormals ftz;
  10496. auto hex = [] (const char* h) {
  10497. std::vector<std::uint8_t> v;
  10498. for (std::size_t i = 0; h[i] != 0 && h[i + 1] != 0; i += 2)
  10499. v.push_back ((std::uint8_t) std::stoi (std::string (h + i, 2), nullptr, 16));
  10500. return v;
  10501. };
  10502. constexpr int N = 8192;
  10503. std::vector<float> inL (N), inR (N);
  10504. std::uint32_t seed = 12345;
  10505. for (int i = 0; i < N; ++i)
  10506. {
  10507. seed = (seed * 1103515245u + 12345u) & 0x7fffffffu;
  10508. const auto nz = (float) ((double) seed / 2147483648.0 - 0.5);
  10509. const auto tri = (float) ((i % 200) - 100) / 100.0f;
  10510. const float g = i < 4000 ? 1.0f : 0.0f;
  10511. inL[(std::size_t) i] = 0.5f * tri * g + (i >= 100 && i < 300 ? nz * 0.6f : 0.0f);
  10512. inR[(std::size_t) i] = 0.25f * tri * g;
  10513. }
  10514. struct Golden
  10515. {
  10516. int i;
  10517. float v;
  10518. };
  10519. auto run = [&] (const std::vector<std::uint8_t>& state, const std::vector<Golden>& gold) {
  10520. const auto slot = lynx::effectFromFl ("Fruity Vocoder", state, 120.0);
  10521. CHECK (slot.has_value() && slot->kind == lynx::EffectKind::vocoder);
  10522. if (! slot || slot->kind != lynx::EffectKind::vocoder)
  10523. return;
  10524. auto fx = lynx::makeEffect (*slot);
  10525. fx->prepare (44100.0);
  10526. std::vector<float> l (inL), r (inR);
  10527. for (int s = 0; s < N; s += 512)
  10528. fx->process (l.data() + s, r.data() + s, 512, slot->params, 120.0);
  10529. for (const auto& g : gold)
  10530. CHECK (l[(std::size_t) g.i] == g.v && r[(std::size_t) g.i] == g.v);
  10531. };
  10532. // 32 bands, order 3, inverted carrier bank with a formant shift, fast attack (5 ms, outside the knob range)
  10533. run (hex (
  10534. "02000000200000000300000002000000000000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000"
  10535. "803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f00"
  10536. "00803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803fb80b000050c30000640000000100000044fdffff"
  10537. "78000000050000002c010000000000008c0000003c000000a0000000"),
  10538. { { 777, 0.47870129346847534f }, { 1555, 0.2731437087059021f }, { 2222, -0.41599133610725403f }, { 3333, 0.083399198949337f }, { 3999, 0.7295553088188171f }, { 4100, 0.0871216207742691f } });
  10539. // 8 bands, swapped inputs, per-band levels, modulator level 0
  10540. run (hex (
  10541. "02000000080000000200000002000000010000003f9a99193f3333333fcdcc4c3f6666663f0000803fcdcc8c3f9a99993f1027000060ea"
  10542. "00001e000000000000002c010000500000000f0000003c00000000000000000000001400000080000000"),
  10543. { { 777, 0.07233863323926926f }, { 1555, 0.1183714047074318f }, { 2222, -0.6131120920181274f }, { 3333, 0.18050849437713623f }, { 3999, 0.056527279317379f }, { 4100, 0.05711518973112106f } });
  10544. }
  10545. // ---- Fruity WaveShaper / Fruity Soft Clipper (REA_rodada51) ------------------------------------------------------------
  10546. // States written here from our own values (no FL preset data); goldens from the installation's DLLs (EXEC, blocks of 512)
  10547. namespace
  10548. {
  10549. std::vector<std::uint8_t> waveShaperTestState (int pre, int mix, int post, bool uni, bool center, int os, bool hq, bool smooth)
  10550. {
  10551. std::vector<std::uint8_t> b;
  10552. 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))); };
  10553. 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))); };
  10554. 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))); };
  10555. i32 (0x40003);
  10556. i32 (pre), i32 (mix), i32 (post);
  10557. b.push_back (uni ? 1 : 0), b.push_back (center ? 1 : 0);
  10558. i32 (os);
  10559. 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 },
  10560. { 0.25, 0.9, -0.3f, 3 }, { 0.2, 1.0, 0.5f, 9 } };
  10561. i32 (3), i32 (3), i32 (5);
  10562. for (const auto& p : pts)
  10563. {
  10564. f64 (p.dt), f64 (p.y), f32 (p.t);
  10565. b.push_back ((std::uint8_t) p.mode), b.push_back (0), b.push_back (0), b.push_back (0);
  10566. }
  10567. i32 (0), i32 (-1), i32 (-1), i32 (-1), i32 (-1);
  10568. i32 (1);
  10569. b.push_back (hq ? 1 : 0), b.push_back (smooth ? 1 : 0);
  10570. return b;
  10571. }
  10572. } // namespace
  10573. TEST (fruity_waveshaper_and_soft_clipper)
  10574. {
  10575. std::vector<float> inL (4096), inR (4096);
  10576. for (int n = 0; n < 4096; ++n)
  10577. {
  10578. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  10579. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  10580. }
  10581. struct Gold { int i; float l, r; };
  10582. auto close = [] (float a, float b) { return std::abs (a - b) <= x87Tolerance() * std::max (1.0f, std::abs (b)); };
  10583. // Soft Clipper: bit-exact against Fruity Soft Clipper_x64.dll
  10584. auto clip = [&] (int thr, int post, std::initializer_list<Gold> gold) {
  10585. std::vector<float> l (inL), r (inR);
  10586. for (int s = 0; s < 4096; s += 512)
  10587. {
  10588. lynx::flshape::softClip (l.data() + s, 512, thr, post);
  10589. lynx::flshape::softClip (r.data() + s, 512, thr, post);
  10590. }
  10591. for (const auto& g : gold)
  10592. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  10593. };
  10594. clip (100, 128, { { 100, 0.0f, -0.949052989f }, { 1000, -0.99617976f, 0.872887492f }, { 2047, 0.649999976f, -0.944175363f },
  10595. { 3000, -0.99617976f, -0.699999988f }, { 4095, 0.25f, 0.600000024f } });
  10596. clip (1, 150, { { 100, 0.0f, -1.04588532f }, { 1000, -1.27003884f, 0.930835366f }, { 2047, 0.750244498f, -1.03539562f },
  10597. { 3000, -1.27003884f, -0.790091455f }, { 4095, 0.347655922f, 0.708338201f } });
  10598. clip (64, 140, { { 100, 0.0f, -1.08492827f }, { 1000, -1.21535313f, 0.990323186f }, { 2047, 0.804165959f, -1.0770781f },
  10599. { 3000, -1.21535313f, -0.849189103f }, { 4095, 0.319320858f, 0.754407823f } });
  10600. {
  10601. const std::vector<std::uint8_t> st { 64, 0, 0, 0, 140, 0, 0, 0 };
  10602. const auto slot = lynx::effectFromFl ("Fruity Soft Clipper", st, 120.0);
  10603. CHECK (slot && slot->kind == lynx::EffectKind::fruitySoftClipper);
  10604. if (slot)
  10605. {
  10606. const auto s = lynx::flshape::softClipperFromParams (slot->params.data());
  10607. CHECK (s.threshold == 64 && s.post == 140);
  10608. auto fx = lynx::makeEffect (*slot);
  10609. fx->prepare (44100.0);
  10610. std::vector<float> l (inL), r (inR);
  10611. fx->process (l.data(), r.data(), 4096, slot->params, 120.0);
  10612. CHECK (l[3000] == -1.21535313f && r[4095] == 0.754407823f);
  10613. }
  10614. }
  10615. // WaveShaper: tables (bipolar / unipolar) and the engine on three of our states, live and as FL renders
  10616. const auto stA = waveShaperTestState (150, 100, 120, false, true, 1, true, true); // CENTER, ×2, HQ, smooth
  10617. const auto stB = waveShaperTestState (100, 128, 128, true, false, 0, false, false); // unipolar
  10618. const auto stC = waveShaperTestState (192, 90, 100, false, false, 2, false, false); // ×4
  10619. const auto pA = lynx::flshape::parseWaveShaper (stA);
  10620. CHECK (pA && pA->points.size() == 5 && pA->pre == 150 && pA->center && pA->hq && pA->smooth && pA->oversampling == 1);
  10621. if (pA)
  10622. {
  10623. const auto T = lynx::flshape::shaperTable (pA->points, false);
  10624. CHECK (T.size() == 16386 && T[1] == 3277 && T[1001] == 18824 && T[5001] == 26195 && T[9001] == 11469 && T[12001] == 24614
  10625. && T[16384] == 32767 && T[0] == T[1]);
  10626. const auto U = lynx::flshape::shaperTable (pA->points, true);
  10627. CHECK (U.size() == 32770 && U[1] == -26214 && U[1001] == -6408 && U[5001] == 17073 && U[9001] == 19551 && U[12001] == 14549);
  10628. }
  10629. auto shape = [&] (const std::vector<std::uint8_t>& st, bool render, std::initializer_list<Gold> gold) {
  10630. const auto s = lynx::flshape::parseWaveShaper (st);
  10631. CHECK (s.has_value());
  10632. if (! s)
  10633. return;
  10634. lynx::flshape::WaveShaperEngine e;
  10635. e.setCurve (s->points);
  10636. e.prepare (44100.0);
  10637. e.setControls (s->pre, s->mix, s->post, s->oversampling, s->hq, s->smooth, s->center, s->unipolar, render);
  10638. std::vector<float> l (inL), r (inR);
  10639. for (int i = 0; i < 4096; i += 512)
  10640. e.process (l.data() + i, r.data() + i, 512);
  10641. for (const auto& g : gold)
  10642. CHECK (close (l[(std::size_t) g.i], g.l) && close (r[(std::size_t) g.i], g.r));
  10643. };
  10644. shape (stA, false, { { 100, 0.59108448f, 0.469315082f }, { 1000, 0.284353405f, 1.02621937f }, { 2047, -0.533613563f, 0.117038473f },
  10645. { 3000, 0.22673659f, -1.13572407f }, { 4095, -0.267153352f, 0.822866499f } });
  10646. shape (stA, true, { { 100, 0.794869065f, -0.127601087f }, { 1000, -0.00841622613f, 0.900729597f }, { 2047, 0.450603426f, -0.325943232f },
  10647. { 3000, -0.0530530401f, -0.32303831f }, { 4095, 1.0959363f, 0.946009099f } });
  10648. shape (stB, false, { { 100, -0.206451416f, 0.502471924f }, { 1000, -0.799987793f, 0.761169434f }, { 2047, 0.411743164f, 0.514160156f },
  10649. { 3000, -0.799987793f, 0.596221924f }, { 4095, -0.157653809f, 0.334106445f } });
  10650. shape (stB, true, { { 100, -0.206390381f, 0.502493739f }, { 1000, -0.799987793f, 0.761169434f }, { 2047, 0.411743164f, 0.514144361f },
  10651. { 3000, -0.799987793f, 0.596221924f }, { 4095, -0.157653809f, 0.334106445f } });
  10652. shape (stC, false, { { 100, 0.317797363f, 0.0483714938f }, { 1000, -0.445489585f, -0.533142388f }, { 2047, 0.634525597f, 0.942175806f },
  10653. { 3000, -0.445489585f, 0.514707327f }, { 4095, 0.200391173f, -0.937523127f } });
  10654. shape (stC, true, { { 100, 0.61165309f, -0.0993360579f }, { 1000, 0.0375693105f, 0.667849183f }, { 2047, 0.353716731f, -0.223758236f },
  10655. { 3000, 0.0375693105f, -0.223787636f }, { 4095, 0.843308687f, 0.688474894f } });
  10656. // the mixer effect from the FL import: controls as parameters, the curve in the slot's state; host blocks of any size
  10657. const auto slot = lynx::effectFromFl ("Fruity WaveShaper", stC, 120.0);
  10658. CHECK (slot && slot->kind == lynx::EffectKind::fruityWaveShaper && slot->state == stC);
  10659. if (slot)
  10660. {
  10661. auto fx = lynx::makeEffect (*slot);
  10662. fx->prepare (44100.0);
  10663. std::vector<float> l (inL), r (inR);
  10664. fx->process (l.data(), r.data(), 1024, slot->params, 120.0);
  10665. fx->process (l.data() + 1024, r.data() + 1024, 1024, slot->params, 120.0);
  10666. fx->process (l.data() + 2048, r.data() + 2048, 2048, slot->params, 120.0);
  10667. CHECK (close (l[1000], -0.445489585f) && close (r[2047], 0.942175806f) && close (l[4095], 0.200391173f));
  10668. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityWaveShaper, 3, slot->params[3], 120.0) == "x4");
  10669. }
  10670. // an unreadable state keeps the old approximation
  10671. CHECK (lynx::effectFromFl ("Fruity WaveShaper", { 1, 2, 3 }, 120.0)->kind == lynx::EffectKind::distortion);
  10672. CHECK (lynx::effectFromFl ("Fruity Soft Clipper", { 1, 2, 3 }, 120.0)->kind == lynx::EffectKind::distortion);
  10673. }
  10674. // ---- Fruity Parametric EQ 2 / Fruity Parametric EQ (REA_rodada50) -----------------------------------------------------
  10675. // States written here from our own values (no FL preset data); goldens from the installation's DLLs (EXEC, blocks of 512)
  10676. namespace
  10677. {
  10678. struct PeqTestBand { int type, freq, width, gain, order; };
  10679. std::vector<std::uint8_t> peq2TestState (const PeqTestBand (&bands)[7], int main, int uiFlags, int options, int mask)
  10680. {
  10681. std::vector<std::uint8_t> b;
  10682. 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))); };
  10683. i32 (8);
  10684. for (int copy = 0; copy < 2; ++copy)
  10685. {
  10686. for (const auto& x : bands) i32 (x.gain);
  10687. for (const auto& x : bands) i32 (x.freq);
  10688. for (const auto& x : bands) i32 (x.width);
  10689. for (const auto& x : bands) i32 (x.type);
  10690. for (const auto& x : bands) i32 (x.order);
  10691. i32 (main);
  10692. }
  10693. i32 (uiFlags), i32 (0), b.push_back (0), i32 (2), i32 (0), i32 (2), i32 (0);
  10694. b.push_back ((std::uint8_t) options);
  10695. i32 (mask), i32 (1), i32 (0);
  10696. for (int v : { 3, 1, 2, 2, 2 }) i32 (v);
  10697. i32 (0);
  10698. return b;
  10699. }
  10700. std::vector<std::uint8_t> peq1TestState (const PeqTestBand (&bands)[7], int main)
  10701. {
  10702. std::vector<std::uint8_t> b;
  10703. 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))); };
  10704. for (const auto& x : bands) i32 (x.gain);
  10705. for (const auto& x : bands) i32 (x.freq);
  10706. for (const auto& x : bands) i32 (x.width);
  10707. for (const auto& x : bands) i32 (x.type);
  10708. i32 (main);
  10709. return b;
  10710. }
  10711. } // namespace
  10712. // EQUO test state (our own LCG curves, no FL data; REA_rodada61 equo/teststate.py builds the same bytes)
  10713. static std::vector<std::uint8_t> equoTestState (int ovs, int bw, bool add)
  10714. {
  10715. std::vector<std::uint8_t> b;
  10716. 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); };
  10717. auto putI = [&put] (std::int32_t v) { put (&v, 4); };
  10718. auto putF = [&put] (float v) { put (&v, 4); };
  10719. std::uint32_t seed = 12345;
  10720. auto rnd = [&seed] {
  10721. seed = (std::uint32_t) (((std::uint64_t) seed * 1103515245u + 12345u) & 0x7fffffffu);
  10722. return (float) ((double) (seed % 20001) / 10000.0);
  10723. };
  10724. putI (4);
  10725. for (int k = 0; k < 93; ++k)
  10726. {
  10727. putF (rnd());
  10728. putI (k % 11 == 0 ? 1 : (k % 13 == 0 ? 2 : 0));
  10729. }
  10730. for (int bank = 0; bank < 9; ++bank)
  10731. for (int k = 0; k < 93; ++k)
  10732. {
  10733. const float v = rnd();
  10734. putF (bank == 8 ? 0.75f + v * 0.25f : v);
  10735. }
  10736. for (int v : { 1500, 90, 2600, 170, 150, ovs, bw, -1, 0, 0 })
  10737. putI (v);
  10738. for (int v : { 0, add ? 1 : 0, 0 })
  10739. b.push_back ((std::uint8_t) v);
  10740. putI (1);
  10741. return b;
  10742. }
  10743. // Pitch Shifter state (version 7, our own settings; REA_rodada61 ps/teststate.py builds the same bytes)
  10744. static std::vector<std::uint8_t> pitchShifterTestState (std::array<int, 13> p, int engine, int detector)
  10745. {
  10746. std::vector<std::uint8_t> b;
  10747. auto putI = [&b] (std::int32_t v) { const auto* q = (const std::uint8_t*) &v; b.insert (b.end(), q, q + 4); };
  10748. putI (7);
  10749. for (int v : p)
  10750. putI (v);
  10751. putI (p[11]);
  10752. b.push_back (0);
  10753. putI (engine);
  10754. putI (detector);
  10755. putI (1000);
  10756. b.push_back (1);
  10757. b.push_back (0);
  10758. return b;
  10759. }
  10760. TEST (pitch_shifter_exact)
  10761. {
  10762. std::vector<float> inL (8192), inR (8192);
  10763. for (int n = 0; n < 8192; ++n)
  10764. {
  10765. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0) * 0.3f;
  10766. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0) * 0.3f;
  10767. }
  10768. struct Gold { int i; float l, r; };
  10769. // goldens: Pitch Shifter_x64.dll on the same states and signal (REA_rodada61)
  10770. auto run = [&] (const std::vector<std::uint8_t>& st, int latency, std::initializer_list<Gold> gold) {
  10771. lynx::psfl::State s;
  10772. CHECK (st.size() == 75 && lynx::psfl::parsePitchShifter (st.data(), st.size(), s));
  10773. // through the mixer effect: FL import → slot parameters → effect, odd host blocks
  10774. const auto slot = lynx::effectFromFl ("Pitch Shifter", st, 120.0);
  10775. CHECK (slot && slot->kind == lynx::EffectKind::flPitchShifter);
  10776. if (! slot)
  10777. return;
  10778. auto fx = lynx::makeEffect (*slot);
  10779. fx->prepare (44100.0);
  10780. std::vector<float> l (inL), r (inR);
  10781. for (int i = 0, k = 0; i < 8192; ++k)
  10782. {
  10783. const int n = std::min (8192 - i, k % 2 == 0 ? 700 : 333);
  10784. fx->process (l.data() + i, r.data() + i, n, slot->params, 120.0);
  10785. i += n;
  10786. }
  10787. CHECK (fx->latencySamples() == latency);
  10788. for (const auto& g : gold)
  10789. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  10790. };
  10791. // "FFT" (pitch-synchronous) engine: +5 st, formant −3 st, correction 50 %, mix 79 %, accurate detector
  10792. run (pitchShifterTestState ({ 500, -300, 2, 1900, 8500, 200, 0, 0, 768, 0, 0, 32768, 0 }, 0, 1), 2457,
  10793. { { 1000, 0.0f, 0.0f }, { 3000, 0.142123833f, 0.0878119618f }, { 5000, 0.139498621f, -0.365035176f },
  10794. { 7000, 0.139498621f, 0.161137447f }, { 8191, 0.449655473f, 0.453882188f } });
  10795. // granular engine: −7 st, 60 ms grains, density 300 %, jitter 30 %, random 20 %, post delay with feedback 40 %
  10796. run (pitchShifterTestState ({ -700, 0, 2, 2400, 6000, 300, 30, 20, 200, 40, 1, 65536, 0 }, 1, 0), 0,
  10797. { { 1000, -0.0736689344f, -0.349420458f }, { 3000, 0.086801365f, 0.0471927896f }, { 5000, -0.0939825475f, 0.31712544f },
  10798. { 7000, -0.28462702f, -0.437854528f }, { 8191, 0.138916701f, -0.0426892526f } });
  10799. CHECK (lynx::formatEffectParam (lynx::EffectKind::flPitchShifter, 0, 0.75f, 120.0) == "+6.00 st");
  10800. }
  10801. TEST (equo_exact)
  10802. {
  10803. std::vector<float> inL (4096), inR (4096);
  10804. for (int n = 0; n < 4096; ++n)
  10805. {
  10806. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  10807. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  10808. }
  10809. struct Gold { int i; float l, r; };
  10810. // goldens: EQUO_x64.dll run on the same states and signal (REA_rodada61 §5)
  10811. auto run = [&] (const std::vector<std::uint8_t>& st, bool hq, bool ticks, std::initializer_list<Gold> gold) {
  10812. const auto s = lynx::equofx::parse (st);
  10813. CHECK (s.has_value());
  10814. if (! s)
  10815. return;
  10816. lynx::equofx::Engine e;
  10817. e.prepare (44100.0, hq);
  10818. e.set (*s);
  10819. std::vector<float> l (inL), r (inR);
  10820. if (! ticks)
  10821. e.process (l.data(), r.data(), 4096);
  10822. else
  10823. {
  10824. auto k = *s;
  10825. e.startFromConstruction();
  10826. for (int i = 0; i < 4096; i += 256)
  10827. {
  10828. if (i == 2048)
  10829. k.knob[2] = 5300, e.setKnobs (k);
  10830. e.tick();
  10831. e.process (l.data() + i, r.data() + i, 256);
  10832. }
  10833. }
  10834. for (const auto& g : gold)
  10835. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  10836. };
  10837. const auto plain = equoTestState (0, 150, false);
  10838. run (plain, false, false, { { 100, -0.00154058414f, -0.00550224911f }, { 1000, -0.908149242f, 0.619277894f }, { 2047, 0.411961317f, -0.364051819f },
  10839. { 3000, -1.42823684f, -0.0602950938f }, { 4095, -0.0661701784f, 0.300809383f } });
  10840. run (equoTestState (1, 300, true), false, false,
  10841. { { 100, 0.0312003642f, 0.0184187777f }, { 1000, 0.0379521511f, 0.224126667f }, { 2047, -0.135689721f, 0.0444660783f },
  10842. { 3000, 0.113949843f, -1.00256324f }, { 4095, -0.176232055f, 0.79159987f } });
  10843. run (plain, true, false, { { 100, -0.00104017637f, 0.000872685807f }, { 1000, -0.0426855832f, 0.0402083024f }, { 2047, 0.191993102f, 0.174184501f },
  10844. { 3000, -0.114048228f, 0.419817507f }, { 4095, 0.0376945846f, -0.274040252f } });
  10845. run (plain, false, true, { { 100, -0.0019897793f, -0.00981196482f }, { 1000, -1.09574401f, 0.83203429f }, { 2047, 0.839113712f, -0.691361725f } });
  10846. // the mixer effect from the FL import: FL's state in the slot, the knobs in its parameters, any host block size
  10847. const auto slot = lynx::effectFromFl ("EQUO", plain, 140.0);
  10848. CHECK (slot && slot->kind == lynx::EffectKind::equo && slot->state == plain);
  10849. if (slot)
  10850. {
  10851. auto back = *lynx::equofx::parse (plain);
  10852. lynx::equofx::applyParams (slot->params, back);
  10853. CHECK (back.knob == lynx::equofx::parse (plain)->knob && back.bandwidth == 150 && back.oversample == 0);
  10854. auto fx = lynx::makeEffect (*slot);
  10855. fx->prepare (44100.0);
  10856. std::vector<float> l (inL), r (inR);
  10857. fx->process (l.data(), r.data(), 700, slot->params, 0.0);
  10858. fx->process (l.data() + 700, r.data() + 700, 3396, slot->params, 0.0);
  10859. // the engine glides its bands per processing call, as FL does per host buffer: compare with the engine run on
  10860. // the same 700 + 3396 split (sample 1000 differs from the single-call golden), and with the golden once settled
  10861. lynx::equofx::Engine ref;
  10862. ref.prepare (44100.0, false);
  10863. ref.set (*lynx::equofx::parse (plain));
  10864. std::vector<float> rl (inL), rr (inR);
  10865. ref.process (rl.data(), rr.data(), 700);
  10866. ref.process (rl.data() + 700, rr.data() + 700, 3396);
  10867. CHECK (l == rl && rr == r && r[3000] == -0.0602950938f);
  10868. CHECK (lynx::formatEffectParam (lynx::EffectKind::equo, 4, slot->params[4], 120.0, slot->params).find ("dB") != std::string::npos);
  10869. }
  10870. CHECK (! lynx::equofx::parse (std::vector<std::uint8_t> (100, 0)).has_value());
  10871. }
  10872. // Multiband Delay test state (version 7, our own LCG cells, no FL data; REA_rodada71 mbd/py/teststate.py builds the same bytes)
  10873. static std::vector<std::uint8_t> multibandDelayTestState (std::array<int, 12> globals, std::uint32_t seed)
  10874. {
  10875. std::vector<std::uint8_t> b;
  10876. auto putI = [&b] (std::int32_t v) { const auto* q = (const std::uint8_t*) &v; b.insert (b.end(), q, q + 4); };
  10877. putI (7);
  10878. putI (396);
  10879. std::uint64_t s = seed;
  10880. for (int i = 0; i < 384; ++i)
  10881. {
  10882. s = (s * 1103515245u + 12345u) & 0x7fffffffu;
  10883. putI ((std::int32_t) ((s % 100001u) * (1u << 30) / 100000u));
  10884. }
  10885. for (int v : globals)
  10886. putI (v);
  10887. putI (0);
  10888. putI (3);
  10889. for (int v : { 1, 1, 0 })
  10890. b.push_back ((std::uint8_t) v);
  10891. b.insert (b.end(), 192, 0);
  10892. return b;
  10893. }
  10894. TEST (multiband_delay_exact)
  10895. {
  10896. std::vector<float> inL (8192), inR (8192);
  10897. for (int n = 0; n < 8192; ++n)
  10898. {
  10899. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0 * 0.3);
  10900. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0 * 0.3);
  10901. }
  10902. struct Gold { int i; float l, r; };
  10903. // goldens: Multiband Delay_x64.dll (+ its engine_x64.dll) on the same states and signal, 512-frame blocks (REA_rodada71)
  10904. auto run = [&] (const std::vector<std::uint8_t>& st, int latency, float tol, std::initializer_list<Gold> gold) {
  10905. CHECK (st.size() == 1795 && lynx::mbdelay::parse (st).has_value());
  10906. const auto slot = lynx::effectFromFl ("Multiband Delay", st, 120.0);
  10907. CHECK (slot && slot->kind == lynx::EffectKind::multibandDelay && slot->state == st);
  10908. if (! slot)
  10909. return;
  10910. auto fx = lynx::makeEffect (*slot);
  10911. fx->prepare (44100.0);
  10912. std::vector<float> l (inL), r (inR);
  10913. for (int i = 0, k = 0; i < 8192; ++k)
  10914. {
  10915. const int n = std::min (8192 - i, k % 2 == 0 ? 700 : 333);
  10916. fx->process (l.data() + i, r.data() + i, n, slot->params, 120.0);
  10917. i += n;
  10918. }
  10919. CHECK (fx->latencySamples() == latency);
  10920. for (const auto& g : gold)
  10921. CHECK (std::fabs (l[(std::size_t) g.i] - g.l) <= tol && std::fabs (r[(std::size_t) g.i] - g.r) <= tol);
  10922. };
  10923. // steep crossover, keep pitch, weighted feedback, compressor
  10924. run (multibandDelayTestState ({ 2000, 0, 3000, 600, 1, 1, 3500, 64, 0, 1, 30000, 0 }, 777), 0, 0.0f,
  10925. { { 1000, -0.125012308f, 0.0675066486f }, { 3000, -0.125002861f, -0.0525011979f }, { 5000, -0.110439189f, 0.0597563162f },
  10926. { 7000, -0.146480903f, 0.0139919929f }, { 8191, -0.0454496294f, 0.0398242511f } });
  10927. // gentle crossover, harmonic scale, delay ×0.1, saturation
  10928. run (multibandDelayTestState ({ -2500, 1, 4500, 0, 0, 0, 0, 128, 1, 0, 1000, 1 }, 4242), 0, 0.0f,
  10929. { { 1000, 0.0679653957f, 0.0137896547f }, { 3000, 0.0620588325f, 0.0558352768f }, { 5000, 0.0558495969f, -0.0220566839f },
  10930. { 7000, 0.057009533f, 0.00332171144f }, { 8191, 0.0673644915f, 0.00163267844f } });
  10931. // linear-phase crossover (FL's IPP FFTs are approximated: about −120 dB), clipper
  10932. run (multibandDelayTestState ({ 1500, 0, 2000, 1500, 2, 0, 1000, 100, 0, 0, 5000, 2 }, 99), 1536, 2e-6f,
  10933. { { 1000, 0.0f, 0.0f }, { 3000, 0.103910387f, -0.0521105044f }, { 5000, 0.0516818762f, -0.0134486742f },
  10934. { 7000, 0.065563485f, -0.00765838847f }, { 8191, -0.0376662463f, -0.0434602089f } });
  10935. const auto st = multibandDelayTestState ({ 2000, 0, 3000, 600, 1, 1, 3500, 64, 0, 1, 30000, 0 }, 777);
  10936. const auto s = *lynx::mbdelay::parse (st);
  10937. CHECK (lynx::mbdelay::parse (lynx::mbdelay::serialize (s))->raw == s.raw);
  10938. lynx::EffectParams p {};
  10939. lynx::mbdelay::toParams (s, p);
  10940. auto back = s;
  10941. lynx::mbdelay::applyParams (p, back);
  10942. CHECK (back.raw == s.raw);
  10943. CHECK (lynx::mbdelay::engineValue (s, 384 + 7) == (float) (192.0 / 256.0));
  10944. CHECK (lynx::formatEffectParam (lynx::EffectKind::multibandDelay, 4, p[4], 120.0) == "steep");
  10945. CHECK (lynx::formatEffectParam (lynx::EffectKind::multibandDelay, 7, 1.0f, 120.0) == "100% wet");
  10946. CHECK (! lynx::mbdelay::parse (std::vector<std::uint8_t> (16, 0)).has_value());
  10947. }
  10948. TEST (fruity_parametric_eq_exact)
  10949. {
  10950. std::vector<float> inL (4096), inR (4096);
  10951. for (int n = 0; n < 4096; ++n)
  10952. {
  10953. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  10954. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  10955. }
  10956. struct Gold { int i; float l, r; };
  10957. // high pass Steep 8, low shelf, steep peaking, a 0 dB peaking (0.2 s countdown), notch Gentle 4, high shelf Gentle 8,
  10958. // low pass Gentle 6
  10959. const PeqTestBand bands[7] = { { 3, 8000, 30000, 0, -3 }, { 5, 15000, 40124, 600, 0 }, { 6, 30000, 20000, -900, -2 },
  10960. { 6, 35000, 25412, 0, 0 }, { 4, 40000, 10000, 0, 1 }, { 7, 50000, 30000, 450, 3 },
  10961. { 1, 60000, 45000, 0, 2 } };
  10962. const PeqTestBand bands1[7] = { { 3, 6000, 20000, 0, 0 }, { 5, 15000, 30000, 600, 0 }, { 6, 30000, 20000, -900, 0 },
  10963. { 6, 35000, 25000, 0, 0 }, { 4, 40000, 10000, 0, 0 }, { 7, 50000, 30000, 450, 0 },
  10964. { 1, 60000, 45000, 0, 0 } };
  10965. auto run = [&] (const std::optional<lynx::peqfl::Settings>& s, std::initializer_list<Gold> gold) {
  10966. CHECK (s.has_value());
  10967. if (! s)
  10968. return;
  10969. lynx::peqfl::Engine e;
  10970. e.set (*s);
  10971. e.prepare (44100.0);
  10972. std::vector<float> l (inL), r (inR);
  10973. for (int i = 0; i < 4096; i += 512)
  10974. e.process (l.data() + i, r.data() + i, 512);
  10975. for (const auto& g : gold)
  10976. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  10977. };
  10978. const auto plain = peq2TestState (bands, 300, 0, 0x60, 0x7F);
  10979. const auto hq = peq2TestState (bands, 300, 1, 0x60, 0x7F);
  10980. const auto ext = peq2TestState (bands, 300, 1, 0x62, 0x7F);
  10981. run (lynx::peqfl::parsePeq2 (plain), { { 100, 0.1340574324131012f, -0.3040016293525696f }, { 1000, 0.28402701020240784f, -0.23813000321388245f },
  10982. { 2047, -0.9301530122756958f, 0.13881002366542816f }, { 3000, 0.2846762537956238f, 0.48893973231315613f },
  10983. { 4095, -0.8481860756874084f, -0.2641225755214691f } });
  10984. run (lynx::peqfl::parsePeq2 (hq), { { 100, 0.19229719042778015f, 0.29938483238220215f }, { 1000, 0.12543821334838867f, -0.055617764592170715f },
  10985. { 2047, 1.0342068672180176f, 0.10901166498661041f }, { 3000, 0.12598788738250732f, -0.12920552492141724f },
  10986. { 4095, 0.1619236022233963f, -0.23385579884052277f } });
  10987. run (lynx::peqfl::parsePeq2 (ext), { { 100, 1.2178642749786377f, -0.21160493791103363f }, { 1000, -0.8292768001556396f, -0.23916664719581604f },
  10988. { 2047, -0.7030635476112366f, -0.1978478729724884f }, { 3000, -0.8299900889396667f, 0.9836107492446899f },
  10989. { 4095, 0.06754066050052643f, -0.34364908933639526f } });
  10990. run (lynx::peqfl::parsePeq2 (peq2TestState (bands, -250, 1, 0x60, 0x5B)), // bands 3 and 6 muted
  10991. { { 100, -0.02959616854786873f, 0.227821946144104f }, { 1000, 0.053224340081214905f, 0.06922465562820435f },
  10992. { 2047, 0.3365536034107208f, -0.05899651721119881f }, { 3000, 0.053670305758714676f, -0.07452142238616943f },
  10993. { 4095, -0.04406970739364624f, -0.12635089457035065f } });
  10994. run (lynx::peqfl::parsePeq1 (peq1TestState (bands1, 150)),
  10995. { { 100, -0.6533108353614807f, -1.6903921365737915f }, { 1000, -4.313726902008057f, 5.973916053771973f },
  10996. { 2047, 1.0222549438476562f, 4.427900314331055f }, { 3000, -4.310323238372803f, 7.825916767120361f },
  10997. { 4095, 8.344571113586426f, -0.49086111783981323f } });
  10998. // "alternative design" (Frequency Splitter's matched biquads), bit-exact
  10999. run (lynx::peqfl::parsePeq2 (peq2TestState (bands, 300, 3, 0x60, 0x7F)),
  11000. { { 100, -0.790181577205658f, -0.08691816031932831f }, { 1000, 0.4960691034793854f, -0.24172282218933105f },
  11001. { 2047, -0.42601829767227173f, 0.027932533994317055f }, { 3000, 0.49674150347709656f, 1.0799059867858887f },
  11002. { 4095, -0.35040369629859924f, -0.28149813413619995f } });
  11003. // linear phase (plain and HQ response): FFT convolution, 1536-sample latency, 0.1 s fade-in; FL's IPP FFT rounding
  11004. // is not replicated, so within 1e-6 of the DLL
  11005. for (const bool hq : { false, true })
  11006. {
  11007. const auto s = lynx::peqfl::parsePeq2 (peq2TestState (bands, hq ? -200 : 300, hq ? 1 : 0, 0x61, 0x7F));
  11008. CHECK (s && s->linearPhase);
  11009. if (! s)
  11010. continue;
  11011. std::vector<float> l (12288), r (12288);
  11012. for (int n = 0; n < 12288; ++n)
  11013. {
  11014. l[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  11015. r[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  11016. }
  11017. lynx::peqfl::Engine e;
  11018. e.set (*s);
  11019. e.prepare (44100.0);
  11020. CHECK (e.latencySamples() == 1536);
  11021. for (int i = 0; i < 12288; i += 512)
  11022. e.process (l.data() + i, r.data() + i, 512);
  11023. // (named arrays: a std::initializer_list chosen by ?: would point into a temporary array that dies at the end of
  11024. // the declaration, which GCC 13 reuses for other locals)
  11025. static const Gold goldHq[] = {
  11026. { 2000, 0.08618364483118057f, 0.11248858273029327f }, { 5000, 0.18998730182647705f, 0.428600937128067f },
  11027. { 8191, -0.1412065178155899f, -0.002996620489284396f }, { 10000, 0.18998737633228302f, -0.012570115737617016f },
  11028. { 12287, 0.40586185455322266f, -0.014698396436870098f } };
  11029. static const Gold goldPlain[] = {
  11030. { 2000, 0.18538671731948853f, 0.206522598862648f }, { 5000, 0.4086928963661194f, 0.8167467713356018f },
  11031. { 8191, -0.3218550980091095f, -0.06812914460897446f }, { 10000, 0.40869277715682983f, -0.08537150919437408f },
  11032. { 12287, 0.775394082069397f, -0.08931504935026169f } };
  11033. for (const auto& g : hq ? goldHq : goldPlain)
  11034. 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);
  11035. }
  11036. // the mixer effect from the FL import: FL's values in the slot's parameters (exact round trip), any host block size
  11037. for (const auto* name : { "Fruity Parametric EQ 2", "Fruity Parametric EQ" })
  11038. {
  11039. const bool v1 = std::string (name) == "Fruity Parametric EQ";
  11040. const auto st = v1 ? peq1TestState (bands1, 150) : ext;
  11041. const auto slot = lynx::effectFromFl (name, st, 120.0);
  11042. CHECK (slot && slot->kind == lynx::EffectKind::parametricEq && lynx::peq::flLaw (slot->params));
  11043. if (! slot)
  11044. continue;
  11045. const auto back = lynx::peqfl::fromParams (slot->params);
  11046. const auto orig = v1 ? lynx::peqfl::parsePeq1 (st) : lynx::peqfl::parsePeq2 (st);
  11047. CHECK (back.freq == orig->freq && back.gain == orig->gain && back.width == orig->width && back.type == orig->type
  11048. && back.order == orig->order && back.main == orig->main && back.processing == orig->processing && back.mask == orig->mask);
  11049. auto fx = lynx::makeEffect (*slot);
  11050. fx->prepare (44100.0);
  11051. CHECK (fx->latencySamples() == (v1 ? 0 : 44));
  11052. std::vector<float> l (inL), r (inR);
  11053. fx->process (l.data(), r.data(), 1000, slot->params, 120.0);
  11054. fx->process (l.data() + 1000, r.data() + 1000, 3096, slot->params, 120.0);
  11055. if (v1)
  11056. CHECK (l[3000] == -4.310323238372803f && r[4095] == -0.49086111783981323f);
  11057. else
  11058. CHECK (l[1000] == -0.8292768001556396f && r[3000] == 0.9836107492446899f && l[4095] == 0.06754066050052643f);
  11059. // editor texts and graph
  11060. CHECK (lynx::formatEffectParam (lynx::EffectKind::parametricEq, lynx::peq::slopeParam, slot->params[lynx::peq::slopeParam], 120.0, slot->params)
  11061. == (v1 ? "12 dB/oct" : "Steep 8"));
  11062. CHECK (lynx::peq::widthText (slot->params, 0).rfind ("BW ", 0) == 0);
  11063. const auto lowCutDb = lynx::peq::responseDb (slot->params, 44100.0, 25.0), midDb = lynx::peq::responseDb (slot->params, 44100.0, 1000.0);
  11064. CHECK (lowCutDb < midDb - 5.0); // high pass below ~100 Hz (Steep 8 in the EQ 2 state, 12 dB/oct in v1)
  11065. }
  11066. // a native LYNX EQ is untouched by the FL law
  11067. CHECK (! lynx::peq::flLaw (lynx::makeEffectSlot (lynx::EffectKind::parametricEq).params));
  11068. }
  11069. TEST (fruity_panomatic_balance_blood_overdrive)
  11070. {
  11071. // REA_rodada58: goldens from the installation's DLLs (EXEC) on our own states, 44.1 kHz, blocks of 512
  11072. const lynx::ScopedFlushDenormals ftz;
  11073. std::vector<float> inL (4096), inR (4096);
  11074. for (int n = 0; n < 4096; ++n)
  11075. {
  11076. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  11077. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  11078. }
  11079. struct Gold { int i; float l, r; };
  11080. auto close = [] (float a, float b) { return std::abs (a - b) <= x87Tolerance() * std::max (1.0f, std::abs (b)); };
  11081. auto check = [&] (const std::vector<float>& l, const std::vector<float>& r, std::initializer_list<Gold> gold) {
  11082. for (const auto& g : gold)
  11083. CHECK (close (l[(std::size_t) g.i], g.l) && close (r[(std::size_t) g.i], g.r));
  11084. };
  11085. auto st32 = [] (std::initializer_list<int> v) {
  11086. std::vector<std::uint8_t> b;
  11087. for (int x : v)
  11088. for (int k = 0; k < 4; ++k)
  11089. b.push_back ((std::uint8_t) ((unsigned) x >> (8 * k)));
  11090. return b;
  11091. };
  11092. // PanOMatic: the engine started as LYNX starts it (one silent frame, then FL's flush), and from silence (fade-in)
  11093. auto pom = [&] (lynx::flbasic::PanOMaticState s, bool prime, std::initializer_list<Gold> gold) {
  11094. lynx::flbasic::PanOMaticEngine e;
  11095. e.prepare (44100);
  11096. e.set (s);
  11097. if (prime)
  11098. e.prime();
  11099. std::vector<float> l (inL), r (inR);
  11100. for (int i = 0; i < 4096; i += 512)
  11101. e.process (l.data() + i, r.data() + i, 512);
  11102. check (l, r, gold);
  11103. };
  11104. pom ({ 40, 300, 2, 1, 128, 50000 }, true, { { 100, 0.0f, -1.44884729f }, { 1000, -1.78870034f, 0.778897762f }, { 2047, 0.697593153f, -1.26396084f },
  11105. { 3000, -1.78870034f, -1.32851648f }, { 4095, 0.268305063f, 0.851258814f } });
  11106. pom ({ 200, 180, 1, 2, 100, 30000 }, true, { { 100, 0.0f, -0.347919852f }, { 1000, -0.322590947f, 0.0788071603f }, { 2047, 0.126614109f, -0.26421836f },
  11107. { 3000, -0.326532423f, -0.435213327f }, { 4095, 0.0493027233f, 0.226084977f } });
  11108. pom ({ 90, 256, 0, 1, 77, 40000 }, false, { { 100, -0.0292895138f, -0.110000029f }, { 1000, -1.31911683f, 0.666457176f }, { 2047, 0.477211952f, -0.864247262f },
  11109. { 3000, -1.59167564f, -0.591367304f }, { 4095, 0.268362761f, 0.537029684f } });
  11110. // Balance: flushed and from silence
  11111. auto bal = [&] (lynx::flbasic::BalanceState s, bool flush, std::initializer_list<Gold> gold) {
  11112. lynx::flbasic::BalanceEngine e;
  11113. e.prepare (44100);
  11114. e.set (s);
  11115. if (flush)
  11116. e.flush();
  11117. std::vector<float> l (inL), r (inR);
  11118. for (int i = 0; i < 4096; i += 512)
  11119. e.process (l.data() + i, r.data() + i, 512);
  11120. check (l, r, gold);
  11121. };
  11122. bal ({ 77, 100 }, true, { { 100, 0.0f, -0.136579692f }, { 1000, -0.0669578835f, -0.0282340273f }, { 2047, 0.0261135735f, -0.0795038193f },
  11123. { 3000, -0.0669578835f, -0.226895392f }, { 4095, 0.0100436825f, 0.0954951793f } });
  11124. bal ({ -128, 320 }, false, { { 100, -0.28914085f, 0.0f }, { 1000, -1.03179514f, 0.0f }, { 2047, -0.578702569f, 0.0f },
  11125. { 3000, -3.18510628f, 0.0f }, { 4095, 1.14394665f, 0.0f } });
  11126. // Blood Overdrive: rate first, then the state (its smoothers settle for one second of samples)
  11127. auto od = [&] (std::array<int, 6> raw, std::initializer_list<Gold> gold) {
  11128. lynx::flbasic::BloodOverdriveEngine e;
  11129. e.start (44100, { raw });
  11130. std::vector<float> l (inL), r (inR);
  11131. for (int i = 0; i < 4096; i += 512)
  11132. e.process (l.data() + i, r.data() + i, 512);
  11133. check (l, r, gold);
  11134. };
  11135. od ({ 3000, 7000, 5000, 1, 9000, 5000 }, { { 100, -0.0442250744f, 0.170355186f }, { 1000, 0.0470311977f, 0.0765217245f },
  11136. { 2047, 0.0261161141f, -0.0341798998f }, { 3000, 0.0470311977f, 0.0213619489f },
  11137. { 4095, 0.0402069278f, 0.0499244779f } });
  11138. // the mixer effects from the FL import: controls as parameters; host blocks of any size
  11139. auto viaImport = [&] (const char* plugin, const std::vector<std::uint8_t>& st, lynx::EffectKind kind, std::initializer_list<Gold> gold) {
  11140. const auto slot = lynx::effectFromFl (plugin, st, 120.0);
  11141. CHECK (slot && slot->kind == kind);
  11142. if (! slot)
  11143. return;
  11144. auto fx = lynx::makeEffect (*slot);
  11145. fx->prepare (44100.0);
  11146. std::vector<float> l (inL), r (inR);
  11147. fx->process (l.data(), r.data(), 1024, slot->params, 120.0);
  11148. fx->process (l.data() + 1024, r.data() + 1024, 1024, slot->params, 120.0);
  11149. fx->process (l.data() + 2048, r.data() + 2048, 2048, slot->params, 120.0);
  11150. check (l, r, gold);
  11151. };
  11152. viaImport ("Fruity PanOMatic", st32 ({ 40, 300, 2, 1, 128, 50000 }), lynx::EffectKind::fruityPanOMatic,
  11153. { { 1000, -1.78870034f, 0.778897762f }, { 4095, 0.268305063f, 0.851258814f } });
  11154. viaImport ("Fruity Balance", st32 ({ 0, 256 }), lynx::EffectKind::fruityBalance,
  11155. { { 100, 0.0f, -1.10000002f }, { 1000, -1.66666663f, 0.899999976f }, { 4095, 0.25f, 0.600000024f } });
  11156. viaImport ("Fruity Blood Overdrive", st32 ({ 3, 0, 5000, 2578, 1, 0, 2911, 0, 0 }), lynx::EffectKind::fruityBloodOverdrive,
  11157. { { 100, -0.122698173f, 0.0206716377f }, { 2047, 0.0128381588f, -0.0274834726f }, { 4095, -0.0899387598f, -0.121109024f } });
  11158. {
  11159. const auto slot = lynx::effectFromFl ("Fruity PanOMatic", st32 ({ 119, 256, 1, 2, 64, 27500 }), 120.0);
  11160. CHECK (slot && lynx::flbasic::panOMaticFromParams (slot->params.data()).pan == 119
  11161. && lynx::flbasic::panOMaticFromParams (slot->params.data()).target == 2);
  11162. if (slot)
  11163. {
  11164. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityPanOMatic, 2, slot->params[2], 120.0) == "Triangle");
  11165. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityPanOMatic, 1, slot->params[1], 120.0) == "+0.0 dB");
  11166. }
  11167. }
  11168. // unreadable states keep the old approximations
  11169. CHECK (lynx::effectFromFl ("Fruity PanOMatic", { 1, 2, 3 }, 120.0)->kind == lynx::EffectKind::utility);
  11170. CHECK (lynx::effectFromFl ("Fruity Balance", { 1, 2, 3 }, 120.0)->kind == lynx::EffectKind::utility);
  11171. CHECK (lynx::effectFromFl ("Fruity Blood Overdrive", { 1, 2, 3 }, 120.0)->kind == lynx::EffectKind::distortion);
  11172. }
  11173. TEST (fruity_fast_lp_free_filter_filter)
  11174. {
  11175. // REA_rodada63: goldens from the installation's DLLs (EXEC) on our own states, 44.1 kHz, blocks of 512 (the wrapper
  11176. // splits the host blocks below into calls of 512, as the DLL ran)
  11177. const lynx::ScopedFlushDenormals ftz;
  11178. std::vector<float> inL (4096), inR (4096);
  11179. for (int n = 0; n < 4096; ++n)
  11180. {
  11181. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  11182. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  11183. }
  11184. struct Gold { int i; float l, r; };
  11185. auto state = [] (const char* hex) {
  11186. std::vector<std::uint8_t> b;
  11187. for (const char* p = hex; p[0] && p[1]; p += 2)
  11188. b.push_back ((std::uint8_t) std::strtol (std::string (p, 2).c_str(), nullptr, 16));
  11189. return b;
  11190. };
  11191. auto viaImport = [&] (const char* plugin, const char* hex, lynx::EffectKind kind, std::initializer_list<Gold> gold) {
  11192. const auto slot = lynx::effectFromFl (plugin, state (hex), 120.0);
  11193. CHECK (slot && slot->kind == kind);
  11194. if (! slot)
  11195. return;
  11196. auto fx = lynx::makeEffect (*slot);
  11197. fx->prepare (44100.0);
  11198. std::vector<float> l (inL), r (inR);
  11199. fx->process (l.data(), r.data(), 1024, slot->params, 120.0);
  11200. fx->process (l.data() + 1024, r.data() + 1024, 1024, slot->params, 120.0);
  11201. fx->process (l.data() + 2048, r.data() + 2048, 2048, slot->params, 120.0);
  11202. for (const auto& g : gold)
  11203. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  11204. };
  11205. using K = lynx::EffectKind;
  11206. viaImport ("Fruity Fast LP", "01000000e0010000bc020000", K::fruityFastLP,
  11207. { { 100, -2.876206159591675f, 1.5413906574249268f }, { 1000, 0.12972253561019897f, 0.02936629205942154f },
  11208. { 2047, -0.2262123078107834f, -0.15169914066791534f }, { 3000, 0.12972258031368256f, -0.0798468142747879f },
  11209. { 4095, 0.0011161785805597901f, -0.08101501315832138f } });
  11210. viaImport ("Fruity Fast LP", "01000000890000008a000000", K::fruityFastLP,
  11211. { { 100, -0.7748895883560181f, -0.2310182899236679f }, { 1000, -0.01256702933460474f, -0.0086913350969553f },
  11212. { 2047, -0.010225297883152962f, -0.014700239524245262f }, { 3000, -0.012712989933788776f, -0.008916046470403671f },
  11213. { 4095, -0.008918046951293945f, -0.020280027762055397f } });
  11214. viaImport ("Fruity Free Filter", "02000000c2010000200300002c0100008403000001", K::fruityFreeFilter, // low shelf, CENTER
  11215. { { 100, -0.6178444623947144f, -0.13762974739074707f }, { 1000, -1.6557703018188477f, 0.8969771862030029f },
  11216. { 2047, 0.6539966464042664f, -0.21659433841705322f }, { 3000, -1.6404345035552979f, 0.4286947548389435f },
  11217. { 4095, -1.2272151708602905f, -0.5219485759735107f } });
  11218. viaImport ("Fruity Free Filter", "020000008a02000058020000bc020000c800000000", K::fruityFreeFilter, // high shelf
  11219. { { 100, -0.06996861100196838f, -0.11112016439437866f }, { 1000, -0.47636136412620544f, 0.31258222460746765f },
  11220. { 2047, 0.1628853678703308f, -0.3169582486152649f }, { 3000, -0.47636136412620544f, -0.22028093039989471f },
  11221. { 4095, -0.0975298061966896f, 0.05710025131702423f } });
  11222. viaImport ("Fruity Free Filter", "020000006400000000040000a9000000a402000001", K::fruityFreeFilter, // low pass 16 kHz
  11223. { { 100, 1.3032524585723877f, -0.007953938096761703f }, { 1000, 0.18662987649440765f, 2.155099391937256f },
  11224. { 2047, 0.5824014544487f, 0.05704556778073311f }, { 3000, 0.18981599807739258f, -0.6362011432647705f },
  11225. { 4095, 1.2624475955963135f, 2.497816324234009f } });
  11226. viaImport ("Fruity Filter", "020000004b01000076000000000400002c010000580200000100000001", K::fruityFilter, // x2, CENTER
  11227. { { 100, 0.2685631811618805f, 0.16885951161384583f }, { 1000, -0.20637311041355133f, 0.13557639718055725f },
  11228. { 2047, 0.07038643211126328f, -0.15532609820365906f }, { 3000, -0.20311756432056427f, -0.09958365559577942f },
  11229. { 4095, 0.08275680989027023f, 0.010792487300932407f } });
  11230. viaImport ("Fruity Filter", "01000000bc020000f401000000000000000400000000000000000000", K::fruityFilter, // band pass
  11231. { { 100, -0.4368843734264374f, -0.12492841482162476f }, { 1000, -0.3362128436565399f, -0.09686881303787231f },
  11232. { 2047, 0.21920710802078247f, 0.3213832974433899f }, { 3000, -0.3362128436565399f, 0.4016203284263611f },
  11233. { 4095, -0.14230486750602722f, -0.22842440009117126f } });
  11234. // an old Free Filter layout (type·10) is converted to the current scale; parameter texts
  11235. {
  11236. const auto slot = lynx::effectFromFl ("Fruity Free Filter", state ("010000003c00000000020000a9000000a4020000"), 120.0);
  11237. CHECK (slot && slot->kind == K::fruityFreeFilter);
  11238. if (slot)
  11239. {
  11240. CHECK (lynx::flfilter::freeFilterFromParams (slot->params.data()).type == 100 + (int) std::nearbyint (5.5 / 7.0 * 600.0));
  11241. CHECK (lynx::formatEffectParam (K::fruityFreeFilter, 0, slot->params[0], 120.0) == "Peaking");
  11242. }
  11243. CHECK (lynx::formatEffectParam (K::fruityFreeFilter, 1, 1.0f, 120.0) == "16000 Hz");
  11244. CHECK (lynx::formatEffectParam (K::fruityFilter, 0, 1.0f, 120.0) == "7350 Hz");
  11245. }
  11246. // unreadable states keep the old approximations
  11247. CHECK (lynx::effectFromFl ("Fruity Fast LP", { 1, 2, 3 }, 120.0)->kind == K::filter);
  11248. CHECK (lynx::effectFromFl ("Fruity Free Filter", { 1, 2, 3 }, 120.0)->kind == K::filter);
  11249. CHECK (lynx::effectFromFl ("Fruity Filter", { 9, 0, 0, 0 }, 120.0)->kind == K::filter);
  11250. }
  11251. // ---- Sytrus (REA_rodada31) -------------------------------------------------------------------------------------------
  11252. // A Sytrus state built from scratch (version 25 layout, our own values — no FL preset data), zlib "stored" blocks.
  11253. namespace
  11254. {
  11255. struct SytrusTestPatch
  11256. {
  11257. std::vector<std::int32_t> main = std::vector<std::int32_t> (240, 0); // 962-byte main block as ints (+ mute bytes)
  11258. struct Op
  11259. {
  11260. int p[10] { 0, 256, 0, 0, 0, 0, 0, 0, 0, 0 };
  11261. int ratio = 20000, offset = 0, noise = 0;
  11262. std::vector<float> amps { 1.0f };
  11263. std::uint8_t flags[8] {};
  11264. int env1[3] { -1, -1, -1 }; // volume envelope: on (≠ −1 → on), decay point, sustain point
  11265. std::vector<std::pair<double, double>> env1pts;
  11266. } ops[6];
  11267. struct Filt
  11268. {
  11269. int p[10] { 0, 0, 128, 0, 128, 0, 0, 128, 128, 0 };
  11270. int type = 3;
  11271. } filts[3];
  11272. std::array<std::array<std::uint8_t, 23>, 3> delays {};
  11273. std::array<std::int32_t, 9> reverb { 19, 221, 0, 50, 80, 30, 221, 2, 0 };
  11274. bool reverbOn = false;
  11275. int eqmode = 0;
  11276. SytrusTestPatch()
  11277. {
  11278. main[0] = 128, main[1] = 128;
  11279. for (int i = 0; i < 8; ++i)
  11280. main[(std::size_t) (165 + i)] = (i % 4 == 2) ? 0 : 128;
  11281. main[173] = 1, main[175] = 128, main[188] = 128, main[189] = -1, main[190] = main[191] = 256;
  11282. }
  11283. void cell (int layer, int row, int col, int v) { main[(std::size_t) (3 + layer * 81 + row * 9 + col)] = v; }
  11284. std::vector<std::uint8_t> blob() const
  11285. {
  11286. std::vector<std::uint8_t> b;
  11287. 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))); };
  11288. auto f32 = [&] (float v) { std::int32_t x; std::memcpy (&x, &v, 4); i32 (x); };
  11289. 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))); };
  11290. // point lists: kind 1 envelope, 2 LFO, 3 map / shaper curve; format version 3
  11291. auto env = [&] (int kind, const std::vector<std::pair<double, double>>& pts, int on, int decay, int sus) {
  11292. i32 (kind), i32 (3), i32 ((std::int32_t) pts.size());
  11293. for (const auto& [dt, y] : pts)
  11294. f64 (dt), f64 (y), f32 (0.0f), i32 (0);
  11295. i32 (0), i32 (on), i32 (decay), i32 (-1), i32 (sus);
  11296. };
  11297. for (int i = 0; i < 240; ++i)
  11298. if (4 * i + 4 <= 0x320)
  11299. i32 (main[(std::size_t) i]);
  11300. b.resize (962, 0); // mute bytes: none muted
  11301. i32 (0); // global declick amount
  11302. b.push_back (0), b.push_back (0), b.push_back (0); // OSC grouping, DC stage, GIBBS OFF
  11303. for (int k = 0; k < 9; ++k)
  11304. b.push_back (k == 7 ? 1 : 0); // flag group (release multiplier on, as Default.fst)
  11305. for (const auto& d : delays)
  11306. b.insert (b.end(), d.begin(), d.end());
  11307. for (auto v : reverb)
  11308. i32 (v);
  11309. b.push_back (reverbOn ? 1 : 0), b.push_back (0);
  11310. i32 (eqmode);
  11311. for (int k = 0; k < 3; ++k)
  11312. i32 (0), i32 (30000), i32 (20000), i32 (k);
  11313. b.push_back (0); // no info text
  11314. for (int u = 0; u < 10; ++u)
  11315. {
  11316. const int narts = u < 6 ? 6 : (u < 9 ? 8 : 2);
  11317. for (int j = 0; j < 10; ++j)
  11318. i32 (u < 6 ? ops[u].p[j] : (u < 9 ? filts[u - 6].p[j] : 0));
  11319. if (u < 6)
  11320. {
  11321. const auto& o = ops[u];
  11322. const auto at = b.size();
  11323. b.resize (at + 0x828, 0);
  11324. auto put = [&] (std::size_t off, const void* src, std::size_t n) { std::memcpy (b.data() + at + off, src, n); };
  11325. put (0, &o.ratio, 4), put (4, &o.offset, 4), put (8, &o.noise, 4);
  11326. put (0xc, o.amps.data(), 4 * o.amps.size());
  11327. const std::int32_t shape[5] { 0, 0, 0, 0, 128 };
  11328. put (0x80c, shape, sizeof shape);
  11329. put (0x820, o.flags, 8);
  11330. }
  11331. else if (u < 9)
  11332. {
  11333. const auto at = b.size();
  11334. b.resize (at + 0x30, 0);
  11335. std::memcpy (b.data() + at + 0x2c, &filts[u - 6].type, 4);
  11336. env (3, { { 0.0, 0.0 }, { 1.0, 1.0 } }, 0, -1, -1);
  11337. b.push_back (0);
  11338. }
  11339. for (int k = 0; k < narts; ++k)
  11340. {
  11341. const bool add = k == 0 || k == 3 || (u >= 6 && u <= 8 && (k == 2 || k == 3)) || (u < 6 && k == 4);
  11342. const double neutral = add ? 0.5 : 1.0;
  11343. i32 (3), i32 (0), f32 (1.0f), f32 (1.0f);
  11344. if (u < 6 && k == 1 && ops[u].env1[0] != -1)
  11345. env (1, ops[u].env1pts, -1, ops[u].env1[1], ops[u].env1[2]);
  11346. else
  11347. env (1, { { 0.0, 0.0 }, { 1.0, 1.0 } }, 0, -1, -1);
  11348. i32 (128), i32 (128), i32 (0), i32 (128);
  11349. env (2, { { 0.0, 1.0 }, { 1.0, 1.0 } }, 0, -1, -1);
  11350. i32 (40000), i32 (0), i32 (0), i32 (0), i32 (0);
  11351. for (int m = 0; m < 6; ++m)
  11352. env (3, { { 0.0, neutral }, { 1.0, neutral } }, 0, -1, -1);
  11353. }
  11354. }
  11355. b.push_back (0), b.push_back (0);
  11356. // zlib with stored blocks
  11357. std::vector<std::uint8_t> z { 0x78, 0x01 };
  11358. for (std::size_t at = 0; at < b.size(); at += 65535)
  11359. {
  11360. const auto n = std::min<std::size_t> (65535, b.size() - at);
  11361. z.push_back (at + n >= b.size() ? 1 : 0);
  11362. z.push_back ((std::uint8_t) n), z.push_back ((std::uint8_t) (n >> 8));
  11363. z.push_back ((std::uint8_t) ~n), z.push_back ((std::uint8_t) (~n >> 8));
  11364. z.insert (z.end(), b.begin() + (std::ptrdiff_t) at, b.begin() + (std::ptrdiff_t) (at + n));
  11365. }
  11366. std::uint32_t a = 1, c = 0;
  11367. for (auto x : b)
  11368. a = (a + x) % 65521, c = (c + a) % 65521;
  11369. for (int k = 3; k >= 0; --k)
  11370. z.push_back ((std::uint8_t) (((c << 16) | a) >> (8 * k)));
  11371. std::vector<std::uint8_t> out { 25, 0, 0, 0 };
  11372. out.insert (out.end(), z.begin(), z.end());
  11373. return out;
  11374. }
  11375. };
  11376. // renders like FL's Gen_Render calls: notes start/release at the block that contains them
  11377. std::pair<std::vector<float>, std::vector<float>> sytrusRender (const std::vector<std::uint8_t>& blob, bool renderMode, int n,
  11378. std::vector<std::array<int, 3>> notes, double fs = 44100.0)
  11379. {
  11380. lynx::sytrus::Engine e;
  11381. e.setTempo (120.0);
  11382. e.prepare (fs, renderMode, 512);
  11383. e.setPatch (lynx::sytrus::parsePatch (blob));
  11384. std::vector<float> l ((std::size_t) n), r ((std::size_t) n);
  11385. for (int n0 = 0; n0 < n; n0 += 512)
  11386. {
  11387. for (std::size_t k = 0; k < notes.size(); ++k)
  11388. {
  11389. if (notes[k][1] >= n0 && notes[k][1] < n0 + 512)
  11390. {
  11391. lynx::sytrus::NoteParams p;
  11392. p.key = (float) notes[k][0];
  11393. p.id = (std::int64_t) k + 1;
  11394. e.noteOn (p);
  11395. }
  11396. if (notes[k][2] >= n0 && notes[k][2] < n0 + 512)
  11397. e.release ((std::int64_t) k + 1);
  11398. }
  11399. e.render (l.data() + n0, r.data() + n0, std::min (512, n - n0), 1.0f, 0.0f);
  11400. }
  11401. return { l, r };
  11402. }
  11403. } // namespace
  11404. TEST (sytrus_engine_sine)
  11405. {
  11406. // one sine operator, ratio 1, OUT 256, no envelopes: C5 = 523.2511 Hz at 0.7071 (centre of the pan law, §6.1)
  11407. SytrusTestPatch t;
  11408. t.cell (0, 0, 8, 256);
  11409. const auto blob = t.blob();
  11410. CHECK (lynx::sytrus::stateUsable (blob));
  11411. CHECK (lynx::sytrus::parsePatch (blob) != nullptr);
  11412. auto broken = blob;
  11413. broken.resize (broken.size() / 2);
  11414. CHECK (! lynx::sytrus::stateUsable (broken));
  11415. const auto [l, r] = sytrusRender (blob, false, 8192, { { 60, 0, 4096 } });
  11416. double worst = 0.0;
  11417. for (int k = 0; k < 4000; ++k)
  11418. 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)));
  11419. CHECK (worst < 2e-5);
  11420. CHECK (l == r);
  11421. // 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
  11422. // 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)
  11423. int last = 0;
  11424. for (int k = 0; k < 8192; ++k)
  11425. if (l[(std::size_t) k] != 0.0f)
  11426. last = k;
  11427. CHECK (last == 4895);
  11428. // key 69 = 880 Hz; 48 kHz
  11429. const auto [l2, r2] = sytrusRender (blob, false, 4096, { { 69, 0, -1 } }, 48000.0);
  11430. worst = 0.0;
  11431. for (int k = 0; k < 4000; ++k)
  11432. 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)));
  11433. CHECK (worst < 5e-5);
  11434. (void) r2;
  11435. }
  11436. TEST (sytrus_engine_reference)
  11437. {
  11438. // FM 2→1, Vanilla LP filter, volume envelope, unison 3, delay and reverb, 2× oversampling with the 8-sample
  11439. // envelope tick (render settings): against our Python reference model of REA_rodada31 (sytmodel.render)
  11440. SytrusTestPatch t;
  11441. t.main[195] = 0, t.main[196] = 1, t.main[193] = 0, t.main[194] = 1;
  11442. t.main[173] = 3, t.main[174] = 64, t.main[176] = 100;
  11443. t.ops[1].ratio = 40000;
  11444. t.cell (0, 0, 1, 128); // op 2 → op 1 FM
  11445. t.cell (0, 0, 8, 128);
  11446. t.cell (0, 6, 0, 256); // op 1 → filter 1
  11447. t.cell (0, 6, 8, 256);
  11448. t.cell (0, 0, 7, 200); // FX send
  11449. t.filts[0].p[0] = 1, t.filts[0].p[2] = 180, t.filts[0].p[3] = 60;
  11450. t.ops[0].env1[0] = 0, t.ops[0].env1[1] = 1, t.ops[0].env1[2] = 2;
  11451. t.ops[0].env1pts = { { 0.0, 0.0 }, { 0.05, 1.0 }, { 0.1, 0.6 }, { 0.2, 0.0 } };
  11452. const std::int32_t d0[5] { 40, 100, 50, 100, 0 };
  11453. std::memcpy (t.delays[0].data(), d0, 20);
  11454. t.delays[0][22] = 1;
  11455. t.reverb = { 19, 221, 10, 50, 80, 30, 100, 2, 100 };
  11456. t.reverbOn = true;
  11457. const auto blob = t.blob();
  11458. const auto [l, r] = sytrusRender (blob, true, 12000, { { 60, 0, 6000 } });
  11459. // reference values (model render_mode=True, 44.1 kHz, block 512, key 60, velocity 0.78125, release at 6000); the same
  11460. // state rendered by FL's own Sytrus DLL in render mode differs from this by −136 dB (EXEC, REA round 31 harness)
  11461. const std::array<std::pair<int, float>, 8> golden { { { 100, -0.00235529384f }, { 777, -0.175126165f }, { 2000, 0.30976975f }, { 4321, -0.0213534199f },
  11462. { 6100, 0.202372357f }, { 7000, 0.249530748f }, { 9000, 0.0307101421f }, { 11999, 0.176390275f } } };
  11463. for (const auto& [i, v] : golden)
  11464. CHECK (std::abs (l[(std::size_t) i] - v) < 1e-6f);
  11465. const std::array<std::pair<int, float>, 3> goldenR { { { 777, -0.166890964f }, { 4321, -0.212358952f }, { 9000, -0.0251909085f } } };
  11466. for (const auto& [i, v] : goldenR)
  11467. CHECK (std::abs (r[(std::size_t) i] - v) < 1e-6f);
  11468. }
  11469. TEST (sytrus_instrument)
  11470. {
  11471. // the LYNX instrument: state in ChannelDef::pluginState, channel volume/pan recovered from the rack gains
  11472. SytrusTestPatch t;
  11473. t.cell (0, 0, 8, 256);
  11474. lynx::ChannelDef c;
  11475. c.instrument = lynx::InstrumentKind::sytrus;
  11476. c.pluginState = t.blob();
  11477. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sytrus);
  11478. inst->prepare (44100.0);
  11479. inst->configure (c, 120.0);
  11480. CHECK (inst->idle());
  11481. std::vector<float> l (4096), r (4096);
  11482. inst->noteOn (60, 100.0f / 128.0f);
  11483. // 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
  11484. inst->render (l.data(), r.data(), 4096, 0.5f, 0.5f);
  11485. double peak = 0.0;
  11486. for (int k = 1000; k < 4096; ++k)
  11487. peak = std::max (peak, (double) std::abs (l[(std::size_t) k]));
  11488. CHECK (std::abs (peak - 0.5 * 0.70710678) < 1e-3); // Final.Vol 0.5 × the plugin's centre pan law
  11489. CHECK (! inst->idle());
  11490. inst->noteOff (60);
  11491. std::vector<float> tail (44100 * 3), tailR (tail.size());
  11492. inst->render (tail.data(), tailR.data(), (int) tail.size(), 0.5f, 0.5f);
  11493. CHECK (inst->idle());
  11494. CHECK (tail.back() == 0.0f);
  11495. }
  11496. TEST (harmor_installation_presets)
  11497. {
  11498. // REA_rodada165g-flbeta165s: every Harmor preset of the installation plays on the exact engine — the 88 old
  11499. // (version 11…15) ones through the upgrade to the current layout, which renders exactly like the old bytes and
  11500. // keeps the parameter offsets editable; small images (Labyrinth's 16 × 12) are played, not skipped
  11501. if (realFlpDir.empty())
  11502. return;
  11503. const auto folder = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "Harmor";
  11504. if (! std::filesystem::exists (folder))
  11505. return;
  11506. int files = 0, usable = 0, old = 0, upgraded = 0, same = 0;
  11507. const std::vector<lynx::harmor::RefNote> notes { { 0, 60, 0.8, 1.0, 0.0, 3000 } };
  11508. for (const auto& entry : std::filesystem::recursive_directory_iterator (folder))
  11509. {
  11510. if (entry.path().extension() != ".fst")
  11511. continue;
  11512. std::ifstream in (entry.path(), std::ios::binary);
  11513. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  11514. std::vector<std::uint8_t> st;
  11515. try
  11516. {
  11517. for (const auto& c : flp::parseProject (file, true).channels)
  11518. if (! c.pluginState.empty())
  11519. st = c.pluginState;
  11520. }
  11521. catch (const flp::Error&)
  11522. {
  11523. }
  11524. ++files;
  11525. usable += lynx::harmorStateUsable (st);
  11526. std::uint32_t v = 0;
  11527. if (st.size() >= 4)
  11528. std::memcpy (&v, st.data(), 4);
  11529. if ((v > 0xffff ? v >> 16 : v) >= 16)
  11530. continue;
  11531. ++old;
  11532. const auto up = lynx::harmorUpgradeState (st);
  11533. if (! up)
  11534. continue;
  11535. std::uint32_t uv = 0;
  11536. std::memcpy (&uv, up->data(), 4);
  11537. upgraded += uv == 0x130003u && lynx::harmorParamOffset (*up, 0, 34) > 0 && lynx::harmorStateUsable (*up);
  11538. if (old <= 6) // (a few: rendering is the slow part)
  11539. same += lynx::harmor::renderReference (st, notes, 44100.0, 4096) == lynx::harmor::renderReference (*up, notes, 44100.0, 4096);
  11540. else
  11541. ++same;
  11542. }
  11543. std::printf (" Harmor installation presets: %d files, %d usable, %d old, %d upgraded\n", files, usable, old, upgraded);
  11544. CHECK (files >= 300 && usable == files && old >= 80 && upgraded == old && same == old);
  11545. std::ifstream in (folder / "Bell" / "Labyrinth.fst", std::ios::binary);
  11546. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  11547. if (file.empty())
  11548. return;
  11549. auto st = flp::parseProject (file, true).channels.at (0).pluginState;
  11550. const auto a = lynx::harmor::renderReference (st, notes, 44100.0, 4096);
  11551. const std::int32_t zero = 0;
  11552. for (int part = 0; part < 2; ++part)
  11553. std::memcpy (st.data() + lynx::harmorParamOffset (st, part, 92), &zero, 4); // image frequency scale 0
  11554. const auto b = lynx::harmor::renderReference (st, notes, 44100.0, 4096);
  11555. CHECK (a.size() == b.size() && a != b);
  11556. }
  11557. TEST (harmor_image)
  11558. {
  11559. // REA_rodada75: image synthesis — a looping 6-column image (gain + frequency planes) read at a speed, cubic gain and
  11560. // linear frequency interpolation, Hz frequency mode, formant shift — against our reference model (r75 imgm.py);
  11561. // since flbeta165h LYNX's own values: the test state plays at precision "Average" (global misc +28 = 0), whose
  11562. // IPP 11-bit Exp/Ln (REA_rodada165g-flbeta165h §3) the model computes exactly
  11563. const auto blob = harmorTestState (false, true);
  11564. if (const char* dump = std::getenv ("LYNX_DUMP_HARMOR_IMAGE"))
  11565. std::ofstream (dump, std::ios::binary).write ((const char*) blob.data(), (std::streamsize) blob.size());
  11566. CHECK (lynx::harmorStateUsable (blob));
  11567. CHECK (lynx::harmorFallbacks (blob).empty()); // (the image is modelled)
  11568. 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);
  11569. CHECK (ref.size() == 2 * 8192);
  11570. if (std::getenv ("LYNX_PRINT_HARMOR_IMAGE") != nullptr)
  11571. for (int i : { 100, 1000, 2500, 3000, 5000, 6500, 8191 })
  11572. std::printf ("harmor image %d %.8g %.8g\n", i, (double) ref[2 * (std::size_t) i], (double) ref[2 * (std::size_t) i + 1]);
  11573. const std::tuple<int, float, float> golden[] {
  11574. { 100, 0.0022266121f, 0.018016478f }, { 1000, 0.013964245f, -0.028604792f }, { 2500, 0.016782919f, -0.028934296f },
  11575. { 3000, -0.010533399f, -0.0061015566f }, { 5000, -0.017427528f, 0.016475817f }, { 6500, 0.16895676f, 0.078423217f },
  11576. { 8191, -0.045406573f, -0.049047086f } };
  11577. for (const auto& [i, gl, gr] : golden)
  11578. CHECK (std::abs (ref[2 * (std::size_t) i] - gl) < 2e-6f && std::abs (ref[2 * (std::size_t) i + 1] - gr) < 2e-6f);
  11579. // the image changes the sound: without it (gain mix 0, frequency scale 0) the same notes render differently
  11580. auto plain = blob;
  11581. const std::int32_t zero = 0;
  11582. std::memcpy (plain.data() + lynx::harmorParamOffset (plain, 0, 89), &zero, 4);
  11583. std::memcpy (plain.data() + lynx::harmorParamOffset (plain, 0, 92), &zero, 4);
  11584. std::memcpy (plain.data() + lynx::harmorParamOffset (plain, 0, 94), &zero, 4);
  11585. 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);
  11586. double diff = 0.0, energy = 0.0;
  11587. for (std::size_t i = 0; i < ref.size(); ++i)
  11588. diff += (double) (ref[i] - ref0[i]) * (ref[i] - ref0[i]), energy += (double) ref[i] * ref[i];
  11589. CHECK (energy > 1.0 && diff > 0.01 * energy);
  11590. // a resynthesis-mode image (a sample analysed at load time) is not modelled: reported, the part plays its timbres
  11591. auto resyn = blob;
  11592. {
  11593. // the image object follows the 26 curves: find its header (version 0, mode 0, loop 1) from the end of part A
  11594. 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 };
  11595. const auto it = std::search (resyn.begin(), resyn.end(), std::begin (hdr), std::end (hdr));
  11596. CHECK (it != resyn.end());
  11597. if (it != resyn.end())
  11598. *(it + 4) = 1;
  11599. }
  11600. const auto fb = lynx::harmorFallbacks (resyn);
  11601. CHECK (std::any_of (fb.begin(), fb.end(), [] (const std::string& f) { return f.find ("resynthesis") != std::string::npos; }));
  11602. }
  11603. TEST (harmor_round75_features)
  11604. {
  11605. // REA_rodada75: arpeggio markers, noise resonance, pluck blur mode, pluck blend, harmonic randomness, phaser and
  11606. // harmonizer masks — LYNX's engine against our reference model (r75 hmodel.py, the Mersenne Twister seeded like LYNX's);
  11607. // since flbeta165h LYNX's own values: the test state plays at precision "Average", whose IPP 11-bit Exp/Ln
  11608. // (REA_rodada165g-flbeta165h §3) the model computes exactly
  11609. 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 } };
  11610. struct Case { int features; std::array<std::tuple<int, float, float>, 4> golden; };
  11611. const Case cases[] {
  11612. { 1, { { { 2000, 0.0032625794f, -0.0014364491f }, { 4500, 0.10464316f, 0.026205163f }, { 7000, -0.0024230368f, 0.0036458215f }, { 11000, -0.0029985344f, -0.0030873856f } } } },
  11613. { 2, { { { 2000, 0.29411051f, -0.22980602f }, { 4500, -0.004929353f, -0.0013274907f }, { 7000, 0.013239861f, 0.0054404787f }, { 11000, -0.0010576053f, -0.0021088289f } } } },
  11614. { 4, { { { 2000, 0.072069392f, -0.029683406f }, { 4500, -0.10765476f, 0.11332012f }, { 7000, -0.22984716f, -0.10102195f }, { 11000, -0.051904719f, -0.066571109f } } } },
  11615. { 8, { { { 2000, 0.072010428f, -0.029725688f }, { 4500, -0.10748001f, 0.1136639f }, { 7000, -0.22849904f, -0.15257275f }, { 11000, -0.041248851f, -0.050450616f } } } },
  11616. { 16, { { { 2000, 0.063442282f, -0.030946331f }, { 4500, -0.11211193f, 0.11279358f }, { 7000, -0.23168804f, -0.1670858f }, { 11000, -0.041186355f, -0.050383076f } } } },
  11617. { 32, { { { 2000, 0.086303107f, -0.035885293f }, { 4500, 0.069407858f, 0.14133474f }, { 7000, -0.15907738f, -0.10380846f }, { 11000, -0.029428774f, -0.041782599f } } } },
  11618. { 63, { { { 2000, 0.017201126f, -0.010683246f }, { 4500, 0.55659908f, 0.08110752f }, { 7000, -0.0025542902f, 0.020610567f }, { 11000, -0.0031871174f, 0.00024673535f } } } } };
  11619. for (const auto& c : cases)
  11620. {
  11621. const auto blob = harmorTestState (false, false, c.features);
  11622. if (const char* dump = std::getenv ("LYNX_DUMP_HARMOR_R75"))
  11623. std::ofstream (std::string (dump) + std::to_string (c.features) + ".bin", std::ios::binary).write ((const char*) blob.data(), (std::streamsize) blob.size());
  11624. CHECK (lynx::harmorStateUsable (blob));
  11625. CHECK (lynx::harmorFallbacks (blob).empty());
  11626. const auto ref = lynx::harmor::renderReference (blob, chord, 44100.0, 12000);
  11627. if (std::getenv ("LYNX_PRINT_HARMOR_R75") != nullptr)
  11628. for (int i : { 2000, 4500, 7000, 11000 })
  11629. 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]);
  11630. for (const auto& [i, gl, gr] : c.golden)
  11631. CHECK (std::abs (ref[2 * (std::size_t) i] - gl) < 2e-6f && std::abs (ref[2 * (std::size_t) i + 1] - gr) < 2e-6f);
  11632. }
  11633. }
  11634. TEST (sytrus_main_settings)
  11635. {
  11636. // the channel settings panel's controls (REA_rodada74 §3): FL's "Main settings" ints inside the zlib state
  11637. SytrusTestPatch t;
  11638. t.cell (0, 0, 8, 256);
  11639. t.main[173] = 3;
  11640. const auto blob = t.blob();
  11641. const auto& S = lynx::sytrus::settings();
  11642. auto find = [&] (const char* name) {
  11643. for (int i = 0; i < (int) S.size(); ++i)
  11644. if (std::string (S[(std::size_t) i].name) == name)
  11645. return i;
  11646. return -1;
  11647. };
  11648. const int master = find ("Master level"), unison = find ("Unison"), porta = find ("Porta"), live = find ("Live oversampling");
  11649. // 25 main controls, then the matrix (round 172a): FM 9 × 9 (sources, PAN, FX, OUT) and RM 9 × 6
  11650. CHECK (master >= 0 && unison >= 0 && porta >= 0 && live >= 0 && S.size() == 25u + 81u + 54u);
  11651. const int out1 = find ("Op 1 out"), fm21 = find ("FM op 2 to op 1"), rm3f2 = find ("RM op 3 to filter 2");
  11652. CHECK (out1 >= 0 && fm21 >= 0 && rm3f2 >= 0 && find ("RM op 1 pan") < 0);
  11653. CHECK (out1 >= 0 && S[(std::size_t) out1].index == 3 + 8 && S[(std::size_t) rm3f2].index == 3 + 81 + 9 * 7 + 2);
  11654. CHECK (lynx::sytrus::settingValue (blob, out1) == 256 && lynx::sytrus::settingValue (blob, fm21) == 0);
  11655. CHECK (lynx::sytrus::settingValue (blob, master) == 128 && lynx::sytrus::settingValue (blob, unison) == 3);
  11656. CHECK (lynx::sytrus::settingValue (blob, porta) == 0 && lynx::sytrus::settingValue (blob, find ("Mod X")) == 256);
  11657. CHECK (! lynx::sytrus::settingValue (blob, -1) && ! lynx::sytrus::settingValue (blob, (int) S.size()));
  11658. auto broken = blob;
  11659. broken.resize (broken.size() / 2);
  11660. CHECK (! lynx::sytrus::settingValue (broken, master) && ! lynx::sytrus::setSetting (broken, master, 64));
  11661. // master level 64: gain (11^(64/128) − 1)/10 instead of 1 (REA_rodada11 §1.3); the state stays readable
  11662. auto quiet = blob;
  11663. CHECK (lynx::sytrus::setSetting (quiet, master, 64) && lynx::sytrus::stateUsable (quiet));
  11664. CHECK (lynx::sytrus::settingValue (quiet, master) == 64 && lynx::sytrus::settingValue (quiet, unison) == 3);
  11665. CHECK (quiet[0] == blob[0] && quiet[1] == blob[1]); // version word kept
  11666. const auto [l1, r1] = sytrusRender (blob, false, 4096, { { 60, 0, -1 } });
  11667. const auto [l2, r2] = sytrusRender (quiet, false, 4096, { { 60, 0, -1 } });
  11668. const auto g = (std::sqrt (11.0) - 1.0) / 10.0;
  11669. double worst = 0.0;
  11670. for (int k = 0; k < 4096; ++k)
  11671. worst = std::max (worst, std::abs ((double) l2[(std::size_t) k] - g * l1[(std::size_t) k]));
  11672. CHECK (worst < 1e-5);
  11673. (void) r1, (void) r2;
  11674. // clamped to the control's range; PORTA is the flag byte the engine reads
  11675. auto edited = blob;
  11676. CHECK (lynx::sytrus::setSetting (edited, unison, 42) && lynx::sytrus::settingValue (edited, unison) == 9);
  11677. CHECK (lynx::sytrus::setSetting (edited, porta, 1) && lynx::sytrus::settingValue (edited, porta) == 1);
  11678. CHECK (lynx::sytrus::setSetting (edited, live, 2) && lynx::sytrus::settingValue (edited, live) == 2);
  11679. CHECK (lynx::sytrus::parsePatch (edited) != nullptr);
  11680. // a matrix cell: op 2 modulates op 1 (FM), clamped to ±256; the patch stays playable
  11681. CHECK (lynx::sytrus::setSetting (edited, fm21, 999) && lynx::sytrus::settingValue (edited, fm21) == 256);
  11682. CHECK (lynx::sytrus::settingValue (edited, out1) == 256 && lynx::sytrus::parsePatch (edited) != nullptr);
  11683. }
  11684. TEST (harmor_effects_rack)
  11685. {
  11686. // REA_rodada38: the rack runs in the state's unit order and the records' FX send joins it at unit 4 — with the FX
  11687. // amount at 1000 every record output bypasses a reverb placed before unit 4 (EXEC: −600 dB vs the rack off)
  11688. auto blob = harmorTestState (false);
  11689. auto set = [&] (std::vector<std::uint8_t>& st, int part, int index, std::int32_t v) {
  11690. std::memcpy (st.data() + lynx::harmorParamOffset (st, part, index), &v, 4);
  11691. };
  11692. set (blob, 0, 10, 1000); // all to the FX send
  11693. auto render = [] (const std::vector<std::uint8_t>& st) {
  11694. return lynx::harmor::renderReference (st, { { 0, 60, 0.8, 1.0, 0.0, 6000 } }, 44100.0, 12000);
  11695. };
  11696. const auto dry = render (blob);
  11697. auto rev = blob;
  11698. set (rev, -1, 22, 1); // reverb on: room 50, diffusion 100, decay 2^3·0.1 s, high cut / damping off, wet 0.8
  11699. 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);
  11700. set (rev, -1, 30, 2), set (rev, -1, 31, 800);
  11701. const auto bypassed = render (rev);
  11702. double eb = 0.0, db = 0.0;
  11703. for (std::size_t i = 0; i < dry.size(); ++i)
  11704. eb += (double) (bypassed[i] - dry[i]) * (bypassed[i] - dry[i]), db += (double) dry[i] * dry[i];
  11705. CHECK (eb < 1e-12 * db && db > 1e-3); // (float rounding of the rack's buffers only)
  11706. auto after = rev;
  11707. const std::int32_t order[6] { 0, 1, 2, 4, 3, 5 }; // the join before the reverb
  11708. for (int k = 0; k < 6; ++k)
  11709. set (after, -1, 53 + k, order[k]);
  11710. const auto wet = render (after);
  11711. double e = 0.0, d = 0.0;
  11712. bool finite = true;
  11713. for (std::size_t i = 0; i < wet.size(); ++i)
  11714. e += (double) (wet[i] - dry[i]) * (wet[i] - dry[i]), d += (double) dry[i] * dry[i], finite = finite && std::isfinite (wet[i]);
  11715. CHECK (finite && e > 1e-4 * d && d > 1e-3);
  11716. // a distortion (type 6) and a delay on the dry path change the sound, stay finite, and the instrument ends quiet
  11717. auto dist = harmorTestState (false);
  11718. {
  11719. const auto at = lynx::harmorParamOffset (dist, -1, 0) + 0xd4 + 0x18 + 48; // global misc +48: distortion type
  11720. const std::int32_t six = 6;
  11721. std::memcpy (dist.data() + at, &six, 4);
  11722. }
  11723. set (dist, -1, 17, 600), set (dist, -1, 19, 1000), set (dist, -1, 21, 1100);
  11724. 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);
  11725. set (dist, -1, 47, 1050);
  11726. lynx::ChannelDef c;
  11727. c.instrument = lynx::InstrumentKind::harmor;
  11728. c.pluginState = dist;
  11729. auto inst = lynx::makeInstrument (lynx::InstrumentKind::harmor);
  11730. inst->configure (c, 120.0);
  11731. inst->prepare (44100.0);
  11732. std::vector<float> l (44100 * 4), r (l.size());
  11733. inst->noteOn (60, 0.8f);
  11734. double energy = 0.0;
  11735. for (int s = 0; s < (int) l.size(); s += 512)
  11736. {
  11737. if (s == 8192)
  11738. inst->noteOff (60);
  11739. inst->render (l.data() + s, r.data() + s, std::min (512, (int) l.size() - s), 1.0f, 1.0f);
  11740. }
  11741. for (std::size_t i = 0; i < l.size(); ++i)
  11742. finite = finite && std::isfinite (l[i]) && std::isfinite (r[i]), energy += (double) l[i] * l[i];
  11743. CHECK (finite && energy > 1.0);
  11744. std::vector<float> tail (44100 * 6), tailR (tail.size());
  11745. for (int s = 0; s < (int) tail.size(); s += 512)
  11746. inst->render (tail.data() + s, tailR.data() + s, std::min (512, (int) tail.size() - s), 1.0f, 1.0f);
  11747. CHECK (inst->idle());
  11748. }
  11749. TEST (harmless_engine)
  11750. {
  11751. // REA_rodada32: state parsing, the additive engine against our reference model (hlmodel.py), host levels, voice end
  11752. std::vector<std::uint8_t> blob;
  11753. 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))); };
  11754. std::array<std::int32_t, 100> p {}; // the reference's clean patch: Saw, open filter, no modulation
  11755. for (int i = 11; i < 23; ++i)
  11756. p[(std::size_t) i] = 1000;
  11757. 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;
  11758. 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;
  11759. // + resonance, pluck, 3-voice unison, tremolo, LFO on pitch
  11760. 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;
  11761. put (12);
  11762. for (auto v : p)
  11763. put (v);
  11764. for (int i = 0; i < 7; ++i)
  11765. put (0);
  11766. put (524); // Custom colour table: 524 × (amplitude, phase)
  11767. for (int i = 0; i < 1048; ++i)
  11768. {
  11769. const auto f = i < 524 ? 1.0f / (float) (i + 1) : 0.25f;
  11770. std::uint32_t bits = 0;
  11771. std::memcpy (&bits, &f, 4);
  11772. put ((std::int32_t) bits);
  11773. }
  11774. const auto parsed = lynx::harmlessParams (blob);
  11775. CHECK (parsed && (int) parsed->size() == lynx::harmlessParamCount);
  11776. CHECK (parsed && (*parsed)[54] == 3500.0f && (*parsed)[99] == 0.0f && (*parsed)[lynx::harmlessCustomFlagIndex] == 1.0f);
  11777. CHECK (parsed && (*parsed)[lynx::harmlessCustomIndex + 1] == 0.5f && (*parsed)[lynx::harmlessCustomIndex + 524] == 0.25f);
  11778. auto old = blob;
  11779. old[0] = 3; // versions below 4 are not Harmless states we know
  11780. CHECK (! lynx::harmlessParams (old));
  11781. old = blob;
  11782. old.resize (300);
  11783. CHECK (! lynx::harmlessParams (old));
  11784. lynx::ChannelDef c;
  11785. c.instrument = lynx::InstrumentKind::harmless;
  11786. c.modelParams = *parsed;
  11787. c.modelParams[lynx::harmlessCustomFlagIndex] = 0.0f; // colour 0 (Saw): the custom table is not read
  11788. auto render = [] (const lynx::ChannelDef& def, int total, float gl, float gr, int offAt) {
  11789. auto inst = lynx::makeInstrument (lynx::InstrumentKind::harmless);
  11790. inst->configure (def, 120.0);
  11791. inst->prepare (44100.0);
  11792. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  11793. inst->noteOn (60, 0.78125f);
  11794. for (int s = 0; s < total; s += 512)
  11795. {
  11796. if (s == offAt)
  11797. inst->noteOff (60);
  11798. inst->render (l.data() + s, r.data() + s, std::min (512, total - s), gl, gr);
  11799. }
  11800. return std::make_tuple (l, r, std::move (inst));
  11801. };
  11802. {
  11803. // reference model (REA round 32 hlmodel.py, C5 at FL velocity 100, channel volume 1, centre, released at 6144)
  11804. auto [l, r, inst] = render (c, 8192, 1.0f, 1.0f, 6144);
  11805. const std::tuple<int, float, float> golden[] {
  11806. { 0, 0.0f, 0.0f }, { 10, -0.02220994f, -0.06232700f }, { 100, 0.40623373f, 0.17308202f }, { 1000, -0.04091963f, -0.26821208f },
  11807. { 3000, 0.14519004f, 0.11027778f }, { 6000, 0.50411958f, 0.27901855f }, { 8191, 0.02117692f, -0.01466602f } };
  11808. for (const auto& [i, gl, gr] : golden)
  11809. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  11810. // after the release the record fades out (below 1e-4, then 30 ms) and the instrument goes idle
  11811. std::vector<float> tail (44100), tailR (tail.size());
  11812. inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
  11813. CHECK (inst->idle());
  11814. }
  11815. // FL's level law applied once: half the channel volume halves the output; full right pan silences the left side
  11816. const auto full = std::get<0> (render (c, 2048, 1.0f, 1.0f, -1));
  11817. const auto half = std::get<0> (render (c, 2048, 0.5f, 0.5f, -1));
  11818. CHECK (std::abs (half[100] - 0.5f * full[100]) < 1e-6f && std::abs (full[100]) > 0.1f);
  11819. auto mono = c;
  11820. mono.modelParams[25] = 0.0f; // no tremolo, no unison pan spread
  11821. mono.modelParams[65] = 1.0f;
  11822. const auto [pl, pr, pi] = render (mono, 2048, 0.0f, 1.41421356f, -1);
  11823. double el = 0.0, er = 0.0;
  11824. for (int i = 0; i < 2048; ++i)
  11825. el += (double) pl[(std::size_t) i] * pl[(std::size_t) i], er += (double) pr[(std::size_t) i] * pr[(std::size_t) i];
  11826. CHECK (el < 1e-10 * er && er > 1.0);
  11827. }
  11828. TEST (startup_sound)
  11829. {
  11830. // the splash's sound: the right length, finite, at its peak level, quiet before the whoosh swells,
  11831. // a landing "pop" at impactTime and a chime ringing after it
  11832. using namespace lynx::startup;
  11833. const auto sound = makeSound (48000.0);
  11834. CHECK (sound.left.size() == (std::size_t) (soundLength * 48000.0) && sound.right.size() == sound.left.size());
  11835. float peak = 0.0f;
  11836. bool finite = true;
  11837. for (std::size_t i = 0; i < sound.left.size(); ++i)
  11838. {
  11839. finite = finite && std::isfinite (sound.left[i]) && std::isfinite (sound.right[i]);
  11840. peak = std::max ({ peak, std::abs (sound.left[i]), std::abs (sound.right[i]) });
  11841. }
  11842. CHECK (finite && std::abs (peak - peakLevel) < 1.0e-4f);
  11843. auto rms = [&sound] (double from, double to) {
  11844. double e = 0.0;
  11845. const auto a = (std::size_t) (from * 48000.0), b = (std::size_t) (to * 48000.0);
  11846. for (auto i = a; i < b; ++i)
  11847. e += (double) sound.left[i] * sound.left[i];
  11848. return std::sqrt (e / (double) (b - a));
  11849. };
  11850. CHECK (rms (0.0, 0.05) < 0.2 * rms (impactTime - 0.05, impactTime)); // the whoosh swells
  11851. CHECK (rms (impactTime, impactTime + 0.05) > rms (impactTime - 0.05, impactTime)); // the landing
  11852. CHECK (rms (0.8, 1.0) > 0.005 && rms (soundLength - 0.02, soundLength) < rms (0.8, 1.0)); // chime rings, tail fades
  11853. CHECK (makeSound (48000.0).left == sound.left); // the same sound on every start
  11854. }
  11855. namespace
  11856. {
  11857. // little-endian byte writer for the state builders below
  11858. struct Bytes
  11859. {
  11860. std::vector<std::uint8_t> b;
  11861. void raw (const char* s, std::size_t n) { b.insert (b.end(), s, s + n); }
  11862. 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); }
  11863. void f32 (float v) { b.insert (b.end(), reinterpret_cast<const std::uint8_t*> (&v), reinterpret_cast<const std::uint8_t*> (&v) + 4); }
  11864. void zeros (std::size_t n) { b.insert (b.end(), n, 0); }
  11865. void bytes (const std::vector<std::uint8_t>& o) { b.insert (b.end(), o.begin(), o.end()); }
  11866. };
  11867. // A GMS state written from scratch (REA_rodada12 §1.1 layout, "Syn4"): oscillator waves that don't exist (silent
  11868. // tables, so the result does not depend on an FL installation), noise through the ladder with envelope and LFO,
  11869. // unison, slide, distortion, delay, reverb and the output EQ.
  11870. std::vector<std::uint8_t> gmsTestState()
  11871. {
  11872. std::array<float, 212> P {};
  11873. for (int i : { 1, 2, 44, 55, 56, 57, 58, 59, 60, 207, 208, 209, 210, 211 })
  11874. P[(std::size_t) i] = 0.5f;
  11875. P[0] = 0.75f, P[4] = 0.1f, P[5] = 0.4f, P[6] = 0.6f, P[7] = 0.3f, P[8] = 0.8f;
  11876. 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;
  11877. 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;
  11878. P[147] = 1.0f, P[117] = 0.3f, P[127] = 0.6f; // distortion
  11879. P[153] = 1.0f, P[123] = 0.5f, P[133] = 0.4f, P[143] = 0.4f; // delay (1/2 beat)
  11880. P[154] = 1.0f, P[124] = 0.6f, P[134] = 0.5f; // reverb
  11881. P[206] = 1.0f, P[207] = 0.7f, P[211] = 0.3f; // output EQ
  11882. Bytes preset;
  11883. preset.raw ("4nyS", 4);
  11884. for (const char* path : { "NoSuchDir\\NoWave A.wav", "NoSuchDir\\NoWave B.wav", "NoSuchDir\\NoWave C.wav", "", "", "", "" })
  11885. {
  11886. const auto n = std::strlen (path);
  11887. preset.raw (path, n);
  11888. preset.zeros (512 - n);
  11889. }
  11890. preset.i32 (0), preset.i32 (0), preset.i32 (0);
  11891. for (auto v : P)
  11892. preset.f32 (v);
  11893. preset.raw ("LYNX test", 9);
  11894. preset.zeros (512 - 9);
  11895. Bytes s;
  11896. s.raw ("SSLF", 4);
  11897. s.i32 ((std::int32_t) preset.b.size());
  11898. s.bytes (preset.b);
  11899. return s.b;
  11900. }
  11901. // A Morphine state written from scratch (REA_rodada16 §3 / REA_rodada39 §2.2 layout): generators A and B with a few
  11902. // frames each (A loops, PWM on A), C and D silent, a morph line A→B, four envelopes (Eg1 → pitch and velocity →
  11903. // amplitude through the matrix), the master ADSR and every plugin effect on.
  11904. std::vector<std::uint8_t> morphineTestState()
  11905. {
  11906. auto mbp = [] (int seed) {
  11907. Bytes b;
  11908. b.raw ("0PBM", 4);
  11909. for (int a = 0; a < 3; ++a)
  11910. for (int byte = 0; byte < 16; ++byte)
  11911. {
  11912. b.b.push_back (0xff);
  11913. for (int bit = 0; bit < 8; ++bit)
  11914. {
  11915. const auto k = byte * 8 + bit;
  11916. // (cbrtf is not correctly rounded before glibc 2.43 and differs between libms; the cube root in double
  11917. // rounded to float gives the same (correctly rounded) bits with old and new glibc)
  11918. const float v = a == 0 ? (float) std::cbrt ((double) (1.0f / (float) (k + 1 + seed * (k % 3)))) * (k < 40 ? 1.0f : 0.2f)
  11919. : a == 1 ? 0.1f * (float) ((k * 7 + seed) % 11 - 5) / 5.0f
  11920. : 0.02f * (float) ((k * 5 + seed) % 7 - 3);
  11921. b.f32 (v);
  11922. }
  11923. }
  11924. return b.b;
  11925. };
  11926. auto msp = [&] (int frames, int seed, int loopMode) {
  11927. Bytes b;
  11928. b.raw ("0PSM", 4);
  11929. b.i32 (frames);
  11930. for (int f = 0; f < frames; ++f)
  11931. {
  11932. const auto m = mbp (seed + f);
  11933. b.i32 (20 + 7 * f), b.f32 (1.0f - 0.2f * (float) f), b.i32 ((std::int32_t) m.size());
  11934. b.bytes (m);
  11935. }
  11936. b.i32 (0x424);
  11937. b.i32 (10), b.i32 (40), b.i32 (0), b.i32 (loopMode);
  11938. b.f32 (0.45f), b.f32 (0.6f), b.f32 (0.5f), b.i32 (0);
  11939. for (int k = 0; k < 128; ++k)
  11940. b.f32 (0.3f);
  11941. for (int k = 0; k < 128; ++k)
  11942. b.f32 (1.0f);
  11943. return b.b;
  11944. };
  11945. auto mvc = [&] (int frames, int seed, int pwmMode) {
  11946. Bytes z;
  11947. z.raw ("0HZM", 4);
  11948. z.i32 (1);
  11949. const auto s = msp (frames, seed, 1);
  11950. z.i32 (128), z.f32 (frames > 1 ? 0.9f : 0.0f), z.i32 ((std::int32_t) s.size());
  11951. z.bytes (s);
  11952. Bytes b;
  11953. b.raw ("0CVM", 4);
  11954. b.i32 ((std::int32_t) z.b.size());
  11955. b.bytes (z.b);
  11956. 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);
  11957. return b.b;
  11958. };
  11959. Bytes p;
  11960. p.raw ("0roM", 4);
  11961. p.raw ("0RPM", 4);
  11962. for (const auto& g : { mvc (3, 1, 2), mvc (2, 7, 0), mvc (1, 3, 0), mvc (1, 5, 0) })
  11963. {
  11964. p.i32 ((std::int32_t) g.size());
  11965. p.bytes (g);
  11966. }
  11967. {
  11968. Bytes m;
  11969. m.raw ("0LMM", 4);
  11970. m.i32 (2), m.i32 (12);
  11971. m.f32 (0.0f), m.f32 (1.0f), m.i32 (60);
  11972. m.f32 (1.0f), m.f32 (1.0f), m.i32 (60);
  11973. m.i32 (0x54), m.i32 (0), m.i32 (0), m.i32 (0), m.i32 (0);
  11974. for (float v : { 1.0f, 0.8f, 0.0f, 0.0f, 0.5f, 0.4f, 0.5f, 0.5f })
  11975. m.f32 (v);
  11976. for (int k = 0; k < 4; ++k)
  11977. m.i32 (0);
  11978. m.f32 (0.5f), m.f32 (0.5f), m.f32 (0.5f), m.i32 (0);
  11979. p.i32 ((std::int32_t) m.b.size());
  11980. p.bytes (m.b);
  11981. }
  11982. {
  11983. Bytes d;
  11984. d.raw ("0DMM", 4);
  11985. d.i32 (4);
  11986. for (int e = 0; e < 4; ++e)
  11987. {
  11988. Bytes g;
  11989. g.raw ("0GEM", 4);
  11990. g.i32 (3), g.i32 (8);
  11991. g.f32 (0.0f), g.i32 (10), g.f32 (1.0f), g.i32 (40 + 10 * e), g.f32 (0.3f), g.i32 (50);
  11992. 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),
  11993. g.i32 (e == 0 ? 1 : 0);
  11994. d.i32 ((std::int32_t) g.b.size());
  11995. d.bytes (g.b);
  11996. }
  11997. d.i32 (0x94);
  11998. const int src[12] = { 5, 2 }, dst[12] = { 3, 1 };
  11999. const float amt[12] = { 0.56f, 0.7f };
  12000. for (int k = 0; k < 12; ++k)
  12001. d.i32 (src[k]);
  12002. for (int k = 0; k < 12; ++k)
  12003. d.i32 (dst[k]);
  12004. for (int k = 0; k < 12; ++k)
  12005. d.f32 (amt[k]);
  12006. p.i32 ((std::int32_t) d.b.size());
  12007. p.bytes (d.b);
  12008. }
  12009. {
  12010. 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
  12011. 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 …
  12012. 0.4f, 0.3f, 1.0f, 0.5f, 0.5f, 0.4f, 1.0f, 0.6f, 0.4f, 0.5f, // … delay, reverb, EQ …
  12013. 0.5f, 0.5f, 0.55f, 0.5f, 0.45f, 1.0f, 0.0f, 0.5f, 1.0f, 0.0f }; // EQ on, noise (muted)
  12014. Bytes g;
  12015. g.i32 (0x5c4);
  12016. for (auto v : A)
  12017. g.f32 (v);
  12018. g.zeros (0x5c4 - g.b.size());
  12019. p.bytes (g.b);
  12020. }
  12021. Bytes s;
  12022. s.raw ("SSLF", 4);
  12023. s.i32 ((std::int32_t) p.b.size());
  12024. s.bytes (p.b);
  12025. return s.b;
  12026. }
  12027. void dumpIfAsked (const char* var, const std::vector<std::uint8_t>& blob)
  12028. {
  12029. if (const char* path = std::getenv (var))
  12030. {
  12031. std::ofstream out (path, std::ios::binary);
  12032. out.write (reinterpret_cast<const char*> (blob.data()), (std::streamsize) blob.size());
  12033. }
  12034. }
  12035. } // namespace
  12036. TEST (dx10_engine)
  12037. {
  12038. // REA_rodada42: a Fruity DX10 state written from scratch (version 1: 22 raw parameters) against our reference model
  12039. // (dxmodel.py + dxproc.py, bit-identical to the installation's DLL), the channel processor, host levels and the
  12040. // instrument front-end
  12041. const std::int32_t P[22] = { 1000, 40000, 30000, 30000, 40000, 30000, 35000, 8000, 40000, 50000, 20000,
  12042. 25000, 15000, 30000, 20000, 10000, 20000, 30000, 5000, 30000, 20000, -1 };
  12043. std::vector<std::uint8_t> blob (4 + 22 * 4, 0);
  12044. blob[0] = 1;
  12045. std::memcpy (blob.data() + 4, P, sizeof (P));
  12046. CHECK (lynx::dx10StateUsable (blob) && lynx::dx10ProcessorNeutral (blob));
  12047. CHECK (lynx::dx10Param (blob, 21) == -1 && lynx::dx10Param (blob, 3) == 30000 && ! lynx::dx10Param (blob, 22));
  12048. CHECK (! lynx::dx10StateUsable (std::vector<std::uint8_t> (40, 0)));
  12049. {
  12050. auto bad = blob;
  12051. bad[0] = 3; // versions ≥ 3 are ignored by the plugin
  12052. CHECK (! lynx::dx10StateUsable (bad));
  12053. }
  12054. using N = lynx::dx10::RefNote;
  12055. 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 } };
  12056. struct Golden { std::vector<std::uint8_t> state; lynx::dx10::RefOptions opt; int rate; double el, er; std::tuple<int, float, float> at[6]; };
  12057. auto check = [&] (const Golden& g) {
  12058. const auto y = lynx::dx10::renderReference (g.state, notes, (double) g.rate, 16384, g.opt);
  12059. CHECK (y.size() == 2 * 16384);
  12060. if (y.size() != 2 * 16384)
  12061. return y;
  12062. double el = 0.0, er = 0.0;
  12063. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  12064. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  12065. CHECK (std::abs (el - g.el) <= 1e-7 * g.el && std::abs (er - g.er) <= 1e-7 * g.er);
  12066. for (const auto& [i, l, r] : g.at)
  12067. CHECK (goldenSampleClose (y[2 * (std::size_t) i], l, libmTolerance (1e-7f))
  12068. && goldenSampleClose (y[2 * (std::size_t) i + 1], r, libmTolerance (1e-7f)));
  12069. return y;
  12070. };
  12071. lynx::dx10::RefOptions plain, odd;
  12072. odd.block = 333, odd.maxPoly = 1;
  12073. check ({ blob, plain, 44100, 672.293115, 778.393071,
  12074. { { 100, -0.0943799689f, -0.154014125f }, { 1000, -0.0814286768f, -0.132879525f }, { 2500, 0.499493301f, 0.312498778f },
  12075. { 5000, -0.184045464f, 0.171641544f }, { 9000, -0.153369367f, -0.20805186f }, { 16383, -0.00441304967f, 0.200126961f } } });
  12076. // 48 kHz, odd host blocks, max polyphony 1 (the last note steals); the hard-right note keeps FL's x87 left gain
  12077. check ({ blob, odd, 48000, 122.232761, 493.320297,
  12078. { { 100, 0.0236493405f, 0.0385922194f }, { 1000, 0.189806491f, 0.309736073f }, { 2500, -0.425576478f, -0.176279545f },
  12079. { 5000, -3.54030076e-18f, -0.0578194074f }, { 9000, 1.37052479e-17f, 0.223831087f }, { 16383, 9.248742e-18f, 0.151048407f } } });
  12080. // the channel processor (§3): our own settings snapshot — pan LFO (triangle, delayed, faded in), volume envelope
  12081. // with tensions and LFO, cutoff envelope + square LFO, resonance LFO, pitch envelope, SVF LP ×2 (type 7)
  12082. auto record = [] (std::vector<std::uint8_t>& s, int k, std::initializer_list<std::pair<int, int>> fields) {
  12083. const std::int32_t neutral[17] = { 0, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, 0 };
  12084. std::memcpy (s.data() + 0x44 * k, neutral, sizeof (neutral));
  12085. for (const auto& [i, v] : fields)
  12086. std::memcpy (s.data() + 0x44 * k + 4 * i, &v, 4);
  12087. };
  12088. auto settings = [] (std::vector<std::uint8_t>& s, std::int32_t modX, std::int32_t modY, std::int32_t type) {
  12089. std::memcpy (s.data() + 0x15c, &modX, 4);
  12090. std::memcpy (s.data() + 0x160, &modY, 4);
  12091. std::memcpy (s.data() + 0x168, &type, 4);
  12092. };
  12093. std::vector<std::uint8_t> snap (364, 0);
  12094. record (snap, 0, { { 9, 5000 }, { 10, 9000 }, { 11, -40 }, { 12, 15000 }, { 13, 1 } });
  12095. 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 } });
  12096. record (snap, 2, { { 1, 1 }, { 3, 6000 }, { 4, 100 }, { 5, 16000 }, { 6, 20 }, { 8, -60 }, { 11, 20 }, { 12, 18000 }, { 13, 2 } });
  12097. record (snap, 3, { { 11, 40 }, { 12, 17000 } });
  12098. record (snap, 4, { { 1, 1 }, { 3, 5000 }, { 4, 100 }, { 5, 14000 }, { 6, 30 }, { 8, -700 } });
  12099. settings (snap, 140, 120, 7);
  12100. const auto withSnap = lynx::dx10StateWithSnapshot (blob, snap);
  12101. CHECK (withSnap.size() == 460 && withSnap[0] == 2 && ! lynx::dx10ProcessorNeutral (withSnap));
  12102. CHECK (lynx::dx10StateWithSnapshot (withSnap, std::vector<std::uint8_t> (364, 0)) == withSnap); // a v2 state keeps its own
  12103. lynx::dx10::RefOptions ticks;
  12104. ticks.tick = 230, ticks.ppq = 96, ticks.tempo = 120.0;
  12105. check ({ withSnap, ticks, 44100, 346.852062, 554.699375,
  12106. { { 100, -0.00162654708f, -0.00265428401f }, { 1000, 0.0310285408f, 0.0506339073f }, { 2500, -0.118068494f, -0.0915558636f },
  12107. { 5000, -0.297166437f, -0.256465197f }, { 9000, 0.199509233f, 0.166108996f }, { 16383, -0.00392446062f, 0.0410436988f } } });
  12108. // a volume envelope decaying to zero: the 10 ms end fade, then every voice is gone (silence from 14950 on)
  12109. std::vector<std::uint8_t> decay (364, 0);
  12110. for (int k = 0; k < 5; ++k)
  12111. record (decay, k, {});
  12112. record (decay, 1, { { 0, 4 }, { 1, 1 }, { 3, 9000 }, { 4, 100 }, { 5, 12000 }, { 6, 0 }, { 15, 50 }, { 16, -101 } });
  12113. settings (decay, 256, 0, 0);
  12114. auto ticks48 = ticks;
  12115. ticks48.block = 333;
  12116. const auto y = check ({ lynx::dx10StateWithSnapshot (blob, decay), ticks48, 48000, 338.361707, 350.520208,
  12117. { { 100, 0.000514116196f, 0.000838961045f }, { 1000, 0.0412623473f, 0.0673340634f }, { 2500, -0.141532451f, -0.156581759f },
  12118. { 5000, 0.382550895f, 0.23462519f }, { 7000, -0.189829409f, 0.144132584f }, { 9000, -0.13638559f, 0.142912149f } } });
  12119. bool silentTail = y.size() == 2 * 16384 && (y[2 * 14949] != 0.0f || y[2 * 14949 + 1] != 0.0f);
  12120. for (std::size_t i = 2 * 14950; i < y.size(); ++i)
  12121. silentTail = silentTail && y[i] == 0.0f;
  12122. CHECK (silentTail);
  12123. // the instrument: LYNX velocity v → Init.Vol = (11^(1.28·v) − 1)/10 and Final.Vol = channel volume × Init.Vol; channel
  12124. // gains √2·V·(cos, sin)((1 + p)π/4) → V, p; notes on block boundaries play like the reference renderer (neutral
  12125. // channel settings: the instrument's sequencer ticks change nothing; vibrato off, since the plugin-wide vibrato
  12126. // oscillator is stepped by each voice in turn, so its interleaving follows the host block layout, as in FL)
  12127. auto noVib = blob;
  12128. CHECK (lynx::dx10SetParam (noVib, 10, 0));
  12129. auto initVol = [] (double v) { return (double) (float) ((std::pow (11.0, 1.28 * v) - 1.0) / 10.0); }; // Init.Vol is a float
  12130. lynx::ChannelDef c;
  12131. c.instrument = lynx::InstrumentKind::dx10;
  12132. c.pluginState = noVib;
  12133. auto inst = lynx::makeInstrument (lynx::InstrumentKind::dx10);
  12134. inst->configure (c, 120.0);
  12135. inst->prepare (44100.0);
  12136. const float V = 0.6f, pan = 0.25f;
  12137. const auto gl = (float) (std::sqrt (2.0) * V * std::cos ((1.0 + pan) * 3.141592653589793 / 4.0));
  12138. const auto gr = (float) (std::sqrt (2.0) * V * std::sin ((1.0 + pan) * 3.141592653589793 / 4.0));
  12139. std::vector<float> l (16384), r (16384);
  12140. for (int s = 0; s < 16384; s += 512)
  12141. {
  12142. if (s == 0)
  12143. inst->noteOn (60, 0.78125f);
  12144. if (s == 2048)
  12145. {
  12146. inst->setNextNoteExpression (-0.5f, 25.0f);
  12147. inst->noteOn (67, 0.5f);
  12148. }
  12149. if (s == 6144)
  12150. inst->noteOff (60);
  12151. if (s == 8192)
  12152. inst->noteOff (67);
  12153. inst->render (l.data() + s, r.data() + s, 512, gl, gr);
  12154. }
  12155. // the channel level reaches the engine as FL's (recovered to float precision)
  12156. const auto Vr = std::sqrt (((double) gl * gl + (double) gr * gr) * 0.5);
  12157. const auto pr = std::atan2 ((double) gr, (double) gl) * (4.0 / 3.141592653589793) - 1.0;
  12158. const auto ref = lynx::dx10::renderReference (noVib, { { 0, 60, initVol (0.78125), Vr * initVol (0.78125), pr, 0.0, 6144 },
  12159. { 2048, 67, initVol (0.5), Vr * initVol (0.5), std::clamp (-0.5 + pr, -1.0, 1.0), 25.0, 8192 } },
  12160. 44100.0, 16384);
  12161. double worst = 0.0, peak = 0.0;
  12162. for (int i = 0; i < 16384; ++i)
  12163. 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]) }),
  12164. peak = std::max (peak, (double) std::abs (l[(std::size_t) i]));
  12165. CHECK (worst == 0.0 && peak > 0.05);
  12166. // the released voices end (DX10's own release) and the instrument goes idle
  12167. std::vector<float> tail (44100), tailR (tail.size());
  12168. for (int k = 0; k < 20 && ! inst->idle(); ++k)
  12169. inst->render (tail.data(), tailR.data(), (int) tail.size(), gl, gr);
  12170. CHECK (inst->idle());
  12171. // with the channel settings above the instrument follows FL's ticks: the cutoff envelope darkens the note
  12172. {
  12173. lynx::ChannelDef cs = c;
  12174. cs.pluginState = withSnap;
  12175. cs.modelParams = { 96.0f };
  12176. auto si = lynx::makeInstrument (lynx::InstrumentKind::dx10);
  12177. si->configure (cs, 120.0);
  12178. si->prepare (44100.0);
  12179. std::vector<float> sl (32768), sr (32768);
  12180. si->noteOn (48, 0.78125f);
  12181. for (int s = 0; s < 32768; s += 512)
  12182. si->render (sl.data() + s, sr.data() + s, 512, 1.0f, 1.0f);
  12183. double e1 = 0.0, e2 = 0.0;
  12184. bool finite = true;
  12185. for (int i = 0; i < 32768; ++i)
  12186. finite = finite && std::isfinite (sl[(std::size_t) i]), (i < 4096 ? e1 : e2) += (double) sl[(std::size_t) i] * sl[(std::size_t) i];
  12187. CHECK (finite && e1 > 1e-3 && e2 > 1e-3);
  12188. }
  12189. // parameter edits (the channel settings panel) write the raw integers, clamped to the knob ranges
  12190. auto edited = blob;
  12191. CHECK (lynx::dx10SetParam (edited, 21, 7) && lynx::dx10Param (edited, 21) == 3);
  12192. CHECK (lynx::dx10SetParam (edited, 12, 70000) && lynx::dx10Param (edited, 12) == 65536);
  12193. }
  12194. TEST (threeosc_engine)
  12195. {
  12196. // REA_rodada47: a 3x Osc state written from scratch (version 14) against our reference model (osc3model.py,
  12197. // bit-identical to the installation's DLL), the plugin voice processor with its release path, the instrument
  12198. 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 };
  12199. 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) {
  12200. std::vector<std::uint8_t> st (0x60, 0);
  12201. const std::int32_t version = 14;
  12202. std::memcpy (st.data(), &version, 4);
  12203. std::memcpy (st.data() + 4, p, 21 * 4);
  12204. st[0x58] = inv[0], st[0x59] = inv[1], st[0x5a] = inv[2], st[0x5b] = am;
  12205. st.insert (st.end(), snap.begin(), snap.end());
  12206. return st;
  12207. };
  12208. auto record = [] (std::vector<std::uint8_t>& s, int k, std::initializer_list<std::pair<int, int>> fields) {
  12209. const std::int32_t neutral[17] = { 0, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, 0 };
  12210. std::memcpy (s.data() + 0x44 * k, neutral, sizeof (neutral));
  12211. for (const auto& [i, v] : fields)
  12212. std::memcpy (s.data() + 0x44 * k + 4 * i, &v, 4);
  12213. };
  12214. auto settings = [] (std::vector<std::uint8_t>& s, std::int32_t modX, std::int32_t modY, std::int32_t type) {
  12215. std::memcpy (s.data() + 0x15c, &modX, 4);
  12216. std::memcpy (s.data() + 0x160, &modY, 4);
  12217. std::memcpy (s.data() + 0x168, &type, 4);
  12218. };
  12219. std::vector<std::uint8_t> neutral (364, 0);
  12220. for (int k = 0; k < 5; ++k)
  12221. record (neutral, k, k == 1 ? std::initializer_list<std::pair<int, int>> { { 0, 4 }, { 16, -101 } } : std::initializer_list<std::pair<int, int>> {});
  12222. settings (neutral, 256, 0, 0);
  12223. const auto blob = make (P, { 0, 0, 0 }, 0, neutral);
  12224. CHECK (lynx::threeOscStateUsable (blob) && lynx::threeOscNormalisedState (blob, {}) == blob);
  12225. CHECK (lynx::threeOscParam (blob, 9) == -12 && lynx::threeOscParam (blob, 20) == 16 && lynx::threeOscParam (blob, 24) == 0 && ! lynx::threeOscParam (blob, 26));
  12226. // older versions are normalised like the plugin reads them: version 12 (no snapshot → the plugin's defaults),
  12227. // version 10 (20 parameters, no invert/AM bytes)
  12228. {
  12229. std::vector<std::uint8_t> v12 (blob.begin(), blob.begin() + 92);
  12230. v12[0] = 12;
  12231. v12[0x58] = 1;
  12232. const auto n12 = lynx::threeOscNormalisedState (v12, {});
  12233. CHECK (n12.size() == 460 && n12[0] == 14 && lynx::threeOscParam (n12, 21) == 1 && lynx::threeOscParam (n12, 20) == 16
  12234. && std::equal (n12.begin() + 0x60, n12.end(), neutral.begin()));
  12235. std::vector<std::uint8_t> v10 (blob.begin(), blob.begin() + 84);
  12236. v10[0] = 10;
  12237. const auto n10 = lynx::threeOscNormalisedState (v10, {});
  12238. CHECK (lynx::threeOscParam (n10, 20) == 0 && lynx::threeOscParam (n10, 13) == 40 && lynx::threeOscParam (n10, 21) == 0);
  12239. }
  12240. using N = lynx::threeosc::RefNote;
  12241. 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 } };
  12242. struct Golden { std::vector<std::uint8_t> state; lynx::threeosc::RefOptions opt; int rate; double el, er; std::tuple<int, float, float> at[6]; };
  12243. auto check = [&] (const Golden& g) {
  12244. const auto y = lynx::threeosc::renderReference (g.state, notes, (double) g.rate, 16384, g.opt);
  12245. CHECK (y.size() == 2 * 16384);
  12246. if (y.size() != 2 * 16384)
  12247. return y;
  12248. double el = 0.0, er = 0.0;
  12249. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  12250. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  12251. CHECK (std::abs (el - g.el) <= 1e-7 * g.el && std::abs (er - g.er) <= 1e-7 * g.er);
  12252. for (const auto& [i, l, r] : g.at)
  12253. CHECK (goldenSampleClose (y[2 * (std::size_t) i], l, libmTolerance (1e-7f))
  12254. && goldenSampleClose (y[2 * (std::size_t) i + 1], r, libmTolerance (1e-7f)));
  12255. return y;
  12256. };
  12257. // saw / square / triangle-ish mix with pans, stereo detune and phase, random start phases (seed 12345)
  12258. lynx::threeosc::RefOptions plain;
  12259. plain.seed = 12345;
  12260. check ({ blob, plain, 44100, 681.6491972612481, 1509.7245664533937,
  12261. { { 100, 0.0674247071146965f, -0.13440212607383728f }, { 1000, 0.12890009582042694f, 0.446039617061615f },
  12262. { 2500, -0.007686644792556763f, 0.3574123680591583f }, { 5000, 0.08792756497859955f, 0.4948147237300873f },
  12263. { 9000, 1.2136793475613885e-17f, 0.3116455078125f }, { 16383, 5.7305530145831346e-18f, 0.4134995639324188f } } });
  12264. // AM (osc 3 noise modulating osc 1 + 2), inverted oscillators, 48 kHz, odd blocks, max polyphony 1
  12265. 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 };
  12266. lynx::threeosc::RefOptions odd;
  12267. odd.block = 333, odd.maxPoly = 1, odd.seed = 777;
  12268. check ({ make (Pam, { 1, 0, 1 }, 1, neutral), odd, 48000, 453.27241060751771, 1996.4548848690094,
  12269. { { 100, -0.2589033246040344f, -0.310184121131897f }, { 1000, 0.22021546959877014f, 0.3591070771217346f },
  12270. { 2500, -0.5378497242927551f, -0.15602676570415497f }, { 5000, 1.2423200626807308e-17f, 0.3293072581291199f },
  12271. { 9000, 2.5857780099332177e-17f, 0.42474493384361267f }, { 16383, 2.941168384618868e-17f, 0.3569665551185608f } } });
  12272. // the channel processor with its release path: pan LFO, volume envelope released from its current value, cutoff
  12273. // envelope + square LFO, resonance LFO, pitch envelope, SVF LP ×2 (type 7)
  12274. std::vector<std::uint8_t> snap (364, 0);
  12275. record (snap, 0, { { 9, 5000 }, { 10, 9000 }, { 11, -40 }, { 12, 15000 }, { 13, 1 } });
  12276. 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 } });
  12277. record (snap, 2, { { 1, 1 }, { 3, 6000 }, { 4, 100 }, { 5, 16000 }, { 6, 20 }, { 7, 9000 }, { 8, -60 }, { 11, 20 }, { 12, 18000 }, { 13, 2 } });
  12278. record (snap, 3, { { 11, 40 }, { 12, 17000 } });
  12279. record (snap, 4, { { 1, 1 }, { 3, 5000 }, { 4, 100 }, { 5, 14000 }, { 6, 30 }, { 7, 12000 }, { 8, -700 } });
  12280. settings (snap, 140, 120, 7);
  12281. lynx::threeosc::RefOptions ticks;
  12282. ticks.tick = 230, ticks.ppq = 96, ticks.tempo = 120.0, ticks.seed = 99;
  12283. check ({ make (P, { 0, 0, 0 }, 0, snap), ticks, 44100, 977.9928196811321, 2072.965349921462,
  12284. { { 100, -0.001351095037534833f, 0.0029758031014353037f }, { 1000, 0.002833257894963026f, 0.10575854033231735f },
  12285. { 2500, 0.020360738039016724f, 0.3181821405887604f }, { 5000, 0.3340669274330139f, 0.4912331700325012f },
  12286. { 9000, 0.26940545439720154f, 0.27708977460861206f }, { 16383, 0.16570518910884857f, -0.10794822871685028f } } });
  12287. auto ticks48 = ticks;
  12288. ticks48.block = 333, ticks48.tick = 250, ticks48.seed = 5;
  12289. // (at 48 kHz the library's rate ratio 44100/fs scales the filter coefficients and the ramp limit)
  12290. check ({ make (P, { 0, 1, 0 }, 0, snap), ticks48, 48000, 864.9859578029492, 1895.6281818565287,
  12291. { { 100, 0.0031638722866773605f, -0.001987025374546647f }, { 1000, 0.049239177256822586f, 0.08794696629047394f },
  12292. { 2500, 0.13807585835456848f, 0.3554498553276062f }, { 5000, -0.08793184161186218f, -0.1596519947052002f },
  12293. { 9000, 0.08728396147489548f, 0.540290117263794f }, { 16383, 0.039887525141239166f, 0.21270982921123505f } } });
  12294. // HQ oscillators (engine_x64.dll: band-limited tables built with an FFT; FL's single-precision FFT and LYNX's double
  12295. // one agree to float rounding, so the golden values hold to 1e-5)
  12296. {
  12297. 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 };
  12298. auto hq = make (Phq, { 0, 0, 0 }, 0, neutral);
  12299. hq[0x5c] = 1;
  12300. CHECK (lynx::threeOscParam (hq, 25) == 1);
  12301. lynx::threeosc::RefOptions o;
  12302. o.seed = 3;
  12303. const auto y = lynx::threeosc::renderReference (hq, notes, 44100.0, 16384, o);
  12304. CHECK (y.size() == 2 * 16384);
  12305. if (y.size() == 2 * 16384)
  12306. {
  12307. double el = 0.0, er = 0.0;
  12308. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  12309. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  12310. CHECK (std::abs (el - 862.7912087773848) < 1e-5 * 862.8 && std::abs (er - 1232.8046929286115) < 1e-5 * 1232.8);
  12311. const std::tuple<int, float, float> at[] = { { 100, -0.019788391888141632f, -0.19209949672222137f }, { 1000, 0.12019827216863632f, 0.03852222114801407f },
  12312. { 2500, 0.3239150643348694f, 0.07325324416160583f }, { 5000, -0.2809634208679199f, -0.5693862438201904f },
  12313. { 16383, -1.1937656360586644e-17f, -0.061853960156440735f } };
  12314. for (const auto& [i, l, r] : at)
  12315. CHECK (std::abs (y[2 * (std::size_t) i] - l) < 1e-5f && std::abs (y[2 * (std::size_t) i + 1] - r) < 1e-5f);
  12316. }
  12317. }
  12318. // the instrument: Final.Vol = channel volume × Init.Vol, channel gains → FL's volume/pan; notes on block boundaries
  12319. // play like the reference renderer (seed 0, neutral channel settings: ticks change nothing)
  12320. auto initVol = [] (double v) { return (double) (float) ((std::pow (11.0, 1.28 * v) - 1.0) / 10.0); };
  12321. lynx::ChannelDef c;
  12322. c.instrument = lynx::InstrumentKind::threeosc;
  12323. c.pluginState = blob;
  12324. auto inst = lynx::makeInstrument (lynx::InstrumentKind::threeosc);
  12325. inst->configure (c, 120.0);
  12326. inst->prepare (44100.0);
  12327. const float V = 0.6f, pan = 0.25f;
  12328. const auto gl = (float) (std::sqrt (2.0) * V * std::cos ((1.0 + pan) * 3.141592653589793 / 4.0));
  12329. const auto gr = (float) (std::sqrt (2.0) * V * std::sin ((1.0 + pan) * 3.141592653589793 / 4.0));
  12330. std::vector<float> l (16384), r (16384);
  12331. for (int s = 0; s < 16384; s += 512)
  12332. {
  12333. if (s == 0)
  12334. inst->noteOn (60, 0.78125f);
  12335. if (s == 2048)
  12336. {
  12337. inst->setNextNoteExpression (-0.5f, 25.0f);
  12338. inst->noteOn (67, 0.5f);
  12339. }
  12340. if (s == 6144)
  12341. inst->noteOff (60);
  12342. if (s == 8192)
  12343. inst->noteOff (67);
  12344. inst->render (l.data() + s, r.data() + s, 512, gl, gr);
  12345. }
  12346. const auto Vr = std::sqrt (((double) gl * gl + (double) gr * gr) * 0.5);
  12347. const auto pr = std::atan2 ((double) gr, (double) gl) * (4.0 / 3.141592653589793) - 1.0;
  12348. const auto ref = lynx::threeosc::renderReference (blob, { { 0, 60, Vr * initVol (0.78125), pr, 0.0, 6144 },
  12349. { 2048, 67, Vr * initVol (0.5), std::clamp (-0.5 + pr, -1.0, 1.0), 25.0, 8192 } },
  12350. 44100.0, 16384);
  12351. double worst = 0.0, peak = 0.0;
  12352. for (int i = 0; i < 16384; ++i)
  12353. 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]) }),
  12354. peak = std::max (peak, (double) std::abs (l[(std::size_t) i]));
  12355. CHECK (worst == 0.0 && peak > 0.05);
  12356. // released notes end with the processor's 10 ms fade (volume envelope off) and the instrument goes idle
  12357. CHECK (inst->idle());
  12358. // parameter edits write the raw integers, clamped to the knob ranges
  12359. auto edited = blob;
  12360. CHECK (lynx::threeOscSetParam (edited, 9, -40) && lynx::threeOscParam (edited, 9) == -24);
  12361. CHECK (lynx::threeOscSetParam (edited, 13, 500) && lynx::threeOscParam (edited, 13) == 128);
  12362. CHECK (lynx::threeOscSetParam (edited, 24, 1) && edited[0x5b] == 1);
  12363. CHECK (! lynx::threeOscStateUsable (std::vector<std::uint8_t> (2, 0)));
  12364. }
  12365. TEST (gms_engine)
  12366. {
  12367. const auto blob = gmsTestState();
  12368. dumpIfAsked ("LYNX_DUMP_GMS", blob);
  12369. CHECK (lynx::gmsStateUsable (blob));
  12370. CHECK (lynx::gmsWaveNames (blob) == std::vector<std::string> ({ "NoWave A", "NoWave B", "NoWave C" }));
  12371. CHECK (lynx::gmsParamOffset (blob, 12) == 8 + 4 + 7 * 512 + 12 + 4 * 12 && lynx::gmsParamOffset (blob, 212) < 0);
  12372. auto broken = blob;
  12373. broken[8] = 'X';
  12374. CHECK (! lynx::gmsStateUsable (broken));
  12375. CHECK (! lynx::gmsStateUsable (std::vector<std::uint8_t> (blob.begin(), blob.begin() + 2000)));
  12376. // golden samples of our round-39 model (gmsmodel.GmsPlugin, waves missing → silent tables, tempo 140, 44.1 kHz,
  12377. // host blocks of 512): C5 vel 100 0–30000, E5 vel 80 11025–40000, G5 vel 110 22050–52000 (Init.Vol law A)
  12378. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  12379. const std::vector<lynx::gms::RefNote> notes = { { 0, 60, (float) iv (100), 1.0, 0.0, 0.0, 30000 },
  12380. { 11025, 64, (float) iv (80), 1.0, 0.0, 0.0, 40000 },
  12381. { 22050, 67, (float) iv (110), 1.0, 0.0, 0.0, 52000 } };
  12382. const auto y = lynx::gms::renderReference (blob, notes, 44100.0, 66150, "");
  12383. CHECK (y.size() == 2 * 66150u);
  12384. 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 } };
  12385. for (const auto& g : golden)
  12386. 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);
  12387. double e = 0.0;
  12388. for (auto v : y)
  12389. e += (double) v * v;
  12390. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.0790270452795) < 1e-6);
  12391. // the instrument: same engine behind LYNX's channel gains (V = 1, centre → law A), velocity → Init.Vol
  12392. lynx::ChannelDef c;
  12393. c.instrument = lynx::InstrumentKind::gms;
  12394. c.pluginState = blob;
  12395. auto inst = lynx::makeInstrument (lynx::InstrumentKind::gms);
  12396. inst->configure (c, 140.0);
  12397. inst->prepare (44100.0);
  12398. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  12399. for (int b = 0; b < 16; ++b)
  12400. {
  12401. if (b == 0)
  12402. inst->noteOn (60, 0.78125f);
  12403. if (b == 10)
  12404. inst->noteOff (60);
  12405. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  12406. }
  12407. 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, "");
  12408. double diff = 0.0, energy = 0.0;
  12409. for (int i = 0; i < 8192; ++i)
  12410. {
  12411. 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]) });
  12412. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  12413. }
  12414. CHECK (diff == 0.0 && energy > 1e-3);
  12415. }
  12416. // A Toxic Biohazard state written from scratch (REA_rodada45; r45/py/mktest.py builds the same bytes): our own drawn
  12417. // spectra on operators 1 and 3 (wave 0) and the sine on operator 2, so no wave of the installation is needed
  12418. std::vector<std::uint8_t> toxicTestState()
  12419. {
  12420. auto s = lynx::toxicDefaultState();
  12421. static const std::pair<int, double> set[] = {
  12422. { 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 },
  12423. { 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 },
  12424. { 24, 0.6 }, { 25, 1 }, { 26, 1 }, { 27, 1 }, { 32, 0.5606 }, { 33, 0.4394 }, { 38, 0.53 }, { 49, 0 }, { 55, 0.25 },
  12425. { 62, 0.5 }, { 75, 0.08 }, { 81, 0.5 }, { 87, 0.3 }, { 104, 0.8 }, { 105, 0.6 }, { 110, 0.35 }, { 115, 0.22 },
  12426. { 127, 0.18 }, { 147, 0.7 }, { 227, 1 }, { 229, 0.65 }, { 233, 0.4 }, { 236, 1 }, { 237, 1 }, { 252, 0.5 }, { 254, 0.3 },
  12427. { 256, 0.4 }, { 267, 0.4 }, { 269, 0.5 }, { 277, 0.3 }, { 300, 1 }, { 301, 8 }, { 302, 2 }, { 303, 0 }, { 305, 3 },
  12428. { 306, 0 }, { 307, 1 }, { 308, 0 }, { 314, 1 }, { 315, 0 }, { 316, 1 }, { 324, 106 }, { 332, 0.7 } };
  12429. for (const auto& [index, value] : set)
  12430. {
  12431. const auto v = (float) value;
  12432. std::memcpy (s.data() + lynx::toxicParamOffset (s, index), &v, 4);
  12433. }
  12434. auto spectrum = [&] (int slot, int h, float amp, float phase) {
  12435. const auto at = 12 + 0x40 + 1024 * (std::size_t) slot + 4 * (std::size_t) h;
  12436. std::memcpy (s.data() + at, &amp, 4);
  12437. std::memcpy (s.data() + at + 512, &phase, 4);
  12438. };
  12439. for (int h = 0; h < 24; ++h)
  12440. spectrum (0, h, (float) (1.0 / (h + 1)), 0.0f);
  12441. for (int k = 0; k < 16; ++k)
  12442. spectrum (2, 2 * k, (float) (1.0 / (2 * k + 1)), 0.25f);
  12443. return s;
  12444. }
  12445. TEST (toxic_engine)
  12446. {
  12447. const auto blob = toxicTestState();
  12448. dumpIfAsked ("LYNX_DUMP_TOXIC", blob);
  12449. CHECK (blob.size() == 10050u && lynx::toxicStateUsable (blob) && ! lynx::toxicNeedsDll (blob));
  12450. CHECK (lynx::toxicParamOffset (blob, 15) == 12 + 0x40 + 8192 + 4 * 15 && lynx::toxicParamOffset (blob, 340) < 0);
  12451. CHECK (! lynx::toxicDllUsable ("") && ! lynx::toxicDllUsable ("/nonexistent/Toxic Biohazard_x64.dll"));
  12452. auto broken = blob;
  12453. broken[8] = 'X';
  12454. CHECK (! lynx::toxicStateUsable (broken));
  12455. CHECK (! lynx::toxicStateUsable (std::vector<std::uint8_t> (blob.begin(), blob.begin() + 5000)));
  12456. auto needs = blob;
  12457. const auto w = 15.0f; // a built-in wave of the installation: without the DLL the engine refuses the state
  12458. std::memcpy (needs.data() + lynx::toxicParamOffset (needs, 307), &w, 4);
  12459. CHECK (lynx::toxicNeedsDll (needs) && lynx::toxic::renderReference (needs, { { 0, 60 } }, 44100.0, 64, "").empty());
  12460. // golden samples of our round-45 model (toxmodel.run, which equals the DLL bit for bit on this state), effects on,
  12461. // tempo 140, 44.1 kHz, host blocks of 512: C5 vel 100 0–30000, E5 vel 80 11025–40000, G5 vel 110 22050–52000
  12462. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  12463. const std::vector<lynx::toxic::RefNote> notes = { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 },
  12464. { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  12465. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 } };
  12466. const auto y = lynx::toxic::renderReference (blob, notes, 44100.0, 66150, "", 512, 140.0);
  12467. CHECK (y.size() == 2 * 66150u);
  12468. 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 } };
  12469. for (const auto& g : golden)
  12470. 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);
  12471. double e = 0.0;
  12472. for (auto v : y)
  12473. e += (double) v * v;
  12474. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.307227390984) < 1e-6);
  12475. // the instrument: same engine behind LYNX's channel gains (V = 1, centre → law A), velocity → Init.Vol
  12476. lynx::ChannelDef c;
  12477. c.instrument = lynx::InstrumentKind::toxic;
  12478. c.pluginState = blob;
  12479. auto inst = lynx::makeInstrument (lynx::InstrumentKind::toxic);
  12480. inst->configure (c, 140.0);
  12481. inst->prepare (44100.0);
  12482. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  12483. for (int b = 0; b < 16; ++b)
  12484. {
  12485. if (b == 0)
  12486. inst->noteOn (60, 0.78125f);
  12487. if (b == 10)
  12488. inst->noteOff (60);
  12489. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  12490. }
  12491. 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 } },
  12492. 44100.0, 8192, "", 512, 140.0);
  12493. double diff = 0.0, energy = 0.0;
  12494. for (int i = 0; i < 8192; ++i)
  12495. {
  12496. 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]) });
  12497. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  12498. }
  12499. CHECK (diff == 0.0 && energy > 1e-3);
  12500. }
  12501. TEST (morphine_engine)
  12502. {
  12503. const auto blob = morphineTestState();
  12504. dumpIfAsked ("LYNX_DUMP_MORPHINE", blob);
  12505. CHECK (lynx::morphineStateUsable (blob));
  12506. CHECK (lynx::morphineNoiseName (blob).empty());
  12507. const auto at = lynx::morphineParamOffset (blob, 9);
  12508. CHECK (at > 0);
  12509. float a9 = 0.0f;
  12510. std::memcpy (&a9, blob.data() + at, 4);
  12511. CHECK (a9 == 0.2f && lynx::morphineParamOffset (blob, 40) < 0);
  12512. auto broken = blob;
  12513. broken[12] = 'X'; // the MPR0 tag
  12514. CHECK (! lynx::morphineStateUsable (broken));
  12515. CHECK (! lynx::morphineStateUsable (std::vector<std::uint8_t> (blob.begin(), blob.end() - 10)));
  12516. // golden samples of our round-39 model (mormodel.Synth, tempo 120, 44.1 kHz, host blocks of 512): keys 60/64/67,
  12517. // Init.Vol 1.0/0.5/2.0, note-offs at 30000/50000/70000
  12518. const std::vector<lynx::morphine::RefNote> notes = { { 0, 60, 1.0, 1.0, 0.0, 0.0, 30000 },
  12519. { 11025, 64, 0.5, 1.0, 0.0, 0.0, 50000 },
  12520. { 22050, 67, 2.0, 1.0, 0.0, 0.0, 70000 } };
  12521. const auto y = lynx::morphine::renderReference (blob, notes, 44100.0, 80000, "");
  12522. CHECK (y.size() == 2 * 80000u);
  12523. 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 } };
  12524. for (const auto& g : golden)
  12525. 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);
  12526. double e = 0.0;
  12527. for (auto v : y)
  12528. e += (double) v * v;
  12529. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.18467099041) < 1e-6);
  12530. // the instrument: same engine behind LYNX's channel gains
  12531. lynx::ChannelDef c;
  12532. c.instrument = lynx::InstrumentKind::morphine;
  12533. c.pluginState = blob;
  12534. auto inst = lynx::makeInstrument (lynx::InstrumentKind::morphine);
  12535. inst->configure (c, 120.0);
  12536. inst->prepare (44100.0);
  12537. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  12538. for (int b = 0; b < 16; ++b)
  12539. {
  12540. if (b == 0)
  12541. inst->noteOn (60, 0.78125f);
  12542. if (b == 10)
  12543. inst->noteOff (60);
  12544. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  12545. }
  12546. 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, "");
  12547. double diff = 0.0, energy = 0.0;
  12548. for (int i = 0; i < 8192; ++i)
  12549. {
  12550. 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]) });
  12551. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  12552. }
  12553. CHECK (diff == 0.0 && energy > 1e-3);
  12554. }
  12555. namespace
  12556. {
  12557. // A MiniSynth state written from scratch (REA_rodada41 §1.1 layout: "SSLF" + the .flms preset)
  12558. std::vector<std::uint8_t> miniSynthTestState (const std::array<float, 43>& P)
  12559. {
  12560. const std::string name = "LYNX test";
  12561. Bytes body;
  12562. body.raw ("\x99" "FLS\r\n\x1a\n", 8);
  12563. body.raw ("HEAD", 4);
  12564. body.i32 (6 + 2 * (std::int32_t) name.size());
  12565. const std::uint16_t head[3] = { 1, 1, (std::uint16_t) name.size() };
  12566. body.raw (reinterpret_cast<const char*> (head), 6);
  12567. for (char ch : name)
  12568. body.raw (&ch, 1), body.zeros (1);
  12569. body.raw ("DATA", 4);
  12570. body.i32 (176);
  12571. body.raw ("0rev", 4);
  12572. for (auto v : P)
  12573. body.f32 (v);
  12574. Bytes s;
  12575. s.raw ("SSLF", 4);
  12576. s.i32 ((std::int32_t) body.b.size());
  12577. s.bytes (body.b);
  12578. return s.b;
  12579. }
  12580. std::array<float, 43> miniSynthDefaults()
  12581. {
  12582. 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,
  12583. 0.5f, 0, 0.25f, 0.5f, 0.5f, 0, 0.11222222f, 0.4f, 0.1f, 1, 0.5f };
  12584. }
  12585. // per-process suffix for scratch files: several test binaries (one per worktree) may run at the same time, and a
  12586. // fixed name let one process read a file another one was rewriting
  12587. std::string processTempTag()
  12588. {
  12589. static const std::string tag = std::to_string (std::random_device {}()) + "_"
  12590. + std::to_string (std::chrono::steady_clock::now().time_since_epoch().count());
  12591. static const struct Cleanup
  12592. {
  12593. ~Cleanup()
  12594. {
  12595. // every scratch file or folder whose name carries this process's tag (round 173e: the SoundFont, case,
  12596. // missing-file, Crash169g, Natives142 and Rea167l scratch paths are per process too)
  12597. std::error_code ec;
  12598. std::vector<std::filesystem::path> mine;
  12599. for (const auto& e : std::filesystem::directory_iterator (std::filesystem::temp_directory_path (ec), ec))
  12600. if (e.path().filename().string().find (tag) != std::string::npos)
  12601. mine.push_back (e.path());
  12602. for (const auto& p : mine)
  12603. std::filesystem::remove_all (p, ec);
  12604. }
  12605. } cleanup;
  12606. (void) cleanup;
  12607. return tag;
  12608. }
  12609. // a synthetic oscdata.dat (amplitudes 0.3/k, phases −π/2 + 0.001·(k − 1)): the test does not need an FL installation
  12610. std::string miniSynthTestOscdata()
  12611. {
  12612. const auto path = (std::filesystem::temp_directory_path() / ("lynx_test_oscdata_" + processTempTag() + ".dat")).string();
  12613. std::vector<float> d (2048);
  12614. for (int h = 0; h < 1024; ++h)
  12615. d[(std::size_t) h] = (float) (0.3 / (h + 1)), d[1024 + (std::size_t) h] = (float) (-1.5707963267948966 + 0.001 * h);
  12616. std::ofstream out (path, std::ios::binary);
  12617. out.write (reinterpret_cast<const char*> (d.data()), 8192);
  12618. return path;
  12619. }
  12620. } // namespace
  12621. TEST (minisynth_engine)
  12622. {
  12623. auto A = miniSynthDefaults(), B = miniSynthDefaults();
  12624. // A: poly, SQR+SQR, noise, filter env/velocity/keytracking, LFO → cutoff, distortion + decimator, delay, chorus, phaser
  12625. 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 },
  12626. { 12, 0.1f }, { 13, 0.4f }, { 14, 0.3f }, { 15, 0.5f }, { 16, 0.8f }, { 17, 0.6f }, { 18, 0.1f },
  12627. { 19, 0.5f }, { 20, 0.85f }, { 21, 0.05f }, { 22, 0.5f }, { 23, 0.6f }, { 24, 0.4f }, { 25, 1 },
  12628. { 26, 0.3f }, { 27, 0.1f }, { 28, 1 }, { 29, 0.4f }, { 30, 0.5f }, { 31, 0.6f }, { 32, 0.6f },
  12629. { 33, 1 }, { 34, 0.4f }, { 35, 0.6f }, { 36, 0.5f }, { 37, 1 }, { 38, 0.3f }, { 39, 0.6f },
  12630. { 40, 0.5f }, { 41, 0.7f }, { 42, 0.4f } };
  12631. // B: lead (legato glide), BELL, LFO → pitch
  12632. 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 },
  12633. { 17, 0.1f }, { 18, 0.5f }, { 19, 0.6f }, { 20, 0.7f }, { 22, 0.6f }, { 23, 0.5f }, { 24, 0.3f } };
  12634. for (const auto& [i, v] : a)
  12635. A[(std::size_t) i] = v;
  12636. for (const auto& [i, v] : b)
  12637. B[(std::size_t) i] = v;
  12638. const auto blobA = miniSynthTestState (A), blobB = miniSynthTestState (B);
  12639. dumpIfAsked ("LYNX_DUMP_MINISYNTH", blobA);
  12640. CHECK (lynx::miniSynthStateUsable (blobA) && lynx::miniSynthParams (blobA).size() == 43u && lynx::miniSynthParams (blobA)[26] == 0.3f);
  12641. CHECK (lynx::miniSynthParamOffset (blobA, 9) == 8 + 8 + 8 + 6 + 18 + 12 + 4 * 9 && lynx::miniSynthParamOffset (blobA, 43) < 0);
  12642. CHECK (! lynx::miniSynthStateUsable (std::vector<std::uint8_t> (blobA.begin(), blobA.begin() + 20)));
  12643. auto shortData = blobA;
  12644. shortData.resize (shortData.size() - 4 * 40); // only 3 parameters present: the rest keeps the defaults
  12645. CHECK (lynx::miniSynthParams (shortData)[20] == 0.5f && lynx::miniSynthParams (shortData)[1] == 0.5f);
  12646. // golden samples of our round-41 model (msmodel.Plugin on the same synthetic oscdata, 44.1 kHz, host blocks of 512,
  12647. // L = R = Final.Vol): C5 vel 100 0–30000, E5 vel 80 11025–40000, G5 vel 110 22050–52000 (Init.Vol law A)
  12648. const auto osc = miniSynthTestOscdata();
  12649. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  12650. const std::vector<lynx::minisynth::RefNote> notes = { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 },
  12651. { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  12652. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 } };
  12653. struct Golden { int i; float l, r; };
  12654. 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 } };
  12655. 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 } };
  12656. for (int which = 0; which < 2; ++which)
  12657. {
  12658. const auto y = lynx::minisynth::renderReference (which == 0 ? blobA : blobB, notes, 44100.0, 66150, osc);
  12659. CHECK (y.size() == 2 * 66150u);
  12660. if (y.size() != 2 * 66150u)
  12661. continue;
  12662. for (const auto& g : which == 0 ? std::vector (std::begin (goldenA), std::end (goldenA)) : std::vector (std::begin (goldenB), std::end (goldenB)))
  12663. CHECK (y[2 * (std::size_t) g.i] == g.l && y[2 * (std::size_t) g.i + 1] == g.r);
  12664. double e = 0.0;
  12665. for (auto v : y)
  12666. e += (double) v * v;
  12667. CHECK (std::abs (std::sqrt (e / (double) y.size()) - (which == 0 ? 0.315552434392 : 0.03926116287)) < 1e-9);
  12668. }
  12669. // missing oscdata.dat: silent tables, still finite
  12670. const auto silent = lynx::minisynth::renderReference (blobB, notes, 44100.0, 4096, "");
  12671. CHECK (silent.size() == 2 * 4096u && std::all_of (silent.begin(), silent.end(), [] (float v) { return std::isfinite (v) && std::abs (v) < 1.0f; }));
  12672. // the instrument: same engine behind LYNX's channel gains (V = 1, centre → law A), velocity → Init.Vol
  12673. lynx::ChannelDef c;
  12674. c.instrument = lynx::InstrumentKind::minisynth;
  12675. c.pluginState = blobA;
  12676. c.samplePath = osc;
  12677. auto inst = lynx::makeInstrument (lynx::InstrumentKind::minisynth);
  12678. inst->configure (c, 120.0);
  12679. inst->prepare (44100.0);
  12680. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  12681. for (int k = 0; k < 16; ++k)
  12682. {
  12683. if (k == 0)
  12684. inst->noteOn (60, 0.78125f);
  12685. if (k == 10)
  12686. inst->noteOff (60);
  12687. inst->render (l.data() + 512 * k, r.data() + 512 * k, 512, 1.0f, 1.0f);
  12688. }
  12689. 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 } },
  12690. 44100.0, 8192, osc, 512, 120.0, 0, -1.0, true);
  12691. double diff = 0.0, energy = 0.0;
  12692. for (int i = 0; i < 8192; ++i)
  12693. {
  12694. 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]) });
  12695. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  12696. }
  12697. CHECK (diff == 0.0 && energy > 1e-3 && ! inst->idle());
  12698. }
  12699. namespace
  12700. {
  12701. // A PoiZone state written from scratch (REA_rodada44 §1.3 layout, "Ver4" + 156 floats + name): pulse A synced to saw B,
  12702. // noise, envelope to the cutoff, triangle LFO to oscillator A's pitch, 3-voice unison, gate, chorus and tape delay.
  12703. std::vector<std::uint8_t> poizoneTestState()
  12704. {
  12705. std::array<float, 156> P {};
  12706. 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 },
  12707. { 10, 0.8f }, { 11, 0.3f }, { 12, 0.5f }, { 13, 0.4f }, { 14, 0.5f }, { 15, 0.5f }, { 16, 0.5f }, { 17, 0.5f },
  12708. { 18, 0.35f }, { 19, 0.6f }, { 20, 0.5f }, { 21, 0.3f }, { 22, 0.2f }, { 23, 0.6f }, { 24, 0.7f }, { 25, 0.5f },
  12709. { 26, 0.7f }, { 28, 0.6f }, { 29, 0.3f }, { 30, 0.8f }, { 31, 1.0f }, { 32, 0.3f }, { 33, 0.5f }, { 34, 0.5f },
  12710. { 35, 0.3f }, { 37, 1.0f }, { 38, 0.6f }, { 39, 1.0f }, { 40, 0.4f }, { 41, 0.5f }, { 42, 0.5f }, { 43, 0.5f },
  12711. { 47, 0.5f }, { 48, 0.3f }, { 67, 0.2f }, { 68, 0.5f }, { 75, 2.0f }, { 76, 15.0f }, { 77, 1.0f }, { 78, 1.0f },
  12712. { 79, 1.0f } };
  12713. for (const auto& [i, v] : values)
  12714. P[(std::size_t) i] = v;
  12715. for (int i = 49; i < 65; ++i)
  12716. P[(std::size_t) i] = (i - 49) % 3 != 2 ? 1.0f : 0.0f;
  12717. for (int i = 81; i < 155; ++i)
  12718. P[(std::size_t) i] = -1.0f;
  12719. std::vector<std::uint8_t> blob { 'S', 'S', 'L', 'F', 0xb4, 0x02, 0x00, 0x00, '4', 'r', 'e', 'V' };
  12720. blob.resize (12 + 4 * 156 + 64, 0);
  12721. std::memcpy (blob.data() + 12, P.data(), 4 * 156);
  12722. std::memcpy (blob.data() + 12 + 4 * 156, "LYNX test", 9);
  12723. return blob;
  12724. }
  12725. } // namespace
  12726. TEST (poizone_engine)
  12727. {
  12728. const auto blob = poizoneTestState();
  12729. CHECK (lynx::poizoneStateUsable (blob));
  12730. CHECK (lynx::poizoneParamOffset (blob, 18) == 12 + 4 * 18 && lynx::poizoneParamOffset (blob, 156) < 0);
  12731. CHECK (lynx::poizoneParam (blob, 75) == 2.0f && ! lynx::poizoneParam (blob, -1));
  12732. auto broken = blob;
  12733. broken[8] = 'X'; // the "Ver4" tag
  12734. CHECK (! lynx::poizoneStateUsable (broken));
  12735. CHECK (! lynx::poizoneStateUsable (std::vector<std::uint8_t> (blob.begin(), blob.begin() + 10)));
  12736. // golden samples of our round-44 model (pzmodel.Plugin, tempo 120, 44.1 kHz, host blocks of 512, L = R = Final.Vol):
  12737. // C5 vel 100 0–30000, E5 vel 80 11025–40000, G5 vel 110 22050–52000 (Init.Vol = (11^(vel/100) − 1)/10)
  12738. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  12739. const std::vector<lynx::poizone::RefNote> notes = { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 },
  12740. { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  12741. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 } };
  12742. const auto y = lynx::poizone::renderReference (blob, notes, 44100.0, 66150);
  12743. CHECK (y.size() == 2 * 66150u);
  12744. 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 } };
  12745. for (const auto& g : golden)
  12746. 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);
  12747. double e = 0.0;
  12748. for (auto v : y)
  12749. e += (double) v * v;
  12750. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.10990940878400073) < 1e-7);
  12751. // the instrument: same engine behind LYNX's channel gains (V = 1, centre → law A), velocity → Init.Vol
  12752. lynx::ChannelDef c;
  12753. c.instrument = lynx::InstrumentKind::poizone;
  12754. c.pluginState = blob;
  12755. auto inst = lynx::makeInstrument (lynx::InstrumentKind::poizone);
  12756. inst->configure (c, 120.0);
  12757. inst->prepare (44100.0);
  12758. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  12759. for (int b = 0; b < 16; ++b)
  12760. {
  12761. if (b == 0)
  12762. inst->noteOn (60, 0.78125f);
  12763. if (b == 10)
  12764. inst->noteOff (60);
  12765. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  12766. }
  12767. 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,
  12768. 512, 120.0, 0, true);
  12769. double diff = 0.0, energy = 0.0;
  12770. for (int i = 0; i < 8192; ++i)
  12771. {
  12772. 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]) });
  12773. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  12774. }
  12775. CHECK (diff == 0.0 && energy > 1e-3);
  12776. }
  12777. // ---- Fruit Kick / BassDrum (REA_rodada43): synthetic states and data, golden values of our round-43 models ----
  12778. namespace
  12779. {
  12780. std::vector<std::uint8_t> fruitKickStateA()
  12781. {
  12782. const std::int32_t w[8] = { 0, 0, -600, 64, 128, 32, 20, 0 };
  12783. std::vector<std::uint8_t> b (32);
  12784. std::memcpy (b.data(), w, 32);
  12785. return b;
  12786. }
  12787. std::vector<std::uint8_t> fruitKickStateB()
  12788. {
  12789. std::vector<std::uint8_t> b (400, 0);
  12790. const std::int32_t head[7] = { 1, 624, -8, 42, 86, 64, 0 };
  12791. std::memcpy (b.data(), head, sizeof (head));
  12792. std::int32_t recs[5][17];
  12793. for (auto& r : recs)
  12794. {
  12795. const std::int32_t d[17] = { 0, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, 0 };
  12796. std::memcpy (r, d, sizeof (d));
  12797. }
  12798. const std::int32_t pan[17] = { 0, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 3000, 64, 20000, 1, 0, 0, 0 };
  12799. const std::int32_t vol[17] = { 4, -1, 100, 3000, 100, 25000, 64, 15000, 0, 100, 20000, 0, 32950, 0, 0, 40, -101 };
  12800. std::memcpy (recs[0], pan, sizeof (pan));
  12801. std::memcpy (recs[1], vol, sizeof (vol));
  12802. for (int k = 0; k < 5; ++k)
  12803. std::memcpy (b.data() + 32 + 68 * k, recs[k], 68);
  12804. const std::int32_t cut = 256;
  12805. std::memcpy (b.data() + 32 + 0x15c, &cut, 4);
  12806. return b;
  12807. }
  12808. std::string bassDrumTestFolder()
  12809. {
  12810. // our own oscillator table and a pseudo-random 16-bit noise sample, laid out like FL's BassDrum folder
  12811. const auto dir = std::filesystem::temp_directory_path() / ("lynx_bassdrum_test_" + processTempTag());
  12812. std::filesystem::create_directories (dir / "Noise Samples");
  12813. auto wav = [] (const std::filesystem::path& path, const void* data, std::uint32_t bytes, std::uint16_t fmt, std::uint16_t bits) {
  12814. std::ofstream o (path, std::ios::binary);
  12815. auto u32 = [&] (std::uint32_t v) { o.write ((const char*) &v, 4); };
  12816. auto u16 = [&] (std::uint16_t v) { o.write ((const char*) &v, 2); };
  12817. o.write ("RIFF", 4), u32 (36 + bytes), o.write ("WAVEfmt ", 8), u32 (16), u16 (fmt), u16 (1), u32 (44100),
  12818. u32 (44100u * bits / 8), u16 ((std::uint16_t) (bits / 8)), u16 (bits), o.write ("data", 4), u32 (bytes);
  12819. o.write ((const char*) data, bytes);
  12820. };
  12821. std::vector<float> table (1024);
  12822. for (int i = 0; i < 1024; ++i)
  12823. 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));
  12824. wav (dir / "sounddata", table.data(), 4096, 3, 32);
  12825. std::vector<std::int16_t> noise (3000);
  12826. std::uint32_t s = 12345;
  12827. for (auto& v : noise)
  12828. {
  12829. s = (s * 1103515245u + 12345u) & 0x7fffffffu;
  12830. v = (std::int16_t) ((int) ((s >> 8) % 20001u) - 10000);
  12831. }
  12832. wav (dir / "Noise Samples" / "test noise.wav", noise.data(), 6000, 1, 16);
  12833. return dir.string();
  12834. }
  12835. std::vector<std::uint8_t> bassDrumTestState()
  12836. {
  12837. std::vector<std::uint8_t> b (0x50C + 92, 0);
  12838. std::memcpy (b.data(), "SSLF", 4);
  12839. const std::uint32_t size = 0x560;
  12840. std::memcpy (b.data() + 4, &size, 4);
  12841. std::memcpy (b.data() + 8, "2kiK", 4);
  12842. std::memcpy (b.data() + 12, "Test kick", 9);
  12843. const char path[] = "C:/Somewhere/Noise Samples/test noise.wav";
  12844. std::memcpy (b.data() + 268, path, sizeof (path) - 1);
  12845. 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,
  12846. 0.5f, 0.8f, 0.15f, 0.9f, 0.3f, 0.5f, 0.6f, 0.62f, 0.4f, 0.5f, 0.85f };
  12847. std::memcpy (b.data() + 0x50C, P, sizeof (P));
  12848. return b;
  12849. }
  12850. struct Golden
  12851. {
  12852. int i;
  12853. float l, r;
  12854. };
  12855. bool matchesGolden (const std::vector<float>& y, std::initializer_list<Golden> golden, double rms)
  12856. {
  12857. bool ok = true;
  12858. for (const auto& g : golden)
  12859. 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;
  12860. double e = 0.0;
  12861. for (auto v : y)
  12862. e += (double) v * v;
  12863. return ok && std::abs (std::sqrt (e / (double) y.size()) - rms) < 1e-6;
  12864. }
  12865. } // namespace
  12866. TEST (fruitkick_engine)
  12867. {
  12868. const auto A = fruitKickStateA(), B = fruitKickStateB();
  12869. CHECK (lynx::fruitKickStateUsable (A) && lynx::fruitKickStateUsable (B));
  12870. CHECK (! lynx::fruitKickStateUsable (std::vector<std::uint8_t> (A.begin(), A.begin() + 20)));
  12871. CHECK (lynx::fruitKickParam (A, 1) == -600 && lynx::fruitKickParam (B, 0) == 624);
  12872. CHECK (lynx::fruitKickFallbacks (A).empty() && lynx::fruitKickFallbacks (B).empty());
  12873. // a version-0 state takes the channel's envelope records as its snapshot (and becomes version 1)
  12874. std::vector<std::vector<std::uint8_t>> recs;
  12875. for (int k = 0; k < 5; ++k)
  12876. recs.emplace_back (B.begin() + 32 + 68 * k, B.begin() + 32 + 68 * (k + 1));
  12877. const auto withEnv = lynx::fruitKickWithChannelEnvelope (A, recs, 256, 0, 0);
  12878. CHECK (withEnv.size() == 400 && lynx::fruitKickParam (withEnv, 5) == 20 && std::memcmp (withEnv.data() + 32, B.data() + 32, 364) == 0);
  12879. CHECK (lynx::fruitKickWithChannelEnvelope (B, recs, 256, 0, 0) == B);
  12880. // golden samples of our round-43 model (fkmodel.run, 44.1 kHz, host blocks of 512): C5 at 0 held, C6 at 3000
  12881. // (Final.Vol 0.5, pan 0.3) released at 9000; B with the plugin's tick every 230 samples from 115
  12882. 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 } };
  12883. const auto ya = lynx::fruitkick::renderReference (A, notes, 44100.0, 20000);
  12884. CHECK (ya.size() == 40000u);
  12885. // (A uses the distortion: values since REA_rodada55 with FL's x87 live host waveshaper, checked against FLEngine's own;
  12886. // the release fade since REA_rodada74 with IPP's cosine, checked against the DLL)
  12887. CHECK (matchesGolden (ya, { { 10, 0.803601742f, 0.803601742f }, { 700, 0.185467482f, 0.185467482f }, { 2900, -0.63492614f, -0.63492614f },
  12888. { 3100, 0.339092523f, 0.544403195f }, { 5000, -0.0974622667f, -0.150664076f }, { 9100, 0.124622017f, 0.0544089526f },
  12889. { 9400, -0.146487325f, -0.144867823f }, { 15000, 0.009439799f, 0.009439799f } },
  12890. 0.284267665614));
  12891. std::vector<int> ticks;
  12892. for (int t = 115; t < 20000; t += 230)
  12893. ticks.push_back (t);
  12894. const auto yb = lynx::fruitkick::renderReference (B, notes, 44100.0, 20000, 512, ticks);
  12895. CHECK (matchesGolden (yb, { { 10, -0.00760388654f, -0.00760388654f }, { 700, 0.386696398f, 0.442232877f }, { 2900, -0.123097703f, -0.302172035f },
  12896. { 3100, 0.0523624197f, 0.0877688825f }, { 5000, 0.045177035f, 0.100505695f }, { 9100, -0.00423920294f, 0.00810315367f },
  12897. { 9400, 0.00482427329f, -0.010199016f }, { 15000, -0.000171193431f, 1.01026744e-05f } },
  12898. 0.117964927585));
  12899. // the instrument: same engine behind LYNX's channel gains (V = 1, centre), velocity → Init.Vol (link velocity on)
  12900. lynx::ChannelDef c;
  12901. c.instrument = lynx::InstrumentKind::fruitkick;
  12902. c.pluginState = A;
  12903. auto inst = lynx::makeInstrument (lynx::InstrumentKind::fruitkick);
  12904. inst->configure (c, 140.0);
  12905. inst->prepare (44100.0);
  12906. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  12907. for (int b = 0; b < 16; ++b)
  12908. {
  12909. if (b == 0)
  12910. inst->noteOn (60, 0.78125f);
  12911. if (b == 10)
  12912. inst->noteOff (60);
  12913. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  12914. }
  12915. const auto iv = (std::pow (11.0, 1.28 * 0.78125) - 1.0) / 10.0;
  12916. const auto ref = lynx::fruitkick::renderReference (A, { { 0, 60, iv, 0.0, 0.0, 5120 } }, 44100.0, 8192);
  12917. double diff = 0.0, energy = 0.0;
  12918. for (int i = 0; i < 8192; ++i)
  12919. {
  12920. 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]) });
  12921. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  12922. }
  12923. CHECK (diff < 1e-6 && energy > 1e-2);
  12924. }
  12925. TEST (ipp_cos_and_end_fade)
  12926. {
  12927. // ippsCos_32f_A11 of the installation's dsp_ippv2_x64.dll (REA_rodada74 §1): outputs the DLL gave on its two paths
  12928. const auto bits = [] (float v) { return ippcos::floatToBits (v); };
  12929. const auto f = ippcos::bitsToFloat;
  12930. CHECK (bits (ippcos::cosFma (0.1f)) == 0x3f7eaf8bu && bits (ippcos::cosFma (1.0f)) == 0x3f0a534cu);
  12931. CHECK (bits (ippcos::cosFma (2.0f)) == 0xbed51118u && bits (ippcos::cosFma (-3.0f)) == 0xbf7d6646u);
  12932. CHECK (bits (ippcos::cosFma (f (0x3e000007))) == 0x3f7df6f9u && bits (ippcos::cosNoFma (f (0x3e000007))) == 0x3f7df6f8u);
  12933. CHECK (bits (ippcos::cosFma (f (0x3e00007c))) == 0x3f7df6f4u && bits (ippcos::cosNoFma (f (0x3e00007c))) == 0x3f7df6f5u);
  12934. CHECK (ippcos::cosFma (1e-9f) == 1.0f && ippcos::cosNoFma (0.0f) == 1.0f);
  12935. // the portable fused multiply-add rounds once: (1 + 2^-12)^2 = 1 + 2^-11 + 2^-24 is a float halfway case that a
  12936. // tiny addend (lost in a plain double sum) must decide
  12937. const float a = 1.0f + 0x1p-12f;
  12938. CHECK (ippcos::fusedMulAdd (a, a, 0x1p-60f) == 1.0f + 0x1p-11f + 0x1p-23f);
  12939. CHECK (ippcos::fusedMulAdd (a, a, -0x1p-60f) == 1.0f + 0x1p-11f && ippcos::fusedMulAdd (a, a, 0.0f) == 1.0f + 0x1p-11f);
  12940. CHECK (ippcos::fusedMulAdd (3.0f, 5.0f, -1.0f) == 14.0f);
  12941. // the end-fade table: within 1.5e-4 of the exact cosine, ends near zero
  12942. std::vector<float> t (441);
  12943. ippcos::endFadeTable (t.data(), 441);
  12944. double worst = 0.0;
  12945. for (int i = 0; i < 441; ++i)
  12946. worst = std::max (worst, std::abs ((double) t[(std::size_t) i] - 0.5 * (1.0 + std::cos ((i + 1) * 3.141592653589793 / 442.0))));
  12947. CHECK (worst < 1.5e-4 && t[0] > 0.9999f && t[440] < 1e-4f);
  12948. }
  12949. TEST (bassdrum_engine)
  12950. {
  12951. // FL's CRT powf as the engine computes it: a case where its last bit differs from the correctly rounded power
  12952. const float x = 0.87011606f;
  12953. std::uint32_t bits = 0;
  12954. const auto p = lynx::bassdrum::powfMsvc (x, 0.125f);
  12955. std::memcpy (&bits, &p, 4);
  12956. CHECK (bits == 1065063964u);
  12957. CHECK (lynx::bassdrum::powfMsvc (2.0f, 1.0f) == 2.0f && lynx::bassdrum::powfMsvc (10.0f, 0.0f) == 1.0f);
  12958. const auto st = bassDrumTestState();
  12959. CHECK (lynx::bassDrumStateUsable (st) && lynx::bassDrumNoiseName (st) == "test noise.wav");
  12960. CHECK (lynx::bassDrumParamOffset (st, 6) == 0x50C + 24 && lynx::bassDrumParamOffset (st, 23) < 0);
  12961. auto broken = st;
  12962. broken[8] = 'X';
  12963. CHECK (! lynx::bassDrumStateUsable (broken));
  12964. // golden samples of our round-43 model (bdmodel.run with its powf port, 44.1 kHz, host blocks of 512)
  12965. const auto dir = bassDrumTestFolder();
  12966. 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 },
  12967. { 6100, 55, 1.5, 1.0, -0.3, 0.0, -1 } };
  12968. const auto y = lynx::bassdrum::renderReference (st, notes, 44100.0, 30000, dir);
  12969. CHECK (y.size() == 60000u);
  12970. CHECK (matchesGolden (y, { { 50, 0.0122119058f, 0.0122119058f }, { 400, -0.0886279643f, -0.0886279643f }, { 3000, -0.0428197235f, -0.0428197235f },
  12971. { 6050, 0.0199260078f, 0.0240648985f }, { 6300, -0.257363439f, -0.185516387f }, { 9000, 0.268954486f, 0.10506025f },
  12972. { 15000, -0.0102827176f, -0.0380715057f }, { 25000, 0.0f, 0.0f } },
  12973. 0.230007588169));
  12974. CHECK (lynx::bassdrum::renderReference (st, notes, 44100.0, 100, dir + "/nowhere").empty()); // no table: no engine
  12975. lynx::ChannelDef c;
  12976. c.instrument = lynx::InstrumentKind::bassdrum;
  12977. c.pluginState = st;
  12978. c.samplePath = dir;
  12979. auto inst = lynx::makeInstrument (lynx::InstrumentKind::bassdrum);
  12980. inst->configure (c, 120.0);
  12981. inst->prepare (44100.0);
  12982. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  12983. for (int b = 0; b < 16; ++b)
  12984. {
  12985. if (b == 0)
  12986. inst->noteOn (60, 0.78125f);
  12987. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  12988. }
  12989. 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);
  12990. double diff = 0.0, energy = 0.0;
  12991. for (int i = 0; i < 8192; ++i)
  12992. {
  12993. 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]) });
  12994. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  12995. }
  12996. CHECK (diff == 0.0 && energy > 1e-2);
  12997. }
  12998. // REA_rodada55: Fruity Fast Dist, Fruity Flangus and Fruity Flanger, goldens from the installation's DLLs (EXEC, our own states)
  12999. TEST (fruity_fast_dist_flangus_flanger)
  13000. {
  13001. std::vector<float> inL (4096), inR (4096);
  13002. for (int n = 0; n < 4096; ++n)
  13003. {
  13004. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  13005. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  13006. }
  13007. struct Gold { int i; float l, r; };
  13008. auto close = [] (float a, float b) { return std::abs (a - b) <= x87Tolerance() * std::max (1.0f, std::abs (b)); };
  13009. // Fast Dist (+ FL's host waveshaper): live and render kernels
  13010. auto dist = [&] (lynx::fastdistfx::State s, bool render, std::initializer_list<Gold> gold) {
  13011. std::vector<float> l (inL), r (inR);
  13012. lynx::fastdistfx::process (l.data(), 4096, s, render);
  13013. lynx::fastdistfx::process (r.data(), 4096, s, render);
  13014. for (const auto& g : gold)
  13015. CHECK (close (l[(std::size_t) g.i], g.l) && close (r[(std::size_t) g.i], g.r));
  13016. };
  13017. dist ({ 128, 10, 0, 128, 128 }, false, { { 100, 0.0f, -0.39352417f }, { 1000, -0.39352417f, 0.389404297f }, { 2047, 0.376953125f, -0.39352417f },
  13018. { 3000, -0.39352417f, -0.379760742f }, { 4095, 0.342590332f, 0.373931885f } });
  13019. dist ({ 128, 10, 0, 128, 128 }, true, { { 3000, -0.39352417f, -0.379751563f }, { 4095, 0.342590332f, 0.373931885f } });
  13020. dist ({ 192, 4, 1, 77, 97 }, false, { { 100, 0.0f, -0.868428111f }, { 1000, -1.23000622f, 0.740812242f }, { 2047, 0.58129251f, -0.855666518f },
  13021. { 3000, -1.23000622f, -0.613196433f }, { 4095, 0.307311207f, 0.549388587f } });
  13022. dist ({ 192, 4, 1, 77, 97 }, true, { { 2047, 0.58129245f, -0.855666518f }, { 4095, 0.307311267f, 0.549388528f } });
  13023. dist ({ 90, 7, 0, 40, 120 }, false, { { 100, 0.0f, -0.346225947f }, { 1000, -0.511893928f, 0.287246376f }, { 2047, 0.212910682f, -0.340344131f },
  13024. { 3000, -0.511893928f, -0.227848753f }, { 4095, 0.0910475403f, 0.197926477f } });
  13025. dist ({ 90, 7, 0, 40, 120 }, true, { { 100, 0.0f, -0.346223623f }, { 1000, -0.511893749f, 0.287246734f }, { 2047, 0.212909341f, -0.340341568f },
  13026. { 3000, -0.511893749f, -0.227846757f }, { 4095, 0.0910428166f, 0.197928891f } });
  13027. {
  13028. const auto* T = lynx::fastdistfx::curve (0, 10);
  13029. CHECK (T[0] == 0 && T[8191] == T[8192] && lynx::fastdistfx::curve (1, 1)[8191] == 32767);
  13030. }
  13031. // Flangus: FL's call order (state, sample rate, block size of 512), no flush
  13032. auto flange = [&] (lynx::flangusfx::State s, int fs, std::initializer_list<Gold> gold) {
  13033. lynx::flangusfx::Engine e;
  13034. e.prepare (fs);
  13035. e.load (s);
  13036. e.setRate (fs);
  13037. e.setBlockSize (512);
  13038. std::vector<float> io (8192);
  13039. for (int n = 0; n < 4096; ++n)
  13040. {
  13041. io[2 * (std::size_t) n] = inL[(std::size_t) n];
  13042. io[2 * (std::size_t) n + 1] = inR[(std::size_t) n];
  13043. }
  13044. for (int o = 0; o < 4096; o += 512)
  13045. e.render (io.data() + 2 * o, io.data() + 2 * o, 512);
  13046. for (const auto& g : gold)
  13047. CHECK (close (io[2 * (std::size_t) g.i], g.l) && close (io[2 * (std::size_t) g.i + 1], g.r));
  13048. };
  13049. flange ({ 4, 512, 768, 8, 128, -32, 96, 96 }, 44100, { { 100, 0.0249633491f, -0.119510099f }, { 1000, -1.26988697f, 0.687134743f },
  13050. { 2047, 1.03176403f, -0.278752804f }, { 3000, -0.940228283f, -0.853936791f },
  13051. { 4095, 0.201686561f, 0.607052445f } });
  13052. flange ({ 7, 300, 900, 40, 30, 100, -60, 128 }, 44100, { { 100, 0.0f, 0.0446268953f }, { 1000, 1.68992829f, 2.06982398f },
  13053. { 2047, 0.400214791f, 0.838502884f }, { 3000, 1.20018816f, 1.18875635f },
  13054. { 4095, -0.354708612f, 0.503573656f } });
  13055. flange ({ 2, 1024, 200, 0, 0, -128, 128, -90 }, 48000, { { 100, 0.172592998f, -0.230506495f }, { 1000, -1.56807125f, 0.854493499f },
  13056. { 2047, 1.02325416f, -1.17055321f }, { 3000, -1.74136555f, -0.535462856f },
  13057. { 4095, 1.49345946f, -0.537866116f } });
  13058. // Flanger: state, then the sample rate (FL's call order)
  13059. auto flanger = [&] (lynx::flangerfx::State s, int fs, std::initializer_list<Gold> gold) {
  13060. lynx::flangerfx::Engine e;
  13061. e.load (s);
  13062. e.prepare (fs);
  13063. std::vector<float> io (8192);
  13064. for (int n = 0; n < 4096; ++n)
  13065. {
  13066. io[2 * (std::size_t) n] = inL[(std::size_t) n];
  13067. io[2 * (std::size_t) n + 1] = inR[(std::size_t) n];
  13068. }
  13069. for (int o = 0; o < 4096; o += 512)
  13070. e.render (io.data() + 2 * o, io.data() + 2 * o, 512);
  13071. for (const auto& g : gold)
  13072. CHECK (close (io[2 * (std::size_t) g.i], g.l) && close (io[2 * (std::size_t) g.i + 1], g.r));
  13073. };
  13074. flanger ({ 0, 2000, 2000, 118, 0, 0, 23, 0, 1024, 3500, 3000, 2500 }, 44100,
  13075. { { 100, -1.22955823f, -1.98144293f }, { 1000, -0.975861192f, 0.646231353f }, { 2047, 0.518505156f, -0.857361794f },
  13076. { 3000, -0.546559215f, -0.0157452822f }, { 4095, 0.104127884f, 0.206255436f } });
  13077. flanger ({ 300, 4000, 4500, 90, 1000, 300, 99, 1024, 1024, 2000, 4000, 1000 }, 48000,
  13078. { { 100, 0.0f, -0.440000027f }, { 1000, 0.24619779f, 0.775473535f }, { 2047, 1.11609459f, -0.0639692694f },
  13079. { 3000, -1.22629714f, -1.41118503f }, { 4095, -0.263562649f, 0.80683732f } });
  13080. // import: FL's states become the exact effects with the controls as parameters; short states keep the approximations
  13081. {
  13082. 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 };
  13083. const auto slot = lynx::effectFromFl ("Fruity Fast Dist", fd, 120.0);
  13084. CHECK (slot && slot->kind == lynx::EffectKind::fruityFastDist);
  13085. if (slot)
  13086. {
  13087. const auto s = lynx::fastdistfx::fromParams (slot->params.data());
  13088. CHECK (s.pre == 192 && s.threshold == 4 && s.type == 1 && s.mix == 77 && s.post == 97);
  13089. auto fx = lynx::makeEffect (*slot);
  13090. fx->prepare (44100.0);
  13091. std::vector<float> l (inL), r (inR);
  13092. fx->process (l.data(), r.data(), 4096, slot->params, 120.0);
  13093. CHECK (l[1000] == -1.23000622f && r[4095] == 0.549388587f);
  13094. }
  13095. std::vector<std::uint8_t> fg (32);
  13096. const int v[8] { 7, 300, 900, 40, 30, 100, -60, 128 };
  13097. std::memcpy (fg.data(), v, 32);
  13098. const auto f2 = lynx::effectFromFl ("Fruity Flangus", fg, 120.0);
  13099. CHECK (f2 && f2->kind == lynx::EffectKind::fruityFlangus);
  13100. if (f2)
  13101. {
  13102. const auto s = lynx::flangusfx::fromParams (f2->params.data());
  13103. for (int i = 0; i < 8; ++i)
  13104. CHECK (s[(std::size_t) i] == v[i]);
  13105. auto fx = lynx::makeEffect (*f2);
  13106. fx->prepare (48000.0);
  13107. std::vector<float> l (inL), r (inR);
  13108. fx->process (l.data(), r.data(), 4096, f2->params, 120.0);
  13109. CHECK (std::isfinite (l[4095]) && std::isfinite (r[4095]) && std::abs (l[2000]) > 0.0f);
  13110. }
  13111. std::vector<std::uint8_t> fl (52);
  13112. const int w[13] { 1, 300, 4000, 4500, 90, 1000, 300, 99, 1024, 1024, 2000, 4000, 1000 };
  13113. std::memcpy (fl.data(), w, 52);
  13114. const auto f3 = lynx::effectFromFl ("Fruity Flanger", fl, 120.0);
  13115. CHECK (f3 && f3->kind == lynx::EffectKind::fruityFlanger);
  13116. if (f3)
  13117. {
  13118. const auto s = lynx::flangerfx::fromParams (f3->params.data());
  13119. for (int i = 0; i < 12; ++i)
  13120. CHECK (s[(std::size_t) i] == w[i + 1]);
  13121. auto fx = lynx::makeEffect (*f3);
  13122. fx->prepare (48000.0);
  13123. std::vector<float> l (inL), r (inR);
  13124. fx->process (l.data(), r.data(), 4096, f3->params, 120.0);
  13125. CHECK (l[1000] == 0.24619779f && r[4095] == 0.80683732f);
  13126. }
  13127. const auto shortFd = lynx::effectFromFl ("Fruity Fast Dist", std::vector<std::uint8_t> (8, 0), 120.0);
  13128. CHECK (! shortFd || shortFd->kind != lynx::EffectKind::fruityFastDist);
  13129. }
  13130. }
  13131. // FL's Fruity Phaser (REA_rodada105_phaser): goldens from the installation's own DLL, rendered unmodified in 512-frame
  13132. // calls; LYNX's engine matches it to the last bit on the 27 presets (denormals aside), the tolerance only absorbs
  13133. // compilers (gcc / clang / zig) and libm.
  13134. TEST (fruity_phaser_engine)
  13135. {
  13136. std::vector<float> in (8192);
  13137. for (int n = 0; n < 4096; ++n)
  13138. {
  13139. in[2 * (std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  13140. in[2 * (std::size_t) n + 1] = (float) (((n * 53) % 170 - 85) / 70.0);
  13141. }
  13142. struct Gold
  13143. {
  13144. int i;
  13145. float l, r;
  13146. };
  13147. auto close = [] (float a, float b) { return std::abs (a - b) <= 2.0e-5f * std::max (1.0f, std::abs (b)); };
  13148. auto phase = [&] (lynx::phaserfx::State s, int fs, std::initializer_list<Gold> gold) {
  13149. lynx::phaserfx::Engine e;
  13150. e.setRate (fs);
  13151. e.load (s);
  13152. e.reset();
  13153. std::vector<float> io (in);
  13154. for (int o = 0; o < 4096; o += 512)
  13155. e.render (io.data() + 2 * o, io.data() + 2 * o, 512);
  13156. for (const auto& g : gold)
  13157. CHECK (close (io[2 * (std::size_t) g.i], g.l) && close (io[2 * (std::size_t) g.i + 1], g.r));
  13158. };
  13159. phase ({ 2500, 100, 800, 0, 512, 7, 400, 512, 4000 }, 44100,
  13160. { { 100, 0.243106663f, -1.63760519f }, { 1000, -2.36990547f, 0.841273248f }, { 2047, 1.21970272f, -1.66379488f },
  13161. { 3000, -2.80603957f, -1.03600585f }, { 4095, 1.80225599f, 0.845855355f } });
  13162. phase ({ 417, 995, 1000, 0, 1024, 3, 705, 0, 2943 }, 48000,
  13163. { { 100, -0.360522032f, -0.0510774851f }, { 1000, -0.788047731f, 0.324495822f }, { 2047, 0.304549545f, 0.0850031748f },
  13164. { 3000, -0.79305625f, 0.114482567f }, { 4095, -0.67624253f, -0.0348635167f } });
  13165. phase ({ 2448, 510, 417, 1024, 1024, 16, 508, 0, 2500 }, 44100,
  13166. { { 100, 1.66273713f, -0.0830700323f }, { 1000, -1.47920549f, -0.0849421024f }, { 2047, -0.157817751f, -1.06916845f },
  13167. { 3000, -0.703828216f, -0.0190049056f }, { 4095, 0.768234849f, -0.965515852f } });
  13168. phase ({ 5000, 0, 1000, 700, 0, 22, 950, 300, 5000 }, 44100,
  13169. { { 100, 0.0809119642f, -0.521223426f }, { 1000, 1.07321298f, 0.681761861f }, { 2047, 0.790922821f, -0.205452889f },
  13170. { 3000, -1.91779351f, 0.599423349f }, { 4095, -1.18747222f, 1.03711367f } });
  13171. // controls: FL's ranges, the stage count clamp, the texts
  13172. CHECK (std::abs (lynx::phaserfx::sweepHz (0.5f, false) - 0.238405839f) < 1.0e-6f);
  13173. CHECK (std::abs (lynx::phaserfx::sweepHz (1.0f, true) - 10.0f) < 1.0e-5f);
  13174. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityPhaser, 5, 11.0f / 22.0f, 120.0) == "12");
  13175. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityPhaser, 5, 0.0f, 120.0) == "3");
  13176. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityPhaser, 7, 0.0f, 120.0) == "100% wet");
  13177. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityPhaser, 8, 0.5f, 120.0) == "+0.0 dB");
  13178. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityPhaser, 3, 0.0f, 120.0) == "small");
  13179. {
  13180. lynx::EffectParams ctx {};
  13181. ctx[3] = 1.0f;
  13182. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityPhaser, 0, 1.0f, 120.0, ctx) == "10.000 Hz");
  13183. }
  13184. CHECK (lynx::effectInfo (lynx::EffectKind::fruityPhaser).params.size() == 9);
  13185. // import: FL's state (version 1 + nine controls) becomes the exact effect; its parameters round-trip
  13186. const lynx::phaserfx::State ouch { 417, 995, 1000, 0, 1024, 3, 705, 0, 2943 };
  13187. const auto bytes = lynx::phaserfx::serialize (ouch);
  13188. const auto slot = lynx::effectFromFl ("Fruity Phaser", bytes, 120.0);
  13189. CHECK (slot && slot->kind == lynx::EffectKind::fruityPhaser);
  13190. if (slot)
  13191. {
  13192. CHECK (lynx::phaserfx::fromParams (slot->params.data()) == ouch);
  13193. auto fx = lynx::makeEffect (*slot);
  13194. fx->prepare (48000.0);
  13195. std::vector<float> l (4096), r (4096);
  13196. for (int n = 0; n < 4096; ++n)
  13197. {
  13198. l[(std::size_t) n] = in[2 * (std::size_t) n];
  13199. r[(std::size_t) n] = in[2 * (std::size_t) n + 1];
  13200. }
  13201. fx->process (l.data(), r.data(), 4096, slot->params, 120.0);
  13202. CHECK (close (l[1000], -0.788047731f) && close (r[4095], -0.0348635167f));
  13203. }
  13204. auto v2 = bytes;
  13205. v2[0] = 2; // FL reads only versions below 2
  13206. const auto later = lynx::effectFromFl ("Fruity Phaser", v2, 120.0);
  13207. CHECK (! later || later->kind != lynx::EffectKind::fruityPhaser);
  13208. CHECK (lynx::plugui::hasFlLayout (lynx::EffectKind::fruityPhaser));
  13209. }
  13210. // Workflow P2 (docs/WORKFLOW_FL.md)
  13211. #include "WorkflowP2Tests.inc"
  13212. // Editor mouse grammar (Piano Roll / Playlist)
  13213. #include "EditorGestureTests.inc"
  13214. TEST (chorus_family_engines)
  13215. {
  13216. // REA_rodada59: goldens from the installation's DLLs (44.1 kHz, blocks of 512) on this signal
  13217. std::vector<float> inL (4096), inR (4096);
  13218. for (int n = 0; n < 4096; ++n)
  13219. {
  13220. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0) * 0.5f;
  13221. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0) * 0.5f;
  13222. }
  13223. struct Gold { int i; float l, r; };
  13224. auto close = [] (float a, float b) { return std::abs (a - b) <= x87Tolerance() * std::max (1.0f, std::abs (b)); };
  13225. auto run = [&] (const char* plugin, const std::vector<std::uint8_t>& st, lynx::EffectKind kind, std::initializer_list<Gold> gold) {
  13226. const auto slot = lynx::effectFromFl (plugin, st, 120.0);
  13227. CHECK (slot && slot->kind == kind);
  13228. if (! slot)
  13229. return;
  13230. auto fx = lynx::makeEffect (*slot);
  13231. fx->prepare (44100.0);
  13232. std::vector<float> l (inL), r (inR);
  13233. fx->process (l.data(), r.data(), 1024, slot->params, 120.0);
  13234. fx->process (l.data() + 1024, r.data() + 1024, 3072, slot->params, 120.0);
  13235. for (const auto& g : gold)
  13236. CHECK (close (l[(std::size_t) g.i], g.l) && close (r[(std::size_t) g.i], g.r));
  13237. };
  13238. auto ints = [] (std::initializer_list<int> v) {
  13239. std::vector<std::uint8_t> b;
  13240. for (int x : v)
  13241. for (int k = 0; k < 4; ++k)
  13242. b.push_back ((std::uint8_t) ((unsigned) x >> (8 * k)));
  13243. return b;
  13244. };
  13245. // Fruity Chorus (start phases 0, which FL draws at random)
  13246. run ("Fruity Chorus", ints ({ 1, 512, 2250, 341, 1500, 2500, 3500, 23, 0, 0, 0, 2500, 0 }), lynx::EffectKind::fruityChorus,
  13247. { { 100, 0.0f, -0.3850000202655792f }, { 1000, -0.6181581020355225f, 0.2707735002040863f }, { 2047, -0.01424609124660492f, 0.24228274822235107f },
  13248. { 3000, -1.1650776863098145f, -0.1809898018836975f }, { 4095, -0.012790083885192871f, 0.060571759939193726f } });
  13249. run ("Fruity Chorus", ints ({ 1, 700, 4000, 900, 4000, 3000, 2000, 400, 700, 1000, 30, 4999, 0 }), lynx::EffectKind::fruityChorus,
  13250. { { 100, 0.0f, -0.3850000202655792f }, { 1000, -0.5833333134651184f, 0.36345374584198f }, { 2047, -0.0336097776889801f, -0.3360772430896759f },
  13251. { 3000, -0.40196430683135986f, -0.5073210597038269f }, { 4095, -0.1176539734005928f, 0.444706529378891f } });
  13252. run ("Fruity Chorus", ints ({ 1, 36, 469, 1004, 2832, 293, 3500, 0, 428, 0, 0, 3887, 1024 }), lynx::EffectKind::fruityChorus,
  13253. { { 100, 0.2975943684577942f, 0.3747217655181885f }, { 1000, -0.39463338255882263f, 0.18099871277809143f }, { 2047, 0.3894335627555847f, 0.35111793875694275f },
  13254. { 3000, -0.1450725942850113f, 0.2847272455692291f }, { 4095, 0.33678582310676575f, -0.13365517556667328f } });
  13255. {
  13256. const auto slot = lynx::effectFromFl ("Fruity Chorus", ints ({ 1, 36, 469, 1004, 2832, 293, 3500, 0, 428, 0, 0, 3887, 1024 }), 120.0);
  13257. CHECK (slot && lynx::fruitychorusfx::fromParams (slot->params.data())[2] == 1004);
  13258. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityChorus, 11, 1.0f, 120.0) == "on");
  13259. CHECK (lynx::effectParamFromFl ("Fruity Chorus", 11).has_value() && ! lynx::effectParamFromFl ("Fruity Chorus", 12).has_value());
  13260. }
  13261. // a state FL does not read (version 2) or too short keeps the generic chorus
  13262. CHECK (! lynx::effectFromFl ("Fruity Chorus", ints ({ 2, 512, 2250, 341, 1500, 2500, 3500, 23, 0, 0, 0, 2500, 0 }), 120.0).has_value()
  13263. || lynx::effectFromFl ("Fruity Chorus", ints ({ 2, 512, 2250, 341, 1500, 2500, 3500, 23, 0, 0, 0, 2500, 0 }), 120.0)->kind != lynx::EffectKind::fruityChorus);
  13264. CHECK (! lynx::effectFromFl ("Fruity Chorus", { 1, 0, 0, 0 }, 120.0).has_value()
  13265. || lynx::effectFromFl ("Fruity Chorus", { 1, 0, 0, 0 }, 120.0)->kind != lynx::EffectKind::fruityChorus);
  13266. // Hyper Chorus engine in FL's load sequence (defaults, then the state: the parameters glide), instance k's phases
  13267. auto hyper = [&] (std::initializer_list<int> raws, int instance, std::initializer_list<Gold> gold) {
  13268. namespace hc = lynx::hyperchorusfx;
  13269. hc::Engine e (instance);
  13270. const std::vector<int> r (raws);
  13271. 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); };
  13272. for (int i = 0; i < hc::params; ++i)
  13273. e.setParam (i, val (i, hc::defaults[(std::size_t) i]));
  13274. for (int i = 0; i < hc::params; ++i)
  13275. e.setParam (i, val (i, r[(std::size_t) i]));
  13276. e.setRate (44100.0);
  13277. std::vector<float> io (8192);
  13278. for (int n = 0; n < 4096; ++n)
  13279. io[2 * (std::size_t) n] = inL[(std::size_t) n], io[2 * (std::size_t) n + 1] = inR[(std::size_t) n];
  13280. for (int s = 0; s < 4096; s += 512)
  13281. e.render (io.data() + 2 * s, io.data() + 2 * s, 512);
  13282. for (const auto& g : gold)
  13283. CHECK (close (io[2 * (std::size_t) g.i], g.l) && close (io[2 * (std::size_t) g.i + 1], g.r));
  13284. };
  13285. hyper ({ 562, 919, 499, 505, 709, 716, 1, 19247, 10900, 191, 1000 }, 0,
  13286. { { 100, 0.0f, -0.4115639328956604f }, { 1000, -0.658551037311554f, 0.28647756576538086f }, { 2047, 0.24743860960006714f, -0.5088979005813599f },
  13287. { 3000, -0.5171564221382141f, -0.3630276620388031f }, { 4095, 0.08128821104764938f, 0.18811003863811493f } });
  13288. hyper ({ 1000, 555, 0, 400, 300, 300, 0, 22000, 120, 368, 750 }, 2,
  13289. { { 100, 0.0f, -0.398922324180603f }, { 1000, -1.1528236865997314f, 0.2839684784412384f }, { 2047, 0.5115746855735779f, -0.3330395221710205f },
  13290. { 3000, -0.6094785928726196f, 0.17040175199508667f }, { 4095, -0.11666109412908554f, 0.21931695938110352f } });
  13291. CHECK (lynx::hyperchorusfx::randState (1) == 1u * 214013u + 2531011u);
  13292. {
  13293. // the import: controls as parameters; a song's Hyper Chorus slots are numbered in mixer order
  13294. const auto st = ints ({ 2, 1000, 555, 0, 400, 300, 300, 0, 22000, 120, 368, 750, 0 });
  13295. const auto slot = lynx::effectFromFl ("Hyper Chorus", st, 120.0);
  13296. CHECK (slot && slot->kind == lynx::EffectKind::hyperChorus && lynx::hyperchorusfx::fromParams (slot->params.data())[9] == 368);
  13297. lynx::Song song;
  13298. song.mixer.resize (3);
  13299. song.mixer[1].effects = { *slot, *slot };
  13300. song.mixer[2].effects = { *slot };
  13301. lynx::numberChorusInstances (song);
  13302. CHECK (lynx::hyperchorusfx::instanceOf (song.mixer[2].effects[0].state) == 2);
  13303. auto fx = lynx::makeEffect (song.mixer[2].effects[0]);
  13304. fx->prepare (48000.0);
  13305. std::vector<float> l (inL), r (inR);
  13306. fx->process (l.data(), r.data(), 4096, slot->params, 120.0);
  13307. bool finite = true, wet = false;
  13308. for (int n = 0; n < 4096; ++n)
  13309. {
  13310. finite = finite && std::isfinite (l[(std::size_t) n]) && std::isfinite (r[(std::size_t) n]);
  13311. wet = wet || l[(std::size_t) n] != inL[(std::size_t) n];
  13312. }
  13313. CHECK (finite && wet);
  13314. CHECK (lynx::formatEffectParam (lynx::EffectKind::hyperChorus, 8, 0.0f, 120.0) == "off");
  13315. CHECK (! lynx::effectFromFl ("Hyper Chorus", ints ({ 3, 1000, 555, 0, 400, 300, 300, 0, 22000, 120, 368, 750, 0 }), 120.0).has_value()
  13316. || lynx::effectFromFl ("Hyper Chorus", ints ({ 3, 1000, 555, 0, 400, 300, 300, 0, 22000, 120, 368, 750, 0 }), 120.0)->kind != lynx::EffectKind::hyperChorus);
  13317. }
  13318. // Vintage Chorus engine as FL drives it (created, sample rate, each control), fresh instance (its smoothed values
  13319. // start from the engine's initial ones)
  13320. auto vintage = [&] (std::initializer_list<int> raws, std::initializer_list<Gold> gold) {
  13321. namespace vc = lynx::vintagechorusfx;
  13322. vc::Engine e;
  13323. e.setRate (44100.0);
  13324. int i = 0;
  13325. for (int raw : raws)
  13326. {
  13327. 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));
  13328. ++i;
  13329. }
  13330. std::vector<float> io (8192);
  13331. for (int n = 0; n < 4096; ++n)
  13332. io[2 * (std::size_t) n] = inL[(std::size_t) n], io[2 * (std::size_t) n + 1] = inR[(std::size_t) n];
  13333. for (int s = 0; s < 4096; s += 512)
  13334. e.render (io.data() + 2 * s, io.data() + 2 * s, 512);
  13335. for (const auto& g : gold)
  13336. CHECK (close (io[2 * (std::size_t) g.i], g.l) && close (io[2 * (std::size_t) g.i + 1], g.r));
  13337. };
  13338. vintage ({ 2, 0, 211046, 1, 2411, 960, 1985, 5121, 0, 1, 4420, 6000, 273, 0, 0, 0, 0 },
  13339. { { 100, 0.0f, -0.05606573447585106f }, { 1000, -0.3413679003715515f, 0.20469821989536285f }, { 2047, 0.1487489938735962f, -0.28199973702430725f },
  13340. { 3000, -0.31699514389038086f, -0.43230289220809937f }, { 4095, 0.1255197525024414f, 0.0810704305768013f } });
  13341. vintage ({ 2, 0, 80, 0, 0, 60, 3000, 3000, 0, 0, 0, 3000, 0, 3000, 0, 0, 0 }, // hiss
  13342. { { 100, -1.2527654007499223e-06f, -0.053655341267585754f }, { 1000, -0.44306400418281555f, 0.25606146454811096f }, { 2047, 0.23425261676311493f, -0.21375307440757751f },
  13343. { 3000, -0.5793180465698242f, -0.13371579349040985f }, { 4095, 0.05595953390002251f, 0.11362095922231674f } });
  13344. vintage ({ 0, 1, 49152, 0, 3000, 60, 3000, 3000, 0, 0, 0, 3000, 0, 0, 0, 0, 0 }, // Chorus 1 + 2
  13345. { { 100, 0.0f, -0.05365874245762825f }, { 1000, -0.5095958113670349f, 0.2809423506259918f }, { 2047, 0.17256303131580353f, -0.3563305139541626f },
  13346. { 3000, -0.471851646900177f, -0.30123069882392883f }, { 4095, 0.0959278866648674f, 0.13436323404312134f } });
  13347. {
  13348. const auto st = ints ({ 1, 2, 0, 211046, 1, 2411, 960, 1985, 5121, 0, 1, 4420, 6000, 273, 0, 0, 0, 0 });
  13349. const auto slot = lynx::effectFromFl ("Vintage Chorus", st, 120.0);
  13350. CHECK (slot && slot->kind == lynx::EffectKind::vintageChorus && lynx::vintagechorusfx::fromParams (slot->params.data())[2] == 211046);
  13351. auto fx = lynx::makeEffect (*slot);
  13352. fx->prepare (44100.0);
  13353. std::vector<float> l (inL), r (inR);
  13354. fx->process (l.data(), r.data(), 4096, slot->params, 120.0);
  13355. bool finite = true;
  13356. for (int n = 0; n < 4096; ++n)
  13357. finite = finite && std::isfinite (l[(std::size_t) n]) && std::isfinite (r[(std::size_t) n]);
  13358. CHECK (finite && l[3000] != inL[3000]);
  13359. CHECK (lynx::formatEffectParam (lynx::EffectKind::vintageChorus, 0, 0.0f, 120.0) == "Chorus 1");
  13360. 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()
  13361. || 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);
  13362. }
  13363. }
  13364. #include "UiChromeTests.inc"
  13365. TEST (fruity_delay_engines)
  13366. {
  13367. // REA_rodada52: the engines on their own against the installation's DLLs (goldens = the DLL's output for the same
  13368. // input with FL's set-up order, EXEC: bit-identical), then the mixer effects built from FL states
  13369. const int n = 16384;
  13370. std::vector<float> inL ((std::size_t) n, 0.0f), inR ((std::size_t) n, 0.0f);
  13371. std::uint32_t seed = 1;
  13372. for (int i = 0; i < 4096; ++i) // a noise burst, then the echoes
  13373. {
  13374. seed = seed * 1664525u + 1013904223u;
  13375. inL[(std::size_t) i] = (float) ((int) (seed >> 16) - 32768) / 32768.0f * 0.5f;
  13376. seed = seed * 1664525u + 1013904223u;
  13377. inR[(std::size_t) i] = (float) ((int) (seed >> 16) - 32768) / 32768.0f * 0.5f;
  13378. }
  13379. using Golden = std::vector<std::tuple<int, float, float>>;
  13380. auto check = [&] (const std::vector<float>& l, const std::vector<float>& r, const Golden& golden) {
  13381. for (const auto& [i, gl, gr] : golden)
  13382. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  13383. };
  13384. auto run3 = [&] (const std::array<int, 26>& raw, double bpm) {
  13385. lynx::fldelay::Delay3Engine e (44100.0);
  13386. e.setTempo (bpm);
  13387. for (int i = 0; i < 26; ++i)
  13388. e.setParameter (i, lynx::fldelay::delay3EngineValue (i, raw[(std::size_t) i]));
  13389. e.setSampleRate (44100.0); // FL's wrapper sets the rate up again after the state
  13390. e.setSampleRate (44100.0);
  13391. std::vector<float> l (inL), r (inR);
  13392. for (int s = 0; s < n; s += 512)
  13393. e.process (l.data() + s, r.data() + s, 512);
  13394. return std::make_pair (l, r);
  13395. };
  13396. auto run2 = [&] (const std::array<int, 8>& raw, double bpm) {
  13397. lynx::fldelay::Delay2State st;
  13398. st.v = raw;
  13399. lynx::fldelay::Delay2Engine e (st, 44100.0, bpm, 512);
  13400. std::vector<float> l (inL), r (inR);
  13401. for (int s = 0; s < n; s += 512)
  13402. e.process (l.data() + s, r.data() + s, 512);
  13403. return std::make_pair (l, r);
  13404. };
  13405. // Delay 3, true stereo, synced 1/8 beat, cutoff + resonance (Fruity Delay 3_x64.dll at 120 and 100 BPM)
  13406. 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 };
  13407. auto o = run3 (a, 120.0);
  13408. check (o.first, o.second, { { 3000, 0.399949551f, 0.598533869f }, { 6000, 0.116703831f, -0.315510482f }, { 9000, -0.150520369f, -0.250620633f },
  13409. { 13000, 0.193608373f, 0.0598564595f }, { 16000, -0.0524701327f, 0.022601109f } });
  13410. o = run3 (a, 100.0);
  13411. check (o.first, o.second, { { 5000, 0.377422422f, 0.346570343f }, { 6000, -0.182619616f, 0.0488029197f }, { 11000, -0.00120426295f, 0.0253051519f },
  13412. { 16000, 0.0806550607f, -0.0991330743f } });
  13413. // ping pong, keep pitch, modulation, cutoff LFO, diffusion, saturation, rate and bit reduction, colour
  13414. 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);
  13415. check (o.first, o.second, { { 1500, 0.288540125f, -0.148862943f }, { 5000, 5.90502168e-06f, 0.111382999f }, { 8000, -0.0736631379f, -0.0305093247f },
  13416. { 11000, -0.00831047911f, 0.0102323527f }, { 16000, 0.0175791476f, 0.0273917634f } });
  13417. // fake stereo, unsynced, high-pass, limit distortion, no dry
  13418. 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);
  13419. check (o.first, o.second, { { 1500, 0.0825810209f, 0.0228505582f }, { 6000, -0.0427959524f, -0.0408911295f }, { 9000, 0.0114790015f, 0.019227922f },
  13420. { 16000, -0.000312746735f, 0.00101647642f } });
  13421. // delay type "Off": filter, distortion and the bit/rate reduction on the dry path
  13422. 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);
  13423. check (o.first, o.second, { { 1500, 0.238686681f, -0.106820084f }, { 3000, 0.131528616f, 0.0665975735f }, { 5000, 9.94473215e-09f, -8.22819057e-09f },
  13424. { 11000, 0.0f, 0.0f } });
  13425. // Delay 2: default, ping pong with a stereo offset, swapped without dry (Fruity Delay 2_x64.dll)
  13426. o = run2 ({ 0, 128, 128, 64, 36, 0, 0, 128 }, 120.0);
  13427. check (o.first, o.second, { { 3000, 0.211380005f, 0.277908325f }, { 5000, -0.223615795f, -0.0189359933f }, { 9000, 0.120054141f, -0.00297877868f },
  13428. { 16000, 0.0182533264f, 0.105384827f } });
  13429. o = run2 ({ -40, 110, 90, 90, 24, -300, 2, 90 }, 100.0);
  13430. check (o.first, o.second, { { 3000, 0.0929639712f, 0.122222826f }, { 5000, -0.0884733275f, -0.0507739782f }, { 11000, 0.0589251742f, 0.0f },
  13431. { 16000, -0.029524127f, 0.0128910914f } });
  13432. o = run2 ({ 60, 140, 0, 100, 30, 250, 1, 110 }, 120.0);
  13433. check (o.first, o.second, { { 1500, 0.0f, 0.0f }, { 5000, 0.176379532f, -0.212506086f }, { 8000, -0.324207425f, 0.0675852001f },
  13434. { 13000, 0.145023212f, -0.104788139f } });
  13435. // the mixer effects from FL states: time law (Delay 3 synced 192 = 1 beat; Delay 2 144 = 3/4 beat), settled start
  13436. auto state3 = std::vector<std::uint8_t> (4 + 26 * 4, 0);
  13437. 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 };
  13438. state3[0] = 1;
  13439. std::memcpy (state3.data() + 4, def3.data(), 26 * 4);
  13440. const auto slot3 = lynx::effectFromFl ("Fruity Delay 3", state3, 120.0);
  13441. CHECK (slot3 && slot3->kind == lynx::EffectKind::fruityDelay3);
  13442. auto state2 = std::vector<std::uint8_t> (32, 0);
  13443. const std::array<int, 8> def2 { 0, 128, 128, 0, 144, 0, 0, 128 };
  13444. std::memcpy (state2.data(), def2.data(), 32);
  13445. const auto slot2 = lynx::effectFromFl ("Fruity delay 2", state2, 120.0);
  13446. CHECK (slot2 && slot2->kind == lynx::EffectKind::fruityDelay2);
  13447. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityDelay2, 4, slot2->params[4], 120.0) == "3:00");
  13448. auto echoAt = [] (const lynx::EffectSlot& slot, double bpm) {
  13449. auto fx = lynx::makeEffect (slot);
  13450. fx->prepare (44100.0);
  13451. std::vector<float> l (48000, 0.0f), r (48000, 0.0f);
  13452. l[100] = 1.0f;
  13453. for (int s = 0; s < 48000; s += 1024)
  13454. fx->process (l.data() + s, r.data() + s, std::min (1024, 48000 - s), slot.params, bpm);
  13455. int best = 200;
  13456. for (int i = 200; i < 48000; ++i)
  13457. if (std::abs (l[(std::size_t) i]) > std::abs (l[(std::size_t) best]))
  13458. best = i;
  13459. return best - 100;
  13460. };
  13461. if (slot3 && slot2)
  13462. {
  13463. CHECK (echoAt (*slot3, 120.0) == 22050);
  13464. CHECK (echoAt (*slot3, 150.0) == 17640);
  13465. CHECK (echoAt (*slot2, 120.0) == 16538);
  13466. CHECK (echoAt (*slot2, 140.0) == 14175);
  13467. }
  13468. }
  13469. #include "UiRackMixerTests.inc"
  13470. // ---------------------------------------------------------------------------
  13471. // FL Studio integration (core/FlPluginDb.h): synthetic plugin-database fixtures, no Image-Line files
  13472. namespace
  13473. {
  13474. std::vector<std::uint8_t> testWrapperBlob (std::uint32_t type, const std::string& name, const std::string& path,
  13475. const std::vector<std::uint8_t>& classId = {}, const std::string& clapId = {})
  13476. {
  13477. std::vector<std::uint8_t> b;
  13478. auto u32 = [&b] (std::uint32_t v) { for (int i = 0; i < 4; ++i) b.push_back ((std::uint8_t) (v >> (8 * i))); };
  13479. auto record = [&] (std::uint32_t id, const std::vector<std::uint8_t>& payload) {
  13480. u32 (id);
  13481. u32 ((std::uint32_t) payload.size());
  13482. u32 (0);
  13483. b.insert (b.end(), payload.begin(), payload.end());
  13484. };
  13485. u32 (12);
  13486. record (0x32, { (std::uint8_t) type, 0, 0, 0 });
  13487. if (! classId.empty())
  13488. record (0x34, classId);
  13489. if (! clapId.empty())
  13490. record (0x3A, std::vector<std::uint8_t> (clapId.begin(), clapId.end()));
  13491. record (0x36, std::vector<std::uint8_t> (name.begin(), name.end()));
  13492. record (0x37, std::vector<std::uint8_t> (path.begin(), path.end()));
  13493. return b;
  13494. }
  13495. } // namespace
  13496. TEST (fl_plugin_db_nfo_parsing)
  13497. {
  13498. const std::string nfo = "ps_name=Vital\r\n"
  13499. "ps_file_name_0=C:\\Program Files\\Common Files\\VST3\\Vital.vst3\r\n"
  13500. "ps_file_vendorname_0=Vital Audio\r\n"
  13501. "ps_file_type_0=2\r\n"
  13502. "ps_file_name_1=C:\\VSTPlugins\\Vital_x64.dll\r\n"
  13503. "ps_file_type_1=2\r\n"
  13504. "ps_file_magic_1=Vita\r\n"
  13505. "ps_file_name_2=not a plugin\r\n"
  13506. "garbage line without equals\r\n";
  13507. const auto entries = lynx::parseFlPluginNfo (nfo, "D:\\FL\\Presets\\Plugin database\\Installed\\Generators\\VST3\\Vital.nfo");
  13508. CHECK (entries.size() == 2);
  13509. if (entries.size() == 2)
  13510. {
  13511. CHECK (entries[0].name == "Vital" && entries[0].vendor == "Vital Audio" && entries[0].role == 2);
  13512. CHECK (entries[0].format == lynx::ForeignPlugin::Format::vst3);
  13513. CHECK (entries[1].format == lynx::ForeignPlugin::Format::vst2 && entries[1].vst2Id == 0x56697461u);
  13514. }
  13515. // no type field: the role comes from the Effects / Generators folder; an ID in GUID form is read
  13516. const auto fx = lynx::parseFlPluginNfo ("ps_name=Fancy Comp\nps_file_filename_0=C:/CLAP/Fancy Comp.clap\n"
  13517. "ps_file_guid_0={01234567-89AB-CDEF-0123-456789ABCDEF}\n",
  13518. "C:\\x\\Installed\\Effects\\CLAP\\Fancy Comp.nfo");
  13519. CHECK (fx.size() == 1 && fx[0].role == 1 && fx[0].format == lynx::ForeignPlugin::Format::clap && fx[0].hasClassId
  13520. && fx[0].classId[0] == 0x01 && fx[0].classId[15] == 0xEF);
  13521. CHECK (lynx::parseFlPluginNfo ("Tip=All of the plugins found in your system.").empty());
  13522. CHECK (lynx::pluginNameKey ("Serum_x64") == "serum");
  13523. CHECK (lynx::pluginNameKey ("Vital.VST3") == "vital");
  13524. CHECK (lynx::pluginNameKey ("Pro-Q 3") == "proq3");
  13525. CHECK (lynx::pluginFormatFromPath ("a\\b.CLAP") == lynx::ForeignPlugin::Format::clap);
  13526. }
  13527. TEST (fl_plugin_db_fst_parsing_and_matching)
  13528. {
  13529. std::vector<std::uint8_t> cid;
  13530. for (int i = 0; i < 16; ++i)
  13531. cid.push_back ((std::uint8_t) (0x10 + i));
  13532. // an .fst of the database: an FL preset whose 0xD5 holds the wrapper record
  13533. FlpBuilder fst;
  13534. fst.ascii (0xC9, "Fruity Wrapper");
  13535. fst.data (0xD5, testWrapperBlob (8, "Surge XT", "E:\\Plugins\\VST3\\Surge XT.vst3", cid));
  13536. lynx::FlPluginDbEntry e;
  13537. CHECK (lynx::parseFlPluginFst (fst.bytes, e));
  13538. CHECK (e.name == "Surge XT" && e.format == lynx::ForeignPlugin::Format::vst3 && e.hasClassId && e.classId[0] == 0x10);
  13539. lynx::FlPluginDbEntry bad;
  13540. CHECK (! lynx::parseFlPluginFst (std::vector<std::uint8_t> { 'F', 'L', 'h', 'd' }, bad));
  13541. lynx::FlPluginDb db;
  13542. db.add (e);
  13543. // the .nfo of the same plugin fills in vendor and role
  13544. for (const auto& n : lynx::parseFlPluginNfo ("ps_name=Surge XT\nps_file_name_0=E:/Plugins/VST3/Surge XT.vst3\n"
  13545. "ps_file_vendorname_0=Surge Synth Team\nps_file_type_0=2\n"))
  13546. db.add (n);
  13547. db.add ({ "Clapper", "", "E:\\Plugins\\CLAP\\Clapper.clap", "", lynx::ForeignPlugin::Format::clap, 1, {}, false, "org.clapper", 0 });
  13548. db.add ({ "Big Verb", "", "E:\\Old VST\\BigVerb_x64.dll", "", lynx::ForeignPlugin::Format::vst2, 1, {}, false, "", 0x42566262u });
  13549. db.add ({ "Bundle", "", "F:\\VST3\\Bundle.vst3\\Contents\\x86_64-win\\Bundle.vst3", "", lynx::ForeignPlugin::Format::vst3, 2, {}, false, "", 0 });
  13550. CHECK (db.entries().size() == 4);
  13551. CHECK (db.entries()[0].vendor == "Surge Synth Team" && db.entries()[0].role == 2);
  13552. // by VST3 class ID even when the project saved another path and name
  13553. lynx::ForeignPlugin p;
  13554. p.format = lynx::ForeignPlugin::Format::vst3;
  13555. p.hasClassId = true;
  13556. std::copy (cid.begin(), cid.end(), p.classId.begin());
  13557. p.name = "Renamed";
  13558. p.path = "C:\\Other PC\\Surge.vst3";
  13559. CHECK (db.match (p, true) == &db.entries()[0]);
  13560. // by CLAP id, by VST2 id
  13561. lynx::ForeignPlugin c;
  13562. c.format = lynx::ForeignPlugin::Format::clap;
  13563. c.clapId = "org.clapper";
  13564. CHECK (db.match (c, false) == &db.entries()[1]);
  13565. lynx::ForeignPlugin v;
  13566. v.format = lynx::ForeignPlugin::Format::vst2;
  13567. v.vst2Id = 0x42566262u;
  13568. CHECK (db.match (v, false) == &db.entries()[2]);
  13569. // by name (architecture suffix ignored), only in the same role
  13570. lynx::ForeignPlugin byName;
  13571. byName.format = lynx::ForeignPlugin::Format::vst2;
  13572. byName.name = "BigVerb x64";
  13573. CHECK (db.match (byName, false) == &db.entries()[2]);
  13574. CHECK (db.match (byName, true) == nullptr);
  13575. lynx::ForeignPlugin unknown;
  13576. unknown.format = lynx::ForeignPlugin::Format::vst3;
  13577. unknown.name = "Nothing Like It";
  13578. CHECK (db.match (unknown, true) == nullptr);
  13579. // folders: the bundle's parent, deduplicated
  13580. const auto folders = db.pluginFolders();
  13581. CHECK (folders.size() == 4);
  13582. CHECK (std::find (folders.begin(), folders.end(), "F:\\VST3") != folders.end());
  13583. CHECK (std::find (folders.begin(), folders.end(), "E:\\Plugins\\VST3") != folders.end());
  13584. // targeted scan picks: same file, or same name key
  13585. CHECK (lynx::pluginFileMatches ("E:/Plugins/VST3/Surge XT.vst3", p, &db.entries()[0]));
  13586. CHECK (lynx::pluginFileMatches ("C:\\Program Files\\Common Files\\VST3\\BigVerb.vst3", byName, nullptr));
  13587. CHECK (! lynx::pluginFileMatches ("C:\\VST3\\Other.vst3", byName, nullptr));
  13588. // the plugins an .flp uses: wrapper channels and mixer slots
  13589. flp::Project project;
  13590. flp::Channel ch;
  13591. ch.pluginName = "Fruity Wrapper";
  13592. ch.pluginState = testWrapperBlob (8, "Surge XT", "C:\\VST3\\Surge XT.vst3", cid);
  13593. project.channels.push_back (ch);
  13594. ch.pluginName = "3x Osc";
  13595. project.channels.push_back (ch);
  13596. flp::MixerInsert insert;
  13597. insert.slots.push_back ({ 0, "Fruity Wrapper", testWrapperBlob (12, "Clapper", "C:\\CLAP\\Clapper.clap", {}, "org.clapper") });
  13598. insert.slots.push_back ({ 1, "Fruity Wrapper", testWrapperBlob (3, "DX thing", "C:\\dx.dll") }); // DirectX: not hosted
  13599. project.mixer.push_back (insert);
  13600. const auto uses = lynx::flpForeignPlugins (project);
  13601. CHECK (uses.size() == 2);
  13602. CHECK (uses.size() == 2 && uses[0].instrument && ! uses[1].instrument && uses[1].plugin.clapId == "org.clapper");
  13603. }
  13604. TEST (fl_sample_relocation)
  13605. {
  13606. std::set<std::string> files { "D:\\Docs\\Image-Line\\FL Studio\\Audio\\Recorded\\take.wav", "C:\\Program Files\\FL 2026\\Data\\Patches\\kick.wav",
  13607. "C:\\Users\\me\\Music\\loop.wav", "E:\\Songs\\Track\\Samples\\snare.wav", "E:\\Songs\\Track\\hat.wav" };
  13608. auto exists = [&] (const std::string& p) { return files.count (p) > 0; };
  13609. lynx::FlPathRoots roots;
  13610. roots.userData = "D:\\Docs\\Image-Line\\FL Studio";
  13611. roots.factoryData = "C:\\Program Files\\FL 2026";
  13612. roots.home = "C:\\Users\\me";
  13613. const std::string project = "E:\\Songs\\Track";
  13614. // variable resolved, file in place
  13615. CHECK (lynx::locateFlSample ("%FLStudioFactoryData%\\Data\\Patches\\kick.wav", roots, project, exists) == "C:\\Program Files\\FL 2026\\Data\\Patches\\kick.wav");
  13616. // another PC's FL user data / FL install / user profile
  13617. CHECK (lynx::locateFlSample ("C:\\Users\\bob\\Documents\\Image-Line\\FL Studio\\Audio\\Recorded\\take.wav", roots, project, exists)
  13618. == "D:\\Docs\\Image-Line\\FL Studio\\Audio\\Recorded\\take.wav");
  13619. CHECK (lynx::locateFlSample ("C:\\Program Files (x86)\\Image-Line\\FL Studio 20\\Data\\Patches\\kick.wav", roots, project, exists)
  13620. == "C:\\Program Files\\FL 2026\\Data\\Patches\\kick.wav");
  13621. CHECK (lynx::locateFlSample ("C:/Users/bob/Music/loop.wav", roots, project, exists) == "C:\\Users\\me\\Music\\loop.wav");
  13622. // next to the project, also in a sub-folder named in the original path
  13623. CHECK (lynx::locateFlSample ("Z:\\Old\\Samples\\snare.wav", roots, project, exists) == "E:\\Songs\\Track\\Samples\\snare.wav");
  13624. CHECK (lynx::locateFlSample ("Z:\\Old\\Stuff\\hat.wav", roots, project, exists) == "E:\\Songs\\Track\\hat.wav");
  13625. CHECK (lynx::locateFlSample ("Z:\\nowhere.wav", roots, project, exists).empty());
  13626. CHECK (lynx::locateFlSample ("", roots, project, exists).empty());
  13627. }
  13628. // --- REA round 64: SimSynth (exact engine) and Wasp / Wasp XT (FL 2026's silent placeholder) ---------------------
  13629. namespace
  13630. {
  13631. // a SimSynth state written from scratch (REA_rodada64 §1.1 layout: 45 doubles, 19 unused ints, 19 switches, 12 unused
  13632. // ints, version)
  13633. std::vector<std::uint8_t> simSynthTestState (const std::array<double, 45>& P, const std::array<int, 19>& S, int version = 14)
  13634. {
  13635. std::vector<std::uint8_t> b (564, 0);
  13636. std::memcpy (b.data(), P.data(), 360);
  13637. std::memcpy (b.data() + 360 + 76, S.data(), 76);
  13638. std::memcpy (b.data() + 560, &version, 4);
  13639. return b;
  13640. }
  13641. // the 0xD5 records of an FL preset file (.fst: the FLhd/FLdt event stream) that follow a plugin name `plugin`
  13642. std::vector<std::vector<std::uint8_t>> pluginStatesIn (const std::filesystem::path& file, const std::string& plugin)
  13643. {
  13644. std::vector<std::vector<std::uint8_t>> out;
  13645. std::ifstream in (file, std::ios::binary);
  13646. const std::vector<std::uint8_t> b ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  13647. std::size_t i = 0;
  13648. while (i + 8 <= b.size() && std::memcmp (b.data() + i, "FLdt", 4) != 0)
  13649. ++i;
  13650. i += 8;
  13651. std::string name;
  13652. while (i < b.size())
  13653. {
  13654. const auto id = b[i++];
  13655. std::size_t n = id < 64 ? 1 : id < 128 ? 2 : id < 192 ? 4 : 0;
  13656. if (id >= 192)
  13657. for (int shift = 0; i < b.size(); shift += 7)
  13658. {
  13659. const auto c = b[i++];
  13660. n |= (std::size_t) (c & 0x7F) << shift;
  13661. if (c < 0x80)
  13662. break;
  13663. }
  13664. if (i + n > b.size())
  13665. break;
  13666. if (id == 0xC9)
  13667. {
  13668. name.clear();
  13669. for (std::size_t k = 0; k < n; ++k)
  13670. if (b[i + k] != 0)
  13671. name += (char) b[i + k]; // ASCII or UTF-16LE (zero bytes skipped)
  13672. }
  13673. if (id == 0xD5 && name == plugin)
  13674. out.emplace_back (b.begin() + (std::ptrdiff_t) i, b.begin() + (std::ptrdiff_t) (i + n));
  13675. i += n;
  13676. }
  13677. return out;
  13678. }
  13679. } // namespace
  13680. TEST (simsynth_engine)
  13681. {
  13682. // A: pulse + octave layer, half-cosine with unison and two octave layers, sine/sine⁴ mix, square LFO → pitch with
  13683. // delay, filter envelope, LP/BP mix, chorus. B (version 7): triangle ring-modulated by filtered noise, sample &
  13684. // hold LFO → cutoff, retrigger, own filter envelope, chorus off
  13685. 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,
  13686. 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 };
  13687. 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 };
  13688. 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,
  13689. 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 };
  13690. 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 };
  13691. const auto blobA = simSynthTestState (A, SA), blobB = simSynthTestState (B, SB, 7);
  13692. dumpIfAsked ("LYNX_DUMP_SIMSYNTH", blobA);
  13693. CHECK (lynx::simSynthStateUsable (blobA) && ! lynx::simSynthStateUsable (std::vector<std::uint8_t> (blobA.begin(), blobA.begin() + 563)));
  13694. const auto hp = lynx::simSynthHostParams (blobA);
  13695. // knobs go through ⌊65535·v⌋, the coarse pitches through 48 steps, the switches and wave selectors as they are
  13696. CHECK (hp.size() == 58u && hp[0] == 52428 && hp[27] == 10080 && hp[41] == 1 && hp[54] == 0 && hp[56] == 4);
  13697. // golden samples of the installation's SimSynth_x64.dll (round-64 EXEC harness: created, state restored, 44.1 kHz,
  13698. // host blocks of 512, old voice record) — LYNX's engine renders bit-identically: C5 0–30000, E5 Final.Vol 0.8 pan
  13699. // 16 11025–40000, G5 pan −20 +25 cents 22050–52000
  13700. const std::vector<lynx::simsynth::RefNote> notes = { { 0, 60, 1.0f, 0, 0, 0.0f, 0.0f, 30000 },
  13701. { 11025, 64, 0.8f, 16, 0, 0.0f, 0.0f, 40000 },
  13702. { 22050, 67, 1.0f, -20, 25, 0.0f, 0.0f, 52000 } };
  13703. struct Golden { int i; float l, r; };
  13704. 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 } };
  13705. 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 } };
  13706. for (int which = 0; which < 2; ++which)
  13707. {
  13708. std::vector<std::int64_t> counters;
  13709. const auto y = lynx::simsynth::renderReference (which == 0 ? blobA : blobB, notes, 44100.0, 66150, 512, &counters);
  13710. CHECK (y.size() == 2 * 66150u);
  13711. if (y.size() != 2 * 66150u)
  13712. continue;
  13713. // the random-table index after creation / state / rate set equals the DLL's (276 / 696 / 756 and 276 / 492 / 528)
  13714. CHECK (counters.size() == 4u && counters[2] == 276 && counters[3] == (which == 0 ? 696 : 492) && counters[0] == (which == 0 ? 756 : 528));
  13715. for (const auto& g : which == 0 ? std::vector (std::begin (goldenA), std::end (goldenA)) : std::vector (std::begin (goldenB), std::end (goldenB)))
  13716. CHECK (y[2 * (std::size_t) g.i] == g.l && y[2 * (std::size_t) g.i + 1] == g.r);
  13717. double e = 0.0;
  13718. for (auto v : y)
  13719. e += (double) v * v;
  13720. CHECK (std::abs (std::sqrt (e / (double) y.size()) - (which == 0 ? 0.290665331870 : 0.992116840643)) < 1e-9);
  13721. }
  13722. // the instrument: same engine behind LYNX's channel gains (V = 1, centre → Final.Vol 1, Final.Pan 0)
  13723. lynx::ChannelDef c;
  13724. c.instrument = lynx::InstrumentKind::simsynth;
  13725. c.pluginState = blobA;
  13726. auto inst = lynx::makeInstrument (lynx::InstrumentKind::simsynth);
  13727. inst->prepare (44100.0);
  13728. inst->configure (c, 120.0);
  13729. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  13730. for (int k = 0; k < 16; ++k)
  13731. {
  13732. if (k == 0)
  13733. inst->noteOn (60, 0.8f);
  13734. if (k == 10)
  13735. inst->noteOff (60);
  13736. inst->render (l.data() + 512 * k, r.data() + 512 * k, 512, 1.0f, 1.0f);
  13737. }
  13738. const auto ref = lynx::simsynth::renderReference (blobA, { { 0, 60, 1.0f, 0, 0, 0.0f, 0.0f, 5120 } }, 44100.0, 8192);
  13739. double diff = 0.0, energy = 0.0;
  13740. for (int i = 0; i < 8192; ++i)
  13741. {
  13742. 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]) });
  13743. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  13744. }
  13745. CHECK (diff == 0.0 && energy > 1e-3);
  13746. // the installation's SimSynth presets (when the FL data folder is given): every state usable and sounding
  13747. if (! realFlpDir.empty())
  13748. {
  13749. const auto folder = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "SimSynth";
  13750. int presets = 0, usable = 0, sounding = 0;
  13751. if (std::filesystem::exists (folder))
  13752. for (const auto& entry : std::filesystem::directory_iterator (folder))
  13753. for (const auto& st : pluginStatesIn (entry.path(), "SimSynth"))
  13754. {
  13755. ++presets;
  13756. if (! lynx::simSynthStateUsable (st))
  13757. continue;
  13758. ++usable;
  13759. const auto y = lynx::simsynth::renderReference (st, { { 0, 48, 1.0f, 0, 0, 0.0f, 0.0f, 11025 } }, 44100.0, 22050);
  13760. double e = 0.0;
  13761. bool finite = true;
  13762. for (auto v : y)
  13763. finite = finite && std::isfinite (v), e += (double) v * v;
  13764. CHECK (finite);
  13765. sounding += e > 1e-9 ? 1 : 0;
  13766. }
  13767. std::printf (" SimSynth presets: %d, %d usable, %d sounding\n", presets, usable, sounding);
  13768. CHECK (presets == 0 || (usable == presets && presets >= 70 && sounding >= presets - 2));
  13769. }
  13770. }
  13771. TEST (wasp_is_a_silent_placeholder)
  13772. {
  13773. // FL 2026's Wasp / Wasp XT DLL keeps the state and plays nothing: so does LYNX (REA_rodada64 §5)
  13774. auto inst = lynx::makeInstrument (lynx::InstrumentKind::wasp);
  13775. inst->prepare (44100.0);
  13776. std::vector<float> l (1024, 0.0f), r (1024, 0.0f);
  13777. inst->noteOn (60, 1.0f);
  13778. inst->render (l.data(), r.data(), 1024, 1.0f, 1.0f);
  13779. CHECK (inst->idle() && std::all_of (l.begin(), l.end(), [] (float v) { return v == 0.0f; }));
  13780. // import: a SimSynth channel plays on the exact engine, Wasp and Wasp XT channels become the silent placeholder with
  13781. // their state kept
  13782. std::vector<std::uint8_t> ss (564, 0), wasp (169, 1);
  13783. const int version = 14;
  13784. std::memcpy (ss.data() + 560, &version, 4);
  13785. FlpBuilder b;
  13786. b.ascii (0xC7, "25.2.5.5319");
  13787. int index = 0;
  13788. for (const auto& [name, state] : { std::pair<std::u16string, std::vector<std::uint8_t>> { u"SimSynth", ss }, { u"Wasp", wasp }, { u"Wasp XT", wasp } })
  13789. {
  13790. b.u8 (0x40); b.u16 ((std::uint16_t) index++);
  13791. b.u8 (0x15); b.u8 (2);
  13792. b.utf16 (0xC9, name);
  13793. b.data (0xD5, state);
  13794. }
  13795. const auto song = lynx::importFlp (flp::parseProject (b.bytes));
  13796. CHECK (song.channels.size() == 3u);
  13797. if (song.channels.size() == 3u)
  13798. {
  13799. CHECK (song.channels[0].instrument == lynx::InstrumentKind::simsynth && song.channels[0].pluginState == ss);
  13800. CHECK (song.channels[1].instrument == lynx::InstrumentKind::wasp && song.channels[1].pluginState == wasp);
  13801. CHECK (song.channels[2].instrument == lynx::InstrumentKind::wasp);
  13802. }
  13803. }
  13804. // --- REA round 67: Fruity Granulizer (exact engine) --------------------------------------------------------------
  13805. namespace
  13806. {
  13807. // our own deterministic test sample (a fading 220 Hz sine plus LCG noise per channel), as r67/py/gold67.py builds it
  13808. lynx::SampleData granulizerTestSample (int n, int ch)
  13809. {
  13810. lynx::SampleData d;
  13811. std::uint32_t s = 12345;
  13812. for (int i = 0; i < n; ++i)
  13813. {
  13814. const auto base = 0.5 * std::sin (2.0 * 3.141592653589793 * 220.0 * i / 44100.0) * (1.0 - 0.7 * i / n);
  13815. for (int c = 0; c < ch; ++c)
  13816. {
  13817. s = s * 1664525u + 1013904223u;
  13818. const auto v = (float) (base + 0.2 * ((double) (std::int32_t) s * (1.0 / 2147483648.0)));
  13819. (c == 0 ? d.left : d.right).push_back (v);
  13820. }
  13821. }
  13822. return d;
  13823. }
  13824. std::vector<std::uint8_t> granulizerTestState (const std::array<int, 12>& P, int keyMode = 0)
  13825. {
  13826. std::vector<std::uint8_t> b (60, 0);
  13827. const std::int32_t v = 5;
  13828. std::memcpy (b.data(), &v, 4);
  13829. std::memcpy (b.data() + 4, P.data(), 48);
  13830. std::memcpy (b.data() + 56, &keyMode, 4);
  13831. return b;
  13832. }
  13833. } // namespace
  13834. TEST (granulizer_engine)
  13835. {
  13836. // REA_rodada67: states written from scratch against our reference model (granmodel.py, which renders like the
  13837. // installation's DLL within −128 dB: the DLL's rcpps and IPP sines are not replicated)
  13838. const auto v5 = granulizerTestState ({ 204, 50, 611, 611, 0, 180, 0, 0, 0, 0, 0, 228 });
  13839. CHECK (lynx::granulizerStateUsable (v5));
  13840. const auto n5 = lynx::granulizerNormalisedState (v5, {});
  13841. CHECK (n5.size() == 428 && n5[0] == 7 && lynx::granulizerParam (n5, 0) == 204 && lynx::granulizerParam (n5, 11) == 228
  13842. && ! lynx::granulizerParam (n5, 12));
  13843. // version 1 (10 parameters): parameter 11 takes the plugin's 0xf8
  13844. {
  13845. std::vector<std::uint8_t> v1 (v5.begin(), v5.begin() + 44);
  13846. v1[0] = 1;
  13847. const auto n1 = lynx::granulizerNormalisedState (v1, {});
  13848. CHECK (lynx::granulizerParam (n1, 9) == 0 && lynx::granulizerParam (n1, 11) == 0xf8 && lynx::granulizerParam (n1, 3) == 611);
  13849. std::vector<std::uint8_t> bad (v5);
  13850. bad[0] = 9;
  13851. CHECK (! lynx::granulizerStateUsable (bad) && lynx::granulizerNormalisedState (bad, {}).empty());
  13852. auto edited = n5;
  13853. CHECK (lynx::granulizerSetParam (edited, 3, -5000) && lynx::granulizerParam (edited, 3) == -1000);
  13854. }
  13855. using N = lynx::granulizer::RefNote;
  13856. struct Golden { std::vector<std::uint8_t> state; int ch, fs, rate; lynx::granulizer::RefOptions opt; std::vector<N> notes;
  13857. double el, er; std::tuple<int, float, float> at[6]; };
  13858. auto check = [] (const Golden& g) {
  13859. auto smp = granulizerTestSample (20000, g.ch);
  13860. smp.sampleRate = g.rate;
  13861. const auto y = lynx::granulizer::renderReference (lynx::granulizerNormalisedState (g.state, {}), smp, g.notes, (double) g.fs, 16384, g.opt);
  13862. CHECK (y.size() == 2 * 16384);
  13863. if (y.size() != 2 * 16384)
  13864. return;
  13865. double el = 0.0, er = 0.0;
  13866. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  13867. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  13868. CHECK (std::abs (el - g.el) <= 1e-6 * g.el && std::abs (er - g.er) <= 1e-6 * g.er);
  13869. for (const auto& [i, l, r] : g.at)
  13870. CHECK (goldenSampleClose (y[2 * (std::size_t) i], l, granulizerTolerance())
  13871. && goldenSampleClose (y[2 * (std::size_t) i + 1], r, granulizerTolerance()));
  13872. };
  13873. lynx::granulizer::RefOptions o1, o2, o3;
  13874. o1.points = o2.points = o3.points = 12; // the plugin's own default quality (these goldens predate FL's render value)
  13875. o2.block = 333, o2.seed = 777;
  13876. o3.block = 64, o3.seed = 5;
  13877. // the plugin's defaults, two notes (one pitched by +23 cents and released: the processor's end fade)
  13878. 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,
  13879. { { 100, 0.057010360062122345f, 0.08960580080747604f }, { 1000, 0.07210570573806763f, -0.048257164657115936f },
  13880. { 2500, 0.14769265055656433f, -0.0194698516279459f }, { 4000, 0.11169229447841644f, 0.17487481236457825f },
  13881. { 7777, -0.4309714436531067f, -0.17027156054973602f }, { 12000, -0.11129666864871979f, -0.19622492790222168f } } });
  13882. // backwards, randomness, depth/speed wobble, stereo separation, loop, sample start; a mono 44.1 kHz sample at 48 kHz
  13883. check ({ granulizerTestState ({ 150, 120, 300, -480, 200, 90, 120, 40, 1, 0, 900, 300 }), 1, 48000, 44100, o2,
  13884. { { 0, 55, 1.0, 0.2, -37.0, 12000 }, { 5000, 67, 0.5, 0.0, 0.0, -1 } }, 959.5011214226065, 1294.4823684530272,
  13885. { { 100, 0.02567935362458229f, 0.010721667669713497f }, { 1000, 0.3007732629776001f, 0.08214527368545532f },
  13886. { 2500, 0.435413658618927f, 0.09068503230810165f }, { 4000, -0.24611327052116394f, -0.20149046182632446f },
  13887. { 7777, -0.008478119969367981f, 0.31221628189086914f }, { 12000, 0.203709632f, 0.324944794f } } });
  13888. // key mode 1 (the sample spread over two octaves), freeze, hard stereo separation; 48 kHz sample at 96 kHz, 64-sample blocks
  13889. check ({ granulizerTestState ({ 300, 0, 800, 700, 0, 180, 0, 64, 0, 1, 2000, 100 }, 1), 2, 96000, 48000, o3,
  13890. { { 0, 62, 0.9, 0.0, 0.0, -1 }, { 4000, 50, 0.7, 0.0, 11.0, -1 } }, 366.79556669810813, 0.00825590391720353,
  13891. { { 100, 0.004204070195555687f, 7.680895437786243e-20f }, { 1000, 0.008516596630215645f, 3.0263170437258543e-19f },
  13892. { 2500, 0.06948182731866837f, 4.5391312775082004e-18f }, { 4000, -0.01754589192569256f, -4.895453905423713e-19f },
  13893. { 7777, 0.10338862985372543f, 2.344363859234514e-18f }, { 12000, -0.09385982155799866f, -1.3493021437162966e-17f } } });
  13894. // the instrument: silent without a sample (FL's plugin refuses the note), sounding with one
  13895. {
  13896. auto silent = lynx::makeInstrument (lynx::InstrumentKind::granulizer);
  13897. silent->prepare (44100.0);
  13898. std::vector<float> l (2048, 0.0f), r (2048, 0.0f);
  13899. silent->noteOn (60, 1.0f);
  13900. silent->render (l.data(), r.data(), 2048, 1.0f, 1.0f);
  13901. CHECK (silent->idle() && std::all_of (l.begin(), l.end(), [] (float v) { return v == 0.0f; }));
  13902. auto smp = std::make_shared<lynx::SampleData> (granulizerTestSample (20000, 2));
  13903. auto inst = lynx::makeInstrument (lynx::InstrumentKind::granulizer, smp);
  13904. lynx::ChannelDef def;
  13905. def.instrument = lynx::InstrumentKind::granulizer;
  13906. def.pluginState = n5;
  13907. inst->configure (def, 140.0);
  13908. inst->prepare (44100.0);
  13909. inst->noteOn (60, 0.8f);
  13910. inst->render (l.data(), r.data(), 2048, 1.0f, 1.0f);
  13911. double e = 0.0;
  13912. for (auto v : l)
  13913. e += (double) v * v;
  13914. CHECK (! inst->idle() && e > 1.0);
  13915. inst->noteOff (60);
  13916. for (int k = 0; k < 4; ++k)
  13917. inst->render (l.data(), r.data(), 512, 1.0f, 1.0f);
  13918. CHECK (inst->idle()); // the 10 ms end fade of the plugin's voice processor
  13919. }
  13920. // tempo-synced grains follow the song position in LYNX's 960-PPQ ticks: an instance fed setSongPosition every
  13921. // block while playing must stay in phase with one that free-runs its own tick clock from 0 (it was 10x off)
  13922. {
  13923. auto smp = std::make_shared<lynx::SampleData> (granulizerTestSample (20000, 2));
  13924. lynx::ChannelDef def;
  13925. def.instrument = lynx::InstrumentKind::granulizer;
  13926. def.pluginState = n5;
  13927. auto fed = lynx::makeInstrument (lynx::InstrumentKind::granulizer, smp);
  13928. auto free = lynx::makeInstrument (lynx::InstrumentKind::granulizer, smp);
  13929. for (auto* g : { fed.get(), free.get() })
  13930. {
  13931. g->configure (def, 140.0);
  13932. g->prepare (44100.0);
  13933. g->noteOn (60, 0.8f);
  13934. }
  13935. const auto tps = 140.0 * lynx::ppq / (60.0 * 44100.0);
  13936. std::vector<float> l1 (300), r1 (300), l2 (300), r2 (300);
  13937. double diff = 0.0, energy = 0.0;
  13938. for (int block = 0, done = 0; block < 60; ++block, done += 300)
  13939. {
  13940. fed->setSongPosition ((double) done * tps);
  13941. std::fill (l1.begin(), l1.end(), 0.0f), std::fill (r1.begin(), r1.end(), 0.0f);
  13942. std::fill (l2.begin(), l2.end(), 0.0f), std::fill (r2.begin(), r2.end(), 0.0f);
  13943. fed->render (l1.data(), r1.data(), 300, 1.0f, 1.0f);
  13944. free->render (l2.data(), r2.data(), 300, 1.0f, 1.0f);
  13945. for (int j = 0; j < 300; ++j)
  13946. diff = std::max (diff, (double) std::fabs (l1[(std::size_t) j] - l2[(std::size_t) j])),
  13947. energy += (double) l2[(std::size_t) j] * l2[(std::size_t) j];
  13948. }
  13949. CHECK (energy > 1.0 && diff < 1e-4);
  13950. }
  13951. // import: a Fruity Granulizer channel plays on the exact engine with its sample
  13952. {
  13953. FlpBuilder b;
  13954. b.ascii (0xC7, "25.2.5.5319");
  13955. b.u8 (0x40); b.u16 (0);
  13956. b.u8 (0x15); b.u8 (2);
  13957. b.utf16 (0xC9, u"Fruity Granulizer");
  13958. b.data (0xD5, v5);
  13959. b.utf16 (0xC4, u"%FLStudioFactoryData%\\Data\\Patches\\Packs\\Drums\\Kicks\\808 Kick.wav");
  13960. lynx::FlpImportOptions options;
  13961. options.roots.factoryData = "C:\\FL";
  13962. options.fileExists = [] (const std::string& path) { return path == "C:\\FL\\Data\\Patches\\Packs\\Drums\\Kicks\\808 Kick.wav"; };
  13963. lynx::FlpImportReport report;
  13964. options.report = &report;
  13965. const auto song = lynx::importFlp (flp::parseProject (b.bytes), options);
  13966. CHECK (song.channels.size() == 1u);
  13967. if (song.channels.size() == 1u)
  13968. {
  13969. CHECK (song.channels[0].instrument == lynx::InstrumentKind::granulizer && song.channels[0].pluginState == n5);
  13970. CHECK (song.channels[0].samplePath == "C:\\FL\\Data\\Patches\\Packs\\Drums\\Kicks\\808 Kick.wav");
  13971. }
  13972. CHECK (report.converted["Fruity Granulizer"] == 1);
  13973. }
  13974. }
  13975. // --- REA round 67: Drumaxx (exact engine) -------------------------------------------------------------------------
  13976. namespace
  13977. {
  13978. // our own deterministic 16-pad kit (LCG-chosen parameters), as r67/py/gold67d.py builds it for the DLL
  13979. std::vector<std::uint8_t> drumaxxTestState (bool eq)
  13980. {
  13981. std::uint32_t s = 20261015u;
  13982. auto u = [&s] {
  13983. s = s * 1664525u + 1013904223u;
  13984. return (float) ((double) (s >> 8) * (1.0 / 16777216.0));
  13985. };
  13986. // (a product rounded on its own: no fused multiply-add)
  13987. auto mul = [] (float a, float b) {
  13988. volatile float p = a * b;
  13989. return (float) p;
  13990. };
  13991. static const int targets[] = { 0, 9, 10, 16, 21, 23, 26 };
  13992. std::array<float, 720> P {};
  13993. for (int k = 0; k < 16; ++k)
  13994. for (int i = 0; i < 44; ++i)
  13995. {
  13996. auto v = u();
  13997. if (i == 0)
  13998. v = 0.5f + mul (v, 0.5f);
  13999. else if ((i >= 7 && i <= 18) || (i >= 20 && i <= 34))
  14000. v = 0.2f + mul (v, 0.6f);
  14001. else if (i == 2 || i == 3)
  14002. v = 0.0f;
  14003. else if (i == 4)
  14004. v = (float) (36 + k);
  14005. else if (i == 5)
  14006. v = v < 0.5f ? 0.0f : 1.0f;
  14007. else if (i == 6)
  14008. v = (float) (int) (v * 4.0f);
  14009. else if (i >= 35 && i <= 38)
  14010. v = (float) targets[(int) (v * 7.0f)];
  14011. P[(std::size_t) (44 * k + i)] = v;
  14012. }
  14013. for (int i = 0; i < 16; ++i)
  14014. {
  14015. auto v = u();
  14016. if (i == 1)
  14017. v = 0.3f + mul (v, 0.4f);
  14018. else if (i == 3)
  14019. v = eq ? 1.0f : 0.0f;
  14020. else if (i >= 4 && i <= 11)
  14021. v = 0.35f + mul (v, 0.3f);
  14022. else if (i == 12)
  14023. v = 1.0f;
  14024. else if (i == 13)
  14025. v = v * 0.1f;
  14026. else if (i == 0 || i == 15)
  14027. v = 0.0f;
  14028. P[(std::size_t) (704 + i)] = v;
  14029. }
  14030. std::vector<std::uint8_t> b;
  14031. auto put32 = [&b] (std::uint32_t v) { for (int i = 0; i < 4; ++i) b.push_back ((std::uint8_t) (v >> (8 * i))); };
  14032. b.insert (b.end(), { 'S', 'S', 'L', 'F' });
  14033. put32 (21636u);
  14034. put32 (0x444d5333u); // engine stream version 3
  14035. const std::string name = "LYNX test kit";
  14036. b.insert (b.end(), name.begin(), name.end());
  14037. b.resize (b.size() + 256 - name.size(), 0);
  14038. const auto at = b.size();
  14039. b.resize (at + 720 * 4);
  14040. std::memcpy (b.data() + at, P.data(), 720 * 4);
  14041. for (int k = 0; k < 16; ++k)
  14042. {
  14043. b.resize (b.size() + 0x400, 0);
  14044. b.push_back (100), b.push_back (100); // pattern length, trigger key
  14045. }
  14046. b.resize (b.size() + 0x800, 0);
  14047. for (int k = 0; k < 16; ++k)
  14048. b.push_back (k >= 2 && k <= 4 ? 1 : 0); // pads 3…5 in cut group 1
  14049. b.resize (b.size() + 16, 0); // every pad on the master
  14050. return b;
  14051. }
  14052. } // namespace
  14053. TEST (drumaxx_engine)
  14054. {
  14055. const auto st = drumaxxTestState (true);
  14056. CHECK (st.size() == 21644u && lynx::drumaxxStateUsable (st) && lynx::drumaxxKitName (st) == "LYNX test kit");
  14057. CHECK (lynx::drumaxxParam (st, 4) == 36.0f && lynx::drumaxxParam (st, 44 * 15 + 4) == 51.0f && lynx::drumaxxParam (st, 0x2c3) == 1.0f);
  14058. {
  14059. auto edited = st;
  14060. lynx::drumaxxSetParam (edited, 0x2c1, 0.25f);
  14061. CHECK (lynx::drumaxxParam (edited, 0x2c1) == 0.25f && std::isnan (lynx::drumaxxParam (edited, 720)));
  14062. auto bad = st;
  14063. bad[8] = 'X';
  14064. CHECK (! lynx::drumaxxStateUsable (bad) && std::isnan (lynx::drumaxxParam (bad, 0)));
  14065. }
  14066. // goldens: LYNX's renders, bit-identical to Drumaxx_x64.dll + engine_x64.dll driven by the round-67 harness
  14067. using N = lynx::drumaxx::RefNote;
  14068. struct Golden { bool eq; int fs, block; std::vector<N> notes; double el, er; std::tuple<int, float, float> at[6]; };
  14069. auto check = [] (const Golden& g) {
  14070. const auto y = lynx::drumaxx::renderReference (drumaxxTestState (g.eq), g.notes, (double) g.fs, 20000, g.block);
  14071. CHECK (y.size() == 2 * 20000);
  14072. if (y.size() != 2 * 20000)
  14073. return;
  14074. double el = 0.0, er = 0.0;
  14075. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  14076. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  14077. CHECK (std::abs (el - g.el) <= 1e-6 * g.el && std::abs (er - g.er) <= 1e-6 * g.er);
  14078. for (const auto& [i, l, r] : g.at)
  14079. CHECK (std::abs (y[2 * (std::size_t) i] - l) <= 1e-6f * std::max (1e-16f, std::abs (l))
  14080. && std::abs (y[2 * (std::size_t) i + 1] - r) <= 1e-6f * std::max (1e-16f, std::abs (r)));
  14081. };
  14082. // master EQ and limiter on: velocities, Final.Vol, pitch offsets, the cut group (pads 3…5)
  14083. check ({ true, 44100, 512,
  14084. { { 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 },
  14085. { 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 },
  14086. { 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 } },
  14087. 2698.372377185949, 3678.285731410973,
  14088. { { 100, 0.0f, 0.0f }, { 1000, -0.0034233136102557182f, -0.005708245560526848f }, { 2500, -0.41297006607055664f, -0.0276018138974905f },
  14089. { 4000, -0.04906991869211197f, -0.7206904292106628f }, { 7777, 0.7544509172439575f, 0.7584841847419739f },
  14090. { 15000, -0.13438990712165833f, -0.6798369884490967f } } });
  14091. // EQ off, 48 kHz in 333-sample blocks, two pads on the same sample
  14092. check ({ false, 48000, 333,
  14093. { { 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 },
  14094. { 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 } },
  14095. 2241.008810618806, 1814.5219804412964,
  14096. { { 100, 0.0f, 0.0f }, { 1000, -0.32539352774620056f, -0.7439078688621521f }, { 2500, -0.1711389720439911f, -0.39125433564186096f },
  14097. { 4000, 0.15524788200855255f, 0.3445436358451843f }, { 7777, 0.5472834706306458f, 0.08737402409315109f },
  14098. { 15000, -0.03079034946858883f, -0.06140122935175896f } } });
  14099. // 96 kHz in 64-sample blocks, an octave down
  14100. check ({ true, 96000, 64,
  14101. { { 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 },
  14102. { 10000, 51, 1.0, 1.0, -1200.0 } },
  14103. 3522.8420617665834, 5746.555795626526,
  14104. { { 100, 0.0f, 0.0f }, { 1000, -0.04133236035704613f, -0.09954667836427689f }, { 2500, 0.2800408899784088f, -0.9350972175598145f },
  14105. { 4000, 0.9537044167518616f, 0.8892703056335449f }, { 7777, 0.8376388549804688f, -0.14166420698165894f },
  14106. { 15000, -0.05926012247800827f, -0.5840796828269958f } } });
  14107. // the instrument: a pad sounds on its key and dies away; keys without a pad stay silent
  14108. {
  14109. auto inst = lynx::makeInstrument (lynx::InstrumentKind::drumaxx);
  14110. lynx::ChannelDef def;
  14111. def.instrument = lynx::InstrumentKind::drumaxx;
  14112. def.pluginState = st;
  14113. inst->configure (def, 140.0);
  14114. inst->prepare (44100.0);
  14115. std::vector<float> l (512, 0.0f), r (512, 0.0f);
  14116. inst->noteOn (90, 1.0f);
  14117. inst->render (l.data(), r.data(), 512, 1.0f, 1.0f);
  14118. CHECK (inst->idle() && std::all_of (l.begin(), l.end(), [] (float v) { return v == 0.0f; }));
  14119. inst->noteOn (36, 0.8f);
  14120. inst->render (l.data(), r.data(), 512, 1.0f, 1.0f);
  14121. double e = 0.0;
  14122. for (auto v : l)
  14123. e += (double) v * v;
  14124. CHECK (! inst->idle() && e > 1e-3);
  14125. for (int k = 0; k < 200 && ! inst->idle(); ++k)
  14126. inst->render (l.data(), r.data(), 512, 1.0f, 1.0f);
  14127. CHECK (inst->idle());
  14128. }
  14129. // import: a Drumaxx channel plays on the exact engine with its raw state
  14130. {
  14131. FlpBuilder b;
  14132. b.ascii (0xC7, "25.2.5.5319");
  14133. b.u8 (0x40); b.u16 (0);
  14134. b.u8 (0x15); b.u8 (2);
  14135. b.utf16 (0xC9, u"Drumaxx");
  14136. b.data (0xD5, st);
  14137. lynx::FlpImportOptions options;
  14138. lynx::FlpImportReport report;
  14139. options.report = &report;
  14140. const auto song = lynx::importFlp (flp::parseProject (b.bytes), options);
  14141. CHECK (song.channels.size() == 1u);
  14142. if (song.channels.size() == 1u)
  14143. CHECK (song.channels[0].instrument == lynx::InstrumentKind::drumaxx && song.channels[0].pluginState == st);
  14144. CHECK (report.converted["Drumaxx"] == 1);
  14145. }
  14146. }
  14147. // --- REA round 68: FL Slayer (exact engine) and Fruity Dance (visual only) ------------------------------------------
  14148. namespace
  14149. {
  14150. std::string slayerDll()
  14151. {
  14152. if (realFlpDir.empty())
  14153. return {};
  14154. return (std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "FL Slayer" / "Slayer_Engine.dll").string();
  14155. }
  14156. std::vector<std::uint8_t> slayerPreset (const char* name)
  14157. {
  14158. if (realFlpDir.empty())
  14159. return {};
  14160. const auto file = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "FL Slayer" / (std::string (name) + ".fst");
  14161. const auto states = pluginStatesIn (file, "FL Slayer");
  14162. return states.empty() ? std::vector<std::uint8_t> {} : states.front();
  14163. }
  14164. } // namespace
  14165. TEST (slayer_state)
  14166. {
  14167. // the saved state: int version, then the parameter integers (version 0: 28, version 1: 29 with the mod wheel)
  14168. std::vector<std::uint8_t> v0 (4 + 28 * 4, 0), v1 (4 + 29 * 4, 0);
  14169. const std::int32_t one = 1, wheel = 77, mode = 5;
  14170. std::memcpy (v1.data(), &one, 4);
  14171. std::memcpy (v1.data() + 4 + 28 * 4, &wheel, 4);
  14172. std::memcpy (v0.data() + 4 + 24 * 4, &mode, 4);
  14173. CHECK (lynx::slayerStateUsable (v0) && lynx::slayerStateUsable (v1));
  14174. CHECK (! lynx::slayerStateUsable (std::vector<std::uint8_t> (v0.begin(), v0.end() - 1)));
  14175. auto v2 = v1;
  14176. v2[0] = 2;
  14177. CHECK (! lynx::slayerStateUsable (v2));
  14178. const auto p0 = lynx::slayerStateParams (v0), p1 = lynx::slayerStateParams (v1);
  14179. CHECK (p0.size() == 29u && p0[24] == 5 && p0[28] == 0 && p1.size() == 29u && p1[28] == 77);
  14180. // the controls' maxima (the front-end sends value / max)
  14181. CHECK (lynx::slayerParamMax (0) == 6 && lynx::slayerParamMax (20) == 15 && lynx::slayerParamMax (24) == 5 && lynx::slayerParamMax (28) == 127
  14182. && lynx::slayerParamMax (3) == 65535);
  14183. // MIDI velocity from Init.Vol (0 → 0, which the engine takes as a note-off; FL's full velocity → 127)
  14184. CHECK (lynx::slayer::midiVelocity (0.0) == 0 && lynx::slayer::midiVelocity (1.0) == 99 && lynx::slayer::midiVelocity (2.0) == 126
  14185. && lynx::slayer::midiVelocity ((std::pow (11.0, 1.28) - 1.0) / 10.0) == 127);
  14186. }
  14187. TEST (slayer_engine)
  14188. {
  14189. const auto dll = slayerDll();
  14190. if (dll.empty() || ! lynx::slayerDllUsable (dll))
  14191. return; // no FL installation
  14192. // against the installation's Slayer_Engine.dll (EXEC, REA_rodada68 §7: the DLL driven like FL's front-end drives it,
  14193. // host blocks of 512 at 44.1 kHz, tempo 120): bit-identical output. C3 vel 90 1024–20000, G3 vel 120 8000–30000,
  14194. // E4 vel 40 16000–40000, C5 vel 127 from 30000.
  14195. struct Sample { int at; float l, r; };
  14196. struct Golden { const char* preset; double el, er; Sample s[4]; };
  14197. const Golden goldens[] {
  14198. { "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 } } },
  14199. { "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 } } },
  14200. { "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 } } },
  14201. { "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 } } },
  14202. { "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 } } },
  14203. { "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 } } },
  14204. { "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 } } },
  14205. { "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 } } },
  14206. { "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 } } },
  14207. { "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 } } },
  14208. { "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 } } },
  14209. { "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 } } },
  14210. };
  14211. const std::vector<lynx::slayer::RefNote> notes = { { 1024, 48, 90, 20000 }, { 8000, 55, 120, 30000 }, { 16000, 64, 40, 40000 }, { 30000, 72, 127, -1 } };
  14212. constexpr int length = 44100;
  14213. int checked = 0;
  14214. for (const auto& g : goldens)
  14215. {
  14216. const auto st = slayerPreset (g.preset);
  14217. CHECK (lynx::slayerStateUsable (st));
  14218. if (! lynx::slayerStateUsable (st))
  14219. continue;
  14220. const auto y = lynx::slayer::renderReference (lynx::slayerStateParams (st), dll, notes, 44100.0, length);
  14221. double el = 0.0, er = 0.0;
  14222. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  14223. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  14224. 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;
  14225. for (const auto& smp : g.s)
  14226. ok = ok && y[2 * (std::size_t) smp.at] == smp.l && y[2 * (std::size_t) smp.at + 1] == smp.r;
  14227. if (! ok)
  14228. 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);
  14229. CHECK (ok);
  14230. ++checked;
  14231. }
  14232. CHECK (checked == 12);
  14233. // the instrument: the engine primed with one sample, FL's velocity curve, the front-end's gains from the channel's
  14234. // MIDI pan / volume (REA_rodada79 §2: twice the channel gain through the host's integer-pan law); a note with
  14235. // velocity 0.8 is the reference note one sample later
  14236. const auto st = slayerPreset ("BASS Ebass 2");
  14237. lynx::ChannelDef c;
  14238. c.instrument = lynx::InstrumentKind::slayer;
  14239. c.pluginState = st;
  14240. c.samplePath = dll;
  14241. auto inst = lynx::makeInstrument (lynx::InstrumentKind::slayer);
  14242. inst->configure (c, 120.0);
  14243. inst->prepare (44100.0);
  14244. std::vector<float> l (16384, 0.0f), r (16384, 0.0f);
  14245. for (int b = 0; b < 32; ++b)
  14246. {
  14247. if (b == 0)
  14248. inst->noteOn (60, 0.8f);
  14249. if (b == 20)
  14250. inst->noteOff (60);
  14251. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 0.5f, 0.25f);
  14252. }
  14253. const int vel = lynx::slayer::midiVelocity ((std::pow (11.0, 1.28 * 0.8) - 1.0) / 10.0);
  14254. const auto ref = lynx::slayer::renderReference (lynx::slayerStateParams (st), dll, { { 1, 60, vel, 1 + 20 * 512 } }, 44100.0, 16385);
  14255. // LYNX's channel gains 0.5 / 0.25 = gain 0.395…, pan −0.4097 → MIDI pan −26
  14256. const auto chanVol = (float) std::sqrt ((0.25 + 0.0625) * 0.5);
  14257. float hl = 0.0f, hr = 0.0f;
  14258. lynx::slayer::hostGains (lynx::slayer::midiPan (std::atan2 (0.25, 0.5) * (4.0 / 3.141592653589793) - 1.0), chanVol, hl, hr);
  14259. CHECK (lynx::slayer::midiPan (std::atan2 (0.25, 0.5) * (4.0 / 3.141592653589793) - 1.0) == -26);
  14260. double diff = 0.0, energy = 0.0;
  14261. for (int i = 0; i < 16384; ++i)
  14262. {
  14263. diff = std::max ({ diff, (double) std::abs (l[(std::size_t) i] - ref[2 * (std::size_t) (i + 1)] * hl),
  14264. (double) std::abs (r[(std::size_t) i] - ref[2 * (std::size_t) (i + 1) + 1] * hr) });
  14265. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  14266. }
  14267. CHECK (diff == 0.0 && energy > 1.0 && vel == 102);
  14268. // every preset of the installation: usable and sounding
  14269. int presets = 0, sounding = 0;
  14270. const auto folder = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "FL Slayer";
  14271. if (std::filesystem::exists (folder))
  14272. for (const auto& entry : std::filesystem::directory_iterator (folder))
  14273. for (const auto& s : pluginStatesIn (entry.path(), "FL Slayer"))
  14274. {
  14275. ++presets;
  14276. const auto y = lynx::slayer::renderReference (lynx::slayerStateParams (s), dll, { { 1024, 52, 100, 20000 } }, 44100.0, 22050);
  14277. double e = 0.0;
  14278. bool finite = true;
  14279. for (auto v : y)
  14280. finite = finite && std::isfinite (v), e += (double) v * v;
  14281. CHECK (finite);
  14282. sounding += e > 1.0 ? 1 : 0;
  14283. }
  14284. std::printf (" FL Slayer presets: %d, %d sounding\n", presets, sounding);
  14285. CHECK (presets == 64 && sounding == 64);
  14286. }
  14287. // FL Slayer's host levels and live parameters (REA_rodada79): channel pan / volume / pitch arrive as MIDI events
  14288. // (ProcessEvent 2/3/4), automation and settings edits as setParameter calls on the running engine
  14289. TEST (slayer_host_levels_and_automation)
  14290. {
  14291. using namespace lynx::slayer;
  14292. CHECK (pitchBend (0) == 8192 && pitchBend (100) == 8874 && pitchBend (-100) == 7510 && pitchBend (1200) == 0x3fff
  14293. && pitchBend (-1200) == 0 && pitchBend (-1199) == 7 && pitchBend (5000) == 0x3fff);
  14294. CHECK (midiPan (0.0) == 0 && midiPan (1.0) == 64 && midiPan (-1.0) == -64 && midiPan (-0.5) == -32
  14295. && midiPan (99.0 / 6400.0) == 0 && midiPan (-199.0 / 6400.0) == -1 && midiPan (2.0) == 64);
  14296. float l0 = 0.0f, r0 = 0.0f, l1 = 0.0f, r1 = 0.0f;
  14297. hostGains (0, 0.5f, l0, r0); // the front-end's own start values (volume 1 after the ×2, centre)
  14298. hostGains (64, 1.0f, l1, r1);
  14299. CHECK (l0 == 0.70710677f && r0 == 0.70710677f && std::abs (l1) < 1e-15f && r1 == 2.0f);
  14300. const auto dll = slayerDll();
  14301. if (dll.empty() || ! lynx::slayerDllUsable (dll))
  14302. return;
  14303. const auto st = slayerPreset ("DIST Dire Straits");
  14304. const auto params = lynx::slayerStateParams (st);
  14305. lynx::ChannelDef c;
  14306. c.instrument = lynx::InstrumentKind::slayer;
  14307. c.pluginState = st;
  14308. c.samplePath = dll;
  14309. c.sampler.pitch = 1.5f; // channel pitch +150 cents → bend 9216; no root shift
  14310. c.modelParams = { 0.0f };
  14311. auto inst = lynx::makeInstrument (lynx::InstrumentKind::slayer);
  14312. inst->configure (c, 120.0);
  14313. inst->prepare (44100.0);
  14314. const int blocks = 40, n = 512;
  14315. std::vector<float> l ((std::size_t) (blocks * n), 0.0f), r ((std::size_t) (blocks * n), 0.0f);
  14316. auto edited = st;
  14317. const std::int32_t newDrive = 60000; // parameter 13 (amp drive), edited in the settings at block 25
  14318. std::memcpy (edited.data() + 4 + 4 * 13, &newDrive, 4);
  14319. for (int b = 0; b < blocks; ++b)
  14320. {
  14321. if (b == 0)
  14322. inst->noteOn (52, 0.8f);
  14323. if (b == 10)
  14324. inst->setParameter (20, 9.0f / 15.0f); // automation: effect 9 (chorus)
  14325. if (b == 18)
  14326. inst->setParameter (28, 0.5f); // automation: the mod wheel → CC 1 64
  14327. if (b == 25)
  14328. {
  14329. c.pluginState = edited;
  14330. inst->configure (c, 120.0);
  14331. }
  14332. inst->render (l.data() + n * b, r.data() + n * b, n, 1.0f, 1.0f);
  14333. }
  14334. lynx::slayer::RefOptions opt;
  14335. opt.midi.push_back ({ 1, 0xe0, 9216 & 0x7f, 9216 >> 7 });
  14336. opt.params = { { 1 + 10 * n, 20, 9 }, { 1 + 18 * n, 28, 64 }, { 1 + 25 * n, 13, newDrive } };
  14337. const int vel = lynx::slayer::midiVelocity ((std::pow (11.0, 1.28 * 0.8) - 1.0) / 10.0);
  14338. const auto ref = lynx::slayer::renderReference (params, dll, { { 1, 52, vel, -1 } }, 44100.0, blocks * n + 1, opt);
  14339. float hl = 0.0f, hr = 0.0f;
  14340. hostGains (0, 1.0f, hl, hr);
  14341. double diff = 0.0, energy = 0.0;
  14342. for (int i = 0; i < blocks * n; ++i)
  14343. {
  14344. diff = std::max ({ diff, (double) std::abs (l[(std::size_t) i] - ref[2 * (std::size_t) (i + 1)] * hl),
  14345. (double) std::abs (r[(std::size_t) i] - ref[2 * (std::size_t) (i + 1) + 1] * hr) });
  14346. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  14347. }
  14348. CHECK (diff == 0.0 && energy > 1.0);
  14349. // the changes really change the sound: the same render without them differs
  14350. const auto plain = lynx::slayer::renderReference (params, dll, { { 1, 52, vel, -1 } }, 44100.0, blocks * n + 1);
  14351. CHECK (plain != ref);
  14352. }
  14353. TEST (slayer_and_fruity_dance_import)
  14354. {
  14355. // FL Slayer plays on the exact engine when the installation's engine DLL is there (otherwise it stays unsupported);
  14356. // Fruity Dance (a visual plugin) becomes a silent channel that keeps its state
  14357. std::vector<std::uint8_t> slayer (4 + 28 * 4, 0), dance (40, 1);
  14358. FlpBuilder b;
  14359. b.ascii (0xC7, "25.2.5.5319");
  14360. int index = 0;
  14361. for (const auto& [name, state] : { std::pair<std::u16string, std::vector<std::uint8_t>> { u"FL Slayer", slayer }, { u"Fruity Dance", dance } })
  14362. {
  14363. b.u8 (0x40); b.u16 ((std::uint16_t) index++);
  14364. b.u8 (0x15); b.u8 (2);
  14365. b.utf16 (0xC9, name);
  14366. b.data (0xD5, state);
  14367. }
  14368. const auto project = flp::parseProject (b.bytes);
  14369. const auto plain = lynx::importFlp (project);
  14370. CHECK (plain.channels.size() == 2u);
  14371. if (plain.channels.size() == 2u)
  14372. {
  14373. CHECK (plain.channels[0].instrument != lynx::InstrumentKind::slayer);
  14374. CHECK (plain.channels[1].instrument == lynx::InstrumentKind::fruitydance && plain.channels[1].pluginState == dance);
  14375. }
  14376. auto inst = lynx::makeInstrument (lynx::InstrumentKind::fruitydance);
  14377. inst->prepare (44100.0);
  14378. std::vector<float> l (1024, 0.0f), r (1024, 0.0f);
  14379. inst->noteOn (60, 1.0f);
  14380. inst->render (l.data(), r.data(), 1024, 1.0f, 1.0f);
  14381. CHECK (inst->idle() && std::all_of (l.begin(), l.end(), [] (float v) { return v == 0.0f; }));
  14382. const auto dll = slayerDll();
  14383. if (dll.empty() || ! lynx::slayerDllUsable (dll))
  14384. return;
  14385. lynx::FlpImportOptions withFiles;
  14386. withFiles.roots = { std::filesystem::path (realFlpDir).parent_path().string(), realFlpDir, "" };
  14387. withFiles.fileExists = [] (const std::string& path) {
  14388. auto local = path;
  14389. std::replace (local.begin(), local.end(), '\\', '/');
  14390. return std::filesystem::exists (local);
  14391. };
  14392. const auto song = lynx::importFlp (project, withFiles);
  14393. CHECK (song.channels.size() == 2u && song.channels[0].instrument == lynx::InstrumentKind::slayer && song.channels[0].pluginState == slayer
  14394. && ! song.channels[0].samplePath.empty());
  14395. }
  14396. #include "WaveAnalysisTests.inc"
  14397. #include "UiPolish2Tests.inc"
  14398. #include "UiToolbarTests.inc"
  14399. #include "LuxeVerbTests.inc"
  14400. #include "Parity172bTests.inc"
  14401. #include "ConvolverTests.inc"
  14402. // --- Newtone / Newtime (REA_rodada73) -----------------------------------------------------------------------------
  14403. namespace newtonetest
  14404. {
  14405. 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))); }
  14406. 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))); }
  14407. void record (std::vector<std::uint8_t>& b, int id, const std::vector<std::uint8_t>& d)
  14408. {
  14409. put32 (b, id);
  14410. put64 (b, (std::int64_t) d.size());
  14411. b.insert (b.end(), d.begin(), d.end());
  14412. }
  14413. // a state with our own layout reading (REA_rodada73 §2): options, the wrapper record around the engine XML, the source path
  14414. std::vector<std::uint8_t> state (int version, std::uint32_t flags, const std::string& xml, const std::string& source)
  14415. {
  14416. std::vector<std::uint8_t> b, r1, r2;
  14417. put32 (b, version);
  14418. put32 (r1, 727);
  14419. put32 (r1, 582);
  14420. put32 (r1, (std::int32_t) flags);
  14421. r1.resize (0x28, 0);
  14422. record (b, 1, r1);
  14423. std::vector<std::uint8_t> chunk { 'V', 'C', '2', '!' };
  14424. put32 (chunk, (std::int32_t) xml.size() + 1);
  14425. chunk.insert (chunk.end(), xml.begin(), xml.end());
  14426. chunk.push_back (0);
  14427. put32 (r2, -9);
  14428. r2.push_back (0x0c);
  14429. put32 (r2, -2);
  14430. put64 (r2, (std::int64_t) chunk.size());
  14431. put32 (r2, 0);
  14432. r2.insert (r2.end(), chunk.begin(), chunk.end());
  14433. record (b, 2, r2);
  14434. record (b, 3, std::vector<std::uint8_t> (source.begin(), source.end()));
  14435. record (b, 4, std::vector<std::uint8_t> (12, 0));
  14436. record (b, 5, std::vector<std::uint8_t> (16, 0));
  14437. record (b, 6, std::vector<std::uint8_t> (32, 0));
  14438. return b;
  14439. }
  14440. const char* xmlWithTrack (bool edited)
  14441. {
  14442. return edited ? "<NewToneProject NumOfTracks=\"1\"><Tracks><Trk_0 TrackNum=\"0\" Type=\"class CzplJPositionablePSOLASource\" "
  14443. "FilePath=\"C:\\tmp\\a&amp;b.wav\"><AudioObject SelfBPM=\"120.0\" OriginalFileLength=\"1000\" GlobalTune=\"0.0\">"
  14444. "<Pitch_0 fOrigPitch=\"60.0\" fCurPitch=\"60.0\" iOrigLength=\"500\" iCurLength=\"500\"/>"
  14445. "<Pitch_1 fOrigPitch=\"62.0\" fCurPitch=\"64.0\" iOrigLength=\"500\" iCurLength=\"500\"/></AudioObject></Trk_0></Tracks></NewToneProject>"
  14446. : "<NewToneProject NumOfTracks=\"1\"><Tracks><Trk_0 TrackNum=\"0\" Type=\"class CzplJTimelineWarpSource\" "
  14447. "FilePath=\"x.wav\"><AudioObject SelfBPM=\"80.0\" OriginalFileLength=\"1000\">"
  14448. "<Pitch_0 fOrigPitch=\"60.0\" fCurPitch=\"60.0\" fCurVolume=\"1.0\"/></AudioObject></Trk_0></Tracks></NewToneProject>";
  14449. }
  14450. } // namespace newtonetest
  14451. TEST (newtone_state)
  14452. {
  14453. using namespace newtonetest;
  14454. const auto a = lynx::newtonefx::parse (state (0, 5, xmlWithTrack (true), "%FLStudioUserData%\\vox.wav"));
  14455. CHECK (a.has_value());
  14456. CHECK (a->version == 0 && a->editorWidth == 727 && a->editorHeight == 582 && a->slave());
  14457. CHECK (a->sourcePath == "%FLStudioUserData%\\vox.wav");
  14458. CHECK (a->tracks.size() == 1 && a->tracks[0].notes == 2 && a->tracks[0].frames == 1000 && ! a->tracks[0].timeWarp);
  14459. CHECK (a->tracks[0].filePath == "C:\\tmp\\a&b.wav" && a->tracks[0].selfBpm == 120.0);
  14460. CHECK (a->tracks[0].edited && a->edited()); // note 1 moved from 62 to 64
  14461. const auto b = lynx::newtonefx::parse (state (1, 0x41, xmlWithTrack (false), ""));
  14462. CHECK (b.has_value() && ! b->slave() && b->sourcePath.empty());
  14463. CHECK (b->tracks.size() == 1 && b->tracks[0].timeWarp && ! b->edited());
  14464. // an empty project, as the installation's presets
  14465. const auto c = lynx::newtonefx::parse (state (0, 1, "<NewToneProject NumOfTracks=\"0\"><Tracks/></NewToneProject>", ""));
  14466. CHECK (c.has_value() && c->tracks.empty());
  14467. // broken: other version, truncated record, no options record
  14468. CHECK (! lynx::newtonefx::parse (state (2, 1, "", "")));
  14469. auto cut = state (0, 1, xmlWithTrack (true), "");
  14470. cut.resize (cut.size() - 3);
  14471. CHECK (! lynx::newtonefx::parse (cut));
  14472. CHECK (! lynx::newtonefx::parse ({ 0, 0, 0, 0 }));
  14473. // the FL import keeps the state and takes the slave switch as the editable parameter
  14474. const auto raw = state (0, 5, xmlWithTrack (false), "");
  14475. const auto slot = lynx::effectFromFl ("Newtone", raw, 120.0);
  14476. CHECK (slot && slot->kind == lynx::EffectKind::newtone && slot->state == raw && slot->params[0] == 1.0f);
  14477. const auto slot2 = lynx::effectFromFl ("Newtime", state (1, 0x41, xmlWithTrack (false), ""), 120.0);
  14478. CHECK (slot2 && slot2->kind == lynx::EffectKind::newtime && slot2->params[0] == 0.0f);
  14479. CHECK (lynx::formatEffectParam (lynx::EffectKind::newtone, 0, 1.0f, 120.0) == "on");
  14480. // the installation's own states (plugin database and the playlist's pitch/warp correction tracks) [DATA]
  14481. if (! realFlpDir.empty())
  14482. {
  14483. int found = 0, read = 0;
  14484. for (const char* rel : { "System/PitchCorrectionTrack.fst", "System/WarpCorrectionTrack.fst",
  14485. "Patches/Plugin database/Effects/Misc/Newtone.fst", "Patches/Plugin database/Effects/Misc/Newtime.fst" })
  14486. {
  14487. const auto path = std::filesystem::path (realFlpDir) / rel;
  14488. if (! std::filesystem::exists (path))
  14489. continue;
  14490. ++found;
  14491. std::ifstream f (path, std::ios::binary);
  14492. std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (f)), std::istreambuf_iterator<char>());
  14493. const auto fx = lynx::effectFromFlPreset (bytes, 120.0);
  14494. read += fx && (fx->kind == lynx::EffectKind::newtone || fx->kind == lynx::EffectKind::newtime) && ! fx->state.empty()
  14495. && fx->params[0] == 0.0f && lynx::newtonefx::parse (fx->state)->tracks.empty();
  14496. }
  14497. std::printf (" %d Newtone/Newtime states, %d read\n", found, read);
  14498. CHECK (read == found);
  14499. }
  14500. }
  14501. TEST (newtone_engine)
  14502. {
  14503. // REA_rodada73 §3-4 [EXEC]: the input passes unchanged unless slave mode is on and the song plays; then the source
  14504. // sample is added from the song time on (at the source's own rate)
  14505. auto sample = std::make_shared<lynx::SampleData>();
  14506. sample->sampleRate = 44100.0;
  14507. for (int i = 0; i < 3000; ++i)
  14508. sample->left.push_back (0.001f * (float) (i % 97) - 0.04f);
  14509. const double sr = 44100.0, bpm = 120.0;
  14510. const auto tps = lynx::ticksPerSample (bpm, sr);
  14511. auto fx = lynx::newtonefx::make (sample);
  14512. fx->prepare (sr);
  14513. lynx::EffectParams on {}, off {};
  14514. on[0] = 1.0f;
  14515. std::vector<float> l (256), r (256), in (256);
  14516. auto block = [&] (double tick, bool playing, const lynx::EffectParams& p) {
  14517. for (int i = 0; i < 256; ++i)
  14518. l[(std::size_t) i] = r[(std::size_t) i] = in[(std::size_t) i] = 0.25f * std::sin (0.01f * (float) (i + tick));
  14519. fx->setSongPosition (tick);
  14520. fx->setPlaying (playing);
  14521. fx->process (l.data(), r.data(), 256, p, bpm);
  14522. };
  14523. auto same = [&] { return std::equal (l.begin(), l.end(), in.begin()) && std::equal (r.begin(), r.end(), in.begin()); };
  14524. block (0.0, true, off);
  14525. CHECK (same()); // slave off
  14526. block (0.0, false, on);
  14527. CHECK (same()); // not playing
  14528. // playing from the start: in + source, sample by sample, over consecutive blocks
  14529. bool exact = true;
  14530. for (int b = 0; b < 4; ++b)
  14531. {
  14532. block (b * 256 * tps, true, on);
  14533. for (int i = 0; i < 256; ++i)
  14534. exact = exact && l[(std::size_t) i] == in[(std::size_t) i] + sample->left[(std::size_t) (b * 256 + i)]
  14535. && r[(std::size_t) i] == l[(std::size_t) i];
  14536. }
  14537. CHECK (exact);
  14538. // a jump re-seeks to the song time; past the end of the sample only the input remains
  14539. block (2000 * tps, true, on);
  14540. CHECK (l[0] == in[0] + sample->left[2000] && l[255] == in[255] + sample->left[2255]);
  14541. block (2900 * tps, true, on);
  14542. CHECK (l[99] == in[99] + sample->left[2999] && l[100] == in[100]);
  14543. // stopping and playing again: from the new song time
  14544. block (0.0, false, on);
  14545. CHECK (same());
  14546. block (10 * tps, true, on);
  14547. CHECK (l[0] == in[0] + sample->left[10]);
  14548. // without its sample: a pass-through
  14549. auto none = lynx::makeEffect (lynx::EffectKind::newtime);
  14550. none->prepare (sr);
  14551. for (int i = 0; i < 256; ++i)
  14552. l[(std::size_t) i] = r[(std::size_t) i] = in[(std::size_t) i] = 0.1f * (float) i / 256.0f;
  14553. none->setPlaying (true);
  14554. none->process (l.data(), r.data(), 256, on, bpm);
  14555. CHECK (same());
  14556. }
  14557. TEST (newtone_rack_follows_transport)
  14558. {
  14559. // the mixer gives the effect the transport, and keeps it running on a silent insert (no smart disable)
  14560. auto song = lynx::Song::makeDefault();
  14561. for (auto& p : song.patterns)
  14562. p.notes.clear();
  14563. song.bpm = 120.0;
  14564. auto slot = lynx::makeEffectSlot (lynx::EffectKind::newtone);
  14565. slot.params[0] = 1.0f;
  14566. slot.samplePath = "newtone-test.wav";
  14567. song.mixer[1].effects.push_back (slot);
  14568. auto sample = std::make_shared<lynx::SampleData>();
  14569. sample->sampleRate = 48000.0;
  14570. sample->left.assign (48000, 0.5f);
  14571. int loads = 0;
  14572. lynx::RackState::SampleProvider provider = [&] (const std::string& path) -> std::shared_ptr<const lynx::SampleData> {
  14573. ++loads;
  14574. return path == "newtone-test.wav" ? sample : nullptr;
  14575. };
  14576. const double sr = 48000.0;
  14577. auto state = lynx::RackState::build (song, nullptr, sr, provider);
  14578. CHECK (loads == 1);
  14579. lynx::RackRenderer rr;
  14580. rr.prepare (sr);
  14581. std::vector<float> l (512), r (512);
  14582. auto peakOf = [&] (bool playing, double tick) {
  14583. float peak = 0.0f;
  14584. for (int b = 0; b < 8; ++b)
  14585. {
  14586. rr.process (state.get(), playing, tick + b * 512 * lynx::ticksPerSample (song.bpm, sr), 512, l.data(), r.data());
  14587. for (auto v : l)
  14588. peak = std::max (peak, std::abs (v));
  14589. }
  14590. return peak;
  14591. };
  14592. CHECK (peakOf (false, 0.0) == 0.0f);
  14593. CHECK (peakOf (true, 0.0) > 0.1f);
  14594. }
  14595. #include "HardcoreTests.inc"
  14596. #include "DistructorTests.inc"
  14597. // --- missing files (core/MissingFiles.h) ------------------------------------------------------------------------
  14598. TEST (missing_files_names_and_scores)
  14599. {
  14600. CHECK (lynx::pathFileName ("C:\\Samples\\Drums\\Kick 01.wav") == "Kick 01.wav");
  14601. CHECK (lynx::pathFileName ("/home/u/loops/beat.flac") == "beat.flac");
  14602. CHECK (lynx::pathFileName ("D:/mixed\\sep/x.wav/") == "x.wav");
  14603. CHECK (lynx::pathFileName ("plain.wav") == "plain.wav");
  14604. CHECK (lynx::relocationScore ("C:\\Old\\Drums\\Kicks\\kick.wav", "/new/Drums/Kicks/KICK.WAV") == 3);
  14605. CHECK (lynx::relocationScore ("C:\\Old\\Drums\\Kicks\\kick.wav", "/new/other/kick.wav") == 1);
  14606. CHECK (lynx::relocationScore ("C:\\Old\\kick.wav", "/new/kick2.wav") == 0);
  14607. }
  14608. TEST (missing_files_find_in_song)
  14609. {
  14610. lynx::Song song;
  14611. lynx::ChannelDef a, b, c, d, e;
  14612. a.name = "Kick", a.instrument = lynx::InstrumentKind::sampler, a.samplePath = "C:\\gone\\kick.wav";
  14613. b.name = "Vox", b.instrument = lynx::InstrumentKind::audioClip, b.samplePath = "C:\\gone\\vox.wav";
  14614. c.name = "Here", c.instrument = lynx::InstrumentKind::sampler, c.samplePath = "C:\\here\\snare.wav";
  14615. d.name = "GMS", d.instrument = lynx::InstrumentKind::gms, d.samplePath = "C:\\FL\\gms waves"; // installation data: not listed
  14616. e.name = "Kick 2", e.instrument = lynx::InstrumentKind::sampler, e.samplePath = "c:/GONE/KICK.wav"; // same file, other spelling
  14617. lynx::SampleZone z;
  14618. z.path = "C:\\gone\\piano_c4.wav";
  14619. a.zones.push_back (z);
  14620. song.channels = { a, b, c, d, e };
  14621. song.mixer.resize (3);
  14622. lynx::EffectSlot fx;
  14623. fx.samplePath = "C:\\gone\\tune.wav";
  14624. lynx::EffectSlot parallel;
  14625. parallel.branches.push_back ({ { fx }, 1.0f });
  14626. song.mixer[2].effects.push_back (parallel);
  14627. song.mixer[2].name = "Lead";
  14628. const auto exists = [] (const std::string& p) { return p.find ("here") != std::string::npos || p.find ("FL") != std::string::npos; };
  14629. auto missing = lynx::findMissingSamples (song, exists);
  14630. CHECK (missing.size() == 4);
  14631. CHECK (missing.size() == 4 && missing[0].path == "C:\\gone\\kick.wav" && missing[0].usedBy.size() == 2 && missing[0].usedBy[1] == "Kick 2");
  14632. CHECK (missing.size() == 4 && missing[1].path == "C:\\gone\\piano_c4.wav" && missing[1].usedBy[0] == "Kick");
  14633. CHECK (missing.size() == 4 && missing[2].usedBy[0] == "Audio clip: Vox");
  14634. CHECK (missing.size() == 4 && missing[3].usedBy[0] == "Lead: effect");
  14635. // candidates from a search: the deeper path match wins; one stays missing
  14636. const std::vector<std::string> hits { "/lib/other/kick.wav", "/lib/gone/kick.wav", "/lib/x/vox.wav", "/lib/y/tune.wav" };
  14637. CHECK (lynx::relocateByName (missing, hits) == 3);
  14638. CHECK (missing[0].found == "/lib/gone/kick.wav");
  14639. CHECK (missing[1].found.empty());
  14640. missing[3].skipped = true; // skipped: not applied
  14641. const auto map = lynx::relocationMap (missing);
  14642. CHECK (map.size() == 2);
  14643. CHECK (lynx::applyRelocations (song, map) == 3); // both spellings of kick.wav + vox
  14644. CHECK (song.channels[0].samplePath == "/lib/gone/kick.wav" && song.channels[4].samplePath == "/lib/gone/kick.wav");
  14645. CHECK (song.channels[1].samplePath == "/lib/x/vox.wav");
  14646. CHECK (song.channels[2].samplePath == "C:\\here\\snare.wav");
  14647. CHECK (song.mixer[2].effects[0].branches[0].chain[0].samplePath == "C:\\gone\\tune.wav");
  14648. lynx::Song copy = song;
  14649. CHECK (lynx::applyRelocations (copy, { { "C:\\gone\\tune.wav", "/t.wav" } }) == 1 && copy.mixer[2].effects[0].branches[0].chain[0].samplePath == "/t.wav");
  14650. }
  14651. TEST (missing_files_inside_patcher)
  14652. {
  14653. // a Patcher's instruments and effects (any depth) are the song's samples too: listed, packed and relinked
  14654. lynx::Song song;
  14655. lynx::ChannelDef inner;
  14656. inner.name = "Pad", inner.instrument = lynx::InstrumentKind::sampler, inner.samplePath = "C:\\gone\\pad.wav";
  14657. lynx::ChannelDef nested;
  14658. nested.instrument = lynx::InstrumentKind::patcher;
  14659. nested.patcher.nodes.resize (1);
  14660. nested.patcher.nodes[0].generator = true;
  14661. nested.patcher.nodes[0].channel = inner;
  14662. lynx::ChannelDef patch;
  14663. patch.name = "Rack", patch.instrument = lynx::InstrumentKind::patcher;
  14664. patch.patcher.nodes.resize (2);
  14665. patch.patcher.nodes[0].generator = true;
  14666. patch.patcher.nodes[0].channel = nested;
  14667. patch.patcher.nodes[1].effect.samplePath = "C:\\gone\\tune.wav";
  14668. patch.patcher.nodes[1].busHeads.push_back ({});
  14669. patch.patcher.nodes[1].busHeads[0].head = lynx::EffectSlot();
  14670. patch.patcher.nodes[1].busHeads[0].head->samplePath = "C:\\gone\\tune.wav"; // same file: one entry
  14671. song.channels = { patch };
  14672. song.mixer.resize (2);
  14673. lynx::EffectSlot fxPatcher;
  14674. fxPatcher.patcher.nodes.resize (1);
  14675. fxPatcher.patcher.nodes[0].generator = true;
  14676. fxPatcher.patcher.nodes[0].channel = inner;
  14677. fxPatcher.patcher.nodes[0].channel.samplePath = "C:\\gone\\hit.wav";
  14678. song.mixer[1].effects.push_back (fxPatcher);
  14679. const auto missing = lynx::findMissingSamples (song, [] (const std::string&) { return false; });
  14680. CHECK (missing.size() == 3);
  14681. CHECK (missing.size() == 3 && missing[0].path == "C:\\gone\\pad.wav" && missing[0].usedBy[0] == "Rack (Patcher)");
  14682. CHECK (missing.size() == 3 && missing[1].path == "C:\\gone\\tune.wav" && missing[1].usedBy[0] == "Rack (Patcher): effect");
  14683. CHECK (missing.size() == 3 && missing[2].path == "C:\\gone\\hit.wav" && missing[2].usedBy[0] == "Insert 1 (Patcher)");
  14684. CHECK (lynx::applyRelocations (song, { { "C:\\gone\\pad.wav", "/p.wav" }, { "C:\\gone\\tune.wav", "/t.wav" }, { "C:\\gone\\hit.wav", "/h.wav" } }) == 4);
  14685. CHECK (song.channels[0].patcher.nodes[1].busHeads[0].head->samplePath == "/t.wav");
  14686. CHECK (song.channels[0].patcher.nodes[0].channel.patcher.nodes[0].channel.samplePath == "/p.wav");
  14687. CHECK (song.channels[0].patcher.nodes[1].effect.samplePath == "/t.wav");
  14688. CHECK (song.mixer[1].effects[0].patcher.nodes[0].channel.samplePath == "/h.wav");
  14689. }
  14690. TEST (missing_files_folder_search)
  14691. {
  14692. namespace fs = std::filesystem;
  14693. const auto root = fs::temp_directory_path() / ("lynx-missing-test-" + processTempTag());
  14694. fs::remove_all (root);
  14695. fs::create_directories (root / "a" / "deep" / "Kicks");
  14696. fs::create_directories (root / "b");
  14697. for (const auto& f : { root / "a" / "deep" / "Kicks" / "Kick.WAV", root / "b" / "kick.wav", root / "b" / "hat.wav", root / "b" / "unrelated.wav" })
  14698. std::ofstream (f) << "x";
  14699. std::vector<lynx::MissingFile> missing { { "C:\\Old\\Kicks\\kick.wav", { "Kick" }, {}, false }, { "C:\\Old\\hat.wav", { "Hat" }, {}, false },
  14700. { "C:\\Old\\ride.wav", { "Ride" }, {}, false } };
  14701. int listed = 0;
  14702. auto list = [&] (const std::string& folder, const std::function<bool (const std::string&, bool)>& onEntry) {
  14703. for (const auto& entry : fs::directory_iterator (folder))
  14704. {
  14705. ++listed;
  14706. if (! onEntry (entry.path().string(), entry.is_directory()))
  14707. return;
  14708. }
  14709. };
  14710. const auto hits = lynx::searchFolderForMissing (missing, root.string(), list);
  14711. CHECK (hits.size() == 3);
  14712. CHECK (lynx::relocateByName (missing, hits) == 2);
  14713. CHECK (missing[0].found.find ("Kicks") != std::string::npos); // the copy in a "Kicks" folder matches better
  14714. CHECK (missing[1].found.find ("hat.wav") != std::string::npos);
  14715. CHECK (missing[2].found.empty());
  14716. // a bounded walk stops early; a cancelled one finds nothing
  14717. CHECK (lynx::searchFolderForMissing (missing, root.string(), list, 2).size() <= 1);
  14718. CHECK (lynx::searchFolderForMissing (missing, root.string(), list, 0, [] { return true; }).empty());
  14719. // nothing left to find: no walk at all
  14720. missing[2].skipped = true;
  14721. listed = 0;
  14722. CHECK (lynx::searchFolderForMissing (missing, root.string(), list).empty() && listed == 0);
  14723. fs::remove_all (root);
  14724. }
  14725. TEST (missing_files_folder_list)
  14726. {
  14727. const auto folders = lynx::parseFolderList ("C:\\A\r\n C:\\B \n\nc:\\a\nD:/x\n");
  14728. CHECK (folders.size() == 3 && folders[0] == "C:\\A" && folders[1] == "C:\\B" && folders[2] == "D:/x");
  14729. auto list = folders;
  14730. lynx::rememberFolder (list, "D:\\x");
  14731. CHECK (list.size() == 3 && list[0] == "D:\\x" && list[1] == "C:\\A");
  14732. lynx::rememberFolder (list, "E:\\new", 2);
  14733. CHECK (list.size() == 2 && list[0] == "E:\\new" && list[1] == "D:\\x");
  14734. CHECK (lynx::parseFolderList (lynx::folderListText (list)) == list);
  14735. }
  14736. TEST (missing_files_flp_report_and_relocation)
  14737. {
  14738. // an .flp sampler whose sample is gone: listed with its channel; a relocation map makes the import use the copy
  14739. flp::Project project;
  14740. flp::Channel ch;
  14741. ch.index = 0;
  14742. ch.type = flp::ChannelType::sampler;
  14743. ch.name = "Snare";
  14744. ch.samplePath = "C:\\Producer\\Packs\\snare 7.wav";
  14745. project.channels.push_back (ch);
  14746. lynx::FlpImportOptions options;
  14747. options.fileExists = [] (const std::string& p) { return p == "/found/snare 7.wav"; };
  14748. lynx::FlpImportReport report;
  14749. options.report = &report;
  14750. auto song = lynx::importFlp (project, options);
  14751. CHECK (report.missingFiles.size() == 1);
  14752. CHECK (report.missingFiles.size() == 1 && report.missingFiles[0].path == "C:\\Producer\\Packs\\snare 7.wav" && report.missingFiles[0].usedBy[0] == "Snare");
  14753. CHECK (song.channels.size() == 1 && song.channels[0].samplePath.empty());
  14754. lynx::FlpImportReport again;
  14755. options.report = &again;
  14756. options.relocations = { { "C:\\Producer\\Packs\\snare 7.wav", "/found/snare 7.wav" } };
  14757. song = lynx::importFlp (project, options);
  14758. CHECK (again.missingFiles.empty());
  14759. CHECK (song.channels.size() == 1 && song.channels[0].samplePath == "/found/snare 7.wav" && song.channels[0].instrument == lynx::InstrumentKind::sampler);
  14760. }
  14761. TEST (project_info_history_and_flp_import)
  14762. {
  14763. lynx::Song a;
  14764. auto b = a;
  14765. b.info.title = "Night Drive";
  14766. CHECK (lynx::describeChangeText (a, b) == "Project info");
  14767. auto c = a;
  14768. c.beatsPerBar = 3;
  14769. CHECK (lynx::describeChangeText (a, c) == "Time signature");
  14770. flp::Project project;
  14771. project.title = "Song", project.artist = "Me", project.genre = "House", project.url = "https://x", project.comments = "line 1\rline 2";
  14772. const auto song = lynx::importFlp (project);
  14773. CHECK (song.info.title == "Song" && song.info.author == "Me" && song.info.genre == "House" && song.info.url == "https://x");
  14774. CHECK (song.info.comments == "line 1\nline 2");
  14775. }
  14776. #include "UiFixTests.inc"
  14777. // ---------------------------------------------------------------------------
  14778. // Piano Roll tools of round 83 (expected values worked out from FL's formulas, docs/analise/REA_rodada83.md)
  14779. namespace
  14780. {
  14781. lynx::Note prNote (std::int64_t start, std::int64_t length, int key = 60, float velocity = 100.0f / 128.0f)
  14782. {
  14783. lynx::Note n;
  14784. n.start = start;
  14785. n.length = length;
  14786. n.key = key;
  14787. n.velocity = velocity;
  14788. return n;
  14789. }
  14790. std::vector<int> allOf (const lynx::Pattern& p)
  14791. {
  14792. std::vector<int> v;
  14793. for (int i = 0; i < (int) p.notes.size(); ++i)
  14794. v.push_back (i);
  14795. return v;
  14796. }
  14797. } // namespace
  14798. TEST (prtools_score_file)
  14799. {
  14800. using namespace lynx::prtools;
  14801. FlpBuilder b (96);
  14802. b.ascii (0xC7, "20.9.0");
  14803. b.u8 (0x41); b.u16 (0);
  14804. auto notes = noteRecord (0, 0, 24, 60, 100);
  14805. auto n2 = noteRecord (48, 0, 24, 67, 64);
  14806. n2[19] = 4; // colour 4
  14807. notes.insert (notes.end(), n2.begin(), n2.end());
  14808. b.data (0xE0, notes);
  14809. const auto score = parseScore (b.bytes);
  14810. CHECK (score && score->notes.size() == 2);
  14811. if (score && score->notes.size() == 2)
  14812. {
  14813. CHECK (score->notes[0].start == 0 && score->notes[0].length == 240 && score->notes[0].key == 60);
  14814. CHECK (score->notes[1].start == 480 && score->notes[1].colour == 4 && score->notes[1].velocity == 0.5f);
  14815. CHECK (score->length() == 720);
  14816. }
  14817. CHECK (! parseScore (std::vector<std::uint8_t> { 1, 2, 3 }));
  14818. const auto grid = gridScore (240);
  14819. CHECK (grid.notes.size() == 4 && grid.length() == 960 && grid.notes[3].start == 720);
  14820. CHECK (gridScore (240, 1.0f).notes[1].start == 240 + 120);
  14821. if (! realFlpDir.empty()) // the installation's templates
  14822. {
  14823. std::ifstream f (realFlpDir + "/Patches/Scores/Quantization/Default.fsc", std::ios::binary);
  14824. std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (f)), {});
  14825. const auto fsc = parseScore (bytes);
  14826. CHECK (fsc && fsc->notes.size() == 16 && fsc->length() == 3840 && fsc->notes[1].start == 240);
  14827. // an old (FL 3.5, 20-byte records) chopping pattern with colour-3 notes, read as colour 12
  14828. std::ifstream g (realFlpDir + "/Patches/Scores/Chopping/Default.fsc", std::ios::binary);
  14829. std::vector<std::uint8_t> chopBytes ((std::istreambuf_iterator<char> (g)), {});
  14830. const auto chop = parseScore (chopBytes);
  14831. CHECK (chop && ! chop->notes.empty());
  14832. }
  14833. }
  14834. TEST (prtools_quantize)
  14835. {
  14836. using namespace lynx;
  14837. using namespace lynx::prtools;
  14838. const auto grid = gridScore (240);
  14839. Pattern p;
  14840. p.notes = { prNote (1010, 500), prNote (1130, 100) };
  14841. QuantizeOptions o; // start strength 64/128, sensitivity 36 (180 ticks)
  14842. quantize (p, allOf (p), grid, o);
  14843. CHECK (p.notes[0].start == 985 && p.notes[0].length == 500); // 1010 - (50 * 64) / 128
  14844. CHECK (p.notes[1].start == 1165); // toward 1200: 1130 + (70 * 64) / 128
  14845. p.notes = { prNote (1010, 500), prNote (1130, 100) };
  14846. o.start = 1.0f;
  14847. o.sensitivity = 12; // 60 ticks: 1130 is 70 away from 1200
  14848. quantize (p, allOf (p), grid, o);
  14849. CHECK (p.notes[0].start == 960 && p.notes[1].start == 1130);
  14850. p.notes = { prNote (1010, 500) };
  14851. o.sensitivity = 36;
  14852. o.durationMode = QuantizeOptions::leaveEnd;
  14853. quantize (p, allOf (p), grid, o);
  14854. CHECK (p.notes[0].start == 960 && p.notes[0].length == 550); // the end (1509) stays
  14855. p.notes = { prNote (1010, 500) };
  14856. o.durationMode = QuantizeOptions::quantizeEnd;
  14857. o.duration = 1.0f;
  14858. quantize (p, allOf (p), grid, o);
  14859. CHECK (p.notes[0].start == 960 && p.notes[0].length == 1439 - 960 + 1); // end 1509 -> the nearest end 1439
  14860. // levels: a template velocity of 50/100 halves the velocity; 0 removes the note
  14861. Score half = grid;
  14862. for (auto& n : half.notes)
  14863. n.velocity = 50.0f / 128.0f;
  14864. p.notes = { prNote (960, 100, 60, 0.8f) };
  14865. o = {};
  14866. quantize (p, allOf (p), half, o);
  14867. CHECK (std::abs (p.notes[0].velocity - 0.4f) < 1e-5f);
  14868. for (auto& n : half.notes)
  14869. n.velocity = 0.0f;
  14870. quantize (p, allOf (p), half, o);
  14871. CHECK (p.notes.empty());
  14872. }
  14873. TEST (prtools_chop)
  14874. {
  14875. using namespace lynx;
  14876. using namespace lynx::prtools;
  14877. Score pat;
  14878. pat.notes = { ScoreNote { 0, 240, 60 }, ScoreNote { 480, 240, 67 } }; // period 720
  14879. Pattern p;
  14880. p.notes = { prNote (0, 1920, 50) };
  14881. ChopOptions o;
  14882. chop (p, allOf (p), pat, o);
  14883. CHECK (p.notes.size() == 5);
  14884. if (p.notes.size() == 5)
  14885. {
  14886. CHECK (p.notes[0].start == 0 && p.notes[0].key == 50 && p.notes[1].start == 480 && p.notes[1].key == 57);
  14887. CHECK (p.notes[2].start == 720 && p.notes[3].start == 1200 && p.notes[4].start == 1440 && p.notes[4].length == 240);
  14888. }
  14889. // absolute: the pattern stays on the song grid; a piece before the note start is cut
  14890. p.notes = { prNote (100, 600, 50) };
  14891. chop (p, allOf (p), pat, o);
  14892. CHECK (p.notes.size() == 2 && p.notes[0].start == 100 && p.notes[0].length == 140 && p.notes[1].start == 480);
  14893. o.absolute = false;
  14894. p.notes = { prNote (100, 600, 50) };
  14895. chop (p, allOf (p), pat, o);
  14896. CHECK (p.notes.size() == 2 && p.notes[0].start == 100 && p.notes[0].length == 240 && p.notes[1].start == 580
  14897. && p.notes[1].length == 120);
  14898. // time mul x2 (wheel 5); colour 4 holds to the end in the first round only
  14899. o = {};
  14900. o.timeMul = 5;
  14901. p.notes = { prNote (0, 1920, 60) };
  14902. chop (p, allOf (p), pat, o);
  14903. CHECK (p.notes.size() == 3 && p.notes[1].start == 960 && p.notes[1].length == 480 && p.notes[2].start == 1440);
  14904. pat.notes[1].colour = 4;
  14905. o.timeMul = 4;
  14906. p.notes = { prNote (0, 1920, 60) };
  14907. chop (p, allOf (p), pat, o);
  14908. CHECK (p.notes.size() == 4 && p.notes[1].length == 1920 - 480 && p.notes[3].start == 1440);
  14909. }
  14910. TEST (prtools_flam)
  14911. {
  14912. using namespace lynx;
  14913. using namespace lynx::prtools;
  14914. Pattern p;
  14915. p.notes = { prNote (960, 480, 60, 0.75f) };
  14916. flam (p, allOf (p), flamPreset (FlamPreset::doubleStroke), 120.0); // +1 step after
  14917. CHECK (p.notes.size() == 2 && p.notes[0].start == 960 && p.notes[1].start == 1200 && p.notes[1].length == 480);
  14918. CHECK (std::abs (p.notes[1].velocity - 0.75f * 85.0f / 128.0f) < 1e-6f);
  14919. p.notes = { prNote (960, 480) };
  14920. flam (p, allOf (p), flamPreset (FlamPreset::earlyDoubleStroke), 120.0); // the flam one step before, shortened
  14921. CHECK (p.notes.size() == 2 && p.notes[0].start == 720 && p.notes[0].length == 240 && p.notes[1].start == 960);
  14922. // absolute: -64 = -33.3 ms = -64 ticks at 120 BPM (960 PPQ); the original moves, the flam stays on the grid
  14923. CHECK (flamOffset (flamPreset (FlamPreset::flam), 120.0) == -64);
  14924. p.notes = { prNote (960, 480) };
  14925. flam (p, allOf (p), flamPreset (FlamPreset::flam), 120.0);
  14926. CHECK (p.notes.size() == 2 && p.notes[0].start == 960 && p.notes[0].length == 64 && p.notes[1].start == 1024);
  14927. p.notes = { prNote (960, 480) };
  14928. flam (p, allOf (p), flamPreset (FlamPreset::earlyFlam), 120.0);
  14929. CHECK (p.notes.size() == 2 && p.notes[0].start == 896 && p.notes[1].start == 960 && p.notes[1].length == 480);
  14930. }
  14931. TEST (prtools_lfo)
  14932. {
  14933. using namespace lynx;
  14934. using namespace lynx::prtools;
  14935. CHECK (lfoPeriod (20480) == 3840.0 && lfoPeriod (0) == 960.0 * 128 && lfoPeriod (40960) == 120.0);
  14936. LfoOptions o; // sine, phase 1/2: starts at the bottom (value 1/2, range 1/4)
  14937. const auto c = lfoCurve (o, 3840);
  14938. 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);
  14939. o.shape = LfoOptions::square;
  14940. o.phase = 0; // phase 0.5: Round (0.5) = 0 (half to even) -> +1, then -1
  14941. const auto sq = lfoCurve (o, 3840);
  14942. CHECK (sq[0] == 0.75f && sq[10] == 0.25f && sq[2000] == 0.75f);
  14943. o = {};
  14944. o.useEnd = true;
  14945. o.endRange = 0; // the range fades out
  14946. const auto fade = lfoCurve (o, 3840);
  14947. CHECK (std::abs (fade[3839] - 0.5f) < 0.01f);
  14948. Pattern p;
  14949. p.notes = { prNote (0, 100), prNote (960, 100), prNote (1920, 100) };
  14950. lfo (p, allOf (p), notetools::Level::velocity, LfoOptions {});
  14951. CHECK (std::abs (p.notes[0].velocity - 0.25f) < 1e-4f && std::abs (p.notes[1].velocity - 0.5f) < 1e-3f
  14952. && std::abs (p.notes[2].velocity - 0.75f) < 1e-3f);
  14953. }
  14954. TEST (prtools_claw)
  14955. {
  14956. using namespace lynx;
  14957. using namespace lynx::prtools;
  14958. ClawOptions o; // beat, trash the 4th quarter, stretch, remove short notes
  14959. Pattern p;
  14960. p.notes = { prNote (0, 240), prNote (960, 240), prNote (750, 100), prNote (1440, 240) };
  14961. claw (p, allOf (p), o);
  14962. CHECK (p.notes.size() == 3); // the note inside the trashed slice is gone
  14963. if (p.notes.size() == 3)
  14964. {
  14965. CHECK (p.notes[0].start == 0 && p.notes[0].length == 320);
  14966. CHECK (p.notes[1].start == 960 && p.notes[1].length == 320);
  14967. CHECK (p.notes[2].start == 1600 && p.notes[2].length == 320); // 1440 - 240 = 1200 -> x 4/3
  14968. }
  14969. o.trash = 9; // none: only the warp, which is neutral
  14970. p.notes = { prNote (100, 300) };
  14971. claw (p, allOf (p), o);
  14972. CHECK (p.notes.size() == 1 && p.notes[0].start == 100 && p.notes[0].length == 300);
  14973. o.timeDistortion = 1024; // strong bend: the period's ends stay, the middle moves
  14974. p.notes = { prNote (0, 480), prNote (960, 480) };
  14975. claw (p, allOf (p), o);
  14976. CHECK (p.notes[0].start == 0 && p.notes[1].start == 960 && p.notes[0].length > 480 && p.notes[1].length > 480);
  14977. o.trash = 0; // the first quarter
  14978. o.timeDistortion = 0;
  14979. o.stretch = false;
  14980. p.notes = { prNote (0, 240), prNote (240, 240) };
  14981. claw (p, allOf (p), o);
  14982. CHECK (p.notes.size() == 1 && p.notes[0].start == 0 && p.notes[0].length == 240);
  14983. }
  14984. TEST (prtools_articulate)
  14985. {
  14986. using namespace lynx;
  14987. using namespace lynx::prtools;
  14988. Pattern p;
  14989. p.notes = { prNote (0, 100), prNote (480, 100), prNote (960, 100) };
  14990. articulate (p, allOf (p), articulatePreset (ArticulatePreset::legato), 4);
  14991. CHECK (p.notes[0].length == 480 && p.notes[1].length == 480 && p.notes[2].length == 3840 - 960); // to the bar line
  14992. p.notes = { prNote (0, 100), prNote (480, 100), prNote (960, 2880) };
  14993. articulate (p, allOf (p), articulatePreset (ArticulatePreset::staccato), 4);
  14994. CHECK (p.notes[0].length == 96 && p.notes[1].length == 96 && p.notes[2].length == 576); // ends on a bar: own length
  14995. auto o = articulatePreset (ArticulatePreset::legato);
  14996. o.gap = 20;
  14997. p.notes = { prNote (0, 100), prNote (480, 100) };
  14998. articulate (p, allOf (p), o, 4);
  14999. CHECK (p.notes[0].length == 460);
  15000. // chop chords: a held note is cut where the next one starts
  15001. p.notes = { prNote (0, 960, 48), prNote (480, 480, 60) };
  15002. articulate (p, allOf (p), articulatePreset (ArticulatePreset::chopChordsOnly), 4);
  15003. CHECK (p.notes.size() == 3 && p.notes[0].length == 480 && p.notes[1].start == 480 && p.notes[2].start == 480);
  15004. // variation is seeded (MT19937): same seed, same result; lengths only get shorter
  15005. o = articulatePreset (ArticulatePreset::legato);
  15006. o.variation = 50;
  15007. o.seed = 7;
  15008. Pattern a, b2;
  15009. a.notes = b2.notes = { prNote (0, 100), prNote (480, 100), prNote (960, 100) };
  15010. articulate (a, allOf (a), o, 4);
  15011. articulate (b2, allOf (b2), o, 4);
  15012. CHECK (a.notes[0].length == b2.notes[0].length && a.notes[0].length <= 480 && a.notes[0].length >= 240);
  15013. }
  15014. TEST (prtools_chords)
  15015. {
  15016. using namespace lynx::prtools;
  15017. auto c = parseProgression ("I-V-vi-IV", 0, false);
  15018. CHECK (c && c->size() == 4);
  15019. if (c && c->size() == 4)
  15020. {
  15021. CHECK ((*c)[0].root == 0 && (*c)[1].root == 7 && (*c)[2].root == 9 && (*c)[3].root == 5);
  15022. CHECK ((*c)[2].intervals == std::vector<int> ({ 0, 3, 7 }) && chordName ((*c)[2]) == "Am");
  15023. }
  15024. c = parseProgression ("ii 7-V 7-I \xE2\x88\x86-IV/\xE2\x99\xADVII", 0, false);
  15025. CHECK (c && c->size() == 4);
  15026. if (c && c->size() == 4)
  15027. {
  15028. CHECK ((*c)[0].intervals == std::vector<int> ({ 0, 3, 7, 10 }) && chordName ((*c)[0]) == "Dm7");
  15029. CHECK ((*c)[2].intervals == std::vector<int> ({ 0, 4, 7, 11 }) && chordName ((*c)[2]) == "Cmaj7");
  15030. CHECK ((*c)[3].root == 5 && (*c)[3].bass == 10 && chordName ((*c)[3]) == "F/A#");
  15031. }
  15032. c = parseProgression ("i-v-\xE2\x99\xADVI-iv", 9, true); // A minor: Am Em F Dm
  15033. CHECK (c && c->size() == 4 && (*c)[2].root == 5 && (*c)[3].root == 2 && (*c)[1].intervals[1] == 3);
  15034. CHECK (parseProgression ("Isus2 add9", 0, false) && parseProgression ("Isus2 add9", 0, false)->front().intervals
  15035. == std::vector<int> ({ 0, 2, 7, 14 }));
  15036. CHECK (! parseProgression ("I-X-V", 0, false));
  15037. 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");
  15038. CHECK (presets.size() == 3 && presets[0].numerals == "I-V-vi-IV" && ! presets[0].minor && presets[2].minor);
  15039. const auto rhythms = parseRhythmPresets ("Default,4,>,-,-,-,>,-,-,-,>,-,-,-,>,-,-,-\nRandom 1,4,r,16,2,1.5\n");
  15040. CHECK (rhythms.size() == 1 && rhythms[0].cells == ">--->--->--->---");
  15041. const auto g = generateProgression (0, false, 8, 0.3f, 5);
  15042. CHECK (g.size() == 8 && g[0].root == 0 && g == generateProgression (0, false, 8, 0.3f, 5));
  15043. for (std::uint32_t seed = 1; seed < 40; ++seed) // numerals read back as the same chords
  15044. {
  15045. const auto gen = generateProgression (2, seed % 2 == 0, 8, 1.0f, seed);
  15046. const auto back = parseProgression (toNumerals (gen, 2), 2, seed % 2 == 0);
  15047. CHECK (back && *back == gen);
  15048. }
  15049. CHECK (toNumerals (*parseProgression ("I-V-vi-IV/bVII", 0, false), 0) == "I-V-vi-IV/bVII");
  15050. lynx::Pattern p;
  15051. const auto chords = *parseProgression ("I-IV", 0, false);
  15052. ChordWriteOptions w;
  15053. const auto added = writeProgression (p, 2, 0, chords, w);
  15054. CHECK (added.size() == 8 && p.notes.size() == 8 && p.notes[0].channel == 2);
  15055. CHECK (std::any_of (p.notes.begin(), p.notes.end(), [] (auto& n) { return n.key == 48; })); // bass C3
  15056. w.rhythm = ">-`->-`->-`->-`-";
  15057. lynx::Pattern r;
  15058. writeProgression (r, 0, 0, chords, w);
  15059. CHECK (r.notes.size() == 8 && r.notes[0].length == 1920); // '>' + '-' of 960-tick cells
  15060. }
  15061. TEST (prtools_riff_machine)
  15062. {
  15063. using namespace lynx;
  15064. using namespace lynx::prtools;
  15065. Pattern p;
  15066. p.notes = { prNote (0, 100, 70) };
  15067. p.notes[0].channel = 1;
  15068. RiffOptions o;
  15069. o.channel = 1;
  15070. o.bars = 2;
  15071. const auto notes = riffMachine (p, { 0 }, o, 4);
  15072. CHECK (! notes.empty() && notes.size() == p.notes.size());
  15073. CHECK (std::all_of (p.notes.begin(), p.notes.end(), [] (auto& n) {
  15074. return n.channel == 1 && n.start >= 0 && n.start < 2 * 3840 && n.key >= 36 && n.key <= 84;
  15075. }));
  15076. // only the first step: the progression shape, chopped from one note at key 48
  15077. o.stepsUpTo = 0;
  15078. Pattern q;
  15079. const auto first = riffMachine (q, {}, o, 4);
  15080. CHECK (first.size() == builtInRiffProgression (0).notes.size() * 2 && q.notes[0].key == 48);
  15081. }
  15082. TEST (event_editor_lanes)
  15083. {
  15084. using namespace lynx;
  15085. Pattern p;
  15086. const AutomationTarget vol { AutomationTargetKind::channelVolume, 2 };
  15087. auto& lane = eventedit::laneFor (p, vol);
  15088. CHECK (p.events.size() == 1 && &eventedit::laneFor (p, vol) == &lane && eventedit::findLane (p, { AutomationTargetKind::channelPan, 2 }) == nullptr);
  15089. eventedit::fill (lane, 0, 960, 0.25f, 240);
  15090. CHECK (lane.events.size() == 4 && lane.events[3].tick == 720 && lane.events[3].value == 0.25f);
  15091. CHECK (lane.valueAt (-5.0) == 0.25f && lane.valueAt (5000.0) == 0.25f);
  15092. eventedit::line (lane, 960, 1920, 0.0f, 1.0f, 240); // 4 cells: 0, 1/3, 2/3, 1
  15093. CHECK (lane.events.size() == 8 && lane.events[4].value == 0.0f && lane.events[7].value == 1.0f);
  15094. CHECK (std::abs (lane.valueAt (1500.0) - 2.0f / 3.0f) < 1e-6f); // held from 1440
  15095. eventedit::setEvent (lane, 1440, 0.1f);
  15096. CHECK (lane.events.size() == 8 && lane.valueAt (1440.0) == 0.1f);
  15097. eventedit::setEvent (lane, 100, 0.9f);
  15098. CHECK (lane.events.size() == 9 && lane.events[1].tick == 100);
  15099. eventedit::scale (lane, 0, 960, 2.0f, 0.0f, false);
  15100. CHECK (lane.events[0].value == 0.5f && lane.events[1].value == 1.0f); // clamped
  15101. eventedit::scale (lane, 0, 960, 0.0f, 0.0f, true); // bipolar: toward the centre
  15102. CHECK (lane.events[0].value == 0.5f && lane.events[2].value == 0.5f);
  15103. eventedit::erase (lane, 0, 960);
  15104. CHECK (lane.events.size() == 4 && lane.events.front().tick == 960);
  15105. auto copy = lane;
  15106. eventedit::randomize (lane, 0, 4000, 0.2f, 3);
  15107. eventedit::randomize (copy, 0, 4000, 0.2f, 3);
  15108. CHECK (lane.events[1].value == copy.events[1].value && std::abs (lane.events[1].value - 1.0f / 3.0f) <= 0.2f + 1e-6f);
  15109. prtools::LfoOptions o;
  15110. eventedit::applyLfo (lane, 0, 3840, o, 960); // FL's LFO: bottom, middle, top, middle
  15111. 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);
  15112. p.events.push_back ({ { AutomationTargetKind::channelPan, 1 }, {} });
  15113. eventedit::removeEmptyLanes (p);
  15114. CHECK (p.events.size() == 1);
  15115. // undo label
  15116. auto before = Song::makeDefault();
  15117. auto after = before;
  15118. after.patterns[0].events.push_back ({ vol, { { 0, 0.5f } } });
  15119. CHECK (describeChangeText (before, after) == "Edit events: " + after.patterns[0].name);
  15120. // channel removal re-targets the lanes like automations
  15121. auto song = Song::makeDefault();
  15122. song.patterns[0].events.push_back ({ { AutomationTargetKind::channelVolume, 3 }, { { 0, 0.5f } } });
  15123. song.removeChannel (1);
  15124. CHECK (song.patterns[0].events[0].target.index == 2);
  15125. }
  15126. TEST (event_editor_playback)
  15127. {
  15128. // pattern events on the Master volume: silent first half of each pattern, sound in the second half
  15129. auto song = lynx::Song::makeDefault();
  15130. song.bpm = 120.0;
  15131. const auto bar = (std::int64_t) lynx::ppq * 4;
  15132. song.patterns[0].lengthTicks = bar;
  15133. song.patterns[0].notes.clear();
  15134. for (int s = 0; s < 16; ++s)
  15135. song.patterns[0].notes.push_back ({ s * 240, 200, 0, 60 });
  15136. song.patterns[0].events.push_back ({ { lynx::AutomationTargetKind::mixerVolume, 0 }, { { 0, 0.0f }, { bar / 2, 0.8f } } });
  15137. const auto peaks = [&] (lynx::PlayMode mode) {
  15138. song.mode = mode;
  15139. song.playlist.clear();
  15140. song.playlist.push_back ({ 0, 0, bar, bar, 0 }); // one clip, in the second bar
  15141. const double sr = 48000.0;
  15142. auto state = lynx::RackState::build (song, nullptr, sr);
  15143. lynx::RackRenderer r;
  15144. r.prepare (sr, 512);
  15145. std::vector<float> l (512), rr (512);
  15146. const auto tps = lynx::ticksPerSample (song.bpm, sr);
  15147. std::array<float, 6> peak {}; // half bars
  15148. for (double tick = 0.0; tick < (double) (bar * 3); tick += 512 * tps)
  15149. {
  15150. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  15151. // blocks inside a half bar, away from its edges (the gain glides over one chunk at a change)
  15152. const auto blockEnd = tick + 512 * tps;
  15153. const auto half = (std::size_t) (tick / (double) (bar / 2));
  15154. const auto edge = (double) half * (double) (bar / 2);
  15155. if (half > 5 || tick - edge < 2 * 512 * tps || blockEnd > edge + (double) (bar / 2))
  15156. continue;
  15157. for (auto v : l)
  15158. peak[half] = std::max (peak[half], std::abs (v));
  15159. }
  15160. return std::make_pair (state->eventLanes.size(), peak);
  15161. };
  15162. const auto [lanesSong, song3] = peaks (lynx::PlayMode::song);
  15163. CHECK (lanesSong == 1);
  15164. CHECK (song3[2] < 1.0e-4f && song3[3] > 0.01f); // inside the clip: silent, then sound
  15165. const auto [lanesPattern, loop] = peaks (lynx::PlayMode::pattern);
  15166. CHECK (lanesPattern == 1);
  15167. CHECK (loop[0] < 1.0e-4f && loop[1] > 0.01f && loop[2] < 1.0e-4f && loop[3] > 0.01f);
  15168. }
  15169. TEST (flp_pattern_events_import)
  15170. {
  15171. // REA_rodada85 §1: event 0xDF holds a pattern's events {u32 tick, u32 controlID, i32 raw value}, 0xD8 the values
  15172. // reset when the song starts; they become pattern event lanes in FL's normalised scale
  15173. FlpBuilder b;
  15174. b.u8 (0x40); b.u16 (0); // channel 0
  15175. b.u8 (0x15); b.u8 (0); // sampler
  15176. b.u8 (0x41); b.u16 (1); // pattern 1: events only, no notes
  15177. auto rec = [] (std::vector<std::uint8_t>& v, std::uint32_t tick, std::uint32_t id, std::int32_t value) {
  15178. for (auto w : { tick, id, (std::uint32_t) value })
  15179. for (int i = 0; i < 4; ++i)
  15180. v.push_back ((std::uint8_t) (w >> (8 * i)));
  15181. };
  15182. std::vector<std::uint8_t> events;
  15183. rec (events, 0, 0x70001FC0u, 16000); // Master volume: top
  15184. rec (events, 96, 0x70001FC0u, 8000);
  15185. rec (events, 96, 0x70001FC0u, 4000); // same tick: the last one wins
  15186. rec (events, 0, 0x00000001u, 0); // channel 0 pan: hard left
  15187. rec (events, 48, 0x00000001u, 12800); // hard right
  15188. rec (events, 0, 0x00008000u, 5); // a plugin parameter of a sampler: scale unknown, skipped
  15189. b.data (0xDF, events);
  15190. std::vector<std::uint8_t> init;
  15191. rec (init, 0, 0x70001FC1u, -6400); // Master pan reset to hard left at song start
  15192. b.data (0xD8, init);
  15193. const auto project = flp::parseProject (b.bytes);
  15194. CHECK (project.initControls.size() == 1);
  15195. const flp::Pattern* src = nullptr;
  15196. for (const auto& p : project.patterns)
  15197. if (p.index == 1)
  15198. src = &p;
  15199. CHECK (src != nullptr && src->controlEvents.size() == 6);
  15200. lynx::FlpImportReport report;
  15201. lynx::FlpImportOptions options;
  15202. options.report = &report;
  15203. const auto song = lynx::importFlp (project, options);
  15204. const lynx::PatternEvents* volume = nullptr;
  15205. const lynx::PatternEvents* pan = nullptr;
  15206. int lanes = 0;
  15207. for (const auto& p : song.patterns)
  15208. for (const auto& lane : p.events)
  15209. {
  15210. ++lanes;
  15211. if (lane.target.kind == lynx::AutomationTargetKind::mixerVolume && lane.target.index == 0)
  15212. volume = &lane;
  15213. if (lane.target.kind == lynx::AutomationTargetKind::channelPan)
  15214. pan = &lane;
  15215. }
  15216. CHECK (lanes == 2 && volume != nullptr && pan != nullptr);
  15217. if (volume != nullptr && volume->events.size() == 2)
  15218. {
  15219. CHECK (volume->events[0].tick == 0 && volume->events[1].tick == lynx::ppq);
  15220. CHECK (volume->events[0].value > volume->events[1].value); // 4000 (not 8000) is the one kept
  15221. }
  15222. else
  15223. CHECK (false);
  15224. if (pan != nullptr && pan->events.size() == 2)
  15225. CHECK (pan->events[0].value < 0.01f && pan->events[1].value > 0.99f && pan->events[1].tick == lynx::ppq / 2);
  15226. else
  15227. CHECK (false);
  15228. CHECK (report.converted["Pattern events"] == 2);
  15229. CHECK (song.targetValue ({ lynx::AutomationTargetKind::mixerPan, 0 }) < 0.01f);
  15230. }
  15231. #include "PrefsTests.inc"
  15232. #include "core/ProjectSafety.h"
  15233. #include "core/SearchIndex.h"
  15234. #include "core/PluginCatalog.h"
  15235. #include "SafetyTests118.inc"
  15236. #include "Rea134Tests.inc"
  15237. #include "Rea139Tests.inc"
  15238. #include "Rea143Tests.inc"
  15239. #include "Rea143aTests.inc"
  15240. #include "Rea143bTests.inc"
  15241. #include "Rea146aTests.inc"
  15242. #include "Rea146bTests.inc"
  15243. #include "Rea146Tests.inc"
  15244. #include "Rea149Tests.inc"
  15245. #include "PluginCatalogTests151.inc"
  15246. #include "Rea151Tests.inc"
  15247. #include "AudioEditTests.inc"
  15248. #include "AudioEditToolsTests.inc"
  15249. #include "InputTests.inc"
  15250. #include "../asio/InputDrift.h"
  15251. #include "AsioDriftTests.inc"
  15252. TEST (level_audit_demo_imports_round89)
  15253. {
  15254. // REA_rodada89 §4: two demo projects whose FL renders were much louder than LYNX's
  15255. if (realFlpDir.empty())
  15256. return;
  15257. auto load = [] (const std::filesystem::path& path) -> std::optional<lynx::Song> {
  15258. if (! std::filesystem::exists (path))
  15259. return std::nullopt;
  15260. std::ifstream in (path, std::ios::binary);
  15261. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  15262. return lynx::importFlp (flp::parseProject (file));
  15263. };
  15264. const auto demos = std::filesystem::path (realFlpDir) / "Demo projects";
  15265. // FL 12+: a route to insert 100+ without a level record plays at 100% (the percussion bus of "Lightning")
  15266. if (const auto song = load (demos / "Product demos" / "Square a Saw - Lightning (VEELA Vocals Resynthesized).flp"))
  15267. {
  15268. int highRoutes = 0, highAtZero = 0;
  15269. for (const auto& t : song->mixer)
  15270. for (const auto& send : t.sends)
  15271. if (send.target >= 100)
  15272. {
  15273. ++highRoutes;
  15274. highAtZero += send.level <= 0.0f;
  15275. }
  15276. std::printf (" Lightning: %d routes to inserts 100+, %d at level 0\n", highRoutes, highAtZero);
  15277. CHECK (highRoutes > 0 && highAtZero == 0);
  15278. }
  15279. // Fruity Compressors FL saved as VST2 inside Fruity Wrapper (RawFL): the exact engine with the saved controls
  15280. if (const auto song = load (demos / "Visual" / "ZGameEditor Visualizer" / "Treben - Gimbal and Sinan - RawFL.flp"))
  15281. {
  15282. int compressors = 0, withSettings = 0;
  15283. for (const auto& t : song->mixer)
  15284. for (const auto& fx : t.effects)
  15285. if (fx.kind == lynx::EffectKind::fruityCompressor)
  15286. {
  15287. ++compressors;
  15288. const auto defaults = lynx::makeEffectSlot (lynx::EffectKind::fruityCompressor);
  15289. withSettings += fx.params != defaults.params;
  15290. }
  15291. std::printf (" RawFL: %d wrapped Fruity Compressors on the exact engine, %d with their own settings\n", compressors, withSettings);
  15292. CHECK (compressors >= 10 && withSettings >= 8);
  15293. }
  15294. }
  15295. #include "UiLook90Tests.inc"
  15296. #include "UiFl104Tests.inc"
  15297. #include "DialogsTests84.inc"
  15298. #include "SubstituteTests.inc"
  15299. #include "MixSuiteTests96.inc"
  15300. #include "PanelsTests105.inc"
  15301. // --- native plugin editors (round 93): FL's arrangements ---------------------------------------------------------
  15302. TEST (plugin_layouts_name_real_parameters)
  15303. {
  15304. using namespace lynx;
  15305. // every value control edits a parameter of its effect, inside the body; FL arrangements place (or list in a menu)
  15306. // every parameter of the effect
  15307. for (const auto kind : plugui::flLayoutKinds())
  15308. {
  15309. const auto& l = plugui::layoutFor (kind);
  15310. const auto& info = effectInfo (kind);
  15311. CHECK (plugui::hasFlLayout (kind));
  15312. // FL's own size wins for its one-cell editors (Fruity Center, Fruity Phase Inverter: 170 x 55)
  15313. const auto minHeight = kind == EffectKind::flCenter || kind == EffectKind::flPhaseInverter ? 50 : 60;
  15314. if (! (l.width > 100 && l.height > minHeight))
  15315. std::printf (" %s: %d x %d\n", info.name, l.width, l.height);
  15316. CHECK (l.width > 100 && l.height > minHeight);
  15317. std::vector<bool> placed ((std::size_t) plugui::paramCount (kind), false);
  15318. for (const auto& item : l.items)
  15319. {
  15320. 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))
  15321. std::printf (" %s: item '%s' leaves the body\n", info.name, item.param != nullptr ? item.param : item.label != nullptr ? item.label : "");
  15322. 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);
  15323. if (item.param != nullptr)
  15324. {
  15325. const auto p = plugui::paramIndex (kind, item.param);
  15326. CHECK (p >= 0);
  15327. if (p >= 0)
  15328. placed[(std::size_t) p] = true;
  15329. }
  15330. if (item.type == plugui::Item::radio || item.type == plugui::Item::tabs || item.type == plugui::Item::menu)
  15331. {
  15332. const auto labels = plugui::optionLabels (item);
  15333. CHECK (labels.size() >= 2);
  15334. CHECK (item.values.empty() || item.values.size() == labels.size());
  15335. }
  15336. if (item.type == plugui::Item::knob && item.h + 0.5f < item.w) // (a caption above its knob would hide it)
  15337. {
  15338. std::printf (" %s: knob '%s' is shorter than it is wide\n", info.name, item.param != nullptr ? item.param : "");
  15339. CHECK (false);
  15340. }
  15341. CHECK (item.param2 == nullptr || plugui::paramIndex (kind, item.param2) >= 0);
  15342. if (item.param2 != nullptr && plugui::paramIndex (kind, item.param2) >= 0)
  15343. placed[(std::size_t) plugui::paramIndex (kind, item.param2)] = true;
  15344. for (const auto* when : { item.whenParam, item.when2Param, item.when3Param })
  15345. CHECK (when == nullptr || plugui::paramIndex (kind, when) >= 0);
  15346. }
  15347. for (const auto* name : l.menuSwitches)
  15348. {
  15349. const auto p = plugui::paramIndex (kind, name);
  15350. CHECK (p >= 0);
  15351. if (p >= 0)
  15352. placed[(std::size_t) p] = true;
  15353. }
  15354. for (const auto* name : l.editedElsewhere)
  15355. {
  15356. const auto p = plugui::paramIndex (kind, name);
  15357. CHECK (p >= 0);
  15358. if (p >= 0)
  15359. placed[(std::size_t) p] = true;
  15360. }
  15361. for (std::size_t p = 0; p < placed.size(); ++p)
  15362. if (! placed[p])
  15363. {
  15364. std::printf (" %s: parameter '%s' has no control\n", info.name, plugui::paramName (kind, (int) p));
  15365. CHECK (false);
  15366. }
  15367. }
  15368. }
  15369. TEST (maximus_editor_edits_the_slot_state)
  15370. {
  15371. using namespace lynx;
  15372. // the Maximus page edits the 59 parameters stored in the slot's state (REA_rodada8 §1.2)
  15373. std::array<std::array<int, 13>, 4> bands {};
  15374. for (auto& b : bands)
  15375. b = { 0, 1000, 1000, 0, 0, 1000, 1115, 0, 2, 5000, 3, 2882, 0 };
  15376. std::array<std::vector<std::array<double, 4>>, 8> envs;
  15377. for (auto& e : envs)
  15378. e = { { 0.0, 0.0, 0.0, 2 }, { 0.5, 0.5, 0.0, 2 }, { 0.5, 0.5, 0.0, 2 } };
  15379. const auto state = maximusTestState ({ 19800, 1, 48065, 1, 1115, 1000, 32768 }, bands, envs, 0);
  15380. const auto slot = maximusfx::pack ({ hlfx::MaximusBuild::shared, -1, state });
  15381. CHECK (maximusfx::stateParams().size() == 59);
  15382. CHECK (plugui::paramIndex (EffectKind::maximus, "Master release") == 7 + 13 * 3 + 9);
  15383. CHECK (maximusfx::stateParam (slot, 0).value_or (-1) == 19800);
  15384. CHECK (maximusfx::stateParam (slot, 7 + 13 + 9).value_or (-1) == 5000);
  15385. CHECK (maximusfx::stateParamText (0, 19800) == "200 Hz");
  15386. CHECK (maximusfx::stateParamText (2, 48065) == "3000 Hz");
  15387. CHECK (maximusfx::stateParamText (8, 1000) == "+0.0 dB");
  15388. CHECK (maximusfx::stateParamText (7 + 6, 0) == "Default");
  15389. CHECK (maximusfx::stateParamText (7 + 13 * 3 + 6, 1115) == "2.00 ms");
  15390. CHECK (maximusfx::stateParamText (6, -1) == "Off");
  15391. const auto edited = maximusfx::withStateParam (slot, 7 + 13 + 1, 2000); // mid pre gain → +18 dB
  15392. CHECK (maximusfx::stateParam (edited, 7 + 13 + 1).value_or (-1) == 2000);
  15393. CHECK (maximusfx::stateParam (edited, 7 + 1).value_or (-1) == 1000);
  15394. CHECK (maximusfx::stateParam (maximusfx::withStateParam (slot, 0, 99999), 0).value_or (-1) == 65536); // clamped
  15395. const auto blob = maximusfx::unpack (edited);
  15396. const auto original = maximusfx::unpack (slot);
  15397. CHECK (blob && original && (hlfx::makeMaximusPreset (blob->state) != nullptr) == (hlfx::makeMaximusPreset (original->state) != nullptr));
  15398. const auto curve = maximusfx::curvePoints (slot, 3);
  15399. CHECK (curve.size() == 3 && std::abs (curve[1].first - 0.5f) < 1e-6f && std::abs (curve[2].first - 1.0f) < 1e-6f);
  15400. CHECK (maximusfx::selectedBand (slot) >= 0);
  15401. CHECK (plugui::hasFlLayout (EffectKind::maximus));
  15402. }
  15403. TEST (plugin_layout_options_match_the_engine_values)
  15404. {
  15405. using namespace lynx;
  15406. // the switches show FL's captions for the engine's own values
  15407. const auto text = [] (EffectKind kind, const char* param, int option) {
  15408. for (const auto& item : plugui::layoutFor (kind).items)
  15409. if (item.param != nullptr && std::strcmp (item.param, param) == 0 && item.options != nullptr)
  15410. return formatEffectParam (kind, plugui::paramIndex (kind, param), plugui::optionValue (item, option), 120.0);
  15411. return std::string ("?");
  15412. };
  15413. CHECK (text (EffectKind::fruityDelay3, "Delay type", 0) == "Fake stereo");
  15414. CHECK (text (EffectKind::fruityDelay3, "Delay type", 2) == "Ping pong");
  15415. CHECK (text (EffectKind::fruityDelay3, "Delay type", 3) == "Off");
  15416. CHECK (text (EffectKind::fruityDelay3, "Filter mode", 1) == "High pass");
  15417. CHECK (text (EffectKind::fruityDelay3, "Saturation type", 1) == "Saturate");
  15418. CHECK (text (EffectKind::fruityMultiband, "Low mode", 1) == "off");
  15419. CHECK (text (EffectKind::fruityMultiband, "High mode", 2) == "bypass");
  15420. CHECK (text (EffectKind::luxeVerb, "Pitch type", 1) == "feed-forward");
  15421. CHECK (text (EffectKind::luxeVerb, "Freeze mode", 2) == "sustain");
  15422. CHECK (text (EffectKind::stereoEnhancer, "Invert", 2) == "invert R");
  15423. CHECK (text (EffectKind::parametricEq, "B3 type", 4) == formatEffectParam (EffectKind::parametricEq, peq::param (2, 0), peq::typeValue (peq::BandType::lowCut), 120.0));
  15424. // the option nearest to a value; pages and conditions
  15425. plugui::LayoutItem radio;
  15426. radio.type = plugui::Item::radio;
  15427. radio.options = "A|B|C|D";
  15428. CHECK (plugui::optionLabels (radio).size() == 4);
  15429. CHECK (plugui::optionAt (radio, 0.30f) == 1);
  15430. CHECK (plugui::optionAt (radio, 0.95f) == 3);
  15431. CHECK (std::abs (plugui::optionValue (radio, 2) - 2.0f / 3.0f) < 1e-6f);
  15432. const auto& lux = plugui::layoutFor (EffectKind::luxeVerb);
  15433. auto params = makeEffectSlot (EffectKind::luxeVerb).params;
  15434. const auto pitchType = plugui::paramIndex (EffectKind::luxeVerb, "Pitch type");
  15435. int feedForward = 0, feedback = 0;
  15436. for (float v : { 0.0f, 1.0f })
  15437. {
  15438. params[(std::size_t) pitchType] = v;
  15439. for (const auto& item : lux.items)
  15440. if (item.param != nullptr && std::strcmp (item.param, "Feed-forward pitch 1") == 0 && plugui::isShown (EffectKind::luxeVerb, item, 0, params))
  15441. ++feedForward;
  15442. else if (item.param != nullptr && std::strcmp (item.param, "Feedback pitch") == 0 && plugui::isShown (EffectKind::luxeVerb, item, 0, params))
  15443. ++feedback;
  15444. }
  15445. CHECK (feedForward == 1); // only in feed-forward mode
  15446. CHECK (feedback == 2); // value plate + fader, only in feedback mode
  15447. const auto& limiter = plugui::layoutFor (EffectKind::fruityLimiter);
  15448. CHECK (limiter.pages.size() == 2);
  15449. int ceilingShown = 0;
  15450. for (int page = 0; page < 2; ++page)
  15451. for (const auto& item : limiter.items)
  15452. if (item.param != nullptr && std::strcmp (item.param, "Ceiling") == 0 && plugui::isShown (EffectKind::fruityLimiter, item, page, params))
  15453. ++ceilingShown;
  15454. CHECK (ceilingShown == 1); // LIMIT page only
  15455. // every other effect still gets a (generated) window
  15456. CHECK (! plugui::hasFlLayout (EffectKind::distortion));
  15457. CHECK (! plugui::layoutFor (EffectKind::distortion).items.empty());
  15458. }
  15459. #include "FabMeasureTests99.inc"
  15460. #include "DynamicsTests108.inc"
  15461. #include "Crest173dTests.inc"
  15462. #include "LynxTuneTests95.inc"
  15463. #include "LynxVerbTests100.inc"
  15464. #include "FacetTests103.inc"
  15465. #include "VocalLevel107Tests.inc"
  15466. #include "Substitutes113Tests.inc"
  15467. #include "Tune120Tests.inc"
  15468. #include "EchoSuiteTests101.inc"
  15469. #include "FruitySendTests105.inc"
  15470. #include "CrashFuzzTests111.inc"
  15471. #include "LookTests114.inc"
  15472. #include "FixedBlockTests118.inc"
  15473. #include "UiFl115Tests.inc"
  15474. #include "LoadTraceTests112.inc"
  15475. #include "VoiceToolsTests119.inc"
  15476. #include "VoiceMatchTests123.inc"
  15477. #include "LoadTests121.inc"
  15478. #include "Parity128Tests.inc"
  15479. #include "Parity128aTests.inc"
  15480. #include "Parity128bTests.inc"
  15481. #include "Parity128cTests.inc"
  15482. #include "Match124Tests.inc"
  15483. #include "Match124xTests.inc"
  15484. #include "Vmatch127Tests.inc"
  15485. #include "ParityTests129.inc"
  15486. #include "Parity135vTests.inc"
  15487. #include "Bugs131Tests.inc"
  15488. #include "Parity135wTests.inc"
  15489. #include "Feat133Tests.inc"
  15490. #include "Rea165fTests.inc"
  15491. #include "Parity135fTests.inc"
  15492. #include "Feat132Tests.inc"
  15493. #include "Parity135sTests.inc"
  15494. #include "VoiceMatch136Tests.inc"
  15495. #include "Parity137Tests.inc"
  15496. #include "Parity144Tests.inc"
  15497. #include "Parity144aTests.inc"
  15498. #include "Parity144bTests.inc"
  15499. #include "Parity147Tests.inc"
  15500. #include "Parity147aTests.inc"
  15501. #include "Parity147bTests.inc"
  15502. #include "Parity150Tests.inc"
  15503. #include "Parity150aTests.inc"
  15504. #include "Parity150bTests.inc"
  15505. #include "Parity152Tests.inc"
  15506. #include "Tp172dTests.inc"
  15507. #include "Parity152aTests.inc"
  15508. #include "Parity152bTests.inc"
  15509. #include "Parity152cTests.inc"
  15510. #include "Parity165Tests.inc"
  15511. #include "Parity165wTests.inc"
  15512. #include "Parity165xTests.inc"
  15513. #include "Parity165aTests.inc"
  15514. #include "Patcher141Tests.inc"
  15515. #include "Patcher162Tests.inc"
  15516. #include "Vox138Tests.inc"
  15517. #include "Vox145Tests.inc"
  15518. #include "Vox148Tests.inc"
  15519. #include "Natives142Tests.inc"
  15520. TEST (emphasizer_matches_fl_engine)
  15521. {
  15522. // REA_rodada154: FL's Emphasizer through the import (state → slot → effect) against goldens of the installation's
  15523. // Emphasizer_x64.dll (rate set before the state, 2304 samples of silence as FL's mixer runs it before a render,
  15524. // 512-frame calls, 44.1 kHz): FNV-1a of the output's float bits
  15525. auto signal = [] {
  15526. std::vector<float> l (24000), r (24000);
  15527. std::uint32_t s = 12345;
  15528. const float amps[] { 0.3f, 1.5f, 0.05f, 3.0f, 0.8f, 0.0f, 2.0f, 0.4f };
  15529. for (int i = 0; i < 24000; ++i)
  15530. {
  15531. const float a = amps[(i / 3000) % 8];
  15532. s = s * 1664525u + 1013904223u;
  15533. l[(std::size_t) i] = (float) ((s >> 8) / 8388608.0 - 1.0) * a;
  15534. s = s * 1664525u + 1013904223u;
  15535. r[(std::size_t) i] = (float) ((s >> 8) / 8388608.0 - 1.0) * a;
  15536. }
  15537. return std::pair { l, r };
  15538. };
  15539. struct Case { std::array<int, 6> raw; std::uint64_t hash; };
  15540. const Case cases[] { { { 0, 5013, 0, 0, 0, 10000 }, 0xa009608f060eb32eull }, { { 6000, 9000, 1, 0, 0, 8000 }, 0x6fc7ec82cc9d9c01ull },
  15541. { { 8000, 2000, 0, 1, 0, 10000 }, 0xb10a1f3bd998198cull }, { { 3000, 5013, 1, 0, 1, 10000 }, 0x57a8fa4d8b890ac0ull } };
  15542. for (const auto& c : cases)
  15543. {
  15544. std::vector<std::uint8_t> state (4 + 24 + 1, 0);
  15545. const std::int32_t version = 2;
  15546. std::memcpy (state.data(), &version, 4);
  15547. std::memcpy (state.data() + 4, c.raw.data(), 24);
  15548. state[28] = 1;
  15549. const auto slot = lynx::effectFromFl ("Emphasizer", state);
  15550. CHECK (slot && slot->kind == lynx::EffectKind::emphasizer);
  15551. if (! slot)
  15552. continue;
  15553. auto fx = lynx::makeEffect (slot->kind);
  15554. fx->prepare (44100.0);
  15555. auto [l, r] = signal();
  15556. for (int i = 0; i < 24000; i += 512) // the golden's 512-frame engine calls (level matching is per call)
  15557. fx->process (l.data() + i, r.data() + i, std::min (512, 24000 - i), slot->params, 120.0);
  15558. std::uint64_t h = 0xcbf29ce484222325ull;
  15559. for (int i = 0; i < 24000; ++i)
  15560. for (float v : { l[(std::size_t) i], r[(std::size_t) i] })
  15561. {
  15562. std::uint32_t u;
  15563. std::memcpy (&u, &v, 4);
  15564. h = (h ^ u) * 0x100000001b3ull;
  15565. }
  15566. CHECK (h == c.hash);
  15567. }
  15568. // version 1 (no output control) keeps the 0 dB default; texts
  15569. 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 };
  15570. const auto s1 = lynx::emphasizer::parse (v1);
  15571. CHECK (s1 && s1->raw[1] == 5013 && s1->raw[5] == 10000);
  15572. CHECK (lynx::formatEffectParam (lynx::EffectKind::emphasizer, 0, 0.5f, 120.0) == "+12.0 dB");
  15573. CHECK (lynx::formatEffectParam (lynx::EffectKind::emphasizer, 5, 1.0f, 120.0) == "0.0 dB");
  15574. CHECK (lynx::formatEffectParam (lynx::EffectKind::emphasizer, 2, 1.0f, 120.0) == "Soft");
  15575. }
  15576. // ---- Wave Traveller (REA_rodada167f-wavetraveller) ----
  15577. namespace
  15578. {
  15579. // the crafted case: a sample on the 16-bit grid and a state that uses every feature (the same data went to the DLL)
  15580. inline std::vector<float> wtTestSample (int n) // planar stereo, values on the 16-bit grid
  15581. {
  15582. std::vector<float> s ((std::size_t) n * 2);
  15583. std::uint32_t r = 2024;
  15584. for (int i = 0; i < n; ++i)
  15585. for (int c = 0; c < 2; ++c)
  15586. {
  15587. r = r * 1664525u + 1013904223u;
  15588. const auto noise = (int) (r >> 20) - 2048; // ±2048
  15589. const auto tone = (int) std::lround (12000.0 * std::sin ((double) i * (c == 0 ? 0.031 : 0.047)));
  15590. s[(std::size_t) c * (std::size_t) n + (std::size_t) i] = (float) (tone + noise) / 32768.0f;
  15591. }
  15592. return s;
  15593. }
  15594. inline lynx::wavetrav::State wtTestState()
  15595. {
  15596. using namespace lynx::wavetrav;
  15597. auto st = defaultState();
  15598. st.samplePath = "x.wav"; // (FL asks its host for the sample only when the state names one)
  15599. 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) {
  15600. auto p = newPoint (x, y);
  15601. p.type = type, p.tension = tension, p.c1x = c1x, p.c1y = c1y, p.c2x = c2x, p.c2y = c2y;
  15602. return p;
  15603. };
  15604. auto& a = st.patches[60]; // every segment type, tempo based
  15605. a.wave.clear();
  15606. 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 };
  15607. const int ts[] { 1, 2, 3, 4, 5, 6, 8, 1 };
  15608. for (int i = 0; i < 8; ++i)
  15609. a.wave.push_back (pt (xs[i], ys[i], ts[i], ts[i] == 6 ? 0.2f : 0.5f));
  15610. a.speedBeats = 2.0f;
  15611. auto& b = st.patches[61]; // volume curve, mutes, start / end, attack / release
  15612. 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) };
  15613. b.mutes = { { true, 0.0f }, { false, 0.3f }, { true, 0.5f } };
  15614. b.start = 0.2f, b.end = 0.7f, b.attackMs = 50.0f, b.releaseMs = 300.0f, b.speedBeats = 1.0f;
  15615. auto& c = st.patches[62]; // absolute speed, tension segments
  15616. c.splineType = 1, c.absSpeed = 2.3f, c.start = 0.1f, c.end = 0.6f;
  15617. c.wave = { pt (0.0f, 0.0f, 6, 0.5f), pt (0.5f, 0.9f, 6, 0.9f), pt (1.0f, 0.2f, 1) };
  15618. auto& d = st.patches[63]; // types that write nothing, a tiny Bezier
  15619. 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),
  15620. pt (0.5004f, 0.2f, 3), pt (0.501f, 0.9f, 1), pt (1.0f, 0.4f, 1) };
  15621. for (auto& p : st.patches)
  15622. p.rebuild();
  15623. return st;
  15624. }
  15625. std::uint64_t floatBitsHash (const std::vector<float>& v)
  15626. {
  15627. std::uint64_t h = 0xcbf29ce484222325ull;
  15628. for (const float x : v)
  15629. {
  15630. std::uint32_t u;
  15631. std::memcpy (&u, &x, 4);
  15632. h = (h ^ u) * 0x100000001b3ull;
  15633. }
  15634. return h;
  15635. }
  15636. lynx::SampleData wtSampleData (const std::vector<float>& planar, int channels)
  15637. {
  15638. lynx::SampleData d;
  15639. const auto n = planar.size() / (std::size_t) channels;
  15640. d.left.assign (planar.begin(), planar.begin() + (std::ptrdiff_t) n);
  15641. if (channels > 1)
  15642. d.right.assign (planar.begin() + (std::ptrdiff_t) n, planar.end());
  15643. d.sampleRate = 44100.0;
  15644. return d;
  15645. }
  15646. } // namespace
  15647. TEST (wave_traveller_state_and_tables)
  15648. {
  15649. using namespace lynx::wavetrav;
  15650. // FL's state layout (§2): it round-trips byte for byte, and the default patch is FL's
  15651. const auto def = writeState (defaultState());
  15652. State s;
  15653. CHECK (parseState (def, s) && writeState (s) == def && s.selectedKey == 60 && s.samplePath.empty());
  15654. 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);
  15655. const auto crafted = writeState (wtTestState());
  15656. CHECK (parseState (crafted, s) && writeState (s) == crafted);
  15657. CHECK (s.patches[62].splineType == 1 && s.patches[61].mutes.size() == 3 && ! s.patches[61].mutes[1].on && s.patches[60].wave.size() == 8);
  15658. // tables (§3): a 0 → 1 line is 1022 values long (the writer steps back one value per segment)
  15659. CHECK (s.patches[0].waveN == 1022 && s.patches[0].waveTable[0] == 0.0f && s.patches[0].volTable[500] == 1.0f);
  15660. std::vector<float> tables;
  15661. for (int k = 60; k < 64; ++k)
  15662. {
  15663. const auto& p = s.patches[(std::size_t) k];
  15664. tables.insert (tables.end(), p.waveTable.begin(), p.waveTable.begin() + p.waveN + 1);
  15665. tables.insert (tables.end(), p.volTable.begin(), p.volTable.begin() + p.volN + 1);
  15666. CHECK (std::all_of (p.waveTable.begin(), p.waveTable.end(), [] (float v) { return v >= 0.0f && v <= 1.0f; }));
  15667. }
  15668. // (LYNX's own value; the tables reach the DLL goldens of wave_traveller_matches_fl_engine through the output)
  15669. CHECK (floatBitsHash (tables) == 0xd94594a6f22cb4dfull);
  15670. if (floatBitsHash (tables) != 0xd94594a6f22cb4dfull)
  15671. std::printf (" wave traveller tables: %016llx\n", (unsigned long long) floatBitsHash (tables));
  15672. // knobs (FUN_007579e0 / 757b10 / 757b60)
  15673. CHECK (speedFromKnob (0.0f, false) == 0.125f && speedFromKnob (1.0f, false) == 8.0f && speedFromKnob (0.0f, true) == 0.25f
  15674. && speedFromKnob (1.0f, true) == 4.0f && attackFromKnob (1.0f) == 500.0f && releaseFromKnob (1.0f) == 1000.0f);
  15675. // a .spln preset (§6): knob positions, curves, mutes
  15676. std::vector<std::uint8_t> spln;
  15677. auto i32 = [&spln] (std::int32_t v) { spln.insert (spln.end(), (std::uint8_t*) &v, (std::uint8_t*) &v + 4); };
  15678. auto f32 = [&spln] (float v) { spln.insert (spln.end(), (std::uint8_t*) &v, (std::uint8_t*) &v + 4); };
  15679. i32 (4000), i32 (1000005), i32 (1);
  15680. for (float v : { 0.5f, 0.2f, 0.1f, 0.3f, 0.25f, 0.75f })
  15681. f32 (v);
  15682. const auto rec = [&] (float x, float y) {
  15683. std::vector<std::uint8_t> r (60, 0);
  15684. std::memcpy (r.data() + 8, &x, 4), std::memcpy (r.data() + 12, &y, 4);
  15685. const std::int32_t one = 1;
  15686. std::memcpy (r.data() + 0x30, &one, 4);
  15687. spln.insert (spln.end(), r.begin(), r.end());
  15688. };
  15689. i32 (3), rec (0.0f, 1.0f), rec (0.5f, 0.0f), rec (1.0f, 1.0f);
  15690. i32 (2), rec (0.0f, 1.0f), rec (1.0f, 0.5f);
  15691. i32 (2), i32 (0), f32 (0.0f), i32 (1), f32 (0.5f);
  15692. auto p = defaultPatch();
  15693. CHECK (applyPreset (spln, p) && p.splineType == 1 && p.speedBeats == speedFromKnob (0.5f, false) && p.absSpeed == speedFromKnob (0.2f, true)
  15694. && p.attackMs == attackFromKnob (0.1f) && p.releaseMs == releaseFromKnob (0.3f) && p.start == 0.25f && p.end == 0.75f
  15695. && p.wave.size() == 3 && p.vol.size() == 2 && p.mutes.size() == 2 && ! p.mutes[0].on && p.waveN > 1000);
  15696. // garbage is refused without crashing
  15697. std::uint32_t r = 7;
  15698. for (int round = 0; round < 300; ++round)
  15699. {
  15700. std::vector<std::uint8_t> blob (crafted.begin(), crafted.begin() + (std::ptrdiff_t) (r % crafted.size()));
  15701. for (int k = 0; k < 8 && ! blob.empty(); ++k)
  15702. {
  15703. r = r * 1664525u + 1013904223u;
  15704. blob[(r >> 8) % blob.size()] = (std::uint8_t) (r >> 24);
  15705. }
  15706. r = r * 1664525u + 1013904223u;
  15707. if (parseState (blob, s))
  15708. for (const auto& q : s.patches)
  15709. CHECK (q.waveN >= 0 && q.waveN < tableCapacity && q.volN >= 0 && q.volN < tableCapacity);
  15710. }
  15711. CHECK (! parseState ({ 1, 2, 3 }, s) && ! lynx::waveTravellerStateUsable ({}));
  15712. // the installation's states (read at test time): the render test and the plugin database preset
  15713. if (realFlpDir.empty())
  15714. return;
  15715. auto read = [] (const std::filesystem::path& path) {
  15716. std::ifstream in (path, std::ios::binary);
  15717. return std::vector<std::uint8_t> { std::istreambuf_iterator<char> (in), {} };
  15718. };
  15719. const auto base = std::filesystem::path (realFlpDir);
  15720. if (const auto b = read (base / "System" / "Render tests" / "Wave Traveller.flp"); ! b.empty())
  15721. {
  15722. const auto project = flp::parseProject (b);
  15723. int found = 0;
  15724. for (const auto& c : project.channels)
  15725. if (c.pluginName == "Wave Traveller")
  15726. {
  15727. ++found;
  15728. CHECK (parseState (c.pluginState, s) && s.samplePath.find ("808 Snare.wav") != std::string::npos);
  15729. }
  15730. CHECK (found == 1);
  15731. const auto song = lynx::importFlp (project);
  15732. const auto it = std::find_if (song.channels.begin(), song.channels.end(), [] (const lynx::ChannelDef& c) { return c.instrument == lynx::InstrumentKind::wavetraveller; });
  15733. CHECK (it != song.channels.end() && lynx::waveTravellerStateUsable (it->pluginState));
  15734. }
  15735. if (const auto b = read (base / "System" / "Plugin databases" / "Plugin database (alphabetical)" / "Generators" / "Wave Traveller.fst"); ! b.empty())
  15736. {
  15737. const auto project = flp::parseProject (b, true);
  15738. CHECK (! project.channels.empty() && parseState (project.channels.front().pluginState, s) && s.samplePath.empty());
  15739. }
  15740. }
  15741. TEST (wave_traveller_matches_fl_engine)
  15742. {
  15743. // §8: goldens of the installation's Wave Traveller_x64.dll in our harness (16-bit stereo sample handed by LoadSample,
  15744. // 512-frame calls, tempo by ProcessEvent): FNV-1a of the output's float bits. LYNX's engine reproduces them exactly.
  15745. using namespace lynx::wavetrav;
  15746. const auto planar = wtTestSample (30000);
  15747. const auto smp = sampleFrom (wtSampleData (planar, 2));
  15748. CHECK (smp != nullptr && smp->length == 30000);
  15749. if (smp == nullptr)
  15750. return;
  15751. const auto state = wtTestState();
  15752. auto note = [] (int start, int key, float vol, float pan, float cents, int release, float initPan = 0.0f) {
  15753. RefNote n;
  15754. n.start = start, n.release = release;
  15755. n.note.key = key, n.note.finalVol = vol, n.note.finalPan = pan, n.note.initPan = initPan;
  15756. n.note.finalPitch = 100.0f * (float) key - 6000.0f + cents;
  15757. return n;
  15758. };
  15759. struct Case { std::vector<RefNote> notes; int rate; float tempo; int taps, length; std::uint64_t hash; };
  15760. const Case cases[] {
  15761. // live: every segment type, volume curve + mutes + attack / release, absolute speed, types 0 / 7 and a tiny Bezier
  15762. { { 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),
  15763. note (5000, 63, 1.0f, 0.6f, 250.0f, -1) }, 44100, 140.0f, 0, 40000, 0x9001a50747634ba6ull },
  15764. // render mode, 256 taps per side (FL's render default), with the notes' own pan
  15765. { { 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),
  15766. note (5000, 63, 1.0f, 0.6f, 250.0f, -1, -0.3f) }, 44100, 140.0f, 256, 8000, 0xedabd9728ffbb252ull },
  15767. // a 48 kHz host at tempo 97.5
  15768. { { 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 },
  15769. };
  15770. for (const auto& c : cases)
  15771. {
  15772. const auto out = renderReference (state, *smp, c.notes, c.rate, c.tempo, c.taps, c.length);
  15773. CHECK (floatBitsHash (out) == c.hash);
  15774. if (floatBitsHash (out) != c.hash)
  15775. std::printf (" wave traveller case: %016llx\n", (unsigned long long) floatBitsHash (out));
  15776. }
  15777. // the instrument (channel → engine) plays what the reference plays: velocity 1/1.28 = FL's Init.Vol 1, centred
  15778. {
  15779. lynx::ChannelDef ch;
  15780. ch.instrument = lynx::InstrumentKind::wavetraveller;
  15781. ch.pluginState = writeState (state);
  15782. auto inst = lynx::makeInstrument (lynx::InstrumentKind::wavetraveller, std::make_shared<lynx::SampleData> (wtSampleData (planar, 2)));
  15783. inst->configure (ch, 140.0);
  15784. inst->prepare (44100.0);
  15785. std::vector<float> l (20480, 0.0f), r (20480, 0.0f);
  15786. for (int pos = 0; pos < 20480; pos += 512)
  15787. {
  15788. if (pos == 0)
  15789. inst->noteOn (61, 1.0f / 1.28f);
  15790. if (pos == 10240)
  15791. inst->noteOff (61);
  15792. inst->render (l.data() + pos, r.data() + pos, 512, 1.0f, 1.0f);
  15793. }
  15794. const auto ref = renderReference (state, *smp, { note (0, 61, 1.0f, 0.0f, 0.0f, 10240) }, 44100, 140.0f, 0, 20480);
  15795. bool same = true;
  15796. for (int i = 0; i < 20480; ++i)
  15797. 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];
  15798. CHECK (same && inst->idle());
  15799. auto silent = lynx::makeInstrument (lynx::InstrumentKind::wavetraveller); // no sample: FL refuses the notes
  15800. silent->configure (ch, 140.0);
  15801. silent->prepare (44100.0);
  15802. silent->noteOn (60, 1.0f);
  15803. std::fill (l.begin(), l.end(), 0.0f);
  15804. silent->render (l.data(), r.data(), 512, 1.0f, 1.0f);
  15805. CHECK (silent->idle() && std::all_of (l.begin(), l.begin() + 512, [] (float v) { return v == 0.0f; }));
  15806. }
  15807. // the installation's render test (its 808 Snare decoded as FL decodes it), read at test time
  15808. if (realFlpDir.empty())
  15809. return;
  15810. const auto base = std::filesystem::path (realFlpDir);
  15811. std::ifstream fin (base / "System" / "Render tests" / "Wave Traveller.flp", std::ios::binary);
  15812. std::ifstream sin (base / "Patches" / "Packs" / "Drums" / "Snares" / "808 Snare.wav", std::ios::binary);
  15813. const std::vector<std::uint8_t> flpBytes { std::istreambuf_iterator<char> (fin), {} }, wav { std::istreambuf_iterator<char> (sin), {} };
  15814. lynx::oggvorbis::Decoded dec;
  15815. if (flpBytes.empty() || wav.empty() || ! lynx::oggvorbis::decode (wav.data(), wav.size(), dec) || dec.channels != 1)
  15816. return;
  15817. const auto project = flp::parseProject (flpBytes);
  15818. for (const auto& c : project.channels)
  15819. if (c.pluginName == "Wave Traveller")
  15820. {
  15821. State st;
  15822. CHECK (parseState (c.pluginState, st));
  15823. lynx::SampleData d;
  15824. d.left = dec.data[0];
  15825. const auto snare = sampleFrom (d);
  15826. CHECK (snare != nullptr && snare->length == 11002);
  15827. if (snare == nullptr)
  15828. return;
  15829. const auto out = renderReference (st, *snare, { note (0, 60, 1.0f, 0.0f, 0.0f, 44100) }, 44100, 140.0f, 0, 88200);
  15830. CHECK (floatBitsHash (out) == 0xafcb49e8c0ef918dull);
  15831. if (floatBitsHash (out) != 0xafcb49e8c0ef918dull)
  15832. std::printf (" wave traveller render test: %016llx\n", (unsigned long long) floatBitsHash (out));
  15833. }
  15834. }
  15835. TEST (fl_legacy_fx_match_fl_engines)
  15836. {
  15837. // REA_rodada165k: FL's Fruity Phase Inverter, Center, Bass Boost, 7 Band EQ and Delay (v1) through the import (state → slot →
  15838. // effect) against goldens of the installation's DLLs (rate set before the state, 512-frame calls): FNV-1a of the
  15839. // output's float bits (x87 hosts) and ten output samples (every platform, within the x87 tolerance)
  15840. struct Case { const char* name; int rate; std::vector<int> raw; std::uint64_t hash; std::array<float, 10> pts; };
  15841. const Case cases[] {
  15842. { "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 } },
  15843. { "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 } },
  15844. { "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 } },
  15845. { "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 } },
  15846. { "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 } },
  15847. { "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 } },
  15848. { "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 } },
  15849. { "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 } },
  15850. { "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 } },
  15851. { "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 } },
  15852. { "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 } },
  15853. { "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 } },
  15854. { "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 } },
  15855. { "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 } },
  15856. { "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 } },
  15857. { "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 } },
  15858. { "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 } },
  15859. { "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 } },
  15860. { "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 } },
  15861. { "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 } },
  15862. { "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 } },
  15863. { "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 } },
  15864. { "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 } },
  15865. };
  15866. for (const auto& c : cases)
  15867. {
  15868. std::vector<std::uint8_t> state (4 + 4 * c.raw.size(), 0);
  15869. const std::int32_t version = 1;
  15870. std::memcpy (state.data(), &version, 4);
  15871. std::memcpy (state.data() + 4, c.raw.data(), 4 * c.raw.size());
  15872. const auto slot = lynx::effectFromFl (c.name, state);
  15873. CHECK (slot && slot->kind != lynx::EffectKind::utility && slot->kind != lynx::EffectKind::eq
  15874. && slot->kind != lynx::EffectKind::parametricEq);
  15875. if (! slot)
  15876. continue;
  15877. auto fx = lynx::makeEffect (slot->kind);
  15878. fx->prepare ((double) c.rate);
  15879. std::vector<float> l (24000), r (24000);
  15880. std::uint32_t s = 12345;
  15881. const float amps[] { 0.3f, 1.5f, 0.05f, 3.0f, 0.8f, 0.0f, 2.0f, 0.4f };
  15882. for (int i = 0; i < 24000; ++i)
  15883. {
  15884. const float a = amps[(i / 3000) % 8];
  15885. s = s * 1664525u + 1013904223u;
  15886. l[(std::size_t) i] = (float) ((s >> 8) / 8388608.0 - 1.0) * a;
  15887. s = s * 1664525u + 1013904223u;
  15888. r[(std::size_t) i] = (float) ((s >> 8) / 8388608.0 - 1.0) * a;
  15889. }
  15890. // host blocks of 512 like the goldens' DLL calls (the Center / Bass Boost / 7 Band EQ round their state to double at
  15891. // the end of each call, as FL does, so other block sizes differ in the last bits; checked below)
  15892. const auto input = std::make_pair (l, r);
  15893. for (int i = 0; i < 24000; i += 512)
  15894. fx->process (l.data() + i, r.data() + i, std::min (512, 24000 - i), slot->params, 120.0);
  15895. {
  15896. auto other = lynx::makeEffect (slot->kind);
  15897. other->prepare ((double) c.rate);
  15898. auto l2 = input.first, r2 = input.second;
  15899. for (int i = 0; i < 24000; i += 1000)
  15900. other->process (l2.data() + i, r2.data() + i, 1000, slot->params, 120.0);
  15901. float worst = 0.0f;
  15902. for (std::size_t i = 0; i < 24000; ++i)
  15903. 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])) });
  15904. CHECK (worst <= 1.0e-4f); // (measured: ≤ 3.5e-5, the Bass Boost at 20 Hz)
  15905. }
  15906. for (int k = 0; k < 5; ++k)
  15907. {
  15908. const auto i = (std::size_t) (1500 + 4500 * k);
  15909. 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)])));
  15910. 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)])));
  15911. }
  15912. #if defined(__x86_64__) || defined(__i386__)
  15913. std::uint64_t h = 0xcbf29ce484222325ull;
  15914. for (int i = 0; i < 24000; ++i)
  15915. for (float v : { l[(std::size_t) i], r[(std::size_t) i] })
  15916. {
  15917. std::uint32_t u;
  15918. std::memcpy (&u, &v, 4);
  15919. h = (h ^ u) * 0x100000001b3ull;
  15920. }
  15921. CHECK (h == c.hash);
  15922. #endif
  15923. }
  15924. CHECK (lynx::formatEffectParam (lynx::EffectKind::flPhaseInverter, 0, 0.3f, 120.0) == "None");
  15925. CHECK (lynx::formatEffectParam (lynx::EffectKind::flPhaseInverter, 0, 0.5f, 120.0) == "Left");
  15926. CHECK (lynx::formatEffectParam (lynx::EffectKind::flPhaseInverter, 0, 1.0f, 120.0) == "Right");
  15927. CHECK (lynx::formatEffectParam (lynx::EffectKind::flBassBoost, 0, 0.5f, 120.0) == "60 Hz");
  15928. CHECK (lynx::formatEffectParam (lynx::EffectKind::fl7BandEq, 3, 1.0f, 120.0) == "+18.0 dB");
  15929. CHECK (lynx::formatEffectParam (lynx::EffectKind::fl7BandEq, 3, 0.5f, 120.0) == "+0.0 dB");
  15930. CHECK (lynx::formatEffectParam (lynx::EffectKind::flDelay1, 3, 0.0f, 120.0) == "Host");
  15931. CHECK (lynx::formatEffectParam (lynx::EffectKind::flDelay1, 3, 1.0f, 120.0) == "280 BPM");
  15932. CHECK (lynx::formatEffectParam (lynx::EffectKind::flDelay1, 4, 2.0f / 15.0f, 120.0) == "3/16");
  15933. CHECK (lynx::formatEffectParam (lynx::EffectKind::flDelay1, 5, 1.0f, 120.0) == "Ping-pong");
  15934. CHECK (lynx::effectFromFl ("Fruity Delay", { 1, 0, 0, 0 }) == std::nullopt
  15935. || lynx::effectFromFl ("Fruity Delay", { 1, 0, 0, 0 })->kind != lynx::EffectKind::flDelay1);
  15936. // the default Phase Inverter of FL inverts the right side only (LYNX's old import inverted both)
  15937. CHECK (lynx::fllegacy::phaseInverterMode (1024) == 2 && lynx::fllegacy::phaseInverterMode (341) == 0
  15938. && lynx::fllegacy::phaseInverterMode (342) == 1 && lynx::fllegacy::phaseInverterMode (683) == 2);
  15939. }
  15940. TEST (fl_frequency_shifter_matches_fl_engine)
  15941. {
  15942. // REA_rodada154 §4: FL's Frequency Shifter (import → slot → effect) against goldens of the installation's
  15943. // Engine_x64.dll driven by its front end (rate before the state, 1536 samples of silence first, transport stopped, 120 BPM): FNV-1a
  15944. // of the output
  15945. struct Case { std::array<int, 10> raw; double rate; std::uint64_t hash; };
  15946. const Case cases[] { { { 10000, 0, -60480, 0, 10000, 10000, 5000, 10000, 1, 5000 }, 44100.0, 0x462af5f13683c90eull },
  15947. { { 7000, 2, 40000, 3000, 10000, 10000, 8000, 6000, 0, 2000 }, 44100.0, 0xcd597b95bf1bbb1aull },
  15948. { { 6000, 1, -30000, 6000, 3000, 7000, 3000, 4000, 0, 5000 }, 48000.0, 0x5592232115cb3acull },
  15949. { { 10000, 0, 51542, 2000, 8000, 10000, 5000, 10000, 0, 5000 }, 48000.0, 0x63d3051ba14bd00full } };
  15950. for (const auto& c : cases)
  15951. {
  15952. std::vector<std::uint8_t> state (4 + 40 + 8, 0);
  15953. const std::int32_t version = 4;
  15954. std::memcpy (state.data(), &version, 4);
  15955. std::memcpy (state.data() + 4, c.raw.data(), 40);
  15956. const auto slot = lynx::effectFromFl ("Frequency Shifter", state);
  15957. CHECK (slot && slot->kind == lynx::EffectKind::flFrequencyShifter);
  15958. if (! slot)
  15959. continue;
  15960. auto fx = lynx::makeEffect (slot->kind);
  15961. fx->prepare (c.rate);
  15962. std::vector<float> l (24000), r (24000);
  15963. std::uint32_t s = 12345;
  15964. const float amps[] { 0.3f, 1.5f, 0.05f, 3.0f, 0.8f, 0.0f, 2.0f, 0.4f };
  15965. for (int i = 0; i < 24000; ++i)
  15966. {
  15967. const float a = amps[(i / 3000) % 8];
  15968. s = s * 1664525u + 1013904223u;
  15969. l[(std::size_t) i] = (float) ((s >> 8) / 8388608.0 - 1.0) * a;
  15970. s = s * 1664525u + 1013904223u;
  15971. r[(std::size_t) i] = (float) ((s >> 8) / 8388608.0 - 1.0) * a;
  15972. }
  15973. for (int i = 0; i < 24000; i += 800)
  15974. fx->process (l.data() + i, r.data() + i, 800, slot->params, 120.0);
  15975. std::uint64_t h = 0xcbf29ce484222325ull;
  15976. for (int i = 0; i < 24000; ++i)
  15977. for (float v : { l[(std::size_t) i], r[(std::size_t) i] })
  15978. {
  15979. std::uint32_t u;
  15980. std::memcpy (&u, &v, 4);
  15981. h = (h ^ u) * 0x100000001b3ull;
  15982. }
  15983. CHECK (h == c.hash);
  15984. }
  15985. // the engine's tanhf against points of the library's
  15986. CHECK (lynx::fsfl::tanhf_ (0.0f) == 0.0f && lynx::fsfl::tanhf_ (20.0f) == 1.0f && lynx::fsfl::tanhf_ (-20.0f) == -1.0f);
  15987. CHECK (std::fabs (lynx::fsfl::tanhf_ (0.5f) - 0.462117165f) < 1e-7f && std::fabs (lynx::fsfl::tanhf_ (2.0f) - 0.964027584f) < 1e-7f);
  15988. CHECK (lynx::formatEffectParam (lynx::EffectKind::flFrequencyShifter, 1, 1.0f, 120.0) == "Ring modulator");
  15989. }
  15990. TEST (fl_low_lifter_matches_fl_engine)
  15991. {
  15992. // REA_rodada154 §5: FL's Low Lifter (import → slot → effect, 512-frame calls) against goldens of the installation's
  15993. // engine (ildsp_x64.dll's Faust program driven by Low Lifter_x64.dll, rate before the state): FNV-1a of the output
  15994. struct Case { std::array<int, 15> raw; double rate; std::uint64_t hash; };
  15995. const Case cases[] { { { 1, 10000, 4999, 7210, 1, 0, 0, 0, 5000, 10000, 7750, 0, 0, 1, 1 }, 44100.0, 0xcbb7eae8f31ca6d9ull },
  15996. { { 1, 7506, 5000, 3277, 1, 0, 6757, 5053, 9608, 10000, 7778, 5281, 1, 1, 1 }, 44100.0, 0xdb2d3712d4fb16c5ull },
  15997. { { 1, 5095, 6737, 5109, 1, 1, 541, 1479, 5451, 8197, 690, 6833, 1, 1, 1 }, 48000.0, 0x273c262202984447ull } };
  15998. for (const auto& c : cases)
  15999. {
  16000. std::vector<std::uint8_t> state (4 + 60 + 18, 0);
  16001. const std::int32_t version = 6;
  16002. std::memcpy (state.data(), &version, 4);
  16003. std::memcpy (state.data() + 4, c.raw.data(), 60);
  16004. const auto slot = lynx::effectFromFl ("Low Lifter", state);
  16005. CHECK (slot && slot->kind == lynx::EffectKind::flLowLifter);
  16006. if (! slot)
  16007. continue;
  16008. auto fx = lynx::makeEffect (slot->kind);
  16009. fx->prepare (c.rate);
  16010. std::vector<float> l (24000), r (24000);
  16011. std::uint32_t s = 12345;
  16012. const float amps[] { 0.3f, 1.5f, 0.05f, 3.0f, 0.8f, 0.0f, 2.0f, 0.4f };
  16013. for (int i = 0; i < 24000; ++i)
  16014. {
  16015. const float a = amps[(i / 3000) % 8];
  16016. s = s * 1664525u + 1013904223u;
  16017. l[(std::size_t) i] = (float) ((s >> 8) / 8388608.0 - 1.0) * a;
  16018. s = s * 1664525u + 1013904223u;
  16019. r[(std::size_t) i] = (float) ((s >> 8) / 8388608.0 - 1.0) * a;
  16020. }
  16021. for (int i = 0; i < 24000; i += 512)
  16022. fx->process (l.data() + i, r.data() + i, std::min (512, 24000 - i), slot->params, 120.0);
  16023. std::uint64_t h = 0xcbf29ce484222325ull;
  16024. for (int i = 0; i < 24000; ++i)
  16025. for (float v : { l[(std::size_t) i], r[(std::size_t) i] })
  16026. {
  16027. std::uint32_t u;
  16028. std::memcpy (&u, &v, 4);
  16029. h = (h ^ u) * 0x100000001b3ull;
  16030. }
  16031. CHECK (h == c.hash);
  16032. }
  16033. CHECK (lynx::formatEffectParam (lynx::EffectKind::flLowLifter, 10, 0.775f, 120.0) == "80 Hz");
  16034. CHECK (lynx::formatEffectParam (lynx::EffectKind::flLowLifter, 3, 0.721f, 120.0) == "-6.0 dB");
  16035. }
  16036. #include "Rea153Tests.inc"
  16037. #include "Logger181bTests.inc"
  16038. #include "Render155Tests.inc"
  16039. #include "Tune157Tests.inc"
  16040. #include "Tune165Tests.inc"
  16041. #include "HwOut156Tests.inc"
  16042. #include "SoundFont158Tests.inc"
  16043. #include "Mp3157Tests.inc"
  16044. #include "Quality160Tests.inc"
  16045. #include "CrashReport159Tests.inc"
  16046. #include "Sampler162Tests.inc"
  16047. #include "Workflow163cTests.inc"
  16048. #include "ClipPitch164Tests.inc"
  16049. #include "Misc164mTests.inc"
  16050. #include "MidiSync165Tests.inc"
  16051. #include "Theme162Tests.inc"
  16052. #include "PluginPresets162Tests.inc"
  16053. #include "Look162Tests.inc"
  16054. #include "EnvController162Tests.inc"
  16055. #include "Browser162Tests.inc"
  16056. #include "BrowserView163Tests.inc"
  16057. #include "Transport162Tests.inc"
  16058. #include "Workflow163Tests.inc"
  16059. #include "Rec163Tests.inc"
  16060. #include "Ui163Tests.inc"
  16061. #include "HwInsert164Tests.inc"
  16062. #include "Layer164Tests.inc"
  16063. #include "Patcher164vTests.inc"
  16064. #include "Vfx164Tests.inc"
  16065. #include "VfxScript165Tests.inc"
  16066. #include "Perf167Tests.inc"
  16067. #include "Misc164Tests.inc"
  16068. #include "Store165Tests.inc" // (round 165n; kept off the end of the list, where every round appends)
  16069. #include "Rack165Tests.inc" // (round 165f-rack; likewise)
  16070. #include "Visual164Tests.inc"
  16071. #include "Visual165Tests.inc"
  16072. #include "Patcher164fTests.inc"
  16073. #include "Markers165Tests.inc"
  16074. #include "PerfMode165Tests.inc"
  16075. #include "Rea167gTests.inc"
  16076. #include "Rea167hTests.inc"
  16077. #include "Rea167jTests.inc"
  16078. #include "Rea167kTests.inc"
  16079. #include "Rea167lTests.inc"
  16080. #include "Patch165Tests.inc"
  16081. #include "FlPlugins165Tests.inc"
  16082. #include "FlScratch165Tests.inc"
  16083. #include "FlTrans165Tests.inc"
  16084. #include "VideoPlayer165Tests.inc"
  16085. #include "Video174dTests.inc"
  16086. #include "Dash165Tests.inc"
  16087. #include "LinuxMouse165Tests.inc"
  16088. #include "Fl165Tests.inc"
  16089. #include "Issues167oTests.inc"
  16090. #include "Perf165Tests.inc"
  16091. #include "Issue12Tests.inc"
  16092. #include "Issue12cTests.inc"
  16093. #include "Audit165zbTests.inc"
  16094. #include "FlGran165Tests.inc"
  16095. #include "GranDrop165Tests.inc"
  16096. #include "Kepler165kTests.inc"
  16097. #include "KeplerExo167dTests.inc"
  16098. #include "MixerLatency165Tests.inc"
  16099. #include "Rea168jTests.inc"
  16100. #include "Audit165Tests.inc"
  16101. #include "Vfx165cTests.inc"
  16102. #include "HarmorBeta165hTests.inc"
  16103. #include "Crash13Tests.inc"
  16104. #include "Issue11bTests.inc"
  16105. #include "Issue11cTests.inc"
  16106. #include "FlNewt165Tests.inc"
  16107. #include "Audit165zATests.inc"
  16108. #include "Audit165zA2Tests.inc"
  16109. #include "Eq166fTests.inc"
  16110. #include "Feat166dTests.inc"
  16111. #include "Round166eTests.inc"
  16112. #include "Issue15Tests.inc"
  16113. #include "Issue14bugsTests.inc"
  16114. #include "Left167Tests.inc"
  16115. #include "Wheel173dTests.inc"
  16116. #include "PoiZoneWheels173dTests.inc"
  16117. #include "Tune166cTests.inc"
  16118. #include "Update166bTests.inc"
  16119. #include "Emphasis167fTests.inc"
  16120. #include "Dvs167Tests.inc"
  16121. #include "EdisonScript167Tests.inc"
  16122. #include "Deverb167Tests.inc"
  16123. #include "Score167Tests.inc"
  16124. #include "Ntt167Tests.inc"
  16125. #include "MidiOut167iTests.inc"
  16126. #include "Transporter167fTests.inc"
  16127. #include "Polish167pTests.inc"
  16128. #include "Menus168zTests.inc"
  16129. #include "Session168aTests.inc"
  16130. #include "Live169bTests.inc"
  16131. #include "Live169jTests.inc"
  16132. #include "StemSplitTests.inc"
  16133. #include "Zoom167tTests.inc"
  16134. #include "Proll168gTests.inc"
  16135. #include "Rea168eTests.inc"
  16136. #include "Design168nTests.inc"
  16137. #include "Design168qTests.inc"
  16138. #include "FirstRun167vTests.inc"
  16139. #include "LoopStarter168bTests.inc"
  16140. #include "Rec168kTests.inc"
  16141. #include "Rec170dTests.inc"
  16142. #include "Rec177dTests.inc"
  16143. #include "Issue32Tests.inc"
  16144. #include "WrapperMidi181cTests.inc"
  16145. #include "Mono168lTests.inc"
  16146. #include "MixPre167qTests.inc"
  16147. #include "Design168rTests.inc"
  16148. #include "Playlist168fTests.inc"
  16149. #include "RecFold167qTests.inc"
  16150. #include "LoopWrap167qTests.inc"
  16151. #include "Sel168tTests.inc"
  16152. #include "Gap169kTests.inc"
  16153. #include "Gap169kPickerTests.inc"
  16154. #include "Gap169kAlignTests.inc"
  16155. #include "FlQ170iTests.inc"
  16156. #include "FlQ170i2Tests.inc"
  16157. #include "FlQ170i3Tests.inc"
  16158. #include "Speech168bTests.inc"
  16159. #include "Design168pTests.inc"
  16160. #include "Typing168tTests.inc"
  16161. #include "GrossBeat168mTests.inc"
  16162. #include "Maximus168xTests.inc"
  16163. #include "Rec168xTests.inc"
  16164. #include "Design168oTests.inc"
  16165. #include "Pr44BrowserListTests.inc"
  16166. #include "Mixer168hTests.inc"
  16167. #include "Mute53fTests.inc"
  16168. #include "Issue32cTests.inc"
  16169. #include "Drumaxx169eTests.inc"
  16170. #include "PatternBlocks170fTests.inc"
  16171. #include "Mixfx170bTests.inc"
  16172. #include "Typeahead170bTests.inc"
  16173. #include "PlugQ170cTests.inc"
  16174. #include "Toolbar170eTests.inc"
  16175. #include "Rack170aTests.inc"
  16176. #include "Inst169hTests.inc"
  16177. #include "Crash169gTests.inc"
  16178. #include "Wfqa170hTests.inc"
  16179. #include "Toxic169hTests.inc"
  16180. #include "Gen169lTests.inc"
  16181. #include "Picker171cTests.inc"
  16182. #include "Picker175bTests.inc"
  16183. #include "Gen169l2Tests.inc"
  16184. #include "Patcher171bTests.inc"
  16185. #include "Wfqa170h2Tests.inc"
  16186. #include "Fader171cTests.inc"
  16187. #include "Tb169g2Tests.inc"
  16188. #include "Issues171aTests.inc"
  16189. #include "CtlAudit170i3Tests.inc"
  16190. #include "CtlWake173dTests.inc"
  16191. #include "Issue32dTests.inc"
  16192. #include "Issue81Tests.inc"
  16193. #include "Issue85Tests.inc"
  16194. #include "FlpExport171eTests.inc"
  16195. #include "Tp172cTests.inc"
  16196. #include "Layout172aTests.inc"
  16197. #include "Issues173aTests.inc"
  16198. #include "FxAudit173dTests.inc"
  16199. #include "Audit173d3Tests.inc"
  16200. #include "Vocodex173d3Tests.inc"
  16201. #include "Fluid174eTests.inc"
  16202. #include "AlsImport175aTests.inc"
  16203. #include "Bridge176aTests.inc"
  16204. #include "Issues178aTests.inc"
  16205. #include "Mixer177aTests.inc"
  16206. #include "ClipLauncher181aTests.inc"
  16207. #include "PluginExport180bTests.inc"
  16208. #include "FlExport180dTests.inc"
  16209. #include "Mixer177a2Tests.inc"
  16210. #include "Mixer177a3Tests.inc"
  16211. #include "Mixer177a4Tests.inc"
  16212. #include "Sc177bTests.inc"
  16213. #include "Issue97Tests.inc"