tests.cpp 733 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030
  1. // Core tests (no JUCE). Usage: lynx_tests [folder-with-flp-files]
  2. #include <atomic>
  3. #include <chrono>
  4. #include <random>
  5. #include <cctype>
  6. #include <cmath>
  7. #include <cstdio>
  8. #include <cstring>
  9. #include <filesystem>
  10. #include <map>
  11. #include <set>
  12. #include <sstream>
  13. #include <fstream>
  14. #include <functional>
  15. #include <iterator>
  16. #include <string>
  17. #include <tuple>
  18. #include <vector>
  19. #include "core/Dither.h"
  20. #include "core/DrumSynthLive.h"
  21. #include "core/Effects.h"
  22. #include "core/Effector.h"
  23. #include "core/FruityDelayBank.h"
  24. #include "core/FruityMultiband.h"
  25. #include "core/Denormals.h"
  26. #include "core/FlpImport.h"
  27. #include "core/FlEffects.h"
  28. #include "core/FlPatcher.h"
  29. #include "core/FlSamplers.h"
  30. #include "core/FlSynths.h"
  31. #include "core/Inflate.h"
  32. #include "core/WavPack.h"
  33. #include "core/FlWrapper.h"
  34. #include "core/FlPluginDb.h"
  35. #include "core/MissingFiles.h"
  36. #include "core/Gms.h"
  37. #include "core/PoiZone.h"
  38. #include "core/MiniSynth.h"
  39. #include "core/SimSynth.h"
  40. #include "core/Granulizer.h"
  41. #include "core/Drumaxx.h"
  42. #include "core/Wasp.h"
  43. #include "core/Plucked.h"
  44. #include "core/BeepMap.h"
  45. #include "core/Sawer.h"
  46. #include "core/Slayer.h"
  47. #include "core/Toxic.h"
  48. #include "core/Harmless.h"
  49. #include "core/Harmor.h"
  50. #include "core/Morphine.h"
  51. #include "core/Ogun.h"
  52. #include "core/BassDrum.h"
  53. #include "core/FruitKick.h"
  54. #include "core/IppCos.h"
  55. #include "core/Dx10.h"
  56. #include "core/Kepler.h"
  57. #include "core/ThreeOsc.h"
  58. #include "core/FlResampler.h"
  59. #include "core/FlKeys.h"
  60. #include "core/DirectWave.h"
  61. #include "core/FlVoiceProcessor.h"
  62. #include "core/TransistorBass.h"
  63. #include "core/HarmlessFx.h"
  64. #include "core/MaximusFx.h"
  65. #include "core/Reeverb2.h"
  66. #include "core/LuxeVerb.h"
  67. #include "core/Denormals.h"
  68. #include "core/FruityDynamics.h"
  69. #include "core/FruityShapers.h"
  70. #include "core/ParametricEqFl.h"
  71. #include "core/FruityBasicFx.h"
  72. #include "core/EquoFx.h"
  73. #include "core/DistructorFx.h"
  74. #include "core/MultibandDelayFx.h"
  75. #include "core/PitchShifterFl.h"
  76. #include "core/FruityFilters.h"
  77. #include "core/NewtoneFx.h"
  78. #include "core/FruityDelay.h"
  79. #include "core/StereoFx.h"
  80. #include "core/FrequencySplitter.h"
  81. #include "core/ChorusFx.h"
  82. #include "core/GrossBeat.h"
  83. #include "core/LovePhilter.h"
  84. #include "core/FlangusFastDist.h"
  85. #include "core/FruityVocoder.h"
  86. #include "core/DelphiMath.h"
  87. #include "core/LiveEventQueue.h"
  88. #include "core/RackRenderer.h"
  89. #include "core/Scheduler.h"
  90. #include "core/SnapshotExchange.h"
  91. #include "core/SoftClip.h"
  92. #include "core/Slicer.h"
  93. #include "core/SytrusEngine.h"
  94. #include "core/Templates.h"
  95. #include "core/Vocodex.h"
  96. #include "core/TimeStretch.h"
  97. #include "core/WorkerPool.h"
  98. #include "core/Song.h"
  99. #include "core/NoteEdit.h"
  100. #include "core/Recorder.h"
  101. #include "core/KeyMap.h"
  102. #include "core/RackOps.h"
  103. #include "core/ValueMath.h"
  104. #include "core/Workspace.h"
  105. #include "core/StartupSound.h"
  106. #include "flp/Flp.h"
  107. #include "ui/I18nData.h"
  108. namespace
  109. {
  110. int failures = 0;
  111. int checks = 0;
  112. std::vector<std::pair<const char*, std::function<void()>>>& registry()
  113. {
  114. static std::vector<std::pair<const char*, std::function<void()>>> r;
  115. return r;
  116. }
  117. struct Register
  118. {
  119. Register (const char* name, std::function<void()> fn) { registry().emplace_back (name, std::move (fn)); }
  120. };
  121. #define TEST(name) \
  122. static void name(); \
  123. static Register reg_##name (#name, name); \
  124. static void name()
  125. #define CHECK(cond) \
  126. do \
  127. { \
  128. ++checks; \
  129. if (! (cond)) \
  130. { \
  131. ++failures; \
  132. std::printf (" FAILED %s:%d: %s\n", __FILE__, __LINE__, #cond); \
  133. } \
  134. } while (false)
  135. std::string realFlpDir;
  136. // --- helpers for building synthetic .flp files -----------------------------
  137. struct FlpBuilder
  138. {
  139. std::vector<std::uint8_t> bytes;
  140. FlpBuilder (std::uint16_t ppq = 96)
  141. {
  142. put ("FLhd");
  143. u32 (6);
  144. u16 (0);
  145. u16 (1);
  146. u16 (ppq);
  147. put ("FLdt");
  148. u32 (0); // size is not used by the reader
  149. }
  150. void put (const char* s) { bytes.insert (bytes.end(), s, s + std::strlen (s)); }
  151. void u8 (std::uint8_t v) { bytes.push_back (v); }
  152. void u16 (std::uint16_t v) { u8 ((std::uint8_t) v); u8 ((std::uint8_t) (v >> 8)); }
  153. void u32 (std::uint32_t v) { u16 ((std::uint16_t) v); u16 ((std::uint16_t) (v >> 16)); }
  154. void varint (std::size_t n)
  155. {
  156. do
  157. {
  158. auto b = (std::uint8_t) (n & 0x7F);
  159. n >>= 7;
  160. u8 (n ? (std::uint8_t) (b | 0x80) : b);
  161. } while (n);
  162. }
  163. void data (std::uint8_t id, const std::vector<std::uint8_t>& payload)
  164. {
  165. u8 (id);
  166. varint (payload.size());
  167. bytes.insert (bytes.end(), payload.begin(), payload.end());
  168. }
  169. void ascii (std::uint8_t id, const std::string& s)
  170. {
  171. std::vector<std::uint8_t> p (s.begin(), s.end());
  172. p.push_back (0);
  173. data (id, p);
  174. }
  175. void utf16 (std::uint8_t id, const std::u16string& s)
  176. {
  177. std::vector<std::uint8_t> p;
  178. for (auto c : s)
  179. {
  180. p.push_back ((std::uint8_t) c);
  181. p.push_back ((std::uint8_t) (c >> 8));
  182. }
  183. p.push_back (0);
  184. p.push_back (0);
  185. data (id, p);
  186. }
  187. };
  188. std::vector<std::uint8_t> noteRecord (std::uint32_t pos, std::uint16_t channel, std::uint32_t len, std::uint16_t key,
  189. std::uint8_t velocity)
  190. {
  191. std::vector<std::uint8_t> r (24, 0);
  192. std::memcpy (&r[0], &pos, 4);
  193. std::memcpy (&r[6], &channel, 2);
  194. std::memcpy (&r[8], &len, 4);
  195. std::memcpy (&r[12], &key, 2);
  196. r[16] = 120;
  197. r[20] = 64;
  198. r[21] = velocity;
  199. return r;
  200. }
  201. } // namespace
  202. // ---------------------------------------------------------------------------
  203. // FLP
  204. TEST (flp_rejects_garbage)
  205. {
  206. std::vector<std::uint8_t> junk (64, 0x41);
  207. bool threw = false;
  208. try { flp::EventReader r (junk); } catch (const flp::Error&) { threw = true; }
  209. CHECK (threw);
  210. }
  211. TEST (flp_reads_size_classes_varint_and_extended_ids)
  212. {
  213. FlpBuilder b;
  214. b.u8 (0x15); b.u8 (2); // u8
  215. b.u8 (0x40); b.u16 (513); // u16
  216. b.u8 (0x9C); b.u32 (128500); // u32
  217. b.data (0xD5, std::vector<std::uint8_t> (300, 7)); // 2-byte varint
  218. b.u8 (0xAC); b.u32 (0xC0000100); b.varint (3); b.u8 (1); b.u8 (2); b.u8 (3); // extended ID
  219. flp::EventReader r (b.bytes);
  220. flp::Event e;
  221. CHECK (r.header().ppq == 96);
  222. CHECK (r.next (e) && e.id == 0x15 && e.value == 2);
  223. CHECK (r.next (e) && e.id == 0x40 && e.value == 513);
  224. CHECK (r.next (e) && e.id == 0x9C && e.value == 128500);
  225. CHECK (r.next (e) && e.id == 0xD5 && e.data.size() == 300 && e.data[299] == 7);
  226. CHECK (r.next (e) && e.id == 0xC0000100 && e.isData() && e.data.size() == 3 && e.data[2] == 3);
  227. CHECK (! r.next (e));
  228. }
  229. TEST (flp_truncated_file_throws)
  230. {
  231. FlpBuilder b;
  232. b.u8 (0x9C); b.u8 (1); // u32 with only 1 byte
  233. flp::EventReader r (b.bytes);
  234. flp::Event e;
  235. bool threw = false;
  236. try { r.next (e); } catch (const flp::Error&) { threw = true; }
  237. CHECK (threw);
  238. }
  239. TEST (flp_version_numbers)
  240. {
  241. CHECK (flp::parseVersionNumber ("25.2.5.5319") == 25002005u);
  242. CHECK (flp::parseVersionNumber ("20.99.3000.3031") == 20102000u);
  243. CHECK (flp::parseVersionNumber ("11.0.3") == 11000003u);
  244. }
  245. TEST (flp_playlist_record_size_by_version)
  246. {
  247. CHECK (flp::playlistRecordSize (12009003, 64) == 32);
  248. CHECK (flp::playlistRecordSize (21000000, 120) == 60);
  249. CHECK (flp::playlistRecordSize (25002005, 160) == 80);
  250. CHECK (flp::playlistRecordSize (25002005, 60) == 60); // size doesn't match the version -> tries the others
  251. CHECK (flp::playlistRecordSize (25002005, 7) == 0);
  252. }
  253. TEST (flp_parses_project_model)
  254. {
  255. FlpBuilder b (96);
  256. b.ascii (0xC7, "25.2.5.5319");
  257. b.u8 (0x9C); b.u32 (174500);
  258. b.utf16 (0xC2, u"Título");
  259. b.u8 (0x41); b.u16 (3);
  260. b.u8 (0x96); b.u32 (0x00A14547); // BGR in the file
  261. auto notes = noteRecord (96, 1, 48, 64, 100);
  262. auto n2 = noteRecord (0, 0, 0, 60, 128);
  263. notes.insert (notes.end(), n2.begin(), n2.end());
  264. b.data (0xE0, notes);
  265. b.u8 (0x40); b.u16 (0);
  266. b.u8 (0x15); b.u8 (0);
  267. b.utf16 (0xCB, u"Kick");
  268. {
  269. std::vector<std::uint8_t> levels (24, 0);
  270. const std::int32_t lv[] = { 3200, 12800, -150 };
  271. std::memcpy (levels.data(), lv, sizeof (lv));
  272. b.data (0xDB, levels);
  273. }
  274. b.u8 (0x68); b.u16 (5);
  275. b.u8 (0x40); b.u16 (1);
  276. b.u8 (0x15); b.u8 (2);
  277. b.utf16 (0xC9, u"FLEX");
  278. b.utf16 (0xCB, u"Lead");
  279. b.u8 (0x63); b.u16 (0);
  280. std::vector<std::uint8_t> item (80, 0);
  281. std::uint32_t pos = 384, len = 768;
  282. std::uint16_t base = 20480, idx = 20480 + 3, rv = 499 - 2;
  283. std::memcpy (&item[0], &pos, 4);
  284. std::memcpy (&item[4], &base, 2);
  285. std::memcpy (&item[6], &idx, 2);
  286. std::memcpy (&item[8], &len, 4);
  287. std::memcpy (&item[12], &rv, 2);
  288. b.data (0xE9, item);
  289. b.utf16 (0xCC, u"Drums");
  290. b.data (0xEC, std::vector<std::uint8_t> (12, 0));
  291. b.data (0xEC, std::vector<std::uint8_t> (12, 0));
  292. const auto p = flp::parseProject (b.bytes);
  293. CHECK (p.version == "25.2.5.5319");
  294. CHECK (std::abs (p.tempo - 174.5) < 1e-9);
  295. CHECK (p.title == "Título");
  296. CHECK (p.channels.size() == 2);
  297. CHECK (p.channels[0].name == "Kick" && p.channels[0].mixerTrack == 5);
  298. CHECK (p.channels[0].hasLevels && p.channels[0].pan == 3200 && p.channels[0].volume == 12800 && p.channels[0].pitchCents == -150);
  299. CHECK (p.channels[1].type == flp::ChannelType::instrument && p.channels[1].pluginName == "FLEX");
  300. CHECK (p.patterns.size() == 1 && p.patterns[0].index == 3);
  301. CHECK (p.patterns[0].colour && *p.patterns[0].colour == 0x4745A1);
  302. CHECK (p.patterns[0].notes.size() == 2 && p.patterns[0].notes[0].position == 0); // sorted
  303. CHECK (p.patterns[0].notes[1].key == 64 && p.patterns[0].notes[1].length == 48);
  304. CHECK (p.arrangements.size() == 1 && p.arrangements[0].items.size() == 1);
  305. const auto& it = p.arrangements[0].items[0];
  306. CHECK (it.kind == flp::PlaylistItem::Kind::pattern && it.index == 3 && it.track == 2 && it.position == 384);
  307. CHECK (p.mixer.size() == 2 && p.mixer[0].name == "Drums" && p.mixer[1].name.empty());
  308. CHECK (p.warnings.empty());
  309. }
  310. TEST (flp_fuzz_corrupted_files_do_not_crash)
  311. {
  312. if (realFlpDir.empty())
  313. return;
  314. // some real .flp files, corrupted in various ways: the parser may reject them (flp::Error), but
  315. // must never hang, read past the buffer or produce a Song with invalid indices
  316. std::vector<std::vector<std::uint8_t>> sources;
  317. for (auto& entry : std::filesystem::recursive_directory_iterator (realFlpDir))
  318. if (entry.path().extension() == ".flp" && sources.size() < 6 && std::filesystem::file_size (entry.path()) < 3'000'000)
  319. {
  320. std::ifstream in (entry.path(), std::ios::binary);
  321. sources.emplace_back ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  322. }
  323. std::uint32_t seed = 2024;
  324. auto rnd = [&] (std::uint32_t n) { seed = seed * 1664525u + 1013904223u; return n == 0 ? 0u : (seed >> 8) % n; };
  325. int parsed = 0, rejected = 0;
  326. for (int round = 0; round < 240 && ! sources.empty(); ++round)
  327. {
  328. auto bytes = sources[(std::size_t) rnd ((std::uint32_t) sources.size())];
  329. switch (round % 4)
  330. {
  331. case 0: bytes.resize (rnd ((std::uint32_t) bytes.size())); break; // truncated
  332. 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;
  333. case 2: // absurd event size
  334. for (int k = 0; k < 5; ++k)
  335. bytes[(std::size_t) rnd ((std::uint32_t) bytes.size())] = 0xFF;
  336. break;
  337. default: // shuffled block
  338. {
  339. const auto a = rnd ((std::uint32_t) bytes.size()), b = rnd ((std::uint32_t) bytes.size());
  340. std::reverse (bytes.begin() + std::min (a, b), bytes.begin() + std::max (a, b));
  341. break;
  342. }
  343. }
  344. try
  345. {
  346. const auto project = flp::parseProject (bytes);
  347. const auto song = lynx::importFlp (project);
  348. ++parsed;
  349. for (const auto& p : song.patterns)
  350. for (const auto& n : p.notes)
  351. CHECK (n.channel >= 0 && n.channel < (int) song.channels.size());
  352. for (const auto& c : song.playlist)
  353. CHECK (c.kind != lynx::ClipKind::pattern || (c.pattern >= 0 && c.pattern < (int) song.patterns.size()));
  354. }
  355. catch (const flp::Error&)
  356. {
  357. ++rejected;
  358. }
  359. }
  360. std::printf (" fuzz: %d imported, %d rejected\n", parsed, rejected);
  361. CHECK (parsed + rejected == 240);
  362. }
  363. TEST (plugin_state_decoders_survive_garbage)
  364. {
  365. // random blobs (varied sizes, including valid headers followed by garbage)
  366. std::uint32_t seed = 77;
  367. auto rnd = [&] { seed = seed * 1664525u + 1013904223u; return seed >> 8; };
  368. const char* plugins[] = { "Fruity Parametric EQ 2", "Fruity Limiter", "Fruity Reeverb 2", "Fruity Delay 2", "Fruity Delay 3",
  369. "Fruity Compressor", "Fruity Balance", "Fruity PanOMatic", "Fruity Stereo Shaper", "Fruity Phase Inverter" };
  370. int decoded = 0;
  371. for (int round = 0; round < 3000; ++round)
  372. {
  373. std::vector<std::uint8_t> blob ((std::size_t) (rnd() % 600));
  374. for (auto& b : blob)
  375. b = (std::uint8_t) rnd();
  376. if (blob.size() >= 4 && round % 3 == 0) // versions the readers accept
  377. {
  378. const std::uint32_t versions[] = { 8, 10001, 10000, 12, 14, 6, 1 };
  379. const auto v = versions[rnd() % 7];
  380. std::memcpy (blob.data(), &v, 4);
  381. }
  382. if (auto fx = lynx::effectFromFl (plugins[rnd() % 10], blob))
  383. {
  384. ++decoded;
  385. for (auto v : fx->params)
  386. CHECK (std::isfinite (v) && v >= 0.0f && v <= 1.0f);
  387. }
  388. lynx::SynthSettings synth;
  389. if (lynx::synthFrom3xOsc (blob, synth))
  390. for (const auto& o : synth.osc)
  391. CHECK (std::isfinite (o.level) && o.level >= 0.0f && o.level <= 1.0f);
  392. float harmlessGain = 1.0f;
  393. if (lynx::synthFromHarmless (blob, 120.0, synth, harmlessGain))
  394. CHECK (std::isfinite (harmlessGain) && std::isfinite (synth.cutoff) && std::isfinite (synth.attack));
  395. if (lynx::synthFromFruitKick (blob, synth))
  396. CHECK (std::isfinite (synth.decay) && std::isfinite (synth.pitchEnv.amount));
  397. if (lynx::synthFromGms (blob, 120.0, synth, harmlessGain) || lynx::synthFromHarmor (blob, synth, harmlessGain) || lynx::synthFromOgun (blob, synth, harmlessGain)
  398. || lynx::synthFromSakura (blob, synth) || lynx::synthFromKepler (blob, synth, harmlessGain))
  399. CHECK (std::isfinite (synth.cutoff) && std::isfinite (synth.attack) && std::isfinite (harmlessGain));
  400. if (auto sound = lynx::drumSynthLiveSound (blob))
  401. CHECK (sound->size() < 256);
  402. if (auto wave = lynx::waveTravellerSample (blob))
  403. CHECK (! wave->first.empty() && wave->second >= 0 && wave->second <= 127);
  404. lynx::synthFromBooBass (blob, synth);
  405. lynx::gmsStateUsable (blob); // (the exact engines' parsers reject garbage without crashing)
  406. lynx::morphineStateUsable (blob);
  407. lynx::poizoneStateUsable (blob);
  408. if (lynx::synthFromPoiZone (blob, synth, harmlessGain) || lynx::synthFromBassDrum (blob, synth, harmlessGain) || lynx::synthFromToxic (blob, synth, harmlessGain)
  409. || lynx::synthFromMorphine (blob, synth, harmlessGain))
  410. CHECK (std::isfinite (synth.cutoff) && std::isfinite (harmlessGain));
  411. if (auto patch = lynx::drumpadPatchName (blob))
  412. CHECK (patch->size() <= 0x30 && std::isfinite (lynx::drumToneForName (*patch).decay));
  413. if (auto flex = lynx::flexPresetName (blob))
  414. CHECK ((! flex->first.empty() || ! flex->second.empty()) && flex->second.size() <= 1024);
  415. if (lynx::synthFromSytrus (blob, 120.0, synth, harmlessGain))
  416. CHECK (std::isfinite (synth.cutoff) && std::isfinite (synth.attack));
  417. if (lynx::synthFromDX10 (blob, synth))
  418. CHECK (std::isfinite (synth.decay) && std::isfinite (synth.release) && std::isfinite (synth.osc[1].level));
  419. if (blob.size() >= 4 && round % 5 == 0) // MiniSynth header followed by garbage
  420. {
  421. std::memcpy (blob.data(), "SSLF", 4);
  422. if (blob.size() > 220)
  423. {
  424. std::memcpy (blob.data() + 40, "DATA", 4);
  425. std::memcpy (blob.data() + 48, "0rev", 4);
  426. }
  427. }
  428. if (lynx::synthFromMiniSynth (blob, 120.0, synth))
  429. CHECK (std::isfinite (synth.cutoff) && std::isfinite (synth.decay) && std::isfinite (synth.attack));
  430. if (lynx::miniSynthStateUsable (blob)) // the exact engine's parser and a short render with silent tables
  431. {
  432. const auto params = lynx::miniSynthParams (blob);
  433. CHECK (params.size() == 43u && std::all_of (params.begin(), params.end(), [] (float v) { return v >= 0.0f && v <= 1.0f; }));
  434. const auto y = lynx::minisynth::renderReference (blob, { { 0, 60, 1.0, 1.0, 0.0, 0.0, 64 } }, 44100.0, 128, "");
  435. CHECK (y.size() == 256u && std::all_of (y.begin(), y.end(), [] (float v) { return std::isfinite (v); }));
  436. }
  437. lynx::ForeignPlugin foreign;
  438. lynx::parseFlWrapper (blob, foreign);
  439. std::vector<std::uint8_t> component, controller;
  440. lynx::splitFlVst3State (blob, component, controller);
  441. }
  442. CHECK (decoded == 3000); // every known name yields an effect (default when the blob is unusable)
  443. }
  444. TEST (inflate_zlib)
  445. {
  446. // fixed Huffman with back-references
  447. const std::uint8_t fixed[] = { 0x78, 0xda, 0x4b, 0x4c, 0x4a, 0x4e, 0xc4, 0x86, 0x22, 0x00, 0x7c, 0x69, 0x09, 0x89 };
  448. const auto a = lynx::inflateZlib (fixed, sizeof (fixed));
  449. CHECK (a && std::string (a->begin(), a->end()) == "abcabcabcabcabcabcabcabcX");
  450. // stored (uncompressed) block
  451. const std::uint8_t stored[] = { 0x78, 0x01, 0x01, 0x03, 0x00, 0xfc, 0xff, 'L', 'Y', 'N', 0x00, 0x00, 0x00, 0x00 };
  452. const auto b = lynx::inflateZlib (stored, sizeof (stored));
  453. CHECK (b && std::string (b->begin(), b->end()) == "LYN");
  454. // truncated, bad header and size limit
  455. CHECK (! lynx::inflateZlib (fixed, 6));
  456. const std::uint8_t bad[] = { 0x12, 0x34, 0x00 };
  457. CHECK (! lynx::inflateZlib (bad, sizeof (bad)));
  458. CHECK (! lynx::inflateZlib (fixed, sizeof (fixed), false, 10));
  459. }
  460. TEST (fl_round15_effects_convert_known_values)
  461. {
  462. auto blob = [] (std::vector<std::int32_t> words) {
  463. std::vector<std::uint8_t> b (words.size() * 4);
  464. std::memcpy (b.data(), words.data(), b.size());
  465. return b;
  466. };
  467. // Frequency Shifter "+800 Hz": frequency 51542 in the 20 kHz range, 100% wet
  468. const auto fs = lynx::effectFromFl ("Frequency Shifter", blob ({ 4, 10000, 0, 51542, 0, 10000, 10000, 5000, 10000, 0, 5000, 0, 0 }));
  469. CHECK (fs && fs->kind == lynx::EffectKind::freqShift);
  470. if (fs)
  471. {
  472. const auto x = (fs->params[0] - 0.5) * 2.0;
  473. CHECK (std::abs (2000.0 * x * std::abs (x) - 800.0) < 10.0);
  474. CHECK (std::abs (fs->params[1] - 1.0f) < 1e-4f);
  475. }
  476. // Emphasis: input 0.0625 (= +1.5 dB) and ceiling 0.9308 (≈ −1.6 dB) -> limiter
  477. const auto em = lynx::effectFromFl ("Emphasis", blob ({ 8, 0, 0, 2, 1, 0, 0, 625, 10000, 10000, 9308, 0, 2, 7, 0 }));
  478. CHECK (em && em->kind == lynx::EffectKind::limiter && em->enabled);
  479. if (em)
  480. {
  481. CHECK (std::abs (em->params[1] * 24.0f - 1.5f) < 0.05f);
  482. CHECK (std::abs (em->params[0] * 12.0f - 12.0f + 1.6f) < 0.1f);
  483. }
  484. // Pitch Shifter +12 st, mix 100%
  485. 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 }));
  486. CHECK (ps && ps->kind == lynx::EffectKind::flPitchShifter && ps->params[0] == 1.0f && ps->params[3] == 1.0f); // REA_rodada61
  487. }
  488. TEST (frequency_shifter_moves_spectrum_by_hz)
  489. {
  490. auto fx = lynx::makeEffect (lynx::EffectKind::freqShift);
  491. fx->prepare (48000.0);
  492. auto slot = lynx::makeEffectSlot (lynx::EffectKind::freqShift);
  493. // +200 Hz: x·|x|·2000 = 200 -> x = sqrt(0.1)
  494. slot.params[0] = (float) (0.5 + std::sqrt (0.1) / 2.0);
  495. std::vector<float> l (48000), r (48000);
  496. for (int i = 0; i < 48000; ++i)
  497. l[(std::size_t) i] = r[(std::size_t) i] = (float) std::sin (2.0 * 3.141592653589793 * 1000.0 * i / 48000.0);
  498. for (int done = 0; done < 48000; done += 480)
  499. fx->process (l.data() + done, r.data() + done, 480, slot.params, 120.0);
  500. auto energy = [&] (double f) {
  501. const auto w = 2.0 * 3.141592653589793 * f / 48000.0, k = 2.0 * std::cos (w);
  502. double s1 = 0.0, s2 = 0.0;
  503. for (int i = 9600; i < 48000; ++i)
  504. {
  505. const auto s0 = l[(std::size_t) i] + k * s1 - s2;
  506. s2 = s1;
  507. s1 = s0;
  508. }
  509. return s1 * s1 + s2 * s2 - k * s1 * s2;
  510. };
  511. // the energy moves to 1200 Hz; little remains at 1000 and almost nothing at the image (800)
  512. CHECK (energy (1200.0) > 50.0 * energy (1000.0));
  513. CHECK (energy (1200.0) > 50.0 * energy (800.0));
  514. }
  515. TEST (pitch_shift_moves_frequency_keeps_length)
  516. {
  517. auto fx = lynx::makeEffect (lynx::EffectKind::pitchShift);
  518. fx->prepare (48000.0);
  519. auto slot = lynx::makeEffectSlot (lynx::EffectKind::pitchShift);
  520. auto crossingsAfter = [&] (float semitoneParam) {
  521. fx->reset();
  522. slot.params[0] = semitoneParam;
  523. std::vector<float> l (48000), r (48000);
  524. for (int i = 0; i < 48000; ++i)
  525. l[(std::size_t) i] = r[(std::size_t) i] = (float) std::sin (2.0 * 3.141592653589793 * 220.0 * i / 48000.0);
  526. for (int done = 0; done < 48000; done += 512)
  527. fx->process (l.data() + done, r.data() + done, std::min (512, 48000 - done), slot.params, 120.0);
  528. int crossings = 0;
  529. for (int i = 9600; i < 48000; ++i)
  530. crossings += (l[(std::size_t) i - 1] < 0.0f) != (l[(std::size_t) i] < 0.0f);
  531. for (auto v : l)
  532. CHECK (std::isfinite (v) && std::abs (v) < 1.6f);
  533. return crossings / 0.8 / 2.0; // cycles per second
  534. };
  535. CHECK (std::abs (crossingsAfter (0.5f) - 220.0) < 8.0); // 0 st
  536. CHECK (std::abs (crossingsAfter (0.5f + 12.0f / 48.0f) - 440.0) < 25.0); // +12 st
  537. CHECK (std::abs (crossingsAfter (0.5f - 12.0f / 48.0f) - 110.0) < 12.0); // −12 st
  538. }
  539. // A Gross Beat state in FL's layout (REA_rodada53 §1), built here: version 8, parameters, options, 72 slots with a name,
  540. // sync bytes and a map envelope (kind 3, version 3, points: Δx in beats, level, tension, curve type)
  541. namespace
  542. {
  543. struct GbPoint
  544. {
  545. double dx, y;
  546. std::uint8_t type;
  547. };
  548. std::vector<std::uint8_t> grossBeatState (const std::array<int, 9>& params, const std::array<std::uint8_t, 9>& options,
  549. const std::map<int, std::vector<GbPoint>>& slots)
  550. {
  551. std::vector<std::uint8_t> b;
  552. 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); };
  553. auto i32 = [&put] (std::int32_t v) { put (&v, 4); };
  554. i32 (0x00080003);
  555. for (auto v : params)
  556. i32 (v);
  557. put (options.data(), options.size());
  558. for (int k = 0; k < 72; ++k)
  559. {
  560. b.push_back (0); // empty name
  561. i32 (0), i32 (5), i32 (0); // trigger sync off, position sync 4 beats, no hold
  562. b.push_back (0), b.push_back (0), b.push_back (0);
  563. const auto it = slots.find (k);
  564. const std::vector<GbPoint> flat { { 0.0, 1.0, 0 }, { 4.0, 1.0, 0 } };
  565. const auto& pts = it != slots.end() ? it->second : flat;
  566. i32 (3), i32 (3), i32 ((std::int32_t) pts.size());
  567. for (const auto& p : pts)
  568. {
  569. put (&p.dx, 8), put (&p.y, 8);
  570. const float tension = 0.0f;
  571. put (&tension, 4);
  572. b.push_back (p.type), b.push_back (0), b.push_back (0), b.push_back (2);
  573. }
  574. i32 (0), i32 (-1), i32 (-1), i32 (-1), i32 (-1);
  575. }
  576. return b;
  577. }
  578. std::vector<float> renderGrossBeat (const lynx::EffectSlot& slot, const std::vector<float>& in, double rate, double bpm)
  579. {
  580. auto fx = lynx::makeEffect (slot);
  581. fx->prepare (rate);
  582. auto l = in, r = in;
  583. const auto tps = bpm / 60.0 * lynx::ppq / rate;
  584. for (std::size_t done = 0; done < l.size(); done += 512)
  585. {
  586. const auto n = (int) std::min<std::size_t> (512, l.size() - done);
  587. fx->setSongPosition ((double) done * tps);
  588. fx->process (l.data() + done, r.data() + done, n, slot.params, bpm);
  589. }
  590. return l;
  591. }
  592. } // namespace
  593. TEST (gross_beat_state_container_and_parameters)
  594. {
  595. const auto state = grossBeatState ({ 3, 40, 128, 256, 1765, 5000, -77, 0, 0x10000 }, { 0, 0, 0, 0, 0, 0, 0, 1, 1 }, {});
  596. const auto parsed = lynx::grossbeat::parseState (state.data(), state.size());
  597. CHECK (parsed.has_value());
  598. CHECK (parsed && parsed->version == 8 && parsed->params[6] == -77 && parsed->slots[0].flat && parsed->slots[71].flat);
  599. const auto slot = lynx::grossbeat::slotFromFl (state, 960);
  600. CHECK (slot && slot->kind == lynx::EffectKind::grossBeat);
  601. std::vector<std::uint8_t> fl;
  602. int ppq = 0;
  603. CHECK (slot && lynx::grossbeat::unpack (slot->state, fl, ppq) && fl == state && ppq == 960);
  604. // every raw FL value survives the 0..1 parameter
  605. const int lo[8] { 0, 0, 0, 0, 0, 0, -256, 0 }, hi[8] { 35, 35, 256, 256, 10000, 10000, 256, 1 };
  606. bool roundTrip = true;
  607. for (int i = 0; i < 8; ++i)
  608. for (int raw = lo[i]; raw <= hi[i]; ++raw)
  609. roundTrip = roundTrip && lynx::grossbeat::rawFromParam (i, lynx::grossbeat::paramFromRaw (i, raw)) == raw;
  610. CHECK (roundTrip);
  611. CHECK (slot && slot->params[1] == lynx::grossbeat::paramFromRaw (1, 35)); // the volume slot 40 is clamped to 35
  612. CHECK (! lynx::grossbeat::parseState (state.data(), 100).has_value());
  613. }
  614. TEST (gross_beat_volume_slot_gates_the_bar)
  615. {
  616. // volume slot 1: open for two beats, closed for two (hold segments); no smoothing; dry path 15 samples late
  617. 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 } };
  618. const auto state = grossBeatState ({ 0, 1, 256, 256, 0, 0, 0, 0, 0x10000 }, { 0, 0, 0, 0, 0, 0, 0, 1, 1 }, { { 37, gate } });
  619. const auto slot = lynx::grossbeat::slotFromFl (state);
  620. CHECK (slot.has_value());
  621. if (! slot)
  622. return;
  623. const auto out = renderGrossBeat (*slot, std::vector<float> (200000, 1.0f), 48000.0, 120.0); // a beat = 24000 samples
  624. CHECK (out[14] == 0.0f && out[15] == 1.0f);
  625. CHECK (out[30000] == 1.0f && out[47990] == 1.0f);
  626. CHECK (out[48010] == 0.0f && out[95990] == 0.0f);
  627. CHECK (out[96010] == 1.0f && out[150000] == 0.0f);
  628. }
  629. TEST (gross_beat_time_slot_reads_the_past_bar)
  630. {
  631. // time slot 1 at level 0.5: the buffer is read one bar back (two bars = level 0), plus the 15-sample latency
  632. const std::vector<GbPoint> half { { 0.0, 0.5, 0 }, { 4.0, 0.5, 2 } };
  633. const auto state = grossBeatState ({ 1, 0, 256, 256, 0, 0, 0, 0, 0x10000 }, { 0, 0, 0, 0, 0, 0, 0, 1, 1 }, { { 1, half } });
  634. const auto slot = lynx::grossbeat::slotFromFl (state);
  635. CHECK (slot.has_value());
  636. if (! slot)
  637. return;
  638. std::vector<float> in (150000, 0.0f);
  639. in[1000] = 1.0f;
  640. const auto out = renderGrossBeat (*slot, in, 48000.0, 120.0); // a bar = 96000 samples
  641. std::size_t peak = 0;
  642. for (std::size_t i = 0; i < out.size(); ++i)
  643. if (std::abs (out[i]) > std::abs (out[peak]))
  644. peak = i;
  645. CHECK (peak == 1000 + 96000 + 15);
  646. CHECK (std::abs (out[peak] - 1.0f) < 1e-6f);
  647. float elsewhere = 0.0f;
  648. for (std::size_t i = 0; i < out.size(); ++i)
  649. if (i + 20 < peak || i > peak + 20)
  650. elsewhere = std::max (elsewhere, std::abs (out[i]));
  651. CHECK (elsewhere == 0.0f);
  652. }
  653. TEST (volume_gate_follows_bar_position)
  654. {
  655. auto fx = lynx::makeEffect (lynx::EffectKind::volumeGate);
  656. fx->prepare (48000.0);
  657. auto slot = lynx::makeEffectSlot (lynx::EffectKind::volumeGate);
  658. for (int i = 0; i < 32; ++i)
  659. slot.params[(std::size_t) i] = i < 16 ? 1.0f : 0.0f; // first two beats open, two closed
  660. slot.params[33] = slot.params[34] = 0.0f; // no smoothing
  661. // 120 BPM: one beat = 0.5 s = 24000 samples; the 4-beat bar starts at tick 0
  662. std::vector<float> l (96000, 1.0f), r (96000, 1.0f);
  663. fx->setSongPosition (0.0);
  664. for (int done = 0; done < 96000; done += 512)
  665. fx->process (l.data() + done, r.data() + done, std::min (512, 96000 - done), slot.params, 120.0);
  666. CHECK (l[10000] > 0.99f && l[40000] > 0.99f); // beats 1 and 2
  667. CHECK (l[60000] < 0.01f && l[90000] < 0.01f); // beats 3 and 4
  668. // starting mid-bar (beat 3): already closed
  669. std::vector<float> l2 (4800, 1.0f), r2 (4800, 1.0f);
  670. fx->reset();
  671. fx->setSongPosition (2.0 * lynx::ppq);
  672. fx->process (l2.data(), r2.data(), 4800, slot.params, 120.0);
  673. CHECK (l2[4000] < 0.01f);
  674. // mix 0: transparent
  675. slot.params[32] = 0.0f;
  676. std::vector<float> l3 (4800, 1.0f), r3 (4800, 1.0f);
  677. fx->setSongPosition (2.0 * lynx::ppq);
  678. fx->process (l3.data(), r3.data(), 4800, slot.params, 120.0);
  679. CHECK (l3[4000] > 0.99f);
  680. }
  681. TEST (patcher_branch_heads_follow_rea)
  682. {
  683. auto words = [] (std::vector<std::int32_t> v) {
  684. std::vector<std::uint8_t> b (v.size() * 4);
  685. std::memcpy (b.data(), v.data(), b.size());
  686. return b;
  687. };
  688. // Frequency Splitter, 2 bands, destinations 1/2/3, slope combo 3 (24 dB/oct), high send −6 dB
  689. std::vector<std::int32_t> fs (18, 0);
  690. fs[0] = 4;
  691. fs[1] = 32768;
  692. fs[3] = 3;
  693. fs[6] = 0;
  694. fs[11] = -600;
  695. fs[15] = 1;
  696. fs[16] = 2;
  697. fs[17] = 3;
  698. const auto split = words (fs);
  699. const auto low = lynx::patcherBranchHead ("Frequency Splitter", split, 1);
  700. CHECK (low.valid && low.slot && low.note.empty());
  701. if (low.slot)
  702. {
  703. // the exact engine (REA_rodada62): the low band's send, 24 dB/oct
  704. CHECK (low.slot->kind == lynx::EffectKind::frequencySplitter && low.slot->params[26] == 1.0f && low.slot->params[27] == 0.0f);
  705. CHECK (low.slot->params[2] == 3.0f / 7.0f);
  706. }
  707. const auto mid = lynx::patcherBranchHead ("Frequency Splitter", split, 2);
  708. 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
  709. const auto high = lynx::patcherBranchHead ("Frequency Splitter", split, 3);
  710. CHECK (high.valid && high.slot && high.slot->params[28] == 1.0f && high.slot->params[10] == (float) (1200.0 / 3600.0));
  711. CHECK (! lynx::patcherBranchHead ("Frequency Splitter", split, 7).valid);
  712. // Stereo Shaper: the difference destination (4) is the exact engine with its difference output (REA_rodada54)
  713. const auto shaper = words ({ 3, 0, 12800, 12800, 0, 0, 0, 4, 0 });
  714. const auto diff = lynx::patcherBranchHead ("Fruity Stereo Shaper", shaper, 4);
  715. CHECK (diff.valid && diff.slot && diff.slot->kind == lynx::EffectKind::stereoShaper && diff.slot->params[7] == 1.0f);
  716. CHECK (! lynx::patcherBranchHead ("Fruity Stereo Shaper", shaper, 2).valid);
  717. }
  718. TEST (parallel_container_sums_branches)
  719. {
  720. // two branches: dry with gain 0.5 and a utility that flips phase (matrix −1) with gain 0.5 -> silence
  721. auto slot = lynx::makeEffectSlot (lynx::EffectKind::parallel);
  722. lynx::EffectBranch dry;
  723. dry.gain = 0.5f;
  724. lynx::EffectBranch inverted;
  725. inverted.gain = 0.5f;
  726. auto flip = lynx::makeEffectSlot (lynx::EffectKind::utility);
  727. flip.params[2] = flip.params[5] = 0.25f; // (−1 + 2)/4
  728. inverted.chain.push_back (flip);
  729. slot.branches = { dry, inverted };
  730. auto fx = lynx::makeParallelEffect (slot);
  731. fx->prepare (48000.0);
  732. std::vector<float> l (9000, 0.8f), r (9000, -0.4f);
  733. fx->process (l.data(), r.data(), 9000, slot.params, 120.0);
  734. CHECK (std::abs (l[100]) < 1e-5f && std::abs (r[8999]) < 1e-5f);
  735. // only the dry branch with gain 2: doubles
  736. slot.branches = { lynx::EffectBranch { {}, 2.0f } };
  737. auto doubler = lynx::makeParallelEffect (slot);
  738. doubler->prepare (48000.0);
  739. std::fill (l.begin(), l.end(), 0.25f);
  740. doubler->process (l.data(), r.data(), 9000, slot.params, 120.0);
  741. CHECK (std::abs (l[5000] - 0.5f) < 1e-6f);
  742. }
  743. TEST (fl_controller_constant_outputs)
  744. {
  745. std::vector<std::int32_t> xy { 2, 32768, 0, 65536, 0, 16384, 0, 65536, 0, 1024, 256 };
  746. std::vector<std::uint8_t> blob (xy.size() * 4 + 8, 0);
  747. std::memcpy (blob.data(), xy.data(), xy.size() * 4);
  748. CHECK (std::abs (lynx::controllerConstantOutput ("Fruity X-Y Controller", blob, 0).value_or (-1.0f) - 0.5f) < 1e-4f);
  749. CHECK (std::abs (lynx::controllerConstantOutput ("Fruity X-Y Controller", blob, 1).value_or (-1.0f) - 0.25f) < 1e-4f);
  750. // Control Surface: version + group { 0x836: value 0.75, -, output 2 }
  751. std::vector<std::uint8_t> cs (4, 0);
  752. cs[0] = 1;
  753. auto chunk = [] (std::vector<std::uint8_t>& out, std::uint32_t id, const std::vector<std::uint8_t>& body) {
  754. const auto size = (std::uint64_t) body.size();
  755. out.insert (out.end(), reinterpret_cast<const std::uint8_t*> (&id), reinterpret_cast<const std::uint8_t*> (&id) + 4);
  756. out.insert (out.end(), reinterpret_cast<const std::uint8_t*> (&size), reinterpret_cast<const std::uint8_t*> (&size) + 8);
  757. out.insert (out.end(), body.begin(), body.end());
  758. };
  759. std::vector<std::uint8_t> value (12, 0);
  760. const float v = 0.75f;
  761. const std::int32_t out = 2;
  762. std::memcpy (value.data(), &v, 4);
  763. std::memcpy (value.data() + 8, &out, 4);
  764. std::vector<std::uint8_t> group;
  765. chunk (group, 0x836, value);
  766. chunk (cs, 0, group);
  767. CHECK (std::abs (lynx::controllerConstantOutput ("Control Surface", cs, 2).value_or (-1.0f) - 0.75f) < 1e-6f);
  768. CHECK (! lynx::controllerConstantOutput ("Control Surface", cs, 0).has_value());
  769. }
  770. TEST (flp_midi_out_notes_reach_wrapper_on_same_port)
  771. {
  772. auto record = [] (std::vector<std::uint8_t>& out, std::uint32_t id, std::vector<std::uint8_t> body) {
  773. const auto size = (std::uint64_t) body.size();
  774. out.insert (out.end(), reinterpret_cast<const std::uint8_t*> (&id), reinterpret_cast<const std::uint8_t*> (&id) + 4);
  775. out.insert (out.end(), reinterpret_cast<const std::uint8_t*> (&size), reinterpret_cast<const std::uint8_t*> (&size) + 8);
  776. out.insert (out.end(), body.begin(), body.end());
  777. };
  778. auto i32s = [] (std::vector<std::int32_t> v) {
  779. std::vector<std::uint8_t> b (v.size() * 4);
  780. std::memcpy (b.data(), v.data(), b.size());
  781. return b;
  782. };
  783. flp::Project p;
  784. flp::Channel midiOut;
  785. midiOut.index = 0;
  786. midiOut.type = flp::ChannelType::native;
  787. midiOut.pluginName = "MIDI Out";
  788. midiOut.pluginState = i32s ({ 7, 0, -1, -1, -1, 0, 3, 0 }); // channel 1, port 3 (@24)
  789. flp::Channel wrapper;
  790. wrapper.index = 1;
  791. wrapper.type = flp::ChannelType::native;
  792. wrapper.pluginName = "Fruity Wrapper";
  793. wrapper.pluginState = i32s ({ 9 });
  794. record (wrapper.pluginState, 0x32, i32s ({ 7 })); // VST3
  795. record (wrapper.pluginState, 0x01, i32s ({ 3, -1, 0, 0, 0 })); // input port 3
  796. p.channels = { midiOut, wrapper };
  797. flp::Pattern pattern;
  798. pattern.index = 1;
  799. flp::Note note;
  800. note.channel = 0;
  801. note.key = 64;
  802. note.length = 96;
  803. pattern.notes.push_back (note);
  804. p.patterns.push_back (pattern);
  805. const auto song = lynx::importFlp (p);
  806. CHECK (song.channels.size() == 2 && song.channels[0].muted);
  807. CHECK (song.channels[1].plugin.foreign.midiInPort == 3);
  808. int toWrapper = 0;
  809. for (const auto& pat : song.patterns)
  810. for (const auto& n : pat.notes)
  811. toWrapper += n.channel == 1 && n.key == 64;
  812. CHECK (toWrapper == 1);
  813. }
  814. TEST (flp_effect_automation_follows_slots_and_params)
  815. {
  816. // track 1: slot 0 with an effect that has no equivalent (dropped), slot 2 with Parametric EQ 2
  817. flp::Project p;
  818. flp::MixerInsert insert;
  819. insert.index = 1;
  820. insert.slots.push_back ({ 0, "Some Unknown FX", {} });
  821. insert.slots.push_back ({ 2, "Fruity Parametric EQ 2", {} });
  822. p.mixer.push_back (insert);
  823. auto clip = [&] (int index, std::uint32_t target) {
  824. flp::Channel c;
  825. c.index = index;
  826. c.type = flp::ChannelType::automationClip;
  827. c.automation = { { 0.0, 0.0 }, { 96.0, 1.0 } };
  828. p.channels.push_back (c);
  829. p.automationLinks.push_back ({ index, { target } });
  830. };
  831. 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; };
  832. clip (0, mixer (1, 2, 0x8000 + 7)); // EQ band 1 frequency
  833. clip (1, mixer (1, 2, 0x1F01)); // EQ slot mix
  834. clip (2, mixer (1, 0, 0x8000)); // parameter of the dropped effect
  835. clip (3, mixer (1, 2, 0x8000 + 21)); // band type: no equivalent
  836. const auto song = lynx::importFlp (p);
  837. CHECK (song.automations.size() == 4);
  838. CHECK (song.mixer[1].effects.size() == 1 && song.mixer[1].effects[0].kind == lynx::EffectKind::parametricEq);
  839. const auto& freq = song.automations[0];
  840. CHECK (freq.target.kind == lynx::AutomationTargetKind::effectParam && freq.target.index == 1 && freq.target.slot == 0);
  841. CHECK (freq.target.param == lynx::peq::param (0, 1));
  842. // FL's 0..1 (20 Hz..20 kHz) becomes our frequency scale
  843. CHECK (std::abs (freq.points.front().value - lynx::peq::hzValue (20.0)) < 1e-4f);
  844. CHECK (std::abs (freq.points.back().value - lynx::peq::hzValue (20000.0)) < 1e-4f);
  845. CHECK (song.automations[1].target.kind == lynx::AutomationTargetKind::effectMix && song.automations[1].target.slot == 0);
  846. CHECK (song.automations[2].target.kind == lynx::AutomationTargetKind::none);
  847. CHECK (song.automations[3].target.kind == lynx::AutomationTargetKind::none);
  848. // the automated mix is read/written through the target
  849. auto copy = song;
  850. CHECK (copy.setTargetValue (song.automations[1].target, 0.25f) && std::abs (copy.mixer[1].effects[0].mix - 0.25f) < 1e-6f);
  851. CHECK (std::abs (copy.targetValue (song.automations[1].target) - 0.25f) < 1e-6f);
  852. }
  853. TEST (flp_real_files)
  854. {
  855. if (realFlpDir.empty())
  856. {
  857. std::printf (" (skipped: pass a folder with .flp files as argument)\n");
  858. return;
  859. }
  860. int files = 0, withNotes = 0, withPlaylist = 0, automations = 0, automationsWithTarget = 0, sends = 0;
  861. int threeOsc = 0, threeOscDecoded = 0, wrapped = 0, wrappedParsed = 0, markers = 0, signatures = 0, trackEqs = 0;
  862. int harmless = 0, harmlessDecoded = 0, harmlessParsed = 0, harmlessKind = 0, sytrus = 0, sytrusDecoded = 0, sytrusEngine = 0, sytrusRendered = 0,
  863. sytrusSounding = 0;
  864. int harmor = 0, harmorUsable = 0, harmorKind = 0, harmorSounding = 0;
  865. int gms = 0, gmsUsable = 0, gmsKind = 0, gmsSounding = 0, morphine = 0, morphineUsable = 0, morphineKind = 0, morphineSounding = 0, demoGoldens = 0;
  866. int ogun = 0, ogunUsable = 0, ogunKind = 0, ogunSounding = 0;
  867. int autogun = 0, autogunUsable = 0, autogunKind = 0, autogunWithSg = 0, autogunSounding = 0; // REA_rodada57
  868. int poizone = 0, poizoneUsable = 0, poizoneKind = 0, poizoneSounding = 0, poizoneGoldens = 0;
  869. int miniSynth = 0, miniSynthUsable = 0, miniSynthKind = 0, miniSynthSounding = 0, miniSynthGoldens = 0;
  870. int dx10 = 0, dx10Usable = 0, dx10Kind = 0, dx10Sounding = 0;
  871. int kepler = 0, keplerUsable = 0, keplerKind = 0, keplerSounding = 0;
  872. int toxic = 0, toxicUsable = 0, toxicKind = 0, toxicSounding = 0, toxicGoldens = 0; // REA_rodada45
  873. int threeOscKind = 0, threeOscSounding = 0;
  874. std::map<std::string, std::pair<int, int>> generators; // GMS/Ogun/Autogun/Sakura/DrumSynth: channels, decoded
  875. int ogunTrumpet = 0; // Ogun channels whose spectrum matches the REA "Trumpet" reference
  876. int peqSlots = 0, peqShaped = 0, reverbs = 0, reverbsDryless = 0, reverbsLowCut = 0;
  877. int dwChannels = 0, dwZones = 0, fpcKits = 0, padChannels = 0, padNotes = 0, dwExact = 0;
  878. int harmlessCompWanted = 0, harmlessCompLoaded = 0; // Harmless rack compressors / presets read from the installation
  879. int maximusSlots = 0, maximusExact = 0, sgzSlots = 0, sgzExact = 0, vocodexSlots = 0, vocodexWithData = 0; // REA_rodada40
  880. int shaperSlots = 0, shaperExact = 0, clipperSlots = 0, clipperExact = 0; // REA_rodada51
  881. int flPeqSlots = 0, flPeqExact = 0; // REA_rodada50
  882. int basicSlots = 0, basicExact = 0; // REA_rodada58: PanOMatic, Balance, Blood Overdrive
  883. int equoSlots = 0, equoExact = 0, pitchShifterSlots = 0, pitchShifterExact = 0; // REA_rodada61
  884. int filterSlots = 0, filterExact = 0; // REA_rodada63: Fast LP, Free Filter, Fruity Filter
  885. int distructorSlots = 0, distructorExact = 0, distructorImpulses = 0; // REA_rodada71
  886. int delay2Slots = 0, delay2Exact = 0, delay3Slots = 0, delay3Exact = 0; // REA_rodada52
  887. int chorusSlots = 0, chorusExact = 0; // REA_rodada59: the exact choruses
  888. int fastDistSlots = 0, fastDistExact = 0, flangusSlots = 0, flangusExact = 0, flangerSlots = 0, flangerExact = 0; // REA_rodada55
  889. // import with the installation's samples (realFlpDir is ...\Data): for DirectWave/FPC
  890. lynx::FlpImportOptions withFiles;
  891. const auto flRoot = std::filesystem::path (realFlpDir).parent_path().string();
  892. withFiles.roots = { flRoot, realFlpDir, "" };
  893. withFiles.fileExists = [] (const std::string& path) {
  894. auto local = path;
  895. std::replace (local.begin(), local.end(), '\\', '/');
  896. return std::filesystem::exists (local);
  897. };
  898. withFiles.readFile = [] (const std::string& path) {
  899. auto local = path;
  900. std::replace (local.begin(), local.end(), '\\', '/');
  901. std::ifstream in (local, std::ios::binary);
  902. return std::vector<std::uint8_t> ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  903. };
  904. int embeddedSaved = 0;
  905. withFiles.saveEmbedded = [&embeddedSaved] (const std::vector<std::uint8_t>& wav, const std::string&) {
  906. ++embeddedSaved;
  907. return wav.size() > 44 ? std::string ("embutido.wav") : std::string();
  908. };
  909. int slicedChannels = 0, flKeysChannels = 0;
  910. for (auto& entry : std::filesystem::recursive_directory_iterator (realFlpDir))
  911. {
  912. if (entry.path().extension() != ".flp")
  913. continue;
  914. std::ifstream in (entry.path(), std::ios::binary);
  915. std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  916. ++files;
  917. try
  918. {
  919. const auto p = flp::parseProject (bytes);
  920. CHECK (! p.version.empty());
  921. CHECK (p.tempo > 10.0 && p.tempo < 1000.0);
  922. const auto song = lynx::importFlp (p);
  923. CHECK (! song.patterns.empty());
  924. {
  925. const auto full = lynx::importFlp (p, withFiles);
  926. bool hasFpc = false;
  927. for (const auto& c : p.channels)
  928. hasFpc = hasFpc || c.pluginName == "FPC";
  929. // GMS and Morphine (REA_rodada39): every state plays on the exact engine (GMS with its waves from the
  930. // installation) and sounds; two demo channels match golden samples of our round-39 models
  931. for (const auto& c : p.channels)
  932. {
  933. if (c.pluginName == "GMS")
  934. ++gms, gmsUsable += lynx::gmsStateUsable (c.pluginState) ? 1 : 0;
  935. if (c.pluginName == "Morphine")
  936. ++morphine, morphineUsable += lynx::morphineStateUsable (c.pluginState) ? 1 : 0;
  937. }
  938. // Kepler: every state in the demos plays on the exact engine (REA_rodada46) when the engine DLL is installed
  939. for (const auto& c : p.channels)
  940. if (c.pluginName == "Kepler")
  941. {
  942. ++kepler;
  943. keplerUsable += lynx::keplerStateUsable (c.pluginState) ? 1 : 0;
  944. }
  945. for (const auto& c : full.channels)
  946. if (c.instrument == lynx::InstrumentKind::kepler)
  947. {
  948. ++keplerKind;
  949. 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 } },
  950. 44100.0, 11025);
  951. double e = 0.0;
  952. bool finite = ! y.empty();
  953. for (auto v : y)
  954. finite = finite && std::isfinite (v), e += (double) v * v;
  955. CHECK (finite);
  956. keplerSounding += e > 1e-6 ? 1 : 0;
  957. }
  958. const auto file = entry.path().filename().string();
  959. for (const auto& c : full.channels)
  960. {
  961. const auto isGms = c.instrument == lynx::InstrumentKind::gms, isMorphine = c.instrument == lynx::InstrumentKind::morphine;
  962. if (! isGms && ! isMorphine)
  963. continue;
  964. (isGms ? gmsKind : morphineKind)++;
  965. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  966. const auto golden = (isGms && c.name == "GMS" && file.find ("Song For You") != std::string::npos)
  967. || (isMorphine && c.name == "Morphine" && file.find ("SHINE") != std::string::npos);
  968. std::vector<float> y;
  969. if (isGms)
  970. y = lynx::gms::renderReference (c.pluginState,
  971. { { 0, 60, (float) iv (100), 1.0, 0.0, 0.0, 30000 }, { 11025, 64, (float) iv (80), 1.0, 0.0, 0.0, 40000 },
  972. { 22050, 67, (float) iv (110), 1.0, 0.0, 0.0, 52000 }, { 60000, 48, (float) iv (127), 1.0, 0.0, 0.0, 70000 } },
  973. 44100.0, golden ? 88200 : 22050, c.samplePath);
  974. else
  975. 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 } },
  976. 44100.0, golden ? 100000 : 22050, c.samplePath);
  977. double e = 0.0;
  978. bool finite = ! y.empty();
  979. for (auto v : y)
  980. finite = finite && std::isfinite (v), e += (double) v * v;
  981. CHECK (finite);
  982. (isGms ? gmsSounding : morphineSounding) += e > 1e-6 ? 1 : 0;
  983. if (golden && ! y.empty())
  984. {
  985. // our model, same events (GMS: tempo 140; Morphine: tempo 120), state as saved, 44.1 kHz
  986. const auto rms = std::sqrt (e / (double) y.size());
  987. CHECK (std::abs (rms - (isGms ? 0.213602668574 : 0.0673606448152)) < 1e-5);
  988. 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 } },
  989. 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 } };
  990. for (const auto& g : isGms ? std::vector (std::begin (gmsAt), std::end (gmsAt)) : std::vector (std::begin (morAt), std::end (morAt)))
  991. 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);
  992. ++demoGoldens;
  993. }
  994. }
  995. // MiniSynth (REA_rodada41): every state plays on the exact engine (tables from the installation's
  996. // oscdata.dat) and sounds; two demo channels match golden samples of our round-41 model (bit-identical to
  997. // renders of the installation's DLL for these states)
  998. for (const auto& c : p.channels)
  999. if (c.pluginName == "MiniSynth")
  1000. ++miniSynth, miniSynthUsable += lynx::miniSynthStateUsable (c.pluginState) ? 1 : 0;
  1001. for (const auto& c : full.channels)
  1002. {
  1003. if (c.instrument != lynx::InstrumentKind::minisynth)
  1004. continue;
  1005. ++miniSynthKind;
  1006. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  1007. const auto exo = file.find ("Exoplanet") != std::string::npos;
  1008. const auto golden = exo && (c.name == "Main Lead 1" || c.name == "Riser");
  1009. const auto y = lynx::minisynth::renderReference (c.pluginState,
  1010. { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 }, { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  1011. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 }, { 60000, 48, iv (127), 1.0, 0.0, 0.0, 70000 } },
  1012. 44100.0, golden ? 88200 : 22050, c.samplePath);
  1013. double e = 0.0;
  1014. bool finite = ! y.empty();
  1015. for (auto v : y)
  1016. finite = finite && std::isfinite (v), e += (double) v * v;
  1017. CHECK (finite);
  1018. miniSynthSounding += e > 1e-6 ? 1 : 0;
  1019. if (golden && ! y.empty())
  1020. {
  1021. // our model, same events, tempo 120, state as saved, 44.1 kHz, host blocks of 512, L = R = Final.Vol
  1022. const auto lead = c.name == "Main Lead 1";
  1023. CHECK (std::abs (std::sqrt (e / (double) y.size()) - (lead ? 0.177495801753 : 0.19006567413)) < 1e-9);
  1024. 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 } },
  1025. riserAt[] = { { 2000, -0.0603227727f }, { 15000, 0.112200871f }, { 30000, 0.0453962795f }, { 50000, 0.234380022f }, { 65000, -0.164545432f }, { 80000, -0.000485120923f } };
  1026. for (const auto& g : lead ? std::vector (std::begin (leadAt), std::end (leadAt)) : std::vector (std::begin (riserAt), std::end (riserAt)))
  1027. CHECK (y[2 * (std::size_t) g.i] == g.v && y[2 * (std::size_t) g.i + 1] == g.v);
  1028. ++miniSynthGoldens;
  1029. }
  1030. }
  1031. // PoiZone (REA_rodada44): every state plays on the exact engine and sounds; the demo channel matches golden
  1032. // samples of our round-44 model
  1033. for (const auto& c : p.channels)
  1034. if (c.pluginName == "PoiZone")
  1035. ++poizone, poizoneUsable += lynx::poizoneStateUsable (c.pluginState) ? 1 : 0;
  1036. for (const auto& c : full.channels)
  1037. {
  1038. if (c.instrument != lynx::InstrumentKind::poizone)
  1039. continue;
  1040. ++poizoneKind;
  1041. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  1042. const auto golden = c.name == "PoiZone" && file.find ("NIGHT TERROR") != std::string::npos;
  1043. const auto y = lynx::poizone::renderReference (c.pluginState,
  1044. { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 }, { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  1045. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 }, { 60000, 48, iv (127), 1.0, 0.0, 0.0, 70000 } },
  1046. 44100.0, golden ? 88200 : 22050);
  1047. double e = 0.0;
  1048. bool finite = ! y.empty();
  1049. for (auto v : y)
  1050. finite = finite && std::isfinite (v), e += (double) v * v;
  1051. CHECK (finite);
  1052. poizoneSounding += e > 1e-6 ? 1 : 0;
  1053. if (golden && ! y.empty())
  1054. {
  1055. // our model, same events, tempo 120, state as saved, 44.1 kHz, L = R = Final.Vol
  1056. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.018975758213507415) < 1e-7);
  1057. 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 } };
  1058. for (const auto& g : at)
  1059. 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);
  1060. ++poizoneGoldens;
  1061. }
  1062. }
  1063. // Toxic Biohazard (REA_rodada45): every demo state plays on the exact engine with the waves read from the
  1064. // installation's DLL and sounds; the "Bombs Away" channel matches golden samples of our round-45 model
  1065. for (const auto& c : p.channels)
  1066. if (c.pluginName == "Toxic Biohazard")
  1067. ++toxic, toxicUsable += lynx::toxicStateUsable (c.pluginState) ? 1 : 0;
  1068. for (const auto& c : full.channels)
  1069. {
  1070. if (c.instrument != lynx::InstrumentKind::toxic)
  1071. continue;
  1072. ++toxicKind;
  1073. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  1074. const auto golden = file.find ("Bombs Away") != std::string::npos;
  1075. const auto y = lynx::toxic::renderReference (
  1076. c.pluginState,
  1077. { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 }, { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  1078. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 }, { 60000, 48, iv (127), 1.0, 0.0, 0.0, 70000 } },
  1079. 44100.0, golden ? 88200 : 22050, c.samplePath, 512, 140.0);
  1080. double e = 0.0;
  1081. bool finite = ! y.empty();
  1082. for (auto v : y)
  1083. finite = finite && std::isfinite (v), e += (double) v * v;
  1084. CHECK (finite);
  1085. toxicSounding += e > 1e-6 ? 1 : 0;
  1086. if (golden && ! y.empty())
  1087. {
  1088. // our model (r45/py/toxmodel.py, which equals the DLL bit for bit), state as saved, tempo 140, 44.1 kHz
  1089. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.0832500924945) < 1e-6);
  1090. 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 } };
  1091. for (const auto& g : at)
  1092. 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);
  1093. ++toxicGoldens;
  1094. }
  1095. }
  1096. for (const auto& c : full.channels)
  1097. {
  1098. if (c.instrument == lynx::InstrumentKind::harmless && c.modelParams.size() >= (std::size_t) lynx::harmlessParamCount
  1099. && c.modelParams[(std::size_t) lynx::harmlessComboIndex + 3] > 0.5f)
  1100. {
  1101. ++harmlessCompWanted;
  1102. harmlessCompLoaded += c.modelParams.size() > (std::size_t) lynx::harmlessCompIndex + 1000 ? 1 : 0;
  1103. }
  1104. flKeysChannels += c.zones.size() == 30 && c.zones.front().path.find ("CHANGPIANO") != std::string::npos ? 1 : 0;
  1105. if (c.sampler.slices.size() >= 4)
  1106. {
  1107. ++slicedChannels;
  1108. CHECK (std::is_sorted (c.sampler.slices.begin(), c.sampler.slices.end()));
  1109. CHECK (c.sampler.slices.back() < 1.0f);
  1110. }
  1111. dwExact += c.instrument == lynx::InstrumentKind::directwave ? 1 : 0;
  1112. if (! c.zones.empty() && c.name.find (" - ") == std::string::npos)
  1113. {
  1114. ++dwChannels;
  1115. dwZones += (int) c.zones.size();
  1116. }
  1117. padChannels += c.name.find (" - ") != std::string::npos && c.instrument == lynx::InstrumentKind::sampler ? 1 : 0;
  1118. }
  1119. fpcKits += hasFpc;
  1120. // Maximus / Soundgoodizer become the exact engine (REA_rodada40); Vocodex carries SG.dat (and its
  1121. // carrier's Sytrus preset) read from the installation
  1122. const int basicSlots0 = basicSlots, basicExact0 = basicExact;
  1123. for (const auto& insert : p.mixer)
  1124. for (const auto& slot : insert.slots)
  1125. {
  1126. maximusSlots += slot.plugin == "Maximus";
  1127. delay2Slots += slot.plugin == "Fruity Delay 2";
  1128. delay3Slots += slot.plugin == "Fruity Delay 3";
  1129. sgzSlots += slot.plugin == "Soundgoodizer";
  1130. vocodexSlots += slot.plugin == "Vocodex";
  1131. shaperSlots += slot.plugin == "Fruity WaveShaper";
  1132. flPeqSlots += slot.plugin == "Fruity Parametric EQ 2" || slot.plugin == "Fruity Parametric EQ";
  1133. equoSlots += slot.plugin == "EQUO";
  1134. distructorSlots += slot.plugin == "Distructor";
  1135. pitchShifterSlots += slot.plugin == "Pitch Shifter";
  1136. clipperSlots += slot.plugin == "Fruity Soft Clipper";
  1137. basicSlots += slot.plugin == "Fruity PanOMatic" || slot.plugin == "Fruity Balance" || slot.plugin == "Fruity Blood Overdrive";
  1138. filterSlots += slot.plugin == "Fruity Fast LP" || slot.plugin == "Fruity Free Filter" || slot.plugin == "Fruity Filter";
  1139. chorusSlots += lynx::chorusControls (slot.plugin) > 0 && slot.state.size() >= 4 ? 1 : 0;
  1140. fastDistSlots += slot.plugin == "Fruity Fast Dist";
  1141. flangusSlots += slot.plugin == "Fruity Flangus";
  1142. flangerSlots += slot.plugin == "Fruity Flanger";
  1143. }
  1144. for (const auto& t : full.mixer)
  1145. for (const auto& fx : t.effects)
  1146. {
  1147. if (fx.kind == lynx::EffectKind::maximus)
  1148. {
  1149. const auto blob = lynx::maximusfx::unpack (fx.state);
  1150. CHECK (blob && lynx::maximusfx::presetOf (*blob) != nullptr);
  1151. if (blob)
  1152. (blob->build == lynx::hlfx::MaximusBuild::soundgoodizer ? sgzExact : maximusExact) += 1;
  1153. }
  1154. fastDistExact += fx.kind == lynx::EffectKind::fruityFastDist;
  1155. flangusExact += fx.kind == lynx::EffectKind::fruityFlangus;
  1156. flangerExact += fx.kind == lynx::EffectKind::fruityFlanger;
  1157. if (fx.kind == lynx::EffectKind::vocodex)
  1158. vocodexWithData += ! lynx::vocodex::splitInstallData (fx.state).second.sgDat.empty();
  1159. if (fx.kind == lynx::EffectKind::fruityWaveShaper)
  1160. shaperExact += lynx::flshape::parseWaveShaper (fx.state).has_value() ? 1 : 0;
  1161. clipperExact += fx.kind == lynx::EffectKind::fruitySoftClipper;
  1162. flPeqExact += fx.kind == lynx::EffectKind::parametricEq && lynx::peq::flLaw (fx.params);
  1163. basicExact += fx.kind == lynx::EffectKind::fruityPanOMatic || fx.kind == lynx::EffectKind::fruityBalance
  1164. || fx.kind == lynx::EffectKind::fruityBloodOverdrive;
  1165. equoExact += fx.kind == lynx::EffectKind::equo && lynx::equofx::parse (fx.state).has_value();
  1166. if (fx.kind == lynx::EffectKind::distructor)
  1167. if (const auto d = lynx::distructor::parseBlob (fx.state))
  1168. {
  1169. ++distructorExact;
  1170. for (const auto& m : d->modules)
  1171. distructorImpulses += m.type == 3 && ! m.impulseFile.empty();
  1172. }
  1173. pitchShifterExact += fx.kind == lynx::EffectKind::flPitchShifter;
  1174. filterExact += fx.kind == lynx::EffectKind::fruityFastLP || fx.kind == lynx::EffectKind::fruityFreeFilter
  1175. || fx.kind == lynx::EffectKind::fruityFilter;
  1176. delay2Exact += fx.kind == lynx::EffectKind::fruityDelay2;
  1177. delay3Exact += fx.kind == lynx::EffectKind::fruityDelay3;
  1178. chorusExact += fx.kind == lynx::EffectKind::fruityChorus || fx.kind == lynx::EffectKind::hyperChorus || fx.kind == lynx::EffectKind::vintageChorus ? 1 : 0;
  1179. }
  1180. if (basicSlots - basicSlots0 != basicExact - basicExact0)
  1181. std::printf (" %s: %d PanOMatic/Balance/Blood Overdrive slots, %d exact effects\n", file.c_str(),
  1182. basicSlots - basicSlots0, basicExact - basicExact0);
  1183. for (const auto& pat : full.patterns)
  1184. for (const auto& n : pat.notes)
  1185. {
  1186. CHECK (n.channel >= 0 && n.channel < (int) full.channels.size());
  1187. padNotes += full.channels[(std::size_t) n.channel].name.find (" - ") != std::string::npos ? 1 : 0;
  1188. }
  1189. }
  1190. const auto peqDefaults = lynx::makeEffectSlot (lynx::EffectKind::parametricEq).params;
  1191. for (const auto& t : song.mixer)
  1192. for (const auto& fx : t.effects)
  1193. {
  1194. if (fx.kind == lynx::EffectKind::parametricEq)
  1195. {
  1196. ++peqSlots;
  1197. peqShaped += fx.params != peqDefaults;
  1198. for (double hz : { 50.0, 1000.0, 10000.0 })
  1199. CHECK (std::isfinite (lynx::peq::responseDb (fx.params, 48000.0, hz)));
  1200. }
  1201. if (fx.kind == lynx::EffectKind::reverb)
  1202. {
  1203. ++reverbs;
  1204. reverbsDryless += fx.params[4] < 0.01f;
  1205. reverbsLowCut += fx.params[5] > 0.0f;
  1206. }
  1207. if (fx.kind == lynx::EffectKind::reeverb2) // FL's exact Reeverb 2 (REA_rodada49): FL's own controls
  1208. {
  1209. const auto r2 = lynx::reeverb2::fromParams (fx.params);
  1210. ++reverbs;
  1211. reverbsDryless += r2.p[10] == 0;
  1212. reverbsLowCut += r2.p[0] >= 20;
  1213. }
  1214. }
  1215. for (const auto& t : song.mixer)
  1216. if (t.eq.active())
  1217. {
  1218. ++trackEqs;
  1219. for (std::size_t b = 0; b < 3; ++b)
  1220. CHECK (t.eq.hz[b] >= 10.0f && t.eq.hz[b] <= 16000.0f && std::abs (t.eq.gainDb[b]) <= 18.0f);
  1221. }
  1222. for (const auto& a : song.arrangements)
  1223. for (const auto& m : a.markers)
  1224. {
  1225. ++markers;
  1226. signatures += m.isSignature();
  1227. CHECK (m.tick >= 0);
  1228. }
  1229. // Fruity Wrapper (channels and mixer slots): every blob in the demos is read
  1230. auto countWrapper = [&] (const std::string& plugin, const std::vector<std::uint8_t>& state) {
  1231. if (plugin != "Fruity Wrapper")
  1232. return;
  1233. ++wrapped;
  1234. lynx::ForeignPlugin f;
  1235. wrappedParsed += lynx::parseFlWrapper (state, f) && f.format != lynx::ForeignPlugin::Format::none && ! f.name.empty();
  1236. };
  1237. for (const auto& c : p.channels)
  1238. countWrapper (c.pluginName, c.pluginState);
  1239. for (const auto& insert : p.mixer)
  1240. for (const auto& slot : insert.slots)
  1241. countWrapper (slot.plugin, slot.state);
  1242. // Harmless: every state in the demos becomes a synth, with finite values
  1243. for (const auto& c : p.channels)
  1244. if (c.pluginName == "Harmless")
  1245. {
  1246. ++harmless;
  1247. lynx::SynthSettings hs;
  1248. float g = 1.0f;
  1249. if (lynx::synthFromHarmless (c.pluginState, 128.0, hs, g))
  1250. {
  1251. ++harmlessDecoded;
  1252. CHECK (std::isfinite (g) && hs.cutoff >= 20.0f && hs.cutoff <= 20000.0f && std::isfinite (hs.decay) && std::isfinite (hs.release));
  1253. }
  1254. if (const auto hp = lynx::harmlessParams (c.pluginState))
  1255. harmlessParsed += (int) hp->size() == lynx::harmlessParamCount ? 1 : 0;
  1256. }
  1257. for (const auto& c : song.channels)
  1258. harmlessKind += c.instrument == lynx::InstrumentKind::harmless ? 1 : 0;
  1259. // Harmor: every state in the demos plays on the exact engine (REA_rodada34) and sounds, with finite samples
  1260. for (const auto& c : p.channels)
  1261. if (c.pluginName == "Harmor")
  1262. {
  1263. ++harmor;
  1264. harmorUsable += lynx::harmorStateUsable (c.pluginState) ? 1 : 0;
  1265. }
  1266. for (const auto& c : song.channels)
  1267. if (c.instrument == lynx::InstrumentKind::harmor)
  1268. {
  1269. ++harmorKind;
  1270. 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);
  1271. double e = 0.0;
  1272. bool finite = ! y.empty();
  1273. for (auto v : y)
  1274. finite = finite && std::isfinite (v), e += (double) v * v;
  1275. CHECK (finite);
  1276. harmorSounding += e > 1e-6 ? 1 : 0;
  1277. }
  1278. // Ogun: every state in the demos plays on the exact engine (REA_rodada37) and sounds, with finite samples
  1279. for (const auto& c : p.channels)
  1280. if (c.pluginName == "Ogun")
  1281. {
  1282. ++ogun;
  1283. ogunUsable += lynx::ogunStateUsable (c.pluginState) ? 1 : 0;
  1284. }
  1285. for (const auto& c : song.channels)
  1286. if (c.instrument == lynx::InstrumentKind::ogun)
  1287. {
  1288. ++ogunKind;
  1289. 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 },
  1290. { 2000, 67, 0.6, 1.0, 0.0, 0.0, 0.0, 0.0, 0.5, 8000 } }, 44100.0, 11025);
  1291. double e = 0.0;
  1292. bool finite = ! y.empty();
  1293. for (auto v : y)
  1294. finite = finite && std::isfinite (v), e += (double) v * v;
  1295. CHECK (finite);
  1296. ogunSounding += e > 1e-6 ? 1 : 0;
  1297. }
  1298. // Autogun (REA_rodada57): every state plays on Ogun's engine in its Autogun variant; with the installation, the
  1299. // Magic Dust stage gets Autogun\Data\SG.dat
  1300. {
  1301. bool any = false;
  1302. for (const auto& c : p.channels)
  1303. if (c.pluginName == "Autogun")
  1304. ++autogun, autogunUsable += lynx::autogunStateUsable (c.pluginState) ? 1 : 0, any = true;
  1305. if (any)
  1306. for (const auto& c : lynx::importFlp (p, withFiles).channels)
  1307. if (c.instrument == lynx::InstrumentKind::autogun)
  1308. {
  1309. ++autogunKind;
  1310. lynx::ogun::RefOptions opt;
  1311. opt.autogun = true;
  1312. if (! c.samplePath.empty())
  1313. {
  1314. auto local = c.samplePath;
  1315. std::replace (local.begin(), local.end(), '\\', '/');
  1316. std::ifstream sg (local, std::ios::binary);
  1317. opt.sgState.assign ((std::istreambuf_iterator<char> (sg)), std::istreambuf_iterator<char>());
  1318. autogunWithSg += opt.sgState.size() == 1342 ? 1 : 0;
  1319. }
  1320. 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 } },
  1321. 44100.0, 11025, opt);
  1322. double e = 0.0;
  1323. bool finite = ! y.empty();
  1324. for (auto v : y)
  1325. finite = finite && std::isfinite (v), e += (double) v * v;
  1326. CHECK (finite);
  1327. autogunSounding += e > 1e-6 ? 1 : 0;
  1328. }
  1329. }
  1330. // Fruity DX10: every state plays on the exact engine when its channel processor is neutral (REA_rodada42)
  1331. for (const auto& c : p.channels)
  1332. if (c.pluginName == "Fruity DX10" || c.pluginName == "DX10")
  1333. ++dx10, dx10Usable += lynx::dx10StateUsable (c.pluginState) ? 1 : 0;
  1334. for (const auto& c : song.channels)
  1335. if (c.instrument == lynx::InstrumentKind::dx10)
  1336. {
  1337. ++dx10Kind;
  1338. 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 } },
  1339. 44100.0, 11025);
  1340. double e = 0.0;
  1341. bool finite = ! y.empty();
  1342. for (auto v : y)
  1343. finite = finite && std::isfinite (v), e += (double) v * v;
  1344. CHECK (finite);
  1345. dx10Sounding += e > 1e-6 ? 1 : 0;
  1346. }
  1347. // 3x Osc: every channel plays on the exact engine (REA_rodada47)
  1348. for (const auto& c : song.channels)
  1349. if (c.instrument == lynx::InstrumentKind::threeosc)
  1350. {
  1351. ++threeOscKind;
  1352. 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 } },
  1353. 44100.0, 11025);
  1354. double e = 0.0;
  1355. bool finite = ! y.empty();
  1356. for (auto v : y)
  1357. finite = finite && std::isfinite (v), e += (double) v * v;
  1358. CHECK (finite);
  1359. threeOscSounding += e > 1e-6 ? 1 : 0;
  1360. }
  1361. // Sytrus: every state in the demos (zlib + 10 units) decodes, with finite values
  1362. for (const auto& c : p.channels)
  1363. if (c.pluginName == "Sytrus")
  1364. {
  1365. ++sytrus;
  1366. lynx::SynthSettings ss;
  1367. float g = 1.0f;
  1368. // the Sytrus engine (REA_rodada31) reads every demo state; the first 24 play a short note (render settings)
  1369. if (lynx::sytrus::stateUsable (c.pluginState))
  1370. {
  1371. ++sytrusEngine;
  1372. if (sytrusRendered < 24)
  1373. {
  1374. ++sytrusRendered;
  1375. lynx::sytrus::Engine e;
  1376. e.prepare (44100.0, true, 512);
  1377. e.setPatch (lynx::sytrus::parsePatch (c.pluginState));
  1378. lynx::sytrus::NoteParams note;
  1379. e.noteOn (note);
  1380. std::vector<float> l (8192), r (8192);
  1381. e.render (l.data(), r.data(), 8192, 0.551f, 0.0f);
  1382. bool finite = true;
  1383. double energy = 0.0;
  1384. for (std::size_t k = 0; k < l.size(); ++k)
  1385. finite = finite && std::isfinite (l[k]) && std::isfinite (r[k]), energy += (double) l[k] * l[k];
  1386. CHECK (finite);
  1387. sytrusSounding += energy > 1e-6;
  1388. }
  1389. }
  1390. if (lynx::synthFromSytrus (c.pluginState, 128.0, ss, g))
  1391. {
  1392. ++sytrusDecoded;
  1393. CHECK (std::isfinite (g) && ss.cutoff >= 20.0f && ss.cutoff <= 20000.0f && std::isfinite (ss.attack) && std::isfinite (ss.decay)
  1394. && std::isfinite (ss.release) && ss.sustain >= 0.0f && ss.sustain <= 1.0f && std::isfinite (ss.filterEnv.amount));
  1395. for (const auto& o : ss.osc)
  1396. CHECK (o.level >= 0.0f && o.level <= 1.0f && o.octave >= -3 && o.octave <= 3);
  1397. }
  1398. }
  1399. // round 12 generators
  1400. for (const auto& c : p.channels)
  1401. {
  1402. lynx::SynthSettings gs;
  1403. float g = 1.0f;
  1404. int decoded = -1;
  1405. if (c.pluginName == "Harmor")
  1406. {
  1407. decoded = lynx::synthFromHarmor (c.pluginState, gs, g);
  1408. if (decoded)
  1409. {
  1410. // REA_rodada25 §4: most resonant settings stay below Q ≈ 3, a few saturate
  1411. CHECK (gs.resonance >= 0.0f && gs.resonance <= 1.0f);
  1412. static int resonant = 0, strong = 0, total = 0;
  1413. resonant += gs.resonance > 0.0f;
  1414. strong += gs.resonance > 0.9f;
  1415. if (++total % 50 == 0)
  1416. std::printf (" Harmor resonance: %d with a peak, %d with Q > ~5 (of %d)\n", resonant, strong, total);
  1417. }
  1418. static int saw = 0, square = 0, other = 0, total = 0;
  1419. if (decoded && std::any_of (gs.osc.begin(), gs.osc.end(), [] (const lynx::SynthOscillator& o) { return o.wave == lynx::Waveform::spectral; }))
  1420. {
  1421. auto isSaw = true, isSquare = true;
  1422. for (int n = 1; n <= 16; ++n)
  1423. {
  1424. isSaw = isSaw && std::abs (gs.spectrum[(std::size_t) n - 1] - 1.0f / n) < 1e-3f;
  1425. isSquare = isSquare && std::abs (gs.spectrum[(std::size_t) n - 1] - (n % 2 == 1 ? 1.1535f / n : 0.0f)) < 2e-3f;
  1426. }
  1427. ++(isSaw ? saw : isSquare ? square : other);
  1428. if (++total % 20 == 0 || total == 100)
  1429. std::printf (" Harmor spectrum: %d saw, %d square, %d other (of %d)\n", saw, square, other, total);
  1430. }
  1431. }
  1432. else if (c.pluginName == "GMS")
  1433. decoded = lynx::synthFromGms (c.pluginState, 128.0, gs, g);
  1434. else if (c.pluginName == "Ogun" || c.pluginName == "Autogun")
  1435. {
  1436. decoded = lynx::synthFromOgun (c.pluginState, gs, g);
  1437. // REA_rodada24 §1.9: the "Trumpet" preset (richness 7, no seeds) has partials 0.9087, 0.8610,
  1438. // 0.8151… at the fixed seed-3 phases 0.919, 0.380, 0.130
  1439. if (decoded && gs.osc[0].wave == lynx::Waveform::spectral)
  1440. {
  1441. CHECK (std::all_of (gs.spectrum.begin(), gs.spectrum.end(), [] (float a) { return std::isfinite (a) && a >= 0.0f; }));
  1442. static constexpr float trumpet[3] { 0.9087f, 0.8610f, 0.8151f }, phases[3] { 0.919f, 0.380f, 0.130f };
  1443. auto match = true;
  1444. for (std::size_t k = 0; k < 3; ++k)
  1445. match = match && std::abs (gs.spectrum[k] * 2.0f / 3.14159265f - trumpet[k]) < 1e-3f && std::abs (gs.spectrumPhase[k] - phases[k]) < 1e-3f;
  1446. ogunTrumpet += match ? 1 : 0;
  1447. }
  1448. }
  1449. else if (c.pluginName == "Sakura")
  1450. decoded = lynx::synthFromSakura (c.pluginState, gs);
  1451. else if (c.pluginName == "Kepler")
  1452. decoded = lynx::synthFromKepler (c.pluginState, gs, g);
  1453. else if (c.pluginName == "Wave Traveller")
  1454. decoded = lynx::waveTravellerSample (c.pluginState).has_value() ? 2 : 0;
  1455. else if (c.pluginName == "PoiZone")
  1456. decoded = lynx::synthFromPoiZone (c.pluginState, gs, g);
  1457. else if (c.pluginName == "BassDrum")
  1458. decoded = lynx::synthFromBassDrum (c.pluginState, gs, g);
  1459. else if (c.pluginName == "Toxic Biohazard")
  1460. decoded = lynx::synthFromToxic (c.pluginState, gs, g);
  1461. else if (c.pluginName == "Morphine")
  1462. decoded = lynx::synthFromMorphine (c.pluginState, gs, g);
  1463. else if (c.pluginName == "BooBass")
  1464. decoded = lynx::synthFromBooBass (c.pluginState, gs);
  1465. else if (c.pluginName == "Drumpad" || c.pluginName == "DrumPad")
  1466. decoded = lynx::drumpadPatchName (c.pluginState).has_value() ? 2 : 0;
  1467. else if (c.pluginName == "Fruity Envelope Controller" && c.pluginState.size() >= 4 && c.pluginState[2] >= 6) // v >= 6
  1468. decoded = lynx::parseEnvelopeController (c.pluginState).has_value() ? 2 : 0;
  1469. else if (c.pluginName == "FLEX")
  1470. decoded = lynx::flexPresetName (c.pluginState).has_value() ? 2 : 0;
  1471. else if (c.pluginName == "Fruity DrumSynth Live")
  1472. decoded = lynx::drumSynthLiveParams (c.pluginState).has_value() ? 2 : 0;
  1473. if (decoded < 0)
  1474. continue;
  1475. auto& counter = generators[c.pluginName];
  1476. ++counter.first;
  1477. counter.second += decoded != 0;
  1478. if (decoded == 1 && c.pluginName != "Fruity DrumSynth Live")
  1479. {
  1480. CHECK (std::isfinite (g) && g >= 0.0f && gs.cutoff >= 20.0f && gs.cutoff <= 20000.0f && gs.sustain >= 0.0f && gs.sustain <= 1.0f);
  1481. 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));
  1482. for (const auto& o : gs.osc)
  1483. CHECK (o.level >= 0.0f && o.level <= 4.0f && std::isfinite (o.detune) && std::isfinite (o.pan));
  1484. }
  1485. }
  1486. // 3x Osc: every state in the demos decodes, with levels summing to at most 1
  1487. for (const auto& c : p.channels)
  1488. if (c.pluginName == "3x Osc")
  1489. {
  1490. ++threeOsc;
  1491. lynx::SynthSettings s;
  1492. if (lynx::synthFrom3xOsc (c.pluginState, s))
  1493. {
  1494. ++threeOscDecoded;
  1495. // with AM, osc 3 is the modulation depth and is left out of the sum
  1496. const auto sum = s.osc[0].level + s.osc[1].level + (s.am ? 0.0f : s.osc[2].level);
  1497. CHECK (sum <= 1.001f && s.osc[2].level <= 1.0f);
  1498. for (const auto& o : s.osc)
  1499. CHECK (o.octave >= -2 && o.octave <= 2 && std::abs (o.semitones) <= 11 && std::abs (o.pan) <= 1.0f);
  1500. }
  1501. }
  1502. for (auto& pat : p.patterns)
  1503. if (! pat.notes.empty())
  1504. {
  1505. ++withNotes;
  1506. break;
  1507. }
  1508. for (auto& a : p.arrangements)
  1509. for (auto& item : a.items)
  1510. CHECK (item.track >= 0 && item.track < 500);
  1511. for (auto& clip : song.playlist)
  1512. {
  1513. CHECK (clip.pattern >= 0 && clip.pattern < (int) song.patterns.size());
  1514. CHECK (clip.length > 0 && clip.offset >= 0);
  1515. if (clip.kind == lynx::ClipKind::automation)
  1516. CHECK (clip.automation >= 0 && clip.automation < (int) song.automations.size());
  1517. }
  1518. for (auto& a : song.automations)
  1519. {
  1520. ++automations;
  1521. automationsWithTarget += a.target.kind != lynx::AutomationTargetKind::none;
  1522. CHECK (a.isLive() || ! a.points.empty());
  1523. for (std::size_t i = 1; i < a.points.size(); ++i)
  1524. CHECK (a.points[i].tick >= a.points[i - 1].tick);
  1525. }
  1526. // imported routing has no cycles and valid destinations
  1527. CHECK (song.mixerOrder().size() + 1 == song.mixer.size());
  1528. for (auto& t : song.mixer)
  1529. for (auto& s : t.sends)
  1530. {
  1531. ++sends;
  1532. CHECK (s.target > 0 && s.target < (int) song.mixer.size());
  1533. }
  1534. if (! song.playlist.empty())
  1535. ++withPlaylist;
  1536. }
  1537. catch (const flp::Error& e)
  1538. {
  1539. ++failures;
  1540. std::printf (" FAILED %s: %s\n", entry.path().string().c_str(), e.what());
  1541. }
  1542. }
  1543. CHECK (threeOscDecoded == threeOsc);
  1544. std::printf (" Harmless: %d channels, %d decoded\n", harmless, harmlessDecoded);
  1545. std::printf (" Sytrus: %d channels, %d decoded, %d by the engine (%d of %d rendered sound)\n", sytrus, sytrusDecoded, sytrusEngine,
  1546. sytrusSounding, sytrusRendered);
  1547. CHECK (sytrusEngine == sytrus);
  1548. CHECK (sytrusSounding >= sytrusRendered * 3 / 4);
  1549. for (const auto& [name, counter] : generators)
  1550. {
  1551. std::printf (" %s: %d channels, %d decoded\n", name.c_str(), counter.first, counter.second);
  1552. CHECK (counter.second == counter.first);
  1553. }
  1554. std::printf (" Ogun spectra matching the Trumpet reference: %d\n", ogunTrumpet);
  1555. CHECK (ogunTrumpet >= 1);
  1556. // demo Gross Beat: the volume slot becomes the gate (those using an envelope slot get steps < 1)
  1557. {
  1558. int vocodexes = 0, vocodexSides = 0;
  1559. int lovePhilterLinks[4] {}; // live links: cutoff, resonance; inside containers: cutoff, resonance
  1560. int embeddedZones = 0, lovePhilters = 0, lovePhiltersRead = 0, lovePhiltersConverted = 0, gates = 0, shaped = 0, effectors = 0, patchers = 0, serial = 0, parallel = 0, generatorPatchers = 0;
  1561. for (const auto& entry : std::filesystem::recursive_directory_iterator (std::filesystem::path (realFlpDir) / "Demo projects"))
  1562. if (entry.path().extension() == ".flp")
  1563. {
  1564. std::ifstream in (entry.path(), std::ios::binary);
  1565. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  1566. try
  1567. {
  1568. const auto project = flp::parseProject (file);
  1569. for (const auto& insert : project.mixer)
  1570. for (const auto& slot : insert.slots)
  1571. if (slot.plugin == "Gross Beat")
  1572. {
  1573. // FL's own engine (REA_rodada53): every demo state is read
  1574. const auto fx = lynx::effectFromFl (slot.plugin, slot.state);
  1575. CHECK (fx && fx->kind == lynx::EffectKind::grossBeat);
  1576. ++gates;
  1577. if (fx && fx->kind == lynx::EffectKind::grossBeat && lynx::grossbeat::slotNames (fx->state, true).size() == 36)
  1578. ++shaped;
  1579. }
  1580. else if (slot.plugin == "Fruity Love Philter")
  1581. {
  1582. ++lovePhilters;
  1583. lovePhiltersRead += lynx::parseLovePhilter (slot.state).has_value();
  1584. if (const auto conv = lynx::lovePhilterEffects (slot.state))
  1585. {
  1586. ++lovePhiltersConverted;
  1587. CHECK (! conv->slots.empty());
  1588. for (const auto& fx : conv->slots)
  1589. for (auto v : fx.params)
  1590. CHECK (std::isfinite (v) && v >= 0.0f && v <= 1.0f);
  1591. for (const auto& fx : conv->slots)
  1592. for (const auto& link : fx.links)
  1593. {
  1594. const auto& chain = fx.branches[(std::size_t) link.branch].chain;
  1595. CHECK (link.source < (int) chain.size() && chain[(std::size_t) link.target].kind == lynx::EffectKind::parametricEq);
  1596. lovePhilterLinks[2 + (link.param == lynx::peq::param (0, 3) ? 1 : 0)]++;
  1597. }
  1598. for (const auto& link : conv->links)
  1599. {
  1600. lovePhilterLinks[link.param == lynx::peq::param (0, 3) ? 1 : 0]++;
  1601. CHECK (link.curve.size() == 33);
  1602. for (auto v : link.curve)
  1603. CHECK (std::isfinite (v) && v >= 0.0f && v <= 1.0f);
  1604. }
  1605. }
  1606. }
  1607. else if (slot.plugin == "Vocodex")
  1608. {
  1609. const auto v = lynx::vocodexEffect (slot.state);
  1610. CHECK (v.has_value());
  1611. vocodexes += v.has_value();
  1612. vocodexSides += v && (v->modIn > 0 || v->carIn > 0);
  1613. }
  1614. else if (slot.plugin == "Effector")
  1615. effectors += lynx::effectFromFl (slot.plugin, slot.state).has_value();
  1616. else if (slot.plugin == "Patcher")
  1617. {
  1618. const auto graph = lynx::parsePatcher (slot.state);
  1619. CHECK (graph.has_value());
  1620. ++patchers;
  1621. if (graph && ! graph->serialChain.empty())
  1622. ++serial;
  1623. if (graph && graph->serialChain.empty() && graph->audio)
  1624. ++parallel;
  1625. }
  1626. for (const auto& c : project.channels)
  1627. if (c.pluginName == "DirectWave")
  1628. if (const auto program = lynx::directWaveProgram (c.pluginState))
  1629. for (const auto& wav : program->embeddedWav)
  1630. if (! wav.empty())
  1631. {
  1632. ++embeddedZones;
  1633. CHECK (wav.size() > 44 && std::memcmp (wav.data(), "RIFF", 4) == 0);
  1634. }
  1635. for (const auto& c : project.channels)
  1636. if (c.pluginName == "Patcher")
  1637. {
  1638. const auto graph = lynx::parsePatcher (c.pluginState);
  1639. CHECK (graph.has_value());
  1640. generatorPatchers += graph && graph->mainGenerator >= 0;
  1641. }
  1642. }
  1643. catch (const flp::Error&) {}
  1644. }
  1645. CHECK (gates >= 30 && shaped == gates);
  1646. std::printf (" Gross Beat: %d slots, %d read exactly; Effector: %d converted; Love Philter: %d, %d read\n", gates, shaped, effectors,
  1647. lovePhilters, lovePhiltersRead);
  1648. CHECK (lovePhiltersRead == lovePhilters && lovePhiltersConverted == lovePhilters);
  1649. std::printf (" Vocodex: %d converted, %d with side inputs\n", vocodexes, vocodexSides);
  1650. CHECK (vocodexes >= 9);
  1651. std::printf (" Love Philter live links: %d cutoff, %d resonance; in multi-unit containers: %d cutoff, %d resonance\n",
  1652. lovePhilterLinks[0], lovePhilterLinks[1], lovePhilterLinks[2], lovePhilterLinks[3]);
  1653. CHECK (lovePhilterLinks[2] + lovePhilterLinks[3] > 0);
  1654. std::printf (" DirectWave: %d zones with embedded audio\n", embeddedZones);
  1655. CHECK (embeddedZones == 12);
  1656. std::printf (" Patcher: %d in the mixer (%d serial, %d reduced to serial/parallel), %d as instrument with generator\n", patchers, serial,
  1657. parallel, generatorPatchers);
  1658. CHECK (gates == 0 || shaped > 0);
  1659. }
  1660. // Envelope Controller "Basic Sidechain" (Future House): drops on the note and returns to ~1 within ~1/3 s
  1661. {
  1662. const auto path = std::filesystem::path (realFlpDir) / "Demo projects" / "Demo songs" / "Asher Postman - Future House.flp";
  1663. if (std::filesystem::exists (path))
  1664. {
  1665. std::ifstream in (path, std::ios::binary);
  1666. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  1667. const auto song = lynx::importFlp (flp::parseProject (file));
  1668. const lynx::ChannelDef* controller = nullptr;
  1669. for (const auto& c : song.channels)
  1670. if (c.instrument == lynx::InstrumentKind::controller)
  1671. controller = &c;
  1672. CHECK (controller != nullptr);
  1673. if (controller != nullptr)
  1674. {
  1675. auto inst = lynx::makeInstrument (lynx::InstrumentKind::controller);
  1676. inst->prepare (48000.0);
  1677. inst->configure (*controller, song.bpm);
  1678. std::vector<float> l (480), r (480);
  1679. inst->render (l.data(), r.data(), 480, 1.0f, 1.0f);
  1680. inst->noteOn (60, 1.0f);
  1681. inst->render (l.data(), r.data(), 480, 1.0f, 1.0f); // 10 ms
  1682. const auto ducked = inst->controlOutput (0);
  1683. for (int i = 0; i < 100; ++i) // +1 s
  1684. inst->render (l.data(), r.data(), 480, 1.0f, 1.0f);
  1685. const auto recovered = inst->controlOutput (0);
  1686. std::printf (" Envelope Controller (Basic Sidechain): %.2f on the note, %.2f after 1 s\n", ducked, recovered);
  1687. CHECK (ducked < 0.3f && recovered > 0.9f);
  1688. }
  1689. }
  1690. }
  1691. // mutation fuzz: real states with flipped and truncated bytes must not crash the readers
  1692. {
  1693. std::map<std::string, std::vector<std::uint8_t>> samples; // one real blob per plugin
  1694. for (const auto& entry : std::filesystem::recursive_directory_iterator (std::filesystem::path (realFlpDir) / "Demo projects"))
  1695. {
  1696. if (entry.path().extension() != ".flp")
  1697. continue;
  1698. std::ifstream in (entry.path(), std::ios::binary);
  1699. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  1700. try
  1701. {
  1702. const auto project = flp::parseProject (file);
  1703. for (const auto& c : project.channels)
  1704. if (! c.pluginState.empty())
  1705. samples.emplace (c.pluginName, c.pluginState);
  1706. for (const auto& insert : project.mixer)
  1707. for (const auto& slot : insert.slots)
  1708. if (! slot.state.empty())
  1709. samples.emplace (slot.plugin, slot.state);
  1710. }
  1711. catch (const flp::Error&) {}
  1712. }
  1713. std::uint32_t seed = 12345;
  1714. auto rnd = [&seed] { seed = seed * 1664525u + 1013904223u; return seed >> 8; };
  1715. int mutations = 0;
  1716. for (const auto& [name, original] : samples)
  1717. for (int round = 0; round < 40; ++round)
  1718. {
  1719. auto blob = original;
  1720. if (round % 4 == 3 && ! blob.empty())
  1721. blob.resize (rnd() % blob.size()); // truncated
  1722. else
  1723. for (int k = 0; k < 1 + (int) (rnd() % 8) && ! blob.empty(); ++k)
  1724. blob[rnd() % blob.size()] = (std::uint8_t) rnd();
  1725. ++mutations;
  1726. lynx::SynthSettings synth;
  1727. float gain = 1.0f;
  1728. lynx::synthFromSytrus (blob, 120.0, synth, gain);
  1729. lynx::synthFromHarmor (blob, synth, gain);
  1730. lynx::synthFromGms (blob, 120.0, synth, gain);
  1731. lynx::synthFromOgun (blob, synth, gain);
  1732. lynx::synthFromMorphine (blob, synth, gain);
  1733. lynx::synthFromKepler (blob, synth, gain);
  1734. lynx::synthFromPoiZone (blob, synth, gain);
  1735. lynx::synthFromToxic (blob, synth, gain);
  1736. lynx::synthFromBassDrum (blob, synth, gain);
  1737. lynx::synthFromHarmless (blob, 120.0, synth, gain);
  1738. lynx::harmlessParams (blob);
  1739. if (lynx::harmorStateUsable (blob))
  1740. lynx::harmorFallbacks (blob);
  1741. lynx::parseEnvelopeController (blob);
  1742. lynx::parseKeyboardController (blob);
  1743. lynx::parseLovePhilter (blob);
  1744. lynx::parsePatcher (blob);
  1745. lynx::effectFromFl (name, blob);
  1746. lynx::fpcPads (blob);
  1747. lynx::directWaveProgram (blob);
  1748. CHECK (std::isfinite (gain) && std::isfinite (synth.cutoff));
  1749. }
  1750. std::printf (" mutation fuzz: %d blobs from %d plugins\n", mutations, (int) samples.size());
  1751. }
  1752. // WavPack (.wv) samples from FL packs: all decode, with finite, non-silent audio
  1753. {
  1754. int files = 0, decoded = 0, audible = 0;
  1755. double seconds = 0.0;
  1756. for (const auto& entry : std::filesystem::recursive_directory_iterator (std::filesystem::path (realFlpDir) / "Patches" / "Packs"))
  1757. if (entry.path().extension() == ".wv")
  1758. {
  1759. std::ifstream in (entry.path(), std::ios::binary);
  1760. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  1761. ++files;
  1762. lynx::DecodedAudio audio;
  1763. if (! lynx::decodeWavPack (file.data(), file.size(), audio))
  1764. continue;
  1765. ++decoded;
  1766. float peak = 0.0f;
  1767. bool finite = true;
  1768. for (const auto& ch : audio.channels)
  1769. for (auto v : ch)
  1770. {
  1771. finite = finite && std::isfinite (v);
  1772. peak = std::max (peak, std::abs (v));
  1773. }
  1774. CHECK (finite && peak <= 4.0f);
  1775. audible += peak > 1e-4f;
  1776. seconds += (double) audio.channels[0].size() / audio.sampleRate;
  1777. }
  1778. std::printf (" WavPack: %d .wv files, %d decoded, %d audible, %.0f s of audio\n", files, decoded, audible, seconds);
  1779. CHECK (decoded == files);
  1780. }
  1781. // native GMS presets (.gmsynth = the contents of 0xD5 without the "SSLF")
  1782. int gmsPresets = 0, gmsDecoded = 0;
  1783. const auto gmsFolder = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "GMS";
  1784. if (std::filesystem::exists (gmsFolder))
  1785. for (const auto& entry : std::filesystem::recursive_directory_iterator (gmsFolder))
  1786. if (entry.path().extension() == ".gmsynth")
  1787. {
  1788. std::ifstream in (entry.path(), std::ios::binary);
  1789. std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  1790. std::vector<std::uint8_t> blob { 'S', 'S', 'L', 'F' };
  1791. const auto n = (std::uint32_t) file.size();
  1792. blob.insert (blob.end(), reinterpret_cast<const std::uint8_t*> (&n), reinterpret_cast<const std::uint8_t*> (&n) + 4);
  1793. blob.insert (blob.end(), file.begin(), file.end());
  1794. lynx::SynthSettings gs;
  1795. float g = 1.0f;
  1796. ++gmsPresets;
  1797. if (lynx::synthFromGms (blob, 120.0, gs, g))
  1798. {
  1799. ++gmsDecoded;
  1800. CHECK (std::isfinite (g) && gs.cutoff >= 20.0f && std::isfinite (gs.pitchEnv.amount) && std::isfinite (gs.lfo[lynx::lfoPitch].period));
  1801. }
  1802. }
  1803. std::printf (" GMS presets: %d, %d decoded\n", gmsPresets, gmsDecoded);
  1804. // the synth's "FL presets" button: Sytrus/Harmor .fst and .gmsynth become synth settings
  1805. int presetFiles = 0, presetsRead = 0;
  1806. for (const auto* name : { "Sytrus", "Harmor", "GMS", "Harmless", "Ogun", "Sakura", "Fruit Kick", "Fruity DX10", "MiniSynth", "Kepler",
  1807. "PoiZone", "Toxic Biohazard", "BassDrum" })
  1808. {
  1809. const auto folder = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / name;
  1810. if (! std::filesystem::exists (folder))
  1811. continue;
  1812. for (const auto& entry : std::filesystem::recursive_directory_iterator (folder))
  1813. if (entry.path().extension() == ".fst" || entry.path().extension() == ".gmsynth")
  1814. {
  1815. std::ifstream in (entry.path(), std::ios::binary);
  1816. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  1817. ++presetFiles;
  1818. presetsRead += lynx::synthFromFlPreset (file).has_value();
  1819. }
  1820. }
  1821. std::printf (" FL synth presets: %d files, %d read\n", presetFiles, presetsRead);
  1822. CHECK (presetsRead >= presetFiles * 95 / 100);
  1823. CHECK (gmsDecoded == gmsPresets);
  1824. CHECK (sytrusDecoded == sytrus);
  1825. CHECK (harmlessDecoded == harmless && harmless > 100);
  1826. std::printf (" Harmless: %d states parsed for the engine, %d channels imported as Harmless\n", harmlessParsed, harmlessKind);
  1827. CHECK (harmlessParsed == harmless && harmlessKind >= harmless);
  1828. std::printf (" Harmor: %d channels, %d states usable by the engine, %d imported as Harmor (%d sounding)\n", harmor, harmorUsable, harmorKind, harmorSounding);
  1829. CHECK (harmorUsable == harmor && harmor > 90 && harmorKind >= harmor && harmorSounding >= harmorKind * 9 / 10);
  1830. 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",
  1831. gms, gmsUsable, gmsKind, gmsSounding, morphine, morphineUsable, morphineKind, morphineSounding, demoGoldens);
  1832. CHECK (gmsUsable == gms && gms >= 9 && gmsKind == gms && gmsSounding == gmsKind);
  1833. std::printf (" MiniSynth channels: %d, %d usable, %d on the exact engine, %d sounding, %d golden\n", miniSynth, miniSynthUsable, miniSynthKind,
  1834. miniSynthSounding, miniSynthGoldens);
  1835. CHECK (miniSynthUsable == miniSynth && miniSynth >= 12 && miniSynthKind == miniSynth && miniSynthSounding == miniSynthKind && miniSynthGoldens == 2);
  1836. CHECK (morphineUsable == morphine && morphine >= 7 && morphineKind == morphine && morphineSounding == morphineKind && demoGoldens == 2);
  1837. std::printf (" PoiZone: %d channels, %d usable, %d imported as PoiZone (%d sounding), %d demo golden\n", poizone, poizoneUsable, poizoneKind,
  1838. poizoneSounding, poizoneGoldens);
  1839. CHECK (poizoneUsable == poizone && poizone >= 2 && poizoneKind == poizone && poizoneSounding == poizoneKind && poizoneGoldens == 1);
  1840. std::printf (" Toxic Biohazard: %d channels, %d usable, %d imported as Toxic (%d sounding), %d golden\n", toxic, toxicUsable, toxicKind,
  1841. toxicSounding, toxicGoldens);
  1842. CHECK (toxicUsable == toxic && toxic >= 3 && toxicKind == toxic && toxicSounding == toxicKind && toxicGoldens == 1);
  1843. std::printf (" Ogun: %d channels, %d states usable by the engine, %d imported as Ogun (%d sounding)\n", ogun, ogunUsable, ogunKind, ogunSounding);
  1844. CHECK (ogunUsable == ogun && ogun >= 20 && ogunKind >= ogun && ogunSounding >= ogunKind * 9 / 10);
  1845. std::printf (" Autogun: %d channels, %d states usable by the engine, %d imported as Autogun (%d with SG.dat, %d sounding)\n", autogun,
  1846. autogunUsable, autogunKind, autogunWithSg, autogunSounding);
  1847. CHECK (autogunUsable == autogun && autogun >= 9 && autogunKind == autogun && autogunWithSg == autogun && autogunSounding == autogun);
  1848. std::printf (" Fruity DX10: %d channels, %d states usable by the engine, %d imported as DX10 (%d sounding)\n", dx10, dx10Usable, dx10Kind, dx10Sounding);
  1849. CHECK (dx10Usable == dx10 && dx10 >= 12 && dx10Kind == dx10 && dx10Sounding == dx10Kind);
  1850. std::printf (" Kepler: %d channels, %d states usable by the engine, %d imported as Kepler (%d sounding)\n", kepler, keplerUsable, keplerKind, keplerSounding);
  1851. CHECK (keplerUsable == kepler && kepler >= 4 && keplerSounding == keplerKind && (keplerKind == 0 || keplerKind == kepler));
  1852. std::printf (" Harmless rack compressor: %d of %d presets read from the installation\n", harmlessCompLoaded, harmlessCompWanted);
  1853. CHECK (harmlessCompLoaded == harmlessCompWanted && harmlessCompWanted > 100);
  1854. std::printf (" Maximus: %d of %d slots on the exact engine; Soundgoodizer: %d of %d; Vocodex with SG.dat: %d of %d\n", maximusExact,
  1855. maximusSlots, sgzExact, sgzSlots, vocodexWithData, vocodexSlots);
  1856. // REA_rodada40 §6: in Demo projects the 4 states left on the approximation use the Frequency Splitter linear-phase
  1857. // option; the other Data folders add states with options not decoded
  1858. CHECK (maximusSlots - maximusExact <= 16 && maximusExact >= 97 && sgzExact == sgzSlots && sgzSlots > 90);
  1859. CHECK (vocodexWithData == vocodexSlots && vocodexSlots >= 10);
  1860. std::printf (" Fruity WaveShaper: %d of %d slots on the exact engine; Fruity Soft Clipper: %d of %d\n", shaperExact, shaperSlots,
  1861. clipperExact, clipperSlots);
  1862. // (Patcher graphs add WaveShaper nodes of their own to the imported mixer)
  1863. CHECK (shaperExact >= shaperSlots && shaperSlots >= 70 && clipperExact >= clipperSlots && clipperSlots >= 25); // REA_rodada51
  1864. std::printf (" Fruity Parametric EQ 2 / Parametric EQ: %d of %d slots on the exact engine\n", flPeqExact, flPeqSlots);
  1865. CHECK (flPeqExact >= flPeqSlots && flPeqSlots >= 1100); // REA_rodada50
  1866. std::printf (" Fruity PanOMatic / Balance / Blood Overdrive: %d of %d slots on the exact engines\n", basicExact, basicSlots);
  1867. // one PanOMatic of "Bombs Away" (insert 69, last slot) falls off LYNX's 10-slot limit after a serial Patcher expands
  1868. CHECK (basicExact + 1 >= basicSlots && basicSlots >= 270); // REA_rodada58
  1869. // (Patcher graphs can add Distructor nodes of their own to the imported mixer)
  1870. std::printf (" Distructor: %d of %d slots on the exact engine (%d cabinet impulses read from the installation)\n",
  1871. distructorExact, distructorSlots, distructorImpulses);
  1872. CHECK (distructorExact >= distructorSlots && distructorSlots >= 4 && distructorImpulses >= 1); // REA_rodada71
  1873. std::printf (" EQUO: %d of %d slots on the exact engine\n", equoExact, equoSlots);
  1874. CHECK (equoExact == equoSlots && equoSlots >= 10); // REA_rodada61
  1875. std::printf (" Pitch Shifter: %d of %d slots on the exact engine\n", pitchShifterExact, pitchShifterSlots);
  1876. CHECK (pitchShifterExact == pitchShifterSlots && pitchShifterSlots >= 15); // REA_rodada61
  1877. std::printf (" Fruity Fast LP / Free Filter / Filter: %d of %d slots on the exact engines\n", filterExact, filterSlots);
  1878. CHECK (filterExact >= filterSlots && filterSlots >= 53); // REA_rodada63
  1879. std::printf (" Fruity Delay 2: %d of %d slots on the exact engine; Fruity Delay 3: %d of %d\n", delay2Exact, delay2Slots,
  1880. delay3Exact, delay3Slots);
  1881. CHECK (delay2Exact == delay2Slots && delay2Slots >= 60 && delay3Exact == delay3Slots && delay3Slots >= 100); // REA_rodada52
  1882. std::printf (" Chorus family: %d of %d slots on the exact engines\n", chorusExact, chorusSlots);
  1883. CHECK (chorusExact >= chorusSlots && chorusSlots >= 47); // REA_rodada59
  1884. 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,
  1885. fastDistSlots, flangusExact, flangusSlots, flangerExact, flangerSlots);
  1886. CHECK (fastDistExact >= fastDistSlots && fastDistSlots >= 30 && flangusExact >= flangusSlots && flangusSlots >= 30
  1887. && flangerExact >= flangerSlots && flangerSlots >= 3); // REA_rodada55
  1888. // 3x Osc channel presets shipped with FL (.fst): all become synth settings
  1889. int presets = 0, presetsDecoded = 0;
  1890. const auto presetDir = std::filesystem::path (realFlpDir) / "Patches" / "Channel presets" / "3x Osc";
  1891. if (std::filesystem::is_directory (presetDir))
  1892. for (auto& entry : std::filesystem::directory_iterator (presetDir))
  1893. if (entry.path().extension() == ".fst")
  1894. {
  1895. ++presets;
  1896. std::ifstream in (entry.path(), std::ios::binary);
  1897. std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  1898. if (auto synth = lynx::synthFromFlPreset (bytes))
  1899. {
  1900. ++presetsDecoded;
  1901. CHECK (synth->osc[0].level + synth->osc[1].level + (synth->am ? 0.0f : synth->osc[2].level) <= 1.001f);
  1902. }
  1903. }
  1904. std::printf (" %d 3x Osc presets, %d read\n", presets, presetsDecoded);
  1905. CHECK (presetsDecoded == presets);
  1906. // FL effect presets that become built-in effects
  1907. int fxPresets = 0, fxDecoded = 0;
  1908. for (auto kind : { lynx::EffectKind::parametricEq, lynx::EffectKind::limiter, lynx::EffectKind::reverb, lynx::EffectKind::delay,
  1909. lynx::EffectKind::utility })
  1910. for (const auto& plugin : lynx::flPluginsForEffect (kind))
  1911. {
  1912. const auto dir = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Effects" / plugin;
  1913. if (! std::filesystem::is_directory (dir))
  1914. continue;
  1915. for (auto& entry : std::filesystem::recursive_directory_iterator (dir))
  1916. if (entry.path().extension() == ".fst")
  1917. {
  1918. ++fxPresets;
  1919. std::ifstream in (entry.path(), std::ios::binary);
  1920. std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  1921. fxDecoded += lynx::effectFromFlPreset (bytes).has_value();
  1922. }
  1923. }
  1924. std::printf (" %d FL effect presets, %d read\n", fxPresets, fxDecoded);
  1925. CHECK (fxDecoded == fxPresets);
  1926. CHECK (wrappedParsed == wrapped);
  1927. std::printf (" %d plugins in Fruity Wrapper, %d read\n", wrapped, wrappedParsed);
  1928. std::printf (" %d markers, %d time signature changes, %d tracks with EQ\n", markers, signatures, trackEqs);
  1929. std::printf (" %d parametric EQs (%d with FL settings), %d reverbs (%d without dry)\n", peqSlots, peqShaped, reverbs, reverbsDryless);
  1930. std::printf (" DirectWave: %d channels, %d zones; FPC: %d projects, %d pad channels, %d pad notes\n", dwChannels, dwZones, fpcKits,
  1931. padChannels, padNotes);
  1932. CHECK (dwZones > 1000 && padChannels > 250 && padNotes > 500);
  1933. std::printf (" DirectWave on the exact engine (REA_rodada72): %d channels\n", dwExact);
  1934. CHECK (dwExact >= 57);
  1935. std::printf (" sliced channels (Slicer/Slicex): %d; embedded WAVs saved: %d\n", slicedChannels, embeddedSaved);
  1936. CHECK (slicedChannels >= 25 && embeddedSaved >= 10); // without decoding Ogg here, some loops have no duration
  1937. std::printf (" FL Keys (Grand Piano, 30 zones): %d channels\n", flKeysChannels);
  1938. CHECK (flKeysChannels >= 8);
  1939. CHECK (peqSlots > 1000 && peqShaped > 900 && reverbsDryless > 20 && reverbsLowCut > 250);
  1940. CHECK (markers > 300 && signatures > 60);
  1941. std::printf (" %d 3x Osc channels, %d decoded, %d on the exact engine (%d sounding)\n", threeOsc, threeOscDecoded, threeOscKind, threeOscSounding);
  1942. // one demo channel (a noise layer of "Right Night feeling") has the channel filter closed (fast LP, Mod X 0): silent
  1943. // with its saved settings, in FL too
  1944. CHECK (threeOscKind == threeOsc && threeOscSounding == threeOscKind - 1);
  1945. std::printf (" %d .flp files read, %d with notes, %d with playlist, %d automations (%d with target), %d sends\n",
  1946. files, withNotes, withPlaylist, automations, automationsWithTarget, sends);
  1947. CHECK (files > 0);
  1948. CHECK (automations > 0 && sends > 0);
  1949. }
  1950. // ---------------------------------------------------------------------------
  1951. // Sequencer
  1952. TEST (pattern_toggle_step_and_length)
  1953. {
  1954. lynx::Pattern p;
  1955. CHECK (! p.hasStep (0, 4));
  1956. p.toggleStep (0, 4);
  1957. CHECK (p.hasStep (0, 4));
  1958. CHECK (p.notes.size() == 1 && p.notes[0].start == 4 * lynx::ticksPerStep);
  1959. CHECK (p.effectiveLength (4) == 4 * lynx::ppq); // one bar
  1960. p.toggleStep (1, 20); // second bar
  1961. CHECK (p.effectiveLength (4) == 8 * lynx::ppq);
  1962. p.toggleStep (0, 4);
  1963. CHECK (! p.hasStep (0, 4) && p.notes.size() == 1);
  1964. }
  1965. TEST (scheduler_places_events_at_exact_samples)
  1966. {
  1967. lynx::Pattern p;
  1968. p.toggleStep (0, 0);
  1969. p.toggleStep (0, 4); // tick 960 = 1 beat
  1970. const double sr = 48000.0, bpm = 120.0; // 1 beat = 0.5 s = 24000 samples
  1971. const auto tps = lynx::ticksPerSample (bpm, sr);
  1972. lynx::EventBuffer ev;
  1973. lynx::schedulePatternLoop (p, p.effectiveLength (4), 23900.0 * tps, 512, tps, ev);
  1974. // the beat-2 note starts at sample 24000 -> offset 100 in this block
  1975. int ons = 0;
  1976. for (auto& e : ev)
  1977. if (e.type == lynx::NoteEvent::Type::on)
  1978. {
  1979. ++ons;
  1980. CHECK (e.sampleOffset == 100);
  1981. }
  1982. CHECK (ons == 1);
  1983. }
  1984. TEST (scheduler_wraps_the_loop)
  1985. {
  1986. lynx::Pattern p;
  1987. p.toggleStep (2, 0);
  1988. const auto len = p.effectiveLength (4); // 3840 ticks
  1989. const double sr = 48000.0;
  1990. const auto tps = lynx::ticksPerSample (120.0, sr);
  1991. lynx::EventBuffer ev;
  1992. // block that crosses the loop end: starts 10 samples before
  1993. const auto start = (double) len - 10.0 * tps;
  1994. lynx::schedulePatternLoop (p, len, start, 64, tps, ev);
  1995. bool found = false;
  1996. for (auto& e : ev)
  1997. if (e.type == lynx::NoteEvent::Type::on && e.channel == 2)
  1998. {
  1999. found = true;
  2000. CHECK (e.sampleOffset == 10);
  2001. }
  2002. CHECK (found);
  2003. }
  2004. TEST (scheduler_off_before_on_at_same_sample)
  2005. {
  2006. lynx::Pattern p;
  2007. lynx::Note a;
  2008. a.start = 0; a.length = 240; a.channel = 0;
  2009. lynx::Note b = a;
  2010. b.start = 240;
  2011. p.notes = { a, b };
  2012. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  2013. lynx::EventBuffer ev;
  2014. lynx::schedulePatternLoop (p, p.effectiveLength (4), 200.0, 4000, tps, ev);
  2015. CHECK (ev.size() >= 2);
  2016. CHECK (ev[0].type == lynx::NoteEvent::Type::off);
  2017. CHECK (ev[1].type == lynx::NoteEvent::Type::on);
  2018. CHECK (ev[0].sampleOffset == ev[1].sampleOffset);
  2019. }
  2020. TEST (snapshot_exchange_hands_over_and_recycles)
  2021. {
  2022. struct Counted
  2023. {
  2024. int value;
  2025. int* alive;
  2026. Counted (int v, int* a) : value (v), alive (a) { ++*alive; }
  2027. ~Counted() { --*alive; }
  2028. };
  2029. int alive = 0;
  2030. {
  2031. lynx::SnapshotExchange<Counted> x;
  2032. CHECK (x.acquire() == nullptr);
  2033. x.publish (std::make_unique<Counted> (1, &alive));
  2034. CHECK (x.acquire()->value == 1);
  2035. x.publish (std::make_unique<Counted> (2, &alive));
  2036. x.publish (std::make_unique<Counted> (3, &alive)); // 2 was never read: freed immediately
  2037. CHECK (alive == 2);
  2038. CHECK (x.acquire()->value == 3);
  2039. CHECK (alive == 2); // 1 was returned, but only the UI frees it
  2040. x.collectGarbage();
  2041. CHECK (alive == 1);
  2042. // without collection, the return slots fill up and the audio side holds on to the current one
  2043. for (int i = 0; i < 20; ++i)
  2044. {
  2045. x.publish (std::make_unique<Counted> (10 + i, &alive));
  2046. CHECK (x.acquire() != nullptr);
  2047. }
  2048. }
  2049. CHECK (alive == 0);
  2050. }
  2051. TEST (rack_renderer_makes_sound_and_reuses_instruments)
  2052. {
  2053. auto song = lynx::Song::makeDefault();
  2054. song.bpm = 120.0;
  2055. const double sr = 48000.0;
  2056. auto state = lynx::RackState::build (song, nullptr, sr);
  2057. lynx::RackRenderer r;
  2058. r.prepare (sr);
  2059. std::vector<float> l (512), rr (512);
  2060. double tick = 0.0;
  2061. float peak = 0.0f;
  2062. int kicks = 0;
  2063. for (int block = 0; block < 93; ++block) // 47616 samples: just before 1 s
  2064. {
  2065. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  2066. for (auto& e : r.lastEvents())
  2067. if (e.type == lynx::NoteEvent::Type::on && e.channel == 0)
  2068. ++kicks;
  2069. for (int i = 0; i < 512; ++i)
  2070. {
  2071. CHECK (std::isfinite (l[(std::size_t) i]));
  2072. peak = std::max (peak, std::abs (l[(std::size_t) i]));
  2073. }
  2074. tick += 512 * lynx::ticksPerSample (song.bpm, sr);
  2075. }
  2076. CHECK (peak > 0.1f && peak < 4.0f);
  2077. CHECK (kicks == 2); // 120 BPM: beats at 0 and 0.5 s
  2078. // editing the song keeps the instruments of channels whose type didn't change
  2079. song.channels[4].instrument = lynx::InstrumentKind::kick;
  2080. auto next = lynx::RackState::build (song, state.get(), sr);
  2081. CHECK (next->instruments[0] == state->instruments[0]);
  2082. CHECK (next->instruments[4] != state->instruments[4]);
  2083. // stopping silences the synth and triggers no new notes
  2084. r.process (next.get(), false, tick, 512, l.data(), rr.data());
  2085. CHECK (r.lastEvents().size() == 0);
  2086. }
  2087. TEST (flp_import_maps_channels_and_rescales)
  2088. {
  2089. flp::Project p;
  2090. p.header.ppq = 96;
  2091. p.tempo = 150.0;
  2092. p.channels = { { 0, flp::ChannelType::sampler, "Kick 808", "", "", 1 },
  2093. { 1, flp::ChannelType::audioClip, "Vocal", "", "", 2 },
  2094. { 2, flp::ChannelType::instrument, "Lead", "FLEX", "", 3 } };
  2095. flp::Pattern pat;
  2096. pat.index = 1;
  2097. flp::Note n;
  2098. n.position = 96;
  2099. n.length = 0;
  2100. n.channel = 2;
  2101. n.key = 72;
  2102. n.velocity = 64;
  2103. pat.notes.push_back (n);
  2104. p.patterns.push_back (pat);
  2105. const auto s = lynx::importFlp (p);
  2106. CHECK (s.bpm == 150.0);
  2107. CHECK (s.channels.size() == 3); // audio clip channels stay in the rack, as in FL
  2108. CHECK (s.channels[0].instrument == lynx::InstrumentKind::kick);
  2109. CHECK (s.channels[1].instrument == lynx::InstrumentKind::audioClip);
  2110. CHECK (s.channels[2].instrument == lynx::InstrumentKind::synth);
  2111. CHECK (s.patterns.size() == 1);
  2112. CHECK (s.patterns[0].notes[0].start == lynx::ppq); // 96 @ PPQ 96 = 1 beat
  2113. CHECK (s.patterns[0].notes[0].channel == 2);
  2114. CHECK (s.patterns[0].notes[0].length == lynx::ticksPerStep); // step note
  2115. CHECK (std::abs (s.patterns[0].notes[0].velocity - 0.5f) < 1e-6f);
  2116. }
  2117. TEST (live_queue_is_fifo_and_bounded)
  2118. {
  2119. lynx::LiveEventQueue q;
  2120. lynx::NoteEvent e;
  2121. CHECK (! q.pop (e));
  2122. for (int i = 0; i < 10; ++i)
  2123. {
  2124. lynx::NoteEvent in;
  2125. in.key = i;
  2126. CHECK (q.push (in));
  2127. }
  2128. for (int i = 0; i < 10; ++i)
  2129. CHECK (q.pop (e) && e.key == i);
  2130. int pushed = 0;
  2131. while (q.push (e))
  2132. ++pushed;
  2133. CHECK (pushed == (int) lynx::LiveEventQueue::capacity - 1); // one slot stays free to tell full from empty
  2134. }
  2135. // FL's sampler interpolators (REA_rodada69 §1): outputs of FLEngine's own kernels (EXEC, r69/py/gold.py) on a synthetic
  2136. // 16-bit stereo signal; the port must give the same bits (linear, 6-point, 24-point sinc, 32-point sinc at speed 1.37,
  2137. // 16-point sinc widened to 24 points with the cutoff at π/9.5 at speed 9.5)
  2138. TEST (fl_sampler_interpolators_match_fl)
  2139. {
  2140. std::vector<float> l, r;
  2141. std::uint32_t x = 12345;
  2142. for (int k = 0; k < 2 * 144; ++k)
  2143. {
  2144. x = x * 1103515245u + 12345u;
  2145. const auto v = (float) (std::int16_t) (std::uint16_t) (x >> 16) / 32768.0f;
  2146. (k % 2 == 0 ? l : r).push_back (v);
  2147. }
  2148. const int pad = 40;
  2149. 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 } };
  2150. struct Case { int quality; double speed; std::uint32_t bits[16]; };
  2151. const Case cases[] = {
  2152. { 1, 1.0, { 0x3ec1a000, 0x3d774000, 0x3ca1e000, 0x3ec39900, 0xbf2d4600, 0xbe096800, 0xbea0cd17, 0x3f433097, 0x3f2796da, 0xbd9fbd17, 0x3f27a3ff, 0xbda03ff2, 0x3dace746, 0x3eba19c4, 0x3e477a00, 0x3f0f2a80 } },
  2153. { 2, 1.0, { 0x3ec1a000, 0x3d773fff, 0x3d34885d, 0x3eedeaa0, 0xbf49e298, 0xbea52fc4, 0xbeba55fc, 0x3f7c58da, 0x3f27c5c2, 0xbda139af, 0x3f27a3ff, 0xbda03ffa, 0x3dd8e7b1, 0x3ee872c1, 0x3e05b2d5, 0x3f144c4f } },
  2154. { 4, 1.0, { 0x3ec19fff, 0x3d773fff, 0x3d889c32, 0x3ef6b184, 0xbf57cf0d, 0xbf0abecd, 0xbea14a13, 0x3f94f11b, 0x3f27edc4, 0xbda10d8c, 0x3f27a3fd, 0xbda03ffa, 0x3e5baf41, 0x3edb298c, 0x3dd76cbf, 0x3ef9d212 } },
  2155. { 5, 1.37, { 0x3f19d373, 0x3ea9b6a3, 0xbdcbe56e, 0x3e2af6c6, 0xbf476f7a, 0xbe566a13, 0xbe9f166a, 0x3f52b5ad, 0x3f27ad16, 0xbce48792, 0x3f27960a, 0xbce01ebe, 0x3e1b96be, 0x3e163fd8, 0x3f042e42, 0x3ec4be83 } },
  2156. { 3, 9.5, { 0xbd97308e, 0x3dd8783c, 0xbd911374, 0x3e162a41, 0xbd8a959e, 0x3e38b220, 0xbd7e6785, 0x3e58895c, 0xbd380c4f, 0x3e7e3008, 0xbd380e4e, 0x3e7e2696, 0x3d00c6d0, 0xbd627d87, 0x3e308cb8, 0xbcece918 } },
  2157. };
  2158. const lynx::flvp::X87Extended precision;
  2159. for (const auto& c : cases)
  2160. {
  2161. lynx::flrs::Kernel k;
  2162. k.setQuality (c.quality);
  2163. k.setSpeed (c.speed);
  2164. int same = 0;
  2165. for (int p = 0; p < 8; ++p)
  2166. {
  2167. const auto lo = pad + positions[p].first - k.halfWidth + 1;
  2168. float a, b;
  2169. lynx::flrs::interpolate (k, l.data() + lo, r.data() + lo, positions[p].second, a, b);
  2170. std::uint32_t ba, bb;
  2171. std::memcpy (&ba, &a, 4);
  2172. std::memcpy (&bb, &b, 4);
  2173. same += (ba == c.bits[2 * p]) + (bb == c.bits[2 * p + 1]);
  2174. }
  2175. CHECK (same == 16);
  2176. }
  2177. CHECK (lynx::flrs::halfWidthFor (4) == 12 && lynx::flrs::halfWidthFor (5) == 16 && lynx::flrs::halfWidthFor (9) == 256);
  2178. CHECK (lynx::flrs::speedFor (0.0f, 1.0) == 1.0 && std::abs (lynx::flrs::speedFor (1200.0f, 0.5) - 1.0) < 1e-12);
  2179. }
  2180. // The sampler at the original rate copies the sample (FL's copy path), and plays until the read position passes the
  2181. // last frame
  2182. TEST (fl_sampler_copies_at_original_rate)
  2183. {
  2184. auto data = std::make_shared<lynx::SampleData>();
  2185. data->sampleRate = 48000.0;
  2186. for (int i = 0; i < 300; ++i)
  2187. data->left.push_back (std::sin (0.05f * (float) i) * 0.5f);
  2188. auto sampler = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  2189. sampler->prepare (48000.0);
  2190. sampler->noteOn (60, 1.0f);
  2191. std::vector<float> l (400), r (400);
  2192. sampler->render (l.data(), r.data(), 400, 1.0f, 1.0f);
  2193. const auto panL = std::cos ((0.0f + 1.0f) * 0.25f * 3.14159265f) * 1.41421356f; // the sampler's centre pan gain
  2194. int exact = 0;
  2195. for (int i = 0; i < 300; ++i)
  2196. exact += l[(std::size_t) i] == data->left[(std::size_t) i] * panL ? 1 : 0;
  2197. CHECK (exact == 300);
  2198. CHECK (l[300] == 0.0f);
  2199. }
  2200. // FL's volume envelope on the sampler (REA_rodada69 §3): the articulator is stepped once per sequencer tick and the level
  2201. // ramps linearly to each tick's value; 96 PPQ at 120 BPM and 48 kHz = 250 samples per tick, attack 20000 = 89 ticks
  2202. TEST (fl_sampler_volume_envelope_steps_per_tick)
  2203. {
  2204. auto data = std::make_shared<lynx::SampleData>();
  2205. data->sampleRate = 48000.0;
  2206. data->left.assign (96000, 0.5f);
  2207. auto sampler = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  2208. sampler->prepare (48000.0);
  2209. lynx::ChannelDef c;
  2210. c.instrument = lynx::InstrumentKind::sampler;
  2211. c.sampler.flVolumeEnvelope = { 0, 1, 100, 20000, 0, 30000, 128, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, 0, 96 };
  2212. sampler->configure (c, 120.0);
  2213. sampler->noteOn (60, 1.0f);
  2214. std::vector<float> l (40000), r (40000);
  2215. sampler->render (l.data(), r.data(), 40000, 1.0f, 1.0f);
  2216. const auto panL = std::cos ((0.0f + 1.0f) * 0.25f * 3.14159265f) * 1.41421356f;
  2217. auto level = [&] (int n) { return l[(std::size_t) n] / (0.5f * panL); };
  2218. CHECK (std::abs (level (1000) - 1000.0f / 22500.0f) < 2e-5f);
  2219. CHECK (std::abs (level (10125) - 10125.0f / 22500.0f) < 2e-5f);
  2220. CHECK (std::abs (level (39000) - 1.0f) < 1e-6f); // the attack ended after 90 ticks; sustain 128 = 1.0
  2221. // release: 20000 = 89 ticks, linear (tension 0), from the current level
  2222. sampler->noteOff (60);
  2223. std::fill (l.begin(), l.end(), 0.0f);
  2224. sampler->render (l.data(), r.data(), 40000, 1.0f, 1.0f);
  2225. CHECK (std::abs (level (2500) - (1.0f - 10.0f / 90.0f)) < 1e-3f);
  2226. CHECK (l[39999] == 0.0f && sampler->idle());
  2227. }
  2228. // FPC's "cycle" layers (REA_rodada69 §4): among the layers covering the velocity, in order, the one after the last
  2229. // chosen, wrapping; FL's velocity index is round(v·128). Slicex's master volume uses FL's L11 law (§5).
  2230. TEST (fl_fpc_cycle_layers_and_slicex_master_volume)
  2231. {
  2232. auto level = [] (float v) {
  2233. auto d = std::make_shared<lynx::SampleData>();
  2234. d->sampleRate = 48000.0;
  2235. d->left.assign (4800, v);
  2236. return d;
  2237. };
  2238. // A covers 0…63, B 32…95, C 64…127 (all keys)
  2239. auto inst = lynx::makeZonedSampler ({ { { "a.wav", 60, 0, 127, 0, 63 }, level (0.1f) },
  2240. { { "b.wav", 60, 0, 127, 32, 95 }, level (0.2f) },
  2241. { { "c.wav", 60, 0, 127, 64, 127 }, level (0.3f) } });
  2242. inst->prepare (48000.0);
  2243. inst->configure (lynx::ChannelDef {}, 120.0);
  2244. auto play = [&] (float velocity) {
  2245. inst->allNotesOff();
  2246. std::vector<float> l (2000), r (2000);
  2247. inst->render (l.data(), r.data(), 2000, 1.0f, 1.0f);
  2248. std::fill (l.begin(), l.end(), 0.0f);
  2249. inst->noteOn (60, velocity);
  2250. inst->render (l.data(), r.data(), 10, 1.0f, 1.0f);
  2251. return l[5] / velocity;
  2252. };
  2253. auto which = [] (float x) { return (int) std::lround (x / 0.1f / std::cos (0.25f * 3.14159265f) / 1.41421356f); };
  2254. CHECK (which (play (64.0f / 128.0f)) == 2); // candidates B, C: first B
  2255. CHECK (which (play (64.0f / 128.0f)) == 3); // then C
  2256. CHECK (which (play (40.0f / 128.0f)) == 1); // candidates A, B: C is not among them → the first, A
  2257. CHECK (which (play (40.0f / 128.0f)) == 2); // then B
  2258. CHECK (which (play (64.0f / 128.0f)) == 3); // B was last → C
  2259. // Slicex state: version, 8 master values (volume 16000), options and UI state, deck A referencing a file
  2260. std::vector<std::uint8_t> state (4 + 32 + 47 + 12, 0);
  2261. const std::int32_t version = 0x00060003, volume = 16000;
  2262. std::memcpy (state.data(), &version, 4);
  2263. std::memcpy (state.data() + 4 + 8, &volume, 4);
  2264. state.push_back (1);
  2265. state.push_back (5);
  2266. for (char ch : std::string ("x.wav"))
  2267. state.push_back ((std::uint8_t) ch);
  2268. const auto sliced = lynx::slicex (state);
  2269. CHECK (sliced.has_value() && sliced->path == "x.wav");
  2270. CHECK (sliced && std::abs (sliced->gain - (float) ((std::pow (11.0, 1.25) - 1.0) / 10.0)) < 1e-5f);
  2271. }
  2272. TEST (fl_keys_state_wti_and_loader)
  2273. {
  2274. using namespace lynx::flkeys;
  2275. // state: u32 1, the wavetable name, 15 int32 knobs
  2276. State st;
  2277. st.wavetable = "Grand Piano";
  2278. for (int i = 0; i < 15; ++i)
  2279. st.knobs[(std::size_t) i] = 10 * i - 50;
  2280. const auto bytes = writeState (st);
  2281. CHECK (bytes.size() == 4 + 1 + 11 + 60);
  2282. const auto back = parseState (bytes);
  2283. CHECK (back && back->wavetable == "Grand Piano" && back->knobs == st.knobs);
  2284. // one WAV per keygroup: the file's own loop gives the default end and loop; a keygroup without a file reuses
  2285. // the previous one; a repeated file name copies the earlier keygroup; each file is followed by a guard frame
  2286. // that repeats the frame `loop` before the end (REA_rodada69 §8.2)
  2287. std::vector<std::string> names;
  2288. auto riff = parseWti ("[info]\nsamples=3\nfiletype=riff\npath=W\nvelocityfactor=2\nsamplerate=22050\n"
  2289. "[sample0]\nname=a.wav\nroot=40\nhigh=50\n[sample1]\nroot=51\nhigh=999\npos=2\nend=5\nloop=3\n"
  2290. "[sample2]\nname=A.WAV\nroot=60\nhigh=70\n", names);
  2291. CHECK (riff && names.size() == 1 && names[0] == "W\\a.wav");
  2292. CHECK (riff && riff->keygroups.size() == 3 && riff->keygroups[2].duplicateOf == 0 && riff->thresholds == std::vector<int> { 0 });
  2293. if (riff)
  2294. {
  2295. riff->files[0].loopStart = 4, riff->files[0].loopEnd = 8;
  2296. FileSamples a;
  2297. for (int i = 0; i < 10; ++i)
  2298. a.samples.push_back ((std::int16_t) (100 * (i + 1)));
  2299. const auto decoded = FlKeysTable::decode (riff->encode());
  2300. CHECK (decoded.has_value());
  2301. const auto w = buildWavetable (*decoded, { a });
  2302. CHECK (w != nullptr);
  2303. if (w != nullptr)
  2304. {
  2305. 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; };
  2306. CHECK (same (w->keygroups[0], { 40, 50, 0, 8, 5 }));
  2307. CHECK (same (w->keygroups[1], { 51, 999, 2, 5, 3 }));
  2308. CHECK (same (w->keygroups[2], { 60, 70, 0, 8, 5 }));
  2309. CHECK (w->data[9] == 400 && w->data[10] == 0 && w->data[8] == 900);
  2310. CHECK (w->sampleRate == 22050.0f && w->velocityFactor == 2.0f && ! w->stereo);
  2311. }
  2312. }
  2313. // one raw file: every keygroup's part is copied one after another with a guard sample; "canloop=false" makes the
  2314. // loop as long as the keygroup's end, so the voice stops there
  2315. auto raw = parseWti ("[info]\nsamples=2\nsamplefile=x.dat\nfiletype=raw\n[sample0]\nroot=36\nhigh=47\npos=0\nend=3\nloop=2\n"
  2316. "[sample1]\nroot=48\nhigh=999\npos=4\nend=7\nloop=4\ncanloop=false\n", names);
  2317. CHECK (raw && raw->raw && names.size() == 1 && names[0] == "x.dat");
  2318. if (raw)
  2319. {
  2320. FileSamples x;
  2321. for (int i = 0; i < 10; ++i)
  2322. x.samples.push_back ((std::int16_t) (10 * (i + 1)));
  2323. const auto w = buildWavetable (*raw, { x });
  2324. CHECK (w != nullptr);
  2325. if (w != nullptr)
  2326. {
  2327. CHECK (w->keygroups[0].pos == 0 && w->keygroups[0].end == 3 && w->keygroups[0].loop == 2);
  2328. CHECK (w->keygroups[1].pos == 5 && w->keygroups[1].end == 8 && w->keygroups[1].loop == 8);
  2329. const std::vector<std::int16_t> expected { 10, 20, 30, 40, 30, 50, 60, 70, 80, 50, 0, 0 };
  2330. CHECK (std::equal (expected.begin(), expected.end(), w->data.begin()));
  2331. // a held note on the non-looping keygroup ends by itself; 33 notes leave 32 sounding (FL's MaxPoly)
  2332. State s2;
  2333. s2.knobs = { -1000, 0, 0, -1000, 500, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  2334. RefNote n;
  2335. n.key = 60;
  2336. const auto out = renderReference (*w, s2, { n }, 44100.0, 4096, 512, true);
  2337. double early = 0, late = 0;
  2338. for (int i = 0; i < 64; ++i)
  2339. early += std::fabs (out[(std::size_t) (2 * i)]);
  2340. for (int i = 2048; i < 4096; ++i)
  2341. late += std::fabs (out[(std::size_t) (2 * i)]);
  2342. CHECK (early > 0.0 && late == 0.0);
  2343. }
  2344. }
  2345. // instrument front-end without a wavetable: silent and idle
  2346. auto inst = makeFlKeysInstrument (nullptr);
  2347. inst->prepare (44100.0);
  2348. inst->noteOn (60, 0.78f);
  2349. std::vector<float> l (256), r (256);
  2350. inst->render (l.data(), r.data(), 256, 1.0f, 1.0f);
  2351. CHECK (inst->idle() && l[100] == 0.0f);
  2352. }
  2353. TEST (directwave_crt_math_and_state)
  2354. {
  2355. using namespace lynx::dw;
  2356. auto f = [] (std::uint32_t b) { float x; std::memcpy (&x, &b, 4); return x; };
  2357. auto u = [] (float x) { std::uint32_t b; std::memcpy (&b, &x, 4); return b; };
  2358. // the plugin's own run-time (r72/run/mt.exe on DirectWave_x64.dll): values where the libm result differs
  2359. CHECK (u (sinfCrt (f (0x40b1a97fu))) == 0xbf2af4bbu);
  2360. CHECK (u (sinfCrt (f (0xc0ad8469u))) == 0x3f4222f6u);
  2361. CHECK (u (sinfCrt (f (0xc016e8c7u))) == 0xbf34b32du);
  2362. CHECK (u (cosfCrt (f (0xc0af1cc0u))) == 0x3f305702u);
  2363. CHECK (u (cosfCrt (f (0x3f15cba3u))) == 0x3f556924u);
  2364. CHECK (u (cosfCrt (f (0xc0aeb047u))) == 0x3f2dddfeu);
  2365. CHECK (u (expfCrt (f (0xbfb679c0u))) == 0x3e7622a1u);
  2366. CHECK (encodeFormats ({ true, false }) == std::vector<float> ({ 1.0f, 2.0f, 1.0f, 0.0f }));
  2367. CHECK (decodeFormats ({ 1.0f, 2.0f, 1.0f, 0.0f }) == std::vector<bool> ({ true, false }));
  2368. CHECK (! parseState ({ 1, 2, 3 }).has_value());
  2369. }
  2370. TEST (directwave_matches_fl_with_demo_programs)
  2371. {
  2372. // Demo programs (voices, mod matrix, filters, chorus, reverb, shaper) with our own 16-bit samples in every zone:
  2373. // FNV-1a 64 of DirectWave_x64.dll's output (r72/py/synth.py + cmp.py: 44.1 kHz, host blocks of 512, tempo 120)
  2374. if (realFlpDir.empty())
  2375. return;
  2376. using namespace lynx::dw;
  2377. auto readAll = [] (const std::filesystem::path& p) {
  2378. std::ifstream in (p, std::ios::binary);
  2379. return std::vector<std::uint8_t> { std::istreambuf_iterator<char> (in), {} };
  2380. };
  2381. auto stateOf = [&] (const std::string& file, const std::string& channel) {
  2382. const auto path = std::filesystem::path (realFlpDir) / file;
  2383. if (! std::filesystem::exists (path))
  2384. return std::vector<std::uint8_t>();
  2385. const auto p = flp::parseProject (readAll (path));
  2386. for (const auto& c : p.channels)
  2387. if (c.pluginName == "DirectWave" && c.name == channel)
  2388. return c.pluginState;
  2389. return std::vector<std::uint8_t>();
  2390. };
  2391. auto sampleValue = [] (int z, std::int64_t j) {
  2392. const auto v = (j * (13 + z * 7)) % 4096 - 2048;
  2393. const auto w = ((j * j + z * 31) % 997) - 498;
  2394. return (std::int16_t) std::clamp<std::int64_t> (v * 6 + w * 8, -32768, 32767);
  2395. };
  2396. std::vector<RefNote> notes;
  2397. auto note = [&] (int start, int key, double vel, double fv, double fp, double cents) {
  2398. RefNote n; n.start = start; n.key = key; n.velocity = vel; n.finalVol = fv; n.finalPan = fp; n.cents = cents;
  2399. notes.push_back (n);
  2400. };
  2401. 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);
  2402. note (44100, 36, 0.3, 0.8, 0.8, -40); note (80000, 72, 0.9, 0.6, -0.6, 0);
  2403. notes[0].release = 40000; notes[1].release = 70000; notes[2].release = 60000;
  2404. auto run = [&] (const std::string& file, const std::string& channel, std::uint64_t expected) {
  2405. const auto st = parseState (stateOf (file, channel));
  2406. if (! st)
  2407. return;
  2408. const auto* prog = playedProgram (*st);
  2409. CHECK (prog != nullptr);
  2410. if (prog == nullptr)
  2411. return;
  2412. std::vector<std::shared_ptr<const ZoneSamples>> zs;
  2413. for (int z = 0; z < (int) prog->zones.size(); ++z)
  2414. {
  2415. auto s = std::make_shared<ZoneSamples>();
  2416. s->bits = 16;
  2417. s->channels = 1;
  2418. s->frames = prog->zones[(std::size_t) z].frames;
  2419. std::vector<std::int16_t> v ((std::size_t) s->frames + 512, 0);
  2420. for (std::int64_t j = 0; j < s->frames; ++j)
  2421. v[(std::size_t) j + 256] = sampleValue (z, j);
  2422. s->i16.push_back (std::move (v));
  2423. zs.push_back (s);
  2424. }
  2425. EngineOptions o;
  2426. o.velocityAsInitVol = (st->flags & 2) != 0;
  2427. o.noGainClamp = (st->flags & 1) != 0;
  2428. const auto out = renderReference (*prog, zs, o, notes, 44100.0, 120000, 512, 1);
  2429. std::uint64_t h = 14695981039346656037ull;
  2430. for (const float x : out)
  2431. {
  2432. std::uint32_t b;
  2433. std::memcpy (&b, &x, 4);
  2434. for (int k = 0; k < 4; ++k)
  2435. h = (h ^ ((b >> (8 * k)) & 0xffu)) * 1099511628211ull;
  2436. }
  2437. CHECK (h == expected);
  2438. if (h != expected)
  2439. std::printf (" DirectWave %s: %016llx\n", channel.c_str(), (unsigned long long) h);
  2440. };
  2441. run ("Demo projects/Visual/ZGameEditor Visualizer/Treben - Umziky - All the same.flp", "Gtr", 0xa1de3dd6be53d665ull);
  2442. run ("Demo projects/Visual/ZGameEditor Visualizer/Treben - Umziky - All the same.flp", "Rhodes", 0xcd458ec6a857f9c7ull);
  2443. run ("System/Render tests/Directwave.flp", "DW", 0x95b4c2e29b664dd1ull);
  2444. }
  2445. TEST (fl_keys_matches_fl_with_the_installation)
  2446. {
  2447. // FL Keys' own output (r69/keys/kcmp3.py: the installation's DLL, FL's x87 host pan law, blocks of 512 at
  2448. // 44.1 kHz): FNV-1a 64 of the interleaved float bits of 24 000 frames
  2449. if (realFlpDir.empty())
  2450. return;
  2451. using namespace lynx::flkeys;
  2452. const auto folder = std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "FL Keys" / "Wavtables";
  2453. if (! std::filesystem::exists (folder / "mda Piano.wti"))
  2454. return;
  2455. auto readAll = [] (const std::filesystem::path& p) {
  2456. std::ifstream f (p, std::ios::binary);
  2457. return std::vector<std::uint8_t> { std::istreambuf_iterator<char> (f), {} };
  2458. };
  2459. auto pcm = [] (const std::vector<std::uint8_t>& w) {
  2460. FileSamples f;
  2461. std::uint16_t channels = 1;
  2462. for (std::size_t pos = 12; pos + 8 <= w.size();)
  2463. {
  2464. std::uint32_t size = 0;
  2465. std::memcpy (&size, w.data() + pos + 4, 4);
  2466. if (std::memcmp (w.data() + pos, "fmt ", 4) == 0)
  2467. std::memcpy (&channels, w.data() + pos + 10, 2);
  2468. else if (std::memcmp (w.data() + pos, "data", 4) == 0)
  2469. {
  2470. f.samples.resize (std::min<std::size_t> (size, w.size() - pos - 8) / 2);
  2471. std::memcpy (f.samples.data(), w.data() + pos + 8, f.samples.size() * 2);
  2472. f.channels = channels;
  2473. }
  2474. pos += 8 + size + (size & 1);
  2475. }
  2476. return f;
  2477. };
  2478. auto run = [&] (const std::string& name, std::array<int, 15> knobs, std::uint64_t expected) {
  2479. const auto wti = readAll (folder / (name + ".wti"));
  2480. std::vector<std::string> names;
  2481. auto table = parseWti (std::string (wti.begin(), wti.end()), names);
  2482. CHECK (table.has_value());
  2483. if (! table)
  2484. return;
  2485. std::vector<FileSamples> files;
  2486. for (std::size_t i = 0; i < names.size(); ++i)
  2487. {
  2488. auto rel = names[i];
  2489. std::replace (rel.begin(), rel.end(), '\\', '/');
  2490. const auto bytes = readAll (folder / rel);
  2491. if (table->raw)
  2492. {
  2493. FileSamples f;
  2494. f.samples.resize (bytes.size() / 2);
  2495. std::memcpy (f.samples.data(), bytes.data(), f.samples.size() * 2);
  2496. files.push_back (std::move (f));
  2497. }
  2498. else
  2499. {
  2500. table->files[i] = wavFileInfo (bytes);
  2501. files.push_back (pcm (bytes));
  2502. }
  2503. }
  2504. const auto w = buildWavetable (*table, files);
  2505. CHECK (w != nullptr);
  2506. if (w == nullptr)
  2507. return;
  2508. State s;
  2509. s.wavetable = name;
  2510. s.knobs = knobs;
  2511. auto note = [] (int start, int key, double iv, double fv, double fp, double cents) {
  2512. RefNote n;
  2513. n.start = start, n.key = key, n.initVol = iv, n.finalVol = fv, n.finalPan = fp, n.cents = cents;
  2514. return n;
  2515. };
  2516. 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),
  2517. 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) };
  2518. // (floats as the harness parses them)
  2519. for (auto& n : notes)
  2520. n.initVol = (float) n.initVol, n.finalVol = (float) n.finalVol, n.finalPan = (float) n.finalPan;
  2521. notes[0].release = 9000, notes[2].release = 12000, notes[3].release = 15000, notes[5].release = 20000;
  2522. const auto out = renderReference (*w, s, notes, 44100.0, 24000, 512, true);
  2523. std::uint64_t h = 0xcbf29ce484222325ull;
  2524. for (auto v : out)
  2525. {
  2526. std::uint32_t b;
  2527. std::memcpy (&b, &v, 4);
  2528. for (int k = 0; k < 4; ++k)
  2529. h = (h ^ ((b >> (8 * k)) & 0xff)) * 0x100000001b3ull;
  2530. }
  2531. CHECK (h == expected);
  2532. };
  2533. run ("mda Piano", { 1000, 138, 388, -1000, 716, 0, -1000, 256, 0, 0, 0, 0, 0, 0, 0 }, 0x78268ec007f31309ull);
  2534. run ("Rhodes", { 200, 300, 500, 800, 300, 1500, 300, 1500, 300, 400, 400, 800, 900, 30000, 700 }, 0xcc91c93d917f1d31ull);
  2535. run ("Roto Organ", { 1000, 1000, 1000, 1000, 0, 0, -1000, 0, 1000, 0, 500, 1000, 1000, 0, 300 }, 0x99eb13e87640a1c6ull);
  2536. }
  2537. TEST (sampler_plays_pitched_one_shot)
  2538. {
  2539. // 1000-frame ramp sample: the position can be measured from the amplitude
  2540. auto data = std::make_shared<lynx::SampleData>();
  2541. data->sampleRate = 48000.0;
  2542. for (int i = 0; i < 1000; ++i)
  2543. data->left.push_back (1.0f);
  2544. auto sampler = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  2545. sampler->prepare (48000.0);
  2546. std::vector<float> l (2048, 0.0f), r (2048, 0.0f);
  2547. sampler->noteOn (60, 1.0f);
  2548. sampler->noteOff (60); // one-shot: noteOff doesn't cut
  2549. sampler->render (l.data(), r.data(), 2048, 1.0f, 1.0f);
  2550. int sounding = 0;
  2551. for (auto s : l)
  2552. sounding += s > 0.5f;
  2553. CHECK (sounding >= 995 && sounding <= 1000); // plays the whole sample at the original pitch
  2554. std::fill (l.begin(), l.end(), 0.0f);
  2555. sampler->noteOn (72, 1.0f); // one octave up: half the duration
  2556. sampler->render (l.data(), r.data(), 2048, 1.0f, 1.0f);
  2557. sounding = 0;
  2558. for (auto s : l)
  2559. sounding += s > 0.5f;
  2560. CHECK (sounding >= 495 && sounding <= 500);
  2561. CHECK (r[10] > 0.5f); // mono goes to both sides
  2562. auto silent = lynx::makeInstrument (lynx::InstrumentKind::sampler, nullptr);
  2563. silent->prepare (48000.0);
  2564. silent->noteOn (60, 1.0f);
  2565. std::fill (l.begin(), l.end(), 0.0f);
  2566. silent->render (l.data(), r.data(), 64, 1.0f, 1.0f);
  2567. CHECK (l[0] == 0.0f);
  2568. }
  2569. TEST (rack_live_notes_and_sample_provider)
  2570. {
  2571. lynx::Song song;
  2572. lynx::ChannelDef c;
  2573. c.name = "Smp";
  2574. c.instrument = lynx::InstrumentKind::sampler;
  2575. c.samplePath = "a.wav";
  2576. song.channels.push_back (c);
  2577. song.patterns.emplace_back();
  2578. int loads = 0;
  2579. auto provider = [&] (const std::string& path) -> std::shared_ptr<const lynx::SampleData> {
  2580. ++loads;
  2581. CHECK (path == "a.wav");
  2582. auto d = std::make_shared<lynx::SampleData>();
  2583. d->left.assign (4800, 0.5f);
  2584. return d;
  2585. };
  2586. auto state = lynx::RackState::build (song, nullptr, 48000.0, provider);
  2587. CHECK (loads == 1);
  2588. lynx::RackRenderer renderer;
  2589. renderer.prepare (48000.0);
  2590. lynx::LiveEventQueue live;
  2591. lynx::NoteEvent on;
  2592. on.channel = 0;
  2593. on.velocity = 1.0f;
  2594. live.push (on);
  2595. std::vector<float> l (256), r (256);
  2596. renderer.process (state.get(), false, 0.0, 256, l.data(), r.data(), &live); // stopped, but the live note plays
  2597. CHECK (l[0] > 0.1f);
  2598. // same sample: reused; different sample: reloaded
  2599. auto same = lynx::RackState::build (song, state.get(), 48000.0, provider);
  2600. CHECK (loads == 1 && same->instruments[0] == state->instruments[0]);
  2601. song.channels[0].samplePath = "b.wav";
  2602. auto other = lynx::RackState::build (song, state.get(), 48000.0, [&] (const std::string&) { ++loads; return nullptr; });
  2603. CHECK (loads == 2 && other->instruments[0] != state->instruments[0]);
  2604. }
  2605. TEST (flp_paths_resolve_and_samplers_import)
  2606. {
  2607. lynx::FlPathRoots roots { "C:\\FL", "C:\\FL\\Data", "D:\\User" };
  2608. CHECK (lynx::resolveFlPath ("%FLStudioFactoryData%\\Data\\x.wav", roots) == "C:\\FL\\Data\\x.wav");
  2609. CHECK (lynx::resolveFlPath ("%FLStudioData%\\Patches\\y.wav", roots) == "C:\\FL\\Data\\Patches\\y.wav");
  2610. CHECK (lynx::resolveFlPath ("%FLStudioUserData%\\z.wav", roots) == "D:\\User\\z.wav");
  2611. CHECK (lynx::resolveFlPath ("E:\\abs.wav", roots) == "E:\\abs.wav");
  2612. CHECK (lynx::resolveFlPath ("%FLStudioUserData%\\z.wav", {}) == "");
  2613. flp::Project p;
  2614. p.channels = { { 0, flp::ChannelType::sampler, "Kick", "", "%FLStudioData%\\k.wav", 1 },
  2615. { 1, flp::ChannelType::sampler, "Hat", "", "%FLStudioData%\\missing.wav", 1 } };
  2616. lynx::FlpImportOptions options;
  2617. options.roots = roots;
  2618. options.fileExists = [] (const std::string& path) { return path == "C:\\FL\\Data\\k.wav"; };
  2619. const auto s = lynx::importFlp (p, options);
  2620. CHECK (s.channels[0].instrument == lynx::InstrumentKind::sampler);
  2621. CHECK (s.channels[0].samplePath == "C:\\FL\\Data\\k.wav");
  2622. CHECK (s.channels[1].instrument == lynx::InstrumentKind::hat); // no file: built-in instrument
  2623. }
  2624. TEST (soft_clip_is_transparent_below_knee_and_bounded)
  2625. {
  2626. float s[] = { 0.5f, -0.89f, 1.5f, -3.0f, 0.95f };
  2627. lynx::softClip (s, 5);
  2628. CHECK (s[0] == 0.5f && s[1] == -0.89f);
  2629. CHECK (s[2] > 0.9f && s[2] <= 1.0f);
  2630. CHECK (s[3] < -0.9f && s[3] >= -1.0f);
  2631. CHECK (s[4] > 0.9f && s[4] < 0.95f);
  2632. }
  2633. TEST (song_scheduler_plays_clips_with_offset_and_cut)
  2634. {
  2635. lynx::Song s;
  2636. s.channels.resize (1);
  2637. auto& p = s.patterns.emplace_back();
  2638. for (int step : { 0, 4, 8, 12 }) // one note per beat
  2639. p.toggleStep (0, step);
  2640. // clip at bar 2 playing only the second half of the pattern (beats 3 and 4)
  2641. lynx::Clip c;
  2642. c.pattern = 0;
  2643. c.start = 4 * lynx::ppq;
  2644. c.length = 2 * lynx::ppq;
  2645. c.offset = 2 * lynx::ppq;
  2646. s.playlist.push_back (c);
  2647. CHECK (s.songLength() == 8 * lynx::ppq);
  2648. const double sr = 48000.0;
  2649. const auto tps = lynx::ticksPerSample (120.0, sr); // 1 beat = 24000 samples
  2650. lynx::EventBuffer ev;
  2651. // whole song in a single block (8 beats = 192000 samples)
  2652. lynx::scheduleSong (s, 0.0, 192000, tps, ev);
  2653. std::vector<int> ons;
  2654. for (auto& e : ev)
  2655. if (e.type == lynx::NoteEvent::Type::on)
  2656. ons.push_back (e.sampleOffset);
  2657. CHECK (ons.size() == 2);
  2658. CHECK (ons.size() == 2 && ons[0] == 4 * 24000 && ons[1] == 5 * 24000);
  2659. // crossing the loop: bar 1 is empty, so nothing at the start of the second pass
  2660. ev.clear();
  2661. lynx::scheduleSong (s, 8.0 * lynx::ppq - 100 * tps, 200, tps, ev);
  2662. CHECK (ev.size() == 0);
  2663. // a note longer than the clip is cut at the clip end
  2664. p.notes[3].length = 4 * lynx::ppq;
  2665. ev.clear();
  2666. lynx::scheduleSong (s, 0.0, 192000, tps, ev);
  2667. int lastOff = -1;
  2668. for (auto& e : ev)
  2669. if (e.type == lynx::NoteEvent::Type::off)
  2670. lastOff = std::max (lastOff, e.sampleOffset);
  2671. CHECK (lastOff == 6 * 24000);
  2672. }
  2673. TEST (remove_pattern_fixes_clips)
  2674. {
  2675. lynx::Song s;
  2676. s.patterns.resize (3);
  2677. s.currentPattern = 2;
  2678. s.playlist = { { 0, 0, 0, 100, 0 }, { 1, 0, 100, 100, 0 }, { 2, 1, 0, 100, 0 } };
  2679. s.removePattern (1);
  2680. CHECK (s.patterns.size() == 2);
  2681. CHECK (s.playlist.size() == 2 && s.playlist[1].pattern == 1);
  2682. CHECK (s.currentPattern == 1);
  2683. }
  2684. TEST (flp_import_3x_osc_state_and_channel_filter)
  2685. {
  2686. // new 3x Osc patch (version 14): osc 2 mix 64 coarse -12, osc 3 mix 32 coarse -24
  2687. std::vector<std::uint8_t> blob (460, 0);
  2688. auto put = [&] (std::size_t off, std::int32_t v) {
  2689. for (int b = 0; b < 4; ++b)
  2690. blob[off + (std::size_t) b] = (std::uint8_t) ((std::uint32_t) v >> (8 * b));
  2691. };
  2692. put (0x00, 14);
  2693. put (0x08, 3); // osc 1 saw
  2694. put (0x10, 7); // osc 1 fine +7
  2695. put (0x04, -32); // osc 1 pan -0.5
  2696. put (0x1C, 64); // osc 2 mix
  2697. put (0x28, -12); // osc 2 coarse
  2698. put (0x38, 32); // osc 3 mix
  2699. put (0x40, 2); // osc 3 square
  2700. put (0x44, -19); // osc 3 coarse: -1 octave and -7 semitones
  2701. lynx::SynthSettings s;
  2702. CHECK (lynx::synthFrom3xOsc (blob, s));
  2703. 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);
  2704. CHECK (s.osc[0].wave == lynx::Waveform::saw && s.osc[0].detune == 7.0f && s.osc[0].pan == -0.5f);
  2705. CHECK (s.osc[1].wave == lynx::Waveform::sine && s.osc[1].octave == -1 && s.osc[1].semitones == 0);
  2706. CHECK (s.osc[2].wave == lynx::Waveform::square && s.osc[2].octave == -1 && s.osc[2].semitones == -7);
  2707. CHECK (! lynx::synthFrom3xOsc (std::vector<std::uint8_t> (40, 0), s));
  2708. // channel filter: open by default; LP with Mod X 128 ~ 2.71 kHz; Fast LP with Mod X 128 ~ 280 Hz
  2709. float hz = 0.0f, res = 1.0f;
  2710. int mode = -1;
  2711. lynx::synthFilterFromChannel (256, 0, 0, hz, res, mode);
  2712. CHECK (hz >= 19999.0f && res == 0.0f);
  2713. lynx::synthFilterFromChannel (128, 0, 1, hz, res, mode);
  2714. CHECK (std::abs (hz - 2710.0f) < 30.0f && res == 0.0f);
  2715. lynx::synthFilterFromChannel (128, 0, 0, hz, res, mode);
  2716. CHECK (hz > 240.0f && hz < 320.0f);
  2717. lynx::synthFilterFromChannel (128, 256, 1, hz, res, mode);
  2718. CHECK (res > 0.8f);
  2719. CHECK (mode == 0);
  2720. lynx::synthFilterFromChannel (64, 0, 3, hz, res, mode); // high-pass at ~840 Hz
  2721. CHECK (mode == 2 && std::abs (hz - 840.0f) < 40.0f);
  2722. lynx::synthFilterFromChannel (128, 0, 2, hz, res, mode); // band-pass
  2723. CHECK (mode == 1);
  2724. }
  2725. TEST (synth_pitch_and_filter_envelopes)
  2726. {
  2727. auto render = [] (const lynx::SynthSettings& s, int frames) {
  2728. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  2729. inst->prepare (48000.0);
  2730. lynx::ChannelDef c;
  2731. c.synth = s;
  2732. inst->configure (c, 120.0);
  2733. inst->noteOn (45, 1.0f); // A2 = 110 Hz
  2734. std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
  2735. inst->render (l.data(), r.data(), frames, 1.0f, 1.0f);
  2736. return l;
  2737. };
  2738. auto crossings = [] (const std::vector<float>& b, int from, int to) {
  2739. int n = 0;
  2740. for (int i = from + 1; i < to; ++i)
  2741. n += (b[(std::size_t) i - 1] < 0.0f) != (b[(std::size_t) i] < 0.0f);
  2742. return n;
  2743. };
  2744. // 3x Osc "kick": sine with a +24 st pitch envelope falling fast
  2745. lynx::SynthSettings kick;
  2746. kick.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  2747. kick.cutoff = 20000.0f;
  2748. kick.pitchEnv = { 24.0f, 0.0f, 0.0f, 0.05f, 0.0f, 0.05f };
  2749. const auto k = render (kick, 48000);
  2750. const auto early = crossings (k, 0, 2400); // first 50 ms: well above 110 Hz
  2751. const auto late = crossings (k, 24000, 48000); // then back to 110 Hz (~110 crossings in 0.5 s)
  2752. CHECK (early > 20); // +24 st ramp over 50 ms: ~2.2x the average frequency (11 crossings without envelope)
  2753. CHECK (std::abs (late - 110) < 4);
  2754. // cutoff envelope: saw closed at 200 Hz opens +5 octaves on attack and closes again
  2755. lynx::SynthSettings pluck;
  2756. pluck.cutoff = 200.0f;
  2757. pluck.filterEnv = { 5.0f, 0.0f, 0.0f, 0.1f, 0.0f, 0.1f };
  2758. const auto p = render (pluck, 48000);
  2759. auto hf = [&] (int from, int to) { // energy of the difference (highs)
  2760. double e = 0.0;
  2761. for (int i = from + 1; i < to; ++i)
  2762. e += std::pow ((double) p[(std::size_t) i] - p[(std::size_t) i - 1], 2.0);
  2763. return e;
  2764. };
  2765. CHECK (hf (0, 2400) > 4.0 * hf (40000, 42400));
  2766. // Toxic Biohazard's envelope (REA_rodada33 §2): exponential towards 5 clipped at 1, so the cutoff is still near
  2767. // its base early in the attack, fully open at A = 0.2 s and back down after the exponential decay
  2768. lynx::SynthSettings toxic = pluck;
  2769. toxic.filterEnv = { 5.0f, 0.2f, 0.0f, 0.1f, 0.0f, 0.1f, 1 };
  2770. const auto t = render (toxic, 48000);
  2771. auto thf = [&] (int from, int to) {
  2772. double e = 0.0;
  2773. for (int i = from + 1; i < to; ++i)
  2774. e += std::pow ((double) t[(std::size_t) i] - t[(std::size_t) i - 1], 2.0);
  2775. return e;
  2776. };
  2777. CHECK (thf (9000, 9600) > 4.0 * thf (300, 900));
  2778. CHECK (thf (9000, 9600) > 4.0 * thf (40000, 40600));
  2779. }
  2780. TEST (synth_mono_reuses_the_sounding_voice)
  2781. {
  2782. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  2783. inst->prepare (48000.0);
  2784. lynx::ChannelDef c;
  2785. c.synth.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  2786. c.synth.cutoff = 20000.0f;
  2787. c.synth.mono = true;
  2788. inst->configure (c, 120.0);
  2789. std::vector<float> l (48000), r (48000);
  2790. inst->noteOn (57, 1.0f);
  2791. inst->render (l.data(), r.data(), 4800, 1.0f, 1.0f);
  2792. inst->noteOn (69, 1.0f); // A4 takes over the A3 voice: only one note sounding
  2793. inst->render (l.data() + 4800, r.data() + 4800, 43200, 1.0f, 1.0f);
  2794. int crossings = 0;
  2795. float peak = 0.0f;
  2796. for (std::size_t i = 24001; i < 48000; ++i)
  2797. {
  2798. crossings += (l[i - 1] < 0.0f) != (l[i] < 0.0f);
  2799. peak = std::max (peak, std::abs (l[i]));
  2800. }
  2801. CHECK (std::abs (crossings - 440) < 4); // 0.5 s of 440 Hz, without the A3 underneath
  2802. CHECK (peak < 0.35f); // one voice (0.3 level), not two summed
  2803. // no click on the handover: the jump between neighbouring samples stays sine-sized
  2804. float jump = 0.0f;
  2805. for (std::size_t i = 4790; i < 4810; ++i)
  2806. jump = std::max (jump, std::abs (l[i] - l[i - 1]));
  2807. CHECK (jump < 0.05f);
  2808. }
  2809. TEST (synth_porta_glides_and_am_modulates)
  2810. {
  2811. auto make = [] (const lynx::SynthSettings& s) {
  2812. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  2813. inst->prepare (48000.0);
  2814. lynx::ChannelDef c;
  2815. c.synth = s;
  2816. inst->configure (c, 120.0);
  2817. return inst;
  2818. };
  2819. auto crossings = [] (const std::vector<float>& b, std::size_t from, std::size_t to) {
  2820. int n = 0;
  2821. for (auto i = from + 1; i < to; ++i)
  2822. n += (b[i - 1] < 0.0f) != (b[i] < 0.0f);
  2823. return n;
  2824. };
  2825. // mono + 1-beat porta (0.5 s at 120 BPM): from A3 to A4, halfway ~E4 (329.6 Hz)
  2826. lynx::SynthSettings s;
  2827. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  2828. s.cutoff = 20000.0f;
  2829. s.mono = true;
  2830. s.porta = true;
  2831. s.portaBeats = 1.0f;
  2832. auto inst = make (s);
  2833. std::vector<float> l (96000), r (96000);
  2834. inst->noteOn (57, 1.0f);
  2835. inst->render (l.data(), r.data(), 4800, 1.0f, 1.0f);
  2836. inst->noteOn (69, 1.0f);
  2837. inst->render (l.data() + 4800, r.data() + 4800, 91200, 1.0f, 1.0f);
  2838. const auto middle = crossings (l, 4800 + 12000 - 2400, 4800 + 12000 + 2400); // 0.1 s around 0.25 s
  2839. CHECK (std::abs (middle - 66) < 6); // ~330 Hz * 0.1 s * 2
  2840. CHECK (std::abs (crossings (l, 48000, 96000) - 880) < 4); // reached A4 (1 s = 880 crossings)
  2841. // AM: osc 3 at level 1.0, slow sine, modulates osc 1 (the average level drops, the sum doesn't gain osc 3)
  2842. lynx::SynthSettings am;
  2843. am.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  2844. am.osc[2] = { lynx::Waveform::sine, -3, 0.0f, 1.0f };
  2845. am.cutoff = 20000.0f;
  2846. am.am = true;
  2847. auto modulated = make (am);
  2848. modulated->noteOn (69, 1.0f);
  2849. std::fill (l.begin(), l.end(), 0.0f);
  2850. modulated->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
  2851. float peak = 0.0f;
  2852. for (std::size_t i = 4800; i < 48000; ++i)
  2853. peak = std::max (peak, std::abs (l[i]));
  2854. CHECK (peak > 0.45f && peak < 0.65f); // 0.3 · (1 + 1) at most: (1 − o3) goes up to 2
  2855. }
  2856. TEST (channel_lfo_vibrato_and_tremolo)
  2857. {
  2858. auto render = [] (const lynx::SynthSettings& s) {
  2859. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  2860. inst->prepare (48000.0);
  2861. lynx::ChannelDef c;
  2862. c.synth = s;
  2863. inst->configure (c, 120.0);
  2864. inst->noteOn (69, 1.0f);
  2865. std::vector<float> l (48000), r (48000);
  2866. inst->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
  2867. return l;
  2868. };
  2869. lynx::SynthSettings s;
  2870. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  2871. s.cutoff = 20000.0f;
  2872. // tremolo: 100% volume LFO at 4 Hz (0.25 s period) -> the amplitude envelope goes from ~0 to ~2x
  2873. auto tremolo = s;
  2874. tremolo.lfo[lynx::lfoVolume] = { 1.0f, 0.25f, false, 0, 0.0f, 0.0f };
  2875. const auto t = render (tremolo);
  2876. float lowest = 1.0f, highest = 0.0f;
  2877. for (std::size_t w = 4800; w + 480 <= 48000; w += 480) // 10 ms windows (4.4 cycles of 440 Hz)
  2878. {
  2879. float peak = 0.0f;
  2880. for (auto i = w; i < w + 480; ++i)
  2881. peak = std::max (peak, std::abs (t[i]));
  2882. lowest = std::min (lowest, peak);
  2883. highest = std::max (highest, peak);
  2884. }
  2885. CHECK (lowest < 0.08f && highest > 0.5f);
  2886. // synced vibrato: ±1 semitone, 1-beat period (0.5 s at 120 BPM); the average stays at 440 Hz
  2887. auto vibrato = s;
  2888. vibrato.lfo[lynx::lfoPitch] = { 1.0f, 1.0f, true, 0, 0.0f, 0.0f };
  2889. const auto v = render (vibrato);
  2890. int crossings = 0;
  2891. for (std::size_t i = 1; i < 48000; ++i)
  2892. crossings += (v[i - 1] < 0.0f) != (v[i] < 0.0f);
  2893. CHECK (std::abs (crossings - 880) < 12);
  2894. int firstQuarter = 0; // first quarter of the cycle: rising sine -> higher than 440
  2895. for (std::size_t i = 1; i < 6000; ++i)
  2896. firstQuarter += (v[i - 1] < 0.0f) != (v[i] < 0.0f);
  2897. CHECK (firstQuarter > 112);
  2898. }
  2899. TEST (sampler_porta_glides_from_previous_note)
  2900. {
  2901. auto data = std::make_shared<lynx::SampleData>();
  2902. data->sampleRate = 48000.0;
  2903. data->left.resize (96000);
  2904. for (std::size_t i = 0; i < data->left.size(); ++i)
  2905. data->left[i] = 0.5f * (float) std::sin (6.283185307 * 220.0 * (double) i / 48000.0);
  2906. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  2907. inst->prepare (48000.0);
  2908. lynx::ChannelDef c;
  2909. c.sampler.cut = true;
  2910. c.sampler.porta = true;
  2911. c.sampler.portaBeats = 1.0f; // 0.5 s at 120 BPM
  2912. inst->configure (c, 120.0);
  2913. std::vector<float> l (24000), r (24000);
  2914. inst->noteOn (60, 1.0f);
  2915. inst->render (l.data(), r.data(), 2400, 1.0f, 1.0f);
  2916. inst->noteOn (72, 1.0f); // one octave up: starts at 220 Hz and rises
  2917. std::fill (l.begin(), l.end(), 0.0f);
  2918. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  2919. auto crossings = [&] (std::size_t from, std::size_t to) {
  2920. int n = 0;
  2921. for (auto i = from + 1; i < to; ++i)
  2922. n += (l[i - 1] < 0.0f) != (l[i] < 0.0f);
  2923. return n;
  2924. };
  2925. const auto start = crossings (600, 2400); // 37.5 ms still near 220 Hz: ~17 crossings (440 Hz would give 33)
  2926. const auto end = crossings (21600, 24000); // 50 ms at the end of the glide: near 440 Hz (~44; 220 Hz would give 22)
  2927. CHECK (start < 22);
  2928. CHECK (end > 36);
  2929. }
  2930. TEST (zoned_sampler_picks_zone_by_key_and_velocity)
  2931. {
  2932. auto sine = [] (double hz, float amp) {
  2933. auto d = std::make_shared<lynx::SampleData>();
  2934. d->sampleRate = 48000.0;
  2935. d->left.resize (48000);
  2936. for (std::size_t i = 0; i < d->left.size(); ++i)
  2937. d->left[i] = amp * (float) std::sin (6.283185307 * hz * (double) i / 48000.0);
  2938. return d;
  2939. };
  2940. // lows: 220 Hz sine recorded as A3 (57), up to key 69; highs: 880 Hz as A5 (81)
  2941. // hard layer on the highs: same tuning, louder
  2942. std::vector<lynx::LoadedZone> zones {
  2943. { { "graves.wav", 57, 0, 69, 0, 127, 0.0f, 1.0f }, sine (220.0, 0.5f) },
  2944. { { "agudos.wav", 81, 70, 127, 0, 99, 0.0f, 1.0f }, sine (880.0, 0.25f) },
  2945. { { "agudos forte.wav", 81, 70, 127, 100, 127, 0.0f, 1.0f }, sine (880.0, 0.9f) },
  2946. };
  2947. auto inst = lynx::makeZonedSampler (zones);
  2948. inst->prepare (48000.0);
  2949. inst->configure (lynx::ChannelDef {}, 120.0);
  2950. auto play = [&] (int key, float velocity) {
  2951. inst->allNotesOff();
  2952. std::vector<float> l (24000), r (24000);
  2953. inst->render (l.data(), r.data(), 2000, 1.0f, 1.0f); // let the old voices die out
  2954. std::fill (l.begin(), l.end(), 0.0f);
  2955. inst->noteOn (key, velocity);
  2956. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  2957. int crossings = 0;
  2958. float peak = 0.0f;
  2959. for (std::size_t i = 1; i < 24000; ++i)
  2960. {
  2961. crossings += (l[i - 1] < 0.0f) != (l[i] < 0.0f);
  2962. peak = std::max (peak, std::abs (l[i]));
  2963. }
  2964. return std::pair { crossings, peak };
  2965. };
  2966. const auto low = play (69, 0.5f); // A4 from root A3: 440 Hz -> 440 crossings in 0.5 s
  2967. CHECK (std::abs (low.first - 440) < 4);
  2968. const auto soft = play (81, 0.5f); // high zone at the root: 880 Hz
  2969. CHECK (std::abs (soft.first - 880) < 6);
  2970. const auto hard = play (81, 0.95f); // high velocity: hard layer
  2971. CHECK (hard.second > soft.second * 2.0f);
  2972. // overlapping layers round-robin: two identical zones at different levels, notes alternate
  2973. auto rr = lynx::makeZonedSampler ({ { { "a.wav", 60 }, sine (440.0, 0.2f) }, { { "b.wav", 60 }, sine (440.0, 0.8f) } });
  2974. rr->prepare (48000.0);
  2975. rr->configure (lynx::ChannelDef {}, 120.0);
  2976. float peaks[2] {};
  2977. for (int k = 0; k < 2; ++k)
  2978. {
  2979. rr->allNotesOff();
  2980. std::vector<float> l (4800), r (4800);
  2981. rr->render (l.data(), r.data(), 2000, 1.0f, 1.0f);
  2982. std::fill (l.begin(), l.end(), 0.0f);
  2983. rr->noteOn (60, 1.0f);
  2984. rr->render (l.data(), r.data(), 4800, 1.0f, 1.0f);
  2985. for (auto v : l)
  2986. peaks[k] = std::max (peaks[k], std::abs (v));
  2987. }
  2988. CHECK (std::abs (peaks[0] - peaks[1]) > 0.3f);
  2989. }
  2990. TEST (zoned_sampler_loops_while_held)
  2991. {
  2992. // 0.1 s sample with a loop in the middle: holding the note it keeps sounding well past the end of the file;
  2993. // on release, it leaves the loop and ends
  2994. auto data = std::make_shared<lynx::SampleData>();
  2995. data->sampleRate = 48000.0;
  2996. data->left.resize (4800);
  2997. for (std::size_t i = 0; i < data->left.size(); ++i)
  2998. data->left[i] = 0.5f * (float) std::sin (6.283185307 * 480.0 * (double) i / 48000.0); // 100 frames per cycle
  2999. lynx::SampleZone zone { "loop.wav", 60 };
  3000. zone.loopStart = 1000;
  3001. zone.loopEnd = 3000;
  3002. zone.loopMode = 2;
  3003. auto inst = lynx::makeZonedSampler ({ { zone, data } });
  3004. inst->prepare (48000.0);
  3005. lynx::ChannelDef c;
  3006. c.sampler.release = 0.01f;
  3007. inst->configure (c, 120.0);
  3008. inst->noteOn (60, 1.0f);
  3009. std::vector<float> l (48000), r (48000);
  3010. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f); // 0.5 s held
  3011. float held = 0.0f;
  3012. for (std::size_t i = 20000; i < 24000; ++i)
  3013. held = std::max (held, std::abs (l[i]));
  3014. CHECK (held > 0.3f);
  3015. inst->noteOff (60);
  3016. inst->render (l.data() + 24000, r.data() + 24000, 24000, 1.0f, 1.0f);
  3017. float after = 0.0f;
  3018. for (std::size_t i = 40000; i < 48000; ++i)
  3019. after = std::max (after, std::abs (l[i]));
  3020. CHECK (after == 0.0f);
  3021. }
  3022. TEST (global_lfo_follows_song_position)
  3023. {
  3024. // global volume LFO (1-beat period): a note mid-cycle starts mid-wave; the
  3025. // same note with a per-note LFO starts at phase 0
  3026. auto first = [] (bool global, double songTicks) {
  3027. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  3028. inst->prepare (48000.0);
  3029. lynx::ChannelDef c;
  3030. c.synth.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  3031. c.synth.cutoff = 20000.0f;
  3032. c.synth.lfo[lynx::lfoVolume] = { 1.0f, 1.0f, true, 2, 0.0f, 0.0f, global }; // pulse: +1 in the first half
  3033. inst->configure (c, 120.0);
  3034. inst->setSongPosition (songTicks);
  3035. inst->noteOn (69, 1.0f);
  3036. std::vector<float> l (4800), r (4800);
  3037. inst->render (l.data(), r.data(), 4800, 1.0f, 1.0f);
  3038. float peak = 0.0f;
  3039. for (std::size_t i = 2400; i < 4800; ++i)
  3040. peak = std::max (peak, std::abs (l[i]));
  3041. return peak;
  3042. };
  3043. // mid-cycle (phase 0.6) the pulse is at −1: volume ~0; per note (phase 0) it is at +1: ~2x
  3044. const auto globalLate = first (true, 0.6 * lynx::ppq);
  3045. const auto perNote = first (false, 0.6 * lynx::ppq);
  3046. CHECK (globalLate < 0.05f);
  3047. CHECK (perNote > 0.4f);
  3048. }
  3049. TEST (sampler_channel_filter_darkens_and_bypasses_when_open)
  3050. {
  3051. // white noise in the sampler: with a 300 Hz cutoff the energy drops a lot; open passes unchanged
  3052. auto data = std::make_shared<lynx::SampleData>();
  3053. data->sampleRate = 48000.0;
  3054. data->left.resize (48000);
  3055. std::uint32_t seed = 7;
  3056. for (auto& v : data->left)
  3057. {
  3058. seed = seed * 1664525u + 1013904223u;
  3059. v = ((float) (seed >> 8) / 16777216.0f - 0.5f);
  3060. }
  3061. data->right = data->left;
  3062. auto energy = [&] (float cutoff) {
  3063. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  3064. inst->prepare (48000.0);
  3065. lynx::ChannelDef c;
  3066. c.sampler.cutoff = cutoff;
  3067. inst->configure (c, 120.0);
  3068. inst->noteOn (60, 1.0f);
  3069. std::vector<float> l (24000), r (24000);
  3070. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  3071. double e = 0.0;
  3072. for (auto v : l)
  3073. e += (double) v * v;
  3074. return e;
  3075. };
  3076. const auto open = energy (20000.0f), dark = energy (300.0f);
  3077. {
  3078. // high-pass at 10 kHz: also removes most of the noise energy, but from the other side
  3079. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  3080. inst->prepare (48000.0);
  3081. lynx::ChannelDef hp;
  3082. hp.sampler.cutoff = 10000.0f;
  3083. hp.sampler.filterType = 2;
  3084. inst->configure (hp, 120.0);
  3085. inst->noteOn (60, 1.0f);
  3086. std::vector<float> l (24000), r (24000);
  3087. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  3088. double e = 0.0, lowE = 0.0;
  3089. float lp = 0.0f;
  3090. for (auto v : l)
  3091. {
  3092. e += (double) v * v;
  3093. lp += 0.01f * (v - lp); // crude low-pass: almost nothing should remain below
  3094. lowE += (double) lp * lp;
  3095. }
  3096. CHECK (e > 10.0 && e < 0.7 * energy (20000.0f));
  3097. CHECK (lowE < e * 0.01);
  3098. }
  3099. CHECK (open > 100.0);
  3100. CHECK (dark < open * 0.05);
  3101. // +12 st pitch envelope with sustain 1: the sample runs at double speed
  3102. // and ends in half the time (1 s of sample -> 0.5 s)
  3103. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  3104. inst->prepare (48000.0);
  3105. lynx::ChannelDef c;
  3106. c.sampler.pitchEnv = { 12.0f, 0.0f, 0.0f, 0.1f, 1.0f, 0.1f };
  3107. inst->configure (c, 120.0);
  3108. inst->noteOn (60, 1.0f);
  3109. std::vector<float> l (48000), r (48000);
  3110. inst->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
  3111. float tail = 0.0f, body = 0.0f;
  3112. for (std::size_t i = 20000; i < 23000; ++i)
  3113. body = std::max (body, std::abs (l[i]));
  3114. for (std::size_t i = 25000; i < 48000; ++i)
  3115. tail = std::max (tail, std::abs (l[i]));
  3116. CHECK (body > 0.1f);
  3117. CHECK (tail == 0.0f);
  3118. }
  3119. TEST (synth_oscillator_pan_and_semitones)
  3120. {
  3121. auto render = [] (lynx::SynthSettings s, std::vector<float>& l, std::vector<float>& r) {
  3122. auto synth = lynx::makeInstrument (lynx::InstrumentKind::synth);
  3123. synth->prepare (48000.0);
  3124. lynx::ChannelDef c;
  3125. c.synth = s;
  3126. synth->configure (c, 120.0);
  3127. synth->noteOn (69, 1.0f);
  3128. l.assign (24000, 0.0f);
  3129. r.assign (24000, 0.0f);
  3130. synth->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  3131. };
  3132. lynx::SynthSettings s;
  3133. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f, 7, 1.0f }; // a fifth up, fully right
  3134. s.cutoff = 20000.0f;
  3135. std::vector<float> l, r;
  3136. render (s, l, r);
  3137. float peakL = 0.0f, peakR = 0.0f;
  3138. int crossings = 0;
  3139. for (std::size_t i = 4800; i < 24000; ++i)
  3140. {
  3141. peakL = std::max (peakL, std::abs (l[i]));
  3142. peakR = std::max (peakR, std::abs (r[i]));
  3143. crossings += (r[i - 1] < 0.0f) != (r[i] < 0.0f);
  3144. }
  3145. CHECK (peakL < 1e-4f && peakR > 0.1f);
  3146. // 440 * 2^(7/12) = 659.3 Hz; 0.4 s = 263.7 cycles = ~527 crossings
  3147. CHECK (std::abs (crossings - 527) < 6);
  3148. }
  3149. TEST (fl_wrapper_state_parses_records_and_vst3_state)
  3150. {
  3151. // Fruity Wrapper blob (version 12): VST3 generator type, class ID, name, path and state
  3152. std::vector<std::uint8_t> blob;
  3153. 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))); };
  3154. auto record = [&] (std::vector<std::uint8_t>& b, std::uint32_t id, const std::vector<std::uint8_t>& payload) {
  3155. u32 (b, id);
  3156. u32 (b, (std::uint32_t) payload.size());
  3157. u32 (b, 0);
  3158. b.insert (b.end(), payload.begin(), payload.end());
  3159. };
  3160. auto bytes = [] (const std::string& s) { return std::vector<std::uint8_t> (s.begin(), s.end()); };
  3161. std::vector<std::uint8_t> vst3State;
  3162. u32 (vst3State, 1);
  3163. record (vst3State, 1, std::vector<std::uint8_t> (64, 0));
  3164. record (vst3State, 3, bytes ("COMPONENTE"));
  3165. record (vst3State, 2, bytes ("CONTROLADOR"));
  3166. // record 4: 3 slots → ParamIDs 700, −1 (empty), 0xFFFFFF01
  3167. std::vector<std::uint8_t> slots;
  3168. for (auto v : { 3u, 700u, 0xFFFFFFFFu, 0xFFFFFF01u })
  3169. u32 (slots, v);
  3170. record (vst3State, 4, slots);
  3171. u32 (blob, 12);
  3172. record (blob, 0x01, std::vector<std::uint8_t> (20, 0xFF));
  3173. record (blob, 0x32, { 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });
  3174. std::vector<std::uint8_t> cid;
  3175. for (int i = 0; i < 16; ++i)
  3176. cid.push_back ((std::uint8_t) (0xA0 + i));
  3177. record (blob, 0x34, cid);
  3178. record (blob, 0x36, bytes ("Surge XT"));
  3179. record (blob, 0x37, bytes ("C:/Program Files/Common Files/VST3/Surge XT.vst3"));
  3180. record (blob, 0x35, vst3State);
  3181. lynx::ForeignPlugin p;
  3182. CHECK (lynx::parseFlWrapper (blob, p));
  3183. CHECK (p.format == lynx::ForeignPlugin::Format::vst3);
  3184. CHECK (p.hasClassId && p.classId[0] == 0xA0 && p.classId[15] == 0xAF);
  3185. CHECK (p.name == "Surge XT" && p.path.find ("Surge XT.vst3") != std::string::npos);
  3186. std::vector<std::uint8_t> component, controller;
  3187. CHECK (lynx::splitFlVst3State (p.state, component, controller));
  3188. CHECK (component == bytes ("COMPONENTE") && controller == bytes ("CONTROLADOR"));
  3189. const auto saved = lynx::flVst3ParamSlots (p.state);
  3190. CHECK (saved == (std::vector<std::int32_t> { 700, -1, (std::int32_t) 0xFFFFFF01u }));
  3191. // FL's slot assignment: saved slots keep their ParamID wherever it now sits; the empty slot stays empty;
  3192. // a vanished ParamID (0xFFFFFF01) leaves its slot dead; parameters without a slot get slots 3, 4… in list order
  3193. const auto positions = lynx::flVst3SlotPositions (saved, { 5u, 700u, 9u });
  3194. CHECK (positions == (std::vector<int> { 1, -1, -1, 0, 2 }));
  3195. CHECK (lynx::flVst3SlotPositions ({}, { 5u, 6u }) == (std::vector<int> { 0, 1 })); // new instance: slot = position
  3196. // version out of range or no type: not a usable wrapper
  3197. blob[0] = 13;
  3198. CHECK (! lynx::parseFlWrapper (blob, p));
  3199. CHECK (! lynx::parseFlWrapper ({ 12, 0, 0, 0 }, p));
  3200. }
  3201. TEST (schedule_index_matches_full_scan)
  3202. {
  3203. // random song (long notes, swing, clips with offset); indexed scheduling must yield
  3204. // exactly the same events as the full scan, including in the block that wraps around
  3205. lynx::Song s = lynx::Song::makeDefault();
  3206. s.swing = 0.6f;
  3207. std::uint32_t seed = 12345;
  3208. auto rnd = [&] (int n) { seed = seed * 1664525u + 1013904223u; return (int) ((seed >> 8) % (std::uint32_t) n); };
  3209. s.patterns.resize (3);
  3210. for (auto& p : s.patterns)
  3211. {
  3212. p.notes.clear();
  3213. for (int i = 0; i < 60; ++i)
  3214. {
  3215. lynx::Note n;
  3216. n.start = rnd (16 * lynx::ppq);
  3217. n.length = rnd (5) == 0 ? rnd (12 * lynx::ppq) + 1 : rnd (lynx::ppq) + 1;
  3218. n.channel = rnd (4);
  3219. n.key = 40 + rnd (40);
  3220. n.slide = rnd (20) == 0;
  3221. p.notes.push_back (n);
  3222. }
  3223. p.sortNotes();
  3224. }
  3225. s.playlist.clear();
  3226. for (int i = 0; i < 12; ++i)
  3227. {
  3228. lynx::Clip c;
  3229. c.pattern = rnd (3);
  3230. c.track = rnd (4);
  3231. c.start = rnd (40) * lynx::ppq;
  3232. c.length = (rnd (12) + 1) * lynx::ppq;
  3233. c.offset = rnd (4) * lynx::ppq;
  3234. s.playlist.push_back (c);
  3235. }
  3236. s.mode = lynx::PlayMode::song;
  3237. const auto index = lynx::makeScheduleIndex (s);
  3238. const auto tps = lynx::ticksPerSample (140.0, 48000.0);
  3239. const auto loop = (double) s.songLength();
  3240. int mismatches = 0, events = 0;
  3241. for (int block = 0; block < 400; ++block)
  3242. {
  3243. const auto start = block == 0 ? loop - 100.0 * tps : (double) rnd ((int) loop * 2);
  3244. const auto samples = 64 + rnd (24000); // up to ~1 beat per block
  3245. lynx::EventBuffer a, b;
  3246. lynx::scheduleSong (s, start, samples, tps, a, false, nullptr);
  3247. lynx::scheduleSong (s, start, samples, tps, b, false, &index);
  3248. events += a.size();
  3249. if (a.size() != b.size())
  3250. {
  3251. ++mismatches;
  3252. continue;
  3253. }
  3254. for (int i = 0; i < a.size(); ++i)
  3255. {
  3256. const auto& x = *(a.begin() + i);
  3257. const auto& y = *(b.begin() + i);
  3258. mismatches += x.sampleOffset != y.sampleOffset || x.type != y.type || x.key != y.key || x.channel != y.channel;
  3259. }
  3260. }
  3261. CHECK (events > 500);
  3262. CHECK (mismatches == 0);
  3263. }
  3264. TEST (mixer_track_eq_defaults_and_boost)
  3265. {
  3266. // FL defaults: 90 Hz, 1.5 kHz and 8 kHz; peak Q 0.632; shelf slope 0.607
  3267. lynx::TrackEq eq;
  3268. CHECK (std::abs (lynx::TrackEq::peakQ (eq.width[1]) - 0.632) < 0.01);
  3269. CHECK (std::abs (lynx::TrackEq::shelfSlope (eq.width[0]) - 0.607) < 0.01);
  3270. CHECK (! eq.active());
  3271. // +12 dB peak at 1.5 kHz on a track: a 1.5 kHz sine rises ~4x
  3272. lynx::Song song = lynx::Song::makeDefault();
  3273. song.mixer[1].eq.gainDb[1] = 12.0f;
  3274. for (auto& c : song.channels)
  3275. c.mixerTrack = 1;
  3276. song.channels[4].instrument = lynx::InstrumentKind::synth;
  3277. song.channels[4].synth = {};
  3278. song.channels[4].synth.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  3279. song.channels[4].synth.cutoff = 20000.0f;
  3280. auto render = [] (const lynx::Song& s) {
  3281. auto state = lynx::RackState::build (s, nullptr, 48000.0);
  3282. lynx::RackRenderer renderer;
  3283. renderer.prepare (48000.0);
  3284. lynx::LiveEventQueue live;
  3285. lynx::NoteEvent on; // 1.5 kHz ~ key 90.6: use 91 (1568 Hz), still within the wide peak
  3286. on.channel = 4;
  3287. on.key = 91;
  3288. on.velocity = 1.0f;
  3289. live.push (on);
  3290. std::vector<float> l (24000), r (24000);
  3291. for (int pos = 0; pos < 24000; pos += 480)
  3292. renderer.process (state.get(), false, 0.0, 480, l.data() + pos, r.data() + pos, pos == 0 ? &live : nullptr);
  3293. float peak = 0.0f;
  3294. for (std::size_t i = 12000; i < 24000; ++i)
  3295. peak = std::max (peak, std::abs (l[i]));
  3296. return peak;
  3297. };
  3298. const auto boosted = render (song);
  3299. song.mixer[1].eq.gainDb[1] = 0.0f;
  3300. const auto flat = render (song);
  3301. CHECK (flat > 0.01f);
  3302. CHECK (boosted / flat > 3.0f && boosted / flat < 4.2f);
  3303. }
  3304. TEST (bars_follow_time_signature_markers)
  3305. {
  3306. lynx::Song song;
  3307. song.beatsPerBar = 4;
  3308. const auto bar4 = (std::int64_t) lynx::ppq * 4;
  3309. auto bars = song.barsBetween (0, bar4 * 3);
  3310. CHECK (bars.size() == 3 && bars[2].tick == bar4 * 2 && bars[2].number == 3);
  3311. // 7/4 from bar 3, and 3/4 in the middle of a 7 bar (cuts and restarts)
  3312. song.addMarker ({ bar4 * 2, "Verso", 7, 4 });
  3313. song.addMarker ({ bar4, "Nome só", 0, 4 });
  3314. const auto seven = (std::int64_t) lynx::ppq * 7;
  3315. song.addMarker ({ bar4 * 2 + seven + lynx::ppq * 2, "", 3, 4 });
  3316. bars = song.barsBetween (0, bar4 * 2 + seven * 2);
  3317. CHECK (bars.size() >= 5);
  3318. CHECK (bars[1].length == bar4 && bars[2].tick == bar4 * 2 && bars[2].length == seven);
  3319. CHECK (bars[3].length == lynx::ppq * 2); // cut short by the change
  3320. CHECK (bars[4].length == lynx::ppq * 3 && bars[4].number == 5);
  3321. CHECK (song.markers()[0].name == "Nome só"); // sorted by tick
  3322. // from far away: starts at the right bar without losing the numbering
  3323. const auto far = song.barsBetween (lynx::ppq * 3000, lynx::ppq * 3001);
  3324. CHECK (far.size() == 1 && far[0].length == lynx::ppq * 3);
  3325. CHECK (song.barAt (lynx::ppq * 3000).number == far[0].number);
  3326. CHECK ((lynx::ppq * 3000 - far[0].tick) < far[0].length);
  3327. }
  3328. TEST (split_drum_channel_moves_notes_to_pads)
  3329. {
  3330. lynx::Song song = lynx::Song::makeDefault();
  3331. const auto channels = (int) song.channels.size();
  3332. song.patterns[0].notes.clear();
  3333. song.patterns[0].notes.push_back ({ 0, 240, 1, 36, 0.9f }); // kit kick (channel 1)
  3334. song.patterns[0].notes.push_back ({ 240, 240, 1, 38, 0.8f }); // snare
  3335. song.patterns[0].notes.push_back ({ 480, 240, 1, 50, 0.8f }); // key without a pad: dropped
  3336. song.patterns[0].notes.push_back ({ 480, 240, 3, 60, 0.8f }); // other channel: shifts by 1 position
  3337. lynx::Automation a;
  3338. a.target = { lynx::AutomationTargetKind::channelVolume, 3, 0, 0 };
  3339. song.automations.push_back (a);
  3340. std::vector<lynx::DrumPad> pads (2);
  3341. pads[0].name = "Kick";
  3342. pads[0].key = 36;
  3343. pads[0].layers = { { "kick.wav" } };
  3344. pads[1].name = "Snare";
  3345. pads[1].key = 38;
  3346. pads[1].layers = { { "snare soft.wav", 60, 0, 127, 0, 90 }, { "snare hard.wav", 60, 0, 127, 91, 127 } };
  3347. pads[1].cutGroup = 2;
  3348. pads[1].cutBy = 2;
  3349. lynx::splitDrumChannel (song, 1, pads);
  3350. CHECK ((int) song.channels.size() == channels + 1);
  3351. CHECK (song.channels[1].samplePath == "kick.wav" && song.channels[1].instrument == lynx::InstrumentKind::sampler);
  3352. CHECK (song.channels[2].zones.size() == 2 && song.channels[2].cutGroup == song.channels[2].cutBy && song.channels[2].cutGroup != 0);
  3353. const auto& notes = song.patterns[0].notes;
  3354. CHECK (notes.size() == 3);
  3355. CHECK (notes[0].channel == 1 && notes[0].key == 60);
  3356. CHECK (notes[1].channel == 2 && notes[1].key == 60);
  3357. CHECK (notes[2].channel == 4); // the old channel 3
  3358. CHECK (song.automations[0].target.index == 4);
  3359. }
  3360. TEST (flp_import_brings_playlist)
  3361. {
  3362. flp::Project p;
  3363. p.header.ppq = 96;
  3364. p.channels = { { 0, flp::ChannelType::sampler, "Kick", "", "", 1 } };
  3365. flp::Pattern empty;
  3366. empty.index = 1;
  3367. flp::Pattern full;
  3368. full.index = 2;
  3369. flp::Note n;
  3370. n.channel = 0;
  3371. full.notes.push_back (n);
  3372. p.patterns = { empty, full };
  3373. flp::Arrangement a;
  3374. flp::PlaylistItem item;
  3375. item.kind = flp::PlaylistItem::Kind::pattern;
  3376. item.index = 2;
  3377. item.position = 384;
  3378. item.length = 192;
  3379. item.track = 3;
  3380. item.patternStartTick = 96;
  3381. a.items.push_back (item);
  3382. item.index = 1; // empty pattern: discarded
  3383. a.items.push_back (item);
  3384. item.kind = flp::PlaylistItem::Kind::channelClip; // audio: not supported yet
  3385. a.items.push_back (item);
  3386. p.arrangements.push_back (a);
  3387. const auto s = lynx::importFlp (p);
  3388. CHECK (s.mode == lynx::PlayMode::song);
  3389. CHECK (s.playlist.size() == 1);
  3390. CHECK (s.playlist[0].pattern == 0 && s.playlist[0].track == 3);
  3391. CHECK (s.playlist[0].start == 4 * lynx::ppq && s.playlist[0].length == 2 * lynx::ppq && s.playlist[0].offset == lynx::ppq);
  3392. }
  3393. TEST (rack_releases_notes_on_position_jump)
  3394. {
  3395. lynx::Song s;
  3396. lynx::ChannelDef c;
  3397. c.instrument = lynx::InstrumentKind::synth;
  3398. s.channels.push_back (c);
  3399. auto& p = s.patterns.emplace_back();
  3400. lynx::Note n;
  3401. n.start = 0;
  3402. n.length = 4 * lynx::ppq; // long note
  3403. p.notes.push_back (n);
  3404. s.bpm = 120.0;
  3405. auto state = lynx::RackState::build (s, nullptr, 48000.0);
  3406. lynx::RackRenderer r;
  3407. r.prepare (48000.0);
  3408. std::vector<float> l (512), rr (512);
  3409. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  3410. double tick = 0.0;
  3411. for (int i = 0; i < 20; ++i, tick += 512 * tps)
  3412. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  3413. // jump to tick 2000 (mid-note, no new "on"): the synth must go silent
  3414. for (int i = 0; i < 40; ++i)
  3415. r.process (state.get(), true, 2000.0 + i * 512 * tps, 512, l.data(), rr.data());
  3416. float tail = 0.0f;
  3417. for (auto v : l)
  3418. tail = std::max (tail, std::abs (v));
  3419. CHECK (tail < 0.001f);
  3420. }
  3421. namespace
  3422. {
  3423. // Two samplers with a constant 0.5 signal on different inserts; returns the output peak.
  3424. float juce_db (float gain)
  3425. {
  3426. return 20.0f * std::log10 (gain);
  3427. }
  3428. float renderMixerCase (const std::function<void (lynx::Song&)>& tweak, lynx::MeterBank* meters = nullptr)
  3429. {
  3430. lynx::Song s;
  3431. s.mixer = lynx::Song::defaultMixer (4);
  3432. for (int i = 0; i < 2; ++i)
  3433. {
  3434. lynx::ChannelDef c;
  3435. c.instrument = lynx::InstrumentKind::sampler;
  3436. c.samplePath = "dc";
  3437. c.volume = 1.0f;
  3438. c.mixerTrack = i + 1;
  3439. s.channels.push_back (c);
  3440. }
  3441. s.patterns.emplace_back();
  3442. tweak (s);
  3443. auto provider = [] (const std::string&) {
  3444. auto d = std::make_shared<lynx::SampleData>();
  3445. d->left.assign (48000, 0.5f);
  3446. return std::shared_ptr<const lynx::SampleData> (d);
  3447. };
  3448. auto state = lynx::RackState::build (s, nullptr, 48000.0, provider);
  3449. lynx::RackRenderer r;
  3450. r.prepare (48000.0, 256);
  3451. lynx::LiveEventQueue live;
  3452. for (int ch = 0; ch < 2; ++ch)
  3453. {
  3454. lynx::NoteEvent on;
  3455. on.channel = ch;
  3456. on.velocity = 1.0f;
  3457. live.push (on);
  3458. }
  3459. std::vector<float> l (1000), rr (1000); // larger than the mixer block: tests the chunking
  3460. r.process (state.get(), false, 0.0, 1000, l.data(), rr.data(), &live, meters);
  3461. return std::max (std::abs (l[999]), std::abs (rr[999]));
  3462. }
  3463. } // namespace
  3464. TEST (mixer_routes_mutes_solos_and_meters)
  3465. {
  3466. // two 0.5 channels summed on the Master at 0 dB
  3467. CHECK (std::abs (renderMixerCase ([] (lynx::Song&) {}) - 1.0f) < 1e-4f);
  3468. // insert 1 muted: only channel 2 remains
  3469. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.mixer[1].muted = true; }) - 0.5f) < 1e-4f);
  3470. // solo on insert 2: same
  3471. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.mixer[2].solo = true; }) - 0.5f) < 1e-4f);
  3472. // FL fader curve: 0.8 = 0 dB, max ≈ +5.6 dB, half travel ≈ -12.7 dB
  3473. CHECK (std::abs (lynx::faderGain (0.8f) - 1.0f) < 1e-5f);
  3474. CHECK (std::abs (juce_db (lynx::faderGain (1.0f)) - 5.59f) < 0.05f);
  3475. CHECK (lynx::faderGain (0.0f) == 0.0f);
  3476. // Master fader at max
  3477. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.mixer[0].volume = 1.0f; }) - lynx::faderGain (1.0f)) < 1e-4f);
  3478. // both channels on the same insert, insert fader at 0.4
  3479. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) {
  3480. s.channels[1].mixerTrack = 1;
  3481. s.mixer[1].volume = 0.4f;
  3482. }) - lynx::faderGain (0.4f)) < 1e-4f);
  3483. // a route outside the mixer falls to the last existing insert, without crashing
  3484. CHECK (renderMixerCase ([] (lynx::Song& s) { s.channels[0].mixerTrack = 100; }) > 0.9f);
  3485. // no mixer: everything straight to the output at 0 dB
  3486. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.mixer.clear(); }) - 1.0f) < 1e-4f);
  3487. lynx::MeterBank meters;
  3488. renderMixerCase ([] (lynx::Song&) {}, &meters);
  3489. CHECK (std::abs (meters.takePeak (1, 0) - 0.5f) < 1e-4f);
  3490. CHECK (std::abs (meters.takePeak (0, 1) - 1.0f) < 1e-4f);
  3491. CHECK (meters.takePeak (1, 0) == 0.0f); // reading resets
  3492. CHECK (meters.takePeak (3, 0) == 0.0f); // insert with no channel
  3493. }
  3494. TEST (next_free_insert_and_flp_mixer_import)
  3495. {
  3496. auto s = lynx::Song::makeDefault();
  3497. CHECK (s.mixer.size() == 17 && s.mixer[0].name == "Master");
  3498. CHECK (s.nextFreeInsert() == 6); // the 5 default channels take 1..5
  3499. flp::Project p;
  3500. p.channels = { { 0, flp::ChannelType::sampler, "Kick", "", "", 30 } };
  3501. p.mixer = { { 0, "" }, { 30, "Drums" } };
  3502. const auto song = lynx::importFlp (p);
  3503. CHECK (song.channels[0].mixerTrack == 30);
  3504. CHECK (song.mixer.size() == 31);
  3505. CHECK (song.mixer[30].name == "Drums" && song.mixer[0].name == "Master");
  3506. }
  3507. namespace
  3508. {
  3509. float runEffectOnSine (lynx::EffectKind kind, const std::function<void (lynx::EffectSlot&)>& tweak, double hz, float amp,
  3510. int frames = 48000)
  3511. {
  3512. auto fx = lynx::makeEffect (kind);
  3513. fx->prepare (48000.0);
  3514. auto slot = lynx::makeEffectSlot (kind);
  3515. tweak (slot);
  3516. std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
  3517. for (int i = 0; i < frames; ++i)
  3518. l[(std::size_t) i] = r[(std::size_t) i] = amp * (float) std::sin (6.283185307 * hz * i / 48000.0);
  3519. for (int i = 0; i < frames; i += 480)
  3520. fx->process (l.data() + i, r.data() + i, std::min (480, frames - i), slot.params, 120.0);
  3521. float peak = 0.0f;
  3522. for (int i = frames / 2; i < frames; ++i) // second half: already settled
  3523. {
  3524. CHECK (std::isfinite (l[(std::size_t) i]));
  3525. peak = std::max (peak, std::abs (l[(std::size_t) i]));
  3526. }
  3527. return peak;
  3528. }
  3529. } // namespace
  3530. TEST (parametric_eq_bands_and_response)
  3531. {
  3532. // default: transparent
  3533. auto slot = lynx::makeEffectSlot (lynx::EffectKind::parametricEq);
  3534. CHECK (std::abs (lynx::peq::responseDb (slot.params, 48000.0, 1000.0)) < 0.01);
  3535. CHECK (std::abs (runEffectOnSine (lynx::EffectKind::parametricEq, [] (auto&) {}, 1000.0, 0.5f) - 0.5f) < 0.005f);
  3536. // +12 dB peak at 1 kHz on band 4: the response and the signal agree
  3537. auto boost = [] (lynx::EffectSlot& s) {
  3538. s.params[(std::size_t) lynx::peq::param (3, 1)] = lynx::peq::hzValue (1000.0);
  3539. s.params[(std::size_t) lynx::peq::param (3, 2)] = lynx::peq::gainValue (12.0);
  3540. };
  3541. boost (slot);
  3542. CHECK (std::abs (lynx::peq::responseDb (slot.params, 48000.0, 1000.0) - 12.0) < 0.2);
  3543. CHECK (std::abs (lynx::peq::responseDb (slot.params, 48000.0, 100.0)) < 1.0);
  3544. const auto peak = runEffectOnSine (lynx::EffectKind::parametricEq, boost, 1000.0, 0.1f);
  3545. CHECK (std::abs (peak - 0.1f * std::pow (10.0f, 12.0f / 20.0f)) < 0.01f);
  3546. // low cut at 48 dB/oct at 200 Hz: 50 Hz (2 octaves below) drops ~96 dB; 2 kHz passes
  3547. auto cut = lynx::makeEffectSlot (lynx::EffectKind::parametricEq);
  3548. cut.params[(std::size_t) lynx::peq::param (0, 0)] = lynx::peq::typeValue (lynx::peq::BandType::lowCut);
  3549. cut.params[(std::size_t) lynx::peq::param (0, 1)] = lynx::peq::hzValue (200.0);
  3550. cut.params[(std::size_t) lynx::peq::param (0, 3)] = lynx::peq::qValue (0.70710678); // Butterworth
  3551. cut.params[(std::size_t) (lynx::peq::slopeParam + 0)] = 1.0f;
  3552. CHECK (lynx::peq::responseDb (cut.params, 48000.0, 50.0) < -80.0);
  3553. CHECK (std::abs (lynx::peq::responseDb (cut.params, 48000.0, 200.0) + 3.0) < 0.5); // -3 dB at the cutoff (Butterworth)
  3554. CHECK (std::abs (lynx::peq::responseDb (cut.params, 48000.0, 2000.0)) < 0.1);
  3555. // round-trip conversions
  3556. CHECK (std::abs (lynx::peq::hz (lynx::peq::hzValue (440.0)) - 440.0) < 0.5);
  3557. CHECK (std::abs (lynx::peq::q (lynx::peq::qValue (2.5)) - 2.5) < 0.01);
  3558. CHECK (lynx::peq::type (lynx::peq::typeValue (lynx::peq::BandType::notch)) == lynx::peq::BandType::notch);
  3559. }
  3560. TEST (fl_parametric_eq2_state_maps_to_bands)
  3561. {
  3562. // v8 blob: band 1 low cut at 100 Hz "Steep 8", band 4 peak +6 dB at 1 kHz, output −3 dB
  3563. std::vector<std::uint8_t> blob (354, 0);
  3564. auto put = [&] (std::size_t off, std::int32_t v) { std::memcpy (blob.data() + off, &v, 4); };
  3565. put (0, 8);
  3566. auto param = [&] (int i, std::int32_t v) { put (4 + 4 * (std::size_t) i, v); };
  3567. auto hzRaw = [] (double hz) { return (std::int32_t) std::lround (std::log (hz / 20.0) / std::log (1000.0) * 65536.0); };
  3568. for (int b = 0; b < 7; ++b)
  3569. param (21 + b, 0); // all off
  3570. param (21 + 0, 3); // high pass (= low cut)
  3571. param (7 + 0, hzRaw (100.0));
  3572. param (14 + 0, 40124); // default width -> Q ~0.68
  3573. param (28 + 0, -3); // Steep 8 -> 4 biquads
  3574. param (21 + 3, 6); // peaking
  3575. param (7 + 3, hzRaw (1000.0));
  3576. param (3, 600); // +6 dB
  3577. param (14 + 3, 25412);
  3578. param (35, -300); // main level −3 dB
  3579. put (0x13E, 0x7F);
  3580. const auto slot = lynx::effectFromFl ("Fruity Parametric EQ 2", blob);
  3581. CHECK (slot.has_value() && slot->kind == lynx::EffectKind::parametricEq);
  3582. const auto& p = slot->params;
  3583. CHECK (lynx::peq::type (p[(std::size_t) lynx::peq::param (0, 0)]) == lynx::peq::BandType::lowCut);
  3584. CHECK (lynx::peq::flLaw (p) && lynx::peqfl::fromParams (p).order[0] == -3); // FL's own order (REA_rodada50)
  3585. CHECK (lynx::peq::type (p[(std::size_t) lynx::peq::param (3, 0)]) == lynx::peq::BandType::peak);
  3586. CHECK (lynx::peq::type (p[(std::size_t) lynx::peq::param (1, 0)]) == lynx::peq::BandType::off);
  3587. CHECK (std::abs (lynx::peq::responseDb (p, 48000.0, 1000.0) - 3.0) < 0.3); // +6 from the peak −3 from the output
  3588. CHECK (lynx::peq::responseDb (p, 48000.0, 30.0) < -40.0); // 48 dB/oct below 100 Hz
  3589. CHECK (std::abs (lynx::peq::responseDb (p, 48000.0, 8000.0) + 3.0) < 0.3);
  3590. // a band muted by the mask doesn't sound (FL's mask travels with the slot, REA_rodada50)
  3591. put (0x13E, 0x7F & ~(1 << 3));
  3592. const auto muted = lynx::effectFromFl ("Fruity Parametric EQ 2", blob);
  3593. CHECK ((lynx::peqfl::fromParams (muted->params).mask & (1u << 3)) == 0);
  3594. CHECK (std::abs (lynx::peq::responseDb (muted->params, 48000.0, 1000.0) + 3.0) < 0.3);
  3595. }
  3596. TEST (reverb_low_cut_and_predelay)
  3597. {
  3598. // impulse: with 100 ms predelay the wet only starts later; with a 3 kHz low cut a
  3599. // 100 Hz sine barely enters the reverb
  3600. auto run = [] (const std::function<void (lynx::EffectSlot&)>& tweak, double hz, bool impulse) {
  3601. auto fx = lynx::makeEffect (lynx::EffectKind::reverb);
  3602. fx->prepare (48000.0);
  3603. auto slot = lynx::makeEffectSlot (lynx::EffectKind::reverb);
  3604. slot.params[4] = 0.0f; // wet only
  3605. slot.params[3] = 1.0f;
  3606. tweak (slot);
  3607. std::vector<float> l (48000), r (48000);
  3608. for (int i = 0; i < 48000; ++i)
  3609. 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);
  3610. for (int i = 0; i < 48000; i += 480)
  3611. fx->process (l.data() + i, r.data() + i, 480, slot.params, 120.0);
  3612. return l;
  3613. };
  3614. const auto delayed = run ([] (auto& s) { s.params[7] = 0.1f; }, 0.0, true);
  3615. float early = 0.0f, later = 0.0f;
  3616. for (std::size_t i = 0; i < 4800; ++i)
  3617. early = std::max (early, std::abs (delayed[i]));
  3618. for (std::size_t i = 4800; i < 24000; ++i)
  3619. later = std::max (later, std::abs (delayed[i]));
  3620. CHECK (early == 0.0f && later > 0.0f);
  3621. 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; };
  3622. const auto open = energy (run ([] (auto&) {}, 100.0, false));
  3623. const auto cut = energy (run ([] (auto& s) { s.params[5] = 1.0f; }, 100.0, false));
  3624. CHECK (cut < open * 0.01);
  3625. }
  3626. TEST (utility_effect_and_fl_stereo_tools)
  3627. {
  3628. // transparent by default; balance fully right silences the left
  3629. CHECK (std::abs (runEffectOnSine (lynx::EffectKind::utility, [] (auto&) {}, 440.0, 0.5f) - 0.5f) < 0.001f);
  3630. CHECK (runEffectOnSine (lynx::EffectKind::utility, [] (auto& s) { s.params[1] = 1.0f; }, 440.0, 0.5f) < 0.001f);
  3631. // Stereo Shaper at half the knob range (6400): each gain = (3^0.5 − 1)/2 (FL's knob law, REA_rodada54 §2.2)
  3632. std::vector<std::uint8_t> blob (36, 0);
  3633. auto put = [&] (std::size_t off, std::int32_t v) { std::memcpy (blob.data() + off, &v, 4); };
  3634. put (4, 6400); put (8, 6400); put (12, 6400); put (16, 6400);
  3635. const auto slot = lynx::effectFromFl ("Fruity Stereo Shaper", blob);
  3636. CHECK (slot && slot->kind == lynx::EffectKind::stereoShaper);
  3637. auto shaperFx = lynx::makeEffect (*slot);
  3638. shaperFx->prepare (44100.0);
  3639. float zl = 0.0f, zr = 0.0f;
  3640. shaperFx->process (&zl, &zr, 0, slot->params, 120.0); // takes the controls
  3641. shaperFx->reset(); // FL's Flush: the gains jump to their targets
  3642. float l = 1.0f, r = 0.0f;
  3643. shaperFx->process (&l, &r, 1, slot->params, 120.0);
  3644. const auto half = (float) ((std::sqrt (3.0) - 1.0) / 2.0);
  3645. CHECK (std::abs (l - half) < 1e-4f && std::abs (r - half) < 1e-4f);
  3646. auto fx = lynx::makeEffect (lynx::EffectKind::utility);
  3647. // Balance: volume 256 = 0 dB, pan −128 = fully left
  3648. std::vector<std::uint8_t> bal (8, 0);
  3649. std::int32_t pan = -128, vol = 256;
  3650. std::memcpy (bal.data(), &pan, 4);
  3651. std::memcpy (bal.data() + 4, &vol, 4);
  3652. const auto b = lynx::effectFromFl ("Fruity Balance", bal);
  3653. CHECK (b && b->kind == lynx::EffectKind::fruityBalance); // FL's exact law (REA_rodada58 §2): fully left sums R into L
  3654. if (b)
  3655. {
  3656. auto balance = lynx::makeEffect (*b);
  3657. balance->prepare (44100.0);
  3658. l = 0.5f; r = 0.5f;
  3659. balance->process (&l, &r, 1, b->params, 120.0);
  3660. CHECK (std::abs (l - 0.70710678f) < 1e-6f && r == 0.0f);
  3661. }
  3662. }
  3663. TEST (effects_eq_compressor_behave)
  3664. {
  3665. // EQ at default is transparent
  3666. CHECK (std::abs (runEffectOnSine (lynx::EffectKind::eq, [] (auto&) {}, 1000.0, 0.5f) - 0.5f) < 0.01f);
  3667. // +18 dB on the lows: 60 Hz rises a lot, 5 kHz barely
  3668. CHECK (runEffectOnSine (lynx::EffectKind::eq, [] (auto& s) { s.params[0] = 1.0f; }, 60.0, 0.05f) > 0.3f);
  3669. CHECK (runEffectOnSine (lynx::EffectKind::eq, [] (auto& s) { s.params[0] = 1.0f; }, 5000.0, 0.05f) < 0.06f);
  3670. // compressor: threshold -20 dB, ratio 20:1 flattens a 0 dBFS signal to near the threshold
  3671. const auto squashed = runEffectOnSine (lynx::EffectKind::compressor, [] (auto& s) {
  3672. s.params[0] = 40.0f / 60.0f;
  3673. s.params[1] = 1.0f;
  3674. }, 200.0, 1.0f);
  3675. CHECK (squashed < 0.2f && squashed > 0.05f);
  3676. // a signal below the threshold passes intact
  3677. CHECK (std::abs (runEffectOnSine (lynx::EffectKind::compressor, [] (auto& s) { s.params[0] = 1.0f; }, 200.0, 0.3f) - 0.3f) < 0.01f);
  3678. }
  3679. TEST (effects_delay_echoes_on_tempo_and_reverb_has_tail)
  3680. {
  3681. auto delay = lynx::makeEffect (lynx::EffectKind::delay);
  3682. delay->prepare (48000.0);
  3683. auto slot = lynx::makeEffectSlot (lynx::EffectKind::delay);
  3684. slot.params[0] = 3.5f / 8.0f; // index 3 = 1/4 = 0.5 s at 120 BPM
  3685. slot.params[1] = 0.0f; // no feedback: a single echo
  3686. slot.params[2] = 1.0f;
  3687. slot.params[4] = 0.0f; // no damping
  3688. std::vector<float> l (48000, 0.0f), r (48000, 0.0f);
  3689. l[0] = r[0] = 1.0f;
  3690. delay->process (l.data(), r.data(), 48000, slot.params, 120.0);
  3691. CHECK (std::abs (l[24000] - 1.0f) < 1e-4f);
  3692. CHECK (std::abs (l[12000]) < 1e-6f);
  3693. auto reverb = lynx::makeEffect (lynx::EffectKind::reverb);
  3694. reverb->prepare (48000.0);
  3695. auto rs = lynx::makeEffectSlot (lynx::EffectKind::reverb);
  3696. rs.params[3] = 1.0f;
  3697. std::fill (l.begin(), l.end(), 0.0f);
  3698. std::fill (r.begin(), r.end(), 0.0f);
  3699. l[0] = r[0] = 1.0f;
  3700. reverb->process (l.data(), r.data(), 48000, rs.params, 120.0);
  3701. float tail = 0.0f;
  3702. for (int i = 4800; i < 9600; ++i) // between 100 and 200 ms after the impulse
  3703. tail = std::max (tail, std::abs (l[(std::size_t) i]));
  3704. CHECK (tail > 1e-4f && tail < 1.0f);
  3705. CHECK (lynx::formatEffectParam (lynx::EffectKind::delay, 0, slot.params[0], 120.0) == "1/4 (500 ms)");
  3706. }
  3707. TEST (rack_runs_insert_effects_and_reuses_them)
  3708. {
  3709. // an insert with EQ at -inf doesn't exist; we use the EQ output at -18 dB to see the effect act
  3710. const auto quiet = renderMixerCase ([] (lynx::Song& s) {
  3711. auto eq = lynx::makeEffectSlot (lynx::EffectKind::eq);
  3712. eq.params[4] = 0.0f; // output -18 dB
  3713. s.mixer[1].effects.push_back (eq);
  3714. });
  3715. CHECK (quiet > 0.55f && quiet < 0.6f); // 0.5 from channel 2 + 0.5 * 0.126 from channel 1
  3716. const auto bypassed = renderMixerCase ([] (lynx::Song& s) {
  3717. auto eq = lynx::makeEffectSlot (lynx::EffectKind::eq);
  3718. eq.params[4] = 0.0f;
  3719. eq.enabled = false;
  3720. s.mixer[1].effects.push_back (eq);
  3721. });
  3722. CHECK (std::abs (bypassed - 1.0f) < 1e-4f);
  3723. auto s = lynx::Song::makeDefault();
  3724. s.mixer[2].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::reverb));
  3725. auto a = lynx::RackState::build (s, nullptr, 48000.0);
  3726. s.mixer[2].effects[0].params[0] = 0.9f; // turning a knob doesn't recreate the effect
  3727. auto b = lynx::RackState::build (s, a.get(), 48000.0);
  3728. CHECK (b->effects[2][0] == a->effects[2][0]);
  3729. s.mixer[2].effects[0] = lynx::makeEffectSlot (lynx::EffectKind::delay);
  3730. auto c = lynx::RackState::build (s, b.get(), 48000.0);
  3731. CHECK (c->effects[2][0] != b->effects[2][0]);
  3732. }
  3733. TEST (audio_clips_trigger_cut_and_resume)
  3734. {
  3735. lynx::Song s;
  3736. s.bpm = 120.0; // 1 beat = 0.5 s = 24000 samples at 48 kHz
  3737. lynx::ChannelDef c;
  3738. c.instrument = lynx::InstrumentKind::audioClip;
  3739. c.samplePath = "ramp";
  3740. c.volume = 1.0f;
  3741. s.channels.push_back (c);
  3742. s.patterns.emplace_back();
  3743. lynx::Clip clip;
  3744. clip.kind = lynx::ClipKind::audio;
  3745. clip.channel = 0;
  3746. clip.start = lynx::ppq; // starts at beat 2
  3747. clip.length = 2 * lynx::ppq; // lasts 2 beats
  3748. clip.offset = lynx::ppq; // skips the first beat of the audio
  3749. s.playlist.push_back (clip);
  3750. s.mode = lynx::PlayMode::song;
  3751. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  3752. lynx::EventBuffer ev;
  3753. lynx::scheduleSong (s, 0.0, 4 * 24000, tps, ev);
  3754. CHECK (ev.size() == 2);
  3755. CHECK (ev[0].type == lynx::NoteEvent::Type::on && ev[0].sampleOffset == 24000 && std::abs (ev[0].startSeconds - 0.5) < 1e-9);
  3756. CHECK (ev[1].type == lynx::NoteEvent::Type::off && ev[1].sampleOffset == 3 * 24000);
  3757. // seek to the middle of the clip (beat 2.5): with resume, starts at the block's first sample
  3758. ev.clear();
  3759. lynx::scheduleSong (s, 1.5 * lynx::ppq, 256, tps, ev, true);
  3760. CHECK (ev.size() == 1 && ev[0].sampleOffset == 0 && std::abs (ev[0].startSeconds - 0.75) < 1e-9);
  3761. ev.clear();
  3762. lynx::scheduleSong (s, 1.5 * lynx::ppq, 256, tps, ev, false);
  3763. CHECK (ev.size() == 0);
  3764. // sample whose value = position in seconds: lets us check where playback started
  3765. auto provider = [] (const std::string&) {
  3766. auto d = std::make_shared<lynx::SampleData>();
  3767. d->sampleRate = 48000.0;
  3768. for (int i = 0; i < 48000 * 3; ++i)
  3769. d->left.push_back ((float) i / 48000.0f);
  3770. return std::shared_ptr<const lynx::SampleData> (d);
  3771. };
  3772. auto state = lynx::RackState::build (s, nullptr, 48000.0, provider);
  3773. lynx::RackRenderer r;
  3774. r.prepare (48000.0, 512);
  3775. std::vector<float> l (256), rr (256);
  3776. // first block already playing mid-clip
  3777. r.process (state.get(), true, 1.5 * lynx::ppq, 256, l.data(), rr.data());
  3778. CHECK (std::abs (l[0] - 0.75f) < 0.001f);
  3779. // the clip ends at beat 3: after the short fade, silence (the sample would still have audio)
  3780. double tick = 3.0 * lynx::ppq - 10 * tps;
  3781. r.process (state.get(), false, tick, 256, l.data(), rr.data()); // stops (fade)
  3782. for (int i = 0; i < 4; ++i)
  3783. r.process (state.get(), false, tick, 256, l.data(), rr.data());
  3784. CHECK (std::abs (l[255]) < 1e-6f);
  3785. }
  3786. TEST (remove_channel_fixes_notes_and_audio_clips)
  3787. {
  3788. lynx::Song s;
  3789. s.channels.resize (3);
  3790. s.patterns.resize (1);
  3791. s.patterns[0].notes = { { 0, 240, 0, 60, 1.0f }, { 0, 240, 1, 60, 1.0f }, { 0, 240, 2, 60, 1.0f } };
  3792. lynx::Clip a;
  3793. a.kind = lynx::ClipKind::audio;
  3794. a.channel = 1;
  3795. a.length = 100;
  3796. lynx::Clip b = a;
  3797. b.channel = 2;
  3798. s.playlist = { a, b };
  3799. s.removeChannel (1);
  3800. CHECK (s.channels.size() == 2);
  3801. CHECK (s.patterns[0].notes.size() == 2 && s.patterns[0].notes[1].channel == 1);
  3802. CHECK (s.playlist.size() == 1 && s.playlist[0].channel == 1);
  3803. }
  3804. TEST (flp_import_audio_clips_with_offsets)
  3805. {
  3806. flp::Project p;
  3807. p.header.ppq = 96;
  3808. p.tempo = 120.0;
  3809. p.channels = { { 0, flp::ChannelType::audioClip, "Vocal", "", "%FLStudioData%\\v.wav", 2 },
  3810. { 1, flp::ChannelType::audioClip, "Loop", "", "%FLStudioData%\\l.wav", 3 } };
  3811. flp::Arrangement arr;
  3812. flp::PlaylistItem beats; // offset in beats: end - start = duration in beats
  3813. beats.kind = flp::PlaylistItem::Kind::channelClip;
  3814. beats.index = 0;
  3815. beats.position = 192;
  3816. beats.length = 384; // 4 beats
  3817. beats.startOffset = 2.0f;
  3818. beats.endOffset = 6.0f;
  3819. flp::PlaylistItem ms = beats; // offset in ms: 4 beats at 120 BPM = 2000 ms
  3820. ms.index = 1;
  3821. ms.startOffset = 500.0f;
  3822. ms.endOffset = 2500.0f;
  3823. arr.items = { beats, ms };
  3824. p.arrangements.push_back (arr);
  3825. lynx::FlpImportOptions options;
  3826. options.roots = { "C:\\FL", "C:\\FL\\Data", "" };
  3827. options.fileExists = [] (const std::string&) { return true; };
  3828. const auto s = lynx::importFlp (p, options);
  3829. CHECK (s.channels.size() == 2 && s.channels[0].instrument == lynx::InstrumentKind::audioClip);
  3830. CHECK (s.channels[0].samplePath == "C:\\FL\\Data\\v.wav");
  3831. CHECK (s.playlist.size() == 2);
  3832. CHECK (s.playlist[0].kind == lynx::ClipKind::audio && s.playlist[0].start == 2 * lynx::ppq);
  3833. CHECK (s.playlist[0].offset == 2 * lynx::ppq);
  3834. CHECK (s.playlist[1].offset == lynx::ppq); // 500 ms at 120 BPM = 1 beat
  3835. CHECK (s.mode == lynx::PlayMode::song);
  3836. }
  3837. TEST (plugin_factories_create_reuse_and_fallback)
  3838. {
  3839. lynx::Song s;
  3840. s.mixer = lynx::Song::defaultMixer (2);
  3841. lynx::ChannelDef c;
  3842. c.instrument = lynx::InstrumentKind::plugin;
  3843. c.plugin.id = "VST3-Synth-123";
  3844. c.mixerTrack = 1;
  3845. s.channels.push_back (c);
  3846. lynx::ChannelDef missing = c;
  3847. missing.plugin.id = "VST3-Ausente-0";
  3848. s.channels.push_back (missing);
  3849. lynx::EffectSlot fx;
  3850. fx.kind = lynx::EffectKind::plugin;
  3851. fx.plugin.id = "VST3-Reverb-9";
  3852. s.mixer[1].effects.push_back (fx);
  3853. s.patterns.emplace_back();
  3854. int instrumentsMade = 0, effectsMade = 0;
  3855. lynx::RackState::Factories f;
  3856. f.pluginInstruments = [&] (const lynx::PluginRef& ref, double) -> std::shared_ptr<lynx::Instrument> {
  3857. if (ref.id != "VST3-Synth-123")
  3858. return nullptr;
  3859. ++instrumentsMade;
  3860. auto synth = lynx::makeInstrument (lynx::InstrumentKind::synth);
  3861. synth->prepare (48000.0);
  3862. return synth;
  3863. };
  3864. f.pluginEffects = [&] (const lynx::PluginRef&, double) -> std::shared_ptr<lynx::Effect> {
  3865. ++effectsMade;
  3866. auto eq = lynx::makeEffect (lynx::EffectKind::eq);
  3867. eq->prepare (48000.0);
  3868. return eq;
  3869. };
  3870. auto a = lynx::RackState::build (s, nullptr, 48000.0, f);
  3871. CHECK (instrumentsMade == 1 && effectsMade == 1);
  3872. CHECK (a->instruments.size() == 2 && a->instruments[1] != nullptr); // missing: muted, but it exists
  3873. // changing the saved state doesn't recreate; swapping the plugin does
  3874. s.channels[0].plugin.state = "abc";
  3875. auto b = lynx::RackState::build (s, a.get(), 48000.0, f);
  3876. CHECK (instrumentsMade == 1 && b->instruments[0] == a->instruments[0] && b->effects[1][0] == a->effects[1][0]);
  3877. s.mixer[1].effects[0].plugin.id = "VST3-Outro-1";
  3878. auto c2 = lynx::RackState::build (s, b.get(), 48000.0, f);
  3879. CHECK (effectsMade == 2);
  3880. // rendering with a missing plugin and a plugin slot breaks nothing
  3881. lynx::RackRenderer r;
  3882. r.prepare (48000.0, 256);
  3883. lynx::LiveEventQueue live;
  3884. lynx::NoteEvent on;
  3885. on.channel = 0;
  3886. on.velocity = 1.0f;
  3887. live.push (on);
  3888. on.channel = 1;
  3889. live.push (on);
  3890. std::vector<float> l (256), rr (256);
  3891. r.process (c2.get(), false, 0.0, 256, l.data(), rr.data(), &live);
  3892. float peak = 0.0f;
  3893. for (auto v : l)
  3894. {
  3895. CHECK (std::isfinite (v));
  3896. peak = std::max (peak, std::abs (v));
  3897. }
  3898. CHECK (peak > 0.0f);
  3899. }
  3900. TEST (export_song_adds_silent_tail_and_never_restarts)
  3901. {
  3902. auto s = lynx::Song::makeDefault();
  3903. s.playlist.clear();
  3904. std::int64_t length = 0;
  3905. auto pat = lynx::makeExportSong (s, true, lynx::ppq * 8, length); // empty Playlist: uses the pattern
  3906. CHECK (length == lynx::ppq * 4);
  3907. CHECK (pat.mode == lynx::PlayMode::song && pat.playlist.size() == 2);
  3908. CHECK (pat.songLength() >= length + lynx::ppq * 8);
  3909. // no new notes after the song ends, only silence until the end of the tail
  3910. lynx::EventBuffer events;
  3911. const auto tps = lynx::ticksPerSample (pat.bpm, 48000.0);
  3912. int onsAfterEnd = 0, onsBefore = 0;
  3913. for (double tick = 0.0; tick < (double) (length + lynx::ppq * 8); tick += 512 * tps)
  3914. {
  3915. events.clear();
  3916. lynx::scheduleSong (pat, tick, 512, tps, events);
  3917. for (const auto& e : events)
  3918. if (e.type == lynx::NoteEvent::Type::on)
  3919. (tick + e.sampleOffset * tps >= (double) length ? onsAfterEnd : onsBefore)++;
  3920. }
  3921. CHECK (onsBefore == (int) s.patterns[0].notes.size());
  3922. CHECK (onsAfterEnd == 0);
  3923. // with Playlist: the end is the last clip's, without rounding
  3924. s.playlist.push_back ({ 0, 0, lynx::ppq * 2, lynx::ppq * 3, 0 });
  3925. auto song = lynx::makeExportSong (s, true, 0, length);
  3926. CHECK (length == lynx::ppq * 5 && song.playlist.size() == 1);
  3927. song = lynx::makeExportSong (s, false, 0, length);
  3928. CHECK (length == lynx::ppq * 4 && song.playlist[0].start == 0);
  3929. }
  3930. TEST (note_edit_selection_operations)
  3931. {
  3932. namespace ne = lynx::noteedit;
  3933. lynx::Pattern p;
  3934. p.notes = { { 0, 240, 0, 60 }, { 480, 240, 0, 64 }, { 960, 480, 1, 67 } };
  3935. // sorting keeps the indices in step
  3936. p.notes[0].start = 1200;
  3937. std::vector<int> tracked { 0, 2 };
  3938. ne::sortTracking (p, tracked);
  3939. CHECK (p.notes[2].start == 1200 && p.notes[(std::size_t) tracked[0]].start == 1200);
  3940. CHECK (p.notes[(std::size_t) tracked[1]].key == 67);
  3941. // group move respects the limits
  3942. std::int64_t dt = -5000;
  3943. int dk = 100;
  3944. ne::move (p, { 0, 1 }, dt, dk);
  3945. CHECK (dt == -480 && dk == 127 - 67 && p.notes[0].start == 0 && p.notes[1].key == 127);
  3946. // duplicate: right after the selection, rounded to the grid
  3947. lynx::Pattern q;
  3948. q.notes = { { 0, 240, 0, 60 }, { 240, 300, 0, 62 } }; // 540-tick selection
  3949. const auto added = ne::duplicate (q, { 0, 1 }, 960);
  3950. CHECK (added.size() == 2 && q.notes[(std::size_t) added[0]].start == 960 && q.notes[(std::size_t) added[1]].start == 1200);
  3951. // paste onto another channel with an offset
  3952. const auto pasted = ne::paste (q, ne::copy (q, { 1 }), 100, 3);
  3953. CHECK (pasted.size() == 1 && q.notes.back().channel == 3 && q.notes.back().start == 340);
  3954. // quantize rounds to the nearest
  3955. q.notes[0].start = 130;
  3956. q.notes[1].start = 100;
  3957. ne::quantize (q, { 0, 1 }, 240);
  3958. CHECK (q.notes[0].start == 240 && q.notes[1].start == 0);
  3959. // remove several (order and duplicates don't matter)
  3960. const auto before = q.notes.size();
  3961. ne::remove (q, { 1, 0, 1 });
  3962. CHECK (q.notes.size() == before - 2);
  3963. }
  3964. TEST (note_edit_quick_operations)
  3965. {
  3966. namespace ne = lynx::noteedit;
  3967. const std::int64_t step = lynx::ticksPerStep, bar = lynx::ppq * 4;
  3968. // split: only strictly inside; the tail is appended and does not slide again
  3969. lynx::Pattern p;
  3970. p.notes = { { 0, 4 * step, 0, 60 } };
  3971. p.notes[0].slide = true;
  3972. CHECK (ne::split (p, 0, 0) == -1 && ne::split (p, 0, 4 * step) == -1);
  3973. CHECK (ne::split (p, 0, step) == 1 && p.notes[0].length == step && p.notes[1].start == step && p.notes[1].length == 3 * step
  3974. && ! p.notes[1].slide);
  3975. // chop to grid: a 2.5-step note from half a step becomes 3 pieces cut on the step lines
  3976. lynx::Pattern c;
  3977. c.notes = { { step / 2, step * 5 / 2, 0, 60 }, { 0, step, 1, 50 } };
  3978. const auto pieces = ne::chop (c, { 0, 1 }, step);
  3979. CHECK (pieces.size() == 4 && c.notes.size() == 4);
  3980. CHECK (c.notes[0].length == step / 2 && c.notes[2].start == step && c.notes[2].length == step && c.notes[3].start == 2 * step
  3981. && c.notes[3].length == step);
  3982. // join: touching and overlapping notes of the same key and channel merge; others stay
  3983. lynx::Pattern j;
  3984. j.notes = { { 0, step, 0, 60 }, { step, step, 0, 60 }, { step * 3 / 2, step * 2, 0, 60 }, { 5 * step, step, 0, 60 },
  3985. { step, step, 0, 62 }, { step, step, 1, 60 } };
  3986. CHECK (ne::join (j, { 0, 1, 2, 3, 4, 5 }) == 2 && j.notes.size() == 4);
  3987. CHECK (j.notes[0].start == 0 && j.notes[0].length == step * 7 / 2);
  3988. // legato: each note reaches the next start (a chord ends together); the last keeps its length
  3989. lynx::Pattern l;
  3990. l.notes = { { 0, 10, 0, 60 }, { 0, 10, 0, 64 }, { step, 10, 0, 62 }, { 3 * step, 10, 0, 65 }, { step, 10, 1, 40 } };
  3991. ne::legato (l, { 0, 1, 2, 3, 4 });
  3992. CHECK (l.notes[0].length == step && l.notes[1].length == step && l.notes[2].length == 2 * step && l.notes[3].length == 10
  3993. && l.notes[4].length == 10);
  3994. // shrink to grid and full quantise (start and end)
  3995. ne::shrinkToGrid (l, { 3 }, step / 2);
  3996. CHECK (l.notes[3].length == step / 2);
  3997. lynx::Pattern q;
  3998. q.notes = { { 10, step - 30, 0, 60 }, { step + 100, 20, 0, 60 } };
  3999. ne::quantizeFull (q, { 0, 1 }, step);
  4000. CHECK (q.notes[0].start == 0 && q.notes[0].length == step && q.notes[1].start == step && q.notes[1].length == step);
  4001. // rotate inside the bar, wrapping
  4002. lynx::Pattern r;
  4003. r.notes = { { 0, step, 0, 60 }, { bar - step, step, 0, 62 } };
  4004. ne::rotate (r, { 0, 1 }, step, bar, 1);
  4005. CHECK (r.notes[0].start == step && r.notes[1].start == 0);
  4006. ne::rotate (r, { 0, 1 }, step, bar, -2);
  4007. CHECK (r.notes[0].start == bar - step && r.notes[1].start == bar - 2 * step);
  4008. r.notes.push_back ({ bar + step, step, 0, 64 }); // alone in the second bar: wraps inside that bar
  4009. ne::rotate (r, { 2 }, step, bar, -2);
  4010. CHECK (r.notes[2].start == 2 * bar - step);
  4011. }
  4012. TEST (note_edit_grid_and_time_text)
  4013. {
  4014. namespace ne = lynx::noteedit;
  4015. const std::int64_t step = lynx::ticksPerStep, bar = lynx::ppq * 4;
  4016. CHECK (ne::snapDown (250.0, step) == step && ne::snapDown (-1.0, step) == -step && ne::snapDown (17.9, 1) == 17);
  4017. CHECK (ne::snapNearest (1000.0, { 400, 1100, 980 }, 50.0, step) == 980);
  4018. CHECK (ne::snapNearest (1000.0, { 400 }, 50.0, step) == 960); // too far: falls back to the grid
  4019. // screen grid: 24 px per step shows steps; zoomed out it climbs to beats, bars and bar multiples
  4020. CHECK (ne::screenStep (24.0 / step, bar) == step);
  4021. CHECK (ne::screenStep (96.0 / bar, bar) == lynx::ppq);
  4022. CHECK (ne::screenStep (8.0 / bar, bar) == 2 * bar);
  4023. CHECK (ne::screenStep (200.0 / step, bar) == step / 4);
  4024. CHECK (ne::formatTime (0, 4) == "1:1:0" && ne::formatTime (bar + 5 * step + 7, 4) == "2:6:7");
  4025. CHECK (ne::formatTime (step * 2, 4, true) == "0:2:0");
  4026. std::int64_t t = 0;
  4027. CHECK (ne::parseTime ("2:6:7", 4, t) && t == bar + 5 * step + 7);
  4028. CHECK (ne::parseTime ("3", 4, t) && t == 2 * bar);
  4029. CHECK (ne::parseTime ("0:2:0", 4, t, true) && t == 2 * step);
  4030. CHECK (! ne::parseTime ("0:1:0", 4, t) && ! ne::parseTime ("1:17:0", 4, t) && ! ne::parseTime ("1:1:240", 4, t)
  4031. && ! ne::parseTime ("a", 4, t) && ! ne::parseTime ("", 4, t));
  4032. CHECK (ne::parseTime ("1:12:0", 3, t) && ! ne::parseTime ("1:13:0", 3, t)); // 3/4: 12 steps per bar
  4033. }
  4034. TEST (playlist_clip_edges_and_time_selection)
  4035. {
  4036. namespace ne = lynx::noteedit;
  4037. const std::int64_t beat = lynx::ppq;
  4038. // left-edge trim: content stays in place, cannot reveal what is before the content's start
  4039. lynx::Clip c { 0, 0, 4 * beat, 4 * beat, beat };
  4040. ne::trimStart (c, 5 * beat, beat);
  4041. CHECK (c.start == 5 * beat && c.offset == 2 * beat && c.length == 3 * beat);
  4042. ne::trimStart (c, 0, beat);
  4043. CHECK (c.start == 3 * beat && c.offset == 0 && c.length == 5 * beat);
  4044. ne::trimStart (c, 100 * beat, beat);
  4045. CHECK (c.length == beat && c.end() == 8 * beat);
  4046. // slip moves the content, not the clip
  4047. lynx::Clip s { 0, 0, 0, 2 * beat, beat };
  4048. ne::slip (s, beat / 2);
  4049. CHECK (s.offset == beat / 2 && s.start == 0 && s.length == 2 * beat);
  4050. ne::slip (s, beat);
  4051. CHECK (s.offset == 0);
  4052. ne::slip (s, -3 * beat);
  4053. CHECK (s.offset == 3 * beat);
  4054. // insert / delete time across all tracks, markers included
  4055. std::vector<lynx::Clip> clips { { 0, 0, 0, 2 * beat, 0 }, { 0, 1, 4 * beat, 2 * beat, 0 }, { 0, 2, beat, 6 * beat, 0 } };
  4056. std::vector<lynx::Marker> markers { { 5 * beat, "A" }, { beat / 2, "B" } };
  4057. ne::insertTime (clips, markers, 2 * beat, 4 * beat);
  4058. CHECK (clips[0].start == 0 && clips[1].start == 8 * beat && clips[2].start == beat && markers[0].tick == 9 * beat
  4059. && markers[1].tick == beat / 2);
  4060. std::vector<lynx::Clip> d { { 0, 0, 0, 2 * beat, 0 }, // before: kept
  4061. { 0, 1, 3 * beat, beat, 0 }, // inside: deleted
  4062. { 0, 2, beat, 6 * beat, 0 }, // spans the cut: head + tail
  4063. { 0, 3, 3 * beat, 4 * beat, 0 }, // tail outside: trimmed and moved
  4064. { 0, 4, 8 * beat, beat, 0 } }; // after: moved left
  4065. std::vector<lynx::Marker> dm { { 3 * beat, "in" }, { 9 * beat, "after" } };
  4066. ne::deleteTime (d, dm, 2 * beat, 5 * beat);
  4067. CHECK (d.size() == 5);
  4068. CHECK (d[1].track == 2 && d[1].start == beat && d[1].length == beat);
  4069. CHECK (d[2].track == 2 && d[2].start == 2 * beat && d[2].offset == 4 * beat && d[2].length == 2 * beat);
  4070. CHECK (d[3].track == 3 && d[3].start == 2 * beat && d[3].offset == 2 * beat && d[3].length == 2 * beat);
  4071. CHECK (d[4].start == 5 * beat && dm.size() == 1 && dm[0].tick == 6 * beat);
  4072. // time selection: overlapping clips; Ctrl+B repeats what starts inside it after itself
  4073. std::vector<lynx::Clip> r { { 0, 0, 0, beat, 0 }, { 0, 1, beat, 2 * beat, 0 }, { 0, 0, 4 * beat, beat, 0 } };
  4074. CHECK ((ne::clipsInTime (r, beat / 2, 2 * beat) == std::vector<int> { 0, 1 }));
  4075. const auto added = ne::repeatTime (r, 0, 4 * beat, {});
  4076. CHECK (added.size() == 2 && r[3].start == 4 * beat && r[4].start == 5 * beat && r[4].track == 1);
  4077. CHECK (ne::repeatTime (r, 0, 2 * beat, { 2 }).size() == 1 && r.back().start == 6 * beat);
  4078. }
  4079. TEST (recorder_builds_notes_in_pattern_and_song_mode)
  4080. {
  4081. auto song = lynx::Song::makeDefault();
  4082. const auto loop = song.patterns[0].effectiveLength (4); // 1 bar = 3840
  4083. lynx::NoteRecorder r;
  4084. r.add (song, { 100.0, 4, 60, 0.5f, true }); // no take: ignored
  4085. CHECK (r.takeFinished().empty());
  4086. r.begin (loop);
  4087. r.add (song, { 4000.0, 4, 60, 0.5f, true }); // second loop pass
  4088. r.add (song, { 4480.0, 4, 60, 0.0f, false });
  4089. r.add (song, { 5000.0, 4, 64, 0.9f, true }); // held until stop
  4090. r.end (song, 5240.0);
  4091. const auto notes = r.takeFinished();
  4092. CHECK (notes.size() == 2);
  4093. CHECK (notes[0].start == 4000 - 3840 && notes[0].length == 480 && notes[0].velocity == 0.5f && notes[0].channel == 4);
  4094. CHECK (notes[1].key == 64 && notes[1].start == 5000 - 3840 && notes[1].length == 240);
  4095. // song mode: position inside the current pattern's clip (with offset)
  4096. song.mode = lynx::PlayMode::song;
  4097. song.playlist = { { 0, 0, 7680, 3840, 960 } };
  4098. CHECK (lynx::patternPositionForTick (song, 7680.0 + 100.0, loop) == 1060);
  4099. CHECK (lynx::patternPositionForTick (song, 100.0, loop) == 100); // outside the clip: like loop
  4100. }
  4101. TEST (automation_curve_and_playback)
  4102. {
  4103. lynx::Automation a;
  4104. a.points = { { 0, 0.0f }, { 960, 1.0f }, { 1920, 0.5f, 0.0f } };
  4105. CHECK (a.valueAt (-10.0) == 0.0f && a.valueAt (480.0) == 0.5f && a.valueAt (5000.0) == 0.5f);
  4106. CHECK (a.length() == 1920);
  4107. a.points[0].tension = 1.0f; // curve: starts slow
  4108. CHECK (a.valueAt (480.0) < 0.2f && a.valueAt (480.0) > 0.0f);
  4109. // automated Master: silence in the first bar, sound in the second, and the last value
  4110. // keeps applying after the automation clip ends
  4111. auto song = lynx::Song::makeDefault();
  4112. song.bpm = 120.0;
  4113. song.mode = lynx::PlayMode::song;
  4114. const auto bar = (std::int64_t) lynx::ppq * 4;
  4115. for (int b = 0; b < 3; ++b)
  4116. song.playlist.push_back ({ 0, 0, bar * b, bar, 0 });
  4117. lynx::Automation master;
  4118. master.target = { lynx::AutomationTargetKind::mixerVolume, 0 };
  4119. master.points = { { 0, 0.0f }, { bar - 1, 0.0f }, { bar, 0.8f } };
  4120. song.automations.push_back (master);
  4121. lynx::Clip ac;
  4122. ac.kind = lynx::ClipKind::automation;
  4123. ac.automation = 0;
  4124. ac.track = 1;
  4125. ac.length = bar + bar / 2;
  4126. song.playlist.push_back (ac);
  4127. const double sr = 48000.0;
  4128. auto state = lynx::RackState::build (song, nullptr, sr);
  4129. CHECK (state->hasAutomation && state->mixerVolumeAuto[0] == 0 && state->automationClips[0].size() == 1);
  4130. lynx::RackRenderer r;
  4131. r.prepare (sr, 512);
  4132. std::vector<float> l (512), rr (512);
  4133. const auto tps = lynx::ticksPerSample (song.bpm, sr);
  4134. float peakBar1 = 0.0f, peakBar2 = 0.0f, peakBar3 = 0.0f;
  4135. for (double tick = 0.0; tick < (double) (bar * 3); tick += 512 * tps)
  4136. {
  4137. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  4138. auto& peak = tick < (double) bar - 512 * tps ? peakBar1 : tick < (double) bar * 2 ? peakBar2 : peakBar3;
  4139. for (auto v : l)
  4140. peak = std::max (peak, std::abs (v));
  4141. }
  4142. CHECK (peakBar1 < 1.0e-6f);
  4143. CHECK (peakBar2 > 0.05f);
  4144. CHECK (peakBar3 > 0.05f); // after the clip: keeps the last value
  4145. // target removed: automation disappears from the tables
  4146. song.removeChannel (0);
  4147. song.automations[0].target = { lynx::AutomationTargetKind::channelVolume, 0 };
  4148. song.removeChannel (0);
  4149. CHECK (song.automations[0].target.kind == lynx::AutomationTargetKind::none);
  4150. song.removeAutomation (0);
  4151. CHECK (song.automations.empty() && song.playlist.size() == 3);
  4152. }
  4153. TEST (mixer_sends_between_inserts)
  4154. {
  4155. // insert 1 goes only to 3; 3 goes to the Master: same sum (0.5 + 0.5)
  4156. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.setRoute (1, 3, true); s.setRoute (1, 0, false); }) - 1.0f) < 1e-4f);
  4157. // send at 50% and muted destination
  4158. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.setRoute (1, 3, true, 0.5f); s.setRoute (1, 0, false); }) - 0.75f) < 1e-4f);
  4159. 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);
  4160. // going to the Master and to 3: the signal doubles
  4161. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.setRoute (1, 3, true); }) - 1.5f) < 1e-4f);
  4162. // solo on 3 lets what reaches it sound (insert 1), but not 2
  4163. 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);
  4164. // solo on 1 lets the path to the Master (3) sound
  4165. 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);
  4166. // order and cycles
  4167. auto song = lynx::Song::makeDefault();
  4168. song.setRoute (4, 2, true);
  4169. song.setRoute (2, 1, true);
  4170. CHECK (! song.canRoute (1, 4) && ! song.canRoute (1, 2) && song.canRoute (1, 5));
  4171. song.setRoute (1, 4, true); // ignored: would create a cycle
  4172. CHECK (! song.routes (1, 4));
  4173. const auto order = song.mixerOrder();
  4174. const auto pos = [&] (int t) { return std::find (order.begin(), order.end(), t) - order.begin(); };
  4175. CHECK ((int) order.size() == (int) song.mixer.size() - 1);
  4176. CHECK (pos (4) < pos (2) && pos (2) < pos (1));
  4177. song.setRoute (4, 2, false);
  4178. CHECK (! song.routes (4, 2) && song.routes (4, 0));
  4179. }
  4180. namespace
  4181. {
  4182. // Test effect: delays the signal by `latency` samples and reports that latency (like a lookahead plugin).
  4183. class LatentEffect final : public lynx::Effect
  4184. {
  4185. public:
  4186. explicit LatentEffect (int samples) : latency (samples), line (samples) {}
  4187. void prepare (double) noexcept override {}
  4188. void reset() noexcept override {}
  4189. void process (float* l, float* r, int n, const lynx::EffectParams&, double) noexcept override { line.process (l, r, n); }
  4190. int latencySamples() const noexcept override { return latency; }
  4191. private:
  4192. int latency;
  4193. lynx::DelayLine line;
  4194. };
  4195. } // namespace
  4196. TEST (delay_line_mix_into_matches_process_and_drains_tail)
  4197. {
  4198. // mixInto = process over source*level, added to the destination; in stretches that cross the ring
  4199. lynx::DelayLine a (37), b (37);
  4200. std::vector<float> sl (300), sr (300), dl (300, 0.5f), dr (300, -0.5f), xl (300), xr (300);
  4201. for (int i = 0; i < 300; ++i)
  4202. {
  4203. sl[(std::size_t) i] = (float) std::sin (i * 0.1);
  4204. sr[(std::size_t) i] = (float) std::cos (i * 0.07);
  4205. }
  4206. for (int start = 0; start < 300; start += 100)
  4207. {
  4208. b.mixInto (sl.data() + start, sr.data() + start, 0.25f, dl.data() + start, dr.data() + start, 100);
  4209. for (int i = start; i < start + 100; ++i)
  4210. {
  4211. xl[(std::size_t) i] = sl[(std::size_t) i] * 0.25f;
  4212. xr[(std::size_t) i] = sr[(std::size_t) i] * 0.25f;
  4213. }
  4214. a.process (xl.data() + start, xr.data() + start, 100);
  4215. }
  4216. float worst = 0.0f;
  4217. for (int i = 0; i < 300; ++i)
  4218. worst = std::max ({ worst, std::abs (dl[(std::size_t) i] - 0.5f - xl[(std::size_t) i]),
  4219. std::abs (dr[(std::size_t) i] + 0.5f - xr[(std::size_t) i]) });
  4220. CHECK (worst < 1e-6f);
  4221. // silenced source: the tail still in the line comes out, then nothing remains
  4222. std::vector<float> tl (64, 0.0f), tr (64, 0.0f);
  4223. b.mixInto (nullptr, nullptr, 1.0f, tl.data(), tr.data(), 64);
  4224. CHECK (std::abs (tl[0] - sl[263] * 0.25f) < 1e-6f);
  4225. std::vector<float> zl (64, 0.0f), zr (64, 0.0f);
  4226. b.mixInto (nullptr, nullptr, 1.0f, zl.data(), zr.data(), 64);
  4227. CHECK (*std::max_element (zl.begin(), zl.end()) == 0.0f && *std::min_element (zl.begin(), zl.end()) == 0.0f);
  4228. }
  4229. TEST (limiter_lookahead_holds_ceiling_on_bursts)
  4230. {
  4231. // short bursts over a low floor: the sliding maximum must see each peak before it leaves
  4232. auto fx = lynx::makeEffect (lynx::EffectKind::limiter);
  4233. fx->prepare (48000.0);
  4234. auto slot = lynx::makeEffectSlot (lynx::EffectKind::limiter);
  4235. slot.params[2] = 0.0f; // 10 ms release
  4236. std::vector<float> l (48000), r (48000);
  4237. std::uint32_t seed = 1;
  4238. for (int i = 0; i < 48000; ++i)
  4239. {
  4240. seed = seed * 1664525u + 1013904223u;
  4241. const auto noise = ((float) (seed >> 8) / 16777216.0f - 0.5f) * 0.1f;
  4242. l[(std::size_t) i] = noise + ((i % 9973) < 3 ? 3.0f : 0.0f);
  4243. r[(std::size_t) i] = -noise;
  4244. }
  4245. for (int i = 0; i < 48000; i += 333)
  4246. fx->process (l.data() + i, r.data() + i, std::min (333, 48000 - i), slot.params, 120.0);
  4247. float peak = 0.0f, quiet = 0.0f;
  4248. for (int i = 0; i < 48000; ++i)
  4249. peak = std::max (peak, std::abs (l[(std::size_t) i]));
  4250. for (int i = 9973 * 2 + 2000; i < 9973 * 2 + 2400; ++i) // 40 ms after the burst the gain has already recovered
  4251. quiet = std::max (quiet, std::abs (r[(std::size_t) i]));
  4252. const auto ceiling = 1.0f; // the default ceiling is at 0 dBFS or below
  4253. CHECK (peak <= ceiling + 1e-5f);
  4254. CHECK (quiet > 0.03f);
  4255. }
  4256. TEST (pdc_aligns_paths_through_latent_effects)
  4257. {
  4258. // plan: channel 0 -> insert 1 (effect of 100) -> insert 3; channel 1 -> insert 2 -> Master
  4259. lynx::Song s;
  4260. s.mixer = lynx::Song::defaultMixer (4);
  4261. s.channels.resize (3);
  4262. s.channels[0].mixerTrack = 1;
  4263. s.channels[1].mixerTrack = 2;
  4264. s.channels[2].mixerTrack = 3;
  4265. s.mixer[1].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::eq));
  4266. s.setRoute (1, 3, true);
  4267. s.setRoute (1, 0, false);
  4268. auto plan = lynx::computePdc (s, { 0, 0, 30 }, { {}, { 100 } });
  4269. CHECK (plan.inAlign[1] == 0 && plan.outLat[1] == 100);
  4270. CHECK (plan.inAlign[3] == 100 && plan.channelDelay[2] == 70); // instrument of 30 on insert 3
  4271. CHECK (plan.sendDelay[1][1] == 0); // the slowest path isn't delayed
  4272. CHECK (plan.sendDelay[2][0] == 100 && plan.sendDelay[3][0] == 0 && plan.total == 100);
  4273. // bypass zeroes the slot's latency
  4274. s.mixer[1].effects[0].enabled = false;
  4275. CHECK (lynx::computePdc (s, { 0, 0, 30 }, { {}, { 100 } }).total == 30);
  4276. // rendered: two impulses played together come out together
  4277. lynx::Song r;
  4278. r.mixer = lynx::Song::defaultMixer (2);
  4279. for (int i = 0; i < 2; ++i)
  4280. {
  4281. lynx::ChannelDef c;
  4282. c.instrument = lynx::InstrumentKind::sampler;
  4283. c.samplePath = "impulso";
  4284. c.volume = 1.0f;
  4285. c.mixerTrack = i + 1;
  4286. r.channels.push_back (c);
  4287. }
  4288. r.patterns.emplace_back();
  4289. lynx::EffectSlot fx;
  4290. fx.kind = lynx::EffectKind::plugin;
  4291. fx.plugin.id = "latente";
  4292. r.mixer[1].effects.push_back (fx);
  4293. lynx::RackState::Factories f;
  4294. f.samples = [] (const std::string&) {
  4295. auto d = std::make_shared<lynx::SampleData>();
  4296. d->left.assign (64, 0.0f);
  4297. d->left[0] = 1.0f;
  4298. return std::shared_ptr<const lynx::SampleData> (d);
  4299. };
  4300. f.pluginEffects = [] (const lynx::PluginRef&, double) -> std::shared_ptr<lynx::Effect> { return std::make_shared<LatentEffect> (100); };
  4301. auto state = lynx::RackState::build (r, nullptr, 48000.0, f);
  4302. CHECK (state->pdc.total == 100 && state->sendDelays[2][0] != nullptr && state->sendDelays[2][0]->length() == 100);
  4303. lynx::RackRenderer renderer;
  4304. renderer.prepare (48000.0, 256);
  4305. lynx::LiveEventQueue live;
  4306. for (int ch = 0; ch < 2; ++ch)
  4307. {
  4308. lynx::NoteEvent on;
  4309. on.channel = ch;
  4310. on.key = 60;
  4311. on.velocity = 1.0f;
  4312. live.push (on);
  4313. }
  4314. std::vector<float> l (256), rr (256);
  4315. renderer.process (state.get(), false, 0.0, 256, l.data(), rr.data(), &live);
  4316. int first = -1;
  4317. for (int i = 0; i < 256 && first < 0; ++i)
  4318. if (std::abs (l[(std::size_t) i]) > 0.01f)
  4319. first = i;
  4320. CHECK (first == 100 && std::abs (l[100] - 2.0f) < 1e-3f); // both impulses arrive together, summed
  4321. // editing without changing latencies reuses the lines
  4322. r.channels[0].volume = 0.5f;
  4323. auto next = lynx::RackState::build (r, state.get(), 48000.0, f);
  4324. CHECK (next->sendDelays[2][0] == state->sendDelays[2][0]);
  4325. }
  4326. TEST (mixer_mute_fades_instead_of_clicking)
  4327. {
  4328. lynx::Song s;
  4329. s.mixer = lynx::Song::defaultMixer (2);
  4330. lynx::ChannelDef c;
  4331. c.instrument = lynx::InstrumentKind::sampler;
  4332. c.samplePath = "dc";
  4333. c.volume = 1.0f;
  4334. c.mixerTrack = 1;
  4335. s.channels.push_back (c);
  4336. s.patterns.emplace_back();
  4337. auto provider = [] (const std::string&) {
  4338. auto d = std::make_shared<lynx::SampleData>();
  4339. d->left.assign (48000, 0.5f);
  4340. return std::shared_ptr<const lynx::SampleData> (d);
  4341. };
  4342. auto on = lynx::RackState::build (s, nullptr, 48000.0, provider);
  4343. s.mixer[1].muted = true;
  4344. auto off = lynx::RackState::build (s, on.get(), 48000.0, provider);
  4345. lynx::RackRenderer r;
  4346. r.prepare (48000.0, 256);
  4347. lynx::LiveEventQueue live;
  4348. lynx::NoteEvent note;
  4349. note.velocity = 1.0f;
  4350. live.push (note);
  4351. std::vector<float> l (256), rr (256);
  4352. r.process (on.get(), false, 0.0, 256, l.data(), rr.data(), &live);
  4353. CHECK (std::abs (l[255] - 0.5f) < 1e-3f);
  4354. r.process (off.get(), false, 0.0, 256, l.data(), rr.data());
  4355. CHECK (l[0] > 0.45f && l[128] > 0.2f && l[128] < 0.3f && std::abs (l[255]) < 1e-6f); // ramp down to zero
  4356. r.process (off.get(), false, 0.0, 256, l.data(), rr.data());
  4357. CHECK (std::abs (l[0]) < 1e-9f);
  4358. }
  4359. TEST (dither_quantizes_to_lsb_grid_with_small_error)
  4360. {
  4361. lynx::Dither d (16);
  4362. std::vector<float> x (4800);
  4363. for (std::size_t i = 0; i < x.size(); ++i)
  4364. x[i] = 0.25f * std::sin ((float) i * 0.05f);
  4365. auto y = x;
  4366. d.process (y.data(), (int) y.size(), 0);
  4367. double errorSum = 0.0;
  4368. bool onGrid = true;
  4369. for (std::size_t i = 0; i < x.size(); ++i)
  4370. {
  4371. const auto steps = y[i] * 32768.0f;
  4372. onGrid = onGrid && std::abs (steps - std::round (steps)) < 1e-3f;
  4373. errorSum += std::abs (y[i] - x[i]);
  4374. }
  4375. CHECK (onGrid);
  4376. CHECK (errorSum / (double) x.size() < 4.0 / 32768.0); // a few LSBs of noise on average
  4377. }
  4378. TEST (parallel_rendering_matches_sequential)
  4379. {
  4380. // 32 channels (worth parallelizing), inserts with chained sends
  4381. auto song = lynx::Song::makeDefault();
  4382. song.mixer = lynx::Song::defaultMixer (16);
  4383. for (int i = 5; i < 32; ++i)
  4384. {
  4385. lynx::ChannelDef c;
  4386. c.instrument = i % 2 ? lynx::InstrumentKind::synth : lynx::InstrumentKind::hat;
  4387. c.mixerTrack = 1 + i % 12;
  4388. c.volume = 0.3f;
  4389. song.channels.push_back (c);
  4390. for (int step = i % 4; step < 16; step += 3)
  4391. song.patterns[0].notes.push_back ({ (std::int64_t) step * lynx::ticksPerStep, 200, i, 48 + i, 0.7f });
  4392. }
  4393. song.patterns[0].sortNotes();
  4394. song.setRoute (3, 7, true, 0.6f);
  4395. song.setRoute (7, 9, true, 0.5f);
  4396. song.setRoute (2, 9, true);
  4397. song.mixer[9].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::reverb));
  4398. auto seqState = lynx::RackState::build (song, nullptr, 48000.0);
  4399. auto parState = lynx::RackState::build (song, nullptr, 48000.0);
  4400. CHECK (parState->parallel && parState->mixerLayers.size() == 3);
  4401. lynx::WorkerPool pool (3);
  4402. lynx::RackRenderer seq, par;
  4403. seq.prepare (48000.0, 512);
  4404. par.prepare (48000.0, 512);
  4405. par.setWorkerPool (&pool);
  4406. std::vector<float> l1 (512), r1 (512), l2 (512), r2 (512);
  4407. const auto tps = lynx::ticksPerSample (song.bpm, 48000.0);
  4408. bool same = true;
  4409. float peak = 0.0f;
  4410. for (int block = 0; block < 200; ++block)
  4411. {
  4412. seq.process (seqState.get(), true, block * 512 * tps, 512, l1.data(), r1.data());
  4413. par.process (parState.get(), true, block * 512 * tps, 512, l2.data(), r2.data());
  4414. same = same && l1 == l2 && r1 == r2;
  4415. for (auto v : l1)
  4416. peak = std::max (peak, std::abs (v));
  4417. }
  4418. CHECK (same);
  4419. CHECK (peak > 0.05f);
  4420. // the pool handles many small rounds in a row
  4421. std::atomic<int> sum { 0 };
  4422. auto add = [&sum] (int i) { sum.fetch_add (i + 1); };
  4423. for (int round = 0; round < 2000; ++round)
  4424. pool.parallelFor (5, add);
  4425. CHECK (sum.load() == 2000 * 15);
  4426. }
  4427. TEST (arrangements_switch_and_keep_clip_indices)
  4428. {
  4429. auto s = lynx::Song::makeDefault();
  4430. s.patterns.emplace_back().name = "B";
  4431. s.playlist = { { 1, 0, 0, 960, 0 } };
  4432. s.loopEnd = 960;
  4433. CHECK (s.arrangements.size() == 1 && s.currentArrangement == 0);
  4434. const auto second = s.addArrangement ("Versão curta", true);
  4435. CHECK (second == 1 && s.currentArrangement == 1 && s.playlist.size() == 1 && s.loopEnd == 960);
  4436. s.playlist.push_back ({ 0, 1, 960, 960, 0 });
  4437. s.loopEnd = 0;
  4438. s.selectArrangement (0);
  4439. CHECK (s.playlist.size() == 1 && s.loopEnd == 960 && s.arrangements[1].clips.size() == 2);
  4440. // removing pattern 0 fixes the clips stored in the other arrangement
  4441. s.removePattern (0);
  4442. CHECK (s.playlist.size() == 1 && s.playlist[0].pattern == 0);
  4443. CHECK (s.arrangements[1].clips.size() == 1 && s.arrangements[1].clips[0].pattern == 0);
  4444. s.removeArrangement (0);
  4445. CHECK (s.arrangements.size() == 1 && s.currentArrangement == 0 && s.playlist.size() == 1 && s.loopEnd == 0);
  4446. }
  4447. namespace
  4448. {
  4449. // Test effect that inverts the signal and counts how many times it processed.
  4450. class CountingInverter final : public lynx::Effect
  4451. {
  4452. public:
  4453. void prepare (double) noexcept override {}
  4454. void reset() noexcept override {}
  4455. void process (float* l, float* r, int n, const lynx::EffectParams&, double) noexcept override
  4456. {
  4457. ++calls;
  4458. for (int i = 0; i < n; ++i)
  4459. {
  4460. l[i] = -l[i];
  4461. r[i] = -r[i];
  4462. }
  4463. }
  4464. int calls = 0;
  4465. };
  4466. } // namespace
  4467. TEST (keyboard_controller_outputs_key_value)
  4468. {
  4469. std::vector<std::uint8_t> blob (554, 0);
  4470. const std::uint32_t version = 3;
  4471. std::memcpy (blob.data(), &version, 4);
  4472. const float key60 = 0.7f, off = 0.2f;
  4473. std::memcpy (blob.data() + 12 + 60 * 4, &key60, 4);
  4474. std::memcpy (blob.data() + 548, &off, 4);
  4475. blob[552] = 1; // auto release
  4476. lynx::ChannelDef c;
  4477. c.instrument = lynx::InstrumentKind::controller;
  4478. c.controllerState = blob;
  4479. auto inst = lynx::makeInstrument (lynx::InstrumentKind::controller);
  4480. inst->prepare (48000.0);
  4481. inst->configure (c, 120.0);
  4482. std::vector<float> l (64), r (64);
  4483. inst->noteOn (60, 1.0f);
  4484. inst->render (l.data(), r.data(), 64, 1.0f, 1.0f);
  4485. CHECK (std::abs (inst->controlOutput (0) - 0.7f) < 1e-6f);
  4486. inst->noteOff (60);
  4487. CHECK (std::abs (inst->controlOutput (0) - 0.2f) < 1e-6f);
  4488. }
  4489. TEST (envelope_controller_ducks_on_each_note)
  4490. {
  4491. // a unipolar articulator like "Basic Sidechain": base 0.05, drops to 0 on the note and rises to 1 in 0.3 s
  4492. lynx::ControllerArticulator a;
  4493. a.envOn = true;
  4494. a.base = 0.05f;
  4495. a.envLevel = 1.0f;
  4496. a.count = 3;
  4497. a.time = { 0.0f, 0.2f, 0.3f };
  4498. a.level = { 0.0f, 0.75f, 1.0f };
  4499. CHECK (std::abs (lynx::controllerValue (a, 0.0, -1.0) - 0.05f) < 1e-4f);
  4500. CHECK (std::abs (lynx::controllerValue (a, 0.1, -1.0) - 0.425f) < 1e-3f);
  4501. CHECK (lynx::controllerValue (a, 1.0, -1.0) > 0.99f); // no sustain: runs to the end and stays
  4502. a.sustain = 1;
  4503. CHECK (std::abs (lynx::controllerValue (a, 1.0, -1.0) - 0.8f) < 1e-3f); // held at sustain
  4504. CHECK (lynx::controllerValue (a, 1.0, 0.5) > 0.99f); // released: continues to the end
  4505. a.bipolar = true;
  4506. a.base = 0.5f;
  4507. a.sustain = -1;
  4508. CHECK (std::abs (lynx::controllerValue (a, 0.0, -1.0) - 0.0f) < 1e-4f); // bipolar: 0.5 + (0 − 0.5)
  4509. }
  4510. TEST (peak_controller_drives_live_automation)
  4511. {
  4512. // track 1: source (DC 0.5) with Peak Controller; track 2: DC 0.5 with volume linked to the controller
  4513. auto render = [] (bool linked) {
  4514. lynx::Song s;
  4515. s.mixer = lynx::Song::defaultMixer (2);
  4516. for (int t = 1; t <= 2; ++t)
  4517. {
  4518. lynx::ChannelDef c;
  4519. c.instrument = lynx::InstrumentKind::sampler;
  4520. c.samplePath = "dc";
  4521. c.volume = 1.0f;
  4522. c.mixerTrack = t;
  4523. s.channels.push_back (c);
  4524. }
  4525. s.patterns.emplace_back();
  4526. auto peak = lynx::makeEffectSlot (lynx::EffectKind::peakController);
  4527. peak.params[0] = 1.0f; // base 1
  4528. peak.params[1] = 0.0f; // amount −1: 1 − peak
  4529. peak.params[2] = 0.5f; // no tension
  4530. s.mixer[1].effects.push_back (peak);
  4531. if (linked)
  4532. {
  4533. lynx::Automation a;
  4534. a.name = "Peak";
  4535. a.target = { lynx::AutomationTargetKind::mixerVolume, 2 };
  4536. a.source = { 1, 0, 0 };
  4537. s.automations.push_back (a);
  4538. }
  4539. lynx::RackState::Factories f;
  4540. f.samples = [] (const std::string&) {
  4541. auto d = std::make_shared<lynx::SampleData>();
  4542. d->left.assign (48000, 0.5f);
  4543. return std::shared_ptr<const lynx::SampleData> (d);
  4544. };
  4545. auto state = lynx::RackState::build (s, nullptr, 48000.0, f);
  4546. lynx::RackRenderer r;
  4547. r.prepare (48000.0, 512);
  4548. lynx::LiveEventQueue live;
  4549. for (int ch = 0; ch < 2; ++ch)
  4550. {
  4551. lynx::NoteEvent on;
  4552. on.channel = ch;
  4553. on.velocity = 1.0f;
  4554. live.push (on);
  4555. }
  4556. std::vector<float> l (4800), rr (4800);
  4557. r.process (state.get(), false, 0.0, 4800, l.data(), rr.data(), &live);
  4558. return l[4000];
  4559. };
  4560. const auto free = render (false), ducked = render (true);
  4561. CHECK (std::isfinite (ducked) && ducked > 0.0f);
  4562. CHECK (ducked < free - 0.05f); // the 0.5 peak on track 1 lowers track 2's volume
  4563. }
  4564. TEST (effect_slot_dry_wet_and_smart_disable)
  4565. {
  4566. lynx::Song s;
  4567. s.mixer = lynx::Song::defaultMixer (1);
  4568. lynx::ChannelDef c;
  4569. c.instrument = lynx::InstrumentKind::sampler;
  4570. c.samplePath = "dc";
  4571. c.volume = 1.0f;
  4572. c.mixerTrack = 1;
  4573. s.channels.push_back (c);
  4574. s.patterns.emplace_back();
  4575. lynx::EffectSlot fx;
  4576. fx.kind = lynx::EffectKind::plugin;
  4577. fx.plugin.id = "inversor";
  4578. fx.mix = 0.25f; // 75% dry (+0.5) and 25% wet (-0.5) = +0.25
  4579. s.mixer[1].effects.push_back (fx);
  4580. auto inverter = std::make_shared<CountingInverter>();
  4581. lynx::RackState::Factories f;
  4582. f.samples = [] (const std::string&) {
  4583. auto d = std::make_shared<lynx::SampleData>();
  4584. d->left.assign (4800, 0.5f);
  4585. return std::shared_ptr<const lynx::SampleData> (d);
  4586. };
  4587. f.pluginEffects = [inverter] (const lynx::PluginRef&, double) -> std::shared_ptr<lynx::Effect> { return inverter; };
  4588. auto state = lynx::RackState::build (s, nullptr, 48000.0, f);
  4589. lynx::RackRenderer r;
  4590. r.prepare (48000.0, 512);
  4591. lynx::LiveEventQueue live;
  4592. lynx::NoteEvent on;
  4593. on.velocity = 1.0f;
  4594. live.push (on);
  4595. std::vector<float> l (480), rr (480);
  4596. r.process (state.get(), false, 0.0, 480, l.data(), rr.data(), &live);
  4597. CHECK (std::abs (l[100] - 0.25f) < 1e-4f);
  4598. // after the sample ends: 4 s of silence and the plugin stops being called
  4599. for (int i = 0; i < 100 * 6; ++i) // 6 s
  4600. r.process (state.get(), false, 0.0, 480, l.data(), rr.data());
  4601. const auto callsAfterSilence = inverter->calls;
  4602. for (int i = 0; i < 100; ++i)
  4603. r.process (state.get(), false, 0.0, 480, l.data(), rr.data());
  4604. CHECK (inverter->calls == callsAfterSilence);
  4605. CHECK (callsAfterSilence < 100 * 6);
  4606. // sound again: processing resumes in the same block
  4607. live.push (on);
  4608. r.process (state.get(), false, 0.0, 480, l.data(), rr.data(), &live);
  4609. CHECK (inverter->calls == callsAfterSilence + 1 && std::abs (l[100] - 0.25f) < 1e-4f);
  4610. }
  4611. TEST (swing_delays_offbeat_sixteenths)
  4612. {
  4613. CHECK (lynx::swingDelay (0, 1.0f) == 0);
  4614. CHECK (lynx::swingDelay (lynx::ticksPerStep, 1.0f) == lynx::ticksPerStep / 2);
  4615. CHECK (lynx::swingDelay (lynx::ticksPerStep * 2, 1.0f) == 0);
  4616. CHECK (lynx::swingDelay (lynx::ticksPerStep + 10, 1.0f) == (lynx::ticksPerStep - 20) / 2); // off the grid: triangular weight (FL)
  4617. CHECK (lynx::swingDelay (lynx::ticksPerStep, 1.0f, 0.5f) == lynx::ticksPerStep / 4); // channel mix
  4618. CHECK (lynx::swingDelay (lynx::ticksPerStep * 3, 0.5f) == lynx::ticksPerStep / 4);
  4619. lynx::Pattern p;
  4620. p.notes = { { 0, 100, 0, 60 }, { lynx::ticksPerStep, 100, 0, 62 } };
  4621. lynx::EventBuffer ev;
  4622. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  4623. lynx::schedulePatternLoop (p, p.effectiveLength (4), 0.0, 12000, tps, ev, 1.0f);
  4624. int onOffset = -1;
  4625. for (auto& e : ev)
  4626. if (e.type == lynx::NoteEvent::Type::on && e.key == 62)
  4627. onOffset = e.sampleOffset;
  4628. CHECK (onOffset == (int) std::floor ((lynx::ticksPerStep * 1.5) / tps));
  4629. }
  4630. TEST (sampler_settings_pitch_reverse_envelope_cut)
  4631. {
  4632. auto ramp = std::make_shared<lynx::SampleData>();
  4633. for (int i = 0; i < 48000; ++i)
  4634. ramp->left.push_back ((float) i / 48000.0f); // ramp: the value tells where the read position is
  4635. ramp->sampleRate = 48000.0;
  4636. auto play = [&] (const lynx::SamplerSettings& s, int key, int frames, bool releaseAfterHalf) {
  4637. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  4638. inst->prepare (48000.0);
  4639. lynx::ChannelDef c;
  4640. c.sampler = s;
  4641. inst->configure (c, 120.0);
  4642. inst->noteOn (key, 1.0f);
  4643. std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
  4644. inst->render (l.data(), r.data(), frames / 2, 1.0f, 1.0f);
  4645. if (releaseAfterHalf)
  4646. inst->noteOff (key);
  4647. inst->render (l.data() + frames / 2, r.data() + frames / 2, frames - frames / 2, 1.0f, 1.0f);
  4648. return l;
  4649. };
  4650. // +12 semitones: reads twice as fast
  4651. lynx::SamplerSettings up;
  4652. up.pitch = 12.0f;
  4653. CHECK (std::abs (play (up, 60, 1000, false)[999] - 2.0f * 999.0f / 48000.0f) < 1e-3f);
  4654. // reversed: starts from the end
  4655. lynx::SamplerSettings back;
  4656. back.reverse = true;
  4657. CHECK (play (back, 60, 100, false)[10] > 0.99f);
  4658. // 10 ms attack: starts silent and reaches full level
  4659. lynx::SamplerSettings slow;
  4660. slow.attack = 0.01f;
  4661. const auto a = play (slow, 60, 2000, false);
  4662. CHECK (a[10] < 0.2f * (10.0f / 48000.0f) + 1e-6f && std::abs (a[1500] - 1500.0f / 48000.0f) < 1e-4f);
  4663. // 5 ms release: gone right after release; without release it plays in full
  4664. lynx::SamplerSettings rel;
  4665. rel.release = 0.005f;
  4666. CHECK (play (rel, 60, 2000, true)[1999] == 0.0f);
  4667. CHECK (play (lynx::SamplerSettings {}, 60, 2000, true)[1999] > 0.0f);
  4668. // cut itself: the second note cuts the first (one voice remains)
  4669. lynx::SamplerSettings cut;
  4670. cut.cut = true;
  4671. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  4672. inst->prepare (48000.0);
  4673. lynx::ChannelDef cutChannel;
  4674. cutChannel.sampler = cut;
  4675. inst->configure (cutChannel, 120.0);
  4676. inst->noteOn (60, 1.0f);
  4677. std::vector<float> l (2000), r (2000);
  4678. inst->render (l.data(), r.data(), 1000, 1.0f, 1.0f);
  4679. inst->noteOn (60, 1.0f);
  4680. std::fill (l.begin(), l.end(), 0.0f);
  4681. inst->render (l.data(), r.data(), 2000, 1.0f, 1.0f);
  4682. CHECK (std::abs (l[1999] - 1999.0f / 48000.0f) < 1e-4f); // only the new note
  4683. }
  4684. TEST (playlist_split_and_track_mute)
  4685. {
  4686. lynx::Clip c { 0, 3, 960, 3840, 480 };
  4687. const auto rest = lynx::splitClip (c, 2000);
  4688. CHECK (c.start == 960 && c.length == 1040 && c.offset == 480);
  4689. CHECK (rest.start == 2000 && rest.length == 2800 && rest.offset == 480 + 1040 && rest.track == 3);
  4690. auto s = lynx::Song::makeDefault();
  4691. s.mode = lynx::PlayMode::song;
  4692. s.playlist = { { 0, 0, 0, 3840, 0 } };
  4693. lynx::EventBuffer ev;
  4694. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  4695. lynx::scheduleSong (s, 0.0, 48000, tps, ev);
  4696. CHECK (ev.size() > 0);
  4697. s.playlistTracks.resize (1);
  4698. s.playlistTracks[0].muted = true;
  4699. ev.clear();
  4700. lynx::scheduleSong (s, 0.0, 48000, tps, ev);
  4701. CHECK (ev.size() == 0);
  4702. }
  4703. TEST (note_pan_pitch_and_mute)
  4704. {
  4705. lynx::Pattern p;
  4706. lynx::Note a { 0, 240, 0, 60 };
  4707. a.pan = -1.0f;
  4708. a.finePitch = 50.0f;
  4709. lynx::Note b { 240, 240, 0, 62 };
  4710. b.muted = true;
  4711. p.notes = { a, b };
  4712. lynx::EventBuffer ev;
  4713. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  4714. lynx::schedulePatternLoop (p, p.effectiveLength (4), 0.0, 48000, tps, ev);
  4715. int ons = 0;
  4716. for (auto& e : ev)
  4717. if (e.type == lynx::NoteEvent::Type::on)
  4718. {
  4719. ++ons;
  4720. CHECK (e.key == 60 && e.pan == -1.0f && e.cents == 50.0f);
  4721. }
  4722. CHECK (ons == 1); // the muted one doesn't play
  4723. // note pan in the sampler: fully left
  4724. auto dc = std::make_shared<lynx::SampleData>();
  4725. dc->left.assign (4800, 0.5f);
  4726. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, dc);
  4727. inst->prepare (48000.0);
  4728. inst->setNextNoteExpression (-1.0f, 0.0f);
  4729. inst->noteOn (60, 1.0f);
  4730. std::vector<float> l (100), r (100);
  4731. inst->render (l.data(), r.data(), 100, 1.0f, 1.0f);
  4732. CHECK (l[50] > 0.6f && std::abs (r[50]) < 1e-6f);
  4733. }
  4734. TEST (tempo_automation_changes_speed_and_duration)
  4735. {
  4736. auto song = lynx::Song::makeDefault();
  4737. song.bpm = 120.0;
  4738. song.mode = lynx::PlayMode::song;
  4739. const auto bar = (std::int64_t) lynx::ppq * 4;
  4740. song.playlist = { { 0, 0, 0, bar, 0 }, { 0, 0, bar, bar, 0 } };
  4741. lynx::Automation tempo;
  4742. tempo.target = { lynx::AutomationTargetKind::tempo, 0 };
  4743. const auto v120 = lynx::valueFromTempo (120.0), v240 = lynx::valueFromTempo (240.0);
  4744. tempo.points = { { 0, v120 }, { bar - 1, v120 }, { bar, v240 } }; // step: 120 -> 240 at bar 2
  4745. song.automations.push_back (tempo);
  4746. lynx::Clip clip;
  4747. clip.kind = lynx::ClipKind::automation;
  4748. clip.automation = 0;
  4749. clip.track = 1;
  4750. clip.length = bar * 2;
  4751. song.playlist.push_back (clip);
  4752. CHECK (std::abs (song.tempoAt (100.0) - 120.0) < 0.01 && std::abs (song.tempoAt ((double) bar + 100.0) - 240.0) < 0.01);
  4753. // 1 bar at 120 (2 s) + 1 at 240 (1 s)
  4754. CHECK (std::abs (song.secondsForTicks (bar * 2) - 3.0) < 0.01);
  4755. auto state = lynx::RackState::build (song, nullptr, 48000.0);
  4756. CHECK (state->tempoAutomation == 0);
  4757. lynx::RackRenderer r;
  4758. r.prepare (48000.0, 512);
  4759. std::vector<float> l (512), rr (512);
  4760. r.process (state.get(), true, 10.0, 512, l.data(), rr.data());
  4761. CHECK (std::abs (r.currentBpm() - 120.0) < 0.01);
  4762. r.process (state.get(), true, (double) bar + 10.0, 512, l.data(), rr.data());
  4763. CHECK (std::abs (r.currentBpm() - 240.0) < 0.01);
  4764. }
  4765. TEST (bounce_insert_song_and_apply)
  4766. {
  4767. auto s = lynx::Song::makeDefault();
  4768. s.setRoute (3, 7, true, 0.5f);
  4769. s.mixer[0].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::reverb));
  4770. s.mixer[2].solo = true;
  4771. const auto b = lynx::makeBounceSong (s, 3);
  4772. CHECK (b.mixer[3].solo && ! b.mixer[2].solo && b.mixer[3].toMaster && b.mixer[3].sends.empty());
  4773. CHECK (b.mixer[0].effects.empty() && b.mode == lynx::PlayMode::song);
  4774. const auto audible = b.soloAudible();
  4775. CHECK (audible[3] && ! audible[1] && ! audible[7]);
  4776. const auto channels = s.channels.size();
  4777. const auto inserts = s.mixer.size();
  4778. const auto created = lynx::applyBounce (s, 3, "C:/x.wav", 3840);
  4779. CHECK (created == (int) inserts && s.mixer.size() == inserts + 1 && s.mixer[3].muted);
  4780. CHECK (s.mixer[(std::size_t) created].sends.size() == 1 && s.mixer[(std::size_t) created].sends[0].target == 7);
  4781. CHECK (s.channels.size() == channels + 1 && s.channels.back().mixerTrack == created);
  4782. CHECK (s.playlist.back().kind == lynx::ClipKind::audio && s.playlist.back().length == 3840);
  4783. }
  4784. TEST (mixer_stereo_separation_phase_swap)
  4785. {
  4786. lynx::MixerTrack t;
  4787. std::vector<float> l { 1.0f }, r { 0.0f };
  4788. t.stereo = 1.0f; // mono
  4789. lynx::RackRenderer::applyStereoForTest (t, l.data(), r.data(), 1);
  4790. CHECK (std::abs (l[0] - 0.5f) < 1e-6f && std::abs (r[0] - 0.5f) < 1e-6f);
  4791. l = { 1.0f };
  4792. r = { 0.0f };
  4793. t.stereo = -1.0f; // doubled side: 1.5 L - 0.5 R
  4794. lynx::RackRenderer::applyStereoForTest (t, l.data(), r.data(), 1);
  4795. CHECK (std::abs (l[0] - 1.5f) < 1e-6f && std::abs (r[0] + 0.5f) < 1e-6f);
  4796. l = { 1.0f };
  4797. r = { 0.25f };
  4798. t.stereo = 0.0f;
  4799. t.swapChannels = true;
  4800. t.invertPhase = true;
  4801. lynx::RackRenderer::applyStereoForTest (t, l.data(), r.data(), 1);
  4802. CHECK (l[0] == -0.25f && r[0] == -1.0f);
  4803. }
  4804. TEST (effects_limiter_filter_distortion_chorus)
  4805. {
  4806. const double sr = 48000.0;
  4807. auto sine = [] (float hz, float amp, int n) {
  4808. std::vector<float> v ((std::size_t) n);
  4809. for (int i = 0; i < n; ++i)
  4810. v[(std::size_t) i] = amp * std::sin (2.0f * 3.14159265f * hz * (float) i / 48000.0f);
  4811. return v;
  4812. };
  4813. auto peak = [] (const std::vector<float>& v, int from) {
  4814. float p = 0.0f;
  4815. for (std::size_t i = (std::size_t) from; i < v.size(); ++i)
  4816. p = std::max (p, std::abs (v[i]));
  4817. return p;
  4818. };
  4819. // limiter: +12 dB on a 0.9 sine never exceeds the ceiling (-1 dB) and reports the latency
  4820. auto lim = lynx::makeEffect (lynx::EffectKind::limiter);
  4821. lim->prepare (sr);
  4822. auto slot = lynx::makeEffectSlot (lynx::EffectKind::limiter);
  4823. slot.params[0] = 11.0f / 12.0f; // -1 dB
  4824. slot.params[1] = 0.5f; // +12 dB
  4825. auto l = sine (100.0f, 0.9f, 48000), r = l;
  4826. lim->process (l.data(), r.data(), 48000, slot.params, 120.0);
  4827. CHECK (peak (l, 0) <= std::pow (10.0f, -1.0f / 20.0f) + 1e-4f);
  4828. CHECK (peak (l, 4800) > 0.8f);
  4829. CHECK (lim->latencySamples() == 72);
  4830. // low-pass filter at 500 Hz knocks down an 8 kHz sine
  4831. auto lp = lynx::makeEffect (lynx::EffectKind::filter);
  4832. lp->prepare (sr);
  4833. auto fs = lynx::makeEffectSlot (lynx::EffectKind::filter);
  4834. fs.params[1] = std::log (500.0f / 20.0f) / std::log (1000.0f);
  4835. l = sine (8000.0f, 1.0f, 9600);
  4836. r = l;
  4837. lp->process (l.data(), r.data(), 9600, fs.params, 120.0);
  4838. CHECK (peak (l, 4800) < 0.05f);
  4839. // distortion and chorus: finite, audible output
  4840. for (auto kind : { lynx::EffectKind::distortion, lynx::EffectKind::chorus })
  4841. {
  4842. auto fx = lynx::makeEffect (kind);
  4843. fx->prepare (sr);
  4844. auto ps = lynx::makeEffectSlot (kind);
  4845. l = sine (220.0f, 0.5f, 9600);
  4846. r = l;
  4847. fx->process (l.data(), r.data(), 9600, ps.params, 120.0);
  4848. bool finite = true;
  4849. for (auto v : l)
  4850. finite = finite && std::isfinite (v);
  4851. CHECK (finite && peak (l, 2400) > 0.1f && peak (l, 0) < 2.0f);
  4852. }
  4853. }
  4854. TEST (slide_notes_glide_without_retrigger)
  4855. {
  4856. // sampler with a ramp: the read speed doubles after a one-octave slide
  4857. auto ramp = std::make_shared<lynx::SampleData>();
  4858. for (int i = 0; i < 96000; ++i)
  4859. ramp->left.push_back ((float) i / 96000.0f);
  4860. ramp->sampleRate = 48000.0;
  4861. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  4862. inst->prepare (48000.0);
  4863. inst->noteOn (60, 1.0f);
  4864. std::vector<float> l (2000), r (2000);
  4865. inst->render (l.data(), r.data(), 1000, 1.0f, 1.0f);
  4866. const auto stepBefore = l[999] - l[998];
  4867. inst->slideTo (72, 0.0f, 100.0f);
  4868. std::fill (l.begin(), l.end(), 0.0f);
  4869. inst->render (l.data(), r.data(), 1000, 1.0f, 1.0f);
  4870. const auto stepAfter = l[999] - l[998];
  4871. CHECK (std::abs (stepAfter / stepBefore - 2.0f) < 0.01f);
  4872. // the sequencer sends "slide" (no off) for the slide note
  4873. lynx::Pattern p;
  4874. lynx::Note base { 0, 960, 0, 48 };
  4875. lynx::Note glide { 480, 240, 0, 55 };
  4876. glide.slide = true;
  4877. p.notes = { base, glide };
  4878. lynx::EventBuffer ev;
  4879. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  4880. lynx::schedulePatternLoop (p, p.effectiveLength (4), 0.0, 30000, tps, ev);
  4881. int ons = 0, offs = 0, slides = 0;
  4882. for (auto& e : ev)
  4883. {
  4884. ons += e.type == lynx::NoteEvent::Type::on;
  4885. offs += e.type == lynx::NoteEvent::Type::off;
  4886. if (e.type == lynx::NoteEvent::Type::slide)
  4887. {
  4888. ++slides;
  4889. CHECK (e.key == 55 && std::abs (e.slideSamples - 240.0f / (float) tps) < 1.0f);
  4890. }
  4891. }
  4892. CHECK (ons == 1 && offs == 1 && slides == 1);
  4893. }
  4894. TEST (synth_fm_phase_modulation)
  4895. {
  4896. auto render = [] (const lynx::SynthSettings& s) {
  4897. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  4898. inst->prepare (48000.0);
  4899. lynx::ChannelDef c;
  4900. c.synth = s;
  4901. inst->configure (c, 120.0);
  4902. inst->noteOn (69, 1.0f); // 440 Hz
  4903. std::vector<float> l (48000), r (48000);
  4904. inst->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
  4905. return l;
  4906. };
  4907. // energy (Goertzel) at frequency f over frames [from, to)
  4908. auto energy = [] (const std::vector<float>& x, double f, int from, int to) {
  4909. const auto w = 2.0 * 3.141592653589793 * f / 48000.0, k = 2.0 * std::cos (w);
  4910. double s1 = 0.0, s2 = 0.0;
  4911. for (int i = from; i < to; ++i)
  4912. {
  4913. const auto s0 = x[(std::size_t) i] + k * s1 - s2;
  4914. s2 = s1;
  4915. s1 = s0;
  4916. }
  4917. return s1 * s1 + s2 * s2 - k * s1 * s2;
  4918. };
  4919. lynx::SynthSettings plain;
  4920. plain.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  4921. plain.osc[2] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  4922. plain.cutoff = 20000.0f;
  4923. auto fm = plain;
  4924. fm.fm = 1.0f;
  4925. const auto a = render (plain), b = render (fm);
  4926. // no FM: osc 3 adds (same pitch) and there's no 2nd harmonic; with FM the sidebands appear
  4927. CHECK (energy (a, 880.0, 4800, 24000) < 1e-3 * energy (a, 440.0, 4800, 24000));
  4928. CHECK (energy (b, 880.0, 4800, 24000) > 0.1 * energy (b, 440.0, 4800, 24000));
  4929. CHECK (energy (b, 1320.0, 4800, 24000) > 0.01 * energy (b, 440.0, 4800, 24000));
  4930. for (auto v : b)
  4931. CHECK (std::isfinite (v) && std::abs (v) < 1.0f);
  4932. // index decaying to 0: the start is bright, the end is nearly a sine again
  4933. auto decaying = fm;
  4934. decaying.fmDecay = 0.1f;
  4935. decaying.fmSustain = 0.0f;
  4936. const auto c = render (decaying);
  4937. const auto early = energy (c, 880.0, 0, 2400) / energy (c, 440.0, 0, 2400);
  4938. const auto late = energy (c, 880.0, 36000, 48000) / energy (c, 440.0, 36000, 48000);
  4939. CHECK (early > 0.05 && late < 1e-3);
  4940. }
  4941. TEST (synth_three_oscillators_adsr_filter)
  4942. {
  4943. auto render = [] (const lynx::SynthSettings& s, int frames, int releaseAt) {
  4944. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  4945. inst->prepare (48000.0);
  4946. lynx::ChannelDef c;
  4947. c.synth = s;
  4948. inst->configure (c, 120.0);
  4949. inst->noteOn (69, 1.0f); // A4 = 440 Hz
  4950. std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
  4951. inst->render (l.data(), r.data(), releaseAt, 1.0f, 1.0f);
  4952. inst->noteOff (69);
  4953. inst->render (l.data() + releaseAt, r.data() + releaseAt, frames - releaseAt, 1.0f, 1.0f);
  4954. return l;
  4955. };
  4956. auto peakIn = [] (const std::vector<float>& v, int from, int to) {
  4957. float p = 0.0f;
  4958. for (int i = from; i < to; ++i)
  4959. p = std::max (p, std::abs (v[(std::size_t) i]));
  4960. return p;
  4961. };
  4962. // default: sounds and fades after the release
  4963. const lynx::SynthSettings standard;
  4964. const auto a = render (standard, 48000, 24000);
  4965. CHECK (peakIn (a, 1000, 20000) > 0.05f && peakIn (a, 40000, 48000) < 1e-3f);
  4966. // pure sine with an open filter: zero-crossing count gives ~440 Hz
  4967. lynx::SynthSettings sine;
  4968. sine.osc[0].wave = lynx::Waveform::sine;
  4969. sine.cutoff = 20000.0f;
  4970. const auto b = render (sine, 48000, 47000);
  4971. int crossings = 0;
  4972. for (int i = 4800; i < 4800 + 48000 / 2; ++i)
  4973. crossings += (b[(std::size_t) i - 1] < 0.0f) != (b[(std::size_t) i] < 0.0f);
  4974. CHECK (std::abs (crossings - 440) < 6); // half a second = 220 cycles = 440 crossings
  4975. float sinePeak = 0.0f;
  4976. for (int i = 4800; i < 9600; ++i)
  4977. sinePeak = std::max (sinePeak, std::abs (b[(std::size_t) i]));
  4978. CHECK (sinePeak > 0.2f && sinePeak < 0.35f); // 0.3 level, approximated sine without clipping
  4979. // second oscillator an octave up adds energy; sustain 0 makes the note decay on its own
  4980. lynx::SynthSettings two = sine;
  4981. two.osc[1] = { lynx::Waveform::sine, 1, 0.0f, 1.0f };
  4982. two.sustain = 0.0f;
  4983. two.decay = 0.1f;
  4984. const auto c = render (two, 48000, 47000);
  4985. CHECK (peakIn (c, 100, 2000) > peakIn (b, 100, 2000) && peakIn (c, 30000, 40000) < 1e-3f);
  4986. }
  4987. TEST (set_target_value_round_trips)
  4988. {
  4989. auto s = lynx::Song::makeDefault();
  4990. s.mixer[2].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::delay));
  4991. const lynx::AutomationTarget targets[] = {
  4992. { lynx::AutomationTargetKind::channelVolume, 1 }, { lynx::AutomationTargetKind::channelPan, 2 },
  4993. { lynx::AutomationTargetKind::mixerVolume, 3 }, { lynx::AutomationTargetKind::mixerPan, 0 },
  4994. { lynx::AutomationTargetKind::effectParam, 2, 0, 1 }, { lynx::AutomationTargetKind::tempo, 0 },
  4995. };
  4996. for (const auto& t : targets)
  4997. {
  4998. CHECK (s.setTargetValue (t, 0.25f));
  4999. CHECK (std::abs (s.targetValue (t) - 0.25f) < 1e-5f);
  5000. }
  5001. CHECK (! s.setTargetValue ({ lynx::AutomationTargetKind::channelParam, 0, 0, 3 }, 0.5f)); // lives in the plugin
  5002. }
  5003. TEST (cut_groups_stretch_and_envelope_import)
  5004. {
  5005. // open hat (cut by 1) is cut when the closed one (group 1) plays
  5006. lynx::Song s;
  5007. s.mixer = lynx::Song::defaultMixer (2);
  5008. for (int i = 0; i < 2; ++i)
  5009. {
  5010. lynx::ChannelDef c;
  5011. c.instrument = lynx::InstrumentKind::sampler;
  5012. c.samplePath = "dc";
  5013. c.volume = 1.0f;
  5014. c.mixerTrack = i + 1;
  5015. s.channels.push_back (c);
  5016. }
  5017. s.channels[0].cutBy = 1; // open
  5018. s.channels[1].cutGroup = 1; // closed
  5019. auto& p = s.patterns.emplace_back();
  5020. p.notes = { { 0, 100, 0, 60 }, { 960, 100, 1, 60 } };
  5021. auto provider = [] (const std::string&) {
  5022. auto d = std::make_shared<lynx::SampleData>();
  5023. d->left.assign (96000, 0.5f);
  5024. return std::shared_ptr<const lynx::SampleData> (d);
  5025. };
  5026. auto state = lynx::RackState::build (s, nullptr, 48000.0, provider);
  5027. CHECK (state->hasCutGroups && state->cutTargets[1].size() == 1 && state->cutTargets[1][0] == 0);
  5028. lynx::RackRenderer r;
  5029. r.prepare (48000.0, 512);
  5030. std::vector<float> l (512), rr (512);
  5031. const auto tps = lynx::ticksPerSample (s.bpm, 48000.0);
  5032. float afterCut = 0.0f;
  5033. for (double tick = 0.0; tick < 960.0 * 3; tick += 512 * tps)
  5034. {
  5035. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  5036. if (tick > 960.0 * 2) // the closed one continues; the open one has stopped
  5037. afterCut = std::max (afterCut, std::abs (l[256]));
  5038. }
  5039. CHECK (std::abs (afterCut - 0.5f * 0.78f) < 0.01f); // only the closed one (default velocity 0.78)
  5040. // resampling stretch: 1 s of sample stretched to 4 beats at 120 BPM = 2 s
  5041. auto ramp = std::make_shared<lynx::SampleData>();
  5042. for (int i = 0; i < 48000; ++i)
  5043. ramp->left.push_back ((float) i / 48000.0f);
  5044. ramp->sampleRate = 48000.0;
  5045. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  5046. inst->prepare (48000.0);
  5047. lynx::ChannelDef c;
  5048. c.sampler.stretchBeats = 4.0f;
  5049. inst->configure (c, 120.0);
  5050. inst->noteOn (72, 1.0f); // the note pitch doesn't change the speed
  5051. std::vector<float> a (48000), b (48000);
  5052. inst->render (a.data(), b.data(), 48000, 1.0f, 1.0f);
  5053. CHECK (std::abs (a[47999] - 0.5f) < 0.01f); // after 1 s it has read half
  5054. // FL envelope time: 32950 = 2 s; < 101 = 0
  5055. CHECK (std::abs (flp::Channel::envelopeTime (32950) - 2.0) < 0.01 && flp::Channel::envelopeTime (50) == 0.0);
  5056. }
  5057. TEST (time_stretch_keeps_pitch)
  5058. {
  5059. lynx::SampleData sine;
  5060. sine.sampleRate = 48000.0;
  5061. for (int i = 0; i < 48000; ++i)
  5062. sine.left.push_back (0.5f * std::sin (2.0f * 3.14159265f * 440.0f * (float) i / 48000.0f));
  5063. auto crossingsPerSecond = [] (const lynx::SampleData& d) {
  5064. int c = 0;
  5065. const auto n = (int) d.frames();
  5066. for (int i = 4800; i < n - 4800; ++i)
  5067. c += (d.left[(std::size_t) i - 1] < 0.0f) != (d.left[(std::size_t) i] < 0.0f);
  5068. return (double) c / ((double) (n - 9600) / d.sampleRate);
  5069. };
  5070. for (double ratio : { 2.0, 0.5, 1.37 })
  5071. {
  5072. const auto out = lynx::timeStretch (sine, ratio);
  5073. CHECK (std::abs ((double) out->frames() - 48000.0 * ratio) < 2.0);
  5074. CHECK (std::abs (crossingsPerSecond (*out) - 880.0) < 15.0); // 440 Hz stays 440 Hz
  5075. float peak = 0.0f;
  5076. for (std::size_t i = 4800; i + 4800 < out->frames(); ++i)
  5077. peak = std::max (peak, std::abs (out->left[i]));
  5078. CHECK (peak > 0.4f && peak < 0.6f); // no gaps or clipping in the overlap
  5079. }
  5080. }
  5081. TEST (sampler_region_start_and_end)
  5082. {
  5083. auto ramp = std::make_shared<lynx::SampleData>();
  5084. for (int i = 0; i < 10000; ++i)
  5085. ramp->left.push_back ((float) i / 10000.0f);
  5086. ramp->sampleRate = 48000.0;
  5087. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  5088. inst->prepare (48000.0);
  5089. lynx::ChannelDef c;
  5090. c.sampler.regionStart = 0.25f;
  5091. c.sampler.regionEnd = 0.5f;
  5092. inst->configure (c, 120.0);
  5093. inst->noteOn (60, 1.0f);
  5094. std::vector<float> l (6000), r (6000);
  5095. inst->render (l.data(), r.data(), 6000, 1.0f, 1.0f);
  5096. CHECK (std::abs (l[0] - 0.25f) < 0.001f); // starts at the beginning of the slice
  5097. CHECK (l[2400] > 0.48f && l[2600] == 0.0f); // and stops at its end
  5098. }
  5099. TEST (arpeggiator_plays_held_chord_in_steps)
  5100. {
  5101. auto song = lynx::Song::makeDefault();
  5102. song.bpm = 120.0;
  5103. song.patterns[0].notes.clear();
  5104. for (int key : { 60, 64, 67 })
  5105. song.patterns[0].notes.push_back ({ 0, lynx::ppq * 4, 4, key, 0.8f }); // chord held for 1 bar
  5106. song.channels[4].arp.direction = 1; // up
  5107. song.channels[4].arp.stepBeats = 0.25f;
  5108. auto state = lynx::RackState::build (song, nullptr, 48000.0);
  5109. CHECK (state->arpeggiators[4] != nullptr && state->arpeggiators[0] == nullptr);
  5110. // the arpeggiator generates the notes; we count what it hands to the instrument
  5111. auto& arp = *state->arpeggiators[4];
  5112. const auto tps = lynx::ticksPerSample (song.bpm, 48000.0);
  5113. std::vector<int> keys;
  5114. lynx::EventBuffer ev;
  5115. for (double tick = 0.0; tick < lynx::ppq * 2.0; tick += 512 * tps)
  5116. {
  5117. ev.clear();
  5118. lynx::schedulePatternLoop (song.patterns[0], song.patterns[0].effectiveLength (4), tick, 512, tps, ev);
  5119. int in = 0, out = 0;
  5120. for (auto& e : ev)
  5121. if (e.channel == 4)
  5122. arp.input[(std::size_t) in++] = e;
  5123. arp.process (4, arp.input.data(), in, tick, tps, 512, song.channels[4].arp, arp.output.data(), out);
  5124. for (int i = 0; i < out; ++i)
  5125. if (arp.output[(std::size_t) i].type == lynx::NoteEvent::Type::on)
  5126. keys.push_back (arp.output[(std::size_t) i].key);
  5127. }
  5128. // 2 beats = 8 sixteenth steps: 60 64 67 60 64 67 60 64
  5129. CHECK (keys.size() >= 8); // the last block goes slightly past 2 beats
  5130. const int expected[] = { 60, 64, 67, 60, 64, 67, 60, 64 };
  5131. for (std::size_t i = 0; i < keys.size() && i < 8; ++i)
  5132. CHECK (keys[i] == expected[i]);
  5133. // actually rendering also makes sound
  5134. lynx::RackRenderer r;
  5135. r.prepare (48000.0, 512);
  5136. std::vector<float> l (512), rr (512);
  5137. float peak = 0.0f;
  5138. for (double tick = 0.0; tick < lynx::ppq; tick += 512 * tps)
  5139. {
  5140. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  5141. for (auto v : l)
  5142. peak = std::max (peak, std::abs (v));
  5143. }
  5144. CHECK (peak > 0.01f);
  5145. }
  5146. TEST (audio_clip_fades_and_gain)
  5147. {
  5148. lynx::Song s;
  5149. s.bpm = 120.0;
  5150. s.mode = lynx::PlayMode::song;
  5151. s.mixer = lynx::Song::defaultMixer (1);
  5152. lynx::ChannelDef c;
  5153. c.instrument = lynx::InstrumentKind::audioClip;
  5154. c.samplePath = "dc";
  5155. c.volume = 1.0f;
  5156. c.mixerTrack = 1;
  5157. s.channels.push_back (c);
  5158. s.patterns.emplace_back();
  5159. lynx::Clip clip;
  5160. clip.kind = lynx::ClipKind::audio;
  5161. clip.channel = 0;
  5162. clip.length = lynx::ppq * 4; // 2 s at 120 BPM
  5163. clip.fadeIn = lynx::ppq; // 0.5 s
  5164. clip.fadeOut = lynx::ppq;
  5165. clip.gain = 0.5f;
  5166. s.playlist.push_back (clip);
  5167. auto provider = [] (const std::string&) {
  5168. auto d = std::make_shared<lynx::SampleData>();
  5169. d->left.assign (48000 * 4, 1.0f);
  5170. return std::shared_ptr<const lynx::SampleData> (d);
  5171. };
  5172. auto state = lynx::RackState::build (s, nullptr, 48000.0, provider);
  5173. lynx::RackRenderer r;
  5174. r.prepare (48000.0, 512);
  5175. std::vector<float> all;
  5176. std::vector<float> l (512), rr (512);
  5177. const auto tps = lynx::ticksPerSample (s.bpm, 48000.0);
  5178. for (double tick = 0.0; tick < lynx::ppq * 4.0; tick += 512 * tps)
  5179. {
  5180. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  5181. all.insert (all.end(), l.begin(), l.end());
  5182. }
  5183. CHECK (all[100] < 0.01f); // fade-in start
  5184. CHECK (std::abs (all[12000] - 0.25f) < 0.02f); // fade-in midpoint: half of 0.5
  5185. CHECK (std::abs (all[48000] - 0.5f) < 0.01f); // body: the clip gain
  5186. CHECK (std::abs (all[96000 - 12000] - 0.25f) < 0.02f); // fade-out midpoint
  5187. }
  5188. TEST (slicer_detects_hits_and_plays_slices_by_key)
  5189. {
  5190. // 4 hits (decaying noise) every 0.25 s
  5191. lynx::SampleData loop;
  5192. loop.sampleRate = 48000.0;
  5193. loop.left.assign (48000, 0.0f);
  5194. lynx::Noise noise;
  5195. for (int hit = 0; hit < 4; ++hit)
  5196. for (int i = 0; i < 6000; ++i)
  5197. loop.left[(std::size_t) (hit * 12000 + i)] = noise.next() * 0.8f * std::exp (-(float) i / 1500.0f);
  5198. const auto slices = lynx::detectSlices (loop, 0.5f);
  5199. CHECK (slices.size() == 4);
  5200. for (std::size_t i = 0; i < slices.size(); ++i)
  5201. CHECK (std::abs (slices[i] - 0.25f * (float) i) < 0.01f);
  5202. // key 61 plays the second slice, which starts at 0.25 and ends before 0.5
  5203. auto ramp = std::make_shared<lynx::SampleData>();
  5204. for (int i = 0; i < 48000; ++i)
  5205. ramp->left.push_back ((float) i / 48000.0f);
  5206. ramp->sampleRate = 48000.0;
  5207. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  5208. inst->prepare (48000.0);
  5209. lynx::ChannelDef c;
  5210. c.sampler.slices = slices;
  5211. inst->configure (c, 120.0);
  5212. inst->noteOn (61, 1.0f);
  5213. std::vector<float> l (20000), r (20000);
  5214. inst->render (l.data(), r.data(), 20000, 1.0f, 1.0f);
  5215. CHECK (std::abs (l[0] - 0.25f) < 0.01f && l[11000] > 0.45f && l[13000] == 0.0f);
  5216. }
  5217. TEST (project_templates_render_sound)
  5218. {
  5219. const auto names = lynx::templateNames();
  5220. CHECK (names.size() == 4);
  5221. for (int t = 1; t < (int) names.size(); ++t)
  5222. {
  5223. auto song = lynx::makeTemplate (t);
  5224. auto state = lynx::RackState::build (song, nullptr, 48000.0);
  5225. lynx::RackRenderer r;
  5226. r.prepare (48000.0, 512);
  5227. std::vector<float> l (512), rr (512);
  5228. const auto tps = lynx::ticksPerSample (song.bpm, 48000.0);
  5229. float peak = 0.0f;
  5230. for (double tick = 0.0; tick < lynx::ppq * 4.0; tick += 512 * tps)
  5231. {
  5232. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  5233. for (auto v : l)
  5234. peak = std::max (peak, std::abs (v));
  5235. }
  5236. CHECK (peak > 0.05f && peak < 4.0f);
  5237. }
  5238. CHECK (lynx::makeTemplate (0).patterns[0].notes.empty());
  5239. }
  5240. // --- interface translations (resources/i18n) -------------------------------------------------------
  5241. namespace i18ntest
  5242. {
  5243. std::string readFile (const std::filesystem::path& path)
  5244. {
  5245. std::ifstream in (path, std::ios::binary);
  5246. return { std::istreambuf_iterator<char> (in), std::istreambuf_iterator<char>() };
  5247. }
  5248. std::string unescape (const std::string& s)
  5249. {
  5250. std::string out;
  5251. for (std::size_t i = 0; i < s.size(); ++i)
  5252. {
  5253. if (s[i] == '\\' && i + 1 < s.size())
  5254. {
  5255. const auto n = s[++i];
  5256. out += n == 'n' ? '\n' : n == 't' ? '\t' : n;
  5257. }
  5258. else
  5259. out += s[i];
  5260. }
  5261. return out;
  5262. }
  5263. // reads a quoted string starting at s[pos] == '"'; returns the raw (escaped) contents and moves pos past the closing quote
  5264. bool quoted (const std::string& s, std::size_t& pos, std::string& raw)
  5265. {
  5266. if (pos >= s.size() || s[pos] != '"')
  5267. return false;
  5268. raw.clear();
  5269. for (++pos; pos < s.size() && s[pos] != '"' && s[pos] != '\n'; ++pos)
  5270. {
  5271. if (s[pos] == '\\' && pos + 1 < s.size())
  5272. raw += s[pos++];
  5273. raw += s[pos];
  5274. }
  5275. if (pos >= s.size() || s[pos] != '"')
  5276. return false;
  5277. ++pos;
  5278. return true;
  5279. }
  5280. // JUCE LocalisedStrings format: "english" = "translation"; duplicates are reported in `duplicates`
  5281. std::map<std::string, std::string> parseTable (const std::string& text, std::vector<std::string>& duplicates)
  5282. {
  5283. std::map<std::string, std::string> table;
  5284. std::istringstream lines (text);
  5285. std::string line;
  5286. while (std::getline (lines, line))
  5287. {
  5288. std::size_t pos = line.find_first_not_of (" \t");
  5289. std::string key, value;
  5290. if (pos == std::string::npos || ! quoted (line, pos, key))
  5291. continue;
  5292. pos = line.find ('"', pos);
  5293. if (pos == std::string::npos || ! quoted (line, pos, value))
  5294. value.clear();
  5295. const auto k = unescape (key);
  5296. if (table.count (k) != 0)
  5297. duplicates.push_back (k);
  5298. table[k] = unescape (value);
  5299. }
  5300. return table;
  5301. }
  5302. // string literals passed to tr() / TR_NOOP() in the sources (adjacent literals are concatenated)
  5303. std::map<std::string, std::string> sourceKeys (const std::filesystem::path& srcDir)
  5304. {
  5305. std::map<std::string, std::string> keys;
  5306. for (const auto& entry : std::filesystem::recursive_directory_iterator (srcDir))
  5307. {
  5308. const auto ext = entry.path().extension().string();
  5309. if (! entry.is_regular_file() || (ext != ".cpp" && ext != ".h"))
  5310. continue;
  5311. // drop // comment lines (they may quote tr ("...") as an example)
  5312. std::string text, line;
  5313. std::istringstream in (readFile (entry.path()));
  5314. while (std::getline (in, line))
  5315. {
  5316. const auto first = line.find_first_not_of (" \t");
  5317. text += (first != std::string::npos && line.compare (first, 2, "//") == 0) ? std::string() : line;
  5318. text += '\n';
  5319. }
  5320. auto isIdent = [] (char c) { return std::isalnum ((unsigned char) c) || c == '_'; };
  5321. for (const std::string name : { "tr", "TR_NOOP" })
  5322. for (auto at = text.find (name); at != std::string::npos; at = text.find (name, at + 1))
  5323. {
  5324. if ((at > 0 && isIdent (text[at - 1])) || isIdent (text[at + name.size()]))
  5325. continue;
  5326. auto pos = text.find_first_not_of (" \t\n", at + name.size());
  5327. if (pos == std::string::npos || text[pos] != '(')
  5328. continue;
  5329. pos = text.find_first_not_of (" \t\n", pos + 1);
  5330. std::string key, raw;
  5331. bool any = false;
  5332. while (pos != std::string::npos && quoted (text, pos, raw))
  5333. {
  5334. key += raw;
  5335. any = true;
  5336. pos = text.find_first_not_of (" \t\n", pos);
  5337. }
  5338. if (any)
  5339. keys.emplace (unescape (key), entry.path().filename().string());
  5340. }
  5341. }
  5342. return keys;
  5343. }
  5344. std::set<std::string> placeholders (const std::string& s)
  5345. {
  5346. std::set<std::string> found;
  5347. for (auto open = s.find ('{'); open != std::string::npos; open = s.find ('{', open + 1))
  5348. if (const auto close = s.find ('}', open); close != std::string::npos && close - open <= 3)
  5349. found.insert (s.substr (open, close - open + 1));
  5350. return found;
  5351. }
  5352. } // namespace i18ntest
  5353. TEST (translations_cover_every_ui_string)
  5354. {
  5355. using namespace i18ntest;
  5356. const auto text = readFile ("resources/i18n/pt-BR.txt");
  5357. CHECK (! text.empty());
  5358. std::vector<std::string> duplicates;
  5359. const auto table = parseTable (text, duplicates);
  5360. CHECK (table.size() > 300);
  5361. CHECK (duplicates.empty());
  5362. for (const auto& d : duplicates)
  5363. std::printf (" duplicate pt-BR entry: \"%s\"\n", d.c_str());
  5364. const auto keys = sourceKeys ("src");
  5365. CHECK (keys.size() > 300);
  5366. int missing = 0;
  5367. for (const auto& [key, file] : keys)
  5368. if (auto it = table.find (key); it == table.end() || it->second.empty())
  5369. {
  5370. ++missing;
  5371. std::printf (" no pt-BR translation for \"%s\" (%s)\n", key.c_str(), file.c_str());
  5372. }
  5373. CHECK (missing == 0);
  5374. // no empty values, and every {n} placeholder survives the translation
  5375. int empty = 0, badPlaceholders = 0;
  5376. for (const auto& [english, portuguese] : table)
  5377. {
  5378. empty += portuguese.empty() ? 1 : 0;
  5379. if (placeholders (english) != placeholders (portuguese))
  5380. {
  5381. ++badPlaceholders;
  5382. std::printf (" placeholders differ: \"%s\"\n", english.c_str());
  5383. }
  5384. }
  5385. CHECK (empty == 0);
  5386. CHECK (badPlaceholders == 0);
  5387. }
  5388. TEST (translations_embedded_table_is_up_to_date)
  5389. {
  5390. // src/ui/I18nData.h must be regenerated (tools/make_i18n.py) whenever the .txt changes
  5391. const auto text = i18ntest::readFile ("resources/i18n/pt-BR.txt");
  5392. CHECK (text.size() == sizeof (lynx::i18n::data::ptBR));
  5393. CHECK (std::memcmp (text.data(), lynx::i18n::data::ptBR, std::min (text.size(), sizeof (lynx::i18n::data::ptBR))) == 0);
  5394. }
  5395. // --- workflow shell: pattern/channel operations, key map, workspace layout, control math -------------
  5396. TEST (workflow_pattern_navigation_and_insert)
  5397. {
  5398. auto song = lynx::Song::makeDefault();
  5399. song.patterns.resize (3);
  5400. song.patterns[1].name = "B";
  5401. song.patterns[2].name = "C";
  5402. song.patterns[2].notes.push_back ({});
  5403. song.currentPattern = 0;
  5404. CHECK (lynx::stepPattern (song, 1) == 1 && lynx::stepPattern (song, -1) == 0 && lynx::stepPattern (song, 10) == 2);
  5405. // pattern 0 has the default notes, 1 is empty, 2 has a note
  5406. CHECK (! lynx::isPatternEmpty (song.patterns[0]) && lynx::isPatternEmpty (song.patterns[1]));
  5407. CHECK (lynx::findNextEmptyPattern (song, 0) == 1 && lynx::findNextEmptyPattern (song, 1) == -1);
  5408. CHECK (lynx::gotoNextEmptyPattern (song) == 1 && song.currentPattern == 1);
  5409. song.patterns[1].notes.push_back ({});
  5410. const auto before = song.patterns.size();
  5411. CHECK (lynx::gotoNextEmptyPattern (song) == (int) before && song.patterns.size() == before + 1);
  5412. CHECK (song.patterns.back().name == "Pattern 1" || song.patterns.back().name.rfind ("Pattern ", 0) == 0);
  5413. // insert keeps Playlist clips on their patterns
  5414. lynx::Clip clip;
  5415. clip.pattern = 2;
  5416. song.playlist = { clip };
  5417. song.arrangements.push_back ({ "Other", { clip }, 0, 0, {} });
  5418. CHECK (lynx::insertPattern (song, 1, "New") == 1 && song.currentPattern == 1);
  5419. CHECK (song.patterns[1].name == "New" && song.patterns[1].notes.empty() && song.patterns[3].name == "C");
  5420. CHECK (song.playlist[0].pattern == 3 && song.arrangements[1].clips[0].pattern == 3);
  5421. }
  5422. TEST (workflow_split_pattern_by_channel)
  5423. {
  5424. auto song = lynx::Song::makeDefault();
  5425. song.patterns.resize (1);
  5426. auto& p = song.patterns[0];
  5427. p.name = "Beat";
  5428. p.notes.clear();
  5429. for (int i = 0; i < 4; ++i)
  5430. p.notes.push_back ({ (std::int64_t) i * lynx::ticksPerStep, lynx::ticksPerStep, i % 2 == 0 ? 0 : 2, 60 });
  5431. song.channels[2].colour = 0xff112233;
  5432. lynx::Clip clip;
  5433. clip.pattern = 0;
  5434. song.playlist = { clip };
  5435. const auto created = lynx::splitPatternByChannel (song, 0);
  5436. CHECK (created == 2 && song.patterns.size() == 3);
  5437. CHECK (song.patterns[0].notes.size() == 4); // the original is unchanged
  5438. CHECK (song.patterns[1].name == "Beat - " + song.channels[0].name && song.patterns[1].notes.size() == 2);
  5439. CHECK (song.patterns[2].colour == 0xff112233u && song.patterns[2].notes.size() == 2 && song.patterns[2].notes[0].channel == 2);
  5440. CHECK (song.playlist[0].pattern == 0);
  5441. CHECK (lynx::splitPatternByChannel (song, 99) == 0);
  5442. }
  5443. TEST (workflow_channel_selection)
  5444. {
  5445. lynx::ChannelSelection s;
  5446. s.selectOnly (2);
  5447. s.toggle (5);
  5448. s.toggle (0);
  5449. CHECK ((s.rows == std::vector<int> { 0, 2, 5 }) && s.contains (5) && ! s.contains (3));
  5450. s.toggle (2);
  5451. CHECK ((s.rows == std::vector<int> { 0, 5 }));
  5452. s.selectOnly (3);
  5453. s.extendTo (6);
  5454. CHECK ((s.rows == std::vector<int> { 3, 4, 5, 6 }));
  5455. s.extendTo (1);
  5456. CHECK ((s.rows == std::vector<int> { 1, 2, 3 }));
  5457. s.selectOnly (4);
  5458. s.toggle (4); // the last one stays
  5459. CHECK ((s.rows == std::vector<int> { 4 }));
  5460. s.selectAll (3);
  5461. CHECK ((s.rows == std::vector<int> { 0, 1, 2 }));
  5462. s.rows = { 7, 9 };
  5463. s.clamp (5);
  5464. CHECK (s.rows.size() == 1 && s.rows[0] < 5);
  5465. }
  5466. TEST (workflow_move_clone_insert_channels)
  5467. {
  5468. auto song = lynx::Song::makeDefault();
  5469. const auto names = [&] {
  5470. std::vector<std::string> n;
  5471. for (const auto& c : song.channels)
  5472. n.push_back (c.name);
  5473. return n;
  5474. };
  5475. const auto original = names();
  5476. const auto count = (int) song.channels.size();
  5477. CHECK (count >= 4);
  5478. // references that must follow their channel
  5479. song.patterns[0].notes.push_back ({ 0, 240, 1, 64 });
  5480. lynx::Automation a;
  5481. a.target = { lynx::AutomationTargetKind::channelVolume, 1 };
  5482. song.automations.push_back (a);
  5483. song.midiLinks.push_back ({ 7, -1, { lynx::AutomationTargetKind::channelPan, 3 } });
  5484. lynx::Clip audio;
  5485. audio.kind = lynx::ClipKind::audio;
  5486. audio.channel = 3;
  5487. song.playlist.push_back (audio);
  5488. std::vector<int> selection { 1, 3 };
  5489. CHECK (lynx::moveChannels (song, selection, -1));
  5490. CHECK ((selection == std::vector<int> { 0, 2 }));
  5491. CHECK (song.channels[0].name == original[1] && song.channels[1].name == original[0] && song.channels[2].name == original[3]
  5492. && song.channels[3].name == original[2]);
  5493. CHECK (song.automations[0].target.index == 0 && song.midiLinks[0].target.index == 2 && song.playlist.back().channel == 2);
  5494. CHECK (std::any_of (song.patterns[0].notes.begin(), song.patterns[0].notes.end(),
  5495. [] (const lynx::Note& n) { return n.channel == 0 && n.key == 64; }));
  5496. CHECK (! lynx::moveChannels (song, selection, -1)); // the block is at the top
  5497. CHECK (lynx::moveChannels (song, selection, 1) && (selection == std::vector<int> { 1, 3 }) && names() == original);
  5498. // clone: settings, not notes, right below, with a new name
  5499. song.channels[1].volume = 0.33f;
  5500. const auto notesBefore = song.patterns[0].notes.size();
  5501. const auto clone = lynx::cloneChannel (song, 1);
  5502. CHECK (clone == 2 && (int) song.channels.size() == count + 1);
  5503. CHECK (song.channels[2].name == original[1] + " #2" && song.channels[2].volume == 0.33f);
  5504. CHECK (song.patterns[0].notes.size() == notesBefore);
  5505. CHECK (song.channels[3].name == original[2] && song.automations[0].target.index == 1);
  5506. CHECK (lynx::cloneChannel (song, 1) == 2 && song.channels[2].name == original[1] + " #3");
  5507. // remove several
  5508. CHECK (lynx::removeChannels (song, { 2, 3, 2 }) == 2 && (int) song.channels.size() == count);
  5509. CHECK (names() == original);
  5510. }
  5511. TEST (workflow_send_channels_to_free_inserts)
  5512. {
  5513. auto song = lynx::Song::makeDefault();
  5514. for (auto& c : song.channels)
  5515. c.mixerTrack = 0;
  5516. song.mixer[1].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::delay)); // not free: has effects
  5517. song.channels[0].mixerTrack = 2; // not free: used
  5518. song.setRoute (5, 3, true); // 3 is not free: receives a send
  5519. CHECK (! lynx::isInsertFree (song, 1) && ! lynx::isInsertFree (song, 2) && ! lynx::isInsertFree (song, 3) && lynx::isInsertFree (song, 4));
  5520. song.channels[1].colour = 0xff445566;
  5521. const auto tracks = lynx::sendChannelsToFreeInserts (song, { 1, 2 });
  5522. CHECK ((tracks == std::vector<int> { 4, 5 }) || (tracks == std::vector<int> { 4, 6 }));
  5523. CHECK (song.channels[1].mixerTrack == 4 && song.mixer[4].name == song.channels[1].name && song.mixer[4].colour == 0xff445566u);
  5524. CHECK (song.mixer[(std::size_t) tracks[1]].name == song.channels[2].name);
  5525. // with no free insert left, a new one is added at the end
  5526. auto full = lynx::Song::makeDefault();
  5527. full.mixer = lynx::Song::defaultMixer (1);
  5528. full.channels[0].mixerTrack = 1;
  5529. const auto added = lynx::sendChannelsToFreeInserts (full, { 1 });
  5530. CHECK (added.size() == 1 && added[0] == 2 && full.mixer.size() == 3 && full.channels[1].mixerTrack == 2);
  5531. }
  5532. TEST (workflow_palette_random_colour)
  5533. {
  5534. const auto& palette = lynx::lynxPalette();
  5535. for (std::uint32_t seed = 0; seed < 50; ++seed)
  5536. {
  5537. const auto current = palette[seed % palette.size()];
  5538. const auto c = lynx::randomPaletteColour (current, seed);
  5539. CHECK (c != current && std::find (palette.begin(), palette.end(), c) != palette.end());
  5540. }
  5541. }
  5542. TEST (workflow_key_map)
  5543. {
  5544. using namespace lynx::keys;
  5545. KeyMap map;
  5546. const auto global = Context::global, rack = Context::channelRack;
  5547. CHECK (map.find ({ space, none }, global, true) == "transport.playStop");
  5548. CHECK (map.find ({ space, ctrl }, global, true) == "transport.pause");
  5549. CHECK (map.find ({ f6, none }, global, true) == "panel.channelRack");
  5550. CHECK (map.find ({ f8, alt }, global, false) == "panel.browser");
  5551. CHECK (map.find ({ insert, ctrl | shift }, global, false) == "pattern.insert");
  5552. CHECK (map.find ({ numpadPlus, none }, rack, true) == "pattern.next");
  5553. // panel context: only while focused
  5554. CHECK (map.find ({ 'L', ctrl }, rack, true) == "rack.sendToInserts");
  5555. CHECK (map.find ({ 'L', ctrl }, global, true).empty());
  5556. CHECK (map.find ({ up, alt }, rack, false) == "rack.moveUp");
  5557. // typing piano: note keys play, other plain keys only for whilePiano actions; chords always work
  5558. CHECK (KeyMap::playsNote ({ 'Z', none }, true) && ! KeyMap::playsNote ({ 'Z', none }, false) && ! KeyMap::playsNote ({ 'Z', ctrl }, true));
  5559. CHECK (map.find ({ 'L', none }, global, true) == "transport.toggleMode");
  5560. CHECK (map.find ({ '1', none }, rack, false) == "rack.mute1" && map.find ({ '0', none }, rack, false) == "rack.mute10");
  5561. CHECK (map.find ({ '1', none }, rack, true).empty()); // a plain digit is not a shortcut while the piano is on
  5562. CHECK (map.find ({ '2', ctrl }, rack, true) == "rack.solo2");
  5563. CHECK (typingPianoNote ('Z') == 60 && typingPianoNote ('q') == 72 && typingPianoNote ('P') == 88 && typingPianoNote ('L') == -1);
  5564. CHECK (map.conflicts().empty());
  5565. // chord text
  5566. CHECK (toString ({ f4, ctrl | shift }) == "Ctrl+Shift+F4" && toString ({ numpadMinus, none }) == "Numpad -");
  5567. CHECK (parseChord ("Shift+Ctrl+Ins") == (Chord { insert, ctrl | shift }));
  5568. CHECK (parseChord ("alt+up") == (Chord { up, alt }) && parseChord ("k") == (Chord { 'K', none }));
  5569. CHECK (parseChord ("Numpad +") == (Chord { numpadPlus, none }) && ! parseChord ("F13") && ! parseChord ("Ctrl+"));
  5570. for (const auto& a : actions())
  5571. for (const auto& c : a.defaults)
  5572. CHECK (parseChord (toString (c)) == c);
  5573. // JSON overrides
  5574. std::vector<std::string> errors;
  5575. CHECK (map.loadJson (R"({ "pattern.next": "Ctrl+Right", "rack.clone": ["Alt+D", "Ctrl+D"], "help.shortcuts": "", "nope": "F1", "file.open": "Hyper+Q" })", &errors));
  5576. CHECK (errors.size() == 2);
  5577. CHECK (map.find ({ right, ctrl }, global, false) == "pattern.next" && map.find ({ numpadPlus, none }, global, false).empty());
  5578. CHECK (map.find ({ 'D', ctrl }, rack, false) == "rack.clone" && map.find ({ f1, none }, global, false).empty());
  5579. map.setChords ("pattern.previous", { { right, ctrl } });
  5580. CHECK (map.conflicts().size() == 1);
  5581. KeyMap reloaded;
  5582. CHECK (reloaded.loadJson (map.toJson()) && reloaded.chordsFor ("rack.clone") == map.chordsFor ("rack.clone"));
  5583. CHECK (! map.loadJson ("[1, 2]") && ! map.loadJson ("{\"a\": }"));
  5584. map.resetToDefaults();
  5585. CHECK (map.find ({ numpadPlus, none }, global, false) == "pattern.next");
  5586. }
  5587. TEST (workflow_workspace_layout)
  5588. {
  5589. using namespace lynx::workspace;
  5590. auto l = Layout::makeDefault (1280, 620);
  5591. // every visible panel is inside the desktop; the default shows Browser, Playlist and Channel Rack
  5592. for (const auto& p : l.panels)
  5593. CHECK (p.bounds.x >= 0 && p.bounds.y >= 0 && p.bounds.right() <= 1280 && p.bounds.bottom() <= 620 && p.bounds.w >= minWidth);
  5594. CHECK (l[Panel::browser].visible && l[Panel::playlist].visible && l[Panel::channelRack].visible);
  5595. CHECK (! l[Panel::pianoRoll].visible && ! l[Panel::mixer].visible);
  5596. CHECK (l[Panel::playlist].bounds.x == l[Panel::browser].bounds.right() && l[Panel::playlist].bounds.right() == 1280);
  5597. CHECK (l.isFrontmost (Panel::channelRack) && l.focused == Panel::channelRack);
  5598. // toggle: show+focus, focus, hide
  5599. CHECK (l.toggle (Panel::mixer) && l.focused == Panel::mixer && l.isFrontmost (Panel::mixer));
  5600. CHECK (l.toggle (Panel::channelRack) && l.focused == Panel::channelRack && l.isFrontmost (Panel::channelRack));
  5601. CHECK (! l.toggle (Panel::channelRack) && ! l[Panel::channelRack].visible && l.focused == Panel::mixer);
  5602. l.toggleMaximised (Panel::playlist);
  5603. CHECK (l[Panel::playlist].maximised && l.focused == Panel::playlist);
  5604. // save and load at another size: positions scale
  5605. l[Panel::mixer].detached = true;
  5606. l[Panel::mixer].windowBounds = { 100, 50, 900, 500 };
  5607. const auto text = l.serialise();
  5608. const auto big = Layout::parse (text, 2560, 1240);
  5609. CHECK (big.has_value());
  5610. if (big)
  5611. {
  5612. CHECK ((*big)[Panel::playlist].maximised && (*big)[Panel::mixer].detached && (*big)[Panel::mixer].windowBounds.w == 900);
  5613. CHECK (std::abs ((*big)[Panel::playlist].bounds.x - 2 * l[Panel::playlist].bounds.x) <= 1);
  5614. CHECK (big->order == l.order && big->focused == l.focused);
  5615. }
  5616. 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));
  5617. // resize keeps panels inside and edges glued to the border
  5618. l.resize (1000, 500);
  5619. CHECK (l[Panel::playlist].bounds.right() == 1000 && l[Panel::browser].bounds.bottom() == 500);
  5620. for (const auto& p : l.panels)
  5621. CHECK (p.bounds.right() <= 1000 && p.bounds.bottom() <= 500);
  5622. // off-screen panels come back
  5623. l[Panel::pianoRoll].bounds = { 5000, -300, 400, 300 };
  5624. CHECK (l.bringBackOffscreen() == 1 && l[Panel::pianoRoll].bounds.x + 400 <= 1000 && l[Panel::pianoRoll].bounds.y == 0);
  5625. // snapping: to the desktop, to another panel, and resizing one edge
  5626. CHECK ((snap ({ 5, 6, 200, 100 }, {}, 1000, 500) == Rect { 0, 0, 200, 100 }));
  5627. CHECK ((snap ({ 304, 50, 200, 100 }, { { 0, 0, 300, 400 } }, 1000, 500) == Rect { 300, 50, 200, 100 }));
  5628. CHECK ((snap ({ 304, 50, 200, 100 }, { { 0, 0, 300, 400 } }, 1000, 500, 2) == Rect { 304, 50, 200, 100 })); // out of range
  5629. CHECK ((snap ({ 100, 100, 195, 100 }, { { 300, 0, 300, 400 } }, 1000, 500, 8, right) == Rect { 100, 100, 200, 100 }));
  5630. CHECK ((snap ({ 100, 100, 195, 100 }, { { 300, 1000, 300, 400 } }, 2000, 2000, 8, right) == Rect { 100, 100, 195, 100 })); // not facing
  5631. }
  5632. TEST (workflow_control_math)
  5633. {
  5634. using namespace lynx::controls;
  5635. CHECK (std::abs (dragValue (0.5, 75, 0, 1, 150, false) - 1.0) < 1e-9);
  5636. CHECK (std::abs (dragValue (0.5, 75, 0, 1, 150, true) - 0.55) < 1e-9);
  5637. CHECK (dragValue (0.5, -1000, -1, 1, 150, false) == -1.0);
  5638. 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);
  5639. CHECK (wheelValue (3, 1, 0, 10, true, 1) == 4.0); // integer controls move at least one step
  5640. CHECK (std::abs (snapToInterval (0.234, 0, 0.1) - 0.2) < 1e-9);
  5641. CHECK (tempoDrag (130.0, 8, false, false) == 132.0 && tempoDrag (130.0, -3, false, false) == 130.0);
  5642. CHECK (std::abs (tempoDrag (130.0, 25, true, false) - 130.25) < 1e-9);
  5643. CHECK (std::abs (tempoDrag (130.0, 8, false, true) - 130.2) < 1e-9);
  5644. CHECK (tempoDrag (520.0, 400, false, false) == lynx::maxTempo && halfTempo (15.0) == lynx::minTempo && doubleTempo (130.0) == 260.0);
  5645. TapTempo tap;
  5646. CHECK (! tap.tap (1000.0));
  5647. CHECK (tap.tap (1500.0) == 120.0);
  5648. CHECK (tap.tap (2000.0) == 120.0 && tap.count() == 3);
  5649. CHECK (std::abs (*tap.tap (2400.0) - 128.57) < 0.01); // average of 500, 500, 400 ms
  5650. CHECK (! tap.tap (9000.0) && tap.count() == 1); // a long pause starts over
  5651. }
  5652. int main (int argc, char** argv)
  5653. {
  5654. if (argc > 1)
  5655. realFlpDir = argv[1];
  5656. const char* only = std::getenv ("LYNX_TEST"); // run only the tests whose name contains this
  5657. for (auto& [name, fn] : registry())
  5658. {
  5659. if (only != nullptr && std::strstr (name, only) == nullptr)
  5660. continue;
  5661. std::printf ("%s\n", name);
  5662. fn();
  5663. }
  5664. std::printf ("\n%d checks, %d failures\n", checks, failures);
  5665. return failures == 0 ? 0 : 1;
  5666. }
  5667. TEST (synth_cutoff_keytrack)
  5668. {
  5669. // the cutoff follows the note from key 48 (130.81 Hz), like Harmor's "Key to filter frequency"
  5670. auto rms = [] (float keyTrack, int key) {
  5671. lynx::SynthSettings s;
  5672. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  5673. s.cutoff = 130.81f;
  5674. s.keyTrack = keyTrack;
  5675. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  5676. inst->prepare (48000.0);
  5677. lynx::ChannelDef c;
  5678. c.synth = s;
  5679. inst->configure (c, 120.0);
  5680. inst->noteOn (key, 1.0f);
  5681. std::vector<float> l (24000), r (24000);
  5682. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  5683. double e = 0.0;
  5684. for (int i = 4800; i < 24000; ++i)
  5685. e += (double) l[(std::size_t) i] * l[(std::size_t) i];
  5686. return std::sqrt (e / 19200.0);
  5687. };
  5688. const auto fixed = rms (0.0f, 72), tracked = rms (1.0f, 72);
  5689. CHECK (tracked > 4.0 * fixed); // 2 octaves up: without keytrack the low-pass cuts
  5690. CHECK (std::abs (rms (1.0f, 48) - rms (0.0f, 48)) < 0.05 * rms (0.0f, 48)); // unchanged at the reference key
  5691. }
  5692. namespace
  5693. {
  5694. std::vector<float> renderSynthNote (const lynx::SynthSettings& s, int key, int frames)
  5695. {
  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 (key, 1.0f);
  5702. std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
  5703. inst->render (l.data(), r.data(), frames, 1.0f, 1.0f);
  5704. return l;
  5705. }
  5706. double goertzel (const std::vector<float>& x, double f, int from, int to)
  5707. {
  5708. const auto w = 2.0 * 3.141592653589793 * f / 48000.0, k = 2.0 * std::cos (w);
  5709. double s1 = 0.0, s2 = 0.0;
  5710. for (int i = from; i < to; ++i)
  5711. {
  5712. const auto s0 = x[(std::size_t) i] + k * s1 - s2;
  5713. s2 = s1;
  5714. s1 = s0;
  5715. }
  5716. return s1 * s1 + s2 * s2 - k * s1 * s2;
  5717. }
  5718. } // namespace
  5719. TEST (synth_pitch_sweep_ring_and_bands)
  5720. {
  5721. lynx::SynthSettings sine;
  5722. sine.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  5723. sine.cutoff = 20000.0f;
  5724. // sweep: starts at f·(1 + sweep) and returns to f (time constant s = 10 ms; s⁴ falls 4× faster)
  5725. auto sweep = sine;
  5726. sweep.sweep = 1.0f;
  5727. sweep.sweepTime = 0.01f;
  5728. const auto a = renderSynthNote (sweep, 69, 24000);
  5729. auto crossings = [] (const std::vector<float>& x, int to) {
  5730. int n = 0;
  5731. for (int i = 1; i < to; ++i)
  5732. n += (x[(std::size_t) i - 1] < 0.0f) != (x[(std::size_t) i] < 0.0f);
  5733. return n;
  5734. };
  5735. CHECK (crossings (a, 480) > 1.3 * crossings (renderSynthNote (sine, 69, 480), 480)); // 10 ms: much higher
  5736. CHECK (goertzel (a, 440.0, 12000, 24000) > 100.0 * goertzel (a, 880.0, 12000, 24000));
  5737. // ring with |a| = 1: carrier 440 × modulator 110 -> only 330 and 550, no 440
  5738. auto ring = sine;
  5739. ring.am = ring.ring = true;
  5740. ring.osc[2] = { lynx::Waveform::sine, -2, 0.0f, 1.0f };
  5741. const auto b = renderSynthNote (ring, 69, 24000);
  5742. CHECK (goertzel (b, 330.0, 4800, 24000) > 100.0 * goertzel (b, 440.0, 4800, 24000));
  5743. CHECK (goertzel (b, 550.0, 4800, 24000) > 100.0 * goertzel (b, 440.0, 4800, 24000));
  5744. // 3 bands: muting the high knocks down a 4 kHz note and barely touches an 80 Hz one
  5745. auto bands = sine;
  5746. bands.bandGain = { 1.0f, 1.0f, 0.0f };
  5747. auto rms = [] (const std::vector<float>& x) {
  5748. double e = 0.0;
  5749. for (std::size_t i = 4800; i < x.size(); ++i)
  5750. e += (double) x[i] * x[i];
  5751. return std::sqrt (e / (double) (x.size() - 4800));
  5752. };
  5753. CHECK (rms (renderSynthNote (bands, 107, 24000)) < 0.3 * rms (renderSynthNote (sine, 107, 24000)));
  5754. CHECK (rms (renderSynthNote (bands, 28, 24000)) > 0.85 * rms (renderSynthNote (sine, 28, 24000)));
  5755. }
  5756. TEST (love_philter_map_evaluation)
  5757. {
  5758. lynx::LovePhilterMap line { { { 0.0, 0.0 }, { 1.0, 1.0 } } };
  5759. CHECK (std::abs (lynx::lovePhilterMapAt (line, 0.5) - 0.5) < 1e-9);
  5760. CHECK (std::abs (lynx::lovePhilterMapAt (line, 1.0) - 1.0) < 1e-9);
  5761. // steps (Δx = 0): 1 until 0.4375, ramp to 0 at 0.5, then 0
  5762. lynx::LovePhilterMap steps { { { 0.0, 1.0 }, { 0.4375, 1.0 }, { 0.0625, 0.0 }, { 0.5, 0.0 } } };
  5763. CHECK (lynx::lovePhilterMapAt (steps, 0.27) == 1.0);
  5764. CHECK (lynx::lovePhilterMapAt (steps, 0.75) == 0.0);
  5765. CHECK (std::abs (lynx::lovePhilterMapAt (steps, 0.46875) - 0.5) < 1e-9);
  5766. CHECK (lynx::lovePhilterMapAt ({}, 0.3) == 1.0);
  5767. }
  5768. TEST (lofi_holds_and_quantizes)
  5769. {
  5770. auto fx = lynx::makeEffect (lynx::EffectKind::lofi);
  5771. fx->prepare (48000.0);
  5772. auto slot = lynx::makeEffectSlot (lynx::EffectKind::lofi);
  5773. slot.params[0] = 0.5f; // 128·0.125 + 1 = 17 samples
  5774. slot.params[1] = 1.0f; // 65536/2049 ≈ 32 levels
  5775. std::vector<float> l (170), r (170);
  5776. for (std::size_t i = 0; i < l.size(); ++i)
  5777. l[i] = r[i] = 0.9f * (float) std::sin (0.05 * (double) i);
  5778. const auto original = l;
  5779. fx->process (l.data(), r.data(), 170, slot.params, 120.0);
  5780. for (int i = 0; i < 17; ++i)
  5781. CHECK (l[(std::size_t) i] == l[0]);
  5782. CHECK (l[17] != l[16]);
  5783. const auto step = 2.0f * 2049.0f / 65536.0f;
  5784. for (auto v : l)
  5785. CHECK (std::abs (v / step - std::round (v / step)) < 1e-3f);
  5786. CHECK (std::abs (l[0] - original[0]) <= step);
  5787. CHECK (lynx::formatEffectParam (lynx::EffectKind::lofi, 1, 0.0f, 120.0) == "16.0 bits");
  5788. }
  5789. namespace
  5790. {
  5791. lynx::EffectParams effectorParams (int type, float x, float y)
  5792. {
  5793. auto slot = lynx::makeEffectSlot (lynx::EffectKind::effector);
  5794. slot.params[0] = (float) type / 11.0f;
  5795. slot.params[3] = x;
  5796. slot.params[4] = y;
  5797. return slot.params;
  5798. }
  5799. } // namespace
  5800. TEST (effector_algorithms)
  5801. {
  5802. CHECK (lynx::effector::typeOf (8.0f / 11.0f) == 8);
  5803. CHECK (std::string (lynx::effector::typeName (11)) == "Ring");
  5804. CHECK (std::abs (lynx::effector::rateBeats (0.5f, 0.0f) - 0.75) < 1e-9); // trunc(8·0.5 + 0.5) = 4 -> 3/4
  5805. CHECK (std::abs (lynx::effector::rateBeats (1.0f, 0.5f) - 2.0) < 1e-9); // 8 × (1 − 0.5)²
  5806. auto run = [] (const lynx::EffectParams& p, std::vector<float> in, double bpm = 120.0) {
  5807. auto fx = lynx::makeEffect (lynx::EffectKind::effector);
  5808. fx->prepare (48000.0);
  5809. fx->setSongPosition (0.0);
  5810. auto r = in;
  5811. fx->process (in.data(), r.data(), (int) in.size(), p, bpm);
  5812. return in;
  5813. };
  5814. // Distort with Y = 1 (everything goes to the cascade): 0.5 comes out 0.5 (compensation G = 0.5/F(0.5))
  5815. {
  5816. auto p = effectorParams (0, 0.5f, 1.0f);
  5817. const auto out = run (p, std::vector<float> (4800, 0.5f));
  5818. CHECK (std::abs (out.back() - 0.5f) < 1e-3f);
  5819. const auto small = run (p, std::vector<float> (4800, 0.05f));
  5820. CHECK (small.back() > 0.45f && small.back() < 0.5f); // saturates near ±0.5
  5821. }
  5822. // Ring: DC 1 becomes the carrier sine, 2^(8.7·0 + 5) = 32 Hz -> 32 cycles per second
  5823. {
  5824. auto p = effectorParams (11, 0.0f, 0.0f);
  5825. const auto out = run (p, std::vector<float> (48000, 1.0f));
  5826. int crossings = 0;
  5827. for (std::size_t i = 4800; i < out.size(); ++i)
  5828. crossings += (out[i - 1] < 0.0f) != (out[i] < 0.0f);
  5829. CHECK (crossings >= 56 && crossings <= 60); // 0.9 s × 32 Hz × 2
  5830. }
  5831. // Trancer, Y = 0: at the start of the period the gain drops to zero; X = 1 returns only at the end of the period
  5832. {
  5833. auto p = effectorParams (8, 1.0f, 0.0f);
  5834. p[5] = 1.0f; // 8/1 = 8 beats = 4 s at 120 BPM
  5835. const auto out = run (p, std::vector<float> (48000, 1.0f));
  5836. CHECK (out[100] > 0.3f); // still rising on the "and" (1% of the period = 40 ms)
  5837. CHECK (out[24000] < 0.05f); // 0.5 s: trough
  5838. }
  5839. // Reverb with Wet = 0: the dry passes intact
  5840. {
  5841. auto p = effectorParams (6, 0.5f, 0.5f);
  5842. p[2] = 0.0f;
  5843. std::vector<float> in (4800);
  5844. for (std::size_t i = 0; i < in.size(); ++i)
  5845. in[i] = (float) std::sin (0.01 * (double) i);
  5846. const auto out = run (p, in);
  5847. for (std::size_t i = 0; i < in.size(); ++i)
  5848. CHECK (std::abs (out[i] - in[i]) < 1e-6f);
  5849. }
  5850. // Delay: the echo arrives after the Mod Rate period (1/4 beat at 120 BPM = 125 ms), at level X²
  5851. {
  5852. auto p = effectorParams (5, 0.7f, 0.5f);
  5853. p[5] = 1.0f / 8.0f; // index 1 = 1/4
  5854. std::vector<float> in (12000, 0.0f);
  5855. in[0] = 1.0f;
  5856. const auto out = run (p, in);
  5857. CHECK (std::abs (out[6000] - 0.49f) < 0.02f);
  5858. CHECK (std::abs (out[3000]) < 1e-6f);
  5859. }
  5860. // Stutter: repeats the recorded start every period
  5861. {
  5862. auto p = effectorParams (9, 0.0f, 0.0f);
  5863. p[5] = 0.0f; // 1/8 beat = 62.5 ms = 3000 samples
  5864. std::vector<float> in (9000);
  5865. for (std::size_t i = 0; i < in.size(); ++i)
  5866. in[i] = (float) i / 9000.0f;
  5867. const auto out = run (p, in);
  5868. CHECK (std::abs (out[3000 + 500] - in[500]) < 0.01f); // after the crossfade, it's the start again
  5869. CHECK (std::abs (out[1000] - in[1000]) < 1e-6f);
  5870. }
  5871. }
  5872. TEST (crossover_linkwitz_riley_sums_flat)
  5873. {
  5874. // low band + high band of the same crossover: sum with magnitude 1 (Linkwitz-Riley; for 12 and 36 dB the HP is inverted)
  5875. for (const auto combo : { 1, 3, 4, 5, 6, 7 })
  5876. for (const auto testHz : { 200.0, 1000.0, 5000.0 })
  5877. {
  5878. auto run = [&] (bool high) {
  5879. auto fx = lynx::makeEffect (lynx::EffectKind::crossover);
  5880. fx->prepare (48000.0);
  5881. auto slot = lynx::makeEffectSlot (lynx::EffectKind::crossover);
  5882. const auto value = (float) (std::log2 (1000.0 / 30.0) / 9.0589);
  5883. slot.params[high ? 0 : 3] = 1.0f;
  5884. slot.params[high ? 1 : 4] = value;
  5885. slot.params[high ? 2 : 5] = (float) combo / 7.0f;
  5886. std::vector<float> l (24000), r (24000);
  5887. for (std::size_t i = 0; i < l.size(); ++i)
  5888. l[i] = r[i] = (float) std::sin (2.0 * 3.141592653589793 * testHz * (double) i / 48000.0);
  5889. fx->process (l.data(), r.data(), 24000, slot.params, 120.0);
  5890. return l;
  5891. };
  5892. const auto low = run (false), high = run (true);
  5893. double peak = 0.0;
  5894. for (std::size_t i = 12000; i < low.size(); ++i)
  5895. peak = std::max (peak, (double) std::abs (low[i] + high[i]));
  5896. CHECK (std::abs (peak - 1.0) < 0.02);
  5897. }
  5898. // 96 dB/oct: two octaves above the cutoff the low-pass is already below −150 dB... in practice, very low
  5899. auto fx = lynx::makeEffect (lynx::EffectKind::crossover);
  5900. fx->prepare (48000.0);
  5901. auto slot = lynx::makeEffectSlot (lynx::EffectKind::crossover);
  5902. slot.params[3] = 1.0f;
  5903. slot.params[4] = (float) (std::log2 (500.0 / 30.0) / 9.0589);
  5904. slot.params[5] = 1.0f;
  5905. std::vector<float> l (24000), r (24000);
  5906. for (std::size_t i = 0; i < l.size(); ++i)
  5907. l[i] = r[i] = (float) std::sin (2.0 * 3.141592653589793 * 2000.0 * (double) i / 48000.0);
  5908. fx->process (l.data(), r.data(), 24000, slot.params, 120.0);
  5909. float peak = 0.0f;
  5910. for (std::size_t i = 12000; i < l.size(); ++i)
  5911. peak = std::max (peak, std::abs (l[i]));
  5912. CHECK (peak < 1e-4f);
  5913. }
  5914. TEST (rhythm_gain_and_autopan)
  5915. {
  5916. // 1-beat gate: 1 → 0 at 0.5 beat (linear) → 1 at the end; at 120 BPM 1 beat = 24000 samples at 48 kHz
  5917. auto slot = lynx::makeEffectSlot (lynx::EffectKind::rhythmGain);
  5918. slot.params[0] = 1.0f / 64.0f;
  5919. slot.params[1] = 3.0f / 12.0f;
  5920. const float pts[3][3] = { { 0.0f, 1.0f, 0.5f }, { 0.5f, 0.0f, 0.5f }, { 1.0f, 1.0f, 0.5f } };
  5921. for (int k = 0; k < 3; ++k)
  5922. for (int j = 0; j < 3; ++j)
  5923. slot.params[(std::size_t) (2 + 3 * k + j)] = pts[k][j];
  5924. auto fx = lynx::makeEffect (lynx::EffectKind::rhythmGain);
  5925. fx->prepare (48000.0);
  5926. fx->setSongPosition (960.0 * 2.0); // beat 2: phase 0
  5927. std::vector<float> l (24000, 1.0f), r (24000, 1.0f);
  5928. fx->process (l.data(), r.data(), 24000, slot.params, 120.0);
  5929. CHECK (std::abs (l[0] - 1.0f) < 1e-3f);
  5930. CHECK (std::abs (l[6000] - 0.5f) < 1e-3f);
  5931. CHECK (l[12000] < 1e-3f);
  5932. CHECK (std::abs (l[18000] - 0.5f) < 1e-3f);
  5933. // positive tension: changes quickly at the start of the segment
  5934. CHECK (lynx::effectInfo (lynx::EffectKind::rhythmGain).params.size() == 39);
  5935. // auto-pan: 1-beat period, full depth; start phase 0.75 -> W = 0 rising (pan centered)
  5936. auto pan = lynx::makeEffectSlot (lynx::EffectKind::autoPan);
  5937. pan.params[0] = 1.0f / 64.0f;
  5938. pan.params[1] = 1.0f;
  5939. auto ap = lynx::makeEffect (lynx::EffectKind::autoPan);
  5940. ap->prepare (48000.0);
  5941. ap->setSongPosition (0.0);
  5942. std::vector<float> a (24000, 1.0f), b (24000, 1.0f);
  5943. ap->process (a.data(), b.data(), 24000, pan.params, 120.0);
  5944. CHECK (std::abs (a[0] - 1.0f) < 1e-3f && std::abs (b[0] - 1.0f) < 1e-3f);
  5945. CHECK (a[6000] < 0.01f && std::abs (b[6000] - 1.0f) < 1e-3f); // ¼ cycle later: fully right
  5946. }
  5947. TEST (vocoder_side_inputs_split_modulator_and_carrier)
  5948. {
  5949. // "Side inputs" mode (Vocodex): modulator from side input 1 (noise in the first half), carrier from side input 2
  5950. // (saw), own input silent → the output follows the side modulator; with input 1 silent, the output is silent
  5951. auto slot = lynx::makeEffectSlot (lynx::EffectKind::vocoder);
  5952. slot.params[16] = 1.0f;
  5953. auto fx = lynx::makeEffect (lynx::EffectKind::vocoder);
  5954. fx->prepare (48000.0);
  5955. std::vector<float> modL (48000), car (48000), l (48000, 0.0f), r (48000, 0.0f);
  5956. std::uint32_t seed = 1;
  5957. for (std::size_t i = 0; i < modL.size(); ++i)
  5958. {
  5959. seed = seed * 1664525u + 1013904223u;
  5960. modL[i] = i < 24000 ? ((float) (seed >> 8) / 16777216.0f - 0.5f) : 0.0f;
  5961. car[i] = (float) (2.0 * std::fmod ((double) i * 220.0 / 48000.0, 1.0) - 1.0) * 0.5f;
  5962. }
  5963. for (int done = 0; done < 48000; done += 480)
  5964. {
  5965. fx->setSidechain (modL.data() + done, modL.data() + done);
  5966. fx->setSidechain2 (car.data() + done, car.data() + done);
  5967. fx->process (l.data() + done, r.data() + done, 480, slot.params, 120.0);
  5968. }
  5969. auto rms = [&] (int from, int to) {
  5970. double e = 0.0;
  5971. for (int i = from; i < to; ++i)
  5972. e += (double) l[(std::size_t) i] * l[(std::size_t) i];
  5973. return std::sqrt (e / (to - from));
  5974. };
  5975. CHECK (rms (6000, 20000) > 0.01);
  5976. CHECK (rms (40000, 48000) < 0.1 * rms (6000, 20000));
  5977. // the side modulator configured but silent: nothing comes out (the own input is not used instead)
  5978. std::vector<float> a (4800, 0.3f), b (4800, 0.3f);
  5979. for (int done = 0; done < 4800; done += 480)
  5980. {
  5981. fx->setSidechain (nullptr, nullptr);
  5982. fx->setSidechain2 (car.data() + done, car.data() + done);
  5983. fx->process (a.data() + done, b.data() + done, 480, slot.params, 120.0);
  5984. }
  5985. CHECK (std::abs (a[4700]) < 0.05f);
  5986. }
  5987. TEST (vocoder_follows_modulator)
  5988. {
  5989. // modulator (L) plays only in the first half; carrier (R) is a continuous saw: the wet follows the modulator
  5990. auto slot = lynx::makeEffectSlot (lynx::EffectKind::vocoder);
  5991. auto fx = lynx::makeEffect (lynx::EffectKind::vocoder);
  5992. fx->prepare (48000.0);
  5993. std::vector<float> l (48000), r (48000);
  5994. std::uint32_t seed = 1;
  5995. for (std::size_t i = 0; i < l.size(); ++i)
  5996. {
  5997. seed = seed * 1664525u + 1013904223u;
  5998. l[i] = i < 24000 ? ((float) (seed >> 8) / 16777216.0f - 0.5f) : 0.0f;
  5999. r[i] = (float) (2.0 * std::fmod ((double) i * 220.0 / 48000.0, 1.0) - 1.0) * 0.5f;
  6000. }
  6001. fx->process (l.data(), r.data(), 48000, slot.params, 120.0);
  6002. auto rms = [&] (int from, int to) {
  6003. double e = 0.0;
  6004. for (int i = from; i < to; ++i)
  6005. e += (double) l[(std::size_t) i] * l[(std::size_t) i];
  6006. return std::sqrt (e / (to - from));
  6007. };
  6008. CHECK (rms (6000, 20000) > 0.01);
  6009. CHECK (rms (40000, 48000) < 0.1 * rms (6000, 20000)); // modulator silent: the vocoder fades out (100 ms release)
  6010. for (std::size_t i = 0; i < l.size(); ++i)
  6011. CHECK (l[i] == r[i] && std::isfinite (l[i])); // mono output
  6012. }
  6013. TEST (fruity_vocoder_engine)
  6014. {
  6015. // REA_rodada70: Fruity Vocoder, states of our own against the installation's DLL (goldens = the DLL's output for
  6016. // the same input, EXEC; all three bit-identical over the whole 4096 frames)
  6017. std::vector<float> inL (4096), inR (4096);
  6018. for (int i = 0; i < 4096; ++i)
  6019. {
  6020. inL[(std::size_t) i] = i < 3000 ? (float) (((i * 37) % 200) - 100) / 128.0f : 0.0f; // modulator, then silence
  6021. inR[(std::size_t) i] = (i * 13) % 64 < 32 ? 0.5f : -0.5f; // carrier
  6022. }
  6023. auto make = [] (int bands, int order, int listen, bool swap, std::vector<float> levels, std::array<int, 12> c) {
  6024. lynx::fvocoder::State s;
  6025. s.bands = bands;
  6026. s.order = order;
  6027. s.listen = listen;
  6028. s.swap = swap;
  6029. for (std::size_t k = 0; k < levels.size(); ++k)
  6030. s.levels[k] = levels[k];
  6031. s.c = c;
  6032. return s;
  6033. };
  6034. auto run = [&] (const lynx::fvocoder::State& s, double rate, int block, int holdAt) {
  6035. std::vector<float> l = inL, r = inR;
  6036. lynx::fvocoder::Engine e;
  6037. e.setState (s);
  6038. e.prepare (rate);
  6039. for (int i = 0; i < 4096;)
  6040. {
  6041. auto len = std::min (block, 4096 - i);
  6042. if (holdAt > i && holdAt < i + len)
  6043. len = holdAt - i;
  6044. if (i == holdAt)
  6045. {
  6046. auto t = e.state();
  6047. t.c[lynx::fvocoder::hold] = 1;
  6048. e.setState (t);
  6049. }
  6050. e.processStereo (l.data() + i, r.data() + i, len);
  6051. i += len;
  6052. }
  6053. return std::make_pair (l, r);
  6054. };
  6055. auto check = [] (const std::pair<std::vector<float>, std::vector<float>>& out, const std::vector<std::pair<int, float>>& golden) {
  6056. for (const auto& [i, g] : golden)
  6057. CHECK (out.first[(std::size_t) i] == g && out.second[(std::size_t) i] == g);
  6058. };
  6059. // 16 bands, order 2, edited band levels, modulator and carrier levels in the mix
  6060. 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 },
  6061. { 0, 65536, 64, 0, 0, 50, 20, 100, 0, 20, 10, 128 });
  6062. const auto outA = run (a, 44100.0, 512, -1);
  6063. check (outA, { { 100, -0.23145467042922974f }, { 1000, 0.2901436686515808f }, { 2047, 0.03407000005245209f }, { 2500, 0.8314149379730225f },
  6064. { 3000, 0.606562614440918f }, { 3500, 0.2637328505516052f }, { 4095, -0.001206810586154461f } });
  6065. // 64 bands, order 3, inverted synthesis bank shifted by −300 cents, 48 kHz in blocks of 300, Hold from frame 2048
  6066. const auto b = make (64, 3, 2, false, {}, { 2000, 60000, 90, 1, -300, 120, 15, 300, 0, 0, 5, 150 });
  6067. check (run (b, 48000.0, 300, 2048), { { 100, 0.005184958688914776f }, { 1000, -0.0008608861826360226f }, { 2047, -0.026466617360711098f },
  6068. { 2500, -0.009371745400130749f }, { 3000, -0.018949978053569794f }, { 3500, -0.02667868137359619f },
  6069. { 4095, -0.021947938948869705f } });
  6070. // 4 bands, order 1, swapped inputs, narrow 30000…50000 range with the linear scale, 22.05 kHz in blocks of 77
  6071. 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 });
  6072. check (run (c, 22050.0, 77, -1), { { 100, 0.31434211134910583f }, { 1000, -1.953513264656067f }, { 2047, 1.4135023355484009f },
  6073. { 2500, 1.539392352104187f }, { 3000, -0.27189597487449646f }, { 3500, -0.11583124846220016f },
  6074. { 4095, -0.11583124846220016f } });
  6075. // FL's state imports into the LYNX slot (controls as parameters, the levels in the state) and the slot's effect
  6076. // (one 4096-frame call, split into 512-frame engine blocks) gives the same output
  6077. const auto bytes = lynx::fvocoder::serialise (a);
  6078. CHECK (bytes.size() == 17 + 16 * 4 + 12 * 4);
  6079. const auto slot = lynx::effectFromFl ("Fruity Vocoder", bytes);
  6080. CHECK (slot && slot->kind == lynx::EffectKind::vocoder && slot->state == bytes);
  6081. CHECK (slot && slot->params[0] == 0.4f && slot->params[1] == 0.5f && slot->params[2] == 1.0f && slot->params[15] == 0.8f);
  6082. if (slot)
  6083. {
  6084. auto fx = lynx::makeEffect (*slot);
  6085. fx->prepare (44100.0);
  6086. std::vector<float> l = inL, r = inR;
  6087. fx->process (l.data(), r.data(), 4096, slot->params, 120.0);
  6088. CHECK (l == outA.first && r == outA.second);
  6089. CHECK (lynx::formatEffectParam (lynx::EffectKind::vocoder, 5, slot->params[5], 120.0) == "16000 Hz");
  6090. CHECK (lynx::formatEffectParam (lynx::EffectKind::vocoder, 10, slot->params[10], 120.0) == "20 ms");
  6091. CHECK (lynx::formatEffectParam (lynx::EffectKind::vocoder, 15, slot->params[15], 120.0) == "1.00");
  6092. }
  6093. const auto parsed = lynx::fvocoder::parse (bytes);
  6094. CHECK (parsed && parsed->bands == 16 && parsed->levels[5] == 2.0f && parsed->c[lynx::fvocoder::release] == 100);
  6095. CHECK (! lynx::fvocoder::parse ({ 2, 0, 0, 0, 12, 0, 0, 0 })); // 12 bands is not a valid count
  6096. // a raw control outside the knob range (FL keeps it, REA_rodada78 §1) survives while the knob shows it, clamped
  6097. CHECK (lynx::fvocoder::rawFromKnob (0.0f, lynx::fvocoder::attack, 5) == 5);
  6098. CHECK (lynx::fvocoder::rawFromKnob (0.5f, lynx::fvocoder::attack, 5) == 505);
  6099. CHECK (lynx::fvocoder::rawFromKnob (1.0f, lynx::fvocoder::wetLevel, 250) == 250);
  6100. CHECK (lynx::fvocoder::rawFromKnob (0.5f, lynx::fvocoder::freqScale, 64) == 64);
  6101. }
  6102. // Delphi run-time math (REA_rodada70 §2): values the plugin's own exp / ln / SinCos return (EXEC, called in place)
  6103. TEST (delphi_math_matches_plugin_runtime)
  6104. {
  6105. // arguments where the C library differs from the Delphi run-time in the last bit
  6106. CHECK (lynx::dmath::exp (0x1.f6e5c5a26a7f8p+3) == 0x1.98170c3deea30p+22);
  6107. CHECK (lynx::dmath::exp (-0x1.12fc91a51d906p+4) == 0x1.27108f934a58cp-25);
  6108. CHECK (lynx::dmath::exp (-0x1.2e9e4c229e93ap+4) == 0x1.a3bf2e4ee3f1ep-28);
  6109. CHECK (lynx::dmath::ln (0x1.76a77e2a09abep-1) == -0x1.3fd111c5297f8p-2);
  6110. CHECK (lynx::dmath::ln (0x1.da1feaa8547dfp+0) == 0x1.3b8abd2756774p-1);
  6111. CHECK (lynx::dmath::ln (0x1.a1e28772a0d78p-1) == -0x1.9ffcaa3a11e30p-3);
  6112. double s = 0.0, c = 0.0;
  6113. lynx::dmath::sinCos (0x1.91b4a19e65a9bp-1, s, c);
  6114. CHECK (s == 0x1.69be278761916p-1 && c == 0x1.6a559572e1ac9p-1);
  6115. lynx::dmath::sinCos (0x1.3284a93d70a8ap-1, s, c);
  6116. CHECK (s == 0x1.2088b313ea643p-1 && c == 0x1.a6f4d889fd91ap-1);
  6117. lynx::dmath::sinCos (0x1.f9b2f53d6ce02p+0, s, c);
  6118. CHECK (s == 0x1.d6a9b8f170994p-1 && c == -0x1.931701be7b5f3p-2);
  6119. }
  6120. // Vocodex (REA_rodada35): a synthetic state in FL's layout (version 8, 13 maps, 20 parameters, the 0x24 block,
  6121. // 13 map-axis bytes, tone, notes)
  6122. struct VocodexSpec
  6123. {
  6124. // Wet, SG, ModVol, ModHP, CarVol, CarLP, CarNoise, CarNoiseHP, Hold, Att, AttCurve, Rel, RelCurve, Freeze, BW,
  6125. // Flatness, ModBW, ModPitch, FatPitch, FatPan
  6126. 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 };
  6127. std::array<std::int32_t, 7> block { 0, 2, 0, 24, 1, 1, 2 }; // hold mode, order, unison, bands, draft field, ModIn, CarIn
  6128. 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
  6129. std::vector<std::uint8_t> bytes() const
  6130. {
  6131. std::vector<std::uint8_t> d;
  6132. 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); };
  6133. auto i32 = [&] (std::int32_t v) { put (&v, 4); };
  6134. i32 (8 << 16);
  6135. for (int m = 0; m < 13; ++m)
  6136. {
  6137. const double y0 = m == 8 ? 0.1429 : (m == 3 || m == 4 || m == 11) ? 0.0 : 0.5;
  6138. const double y1 = m == 8 ? 0.9918 : y0;
  6139. i32 (1), i32 (0), i32 (2);
  6140. for (const auto& [dx, y] : { std::pair { 0.0, y0 }, std::pair { 1.0, y1 } })
  6141. {
  6142. put (&dx, 8), put (&y, 8);
  6143. i32 (0), i32 (0); // tension (f32 0), flags
  6144. }
  6145. const std::array<std::uint8_t, 20> tail {};
  6146. put (tail.data(), tail.size());
  6147. }
  6148. for (auto v : params)
  6149. i32 (v);
  6150. for (auto v : block)
  6151. i32 (v);
  6152. put (flags.data(), flags.size());
  6153. const std::array<std::uint8_t, 13> axis {};
  6154. put (axis.data(), axis.size());
  6155. d.push_back (3), put ("PWM", 3);
  6156. i32 (1), i32 (60);
  6157. return d;
  6158. }
  6159. };
  6160. std::vector<float> vocodexSaw (int n, double hz, double fs)
  6161. {
  6162. std::vector<float> out ((std::size_t) n);
  6163. for (int i = 0; i < n; ++i)
  6164. out[(std::size_t) i] = (float) (2.0 * std::fmod (i * hz / fs, 1.0) - 1.0) * 0.4f;
  6165. return out;
  6166. }
  6167. TEST (vocodex_state_parse_and_conversion)
  6168. {
  6169. VocodexSpec spec;
  6170. spec.block = { 1, 3, 2, 40, 0, 2, 1 };
  6171. spec.params[17] = -1200;
  6172. const auto raw = spec.bytes();
  6173. const auto s = lynx::vocodex::parseState (raw);
  6174. CHECK (s.has_value());
  6175. if (! s)
  6176. return;
  6177. CHECK (s->version == 8 && s->holdMode == 1 && s->order == 3 && s->unison == 2 && s->bands == 40);
  6178. CHECK (s->draft() && s->modIn == 2 && s->carIn == 1 && s->minTimes && s->subtractiveGate && ! s->lrEncoding);
  6179. CHECK (s->params[lynx::vocodex::modPitch] == -1200 && s->params[lynx::vocodex::carLp] == 65536);
  6180. 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);
  6181. CHECK (s->tone == "PWM" && s->notes.size() == 1 && s->notes[0] == 60);
  6182. const auto v = lynx::vocodexEffect (raw);
  6183. CHECK (v && v->slot.kind == lynx::EffectKind::vocodex && v->slot.state == raw && v->modIn == 2 && v->carIn == 1 && v->notes.empty());
  6184. spec.block[6] = -1; // internal synth carrier: not available yet → reported
  6185. spec.params[1] = 150; // Soundgoodizer stage
  6186. const auto w = lynx::vocodexEffect (spec.bytes());
  6187. CHECK (w && w->carIn == -1 && w->notes.size() == 2);
  6188. CHECK (! lynx::vocodex::parseState ({ 1, 2, 3 }).has_value());
  6189. }
  6190. TEST (vocodex_vocodes_side_inputs)
  6191. {
  6192. // modulator on side input 1 (noise in the first half), carrier on side input 2 (saw), own input silent: the
  6193. // output follows the modulator's envelope and fades after it stops
  6194. VocodexSpec spec;
  6195. lynx::EffectSlot slot = lynx::makeEffectSlot (lynx::EffectKind::vocodex);
  6196. slot.state = spec.bytes();
  6197. auto fx = lynx::makeEffect (slot);
  6198. fx->prepare (48000.0);
  6199. CHECK (fx->latencySamples() == 0);
  6200. const auto car = vocodexSaw (48000, 110.0, 48000.0);
  6201. std::vector<float> mod (48000), l (48000, 0.0f), r (48000, 0.0f);
  6202. std::uint32_t seed = 7;
  6203. for (std::size_t i = 0; i < mod.size(); ++i)
  6204. {
  6205. seed = seed * 1664525u + 1013904223u;
  6206. mod[i] = i < 24000 ? ((float) (seed >> 8) / 16777216.0f - 0.5f) : 0.0f;
  6207. }
  6208. for (int done = 0; done < 48000; done += 512)
  6209. {
  6210. const int n = std::min (512, 48000 - done);
  6211. fx->setSidechain (mod.data() + done, mod.data() + done);
  6212. fx->setSidechain2 (car.data() + done, car.data() + done);
  6213. fx->process (l.data() + done, r.data() + done, n, slot.params, 120.0);
  6214. }
  6215. auto rms = [&] (int from, int to) {
  6216. double e = 0.0;
  6217. for (int i = from; i < to; ++i)
  6218. e += (double) l[(std::size_t) i] * l[(std::size_t) i] + (double) r[(std::size_t) i] * r[(std::size_t) i];
  6219. return std::sqrt (e / (2.0 * (to - from)));
  6220. };
  6221. CHECK (rms (6000, 20000) > 0.02);
  6222. CHECK (rms (40000, 48000) < 0.01 * rms (6000, 20000));
  6223. CHECK (std::abs (l[0]) < 1e-6f); // the gains ramp up from 0 when the plugin is created
  6224. }
  6225. TEST (vocodex_draft_latency_and_pass_through)
  6226. {
  6227. // carrier pass-through at unit gain only (Wet 0): after a Flush the gains snap, so the output is the carrier,
  6228. // 16 samples late in Draft
  6229. VocodexSpec spec;
  6230. spec.params[0] = 0; // Wet
  6231. spec.params[4] = 12800; // CarVol = 1
  6232. for (const int draftField : { 1, 0 })
  6233. {
  6234. spec.block[4] = draftField;
  6235. const auto s = lynx::vocodex::parseState (spec.bytes());
  6236. CHECK (s.has_value());
  6237. lynx::vocodex::Engine engine;
  6238. engine.prepare (*s, 44100.0, 300, s->draft());
  6239. CHECK (engine.latencySamples() == (draftField == 0 ? 16 : 0));
  6240. engine.flush();
  6241. const auto saw = vocodexSaw (3000, 220.0, 44100.0);
  6242. std::vector<float> car (6000), mod (6000, 0.0f), out (6000);
  6243. for (int i = 0; i < 3000; ++i)
  6244. car[(std::size_t) (2 * i)] = car[(std::size_t) (2 * i + 1)] = saw[(std::size_t) i];
  6245. static constexpr int sizes[] { 300, 37, 128, 1, 255, 16, 299 }; // uneven blocks exercise the FIFOs
  6246. for (int done = 0, k = 0, n = 0; done < 3000; done += n, ++k)
  6247. {
  6248. n = std::min (sizes[k % 7], 3000 - done);
  6249. engine.process (mod.data() + 2 * done, mod.data() + 2 * done, car.data() + 2 * done, out.data() + 2 * done, n);
  6250. }
  6251. const int lag = engine.latencySamples();
  6252. auto maxError = 0.0f;
  6253. for (int i = lag; i < 3000; ++i)
  6254. maxError = std::max (maxError, std::abs (out[(std::size_t) (2 * i)] - saw[(std::size_t) (i - lag)]));
  6255. CHECK (maxError == 0.0f);
  6256. }
  6257. }
  6258. TEST (vocodex_soundgoodizer_stage_limits_the_output)
  6259. {
  6260. // SG > 0: the vocoder's output (loud here: Wet 2000) goes through the Soundgoodizer approximation, whose
  6261. // master limiter keeps the peaks under −0.2 dBFS; its look-ahead is reported as latency
  6262. VocodexSpec spec;
  6263. spec.params[0] = 2000;
  6264. spec.params[1] = 250;
  6265. lynx::EffectSlot slot = lynx::makeEffectSlot (lynx::EffectKind::vocodex);
  6266. slot.state = spec.bytes();
  6267. auto fx = lynx::makeEffect (slot);
  6268. fx->prepare (48000.0);
  6269. CHECK (fx->latencySamples() == 72); // 1.5 ms at 48 kHz
  6270. const auto car = vocodexSaw (48000, 110.0, 48000.0);
  6271. std::vector<float> mod (48000), l (48000, 0.0f), r (48000, 0.0f);
  6272. for (std::size_t i = 0; i < mod.size(); ++i)
  6273. mod[i] = 0.9f * (float) std::sin (0.07 * (double) i) * (float) std::sin (0.0003 * (double) i);
  6274. auto peak = 0.0f;
  6275. for (int done = 0; done < 48000; done += 512)
  6276. {
  6277. const int n = std::min (512, 48000 - done);
  6278. fx->setSidechain (mod.data() + done, mod.data() + done);
  6279. fx->setSidechain2 (car.data() + done, car.data() + done);
  6280. fx->process (l.data() + done, r.data() + done, n, slot.params, 120.0);
  6281. }
  6282. for (std::size_t i = 0; i < l.size(); ++i)
  6283. peak = std::max ({ peak, std::abs (l[i]), std::abs (r[i]) });
  6284. CHECK (peak > 0.5f && peak <= 0.9775f);
  6285. }
  6286. TEST (vocodex_tail_bypass_and_noise)
  6287. {
  6288. // both inputs silent for longer than 0.5 s + hold + release: the own input passes unchanged
  6289. VocodexSpec spec;
  6290. const auto s = lynx::vocodex::parseState (spec.bytes());
  6291. CHECK (s.has_value());
  6292. lynx::vocodex::Engine engine;
  6293. engine.prepare (*s, 44100.0, 512, false);
  6294. std::vector<float> zero (1024, 0.0f), own (1024), out (1024);
  6295. for (std::size_t i = 0; i < own.size(); ++i)
  6296. own[i] = 0.25f * (float) std::sin (0.01 * (double) i);
  6297. for (int b = 0; b < 80; ++b)
  6298. engine.process (own.data(), zero.data(), zero.data(), out.data(), 512);
  6299. CHECK (out == own);
  6300. // carrier noise: the IPP-compatible generator restarts from the same seed after a Flush
  6301. spec.params[6] = 9000;
  6302. const auto noisy = lynx::vocodex::parseState (spec.bytes());
  6303. lynx::vocodex::Engine a;
  6304. a.prepare (*noisy, 44100.0, 512, false);
  6305. std::vector<float> mod (1024), first (1024), second (1024);
  6306. for (std::size_t i = 0; i < mod.size(); ++i)
  6307. mod[i] = 0.3f * (float) std::sin (0.05 * (double) i);
  6308. a.flush();
  6309. a.process (own.data(), mod.data(), zero.data(), first.data(), 512);
  6310. a.flush();
  6311. a.process (own.data(), mod.data(), zero.data(), second.data(), 512);
  6312. CHECK (first == second);
  6313. auto energy = 0.0;
  6314. for (auto v : first)
  6315. energy += (double) v * v;
  6316. CHECK (energy > 1e-6 && std::isfinite (energy));
  6317. }
  6318. TEST (synth_spectral_oscillator)
  6319. {
  6320. // 1/n spectrum = saw: same harmonic proportions and same level as LYNX's saw
  6321. lynx::SynthSettings saw;
  6322. saw.osc[0] = { lynx::Waveform::saw, 0, 0.0f, 1.0f };
  6323. saw.cutoff = 20000.0f;
  6324. auto spectral = saw;
  6325. spectral.osc[0].wave = lynx::Waveform::spectral;
  6326. for (int n = 1; n <= lynx::SynthSettings::spectrumSize; ++n)
  6327. spectral.spectrum[(std::size_t) n - 1] = 1.0f / (float) n;
  6328. const auto a = renderSynthNote (saw, 57, 24000), b = renderSynthNote (spectral, 57, 24000); // 220 Hz
  6329. for (const auto h : { 1, 2, 3, 5 })
  6330. {
  6331. const auto ea = goertzel (a, 220.0 * h, 4800, 24000), eb = goertzel (b, 220.0 * h, 4800, 24000);
  6332. CHECK (std::abs (10.0 * std::log10 (eb / ea)) < 0.5); // within 0.5 dB
  6333. }
  6334. // odd only: no 2nd harmonic
  6335. auto odd = spectral;
  6336. for (int n = 2; n <= lynx::SynthSettings::spectrumSize; n += 2)
  6337. odd.spectrum[(std::size_t) n - 1] = 0.0f;
  6338. const auto c = renderSynthNote (odd, 57, 24000);
  6339. CHECK (goertzel (c, 440.0, 4800, 24000) < 1e-4 * goertzel (c, 220.0, 4800, 24000));
  6340. // high note: the band-limited tables don't pass Nyquist (no aliasing below the fundamental)
  6341. const auto d = renderSynthNote (spectral, 105, 24000); // 3520 Hz
  6342. CHECK (goertzel (d, 1000.0, 4800, 24000) < 1e-3 * goertzel (d, 3520.0, 4800, 24000));
  6343. }
  6344. TEST (compressor_sidechain_ducks_and_orders_mixer)
  6345. {
  6346. // the detector listens to the side input: quiet signal on the track, loud kick on the sidechain -> reduction
  6347. auto slot = lynx::makeEffectSlot (lynx::EffectKind::compressor);
  6348. slot.params[0] = 0.5f; // threshold
  6349. slot.params[1] = 1.0f; // high ratio
  6350. slot.params[4] = 0.0f;
  6351. auto run = [&] (bool withSidechain) {
  6352. auto fx = lynx::makeEffect (lynx::EffectKind::compressor);
  6353. fx->prepare (48000.0);
  6354. std::vector<float> l (4800, 0.1f), r (4800, 0.1f), kick (4800, 1.0f);
  6355. if (withSidechain)
  6356. fx->setSidechain (kick.data(), kick.data());
  6357. fx->process (l.data(), r.data(), 4800, slot.params, 120.0);
  6358. return l.back();
  6359. };
  6360. const auto alone = run (false), ducked = run (true);
  6361. CHECK (ducked < 0.5f * alone);
  6362. // order: track 5 listens to 7 via sidechain -> 7 comes before 5
  6363. auto song = lynx::Song::makeDefault();
  6364. while (song.mixer.size() < 8)
  6365. song.mixer.push_back ({});
  6366. auto comp = lynx::makeEffectSlot (lynx::EffectKind::compressor);
  6367. comp.sidechain = 7;
  6368. song.mixer[5].effects.push_back (comp);
  6369. const auto order = song.mixerOrder();
  6370. const auto at = [&] (int t) { return std::find (order.begin(), order.end(), t) - order.begin(); };
  6371. CHECK (at (7) < at (5));
  6372. }
  6373. TEST (pitch_correction_pulls_to_allowed_note)
  6374. {
  6375. auto run = [] (const lynx::EffectParams& p, double hz) {
  6376. auto fx = lynx::makeEffect (lynx::EffectKind::pitchCorrect);
  6377. fx->prepare (48000.0);
  6378. std::vector<float> l (48000 * 2), r;
  6379. for (std::size_t i = 0; i < l.size(); ++i)
  6380. l[i] = 0.5f * (float) std::sin (2.0 * 3.141592653589793 * hz * (double) i / 48000.0);
  6381. r = l;
  6382. fx->process (l.data(), r.data(), (int) l.size(), p, 120.0);
  6383. // frequency from zero crossings in the last second
  6384. int crossings = 0;
  6385. for (std::size_t i = 48000 + 1; i < l.size(); ++i)
  6386. crossings += (l[i - 1] < 0.0f) != (l[i] < 0.0f);
  6387. return crossings / 2.0;
  6388. };
  6389. auto slot = lynx::makeEffectSlot (lynx::EffectKind::pitchCorrect); // chromatic, instant, A 440
  6390. CHECK (std::abs (run (slot.params, 452.0) - 440.0) < 4.0);
  6391. for (int k = 0; k < 12; ++k)
  6392. slot.params[(std::size_t) (5 + k)] = k == 0 ? 1.0f : 0.0f; // only C
  6393. CHECK (std::abs (run (slot.params, 452.0) - 523.25) < 5.0); // C5 (523 Hz) is closer than C4 (262)
  6394. slot.params[2] = 1.0f; // turn everything off: passes unchanged
  6395. CHECK (std::abs (run (slot.params, 452.0) - 452.0) < 4.0);
  6396. }
  6397. TEST (pitch_correction_bypass_is_a_pure_delay)
  6398. {
  6399. // FL's Pitcher with Bypass All still runs its shifter: the plain path is an exact 9-sample delay, the formant
  6400. // path (256-Hann overlap-add) an exact 257-sample delay, and Gender changes nothing without a shift
  6401. // (REA_rodada30 §2.4/2.5); both are reported as latency
  6402. std::vector<float> in (48000);
  6403. for (std::size_t i = 0; i < in.size(); ++i)
  6404. in[i] = 0.5f * (float) std::sin (2.0 * 3.141592653589793 * 440.0 * (double) i / 48000.0)
  6405. + 0.2f * (float) std::sin (2.0 * 3.141592653589793 * 1234.5 * (double) i / 48000.0);
  6406. auto run = [&] (bool formant, float gender, int& latency) {
  6407. auto fx = lynx::makeEffect (lynx::EffectKind::pitchCorrect);
  6408. fx->prepare (48000.0);
  6409. auto slot = lynx::makeEffectSlot (lynx::EffectKind::pitchCorrect);
  6410. slot.params[2] = 1.0f;
  6411. slot.params[29] = formant ? 1.0f : 0.0f;
  6412. slot.params[30] = gender;
  6413. auto l = in, r = in;
  6414. for (std::size_t i = 0; i < l.size(); i += 480)
  6415. fx->process (l.data() + i, r.data() + i, 480, slot.params, 120.0);
  6416. latency = fx->latencySamples();
  6417. return l;
  6418. };
  6419. int latency = 0;
  6420. auto plain = run (false, 0.5f, latency);
  6421. CHECK (latency == 9);
  6422. auto worst = 0.0f;
  6423. for (std::size_t i = 4000; i < in.size(); ++i)
  6424. worst = std::max (worst, std::abs (plain[i] - in[i - 9]));
  6425. CHECK (worst < 1e-5f);
  6426. auto formant = run (true, 0.8f, latency);
  6427. CHECK (latency == 257);
  6428. worst = 0.0f;
  6429. for (std::size_t i = 4000; i < in.size(); ++i)
  6430. worst = std::max (worst, std::abs (formant[i] - in[i - 257]));
  6431. CHECK (worst < 1e-5f);
  6432. }
  6433. TEST (pitch_correction_formant_path)
  6434. {
  6435. // Formant on: PSOLA keeps the correction (output pitch = grain spacing) while Gender resamples the grains:
  6436. // the spectrum of a saw moves up with Gender > 0.5 and down with Gender < 0.5
  6437. auto run = [] (float gender) {
  6438. auto fx = lynx::makeEffect (lynx::EffectKind::pitchCorrect);
  6439. fx->prepare (48000.0);
  6440. auto slot = lynx::makeEffectSlot (lynx::EffectKind::pitchCorrect); // chromatic, instant, A 440
  6441. slot.params[29] = 1.0f;
  6442. slot.params[30] = gender;
  6443. std::vector<float> l (48000 * 2), r;
  6444. for (std::size_t i = 0; i < l.size(); ++i)
  6445. l[i] = 0.5f * (float) (2.0 * std::fmod (452.0 * (double) i / 48000.0, 1.0) - 1.0); // saw
  6446. r = l;
  6447. fx->process (l.data(), r.data(), (int) l.size(), slot.params, 120.0);
  6448. l.erase (l.begin(), l.begin() + 48000);
  6449. return l;
  6450. };
  6451. // period by autocorrelation (lags 60…160 = 300…800 Hz)
  6452. auto pitch = [] (const std::vector<float>& x) {
  6453. auto best = 0.0, bestLag = 0.0;
  6454. for (int lag = 60; lag <= 160; ++lag)
  6455. {
  6456. auto sum = 0.0;
  6457. for (std::size_t i = 0; i + (std::size_t) lag < x.size(); ++i)
  6458. sum += (double) x[i] * x[i + (std::size_t) lag];
  6459. if (sum > best)
  6460. {
  6461. best = sum;
  6462. bestLag = lag;
  6463. }
  6464. }
  6465. return 48000.0 / bestLag;
  6466. };
  6467. // brightness: energy of the first difference relative to the energy
  6468. auto brightness = [] (const std::vector<float>& x) {
  6469. auto e = 0.0, d = 0.0;
  6470. for (std::size_t i = 1; i < x.size(); ++i)
  6471. {
  6472. e += (double) x[i] * x[i];
  6473. d += (double) (x[i] - x[i - 1]) * (x[i] - x[i - 1]);
  6474. }
  6475. return d / e;
  6476. };
  6477. const auto neutral = run (0.5f), up = run (0.8f), down = run (0.3f);
  6478. CHECK (std::abs (pitch (neutral) - 440.0) < 5.0); // 452 Hz pulled to A
  6479. CHECK (std::abs (pitch (up) - 440.0) < 5.0); // Gender does not move the pitch
  6480. CHECK (brightness (up) > 1.5 * brightness (neutral));
  6481. CHECK (brightness (down) < 0.7 * brightness (neutral));
  6482. }
  6483. TEST (parallel_container_inner_links_modulate)
  6484. {
  6485. // one branch: utility (gain) + a control-only auto-pan LFO (1 beat); the link maps the LFO 0..1 onto the
  6486. // utility gain −24 dB..0 dB, so the level must swing; without the link it stays put
  6487. auto container = lynx::makeEffectSlot (lynx::EffectKind::parallel);
  6488. auto gain = lynx::makeEffectSlot (lynx::EffectKind::utility);
  6489. auto lfo = lynx::makeEffectSlot (lynx::EffectKind::autoPan);
  6490. lfo.params[0] = 1.0f / 64.0f;
  6491. lfo.params[4] = 1.0f; // control only: audio passes unchanged
  6492. container.branches.push_back ({ { gain, lfo }, 1.0f });
  6493. lynx::EffectLink link;
  6494. link.source = 1;
  6495. link.param = 0;
  6496. for (int i = 0; i <= 32; ++i)
  6497. link.curve.push_back (0.5f * (float) i / 32.0f);
  6498. auto levels = [&] (bool linked) {
  6499. auto slot = container;
  6500. if (linked)
  6501. slot.links.push_back (link);
  6502. auto fx = lynx::makeParallelEffect (slot);
  6503. fx->prepare (48000.0);
  6504. float lo = 1e9f, hi = 0.0f;
  6505. for (int block = 0; block < 94; ++block) // ~1 beat at 120 BPM in 256-sample blocks
  6506. {
  6507. std::vector<float> l (256, 1.0f), r (256, 1.0f);
  6508. fx->setSongPosition (block * 256.0 / 48000.0 * 2.0 * 960.0); // the engine positions every block
  6509. fx->process (l.data(), r.data(), 256, slot.params, 120.0);
  6510. lo = std::min (lo, std::abs (l[128]));
  6511. hi = std::max (hi, std::abs (l[128]));
  6512. }
  6513. return std::pair { lo, hi };
  6514. };
  6515. const auto [lo, hi] = levels (true);
  6516. CHECK (hi > 0.8f && lo < 0.12f);
  6517. const auto [lo0, hi0] = levels (false);
  6518. CHECK (std::abs (hi0 - lo0) < 1e-4f);
  6519. }
  6520. TEST (synth_spectrum_decays_per_harmonic)
  6521. {
  6522. // 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
  6523. lynx::SynthSettings s;
  6524. s.osc[0] = { lynx::Waveform::spectral, 0, 0.0f, 1.0f };
  6525. s.cutoff = 20000.0f;
  6526. s.decay = 0.0f;
  6527. s.sustain = 1.0f;
  6528. s.spectrum[0] = 1.0f;
  6529. s.spectrum[2] = 1.0f;
  6530. s.spectrumDecay[2] = 0.2f;
  6531. const auto x = renderSynthNote (s, 57, 48000); // 220 Hz, 48 kHz, 1 s
  6532. // equal 2400-sample windows (Goertzel power grows with the window): 10…60 ms and 500…550 ms
  6533. const auto early1 = goertzel (x, 220.0, 480, 2880), early3 = goertzel (x, 660.0, 480, 2880);
  6534. const auto late1 = goertzel (x, 220.0, 24000, 26400), late3 = goertzel (x, 660.0, 24000, 26400);
  6535. CHECK (early3 > 0.05 * early1); // ≈ −9 dB on average over 10…60 ms
  6536. CHECK (late3 < 1e-4 * late1);
  6537. CHECK (std::abs (10.0 * std::log10 (late1 / early1)) < 1.0); // the 1st holds
  6538. // without per-harmonic decay the spectrum stays as it is
  6539. s.spectrumDecay[2] = 0.0f;
  6540. const auto y = renderSynthNote (s, 57, 48000);
  6541. CHECK (std::abs (10.0 * std::log10 (goertzel (y, 660.0, 24000, 26400) / goertzel (y, 660.0, 480, 2880))) < 1.0);
  6542. }
  6543. TEST (worker_pool_runs_each_index_once_per_round)
  6544. {
  6545. // rounds of alternating sizes (a stalled worker must never run an index of a later round), each index
  6546. // exactly once, and nothing running after parallelFor returns
  6547. lynx::WorkerPool pool (3);
  6548. std::vector<std::atomic<int>> hits (64);
  6549. std::atomic<int> round { 0 }, outside { 0 }, wrongRound { 0 };
  6550. std::atomic<bool> open { false };
  6551. int failures = 0;
  6552. for (int r = 1; r <= 20000; ++r)
  6553. {
  6554. const auto n = (r % 3 == 0) ? 64 : (r % 3 == 1 ? 2 : 9);
  6555. for (int i = 0; i < n; ++i)
  6556. hits[(std::size_t) i].store (0);
  6557. round.store (r);
  6558. open.store (true);
  6559. auto fn = [&, r] (int i) {
  6560. if (! open.load())
  6561. outside.fetch_add (1);
  6562. if (round.load() != r)
  6563. wrongRound.fetch_add (1);
  6564. hits[(std::size_t) i].fetch_add (1);
  6565. };
  6566. pool.parallelFor (n, fn);
  6567. open.store (false);
  6568. for (int i = 0; i < n; ++i)
  6569. failures += hits[(std::size_t) i].load() != 1;
  6570. }
  6571. CHECK (failures == 0);
  6572. CHECK (outside.load() == 0);
  6573. CHECK (wrongRound.load() == 0);
  6574. }
  6575. TEST (ladder_filter_follows_toxic_law)
  6576. {
  6577. // the 4-pole ladder: −24 dB/oct far above the cutoff (vs −12 for the SVF), resonance raises the corner, and
  6578. // the input saturation keeps it bounded even at full resonance
  6579. auto level = [] (bool ladder, float res, double hz) {
  6580. lynx::SynthSettings s;
  6581. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  6582. s.cutoff = 500.0f;
  6583. s.resonance = res;
  6584. s.ladder = ladder;
  6585. const auto key = (int) std::lround (69.0 + 12.0 * std::log2 (hz / 440.0));
  6586. const auto x = renderSynthNote (s, key, 48000);
  6587. double e = 0.0, peak = 0.0;
  6588. for (std::size_t i = 24000; i < 48000; ++i)
  6589. e += (double) x[i] * x[i], peak = std::max (peak, (double) std::abs (x[i]));
  6590. return std::pair { std::sqrt (e / 24000.0), peak };
  6591. };
  6592. 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
  6593. const auto svfHigh = level (false, 0.0f, 3520.0).first / level (false, 0.0f, 110.0).first;
  6594. CHECK (20.0 * std::log10 (high / low) < -55.0); // ≈ 24 dB/oct over ~2.8 octaves
  6595. CHECK (20.0 * std::log10 (svfHigh) > -45.0); // the 2-pole SVF falls much less
  6596. const auto corner0 = level (true, 0.0f, 523.25).first, corner1 = level (true, 0.6f, 523.25).first;
  6597. CHECK (corner1 > 1.5 * corner0); // resonance boosts near the cutoff
  6598. const auto loud = level (true, 1.0f, 523.25);
  6599. CHECK (std::isfinite (loud.first) && loud.second < 20.0); // self-oscillation stays bounded
  6600. }
  6601. TEST (synth_spectrum_settles_on_target)
  6602. {
  6603. // ALT DECAY-like morph: harmonic 3 starts at 1 and settles on 0.25 (−12 dB) instead of fading out
  6604. lynx::SynthSettings s;
  6605. s.osc[0] = { lynx::Waveform::spectral, 0, 0.0f, 1.0f };
  6606. s.cutoff = 20000.0f;
  6607. s.decay = 0.0f;
  6608. s.sustain = 1.0f;
  6609. s.spectrum[0] = 1.0f;
  6610. s.spectrum[2] = 1.0f;
  6611. s.spectrumDecay[2] = 0.1f;
  6612. s.spectrumTarget[2] = 0.25f;
  6613. const auto x = renderSynthNote (s, 57, 48000);
  6614. const auto ratio = goertzel (x, 660.0, 24000, 26400) / goertzel (x, 220.0, 24000, 26400);
  6615. CHECK (std::abs (10.0 * std::log10 (ratio) - 20.0 * std::log10 (0.25)) < 1.0);
  6616. }
  6617. TEST (kepler_chorus_is_stereo_and_keeps_level)
  6618. {
  6619. // chorus I on a sine: L and R differ (opposite LFO phases), the level stays close to the dry one
  6620. lynx::SynthSettings s;
  6621. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  6622. s.cutoff = 20000.0f;
  6623. auto dry = s;
  6624. s.chorus = 1;
  6625. auto render = [] (const lynx::SynthSettings& set) {
  6626. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  6627. inst->prepare (48000.0);
  6628. lynx::ChannelDef c;
  6629. c.synth = set;
  6630. inst->configure (c, 120.0);
  6631. inst->noteOn (69, 1.0f);
  6632. std::vector<float> l (96000), r (96000);
  6633. inst->render (l.data(), r.data(), 96000, 1.0f, 1.0f);
  6634. return std::pair { l, r };
  6635. };
  6636. const auto [l, r] = render (s);
  6637. const auto [dl, dr] = render (dry);
  6638. double diff = 0.0, e = 0.0, ed = 0.0;
  6639. for (std::size_t i = 48000; i < 96000; ++i)
  6640. diff += std::abs (l[i] - r[i]), e += (double) l[i] * l[i], ed += (double) dl[i] * dl[i];
  6641. CHECK (diff / 48000.0 > 1e-3);
  6642. CHECK (std::abs (10.0 * std::log10 (e / ed)) < 3.0);
  6643. }
  6644. TEST (fl_pan_law_matches_love_philter_dll)
  6645. {
  6646. // values printed by the installation's Love Philter pan routine for vol 0.8 (REA_rodada27 §3.1, lppan.txt)
  6647. struct Row { double pan, ll, rr, rl, lr; };
  6648. const Row rows[] = { { -1.5, 0.239782482, -0.432956904, 0.805467963, 0.0 }, { -0.5, 0.805467963, 0.432956904, 0.239782482, 0.0 },
  6649. { 0.0, 0.8, 0.8, 0.0, 0.0 }, { 0.75, 0.220719516, 0.707889318, 0.0, 0.401742548 },
  6650. { 1.0, 0.0, 0.565685451, 0.0, 0.565685451 } };
  6651. for (const auto& row : rows)
  6652. {
  6653. float ll, rl, lr, rr;
  6654. lynx::flPanMatrix (row.pan, ll, rl, lr, rr);
  6655. CHECK (std::abs (0.8 * ll - row.ll) < 1e-6 && std::abs (0.8 * rr - row.rr) < 1e-6);
  6656. CHECK (std::abs (0.8 * rl - row.rl) < 1e-6 && std::abs (0.8 * lr - row.lr) < 1e-6);
  6657. }
  6658. }
  6659. TEST (effector_units_match_fl_dll_impulse_responses)
  6660. {
  6661. // impulse responses of the installation's Effector engine (REA_rodada27 §2, eu_<kind>_<X>_<Y>.txt; LFO off,
  6662. // wet 1, blocks of 256): samples 1, 10, 100 and 1000
  6663. struct Case { int kind; float x, y; double v[4]; };
  6664. const Case cases[] = { { 3, 0.5f, 0.5f, { -0.603055418, 0.159798339, -0.0055043716, -1.06241259e-06 } },
  6665. { 4, 0.2f, 0.5f, { 4.68885949e-26, 4.20032622e-19, 0.000487731799, 2.27715846e-09 } },
  6666. { 4, 0.8f, 0.3f, { -0.293783396, -0.0306414738, -0.00068128563, 1.358577e-15 } } };
  6667. for (const auto& c : cases)
  6668. {
  6669. auto fx = lynx::effector::make();
  6670. fx->prepare (44100.0);
  6671. lynx::EffectParams p {};
  6672. p[lynx::effector::type] = c.kind / 11.0f;
  6673. p[lynx::effector::wetMix] = 1.0f;
  6674. p[lynx::effector::paramX] = c.x;
  6675. p[lynx::effector::paramY] = c.y;
  6676. p[lynx::effector::modX] = p[lynx::effector::modY] = 0.5f;
  6677. p[lynx::effector::modRate] = 0.5f;
  6678. p[lynx::effector::inputLevel] = 1.0f;
  6679. p[lynx::effector::outputGain] = 0.5f;
  6680. std::vector<float> l (1024, 0.0f), r (1024, 0.0f);
  6681. l[0] = r[0] = 1.0f;
  6682. for (int d = 0; d < 1024; d += 256)
  6683. fx->process (l.data() + d, r.data() + d, 256, p, 120.0);
  6684. const int at[4] { 1, 10, 100, 1000 };
  6685. for (int k = 0; k < 4; ++k)
  6686. CHECK (std::abs (l[(std::size_t) at[k]] - c.v[k]) < 2e-7 + 1e-5 * std::abs (c.v[k]));
  6687. }
  6688. }
  6689. TEST (harmless_stereo_tremolo_and_vibrato)
  6690. {
  6691. // tremolo d = 1: each side swings between 0 and 1.25 at 5 Hz, L and R in anti-phase; vibrato modulates the pitch
  6692. lynx::SynthSettings s;
  6693. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  6694. s.cutoff = 20000.0f;
  6695. s.stereoTremolo = 1.0f;
  6696. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  6697. inst->prepare (48000.0);
  6698. lynx::ChannelDef c;
  6699. c.synth = s;
  6700. inst->configure (c, 120.0);
  6701. inst->noteOn (69, 1.0f);
  6702. std::vector<float> l (48000), r (48000);
  6703. inst->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
  6704. 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; };
  6705. // θ = 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
  6706. CHECK (envelope (l, 19200) > 4.0f * envelope (r, 19200));
  6707. CHECK (envelope (r, 24000) > 4.0f * envelope (l, 24000));
  6708. // vibrato 100 cents: zero crossings vary in spacing; without it they are regular
  6709. auto crossingsSpread = [] (float cents) {
  6710. lynx::SynthSettings v;
  6711. v.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  6712. v.cutoff = 20000.0f;
  6713. v.vibratoCents = cents;
  6714. const auto x = renderSynthNote (v, 69, 48000);
  6715. std::vector<int> zc;
  6716. for (int i = 24000; i < 48000; ++i)
  6717. if (x[(std::size_t) i - 1] < 0.0f && x[(std::size_t) i] >= 0.0f)
  6718. zc.push_back (i);
  6719. int lo = 1 << 30, hi = 0;
  6720. for (std::size_t k = 1; k < zc.size(); ++k)
  6721. lo = std::min (lo, zc[k] - zc[k - 1]), hi = std::max (hi, zc[k] - zc[k - 1]);
  6722. return hi - lo;
  6723. };
  6724. CHECK (crossingsSpread (0.0f) <= 1);
  6725. CHECK (crossingsSpread (100.0f) >= 4);
  6726. }
  6727. TEST (physical_models_sound_and_end)
  6728. {
  6729. // Sakura: a plucked string with default-ish parameters rings and fades after the release; Drumpad: a hit decays
  6730. for (const auto kind : { lynx::InstrumentKind::sakura, lynx::InstrumentKind::drumpad })
  6731. {
  6732. lynx::ChannelDef c;
  6733. c.instrument = kind;
  6734. if (kind == lynx::InstrumentKind::sakura)
  6735. {
  6736. c.modelParams.assign (146, 0.5f);
  6737. c.modelParams[111] = 3.0f; // both strings at the note
  6738. c.modelParams[113] = 0.5f; // octave ×1
  6739. c.modelParams[6] = c.modelParams[11] = 0.99f; // long decay
  6740. c.modelParams[31] = 0.0f; // noise exciter
  6741. }
  6742. else
  6743. {
  6744. c.modelParams.assign (38, 0.5f);
  6745. c.modelParams[0] = 1.0f; // retrigger noise
  6746. c.modelParams[30] = c.modelParams[31] = c.modelParams[32] = c.modelParams[33] = 0.0f; // no velocity morph
  6747. }
  6748. auto inst = lynx::makeInstrument (kind);
  6749. inst->prepare (48000.0);
  6750. inst->configure (c, 120.0);
  6751. inst->noteOn (60, 0.8f);
  6752. std::vector<float> l (48000), r (48000);
  6753. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  6754. inst->noteOff (60);
  6755. inst->render (l.data() + 24000, r.data() + 24000, 24000, 1.0f, 1.0f);
  6756. double early = 0.0;
  6757. for (int i = 0; i < 12000; ++i)
  6758. early += (double) l[(std::size_t) i] * l[(std::size_t) i];
  6759. CHECK (early > 1e-6);
  6760. CHECK (std::all_of (l.begin(), l.end(), [] (float v) { return std::isfinite (v); }));
  6761. std::vector<float> tail (480000), tailR (480000);
  6762. inst->render (tail.data(), tailR.data(), 480000, 1.0f, 1.0f); // 10 s later the voice has ended
  6763. CHECK (inst->idle());
  6764. }
  6765. }
  6766. TEST (squeeze_matches_fl_model)
  6767. {
  6768. // Fruity Squeeze: Steps quantises, Mix 0 passes the input dry ×Volume/127; a silent block is left untouched
  6769. auto slot = lynx::makeEffectSlot (lynx::EffectKind::squeeze);
  6770. slot.params[1] = 0.0f; // mix 0: dry only
  6771. auto fx = lynx::makeEffect (lynx::EffectKind::squeeze);
  6772. fx->prepare (44100.0);
  6773. std::vector<float> l (512, 0.3f), r (512, -0.2f);
  6774. fx->process (l.data(), r.data(), 512, slot.params, 120.0);
  6775. CHECK (std::abs (l[100] - 0.3f) < 1e-6f && std::abs (r[100] + 0.2f) < 1e-6f); // Volume 127/127
  6776. slot.params[1] = 1.0f; // wet only
  6777. slot.params[2] = 32000.0f / 32768.0f; // 32768^(1 − 32000/32768) ≈ 1.27 levels per unit → heavy quantisation
  6778. std::vector<float> a (512), b (512);
  6779. for (int i = 0; i < 512; ++i)
  6780. a[(std::size_t) i] = b[(std::size_t) i] = 0.4f * (float) std::sin (i * 0.05);
  6781. fx->process (a.data(), b.data(), 512, slot.params, 120.0);
  6782. const auto levels = std::pow (32768.0f, 1.0f - 32000.0f / 32768.0f);
  6783. for (int i = 0; i < 512; i += 37)
  6784. CHECK (std::abs (a[(std::size_t) i] * levels - std::nearbyint (a[(std::size_t) i] * levels)) < 1e-3f);
  6785. std::vector<float> z (512, 0.0f), z2 (512, 0.0f);
  6786. fx->process (z.data(), z2.data(), 512, slot.params, 120.0);
  6787. CHECK (z[10] == 0.0f);
  6788. }
  6789. TEST (sakura_matrix_and_effects)
  6790. {
  6791. // REA_rodada29 §1: velocity → level follows 10^(2m)·0.05 (vel 1 vs 0.5 → ×10 in amplitude); the delay leaves
  6792. // an echo after the note has ended and keeps the instrument from going idle until its tail is silent
  6793. auto base = [] {
  6794. lynx::ChannelDef c;
  6795. c.instrument = lynx::InstrumentKind::sakura;
  6796. c.modelParams.assign (146, 0.0f);
  6797. const std::pair<int, float> values[] { { 0, 0.375f }, { 1, 0.5f }, { 3, 0.5f }, { 4, 1.0f }, { 6, 0.95f }, { 9, 0.3f },
  6798. { 11, 0.95f }, { 14, 0.3f }, { 18, 0.5f }, { 19, 0.5f }, { 27, 0.5f }, { 28, 1.0f },
  6799. { 29, 0.7f }, { 31, 1.0f }, { 32, 0.5f }, { 111, 3.0f }, { 113, 0.5f } };
  6800. for (const auto& [i, v] : values)
  6801. c.modelParams[(std::size_t) i] = v;
  6802. return c;
  6803. };
  6804. auto render = [] (const lynx::ChannelDef& c, float velocity, int held, int total) {
  6805. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sakura);
  6806. inst->prepare (48000.0);
  6807. inst->configure (c, 120.0);
  6808. inst->noteOn (60, velocity);
  6809. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  6810. for (int s = 0; s < total; s += 256)
  6811. {
  6812. if (s == held)
  6813. inst->noteOff (60);
  6814. inst->render (l.data() + s, r.data() + s, std::min (256, total - s), 1.0f, 1.0f);
  6815. }
  6816. return std::make_pair (l, std::move (inst));
  6817. };
  6818. auto energy = [] (const std::vector<float>& x, int from, int to) {
  6819. double e = 0.0;
  6820. for (int i = from; i < to; ++i)
  6821. e += (double) x[(std::size_t) i] * x[(std::size_t) i];
  6822. return e;
  6823. };
  6824. auto c = base();
  6825. c.modelParams[79] = 6.0f; // slot 1: velocity → level, amount 1
  6826. c.modelParams[80] = 1.0f;
  6827. c.modelParams[82] = 1.0f;
  6828. const auto loud = energy (render (c, 1.0f, 24064, 24064).first, 0, 24064);
  6829. const auto soft = energy (render (c, 0.5f, 24064, 24064).first, 0, 24064);
  6830. CHECK (loud > 1e-6 && std::abs (loud / soft - 100.0) < 1.0);
  6831. auto d = base();
  6832. auto [dry, dryInst] = render (d, 0.8f, 4864, 72000);
  6833. d.modelParams[63] = 1.0f; // delay on: time 0.5 → ≈ 0.52 s, wet 1, no feedback: one echo
  6834. d.modelParams[64] = 0.5f;
  6835. d.modelParams[69] = 1.0f;
  6836. auto [wet, wetInst] = render (d, 0.8f, 4864, 72000);
  6837. CHECK (energy (wet, 26000, 40000) > 100.0 * energy (dry, 26000, 40000) + 1e-9); // the note ends near 0.2 s
  6838. CHECK (dryInst->idle() || energy (dry, 70000, 72000) < 1e-12);
  6839. CHECK (! wetInst->idle()); // the echo tail has not been silent for 3 s yet
  6840. }
  6841. TEST (drumsynth_live_engine)
  6842. {
  6843. // REA_rodada30 §1: key → patch slot, state parsing, the engine against our reference model (no DLL: synthesised
  6844. // sources only, noise from the fallback seed), voice modes, patch level/pan
  6845. CHECK (lynx::drumSynthKeyToSlot (48) == 0 && lynx::drumSynthKeyToSlot (71) == 23 && lynx::drumSynthKeyToSlot (0) == 24);
  6846. CHECK (lynx::drumSynthKeyToSlot (47) == 71 && lynx::drumSynthKeyToSlot (72) == 72 && lynx::drumSynthKeyToSlot (119) == 119);
  6847. CHECK (lynx::drumSynthKeyToSlot (120) == -1);
  6848. // a version 3 state: 120 names, 120 × 38 ints (send track −1 becomes 0 below version 4), flags
  6849. std::vector<std::uint8_t> blob;
  6850. auto put = [&] (std::uint32_t v) { for (int k = 0; k < 4; ++k) blob.push_back ((std::uint8_t) (v >> (8 * k))); };
  6851. put (3);
  6852. put (66);
  6853. for (int i = 0; i < 120; ++i)
  6854. blob.push_back (2), blob.push_back ('a'), blob.push_back ((std::uint8_t) ('0' + i % 10));
  6855. for (int i = 0; i < 120 * 38; ++i)
  6856. put ((std::uint32_t) (i % 38 == 36 ? -1 : i % 38 == 4 ? 300 : 0));
  6857. put (0);
  6858. const auto parsed = lynx::drumSynthLiveParams (blob);
  6859. CHECK (parsed && (int) parsed->size() == lynx::drumSynthParamCount);
  6860. CHECK (parsed && (*parsed)[36] == 0.0f && (*parsed)[38 * 7 + 4] == 300.0f && (*parsed)[(std::size_t) lynx::drumSynthEditorKeyIndex] == 66.0f);
  6861. blob.resize (500);
  6862. CHECK (! lynx::drumSynthLiveParams (blob));
  6863. // patch on key 60 (slot 12): tone-mode Osc1 with sweep and click, noise-band Osc2, ring, 2 retriggers
  6864. 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,
  6865. 0, 0, 0, 0, 0, 0, 30, -1, 0, 0, 64 };
  6866. lynx::ChannelDef c;
  6867. c.instrument = lynx::InstrumentKind::drumsynth;
  6868. c.modelParams.assign ((std::size_t) lynx::drumSynthParamCount, 0.0f);
  6869. for (int s = 0; s < 120; ++s)
  6870. c.modelParams[(std::size_t) (38 * s + 15)] = c.modelParams[(std::size_t) (38 * s + 34)] = -1.0f;
  6871. for (int i = 0; i < 38; ++i)
  6872. c.modelParams[(std::size_t) (38 * 12 + i)] = (float) patch[i];
  6873. auto render = [] (const lynx::ChannelDef& def, std::vector<int> keys, int total, float gl = 1.0f, float gr = 1.0f) {
  6874. auto inst = lynx::makeInstrument (lynx::InstrumentKind::drumsynth);
  6875. inst->configure (def, 120.0);
  6876. inst->prepare (48000.0);
  6877. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  6878. for (int s = 0; s < total; s += 512)
  6879. {
  6880. for (std::size_t k = 0; k + 1 < keys.size(); k += 2)
  6881. if (keys[k + 1] == s)
  6882. inst->noteOn (keys[k], 100.0f / 128.0f);
  6883. inst->render (l.data() + s, r.data() + s, std::min (512, total - s), gl, gr);
  6884. }
  6885. return std::make_tuple (l, r, std::move (inst));
  6886. };
  6887. {
  6888. auto [l, r, inst] = render (c, { 60, 0 }, 9600);
  6889. // reference model (REA round 30 dsl_model.py, seed 0x56CE, FL velocity 100, V = 1, centre: × g(100)·cos(π/4))
  6890. const std::pair<int, float> golden[] { { 0, -0.0074092713f }, { 10, -0.13961405f }, { 100, -0.47290668f }, { 1000, -0.29188696f },
  6891. { 3000, -0.22130175f }, { 6000, -0.049537566f }, { 9599, 0.010392102f } };
  6892. for (const auto& [i, v] : golden)
  6893. CHECK (std::abs (l[(std::size_t) i] - v) < 2e-6f && std::abs (r[(std::size_t) i] - v) < 2e-6f);
  6894. std::vector<float> tail (48000 * 4), tailR (tail.size());
  6895. inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
  6896. CHECK (inst->idle()); // the engine reported the voice finished
  6897. }
  6898. auto energy = [] (const std::vector<float>& x, int from, int to) {
  6899. double e = 0.0;
  6900. for (int i = from; i < to; ++i)
  6901. e += (double) x[(std::size_t) i] * x[(std::size_t) i];
  6902. return e;
  6903. };
  6904. // a key without a patch level (slot 0, level 0) and a key outside the 120 patches are silent
  6905. CHECK (energy (std::get<0> (render (c, { 48, 0, 125, 0 }, 4096)), 0, 4096) == 0.0);
  6906. // mono: a second hit on the same key cuts the first; poly: both ring together (Osc2 off: no noise, deterministic)
  6907. auto tone = c;
  6908. tone.modelParams[38 * 12 + 14] = 0.0f;
  6909. const auto single = std::get<0> (render (tone, { 60, 0 }, 8192));
  6910. const auto mono = std::get<0> (render (tone, { 60, 0, 60, 2048 }, 8192));
  6911. auto poly = tone;
  6912. poly.modelParams[38 * 12 + 24] = 1.0f;
  6913. const auto both = std::get<0> (render (poly, { 60, 0, 60, 2048 }, 8192));
  6914. double monoDiff = 0.0, polyDiff = 0.0;
  6915. for (int i = 2048 + 300; i < 8192; ++i) // after the cut fade: mono = only the second hit, a copy of the first
  6916. 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]);
  6917. CHECK (monoDiff < 1e-3 && polyDiff > 1.0);
  6918. // patch pan +64 → right only; channel volume counts twice (FL applies Final.Vol in the engine and at the host)
  6919. auto right = c;
  6920. right.modelParams[38 * 12 + 1] = 64.0f;
  6921. const auto [pl, pr, pi] = render (right, { 60, 0 }, 4096);
  6922. CHECK (energy (pl, 0, 4096) < 1e-12 * energy (pr, 0, 4096) && energy (pr, 0, 4096) > 1.0);
  6923. const auto full = std::get<0> (render (tone, { 60, 0 }, 4096));
  6924. const auto half = std::get<0> (render (tone, { 60, 0 }, 4096, 0.5f, 0.5f));
  6925. CHECK (std::abs (half[100] - 0.25f * full[100]) < 1e-6f && std::abs (full[100]) > 0.01f);
  6926. }
  6927. TEST (kepler_lfo_bpm_sync)
  6928. {
  6929. // 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)
  6930. std::vector<std::uint8_t> state (0x21 + 34 * 4, 0);
  6931. auto putU32 = [&] (std::size_t o, std::uint32_t v) { std::memcpy (state.data() + o, &v, 4); };
  6932. putU32 (12, 9);
  6933. std::memcpy (state.data() + 16, "Kepler-1", 9);
  6934. putU32 (0x19, 4);
  6935. putU32 (0x1D, 34);
  6936. float k[34] {};
  6937. k[5] = 0.602085769f; // "Pads - Kepler" demo: n = 286
  6938. k[6] = 1.0f;
  6939. k[10] = 1.0f; // vibrato on
  6940. k[33] = 1.0f;
  6941. std::memcpy (state.data() + 0x21, k, sizeof k);
  6942. lynx::SynthSettings s;
  6943. float gain = 0.0f;
  6944. CHECK (lynx::synthFromKepler (state, s, gain));
  6945. CHECK (s.lfo[lynx::lfoPitch].sync && s.lfo[lynx::lfoPitch].secondsTiming);
  6946. CHECK (std::abs (s.lfo[lynx::lfoPitch].period - 286.0f / 192.0f) < 1e-6f);
  6947. k[5] = 0.0f; // slowest: 32 beats
  6948. std::memcpy (state.data() + 0x21, k, sizeof k);
  6949. CHECK (lynx::synthFromKepler (state, s, gain) && std::abs (s.lfo[lynx::lfoPitch].period - 32.0f) < 1e-6f);
  6950. k[6] = 0.0f; // free rate in seconds
  6951. std::memcpy (state.data() + 0x21, k, sizeof k);
  6952. CHECK (lynx::synthFromKepler (state, s, gain) && ! s.lfo[lynx::lfoPitch].sync);
  6953. }
  6954. namespace
  6955. {
  6956. // A Harmor state written from scratch (REA_rodada11 §4.1 layout, version 18): part A only — a decaying-harmonics
  6957. // timbre, low-pass + resonance, filter envelope, 3-voice unison, vibrato, pluck, tremolo, volume envelope with a
  6958. // velocity map — and the global block. Every other curve is neutral.
  6959. // features (REA_rodada75): 1 arpeggio markers on the volume envelope, 2 noise resonance on filter 1, 4 "pluck replaces
  6960. // blur decay", 8 pluck blend, 16 harmonic randomness, 32 phaser + harmonizer masks
  6961. std::vector<std::uint8_t> harmorTestState (bool panLfo = true, bool image = false, int features = 0)
  6962. {
  6963. std::vector<std::uint8_t> b;
  6964. 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))); };
  6965. 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))); };
  6966. struct Pt { double dx, y, tension; int mode; int arp = 0; };
  6967. auto curve = [&] (const std::vector<Pt>& pts, std::array<std::int32_t, 5> hdr) {
  6968. i32 (3), i32 (3), i32 ((std::int32_t) pts.size());
  6969. for (const auto& p : pts)
  6970. {
  6971. f64 (p.dx), f64 (p.y);
  6972. const auto t = (float) p.tension;
  6973. std::uint32_t u;
  6974. std::memcpy (&u, &t, 4);
  6975. i32 ((std::int32_t) u);
  6976. b.push_back ((std::uint8_t) p.mode), b.push_back ((std::uint8_t) p.arp), b.push_back (0), b.push_back (0);
  6977. }
  6978. for (auto h : hdr)
  6979. i32 (h);
  6980. };
  6981. const std::array<std::int32_t, 5> off { 0, 0, -1, -1, -1 }, on { 0, 1, -1, -1, -1 };
  6982. 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); };
  6983. i32 (18);
  6984. i32 (1 | 4);
  6985. std::vector<std::uint8_t> part;
  6986. std::swap (b, part);
  6987. std::array<std::int32_t, 386> p {};
  6988. const std::pair<int, int> values[] = { { 0, 500 }, { 3, 1000 }, { 9, 1024 }, { 11, 1000 }, { 12, 1000 }, { 17, 300 }, { 18, 4000 },
  6989. { 19, 500 }, { 20, 1000 }, { 24, 500 }, { 25, 500 }, { 28, 500 }, { 29, 256 }, { 30, -1 }, { 31, -1 }, { 33, -1 }, { 34, 3 },
  6990. { 35, 1 }, { 37, 192 }, { 38, 128 }, { 39, 500 }, { 40, 10000 }, { 41, 1 }, { 42, 10000 }, { 43, 1 }, { 45, 200 }, { 46, 500 },
  6991. { 47, 1 }, { 49, -140 }, { 50, 1200 }, { 51, 0 }, { 52, 5000 }, { 54, 500 }, { 56, 600 }, { 57, 300 }, { 59, 500 }, { 60, 600 },
  6992. { 64, 8192 }, { 66, 500 }, { 72, 600 }, { 74, 1000 }, { 75, 600 }, { 78, 5000 }, { 79, 500 }, { 80, 3000 } };
  6993. for (const auto& [i, v] : values)
  6994. p[(std::size_t) i] = v;
  6995. if ((features & 2) != 0)
  6996. p[57] = 400, p[59] = 300; // filter-1 resonance amount and width (noise type set in the combo block)
  6997. if ((features & 4) != 0)
  6998. p[75] = 700, p[21] = 300; // pluck value, time-blur attack
  6999. if ((features & 8) != 0)
  7000. p[75] = 650, p[4] = 600; // pluck, timbre mix
  7001. if ((features & 32) != 0)
  7002. p[76] = 700, p[27] = 600; // phaser mix, harmonizer mix
  7003. if (image) // image synthesis (REA_rodada75): speed, cubic gain / linear freq interpolation, Hz freq mode, formant
  7004. for (const auto& [i, v] : std::initializer_list<std::pair<int, int>> { { 82, 200 }, { 83, 100000 }, { 84, 10000 }, { 85, 20000 },
  7005. { 86, 750 }, { 87, 4 }, { 88, 600 }, { 89, 900 }, { 90, 1 }, { 91, 3 }, { 92, 1500 }, { 93, -300 }, { 94, 800 } })
  7006. p[(std::size_t) i] = v;
  7007. for (auto v : p)
  7008. i32 (v);
  7009. for (int i = 0; i < 12; ++i)
  7010. i32 (i); // module order
  7011. std::array<std::uint8_t, 0x4b> misc {};
  7012. auto put = [&] (int o, std::int32_t v) { std::memcpy (misc.data() + o, &v, 4); };
  7013. const float one = 1.0f;
  7014. put (0x08, 107), put (0x1c, 1), put (0x28, 200), std::memcpy (misc.data() + 0x30, &one, 4);
  7015. put (0x34, 0), put (0x38, 4), put (0x3c, (features & 2) != 0 ? 109 : 100), put (0x40, 108);
  7016. if ((features & 8) != 0)
  7017. put (0x00, 5);
  7018. misc[0x44] = 1;
  7019. if ((features & 4) != 0)
  7020. misc[0x46] = 1;
  7021. b.insert (b.end(), misc.begin(), misc.end());
  7022. 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 };
  7023. for (int k = 0; k < 43; ++k)
  7024. {
  7025. static const int mode2[] = { 2, 14, 23, 28, 30, 38, 39, 41, 42 }; // ×2 around 0.5: 1.0 is neutral
  7026. const bool centred = std::find (std::begin (mode1), std::end (mode1), k) != std::end (mode1);
  7027. const bool doubled = std::find (std::begin (mode2), std::end (mode2), k) != std::end (mode2);
  7028. const double neutral = centred && ! doubled ? 0.5 : 1.0;
  7029. i32 (3);
  7030. i32 (1065353216), i32 (1065353216), i32 (1065353216);
  7031. if (k == 1 && (features & 1) != 0) // arpeggio volume envelope: a looping pattern with chord markers
  7032. 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 },
  7033. { 0.1, 0, 0.2, 0 } },
  7034. { 0, 1, -1, 0, 4 });
  7035. else if (k == 1) // volume envelope: attack, decay to the sustain point, release
  7036. 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 });
  7037. else if (k == 4) // filter-1 envelope
  7038. curve ({ { 0, 1, 0, 0 }, { 0.25, 0.2, -0.5, 0 } }, { 0, 1, -1, -1, -1 });
  7039. else
  7040. flat (neutral);
  7041. for (int a : { 128, 128, 0, 128 })
  7042. i32 (a);
  7043. if (k == 0 && panLfo) // pan LFO
  7044. flat (1.0, on);
  7045. else
  7046. flat (0.5);
  7047. for (int a : { k == 0 ? 40000 : 0, 0, 0, 0 })
  7048. i32 (a);
  7049. i32 (0);
  7050. for (int m = 2; m < 11; ++m)
  7051. if (k == 1 && m == 3)
  7052. curve ({ { 0, 0.2, 0, 0 }, { 1, 1, -0.3, 0 } }, off); // velocity → volume
  7053. else
  7054. flat (neutral);
  7055. }
  7056. for (int c = 0; c < 26; ++c)
  7057. if ((features & 16) != 0 && c == 4)
  7058. curve ({ { 0, 0.5, 0, 0 }, { 0.2, 0.8, 0.3, 0 }, { 0.8, 0.3, 0, 0 } }, off); // harmonic randomness
  7059. else if ((features & 32) != 0 && (c == 22 || c == 23))
  7060. curve ({ { 0, 0.3, 0, 0 }, { 0.6, 1.0, 0.2, 0 }, { 0.4, 0.5, 0, 0 } }, off); // phaser / harmonizer masks
  7061. else if (c == 3 && (features & 8) != 0)
  7062. curve ({ { 0, 0.2, 0, 0 }, { 1, 0.05, 0.4, 0 } }, off); // timbre 2 for the pluck blend
  7063. else if (c == 2 || c == 3)
  7064. curve ({ { 0, 0.25, 0, 0 }, { 0.006, 0.12, -0.46, 0 }, { 0.994, 0, 0, 0 } }, off); // timbres
  7065. else if (c == 14)
  7066. curve ({ { 0, 1, 0, 0 }, { 1, 0, 0, 0 } }, off); // pluck curve
  7067. else
  7068. 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)));
  7069. if (image)
  7070. {
  7071. // the image object (FUN_00793300 layout): version 0, image mode, looping, 6 columns × 40 rows, gain + freq planes
  7072. const int cols = 6, rows = 40;
  7073. i32 (0), i32 (0), i32 (1), i32 (0);
  7074. f64 (-1.0), f64 (-1.0), f64 (0.0);
  7075. i32 (cols), i32 (rows);
  7076. i32 (cols * rows);
  7077. for (int c2 = 0; c2 < cols; ++c2)
  7078. for (int r2 = 0; r2 < rows; ++r2)
  7079. {
  7080. const auto v = (float) (255.0 * std::pow (0.93, r2) * (0.6 + 0.4 * std::cos (0.9 * c2 + 0.37 * r2)));
  7081. std::uint32_t u;
  7082. std::memcpy (&u, &v, 4);
  7083. i32 ((std::int32_t) u);
  7084. }
  7085. i32 (cols * rows);
  7086. for (int c2 = 0; c2 < cols; ++c2)
  7087. for (int r2 = 0; r2 < rows; ++r2)
  7088. {
  7089. const auto v = (float) (0.3 * std::sin (1.3 * c2 + 0.5 * r2));
  7090. std::uint32_t u;
  7091. std::memcpy (&u, &v, 4);
  7092. i32 ((std::int32_t) u);
  7093. }
  7094. i32 (0);
  7095. b.push_back (0); // (file name: empty)
  7096. }
  7097. std::swap (b, part);
  7098. i32 ((std::int32_t) part.size());
  7099. b.insert (b.end(), part.begin(), part.end());
  7100. std::array<std::int32_t, 53> g {};
  7101. g[0] = g[1] = g[2] = 256, g[10] = 500, g[11] = 1000, g[12] = 1000, g[15] = 1000;
  7102. for (auto v : g)
  7103. i32 (v);
  7104. for (int i = 0; i < 6; ++i)
  7105. i32 (i);
  7106. std::array<std::uint8_t, 0x3d> gm {};
  7107. const std::int32_t cr = 200, vr = 1;
  7108. std::memcpy (gm.data() + 12, &vr, 4);
  7109. std::memcpy (gm.data() + 24, &cr, 4);
  7110. b.insert (b.end(), gm.begin(), gm.end());
  7111. return b;
  7112. }
  7113. } // namespace
  7114. TEST (harmor_engine)
  7115. {
  7116. // REA_rodada34: state parsing, the additive engine against our reference model (hmodel.py), host levels, voice end
  7117. const auto blob = harmorTestState();
  7118. if (const char* dump = std::getenv ("LYNX_DUMP_HARMOR"))
  7119. std::ofstream (dump, std::ios::binary).write ((const char*) blob.data(), (std::streamsize) blob.size());
  7120. CHECK (lynx::harmorStateUsable (blob));
  7121. CHECK (lynx::harmorFallbacks (blob).empty());
  7122. const auto at = lynx::harmorParamOffset (blob, 0, 52);
  7123. std::int32_t cutoff = 0;
  7124. CHECK (at > 0 && (std::memcpy (&cutoff, blob.data() + at, 4), cutoff == 5000));
  7125. CHECK (lynx::harmorParamOffset (blob, 1, 52) < 0 && lynx::harmorParamOffset (blob, -1, 12) > at);
  7126. auto broken = blob;
  7127. broken.resize (broken.size() / 2);
  7128. CHECK (! lynx::harmorStateUsable (broken));
  7129. auto old = blob;
  7130. old[0] = 15; // articulators and curves are stored from version 16
  7131. CHECK (! lynx::harmorStateUsable (old));
  7132. lynx::ChannelDef c;
  7133. c.instrument = lynx::InstrumentKind::harmor;
  7134. c.pluginState = blob;
  7135. auto render = [] (const lynx::ChannelDef& def, int total, float gl, float gr, int offAt) {
  7136. auto inst = lynx::makeInstrument (lynx::InstrumentKind::harmor);
  7137. inst->configure (def, 120.0);
  7138. inst->prepare (44100.0);
  7139. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  7140. inst->noteOn (60, 0.78125f);
  7141. for (int s = 0; s < total; s += 512)
  7142. {
  7143. if (s == offAt)
  7144. inst->noteOff (60);
  7145. inst->render (l.data() + s, r.data() + s, std::min (512, total - s), gl, gr);
  7146. }
  7147. return std::make_tuple (l, r, std::move (inst));
  7148. };
  7149. {
  7150. // reference model (REA round 34 hmodel.py; C5 at FL velocity 100, Final.Vol 1, centre, released at 6144, the
  7151. // Mersenne Twister seeded like LYNX's)
  7152. auto [l, r, inst] = render (c, 8192, 1.0f, 1.0f, 6144);
  7153. const std::tuple<int, float, float> golden[] {
  7154. { 0, 0.0f, 0.0f }, { 100, 0.00947693f, 0.04249846f }, { 1000, 0.08140575f, -0.03251422f }, { 3000, 0.05928446f, -0.00247572f },
  7155. { 6000, 0.02997839f, 0.14549091f }, { 7000, -0.01121871f, 0.04197952f }, { 8191, 0.00314545f, 0.03857505f } };
  7156. for (const auto& [i, gl, gr] : golden)
  7157. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  7158. // after the release the volume envelope ends, the record declicks and the instrument goes idle
  7159. std::vector<float> tail (44100), tailR (tail.size());
  7160. inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
  7161. CHECK (inst->idle());
  7162. CHECK (std::abs (tail.back()) == 0.0f);
  7163. }
  7164. // the reference renderer gives the same samples as the instrument
  7165. {
  7166. const auto ref = lynx::harmor::renderReference (blob, { { 0, 60, 0.78125, 1.0, 0.0, 6144 } }, 44100.0, 8192);
  7167. const auto [l, r, inst] = render (c, 8192, 1.0f, 1.0f, 6144);
  7168. double worst = 0.0;
  7169. for (int i = 0; i < 8192; ++i)
  7170. 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]) });
  7171. CHECK (worst == 0.0);
  7172. }
  7173. // FL's level law applied once: half the channel volume halves the output; full right pan silences the left side
  7174. const auto full = std::get<0> (render (c, 2048, 1.0f, 1.0f, -1));
  7175. const auto half = std::get<0> (render (c, 2048, 0.5f, 0.5f, -1));
  7176. CHECK (std::abs (half[1000] - 0.5f * full[1000]) < 1e-6f && std::abs (full[1000]) > 0.01f);
  7177. auto mono = c;
  7178. mono.pluginState = harmorTestState (false); // no pan LFO ...
  7179. const std::int32_t one = 1;
  7180. std::memcpy (mono.pluginState.data() + lynx::harmorParamOffset (mono.pluginState, 0, 34), &one, 4); // ... nor unison spread
  7181. const auto [pl, pr, pi] = render (mono, 2048, 0.0f, 1.41421356f, -1);
  7182. double el = 0.0, er = 0.0;
  7183. for (int i = 0; i < 2048; ++i)
  7184. el += (double) pl[(std::size_t) i] * pl[(std::size_t) i], er += (double) pr[(std::size_t) i] * pr[(std::size_t) i];
  7185. CHECK (el < 1e-10 * er && er > 1e-3);
  7186. }
  7187. namespace
  7188. {
  7189. // An Ogun state written from scratch (REA_rodada37 §1.3 layout, version 18; zlib with stored blocks): richness 4
  7190. // (255 partials), pre-decay, decay/release, fullness, Seed 1 + fixed Seed 2 with morph, 3-voice unison, a 2-stage
  7191. // low-pass with an LFO on its cutoff, a volume envelope with a sustain point and a velocity map, declick, normalisation,
  7192. // Impulse/Decay maps, and every plugin effect (EQ band, chorus, ping-pong delay, reverb). Everything else is neutral.
  7193. std::vector<std::uint8_t> ogunTestState()
  7194. {
  7195. std::vector<std::uint8_t> b;
  7196. 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))); };
  7197. auto f32v = [&] (float v) { std::uint32_t u; std::memcpy (&u, &v, 4); i32 ((std::int32_t) u); };
  7198. 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))); };
  7199. struct Pt { double dx, y, tension; int mode; };
  7200. auto curve = [&] (int type, const std::vector<Pt>& pts, std::array<std::int32_t, 5> hdr) {
  7201. i32 (type), i32 (3), i32 ((std::int32_t) pts.size());
  7202. for (const auto& p : pts)
  7203. {
  7204. f64 (p.dx), f64 (p.y), f32v ((float) p.tension);
  7205. b.push_back ((std::uint8_t) p.mode), b.push_back (0), b.push_back (0), b.push_back (0);
  7206. }
  7207. for (auto h : hdr)
  7208. i32 (h);
  7209. };
  7210. const std::array<std::int32_t, 5> off { 0, 0, -1, -1, -1 }, on { 0, 1, -1, -1, -1 };
  7211. auto flat = [&] (int type, double y) { curve (type, { { 0, y, 0, 0 }, { 1, y, 0, 0 } }, off); };
  7212. std::array<std::int32_t, 64> P {};
  7213. const std::pair<int, int> values[] = { { 0, 128 }, { 5, 300 }, { 6, 3000 }, { 7, 2000 }, { 8, 200 }, { 9, 500 }, { 10, 12 },
  7214. { 11, 34 }, { 12, 3 }, { 13, 64 }, { 14, 64 }, { 15, 40 }, { 16, 64 }, { 17, 180 }, { 18, 60 }, { 19, 2 }, { 20, 300 },
  7215. { 21, 400 }, { 22, 40 }, { 23, 30 }, { 24, 20 }, { 25, 64 }, { 26, 40 }, { 27, 96 }, { 28, 100 }, { 29, 64 }, { 30, 2 },
  7216. { 31, 19 }, { 32, 221 }, { 33, 0 }, { 34, 50 }, { 35, 100 }, { 36, 20 }, { 37, 60 }, { 38, 2 }, { 39, 64 }, { 40, 300 },
  7217. { 43, 30000 }, { 46, 20000 }, { 49, 0 }, { 50, 2 }, { 51, 0 }, { 52, 9 }, { 53, 1 }, { 54, 0 },
  7218. { 55, 0x01000000 }, // +0x20b: pitch follows the key
  7219. { 56, 0x00010000 }, // +0x20e: declick
  7220. { 62, 0x00000100 } }; // +0x225: normalise
  7221. for (const auto& [i, v] : values)
  7222. P[(std::size_t) i] = v;
  7223. for (auto v : P)
  7224. i32 (v);
  7225. b.push_back (0), b.push_back (1), b.push_back (0); // delay: not tempo-based, on
  7226. i32 (1), i32 (1), i32 (1); // EQ band types: peaking
  7227. b.push_back (0); // comment: 0 characters
  7228. for (int k = 0; k < 13; ++k)
  7229. {
  7230. static constexpr int sum[] = { 1, 2, 3, 9, 11, 12 };
  7231. const double neutral = std::find (std::begin (sum), std::end (sum), k) != std::end (sum) ? 0.5 : 1.0;
  7232. i32 (1);
  7233. f32v (0.0f), f32v (1.0f), f32v (k == 1 ? 0.3f : 0.0f);
  7234. if (k == 0) // volume envelope: attack, decay to the sustain point, release
  7235. 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 });
  7236. else
  7237. flat (1, neutral);
  7238. for (int a : { 128, 128, 128, 128 })
  7239. i32 (a);
  7240. if (k == 1) // cutoff LFO
  7241. curve (2, { { 0, 0.8, 0, 0 } }, on);
  7242. else
  7243. flat (2, 0.5);
  7244. for (int a : { k == 1 ? 40000 : 0, 0, 0, 0 })
  7245. i32 (a);
  7246. i32 (0);
  7247. for (int m = 0; m < 4; ++m)
  7248. if (k == 0 && m == 1)
  7249. curve (3, { { 0, 0.3, 0, 0 }, { 1, 1, -0.2, 0 } }, off); // velocity → volume
  7250. else
  7251. flat (3, neutral);
  7252. }
  7253. flat (3, 0.0); // Forced: none
  7254. curve (3, { { 0, 0.6, 0, 0 }, { 1, 0.4, 0.3, 0 } }, off); // Impulse EQ
  7255. curve (3, { { 0, 0.5, 0, 0 }, { 1, 0.3, 0, 0 } }, off); // Decay EQ
  7256. flat (3, 0.5); // Release EQ
  7257. // u32 version + zlib (stored blocks)
  7258. std::vector<std::uint8_t> s { 18, 0, 0, 0, 0x78, 0x01 };
  7259. for (std::size_t i = 0; i < b.size();)
  7260. {
  7261. const auto len = std::min<std::size_t> (65535, b.size() - i);
  7262. s.push_back (i + len >= b.size() ? 1 : 0);
  7263. s.push_back ((std::uint8_t) len), s.push_back ((std::uint8_t) (len >> 8));
  7264. s.push_back ((std::uint8_t) ~len), s.push_back ((std::uint8_t) (~len >> 8));
  7265. s.insert (s.end(), b.begin() + (std::ptrdiff_t) i, b.begin() + (std::ptrdiff_t) (i + len));
  7266. i += len;
  7267. }
  7268. std::uint32_t a1 = 1, a2 = 0;
  7269. for (auto c : b)
  7270. a1 = (a1 + c) % 65521u, a2 = (a2 + a1) % 65521u;
  7271. for (int sh = 24; sh >= 0; sh -= 8)
  7272. s.push_back ((std::uint8_t) (((a2 << 16) | a1) >> sh));
  7273. return s;
  7274. }
  7275. } // namespace
  7276. TEST (ogun_engine)
  7277. {
  7278. // REA_rodada37: state parsing, the engine against our reference model (ogmodel.py), host levels, export quality
  7279. const auto blob = ogunTestState();
  7280. if (const char* dump = std::getenv ("LYNX_DUMP_OGUN"))
  7281. std::ofstream (dump, std::ios::binary).write ((const char*) blob.data(), (std::streamsize) blob.size());
  7282. CHECK (lynx::ogunStateUsable (blob));
  7283. CHECK (lynx::ogunFallbacks (blob).empty());
  7284. CHECK (lynx::ogunParam (blob, 17) == 180 && lynx::ogunParam (blob, 52) == 9 && ! lynx::ogunParam (blob, 64));
  7285. auto broken = blob;
  7286. broken.resize (broken.size() - 300);
  7287. CHECK (! lynx::ogunStateUsable (broken));
  7288. auto old = blob;
  7289. old[0] = 13; // the articulator layout is from version 14
  7290. CHECK (! lynx::ogunStateUsable (old));
  7291. // reference model (REA round 37 ogmodel.Plugin(state).run: C5 velocity 0.75 released at 6144, G5 velocity 0.625 at 2048
  7292. // released at 8192; host block 512)
  7293. 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 } };
  7294. const auto ref = lynx::ogun::renderReference (blob, notes, 44100.0, 16384);
  7295. CHECK (ref.size() == 2 * 16384);
  7296. if (const char* dump = std::getenv ("LYNX_DUMP_OGUN_OUT"))
  7297. std::ofstream (dump, std::ios::binary).write ((const char*) ref.data(), (std::streamsize) (ref.size() * 4));
  7298. const std::tuple<int, float, float> golden[] {
  7299. { 100, -7.763202e-05f, -8.919523e-05f }, { 1000, 0.06195267f, 0.03858230f }, { 3000, 0.01363476f, -0.00378433f },
  7300. { 6000, -0.06601223f, -0.03084030f }, { 9000, 0.04204506f, 0.02452896f }, { 12000, 0.03420147f, -0.01837870f },
  7301. { 16383, -0.00465311f, -0.00119627f } };
  7302. for (const auto& [i, gl, gr] : golden)
  7303. CHECK (std::abs (ref[2 * (std::size_t) i] - gl) < 2e-6f && std::abs (ref[2 * (std::size_t) i + 1] - gr) < 2e-6f);
  7304. // the instrument plays like the reference renderer (Final.Vol 1, centre)
  7305. lynx::ChannelDef c;
  7306. c.instrument = lynx::InstrumentKind::ogun;
  7307. c.pluginState = blob;
  7308. auto render = [&] (const lynx::ChannelDef& def, int total, float gl, float gr, bool held = false) {
  7309. auto inst = lynx::makeInstrument (lynx::InstrumentKind::ogun);
  7310. inst->configure (def, 120.0);
  7311. inst->prepare (44100.0);
  7312. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  7313. for (int s = 0; s < total; s += 512)
  7314. {
  7315. if (s == 0)
  7316. inst->noteOn (60, 0.75f);
  7317. if (s == 2048)
  7318. inst->noteOn (67, 0.625f);
  7319. if (s == 6144 && ! held)
  7320. inst->noteOff (60);
  7321. if (s == 8192 && ! held)
  7322. inst->noteOff (67);
  7323. inst->render (l.data() + s, r.data() + s, std::min (512, total - s), gl, gr);
  7324. }
  7325. return std::make_tuple (l, r, std::move (inst));
  7326. };
  7327. {
  7328. auto [l, r, inst] = render (c, 16384, 1.0f, 1.0f);
  7329. double worst = 0.0;
  7330. for (int i = 0; i < 16384; ++i)
  7331. 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]) });
  7332. CHECK (worst == 0.0);
  7333. // the voices end and the effect tails die out: the instrument goes idle
  7334. std::vector<float> tail (44100), tailR (tail.size());
  7335. for (int k = 0; k < 20 && ! inst->idle(); ++k)
  7336. inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
  7337. CHECK (inst->idle());
  7338. }
  7339. // FL's level law applied once (effects off): half the channel volume halves the output once the gain ramps
  7340. // (limited to 0.001 per sample) have settled on the sustain level; full right pan silences the left side
  7341. {
  7342. auto dry = c;
  7343. for (int i : { 19, 29, 40, 53 })
  7344. CHECK (lynx::ogunSetParam (dry.pluginState, i, 0)); // chorus, delay wet, EQ band and reverb off
  7345. CHECK (lynx::ogunStateUsable (dry.pluginState) && lynx::ogunParam (dry.pluginState, 19) == 0);
  7346. const auto full = std::get<0> (render (dry, 32768, 1.0f, 1.0f, true));
  7347. const auto half = std::get<0> (render (dry, 32768, 0.5f, 0.5f, true));
  7348. double worst = 0.0, peak = 0.0;
  7349. for (int i = 30000; i < 32768; ++i)
  7350. worst = std::max (worst, (double) std::abs (half[(std::size_t) i] - 0.5f * full[(std::size_t) i])),
  7351. peak = std::max (peak, (double) std::abs (full[(std::size_t) i]));
  7352. CHECK (worst <= 1e-7 * peak && peak > 1e-3);
  7353. // FL's default channel volume (100/128): FL hands Ogun Final.Vol = (11^v − 1)/10 (REA_rodada28 §4.1); LYNX's
  7354. // centred channel gains (√2·V·cos(π/4) each) give the reference model's level at that Final.Vol
  7355. const auto V = lynx::channelGain (0.78125f);
  7356. const auto g = V * std::cos (0.25f * 3.14159265f) * 1.41421356f;
  7357. const auto atDefault = std::get<0> (render (dry, 32768, g, g, true));
  7358. const auto refDefault = lynx::ogun::renderReference (dry.pluginState, { { 0, 60, 0.75, V, 0.0, 0.0, 0.0, 0.0, 0.5, -1 },
  7359. { 2048, 67, 0.625, V, 0.0, 0.0, 0.0, 0.0, 0.5, -1 } }, 44100.0, 32768);
  7360. double ei = 0.0, er0 = 0.0;
  7361. for (int i = 30000; i < 32768; ++i)
  7362. 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];
  7363. CHECK (er0 > 0.0 && std::abs (10.0 * std::log10 (ei / er0)) < 0.001);
  7364. const auto [pl, pr, pi] = render (dry, 4096, 0.0f, 1.41421356f);
  7365. double el = 0.0, er = 0.0;
  7366. for (int i = 0; i < 4096; ++i)
  7367. el += (double) pl[(std::size_t) i] * pl[(std::size_t) i], er += (double) pr[(std::size_t) i] * pr[(std::size_t) i];
  7368. CHECK (el < 1e-10 * er && er > 1e-3);
  7369. }
  7370. // export: FL renders Ogun at quality 4 (×2 rate, 32-point reader), LYNX too while it renders offline
  7371. {
  7372. lynx::setOfflineRendering (true);
  7373. auto [l, r, inst] = render (c, 8192, 1.0f, 1.0f);
  7374. lynx::setOfflineRendering (false);
  7375. lynx::ogun::RefOptions q4;
  7376. q4.quality = 4;
  7377. const auto ref4 = lynx::ogun::renderReference (blob, notes, 44100.0, 8192, q4);
  7378. double worst = 0.0, diff = 0.0;
  7379. for (int i = 0; i < 8192; ++i)
  7380. worst = std::max (worst, (double) std::abs (ref4[2 * (std::size_t) i] - l[(std::size_t) i])),
  7381. diff = std::max (diff, (double) std::abs (ref[2 * (std::size_t) i] - l[(std::size_t) i]));
  7382. CHECK (worst == 0.0 && diff > 1e-3);
  7383. const std::tuple<int, float, float> golden4[] {
  7384. { 100, 0.00605961f, 0.00628969f }, { 1000, 0.03729841f, 0.01727954f }, { 3000, -0.09010383f, -0.02551294f },
  7385. { 6000, 0.02167798f, -0.01115632f }, { 8191, 0.00320288f, -0.00615792f } };
  7386. for (const auto& [i, gl, gr] : golden4)
  7387. CHECK (std::abs (ref4[2 * (std::size_t) i] - gl) < 2e-6f && std::abs (ref4[2 * (std::size_t) i + 1] - gr) < 2e-6f);
  7388. }
  7389. // feature variants of the same state against the reference model (energy per side over 8192 samples, three
  7390. // samples; C1 released at 6144): qualities, richness, unison, the filter types, ALT DECAY, 1/√h, Seed 2 Random,
  7391. // infinite decay, logarithmic axis, velocity → position, other rates. Goldens from ogmodel.py (REA round 37).
  7392. {
  7393. struct Sample { int at; float l, r; };
  7394. struct Variant { const char* name; std::vector<std::pair<int, int>> params; int rate; double el, er; Sample s[3]; };
  7395. const Variant variants[] {
  7396. { "q1", { { 54, 1 } }, 44100, 11.0970402, 6.87820714, { { 1500, 0.0240363069f, -0.0148524037f }, { 4000, 0.0873190612f, -0.000715861097f }, { 7000, -0.0378603041f, -0.00338819111f } } },
  7397. { "q2", { { 54, 2 } }, 44100, 11.2405446, 6.68045082, { { 1500, 0.0448660068f, -0.00560745923f }, { 4000, -0.0122657465f, 0.0104285963f }, { 7000, -0.0216034129f, -0.0134632047f } } },
  7398. { "q3", { { 54, 3 } }, 44100, 11.3200608, 6.86196449, { { 1500, -0.0031537537f, -0.0242648814f }, { 4000, 0.0426301025f, 0.0364697985f }, { 7000, 0.00843803957f, 0.0302239582f } } },
  7399. { "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 } } },
  7400. { "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 } } },
  7401. { "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 } } },
  7402. { "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 } } },
  7403. { "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 } } },
  7404. { "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 } } },
  7405. { "alt", { { 62, 257 } }, 44100, 17.2485711, 9.99223979, { { 1500, -0.0319156498f, -0.0186850764f }, { 4000, -0.0769012421f, -0.0375530645f }, { 7000, 0.0310976505f, 0.0445244014f } } },
  7406. { "isqrt", { { 62, 65792 } }, 44100, 45.7309307, 27.1224593, { { 1500, -0.04376591f, -0.00845711026f }, { 4000, 0.0351300463f, -0.129557073f }, { 7000, 0.0096148327f, 0.00123740174f } } },
  7407. { "seed2rnd", { { 11, 0 } }, 44100, 16.8228819, 11.5433061, { { 1500, -0.0117614279f, -0.026497297f }, { 4000, 0.00265733898f, -0.07069581f }, { 7000, 0.00576663949f, 0.0565217957f } } },
  7408. { "decinf", { { 6, 10000 } }, 44100, 19.1879226, 11.0487311, { { 1500, -0.0324105881f, -0.0190765262f }, { 4000, -0.0809780285f, -0.0395730771f }, { 7000, 0.034197174f, 0.048585102f } } },
  7409. { "logx", { { 57, 16777216 } }, 44100, 12.3064326, 7.31519289, { { 1500, -0.0438990779f, 0.0096983267f }, { 4000, -0.0175748877f, 0.0313935578f }, { 7000, 0.0639200211f, 0.024419222f } } },
  7410. { "velpos", { { 56, 66304 } }, 44100, 4.9966925, 3.16540662, { { 1500, 0.0083851777f, -0.0165472236f }, { 4000, 0.0630451962f, 0.0156494901f }, { 7000, -0.0293043777f, -0.00182877108f } } },
  7411. { "fs48", { }, 48000, 10.8813888, 6.53592373, { { 1500, -0.0485184453f, -0.0319588967f }, { 4000, 0.0230421089f, -0.0286604818f }, { 7000, -0.0117630213f, -0.017459048f } } },
  7412. { "fs96", { }, 96000, 11.0189683, 6.39821883, { { 1500, -0.0267860368f, -0.0227564126f }, { 4000, 0.0238709711f, -0.0100849615f }, { 7000, -0.0034619756f, 0.0451687202f } } },
  7413. };
  7414. for (const auto& v : variants)
  7415. {
  7416. auto st = blob;
  7417. for (const auto& [i, x] : v.params)
  7418. CHECK (lynx::ogunSetParam (st, i, x));
  7419. const auto y = lynx::ogun::renderReference (st, { { 0, 60, 0.75, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 6144 },
  7420. { 2048, 67, 0.625, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1 } }, (double) v.rate, 8192);
  7421. double el = 0.0, er = 0.0, peak = 0.0;
  7422. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  7423. 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]) });
  7424. bool ok = y.size() == 2 * 8192 && std::abs (el - v.el) <= 1e-5 * v.el && std::abs (er - v.er) <= 1e-5 * v.er;
  7425. for (const auto& smp : v.s)
  7426. 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;
  7427. if (! ok)
  7428. std::printf (" Ogun variant %s differs from the model: energy %.9g %.9g (model %.9g %.9g)\n", v.name, el, er, v.el, v.er);
  7429. CHECK (ok);
  7430. }
  7431. }
  7432. }
  7433. // our own Maximus v7 state (layout REA_rodada8 §1.1): 7 globals, 4 × 13 band parameters, 8 envelopes (dx, y, tension, type)
  7434. static std::vector<std::uint8_t> maximusTestState (const std::array<int, 7>& g, const std::array<std::array<int, 13>, 4>& bands,
  7435. const std::array<std::vector<std::array<double, 4>>, 8>& envs, int oversampling)
  7436. {
  7437. std::vector<std::uint8_t> d;
  7438. auto put = [&] (const void* v, std::size_t n) { d.insert (d.end(), (const std::uint8_t*) v, (const std::uint8_t*) v + n); };
  7439. auto i32 = [&] (std::int32_t v) { put (&v, 4); };
  7440. i32 (7);
  7441. for (int copy = 0; copy < 2; ++copy)
  7442. {
  7443. for (auto v : g)
  7444. i32 (v);
  7445. for (const auto& b : bands)
  7446. for (auto v : b)
  7447. i32 (v);
  7448. }
  7449. d.resize (0x1fe, 0);
  7450. std::memcpy (d.data() + 0x1f4, &oversampling, 4);
  7451. for (const auto& e : envs)
  7452. {
  7453. i32 (3), i32 (2), i32 ((std::int32_t) e.size());
  7454. for (const auto& pt : e)
  7455. {
  7456. const double dx = pt[0], y = pt[1];
  7457. const float t = (float) pt[2];
  7458. put (&dx, 8), put (&y, 8), put (&t, 4);
  7459. const std::uint8_t flags[4] { (std::uint8_t) pt[3], 0, 0, 0 };
  7460. put (flags, 4);
  7461. }
  7462. i32 (0), i32 (-1), i32 (-1), i32 (-1), i32 (-1);
  7463. }
  7464. return d;
  7465. }
  7466. // A "Kepler-1" state written from scratch (REA_rodada46 §1.1 layout, version 2): the default panel values
  7467. std::vector<std::uint8_t> keplerTestState()
  7468. {
  7469. 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 };
  7470. std::vector<std::uint8_t> st;
  7471. auto u32 = [&] (std::uint32_t v) { for (int i = 0; i < 4; ++i) st.push_back ((std::uint8_t) (v >> (8 * i))); };
  7472. auto f32 = [&] (float f) { std::uint32_t v; std::memcpy (&v, &f, 4); u32 (v); };
  7473. u32 (0), u32 (0), u32 (0), u32 (9);
  7474. for (char c : std::string ("Kepler-1"))
  7475. st.push_back ((std::uint8_t) c);
  7476. st.push_back (0);
  7477. u32 (2), u32 (34);
  7478. for (auto f : panel)
  7479. f32 (f);
  7480. st.push_back (0);
  7481. f32 (1.0f), f32 (0.5f);
  7482. const auto size = (std::uint32_t) st.size() - 12;
  7483. std::memcpy (st.data() + 4, &size, 4);
  7484. return st;
  7485. }
  7486. std::string keplerDll()
  7487. {
  7488. if (realFlpDir.empty())
  7489. return {};
  7490. return (std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "Kepler" / "engine_x64.dll").string();
  7491. }
  7492. TEST (kepler_engine)
  7493. {
  7494. const auto st = keplerTestState();
  7495. CHECK (st.size() == 178 && lynx::keplerStateUsable (st));
  7496. CHECK (lynx::keplerPanelOffset (st, 19) == 0x21 + 4 * 19 && lynx::keplerPanelOffset (st, 34) == -1);
  7497. auto broken = st;
  7498. broken[17] = 'X'; // not "Kepler-1"
  7499. CHECK (! lynx::keplerStateUsable (broken) && lynx::keplerPanelOffset (broken, 0) == -1);
  7500. auto v3 = st;
  7501. v3[0x19] = 3; // version 3 needs the gate / noise / oversampling bytes
  7502. CHECK (! lynx::keplerStateUsable (v3));
  7503. const auto dll = keplerDll();
  7504. if (dll.empty() || ! lynx::keplerDllUsable (dll))
  7505. return; // no FL installation (the engine needs the user's own DLL tables)
  7506. // against FL's engine DLL (EXEC, REA_rodada46 §6): three notes, 44.1 kHz, host blocks of 512 — bit-exact
  7507. 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 } };
  7508. const auto y = lynx::kepler::renderReference (st, dll, notes, 44100.0, 39690);
  7509. CHECK (y.size() == 2 * 39690);
  7510. if (y.size() != 2 * 39690)
  7511. return;
  7512. double el = 0.0, er = 0.0;
  7513. for (std::size_t i = 0; i < y.size(); i += 2)
  7514. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  7515. std::printf (" Kepler default patch: energy %.9g %.9g (FL's DLL 250.641268)\n", el, er);
  7516. CHECK (std::abs (el - 250.641268) < 1e-6 && std::abs (er - 250.641268) < 1e-6);
  7517. const std::tuple<int, float, float> golden[] { { 5000, 0.0467273332f, 0.0467273332f }, { 12000, -0.119412214f, -0.119412214f },
  7518. { 25000, 0.0855825394f, 0.0855825394f }, { 35000, -3.34166579e-08f, -3.34166579e-08f } };
  7519. for (const auto& [i, l, r] : golden)
  7520. CHECK (y[2 * (std::size_t) i] == l && y[2 * (std::size_t) i + 1] == r);
  7521. // the instrument: FL's wrapper velocity, channel volume as an output gain, idle after the release
  7522. lynx::ChannelDef c;
  7523. c.instrument = lynx::InstrumentKind::kepler;
  7524. c.pluginState = st;
  7525. c.samplePath = dll;
  7526. auto inst = lynx::makeInstrument (lynx::InstrumentKind::kepler);
  7527. inst->configure (c, 120.0);
  7528. inst->prepare (44100.0);
  7529. std::vector<float> l (4096), r (4096);
  7530. inst->noteOn (60, 0.8f);
  7531. double e = 0.0;
  7532. bool finite = true;
  7533. for (int b = 0; b < 8; ++b)
  7534. {
  7535. std::fill (l.begin(), l.end(), 0.0f);
  7536. std::fill (r.begin(), r.end(), 0.0f);
  7537. inst->render (l.data(), r.data(), 4096, 0.5f, 0.5f);
  7538. for (int i = 0; i < 4096; ++i)
  7539. 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];
  7540. if (b == 3)
  7541. inst->noteOff (60);
  7542. }
  7543. CHECK (finite && e > 1.0);
  7544. for (int b = 0; b < 40; ++b)
  7545. inst->render (l.data(), r.data(), 4096, 0.5f, 0.5f);
  7546. CHECK (inst->idle());
  7547. }
  7548. // REA round 57 §2: Transistor Bass
  7549. // the plugin state (event 0xD5) of an FL preset file (.fst: the FLhd/FLdt event stream)
  7550. std::vector<std::uint8_t> tbassPresetState (const char* name)
  7551. {
  7552. if (realFlpDir.empty())
  7553. return {};
  7554. std::ifstream in (std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "Transistor Bass" / name, std::ios::binary);
  7555. const std::vector<std::uint8_t> b ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  7556. std::size_t i = 0;
  7557. while (i + 8 <= b.size() && std::memcmp (b.data() + i, "FLdt", 4) != 0)
  7558. ++i;
  7559. i += 8;
  7560. while (i < b.size())
  7561. {
  7562. const auto id = b[i++];
  7563. std::size_t n = id < 64 ? 1 : id < 128 ? 2 : id < 192 ? 4 : 0;
  7564. if (id >= 192)
  7565. for (int shift = 0; i < b.size(); shift += 7)
  7566. {
  7567. const auto c = b[i++];
  7568. n |= (std::size_t) (c & 0x7F) << shift;
  7569. if (c < 0x80)
  7570. break;
  7571. }
  7572. if (id == 0xD5 && i + n <= b.size())
  7573. return { b.begin() + (std::ptrdiff_t) i, b.begin() + (std::ptrdiff_t) (i + n) };
  7574. i += n;
  7575. }
  7576. return {};
  7577. }
  7578. // the same state re-saved as engine-stream version 5 with an engine selector (REA_rodada57 §2.2)
  7579. std::vector<std::uint8_t> tbassWithEngine (const std::vector<std::uint8_t>& s, std::int32_t engine)
  7580. {
  7581. std::vector<std::uint8_t> out (s.begin(), s.begin() + 4);
  7582. auto put = [&] (const void* p, std::size_t n) { out.insert (out.end(), (const std::uint8_t*) p, (const std::uint8_t*) p + n); };
  7583. for (std::size_t at = 4; at + 12 <= s.size();)
  7584. {
  7585. std::uint32_t id;
  7586. std::uint64_t n;
  7587. std::memcpy (&id, s.data() + at, 4);
  7588. std::memcpy (&n, s.data() + at + 4, 8);
  7589. std::vector<std::uint8_t> d (s.begin() + (std::ptrdiff_t) (at + 12), s.begin() + (std::ptrdiff_t) (at + 12 + n));
  7590. at += 12 + (std::size_t) n;
  7591. if (id == 2)
  7592. {
  7593. std::uint64_t m;
  7594. std::memcpy (&m, d.data() + 4, 8);
  7595. std::vector<std::uint8_t> e (d.begin() + 12, d.begin() + 12 + (std::ptrdiff_t) m);
  7596. std::int16_t v;
  7597. std::memcpy (&v, e.data(), 2);
  7598. if (v < 5)
  7599. {
  7600. v = 5;
  7601. std::memcpy (e.data(), &v, 2);
  7602. e.insert (e.end(), 4, 0);
  7603. }
  7604. std::memcpy (e.data() + e.size() - 4, &engine, 4);
  7605. m = e.size();
  7606. d.resize (12);
  7607. std::memcpy (d.data() + 4, &m, 8);
  7608. d.insert (d.end(), e.begin(), e.end());
  7609. }
  7610. const std::uint64_t size = d.size();
  7611. put (&id, 4);
  7612. put (&size, 8);
  7613. put (d.data(), d.size());
  7614. }
  7615. return out;
  7616. }
  7617. std::string tbassDll()
  7618. {
  7619. if (realFlpDir.empty())
  7620. return {};
  7621. return (std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "Transistor Bass" / "engine_x64.dll").string();
  7622. }
  7623. // Plucked! (REA_rodada65 §1): the C++ engine against FL's DLL (EXEC: same notes, the DLL's RandSeed set before the first
  7624. // note, the host killing voices when the plugin asks). LYNX renders these bit-identically to the DLL.
  7625. TEST (plucked_engine)
  7626. {
  7627. auto stateOf = [] (std::initializer_list<std::int32_t> k) {
  7628. std::vector<std::uint8_t> st (20);
  7629. std::memcpy (st.data(), std::data (k), 20);
  7630. return st;
  7631. };
  7632. CHECK (! lynx::pluckedStateUsable (std::vector<std::uint8_t> (19)));
  7633. CHECK (lynx::pluckedStateUsable (stateOf ({ 128, 64, 1, 0, 1 })));
  7634. CHECK (lynx::pluckedParams (stateOf ({ 7, 9, 0, 1, 0 })) == std::vector<int> ({ 7, 9, 0, 1, 0 }));
  7635. using N = lynx::plucked::RefNote;
  7636. struct Sample { int at; float l, r; };
  7637. struct Golden { int set; std::int32_t P[5]; double fs; int block, law, maxPoly; double el, er; Sample s[4]; };
  7638. const std::vector<std::vector<N>> sets {
  7639. { { 0, 60, 1.0, 0.0, 0.0, 0.0, 0.0, -1 } },
  7640. { { 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 } },
  7641. { { 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 } },
  7642. { { 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 } },
  7643. { { 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 } },
  7644. };
  7645. const Golden goldens[] {
  7646. { 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 } } },
  7647. { 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 } } },
  7648. { 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 } } },
  7649. { 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 } } },
  7650. { 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 } } },
  7651. };
  7652. for (const auto& g : goldens)
  7653. {
  7654. const auto st = stateOf ({ g.P[0], g.P[1], g.P[2], g.P[3], g.P[4] });
  7655. const auto length = (int) g.fs * 2;
  7656. const auto y = lynx::plucked::renderReference (st, sets[(std::size_t) g.set], g.fs, length, g.block, 777u + (std::uint32_t) g.set,
  7657. g.law != 0, g.maxPoly);
  7658. double el = 0.0, er = 0.0;
  7659. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  7660. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  7661. 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;
  7662. for (const auto& smp : g.s)
  7663. 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;
  7664. if (! ok)
  7665. 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);
  7666. CHECK (ok);
  7667. }
  7668. // the instrument: plays, the gate makes a note-off silence the string, a held string dies out by itself
  7669. lynx::ChannelDef c;
  7670. c.instrument = lynx::InstrumentKind::plucked;
  7671. c.pluginState = stateOf ({ 200, 40, 1, 1, 1 });
  7672. auto inst = lynx::makeInstrument (lynx::InstrumentKind::plucked);
  7673. inst->configure (c, 120.0);
  7674. inst->prepare (44100.0);
  7675. std::vector<float> l (1024), r (1024);
  7676. inst->noteOn (57, 0.8f);
  7677. double e = 0.0;
  7678. for (int b = 0; b < 8; ++b)
  7679. {
  7680. std::fill (l.begin(), l.end(), 0.0f);
  7681. std::fill (r.begin(), r.end(), 0.0f);
  7682. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  7683. for (int i = 0; i < 1024; ++i)
  7684. e += (double) l[(std::size_t) i] * l[(std::size_t) i] + (double) r[(std::size_t) i] * r[(std::size_t) i];
  7685. }
  7686. CHECK (e > 1.0 && ! inst->idle());
  7687. inst->noteOff (57);
  7688. for (int b = 0; b < 4; ++b)
  7689. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  7690. CHECK (inst->idle());
  7691. c.pluginState = stateOf ({ 0, 64, 0, 0, 0 });
  7692. inst->configure (c, 120.0);
  7693. inst->noteOn (60, 1.0f);
  7694. inst->noteOff (60); // (no gate: the string rings on)
  7695. for (int b = 0; b < 2000 && ! inst->idle(); ++b)
  7696. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  7697. CHECK (inst->idle());
  7698. }
  7699. // BeepMap (REA_rodada65 §2): the C++ engine against FL's DLL (EXEC: same notes, the DLL's RandSeed set before the first
  7700. // note, the host killing voices when the plugin asks): a synthetic 9×6 bitmap with every Scale mode and switch, and two
  7701. // of the installation's presets. LYNX renders these bit-identically to the DLL.
  7702. std::vector<std::uint8_t> beepMapTestState (std::initializer_list<std::int32_t> settings)
  7703. {
  7704. constexpr int W = 9, H = 6;
  7705. std::vector<std::uint8_t> img (54 + 4 * W * H, 0);
  7706. 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)); };
  7707. img[0] = 'B', img[1] = 'M';
  7708. 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);
  7709. put (34, 4 * W * H, 4);
  7710. for (int y = 0; y < H; ++y) // bottom-up rows
  7711. for (int x = 0; x < W; ++x)
  7712. {
  7713. const auto yy = H - 1 - y;
  7714. auto* px = img.data() + 54 + 4 * (y * W + x);
  7715. px[0] = (std::uint8_t) ((x * 91 + yy * 29) & 255), px[1] = (std::uint8_t) ((x * 13 + yy * 71) & 255);
  7716. px[2] = (std::uint8_t) ((x * 37 + yy * 11) & 255), px[3] = 255;
  7717. }
  7718. std::vector<std::uint8_t> st (4 + 60 + 4, 0);
  7719. std::size_t k = 0;
  7720. for (auto v : settings)
  7721. std::memcpy (st.data() + 4 + 4 * k++, &v, 4);
  7722. const auto len = (std::int32_t) img.size();
  7723. std::memcpy (st.data() + 64, &len, 4);
  7724. st.insert (st.end(), img.begin(), img.end());
  7725. return st;
  7726. }
  7727. // the 0xD5 state of one of the installation's BeepMap presets (empty without an installation)
  7728. std::vector<std::uint8_t> beepMapPresetState (const char* name)
  7729. {
  7730. if (realFlpDir.empty())
  7731. return {};
  7732. std::ifstream in (std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "BeepMap" / name, std::ios::binary);
  7733. const std::vector<std::uint8_t> b ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  7734. std::size_t i = 0;
  7735. while (i + 8 <= b.size() && std::memcmp (b.data() + i, "FLdt", 4) != 0)
  7736. ++i;
  7737. i += 8;
  7738. while (i < b.size())
  7739. {
  7740. const auto id = b[i++];
  7741. std::size_t n = id < 64 ? 1 : id < 128 ? 2 : id < 192 ? 4 : 0;
  7742. if (id >= 192)
  7743. for (int shift = 0; i < b.size(); shift += 7)
  7744. {
  7745. const auto c = b[i++];
  7746. n |= (std::size_t) (c & 0x7F) << shift;
  7747. if (c < 0x80)
  7748. break;
  7749. }
  7750. if (id == 0xD5 && i + n <= b.size())
  7751. return { b.begin() + (std::ptrdiff_t) i, b.begin() + (std::ptrdiff_t) (i + n) };
  7752. i += n;
  7753. }
  7754. return {};
  7755. }
  7756. TEST (beepmap_engine)
  7757. {
  7758. const auto synthA = beepMapTestState ({ 3000, 200, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64 });
  7759. CHECK (lynx::beepMapStateUsable (synthA));
  7760. CHECK (lynx::beepMapParams (synthA)->at (1) == 200 && lynx::beepMapParamOffset (synthA, 2) == 12);
  7761. auto broken = synthA;
  7762. broken[0] = 2; // versions above 1 are refused
  7763. CHECK (! lynx::beepMapStateUsable (broken));
  7764. broken = synthA;
  7765. broken.resize (broken.size() - 20); // truncated bitmap
  7766. CHECK (! lynx::beepMapStateUsable (broken));
  7767. using N = lynx::beepmap::RefNote;
  7768. struct Sample { int at; float l, r; };
  7769. struct Golden { int set; double fs; int block; double el, er; Sample s[4]; };
  7770. std::vector<std::vector<std::uint8_t>> states {
  7771. synthA,
  7772. beepMapTestState ({ 5000, 600, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 64 }),
  7773. beepMapTestState ({ 8000, 100, 2, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 64 }),
  7774. beepMapTestState ({ 1000, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 64 }),
  7775. beepMapPresetState ("Default.fst"),
  7776. beepMapPresetState ("Voice.fst"),
  7777. };
  7778. const std::vector<std::vector<N>> sets {
  7779. { { 0, 60, 1.0, 0.0, 0.0, -1 } },
  7780. { { 0, 57, 0.8, 0.3, 25.0, -1 }, { 7000, 64, 0.6, -0.5, 0.0, -1 } },
  7781. { { 0, 48, 1.0, 0.0, 0.0, -1 }, { 300, 72, 0.5, 0.0, -40.0, -1 } },
  7782. { { 0, 67, 1.0, -0.2, 0.0, -1 } },
  7783. { { 0, 60, 1.0, 0.0, 0.0, -1 } },
  7784. { { 0, 55, 0.8, 0.0, 0.0, -1 }, { 20000, 62, 0.8, 0.4, 0.0, -1 } },
  7785. };
  7786. const Golden goldens[] {
  7787. { 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 } } },
  7788. { 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 } } },
  7789. { 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 } } },
  7790. { 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 } } },
  7791. { 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 } } },
  7792. { 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 } } },
  7793. };
  7794. for (const auto& g : goldens)
  7795. {
  7796. const auto& st = states[(std::size_t) g.set];
  7797. if (st.empty())
  7798. continue; // (no FL installation: the presets are skipped)
  7799. const auto length = (int) g.fs * 2;
  7800. const auto y = lynx::beepmap::renderReference (st, sets[(std::size_t) g.set], g.fs, length, g.block, 4321u + (std::uint32_t) g.set);
  7801. double el = 0.0, er = 0.0;
  7802. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  7803. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  7804. 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;
  7805. for (const auto& smp : g.s)
  7806. 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;
  7807. if (! ok)
  7808. 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);
  7809. CHECK (ok);
  7810. }
  7811. // the instrument: plays the image, ends after the last column, a note-off fades the voice out
  7812. lynx::ChannelDef c;
  7813. c.instrument = lynx::InstrumentKind::beepmap;
  7814. c.pluginState = states[1];
  7815. auto inst = lynx::makeInstrument (lynx::InstrumentKind::beepmap);
  7816. inst->configure (c, 120.0);
  7817. inst->prepare (44100.0);
  7818. std::vector<float> l (1024), r (1024);
  7819. inst->noteOn (60, 0.8f);
  7820. double e = 0.0;
  7821. for (int b = 0; b < 8; ++b)
  7822. {
  7823. std::fill (l.begin(), l.end(), 0.0f);
  7824. std::fill (r.begin(), r.end(), 0.0f);
  7825. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  7826. for (int i = 0; i < 1024; ++i)
  7827. e += (double) l[(std::size_t) i] * l[(std::size_t) i] + (double) r[(std::size_t) i] * r[(std::size_t) i];
  7828. }
  7829. CHECK (e > 0.01 && ! inst->idle()); // (Loop on)
  7830. inst->noteOff (60);
  7831. for (int b = 0; b < 4; ++b)
  7832. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  7833. CHECK (inst->idle());
  7834. c.pluginState = synthA; // (no loop: 9 columns of 64·(e^(200/4096·ln 33) − 1)… samples, then the voice ends)
  7835. inst->configure (c, 120.0);
  7836. inst->noteOn (60, 1.0f);
  7837. for (int b = 0; b < 200 && ! inst->idle(); ++b)
  7838. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  7839. CHECK (inst->idle());
  7840. }
  7841. // ---- Sawer (REA_rodada65 §3): installation presets and data.saw read at run time, fingerprints of engine_x64.dll ----
  7842. namespace
  7843. {
  7844. std::vector<std::uint8_t> sawerPresetState (const char* rel)
  7845. {
  7846. if (realFlpDir.empty())
  7847. return {};
  7848. std::ifstream in (std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "Sawer" / rel, std::ios::binary);
  7849. const std::vector<std::uint8_t> b ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  7850. std::size_t i = 0;
  7851. while (i + 8 <= b.size() && std::memcmp (b.data() + i, "FLdt", 4) != 0)
  7852. ++i;
  7853. i += 8;
  7854. while (i < b.size())
  7855. {
  7856. const auto id = b[i++];
  7857. std::size_t n = id < 64 ? 1 : id < 128 ? 2 : id < 192 ? 4 : 0;
  7858. if (id >= 192)
  7859. for (int shift = 0; i < b.size(); shift += 7)
  7860. {
  7861. const auto c = b[i++];
  7862. n |= (std::size_t) (c & 0x7F) << shift;
  7863. if (c < 0x80)
  7864. break;
  7865. }
  7866. if (id == 0xD5 && i + n <= b.size())
  7867. return { b.begin() + (std::ptrdiff_t) i, b.begin() + (std::ptrdiff_t) (i + n) };
  7868. i += n;
  7869. }
  7870. return {};
  7871. }
  7872. } // namespace
  7873. TEST (sawer_engine)
  7874. {
  7875. // a synthetic header: "SSLF" + size + "1rwS" + 96 floats
  7876. std::vector<std::uint8_t> synth (12 + 4 * 96, 0);
  7877. std::memcpy (synth.data(), "SSLF", 4);
  7878. const std::uint32_t size = 4 + 4 * 96;
  7879. std::memcpy (synth.data() + 4, &size, 4);
  7880. std::memcpy (synth.data() + 8, "1rwS", 4);
  7881. const float p27 = 0.75f;
  7882. std::memcpy (synth.data() + 12 + 4 * 27, &p27, 4);
  7883. CHECK (lynx::sawerStateUsable (synth));
  7884. CHECK (lynx::sawerParam (synth, 27) == 0.75f && lynx::sawerParamOffset (synth, 27) == 12 + 4 * 27);
  7885. CHECK (! lynx::sawerParam (synth, 96) && lynx::sawerParamOffset (synth, -1) < 0);
  7886. auto broken = synth;
  7887. broken[8] = 'X';
  7888. CHECK (! lynx::sawerStateUsable (broken));
  7889. CHECK (! lynx::sawerStateUsable (std::vector<std::uint8_t> (synth.begin(), synth.end() - 4)));
  7890. const auto dflt = sawerPresetState ("Default.fst");
  7891. const auto slap = sawerPresetState ("Arpeggios/FG Velocity Slapper.fst");
  7892. const auto tablePath = realFlpDir.empty() ? std::string() : (std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "Sawer" / "data.saw").string();
  7893. const auto table = tablePath.empty() ? std::vector<float> {} : lynx::sawerReadTable (tablePath);
  7894. if (dflt.empty() || slap.empty() || table.size() != 8192u)
  7895. {
  7896. std::printf (" (sawer_engine: FL Studio installation not found, DLL fingerprints skipped)\n");
  7897. return;
  7898. }
  7899. CHECK (lynx::sawerStateUsable (dflt) && lynx::sawerStateUsable (slap));
  7900. // DLL fingerprints (py/gold_sw.py: the EXEC harness, L = R = Final.Vol); "syn" = Default with our own settings
  7901. // (unison 4, sub pulse at 0.6, sync, band-pass, noise, resonance, chorus)
  7902. auto setP = [] (std::vector<std::uint8_t> s, int k, float v) { std::memcpy (s.data() + 12 + 4 * k, &v, 4); return s; };
  7903. auto syn = dflt;
  7904. 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 },
  7905. { 26, 0.3f }, { 28, 0.7f }, { 45, 0.5f }, { 46, 0.0f } })
  7906. syn = setP (syn, kv.first, kv.second);
  7907. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  7908. using N = lynx::sawer::RefNote;
  7909. struct Sample { int at; float l, r; };
  7910. struct Golden { const std::vector<std::uint8_t>* state; std::vector<N> notes; double fs; int block; double el, er; Sample s[4]; };
  7911. const Golden goldens[] {
  7912. { &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 } },
  7913. 44100, 512, 1172.343107, 1169.673445,
  7914. { { 1000, 0.0349505879f, 0.0349650607f }, { 10000, -0.0326882228f, -0.0326587185f }, { 30000, 0.139422998f, 0.139268339f }, { 60000, -1.01108653e-05f, -1.01210762e-05f } } },
  7915. { &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,
  7916. { { 1000, 0.00919434987f, 0.0769683793f }, { 10000, 0.0363935083f, 0.0227626693f }, { 30000, -0.00462511741f, -0.0105419969f }, { 60000, -0.00727002183f, -0.00159690797f } } },
  7917. { &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,
  7918. { { 1000, -0.162745371f, -0.172861978f }, { 10000, 0.0468756184f, 0.112415045f }, { 30000, -0.123887017f, -0.132816017f }, { 60000, 0.0f, 0.0f } } },
  7919. };
  7920. for (const auto& g : goldens)
  7921. {
  7922. const auto y = lynx::sawer::renderReference (*g.state, table, g.notes, g.fs, 66150, g.block);
  7923. CHECK (y.size() == 2 * 66150u);
  7924. if (y.size() != 2 * 66150u)
  7925. continue;
  7926. double el = 0.0, er = 0.0;
  7927. for (std::size_t i = 0; i < 66150; ++i)
  7928. el += (double) y[2 * i] * y[2 * i], er += (double) y[2 * i + 1] * y[2 * i + 1];
  7929. CHECK (std::abs (el - g.el) < 1e-5 * g.el && std::abs (er - g.er) < 1e-5 * g.er);
  7930. for (const auto& s : g.s)
  7931. 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);
  7932. }
  7933. // the instrument: the same engine behind LYNX's channel gains (centre → law A), velocity → Init.Vol; it goes idle
  7934. lynx::ChannelDef c;
  7935. c.instrument = lynx::InstrumentKind::sawer;
  7936. c.pluginState = dflt;
  7937. c.samplePath = tablePath;
  7938. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sawer);
  7939. inst->configure (c, 120.0);
  7940. inst->prepare (44100.0);
  7941. std::vector<float> l (512 * 40, 0.0f), r (512 * 40, 0.0f);
  7942. for (int b = 0; b < 40; ++b)
  7943. {
  7944. if (b == 0)
  7945. inst->noteOn (60, 0.78125f);
  7946. if (b == 10)
  7947. inst->noteOff (60);
  7948. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  7949. }
  7950. 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,
  7951. 8192, 512, 120.0, 0, true);
  7952. double diff = 0.0, energy = 0.0;
  7953. for (int i = 0; i < 8192; ++i)
  7954. {
  7955. 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]) });
  7956. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  7957. }
  7958. CHECK (diff == 0.0 && energy > 1e-3);
  7959. CHECK (inst->idle());
  7960. }
  7961. TEST (transistorbass_engine)
  7962. {
  7963. const auto factory = tbassPresetState ("Factory programs.fst");
  7964. const auto seqOff = tbassPresetState ("Sequencer off.fst");
  7965. if (factory.empty() || seqOff.empty())
  7966. return; // no FL installation
  7967. CHECK (factory.size() == 197887 && seqOff.size() == 196051);
  7968. CHECK (lynx::transistorBassStateUsable (factory) && lynx::transistorBassStateUsable (seqOff));
  7969. CHECK (lynx::transistorBassStateUsable (tbassWithEngine (factory, 2)));
  7970. auto broken = factory;
  7971. broken[0] = 3; // the wrapper refuses versions above 2
  7972. CHECK (! lynx::transistorBassStateUsable (broken));
  7973. auto truncated = factory;
  7974. truncated.resize (truncated.size() - 1000);
  7975. CHECK (! lynx::transistorBassStateUsable (truncated));
  7976. const auto dll = tbassDll();
  7977. if (dll.empty() || ! lynx::transistorBassDllUsable (dll))
  7978. return;
  7979. // against FL's engine DLL (EXEC, REA_rodada57 §2.11): programs of the factory bank (sequencer, accents, slides,
  7980. // distortion, delay, reverb) on the three engines and the sequencer-off preset with legato and accent notes; host
  7981. // blocks of 512 at 44.1 kHz, tempo 125. LYNX differs from the DLL by −121 dB or less here (library pow rounding).
  7982. struct Sample { int at; float l, r; };
  7983. struct Golden { const char* preset; int engine, key; double el, er; Sample s[4]; };
  7984. const Golden goldens[] {
  7985. { "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 } } },
  7986. { "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 } } },
  7987. { "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 } } },
  7988. { "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 } } },
  7989. { "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 } } },
  7990. { "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 } } },
  7991. { "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 } } },
  7992. { "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 } } },
  7993. { "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 } } },
  7994. { "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 } } },
  7995. };
  7996. constexpr int length = 88200;
  7997. for (const auto& g : goldens)
  7998. {
  7999. const bool factoryBank = std::string (g.preset) == "Factory";
  8000. auto st = factoryBank ? factory : seqOff;
  8001. if (g.engine >= 0)
  8002. st = tbassWithEngine (st, g.engine);
  8003. std::vector<lynx::tbass::RefNote> notes;
  8004. if (factoryBank)
  8005. notes.push_back ({ 0, g.key, 0.5, 0.0, 0.0, 0.0, 0.0, 70560 });
  8006. else
  8007. {
  8008. notes.push_back ({ 0, g.key, 0.5, 0.0, 0.0, 0.0, 0.0, 30000 });
  8009. notes.push_back ({ 20000, g.key + 7, 1.6, 0.0, 0.0, 0.0, 0.0, 50000 });
  8010. notes.push_back ({ 45000, g.key - 5, 0.3, 0.0, 0.0, 0.0, 0.0, 70560 });
  8011. }
  8012. const auto y = lynx::tbass::renderReference (st, dll, notes, 44100.0, length);
  8013. double el = 0.0, er = 0.0, peak = 0.0;
  8014. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  8015. 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]) });
  8016. 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;
  8017. for (const auto& smp : g.s)
  8018. 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;
  8019. if (! ok)
  8020. 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);
  8021. CHECK (ok);
  8022. }
  8023. // the instrument: FL's wrapper velocity, the channel volume as an output gain, the sequencer plays while the key is held
  8024. lynx::ChannelDef c;
  8025. c.instrument = lynx::InstrumentKind::transistorbass;
  8026. c.pluginState = factory;
  8027. c.samplePath = dll;
  8028. auto inst = lynx::makeInstrument (lynx::InstrumentKind::transistorbass);
  8029. inst->configure (c, 128.0);
  8030. inst->prepare (44100.0);
  8031. std::vector<float> l (4096), r (4096);
  8032. inst->noteOn (65, 0.8f);
  8033. double e = 0.0;
  8034. bool finite = true;
  8035. for (int b = 0; b < 24; ++b)
  8036. {
  8037. std::fill (l.begin(), l.end(), 0.0f);
  8038. std::fill (r.begin(), r.end(), 0.0f);
  8039. inst->render (l.data(), r.data(), 4096, 0.5f, 0.5f);
  8040. for (int i = 0; i < 4096; ++i)
  8041. 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];
  8042. if (b == 20)
  8043. inst->noteOff (65);
  8044. }
  8045. CHECK (finite && e > 1.0 && ! inst->idle());
  8046. for (int b = 0; b < 400 && ! inst->idle(); ++b)
  8047. inst->render (l.data(), r.data(), 4096, 0.5f, 0.5f);
  8048. CHECK (inst->idle());
  8049. }
  8050. TEST (kepler_demo_channels)
  8051. {
  8052. // every Kepler channel of FL's demo projects on LYNX's engine against FL's engine DLL (EXEC fingerprints, REA_rodada46 §6;
  8053. // the engine differs from the DLL by −127 dB or less on these, from the DLL library's pow rounding)
  8054. const auto dll = keplerDll();
  8055. if (dll.empty() || ! lynx::keplerDllUsable (dll))
  8056. return;
  8057. struct Sample { int at; float l, r; };
  8058. struct Golden { const char* file; const char* channel; std::size_t stateSize; double el, er; Sample s[4]; };
  8059. const Golden goldens[] {
  8060. { "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 } } },
  8061. { "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 } } },
  8062. { "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 } } },
  8063. { "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 } } },
  8064. };
  8065. int checked = 0;
  8066. for (const auto& g : goldens)
  8067. {
  8068. std::ifstream in (std::filesystem::path (realFlpDir) / "Demo projects" / g.file, std::ios::binary);
  8069. const std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  8070. if (bytes.empty())
  8071. continue;
  8072. const flp::Channel* ch = nullptr;
  8073. const auto project = flp::parseProject (bytes);
  8074. for (const auto& c : project.channels)
  8075. if (c.pluginName == "Kepler" && c.name == g.channel)
  8076. ch = &c;
  8077. CHECK (ch != nullptr);
  8078. if (ch == nullptr)
  8079. continue;
  8080. CHECK (ch->pluginState.size() == g.stateSize && lynx::keplerStateUsable (ch->pluginState));
  8081. 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 },
  8082. { 16000, 55, 0.95, 0.0, 0.0, 30000 } }, 44100.0, 39690);
  8083. double el = 0.0, er = 0.0, peak = 0.0;
  8084. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  8085. 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]) });
  8086. bool ok = y.size() == 2 * 39690 && std::abs (el - g.el) <= 1e-5 * g.el && std::abs (er - g.er) <= 1e-5 * g.er;
  8087. for (const auto& smp : g.s)
  8088. 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;
  8089. if (! ok)
  8090. 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);
  8091. CHECK (ok);
  8092. ++checked;
  8093. }
  8094. std::printf (" Kepler: %d demo channels checked against FL's engine DLL\n", checked);
  8095. CHECK (checked == (int) std::size (goldens));
  8096. }
  8097. TEST (ogun_demo_channels)
  8098. {
  8099. // every Ogun channel of FL's demo projects (with its plugin effects) against the round-37 reference model, which is
  8100. // 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),
  8101. // 39 690 samples at 44.1 kHz, at the state's quality and at the export quality 4. Fingerprint: energy per side and
  8102. // four samples (the engine matches the model within float rounding, −125 dB or better on the whole signal).
  8103. if (realFlpDir.empty())
  8104. return;
  8105. struct Sample { int at; float l, r; };
  8106. struct Golden { const char* file; int ordinal; std::size_t stateSize; bool export4; double el, er; Sample s[4]; };
  8107. const Golden goldens[] {
  8108. { "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
  8109. { "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
  8110. { "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
  8111. { "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
  8112. { "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
  8113. { "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
  8114. { "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
  8115. { "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
  8116. { "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
  8117. { "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
  8118. { "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
  8119. { "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
  8120. { "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
  8121. { "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
  8122. { "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
  8123. { "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
  8124. { "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
  8125. { "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
  8126. { "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
  8127. { "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
  8128. { "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
  8129. { "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
  8130. { "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
  8131. { "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
  8132. { "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
  8133. { "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
  8134. { "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
  8135. { "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
  8136. { "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
  8137. { "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
  8138. { "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
  8139. { "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
  8140. { "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
  8141. { "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
  8142. { "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
  8143. { "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
  8144. { "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
  8145. { "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
  8146. { "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
  8147. { "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
  8148. };
  8149. std::map<std::string, std::vector<std::vector<std::uint8_t>>> states;
  8150. int checked = 0;
  8151. for (const auto& g : goldens)
  8152. {
  8153. if (! states.contains (g.file))
  8154. {
  8155. std::ifstream in (std::filesystem::path (realFlpDir) / "Demo projects" / g.file, std::ios::binary);
  8156. const std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  8157. auto& list = states[g.file];
  8158. if (! bytes.empty())
  8159. for (const auto& c : flp::parseProject (bytes).channels)
  8160. if (c.pluginName == "Ogun")
  8161. list.push_back (c.pluginState);
  8162. }
  8163. const auto& list = states[g.file];
  8164. if (list.empty())
  8165. continue; // not this installation
  8166. CHECK ((int) list.size() > g.ordinal);
  8167. if ((int) list.size() <= g.ordinal)
  8168. continue;
  8169. const auto& st = list[(std::size_t) g.ordinal];
  8170. CHECK (st.size() == g.stateSize);
  8171. lynx::ogun::RefOptions opt;
  8172. opt.quality = g.export4 ? 4 : -1;
  8173. const auto y = lynx::ogun::renderReference (st, { { 0, 60, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 20000 },
  8174. { 8000, 67, 0.6, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 26000 },
  8175. { 16000, 55, 0.95, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 30000 } }, 44100.0, 39690, opt);
  8176. double el = 0.0, er = 0.0, peak = 0.0;
  8177. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  8178. 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]) });
  8179. bool ok = y.size() == 2 * 39690 && std::abs (el - g.el) <= 1e-5 * g.el && std::abs (er - g.er) <= 1e-5 * g.er;
  8180. for (const auto& smp : g.s)
  8181. 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;
  8182. if (! ok)
  8183. std::printf (" Ogun %s #%d (q%s) differs from the model: energy %.9g %.9g (model %.9g %.9g)\n", g.file, g.ordinal,
  8184. g.export4 ? "4" : "-state", el, er, g.el, g.er);
  8185. CHECK (ok);
  8186. ++checked;
  8187. }
  8188. std::printf (" Ogun: %d demo renders checked against the reference model\n", checked);
  8189. CHECK (checked == (int) std::size (goldens));
  8190. }
  8191. TEST (autogun_template_channels)
  8192. {
  8193. // REA_rodada57: every distinct Autogun state of the installation's projects (templates and the render test), with its
  8194. // effects and the Magic Dust stage (SG.dat from the installation), three notes (C5 1.0 0–20000, G5 0.6 8000–26000,
  8195. // 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
  8196. // goldens are renders of the installation's Autogun_x64.dll (LYNX is within −127 dB of them); the render-test state
  8197. // uses Seed 2 "Random", so its golden is LYNX's own render with the counter at 0 (a fingerprint of the engine).
  8198. if (realFlpDir.empty())
  8199. return;
  8200. const auto sgPath = std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "Autogun" / "Data" / "SG.dat";
  8201. std::ifstream sgIn (sgPath, std::ios::binary);
  8202. const std::vector<std::uint8_t> sgState ((std::istreambuf_iterator<char> (sgIn)), std::istreambuf_iterator<char>());
  8203. if (sgState.size() != 1342)
  8204. return; // not this installation
  8205. struct Sample { int at; float l, r; };
  8206. struct Golden { const char* file; std::size_t stateSize; bool export4; double el, er; Sample s[4]; };
  8207. const Golden goldens[] {
  8208. { "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)
  8209. { "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)
  8210. { "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)
  8211. { "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)
  8212. { "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)
  8213. { "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)
  8214. { "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)
  8215. { "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)
  8216. { "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)
  8217. { "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)
  8218. };
  8219. int checked = 0;
  8220. for (const auto& g : goldens)
  8221. {
  8222. std::ifstream in (std::filesystem::path (realFlpDir) / g.file, std::ios::binary);
  8223. const std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  8224. if (bytes.empty())
  8225. continue;
  8226. std::vector<std::uint8_t> st;
  8227. for (const auto& c : flp::parseProject (bytes).channels)
  8228. if (c.pluginName == "Autogun" && st.empty())
  8229. st = c.pluginState;
  8230. CHECK (st.size() == g.stateSize);
  8231. CHECK (lynx::autogunStateUsable (st));
  8232. lynx::ogun::RefOptions opt;
  8233. opt.autogun = true;
  8234. opt.sgState = sgState;
  8235. opt.quality = g.export4 ? 4 : -1;
  8236. const auto y = lynx::ogun::renderReference (st, { { 0, 60, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 20000 },
  8237. { 8000, 67, 0.6, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 26000 },
  8238. { 16000, 55, 0.95, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 30000 } }, 44100.0, 39690, opt);
  8239. double el = 0.0, er = 0.0, peak = 0.0;
  8240. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  8241. 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]) });
  8242. bool ok = y.size() == 2 * 39690 && std::abs (el - g.el) <= 1e-5 * g.el && std::abs (er - g.er) <= 1e-5 * g.er;
  8243. for (const auto& smp : g.s)
  8244. 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;
  8245. if (! ok)
  8246. 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);
  8247. CHECK (ok);
  8248. ++checked;
  8249. }
  8250. std::printf (" Autogun: %d template/render-test renders checked (DLL goldens for the fixed-spectrum states)\n", checked);
  8251. CHECK (checked == (int) std::size (goldens));
  8252. // the instrument (SG.dat read from ChannelDef::samplePath) plays like the reference renderer, then goes idle
  8253. {
  8254. std::ifstream in (std::filesystem::path (realFlpDir) / "Templates" / "Advanced" / "Electronic" / "Trap.flp", std::ios::binary);
  8255. const std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  8256. lynx::ChannelDef c;
  8257. c.instrument = lynx::InstrumentKind::autogun;
  8258. for (const auto& ch : flp::parseProject (bytes).channels)
  8259. if (ch.pluginName == "Autogun")
  8260. c.pluginState = ch.pluginState;
  8261. c.samplePath = sgPath.string();
  8262. lynx::ogun::RefOptions opt;
  8263. opt.autogun = true;
  8264. opt.sgState = sgState;
  8265. opt.tempo = 120.0; // the instrument is configured at 120 BPM
  8266. 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);
  8267. auto inst = lynx::makeInstrument (lynx::InstrumentKind::autogun);
  8268. inst->configure (c, 120.0);
  8269. inst->prepare (44100.0);
  8270. std::vector<float> l (16384), r (16384);
  8271. for (int s = 0; s < 16384; s += 512)
  8272. {
  8273. if (s == 0)
  8274. inst->noteOn (60, 0.75f);
  8275. if (s == 6144)
  8276. inst->noteOff (60);
  8277. inst->render (l.data() + s, r.data() + s, 512, 1.0f, 1.0f);
  8278. }
  8279. double worst = 0.0, peak = 0.0;
  8280. for (int i = 0; i < 16384; ++i)
  8281. 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]) }),
  8282. peak = std::max (peak, (double) std::abs (l[(std::size_t) i]));
  8283. if (worst != 0.0)
  8284. std::printf (" Autogun instrument vs reference: worst %.3g (peak %.3g)\n", worst, peak);
  8285. CHECK (ref.size() == 2 * 16384 && peak > 0.01 && worst == 0.0);
  8286. std::vector<float> tail (44100), tailR (tail.size());
  8287. for (int k = 0; k < 40 && ! inst->idle(); ++k)
  8288. inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
  8289. CHECK (inst->idle());
  8290. }
  8291. // the state variants: Ogun's parser rejects the version-19 Autogun layout, the comment length is one byte or 0xff + i32
  8292. std::ifstream rt (std::filesystem::path (realFlpDir) / "System" / "Render tests" / "Autogun.flp", std::ios::binary);
  8293. const std::vector<std::uint8_t> rtBytes ((std::istreambuf_iterator<char> (rt)), std::istreambuf_iterator<char>());
  8294. for (const auto& c : flp::parseProject (rtBytes).channels)
  8295. if (c.pluginName == "Autogun")
  8296. CHECK (lynx::autogunStateUsable (c.pluginState) && lynx::ogunParam (c.pluginState, 63) == 430);
  8297. }
  8298. TEST (harmless_rack)
  8299. {
  8300. // REA_rodada36: Harmless's effects rack against our reference model (rack.py / maxmodel.py, which match the
  8301. // installation's Harmless_x64.dll and SG_x64.dll), 44.1 kHz, host blocks of 512
  8302. std::vector<float> inL (8192), inR (8192);
  8303. for (int i = 0; i < 4096; ++i)
  8304. {
  8305. 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));
  8306. inR[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / 44100.0));
  8307. }
  8308. auto run = [&] (const std::array<int, 100>& p, int compType, std::shared_ptr<const lynx::hlfx::MaximusPreset> comp, int total, int voicesUntil) {
  8309. lynx::hlfx::Rack rack;
  8310. std::array<int, 7> t {};
  8311. t[3] = compType;
  8312. rack.prepare (p, t, 44100.0, 120.0, std::move (comp));
  8313. std::vector<float> l (inL.begin(), inL.begin() + total), r (inR.begin(), inR.begin() + total);
  8314. for (int s = 0; s < total; s += 512)
  8315. rack.process (l.data() + s, r.data() + s, std::min (512, total - s), s < voicesUntil);
  8316. return std::make_pair (l, r);
  8317. };
  8318. {
  8319. // chorus type 3 (mix 400), ping-pong delay type 2 (vol 600, feedback 500, 1/4 beat), reverb type 4 (vol 700)
  8320. std::array<int, 100> p {};
  8321. p[90] = 3, p[91] = 400, p[92] = 2, p[93] = 600, p[94] = 500, p[95] = 48, p[96] = 4, p[97] = 700;
  8322. const auto [l, r] = run (p, 0, nullptr, 8192, 4096);
  8323. const std::tuple<int, float, float> golden[] { { 0, 0.0f, 0.0f }, { 500, -0.25248247f, 0.15141529f }, { 3000, -0.65128994f, -0.64854866f },
  8324. { 5000, -0.54308629f, -0.36827448f }, { 6000, -0.98378551f, -0.72790027f },
  8325. { 8191, 0.50599128f, -0.43637198f } };
  8326. for (const auto& [i, gl, gr] : golden)
  8327. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  8328. }
  8329. {
  8330. // the delay alone: an impulse comes back after round(time·fs·0.3125/bpm) samples × (11^0.6 − 1)/10, then × feedback
  8331. std::array<int, 100> p {};
  8332. p[92] = 1, p[93] = 600, p[94] = 500, p[95] = 48;
  8333. lynx::hlfx::Rack rack;
  8334. rack.prepare (p, {}, 44100.0, 120.0, nullptr);
  8335. std::vector<float> l (16384), r (16384);
  8336. l[0] = 1.0f;
  8337. for (int s = 0; s < 16384; s += 512)
  8338. rack.process (l.data() + s, r.data() + s, 512, s == 0);
  8339. const auto D = 5512; // 48·44100·0.3125/120 = 5512.5 → 5512 (round half to even)
  8340. const auto vol = (float) ((std::exp ((double) (600 * 0.001f) * (double) (float) std::log (11.0)) - 1.0) * (double) 0.1f);
  8341. CHECK (std::abs (l[(std::size_t) D] - vol) < 1e-7f && std::abs (l[(std::size_t) D - 1]) < 1e-12f);
  8342. // the feedback ramps from 0.5 toward 0.5: the second echo is vol·0.5
  8343. CHECK (std::abs (l[(std::size_t) (2 * D)] - vol * 0.5f) < 1e-7f && std::abs (r[(std::size_t) D]) < 1e-12f);
  8344. }
  8345. // the compressor: Maximus states of our own (no preset data), checked against SG_x64.dll by REA round 36
  8346. using Env = std::vector<std::array<double, 4>>;
  8347. const Env def { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 } };
  8348. 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,
  8349. int sus = 2882) { return std::array<int, 13> { mode, pre, post, sep, sat, ceil, att, rel2, 2, 5000, 3, sus, 0 }; };
  8350. {
  8351. // master brickwall at −0.22 dB only (bands off / compression off)
  8352. const auto st = maximusTestState ({ 19800, 1, 48065, 1, 1115, 1000, -1 }, { band (3), band (1), band (3), band (0) },
  8353. { def, def, def, def, def, def, def, def }, 0);
  8354. const auto preset = lynx::hlfx::makeMaximusPreset (st);
  8355. CHECK (preset != nullptr);
  8356. std::array<int, 100> p {};
  8357. p[98] = 1000;
  8358. const auto [l, r] = run (p, 1, preset, 4096, 4096);
  8359. // SG_x64.dll's own output (EXEC, REA_rodada40: bit-identical to the port)
  8360. const std::tuple<int, float, float> golden[] { { 100, 0.024997942f, 0.88166064f }, { 1000, 0.56971401f, -0.62808102f },
  8361. { 2047, -0.27541628f, 0.709135f }, { 3000, 0.32431623f, -0.37985197f },
  8362. { 4095, -0.12743522f, -0.15237485f } };
  8363. for (const auto& [i, gl, gr] : golden)
  8364. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  8365. float peak = 0.0f;
  8366. for (int i = 2048; i < 4096; ++i)
  8367. peak = std::max ({ peak, std::abs (l[(std::size_t) i]), std::abs (r[(std::size_t) i]) });
  8368. CHECK (peak < 0.9751f);
  8369. }
  8370. {
  8371. // 24 dB crossovers, ×2 oversampling, low cut, stereo separation, saturation A, a knee with tension (type 7) and
  8372. // exponential tensions (type 0), release 2 on the master, band mix 600
  8373. const Env knee { { 0.0, 0.0, 0.0, 0 }, { 0.35, 0.35, 0.0, 0 }, { 0.65, 0.6, 0.36, 7 } };
  8374. const Env t0 { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.14, 0 }, { 0.5, 0.5, 0.0, 0 } };
  8375. const Env m0 { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 }, { 0.5, 0.6, 0.085, 0 } };
  8376. const auto st = maximusTestState ({ 19800, 1, 48065, 1, 891, 600, 32768 },
  8377. { band (1, 1264, 1296, 500, -500, 1200), band (0, 1340, 1000, -348, 0, 1000, 1115, 0, 1570),
  8378. band (0, 1364, 1152, 0, 0, 1000, 1115, 0, 1186), band (0, 1000, 1000, 0, -196, 1000, 1115, 4464, 1538) },
  8379. { def, def, knee, def, t0, def, m0, def }, 1);
  8380. const auto preset = lynx::hlfx::makeMaximusPreset (st);
  8381. CHECK (preset != nullptr);
  8382. std::array<int, 100> p {};
  8383. p[98] = 600;
  8384. const auto [l, r] = run (p, 2, preset, 4096, 4096);
  8385. // SG_x64.dll's own output (EXEC, REA_rodada40; the port is within −142 dB of it)
  8386. const std::tuple<int, float, float> golden[] { { 100, 0.0f, 0.0f }, { 1000, -0.46856543f, -0.63262129f },
  8387. { 2047, 0.20268451f, 0.54145539f }, { 3000, -0.25613472f, -0.49985155f },
  8388. { 4095, 0.10172445f, 0.050564945f } };
  8389. for (const auto& [i, gl, gr] : golden)
  8390. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  8391. }
  8392. {
  8393. // an .fst wrapper around a state is found; unsupported states are rejected
  8394. const auto st = maximusTestState ({ 19800, 1, 48065, 1, 1115, 1000, -1 }, { band (3), band (1), band (3), band (0) },
  8395. { def, def, def, def, def, def, def, def }, 0);
  8396. std::vector<std::uint8_t> fst { 'F', 'L', 'h', 'd', 6, 0, 0, 0, 0, 0, 1, 0, 0x60, 0, 'F', 'L', 'd', 't' };
  8397. std::vector<std::uint8_t> body { 0xC9, 8, 'M', 'a', 'x', 'i', 'm', 'u', 's', 0, 0xD5 };
  8398. for (auto n = st.size(); ; n >>= 7)
  8399. {
  8400. body.push_back ((std::uint8_t) ((n & 0x7f) | (n >= 0x80 ? 0x80 : 0)));
  8401. if (n < 0x80)
  8402. break;
  8403. }
  8404. body.insert (body.end(), st.begin(), st.end());
  8405. const auto len = (std::uint32_t) body.size();
  8406. for (int k = 0; k < 4; ++k)
  8407. fst.push_back ((std::uint8_t) (len >> (8 * k)));
  8408. fst.insert (fst.end(), body.begin(), body.end());
  8409. const auto found = lynx::hlfx::maximusStateFromFst (fst);
  8410. CHECK (found && *found == st);
  8411. auto rms = st;
  8412. rms[4 + 4 * (7 + 13 * 3 + 12)] = 1; // RMS detector on the master (decoded in REA_rodada40)
  8413. CHECK (lynx::hlfx::makeMaximusPreset (rms) != nullptr);
  8414. auto masterMid = st;
  8415. masterMid[0x1fc] = 1; // "Master Mid mode": not decoded
  8416. CHECK (lynx::hlfx::makeMaximusPreset (masterMid) == nullptr);
  8417. }
  8418. }
  8419. TEST (fruity_delay_bank_engine)
  8420. {
  8421. // REA_rodada56: Fruity Delay Bank, two states of our own against the installation's DLL (goldens = the DLL's output
  8422. // for the same input, EXEC; a new instance, FTZ/DAZ as the plugin's thread). Both are bit-identical.
  8423. lynx::ScopedFlushDenormals ftz;
  8424. struct Bank
  8425. {
  8426. std::array<int, 16> ctl;
  8427. lynx::fldbank::BankOptions opt;
  8428. };
  8429. auto build = [] (std::array<int, 4> globals, std::map<int, Bank> banks, int oversampling) {
  8430. auto s = lynx::fldbank::defaultState();
  8431. for (int i = 0; i < 4; ++i)
  8432. s.p[(std::size_t) i] = globals[(std::size_t) i];
  8433. s.p[132] = oversampling;
  8434. for (auto& [b, k] : banks)
  8435. {
  8436. for (int c = 0; c < 16; ++c)
  8437. if (16 * b + 4 + c < lynx::fldbank::automatable)
  8438. s.p[(std::size_t) (16 * b + 4 + c)] = k.ctl[(std::size_t) c];
  8439. s.bank[(std::size_t) b] = k.opt;
  8440. s.bank[(std::size_t) b].enabled = true;
  8441. }
  8442. return s;
  8443. };
  8444. auto opt = [] (int f1, int o1, int f2, int o2, bool after, int mode, int steps, int amount, bool tap, bool sync) {
  8445. lynx::fldbank::BankOptions o;
  8446. o.filterType[0] = f1;
  8447. o.filterOrder[0] = o1;
  8448. o.filterType[1] = f2;
  8449. o.filterOrder[1] = o2;
  8450. o.filterAfter = after;
  8451. o.mode = mode;
  8452. o.steps = steps;
  8453. o.amount = amount;
  8454. o.tapBeforeFeedback = tap;
  8455. o.sync = sync;
  8456. return o;
  8457. };
  8458. auto run = [] (const lynx::fldbank::State& s, double rate, int block, double bpm) {
  8459. std::vector<float> l (4096), r (4096);
  8460. for (int i = 0; i < 4096; ++i)
  8461. {
  8462. 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));
  8463. r[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / rate));
  8464. }
  8465. lynx::fldbank::Engine e (s, rate, bpm, block);
  8466. for (int i = 0; i < 4096; i += block)
  8467. e.process (l.data() + i, r.data() + i, std::min (block, 4096 - i));
  8468. return std::make_pair (l, r);
  8469. };
  8470. auto check = [] (const std::pair<std::vector<float>, std::vector<float>>& out, const std::vector<std::tuple<int, float, float>>& golden) {
  8471. for (const auto& [i, gl, gr] : golden)
  8472. CHECK (out.first[(std::size_t) i] == gl && out.second[(std::size_t) i] == gr);
  8473. };
  8474. // controls: in pan, in vol, f1 cutoff, f1 res, f1 gain, fb pan, fb level, fb cross, time, offset, f2 cutoff, f2 res,
  8475. // f2 gain, out pan, out vol, send. Three banks: ping-pong with low-pass ×2 / peaking feedback filter, pattern and send;
  8476. // free time with a high shelf after the delay, band-pass feedback, negative feedback/cross, tap before the feedback;
  8477. // swapped outputs with a 3-section high-pass in the loop and a negative pattern shape.
  8478. const auto a = build ({ 100, 128, 120, 110 },
  8479. { { 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) } },
  8480. { 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) } },
  8481. { 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) } } },
  8482. 0);
  8483. check (run (a, 44100.0, 512, 120.0), { { 100, -0.00294386433f, 0.0141299525f }, { 1000, -0.117360562f, -0.140237048f },
  8484. { 2047, 0.172598198f, -0.401046395f }, { 3000, 0.303020209f, -0.127763838f },
  8485. { 4095, 0.0093755126f, 0.241272509f } });
  8486. // 2× oversampling at 48 kHz in blocks of 300, low shelf before the delay, 2-section notch in the loop, a send into a
  8487. // ping-pong bank 3 (bank 1 and 2 off: no send) with a 2-step pattern
  8488. const auto b = build ({ 0, 128, 128, 128 },
  8489. { { 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) } },
  8490. { 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) } } },
  8491. 1);
  8492. check (run (b, 48000.0, 300, 93.0), { { 100, 0.0f, 0.0f }, { 1000, 0.0f, 0.0f }, { 2047, 0.201803684f, -0.327834547f },
  8493. { 3000, 0.412344277f, -0.101866335f }, { 4095, 0.204572573f, 0.71171385f } });
  8494. // FL's state round-trips through the import (version 15 times are converted), the globals are the slot's parameters
  8495. auto bytes = lynx::fldbank::serialise (a);
  8496. const auto slot = lynx::effectFromFl ("Fruity Delay Bank", bytes);
  8497. CHECK (slot && slot->kind == lynx::EffectKind::fruityDelayBank && slot->state == bytes);
  8498. CHECK (slot && slot->params[0] == 100.0f / 128.0f && slot->params[3] == 110.0f / 128.0f);
  8499. bytes[0] = 15;
  8500. const auto v15 = lynx::fldbank::parse (bytes);
  8501. CHECK (v15 && v15->p[12] == 2400 && v15->p[28] == 1500);
  8502. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityDelayBank, 3, 0.5f, 120.0) == "50%");
  8503. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityDelayBank, 0, 1.0f, 120.0) == "0.0 dB");
  8504. CHECK (! lynx::fldbank::parse ({ 16, 0, 0, 0 }));
  8505. // the LYNX effect (settled ramps, 512-frame engine blocks) runs from the slot
  8506. auto fx = lynx::makeEffect (*slot);
  8507. fx->prepare (44100.0);
  8508. std::vector<float> l (1024, 0.5f), r (1024, -0.25f);
  8509. fx->process (l.data(), r.data(), 1024, slot->params, 120.0);
  8510. CHECK (std::isfinite (l[1023]) && std::isfinite (r[1023]));
  8511. }
  8512. TEST (fruity_multiband_engine)
  8513. {
  8514. // REA_rodada56: Fruity Multiband Compressor, states of our own against the installation's DLL
  8515. // (goldens = the DLL's output for the same input, EXEC; blocks of 512 at 44.1 kHz; all three bit-identical)
  8516. std::vector<float> inL (4096), inR (4096);
  8517. for (int i = 0; i < 4096; ++i)
  8518. {
  8519. 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)
  8520. + 0.5 * std::sin (2.0 * 3.141592653589793 * 90.0 * i / 44100.0));
  8521. inR[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / 44100.0)
  8522. + 0.4 * std::sin (2.0 * 3.141592653589793 * 7000.0 * i / 44100.0));
  8523. }
  8524. auto multiband = [] (std::array<int, 28> c) {
  8525. std::vector<std::uint8_t> b;
  8526. auto put = [&b] (std::int32_t v) {
  8527. std::uint8_t q[4];
  8528. std::memcpy (q, &v, 4);
  8529. b.insert (b.end(), q, q + 4);
  8530. };
  8531. put (0);
  8532. for (auto v : c)
  8533. put (v);
  8534. put (0); // UI words of the state (not used by the engine)
  8535. put (50);
  8536. return b;
  8537. };
  8538. struct Out { std::vector<float> l, r; int latency = 0; };
  8539. auto run = [&] (const std::vector<std::uint8_t>& st) {
  8540. const auto s = lynx::flmb::parse (st);
  8541. CHECK (s.has_value());
  8542. lynx::EffectParams p {};
  8543. lynx::flmb::toParams (*s, p.data());
  8544. auto fx = lynx::flmb::make();
  8545. fx->prepare (44100.0);
  8546. Out o { inL, inR };
  8547. for (int s0 = 0; s0 < 4096; s0 += 512)
  8548. fx->process (o.l.data() + s0, o.r.data() + s0, 512, p, 120.0);
  8549. o.latency = fx->latencySamples();
  8550. return o;
  8551. };
  8552. auto check = [] (const Out& out, const std::vector<std::tuple<int, float, float>>& golden) {
  8553. for (const auto& [i, gl, gr] : golden)
  8554. CHECK (std::abs (out.l[(std::size_t) i] - gl) < 2e-6f && std::abs (out.r[(std::size_t) i] - gr) < 2e-6f);
  8555. };
  8556. // IIR crossovers, limiter on, high band compressing, mid bypassed, low compressing, +2 dB output
  8557. 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 }));
  8558. CHECK (oa.latency == 0);
  8559. check (oa,
  8560. { { 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 } });
  8561. // linear phase (FIR, 52 frames of latency), overlapping mid transitions, low band off, limiter off
  8562. 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 }));
  8563. CHECK (ob.latency == 52);
  8564. check (ob,
  8565. { { 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 } });
  8566. // linear phase, hard compression everywhere, limiter on, mid crossovers crossed (the FIR mid band is silent)
  8567. 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 }));
  8568. CHECK (oc.latency == 52);
  8569. check (oc,
  8570. { { 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 } });
  8571. // the slot's parameters carry FL's raw controls exactly; after the project file's 4-decimal rounding the
  8572. // effect built with FL's state still uses the exact controls
  8573. const auto st = multiband ({ 42598, 1, 1, 0, 15604, 25782, 53524, 32768, 11, 298, 51118, 0, 14994, 5037, 24736, 55755, 26870, 221,
  8574. 1090, 49807, 0, 5037, 21845, 47074, 32768, 550, 1090, 40632 });
  8575. const auto parsed = lynx::flmb::parse (st);
  8576. CHECK (parsed && parsed->c[4] == 15604);
  8577. lynx::EffectParams p {};
  8578. lynx::flmb::toParams (*parsed, p.data());
  8579. for (int i = 0; i < lynx::flmb::controls; ++i)
  8580. 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]);
  8581. lynx::EffectParams rounded = p;
  8582. for (auto& v : rounded)
  8583. v = std::round (v * 10000.0f) / 10000.0f;
  8584. std::vector<float> l1 (inL), r1 (inR), l2 (inL), r2 (inR);
  8585. auto exact = lynx::flmb::make();
  8586. auto fromState = lynx::flmb::make (st);
  8587. exact->prepare (44100.0);
  8588. fromState->prepare (44100.0);
  8589. for (int s0 = 0; s0 < 4096; s0 += 512)
  8590. {
  8591. exact->process (l1.data() + s0, r1.data() + s0, 512, p, 120.0);
  8592. fromState->process (l2.data() + s0, r2.data() + s0, 512, rounded, 120.0);
  8593. }
  8594. CHECK (l1 == l2 && r1 == r2);
  8595. CHECK (lynx::flmb::paramText (4, p[4]) == "6302 Hz");
  8596. CHECK (lynx::flmb::paramText (3, 0.5f) == "off");
  8597. // the FL import builds the exact effect with FL's state kept
  8598. const auto slot = lynx::effectFromFl ("Fruity Multiband Compressor", st);
  8599. CHECK (slot && slot->kind == lynx::EffectKind::fruityMultiband && slot->state == st && slot->params[4] == p[4]);
  8600. // unknown versions are rejected
  8601. CHECK (! lynx::flmb::parse ({ 1, 0, 0, 0 }));
  8602. }
  8603. // REA_rodada54: input of the stereo-effect goldens (the installation DLLs' output for the same input, EXEC)
  8604. static std::pair<std::vector<float>, std::vector<float>> stereoFxInput()
  8605. {
  8606. std::vector<float> l (4096), r (4096);
  8607. for (int i = 0; i < 4096; ++i)
  8608. {
  8609. 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));
  8610. r[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / 44100.0));
  8611. }
  8612. return { l, r };
  8613. }
  8614. static std::vector<std::uint8_t> i32State (std::initializer_list<int> words)
  8615. {
  8616. std::vector<std::uint8_t> d;
  8617. for (const auto w : words)
  8618. for (int b = 0; b < 4; ++b)
  8619. d.push_back ((std::uint8_t) ((std::uint32_t) w >> (8 * b)));
  8620. return d;
  8621. }
  8622. static void checkStereoFx (lynx::EffectSlot slot, const std::vector<std::tuple<int, float, float>>& golden)
  8623. {
  8624. auto [l, r] = stereoFxInput();
  8625. auto fx = lynx::makeEffect (slot);
  8626. fx->prepare (44100.0);
  8627. for (int s = 0; s < 4096; s += 512)
  8628. fx->process (l.data() + s, r.data() + s, 512, slot.params, 120.0);
  8629. for (const auto& [i, gl, gr] : golden)
  8630. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  8631. }
  8632. TEST (stereo_shaper_engine)
  8633. {
  8634. // Fruity Stereo Shaper (REA_rodada54 §2): matrix with cross terms, delay on R, all-pass on L, effects post / pre
  8635. auto slot = lynx::effectFromFl ("Fruity Stereo Shaper", i32State ({ 0, -3050, 12800, 9000, 2000, 1408, -2496, 0, 1 }));
  8636. CHECK (slot && slot->kind == lynx::EffectKind::stereoShaper);
  8637. if (slot)
  8638. checkStereoFx (*slot, { { 100, 0.0438031591f, 0.0f }, { 1000, 1.11109531f, -0.781641483f }, { 2047, -0.73191303f, 0.483852893f },
  8639. { 3000, 0.950346172f, -0.769737065f }, { 4095, 0.251555145f, 0.153306276f } });
  8640. slot = lynx::effectFromFl ("Fruity Stereo Shaper", i32State ({ 0, 0, 12800, 12800, 0, -2656, 2184, 0, 0 }));
  8641. CHECK (slot.has_value());
  8642. if (slot)
  8643. checkStereoFx (*slot, { { 100, 0.0f, -0.104282036f }, { 1000, 0.0f, 1.08441091f }, { 2047, 0.463494211f, -0.93754214f },
  8644. { 3000, -1.49942112f, 0.820726812f }, { 4095, -1.31784308f, -0.057449434f } });
  8645. // controls round-trip through the slot parameters; the Patcher's difference bus is the same engine flagged
  8646. const auto st = lynx::stereofx::parseStereoShaper (i32State ({ 0, -3050, 12800, 9000, 2000, 1408, -2496, 2, 1 }));
  8647. CHECK (st.has_value());
  8648. if (st)
  8649. {
  8650. lynx::EffectParams p {};
  8651. lynx::stereofx::stereoShaperToParams (*st, p.data());
  8652. const auto back = lynx::stereofx::stereoShaperFromParams (p.data());
  8653. CHECK (back.rightToLeft == -3050 && back.left == 12800 && back.right == 9000 && back.leftToRight == 2000 && back.delay == 1408
  8654. && back.phase == -2496 && back.post == 1);
  8655. const auto head = lynx::patcherBranchHead ("Fruity Stereo Shaper", i32State ({ 0, -3050, 12800, 9000, 2000, 1408, -2496, 2, 1 }), 2);
  8656. CHECK (head.valid && head.slot && head.slot->kind == lynx::EffectKind::stereoShaper && head.slot->params[7] == 1.0f);
  8657. }
  8658. CHECK (! lynx::stereofx::parseStereoShaper ({ 1, 2, 3 }));
  8659. }
  8660. TEST (frequency_splitter_engine)
  8661. {
  8662. // Frequency Splitter (REA_rodada62): our own states through Frequency Splitter_x64.dll from its creation (gains
  8663. // ramping up from 0, FFT response faded in), blocks of 512 at 44.1 kHz; every band sent to its own destination
  8664. // (1 low, 2 mid, 3 high). IIR modes are bit-exact; the FFT mode's residual is the plugin's single-precision FFT.
  8665. using Gold = std::vector<std::tuple<int, float, float>>;
  8666. struct Case
  8667. {
  8668. std::vector<int> words;
  8669. int mute, latency;
  8670. std::array<Gold, 4> gold; // low, mid, high sends, summed output
  8671. };
  8672. const std::vector<Case> cases {
  8673. { { 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
  8674. { { 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
  8675. { { 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
  8676. { { 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
  8677. { { 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
  8678. { { 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
  8679. { { 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
  8680. };
  8681. auto [l, r] = stereoFxInput();
  8682. for (const auto& c : cases)
  8683. {
  8684. auto words = c.words;
  8685. words.insert (words.begin(), 4);
  8686. for (const int w : { 0, 0, 0, c.mute })
  8687. words.push_back (w);
  8688. std::vector<std::uint8_t> st;
  8689. for (const auto w : words)
  8690. for (int b = 0; b < 4; ++b)
  8691. st.push_back ((std::uint8_t) ((std::uint32_t) w >> (8 * b)));
  8692. st.push_back (0);
  8693. st.resize (st.size() + 24, 0);
  8694. const auto s = lynx::fsplit::parseState (st);
  8695. CHECK (s.has_value());
  8696. if (! s)
  8697. continue;
  8698. lynx::fsplit::Engine e;
  8699. for (int i = 0; i < lynx::fsplit::engineParams; ++i)
  8700. e.setParameter (i, s->params[(std::size_t) i]);
  8701. e.prepare (44100.0);
  8702. e.reset (false);
  8703. std::array<std::vector<float>, 8> o;
  8704. for (auto& v : o)
  8705. v.assign (4096, 0.0f);
  8706. for (int b = 0; b < 4096; b += 512)
  8707. {
  8708. float* out[8];
  8709. for (int k = 0; k < 8; ++k)
  8710. out[k] = o[(std::size_t) k].data() + b;
  8711. e.process (l.data() + b, r.data() + b, out, 512);
  8712. }
  8713. CHECK (e.latencySamples() == c.latency);
  8714. const auto tol = c.latency > 0 ? 2e-5f : 2e-6f;
  8715. for (int k = 0; k < 4; ++k)
  8716. for (const auto& [i, gl, gr] : c.gold[(std::size_t) k])
  8717. CHECK (std::abs (o[(std::size_t) (2 * k)][(std::size_t) i] - gl) <= tol * std::max (1.0f, std::abs (gl))
  8718. && std::abs (o[(std::size_t) (2 * k + 1)][(std::size_t) i] - gr) <= tol * std::max (1.0f, std::abs (gr)));
  8719. // the mixer effect: the summed output (FL's main output) of an instance that has been running, and a
  8720. // Patcher bus = the bands sent to it
  8721. auto slot = lynx::effectFromFl ("Frequency Splitter", st);
  8722. CHECK (slot && slot->kind == lynx::EffectKind::frequencySplitter);
  8723. const auto head = lynx::patcherBranchHead ("Frequency Splitter", st, 3);
  8724. CHECK (head.valid && head.slot && head.slot->params[lynx::fsplit::outputHigh] == 1.0f
  8725. && head.slot->params[lynx::fsplit::outputLow] == 0.0f);
  8726. if (slot && head.slot)
  8727. {
  8728. lynx::fsplit::Engine settled;
  8729. for (int i = 0; i < lynx::fsplit::engineParams; ++i)
  8730. settled.setParameter (i, s->params[(std::size_t) i]);
  8731. settled.prepare (44100.0);
  8732. settled.reset (true);
  8733. std::vector<float> sl (4096), sr (4096), hl (4096), hr (4096);
  8734. float* out[8] { nullptr, nullptr, nullptr, nullptr, hl.data(), hr.data(), sl.data(), sr.data() };
  8735. settled.process (l.data(), r.data(), out, 4096);
  8736. const lynx::EffectSlot* both[2] { &*slot, &*head.slot };
  8737. for (const auto* p : both)
  8738. {
  8739. auto fx = lynx::makeEffect (*p);
  8740. fx->prepare (44100.0);
  8741. auto ml = l, mr = r;
  8742. for (int b = 0; b < 4096; b += 300)
  8743. fx->process (ml.data() + b, mr.data() + b, std::min (300, 4096 - b), p->params, 120.0);
  8744. const auto& wl = p == &*slot ? sl : hl;
  8745. const auto& wr = p == &*slot ? sr : hr;
  8746. CHECK (ml == wl && mr == wr);
  8747. CHECK (fx->latencySamples() == c.latency);
  8748. }
  8749. }
  8750. }
  8751. CHECK (! lynx::patcherBranchHead ("Frequency Splitter", std::vector<std::uint8_t> (0x58, 0), 2).valid); // no band sent to bus 2
  8752. CHECK (lynx::fsplit::paramText (2, 3.0f / 7.0f) == "24 dB/oct" && lynx::fsplit::paramText (11, 0.5f) == "0.0 dB");
  8753. CHECK (lynx::fsplit::paramText (0, 0.0f) == "30.0 Hz" && lynx::fsplit::paramText (4, 1.0f) == "FFT");
  8754. }
  8755. TEST (stereo_enhancer_transient_spreader_engines)
  8756. {
  8757. // REA_rodada54 §3–5: own states against the installation's DLLs (goldens = the DLL's output, EXEC)
  8758. auto se = lynx::effectFromFl ("Fruity Stereo Enhancer", i32State ({ -18, 300, -54, -148, 1, 1 }));
  8759. CHECK (se && se->kind == lynx::EffectKind::stereoEnhancer);
  8760. if (se)
  8761. checkStereoFx (*se, { { 100, 0.0f, 0.00309863733f }, { 1000, 0.0101111531f, -0.110266283f }, { 2047, -0.688752949f, -1.93998075f },
  8762. { 3000, -0.283846498f, -0.739818454f }, { 4095, 0.191147864f, 2.70425582f } });
  8763. auto tpState = i32State ({ 1, 138, 116, 64, 90, 2, 0, 70, 100, 1 });
  8764. tpState.push_back (5);
  8765. for (const auto w : i32State ({ 0, 50 }))
  8766. tpState.push_back (w);
  8767. auto tp = lynx::effectFromFl ("Transient Processor", tpState);
  8768. CHECK (tp && tp->kind == lynx::EffectKind::transientProcessor);
  8769. if (tp)
  8770. checkStereoFx (*tp, { { 100, 0.0568947345f, 0.124077775f }, { 1000, -0.326598823f, -0.193128616f }, { 2047, 0.612462878f, -0.0517415144f },
  8771. { 3000, -0.535302937f, -0.446303189f }, { 4095, -0.634670973f, 0.316044629f } });
  8772. auto spState = i32State ({ 0x10003, 7861, 3215, 106000, 1, 1, 0 });
  8773. spState.push_back (1);
  8774. spState.push_back (1);
  8775. auto sp = lynx::effectFromFl ("Spreader", spState);
  8776. CHECK (sp && sp->kind == lynx::EffectKind::spreader);
  8777. if (sp)
  8778. checkStereoFx (*sp, { { 100, 0.11841587f, -0.0958916694f }, { 1000, 1.40852344f, 0.087452054f }, { 2047, 1.07267976f, -2.02493715f },
  8779. { 3000, -0.781082511f, 0.811192274f }, { 4095, -1.80673707f, 0.89967829f } });
  8780. // unknown layouts keep the earlier fallback; FL automation maps one to one
  8781. CHECK (! lynx::stereofx::parseSpreader (i32State ({ 0x10001, 1, 2, 3, 4, 5 })));
  8782. CHECK (! lynx::stereofx::parseTransientProcessor (i32State ({ 2, 1, 2, 3, 4, 5, 6, 7, 8, 9 })));
  8783. const auto map = lynx::effectParamFromFl ("Transient Processor", 6);
  8784. CHECK (map && map->param == 6 && map->convert (0.25f) == 0.25f);
  8785. }
  8786. TEST (fruity_dynamics_engines)
  8787. {
  8788. // REA_rodada48: Fruity Limiter / Fruity Compressor, states of our own against the installation's DLLs
  8789. // (goldens = the DLL's output for the same input, EXEC; blocks of 512 at 44.1 kHz)
  8790. std::vector<float> inL (4096), inR (4096), side (4096);
  8791. for (int i = 0; i < 4096; ++i)
  8792. {
  8793. 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));
  8794. inR[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / 44100.0));
  8795. side[(std::size_t) i] = (float) (0.9 * std::sin (2.0 * 3.141592653589793 * 55.0 * i / 44100.0));
  8796. }
  8797. auto put = [] (std::vector<std::uint8_t>& b, std::int32_t v) {
  8798. std::uint8_t q[4];
  8799. std::memcpy (q, &v, 4);
  8800. b.insert (b.end(), q, q + 4);
  8801. };
  8802. auto limiterState = [&] (std::array<int, 18> p, int k = 0, int exact = 0) {
  8803. std::vector<std::uint8_t> b;
  8804. put (b, 7);
  8805. for (auto v : p)
  8806. put (b, v);
  8807. b.resize (0xA4, 0);
  8808. put (b, k);
  8809. b.push_back ((std::uint8_t) exact);
  8810. return b;
  8811. };
  8812. auto compressorState = [&] (int version, std::array<int, 6> p, int flags, int k = 0) {
  8813. std::vector<std::uint8_t> b;
  8814. put (b, version);
  8815. for (auto v : p)
  8816. put (b, v);
  8817. put (b, flags);
  8818. if (version > 2)
  8819. put (b, k);
  8820. return b;
  8821. };
  8822. struct Out { std::vector<float> l, r; int latency = 0; };
  8823. auto run = [&] (const lynx::EffectSlot& slot, bool sidechain) {
  8824. auto fx = lynx::makeEffect (slot);
  8825. fx->prepare (44100.0);
  8826. Out o { inL, inR };
  8827. for (int s = 0; s < 4096; s += 512)
  8828. {
  8829. if (sidechain)
  8830. fx->setSidechain (side.data() + s, side.data() + s);
  8831. fx->process (o.l.data() + s, o.r.data() + s, 512, slot.params, 120.0);
  8832. }
  8833. o.latency = fx->latencySamples();
  8834. return o;
  8835. };
  8836. auto check = [] (const Out& out, const std::vector<std::tuple<int, float, float>>& golden) {
  8837. for (const auto& [i, gl, gr] : golden)
  8838. CHECK (std::abs (out.l[(std::size_t) i] - gl) < 2e-6f && std::abs (out.r[(std::size_t) i] - gr) < 2e-6f);
  8839. };
  8840. auto limiter = [] (const std::vector<std::uint8_t>& st) {
  8841. auto slot = lynx::effectFromFl ("Fruity Limiter", st);
  8842. CHECK (slot && slot->kind == lynx::EffectKind::fruityLimiter);
  8843. return *slot;
  8844. };
  8845. // look-ahead limiter: +4 dB gain into a −2.5 dB ceiling, 2 ms attack (88 samples of latency); bit-identical
  8846. 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);
  8847. CHECK (a.latency == 88);
  8848. check (a, { { 100, 0.0176312141f, 0.621841073f }, { 1000, 0.401142687f, -0.442239583f }, { 2047, -0.19392401f, 0.499310732f },
  8849. { 3000, 0.22835508f, -0.267458469f }, { 4095, -0.089728713f, -0.107289009f } });
  8850. // compressor section with a positive knee, 2 compressor curve stages and soft saturation (port within −143 dB)
  8851. check (run (limiter (limiterState ({ 1000, 900, 1000, 0, 3, 5000, 3, 2882, 400, 600, 400, 1115, 6666, 2, 650, 1000, 0, 4187 })), false),
  8852. { { 100, -0.00208989787f, 0.0100310864f }, { 1000, -0.0545219742f, -0.0651496574f }, { 2047, 0.177322432f, -0.230846375f },
  8853. { 3000, -0.159136176f, -0.183688983f }, { 4095, -0.300143838f, 0.299418598f } });
  8854. // sidechained compressor (side input 1) with a negative knee, noise gate and the exact-ceiling option
  8855. 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),
  8856. { { 100, -0.00186592329f, 0.00895605329f }, { 1000, -0.0375732854f, -0.0448972471f }, { 2047, 0.124697573f, -0.162336946f },
  8857. { 3000, -0.111069582f, -0.128206298f }, { 4095, -0.218446419f, 0.21791859f } });
  8858. auto compressor = [] (const std::vector<std::uint8_t>& st) {
  8859. auto slot = lynx::effectFromFl ("Fruity Compressor", st);
  8860. CHECK (slot && slot->kind == lynx::EffectKind::fruityCompressor);
  8861. return *slot;
  8862. };
  8863. // Vintage type, look-ahead flag, side input 1: bit-identical
  8864. const auto ca = run (compressor (compressorState (3, { -200, 40, 30, 50, 300, 2 }, 1, 1)), true);
  8865. CHECK (ca.latency == 64);
  8866. check (ca, { { 100, 0.0795554966f, -0.230549783f }, { 1000, 0.483903229f, 0.0241768137f }, { 2047, -0.596430957f, -0.441769004f },
  8867. { 3000, 0.597724319f, -0.18129006f }, { 4095, 0.607042372f, 0.435067475f } });
  8868. // Soft/R, version 2 without the flag (the output is still 64 frames late, but not reported): bit-identical
  8869. const auto cb = run (compressor (compressorState (2, { -150, 80, 0, 150, 100, 7 }, 0)), false);
  8870. CHECK (cb.latency == 0);
  8871. check (cb, { { 100, 0.0813739002f, -0.235819489f }, { 1000, 0.264188439f, 0.0131994039f }, { 2047, -0.260424435f, -0.192893147f },
  8872. { 3000, 0.251500547f, -0.0762802213f }, { 4095, 0.252972007f, 0.181305125f } });
  8873. // the slot's parameters carry FL's raw controls exactly (and survive the 4-decimal project file)
  8874. const auto st = limiterState ({ 1261, 995, 1303, 1115, 3, 5000, 3, 2882, 264, 244, -590, 1115, 6666, 1, 650, 1000, 0, 4187 }, 1);
  8875. const auto parsed = lynx::fldyn::parseLimiter (st);
  8876. CHECK (parsed && parsed->sidechain == 1 && ! parsed->exactCeiling);
  8877. const auto slot = limiter (st);
  8878. for (int i = 0; i < lynx::fldyn::limiterControls; ++i)
  8879. {
  8880. const auto rounded = std::round (slot.params[(std::size_t) i] * 10000.0f) / 10000.0f;
  8881. 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]);
  8882. }
  8883. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityLimiter, 3, slot.params[3], 120.0) == "2.0 ms");
  8884. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityCompressor, 5, 2.0f / 7.0f, 120.0) == "Vintage");
  8885. // unreadable states keep the old approximation
  8886. CHECK (lynx::effectFromFl ("Fruity Limiter", { 7, 0, 0, 0 }).has_value() == false
  8887. || lynx::effectFromFl ("Fruity Limiter", { 7, 0, 0, 0 })->kind != lynx::EffectKind::fruityLimiter);
  8888. CHECK (! lynx::fldyn::parseCompressor ({ 4, 0, 0, 0 }));
  8889. }
  8890. TEST (maximus_engine)
  8891. {
  8892. // REA_rodada40: the engine on its own (MaximusProcessor), states of our own against the installation's DLLs
  8893. // (goldens = the DLL's output for the same input, EXEC), and the containers of the mixer effect and Vocodex
  8894. std::vector<float> inL (4096), inR (4096);
  8895. for (int i = 0; i < 4096; ++i)
  8896. {
  8897. 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));
  8898. inR[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / 44100.0));
  8899. }
  8900. auto run = [&] (std::shared_ptr<const lynx::hlfx::MaximusPreset> preset, int mix) {
  8901. lynx::hlfx::MaximusProcessor m;
  8902. m.prepare (std::move (preset), 44100.0, mix);
  8903. std::vector<float> l (inL), r (inR);
  8904. for (int s = 0; s < 4096; s += 512)
  8905. m.process (l.data() + s, r.data() + s, 512);
  8906. return std::make_pair (l, r);
  8907. };
  8908. using Env = std::vector<std::array<double, 4>>;
  8909. const Env def { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 } };
  8910. const Env knee { { 0.0, 0.0, 0.0, 0 }, { 0.35, 0.35, 0.0, 0 }, { 0.65, 0.6, 0.36, 7 } };
  8911. const Env t0 { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.14, 0 }, { 0.5, 0.5, 0.0, 0 } };
  8912. const Env m0 { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 }, { 0.5, 0.6, 0.085, 0 } };
  8913. 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,
  8914. int sus = 2882, int rms = 0) { return std::array<int, 13> { mode, pre, post, sep, sat, ceil, att, rel2, 2, 5000, 3, sus, rms }; };
  8915. auto check = [] (const std::pair<std::vector<float>, std::vector<float>>& out, const std::vector<std::tuple<int, float, float>>& golden) {
  8916. for (const auto& [i, gl, gr] : golden)
  8917. CHECK (std::abs (out.first[(std::size_t) i] - gl) < 2e-6f && std::abs (out.second[(std::size_t) i] - gr) < 2e-6f);
  8918. };
  8919. // RMS detectors, saturation mode B, a 5 Hz low cut, 12 dB high split (SG_x64.dll: port within −152 dB)
  8920. auto st3 = maximusTestState ({ 19800, 1, 48065, 0, 1115, 1000, 0 },
  8921. { band (0, 1500, 1000, 0, 400, 900, 1115, 0, 2882, 1), band (0, 1340, 1000, 0, 0, 1000, 1115, 0, 1570, 1),
  8922. band (1, 1000, 1200, 0, 1000, 1000), band (0, 1000, 1000, 0, 250, 1000, 1115, 4464, 2882, 1) },
  8923. { def, def, knee, def, t0, def, m0, def }, 0);
  8924. const auto p3 = lynx::hlfx::makeMaximusPreset (st3);
  8925. CHECK (p3 != nullptr);
  8926. check (run (p3, 1000), { { 100, 0.0f, 0.0f }, { 1000, 0.87061065f, 0.64004499f }, { 2047, -0.97266865f, 0.60940075f },
  8927. { 3000, 0.96658099f, 0.9250561f }, { 4095, 0.99094611f, -0.964059f } });
  8928. // the same with solo on (UI byte 0x1EF) and the low band selected (0x1E0): bit-identical to SG_x64.dll
  8929. auto solo = st3;
  8930. solo[0x1ef] = 1;
  8931. std::memset (solo.data() + 0x1e0, 0, 4);
  8932. check (run (lynx::hlfx::makeMaximusPreset (solo), 1000),
  8933. { { 100, 3.2922967e-06f, 0.00036916698f }, { 1000, 0.15653539f, -0.047913823f }, { 2047, -0.17336531f, 0.019099623f },
  8934. { 3000, 0.17185682f, -0.017173048f }, { 4095, 0.16714551f, 0.019473195f } });
  8935. // the legacy linear-phase FIR band split (UI byte 0x1FD; SG_x64.dll: port within −148 dB)
  8936. auto fir = st3;
  8937. fir[0x1fd] = 1;
  8938. check (run (lynx::hlfx::makeMaximusPreset (fir), 1000),
  8939. { { 100, 0.0f, 0.0f }, { 1000, 0.97986746f, 0.18555209f }, { 2047, -0.98930448f, -0.95970845f },
  8940. { 3000, 0.98856646f, -0.44592777f }, { 4095, 0.99069679f, 0.94382554f } });
  8941. // Soundgoodizer_x64.dll's own build with a v4 file (its presets' layout): a copy of the round-36 state without
  8942. // oversampling, through a byte-identical copy of the installation's DLL with this file as its preset (EXEC)
  8943. const auto st2 = maximusTestState ({ 19800, 1, 48065, 1, 891, 600, 32768 },
  8944. { band (1, 1264, 1296, 500, -500, 1200), band (0, 1340, 1000, -348, 0, 1000, 1115, 0, 1570),
  8945. band (0, 1364, 1152, 0, 0, 1000, 1115, 0, 1186), band (0, 1000, 1000, 0, -196, 1000, 1115, 4464, 1538) },
  8946. { def, def, knee, def, t0, def, m0, def }, 0);
  8947. std::vector<std::uint8_t> v4 { 4, 0, 0, 0 };
  8948. v4.insert (v4.end(), st2.begin() + 4, st2.begin() + 0x1f5);
  8949. v4.insert (v4.end(), st2.begin() + 0x1fe, st2.end());
  8950. const auto p4 = lynx::hlfx::makeMaximusPreset (v4, lynx::hlfx::MaximusBuild::soundgoodizer);
  8951. CHECK (p4 != nullptr);
  8952. check (run (p4, 600), { { 100, 0.0f, 0.0f }, { 1000, -0.60926896f, -0.6652832f }, { 2047, 0.40872875f, 0.36375371f },
  8953. { 3000, -0.41941205f, -0.61312866f }, { 4095, -0.12878418f, 0.27944946f } });
  8954. CHECK (lynx::hlfx::makeMaximusPreset (st2, lynx::hlfx::MaximusBuild::soundgoodizer) != nullptr); // v7: also read by that build
  8955. // the mixer effect's container and the effect itself
  8956. lynx::maximusfx::Blob blob;
  8957. blob.build = lynx::hlfx::MaximusBuild::soundgoodizer;
  8958. blob.mix = 600;
  8959. blob.state = v4;
  8960. const auto packed = lynx::maximusfx::pack (blob);
  8961. const auto back = lynx::maximusfx::unpack (packed);
  8962. CHECK (back && back->build == blob.build && back->mix == 600 && back->state == v4);
  8963. auto slot = lynx::makeEffectSlot (lynx::EffectKind::maximus);
  8964. slot.state = packed;
  8965. auto fx = lynx::makeEffect (slot);
  8966. fx->prepare (44100.0);
  8967. {
  8968. std::vector<float> l (inL), r (inR);
  8969. for (int s = 0; s < 4096; s += 1024) // longer host blocks are split into the engine's 512-frame calls
  8970. fx->process (l.data() + s, r.data() + s, 1024, slot.params, 120.0);
  8971. const auto ref = run (p4, 600);
  8972. CHECK (l == ref.first && r == ref.second);
  8973. }
  8974. const std::vector<std::uint8_t> sgState { 3, 0, 0, 0, 2, 0, 0, 0, 0x78, 2, 0, 0 };
  8975. const auto sg = lynx::maximusfx::parseSoundgoodizer (sgState);
  8976. CHECK (sg && sg->preset == 2 && sg->amount == 632 && std::string (lynx::maximusfx::soundgoodizerPresetFile (2)) == "c.dat");
  8977. CHECK (lynx::soundgoodizerPresetPath (sgState) == "Plugins\\Fruity\\Effects\\Soundgoodizer\\Data\\Presets\\c.dat");
  8978. CHECK (lynx::soundgoodizerExact (sgState, v4).has_value() && ! lynx::soundgoodizerExact (sgState, {}).has_value());
  8979. // Vocodex's appended installation data survives a round trip and leaves FL's state untouched
  8980. const std::vector<std::uint8_t> flState { 8, 0, 0, 0, 1, 2, 3 };
  8981. lynx::vocodex::InstallData data;
  8982. data.sgDat = st2;
  8983. data.carrierState = { 9, 9, 9 };
  8984. const auto joined = lynx::vocodex::appendInstallData (flState, data);
  8985. const auto [raw, files] = lynx::vocodex::splitInstallData (joined);
  8986. CHECK (raw == flState && files.sgDat == st2 && files.carrierState == data.carrierState);
  8987. CHECK (lynx::vocodex::splitInstallData (flState).first == flState);
  8988. }
  8989. // ---- Fruity Reeverb 2 (REA_rodada49) --------------------------------------------------------------------------------
  8990. // Goldens: the installation's Fruity Reeverb 2_x64.dll (unmodified, rate set before the state, flushed, blocks of 512)
  8991. // on our own states and input; LYNX's engine is bit-identical to it.
  8992. TEST (reeverb2_engine)
  8993. {
  8994. auto hex = [] (const char* h) {
  8995. std::vector<std::uint8_t> v;
  8996. for (std::size_t i = 0; h[i] != 0 && h[i + 1] != 0; i += 2)
  8997. v.push_back ((std::uint8_t) std::stoi (std::string (h + i, 2), nullptr, 16));
  8998. return v;
  8999. };
  9000. constexpr int N = 8192;
  9001. std::vector<float> inL (N), inR (N);
  9002. std::uint32_t seed = 12345;
  9003. for (int i = 0; i < N; ++i)
  9004. {
  9005. seed = (seed * 1103515245u + 12345u) & 0x7fffffffu;
  9006. const auto nz = (float) ((double) seed / 2147483648.0 - 0.5);
  9007. const auto tri = (float) ((i % 200) - 100) / 100.0f;
  9008. const float g = i < 4000 ? 1.0f : 0.0f;
  9009. inL[(std::size_t) i] = 0.5f * tri * g + (i >= 100 && i < 300 ? nz * 0.6f : 0.0f);
  9010. inR[(std::size_t) i] = 0.25f * tri * g;
  9011. }
  9012. struct Golden
  9013. {
  9014. int i;
  9015. float l, r;
  9016. };
  9017. auto run = [&] (const std::vector<std::uint8_t>& state, double bpm, const std::vector<Golden>& gold) {
  9018. const auto slot = lynx::reeverb2FromFl (state);
  9019. CHECK (slot.has_value() && slot->kind == lynx::EffectKind::reeverb2);
  9020. if (! slot)
  9021. return;
  9022. auto fx = lynx::makeEffect (*slot);
  9023. fx->prepare (44100.0);
  9024. fx->reset();
  9025. std::vector<float> l (inL), r (inR);
  9026. for (int s = 0; s < N; s += 512)
  9027. fx->process (l.data() + s, r.data() + s, 512, slot->params, bpm);
  9028. for (const auto& g : gold)
  9029. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  9030. };
  9031. // modulation, early reflections, negative separation, filters, predelay
  9032. run (hex ("112700004b000000780000001e0000004600000055000000190000003c0000009600000090010000e2ffffff6e000000640000008c00000084030000b00400000000"),
  9033. 120.0, { { 777, 0.263778746f, 0.131889373f }, { 1555, 0.187972173f, 0.0587911084f }, { 3333, 0.10398978f, 0.0566705838f },
  9034. { 5000, 0.087177977f, -0.0197417438f }, { 8191, 0.0769432634f, -0.282023013f } });
  9035. // Mid/Side input, tempo-synced predelay (27 ticks at 128 BPM), positive separation
  9036. run (hex ("112700004b000000280000001b00000032000000640000000f0000002800000064000000f401000028000000800000000000000040000000ac020000000000000101"),
  9037. 128.0, { { 777, 0.38499999f, 0.192499995f }, { 1555, 0.275000006f, 0.137500003f }, { 3333, 0.164420277f, 0.0826604739f },
  9038. { 5000, -0.00861243438f, -0.00140450173f }, { 8191, -0.00873835851f, 0.00888271444f } });
  9039. // version 10000 (no modulation controls), everything off or at its extreme, dry 0
  9040. run (hex ("1027000013000000dd000000000000006400000000000000c8000000dd00000014000000d00700004000000000000000a0000000a00000000000"),
  9041. 120.0, { { 777, 0.0f, 0.0f }, { 1555, 0.0f, 0.0f }, { 3333, 0.0632086769f, 0.0632086769f }, { 5000, -0.108072646f, -0.108072646f },
  9042. { 8191, 0.144686908f, 0.144686908f } });
  9043. // the parameters map back to FL's integers; unsupported states fall back to the approximation
  9044. lynx::reeverb2::Settings s;
  9045. s.p = { 3000, 5, 384, 1, 0, 200, 221, 20, 2000, -64, 160, 0, 160, 2048, 1 };
  9046. s.tempoPredelay = true;
  9047. lynx::EffectParams p {};
  9048. lynx::reeverb2::toParams (s, p);
  9049. CHECK (lynx::reeverb2::fromParams (p) == s);
  9050. CHECK (! lynx::reeverb2FromFl ({ 0x11, 0x27, 0, 0, 1, 0 }).has_value());
  9051. CHECK (! lynx::reeverb2FromFl ({ 0x12, 0x27, 0, 0 }).has_value());
  9052. const auto approx = lynx::effectFromFl ("Fruity Reeverb 2", { 0x11, 0x27, 0, 0 }, 120.0);
  9053. CHECK (! approx || approx->kind != lynx::EffectKind::reeverb2);
  9054. CHECK (lynx::reeverb2ParamText (0, 0.0f) == "off" && lynx::reeverb2ParamText (5, 1.0f) == "20.0 s");
  9055. // Fruity Reeverb (the first version): Fruity Reeverb_x64.dll goldens on our own states (denormals flushed, as the
  9056. // plugin runs and as LYNX renders)
  9057. lynx::ScopedFlushDenormals ftz;
  9058. auto run1 = [&] (const std::vector<std::uint8_t>& state, const std::vector<Golden>& gold) {
  9059. const auto slot = lynx::reeverb1FromFl (state);
  9060. CHECK (slot.has_value() && slot->kind == lynx::EffectKind::reeverb1);
  9061. if (! slot)
  9062. return;
  9063. auto fx = lynx::makeEffect (*slot);
  9064. fx->prepare (44100.0);
  9065. std::vector<float> l (inL), r (inR);
  9066. for (int s = 0; s < N; s += 512)
  9067. fx->process (l.data() + s, r.data() + s, 512, slot->params, 120.0);
  9068. for (const auto& g : gold)
  9069. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  9070. };
  9071. run1 (hex ("01000000204e0000204e000025000000409c000050c300000a00000030750000a861000050c3000060ea0000"),
  9072. { { 777, 0.293731689f, 0.146865845f }, { 1555, 0.20980835f, 0.104904175f }, { 3333, 0.0998796821f, 0.0754205957f },
  9073. { 5000, -0.0994476378f, 0.0116700521f }, { 8191, -0.141061321f, 0.0601170398f } });
  9074. run1 (hex ("0100000000000000000001000000000000000100000001002800000000000100000001000000000000000100"),
  9075. { { 777, -0.016678432f, -4.58686594e-31f }, { 1555, 0.0221031085f, 0.0230635051f }, { 3333, 0.0875115693f, 0.1695389f },
  9076. { 5000, -0.111421227f, -0.295867324f }, { 8191, 0.503498077f, 0.190456569f } });
  9077. CHECK (lynx::reeverb1FromFl (std::vector<std::uint8_t> (44, 0)).has_value());
  9078. CHECK (! lynx::reeverb1FromFl (std::vector<std::uint8_t> (40, 0)).has_value());
  9079. CHECK (lynx::reeverb1ParamText (5, 0.0f) == "Brighter" && lynx::reeverb1ParamText (0, 0.0f) == "off");
  9080. }
  9081. // ---- Fruity Love Philter (REA_rodada66) ---------------------------------------------------------------------------------
  9082. TEST (love_philter_engine)
  9083. {
  9084. // goldens: Fruity Love Philter_x64.dll on our own synthetic states (44.1 kHz, blocks of 512, 120 BPM, live mode),
  9085. // bit-identical on the test machine (denormals flushed, as the plugin runs and as LYNX renders)
  9086. lynx::ScopedFlushDenormals ftz;
  9087. auto hex = [] (const char* h) {
  9088. std::vector<std::uint8_t> v;
  9089. for (std::size_t i = 0; h[i] != 0 && h[i + 1] != 0; i += 2)
  9090. v.push_back ((std::uint8_t) std::stoi (std::string (h + i, 2), nullptr, 16));
  9091. return v;
  9092. };
  9093. constexpr int N = 8192;
  9094. std::vector<float> inL (N), inR (N);
  9095. std::uint32_t seed = 12345;
  9096. for (int i = 0; i < N; ++i)
  9097. {
  9098. seed = (seed * 1103515245u + 12345u) & 0x7fffffffu;
  9099. const auto nz = (float) ((double) seed / 2147483648.0 - 0.5);
  9100. const auto tri = (float) ((i % 200) - 100) / 100.0f;
  9101. const float g = i < 4000 ? 1.0f : 0.0f;
  9102. inL[(std::size_t) i] = 0.5f * tri * g + (i >= 100 && i < 300 ? nz * 0.6f : 0.0f);
  9103. inR[(std::size_t) i] = 0.25f * tri * g;
  9104. }
  9105. struct Golden
  9106. {
  9107. int i;
  9108. float l, r;
  9109. };
  9110. auto run = [&] (const std::vector<std::uint8_t>& state, const std::vector<Golden>& gold) {
  9111. const auto slot = lynx::effectFromFl ("Fruity Love Philter", state, 120.0);
  9112. CHECK (slot.has_value() && slot->kind == lynx::EffectKind::lovePhilter);
  9113. if (! slot || slot->kind != lynx::EffectKind::lovePhilter)
  9114. return;
  9115. auto fx = lynx::makeEffect (*slot);
  9116. fx->prepare (44100.0);
  9117. std::vector<float> l (inL), r (inR);
  9118. for (int s = 0; s < N; s += 512)
  9119. {
  9120. fx->setSongPosition (s * 2.0 * 960.0 / 44100.0);
  9121. fx->process (l.data() + s, r.data() + s, 512, slot->params, 120.0);
  9122. }
  9123. for (const auto& g : gold)
  9124. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  9125. };
  9126. // SVF triple with Low/Band/High, a song-locked volume pattern with tension, a cutoff LFO; Lime HP fed by NEXT, panned
  9127. run (hex (
  9128. "03000c00789cedda316ed44018c5f1cfd90b5050234a0e81702a1a6e4045c11d68e7005c00c40928e96810254590380225e552538471ec"
  9129. "d97c314c841e0259cfff91463bf66e7e3b9ac42fdf582e1151961e776abf1f314ce3b3a5d7f6e4fb8bf1b2b6693c5cf56118e6b79ed5fe"
  9130. "b2f6b7b53fadfd41ed8f1379378d9fd77e88a50d57c3c3f597dcd28ee3fcfaadbe9e8fcb24e272d56238fd4019a73e2c5f3074c1ebf732"
  9131. "745a8ee51507670bce99e89cbff9e5076eb9f8be8e378f9b7df3fcfae2dbba37afcb9c0b391b0eb16ec795ff657cfd6a6aefc7cf17178f"
  9132. "f2f9f6f9fafe8769342c7dfabe58e65e62db7f5338fb74fe4f96acafa5e3f8bbf3eb6b75eb1e75068eb3d3b241b95ef2ffc0d67ebc8bb8"
  9133. "b792b65e2ffc9bfa636a64068e9fc3de44f77a7b93adfc6e7170fec661afa17b64038eb34336e81ed980e3ec900dba4736e0383b6483ee"
  9134. "957aee61ed9fe27a4d63194ffd63fdccf44cc7f972fef42408cf6ce0e06c30a7b6711f93fba238387fee50c3e81e75038eb343dda07b64"
  9135. "038eb34336e81e7b0a1c67873d85ee910d38ce0ed9a07b64038eb34336e81ed980e3ec900dba57625ec3d65bcbc7799cd6b83f95ded478"
  9136. "d60267c70ef745758f1a06c7d9a186d13dea061c6787ba41f7c8061c67876cd03df61438ce0e7b0add231b709c1db241f7c8061c67876c"
  9137. "d03db201c7d9211b74afc4bc86adb7968ff338ad717f2abda9f1ac05ce8e1dee8bea1e350c8eb3430da37bd40d38ce0e7583ee910d38ce"
  9138. "0ed9a07bec29709c1df614ba4736e0383b6483ee910d38ce0ed9a07b64038eb34336e85e89790d5b6f2d1fe7715ae3fe547a53e3590b9c"
  9139. "1d3bdc17d53d6a181c67871a46f7a81b709c1dea06dd231b709c1db241f7d853e0383bec29748f6cc07176c806dd231b709c1db241f7c8"
  9140. "061c67876cd0bd12f31ab6de5a3ecee3b4c6fda9f4a6c6b316383b76b82faa7bd43038ce0e358cee5137e0383bd40dba4736e0383b6483"
  9141. "eeb1a7c07176d853e81ed980e3ec900dba4736e0383b6483ee910d38ce0ed9a07b25e6356cbdb57c9cc7698dfb53e94d8d672d7076ec70"
  9142. "5f54f7a861709c1d6a18dda36ec07176a81b748f6cc07176c806dd634f81e3ecb0a7d03db201c7d9211b748f6cc07176c806dd231b709c"
  9143. "1db241f74acc6bd87a6bf9388fd31af7a7d29b1acf5ae0ecd8e1bea8ee51c3e0383bd430ba47dd80e3ec5037e81ed980e3ec900dbac79e"
  9144. "02c7d9614fa17b64038eb34336e81ed980e3ec900dba4736e0383b6483eec54ff7970e90"),
  9145. { { 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 } });
  9146. // Mango alternate triple with drive, waveshaper, Mod X/Y maps (smoothed), follower map on the volume; ×4 oversampling
  9147. run (hex (
  9148. "03000c00789cedda4d8ed33018c671a788151b8ec0013803993dd7e00e6c7d0a24100bae81c40590e00e5d82c46238c1e0c879db7752b9"
  9149. "8b470c1f4ffe963c759af6371e4b79e675945a4aa96b2f4f5b7f56cad45e3e1d4a79be0c5a7bf9f3f57cd7da326e6fb7f3d3b49e5abef6"
  9150. "a1f537adbf2ae7968f4f7c6b8f5b7fb20ca6e951fb19fd7a3bceefde2eedfbf2fa22bfdf5f6fe7fce9bb4d2bd3e9549d973eadbf742adb"
  9151. "7686e25c86f2df51d7cfe0e0fc6de7203a37ef2fbe70ba200f17678ef3fde3b08f572fbe7fddebebd273816cc07173fe4c36dc0eaebdeb"
  9152. "ff981fcabb2c28cedfeb85c4c70befeb97a5fd580b8dcfcbfba773dbdf433d81e3ec3c7466a46bec9e1d457e7e777bedfd1ff5c4d2c806"
  9153. "1c3f87bd86eeb1d7c07176f6b8d7f85d1ed980e3ec900dba4736e0383b6483ee910d38ce0ed9a07bb5f4358c1e2d1fe7715ae3f1544653"
  9154. "fbd65e6fe650b65349363985e3e7705f54f7a861709c1d6a18dda36ec07176a81b748f6cc07176c806dd634f81e3ecb0a7d03db201c7d9"
  9155. "211b748f6cc07176c806dd231b709c1db241f76ae96b183d5a3ecee3b4c6e3a98ca6c6b316383b76b82faa7bd43038ce0e358cee5137e0"
  9156. "383bd40dba4736e0383b6483eeb1a7c07176d853e81ed980e3ec900dba4736e0383b6483ee910d38ce0ed9a07bb5f4358c1e2d1fe7715a"
  9157. "e3f1544653e3590b9c1d3bdc17d53d6a181c67871a46f7a81b709c1dea06dd231b709c1db241f7d853e0383bec29748f6cc07176c806dd"
  9158. "231b709c1db241f7c8061c67876cd0bd5afa1a468f968ff338adf1782aa3a9f1ac05ce8e1dee8bea1e350c8eb3430da37bd40d38ce0e75"
  9159. "83ee910d38ce0ed9a07bec29709c1df614ba4736e0383b6483ee910d38ce0ed9a07b64038eb34336e85e2d7d0da347cbc7799cd6783c95"
  9160. "d1d478d60267c70ef745758f1a06c7d9a186d13dea061c6787ba41f7c8061c67876cd03df61438ce0e7b0add231b709c1db241f7c8061c"
  9161. "67876cd03db201c7d9211b74af96be86d1a3e5e33c4e6b3c9eca686a3c6b81b36387fba2ba470d83e3ec50c3e81e75038eb343dda07b64"
  9162. "038eb34336e81e7b0a1c67873d85ee910d38ce0ed9a07b64038eb34336e81ed980e3ec900dba574b5fc3e8d1f2711ea7351e4f6534359e"
  9163. "b5c0d9b1c37d51dda386c17176a861748fba01c7d9a16ed03db201c7d9211b748f3d058eb3c39e42f7c8061c67876cd03db201c7d9211b"
  9164. "748f6cc07176c806dd2bbf0040771f82"),
  9165. { { 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 } });
  9166. // band-pass alternate dual with FLAT, low shelf, all-pass with a pan LFO and inverted input, CENTER, no oversampling
  9167. run (hex (
  9168. "03000c00789ceddd418ad44018c5f12f3303baf404e2d243083d7b6fe0dab55b5dd64106bc8427f024aea497ed09da8a95225ff75883bc"
  9169. "01092fff82a22b19fb97a2248faf62184b4494a5c7abdadf444cf3f866e9b5bdfff5f970ae2d9636cdad0de7af7da9fd6327623d5ffe32"
  9170. "be5b7afdfe6dfdb85d2ff2443b1ddae7b17edecfe33f173f5fb598d6ab1fe63e2d179886e0fab30c5dcf1d07670bce8de8dc3f3cfac213"
  9171. "37df8fc3e571b72fcf5fdf7c5bf7dabab45c201b70dc9cff930da7c1bd7712efe56d78d40d38ce0e7583ee910d38ce0ed9a07bec29709c"
  9172. "1df614ba4736e0383b6483ee910d38ce0ed9a07b64038eb34336e85e89b686bdf7968ff338adf1782aa3a9f1ae05ce8e1d9e8bea1e350c"
  9173. "8eb3430da37bd40d38ce0e7583ee910d38ce0ed9a07bec29709c1df614ba4736e0383b6483ee910d38ce0ed9a07b64038eb34336e85ea9"
  9174. "e73ed4feaef64fe9cf95a5bf8ef67b2fca727ebeeecb79c0efb5c0c1d9604e6de33926cf457170fedda186d13dea061c6787ba41f7c806"
  9175. "1c67876cd03df61438ce0e7b0add231b709c1db241f7c8061c67876cd03db201c7d9211b74afd473df6aff19eb9ac6322e69fc706eff05"
  9176. "c9bc8e2fda603c95d1d478d70267c74ecfa9c737cc7170ef1e2ffce9cafffeb5fdec6db477a222785ebab5bf731c9ce738d436ba473d81"
  9177. "e3ecf0efacba4736e0383b6483eeb1a7c07176d853e81ed980e3ec900dba4736e0383b6483ee910d38ce0ed9a07b25da1af6de5b3ecee3"
  9178. "b4c6e3a98ca6c63b18383b76782eaa7bd43038ce0e358cee5137e0383bd40dba4736e0383b6483eeb1a7c07176d853e81ed980e3ec900d"
  9179. "ba4736e0383b6483ee910d38ce0ed9a07b25da1af6de5b3ecee3b4c6e3a98ca6c6bb16383b76782eaa7bd43038ce0e358cee5137e0383b"
  9180. "d40dba4736e0383b6483eeb1a7c07176d853e81ed980e3ec900dba4736e0383b6483ee910d38ce0ed9a07b25da1af6de5b3ecee3b4c6e3"
  9181. "a98ca6c6bb16383b76782eaa7bd43038ce0e358cee5137e0383bd40dba4736e0383b6483eeb1a7c07176d853e81ed980e3ec900dba4736"
  9182. "e0383b6483ee910d38ce0ed9a07b25da1af6de5b3ecee3b4c6e3a98ca6c6bb16383b76782eaa7bd43038ce0e358cee5137e0383bd40dba"
  9183. "4736e0383b6483eeb1a7c07176d853e81ed980e3ec900dba4736e0383b6483ee910d38ce0ed9a07bf11b286e0820"),
  9184. { { 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 } });
  9185. // the slot exposes FL's controls; FL indices map both ways; unreadable states keep the old conversion
  9186. CHECK (lynx::philter::flIndexOf (6) == 6 && lynx::philter::flIndexOf (8) == 9 && lynx::philter::flIndexOf (8 + 5 * 7 + 4) == 8 + 44 * 7 + 9);
  9187. for (int i = 0; i < lynx::philter::paramCount; ++i)
  9188. CHECK (lynx::philter::paramOfFlIndex (lynx::philter::flIndexOf (i)) == i);
  9189. CHECK (lynx::philter::paramOfFlIndex (8 + 3) == -1 && lynx::philter::paramOfFlIndex (8 + 12) == -1);
  9190. CHECK (lynx::philter::parse ({ 3, 0, 12, 0, 1, 2, 3, 4 }) == nullptr);
  9191. CHECK (lynx::effectInfo (lynx::EffectKind::lovePhilter).params.size() == (std::size_t) lynx::philter::paramCount);
  9192. CHECK (lynx::philter::paramText (5, 0.0f) == "pattern 1" && lynx::philter::paramText (8 + 3, 0.5f) == "centred");
  9193. // a control edited in LYNX is applied when the effect is built from the slot (the state keeps FL's value)
  9194. {
  9195. const auto stateA = hex (
  9196. "03000c00789cedda316ed44018c5f1cfd90b5050234a0e81702a1a6e4045c11d68e7005c00c40928e96810254590380225e552538471ec"
  9197. "d97c314c841e0259cfff91463bf66e7e3b9ac42fdf582e1151961e776abf1f314ce3b3a5d7f6e4fb8bf1b2b6693c5cf56118e6b79ed5fe"
  9198. "b2f6b7b53fadfd41ed8f1379378d9fd77e88a50d57c3c3f597dcd28ee3fcfaadbe9e8fcb24e272d56238fd4019a73e2c5f3074c1ebf732"
  9199. "745a8ee51507670bce99e89cbff9e5076eb9f8be8e378f9b7df3fcfae2dbba37afcb9c0b391b0eb16ec795ff657cfd6a6aefc7cf17178f"
  9200. "f2f9f6f9fafe8769342c7dfabe58e65e62db7f5338fb74fe4f96acafa5e3f8bbf3eb6b75eb1e75068eb3d3b241b95ef2ffc0d67ebc8bb8"
  9201. "b792b65e2ffc9bfa636a64068e9fc3de44f77a7b93adfc6e7170fec661afa17b64038eb34336e81ed980e3ec900dba4736e0383b6483ee"
  9202. "957aee61ed9fe27a4d63194ffd63fdccf44cc7f972fef42408cf6ce0e06c30a7b6711f93fba238387fee50c3e81e75038eb343dda07b64"
  9203. "038eb34336e81e7b0a1c67873d85ee910d38ce0ed9a07b64038eb34336e81ed980e3ec900dba57625ec3d65bcbc7799cd6b83f95ded478"
  9204. "d60267c70ef745758f1a06c7d9a186d13dea061c6787ba41f7c8061c67876cd03df61438ce0e7b0add231b709c1db241f7c8061c67876c"
  9205. "d03db201c7d9211b74afc4bc86adb7968ff338ad717f2abda9f1ac05ce8e1dee8bea1e350c8eb3430da37bd40d38ce0e7583ee910d38ce"
  9206. "0ed9a07bec29709c1df614ba4736e0383b6483ee910d38ce0ed9a07b64038eb34336e85e89790d5b6f2d1fe7715ae3fe547a53e3590b9c"
  9207. "1d3bdc17d53d6a181c67871a46f7a81b709c1dea06dd231b709c1db241f7d853e0383bec29748f6cc07176c806dd231b709c1db241f7c8"
  9208. "061c67876cd0bd12f31ab6de5a3ecee3b4c6fda9f4a6c6b316383b76b82faa7bd43038ce0e358cee5137e0383bd40dba4736e0383b6483"
  9209. "eeb1a7c07176d853e81ed980e3ec900dba4736e0383b6483ee910d38ce0ed9a07b25e6356cbdb57c9cc7698dfb53e94d8d672d7076ec70"
  9210. "5f54f7a861709c1d6a18dda36ec07176a81b748f6cc07176c806dd634f81e3ecb0a7d03db201c7d9211b748f6cc07176c806dd231b709c"
  9211. "1db241f74acc6bd87a6bf9388fd31af7a7d29b1acf5ae0ecd8e1bea8ee51c3e0383bd430ba47dd80e3ec5037e81ed980e3ec900dbac79e"
  9212. "02c7d9614fa17b64038eb34336e81ed980e3ec900dba4736e0383b6483eec54ff7970e90");
  9213. auto slot = lynx::philter::slotFromFl (stateA);
  9214. CHECK (slot.has_value() && ! lynx::philter::slotFromFl (hex ("0300000078")).has_value());
  9215. if (slot)
  9216. {
  9217. auto render = [&] (const lynx::EffectSlot& sl) {
  9218. auto fx = lynx::makeEffect (sl);
  9219. fx->prepare (44100.0);
  9220. std::vector<float> l (inL), r (inR);
  9221. for (int s = 0; s < 4096; s += 512)
  9222. {
  9223. fx->setSongPosition (s * 2.0 * 960.0 / 44100.0);
  9224. fx->process (l.data() + s, r.data() + s, 512, sl.params, 120.0);
  9225. }
  9226. return l[3333];
  9227. };
  9228. const auto base = render (*slot);
  9229. auto edited = *slot;
  9230. edited.params[8] = 0.1f; // unit 1 cutoff
  9231. CHECK (base == 0.0f || base != render (edited));
  9232. CHECK (base == render (*slot));
  9233. }
  9234. }
  9235. }
  9236. TEST (fruity_vocoder_round66_goldens)
  9237. {
  9238. // REA_rodada66 §6 goldens against the round-70 engine (core/FruityVocoder), through FL's state import and the mixer
  9239. // effect: Fruity Vocoder_x64.dll on our own states (44.1 kHz, blocks of 512): left = modulator, right = carrier.
  9240. // The first state's attack (5 ms) is below the knob range (10…1000): the plugin uses it as it is (REA_rodada78 §1)
  9241. lynx::ScopedFlushDenormals ftz;
  9242. auto hex = [] (const char* h) {
  9243. std::vector<std::uint8_t> v;
  9244. for (std::size_t i = 0; h[i] != 0 && h[i + 1] != 0; i += 2)
  9245. v.push_back ((std::uint8_t) std::stoi (std::string (h + i, 2), nullptr, 16));
  9246. return v;
  9247. };
  9248. constexpr int N = 8192;
  9249. std::vector<float> inL (N), inR (N);
  9250. std::uint32_t seed = 12345;
  9251. for (int i = 0; i < N; ++i)
  9252. {
  9253. seed = (seed * 1103515245u + 12345u) & 0x7fffffffu;
  9254. const auto nz = (float) ((double) seed / 2147483648.0 - 0.5);
  9255. const auto tri = (float) ((i % 200) - 100) / 100.0f;
  9256. const float g = i < 4000 ? 1.0f : 0.0f;
  9257. inL[(std::size_t) i] = 0.5f * tri * g + (i >= 100 && i < 300 ? nz * 0.6f : 0.0f);
  9258. inR[(std::size_t) i] = 0.25f * tri * g;
  9259. }
  9260. struct Golden
  9261. {
  9262. int i;
  9263. float v;
  9264. };
  9265. auto run = [&] (const std::vector<std::uint8_t>& state, const std::vector<Golden>& gold) {
  9266. const auto slot = lynx::effectFromFl ("Fruity Vocoder", state, 120.0);
  9267. CHECK (slot.has_value() && slot->kind == lynx::EffectKind::vocoder);
  9268. if (! slot || slot->kind != lynx::EffectKind::vocoder)
  9269. return;
  9270. auto fx = lynx::makeEffect (*slot);
  9271. fx->prepare (44100.0);
  9272. std::vector<float> l (inL), r (inR);
  9273. for (int s = 0; s < N; s += 512)
  9274. fx->process (l.data() + s, r.data() + s, 512, slot->params, 120.0);
  9275. for (const auto& g : gold)
  9276. CHECK (l[(std::size_t) g.i] == g.v && r[(std::size_t) g.i] == g.v);
  9277. };
  9278. // 32 bands, order 3, inverted carrier bank with a formant shift, fast attack (5 ms, outside the knob range)
  9279. run (hex (
  9280. "02000000200000000300000002000000000000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000"
  9281. "803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f00"
  9282. "00803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803fb80b000050c30000640000000100000044fdffff"
  9283. "78000000050000002c010000000000008c0000003c000000a0000000"),
  9284. { { 777, 0.47870129346847534f }, { 1555, 0.2731437087059021f }, { 2222, -0.41599133610725403f }, { 3333, 0.083399198949337f }, { 3999, 0.7295553088188171f }, { 4100, 0.0871216207742691f } });
  9285. // 8 bands, swapped inputs, per-band levels, modulator level 0
  9286. run (hex (
  9287. "02000000080000000200000002000000010000003f9a99193f3333333fcdcc4c3f6666663f0000803fcdcc8c3f9a99993f1027000060ea"
  9288. "00001e000000000000002c010000500000000f0000003c00000000000000000000001400000080000000"),
  9289. { { 777, 0.07233863323926926f }, { 1555, 0.1183714047074318f }, { 2222, -0.6131120920181274f }, { 3333, 0.18050849437713623f }, { 3999, 0.056527279317379f }, { 4100, 0.05711518973112106f } });
  9290. }
  9291. // ---- Fruity WaveShaper / Fruity Soft Clipper (REA_rodada51) ------------------------------------------------------------
  9292. // States written here from our own values (no FL preset data); goldens from the installation's DLLs (EXEC, blocks of 512)
  9293. namespace
  9294. {
  9295. std::vector<std::uint8_t> waveShaperTestState (int pre, int mix, int post, bool uni, bool center, int os, bool hq, bool smooth)
  9296. {
  9297. std::vector<std::uint8_t> b;
  9298. 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))); };
  9299. 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))); };
  9300. 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))); };
  9301. i32 (0x40003);
  9302. i32 (pre), i32 (mix), i32 (post);
  9303. b.push_back (uni ? 1 : 0), b.push_back (center ? 1 : 0);
  9304. i32 (os);
  9305. 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 },
  9306. { 0.25, 0.9, -0.3f, 3 }, { 0.2, 1.0, 0.5f, 9 } };
  9307. i32 (3), i32 (3), i32 (5);
  9308. for (const auto& p : pts)
  9309. {
  9310. f64 (p.dt), f64 (p.y), f32 (p.t);
  9311. b.push_back ((std::uint8_t) p.mode), b.push_back (0), b.push_back (0), b.push_back (0);
  9312. }
  9313. i32 (0), i32 (-1), i32 (-1), i32 (-1), i32 (-1);
  9314. i32 (1);
  9315. b.push_back (hq ? 1 : 0), b.push_back (smooth ? 1 : 0);
  9316. return b;
  9317. }
  9318. } // namespace
  9319. TEST (fruity_waveshaper_and_soft_clipper)
  9320. {
  9321. std::vector<float> inL (4096), inR (4096);
  9322. for (int n = 0; n < 4096; ++n)
  9323. {
  9324. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  9325. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  9326. }
  9327. struct Gold { int i; float l, r; };
  9328. auto close = [] (float a, float b) { return std::abs (a - b) <= 2.0e-7f * std::max (1.0f, std::abs (b)); };
  9329. // Soft Clipper: bit-exact against Fruity Soft Clipper_x64.dll
  9330. auto clip = [&] (int thr, int post, std::initializer_list<Gold> gold) {
  9331. std::vector<float> l (inL), r (inR);
  9332. for (int s = 0; s < 4096; s += 512)
  9333. {
  9334. lynx::flshape::softClip (l.data() + s, 512, thr, post);
  9335. lynx::flshape::softClip (r.data() + s, 512, thr, post);
  9336. }
  9337. for (const auto& g : gold)
  9338. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  9339. };
  9340. clip (100, 128, { { 100, 0.0f, -0.949052989f }, { 1000, -0.99617976f, 0.872887492f }, { 2047, 0.649999976f, -0.944175363f },
  9341. { 3000, -0.99617976f, -0.699999988f }, { 4095, 0.25f, 0.600000024f } });
  9342. clip (1, 150, { { 100, 0.0f, -1.04588532f }, { 1000, -1.27003884f, 0.930835366f }, { 2047, 0.750244498f, -1.03539562f },
  9343. { 3000, -1.27003884f, -0.790091455f }, { 4095, 0.347655922f, 0.708338201f } });
  9344. clip (64, 140, { { 100, 0.0f, -1.08492827f }, { 1000, -1.21535313f, 0.990323186f }, { 2047, 0.804165959f, -1.0770781f },
  9345. { 3000, -1.21535313f, -0.849189103f }, { 4095, 0.319320858f, 0.754407823f } });
  9346. {
  9347. const std::vector<std::uint8_t> st { 64, 0, 0, 0, 140, 0, 0, 0 };
  9348. const auto slot = lynx::effectFromFl ("Fruity Soft Clipper", st, 120.0);
  9349. CHECK (slot && slot->kind == lynx::EffectKind::fruitySoftClipper);
  9350. if (slot)
  9351. {
  9352. const auto s = lynx::flshape::softClipperFromParams (slot->params.data());
  9353. CHECK (s.threshold == 64 && s.post == 140);
  9354. auto fx = lynx::makeEffect (*slot);
  9355. fx->prepare (44100.0);
  9356. std::vector<float> l (inL), r (inR);
  9357. fx->process (l.data(), r.data(), 4096, slot->params, 120.0);
  9358. CHECK (l[3000] == -1.21535313f && r[4095] == 0.754407823f);
  9359. }
  9360. }
  9361. // WaveShaper: tables (bipolar / unipolar) and the engine on three of our states, live and as FL renders
  9362. const auto stA = waveShaperTestState (150, 100, 120, false, true, 1, true, true); // CENTER, ×2, HQ, smooth
  9363. const auto stB = waveShaperTestState (100, 128, 128, true, false, 0, false, false); // unipolar
  9364. const auto stC = waveShaperTestState (192, 90, 100, false, false, 2, false, false); // ×4
  9365. const auto pA = lynx::flshape::parseWaveShaper (stA);
  9366. CHECK (pA && pA->points.size() == 5 && pA->pre == 150 && pA->center && pA->hq && pA->smooth && pA->oversampling == 1);
  9367. if (pA)
  9368. {
  9369. const auto T = lynx::flshape::shaperTable (pA->points, false);
  9370. CHECK (T.size() == 16386 && T[1] == 3277 && T[1001] == 18824 && T[5001] == 26195 && T[9001] == 11469 && T[12001] == 24614
  9371. && T[16384] == 32767 && T[0] == T[1]);
  9372. const auto U = lynx::flshape::shaperTable (pA->points, true);
  9373. CHECK (U.size() == 32770 && U[1] == -26214 && U[1001] == -6408 && U[5001] == 17073 && U[9001] == 19551 && U[12001] == 14549);
  9374. }
  9375. auto shape = [&] (const std::vector<std::uint8_t>& st, bool render, std::initializer_list<Gold> gold) {
  9376. const auto s = lynx::flshape::parseWaveShaper (st);
  9377. CHECK (s.has_value());
  9378. if (! s)
  9379. return;
  9380. lynx::flshape::WaveShaperEngine e;
  9381. e.setCurve (s->points);
  9382. e.prepare (44100.0);
  9383. e.setControls (s->pre, s->mix, s->post, s->oversampling, s->hq, s->smooth, s->center, s->unipolar, render);
  9384. std::vector<float> l (inL), r (inR);
  9385. for (int i = 0; i < 4096; i += 512)
  9386. e.process (l.data() + i, r.data() + i, 512);
  9387. for (const auto& g : gold)
  9388. CHECK (close (l[(std::size_t) g.i], g.l) && close (r[(std::size_t) g.i], g.r));
  9389. };
  9390. shape (stA, false, { { 100, 0.59108448f, 0.469315082f }, { 1000, 0.284353405f, 1.02621937f }, { 2047, -0.533613563f, 0.117038473f },
  9391. { 3000, 0.22673659f, -1.13572407f }, { 4095, -0.267153352f, 0.822866499f } });
  9392. shape (stA, true, { { 100, 0.794869065f, -0.127601087f }, { 1000, -0.00841622613f, 0.900729597f }, { 2047, 0.450603426f, -0.325943232f },
  9393. { 3000, -0.0530530401f, -0.32303831f }, { 4095, 1.0959363f, 0.946009099f } });
  9394. shape (stB, false, { { 100, -0.206451416f, 0.502471924f }, { 1000, -0.799987793f, 0.761169434f }, { 2047, 0.411743164f, 0.514160156f },
  9395. { 3000, -0.799987793f, 0.596221924f }, { 4095, -0.157653809f, 0.334106445f } });
  9396. shape (stB, true, { { 100, -0.206390381f, 0.502493739f }, { 1000, -0.799987793f, 0.761169434f }, { 2047, 0.411743164f, 0.514144361f },
  9397. { 3000, -0.799987793f, 0.596221924f }, { 4095, -0.157653809f, 0.334106445f } });
  9398. shape (stC, false, { { 100, 0.317797363f, 0.0483714938f }, { 1000, -0.445489585f, -0.533142388f }, { 2047, 0.634525597f, 0.942175806f },
  9399. { 3000, -0.445489585f, 0.514707327f }, { 4095, 0.200391173f, -0.937523127f } });
  9400. shape (stC, true, { { 100, 0.61165309f, -0.0993360579f }, { 1000, 0.0375693105f, 0.667849183f }, { 2047, 0.353716731f, -0.223758236f },
  9401. { 3000, 0.0375693105f, -0.223787636f }, { 4095, 0.843308687f, 0.688474894f } });
  9402. // the mixer effect from the FL import: controls as parameters, the curve in the slot's state; host blocks of any size
  9403. const auto slot = lynx::effectFromFl ("Fruity WaveShaper", stC, 120.0);
  9404. CHECK (slot && slot->kind == lynx::EffectKind::fruityWaveShaper && slot->state == stC);
  9405. if (slot)
  9406. {
  9407. auto fx = lynx::makeEffect (*slot);
  9408. fx->prepare (44100.0);
  9409. std::vector<float> l (inL), r (inR);
  9410. fx->process (l.data(), r.data(), 1024, slot->params, 120.0);
  9411. fx->process (l.data() + 1024, r.data() + 1024, 1024, slot->params, 120.0);
  9412. fx->process (l.data() + 2048, r.data() + 2048, 2048, slot->params, 120.0);
  9413. CHECK (close (l[1000], -0.445489585f) && close (r[2047], 0.942175806f) && close (l[4095], 0.200391173f));
  9414. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityWaveShaper, 3, slot->params[3], 120.0) == "x4");
  9415. }
  9416. // an unreadable state keeps the old approximation
  9417. CHECK (lynx::effectFromFl ("Fruity WaveShaper", { 1, 2, 3 }, 120.0)->kind == lynx::EffectKind::distortion);
  9418. CHECK (lynx::effectFromFl ("Fruity Soft Clipper", { 1, 2, 3 }, 120.0)->kind == lynx::EffectKind::distortion);
  9419. }
  9420. // ---- Fruity Parametric EQ 2 / Fruity Parametric EQ (REA_rodada50) -----------------------------------------------------
  9421. // States written here from our own values (no FL preset data); goldens from the installation's DLLs (EXEC, blocks of 512)
  9422. namespace
  9423. {
  9424. struct PeqTestBand { int type, freq, width, gain, order; };
  9425. std::vector<std::uint8_t> peq2TestState (const PeqTestBand (&bands)[7], int main, int uiFlags, int options, int mask)
  9426. {
  9427. std::vector<std::uint8_t> b;
  9428. 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))); };
  9429. i32 (8);
  9430. for (int copy = 0; copy < 2; ++copy)
  9431. {
  9432. for (const auto& x : bands) i32 (x.gain);
  9433. for (const auto& x : bands) i32 (x.freq);
  9434. for (const auto& x : bands) i32 (x.width);
  9435. for (const auto& x : bands) i32 (x.type);
  9436. for (const auto& x : bands) i32 (x.order);
  9437. i32 (main);
  9438. }
  9439. i32 (uiFlags), i32 (0), b.push_back (0), i32 (2), i32 (0), i32 (2), i32 (0);
  9440. b.push_back ((std::uint8_t) options);
  9441. i32 (mask), i32 (1), i32 (0);
  9442. for (int v : { 3, 1, 2, 2, 2 }) i32 (v);
  9443. i32 (0);
  9444. return b;
  9445. }
  9446. std::vector<std::uint8_t> peq1TestState (const PeqTestBand (&bands)[7], int main)
  9447. {
  9448. std::vector<std::uint8_t> b;
  9449. 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))); };
  9450. for (const auto& x : bands) i32 (x.gain);
  9451. for (const auto& x : bands) i32 (x.freq);
  9452. for (const auto& x : bands) i32 (x.width);
  9453. for (const auto& x : bands) i32 (x.type);
  9454. i32 (main);
  9455. return b;
  9456. }
  9457. } // namespace
  9458. // EQUO test state (our own LCG curves, no FL data; REA_rodada61 equo/teststate.py builds the same bytes)
  9459. static std::vector<std::uint8_t> equoTestState (int ovs, int bw, bool add)
  9460. {
  9461. std::vector<std::uint8_t> b;
  9462. 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); };
  9463. auto putI = [&put] (std::int32_t v) { put (&v, 4); };
  9464. auto putF = [&put] (float v) { put (&v, 4); };
  9465. std::uint32_t seed = 12345;
  9466. auto rnd = [&seed] {
  9467. seed = (std::uint32_t) (((std::uint64_t) seed * 1103515245u + 12345u) & 0x7fffffffu);
  9468. return (float) ((double) (seed % 20001) / 10000.0);
  9469. };
  9470. putI (4);
  9471. for (int k = 0; k < 93; ++k)
  9472. {
  9473. putF (rnd());
  9474. putI (k % 11 == 0 ? 1 : (k % 13 == 0 ? 2 : 0));
  9475. }
  9476. for (int bank = 0; bank < 9; ++bank)
  9477. for (int k = 0; k < 93; ++k)
  9478. {
  9479. const float v = rnd();
  9480. putF (bank == 8 ? 0.75f + v * 0.25f : v);
  9481. }
  9482. for (int v : { 1500, 90, 2600, 170, 150, ovs, bw, -1, 0, 0 })
  9483. putI (v);
  9484. for (int v : { 0, add ? 1 : 0, 0 })
  9485. b.push_back ((std::uint8_t) v);
  9486. putI (1);
  9487. return b;
  9488. }
  9489. // Pitch Shifter state (version 7, our own settings; REA_rodada61 ps/teststate.py builds the same bytes)
  9490. static std::vector<std::uint8_t> pitchShifterTestState (std::array<int, 13> p, int engine, int detector)
  9491. {
  9492. std::vector<std::uint8_t> b;
  9493. auto putI = [&b] (std::int32_t v) { const auto* q = (const std::uint8_t*) &v; b.insert (b.end(), q, q + 4); };
  9494. putI (7);
  9495. for (int v : p)
  9496. putI (v);
  9497. putI (p[11]);
  9498. b.push_back (0);
  9499. putI (engine);
  9500. putI (detector);
  9501. putI (1000);
  9502. b.push_back (1);
  9503. b.push_back (0);
  9504. return b;
  9505. }
  9506. TEST (pitch_shifter_exact)
  9507. {
  9508. std::vector<float> inL (8192), inR (8192);
  9509. for (int n = 0; n < 8192; ++n)
  9510. {
  9511. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0) * 0.3f;
  9512. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0) * 0.3f;
  9513. }
  9514. struct Gold { int i; float l, r; };
  9515. // goldens: Pitch Shifter_x64.dll on the same states and signal (REA_rodada61)
  9516. auto run = [&] (const std::vector<std::uint8_t>& st, int latency, std::initializer_list<Gold> gold) {
  9517. lynx::psfl::State s;
  9518. CHECK (st.size() == 75 && lynx::psfl::parsePitchShifter (st.data(), st.size(), s));
  9519. // through the mixer effect: FL import → slot parameters → effect, odd host blocks
  9520. const auto slot = lynx::effectFromFl ("Pitch Shifter", st, 120.0);
  9521. CHECK (slot && slot->kind == lynx::EffectKind::flPitchShifter);
  9522. if (! slot)
  9523. return;
  9524. auto fx = lynx::makeEffect (*slot);
  9525. fx->prepare (44100.0);
  9526. std::vector<float> l (inL), r (inR);
  9527. for (int i = 0, k = 0; i < 8192; ++k)
  9528. {
  9529. const int n = std::min (8192 - i, k % 2 == 0 ? 700 : 333);
  9530. fx->process (l.data() + i, r.data() + i, n, slot->params, 120.0);
  9531. i += n;
  9532. }
  9533. CHECK (fx->latencySamples() == latency);
  9534. for (const auto& g : gold)
  9535. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  9536. };
  9537. // "FFT" (pitch-synchronous) engine: +5 st, formant −3 st, correction 50 %, mix 79 %, accurate detector
  9538. run (pitchShifterTestState ({ 500, -300, 2, 1900, 8500, 200, 0, 0, 768, 0, 0, 32768, 0 }, 0, 1), 2457,
  9539. { { 1000, 0.0f, 0.0f }, { 3000, 0.142123833f, 0.0878119618f }, { 5000, 0.139498621f, -0.365035176f },
  9540. { 7000, 0.139498621f, 0.161137447f }, { 8191, 0.449655473f, 0.453882188f } });
  9541. // granular engine: −7 st, 60 ms grains, density 300 %, jitter 30 %, random 20 %, post delay with feedback 40 %
  9542. run (pitchShifterTestState ({ -700, 0, 2, 2400, 6000, 300, 30, 20, 200, 40, 1, 65536, 0 }, 1, 0), 0,
  9543. { { 1000, -0.0736689344f, -0.349420458f }, { 3000, 0.086801365f, 0.0471927896f }, { 5000, -0.0939825475f, 0.31712544f },
  9544. { 7000, -0.28462702f, -0.437854528f }, { 8191, 0.138916701f, -0.0426892526f } });
  9545. CHECK (lynx::formatEffectParam (lynx::EffectKind::flPitchShifter, 0, 0.75f, 120.0) == "+6.00 st");
  9546. }
  9547. TEST (equo_exact)
  9548. {
  9549. std::vector<float> inL (4096), inR (4096);
  9550. for (int n = 0; n < 4096; ++n)
  9551. {
  9552. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  9553. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  9554. }
  9555. struct Gold { int i; float l, r; };
  9556. // goldens: EQUO_x64.dll run on the same states and signal (REA_rodada61 §5)
  9557. auto run = [&] (const std::vector<std::uint8_t>& st, bool hq, bool ticks, std::initializer_list<Gold> gold) {
  9558. const auto s = lynx::equofx::parse (st);
  9559. CHECK (s.has_value());
  9560. if (! s)
  9561. return;
  9562. lynx::equofx::Engine e;
  9563. e.prepare (44100.0, hq);
  9564. e.set (*s);
  9565. std::vector<float> l (inL), r (inR);
  9566. if (! ticks)
  9567. e.process (l.data(), r.data(), 4096);
  9568. else
  9569. {
  9570. auto k = *s;
  9571. e.startFromConstruction();
  9572. for (int i = 0; i < 4096; i += 256)
  9573. {
  9574. if (i == 2048)
  9575. k.knob[2] = 5300, e.setKnobs (k);
  9576. e.tick();
  9577. e.process (l.data() + i, r.data() + i, 256);
  9578. }
  9579. }
  9580. for (const auto& g : gold)
  9581. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  9582. };
  9583. const auto plain = equoTestState (0, 150, false);
  9584. run (plain, false, false, { { 100, -0.00154058414f, -0.00550224911f }, { 1000, -0.908149242f, 0.619277894f }, { 2047, 0.411961317f, -0.364051819f },
  9585. { 3000, -1.42823684f, -0.0602950938f }, { 4095, -0.0661701784f, 0.300809383f } });
  9586. run (equoTestState (1, 300, true), false, false,
  9587. { { 100, 0.0312003642f, 0.0184187777f }, { 1000, 0.0379521511f, 0.224126667f }, { 2047, -0.135689721f, 0.0444660783f },
  9588. { 3000, 0.113949843f, -1.00256324f }, { 4095, -0.176232055f, 0.79159987f } });
  9589. run (plain, true, false, { { 100, -0.00104017637f, 0.000872685807f }, { 1000, -0.0426855832f, 0.0402083024f }, { 2047, 0.191993102f, 0.174184501f },
  9590. { 3000, -0.114048228f, 0.419817507f }, { 4095, 0.0376945846f, -0.274040252f } });
  9591. run (plain, false, true, { { 100, -0.0019897793f, -0.00981196482f }, { 1000, -1.09574401f, 0.83203429f }, { 2047, 0.839113712f, -0.691361725f } });
  9592. // the mixer effect from the FL import: FL's state in the slot, the knobs in its parameters, any host block size
  9593. const auto slot = lynx::effectFromFl ("EQUO", plain, 140.0);
  9594. CHECK (slot && slot->kind == lynx::EffectKind::equo && slot->state == plain);
  9595. if (slot)
  9596. {
  9597. auto back = *lynx::equofx::parse (plain);
  9598. lynx::equofx::applyParams (slot->params, back);
  9599. CHECK (back.knob == lynx::equofx::parse (plain)->knob && back.bandwidth == 150 && back.oversample == 0);
  9600. auto fx = lynx::makeEffect (*slot);
  9601. fx->prepare (44100.0);
  9602. std::vector<float> l (inL), r (inR);
  9603. fx->process (l.data(), r.data(), 700, slot->params, 0.0);
  9604. fx->process (l.data() + 700, r.data() + 700, 3396, slot->params, 0.0);
  9605. // the engine glides its bands per processing call, as FL does per host buffer: compare with the engine run on
  9606. // the same 700 + 3396 split (sample 1000 differs from the single-call golden), and with the golden once settled
  9607. lynx::equofx::Engine ref;
  9608. ref.prepare (44100.0, false);
  9609. ref.set (*lynx::equofx::parse (plain));
  9610. std::vector<float> rl (inL), rr (inR);
  9611. ref.process (rl.data(), rr.data(), 700);
  9612. ref.process (rl.data() + 700, rr.data() + 700, 3396);
  9613. CHECK (l == rl && rr == r && r[3000] == -0.0602950938f);
  9614. CHECK (lynx::formatEffectParam (lynx::EffectKind::equo, 4, slot->params[4], 120.0, slot->params).find ("dB") != std::string::npos);
  9615. }
  9616. CHECK (! lynx::equofx::parse (std::vector<std::uint8_t> (100, 0)).has_value());
  9617. }
  9618. // Multiband Delay test state (version 7, our own LCG cells, no FL data; REA_rodada71 mbd/py/teststate.py builds the same bytes)
  9619. static std::vector<std::uint8_t> multibandDelayTestState (std::array<int, 12> globals, std::uint32_t seed)
  9620. {
  9621. std::vector<std::uint8_t> b;
  9622. auto putI = [&b] (std::int32_t v) { const auto* q = (const std::uint8_t*) &v; b.insert (b.end(), q, q + 4); };
  9623. putI (7);
  9624. putI (396);
  9625. std::uint64_t s = seed;
  9626. for (int i = 0; i < 384; ++i)
  9627. {
  9628. s = (s * 1103515245u + 12345u) & 0x7fffffffu;
  9629. putI ((std::int32_t) ((s % 100001u) * (1u << 30) / 100000u));
  9630. }
  9631. for (int v : globals)
  9632. putI (v);
  9633. putI (0);
  9634. putI (3);
  9635. for (int v : { 1, 1, 0 })
  9636. b.push_back ((std::uint8_t) v);
  9637. b.insert (b.end(), 192, 0);
  9638. return b;
  9639. }
  9640. TEST (multiband_delay_exact)
  9641. {
  9642. std::vector<float> inL (8192), inR (8192);
  9643. for (int n = 0; n < 8192; ++n)
  9644. {
  9645. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0 * 0.3);
  9646. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0 * 0.3);
  9647. }
  9648. struct Gold { int i; float l, r; };
  9649. // goldens: Multiband Delay_x64.dll (+ its engine_x64.dll) on the same states and signal, 512-frame blocks (REA_rodada71)
  9650. auto run = [&] (const std::vector<std::uint8_t>& st, int latency, float tol, std::initializer_list<Gold> gold) {
  9651. CHECK (st.size() == 1795 && lynx::mbdelay::parse (st).has_value());
  9652. const auto slot = lynx::effectFromFl ("Multiband Delay", st, 120.0);
  9653. CHECK (slot && slot->kind == lynx::EffectKind::multibandDelay && slot->state == st);
  9654. if (! slot)
  9655. return;
  9656. auto fx = lynx::makeEffect (*slot);
  9657. fx->prepare (44100.0);
  9658. std::vector<float> l (inL), r (inR);
  9659. for (int i = 0, k = 0; i < 8192; ++k)
  9660. {
  9661. const int n = std::min (8192 - i, k % 2 == 0 ? 700 : 333);
  9662. fx->process (l.data() + i, r.data() + i, n, slot->params, 120.0);
  9663. i += n;
  9664. }
  9665. CHECK (fx->latencySamples() == latency);
  9666. for (const auto& g : gold)
  9667. CHECK (std::fabs (l[(std::size_t) g.i] - g.l) <= tol && std::fabs (r[(std::size_t) g.i] - g.r) <= tol);
  9668. };
  9669. // steep crossover, keep pitch, weighted feedback, compressor
  9670. run (multibandDelayTestState ({ 2000, 0, 3000, 600, 1, 1, 3500, 64, 0, 1, 30000, 0 }, 777), 0, 0.0f,
  9671. { { 1000, -0.125012308f, 0.0675066486f }, { 3000, -0.125002861f, -0.0525011979f }, { 5000, -0.110439189f, 0.0597563162f },
  9672. { 7000, -0.146480903f, 0.0139919929f }, { 8191, -0.0454496294f, 0.0398242511f } });
  9673. // gentle crossover, harmonic scale, delay ×0.1, saturation
  9674. run (multibandDelayTestState ({ -2500, 1, 4500, 0, 0, 0, 0, 128, 1, 0, 1000, 1 }, 4242), 0, 0.0f,
  9675. { { 1000, 0.0679653957f, 0.0137896547f }, { 3000, 0.0620588325f, 0.0558352768f }, { 5000, 0.0558495969f, -0.0220566839f },
  9676. { 7000, 0.057009533f, 0.00332171144f }, { 8191, 0.0673644915f, 0.00163267844f } });
  9677. // linear-phase crossover (FL's IPP FFTs are approximated: about −120 dB), clipper
  9678. run (multibandDelayTestState ({ 1500, 0, 2000, 1500, 2, 0, 1000, 100, 0, 0, 5000, 2 }, 99), 1536, 2e-6f,
  9679. { { 1000, 0.0f, 0.0f }, { 3000, 0.103910387f, -0.0521105044f }, { 5000, 0.0516818762f, -0.0134486742f },
  9680. { 7000, 0.065563485f, -0.00765838847f }, { 8191, -0.0376662463f, -0.0434602089f } });
  9681. const auto st = multibandDelayTestState ({ 2000, 0, 3000, 600, 1, 1, 3500, 64, 0, 1, 30000, 0 }, 777);
  9682. const auto s = *lynx::mbdelay::parse (st);
  9683. CHECK (lynx::mbdelay::parse (lynx::mbdelay::serialize (s))->raw == s.raw);
  9684. lynx::EffectParams p {};
  9685. lynx::mbdelay::toParams (s, p);
  9686. auto back = s;
  9687. lynx::mbdelay::applyParams (p, back);
  9688. CHECK (back.raw == s.raw);
  9689. CHECK (lynx::mbdelay::engineValue (s, 384 + 7) == (float) (192.0 / 256.0));
  9690. CHECK (lynx::formatEffectParam (lynx::EffectKind::multibandDelay, 4, p[4], 120.0) == "steep");
  9691. CHECK (lynx::formatEffectParam (lynx::EffectKind::multibandDelay, 7, 1.0f, 120.0) == "100% wet");
  9692. CHECK (! lynx::mbdelay::parse (std::vector<std::uint8_t> (16, 0)).has_value());
  9693. }
  9694. TEST (fruity_parametric_eq_exact)
  9695. {
  9696. std::vector<float> inL (4096), inR (4096);
  9697. for (int n = 0; n < 4096; ++n)
  9698. {
  9699. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  9700. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  9701. }
  9702. struct Gold { int i; float l, r; };
  9703. // high pass Steep 8, low shelf, steep peaking, a 0 dB peaking (0.2 s countdown), notch Gentle 4, high shelf Gentle 8,
  9704. // low pass Gentle 6
  9705. const PeqTestBand bands[7] = { { 3, 8000, 30000, 0, -3 }, { 5, 15000, 40124, 600, 0 }, { 6, 30000, 20000, -900, -2 },
  9706. { 6, 35000, 25412, 0, 0 }, { 4, 40000, 10000, 0, 1 }, { 7, 50000, 30000, 450, 3 },
  9707. { 1, 60000, 45000, 0, 2 } };
  9708. const PeqTestBand bands1[7] = { { 3, 6000, 20000, 0, 0 }, { 5, 15000, 30000, 600, 0 }, { 6, 30000, 20000, -900, 0 },
  9709. { 6, 35000, 25000, 0, 0 }, { 4, 40000, 10000, 0, 0 }, { 7, 50000, 30000, 450, 0 },
  9710. { 1, 60000, 45000, 0, 0 } };
  9711. auto run = [&] (const std::optional<lynx::peqfl::Settings>& s, std::initializer_list<Gold> gold) {
  9712. CHECK (s.has_value());
  9713. if (! s)
  9714. return;
  9715. lynx::peqfl::Engine e;
  9716. e.set (*s);
  9717. e.prepare (44100.0);
  9718. std::vector<float> l (inL), r (inR);
  9719. for (int i = 0; i < 4096; i += 512)
  9720. e.process (l.data() + i, r.data() + i, 512);
  9721. for (const auto& g : gold)
  9722. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  9723. };
  9724. const auto plain = peq2TestState (bands, 300, 0, 0x60, 0x7F);
  9725. const auto hq = peq2TestState (bands, 300, 1, 0x60, 0x7F);
  9726. const auto ext = peq2TestState (bands, 300, 1, 0x62, 0x7F);
  9727. run (lynx::peqfl::parsePeq2 (plain), { { 100, 0.1340574324131012f, -0.3040016293525696f }, { 1000, 0.28402701020240784f, -0.23813000321388245f },
  9728. { 2047, -0.9301530122756958f, 0.13881002366542816f }, { 3000, 0.2846762537956238f, 0.48893973231315613f },
  9729. { 4095, -0.8481860756874084f, -0.2641225755214691f } });
  9730. run (lynx::peqfl::parsePeq2 (hq), { { 100, 0.19229719042778015f, 0.29938483238220215f }, { 1000, 0.12543821334838867f, -0.055617764592170715f },
  9731. { 2047, 1.0342068672180176f, 0.10901166498661041f }, { 3000, 0.12598788738250732f, -0.12920552492141724f },
  9732. { 4095, 0.1619236022233963f, -0.23385579884052277f } });
  9733. run (lynx::peqfl::parsePeq2 (ext), { { 100, 1.2178642749786377f, -0.21160493791103363f }, { 1000, -0.8292768001556396f, -0.23916664719581604f },
  9734. { 2047, -0.7030635476112366f, -0.1978478729724884f }, { 3000, -0.8299900889396667f, 0.9836107492446899f },
  9735. { 4095, 0.06754066050052643f, -0.34364908933639526f } });
  9736. run (lynx::peqfl::parsePeq2 (peq2TestState (bands, -250, 1, 0x60, 0x5B)), // bands 3 and 6 muted
  9737. { { 100, -0.02959616854786873f, 0.227821946144104f }, { 1000, 0.053224340081214905f, 0.06922465562820435f },
  9738. { 2047, 0.3365536034107208f, -0.05899651721119881f }, { 3000, 0.053670305758714676f, -0.07452142238616943f },
  9739. { 4095, -0.04406970739364624f, -0.12635089457035065f } });
  9740. run (lynx::peqfl::parsePeq1 (peq1TestState (bands1, 150)),
  9741. { { 100, -0.6533108353614807f, -1.6903921365737915f }, { 1000, -4.313726902008057f, 5.973916053771973f },
  9742. { 2047, 1.0222549438476562f, 4.427900314331055f }, { 3000, -4.310323238372803f, 7.825916767120361f },
  9743. { 4095, 8.344571113586426f, -0.49086111783981323f } });
  9744. // "alternative design" (Frequency Splitter's matched biquads), bit-exact
  9745. run (lynx::peqfl::parsePeq2 (peq2TestState (bands, 300, 3, 0x60, 0x7F)),
  9746. { { 100, -0.790181577205658f, -0.08691816031932831f }, { 1000, 0.4960691034793854f, -0.24172282218933105f },
  9747. { 2047, -0.42601829767227173f, 0.027932533994317055f }, { 3000, 0.49674150347709656f, 1.0799059867858887f },
  9748. { 4095, -0.35040369629859924f, -0.28149813413619995f } });
  9749. // linear phase (plain and HQ response): FFT convolution, 1536-sample latency, 0.1 s fade-in; FL's IPP FFT rounding
  9750. // is not replicated, so within 1e-6 of the DLL
  9751. for (const bool hq : { false, true })
  9752. {
  9753. const auto s = lynx::peqfl::parsePeq2 (peq2TestState (bands, hq ? -200 : 300, hq ? 1 : 0, 0x61, 0x7F));
  9754. CHECK (s && s->linearPhase);
  9755. if (! s)
  9756. continue;
  9757. std::vector<float> l (12288), r (12288);
  9758. for (int n = 0; n < 12288; ++n)
  9759. {
  9760. l[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  9761. r[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  9762. }
  9763. lynx::peqfl::Engine e;
  9764. e.set (*s);
  9765. e.prepare (44100.0);
  9766. CHECK (e.latencySamples() == 1536);
  9767. for (int i = 0; i < 12288; i += 512)
  9768. e.process (l.data() + i, r.data() + i, 512);
  9769. const std::initializer_list<Gold> gold = hq ? std::initializer_list<Gold> {
  9770. { 2000, 0.08618364483118057f, 0.11248858273029327f }, { 5000, 0.18998730182647705f, 0.428600937128067f },
  9771. { 8191, -0.1412065178155899f, -0.002996620489284396f }, { 10000, 0.18998737633228302f, -0.012570115737617016f },
  9772. { 12287, 0.40586185455322266f, -0.014698396436870098f } } : std::initializer_list<Gold> {
  9773. { 2000, 0.18538671731948853f, 0.206522598862648f }, { 5000, 0.4086928963661194f, 0.8167467713356018f },
  9774. { 8191, -0.3218550980091095f, -0.06812914460897446f }, { 10000, 0.40869277715682983f, -0.08537150919437408f },
  9775. { 12287, 0.775394082069397f, -0.08931504935026169f } };
  9776. for (const auto& g : gold)
  9777. 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);
  9778. }
  9779. // the mixer effect from the FL import: FL's values in the slot's parameters (exact round trip), any host block size
  9780. for (const auto* name : { "Fruity Parametric EQ 2", "Fruity Parametric EQ" })
  9781. {
  9782. const bool v1 = std::string (name) == "Fruity Parametric EQ";
  9783. const auto st = v1 ? peq1TestState (bands1, 150) : ext;
  9784. const auto slot = lynx::effectFromFl (name, st, 120.0);
  9785. CHECK (slot && slot->kind == lynx::EffectKind::parametricEq && lynx::peq::flLaw (slot->params));
  9786. if (! slot)
  9787. continue;
  9788. const auto back = lynx::peqfl::fromParams (slot->params);
  9789. const auto orig = v1 ? lynx::peqfl::parsePeq1 (st) : lynx::peqfl::parsePeq2 (st);
  9790. CHECK (back.freq == orig->freq && back.gain == orig->gain && back.width == orig->width && back.type == orig->type
  9791. && back.order == orig->order && back.main == orig->main && back.processing == orig->processing && back.mask == orig->mask);
  9792. auto fx = lynx::makeEffect (*slot);
  9793. fx->prepare (44100.0);
  9794. CHECK (fx->latencySamples() == (v1 ? 0 : 44));
  9795. std::vector<float> l (inL), r (inR);
  9796. fx->process (l.data(), r.data(), 1000, slot->params, 120.0);
  9797. fx->process (l.data() + 1000, r.data() + 1000, 3096, slot->params, 120.0);
  9798. if (v1)
  9799. CHECK (l[3000] == -4.310323238372803f && r[4095] == -0.49086111783981323f);
  9800. else
  9801. CHECK (l[1000] == -0.8292768001556396f && r[3000] == 0.9836107492446899f && l[4095] == 0.06754066050052643f);
  9802. // editor texts and graph
  9803. CHECK (lynx::formatEffectParam (lynx::EffectKind::parametricEq, lynx::peq::slopeParam, slot->params[lynx::peq::slopeParam], 120.0, slot->params)
  9804. == (v1 ? "12 dB/oct" : "Steep 8"));
  9805. CHECK (lynx::peq::widthText (slot->params, 0).rfind ("BW ", 0) == 0);
  9806. const auto lowCutDb = lynx::peq::responseDb (slot->params, 44100.0, 25.0), midDb = lynx::peq::responseDb (slot->params, 44100.0, 1000.0);
  9807. CHECK (lowCutDb < midDb - 5.0); // high pass below ~100 Hz (Steep 8 in the EQ 2 state, 12 dB/oct in v1)
  9808. }
  9809. // a native LYNX EQ is untouched by the FL law
  9810. CHECK (! lynx::peq::flLaw (lynx::makeEffectSlot (lynx::EffectKind::parametricEq).params));
  9811. }
  9812. TEST (fruity_panomatic_balance_blood_overdrive)
  9813. {
  9814. // REA_rodada58: goldens from the installation's DLLs (EXEC) on our own states, 44.1 kHz, blocks of 512
  9815. const lynx::ScopedFlushDenormals ftz;
  9816. std::vector<float> inL (4096), inR (4096);
  9817. for (int n = 0; n < 4096; ++n)
  9818. {
  9819. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  9820. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  9821. }
  9822. struct Gold { int i; float l, r; };
  9823. auto close = [] (float a, float b) { return std::abs (a - b) <= 2.0e-7f * std::max (1.0f, std::abs (b)); };
  9824. auto check = [&] (const std::vector<float>& l, const std::vector<float>& r, std::initializer_list<Gold> gold) {
  9825. for (const auto& g : gold)
  9826. CHECK (close (l[(std::size_t) g.i], g.l) && close (r[(std::size_t) g.i], g.r));
  9827. };
  9828. auto st32 = [] (std::initializer_list<int> v) {
  9829. std::vector<std::uint8_t> b;
  9830. for (int x : v)
  9831. for (int k = 0; k < 4; ++k)
  9832. b.push_back ((std::uint8_t) ((unsigned) x >> (8 * k)));
  9833. return b;
  9834. };
  9835. // PanOMatic: the engine started as LYNX starts it (one silent frame, then FL's flush), and from silence (fade-in)
  9836. auto pom = [&] (lynx::flbasic::PanOMaticState s, bool prime, std::initializer_list<Gold> gold) {
  9837. lynx::flbasic::PanOMaticEngine e;
  9838. e.prepare (44100);
  9839. e.set (s);
  9840. if (prime)
  9841. e.prime();
  9842. std::vector<float> l (inL), r (inR);
  9843. for (int i = 0; i < 4096; i += 512)
  9844. e.process (l.data() + i, r.data() + i, 512);
  9845. check (l, r, gold);
  9846. };
  9847. pom ({ 40, 300, 2, 1, 128, 50000 }, true, { { 100, 0.0f, -1.44884729f }, { 1000, -1.78870034f, 0.778897762f }, { 2047, 0.697593153f, -1.26396084f },
  9848. { 3000, -1.78870034f, -1.32851648f }, { 4095, 0.268305063f, 0.851258814f } });
  9849. pom ({ 200, 180, 1, 2, 100, 30000 }, true, { { 100, 0.0f, -0.347919852f }, { 1000, -0.322590947f, 0.0788071603f }, { 2047, 0.126614109f, -0.26421836f },
  9850. { 3000, -0.326532423f, -0.435213327f }, { 4095, 0.0493027233f, 0.226084977f } });
  9851. pom ({ 90, 256, 0, 1, 77, 40000 }, false, { { 100, -0.0292895138f, -0.110000029f }, { 1000, -1.31911683f, 0.666457176f }, { 2047, 0.477211952f, -0.864247262f },
  9852. { 3000, -1.59167564f, -0.591367304f }, { 4095, 0.268362761f, 0.537029684f } });
  9853. // Balance: flushed and from silence
  9854. auto bal = [&] (lynx::flbasic::BalanceState s, bool flush, std::initializer_list<Gold> gold) {
  9855. lynx::flbasic::BalanceEngine e;
  9856. e.prepare (44100);
  9857. e.set (s);
  9858. if (flush)
  9859. e.flush();
  9860. std::vector<float> l (inL), r (inR);
  9861. for (int i = 0; i < 4096; i += 512)
  9862. e.process (l.data() + i, r.data() + i, 512);
  9863. check (l, r, gold);
  9864. };
  9865. bal ({ 77, 100 }, true, { { 100, 0.0f, -0.136579692f }, { 1000, -0.0669578835f, -0.0282340273f }, { 2047, 0.0261135735f, -0.0795038193f },
  9866. { 3000, -0.0669578835f, -0.226895392f }, { 4095, 0.0100436825f, 0.0954951793f } });
  9867. bal ({ -128, 320 }, false, { { 100, -0.28914085f, 0.0f }, { 1000, -1.03179514f, 0.0f }, { 2047, -0.578702569f, 0.0f },
  9868. { 3000, -3.18510628f, 0.0f }, { 4095, 1.14394665f, 0.0f } });
  9869. // Blood Overdrive: rate first, then the state (its smoothers settle for one second of samples)
  9870. auto od = [&] (std::array<int, 6> raw, std::initializer_list<Gold> gold) {
  9871. lynx::flbasic::BloodOverdriveEngine e;
  9872. e.start (44100, { raw });
  9873. std::vector<float> l (inL), r (inR);
  9874. for (int i = 0; i < 4096; i += 512)
  9875. e.process (l.data() + i, r.data() + i, 512);
  9876. check (l, r, gold);
  9877. };
  9878. od ({ 3000, 7000, 5000, 1, 9000, 5000 }, { { 100, -0.0442250744f, 0.170355186f }, { 1000, 0.0470311977f, 0.0765217245f },
  9879. { 2047, 0.0261161141f, -0.0341798998f }, { 3000, 0.0470311977f, 0.0213619489f },
  9880. { 4095, 0.0402069278f, 0.0499244779f } });
  9881. // the mixer effects from the FL import: controls as parameters; host blocks of any size
  9882. auto viaImport = [&] (const char* plugin, const std::vector<std::uint8_t>& st, lynx::EffectKind kind, std::initializer_list<Gold> gold) {
  9883. const auto slot = lynx::effectFromFl (plugin, st, 120.0);
  9884. CHECK (slot && slot->kind == kind);
  9885. if (! slot)
  9886. return;
  9887. auto fx = lynx::makeEffect (*slot);
  9888. fx->prepare (44100.0);
  9889. std::vector<float> l (inL), r (inR);
  9890. fx->process (l.data(), r.data(), 1024, slot->params, 120.0);
  9891. fx->process (l.data() + 1024, r.data() + 1024, 1024, slot->params, 120.0);
  9892. fx->process (l.data() + 2048, r.data() + 2048, 2048, slot->params, 120.0);
  9893. check (l, r, gold);
  9894. };
  9895. viaImport ("Fruity PanOMatic", st32 ({ 40, 300, 2, 1, 128, 50000 }), lynx::EffectKind::fruityPanOMatic,
  9896. { { 1000, -1.78870034f, 0.778897762f }, { 4095, 0.268305063f, 0.851258814f } });
  9897. viaImport ("Fruity Balance", st32 ({ 0, 256 }), lynx::EffectKind::fruityBalance,
  9898. { { 100, 0.0f, -1.10000002f }, { 1000, -1.66666663f, 0.899999976f }, { 4095, 0.25f, 0.600000024f } });
  9899. viaImport ("Fruity Blood Overdrive", st32 ({ 3, 0, 5000, 2578, 1, 0, 2911, 0, 0 }), lynx::EffectKind::fruityBloodOverdrive,
  9900. { { 100, -0.122698173f, 0.0206716377f }, { 2047, 0.0128381588f, -0.0274834726f }, { 4095, -0.0899387598f, -0.121109024f } });
  9901. {
  9902. const auto slot = lynx::effectFromFl ("Fruity PanOMatic", st32 ({ 119, 256, 1, 2, 64, 27500 }), 120.0);
  9903. CHECK (slot && lynx::flbasic::panOMaticFromParams (slot->params.data()).pan == 119
  9904. && lynx::flbasic::panOMaticFromParams (slot->params.data()).target == 2);
  9905. if (slot)
  9906. {
  9907. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityPanOMatic, 2, slot->params[2], 120.0) == "Triangle");
  9908. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityPanOMatic, 1, slot->params[1], 120.0) == "+0.0 dB");
  9909. }
  9910. }
  9911. // unreadable states keep the old approximations
  9912. CHECK (lynx::effectFromFl ("Fruity PanOMatic", { 1, 2, 3 }, 120.0)->kind == lynx::EffectKind::utility);
  9913. CHECK (lynx::effectFromFl ("Fruity Balance", { 1, 2, 3 }, 120.0)->kind == lynx::EffectKind::utility);
  9914. CHECK (lynx::effectFromFl ("Fruity Blood Overdrive", { 1, 2, 3 }, 120.0)->kind == lynx::EffectKind::distortion);
  9915. }
  9916. TEST (fruity_fast_lp_free_filter_filter)
  9917. {
  9918. // REA_rodada63: goldens from the installation's DLLs (EXEC) on our own states, 44.1 kHz, blocks of 512 (the wrapper
  9919. // splits the host blocks below into calls of 512, as the DLL ran)
  9920. const lynx::ScopedFlushDenormals ftz;
  9921. std::vector<float> inL (4096), inR (4096);
  9922. for (int n = 0; n < 4096; ++n)
  9923. {
  9924. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  9925. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  9926. }
  9927. struct Gold { int i; float l, r; };
  9928. auto state = [] (const char* hex) {
  9929. std::vector<std::uint8_t> b;
  9930. for (const char* p = hex; p[0] && p[1]; p += 2)
  9931. b.push_back ((std::uint8_t) std::strtol (std::string (p, 2).c_str(), nullptr, 16));
  9932. return b;
  9933. };
  9934. auto viaImport = [&] (const char* plugin, const char* hex, lynx::EffectKind kind, std::initializer_list<Gold> gold) {
  9935. const auto slot = lynx::effectFromFl (plugin, state (hex), 120.0);
  9936. CHECK (slot && slot->kind == kind);
  9937. if (! slot)
  9938. return;
  9939. auto fx = lynx::makeEffect (*slot);
  9940. fx->prepare (44100.0);
  9941. std::vector<float> l (inL), r (inR);
  9942. fx->process (l.data(), r.data(), 1024, slot->params, 120.0);
  9943. fx->process (l.data() + 1024, r.data() + 1024, 1024, slot->params, 120.0);
  9944. fx->process (l.data() + 2048, r.data() + 2048, 2048, slot->params, 120.0);
  9945. for (const auto& g : gold)
  9946. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  9947. };
  9948. using K = lynx::EffectKind;
  9949. viaImport ("Fruity Fast LP", "01000000e0010000bc020000", K::fruityFastLP,
  9950. { { 100, -2.876206159591675f, 1.5413906574249268f }, { 1000, 0.12972253561019897f, 0.02936629205942154f },
  9951. { 2047, -0.2262123078107834f, -0.15169914066791534f }, { 3000, 0.12972258031368256f, -0.0798468142747879f },
  9952. { 4095, 0.0011161785805597901f, -0.08101501315832138f } });
  9953. viaImport ("Fruity Fast LP", "01000000890000008a000000", K::fruityFastLP,
  9954. { { 100, -0.7748895883560181f, -0.2310182899236679f }, { 1000, -0.01256702933460474f, -0.0086913350969553f },
  9955. { 2047, -0.010225297883152962f, -0.014700239524245262f }, { 3000, -0.012712989933788776f, -0.008916046470403671f },
  9956. { 4095, -0.008918046951293945f, -0.020280027762055397f } });
  9957. viaImport ("Fruity Free Filter", "02000000c2010000200300002c0100008403000001", K::fruityFreeFilter, // low shelf, CENTER
  9958. { { 100, -0.6178444623947144f, -0.13762974739074707f }, { 1000, -1.6557703018188477f, 0.8969771862030029f },
  9959. { 2047, 0.6539966464042664f, -0.21659433841705322f }, { 3000, -1.6404345035552979f, 0.4286947548389435f },
  9960. { 4095, -1.2272151708602905f, -0.5219485759735107f } });
  9961. viaImport ("Fruity Free Filter", "020000008a02000058020000bc020000c800000000", K::fruityFreeFilter, // high shelf
  9962. { { 100, -0.06996861100196838f, -0.11112016439437866f }, { 1000, -0.47636136412620544f, 0.31258222460746765f },
  9963. { 2047, 0.1628853678703308f, -0.3169582486152649f }, { 3000, -0.47636136412620544f, -0.22028093039989471f },
  9964. { 4095, -0.0975298061966896f, 0.05710025131702423f } });
  9965. viaImport ("Fruity Free Filter", "020000006400000000040000a9000000a402000001", K::fruityFreeFilter, // low pass 16 kHz
  9966. { { 100, 1.3032524585723877f, -0.007953938096761703f }, { 1000, 0.18662987649440765f, 2.155099391937256f },
  9967. { 2047, 0.5824014544487f, 0.05704556778073311f }, { 3000, 0.18981599807739258f, -0.6362011432647705f },
  9968. { 4095, 1.2624475955963135f, 2.497816324234009f } });
  9969. viaImport ("Fruity Filter", "020000004b01000076000000000400002c010000580200000100000001", K::fruityFilter, // x2, CENTER
  9970. { { 100, 0.2685631811618805f, 0.16885951161384583f }, { 1000, -0.20637311041355133f, 0.13557639718055725f },
  9971. { 2047, 0.07038643211126328f, -0.15532609820365906f }, { 3000, -0.20311756432056427f, -0.09958365559577942f },
  9972. { 4095, 0.08275680989027023f, 0.010792487300932407f } });
  9973. viaImport ("Fruity Filter", "01000000bc020000f401000000000000000400000000000000000000", K::fruityFilter, // band pass
  9974. { { 100, -0.4368843734264374f, -0.12492841482162476f }, { 1000, -0.3362128436565399f, -0.09686881303787231f },
  9975. { 2047, 0.21920710802078247f, 0.3213832974433899f }, { 3000, -0.3362128436565399f, 0.4016203284263611f },
  9976. { 4095, -0.14230486750602722f, -0.22842440009117126f } });
  9977. // an old Free Filter layout (type·10) is converted to the current scale; parameter texts
  9978. {
  9979. const auto slot = lynx::effectFromFl ("Fruity Free Filter", state ("010000003c00000000020000a9000000a4020000"), 120.0);
  9980. CHECK (slot && slot->kind == K::fruityFreeFilter);
  9981. if (slot)
  9982. {
  9983. CHECK (lynx::flfilter::freeFilterFromParams (slot->params.data()).type == 100 + (int) std::nearbyint (5.5 / 7.0 * 600.0));
  9984. CHECK (lynx::formatEffectParam (K::fruityFreeFilter, 0, slot->params[0], 120.0) == "Peaking");
  9985. }
  9986. CHECK (lynx::formatEffectParam (K::fruityFreeFilter, 1, 1.0f, 120.0) == "16000 Hz");
  9987. CHECK (lynx::formatEffectParam (K::fruityFilter, 0, 1.0f, 120.0) == "7350 Hz");
  9988. }
  9989. // unreadable states keep the old approximations
  9990. CHECK (lynx::effectFromFl ("Fruity Fast LP", { 1, 2, 3 }, 120.0)->kind == K::filter);
  9991. CHECK (lynx::effectFromFl ("Fruity Free Filter", { 1, 2, 3 }, 120.0)->kind == K::filter);
  9992. CHECK (lynx::effectFromFl ("Fruity Filter", { 9, 0, 0, 0 }, 120.0)->kind == K::filter);
  9993. }
  9994. // ---- Sytrus (REA_rodada31) -------------------------------------------------------------------------------------------
  9995. // A Sytrus state built from scratch (version 25 layout, our own values — no FL preset data), zlib "stored" blocks.
  9996. namespace
  9997. {
  9998. struct SytrusTestPatch
  9999. {
  10000. std::vector<std::int32_t> main = std::vector<std::int32_t> (240, 0); // 962-byte main block as ints (+ mute bytes)
  10001. struct Op
  10002. {
  10003. int p[10] { 0, 256, 0, 0, 0, 0, 0, 0, 0, 0 };
  10004. int ratio = 20000, offset = 0, noise = 0;
  10005. std::vector<float> amps { 1.0f };
  10006. std::uint8_t flags[8] {};
  10007. int env1[3] { -1, -1, -1 }; // volume envelope: on (≠ −1 → on), decay point, sustain point
  10008. std::vector<std::pair<double, double>> env1pts;
  10009. } ops[6];
  10010. struct Filt
  10011. {
  10012. int p[10] { 0, 0, 128, 0, 128, 0, 0, 128, 128, 0 };
  10013. int type = 3;
  10014. } filts[3];
  10015. std::array<std::array<std::uint8_t, 23>, 3> delays {};
  10016. std::array<std::int32_t, 9> reverb { 19, 221, 0, 50, 80, 30, 221, 2, 0 };
  10017. bool reverbOn = false;
  10018. int eqmode = 0;
  10019. SytrusTestPatch()
  10020. {
  10021. main[0] = 128, main[1] = 128;
  10022. for (int i = 0; i < 8; ++i)
  10023. main[(std::size_t) (165 + i)] = (i % 4 == 2) ? 0 : 128;
  10024. main[173] = 1, main[175] = 128, main[188] = 128, main[189] = -1, main[190] = main[191] = 256;
  10025. }
  10026. void cell (int layer, int row, int col, int v) { main[(std::size_t) (3 + layer * 81 + row * 9 + col)] = v; }
  10027. std::vector<std::uint8_t> blob() const
  10028. {
  10029. std::vector<std::uint8_t> b;
  10030. 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))); };
  10031. auto f32 = [&] (float v) { std::int32_t x; std::memcpy (&x, &v, 4); i32 (x); };
  10032. 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))); };
  10033. // point lists: kind 1 envelope, 2 LFO, 3 map / shaper curve; format version 3
  10034. auto env = [&] (int kind, const std::vector<std::pair<double, double>>& pts, int on, int decay, int sus) {
  10035. i32 (kind), i32 (3), i32 ((std::int32_t) pts.size());
  10036. for (const auto& [dt, y] : pts)
  10037. f64 (dt), f64 (y), f32 (0.0f), i32 (0);
  10038. i32 (0), i32 (on), i32 (decay), i32 (-1), i32 (sus);
  10039. };
  10040. for (int i = 0; i < 240; ++i)
  10041. if (4 * i + 4 <= 0x320)
  10042. i32 (main[(std::size_t) i]);
  10043. b.resize (962, 0); // mute bytes: none muted
  10044. i32 (0); // global declick amount
  10045. b.push_back (0), b.push_back (0), b.push_back (0); // OSC grouping, DC stage, GIBBS OFF
  10046. for (int k = 0; k < 9; ++k)
  10047. b.push_back (k == 7 ? 1 : 0); // flag group (release multiplier on, as Default.fst)
  10048. for (const auto& d : delays)
  10049. b.insert (b.end(), d.begin(), d.end());
  10050. for (auto v : reverb)
  10051. i32 (v);
  10052. b.push_back (reverbOn ? 1 : 0), b.push_back (0);
  10053. i32 (eqmode);
  10054. for (int k = 0; k < 3; ++k)
  10055. i32 (0), i32 (30000), i32 (20000), i32 (k);
  10056. b.push_back (0); // no info text
  10057. for (int u = 0; u < 10; ++u)
  10058. {
  10059. const int narts = u < 6 ? 6 : (u < 9 ? 8 : 2);
  10060. for (int j = 0; j < 10; ++j)
  10061. i32 (u < 6 ? ops[u].p[j] : (u < 9 ? filts[u - 6].p[j] : 0));
  10062. if (u < 6)
  10063. {
  10064. const auto& o = ops[u];
  10065. const auto at = b.size();
  10066. b.resize (at + 0x828, 0);
  10067. auto put = [&] (std::size_t off, const void* src, std::size_t n) { std::memcpy (b.data() + at + off, src, n); };
  10068. put (0, &o.ratio, 4), put (4, &o.offset, 4), put (8, &o.noise, 4);
  10069. put (0xc, o.amps.data(), 4 * o.amps.size());
  10070. const std::int32_t shape[5] { 0, 0, 0, 0, 128 };
  10071. put (0x80c, shape, sizeof shape);
  10072. put (0x820, o.flags, 8);
  10073. }
  10074. else if (u < 9)
  10075. {
  10076. const auto at = b.size();
  10077. b.resize (at + 0x30, 0);
  10078. std::memcpy (b.data() + at + 0x2c, &filts[u - 6].type, 4);
  10079. env (3, { { 0.0, 0.0 }, { 1.0, 1.0 } }, 0, -1, -1);
  10080. b.push_back (0);
  10081. }
  10082. for (int k = 0; k < narts; ++k)
  10083. {
  10084. const bool add = k == 0 || k == 3 || (u >= 6 && u <= 8 && (k == 2 || k == 3)) || (u < 6 && k == 4);
  10085. const double neutral = add ? 0.5 : 1.0;
  10086. i32 (3), i32 (0), f32 (1.0f), f32 (1.0f);
  10087. if (u < 6 && k == 1 && ops[u].env1[0] != -1)
  10088. env (1, ops[u].env1pts, -1, ops[u].env1[1], ops[u].env1[2]);
  10089. else
  10090. env (1, { { 0.0, 0.0 }, { 1.0, 1.0 } }, 0, -1, -1);
  10091. i32 (128), i32 (128), i32 (0), i32 (128);
  10092. env (2, { { 0.0, 1.0 }, { 1.0, 1.0 } }, 0, -1, -1);
  10093. i32 (40000), i32 (0), i32 (0), i32 (0), i32 (0);
  10094. for (int m = 0; m < 6; ++m)
  10095. env (3, { { 0.0, neutral }, { 1.0, neutral } }, 0, -1, -1);
  10096. }
  10097. }
  10098. b.push_back (0), b.push_back (0);
  10099. // zlib with stored blocks
  10100. std::vector<std::uint8_t> z { 0x78, 0x01 };
  10101. for (std::size_t at = 0; at < b.size(); at += 65535)
  10102. {
  10103. const auto n = std::min<std::size_t> (65535, b.size() - at);
  10104. z.push_back (at + n >= b.size() ? 1 : 0);
  10105. z.push_back ((std::uint8_t) n), z.push_back ((std::uint8_t) (n >> 8));
  10106. z.push_back ((std::uint8_t) ~n), z.push_back ((std::uint8_t) (~n >> 8));
  10107. z.insert (z.end(), b.begin() + (std::ptrdiff_t) at, b.begin() + (std::ptrdiff_t) (at + n));
  10108. }
  10109. std::uint32_t a = 1, c = 0;
  10110. for (auto x : b)
  10111. a = (a + x) % 65521, c = (c + a) % 65521;
  10112. for (int k = 3; k >= 0; --k)
  10113. z.push_back ((std::uint8_t) (((c << 16) | a) >> (8 * k)));
  10114. std::vector<std::uint8_t> out { 25, 0, 0, 0 };
  10115. out.insert (out.end(), z.begin(), z.end());
  10116. return out;
  10117. }
  10118. };
  10119. // renders like FL's Gen_Render calls: notes start/release at the block that contains them
  10120. std::pair<std::vector<float>, std::vector<float>> sytrusRender (const std::vector<std::uint8_t>& blob, bool renderMode, int n,
  10121. std::vector<std::array<int, 3>> notes, double fs = 44100.0)
  10122. {
  10123. lynx::sytrus::Engine e;
  10124. e.setTempo (120.0);
  10125. e.prepare (fs, renderMode, 512);
  10126. e.setPatch (lynx::sytrus::parsePatch (blob));
  10127. std::vector<float> l ((std::size_t) n), r ((std::size_t) n);
  10128. for (int n0 = 0; n0 < n; n0 += 512)
  10129. {
  10130. for (std::size_t k = 0; k < notes.size(); ++k)
  10131. {
  10132. if (notes[k][1] >= n0 && notes[k][1] < n0 + 512)
  10133. {
  10134. lynx::sytrus::NoteParams p;
  10135. p.key = (float) notes[k][0];
  10136. p.id = (std::int64_t) k + 1;
  10137. e.noteOn (p);
  10138. }
  10139. if (notes[k][2] >= n0 && notes[k][2] < n0 + 512)
  10140. e.release ((std::int64_t) k + 1);
  10141. }
  10142. e.render (l.data() + n0, r.data() + n0, std::min (512, n - n0), 1.0f, 0.0f);
  10143. }
  10144. return { l, r };
  10145. }
  10146. } // namespace
  10147. TEST (sytrus_engine_sine)
  10148. {
  10149. // one sine operator, ratio 1, OUT 256, no envelopes: C5 = 523.2511 Hz at 0.7071 (centre of the pan law, §6.1)
  10150. SytrusTestPatch t;
  10151. t.cell (0, 0, 8, 256);
  10152. const auto blob = t.blob();
  10153. CHECK (lynx::sytrus::stateUsable (blob));
  10154. CHECK (lynx::sytrus::parsePatch (blob) != nullptr);
  10155. auto broken = blob;
  10156. broken.resize (broken.size() / 2);
  10157. CHECK (! lynx::sytrus::stateUsable (broken));
  10158. const auto [l, r] = sytrusRender (blob, false, 8192, { { 60, 0, 4096 } });
  10159. double worst = 0.0;
  10160. for (int k = 0; k < 4000; ++k)
  10161. 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)));
  10162. CHECK (worst < 2e-5);
  10163. CHECK (l == r);
  10164. // 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
  10165. // 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)
  10166. int last = 0;
  10167. for (int k = 0; k < 8192; ++k)
  10168. if (l[(std::size_t) k] != 0.0f)
  10169. last = k;
  10170. CHECK (last == 4895);
  10171. // key 69 = 880 Hz; 48 kHz
  10172. const auto [l2, r2] = sytrusRender (blob, false, 4096, { { 69, 0, -1 } }, 48000.0);
  10173. worst = 0.0;
  10174. for (int k = 0; k < 4000; ++k)
  10175. 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)));
  10176. CHECK (worst < 5e-5);
  10177. (void) r2;
  10178. }
  10179. TEST (sytrus_engine_reference)
  10180. {
  10181. // FM 2→1, Vanilla LP filter, volume envelope, unison 3, delay and reverb, 2× oversampling with the 8-sample
  10182. // envelope tick (render settings): against our Python reference model of REA_rodada31 (sytmodel.render)
  10183. SytrusTestPatch t;
  10184. t.main[195] = 0, t.main[196] = 1, t.main[193] = 0, t.main[194] = 1;
  10185. t.main[173] = 3, t.main[174] = 64, t.main[176] = 100;
  10186. t.ops[1].ratio = 40000;
  10187. t.cell (0, 0, 1, 128); // op 2 → op 1 FM
  10188. t.cell (0, 0, 8, 128);
  10189. t.cell (0, 6, 0, 256); // op 1 → filter 1
  10190. t.cell (0, 6, 8, 256);
  10191. t.cell (0, 0, 7, 200); // FX send
  10192. t.filts[0].p[0] = 1, t.filts[0].p[2] = 180, t.filts[0].p[3] = 60;
  10193. t.ops[0].env1[0] = 0, t.ops[0].env1[1] = 1, t.ops[0].env1[2] = 2;
  10194. t.ops[0].env1pts = { { 0.0, 0.0 }, { 0.05, 1.0 }, { 0.1, 0.6 }, { 0.2, 0.0 } };
  10195. const std::int32_t d0[5] { 40, 100, 50, 100, 0 };
  10196. std::memcpy (t.delays[0].data(), d0, 20);
  10197. t.delays[0][22] = 1;
  10198. t.reverb = { 19, 221, 10, 50, 80, 30, 100, 2, 100 };
  10199. t.reverbOn = true;
  10200. const auto blob = t.blob();
  10201. const auto [l, r] = sytrusRender (blob, true, 12000, { { 60, 0, 6000 } });
  10202. // reference values (model render_mode=True, 44.1 kHz, block 512, key 60, velocity 0.78125, release at 6000); the same
  10203. // state rendered by FL's own Sytrus DLL in render mode differs from this by −136 dB (EXEC, REA round 31 harness)
  10204. const std::array<std::pair<int, float>, 8> golden { { { 100, -0.00235529384f }, { 777, -0.175126165f }, { 2000, 0.30976975f }, { 4321, -0.0213534199f },
  10205. { 6100, 0.202372357f }, { 7000, 0.249530748f }, { 9000, 0.0307101421f }, { 11999, 0.176390275f } } };
  10206. for (const auto& [i, v] : golden)
  10207. CHECK (std::abs (l[(std::size_t) i] - v) < 1e-6f);
  10208. const std::array<std::pair<int, float>, 3> goldenR { { { 777, -0.166890964f }, { 4321, -0.212358952f }, { 9000, -0.0251909085f } } };
  10209. for (const auto& [i, v] : goldenR)
  10210. CHECK (std::abs (r[(std::size_t) i] - v) < 1e-6f);
  10211. }
  10212. TEST (sytrus_instrument)
  10213. {
  10214. // the LYNX instrument: state in ChannelDef::pluginState, channel volume/pan recovered from the rack gains
  10215. SytrusTestPatch t;
  10216. t.cell (0, 0, 8, 256);
  10217. lynx::ChannelDef c;
  10218. c.instrument = lynx::InstrumentKind::sytrus;
  10219. c.pluginState = t.blob();
  10220. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sytrus);
  10221. inst->prepare (44100.0);
  10222. inst->configure (c, 120.0);
  10223. CHECK (inst->idle());
  10224. std::vector<float> l (4096), r (4096);
  10225. inst->noteOn (60, 100.0f / 128.0f);
  10226. // 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
  10227. inst->render (l.data(), r.data(), 4096, 0.5f, 0.5f);
  10228. double peak = 0.0;
  10229. for (int k = 1000; k < 4096; ++k)
  10230. peak = std::max (peak, (double) std::abs (l[(std::size_t) k]));
  10231. CHECK (std::abs (peak - 0.5 * 0.70710678) < 1e-3); // Final.Vol 0.5 × the plugin's centre pan law
  10232. CHECK (! inst->idle());
  10233. inst->noteOff (60);
  10234. std::vector<float> tail (44100 * 3), tailR (tail.size());
  10235. inst->render (tail.data(), tailR.data(), (int) tail.size(), 0.5f, 0.5f);
  10236. CHECK (inst->idle());
  10237. CHECK (tail.back() == 0.0f);
  10238. }
  10239. TEST (harmor_image)
  10240. {
  10241. // REA_rodada75: image synthesis — a looping 6-column image (gain + frequency planes) read at a speed, cubic gain and
  10242. // linear frequency interpolation, Hz frequency mode, formant shift — against our reference model (r75 imgm.py)
  10243. const auto blob = harmorTestState (false, true);
  10244. if (const char* dump = std::getenv ("LYNX_DUMP_HARMOR_IMAGE"))
  10245. std::ofstream (dump, std::ios::binary).write ((const char*) blob.data(), (std::streamsize) blob.size());
  10246. CHECK (lynx::harmorStateUsable (blob));
  10247. CHECK (lynx::harmorFallbacks (blob).empty()); // (the image is modelled)
  10248. 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);
  10249. CHECK (ref.size() == 2 * 8192);
  10250. if (std::getenv ("LYNX_PRINT_HARMOR_IMAGE") != nullptr)
  10251. for (int i : { 100, 1000, 2500, 3000, 5000, 6500, 8191 })
  10252. std::printf ("harmor image %d %.8g %.8g\n", i, (double) ref[2 * (std::size_t) i], (double) ref[2 * (std::size_t) i + 1]);
  10253. const std::tuple<int, float, float> golden[] {
  10254. { 100, 0.0022269291f, 0.018017211f }, { 1000, 0.013962966f, -0.028602127f }, { 2500, 0.016781429f, -0.028931717f },
  10255. { 3000, -0.010532441f, -0.0061010128f }, { 5000, -0.017426063f, 0.016474312f }, { 6500, 0.1689423f, 0.078417062f },
  10256. { 8191, -0.045403875f, -0.049045475f } };
  10257. for (const auto& [i, gl, gr] : golden)
  10258. CHECK (std::abs (ref[2 * (std::size_t) i] - gl) < 2e-6f && std::abs (ref[2 * (std::size_t) i + 1] - gr) < 2e-6f);
  10259. // the image changes the sound: without it (gain mix 0, frequency scale 0) the same notes render differently
  10260. auto plain = blob;
  10261. const std::int32_t zero = 0;
  10262. std::memcpy (plain.data() + lynx::harmorParamOffset (plain, 0, 89), &zero, 4);
  10263. std::memcpy (plain.data() + lynx::harmorParamOffset (plain, 0, 92), &zero, 4);
  10264. std::memcpy (plain.data() + lynx::harmorParamOffset (plain, 0, 94), &zero, 4);
  10265. 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);
  10266. double diff = 0.0, energy = 0.0;
  10267. for (std::size_t i = 0; i < ref.size(); ++i)
  10268. diff += (double) (ref[i] - ref0[i]) * (ref[i] - ref0[i]), energy += (double) ref[i] * ref[i];
  10269. CHECK (energy > 1.0 && diff > 0.01 * energy);
  10270. // a resynthesis-mode image (a sample analysed at load time) is not modelled: reported, the part plays its timbres
  10271. auto resyn = blob;
  10272. {
  10273. // the image object follows the 26 curves: find its header (version 0, mode 0, loop 1) from the end of part A
  10274. 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 };
  10275. const auto it = std::search (resyn.begin(), resyn.end(), std::begin (hdr), std::end (hdr));
  10276. CHECK (it != resyn.end());
  10277. if (it != resyn.end())
  10278. *(it + 4) = 1;
  10279. }
  10280. const auto fb = lynx::harmorFallbacks (resyn);
  10281. CHECK (std::any_of (fb.begin(), fb.end(), [] (const std::string& f) { return f.find ("resynthesis") != std::string::npos; }));
  10282. }
  10283. TEST (harmor_round75_features)
  10284. {
  10285. // REA_rodada75: arpeggio markers, noise resonance, pluck blur mode, pluck blend, harmonic randomness, phaser and
  10286. // harmonizer masks — LYNX's engine against our reference model (r75 hmodel.py, the Mersenne Twister seeded like LYNX's)
  10287. 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 } };
  10288. struct Case { int features; std::array<std::tuple<int, float, float>, 4> golden; };
  10289. const Case cases[] {
  10290. { 1, { { { 2000, 0.00326227601f, -0.00143631078f }, { 4500, 0.104633873f, 0.0262026641f }, { 7000, -0.00242280135f, 0.00364600495f }, { 11000, -0.0029983636f, -0.00308720154f } } } },
  10291. { 2, { { { 2000, 0.294110536f, -0.229805974f }, { 4500, -0.00492941859f, -0.00132748827f }, { 7000, 0.0132401136f, 0.00544014934f }, { 11000, -0.00105760543f, -0.00210882883f } } } },
  10292. { 4, { { { 2000, 0.0720629904f, -0.0296807697f }, { 4500, -0.10764533f, 0.113311439f }, { 7000, -0.229827746f, -0.101087983f }, { 11000, -0.0519010639f, -0.0665841137f } } } },
  10293. { 8, { { { 2000, 0.0720039901f, -0.0297230242f }, { 4500, -0.107470558f, 0.113653905f }, { 7000, -0.228478677f, -0.152556434f }, { 11000, -0.0412466244f, -0.0504480202f } } } },
  10294. { 16, { { { 2000, 0.0634379551f, -0.0309438618f }, { 4500, -0.112099687f, 0.112784195f }, { 7000, -0.231666087f, -0.167064955f }, { 11000, -0.0411841346f, -0.0503804788f } } } },
  10295. { 32, { { { 2000, 0.0862953135f, -0.0358822417f }, { 4500, 0.0694009664f, 0.141198453f }, { 7000, -0.158969622f, -0.103794064f }, { 11000, -0.0294273162f, -0.0417808629f } } } },
  10296. { 63, { { { 2000, 0.0172010472f, -0.0106831535f }, { 4500, 0.556600683f, 0.0811084217f }, { 7000, -0.00255421684f, 0.020610704f }, { 11000, -0.00318711501f, 0.000247763831f } } } } };
  10297. for (const auto& c : cases)
  10298. {
  10299. const auto blob = harmorTestState (false, false, c.features);
  10300. if (const char* dump = std::getenv ("LYNX_DUMP_HARMOR_R75"))
  10301. std::ofstream (std::string (dump) + std::to_string (c.features) + ".bin", std::ios::binary).write ((const char*) blob.data(), (std::streamsize) blob.size());
  10302. CHECK (lynx::harmorStateUsable (blob));
  10303. CHECK (lynx::harmorFallbacks (blob).empty());
  10304. const auto ref = lynx::harmor::renderReference (blob, chord, 44100.0, 12000);
  10305. if (std::getenv ("LYNX_PRINT_HARMOR_R75") != nullptr)
  10306. for (int i : { 2000, 4500, 7000, 11000 })
  10307. 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]);
  10308. for (const auto& [i, gl, gr] : c.golden)
  10309. CHECK (std::abs (ref[2 * (std::size_t) i] - gl) < 2e-6f && std::abs (ref[2 * (std::size_t) i + 1] - gr) < 2e-6f);
  10310. }
  10311. }
  10312. TEST (sytrus_main_settings)
  10313. {
  10314. // the channel settings panel's controls (REA_rodada74 §3): FL's "Main settings" ints inside the zlib state
  10315. SytrusTestPatch t;
  10316. t.cell (0, 0, 8, 256);
  10317. t.main[173] = 3;
  10318. const auto blob = t.blob();
  10319. const auto& S = lynx::sytrus::settings();
  10320. auto find = [&] (const char* name) {
  10321. for (int i = 0; i < (int) S.size(); ++i)
  10322. if (std::string (S[(std::size_t) i].name) == name)
  10323. return i;
  10324. return -1;
  10325. };
  10326. const int master = find ("Master level"), unison = find ("Unison"), porta = find ("Porta"), live = find ("Live oversampling");
  10327. CHECK (master >= 0 && unison >= 0 && porta >= 0 && live >= 0 && S.size() == 25u);
  10328. CHECK (lynx::sytrus::settingValue (blob, master) == 128 && lynx::sytrus::settingValue (blob, unison) == 3);
  10329. CHECK (lynx::sytrus::settingValue (blob, porta) == 0 && lynx::sytrus::settingValue (blob, find ("Mod X")) == 256);
  10330. CHECK (! lynx::sytrus::settingValue (blob, -1) && ! lynx::sytrus::settingValue (blob, (int) S.size()));
  10331. auto broken = blob;
  10332. broken.resize (broken.size() / 2);
  10333. CHECK (! lynx::sytrus::settingValue (broken, master) && ! lynx::sytrus::setSetting (broken, master, 64));
  10334. // master level 64: gain (11^(64/128) − 1)/10 instead of 1 (REA_rodada11 §1.3); the state stays readable
  10335. auto quiet = blob;
  10336. CHECK (lynx::sytrus::setSetting (quiet, master, 64) && lynx::sytrus::stateUsable (quiet));
  10337. CHECK (lynx::sytrus::settingValue (quiet, master) == 64 && lynx::sytrus::settingValue (quiet, unison) == 3);
  10338. CHECK (quiet[0] == blob[0] && quiet[1] == blob[1]); // version word kept
  10339. const auto [l1, r1] = sytrusRender (blob, false, 4096, { { 60, 0, -1 } });
  10340. const auto [l2, r2] = sytrusRender (quiet, false, 4096, { { 60, 0, -1 } });
  10341. const auto g = (std::sqrt (11.0) - 1.0) / 10.0;
  10342. double worst = 0.0;
  10343. for (int k = 0; k < 4096; ++k)
  10344. worst = std::max (worst, std::abs ((double) l2[(std::size_t) k] - g * l1[(std::size_t) k]));
  10345. CHECK (worst < 1e-5);
  10346. (void) r1, (void) r2;
  10347. // clamped to the control's range; PORTA is the flag byte the engine reads
  10348. auto edited = blob;
  10349. CHECK (lynx::sytrus::setSetting (edited, unison, 42) && lynx::sytrus::settingValue (edited, unison) == 9);
  10350. CHECK (lynx::sytrus::setSetting (edited, porta, 1) && lynx::sytrus::settingValue (edited, porta) == 1);
  10351. CHECK (lynx::sytrus::setSetting (edited, live, 2) && lynx::sytrus::settingValue (edited, live) == 2);
  10352. CHECK (lynx::sytrus::parsePatch (edited) != nullptr);
  10353. }
  10354. TEST (harmor_effects_rack)
  10355. {
  10356. // REA_rodada38: the rack runs in the state's unit order and the records' FX send joins it at unit 4 — with the FX
  10357. // amount at 1000 every record output bypasses a reverb placed before unit 4 (EXEC: −600 dB vs the rack off)
  10358. auto blob = harmorTestState (false);
  10359. auto set = [&] (std::vector<std::uint8_t>& st, int part, int index, std::int32_t v) {
  10360. std::memcpy (st.data() + lynx::harmorParamOffset (st, part, index), &v, 4);
  10361. };
  10362. set (blob, 0, 10, 1000); // all to the FX send
  10363. auto render = [] (const std::vector<std::uint8_t>& st) {
  10364. return lynx::harmor::renderReference (st, { { 0, 60, 0.8, 1.0, 0.0, 6000 } }, 44100.0, 12000);
  10365. };
  10366. const auto dry = render (blob);
  10367. auto rev = blob;
  10368. set (rev, -1, 22, 1); // reverb on: room 50, diffusion 100, decay 2^3·0.1 s, high cut / damping off, wet 0.8
  10369. 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);
  10370. set (rev, -1, 30, 2), set (rev, -1, 31, 800);
  10371. const auto bypassed = render (rev);
  10372. double eb = 0.0, db = 0.0;
  10373. for (std::size_t i = 0; i < dry.size(); ++i)
  10374. eb += (double) (bypassed[i] - dry[i]) * (bypassed[i] - dry[i]), db += (double) dry[i] * dry[i];
  10375. CHECK (eb < 1e-12 * db && db > 1e-3); // (float rounding of the rack's buffers only)
  10376. auto after = rev;
  10377. const std::int32_t order[6] { 0, 1, 2, 4, 3, 5 }; // the join before the reverb
  10378. for (int k = 0; k < 6; ++k)
  10379. set (after, -1, 53 + k, order[k]);
  10380. const auto wet = render (after);
  10381. double e = 0.0, d = 0.0;
  10382. bool finite = true;
  10383. for (std::size_t i = 0; i < wet.size(); ++i)
  10384. e += (double) (wet[i] - dry[i]) * (wet[i] - dry[i]), d += (double) dry[i] * dry[i], finite = finite && std::isfinite (wet[i]);
  10385. CHECK (finite && e > 1e-4 * d && d > 1e-3);
  10386. // a distortion (type 6) and a delay on the dry path change the sound, stay finite, and the instrument ends quiet
  10387. auto dist = harmorTestState (false);
  10388. {
  10389. const auto at = lynx::harmorParamOffset (dist, -1, 0) + 0xd4 + 0x18 + 48; // global misc +48: distortion type
  10390. const std::int32_t six = 6;
  10391. std::memcpy (dist.data() + at, &six, 4);
  10392. }
  10393. set (dist, -1, 17, 600), set (dist, -1, 19, 1000), set (dist, -1, 21, 1100);
  10394. 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);
  10395. set (dist, -1, 47, 1050);
  10396. lynx::ChannelDef c;
  10397. c.instrument = lynx::InstrumentKind::harmor;
  10398. c.pluginState = dist;
  10399. auto inst = lynx::makeInstrument (lynx::InstrumentKind::harmor);
  10400. inst->configure (c, 120.0);
  10401. inst->prepare (44100.0);
  10402. std::vector<float> l (44100 * 4), r (l.size());
  10403. inst->noteOn (60, 0.8f);
  10404. double energy = 0.0;
  10405. for (int s = 0; s < (int) l.size(); s += 512)
  10406. {
  10407. if (s == 8192)
  10408. inst->noteOff (60);
  10409. inst->render (l.data() + s, r.data() + s, std::min (512, (int) l.size() - s), 1.0f, 1.0f);
  10410. }
  10411. for (std::size_t i = 0; i < l.size(); ++i)
  10412. finite = finite && std::isfinite (l[i]) && std::isfinite (r[i]), energy += (double) l[i] * l[i];
  10413. CHECK (finite && energy > 1.0);
  10414. std::vector<float> tail (44100 * 6), tailR (tail.size());
  10415. for (int s = 0; s < (int) tail.size(); s += 512)
  10416. inst->render (tail.data() + s, tailR.data() + s, std::min (512, (int) tail.size() - s), 1.0f, 1.0f);
  10417. CHECK (inst->idle());
  10418. }
  10419. TEST (harmless_engine)
  10420. {
  10421. // REA_rodada32: state parsing, the additive engine against our reference model (hlmodel.py), host levels, voice end
  10422. std::vector<std::uint8_t> blob;
  10423. 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))); };
  10424. std::array<std::int32_t, 100> p {}; // the reference's clean patch: Saw, open filter, no modulation
  10425. for (int i = 11; i < 23; ++i)
  10426. p[(std::size_t) i] = 1000;
  10427. 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;
  10428. 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;
  10429. // + resonance, pluck, 3-voice unison, tremolo, LFO on pitch
  10430. 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;
  10431. put (12);
  10432. for (auto v : p)
  10433. put (v);
  10434. for (int i = 0; i < 7; ++i)
  10435. put (0);
  10436. put (524); // Custom colour table: 524 × (amplitude, phase)
  10437. for (int i = 0; i < 1048; ++i)
  10438. {
  10439. const auto f = i < 524 ? 1.0f / (float) (i + 1) : 0.25f;
  10440. std::uint32_t bits = 0;
  10441. std::memcpy (&bits, &f, 4);
  10442. put ((std::int32_t) bits);
  10443. }
  10444. const auto parsed = lynx::harmlessParams (blob);
  10445. CHECK (parsed && (int) parsed->size() == lynx::harmlessParamCount);
  10446. CHECK (parsed && (*parsed)[54] == 3500.0f && (*parsed)[99] == 0.0f && (*parsed)[lynx::harmlessCustomFlagIndex] == 1.0f);
  10447. CHECK (parsed && (*parsed)[lynx::harmlessCustomIndex + 1] == 0.5f && (*parsed)[lynx::harmlessCustomIndex + 524] == 0.25f);
  10448. auto old = blob;
  10449. old[0] = 3; // versions below 4 are not Harmless states we know
  10450. CHECK (! lynx::harmlessParams (old));
  10451. old = blob;
  10452. old.resize (300);
  10453. CHECK (! lynx::harmlessParams (old));
  10454. lynx::ChannelDef c;
  10455. c.instrument = lynx::InstrumentKind::harmless;
  10456. c.modelParams = *parsed;
  10457. c.modelParams[lynx::harmlessCustomFlagIndex] = 0.0f; // colour 0 (Saw): the custom table is not read
  10458. auto render = [] (const lynx::ChannelDef& def, int total, float gl, float gr, int offAt) {
  10459. auto inst = lynx::makeInstrument (lynx::InstrumentKind::harmless);
  10460. inst->configure (def, 120.0);
  10461. inst->prepare (44100.0);
  10462. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  10463. inst->noteOn (60, 0.78125f);
  10464. for (int s = 0; s < total; s += 512)
  10465. {
  10466. if (s == offAt)
  10467. inst->noteOff (60);
  10468. inst->render (l.data() + s, r.data() + s, std::min (512, total - s), gl, gr);
  10469. }
  10470. return std::make_tuple (l, r, std::move (inst));
  10471. };
  10472. {
  10473. // reference model (REA round 32 hlmodel.py, C5 at FL velocity 100, channel volume 1, centre, released at 6144)
  10474. auto [l, r, inst] = render (c, 8192, 1.0f, 1.0f, 6144);
  10475. const std::tuple<int, float, float> golden[] {
  10476. { 0, 0.0f, 0.0f }, { 10, -0.02220994f, -0.06232700f }, { 100, 0.40623373f, 0.17308202f }, { 1000, -0.04091963f, -0.26821208f },
  10477. { 3000, 0.14519004f, 0.11027778f }, { 6000, 0.50411958f, 0.27901855f }, { 8191, 0.02117692f, -0.01466602f } };
  10478. for (const auto& [i, gl, gr] : golden)
  10479. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  10480. // after the release the record fades out (below 1e-4, then 30 ms) and the instrument goes idle
  10481. std::vector<float> tail (44100), tailR (tail.size());
  10482. inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
  10483. CHECK (inst->idle());
  10484. }
  10485. // FL's level law applied once: half the channel volume halves the output; full right pan silences the left side
  10486. const auto full = std::get<0> (render (c, 2048, 1.0f, 1.0f, -1));
  10487. const auto half = std::get<0> (render (c, 2048, 0.5f, 0.5f, -1));
  10488. CHECK (std::abs (half[100] - 0.5f * full[100]) < 1e-6f && std::abs (full[100]) > 0.1f);
  10489. auto mono = c;
  10490. mono.modelParams[25] = 0.0f; // no tremolo, no unison pan spread
  10491. mono.modelParams[65] = 1.0f;
  10492. const auto [pl, pr, pi] = render (mono, 2048, 0.0f, 1.41421356f, -1);
  10493. double el = 0.0, er = 0.0;
  10494. for (int i = 0; i < 2048; ++i)
  10495. el += (double) pl[(std::size_t) i] * pl[(std::size_t) i], er += (double) pr[(std::size_t) i] * pr[(std::size_t) i];
  10496. CHECK (el < 1e-10 * er && er > 1.0);
  10497. }
  10498. TEST (startup_sound)
  10499. {
  10500. // the splash's sound: the right length, finite, at its peak level, quiet before the whoosh swells,
  10501. // a landing "pop" at impactTime and a chime ringing after it
  10502. using namespace lynx::startup;
  10503. const auto sound = makeSound (48000.0);
  10504. CHECK (sound.left.size() == (std::size_t) (soundLength * 48000.0) && sound.right.size() == sound.left.size());
  10505. float peak = 0.0f;
  10506. bool finite = true;
  10507. for (std::size_t i = 0; i < sound.left.size(); ++i)
  10508. {
  10509. finite = finite && std::isfinite (sound.left[i]) && std::isfinite (sound.right[i]);
  10510. peak = std::max ({ peak, std::abs (sound.left[i]), std::abs (sound.right[i]) });
  10511. }
  10512. CHECK (finite && std::abs (peak - peakLevel) < 1.0e-4f);
  10513. auto rms = [&sound] (double from, double to) {
  10514. double e = 0.0;
  10515. const auto a = (std::size_t) (from * 48000.0), b = (std::size_t) (to * 48000.0);
  10516. for (auto i = a; i < b; ++i)
  10517. e += (double) sound.left[i] * sound.left[i];
  10518. return std::sqrt (e / (double) (b - a));
  10519. };
  10520. CHECK (rms (0.0, 0.05) < 0.2 * rms (impactTime - 0.05, impactTime)); // the whoosh swells
  10521. CHECK (rms (impactTime, impactTime + 0.05) > rms (impactTime - 0.05, impactTime)); // the landing
  10522. CHECK (rms (0.8, 1.0) > 0.005 && rms (soundLength - 0.02, soundLength) < rms (0.8, 1.0)); // chime rings, tail fades
  10523. CHECK (makeSound (48000.0).left == sound.left); // the same sound on every start
  10524. }
  10525. namespace
  10526. {
  10527. // little-endian byte writer for the state builders below
  10528. struct Bytes
  10529. {
  10530. std::vector<std::uint8_t> b;
  10531. void raw (const char* s, std::size_t n) { b.insert (b.end(), s, s + n); }
  10532. 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); }
  10533. void f32 (float v) { b.insert (b.end(), reinterpret_cast<const std::uint8_t*> (&v), reinterpret_cast<const std::uint8_t*> (&v) + 4); }
  10534. void zeros (std::size_t n) { b.insert (b.end(), n, 0); }
  10535. void bytes (const std::vector<std::uint8_t>& o) { b.insert (b.end(), o.begin(), o.end()); }
  10536. };
  10537. // A GMS state written from scratch (REA_rodada12 §1.1 layout, "Syn4"): oscillator waves that don't exist (silent
  10538. // tables, so the result does not depend on an FL installation), noise through the ladder with envelope and LFO,
  10539. // unison, slide, distortion, delay, reverb and the output EQ.
  10540. std::vector<std::uint8_t> gmsTestState()
  10541. {
  10542. std::array<float, 212> P {};
  10543. for (int i : { 1, 2, 44, 55, 56, 57, 58, 59, 60, 207, 208, 209, 210, 211 })
  10544. P[(std::size_t) i] = 0.5f;
  10545. P[0] = 0.75f, P[4] = 0.1f, P[5] = 0.4f, P[6] = 0.6f, P[7] = 0.3f, P[8] = 0.8f;
  10546. 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;
  10547. 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;
  10548. P[147] = 1.0f, P[117] = 0.3f, P[127] = 0.6f; // distortion
  10549. P[153] = 1.0f, P[123] = 0.5f, P[133] = 0.4f, P[143] = 0.4f; // delay (1/2 beat)
  10550. P[154] = 1.0f, P[124] = 0.6f, P[134] = 0.5f; // reverb
  10551. P[206] = 1.0f, P[207] = 0.7f, P[211] = 0.3f; // output EQ
  10552. Bytes preset;
  10553. preset.raw ("4nyS", 4);
  10554. for (const char* path : { "NoSuchDir\\NoWave A.wav", "NoSuchDir\\NoWave B.wav", "NoSuchDir\\NoWave C.wav", "", "", "", "" })
  10555. {
  10556. const auto n = std::strlen (path);
  10557. preset.raw (path, n);
  10558. preset.zeros (512 - n);
  10559. }
  10560. preset.i32 (0), preset.i32 (0), preset.i32 (0);
  10561. for (auto v : P)
  10562. preset.f32 (v);
  10563. preset.raw ("LYNX test", 9);
  10564. preset.zeros (512 - 9);
  10565. Bytes s;
  10566. s.raw ("SSLF", 4);
  10567. s.i32 ((std::int32_t) preset.b.size());
  10568. s.bytes (preset.b);
  10569. return s.b;
  10570. }
  10571. // A Morphine state written from scratch (REA_rodada16 §3 / REA_rodada39 §2.2 layout): generators A and B with a few
  10572. // frames each (A loops, PWM on A), C and D silent, a morph line A→B, four envelopes (Eg1 → pitch and velocity →
  10573. // amplitude through the matrix), the master ADSR and every plugin effect on.
  10574. std::vector<std::uint8_t> morphineTestState()
  10575. {
  10576. auto mbp = [] (int seed) {
  10577. Bytes b;
  10578. b.raw ("0PBM", 4);
  10579. for (int a = 0; a < 3; ++a)
  10580. for (int byte = 0; byte < 16; ++byte)
  10581. {
  10582. b.b.push_back (0xff);
  10583. for (int bit = 0; bit < 8; ++bit)
  10584. {
  10585. const auto k = byte * 8 + bit;
  10586. const float v = a == 0 ? std::cbrt (1.0f / (float) (k + 1 + seed * (k % 3))) * (k < 40 ? 1.0f : 0.2f)
  10587. : a == 1 ? 0.1f * (float) ((k * 7 + seed) % 11 - 5) / 5.0f
  10588. : 0.02f * (float) ((k * 5 + seed) % 7 - 3);
  10589. b.f32 (v);
  10590. }
  10591. }
  10592. return b.b;
  10593. };
  10594. auto msp = [&] (int frames, int seed, int loopMode) {
  10595. Bytes b;
  10596. b.raw ("0PSM", 4);
  10597. b.i32 (frames);
  10598. for (int f = 0; f < frames; ++f)
  10599. {
  10600. const auto m = mbp (seed + f);
  10601. b.i32 (20 + 7 * f), b.f32 (1.0f - 0.2f * (float) f), b.i32 ((std::int32_t) m.size());
  10602. b.bytes (m);
  10603. }
  10604. b.i32 (0x424);
  10605. b.i32 (10), b.i32 (40), b.i32 (0), b.i32 (loopMode);
  10606. b.f32 (0.45f), b.f32 (0.6f), b.f32 (0.5f), b.i32 (0);
  10607. for (int k = 0; k < 128; ++k)
  10608. b.f32 (0.3f);
  10609. for (int k = 0; k < 128; ++k)
  10610. b.f32 (1.0f);
  10611. return b.b;
  10612. };
  10613. auto mvc = [&] (int frames, int seed, int pwmMode) {
  10614. Bytes z;
  10615. z.raw ("0HZM", 4);
  10616. z.i32 (1);
  10617. const auto s = msp (frames, seed, 1);
  10618. z.i32 (128), z.f32 (frames > 1 ? 0.9f : 0.0f), z.i32 ((std::int32_t) s.size());
  10619. z.bytes (s);
  10620. Bytes b;
  10621. b.raw ("0CVM", 4);
  10622. b.i32 ((std::int32_t) z.b.size());
  10623. b.bytes (z.b);
  10624. 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);
  10625. return b.b;
  10626. };
  10627. Bytes p;
  10628. p.raw ("0roM", 4);
  10629. p.raw ("0RPM", 4);
  10630. for (const auto& g : { mvc (3, 1, 2), mvc (2, 7, 0), mvc (1, 3, 0), mvc (1, 5, 0) })
  10631. {
  10632. p.i32 ((std::int32_t) g.size());
  10633. p.bytes (g);
  10634. }
  10635. {
  10636. Bytes m;
  10637. m.raw ("0LMM", 4);
  10638. m.i32 (2), m.i32 (12);
  10639. m.f32 (0.0f), m.f32 (1.0f), m.i32 (60);
  10640. m.f32 (1.0f), m.f32 (1.0f), m.i32 (60);
  10641. m.i32 (0x54), m.i32 (0), m.i32 (0), m.i32 (0), m.i32 (0);
  10642. for (float v : { 1.0f, 0.8f, 0.0f, 0.0f, 0.5f, 0.4f, 0.5f, 0.5f })
  10643. m.f32 (v);
  10644. for (int k = 0; k < 4; ++k)
  10645. m.i32 (0);
  10646. m.f32 (0.5f), m.f32 (0.5f), m.f32 (0.5f), m.i32 (0);
  10647. p.i32 ((std::int32_t) m.b.size());
  10648. p.bytes (m.b);
  10649. }
  10650. {
  10651. Bytes d;
  10652. d.raw ("0DMM", 4);
  10653. d.i32 (4);
  10654. for (int e = 0; e < 4; ++e)
  10655. {
  10656. Bytes g;
  10657. g.raw ("0GEM", 4);
  10658. g.i32 (3), g.i32 (8);
  10659. g.f32 (0.0f), g.i32 (10), g.f32 (1.0f), g.i32 (40 + 10 * e), g.f32 (0.3f), g.i32 (50);
  10660. 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),
  10661. g.i32 (e == 0 ? 1 : 0);
  10662. d.i32 ((std::int32_t) g.b.size());
  10663. d.bytes (g.b);
  10664. }
  10665. d.i32 (0x94);
  10666. const int src[12] = { 5, 2 }, dst[12] = { 3, 1 };
  10667. const float amt[12] = { 0.56f, 0.7f };
  10668. for (int k = 0; k < 12; ++k)
  10669. d.i32 (src[k]);
  10670. for (int k = 0; k < 12; ++k)
  10671. d.i32 (dst[k]);
  10672. for (int k = 0; k < 12; ++k)
  10673. d.f32 (amt[k]);
  10674. p.i32 ((std::int32_t) d.b.size());
  10675. p.bytes (d.b);
  10676. }
  10677. {
  10678. 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
  10679. 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 …
  10680. 0.4f, 0.3f, 1.0f, 0.5f, 0.5f, 0.4f, 1.0f, 0.6f, 0.4f, 0.5f, // … delay, reverb, EQ …
  10681. 0.5f, 0.5f, 0.55f, 0.5f, 0.45f, 1.0f, 0.0f, 0.5f, 1.0f, 0.0f }; // EQ on, noise (muted)
  10682. Bytes g;
  10683. g.i32 (0x5c4);
  10684. for (auto v : A)
  10685. g.f32 (v);
  10686. g.zeros (0x5c4 - g.b.size());
  10687. p.bytes (g.b);
  10688. }
  10689. Bytes s;
  10690. s.raw ("SSLF", 4);
  10691. s.i32 ((std::int32_t) p.b.size());
  10692. s.bytes (p.b);
  10693. return s.b;
  10694. }
  10695. void dumpIfAsked (const char* var, const std::vector<std::uint8_t>& blob)
  10696. {
  10697. if (const char* path = std::getenv (var))
  10698. {
  10699. std::ofstream out (path, std::ios::binary);
  10700. out.write (reinterpret_cast<const char*> (blob.data()), (std::streamsize) blob.size());
  10701. }
  10702. }
  10703. } // namespace
  10704. TEST (dx10_engine)
  10705. {
  10706. // REA_rodada42: a Fruity DX10 state written from scratch (version 1: 22 raw parameters) against our reference model
  10707. // (dxmodel.py + dxproc.py, bit-identical to the installation's DLL), the channel processor, host levels and the
  10708. // instrument front-end
  10709. const std::int32_t P[22] = { 1000, 40000, 30000, 30000, 40000, 30000, 35000, 8000, 40000, 50000, 20000,
  10710. 25000, 15000, 30000, 20000, 10000, 20000, 30000, 5000, 30000, 20000, -1 };
  10711. std::vector<std::uint8_t> blob (4 + 22 * 4, 0);
  10712. blob[0] = 1;
  10713. std::memcpy (blob.data() + 4, P, sizeof (P));
  10714. CHECK (lynx::dx10StateUsable (blob) && lynx::dx10ProcessorNeutral (blob));
  10715. CHECK (lynx::dx10Param (blob, 21) == -1 && lynx::dx10Param (blob, 3) == 30000 && ! lynx::dx10Param (blob, 22));
  10716. CHECK (! lynx::dx10StateUsable (std::vector<std::uint8_t> (40, 0)));
  10717. {
  10718. auto bad = blob;
  10719. bad[0] = 3; // versions ≥ 3 are ignored by the plugin
  10720. CHECK (! lynx::dx10StateUsable (bad));
  10721. }
  10722. using N = lynx::dx10::RefNote;
  10723. 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 } };
  10724. struct Golden { std::vector<std::uint8_t> state; lynx::dx10::RefOptions opt; int rate; double el, er; std::tuple<int, float, float> at[6]; };
  10725. auto check = [&] (const Golden& g) {
  10726. const auto y = lynx::dx10::renderReference (g.state, notes, (double) g.rate, 16384, g.opt);
  10727. CHECK (y.size() == 2 * 16384);
  10728. if (y.size() != 2 * 16384)
  10729. return y;
  10730. double el = 0.0, er = 0.0;
  10731. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  10732. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  10733. CHECK (std::abs (el - g.el) <= 1e-7 * g.el && std::abs (er - g.er) <= 1e-7 * g.er);
  10734. for (const auto& [i, l, r] : g.at)
  10735. CHECK (std::abs (y[2 * (std::size_t) i] - l) <= 1e-7f * std::max (1e-16f, std::abs (l))
  10736. && std::abs (y[2 * (std::size_t) i + 1] - r) <= 1e-7f * std::max (1e-16f, std::abs (r)));
  10737. return y;
  10738. };
  10739. lynx::dx10::RefOptions plain, odd;
  10740. odd.block = 333, odd.maxPoly = 1;
  10741. check ({ blob, plain, 44100, 672.293115, 778.393071,
  10742. { { 100, -0.0943799689f, -0.154014125f }, { 1000, -0.0814286768f, -0.132879525f }, { 2500, 0.499493301f, 0.312498778f },
  10743. { 5000, -0.184045464f, 0.171641544f }, { 9000, -0.153369367f, -0.20805186f }, { 16383, -0.00441304967f, 0.200126961f } } });
  10744. // 48 kHz, odd host blocks, max polyphony 1 (the last note steals); the hard-right note keeps FL's x87 left gain
  10745. check ({ blob, odd, 48000, 122.232761, 493.320297,
  10746. { { 100, 0.0236493405f, 0.0385922194f }, { 1000, 0.189806491f, 0.309736073f }, { 2500, -0.425576478f, -0.176279545f },
  10747. { 5000, -3.54030076e-18f, -0.0578194074f }, { 9000, 1.37052479e-17f, 0.223831087f }, { 16383, 9.248742e-18f, 0.151048407f } } });
  10748. // the channel processor (§3): our own settings snapshot — pan LFO (triangle, delayed, faded in), volume envelope
  10749. // with tensions and LFO, cutoff envelope + square LFO, resonance LFO, pitch envelope, SVF LP ×2 (type 7)
  10750. auto record = [] (std::vector<std::uint8_t>& s, int k, std::initializer_list<std::pair<int, int>> fields) {
  10751. const std::int32_t neutral[17] = { 0, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, 0 };
  10752. std::memcpy (s.data() + 0x44 * k, neutral, sizeof (neutral));
  10753. for (const auto& [i, v] : fields)
  10754. std::memcpy (s.data() + 0x44 * k + 4 * i, &v, 4);
  10755. };
  10756. auto settings = [] (std::vector<std::uint8_t>& s, std::int32_t modX, std::int32_t modY, std::int32_t type) {
  10757. std::memcpy (s.data() + 0x15c, &modX, 4);
  10758. std::memcpy (s.data() + 0x160, &modY, 4);
  10759. std::memcpy (s.data() + 0x168, &type, 4);
  10760. };
  10761. std::vector<std::uint8_t> snap (364, 0);
  10762. record (snap, 0, { { 9, 5000 }, { 10, 9000 }, { 11, -40 }, { 12, 15000 }, { 13, 1 } });
  10763. 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 } });
  10764. record (snap, 2, { { 1, 1 }, { 3, 6000 }, { 4, 100 }, { 5, 16000 }, { 6, 20 }, { 8, -60 }, { 11, 20 }, { 12, 18000 }, { 13, 2 } });
  10765. record (snap, 3, { { 11, 40 }, { 12, 17000 } });
  10766. record (snap, 4, { { 1, 1 }, { 3, 5000 }, { 4, 100 }, { 5, 14000 }, { 6, 30 }, { 8, -700 } });
  10767. settings (snap, 140, 120, 7);
  10768. const auto withSnap = lynx::dx10StateWithSnapshot (blob, snap);
  10769. CHECK (withSnap.size() == 460 && withSnap[0] == 2 && ! lynx::dx10ProcessorNeutral (withSnap));
  10770. CHECK (lynx::dx10StateWithSnapshot (withSnap, std::vector<std::uint8_t> (364, 0)) == withSnap); // a v2 state keeps its own
  10771. lynx::dx10::RefOptions ticks;
  10772. ticks.tick = 230, ticks.ppq = 96, ticks.tempo = 120.0;
  10773. check ({ withSnap, ticks, 44100, 346.852062, 554.699375,
  10774. { { 100, -0.00162654708f, -0.00265428401f }, { 1000, 0.0310285408f, 0.0506339073f }, { 2500, -0.118068494f, -0.0915558636f },
  10775. { 5000, -0.297166437f, -0.256465197f }, { 9000, 0.199509233f, 0.166108996f }, { 16383, -0.00392446062f, 0.0410436988f } } });
  10776. // a volume envelope decaying to zero: the 10 ms end fade, then every voice is gone (silence from 14950 on)
  10777. std::vector<std::uint8_t> decay (364, 0);
  10778. for (int k = 0; k < 5; ++k)
  10779. record (decay, k, {});
  10780. record (decay, 1, { { 0, 4 }, { 1, 1 }, { 3, 9000 }, { 4, 100 }, { 5, 12000 }, { 6, 0 }, { 15, 50 }, { 16, -101 } });
  10781. settings (decay, 256, 0, 0);
  10782. auto ticks48 = ticks;
  10783. ticks48.block = 333;
  10784. const auto y = check ({ lynx::dx10StateWithSnapshot (blob, decay), ticks48, 48000, 338.361707, 350.520208,
  10785. { { 100, 0.000514116196f, 0.000838961045f }, { 1000, 0.0412623473f, 0.0673340634f }, { 2500, -0.141532451f, -0.156581759f },
  10786. { 5000, 0.382550895f, 0.23462519f }, { 7000, -0.189829409f, 0.144132584f }, { 9000, -0.13638559f, 0.142912149f } } });
  10787. bool silentTail = y.size() == 2 * 16384 && (y[2 * 14949] != 0.0f || y[2 * 14949 + 1] != 0.0f);
  10788. for (std::size_t i = 2 * 14950; i < y.size(); ++i)
  10789. silentTail = silentTail && y[i] == 0.0f;
  10790. CHECK (silentTail);
  10791. // the instrument: LYNX velocity v → Init.Vol = (11^(1.28·v) − 1)/10 and Final.Vol = channel volume × Init.Vol; channel
  10792. // gains √2·V·(cos, sin)((1 + p)π/4) → V, p; notes on block boundaries play like the reference renderer (neutral
  10793. // channel settings: the instrument's sequencer ticks change nothing; vibrato off, since the plugin-wide vibrato
  10794. // oscillator is stepped by each voice in turn, so its interleaving follows the host block layout, as in FL)
  10795. auto noVib = blob;
  10796. CHECK (lynx::dx10SetParam (noVib, 10, 0));
  10797. auto initVol = [] (double v) { return (double) (float) ((std::pow (11.0, 1.28 * v) - 1.0) / 10.0); }; // Init.Vol is a float
  10798. lynx::ChannelDef c;
  10799. c.instrument = lynx::InstrumentKind::dx10;
  10800. c.pluginState = noVib;
  10801. auto inst = lynx::makeInstrument (lynx::InstrumentKind::dx10);
  10802. inst->configure (c, 120.0);
  10803. inst->prepare (44100.0);
  10804. const float V = 0.6f, pan = 0.25f;
  10805. const auto gl = (float) (std::sqrt (2.0) * V * std::cos ((1.0 + pan) * 3.141592653589793 / 4.0));
  10806. const auto gr = (float) (std::sqrt (2.0) * V * std::sin ((1.0 + pan) * 3.141592653589793 / 4.0));
  10807. std::vector<float> l (16384), r (16384);
  10808. for (int s = 0; s < 16384; s += 512)
  10809. {
  10810. if (s == 0)
  10811. inst->noteOn (60, 0.78125f);
  10812. if (s == 2048)
  10813. {
  10814. inst->setNextNoteExpression (-0.5f, 25.0f);
  10815. inst->noteOn (67, 0.5f);
  10816. }
  10817. if (s == 6144)
  10818. inst->noteOff (60);
  10819. if (s == 8192)
  10820. inst->noteOff (67);
  10821. inst->render (l.data() + s, r.data() + s, 512, gl, gr);
  10822. }
  10823. // the channel level reaches the engine as FL's (recovered to float precision)
  10824. const auto Vr = std::sqrt (((double) gl * gl + (double) gr * gr) * 0.5);
  10825. const auto pr = std::atan2 ((double) gr, (double) gl) * (4.0 / 3.141592653589793) - 1.0;
  10826. const auto ref = lynx::dx10::renderReference (noVib, { { 0, 60, initVol (0.78125), Vr * initVol (0.78125), pr, 0.0, 6144 },
  10827. { 2048, 67, initVol (0.5), Vr * initVol (0.5), std::clamp (-0.5 + pr, -1.0, 1.0), 25.0, 8192 } },
  10828. 44100.0, 16384);
  10829. double worst = 0.0, peak = 0.0;
  10830. for (int i = 0; i < 16384; ++i)
  10831. 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]) }),
  10832. peak = std::max (peak, (double) std::abs (l[(std::size_t) i]));
  10833. CHECK (worst == 0.0 && peak > 0.05);
  10834. // the released voices end (DX10's own release) and the instrument goes idle
  10835. std::vector<float> tail (44100), tailR (tail.size());
  10836. for (int k = 0; k < 20 && ! inst->idle(); ++k)
  10837. inst->render (tail.data(), tailR.data(), (int) tail.size(), gl, gr);
  10838. CHECK (inst->idle());
  10839. // with the channel settings above the instrument follows FL's ticks: the cutoff envelope darkens the note
  10840. {
  10841. lynx::ChannelDef cs = c;
  10842. cs.pluginState = withSnap;
  10843. cs.modelParams = { 96.0f };
  10844. auto si = lynx::makeInstrument (lynx::InstrumentKind::dx10);
  10845. si->configure (cs, 120.0);
  10846. si->prepare (44100.0);
  10847. std::vector<float> sl (32768), sr (32768);
  10848. si->noteOn (48, 0.78125f);
  10849. for (int s = 0; s < 32768; s += 512)
  10850. si->render (sl.data() + s, sr.data() + s, 512, 1.0f, 1.0f);
  10851. double e1 = 0.0, e2 = 0.0;
  10852. bool finite = true;
  10853. for (int i = 0; i < 32768; ++i)
  10854. finite = finite && std::isfinite (sl[(std::size_t) i]), (i < 4096 ? e1 : e2) += (double) sl[(std::size_t) i] * sl[(std::size_t) i];
  10855. CHECK (finite && e1 > 1e-3 && e2 > 1e-3);
  10856. }
  10857. // parameter edits (the channel settings panel) write the raw integers, clamped to the knob ranges
  10858. auto edited = blob;
  10859. CHECK (lynx::dx10SetParam (edited, 21, 7) && lynx::dx10Param (edited, 21) == 3);
  10860. CHECK (lynx::dx10SetParam (edited, 12, 70000) && lynx::dx10Param (edited, 12) == 65536);
  10861. }
  10862. TEST (threeosc_engine)
  10863. {
  10864. // REA_rodada47: a 3x Osc state written from scratch (version 14) against our reference model (osc3model.py,
  10865. // bit-identical to the installation's DLL), the plugin voice processor with its release path, the instrument
  10866. 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 };
  10867. 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) {
  10868. std::vector<std::uint8_t> st (0x60, 0);
  10869. const std::int32_t version = 14;
  10870. std::memcpy (st.data(), &version, 4);
  10871. std::memcpy (st.data() + 4, p, 21 * 4);
  10872. st[0x58] = inv[0], st[0x59] = inv[1], st[0x5a] = inv[2], st[0x5b] = am;
  10873. st.insert (st.end(), snap.begin(), snap.end());
  10874. return st;
  10875. };
  10876. auto record = [] (std::vector<std::uint8_t>& s, int k, std::initializer_list<std::pair<int, int>> fields) {
  10877. const std::int32_t neutral[17] = { 0, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, 0 };
  10878. std::memcpy (s.data() + 0x44 * k, neutral, sizeof (neutral));
  10879. for (const auto& [i, v] : fields)
  10880. std::memcpy (s.data() + 0x44 * k + 4 * i, &v, 4);
  10881. };
  10882. auto settings = [] (std::vector<std::uint8_t>& s, std::int32_t modX, std::int32_t modY, std::int32_t type) {
  10883. std::memcpy (s.data() + 0x15c, &modX, 4);
  10884. std::memcpy (s.data() + 0x160, &modY, 4);
  10885. std::memcpy (s.data() + 0x168, &type, 4);
  10886. };
  10887. std::vector<std::uint8_t> neutral (364, 0);
  10888. for (int k = 0; k < 5; ++k)
  10889. record (neutral, k, k == 1 ? std::initializer_list<std::pair<int, int>> { { 0, 4 }, { 16, -101 } } : std::initializer_list<std::pair<int, int>> {});
  10890. settings (neutral, 256, 0, 0);
  10891. const auto blob = make (P, { 0, 0, 0 }, 0, neutral);
  10892. CHECK (lynx::threeOscStateUsable (blob) && lynx::threeOscNormalisedState (blob, {}) == blob);
  10893. CHECK (lynx::threeOscParam (blob, 9) == -12 && lynx::threeOscParam (blob, 20) == 16 && lynx::threeOscParam (blob, 24) == 0 && ! lynx::threeOscParam (blob, 26));
  10894. // older versions are normalised like the plugin reads them: version 12 (no snapshot → the plugin's defaults),
  10895. // version 10 (20 parameters, no invert/AM bytes)
  10896. {
  10897. std::vector<std::uint8_t> v12 (blob.begin(), blob.begin() + 92);
  10898. v12[0] = 12;
  10899. v12[0x58] = 1;
  10900. const auto n12 = lynx::threeOscNormalisedState (v12, {});
  10901. CHECK (n12.size() == 460 && n12[0] == 14 && lynx::threeOscParam (n12, 21) == 1 && lynx::threeOscParam (n12, 20) == 16
  10902. && std::equal (n12.begin() + 0x60, n12.end(), neutral.begin()));
  10903. std::vector<std::uint8_t> v10 (blob.begin(), blob.begin() + 84);
  10904. v10[0] = 10;
  10905. const auto n10 = lynx::threeOscNormalisedState (v10, {});
  10906. CHECK (lynx::threeOscParam (n10, 20) == 0 && lynx::threeOscParam (n10, 13) == 40 && lynx::threeOscParam (n10, 21) == 0);
  10907. }
  10908. using N = lynx::threeosc::RefNote;
  10909. 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 } };
  10910. struct Golden { std::vector<std::uint8_t> state; lynx::threeosc::RefOptions opt; int rate; double el, er; std::tuple<int, float, float> at[6]; };
  10911. auto check = [&] (const Golden& g) {
  10912. const auto y = lynx::threeosc::renderReference (g.state, notes, (double) g.rate, 16384, g.opt);
  10913. CHECK (y.size() == 2 * 16384);
  10914. if (y.size() != 2 * 16384)
  10915. return y;
  10916. double el = 0.0, er = 0.0;
  10917. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  10918. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  10919. CHECK (std::abs (el - g.el) <= 1e-7 * g.el && std::abs (er - g.er) <= 1e-7 * g.er);
  10920. for (const auto& [i, l, r] : g.at)
  10921. CHECK (std::abs (y[2 * (std::size_t) i] - l) <= 1e-7f * std::max (1e-16f, std::abs (l))
  10922. && std::abs (y[2 * (std::size_t) i + 1] - r) <= 1e-7f * std::max (1e-16f, std::abs (r)));
  10923. return y;
  10924. };
  10925. // saw / square / triangle-ish mix with pans, stereo detune and phase, random start phases (seed 12345)
  10926. lynx::threeosc::RefOptions plain;
  10927. plain.seed = 12345;
  10928. check ({ blob, plain, 44100, 681.6491972612481, 1509.7245664533937,
  10929. { { 100, 0.0674247071146965f, -0.13440212607383728f }, { 1000, 0.12890009582042694f, 0.446039617061615f },
  10930. { 2500, -0.007686644792556763f, 0.3574123680591583f }, { 5000, 0.08792756497859955f, 0.4948147237300873f },
  10931. { 9000, 1.2136793475613885e-17f, 0.3116455078125f }, { 16383, 5.7305530145831346e-18f, 0.4134995639324188f } } });
  10932. // AM (osc 3 noise modulating osc 1 + 2), inverted oscillators, 48 kHz, odd blocks, max polyphony 1
  10933. 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 };
  10934. lynx::threeosc::RefOptions odd;
  10935. odd.block = 333, odd.maxPoly = 1, odd.seed = 777;
  10936. check ({ make (Pam, { 1, 0, 1 }, 1, neutral), odd, 48000, 453.27241060751771, 1996.4548848690094,
  10937. { { 100, -0.2589033246040344f, -0.310184121131897f }, { 1000, 0.22021546959877014f, 0.3591070771217346f },
  10938. { 2500, -0.5378497242927551f, -0.15602676570415497f }, { 5000, 1.2423200626807308e-17f, 0.3293072581291199f },
  10939. { 9000, 2.5857780099332177e-17f, 0.42474493384361267f }, { 16383, 2.941168384618868e-17f, 0.3569665551185608f } } });
  10940. // the channel processor with its release path: pan LFO, volume envelope released from its current value, cutoff
  10941. // envelope + square LFO, resonance LFO, pitch envelope, SVF LP ×2 (type 7)
  10942. std::vector<std::uint8_t> snap (364, 0);
  10943. record (snap, 0, { { 9, 5000 }, { 10, 9000 }, { 11, -40 }, { 12, 15000 }, { 13, 1 } });
  10944. 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 } });
  10945. record (snap, 2, { { 1, 1 }, { 3, 6000 }, { 4, 100 }, { 5, 16000 }, { 6, 20 }, { 7, 9000 }, { 8, -60 }, { 11, 20 }, { 12, 18000 }, { 13, 2 } });
  10946. record (snap, 3, { { 11, 40 }, { 12, 17000 } });
  10947. record (snap, 4, { { 1, 1 }, { 3, 5000 }, { 4, 100 }, { 5, 14000 }, { 6, 30 }, { 7, 12000 }, { 8, -700 } });
  10948. settings (snap, 140, 120, 7);
  10949. lynx::threeosc::RefOptions ticks;
  10950. ticks.tick = 230, ticks.ppq = 96, ticks.tempo = 120.0, ticks.seed = 99;
  10951. check ({ make (P, { 0, 0, 0 }, 0, snap), ticks, 44100, 977.9928196811321, 2072.965349921462,
  10952. { { 100, -0.001351095037534833f, 0.0029758031014353037f }, { 1000, 0.002833257894963026f, 0.10575854033231735f },
  10953. { 2500, 0.020360738039016724f, 0.3181821405887604f }, { 5000, 0.3340669274330139f, 0.4912331700325012f },
  10954. { 9000, 0.26940545439720154f, 0.27708977460861206f }, { 16383, 0.16570518910884857f, -0.10794822871685028f } } });
  10955. auto ticks48 = ticks;
  10956. ticks48.block = 333, ticks48.tick = 250, ticks48.seed = 5;
  10957. // (at 48 kHz the library's rate ratio 44100/fs scales the filter coefficients and the ramp limit)
  10958. check ({ make (P, { 0, 1, 0 }, 0, snap), ticks48, 48000, 864.9859578029492, 1895.6281818565287,
  10959. { { 100, 0.0031638722866773605f, -0.001987025374546647f }, { 1000, 0.049239177256822586f, 0.08794696629047394f },
  10960. { 2500, 0.13807585835456848f, 0.3554498553276062f }, { 5000, -0.08793184161186218f, -0.1596519947052002f },
  10961. { 9000, 0.08728396147489548f, 0.540290117263794f }, { 16383, 0.039887525141239166f, 0.21270982921123505f } } });
  10962. // HQ oscillators (engine_x64.dll: band-limited tables built with an FFT; FL's single-precision FFT and LYNX's double
  10963. // one agree to float rounding, so the golden values hold to 1e-5)
  10964. {
  10965. 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 };
  10966. auto hq = make (Phq, { 0, 0, 0 }, 0, neutral);
  10967. hq[0x5c] = 1;
  10968. CHECK (lynx::threeOscParam (hq, 25) == 1);
  10969. lynx::threeosc::RefOptions o;
  10970. o.seed = 3;
  10971. const auto y = lynx::threeosc::renderReference (hq, notes, 44100.0, 16384, o);
  10972. CHECK (y.size() == 2 * 16384);
  10973. if (y.size() == 2 * 16384)
  10974. {
  10975. double el = 0.0, er = 0.0;
  10976. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  10977. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  10978. CHECK (std::abs (el - 862.7912087773848) < 1e-5 * 862.8 && std::abs (er - 1232.8046929286115) < 1e-5 * 1232.8);
  10979. const std::tuple<int, float, float> at[] = { { 100, -0.019788391888141632f, -0.19209949672222137f }, { 1000, 0.12019827216863632f, 0.03852222114801407f },
  10980. { 2500, 0.3239150643348694f, 0.07325324416160583f }, { 5000, -0.2809634208679199f, -0.5693862438201904f },
  10981. { 16383, -1.1937656360586644e-17f, -0.061853960156440735f } };
  10982. for (const auto& [i, l, r] : at)
  10983. CHECK (std::abs (y[2 * (std::size_t) i] - l) < 1e-5f && std::abs (y[2 * (std::size_t) i + 1] - r) < 1e-5f);
  10984. }
  10985. }
  10986. // the instrument: Final.Vol = channel volume × Init.Vol, channel gains → FL's volume/pan; notes on block boundaries
  10987. // play like the reference renderer (seed 0, neutral channel settings: ticks change nothing)
  10988. auto initVol = [] (double v) { return (double) (float) ((std::pow (11.0, 1.28 * v) - 1.0) / 10.0); };
  10989. lynx::ChannelDef c;
  10990. c.instrument = lynx::InstrumentKind::threeosc;
  10991. c.pluginState = blob;
  10992. auto inst = lynx::makeInstrument (lynx::InstrumentKind::threeosc);
  10993. inst->configure (c, 120.0);
  10994. inst->prepare (44100.0);
  10995. const float V = 0.6f, pan = 0.25f;
  10996. const auto gl = (float) (std::sqrt (2.0) * V * std::cos ((1.0 + pan) * 3.141592653589793 / 4.0));
  10997. const auto gr = (float) (std::sqrt (2.0) * V * std::sin ((1.0 + pan) * 3.141592653589793 / 4.0));
  10998. std::vector<float> l (16384), r (16384);
  10999. for (int s = 0; s < 16384; s += 512)
  11000. {
  11001. if (s == 0)
  11002. inst->noteOn (60, 0.78125f);
  11003. if (s == 2048)
  11004. {
  11005. inst->setNextNoteExpression (-0.5f, 25.0f);
  11006. inst->noteOn (67, 0.5f);
  11007. }
  11008. if (s == 6144)
  11009. inst->noteOff (60);
  11010. if (s == 8192)
  11011. inst->noteOff (67);
  11012. inst->render (l.data() + s, r.data() + s, 512, gl, gr);
  11013. }
  11014. const auto Vr = std::sqrt (((double) gl * gl + (double) gr * gr) * 0.5);
  11015. const auto pr = std::atan2 ((double) gr, (double) gl) * (4.0 / 3.141592653589793) - 1.0;
  11016. const auto ref = lynx::threeosc::renderReference (blob, { { 0, 60, Vr * initVol (0.78125), pr, 0.0, 6144 },
  11017. { 2048, 67, Vr * initVol (0.5), std::clamp (-0.5 + pr, -1.0, 1.0), 25.0, 8192 } },
  11018. 44100.0, 16384);
  11019. double worst = 0.0, peak = 0.0;
  11020. for (int i = 0; i < 16384; ++i)
  11021. 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]) }),
  11022. peak = std::max (peak, (double) std::abs (l[(std::size_t) i]));
  11023. CHECK (worst == 0.0 && peak > 0.05);
  11024. // released notes end with the processor's 10 ms fade (volume envelope off) and the instrument goes idle
  11025. CHECK (inst->idle());
  11026. // parameter edits write the raw integers, clamped to the knob ranges
  11027. auto edited = blob;
  11028. CHECK (lynx::threeOscSetParam (edited, 9, -40) && lynx::threeOscParam (edited, 9) == -24);
  11029. CHECK (lynx::threeOscSetParam (edited, 13, 500) && lynx::threeOscParam (edited, 13) == 128);
  11030. CHECK (lynx::threeOscSetParam (edited, 24, 1) && edited[0x5b] == 1);
  11031. CHECK (! lynx::threeOscStateUsable (std::vector<std::uint8_t> (2, 0)));
  11032. }
  11033. TEST (gms_engine)
  11034. {
  11035. const auto blob = gmsTestState();
  11036. dumpIfAsked ("LYNX_DUMP_GMS", blob);
  11037. CHECK (lynx::gmsStateUsable (blob));
  11038. CHECK (lynx::gmsWaveNames (blob) == std::vector<std::string> ({ "NoWave A", "NoWave B", "NoWave C" }));
  11039. CHECK (lynx::gmsParamOffset (blob, 12) == 8 + 4 + 7 * 512 + 12 + 4 * 12 && lynx::gmsParamOffset (blob, 212) < 0);
  11040. auto broken = blob;
  11041. broken[8] = 'X';
  11042. CHECK (! lynx::gmsStateUsable (broken));
  11043. CHECK (! lynx::gmsStateUsable (std::vector<std::uint8_t> (blob.begin(), blob.begin() + 2000)));
  11044. // golden samples of our round-39 model (gmsmodel.GmsPlugin, waves missing → silent tables, tempo 140, 44.1 kHz,
  11045. // host blocks of 512): C5 vel 100 0–30000, E5 vel 80 11025–40000, G5 vel 110 22050–52000 (Init.Vol law A)
  11046. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  11047. const std::vector<lynx::gms::RefNote> notes = { { 0, 60, (float) iv (100), 1.0, 0.0, 0.0, 30000 },
  11048. { 11025, 64, (float) iv (80), 1.0, 0.0, 0.0, 40000 },
  11049. { 22050, 67, (float) iv (110), 1.0, 0.0, 0.0, 52000 } };
  11050. const auto y = lynx::gms::renderReference (blob, notes, 44100.0, 66150, "");
  11051. CHECK (y.size() == 2 * 66150u);
  11052. 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 } };
  11053. for (const auto& g : golden)
  11054. 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);
  11055. double e = 0.0;
  11056. for (auto v : y)
  11057. e += (double) v * v;
  11058. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.0790270452795) < 1e-6);
  11059. // the instrument: same engine behind LYNX's channel gains (V = 1, centre → law A), velocity → Init.Vol
  11060. lynx::ChannelDef c;
  11061. c.instrument = lynx::InstrumentKind::gms;
  11062. c.pluginState = blob;
  11063. auto inst = lynx::makeInstrument (lynx::InstrumentKind::gms);
  11064. inst->configure (c, 140.0);
  11065. inst->prepare (44100.0);
  11066. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  11067. for (int b = 0; b < 16; ++b)
  11068. {
  11069. if (b == 0)
  11070. inst->noteOn (60, 0.78125f);
  11071. if (b == 10)
  11072. inst->noteOff (60);
  11073. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  11074. }
  11075. 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, "");
  11076. double diff = 0.0, energy = 0.0;
  11077. for (int i = 0; i < 8192; ++i)
  11078. {
  11079. 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]) });
  11080. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  11081. }
  11082. CHECK (diff == 0.0 && energy > 1e-3);
  11083. }
  11084. // A Toxic Biohazard state written from scratch (REA_rodada45; r45/py/mktest.py builds the same bytes): our own drawn
  11085. // spectra on operators 1 and 3 (wave 0) and the sine on operator 2, so no wave of the installation is needed
  11086. std::vector<std::uint8_t> toxicTestState()
  11087. {
  11088. auto s = lynx::toxicDefaultState();
  11089. static const std::pair<int, double> set[] = {
  11090. { 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 },
  11091. { 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 },
  11092. { 24, 0.6 }, { 25, 1 }, { 26, 1 }, { 27, 1 }, { 32, 0.5606 }, { 33, 0.4394 }, { 38, 0.53 }, { 49, 0 }, { 55, 0.25 },
  11093. { 62, 0.5 }, { 75, 0.08 }, { 81, 0.5 }, { 87, 0.3 }, { 104, 0.8 }, { 105, 0.6 }, { 110, 0.35 }, { 115, 0.22 },
  11094. { 127, 0.18 }, { 147, 0.7 }, { 227, 1 }, { 229, 0.65 }, { 233, 0.4 }, { 236, 1 }, { 237, 1 }, { 252, 0.5 }, { 254, 0.3 },
  11095. { 256, 0.4 }, { 267, 0.4 }, { 269, 0.5 }, { 277, 0.3 }, { 300, 1 }, { 301, 8 }, { 302, 2 }, { 303, 0 }, { 305, 3 },
  11096. { 306, 0 }, { 307, 1 }, { 308, 0 }, { 314, 1 }, { 315, 0 }, { 316, 1 }, { 324, 106 }, { 332, 0.7 } };
  11097. for (const auto& [index, value] : set)
  11098. {
  11099. const auto v = (float) value;
  11100. std::memcpy (s.data() + lynx::toxicParamOffset (s, index), &v, 4);
  11101. }
  11102. auto spectrum = [&] (int slot, int h, float amp, float phase) {
  11103. const auto at = 12 + 0x40 + 1024 * (std::size_t) slot + 4 * (std::size_t) h;
  11104. std::memcpy (s.data() + at, &amp, 4);
  11105. std::memcpy (s.data() + at + 512, &phase, 4);
  11106. };
  11107. for (int h = 0; h < 24; ++h)
  11108. spectrum (0, h, (float) (1.0 / (h + 1)), 0.0f);
  11109. for (int k = 0; k < 16; ++k)
  11110. spectrum (2, 2 * k, (float) (1.0 / (2 * k + 1)), 0.25f);
  11111. return s;
  11112. }
  11113. TEST (toxic_engine)
  11114. {
  11115. const auto blob = toxicTestState();
  11116. dumpIfAsked ("LYNX_DUMP_TOXIC", blob);
  11117. CHECK (blob.size() == 10050u && lynx::toxicStateUsable (blob) && ! lynx::toxicNeedsDll (blob));
  11118. CHECK (lynx::toxicParamOffset (blob, 15) == 12 + 0x40 + 8192 + 4 * 15 && lynx::toxicParamOffset (blob, 340) < 0);
  11119. CHECK (! lynx::toxicDllUsable ("") && ! lynx::toxicDllUsable ("/nonexistent/Toxic Biohazard_x64.dll"));
  11120. auto broken = blob;
  11121. broken[8] = 'X';
  11122. CHECK (! lynx::toxicStateUsable (broken));
  11123. CHECK (! lynx::toxicStateUsable (std::vector<std::uint8_t> (blob.begin(), blob.begin() + 5000)));
  11124. auto needs = blob;
  11125. const auto w = 15.0f; // a built-in wave of the installation: without the DLL the engine refuses the state
  11126. std::memcpy (needs.data() + lynx::toxicParamOffset (needs, 307), &w, 4);
  11127. CHECK (lynx::toxicNeedsDll (needs) && lynx::toxic::renderReference (needs, { { 0, 60 } }, 44100.0, 64, "").empty());
  11128. // golden samples of our round-45 model (toxmodel.run, which equals the DLL bit for bit on this state), effects on,
  11129. // tempo 140, 44.1 kHz, host blocks of 512: C5 vel 100 0–30000, E5 vel 80 11025–40000, G5 vel 110 22050–52000
  11130. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  11131. const std::vector<lynx::toxic::RefNote> notes = { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 },
  11132. { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  11133. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 } };
  11134. const auto y = lynx::toxic::renderReference (blob, notes, 44100.0, 66150, "", 512, 140.0);
  11135. CHECK (y.size() == 2 * 66150u);
  11136. 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 } };
  11137. for (const auto& g : golden)
  11138. 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);
  11139. double e = 0.0;
  11140. for (auto v : y)
  11141. e += (double) v * v;
  11142. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.307227390984) < 1e-6);
  11143. // the instrument: same engine behind LYNX's channel gains (V = 1, centre → law A), velocity → Init.Vol
  11144. lynx::ChannelDef c;
  11145. c.instrument = lynx::InstrumentKind::toxic;
  11146. c.pluginState = blob;
  11147. auto inst = lynx::makeInstrument (lynx::InstrumentKind::toxic);
  11148. inst->configure (c, 140.0);
  11149. inst->prepare (44100.0);
  11150. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  11151. for (int b = 0; b < 16; ++b)
  11152. {
  11153. if (b == 0)
  11154. inst->noteOn (60, 0.78125f);
  11155. if (b == 10)
  11156. inst->noteOff (60);
  11157. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  11158. }
  11159. 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 } },
  11160. 44100.0, 8192, "", 512, 140.0);
  11161. double diff = 0.0, energy = 0.0;
  11162. for (int i = 0; i < 8192; ++i)
  11163. {
  11164. 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]) });
  11165. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  11166. }
  11167. CHECK (diff == 0.0 && energy > 1e-3);
  11168. }
  11169. TEST (morphine_engine)
  11170. {
  11171. const auto blob = morphineTestState();
  11172. dumpIfAsked ("LYNX_DUMP_MORPHINE", blob);
  11173. CHECK (lynx::morphineStateUsable (blob));
  11174. CHECK (lynx::morphineNoiseName (blob).empty());
  11175. const auto at = lynx::morphineParamOffset (blob, 9);
  11176. CHECK (at > 0);
  11177. float a9 = 0.0f;
  11178. std::memcpy (&a9, blob.data() + at, 4);
  11179. CHECK (a9 == 0.2f && lynx::morphineParamOffset (blob, 40) < 0);
  11180. auto broken = blob;
  11181. broken[12] = 'X'; // the MPR0 tag
  11182. CHECK (! lynx::morphineStateUsable (broken));
  11183. CHECK (! lynx::morphineStateUsable (std::vector<std::uint8_t> (blob.begin(), blob.end() - 10)));
  11184. // golden samples of our round-39 model (mormodel.Synth, tempo 120, 44.1 kHz, host blocks of 512): keys 60/64/67,
  11185. // Init.Vol 1.0/0.5/2.0, note-offs at 30000/50000/70000
  11186. const std::vector<lynx::morphine::RefNote> notes = { { 0, 60, 1.0, 1.0, 0.0, 0.0, 30000 },
  11187. { 11025, 64, 0.5, 1.0, 0.0, 0.0, 50000 },
  11188. { 22050, 67, 2.0, 1.0, 0.0, 0.0, 70000 } };
  11189. const auto y = lynx::morphine::renderReference (blob, notes, 44100.0, 80000, "");
  11190. CHECK (y.size() == 2 * 80000u);
  11191. 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 } };
  11192. for (const auto& g : golden)
  11193. 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);
  11194. double e = 0.0;
  11195. for (auto v : y)
  11196. e += (double) v * v;
  11197. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.18467099041) < 1e-6);
  11198. // the instrument: same engine behind LYNX's channel gains
  11199. lynx::ChannelDef c;
  11200. c.instrument = lynx::InstrumentKind::morphine;
  11201. c.pluginState = blob;
  11202. auto inst = lynx::makeInstrument (lynx::InstrumentKind::morphine);
  11203. inst->configure (c, 120.0);
  11204. inst->prepare (44100.0);
  11205. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  11206. for (int b = 0; b < 16; ++b)
  11207. {
  11208. if (b == 0)
  11209. inst->noteOn (60, 0.78125f);
  11210. if (b == 10)
  11211. inst->noteOff (60);
  11212. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  11213. }
  11214. 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, "");
  11215. double diff = 0.0, energy = 0.0;
  11216. for (int i = 0; i < 8192; ++i)
  11217. {
  11218. 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]) });
  11219. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  11220. }
  11221. CHECK (diff == 0.0 && energy > 1e-3);
  11222. }
  11223. namespace
  11224. {
  11225. // A MiniSynth state written from scratch (REA_rodada41 §1.1 layout: "SSLF" + the .flms preset)
  11226. std::vector<std::uint8_t> miniSynthTestState (const std::array<float, 43>& P)
  11227. {
  11228. const std::string name = "LYNX test";
  11229. Bytes body;
  11230. body.raw ("\x99" "FLS\r\n\x1a\n", 8);
  11231. body.raw ("HEAD", 4);
  11232. body.i32 (6 + 2 * (std::int32_t) name.size());
  11233. const std::uint16_t head[3] = { 1, 1, (std::uint16_t) name.size() };
  11234. body.raw (reinterpret_cast<const char*> (head), 6);
  11235. for (char ch : name)
  11236. body.raw (&ch, 1), body.zeros (1);
  11237. body.raw ("DATA", 4);
  11238. body.i32 (176);
  11239. body.raw ("0rev", 4);
  11240. for (auto v : P)
  11241. body.f32 (v);
  11242. Bytes s;
  11243. s.raw ("SSLF", 4);
  11244. s.i32 ((std::int32_t) body.b.size());
  11245. s.bytes (body.b);
  11246. return s.b;
  11247. }
  11248. std::array<float, 43> miniSynthDefaults()
  11249. {
  11250. 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,
  11251. 0.5f, 0, 0.25f, 0.5f, 0.5f, 0, 0.11222222f, 0.4f, 0.1f, 1, 0.5f };
  11252. }
  11253. // per-process suffix for scratch files: several test binaries (one per worktree) may run at the same time, and a
  11254. // fixed name let one process read a file another one was rewriting
  11255. std::string processTempTag()
  11256. {
  11257. static const std::string tag = std::to_string (std::random_device {}()) + "_"
  11258. + std::to_string (std::chrono::steady_clock::now().time_since_epoch().count());
  11259. static const struct Cleanup
  11260. {
  11261. ~Cleanup()
  11262. {
  11263. std::error_code ec;
  11264. std::filesystem::remove (std::filesystem::temp_directory_path (ec) / ("lynx_test_oscdata_" + tag + ".dat"), ec);
  11265. std::filesystem::remove_all (std::filesystem::temp_directory_path (ec) / ("lynx_bassdrum_test_" + tag), ec);
  11266. }
  11267. } cleanup;
  11268. (void) cleanup;
  11269. return tag;
  11270. }
  11271. // a synthetic oscdata.dat (amplitudes 0.3/k, phases −π/2 + 0.001·(k − 1)): the test does not need an FL installation
  11272. std::string miniSynthTestOscdata()
  11273. {
  11274. const auto path = (std::filesystem::temp_directory_path() / ("lynx_test_oscdata_" + processTempTag() + ".dat")).string();
  11275. std::vector<float> d (2048);
  11276. for (int h = 0; h < 1024; ++h)
  11277. d[(std::size_t) h] = (float) (0.3 / (h + 1)), d[1024 + (std::size_t) h] = (float) (-1.5707963267948966 + 0.001 * h);
  11278. std::ofstream out (path, std::ios::binary);
  11279. out.write (reinterpret_cast<const char*> (d.data()), 8192);
  11280. return path;
  11281. }
  11282. } // namespace
  11283. TEST (minisynth_engine)
  11284. {
  11285. auto A = miniSynthDefaults(), B = miniSynthDefaults();
  11286. // A: poly, SQR+SQR, noise, filter env/velocity/keytracking, LFO → cutoff, distortion + decimator, delay, chorus, phaser
  11287. 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 },
  11288. { 12, 0.1f }, { 13, 0.4f }, { 14, 0.3f }, { 15, 0.5f }, { 16, 0.8f }, { 17, 0.6f }, { 18, 0.1f },
  11289. { 19, 0.5f }, { 20, 0.85f }, { 21, 0.05f }, { 22, 0.5f }, { 23, 0.6f }, { 24, 0.4f }, { 25, 1 },
  11290. { 26, 0.3f }, { 27, 0.1f }, { 28, 1 }, { 29, 0.4f }, { 30, 0.5f }, { 31, 0.6f }, { 32, 0.6f },
  11291. { 33, 1 }, { 34, 0.4f }, { 35, 0.6f }, { 36, 0.5f }, { 37, 1 }, { 38, 0.3f }, { 39, 0.6f },
  11292. { 40, 0.5f }, { 41, 0.7f }, { 42, 0.4f } };
  11293. // B: lead (legato glide), BELL, LFO → pitch
  11294. 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 },
  11295. { 17, 0.1f }, { 18, 0.5f }, { 19, 0.6f }, { 20, 0.7f }, { 22, 0.6f }, { 23, 0.5f }, { 24, 0.3f } };
  11296. for (const auto& [i, v] : a)
  11297. A[(std::size_t) i] = v;
  11298. for (const auto& [i, v] : b)
  11299. B[(std::size_t) i] = v;
  11300. const auto blobA = miniSynthTestState (A), blobB = miniSynthTestState (B);
  11301. dumpIfAsked ("LYNX_DUMP_MINISYNTH", blobA);
  11302. CHECK (lynx::miniSynthStateUsable (blobA) && lynx::miniSynthParams (blobA).size() == 43u && lynx::miniSynthParams (blobA)[26] == 0.3f);
  11303. CHECK (lynx::miniSynthParamOffset (blobA, 9) == 8 + 8 + 8 + 6 + 18 + 12 + 4 * 9 && lynx::miniSynthParamOffset (blobA, 43) < 0);
  11304. CHECK (! lynx::miniSynthStateUsable (std::vector<std::uint8_t> (blobA.begin(), blobA.begin() + 20)));
  11305. auto shortData = blobA;
  11306. shortData.resize (shortData.size() - 4 * 40); // only 3 parameters present: the rest keeps the defaults
  11307. CHECK (lynx::miniSynthParams (shortData)[20] == 0.5f && lynx::miniSynthParams (shortData)[1] == 0.5f);
  11308. // golden samples of our round-41 model (msmodel.Plugin on the same synthetic oscdata, 44.1 kHz, host blocks of 512,
  11309. // L = R = Final.Vol): C5 vel 100 0–30000, E5 vel 80 11025–40000, G5 vel 110 22050–52000 (Init.Vol law A)
  11310. const auto osc = miniSynthTestOscdata();
  11311. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  11312. const std::vector<lynx::minisynth::RefNote> notes = { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 },
  11313. { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  11314. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 } };
  11315. struct Golden { int i; float l, r; };
  11316. 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 } };
  11317. 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 } };
  11318. for (int which = 0; which < 2; ++which)
  11319. {
  11320. const auto y = lynx::minisynth::renderReference (which == 0 ? blobA : blobB, notes, 44100.0, 66150, osc);
  11321. CHECK (y.size() == 2 * 66150u);
  11322. if (y.size() != 2 * 66150u)
  11323. continue;
  11324. for (const auto& g : which == 0 ? std::vector (std::begin (goldenA), std::end (goldenA)) : std::vector (std::begin (goldenB), std::end (goldenB)))
  11325. CHECK (y[2 * (std::size_t) g.i] == g.l && y[2 * (std::size_t) g.i + 1] == g.r);
  11326. double e = 0.0;
  11327. for (auto v : y)
  11328. e += (double) v * v;
  11329. CHECK (std::abs (std::sqrt (e / (double) y.size()) - (which == 0 ? 0.315552434392 : 0.03926116287)) < 1e-9);
  11330. }
  11331. // missing oscdata.dat: silent tables, still finite
  11332. const auto silent = lynx::minisynth::renderReference (blobB, notes, 44100.0, 4096, "");
  11333. CHECK (silent.size() == 2 * 4096u && std::all_of (silent.begin(), silent.end(), [] (float v) { return std::isfinite (v) && std::abs (v) < 1.0f; }));
  11334. // the instrument: same engine behind LYNX's channel gains (V = 1, centre → law A), velocity → Init.Vol
  11335. lynx::ChannelDef c;
  11336. c.instrument = lynx::InstrumentKind::minisynth;
  11337. c.pluginState = blobA;
  11338. c.samplePath = osc;
  11339. auto inst = lynx::makeInstrument (lynx::InstrumentKind::minisynth);
  11340. inst->configure (c, 120.0);
  11341. inst->prepare (44100.0);
  11342. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  11343. for (int k = 0; k < 16; ++k)
  11344. {
  11345. if (k == 0)
  11346. inst->noteOn (60, 0.78125f);
  11347. if (k == 10)
  11348. inst->noteOff (60);
  11349. inst->render (l.data() + 512 * k, r.data() + 512 * k, 512, 1.0f, 1.0f);
  11350. }
  11351. 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 } },
  11352. 44100.0, 8192, osc, 512, 120.0, 0, -1.0, true);
  11353. double diff = 0.0, energy = 0.0;
  11354. for (int i = 0; i < 8192; ++i)
  11355. {
  11356. 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]) });
  11357. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  11358. }
  11359. CHECK (diff == 0.0 && energy > 1e-3 && ! inst->idle());
  11360. }
  11361. namespace
  11362. {
  11363. // A PoiZone state written from scratch (REA_rodada44 §1.3 layout, "Ver4" + 156 floats + name): pulse A synced to saw B,
  11364. // noise, envelope to the cutoff, triangle LFO to oscillator A's pitch, 3-voice unison, gate, chorus and tape delay.
  11365. std::vector<std::uint8_t> poizoneTestState()
  11366. {
  11367. std::array<float, 156> P {};
  11368. 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 },
  11369. { 10, 0.8f }, { 11, 0.3f }, { 12, 0.5f }, { 13, 0.4f }, { 14, 0.5f }, { 15, 0.5f }, { 16, 0.5f }, { 17, 0.5f },
  11370. { 18, 0.35f }, { 19, 0.6f }, { 20, 0.5f }, { 21, 0.3f }, { 22, 0.2f }, { 23, 0.6f }, { 24, 0.7f }, { 25, 0.5f },
  11371. { 26, 0.7f }, { 28, 0.6f }, { 29, 0.3f }, { 30, 0.8f }, { 31, 1.0f }, { 32, 0.3f }, { 33, 0.5f }, { 34, 0.5f },
  11372. { 35, 0.3f }, { 37, 1.0f }, { 38, 0.6f }, { 39, 1.0f }, { 40, 0.4f }, { 41, 0.5f }, { 42, 0.5f }, { 43, 0.5f },
  11373. { 47, 0.5f }, { 48, 0.3f }, { 67, 0.2f }, { 68, 0.5f }, { 75, 2.0f }, { 76, 15.0f }, { 77, 1.0f }, { 78, 1.0f },
  11374. { 79, 1.0f } };
  11375. for (const auto& [i, v] : values)
  11376. P[(std::size_t) i] = v;
  11377. for (int i = 49; i < 65; ++i)
  11378. P[(std::size_t) i] = (i - 49) % 3 != 2 ? 1.0f : 0.0f;
  11379. for (int i = 81; i < 155; ++i)
  11380. P[(std::size_t) i] = -1.0f;
  11381. std::vector<std::uint8_t> blob { 'S', 'S', 'L', 'F', 0xb4, 0x02, 0x00, 0x00, '4', 'r', 'e', 'V' };
  11382. blob.resize (12 + 4 * 156 + 64, 0);
  11383. std::memcpy (blob.data() + 12, P.data(), 4 * 156);
  11384. std::memcpy (blob.data() + 12 + 4 * 156, "LYNX test", 9);
  11385. return blob;
  11386. }
  11387. } // namespace
  11388. TEST (poizone_engine)
  11389. {
  11390. const auto blob = poizoneTestState();
  11391. CHECK (lynx::poizoneStateUsable (blob));
  11392. CHECK (lynx::poizoneParamOffset (blob, 18) == 12 + 4 * 18 && lynx::poizoneParamOffset (blob, 156) < 0);
  11393. CHECK (lynx::poizoneParam (blob, 75) == 2.0f && ! lynx::poizoneParam (blob, -1));
  11394. auto broken = blob;
  11395. broken[8] = 'X'; // the "Ver4" tag
  11396. CHECK (! lynx::poizoneStateUsable (broken));
  11397. CHECK (! lynx::poizoneStateUsable (std::vector<std::uint8_t> (blob.begin(), blob.begin() + 10)));
  11398. // golden samples of our round-44 model (pzmodel.Plugin, tempo 120, 44.1 kHz, host blocks of 512, L = R = Final.Vol):
  11399. // C5 vel 100 0–30000, E5 vel 80 11025–40000, G5 vel 110 22050–52000 (Init.Vol = (11^(vel/100) − 1)/10)
  11400. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  11401. const std::vector<lynx::poizone::RefNote> notes = { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 },
  11402. { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  11403. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 } };
  11404. const auto y = lynx::poizone::renderReference (blob, notes, 44100.0, 66150);
  11405. CHECK (y.size() == 2 * 66150u);
  11406. 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 } };
  11407. for (const auto& g : golden)
  11408. 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);
  11409. double e = 0.0;
  11410. for (auto v : y)
  11411. e += (double) v * v;
  11412. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.10990940878400073) < 1e-7);
  11413. // the instrument: same engine behind LYNX's channel gains (V = 1, centre → law A), velocity → Init.Vol
  11414. lynx::ChannelDef c;
  11415. c.instrument = lynx::InstrumentKind::poizone;
  11416. c.pluginState = blob;
  11417. auto inst = lynx::makeInstrument (lynx::InstrumentKind::poizone);
  11418. inst->configure (c, 120.0);
  11419. inst->prepare (44100.0);
  11420. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  11421. for (int b = 0; b < 16; ++b)
  11422. {
  11423. if (b == 0)
  11424. inst->noteOn (60, 0.78125f);
  11425. if (b == 10)
  11426. inst->noteOff (60);
  11427. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  11428. }
  11429. 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,
  11430. 512, 120.0, 0, true);
  11431. double diff = 0.0, energy = 0.0;
  11432. for (int i = 0; i < 8192; ++i)
  11433. {
  11434. 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]) });
  11435. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  11436. }
  11437. CHECK (diff == 0.0 && energy > 1e-3);
  11438. }
  11439. // ---- Fruit Kick / BassDrum (REA_rodada43): synthetic states and data, golden values of our round-43 models ----
  11440. namespace
  11441. {
  11442. std::vector<std::uint8_t> fruitKickStateA()
  11443. {
  11444. const std::int32_t w[8] = { 0, 0, -600, 64, 128, 32, 20, 0 };
  11445. std::vector<std::uint8_t> b (32);
  11446. std::memcpy (b.data(), w, 32);
  11447. return b;
  11448. }
  11449. std::vector<std::uint8_t> fruitKickStateB()
  11450. {
  11451. std::vector<std::uint8_t> b (400, 0);
  11452. const std::int32_t head[7] = { 1, 624, -8, 42, 86, 64, 0 };
  11453. std::memcpy (b.data(), head, sizeof (head));
  11454. std::int32_t recs[5][17];
  11455. for (auto& r : recs)
  11456. {
  11457. const std::int32_t d[17] = { 0, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, 0 };
  11458. std::memcpy (r, d, sizeof (d));
  11459. }
  11460. const std::int32_t pan[17] = { 0, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 3000, 64, 20000, 1, 0, 0, 0 };
  11461. const std::int32_t vol[17] = { 4, -1, 100, 3000, 100, 25000, 64, 15000, 0, 100, 20000, 0, 32950, 0, 0, 40, -101 };
  11462. std::memcpy (recs[0], pan, sizeof (pan));
  11463. std::memcpy (recs[1], vol, sizeof (vol));
  11464. for (int k = 0; k < 5; ++k)
  11465. std::memcpy (b.data() + 32 + 68 * k, recs[k], 68);
  11466. const std::int32_t cut = 256;
  11467. std::memcpy (b.data() + 32 + 0x15c, &cut, 4);
  11468. return b;
  11469. }
  11470. std::string bassDrumTestFolder()
  11471. {
  11472. // our own oscillator table and a pseudo-random 16-bit noise sample, laid out like FL's BassDrum folder
  11473. const auto dir = std::filesystem::temp_directory_path() / ("lynx_bassdrum_test_" + processTempTag());
  11474. std::filesystem::create_directories (dir / "Noise Samples");
  11475. auto wav = [] (const std::filesystem::path& path, const void* data, std::uint32_t bytes, std::uint16_t fmt, std::uint16_t bits) {
  11476. std::ofstream o (path, std::ios::binary);
  11477. auto u32 = [&] (std::uint32_t v) { o.write ((const char*) &v, 4); };
  11478. auto u16 = [&] (std::uint16_t v) { o.write ((const char*) &v, 2); };
  11479. o.write ("RIFF", 4), u32 (36 + bytes), o.write ("WAVEfmt ", 8), u32 (16), u16 (fmt), u16 (1), u32 (44100),
  11480. u32 (44100u * bits / 8), u16 ((std::uint16_t) (bits / 8)), u16 (bits), o.write ("data", 4), u32 (bytes);
  11481. o.write ((const char*) data, bytes);
  11482. };
  11483. std::vector<float> table (1024);
  11484. for (int i = 0; i < 1024; ++i)
  11485. 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));
  11486. wav (dir / "sounddata", table.data(), 4096, 3, 32);
  11487. std::vector<std::int16_t> noise (3000);
  11488. std::uint32_t s = 12345;
  11489. for (auto& v : noise)
  11490. {
  11491. s = (s * 1103515245u + 12345u) & 0x7fffffffu;
  11492. v = (std::int16_t) ((int) ((s >> 8) % 20001u) - 10000);
  11493. }
  11494. wav (dir / "Noise Samples" / "test noise.wav", noise.data(), 6000, 1, 16);
  11495. return dir.string();
  11496. }
  11497. std::vector<std::uint8_t> bassDrumTestState()
  11498. {
  11499. std::vector<std::uint8_t> b (0x50C + 92, 0);
  11500. std::memcpy (b.data(), "SSLF", 4);
  11501. const std::uint32_t size = 0x560;
  11502. std::memcpy (b.data() + 4, &size, 4);
  11503. std::memcpy (b.data() + 8, "2kiK", 4);
  11504. std::memcpy (b.data() + 12, "Test kick", 9);
  11505. const char path[] = "C:/Somewhere/Noise Samples/test noise.wav";
  11506. std::memcpy (b.data() + 268, path, sizeof (path) - 1);
  11507. 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,
  11508. 0.5f, 0.8f, 0.15f, 0.9f, 0.3f, 0.5f, 0.6f, 0.62f, 0.4f, 0.5f, 0.85f };
  11509. std::memcpy (b.data() + 0x50C, P, sizeof (P));
  11510. return b;
  11511. }
  11512. struct Golden
  11513. {
  11514. int i;
  11515. float l, r;
  11516. };
  11517. bool matchesGolden (const std::vector<float>& y, std::initializer_list<Golden> golden, double rms)
  11518. {
  11519. bool ok = true;
  11520. for (const auto& g : golden)
  11521. 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;
  11522. double e = 0.0;
  11523. for (auto v : y)
  11524. e += (double) v * v;
  11525. return ok && std::abs (std::sqrt (e / (double) y.size()) - rms) < 1e-6;
  11526. }
  11527. } // namespace
  11528. TEST (fruitkick_engine)
  11529. {
  11530. const auto A = fruitKickStateA(), B = fruitKickStateB();
  11531. CHECK (lynx::fruitKickStateUsable (A) && lynx::fruitKickStateUsable (B));
  11532. CHECK (! lynx::fruitKickStateUsable (std::vector<std::uint8_t> (A.begin(), A.begin() + 20)));
  11533. CHECK (lynx::fruitKickParam (A, 1) == -600 && lynx::fruitKickParam (B, 0) == 624);
  11534. CHECK (lynx::fruitKickFallbacks (A).empty() && lynx::fruitKickFallbacks (B).empty());
  11535. // a version-0 state takes the channel's envelope records as its snapshot (and becomes version 1)
  11536. std::vector<std::vector<std::uint8_t>> recs;
  11537. for (int k = 0; k < 5; ++k)
  11538. recs.emplace_back (B.begin() + 32 + 68 * k, B.begin() + 32 + 68 * (k + 1));
  11539. const auto withEnv = lynx::fruitKickWithChannelEnvelope (A, recs, 256, 0, 0);
  11540. CHECK (withEnv.size() == 400 && lynx::fruitKickParam (withEnv, 5) == 20 && std::memcmp (withEnv.data() + 32, B.data() + 32, 364) == 0);
  11541. CHECK (lynx::fruitKickWithChannelEnvelope (B, recs, 256, 0, 0) == B);
  11542. // golden samples of our round-43 model (fkmodel.run, 44.1 kHz, host blocks of 512): C5 at 0 held, C6 at 3000
  11543. // (Final.Vol 0.5, pan 0.3) released at 9000; B with the plugin's tick every 230 samples from 115
  11544. 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 } };
  11545. const auto ya = lynx::fruitkick::renderReference (A, notes, 44100.0, 20000);
  11546. CHECK (ya.size() == 40000u);
  11547. // (A uses the distortion: values since REA_rodada55 with FL's x87 live host waveshaper, checked against FLEngine's own;
  11548. // the release fade since REA_rodada74 with IPP's cosine, checked against the DLL)
  11549. CHECK (matchesGolden (ya, { { 10, 0.803601742f, 0.803601742f }, { 700, 0.185467482f, 0.185467482f }, { 2900, -0.63492614f, -0.63492614f },
  11550. { 3100, 0.339092523f, 0.544403195f }, { 5000, -0.0974622667f, -0.150664076f }, { 9100, 0.124622017f, 0.0544089526f },
  11551. { 9400, -0.146487325f, -0.144867823f }, { 15000, 0.009439799f, 0.009439799f } },
  11552. 0.284267665614));
  11553. std::vector<int> ticks;
  11554. for (int t = 115; t < 20000; t += 230)
  11555. ticks.push_back (t);
  11556. const auto yb = lynx::fruitkick::renderReference (B, notes, 44100.0, 20000, 512, ticks);
  11557. CHECK (matchesGolden (yb, { { 10, -0.00760388654f, -0.00760388654f }, { 700, 0.386696398f, 0.442232877f }, { 2900, -0.123097703f, -0.302172035f },
  11558. { 3100, 0.0523624197f, 0.0877688825f }, { 5000, 0.045177035f, 0.100505695f }, { 9100, -0.00423920294f, 0.00810315367f },
  11559. { 9400, 0.00482427329f, -0.010199016f }, { 15000, -0.000171193431f, 1.01026744e-05f } },
  11560. 0.117964927585));
  11561. // the instrument: same engine behind LYNX's channel gains (V = 1, centre), velocity → Init.Vol (link velocity on)
  11562. lynx::ChannelDef c;
  11563. c.instrument = lynx::InstrumentKind::fruitkick;
  11564. c.pluginState = A;
  11565. auto inst = lynx::makeInstrument (lynx::InstrumentKind::fruitkick);
  11566. inst->configure (c, 140.0);
  11567. inst->prepare (44100.0);
  11568. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  11569. for (int b = 0; b < 16; ++b)
  11570. {
  11571. if (b == 0)
  11572. inst->noteOn (60, 0.78125f);
  11573. if (b == 10)
  11574. inst->noteOff (60);
  11575. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  11576. }
  11577. const auto iv = (std::pow (11.0, 1.28 * 0.78125) - 1.0) / 10.0;
  11578. const auto ref = lynx::fruitkick::renderReference (A, { { 0, 60, iv, 0.0, 0.0, 5120 } }, 44100.0, 8192);
  11579. double diff = 0.0, energy = 0.0;
  11580. for (int i = 0; i < 8192; ++i)
  11581. {
  11582. 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]) });
  11583. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  11584. }
  11585. CHECK (diff < 1e-6 && energy > 1e-2);
  11586. }
  11587. TEST (ipp_cos_and_end_fade)
  11588. {
  11589. // ippsCos_32f_A11 of the installation's dsp_ippv2_x64.dll (REA_rodada74 §1): outputs the DLL gave on its two paths
  11590. const auto bits = [] (float v) { return ippcos::floatToBits (v); };
  11591. const auto f = ippcos::bitsToFloat;
  11592. CHECK (bits (ippcos::cosFma (0.1f)) == 0x3f7eaf8bu && bits (ippcos::cosFma (1.0f)) == 0x3f0a534cu);
  11593. CHECK (bits (ippcos::cosFma (2.0f)) == 0xbed51118u && bits (ippcos::cosFma (-3.0f)) == 0xbf7d6646u);
  11594. CHECK (bits (ippcos::cosFma (f (0x3e000007))) == 0x3f7df6f9u && bits (ippcos::cosNoFma (f (0x3e000007))) == 0x3f7df6f8u);
  11595. CHECK (bits (ippcos::cosFma (f (0x3e00007c))) == 0x3f7df6f4u && bits (ippcos::cosNoFma (f (0x3e00007c))) == 0x3f7df6f5u);
  11596. CHECK (ippcos::cosFma (1e-9f) == 1.0f && ippcos::cosNoFma (0.0f) == 1.0f);
  11597. // the portable fused multiply-add rounds once: (1 + 2^-12)^2 = 1 + 2^-11 + 2^-24 is a float halfway case that a
  11598. // tiny addend (lost in a plain double sum) must decide
  11599. const float a = 1.0f + 0x1p-12f;
  11600. CHECK (ippcos::fusedMulAdd (a, a, 0x1p-60f) == 1.0f + 0x1p-11f + 0x1p-23f);
  11601. CHECK (ippcos::fusedMulAdd (a, a, -0x1p-60f) == 1.0f + 0x1p-11f && ippcos::fusedMulAdd (a, a, 0.0f) == 1.0f + 0x1p-11f);
  11602. CHECK (ippcos::fusedMulAdd (3.0f, 5.0f, -1.0f) == 14.0f);
  11603. // the end-fade table: within 1.5e-4 of the exact cosine, ends near zero
  11604. std::vector<float> t (441);
  11605. ippcos::endFadeTable (t.data(), 441);
  11606. double worst = 0.0;
  11607. for (int i = 0; i < 441; ++i)
  11608. worst = std::max (worst, std::abs ((double) t[(std::size_t) i] - 0.5 * (1.0 + std::cos ((i + 1) * 3.141592653589793 / 442.0))));
  11609. CHECK (worst < 1.5e-4 && t[0] > 0.9999f && t[440] < 1e-4f);
  11610. }
  11611. TEST (bassdrum_engine)
  11612. {
  11613. // FL's CRT powf as the engine computes it: a case where its last bit differs from the correctly rounded power
  11614. const float x = 0.87011606f;
  11615. std::uint32_t bits = 0;
  11616. const auto p = lynx::bassdrum::powfMsvc (x, 0.125f);
  11617. std::memcpy (&bits, &p, 4);
  11618. CHECK (bits == 1065063964u);
  11619. CHECK (lynx::bassdrum::powfMsvc (2.0f, 1.0f) == 2.0f && lynx::bassdrum::powfMsvc (10.0f, 0.0f) == 1.0f);
  11620. const auto st = bassDrumTestState();
  11621. CHECK (lynx::bassDrumStateUsable (st) && lynx::bassDrumNoiseName (st) == "test noise.wav");
  11622. CHECK (lynx::bassDrumParamOffset (st, 6) == 0x50C + 24 && lynx::bassDrumParamOffset (st, 23) < 0);
  11623. auto broken = st;
  11624. broken[8] = 'X';
  11625. CHECK (! lynx::bassDrumStateUsable (broken));
  11626. // golden samples of our round-43 model (bdmodel.run with its powf port, 44.1 kHz, host blocks of 512)
  11627. const auto dir = bassDrumTestFolder();
  11628. 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 },
  11629. { 6100, 55, 1.5, 1.0, -0.3, 0.0, -1 } };
  11630. const auto y = lynx::bassdrum::renderReference (st, notes, 44100.0, 30000, dir);
  11631. CHECK (y.size() == 60000u);
  11632. CHECK (matchesGolden (y, { { 50, 0.0122119058f, 0.0122119058f }, { 400, -0.0886279643f, -0.0886279643f }, { 3000, -0.0428197235f, -0.0428197235f },
  11633. { 6050, 0.0199260078f, 0.0240648985f }, { 6300, -0.257363439f, -0.185516387f }, { 9000, 0.268954486f, 0.10506025f },
  11634. { 15000, -0.0102827176f, -0.0380715057f }, { 25000, 0.0f, 0.0f } },
  11635. 0.230007588169));
  11636. CHECK (lynx::bassdrum::renderReference (st, notes, 44100.0, 100, dir + "/nowhere").empty()); // no table: no engine
  11637. lynx::ChannelDef c;
  11638. c.instrument = lynx::InstrumentKind::bassdrum;
  11639. c.pluginState = st;
  11640. c.samplePath = dir;
  11641. auto inst = lynx::makeInstrument (lynx::InstrumentKind::bassdrum);
  11642. inst->configure (c, 120.0);
  11643. inst->prepare (44100.0);
  11644. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  11645. for (int b = 0; b < 16; ++b)
  11646. {
  11647. if (b == 0)
  11648. inst->noteOn (60, 0.78125f);
  11649. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  11650. }
  11651. 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);
  11652. double diff = 0.0, energy = 0.0;
  11653. for (int i = 0; i < 8192; ++i)
  11654. {
  11655. 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]) });
  11656. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  11657. }
  11658. CHECK (diff == 0.0 && energy > 1e-2);
  11659. }
  11660. // REA_rodada55: Fruity Fast Dist, Fruity Flangus and Fruity Flanger, goldens from the installation's DLLs (EXEC, our own states)
  11661. TEST (fruity_fast_dist_flangus_flanger)
  11662. {
  11663. std::vector<float> inL (4096), inR (4096);
  11664. for (int n = 0; n < 4096; ++n)
  11665. {
  11666. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  11667. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  11668. }
  11669. struct Gold { int i; float l, r; };
  11670. auto close = [] (float a, float b) { return std::abs (a - b) <= 2.0e-7f * std::max (1.0f, std::abs (b)); };
  11671. // Fast Dist (+ FL's host waveshaper): live and render kernels
  11672. auto dist = [&] (lynx::fastdistfx::State s, bool render, std::initializer_list<Gold> gold) {
  11673. std::vector<float> l (inL), r (inR);
  11674. lynx::fastdistfx::process (l.data(), 4096, s, render);
  11675. lynx::fastdistfx::process (r.data(), 4096, s, render);
  11676. for (const auto& g : gold)
  11677. CHECK (close (l[(std::size_t) g.i], g.l) && close (r[(std::size_t) g.i], g.r));
  11678. };
  11679. dist ({ 128, 10, 0, 128, 128 }, false, { { 100, 0.0f, -0.39352417f }, { 1000, -0.39352417f, 0.389404297f }, { 2047, 0.376953125f, -0.39352417f },
  11680. { 3000, -0.39352417f, -0.379760742f }, { 4095, 0.342590332f, 0.373931885f } });
  11681. dist ({ 128, 10, 0, 128, 128 }, true, { { 3000, -0.39352417f, -0.379751563f }, { 4095, 0.342590332f, 0.373931885f } });
  11682. dist ({ 192, 4, 1, 77, 97 }, false, { { 100, 0.0f, -0.868428111f }, { 1000, -1.23000622f, 0.740812242f }, { 2047, 0.58129251f, -0.855666518f },
  11683. { 3000, -1.23000622f, -0.613196433f }, { 4095, 0.307311207f, 0.549388587f } });
  11684. dist ({ 192, 4, 1, 77, 97 }, true, { { 2047, 0.58129245f, -0.855666518f }, { 4095, 0.307311267f, 0.549388528f } });
  11685. dist ({ 90, 7, 0, 40, 120 }, false, { { 100, 0.0f, -0.346225947f }, { 1000, -0.511893928f, 0.287246376f }, { 2047, 0.212910682f, -0.340344131f },
  11686. { 3000, -0.511893928f, -0.227848753f }, { 4095, 0.0910475403f, 0.197926477f } });
  11687. dist ({ 90, 7, 0, 40, 120 }, true, { { 100, 0.0f, -0.346223623f }, { 1000, -0.511893749f, 0.287246734f }, { 2047, 0.212909341f, -0.340341568f },
  11688. { 3000, -0.511893749f, -0.227846757f }, { 4095, 0.0910428166f, 0.197928891f } });
  11689. {
  11690. const auto* T = lynx::fastdistfx::curve (0, 10);
  11691. CHECK (T[0] == 0 && T[8191] == T[8192] && lynx::fastdistfx::curve (1, 1)[8191] == 32767);
  11692. }
  11693. // Flangus: FL's call order (state, sample rate, block size of 512), no flush
  11694. auto flange = [&] (lynx::flangusfx::State s, int fs, std::initializer_list<Gold> gold) {
  11695. lynx::flangusfx::Engine e;
  11696. e.prepare (fs);
  11697. e.load (s);
  11698. e.setRate (fs);
  11699. e.setBlockSize (512);
  11700. std::vector<float> io (8192);
  11701. for (int n = 0; n < 4096; ++n)
  11702. {
  11703. io[2 * (std::size_t) n] = inL[(std::size_t) n];
  11704. io[2 * (std::size_t) n + 1] = inR[(std::size_t) n];
  11705. }
  11706. for (int o = 0; o < 4096; o += 512)
  11707. e.render (io.data() + 2 * o, io.data() + 2 * o, 512);
  11708. for (const auto& g : gold)
  11709. CHECK (close (io[2 * (std::size_t) g.i], g.l) && close (io[2 * (std::size_t) g.i + 1], g.r));
  11710. };
  11711. flange ({ 4, 512, 768, 8, 128, -32, 96, 96 }, 44100, { { 100, 0.0249633491f, -0.119510099f }, { 1000, -1.26988697f, 0.687134743f },
  11712. { 2047, 1.03176403f, -0.278752804f }, { 3000, -0.940228283f, -0.853936791f },
  11713. { 4095, 0.201686561f, 0.607052445f } });
  11714. flange ({ 7, 300, 900, 40, 30, 100, -60, 128 }, 44100, { { 100, 0.0f, 0.0446268953f }, { 1000, 1.68992829f, 2.06982398f },
  11715. { 2047, 0.400214791f, 0.838502884f }, { 3000, 1.20018816f, 1.18875635f },
  11716. { 4095, -0.354708612f, 0.503573656f } });
  11717. flange ({ 2, 1024, 200, 0, 0, -128, 128, -90 }, 48000, { { 100, 0.172592998f, -0.230506495f }, { 1000, -1.56807125f, 0.854493499f },
  11718. { 2047, 1.02325416f, -1.17055321f }, { 3000, -1.74136555f, -0.535462856f },
  11719. { 4095, 1.49345946f, -0.537866116f } });
  11720. // Flanger: state, then the sample rate (FL's call order)
  11721. auto flanger = [&] (lynx::flangerfx::State s, int fs, std::initializer_list<Gold> gold) {
  11722. lynx::flangerfx::Engine e;
  11723. e.load (s);
  11724. e.prepare (fs);
  11725. std::vector<float> io (8192);
  11726. for (int n = 0; n < 4096; ++n)
  11727. {
  11728. io[2 * (std::size_t) n] = inL[(std::size_t) n];
  11729. io[2 * (std::size_t) n + 1] = inR[(std::size_t) n];
  11730. }
  11731. for (int o = 0; o < 4096; o += 512)
  11732. e.render (io.data() + 2 * o, io.data() + 2 * o, 512);
  11733. for (const auto& g : gold)
  11734. CHECK (close (io[2 * (std::size_t) g.i], g.l) && close (io[2 * (std::size_t) g.i + 1], g.r));
  11735. };
  11736. flanger ({ 0, 2000, 2000, 118, 0, 0, 23, 0, 1024, 3500, 3000, 2500 }, 44100,
  11737. { { 100, -1.22955823f, -1.98144293f }, { 1000, -0.975861192f, 0.646231353f }, { 2047, 0.518505156f, -0.857361794f },
  11738. { 3000, -0.546559215f, -0.0157452822f }, { 4095, 0.104127884f, 0.206255436f } });
  11739. flanger ({ 300, 4000, 4500, 90, 1000, 300, 99, 1024, 1024, 2000, 4000, 1000 }, 48000,
  11740. { { 100, 0.0f, -0.440000027f }, { 1000, 0.24619779f, 0.775473535f }, { 2047, 1.11609459f, -0.0639692694f },
  11741. { 3000, -1.22629714f, -1.41118503f }, { 4095, -0.263562649f, 0.80683732f } });
  11742. // import: FL's states become the exact effects with the controls as parameters; short states keep the approximations
  11743. {
  11744. 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 };
  11745. const auto slot = lynx::effectFromFl ("Fruity Fast Dist", fd, 120.0);
  11746. CHECK (slot && slot->kind == lynx::EffectKind::fruityFastDist);
  11747. if (slot)
  11748. {
  11749. const auto s = lynx::fastdistfx::fromParams (slot->params.data());
  11750. CHECK (s.pre == 192 && s.threshold == 4 && s.type == 1 && s.mix == 77 && s.post == 97);
  11751. auto fx = lynx::makeEffect (*slot);
  11752. fx->prepare (44100.0);
  11753. std::vector<float> l (inL), r (inR);
  11754. fx->process (l.data(), r.data(), 4096, slot->params, 120.0);
  11755. CHECK (l[1000] == -1.23000622f && r[4095] == 0.549388587f);
  11756. }
  11757. std::vector<std::uint8_t> fg (32);
  11758. const int v[8] { 7, 300, 900, 40, 30, 100, -60, 128 };
  11759. std::memcpy (fg.data(), v, 32);
  11760. const auto f2 = lynx::effectFromFl ("Fruity Flangus", fg, 120.0);
  11761. CHECK (f2 && f2->kind == lynx::EffectKind::fruityFlangus);
  11762. if (f2)
  11763. {
  11764. const auto s = lynx::flangusfx::fromParams (f2->params.data());
  11765. for (int i = 0; i < 8; ++i)
  11766. CHECK (s[(std::size_t) i] == v[i]);
  11767. auto fx = lynx::makeEffect (*f2);
  11768. fx->prepare (48000.0);
  11769. std::vector<float> l (inL), r (inR);
  11770. fx->process (l.data(), r.data(), 4096, f2->params, 120.0);
  11771. CHECK (std::isfinite (l[4095]) && std::isfinite (r[4095]) && std::abs (l[2000]) > 0.0f);
  11772. }
  11773. std::vector<std::uint8_t> fl (52);
  11774. const int w[13] { 1, 300, 4000, 4500, 90, 1000, 300, 99, 1024, 1024, 2000, 4000, 1000 };
  11775. std::memcpy (fl.data(), w, 52);
  11776. const auto f3 = lynx::effectFromFl ("Fruity Flanger", fl, 120.0);
  11777. CHECK (f3 && f3->kind == lynx::EffectKind::fruityFlanger);
  11778. if (f3)
  11779. {
  11780. const auto s = lynx::flangerfx::fromParams (f3->params.data());
  11781. for (int i = 0; i < 12; ++i)
  11782. CHECK (s[(std::size_t) i] == w[i + 1]);
  11783. auto fx = lynx::makeEffect (*f3);
  11784. fx->prepare (48000.0);
  11785. std::vector<float> l (inL), r (inR);
  11786. fx->process (l.data(), r.data(), 4096, f3->params, 120.0);
  11787. CHECK (l[1000] == 0.24619779f && r[4095] == 0.80683732f);
  11788. }
  11789. const auto shortFd = lynx::effectFromFl ("Fruity Fast Dist", std::vector<std::uint8_t> (8, 0), 120.0);
  11790. CHECK (! shortFd || shortFd->kind != lynx::EffectKind::fruityFastDist);
  11791. }
  11792. }
  11793. // Workflow P2 (docs/WORKFLOW_FL.md)
  11794. #include "WorkflowP2Tests.inc"
  11795. // Editor mouse grammar (Piano Roll / Playlist)
  11796. #include "EditorGestureTests.inc"
  11797. TEST (chorus_family_engines)
  11798. {
  11799. // REA_rodada59: goldens from the installation's DLLs (44.1 kHz, blocks of 512) on this signal
  11800. std::vector<float> inL (4096), inR (4096);
  11801. for (int n = 0; n < 4096; ++n)
  11802. {
  11803. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0) * 0.5f;
  11804. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0) * 0.5f;
  11805. }
  11806. struct Gold { int i; float l, r; };
  11807. auto close = [] (float a, float b) { return std::abs (a - b) <= 2.0e-7f * std::max (1.0f, std::abs (b)); };
  11808. auto run = [&] (const char* plugin, const std::vector<std::uint8_t>& st, lynx::EffectKind kind, std::initializer_list<Gold> gold) {
  11809. const auto slot = lynx::effectFromFl (plugin, st, 120.0);
  11810. CHECK (slot && slot->kind == kind);
  11811. if (! slot)
  11812. return;
  11813. auto fx = lynx::makeEffect (*slot);
  11814. fx->prepare (44100.0);
  11815. std::vector<float> l (inL), r (inR);
  11816. fx->process (l.data(), r.data(), 1024, slot->params, 120.0);
  11817. fx->process (l.data() + 1024, r.data() + 1024, 3072, slot->params, 120.0);
  11818. for (const auto& g : gold)
  11819. CHECK (close (l[(std::size_t) g.i], g.l) && close (r[(std::size_t) g.i], g.r));
  11820. };
  11821. auto ints = [] (std::initializer_list<int> v) {
  11822. std::vector<std::uint8_t> b;
  11823. for (int x : v)
  11824. for (int k = 0; k < 4; ++k)
  11825. b.push_back ((std::uint8_t) ((unsigned) x >> (8 * k)));
  11826. return b;
  11827. };
  11828. // Fruity Chorus (start phases 0, which FL draws at random)
  11829. run ("Fruity Chorus", ints ({ 1, 512, 2250, 341, 1500, 2500, 3500, 23, 0, 0, 0, 2500, 0 }), lynx::EffectKind::fruityChorus,
  11830. { { 100, 0.0f, -0.3850000202655792f }, { 1000, -0.6181581020355225f, 0.2707735002040863f }, { 2047, -0.01424609124660492f, 0.24228274822235107f },
  11831. { 3000, -1.1650776863098145f, -0.1809898018836975f }, { 4095, -0.012790083885192871f, 0.060571759939193726f } });
  11832. run ("Fruity Chorus", ints ({ 1, 700, 4000, 900, 4000, 3000, 2000, 400, 700, 1000, 30, 4999, 0 }), lynx::EffectKind::fruityChorus,
  11833. { { 100, 0.0f, -0.3850000202655792f }, { 1000, -0.5833333134651184f, 0.36345374584198f }, { 2047, -0.0336097776889801f, -0.3360772430896759f },
  11834. { 3000, -0.40196430683135986f, -0.5073210597038269f }, { 4095, -0.1176539734005928f, 0.444706529378891f } });
  11835. run ("Fruity Chorus", ints ({ 1, 36, 469, 1004, 2832, 293, 3500, 0, 428, 0, 0, 3887, 1024 }), lynx::EffectKind::fruityChorus,
  11836. { { 100, 0.2975943684577942f, 0.3747217655181885f }, { 1000, -0.39463338255882263f, 0.18099871277809143f }, { 2047, 0.3894335627555847f, 0.35111793875694275f },
  11837. { 3000, -0.1450725942850113f, 0.2847272455692291f }, { 4095, 0.33678582310676575f, -0.13365517556667328f } });
  11838. {
  11839. const auto slot = lynx::effectFromFl ("Fruity Chorus", ints ({ 1, 36, 469, 1004, 2832, 293, 3500, 0, 428, 0, 0, 3887, 1024 }), 120.0);
  11840. CHECK (slot && lynx::fruitychorusfx::fromParams (slot->params.data())[2] == 1004);
  11841. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityChorus, 11, 1.0f, 120.0) == "on");
  11842. CHECK (lynx::effectParamFromFl ("Fruity Chorus", 11).has_value() && ! lynx::effectParamFromFl ("Fruity Chorus", 12).has_value());
  11843. }
  11844. // a state FL does not read (version 2) or too short keeps the generic chorus
  11845. CHECK (! lynx::effectFromFl ("Fruity Chorus", ints ({ 2, 512, 2250, 341, 1500, 2500, 3500, 23, 0, 0, 0, 2500, 0 }), 120.0).has_value()
  11846. || lynx::effectFromFl ("Fruity Chorus", ints ({ 2, 512, 2250, 341, 1500, 2500, 3500, 23, 0, 0, 0, 2500, 0 }), 120.0)->kind != lynx::EffectKind::fruityChorus);
  11847. CHECK (! lynx::effectFromFl ("Fruity Chorus", { 1, 0, 0, 0 }, 120.0).has_value()
  11848. || lynx::effectFromFl ("Fruity Chorus", { 1, 0, 0, 0 }, 120.0)->kind != lynx::EffectKind::fruityChorus);
  11849. // Hyper Chorus engine in FL's load sequence (defaults, then the state: the parameters glide), instance k's phases
  11850. auto hyper = [&] (std::initializer_list<int> raws, int instance, std::initializer_list<Gold> gold) {
  11851. namespace hc = lynx::hyperchorusfx;
  11852. hc::Engine e (instance);
  11853. const std::vector<int> r (raws);
  11854. 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); };
  11855. for (int i = 0; i < hc::params; ++i)
  11856. e.setParam (i, val (i, hc::defaults[(std::size_t) i]));
  11857. for (int i = 0; i < hc::params; ++i)
  11858. e.setParam (i, val (i, r[(std::size_t) i]));
  11859. e.setRate (44100.0);
  11860. std::vector<float> io (8192);
  11861. for (int n = 0; n < 4096; ++n)
  11862. io[2 * (std::size_t) n] = inL[(std::size_t) n], io[2 * (std::size_t) n + 1] = inR[(std::size_t) n];
  11863. for (int s = 0; s < 4096; s += 512)
  11864. e.render (io.data() + 2 * s, io.data() + 2 * s, 512);
  11865. for (const auto& g : gold)
  11866. CHECK (close (io[2 * (std::size_t) g.i], g.l) && close (io[2 * (std::size_t) g.i + 1], g.r));
  11867. };
  11868. hyper ({ 562, 919, 499, 505, 709, 716, 1, 19247, 10900, 191, 1000 }, 0,
  11869. { { 100, 0.0f, -0.4115639328956604f }, { 1000, -0.658551037311554f, 0.28647756576538086f }, { 2047, 0.24743860960006714f, -0.5088979005813599f },
  11870. { 3000, -0.5171564221382141f, -0.3630276620388031f }, { 4095, 0.08128821104764938f, 0.18811003863811493f } });
  11871. hyper ({ 1000, 555, 0, 400, 300, 300, 0, 22000, 120, 368, 750 }, 2,
  11872. { { 100, 0.0f, -0.398922324180603f }, { 1000, -1.1528236865997314f, 0.2839684784412384f }, { 2047, 0.5115746855735779f, -0.3330395221710205f },
  11873. { 3000, -0.6094785928726196f, 0.17040175199508667f }, { 4095, -0.11666109412908554f, 0.21931695938110352f } });
  11874. CHECK (lynx::hyperchorusfx::randState (1) == 1u * 214013u + 2531011u);
  11875. {
  11876. // the import: controls as parameters; a song's Hyper Chorus slots are numbered in mixer order
  11877. const auto st = ints ({ 2, 1000, 555, 0, 400, 300, 300, 0, 22000, 120, 368, 750, 0 });
  11878. const auto slot = lynx::effectFromFl ("Hyper Chorus", st, 120.0);
  11879. CHECK (slot && slot->kind == lynx::EffectKind::hyperChorus && lynx::hyperchorusfx::fromParams (slot->params.data())[9] == 368);
  11880. lynx::Song song;
  11881. song.mixer.resize (3);
  11882. song.mixer[1].effects = { *slot, *slot };
  11883. song.mixer[2].effects = { *slot };
  11884. lynx::numberChorusInstances (song);
  11885. CHECK (lynx::hyperchorusfx::instanceOf (song.mixer[2].effects[0].state) == 2);
  11886. auto fx = lynx::makeEffect (song.mixer[2].effects[0]);
  11887. fx->prepare (48000.0);
  11888. std::vector<float> l (inL), r (inR);
  11889. fx->process (l.data(), r.data(), 4096, slot->params, 120.0);
  11890. bool finite = true, wet = false;
  11891. for (int n = 0; n < 4096; ++n)
  11892. {
  11893. finite = finite && std::isfinite (l[(std::size_t) n]) && std::isfinite (r[(std::size_t) n]);
  11894. wet = wet || l[(std::size_t) n] != inL[(std::size_t) n];
  11895. }
  11896. CHECK (finite && wet);
  11897. CHECK (lynx::formatEffectParam (lynx::EffectKind::hyperChorus, 8, 0.0f, 120.0) == "off");
  11898. CHECK (! lynx::effectFromFl ("Hyper Chorus", ints ({ 3, 1000, 555, 0, 400, 300, 300, 0, 22000, 120, 368, 750, 0 }), 120.0).has_value()
  11899. || lynx::effectFromFl ("Hyper Chorus", ints ({ 3, 1000, 555, 0, 400, 300, 300, 0, 22000, 120, 368, 750, 0 }), 120.0)->kind != lynx::EffectKind::hyperChorus);
  11900. }
  11901. // Vintage Chorus engine as FL drives it (created, sample rate, each control), fresh instance (its smoothed values
  11902. // start from the engine's initial ones)
  11903. auto vintage = [&] (std::initializer_list<int> raws, std::initializer_list<Gold> gold) {
  11904. namespace vc = lynx::vintagechorusfx;
  11905. vc::Engine e;
  11906. e.setRate (44100.0);
  11907. int i = 0;
  11908. for (int raw : raws)
  11909. {
  11910. 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));
  11911. ++i;
  11912. }
  11913. std::vector<float> io (8192);
  11914. for (int n = 0; n < 4096; ++n)
  11915. io[2 * (std::size_t) n] = inL[(std::size_t) n], io[2 * (std::size_t) n + 1] = inR[(std::size_t) n];
  11916. for (int s = 0; s < 4096; s += 512)
  11917. e.render (io.data() + 2 * s, io.data() + 2 * s, 512);
  11918. for (const auto& g : gold)
  11919. CHECK (close (io[2 * (std::size_t) g.i], g.l) && close (io[2 * (std::size_t) g.i + 1], g.r));
  11920. };
  11921. vintage ({ 2, 0, 211046, 1, 2411, 960, 1985, 5121, 0, 1, 4420, 6000, 273, 0, 0, 0, 0 },
  11922. { { 100, 0.0f, -0.05606573447585106f }, { 1000, -0.3413679003715515f, 0.20469821989536285f }, { 2047, 0.1487489938735962f, -0.28199973702430725f },
  11923. { 3000, -0.31699514389038086f, -0.43230289220809937f }, { 4095, 0.1255197525024414f, 0.0810704305768013f } });
  11924. vintage ({ 2, 0, 80, 0, 0, 60, 3000, 3000, 0, 0, 0, 3000, 0, 3000, 0, 0, 0 }, // hiss
  11925. { { 100, -1.2527654007499223e-06f, -0.053655341267585754f }, { 1000, -0.44306400418281555f, 0.25606146454811096f }, { 2047, 0.23425261676311493f, -0.21375307440757751f },
  11926. { 3000, -0.5793180465698242f, -0.13371579349040985f }, { 4095, 0.05595953390002251f, 0.11362095922231674f } });
  11927. vintage ({ 0, 1, 49152, 0, 3000, 60, 3000, 3000, 0, 0, 0, 3000, 0, 0, 0, 0, 0 }, // Chorus 1 + 2
  11928. { { 100, 0.0f, -0.05365874245762825f }, { 1000, -0.5095958113670349f, 0.2809423506259918f }, { 2047, 0.17256303131580353f, -0.3563305139541626f },
  11929. { 3000, -0.471851646900177f, -0.30123069882392883f }, { 4095, 0.0959278866648674f, 0.13436323404312134f } });
  11930. {
  11931. const auto st = ints ({ 1, 2, 0, 211046, 1, 2411, 960, 1985, 5121, 0, 1, 4420, 6000, 273, 0, 0, 0, 0 });
  11932. const auto slot = lynx::effectFromFl ("Vintage Chorus", st, 120.0);
  11933. CHECK (slot && slot->kind == lynx::EffectKind::vintageChorus && lynx::vintagechorusfx::fromParams (slot->params.data())[2] == 211046);
  11934. auto fx = lynx::makeEffect (*slot);
  11935. fx->prepare (44100.0);
  11936. std::vector<float> l (inL), r (inR);
  11937. fx->process (l.data(), r.data(), 4096, slot->params, 120.0);
  11938. bool finite = true;
  11939. for (int n = 0; n < 4096; ++n)
  11940. finite = finite && std::isfinite (l[(std::size_t) n]) && std::isfinite (r[(std::size_t) n]);
  11941. CHECK (finite && l[3000] != inL[3000]);
  11942. CHECK (lynx::formatEffectParam (lynx::EffectKind::vintageChorus, 0, 0.0f, 120.0) == "Chorus 1");
  11943. 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()
  11944. || 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);
  11945. }
  11946. }
  11947. #include "UiChromeTests.inc"
  11948. TEST (fruity_delay_engines)
  11949. {
  11950. // REA_rodada52: the engines on their own against the installation's DLLs (goldens = the DLL's output for the same
  11951. // input with FL's set-up order, EXEC: bit-identical), then the mixer effects built from FL states
  11952. const int n = 16384;
  11953. std::vector<float> inL ((std::size_t) n, 0.0f), inR ((std::size_t) n, 0.0f);
  11954. std::uint32_t seed = 1;
  11955. for (int i = 0; i < 4096; ++i) // a noise burst, then the echoes
  11956. {
  11957. seed = seed * 1664525u + 1013904223u;
  11958. inL[(std::size_t) i] = (float) ((int) (seed >> 16) - 32768) / 32768.0f * 0.5f;
  11959. seed = seed * 1664525u + 1013904223u;
  11960. inR[(std::size_t) i] = (float) ((int) (seed >> 16) - 32768) / 32768.0f * 0.5f;
  11961. }
  11962. using Golden = std::vector<std::tuple<int, float, float>>;
  11963. auto check = [&] (const std::vector<float>& l, const std::vector<float>& r, const Golden& golden) {
  11964. for (const auto& [i, gl, gr] : golden)
  11965. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  11966. };
  11967. auto run3 = [&] (const std::array<int, 26>& raw, double bpm) {
  11968. lynx::fldelay::Delay3Engine e (44100.0);
  11969. e.setTempo (bpm);
  11970. for (int i = 0; i < 26; ++i)
  11971. e.setParameter (i, lynx::fldelay::delay3EngineValue (i, raw[(std::size_t) i]));
  11972. e.setSampleRate (44100.0); // FL's wrapper sets the rate up again after the state
  11973. e.setSampleRate (44100.0);
  11974. std::vector<float> l (inL), r (inR);
  11975. for (int s = 0; s < n; s += 512)
  11976. e.process (l.data() + s, r.data() + s, 512);
  11977. return std::make_pair (l, r);
  11978. };
  11979. auto run2 = [&] (const std::array<int, 8>& raw, double bpm) {
  11980. lynx::fldelay::Delay2State st;
  11981. st.v = raw;
  11982. lynx::fldelay::Delay2Engine e (st, 44100.0, bpm, 512);
  11983. std::vector<float> l (inL), r (inR);
  11984. for (int s = 0; s < n; s += 512)
  11985. e.process (l.data() + s, r.data() + s, 512);
  11986. return std::make_pair (l, r);
  11987. };
  11988. // Delay 3, true stereo, synced 1/8 beat, cutoff + resonance (Fruity Delay 3_x64.dll at 120 and 100 BPM)
  11989. 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 };
  11990. auto o = run3 (a, 120.0);
  11991. check (o.first, o.second, { { 3000, 0.399949551f, 0.598533869f }, { 6000, 0.116703831f, -0.315510482f }, { 9000, -0.150520369f, -0.250620633f },
  11992. { 13000, 0.193608373f, 0.0598564595f }, { 16000, -0.0524701327f, 0.022601109f } });
  11993. o = run3 (a, 100.0);
  11994. check (o.first, o.second, { { 5000, 0.377422422f, 0.346570343f }, { 6000, -0.182619616f, 0.0488029197f }, { 11000, -0.00120426295f, 0.0253051519f },
  11995. { 16000, 0.0806550607f, -0.0991330743f } });
  11996. // ping pong, keep pitch, modulation, cutoff LFO, diffusion, saturation, rate and bit reduction, colour
  11997. 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);
  11998. check (o.first, o.second, { { 1500, 0.288540125f, -0.148862943f }, { 5000, 5.90502168e-06f, 0.111382999f }, { 8000, -0.0736631379f, -0.0305093247f },
  11999. { 11000, -0.00831047911f, 0.0102323527f }, { 16000, 0.0175791476f, 0.0273917634f } });
  12000. // fake stereo, unsynced, high-pass, limit distortion, no dry
  12001. 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);
  12002. check (o.first, o.second, { { 1500, 0.0825810209f, 0.0228505582f }, { 6000, -0.0427959524f, -0.0408911295f }, { 9000, 0.0114790015f, 0.019227922f },
  12003. { 16000, -0.000312746735f, 0.00101647642f } });
  12004. // delay type "Off": filter, distortion and the bit/rate reduction on the dry path
  12005. 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);
  12006. check (o.first, o.second, { { 1500, 0.238686681f, -0.106820084f }, { 3000, 0.131528616f, 0.0665975735f }, { 5000, 9.94473215e-09f, -8.22819057e-09f },
  12007. { 11000, 0.0f, 0.0f } });
  12008. // Delay 2: default, ping pong with a stereo offset, swapped without dry (Fruity Delay 2_x64.dll)
  12009. o = run2 ({ 0, 128, 128, 64, 36, 0, 0, 128 }, 120.0);
  12010. check (o.first, o.second, { { 3000, 0.211380005f, 0.277908325f }, { 5000, -0.223615795f, -0.0189359933f }, { 9000, 0.120054141f, -0.00297877868f },
  12011. { 16000, 0.0182533264f, 0.105384827f } });
  12012. o = run2 ({ -40, 110, 90, 90, 24, -300, 2, 90 }, 100.0);
  12013. check (o.first, o.second, { { 3000, 0.0929639712f, 0.122222826f }, { 5000, -0.0884733275f, -0.0507739782f }, { 11000, 0.0589251742f, 0.0f },
  12014. { 16000, -0.029524127f, 0.0128910914f } });
  12015. o = run2 ({ 60, 140, 0, 100, 30, 250, 1, 110 }, 120.0);
  12016. check (o.first, o.second, { { 1500, 0.0f, 0.0f }, { 5000, 0.176379532f, -0.212506086f }, { 8000, -0.324207425f, 0.0675852001f },
  12017. { 13000, 0.145023212f, -0.104788139f } });
  12018. // the mixer effects from FL states: time law (Delay 3 synced 192 = 1 beat; Delay 2 144 = 3/4 beat), settled start
  12019. auto state3 = std::vector<std::uint8_t> (4 + 26 * 4, 0);
  12020. 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 };
  12021. state3[0] = 1;
  12022. std::memcpy (state3.data() + 4, def3.data(), 26 * 4);
  12023. const auto slot3 = lynx::effectFromFl ("Fruity Delay 3", state3, 120.0);
  12024. CHECK (slot3 && slot3->kind == lynx::EffectKind::fruityDelay3);
  12025. auto state2 = std::vector<std::uint8_t> (32, 0);
  12026. const std::array<int, 8> def2 { 0, 128, 128, 0, 144, 0, 0, 128 };
  12027. std::memcpy (state2.data(), def2.data(), 32);
  12028. const auto slot2 = lynx::effectFromFl ("Fruity delay 2", state2, 120.0);
  12029. CHECK (slot2 && slot2->kind == lynx::EffectKind::fruityDelay2);
  12030. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityDelay2, 4, slot2->params[4], 120.0) == "3:00");
  12031. auto echoAt = [] (const lynx::EffectSlot& slot, double bpm) {
  12032. auto fx = lynx::makeEffect (slot);
  12033. fx->prepare (44100.0);
  12034. std::vector<float> l (48000, 0.0f), r (48000, 0.0f);
  12035. l[100] = 1.0f;
  12036. for (int s = 0; s < 48000; s += 1024)
  12037. fx->process (l.data() + s, r.data() + s, std::min (1024, 48000 - s), slot.params, bpm);
  12038. int best = 200;
  12039. for (int i = 200; i < 48000; ++i)
  12040. if (std::abs (l[(std::size_t) i]) > std::abs (l[(std::size_t) best]))
  12041. best = i;
  12042. return best - 100;
  12043. };
  12044. if (slot3 && slot2)
  12045. {
  12046. CHECK (echoAt (*slot3, 120.0) == 22050);
  12047. CHECK (echoAt (*slot3, 150.0) == 17640);
  12048. CHECK (echoAt (*slot2, 120.0) == 16538);
  12049. CHECK (echoAt (*slot2, 140.0) == 14175);
  12050. }
  12051. }
  12052. #include "UiRackMixerTests.inc"
  12053. // ---------------------------------------------------------------------------
  12054. // FL Studio integration (core/FlPluginDb.h): synthetic plugin-database fixtures, no Image-Line files
  12055. namespace
  12056. {
  12057. std::vector<std::uint8_t> testWrapperBlob (std::uint32_t type, const std::string& name, const std::string& path,
  12058. const std::vector<std::uint8_t>& classId = {}, const std::string& clapId = {})
  12059. {
  12060. std::vector<std::uint8_t> b;
  12061. auto u32 = [&b] (std::uint32_t v) { for (int i = 0; i < 4; ++i) b.push_back ((std::uint8_t) (v >> (8 * i))); };
  12062. auto record = [&] (std::uint32_t id, const std::vector<std::uint8_t>& payload) {
  12063. u32 (id);
  12064. u32 ((std::uint32_t) payload.size());
  12065. u32 (0);
  12066. b.insert (b.end(), payload.begin(), payload.end());
  12067. };
  12068. u32 (12);
  12069. record (0x32, { (std::uint8_t) type, 0, 0, 0 });
  12070. if (! classId.empty())
  12071. record (0x34, classId);
  12072. if (! clapId.empty())
  12073. record (0x3A, std::vector<std::uint8_t> (clapId.begin(), clapId.end()));
  12074. record (0x36, std::vector<std::uint8_t> (name.begin(), name.end()));
  12075. record (0x37, std::vector<std::uint8_t> (path.begin(), path.end()));
  12076. return b;
  12077. }
  12078. } // namespace
  12079. TEST (fl_plugin_db_nfo_parsing)
  12080. {
  12081. const std::string nfo = "ps_name=Vital\r\n"
  12082. "ps_file_name_0=C:\\Program Files\\Common Files\\VST3\\Vital.vst3\r\n"
  12083. "ps_file_vendorname_0=Vital Audio\r\n"
  12084. "ps_file_type_0=2\r\n"
  12085. "ps_file_name_1=C:\\VSTPlugins\\Vital_x64.dll\r\n"
  12086. "ps_file_type_1=2\r\n"
  12087. "ps_file_magic_1=Vita\r\n"
  12088. "ps_file_name_2=not a plugin\r\n"
  12089. "garbage line without equals\r\n";
  12090. const auto entries = lynx::parseFlPluginNfo (nfo, "D:\\FL\\Presets\\Plugin database\\Installed\\Generators\\VST3\\Vital.nfo");
  12091. CHECK (entries.size() == 2);
  12092. if (entries.size() == 2)
  12093. {
  12094. CHECK (entries[0].name == "Vital" && entries[0].vendor == "Vital Audio" && entries[0].role == 2);
  12095. CHECK (entries[0].format == lynx::ForeignPlugin::Format::vst3);
  12096. CHECK (entries[1].format == lynx::ForeignPlugin::Format::vst2 && entries[1].vst2Id == 0x56697461u);
  12097. }
  12098. // no type field: the role comes from the Effects / Generators folder; an ID in GUID form is read
  12099. const auto fx = lynx::parseFlPluginNfo ("ps_name=Fancy Comp\nps_file_filename_0=C:/CLAP/Fancy Comp.clap\n"
  12100. "ps_file_guid_0={01234567-89AB-CDEF-0123-456789ABCDEF}\n",
  12101. "C:\\x\\Installed\\Effects\\CLAP\\Fancy Comp.nfo");
  12102. CHECK (fx.size() == 1 && fx[0].role == 1 && fx[0].format == lynx::ForeignPlugin::Format::clap && fx[0].hasClassId
  12103. && fx[0].classId[0] == 0x01 && fx[0].classId[15] == 0xEF);
  12104. CHECK (lynx::parseFlPluginNfo ("Tip=All of the plugins found in your system.").empty());
  12105. CHECK (lynx::pluginNameKey ("Serum_x64") == "serum");
  12106. CHECK (lynx::pluginNameKey ("Vital.VST3") == "vital");
  12107. CHECK (lynx::pluginNameKey ("Pro-Q 3") == "proq3");
  12108. CHECK (lynx::pluginFormatFromPath ("a\\b.CLAP") == lynx::ForeignPlugin::Format::clap);
  12109. }
  12110. TEST (fl_plugin_db_fst_parsing_and_matching)
  12111. {
  12112. std::vector<std::uint8_t> cid;
  12113. for (int i = 0; i < 16; ++i)
  12114. cid.push_back ((std::uint8_t) (0x10 + i));
  12115. // an .fst of the database: an FL preset whose 0xD5 holds the wrapper record
  12116. FlpBuilder fst;
  12117. fst.ascii (0xC9, "Fruity Wrapper");
  12118. fst.data (0xD5, testWrapperBlob (8, "Surge XT", "E:\\Plugins\\VST3\\Surge XT.vst3", cid));
  12119. lynx::FlPluginDbEntry e;
  12120. CHECK (lynx::parseFlPluginFst (fst.bytes, e));
  12121. CHECK (e.name == "Surge XT" && e.format == lynx::ForeignPlugin::Format::vst3 && e.hasClassId && e.classId[0] == 0x10);
  12122. lynx::FlPluginDbEntry bad;
  12123. CHECK (! lynx::parseFlPluginFst (std::vector<std::uint8_t> { 'F', 'L', 'h', 'd' }, bad));
  12124. lynx::FlPluginDb db;
  12125. db.add (e);
  12126. // the .nfo of the same plugin fills in vendor and role
  12127. for (const auto& n : lynx::parseFlPluginNfo ("ps_name=Surge XT\nps_file_name_0=E:/Plugins/VST3/Surge XT.vst3\n"
  12128. "ps_file_vendorname_0=Surge Synth Team\nps_file_type_0=2\n"))
  12129. db.add (n);
  12130. db.add ({ "Clapper", "", "E:\\Plugins\\CLAP\\Clapper.clap", "", lynx::ForeignPlugin::Format::clap, 1, {}, false, "org.clapper", 0 });
  12131. db.add ({ "Big Verb", "", "E:\\Old VST\\BigVerb_x64.dll", "", lynx::ForeignPlugin::Format::vst2, 1, {}, false, "", 0x42566262u });
  12132. db.add ({ "Bundle", "", "F:\\VST3\\Bundle.vst3\\Contents\\x86_64-win\\Bundle.vst3", "", lynx::ForeignPlugin::Format::vst3, 2, {}, false, "", 0 });
  12133. CHECK (db.entries().size() == 4);
  12134. CHECK (db.entries()[0].vendor == "Surge Synth Team" && db.entries()[0].role == 2);
  12135. // by VST3 class ID even when the project saved another path and name
  12136. lynx::ForeignPlugin p;
  12137. p.format = lynx::ForeignPlugin::Format::vst3;
  12138. p.hasClassId = true;
  12139. std::copy (cid.begin(), cid.end(), p.classId.begin());
  12140. p.name = "Renamed";
  12141. p.path = "C:\\Other PC\\Surge.vst3";
  12142. CHECK (db.match (p, true) == &db.entries()[0]);
  12143. // by CLAP id, by VST2 id
  12144. lynx::ForeignPlugin c;
  12145. c.format = lynx::ForeignPlugin::Format::clap;
  12146. c.clapId = "org.clapper";
  12147. CHECK (db.match (c, false) == &db.entries()[1]);
  12148. lynx::ForeignPlugin v;
  12149. v.format = lynx::ForeignPlugin::Format::vst2;
  12150. v.vst2Id = 0x42566262u;
  12151. CHECK (db.match (v, false) == &db.entries()[2]);
  12152. // by name (architecture suffix ignored), only in the same role
  12153. lynx::ForeignPlugin byName;
  12154. byName.format = lynx::ForeignPlugin::Format::vst2;
  12155. byName.name = "BigVerb x64";
  12156. CHECK (db.match (byName, false) == &db.entries()[2]);
  12157. CHECK (db.match (byName, true) == nullptr);
  12158. lynx::ForeignPlugin unknown;
  12159. unknown.format = lynx::ForeignPlugin::Format::vst3;
  12160. unknown.name = "Nothing Like It";
  12161. CHECK (db.match (unknown, true) == nullptr);
  12162. // folders: the bundle's parent, deduplicated
  12163. const auto folders = db.pluginFolders();
  12164. CHECK (folders.size() == 4);
  12165. CHECK (std::find (folders.begin(), folders.end(), "F:\\VST3") != folders.end());
  12166. CHECK (std::find (folders.begin(), folders.end(), "E:\\Plugins\\VST3") != folders.end());
  12167. // targeted scan picks: same file, or same name key
  12168. CHECK (lynx::pluginFileMatches ("E:/Plugins/VST3/Surge XT.vst3", p, &db.entries()[0]));
  12169. CHECK (lynx::pluginFileMatches ("C:\\Program Files\\Common Files\\VST3\\BigVerb.vst3", byName, nullptr));
  12170. CHECK (! lynx::pluginFileMatches ("C:\\VST3\\Other.vst3", byName, nullptr));
  12171. // the plugins an .flp uses: wrapper channels and mixer slots
  12172. flp::Project project;
  12173. flp::Channel ch;
  12174. ch.pluginName = "Fruity Wrapper";
  12175. ch.pluginState = testWrapperBlob (8, "Surge XT", "C:\\VST3\\Surge XT.vst3", cid);
  12176. project.channels.push_back (ch);
  12177. ch.pluginName = "3x Osc";
  12178. project.channels.push_back (ch);
  12179. flp::MixerInsert insert;
  12180. insert.slots.push_back ({ 0, "Fruity Wrapper", testWrapperBlob (12, "Clapper", "C:\\CLAP\\Clapper.clap", {}, "org.clapper") });
  12181. insert.slots.push_back ({ 1, "Fruity Wrapper", testWrapperBlob (3, "DX thing", "C:\\dx.dll") }); // DirectX: not hosted
  12182. project.mixer.push_back (insert);
  12183. const auto uses = lynx::flpForeignPlugins (project);
  12184. CHECK (uses.size() == 2);
  12185. CHECK (uses.size() == 2 && uses[0].instrument && ! uses[1].instrument && uses[1].plugin.clapId == "org.clapper");
  12186. }
  12187. TEST (fl_sample_relocation)
  12188. {
  12189. std::set<std::string> files { "D:\\Docs\\Image-Line\\FL Studio\\Audio\\Recorded\\take.wav", "C:\\Program Files\\FL 2026\\Data\\Patches\\kick.wav",
  12190. "C:\\Users\\me\\Music\\loop.wav", "E:\\Songs\\Track\\Samples\\snare.wav", "E:\\Songs\\Track\\hat.wav" };
  12191. auto exists = [&] (const std::string& p) { return files.count (p) > 0; };
  12192. lynx::FlPathRoots roots;
  12193. roots.userData = "D:\\Docs\\Image-Line\\FL Studio";
  12194. roots.factoryData = "C:\\Program Files\\FL 2026";
  12195. roots.home = "C:\\Users\\me";
  12196. const std::string project = "E:\\Songs\\Track";
  12197. // variable resolved, file in place
  12198. CHECK (lynx::locateFlSample ("%FLStudioFactoryData%\\Data\\Patches\\kick.wav", roots, project, exists) == "C:\\Program Files\\FL 2026\\Data\\Patches\\kick.wav");
  12199. // another PC's FL user data / FL install / user profile
  12200. CHECK (lynx::locateFlSample ("C:\\Users\\bob\\Documents\\Image-Line\\FL Studio\\Audio\\Recorded\\take.wav", roots, project, exists)
  12201. == "D:\\Docs\\Image-Line\\FL Studio\\Audio\\Recorded\\take.wav");
  12202. CHECK (lynx::locateFlSample ("C:\\Program Files (x86)\\Image-Line\\FL Studio 20\\Data\\Patches\\kick.wav", roots, project, exists)
  12203. == "C:\\Program Files\\FL 2026\\Data\\Patches\\kick.wav");
  12204. CHECK (lynx::locateFlSample ("C:/Users/bob/Music/loop.wav", roots, project, exists) == "C:\\Users\\me\\Music\\loop.wav");
  12205. // next to the project, also in a sub-folder named in the original path
  12206. CHECK (lynx::locateFlSample ("Z:\\Old\\Samples\\snare.wav", roots, project, exists) == "E:\\Songs\\Track\\Samples\\snare.wav");
  12207. CHECK (lynx::locateFlSample ("Z:\\Old\\Stuff\\hat.wav", roots, project, exists) == "E:\\Songs\\Track\\hat.wav");
  12208. CHECK (lynx::locateFlSample ("Z:\\nowhere.wav", roots, project, exists).empty());
  12209. CHECK (lynx::locateFlSample ("", roots, project, exists).empty());
  12210. }
  12211. // --- REA round 64: SimSynth (exact engine) and Wasp / Wasp XT (FL 2026's silent placeholder) ---------------------
  12212. namespace
  12213. {
  12214. // a SimSynth state written from scratch (REA_rodada64 §1.1 layout: 45 doubles, 19 unused ints, 19 switches, 12 unused
  12215. // ints, version)
  12216. std::vector<std::uint8_t> simSynthTestState (const std::array<double, 45>& P, const std::array<int, 19>& S, int version = 14)
  12217. {
  12218. std::vector<std::uint8_t> b (564, 0);
  12219. std::memcpy (b.data(), P.data(), 360);
  12220. std::memcpy (b.data() + 360 + 76, S.data(), 76);
  12221. std::memcpy (b.data() + 560, &version, 4);
  12222. return b;
  12223. }
  12224. // the 0xD5 records of an FL preset file (.fst: the FLhd/FLdt event stream) that follow a plugin name `plugin`
  12225. std::vector<std::vector<std::uint8_t>> pluginStatesIn (const std::filesystem::path& file, const std::string& plugin)
  12226. {
  12227. std::vector<std::vector<std::uint8_t>> out;
  12228. std::ifstream in (file, std::ios::binary);
  12229. const std::vector<std::uint8_t> b ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  12230. std::size_t i = 0;
  12231. while (i + 8 <= b.size() && std::memcmp (b.data() + i, "FLdt", 4) != 0)
  12232. ++i;
  12233. i += 8;
  12234. std::string name;
  12235. while (i < b.size())
  12236. {
  12237. const auto id = b[i++];
  12238. std::size_t n = id < 64 ? 1 : id < 128 ? 2 : id < 192 ? 4 : 0;
  12239. if (id >= 192)
  12240. for (int shift = 0; i < b.size(); shift += 7)
  12241. {
  12242. const auto c = b[i++];
  12243. n |= (std::size_t) (c & 0x7F) << shift;
  12244. if (c < 0x80)
  12245. break;
  12246. }
  12247. if (i + n > b.size())
  12248. break;
  12249. if (id == 0xC9)
  12250. {
  12251. name.clear();
  12252. for (std::size_t k = 0; k < n; ++k)
  12253. if (b[i + k] != 0)
  12254. name += (char) b[i + k]; // ASCII or UTF-16LE (zero bytes skipped)
  12255. }
  12256. if (id == 0xD5 && name == plugin)
  12257. out.emplace_back (b.begin() + (std::ptrdiff_t) i, b.begin() + (std::ptrdiff_t) (i + n));
  12258. i += n;
  12259. }
  12260. return out;
  12261. }
  12262. } // namespace
  12263. TEST (simsynth_engine)
  12264. {
  12265. // A: pulse + octave layer, half-cosine with unison and two octave layers, sine/sine⁴ mix, square LFO → pitch with
  12266. // delay, filter envelope, LP/BP mix, chorus. B (version 7): triangle ring-modulated by filtered noise, sample &
  12267. // hold LFO → cutoff, retrigger, own filter envelope, chorus off
  12268. 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,
  12269. 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 };
  12270. 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 };
  12271. 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,
  12272. 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 };
  12273. 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 };
  12274. const auto blobA = simSynthTestState (A, SA), blobB = simSynthTestState (B, SB, 7);
  12275. dumpIfAsked ("LYNX_DUMP_SIMSYNTH", blobA);
  12276. CHECK (lynx::simSynthStateUsable (blobA) && ! lynx::simSynthStateUsable (std::vector<std::uint8_t> (blobA.begin(), blobA.begin() + 563)));
  12277. const auto hp = lynx::simSynthHostParams (blobA);
  12278. // knobs go through ⌊65535·v⌋, the coarse pitches through 48 steps, the switches and wave selectors as they are
  12279. CHECK (hp.size() == 58u && hp[0] == 52428 && hp[27] == 10080 && hp[41] == 1 && hp[54] == 0 && hp[56] == 4);
  12280. // golden samples of the installation's SimSynth_x64.dll (round-64 EXEC harness: created, state restored, 44.1 kHz,
  12281. // host blocks of 512, old voice record) — LYNX's engine renders bit-identically: C5 0–30000, E5 Final.Vol 0.8 pan
  12282. // 16 11025–40000, G5 pan −20 +25 cents 22050–52000
  12283. const std::vector<lynx::simsynth::RefNote> notes = { { 0, 60, 1.0f, 0, 0, 0.0f, 0.0f, 30000 },
  12284. { 11025, 64, 0.8f, 16, 0, 0.0f, 0.0f, 40000 },
  12285. { 22050, 67, 1.0f, -20, 25, 0.0f, 0.0f, 52000 } };
  12286. struct Golden { int i; float l, r; };
  12287. 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 } };
  12288. 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 } };
  12289. for (int which = 0; which < 2; ++which)
  12290. {
  12291. std::vector<std::int64_t> counters;
  12292. const auto y = lynx::simsynth::renderReference (which == 0 ? blobA : blobB, notes, 44100.0, 66150, 512, &counters);
  12293. CHECK (y.size() == 2 * 66150u);
  12294. if (y.size() != 2 * 66150u)
  12295. continue;
  12296. // the random-table index after creation / state / rate set equals the DLL's (276 / 696 / 756 and 276 / 492 / 528)
  12297. CHECK (counters.size() == 4u && counters[2] == 276 && counters[3] == (which == 0 ? 696 : 492) && counters[0] == (which == 0 ? 756 : 528));
  12298. for (const auto& g : which == 0 ? std::vector (std::begin (goldenA), std::end (goldenA)) : std::vector (std::begin (goldenB), std::end (goldenB)))
  12299. CHECK (y[2 * (std::size_t) g.i] == g.l && y[2 * (std::size_t) g.i + 1] == g.r);
  12300. double e = 0.0;
  12301. for (auto v : y)
  12302. e += (double) v * v;
  12303. CHECK (std::abs (std::sqrt (e / (double) y.size()) - (which == 0 ? 0.290665331870 : 0.992116840643)) < 1e-9);
  12304. }
  12305. // the instrument: same engine behind LYNX's channel gains (V = 1, centre → Final.Vol 1, Final.Pan 0)
  12306. lynx::ChannelDef c;
  12307. c.instrument = lynx::InstrumentKind::simsynth;
  12308. c.pluginState = blobA;
  12309. auto inst = lynx::makeInstrument (lynx::InstrumentKind::simsynth);
  12310. inst->prepare (44100.0);
  12311. inst->configure (c, 120.0);
  12312. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  12313. for (int k = 0; k < 16; ++k)
  12314. {
  12315. if (k == 0)
  12316. inst->noteOn (60, 0.8f);
  12317. if (k == 10)
  12318. inst->noteOff (60);
  12319. inst->render (l.data() + 512 * k, r.data() + 512 * k, 512, 1.0f, 1.0f);
  12320. }
  12321. const auto ref = lynx::simsynth::renderReference (blobA, { { 0, 60, 1.0f, 0, 0, 0.0f, 0.0f, 5120 } }, 44100.0, 8192);
  12322. double diff = 0.0, energy = 0.0;
  12323. for (int i = 0; i < 8192; ++i)
  12324. {
  12325. 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]) });
  12326. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  12327. }
  12328. CHECK (diff == 0.0 && energy > 1e-3);
  12329. // the installation's SimSynth presets (when the FL data folder is given): every state usable and sounding
  12330. if (! realFlpDir.empty())
  12331. {
  12332. const auto folder = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "SimSynth";
  12333. int presets = 0, usable = 0, sounding = 0;
  12334. if (std::filesystem::exists (folder))
  12335. for (const auto& entry : std::filesystem::directory_iterator (folder))
  12336. for (const auto& st : pluginStatesIn (entry.path(), "SimSynth"))
  12337. {
  12338. ++presets;
  12339. if (! lynx::simSynthStateUsable (st))
  12340. continue;
  12341. ++usable;
  12342. const auto y = lynx::simsynth::renderReference (st, { { 0, 48, 1.0f, 0, 0, 0.0f, 0.0f, 11025 } }, 44100.0, 22050);
  12343. double e = 0.0;
  12344. bool finite = true;
  12345. for (auto v : y)
  12346. finite = finite && std::isfinite (v), e += (double) v * v;
  12347. CHECK (finite);
  12348. sounding += e > 1e-9 ? 1 : 0;
  12349. }
  12350. std::printf (" SimSynth presets: %d, %d usable, %d sounding\n", presets, usable, sounding);
  12351. CHECK (presets == 0 || (usable == presets && presets >= 70 && sounding >= presets - 2));
  12352. }
  12353. }
  12354. TEST (wasp_is_a_silent_placeholder)
  12355. {
  12356. // FL 2026's Wasp / Wasp XT DLL keeps the state and plays nothing: so does LYNX (REA_rodada64 §5)
  12357. auto inst = lynx::makeInstrument (lynx::InstrumentKind::wasp);
  12358. inst->prepare (44100.0);
  12359. std::vector<float> l (1024, 0.0f), r (1024, 0.0f);
  12360. inst->noteOn (60, 1.0f);
  12361. inst->render (l.data(), r.data(), 1024, 1.0f, 1.0f);
  12362. CHECK (inst->idle() && std::all_of (l.begin(), l.end(), [] (float v) { return v == 0.0f; }));
  12363. // import: a SimSynth channel plays on the exact engine, Wasp and Wasp XT channels become the silent placeholder with
  12364. // their state kept
  12365. std::vector<std::uint8_t> ss (564, 0), wasp (169, 1);
  12366. const int version = 14;
  12367. std::memcpy (ss.data() + 560, &version, 4);
  12368. FlpBuilder b;
  12369. b.ascii (0xC7, "25.2.5.5319");
  12370. int index = 0;
  12371. for (const auto& [name, state] : { std::pair<std::u16string, std::vector<std::uint8_t>> { u"SimSynth", ss }, { u"Wasp", wasp }, { u"Wasp XT", wasp } })
  12372. {
  12373. b.u8 (0x40); b.u16 ((std::uint16_t) index++);
  12374. b.u8 (0x15); b.u8 (2);
  12375. b.utf16 (0xC9, name);
  12376. b.data (0xD5, state);
  12377. }
  12378. const auto song = lynx::importFlp (flp::parseProject (b.bytes));
  12379. CHECK (song.channels.size() == 3u);
  12380. if (song.channels.size() == 3u)
  12381. {
  12382. CHECK (song.channels[0].instrument == lynx::InstrumentKind::simsynth && song.channels[0].pluginState == ss);
  12383. CHECK (song.channels[1].instrument == lynx::InstrumentKind::wasp && song.channels[1].pluginState == wasp);
  12384. CHECK (song.channels[2].instrument == lynx::InstrumentKind::wasp);
  12385. }
  12386. }
  12387. // --- REA round 67: Fruity Granulizer (exact engine) --------------------------------------------------------------
  12388. namespace
  12389. {
  12390. // our own deterministic test sample (a fading 220 Hz sine plus LCG noise per channel), as r67/py/gold67.py builds it
  12391. lynx::SampleData granulizerTestSample (int n, int ch)
  12392. {
  12393. lynx::SampleData d;
  12394. std::uint32_t s = 12345;
  12395. for (int i = 0; i < n; ++i)
  12396. {
  12397. const auto base = 0.5 * std::sin (2.0 * 3.141592653589793 * 220.0 * i / 44100.0) * (1.0 - 0.7 * i / n);
  12398. for (int c = 0; c < ch; ++c)
  12399. {
  12400. s = s * 1664525u + 1013904223u;
  12401. const auto v = (float) (base + 0.2 * ((double) (std::int32_t) s * (1.0 / 2147483648.0)));
  12402. (c == 0 ? d.left : d.right).push_back (v);
  12403. }
  12404. }
  12405. return d;
  12406. }
  12407. std::vector<std::uint8_t> granulizerTestState (const std::array<int, 12>& P, int keyMode = 0)
  12408. {
  12409. std::vector<std::uint8_t> b (60, 0);
  12410. const std::int32_t v = 5;
  12411. std::memcpy (b.data(), &v, 4);
  12412. std::memcpy (b.data() + 4, P.data(), 48);
  12413. std::memcpy (b.data() + 56, &keyMode, 4);
  12414. return b;
  12415. }
  12416. } // namespace
  12417. TEST (granulizer_engine)
  12418. {
  12419. // REA_rodada67: states written from scratch against our reference model (granmodel.py, which renders like the
  12420. // installation's DLL within −128 dB: the DLL's rcpps and IPP sines are not replicated)
  12421. const auto v5 = granulizerTestState ({ 204, 50, 611, 611, 0, 180, 0, 0, 0, 0, 0, 228 });
  12422. CHECK (lynx::granulizerStateUsable (v5));
  12423. const auto n5 = lynx::granulizerNormalisedState (v5, {});
  12424. CHECK (n5.size() == 428 && n5[0] == 7 && lynx::granulizerParam (n5, 0) == 204 && lynx::granulizerParam (n5, 11) == 228
  12425. && ! lynx::granulizerParam (n5, 12));
  12426. // version 1 (10 parameters): parameter 11 takes the plugin's 0xf8
  12427. {
  12428. std::vector<std::uint8_t> v1 (v5.begin(), v5.begin() + 44);
  12429. v1[0] = 1;
  12430. const auto n1 = lynx::granulizerNormalisedState (v1, {});
  12431. CHECK (lynx::granulizerParam (n1, 9) == 0 && lynx::granulizerParam (n1, 11) == 0xf8 && lynx::granulizerParam (n1, 3) == 611);
  12432. std::vector<std::uint8_t> bad (v5);
  12433. bad[0] = 9;
  12434. CHECK (! lynx::granulizerStateUsable (bad) && lynx::granulizerNormalisedState (bad, {}).empty());
  12435. auto edited = n5;
  12436. CHECK (lynx::granulizerSetParam (edited, 3, -5000) && lynx::granulizerParam (edited, 3) == -1000);
  12437. }
  12438. using N = lynx::granulizer::RefNote;
  12439. struct Golden { std::vector<std::uint8_t> state; int ch, fs, rate; lynx::granulizer::RefOptions opt; std::vector<N> notes;
  12440. double el, er; std::tuple<int, float, float> at[6]; };
  12441. auto check = [] (const Golden& g) {
  12442. auto smp = granulizerTestSample (20000, g.ch);
  12443. smp.sampleRate = g.rate;
  12444. const auto y = lynx::granulizer::renderReference (lynx::granulizerNormalisedState (g.state, {}), smp, g.notes, (double) g.fs, 16384, g.opt);
  12445. CHECK (y.size() == 2 * 16384);
  12446. if (y.size() != 2 * 16384)
  12447. return;
  12448. double el = 0.0, er = 0.0;
  12449. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  12450. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  12451. CHECK (std::abs (el - g.el) <= 1e-6 * g.el && std::abs (er - g.er) <= 1e-6 * g.er);
  12452. for (const auto& [i, l, r] : g.at)
  12453. CHECK (std::abs (y[2 * (std::size_t) i] - l) <= 1e-6f * std::max (1e-16f, std::abs (l))
  12454. && std::abs (y[2 * (std::size_t) i + 1] - r) <= 1e-6f * std::max (1e-16f, std::abs (r)));
  12455. };
  12456. lynx::granulizer::RefOptions o1, o2, o3;
  12457. o2.block = 333, o2.seed = 777;
  12458. o3.block = 64, o3.seed = 5;
  12459. // the plugin's defaults, two notes (one pitched by +23 cents and released: the processor's end fade)
  12460. 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,
  12461. { { 100, 0.057010360062122345f, 0.08960580080747604f }, { 1000, 0.07210570573806763f, -0.048257164657115936f },
  12462. { 2500, 0.14769265055656433f, -0.0194698516279459f }, { 4000, 0.11169229447841644f, 0.17487481236457825f },
  12463. { 7777, -0.4309714436531067f, -0.17027156054973602f }, { 12000, -0.11129666864871979f, -0.19622492790222168f } } });
  12464. // backwards, randomness, depth/speed wobble, stereo separation, loop, sample start; a mono 44.1 kHz sample at 48 kHz
  12465. check ({ granulizerTestState ({ 150, 120, 300, -480, 200, 90, 120, 40, 1, 0, 900, 300 }), 1, 48000, 44100, o2,
  12466. { { 0, 55, 1.0, 0.2, -37.0, 12000 }, { 5000, 67, 0.5, 0.0, 0.0, -1 } }, 959.5011214226065, 1294.4823684530272,
  12467. { { 100, 0.02567935362458229f, 0.010721667669713497f }, { 1000, 0.3007732629776001f, 0.08214527368545532f },
  12468. { 2500, 0.435413658618927f, 0.09068503230810165f }, { 4000, -0.24611327052116394f, -0.20149046182632446f },
  12469. { 7777, -0.008478119969367981f, 0.31221628189086914f }, { 12000, 0.203709632f, 0.324944794f } } });
  12470. // key mode 1 (the sample spread over two octaves), freeze, hard stereo separation; 48 kHz sample at 96 kHz, 64-sample blocks
  12471. check ({ granulizerTestState ({ 300, 0, 800, 700, 0, 180, 0, 64, 0, 1, 2000, 100 }, 1), 2, 96000, 48000, o3,
  12472. { { 0, 62, 0.9, 0.0, 0.0, -1 }, { 4000, 50, 0.7, 0.0, 11.0, -1 } }, 366.79556669810813, 0.00825590391720353,
  12473. { { 100, 0.004204070195555687f, 7.680895437786243e-20f }, { 1000, 0.008516596630215645f, 3.0263170437258543e-19f },
  12474. { 2500, 0.06948182731866837f, 4.5391312775082004e-18f }, { 4000, -0.01754589192569256f, -4.895453905423713e-19f },
  12475. { 7777, 0.10338862985372543f, 2.344363859234514e-18f }, { 12000, -0.09385982155799866f, -1.3493021437162966e-17f } } });
  12476. // the instrument: silent without a sample (FL's plugin refuses the note), sounding with one
  12477. {
  12478. auto silent = lynx::makeInstrument (lynx::InstrumentKind::granulizer);
  12479. silent->prepare (44100.0);
  12480. std::vector<float> l (2048, 0.0f), r (2048, 0.0f);
  12481. silent->noteOn (60, 1.0f);
  12482. silent->render (l.data(), r.data(), 2048, 1.0f, 1.0f);
  12483. CHECK (silent->idle() && std::all_of (l.begin(), l.end(), [] (float v) { return v == 0.0f; }));
  12484. auto smp = std::make_shared<lynx::SampleData> (granulizerTestSample (20000, 2));
  12485. auto inst = lynx::makeInstrument (lynx::InstrumentKind::granulizer, smp);
  12486. lynx::ChannelDef def;
  12487. def.instrument = lynx::InstrumentKind::granulizer;
  12488. def.pluginState = n5;
  12489. inst->configure (def, 140.0);
  12490. inst->prepare (44100.0);
  12491. inst->noteOn (60, 0.8f);
  12492. inst->render (l.data(), r.data(), 2048, 1.0f, 1.0f);
  12493. double e = 0.0;
  12494. for (auto v : l)
  12495. e += (double) v * v;
  12496. CHECK (! inst->idle() && e > 1.0);
  12497. inst->noteOff (60);
  12498. for (int k = 0; k < 4; ++k)
  12499. inst->render (l.data(), r.data(), 512, 1.0f, 1.0f);
  12500. CHECK (inst->idle()); // the 10 ms end fade of the plugin's voice processor
  12501. }
  12502. // import: a Fruity Granulizer channel plays on the exact engine with its sample
  12503. {
  12504. FlpBuilder b;
  12505. b.ascii (0xC7, "25.2.5.5319");
  12506. b.u8 (0x40); b.u16 (0);
  12507. b.u8 (0x15); b.u8 (2);
  12508. b.utf16 (0xC9, u"Fruity Granulizer");
  12509. b.data (0xD5, v5);
  12510. b.utf16 (0xC4, u"%FLStudioFactoryData%\\Data\\Patches\\Packs\\Drums\\Kicks\\808 Kick.wav");
  12511. lynx::FlpImportOptions options;
  12512. options.roots.factoryData = "C:\\FL";
  12513. options.fileExists = [] (const std::string& path) { return path == "C:\\FL\\Data\\Patches\\Packs\\Drums\\Kicks\\808 Kick.wav"; };
  12514. lynx::FlpImportReport report;
  12515. options.report = &report;
  12516. const auto song = lynx::importFlp (flp::parseProject (b.bytes), options);
  12517. CHECK (song.channels.size() == 1u);
  12518. if (song.channels.size() == 1u)
  12519. {
  12520. CHECK (song.channels[0].instrument == lynx::InstrumentKind::granulizer && song.channels[0].pluginState == n5);
  12521. CHECK (song.channels[0].samplePath == "C:\\FL\\Data\\Patches\\Packs\\Drums\\Kicks\\808 Kick.wav");
  12522. }
  12523. CHECK (report.converted["Fruity Granulizer"] == 1);
  12524. }
  12525. }
  12526. // --- REA round 67: Drumaxx (exact engine) -------------------------------------------------------------------------
  12527. namespace
  12528. {
  12529. // our own deterministic 16-pad kit (LCG-chosen parameters), as r67/py/gold67d.py builds it for the DLL
  12530. std::vector<std::uint8_t> drumaxxTestState (bool eq)
  12531. {
  12532. std::uint32_t s = 20261015u;
  12533. auto u = [&s] {
  12534. s = s * 1664525u + 1013904223u;
  12535. return (float) ((double) (s >> 8) * (1.0 / 16777216.0));
  12536. };
  12537. // (a product rounded on its own: no fused multiply-add)
  12538. auto mul = [] (float a, float b) {
  12539. volatile float p = a * b;
  12540. return (float) p;
  12541. };
  12542. static const int targets[] = { 0, 9, 10, 16, 21, 23, 26 };
  12543. std::array<float, 720> P {};
  12544. for (int k = 0; k < 16; ++k)
  12545. for (int i = 0; i < 44; ++i)
  12546. {
  12547. auto v = u();
  12548. if (i == 0)
  12549. v = 0.5f + mul (v, 0.5f);
  12550. else if ((i >= 7 && i <= 18) || (i >= 20 && i <= 34))
  12551. v = 0.2f + mul (v, 0.6f);
  12552. else if (i == 2 || i == 3)
  12553. v = 0.0f;
  12554. else if (i == 4)
  12555. v = (float) (36 + k);
  12556. else if (i == 5)
  12557. v = v < 0.5f ? 0.0f : 1.0f;
  12558. else if (i == 6)
  12559. v = (float) (int) (v * 4.0f);
  12560. else if (i >= 35 && i <= 38)
  12561. v = (float) targets[(int) (v * 7.0f)];
  12562. P[(std::size_t) (44 * k + i)] = v;
  12563. }
  12564. for (int i = 0; i < 16; ++i)
  12565. {
  12566. auto v = u();
  12567. if (i == 1)
  12568. v = 0.3f + mul (v, 0.4f);
  12569. else if (i == 3)
  12570. v = eq ? 1.0f : 0.0f;
  12571. else if (i >= 4 && i <= 11)
  12572. v = 0.35f + mul (v, 0.3f);
  12573. else if (i == 12)
  12574. v = 1.0f;
  12575. else if (i == 13)
  12576. v = v * 0.1f;
  12577. else if (i == 0 || i == 15)
  12578. v = 0.0f;
  12579. P[(std::size_t) (704 + i)] = v;
  12580. }
  12581. std::vector<std::uint8_t> b;
  12582. auto put32 = [&b] (std::uint32_t v) { for (int i = 0; i < 4; ++i) b.push_back ((std::uint8_t) (v >> (8 * i))); };
  12583. b.insert (b.end(), { 'S', 'S', 'L', 'F' });
  12584. put32 (21636u);
  12585. put32 (0x444d5333u); // engine stream version 3
  12586. const std::string name = "LYNX test kit";
  12587. b.insert (b.end(), name.begin(), name.end());
  12588. b.resize (b.size() + 256 - name.size(), 0);
  12589. const auto at = b.size();
  12590. b.resize (at + 720 * 4);
  12591. std::memcpy (b.data() + at, P.data(), 720 * 4);
  12592. for (int k = 0; k < 16; ++k)
  12593. {
  12594. b.resize (b.size() + 0x400, 0);
  12595. b.push_back (100), b.push_back (100); // pattern length, trigger key
  12596. }
  12597. b.resize (b.size() + 0x800, 0);
  12598. for (int k = 0; k < 16; ++k)
  12599. b.push_back (k >= 2 && k <= 4 ? 1 : 0); // pads 3…5 in cut group 1
  12600. b.resize (b.size() + 16, 0); // every pad on the master
  12601. return b;
  12602. }
  12603. } // namespace
  12604. TEST (drumaxx_engine)
  12605. {
  12606. const auto st = drumaxxTestState (true);
  12607. CHECK (st.size() == 21644u && lynx::drumaxxStateUsable (st) && lynx::drumaxxKitName (st) == "LYNX test kit");
  12608. CHECK (lynx::drumaxxParam (st, 4) == 36.0f && lynx::drumaxxParam (st, 44 * 15 + 4) == 51.0f && lynx::drumaxxParam (st, 0x2c3) == 1.0f);
  12609. {
  12610. auto edited = st;
  12611. lynx::drumaxxSetParam (edited, 0x2c1, 0.25f);
  12612. CHECK (lynx::drumaxxParam (edited, 0x2c1) == 0.25f && std::isnan (lynx::drumaxxParam (edited, 720)));
  12613. auto bad = st;
  12614. bad[8] = 'X';
  12615. CHECK (! lynx::drumaxxStateUsable (bad) && std::isnan (lynx::drumaxxParam (bad, 0)));
  12616. }
  12617. // goldens: LYNX's renders, bit-identical to Drumaxx_x64.dll + engine_x64.dll driven by the round-67 harness
  12618. using N = lynx::drumaxx::RefNote;
  12619. struct Golden { bool eq; int fs, block; std::vector<N> notes; double el, er; std::tuple<int, float, float> at[6]; };
  12620. auto check = [] (const Golden& g) {
  12621. const auto y = lynx::drumaxx::renderReference (drumaxxTestState (g.eq), g.notes, (double) g.fs, 20000, g.block);
  12622. CHECK (y.size() == 2 * 20000);
  12623. if (y.size() != 2 * 20000)
  12624. return;
  12625. double el = 0.0, er = 0.0;
  12626. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  12627. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  12628. CHECK (std::abs (el - g.el) <= 1e-6 * g.el && std::abs (er - g.er) <= 1e-6 * g.er);
  12629. for (const auto& [i, l, r] : g.at)
  12630. CHECK (std::abs (y[2 * (std::size_t) i] - l) <= 1e-6f * std::max (1e-16f, std::abs (l))
  12631. && std::abs (y[2 * (std::size_t) i + 1] - r) <= 1e-6f * std::max (1e-16f, std::abs (r)));
  12632. };
  12633. // master EQ and limiter on: velocities, Final.Vol, pitch offsets, the cut group (pads 3…5)
  12634. check ({ true, 44100, 512,
  12635. { { 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 },
  12636. { 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 },
  12637. { 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 } },
  12638. 2698.372377185949, 3678.285731410973,
  12639. { { 100, 0.0f, 0.0f }, { 1000, -0.0034233136102557182f, -0.005708245560526848f }, { 2500, -0.41297006607055664f, -0.0276018138974905f },
  12640. { 4000, -0.04906991869211197f, -0.7206904292106628f }, { 7777, 0.7544509172439575f, 0.7584841847419739f },
  12641. { 15000, -0.13438990712165833f, -0.6798369884490967f } } });
  12642. // EQ off, 48 kHz in 333-sample blocks, two pads on the same sample
  12643. check ({ false, 48000, 333,
  12644. { { 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 },
  12645. { 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 } },
  12646. 2241.008810618806, 1814.5219804412964,
  12647. { { 100, 0.0f, 0.0f }, { 1000, -0.32539352774620056f, -0.7439078688621521f }, { 2500, -0.1711389720439911f, -0.39125433564186096f },
  12648. { 4000, 0.15524788200855255f, 0.3445436358451843f }, { 7777, 0.5472834706306458f, 0.08737402409315109f },
  12649. { 15000, -0.03079034946858883f, -0.06140122935175896f } } });
  12650. // 96 kHz in 64-sample blocks, an octave down
  12651. check ({ true, 96000, 64,
  12652. { { 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 },
  12653. { 10000, 51, 1.0, 1.0, -1200.0 } },
  12654. 3522.8420617665834, 5746.555795626526,
  12655. { { 100, 0.0f, 0.0f }, { 1000, -0.04133236035704613f, -0.09954667836427689f }, { 2500, 0.2800408899784088f, -0.9350972175598145f },
  12656. { 4000, 0.9537044167518616f, 0.8892703056335449f }, { 7777, 0.8376388549804688f, -0.14166420698165894f },
  12657. { 15000, -0.05926012247800827f, -0.5840796828269958f } } });
  12658. // the instrument: a pad sounds on its key and dies away; keys without a pad stay silent
  12659. {
  12660. auto inst = lynx::makeInstrument (lynx::InstrumentKind::drumaxx);
  12661. lynx::ChannelDef def;
  12662. def.instrument = lynx::InstrumentKind::drumaxx;
  12663. def.pluginState = st;
  12664. inst->configure (def, 140.0);
  12665. inst->prepare (44100.0);
  12666. std::vector<float> l (512, 0.0f), r (512, 0.0f);
  12667. inst->noteOn (90, 1.0f);
  12668. inst->render (l.data(), r.data(), 512, 1.0f, 1.0f);
  12669. CHECK (inst->idle() && std::all_of (l.begin(), l.end(), [] (float v) { return v == 0.0f; }));
  12670. inst->noteOn (36, 0.8f);
  12671. inst->render (l.data(), r.data(), 512, 1.0f, 1.0f);
  12672. double e = 0.0;
  12673. for (auto v : l)
  12674. e += (double) v * v;
  12675. CHECK (! inst->idle() && e > 1e-3);
  12676. for (int k = 0; k < 200 && ! inst->idle(); ++k)
  12677. inst->render (l.data(), r.data(), 512, 1.0f, 1.0f);
  12678. CHECK (inst->idle());
  12679. }
  12680. // import: a Drumaxx channel plays on the exact engine with its raw state
  12681. {
  12682. FlpBuilder b;
  12683. b.ascii (0xC7, "25.2.5.5319");
  12684. b.u8 (0x40); b.u16 (0);
  12685. b.u8 (0x15); b.u8 (2);
  12686. b.utf16 (0xC9, u"Drumaxx");
  12687. b.data (0xD5, st);
  12688. lynx::FlpImportOptions options;
  12689. lynx::FlpImportReport report;
  12690. options.report = &report;
  12691. const auto song = lynx::importFlp (flp::parseProject (b.bytes), options);
  12692. CHECK (song.channels.size() == 1u);
  12693. if (song.channels.size() == 1u)
  12694. CHECK (song.channels[0].instrument == lynx::InstrumentKind::drumaxx && song.channels[0].pluginState == st);
  12695. CHECK (report.converted["Drumaxx"] == 1);
  12696. }
  12697. }
  12698. // --- REA round 68: FL Slayer (exact engine) and Fruity Dance (visual only) ------------------------------------------
  12699. namespace
  12700. {
  12701. std::string slayerDll()
  12702. {
  12703. if (realFlpDir.empty())
  12704. return {};
  12705. return (std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "FL Slayer" / "Slayer_Engine.dll").string();
  12706. }
  12707. std::vector<std::uint8_t> slayerPreset (const char* name)
  12708. {
  12709. if (realFlpDir.empty())
  12710. return {};
  12711. const auto file = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "FL Slayer" / (std::string (name) + ".fst");
  12712. const auto states = pluginStatesIn (file, "FL Slayer");
  12713. return states.empty() ? std::vector<std::uint8_t> {} : states.front();
  12714. }
  12715. } // namespace
  12716. TEST (slayer_state)
  12717. {
  12718. // the saved state: int version, then the parameter integers (version 0: 28, version 1: 29 with the mod wheel)
  12719. std::vector<std::uint8_t> v0 (4 + 28 * 4, 0), v1 (4 + 29 * 4, 0);
  12720. const std::int32_t one = 1, wheel = 77, mode = 5;
  12721. std::memcpy (v1.data(), &one, 4);
  12722. std::memcpy (v1.data() + 4 + 28 * 4, &wheel, 4);
  12723. std::memcpy (v0.data() + 4 + 24 * 4, &mode, 4);
  12724. CHECK (lynx::slayerStateUsable (v0) && lynx::slayerStateUsable (v1));
  12725. CHECK (! lynx::slayerStateUsable (std::vector<std::uint8_t> (v0.begin(), v0.end() - 1)));
  12726. auto v2 = v1;
  12727. v2[0] = 2;
  12728. CHECK (! lynx::slayerStateUsable (v2));
  12729. const auto p0 = lynx::slayerStateParams (v0), p1 = lynx::slayerStateParams (v1);
  12730. CHECK (p0.size() == 29u && p0[24] == 5 && p0[28] == 0 && p1.size() == 29u && p1[28] == 77);
  12731. // the controls' maxima (the front-end sends value / max)
  12732. CHECK (lynx::slayerParamMax (0) == 6 && lynx::slayerParamMax (20) == 15 && lynx::slayerParamMax (24) == 5 && lynx::slayerParamMax (28) == 127
  12733. && lynx::slayerParamMax (3) == 65535);
  12734. // MIDI velocity from Init.Vol (0 → 0, which the engine takes as a note-off; FL's full velocity → 127)
  12735. CHECK (lynx::slayer::midiVelocity (0.0) == 0 && lynx::slayer::midiVelocity (1.0) == 99 && lynx::slayer::midiVelocity (2.0) == 126
  12736. && lynx::slayer::midiVelocity ((std::pow (11.0, 1.28) - 1.0) / 10.0) == 127);
  12737. }
  12738. TEST (slayer_engine)
  12739. {
  12740. const auto dll = slayerDll();
  12741. if (dll.empty() || ! lynx::slayerDllUsable (dll))
  12742. return; // no FL installation
  12743. // against the installation's Slayer_Engine.dll (EXEC, REA_rodada68 §7: the DLL driven like FL's front-end drives it,
  12744. // host blocks of 512 at 44.1 kHz, tempo 120): bit-identical output. C3 vel 90 1024–20000, G3 vel 120 8000–30000,
  12745. // E4 vel 40 16000–40000, C5 vel 127 from 30000.
  12746. struct Sample { int at; float l, r; };
  12747. struct Golden { const char* preset; double el, er; Sample s[4]; };
  12748. const Golden goldens[] {
  12749. { "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 } } },
  12750. { "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 } } },
  12751. { "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 } } },
  12752. { "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 } } },
  12753. { "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 } } },
  12754. { "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 } } },
  12755. { "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 } } },
  12756. { "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 } } },
  12757. { "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 } } },
  12758. { "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 } } },
  12759. { "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 } } },
  12760. { "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 } } },
  12761. };
  12762. const std::vector<lynx::slayer::RefNote> notes = { { 1024, 48, 90, 20000 }, { 8000, 55, 120, 30000 }, { 16000, 64, 40, 40000 }, { 30000, 72, 127, -1 } };
  12763. constexpr int length = 44100;
  12764. int checked = 0;
  12765. for (const auto& g : goldens)
  12766. {
  12767. const auto st = slayerPreset (g.preset);
  12768. CHECK (lynx::slayerStateUsable (st));
  12769. if (! lynx::slayerStateUsable (st))
  12770. continue;
  12771. const auto y = lynx::slayer::renderReference (lynx::slayerStateParams (st), dll, notes, 44100.0, length);
  12772. double el = 0.0, er = 0.0;
  12773. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  12774. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  12775. 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;
  12776. for (const auto& smp : g.s)
  12777. ok = ok && y[2 * (std::size_t) smp.at] == smp.l && y[2 * (std::size_t) smp.at + 1] == smp.r;
  12778. if (! ok)
  12779. 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);
  12780. CHECK (ok);
  12781. ++checked;
  12782. }
  12783. CHECK (checked == 12);
  12784. // the instrument: the engine primed with one sample, FL's velocity curve, the front-end's gains from the channel's
  12785. // MIDI pan / volume (REA_rodada79 §2: twice the channel gain through the host's integer-pan law); a note with
  12786. // velocity 0.8 is the reference note one sample later
  12787. const auto st = slayerPreset ("BASS Ebass 2");
  12788. lynx::ChannelDef c;
  12789. c.instrument = lynx::InstrumentKind::slayer;
  12790. c.pluginState = st;
  12791. c.samplePath = dll;
  12792. auto inst = lynx::makeInstrument (lynx::InstrumentKind::slayer);
  12793. inst->configure (c, 120.0);
  12794. inst->prepare (44100.0);
  12795. std::vector<float> l (16384, 0.0f), r (16384, 0.0f);
  12796. for (int b = 0; b < 32; ++b)
  12797. {
  12798. if (b == 0)
  12799. inst->noteOn (60, 0.8f);
  12800. if (b == 20)
  12801. inst->noteOff (60);
  12802. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 0.5f, 0.25f);
  12803. }
  12804. const int vel = lynx::slayer::midiVelocity ((std::pow (11.0, 1.28 * 0.8) - 1.0) / 10.0);
  12805. const auto ref = lynx::slayer::renderReference (lynx::slayerStateParams (st), dll, { { 1, 60, vel, 1 + 20 * 512 } }, 44100.0, 16385);
  12806. // LYNX's channel gains 0.5 / 0.25 = gain 0.395…, pan −0.4097 → MIDI pan −26
  12807. const auto chanVol = (float) std::sqrt ((0.25 + 0.0625) * 0.5);
  12808. float hl = 0.0f, hr = 0.0f;
  12809. lynx::slayer::hostGains (lynx::slayer::midiPan (std::atan2 (0.25, 0.5) * (4.0 / 3.141592653589793) - 1.0), chanVol, hl, hr);
  12810. CHECK (lynx::slayer::midiPan (std::atan2 (0.25, 0.5) * (4.0 / 3.141592653589793) - 1.0) == -26);
  12811. double diff = 0.0, energy = 0.0;
  12812. for (int i = 0; i < 16384; ++i)
  12813. {
  12814. diff = std::max ({ diff, (double) std::abs (l[(std::size_t) i] - ref[2 * (std::size_t) (i + 1)] * hl),
  12815. (double) std::abs (r[(std::size_t) i] - ref[2 * (std::size_t) (i + 1) + 1] * hr) });
  12816. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  12817. }
  12818. CHECK (diff == 0.0 && energy > 1.0 && vel == 102);
  12819. // every preset of the installation: usable and sounding
  12820. int presets = 0, sounding = 0;
  12821. const auto folder = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "FL Slayer";
  12822. if (std::filesystem::exists (folder))
  12823. for (const auto& entry : std::filesystem::directory_iterator (folder))
  12824. for (const auto& s : pluginStatesIn (entry.path(), "FL Slayer"))
  12825. {
  12826. ++presets;
  12827. const auto y = lynx::slayer::renderReference (lynx::slayerStateParams (s), dll, { { 1024, 52, 100, 20000 } }, 44100.0, 22050);
  12828. double e = 0.0;
  12829. bool finite = true;
  12830. for (auto v : y)
  12831. finite = finite && std::isfinite (v), e += (double) v * v;
  12832. CHECK (finite);
  12833. sounding += e > 1.0 ? 1 : 0;
  12834. }
  12835. std::printf (" FL Slayer presets: %d, %d sounding\n", presets, sounding);
  12836. CHECK (presets == 64 && sounding == 64);
  12837. }
  12838. // FL Slayer's host levels and live parameters (REA_rodada79): channel pan / volume / pitch arrive as MIDI events
  12839. // (ProcessEvent 2/3/4), automation and settings edits as setParameter calls on the running engine
  12840. TEST (slayer_host_levels_and_automation)
  12841. {
  12842. using namespace lynx::slayer;
  12843. CHECK (pitchBend (0) == 8192 && pitchBend (100) == 8874 && pitchBend (-100) == 7510 && pitchBend (1200) == 0x3fff
  12844. && pitchBend (-1200) == 0 && pitchBend (-1199) == 7 && pitchBend (5000) == 0x3fff);
  12845. CHECK (midiPan (0.0) == 0 && midiPan (1.0) == 64 && midiPan (-1.0) == -64 && midiPan (-0.5) == -32
  12846. && midiPan (99.0 / 6400.0) == 0 && midiPan (-199.0 / 6400.0) == -1 && midiPan (2.0) == 64);
  12847. float l0 = 0.0f, r0 = 0.0f, l1 = 0.0f, r1 = 0.0f;
  12848. hostGains (0, 0.5f, l0, r0); // the front-end's own start values (volume 1 after the ×2, centre)
  12849. hostGains (64, 1.0f, l1, r1);
  12850. CHECK (l0 == 0.70710677f && r0 == 0.70710677f && std::abs (l1) < 1e-15f && r1 == 2.0f);
  12851. const auto dll = slayerDll();
  12852. if (dll.empty() || ! lynx::slayerDllUsable (dll))
  12853. return;
  12854. const auto st = slayerPreset ("DIST Dire Straits");
  12855. const auto params = lynx::slayerStateParams (st);
  12856. lynx::ChannelDef c;
  12857. c.instrument = lynx::InstrumentKind::slayer;
  12858. c.pluginState = st;
  12859. c.samplePath = dll;
  12860. c.sampler.pitch = 1.5f; // channel pitch +150 cents → bend 9216; no root shift
  12861. c.modelParams = { 0.0f };
  12862. auto inst = lynx::makeInstrument (lynx::InstrumentKind::slayer);
  12863. inst->configure (c, 120.0);
  12864. inst->prepare (44100.0);
  12865. const int blocks = 40, n = 512;
  12866. std::vector<float> l ((std::size_t) (blocks * n), 0.0f), r ((std::size_t) (blocks * n), 0.0f);
  12867. auto edited = st;
  12868. const std::int32_t newDrive = 60000; // parameter 13 (amp drive), edited in the settings at block 25
  12869. std::memcpy (edited.data() + 4 + 4 * 13, &newDrive, 4);
  12870. for (int b = 0; b < blocks; ++b)
  12871. {
  12872. if (b == 0)
  12873. inst->noteOn (52, 0.8f);
  12874. if (b == 10)
  12875. inst->setParameter (20, 9.0f / 15.0f); // automation: effect 9 (chorus)
  12876. if (b == 18)
  12877. inst->setParameter (28, 0.5f); // automation: the mod wheel → CC 1 64
  12878. if (b == 25)
  12879. {
  12880. c.pluginState = edited;
  12881. inst->configure (c, 120.0);
  12882. }
  12883. inst->render (l.data() + n * b, r.data() + n * b, n, 1.0f, 1.0f);
  12884. }
  12885. lynx::slayer::RefOptions opt;
  12886. opt.midi.push_back ({ 1, 0xe0, 9216 & 0x7f, 9216 >> 7 });
  12887. opt.params = { { 1 + 10 * n, 20, 9 }, { 1 + 18 * n, 28, 64 }, { 1 + 25 * n, 13, newDrive } };
  12888. const int vel = lynx::slayer::midiVelocity ((std::pow (11.0, 1.28 * 0.8) - 1.0) / 10.0);
  12889. const auto ref = lynx::slayer::renderReference (params, dll, { { 1, 52, vel, -1 } }, 44100.0, blocks * n + 1, opt);
  12890. float hl = 0.0f, hr = 0.0f;
  12891. hostGains (0, 1.0f, hl, hr);
  12892. double diff = 0.0, energy = 0.0;
  12893. for (int i = 0; i < blocks * n; ++i)
  12894. {
  12895. diff = std::max ({ diff, (double) std::abs (l[(std::size_t) i] - ref[2 * (std::size_t) (i + 1)] * hl),
  12896. (double) std::abs (r[(std::size_t) i] - ref[2 * (std::size_t) (i + 1) + 1] * hr) });
  12897. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  12898. }
  12899. CHECK (diff == 0.0 && energy > 1.0);
  12900. // the changes really change the sound: the same render without them differs
  12901. const auto plain = lynx::slayer::renderReference (params, dll, { { 1, 52, vel, -1 } }, 44100.0, blocks * n + 1);
  12902. CHECK (plain != ref);
  12903. }
  12904. TEST (slayer_and_fruity_dance_import)
  12905. {
  12906. // FL Slayer plays on the exact engine when the installation's engine DLL is there (otherwise it stays unsupported);
  12907. // Fruity Dance (a visual plugin) becomes a silent channel that keeps its state
  12908. std::vector<std::uint8_t> slayer (4 + 28 * 4, 0), dance (40, 1);
  12909. FlpBuilder b;
  12910. b.ascii (0xC7, "25.2.5.5319");
  12911. int index = 0;
  12912. for (const auto& [name, state] : { std::pair<std::u16string, std::vector<std::uint8_t>> { u"FL Slayer", slayer }, { u"Fruity Dance", dance } })
  12913. {
  12914. b.u8 (0x40); b.u16 ((std::uint16_t) index++);
  12915. b.u8 (0x15); b.u8 (2);
  12916. b.utf16 (0xC9, name);
  12917. b.data (0xD5, state);
  12918. }
  12919. const auto project = flp::parseProject (b.bytes);
  12920. const auto plain = lynx::importFlp (project);
  12921. CHECK (plain.channels.size() == 2u);
  12922. if (plain.channels.size() == 2u)
  12923. {
  12924. CHECK (plain.channels[0].instrument != lynx::InstrumentKind::slayer);
  12925. CHECK (plain.channels[1].instrument == lynx::InstrumentKind::fruitydance && plain.channels[1].pluginState == dance);
  12926. }
  12927. auto inst = lynx::makeInstrument (lynx::InstrumentKind::fruitydance);
  12928. inst->prepare (44100.0);
  12929. std::vector<float> l (1024, 0.0f), r (1024, 0.0f);
  12930. inst->noteOn (60, 1.0f);
  12931. inst->render (l.data(), r.data(), 1024, 1.0f, 1.0f);
  12932. CHECK (inst->idle() && std::all_of (l.begin(), l.end(), [] (float v) { return v == 0.0f; }));
  12933. const auto dll = slayerDll();
  12934. if (dll.empty() || ! lynx::slayerDllUsable (dll))
  12935. return;
  12936. lynx::FlpImportOptions withFiles;
  12937. withFiles.roots = { std::filesystem::path (realFlpDir).parent_path().string(), realFlpDir, "" };
  12938. withFiles.fileExists = [] (const std::string& path) {
  12939. auto local = path;
  12940. std::replace (local.begin(), local.end(), '\\', '/');
  12941. return std::filesystem::exists (local);
  12942. };
  12943. const auto song = lynx::importFlp (project, withFiles);
  12944. CHECK (song.channels.size() == 2u && song.channels[0].instrument == lynx::InstrumentKind::slayer && song.channels[0].pluginState == slayer
  12945. && ! song.channels[0].samplePath.empty());
  12946. }
  12947. #include "WaveAnalysisTests.inc"
  12948. #include "UiPolish2Tests.inc"
  12949. #include "UiToolbarTests.inc"
  12950. #include "LuxeVerbTests.inc"
  12951. #include "ConvolverTests.inc"
  12952. // --- Newtone / Newtime (REA_rodada73) -----------------------------------------------------------------------------
  12953. namespace newtonetest
  12954. {
  12955. 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))); }
  12956. 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))); }
  12957. void record (std::vector<std::uint8_t>& b, int id, const std::vector<std::uint8_t>& d)
  12958. {
  12959. put32 (b, id);
  12960. put64 (b, (std::int64_t) d.size());
  12961. b.insert (b.end(), d.begin(), d.end());
  12962. }
  12963. // a state with our own layout reading (REA_rodada73 §2): options, the wrapper record around the engine XML, the source path
  12964. std::vector<std::uint8_t> state (int version, std::uint32_t flags, const std::string& xml, const std::string& source)
  12965. {
  12966. std::vector<std::uint8_t> b, r1, r2;
  12967. put32 (b, version);
  12968. put32 (r1, 727);
  12969. put32 (r1, 582);
  12970. put32 (r1, (std::int32_t) flags);
  12971. r1.resize (0x28, 0);
  12972. record (b, 1, r1);
  12973. std::vector<std::uint8_t> chunk { 'V', 'C', '2', '!' };
  12974. put32 (chunk, (std::int32_t) xml.size() + 1);
  12975. chunk.insert (chunk.end(), xml.begin(), xml.end());
  12976. chunk.push_back (0);
  12977. put32 (r2, -9);
  12978. r2.push_back (0x0c);
  12979. put32 (r2, -2);
  12980. put64 (r2, (std::int64_t) chunk.size());
  12981. put32 (r2, 0);
  12982. r2.insert (r2.end(), chunk.begin(), chunk.end());
  12983. record (b, 2, r2);
  12984. record (b, 3, std::vector<std::uint8_t> (source.begin(), source.end()));
  12985. record (b, 4, std::vector<std::uint8_t> (12, 0));
  12986. record (b, 5, std::vector<std::uint8_t> (16, 0));
  12987. record (b, 6, std::vector<std::uint8_t> (32, 0));
  12988. return b;
  12989. }
  12990. const char* xmlWithTrack (bool edited)
  12991. {
  12992. return edited ? "<NewToneProject NumOfTracks=\"1\"><Tracks><Trk_0 TrackNum=\"0\" Type=\"class CzplJPositionablePSOLASource\" "
  12993. "FilePath=\"C:\\tmp\\a&amp;b.wav\"><AudioObject SelfBPM=\"120.0\" OriginalFileLength=\"1000\" GlobalTune=\"0.0\">"
  12994. "<Pitch_0 fOrigPitch=\"60.0\" fCurPitch=\"60.0\" iOrigLength=\"500\" iCurLength=\"500\"/>"
  12995. "<Pitch_1 fOrigPitch=\"62.0\" fCurPitch=\"64.0\" iOrigLength=\"500\" iCurLength=\"500\"/></AudioObject></Trk_0></Tracks></NewToneProject>"
  12996. : "<NewToneProject NumOfTracks=\"1\"><Tracks><Trk_0 TrackNum=\"0\" Type=\"class CzplJTimelineWarpSource\" "
  12997. "FilePath=\"x.wav\"><AudioObject SelfBPM=\"80.0\" OriginalFileLength=\"1000\">"
  12998. "<Pitch_0 fOrigPitch=\"60.0\" fCurPitch=\"60.0\" fCurVolume=\"1.0\"/></AudioObject></Trk_0></Tracks></NewToneProject>";
  12999. }
  13000. } // namespace newtonetest
  13001. TEST (newtone_state)
  13002. {
  13003. using namespace newtonetest;
  13004. const auto a = lynx::newtonefx::parse (state (0, 5, xmlWithTrack (true), "%FLStudioUserData%\\vox.wav"));
  13005. CHECK (a.has_value());
  13006. CHECK (a->version == 0 && a->editorWidth == 727 && a->editorHeight == 582 && a->slave());
  13007. CHECK (a->sourcePath == "%FLStudioUserData%\\vox.wav");
  13008. CHECK (a->tracks.size() == 1 && a->tracks[0].notes == 2 && a->tracks[0].frames == 1000 && ! a->tracks[0].timeWarp);
  13009. CHECK (a->tracks[0].filePath == "C:\\tmp\\a&b.wav" && a->tracks[0].selfBpm == 120.0);
  13010. CHECK (a->tracks[0].edited && a->edited()); // note 1 moved from 62 to 64
  13011. const auto b = lynx::newtonefx::parse (state (1, 0x41, xmlWithTrack (false), ""));
  13012. CHECK (b.has_value() && ! b->slave() && b->sourcePath.empty());
  13013. CHECK (b->tracks.size() == 1 && b->tracks[0].timeWarp && ! b->edited());
  13014. // an empty project, as the installation's presets
  13015. const auto c = lynx::newtonefx::parse (state (0, 1, "<NewToneProject NumOfTracks=\"0\"><Tracks/></NewToneProject>", ""));
  13016. CHECK (c.has_value() && c->tracks.empty());
  13017. // broken: other version, truncated record, no options record
  13018. CHECK (! lynx::newtonefx::parse (state (2, 1, "", "")));
  13019. auto cut = state (0, 1, xmlWithTrack (true), "");
  13020. cut.resize (cut.size() - 3);
  13021. CHECK (! lynx::newtonefx::parse (cut));
  13022. CHECK (! lynx::newtonefx::parse ({ 0, 0, 0, 0 }));
  13023. // the FL import keeps the state and takes the slave switch as the editable parameter
  13024. const auto raw = state (0, 5, xmlWithTrack (false), "");
  13025. const auto slot = lynx::effectFromFl ("Newtone", raw, 120.0);
  13026. CHECK (slot && slot->kind == lynx::EffectKind::newtone && slot->state == raw && slot->params[0] == 1.0f);
  13027. const auto slot2 = lynx::effectFromFl ("Newtime", state (1, 0x41, xmlWithTrack (false), ""), 120.0);
  13028. CHECK (slot2 && slot2->kind == lynx::EffectKind::newtime && slot2->params[0] == 0.0f);
  13029. CHECK (lynx::formatEffectParam (lynx::EffectKind::newtone, 0, 1.0f, 120.0) == "on");
  13030. // the installation's own states (plugin database and the playlist's pitch/warp correction tracks) [DATA]
  13031. if (! realFlpDir.empty())
  13032. {
  13033. int found = 0, read = 0;
  13034. for (const char* rel : { "System/PitchCorrectionTrack.fst", "System/WarpCorrectionTrack.fst",
  13035. "Patches/Plugin database/Effects/Misc/Newtone.fst", "Patches/Plugin database/Effects/Misc/Newtime.fst" })
  13036. {
  13037. const auto path = std::filesystem::path (realFlpDir) / rel;
  13038. if (! std::filesystem::exists (path))
  13039. continue;
  13040. ++found;
  13041. std::ifstream f (path, std::ios::binary);
  13042. std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (f)), std::istreambuf_iterator<char>());
  13043. const auto fx = lynx::effectFromFlPreset (bytes, 120.0);
  13044. read += fx && (fx->kind == lynx::EffectKind::newtone || fx->kind == lynx::EffectKind::newtime) && ! fx->state.empty()
  13045. && fx->params[0] == 0.0f && lynx::newtonefx::parse (fx->state)->tracks.empty();
  13046. }
  13047. std::printf (" %d Newtone/Newtime states, %d read\n", found, read);
  13048. CHECK (read == found);
  13049. }
  13050. }
  13051. TEST (newtone_engine)
  13052. {
  13053. // REA_rodada73 §3-4 [EXEC]: the input passes unchanged unless slave mode is on and the song plays; then the source
  13054. // sample is added from the song time on (at the source's own rate)
  13055. auto sample = std::make_shared<lynx::SampleData>();
  13056. sample->sampleRate = 44100.0;
  13057. for (int i = 0; i < 3000; ++i)
  13058. sample->left.push_back (0.001f * (float) (i % 97) - 0.04f);
  13059. const double sr = 44100.0, bpm = 120.0;
  13060. const auto tps = lynx::ticksPerSample (bpm, sr);
  13061. auto fx = lynx::newtonefx::make (sample);
  13062. fx->prepare (sr);
  13063. lynx::EffectParams on {}, off {};
  13064. on[0] = 1.0f;
  13065. std::vector<float> l (256), r (256), in (256);
  13066. auto block = [&] (double tick, bool playing, const lynx::EffectParams& p) {
  13067. for (int i = 0; i < 256; ++i)
  13068. l[(std::size_t) i] = r[(std::size_t) i] = in[(std::size_t) i] = 0.25f * std::sin (0.01f * (float) (i + tick));
  13069. fx->setSongPosition (tick);
  13070. fx->setPlaying (playing);
  13071. fx->process (l.data(), r.data(), 256, p, bpm);
  13072. };
  13073. auto same = [&] { return std::equal (l.begin(), l.end(), in.begin()) && std::equal (r.begin(), r.end(), in.begin()); };
  13074. block (0.0, true, off);
  13075. CHECK (same()); // slave off
  13076. block (0.0, false, on);
  13077. CHECK (same()); // not playing
  13078. // playing from the start: in + source, sample by sample, over consecutive blocks
  13079. bool exact = true;
  13080. for (int b = 0; b < 4; ++b)
  13081. {
  13082. block (b * 256 * tps, true, on);
  13083. for (int i = 0; i < 256; ++i)
  13084. exact = exact && l[(std::size_t) i] == in[(std::size_t) i] + sample->left[(std::size_t) (b * 256 + i)]
  13085. && r[(std::size_t) i] == l[(std::size_t) i];
  13086. }
  13087. CHECK (exact);
  13088. // a jump re-seeks to the song time; past the end of the sample only the input remains
  13089. block (2000 * tps, true, on);
  13090. CHECK (l[0] == in[0] + sample->left[2000] && l[255] == in[255] + sample->left[2255]);
  13091. block (2900 * tps, true, on);
  13092. CHECK (l[99] == in[99] + sample->left[2999] && l[100] == in[100]);
  13093. // stopping and playing again: from the new song time
  13094. block (0.0, false, on);
  13095. CHECK (same());
  13096. block (10 * tps, true, on);
  13097. CHECK (l[0] == in[0] + sample->left[10]);
  13098. // without its sample: a pass-through
  13099. auto none = lynx::makeEffect (lynx::EffectKind::newtime);
  13100. none->prepare (sr);
  13101. for (int i = 0; i < 256; ++i)
  13102. l[(std::size_t) i] = r[(std::size_t) i] = in[(std::size_t) i] = 0.1f * (float) i / 256.0f;
  13103. none->setPlaying (true);
  13104. none->process (l.data(), r.data(), 256, on, bpm);
  13105. CHECK (same());
  13106. }
  13107. TEST (newtone_rack_follows_transport)
  13108. {
  13109. // the mixer gives the effect the transport, and keeps it running on a silent insert (no smart disable)
  13110. auto song = lynx::Song::makeDefault();
  13111. for (auto& p : song.patterns)
  13112. p.notes.clear();
  13113. song.bpm = 120.0;
  13114. auto slot = lynx::makeEffectSlot (lynx::EffectKind::newtone);
  13115. slot.params[0] = 1.0f;
  13116. slot.samplePath = "newtone-test.wav";
  13117. song.mixer[1].effects.push_back (slot);
  13118. auto sample = std::make_shared<lynx::SampleData>();
  13119. sample->sampleRate = 48000.0;
  13120. sample->left.assign (48000, 0.5f);
  13121. int loads = 0;
  13122. lynx::RackState::SampleProvider provider = [&] (const std::string& path) -> std::shared_ptr<const lynx::SampleData> {
  13123. ++loads;
  13124. return path == "newtone-test.wav" ? sample : nullptr;
  13125. };
  13126. const double sr = 48000.0;
  13127. auto state = lynx::RackState::build (song, nullptr, sr, provider);
  13128. CHECK (loads == 1);
  13129. lynx::RackRenderer rr;
  13130. rr.prepare (sr);
  13131. std::vector<float> l (512), r (512);
  13132. auto peakOf = [&] (bool playing, double tick) {
  13133. float peak = 0.0f;
  13134. for (int b = 0; b < 8; ++b)
  13135. {
  13136. rr.process (state.get(), playing, tick + b * 512 * lynx::ticksPerSample (song.bpm, sr), 512, l.data(), r.data());
  13137. for (auto v : l)
  13138. peak = std::max (peak, std::abs (v));
  13139. }
  13140. return peak;
  13141. };
  13142. CHECK (peakOf (false, 0.0) == 0.0f);
  13143. CHECK (peakOf (true, 0.0) > 0.1f);
  13144. }
  13145. #include "HardcoreTests.inc"
  13146. #include "DistructorTests.inc"
  13147. // --- missing files (core/MissingFiles.h) ------------------------------------------------------------------------
  13148. TEST (missing_files_names_and_scores)
  13149. {
  13150. CHECK (lynx::pathFileName ("C:\\Samples\\Drums\\Kick 01.wav") == "Kick 01.wav");
  13151. CHECK (lynx::pathFileName ("/home/u/loops/beat.flac") == "beat.flac");
  13152. CHECK (lynx::pathFileName ("D:/mixed\\sep/x.wav/") == "x.wav");
  13153. CHECK (lynx::pathFileName ("plain.wav") == "plain.wav");
  13154. CHECK (lynx::relocationScore ("C:\\Old\\Drums\\Kicks\\kick.wav", "/new/Drums/Kicks/KICK.WAV") == 3);
  13155. CHECK (lynx::relocationScore ("C:\\Old\\Drums\\Kicks\\kick.wav", "/new/other/kick.wav") == 1);
  13156. CHECK (lynx::relocationScore ("C:\\Old\\kick.wav", "/new/kick2.wav") == 0);
  13157. }
  13158. TEST (missing_files_find_in_song)
  13159. {
  13160. lynx::Song song;
  13161. lynx::ChannelDef a, b, c, d, e;
  13162. a.name = "Kick", a.instrument = lynx::InstrumentKind::sampler, a.samplePath = "C:\\gone\\kick.wav";
  13163. b.name = "Vox", b.instrument = lynx::InstrumentKind::audioClip, b.samplePath = "C:\\gone\\vox.wav";
  13164. c.name = "Here", c.instrument = lynx::InstrumentKind::sampler, c.samplePath = "C:\\here\\snare.wav";
  13165. d.name = "GMS", d.instrument = lynx::InstrumentKind::gms, d.samplePath = "C:\\FL\\gms waves"; // installation data: not listed
  13166. e.name = "Kick 2", e.instrument = lynx::InstrumentKind::sampler, e.samplePath = "c:/GONE/KICK.wav"; // same file, other spelling
  13167. lynx::SampleZone z;
  13168. z.path = "C:\\gone\\piano_c4.wav";
  13169. a.zones.push_back (z);
  13170. song.channels = { a, b, c, d, e };
  13171. song.mixer.resize (3);
  13172. lynx::EffectSlot fx;
  13173. fx.samplePath = "C:\\gone\\tune.wav";
  13174. lynx::EffectSlot parallel;
  13175. parallel.branches.push_back ({ { fx }, 1.0f });
  13176. song.mixer[2].effects.push_back (parallel);
  13177. song.mixer[2].name = "Lead";
  13178. const auto exists = [] (const std::string& p) { return p.find ("here") != std::string::npos || p.find ("FL") != std::string::npos; };
  13179. auto missing = lynx::findMissingSamples (song, exists);
  13180. CHECK (missing.size() == 4);
  13181. CHECK (missing.size() == 4 && missing[0].path == "C:\\gone\\kick.wav" && missing[0].usedBy.size() == 2 && missing[0].usedBy[1] == "Kick 2");
  13182. CHECK (missing.size() == 4 && missing[1].path == "C:\\gone\\piano_c4.wav" && missing[1].usedBy[0] == "Kick");
  13183. CHECK (missing.size() == 4 && missing[2].usedBy[0] == "Audio clip: Vox");
  13184. CHECK (missing.size() == 4 && missing[3].usedBy[0] == "Lead: effect");
  13185. // candidates from a search: the deeper path match wins; one stays missing
  13186. const std::vector<std::string> hits { "/lib/other/kick.wav", "/lib/gone/kick.wav", "/lib/x/vox.wav", "/lib/y/tune.wav" };
  13187. CHECK (lynx::relocateByName (missing, hits) == 3);
  13188. CHECK (missing[0].found == "/lib/gone/kick.wav");
  13189. CHECK (missing[1].found.empty());
  13190. missing[3].skipped = true; // skipped: not applied
  13191. const auto map = lynx::relocationMap (missing);
  13192. CHECK (map.size() == 2);
  13193. CHECK (lynx::applyRelocations (song, map) == 3); // both spellings of kick.wav + vox
  13194. CHECK (song.channels[0].samplePath == "/lib/gone/kick.wav" && song.channels[4].samplePath == "/lib/gone/kick.wav");
  13195. CHECK (song.channels[1].samplePath == "/lib/x/vox.wav");
  13196. CHECK (song.channels[2].samplePath == "C:\\here\\snare.wav");
  13197. CHECK (song.mixer[2].effects[0].branches[0].chain[0].samplePath == "C:\\gone\\tune.wav");
  13198. lynx::Song copy = song;
  13199. CHECK (lynx::applyRelocations (copy, { { "C:\\gone\\tune.wav", "/t.wav" } }) == 1 && copy.mixer[2].effects[0].branches[0].chain[0].samplePath == "/t.wav");
  13200. }
  13201. TEST (missing_files_folder_search)
  13202. {
  13203. namespace fs = std::filesystem;
  13204. const auto root = fs::temp_directory_path() / "lynx-missing-test";
  13205. fs::remove_all (root);
  13206. fs::create_directories (root / "a" / "deep" / "Kicks");
  13207. fs::create_directories (root / "b");
  13208. for (const auto& f : { root / "a" / "deep" / "Kicks" / "Kick.WAV", root / "b" / "kick.wav", root / "b" / "hat.wav", root / "b" / "unrelated.wav" })
  13209. std::ofstream (f) << "x";
  13210. std::vector<lynx::MissingFile> missing { { "C:\\Old\\Kicks\\kick.wav", { "Kick" }, {}, false }, { "C:\\Old\\hat.wav", { "Hat" }, {}, false },
  13211. { "C:\\Old\\ride.wav", { "Ride" }, {}, false } };
  13212. int listed = 0;
  13213. auto list = [&] (const std::string& folder, const std::function<bool (const std::string&, bool)>& onEntry) {
  13214. for (const auto& entry : fs::directory_iterator (folder))
  13215. {
  13216. ++listed;
  13217. if (! onEntry (entry.path().string(), entry.is_directory()))
  13218. return;
  13219. }
  13220. };
  13221. const auto hits = lynx::searchFolderForMissing (missing, root.string(), list);
  13222. CHECK (hits.size() == 3);
  13223. CHECK (lynx::relocateByName (missing, hits) == 2);
  13224. CHECK (missing[0].found.find ("Kicks") != std::string::npos); // the copy in a "Kicks" folder matches better
  13225. CHECK (missing[1].found.find ("hat.wav") != std::string::npos);
  13226. CHECK (missing[2].found.empty());
  13227. // a bounded walk stops early; a cancelled one finds nothing
  13228. CHECK (lynx::searchFolderForMissing (missing, root.string(), list, 2).size() <= 1);
  13229. CHECK (lynx::searchFolderForMissing (missing, root.string(), list, 0, [] { return true; }).empty());
  13230. // nothing left to find: no walk at all
  13231. missing[2].skipped = true;
  13232. listed = 0;
  13233. CHECK (lynx::searchFolderForMissing (missing, root.string(), list).empty() && listed == 0);
  13234. fs::remove_all (root);
  13235. }
  13236. TEST (missing_files_folder_list)
  13237. {
  13238. const auto folders = lynx::parseFolderList ("C:\\A\r\n C:\\B \n\nc:\\a\nD:/x\n");
  13239. CHECK (folders.size() == 3 && folders[0] == "C:\\A" && folders[1] == "C:\\B" && folders[2] == "D:/x");
  13240. auto list = folders;
  13241. lynx::rememberFolder (list, "D:\\x");
  13242. CHECK (list.size() == 3 && list[0] == "D:\\x" && list[1] == "C:\\A");
  13243. lynx::rememberFolder (list, "E:\\new", 2);
  13244. CHECK (list.size() == 2 && list[0] == "E:\\new" && list[1] == "D:\\x");
  13245. CHECK (lynx::parseFolderList (lynx::folderListText (list)) == list);
  13246. }
  13247. TEST (missing_files_flp_report_and_relocation)
  13248. {
  13249. // an .flp sampler whose sample is gone: listed with its channel; a relocation map makes the import use the copy
  13250. flp::Project project;
  13251. flp::Channel ch;
  13252. ch.index = 0;
  13253. ch.type = flp::ChannelType::sampler;
  13254. ch.name = "Snare";
  13255. ch.samplePath = "C:\\Producer\\Packs\\snare 7.wav";
  13256. project.channels.push_back (ch);
  13257. lynx::FlpImportOptions options;
  13258. options.fileExists = [] (const std::string& p) { return p == "/found/snare 7.wav"; };
  13259. lynx::FlpImportReport report;
  13260. options.report = &report;
  13261. auto song = lynx::importFlp (project, options);
  13262. CHECK (report.missingFiles.size() == 1);
  13263. CHECK (report.missingFiles.size() == 1 && report.missingFiles[0].path == "C:\\Producer\\Packs\\snare 7.wav" && report.missingFiles[0].usedBy[0] == "Snare");
  13264. CHECK (song.channels.size() == 1 && song.channels[0].samplePath.empty());
  13265. lynx::FlpImportReport again;
  13266. options.report = &again;
  13267. options.relocations = { { "C:\\Producer\\Packs\\snare 7.wav", "/found/snare 7.wav" } };
  13268. song = lynx::importFlp (project, options);
  13269. CHECK (again.missingFiles.empty());
  13270. CHECK (song.channels.size() == 1 && song.channels[0].samplePath == "/found/snare 7.wav" && song.channels[0].instrument == lynx::InstrumentKind::sampler);
  13271. }